noutline.el 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051
  1. ;;; outline.el --- outline mode commands for Emacs
  2. ;; ----------------------------------------------------------------------
  3. ;; This is a port of GNU Emacs outline.el to XEmacs. The port was
  4. ;; done by Greg Chernov and is temporarily made available on the Org-mode
  5. ;; homepage http://www.astro.uva.nl/~dominik/Tools/org/, and as part
  6. ;; of the Org-mode distribution.
  7. ;; ----------------------------------------------------------------------
  8. ;; Copyright (C) 1986, 1993, 1994, 1995, 1997, 2000, 2001, 2002,
  9. ;; 2003, 2004, 2005 Free Software Foundation, Inc.
  10. ;; Maintainer: FSF
  11. ;; Keywords: outlines
  12. ;; This file is part of GNU Emacs.
  13. ;; GNU Emacs is free software; you can redistribute it and/or modify
  14. ;; it under the terms of the GNU General Public License as published by
  15. ;; the Free Software Foundation; either version 2, or (at your option)
  16. ;; any later version.
  17. ;; GNU Emacs is distributed in the hope that it will be useful,
  18. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. ;; GNU General Public License for more details.
  21. ;; You should have received a copy of the GNU General Public License
  22. ;; along with GNU Emacs; see the file COPYING. If not, write to the
  23. ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  24. ;; Boston, MA 02110-1301, USA.
  25. ;;; Commentary:
  26. ;; This package is a major mode for editing outline-format documents.
  27. ;; An outline can be `abstracted' to show headers at any given level,
  28. ;; with all stuff below hidden. See the Emacs manual for details.
  29. ;;; Todo:
  30. ;; - subtree-terminators
  31. ;; - better handle comments before function bodies (i.e. heading)
  32. ;; - don't bother hiding whitespace
  33. ;;; Code:
  34. (require 'xemacs)
  35. (require 'easymenu)
  36. ;; XEmacs and compatibility
  37. (defalias 'match-string-no-properties 'match-string)
  38. (if (not (fboundp 'add-to-invisibility-spec))
  39. (defun add-to-invisibility-spec (arg)
  40. "Add elements to `buffer-invisibility-spec'.
  41. See documentation for `buffer-invisibility-spec' for the kind of elements
  42. that can be added."
  43. (if (eq buffer-invisibility-spec t)
  44. (setq buffer-invisibility-spec (list t)))
  45. (setq buffer-invisibility-spec
  46. (cons arg buffer-invisibility-spec))))
  47. (if (not (fboundp 'remove-from-invisibility-spec))
  48. (defun remove-from-invisibility-spec (arg)
  49. "Remove elements from `buffer-invisibility-spec'."
  50. (if (consp buffer-invisibility-spec)
  51. (setq buffer-invisibility-spec
  52. (delete arg buffer-invisibility-spec)))))
  53. (defvar font-lock-warning-face)
  54. (defgroup outlines nil
  55. "Support for hierarchical outlining."
  56. :prefix "outline-"
  57. :group 'editing)
  58. (defcustom outline-regexp "[*\^L]+"
  59. "Regular expression to match the beginning of a heading.
  60. Any line whose beginning matches this regexp is considered to start a heading.
  61. Note that Outline mode only checks this regexp at the start of a line,
  62. so the regexp need not (and usually does not) start with `^'.
  63. The recommended way to set this is with a Local Variables: list
  64. in the file it applies to. See also `outline-heading-end-regexp'."
  65. :type '(choice regexp (const nil))
  66. :group 'outlines)
  67. (defcustom outline-heading-end-regexp "\n"
  68. "Regular expression to match the end of a heading line.
  69. You can assume that point is at the beginning of a heading when this
  70. regexp is searched for. The heading ends at the end of the match.
  71. The recommended way to set this is with a `Local Variables:' list
  72. in the file it applies to."
  73. :type 'regexp
  74. :group 'outlines)
  75. (defvar outline-mode-prefix-map
  76. (let ((map (make-sparse-keymap)))
  77. (define-key map "@" 'outline-mark-subtree)
  78. (define-key map "\C-n" 'outline-next-visible-heading)
  79. (define-key map "\C-p" 'outline-previous-visible-heading)
  80. (define-key map "\C-i" 'show-children)
  81. (define-key map "\C-s" 'show-subtree)
  82. (define-key map "\C-d" 'hide-subtree)
  83. (define-key map "\C-u" 'outline-up-heading)
  84. (define-key map "\C-f" 'outline-forward-same-level)
  85. (define-key map "\C-b" 'outline-backward-same-level)
  86. (define-key map "\C-t" 'hide-body)
  87. (define-key map "\C-a" 'show-all)
  88. (define-key map "\C-c" 'hide-entry)
  89. (define-key map "\C-e" 'show-entry)
  90. (define-key map "\C-l" 'hide-leaves)
  91. (define-key map "\C-k" 'show-branches)
  92. (define-key map "\C-q" 'hide-sublevels)
  93. (define-key map "\C-o" 'hide-other)
  94. (define-key map "\C-^" 'outline-move-subtree-up)
  95. (define-key map "\C-v" 'outline-move-subtree-down)
  96. (define-key map [(control ?<)] 'outline-promote)
  97. (define-key map [(control ?>)] 'outline-demote)
  98. (define-key map "\C-m" 'outline-insert-heading)
  99. ;; Where to bind outline-cycle ?
  100. map))
  101. (defvar outline-mode-menu-heading
  102. '("Headings"
  103. ["Up" outline-up-heading t]
  104. ["Next" outline-next-visible-heading t]
  105. ["Previous" outline-previous-visible-heading t]
  106. ["Next Same Level" outline-forward-same-level t]
  107. ["Previous Same Level" outline-backward-same-level t]
  108. ["New heading" outline-insert-heading t]
  109. ["Copy to kill ring" outline-headers-as-kill :active (region-active-p)]
  110. ["Move subtree up" outline-move-subtree-up t]
  111. ["Move subtree down" outline-move-subtree-down t]
  112. ["Promote subtree" outline-promote t]
  113. ["Demote subtree" outline-demote t]))
  114. (defvar outline-mode-menu-show
  115. '("Show"
  116. ["Show All" show-all t]
  117. ["Show Entry" show-entry t]
  118. ["Show Branches" show-branches t]
  119. ["Show Children" show-children t]
  120. ["Show Subtree" show-subtree t]))
  121. (defvar outline-mode-menu-hide
  122. '("Hide"
  123. ["Hide Leaves" hide-leaves t]
  124. ["Hide Body" hide-body t]
  125. ["Hide Entry" hide-entry t]
  126. ["Hide Subtree" hide-subtree t]
  127. ["Hide Other" hide-other t]
  128. ["Hide Sublevels" hide-sublevels t]))
  129. (defvar outline-mode-map
  130. (let ((map (make-sparse-keymap)))
  131. (define-key map "\C-c" outline-mode-prefix-map)
  132. map))
  133. (defvar outline-font-lock-keywords
  134. '(;;
  135. ;; Highlight headings according to the level.
  136. (eval . (list (concat "^\\(?:" outline-regexp "\\).+")
  137. 0 '(outline-font-lock-face) nil t)))
  138. "Additional expressions to highlight in Outline mode.")
  139. (defface outline-1
  140. '((t (:foreground "Blue1")))
  141. "Level 1."
  142. :group 'outlines)
  143. (defface outline-2
  144. '((t (:foreground "DarkGoldenrod")))
  145. "Level 2."
  146. :group 'outlines)
  147. (defface outline-3
  148. '((t (:foreground "Purple")))
  149. "Level 3."
  150. :group 'outlines)
  151. (defface outline-4
  152. '((t (:foreground "Firebrick")))
  153. "Level 4."
  154. :group 'outlines)
  155. (defface outline-5
  156. '((t (:foreground "ForestGreen")))
  157. "Level 5."
  158. :group 'outlines)
  159. (defface outline-6
  160. '((t (:foreground "CadetBlue")))
  161. "Level 6."
  162. :group 'outlines)
  163. (defface outline-7
  164. '((t (:foreground "Orchid")))
  165. "Level 7."
  166. :group 'outlines)
  167. (defface outline-8
  168. '((t (:foreground "RosyBrown")))
  169. "Level 8."
  170. :group 'outlines)
  171. (defvar outline-font-lock-faces
  172. [outline-1 outline-2 outline-3 outline-4
  173. outline-5 outline-6 outline-7 outline-8])
  174. (defvar outline-font-lock-levels nil)
  175. (make-variable-buffer-local 'outline-font-lock-levels)
  176. (defun outline-font-lock-face ()
  177. ;; (save-excursion
  178. ;; (outline-back-to-heading t)
  179. ;; (let* ((count 0)
  180. ;; (start-level (funcall outline-level))
  181. ;; (level start-level)
  182. ;; face-level)
  183. ;; (while (not (setq face-level
  184. ;; (if (or (bobp) (eq level 1)) 0
  185. ;; (cdr (assq level outline-font-lock-levels)))))
  186. ;; (outline-up-heading 1 t)
  187. ;; (setq count (1+ count))
  188. ;; (setq level (funcall outline-level)))
  189. ;; ;; Remember for later.
  190. ;; (unless (zerop count)
  191. ;; (setq face-level (+ face-level count))
  192. ;; (push (cons start-level face-level) outline-font-lock-levels))
  193. ;; (condition-case nil
  194. ;; (aref outline-font-lock-faces face-level)
  195. ;; (error font-lock-warning-face))))
  196. (save-excursion
  197. (goto-char (match-beginning 0))
  198. (looking-at outline-regexp)
  199. (condition-case nil
  200. (aref outline-font-lock-faces (1- (funcall outline-level)))
  201. (error font-lock-warning-face))))
  202. (defvar outline-view-change-hook nil
  203. "Normal hook to be run after outline visibility changes.")
  204. (defvar outline-mode-hook nil
  205. "This hook is run when outline mode starts.")
  206. (defvar outline-blank-line nil
  207. "Non-nil means to leave unhidden blank line before heading.")
  208. ;;;###autoload
  209. (define-derived-mode outline-mode text-mode "Outline"
  210. "Set major mode for editing outlines with selective display.
  211. Headings are lines which start with asterisks: one for major headings,
  212. two for subheadings, etc. Lines not starting with asterisks are body lines.
  213. Body text or subheadings under a heading can be made temporarily
  214. invisible, or visible again. Invisible lines are attached to the end
  215. of the heading, so they move with it, if the line is killed and yanked
  216. back. A heading with text hidden under it is marked with an ellipsis (...).
  217. Commands:\\<outline-mode-map>
  218. \\[outline-next-visible-heading] outline-next-visible-heading move by visible headings
  219. \\[outline-previous-visible-heading] outline-previous-visible-heading
  220. \\[outline-forward-same-level] outline-forward-same-level similar but skip subheadings
  221. \\[outline-backward-same-level] outline-backward-same-level
  222. \\[outline-up-heading] outline-up-heading move from subheading to heading
  223. \\[hide-body] make all text invisible (not headings).
  224. \\[show-all] make everything in buffer visible.
  225. \\[hide-sublevels] make only the first N levels of headers visible.
  226. The remaining commands are used when point is on a heading line.
  227. They apply to some of the body or subheadings of that heading.
  228. \\[hide-subtree] hide-subtree make body and subheadings invisible.
  229. \\[show-subtree] show-subtree make body and subheadings visible.
  230. \\[show-children] show-children make direct subheadings visible.
  231. No effect on body, or subheadings 2 or more levels down.
  232. With arg N, affects subheadings N levels down.
  233. \\[hide-entry] make immediately following body invisible.
  234. \\[show-entry] make it visible.
  235. \\[hide-leaves] make body under heading and under its subheadings invisible.
  236. The subheadings remain visible.
  237. \\[show-branches] make all subheadings at all levels visible.
  238. The variable `outline-regexp' can be changed to control what is a heading.
  239. A line is a heading if `outline-regexp' matches something at the
  240. beginning of the line. The longer the match, the deeper the level.
  241. Turning on outline mode calls the value of `text-mode-hook' and then of
  242. `outline-mode-hook', if they are non-nil."
  243. (make-local-variable 'line-move-ignore-invisible)
  244. (setq line-move-ignore-invisible t)
  245. ;; Cause use of ellipses for invisible text.
  246. (add-to-invisibility-spec '(outline . t))
  247. (easy-menu-add outline-mode-menu-heading)
  248. (easy-menu-add outline-mode-menu-show)
  249. (easy-menu-add outline-mode-menu-hide)
  250. (set (make-local-variable 'paragraph-start)
  251. (concat paragraph-start "\\|\\(?:" outline-regexp "\\)"))
  252. ;; Inhibit auto-filling of header lines.
  253. (set (make-local-variable 'auto-fill-inhibit-regexp) outline-regexp)
  254. (set (make-local-variable 'paragraph-separate)
  255. (concat paragraph-separate "\\|\\(?:" outline-regexp "\\)"))
  256. (set (make-local-variable 'font-lock-defaults)
  257. '(outline-font-lock-keywords t nil nil backward-paragraph))
  258. (setq imenu-generic-expression
  259. (list (list nil (concat "^\\(?:" outline-regexp "\\).*$") 0)))
  260. (add-hook 'change-major-mode-hook 'show-all nil t))
  261. (defcustom outline-minor-mode-prefix "\C-c@"
  262. "Prefix key to use for Outline commands in Outline minor mode.
  263. The value of this variable is checked as part of loading Outline mode.
  264. After that, changing the prefix key requires manipulating keymaps."
  265. :type 'string
  266. :group 'outlines)
  267. ;;;###autoload
  268. (define-minor-mode outline-minor-mode
  269. "Toggle Outline minor mode.
  270. With arg, turn Outline minor mode on if arg is positive, off otherwise.
  271. See the command `outline-mode' for more information on this mode."
  272. nil " Outl" (list (cons outline-minor-mode-prefix outline-mode-prefix-map))
  273. :group 'outlines
  274. (if outline-minor-mode
  275. (progn
  276. ;; Turn off this mode if we change major modes.
  277. (easy-menu-add outline-mode-menu-heading)
  278. (easy-menu-add outline-mode-menu-show)
  279. (easy-menu-add outline-mode-menu-hide)
  280. (add-hook 'change-major-mode-hook
  281. (lambda () (outline-minor-mode -1))
  282. nil t)
  283. (set (make-local-variable 'line-move-ignore-invisible) t)
  284. ;; Cause use of ellipses for invisible text.
  285. (add-to-invisibility-spec '(outline . t)))
  286. (easy-menu-remove outline-mode-menu-heading)
  287. (easy-menu-remove outline-mode-menu-show)
  288. (easy-menu-remove outline-mode-menu-hide)
  289. (setq line-move-ignore-invisible nil)
  290. ;; Cause use of ellipses for invisible text.
  291. (remove-from-invisibility-spec '(outline . t))
  292. ;; When turning off outline mode, get rid of any outline hiding.
  293. (show-all)))
  294. (defvar outline-level 'outline-level
  295. "Function of no args to compute a header's nesting level in an outline.
  296. It can assume point is at the beginning of a header line and that the match
  297. data reflects the `outline-regexp'.")
  298. (defvar outline-heading-alist ()
  299. "Alist associating a heading for every possible level.
  300. Each entry is of the form (HEADING . LEVEL).
  301. This alist is used two ways: to find the heading corresponding to
  302. a given level and to find the level of a given heading.
  303. If a mode or document needs several sets of outline headings (for example
  304. numbered and unnumbered sections), list them set by set and sorted by level
  305. within each set. For example in texinfo mode:
  306. (setq outline-heading-alist
  307. '((\"@chapter\" . 2) (\"@section\" . 3) (\"@subsection\" . 4)
  308. (\"@subsubsection\" . 5)
  309. (\"@unnumbered\" . 2) (\"@unnumberedsec\" . 3)
  310. (\"@unnumberedsubsec\" . 4) (\"@unnumberedsubsubsec\" . 5)
  311. (\"@appendix\" . 2) (\"@appendixsec\" . 3)...
  312. (\"@appendixsubsec\" . 4) (\"@appendixsubsubsec\" . 5) ..))
  313. Instead of sorting the entries in each set, you can also separate the
  314. sets with nil.")
  315. (make-variable-buffer-local 'outline-heading-alist)
  316. ;; This used to count columns rather than characters, but that made ^L
  317. ;; appear to be at level 2 instead of 1. Columns would be better for
  318. ;; tab handling, but the default regexp doesn't use tabs, and anyone
  319. ;; who changes the regexp can also redefine the outline-level variable
  320. ;; as appropriate.
  321. (defun outline-level ()
  322. "Return the depth to which a statement is nested in the outline.
  323. Point must be at the beginning of a header line.
  324. This is actually either the level specified in `outline-heading-alist'
  325. or else the number of characters matched by `outline-regexp'."
  326. (or (cdr (assoc (match-string 0) outline-heading-alist))
  327. (- (match-end 0) (match-beginning 0))))
  328. (defun outline-next-preface ()
  329. "Skip forward to just before the next heading line.
  330. If there's no following heading line, stop before the newline
  331. at the end of the buffer."
  332. (if (re-search-forward (concat "\n\\(?:" outline-regexp "\\)")
  333. nil 'move)
  334. (goto-char (match-beginning 0)))
  335. (if (and (bolp) (or outline-blank-line (eobp)) (not (bobp)))
  336. (forward-char -1)))
  337. (defun outline-next-heading ()
  338. "Move to the next (possibly invisible) heading line."
  339. (interactive)
  340. ;; Make sure we don't match the heading we're at.
  341. (if (and (bolp) (not (eobp))) (forward-char 1))
  342. (if (re-search-forward (concat "^\\(?:" outline-regexp "\\)")
  343. nil 'move)
  344. (goto-char (match-beginning 0))))
  345. (defun outline-previous-heading ()
  346. "Move to the previous (possibly invisible) heading line."
  347. (interactive)
  348. (re-search-backward (concat "^\\(?:" outline-regexp "\\)")
  349. nil 'move))
  350. (defsubst outline-invisible-p (&optional pos)
  351. "Non-nil if the character after point is invisible."
  352. (get-char-property (or pos (point)) 'invisible))
  353. (defun outline-visible ()
  354. (not (outline-invisible-p)))
  355. (make-obsolete 'outline-visible 'outline-invisible-p)
  356. (defun outline-back-to-heading (&optional invisible-ok)
  357. "Move to previous heading line, or beg of this line if it's a heading.
  358. Only visible heading lines are considered, unless INVISIBLE-OK is non-nil."
  359. (beginning-of-line)
  360. (or (outline-on-heading-p invisible-ok)
  361. (let (found)
  362. (save-excursion
  363. (while (not found)
  364. (or (re-search-backward (concat "^\\(?:" outline-regexp "\\)")
  365. nil t)
  366. (error "before first heading"))
  367. (setq found (and (or invisible-ok (not (outline-invisible-p)))
  368. (point)))))
  369. (goto-char found)
  370. found)))
  371. (defun outline-on-heading-p (&optional invisible-ok)
  372. "Return t if point is on a (visible) heading line.
  373. If INVISIBLE-OK is non-nil, an invisible heading line is ok too."
  374. (save-excursion
  375. (beginning-of-line)
  376. (and (bolp) (or invisible-ok (not (outline-invisible-p)))
  377. (looking-at outline-regexp))))
  378. (defun outline-insert-heading ()
  379. "Insert a new heading at same depth at point."
  380. (interactive)
  381. (let ((head (save-excursion
  382. (condition-case nil
  383. (outline-back-to-heading)
  384. (error (outline-next-heading)))
  385. (if (eobp)
  386. (or (caar outline-heading-alist) "")
  387. (match-string 0)))))
  388. (unless (or (string-match "[ \t]\\'" head)
  389. (not (string-match (concat "\\`\\(?:" outline-regexp "\\)")
  390. (concat head " "))))
  391. (setq head (concat head " ")))
  392. (unless (bolp) (end-of-line) (newline))
  393. (insert head)
  394. (unless (eolp)
  395. (save-excursion (newline-and-indent)))
  396. (run-hooks 'outline-insert-heading-hook)))
  397. (defun outline-invent-heading (head up)
  398. (save-match-data
  399. ;; Let's try to invent one by repeating or deleting the last char.
  400. (let ((new-head (if up (substring head 0 -1)
  401. (concat head (substring head -1)))))
  402. (if (string-match (concat "\\`\\(?:" outline-regexp "\\)")
  403. new-head)
  404. ;; Why bother checking that it is indeed higher/lower level ?
  405. new-head
  406. ;; Didn't work, so ask what to do.
  407. (read-string (format "%s heading for `%s': "
  408. (if up "Parent" "Demoted") head)
  409. head nil nil)))))
  410. (defun outline-promote (&optional children)
  411. "Promote headings higher up the tree.
  412. If prefix argument CHILDREN is given, promote also all the children.
  413. If the region is active in `transient-mark-mode', promote all headings
  414. in the region."
  415. (interactive
  416. (list (if (and zmacs-regions (region-active-p)) 'region
  417. (outline-back-to-heading)
  418. (if current-prefix-arg nil 'subtree))))
  419. (cond
  420. ((eq children 'region)
  421. (outline-map-region 'outline-promote (region-beginning) (region-end)))
  422. (children
  423. (outline-map-region 'outline-promote
  424. (point)
  425. (save-excursion (outline-get-next-sibling) (point))))
  426. (t
  427. (outline-back-to-heading t)
  428. (let* ((head (match-string-no-properties 0))
  429. (level (save-match-data (funcall outline-level)))
  430. (up-head (or (outline-head-from-level (1- level) head)
  431. ;; Use the parent heading, if it is really
  432. ;; one level less.
  433. (save-excursion
  434. (save-match-data
  435. (outline-up-heading 1 t)
  436. (and (= (1- level) (funcall outline-level))
  437. (match-string-no-properties 0))))
  438. ;; Bummer!! There is no lower level heading.
  439. (outline-invent-heading head 'up))))
  440. (unless (rassoc level outline-heading-alist)
  441. (push (cons head level) outline-heading-alist))
  442. (replace-match up-head nil t)))))
  443. (defun outline-demote (&optional children)
  444. "Demote headings lower down the tree.
  445. If prefix argument CHILDREN is given, demote also all the children.
  446. If the region is active in `transient-mark-mode', demote all headings
  447. in the region."
  448. (interactive
  449. (list (if (and zmacs-regions (region-active-p)) 'region
  450. (outline-back-to-heading)
  451. (if current-prefix-arg nil 'subtree))))
  452. (cond
  453. ((eq children 'region)
  454. (outline-map-region 'outline-demote (region-beginning) (region-end)))
  455. (children
  456. (outline-map-region 'outline-demote
  457. (point)
  458. (save-excursion (outline-get-next-sibling) (point))))
  459. (t
  460. (let* ((head (match-string-no-properties 0))
  461. (level (save-match-data (funcall outline-level)))
  462. (down-head
  463. (or (outline-head-from-level (1+ level) head)
  464. (save-excursion
  465. (save-match-data
  466. (while (and (progn (outline-next-heading) (not (eobp)))
  467. (<= (funcall outline-level) level)))
  468. (when (eobp)
  469. ;; Try again from the beginning of the buffer.
  470. (goto-char (point-min))
  471. (while (and (progn (outline-next-heading) (not (eobp)))
  472. (<= (funcall outline-level) level))))
  473. (unless (eobp)
  474. (looking-at outline-regexp)
  475. (match-string-no-properties 0))))
  476. ;; Bummer!! There is no higher-level heading in the buffer.
  477. (outline-invent-heading head nil))))
  478. (unless (rassoc level outline-heading-alist)
  479. (push (cons head level) outline-heading-alist))
  480. (replace-match down-head nil t)))))
  481. (defun outline-head-from-level (level head &optional alist)
  482. "Get new heading with level LEVEL from ALIST.
  483. If there are no such entries, return nil.
  484. ALIST defaults to `outline-heading-alist'.
  485. Similar to (car (rassoc LEVEL ALIST)).
  486. If there are several different entries with same new level, choose
  487. the one with the smallest distance to the assocation of HEAD in the alist.
  488. This makes it possible for promotion to work in modes with several
  489. independent sets of headings (numbered, unnumbered, appendix...)"
  490. (unless alist (setq alist outline-heading-alist))
  491. (let ((l (rassoc level alist))
  492. ll h hl l2 l2l)
  493. (cond
  494. ((null l) nil)
  495. ;; If there's no HEAD after L, any other entry for LEVEL after L
  496. ;; can't be much better than L.
  497. ((null (setq h (assoc head (setq ll (memq l alist))))) (car l))
  498. ;; If there's no other entry for LEVEL, just keep L.
  499. ((null (setq l2 (rassoc level (cdr ll)))) (car l))
  500. ;; Now we have L, L2, and H: see if L2 seems better than L.
  501. ;; If H is after L2, L2 is better.
  502. ((memq h (setq l2l (memq l2 (cdr ll))))
  503. (outline-head-from-level level head l2l))
  504. ;; Now we have H between L and L2.
  505. ;; If there's a separator between L and H, prefer L2.
  506. ((memq h (memq nil ll))
  507. (outline-head-from-level level head l2l))
  508. ;; If there's a separator between L2 and H, prefer L.
  509. ((memq l2 (memq nil (setq hl (memq h ll)))) (car l))
  510. ;; No separator between L and L2, check the distance.
  511. ((< (* 2 (length hl)) (+ (length ll) (length l2l)))
  512. (outline-head-from-level level head l2l))
  513. ;; If all else fails, just keep L.
  514. (t (car l)))))
  515. (defun outline-map-region (fun beg end)
  516. "Call FUN for every heading between BEG and END.
  517. When FUN is called, point is at the beginning of the heading and
  518. the match data is set appropriately."
  519. (save-excursion
  520. (setq end (copy-marker end))
  521. (goto-char beg)
  522. (when (re-search-forward (concat "^\\(?:" outline-regexp "\\)") end t)
  523. (goto-char (match-beginning 0))
  524. (funcall fun)
  525. (while (and (progn
  526. (outline-next-heading)
  527. (< (point) end))
  528. (not (eobp)))
  529. (funcall fun)))))
  530. ;; Vertical tree motion
  531. (defun outline-move-subtree-up (&optional arg)
  532. "Move the currrent subtree up past ARG headlines of the same level."
  533. (interactive "p")
  534. (outline-move-subtree-down (- arg)))
  535. (defun outline-move-subtree-down (&optional arg)
  536. "Move the currrent subtree down past ARG headlines of the same level."
  537. (interactive "p")
  538. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  539. 'outline-get-last-sibling))
  540. (ins-point (make-marker))
  541. (cnt (abs arg))
  542. (tmp-string "")
  543. beg end folded)
  544. ;; Select the tree
  545. (outline-back-to-heading)
  546. (setq beg (point))
  547. (save-match-data
  548. (save-excursion (outline-end-of-heading)
  549. (setq folded (outline-invisible-p)))
  550. (outline-end-of-subtree))
  551. (if (= (char-after) ?\n) (forward-char 1))
  552. (setq end (point))
  553. ;; Find insertion point, with error handling
  554. (goto-char beg)
  555. (while (> cnt 0)
  556. (or (funcall movfunc)
  557. (progn (goto-char beg)
  558. (error "Cannot move past superior level")))
  559. (setq cnt (1- cnt)))
  560. (if (> arg 0)
  561. ;; Moving forward - still need to move over subtree
  562. (progn (outline-end-of-subtree)
  563. (if (= (char-after) ?\n) (forward-char 1))))
  564. (move-marker ins-point (point))
  565. (setq tmp-string (buffer-substring beg end))
  566. (delete-region beg end)
  567. (insert tmp-string)
  568. (goto-char ins-point)
  569. (if folded (hide-subtree))
  570. (move-marker ins-point nil)))
  571. (defun outline-end-of-heading ()
  572. (if (re-search-forward outline-heading-end-regexp nil 'move)
  573. (forward-char -1)))
  574. (defun outline-next-visible-heading (arg)
  575. "Move to the next visible heading line.
  576. With argument, repeats or can move backward if negative.
  577. A heading line is one that starts with a `*' (or that
  578. `outline-regexp' matches)."
  579. (interactive "p")
  580. (if (< arg 0)
  581. (beginning-of-line)
  582. (end-of-line))
  583. (while (and (not (bobp)) (< arg 0))
  584. (while (and (not (bobp))
  585. (re-search-backward (concat "^\\(?:" outline-regexp "\\)")
  586. nil 'move)
  587. (outline-invisible-p)))
  588. (setq arg (1+ arg)))
  589. (while (and (not (eobp)) (> arg 0))
  590. (while (and (not (eobp))
  591. (re-search-forward (concat "^\\(?:" outline-regexp "\\)")
  592. nil 'move)
  593. (outline-invisible-p (match-beginning 0))))
  594. (setq arg (1- arg)))
  595. (beginning-of-line))
  596. (defun outline-previous-visible-heading (arg)
  597. "Move to the previous heading line.
  598. With argument, repeats or can move forward if negative.
  599. A heading line is one that starts with a `*' (or that
  600. `outline-regexp' matches)."
  601. (interactive "p")
  602. (outline-next-visible-heading (- arg)))
  603. (defun outline-mark-subtree ()
  604. "Mark the current subtree in an outlined document.
  605. This puts point at the start of the current subtree, and mark at the end."
  606. (interactive)
  607. (let ((beg))
  608. (if (outline-on-heading-p)
  609. ;; we are already looking at a heading
  610. (beginning-of-line)
  611. ;; else go back to previous heading
  612. (outline-previous-visible-heading 1))
  613. (setq beg (point))
  614. (outline-end-of-subtree)
  615. (push-mark (point) nil t)
  616. (goto-char beg)))
  617. (defvar outline-isearch-open-invisible-function nil
  618. "Function called if `isearch' finishes in an invisible overlay.
  619. The function is called with the overlay as its only argument.
  620. If nil, `show-entry' is called to reveal the invisible text.")
  621. (defun outline-discard-extents (&optional beg end)
  622. "Clear BEG and END of overlays whose property NAME has value VAL.
  623. Overlays might be moved and/or split.
  624. BEG and END default respectively to the beginning and end of buffer."
  625. (unless beg (setq beg (point-min)))
  626. (unless end (setq end (point-max)))
  627. (if (< end beg)
  628. (setq beg (prog1 end (setq end beg))))
  629. (save-excursion
  630. (map-extents
  631. #'(lambda (ex ignored)
  632. (if (< (extent-start-position ex) beg)
  633. (if (> (extent-end-position ex) end)
  634. (progn
  635. (set-extent-endpoints (copy-extent ex)
  636. (extent-start-position ex) beg)
  637. (set-extent-endpoints ex end (extent-end-position ex)))
  638. (set-extent-endpoints ex (extent-start-position ex) beg))
  639. (if (> (extent-end-position ex) end)
  640. (set-extent-endpoints ex end (extent-end-position ex))
  641. (delete-extent ex))))
  642. (current-buffer) beg end nil 'end-closed 'outline)))
  643. (defun outline-flag-region (from to flag)
  644. "Hide or show lines from FROM to TO, according to FLAG.
  645. If FLAG is nil then text is shown, while if FLAG is t the text is hidden."
  646. (when (< to from)
  647. (setq from (prog1 to (setq to from))))
  648. ;; first clear it all out
  649. (outline-discard-extents from to)
  650. (when flag
  651. (let ((ex (make-extent from to)))
  652. (set-extent-property ex 'invisible 'outline)
  653. (set-extent-property ex 'outline flag)
  654. ;; FIXME: I don't think XEmacs uses this, actually.
  655. (set-extent-property ex 'isearch-open-invisible
  656. (or outline-isearch-open-invisible-function
  657. 'outline-isearch-open-invisible))))
  658. ;; Seems only used by lazy-lock. I.e. obsolete.
  659. (run-hooks 'outline-view-change-hook))
  660. ;; Function to be set as an outline-isearch-open-invisible' property
  661. ;; to the overlay that makes the outline invisible (see
  662. ;; `outline-flag-region').
  663. (defun outline-isearch-open-invisible (overlay)
  664. ;; We rely on the fact that isearch places point on the matched text.
  665. (show-entry))
  666. (defun hide-entry ()
  667. "Hide the body directly following this heading."
  668. (interactive)
  669. (save-excursion
  670. (outline-back-to-heading)
  671. (outline-end-of-heading)
  672. (outline-flag-region (point) (progn (outline-next-preface) (point)) t)))
  673. (defun show-entry ()
  674. "Show the body directly following this heading.
  675. Show the heading too, if it is currently invisible."
  676. (interactive)
  677. (save-excursion
  678. (outline-back-to-heading t)
  679. (outline-flag-region (max 1 (1- (point)))
  680. (progn (outline-next-preface) (point)) nil)))
  681. (defun hide-body ()
  682. "Hide all body lines in buffer, leaving all headings visible."
  683. (interactive)
  684. (hide-region-body (point-min) (point-max)))
  685. (defun hide-region-body (start end)
  686. "Hide all body lines in the region, but not headings."
  687. ;; Nullify the hook to avoid repeated calls to `outline-flag-region'
  688. ;; wasting lots of time running `lazy-lock-fontify-after-outline'
  689. ;; and run the hook finally.
  690. (let (outline-view-change-hook)
  691. (save-excursion
  692. (save-restriction
  693. (narrow-to-region start end)
  694. (goto-char (point-min))
  695. (if (outline-on-heading-p)
  696. (outline-end-of-heading)
  697. (outline-next-preface))
  698. (while (not (eobp))
  699. (outline-flag-region (point)
  700. (progn (outline-next-preface) (point)) t)
  701. (unless (eobp)
  702. (forward-char (if (looking-at "\n\n") 2 1))
  703. (outline-end-of-heading))))))
  704. (run-hooks 'outline-view-change-hook))
  705. (defun show-all ()
  706. "Show all of the text in the buffer."
  707. (interactive)
  708. (outline-flag-region (point-min) (point-max) nil))
  709. (defun hide-subtree ()
  710. "Hide everything after this heading at deeper levels."
  711. (interactive)
  712. (outline-flag-subtree t))
  713. (defun hide-leaves ()
  714. "Hide all body after this heading at deeper levels."
  715. (interactive)
  716. (save-excursion
  717. (outline-back-to-heading)
  718. (outline-end-of-heading)
  719. (hide-region-body (point) (progn (outline-end-of-subtree) (point)))))
  720. (defun show-subtree ()
  721. "Show everything after this heading at deeper levels."
  722. (interactive)
  723. (outline-flag-subtree nil))
  724. (defun outline-show-heading ()
  725. "Show the current heading and move to its end."
  726. (outline-flag-region (- (point)
  727. (if (bobp) 0
  728. (if (and outline-blank-line
  729. (eq (char-before (1- (point))) ?\n))
  730. 2 1)))
  731. (progn (outline-end-of-heading) (point))
  732. nil))
  733. (defun hide-sublevels (levels)
  734. "Hide everything but the top LEVELS levels of headers, in whole buffer."
  735. (interactive "p")
  736. (if (< levels 1)
  737. (error "Must keep at least one level of headers"))
  738. (let (outline-view-change-hook)
  739. (save-excursion
  740. (goto-char (point-min))
  741. ;; Skip the prelude, if any.
  742. (unless (outline-on-heading-p t) (outline-next-heading))
  743. ;; First hide everything.
  744. (outline-flag-region (point) (point-max) t)
  745. ;; Then unhide the top level headers.
  746. (outline-map-region
  747. (lambda ()
  748. (if (<= (funcall outline-level) levels)
  749. (outline-show-heading)))
  750. (point) (point-max))))
  751. (run-hooks 'outline-view-change-hook))
  752. (defun hide-other ()
  753. "Hide everything except current body and parent and top-level headings."
  754. (interactive)
  755. (hide-sublevels 1)
  756. (let (outline-view-change-hook)
  757. (save-excursion
  758. (outline-back-to-heading t)
  759. (show-entry)
  760. (while (condition-case nil (progn (outline-up-heading 1 t) (not (bobp)))
  761. (error nil))
  762. (outline-flag-region (max 1 (1- (point)))
  763. (save-excursion (forward-line 1) (point))
  764. nil))))
  765. (run-hooks 'outline-view-change-hook))
  766. (defun outline-toggle-children ()
  767. "Show or hide the current subtree depending on its current state."
  768. (interactive)
  769. (save-excursion
  770. (outline-back-to-heading)
  771. (if (not (outline-invisible-p (point-at-eol)))
  772. (hide-subtree)
  773. (show-children)
  774. (show-entry))))
  775. (defun outline-flag-subtree (flag)
  776. (save-excursion
  777. (outline-back-to-heading)
  778. (outline-end-of-heading)
  779. (outline-flag-region (point)
  780. (progn (outline-end-of-subtree) (point))
  781. flag)))
  782. (defun outline-end-of-subtree ()
  783. (outline-back-to-heading)
  784. (let ((first t)
  785. (level (funcall outline-level)))
  786. (while (and (not (eobp))
  787. (or first (> (funcall outline-level) level)))
  788. (setq first nil)
  789. (outline-next-heading))
  790. (if (and (bolp) (not (eolp)))
  791. ;; We stopped at a nonempty line (the next heading).
  792. (progn
  793. ;; Go to end of line before heading
  794. (forward-char -1)
  795. (if (and outline-blank-line (bolp))
  796. ;; leave blank line before heading
  797. (forward-char -1))))))
  798. (defun show-branches ()
  799. "Show all subheadings of this heading, but not their bodies."
  800. (interactive)
  801. (show-children 1000))
  802. (defun show-children (&optional level)
  803. "Show all direct subheadings of this heading.
  804. Prefix arg LEVEL is how many levels below the current level should be shown.
  805. Default is enough to cause the following heading to appear."
  806. (interactive "P")
  807. (setq level
  808. (if level (prefix-numeric-value level)
  809. (save-excursion
  810. (outline-back-to-heading)
  811. (let ((start-level (funcall outline-level)))
  812. (outline-next-heading)
  813. (if (eobp)
  814. 1
  815. (max 1 (- (funcall outline-level) start-level)))))))
  816. (let (outline-view-change-hook)
  817. (save-excursion
  818. (outline-back-to-heading)
  819. (setq level (+ level (funcall outline-level)))
  820. (outline-map-region
  821. (lambda ()
  822. (if (<= (funcall outline-level) level)
  823. (outline-show-heading)))
  824. (point)
  825. (progn (outline-end-of-subtree)
  826. (if (eobp) (point-max) (1+ (point)))))))
  827. (run-hooks 'outline-view-change-hook))
  828. (defun outline-up-heading (arg &optional invisible-ok)
  829. "Move to the visible heading line of which the present line is a subheading.
  830. With argument, move up ARG levels.
  831. If INVISIBLE-OK is non-nil, also consider invisible lines."
  832. (interactive "p")
  833. (and (eq this-command 'outline-up-heading)
  834. (or (eq last-command 'outline-up-heading) (push-mark)))
  835. (outline-back-to-heading invisible-ok)
  836. (let ((start-level (funcall outline-level)))
  837. (if (eq start-level 1)
  838. (error "Already at top level of the outline"))
  839. (while (and (> start-level 1) (> arg 0) (not (bobp)))
  840. (let ((level start-level))
  841. (while (not (or (< level start-level) (bobp)))
  842. (if invisible-ok
  843. (outline-previous-heading)
  844. (outline-previous-visible-heading 1))
  845. (setq level (funcall outline-level)))
  846. (setq start-level level))
  847. (setq arg (- arg 1))))
  848. (looking-at outline-regexp))
  849. (defun outline-forward-same-level (arg)
  850. "Move forward to the ARG'th subheading at same level as this one.
  851. Stop at the first and last subheadings of a superior heading."
  852. (interactive "p")
  853. (outline-back-to-heading)
  854. (while (> arg 0)
  855. (let ((point-to-move-to (save-excursion
  856. (outline-get-next-sibling))))
  857. (if point-to-move-to
  858. (progn
  859. (goto-char point-to-move-to)
  860. (setq arg (1- arg)))
  861. (progn
  862. (setq arg 0)
  863. (error "No following same-level heading"))))))
  864. (defun outline-get-next-sibling ()
  865. "Move to next heading of the same level, and return point or nil if none."
  866. (let ((level (funcall outline-level)))
  867. (outline-next-visible-heading 1)
  868. (while (and (not (eobp)) (> (funcall outline-level) level))
  869. (outline-next-visible-heading 1))
  870. (if (or (eobp) (< (funcall outline-level) level))
  871. nil
  872. (point))))
  873. (defun outline-backward-same-level (arg)
  874. "Move backward to the ARG'th subheading at same level as this one.
  875. Stop at the first and last subheadings of a superior heading."
  876. (interactive "p")
  877. (outline-back-to-heading)
  878. (while (> arg 0)
  879. (let ((point-to-move-to (save-excursion
  880. (outline-get-last-sibling))))
  881. (if point-to-move-to
  882. (progn
  883. (goto-char point-to-move-to)
  884. (setq arg (1- arg)))
  885. (progn
  886. (setq arg 0)
  887. (error "No previous same-level heading"))))))
  888. (defun outline-get-last-sibling ()
  889. "Move to previous heading of the same level, and return point or nil if none."
  890. (let ((level (funcall outline-level)))
  891. (outline-previous-visible-heading 1)
  892. (while (and (> (funcall outline-level) level)
  893. (not (bobp)))
  894. (outline-previous-visible-heading 1))
  895. (if (< (funcall outline-level) level)
  896. nil
  897. (point))))
  898. (defun outline-headers-as-kill (beg end)
  899. "Save the visible outline headers in region at the start of the kill ring.
  900. Text shown between the headers isn't copied. Two newlines are
  901. inserted between saved headers. Yanking the result may be a
  902. convenient way to make a table of contents of the buffer."
  903. (interactive "r")
  904. (save-excursion
  905. (save-restriction
  906. (narrow-to-region beg end)
  907. (goto-char (point-min))
  908. (let ((buffer (current-buffer))
  909. start end)
  910. (with-temp-buffer
  911. (with-current-buffer buffer
  912. ;; Boundary condition: starting on heading:
  913. (when (outline-on-heading-p)
  914. (outline-back-to-heading)
  915. (setq start (point)
  916. end (progn (outline-end-of-heading)
  917. (point)))
  918. (insert-buffer-substring buffer start end)
  919. (insert "\n\n")))
  920. (let ((temp-buffer (current-buffer)))
  921. (with-current-buffer buffer
  922. (while (outline-next-heading)
  923. (unless (outline-invisible-p)
  924. (setq start (point)
  925. end (progn (outline-end-of-heading) (point)))
  926. (with-current-buffer temp-buffer
  927. (insert-buffer-substring buffer start end)
  928. (insert "\n\n"))))))
  929. (kill-new (buffer-string)))))))
  930. (provide 'outline)
  931. (provide 'noutline)
  932. ;; arch-tag: 1724410e-7d4d-4f46-b801-49e18171e874
  933. ;;; outline.el ends here