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