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