234
|
1 ;; Outline mode commands for Emacs
|
|
2 ;; Copyright (C) 1986 Free Software Foundation, Inc.
|
|
3
|
|
4 ;; This file is part of GNU Emacs.
|
|
5
|
|
6 ;; GNU Emacs is free software; you can redistribute it and/or modify
|
|
7 ;; it under the terms of the GNU General Public License as published by
|
|
8 ;; the Free Software Foundation; either version 1, or (at your option)
|
|
9 ;; any later version.
|
|
10
|
|
11 ;; GNU Emacs is distributed in the hope that it will be useful,
|
|
12 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
13 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
14 ;; GNU General Public License for more details.
|
|
15
|
|
16 ;; You should have received a copy of the GNU General Public License
|
|
17 ;; along with GNU Emacs; see the file COPYING. If not, write to
|
|
18 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
19
|
|
20 ;; Jan '86, Some new features added by Peter Desnoyers and rewritten by RMS.
|
|
21
|
|
22 (defvar outline-regexp "[*\^l]+"
|
|
23 "*Regular expression to match the beginning of a heading.
|
|
24 Any line whose beginning matches this regexp is considered to start a heading.
|
|
25 The recommended way to set this is with a Local Variables: list
|
|
26 in the file it applies to. See also outline-heading-end-regexp.")
|
|
27
|
|
28 (defvar outline-heading-end-regexp "[\n^M]"
|
|
29 "*Regular expression to match the end of a heading line.
|
|
30 You can assume that point is at the beginning of a heading when this
|
|
31 regexp is searched for. The heading ends at the end of the match.
|
|
32 The recommended way to set this is with a \"Local Variables:\" list
|
|
33 in the file it applies to.")
|
|
34
|
|
35 (defvar outline-mode-map nil "")
|
|
36
|
|
37 (if outline-mode-map
|
|
38 nil
|
|
39 (setq outline-mode-map (nconc (make-sparse-keymap) text-mode-map))
|
|
40 (define-key outline-mode-map "\C-c\C-n" 'outline-next-visible-heading)
|
|
41 (define-key outline-mode-map "\C-c\C-p" 'outline-previous-visible-heading)
|
|
42 (define-key outline-mode-map "\C-c\C-i" 'show-children)
|
|
43 (define-key outline-mode-map "\C-c\C-s" 'show-subtree)
|
|
44 (define-key outline-mode-map "\C-c\C-h" 'hide-subtree)
|
|
45 (define-key outline-mode-map "\C-c\C-u" 'outline-up-heading)
|
|
46 (define-key outline-mode-map "\C-c\C-f" 'outline-forward-same-level)
|
|
47 (define-key outline-mode-map "\C-c\C-b" 'outline-backward-same-level))
|
|
48
|
|
49 (defvar outline-minor-mode nil
|
|
50 "Non-nil if using Outline mode as a minor mode of some other mode.")
|
|
51 (setq minor-mode-alist (append minor-mode-alist
|
|
52 (list '(outline-minor-mode " Outl"))))
|
|
53
|
258
|
54 ;;;###autoload
|
234
|
55 (defun outline-mode ()
|
|
56 "Set major mode for editing outlines with selective display.
|
|
57 Headings are lines which start with asterisks: one for major headings,
|
|
58 two for subheadings, etc. Lines not starting with asterisks are body lines.
|
|
59
|
|
60 Body text or subheadings under a heading can be made temporarily
|
|
61 invisible, or visible again. Invisible lines are attached to the end
|
|
62 of the heading, so they move with it, if the line is killed and yanked
|
|
63 back. A heading with text hidden under it is marked with an ellipsis (...).
|
|
64
|
|
65 Commands:\\<outline-mode-map>
|
|
66 \\[outline-next-visible-heading] outline-next-visible-heading move by visible headings
|
|
67 \\[outline-previous-visible-heading] outline-previous-visible-heading
|
|
68 \\[outline-forward-same-level] outline-forward-same-level similar but skip subheadings
|
|
69 \\[outline-backward-same-level] outline-backward-same-level
|
|
70 \\[outline-up-heading] outline-up-heading move from subheading to heading
|
|
71
|
|
72 M-x hide-body make all text invisible (not headings).
|
|
73 M-x show-all make everything in buffer visible.
|
|
74
|
|
75 The remaining commands are used when point is on a heading line.
|
|
76 They apply to some of the body or subheadings of that heading.
|
|
77 \\[hide-subtree] hide-subtree make body and subheadings invisible.
|
|
78 \\[show-subtree] show-subtree make body and subheadings visible.
|
|
79 \\[show-children] show-children make direct subheadings visible.
|
|
80 No effect on body, or subheadings 2 or more levels down.
|
|
81 With arg N, affects subheadings N levels down.
|
|
82 M-x hide-entry make immediately following body invisible.
|
|
83 M-x show-entry make it visible.
|
|
84 M-x hide-leaves make body under heading and under its subheadings invisible.
|
|
85 The subheadings remain visible.
|
|
86 M-x show-branches make all subheadings at all levels visible.
|
|
87
|
|
88 The variable `outline-regexp' can be changed to control what is a heading.
|
|
89 A line is a heading if `outline-regexp' matches something at the
|
|
90 beginning of the line. The longer the match, the deeper the level.
|
|
91
|
|
92 Turning on outline mode calls the value of `text-mode-hook' and then of
|
|
93 `outline-mode-hook', if they are non-nil."
|
|
94 (interactive)
|
|
95 (kill-all-local-variables)
|
|
96 (setq selective-display t)
|
|
97 (use-local-map outline-mode-map)
|
|
98 (setq mode-name "Outline")
|
|
99 (setq major-mode 'outline-mode)
|
|
100 (define-abbrev-table 'text-mode-abbrev-table ())
|
|
101 (setq local-abbrev-table text-mode-abbrev-table)
|
|
102 (set-syntax-table text-mode-syntax-table)
|
|
103 (make-local-variable 'paragraph-start)
|
|
104 (setq paragraph-start (concat paragraph-start "\\|^\\("
|
|
105 outline-regexp "\\)"))
|
|
106 ;; Inhibit auto-filling of header lines.
|
|
107 (make-local-variable 'auto-fill-inhibit-regexp)
|
|
108 (setq auto-fill-inhibit-regexp outline-regexp)
|
|
109 (make-local-variable 'paragraph-separate)
|
|
110 (setq paragraph-separate (concat paragraph-separate "\\|^\\("
|
|
111 outline-regexp "\\)"))
|
|
112 (run-hooks 'text-mode-hook 'outline-mode-hook))
|
|
113
|
|
114 (defun outline-minor-mode (arg)
|
|
115 (interactive "P")
|
|
116 (setq outline-minor-mode
|
|
117 (if (null arg) (not outline-minor-mode)
|
|
118 (> (prefix-numeric-value arg) 0)))
|
|
119 (if outline-minor-mode
|
|
120 (progn
|
|
121 (setq selective-display t)
|
|
122 (make-local-variable 'outline-old-map)
|
|
123 (setq outline-old-map (current-local-map))
|
|
124 (let ((new-map (copy-keymap outline-old-map)))
|
|
125 (define-key new-map "\C-c"
|
|
126 (lookup-key outline-mode-map "\C-c"))
|
|
127 (use-local-map new-map))
|
|
128 (make-local-variable 'outline-regexp)
|
|
129 (setq outline-regexp "[ \t]*/\\*")
|
|
130 (make-local-variable 'outline-heading-end-regexp)
|
|
131 (setq outline-heading-end-regexp "\\*/[^\n\^M]*[\n\^M]")
|
|
132 (run-hooks 'outline-minor-mode-hook))
|
|
133 (progn
|
|
134 (setq selective-display nil)
|
|
135 (use-local-map outline-old-map))))
|
|
136
|
|
137 (defun outline-level ()
|
|
138 "Return the depth to which a statement is nested in the outline.
|
|
139 Point must be at the beginning of a header line. This is actually
|
|
140 the column number of the end of what `outline-regexp matches'."
|
|
141 (save-excursion
|
|
142 (looking-at outline-regexp)
|
|
143 (save-excursion (goto-char (match-end 0)) (current-column))))
|
|
144
|
|
145 (defun outline-next-preface ()
|
|
146 "Skip forward to just before the next heading line."
|
|
147 (if (re-search-forward (concat "[\n\^M]\\(" outline-regexp "\\)")
|
|
148 nil 'move)
|
|
149 (goto-char (match-beginning 0)))
|
|
150 (if (memq (preceding-char) '(?\n ?\^M))
|
|
151 (forward-char -1)))
|
|
152
|
|
153 (defun outline-next-heading ()
|
|
154 "Move to the next (possibly invisible) heading line."
|
|
155 (interactive)
|
|
156 (if (re-search-forward (concat "[\n\^M]\\(" outline-regexp "\\)")
|
|
157 nil 'move)
|
|
158 (goto-char (1+ (match-beginning 0)))))
|
|
159
|
|
160 (defun outline-back-to-heading ()
|
|
161 "Move to previous (possibly invisible) heading line,
|
|
162 or to the beginning of this line if it is a heading line."
|
|
163 (beginning-of-line)
|
|
164 (or (outline-on-heading-p)
|
|
165 (re-search-backward (concat "^\\(" outline-regexp "\\)") nil 'move)))
|
|
166
|
|
167 (defun outline-on-heading-p ()
|
|
168 "Return T if point is on a header line."
|
|
169 (save-excursion
|
|
170 (beginning-of-line)
|
|
171 (and (eq (preceding-char) ?\n)
|
|
172 (looking-at outline-regexp))))
|
|
173
|
|
174 (defun outline-end-of-heading ()
|
|
175 (if (re-search-forward outline-heading-end-regexp nil 'move)
|
|
176 (forward-char -1)))
|
|
177
|
|
178 (defun outline-next-visible-heading (arg)
|
|
179 "Move to the next visible heading line.
|
|
180 With argument, repeats or can move backward if negative.
|
|
181 A heading line is one that starts with a `*' (or that
|
|
182 `outline-regexp' matches)."
|
|
183 (interactive "p")
|
|
184 (if (< arg 0)
|
|
185 (beginning-of-line)
|
|
186 (end-of-line))
|
|
187 (re-search-forward (concat "^\\(" outline-regexp "\\)") nil nil arg)
|
|
188 (beginning-of-line))
|
|
189
|
|
190 (defun outline-previous-visible-heading (arg)
|
|
191 "Move to the previous heading line.
|
|
192 With argument, repeats or can move forward if negative.
|
|
193 A heading line is one that starts with a `*' (or that
|
|
194 `outline-regexp' matches)."
|
|
195 (interactive "p")
|
|
196 (outline-next-visible-heading (- arg)))
|
|
197
|
|
198 (defun outline-flag-region (from to flag)
|
|
199 "Hides or shows lines from FROM to TO, according to FLAG.
|
|
200 If FLAG is `\\n' (newline character) then text is shown,
|
|
201 while if FLAG is `\\^M' (control-M) the text is hidden."
|
|
202 (let ((modp (buffer-modified-p)))
|
|
203 (unwind-protect
|
|
204 (subst-char-in-region from to
|
|
205 (if (= flag ?\n) ?\^M ?\n)
|
|
206 flag)
|
|
207 (set-buffer-modified-p modp))))
|
|
208
|
|
209 (defun hide-entry ()
|
|
210 "Hide the body directly following this heading."
|
|
211 (interactive)
|
|
212 (outline-back-to-heading)
|
|
213 (outline-end-of-heading)
|
|
214 (save-excursion
|
|
215 (outline-flag-region (point) (progn (outline-next-preface) (point)) ?\^M)))
|
|
216
|
|
217 (defun show-entry ()
|
|
218 "Show the body directly following this heading."
|
|
219 (interactive)
|
|
220 (save-excursion
|
|
221 (outline-flag-region (point) (progn (outline-next-preface) (point)) ?\n)))
|
|
222
|
|
223 (defun hide-body ()
|
|
224 "Hide all of buffer except headings."
|
|
225 (interactive)
|
|
226 (hide-region-body (point-min) (point-max)))
|
|
227
|
|
228 (defun hide-region-body (start end)
|
|
229 "Hide all body lines in the region, but not headings."
|
|
230 (save-excursion
|
|
231 (save-restriction
|
|
232 (narrow-to-region start end)
|
|
233 (goto-char (point-min))
|
|
234 (if (outline-on-heading-p)
|
|
235 (outline-end-of-heading))
|
|
236 (while (not (eobp))
|
|
237 (outline-flag-region (point)
|
|
238 (progn (outline-next-preface) (point)) ?\^M)
|
|
239 (if (not (eobp))
|
|
240 (progn
|
|
241 (forward-char
|
|
242 (if (looking-at "[\n\^M][\n\^M]")
|
|
243 2 1))
|
|
244 (outline-end-of-heading)))))))
|
|
245
|
|
246 (defun show-all ()
|
|
247 "Show all of the text in the buffer."
|
|
248 (interactive)
|
|
249 (outline-flag-region (point-min) (point-max) ?\n))
|
|
250
|
|
251 (defun hide-subtree ()
|
|
252 "Hide everything after this heading at deeper levels."
|
|
253 (interactive)
|
|
254 (outline-flag-subtree ?\^M))
|
|
255
|
|
256 (defun hide-leaves ()
|
|
257 "Hide all body after this heading at deeper levels."
|
|
258 (interactive)
|
|
259 (outline-back-to-heading)
|
|
260 (outline-end-of-heading)
|
|
261 (hide-region-body (point) (progn (outline-end-of-subtree) (point))))
|
|
262
|
|
263 (defun show-subtree ()
|
|
264 "Show everything after this heading at deeper levels."
|
|
265 (interactive)
|
|
266 (outline-flag-subtree ?\n))
|
|
267
|
|
268 (defun outline-flag-subtree (flag)
|
|
269 (save-excursion
|
|
270 (outline-back-to-heading)
|
|
271 (outline-end-of-heading)
|
|
272 (outline-flag-region (point)
|
|
273 (progn (outline-end-of-subtree) (point))
|
|
274 flag)))
|
|
275
|
|
276 (defun outline-end-of-subtree ()
|
|
277 (outline-back-to-heading)
|
|
278 (let ((opoint (point))
|
|
279 (first t)
|
|
280 (level (outline-level)))
|
|
281 (while (and (not (eobp))
|
|
282 (or first (> (outline-level) level)))
|
|
283 (setq first nil)
|
|
284 (outline-next-heading))
|
|
285 (forward-char -1)
|
|
286 (if (memq (preceding-char) '(?\n ?\^M))
|
|
287 (forward-char -1))))
|
|
288
|
|
289 (defun show-branches ()
|
|
290 "Show all subheadings of this heading, but not their bodies."
|
|
291 (interactive)
|
|
292 (show-children 1000))
|
|
293
|
|
294 (defun show-children (&optional level)
|
|
295 "Show all direct subheadings of this heading.
|
|
296 Prefix arg LEVEL is how many levels below the current level should be shown.
|
|
297 Default is enough to cause the following heading to appear."
|
|
298 (interactive "P")
|
|
299 (setq level
|
|
300 (if level (prefix-numeric-value level)
|
|
301 (save-excursion
|
|
302 (beginning-of-line)
|
|
303 (let ((start-level (outline-level)))
|
|
304 (outline-next-heading)
|
|
305 (max 1 (- (outline-level) start-level))))))
|
|
306 (save-excursion
|
|
307 (save-restriction
|
|
308 (beginning-of-line)
|
|
309 (setq level (+ level (outline-level)))
|
|
310 (narrow-to-region (point)
|
|
311 (progn (outline-end-of-subtree) (1+ (point))))
|
|
312 (goto-char (point-min))
|
|
313 (while (and (not (eobp))
|
|
314 (progn
|
|
315 (outline-next-heading)
|
|
316 (not (eobp))))
|
|
317 (if (<= (outline-level) level)
|
|
318 (save-excursion
|
|
319 (outline-flag-region (save-excursion
|
|
320 (forward-char -1)
|
|
321 (if (memq (preceding-char) '(?\n ?\^M))
|
|
322 (forward-char -1))
|
|
323 (point))
|
|
324 (progn (outline-end-of-heading) (point))
|
|
325 ?\n)))))))
|
|
326
|
|
327 (defun outline-up-heading (arg)
|
|
328 "Move to the heading line of which the present line is a subheading.
|
|
329 With argument, move up ARG levels."
|
|
330 (interactive "p")
|
|
331 (outline-back-to-heading)
|
|
332 (if (eq (outline-level) 1)
|
|
333 (error ""))
|
|
334 (while (and (> (outline-level) 1)
|
|
335 (> arg 0)
|
|
336 (not (bobp)))
|
|
337 (let ((present-level (outline-level)))
|
|
338 (while (not (< (outline-level) present-level))
|
|
339 (outline-previous-visible-heading 1))
|
|
340 (setq arg (- arg 1)))))
|
|
341
|
|
342 (defun outline-forward-same-level (arg)
|
|
343 "Move forward to the ARG'th subheading from here of the same level as the
|
|
344 present one. It stops at the first and last subheadings of a superior heading."
|
|
345 (interactive "p")
|
|
346 (outline-back-to-heading)
|
|
347 (while (> arg 0)
|
|
348 (let ((point-to-move-to (save-excursion
|
|
349 (outline-get-next-sibling))))
|
|
350 (if point-to-move-to
|
|
351 (progn
|
|
352 (goto-char point-to-move-to)
|
|
353 (setq arg (1- arg)))
|
|
354 (progn
|
|
355 (setq arg 0)
|
|
356 (error ""))))))
|
|
357
|
|
358 (defun outline-get-next-sibling ()
|
|
359 "Position the point at the next heading of the same level,
|
|
360 and return that position or nil if it cannot be found."
|
|
361 (let ((level (outline-level)))
|
|
362 (outline-next-visible-heading 1)
|
|
363 (while (and (> (outline-level) level)
|
|
364 (not (eobp)))
|
|
365 (outline-next-visible-heading 1))
|
|
366 (if (< (outline-level) level)
|
|
367 nil
|
|
368 (point))))
|
|
369
|
|
370 (defun outline-backward-same-level (arg)
|
|
371 "Move backward to the ARG'th subheading from here of the same level as the
|
|
372 present one. It stops at the first and last subheadings of a superior heading."
|
|
373 (interactive "p")
|
|
374 (outline-back-to-heading)
|
|
375 (while (> arg 0)
|
|
376 (let ((point-to-move-to (save-excursion
|
|
377 (outline-get-last-sibling))))
|
|
378 (if point-to-move-to
|
|
379 (progn
|
|
380 (goto-char point-to-move-to)
|
|
381 (setq arg (1- arg)))
|
|
382 (progn
|
|
383 (setq arg 0)
|
|
384 (error ""))))))
|
|
385
|
|
386 (defun outline-get-last-sibling ()
|
|
387 "Position the point at the previous heading of the same level,
|
|
388 and return that position or nil if it cannot be found."
|
|
389 (let ((level (outline-level)))
|
|
390 (outline-previous-visible-heading 1)
|
|
391 (while (and (> (outline-level) level)
|
|
392 (not (bobp)))
|
|
393 (outline-previous-visible-heading 1))
|
|
394 (if (< (outline-level) level)
|
|
395 nil
|
|
396 (point))))
|
|
397
|