]> code.delx.au - gnu-emacs/blob - lisp/allout.el
(defcustom): Doc fix.
[gnu-emacs] / lisp / allout.el
1 ;;; allout.el --- extensive outline mode for use alone and with other modes
2
3 ;; Copyright (C) 1992, 1993, 1994, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006 Free Software Foundation, Inc.
5
6 ;; Author: Ken Manheimer <ken dot manheimer at gmail dot com>
7 ;; Maintainer: Ken Manheimer <ken dot manheimer at gmail dot com>
8 ;; Created: Dec 1991 - first release to usenet
9 ;; Version: 2.2.1
10 ;; Keywords: outlines wp languages
11 ;; Website: http://myriadicity.net/Sundry/EmacsAllout
12
13 ;; This file is part of GNU Emacs.
14
15 ;; GNU Emacs is free software; you can redistribute it and/or modify
16 ;; it under the terms of the GNU General Public License as published by
17 ;; the Free Software Foundation; either version 2, or (at your option)
18 ;; any later version.
19
20 ;; GNU Emacs is distributed in the hope that it will be useful,
21 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
22 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 ;; GNU General Public License for more details.
24
25 ;; You should have received a copy of the GNU General Public License
26 ;; along with GNU Emacs; see the file COPYING. If not, write to the
27 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
28 ;; Boston, MA 02110-1301, USA.
29
30 ;;; Commentary:
31
32 ;; Allout outline minor mode provides extensive outline formatting and
33 ;; and manipulation beyond standard emacs outline mode. Some features:
34 ;;
35 ;; - Classic outline-mode topic-oriented navigation and exposure adjustment
36 ;; - Topic-oriented editing including coherent topic and subtopic
37 ;; creation, promotion, demotion, cut/paste across depths, etc.
38 ;; - Incremental search with dynamic exposure and reconcealment of text
39 ;; - Customizable bullet format - enables programming-language specific
40 ;; outlining, for code-folding editing. (Allout code itself is to try it;
41 ;; formatted as an outline - do ESC-x eval-buffer in allout.el; but
42 ;; emacs local file variables need to be enabled when the
43 ;; file was visited - see `enable-local-variables'.)
44 ;; - Configurable per-file initial exposure settings
45 ;; - Symmetric-key and key-pair topic encryption, plus symmetric passphrase
46 ;; mnemonic support, with verification against an established passphrase
47 ;; (using a stashed encrypted dummy string) and user-supplied hint
48 ;; maintenance. (See allout-toggle-current-subtree-encryption docstring.)
49 ;; - Automatic topic-number maintenance
50 ;; - "Hot-spot" operation, for single-keystroke maneuvering and
51 ;; exposure control (see the allout-mode docstring)
52 ;; - Easy rendering of exposed portions into numbered, latex, indented, etc
53 ;; outline styles
54 ;; - Careful attention to whitespace - enabling blank lines between items
55 ;; and maintenance of hanging indentation (in paragraph auto-fill and
56 ;; across topic promotion and demotion) of topic bodies consistent with
57 ;; indentation of their topic header.
58 ;;
59 ;; and more.
60 ;;
61 ;; See the `allout-mode' function's docstring for an introduction to the
62 ;; mode.
63 ;;
64 ;; The latest development version and helpful notes are available at
65 ;; http://myriadicity.net/Sundry/EmacsAllout .
66 ;;
67 ;; The outline menubar additions provide quick reference to many of
68 ;; the features, and see the docstring of the variable `allout-init'
69 ;; for instructions on priming your emacs session for automatic
70 ;; activation of allout-mode.
71 ;;
72 ;; See the docstring of the variables `allout-layout' and
73 ;; `allout-auto-activation' for details on automatic activation of
74 ;; `allout-mode' as a minor mode. (It has changed since allout
75 ;; 3.x, for those of you that depend on the old method.)
76 ;;
77 ;; Note - the lines beginning with `;;;_' are outline topic headers.
78 ;; Just `ESC-x eval-buffer' to give it a whirl.
79
80 ;; ken manheimer (ken dot manheimer at gmail dot com)
81
82 ;;; Code:
83
84 ;;;_* Dependency autoloads
85 (require 'overlay)
86 (eval-when-compile
87 ;; Most of the requires here are for stuff covered by autoloads.
88 ;; Since just byte-compiling doesn't trigger autoloads, so that
89 ;; "function not found" warnings would occur without these requires.
90 (progn
91 (require 'pgg)
92 (require 'pgg-gpg)
93 (require 'overlay)
94 ;; `cl' is required for `assert'. `assert' is not covered by a standard
95 ;; autoload, but it is a macro, so that eval-when-compile is sufficient
96 ;; to byte-compile it in, or to do the require when the buffer evalled.
97 (require 'cl)
98 ))
99
100 ;;;_* USER CUSTOMIZATION VARIABLES:
101
102 ;;;_ > defgroup allout
103 (defgroup allout nil
104 "Extensive outline mode for use alone and with other modes."
105 :prefix "allout-"
106 :group 'outlines)
107
108 ;;;_ + Layout, Mode, and Topic Header Configuration
109
110 ;;;_ = allout-auto-activation
111 (defcustom allout-auto-activation nil
112 "*Regulates auto-activation modality of allout outlines - see `allout-init'.
113
114 Setq-default by `allout-init' to regulate whether or not allout
115 outline mode is automatically activated when the buffer-specific
116 variable `allout-layout' is non-nil, and whether or not the layout
117 dictated by `allout-layout' should be imposed on mode activation.
118
119 With value t, auto-mode-activation and auto-layout are enabled.
120 \(This also depends on `allout-find-file-hook' being installed in
121 `find-file-hook', which is also done by `allout-init'.)
122
123 With value `ask', auto-mode-activation is enabled, and endorsement for
124 performing auto-layout is asked of the user each time.
125
126 With value `activate', only auto-mode-activation is enabled,
127 auto-layout is not.
128
129 With value nil, neither auto-mode-activation nor auto-layout are
130 enabled.
131
132 See the docstring for `allout-init' for the proper interface to
133 this variable."
134 :type '(choice (const :tag "On" t)
135 (const :tag "Ask about layout" "ask")
136 (const :tag "Mode only" "activate")
137 (const :tag "Off" nil))
138 :group 'allout)
139 ;;;_ = allout-default-layout
140 (defcustom allout-default-layout '(-2 : 0)
141 "*Default allout outline layout specification.
142
143 This setting specifies the outline exposure to use when
144 `allout-layout' has the local value `t'. This docstring describes the
145 layout specifications.
146
147 A list value specifies a default layout for the current buffer,
148 to be applied upon activation of `allout-mode'. Any non-nil
149 value will automatically trigger `allout-mode', provided
150 `allout-init' has been called to enable this behavior.
151
152 The types of elements in the layout specification are:
153
154 integer - dictate the relative depth to open the corresponding topic(s),
155 where:
156 - negative numbers force the topic to be closed before opening
157 to the absolute value of the number, so all siblings are open
158 only to that level.
159 - positive numbers open to the relative depth indicated by the
160 number, but do not force already opened subtopics to be closed.
161 - 0 means to close topic - hide all subitems.
162 : - repeat spec - apply the preceeding element to all siblings at
163 current level, *up to* those siblings that would be covered by specs
164 following the `:' on the list. Ie, apply to all topics at level but
165 trailing ones accounted for by trailing specs. \(Only the first of
166 multiple colons at the same level is honored - later ones are ignored.)
167 * - completely exposes the topic, including bodies
168 + - exposes all subtopics, but not the bodies
169 - - exposes the body of the corresponding topic, but not subtopics
170 list - a nested layout spec, to be applied intricately to its
171 corresponding item(s)
172
173 Examples:
174 '(-2 : 0)
175 Collapse the top-level topics to show their children and
176 grandchildren, but completely collapse the final top-level topic.
177 '(-1 () : 1 0)
178 Close the first topic so only the immediate subtopics are shown,
179 leave the subsequent topics exposed as they are until the second
180 second to last topic, which is exposed at least one level, and
181 completely close the last topic.
182 '(-2 : -1 *)
183 Expose children and grandchildren of all topics at current
184 level except the last two; expose children of the second to
185 last and completely expose the last one, including its subtopics.
186
187 See `allout-expose-topic' for more about the exposure process.
188
189 Also, allout's mode-specific provisions will make topic prefixes default
190 to the comment-start string, if any, of the language of the file. This
191 is modulo the setting of `allout-use-mode-specific-leader', which see."
192 :type 'allout-layout-type
193 :group 'allout)
194 ;;;_ : allout-layout-type
195 (define-widget 'allout-layout-type 'lazy
196 "Allout layout format customization basic building blocks."
197 :type '(repeat
198 (choice (integer :tag "integer (<= zero is strict)")
199 (const :tag ": (repeat prior)" :)
200 (const :tag "* (completely expose)" *)
201 (const :tag "+ (expose all offspring, headlines only)" +)
202 (const :tag "- (expose topic body but not offspring)" -)
203 (allout-layout-type :tag "<Nested layout>"))))
204
205 ;;;_ = allout-show-bodies
206 (defcustom allout-show-bodies nil
207 "*If non-nil, show entire body when exposing a topic, rather than
208 just the header."
209 :type 'boolean
210 :group 'allout)
211 (make-variable-buffer-local 'allout-show-bodies)
212 ;;;###autoload
213 (put 'allout-show-bodies 'safe-local-variable
214 (if (fboundp 'booleanp) 'booleanp '(lambda (x) (member x '(t nil)))))
215
216 ;;;_ = allout-beginning-of-line-cycles
217 (defcustom allout-beginning-of-line-cycles t
218 "*If non-nil, \\[allout-beginning-of-line] will cycle through smart-placement options.
219
220 Cycling only happens on when the command is repeated, not when it
221 follows a different command.
222
223 Smart-placement means that repeated calls to this function will
224 advance as follows:
225
226 - if the cursor is on a non-headline body line and not on the first column:
227 then it goes to the first column
228 - if the cursor is on the first column of a non-headline body line:
229 then it goes to the start of the headline within the item body
230 - if the cursor is on the headline and not the start of the headline:
231 then it goes to the start of the headline
232 - if the cursor is on the start of the headline:
233 then it goes to the bullet character \(for hotspot navigation\)
234 - if the cursor is on the bullet character:
235 then it goes to the first column of that line \(the headline\)
236 - if the cursor is on the first column of the headline:
237 then it goes to the start of the headline within the item body.
238
239 In this fashion, you can use the beginning-of-line command to do
240 its normal job and then, when repeated, advance through the
241 entry, cycling back to start.
242
243 If this configuration variable is nil, then the cursor is just
244 advanced to the beginning of the line and remains there on
245 repeated calls."
246 :type 'boolean :group 'allout)
247 ;;;_ = allout-end-of-line-cycles
248 (defcustom allout-end-of-line-cycles t
249 "*If non-nil, \\[allout-end-of-line] will cycle through smart-placement options.
250
251 Cycling only happens on when the command is repeated, not when it
252 follows a different command.
253
254 Smart-placement means that repeated calls to this function will
255 advance as follows:
256
257 - if the cursor is not on the end-of-line,
258 then it goes to the end-of-line
259 - if the cursor is on the end-of-line but not the end-of-entry,
260 then it goes to the end-of-entry, exposing it if necessary
261 - if the cursor is on the end-of-entry,
262 then it goes to the end of the head line
263
264 In this fashion, you can use the end-of-line command to do its
265 normal job and then, when repeated, advance through the entry,
266 cycling back to start.
267
268 If this configuration variable is nil, then the cursor is just
269 advanced to the end of the line and remains there on repeated
270 calls."
271 :type 'boolean :group 'allout)
272
273 ;;;_ = allout-header-prefix
274 (defcustom allout-header-prefix "."
275 ;; this string is treated as literal match. it will be `regexp-quote'd, so
276 ;; one cannot use regular expressions to match varying header prefixes.
277 "*Leading string which helps distinguish topic headers.
278
279 Outline topic header lines are identified by a leading topic
280 header prefix, which mostly have the value of this var at their front.
281 Level 1 topics are exceptions. They consist of only a single
282 character, which is typically set to the `allout-primary-bullet'."
283 :type 'string
284 :group 'allout)
285 (make-variable-buffer-local 'allout-header-prefix)
286 ;;;###autoload
287 (put 'allout-header-prefix 'safe-local-variable 'stringp)
288 ;;;_ = allout-primary-bullet
289 (defcustom allout-primary-bullet "*"
290 "Bullet used for top-level outline topics.
291
292 Outline topic header lines are identified by a leading topic header
293 prefix, which is concluded by bullets that includes the value of this
294 var and the respective allout-*-bullets-string vars.
295
296 The value of an asterisk (`*') provides for backwards compatibility
297 with the original Emacs outline mode. See `allout-plain-bullets-string'
298 and `allout-distinctive-bullets-string' for the range of available
299 bullets."
300 :type 'string
301 :group 'allout)
302 (make-variable-buffer-local 'allout-primary-bullet)
303 ;;;###autoload
304 (put 'allout-primary-bullet 'safe-local-variable 'stringp)
305 ;;;_ = allout-plain-bullets-string
306 (defcustom allout-plain-bullets-string ".,"
307 "*The bullets normally used in outline topic prefixes.
308
309 See `allout-distinctive-bullets-string' for the other kind of
310 bullets.
311
312 DO NOT include the close-square-bracket, `]', as a bullet.
313
314 Outline mode has to be reactivated in order for changes to the value
315 of this var to take effect."
316 :type 'string
317 :group 'allout)
318 (make-variable-buffer-local 'allout-plain-bullets-string)
319 ;;;###autoload
320 (put 'allout-plain-bullets-string 'safe-local-variable 'stringp)
321 ;;;_ = allout-distinctive-bullets-string
322 (defcustom allout-distinctive-bullets-string "*+-=>()[{}&!?#%\"X@$~_\\:;^"
323 "*Persistent outline header bullets used to distinguish special topics.
324
325 These bullets are used to distinguish topics from the run-of-the-mill
326 ones. They are not used in the standard topic headers created by
327 the topic-opening, shifting, and rebulleting \(eg, on topic shift,
328 topic paste, blanket rebulleting) routines, but are offered among the
329 choices for rebulleting. They are not altered by the above automatic
330 rebulleting, so they can be used to characterize topics, eg:
331
332 `?' question topics
333 `\(' parenthetic comment \(with a matching close paren inside)
334 `[' meta-note \(with a matching close ] inside)
335 `\"' a quotation
336 `=' value settings
337 `~' \"more or less\"
338 `^' see above
339
340 ... for example. (`#' typically has a special meaning to the software,
341 according to the value of `allout-numbered-bullet'.)
342
343 See `allout-plain-bullets-string' for the selection of
344 alternating bullets.
345
346 You must run `set-allout-regexp' in order for outline mode to
347 reconcile to changes of this value.
348
349 DO NOT include the close-square-bracket, `]', on either of the bullet
350 strings."
351 :type 'string
352 :group 'allout)
353 (make-variable-buffer-local 'allout-distinctive-bullets-string)
354 ;;;###autoload
355 (put 'allout-distinctive-bullets-string 'safe-local-variable 'stringp)
356
357 ;;;_ = allout-use-mode-specific-leader
358 (defcustom allout-use-mode-specific-leader t
359 "*When non-nil, use mode-specific topic-header prefixes.
360
361 Allout outline mode will use the mode-specific `allout-mode-leaders' or
362 comment-start string, if any, to lead the topic prefix string, so topic
363 headers look like comments in the programming language. It will also use
364 the comment-start string, with an '_' appended, for `allout-primary-bullet'.
365
366 String values are used as literals, not regular expressions, so
367 do not escape any regulare-expression characters.
368
369 Value t means to first check for assoc value in `allout-mode-leaders'
370 alist, then use comment-start string, if any, then use default \(`.').
371 \(See note about use of comment-start strings, below.)
372
373 Set to the symbol for either of `allout-mode-leaders' or
374 `comment-start' to use only one of them, respectively.
375
376 Value nil means to always use the default \(`.') and leave
377 `allout-primary-bullet' unaltered.
378
379 comment-start strings that do not end in spaces are tripled in
380 the header-prefix, and an `_' underscore is tacked on the end, to
381 distinguish them from regular comment strings. comment-start
382 strings that do end in spaces are not tripled, but an underscore
383 is substituted for the space. [This presumes that the space is
384 for appearance, not comment syntax. You can use
385 `allout-mode-leaders' to override this behavior, when
386 undesired.]"
387 :type '(choice (const t) (const nil) string
388 (const allout-mode-leaders)
389 (const comment-start))
390 :group 'allout)
391 ;;;###autoload
392 (put 'allout-use-mode-specific-leader 'safe-local-variable
393 '(lambda (x) (or (memq x '(t nil allout-mode-leaders comment-start))
394 (stringp x))))
395 ;;;_ = allout-mode-leaders
396 (defvar allout-mode-leaders '()
397 "Specific allout-prefix leading strings per major modes.
398
399 Use this if the mode's comment-start string isn't what you
400 prefer, or if the mode lacks a comment-start string. See
401 `allout-use-mode-specific-leader' for more details.
402
403 If you're constructing a string that will comment-out outline
404 structuring so it can be included in program code, append an extra
405 character, like an \"_\" underscore, to distinguish the lead string
406 from regular comments that start at the beginning-of-line.")
407
408 ;;;_ = allout-old-style-prefixes
409 (defcustom allout-old-style-prefixes nil
410 "*When non-nil, use only old-and-crusty `outline-mode' `*' topic prefixes.
411
412 Non-nil restricts the topic creation and modification
413 functions to asterix-padded prefixes, so they look exactly
414 like the original Emacs-outline style prefixes.
415
416 Whatever the setting of this variable, both old and new style prefixes
417 are always respected by the topic maneuvering functions."
418 :type 'boolean
419 :group 'allout)
420 (make-variable-buffer-local 'allout-old-style-prefixes)
421 ;;;###autoload
422 (put 'allout-old-style-prefixes 'safe-local-variable
423 (if (fboundp 'booleanp) 'booleanp '(lambda (x) (member x '(t nil)))))
424 ;;;_ = allout-stylish-prefixes - alternating bullets
425 (defcustom allout-stylish-prefixes t
426 "*Do fancy stuff with topic prefix bullets according to level, etc.
427
428 Non-nil enables topic creation, modification, and repositioning
429 functions to vary the topic bullet char (the char that marks the topic
430 depth) just preceding the start of the topic text) according to level.
431 Otherwise, only asterisks (`*') and distinctive bullets are used.
432
433 This is how an outline can look (but sans indentation) with stylish
434 prefixes:
435
436 * Top level
437 .* A topic
438 . + One level 3 subtopic
439 . . One level 4 subtopic
440 . . A second 4 subtopic
441 . + Another level 3 subtopic
442 . #1 A numbered level 4 subtopic
443 . #2 Another
444 . ! Another level 4 subtopic with a different distinctive bullet
445 . #4 And another numbered level 4 subtopic
446
447 This would be an outline with stylish prefixes inhibited (but the
448 numbered and other distinctive bullets retained):
449
450 * Top level
451 .* A topic
452 . * One level 3 subtopic
453 . * One level 4 subtopic
454 . * A second 4 subtopic
455 . * Another level 3 subtopic
456 . #1 A numbered level 4 subtopic
457 . #2 Another
458 . ! Another level 4 subtopic with a different distinctive bullet
459 . #4 And another numbered level 4 subtopic
460
461 Stylish and constant prefixes (as well as old-style prefixes) are
462 always respected by the topic maneuvering functions, regardless of
463 this variable setting.
464
465 The setting of this var is not relevant when `allout-old-style-prefixes'
466 is non-nil."
467 :type 'boolean
468 :group 'allout)
469 (make-variable-buffer-local 'allout-stylish-prefixes)
470 ;;;###autoload
471 (put 'allout-stylish-prefixes 'safe-local-variable
472 (if (fboundp 'booleanp) 'booleanp '(lambda (x) (member x '(t nil)))))
473
474 ;;;_ = allout-numbered-bullet
475 (defcustom allout-numbered-bullet "#"
476 "*String designating bullet of topics that have auto-numbering; nil for none.
477
478 Topics having this bullet have automatic maintenance of a sibling
479 sequence-number tacked on, just after the bullet. Conventionally set
480 to \"#\", you can set it to a bullet of your choice. A nil value
481 disables numbering maintenance."
482 :type '(choice (const nil) string)
483 :group 'allout)
484 (make-variable-buffer-local 'allout-numbered-bullet)
485 ;;;###autoload
486 (put 'allout-numbered-bullet 'safe-local-variable
487 (if (fboundp 'string-or-null-p)
488 'string-or-null-p
489 '(lambda (x) (or (stringp x) (null x)))))
490 ;;;_ = allout-file-xref-bullet
491 (defcustom allout-file-xref-bullet "@"
492 "*Bullet signifying file cross-references, for `allout-resolve-xref'.
493
494 Set this var to the bullet you want to use for file cross-references."
495 :type '(choice (const nil) string)
496 :group 'allout)
497 ;;;###autoload
498 (put 'allout-file-xref-bullet 'safe-local-variable
499 (if (fboundp 'string-or-null-p)
500 'string-or-null-p
501 '(lambda (x) (or (stringp x) (null x)))))
502 ;;;_ = allout-presentation-padding
503 (defcustom allout-presentation-padding 2
504 "*Presentation-format white-space padding factor, for greater indent."
505 :type 'integer
506 :group 'allout)
507
508 (make-variable-buffer-local 'allout-presentation-padding)
509 ;;;###autoload
510 (put 'allout-presentation-padding 'safe-local-variable 'integerp)
511
512 ;;;_ = allout-abbreviate-flattened-numbering
513 (defcustom allout-abbreviate-flattened-numbering nil
514 "*If non-nil, `allout-flatten-exposed-to-buffer' abbreviates topic
515 numbers to minimal amount with some context. Otherwise, entire
516 numbers are always used."
517 :type 'boolean
518 :group 'allout)
519
520 ;;;_ + LaTeX formatting
521 ;;;_ - allout-number-pages
522 (defcustom allout-number-pages nil
523 "*Non-nil turns on page numbering for LaTeX formatting of an outline."
524 :type 'boolean
525 :group 'allout)
526 ;;;_ - allout-label-style
527 (defcustom allout-label-style "\\large\\bf"
528 "*Font and size of labels for LaTeX formatting of an outline."
529 :type 'string
530 :group 'allout)
531 ;;;_ - allout-head-line-style
532 (defcustom allout-head-line-style "\\large\\sl "
533 "*Font and size of entries for LaTeX formatting of an outline."
534 :type 'string
535 :group 'allout)
536 ;;;_ - allout-body-line-style
537 (defcustom allout-body-line-style " "
538 "*Font and size of entries for LaTeX formatting of an outline."
539 :type 'string
540 :group 'allout)
541 ;;;_ - allout-title-style
542 (defcustom allout-title-style "\\Large\\bf"
543 "*Font and size of titles for LaTeX formatting of an outline."
544 :type 'string
545 :group 'allout)
546 ;;;_ - allout-title
547 (defcustom allout-title '(or buffer-file-name (buffer-name))
548 "*Expression to be evaluated to determine the title for LaTeX
549 formatted copy."
550 :type 'sexp
551 :group 'allout)
552 ;;;_ - allout-line-skip
553 (defcustom allout-line-skip ".05cm"
554 "*Space between lines for LaTeX formatting of an outline."
555 :type 'string
556 :group 'allout)
557 ;;;_ - allout-indent
558 (defcustom allout-indent ".3cm"
559 "*LaTeX formatted depth-indent spacing."
560 :type 'string
561 :group 'allout)
562
563 ;;;_ + Topic encryption
564 ;;;_ = allout-encryption group
565 (defgroup allout-encryption nil
566 "Settings for topic encryption features of allout outliner."
567 :group 'allout)
568 ;;;_ = allout-topic-encryption-bullet
569 (defcustom allout-topic-encryption-bullet "~"
570 "*Bullet signifying encryption of the entry's body."
571 :type '(choice (const nil) string)
572 :version "22.0"
573 :group 'allout-encryption)
574 ;;;_ = allout-passphrase-verifier-handling
575 (defcustom allout-passphrase-verifier-handling t
576 "*Enable use of symmetric encryption passphrase verifier if non-nil.
577
578 See the docstring for the `allout-enable-file-variable-adjustment'
579 variable for details about allout ajustment of file variables."
580 :type 'boolean
581 :version "22.0"
582 :group 'allout-encryption)
583 (make-variable-buffer-local 'allout-passphrase-verifier-handling)
584 ;;;_ = allout-passphrase-hint-handling
585 (defcustom allout-passphrase-hint-handling 'always
586 "*Dictate outline encryption passphrase reminder handling:
587
588 always - always show reminder when prompting
589 needed - show reminder on passphrase entry failure
590 disabled - never present or adjust reminder
591
592 See the docstring for the `allout-enable-file-variable-adjustment'
593 variable for details about allout ajustment of file variables."
594 :type '(choice (const always)
595 (const needed)
596 (const disabled))
597 :version "22.0"
598 :group 'allout-encryption)
599 (make-variable-buffer-local 'allout-passphrase-hint-handling)
600 ;;;_ = allout-encrypt-unencrypted-on-saves
601 (defcustom allout-encrypt-unencrypted-on-saves t
602 "*When saving, should topics pending encryption be encrypted?
603
604 The idea is to prevent file-system exposure of any un-encrypted stuff, and
605 mostly covers both deliberate file writes and auto-saves.
606
607 - Yes: encrypt all topics pending encryption, even if it's the one
608 currently being edited. \(In that case, the currently edited topic
609 will be automatically decrypted before any user interaction, so they
610 can continue editing but the copy on the file system will be
611 encrypted.)
612 Auto-saves will use the \"All except current topic\" mode if this
613 one is selected, to avoid practical difficulties - see below.
614 - All except current topic: skip the topic currently being edited, even if
615 it's pending encryption. This may expose the current topic on the
616 file sytem, but avoids the nuisance of prompts for the encryption
617 passphrase in the middle of editing for, eg, autosaves.
618 This mode is used for auto-saves for both this option and \"Yes\".
619 - No: leave it to the user to encrypt any unencrypted topics.
620
621 For practical reasons, auto-saves always use the 'except-current policy
622 when auto-encryption is enabled. \(Otherwise, spurious passphrase prompts
623 and unavoidable timing collisions are too disruptive.) If security for a
624 file requires that even the current topic is never auto-saved in the clear,
625 disable auto-saves for that file."
626
627 :type '(choice (const :tag "Yes" t)
628 (const :tag "All except current topic" except-current)
629 (const :tag "No" nil))
630 :version "22.0"
631 :group 'allout-encryption)
632 (make-variable-buffer-local 'allout-encrypt-unencrypted-on-saves)
633
634 ;;;_ + Developer
635 ;;;_ = allout-developer group
636 (defgroup allout-developer nil
637 "Settings for topic encryption features of allout outliner."
638 :group 'allout)
639 ;;;_ = allout-run-unit-tests-on-load
640 (defcustom allout-run-unit-tests-on-load nil
641 "*When non-nil, unit tests will be run at end of loading the allout module.
642
643 Generally, allout code developers are the only ones who'll want to set this.
644
645 \(If set, this makes it an even better practice to exercise changes by
646 doing byte-compilation with a repeat count, so the file is loaded after
647 compilation.)
648
649 See `allout-run-unit-tests' to see what's run."
650 :type 'boolean
651 :group 'allout-developer)
652
653 ;;;_ + Miscellaneous customization
654
655 ;;;_ = allout-command-prefix
656 (defcustom allout-command-prefix "\C-c "
657 "*Key sequence to be used as prefix for outline mode command key bindings.
658
659 Default is '\C-c<space>'; just '\C-c' is more short-and-sweet, if you're
660 willing to let allout use a bunch of \C-c keybindings."
661 :type 'string
662 :group 'allout)
663
664 ;;;_ = allout-keybindings-list
665 ;;; You have to reactivate allout-mode - `(allout-mode t)' - to
666 ;;; institute changes to this var.
667 (defvar allout-keybindings-list ()
668 "*List of `allout-mode' key / function bindings, for `allout-mode-map'.
669
670 String or vector key will be prefaced with `allout-command-prefix',
671 unless optional third, non-nil element is present.")
672 (setq allout-keybindings-list
673 '(
674 ; Motion commands:
675 ("\C-n" allout-next-visible-heading)
676 ("\C-p" allout-previous-visible-heading)
677 ("\C-u" allout-up-current-level)
678 ("\C-f" allout-forward-current-level)
679 ("\C-b" allout-backward-current-level)
680 ("\C-a" allout-beginning-of-current-entry)
681 ("\C-e" allout-end-of-entry)
682 ; Exposure commands:
683 ("\C-i" allout-show-children)
684 ("\C-s" allout-show-current-subtree)
685 ("\C-h" allout-hide-current-subtree)
686 ("h" allout-hide-current-subtree)
687 ("\C-o" allout-show-current-entry)
688 ("!" allout-show-all)
689 ("x" allout-toggle-current-subtree-encryption)
690 ; Alteration commands:
691 (" " allout-open-sibtopic)
692 ("." allout-open-subtopic)
693 ("," allout-open-supertopic)
694 ("'" allout-shift-in)
695 (">" allout-shift-in)
696 ("<" allout-shift-out)
697 ("\C-m" allout-rebullet-topic)
698 ("*" allout-rebullet-current-heading)
699 ("#" allout-number-siblings)
700 ("\C-k" allout-kill-line t)
701 ("\M-k" allout-copy-line-as-kill t)
702 ("\C-y" allout-yank t)
703 ("\M-y" allout-yank-pop t)
704 ("\C-k" allout-kill-topic)
705 ("\M-k" allout-copy-topic-as-kill)
706 ; Miscellaneous commands:
707 ;([?\C-\ ] allout-mark-topic)
708 ("@" allout-resolve-xref)
709 ("=c" allout-copy-exposed-to-buffer)
710 ("=i" allout-indented-exposed-to-buffer)
711 ("=t" allout-latexify-exposed)
712 ("=p" allout-flatten-exposed-to-buffer)))
713
714 ;;;_ = allout-inhibit-auto-fill
715 (defcustom allout-inhibit-auto-fill nil
716 "*If non-nil, auto-fill will be inhibited in the allout buffers.
717
718 You can customize this setting to set it for all allout buffers, or set it
719 in individual buffers if you want to inhibit auto-fill only in particular
720 buffers. \(You could use a function on `allout-mode-hook' to inhibit
721 auto-fill according, eg, to the major mode.\)
722
723 If you don't set this and auto-fill-mode is enabled, allout will use the
724 value that `normal-auto-fill-function', if any, when allout mode starts, or
725 else allout's special hanging-indent maintaining auto-fill function,
726 `allout-auto-fill'."
727 :type 'boolean
728 :group 'allout)
729 (make-variable-buffer-local 'allout-inhibit-auto-fill)
730
731 ;;;_ = allout-use-hanging-indents
732 (defcustom allout-use-hanging-indents t
733 "*If non-nil, topic body text auto-indent defaults to indent of the header.
734 Ie, it is indented to be just past the header prefix. This is
735 relevant mostly for use with indented-text-mode, or other situations
736 where auto-fill occurs."
737 :type 'boolean
738 :group 'allout)
739 (make-variable-buffer-local 'allout-use-hanging-indents)
740 ;;;###autoload
741 (put 'allout-use-hanging-indents 'safe-local-variable
742 (if (fboundp 'booleanp) 'booleanp '(lambda (x) (member x '(t nil)))))
743
744 ;;;_ = allout-reindent-bodies
745 (defcustom allout-reindent-bodies (if allout-use-hanging-indents
746 'text)
747 "*Non-nil enables auto-adjust of topic body hanging indent with depth shifts.
748
749 When active, topic body lines that are indented even with or beyond
750 their topic header are reindented to correspond with depth shifts of
751 the header.
752
753 A value of t enables reindent in non-programming-code buffers, ie
754 those that do not have the variable `comment-start' set. A value of
755 `force' enables reindent whether or not `comment-start' is set."
756 :type '(choice (const nil) (const t) (const text) (const force))
757 :group 'allout)
758
759 (make-variable-buffer-local 'allout-reindent-bodies)
760 ;;;###autoload
761 (put 'allout-reindent-bodies 'safe-local-variable
762 '(lambda (x) (memq x '(nil t text force))))
763
764 ;;;_ = allout-enable-file-variable-adjustment
765 (defcustom allout-enable-file-variable-adjustment t
766 "*If non-nil, some allout outline actions edit Emacs local file var text.
767
768 This can range from changes to existing entries, addition of new ones,
769 and creation of a new local variables section when necessary.
770
771 Emacs file variables adjustments are also inhibited if `enable-local-variables'
772 is nil.
773
774 Operations potentially causing edits include allout encryption routines.
775 For details, see `allout-toggle-current-subtree-encryption's docstring."
776 :type 'boolean
777 :group 'allout)
778 (make-variable-buffer-local 'allout-enable-file-variable-adjustment)
779
780 ;;;_* CODE - no user customizations below.
781
782 ;;;_ #1 Internal Outline Formatting and Configuration
783 ;;;_ : Version
784 ;;;_ = allout-version
785 (defvar allout-version "2.2.1"
786 "Version of currently loaded outline package. \(allout.el)")
787 ;;;_ > allout-version
788 (defun allout-version (&optional here)
789 "Return string describing the loaded outline version."
790 (interactive "P")
791 (let ((msg (concat "Allout Outline Mode v " allout-version)))
792 (if here (insert msg))
793 (message "%s" msg)
794 msg))
795 ;;;_ : Mode activation (defined here because it's referenced early)
796 ;;;_ = allout-mode
797 (defvar allout-mode nil "Allout outline mode minor-mode flag.")
798 (make-variable-buffer-local 'allout-mode)
799 ;;;_ = allout-layout nil
800 (defvar allout-layout nil ; LEAVE GLOBAL VALUE NIL - see docstring.
801 "Buffer-specific setting for allout layout.
802
803 In buffers where this is non-nil \(and if `allout-init' has been run, to
804 enable this behavior), `allout-mode' will be automatically activated. The
805 layout dictated by the value will be used to set the initial exposure when
806 `allout-mode' is activated.
807
808 \*You should not setq-default this variable non-nil unless you want every
809 visited file to be treated as an allout file.*
810
811 The value would typically be set by a file local variable. For
812 example, the following lines at the bottom of an Emacs Lisp file:
813
814 ;;;Local variables:
815 ;;;allout-layout: \(0 : -1 -1 0)
816 ;;;End:
817
818 dictate activation of `allout-mode' mode when the file is visited
819 \(presuming allout-init was already run), followed by the
820 equivalent of `\(allout-expose-topic 0 : -1 -1 0)'. \(This is
821 the layout used for the allout.el source file.)
822
823 `allout-default-layout' describes the specification format.
824 `allout-layout' can additionally have the value `t', in which
825 case the value of `allout-default-layout' is used.")
826 (make-variable-buffer-local 'allout-layout)
827 ;;;###autoload
828 (put 'allout-layout 'safe-local-variable
829 '(lambda (x) (or (numberp x) (listp x) (memq x '(: * + -)))))
830
831 ;;;_ : Topic header format
832 ;;;_ = allout-regexp
833 (defvar allout-regexp ""
834 "*Regular expression to match the beginning of a heading line.
835
836 Any line whose beginning matches this regexp is considered a
837 heading. This var is set according to the user configuration vars
838 by `set-allout-regexp'.")
839 (make-variable-buffer-local 'allout-regexp)
840 ;;;_ = allout-bullets-string
841 (defvar allout-bullets-string ""
842 "A string dictating the valid set of outline topic bullets.
843
844 This var should *not* be set by the user - it is set by `set-allout-regexp',
845 and is produced from the elements of `allout-plain-bullets-string'
846 and `allout-distinctive-bullets-string'.")
847 (make-variable-buffer-local 'allout-bullets-string)
848 ;;;_ = allout-bullets-string-len
849 (defvar allout-bullets-string-len 0
850 "Length of current buffers' `allout-plain-bullets-string'.")
851 (make-variable-buffer-local 'allout-bullets-string-len)
852 ;;;_ = allout-depth-specific-regexp
853 (defvar allout-depth-specific-regexp ""
854 "*Regular expression to match a heading line prefix for a particular depth.
855
856 This expression is used to search for depth-specific topic
857 headers at depth 2 and greater. Use `allout-depth-one-regexp'
858 for to seek topics at depth one.
859
860 This var is set according to the user configuration vars by
861 `set-allout-regexp'. It is prepared with format strings for two
862 decimal numbers, which should each be one less than the depth of the
863 topic prefix to be matched.")
864 (make-variable-buffer-local 'allout-depth-specific-regexp)
865 ;;;_ = allout-depth-one-regexp
866 (defvar allout-depth-one-regexp ""
867 "*Regular expression to match a heading line prefix for depth one.
868
869 This var is set according to the user configuration vars by
870 `set-allout-regexp'. It is prepared with format strings for two
871 decimal numbers, which should each be one less than the depth of the
872 topic prefix to be matched.")
873 (make-variable-buffer-local 'allout-depth-one-regexp)
874 ;;;_ = allout-line-boundary-regexp
875 (defvar allout-line-boundary-regexp ()
876 "`allout-regexp' with outline style beginning-of-line anchor.
877
878 This is properly set by `set-allout-regexp'.")
879 (make-variable-buffer-local 'allout-line-boundary-regexp)
880 ;;;_ = allout-bob-regexp
881 (defvar allout-bob-regexp ()
882 "Like `allout-line-boundary-regexp', for headers at beginning of buffer.")
883 (make-variable-buffer-local 'allout-bob-regexp)
884 ;;;_ = allout-header-subtraction
885 (defvar allout-header-subtraction (1- (length allout-header-prefix))
886 "Allout-header prefix length to subtract when computing topic depth.")
887 (make-variable-buffer-local 'allout-header-subtraction)
888 ;;;_ = allout-plain-bullets-string-len
889 (defvar allout-plain-bullets-string-len (length allout-plain-bullets-string)
890 "Length of `allout-plain-bullets-string', updated by `set-allout-regexp'.")
891 (make-variable-buffer-local 'allout-plain-bullets-string-len)
892
893 ;;;_ = allout-doublecheck-at-and-shallower
894 (defconst allout-doublecheck-at-and-shallower 3
895 "Verify apparent topics of this depth and shallower as being non-aberrant.
896
897 Verified with `allout-aberrant-container-p'. This check's usefulness is
898 limited to shallow prospects, because the determination of aberrance
899 depends on the mistaken item being followed by a legitimate item of
900 excessively greater depth.")
901 ;;;_ X allout-reset-header-lead (header-lead)
902 (defun allout-reset-header-lead (header-lead)
903 "*Reset the leading string used to identify topic headers."
904 (interactive "sNew lead string: ")
905 (setq allout-header-prefix header-lead)
906 (setq allout-header-subtraction (1- (length allout-header-prefix)))
907 (set-allout-regexp))
908 ;;;_ X allout-lead-with-comment-string (header-lead)
909 (defun allout-lead-with-comment-string (&optional header-lead)
910 "*Set the topic-header leading string to specified string.
911
912 Useful when for encapsulating outline structure in programming
913 language comments. Returns the leading string."
914
915 (interactive "P")
916 (if (not (stringp header-lead))
917 (setq header-lead (read-string
918 "String prefix for topic headers: ")))
919 (setq allout-reindent-bodies nil)
920 (allout-reset-header-lead header-lead)
921 header-lead)
922 ;;;_ > allout-infer-header-lead-and-primary-bullet ()
923 (defun allout-infer-header-lead-and-primary-bullet ()
924 "Determine appropriate `allout-header-prefix' and `allout-primary-bullet'.
925
926 Works according to settings of:
927
928 `comment-start'
929 `allout-header-prefix' (default)
930 `allout-use-mode-specific-leader'
931 and `allout-mode-leaders'.
932
933 Apply this via \(re)activation of `allout-mode', rather than
934 invoking it directly."
935 (let* ((use-leader (and (boundp 'allout-use-mode-specific-leader)
936 (if (or (stringp allout-use-mode-specific-leader)
937 (memq allout-use-mode-specific-leader
938 '(allout-mode-leaders
939 comment-start
940 t)))
941 allout-use-mode-specific-leader
942 ;; Oops - garbled value, equate with effect of 't:
943 t)))
944 (leader
945 (cond
946 ((not use-leader) nil)
947 ;; Use the explicitly designated leader:
948 ((stringp use-leader) use-leader)
949 (t (or (and (memq use-leader '(t allout-mode-leaders))
950 ;; Get it from outline mode leaders?
951 (cdr (assq major-mode allout-mode-leaders)))
952 ;; ... didn't get from allout-mode-leaders...
953 (and (memq use-leader '(t comment-start))
954 comment-start
955 ;; Use comment-start, maybe tripled, and with
956 ;; underscore:
957 (concat
958 (if (string= " "
959 (substring comment-start
960 (1- (length comment-start))))
961 ;; Use comment-start, sans trailing space:
962 (substring comment-start 0 -1)
963 (concat comment-start comment-start comment-start))
964 ;; ... and append underscore, whichever:
965 "_")))))))
966 (if (not leader)
967 nil
968 (setq allout-header-prefix leader)
969 (if (not allout-old-style-prefixes)
970 ;; setting allout-primary-bullet makes the top level topics use -
971 ;; actually, be - the special prefix:
972 (setq allout-primary-bullet leader))
973 allout-header-prefix)))
974 (defalias 'allout-infer-header-lead
975 'allout-infer-header-lead-and-primary-bullet)
976 ;;;_ > allout-infer-body-reindent ()
977 (defun allout-infer-body-reindent ()
978 "Determine proper setting for `allout-reindent-bodies'.
979
980 Depends on default setting of `allout-reindent-bodies' \(which see)
981 and presence of setting for `comment-start', to tell whether the
982 file is programming code."
983 (if (and allout-reindent-bodies
984 comment-start
985 (not (eq 'force allout-reindent-bodies)))
986 (setq allout-reindent-bodies nil)))
987 ;;;_ > set-allout-regexp ()
988 (defun set-allout-regexp ()
989 "Generate proper topic-header regexp form for outline functions.
990
991 Works with respect to `allout-plain-bullets-string' and
992 `allout-distinctive-bullets-string'.
993
994 Also refresh various data structures that hinge on the regexp."
995
996 (interactive)
997 ;; Derive allout-bullets-string from user configured components:
998 (setq allout-bullets-string "")
999 (let ((strings (list 'allout-plain-bullets-string
1000 'allout-distinctive-bullets-string
1001 'allout-primary-bullet))
1002 cur-string
1003 cur-len
1004 cur-char
1005 index)
1006 (while strings
1007 (setq index 0)
1008 (setq cur-len (length (setq cur-string (symbol-value (car strings)))))
1009 (while (< index cur-len)
1010 (setq cur-char (aref cur-string index))
1011 (setq allout-bullets-string
1012 (concat allout-bullets-string
1013 (cond
1014 ; Single dash would denote a
1015 ; sequence, repeated denotes
1016 ; a dash:
1017 ((eq cur-char ?-) "--")
1018 ; literal close-square-bracket
1019 ; doesn't work right in the
1020 ; expr, exclude it:
1021 ((eq cur-char ?\]) "")
1022 (t (regexp-quote (char-to-string cur-char))))))
1023 (setq index (1+ index)))
1024 (setq strings (cdr strings)))
1025 )
1026 ;; Derive next for repeated use in allout-pending-bullet:
1027 (setq allout-plain-bullets-string-len (length allout-plain-bullets-string))
1028 (setq allout-header-subtraction (1- (length allout-header-prefix)))
1029
1030 (let (new-part old-part)
1031 (setq new-part (concat "\\("
1032 (regexp-quote allout-header-prefix)
1033 "[ \t]*"
1034 ;; already regexp-quoted in a custom way:
1035 "[" allout-bullets-string "]"
1036 "\\)")
1037 old-part (concat "\\("
1038 (regexp-quote allout-primary-bullet)
1039 "\\|"
1040 (regexp-quote allout-header-prefix)
1041 "\\)"
1042 "+"
1043 " ?[^" allout-primary-bullet "]")
1044 allout-regexp (concat new-part
1045 "\\|"
1046 old-part
1047 "\\|\^l")
1048
1049 allout-line-boundary-regexp (concat "\n" new-part
1050 "\\|"
1051 "\n" old-part)
1052
1053 allout-bob-regexp (concat "\\`" new-part
1054 "\\|"
1055 "\\`" old-part))
1056
1057 (setq allout-depth-specific-regexp
1058 (concat "\\(^\\|\\`\\)"
1059 "\\("
1060
1061 ;; new-style spacers-then-bullet string:
1062 "\\("
1063 (allout-format-quote (regexp-quote allout-header-prefix))
1064 " \\{%s\\}"
1065 "[" (allout-format-quote allout-bullets-string) "]"
1066 "\\)"
1067
1068 ;; old-style all-bullets string, if primary not multi-char:
1069 (if (< 0 allout-header-subtraction)
1070 ""
1071 (concat "\\|\\("
1072 (allout-format-quote
1073 (regexp-quote allout-primary-bullet))
1074 (allout-format-quote
1075 (regexp-quote allout-primary-bullet))
1076 (allout-format-quote
1077 (regexp-quote allout-primary-bullet))
1078 "\\{%s\\}"
1079 ;; disqualify greater depths:
1080 "[^"
1081 (allout-format-quote allout-primary-bullet)
1082 "]\\)"
1083 ))
1084 "\\)"
1085 ))
1086 (setq allout-depth-one-regexp
1087 (concat "\\(^\\|\\`\\)"
1088 "\\("
1089
1090 "\\("
1091 (regexp-quote allout-header-prefix)
1092 ;; disqualify any bullet char following any amount of
1093 ;; intervening whitespace:
1094 " *"
1095 (concat "[^ " allout-bullets-string "]")
1096 "\\)"
1097 (if (< 0 allout-header-subtraction)
1098 ;; Need not support anything like the old
1099 ;; bullet style if the prefix is multi-char.
1100 ""
1101 (concat "\\|"
1102 (regexp-quote allout-primary-bullet)
1103 ;; disqualify deeper primary-bullet sequences:
1104 "[^" allout-primary-bullet "]"))
1105 "\\)"
1106 ))))
1107 ;;;_ : Key bindings
1108 ;;;_ = allout-mode-map
1109 (defvar allout-mode-map nil "Keybindings for (allout) outline minor mode.")
1110 ;;;_ > produce-allout-mode-map (keymap-alist &optional base-map)
1111 (defun produce-allout-mode-map (keymap-list &optional base-map)
1112 "Produce keymap for use as allout-mode-map, from KEYMAP-LIST.
1113
1114 Built on top of optional BASE-MAP, or empty sparse map if none specified.
1115 See doc string for allout-keybindings-list for format of binding list."
1116 (let ((map (or base-map (make-sparse-keymap)))
1117 (pref (list allout-command-prefix)))
1118 (mapcar (function
1119 (lambda (cell)
1120 (let ((add-pref (null (cdr (cdr cell))))
1121 (key-suff (list (car cell))))
1122 (apply 'define-key
1123 (list map
1124 (apply 'concat (if add-pref
1125 (append pref key-suff)
1126 key-suff))
1127 (car (cdr cell)))))))
1128 keymap-list)
1129 map))
1130 ;;;_ : Menu bar
1131 (defvar allout-mode-exposure-menu)
1132 (defvar allout-mode-editing-menu)
1133 (defvar allout-mode-navigation-menu)
1134 (defvar allout-mode-misc-menu)
1135 (defun produce-allout-mode-menubar-entries ()
1136 (require 'easymenu)
1137 (easy-menu-define allout-mode-exposure-menu
1138 allout-mode-map
1139 "Allout outline exposure menu."
1140 '("Exposure"
1141 ["Show Entry" allout-show-current-entry t]
1142 ["Show Children" allout-show-children t]
1143 ["Show Subtree" allout-show-current-subtree t]
1144 ["Hide Subtree" allout-hide-current-subtree t]
1145 ["Hide Leaves" allout-hide-current-leaves t]
1146 "----"
1147 ["Show All" allout-show-all t]))
1148 (easy-menu-define allout-mode-editing-menu
1149 allout-mode-map
1150 "Allout outline editing menu."
1151 '("Headings"
1152 ["Open Sibling" allout-open-sibtopic t]
1153 ["Open Subtopic" allout-open-subtopic t]
1154 ["Open Supertopic" allout-open-supertopic t]
1155 "----"
1156 ["Shift Topic In" allout-shift-in t]
1157 ["Shift Topic Out" allout-shift-out t]
1158 ["Rebullet Topic" allout-rebullet-topic t]
1159 ["Rebullet Heading" allout-rebullet-current-heading t]
1160 ["Number Siblings" allout-number-siblings t]
1161 "----"
1162 ["Toggle Topic Encryption"
1163 allout-toggle-current-subtree-encryption
1164 (> (allout-current-depth) 1)]))
1165 (easy-menu-define allout-mode-navigation-menu
1166 allout-mode-map
1167 "Allout outline navigation menu."
1168 '("Navigation"
1169 ["Next Visible Heading" allout-next-visible-heading t]
1170 ["Previous Visible Heading"
1171 allout-previous-visible-heading t]
1172 "----"
1173 ["Up Level" allout-up-current-level t]
1174 ["Forward Current Level" allout-forward-current-level t]
1175 ["Backward Current Level"
1176 allout-backward-current-level t]
1177 "----"
1178 ["Beginning of Entry"
1179 allout-beginning-of-current-entry t]
1180 ["End of Entry" allout-end-of-entry t]
1181 ["End of Subtree" allout-end-of-current-subtree t]))
1182 (easy-menu-define allout-mode-misc-menu
1183 allout-mode-map
1184 "Allout outlines miscellaneous bindings."
1185 '("Misc"
1186 ["Version" allout-version t]
1187 "----"
1188 ["Duplicate Exposed" allout-copy-exposed-to-buffer t]
1189 ["Duplicate Exposed, numbered"
1190 allout-flatten-exposed-to-buffer t]
1191 ["Duplicate Exposed, indented"
1192 allout-indented-exposed-to-buffer t]
1193 "----"
1194 ["Set Header Lead" allout-reset-header-lead t]
1195 ["Set New Exposure" allout-expose-topic t])))
1196 ;;;_ : Allout Modal-Variables Utilities
1197 ;;;_ = allout-mode-prior-settings
1198 (defvar allout-mode-prior-settings nil
1199 "Internal `allout-mode' use; settings to be resumed on mode deactivation.
1200
1201 See `allout-add-resumptions' and `allout-do-resumptions'.")
1202 (make-variable-buffer-local 'allout-mode-prior-settings)
1203 ;;;_ > allout-add-resumptions (&rest pairs)
1204 (defun allout-add-resumptions (&rest pairs)
1205 "Set name/value PAIRS.
1206
1207 Old settings are preserved for later resumption using `allout-do-resumptions'.
1208
1209 The new values are set as a buffer local. On resumption, the prior buffer
1210 scope of the variable is restored along with its value. If it was a void
1211 buffer-local value, then it is left as nil on resumption.
1212
1213 The pairs are lists whose car is the name of the variable and car of the
1214 cdr is the new value: '(some-var some-value)'. The pairs can actually be
1215 triples, where the third element qualifies the disposition of the setting,
1216 as described further below.
1217
1218 If the optional third element is the symbol 'extend, then the new value
1219 created by `cons'ing the second element of the pair onto the front of the
1220 existing value.
1221
1222 If the optional third element is the symbol 'append, then the new value is
1223 extended from the existing one by `append'ing a list containing the second
1224 element of the pair onto the end of the existing value.
1225
1226 Extension, and resumptions in general, should not be used for hook
1227 functions - use the 'local mode of `add-hook' for that, instead.
1228
1229 The settings are stored on `allout-mode-prior-settings'."
1230 (while pairs
1231 (let* ((pair (pop pairs))
1232 (name (car pair))
1233 (value (cadr pair))
1234 (qualifier (if (> (length pair) 2)
1235 (caddr pair)))
1236 prior-value)
1237 (if (not (symbolp name))
1238 (error "Pair's name, %S, must be a symbol, not %s"
1239 name (type-of name)))
1240 (setq prior-value (condition-case nil
1241 (symbol-value name)
1242 (void-variable nil)))
1243 (when (not (assoc name allout-mode-prior-settings))
1244 ;; Not already added as a resumption, create the prior setting entry.
1245 (if (local-variable-p name)
1246 ;; is already local variable - preserve the prior value:
1247 (push (list name prior-value) allout-mode-prior-settings)
1248 ;; wasn't local variable, indicate so for resumption by killing
1249 ;; local value, and make it local:
1250 (push (list name) allout-mode-prior-settings)
1251 (make-local-variable name)))
1252 (if qualifier
1253 (cond ((eq qualifier 'extend)
1254 (if (not (listp prior-value))
1255 (error "extension of non-list prior value attempted")
1256 (set name (cons value prior-value))))
1257 ((eq qualifier 'append)
1258 (if (not (listp prior-value))
1259 (error "appending of non-list prior value attempted")
1260 (set name (append prior-value (list value)))))
1261 (t (error "unrecognized setting qualifier `%s' encountered"
1262 qualifier)))
1263 (set name value)))))
1264 ;;;_ > allout-do-resumptions ()
1265 (defun allout-do-resumptions ()
1266 "Resume all name/value settings registered by `allout-add-resumptions'.
1267
1268 This is used when concluding allout-mode, to resume selected variables to
1269 their settings before allout-mode was started."
1270
1271 (while allout-mode-prior-settings
1272 (let* ((pair (pop allout-mode-prior-settings))
1273 (name (car pair))
1274 (value-cell (cdr pair)))
1275 (if (not value-cell)
1276 ;; Prior value was global:
1277 (kill-local-variable name)
1278 ;; Prior value was explicit:
1279 (set name (car value-cell))))))
1280 ;;;_ : Mode-specific incidentals
1281 ;;;_ > allout-unprotected (expr)
1282 (defmacro allout-unprotected (expr)
1283 "Enable internal outline operations to alter invisible text."
1284 `(let ((inhibit-read-only (if (not buffer-read-only) t))
1285 (inhibit-field-text-motion t))
1286 ,expr))
1287 ;;;_ = allout-mode-hook
1288 (defvar allout-mode-hook nil
1289 "*Hook that's run when allout mode starts.")
1290 ;;;_ = allout-mode-deactivate-hook
1291 (defvar allout-mode-deactivate-hook nil
1292 "*Hook that's run when allout mode ends.")
1293 ;;;_ = allout-exposure-category
1294 (defvar allout-exposure-category nil
1295 "Symbol for use as allout invisible-text overlay category.")
1296 ;;;_ x allout-view-change-hook
1297 (defvar allout-view-change-hook nil
1298 "*\(Deprecated\) A hook run after allout outline exposure changes.
1299
1300 Switch to using `allout-exposure-change-hook' instead. Both hooks are
1301 currently respected, but the other conveys the details of the exposure
1302 change via explicit parameters, and this one will eventually be disabled in
1303 a subsequent allout version.")
1304 ;;;_ = allout-exposure-change-hook
1305 (defvar allout-exposure-change-hook nil
1306 "*Hook that's run after allout outline subtree exposure changes.
1307
1308 It is run at the conclusion of `allout-flag-region'.
1309
1310 Functions on the hook must take three arguments:
1311
1312 - from - integer indicating the point at the start of the change.
1313 - to - integer indicating the point of the end of the change.
1314 - flag - change mode: nil for exposure, otherwise concealment.
1315
1316 This hook might be invoked multiple times by a single command.
1317
1318 This hook is replacing `allout-view-change-hook', which is being deprecated
1319 and eventually will not be invoked.")
1320 ;;;_ = allout-structure-added-hook
1321 (defvar allout-structure-added-hook nil
1322 "*Hook that's run after addition of items to the outline.
1323
1324 Functions on the hook should take two arguments:
1325
1326 - new-start - integer indicating the point at the start of the first new item.
1327 - new-end - integer indicating the point of the end of the last new item.
1328
1329 Some edits that introduce new items may missed by this hook -
1330 specifically edits that native allout routines do not control.
1331
1332 This hook might be invoked multiple times by a single command.")
1333 ;;;_ = allout-structure-deleted-hook
1334 (defvar allout-structure-deleted-hook nil
1335 "*Hook that's run after disciplined deletion of subtrees from the outline.
1336
1337 Functions on the hook must take two arguments:
1338
1339 - depth - integer indicating the depth of the subtree that was deleted.
1340 - removed-from - integer indicating the point where the subtree was removed.
1341
1342 Some edits that remove or invalidate items may missed by this hook -
1343 specifically edits that native allout routines do not control.
1344
1345 This hook might be invoked multiple times by a single command.")
1346 ;;;_ = allout-structure-shifted-hook
1347 (defvar allout-structure-shifted-hook nil
1348 "*Hook that's run after shifting of items in the outline.
1349
1350 Functions on the hook should take two arguments:
1351
1352 - depth-change - integer indicating depth increase, negative for decrease
1353 - start - integer indicating the start point of the shifted parent item.
1354
1355 Some edits that shift items can be missed by this hook - specifically edits
1356 that native allout routines do not control.
1357
1358 This hook might be invoked multiple times by a single command.")
1359 ;;;_ = allout-outside-normal-auto-fill-function
1360 (defvar allout-outside-normal-auto-fill-function nil
1361 "Value of normal-auto-fill-function outside of allout mode.
1362
1363 Used by allout-auto-fill to do the mandated normal-auto-fill-function
1364 wrapped within allout's automatic fill-prefix setting.")
1365 (make-variable-buffer-local 'allout-outside-normal-auto-fill-function)
1366 ;;;_ = file-var-bug hack
1367 (defvar allout-v18/19-file-var-hack nil
1368 "Horrible hack used to prevent invalid multiple triggering of outline
1369 mode from prop-line file-var activation. Used by `allout-mode' function
1370 to track repeats.")
1371 ;;;_ = allout-passphrase-verifier-string
1372 (defvar allout-passphrase-verifier-string nil
1373 "Setting used to test solicited encryption passphrases against the one
1374 already associated with a file.
1375
1376 It consists of an encrypted random string useful only to verify that a
1377 passphrase entered by the user is effective for decryption. The passphrase
1378 itself is \*not* recorded in the file anywhere, and the encrypted contents
1379 are random binary characters to avoid exposing greater susceptibility to
1380 search attacks.
1381
1382 The verifier string is retained as an Emacs file variable, as well as in
1383 the emacs buffer state, if file variable adjustments are enabled. See
1384 `allout-enable-file-variable-adjustment' for details about that.")
1385 (make-variable-buffer-local 'allout-passphrase-verifier-string)
1386 ;;;###autoload
1387 (put 'allout-passphrase-verifier-string 'safe-local-variable 'stringp)
1388 ;;;_ = allout-passphrase-hint-string
1389 (defvar allout-passphrase-hint-string ""
1390 "Variable used to retain reminder string for file's encryption passphrase.
1391
1392 See the description of `allout-passphrase-hint-handling' for details about how
1393 the reminder is deployed.
1394
1395 The hint is retained as an Emacs file variable, as well as in the emacs buffer
1396 state, if file variable adjustments are enabled. See
1397 `allout-enable-file-variable-adjustment' for details about that.")
1398 (make-variable-buffer-local 'allout-passphrase-hint-string)
1399 (setq-default allout-passphrase-hint-string "")
1400 ;;;###autoload
1401 (put 'allout-passphrase-hint-string 'safe-local-variable 'stringp)
1402 ;;;_ = allout-after-save-decrypt
1403 (defvar allout-after-save-decrypt nil
1404 "Internal variable, is nil or has the value of two points:
1405
1406 - the location of a topic to be decrypted after saving is done
1407 - where to situate the cursor after the decryption is performed
1408
1409 This is used to decrypt the topic that was currently being edited, if it
1410 was encrypted automatically as part of a file write or autosave.")
1411 (make-variable-buffer-local 'allout-after-save-decrypt)
1412 ;;;_ = allout-encryption-plaintext-sanitization-regexps
1413 (defvar allout-encryption-plaintext-sanitization-regexps nil
1414 "List of regexps whose matches are removed from plaintext before encryption.
1415
1416 This is for the sake of removing artifacts, like escapes, that are added on
1417 and not actually part of the original plaintext. The removal is done just
1418 prior to encryption.
1419
1420 Entries must be symbols that are bound to the desired values.
1421
1422 Each value can be a regexp or a list with a regexp followed by a
1423 substitution string. If it's just a regexp, all its matches are removed
1424 before the text is encrypted. If it's a regexp and a substitution, the
1425 substition is used against the regexp matches, a la `replace-match'.")
1426 (make-variable-buffer-local 'allout-encryption-text-removal-regexps)
1427 ;;;_ = allout-encryption-ciphertext-rejection-regexps
1428 (defvar allout-encryption-ciphertext-rejection-regexps nil
1429 "Variable for regexps matching plaintext to remove before encryption.
1430
1431 This is for the sake of redoing encryption in cases where the ciphertext
1432 incidentally contains strings that would disrupt mode operation -
1433 for example, a line that happens to look like an allout-mode topic prefix.
1434
1435 Entries must be symbols that are bound to the desired regexp values.
1436
1437 The encryption will be retried up to
1438 `allout-encryption-ciphertext-rejection-limit' times, after which an error
1439 is raised.")
1440
1441 (make-variable-buffer-local 'allout-encryption-ciphertext-rejection-regexps)
1442 ;;;_ = allout-encryption-ciphertext-rejection-ceiling
1443 (defvar allout-encryption-ciphertext-rejection-ceiling 5
1444 "Limit on number of times encryption ciphertext is rejected.
1445
1446 See `allout-encryption-ciphertext-rejection-regexps' for rejection reasons.")
1447 (make-variable-buffer-local 'allout-encryption-ciphertext-rejection-ceiling)
1448 ;;;_ > allout-mode-p ()
1449 ;; Must define this macro above any uses, or byte compilation will lack
1450 ;; proper def, if file isn't loaded - eg, during emacs build!
1451 (defmacro allout-mode-p ()
1452 "Return t if `allout-mode' is active in current buffer."
1453 'allout-mode)
1454 ;;;_ > allout-write-file-hook-handler ()
1455 (defun allout-write-file-hook-handler ()
1456 "Implement `allout-encrypt-unencrypted-on-saves' policy for file writes."
1457
1458 (if (or (not (allout-mode-p))
1459 (not (boundp 'allout-encrypt-unencrypted-on-saves))
1460 (not allout-encrypt-unencrypted-on-saves))
1461 nil
1462 (let ((except-mark (and (equal allout-encrypt-unencrypted-on-saves
1463 'except-current)
1464 (point-marker))))
1465 (if (save-excursion (goto-char (point-min))
1466 (allout-next-topic-pending-encryption except-mark))
1467 (progn
1468 (message "auto-encrypting pending topics")
1469 (sit-for 0)
1470 (condition-case failure
1471 (setq allout-after-save-decrypt
1472 (allout-encrypt-decrypted except-mark))
1473 (error (progn
1474 (message
1475 "allout-write-file-hook-handler suppressing error %s"
1476 failure)
1477 (sit-for 2))))))
1478 ))
1479 nil)
1480 ;;;_ > allout-auto-save-hook-handler ()
1481 (defun allout-auto-save-hook-handler ()
1482 "Implement `allout-encrypt-unencrypted-on-saves' policy for auto save."
1483
1484 (if (and (allout-mode-p) allout-encrypt-unencrypted-on-saves)
1485 ;; Always implement 'except-current policy when enabled.
1486 (let ((allout-encrypt-unencrypted-on-saves 'except-current))
1487 (allout-write-file-hook-handler))))
1488 ;;;_ > allout-after-saves-handler ()
1489 (defun allout-after-saves-handler ()
1490 "Decrypt topic encrypted for save, if it's currently being edited.
1491
1492 Ie, if it was pending encryption and contained the point in its body before
1493 the save.
1494
1495 We use values stored in `allout-after-save-decrypt' to locate the topic
1496 and the place for the cursor after the decryption is done."
1497 (if (not (and (allout-mode-p)
1498 (boundp 'allout-after-save-decrypt)
1499 allout-after-save-decrypt))
1500 t
1501 (goto-char (car allout-after-save-decrypt))
1502 (let ((was-modified (buffer-modified-p)))
1503 (allout-toggle-subtree-encryption)
1504 (if (not was-modified)
1505 (set-buffer-modified-p nil)))
1506 (goto-char (cadr allout-after-save-decrypt))
1507 (setq allout-after-save-decrypt nil))
1508 )
1509
1510 ;;;_ #2 Mode activation
1511 ;;;_ = allout-explicitly-deactivated
1512 (defvar allout-explicitly-deactivated nil
1513 "If t, `allout-mode's last deactivation was deliberate.
1514 So `allout-post-command-business' should not reactivate it...")
1515 (make-variable-buffer-local 'allout-explicitly-deactivated)
1516 ;;;_ > allout-init (&optional mode)
1517 (defun allout-init (&optional mode)
1518 "Prime `allout-mode' to enable/disable auto-activation, wrt `allout-layout'.
1519
1520 MODE is one of the following symbols:
1521
1522 - nil \(or no argument) deactivate auto-activation/layout;
1523 - `activate', enable auto-activation only;
1524 - `ask', enable auto-activation, and enable auto-layout but with
1525 confirmation for layout operation solicited from user each time;
1526 - `report', just report and return the current auto-activation state;
1527 - anything else \(eg, t) for auto-activation and auto-layout, without
1528 any confirmation check.
1529
1530 Use this function to setup your Emacs session for automatic activation
1531 of allout outline mode, contingent to the buffer-specific setting of
1532 the `allout-layout' variable. (See `allout-layout' and
1533 `allout-expose-topic' docstrings for more details on auto layout).
1534
1535 `allout-init' works by setting up (or removing) the `allout-mode'
1536 find-file-hook, and giving `allout-auto-activation' a suitable
1537 setting.
1538
1539 To prime your Emacs session for full auto-outline operation, include
1540 the following two lines in your Emacs init file:
1541
1542 \(require 'allout)
1543 \(allout-init t)"
1544
1545 (interactive)
1546 (if (interactive-p)
1547 (progn
1548 (setq mode
1549 (completing-read
1550 (concat "Select outline auto setup mode "
1551 "(empty for report, ? for options) ")
1552 '(("nil")("full")("activate")("deactivate")
1553 ("ask") ("report") (""))
1554 nil
1555 t))
1556 (if (string= mode "")
1557 (setq mode 'report)
1558 (setq mode (intern-soft mode)))))
1559 (let
1560 ;; convenience aliases, for consistent ref to respective vars:
1561 ((hook 'allout-find-file-hook)
1562 (find-file-hook-var-name (if (boundp 'find-file-hook)
1563 'find-file-hook
1564 'find-file-hooks))
1565 (curr-mode 'allout-auto-activation))
1566
1567 (cond ((not mode)
1568 (set find-file-hook-var-name
1569 (delq hook (symbol-value find-file-hook-var-name)))
1570 (if (interactive-p)
1571 (message "Allout outline mode auto-activation inhibited.")))
1572 ((eq mode 'report)
1573 (if (not (memq hook (symbol-value find-file-hook-var-name)))
1574 (allout-init nil)
1575 ;; Just punt and use the reports from each of the modes:
1576 (allout-init (symbol-value curr-mode))))
1577 (t (add-hook find-file-hook-var-name hook)
1578 (set curr-mode ; `set', not `setq'!
1579 (cond ((eq mode 'activate)
1580 (message
1581 "Outline mode auto-activation enabled.")
1582 'activate)
1583 ((eq mode 'report)
1584 ;; Return the current mode setting:
1585 (allout-init mode))
1586 ((eq mode 'ask)
1587 (message
1588 (concat "Outline mode auto-activation and "
1589 "-layout \(upon confirmation) enabled."))
1590 'ask)
1591 ((message
1592 "Outline mode auto-activation and -layout enabled.")
1593 'full)))))))
1594 ;;;_ > allout-setup-menubar ()
1595 (defun allout-setup-menubar ()
1596 "Populate the current buffer's menubar with `allout-mode' stuff."
1597 (let ((menus (list allout-mode-exposure-menu
1598 allout-mode-editing-menu
1599 allout-mode-navigation-menu
1600 allout-mode-misc-menu))
1601 cur)
1602 (while menus
1603 (setq cur (car menus)
1604 menus (cdr menus))
1605 (easy-menu-add cur))))
1606 ;;;_ > allout-overlay-preparations
1607 (defun allout-overlay-preparations ()
1608 "Set the properties of the allout invisible-text overlay and others."
1609 (setplist 'allout-exposure-category nil)
1610 (put 'allout-exposure-category 'invisible 'allout)
1611 (put 'allout-exposure-category 'evaporate t)
1612 ;; XXX We use isearch-open-invisible *and* isearch-mode-end-hook. The
1613 ;; latter would be sufficient, but it seems that a separate behavior -
1614 ;; the _transient_ opening of invisible text during isearch - is keyed to
1615 ;; presence of the isearch-open-invisible property - even though this
1616 ;; property controls the isearch _arrival_ behavior. This is the case at
1617 ;; least in emacs 21, 22.0, and xemacs 21.4.
1618 (put 'allout-exposure-category 'isearch-open-invisible
1619 'allout-isearch-end-handler)
1620 (if (featurep 'xemacs)
1621 (put 'allout-exposure-category 'start-open t)
1622 (put 'allout-exposure-category 'insert-in-front-hooks
1623 '(allout-overlay-insert-in-front-handler)))
1624 (put 'allout-exposure-category 'modification-hooks
1625 '(allout-overlay-interior-modification-handler)))
1626 ;;;_ > allout-mode (&optional toggle)
1627 ;;;_ : Defun:
1628 ;;;###autoload
1629 (defun allout-mode (&optional toggle)
1630 ;;;_ . Doc string:
1631 "Toggle minor mode for controlling exposure and editing of text outlines.
1632 \\<allout-mode-map>
1633
1634 Optional arg forces mode to re-initialize iff arg is positive num or
1635 symbol. Allout outline mode always runs as a minor mode.
1636
1637 Allout outline mode provides extensive outline oriented formatting and
1638 manipulation. It enables structural editing of outlines, as well as
1639 navigation and exposure. It also is specifically aimed at
1640 accommodating syntax-sensitive text like programming languages. \(For
1641 an example, see the allout code itself, which is organized as an allout
1642 outline.)
1643
1644 In addition to outline navigation and exposure, allout includes:
1645
1646 - topic-oriented repositioning, promotion/demotion, cut, and paste
1647 - integral outline exposure-layout
1648 - incremental search with dynamic exposure and reconcealment of hidden text
1649 - automatic topic-number maintenance
1650 - easy topic encryption and decryption
1651 - \"Hot-spot\" operation, for single-keystroke maneuvering and
1652 exposure control. \(See the allout-mode docstring.)
1653
1654 and many other features.
1655
1656 Below is a description of the bindings, and then explanation of
1657 special `allout-mode' features and terminology. See also the outline
1658 menubar additions for quick reference to many of the features, and see
1659 the docstring of the function `allout-init' for instructions on
1660 priming your emacs session for automatic activation of `allout-mode'.
1661
1662
1663 The bindings are dictated by the `allout-keybindings-list' and
1664 `allout-command-prefix' variables.
1665
1666 Navigation: Exposure Control:
1667 ---------- ----------------
1668 \\[allout-next-visible-heading] allout-next-visible-heading | \\[allout-hide-current-subtree] allout-hide-current-subtree
1669 \\[allout-previous-visible-heading] allout-previous-visible-heading | \\[allout-show-children] allout-show-children
1670 \\[allout-up-current-level] allout-up-current-level | \\[allout-show-current-subtree] allout-show-current-subtree
1671 \\[allout-forward-current-level] allout-forward-current-level | \\[allout-show-current-entry] allout-show-current-entry
1672 \\[allout-backward-current-level] allout-backward-current-level | \\[allout-show-all] allout-show-all
1673 \\[allout-end-of-entry] allout-end-of-entry
1674 \\[allout-beginning-of-current-entry] allout-beginning-of-current-entry, alternately, goes to hot-spot
1675
1676 Topic Header Production:
1677 -----------------------
1678 \\[allout-open-sibtopic] allout-open-sibtopic Create a new sibling after current topic.
1679 \\[allout-open-subtopic] allout-open-subtopic ... an offspring of current topic.
1680 \\[allout-open-supertopic] allout-open-supertopic ... a sibling of the current topic's parent.
1681
1682 Topic Level and Prefix Adjustment:
1683 ---------------------------------
1684 \\[allout-shift-in] allout-shift-in Shift current topic and all offspring deeper.
1685 \\[allout-shift-out] allout-shift-out ... less deep.
1686 \\[allout-rebullet-current-heading] allout-rebullet-current-heading Prompt for alternate bullet for
1687 current topic.
1688 \\[allout-rebullet-topic] allout-rebullet-topic Reconcile bullets of topic and its offspring
1689 - distinctive bullets are not changed, others
1690 alternated according to nesting depth.
1691 \\[allout-number-siblings] allout-number-siblings Number bullets of topic and siblings - the
1692 offspring are not affected. With repeat
1693 count, revoke numbering.
1694
1695 Topic-oriented Killing and Yanking:
1696 ----------------------------------
1697 \\[allout-kill-topic] allout-kill-topic Kill current topic, including offspring.
1698 \\[allout-copy-topic-as-kill] allout-copy-topic-as-kill Copy current topic, including offspring.
1699 \\[allout-kill-line] allout-kill-line kill-line, attending to outline structure.
1700 \\[allout-copy-line-as-kill] allout-copy-line-as-kill Copy line but don't delete it.
1701 \\[allout-yank] allout-yank Yank, adjusting depth of yanked topic to
1702 depth of heading if yanking into bare topic
1703 heading (ie, prefix sans text).
1704 \\[allout-yank-pop] allout-yank-pop Is to allout-yank as yank-pop is to yank
1705
1706 Topic-oriented Encryption:
1707 -------------------------
1708 \\[allout-toggle-current-subtree-encryption] allout-toggle-current-subtree-encryption Encrypt/Decrypt topic content
1709
1710 Misc commands:
1711 -------------
1712 M-x outlineify-sticky Activate outline mode for current buffer,
1713 and establish a default file-var setting
1714 for `allout-layout'.
1715 \\[allout-mark-topic] allout-mark-topic
1716 \\[allout-copy-exposed-to-buffer] allout-copy-exposed-to-buffer
1717 Duplicate outline, sans concealed text, to
1718 buffer with name derived from derived from that
1719 of current buffer - \"*BUFFERNAME exposed*\".
1720 \\[allout-flatten-exposed-to-buffer] allout-flatten-exposed-to-buffer
1721 Like above 'copy-exposed', but convert topic
1722 prefixes to section.subsection... numeric
1723 format.
1724 \\[eval-expression] (allout-init t) Setup Emacs session for outline mode
1725 auto-activation.
1726
1727 Topic Encryption
1728
1729 Outline mode supports gpg encryption of topics, with support for
1730 symmetric and key-pair modes, passphrase timeout, passphrase
1731 consistency checking, user-provided hinting for symmetric key
1732 mode, and auto-encryption of topics pending encryption on save.
1733 \(Topics pending encryption are, by default, automatically
1734 encrypted during file saves; if you're editing the contents of
1735 such a topic, it is automatically decrypted for continued
1736 editing.) The aim is reliable topic privacy while preventing
1737 accidents like neglected encryption before saves, forgetting
1738 which passphrase was used, and other practical pitfalls.
1739
1740 See `allout-toggle-current-subtree-encryption' function docstring and
1741 `allout-encrypt-unencrypted-on-saves' customization variable for details.
1742
1743 HOT-SPOT Operation
1744
1745 Hot-spot operation provides a means for easy, single-keystroke outline
1746 navigation and exposure control.
1747
1748 When the text cursor is positioned directly on the bullet character of
1749 a topic, regular characters (a to z) invoke the commands of the
1750 corresponding allout-mode keymap control chars. For example, \"f\"
1751 would invoke the command typically bound to \"C-c<space>C-f\"
1752 \(\\[allout-forward-current-level] `allout-forward-current-level').
1753
1754 Thus, by positioning the cursor on a topic bullet, you can
1755 execute the outline navigation and manipulation commands with a
1756 single keystroke. Regular navigation keys (eg, \\[forward-char], \\[next-line]) never get
1757 this special translation, so you can use them to get out of the
1758 hot-spot and back to normal operation.
1759
1760 Note that the command `allout-beginning-of-current-entry' \(\\[allout-beginning-of-current-entry]\)
1761 will move to the hot-spot when the cursor is already located at the
1762 beginning of the current entry, so you usually can hit \\[allout-beginning-of-current-entry]
1763 twice in a row to get to the hot-spot.
1764
1765 Terminology
1766
1767 Topic hierarchy constituents - TOPICS and SUBTOPICS:
1768
1769 TOPIC: A basic, coherent component of an Emacs outline. It can
1770 contain and be contained by other topics.
1771 CURRENT topic:
1772 The visible topic most immediately containing the cursor.
1773 DEPTH: The degree of nesting of a topic; it increases with
1774 containment. Also called the:
1775 LEVEL: The same as DEPTH.
1776
1777 ANCESTORS:
1778 The topics that contain a topic.
1779 PARENT: A topic's immediate ancestor. It has a depth one less than
1780 the topic.
1781 OFFSPRING:
1782 The topics contained by a topic;
1783 SUBTOPIC:
1784 An immediate offspring of a topic;
1785 CHILDREN:
1786 The immediate offspring of a topic.
1787 SIBLINGS:
1788 Topics having the same parent and depth.
1789
1790 Topic text constituents:
1791
1792 HEADER: The first line of a topic, include the topic PREFIX and header
1793 text.
1794 PREFIX: The leading text of a topic which distinguishes it from normal
1795 text. It has a strict form, which consists of a prefix-lead
1796 string, padding, and a bullet. The bullet may be followed by a
1797 number, indicating the ordinal number of the topic among its
1798 siblings, a space, and then the header text.
1799
1800 The relative length of the PREFIX determines the nesting depth
1801 of the topic.
1802 PREFIX-LEAD:
1803 The string at the beginning of a topic prefix, normally a `.'.
1804 It can be customized by changing the setting of
1805 `allout-header-prefix' and then reinitializing `allout-mode'.
1806
1807 By setting the prefix-lead to the comment-string of a
1808 programming language, you can embed outline structuring in
1809 program code without interfering with the language processing
1810 of that code. See `allout-use-mode-specific-leader'
1811 docstring for more detail.
1812 PREFIX-PADDING:
1813 Spaces or asterisks which separate the prefix-lead and the
1814 bullet, determining the depth of the topic.
1815 BULLET: A character at the end of the topic prefix, it must be one of
1816 the characters listed on `allout-plain-bullets-string' or
1817 `allout-distinctive-bullets-string'. (See the documentation
1818 for these variables for more details.) The default choice of
1819 bullet when generating topics varies in a cycle with the depth of
1820 the topic.
1821 ENTRY: The text contained in a topic before any offspring.
1822 BODY: Same as ENTRY.
1823
1824
1825 EXPOSURE:
1826 The state of a topic which determines the on-screen visibility
1827 of its offspring and contained text.
1828 CONCEALED:
1829 Topics and entry text whose display is inhibited. Contiguous
1830 units of concealed text is represented by `...' ellipses.
1831
1832 Concealed topics are effectively collapsed within an ancestor.
1833 CLOSED: A topic whose immediate offspring and body-text is concealed.
1834 OPEN: A topic that is not closed, though its offspring or body may be."
1835 ;;;_ . Code
1836 (interactive "P")
1837
1838 (let* ((active (and (not (equal major-mode 'outline))
1839 (allout-mode-p)))
1840 ; Massage universal-arg `toggle' val:
1841 (toggle (and toggle
1842 (or (and (listp toggle)(car toggle))
1843 toggle)))
1844 ; Activation specifically demanded?
1845 (explicit-activation (and toggle
1846 (or (symbolp toggle)
1847 (and (wholenump toggle)
1848 (not (zerop toggle))))))
1849 ;; allout-mode already called once during this complex command?
1850 (same-complex-command (eq allout-v18/19-file-var-hack
1851 (car command-history)))
1852 (write-file-hook-var-name (cond ((boundp 'write-file-functions)
1853 'write-file-functions)
1854 ((boundp 'write-file-hooks)
1855 'write-file-hooks)
1856 (t 'local-write-file-hooks)))
1857 do-layout
1858 )
1859
1860 ; See comments below re v19.18,.19 bug.
1861 (setq allout-v18/19-file-var-hack (car command-history))
1862
1863 (cond
1864
1865 ;; Provision for v19.18, 19.19 bug -
1866 ;; Emacs v 19.18, 19.19 file-var code invokes prop-line-designated
1867 ;; modes twice when file is visited. We have to avoid toggling mode
1868 ;; off on second invocation, so we detect it as best we can, and
1869 ;; skip everything.
1870 ((and same-complex-command ; Still in same complex command
1871 ; as last time `allout-mode' invoked.
1872 active ; Already activated.
1873 (not explicit-activation) ; Prop-line file-vars don't have args.
1874 (string-match "^19.1[89]" ; Bug only known to be in v19.18 and
1875 emacs-version)); 19.19.
1876 t)
1877
1878 ;; Deactivation:
1879 ((and (not explicit-activation)
1880 (or active toggle))
1881 ; Activation not explicitly
1882 ; requested, and either in
1883 ; active state or *de*activation
1884 ; specifically requested:
1885 (setq allout-explicitly-deactivated t)
1886
1887 (allout-do-resumptions)
1888
1889 (remove-from-invisibility-spec '(allout . t))
1890 (remove-hook 'pre-command-hook 'allout-pre-command-business t)
1891 (remove-hook 'post-command-hook 'allout-post-command-business t)
1892 (remove-hook 'before-change-functions 'allout-before-change-handler t)
1893 (remove-hook 'isearch-mode-end-hook 'allout-isearch-end-handler t)
1894 (remove-hook write-file-hook-var-name 'allout-write-file-hook-handler t)
1895 (remove-hook 'auto-save-hook 'allout-auto-save-hook-handler t)
1896
1897 (remove-overlays (point-min) (point-max)
1898 'category 'allout-exposure-category)
1899
1900 (setq allout-mode nil)
1901 (run-hooks 'allout-mode-deactivate-hook))
1902
1903 ;; Activation:
1904 ((not active)
1905 (setq allout-explicitly-deactivated nil)
1906 (if allout-old-style-prefixes
1907 ;; Inhibit all the fancy formatting:
1908 (allout-add-resumptions '(allout-primary-bullet "*")))
1909
1910 (allout-overlay-preparations) ; Doesn't hurt to redo this.
1911
1912 (allout-infer-header-lead-and-primary-bullet)
1913 (allout-infer-body-reindent)
1914
1915 (set-allout-regexp)
1916 (allout-add-resumptions
1917 '(allout-encryption-ciphertext-rejection-regexps
1918 allout-line-boundary-regexp
1919 extend)
1920 '(allout-encryption-ciphertext-rejection-regexps
1921 allout-bob-regexp
1922 extend))
1923
1924 ;; Produce map from current version of allout-keybindings-list:
1925 (setq allout-mode-map
1926 (produce-allout-mode-map allout-keybindings-list))
1927 (substitute-key-definition 'beginning-of-line
1928 'allout-beginning-of-line
1929 allout-mode-map global-map)
1930 (substitute-key-definition 'move-beginning-of-line
1931 'allout-beginning-of-line
1932 allout-mode-map global-map)
1933 (substitute-key-definition 'end-of-line
1934 'allout-end-of-line
1935 allout-mode-map global-map)
1936 (substitute-key-definition 'move-end-of-line
1937 'allout-end-of-line
1938 allout-mode-map global-map)
1939 (produce-allout-mode-menubar-entries)
1940 (fset 'allout-mode-map allout-mode-map)
1941
1942 ;; Include on minor-mode-map-alist, if not already there:
1943 (if (not (member '(allout-mode . allout-mode-map)
1944 minor-mode-map-alist))
1945 (setq minor-mode-map-alist
1946 (cons '(allout-mode . allout-mode-map)
1947 minor-mode-map-alist)))
1948
1949 (add-to-invisibility-spec '(allout . t))
1950 (allout-add-resumptions '(line-move-ignore-invisible t))
1951 (add-hook 'pre-command-hook 'allout-pre-command-business nil t)
1952 (add-hook 'post-command-hook 'allout-post-command-business nil t)
1953 (add-hook 'before-change-functions 'allout-before-change-handler
1954 nil t)
1955 (add-hook 'isearch-mode-end-hook 'allout-isearch-end-handler nil t)
1956 (add-hook write-file-hook-var-name 'allout-write-file-hook-handler
1957 nil t)
1958 (add-hook 'auto-save-hook 'allout-auto-save-hook-handler
1959 nil t)
1960
1961 ;; Stash auto-fill settings and adjust so custom allout auto-fill
1962 ;; func will be used if auto-fill is active or activated. (The
1963 ;; custom func respects topic headline, maintains hanging-indents,
1964 ;; etc.)
1965 (if (and auto-fill-function (not allout-inhibit-auto-fill))
1966 ;; allout-auto-fill will use the stashed values and so forth.
1967 (allout-add-resumptions '(auto-fill-function allout-auto-fill)))
1968 (allout-add-resumptions (list 'allout-former-auto-filler
1969 auto-fill-function)
1970 ;; Register allout-auto-fill to be used if
1971 ;; filling is active:
1972 (list 'allout-outside-normal-auto-fill-function
1973 normal-auto-fill-function)
1974 '(normal-auto-fill-function allout-auto-fill)
1975 ;; Paragraphs are broken by topic headlines.
1976 (list 'paragraph-start
1977 (concat paragraph-start "\\|^\\("
1978 allout-regexp "\\)"))
1979 (list 'paragraph-separate
1980 (concat paragraph-separate "\\|^\\("
1981 allout-regexp "\\)")))
1982 (or (assq 'allout-mode minor-mode-alist)
1983 (setq minor-mode-alist
1984 (cons '(allout-mode " Allout") minor-mode-alist)))
1985
1986 (allout-setup-menubar)
1987
1988 (if allout-layout
1989 (setq do-layout t))
1990
1991 (setq allout-mode t)
1992 (run-hooks 'allout-mode-hook))
1993
1994 ;; Reactivation:
1995 ((setq do-layout t)
1996 (allout-infer-body-reindent))
1997 ) ;; end of activation-mode cases.
1998
1999 ;; Do auto layout if warranted:
2000 (let ((use-layout (if (listp allout-layout)
2001 allout-layout
2002 allout-default-layout)))
2003 (if (and do-layout
2004 allout-auto-activation
2005 use-layout
2006 (and (not (eq allout-auto-activation 'activate))
2007 (if (eq allout-auto-activation 'ask)
2008 (if (y-or-n-p (format "Expose %s with layout '%s'? "
2009 (buffer-name)
2010 use-layout))
2011 t
2012 (message "Skipped %s layout." (buffer-name))
2013 nil)
2014 t)))
2015 (save-excursion
2016 (message "Adjusting '%s' exposure..." (buffer-name))
2017 (goto-char 0)
2018 (allout-this-or-next-heading)
2019 (condition-case err
2020 (progn
2021 (apply 'allout-expose-topic (list use-layout))
2022 (message "Adjusting '%s' exposure... done." (buffer-name)))
2023 ;; Problem applying exposure - notify user, but don't
2024 ;; interrupt, eg, file visit:
2025 (error (message "%s" (car (cdr err)))
2026 (sit-for 1))))))
2027 allout-mode
2028 ) ; let*
2029 ) ; defun
2030 ;;;_ > allout-minor-mode
2031 (defalias 'allout-minor-mode 'allout-mode)
2032
2033 ;;;_ - Position Assessment
2034 ;;;_ > allout-hidden-p (&optional pos)
2035 (defsubst allout-hidden-p (&optional pos)
2036 "Non-nil if the character after point is invisible."
2037 (eq (get-char-property (or pos (point)) 'invisible) 'allout))
2038
2039 ;;;_ > allout-overlay-insert-in-front-handler (ol after beg end
2040 ;;; &optional prelen)
2041 (defun allout-overlay-insert-in-front-handler (ol after beg end
2042 &optional prelen)
2043 "Shift the overlay so stuff inserted in front of it are excluded."
2044 (if after
2045 (move-overlay ol (1+ beg) (overlay-end ol))))
2046 ;;;_ > allout-overlay-interior-modification-handler (ol after beg end
2047 ;;; &optional prelen)
2048 (defun allout-overlay-interior-modification-handler (ol after beg end
2049 &optional prelen)
2050 "Get confirmation before making arbitrary changes to invisible text.
2051
2052 We expose the invisible text and ask for confirmation. Refusal or
2053 keyboard-quit abandons the changes, with keyboard-quit additionally
2054 reclosing the opened text.
2055
2056 No confirmation is necessary when inhibit-read-only is set - eg, allout
2057 internal functions use this feature cohesively bunch changes."
2058
2059 (when (and (not inhibit-read-only) (not after))
2060 (let ((start (point))
2061 (ol-start (overlay-start ol))
2062 (ol-end (overlay-end ol))
2063 first)
2064 (goto-char beg)
2065 (while (< (point) end)
2066 (when (allout-hidden-p)
2067 (allout-show-to-offshoot)
2068 (if (allout-hidden-p)
2069 (save-excursion (forward-char 1)
2070 (allout-show-to-offshoot)))
2071 (when (not first)
2072 (setq first (point))))
2073 (goto-char (if (featurep 'xemacs)
2074 (next-property-change (1+ (point)) nil end)
2075 (next-char-property-change (1+ (point)) end))))
2076 (when first
2077 (goto-char first)
2078 (condition-case nil
2079 (if (not
2080 (yes-or-no-p
2081 (substitute-command-keys
2082 (concat "Modify concealed text? (\"no\" just aborts,"
2083 " \\[keyboard-quit] also reconceals) "))))
2084 (progn (goto-char start)
2085 (error "Concealed-text change refused.")))
2086 (quit (allout-flag-region ol-start ol-end nil)
2087 (allout-flag-region ol-start ol-end t)
2088 (error "Concealed-text change abandoned, text reconcealed."))))
2089 (goto-char start))))
2090 ;;;_ > allout-before-change-handler (beg end)
2091 (defun allout-before-change-handler (beg end)
2092 "Protect against changes to invisible text.
2093
2094 See allout-overlay-interior-modification-handler for details."
2095
2096 (if (and (allout-mode-p) undo-in-progress (allout-hidden-p))
2097 (allout-show-to-offshoot))
2098
2099 ;; allout-overlay-interior-modification-handler on an overlay handles
2100 ;; this in other emacs, via `allout-exposure-category's 'modification-hooks.
2101 (when (and (featurep 'xemacs) (allout-mode-p))
2102 ;; process all of the pending overlays:
2103 (save-excursion
2104 (got-char beg)
2105 (let ((overlay (allout-get-invisibility-overlay)))
2106 (allout-overlay-interior-modification-handler
2107 overlay nil beg end nil)))))
2108 ;;;_ > allout-isearch-end-handler (&optional overlay)
2109 (defun allout-isearch-end-handler (&optional overlay)
2110 "Reconcile allout outline exposure on arriving in hidden text after isearch.
2111
2112 Optional OVERLAY parameter is for when this function is used by
2113 `isearch-open-invisible' overlay property. It is otherwise unused, so this
2114 function can also be used as an `isearch-mode-end-hook'."
2115
2116 (if (and (allout-mode-p) (allout-hidden-p))
2117 (allout-show-to-offshoot)))
2118
2119 ;;;_ #3 Internal Position State-Tracking - "allout-recent-*" funcs
2120 ;;; All the basic outline functions that directly do string matches to
2121 ;;; evaluate heading prefix location set the variables
2122 ;;; `allout-recent-prefix-beginning' and `allout-recent-prefix-end'
2123 ;;; when successful. Functions starting with `allout-recent-' all
2124 ;;; use this state, providing the means to avoid redundant searches
2125 ;;; for just-established data. This optimization can provide
2126 ;;; significant speed improvement, but it must be employed carefully.
2127 ;;;_ = allout-recent-prefix-beginning
2128 (defvar allout-recent-prefix-beginning 0
2129 "Buffer point of the start of the last topic prefix encountered.")
2130 (make-variable-buffer-local 'allout-recent-prefix-beginning)
2131 ;;;_ = allout-recent-prefix-end
2132 (defvar allout-recent-prefix-end 0
2133 "Buffer point of the end of the last topic prefix encountered.")
2134 (make-variable-buffer-local 'allout-recent-prefix-end)
2135 ;;;_ = allout-recent-depth
2136 (defvar allout-recent-depth 0
2137 "Depth of the last topic prefix encountered.")
2138 (make-variable-buffer-local 'allout-recent-depth)
2139 ;;;_ = allout-recent-end-of-subtree
2140 (defvar allout-recent-end-of-subtree 0
2141 "Buffer point last returned by `allout-end-of-current-subtree'.")
2142 (make-variable-buffer-local 'allout-recent-end-of-subtree)
2143 ;;;_ > allout-prefix-data ()
2144 (defsubst allout-prefix-data ()
2145 "Register allout-prefix state data.
2146
2147 For reference by `allout-recent' funcs. Returns BEGINNING."
2148 (setq allout-recent-prefix-end (or (match-end 1) (match-end 2))
2149 allout-recent-prefix-beginning (or (match-beginning 1)
2150 (match-beginning 2))
2151 allout-recent-depth (max 1 (- allout-recent-prefix-end
2152 allout-recent-prefix-beginning
2153 allout-header-subtraction)))
2154 allout-recent-prefix-beginning)
2155 ;;;_ > nullify-allout-prefix-data ()
2156 (defsubst nullify-allout-prefix-data ()
2157 "Mark allout prefix data as being uninformative."
2158 (setq allout-recent-prefix-end (point)
2159 allout-recent-prefix-beginning (point)
2160 allout-recent-depth 0)
2161 allout-recent-prefix-beginning)
2162 ;;;_ > allout-recent-depth ()
2163 (defsubst allout-recent-depth ()
2164 "Return depth of last heading encountered by an outline maneuvering function.
2165
2166 All outline functions which directly do string matches to assess
2167 headings set the variables `allout-recent-prefix-beginning' and
2168 `allout-recent-prefix-end' if successful. This function uses those settings
2169 to return the current depth."
2170
2171 allout-recent-depth)
2172 ;;;_ > allout-recent-prefix ()
2173 (defsubst allout-recent-prefix ()
2174 "Like `allout-recent-depth', but returns text of last encountered prefix.
2175
2176 All outline functions which directly do string matches to assess
2177 headings set the variables `allout-recent-prefix-beginning' and
2178 `allout-recent-prefix-end' if successful. This function uses those settings
2179 to return the current prefix."
2180 (buffer-substring-no-properties allout-recent-prefix-beginning
2181 allout-recent-prefix-end))
2182 ;;;_ > allout-recent-bullet ()
2183 (defmacro allout-recent-bullet ()
2184 "Like allout-recent-prefix, but returns bullet of last encountered prefix.
2185
2186 All outline functions which directly do string matches to assess
2187 headings set the variables `allout-recent-prefix-beginning' and
2188 `allout-recent-prefix-end' if successful. This function uses those settings
2189 to return the current depth of the most recently matched topic."
2190 '(buffer-substring-no-properties (1- allout-recent-prefix-end)
2191 allout-recent-prefix-end))
2192
2193 ;;;_ #4 Navigation
2194
2195 ;;;_ - Position Assessment
2196 ;;;_ : Location Predicates
2197 ;;;_ > allout-on-current-heading-p ()
2198 (defun allout-on-current-heading-p ()
2199 "Return non-nil if point is on current visible topics' header line.
2200
2201 Actually, returns prefix beginning point."
2202 (save-excursion
2203 (allout-beginning-of-current-line)
2204 (and (looking-at allout-regexp)
2205 (not (allout-aberrant-container-p))
2206 (allout-prefix-data))))
2207 ;;;_ > allout-on-heading-p ()
2208 (defalias 'allout-on-heading-p 'allout-on-current-heading-p)
2209 ;;;_ > allout-e-o-prefix-p ()
2210 (defun allout-e-o-prefix-p ()
2211 "True if point is located where current topic prefix ends, heading begins."
2212 (and (save-excursion (let ((inhibit-field-text-motion t))
2213 (beginning-of-line))
2214 (looking-at allout-regexp))
2215 (= (point)(save-excursion (allout-end-of-prefix)(point)))))
2216 ;;;_ > allout-aberrant-container-p ()
2217 (defun allout-aberrant-container-p ()
2218 "True if topic, or next sibling with children, contains them discontinuously.
2219
2220 Discontinuous means an immediate offspring that is nested more
2221 than one level deeper than the topic.
2222
2223 If topic has no offspring, then the next sibling with offspring will
2224 determine whether or not this one is determined to be aberrant.
2225
2226 If true, then the allout-recent-* settings are calibrated on the
2227 offspring that qaulifies it as aberrant, ie with depth that
2228 exceeds the topic by more than one."
2229
2230 ;; This is most clearly understood when considering standard-prefix-leader
2231 ;; low-level topics, which can all too easily match text not intended as
2232 ;; headers. For example, any line with a leading '.' or '*' and lacking a
2233 ;; following bullet qualifies without this protection. (A sequence of
2234 ;; them can occur naturally, eg a typical textual bullet list.) We
2235 ;; disqualify such low-level sequences when they are followed by a
2236 ;; discontinuously contained child, inferring that the sequences are not
2237 ;; actually connected with their prospective context.
2238
2239 (let ((depth (allout-depth))
2240 (start-point (point))
2241 done aberrant)
2242 (save-excursion
2243 (while (and (not done)
2244 (re-search-forward allout-line-boundary-regexp nil 0))
2245 (allout-prefix-data)
2246 (goto-char allout-recent-prefix-beginning)
2247 (cond
2248 ;; sibling - continue:
2249 ((eq allout-recent-depth depth))
2250 ;; first offspring is excessive - aberrant:
2251 ((> allout-recent-depth (1+ depth))
2252 (setq done t aberrant t))
2253 ;; next non-sibling is lower-depth - not aberrant:
2254 (t (setq done t)))))
2255 (if aberrant
2256 aberrant
2257 (goto-char start-point)
2258 ;; recalibrate allout-recent-*
2259 (allout-depth)
2260 nil)))
2261 ;;;_ : Location attributes
2262 ;;;_ > allout-depth ()
2263 (defun allout-depth ()
2264 "Return depth of topic most immediately containing point.
2265
2266 Return zero if point is not within any topic.
2267
2268 Like `allout-current-depth', but respects hidden as well as visible topics."
2269 (save-excursion
2270 (let ((start-point (point)))
2271 (if (and (allout-goto-prefix)
2272 (not (< start-point (point))))
2273 allout-recent-depth
2274 (progn
2275 ;; Oops, no prefix, nullify it:
2276 (nullify-allout-prefix-data)
2277 ;; ... and return 0:
2278 0)))))
2279 ;;;_ > allout-current-depth ()
2280 (defun allout-current-depth ()
2281 "Return depth of visible topic most immediately containing point.
2282
2283 Return zero if point is not within any topic."
2284 (save-excursion
2285 (if (allout-back-to-current-heading)
2286 (max 1
2287 (- allout-recent-prefix-end
2288 allout-recent-prefix-beginning
2289 allout-header-subtraction))
2290 0)))
2291 ;;;_ > allout-get-current-prefix ()
2292 (defun allout-get-current-prefix ()
2293 "Topic prefix of the current topic."
2294 (save-excursion
2295 (if (allout-goto-prefix)
2296 (allout-recent-prefix))))
2297 ;;;_ > allout-get-bullet ()
2298 (defun allout-get-bullet ()
2299 "Return bullet of containing topic (visible or not)."
2300 (save-excursion
2301 (and (allout-goto-prefix)
2302 (allout-recent-bullet))))
2303 ;;;_ > allout-current-bullet ()
2304 (defun allout-current-bullet ()
2305 "Return bullet of current (visible) topic heading, or none if none found."
2306 (condition-case nil
2307 (save-excursion
2308 (allout-back-to-current-heading)
2309 (buffer-substring-no-properties (- allout-recent-prefix-end 1)
2310 allout-recent-prefix-end))
2311 ;; Quick and dirty provision, ostensibly for missing bullet:
2312 (args-out-of-range nil))
2313 )
2314 ;;;_ > allout-get-prefix-bullet (prefix)
2315 (defun allout-get-prefix-bullet (prefix)
2316 "Return the bullet of the header prefix string PREFIX."
2317 ;; Doesn't make sense if we're old-style prefixes, but this just
2318 ;; oughtn't be called then, so forget about it...
2319 (if (string-match allout-regexp prefix)
2320 (substring prefix (1- (match-end 2)) (match-end 2))))
2321 ;;;_ > allout-sibling-index (&optional depth)
2322 (defun allout-sibling-index (&optional depth)
2323 "Item number of this prospective topic among its siblings.
2324
2325 If optional arg DEPTH is greater than current depth, then we're
2326 opening a new level, and return 0.
2327
2328 If less than this depth, ascend to that depth and count..."
2329
2330 (save-excursion
2331 (cond ((and depth (<= depth 0) 0))
2332 ((or (not depth) (= depth (allout-depth)))
2333 (let ((index 1))
2334 (while (allout-previous-sibling allout-recent-depth nil)
2335 (setq index (1+ index)))
2336 index))
2337 ((< depth allout-recent-depth)
2338 (allout-ascend-to-depth depth)
2339 (allout-sibling-index))
2340 (0))))
2341 ;;;_ > allout-topic-flat-index ()
2342 (defun allout-topic-flat-index ()
2343 "Return a list indicating point's numeric section.subsect.subsubsect...
2344 Outermost is first."
2345 (let* ((depth (allout-depth))
2346 (next-index (allout-sibling-index depth))
2347 (rev-sibls nil))
2348 (while (> next-index 0)
2349 (setq rev-sibls (cons next-index rev-sibls))
2350 (setq depth (1- depth))
2351 (setq next-index (allout-sibling-index depth)))
2352 rev-sibls)
2353 )
2354
2355 ;;;_ - Navigation routines
2356 ;;;_ > allout-beginning-of-current-line ()
2357 (defun allout-beginning-of-current-line ()
2358 "Like beginning of line, but to visible text."
2359
2360 ;; This combination of move-beginning-of-line and beginning-of-line is
2361 ;; deliberate, but the (beginning-of-line) may now be superfluous.
2362 (let ((inhibit-field-text-motion t))
2363 (move-beginning-of-line 1)
2364 (beginning-of-line)
2365 (while (and (not (bobp)) (or (not (bolp)) (allout-hidden-p)))
2366 (beginning-of-line)
2367 (if (or (allout-hidden-p) (not (bolp)))
2368 (forward-char -1)))))
2369 ;;;_ > allout-end-of-current-line ()
2370 (defun allout-end-of-current-line ()
2371 "Move to the end of line, past concealed text if any."
2372 ;; XXX This is for symmetry with `allout-beginning-of-current-line' -
2373 ;; `move-end-of-line' doesn't suffer the same problem as
2374 ;; `move-beginning-of-line'.
2375 (let ((inhibit-field-text-motion t))
2376 (end-of-line)
2377 (while (allout-hidden-p)
2378 (end-of-line)
2379 (if (allout-hidden-p) (forward-char 1)))))
2380 ;;;_ > allout-beginning-of-line ()
2381 (defun allout-beginning-of-line ()
2382 "Beginning-of-line with `allout-beginning-of-line-cycles' behavior, if set."
2383
2384 (interactive)
2385
2386 (if (or (not allout-beginning-of-line-cycles)
2387 (not (equal last-command this-command)))
2388 (move-beginning-of-line 1)
2389 (allout-depth)
2390 (let ((beginning-of-body
2391 (save-excursion
2392 (while (and (<= allout-recent-depth
2393 allout-doublecheck-at-and-shallower)
2394 (allout-aberrant-container-p)
2395 (allout-previous-visible-heading 1)))
2396 (allout-beginning-of-current-entry)
2397 (point))))
2398 (cond ((= (current-column) 0)
2399 (goto-char beginning-of-body))
2400 ((< (point) beginning-of-body)
2401 (allout-beginning-of-current-line))
2402 ((= (point) beginning-of-body)
2403 (goto-char (allout-current-bullet-pos)))
2404 (t (allout-beginning-of-current-line)
2405 (if (< (point) beginning-of-body)
2406 ;; we were on the headline after its start:
2407 (goto-char beginning-of-body)))))))
2408 ;;;_ > allout-end-of-line ()
2409 (defun allout-end-of-line ()
2410 "End-of-line with `allout-end-of-line-cycles' behavior, if set."
2411
2412 (interactive)
2413
2414 (if (or (not allout-end-of-line-cycles)
2415 (not (equal last-command this-command)))
2416 (allout-end-of-current-line)
2417 (let ((end-of-entry (save-excursion
2418 (allout-end-of-entry)
2419 (point))))
2420 (cond ((not (eolp))
2421 (allout-end-of-current-line))
2422 ((or (allout-hidden-p) (save-excursion
2423 (forward-char -1)
2424 (allout-hidden-p)))
2425 (allout-back-to-current-heading)
2426 (allout-show-current-entry)
2427 (allout-show-children)
2428 (allout-end-of-entry))
2429 ((>= (point) end-of-entry)
2430 (allout-back-to-current-heading)
2431 (allout-end-of-current-line))
2432 (t (allout-end-of-entry))))))
2433 ;;;_ > allout-next-heading ()
2434 (defsubst allout-next-heading ()
2435 "Move to the heading for the topic \(possibly invisible) after this one.
2436
2437 Returns the location of the heading, or nil if none found.
2438
2439 We skip anomolous low-level topics, a la `allout-aberrant-container-p'."
2440 (if (looking-at allout-regexp)
2441 (forward-char 1))
2442
2443 (when (re-search-forward allout-line-boundary-regexp nil 0)
2444 (allout-prefix-data)
2445 (and (<= allout-recent-depth allout-doublecheck-at-and-shallower)
2446 ;; this will set allout-recent-* on the first non-aberrant topic,
2447 ;; whether it's the current one or one that disqualifies it:
2448 (allout-aberrant-container-p))
2449 (goto-char allout-recent-prefix-beginning)))
2450 ;;;_ > allout-this-or-next-heading
2451 (defun allout-this-or-next-heading ()
2452 "Position cursor on current or next heading."
2453 ;; A throwaway non-macro that is defined after allout-next-heading
2454 ;; and usable by allout-mode.
2455 (if (not (allout-goto-prefix-doublechecked)) (allout-next-heading)))
2456 ;;;_ > allout-previous-heading ()
2457 (defun allout-previous-heading ()
2458 "Move to the prior \(possibly invisible) heading line.
2459
2460 Return the location of the beginning of the heading, or nil if not found.
2461
2462 We skip anomolous low-level topics, a la `allout-aberrant-container-p'."
2463
2464 (if (bobp)
2465 nil
2466 ;; allout-goto-prefix-doublechecked calls us, so we can't use it here.
2467 (let ((start-point (point)))
2468 (allout-goto-prefix)
2469 (when (or (re-search-backward allout-line-boundary-regexp nil 0)
2470 (looking-at allout-bob-regexp))
2471 (goto-char (allout-prefix-data))
2472 (if (and (<= allout-recent-depth allout-doublecheck-at-and-shallower)
2473 (allout-aberrant-container-p))
2474 (or (allout-previous-heading)
2475 (and (goto-char start-point)
2476 ;; recalibrate allout-recent-*:
2477 (allout-depth)
2478 nil))
2479 (point))))))
2480 ;;;_ > allout-get-invisibility-overlay ()
2481 (defun allout-get-invisibility-overlay ()
2482 "Return the overlay at point that dictates allout invisibility."
2483 (let ((overlays (overlays-at (point)))
2484 got)
2485 (while (and overlays (not got))
2486 (if (equal (overlay-get (car overlays) 'invisible) 'allout)
2487 (setq got (car overlays))
2488 (pop overlays)))
2489 got))
2490 ;;;_ > allout-back-to-visible-text ()
2491 (defun allout-back-to-visible-text ()
2492 "Move to most recent prior character that is visible, and return point."
2493 (if (allout-hidden-p)
2494 (goto-char (overlay-start (allout-get-invisibility-overlay))))
2495 (point))
2496
2497 ;;;_ - Subtree Charting
2498 ;;;_ " These routines either produce or assess charts, which are
2499 ;;; nested lists of the locations of topics within a subtree.
2500 ;;;
2501 ;;; Charts enable efficient subtree navigation by providing a reusable basis
2502 ;;; for elaborate, compound assessment and adjustment of a subtree.
2503
2504 ;;;_ > allout-chart-subtree (&optional levels visible orig-depth prev-depth)
2505 (defun allout-chart-subtree (&optional levels visible orig-depth prev-depth)
2506 "Produce a location \"chart\" of subtopics of the containing topic.
2507
2508 Optional argument LEVELS specifies the depth \(relative to start
2509 depth) for the chart.
2510
2511 When optional argument VISIBLE is non-nil, the chart includes
2512 only the visible subelements of the charted subjects.
2513
2514 The remaining optional args are not for internal use by the function.
2515
2516 Point is left at the end of the subtree.
2517
2518 Charts are used to capture outline structure, so that outline-altering
2519 routines need assess the structure only once, and then use the chart
2520 for their elaborate manipulations.
2521
2522 The chart entries for the topics are in reverse order, so the
2523 last topic is listed first. The entry for each topic consists of
2524 an integer indicating the point at the beginning of the topic
2525 prefix. Charts for offspring consists of a list containing,
2526 recursively, the charts for the respective subtopics. The chart
2527 for a topics' offspring precedes the entry for the topic itself.
2528
2529 The other function parameters are for internal recursion, and should
2530 not be specified by external callers. ORIG-DEPTH is depth of topic at
2531 starting point, and PREV-DEPTH is depth of prior topic."
2532
2533 (let ((original (not orig-depth)) ; `orig-depth' set only in recursion.
2534 chart curr-depth)
2535
2536 (if original ; Just starting?
2537 ; Register initial settings and
2538 ; position to first offspring:
2539 (progn (setq orig-depth (allout-depth))
2540 (or prev-depth (setq prev-depth (1+ orig-depth)))
2541 (if visible
2542 (allout-next-visible-heading 1)
2543 (allout-next-heading))))
2544
2545 ;; Loop over the current levels' siblings. Besides being more
2546 ;; efficient than tail-recursing over a level, it avoids exceeding
2547 ;; the typically quite constrained Emacs max-lisp-eval-depth.
2548 ;;
2549 ;; Probably would speed things up to implement loop-based stack
2550 ;; operation rather than recursing for lower levels. Bah.
2551
2552 (while (and (not (eobp))
2553 ; Still within original topic?
2554 (< orig-depth (setq curr-depth allout-recent-depth))
2555 (cond ((= prev-depth curr-depth)
2556 ;; Register this one and move on:
2557 (setq chart (cons allout-recent-prefix-beginning chart))
2558 (if (and levels (<= levels 1))
2559 ;; At depth limit - skip sublevels:
2560 (or (allout-next-sibling curr-depth)
2561 ;; or no more siblings - proceed to
2562 ;; next heading at lesser depth:
2563 (while (and (<= curr-depth
2564 allout-recent-depth)
2565 (if visible
2566 (allout-next-visible-heading 1)
2567 (allout-next-heading)))))
2568 (if visible
2569 (allout-next-visible-heading 1)
2570 (allout-next-heading))))
2571
2572 ((and (< prev-depth curr-depth)
2573 (or (not levels)
2574 (> levels 0)))
2575 ;; Recurse on deeper level of curr topic:
2576 (setq chart
2577 (cons (allout-chart-subtree (and levels
2578 (1- levels))
2579 visible
2580 orig-depth
2581 curr-depth)
2582 chart))
2583 ;; ... then continue with this one.
2584 )
2585
2586 ;; ... else nil if we've ascended back to prev-depth.
2587
2588 )))
2589
2590 (if original ; We're at the last sibling on
2591 ; the original level. Position
2592 ; to the end of it:
2593 (progn (and (not (eobp)) (forward-char -1))
2594 (and (= (preceding-char) ?\n)
2595 (= (aref (buffer-substring (max 1 (- (point) 3))
2596 (point))
2597 1)
2598 ?\n)
2599 (forward-char -1))
2600 (setq allout-recent-end-of-subtree (point))))
2601
2602 chart ; (nreverse chart) not necessary,
2603 ; and maybe not preferable.
2604 ))
2605 ;;;_ > allout-chart-siblings (&optional start end)
2606 (defun allout-chart-siblings (&optional start end)
2607 "Produce a list of locations of this and succeeding sibling topics.
2608 Effectively a top-level chart of siblings. See `allout-chart-subtree'
2609 for an explanation of charts."
2610 (save-excursion
2611 (when (allout-goto-prefix-doublechecked)
2612 (let ((chart (list (point))))
2613 (while (allout-next-sibling)
2614 (setq chart (cons (point) chart)))
2615 (if chart (setq chart (nreverse chart)))))))
2616 ;;;_ > allout-chart-to-reveal (chart depth)
2617 (defun allout-chart-to-reveal (chart depth)
2618
2619 "Return a flat list of hidden points in subtree CHART, up to DEPTH.
2620
2621 Note that point can be left at any of the points on chart, or at the
2622 start point."
2623
2624 (let (result here)
2625 (while (and (or (eq depth t) (> depth 0))
2626 chart)
2627 (setq here (car chart))
2628 (if (listp here)
2629 (let ((further (allout-chart-to-reveal here (or (eq depth t)
2630 (1- depth)))))
2631 ;; We're on the start of a subtree - recurse with it, if there's
2632 ;; more depth to go:
2633 (if further (setq result (append further result)))
2634 (setq chart (cdr chart)))
2635 (goto-char here)
2636 (if (allout-hidden-p)
2637 (setq result (cons here result)))
2638 (setq chart (cdr chart))))
2639 result))
2640 ;;;_ X allout-chart-spec (chart spec &optional exposing)
2641 ;; (defun allout-chart-spec (chart spec &optional exposing)
2642 ;; "Not yet \(if ever) implemented.
2643
2644 ;; Produce exposure directives given topic/subtree CHART and an exposure SPEC.
2645
2646 ;; Exposure spec indicates the locations to be exposed and the prescribed
2647 ;; exposure status. Optional arg EXPOSING is an integer, with 0
2648 ;; indicating pending concealment, anything higher indicating depth to
2649 ;; which subtopic headers should be exposed, and negative numbers
2650 ;; indicating (negative of) the depth to which subtopic headers and
2651 ;; bodies should be exposed.
2652
2653 ;; The produced list can have two types of entries. Bare numbers
2654 ;; indicate points in the buffer where topic headers that should be
2655 ;; exposed reside.
2656
2657 ;; - bare negative numbers indicates that the topic starting at the
2658 ;; point which is the negative of the number should be opened,
2659 ;; including their entries.
2660 ;; - bare positive values indicate that this topic header should be
2661 ;; opened.
2662 ;; - Lists signify the beginning and end points of regions that should
2663 ;; be flagged, and the flag to employ. (For concealment: `\(\?r\)', and
2664 ;; exposure:"
2665 ;; (while spec
2666 ;; (cond ((listp spec)
2667 ;; )
2668 ;; )
2669 ;; (setq spec (cdr spec)))
2670 ;; )
2671
2672 ;;;_ - Within Topic
2673 ;;;_ > allout-goto-prefix ()
2674 (defun allout-goto-prefix ()
2675 "Put point at beginning of immediately containing outline topic.
2676
2677 Goes to most immediate subsequent topic if none immediately containing.
2678
2679 Not sensitive to topic visibility.
2680
2681 Returns the point at the beginning of the prefix, or nil if none."
2682
2683 (let (done)
2684 (while (and (not done)
2685 (search-backward "\n" nil 1))
2686 (forward-char 1)
2687 (if (looking-at allout-regexp)
2688 (setq done (allout-prefix-data))
2689 (forward-char -1)))
2690 (if (bobp)
2691 (cond ((looking-at allout-regexp)
2692 (allout-prefix-data))
2693 ((allout-next-heading))
2694 (done))
2695 done)))
2696 ;;;_ > allout-goto-prefix-doublechecked ()
2697 (defun allout-goto-prefix-doublechecked ()
2698 "Put point at beginning of immediately containing outline topic.
2699
2700 Like `allout-goto-prefix', but shallow topics \(according to `allout-doublecheck-at-and-shallower') are checked and disqualified for child containment discontinuity, according to `allout-aberrant-container-p'."
2701 (allout-goto-prefix)
2702 (if (and (<= allout-recent-depth allout-doublecheck-at-and-shallower)
2703 (allout-aberrant-container-p))
2704 (allout-previous-heading)
2705 (point)))
2706
2707 ;;;_ > allout-end-of-prefix ()
2708 (defun allout-end-of-prefix (&optional ignore-decorations)
2709 "Position cursor at beginning of header text.
2710
2711 If optional IGNORE-DECORATIONS is non-nil, put just after bullet,
2712 otherwise skip white space between bullet and ensuing text."
2713
2714 (if (not (allout-goto-prefix-doublechecked))
2715 nil
2716 (goto-char allout-recent-prefix-end)
2717 (if ignore-decorations
2718 t
2719 (while (looking-at "[0-9]") (forward-char 1))
2720 (if (and (not (eolp)) (looking-at "\\s-")) (forward-char 1)))
2721 ;; Reestablish where we are:
2722 (allout-current-depth)))
2723 ;;;_ > allout-current-bullet-pos ()
2724 (defun allout-current-bullet-pos ()
2725 "Return position of current \(visible) topic's bullet."
2726
2727 (if (not (allout-current-depth))
2728 nil
2729 (1- allout-recent-prefix-end)))
2730 ;;;_ > allout-back-to-current-heading ()
2731 (defun allout-back-to-current-heading ()
2732 "Move to heading line of current topic, or beginning if already on the line.
2733
2734 Return value of point, unless we started outside of (before any) topics,
2735 in which case we return nil."
2736
2737 (allout-beginning-of-current-line)
2738 (if (or (allout-on-current-heading-p)
2739 (and (re-search-backward (concat "^\\(" allout-regexp "\\)")
2740 nil 'move)
2741 (progn (while (allout-hidden-p)
2742 (allout-beginning-of-current-line)
2743 (if (not (looking-at allout-regexp))
2744 (re-search-backward allout-regexp
2745 nil 'move)))
2746 (allout-prefix-data))))
2747 (if (interactive-p)
2748 (allout-end-of-prefix)
2749 (point))))
2750 ;;;_ > allout-back-to-heading ()
2751 (defalias 'allout-back-to-heading 'allout-back-to-current-heading)
2752 ;;;_ > allout-pre-next-prefix ()
2753 (defun allout-pre-next-prefix ()
2754 "Skip forward to just before the next heading line.
2755
2756 Returns that character position."
2757
2758 (if (allout-next-heading)
2759 (goto-char (1- allout-recent-prefix-beginning))))
2760 ;;;_ > allout-end-of-subtree (&optional current include-trailing-blank)
2761 (defun allout-end-of-subtree (&optional current include-trailing-blank)
2762 "Put point at the end of the last leaf in the containing topic.
2763
2764 Optional CURRENT means put point at the end of the containing
2765 visible topic.
2766
2767 Optional INCLUDE-TRAILING-BLANK means include a trailing blank line, if
2768 any, as part of the subtree. Otherwise, that trailing blank will be
2769 excluded as delimiting whitespace between topics.
2770
2771 Returns the value of point."
2772 (interactive "P")
2773 (if current
2774 (allout-back-to-current-heading)
2775 (allout-goto-prefix-doublechecked))
2776 (let ((level allout-recent-depth))
2777 (allout-next-heading)
2778 (while (and (not (eobp))
2779 (> allout-recent-depth level))
2780 (allout-next-heading))
2781 (if (eobp)
2782 (allout-end-of-entry)
2783 (forward-char -1))
2784 (if (and (not include-trailing-blank) (= ?\n (preceding-char)))
2785 (forward-char -1))
2786 (setq allout-recent-end-of-subtree (point))))
2787 ;;;_ > allout-end-of-current-subtree (&optional include-trailing-blank)
2788 (defun allout-end-of-current-subtree (&optional include-trailing-blank)
2789
2790 "Put point at end of last leaf in currently visible containing topic.
2791
2792 Optional INCLUDE-TRAILING-BLANK means include a trailing blank line, if
2793 any, as part of the subtree. Otherwise, that trailing blank will be
2794 excluded as delimiting whitespace between topics.
2795
2796 Returns the value of point."
2797 (interactive)
2798 (allout-end-of-subtree t include-trailing-blank))
2799 ;;;_ > allout-beginning-of-current-entry ()
2800 (defun allout-beginning-of-current-entry ()
2801 "When not already there, position point at beginning of current topic header.
2802
2803 If already there, move cursor to bullet for hot-spot operation.
2804 \(See `allout-mode' doc string for details of hot-spot operation.)"
2805 (interactive)
2806 (let ((start-point (point)))
2807 (move-beginning-of-line 1)
2808 (if (< 0 (allout-current-depth))
2809 (goto-char allout-recent-prefix-end)
2810 (goto-char (point-min)))
2811 (allout-end-of-prefix)
2812 (if (and (interactive-p)
2813 (= (point) start-point))
2814 (goto-char (allout-current-bullet-pos)))))
2815 ;;;_ > allout-end-of-entry (&optional inclusive)
2816 (defun allout-end-of-entry (&optional inclusive)
2817 "Position the point at the end of the current topics' entry.
2818
2819 Optional INCLUSIVE means also include trailing empty line, if any. When
2820 unset, whitespace between items separates them even when the items are
2821 collapsed."
2822 (interactive)
2823 (allout-pre-next-prefix)
2824 (if (and (not inclusive) (not (bobp)) (= ?\n (preceding-char)))
2825 (forward-char -1))
2826 (point))
2827 ;;;_ > allout-end-of-current-heading ()
2828 (defun allout-end-of-current-heading ()
2829 (interactive)
2830 (allout-beginning-of-current-entry)
2831 (search-forward "\n" nil t)
2832 (forward-char -1))
2833 (defalias 'allout-end-of-heading 'allout-end-of-current-heading)
2834 ;;;_ > allout-get-body-text ()
2835 (defun allout-get-body-text ()
2836 "Return the unmangled body text of the topic immediately containing point."
2837 (save-excursion
2838 (allout-end-of-prefix)
2839 (if (not (search-forward "\n" nil t))
2840 nil
2841 (backward-char 1)
2842 (let ((pre-body (point)))
2843 (if (not pre-body)
2844 nil
2845 (allout-end-of-entry t)
2846 (if (not (= pre-body (point)))
2847 (buffer-substring-no-properties (1+ pre-body) (point))))
2848 )
2849 )
2850 )
2851 )
2852
2853 ;;;_ - Depth-wise
2854 ;;;_ > allout-ascend-to-depth (depth)
2855 (defun allout-ascend-to-depth (depth)
2856 "Ascend to depth DEPTH, returning depth if successful, nil if not."
2857 (if (and (> depth 0)(<= depth (allout-depth)))
2858 (let (last-ascended)
2859 (while (and (< depth allout-recent-depth)
2860 (setq last-ascended (allout-ascend))))
2861 (goto-char allout-recent-prefix-beginning)
2862 (if (interactive-p) (allout-end-of-prefix))
2863 (and last-ascended allout-recent-depth))))
2864 ;;;_ > allout-ascend ()
2865 (defun allout-ascend ()
2866 "Ascend one level, returning t if successful, nil if not."
2867 (prog1
2868 (if (allout-beginning-of-level)
2869 (allout-previous-heading))
2870 (if (interactive-p) (allout-end-of-prefix))))
2871 ;;;_ > allout-descend-to-depth (depth)
2872 (defun allout-descend-to-depth (depth)
2873 "Descend to depth DEPTH within current topic.
2874
2875 Returning depth if successful, nil if not."
2876 (let ((start-point (point))
2877 (start-depth (allout-depth)))
2878 (while
2879 (and (> (allout-depth) 0)
2880 (not (= depth allout-recent-depth)) ; ... not there yet
2881 (allout-next-heading) ; ... go further
2882 (< start-depth allout-recent-depth))) ; ... still in topic
2883 (if (and (> (allout-depth) 0)
2884 (= allout-recent-depth depth))
2885 depth
2886 (goto-char start-point)
2887 nil))
2888 )
2889 ;;;_ > allout-up-current-level (arg)
2890 (defun allout-up-current-level (arg)
2891 "Move out ARG levels from current visible topic."
2892 (interactive "p")
2893 (let ((start-point (point)))
2894 (allout-back-to-current-heading)
2895 (if (not (allout-ascend))
2896 (progn (goto-char start-point)
2897 (error "Can't ascend past outermost level"))
2898 (if (interactive-p) (allout-end-of-prefix))
2899 allout-recent-prefix-beginning)))
2900
2901 ;;;_ - Linear
2902 ;;;_ > allout-next-sibling (&optional depth backward)
2903 (defun allout-next-sibling (&optional depth backward)
2904 "Like `allout-forward-current-level', but respects invisible topics.
2905
2906 Traverse at optional DEPTH, or current depth if none specified.
2907
2908 Go backward if optional arg BACKWARD is non-nil.
2909
2910 Return the start point of the new topic if successful, nil otherwise."
2911
2912 (if (if backward (bobp) (eobp))
2913 nil
2914 (let ((target-depth (or depth (allout-depth)))
2915 (start-point (point))
2916 (count 0)
2917 leaping
2918 last-depth)
2919 (while (and
2920 ;; done too few single steps to resort to the leap routine:
2921 (not leaping)
2922 ;; not at limit:
2923 (not (if backward (bobp) (eobp)))
2924 ;; still traversable:
2925 (if backward (allout-previous-heading) (allout-next-heading))
2926 ;; we're below the target depth
2927 (> (setq last-depth allout-recent-depth) target-depth))
2928 (setq count (1+ count))
2929 (if (> count 7) ; lists are commonly 7 +- 2, right?-)
2930 (setq leaping t)))
2931 (cond (leaping
2932 (or (allout-next-sibling-leap target-depth backward)
2933 (progn
2934 (goto-char start-point)
2935 (if depth (allout-depth) target-depth)
2936 nil)))
2937 ((and (not (eobp))
2938 (and (> (or last-depth (allout-depth)) 0)
2939 (= allout-recent-depth target-depth)))
2940 allout-recent-prefix-beginning)
2941 (t
2942 (goto-char start-point)
2943 (if depth (allout-depth) target-depth)
2944 nil)))))
2945 ;;;_ > allout-next-sibling-leap (&optional depth backward)
2946 (defun allout-next-sibling-leap (&optional depth backward)
2947 "Like `allout-next-sibling', but by direct search for topic at depth.
2948
2949 Traverse at optional DEPTH, or current depth if none specified.
2950
2951 Go backward if optional arg BACKWARD is non-nil.
2952
2953 Return the start point of the new topic if successful, nil otherwise.
2954
2955 Costs more than regular `allout-next-sibling' for short traversals:
2956
2957 - we have to check the prior \(next, if travelling backwards)
2958 item to confirm connectivity with the prior topic, and
2959 - if confirmed, we have to reestablish the allout-recent-* settings with
2960 some extra navigation
2961 - if confirmation fails, we have to do more work to recover
2962
2963 It is an increasingly big win when there are many intervening
2964 offspring before the next sibling, however, so
2965 `allout-next-sibling' resorts to this if it finds itself in that
2966 situation."
2967
2968 (if (if backward (bobp) (eobp))
2969 nil
2970 (let* ((start-point (point))
2971 (target-depth (or depth (allout-depth)))
2972 (search-whitespace-regexp nil)
2973 (depth-biased (- target-depth 2))
2974 (expression (if (<= target-depth 1)
2975 allout-depth-one-regexp
2976 (format allout-depth-specific-regexp
2977 depth-biased depth-biased)))
2978 found
2979 done)
2980 (while (not done)
2981 (setq found (if backward
2982 (re-search-backward expression nil 'to-limit)
2983 (forward-char 1)
2984 (re-search-forward expression nil 'to-limit)))
2985 (if (and found (allout-aberrant-container-p))
2986 (setq found nil))
2987 (setq done (or found (if backward (bobp) (eobp)))))
2988 (if (not found)
2989 (progn (goto-char start-point)
2990 nil)
2991 ;; rationale: if any intervening items were at a lower depth, we
2992 ;; would now be on the first offspring at the target depth - ie,
2993 ;; the preceeding item (per the search direction) must be at a
2994 ;; lesser depth. that's all we need to check.
2995 (if backward (allout-next-heading) (allout-previous-heading))
2996 (if (< allout-recent-depth target-depth)
2997 ;; return to start and reestablish allout-recent-*:
2998 (progn
2999 (goto-char start-point)
3000 (allout-depth)
3001 nil)
3002 (goto-char found)
3003 ;; locate cursor and set allout-recent-*:
3004 (allout-goto-prefix))))))
3005 ;;;_ > allout-previous-sibling (&optional depth backward)
3006 (defun allout-previous-sibling (&optional depth backward)
3007 "Like `allout-forward-current-level' backwards, respecting invisible topics.
3008
3009 Optional DEPTH specifies depth to traverse, default current depth.
3010
3011 Optional BACKWARD reverses direction.
3012
3013 Return depth if successful, nil otherwise."
3014 (allout-next-sibling depth (not backward))
3015 )
3016 ;;;_ > allout-snug-back ()
3017 (defun allout-snug-back ()
3018 "Position cursor at end of previous topic.
3019
3020 Presumes point is at the start of a topic prefix."
3021 (if (or (bobp) (eobp))
3022 nil
3023 (forward-char -1))
3024 (if (or (bobp) (not (= ?\n (preceding-char))))
3025 nil
3026 (forward-char -1))
3027 (point))
3028 ;;;_ > allout-beginning-of-level ()
3029 (defun allout-beginning-of-level ()
3030 "Go back to the first sibling at this level, visible or not."
3031 (allout-end-of-level 'backward))
3032 ;;;_ > allout-end-of-level (&optional backward)
3033 (defun allout-end-of-level (&optional backward)
3034 "Go to the last sibling at this level, visible or not."
3035
3036 (let ((depth (allout-depth)))
3037 (while (allout-previous-sibling depth nil))
3038 (prog1 allout-recent-depth
3039 (if (interactive-p) (allout-end-of-prefix)))))
3040 ;;;_ > allout-next-visible-heading (arg)
3041 (defun allout-next-visible-heading (arg)
3042 "Move to the next ARG'th visible heading line, backward if arg is negative.
3043
3044 Move to buffer limit in indicated direction if headings are exhausted."
3045
3046 (interactive "p")
3047 (let* ((inhibit-field-text-motion t)
3048 (backward (if (< arg 0) (setq arg (* -1 arg))))
3049 (step (if backward -1 1))
3050 prev got)
3051
3052 (while (> arg 0)
3053 (while (and
3054 ;; Boundary condition:
3055 (not (if backward (bobp)(eobp)))
3056 ;; Move, skipping over all concealed lines in one fell swoop:
3057 (prog1 (condition-case nil (or (line-move step) t)
3058 (error nil))
3059 (allout-beginning-of-current-line))
3060 ;; Deal with apparent header line:
3061 (if (not (looking-at allout-regexp))
3062 ;; not a header line, keep looking:
3063 t
3064 (allout-prefix-data)
3065 (if (and (<= allout-recent-depth
3066 allout-doublecheck-at-and-shallower)
3067 (allout-aberrant-container-p))
3068 ;; skip this aberrant prospective header line:
3069 t
3070 ;; this prospective headerline qualifies - register:
3071 (setq got allout-recent-prefix-beginning)
3072 ;; and break the loop:
3073 nil))))
3074 ;; Register this got, it may be the last:
3075 (if got (setq prev got))
3076 (setq arg (1- arg)))
3077 (cond (got ; Last move was to a prefix:
3078 (allout-end-of-prefix))
3079 (prev ; Last move wasn't, but prev was:
3080 (goto-char prev)
3081 (allout-end-of-prefix))
3082 ((not backward) (end-of-line) nil))))
3083 ;;;_ > allout-previous-visible-heading (arg)
3084 (defun allout-previous-visible-heading (arg)
3085 "Move to the previous heading line.
3086
3087 With argument, repeats or can move forward if negative.
3088 A heading line is one that starts with a `*' (or that `allout-regexp'
3089 matches)."
3090 (interactive "p")
3091 (prog1 (allout-next-visible-heading (- arg))
3092 (if (interactive-p) (allout-end-of-prefix))))
3093 ;;;_ > allout-forward-current-level (arg)
3094 (defun allout-forward-current-level (arg)
3095 "Position point at the next heading of the same level.
3096
3097 Takes optional repeat-count, goes backward if count is negative.
3098
3099 Returns resulting position, else nil if none found."
3100 (interactive "p")
3101 (let ((start-depth (allout-current-depth))
3102 (start-arg arg)
3103 (backward (> 0 arg)))
3104 (if (= 0 start-depth)
3105 (error "No siblings, not in a topic..."))
3106 (if backward (setq arg (* -1 arg)))
3107 (allout-back-to-current-heading)
3108 (while (and (not (zerop arg))
3109 (if backward
3110 (allout-previous-sibling)
3111 (allout-next-sibling)))
3112 (setq arg (1- arg)))
3113 (if (not (interactive-p))
3114 nil
3115 (allout-end-of-prefix)
3116 (if (not (zerop arg))
3117 (error "Hit %s level %d topic, traversed %d of %d requested"
3118 (if backward "first" "last")
3119 allout-recent-depth
3120 (- (abs start-arg) arg)
3121 (abs start-arg))))))
3122 ;;;_ > allout-backward-current-level (arg)
3123 (defun allout-backward-current-level (arg)
3124 "Inverse of `allout-forward-current-level'."
3125 (interactive "p")
3126 (if (interactive-p)
3127 (let ((current-prefix-arg (* -1 arg)))
3128 (call-interactively 'allout-forward-current-level))
3129 (allout-forward-current-level (* -1 arg))))
3130
3131 ;;;_ #5 Alteration
3132
3133 ;;;_ - Fundamental
3134 ;;;_ = allout-post-goto-bullet
3135 (defvar allout-post-goto-bullet nil
3136 "Outline internal var, for `allout-pre-command-business' hot-spot operation.
3137
3138 When set, tells post-processing to reposition on topic bullet, and
3139 then unset it. Set by `allout-pre-command-business' when implementing
3140 hot-spot operation, where literal characters typed over a topic bullet
3141 are mapped to the command of the corresponding control-key on the
3142 `allout-mode-map'.")
3143 (make-variable-buffer-local 'allout-post-goto-bullet)
3144 ;;;_ = allout-command-counter
3145 (defvar allout-command-counter 0
3146 "Counter that monotonically increases in allout-mode buffers.
3147
3148 Set by `allout-pre-command-business', to support allout addons in
3149 coordinating with allout activity.")
3150 (make-variable-buffer-local 'allout-command-counter)
3151 ;;;_ > allout-post-command-business ()
3152 (defun allout-post-command-business ()
3153 "Outline `post-command-hook' function.
3154
3155 - Implement (and clear) `allout-post-goto-bullet', for hot-spot
3156 outline commands.
3157
3158 - Decrypt topic currently being edited if it was encrypted for a save."
3159
3160 ; Apply any external change func:
3161 (if (not (allout-mode-p)) ; In allout-mode.
3162 nil
3163
3164 (if (and (boundp 'allout-after-save-decrypt)
3165 allout-after-save-decrypt)
3166 (allout-after-saves-handler))
3167
3168 ;; Implement allout-post-goto-bullet, if set:
3169 (if (and allout-post-goto-bullet
3170 (allout-current-bullet-pos))
3171 (progn (goto-char (allout-current-bullet-pos))
3172 (setq allout-post-goto-bullet nil)))
3173 ))
3174 ;;;_ > allout-pre-command-business ()
3175 (defun allout-pre-command-business ()
3176 "Outline `pre-command-hook' function for outline buffers.
3177
3178 Among other things, implements special behavior when the cursor is on the
3179 topic bullet character.
3180
3181 When the cursor is on the bullet character, self-insert characters are
3182 reinterpreted as the corresponding control-character in the
3183 `allout-mode-map'. The `allout-mode' `post-command-hook' insures that
3184 the cursor which has moved as a result of such reinterpretation is
3185 positioned on the bullet character of the destination topic.
3186
3187 The upshot is that you can get easy, single \(ie, unmodified\) key
3188 outline maneuvering operations by positioning the cursor on the bullet
3189 char. When in this mode you can use regular cursor-positioning
3190 command/keystrokes to relocate the cursor off of a bullet character to
3191 return to regular interpretation of self-insert characters."
3192
3193 (if (not (allout-mode-p))
3194 nil
3195 ;; Increment allout-command-counter
3196 (setq allout-command-counter (1+ allout-command-counter))
3197 ;; Do hot-spot navigation.
3198 (if (and (eq this-command 'self-insert-command)
3199 (eq (point)(allout-current-bullet-pos)))
3200 (allout-hotspot-key-handler))))
3201 ;;;_ > allout-hotspot-key-handler ()
3202 (defun allout-hotspot-key-handler ()
3203 "Catchall handling of key bindings in hot-spots.
3204
3205 Translates unmodified keystrokes to corresponding allout commands, when
3206 they would qualify if prefixed with the allout-command-prefix, and sets
3207 this-command accordingly.
3208
3209 Returns the qualifying command, if any, else nil."
3210 (interactive)
3211 (let* ((key-string (if (numberp last-command-char)
3212 (char-to-string last-command-char)))
3213 (key-num (cond ((numberp last-command-char) last-command-char)
3214 ;; for XEmacs character type:
3215 ((and (fboundp 'characterp)
3216 (apply 'characterp (list last-command-char)))
3217 (apply 'char-to-int (list last-command-char)))
3218 (t 0)))
3219 mapped-binding)
3220
3221 (if (zerop key-num)
3222 nil
3223
3224 (if (and
3225 ;; exclude control chars and escape:
3226 (<= 33 key-num)
3227 (setq mapped-binding
3228 (or (and (assoc key-string allout-keybindings-list)
3229 ;; translate literal membership on list:
3230 (cadr (assoc key-string allout-keybindings-list)))
3231 ;; translate as a keybinding:
3232 (key-binding (concat allout-command-prefix
3233 (char-to-string
3234 (if (and (<= 97 key-num) ; "a"
3235 (>= 122 key-num)) ; "z"
3236 (- key-num 96) key-num)))
3237 t))))
3238 ;; Qualified as an allout command - do hot-spot operation.
3239 (setq allout-post-goto-bullet t)
3240 ;; accept-defaults nil, or else we'll get allout-item-icon-key-handler.
3241 (setq mapped-binding (key-binding (char-to-string key-num))))
3242
3243 (while (keymapp mapped-binding)
3244 (setq mapped-binding
3245 (lookup-key mapped-binding (vector (read-char)))))
3246
3247 (if mapped-binding
3248 (setq this-command mapped-binding)))))
3249
3250 ;;;_ > allout-find-file-hook ()
3251 (defun allout-find-file-hook ()
3252 "Activate `allout-mode' on non-nil `allout-auto-activation', `allout-layout'.
3253
3254 See `allout-init' for setup instructions."
3255 (if (and allout-auto-activation
3256 (not (allout-mode-p))
3257 allout-layout)
3258 (allout-mode t)))
3259
3260 ;;;_ - Topic Format Assessment
3261 ;;;_ > allout-solicit-alternate-bullet (depth &optional current-bullet)
3262 (defun allout-solicit-alternate-bullet (depth &optional current-bullet)
3263
3264 "Prompt for and return a bullet char as an alternative to the current one.
3265
3266 Offer one suitable for current depth DEPTH as default."
3267
3268 (let* ((default-bullet (or (and (stringp current-bullet) current-bullet)
3269 (allout-bullet-for-depth depth)))
3270 (sans-escapes (regexp-sans-escapes allout-bullets-string))
3271 choice)
3272 (save-excursion
3273 (goto-char (allout-current-bullet-pos))
3274 (setq choice (solicit-char-in-string
3275 (format "Select bullet: %s ('%s' default): "
3276 sans-escapes
3277 (substring-no-properties default-bullet))
3278 sans-escapes
3279 t)))
3280 (message "")
3281 (if (string= choice "") default-bullet choice))
3282 )
3283 ;;;_ > allout-distinctive-bullet (bullet)
3284 (defun allout-distinctive-bullet (bullet)
3285 "True if BULLET is one of those on `allout-distinctive-bullets-string'."
3286 (string-match (regexp-quote bullet) allout-distinctive-bullets-string))
3287 ;;;_ > allout-numbered-type-prefix (&optional prefix)
3288 (defun allout-numbered-type-prefix (&optional prefix)
3289 "True if current header prefix bullet is numbered bullet."
3290 (and allout-numbered-bullet
3291 (string= allout-numbered-bullet
3292 (if prefix
3293 (allout-get-prefix-bullet prefix)
3294 (allout-get-bullet)))))
3295 ;;;_ > allout-encrypted-type-prefix (&optional prefix)
3296 (defun allout-encrypted-type-prefix (&optional prefix)
3297 "True if current header prefix bullet is for an encrypted entry \(body)."
3298 (and allout-topic-encryption-bullet
3299 (string= allout-topic-encryption-bullet
3300 (if prefix
3301 (allout-get-prefix-bullet prefix)
3302 (allout-get-bullet)))))
3303 ;;;_ > allout-bullet-for-depth (&optional depth)
3304 (defun allout-bullet-for-depth (&optional depth)
3305 "Return outline topic bullet suited to optional DEPTH, or current depth."
3306 ;; Find bullet in plain-bullets-string modulo DEPTH.
3307 (if allout-stylish-prefixes
3308 (char-to-string (aref allout-plain-bullets-string
3309 (% (max 0 (- depth 2))
3310 allout-plain-bullets-string-len)))
3311 allout-primary-bullet)
3312 )
3313
3314 ;;;_ - Topic Production
3315 ;;;_ > allout-make-topic-prefix (&optional prior-bullet
3316 (defun allout-make-topic-prefix (&optional prior-bullet
3317 new
3318 depth
3319 solicit
3320 number-control
3321 index)
3322 ;; Depth null means use current depth, non-null means we're either
3323 ;; opening a new topic after current topic, lower or higher, or we're
3324 ;; changing level of current topic.
3325 ;; Solicit dominates specified bullet-char.
3326 ;;;_ . Doc string:
3327 "Generate a topic prefix suitable for optional arg DEPTH, or current depth.
3328
3329 All the arguments are optional.
3330
3331 PRIOR-BULLET indicates the bullet of the prefix being changed, or
3332 nil if none. This bullet may be preserved (other options
3333 notwithstanding) if it is on the `allout-distinctive-bullets-string',
3334 for instance.
3335
3336 Second arg NEW indicates that a new topic is being opened after the
3337 topic at point, if non-nil. Default bullet for new topics, eg, may
3338 be set (contingent to other args) to numbered bullets if previous
3339 sibling is one. The implication otherwise is that the current topic
3340 is being adjusted - shifted or rebulleted - and we don't consider
3341 bullet or previous sibling.
3342
3343 Third arg DEPTH forces the topic prefix to that depth, regardless of
3344 the current topics' depth.
3345
3346 If SOLICIT is non-nil, then the choice of bullet is solicited from
3347 user. If it's a character, then that character is offered as the
3348 default, otherwise the one suited to the context \(according to
3349 distinction or depth) is offered. \(This overrides other options,
3350 including, eg, a distinctive PRIOR-BULLET.) If non-nil, then the
3351 context-specific bullet is used.
3352
3353 Fifth arg, NUMBER-CONTROL, matters only if `allout-numbered-bullet'
3354 is non-nil *and* soliciting was not explicitly invoked. Then
3355 NUMBER-CONTROL non-nil forces prefix to either numbered or
3356 denumbered format, depending on the value of the sixth arg, INDEX.
3357
3358 \(Note that NUMBER-CONTROL does *not* apply to level 1 topics. Sorry...)
3359
3360 If NUMBER-CONTROL is non-nil and sixth arg INDEX is non-nil then
3361 the prefix of the topic is forced to be numbered. Non-nil
3362 NUMBER-CONTROL and nil INDEX forces non-numbered format on the
3363 bullet. Non-nil NUMBER-CONTROL and non-nil, non-number INDEX means
3364 that the index for the numbered prefix will be derived, by counting
3365 siblings back to start of level. If INDEX is a number, then that
3366 number is used as the index for the numbered prefix (allowing, eg,
3367 sequential renumbering to not require this function counting back the
3368 index for each successive sibling)."
3369 ;;;_ . Code:
3370 ;; The options are ordered in likely frequence of use, most common
3371 ;; highest, least lowest. Ie, more likely to be doing prefix
3372 ;; adjustments than soliciting, and yet more than numbering.
3373 ;; Current prefix is least dominant, but most likely to be commonly
3374 ;; specified...
3375
3376 (let* (body
3377 numbering
3378 denumbering
3379 (depth (or depth (allout-depth)))
3380 (header-lead allout-header-prefix)
3381 (bullet-char
3382
3383 ;; Getting value for bullet char is practically the whole job:
3384
3385 (cond
3386 ; Simplest situation - level 1:
3387 ((<= depth 1) (setq header-lead "") allout-primary-bullet)
3388 ; Simple, too: all asterisks:
3389 (allout-old-style-prefixes
3390 ;; Cheat - make body the whole thing, null out header-lead and
3391 ;; bullet-char:
3392 (setq body (make-string depth
3393 (string-to-char allout-primary-bullet)))
3394 (setq header-lead "")
3395 "")
3396
3397 ;; (Neither level 1 nor old-style, so we're space padding.
3398 ;; Sneak it in the condition of the next case, whatever it is.)
3399
3400 ;; Solicitation overrides numbering and other cases:
3401 ((progn (setq body (make-string (- depth 2) ?\ ))
3402 ;; The actual condition:
3403 solicit)
3404 (let* ((got (allout-solicit-alternate-bullet depth solicit)))
3405 ;; Gotta check whether we're numbering and got a numbered bullet:
3406 (setq numbering (and allout-numbered-bullet
3407 (not (and number-control (not index)))
3408 (string= got allout-numbered-bullet)))
3409 ;; Now return what we got, regardless:
3410 got))
3411
3412 ;; Numbering invoked through args:
3413 ((and allout-numbered-bullet number-control)
3414 (if (setq numbering (not (setq denumbering (not index))))
3415 allout-numbered-bullet
3416 (if (and prior-bullet
3417 (not (string= allout-numbered-bullet
3418 prior-bullet)))
3419 prior-bullet
3420 (allout-bullet-for-depth depth))))
3421
3422 ;;; Neither soliciting nor controlled numbering ;;;
3423 ;;; (may be controlled denumbering, tho) ;;;
3424
3425 ;; Check wrt previous sibling:
3426 ((and new ; only check for new prefixes
3427 (<= depth (allout-depth))
3428 allout-numbered-bullet ; ... & numbering enabled
3429 (not denumbering)
3430 (let ((sibling-bullet
3431 (save-excursion
3432 ;; Locate correct sibling:
3433 (or (>= depth (allout-depth))
3434 (allout-ascend-to-depth depth))
3435 (allout-get-bullet))))
3436 (if (and sibling-bullet
3437 (string= allout-numbered-bullet sibling-bullet))
3438 (setq numbering sibling-bullet)))))
3439
3440 ;; Distinctive prior bullet?
3441 ((and prior-bullet
3442 (allout-distinctive-bullet prior-bullet)
3443 ;; Either non-numbered:
3444 (or (not (and allout-numbered-bullet
3445 (string= prior-bullet allout-numbered-bullet)))
3446 ;; or numbered, and not denumbering:
3447 (setq numbering (not denumbering)))
3448 ;; Here 'tis:
3449 prior-bullet))
3450
3451 ;; Else, standard bullet per depth:
3452 ((allout-bullet-for-depth depth)))))
3453
3454 (concat header-lead
3455 body
3456 bullet-char
3457 (if numbering
3458 (format "%d" (cond ((and index (numberp index)) index)
3459 (new (1+ (allout-sibling-index depth)))
3460 ((allout-sibling-index))))))
3461 )
3462 )
3463 ;;;_ > allout-open-topic (relative-depth &optional before offer-recent-bullet)
3464 (defun allout-open-topic (relative-depth &optional before offer-recent-bullet)
3465 "Open a new topic at depth DEPTH.
3466
3467 New topic is situated after current one, unless optional flag BEFORE
3468 is non-nil, or unless current line is completely empty - lacking even
3469 whitespace - in which case open is done on the current line.
3470
3471 When adding an offspring, it will be added immediately after the parent if
3472 the other offspring are exposed, or after the last child if the offspring
3473 are hidden. \(The intervening offspring will be exposed in the latter
3474 case.)
3475
3476 If OFFER-RECENT-BULLET is true, offer to use the bullet of the prior sibling.
3477
3478 Runs
3479
3480 Nuances:
3481
3482 - Creation of new topics is with respect to the visible topic
3483 containing the cursor, regardless of intervening concealed ones.
3484
3485 - New headers are generally created after/before the body of a
3486 topic. However, they are created right at cursor location if the
3487 cursor is on a blank line, even if that breaks the current topic
3488 body. This is intentional, to provide a simple means for
3489 deliberately dividing topic bodies.
3490
3491 - Double spacing of topic lists is preserved. Also, the first
3492 level two topic is created double-spaced (and so would be
3493 subsequent siblings, if that's left intact). Otherwise,
3494 single-spacing is used.
3495
3496 - Creation of sibling or nested topics is with respect to the topic
3497 you're starting from, even when creating backwards. This way you
3498 can easily create a sibling in front of the current topic without
3499 having to go to its preceding sibling, and then open forward
3500 from there."
3501
3502 (allout-beginning-of-current-line)
3503 (let* ((inhibit-field-text-motion t)
3504 (depth (+ (allout-current-depth) relative-depth))
3505 (opening-on-blank (if (looking-at "^\$")
3506 (not (setq before nil))))
3507 ;; bunch o vars set while computing ref-topic
3508 opening-numbered
3509 ref-depth
3510 ref-bullet
3511 (ref-topic (save-excursion
3512 (cond ((< relative-depth 0)
3513 (allout-ascend-to-depth depth))
3514 ((>= relative-depth 1) nil)
3515 (t (allout-back-to-current-heading)))
3516 (setq ref-depth allout-recent-depth)
3517 (setq ref-bullet
3518 (if (> allout-recent-prefix-end 1)
3519 (allout-recent-bullet)
3520 ""))
3521 (setq opening-numbered
3522 (save-excursion
3523 (and allout-numbered-bullet
3524 (or (<= relative-depth 0)
3525 (allout-descend-to-depth depth))
3526 (if (allout-numbered-type-prefix)
3527 allout-numbered-bullet))))
3528 (point)))
3529 dbl-space
3530 doing-beginning
3531 start end)
3532
3533 (if (not opening-on-blank)
3534 ; Positioning and vertical
3535 ; padding - only if not
3536 ; opening-on-blank:
3537 (progn
3538 (goto-char ref-topic)
3539 (setq dbl-space ; Determine double space action:
3540 (or (and (<= relative-depth 0) ; not descending;
3541 (save-excursion
3542 ;; at b-o-b or preceded by a blank line?
3543 (or (> 0 (forward-line -1))
3544 (looking-at "^\\s-*$")
3545 (bobp)))
3546 (save-excursion
3547 ;; succeeded by a blank line?
3548 (allout-end-of-current-subtree)
3549 (looking-at "\n\n")))
3550 (and (= ref-depth 1)
3551 (or before
3552 (= depth 1)
3553 (save-excursion
3554 ;; Don't already have following
3555 ;; vertical padding:
3556 (not (allout-pre-next-prefix)))))))
3557
3558 ;; Position to prior heading, if inserting backwards, and not
3559 ;; going outwards:
3560 (if (and before (>= relative-depth 0))
3561 (progn (allout-back-to-current-heading)
3562 (setq doing-beginning (bobp))
3563 (if (not (bobp))
3564 (allout-previous-heading)))
3565 (if (and before (bobp))
3566 (open-line 1)))
3567
3568 (if (<= relative-depth 0)
3569 ;; Not going inwards, don't snug up:
3570 (if doing-beginning
3571 (if (not dbl-space)
3572 (open-line 1)
3573 (open-line 2))
3574 (if before
3575 (progn (end-of-line)
3576 (allout-pre-next-prefix)
3577 (while (and (= ?\n (following-char))
3578 (save-excursion
3579 (forward-char 1)
3580 (allout-hidden-p)))
3581 (forward-char 1))
3582 (if (not (looking-at "^$"))
3583 (open-line 1)))
3584 (allout-end-of-current-subtree)
3585 (if (looking-at "\n\n") (forward-char 1))))
3586 ;; Going inwards - double-space if first offspring is
3587 ;; double-spaced, otherwise snug up.
3588 (allout-end-of-entry)
3589 (if (eobp)
3590 (newline 1)
3591 (line-move 1))
3592 (allout-beginning-of-current-line)
3593 (backward-char 1)
3594 (if (bolp)
3595 ;; Blank lines between current header body and next
3596 ;; header - get to last substantive (non-white-space)
3597 ;; line in body:
3598 (progn (setq dbl-space t)
3599 (re-search-backward "[^ \t\n]" nil t)))
3600 (if (looking-at "\n\n")
3601 (setq dbl-space t))
3602 (if (save-excursion
3603 (allout-next-heading)
3604 (when (> allout-recent-depth ref-depth)
3605 ;; This is an offspring.
3606 (forward-line -1)
3607 (looking-at "^\\s-*$")))
3608 (progn (forward-line 1)
3609 (open-line 1)
3610 (forward-line 1)))
3611 (allout-end-of-current-line))
3612
3613 ;;(if doing-beginning (goto-char doing-beginning))
3614 (if (not (bobp))
3615 ;; We insert a newline char rather than using open-line to
3616 ;; avoid rear-stickiness inheritence of read-only property.
3617 (progn (if (and (not (> depth ref-depth))
3618 (not before))
3619 (open-line 1)
3620 (if (and (not dbl-space) (> depth ref-depth))
3621 (newline 1)
3622 (if dbl-space
3623 (open-line 1)
3624 (if (not before)
3625 (newline 1)))))
3626 (if (and dbl-space (not (> relative-depth 0)))
3627 (newline 1))
3628 (if (and (not (eobp))
3629 (or (not (bolp))
3630 (and (not (bobp))
3631 ;; bolp doesnt detect concealed
3632 ;; trailing newlines, compensate:
3633 (save-excursion
3634 (forward-char -1)
3635 (allout-hidden-p)))))
3636 (forward-char 1))))
3637 ))
3638 (setq start (point))
3639 (insert (concat (allout-make-topic-prefix opening-numbered t depth)
3640 " "))
3641 (setq end (1+ (point)))
3642
3643 (allout-rebullet-heading (and offer-recent-bullet ref-bullet)
3644 depth nil nil t)
3645 (if (> relative-depth 0)
3646 (save-excursion (goto-char ref-topic)
3647 (allout-show-children)))
3648 (end-of-line)
3649
3650 (run-hook-with-args 'allout-structure-added-hook start end)
3651 )
3652 )
3653 ;;;_ > allout-open-subtopic (arg)
3654 (defun allout-open-subtopic (arg)
3655 "Open new topic header at deeper level than the current one.
3656
3657 Negative universal arg means to open deeper, but place the new topic
3658 prior to the current one."
3659 (interactive "p")
3660 (allout-open-topic 1 (> 0 arg) (< 1 arg)))
3661 ;;;_ > allout-open-sibtopic (arg)
3662 (defun allout-open-sibtopic (arg)
3663 "Open new topic header at same level as the current one.
3664
3665 Positive universal arg means to use the bullet of the prior sibling.
3666
3667 Negative universal arg means to place the new topic prior to the current
3668 one."
3669 (interactive "p")
3670 (allout-open-topic 0 (> 0 arg) (not (= 1 arg))))
3671 ;;;_ > allout-open-supertopic (arg)
3672 (defun allout-open-supertopic (arg)
3673 "Open new topic header at shallower level than the current one.
3674
3675 Negative universal arg means to open shallower, but place the new
3676 topic prior to the current one."
3677
3678 (interactive "p")
3679 (allout-open-topic -1 (> 0 arg) (< 1 arg)))
3680
3681 ;;;_ - Outline Alteration
3682 ;;;_ : Topic Modification
3683 ;;;_ = allout-former-auto-filler
3684 (defvar allout-former-auto-filler nil
3685 "Name of modal fill function being wrapped by `allout-auto-fill'.")
3686 ;;;_ > allout-auto-fill ()
3687 (defun allout-auto-fill ()
3688 "`allout-mode' autofill function.
3689
3690 Maintains outline hanging topic indentation if
3691 `allout-use-hanging-indents' is set."
3692
3693 (when (not allout-inhibit-auto-fill)
3694 (let ((fill-prefix (if allout-use-hanging-indents
3695 ;; Check for topic header indentation:
3696 (save-excursion
3697 (beginning-of-line)
3698 (if (looking-at allout-regexp)
3699 ;; ... construct indentation to account for
3700 ;; length of topic prefix:
3701 (make-string (progn (allout-end-of-prefix)
3702 (current-column))
3703 ?\ )))))
3704 (use-auto-fill-function (or allout-outside-normal-auto-fill-function
3705 auto-fill-function
3706 'do-auto-fill)))
3707 (if (or allout-former-auto-filler allout-use-hanging-indents)
3708 (funcall use-auto-fill-function)))))
3709 ;;;_ > allout-reindent-body (old-depth new-depth &optional number)
3710 (defun allout-reindent-body (old-depth new-depth &optional number)
3711 "Reindent body lines which were indented at OLD-DEPTH to NEW-DEPTH.
3712
3713 Optional arg NUMBER indicates numbering is being added, and it must
3714 be accommodated.
3715
3716 Note that refill of indented paragraphs is not done."
3717
3718 (save-excursion
3719 (allout-end-of-prefix)
3720 (let* ((new-margin (current-column))
3721 excess old-indent-begin old-indent-end
3722 ;; We want the column where the header-prefix text started
3723 ;; *before* the prefix was changed, so we infer it relative
3724 ;; to the new margin and the shift in depth:
3725 (old-margin (+ old-depth (- new-margin new-depth))))
3726
3727 ;; Process lines up to (but excluding) next topic header:
3728 (allout-unprotected
3729 (save-match-data
3730 (while
3731 (and (re-search-forward "\n\\(\\s-*\\)"
3732 nil
3733 t)
3734 ;; Register the indent data, before we reset the
3735 ;; match data with a subsequent `looking-at':
3736 (setq old-indent-begin (match-beginning 1)
3737 old-indent-end (match-end 1))
3738 (not (looking-at allout-regexp)))
3739 (if (> 0 (setq excess (- (- old-indent-end old-indent-begin)
3740 old-margin)))
3741 ;; Text starts left of old margin - don't adjust:
3742 nil
3743 ;; Text was hanging at or right of old left margin -
3744 ;; reindent it, preserving its existing indentation
3745 ;; beyond the old margin:
3746 (delete-region old-indent-begin old-indent-end)
3747 (indent-to (+ new-margin excess (current-column))))))))))
3748 ;;;_ > allout-rebullet-current-heading (arg)
3749 (defun allout-rebullet-current-heading (arg)
3750 "Solicit new bullet for current visible heading."
3751 (interactive "p")
3752 (let ((initial-col (current-column))
3753 (on-bullet (eq (point)(allout-current-bullet-pos)))
3754 from to
3755 (backwards (if (< arg 0)
3756 (setq arg (* arg -1)))))
3757 (while (> arg 0)
3758 (save-excursion (allout-back-to-current-heading)
3759 (allout-end-of-prefix)
3760 (setq from allout-recent-prefix-beginning
3761 to allout-recent-prefix-end)
3762 (allout-rebullet-heading t ;;; solicit
3763 nil ;;; depth
3764 nil ;;; number-control
3765 nil ;;; index
3766 t) ;;; do-successors
3767 (run-hook-with-args 'allout-exposure-change-hook
3768 from to t))
3769 (setq arg (1- arg))
3770 (if (<= arg 0)
3771 nil
3772 (setq initial-col nil) ; Override positioning back to init col
3773 (if (not backwards)
3774 (allout-next-visible-heading 1)
3775 (allout-goto-prefix-doublechecked)
3776 (allout-next-visible-heading -1))))
3777 (message "Done.")
3778 (cond (on-bullet (goto-char (allout-current-bullet-pos)))
3779 (initial-col (move-to-column initial-col)))))
3780 ;;;_ > allout-rebullet-heading (&optional solicit ...)
3781 (defun allout-rebullet-heading (&optional solicit
3782 new-depth
3783 number-control
3784 index
3785 do-successors)
3786
3787 "Adjust bullet of current topic prefix.
3788
3789 All args are optional.
3790
3791 If SOLICIT is non-nil, then the choice of bullet is solicited from
3792 user. If it's a character, then that character is offered as the
3793 default, otherwise the one suited to the context \(according to
3794 distinction or depth) is offered. If non-nil, then the
3795 context-specific bullet is just used.
3796
3797 Second arg DEPTH forces the topic prefix to that depth, regardless
3798 of the topic's current depth.
3799
3800 Third arg NUMBER-CONTROL can force the prefix to or away from
3801 numbered form. It has effect only if `allout-numbered-bullet' is
3802 non-nil and soliciting was not explicitly invoked (via first arg).
3803 Its effect, numbering or denumbering, then depends on the setting
3804 of the forth arg, INDEX.
3805
3806 If NUMBER-CONTROL is non-nil and forth arg INDEX is nil, then the
3807 prefix of the topic is forced to be non-numbered. Null index and
3808 non-nil NUMBER-CONTROL forces denumbering. Non-nil INDEX (and
3809 non-nil NUMBER-CONTROL) forces a numbered-prefix form. If non-nil
3810 INDEX is a number, then that number is used for the numbered
3811 prefix. Non-nil and non-number means that the index for the
3812 numbered prefix will be derived by allout-make-topic-prefix.
3813
3814 Fifth arg DO-SUCCESSORS t means re-resolve count on succeeding
3815 siblings.
3816
3817 Cf vars `allout-stylish-prefixes', `allout-old-style-prefixes',
3818 and `allout-numbered-bullet', which all affect the behavior of
3819 this function."
3820
3821 (let* ((current-depth (allout-depth))
3822 (new-depth (or new-depth current-depth))
3823 (mb allout-recent-prefix-beginning)
3824 (me allout-recent-prefix-end)
3825 (current-bullet (buffer-substring-no-properties (- me 1) me))
3826 (new-prefix (allout-make-topic-prefix current-bullet
3827 nil
3828 new-depth
3829 solicit
3830 number-control
3831 index)))
3832
3833 ;; Is new one is identical to old?
3834 (if (and (= current-depth new-depth)
3835 (string= current-bullet
3836 (substring new-prefix (1- (length new-prefix)))))
3837 ;; Nothing to do:
3838 t
3839
3840 ;; New prefix probably different from old:
3841 ; get rid of old one:
3842 (allout-unprotected (delete-region mb me))
3843 (goto-char mb)
3844 ; Dispense with number if
3845 ; numbered-bullet prefix:
3846 (if (and allout-numbered-bullet
3847 (string= allout-numbered-bullet current-bullet)
3848 (looking-at "[0-9]+"))
3849 (allout-unprotected
3850 (delete-region (match-beginning 0)(match-end 0))))
3851
3852 ; Put in new prefix:
3853 (allout-unprotected (insert new-prefix))
3854
3855 ;; Reindent the body if elected, margin changed, and not encrypted body:
3856 (if (and allout-reindent-bodies
3857 (not (= new-depth current-depth))
3858 (not (allout-encrypted-topic-p)))
3859 (allout-reindent-body current-depth new-depth))
3860
3861 ;; Recursively rectify successive siblings of orig topic if
3862 ;; caller elected for it:
3863 (if do-successors
3864 (save-excursion
3865 (while (allout-next-sibling new-depth nil)
3866 (setq index
3867 (cond ((numberp index) (1+ index))
3868 ((not number-control) (allout-sibling-index))))
3869 (if (allout-numbered-type-prefix)
3870 (allout-rebullet-heading nil ;;; solicit
3871 new-depth ;;; new-depth
3872 number-control;;; number-control
3873 index ;;; index
3874 nil))))) ;;;(dont!)do-successors
3875 ) ; (if (and (= current-depth new-depth)...))
3876 ) ; let* ((current-depth (allout-depth))...)
3877 ) ; defun
3878 ;;;_ > allout-rebullet-topic (arg)
3879 (defun allout-rebullet-topic (arg &optional sans-offspring)
3880 "Rebullet the visible topic containing point and all contained subtopics.
3881
3882 Descends into invisible as well as visible topics, however.
3883
3884 When optional sans-offspring is non-nil, subtopics are not
3885 shifted. \(Shifting a topic outwards without shifting its
3886 offspring is disallowed, since this would create a \"containment
3887 discontinuity\", where the depth difference between a topic and
3888 its immediate offspring is greater than one.)
3889
3890 With repeat count, shift topic depth by that amount."
3891 (interactive "P")
3892 (let ((start-col (current-column)))
3893 (save-excursion
3894 ;; Normalize arg:
3895 (cond ((null arg) (setq arg 0))
3896 ((listp arg) (setq arg (car arg))))
3897 ;; Fill the user in, in case we're shifting a big topic:
3898 (if (not (zerop arg)) (message "Shifting..."))
3899 (allout-back-to-current-heading)
3900 (if (<= (+ allout-recent-depth arg) 0)
3901 (error "Attempt to shift topic below level 1"))
3902 (allout-rebullet-topic-grunt arg nil nil nil nil sans-offspring)
3903 (if (not (zerop arg)) (message "Shifting... done.")))
3904 (move-to-column (max 0 (+ start-col arg)))))
3905 ;;;_ > allout-rebullet-topic-grunt (&optional relative-depth ...)
3906 (defun allout-rebullet-topic-grunt (&optional relative-depth
3907 starting-depth
3908 starting-point
3909 index
3910 do-successors
3911 sans-offspring)
3912 "Like `allout-rebullet-topic', but on nearest containing topic
3913 \(visible or not).
3914
3915 See `allout-rebullet-heading' for rebulleting behavior.
3916
3917 All arguments are optional.
3918
3919 First arg RELATIVE-DEPTH means to shift the depth of the entire
3920 topic that amount.
3921
3922 Several subsequent args are for internal recursive use by the function
3923 itself: STARTING-DEPTH, STARTING-POINT, and INDEX.
3924
3925 Finally, if optional SANS-OFFSPRING is non-nil then the offspring
3926 are not shifted. \(Shifting a topic outwards without shifting
3927 its offspring is disallowed, since this would create a
3928 \"containment discontinuity\", where the depth difference between
3929 a topic and its immediate offspring is greater than one..)"
3930
3931 ;; XXX the recursion here is peculiar, and in general the routine may
3932 ;; need simplification with refactoring.
3933
3934 (if (and sans-offspring
3935 relative-depth
3936 (< relative-depth 0))
3937 (error (concat "Attempt to shift topic outwards without offspring,"
3938 " would cause containment discontinuity.")))
3939
3940 (let* ((relative-depth (or relative-depth 0))
3941 (new-depth (allout-depth))
3942 (starting-depth (or starting-depth new-depth))
3943 (on-starting-call (null starting-point))
3944 (index (or index
3945 ;; Leave index null on starting call, so rebullet-heading
3946 ;; calculates it at what might be new depth:
3947 (and (or (zerop relative-depth)
3948 (not on-starting-call))
3949 (allout-sibling-index))))
3950 (starting-index index)
3951 (moving-outwards (< 0 relative-depth))
3952 (starting-point (or starting-point (point)))
3953 (local-point (point)))
3954
3955 ;; Sanity check for excessive promotion done only on starting call:
3956 (and on-starting-call
3957 moving-outwards
3958 (> 0 (+ starting-depth relative-depth))
3959 (error "Attempt to shift topic out beyond level 1"))
3960
3961 (cond ((= starting-depth new-depth)
3962 ;; We're at depth to work on this one.
3963
3964 ;; When shifting out we work on the children before working on
3965 ;; the parent to avoid interim `allout-aberrant-container-p'
3966 ;; aberrancy, and vice-versa when shifting in:
3967 (if (>= relative-depth 0)
3968 (allout-rebullet-heading nil
3969 (+ starting-depth relative-depth)
3970 nil ;;; number
3971 index
3972 nil)) ;;; do-successors
3973 (when (not sans-offspring)
3974 ;; ... and work on subsequent ones which are at greater depth:
3975 (setq index 0)
3976 (allout-next-heading)
3977 (while (and (not (eobp))
3978 (< starting-depth (allout-depth)))
3979 (setq index (1+ index))
3980 (allout-rebullet-topic-grunt relative-depth
3981 (1+ starting-depth)
3982 starting-point
3983 index)))
3984 (when (< relative-depth 0)
3985 (save-excursion
3986 (goto-char local-point)
3987 (allout-rebullet-heading nil ;;; solicit
3988 (+ starting-depth relative-depth)
3989 nil ;;; number
3990 starting-index
3991 nil)))) ;;; do-successors
3992
3993 ((< starting-depth new-depth)
3994 ;; Rare case - subtopic more than one level deeper than parent.
3995 ;; Treat this one at an even deeper level:
3996 (allout-rebullet-topic-grunt relative-depth
3997 new-depth
3998 starting-point
3999 index
4000 sans-offspring)))
4001
4002 (if on-starting-call
4003 (progn
4004 ;; Rectify numbering of former siblings of the adjusted topic,
4005 ;; if topic has changed depth
4006 (if (or do-successors
4007 (and (not (zerop relative-depth))
4008 (or (= allout-recent-depth starting-depth)
4009 (= allout-recent-depth (+ starting-depth
4010 relative-depth)))))
4011 (allout-rebullet-heading nil nil nil nil t))
4012 ;; Now rectify numbering of new siblings of the adjusted topic,
4013 ;; if depth has been changed:
4014 (progn (goto-char starting-point)
4015 (if (not (zerop relative-depth))
4016 (allout-rebullet-heading nil nil nil nil t)))))
4017 )
4018 )
4019 ;;;_ > allout-renumber-to-depth (&optional depth)
4020 (defun allout-renumber-to-depth (&optional depth)
4021 "Renumber siblings at current depth.
4022
4023 Affects superior topics if optional arg DEPTH is less than current depth.
4024
4025 Returns final depth."
4026
4027 ;; Proceed by level, processing subsequent siblings on each,
4028 ;; ascending until we get shallower than the start depth:
4029
4030 (let ((ascender (allout-depth))
4031 was-eobp)
4032 (while (and (not (eobp))
4033 (allout-depth)
4034 (>= allout-recent-depth depth)
4035 (>= ascender depth))
4036 ; Skip over all topics at
4037 ; lesser depths, which can not
4038 ; have been disturbed:
4039 (while (and (not (setq was-eobp (eobp)))
4040 (> allout-recent-depth ascender))
4041 (allout-next-heading))
4042 ; Prime ascender for ascension:
4043 (setq ascender (1- allout-recent-depth))
4044 (if (>= allout-recent-depth depth)
4045 (allout-rebullet-heading nil ;;; solicit
4046 nil ;;; depth
4047 nil ;;; number-control
4048 nil ;;; index
4049 t)) ;;; do-successors
4050 (if was-eobp (goto-char (point-max)))))
4051 allout-recent-depth)
4052 ;;;_ > allout-number-siblings (&optional denumber)
4053 (defun allout-number-siblings (&optional denumber)
4054 "Assign numbered topic prefix to this topic and its siblings.
4055
4056 With universal argument, denumber - assign default bullet to this
4057 topic and its siblings.
4058
4059 With repeated universal argument (`^U^U'), solicit bullet for each
4060 rebulleting each topic at this level."
4061
4062 (interactive "P")
4063
4064 (save-excursion
4065 (allout-back-to-current-heading)
4066 (allout-beginning-of-level)
4067 (let ((depth allout-recent-depth)
4068 (index (if (not denumber) 1))
4069 (use-bullet (equal '(16) denumber))
4070 (more t))
4071 (while more
4072 (allout-rebullet-heading use-bullet ;;; solicit
4073 depth ;;; depth
4074 t ;;; number-control
4075 index ;;; index
4076 nil) ;;; do-successors
4077 (if index (setq index (1+ index)))
4078 (setq more (allout-next-sibling depth nil))))))
4079 ;;;_ > allout-shift-in (arg)
4080 (defun allout-shift-in (arg)
4081 "Increase depth of current heading and any items collapsed within it.
4082
4083 With a negative argument, the item is shifted out using
4084 `allout-shift-out', instead.
4085
4086 With an argument greater than one, shift-in the item but not its
4087 offspring, making the item into a sibling of its former children,
4088 and a child of sibling that formerly preceeded it.
4089
4090 You are not allowed to shift the first offspring of a topic
4091 inwards, because that would yield a \"containment
4092 discontinuity\", where the depth difference between a topic and
4093 its immediate offspring is greater than one. The first topic in
4094 the file can be adjusted to any positive depth, however."
4095
4096 (interactive "p")
4097 (if (< arg 0)
4098 (allout-shift-out (* arg -1))
4099 ;; refuse to create a containment discontinuity:
4100 (save-excursion
4101 (allout-back-to-current-heading)
4102 (if (not (bobp))
4103 (let* ((current-depth allout-recent-depth)
4104 (start-point (point))
4105 (predecessor-depth (progn
4106 (forward-char -1)
4107 (allout-goto-prefix-doublechecked)
4108 (if (< (point) start-point)
4109 allout-recent-depth
4110 0))))
4111 (if (and (> predecessor-depth 0)
4112 (> (1+ current-depth)
4113 (1+ predecessor-depth)))
4114 (error (concat "Disallowed shift deeper than"
4115 " containing topic's children."))))))
4116 (let ((where (point)))
4117 (allout-rebullet-topic 1 (and (> arg 1) 'sans-offspring))
4118 (run-hook-with-args 'allout-structure-shifted-hook arg where))))
4119 ;;;_ > allout-shift-out (arg)
4120 (defun allout-shift-out (arg)
4121 "Decrease depth of current heading and any topics collapsed within it.
4122 This will make the item a sibling of its former container.
4123
4124 With a negative argument, the item is shifted in using
4125 `allout-shift-in', instead.
4126
4127 With an argument greater than one, shift-out the item's offspring
4128 but not the item itself, making the former children siblings of
4129 the item.
4130
4131 With an argument greater than 1, the item's offspring are shifted
4132 out without shifting the item. This will make the immediate
4133 subtopics into siblings of the item."
4134 (interactive "p")
4135 (if (< arg 0)
4136 (allout-shift-in (* arg -1))
4137 ;; Get proper exposure in this area:
4138 (save-excursion (if (allout-ascend)
4139 (allout-show-children)))
4140 ;; Show collapsed children if there's a successor which will become
4141 ;; their sibling:
4142 (if (and (allout-current-topic-collapsed-p)
4143 (save-excursion (allout-next-sibling)))
4144 (allout-show-children))
4145 (let ((where (and (allout-depth) allout-recent-prefix-beginning)))
4146 (save-excursion
4147 (if (> arg 1)
4148 ;; Shift the offspring but not the topic:
4149 (let ((children-chart (allout-chart-subtree 1)))
4150 (if (listp (car children-chart))
4151 ;; whoops:
4152 (setq children-chart (allout-flatten children-chart)))
4153 (save-excursion
4154 (dolist (child-point children-chart)
4155 (goto-char child-point)
4156 (allout-shift-out 1))))
4157 (allout-rebullet-topic (* arg -1))))
4158 (run-hook-with-args 'allout-structure-shifted-hook (* arg -1) where))))
4159 ;;;_ : Surgery (kill-ring) functions with special provisions for outlines:
4160 ;;;_ > allout-kill-line (&optional arg)
4161 (defun allout-kill-line (&optional arg)
4162 "Kill line, adjusting subsequent lines suitably for outline mode."
4163
4164 (interactive "*P")
4165
4166 (if (or (not (allout-mode-p))
4167 (not (bolp))
4168 (not (looking-at allout-regexp)))
4169 ;; Just do a regular kill:
4170 (kill-line arg)
4171 ;; Ah, have to watch out for adjustments:
4172 (let* ((beg (point))
4173 end
4174 (beg-hidden (allout-hidden-p))
4175 (end-hidden (save-excursion (allout-end-of-current-line)
4176 (setq end (point))
4177 (allout-hidden-p)))
4178 (depth (allout-depth)))
4179
4180 (allout-annotate-hidden beg end)
4181
4182 (if (and (not beg-hidden) (not end-hidden))
4183 (allout-unprotected (kill-line arg))
4184 (kill-line arg))
4185 (if allout-numbered-bullet
4186 (save-excursion ; Renumber subsequent topics if needed:
4187 (if (not (looking-at allout-regexp))
4188 (allout-next-heading))
4189 (allout-renumber-to-depth depth)))
4190 (run-hook-with-args 'allout-structure-deleted-hook depth (point)))))
4191 ;;;_ > allout-copy-line-as-kill ()
4192 (defun allout-copy-line-as-kill ()
4193 "Like allout-kill-topic, but save to kill ring instead of deleting."
4194 (interactive)
4195 (let ((buffer-read-only t))
4196 (condition-case nil
4197 (allout-kill-line)
4198 (buffer-read-only nil))))
4199 ;;;_ > allout-kill-topic ()
4200 (defun allout-kill-topic ()
4201 "Kill topic together with subtopics.
4202
4203 Trailing whitespace is killed with a topic if that whitespace:
4204
4205 - would separate the topic from a subsequent sibling
4206 - would separate the topic from the end of buffer
4207 - would not be added to whitespace already separating the topic from the
4208 previous one.
4209
4210 Topic exposure is marked with text-properties, to be used by
4211 allout-yank-processing for exposure recovery."
4212
4213 (interactive)
4214 (let* ((inhibit-field-text-motion t)
4215 (beg (prog1 (allout-back-to-current-heading) (beginning-of-line)))
4216 (depth allout-recent-depth))
4217 (allout-end-of-current-subtree)
4218 (if (and (/= (current-column) 0) (not (eobp)))
4219 (forward-char 1))
4220 (if (not (eobp))
4221 (if (and (looking-at "\n")
4222 (or (save-excursion
4223 (or (not (allout-next-heading))
4224 (= depth allout-recent-depth)))
4225 (and (> (- beg (point-min)) 3)
4226 (string= (buffer-substring (- beg 2) beg) "\n\n"))))
4227 (forward-char 1)))
4228
4229 (allout-annotate-hidden beg (point))
4230
4231 (allout-unprotected (kill-region beg (point)))
4232 (save-excursion
4233 (allout-renumber-to-depth depth))
4234 (run-hook-with-args 'allout-structure-deleted-hook depth (point))))
4235 ;;;_ > allout-copy-topic-as-kill ()
4236 (defun allout-copy-topic-as-kill ()
4237 "Like allout-kill-topic, but save to kill ring instead of deleting."
4238 (interactive)
4239 (let ((buffer-read-only t))
4240 (condition-case nil
4241 (allout-kill-topic)
4242 (buffer-read-only (message "Topic copied...")))))
4243 ;;;_ > allout-annotate-hidden (begin end)
4244 (defun allout-annotate-hidden (begin end)
4245 "Qualify text with properties to indicate exposure status."
4246
4247 (let ((was-modified (buffer-modified-p))
4248 (buffer-read-only nil))
4249 (allout-unprotected
4250 (remove-text-properties begin end '(allout-was-hidden t)))
4251 (save-excursion
4252 (goto-char begin)
4253 (let (done next prev overlay)
4254 (while (not done)
4255 ;; at or advance to start of next hidden region:
4256 (if (not (allout-hidden-p))
4257 (setq next
4258 (max (1+ (point))
4259 (next-single-char-property-change (point)
4260 'invisible
4261 nil end))))
4262 (if (or (not next) (eq prev next))
4263 ;; still not at start of hidden area - must not be any left.
4264 (setq done t)
4265 (goto-char next)
4266 (setq prev next)
4267 (if (not (allout-hidden-p))
4268 ;; still not at start of hidden area.
4269 (setq done t)
4270 (setq overlay (allout-get-invisibility-overlay))
4271 (setq next (overlay-end overlay)
4272 prev next)
4273 ;; advance to end of this hidden area:
4274 (when next
4275 (goto-char next)
4276 (allout-unprotected
4277 (put-text-property (overlay-start overlay) next
4278 'allout-was-hidden t))))))))
4279 (set-buffer-modified-p was-modified)))
4280 ;;;_ > allout-hide-by-annotation (begin end)
4281 (defun allout-hide-by-annotation (begin end)
4282 "Translate text properties indicating exposure status into actual exposure."
4283 (save-excursion
4284 (goto-char begin)
4285 (let ((was-modified (buffer-modified-p))
4286 done next prev)
4287 (while (not done)
4288 ;; at or advance to start of next annotation:
4289 (if (not (get-text-property (point) 'allout-was-hidden))
4290 (setq next (next-single-char-property-change (point)
4291 'allout-was-hidden
4292 nil end)))
4293 (if (or (not next) (eq prev next))
4294 ;; no more or not advancing - must not be any left.
4295 (setq done t)
4296 (goto-char next)
4297 (setq prev next)
4298 (if (not (get-text-property (point) 'allout-was-hidden))
4299 ;; still not at start of annotation.
4300 (setq done t)
4301 ;; advance to just after end of this annotation:
4302 (setq next (next-single-char-property-change (point)
4303 'allout-was-hidden
4304 nil end))
4305 (overlay-put (make-overlay prev next)
4306 'category 'allout-exposure-category)
4307 (allout-unprotected
4308 (remove-text-properties prev next '(allout-was-hidden t)))
4309 (setq prev next)
4310 (if next (goto-char next)))))
4311 (set-buffer-modified-p was-modified))))
4312 ;;;_ > allout-remove-exposure-annotation (begin end)
4313 (defun allout-remove-exposure-annotation (begin end)
4314 "Remove text properties indicating exposure status."
4315 (remove-text-properties begin end '(allout-was-hidden t)))
4316
4317 ;;;_ > allout-yank-processing ()
4318 (defun allout-yank-processing (&optional arg)
4319
4320 "Incidental allout-specific business to be done just after text yanks.
4321
4322 Does depth adjustment of yanked topics, when:
4323
4324 1 the stuff being yanked starts with a valid outline header prefix, and
4325 2 it is being yanked at the end of a line which consists of only a valid
4326 topic prefix.
4327
4328 Also, adjusts numbering of subsequent siblings when appropriate.
4329
4330 Depth adjustment alters the depth of all the topics being yanked
4331 the amount it takes to make the first topic have the depth of the
4332 header into which it's being yanked.
4333
4334 The point is left in front of yanked, adjusted topics, rather than
4335 at the end (and vice-versa with the mark). Non-adjusted yanks,
4336 however, are left exactly like normal, non-allout-specific yanks."
4337
4338 (interactive "*P")
4339 ; Get to beginning, leaving
4340 ; region around subject:
4341 (if (< (allout-mark-marker t) (point))
4342 (exchange-point-and-mark))
4343 (allout-unprotected
4344 (let* ((subj-beg (point))
4345 (into-bol (bolp))
4346 (subj-end (allout-mark-marker t))
4347 ;; 'resituate' if yanking an entire topic into topic header:
4348 (resituate (and (allout-e-o-prefix-p)
4349 (looking-at allout-regexp)
4350 (allout-prefix-data)))
4351 ;; `rectify-numbering' if resituating (where several topics may
4352 ;; be resituating) or yanking a topic into a topic slot (bol):
4353 (rectify-numbering (or resituate
4354 (and into-bol (looking-at allout-regexp)))))
4355 (if resituate
4356 ; The yanked stuff is a topic:
4357 (let* ((prefix-len (- (match-end 1) subj-beg))
4358 (subj-depth allout-recent-depth)
4359 (prefix-bullet (allout-recent-bullet))
4360 (adjust-to-depth
4361 ;; Nil if adjustment unnecessary, otherwise depth to which
4362 ;; adjustment should be made:
4363 (save-excursion
4364 (and (goto-char subj-end)
4365 (eolp)
4366 (goto-char subj-beg)
4367 (and (looking-at allout-regexp)
4368 (progn
4369 (beginning-of-line)
4370 (not (= (point) subj-beg)))
4371 (looking-at allout-regexp)
4372 (allout-prefix-data))
4373 allout-recent-depth)))
4374 (more t))
4375 (setq rectify-numbering allout-numbered-bullet)
4376 (if adjust-to-depth
4377 ; Do the adjustment:
4378 (progn
4379 (save-restriction
4380 (narrow-to-region subj-beg subj-end)
4381 ; Trim off excessive blank
4382 ; line at end, if any:
4383 (goto-char (point-max))
4384 (if (looking-at "^$")
4385 (allout-unprotected (delete-char -1)))
4386 ; Work backwards, with each
4387 ; shallowest level,
4388 ; successively excluding the
4389 ; last processed topic from
4390 ; the narrow region:
4391 (while more
4392 (allout-back-to-current-heading)
4393 ; go as high as we can in each bunch:
4394 (while (allout-ascend))
4395 (save-excursion
4396 (allout-rebullet-topic-grunt (- adjust-to-depth
4397 subj-depth))
4398 (allout-depth))
4399 (if (setq more (not (bobp)))
4400 (progn (widen)
4401 (forward-char -1)
4402 (narrow-to-region subj-beg (point))))))
4403 ;; Preserve new bullet if it's a distinctive one, otherwise
4404 ;; use old one:
4405 (if (string-match (regexp-quote prefix-bullet)
4406 allout-distinctive-bullets-string)
4407 ; Delete from bullet of old to
4408 ; before bullet of new:
4409 (progn
4410 (beginning-of-line)
4411 (delete-region (point) subj-beg)
4412 (set-marker (allout-mark-marker t) subj-end)
4413 (goto-char subj-beg)
4414 (allout-end-of-prefix))
4415 ; Delete base subj prefix,
4416 ; leaving old one:
4417 (delete-region (point) (+ (point)
4418 prefix-len
4419 (- adjust-to-depth subj-depth)))
4420 ; and delete residual subj
4421 ; prefix digits and space:
4422 (while (looking-at "[0-9]") (delete-char 1))
4423 (if (looking-at " ") (delete-char 1))))
4424 (exchange-point-and-mark))))
4425 (if rectify-numbering
4426 (progn
4427 (save-excursion
4428 ; Give some preliminary feedback:
4429 (message "... reconciling numbers")
4430 ; ... and renumber, in case necessary:
4431 (goto-char subj-beg)
4432 (if (allout-goto-prefix-doublechecked)
4433 (allout-rebullet-heading nil ;;; solicit
4434 (allout-depth) ;;; depth
4435 nil ;;; number-control
4436 nil ;;; index
4437 t))
4438 (message ""))))
4439 (if (or into-bol resituate)
4440 (allout-hide-by-annotation (point) (allout-mark-marker t))
4441 (allout-remove-exposure-annotation (allout-mark-marker t) (point)))
4442 (if (not resituate)
4443 (exchange-point-and-mark))
4444 (run-hook-with-args 'allout-structure-added-hook subj-beg subj-end))))
4445 ;;;_ > allout-yank (&optional arg)
4446 (defun allout-yank (&optional arg)
4447 "`allout-mode' yank, with depth and numbering adjustment of yanked topics.
4448
4449 Non-topic yanks work no differently than normal yanks.
4450
4451 If a topic is being yanked into a bare topic prefix, the depth of the
4452 yanked topic is adjusted to the depth of the topic prefix.
4453
4454 1 we're yanking in an `allout-mode' buffer
4455 2 the stuff being yanked starts with a valid outline header prefix, and
4456 3 it is being yanked at the end of a line which consists of only a valid
4457 topic prefix.
4458
4459 If these conditions hold then the depth of the yanked topics are all
4460 adjusted the amount it takes to make the first one at the depth of the
4461 header into which it's being yanked.
4462
4463 The point is left in front of yanked, adjusted topics, rather than
4464 at the end (and vice-versa with the mark). Non-adjusted yanks,
4465 however, (ones that don't qualify for adjustment) are handled
4466 exactly like normal yanks.
4467
4468 Numbering of yanked topics, and the successive siblings at the depth
4469 into which they're being yanked, is adjusted.
4470
4471 `allout-yank-pop' works with `allout-yank' just like normal `yank-pop'
4472 works with normal `yank' in non-outline buffers."
4473
4474 (interactive "*P")
4475 (setq this-command 'yank)
4476 (allout-unprotected
4477 (yank arg))
4478 (if (allout-mode-p)
4479 (allout-yank-processing)))
4480 ;;;_ > allout-yank-pop (&optional arg)
4481 (defun allout-yank-pop (&optional arg)
4482 "Yank-pop like `allout-yank' when popping to bare outline prefixes.
4483
4484 Adapts level of popped topics to level of fresh prefix.
4485
4486 Note - prefix changes to distinctive bullets will stick, if followed
4487 by pops to non-distinctive yanks. Bug..."
4488
4489 (interactive "*p")
4490 (setq this-command 'yank)
4491 (yank-pop arg)
4492 (if (allout-mode-p)
4493 (allout-yank-processing)))
4494
4495 ;;;_ - Specialty bullet functions
4496 ;;;_ : File Cross references
4497 ;;;_ > allout-resolve-xref ()
4498 (defun allout-resolve-xref ()
4499 "Pop to file associated with current heading, if it has an xref bullet.
4500
4501 \(Works according to setting of `allout-file-xref-bullet')."
4502 (interactive)
4503 (if (not allout-file-xref-bullet)
4504 (error
4505 "Outline cross references disabled - no `allout-file-xref-bullet'")
4506 (if (not (string= (allout-current-bullet) allout-file-xref-bullet))
4507 (error "Current heading lacks cross-reference bullet `%s'"
4508 allout-file-xref-bullet)
4509 (let ((inhibit-field-text-motion t)
4510 file-name)
4511 (save-excursion
4512 (let* ((text-start allout-recent-prefix-end)
4513 (heading-end (progn (end-of-line) (point))))
4514 (goto-char text-start)
4515 (setq file-name
4516 (if (re-search-forward "\\s-\\(\\S-*\\)" heading-end t)
4517 (buffer-substring (match-beginning 1) (match-end 1))))))
4518 (setq file-name (expand-file-name file-name))
4519 (if (or (file-exists-p file-name)
4520 (if (file-writable-p file-name)
4521 (y-or-n-p (format "%s not there, create one? "
4522 file-name))
4523 (error "%s not found and can't be created" file-name)))
4524 (condition-case failure
4525 (find-file-other-window file-name)
4526 (error failure))
4527 (error "%s not found" file-name))
4528 )
4529 )
4530 )
4531 )
4532
4533 ;;;_ #6 Exposure Control
4534
4535 ;;;_ - Fundamental
4536 ;;;_ > allout-flag-region (from to flag)
4537 (defun allout-flag-region (from to flag)
4538 "Conceal text between FROM and TO if FLAG is non-nil, else reveal it.
4539
4540 Exposure-change hook `allout-exposure-change-hook' is run with the same
4541 arguments as this function, after the exposure changes are made. \(The old
4542 `allout-view-change-hook' is being deprecated, and eventually will not be
4543 invoked.\)"
4544
4545 ;; We use outline invisibility spec.
4546 (remove-overlays from to 'category 'allout-exposure-category)
4547 (when flag
4548 (let ((o (make-overlay from to)))
4549 (overlay-put o 'category 'allout-exposure-category)
4550 (when (featurep 'xemacs)
4551 (let ((props (symbol-plist 'allout-exposure-category)))
4552 (while props
4553 (overlay-put o (pop props) (pop props)))))))
4554 (run-hooks 'allout-view-change-hook)
4555 (run-hook-with-args 'allout-exposure-change-hook from to flag))
4556 ;;;_ > allout-flag-current-subtree (flag)
4557 (defun allout-flag-current-subtree (flag)
4558 "Conceal currently-visible topic's subtree if FLAG non-nil, else reveal it."
4559
4560 (save-excursion
4561 (allout-back-to-current-heading)
4562 (let ((inhibit-field-text-motion t))
4563 (end-of-line))
4564 (allout-flag-region (point)
4565 ;; Exposing must not leave trailing blanks hidden,
4566 ;; but can leave them exposed when hiding, so we
4567 ;; can use flag's inverse as the
4568 ;; include-trailing-blank cue:
4569 (allout-end-of-current-subtree (not flag))
4570 flag)))
4571
4572 ;;;_ - Topic-specific
4573 ;;;_ > allout-show-entry ()
4574 (defun allout-show-entry ()
4575 "Like `allout-show-current-entry', but reveals entries in hidden topics.
4576
4577 This is a way to give restricted peek at a concealed locality without the
4578 expense of exposing its context, but can leave the outline with aberrant
4579 exposure. `allout-show-offshoot' should be used after the peek to rectify
4580 the exposure."
4581
4582 (interactive)
4583 (save-excursion
4584 (let (beg end)
4585 (allout-goto-prefix-doublechecked)
4586 (setq beg (if (allout-hidden-p) (1- (point)) (point)))
4587 (setq end (allout-pre-next-prefix))
4588 (allout-flag-region beg end nil)
4589 (list beg end))))
4590 ;;;_ > allout-show-children (&optional level strict)
4591 (defun allout-show-children (&optional level strict)
4592
4593 "If point is visible, show all direct subheadings of this heading.
4594
4595 Otherwise, do `allout-show-to-offshoot', and then show subheadings.
4596
4597 Optional LEVEL specifies how many levels below the current level
4598 should be shown, or all levels if t. Default is 1.
4599
4600 Optional STRICT means don't resort to -show-to-offshoot, no matter
4601 what. This is basically so -show-to-offshoot, which is called by
4602 this function, can employ the pure offspring-revealing capabilities of
4603 it.
4604
4605 Returns point at end of subtree that was opened, if any. (May get a
4606 point of non-opened subtree?)"
4607
4608 (interactive "p")
4609 (let ((start-point (point)))
4610 (if (and (not strict)
4611 (allout-hidden-p))
4612
4613 (progn (allout-show-to-offshoot) ; Point's concealed, open to
4614 ; expose it.
4615 ;; Then recurse, but with "strict" set so we don't
4616 ;; infinite regress:
4617 (allout-show-children level t))
4618
4619 (save-excursion
4620 (allout-beginning-of-current-line)
4621 (save-restriction
4622 (let* (depth
4623 (chart (allout-chart-subtree (or level 1)))
4624 (to-reveal (or (allout-chart-to-reveal chart (or level 1))
4625 ;; interactive, show discontinuous children:
4626 (and chart
4627 (interactive-p)
4628 (save-excursion
4629 (allout-back-to-current-heading)
4630 (setq depth (allout-current-depth))
4631 (and (allout-next-heading)
4632 (> allout-recent-depth
4633 (1+ depth))))
4634 (message
4635 "Discontinuous offspring; use `%s %s'%s."
4636 (substitute-command-keys
4637 "\\[universal-argument]")
4638 (substitute-command-keys
4639 "\\[allout-shift-out]")
4640 " to elevate them.")
4641 (allout-chart-to-reveal
4642 chart (- allout-recent-depth depth))))))
4643 (goto-char start-point)
4644 (when (and strict (allout-hidden-p))
4645 ;; Concealed root would already have been taken care of,
4646 ;; unless strict was set.
4647 (allout-flag-region (point) (allout-snug-back) nil)
4648 (when allout-show-bodies
4649 (goto-char (car to-reveal))
4650 (allout-show-current-entry)))
4651 (while to-reveal
4652 (goto-char (car to-reveal))
4653 (allout-flag-region (save-excursion (allout-snug-back) (point))
4654 (progn (search-forward "\n" nil t)
4655 (1- (point)))
4656 nil)
4657 (when allout-show-bodies
4658 (goto-char (car to-reveal))
4659 (allout-show-current-entry))
4660 (setq to-reveal (cdr to-reveal)))))))
4661 ;; Compensate for `save-excursion's maintenance of point
4662 ;; within invisible text:
4663 (goto-char start-point)))
4664 ;;;_ > allout-show-to-offshoot ()
4665 (defun allout-show-to-offshoot ()
4666 "Like `allout-show-entry', but reveals all concealed ancestors, as well.
4667
4668 Useful for coherently exposing to a random point in a hidden region."
4669 (interactive)
4670 (save-excursion
4671 (let ((inhibit-field-text-motion t)
4672 (orig-pt (point))
4673 (orig-pref (allout-goto-prefix-doublechecked))
4674 (last-at (point))
4675 bag-it)
4676 (while (or bag-it (allout-hidden-p))
4677 (while (allout-hidden-p)
4678 (move-beginning-of-line 1)
4679 (if (allout-hidden-p) (forward-char -1)))
4680 (if (= last-at (setq last-at (point)))
4681 ;; Oops, we're not making any progress! Show the current
4682 ;; topic completely, and bag this try.
4683 (progn (beginning-of-line)
4684 (allout-show-current-subtree)
4685 (goto-char orig-pt)
4686 (setq bag-it t)
4687 (beep)
4688 (message "%s: %s"
4689 "allout-show-to-offshoot: "
4690 "Aberrant nesting encountered."))
4691 (allout-show-children)
4692 (goto-char orig-pref)))
4693 (goto-char orig-pt)))
4694 (if (allout-hidden-p)
4695 (allout-show-entry)))
4696 ;;;_ > allout-hide-current-entry ()
4697 (defun allout-hide-current-entry ()
4698 "Hide the body directly following this heading."
4699 (interactive)
4700 (allout-back-to-current-heading)
4701 (save-excursion
4702 (let ((inhibit-field-text-motion t))
4703 (end-of-line))
4704 (allout-flag-region (point)
4705 (progn (allout-end-of-entry) (point))
4706 t)))
4707 ;;;_ > allout-show-current-entry (&optional arg)
4708 (defun allout-show-current-entry (&optional arg)
4709 "Show body following current heading, or hide entry with universal argument."
4710
4711 (interactive "P")
4712 (if arg
4713 (allout-hide-current-entry)
4714 (save-excursion (allout-show-to-offshoot))
4715 (save-excursion
4716 (allout-flag-region (point)
4717 (progn (allout-end-of-entry t) (point))
4718 nil)
4719 )))
4720 ;;;_ > allout-show-current-subtree (&optional arg)
4721 (defun allout-show-current-subtree (&optional arg)
4722 "Show everything within the current topic. With a repeat-count,
4723 expose this topic and its siblings."
4724 (interactive "P")
4725 (save-excursion
4726 (if (<= (allout-current-depth) 0)
4727 ;; Outside any topics - try to get to the first:
4728 (if (not (allout-next-heading))
4729 (error "No topics")
4730 ;; got to first, outermost topic - set to expose it and siblings:
4731 (message "Above outermost topic - exposing all.")
4732 (allout-flag-region (point-min)(point-max) nil))
4733 (allout-beginning-of-current-line)
4734 (if (not arg)
4735 (allout-flag-current-subtree nil)
4736 (allout-beginning-of-level)
4737 (allout-expose-topic '(* :))))))
4738 ;;;_ > allout-current-topic-collapsed-p (&optional include-single-liners)
4739 (defun allout-current-topic-collapsed-p (&optional include-single-liners)
4740 "True if the currently visible containing topic is already collapsed.
4741
4742 Single line topics intrinsically can be considered as being both
4743 collapsed and uncollapsed. If optional INCLUDE-SINGLE-LINERS is
4744 true, then single-line topics are considered to be collapsed. By
4745 default, they are treated as being uncollapsed."
4746 (save-excursion
4747 (and
4748 ;; Is the topic all on one line (allowing for trailing blank line)?
4749 (>= (progn (allout-back-to-current-heading)
4750 (move-end-of-line 1)
4751 (point))
4752 (allout-end-of-current-subtree (not (looking-at "\n\n"))))
4753
4754 (or include-single-liners
4755 (progn (backward-char 1) (allout-hidden-p))))))
4756 ;;;_ > allout-hide-current-subtree (&optional just-close)
4757 (defun allout-hide-current-subtree (&optional just-close)
4758 "Close the current topic, or containing topic if this one is already closed.
4759
4760 If this topic is closed and it's a top level topic, close this topic
4761 and its siblings.
4762
4763 If optional arg JUST-CLOSE is non-nil, do not close the parent or
4764 siblings, even if the target topic is already closed."
4765
4766 (interactive)
4767 (let* ((from (point))
4768 (sibs-msg "Top-level topic already closed - closing siblings...")
4769 (current-exposed (not (allout-current-topic-collapsed-p t))))
4770 (cond (current-exposed (allout-flag-current-subtree t))
4771 (just-close nil)
4772 ((allout-ascend) (allout-hide-current-subtree))
4773 (t (goto-char 0)
4774 (message sibs-msg)
4775 (allout-goto-prefix-doublechecked)
4776 (allout-expose-topic '(0 :))
4777 (message (concat sibs-msg " Done."))))
4778 (goto-char from)))
4779 ;;;_ > allout-show-current-branches ()
4780 (defun allout-show-current-branches ()
4781 "Show all subheadings of this heading, but not their bodies."
4782 (interactive)
4783 (let ((inhibit-field-text-motion t))
4784 (beginning-of-line))
4785 (allout-show-children t))
4786 ;;;_ > allout-hide-current-leaves ()
4787 (defun allout-hide-current-leaves ()
4788 "Hide the bodies of the current topic and all its offspring."
4789 (interactive)
4790 (allout-back-to-current-heading)
4791 (allout-hide-region-body (point) (progn (allout-end-of-current-subtree)
4792 (point))))
4793
4794 ;;;_ - Region and beyond
4795 ;;;_ > allout-show-all ()
4796 (defun allout-show-all ()
4797 "Show all of the text in the buffer."
4798 (interactive)
4799 (message "Exposing entire buffer...")
4800 (allout-flag-region (point-min) (point-max) nil)
4801 (message "Exposing entire buffer... Done."))
4802 ;;;_ > allout-hide-bodies ()
4803 (defun allout-hide-bodies ()
4804 "Hide all of buffer except headings."
4805 (interactive)
4806 (allout-hide-region-body (point-min) (point-max)))
4807 ;;;_ > allout-hide-region-body (start end)
4808 (defun allout-hide-region-body (start end)
4809 "Hide all body lines in the region, but not headings."
4810 (save-excursion
4811 (save-restriction
4812 (narrow-to-region start end)
4813 (goto-char (point-min))
4814 (let ((inhibit-field-text-motion t))
4815 (while (not (eobp))
4816 (end-of-line)
4817 (allout-flag-region (point) (allout-end-of-entry) t)
4818 (if (not (eobp))
4819 (forward-char
4820 (if (looking-at "\n\n")
4821 2 1))))))))
4822
4823 ;;;_ > allout-expose-topic (spec)
4824 (defun allout-expose-topic (spec)
4825 "Apply exposure specs to successive outline topic items.
4826
4827 Use the more convenient frontend, `allout-new-exposure', if you don't
4828 need evaluation of the arguments, or even better, the `allout-layout'
4829 variable-keyed mode-activation/auto-exposure feature of allout outline
4830 mode. See the respective documentation strings for more details.
4831
4832 Cursor is left at start position.
4833
4834 SPEC is either a number or a list.
4835
4836 Successive specs on a list are applied to successive sibling topics.
4837
4838 A simple spec \(either a number, one of a few symbols, or the null
4839 list) dictates the exposure for the corresponding topic.
4840
4841 Non-null lists recursively designate exposure specs for respective
4842 subtopics of the current topic.
4843
4844 The `:' repeat spec is used to specify exposure for any number of
4845 successive siblings, up to the trailing ones for which there are
4846 explicit specs following the `:'.
4847
4848 Simple (numeric and null-list) specs are interpreted as follows:
4849
4850 Numbers indicate the relative depth to open the corresponding topic.
4851 - negative numbers force the topic to be closed before opening to the
4852 absolute value of the number, so all siblings are open only to
4853 that level.
4854 - positive numbers open to the relative depth indicated by the
4855 number, but do not force already opened subtopics to be closed.
4856 - 0 means to close topic - hide all offspring.
4857 : - `repeat'
4858 apply prior element to all siblings at current level, *up to*
4859 those siblings that would be covered by specs following the `:'
4860 on the list. Ie, apply to all topics at level but the last
4861 ones. \(Only first of multiple colons at same level is
4862 respected - subsequent ones are discarded.)
4863 * - completely opens the topic, including bodies.
4864 + - shows all the sub headers, but not the bodies
4865 - - exposes the body of the corresponding topic.
4866
4867 Examples:
4868 \(allout-expose-topic '(-1 : 0))
4869 Close this and all following topics at current level, exposing
4870 only their immediate children, but close down the last topic
4871 at this current level completely.
4872 \(allout-expose-topic '(-1 () : 1 0))
4873 Close current topic so only the immediate subtopics are shown;
4874 show the children in the second to last topic, and completely
4875 close the last one.
4876 \(allout-expose-topic '(-2 : -1 *))
4877 Expose children and grandchildren of all topics at current
4878 level except the last two; expose children of the second to
4879 last and completely open the last one."
4880
4881 (interactive "xExposure spec: ")
4882 (if (not (listp spec))
4883 nil
4884 (let ((depth (allout-depth))
4885 (max-pos 0)
4886 prev-elem curr-elem
4887 stay)
4888 (while spec
4889 (setq prev-elem curr-elem
4890 curr-elem (car spec)
4891 spec (cdr spec))
4892 (cond ; Do current element:
4893 ((null curr-elem) nil)
4894 ((symbolp curr-elem)
4895 (cond ((eq curr-elem '*) (allout-show-current-subtree)
4896 (if (> allout-recent-end-of-subtree max-pos)
4897 (setq max-pos allout-recent-end-of-subtree)))
4898 ((eq curr-elem '+) (allout-show-current-branches)
4899 (if (> allout-recent-end-of-subtree max-pos)
4900 (setq max-pos allout-recent-end-of-subtree)))
4901 ((eq curr-elem '-) (allout-show-current-entry))
4902 ((eq curr-elem ':)
4903 (setq stay t)
4904 ;; Expand the `repeat' spec to an explicit version,
4905 ;; w.r.t. remaining siblings:
4906 (let ((residue ; = # of sibs not covered by remaining spec
4907 ;; Dang - could be nice to make use of the chart, sigh:
4908 (- (length (allout-chart-siblings))
4909 (length spec))))
4910 (if (< 0 residue)
4911 ;; Some residue - cover it with prev-elem:
4912 (setq spec (append (make-list residue prev-elem)
4913 spec)))))))
4914 ((numberp curr-elem)
4915 (if (and (>= 0 curr-elem) (not (allout-hidden-p)))
4916 (save-excursion (allout-hide-current-subtree t)
4917 (if (> 0 curr-elem)
4918 nil
4919 (if (> allout-recent-end-of-subtree max-pos)
4920 (setq max-pos
4921 allout-recent-end-of-subtree)))))
4922 (if (> (abs curr-elem) 0)
4923 (progn (allout-show-children (abs curr-elem))
4924 (if (> allout-recent-end-of-subtree max-pos)
4925 (setq max-pos allout-recent-end-of-subtree)))))
4926 ((listp curr-elem)
4927 (if (allout-descend-to-depth (1+ depth))
4928 (let ((got (allout-expose-topic curr-elem)))
4929 (if (and got (> got max-pos)) (setq max-pos got))))))
4930 (cond (stay (setq stay nil))
4931 ((listp (car spec)) nil)
4932 ((> max-pos (point))
4933 ;; Capitalize on max-pos state to get us nearer next sibling:
4934 (progn (goto-char (min (point-max) max-pos))
4935 (allout-next-heading)))
4936 ((allout-next-sibling depth))))
4937 max-pos)))
4938 ;;;_ > allout-old-expose-topic (spec &rest followers)
4939 (defun allout-old-expose-topic (spec &rest followers)
4940
4941 "Deprecated. Use `allout-expose-topic' \(with different schema
4942 format) instead.
4943
4944 Dictate wholesale exposure scheme for current topic, according to SPEC.
4945
4946 SPEC is either a number or a list. Optional successive args
4947 dictate exposure for subsequent siblings of current topic.
4948
4949 A simple spec (either a number, a special symbol, or the null list)
4950 dictates the overall exposure for a topic. Non null lists are
4951 composite specs whose first element dictates the overall exposure for
4952 a topic, with the subsequent elements in the list interpreted as specs
4953 that dictate the exposure for the successive offspring of the topic.
4954
4955 Simple (numeric and null-list) specs are interpreted as follows:
4956
4957 - Numbers indicate the relative depth to open the corresponding topic:
4958 - negative numbers force the topic to be close before opening to the
4959 absolute value of the number.
4960 - positive numbers just open to the relative depth indicated by the number.
4961 - 0 just closes
4962 - `*' completely opens the topic, including bodies.
4963 - `+' shows all the sub headers, but not the bodies
4964 - `-' exposes the body and immediate offspring of the corresponding topic.
4965
4966 If the spec is a list, the first element must be a number, which
4967 dictates the exposure depth of the topic as a whole. Subsequent
4968 elements of the list are nested SPECs, dictating the specific exposure
4969 for the corresponding offspring of the topic.
4970
4971 Optional FOLLOWERS arguments dictate exposure for succeeding siblings."
4972
4973 (interactive "xExposure spec: ")
4974 (let ((inhibit-field-text-motion t)
4975 (depth (allout-current-depth))
4976 max-pos)
4977 (cond ((null spec) nil)
4978 ((symbolp spec)
4979 (if (eq spec '*) (allout-show-current-subtree))
4980 (if (eq spec '+) (allout-show-current-branches))
4981 (if (eq spec '-) (allout-show-current-entry)))
4982 ((numberp spec)
4983 (if (>= 0 spec)
4984 (save-excursion (allout-hide-current-subtree t)
4985 (end-of-line)
4986 (if (or (not max-pos)
4987 (> (point) max-pos))
4988 (setq max-pos (point)))
4989 (if (> 0 spec)
4990 (setq spec (* -1 spec)))))
4991 (if (> spec 0)
4992 (allout-show-children spec)))
4993 ((listp spec)
4994 ;(let ((got (allout-old-expose-topic (car spec))))
4995 ; (if (and got (or (not max-pos) (> got max-pos)))
4996 ; (setq max-pos got)))
4997 (let ((new-depth (+ (allout-current-depth) 1))
4998 got)
4999 (setq max-pos (allout-old-expose-topic (car spec)))
5000 (setq spec (cdr spec))
5001 (if (and spec
5002 (allout-descend-to-depth new-depth)
5003 (not (allout-hidden-p)))
5004 (progn (setq got (apply 'allout-old-expose-topic spec))
5005 (if (and got (or (not max-pos) (> got max-pos)))
5006 (setq max-pos got)))))))
5007 (while (and followers
5008 (progn (if (and max-pos (< (point) max-pos))
5009 (progn (goto-char max-pos)
5010 (setq max-pos nil)))
5011 (end-of-line)
5012 (allout-next-sibling depth)))
5013 (allout-old-expose-topic (car followers))
5014 (setq followers (cdr followers)))
5015 max-pos))
5016 ;;;_ > allout-new-exposure '()
5017 (defmacro allout-new-exposure (&rest spec)
5018 "Literal frontend for `allout-expose-topic', doesn't evaluate arguments.
5019 Some arguments that would need to be quoted in `allout-expose-topic'
5020 need not be quoted in `allout-new-exposure'.
5021
5022 Cursor is left at start position.
5023
5024 Use this instead of obsolete `allout-exposure'.
5025
5026 Examples:
5027 \(allout-new-exposure (-1 () () () 1) 0)
5028 Close current topic at current level so only the immediate
5029 subtopics are shown, except also show the children of the
5030 third subtopic; and close the next topic at the current level.
5031 \(allout-new-exposure : -1 0)
5032 Close all topics at current level to expose only their
5033 immediate children, except for the last topic at the current
5034 level, in which even its immediate children are hidden.
5035 \(allout-new-exposure -2 : -1 *)
5036 Expose children and grandchildren of first topic at current
5037 level, and expose children of subsequent topics at current
5038 level *except* for the last, which should be opened completely."
5039 (list 'save-excursion
5040 '(if (not (or (allout-goto-prefix-doublechecked)
5041 (allout-next-heading)))
5042 (error "allout-new-exposure: Can't find any outline topics"))
5043 (list 'allout-expose-topic (list 'quote spec))))
5044
5045 ;;;_ #7 Systematic outline presentation - copying, printing, flattening
5046
5047 ;;;_ - Mapping and processing of topics
5048 ;;;_ ( See also Subtree Charting, in Navigation code.)
5049 ;;;_ > allout-stringify-flat-index (flat-index)
5050 (defun allout-stringify-flat-index (flat-index &optional context)
5051 "Convert list representing section/subsection/... to document string.
5052
5053 Optional arg CONTEXT indicates interior levels to include."
5054 (let ((delim ".")
5055 result
5056 numstr
5057 (context-depth (or (and context 2) 1)))
5058 ;; Take care of the explicit context:
5059 (while (> context-depth 0)
5060 (setq numstr (int-to-string (car flat-index))
5061 flat-index (cdr flat-index)
5062 result (if flat-index
5063 (cons delim (cons numstr result))
5064 (cons numstr result))
5065 context-depth (if flat-index (1- context-depth) 0)))
5066 (setq delim " ")
5067 ;; Take care of the indentation:
5068 (if flat-index
5069 (progn
5070 (while flat-index
5071 (setq result
5072 (cons delim
5073 (cons (make-string
5074 (1+ (truncate (if (zerop (car flat-index))
5075 1
5076 (log10 (car flat-index)))))
5077 ? )
5078 result)))
5079 (setq flat-index (cdr flat-index)))
5080 ;; Dispose of single extra delim:
5081 (setq result (cdr result))))
5082 (apply 'concat result)))
5083 ;;;_ > allout-stringify-flat-index-plain (flat-index)
5084 (defun allout-stringify-flat-index-plain (flat-index)
5085 "Convert list representing section/subsection/... to document string."
5086 (let ((delim ".")
5087 result)
5088 (while flat-index
5089 (setq result (cons (int-to-string (car flat-index))
5090 (if result
5091 (cons delim result))))
5092 (setq flat-index (cdr flat-index)))
5093 (apply 'concat result)))
5094 ;;;_ > allout-stringify-flat-index-indented (flat-index)
5095 (defun allout-stringify-flat-index-indented (flat-index)
5096 "Convert list representing section/subsection/... to document string."
5097 (let ((delim ".")
5098 result
5099 numstr)
5100 ;; Take care of the explicit context:
5101 (setq numstr (int-to-string (car flat-index))
5102 flat-index (cdr flat-index)
5103 result (if flat-index
5104 (cons delim (cons numstr result))
5105 (cons numstr result)))
5106 (setq delim " ")
5107 ;; Take care of the indentation:
5108 (if flat-index
5109 (progn
5110 (while flat-index
5111 (setq result
5112 (cons delim
5113 (cons (make-string
5114 (1+ (truncate (if (zerop (car flat-index))
5115 1
5116 (log10 (car flat-index)))))
5117 ? )
5118 result)))
5119 (setq flat-index (cdr flat-index)))
5120 ;; Dispose of single extra delim:
5121 (setq result (cdr result))))
5122 (apply 'concat result)))
5123 ;;;_ > allout-listify-exposed (&optional start end format)
5124 (defun allout-listify-exposed (&optional start end format)
5125
5126 "Produce a list representing exposed topics in current region.
5127
5128 This list can then be used by `allout-process-exposed' to manipulate
5129 the subject region.
5130
5131 Optional START and END indicate bounds of region.
5132
5133 optional arg, FORMAT, designates an alternate presentation form for
5134 the prefix:
5135
5136 list - Present prefix as numeric section.subsection..., starting with
5137 section indicated by the list, innermost nesting first.
5138 `indent' \(symbol) - Convert header prefixes to all white space,
5139 except for distinctive bullets.
5140
5141 The elements of the list produced are lists that represents a topic
5142 header and body. The elements of that list are:
5143
5144 - a number representing the depth of the topic,
5145 - a string representing the header-prefix, including trailing whitespace and
5146 bullet.
5147 - a string representing the bullet character,
5148 - and a series of strings, each containing one line of the exposed
5149 portion of the topic entry."
5150
5151 (interactive "r")
5152 (save-excursion
5153 (let*
5154 ((inhibit-field-text-motion t)
5155 ;; state vars:
5156 strings prefix result depth new-depth out gone-out bullet beg
5157 next done)
5158
5159 (goto-char start)
5160 (beginning-of-line)
5161 ;; Goto initial topic, and register preceeding stuff, if any:
5162 (if (> (allout-goto-prefix-doublechecked) start)
5163 ;; First topic follows beginning point - register preliminary stuff:
5164 (setq result (list (list 0 "" nil
5165 (buffer-substring start (1- (point)))))))
5166 (while (and (not done)
5167 (not (eobp)) ; Loop until we've covered the region.
5168 (not (> (point) end)))
5169 (setq depth allout-recent-depth ; Current topics depth,
5170 bullet (allout-recent-bullet) ; ... bullet,
5171 prefix (allout-recent-prefix)
5172 beg (progn (allout-end-of-prefix t) (point))) ; and beginning.
5173 (setq done ; The boundary for the current topic:
5174 (not (allout-next-visible-heading 1)))
5175 (setq new-depth allout-recent-depth)
5176 (setq gone-out out
5177 out (< new-depth depth))
5178 (beginning-of-line)
5179 (setq next (point))
5180 (goto-char beg)
5181 (setq strings nil)
5182 (while (> next (point)) ; Get all the exposed text in
5183 (setq strings
5184 (cons (buffer-substring
5185 beg
5186 ;To hidden text or end of line:
5187 (progn
5188 (end-of-line)
5189 (allout-back-to-visible-text)))
5190 strings))
5191 (when (< (point) next) ; Resume from after hid text, if any.
5192 (line-move 1)
5193 (beginning-of-line))
5194 (setq beg (point)))
5195 ;; Accumulate list for this topic:
5196 (setq strings (nreverse strings))
5197 (setq result
5198 (cons
5199 (if format
5200 (let ((special (if (string-match
5201 (regexp-quote bullet)
5202 allout-distinctive-bullets-string)
5203 bullet)))
5204 (cond ((listp format)
5205 (list depth
5206 (if allout-abbreviate-flattened-numbering
5207 (allout-stringify-flat-index format
5208 gone-out)
5209 (allout-stringify-flat-index-plain
5210 format))
5211 strings
5212 special))
5213 ((eq format 'indent)
5214 (if special
5215 (list depth
5216 (concat (make-string (1+ depth) ? )
5217 (substring prefix -1))
5218 strings)
5219 (list depth
5220 (make-string depth ? )
5221 strings)))
5222 (t (error "allout-listify-exposed: %s %s"
5223 "invalid format" format))))
5224 (list depth prefix strings))
5225 result))
5226 ;; Reasses format, if any:
5227 (if (and format (listp format))
5228 (cond ((= new-depth depth)
5229 (setq format (cons (1+ (car format))
5230 (cdr format))))
5231 ((> new-depth depth) ; descending - assume by 1:
5232 (setq format (cons 1 format)))
5233 (t
5234 ; Pop the residue:
5235 (while (< new-depth depth)
5236 (setq format (cdr format))
5237 (setq depth (1- depth)))
5238 ; And increment the current one:
5239 (setq format
5240 (cons (1+ (or (car format)
5241 -1))
5242 (cdr format)))))))
5243 ;; Put the list with first at front, to last at back:
5244 (nreverse result))))
5245 ;;;_ > my-region-active-p ()
5246 (defmacro my-region-active-p ()
5247 (if (fboundp 'region-active-p)
5248 '(region-active-p)
5249 'mark-active))
5250 ;;;_ > allout-process-exposed (&optional func from to frombuf
5251 ;;; tobuf format)
5252 (defun allout-process-exposed (&optional func from to frombuf tobuf
5253 format start-num)
5254 "Map function on exposed parts of current topic; results to another buffer.
5255
5256 All args are options; default values itemized below.
5257
5258 Apply FUNCTION to exposed portions FROM position TO position in buffer
5259 FROMBUF to buffer TOBUF. Sixth optional arg, FORMAT, designates an
5260 alternate presentation form:
5261
5262 `flat' - Present prefix as numeric section.subsection..., starting with
5263 section indicated by the start-num, innermost nesting first.
5264 X`flat-indented' - Prefix is like `flat' for first topic at each
5265 X level, but subsequent topics have only leaf topic
5266 X number, padded with blanks to line up with first.
5267 `indent' \(symbol) - Convert header prefixes to all white space,
5268 except for distinctive bullets.
5269
5270 Defaults:
5271 FUNCTION: `allout-insert-listified'
5272 FROM: region start, if region active, else start of buffer
5273 TO: region end, if region active, else end of buffer
5274 FROMBUF: current buffer
5275 TOBUF: buffer name derived: \"*current-buffer-name exposed*\"
5276 FORMAT: nil"
5277
5278 ; Resolve arguments,
5279 ; defaulting if necessary:
5280 (if (not func) (setq func 'allout-insert-listified))
5281 (if (not (and from to))
5282 (if (my-region-active-p)
5283 (setq from (region-beginning) to (region-end))
5284 (setq from (point-min) to (point-max))))
5285 (if frombuf
5286 (if (not (bufferp frombuf))
5287 ;; Specified but not a buffer - get it:
5288 (let ((got (get-buffer frombuf)))
5289 (if (not got)
5290 (error (concat "allout-process-exposed: source buffer "
5291 frombuf
5292 " not found."))
5293 (setq frombuf got))))
5294 ;; not specified - default it:
5295 (setq frombuf (current-buffer)))
5296 (if tobuf
5297 (if (not (bufferp tobuf))
5298 (setq tobuf (get-buffer-create tobuf)))
5299 ;; not specified - default it:
5300 (setq tobuf (concat "*" (buffer-name frombuf) " exposed*")))
5301 (if (listp format)
5302 (nreverse format))
5303
5304 (let* ((listified
5305 (progn (set-buffer frombuf)
5306 (allout-listify-exposed from to format))))
5307 (set-buffer tobuf)
5308 (mapcar func listified)
5309 (pop-to-buffer tobuf)))
5310
5311 ;;;_ - Copy exposed
5312 ;;;_ > allout-insert-listified (listified)
5313 (defun allout-insert-listified (listified)
5314 "Insert contents of listified outline portion in current buffer.
5315
5316 LISTIFIED is a list representing each topic header and body:
5317
5318 \`(depth prefix text)'
5319
5320 or \`(depth prefix text bullet-plus)'
5321
5322 If `bullet-plus' is specified, it is inserted just after the entire prefix."
5323 (setq listified (cdr listified))
5324 (let ((prefix (prog1
5325 (car listified)
5326 (setq listified (cdr listified))))
5327 (text (prog1
5328 (car listified)
5329 (setq listified (cdr listified))))
5330 (bullet-plus (car listified)))
5331 (insert prefix)
5332 (if bullet-plus (insert (concat " " bullet-plus)))
5333 (while text
5334 (insert (car text))
5335 (if (setq text (cdr text))
5336 (insert "\n")))
5337 (insert "\n")))
5338 ;;;_ > allout-copy-exposed-to-buffer (&optional arg tobuf format)
5339 (defun allout-copy-exposed-to-buffer (&optional arg tobuf format)
5340 "Duplicate exposed portions of current outline to another buffer.
5341
5342 Other buffer has current buffers name with \" exposed\" appended to it.
5343
5344 With repeat count, copy the exposed parts of only the current topic.
5345
5346 Optional second arg TOBUF is target buffer name.
5347
5348 Optional third arg FORMAT, if non-nil, symbolically designates an
5349 alternate presentation format for the outline:
5350
5351 `flat' - Convert topic header prefixes to numeric
5352 section.subsection... identifiers.
5353 `indent' - Convert header prefixes to all white space, except for
5354 distinctive bullets.
5355 `indent-flat' - The best of both - only the first of each level has
5356 the full path, the rest have only the section number
5357 of the leaf, preceded by the right amount of indentation."
5358
5359 (interactive "P")
5360 (if (not tobuf)
5361 (setq tobuf (get-buffer-create (concat "*" (buffer-name) " exposed*"))))
5362 (let* ((start-pt (point))
5363 (beg (if arg (allout-back-to-current-heading) (point-min)))
5364 (end (if arg (allout-end-of-current-subtree) (point-max)))
5365 (buf (current-buffer))
5366 (start-list ()))
5367 (if (eq format 'flat)
5368 (setq format (if arg (save-excursion
5369 (goto-char beg)
5370 (allout-topic-flat-index))
5371 '(1))))
5372 (save-excursion (set-buffer tobuf)(erase-buffer))
5373 (allout-process-exposed 'allout-insert-listified
5374 beg
5375 end
5376 (current-buffer)
5377 tobuf
5378 format start-list)
5379 (goto-char (point-min))
5380 (pop-to-buffer buf)
5381 (goto-char start-pt)))
5382 ;;;_ > allout-flatten-exposed-to-buffer (&optional arg tobuf)
5383 (defun allout-flatten-exposed-to-buffer (&optional arg tobuf)
5384 "Present numeric outline of outline's exposed portions in another buffer.
5385
5386 The resulting outline is not compatible with outline mode - use
5387 `allout-copy-exposed-to-buffer' if you want that.
5388
5389 Use `allout-indented-exposed-to-buffer' for indented presentation.
5390
5391 With repeat count, copy the exposed portions of only current topic.
5392
5393 Other buffer has current buffer's name with \" exposed\" appended to
5394 it, unless optional second arg TOBUF is specified, in which case it is
5395 used verbatim."
5396 (interactive "P")
5397 (allout-copy-exposed-to-buffer arg tobuf 'flat))
5398 ;;;_ > allout-indented-exposed-to-buffer (&optional arg tobuf)
5399 (defun allout-indented-exposed-to-buffer (&optional arg tobuf)
5400 "Present indented outline of outline's exposed portions in another buffer.
5401
5402 The resulting outline is not compatible with outline mode - use
5403 `allout-copy-exposed-to-buffer' if you want that.
5404
5405 Use `allout-flatten-exposed-to-buffer' for numeric sectional presentation.
5406
5407 With repeat count, copy the exposed portions of only current topic.
5408
5409 Other buffer has current buffer's name with \" exposed\" appended to
5410 it, unless optional second arg TOBUF is specified, in which case it is
5411 used verbatim."
5412 (interactive "P")
5413 (allout-copy-exposed-to-buffer arg tobuf 'indent))
5414
5415 ;;;_ - LaTeX formatting
5416 ;;;_ > allout-latex-verb-quote (string &optional flow)
5417 (defun allout-latex-verb-quote (string &optional flow)
5418 "Return copy of STRING for literal reproduction across LaTeX processing.
5419 Expresses the original characters \(including carriage returns) of the
5420 string across LaTeX processing."
5421 (mapconcat (function
5422 (lambda (char)
5423 (cond ((memq char '(?\\ ?$ ?% ?# ?& ?{ ?} ?_ ?^ ?- ?*))
5424 (concat "\\char" (number-to-string char) "{}"))
5425 ((= char ?\n) "\\\\")
5426 (t (char-to-string char)))))
5427 string
5428 ""))
5429 ;;;_ > allout-latex-verbatim-quote-curr-line ()
5430 (defun allout-latex-verbatim-quote-curr-line ()
5431 "Express line for exact \(literal) representation across LaTeX processing.
5432
5433 Adjust line contents so it is unaltered \(from the original line)
5434 across LaTeX processing, within the context of a `verbatim'
5435 environment. Leaves point at the end of the line."
5436 (let ((inhibit-field-text-motion t))
5437 (beginning-of-line)
5438 (let ((beg (point))
5439 (end (progn (end-of-line)(point))))
5440 (goto-char beg)
5441 (while (re-search-forward "\\\\"
5442 ;;"\\\\\\|\\{\\|\\}\\|\\_\\|\\$\\|\\\"\\|\\&\\|\\^\\|\\-\\|\\*\\|#"
5443 end ; bounded by end-of-line
5444 1) ; no matches, move to end & return nil
5445 (goto-char (match-beginning 2))
5446 (insert "\\")
5447 (setq end (1+ end))
5448 (goto-char (1+ (match-end 2)))))))
5449 ;;;_ > allout-insert-latex-header (buffer)
5450 (defun allout-insert-latex-header (buffer)
5451 "Insert initial LaTeX commands at point in BUFFER."
5452 ;; Much of this is being derived from the stuff in appendix of E in
5453 ;; the TeXBook, pg 421.
5454 (set-buffer buffer)
5455 (let ((doc-style (format "\n\\documentstyle{%s}\n"
5456 "report"))
5457 (page-numbering (if allout-number-pages
5458 "\\pagestyle{empty}\n"
5459 ""))
5460 (titlecmd (format "\\newcommand{\\titlecmd}[1]{{%s #1}}\n"
5461 allout-title-style))
5462 (labelcmd (format "\\newcommand{\\labelcmd}[1]{{%s #1}}\n"
5463 allout-label-style))
5464 (headlinecmd (format "\\newcommand{\\headlinecmd}[1]{{%s #1}}\n"
5465 allout-head-line-style))
5466 (bodylinecmd (format "\\newcommand{\\bodylinecmd}[1]{{%s #1}}\n"
5467 allout-body-line-style))
5468 (setlength (format "%s%s%s%s"
5469 "\\newlength{\\stepsize}\n"
5470 "\\setlength{\\stepsize}{"
5471 allout-indent
5472 "}\n"))
5473 (oneheadline (format "%s%s%s%s%s%s%s"
5474 "\\newcommand{\\OneHeadLine}[3]{%\n"
5475 "\\noindent%\n"
5476 "\\hspace*{#2\\stepsize}%\n"
5477 "\\labelcmd{#1}\\hspace*{.2cm}"
5478 "\\headlinecmd{#3}\\\\["
5479 allout-line-skip
5480 "]\n}\n"))
5481 (onebodyline (format "%s%s%s%s%s%s"
5482 "\\newcommand{\\OneBodyLine}[2]{%\n"
5483 "\\noindent%\n"
5484 "\\hspace*{#1\\stepsize}%\n"
5485 "\\bodylinecmd{#2}\\\\["
5486 allout-line-skip
5487 "]\n}\n"))
5488 (begindoc "\\begin{document}\n\\begin{center}\n")
5489 (title (format "%s%s%s%s"
5490 "\\titlecmd{"
5491 (allout-latex-verb-quote (if allout-title
5492 (condition-case nil
5493 (eval allout-title)
5494 (error "<unnamed buffer>"))
5495 "Unnamed Outline"))
5496 "}\n"
5497 "\\end{center}\n\n"))
5498 (hsize "\\hsize = 7.5 true in\n")
5499 (hoffset "\\hoffset = -1.5 true in\n")
5500 (vspace "\\vspace{.1cm}\n\n"))
5501 (insert (concat doc-style
5502 page-numbering
5503 titlecmd
5504 labelcmd
5505 headlinecmd
5506 bodylinecmd
5507 setlength
5508 oneheadline
5509 onebodyline
5510 begindoc
5511 title
5512 hsize
5513 hoffset
5514 vspace)
5515 )))
5516 ;;;_ > allout-insert-latex-trailer (buffer)
5517 (defun allout-insert-latex-trailer (buffer)
5518 "Insert concluding LaTeX commands at point in BUFFER."
5519 (set-buffer buffer)
5520 (insert "\n\\end{document}\n"))
5521 ;;;_ > allout-latexify-one-item (depth prefix bullet text)
5522 (defun allout-latexify-one-item (depth prefix bullet text)
5523 "Insert LaTeX commands for formatting one outline item.
5524
5525 Args are the topics numeric DEPTH, the header PREFIX lead string, the
5526 BULLET string, and a list of TEXT strings for the body."
5527 (let* ((head-line (if text (car text)))
5528 (body-lines (cdr text))
5529 (curr-line)
5530 body-content bop)
5531 ; Do the head line:
5532 (insert (concat "\\OneHeadLine{\\verb\1 "
5533 (allout-latex-verb-quote bullet)
5534 "\1}{"
5535 depth
5536 "}{\\verb\1 "
5537 (if head-line
5538 (allout-latex-verb-quote head-line)
5539 "")
5540 "\1}\n"))
5541 (if (not body-lines)
5542 nil
5543 ;;(insert "\\beginlines\n")
5544 (insert "\\begin{verbatim}\n")
5545 (while body-lines
5546 (setq curr-line (car body-lines))
5547 (if (and (not body-content)
5548 (not (string-match "^\\s-*$" curr-line)))
5549 (setq body-content t))
5550 ; Mangle any occurrences of
5551 ; "\end{verbatim}" in text,
5552 ; it's special:
5553 (if (and body-content
5554 (setq bop (string-match "\\end{verbatim}" curr-line)))
5555 (setq curr-line (concat (substring curr-line 0 bop)
5556 ">"
5557 (substring curr-line bop))))
5558 ;;(insert "|" (car body-lines) "|")
5559 (insert curr-line)
5560 (allout-latex-verbatim-quote-curr-line)
5561 (insert "\n")
5562 (setq body-lines (cdr body-lines)))
5563 (if body-content
5564 (setq body-content nil)
5565 (forward-char -1)
5566 (insert "\\ ")
5567 (forward-char 1))
5568 ;;(insert "\\endlines\n")
5569 (insert "\\end{verbatim}\n")
5570 )))
5571 ;;;_ > allout-latexify-exposed (arg &optional tobuf)
5572 (defun allout-latexify-exposed (arg &optional tobuf)
5573 "Format current topics exposed portions to TOBUF for LaTeX processing.
5574 TOBUF defaults to a buffer named the same as the current buffer, but
5575 with \"*\" prepended and \" latex-formed*\" appended.
5576
5577 With repeat count, copy the exposed portions of entire buffer."
5578
5579 (interactive "P")
5580 (if (not tobuf)
5581 (setq tobuf
5582 (get-buffer-create (concat "*" (buffer-name) " latexified*"))))
5583 (let* ((start-pt (point))
5584 (beg (if arg (point-min) (allout-back-to-current-heading)))
5585 (end (if arg (point-max) (allout-end-of-current-subtree)))
5586 (buf (current-buffer)))
5587 (set-buffer tobuf)
5588 (erase-buffer)
5589 (allout-insert-latex-header tobuf)
5590 (goto-char (point-max))
5591 (allout-process-exposed 'allout-latexify-one-item
5592 beg
5593 end
5594 buf
5595 tobuf)
5596 (goto-char (point-max))
5597 (allout-insert-latex-trailer tobuf)
5598 (goto-char (point-min))
5599 (pop-to-buffer buf)
5600 (goto-char start-pt)))
5601
5602 ;;;_ #8 Encryption
5603 ;;;_ > allout-toggle-current-subtree-encryption (&optional fetch-pass)
5604 (defun allout-toggle-current-subtree-encryption (&optional fetch-pass)
5605 "Encrypt clear or decrypt encoded text of visibly-containing topic's contents.
5606
5607 Optional FETCH-PASS universal argument provokes key-pair encryption with
5608 single universal argument. With doubled universal argument \(value = 16),
5609 it forces prompting for the passphrase regardless of availability from the
5610 passphrase cache. With no universal argument, the appropriate passphrase
5611 is obtained from the cache, if available, else from the user.
5612
5613 Currently only GnuPG encryption is supported.
5614
5615 \**NOTE WELL** that the encrypted text must be ascii-armored. For gnupg
5616 encryption, include the option ``armor'' in your ~/.gnupg/gpg.conf file.
5617
5618 Both symmetric-key and key-pair encryption is implemented. Symmetric is
5619 the default, use a single \(x4) universal argument for keypair mode.
5620
5621 Encrypted topic's bullet is set to a `~' to signal that the contents of the
5622 topic \(body and subtopics, but not heading) is pending encryption or
5623 encrypted. `*' asterisk immediately after the bullet signals that the body
5624 is encrypted, its' absence means the topic is meant to be encrypted but is
5625 not. When a file with topics pending encryption is saved, topics pending
5626 encryption are encrypted. See allout-encrypt-unencrypted-on-saves for
5627 auto-encryption specifics.
5628
5629 \**NOTE WELL** that automatic encryption that happens during saves will
5630 default to symmetric encryption - you must manually \(re)encrypt key-pair
5631 encrypted topics if you want them to continue to use the key-pair cipher.
5632
5633 Level-one topics, with prefix consisting solely of an `*' asterisk, cannot be
5634 encrypted. If you want to encrypt the contents of a top-level topic, use
5635 \\[allout-shift-in] to increase its depth.
5636
5637 Passphrase Caching
5638
5639 The encryption passphrase is solicited if not currently available in the
5640 passphrase cache from a recent encryption action.
5641
5642 The solicited passphrase is retained for reuse in a buffer-specific cache
5643 for some set period of time \(default, 60 seconds), after which the string
5644 is nulled. The passphrase cache timeout is customized by setting
5645 `pgg-passphrase-cache-expiry'.
5646
5647 Symmetric Passphrase Hinting and Verification
5648
5649 If the file previously had no associated passphrase, or had a different
5650 passphrase than specified, the user is prompted to repeat the new one for
5651 corroboration. A random string encrypted by the new passphrase is set on
5652 the buffer-specific variable `allout-passphrase-verifier-string', for
5653 confirmation of the passphrase when next obtained, before encrypting or
5654 decrypting anything with it. This helps avoid mistakenly shifting between
5655 keys.
5656
5657 If allout customization var `allout-passphrase-verifier-handling' is
5658 non-nil, an entry for `allout-passphrase-verifier-string' and its value is
5659 added to an Emacs 'local variables' section at the end of the file, which
5660 is created if necessary. That setting is for retention of the passphrase
5661 verifier across emacs sessions.
5662
5663 Similarly, `allout-passphrase-hint-string' stores a user-provided reminder
5664 about their passphrase, and `allout-passphrase-hint-handling' specifies
5665 when the hint is presented, or if passphrase hints are disabled. If
5666 enabled \(see the `allout-passphrase-hint-handling' docstring for details),
5667 the hint string is stored in the local-variables section of the file, and
5668 solicited whenever the passphrase is changed."
5669 (interactive "P")
5670 (save-excursion
5671 (allout-back-to-current-heading)
5672 (allout-toggle-subtree-encryption fetch-pass)
5673 )
5674 )
5675 ;;;_ > allout-toggle-subtree-encryption (&optional fetch-pass)
5676 (defun allout-toggle-subtree-encryption (&optional fetch-pass)
5677 "Encrypt clear text or decrypt encoded topic contents \(body and subtopics.)
5678
5679 Optional FETCH-PASS universal argument provokes key-pair encryption with
5680 single universal argument. With doubled universal argument \(value = 16),
5681 it forces prompting for the passphrase regardless of availability from the
5682 passphrase cache. With no universal argument, the appropriate passphrase
5683 is obtained from the cache, if available, else from the user.
5684
5685 Currently only GnuPG encryption is supported.
5686
5687 \**NOTE WELL** that the encrypted text must be ascii-armored. For gnupg
5688 encryption, include the option ``armor'' in your ~/.gnupg/gpg.conf file.
5689
5690 See `allout-toggle-current-subtree-encryption' for more details."
5691
5692 (interactive "P")
5693 (save-excursion
5694 (allout-end-of-prefix t)
5695
5696 (if (= allout-recent-depth 1)
5697 (error (concat "Cannot encrypt or decrypt level 1 topics -"
5698 " shift it in to make it encryptable")))
5699
5700 (let* ((allout-buffer (current-buffer))
5701 ;; Asses location:
5702 (bullet-pos allout-recent-prefix-beginning)
5703 (after-bullet-pos (point))
5704 (was-encrypted
5705 (progn (if (= (point-max) after-bullet-pos)
5706 (error "no body to encrypt"))
5707 (allout-encrypted-topic-p)))
5708 (was-collapsed (if (not (search-forward "\n" nil t))
5709 nil
5710 (backward-char 1)
5711 (allout-hidden-p)))
5712 (subtree-beg (1+ (point)))
5713 (subtree-end (allout-end-of-subtree))
5714 (subject-text (buffer-substring-no-properties subtree-beg
5715 subtree-end))
5716 (subtree-end-char (char-after (1- subtree-end)))
5717 (subtree-trailing-char (char-after subtree-end))
5718 ;; kluge - result-text needs to be nil, but we also want to
5719 ;; check for the error condition
5720 (result-text (if (or (string= "" subject-text)
5721 (string= "\n" subject-text))
5722 (error "No topic contents to %scrypt"
5723 (if was-encrypted "de" "en"))
5724 nil))
5725 ;; Assess key parameters:
5726 (key-info (or
5727 ;; detect the type by which it is already encrypted
5728 (and was-encrypted
5729 (allout-encrypted-key-info subject-text))
5730 (and (member fetch-pass '(4 (4)))
5731 '(keypair nil))
5732 '(symmetric nil)))
5733 (for-key-type (car key-info))
5734 (for-key-identity (cadr key-info))
5735 (fetch-pass (and fetch-pass (member fetch-pass '(16 (16))))))
5736
5737 (setq result-text
5738 (allout-encrypt-string subject-text was-encrypted
5739 (current-buffer)
5740 for-key-type for-key-identity fetch-pass))
5741
5742 ;; Replace the subtree with the processed product.
5743 (allout-unprotected
5744 (progn
5745 (set-buffer allout-buffer)
5746 (delete-region subtree-beg subtree-end)
5747 (insert result-text)
5748 (if was-collapsed
5749 (allout-flag-region (1- subtree-beg) (point) t))
5750 ;; adjust trailing-blank-lines to preserve topic spacing:
5751 (if (not was-encrypted)
5752 (if (and (= subtree-end-char ?\n)
5753 (= subtree-trailing-char ?\n))
5754 (insert subtree-trailing-char)))
5755 ;; Ensure that the item has an encrypted-entry bullet:
5756 (if (not (string= (buffer-substring-no-properties
5757 (1- after-bullet-pos) after-bullet-pos)
5758 allout-topic-encryption-bullet))
5759 (progn (goto-char (1- after-bullet-pos))
5760 (delete-char 1)
5761 (insert allout-topic-encryption-bullet)))
5762 (if was-encrypted
5763 ;; Remove the is-encrypted bullet qualifier:
5764 (progn (goto-char after-bullet-pos)
5765 (delete-char 1))
5766 ;; Add the is-encrypted bullet qualifier:
5767 (goto-char after-bullet-pos)
5768 (insert "*"))))
5769 (run-hook-with-args 'allout-structure-added-hook
5770 bullet-pos subtree-end))))
5771 ;;;_ > allout-encrypt-string (text decrypt allout-buffer key-type for-key
5772 ;;; fetch-pass &optional retried verifying
5773 ;;; passphrase)
5774 (defun allout-encrypt-string (text decrypt allout-buffer key-type for-key
5775 fetch-pass &optional retried rejected
5776 verifying passphrase)
5777 "Encrypt or decrypt message TEXT.
5778
5779 If DECRYPT is true (default false), then decrypt instead of encrypt.
5780
5781 FETCH-PASS (default false) forces fresh prompting for the passphrase.
5782
5783 KEY-TYPE indicates whether to use a 'symmetric or 'keypair cipher.
5784
5785 FOR-KEY is human readable identification of the first of the user's
5786 eligible secret keys a keypair decryption targets, or else nil.
5787
5788 Optional RETRIED is for internal use - conveys the number of failed keys
5789 that have been solicited in sequence leading to this current call.
5790
5791 Optional PASSPHRASE enables explicit delivery of the decryption passphrase,
5792 for verification purposes.
5793
5794 Optional REJECTED is for internal use - conveys the number of
5795 rejections due to matches against
5796 `allout-encryption-ciphertext-rejection-regexps', as limited by
5797 `allout-encryption-ciphertext-rejection-ceiling'.
5798
5799 Returns the resulting string, or nil if the transformation fails."
5800
5801 (require 'pgg)
5802
5803 (if (not (fboundp 'pgg-encrypt-symmetric))
5804 (error "Allout encryption depends on a newer version of pgg"))
5805
5806 (let* ((scheme (upcase
5807 (format "%s" (or pgg-scheme pgg-default-scheme "GPG"))))
5808 (for-key (and (equal key-type 'keypair)
5809 (or for-key
5810 (split-string (read-string
5811 (format "%s message recipients: "
5812 scheme))
5813 "[ \t,]+"))))
5814 (target-prompt-id (if (equal key-type 'keypair)
5815 (if (= (length for-key) 1)
5816 (car for-key) for-key)
5817 (buffer-name allout-buffer)))
5818 (target-cache-id (format "%s-%s"
5819 key-type
5820 (if (equal key-type 'keypair)
5821 target-prompt-id
5822 (or (buffer-file-name allout-buffer)
5823 target-prompt-id))))
5824 (strip-plaintext-regexps
5825 (if (not decrypt)
5826 (allout-get-configvar-values
5827 'allout-encryption-plaintext-sanitization-regexps)))
5828 (reject-ciphertext-regexps
5829 (if (not decrypt)
5830 (allout-get-configvar-values
5831 'allout-encryption-ciphertext-rejection-regexps)))
5832 (rejected (or rejected 0))
5833 (rejections-left (- allout-encryption-ciphertext-rejection-ceiling
5834 rejected))
5835 result-text status)
5836
5837 (if (and fetch-pass (not passphrase))
5838 ;; Force later fetch by evicting passphrase from the cache.
5839 (pgg-remove-passphrase-from-cache target-cache-id t))
5840
5841 (catch 'encryption-failed
5842
5843 ;; Obtain the passphrase if we don't already have one and we're not
5844 ;; doing a keypair encryption:
5845 (if (not (or passphrase
5846 (and (equal key-type 'keypair)
5847 (not decrypt))))
5848
5849 (setq passphrase (allout-obtain-passphrase for-key
5850 target-cache-id
5851 target-prompt-id
5852 key-type
5853 allout-buffer
5854 retried fetch-pass)))
5855
5856 (with-temp-buffer
5857
5858 (insert text)
5859
5860 (when (and strip-plaintext-regexps (not decrypt))
5861 (dolist (re strip-plaintext-regexps)
5862 (let ((re (if (listp re) (car re) re))
5863 (replacement (if (listp re) (cadr re) "")))
5864 (goto-char (point-min))
5865 (while (re-search-forward re nil t)
5866 (replace-match replacement nil nil)))))
5867
5868 (cond
5869
5870 ;; symmetric:
5871 ((equal key-type 'symmetric)
5872 (setq status
5873 (if decrypt
5874
5875 (pgg-decrypt (point-min) (point-max) passphrase)
5876
5877 (pgg-encrypt-symmetric (point-min) (point-max)
5878 passphrase)))
5879
5880 (if status
5881 (pgg-situate-output (point-min) (point-max))
5882 ;; failed - handle passphrase caching
5883 (if verifying
5884 (throw 'encryption-failed nil)
5885 (pgg-remove-passphrase-from-cache target-cache-id t)
5886 (error "Symmetric-cipher %scryption failed - %s"
5887 (if decrypt "de" "en")
5888 "try again with different passphrase."))))
5889
5890 ;; encrypt 'keypair:
5891 ((not decrypt)
5892
5893 (setq status
5894
5895 (pgg-encrypt for-key
5896 nil (point-min) (point-max) passphrase))
5897
5898 (if status
5899 (pgg-situate-output (point-min) (point-max))
5900 (error (pgg-remove-passphrase-from-cache target-cache-id t)
5901 (error "encryption failed"))))
5902
5903 ;; decrypt 'keypair:
5904 (t
5905
5906 (setq status
5907 (pgg-decrypt (point-min) (point-max) passphrase))
5908
5909 (if status
5910 (pgg-situate-output (point-min) (point-max))
5911 (error (pgg-remove-passphrase-from-cache target-cache-id t)
5912 (error "decryption failed")))))
5913
5914 (setq result-text
5915 (buffer-substring-no-properties
5916 1 (- (point-max) (if decrypt 0 1))))
5917 )
5918
5919 ;; validate result - non-empty
5920 (cond ((not result-text)
5921 (if verifying
5922 nil
5923 ;; transform was fruitless, retry w/new passphrase.
5924 (pgg-remove-passphrase-from-cache target-cache-id t)
5925 (allout-encrypt-string text decrypt allout-buffer
5926 key-type for-key nil
5927 (if retried (1+ retried) 1)
5928 rejected verifying nil)))
5929
5930 ;; Retry (within limit) if ciphertext contains rejections:
5931 ((and (not decrypt)
5932 ;; Check for disqualification of this ciphertext:
5933 (let ((regexps reject-ciphertext-regexps)
5934 reject-it)
5935 (while (and regexps (not reject-it))
5936 (setq reject-it (string-match (car regexps)
5937 result-text))
5938 (pop regexps))
5939 reject-it))
5940 (setq rejections-left (1- rejections-left))
5941 (if (<= rejections-left 0)
5942 (error (concat "Ciphertext rejected too many times"
5943 " (%s), per `%s'")
5944 allout-encryption-ciphertext-rejection-ceiling
5945 'allout-encryption-ciphertext-rejection-regexps)
5946 (allout-encrypt-string text decrypt allout-buffer
5947 key-type for-key nil
5948 retried (1+ rejected)
5949 verifying passphrase)))
5950 ;; Barf if encryption yields extraordinary control chars:
5951 ((and (not decrypt)
5952 (string-match "[\C-a\C-k\C-o-\C-z\C-@]"
5953 result-text))
5954 (error (concat "Encryption produced non-armored text, which"
5955 "conflicts with allout mode - reconfigure!")))
5956
5957 ;; valid result and just verifying or non-symmetric:
5958 ((or verifying (not (equal key-type 'symmetric)))
5959 (if (or verifying decrypt)
5960 (pgg-add-passphrase-to-cache target-cache-id
5961 passphrase t))
5962 result-text)
5963
5964 ;; valid result and regular symmetric - "register"
5965 ;; passphrase with mnemonic aids/cache.
5966 (t
5967 (set-buffer allout-buffer)
5968 (if passphrase
5969 (pgg-add-passphrase-to-cache target-cache-id
5970 passphrase t))
5971 (allout-update-passphrase-mnemonic-aids for-key passphrase
5972 allout-buffer)
5973 result-text)
5974 )
5975 )
5976 )
5977 )
5978 ;;;_ > allout-obtain-passphrase (for-key cache-id prompt-id key-type
5979 ;;; allout-buffer retried fetch-pass)
5980 (defun allout-obtain-passphrase (for-key cache-id prompt-id key-type
5981 allout-buffer retried fetch-pass)
5982 "Obtain passphrase for a key from the cache or else from the user.
5983
5984 When obtaining from the user, symmetric-cipher passphrases are verified
5985 against either, if available and enabled, a random string that was
5986 encrypted against the passphrase, or else against repeated entry by the
5987 user for corroboration.
5988
5989 FOR-KEY is the key for which the passphrase is being obtained.
5990
5991 CACHE-ID is the cache id of the key for the passphrase.
5992
5993 PROMPT-ID is the id for use when prompting the user.
5994
5995 KEY-TYPE is either 'symmetric or 'keypair.
5996
5997 ALLOUT-BUFFER is the buffer containing the entry being en/decrypted.
5998
5999 RETRIED is the number of this attempt to obtain this passphrase.
6000
6001 FETCH-PASS causes the passphrase to be solicited from the user, regardless
6002 of the availability of a cached copy."
6003
6004 (if (not (equal key-type 'symmetric))
6005 ;; do regular passphrase read on non-symmetric passphrase:
6006 (pgg-read-passphrase (format "%s passphrase%s: "
6007 (upcase (format "%s" (or pgg-scheme
6008 pgg-default-scheme
6009 "GPG")))
6010 (if prompt-id
6011 (format " for %s" prompt-id)
6012 ""))
6013 cache-id t)
6014
6015 ;; Symmetric hereon:
6016
6017 (save-excursion
6018 (set-buffer allout-buffer)
6019 (let* ((hint (if (and (not (string= allout-passphrase-hint-string ""))
6020 (or (equal allout-passphrase-hint-handling 'always)
6021 (and (equal allout-passphrase-hint-handling
6022 'needed)
6023 retried)))
6024 (format " [%s]" allout-passphrase-hint-string)
6025 ""))
6026 (retry-message (if retried (format " (%s retry)" retried) ""))
6027 (prompt-sans-hint (format "'%s' symmetric passphrase%s: "
6028 prompt-id retry-message))
6029 (full-prompt (format "'%s' symmetric passphrase%s%s: "
6030 prompt-id hint retry-message))
6031 (prompt full-prompt)
6032 (verifier-string (allout-get-encryption-passphrase-verifier))
6033
6034 (cached (and (not fetch-pass)
6035 (pgg-read-passphrase-from-cache cache-id t)))
6036 (got-pass (or cached
6037 (pgg-read-passphrase full-prompt cache-id t)))
6038 confirmation)
6039
6040 (if (not got-pass)
6041 nil
6042
6043 ;; Duplicate our handle on the passphrase so it's not clobbered by
6044 ;; deactivate-passwd memory clearing:
6045 (setq got-pass (copy-sequence got-pass))
6046
6047 (cond (verifier-string
6048 (save-window-excursion
6049 (if (allout-encrypt-string verifier-string 'decrypt
6050 allout-buffer 'symmetric
6051 for-key nil 0 0 'verifying
6052 (copy-sequence got-pass))
6053 (setq confirmation (format "%s" got-pass))))
6054
6055 (if (and (not confirmation)
6056 (if (yes-or-no-p
6057 (concat "Passphrase differs from established"
6058 " - use new one instead? "))
6059 ;; deactivate password for subsequent
6060 ;; confirmation:
6061 (progn
6062 (pgg-remove-passphrase-from-cache cache-id t)
6063 (setq prompt prompt-sans-hint)
6064 nil)
6065 t))
6066 (progn (pgg-remove-passphrase-from-cache cache-id t)
6067 (error "Wrong passphrase."))))
6068 ;; No verifier string - force confirmation by repetition of
6069 ;; (new) passphrase:
6070 ((or fetch-pass (not cached))
6071 (pgg-remove-passphrase-from-cache cache-id t))))
6072 ;; confirmation vs new input - doing pgg-read-passphrase will do the
6073 ;; right thing, in either case:
6074 (if (not confirmation)
6075 (setq confirmation
6076 (pgg-read-passphrase (concat prompt
6077 " ... confirm spelling: ")
6078 cache-id t)))
6079 (prog1
6080 (if (equal got-pass confirmation)
6081 confirmation
6082 (if (yes-or-no-p (concat "spelling of original and"
6083 " confirmation differ - retry? "))
6084 (progn (setq retried (if retried (1+ retried) 1))
6085 (pgg-remove-passphrase-from-cache cache-id t)
6086 ;; recurse to this routine:
6087 (pgg-read-passphrase prompt-sans-hint cache-id t))
6088 (pgg-remove-passphrase-from-cache cache-id t)
6089 (error "Confirmation failed."))))))))
6090 ;;;_ > allout-encrypted-topic-p ()
6091 (defun allout-encrypted-topic-p ()
6092 "True if the current topic is encryptable and encrypted."
6093 (save-excursion
6094 (allout-end-of-prefix t)
6095 (and (string= (buffer-substring-no-properties (1- (point)) (point))
6096 allout-topic-encryption-bullet)
6097 (looking-at "\\*"))
6098 )
6099 )
6100 ;;;_ > allout-encrypted-key-info (text)
6101 ;; XXX gpg-specific, alas
6102 (defun allout-encrypted-key-info (text)
6103 "Return a pair of the key type and identity of a recipient's secret key.
6104
6105 The key type is one of 'symmetric or 'keypair.
6106
6107 if 'keypair, and some of the user's secret keys are among those for which
6108 the message was encoded, return the identity of the first. otherwise,
6109 return nil for the second item of the pair.
6110
6111 An error is raised if the text is not encrypted."
6112 (require 'pgg-parse)
6113 (save-excursion
6114 (with-temp-buffer
6115 (insert text)
6116 (let* ((parsed-armor (pgg-parse-armor-region (point-min) (point-max)))
6117 (type (if (pgg-gpg-symmetric-key-p parsed-armor)
6118 'symmetric
6119 'keypair))
6120 secret-keys first-secret-key for-key-owner)
6121 (if (equal type 'keypair)
6122 (setq secret-keys (pgg-gpg-lookup-all-secret-keys)
6123 first-secret-key (pgg-gpg-select-matching-key parsed-armor
6124 secret-keys)
6125 for-key-owner (and first-secret-key
6126 (pgg-gpg-lookup-key-owner
6127 first-secret-key))))
6128 (list type (pgg-gpg-key-id-from-key-owner for-key-owner))
6129 )
6130 )
6131 )
6132 )
6133 ;;;_ > allout-create-encryption-passphrase-verifier (passphrase)
6134 (defun allout-create-encryption-passphrase-verifier (passphrase)
6135 "Encrypt random message for later validation of symmetric key's passphrase."
6136 ;; use 20 random ascii characters, across the entire ascii range.
6137 (random t)
6138 (let ((spew (make-string 20 ?\0)))
6139 (dotimes (i (length spew))
6140 (aset spew i (1+ (random 254))))
6141 (allout-encrypt-string spew nil (current-buffer) 'symmetric
6142 nil nil 0 0 passphrase))
6143 )
6144 ;;;_ > allout-update-passphrase-mnemonic-aids (for-key passphrase
6145 ;;; outline-buffer)
6146 (defun allout-update-passphrase-mnemonic-aids (for-key passphrase
6147 outline-buffer)
6148 "Update passphrase verifier and hint strings if necessary.
6149
6150 See `allout-passphrase-verifier-string' and `allout-passphrase-hint-string'
6151 settings.
6152
6153 PASSPHRASE is the passphrase being mnemonicized
6154
6155 OUTLINE-BUFFER is the buffer of the outline being adjusted.
6156
6157 These are used to help the user keep track of the passphrase they use for
6158 symmetric encryption in the file.
6159
6160 Behavior is governed by `allout-passphrase-verifier-handling',
6161 `allout-passphrase-hint-handling', and also, controlling whether the values
6162 are preserved on Emacs local file variables,
6163 `allout-enable-file-variable-adjustment'."
6164
6165 ;; If passphrase doesn't agree with current verifier:
6166 ;; - adjust the verifier
6167 ;; - if passphrase hint handling is enabled, adjust the passphrase hint
6168 ;; - if file var settings are enabled, adjust the file vars
6169
6170 (let* ((new-verifier-needed (not (allout-verify-passphrase
6171 for-key passphrase outline-buffer)))
6172 (new-verifier-string
6173 (if new-verifier-needed
6174 ;; Collapse to a single line and enclose in string quotes:
6175 (subst-char-in-string
6176 ?\n ?\C-a (allout-create-encryption-passphrase-verifier
6177 passphrase))))
6178 new-hint)
6179 (when new-verifier-string
6180 ;; do the passphrase hint first, since it's interactive
6181 (when (and allout-passphrase-hint-handling
6182 (not (equal allout-passphrase-hint-handling 'disabled)))
6183 (setq new-hint
6184 (read-from-minibuffer "Passphrase hint to jog your memory: "
6185 allout-passphrase-hint-string))
6186 (when (not (string= new-hint allout-passphrase-hint-string))
6187 (setq allout-passphrase-hint-string new-hint)
6188 (allout-adjust-file-variable "allout-passphrase-hint-string"
6189 allout-passphrase-hint-string)))
6190 (when allout-passphrase-verifier-handling
6191 (setq allout-passphrase-verifier-string new-verifier-string)
6192 (allout-adjust-file-variable "allout-passphrase-verifier-string"
6193 allout-passphrase-verifier-string))
6194 )
6195 )
6196 )
6197 ;;;_ > allout-get-encryption-passphrase-verifier ()
6198 (defun allout-get-encryption-passphrase-verifier ()
6199 "Return text of the encrypt passphrase verifier, unmassaged, or nil if none.
6200
6201 Derived from value of `allout-passphrase-verifier-string'."
6202
6203 (let ((verifier-string (and (boundp 'allout-passphrase-verifier-string)
6204 allout-passphrase-verifier-string)))
6205 (if verifier-string
6206 ;; Return it uncollapsed
6207 (subst-char-in-string ?\C-a ?\n verifier-string))
6208 )
6209 )
6210 ;;;_ > allout-verify-passphrase (key passphrase allout-buffer)
6211 (defun allout-verify-passphrase (key passphrase allout-buffer)
6212 "True if passphrase successfully decrypts verifier, nil otherwise.
6213
6214 \"Otherwise\" includes absence of passphrase verifier."
6215 (save-excursion
6216 (set-buffer allout-buffer)
6217 (and (boundp 'allout-passphrase-verifier-string)
6218 allout-passphrase-verifier-string
6219 (allout-encrypt-string (allout-get-encryption-passphrase-verifier)
6220 'decrypt allout-buffer 'symmetric
6221 key nil 0 0 'verifying passphrase)
6222 t)))
6223 ;;;_ > allout-next-topic-pending-encryption (&optional except-mark)
6224 (defun allout-next-topic-pending-encryption (&optional except-mark)
6225 "Return the point of the next topic pending encryption, or nil if none.
6226
6227 EXCEPT-MARK identifies a point whose containing topics should be excluded
6228 from encryption. This supports 'except-current mode of
6229 `allout-encrypt-unencrypted-on-saves'.
6230
6231 Such a topic has the allout-topic-encryption-bullet without an
6232 immediately following '*' that would mark the topic as being encrypted. It
6233 must also have content."
6234 (let (done got content-beg)
6235 (while (not done)
6236
6237 (if (not (re-search-forward
6238 (format "\\(\\`\\|\n\\)%s *%s[^*]"
6239 (regexp-quote allout-header-prefix)
6240 (regexp-quote allout-topic-encryption-bullet))
6241 nil t))
6242 (setq got nil
6243 done t)
6244 (goto-char (setq got (match-beginning 0)))
6245 (if (looking-at "\n")
6246 (forward-char 1))
6247 (setq got (point)))
6248
6249 (cond ((not got)
6250 (setq done t))
6251
6252 ((not (search-forward "\n"))
6253 (setq got nil
6254 done t))
6255
6256 ((eobp)
6257 (setq got nil
6258 done t))
6259
6260 (t
6261 (setq content-beg (point))
6262 (backward-char 1)
6263 (allout-end-of-subtree)
6264 (if (or (<= (point) content-beg)
6265 (and except-mark
6266 (<= content-beg except-mark)
6267 (>= (point) except-mark)))
6268 ;; Continue looking
6269 (setq got nil)
6270 ;; Got it!
6271 (setq done t)))
6272 )
6273 )
6274 (if got
6275 (goto-char got))
6276 )
6277 )
6278 ;;;_ > allout-encrypt-decrypted (&optional except-mark)
6279 (defun allout-encrypt-decrypted (&optional except-mark)
6280 "Encrypt topics pending encryption except those containing exemption point.
6281
6282 EXCEPT-MARK identifies a point whose containing topics should be excluded
6283 from encryption. This supports 'except-current mode of
6284 `allout-encrypt-unencrypted-on-saves'.
6285
6286 If a topic that is currently being edited was encrypted, we return a list
6287 containing the location of the topic and the location of the cursor just
6288 before the topic was encrypted. This can be used, eg, to decrypt the topic
6289 and exactly resituate the cursor if this is being done as part of a file
6290 save. See `allout-encrypt-unencrypted-on-saves' for more info."
6291
6292 (interactive "p")
6293 (save-excursion
6294 (let* ((current-mark (point-marker))
6295 (current-mark-position (marker-position current-mark))
6296 was-modified
6297 bo-subtree
6298 editing-topic editing-point)
6299 (goto-char (point-min))
6300 (while (allout-next-topic-pending-encryption except-mark)
6301 (setq was-modified (buffer-modified-p))
6302 (when (save-excursion
6303 (and (boundp 'allout-encrypt-unencrypted-on-saves)
6304 allout-encrypt-unencrypted-on-saves
6305 (setq bo-subtree (re-search-forward "$"))
6306 (not (allout-hidden-p))
6307 (>= current-mark (point))
6308 (allout-end-of-current-subtree)
6309 (<= current-mark (point))))
6310 (setq editing-topic (point)
6311 ;; we had to wait for this 'til now so prior topics are
6312 ;; encrypted, any relevant text shifts are in place:
6313 editing-point (- current-mark-position
6314 (count-trailing-whitespace-region
6315 bo-subtree current-mark-position))))
6316 (allout-toggle-subtree-encryption)
6317 (if (not was-modified)
6318 (set-buffer-modified-p nil))
6319 )
6320 (if (not was-modified)
6321 (set-buffer-modified-p nil))
6322 (if editing-topic (list editing-topic editing-point))
6323 )
6324 )
6325 )
6326
6327 ;;;_ #9 miscellaneous
6328 ;;;_ : Mode:
6329 ;;;_ > outlineify-sticky ()
6330 ;; outlinify-sticky is correct spelling; provide this alias for sticklers:
6331 ;;;###autoload
6332 (defalias 'outlinify-sticky 'outlineify-sticky)
6333 ;;;###autoload
6334 (defun outlineify-sticky (&optional arg)
6335 "Activate outline mode and establish file var so it is started subsequently.
6336
6337 See doc-string for `allout-layout' and `allout-init' for details on
6338 setup for auto-startup."
6339
6340 (interactive "P")
6341
6342 (allout-mode t)
6343
6344 (save-excursion
6345 (goto-char (point-min))
6346 (if (looking-at allout-regexp)
6347 t
6348 (allout-open-topic 2)
6349 (insert (concat "Dummy outline topic header - see"
6350 "`allout-mode' docstring: `^Hm'."))
6351 (allout-adjust-file-variable
6352 "allout-layout" (or allout-layout '(-1 : 0))))))
6353 ;;;_ > allout-file-vars-section-data ()
6354 (defun allout-file-vars-section-data ()
6355 "Return data identifying the file-vars section, or nil if none.
6356
6357 Returns list `(beginning-point prefix-string suffix-string)'."
6358 ;; minimally gleaned from emacs 21.4 files.el hack-local-variables function.
6359 (let (beg prefix suffix)
6360 (save-excursion
6361 (goto-char (point-max))
6362 (search-backward "\n\^L" (max (- (point-max) 3000) (point-min)) 'move)
6363 (if (let ((case-fold-search t))
6364 (not (search-forward "Local Variables:" nil t)))
6365 nil
6366 (setq beg (- (point) 16))
6367 (setq suffix (buffer-substring-no-properties
6368 (point)
6369 (progn (if (search-forward "\n" nil t)
6370 (forward-char -1))
6371 (point))))
6372 (setq prefix (buffer-substring-no-properties
6373 (progn (if (search-backward "\n" nil t)
6374 (forward-char 1))
6375 (point))
6376 beg))
6377 (list beg prefix suffix))
6378 )
6379 )
6380 )
6381 ;;;_ > allout-adjust-file-variable (varname value)
6382 (defun allout-adjust-file-variable (varname value)
6383 "Adjust the setting of an emacs file variable named VARNAME to VALUE.
6384
6385 This activity is inhibited if either `enable-local-variables'
6386 `allout-enable-file-variable-adjustment' are nil.
6387
6388 When enabled, an entry for the variable is created if not already present,
6389 or changed if established with a different value. The section for the file
6390 variables, itself, is created if not already present. When created, the
6391 section lines \(including the section line) exist as second-level topics in
6392 a top-level topic at the end of the file.
6393
6394 enable-local-variables must be true for any of this to happen."
6395 (if (not (and enable-local-variables
6396 allout-enable-file-variable-adjustment))
6397 nil
6398 (save-excursion
6399 (let ((inhibit-field-text-motion t)
6400 (section-data (allout-file-vars-section-data))
6401 beg prefix suffix)
6402 (if section-data
6403 (setq beg (car section-data)
6404 prefix (cadr section-data)
6405 suffix (car (cddr section-data)))
6406 ;; create the section
6407 (goto-char (point-max))
6408 (open-line 1)
6409 (allout-open-topic 0)
6410 (end-of-line)
6411 (insert "Local emacs vars.\n")
6412 (allout-open-topic 1)
6413 (setq beg (point)
6414 suffix ""
6415 prefix (buffer-substring-no-properties (progn
6416 (beginning-of-line)
6417 (point))
6418 beg))
6419 (goto-char beg)
6420 (insert "Local variables:\n")
6421 (allout-open-topic 0)
6422 (insert "End:\n")
6423 )
6424 ;; look for existing entry or create one, leaving point for insertion
6425 ;; of new value:
6426 (goto-char beg)
6427 (allout-show-to-offshoot)
6428 (if (search-forward (concat "\n" prefix varname ":") nil t)
6429 (let* ((value-beg (point))
6430 (line-end (progn (if (search-forward "\n" nil t)
6431 (forward-char -1))
6432 (point)))
6433 (value-end (- line-end (length suffix))))
6434 (if (> value-end value-beg)
6435 (delete-region value-beg value-end)))
6436 (end-of-line)
6437 (open-line 1)
6438 (forward-line 1)
6439 (insert (concat prefix varname ":")))
6440 (insert (format " %S%s" value suffix))
6441 )
6442 )
6443 )
6444 )
6445 ;;;_ > allout-get-configvar-values (varname)
6446 (defun allout-get-configvar-values (configvar-name)
6447 "Return a list of values of the symbols in list bound to CONFIGVAR-NAME.
6448
6449 The user is prompted for removal of symbols that are unbound, and they
6450 otherwise are ignored.
6451
6452 CONFIGVAR-NAME should be the name of the configuration variable,
6453 not its value."
6454
6455 (let ((configvar-value (symbol-value configvar-name))
6456 got)
6457 (dolist (sym configvar-value)
6458 (if (not (boundp sym))
6459 (if (yes-or-no-p (format "%s entry `%s' is unbound - remove it? "
6460 configvar-name sym))
6461 (delq sym (symbol-value configvar-name)))
6462 (push (symbol-value sym) got)))
6463 (reverse got)))
6464 ;;;_ : Topics:
6465 ;;;_ > allout-mark-topic ()
6466 (defun allout-mark-topic ()
6467 "Put the region around topic currently containing point."
6468 (interactive)
6469 (let ((inhibit-field-text-motion t))
6470 (beginning-of-line))
6471 (allout-goto-prefix-doublechecked)
6472 (push-mark (point))
6473 (allout-end-of-current-subtree)
6474 (exchange-point-and-mark))
6475 ;;;_ : UI:
6476 ;;;_ > solicit-char-in-string (prompt string &optional do-defaulting)
6477 (defun solicit-char-in-string (prompt string &optional do-defaulting)
6478 "Solicit (with first arg PROMPT) choice of a character from string STRING.
6479
6480 Optional arg DO-DEFAULTING indicates to accept empty input (CR)."
6481
6482 (let ((new-prompt prompt)
6483 got)
6484
6485 (while (not got)
6486 (message "%s" new-prompt)
6487
6488 ;; We do our own reading here, so we can circumvent, eg, special
6489 ;; treatment for `?' character. (Oughta use minibuffer keymap instead.)
6490 (setq got
6491 (char-to-string (let ((cursor-in-echo-area nil)) (read-char))))
6492
6493 (setq got
6494 (cond ((string-match (regexp-quote got) string) got)
6495 ((and do-defaulting (string= got "\r"))
6496 ;; Return empty string to default:
6497 "")
6498 ((string= got "\C-g") (signal 'quit nil))
6499 (t
6500 (setq new-prompt (concat prompt
6501 got
6502 " ...pick from: "
6503 string
6504 ""))
6505 nil))))
6506 ;; got something out of loop - return it:
6507 got)
6508 )
6509 ;;;_ : Strings:
6510 ;;;_ > regexp-sans-escapes (string)
6511 (defun regexp-sans-escapes (regexp &optional successive-backslashes)
6512 "Return a copy of REGEXP with all character escapes stripped out.
6513
6514 Representations of actual backslashes - '\\\\\\\\' - are left as a
6515 single backslash.
6516
6517 Optional arg SUCCESSIVE-BACKSLASHES is used internally for recursion."
6518
6519 (if (string= regexp "")
6520 ""
6521 ;; Set successive-backslashes to number if current char is
6522 ;; backslash, or else to nil:
6523 (setq successive-backslashes
6524 (if (= (aref regexp 0) ?\\)
6525 (if successive-backslashes (1+ successive-backslashes) 1)
6526 nil))
6527 (if (or (not successive-backslashes) (= 2 successive-backslashes))
6528 ;; Include first char:
6529 (concat (substring regexp 0 1)
6530 (regexp-sans-escapes (substring regexp 1)))
6531 ;; Exclude first char, but maintain count:
6532 (regexp-sans-escapes (substring regexp 1) successive-backslashes))))
6533 ;;;_ > count-trailing-whitespace-region (beg end)
6534 (defun count-trailing-whitespace-region (beg end)
6535 "Return number of trailing whitespace chars between BEG and END.
6536
6537 If BEG is bigger than END we return 0."
6538 (if (> beg end)
6539 0
6540 (save-excursion
6541 (goto-char beg)
6542 (let ((count 0))
6543 (while (re-search-forward "[ ][ ]*$" end t)
6544 (goto-char (1+ (match-beginning 2)))
6545 (setq count (1+ count)))
6546 count))))
6547 ;;;_ > allout-format-quote (string)
6548 (defun allout-format-quote (string)
6549 "Return a copy of string with all \"%\" characters doubled."
6550 (apply 'concat
6551 (mapcar (lambda (char) (if (= char ?%) "%%" (char-to-string char)))
6552 string)))
6553 ;;;_ : lists
6554 ;;;_ > allout-flatten (list)
6555 (defun allout-flatten (list)
6556 "Return a list of all atoms in list."
6557 ;; classic.
6558 (cond ((null list) nil)
6559 ((atom (car list)) (cons (car list) (allout-flatten (cdr list))))
6560 (t (append (allout-flatten (car list)) (allout-flatten (cdr list))))))
6561 ;;;_ : Compatability:
6562 ;;;_ > allout-mark-marker to accommodate divergent emacsen:
6563 (defun allout-mark-marker (&optional force buffer)
6564 "Accommodate the different signature for `mark-marker' across Emacsen.
6565
6566 XEmacs takes two optional args, while mainline GNU Emacs does not,
6567 so pass them along when appropriate."
6568 (if (featurep 'xemacs)
6569 (apply 'mark-marker force buffer)
6570 (mark-marker)))
6571 ;;;_ > subst-char-in-string if necessary
6572 (if (not (fboundp 'subst-char-in-string))
6573 (defun subst-char-in-string (fromchar tochar string &optional inplace)
6574 "Replace FROMCHAR with TOCHAR in STRING each time it occurs.
6575 Unless optional argument INPLACE is non-nil, return a new string."
6576 (let ((i (length string))
6577 (newstr (if inplace string (copy-sequence string))))
6578 (while (> i 0)
6579 (setq i (1- i))
6580 (if (eq (aref newstr i) fromchar)
6581 (aset newstr i tochar)))
6582 newstr)))
6583 ;;;_ > wholenump if necessary
6584 (if (not (fboundp 'wholenump))
6585 (defalias 'wholenump 'natnump))
6586 ;;;_ > remove-overlays if necessary
6587 (if (not (fboundp 'remove-overlays))
6588 (defun remove-overlays (&optional beg end name val)
6589 "Clear BEG and END of overlays whose property NAME has value VAL.
6590 Overlays might be moved and/or split.
6591 BEG and END default respectively to the beginning and end of buffer."
6592 (unless beg (setq beg (point-min)))
6593 (unless end (setq end (point-max)))
6594 (if (< end beg)
6595 (setq beg (prog1 end (setq end beg))))
6596 (save-excursion
6597 (dolist (o (overlays-in beg end))
6598 (when (eq (overlay-get o name) val)
6599 ;; Either push this overlay outside beg...end
6600 ;; or split it to exclude beg...end
6601 ;; or delete it entirely (if it is contained in beg...end).
6602 (if (< (overlay-start o) beg)
6603 (if (> (overlay-end o) end)
6604 (progn
6605 (move-overlay (copy-overlay o)
6606 (overlay-start o) beg)
6607 (move-overlay o end (overlay-end o)))
6608 (move-overlay o (overlay-start o) beg))
6609 (if (> (overlay-end o) end)
6610 (move-overlay o end (overlay-end o))
6611 (delete-overlay o)))))))
6612 )
6613 ;;;_ > copy-overlay if necessary - xemacs ~ 21.4
6614 (if (not (fboundp 'copy-overlay))
6615 (defun copy-overlay (o)
6616 "Return a copy of overlay O."
6617 (let ((o1 (make-overlay (overlay-start o) (overlay-end o)
6618 ;; FIXME: there's no easy way to find the
6619 ;; insertion-type of the two markers.
6620 (overlay-buffer o)))
6621 (props (overlay-properties o)))
6622 (while props
6623 (overlay-put o1 (pop props) (pop props)))
6624 o1)))
6625 ;;;_ > add-to-invisibility-spec if necessary - xemacs ~ 21.4
6626 (if (not (fboundp 'add-to-invisibility-spec))
6627 (defun add-to-invisibility-spec (element)
6628 "Add ELEMENT to `buffer-invisibility-spec'.
6629 See documentation for `buffer-invisibility-spec' for the kind of elements
6630 that can be added."
6631 (if (eq buffer-invisibility-spec t)
6632 (setq buffer-invisibility-spec (list t)))
6633 (setq buffer-invisibility-spec
6634 (cons element buffer-invisibility-spec))))
6635 ;;;_ > remove-from-invisibility-spec if necessary - xemacs ~ 21.4
6636 (if (not (fboundp 'remove-from-invisibility-spec))
6637 (defun remove-from-invisibility-spec (element)
6638 "Remove ELEMENT from `buffer-invisibility-spec'."
6639 (if (consp buffer-invisibility-spec)
6640 (setq buffer-invisibility-spec (delete element
6641 buffer-invisibility-spec)))))
6642 ;;;_ > move-beginning-of-line if necessary - older emacs, xemacs
6643 (if (not (fboundp 'move-beginning-of-line))
6644 (defun move-beginning-of-line (arg)
6645 "Move point to beginning of current line as displayed.
6646 \(This disregards invisible newlines such as those
6647 which are part of the text that an image rests on.)
6648
6649 With argument ARG not nil or 1, move forward ARG - 1 lines first.
6650 If point reaches the beginning or end of buffer, it stops there.
6651 To ignore intangibility, bind `inhibit-point-motion-hooks' to t."
6652 (interactive "p")
6653 (or arg (setq arg 1))
6654 (if (/= arg 1)
6655 (condition-case nil (line-move (1- arg)) (error nil)))
6656
6657 ;; Move to beginning-of-line, ignoring fields and invisibles.
6658 (skip-chars-backward "^\n")
6659 (while (and (not (bobp)) (line-move-invisible-p (1- (point))))
6660 (goto-char (if (featurep 'xemacs)
6661 (previous-property-change (point))
6662 (previous-char-property-change (point))))
6663 (skip-chars-backward "^\n"))
6664 (vertical-motion 0))
6665 )
6666 ;;;_ > move-end-of-line if necessary - older emacs, xemacs
6667 (if (not (fboundp 'move-end-of-line))
6668 (defun move-end-of-line (arg)
6669 "Move point to end of current line as displayed.
6670 \(This disregards invisible newlines such as those
6671 which are part of the text that an image rests on.)
6672
6673 With argument ARG not nil or 1, move forward ARG - 1 lines first.
6674 If point reaches the beginning or end of buffer, it stops there.
6675 To ignore intangibility, bind `inhibit-point-motion-hooks' to t."
6676 (interactive "p")
6677 (or arg (setq arg 1))
6678 (let (done)
6679 (while (not done)
6680 (let ((newpos
6681 (save-excursion
6682 (let ((goal-column 0))
6683 (and (condition-case nil
6684 (or (line-move arg) t)
6685 (error nil))
6686 (not (bobp))
6687 (progn
6688 (while (and (not (bobp))
6689 (line-move-invisible-p (1- (point))))
6690 (goto-char
6691 (previous-char-property-change (point))))
6692 (backward-char 1)))
6693 (point)))))
6694 (goto-char newpos)
6695 (if (and (> (point) newpos)
6696 (eq (preceding-char) ?\n))
6697 (backward-char 1)
6698 (if (and (> (point) newpos) (not (eobp))
6699 (not (eq (following-char) ?\n)))
6700 ;; If we skipped something intangible
6701 ;; and now we're not really at eol,
6702 ;; keep going.
6703 (setq arg 1)
6704 (setq done t)))))))
6705 )
6706 ;;;_ > line-move-invisible-p if necessary
6707 (if (not (fboundp 'line-move-invisible-p))
6708 (defun line-move-invisible-p (pos)
6709 "Return non-nil if the character after POS is currently invisible."
6710 (let ((prop
6711 (get-char-property pos 'invisible)))
6712 (if (eq buffer-invisibility-spec t)
6713 prop
6714 (or (memq prop buffer-invisibility-spec)
6715 (assq prop buffer-invisibility-spec))))))
6716
6717 ;;;_ #10 Unfinished
6718 ;;;_ > allout-bullet-isearch (&optional bullet)
6719 (defun allout-bullet-isearch (&optional bullet)
6720 "Isearch \(regexp) for topic with bullet BULLET."
6721 (interactive)
6722 (if (not bullet)
6723 (setq bullet (solicit-char-in-string
6724 "ISearch for topic with bullet: "
6725 (regexp-sans-escapes allout-bullets-string))))
6726
6727 (let ((isearch-regexp t)
6728 (isearch-string (concat "^"
6729 allout-header-prefix
6730 "[ \t]*"
6731 bullet)))
6732 (isearch-repeat 'forward)
6733 (isearch-mode t)))
6734
6735 ;;;_ #11 Unit tests - this should be last item before "Provide"
6736 ;;;_ > allout-run-unit-tests ()
6737 (defun allout-run-unit-tests ()
6738 "Run the various allout unit tests."
6739 (message "Running allout tests...")
6740 (allout-test-resumptions)
6741 (message "Running allout tests... Done.")
6742 (sit-for .5))
6743 ;;;_ : test resumptions:
6744 ;;;_ > allout-tests-obliterate-variable (name)
6745 (defun allout-tests-obliterate-variable (name)
6746 "Completely unbind variable with NAME."
6747 (if (local-variable-p name) (kill-local-variable name))
6748 (while (boundp name) (makunbound name)))
6749 ;;;_ > allout-test-resumptions ()
6750 (defvar allout-tests-globally-unbound nil
6751 "Fodder for allout resumptions tests - defvar just for byte compiler.")
6752 (defvar allout-tests-globally-true nil
6753 "Fodder for allout resumptions tests - defvar just just for byte compiler.")
6754 (defvar allout-tests-locally-true nil
6755 "Fodder for allout resumptions tests - defvar just for byte compiler.")
6756 (defun allout-test-resumptions ()
6757 "Exercise allout resumptions."
6758 ;; for each resumption case, we also test that the right local/global
6759 ;; scopes are affected during resumption effects:
6760
6761 ;; ensure that previously unbound variables return to the unbound state.
6762 (with-temp-buffer
6763 (allout-tests-obliterate-variable 'allout-tests-globally-unbound)
6764 (allout-add-resumptions '(allout-tests-globally-unbound t))
6765 (assert (not (default-boundp 'allout-tests-globally-unbound)))
6766 (assert (local-variable-p 'allout-tests-globally-unbound))
6767 (assert (boundp 'allout-tests-globally-unbound))
6768 (assert (equal allout-tests-globally-unbound t))
6769 (allout-do-resumptions)
6770 (assert (not (local-variable-p 'allout-tests-globally-unbound)))
6771 (assert (not (boundp 'allout-tests-globally-unbound))))
6772
6773 ;; ensure that variable with prior global value is resumed
6774 (with-temp-buffer
6775 (allout-tests-obliterate-variable 'allout-tests-globally-true)
6776 (setq allout-tests-globally-true t)
6777 (allout-add-resumptions '(allout-tests-globally-true nil))
6778 (assert (equal (default-value 'allout-tests-globally-true) t))
6779 (assert (local-variable-p 'allout-tests-globally-true))
6780 (assert (equal allout-tests-globally-true nil))
6781 (allout-do-resumptions)
6782 (assert (not (local-variable-p 'allout-tests-globally-true)))
6783 (assert (boundp 'allout-tests-globally-true))
6784 (assert (equal allout-tests-globally-true t)))
6785
6786 ;; ensure that prior local value is resumed
6787 (with-temp-buffer
6788 (allout-tests-obliterate-variable 'allout-tests-locally-true)
6789 (set (make-local-variable 'allout-tests-locally-true) t)
6790 (assert (not (default-boundp 'allout-tests-locally-true))
6791 nil (concat "Test setup mistake - variable supposed to"
6792 " not have global binding, but it does."))
6793 (assert (local-variable-p 'allout-tests-locally-true)
6794 nil (concat "Test setup mistake - variable supposed to have"
6795 " local binding, but it lacks one."))
6796 (allout-add-resumptions '(allout-tests-locally-true nil))
6797 (assert (not (default-boundp 'allout-tests-locally-true)))
6798 (assert (local-variable-p 'allout-tests-locally-true))
6799 (assert (equal allout-tests-locally-true nil))
6800 (allout-do-resumptions)
6801 (assert (boundp 'allout-tests-locally-true))
6802 (assert (local-variable-p 'allout-tests-locally-true))
6803 (assert (equal allout-tests-locally-true t))
6804 (assert (not (default-boundp 'allout-tests-locally-true))))
6805
6806 ;; ensure that last of multiple resumptions holds, for various scopes.
6807 (with-temp-buffer
6808 (allout-tests-obliterate-variable 'allout-tests-globally-unbound)
6809 (allout-tests-obliterate-variable 'allout-tests-globally-true)
6810 (setq allout-tests-globally-true t)
6811 (allout-tests-obliterate-variable 'allout-tests-locally-true)
6812 (set (make-local-variable 'allout-tests-locally-true) t)
6813 (allout-add-resumptions '(allout-tests-globally-unbound t)
6814 '(allout-tests-globally-true nil)
6815 '(allout-tests-locally-true nil))
6816 (allout-add-resumptions '(allout-tests-globally-unbound 2)
6817 '(allout-tests-globally-true 3)
6818 '(allout-tests-locally-true 4))
6819 ;; reestablish many of the basic conditions are maintained after re-add:
6820 (assert (not (default-boundp 'allout-tests-globally-unbound)))
6821 (assert (local-variable-p 'allout-tests-globally-unbound))
6822 (assert (equal allout-tests-globally-unbound 2))
6823 (assert (default-boundp 'allout-tests-globally-true))
6824 (assert (local-variable-p 'allout-tests-globally-true))
6825 (assert (equal allout-tests-globally-true 3))
6826 (assert (not (default-boundp 'allout-tests-locally-true)))
6827 (assert (local-variable-p 'allout-tests-locally-true))
6828 (assert (equal allout-tests-locally-true 4))
6829 (allout-do-resumptions)
6830 (assert (not (local-variable-p 'allout-tests-globally-unbound)))
6831 (assert (not (boundp 'allout-tests-globally-unbound)))
6832 (assert (not (local-variable-p 'allout-tests-globally-true)))
6833 (assert (boundp 'allout-tests-globally-true))
6834 (assert (equal allout-tests-globally-true t))
6835 (assert (boundp 'allout-tests-locally-true))
6836 (assert (local-variable-p 'allout-tests-locally-true))
6837 (assert (equal allout-tests-locally-true t))
6838 (assert (not (default-boundp 'allout-tests-locally-true))))
6839
6840 ;; ensure that deliberately unbinding registered variables doesn't foul things
6841 (with-temp-buffer
6842 (allout-tests-obliterate-variable 'allout-tests-globally-unbound)
6843 (allout-tests-obliterate-variable 'allout-tests-globally-true)
6844 (setq allout-tests-globally-true t)
6845 (allout-tests-obliterate-variable 'allout-tests-locally-true)
6846 (set (make-local-variable 'allout-tests-locally-true) t)
6847 (allout-add-resumptions '(allout-tests-globally-unbound t)
6848 '(allout-tests-globally-true nil)
6849 '(allout-tests-locally-true nil))
6850 (allout-tests-obliterate-variable 'allout-tests-globally-unbound)
6851 (allout-tests-obliterate-variable 'allout-tests-globally-true)
6852 (allout-tests-obliterate-variable 'allout-tests-locally-true)
6853 (allout-do-resumptions))
6854 )
6855 ;;;_ % Run unit tests if `allout-run-unit-tests-after-load' is true:
6856 (when allout-run-unit-tests-on-load
6857 (allout-run-unit-tests))
6858
6859 ;;;_ #12 Provide
6860 (provide 'allout)
6861
6862 ;;;_* Local emacs vars.
6863 ;; The following `allout-layout' local variable setting:
6864 ;; - closes all topics from the first topic to just before the third-to-last,
6865 ;; - shows the children of the third to last (config vars)
6866 ;; - and the second to last (code section),
6867 ;; - and closes the last topic (this local-variables section).
6868 ;;Local variables:
6869 ;;allout-layout: (0 : -1 -1 0)
6870 ;;End:
6871
6872 ;; arch-tag: cf38fbc3-c044-450f-8bff-afed8ba5681c
6873 ;;; allout.el ends here