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