2

I'm trying to export an org file into a file that contains only the visible entries (and parents) of a sparse tree (matching a specific tag).

For example, applying this to the following tree

* A
  Some content at level 0
** A1 :matchthis:
   Some content at level 1
** A2
* B
  Some content at level 0

matching the tag matchthis should produce the same result as exporting the tree:

* A
  Some content at level 0
** A1 :matchthis:
   Some content at level 1

If I export everything (in PDF, or txt), then the top level headlines (B) are also visible and therefore always included.

I don't see a way to export an org-file to an org-file with only the matched entries, which could be an acceptable intermediate step.

1 Answer 1

2

As the built-int sparse tree has some limitations I've made a custom sparse tree function. I think with this function you can do what you want.

First it makes the sparse tree, then it hides everything (this is needed as the sparse tree function calls org-overview which reveals all level 1 headings) to reveal only the matches and the parents. You can optionally also show the body of the matches and/or the parents.

For your example

  • call it with (my-org-custom-sparse-tree nil "matchthis" t t) to create the sparse tree
  • and then use (org-org-export-as-org nil nil t t) to export the visible part to an org buffer.

Note: This only works if org-highlight-sparse-tree-matches is set to t (it's the default) as the functions relies on the org-occur-highlights.

(defun my-org-custom-sparse-tree (todo-only match &optional
                                            match-body parents-body)
  "Create a custom sparse tree that only shows matched headings and parents.
For TODO-ONLY and MATCH see `org-match-sparse-tree'.
If MATCH-BODY is non-nil the bodies of the matches are shown.
If PARENTS-BODY is non-nil the bodies of the parents are shown."
  ;; Create the sparse tree.
  (org-match-sparse-tree todo-only match)
  (let ((pt-first (save-excursion
                    (org-first-headline-recenter)
                    (point)))
        (hls org-occur-highlights))
    ;; Hide everything.
    (outline-flag-region pt-first (point-max) t)
    ;; For each occur highlight overlay (the matches).
    (dolist (hl hls)
      (save-excursion
        (goto-char (overlay-start hl))
        ;; Unhide match.
        (outline-show-heading)
        (when match-body (outline-show-entry))
        ;; Unhide parents.
        (while (org-up-heading-safe)
          (outline-show-heading)
          (when parents-body (outline-show-entry))))))
  ;; Hide all archived subtrees again.
  (org-hide-archived-subtrees (point-min) (point-max)))
5
  • I need this feature, but this function is not work fine for me.thx
    – driftcrow
    Commented Nov 1, 2019 at 15:39
  • @driftcrow Can you let me know, what is not working fine so I can adapt the function if needed? How are you calling the function (parameters)? The function from my answer has been working for me so far.
    – Hubisan
    Commented Nov 2, 2019 at 18:02
  • @Hubsian I trace to function, after eval (org-match-sparse-tree todo-only match) ,the value of org-occur-highlights is nil, so after eval this function , all nodes has be hided.
    – driftcrow
    Commented Nov 3, 2019 at 4:02
  • @driftcrow hmm, does calling the built-in org-match-sparse-tree with your match work? what are u using as match? see org-manual
    – Hubisan
    Commented Nov 3, 2019 at 16:13
  • It is work fine now ,thanks @Hubisan . cause I use doom-config as my base config, for void org-indent-mode conflict , doom add a sentence (setq org-highlight-sparse-tree-matches nil) in hook ,so org-occur-highlights is nil
    – driftcrow
    Commented Nov 4, 2019 at 13:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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