org-mode: go back from sparse tree to previous visibility
Asked Answered
J

6

25

After filtering my document to a sparse tree, how do I undo the filter, going back to the previous view/visibility? Because I haven't worked out how to do this out I have to make everything visible, which isn't nice in big documents. Seems like it must be obvious but I can't find the answer...

Jilly answered 12/7, 2011 at 15:34 Comment(0)
M
4

This looks like it might be possible to me. In the following code I use some advice to save the outline state before generating the sparse tree, and to restore it when it is cleared via C-c C-c.

(setq lexical-binding t)

(let ((*outline-data* nil))
  (defun org-save-outline-state (&optional arg type)
    (setq *outline-data* (org-outline-overlay-data t)))

  (defun org-restore-outline-state (&optional arg)
    (when *outline-data*
      (org-set-outline-overlay-data *outline-data*)
      (setq *outline-data* nil))))

(advice-add 'org-sparse-tree :before 'org-save-outline-state)
(advice-add 'org-match-sparse-tree :before 'org-save-outline-state)
(advice-add 'org-ctrl-c-ctrl-c :after 'org-restore-outline-state)

It seems to do what you want.

Melanymelaphyre answered 24/5, 2017 at 12:46 Comment(1)
org-outline-overlay-data and org-set-outline-overlay-data have been removed from Orgmode: code.orgmode.org/bzg/org-mode/commit/…Resurrection
J
13

According to Bastien on the org mailing list, this isn't possible and is a long-standing wishlist item.

Jilly answered 13/7, 2011 at 12:49 Comment(0)
M
9

I'm not sure you can go back to the exact previous view. But one first step is C-c C-c which will remove temporary highlights/overlays from the current buffer.

Motherhood answered 12/7, 2011 at 17:1 Comment(1)
Thanks, but that doesn't restore the previous view. I want something like winner-mode for org.Jilly
M
4

This looks like it might be possible to me. In the following code I use some advice to save the outline state before generating the sparse tree, and to restore it when it is cleared via C-c C-c.

(setq lexical-binding t)

(let ((*outline-data* nil))
  (defun org-save-outline-state (&optional arg type)
    (setq *outline-data* (org-outline-overlay-data t)))

  (defun org-restore-outline-state (&optional arg)
    (when *outline-data*
      (org-set-outline-overlay-data *outline-data*)
      (setq *outline-data* nil))))

(advice-add 'org-sparse-tree :before 'org-save-outline-state)
(advice-add 'org-match-sparse-tree :before 'org-save-outline-state)
(advice-add 'org-ctrl-c-ctrl-c :after 'org-restore-outline-state)

It seems to do what you want.

Melanymelaphyre answered 24/5, 2017 at 12:46 Comment(1)
org-outline-overlay-data and org-set-outline-overlay-data have been removed from Orgmode: code.orgmode.org/bzg/org-mode/commit/…Resurrection
S
2

You can create two windows using C-x 2, then switch to the other window, do the sparse tree. When you are done simply go back to the original window and press C-x 1 to close the sparse tree window.

Shawana answered 26/1, 2013 at 12:54 Comment(1)
Yeah, that doesn't work. It's still the same buffer, just two different views. It is possible to create a indirect buffer (M-x make-indirect-buffer), enable org-mode in it and do the sparse tree there.Irritable
H
1

You probably cannot go back to the same view as before.

But there are 3 steps to remove the spare tree effects:

  1. org-agenda-remove-restriction-lock to remove the locked items' highlighted background.

  2. org-remove-occur-highlights to remove the prefix's highlight.

  3. widen to remove the restriction to the special entry, and expand to view the full file.

Hangup answered 21/8, 2015 at 7:14 Comment(0)
B
1

The solution is the "org-tree-to-indirect-buffer" function (it is bound to C-c C-x b). This function creates a new indirect buffer and restricts it to the current subtree. This new buffer must then be extended in the first step with the "widen" function (it is bound to C-x n w).

Then you can do whatever you want in this new buffer without affecting the visibility (narrowing, sparse tree, highlighting, ...) of the original buffer. So you can simply filter and edit your sparse tree in the new buffer.

And when you are done, close the second buffer and you are back in the original buffer you were in, thus returning to the previous view/visibility.

Of course, you can also create a new function yourself to automate these steps.

Bice answered 15/4, 2024 at 7:4 Comment(1)
Could that be made to work across several level 1 headings in a single file? I did try it with point at the top of the file (i.e. not on any heading), but it fails. And if I put it on the first heading in the file, the new buffer contains only that tree.Somnus

© 2022 - 2025 — McMap. All rights reserved.