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