I want to use org-mode's sparse trees to export some of the headings in a document. Specifically, I want headlines and entries for all matches of a tag search, plus parents. org-show-context-detail controls this, but doesn't have quite the right setting. local is closest, but local also includes the next heading at the same level of match; I do not want next heading.

* Heading
  :PROPERTIES:
  :EXPORT: yes
  :END:

  * Subheading
    :PROPERTIES:
    :EXPORT: yes
    :END:

    I am some content! I should be exported and visible!


  * Definitely Don't Want This Subheading
    :PROPERTIES:
    :EXPORT: NO
    :END

So how do I make and export a sparse tree using an EXPORT=yes tag search, containing Heading, Subheading, and the entry of Subheading, without including the next heading? (Note: I do know about C-v in the org export dispatcher -- but it will still include a visible-but-folded headline, like Definitely Don't Want This Subheading.)

I was just looking at this, ended up kludging a workaround.

According to http://orgmode.org/cgit.cgi/org-mode.git/plain/etc/ORG-NEWS:

org-show-following-heading, org-show-siblings, org-show-entry-below and org-show-hierarchy-above no longer exist. Instead, visibility is controlled through a single variable: org-show-context-detail.

But the values for for org-show-context-detail; minimal, local, ancestors, lineage, tree, canonical or t don't work. You want ancestors, but this doesn't show the entry. The entry text is shown for the 'local' value, but that shows the following headline.

My solution was to locate the function that does the display, org-show-set-visibility() in org.el, copy it wholesale to my init.el, and hack the line that decides whether to show the entry. I changed the check for 'local' to be 'ancestors'.

;; Show current heading and possibly its entry, following headline
;; or all children.
;;  (if (and (org-at-heading-p) (not (eq detail 'local)))  <-- ORIGINAL
  (if (and (org-at-heading-p) (not (eq detail 'ancestors)))    
      (org-flag-heading nil)
    (org-show-entry)

And by configuring the variable org-show-context-detail for tags-tree to be ancestors, again in init.el

(push '(tags-tree . ancestors) org-show-context-detail)

I can get sparse trees with the headline ancestry and the entries as well.

Hope this helps!

The answer by @Arb worked perfectly for me. However, it involves overwriting an internal function, which is dangerous since that function might be updated in a future version of Org. One solution is to use my library el-patch, which is specifically designed for this use case. Notice the invocation of el-patch-swap below which highlights the difference between the original version of the function and the new version:

(el-patch-defun org-show-set-visibility (detail)
  "Set visibility around point according to DETAIL.
DETAIL is either nil, `minimal', `local', `ancestors', `lineage',
`tree', `canonical' or t.  See `org-show-context-detail' for more
information."
  ;; Show current heading and possibly its entry, following headline
  ;; or all children.
  (if (and (org-at-heading-p) (not (eq detail (el-patch-swap
                                                'local
                                                'ancestors))))
      (org-flag-heading nil)
    (org-show-entry)
    ;; If point is hidden within a drawer or a block, make sure to
    ;; expose it.
    (dolist (o (overlays-at (point)))
      (when (memq (overlay-get o 'invisible) '(org-hide-block outline))
        (delete-overlay o)))
    (unless (org-before-first-heading-p)
      (org-with-limited-levels
       (cl-case detail
         ((tree canonical t) (org-show-children))
         ((nil minimal ancestors))
         (t (save-excursion
              (outline-next-heading)
              (org-flag-heading nil)))))))
  ;; Show all siblings.
  (when (eq detail 'lineage) (org-show-siblings))
  ;; Show ancestors, possibly with their children.
  (when (memq detail '(ancestors lineage tree canonical t))
    (save-excursion
      (while (org-up-heading-safe)
        (org-flag-heading nil)
        (when (memq detail '(canonical t)) (org-show-entry))
        (when (memq detail '(tree canonical t)) (org-show-children))))))

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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