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