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