dajva / rg.el

Emacs search tool based on ripgrep
https://rgel.readthedocs.io
GNU General Public License v3.0
471 stars 38 forks source link

Automatically "reveal context" when opening matches in org buffers #86

Closed publicimageltd closed 4 years ago

publicimageltd commented 4 years ago

I use rg.el to search my zettelkasten, and I really like it (thanks for a great tool!). However, if I search the org files and want to enter the file where the match is, org often hides the match in a subtree due to its settings. Subtrees are shown narrowed by default. In the org universe, there is the possibility to "reveal context": I press C-c C-r, and the subtree where the match is is opened. This is even done automatically, i.e., when I jump from an entry in the agenda to the corresponding org file. (The variable is called org-show-context-detail). Now is there any possibility to automatically reveal context when entering an org buffer from rg.el? I have searched for hooks or the like, but didn't find any. I'm thinking of a simple function which is called when entering the buffer, something of this sort:

(when (derived-mode-p 'org-mode)
  (org-reveal))
dajva commented 4 years ago

Thanks!

rg.el is based on compilation-mode which is using next-error for jumping to source files. So next-error-hook should be the hook you are looking for. Something like this should work (minus any mistakes I made) if you only want it in rg result buffers.

(defun my-next-error-hook ()
  (message "my-next-error-hook called!"))

(defun my-add-next-error-hook ()
  (add-hook 'next-error-hook #'my-next-error-hook nil 'local))

(add-hook 'rg-mode-hook #'my-add-next-error-hook)

Skip the rg-mode-hook indirection if you want it for all compilation buffers like compile, grep etc.

publicimageltd commented 4 years ago

Perfect! I used the following code, and it works like a charm:

;; teach next-error, which is used by rg, to reveal org mode files:
(use-package compile
  :config
  (defun my-reveal-if-org ()
    "Call`org-reveal' if current buffer is an `org-mode' buffer."
    (when (derived-mode-p 'org-mode)
      (org-reveal)))
  (add-hook 'next-error-hook #'my-reveal-if-org))
dajva commented 4 years ago

Good that it works.