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