]> code.delx.au - gnu-emacs/blob - lisp/allout.el
(outline-init): Delete junk at end.
[gnu-emacs] / lisp / allout.el
1 ;;;_* allout.el - Extensive outline mode for use alone and with other modes.
2
3 ;;;_* Copyright (C) 1992, 1993, 1994 Free Software Foundation, Inc.
4
5 ;; Author: Ken Manheimer <klm@nist.gov>
6 ;; Maintainer: Ken Manheimer <klm@nist.gov>
7 ;; Created: Dec 1991 - first release to usenet
8 ;; Version: $Id: allout.el,v 1.7 1994/05/09 06:36:19 rms Exp rms $||
9 ;; Keywords: outline mode
10
11 ;; This file is part of GNU Emacs.
12
13 ;; GNU Emacs is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; any later version.
17
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
22
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING. If not, write to
25 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
26
27 ;;;_* Commentary:
28
29 ;; Allout outline mode provides extensive outline formatting and
30 ;; manipulation capabilities, subsuming and well beyond that of
31 ;; standard emacs outline mode. It is specifically aimed at
32 ;; supporting outline structuring and manipulation of syntax-
33 ;; sensitive text, eg programming languages. (For an example, see the
34 ;; allout code itself, which is organized in outline structure.)
35 ;;
36 ;; It also includes such things as topic-oriented repositioning, cut, and
37 ;; paste; integral outline exposure-layout; incremental search with
38 ;; dynamic exposure/conceament of concealed text; automatic topic-number
39 ;; maintenance; and many other features.
40 ;;
41 ;; See the docstring of the variables `outline-layout' and
42 ;; `outline-auto-activation' for details on automatic activation of
43 ;; allout outline-mode as a minor mode. (It has changed since allout
44 ;; 3.x, for those of you that depend on the old method.)
45 ;;
46 ;; Note - the lines beginning with ';;;_' are outline topic headers.
47 ;; Just 'ESC-x eval-current-buffer' to give it a whirl.
48
49 ;;Ken Manheimer 301 975-3539
50 ;;ken.manheimer@nist.gov FAX: 301 963-9137
51 ;;
52 ;;Computer Systems and Communications Division
53 ;;
54 ;; Nat'l Institute of Standards and Technology
55 ;; Technology A151
56 ;; Gaithersburg, MD 20899
57
58 ;;;_* Provide
59 (provide 'outline)
60 (provide 'allout)
61
62 ;;;_* USER CUSTOMIZATION VARIABLES:
63
64 ;;;_ + Layout, Mode, and Topic Header Configuration
65
66 ;;;_ = outline-auto-activation
67 (defvar outline-auto-activation nil
68 "*Regulates auto-activation modality of allout outlines - see `outline-init'.
69
70 Setq-default by `outline-init' to regulate whether or not allout
71 outline mode is automatically activated when the buffer-specific
72 variable `outline-layout' is non-nil, and whether or not the layout
73 dictated by `outline-layout' should be imposed on mode activation.
74
75 With value `t', auto-mode-activation and auto-layout are enabled.
76 \(This also depends on `outline-find-file-hooks' being installed in
77 `find-file-hooks', which is also done by `outline-init'.)
78
79 With value `ask', auto-mode-activation is enabled, and endorsement for
80 performing auto-layout is asked of the user each time.
81
82 With value `activate', only auto-mode-activation is enabled, auto-
83 layout is not.
84
85 With value `nil', neither auto-mode-activation nor auto-layout are
86 enabled.
87
88 See the docstring for `outline-init' for the proper interface to
89 this variable.")
90 ;;;_ = outline-layout
91 (defvar outline-layout nil
92 "*Layout specification and provisional mode trigger for allout outlines.
93
94 Buffer-specific.
95
96 A list value specifies a default layout for the current buffer, to be
97 applied upon activation of allout outline-mode. Any non-nil value
98 will automatically trigger allout outline-mode, provided `outline-
99 init' has been called to enable it.
100
101 See the docstring for `outline-init' for details on setting up for
102 auto-mode-activation, and for `outline-expose-topic' for the format of
103 the layout specification.
104
105 You can associate a particular outline layout with a file by setting
106 this var via the file's local variables. For example, the following
107 lines at the bottom of an elisp file:
108
109 ;;;Local variables:
110 ;;;outline-layout: \(0 : -1 -1 0\)
111 ;;;End:
112
113 will, modulo the above-mentioned conditions, cause the mode to be
114 activated when the file is visited, followed by the equivalent of
115 `\(outline-expose-topic 0 : -1 -1 0\)'. \(This is the layout used for
116 the allout.el, itself.)
117
118 Also, allout's mode-specific provisions will make topic prefixes
119 default to the comment-start string, if any, of the language of the
120 file. This is modulo the setting of `outline-use-mode-specific-
121 leader', which see.")
122 (make-variable-buffer-local 'outline-layout)
123
124 ;;;_ = outline-header-prefix
125 (defvar outline-header-prefix "."
126 "*Leading string which helps distinguish topic headers.
127
128 Outline topic header lines are identified by a leading topic
129 header prefix, which mostly have the value of this var at their front.
130 \(Level 1 topics are exceptions. They consist of only a single
131 character, which is typically set to the outline-primary-bullet. Many
132 outlines start at level 2 to avoid this discrepancy.")
133 (make-variable-buffer-local 'outline-header-prefix)
134 ;;;_ = outline-primary-bullet
135 (defvar outline-primary-bullet "*"
136 "Bullet used for top-level outline topics.
137
138 Outline topic header lines are identified by a leading topic header
139 prefix, which is concluded by bullets that includes the value of this
140 var and the respective outline-*-bullets-string vars.
141
142 The value of an asterisk ('*') provides for backwards compatability
143 with the original emacs outline mode. See outline-plain-bullets-string
144 and outline-distinctive-bullets-string for the range of available
145 bullets.")
146 (make-variable-buffer-local 'outline-primary-bullet)
147 ;;;_ = outline-plain-bullets-string
148 (defvar outline-plain-bullets-string (concat outline-primary-bullet
149 "+-:.;,")
150 "*The bullets normally used in outline topic prefixes.
151
152 See 'outline-distinctive-bullets-string' for the other kind of
153 bullets.
154
155 DO NOT include the close-square-bracket, ']', as a bullet.
156
157 Outline mode has to be reactivated in order for changes to the value
158 of this var to take effect.")
159 (make-variable-buffer-local 'outline-plain-bullets-string)
160 ;;;_ = outline-distinctive-bullets-string
161 (defvar outline-distinctive-bullets-string "=>([{}&!?#%\"X@$~\\"
162 "*Persistent outline header bullets used to distinguish special topics.
163
164 These bullets are not offered among the regular, level-specific
165 rotation, and are not altered by automatic rebulleting, as when
166 shifting the level of a topic. See `outline-plain-bullets-string' for
167 the selection of alternating bullets.
168
169 You must run 'set-outline-regexp' in order for changes
170 to the value of this var to effect outline-mode operation.
171
172 DO NOT include the close-square-bracket, ']', on either of the bullet
173 strings.")
174 (make-variable-buffer-local 'outline-distinctive-bullets-string)
175
176 ;;;_ = outline-use-mode-specific-leader
177 (defvar outline-use-mode-specific-leader t
178 "*When non-nil, use mode-specific topic-header prefixes.
179
180 Allout outline mode will use the mode-specific `outline-mode-leaders'
181 and/or comment-start string, if any, to lead the topic prefix string,
182 so topic headers look like comments in the programming language.
183
184 String values are used as they stand.
185
186 Value `t' means to first check for assoc value in `outline-mode-leaders'
187 alist, then use comment-start string, if any, then use default \(`.').
188 \(See note about use of comment-start strings, below.\)
189
190 Set to the symbol for either of `outline-mode-leaders' or
191 `comment-start' to use only one of them, respectively.
192
193 Value `nil' means to always use the default \(`.'\).
194
195 comment-start strings that do not end in spaces are tripled, and an
196 '_' underscore is tacked on the end, to distinguish them from regular
197 comment strings. comment-start strings that do end in spaces are not
198 tripled, but an underscore is substituted for the space. \[This
199 presumes that the space is for appearance, not comment syntax. You
200 can use `outline-mode-leaders' to override this behavior, when
201 incorrect.\]")
202 ;;;_ = outline-mode-leaders
203 (defvar outline-mode-leaders '()
204 "Specific outline-prefix leading strings per major modes.
205
206 Entries will be used in the stead (or lieu) of mode-specific
207 comment-start strings. See also `outline-use-mode-specific-leader'.
208
209 If you're constructing a string that will comment-out outline
210 structuring so it can be included in program code, append an extra
211 character, like an \"_\" underscore, to distinguish the lead string
212 from regular comments that start at bol.")
213
214 ;;;_ = outline-old-style-prefixes
215 (defvar outline-old-style-prefixes nil
216 "*When non-nil, use only old-and-crusty outline-mode '*' topic prefixes.
217
218 Non-nil restricts the topic creation and modification
219 functions to asterix-padded prefixes, so they look exactly
220 like the original emacs-outline style prefixes.
221
222 Whatever the setting of this variable, both old and new style prefixes
223 are always respected by the topic maneuvering functions.")
224 (make-variable-buffer-local 'outline-old-style-prefixes)
225 ;;;_ = outline-stylish-prefixes - alternating bullets
226 (defvar outline-stylish-prefixes t
227 "*Do fancy stuff with topic prefix bullets according to level, etc.
228
229 Non-nil enables topic creation, modification, and repositioning
230 functions to vary the topic bullet char (the char that marks the topic
231 depth) just preceding the start of the topic text) according to level.
232 Otherwise, only asterisks ('*') and distinctive bullets are used.
233
234 This is how an outline can look (but sans indentation) with stylish
235 prefixes:
236
237 * Top level
238 .* A topic
239 . + One level 3 subtopic
240 . . One level 4 subtopic
241 . . A second 4 subtopic
242 . + Another level 3 subtopic
243 . #1 A numbered level 4 subtopic
244 . #2 Another
245 . ! Another level 4 subtopic with a different distinctive bullet
246 . #4 And another numbered level 4 subtopic
247
248 This would be an outline with stylish prefixes inhibited (but the
249 numbered and other distinctive bullets retained):
250
251 * Top level
252 .* A topic
253 . * One level 3 subtopic
254 . * One level 4 subtopic
255 . * A second 4 subtopic
256 . * Another level 3 subtopic
257 . #1 A numbered level 4 subtopic
258 . #2 Another
259 . ! Another level 4 subtopic with a different distinctive bullet
260 . #4 And another numbered level 4 subtopic
261
262 Stylish and constant prefixes (as well as old-style prefixes) are
263 always respected by the topic maneuvering functions, regardless of
264 this variable setting.
265
266 The setting of this var is not relevant when outline-old-style-prefixes
267 is non-nil.")
268 (make-variable-buffer-local 'outline-stylish-prefixes)
269
270 ;;;_ = outline-numbered-bullet
271 (defvar outline-numbered-bullet "#"
272 "*String designating bullet of topics that have auto-numbering; nil for none.
273
274 Topics having this bullet have automatic maintainence of a sibling
275 sequence-number tacked on, just after the bullet. Conventionally set
276 to \"#\", you can set it to a bullet of your choice. A nil value
277 disables numbering maintainence.")
278 (make-variable-buffer-local 'outline-numbered-bullet)
279 ;;;_ = outline-file-xref-bullet
280 (defvar outline-file-xref-bullet "@"
281 "*Bullet signifying file cross-references, for `outline-resolve-xref'.
282
283 Set this var to the bullet you want to use for file cross-references.
284 Set it 'nil' if you want to inhibit this capability.")
285
286 ;;;_ + LaTeX formatting
287 ;;;_ - outline-number-pages
288 (defvar outline-number-pages nil
289 "*Non-nil turns on page numbering for LaTeX formatting of an outline.")
290 ;;;_ - outline-label-style
291 (defvar outline-label-style "\\large\\bf"
292 "*Font and size of labels for LaTeX formatting of an outline.")
293 ;;;_ - outline-head-line-style
294 (defvar outline-head-line-style "\\large\\sl "
295 "*Font and size of entries for LaTeX formatting of an outline.")
296 ;;;_ - outline-body-line-style
297 (defvar outline-body-line-style " "
298 "*Font and size of entries for LaTeX formatting of an outline.")
299 ;;;_ - outline-title-style
300 (defvar outline-title-style "\\Large\\bf"
301 "*Font and size of titles for LaTeX formatting of an outline.")
302 ;;;_ - outline-title
303 (defvar outline-title '(or buffer-file-name (current-buffer-name))
304 "*Expression to be evaluated to determine the title for LaTeX
305 formatted copy.")
306 ;;;_ - outline-line-skip
307 (defvar outline-line-skip ".05cm"
308 "*Space between lines for LaTeX formatting of an outline.")
309 ;;;_ - outline-indent
310 (defvar outline-indent ".3cm"
311 "*LaTeX formatted depth-indent spacing.")
312
313 ;;;_ + Miscellaneous customization
314
315 ;;;_ = outline-keybindings-list
316 ;;; You have to reactivate outline-mode - '(outline-mode t)' - to
317 ;;; institute changes to this var.
318 (defvar outline-keybindings-list ()
319 "*List of outline-mode key / function bindings.
320
321 These bindings will be locally bound on the outline-mode-map. The
322 keys will be prefixed by outline-command-prefix, unless the cell
323 contains a third, no-nil element, in which case the initial string
324 will be used as is.")
325 (setq outline-keybindings-list
326 '(
327 ; Motion commands:
328 ("?t" outline-latexify-exposed)
329 ("\C-n" outline-next-visible-heading)
330 ("\C-p" outline-previous-visible-heading)
331 ("\C-u" outline-up-current-level)
332 ("\C-f" outline-forward-current-level)
333 ("\C-b" outline-backward-current-level)
334 ("\C-a" outline-beginning-of-current-entry)
335 ("\C-e" outline-end-of-current-entry)
336 ;;("\C-n" outline-next-line-or-topic)
337 ;;("\C-p" outline-previous-line-or-topic)
338 ; Exposure commands:
339 ("\C-i" outline-show-children)
340 ("\C-s" outline-show-current-subtree)
341 ("\C-h" outline-hide-current-subtree)
342 ("\C-o" outline-show-current-entry)
343 ("!" outline-show-all)
344 ; Alteration commands:
345 (" " outline-open-sibtopic)
346 ("." outline-open-subtopic)
347 ("," outline-open-supertopic)
348 ("'" outline-shift-in)
349 (">" outline-shift-in)
350 ("<" outline-shift-out)
351 ("\C-m" outline-rebullet-topic)
352 ("b" outline-rebullet-current-heading)
353 ("#" outline-number-siblings)
354 ("\C-k" outline-kill-line t)
355 ("\C-y" outline-yank t)
356 ("\M-y" outline-yank-pop t)
357 ("\C-k" outline-kill-topic)
358 ; Miscellaneous commands:
359 ("\C-@" outline-mark-topic)
360 ("@" outline-resolve-xref)
361 ("?c" outline-copy-exposed)))
362
363 ;;;_ = outline-command-prefix
364 (defvar outline-command-prefix "\C-c"
365 "*Key sequence to be used as prefix for outline mode command key bindings.")
366
367 ;;;_ = outline-enwrap-isearch-mode
368 (defvar outline-enwrap-isearch-mode t
369 "*Set non-nil to enable automatic exposure of concealed isearch targets.
370
371 If non-nil, isearch will expose hidden text encountered in the course
372 of a search, and to reconceal it if the search is continued past it.")
373
374 ;;;_ = outline-use-hanging-indents
375 (defvar outline-use-hanging-indents t
376 "*If non-nil, topic body text auto-indent defaults to indent of the header.
377 Ie, it is indented to be just past the header prefix. This is
378 relevant mostly for use with indented-text-mode, or other situations
379 where auto-fill occurs.
380
381 [This feature no longer depends in any way on the 'filladapt.el'
382 lisp-archive package.]")
383 (make-variable-buffer-local 'outline-use-hanging-indents)
384
385 ;;;_ = outline-reindent-bodies
386 (defvar outline-reindent-bodies outline-use-hanging-indents
387 "*Non-nil enables auto-adjust of topic body hanging indent with depth shifts.
388
389 Indented hanging bodies are adjusted to remain even with \(or
390 right-indented from\) the beginning of heading text.")
391 (make-variable-buffer-local 'outline-reindent-bodies)
392
393 ;;;_ = outline-inhibit-protection
394 (defvar outline-inhibit-protection nil
395 "*Non-nil disables warnings and confirmation-checks for concealed-text edits.
396
397 Outline mode uses emacs change-triggered functions to detect unruly
398 changes to concealed regions. Set this var non-nil to disable the
399 protection, potentially increasing text-entry responsiveness a bit.
400
401 This var takes effect at outline-mode activation, so you may have to
402 deactivate and then reactivate the mode if you want to toggle the
403 behavior.")
404
405 ;;;_* CODE - no user customizations below.
406
407 ;;;_ #1 Internal Outline Formatting and Configuration
408 ;;;_ - Version
409 ;;;_ = outline-version
410 (defvar outline-version
411 (let ((rcs-rev "$Revision: 1.7 $"))
412 (condition-case err
413 (save-match-data
414 (string-match "\\$Revision: \\([0-9]+\\.[0-9]+\\)" rcs-rev)
415 (substring rcs-rev (match-beginning 1) (match-end 1)))
416 (error rcs-rev)))
417 "Revision number of currently loaded outline package. (Currently
418 specific to allout.el.)")
419 ;;;_ > outline-version
420 (defun outline-version (&optional here)
421 "Return string describing the loaded outline version."
422 (interactive "P")
423 (let ((msg (concat "Allout Outline Mode v " outline-version)))
424 (if here (insert-string msg))
425 (message "%s" msg)
426 msg))
427 ;;;_ - Topic header format
428 ;;;_ = outline-regexp
429 (defvar outline-regexp ""
430 "*Regular expression to match the beginning of a heading line.
431
432 Any line whose beginning matches this regexp is considered a
433 heading. This var is set according to the user configuration vars
434 by set-outline-regexp.")
435 (make-variable-buffer-local 'outline-regexp)
436 ;;;_ = outline-bullets-string
437 (defvar outline-bullets-string ""
438 "A string dictating the valid set of outline topic bullets.
439
440 This var should *not* be set by the user - it is set by 'set-outline-regexp',
441 and is produced from the elements of 'outline-plain-bullets-string'
442 and 'outline-distinctive-bullets-string'.")
443 (make-variable-buffer-local 'outline-bullets-string)
444 ;;;_ = outline-bullets-string-len
445 (defvar outline-bullets-string-len 0
446 "Length of current buffers' outline-plain-bullets-string.")
447 (make-variable-buffer-local 'outline-bullets-string-len)
448 ;;;_ = outline-line-boundary-regexp
449 (defvar outline-line-boundary-regexp ()
450 "Outline-regexp with outline-style beginning-of-line anchor.
451
452 (Ie, C-j, *or* C-m, for prefixes of hidden topics). This is properly
453 set when outline-regexp is produced by 'set-outline-regexp', so
454 that (match-beginning 2) and (match-end 2) delimit the prefix.")
455 (make-variable-buffer-local 'outline-line-boundary-regexp)
456 ;;;_ = outline-bob-regexp
457 (defvar outline-bob-regexp ()
458 "Like outline-line-boundary-regexp, for headers at beginning of buffer.
459 (match-beginning 2) and (match-end 2) delimit the prefix.")
460 (make-variable-buffer-local 'outline-bob-regexp)
461 ;;;_ = outline-header-subtraction
462 (defvar outline-header-subtraction (1- (length outline-header-prefix))
463 "Outline-header prefix length to subtract when computing topic depth.")
464 (make-variable-buffer-local 'outline-header-subtraction)
465 ;;;_ = outline-plain-bullets-string-len
466 (defvar outline-plain-bullets-string-len (length outline-plain-bullets-string)
467 "Length of outline-plain-bullets-string, updated by set-outline-regexp.")
468 (make-variable-buffer-local 'outline-plain-bullets-string-len)
469
470
471 ;;;_ > outline-reset-header-lead (header-lead)
472 (defun outline-reset-header-lead (header-lead)
473 "*Reset the leading string used to identify topic headers."
474 (interactive "sNew lead string: ")
475 (setq outline-header-prefix header-lead)
476 (setq outline-header-subtraction (1- (length outline-header-prefix)))
477 (set-outline-regexp))
478 ;;;_ > outline-lead-with-comment-string (header-lead)
479 (defun outline-lead-with-comment-string (&optional header-lead)
480 "*Set the topic-header leading string to specified string.
481
482 Useful when for encapsulating outline structure in programming
483 language comments. Returns the leading string."
484
485 (interactive "P")
486 (if (not (stringp header-lead))
487 (setq header-lead (read-string
488 "String prefix for topic headers: ")))
489 (setq outline-reindent-bodies nil)
490 (outline-reset-header-lead header-lead)
491 header-lead)
492 ;;;_ > outline-infer-header-lead (&optional reset)
493 (defun outline-infer-header-lead (&optional set)
494 "Determine appropriate `outline-header-prefix'.
495
496 Works according to settings of:
497
498 `outline-header-prefix' (default)
499 `outline-use-mode-specific-leader'
500 and `outline-mode-leaders'.
501
502 Optional arg SET means to do the processing to establish that prefix
503 for current outline processing, if it has changed from prior setting."
504 (let* ((use-leader (and (boundp 'outline-use-mode-specific-leader)
505 (if (or (stringp outline-use-mode-specific-leader)
506 (memq outline-use-mode-specific-leader
507 '(outline-mode-leaders
508 comment-start
509 t)))
510 outline-use-mode-specific-leader
511 ;; Oops - garbled value, equate with effect of 't:
512 t)))
513 (leader
514 (cond
515 ((not use-leader) nil)
516 ;; Use the explicitly designated leader:
517 ((stringp use-leader) use-leader)
518 (t (or (and (memq use-leader '(t outline-mode-leaders))
519 ;; Get it from outline mode leaders?
520 (cdr (assq major-mode outline-mode-leaders)))
521 ;; ... didn't get from outline-mode-leaders...
522 (and (memq use-leader '(t comment-start))
523 comment-start
524 ;; Use comment-start, maybe tripled, and with
525 ;; underscore:
526 (concat
527 (if (string= " "
528 (substring comment-start
529 (1- (length comment-start))))
530 ;; Use comment-start, sans trailing space:
531 (substring comment-start 0 -1)
532 (concat comment-start comment-start comment-start))
533 ;; ... and append underscore, whichever:
534 "_")))))))
535 (if (not leader)
536 nil
537 (if (string= leader outline-header-prefix)
538 nil ; no change, nothing to do.
539 (setq outline-header-prefix leader)
540 (if set (outline-reset-header-lead outline-header-prefix))
541 outline-header-prefix))))
542 ;;;_ > set-outline-regexp ()
543 (defun set-outline-regexp ()
544 "Generate proper topic-header regexp form for outline functions.
545
546 Works with respect to `outline-plain-bullets-string' and
547 `outline-distinctive-bullets-string'."
548
549 (interactive)
550 ;; Derive outline-bullets-string from user configured components:
551 (setq outline-bullets-string "")
552 (let ((strings (list 'outline-plain-bullets-string
553 'outline-distinctive-bullets-string))
554 cur-string
555 cur-len
556 cur-char
557 cur-char-string
558 index
559 new-string)
560 (while strings
561 (setq new-string "") (setq index 0)
562 (setq cur-len (length (setq cur-string (symbol-value (car strings)))))
563 (while (< index cur-len)
564 (setq cur-char (aref cur-string index))
565 (setq outline-bullets-string
566 (concat outline-bullets-string
567 (cond
568 ; Single dash would denote a
569 ; sequence, repeated denotes
570 ; a dash:
571 ((eq cur-char ?-) "--")
572 ; literal close-square-bracket
573 ; doesn't work right in the
574 ; expr, exclude it:
575 ((eq cur-char ?\]) "")
576 (t (regexp-quote (char-to-string cur-char))))))
577 (setq index (1+ index)))
578 (setq strings (cdr strings)))
579 )
580 ;; Derive next for repeated use in outline-pending-bullet:
581 (setq outline-plain-bullets-string-len (length outline-plain-bullets-string))
582 (setq outline-header-subtraction (1- (length outline-header-prefix)))
583 ;; Produce the new outline-regexp:
584 (setq outline-regexp (concat "\\(\\"
585 outline-header-prefix
586 "[ \t]*["
587 outline-bullets-string
588 "]\\)\\|\\"
589 outline-primary-bullet
590 "+\\|\^l"))
591 (setq outline-line-boundary-regexp
592 (concat "\\([\n\r]\\)\\(" outline-regexp "\\)"))
593 (setq outline-bob-regexp
594 (concat "\\(\\`\\)\\(" outline-regexp "\\)"))
595 )
596 ;;;_ - Key bindings
597 ;;;_ = outline-mode-map
598 (defvar outline-mode-map nil "Keybindings for (allout) outline minor mode.")
599 ;;;_ > produce-outline-mode-map (keymap-alist &optional base-map)
600 (defun produce-outline-mode-map (keymap-list &optional base-map)
601 "Produce keymap for use as outline-mode-map, from keymap-list.
602
603 Built on top of optional BASE-MAP, or empty sparse map if none specified.
604 See doc string for outline-keybindings-list for format of binding list."
605 (let ((map (or base-map (make-sparse-keymap))))
606 (mapcar (lambda (cell)
607 (apply 'define-key map (if (null (cdr (cdr cell)))
608 (cons (concat outline-command-prefix
609 (car cell))
610 (cdr cell))
611 (list (car cell) (car (cdr cell))))))
612 keymap-list)
613 map))
614 ;;;_ = outline-prior-bindings - being deprecated.
615 (defvar outline-prior-bindings nil
616 "Variable for use in V18, with outline-added-bindings, for
617 resurrecting, on mode deactivation, bindings that existed before
618 activation. Being deprecated.")
619 ;;;_ = outline-added-bindings - being deprecated
620 (defvar outline-added-bindings nil
621 "Variable for use in V18, with outline-prior-bindings, for
622 resurrecting, on mode deactivation, bindings that existed before
623 activation. Being deprecated.")
624 ;;;_ - Mode-Specific Variable Maintenance Utilities
625 ;;;_ = outline-mode-prior-settings
626 (defvar outline-mode-prior-settings nil
627 "Internal outline mode use; settings to be resumed on mode deactivation.")
628 (make-variable-buffer-local 'outline-mode-prior-settings)
629 ;;;_ > outline-resumptions (name &optional value)
630 (defun outline-resumptions (name &optional value)
631
632 "Registers or resumes settings over outline-mode activation/deactivation.
633
634 First arg is NAME of variable affected. Optional second arg is list
635 containing outline-mode-specific VALUE to be imposed on named
636 variable, and to be registered. (It's a list so you can specify
637 registrations of null values.) If no value is specified, the
638 registered value is returned (encapsulated in the list, so the caller
639 can distinguish nil vs no value), and the registration is popped
640 from the list."
641
642 (let ((on-list (assq name outline-mode-prior-settings))
643 prior-capsule ; By 'capsule' i mean a list
644 ; containing a value, so we can
645 ; distinguish nil from no value.
646 )
647
648 (if value
649
650 ;; Registering:
651 (progn
652 (if on-list
653 nil ; Already preserved prior value - don't mess with it.
654 ;; Register the old value, or nil if previously unbound:
655 (setq outline-mode-prior-settings
656 (cons (list name
657 (if (boundp name) (list (symbol-value name))))
658 outline-mode-prior-settings)))
659 ; And impose the new value, locally:
660 (progn (make-local-variable name)
661 (set name (car value))))
662
663 ;; Relinquishing:
664 (if (not on-list)
665
666 ;; Oops, not registered - leave it be:
667 nil
668
669 ;; Some registration:
670 ; reestablish it:
671 (setq prior-capsule (car (cdr on-list)))
672 (if prior-capsule
673 (set name (car prior-capsule)) ; Some prior value - reestablish it.
674 (makunbound name)) ; Previously unbound - demolish var.
675 ; Remove registration:
676 (let (rebuild)
677 (while outline-mode-prior-settings
678 (if (not (eq (car outline-mode-prior-settings)
679 on-list))
680 (setq rebuild
681 (cons (car outline-mode-prior-settings)
682 rebuild)))
683 (setq outline-mode-prior-settings
684 (cdr outline-mode-prior-settings)))
685 (setq outline-mode-prior-settings rebuild)))))
686 )
687 ;;;_ - Mode-specific incidentals
688 ;;;_ = outline-during-write-cue nil
689 (defvar outline-during-write-cue nil
690 "Used to inhibit outline change-protection during file write.
691
692 See also `outline-post-command-business', `outline-write-file-hook',
693 `outline-before-change-protect', and `outline-post-command-business'
694 functions.")
695 ;;;_ = outline-override-protect nil
696 (defvar outline-override-protect nil
697 "Used in outline-mode for regulate of concealed-text protection mechanism.
698
699 Allout outline mode regulates alteration of concealed text to protect
700 against inadvertant, unnoticed changes. This is for use by specific,
701 native outline functions to temporarily override that protection.
702 It's automatically reset to nil after every buffer modification.")
703 (make-variable-buffer-local 'outline-override-protect)
704 ;;;_ > outline-unprotected (expr)
705 (defmacro outline-unprotected (expr)
706 "Evaluate EXPRESSION with `outline-override-protect' let-bound 't'."
707 (` (let ((outline-override-protect t))
708 (, expr))))
709 ;;;_ = outline-undo-aggregation
710 (defvar outline-undo-aggregation 30
711 "Amount of successive self-insert actions to bunch together per undo.
712
713 This is purely a kludge variable, regulating the compensation for a bug in
714 the way that before-change-function and undo interact.")
715 (make-variable-buffer-local 'outline-undo-aggregation)
716 ;;;_ = file-var-bug hack
717 (defvar outline-v18/9-file-var-hack nil
718 "Horrible hack used to prevent invalid multiple triggering of outline
719 mode from prop-line file-var activation. Used by outline-mode function
720 to track repeats.")
721 ;;;_ > outline-write-file-hook ()
722 (defun outline-write-file-hook ()
723 "In outline mode, run as a local-write-file-hooks activity.
724
725 Currently just sets 'outline-during-write-cue', so outline-change-
726 protection knows to keep inactive during file write."
727 (setq outline-during-write-cue t)
728 nil)
729
730 ;;;_ #2 Mode activation
731 ;;;_ = outline-mode
732 (defvar outline-mode () "Allout outline mode minor-mode flag.")
733 (make-variable-buffer-local 'outline-mode)
734 ;;;_ > outline-mode-p ()
735 (defmacro outline-mode-p ()
736 "Return t if outline-mode is active in current buffer."
737 'outline-mode)
738 ;;;_ = outline-explicitly-deactivated
739 (defvar outline-explicitly-deactivated nil
740 "Outline-mode was last deliberately deactived.
741 So outline-post-command-business should not reactivate it...")
742 (make-variable-buffer-local 'outline-explicitly-deactivated)
743 ;;;_ > outline-init (mode)
744 (defun outline-init (mode)
745 "Prime outline-mode to enable/disable auto-activation, wrt `outline-layout'.
746
747 MODE is one of:
748
749 - nil, for no auto-activation,
750 - `activation', for auto-activation only,
751 - `ask' for auto-activation and auto-layout on confirmation from user,
752 - anything else, for auto-activation and auto-layout, without any
753 confirmation check.
754
755 Use this function to setup your emacs session for automatic activation
756 of allout outline mode, contingent to the buffer-specific setting of
757 the `outline-layout' variable. (See `outline-layout' and
758 `outline-expose-topic' docstrings for more details on auto layout).
759
760 `outline-init' works by setting up (or removing) the outline-mode
761 find-file-hook, and giving `outline-auto-activation' a suitable
762 setting.
763
764 To prime your emacs session for full auto-outline operation, include
765 the following two lines in your emacs init file:
766
767 \(require 'allout)
768 \(outline-init t)"
769
770 (if (not mode)
771 (progn
772 (setq find-file-hooks (delq 'outline-find-file-hook find-file-hooks))
773 (if (interactive-p)
774 (message "Allout outline mode auto-activation inhibited.")))
775 (add-hook 'find-file-hooks 'outline-find-file-hook)
776 (setq outline-auto-activation
777 (cond ((eq mode 'activation)
778 (message "Allout outline mode auto-activation enabled.")
779 'activate)
780 ((eq mode 'ask)
781 (message "Allout outline mode auto-activation enabled.")
782 'ask)
783 ((message
784 "Allout outline mode auto-activation and -layout enabled.")
785 t)))))
786 ;;;_ > outline-mode (&optional toggle)
787 ;;;_ : Defun:
788 (defun outline-mode (&optional toggle)
789 ;;;_ . Doc string:
790 "Toggle minor mode for controlling exposure and editing of text outlines.
791
792 Optional arg forces mode reactivation iff arg is positive num or symbol.
793
794 Allout outline mode provides extensive outline formatting and
795 manipulation capabilities. It is specifically aimed at supporting
796 outline structuring and manipulation of syntax-sensitive text, eg
797 programming languages. \(For an example, see the allout code itself,
798 which is organized in outline structure.\)
799
800 It also includes such things as topic-oriented repositioning, cut, and
801 paste; integral outline exposure-layout; incremental search with
802 dynamic exposure/conceament of concealed text; automatic topic-number
803 maintenance; and many other features.
804
805 See the docstring of the variable `outline-init' for instructions on
806 priming your emacs session for automatic activation of outline-mode,
807 according to file-var settings of the `outline-layout' variable.
808
809 Below is a description of the bindings, and then explanation of
810 special outline-mode features and terminology.
811
812 The bindings themselves are established according to the values of
813 variables `outline-keybindings-list' and `outline-command-prefix',
814 each time the mode is invoked. Prior bindings are resurrected when
815 the mode is revoked.
816
817 Navigation: Exposure Control:
818 ---------- ----------------
819 C-c C-n outline-next-visible-heading | C-c C-h outline-hide-current-subtree
820 C-c C-p outline-previous-visible-heading | C-c C-i outline-show-children
821 C-c C-u outline-up-current-level | C-c C-s outline-show-current-subtree
822 C-c C-f outline-forward-current-level | C-c C-o outline-show-current-entry
823 C-c C-b outline-backward-current-level | ^U C-c C-s outline-show-all
824 C-c C-e outline-end-of-current-entry | outline-hide-current-leaves
825 C-c C-a outline-beginning-of-current-entry, alternately, goes to hot-spot
826
827 Topic Header Production:
828 -----------------------
829 C-c<SP> outline-open-sibtopic Create a new sibling after current topic.
830 C-c . outline-open-subtopic ... an offspring of current topic.
831 C-c , outline-open-supertopic ... a sibling of the current topic's parent.
832
833 Topic Level and Prefix Adjustment:
834 ---------------------------------
835 C-c > outline-shift-in Shift current topic and all offspring deeper.
836 C-c < outline-shift-out ... less deep.
837 C-c<CR> outline-rebullet-topic Reconcile bullets of topic and its' offspring
838 - distinctive bullets are not changed, others
839 alternated according to nesting depth.
840 C-c b outline-rebullet-current-heading Prompt for alternate bullet for
841 current topic.
842 C-c # outline-number-siblings Number bullets of topic and siblings - the
843 offspring are not affected. With repeat
844 count, revoke numbering.
845
846 Topic-oriented Killing and Yanking:
847 ----------------------------------
848 C-c C-k outline-kill-topic Kill current topic, including offspring.
849 C-k outline-kill-line Like kill-line, but reconciles numbering, etc.
850 C-y outline-yank Yank, adjusting depth of yanked topic to
851 depth of heading if yanking into bare topic
852 heading (ie, prefix sans text).
853 M-y outline-yank-pop Is to outline-yank as yank-pop is to yank
854
855 Misc commands:
856 -------------
857 C-c @ outline-resolve-xref pop-to-buffer named by xref (cf
858 outline-file-xref-bullet)
859 C-c c outline-copy-exposed Copy current topic outline sans concealed
860 text, to buffer with name derived from
861 current buffer - \"XXX exposed\"
862 M-x outlineify-sticky Activate outline mode for current buffer,
863 and establish a default file-var setting
864 for `outline-layout'.
865 ESC ESC (outline-init t) Setup emacs session for outline mode
866 auto-activation.
867
868 HOT-SPOT Operation
869
870 Hot-spot operation provides a means for easy, single-keystroke outline
871 navigation and exposure control.
872
873 \\<outline-mode-map>
874 When the text cursor is positioned directly on the bullet character of
875 a topic, regular characters (a to z) invoke the commands of the
876 corresponding outline-mode keymap control chars. For example, \"f\"
877 would invoke the command typically bound to \"C-c C-f\"
878 \(\\[outline-forward-current-level] `outline-forward-current-level').
879
880 Thus, by positioning the cursor on a topic bullet, you can execute
881 the outline navigation and manipulation commands with a single
882 keystroke. Non-literal chars never get this special translation, so
883 you can use them to get away from the hot-spot, and back to normal
884 operation.
885
886 Note that the command `outline-beginning-of-current-entry' \(\\[outline-beginning-of-current-entry]\)
887 will move to the hot-spot when the cursor is already located at the
888 beginning of the current entry, so you can simply hit \\[outline-beginning-of-current-entry]
889 twice in a row to get to the hot-spot.
890
891 Terminology
892
893 Topic hierarchy constituents - TOPICS and SUBTOPICS:
894
895 TOPIC: A basic, coherent component of an emacs outline. It can
896 contain other topics, and it can be subsumed by other topics,
897 CURRENT topic:
898 The visible topic most immediately containing the cursor.
899 DEPTH: The degree of nesting of a topic; it increases with
900 containment. Also called the:
901 LEVEL: The same as DEPTH.
902
903 ANCESTORS:
904 The topics that contain a topic.
905 PARENT: A topic's immediate ancestor. It has a depth one less than
906 the topic.
907 OFFSPRING:
908 The topics contained by a topic;
909 SUBTOPIC:
910 An immediate offspring of a topic;
911 CHILDREN:
912 The immediate offspring of a topic.
913 SIBLINGS:
914 Topics having the same parent and depth.
915
916 Topic text constituents:
917
918 HEADER: The first line of a topic, include the topic PREFIX and header
919 text.
920 PREFIX: The leading text of a topic which which distinguishes it from
921 normal text. It has a strict form, which consists of a
922 prefix-lead string, padding, and a bullet. The bullet may be
923 followed by a number, indicating the ordinal number of the
924 topic among its siblings, a space, and then the header text.
925
926 The relative length of the PREFIX determines the nesting depth
927 of the topic.
928 PREFIX-LEAD:
929 The string at the beginning of a topic prefix, normally a '.'.
930 It can be customized by changing the setting of
931 `outline-header-prefix' and then reinitializing outline-mode.
932
933 By setting the prefix-lead to the comment-string of a
934 programming language, you can embed outline-structuring in
935 program code without interfering with the language processing
936 of that code. See `outline-use-mode-specific-leader'
937 docstring for more detail.
938 PREFIX-PADDING:
939 Spaces or asterisks which separate the prefix-lead and the
940 bullet, according to the depth of the topic.
941 BULLET: A character at the end of the topic prefix, it must be one of
942 the characters listed on 'outline-plain-bullets-string' or
943 'outline-distinctive-bullets-string'. (See the documentation
944 for these variables for more details.) The default choice of
945 bullet when generating varies in a cycle with the depth of the
946 topic.
947 ENTRY: The text contained in a topic before any offspring.
948 BODY: Same as ENTRY.
949
950
951 EXPOSURE:
952 The state of a topic which determines the on-screen visibility
953 of its' offspring and contained text.
954 CONCEALED:
955 Topics and entry text whose display is inhibited. Contiguous
956 units of concealed text is represented by '...' ellipses.
957 (Ref the 'selective-display' var.)
958
959 Concealed topics are effectively collapsed within an ancestor.
960 CLOSED: A topic whose immediate offspring and body-text is concealed.
961 OPEN: A topic that is not closed, though its' offspring or body may be."
962 ;;;_ . Code
963 (interactive "P")
964
965 (let* ((active (and (not (equal major-mode 'outline))
966 (outline-mode-p)))
967 ; Massage universal-arg 'toggle' val:
968 (toggle (and toggle
969 (or (and (listp toggle)(car toggle))
970 toggle)))
971 ; Activation specficially demanded?
972 (explicit-activation (or
973 ;;
974 (and toggle
975 (or (symbolp toggle)
976 (and (natnump toggle)
977 (not (zerop toggle)))))))
978 ;; outline-mode already called once during this complex command?
979 (same-complex-command (eq outline-v18/9-file-var-hack
980 (car command-history)))
981 do-layout
982 )
983
984 ; See comments below re v19.18,.19 bug.
985 (setq outline-v18/9-file-var-hack (car command-history))
986
987 (cond
988
989 ;; Provision for v19.18, 19.19 bug -
990 ;; Emacs v 19.18, 19.19 file-var code invokes prop-line-designated
991 ;; modes twice when file is visited. We have to avoid toggling mode
992 ;; off on second invocation, so we detect it as best we can, and
993 ;; skip everything.
994 ((and same-complex-command ; Still in same complex command
995 ; as last time outline-mode invoked.
996 active ; Already activated.
997 (not explicit-activation) ; Prop-line file-vars don't have args.
998 (string-match "^19.1[89]" ; Bug only known to be in v19.18 and
999 emacs-version)); 19.19.
1000 t)
1001
1002 ;; Deactivation:
1003 ((and (not explicit-activation)
1004 (or active toggle))
1005 ; Activation not explicitly
1006 ; requested, and either in
1007 ; active state or *de*activation
1008 ; specifically requested:
1009 (setq outline-explicitly-deactivated t)
1010 (if (string-match "^18\." emacs-version)
1011 ; Revoke those keys that remain
1012 ; as we set them:
1013 (let ((curr-loc (current-local-map)))
1014 (mapcar '(lambda (cell)
1015 (if (eq (lookup-key curr-loc (car cell))
1016 (car (cdr cell)))
1017 (define-key curr-loc (car cell)
1018 (assq (car cell) outline-prior-bindings))))
1019 outline-added-bindings)
1020 (outline-resumptions 'outline-added-bindings)
1021 (outline-resumptions 'outline-prior-bindings)))
1022
1023 (if outline-old-style-prefixes
1024 (progn
1025 (outline-resumptions 'outline-primary-bullet)
1026 (outline-resumptions 'outline-old-style-prefixes)))
1027 (outline-resumptions 'selective-display)
1028 (if (and (boundp 'before-change-function) before-change-function)
1029 (outline-resumptions 'before-change-function))
1030 (setq pre-command-hook (delq 'outline-pre-command-business
1031 pre-command-hook))
1032 (setq local-write-file-hooks
1033 (delq 'outline-write-file-hook
1034 local-write-file-hooks))
1035 (outline-resumptions 'paragraph-start)
1036 (outline-resumptions 'paragraph-separate)
1037 (outline-resumptions (if (string-match "^18" emacs-version)
1038 'auto-fill-hook
1039 'auto-fill-function))
1040 (outline-resumptions 'outline-former-auto-filler)
1041 (setq outline-mode nil))
1042
1043 ;; Activation:
1044 ((not active)
1045 (setq outline-explicitly-deactivated nil)
1046 (if outline-old-style-prefixes
1047 (progn ; Inhibit all the fancy formatting:
1048 (outline-resumptions 'outline-primary-bullet '("*"))
1049 (outline-resumptions 'outline-old-style-prefixes '(()))))
1050
1051 (outline-infer-header-lead)
1052
1053 (set-outline-regexp)
1054
1055 ; Produce map from current version
1056 ; of outline-keybindings-list:
1057 (if (boundp 'minor-mode-map-alist)
1058
1059 (progn ; V19, and maybe lucid and
1060 ; epoch, minor-mode key bindings:
1061 (setq outline-mode-map
1062 (produce-outline-mode-map outline-keybindings-list))
1063 (fset 'outline-mode-map outline-mode-map)
1064 ; Include on minor-mode-map-alist,
1065 ; if not already there:
1066 (if (not (member '(outline-mode . outline-mode-map)
1067 minor-mode-map-alist))
1068 (setq minor-mode-map-alist
1069 (cons '(outline-mode . outline-mode-map)
1070 minor-mode-map-alist))))
1071
1072 ; V18 minor-mode key bindings:
1073 ; Stash record of added bindings
1074 ; for later revocation:
1075 (outline-resumptions 'outline-added-bindings
1076 (list outline-keybindings-list))
1077 (outline-resumptions 'outline-prior-bindings
1078 (list (current-local-map)))
1079 ; and add them:
1080 (use-local-map (produce-outline-mode-map outline-keybindings-list
1081 (current-local-map)))
1082 )
1083
1084 ; selective-display is the
1085 ; emacs conditional exposure
1086 ; mechanism:
1087 (outline-resumptions 'selective-display '(t))
1088 (if outline-inhibit-protection
1089 t
1090 (outline-resumptions 'before-change-function
1091 '(outline-before-change-protect)))
1092 ; Temporarily set by any outline
1093 ; functions that can be trusted to
1094 ; deal properly with concealed text.
1095 (add-hook 'local-write-file-hooks 'outline-write-file-hook)
1096 ; Custom auto-fill func, to support
1097 ; respect for topic headline,
1098 ; hanging-indents, etc:
1099 (let* ((fill-func-var (if (string-match "^18" emacs-version)
1100 'auto-fill-hook
1101 'auto-fill-function))
1102 (fill-func (symbol-value fill-func-var)))
1103 ;; Register prevailing fill func for use by outline-auto-fill:
1104 (outline-resumptions 'outline-former-auto-filler (list fill-func))
1105 ;; Register outline-auto-fill to be used if filling is active:
1106 (outline-resumptions fill-func-var '(outline-auto-fill)))
1107 ;; Paragraphs are broken by topic headlines.
1108 (make-local-variable 'paragraph-start)
1109 (outline-resumptions 'paragraph-start
1110 (list (concat paragraph-start "\\|^\\("
1111 outline-regexp "\\)")))
1112 (make-local-variable 'paragraph-separate)
1113 (outline-resumptions 'paragraph-separate
1114 (list (concat paragraph-separate "\\|^\\("
1115 outline-regexp "\\)")))
1116
1117 (or (assq 'outline-mode minor-mode-alist)
1118 (setq minor-mode-alist
1119 (cons '(outline-mode " Outl") minor-mode-alist)))
1120
1121 (if outline-layout
1122 (setq do-layout t))
1123
1124 (if outline-enwrap-isearch-mode
1125 (outline-enwrap-isearch))
1126
1127 (run-hooks 'outline-mode-hook)
1128 (setq outline-mode t))
1129
1130 ;; Reactivation:
1131 ((setq do-layout t))
1132 ) ; cond
1133
1134 (if (and do-layout
1135 outline-auto-activation
1136 (listp outline-layout)
1137 (and (not (eq outline-auto-activation 'activate))
1138 (if (eq outline-auto-activation 'ask)
1139 (if (y-or-n-p (format "Expose %s with layout '%s'? "
1140 (buffer-name)
1141 outline-layout))
1142 t
1143 (message "Not doing %s layout.")
1144 nil)
1145 t)))
1146 (save-excursion
1147 (message "Adjusting '%s' exposure..." (buffer-name))
1148 (goto-char 0)
1149 (if (not (outline-goto-prefix))
1150 (outline-next-heading))
1151 (apply 'outline-expose-topic (list outline-layout))
1152 (message "Adjusting '%s' exposure... done." (buffer-name))))
1153 outline-mode
1154 ) ; let*
1155 ) ; defun
1156
1157 ;;;_ #3 Internal Position State-Tracking - "outline-recent-*" funcs
1158 ;;; All the basic outline functions that directly do string matches to
1159 ;;; evaluate heading prefix location set the variables
1160 ;;; `outline-recent-prefix-beginning' and `outline-recent-prefix-end'
1161 ;;; when successful. Functions starting with `outline-recent-' all
1162 ;;; use this state, providing the means to avoid redundant searches
1163 ;;; for just-established data. This optimization can provide
1164 ;;; significant speed improvement, but it must be employed carefully.
1165 ;;;_ = outline-recent-prefix-beginning
1166 (defvar outline-recent-prefix-beginning 0
1167 "Buffer point of the start of the last topic prefix encountered.")
1168 (make-variable-buffer-local 'outline-recent-prefix-beginning)
1169 ;;;_ = outline-recent-prefix-end
1170 (defvar outline-recent-prefix-end 0
1171 "Buffer point of the end of the last topic prefix encountered.")
1172 (make-variable-buffer-local 'outline-recent-prefix-end)
1173 ;;;_ = outline-recent-end-of-subtree
1174 (defvar outline-recent-end-of-subtree 0
1175 "Buffer point last returned by outline-end-of-current-subtree.")
1176 (make-variable-buffer-local 'outline-recent-end-of-subtree)
1177 ;;;_ > outline-prefix-data (beg end)
1178 (defmacro outline-prefix-data (beg end)
1179 "Register outline-prefix state data - BEGINNING and END of prefix.
1180
1181 For reference by 'outline-recent' funcs. Returns BEGINNING."
1182 (` (setq outline-recent-prefix-end (, end)
1183 outline-recent-prefix-beginning (, beg))))
1184 ;;;_ > outline-recent-depth ()
1185 (defmacro outline-recent-depth ()
1186 "Return depth of last heading encountered by an outline maneuvering function.
1187
1188 All outline functions which directly do string matches to assess
1189 headings set the variables outline-recent-prefix-beginning and
1190 outline-recent-prefix-end if successful. This function uses those settings
1191 to return the current depth."
1192
1193 '(max 1 (- outline-recent-prefix-end
1194 outline-recent-prefix-beginning
1195 outline-header-subtraction)))
1196 ;;;_ > outline-recent-prefix ()
1197 (defmacro outline-recent-prefix ()
1198 "Like outline-recent-depth, but returns text of last encountered prefix.
1199
1200 All outline functions which directly do string matches to assess
1201 headings set the variables outline-recent-prefix-beginning and
1202 outline-recent-prefix-end if successful. This function uses those settings
1203 to return the current depth."
1204 '(buffer-substring outline-recent-prefix-beginning
1205 outline-recent-prefix-end))
1206 ;;;_ > outline-recent-bullet ()
1207 (defmacro outline-recent-bullet ()
1208 "Like outline-recent-prefix, but returns bullet of last encountered prefix.
1209
1210 All outline functions which directly do string matches to assess
1211 headings set the variables outline-recent-prefix-beginning and
1212 outline-recent-prefix-end if successful. This function uses those settings
1213 to return the current depth of the most recently matched topic."
1214 '(buffer-substring (1- outline-recent-prefix-end)
1215 outline-recent-prefix-end))
1216
1217 ;;;_ #4 Navigation
1218
1219 ;;;_ - Position Assessment
1220 ;;;_ : Location Predicates
1221 ;;;_ > outline-on-current-heading-p ()
1222 (defun outline-on-current-heading-p ()
1223 "Return non-nil if point is on current visible topics' header line.
1224
1225 Actually, returns prefix beginning point."
1226 (save-excursion
1227 (beginning-of-line)
1228 (and (looking-at outline-regexp)
1229 (outline-prefix-data (match-beginning 0) (match-end 0)))))
1230 ;;;_ > outline-e-o-prefix-p ()
1231 (defun outline-e-o-prefix-p ()
1232 "True if point is located where current topic prefix ends, heading begins."
1233 (and (save-excursion (beginning-of-line)
1234 (looking-at outline-regexp))
1235 (= (point)(save-excursion (outline-end-of-prefix)(point)))))
1236 ;;;_ > outline-hidden-p ()
1237 (defmacro outline-hidden-p ()
1238 "True if point is in hidden text."
1239 '(save-excursion
1240 (and (re-search-backward "[\n\r]" () t)
1241 (= ?\r (following-char)))))
1242 ;;;_ > outline-visible-p ()
1243 (defmacro outline-visible-p ()
1244 "True if point is not in hidden text."
1245 (interactive)
1246 '(not (outline-hidden-p)))
1247 ;;;_ : Location attributes
1248 ;;;_ > outline-depth ()
1249 (defmacro outline-depth ()
1250 "Like outline-current-depth, but respects hidden as well as visible topics."
1251 '(save-excursion
1252 (if (outline-goto-prefix)
1253 (outline-recent-depth)
1254 (progn
1255 ;; Oops, no prefix, zero prefix data:
1256 (outline-prefix-data (point)(point))
1257 ;; ... and return 0:
1258 0))))
1259 ;;;_ > outline-current-depth ()
1260 (defmacro outline-current-depth ()
1261 "Return nesting depth of visible topic most immediately containing point."
1262 '(save-excursion
1263 (if (outline-back-to-current-heading)
1264 (max 1
1265 (- outline-recent-prefix-end
1266 outline-recent-prefix-beginning
1267 outline-header-subtraction))
1268 0)))
1269 ;;;_ > outline-get-current-prefix ()
1270 (defun outline-get-current-prefix ()
1271 "Topic prefix of the current topic."
1272 (save-excursion
1273 (if (outline-goto-prefix)
1274 (outline-recent-prefix))))
1275 ;;;_ > outline-get-bullet ()
1276 (defun outline-get-bullet ()
1277 "Return bullet of containing topic (visible or not)."
1278 (save-excursion
1279 (and (outline-goto-prefix)
1280 (outline-recent-bullet))))
1281 ;;;_ > outline-current-bullet ()
1282 (defun outline-current-bullet ()
1283 "Return bullet of current (visible) topic heading, or none if none found."
1284 (condition-case err
1285 (save-excursion
1286 (outline-back-to-current-heading)
1287 (buffer-substring (- outline-recent-prefix-end 1)
1288 outline-recent-prefix-end))
1289 ;; Quick and dirty provision, ostensibly for missing bullet:
1290 (args-out-of-range nil))
1291 )
1292 ;;;_ > outline-get-prefix-bullet (prefix)
1293 (defun outline-get-prefix-bullet (prefix)
1294 "Return the bullet of the header prefix string PREFIX."
1295 ;; Doesn't make sense if we're old-style prefixes, but this just
1296 ;; oughtn't be called then, so forget about it...
1297 (if (string-match outline-regexp prefix)
1298 (substring prefix (1- (match-end 0)) (match-end 0))))
1299
1300 ;;;_ - Navigation macros
1301 ;;;_ > outline-next-heading ()
1302 (defmacro outline-next-heading ()
1303 "Move to the heading for the topic \(possibly invisible) before this one.
1304
1305 Returns the location of the heading, or nil if none found."
1306
1307 '(if (and (bobp) (not (eobp)))
1308 (forward-char 1))
1309
1310 '(if (re-search-forward outline-line-boundary-regexp nil 0)
1311 (progn ; Got valid location state - set vars:
1312 (outline-prefix-data
1313 (goto-char (or (match-beginning 2)
1314 outline-recent-prefix-beginning))
1315 (or (match-end 2) outline-recent-prefix-end)))))
1316 ;;;_ > outline-previous-heading ()
1317 (defmacro outline-previous-heading ()
1318 "Move to the prior \(possibly invisible) heading line.
1319
1320 Return the location of the beginning of the heading, or nil if not found."
1321
1322 '(if (bobp)
1323 nil
1324 (outline-goto-prefix)
1325 (if
1326 ;; searches are unbounded and return nil if failed:
1327 (or (re-search-backward outline-line-boundary-regexp nil 0)
1328 (looking-at outline-bob-regexp))
1329 (progn ; Got valid location state - set vars:
1330 (outline-prefix-data
1331 (goto-char (or (match-beginning 2)
1332 outline-recent-prefix-beginning))
1333 (or (match-end 2) outline-recent-prefix-end))))))
1334
1335 ;;;_ - Subtree Charting
1336 ;;;_ " These routines either produce or assess charts, which are
1337 ;;; nested lists of the locations of topics within a subtree.
1338 ;;;
1339 ;;; Use of charts enables efficient navigation of subtrees, by
1340 ;;; requiring only a single regexp-search based traversal, to scope
1341 ;;; out the subtopic locations. The chart then serves as the basis
1342 ;;; for whatever assessment or adjustment of the subtree that is
1343 ;;; required, without requiring redundant topic-traversal procedures.
1344
1345 ;;;_ > outline-chart-subtree (&optional levels orig-depth prev-depth)
1346 (defun outline-chart-subtree (&optional levels orig-depth prev-depth)
1347 "Produce a location \"chart\" of subtopics of the containing topic.
1348
1349 Optional argument LEVELS specifies the depth \(releative to start
1350 depth\) for the chart. Subsequent optional args are not for public
1351 use.
1352
1353 Charts are used to capture outline structure, so that outline-altering
1354 routines need assess the structure only once, and then use the chart
1355 for their elaborate manipulations.
1356
1357 Topics are entered in the chart so the last one is at the car.
1358 The entry for each topic consists of an integer indicating the point
1359 at the beginning of the topic. Charts for offspring consists of a
1360 list containing, recursively, the charts for the respective subtopics.
1361 The chart for a topics' offspring precedes the entry for the topic
1362 itself.
1363
1364 The other function parameters are for internal recursion, and should
1365 not be specified by external callers. ORIG-DEPTH is depth of topic at
1366 starting point, and PREV-DEPTH is depth of prior topic."
1367
1368 (let ((original (not orig-depth)) ; 'orig-depth' set only in recursion.
1369 chart curr-depth)
1370
1371 (if original ; Just starting?
1372 ; Register initial settings and
1373 ; position to first offspring:
1374 (progn (setq orig-depth (outline-depth))
1375 (or prev-depth (setq prev-depth (1+ orig-depth)))
1376 (outline-next-heading)))
1377
1378 ;; Loop over the current levels' siblings. Besides being more
1379 ;; efficient than tail-recursing over a level, it avoids exceeding
1380 ;; the typically quite constrained emacs max-lisp-eval-depth.
1381 ;; Probably would speed things up to implement loop-based stack
1382 ;; operation rather than recursing for lower levels. Bah.
1383 (while (and (not (eobp))
1384 ; Still within original topic?
1385 (< orig-depth (setq curr-depth (outline-recent-depth)))
1386 (cond ((= prev-depth curr-depth)
1387 ;; Register this one and move on:
1388 (setq chart (cons (point) chart))
1389 (if (and levels (<= levels 1))
1390 ;; At depth limit - skip sublevels:
1391 (or (outline-next-sibling curr-depth)
1392 ;; or no more siblings - proceed to
1393 ;; next heading at lesser depth:
1394 (while (<= curr-depth
1395 (outline-recent-depth))
1396 (outline-next-heading)))
1397 (outline-next-heading)))
1398
1399 ((and (< prev-depth curr-depth)
1400 (or (not levels)
1401 (> levels 0)))
1402 ;; Recurse on deeper level of curr topic:
1403 (setq chart
1404 (cons (outline-chart-subtree (and levels
1405 (1- levels))
1406 orig-depth
1407 curr-depth)
1408 chart))
1409 ;; ... then continue with this one.
1410 )
1411
1412 ;; ... else nil if we've ascended back to prev-depth.
1413
1414 )))
1415
1416 (if original ; We're at the last sibling on
1417 ; the original level. Position
1418 ; to the end of it:
1419 (progn (and (not (eobp)) (forward-char -1))
1420 (and (memq (preceding-char) '(?\n ?\^M))
1421 (memq (aref (buffer-substring (max 1 (- (point) 3))
1422 (point))
1423 1)
1424 '(?\n ?\^M))
1425 (forward-char -1))
1426 (setq outline-recent-end-of-subtree (point))))
1427
1428 chart ; (nreverse chart) not necessary,
1429 ; and maybe not preferable.
1430 ))
1431 ;;;_ > outline-chart-siblings (&optional start end)
1432 (defun outline-chart-siblings (&optional start end)
1433 "Produce a list of locations of this and succeeding sibling topics.
1434 Effectively a top-level chart of siblings. See 'outline-chart-subtree'
1435 for an explanation of charts."
1436 (save-excursion
1437 (if (outline-goto-prefix)
1438 (let ((chart (list (point))))
1439 (while (outline-next-sibling)
1440 (setq chart (cons (point) chart)))
1441 (if chart (setq chart (nreverse chart)))))))
1442 ;;;_ > outline-chart-to-reveal (chart depth)
1443 (defun outline-chart-to-reveal (chart depth)
1444
1445 "Return a flat list of hidden points in subtree CHART, up to DEPTH.
1446
1447 Note that point can be left at any of the points on chart, or at the
1448 start point."
1449
1450 (let (result here)
1451 (while (and (or (eq depth t) (> depth 0))
1452 chart)
1453 (setq here (car chart))
1454 (if (listp here)
1455 (let ((further (outline-chart-to-reveal here (or (eq depth t)
1456 (1- depth)))))
1457 ;; We're on the start of a subtree - recurse with it, if there's
1458 ;; more depth to go:
1459 (if further (setq result (append further result)))
1460 (setq chart (cdr chart)))
1461 (goto-char here)
1462 (if (= (preceding-char) ?\r)
1463 (setq result (cons here result)))
1464 (setq chart (cdr chart))))
1465 result))
1466 ;;;_ X outline-chart-spec (chart spec &optional exposing)
1467 (defun outline-chart-spec (chart spec &optional exposing)
1468 "Not yet \(if ever\) implemented.
1469
1470 Produce exposure directives given topic/subtree CHART and an exposure SPEC.
1471
1472 Exposure spec indicates the locations to be exposed and the prescribed
1473 exposure status. Optional arg EXPOSING is an integer, with 0
1474 indicating pending concealment, anything higher indicating depth to
1475 which subtopic headers should be exposed, and negative numbers
1476 indicating (negative of) the depth to which subtopic headers and
1477 bodies should be exposed.
1478
1479 The produced list can have two types of entries. Bare numbers
1480 indicate points in the buffer where topic headers that should be
1481 exposed reside.
1482
1483 - bare negative numbers indicates that the topic starting at the
1484 point which is the negative of the number should be opened,
1485 including their entries.
1486 - bare positive values indicate that this topic header should be
1487 openned.
1488 - Lists signify the beginning and end points of regions that should
1489 be flagged, and the flag to employ. (For concealment: '\(\?r\)', and
1490 exposure:"
1491 (while spec
1492 (cond ((listp spec)
1493 )
1494 )
1495 (setq spec (cdr spec)))
1496 )
1497
1498 ;;;_ - Within Topic
1499 ;;;_ > outline-goto-prefix ()
1500 (defun outline-goto-prefix ()
1501 "Put point at beginning of outline prefix for immediately containing topic.
1502
1503 Goes to first subsequent topic if none immediately containing.
1504
1505 Not sensitive to topic visibility.
1506
1507 Returns a the point at the beginning of the prefix, or nil if none."
1508
1509 (let (done)
1510 (while (and (not done)
1511 (re-search-backward "[\n\r]" nil 1))
1512 (forward-char 1)
1513 (if (looking-at outline-regexp)
1514 (setq done (outline-prefix-data (match-beginning 0)
1515 (match-end 0)))
1516 (forward-char -1)))
1517 (if (bobp)
1518 (cond ((looking-at outline-regexp)
1519 (outline-prefix-data (match-beginning 0)(match-end 0)))
1520 ((outline-next-heading)
1521 (outline-prefix-data (match-beginning 0)(match-end 0)))
1522 (done))
1523 done)))
1524 ;;;_ > outline-end-of-prefix ()
1525 (defun outline-end-of-prefix (&optional ignore-decorations)
1526 "Position cursor at beginning of header text.
1527
1528 If optional IGNORE-DECORATIONS is non-nil, put just after bullet,
1529 otherwise skip white space between bullet and ensuing text."
1530
1531 (if (not (outline-goto-prefix))
1532 nil
1533 (let ((match-data (match-data)))
1534 (goto-char (match-end 0))
1535 (if ignore-decorations
1536 t
1537 (while (looking-at "[0-9]") (forward-char 1))
1538 (if (and (not (eolp)) (looking-at "\\s-")) (forward-char 1)))
1539 (store-match-data match-data))
1540 ;; Reestablish where we are:
1541 (outline-current-depth)))
1542 ;;;_ > outline-current-bullet-pos ()
1543 (defun outline-current-bullet-pos ()
1544 "Return position of current \(visible) topic's bullet."
1545
1546 (if (not (outline-current-depth))
1547 nil
1548 (1- (match-end 0))))
1549 ;;;_ > outline-back-to-current-heading ()
1550 (defun outline-back-to-current-heading ()
1551 "Move to heading line of current topic, or beginning if already on the line."
1552
1553 (beginning-of-line)
1554 (prog1 (or (outline-on-current-heading-p)
1555 (and (re-search-backward (concat "^\\(" outline-regexp "\\)")
1556 nil
1557 'move)
1558 (outline-prefix-data (match-beginning 1)(match-end 1))))
1559 (if (interactive-p) (outline-end-of-prefix))))
1560 ;;;_ > outline-pre-next-preface ()
1561 (defun outline-pre-next-preface ()
1562 "Skip forward to just before the next heading line.
1563
1564 Returns that character position."
1565
1566 (if (re-search-forward outline-line-boundary-regexp nil 'move)
1567 (prog1 (goto-char (match-beginning 0))
1568 (outline-prefix-data (match-beginning 2)(match-end 2)))))
1569 ;;;_ > outline-end-of-current-subtree ()
1570 (defun outline-end-of-current-subtree ()
1571 "Put point at the end of the last leaf in the currently visible topic."
1572 (interactive)
1573 (outline-back-to-current-heading)
1574 (let ((level (outline-recent-depth)))
1575 (outline-next-heading)
1576 (while (and (not (eobp))
1577 (> (outline-recent-depth) level))
1578 (outline-next-heading))
1579 (and (not (eobp)) (forward-char -1))
1580 (and (memq (preceding-char) '(?\n ?\^M))
1581 (memq (aref (buffer-substring (max 1 (- (point) 3)) (point)) 1)
1582 '(?\n ?\^M))
1583 (forward-char -1))
1584 (setq outline-recent-end-of-subtree (point))))
1585 ;;;_ > outline-beginning-of-current-entry ()
1586 (defun outline-beginning-of-current-entry ()
1587 "When not already there, position point at beginning of current topic's body.
1588
1589 If already there, move cursor to bullet for hot-spot operation.
1590 \(See outline-mode doc string for details on hot-spot operation.)"
1591 (interactive)
1592 (let ((start-point (point)))
1593 (outline-end-of-prefix)
1594 (if (and (interactive-p)
1595 (= (point) start-point))
1596 (goto-char (outline-current-bullet-pos)))))
1597 ;;;_ > outline-end-of-current-entry ()
1598 (defun outline-end-of-current-entry ()
1599 "Position the point at the end of the current topics' entry."
1600 (interactive)
1601 (outline-show-entry)
1602 (prog1 (outline-pre-next-preface)
1603 (if (and (not (bobp))(looking-at "^$"))
1604 (forward-char -1))))
1605
1606 ;;;_ - Depth-wise
1607 ;;;_ > outline-ascend-to-depth (depth)
1608 (defun outline-ascend-to-depth (depth)
1609 "Ascend to depth DEPTH, returning depth if successful, nil if not."
1610 (if (and (> depth 0)(<= depth (outline-depth)))
1611 (let ((last-good (point)))
1612 (while (and (< depth (outline-depth))
1613 (setq last-good (point))
1614 (outline-beginning-of-level)
1615 (outline-previous-heading)))
1616 (if (= (outline-recent-depth) depth)
1617 (progn (goto-char outline-recent-prefix-beginning)
1618 depth)
1619 (goto-char last-good)
1620 nil))
1621 (if (interactive-p) (outline-end-of-prefix))))
1622 ;;;_ > outline-descend-to-depth (depth)
1623 (defun outline-descend-to-depth (depth)
1624 "Descend to depth DEPTH within current topic.
1625
1626 Returning depth if successful, nil if not."
1627 (let ((start-point (point))
1628 (start-depth (outline-depth)))
1629 (while
1630 (and (> (outline-depth) 0)
1631 (not (= depth (outline-recent-depth))) ; ... not there yet
1632 (outline-next-heading) ; ... go further
1633 (< start-depth (outline-recent-depth)))) ; ... still in topic
1634 (if (and (> (outline-depth) 0)
1635 (= (outline-recent-depth) depth))
1636 depth
1637 (goto-char start-point)
1638 nil))
1639 )
1640 ;;;_ > outline-up-current-level (arg &optional dont-complain)
1641 (defun outline-up-current-level (arg &optional dont-complain)
1642 "Move out ARG levels from current visible topic.
1643
1644 Positions on heading line of containing topic. Error if unable to
1645 ascend that far, or nil if unable to ascend but optional arg
1646 DONT-COMPLAIN is non-nil."
1647 (interactive "p")
1648 (outline-back-to-current-heading)
1649 (let ((present-level (outline-recent-depth))
1650 (last-good (point))
1651 failed
1652 return)
1653 ;; Loop for iterating arg:
1654 (while (and (> (outline-recent-depth) 1)
1655 (> arg 0)
1656 (not (bobp))
1657 (not failed))
1658 (setq last-good (point))
1659 ;; Loop for going back over current or greater depth:
1660 (while (and (not (< (outline-recent-depth) present-level))
1661 (or (outline-previous-visible-heading 1)
1662 (not (setq failed present-level)))))
1663 (setq present-level (outline-current-depth))
1664 (setq arg (- arg 1)))
1665 (if (or failed
1666 (> arg 0))
1667 (progn (goto-char last-good)
1668 (if (interactive-p) (outline-end-of-prefix))
1669 (if (not dont-complain)
1670 (error "Can't ascend past outermost level.")
1671 (if (interactive-p) (outline-end-of-prefix))
1672 nil))
1673 (if (interactive-p) (outline-end-of-prefix))
1674 outline-recent-prefix-beginning)))
1675
1676 ;;;_ - Linear
1677 ;;;_ > outline-next-sibling (&optional depth backward)
1678 (defun outline-next-sibling (&optional depth backward)
1679 "Like outline-forward-current-level, but respects invisible topics.
1680
1681 Traverse at optional DEPTH, or current depth if none specified.
1682
1683 Go backward if optional arg BACKWARD is non-nil.
1684
1685 Return depth if successful, nil otherwise."
1686
1687 (if (and backward (bobp))
1688 nil
1689 (let ((start-depth (or depth (outline-depth)))
1690 (start-point (point))
1691 last-depth)
1692 (while (and (not (if backward (bobp) (eobp)))
1693 (if backward (outline-previous-heading)
1694 (outline-next-heading))
1695 (> (setq last-depth (outline-recent-depth)) start-depth)))
1696 (if (and (not (eobp))
1697 (and (> (or last-depth (outline-depth)) 0)
1698 (= (outline-recent-depth) start-depth)))
1699 outline-recent-prefix-beginning
1700 (goto-char start-point)
1701 (if depth (outline-depth) start-depth)
1702 nil))))
1703 ;;;_ > outline-previous-sibling (&optional depth backward)
1704 (defun outline-previous-sibling (&optional depth backward)
1705 "Like outline-forward-current-level,but backwards & respect invisible topics.
1706
1707 Optional DEPTH specifies depth to traverse, default current depth.
1708
1709 Optional BACKWARD reverses direction.
1710
1711 Return depth if successful, nil otherwise."
1712 (outline-next-sibling depth (not backward))
1713 )
1714 ;;;_ > outline-snug-back ()
1715 (defun outline-snug-back ()
1716 "Position cursor at end of previous topic
1717
1718 Presumes point is at the start of a topic prefix."
1719 (if (or (bobp) (eobp))
1720 nil
1721 (forward-char -1))
1722 (if (or (bobp) (not (memq (preceding-char) '(?\n ?\^M))))
1723 nil
1724 (forward-char -1)
1725 (if (or (bobp) (not (memq (preceding-char) '(?\n ?\^M))))
1726 (forward-char -1)))
1727 (point))
1728 ;;;_ > outline-beginning-of-level ()
1729 (defun outline-beginning-of-level ()
1730 "Go back to the first sibling at this level, visible or not."
1731 (outline-end-of-level 'backward))
1732 ;;;_ > outline-end-of-level (&optional backward)
1733 (defun outline-end-of-level (&optional backward)
1734 "Go to the last sibling at this level, visible or not."
1735
1736 (let ((depth (outline-depth)))
1737 (while (outline-previous-sibling depth nil))
1738 (prog1 (outline-recent-depth)
1739 (if (interactive-p) (outline-end-of-prefix)))))
1740 ;;;_ > outline-next-visible-heading (arg)
1741 (defun outline-next-visible-heading (arg)
1742 "Move to the next ARG'th visible heading line, backward if arg is negative.
1743
1744 Move as far as possible in indicated direction \(beginning or end of
1745 buffer\) if headings are exhausted."
1746
1747 (interactive "p")
1748 (let* ((backward (if (< arg 0) (setq arg (* -1 arg))))
1749 (step (if backward -1 1))
1750 (start-point (point))
1751 prev got)
1752
1753 (while (> arg 0) ; limit condition
1754 (while (and (not (if backward (bobp)(eobp))) ; boundary condition
1755 ;; Move, skipping over all those concealed lines:
1756 (< -1 (forward-line step))
1757 (not (setq got (looking-at outline-regexp)))))
1758 ;; Register this got, it may be the last:
1759 (if got (setq prev got))
1760 (setq arg (1- arg)))
1761 (cond (got ; Last move was to a prefix:
1762 (outline-prefix-data (match-beginning 0) (match-end 0))
1763 (outline-end-of-prefix))
1764 (prev ; Last move wasn't, but prev was:
1765 (outline-prefix-data (match-beginning 0) (match-end 0)))
1766 ((not backward) (end-of-line) nil))))
1767 ;;;_ > outline-previous-visible-heading (arg)
1768 (defun outline-previous-visible-heading (arg)
1769 "Move to the previous heading line.
1770
1771 With argument, repeats or can move forward if negative.
1772 A heading line is one that starts with a `*' (or that outline-regexp
1773 matches)."
1774 (interactive "p")
1775 (outline-next-visible-heading (- arg)))
1776 ;;;_ > outline-forward-current-level (arg)
1777 (defun outline-forward-current-level (arg)
1778 "Position point at the next heading of the same level.
1779
1780 Takes optional repeat-count, goes backward if count is negative.
1781
1782 Returns resulting position, else nil if none found."
1783 (interactive "p")
1784 (let ((start-depth (outline-current-depth))
1785 (start-point (point))
1786 (start-arg arg)
1787 (backward (> 0 arg))
1788 last-depth
1789 (last-good (point))
1790 at-boundary)
1791 (if (= 0 start-depth)
1792 (error "No siblings, not in a topic..."))
1793 (if backward (setq arg (* -1 arg)))
1794 (while (not (or (zerop arg)
1795 at-boundary))
1796 (while (and (not (if backward (bobp) (eobp)))
1797 (if backward (outline-previous-visible-heading 1)
1798 (outline-next-visible-heading 1))
1799 (> (setq last-depth (outline-recent-depth)) start-depth)))
1800 (if (and last-depth (= last-depth start-depth)
1801 (not (if backward (bobp) (eobp))))
1802 (setq last-good (point)
1803 arg (1- arg))
1804 (setq at-boundary t)))
1805 (if (and (not (eobp))
1806 (= arg 0)
1807 (and (> (or last-depth (outline-depth)) 0)
1808 (= (outline-recent-depth) start-depth)))
1809 outline-recent-prefix-beginning
1810 (goto-char last-good)
1811 (if (not (interactive-p))
1812 nil
1813 (outline-end-of-prefix)
1814 (error "Hit %s level %d topic, traversed %d of %d requested."
1815 (if backward "first" "last")
1816 (outline-recent-depth)
1817 (- (abs start-arg) arg)
1818 (abs start-arg))))))
1819 ;;;_ > outline-backward-current-level (arg)
1820 (defun outline-backward-current-level (arg)
1821 "Inverse of `outline-forward-current-level'."
1822 (interactive "p")
1823 (if (interactive-p)
1824 (let ((current-prefix-arg (* -1 arg)))
1825 (call-interactively 'outline-forward-current-level))
1826 (outline-forward-current-level (* -1 arg))))
1827
1828 ;;;_ #5 Alteration
1829
1830 ;;;_ - Fundamental
1831 ;;;_ > outline-before-change-protect (beg end)
1832 (defun outline-before-change-protect (beg end)
1833 "Outline before-change hook, regulates changes to concealed text.
1834
1835 Reveal concealed text that would be changed by current command, and
1836 offer user choice to commit or forego the change. Unchanged text is
1837 reconcealed. User has option to have changed text reconcealed.
1838
1839 Undo commands are specially treated - the user is not prompted for
1840 choice, the undoes are always committed (based on presumption that the
1841 things being undone were already subject to this regulation routine),
1842 and undoes always leave the changed stuff exposed.
1843
1844 Changes to concealed regions are ignored while file is being written.
1845 \(This is for the sake of functions that do change the file during
1846 writes, like crypt and zip modes.)
1847
1848 Locally bound in outline buffers to 'before-change-function', which
1849 in emacs 19 is run before any change to the buffer. (Has no effect
1850 in Emacs 18, which doesn't support before-change-function.)
1851
1852 Any functions which set ['this-command' to 'undo', or which set]
1853 'outline-override-protect' non-nil (as does, eg, outline-flag-chars)
1854 are exempt from this restriction."
1855 (if (and (outline-mode-p)
1856 ; outline-override-protect
1857 ; set by functions that know what
1858 ; they're doing, eg outline internals:
1859 (not outline-override-protect)
1860 (not outline-during-write-cue)
1861 (save-match-data ; Preserve operation position state.
1862 ; Both beginning and end chars must
1863 ; be exposed:
1864 (save-excursion (if (memq this-command '(newline open-line))
1865 ;; Compensate for stupid emacs {new,
1866 ;; open-}line display optimization:
1867 (setq beg (1+ beg)
1868 end (1+ end)))
1869 (goto-char beg)
1870 (or (outline-hidden-p)
1871 (and (not (= beg end))
1872 (goto-char end)
1873 (outline-hidden-p))))))
1874 (save-match-data
1875 (if (equal this-command 'undo)
1876 ;; Allow undo without inhibition.
1877 ;; - Undoing new and open-line hits stupid emacs redisplay
1878 ;; optimization (em 19 cmds.c, ~ line 200).
1879 ;; - Presumably, undoing what was properly protected when
1880 ;; done.
1881 ;; - Undo may be users' only recourse in protection faults.
1882 ;; So, expose what getting changed:
1883 (progn (message "Undo! - exposing concealed target...")
1884 (if (outline-hidden-p)
1885 (outline-show-children))
1886 (message "Undo!"))
1887 (let (response
1888 (rehide-completely (save-excursion (outline-goto-prefix)
1889 (outline-hidden-p)))
1890 rehide-place)
1891
1892 (save-excursion
1893 (if (condition-case err
1894 ;; Condition case to catch keyboard quits during reads.
1895 (progn
1896 ; Give them a peek where
1897 (save-excursion
1898 (if (eolp) (setq rehide-place
1899 (outline-goto-prefix)))
1900 (outline-show-entry))
1901 ; Present the message, but...
1902 ; leave the cursor at the location
1903 ; until they respond:
1904 ; Then interpret the response:
1905 (while
1906 (progn
1907 (message (concat "Change inside concealed"
1908 " region - do it? "
1909 "(n or 'y'/'r'eclose)"))
1910 (setq response (read-char))
1911 (not
1912 (cond ((memq response '(?r ?R))
1913 (setq response 'reclose))
1914 ((memq response '(?y ?Y ? ))
1915 (setq response t))
1916 ((memq response '(?n ?N 127))
1917 (setq response nil)
1918 t)
1919 ((eq response ??)
1920 (message
1921 "'r' means 'yes, then reclose")
1922 nil)
1923 (t (message "Please answer y, n, or r")
1924 (sit-for 1)
1925 nil)))))
1926 response)
1927 (quit nil))
1928 ; Continue:
1929 (if (eq response 'reclose)
1930 (save-excursion
1931 (if rehide-place (goto-char rehide-place))
1932 (if rehide-completely
1933 (outline-hide-current-entry-completely)
1934 (outline-hide-current-entry)))
1935 (if (outline-ascend-to-depth (1- (outline-recent-depth)))
1936 (outline-show-children)
1937 (outline-show-to-offshoot)))
1938 ; Prevent:
1939 (if rehide-completely
1940 (save-excursion
1941 (if rehide-place (goto-char rehide-place))
1942 (outline-hide-current-entry-completely))
1943 (outline-hide-current-entry))
1944 (error (concat
1945 "Change within concealed region prevented.")))))))
1946 ) ; if
1947 ) ; defun
1948 ;;;_ = outline-post-goto-bullet
1949 (defvar outline-post-goto-bullet nil
1950 "Outline internal var, for `outline-pre-command-business' hot-spot operation.
1951
1952 When set, tells post-processing to reposition on topic bullet, and
1953 then unset it. Set by outline-pre-command-business when implementing
1954 hot-spot operation, where literal characters typed over a topic bullet
1955 are mapped to the command of the corresponding control-key on the
1956 outline-mode-map.")
1957 (make-variable-buffer-local 'outline-post-goto-bullet)
1958 ;;;_ > outline-post-command-business ()
1959 (defun outline-post-command-business ()
1960 "Outline post-command-hook function.
1961
1962 - Null outline-override-protect, so it's not left open.
1963
1964 - Implement (and clear) outline-post-goto-bullet, for hot-spot
1965 outline commands.
1966
1967 - Massages buffer-undo-list so successive, standard character self-inserts are
1968 aggregated. This kludge compensates for lack of undo bunching when
1969 before-change-function is used."
1970
1971 ; Apply any external change func:
1972 (if (not (outline-mode-p)) ; In outline-mode.
1973 nil
1974 (setq outline-override-protect nil)
1975 (if outline-during-write-cue
1976 ;; Was used by outline-before-change-protect, done with it now:
1977 (setq outline-during-write-cue nil))
1978 ;; Undo bunching business:
1979 (if (and (listp buffer-undo-list) ; Undo history being kept.
1980 (equal this-command 'self-insert-command)
1981 (equal last-command 'self-insert-command))
1982 (let* ((prev-stuff (cdr buffer-undo-list))
1983 (before-prev-stuff (cdr (cdr prev-stuff)))
1984 cur-cell cur-from cur-to
1985 prev-cell prev-from prev-to)
1986 (if (and before-prev-stuff ; Goes back far enough to bother,
1987 (not (car prev-stuff)) ; and break before current,
1988 (not (car before-prev-stuff)) ; !and break before prev!
1989 (setq prev-cell (car (cdr prev-stuff))) ; contents now,
1990 (setq cur-cell (car buffer-undo-list)) ; contents prev.
1991
1992 ;; cur contents denote a single char insertion:
1993 (numberp (setq cur-from (car cur-cell)))
1994 (numberp (setq cur-to (cdr cur-cell)))
1995 (= 1 (- cur-to cur-from))
1996
1997 ;; prev contents denote fewer than aggregate-limit
1998 ;; insertions:
1999 (numberp (setq prev-from (car prev-cell)))
2000 (numberp (setq prev-to (cdr prev-cell)))
2001 ; Below threshold:
2002 (> outline-undo-aggregation (- prev-to prev-from)))
2003 (setq buffer-undo-list
2004 (cons (cons prev-from cur-to)
2005 (cdr (cdr (cdr buffer-undo-list))))))))
2006 ;; Implement -post-goto-bullet, if set: (must be after undo business)
2007 (if (and outline-post-goto-bullet
2008 (outline-current-bullet-pos))
2009 (progn (goto-char (outline-current-bullet-pos))
2010 (setq outline-post-goto-bullet nil)))
2011 ))
2012 ;;;_ > outline-pre-command-business ()
2013 (defun outline-pre-command-business ()
2014 "Outline pre-command-hook function for outline buffers.
2015
2016 Implements special behavior when cursor is on bullet char.
2017
2018 Self-insert characters are reinterpreted control-character references
2019 into the outline-mode-map. The outline-mode post-command hook will
2020 position a cursor that has moved as a result of such reinterpretation,
2021 on the destination topic's bullet, when the cursor wound up in the
2022
2023 The upshot is that you can get easy, single (unmodified) key outline
2024 maneuvering and general operations by positioning the cursor on the
2025 bullet char, and it continues until you deliberately some non-outline
2026 motion command to relocate the cursor off of a bullet char."
2027
2028 (if (and (boundp 'outline-mode)
2029 outline-mode
2030 (eq this-command 'self-insert-command)
2031 (eq (point)(outline-current-bullet-pos)))
2032
2033 (let* ((this-key-num (if (numberp last-command-event)
2034 last-command-event))
2035 mapped-binding)
2036
2037 ; Map upper-register literals
2038 ; to lower register:
2039 (if (<= 96 this-key-num)
2040 (setq this-key-num (- this-key-num 32)))
2041 ; Check if we have a literal:
2042 (if (and (<= 64 this-key-num)
2043 (>= 96 this-key-num))
2044 (setq mapped-binding
2045 (lookup-key 'outline-mode-map
2046 (concat outline-command-prefix
2047 (char-to-string (- this-key-num 64))))))
2048 (if mapped-binding
2049 (setq outline-post-goto-bullet t
2050 this-command mapped-binding)))))
2051 ;;;_ > outline-find-file-hook ()
2052 (defun outline-find-file-hook ()
2053 "Activate outline-mode when `outline-auto-activation' & `outline-layout' are non-nil.
2054
2055 See `outline-init' for setup instructions."
2056 (if (and outline-auto-activation
2057 (not (outline-mode-p))
2058 outline-layout)
2059 (outline-mode t)))
2060 ;;;_ : Establish the hooks
2061 (add-hook 'post-command-hook 'outline-post-command-business)
2062 (add-hook 'pre-command-hook 'outline-pre-command-business)
2063
2064 ;;;_ - Topic Format Assessment
2065 ;;;_ > outline-solicit-alternate-bullet (depth &optional current-bullet)
2066 (defun outline-solicit-alternate-bullet (depth &optional current-bullet)
2067
2068 "Prompt for and return a bullet char as an alternative to the current one.
2069
2070 Offer one suitable for current depth DEPTH as default."
2071
2072 (let* ((default-bullet (or current-bullet
2073 (outline-bullet-for-depth depth)))
2074 (sans-escapes (regexp-sans-escapes outline-bullets-string))
2075 (choice (solicit-char-in-string
2076 (format "Select bullet: %s ('%s' default): "
2077 sans-escapes
2078 default-bullet)
2079 sans-escapes
2080 t)))
2081 (if (string= choice "") default-bullet choice))
2082 )
2083 ;;;_ > outline-sibling-index (&optional depth)
2084 (defun outline-sibling-index (&optional depth)
2085 "Item number of this prospective topic among its siblings.
2086
2087 If optional arg depth is greater than current depth, then we're
2088 opening a new level, and return 0.
2089
2090 If less than this depth, ascend to that depth and count..."
2091
2092 (save-excursion
2093 (cond ((and depth (<= depth 0) 0))
2094 ((or (not depth) (= depth (outline-depth)))
2095 (let ((index 1))
2096 (while (outline-previous-sibling (outline-recent-depth) nil)
2097 (setq index (1+ index)))
2098 index))
2099 ((< depth (outline-recent-depth))
2100 (outline-ascend-to-depth depth)
2101 (outline-sibling-index))
2102 (0))))
2103 ;;;_ > outline-distinctive-bullet (bullet)
2104 (defun outline-distinctive-bullet (bullet)
2105 "True if bullet is one of those on outline-distinctive-bullets-string."
2106 (string-match (regexp-quote bullet) outline-distinctive-bullets-string))
2107 ;;;_ > outline-numbered-type-prefix (&optional prefix)
2108 (defun outline-numbered-type-prefix (&optional prefix)
2109 "True if current header prefix bullet is numbered bullet."
2110 (and outline-numbered-bullet
2111 (string= outline-numbered-bullet
2112 (if prefix
2113 (outline-get-prefix-bullet prefix)
2114 (outline-get-bullet)))))
2115 ;;;_ > outline-bullet-for-depth (&optional depth)
2116 (defun outline-bullet-for-depth (&optional depth)
2117 "Return outline topic bullet suited to optional DEPTH, or current depth."
2118 ;; Find bullet in plain-bullets-string modulo DEPTH.
2119 (if outline-stylish-prefixes
2120 (char-to-string (aref outline-plain-bullets-string
2121 (% (max 0 (- depth 2))
2122 outline-plain-bullets-string-len)))
2123 outline-primary-bullet)
2124 )
2125
2126 ;;;_ - Topic Production
2127 ;;;_ > outline-make-topic-prefix (&optional prior-bullet
2128 (defun outline-make-topic-prefix (&optional prior-bullet
2129 new
2130 depth
2131 solicit
2132 number-control
2133 index)
2134 ;; Depth null means use current depth, non-null means we're either
2135 ;; opening a new topic after current topic, lower or higher, or we're
2136 ;; changing level of current topic.
2137 ;; Solicit dominates specified bullet-char.
2138 ;;;_ . Doc string:
2139 "Generate a topic prefix suitable for optional arg DEPTH, or current depth.
2140
2141 All the arguments are optional.
2142
2143 PRIOR-BULLET indicates the bullet of the prefix being changed, or
2144 nil if none. This bullet may be preserved (other options
2145 notwithstanding) if it is on the outline-distinctive-bullets-string,
2146 for instance.
2147
2148 Second arg NEW indicates that a new topic is being opened after the
2149 topic at point, if non-nil. Default bullet for new topics, eg, may
2150 be set (contingent to other args) to numbered bullets if previous
2151 sibling is one. The implication otherwise is that the current topic
2152 is being adjusted - shifted or rebulleted - and we don't consider
2153 bullet or previous sibling.
2154
2155 Third arg DEPTH forces the topic prefix to that depth, regardless of
2156 the current topics' depth.
2157
2158 Fourth arg SOLICIT non-nil provokes solicitation from the user of a
2159 choice among the valid bullets. (This overrides other all the
2160 options, including, eg, a distinctive PRIOR-BULLET.)
2161
2162 Fifth arg, NUMBER-CONTROL, matters only if 'outline-numbered-bullet'
2163 is non-nil *and* soliciting was not explicitly invoked. Then
2164 NUMBER-CONTROL non-nil forces prefix to either numbered or
2165 denumbered format, depending on the value of the sixth arg, INDEX.
2166
2167 \(Note that NUMBER-CONTROL does *not* apply to level 1 topics. Sorry...)
2168
2169 If NUMBER-CONTROL is non-nil and sixth arg INDEX is non-nil then
2170 the prefix of the topic is forced to be numbered. Non-nil
2171 NUMBER-CONTROL and nil INDEX forces non-numbered format on the
2172 bullet. Non-nil NUMBER-CONTROL and non-nil, non-number INDEX means
2173 that the index for the numbered prefix will be derived, by counting
2174 siblings back to start of level. If INDEX is a number, then that
2175 number is used as the index for the numbered prefix (allowing, eg,
2176 sequential renumbering to not requre this function counting back the
2177 index for each successive sibling)."
2178 ;;;_ . Code:
2179 ;; The options are ordered in likely frequence of use, most common
2180 ;; highest, least lowest. Ie, more likely to be doing prefix
2181 ;; adjustments than soliciting, and yet more than numbering.
2182 ;; Current prefix is least dominant, but most likely to be commonly
2183 ;; specified...
2184
2185 (let* (body
2186 numbering
2187 denumbering
2188 (depth (or depth (outline-depth)))
2189 (header-lead outline-header-prefix)
2190 (bullet-char
2191
2192 ;; Getting value for bullet char is practically the whole job:
2193
2194 (cond
2195 ; Simplest situation - level 1:
2196 ((<= depth 1) (setq header-lead "") outline-primary-bullet)
2197 ; Simple, too: all asterisks:
2198 (outline-old-style-prefixes
2199 ;; Cheat - make body the whole thing, null out header-lead and
2200 ;; bullet-char:
2201 (setq body (make-string depth
2202 (string-to-char outline-primary-bullet)))
2203 (setq header-lead "")
2204 "")
2205
2206 ;; (Neither level 1 nor old-style, so we're space padding.
2207 ;; Sneak it in the condition of the next case, whatever it is.)
2208
2209 ;; Solicitation overrides numbering and other cases:
2210 ((progn (setq body (make-string (- depth 2) ?\ ))
2211 ;; The actual condition:
2212 solicit)
2213 (let* ((got (outline-solicit-alternate-bullet depth)))
2214 ;; Gotta check whether we're numbering and got a numbered bullet:
2215 (setq numbering (and outline-numbered-bullet
2216 (not (and number-control (not index)))
2217 (string= got outline-numbered-bullet)))
2218 ;; Now return what we got, regardless:
2219 got))
2220
2221 ;; Numbering invoked through args:
2222 ((and outline-numbered-bullet number-control)
2223 (if (setq numbering (not (setq denumbering (not index))))
2224 outline-numbered-bullet
2225 (if (and prior-bullet
2226 (not (string= outline-numbered-bullet
2227 prior-bullet)))
2228 prior-bullet
2229 (outline-bullet-for-depth depth))))
2230
2231 ;;; Neither soliciting nor controlled numbering ;;;
2232 ;;; (may be controlled denumbering, tho) ;;;
2233
2234 ;; Check wrt previous sibling:
2235 ((and new ; only check for new prefixes
2236 (<= depth (outline-depth))
2237 outline-numbered-bullet ; ... & numbering enabled
2238 (not denumbering)
2239 (let ((sibling-bullet
2240 (save-excursion
2241 ;; Locate correct sibling:
2242 (or (>= depth (outline-depth))
2243 (outline-ascend-to-depth depth))
2244 (outline-get-bullet))))
2245 (if (and sibling-bullet
2246 (string= outline-numbered-bullet sibling-bullet))
2247 (setq numbering sibling-bullet)))))
2248
2249 ;; Distinctive prior bullet?
2250 ((and prior-bullet
2251 (outline-distinctive-bullet prior-bullet)
2252 ;; Either non-numbered:
2253 (or (not (and outline-numbered-bullet
2254 (string= prior-bullet outline-numbered-bullet)))
2255 ;; or numbered, and not denumbering:
2256 (setq numbering (not denumbering)))
2257 ;; Here 'tis:
2258 prior-bullet))
2259
2260 ;; Else, standard bullet per depth:
2261 ((outline-bullet-for-depth depth)))))
2262
2263 (concat header-lead
2264 body
2265 bullet-char
2266 (if numbering
2267 (format "%d" (cond ((and index (numberp index)) index)
2268 (new (1+ (outline-sibling-index depth)))
2269 ((outline-sibling-index))))))
2270 )
2271 )
2272 ;;;_ > outline-open-topic (relative-depth &optional before)
2273 (defun outline-open-topic (relative-depth &optional before)
2274 "Open a new topic at depth DEPTH.
2275
2276 New topic is situated after current one, unless optional flag BEFORE
2277 is non-nil, or unless current line is complete empty (not even
2278 whitespace), in which case open is done on current line.
2279
2280 Nuances:
2281
2282 - Creation of new topics is with respect to the visible topic
2283 containing the cursor, regardless of intervening concealed ones.
2284
2285 - New headers are generally created after/before the body of a
2286 topic. However, they are created right at cursor location if the
2287 cursor is on a blank line, even if that breaks the current topic
2288 body. This is intentional, to provide a simple means for
2289 deliberately dividing topic bodies.
2290
2291 - Double spacing of topic lists is preserved. Also, the first
2292 level two topic is created double-spaced (and so would be
2293 subsequent siblings, if that's left intact). Otherwise,
2294 single-spacing is used.
2295
2296 - Creation of sibling or nested topics is with respect to the topic
2297 you're starting from, even when creating backwards. This way you
2298 can easily create a sibling in front of the current topic without
2299 having to go to its preceeding sibling, and then open forward
2300 from there."
2301
2302 (let* ((depth (+ (outline-current-depth) relative-depth))
2303 (opening-on-blank (if (looking-at "^\$")
2304 (not (setq before nil))))
2305 opening-numbered ; Will get while computing ref-topic, below
2306 ref-depth ; Will get while computing ref-topic, next
2307 (ref-topic (save-excursion
2308 (cond ((< relative-depth 0)
2309 (outline-ascend-to-depth depth))
2310 ((>= relative-depth 1) nil)
2311 (t (outline-back-to-current-heading)))
2312 (setq ref-depth (outline-recent-depth))
2313 (setq opening-numbered
2314 (save-excursion
2315 (and outline-numbered-bullet
2316 (or (<= relative-depth 0)
2317 (outline-descend-to-depth depth))
2318 (if (outline-numbered-type-prefix)
2319 outline-numbered-bullet))))
2320 (point)))
2321 dbl-space
2322 doing-beginning)
2323
2324 (if (not opening-on-blank)
2325 ; Positioning and vertical
2326 ; padding - only if not
2327 ; opening-on-blank:
2328 (progn
2329 (goto-char ref-topic)
2330 (setq dbl-space ; Determine double space action:
2331 (or (and (<= relative-depth 0) ; not descending;
2332 (save-excursion
2333 ;; at b-o-b or preceeded by a blank line?
2334 (or (> 0 (forward-line -1))
2335 (looking-at "^\\s-*$")
2336 (bobp)))
2337 (save-excursion
2338 ;; succeeded by a blank line?
2339 (outline-end-of-current-subtree)
2340 (bolp)))
2341 (and (= ref-depth 1)
2342 (or before
2343 (= depth 1)
2344 (save-excursion
2345 ;; Don't already have following
2346 ;; vertical padding:
2347 (not (outline-pre-next-preface)))))))
2348
2349 ; Position to prior heading,
2350 ; if inserting backwards, and
2351 ; not going outwards:
2352 (if (and before (>= relative-depth 0))
2353 (progn (outline-back-to-current-heading)
2354 (setq doing-beginning (bobp))
2355 (if (not (bobp))
2356 (outline-previous-heading)))
2357 (if (and before (bobp))
2358 (outline-unprotected (open-line 1))))
2359
2360 (if (<= relative-depth 0)
2361 ;; Not going inwards, don't snug up:
2362 (if doing-beginning
2363 (outline-unprotected (open-line (if dbl-space 2 1)))
2364 (if before
2365 (progn (end-of-line)
2366 (outline-pre-next-preface)
2367 (while (= ?\r (following-char))
2368 (forward-char 1))
2369 (if (not (looking-at "^$"))
2370 (outline-unprotected (open-line 1))))
2371 (outline-end-of-current-subtree)))
2372 ;; Going inwards - double-space if first offspring is,
2373 ;; otherwise snug up.
2374 (end-of-line) ; So we skip any concealed progeny.
2375 (outline-pre-next-preface)
2376 (if (bolp)
2377 ;; Blank lines between current header body and next
2378 ;; header - get to last substantive (non-white-space)
2379 ;; line in body:
2380 (re-search-backward "[^ \t\n]" nil t))
2381 (if (save-excursion
2382 (outline-next-heading)
2383 (if (> (outline-recent-depth) ref-depth)
2384 ;; This is an offspring.
2385 (progn (forward-line -1)
2386 (looking-at "^\\s-*$"))))
2387 (progn (forward-line 1)
2388 (outline-unprotected (open-line 1))))
2389 (end-of-line))
2390 ;;(if doing-beginning (goto-char doing-beginning))
2391 (if (not (bobp))
2392 (progn (if (and (not (> depth ref-depth))
2393 (not before))
2394 (outline-unprotected (open-line 1))
2395 (if (> depth ref-depth)
2396 (outline-unprotected (newline 1))
2397 (if dbl-space
2398 (outline-unprotected (open-line 1))
2399 (if (not before)
2400 (outline-unprotected (newline 1))))))
2401 (if dbl-space
2402 (outline-unprotected (newline 1)))
2403 (if (and (not (eobp))
2404 (not (bolp)))
2405 (forward-char 1))))
2406 ))
2407 (insert-string (concat (outline-make-topic-prefix opening-numbered
2408 t
2409 depth)
2410 " "))
2411
2412 ;;(if doing-beginning (save-excursion (newline (if dbl-space 2 1))))
2413
2414
2415 (outline-rebullet-heading nil ;;; solicit
2416 depth ;;; depth
2417 nil ;;; number-control
2418 nil ;;; index
2419 t) (end-of-line)
2420 )
2421 )
2422 ;;;_ . open-topic contingencies
2423 ;;;_ ; base topic - one from which open was issued
2424 ;;;_ , beginning char
2425 ;;;_ , amount of space before will be used, unless openning in place
2426 ;;;_ , end char will be used, unless opening before (and it still may)
2427 ;;;_ ; absolute depth of new topic
2428 ;;;_ ! insert in place - overrides most stuff
2429 ;;;_ ; relative depth of new re base
2430 ;;;_ ; before or after base topic
2431 ;;;_ ; spacing around topic, if any, prior to new topic and at same depth
2432 ;;;_ ; buffer boundaries - special provisions for beginning and end ob
2433 ;;;_ ; level 1 topics have special provisions also - double space.
2434 ;;;_ ; location of new topic
2435 ;;;_ .
2436 ;;;_ > outline-open-subtopic (arg)
2437 (defun outline-open-subtopic (arg)
2438 "Open new topic header at deeper level than the current one.
2439
2440 Negative universal arg means to open deeper, but place the new topic
2441 prior to the current one."
2442 (interactive "p")
2443 (outline-open-topic 1 (> 0 arg)))
2444 ;;;_ > outline-open-sibtopic (arg)
2445 (defun outline-open-sibtopic (arg)
2446 "Open new topic header at same level as the current one.
2447
2448 Negative universal arg means to place the new topic prior to the current
2449 one."
2450 (interactive "p")
2451 (outline-open-topic 0 (> 0 arg)))
2452 ;;;_ > outline-open-supertopic (arg)
2453 (defun outline-open-supertopic (arg)
2454 "Open new topic header at shallower level than the current one.
2455
2456 Negative universal arg means to open shallower, but place the new
2457 topic prior to the current one."
2458
2459 (interactive "p")
2460 (outline-open-topic -1 (> 0 arg)))
2461
2462 ;;;_ - Outline Alteration
2463 ;;;_ : Topic Modification
2464 ;;;_ = outline-former-auto-filler
2465 (defvar outline-former-auto-filler nil
2466 "Name of modal fill function being wrapped by outline-auto-fill.")
2467 ;;;_ > outline-auto-fill ()
2468 (defun outline-auto-fill ()
2469 "Outline-mode autofill function.
2470
2471 Maintains outline hanging topic indentation if
2472 `outline-use-hanging-indents' is set."
2473 (let ((fill-prefix (if outline-use-hanging-indents
2474 ;; Check for topic header indentation:
2475 (save-excursion
2476 (beginning-of-line)
2477 (if (looking-at outline-regexp)
2478 ;; ... construct indentation to account for
2479 ;; length of topic prefix:
2480 (make-string (progn (outline-end-of-prefix)
2481 (current-column))
2482 ?\ ))))))
2483 (if (or outline-former-auto-filler outline-use-hanging-indents)
2484 (do-auto-fill))))
2485 ;;;_ > outline-reindent-body (old-depth new-depth &optional number)
2486 (defun outline-reindent-body (old-depth new-depth &optional number)
2487 "Reindent body lines which were indented at old-depth to new-depth.
2488
2489 Optional arg NUMBER indicates numbering is being added, and it must
2490 be accomodated.
2491
2492 Note that refill of indented paragraphs is not done."
2493
2494 (save-excursion
2495 (outline-end-of-prefix)
2496 (let* ((new-margin (current-column))
2497 excess old-indent-begin old-indent-end
2498 curr-ind
2499 ;; We want the column where the header-prefix text started
2500 ;; *before* the prefix was changed, so we infer it relative
2501 ;; to the new margin and the shift in depth:
2502 (old-margin (+ old-depth (- new-margin new-depth))))
2503
2504 ;; Process lines up to (but excluding) next topic header:
2505 (outline-unprotected
2506 (save-match-data
2507 (while
2508 (and (re-search-forward "[\n\r]\\(\\s-*\\)"
2509 nil
2510 t)
2511 ;; Register the indent data, before we reset the
2512 ;; match data with a subsequent 'looking-at':
2513 (setq old-indent-begin (match-beginning 1)
2514 old-indent-end (match-end 1))
2515 (not (looking-at outline-regexp)))
2516 (if (> 0 (setq excess (- (current-column)
2517 old-margin)))
2518 ;; Text starts left of old margin - don't adjust:
2519 nil
2520 ;; Text was hanging at or right of old left margin -
2521 ;; reindent it, preserving its existing indentation
2522 ;; beyond the old margin:
2523 (delete-region old-indent-begin old-indent-end)
2524 (indent-to (+ new-margin excess)))))))))
2525 ;;;_ > outline-rebullet-current-heading (arg)
2526 (defun outline-rebullet-current-heading (arg)
2527 "Like non-interactive version 'outline-rebullet-heading'.
2528
2529 But \(only\) affects visible heading containing point.
2530
2531 With repeat count, solicit for bullet."
2532 (interactive "P")
2533 (save-excursion (outline-back-to-current-heading)
2534 (outline-end-of-prefix)
2535 (outline-rebullet-heading (not arg) ;;; solicit
2536 nil ;;; depth
2537 nil ;;; number-control
2538 nil ;;; index
2539 t) ;;; do-successors
2540 )
2541 )
2542 ;;;_ > outline-rebullet-heading (&optional solicit ...)
2543 (defun outline-rebullet-heading (&optional solicit
2544 new-depth
2545 number-control
2546 index
2547 do-successors)
2548
2549 "Adjust bullet of current topic prefix.
2550
2551 All args are optional.
2552
2553 If SOLICIT is non-nil then the choice of bullet is solicited from
2554 user. Otherwise the distinctiveness of the bullet or the topic
2555 depth determines it.
2556
2557 Second arg DEPTH forces the topic prefix to that depth, regardless
2558 of the topics current depth.
2559
2560 Third arg NUMBER-CONTROL can force the prefix to or away from
2561 numbered form. It has effect only if 'outline-numbered-bullet' is
2562 non-nil and soliciting was not explicitly invoked (via first arg).
2563 Its effect, numbering or denumbering, then depends on the setting
2564 of the forth arg, INDEX.
2565
2566 If NUMBER-CONTROL is non-nil and forth arg INDEX is nil, then the
2567 prefix of the topic is forced to be non-numbered. Null index and
2568 non-nil NUMBER-CONTROL forces denumbering. Non-nil INDEX (and
2569 non-nil NUMBER-CONTROL) forces a numbered-prefix form. If non-nil
2570 INDEX is a number, then that number is used for the numbered
2571 prefix. Non-nil and non-number means that the index for the
2572 numbered prefix will be derived by outline-make-topic-prefix.
2573
2574 Fifth arg DO-SUCCESSORS t means re-resolve count on succeeding
2575 siblings.
2576
2577 Cf vars 'outline-stylish-prefixes', 'outline-old-style-prefixes',
2578 and 'outline-numbered-bullet', which all affect the behavior of
2579 this function."
2580
2581 (let* ((current-depth (outline-depth))
2582 (new-depth (or new-depth current-depth))
2583 (mb outline-recent-prefix-beginning)
2584 (me outline-recent-prefix-end)
2585 (current-bullet (buffer-substring (- me 1) me))
2586 (new-prefix (outline-make-topic-prefix current-bullet
2587 nil
2588 new-depth
2589 solicit
2590 number-control
2591 index)))
2592
2593 ;; Is new one is identical to old?
2594 (if (and (= current-depth new-depth)
2595 (string= current-bullet
2596 (substring new-prefix (1- (length new-prefix)))))
2597 ;; Nothing to do:
2598 t
2599
2600 ;; New prefix probably different from old:
2601 ; get rid of old one:
2602 (outline-unprotected (delete-region mb me))
2603 (goto-char mb)
2604 ; Dispense with number if
2605 ; numbered-bullet prefix:
2606 (if (and outline-numbered-bullet
2607 (string= outline-numbered-bullet current-bullet)
2608 (looking-at "[0-9]+"))
2609 (outline-unprotected
2610 (delete-region (match-beginning 0)(match-end 0))))
2611
2612 ; Put in new prefix:
2613 (outline-unprotected (insert-string new-prefix))
2614
2615 ;; Reindent the body if elected and margin changed:
2616 (if (and outline-reindent-bodies
2617 (not (= new-depth current-depth)))
2618 (outline-reindent-body current-depth new-depth))
2619
2620 ;; Recursively rectify successive siblings of orig topic if
2621 ;; caller elected for it:
2622 (if do-successors
2623 (save-excursion
2624 (while (outline-next-sibling new-depth nil)
2625 (setq index
2626 (cond ((numberp index) (1+ index))
2627 ((not number-control) (outline-sibling-index))))
2628 (if (outline-numbered-type-prefix)
2629 (outline-rebullet-heading nil ;;; solicit
2630 new-depth ;;; new-depth
2631 number-control;;; number-control
2632 index ;;; index
2633 nil))))) ;;;(dont!)do-successors
2634 ) ; (if (and (= current-depth new-depth)...))
2635 ) ; let* ((current-depth (outline-depth))...)
2636 ) ; defun
2637 ;;;_ > outline-rebullet-topic (arg)
2638 (defun outline-rebullet-topic (arg)
2639 "Like outline-rebullet-topic-grunt, but start from topic visible at point.
2640
2641 Descends into invisible as well as visible topics, however.
2642
2643 With repeat count, shift topic depth by that amount."
2644 (interactive "P")
2645 (let ((start-col (current-column))
2646 (was-eol (eolp)))
2647 (save-excursion
2648 ;; Normalize arg:
2649 (cond ((null arg) (setq arg 0))
2650 ((listp arg) (setq arg (car arg))))
2651 ;; Fill the user in, in case we're shifting a big topic:
2652 (if (not (zerop arg)) (message "Shifting..."))
2653 (outline-back-to-current-heading)
2654 (if (<= (+ (outline-recent-depth) arg) 0)
2655 (error "Attempt to shift topic below level 1"))
2656 (outline-rebullet-topic-grunt arg)
2657 (if (not (zerop arg)) (message "Shifting... done.")))
2658 (move-to-column (max 0 (+ start-col arg)))))
2659 ;;;_ > outline-rebullet-topic-grunt (&optional relative-depth ...)
2660 (defun outline-rebullet-topic-grunt (&optional relative-depth
2661 starting-depth
2662 starting-point
2663 index
2664 do-successors)
2665
2666 "Rebullet the topic at point, visible or invisible, and all
2667 contained subtopics. See outline-rebullet-heading for rebulleting
2668 behavior.
2669
2670 All arguments are optional.
2671
2672 First arg RELATIVE-DEPTH means to shift the depth of the entire
2673 topic that amount.
2674
2675 The rest of the args are for internal recursive use by the function
2676 itself. The are STARTING-DEPTH, STARTING-POINT, and INDEX."
2677
2678 (let* ((relative-depth (or relative-depth 0))
2679 (new-depth (outline-depth))
2680 (starting-depth (or starting-depth new-depth))
2681 (on-starting-call (null starting-point))
2682 (index (or index
2683 ;; Leave index null on starting call, so rebullet-heading
2684 ;; calculates it at what might be new depth:
2685 (and (or (zerop relative-depth)
2686 (not on-starting-call))
2687 (outline-sibling-index))))
2688 (moving-outwards (< 0 relative-depth))
2689 (starting-point (or starting-point (point))))
2690
2691 ;; Sanity check for excessive promotion done only on starting call:
2692 (and on-starting-call
2693 moving-outwards
2694 (> 0 (+ starting-depth relative-depth))
2695 (error "Attempt to shift topic out beyond level 1.")) ;;; ====>
2696
2697 (cond ((= starting-depth new-depth)
2698 ;; We're at depth to work on this one:
2699 (outline-rebullet-heading nil ;;; solicit
2700 (+ starting-depth ;;; starting-depth
2701 relative-depth)
2702 nil ;;; number
2703 index ;;; index
2704 ;; Every contained topic will get hit,
2705 ;; and we have to get to outside ones
2706 ;; deliberately:
2707 nil) ;;; do-successors
2708 ;; ... and work on subsequent ones which are at greater depth:
2709 (setq index 0)
2710 (outline-next-heading)
2711 (while (and (not (eobp))
2712 (< starting-depth (outline-recent-depth)))
2713 (setq index (1+ index))
2714 (outline-rebullet-topic-grunt relative-depth ;;; relative-depth
2715 (1+ starting-depth);;;starting-depth
2716 starting-point ;;; starting-point
2717 index))) ;;; index
2718
2719 ((< starting-depth new-depth)
2720 ;; Rare case - subtopic more than one level deeper than parent.
2721 ;; Treat this one at an even deeper level:
2722 (outline-rebullet-topic-grunt relative-depth ;;; relative-depth
2723 new-depth ;;; starting-depth
2724 starting-point ;;; starting-point
2725 index))) ;;; index
2726
2727 (if on-starting-call
2728 (progn
2729 ;; Rectify numbering of former siblings of the adjusted topic,
2730 ;; if topic has changed depth
2731 (if (or do-successors
2732 (and (not (zerop relative-depth))
2733 (or (= (outline-recent-depth) starting-depth)
2734 (= (outline-recent-depth) (+ starting-depth
2735 relative-depth)))))
2736 (outline-rebullet-heading nil nil nil nil t))
2737 ;; Now rectify numbering of new siblings of the adjusted topic,
2738 ;; if depth has been changed:
2739 (progn (goto-char starting-point)
2740 (if (not (zerop relative-depth))
2741 (outline-rebullet-heading nil nil nil nil t)))))
2742 )
2743 )
2744 ;;;_ > outline-renumber-to-depth (&optional depth)
2745 (defun outline-renumber-to-depth (&optional depth)
2746 "Renumber siblings at current depth.
2747
2748 Affects superior topics if optional arg DEPTH is less than current depth.
2749
2750 Returns final depth."
2751
2752 ;; Proceed by level, processing subsequent siblings on each,
2753 ;; ascending until we get shallower than the start depth:
2754
2755 (let ((ascender (outline-depth)))
2756 (while (and (not (eobp))
2757 (outline-depth)
2758 (>= (outline-recent-depth) depth)
2759 (>= ascender depth))
2760 ; Skip over all topics at
2761 ; lesser depths, which can not
2762 ; have been disturbed:
2763 (while (and (not (eobp))
2764 (> (outline-recent-depth) ascender))
2765 (outline-next-heading))
2766 ; Prime ascender for ascension:
2767 (setq ascender (1- (outline-recent-depth)))
2768 (if (>= (outline-recent-depth) depth)
2769 (outline-rebullet-heading nil ;;; solicit
2770 nil ;;; depth
2771 nil ;;; number-control
2772 nil ;;; index
2773 t))));;; do-successors
2774 (outline-recent-depth))
2775 ;;;_ > outline-number-siblings (&optional denumber)
2776 (defun outline-number-siblings (&optional denumber)
2777 "Assign numbered topic prefix to this topic and its siblings.
2778
2779 With universal argument, denumber - assign default bullet to this
2780 topic and its siblings.
2781
2782 With repeated universal argument (`^U^U'), solicit bullet for each
2783 rebulleting each topic at this level."
2784
2785 (interactive "P")
2786
2787 (save-excursion
2788 (outline-back-to-current-heading)
2789 (outline-beginning-of-level)
2790 (let ((depth (outline-recent-depth))
2791 (index (if (not denumber) 1))
2792 (use-bullet (equal '(16) denumber))
2793 (more t))
2794 (while more
2795 (outline-rebullet-heading use-bullet ;;; solicit
2796 depth ;;; depth
2797 t ;;; number-control
2798 index ;;; index
2799 nil) ;;; do-successors
2800 (if index (setq index (1+ index)))
2801 (setq more (outline-next-sibling depth nil))))))
2802 ;;;_ > outline-shift-in (arg)
2803 (defun outline-shift-in (arg)
2804 "Increase depth of current heading and any topics collapsed within it."
2805 (interactive "p")
2806 (outline-rebullet-topic arg))
2807 ;;;_ > outline-shift-out (arg)
2808 (defun outline-shift-out (arg)
2809 "Decrease depth of current heading and any topics collapsed within it."
2810 (interactive "p")
2811 (outline-rebullet-topic (* arg -1)))
2812 ;;;_ : Surgery (kill-ring) functions with special provisions for outlines:
2813 ;;;_ > outline-kill-line (&optional arg)
2814 (defun outline-kill-line (&optional arg)
2815 "Kill line, adjusting subsequent lines suitably for outline mode."
2816
2817 (interactive "*P")
2818 (if (not (and (outline-mode-p) ; active outline mode,
2819 outline-numbered-bullet ; numbers may need adjustment,
2820 (bolp) ; may be clipping topic head,
2821 (looking-at outline-regexp))) ; are clipping topic head.
2822 ;; Above conditions do not obtain - just do a regular kill:
2823 (kill-line arg)
2824 ;; Ah, have to watch out for adjustments:
2825 (let* ((depth (outline-depth)))
2826 ; Do the kill:
2827 (kill-line arg)
2828 ; Provide some feedback:
2829 (sit-for 0)
2830 (save-excursion
2831 ; Start with the topic
2832 ; following killed line:
2833 (if (not (looking-at outline-regexp))
2834 (outline-next-heading))
2835 (outline-renumber-to-depth depth)))))
2836 ;;;_ > outline-kill-topic ()
2837 (defun outline-kill-topic ()
2838 "Kill topic together with subtopics.
2839
2840 Leaves primary topic's trailing vertical whitespace, if any."
2841
2842 ;; Some finagling is done to make complex topic kills appear faster
2843 ;; than they actually are. A redisplay is performed immediately
2844 ;; after the region is disposed of, though the renumbering process
2845 ;; has yet to be performed. This means that there may appear to be
2846 ;; a lag *after* the kill has been performed.
2847
2848 (interactive)
2849 (let* ((beg (prog1 (outline-back-to-current-heading)(beginning-of-line)))
2850 (depth (outline-recent-depth)))
2851 (outline-end-of-current-subtree)
2852 (if (not (eobp))
2853 (if (or (not (looking-at "^$"))
2854 ;; A blank line - cut it with this topic *unless* this
2855 ;; is the last topic at this level, in which case
2856 ;; we'll leave the blank line as part of the
2857 ;; containing topic:
2858 (save-excursion
2859 (and (outline-next-heading)
2860 (>= (outline-recent-depth) depth))))
2861 (forward-char 1)))
2862
2863 (kill-region beg (point))
2864 (sit-for 0)
2865 (save-excursion
2866 (outline-renumber-to-depth depth))))
2867 ;;;_ > outline-yank-processing ()
2868 (defun outline-yank-processing (&optional arg)
2869
2870 "Incidental outline-specific business to be done just after text yanks.
2871
2872 Does depth adjustment of yanked topics, when:
2873
2874 1 the stuff being yanked starts with a valid outline header prefix, and
2875 2 it is being yanked at the end of a line which consists of only a valid
2876 topic prefix.
2877
2878 Also, adjusts numbering of subsequent siblings when appropropriate.
2879
2880 Depth adjustment alters the depth of all the topics being yanked
2881 the amount it takes to make the first topic have the depth of the
2882 header into which it's being yanked.
2883
2884 The point is left in front of yanked, adjusted topics, rather than
2885 at the end (and vice-versa with the mark). Non-adjusted yanks,
2886 however, are left exactly like normal, non-outline-specific yanks."
2887
2888 (interactive "*P")
2889 ; Get to beginning, leaving
2890 ; region around subject:
2891 (if (< (mark-marker) (point))
2892 (exchange-point-and-mark))
2893 (let* ((subj-beg (point))
2894 (subj-end (mark-marker))
2895 ;; 'resituate' if yanking an entire topic into topic header:
2896 (resituate (and (outline-e-o-prefix-p)
2897 (looking-at (concat "\\(" outline-regexp "\\)"))
2898 (outline-prefix-data (match-beginning 1)
2899 (match-end 1))))
2900 ;; 'rectify-numbering' if resituating (where several topics may
2901 ;; be resituating) or yanking a topic into a topic slot (bol):
2902 (rectify-numbering (or resituate
2903 (and (bolp) (looking-at outline-regexp)))))
2904 (if resituate
2905 ; The yanked stuff is a topic:
2906 (let* ((prefix-len (- (match-end 1) subj-beg))
2907 (subj-depth (outline-recent-depth))
2908 (prefix-bullet (outline-recent-bullet))
2909 (adjust-to-depth
2910 ;; Nil if adjustment unnecessary, otherwise depth to which
2911 ;; adjustment should be made:
2912 (save-excursion
2913 (and (goto-char subj-end)
2914 (eolp)
2915 (goto-char subj-beg)
2916 (and (looking-at outline-regexp)
2917 (progn
2918 (beginning-of-line)
2919 (not (= (point) subj-beg)))
2920 (looking-at outline-regexp)
2921 (outline-prefix-data (match-beginning 0)
2922 (match-end 0)))
2923 (outline-recent-depth))))
2924 done
2925 (more t))
2926 (setq rectify-numbering outline-numbered-bullet)
2927 (if adjust-to-depth
2928 ; Do the adjustment:
2929 (progn
2930 (message "... yanking") (sit-for 0)
2931 (save-restriction
2932 (narrow-to-region subj-beg subj-end)
2933 ; Trim off excessive blank
2934 ; line at end, if any:
2935 (goto-char (point-max))
2936 (if (looking-at "^$")
2937 (outline-unprotected (delete-char -1)))
2938 ; Work backwards, with each
2939 ; shallowest level,
2940 ; successively excluding the
2941 ; last processed topic from
2942 ; the narrow region:
2943 (while more
2944 (outline-back-to-current-heading)
2945 ; go as high as we can in each bunch:
2946 (while (outline-ascend-to-depth (1- (outline-depth))))
2947 (save-excursion
2948 (outline-rebullet-topic-grunt (- adjust-to-depth
2949 subj-depth))
2950 (outline-depth))
2951 (if (setq more (not (bobp)))
2952 (progn (widen)
2953 (forward-char -1)
2954 (narrow-to-region subj-beg (point))))))
2955 (message "")
2956 ;; Preserve new bullet if it's a distinctive one, otherwise
2957 ;; use old one:
2958 (if (string-match (regexp-quote prefix-bullet)
2959 outline-distinctive-bullets-string)
2960 ; Delete from bullet of old to
2961 ; before bullet of new:
2962 (progn
2963 (beginning-of-line)
2964 (delete-region (point) subj-beg)
2965 (set-marker (mark-marker) subj-end)
2966 (goto-char subj-beg)
2967 (outline-end-of-prefix))
2968 ; Delete base subj prefix,
2969 ; leaving old one:
2970 (delete-region (point) (+ (point)
2971 prefix-len
2972 (- adjust-to-depth subj-depth)))
2973 ; and delete residual subj
2974 ; prefix digits and space:
2975 (while (looking-at "[0-9]") (delete-char 1))
2976 (if (looking-at " ") (delete-char 1))))
2977 (exchange-point-and-mark))))
2978 (if rectify-numbering
2979 (progn
2980 (save-excursion
2981 ; Give some preliminary feedback:
2982 (message "... reconciling numbers") (sit-for 0)
2983 ; ... and renumber, in case necessary:
2984 (goto-char subj-beg)
2985 (if (outline-goto-prefix)
2986 (outline-rebullet-heading nil ;;; solicit
2987 (outline-depth) ;;; depth
2988 nil ;;; number-control
2989 nil ;;; index
2990 t))
2991 (message ""))))
2992 (if (not resituate)
2993 (exchange-point-and-mark))))
2994 ;;;_ > outline-yank (&optional arg)
2995 (defun outline-yank (&optional arg)
2996 "Outline-mode yank, with depth and numbering adjustment of yanked topics.
2997
2998 Non-topic yanks work no differntly than normal yanks.
2999
3000 If a topic is being yanked into a bare topic prefix, the depth of the
3001 yanked topic is adjusted to the depth of the topic prefix.
3002
3003 1 we're yanking in an outline-mode buffer
3004 2 the stuff being yanked starts with a valid outline header prefix, and
3005 3 it is being yanked at the end of a line which consists of only a valid
3006 topic prefix.
3007
3008 If these conditions hold then the depth of the yanked topics are all
3009 adjusted the amount it takes to make the first one at the depth of the
3010 header into which it's being yanked.
3011
3012 The point is left in front of yanked, adjusted topics, rather than
3013 at the end (and vice-versa with the mark). Non-adjusted yanks,
3014 however, (ones that don't qualify for adjustment) are handled
3015 exactly like normal yanks.
3016
3017 Numbering of yanked topics, and the succesive siblings at the depth
3018 into which they're being yanked, is adjusted.
3019
3020 Outline-yank-pop works with outline-yank just like normal yank-pop
3021 works with normal yank in non-outline buffers."
3022
3023 (interactive "*P")
3024 (setq this-command 'yank)
3025 (yank arg)
3026 (if (outline-mode-p)
3027 (outline-yank-processing)))
3028 ;;;_ > outline-yank-pop (&optional arg)
3029 (defun outline-yank-pop (&optional arg)
3030 "Yank-pop like outline-yank when popping to bare outline prefixes.
3031
3032 Adapts level of popped topics to level of fresh prefix.
3033
3034 Note - prefix changes to distinctive bullets will stick, if followed
3035 by pops to non-distinctive yanks. Bug..."
3036
3037 (interactive "*p")
3038 (setq this-command 'yank)
3039 (yank-pop arg)
3040 (if (outline-mode-p)
3041 (outline-yank-processing)))
3042
3043 ;;;_ - Specialty bullet functions
3044 ;;;_ : File Cross references
3045 ;;;_ > outline-resolve-xref ()
3046 (defun outline-resolve-xref ()
3047 "Pop to file associated with current heading, if it has an xref bullet.
3048
3049 \(Works according to setting of `outline-file-xref-bullet')."
3050 (interactive)
3051 (if (not outline-file-xref-bullet)
3052 (error
3053 "outline cross references disabled - no 'outline-file-xref-bullet'")
3054 (if (not (string= (outline-current-bullet) outline-file-xref-bullet))
3055 (error "current heading lacks cross-reference bullet '%s'"
3056 outline-file-xref-bullet)
3057 (let (file-name)
3058 (save-excursion
3059 (let* ((text-start outline-recent-prefix-end)
3060 (heading-end (progn (outline-pre-next-preface)
3061 (point))))
3062 (goto-char text-start)
3063 (setq file-name
3064 (if (re-search-forward "\\s-\\(\\S-*\\)" heading-end t)
3065 (buffer-substring (match-beginning 1) (match-end 1))))))
3066 (setq file-name
3067 (if (not (= (aref file-name 0) ?:))
3068 (expand-file-name file-name)
3069 ; A registry-files ref, strip the ':'
3070 ; and try to follow it:
3071 (let ((reg-ref (reference-registered-file
3072 (substring file-name 1) nil t)))
3073 (if reg-ref (car (cdr reg-ref))))))
3074 (if (or (file-exists-p file-name)
3075 (if (file-writable-p file-name)
3076 (y-or-n-p (format "%s not there, create one? "
3077 file-name))
3078 (error "%s not found and can't be created" file-name)))
3079 (condition-case failure
3080 (find-file-other-window file-name)
3081 (error failure))
3082 (error "%s not found" file-name))
3083 )
3084 )
3085 )
3086 )
3087
3088 ;;;_ #6 Exposure Control and Processing
3089
3090 ;;;_ - Fundamental
3091 ;;;_ > outline-flag-region (from to flag)
3092 (defmacro outline-flag-region (from to flag)
3093 "Hide or show lines from FROM to TO, via emacs selective-display FLAG char.
3094 Ie, text following flag C-m \(carriage-return) is hidden until the
3095 next C-j (newline) char.
3096
3097 Returns the endpoint of the region."
3098 (` (let ((buffer-read-only nil)
3099 (outline-override-protect t))
3100 (subst-char-in-region (, from) (, to)
3101 (if (= (, flag) ?\n) ?\r ?\n)
3102 (, flag) t))))
3103 ;;;_ > outline-flag-current-subtree (flag)
3104 (defun outline-flag-current-subtree (flag)
3105 "Hide or show subtree of currently-visible topic.
3106
3107 See `outline-flag-region' for more details."
3108
3109 (save-excursion
3110 (outline-back-to-current-heading)
3111 (outline-flag-region (point)
3112 (progn (outline-end-of-current-subtree) (1- (point)))
3113 flag)))
3114
3115 ;;;_ - Mapping and processing of topics
3116 ;;;_ " See also chart functions, in navigation
3117 ;;;_ > outline-listify-exposed (&optional start end)
3118 (defun outline-listify-exposed (&optional start end)
3119
3120 "Produce a list representing exposed topics in current region.
3121
3122 This list can then be used by 'outline-process-exposed' to manipulate
3123 the subject region.
3124
3125 List is composed of elements that may themselves be lists representing
3126 exposed components in subtopic.
3127
3128 Each component list contains:
3129 - a number representing the depth of the topic,
3130 - a string representing the header-prefix (ref. 'outline-header-prefix'),
3131 - a string representing the bullet character,
3132 - and a series of strings, each containing one line of the exposed
3133 portion of the topic entry."
3134
3135 (interactive "r")
3136 (save-excursion
3137 (let* (strings pad result depth bullet beg next done) ; State vars.
3138 (goto-char start)
3139 (beginning-of-line)
3140 (if (not (outline-goto-prefix)) ; Get initial position within a topic:
3141 (outline-next-visible-heading 1))
3142 (while (and (not done)
3143 (not (eobp)) ; Loop until we've covered the region.
3144 (not (> (point) end)))
3145 (setq depth (outline-recent-depth) ; Current topics' depth,
3146 bullet (outline-recent-bullet) ; ... bullet,
3147 beg (progn (outline-end-of-prefix t) (point))) ; and beginning.
3148 (setq done ; The boundary for the current topic:
3149 (not (outline-next-visible-heading 1)))
3150 (beginning-of-line)
3151 (setq next (point))
3152 (goto-char beg)
3153 (setq strings nil)
3154 (while (> next (point)) ; Get all the exposed text in
3155 (setq strings
3156 (cons (buffer-substring
3157 beg
3158 ;To hidden text or end of line:
3159 (progn
3160 (search-forward "\r"
3161 (save-excursion (end-of-line)
3162 (point))
3163 1)
3164 (if (= (preceding-char) ?\r)
3165 (1- (point))
3166 (point))))
3167 strings))
3168 (if (< (point) next) ; Resume from after hid text, if any.
3169 (forward-line 1))
3170 (setq beg (point)))
3171 ;; Accumulate list for this topic:
3172 (setq result
3173 (cons (append (list depth
3174 outline-header-prefix
3175 bullet)
3176 (nreverse strings))
3177 result)))
3178 ;; Put the list with first at front, to last at back:
3179 (nreverse result))))
3180 ;;;_ > outline-process-exposed (arg &optional tobuf)
3181 (defun outline-process-exposed (&optional func from to frombuf tobuf)
3182 "Map function on exposed parts of current topic; results to another buffer.
3183
3184 Apply FUNCTION \(default 'outline-insert-listified) to exposed
3185 portions FROM position TO position \(default region, or the entire
3186 buffer if no region active) in buffer FROMBUF \(default current
3187 buffer) to buffer TOBUF \(default is buffer named like frombuf but
3188 with \"*\" prepended and \" exposed*\" appended).
3189
3190 The function must as its arguments the elements of the list
3191 representations of topic entries produced by outline-listify-exposed."
3192
3193 ; Resolve arguments,
3194 ; defaulting if necessary:
3195 (if (not func) (setq func 'outline-insert-listified))
3196 (if (not (and from to))
3197 (if mark-active
3198 (setq from (region-beginning) to (region-end))
3199 (setq from (point-min) to (point-max))))
3200 (if frombuf
3201 (if (not (bufferp frombuf))
3202 ;; Specified but not a buffer - get it:
3203 (let ((got (get-buffer frombuf)))
3204 (if (not got)
3205 (error (concat "outline-process-exposed: source buffer "
3206 frombuf
3207 " not found."))
3208 (setq frombuf got))))
3209 ;; not specified - default it:
3210 (setq frombuf (current-buffer)))
3211 (if tobuf
3212 (if (not (bufferp tobuf))
3213 (setq tobuf (get-buffer-create tobuf)))
3214 ;; not specified - default it:
3215 (setq tobuf (concat "*" (buffer-name frombuf) " exposed*")))
3216
3217 (let* ((listified (progn (set-buffer frombuf)
3218 (outline-listify-exposed from to)))
3219 (prefix outline-header-prefix) ; ... as set in frombuf.
3220 curr)
3221 (set-buffer tobuf)
3222 (while listified
3223 (setq curr (car listified))
3224 (setq listified (cdr listified))
3225 (apply func (list (car curr) ; depth
3226 (car (cdr curr)) ; header-prefix
3227 (car (cdr (cdr curr))) ; bullet
3228 (cdr (cdr (cdr curr)))))) ; list of text lines
3229 (pop-to-buffer tobuf)))
3230
3231 ;;;_ - Topic-specific
3232 ;;;_ > outline-show-entry ()
3233 ; outline-show-entry basically for isearch dynamic exposure, as is...
3234 (defun outline-show-entry ()
3235 "Like `outline-show-current-entry', reveals entries nested in hidden topics.
3236
3237 This is a way to give restricted peek at a concealed locality without the
3238 expense of exposing its context, but can leave the outline with aberrant
3239 exposure. outline-hide-current-entry-completely or outline-show-offshoot
3240 should be used after the peek to rectify the exposure."
3241
3242 (interactive)
3243 (save-excursion
3244 (outline-goto-prefix)
3245 (outline-flag-region (if (bobp) (point) (1- (point)))
3246 (or (outline-pre-next-preface) (point))
3247 ?\n)))
3248 ;;;_ > outline-show-children (&optional level strict)
3249 (defun outline-show-children (&optional level strict)
3250
3251 "If point is visible, show all direct subheadings of this heading.
3252
3253 Otherwise, do outline-show-to-offshoot, and then show subheadings.
3254
3255 Optional LEVEL specifies how many levels below the current level
3256 should be shown, or all levels if t. Default is 1.
3257
3258 Optional STRICT means don't resort to -show-to-offshoot, no matter
3259 what. This is basically so -show-to-offshoot, which is called by
3260 this function, can employ the pure offspring-revealing capabilities of
3261 it.
3262
3263 Returns point at end of subtree that was opened, if any. (May get a
3264 point of non-opened subtree?)"
3265
3266 (interactive "p")
3267 (let (max-pos)
3268 (if (and (not strict)
3269 (outline-hidden-p))
3270
3271 (progn (outline-show-to-offshoot) ; Point's concealed, open to
3272 ; expose it.
3273 ;; Then recurse, but with "strict" set so we don't
3274 ;; infinite regress:
3275 (setq max-pos (outline-show-children level t)))
3276
3277 (save-excursion
3278 (save-restriction
3279 (let* ((start-pt (point))
3280 (chart (outline-chart-subtree (or level 1)))
3281 (to-reveal (outline-chart-to-reveal chart (or level 1))))
3282 (goto-char start-pt)
3283 (if (and strict (= (preceding-char) ?\r))
3284 ;; Concealed root would already have been taken care of,
3285 ;; unless strict was set.
3286 (outline-flag-region (point) (outline-snug-back) ?\n))
3287 (while to-reveal
3288 (goto-char (car to-reveal))
3289 (outline-flag-region (point) (outline-snug-back) ?\n)
3290 (setq to-reveal (cdr to-reveal)))))))))
3291 ;;;_ x outline-show-current-children (&optional level strict)
3292 (defun outline-show-current-children (&optional level strict)
3293 "This command was misnamed, use `outline-show-children' instead.
3294
3295 \(The \"current\" in the name is supposed to imply that it works on
3296 the visible topic containing point, while it really works with respect
3297 to the most immediate topic, concealed or not. I'll leave this old
3298 name around for a bit, but i'll soon activate an annoying message to
3299 warn people about the change, and then deprecate this alias."
3300
3301 (interactive "p")
3302 ;;(beep)
3303 ;;(message (format "Use '%s' instead of '%s' (%s)."
3304 ;; "outline-show-children"
3305 ;; "outline-show-current-children"
3306 ;; (buffer-name (current-buffer))))
3307 (outline-show-children level strict))
3308 ;;;_ > outline-hide-point-reconcile ()
3309 (defun outline-hide-reconcile ()
3310 "Like `outline-hide-current-entry'; hides completely if within hidden region.
3311
3312 Specifically intended for aberrant exposure states, like entries that were
3313 exposed by outline-show-entry but are within otherwise concealed regions."
3314 (interactive)
3315 (save-excursion
3316 (outline-goto-prefix)
3317 (outline-flag-region (if (not (bobp)) (1- (point)) (point))
3318 (progn (outline-pre-next-preface)
3319 (if (= ?\r (following-char))
3320 (point)
3321 (1- (point))))
3322 ?\r)))
3323 ;;;_ > outline-show-to-offshoot ()
3324 (defun outline-show-to-offshoot ()
3325 "Like outline-show-entry, but reveals opens all concealed ancestors, as well.
3326
3327 As with outline-hide-current-entry-completely, useful for rectifying
3328 aberrant exposure states produced by outline-show-entry."
3329
3330 (interactive)
3331 (save-excursion
3332 (let ((orig-pt (point))
3333 (orig-pref (outline-goto-prefix))
3334 (last-at (point))
3335 bag-it)
3336 (while (or bag-it (= (preceding-char) ?\r))
3337 (beginning-of-line)
3338 (if (= last-at (setq last-at (point)))
3339 ;; Oops, we're not making any progress! Show the current
3340 ;; topic completely, and bag this try.
3341 (progn (beginning-of-line)
3342 (outline-show-current-subtree)
3343 (goto-char orig-pt)
3344 (setq bag-it t)
3345 (beep)
3346 (message "%s: %s"
3347 "outline-show-to-offshoot: "
3348 "Aberrant nesting encountered.")))
3349 (outline-show-children)
3350 (goto-char orig-pref))
3351 (goto-char orig-pt)))
3352 (if (outline-hidden-p)
3353 (outline-show-entry)))
3354 ;;;_ > outline-hide-current-entry ()
3355 (defun outline-hide-current-entry ()
3356 "Hide the body directly following this heading."
3357 (interactive)
3358 (outline-back-to-current-heading)
3359 (save-excursion
3360 (outline-flag-region (point)
3361 (progn (outline-end-of-current-entry) (point))
3362 ?\^M)))
3363 ;;;_ > outline-show-current-entry (&optional arg)
3364 (defun outline-show-current-entry (&optional arg)
3365
3366 "Show body following current heading, or hide the entry if repeat count."
3367
3368 (interactive "P")
3369 (if arg
3370 (outline-hide-current-entry)
3371 (save-excursion
3372 (outline-flag-region (point)
3373 (progn (outline-end-of-current-entry) (point))
3374 ?\n))))
3375 ;;;_ > outline-hide-current-entry-completely ()
3376 ; ... outline-hide-current-entry-completely also for isearch dynamic exposure:
3377 (defun outline-hide-current-entry-completely ()
3378 "Like outline-hide-current-entry, but conceal topic completely.
3379
3380 Specifically intended for aberrant exposure states, like entries that were
3381 exposed by outline-show-entry but are within otherwise concealed regions."
3382 (interactive)
3383 (save-excursion
3384 (outline-goto-prefix)
3385 (outline-flag-region (if (not (bobp)) (1- (point)) (point))
3386 (progn (outline-pre-next-preface)
3387 (if (= ?\r (following-char))
3388 (point)
3389 (1- (point))))
3390 ?\r)))
3391 ;;;_ > outline-show-current-subtree (&optional arg)
3392 (defun outline-show-current-subtree (&optional arg)
3393 "Show everything within the current topic. With a repeat-count,
3394 expose this topic and its' siblings."
3395 (interactive "P")
3396 (save-excursion
3397 (if (<= (outline-current-depth) 0)
3398 ;; Outside any topics - try to get to the first:
3399 (if (not (outline-next-heading))
3400 (error "No topics.")
3401 ;; got to first, outermost topic - set to expose it and siblings:
3402 (message "Above outermost topic - exposing all.")
3403 (outline-flag-region (point-min)(point-max) ?\n))
3404 (if (not arg)
3405 (outline-flag-current-subtree ?\n)
3406 (outline-beginning-of-level)
3407 (outline-expose-topic '(* :))))))
3408 ;;;_ > outline-hide-current-subtree (&optional just-close)
3409 (defun outline-hide-current-subtree (&optional just-close)
3410 "Close the current topic, or containing topic if this one is already closed.
3411
3412 If this topic is closed and it's a top level topic, close this topic
3413 and its' siblings.
3414
3415 If optional arg JUST-CLOSE is non-nil, do not treat the parent or
3416 siblings, even if the target topic is already closed."
3417
3418 (interactive)
3419 (let ((from (point))
3420 (orig-eol (progn (end-of-line)
3421 (if (not (outline-goto-prefix))
3422 (error "No topics found.")
3423 (end-of-line)(point)))))
3424 (outline-flag-current-subtree ?\^M)
3425 (goto-char from)
3426 (if (and (= orig-eol (progn (goto-char orig-eol)
3427 (end-of-line)
3428 (point)))
3429 (not just-close)
3430 ;; Structure didn't change - try hiding current level:
3431 (goto-char from)
3432 (if (outline-up-current-level 1 t)
3433 t
3434 (goto-char 0)
3435 (let ((msg
3436 "Top-level topic already closed - closing siblings..."))
3437 (message msg)
3438 (outline-expose-topic '(0 :))
3439 (message (concat msg " Done.")))
3440 nil)
3441 (/= (outline-recent-depth) 0))
3442 (outline-hide-current-subtree))
3443 (goto-char from)))
3444 ;;;_ > outline-show-current-branches ()
3445 (defun outline-show-current-branches ()
3446 "Show all subheadings of this heading, but not their bodies."
3447 (interactive)
3448 (beginning-of-line)
3449 (outline-show-children t))
3450 ;;;_ > outline-hide-current-leaves ()
3451 (defun outline-hide-current-leaves ()
3452 "Hide the bodies of the current topic and all its' offspring."
3453 (interactive)
3454 (outline-back-to-current-heading)
3455 (outline-hide-region-body (point) (progn (outline-end-of-current-subtree)
3456 (point))))
3457
3458 ;;;_ - Region and beyond
3459 ;;;_ > outline-show-all ()
3460 (defun outline-show-all ()
3461 "Show all of the text in the buffer."
3462 (interactive)
3463 (message "Exposing entire buffer...")
3464 (outline-flag-region (point-min) (point-max) ?\n)
3465 (message "Exposing entire buffer... Done."))
3466 ;;;_ > outline-hide-bodies ()
3467 (defun outline-hide-bodies ()
3468 "Hide all of buffer except headings."
3469 (interactive)
3470 (outline-hide-region-body (point-min) (point-max)))
3471 ;;;_ > outline-hide-region-body (start end)
3472 (defun outline-hide-region-body (start end)
3473 "Hide all body lines in the region, but not headings."
3474 (save-excursion
3475 (save-restriction
3476 (narrow-to-region start end)
3477 (goto-char (point-min))
3478 (while (not (eobp))
3479 (outline-flag-region (point)
3480 (progn (outline-pre-next-preface) (point)) ?\^M)
3481 (if (not (eobp))
3482 (forward-char
3483 (if (looking-at "[\n\r][\n\r]")
3484 2 1)))))))
3485
3486 ;;;_ > outline-expose-topic (spec)
3487 (defun outline-expose-topic (spec)
3488 "Apply exposure specs to successive outline topic items.
3489
3490 Use the more convenient frontend, `outline-new-exposure', if you don't
3491 need evaluation of the arguments, or even better, the `outline-layout'
3492 variable-keyed mode-activation/auto-exposure feature of allout outline
3493 mode. See the respective documentation strings for more details.
3494
3495 Cursor is left at start position.
3496
3497 SPEC is either a number or a list.
3498
3499 Successive specs on a list are applied to successive sibling topics.
3500
3501 A simple spec \(either a number, one of a few symbols, or the null
3502 list) dictates the exposure for the corresponding topic.
3503
3504 Non-null lists recursively designate exposure specs for respective
3505 subtopics of the current topic.
3506
3507 The ':' repeat spec is used to specify exposure for any number of
3508 successive siblings, up to the trailing ones for which there are
3509 explicit specs following the ':'.
3510
3511 Simple (numeric and null-list) specs are interpreted as follows:
3512
3513 Numbers indicate the relative depth to open the corresponding topic.
3514 - negative numbers force the topic to be closed before opening to the
3515 absolute value of the number, so all siblings are open only to
3516 that level.
3517 - positive numbers open to the relative depth indicated by the
3518 number, but do not force already opened subtopics to be closed.
3519 - 0 means to close topic - hide all offspring.
3520 : - 'repeat'
3521 apply prior element to all siblings at current level, *up to*
3522 those siblings that would be covered by specs following the ':'
3523 on the list. Ie, apply to all topics at level but the last
3524 ones. \(Only first of multiple colons at same level is
3525 respected - subsequent ones are discarded.)
3526 * - completely opens the topic, including bodies.
3527 + - shows all the sub headers, but not the bodies
3528 - - exposes the body of the corresponding topic.
3529
3530 Examples:
3531 \(outline-expose-topic '(-1 : 0))
3532 Close this and all following topics at current level, exposing
3533 only their immediate children, but close down the last topic
3534 at this current level completely.
3535 \(outline-expose-topic '(-1 () : 1 0))
3536 Close current topic so only the immediate subtopics are shown;
3537 show the children in the second to last topic, and completely
3538 close the last one.
3539 \(outline-expose-topic '(-2 : -1 *))
3540 Expose children and grandchildren of all topics at current
3541 level except the last two; expose children of the second to
3542 last and completely open the last one."
3543
3544 (interactive "xExposure spec: ")
3545 (if (not (listp spec))
3546 nil
3547 (let ((depth (outline-depth))
3548 (max-pos 0)
3549 prev-elem curr-elem
3550 stay done
3551 snug-back
3552 )
3553 (while spec
3554 (setq prev-elem curr-elem
3555 curr-elem (car spec)
3556 spec (cdr spec))
3557 (cond ; Do current element:
3558 ((null curr-elem) nil)
3559 ((symbolp curr-elem)
3560 (cond ((eq curr-elem '*) (outline-show-current-subtree)
3561 (if (> outline-recent-end-of-subtree max-pos)
3562 (setq max-pos outline-recent-end-of-subtree)))
3563 ((eq curr-elem '+) (outline-show-current-branches)
3564 (if (> outline-recent-end-of-subtree max-pos)
3565 (setq max-pos outline-recent-end-of-subtree)))
3566 ((eq curr-elem '-) (outline-show-current-entry))
3567 ((eq curr-elem ':)
3568 (setq stay t)
3569 ;; Expand the 'repeat' spec to an explicit version,
3570 ;; w.r.t. remaining siblings:
3571 (let ((residue ; = # of sibs not covered by remaining spec
3572 ;; Dang - could be nice to make use of the chart, sigh:
3573 (- (length (outline-chart-siblings))
3574 (length spec))))
3575 (if (< 0 residue)
3576 ;; Some residue - cover it with prev-elem:
3577 (setq spec (append (make-list residue prev-elem)
3578 spec)))))))
3579 ((numberp curr-elem)
3580 (if (and (>= 0 curr-elem) (outline-visible-p))
3581 (save-excursion (outline-hide-current-subtree t)
3582 (if (> 0 curr-elem)
3583 nil
3584 (if (> outline-recent-end-of-subtree max-pos)
3585 (setq max-pos
3586 outline-recent-end-of-subtree)))))
3587 (if (> (abs curr-elem) 0)
3588 (progn (outline-show-children (abs curr-elem))
3589 (if (> outline-recent-end-of-subtree max-pos)
3590 (setq max-pos outline-recent-end-of-subtree)))))
3591 ((listp curr-elem)
3592 (if (outline-descend-to-depth (1+ depth))
3593 (let ((got (outline-expose-topic curr-elem)))
3594 (if (and got (> got max-pos)) (setq max-pos got))))))
3595 (cond (stay (setq stay nil))
3596 ((listp (car spec)) nil)
3597 ((> max-pos (point))
3598 ;; Capitalize on max-pos state to get us nearer next sibling:
3599 (progn (goto-char (min (point-max) max-pos))
3600 (outline-next-heading)))
3601 ((outline-next-sibling depth))))
3602 max-pos)))
3603 ;;;_ > outline-old-expose-topic (spec &rest followers)
3604 (defun outline-old-expose-topic (spec &rest followers)
3605
3606 "Deprecated. Use outline-expose-topic \(with different schema
3607 format\) instead.
3608
3609 Dictate wholesale exposure scheme for current topic, according to SPEC.
3610
3611 SPEC is either a number or a list. Optional successive args
3612 dictate exposure for subsequent siblings of current topic.
3613
3614 A simple spec (either a number, a special symbol, or the null list)
3615 dictates the overall exposure for a topic. Non null lists are
3616 composite specs whose first element dictates the overall exposure for
3617 a topic, with the subsequent elements in the list interpreted as specs
3618 that dictate the exposure for the successive offspring of the topic.
3619
3620 Simple (numeric and null-list) specs are interpreted as follows:
3621
3622 - Numbers indicate the relative depth to open the corresponding topic:
3623 - negative numbers force the topic to be close before opening to the
3624 absolute value of the number.
3625 - positive numbers just open to the relative depth indicated by the number.
3626 - 0 just closes
3627 - '*' completely opens the topic, including bodies.
3628 - '+' shows all the sub headers, but not the bodies
3629 - '-' exposes the body and immediate offspring of the corresponding topic.
3630
3631 If the spec is a list, the first element must be a number, which
3632 dictates the exposure depth of the topic as a whole. Subsequent
3633 elements of the list are nested SPECs, dictating the specific exposure
3634 for the corresponding offspring of the topic.
3635
3636 Optional FOLLOWER arguments dictate exposure for succeeding siblings."
3637
3638 (interactive "xExposure spec: ")
3639 (let ((depth (outline-current-depth))
3640 done
3641 max-pos)
3642 (cond ((null spec) nil)
3643 ((symbolp spec)
3644 (if (eq spec '*) (outline-show-current-subtree))
3645 (if (eq spec '+) (outline-show-current-branches))
3646 (if (eq spec '-) (outline-show-current-entry)))
3647 ((numberp spec)
3648 (if (>= 0 spec)
3649 (save-excursion (outline-hide-current-subtree t)
3650 (end-of-line)
3651 (if (or (not max-pos)
3652 (> (point) max-pos))
3653 (setq max-pos (point)))
3654 (if (> 0 spec)
3655 (setq spec (* -1 spec)))))
3656 (if (> spec 0)
3657 (outline-show-children spec)))
3658 ((listp spec)
3659 ;(let ((got (outline-old-expose-topic (car spec))))
3660 ; (if (and got (or (not max-pos) (> got max-pos)))
3661 ; (setq max-pos got)))
3662 (let ((new-depth (+ (outline-current-depth) 1))
3663 got)
3664 (setq max-pos (outline-old-expose-topic (car spec)))
3665 (setq spec (cdr spec))
3666 (if (and spec
3667 (outline-descend-to-depth new-depth)
3668 (not (outline-hidden-p)))
3669 (progn (setq got (apply 'outline-old-expose-topic spec))
3670 (if (and got (or (not max-pos) (> got max-pos)))
3671 (setq max-pos got)))))))
3672 (while (and followers
3673 (progn (if (and max-pos (< (point) max-pos))
3674 (progn (goto-char max-pos)
3675 (setq max-pos nil)))
3676 (end-of-line)
3677 (outline-next-sibling depth)))
3678 (outline-old-expose-topic (car followers))
3679 (setq followers (cdr followers)))
3680 max-pos))
3681 ;;;_ > outline-new-exposure '()
3682 (defmacro outline-new-exposure (&rest spec)
3683 "Literal frontend for `outline-expose-topic', doesn't evaluate arguments.
3684 Some arguments that would need to be quoted in outline-expose-topic
3685 need not be quoted in outline-new-exposure.
3686
3687 Cursor is left at start position.
3688
3689 Use this instead of obsolete 'outline-exposure'.
3690
3691 Examples:
3692 \(outline-exposure (-1 () () () 1) 0)
3693 Close current topic at current level so only the immediate
3694 subtopics are shown, except also show the children of the
3695 third subtopic; and close the next topic at the current level.
3696 \(outline-exposure : -1 0)
3697 Close all topics at current level to expose only their
3698 immediate children, except for the last topic at the current
3699 level, in which even its' immediate children are hidden.
3700 \(outline-exposure -2 : -1 *)
3701 Expose children and grandchildren of first topic at current
3702 level, and expose children of subsequent topics at current
3703 level *except* for the last, which should be opened completely."
3704 (list 'save-excursion
3705 '(if (not (or (outline-goto-prefix)
3706 (outline-next-heading)))
3707 (error "outline-new-exposure: Can't find any outline topics."))
3708 (list 'outline-expose-topic (list 'quote spec))))
3709 ;;;_ > outline-exposure '()
3710 (defmacro outline-exposure (&rest spec)
3711 "Being deprecated - use more recent 'outline-new-exposure' instead.
3712
3713 Literal frontend for `outline-old-expose-topic', doesn't evaluate arguments
3714 and retains start position."
3715 (list 'save-excursion
3716 '(if (not (or (outline-goto-prefix)
3717 (outline-next-heading)))
3718 (error "Can't find any outline topics."))
3719 (cons 'outline-old-expose-topic
3720 (mapcar '(lambda (x) (list 'quote x)) spec))))
3721
3722 ;;;_ #7 ISearch with Dynamic Exposure
3723 ;;;_ = outline-search-reconceal
3724 (defvar outline-search-reconceal nil
3725 "Track whether current search match was concealed outside of search.
3726
3727 The value is the location of the match, if it was concealed, regular
3728 if the entire topic was concealed, in a list if the entry was concealed.")
3729 ;;;_ = outline-search-quitting
3730 (defconst outline-search-quitting nil
3731 "Distinguishes isearch conclusion and cancellation.
3732
3733 Used by isearch-terminate/outline-provisions and
3734 isearch-done/outline-provisions")
3735
3736
3737 ;;;_ > outline-enwrap-isearch ()
3738 (defun outline-enwrap-isearch ()
3739 "Impose outline-mode isearch-mode wrappers for dynamic exposure in isearch.
3740
3741 Isearch progressively exposes and reconceals hidden topics when
3742 working in outline mode, but works normally elsewhere.
3743
3744 The function checks to ensure that the rebindings are done only once."
3745
3746 ; Should isearch-mode be employed,
3747 (if (or (not outline-enwrap-isearch-mode)
3748 ; or are preparations already done?
3749 (fboundp 'real-isearch-terminate))
3750
3751 ;; ... no - skip this all:
3752 nil
3753
3754 ;; ... yes:
3755
3756 ; Ensure load of isearch-mode:
3757 (if (or (and (fboundp 'isearch-mode)
3758 (fboundp 'isearch-quote-char))
3759 (condition-case error
3760 (load-library outline-enwrap-isearch-mode)
3761 (file-error (message "Skipping isearch-mode provisions - %s '%s'"
3762 (car (cdr error))
3763 (car (cdr (cdr error))))
3764 (sit-for 1)
3765 ;; Inhibit subsequent tries and return nil:
3766 (setq outline-enwrap-isearch-mode nil))))
3767 ;; Isearch-mode loaded, encapsulate specific entry points for
3768 ;; outline dynamic-exposure business:
3769 (progn
3770
3771 ;; stash crucial isearch-mode funcs under known, private
3772 ;; names, then register wrapper functions under the old
3773 ;; names, in their stead: 'isearch-quit' is pre isearch v 1.2.
3774 (fset 'real-isearch-terminate
3775 ; 'isearch-quit is pre v 1.2:
3776 (or (if (fboundp 'isearch-quit)
3777 (symbol-function 'isearch-quit))
3778 (if (fboundp 'isearch-abort)
3779 ; 'isearch-abort' is v 1.2 and on:
3780 (symbol-function 'isearch-abort))))
3781 (fset 'isearch-quit 'isearch-terminate/outline-provisions)
3782 (fset 'isearch-abort 'isearch-terminate/outline-provisions)
3783 (fset 'real-isearch-done (symbol-function 'isearch-done))
3784 (fset 'isearch-done 'isearch-done/outline-provisions)
3785 (fset 'real-isearch-update (symbol-function 'isearch-update))
3786 (fset 'isearch-update 'isearch-update/outline-provisions)
3787 (make-variable-buffer-local 'outline-search-reconceal)))))
3788 ;;;_ > outline-isearch-arrival-business ()
3789 (defun outline-isearch-arrival-business ()
3790 "Do outline business like exposing current point, if necessary.
3791
3792 Registers reconcealment requirements in outline-search-reconceal
3793 accordingly.
3794
3795 Set outline-search-reconceal to nil if current point is not
3796 concealed, to value of point if entire topic is concealed, and a
3797 list containing point if only the topic body is concealed.
3798
3799 This will be used to determine whether outline-hide-current-entry
3800 or outline-hide-current-entry-completely will be necessary to
3801 restore the prior concealment state."
3802
3803 (if (outline-mode-p)
3804 (setq outline-search-reconceal
3805 (if (outline-hidden-p)
3806 (save-excursion
3807 (if (re-search-backward outline-line-boundary-regexp nil 1)
3808 ;; Nil value means we got to b-o-b - wouldn't need
3809 ;; to advance.
3810 (forward-char 1))
3811 ; We'll return point or list
3812 ; containing point, depending
3813 ; on concealment state of
3814 ; topic prefix.
3815 (prog1 (if (outline-hidden-p) (point) (list (point)))
3816 ; And reveal the current
3817 ; search target:
3818 (outline-show-entry)))))))
3819 ;;;_ > outline-isearch-advancing-business ()
3820 (defun outline-isearch-advancing-business ()
3821 "Do outline business like deexposing current point, if necessary.
3822
3823 Works according to reconceal state registration."
3824 (if (and (outline-mode-p) outline-search-reconceal)
3825 (save-excursion
3826 (if (listp outline-search-reconceal)
3827 ;; Leave the topic visible:
3828 (progn (goto-char (car outline-search-reconceal))
3829 (outline-hide-current-entry))
3830 ;; Rehide the entire topic:
3831 (goto-char outline-search-reconceal)
3832 (outline-hide-current-entry-completely)))))
3833 ;;;_ > isearch-terminate/outline-provisions ()
3834 (defun isearch-terminate/outline-provisions ()
3835 (interactive)
3836 (if (and (outline-mode-p) outline-enwrap-isearch-mode)
3837 (outline-isearch-advancing-business))
3838 (let ((outline-search-quitting t)
3839 (outline-search-reconceal nil))
3840 (real-isearch-terminate)))
3841 ;;;_ > isearch-done/outline-provisions ()
3842 (defun isearch-done/outline-provisions (&optional nopush)
3843 (interactive)
3844 (if (and (outline-mode-p) outline-enwrap-isearch-mode)
3845 (progn (if (and outline-search-reconceal
3846 (not (listp outline-search-reconceal)))
3847 ;; The topic was concealed - reveal it, its siblings,
3848 ;; and any ancestors that are still concealed:
3849 (save-excursion
3850 (message "(exposing destination)")(sit-for 0)
3851 (outline-goto-prefix)
3852 ; There may be a closed blank
3853 ; line between prior and
3854 ; current topic that would be
3855 ; missed - provide for it:
3856 (if (not (bobp))
3857 (progn (forward-char -1) ; newline
3858 (if (eq ?\r (preceding-char))
3859 (outline-flag-region (1- (point))
3860 (point)
3861 ?\n))
3862 (forward-char 1)))
3863 ; Goto parent
3864 (outline-ascend-to-depth (1- (outline-recent-depth)))
3865 (outline-show-children)))
3866 (if (and (boundp 'outline-search-quitting)
3867 outline-search-quitting)
3868 nil
3869 ; We're concluding abort:
3870 (outline-isearch-arrival-business)
3871 (outline-show-children))))
3872 (if nopush
3873 ;; isearch-done in newer version of isearch mode takes arg:
3874 (real-isearch-done nopush)
3875 (real-isearch-done)))
3876 ;;;_ > isearch-update/outline-provisions ()
3877 (defun isearch-update/outline-provisions ()
3878 "Wrapper dynamically adjusts isearch target exposure.
3879
3880 Appropriately exposes and reconceals hidden outline portions, as
3881 necessary, in the course of searching."
3882 (if (not (and (outline-mode-p) outline-enwrap-isearch-mode))
3883 ;; Just do the plain business:
3884 (real-isearch-update)
3885
3886 ;; Ah - provide for outline conditions:
3887 (outline-isearch-advancing-business)
3888 (real-isearch-update)
3889 (cond (isearch-success (outline-isearch-arrival-business))
3890 ((not isearch-success) (outline-isearch-advancing-business)))))
3891
3892 ;;;_ #8 Copying and printing
3893
3894 ;;;_ - Copy exposed
3895 ;;;_ > outline-insert-listified (depth prefix bullet text)
3896 (defun outline-insert-listified (depth prefix bullet text)
3897 "Insert contents of listified outline portion in current buffer."
3898 (insert-string (concat (if (> depth 1) prefix "")
3899 (make-string (1- depth) ?\ )
3900 bullet))
3901 (while text
3902 (insert-string (car text))
3903 (if (setq text (cdr text))
3904 (insert-string "\n")))
3905 (insert-string "\n"))
3906 ;;;_ > outline-copy-exposed (arg &optional tobuf)
3907 (defun outline-copy-exposed (arg &optional tobuf)
3908 "Duplicate exposed portions of current topic to another buffer.
3909
3910 Other buffer has current buffers' name with \" exposed\" appended to it.
3911
3912 With repeat count, copy the exposed portions of entire buffer."
3913
3914 (interactive "P")
3915 (if (not tobuf)
3916 (setq tobuf (get-buffer-create (concat "*" (buffer-name) " exposed*"))))
3917 (let* ((start-pt (point))
3918 (beg (if arg (point-min) (outline-back-to-current-heading)))
3919 (end (if arg (point-max) (outline-end-of-current-subtree)))
3920 (buf (current-buffer)))
3921 (save-excursion (set-buffer tobuf)(erase-buffer))
3922 (outline-process-exposed 'outline-insert-listified
3923 beg
3924 end
3925 (current-buffer)
3926 tobuf)
3927 (goto-char (point-min))
3928 (pop-to-buffer buf)
3929 (goto-char start-pt)))
3930
3931 ;;;_ - LaTeX formatting
3932 ;;;_ > outline-latex-verb-quote (str &optional flow)
3933 (defun outline-latex-verb-quote (str &optional flow)
3934 "Return copy of STRING for literal reproduction across latex processing.
3935 Expresses the original characters \(including carriage returns) of the
3936 string across latex processing."
3937 (mapconcat '(lambda (char)
3938 ;;;mess: (cond ((memq char '(?"" ?$ ?% ?# ?& ?- ?" ?` ?^ ?- ?*));;;"))))
3939 (cond ((memq char '(?\\ ?$ ?% ?# ?& ?{ ?} ?_ ?^ ?- ?*))
3940 (concat "\\char" (number-to-string char) "{}"))
3941 ((= char ?\n) "\\\\")
3942 (t (char-to-string char))))
3943 str
3944 ""))
3945 ;;;_ > outline-latex-verbatim-quote-curr-line ()
3946 (defun outline-latex-verbatim-quote-curr-line ()
3947 "Express line for exact \(literal\) representation across latex processing.
3948
3949 Adjust line contents so it is unaltered \(from the original line)
3950 across latex processing, within the context of a 'verbatim'
3951 environment. Leaves point at the end of the line."
3952 (beginning-of-line)
3953 (let ((beg (point))
3954 (end (progn (end-of-line)(point))))
3955 (goto-char beg)
3956 (while (re-search-forward "\\\\"
3957 ;;"\\\\\\|\\{\\|\\}\\|\\_\\|\\$\\|\\\"\\|\\&\\|\\^\\|\\-\\|\\*\\|#"
3958 end ; bounded by end-of-line
3959 1) ; no matches, move to end & return nil
3960 (goto-char (match-beginning 0))
3961 (insert-string "\\")
3962 (setq end (1+ end))
3963 (goto-char (1+ (match-end 0))))))
3964 ;;;_ > outline-insert-latex-header (buf)
3965 (defun outline-insert-latex-header (buf)
3966 "Insert initial latex commands at point in BUFFER."
3967 ;; Much of this is being derived from the stuff in appendix of E in
3968 ;; the TeXBook, pg 421.
3969 (set-buffer buf)
3970 (let ((doc-style (format "\n\\documentstyle{%s}\n"
3971 "report"))
3972 (page-numbering (if outline-number-pages
3973 "\\pagestyle{empty}\n"
3974 ""))
3975 (linesdef (concat "\\def\\beginlines{"
3976 "\\par\\begingroup\\nobreak\\medskip"
3977 "\\parindent=0pt\n"
3978 " \\kern1pt\\nobreak \\obeylines \\obeyspaces "
3979 "\\everypar{\\strut}}\n"
3980 "\\def\\endlines{"
3981 "\\kern1pt\\endgroup\\medbreak\\noindent}\n"))
3982 (titlecmd (format "\\newcommand{\\titlecmd}[1]{{%s #1}}\n"
3983 outline-title-style))
3984 (labelcmd (format "\\newcommand{\\labelcmd}[1]{{%s #1}}\n"
3985 outline-label-style))
3986 (headlinecmd (format "\\newcommand{\\headlinecmd}[1]{{%s #1}}\n"
3987 outline-head-line-style))
3988 (bodylinecmd (format "\\newcommand{\\bodylinecmd}[1]{{%s #1}}\n"
3989 outline-body-line-style))
3990 (setlength (format "%s%s%s%s"
3991 "\\newlength{\\stepsize}\n"
3992 "\\setlength{\\stepsize}{"
3993 outline-indent
3994 "}\n"))
3995 (oneheadline (format "%s%s%s%s%s%s%s"
3996 "\\newcommand{\\OneHeadLine}[3]{%\n"
3997 "\\noindent%\n"
3998 "\\hspace*{#2\\stepsize}%\n"
3999 "\\labelcmd{#1}\\hspace*{.2cm}"
4000 "\\headlinecmd{#3}\\\\["
4001 outline-line-skip
4002 "]\n}\n"))
4003 (onebodyline (format "%s%s%s%s%s%s"
4004 "\\newcommand{\\OneBodyLine}[2]{%\n"
4005 "\\noindent%\n"
4006 "\\hspace*{#1\\stepsize}%\n"
4007 "\\bodylinecmd{#2}\\\\["
4008 outline-line-skip
4009 "]\n}\n"))
4010 (begindoc "\\begin{document}\n\\begin{center}\n")
4011 (title (format "%s%s%s%s"
4012 "\\titlecmd{"
4013 (outline-latex-verb-quote (if outline-title
4014 (condition-case err
4015 (eval outline-title)
4016 (error "<unnamed buffer>"))
4017 "Unnamed Outline"))
4018 "}\n"
4019 "\\end{center}\n\n"))
4020 (hsize "\\hsize = 7.5 true in\n")
4021 (hoffset "\\hoffset = -1.5 true in\n")
4022 (vspace "\\vspace{.1cm}\n\n"))
4023 (insert (concat doc-style
4024 page-numbering
4025 titlecmd
4026 labelcmd
4027 headlinecmd
4028 bodylinecmd
4029 setlength
4030 oneheadline
4031 onebodyline
4032 begindoc
4033 title
4034 hsize
4035 hoffset
4036 vspace)
4037 )))
4038 ;;;_ > outline-insert-latex-trailer (buf)
4039 (defun outline-insert-latex-trailer (buf)
4040 "Insert concluding latex commands at point in BUFFER."
4041 (set-buffer buf)
4042 (insert "\n\\end{document}\n"))
4043 ;;;_ > outline-latexify-one-item (depth prefix bullet text)
4044 (defun outline-latexify-one-item (depth prefix bullet text)
4045 "Insert LaTeX commands for formatting one outline item.
4046
4047 Args are the topics' numeric DEPTH, the header PREFIX lead string, the
4048 BULLET string, and a list of TEXT strings for the body."
4049 (let* ((head-line (if text (car text)))
4050 (body-lines (cdr text))
4051 (curr-line)
4052 body-content bop)
4053 ; Do the head line:
4054 (insert-string (concat "\\OneHeadLine{\\verb\1 "
4055 (outline-latex-verb-quote bullet)
4056 "\1}{"
4057 depth
4058 "}{\\verb\1 "
4059 (if head-line
4060 (outline-latex-verb-quote head-line)
4061 "")
4062 "\1}\n"))
4063 (if (not body-lines)
4064 nil
4065 ;;(insert-string "\\beginlines\n")
4066 (insert-string "\\begin{verbatim}\n")
4067 (while body-lines
4068 (setq curr-line (car body-lines))
4069 (if (and (not body-content)
4070 (not (string-match "^\\s-*$" curr-line)))
4071 (setq body-content t))
4072 ; Mangle any occurrences of
4073 ; "\end{verbatim}" in text,
4074 ; it's special:
4075 (if (and body-content
4076 (setq bop (string-match "\\end{verbatim}" curr-line)))
4077 (setq curr-line (concat (substring curr-line 0 bop)
4078 ">"
4079 (substring curr-line bop))))
4080 ;;(insert-string "|" (car body-lines) "|")
4081 (insert-string curr-line)
4082 (outline-latex-verbatim-quote-curr-line)
4083 (insert-string "\n")
4084 (setq body-lines (cdr body-lines)))
4085 (if body-content
4086 (setq body-content nil)
4087 (forward-char -1)
4088 (insert-string "\\ ")
4089 (forward-char 1))
4090 ;;(insert-string "\\endlines\n")
4091 (insert-string "\\end{verbatim}\n")
4092 )))
4093 ;;;_ > outline-latexify-exposed (arg &optional tobuf)
4094 (defun outline-latexify-exposed (arg &optional tobuf)
4095 "Format current topic's exposed portions to TOBUF for latex processing.
4096 TOBUF defaults to a buffer named the same as the current buffer, but
4097 with \"*\" prepended and \" latex-formed*\" appended.
4098
4099 With repeat count, copy the exposed portions of entire buffer."
4100
4101 (interactive "P")
4102 (if (not tobuf)
4103 (setq tobuf
4104 (get-buffer-create (concat "*" (buffer-name) " latexified*"))))
4105 (let* ((start-pt (point))
4106 (beg (if arg (point-min) (outline-back-to-current-heading)))
4107 (end (if arg (point-max) (outline-end-of-current-subtree)))
4108 (buf (current-buffer)))
4109 (set-buffer tobuf)
4110 (erase-buffer)
4111 (outline-insert-latex-header tobuf)
4112 (goto-char (point-max))
4113 (outline-process-exposed 'outline-latexify-one-item
4114 beg
4115 end
4116 buf
4117 tobuf)
4118 (goto-char (point-max))
4119 (outline-insert-latex-trailer tobuf)
4120 (goto-char (point-min))
4121 (pop-to-buffer buf)
4122 (goto-char start-pt)))
4123
4124 ;;;_ #9 miscellaneous
4125 ;;;_ > outline-mark-topic ()
4126 (defun outline-mark-topic ()
4127 "Put the region around topic currently containing point."
4128 (interactive)
4129 (beginning-of-line)
4130 (outline-goto-prefix)
4131 (push-mark (point))
4132 (outline-end-of-current-subtree)
4133 (exchange-point-and-mark))
4134 ;;;_ > outlineify-sticky ()
4135 ;; outlinify-sticky is correct spelling; provide this alias for sticklers:
4136 (defalias 'outlinify-sticky 'outlineify-sticky)
4137 (defun outlineify-sticky (&optional arg)
4138 "Activate outline mode and establish file var so it is started subseqently.
4139
4140 See doc-string for `outline-layout' and `outline-init' for details on
4141 setup for auto-startup."
4142
4143 (interactive "P")
4144
4145 (outline-mode t)
4146
4147 (save-excursion
4148 (goto-char (point-min))
4149 (if (looking-at outline-regexp)
4150 t
4151 (outline-open-topic 2)
4152 (insert-string (concat "Dummy outline topic header - see"
4153 "`outline-mode' docstring for info."))
4154 (next-line 1)
4155 (goto-char (point-max))
4156 (next-line 1)
4157 (outline-open-topic 0)
4158 (insert-string "Local emacs vars.\n")
4159 (outline-open-topic 1)
4160 (insert-string "(`outline-layout' is for allout.el outline-mode)\n")
4161 (outline-open-topic 0)
4162 (insert-string "Local variables:\n")
4163 (outline-open-topic 0)
4164 (insert-string (format "outline-layout: %s\n"
4165 (or outline-layout
4166 '(1 : 0))))
4167 (outline-open-topic 0)
4168 (insert-string "End:\n"))))
4169 ;;;_ > solicit-char-in-string (prompt string &optional do-defaulting)
4170 (defun solicit-char-in-string (prompt string &optional do-defaulting)
4171 "Solicit (with first arg PROMPT) choice of a character from string STRING.
4172
4173 Optional arg DO-DEFAULTING indicates to accept empty input (CR)."
4174
4175 (let ((new-prompt prompt)
4176 got)
4177
4178 (while (not got)
4179 (message "%s" new-prompt)
4180
4181 ;; We do our own reading here, so we can circumvent, eg, special
4182 ;; treatment for '?' character. (Might oughta change minibuffer
4183 ;; keymap instead, oh well.)
4184 (setq got
4185 (char-to-string (let ((cursor-in-echo-area nil)) (read-char))))
4186
4187 (if (null (string-match (regexp-quote got) string))
4188 (if (and do-defaulting (string= got "\^M"))
4189 ;; We're defaulting, return null string to indicate that:
4190 (setq got "")
4191 ;; Failed match and not defaulting,
4192 ;; set the prompt to give feedback,
4193 (setq new-prompt (concat prompt
4194 got
4195 " ...pick from: "
4196 string
4197 ""))
4198 ;; and set loop to try again:
4199 (setq got nil))
4200 ;; Got a match - give feedback:
4201 (message "")))
4202 ;; got something out of loop - return it:
4203 got)
4204 )
4205 ;;;_ > regexp-sans-escapes (string)
4206 (defun regexp-sans-escapes (regexp &optional successive-backslashes)
4207 "Return a copy of REGEXP with all character escapes stripped out.
4208
4209 Representations of actual backslashes - '\\\\\\\\' - are left as a
4210 single backslash.
4211
4212 Optional arg SUCCESSIVE-BACKSLASHES is used internally for recursion."
4213
4214 (if (string= regexp "")
4215 ""
4216 ;; Set successive-backslashes to number if current char is
4217 ;; backslash, or else to nil:
4218 (setq successive-backslashes
4219 (if (= (aref regexp 0) ?\\)
4220 (if successive-backslashes (1+ successive-backslashes) 1)
4221 nil))
4222 (if (or (not successive-backslashes) (= 2 successive-backslashes))
4223 ;; Include first char:
4224 (concat (substring regexp 0 1)
4225 (regexp-sans-escapes (substring regexp 1)))
4226 ;; Exclude first char, but maintain count:
4227 (regexp-sans-escapes (substring regexp 1) successive-backslashes))))
4228 ;;;_ - add-hook definition for divergent emacsen
4229 ;;;_ > add-hook (hook function &optional append)
4230 (if (not (fboundp 'add-hook))
4231 (defun add-hook (hook function &optional append)
4232 "Add to the value of HOOK the function FUNCTION unless already present.
4233 \(It becomes the first hook on the list unless optional APPEND is non-nil, in
4234 which case it becomes the last). HOOK should be a symbol, and FUNCTION may be
4235 any valid function. HOOK's value should be a list of functions, not a single
4236 function. If HOOK is void, it is first set to nil."
4237 (or (boundp hook) (set hook nil))
4238 (or (if (consp function)
4239 ;; Clever way to tell whether a given lambda-expression
4240 ;; is equal to anything in the hook.
4241 (let ((tail (assoc (cdr function) (symbol-value hook))))
4242 (equal function tail))
4243 (memq function (symbol-value hook)))
4244 (set hook
4245 (if append
4246 (nconc (symbol-value hook) (list function))
4247 (cons function (symbol-value hook)))))))
4248
4249 ;;;_ #10 Under development
4250 ;;;_ > outline-bullet-isearch (&optional bullet)
4251 (defun outline-bullet-isearch (&optional bullet)
4252 "Isearch \(regexp\) for topic with bullet BULLET."
4253 (interactive)
4254 (if (not bullet)
4255 (setq bullet (solicit-char-in-string
4256 "ISearch for topic with bullet: "
4257 (regexp-sans-escapes outline-bullets-string))))
4258
4259 (let ((isearch-regexp t)
4260 (isearch-string (concat "^"
4261 outline-header-prefix
4262 "[ \t]*"
4263 bullet)))
4264 (isearch-repeat 'forward)
4265 (isearch-mode t)))
4266 ;;;_ ? Re hooking up with isearch - use isearch-op-fun rather than
4267 ;;; wrapping the isearch functions.
4268
4269 ;;;_* Local emacs vars.
4270 ;;; The following `outline-layout' local variable setting:
4271 ;;; - closes all topics from the first topic to just before the third-to-last,
4272 ;;; - shows the children of the third to last (config vars)
4273 ;;; - and the second to last (code section),
4274 ;;; - and closes the last topic (this local-variables section).
4275 ;;;Local variables:
4276 ;;;outline-layout: (0 : -1 -1 0)
4277 ;;;End:
4278
4279 ;; allout.el ends here
4280