emacsorg-mode

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


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...


Solution

  • 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.