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