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