r/orgmode • • Oct 01 '19

Finally solving the lack of a tree-view navigation window in Org

101 Upvotes

33 comments sorted by

20

u/github-alphapapa Oct 01 '19 edited Oct 01 '19

For a long time, I've wanted this feature in Org. Several years ago there was this Emacs.SE question and answer, and last year I copied some of my answer into this gist.

A couple of years ago this YouTube video demonstrated a similar workflow (recently mentioned here), but the associated configuration code is buried in a zip file on a web site, and it's not clear how to use it. It also doesn't treat the tree buffer specially, so it's easy to accidentally expand entry content, after which it isn't really a tree buffer anymore (unless you manually fix it).

So, finally, I've fixed up and released a solution in org-sidebar: org-sidebar-tree. It prevents accidentally expanding entry text, and you can flexibly display as much or as little of an entry as you want to see (see documentation).

Unlike imenu-list, the tree buffer is an indirect Org buffer, so you can still use it as an Org buffer, e.g. moving headings, changing to-do/priority, even typing new heading text, and it looks just like every other Org buffer, rather than having its own faces and formatting.

And since it's part of org-sidebar, it can also be displayed with other org-sidebar buffers: https://github.com/alphapapa/org-sidebar/raw/master/images/sidebar-with-tree.png

Another demo: https://github.com/alphapapa/org-sidebar/raw/master/images/tree2.gif

It seems to work well, but I'm sure wider usage will reveal some small fixes to be made. I'd appreciate any feedback.

2

u/im_not_juicing Oct 01 '19

Wow this is awesome ! Thank you!

5

u/tsdwm52 Oct 01 '19

Org-sidebar is changing my workflow in a very nice way.

3

u/github-alphapapa Oct 01 '19

I'd love to hear more about how you're using it. I try to make the defaults useful, but I can't anticipate how everyone will actually use it. :)

2

u/tsdwm52 Oct 02 '19

I used to have quite a few agenda files and a packed agenda that hindered as much as it helped me. Now, I keep just a few standard agenda files with birthdays, appointments, and a file with an entry for each open project. My agenda tells me if I have to go somewhere and which open projects I'm working on. Exactly what I want to know.

All the details in the various open project org mode files are now handled by org-subtree. When I'm working on a project, I find it very helpful to have the project specific todo list next to where I'm working in the org mode buffer. It keeps me focused and offers a convenient way to select and jump to the next task.

Thanks for your good work.

1

u/github-alphapapa Oct 02 '19

Thanks, I'm glad to hear that works for you.

1

u/publicvoit Oct 02 '19

Super cool!

I'll have to think about it.

So far, I'm using org-tree-to-indirect-buffer (C-c C-x b) to narrow down any sub-hierarchy when doing concentrated work with it. It's not related to navigation but has a similar effect.

Downside of org-tree-to-indirect-buffer is, that there can only be one such indirect buffer per Org mode file. So, my idea would have been to enable multiple indirect buffers for it.

1

u/github-alphapapa Oct 02 '19

Downside of org-tree-to-indirect-buffer is, that there can only be one such indirect buffer per Org mode file. So, my idea would have been to enable multiple indirect buffers for it.

You can change that by customizing org-indirect-buffer-display. However, I use my own function to instead of org-tree-to-indirect-buffer, which always does what I want:

(defun ap/org-tree-to-indirect-buffer (&optional arg)
  "Create indirect buffer and narrow it to current subtree.
The buffer is named after the subtree heading, with the filename
appended.  If a buffer by that name already exists, it is
selected instead of creating a new buffer."
  (interactive "P")
  (let* ((new-buffer-p)
         (pos (point))
         (buffer-name (let* ((heading (org-get-heading t t))
                             (level (org-outline-level))
                             (face (intern (concat "outline-" (number-to-string level))))
                             (heading-string (propertize (org-link-display-format heading)
                                                         'face face)))
                        (concat heading-string "::" (buffer-name))))
         (new-buffer (or (get-buffer buffer-name)
                         (prog1 (condition-case nil
                                    (make-indirect-buffer (current-buffer) buffer-name 'clone)
                                  (error (make-indirect-buffer (current-buffer) buffer-name)))
                           (setq new-buffer-p t)))))
    (switch-to-buffer new-buffer)
    (when new-buffer-p
      ;; I don't understand why setting the point again is necessary, but it is.
      (goto-char pos)
      (rename-buffer buffer-name)
      (org-narrow-to-subtree))))

(advice-add 'org-tree-to-indirect-buffer :override 'ap/org-tree-to-indirect-buffer)

I also put a function in org-sidebar, org-sidebar--subtree-buffer, which creates an indirect buffer for a subtree and returns the buffer, so you can do whatever you want with it.

2

u/publicvoit Oct 02 '19

You are my hero!

Thank you for this piece of elisp. I'll use it on a daily basis from now on.

2

u/fred982 Feb 24 '25

Thanks so much, this code was a great help !

2

u/juandenciso Oct 01 '19

How can I get this given I had org-sidebar installed previously? I cannot find the function in my emacs and I have just updated my packages.

2

u/github-alphapapa Oct 01 '19

Installing a newer version of package in a running Emacs instance does not automatically load the newer version into Emacs. Either restart Emacs, or try evaluating (load "org-sidebar").

If that doesn't work, ensure that you've deleted any old versions of the package, then restart Emacs.

2

u/ftrx Oct 04 '19

Oh, nice! Thanks for sharing.

It probably fit my new Zotero replacement workflow with a very big org-capture inverted datetree file and a separate "categories file", I'll have to see how to integrate it for easy navigation :-)

1

u/github-alphapapa Oct 04 '19

Let me know if you come up with something cool! BTW, you might want to customize the jump function to use indirect buffers.

1

u/[deleted] Oct 08 '19

Would it be possible to display a list (or hierarchy) of tags in the sidebar instead of headlines? It would be cool to be able to progressively narrow the buffer by selecting tags in a sidebar.

1

u/github-alphapapa Oct 08 '19

That's an interesting idea, but it would be completely different functionality. The sidebar tree-view buffer is an Org buffer; it's an indirect buffer to the actual source buffer.

You could build something like that based on org-ql-sparse-tree.

1

u/avkoval Oct 11 '19

Hi github-alphapapa - small feature request - can we have 'org-sidebar-tree-toggle` function? I would like to bind it to a key

1

u/itistheblurstoftimes Oct 16 '19

I accidentally ran this in an elisp buffer and was pleasantly surprised.

1

u/itistheblurstoftimes Oct 16 '19

One of the biggest issues I am seeing is that text entered in the body of a heading appears in the tree buffer. Do not yet have a view of how keep it from happening.

1

u/github-alphapapa Oct 16 '19

Yes, sometimes you have to re-cycle the display of the heading's visibility in the tree buffer to hide new entry text. I don't know if it's possible to fix that.

1

u/itistheblurstoftimes Oct 16 '19 edited Oct 16 '19

What about using font lock on the tree buffer? This is buggy, but seems promising. I have no idea if I am using font-lock properly, but regardless using overlays in the tree buffer seems the best way to change its appearance since they are not shared.

(setq org-sidebar-tree--hide-body-keyword
      `(("\\(^[^*]+\\)\n"
	 (0
	  (progn
	    (ov (match-beginning 1) (match-end 1) '(invisible t))
	    nil)))))
(font-lock-add-keywords nil org-sidebar-tree--hide-body-keyword)

1

u/github-alphapapa Oct 17 '19

That's a very cool idea, thanks. I'll make note of it and give it a try.

1

u/itistheblurstoftimes Oct 16 '19

Played with this and cannot get the regexp right, and every time I seem to make progress there is a new bug. I have a feeling you could figure out out in 1/10th the time I'd spend on it.

1

u/itistheblurstoftimes Oct 16 '19

"^\\([^*].+\\)$"

Is the best I have done. It seems to work for everything except that I occasionally get a double ellipsis in the tree buffer. Adding headings in the body buffer works, so long as there is not body text in the top level heading (of that buffer). Again, no idea if this is a good way to do it. I also switched to hiding text with (outline-hide-body) instead of creating an overlay. Again, a terrible use of font lock? Any feedback here would be great and I could continue to work on it.

1

u/github-alphapapa Oct 17 '19

Yeah, it's a cool idea, I'll have to try it out. font-locking is complicated, as you can see by the manual pages about it. I'm far from an expert on it, so I don't know if it's suitable here, but definitely worth a try.

2

u/itistheblurstoftimes Oct 17 '19

Check out org-fancy-priorities and org-bullets which both use it.

1

u/cowboycamilo Nov 17 '19

Hi u all, This looks awesome!. I'm new to the emacs world, so please be a little patient with me.. I installed it using MELPA, so I'm supposed to be good to go (restarted emacs), I can see that the package is loaded in my config file (I'm on spacemacs), but when I try to run the command org-sidebar-tree it isn't there... not sure what I'm doing wrong... hope you lot can help. Cheers.

https://imgur.com/3kY7e6r.png

https://imgur.com/YFin6Oi.png

1

u/github-alphapapa Nov 17 '19

Unfortunately, you'll need to ask other Spacemacs users for help with Spacemacs, because it does some things in non-standard ways. Try /r/Spacemacs.

2

u/mrwazsx Oct 01 '19

Incredible!