Skip to content

Instantly share code, notes, and snippets.

@alphapapa
Last active October 1, 2019 13:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alphapapa/968e22ef0f3873dcd4c5609bc6498a44 to your computer and use it in GitHub Desktop.
Save alphapapa/968e22ef0f3873dcd4c5609bc6498a44 to your computer and use it in GitHub Desktop.
Show Org outline tree in a side buffer
;; NOTE: This has been superseded by `org-sidebar-tree': https://github.com/alphapapa/org-sidebar#org-sidebar-tree-command
(defun ap/open-tree-view ()
"Open a clone of the current buffer to the left, resize it to
30 columns, and bind <mouse-1> to jump to the same position in
the base buffer."
;; http://emacs.stackexchange.com/questions/9530/how-can-i-get-an-org-mode-outline-in-a-2nd-buffer-as-a-dynamic-table-of-contents
;; TODO: Make this use navi-mode, which handles most of this already
(interactive)
(let ((new-buffer-name (concat "<tree>" (buffer-name))))
;; Create tree buffer
(split-window-right 60)
(if (get-buffer new-buffer-name)
;; Use existing tree buffer
(switch-to-buffer new-buffer-name)
(progn
;; Make new tree buffer
(clone-indirect-buffer new-buffer-name nil t)
(switch-to-buffer new-buffer-name)
(read-only-mode)
(hide-body)
(toggle-truncate-lines)
;; Do this twice in case the point is in a hidden line
(dotimes (_ 2 (forward-line 0)))
;; Map keys
(use-local-map (copy-keymap outline-mode-map))
(local-set-key (kbd "q") '(lambda ()
(interactive)
(let ((buffer (current-buffer)))
(delete-window)
(kill-buffer buffer))))
(mapc (lambda (key) (local-set-key (kbd key) 'ap/jump-to-point-and-show))
'("<mouse-1>" "RET"))))))
(defun ap/jump-to-point-and-show ()
"Switch to a cloned buffer's base buffer and move point to the
cursor position in the clone."
(interactive)
(let ((buffer (buffer-base-buffer)))
(unless buffer
(error "Must be in a cloned buffer."))
(when (and (outline-subheadings-p)
(not (outline-subheadings-visible-p)))
(outline-show-children))
(let ((pos (point))
(window (car (get-buffer-window-list buffer))))
(if window
(select-window window)
(other-window 1)
(switch-to-buffer buffer))
(widen)
(goto-char pos)
(org-show-entry)
(narrow-to-region (org-entry-beginning-position) (org-entry-end-position)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment