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