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