oantolin / embark

Emacs Mini-Buffer Actions Rooted in Keymaps

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Not possible to open grep location in other window and switch to it?

ktfleming opened this issue · comments

I'm trying to write an Embark action that will operate on a consult-grep location by jumping to it in another window. Here's the approach I'm taking:

(defun +embark-consult-ripgrep--other-window-do-it (dir location)
  "Jump to LOCATION, using DIR as the base directory.
Base directory here means that it will be used as the base for
creating an absolute file path, as the file path in LOCATION will
be relative (like some/dir/some/file.txt)."
  ;; Fortunately consult--grep-position allows you to provide a
  ;; find-file function. We'll use one that takes the relative
  ;; path from the location arg and expands it to an absolute path
  ;; before finding it. This is necessary because otherwise it
  ;; will try to find a file relative to the directory in the
  ;; buffer in the other-window, which could be anything.
  (let ((consult--buffer-display #'switch-to-buffer-other-window))
    (consult--jump (consult--grep-position location
                                           (lambda (filename)
                                             (find-file (expand-file-name filename dir)))))))

;; note: not an interactive command because 'location' is a string
;; with text properties -- see https://github.com/oantolin/embark/issues/495
(defun +embark-consult-ripgrep-other-window (location)
  "Jump to LOCATION in a window selected by ace-window."
  ;; default-directory will be whatever is used as the "base" for
  ;; consult-ripgrep. This will be the project root normally, or
  ;; whatever you select if called with a prefix arg. Pass this
  ;; along to be used as the base.
  (+embark-consult-ripgrep--other-window-do-it default-directory location))

(defvar-keymap embark-consult-grep-map
  :doc "Keymap for acting on consult-grep candidates"
  :parent embark-general-map
  "o" #'+embark-consult-ripgrep-other-window)

The issue I'm running into is that jumping to the location in the other window works, but then the original window (the one I called consult-ripgrep from) is selected, while I would like the other window (with the location I jumped to) to be selected. From what I can tell, this is because of this use of with-selected-window inside embark--act. I've tried using embark's around/post-action hooks to solve this, but as these hooks are run inside with-selected-window, they don't run late enough to prevent the original window from being selected again.

I might be missing a good way to do this -- any insights here would be appreciated! Thanks for all the great work on this package.