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