1

As my org files keep growing, it is getting more complicated to find what I'm looking for. For now I'm using helm-occur to find strings and helm-org-in-buffer-headings to find headings in org files. However I want to be able to limit the scope of the search to the current heading including subheadings.

I have found helm-org-parent-headings, but I couldn't find "helm-org-child-headings".

EDIT

The command org-narrow-to-subtree mentioned by Tobias is a good solution, however I'm looking for a command that does not change the contents of the org buffer (I still can't figure out how to return to the "original" buffer without killing the buffer created by org-narrow-to-subtree).

helm-org-parent-headings displays headings like helm-org-in-buffer-headings WITHOUT changing the buffer, but as the name suggests it only displays parents.

So what I am looking for is a command that does the opposite and only displays children of the heading in a helm buffer.

Furthermore I want to modify helm-occur to search only for strings, that belong to children of the heading at point.

  • 4
    Maybe you can use org-narrow-to-subtree. – Tobias Mar 21 '17 at 14:36
  • Thanks. But I'm looking for a solution similiar to helm-org-parent-headings. – bertfred Mar 21 '17 at 17:15
  • Explain what your comment means in the question body: Just what behavior are you looking for? Describe what helm-org-parent-headings does, for which you want something similar. – Drew Mar 21 '17 at 19:42
  • You need to "widen" the buffer to undo the narrowing. Either use: M-x widen or C-x n w You can narrow with C-x n s It looks like helm-org-in-buffer-headings temporarily widens your buffer, so you are going to have to write your own code to get what you want. – John Kitchin Mar 22 '17 at 10:46
  • As @JohnKitchin and @Tobias, I recommend to use narrowing. If you're struggeling with getting back to the original buffer (aka widening), I recommend to use narrow-or-widen-dwim that Artur Malabarba kindly shared in his blog some time ago: endlessparentheses.com/emacs-narrow-or-widen-dwim.html Narrowing and widening can then be done with one and the same key. Artur Malabarba binds it to C-x n, but it's easy to change this. – Timm Oct 18 '17 at 6:33
2

The following commands search within current heading or within current top level heading when called with prefix argument:

(require 'rx)
(require 'cl-lib)

(defun org-goto-top-level+ ()
  (let ((rx (rx bol "*" (one-or-more (not (any "*"))) eol)))
    (or  (looking-at rx)
         (re-search-backward rx nil t)
         (re-search-forward rx nil t))))

(defun helm-occur-current+ (arg)
  (interactive "P")
  (let ((opos (point)))
    (when (or (not arg)
              (org-goto-top-level+))
      (save-restriction
        (org-narrow-to-subtree)
        (helm-occur))
      (when (= helm-exit-status 1)
        (goto-char opos)))))

(defun helm-org-in-current-headings+ (arg)
  (interactive "P")
  (let ((opos (point)))
    (when (or (not arg)
              (org-goto-top-level+))
      (save-restriction
        (org-narrow-to-subtree)
        (cl-letf (((symbol-function #'buffer-base-buffer)
                   ;; see `helm-org--get-candidates-in-file'
                   ;; this prevents it from widening the buffer
                   (lambda (&rest _) t)))
          (helm-org-in-buffer-headings)))
      (when (= helm-exit-status 1)
        (goto-char opos)))))
  • Perfect. Thanks! – bertfred Aug 17 at 14:42
0

It isn't quite the same as helm-occur, but the agenda is a useful tool to find things. Agenda searches can be restricted to the current buffer or subtree using < in the agenda dispatcher. C-c a < < will restrict the agenda search to just the current subtree. You can search for tags, properties or just strings.

0

John Kitchin proposed in his comment that you should write your own function to get exactly what you want. I assume you want to call occur for the current sub-tree of org.

Maybe the following example is sufficient for you or it gives you a starter.

Just copy-paste the code in your emacs init file.

After evaluating the init file or after re-starting emacs you can place point in one of the headers of an org file and run M-x org-occur-children RET in the same way as you would run occur.

(require 'org)
(defun org-occur-children (regexp &optional nlines)
  "Run occur only in current org subtree.
See `occur' for the meaning of the arguments and for interactive calls."
  (interactive (occur-read-primary-args))
  (save-restriction
    (org-narrow-to-subtree)
    (funcall #'occur regexp nlines)))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.