org-toc.el 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. ;;; org-toc.el --- Table of contents for Org-mode buffer
  2. ;; Copyright 2007 Bastien Guerry
  3. ;;
  4. ;; Author: Bastien Guerry <bzg AT altern DOT org>
  5. ;; Keywords: Org table of contents
  6. ;; Homepage: http://www.cognition.ens.fr/~guerry/u/org-toc.el
  7. ;; Version: 0.8
  8. ;; This program is free software; you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation; either version 3, or (at your option)
  11. ;; any later version.
  12. ;;
  13. ;; This program is distributed in the hope that it will be useful,
  14. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;; GNU General Public License for more details.
  17. ;;
  18. ;; You should have received a copy of the GNU General Public License
  19. ;; along with this program; if not, write to the Free Software
  20. ;; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. ;;; Commentary:
  22. ;; This library implements a browsable table of contents for Org files.
  23. ;; Put this file into your load-path and the following into your ~/.emacs:
  24. ;; (require 'org-toc)
  25. ;;; Code:
  26. (provide 'org-toc)
  27. (eval-when-compile
  28. (require 'cl))
  29. ;;; Custom variables:
  30. (defvar org-toc-base-buffer nil)
  31. (defvar org-toc-columns-shown nil)
  32. (defvar org-toc-odd-levels-only nil)
  33. (defvar org-toc-config-alist nil)
  34. (defvar org-toc-cycle-global-status nil)
  35. (defalias 'org-show-table-of-contents 'org-toc-show)
  36. (defgroup org-toc nil
  37. "Options concerning the browsable table of contents of Org-mode."
  38. :tag "Org TOC"
  39. :group 'org)
  40. (defcustom org-toc-default-depth 1
  41. "Default depth when invoking `org-toc-show' without argument."
  42. :group 'org-toc
  43. :type '(choice
  44. (const :tag "same as base buffer" nil)
  45. (integer :tag "level")))
  46. (defcustom org-toc-follow-mode nil
  47. "Non-nil means navigating through the table of contents will
  48. move the point in the Org buffer accordingly."
  49. :group 'org-toc
  50. :type 'boolean)
  51. (defcustom org-toc-info-mode nil
  52. "Non-nil means navigating through the table of contents will
  53. show the properties for the current headline in the echo-area."
  54. :group 'org-toc
  55. :type 'boolean)
  56. (defcustom org-toc-show-subtree-mode nil
  57. "Non-nil means show subtree when going to headline or following
  58. it while browsing the table of contents."
  59. :group 'org-toc
  60. :type '(choice
  61. (const :tag "show subtree" t)
  62. (const :tag "show entry" nil)))
  63. (defcustom org-toc-recenter-mode t
  64. "Non-nil means recenter the Org buffer when following the
  65. headlines in the TOC buffer."
  66. :group 'org-toc
  67. :type 'boolean)
  68. (defcustom org-toc-recenter 0
  69. "Where to recenter the Org buffer when unfolding a subtree.
  70. This variable is only used when `org-toc-recenter-mode' is set to
  71. 'custom. A value >=1000 will call recenter with no arg."
  72. :group 'org-toc
  73. :type 'integer)
  74. (defcustom org-toc-info-exclude '("ALLTAGS")
  75. "A list of excluded properties when displaying info in the
  76. echo-area. The COLUMNS property is always exluded."
  77. :group 'org-toc
  78. :type 'lits)
  79. ;;; Org TOC mode:
  80. (defvar org-toc-mode-map (make-sparse-keymap)
  81. "Keymap for `org-toc-mode'.")
  82. (defun org-toc-mode ()
  83. "A major mode for browsing the table of contents of an Org buffer.
  84. \\{org-toc-mode-map}"
  85. (interactive)
  86. (kill-all-local-variables)
  87. (use-local-map org-toc-mode-map)
  88. (setq mode-name "Org TOC")
  89. (setq major-mode 'org-toc-mode))
  90. ;; toggle modes
  91. (define-key org-toc-mode-map "f" 'org-toc-follow-mode)
  92. (define-key org-toc-mode-map "S" 'org-toc-show-subtree-mode)
  93. (define-key org-toc-mode-map "s" 'org-toc-store-config)
  94. (define-key org-toc-mode-map "g" 'org-toc-restore-config)
  95. (define-key org-toc-mode-map "i" 'org-toc-info-mode)
  96. (define-key org-toc-mode-map "r" 'org-toc-recenter-mode)
  97. ;; navigation keys
  98. (define-key org-toc-mode-map "p" 'org-toc-previous)
  99. (define-key org-toc-mode-map "n" 'org-toc-next)
  100. (define-key org-toc-mode-map [(left)] 'org-toc-previous)
  101. (define-key org-toc-mode-map [(right)] 'org-toc-next)
  102. (define-key org-toc-mode-map [(up)] 'org-toc-previous)
  103. (define-key org-toc-mode-map [(down)] 'org-toc-next)
  104. (define-key org-toc-mode-map "1" (lambda() (interactive) (org-toc-show 1 (point))))
  105. (define-key org-toc-mode-map "2" (lambda() (interactive) (org-toc-show 2 (point))))
  106. (define-key org-toc-mode-map "3" (lambda() (interactive) (org-toc-show 3 (point))))
  107. (define-key org-toc-mode-map "4" (lambda() (interactive) (org-toc-show 4 (point))))
  108. (define-key org-toc-mode-map " " 'org-toc-goto)
  109. (define-key org-toc-mode-map "q" 'org-toc-quit)
  110. (define-key org-toc-mode-map "x" 'org-toc-quit)
  111. ;; go to the location and stay in the base buffer
  112. (define-key org-toc-mode-map [(tab)] 'org-toc-jump)
  113. (define-key org-toc-mode-map "v" 'org-toc-jump)
  114. ;; go to the location and delete other windows
  115. (define-key org-toc-mode-map [(return)]
  116. (lambda() (interactive) (org-toc-jump t)))
  117. ;; special keys
  118. (define-key org-toc-mode-map "c" 'org-toc-columns)
  119. (define-key org-toc-mode-map "?" 'org-toc-help)
  120. (define-key org-toc-mode-map ":" 'org-toc-cycle-subtree)
  121. (define-key org-toc-mode-map "\C-c\C-o" 'org-open-at-point)
  122. ;; global cycling in the base buffer
  123. (define-key org-toc-mode-map (kbd "C-S-<iso-lefttab>")
  124. 'org-toc-cycle-base-buffer)
  125. ;; subtree cycling in the base buffer
  126. (define-key org-toc-mode-map [(control tab)]
  127. (lambda() (interactive) (org-toc-goto nil t)))
  128. ;;; Toggle functions:
  129. (defun org-toc-follow-mode ()
  130. "Toggle follow mode in a `org-toc-mode' buffer."
  131. (interactive)
  132. (setq org-toc-follow-mode (not org-toc-follow-mode))
  133. (message "Follow mode is %s"
  134. (if org-toc-follow-mode "on" "off")))
  135. (defun org-toc-info-mode ()
  136. "Toggle info mode in a `org-toc-mode' buffer."
  137. (interactive)
  138. (setq org-toc-info-mode (not org-toc-info-mode))
  139. (message "Info mode is %s"
  140. (if org-toc-info-mode "on" "off")))
  141. (defun org-toc-show-subtree-mode ()
  142. "Toggle show subtree mode in a `org-toc-mode' buffer."
  143. (interactive)
  144. (setq org-toc-show-subtree-mode (not org-toc-show-subtree-mode))
  145. (message "Show subtree mode is %s"
  146. (if org-toc-show-subtree-mode "on" "off")))
  147. (defun org-toc-recenter-mode (&optional line)
  148. "Toggle recenter mode in a `org-toc-mode' buffer. If LINE is
  149. specified, then make `org-toc-recenter' use this value."
  150. (interactive "P")
  151. (setq org-toc-recenter-mode (not org-toc-recenter-mode))
  152. (when (numberp line)
  153. (setq org-toc-recenter-mode t)
  154. (setq org-toc-recenter line))
  155. (message "Recenter mode is %s"
  156. (if org-toc-recenter-mode
  157. (format "on, line %d" org-toc-recenter) "off")))
  158. (defun org-toc-cycle-subtree ()
  159. "Locally cycle a headline through two states: 'children and
  160. 'folded"
  161. (interactive)
  162. (let ((beg (point))
  163. (end (save-excursion (end-of-line) (point)))
  164. (ov (car (org-overlays-at (point))))
  165. status)
  166. (if ov (setq status (org-overlay-get ov 'status))
  167. (setq ov (org-make-overlay beg end)))
  168. ;; change the folding status of this headline
  169. (cond ((or (null status) (eq status 'folded))
  170. (show-children)
  171. (message "CHILDREN")
  172. (org-overlay-put ov 'status 'children))
  173. ((eq status 'children)
  174. (show-branches)
  175. (message "BRANCHES")
  176. (org-overlay-put ov 'status 'branches))
  177. (t (hide-subtree)
  178. (message "FOLDED")
  179. (org-overlay-put ov 'status 'folded)))))
  180. ;;; Main show function:
  181. ;; FIXME name this org-before-first-heading-p?
  182. (defun org-toc-before-first-heading-p ()
  183. "Before first heading?"
  184. (save-excursion
  185. (null (re-search-backward "^\\*+ " nil t))))
  186. ;;;###autoload
  187. (defun org-toc-show (&optional depth position)
  188. "Show the table of contents of the current Org-mode buffer."
  189. (interactive "P")
  190. (if (org-mode-p)
  191. (progn (setq org-toc-base-buffer (current-buffer))
  192. (setq org-toc-odd-levels-only org-odd-levels-only))
  193. (if (eq major-mode 'org-toc-mode)
  194. (switch-to-buffer org-toc-base-buffer)
  195. (error "Not in an Org buffer")))
  196. ;; create the new window display
  197. (let ((pos (or position
  198. (save-excursion
  199. (if (org-toc-before-first-heading-p)
  200. (progn (re-search-forward "^\\*+ " nil t)
  201. (match-beginning 0))
  202. (point))))))
  203. (setq org-toc-cycle-global-status org-cycle-global-status)
  204. (delete-other-windows)
  205. (and (get-buffer "*org-toc*") (kill-buffer "*org-toc*"))
  206. (switch-to-buffer-other-window
  207. (make-indirect-buffer org-toc-base-buffer "*org-toc*"))
  208. ;; make content before 1st headline invisible
  209. (goto-char (point-min))
  210. (let* ((beg (point-min))
  211. (end (and (re-search-forward "^\\*" nil t)
  212. (1- (match-beginning 0))))
  213. (ov (org-make-overlay beg end))
  214. (help (format "Table of contents for %s (press ? for a quick help):\n"
  215. (buffer-name org-toc-base-buffer))))
  216. (org-overlay-put ov 'invisible t)
  217. (org-overlay-put ov 'before-string help))
  218. ;; build the browsable TOC
  219. (cond (depth
  220. (let* ((dpth (if org-toc-odd-levels-only
  221. (1- (* depth 2)) depth)))
  222. (org-content dpth)
  223. (setq org-toc-cycle-global-status
  224. `(org-content ,dpth))))
  225. ((null org-toc-default-depth)
  226. (if (eq org-toc-cycle-global-status 'overview)
  227. (progn (org-overview)
  228. (setq org-cycle-global-status 'overview)
  229. (run-hook-with-args 'org-cycle-hook 'overview))
  230. (progn (org-overview)
  231. ;; FIXME org-content to show only headlines?
  232. (org-content)
  233. (setq org-cycle-global-status 'contents)
  234. (run-hook-with-args 'org-cycle-hook 'contents))))
  235. (t (let* ((dpth0 org-toc-default-depth)
  236. (dpth (if org-toc-odd-levels-only
  237. (1- (* dpth0 2)) dpth0)))
  238. (org-content dpth)
  239. (setq org-toc-cycle-global-status
  240. `(org-content ,dpth)))))
  241. (goto-char pos))
  242. (move-beginning-of-line nil)
  243. (org-toc-mode)
  244. (shrink-window-if-larger-than-buffer)
  245. (setq buffer-read-only t))
  246. ;;; Navigation functions:
  247. (defun org-toc-goto (&optional jump cycle)
  248. "From Org TOC buffer, follow the targeted subtree in the Org window.
  249. If JUMP is non-nil, go to the base buffer.
  250. If JUMP is 'delete, go to the base buffer and delete other windows.
  251. If CYCLE is non-nil, cycle the targeted subtree in the Org window."
  252. (interactive)
  253. (let ((pos (point))
  254. (toc-buf (current-buffer)))
  255. (switch-to-buffer-other-window org-toc-base-buffer)
  256. (goto-char pos)
  257. (if cycle (org-cycle)
  258. (progn (org-overview)
  259. (if org-toc-show-subtree-mode
  260. (org-show-subtree)
  261. (org-show-entry))
  262. (org-show-context)))
  263. (if org-toc-recenter-mode
  264. (if (>= org-toc-recenter 1000) (recenter)
  265. (recenter org-toc-recenter)))
  266. (cond ((null jump)
  267. (switch-to-buffer-other-window toc-buf))
  268. ((eq jump 'delete)
  269. (delete-other-windows)))))
  270. (defun org-toc-cycle-base-buffer ()
  271. "Call `org-cycle' with a prefix argument in the base buffer."
  272. (interactive)
  273. (switch-to-buffer-other-window org-toc-base-buffer)
  274. (org-cycle t)
  275. (other-window 1))
  276. (defun org-toc-jump (&optional delete)
  277. "From Org TOC buffer, jump to the targeted subtree in the Org window.
  278. If DELETE is non-nil, delete other windows when in the Org buffer."
  279. (interactive "P")
  280. (if delete (org-toc-goto 'delete)
  281. (org-toc-goto t)))
  282. (defun org-toc-previous ()
  283. "Go to the previous headline of the TOC."
  284. (interactive)
  285. (if (save-excursion
  286. (beginning-of-line)
  287. (re-search-backward "^\\*" nil t))
  288. (outline-previous-visible-heading 1)
  289. (message "No previous heading"))
  290. (if org-toc-info-mode (org-toc-info))
  291. (if org-toc-follow-mode (org-toc-goto)))
  292. (defun org-toc-next ()
  293. "Go to the next headline of the TOC."
  294. (interactive)
  295. (outline-next-visible-heading 1)
  296. (if org-toc-info-mode (org-toc-info))
  297. (if org-toc-follow-mode (org-toc-goto)))
  298. (defun org-toc-quit ()
  299. "Quit the current Org TOC buffer."
  300. (interactive)
  301. (kill-this-buffer)
  302. (other-window 1)
  303. (delete-other-windows))
  304. ;;; Special functions:
  305. (defun org-toc-columns ()
  306. "Toggle columns view in the Org buffer from Org TOC."
  307. (interactive)
  308. (let ((indirect-buffer (current-buffer)))
  309. (switch-to-buffer org-toc-base-buffer)
  310. (if (not org-toc-columns-shown)
  311. (progn (org-columns)
  312. (setq org-toc-columns-shown t))
  313. (progn (org-columns-remove-overlays)
  314. (setq org-toc-columns-shown nil)))
  315. (switch-to-buffer indirect-buffer)))
  316. (defun org-toc-info ()
  317. "Show properties of current subtree in the echo-area."
  318. (interactive)
  319. (let ((pos (point))
  320. (indirect-buffer (current-buffer))
  321. props prop msg)
  322. (switch-to-buffer org-toc-base-buffer)
  323. (goto-char pos)
  324. (setq props (org-entry-properties))
  325. (while (setq prop (pop props))
  326. (unless (or (equal (car prop) "COLUMNS")
  327. (member (car prop) org-toc-info-exclude))
  328. (let ((p (car prop))
  329. (v (cdr prop)))
  330. (if (equal p "TAGS")
  331. (setq v (mapconcat 'identity (split-string v ":" t) " ")))
  332. (setq p (concat p ":"))
  333. (add-text-properties 0 (length p) '(face org-special-keyword) p)
  334. (setq msg (concat msg p " " v " ")))))
  335. (switch-to-buffer indirect-buffer)
  336. (message msg)))
  337. ;;; Store and restore TOC configuration:
  338. (defun org-toc-store-config ()
  339. "Store the current status of the tables of contents in
  340. `org-toc-config-alist'."
  341. (interactive)
  342. (let ((file (buffer-file-name org-toc-base-buffer))
  343. (pos (point))
  344. (hlcfg (org-toc-get-headlines-status)))
  345. (setq org-toc-config-alist
  346. (delete (assoc file org-toc-config-alist)
  347. org-toc-config-alist))
  348. (add-to-list 'org-toc-config-alist
  349. `(,file ,pos ,org-toc-cycle-global-status ,hlcfg))
  350. (message "TOC configuration saved: (%s)"
  351. (if (listp org-toc-cycle-global-status)
  352. (concat "org-content "
  353. (number-to-string
  354. (cadr org-toc-cycle-global-status)))
  355. (symbol-name org-toc-cycle-global-status)))))
  356. (defun org-toc-restore-config ()
  357. "Get the stored status in `org-toc-config-alist' and set the
  358. current table of contents to it."
  359. (interactive)
  360. (let* ((file (buffer-file-name org-toc-base-buffer))
  361. (conf (cdr (assoc file org-toc-config-alist)))
  362. (pos (car conf))
  363. (status (cadr conf))
  364. (hlcfg (caddr conf)) hlcfg0 ov)
  365. (cond ((listp status)
  366. (org-toc-show (cadr status) (point)))
  367. ((eq status 'overview)
  368. (org-overview)
  369. (setq org-cycle-global-status 'overview)
  370. (run-hook-with-args 'org-cycle-hook 'overview))
  371. (t
  372. (org-overview)
  373. (org-content)
  374. (setq org-cycle-global-status 'contents)
  375. (run-hook-with-args 'org-cycle-hook 'contents)))
  376. (while (setq hlcfg0 (pop hlcfg))
  377. (save-excursion
  378. (goto-char (point-min))
  379. (when (search-forward (car hlcfg0) nil t)
  380. (unless (org-overlays-at (match-beginning 0))
  381. (setq ov (org-make-overlay (match-beginning 0)
  382. (match-end 0))))
  383. (cond ((eq (cdr hlcfg0) 'children)
  384. (show-children)
  385. (message "CHILDREN")
  386. (org-overlay-put ov 'status 'children))
  387. ((eq (cdr hlcfg0) 'branches)
  388. (show-branches)
  389. (message "BRANCHES")
  390. (org-overlay-put ov 'status 'branches))))))
  391. (goto-char pos)
  392. (if org-toc-follow-mode (org-toc-goto))
  393. (message "Last TOC configuration restored")
  394. (sit-for 1)
  395. (if org-toc-info-mode (org-toc-info))))
  396. (defun org-toc-get-headlines-status ()
  397. "Return an alist of headlines and their associated folding
  398. status."
  399. (let (output ovs)
  400. (save-excursion
  401. (goto-char (point-min))
  402. (while (and (not (eobp))
  403. (goto-char (next-overlay-change (point))))
  404. (when (looking-at "^\\*+ ")
  405. (add-to-list
  406. 'output
  407. (cons (buffer-substring-no-properties
  408. (match-beginning 0)
  409. (save-excursion
  410. (end-of-line) (point)))
  411. (overlay-get
  412. (car (overlays-at (point))) 'status))))))
  413. ;; return an alist like (("* Headline" . 'status))
  414. output))
  415. ;; In Org TOC buffer, hide headlines below the first level.
  416. (defun org-toc-help ()
  417. "Display a quick help message in the echo-area for `org-toc-mode'."
  418. (interactive)
  419. (let ((st-start 0)
  420. (help-message
  421. "\[space\] show heading \[1-4\] hide headlines below this level
  422. \[TAB\] jump to heading \[f\] toggle follow mode (currently %s)
  423. \[return\] jump and delete others windows \[i\] toggle info mode (currently %s)
  424. \[S-TAB\] cycle subtree (in Org) \[S\] toggle show subtree mode (currently %s)
  425. \[C-S-TAB\] global cycle (in Org) \[r\] toggle recenter mode (currently %s)
  426. \[:\] cycle subtree (in TOC) \[c\] toggle column view (currently %s)
  427. \[n/p\] next/previous heading \[s\] save TOC configuration
  428. \[q\] quit the TOC \[g\] restore last TOC configuration"))
  429. (while (string-match "\\[[^]]+\\]" help-message st-start)
  430. (add-text-properties (match-beginning 0)
  431. (match-end 0) '(face bold) help-message)
  432. (setq st-start (match-end 0)))
  433. (message help-message
  434. (if org-toc-follow-mode "on" "off")
  435. (if org-toc-info-mode "on" "off")
  436. (if org-toc-show-subtree-mode "on" "off")
  437. (if org-toc-recenter-mode (format "on, line %s" org-toc-recenter) "off")
  438. (if org-toc-columns-shown "on" "off"))))
  439. ;;;;##########################################################################
  440. ;;;; User Options, Variables
  441. ;;;;##########################################################################
  442. ;;; org-toc.el ends here