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