Narrow to subtree with org-velocity

2019-08-05 05:21发布

问题:

I would like to use org-velocity as my primary means of navigating large .org files but with the following changes:

  1. After running a search in org-velocity, I would like the buffer to automatically narrow to that subtree, once I make my selection.

  2. Org-velocity should run its search against the entire file, even if the buffer is narrowed.

For part (1) I think something like this should work:

(add-hook 'org-follow-link-hook (lambda () (org-narrow-to-subtree)))

But this is not the right hook. Not sure how to approach (2). Any ideas? Thanks!

回答1:

I am writing a partial answer for part (2) of the question since the following does not fit nicely into a comment. Note, that I do not use org-velocity. So, the following is not really tested. save-restriction saves the current narrowing to the org-subtree and widen removes this narrowing temporarily during the search. To see exactly how it works read the help for the functions save-restriction and widen with C-h f and consult the info C-h i for elisp and there the Section "Advising functions".

(defadvice org-velocity (around search-all activate)
  "Widen for search with org-velocity"
  (save-restriction
    (widen)
    ad-do-it
    ))


回答2:

Okay, I think I have a complete solution!

  1. Make sure you have this fork of org-velocity installed:

    https://github.com/Fuco1/org-velocity

  2. Open your org-velocity.el file and replace lines 763-765 with this:

    (progn
      (with-current-buffer (org-velocity-match-buffer)
        (kill-buffer-and-window))
      (org-narrow-to-subtree)
      (show-all))))))
    

    The additional code tells org-velocity to first narrow the buffer to the selected subtree and secondly to expand that node.

  3. Put this code somewhere in your search path (init.el, .emacs, etc.)

    (defadvice org-velocity (around search-all activate)
       "Widen for search with org-velocity"
       (widen)
       ad-do-it)
    

    And that's it!

Thank you Tobias, Paul and Matúš for walking me through this!!

Take care,

-Adam