17

I use org-mode to handle my tasks and projects in multiple files.

In the weekly agenda, it is possible to jump to the location of each TODO entry using <TAB> or <RET>. If the target file was not previously open, it is loaded an the cursor is set to the correct headline and the entire document is unfolded, including drawers.

I would very much prefer to see only a sparse tree with everything but the correct headline folded (subtree visibility does not matter).

It is possible to collapse the entire tree by cycling global visibility using C-u <TAB, but then I have to find the heading again.

I know I can hide the rest by narrowing the buffer as described here: Emacs, How can I display only current task and hide others in org-mode? but then I loose context (parent heading, easy access to siblings) and the drawers are still open.

Ideally, I would like to have a command that shows the following:

  • The top level headings
  • The current headline, and all it's parents up to the top level
  • The current headline's children

Edit:

A slighty modified version of the functions user3173715 posted seems to do the trick:

(defun org-show-current-heading-tidily ()
  "Show next entry, keeping other entries closed."
  (if (save-excursion (end-of-line) (outline-invisible-p))
      (progn (org-show-entry) (show-children))
    (outline-back-to-heading)
    (unless (and (bolp) (org-on-heading-p))
      (org-up-heading-safe)
      (hide-subtree)
      (error "Boundary reached"))
    (org-overview)
    (org-reveal t)
    (org-show-entry)
    (show-children)))
Community
  • 1
  • 1
Patrick B.
  • 173
  • 1
  • 7
  • Not a full answer, but have a look at the following variables to see if they'll suit your needs: `org-startup-folded`, `org-startup-options`, and `org-agenda-inhibit-startup`. – Dan Aug 06 '14 at 14:28
  • With `org-agenda-inhibit-startup` toggled it almost does what I want, thank you! Strangely, the heading's siblings are hidden, and only the first child node is shown, but this is definitely an improvement! – Patrick B. Aug 06 '14 at 14:49
  • Without modifying the source code, there are a limited number of options for folding -- the manual only has a handful of interactive commands: https://www.gnu.org/software/emacs/manual/html_node/org/Visibility-cycling.html Another option to narrow what is visible would be to do a tag or keyword search: http://orgmode.org/worg/org-tutorials/advanced-searching.html The pleasure of successfully modifying the source code to create a custom view is long-lasting, but be prepared to spend an enormous amount of time if that is something which really interests you. – lawlist Aug 06 '14 at 15:46
  • This was pretty useful, but it hides the context. I'd like to show all the ancestors of the current task, their properties (folded, but shown), plus the siblings. Would that be possible? – Ivan Perez Dec 07 '17 at 09:32

4 Answers4

8

This is based on the answer in the edit in the actual question.

If of help to anyone: When I tried to bind the above to a hotkey, I kept getting an error, commandp wrong argument something something ... It turned out one had to add the (interactive) flag to make it work. Below is an example of the function tied to M-=

(defun org-show-current-heading-tidily ()
  (interactive)  ;Inteactive
  "Show next entry, keeping other entries closed."
  (if (save-excursion (end-of-line) (outline-invisible-p))
      (progn (org-show-entry) (show-children))
    (outline-back-to-heading)
    (unless (and (bolp) (org-on-heading-p))
      (org-up-heading-safe)
      (hide-subtree)
      (error "Boundary reached"))
    (org-overview)
    (org-reveal t)
    (org-show-entry)
    (show-children)))

(global-set-key "\M-=" 'org-show-current-heading-tidily)

@Patrick.B thanks for edit!

Leo Ufimtsev
  • 6,240
  • 5
  • 40
  • 48
  • This almost works for me - I found the siblings one level up are not visible, and had to do `C-u C-c C-r` to show everything. I confirmed that `M-: (org-reveal t)` does nothing, so it's evidently not the same as `C-u C-c C-r`. Using [this](https://stackoverflow.com/questions/6156286/emacs-lisp-call-function-with-prefix-argument-programmatically#comment7152611_615644) idea, I replaced with `(let ((current-prefix-arg '(4))) (call-interactively 'org-reveal))`, and this works. – Liam Feb 26 '22 at 15:52
  • 1
    Since the function does not make sense outside of org buffers, I replaced the `global-set-key` with a local key binding: `(add-hook 'org-mode-hook (lambda () (local-set-key "\M-=" 'org-show-current-heading-tidily)))`. Note you need to reload the org file for this to take effect. – Liam Feb 26 '22 at 16:18
4

Check your org startup options (customize-group > org-startup) like org-startup-folded or org-agenda-inhibit-startup (others have mentioned these already) and set the options to show only the folded view. Org mode variables like #+STARTUP are discussed here.

You may notice that everything is folded when you now jump to the agenda, even the parents of the active item may not be visible. You can then make the context (parents, children, next sibling) visible with org-reveal (C-c C-r as per the manual)

quazgar
  • 4,304
  • 2
  • 29
  • 41
  • Hurrah! I had the same question as OP, and your answer is hugely simple compared to the one that got accepted. This should be the accepted answer in my opinion @patrick-b – Johan Jan 15 '20 at 07:24
3

I am not very sure if this is your demand (I just think it is suitable for your question title), but I use these two functions with plenty of pleasures by binding them in the speed command of org-mode. You can find these two functions in org-mode hacks. I slightly modified them to meet my purposes.

The two functions support:

  1. Unfold every other headings except current heading
  2. Move current heading to top of screen for wider reading area.

In order to accomplish (2), you need to (setq recenter-positions '(top bottom)), there may be some better solutions, but I did not dig into it.

(defun ded/org-show-next-heading-tidily ()
  "Show next entry, keeping other entries closed."
  (if (save-excursion (end-of-line) (outline-invisible-p))
      (progn (org-show-entry) (show-children))
    (outline-next-heading)
    (unless (and (bolp) (org-on-heading-p))
      (org-up-heading-safe)
      (hide-subtree)
      (error "Boundary reached"))
    (org-overview)
    (org-reveal t)
    (org-show-entry)
    (recenter-top-bottom)
    (show-children)
    (recenter-top-bottom)))

(defun ded/org-show-previous-heading-tidily ()
  "Show previous entry, keeping other entries closed."
  (let ((pos (point)))
    (outline-previous-heading)
    (unless (and (< (point) pos) (bolp) (org-on-heading-p))
      (goto-char pos)
      (hide-subtree)
      (error "Boundary reached"))
    (org-overview)
    (org-reveal t)
    (org-show-entry)
    (recenter-top-bottom)
    (show-children)
    (recenter-top-bottom)))

And you can bind them with org-mode speed key with j and l, then you can use j and l to control the folding of headings when your cursor is in the beginning of headings.

(setq org-speed-commands-user
      '(("j" . ded/org-show-next-heading-tidily)
        ("l" . ded/org-show-previous-heading-tidily))))

It is perfect for reading org-mode files, cheers!

Sean Allred
  • 3,558
  • 3
  • 32
  • 71
Leu_Grady
  • 498
  • 3
  • 15
  • 2
    By simply replacing (outline-next-heading) with (outline-back-to-heading), I managed to make the first function do what I want. Speed keys and org-hacks are interesting too, thank you! – Patrick B. Aug 10 '14 at 22:57
3

Current (2022) emacs no longer requires elaborate setup to achieve the goals stated in the Question:

  • The top level headings
  • The current headline, and all it's parents up to the top level
  • The current headline's children

In the current Emacs and org-mode versions (Emacs 27.1, as of August 2022), you can press Shift-Tab Tab to close all headings, then open the current heading. Crucially, the cursor remains on the current collapsed heading so Tab reopens it.

The only significant change that I have from a default install is Evil, which may or may not affect the fact that the cursor remains on the collapsed heading.

dotancohen
  • 30,064
  • 36
  • 138
  • 197