Search code examples
emacsexpandorg-modeoutline

Emacs outline-mode auto expand upon grep


I'm using outline-mode (org-mode mostly) in emacs, and use grep-mode to search. What annoys me is when I follow the links in the grep buffer, the folded texts do not unfold. Is there a way to automate that?

A similar problem is to auto-unfold when goto-line is called, this can be achieved by define an advice to the goto-line function, however, defadvice for goto-line and goto-char does not work for mouse-events.

More generally, what happens when I click a link in emacs ? I tried to track down the function stack but can not find the source due to the event-mode used in emacs.


Solution

  • For the very first part of your question, here is the trickery I use. Comments follow.

    (setq org-directory "~/TOPDIR-OF-ORG-FILES")
    
    (global-set-key "\C-cog" 'fp-org-grep)
    
    (defun fp-org-grep (regexp)
      (interactive "MOrg grep? ")
      (if (string-equal regexp "")
          (error "Rien à trouver!")
        (require 'grep)
        (unless grep-find-template
          (grep-compute-defaults))
        (let ((grep-find-ignored-directories nil)
              (grep-find-ignored-files nil))
          (rgrep regexp "*.org" org-directory))
        (setq fp-org-isearch-string regexp)
        (save-excursion
          (set-buffer "*grep*")
          (setq next-error-function 'fp-org-grep-next-error))))
    
    (defun fp-org-grep-next-error (n &optional reset)
      (compilation-next-error-function n reset)
      (org-reveal)
      (setq isearch-string fp-org-isearch-string)
      (when (or (null search-ring)
                (not (string-equal (car search-ring) fp-org-isearch-string)))
        (push fp-org-isearch-string search-ring)))
    
    (defvar fp-org-isearch-string "")
    

    For speed, I only keep three Org files to my agenda, but define a "C-c o g" command to grep recursively through all my Org files. When you go to any entry through the usual means, the Org surrounding context gets revealed. You may also hit "C-s" to highlight the searched text.

    François