Followup to 2011-10-19T09:48:35Z!eliz@gnu.org.
[bpt/emacs.git] / lisp / allout.el
CommitLineData
6a05d05f 1;;; allout.el --- extensive outline mode for use alone and with other modes
19b84ba3 2
73b0cd50 3;; Copyright (C) 1992-1994, 2001-2011 Free Software Foundation, Inc.
1977b8f6 4
d82979ea
EZ
5;; Author: Ken Manheimer <ken dot manheimer at gmail dot com>
6;; Maintainer: Ken Manheimer <ken dot manheimer at gmail dot com>
60c7bbd2 7;; Created: Dec 1991 -- first release to usenet
0af8c88b 8;; Version: 2.3
96497653 9;; Keywords: outlines, wp, languages, PGP, GnuPG
01fc9422 10;; Website: http://myriadicity.net/Sundry/EmacsAllout
1977b8f6
RS
11
12;; This file is part of GNU Emacs.
13
eb3fa2cf 14;; GNU Emacs is free software: you can redistribute it and/or modify
59243403 15;; it under the terms of the GNU General Public License as published by
eb3fa2cf
GM
16;; the Free Software Foundation, either version 3 of the License, or
17;; (at your option) any later version.
59243403 18
1977b8f6 19;; GNU Emacs is distributed in the hope that it will be useful,
59243403
RS
20;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22;; GNU General Public License for more details.
23
24;; You should have received a copy of the GNU General Public License
eb3fa2cf 25;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
1977b8f6 26
6a05d05f 27;;; Commentary:
19b84ba3 28
96b83743
EZ
29;; Allout outline minor mode provides extensive outline formatting and
30;; and manipulation beyond standard emacs outline mode. Some features:
71296446 31;;
96b83743
EZ
32;; - Classic outline-mode topic-oriented navigation and exposure adjustment
33;; - Topic-oriented editing including coherent topic and subtopic
34;; creation, promotion, demotion, cut/paste across depths, etc.
35;; - Incremental search with dynamic exposure and reconcealment of text
60c7bbd2 36;; - Customizable bullet format -- enables programming-language specific
96b83743 37;; outlining, for code-folding editing. (Allout code itself is to try it;
60c7bbd2 38;; formatted as an outline -- do ESC-x eval-buffer in allout.el; but
96b83743 39;; emacs local file variables need to be enabled when the
60c7bbd2 40;; file was visited -- see `enable-local-variables'.)
96b83743 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
828This provents file-system exposure of un-encrypted contents of
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
1540substition is used against the regexp matches, a la `replace-match'.")
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
KM
1546This is used to detect strings in encryption results that would
1547register as allout mode structural elements, for exmple, as a
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:
19b84ba3 1705 "Toggle minor mode for controlling exposure and editing of text outlines.
d3e51865 1706\\<allout-mode-map-value>
19b84ba3 1707
2576c28f 1708Allout outline mode always runs as a minor mode.
19b84ba3 1709
2576c28f
KM
1710Allout outline mode provides extensive outline oriented
1711formatting and manipulation. It enables structural editing of
1712outlines, as well as navigation and exposure. It also is
1713specifically aimed at accommodating syntax-sensitive text like
1714programming languages. \(For example, see the allout code itself,
1715which is organized as an allout outline.)
19b84ba3 1716
c07583cd 1717In addition to typical outline navigation and exposure, allout includes:
19b84ba3 1718
c07583cd
KS
1719 - topic-oriented authoring, including keystroke-based topic creation,
1720 repositioning, promotion/demotion, cut, and paste
9179616f 1721 - incremental search with dynamic exposure and reconcealment of hidden text
c07583cd 1722 - adjustable format, so programming code can be developed in outline-structure
2576c28f 1723 - easy topic encryption and decryption, symmetric or key-pair
c07583cd
KS
1724 - \"Hot-spot\" operation, for single-keystroke maneuvering and exposure control
1725 - integral outline layout, for automatic initial exposure when visiting a file
1726 - independent extensibility, using comprehensive exposure and authoring hooks
9179616f
DL
1727
1728and many other features.
19b84ba3 1729
2576c28f
KM
1730Below is a description of the key bindings, and then description
1731of special `allout-mode' features and terminology. See also the
1732outline menubar additions for quick reference to many of the
11c46e15
KM
1733features. Customize `allout-auto-activation' to prepare your
1734emacs session for automatic activation of `allout-mode'.
9179616f 1735
d3e51865
KM
1736The bindings are those listed in `allout-prefixed-keybindings'
1737and `allout-unprefixed-keybindings'. We recommend customizing
1738`allout-command-prefix' to use just `\\C-c' as the command
1739prefix, if the allout bindings don't conflict with any personal
1740bindings you have on \\C-c. In any case, outline structure
1741navigation and authoring is simplified by positioning the cursor
1742on an item's bullet character, the \"hot-spot\" -- then you can
1743invoke allout commands with just the un-prefixed,
1744un-control-shifted command letters. This is described further in
1745the HOT-SPOT Operation section.
c07583cd
KS
1746
1747 Exposure Control:
1748 ----------------
1749\\[allout-hide-current-subtree] `allout-hide-current-subtree'
1750\\[allout-show-children] `allout-show-children'
1751\\[allout-show-current-subtree] `allout-show-current-subtree'
1752\\[allout-show-current-entry] `allout-show-current-entry'
1753\\[allout-show-all] `allout-show-all'
1754
1755 Navigation:
1756 ----------
1757\\[allout-next-visible-heading] `allout-next-visible-heading'
1758\\[allout-previous-visible-heading] `allout-previous-visible-heading'
1759\\[allout-up-current-level] `allout-up-current-level'
1760\\[allout-forward-current-level] `allout-forward-current-level'
1761\\[allout-backward-current-level] `allout-backward-current-level'
1762\\[allout-end-of-entry] `allout-end-of-entry'
1763\\[allout-beginning-of-current-entry] `allout-beginning-of-current-entry' (alternately, goes to hot-spot)
60c7bbd2 1764\\[allout-beginning-of-line] `allout-beginning-of-line' -- like regular beginning-of-line, but
c07583cd
KS
1765 if immediately repeated cycles to the beginning of the current item
1766 and then to the hot-spot (if `allout-beginning-of-line-cycles' is set).
1767
1768
1769 Topic Header Production:
1770 -----------------------
1771\\[allout-open-sibtopic] `allout-open-sibtopic' Create a new sibling after current topic.
1772\\[allout-open-subtopic] `allout-open-subtopic' ... an offspring of current topic.
1773\\[allout-open-supertopic] `allout-open-supertopic' ... a sibling of the current topic's parent.
1774
1775 Topic Level and Prefix Adjustment:
1776 ---------------------------------
1777\\[allout-shift-in] `allout-shift-in' Shift current topic and all offspring deeper
1778\\[allout-shift-out] `allout-shift-out' ... less deep
1779\\[allout-rebullet-current-heading] `allout-rebullet-current-heading' Prompt for alternate bullet for
1780 current topic
1781\\[allout-rebullet-topic] `allout-rebullet-topic' Reconcile bullets of topic and
60c7bbd2 1782 its' offspring -- distinctive bullets are not changed, others
c07583cd 1783 are alternated according to nesting depth.
ea18b2b1 1784\\[allout-number-siblings] `allout-number-siblings' Number bullets of topic and siblings --
c07583cd
KS
1785 the offspring are not affected.
1786 With repeat count, revoke numbering.
1787
1788 Topic-oriented Killing and Yanking:
1789 ----------------------------------
1790\\[allout-kill-topic] `allout-kill-topic' Kill current topic, including offspring.
1791\\[allout-copy-topic-as-kill] `allout-copy-topic-as-kill' Copy current topic, including offspring.
1792\\[allout-kill-line] `allout-kill-line' kill-line, attending to outline structure.
1793\\[allout-copy-line-as-kill] `allout-copy-line-as-kill' Copy line but don't delete it.
1794\\[allout-yank] `allout-yank' Yank, adjusting depth of yanked topic to
1795 depth of heading if yanking into bare topic
1796 heading (ie, prefix sans text).
1797\\[allout-yank-pop] `allout-yank-pop' Is to allout-yank as yank-pop is to yank
1798
1799 Topic-oriented Encryption:
1800 -------------------------
1801\\[allout-toggle-current-subtree-encryption] `allout-toggle-current-subtree-encryption'
1802 Encrypt/Decrypt topic content
1803
1804 Misc commands:
1805 -------------
1806M-x outlineify-sticky Activate outline mode for current buffer,
1807 and establish a default file-var setting
1808 for `allout-layout'.
1809\\[allout-mark-topic] `allout-mark-topic'
1810\\[allout-copy-exposed-to-buffer] `allout-copy-exposed-to-buffer'
1811 Duplicate outline, sans concealed text, to
1812 buffer with name derived from derived from that
60c7bbd2 1813 of current buffer -- \"*BUFFERNAME exposed*\".
c07583cd
KS
1814\\[allout-flatten-exposed-to-buffer] `allout-flatten-exposed-to-buffer'
1815 Like above 'copy-exposed', but convert topic
1816 prefixes to section.subsection... numeric
1817 format.
11c46e15
KM
1818\\[customize-variable] allout-auto-activation
1819 Prepare Emacs session for allout outline mode
c07583cd 1820 auto-activation.
1977b8f6 1821
96b83743 1822 Topic Encryption
d82979ea 1823
96b83743 1824Outline mode supports gpg encryption of topics, with support for
29fac3fe
KM
1825symmetric and key-pair modes, and auto-encryption of topics
1826pending encryption on save.
d82979ea 1827
c07583cd 1828Topics pending encryption are, by default, automatically
29fac3fe
KM
1829encrypted during file saves, including checkpoint saves, to avoid
1830exposing the plain text of encrypted topics in the file system.
1831If the content of the topic containing the cursor was encrypted
1832for a save, it is automatically decrypted for continued editing.
c07583cd 1833
2576c28f
KM
1834NOTE: A few GnuPG v2 versions improperly preserve incorrect
1835symmetric decryption keys, preventing entry of the correct key on
1836subsequent decryption attempts until the cache times-out. That
1837can take several minutes. \(Decryption of other entries is not
1838affected.) Upgrade your EasyPG version, if you can, and you can
1839deliberately clear your gpg-agent's cache by sending it a '-HUP'
1840signal.
c07583cd
KS
1841
1842See `allout-toggle-current-subtree-encryption' function docstring
1843and `allout-encrypt-unencrypted-on-saves' customization variable
1844for details.
d82979ea 1845
c07583cd 1846 HOT-SPOT Operation
c567ac01 1847
19b84ba3
RS
1848Hot-spot operation provides a means for easy, single-keystroke outline
1849navigation and exposure control.
c567ac01 1850
19b84ba3
RS
1851When the text cursor is positioned directly on the bullet character of
1852a topic, regular characters (a to z) invoke the commands of the
fd5359c6 1853corresponding allout-mode keymap control chars. For example, \"f\"
96b83743 1854would invoke the command typically bound to \"C-c<space>C-f\"
fd5359c6 1855\(\\[allout-forward-current-level] `allout-forward-current-level').
19b84ba3 1856
96b83743
EZ
1857Thus, by positioning the cursor on a topic bullet, you can
1858execute the outline navigation and manipulation commands with a
c07583cd 1859single keystroke. Regular navigation keys (eg, \\[forward-char], \\[next-line]) don't get
96b83743 1860this special translation, so you can use them to get out of the
c07583cd
KS
1861hot-spot and back to normal editing operation.
1862
d41b91e6 1863In allout-mode, the normal beginning-of-line command (\\[allout-beginning-of-line]) is
c07583cd
KS
1864replaced with one that makes it easy to get to the hot-spot. If you
1865repeat it immediately it cycles (if `allout-beginning-of-line-cycles'
1866is set) to the beginning of the item and then, if you hit it again
1867immediately, to the hot-spot. Similarly, `allout-beginning-of-current-entry'
1868\(\\[allout-beginning-of-current-entry]) moves to the hot-spot when the cursor is already located
1869at the beginning of the current entry.
1870
1871 Extending Allout
c567ac01 1872
c07583cd
KS
1873Allout exposure and authoring activites all have associated
1874hooks, by which independent code can cooperate with allout
1875without changes to the allout core. Here are key ones:
1977b8f6 1876
c07583cd 1877`allout-mode-hook'
2576c28f
KM
1878`allout-mode-deactivate-hook' \(deprecated)
1879`allout-mode-off-hook'
c07583cd
KS
1880`allout-exposure-change-hook'
1881`allout-structure-added-hook'
1882`allout-structure-deleted-hook'
1883`allout-structure-shifted-hook'
4ec28e14 1884`allout-after-copy-or-kill-hook'
919d884a 1885`allout-post-undo-hook'
c07583cd
KS
1886
1887 Terminology
1977b8f6 1888
60c7bbd2 1889Topic hierarchy constituents -- TOPICS and SUBTOPICS:
c567ac01 1890
c07583cd
KS
1891ITEM: A unitary outline element, including the HEADER and ENTRY text.
1892TOPIC: An ITEM and any ITEMs contained within it, ie having greater DEPTH
1893 and with no intervening items of lower DEPTH than the container.
1894CURRENT ITEM:
1895 The visible ITEM most immediately containing the cursor.
1896DEPTH: The degree of nesting of an ITEM; it increases with containment.
1897 The DEPTH is determined by the HEADER PREFIX. The DEPTH is also
1898 called the:
1899LEVEL: The same as DEPTH.
c567ac01
RS
1900
1901ANCESTORS:
c07583cd
KS
1902 Those ITEMs whose TOPICs contain an ITEM.
1903PARENT: An ITEM's immediate ANCESTOR. It has a DEPTH one less than that
1904 of the ITEM.
c567ac01 1905OFFSPRING:
c07583cd 1906 The ITEMs contained within an ITEM's TOPIC.
19b84ba3 1907SUBTOPIC:
c07583cd
KS
1908 An OFFSPRING of its ANCESTOR TOPICs.
1909CHILD:
1910 An immediate SUBTOPIC of its PARENT.
c567ac01 1911SIBLINGS:
c07583cd 1912 TOPICs having the same PARENT and DEPTH.
353e2ef2 1913
c567ac01
RS
1914Topic text constituents:
1915
c07583cd
KS
1916HEADER: The first line of an ITEM, include the ITEM PREFIX and HEADER
1917 text.
1918ENTRY: The text content of an ITEM, before any OFFSPRING, but including
1919 the HEADER text and distinct from the ITEM PREFIX.
1920BODY: Same as ENTRY.
1921PREFIX: The leading text of an ITEM which distinguishes it from normal
1922 ENTRY text. Allout recognizes the outline structure according
1923 to the strict PREFIX format. It consists of a PREFIX-LEAD string,
1924 PREFIX-PADDING, and a BULLET. The BULLET might be followed by a
d82979ea 1925 number, indicating the ordinal number of the topic among its
c07583cd
KS
1926 siblings, or an asterisk indicating encryption, plus an optional
1927 space. After that is the ITEM HEADER text, which is not part of
1928 the PREFIX.
c567ac01 1929
c07583cd
KS
1930 The relative length of the PREFIX determines the nesting DEPTH
1931 of the ITEM.
c567ac01 1932PREFIX-LEAD:
c07583cd
KS
1933 The string at the beginning of a HEADER PREFIX, by default a `.'.
1934 It can be customized by changing the setting of
1935 `allout-header-prefix' and then reinitializing `allout-mode'.
1936
1937 When the PREFIX-LEAD is set to the comment-string of a
1938 programming language, outline structuring can be embedded in
1939 program code without interfering with processing of the text
1940 (by emacs or the language processor) as program code. This
1941 setting happens automatically when allout mode is used in
1942 programming-mode buffers. See `allout-use-mode-specific-leader'
1943 docstring for more detail.
c567ac01 1944PREFIX-PADDING:
c07583cd
KS
1945 Spaces or asterisks which separate the PREFIX-LEAD and the
1946 bullet, determining the ITEM's DEPTH.
1947BULLET: A character at the end of the ITEM PREFIX, it must be one of
1948 the characters listed on `allout-plain-bullets-string' or
38b2ca53
NR
1949 `allout-distinctive-bullets-string'. When creating a TOPIC,
1950 plain BULLETs are by default used, according to the DEPTH of the
1951 TOPIC. Choice among the distinctive BULLETs is offered when you
1952 provide a universal argugment \(\\[universal-argument]) to the
1953 TOPIC creation command, or when explictly rebulleting a TOPIC. The
1954 significance of the various distinctive bullets is purely by
1955 convention. See the documentation for the above bullet strings for
1956 more details.
c567ac01 1957EXPOSURE:
c07583cd
KS
1958 The state of a TOPIC which determines the on-screen visibility
1959 of its OFFSPRING and contained ENTRY text.
c567ac01 1960CONCEALED:
c07583cd
KS
1961 TOPICs and ENTRY text whose EXPOSURE is inhibited. Concealed
1962 text is represented by \"...\" ellipses.
c567ac01 1963
c07583cd
KS
1964 CONCEALED TOPICs are effectively collapsed within an ANCESTOR.
1965CLOSED: A TOPIC whose immediate OFFSPRING and body-text is CONCEALED.
1966OPEN: A TOPIC that is not CLOSED, though its OFFSPRING or BODY may be."
c567ac01 1967;;;_ . Code
d3e51865
KM
1968 :lighter " Allout"
1969 :keymap 'allout-mode-map
1977b8f6 1970
13aa217b 1971 (let ((use-layout (if (listp allout-layout)
2576c28f
KM
1972 allout-layout
1973 allout-default-layout)))
1977b8f6 1974
d3e51865 1975 (if (not (allout-mode-p))
2576c28f
KM
1976 (progn
1977 ;; Deactivation:
1978
1979 ; Activation not explicitly
1980 ; requested, and either in
1981 ; active state or *de*activation
1982 ; specifically requested:
1983 (allout-do-resumptions)
1984
1985 (remove-from-invisibility-spec '(allout . t))
1986 (remove-hook 'pre-command-hook 'allout-pre-command-business t)
1987 (remove-hook 'post-command-hook 'allout-post-command-business t)
1988 (remove-hook 'before-change-functions 'allout-before-change-handler t)
1989 (remove-hook 'isearch-mode-end-hook 'allout-isearch-end-handler t)
13aa217b
KM
1990 (remove-hook 'write-contents-functions
1991 'allout-write-contents-hook-handler t)
2576c28f
KM
1992
1993 (remove-overlays (point-min) (point-max)
d3e51865 1994 'category 'allout-exposure-category))
2576c28f 1995
d3e51865 1996 ;; Activating:
fd5359c6 1997 (if allout-old-style-prefixes
01fc9422 1998 ;; Inhibit all the fancy formatting:
48bd8440 1999 (allout-add-resumptions '(allout-primary-bullet "*")))
19b84ba3 2000
01fc9422 2001 (allout-overlay-preparations) ; Doesn't hurt to redo this.
96b83743 2002
d8350998 2003 (allout-infer-header-lead-and-primary-bullet)
fd5359c6 2004 (allout-infer-body-reindent)
19b84ba3 2005
fd5359c6 2006 (set-allout-regexp)
2576c28f
KM
2007 (allout-add-resumptions '(allout-encryption-ciphertext-rejection-regexps
2008 allout-line-boundary-regexp
2009 extend)
2010 '(allout-encryption-ciphertext-rejection-regexps
2011 allout-bob-regexp
2012 extend))
19b84ba3 2013
d3e51865 2014 (allout-compose-and-institute-keymap)
01fc9422 2015 (produce-allout-mode-menubar-entries)
01fc9422 2016
96b83743 2017 (add-to-invisibility-spec '(allout . t))
38b2ca53 2018
01fc9422
EZ
2019 (allout-add-resumptions '(line-move-ignore-invisible t))
2020 (add-hook 'pre-command-hook 'allout-pre-command-business nil t)
2021 (add-hook 'post-command-hook 'allout-post-command-business nil t)
2576c28f 2022 (add-hook 'before-change-functions 'allout-before-change-handler nil t)
01fc9422 2023 (add-hook 'isearch-mode-end-hook 'allout-isearch-end-handler nil t)
13aa217b 2024 (add-hook 'write-contents-functions 'allout-write-contents-hook-handler
01fc9422 2025 nil t)
01fc9422
EZ
2026
2027 ;; Stash auto-fill settings and adjust so custom allout auto-fill
2028 ;; func will be used if auto-fill is active or activated. (The
2029 ;; custom func respects topic headline, maintains hanging-indents,
2030 ;; etc.)
01fc9422
EZ
2031 (allout-add-resumptions (list 'allout-former-auto-filler
2032 auto-fill-function)
2033 ;; Register allout-auto-fill to be used if
2034 ;; filling is active:
2035 (list 'allout-outside-normal-auto-fill-function
2036 normal-auto-fill-function)
2037 '(normal-auto-fill-function allout-auto-fill)
2038 ;; Paragraphs are broken by topic headlines.
2039 (list 'paragraph-start
2040 (concat paragraph-start "\\|^\\("
2041 allout-regexp "\\)"))
2042 (list 'paragraph-separate
2043 (concat paragraph-separate "\\|^\\("
2044 allout-regexp "\\)")))
2576c28f
KM
2045 (if (and auto-fill-function (not allout-inhibit-auto-fill))
2046 ;; allout-auto-fill will use the stashed values and so forth.
2047 (allout-add-resumptions '(auto-fill-function allout-auto-fill)))
2048
fd5359c6 2049 (allout-setup-menubar)
2576c28f
KM
2050
2051 ;; Do auto layout if warranted:
2052 (when (and allout-layout
2053 allout-auto-activation
2054 use-layout
11c46e15
KM
2055 (and (not (string= allout-auto-activation "activate"))
2056 (if (string= allout-auto-activation "ask")
2576c28f
KM
2057 (if (y-or-n-p (format "Expose %s with layout '%s'? "
2058 (buffer-name)
2059 use-layout))
2060 t
2061 (message "Skipped %s layout." (buffer-name))
2062 nil)
2063 t)))
2064 (save-excursion
2065 (message "Adjusting '%s' exposure..." (buffer-name))
2066 (goto-char 0)
2067 (allout-this-or-next-heading)
2068 (condition-case err
2069 (progn
2070 (apply 'allout-expose-topic (list use-layout))
2071 (message "Adjusting '%s' exposure... done."
2072 (buffer-name)))
2073 ;; Problem applying exposure -- notify user, but don't
2074 ;; interrupt, eg, file visit:
2075 (error (message "%s" (car (cdr err)))
2076 (sit-for 1))))
2077 ) ; when allout-layout
2078 ) ; if (allout-mode-p)
2079 ) ; let (())
2080 ) ; define-minor-mode
2081;;;_ > allout-minor-mode alias
2082(defalias 'allout-minor-mode 'allout-mode)
41ff6d0a
JB
2083;;;_ > allout-unload-function
2084(defun allout-unload-function ()
2085 "Unload the allout outline library."
2086 (save-current-buffer
2087 (dolist (buffer (buffer-list))
2088 (set-buffer buffer)
74f53697 2089 (when (allout-mode-p) (allout-mode -1))))
41ff6d0a
JB
2090 ;; continue standard unloading
2091 nil)
2092
773e7e48
EZ
2093;;;_ - Position Assessment
2094;;;_ > allout-hidden-p (&optional pos)
2095(defsubst allout-hidden-p (&optional pos)
1c9b9df0 2096 "Non-nil if the character after point was made invisible by allout."
da506f05 2097 (eq (get-char-property (or pos (point)) 'invisible) 'allout))
773e7e48 2098
96b83743
EZ
2099;;;_ > allout-overlay-insert-in-front-handler (ol after beg end
2100;;; &optional prelen)
2101(defun allout-overlay-insert-in-front-handler (ol after beg end
2102 &optional prelen)
2a1408fd 2103 "Shift the overlay so stuff inserted in front of it is excluded."
96b83743 2104 (if after
1c9b9df0
KM
2105 ;; ??? Shouldn't moving the overlay should be unnecessary, if overlay
2106 ;; front-advance on the overlay worked as expected?
96b83743
EZ
2107 (move-overlay ol (1+ beg) (overlay-end ol))))
2108;;;_ > allout-overlay-interior-modification-handler (ol after beg end
2109;;; &optional prelen)
2110(defun allout-overlay-interior-modification-handler (ol after beg end
2111 &optional prelen)
2112 "Get confirmation before making arbitrary changes to invisible text.
2113
2114We expose the invisible text and ask for confirmation. Refusal or
75bcb912 2115`keyboard-quit' abandons the changes, with keyboard-quit additionally
96b83743
EZ
2116reclosing the opened text.
2117
60c7bbd2 2118No confirmation is necessary when `inhibit-read-only' is set -- eg, allout
96b83743
EZ
2119internal functions use this feature cohesively bunch changes."
2120
2121 (when (and (not inhibit-read-only) (not after))
2122 (let ((start (point))
2123 (ol-start (overlay-start ol))
2124 (ol-end (overlay-end ol))
96b83743
EZ
2125 first)
2126 (goto-char beg)
2127 (while (< (point) end)
2128 (when (allout-hidden-p)
2129 (allout-show-to-offshoot)
2130 (if (allout-hidden-p)
2131 (save-excursion (forward-char 1)
2132 (allout-show-to-offshoot)))
2133 (when (not first)
96b83743
EZ
2134 (setq first (point))))
2135 (goto-char (if (featurep 'xemacs)
2136 (next-property-change (1+ (point)) nil end)
2137 (next-char-property-change (1+ (point)) end))))
2138 (when first
2139 (goto-char first)
2140 (condition-case nil
2141 (if (not
2142 (yes-or-no-p
2143 (substitute-command-keys
4034b0e2 2144 (concat "Modify concealed text? (\"no\" just aborts,"
96b83743
EZ
2145 " \\[keyboard-quit] also reconceals) "))))
2146 (progn (goto-char start)
5a0c3f56 2147 (error "Concealed-text change refused")))
96b83743
EZ
2148 (quit (allout-flag-region ol-start ol-end nil)
2149 (allout-flag-region ol-start ol-end t)
5a0c3f56 2150 (error "Concealed-text change abandoned, text reconcealed"))))
96b83743
EZ
2151 (goto-char start))))
2152;;;_ > allout-before-change-handler (beg end)
2153(defun allout-before-change-handler (beg end)
2154 "Protect against changes to invisible text.
2155
60c7bbd2 2156See `allout-overlay-interior-modification-handler' for details."
d8350998 2157
13aa217b
KM
2158 (when (and (allout-mode-p) undo-in-progress)
2159 (setq allout-just-did-undo t)
2160 (if (allout-hidden-p)
2161 (allout-show-children)))
d8350998 2162
01fc9422
EZ
2163 ;; allout-overlay-interior-modification-handler on an overlay handles
2164 ;; this in other emacs, via `allout-exposure-category's 'modification-hooks.
2165 (when (and (featurep 'xemacs) (allout-mode-p))
2166 ;; process all of the pending overlays:
d8350998 2167 (save-excursion
f520c6f2 2168 (goto-char beg)
d8350998 2169 (let ((overlay (allout-get-invisibility-overlay)))
1c9b9df0
KM
2170 (if overlay
2171 (allout-overlay-interior-modification-handler
2172 overlay nil beg end nil))))))
96b83743
EZ
2173;;;_ > allout-isearch-end-handler (&optional overlay)
2174(defun allout-isearch-end-handler (&optional overlay)
2175 "Reconcile allout outline exposure on arriving in hidden text after isearch.
2176
2177Optional OVERLAY parameter is for when this function is used by
2178`isearch-open-invisible' overlay property. It is otherwise unused, so this
2179function can also be used as an `isearch-mode-end-hook'."
2180
2181 (if (and (allout-mode-p) (allout-hidden-p))
2182 (allout-show-to-offshoot)))
2183
60c7bbd2 2184;;;_ #3 Internal Position State-Tracking -- "allout-recent-*" funcs
a5a1e2ef
SM
2185;; All the basic outline functions that directly do string matches to
2186;; evaluate heading prefix location set the variables
2187;; `allout-recent-prefix-beginning' and `allout-recent-prefix-end'
2188;; when successful. Functions starting with `allout-recent-' all
2189;; use this state, providing the means to avoid redundant searches
2190;; for just-established data. This optimization can provide
2191;; significant speed improvement, but it must be employed carefully.
fd5359c6
MR
2192;;;_ = allout-recent-prefix-beginning
2193(defvar allout-recent-prefix-beginning 0
c567ac01 2194 "Buffer point of the start of the last topic prefix encountered.")
fd5359c6
MR
2195(make-variable-buffer-local 'allout-recent-prefix-beginning)
2196;;;_ = allout-recent-prefix-end
2197(defvar allout-recent-prefix-end 0
c567ac01 2198 "Buffer point of the end of the last topic prefix encountered.")
fd5359c6 2199(make-variable-buffer-local 'allout-recent-prefix-end)
d8350998
CY
2200;;;_ = allout-recent-depth
2201(defvar allout-recent-depth 0
2202 "Depth of the last topic prefix encountered.")
2203(make-variable-buffer-local 'allout-recent-depth)
fd5359c6
MR
2204;;;_ = allout-recent-end-of-subtree
2205(defvar allout-recent-end-of-subtree 0
2265e017 2206 "Buffer point last returned by `allout-end-of-current-subtree'.")
fd5359c6 2207(make-variable-buffer-local 'allout-recent-end-of-subtree)
d8350998
CY
2208;;;_ > allout-prefix-data ()
2209(defsubst allout-prefix-data ()
2210 "Register allout-prefix state data.
fd5359c6 2211
75bcb912
JB
2212For reference by `allout-recent' funcs. Return
2213the new value of `allout-recent-prefix-beginning'."
42c3e51c 2214 (setq allout-recent-prefix-end (or (match-end 1) (match-end 2) (match-end 3))
d8350998 2215 allout-recent-prefix-beginning (or (match-beginning 1)
42c3e51c
KM
2216 (match-beginning 2)
2217 (match-beginning 3))
d8350998
CY
2218 allout-recent-depth (max 1 (- allout-recent-prefix-end
2219 allout-recent-prefix-beginning
2220 allout-header-subtraction)))
2221 allout-recent-prefix-beginning)
2222;;;_ > nullify-allout-prefix-data ()
2223(defsubst nullify-allout-prefix-data ()
2224 "Mark allout prefix data as being uninformative."
2225 (setq allout-recent-prefix-end (point)
2226 allout-recent-prefix-beginning (point)
2227 allout-recent-depth 0)
2228 allout-recent-prefix-beginning)
fd5359c6 2229;;;_ > allout-recent-depth ()
d8350998 2230(defsubst allout-recent-depth ()
19b84ba3 2231 "Return depth of last heading encountered by an outline maneuvering function.
c567ac01
RS
2232
2233All outline functions which directly do string matches to assess
2265e017
MR
2234headings set the variables `allout-recent-prefix-beginning' and
2235`allout-recent-prefix-end' if successful. This function uses those settings
c567ac01
RS
2236to return the current depth."
2237
d8350998 2238 allout-recent-depth)
fd5359c6 2239;;;_ > allout-recent-prefix ()
d8350998 2240(defsubst allout-recent-prefix ()
2265e017 2241 "Like `allout-recent-depth', but returns text of last encountered prefix.
c567ac01
RS
2242
2243All outline functions which directly do string matches to assess
2265e017
MR
2244headings set the variables `allout-recent-prefix-beginning' and
2245`allout-recent-prefix-end' if successful. This function uses those settings
d8350998
CY
2246to return the current prefix."
2247 (buffer-substring-no-properties allout-recent-prefix-beginning
2248 allout-recent-prefix-end))
fd5359c6
MR
2249;;;_ > allout-recent-bullet ()
2250(defmacro allout-recent-bullet ()
2251 "Like allout-recent-prefix, but returns bullet of last encountered prefix.
c567ac01
RS
2252
2253All outline functions which directly do string matches to assess
2265e017
MR
2254headings set the variables `allout-recent-prefix-beginning' and
2255`allout-recent-prefix-end' if successful. This function uses those settings
c567ac01 2256to return the current depth of the most recently matched topic."
d8350998
CY
2257 '(buffer-substring-no-properties (1- allout-recent-prefix-end)
2258 allout-recent-prefix-end))
c567ac01 2259
19b84ba3 2260;;;_ #4 Navigation
c567ac01 2261
46771bb3 2262;;;_ - Position Assessment
c567ac01 2263;;;_ : Location Predicates
ede4ac6a
KS
2264;;;_ > allout-do-doublecheck ()
2265(defsubst allout-do-doublecheck ()
2266 "True if current item conditions qualify for checking on topic aberrance."
2267 (and
60c7bbd2 2268 ;; presume integrity of outline and yanked content during yank -- necessary
ede4ac6a 2269 ;; to allow for level disparity of yank location and yanked text:
498a6493 2270 (not allout-inhibit-aberrance-doublecheck)
ede4ac6a
KS
2271 ;; allout-doublecheck-at-and-shallower is ceiling for doublecheck:
2272 (<= allout-recent-depth allout-doublecheck-at-and-shallower)))
2273;;;_ > allout-aberrant-container-p ()
d8350998
CY
2274(defun allout-aberrant-container-p ()
2275 "True if topic, or next sibling with children, contains them discontinuously.
2276
2277Discontinuous means an immediate offspring that is nested more
2278than one level deeper than the topic.
2279
2280If topic has no offspring, then the next sibling with offspring will
2281determine whether or not this one is determined to be aberrant.
2282
2283If true, then the allout-recent-* settings are calibrated on the
2284offspring that qaulifies it as aberrant, ie with depth that
2285exceeds the topic by more than one."
2286
2287 ;; This is most clearly understood when considering standard-prefix-leader
2288 ;; low-level topics, which can all too easily match text not intended as
2289 ;; headers. For example, any line with a leading '.' or '*' and lacking a
2290 ;; following bullet qualifies without this protection. (A sequence of
2291 ;; them can occur naturally, eg a typical textual bullet list.) We
2292 ;; disqualify such low-level sequences when they are followed by a
2293 ;; discontinuously contained child, inferring that the sequences are not
2294 ;; actually connected with their prospective context.
2295
2296 (let ((depth (allout-depth))
2297 (start-point (point))
2298 done aberrant)
2a1408fd
DN
2299 (save-match-data
2300 (save-excursion
2301 (while (and (not done)
2302 (re-search-forward allout-line-boundary-regexp nil 0))
2303 (allout-prefix-data)
2304 (goto-char allout-recent-prefix-beginning)
2305 (cond
8dd59f01 2306 ;; sibling -- continue:
2a1408fd 2307 ((eq allout-recent-depth depth))
8dd59f01 2308 ;; first offspring is excessive -- aberrant:
2a1408fd
DN
2309 ((> allout-recent-depth (1+ depth))
2310 (setq done t aberrant t))
8dd59f01 2311 ;; next non-sibling is lower-depth -- not aberrant:
2a1408fd 2312 (t (setq done t))))))
d8350998
CY
2313 (if aberrant
2314 aberrant
2315 (goto-char start-point)
2316 ;; recalibrate allout-recent-*
2317 (allout-depth)
2318 nil)))
ede4ac6a
KS
2319;;;_ > allout-on-current-heading-p ()
2320(defun allout-on-current-heading-p ()
2321 "Return non-nil if point is on current visible topics' header line.
2322
2323Actually, returns prefix beginning point."
2324 (save-excursion
2325 (allout-beginning-of-current-line)
2a1408fd
DN
2326 (save-match-data
2327 (and (looking-at allout-regexp)
2328 (allout-prefix-data)
2329 (or (not (allout-do-doublecheck))
2330 (not (allout-aberrant-container-p)))))))
ede4ac6a
KS
2331;;;_ > allout-on-heading-p ()
2332(defalias 'allout-on-heading-p 'allout-on-current-heading-p)
2333;;;_ > allout-e-o-prefix-p ()
2334(defun allout-e-o-prefix-p ()
2335 "True if point is located where current topic prefix ends, heading begins."
2a1408fd
DN
2336 (and (save-match-data
2337 (save-excursion (let ((inhibit-field-text-motion t))
2338 (beginning-of-line))
2339 (looking-at allout-regexp))
2340 (= (point) (save-excursion (allout-end-of-prefix)(point))))))
c567ac01 2341;;;_ : Location attributes
fd5359c6 2342;;;_ > allout-depth ()
96b83743
EZ
2343(defun allout-depth ()
2344 "Return depth of topic most immediately containing point.
2345
42c3e51c
KM
2346Does not do doublecheck for aberrant topic header.
2347
96b83743
EZ
2348Return zero if point is not within any topic.
2349
2350Like `allout-current-depth', but respects hidden as well as visible topics."
9179616f 2351 (save-excursion
96b83743
EZ
2352 (let ((start-point (point)))
2353 (if (and (allout-goto-prefix)
2354 (not (< start-point (point))))
d8350998 2355 allout-recent-depth
96b83743 2356 (progn
d8350998
CY
2357 ;; Oops, no prefix, nullify it:
2358 (nullify-allout-prefix-data)
96b83743
EZ
2359 ;; ... and return 0:
2360 0)))))
fd5359c6 2361;;;_ > allout-current-depth ()
96b83743
EZ
2362(defun allout-current-depth ()
2363 "Return depth of visible topic most immediately containing point.
2364
2365Return zero if point is not within any topic."
2366 (save-excursion
2367 (if (allout-back-to-current-heading)
2368 (max 1
2369 (- allout-recent-prefix-end
2370 allout-recent-prefix-beginning
2371 allout-header-subtraction))
2372 0)))
fd5359c6
MR
2373;;;_ > allout-get-current-prefix ()
2374(defun allout-get-current-prefix ()
c567ac01 2375 "Topic prefix of the current topic."
1977b8f6 2376 (save-excursion
fd5359c6
MR
2377 (if (allout-goto-prefix)
2378 (allout-recent-prefix))))
2379;;;_ > allout-get-bullet ()
2380(defun allout-get-bullet ()
c567ac01 2381 "Return bullet of containing topic (visible or not)."
1977b8f6 2382 (save-excursion
fd5359c6
MR
2383 (and (allout-goto-prefix)
2384 (allout-recent-bullet))))
2385;;;_ > allout-current-bullet ()
2386(defun allout-current-bullet ()
c567ac01 2387 "Return bullet of current (visible) topic heading, or none if none found."
96b83743 2388 (condition-case nil
1977b8f6 2389 (save-excursion
fd5359c6 2390 (allout-back-to-current-heading)
d8350998
CY
2391 (buffer-substring-no-properties (- allout-recent-prefix-end 1)
2392 allout-recent-prefix-end))
1977b8f6 2393 ;; Quick and dirty provision, ostensibly for missing bullet:
d8350998 2394 (args-out-of-range nil))
1977b8f6 2395 )
fd5359c6
MR
2396;;;_ > allout-get-prefix-bullet (prefix)
2397(defun allout-get-prefix-bullet (prefix)
c567ac01 2398 "Return the bullet of the header prefix string PREFIX."
1977b8f6
RS
2399 ;; Doesn't make sense if we're old-style prefixes, but this just
2400 ;; oughtn't be called then, so forget about it...
fd5359c6 2401 (if (string-match allout-regexp prefix)
d8350998 2402 (substring prefix (1- (match-end 2)) (match-end 2))))
fd5359c6
MR
2403;;;_ > allout-sibling-index (&optional depth)
2404(defun allout-sibling-index (&optional depth)
9179616f
DL
2405 "Item number of this prospective topic among its siblings.
2406
e126900f 2407If optional arg DEPTH is greater than current depth, then we're
9179616f
DL
2408opening a new level, and return 0.
2409
2410If less than this depth, ascend to that depth and count..."
2411
2412 (save-excursion
2413 (cond ((and depth (<= depth 0) 0))
65e7eb3d 2414 ((or (null depth) (= depth (allout-depth)))
9179616f 2415 (let ((index 1))
d8350998 2416 (while (allout-previous-sibling allout-recent-depth nil)
9179616f
DL
2417 (setq index (1+ index)))
2418 index))
d8350998 2419 ((< depth allout-recent-depth)
fd5359c6
MR
2420 (allout-ascend-to-depth depth)
2421 (allout-sibling-index))
9179616f 2422 (0))))
fd5359c6
MR
2423;;;_ > allout-topic-flat-index ()
2424(defun allout-topic-flat-index ()
9179616f
DL
2425 "Return a list indicating point's numeric section.subsect.subsubsect...
2426Outermost is first."
fd5359c6
MR
2427 (let* ((depth (allout-depth))
2428 (next-index (allout-sibling-index depth))
9179616f
DL
2429 (rev-sibls nil))
2430 (while (> next-index 0)
2431 (setq rev-sibls (cons next-index rev-sibls))
2432 (setq depth (1- depth))
fd5359c6 2433 (setq next-index (allout-sibling-index depth)))
9179616f
DL
2434 rev-sibls)
2435 )
1977b8f6 2436
96b83743
EZ
2437;;;_ - Navigation routines
2438;;;_ > allout-beginning-of-current-line ()
2439(defun allout-beginning-of-current-line ()
2440 "Like beginning of line, but to visible text."
2441
5204800f
NR
2442 ;; This combination of move-beginning-of-line and beginning-of-line is
2443 ;; deliberate, but the (beginning-of-line) may now be superfluous.
0458fa80
EZ
2444 (let ((inhibit-field-text-motion t))
2445 (move-beginning-of-line 1)
96b83743 2446 (beginning-of-line)
0458fa80
EZ
2447 (while (and (not (bobp)) (or (not (bolp)) (allout-hidden-p)))
2448 (beginning-of-line)
2449 (if (or (allout-hidden-p) (not (bolp)))
2450 (forward-char -1)))))
96b83743
EZ
2451;;;_ > allout-end-of-current-line ()
2452(defun allout-end-of-current-line ()
2453 "Move to the end of line, past concealed text if any."
1c9b9df0 2454 ;; This is for symmetry with `allout-beginning-of-current-line' --
96b83743
EZ
2455 ;; `move-end-of-line' doesn't suffer the same problem as
2456 ;; `move-beginning-of-line'.
0458fa80 2457 (let ((inhibit-field-text-motion t))
96b83743 2458 (end-of-line)
0458fa80
EZ
2459 (while (allout-hidden-p)
2460 (end-of-line)
2461 (if (allout-hidden-p) (forward-char 1)))))
48bd8440
EZ
2462;;;_ > allout-beginning-of-line ()
2463(defun allout-beginning-of-line ()
2464 "Beginning-of-line with `allout-beginning-of-line-cycles' behavior, if set."
2465
2466 (interactive)
2467
2468 (if (or (not allout-beginning-of-line-cycles)
2469 (not (equal last-command this-command)))
2a1408fd
DN
2470 (progn
2471 (if (and (not (bolp))
2472 (allout-hidden-p (1- (point))))
1c9b9df0 2473 (goto-char (allout-previous-single-char-property-change
2a1408fd
DN
2474 (1- (point)) 'invisible)))
2475 (move-beginning-of-line 1))
d8350998
CY
2476 (allout-depth)
2477 (let ((beginning-of-body
2478 (save-excursion
ede4ac6a 2479 (while (and (allout-do-doublecheck)
d8350998
CY
2480 (allout-aberrant-container-p)
2481 (allout-previous-visible-heading 1)))
2482 (allout-beginning-of-current-entry)
2483 (point))))
48bd8440 2484 (cond ((= (current-column) 0)
d8350998 2485 (goto-char beginning-of-body))
48bd8440
EZ
2486 ((< (point) beginning-of-body)
2487 (allout-beginning-of-current-line))
2488 ((= (point) beginning-of-body)
2489 (goto-char (allout-current-bullet-pos)))
2490 (t (allout-beginning-of-current-line)
2491 (if (< (point) beginning-of-body)
2492 ;; we were on the headline after its start:
d8350998 2493 (goto-char beginning-of-body)))))))
48bd8440
EZ
2494;;;_ > allout-end-of-line ()
2495(defun allout-end-of-line ()
2496 "End-of-line with `allout-end-of-line-cycles' behavior, if set."
2497
2498 (interactive)
2499
2500 (if (or (not allout-end-of-line-cycles)
2501 (not (equal last-command this-command)))
2502 (allout-end-of-current-line)
2503 (let ((end-of-entry (save-excursion
2504 (allout-end-of-entry)
2505 (point))))
2506 (cond ((not (eolp))
2507 (allout-end-of-current-line))
2508 ((or (allout-hidden-p) (save-excursion
2509 (forward-char -1)
2510 (allout-hidden-p)))
2511 (allout-back-to-current-heading)
2512 (allout-show-current-entry)
d8350998 2513 (allout-show-children)
48bd8440
EZ
2514 (allout-end-of-entry))
2515 ((>= (point) end-of-entry)
2516 (allout-back-to-current-heading)
2517 (allout-end-of-current-line))
2a1408fd 2518 (t
1c9b9df0 2519 (if (not (allout-mark-active-p))
2a1408fd
DN
2520 (push-mark))
2521 (allout-end-of-entry))))))
1c9b9df0
KM
2522;;;_ > allout-mark-active-p ()
2523(defun allout-mark-active-p ()
2524 "True if the mark is currently or always active."
2525 ;; `(cond (boundp...))' (or `(if ...)') invokes special byte-compiler
2526 ;; provisions, at least in fsf emacs to prevent warnings about lack of,
2527 ;; eg, region-active-p.
2528 (cond ((boundp 'mark-active)
2529 mark-active)
2530 ((fboundp 'region-active-p)
2531 (region-active-p))
2532 (t)))
fd5359c6
MR
2533;;;_ > allout-next-heading ()
2534(defsubst allout-next-heading ()
c07583cd 2535 "Move to the heading for the topic (possibly invisible) after this one.
c567ac01 2536
d8350998 2537Returns the location of the heading, or nil if none found.
c567ac01 2538
75bcb912 2539We skip anomalous low-level topics, a la `allout-aberrant-container-p'."
2a1408fd
DN
2540 (save-match-data
2541
2542 (if (looking-at allout-regexp)
2543 (forward-char 1))
2544
2545 (when (re-search-forward allout-line-boundary-regexp nil 0)
2546 (allout-prefix-data)
42c3e51c
KM
2547 (goto-char allout-recent-prefix-beginning)
2548 (while (not (bolp))
2549 (forward-char -1))
2a1408fd
DN
2550 (and (allout-do-doublecheck)
2551 ;; this will set allout-recent-* on the first non-aberrant topic,
2552 ;; whether it's the current one or one that disqualifies it:
2553 (allout-aberrant-container-p))
42c3e51c 2554 ;; this may or may not be the same as above depending on doublecheck:
2a1408fd 2555 (goto-char allout-recent-prefix-beginning))))
96b83743 2556;;;_ > allout-this-or-next-heading
fd5359c6 2557(defun allout-this-or-next-heading ()
8d118843 2558 "Position cursor on current or next heading."
fd5359c6
MR
2559 ;; A throwaway non-macro that is defined after allout-next-heading
2560 ;; and usable by allout-mode.
d8350998 2561 (if (not (allout-goto-prefix-doublechecked)) (allout-next-heading)))
fd5359c6 2562;;;_ > allout-previous-heading ()
8a559d4e 2563(defun allout-previous-heading ()
c07583cd 2564 "Move to the prior (possibly invisible) heading line.
c567ac01 2565
d8350998
CY
2566Return the location of the beginning of the heading, or nil if not found.
2567
75bcb912 2568We skip anomalous low-level topics, a la `allout-aberrant-container-p'."
d8350998
CY
2569
2570 (if (bobp)
2571 nil
d8350998 2572 (let ((start-point (point)))
ede4ac6a 2573 ;; allout-goto-prefix-doublechecked calls us, so we can't use it here.
d8350998 2574 (allout-goto-prefix)
2a1408fd
DN
2575 (save-match-data
2576 (when (or (re-search-backward allout-line-boundary-regexp nil 0)
2577 (looking-at allout-bob-regexp))
2578 (goto-char (allout-prefix-data))
2579 (if (and (allout-do-doublecheck)
2580 (allout-aberrant-container-p))
2581 (or (allout-previous-heading)
2582 (and (goto-char start-point)
2583 ;; recalibrate allout-recent-*:
2584 (allout-depth)
2585 nil))
2586 (point)))))))
96b83743
EZ
2587;;;_ > allout-get-invisibility-overlay ()
2588(defun allout-get-invisibility-overlay ()
2589 "Return the overlay at point that dictates allout invisibility."
2590 (let ((overlays (overlays-at (point)))
2591 got)
2592 (while (and overlays (not got))
2593 (if (equal (overlay-get (car overlays) 'invisible) 'allout)
d8350998
CY
2594 (setq got (car overlays))
2595 (pop overlays)))
96b83743
EZ
2596 got))
2597;;;_ > allout-back-to-visible-text ()
2598(defun allout-back-to-visible-text ()
2599 "Move to most recent prior character that is visible, and return point."
2600 (if (allout-hidden-p)
2601 (goto-char (overlay-start (allout-get-invisibility-overlay))))
2602 (point))
c567ac01
RS
2603
2604;;;_ - Subtree Charting
2605;;;_ " These routines either produce or assess charts, which are
2606;;; nested lists of the locations of topics within a subtree.
2607;;;
d8350998
CY
2608;;; Charts enable efficient subtree navigation by providing a reusable basis
2609;;; for elaborate, compound assessment and adjustment of a subtree.
c567ac01 2610
48bd8440
EZ
2611;;;_ > allout-chart-subtree (&optional levels visible orig-depth prev-depth)
2612(defun allout-chart-subtree (&optional levels visible orig-depth prev-depth)
19b84ba3
RS
2613 "Produce a location \"chart\" of subtopics of the containing topic.
2614
c07583cd 2615Optional argument LEVELS specifies a depth limit (relative to start
65e7eb3d 2616depth) for the chart. Null LEVELS means no limit.
48bd8440
EZ
2617
2618When optional argument VISIBLE is non-nil, the chart includes
2619only the visible subelements of the charted subjects.
2620
65e7eb3d 2621The remaining optional args are for internal use by the function.
d82979ea
EZ
2622
2623Point is left at the end of the subtree.
19b84ba3 2624
d82979ea 2625Charts are used to capture outline structure, so that outline-altering
19b84ba3
RS
2626routines need assess the structure only once, and then use the chart
2627for their elaborate manipulations.
2628
d8350998
CY
2629The chart entries for the topics are in reverse order, so the
2630last topic is listed first. The entry for each topic consists of
2631an integer indicating the point at the beginning of the topic
2632prefix. Charts for offspring consists of a list containing,
2633recursively, the charts for the respective subtopics. The chart
2634for a topics' offspring precedes the entry for the topic itself.
19b84ba3 2635
d82979ea
EZ
2636The other function parameters are for internal recursion, and should
2637not be specified by external callers. ORIG-DEPTH is depth of topic at
2638starting point, and PREV-DEPTH is depth of prior topic."
19b84ba3 2639
353e2ef2 2640 (let ((original (not orig-depth)) ; `orig-depth' set only in recursion.
19b84ba3
RS
2641 chart curr-depth)
2642
2643 (if original ; Just starting?
2644 ; Register initial settings and
2645 ; position to first offspring:
fd5359c6 2646 (progn (setq orig-depth (allout-depth))
19b84ba3 2647 (or prev-depth (setq prev-depth (1+ orig-depth)))
48bd8440
EZ
2648 (if visible
2649 (allout-next-visible-heading 1)
2650 (allout-next-heading))))
c567ac01 2651
19b84ba3
RS
2652 ;; Loop over the current levels' siblings. Besides being more
2653 ;; efficient than tail-recursing over a level, it avoids exceeding
e126900f 2654 ;; the typically quite constrained Emacs max-lisp-eval-depth.
9179616f 2655 ;;
19b84ba3
RS
2656 ;; Probably would speed things up to implement loop-based stack
2657 ;; operation rather than recursing for lower levels. Bah.
9179616f 2658
c567ac01 2659 (while (and (not (eobp))
19b84ba3 2660 ; Still within original topic?
d8350998 2661 (< orig-depth (setq curr-depth allout-recent-depth))
19b84ba3
RS
2662 (cond ((= prev-depth curr-depth)
2663 ;; Register this one and move on:
d8350998 2664 (setq chart (cons allout-recent-prefix-beginning chart))
19b84ba3 2665 (if (and levels (<= levels 1))
60c7bbd2 2666 ;; At depth limit -- skip sublevels:
fd5359c6 2667 (or (allout-next-sibling curr-depth)
60c7bbd2 2668 ;; or no more siblings -- proceed to
19b84ba3 2669 ;; next heading at lesser depth:
e6a9aec7 2670 (while (and (<= curr-depth
d8350998 2671 allout-recent-depth)
48bd8440
EZ
2672 (if visible
2673 (allout-next-visible-heading 1)
2674 (allout-next-heading)))))
2675 (if visible
2676 (allout-next-visible-heading 1)
2677 (allout-next-heading))))
19b84ba3
RS
2678
2679 ((and (< prev-depth curr-depth)
2680 (or (not levels)
2681 (> levels 0)))
2682 ;; Recurse on deeper level of curr topic:
2683 (setq chart
fd5359c6 2684 (cons (allout-chart-subtree (and levels
19b84ba3 2685 (1- levels))
48bd8440
EZ
2686 visible
2687 orig-depth
2688 curr-depth)
19b84ba3
RS
2689 chart))
2690 ;; ... then continue with this one.
2691 )
2692
2693 ;; ... else nil if we've ascended back to prev-depth.
2694
2695 )))
2696
2697 (if original ; We're at the last sibling on
2698 ; the original level. Position
2699 ; to the end of it:
c567ac01 2700 (progn (and (not (eobp)) (forward-char -1))
96b83743
EZ
2701 (and (= (preceding-char) ?\n)
2702 (= (aref (buffer-substring (max 1 (- (point) 3))
2703 (point))
2704 1)
2705 ?\n)
19b84ba3 2706 (forward-char -1))
fd5359c6 2707 (setq allout-recent-end-of-subtree (point))))
353e2ef2 2708
c567ac01
RS
2709 chart ; (nreverse chart) not necessary,
2710 ; and maybe not preferable.
2711 ))
fd5359c6
MR
2712;;;_ > allout-chart-siblings (&optional start end)
2713(defun allout-chart-siblings (&optional start end)
c567ac01 2714 "Produce a list of locations of this and succeeding sibling topics.
fd5359c6 2715Effectively a top-level chart of siblings. See `allout-chart-subtree'
c567ac01
RS
2716for an explanation of charts."
2717 (save-excursion
d8350998
CY
2718 (when (allout-goto-prefix-doublechecked)
2719 (let ((chart (list (point))))
2720 (while (allout-next-sibling)
2721 (setq chart (cons (point) chart)))
2722 (if chart (setq chart (nreverse chart)))))))
fd5359c6
MR
2723;;;_ > allout-chart-to-reveal (chart depth)
2724(defun allout-chart-to-reveal (chart depth)
c567ac01 2725
19b84ba3 2726 "Return a flat list of hidden points in subtree CHART, up to DEPTH.
c567ac01 2727
65e7eb3d
EZ
2728If DEPTH is nil, include hidden points at any depth.
2729
19b84ba3 2730Note that point can be left at any of the points on chart, or at the
c567ac01
RS
2731start point."
2732
2733 (let (result here)
65e7eb3d 2734 (while (and (or (null depth) (> depth 0))
c567ac01
RS
2735 chart)
2736 (setq here (car chart))
2737 (if (listp here)
65e7eb3d
EZ
2738 (let ((further (allout-chart-to-reveal here (if (null depth)
2739 depth
2740 (1- depth)))))
60c7bbd2 2741 ;; We're on the start of a subtree -- recurse with it, if there's
c567ac01
RS
2742 ;; more depth to go:
2743 (if further (setq result (append further result)))
2744 (setq chart (cdr chart)))
2745 (goto-char here)
96b83743 2746 (if (allout-hidden-p)
c567ac01
RS
2747 (setq result (cons here result)))
2748 (setq chart (cdr chart))))
2749 result))
fd5359c6 2750;;;_ X allout-chart-spec (chart spec &optional exposing)
e126900f 2751;; (defun allout-chart-spec (chart spec &optional exposing)
c07583cd 2752;; "Not yet (if ever) implemented.
e126900f
JB
2753
2754;; Produce exposure directives given topic/subtree CHART and an exposure SPEC.
2755
2756;; Exposure spec indicates the locations to be exposed and the prescribed
2757;; exposure status. Optional arg EXPOSING is an integer, with 0
2758;; indicating pending concealment, anything higher indicating depth to
2759;; which subtopic headers should be exposed, and negative numbers
2760;; indicating (negative of) the depth to which subtopic headers and
2761;; bodies should be exposed.
2762
2763;; The produced list can have two types of entries. Bare numbers
2764;; indicate points in the buffer where topic headers that should be
2765;; exposed reside.
2766
2767;; - bare negative numbers indicates that the topic starting at the
2768;; point which is the negative of the number should be opened,
2769;; including their entries.
2770;; - bare positive values indicate that this topic header should be
2771;; opened.
2772;; - Lists signify the beginning and end points of regions that should
c07583cd 2773;; be flagged, and the flag to employ. (For concealment: `(\?r)', and
e126900f
JB
2774;; exposure:"
2775;; (while spec
2776;; (cond ((listp spec)
2777;; )
2778;; )
2779;; (setq spec (cdr spec)))
2780;; )
c567ac01
RS
2781
2782;;;_ - Within Topic
fd5359c6
MR
2783;;;_ > allout-goto-prefix ()
2784(defun allout-goto-prefix ()
9179616f 2785 "Put point at beginning of immediately containing outline topic.
19b84ba3 2786
9179616f 2787Goes to most immediate subsequent topic if none immediately containing.
19b84ba3
RS
2788
2789Not sensitive to topic visibility.
c567ac01 2790
d27081f5 2791Returns the point at the beginning of the prefix, or nil if none."
c567ac01 2792
2a1408fd
DN
2793 (save-match-data
2794 (let (done)
2795 (while (and (not done)
2796 (search-backward "\n" nil 1))
2797 (forward-char 1)
2798 (if (looking-at allout-regexp)
2799 (setq done (allout-prefix-data))
2800 (forward-char -1)))
2801 (if (bobp)
2802 (cond ((looking-at allout-regexp)
2803 (allout-prefix-data))
2804 ((allout-next-heading))
2805 (done))
2806 done))))
d8350998
CY
2807;;;_ > allout-goto-prefix-doublechecked ()
2808(defun allout-goto-prefix-doublechecked ()
2809 "Put point at beginning of immediately containing outline topic.
2810
c07583cd 2811Like `allout-goto-prefix', but shallow topics (according to
65e7eb3d
EZ
2812`allout-doublecheck-at-and-shallower') are checked and
2813disqualified for child containment discontinuity, according to
2814`allout-aberrant-container-p'."
ede4ac6a
KS
2815 (if (allout-goto-prefix)
2816 (if (and (allout-do-doublecheck)
2817 (allout-aberrant-container-p))
2818 (allout-previous-heading)
2819 (point))))
d8350998 2820
fd5359c6
MR
2821;;;_ > allout-end-of-prefix ()
2822(defun allout-end-of-prefix (&optional ignore-decorations)
19b84ba3
RS
2823 "Position cursor at beginning of header text.
2824
2825If optional IGNORE-DECORATIONS is non-nil, put just after bullet,
2826otherwise skip white space between bullet and ensuing text."
c567ac01 2827
d8350998 2828 (if (not (allout-goto-prefix-doublechecked))
1977b8f6 2829 nil
d8350998 2830 (goto-char allout-recent-prefix-end)
2a1408fd
DN
2831 (save-match-data
2832 (if ignore-decorations
2833 t
2834 (while (looking-at "[0-9]") (forward-char 1))
2835 (if (and (not (eolp)) (looking-at "\\s-")) (forward-char 1))))
1977b8f6 2836 ;; Reestablish where we are:
fd5359c6
MR
2837 (allout-current-depth)))
2838;;;_ > allout-current-bullet-pos ()
2839(defun allout-current-bullet-pos ()
c07583cd 2840 "Return position of current (visible) topic's bullet."
c567ac01 2841
8a559d4e 2842 (if (not (allout-current-depth))
c567ac01 2843 nil
8a559d4e 2844 (1- allout-recent-prefix-end)))
1c9b9df0
KM
2845;;;_ > allout-back-to-current-heading (&optional interactive)
2846(defun allout-back-to-current-heading (&optional interactive)
80f66ef4 2847 "Move to heading line of current topic, or beginning if not in a topic.
96b83743 2848
80f66ef4
CY
2849If interactive, we position at the end of the prefix.
2850
2851Return value of resulting point, unless we started outside
2852of (before any) topics, in which case we return nil."
96b83743 2853
1c9b9df0
KM
2854 (interactive "p")
2855
96b83743 2856 (allout-beginning-of-current-line)
80f66ef4 2857 (let ((bol-point (point)))
1e004a83
KM
2858 (when (allout-goto-prefix-doublechecked)
2859 (if (<= (point) bol-point)
2860 (progn
2861 (setq bol-point (point))
2862 (allout-beginning-of-current-line)
2863 (if (not (= bol-point (point)))
2864 (if (looking-at allout-regexp)
2865 (allout-prefix-data)))
1c9b9df0 2866 (if interactive
ede4ac6a 2867 (allout-end-of-prefix)
1e004a83
KM
2868 (point)))
2869 (goto-char (point-min))
2870 nil))))
fd5359c6
MR
2871;;;_ > allout-back-to-heading ()
2872(defalias 'allout-back-to-heading 'allout-back-to-current-heading)
96b83743
EZ
2873;;;_ > allout-pre-next-prefix ()
2874(defun allout-pre-next-prefix ()
1977b8f6
RS
2875 "Skip forward to just before the next heading line.
2876
c567ac01 2877Returns that character position."
1977b8f6 2878
8a559d4e
EZ
2879 (if (allout-next-heading)
2880 (goto-char (1- allout-recent-prefix-beginning))))
96b83743
EZ
2881;;;_ > allout-end-of-subtree (&optional current include-trailing-blank)
2882(defun allout-end-of-subtree (&optional current include-trailing-blank)
d82979ea
EZ
2883 "Put point at the end of the last leaf in the containing topic.
2884
96b83743
EZ
2885Optional CURRENT means put point at the end of the containing
2886visible topic.
2887
2888Optional INCLUDE-TRAILING-BLANK means include a trailing blank line, if
2889any, as part of the subtree. Otherwise, that trailing blank will be
2890excluded as delimiting whitespace between topics.
d82979ea
EZ
2891
2892Returns the value of point."
2893 (interactive "P")
2894 (if current
2895 (allout-back-to-current-heading)
d8350998
CY
2896 (allout-goto-prefix-doublechecked))
2897 (let ((level allout-recent-depth))
fd5359c6 2898 (allout-next-heading)
1977b8f6 2899 (while (and (not (eobp))
d8350998 2900 (> allout-recent-depth level))
fd5359c6 2901 (allout-next-heading))
48bd8440
EZ
2902 (if (eobp)
2903 (allout-end-of-entry)
2904 (forward-char -1))
96b83743 2905 (if (and (not include-trailing-blank) (= ?\n (preceding-char)))
c567ac01 2906 (forward-char -1))
fd5359c6 2907 (setq allout-recent-end-of-subtree (point))))
96b83743
EZ
2908;;;_ > allout-end-of-current-subtree (&optional include-trailing-blank)
2909(defun allout-end-of-current-subtree (&optional include-trailing-blank)
2910
d82979ea
EZ
2911 "Put point at end of last leaf in currently visible containing topic.
2912
96b83743
EZ
2913Optional INCLUDE-TRAILING-BLANK means include a trailing blank line, if
2914any, as part of the subtree. Otherwise, that trailing blank will be
2915excluded as delimiting whitespace between topics.
2916
d82979ea
EZ
2917Returns the value of point."
2918 (interactive)
96b83743 2919 (allout-end-of-subtree t include-trailing-blank))
1c9b9df0
KM
2920;;;_ > allout-beginning-of-current-entry (&optional interactive)
2921(defun allout-beginning-of-current-entry (&optional interactive)
d82979ea 2922 "When not already there, position point at beginning of current topic header.
c567ac01
RS
2923
2924If already there, move cursor to bullet for hot-spot operation.
46771bb3 2925\(See `allout-mode' doc string for details of hot-spot operation.)"
1c9b9df0 2926 (interactive "p")
c567ac01 2927 (let ((start-point (point)))
46771bb3 2928 (move-beginning-of-line 1)
d8350998
CY
2929 (if (< 0 (allout-current-depth))
2930 (goto-char allout-recent-prefix-end)
2931 (goto-char (point-min)))
fd5359c6 2932 (allout-end-of-prefix)
1c9b9df0 2933 (if (and interactive
c567ac01 2934 (= (point) start-point))
fd5359c6 2935 (goto-char (allout-current-bullet-pos)))))
96b83743
EZ
2936;;;_ > allout-end-of-entry (&optional inclusive)
2937(defun allout-end-of-entry (&optional inclusive)
2938 "Position the point at the end of the current topics' entry.
2939
2940Optional INCLUSIVE means also include trailing empty line, if any. When
2941unset, whitespace between items separates them even when the items are
2942collapsed."
1977b8f6 2943 (interactive)
96b83743
EZ
2944 (allout-pre-next-prefix)
2945 (if (and (not inclusive) (not (bobp)) (= ?\n (preceding-char)))
2946 (forward-char -1))
2947 (point))
fd5359c6
MR
2948;;;_ > allout-end-of-current-heading ()
2949(defun allout-end-of-current-heading ()
9179616f 2950 (interactive)
fd5359c6 2951 (allout-beginning-of-current-entry)
96b83743 2952 (search-forward "\n" nil t)
d82979ea 2953 (forward-char -1))
fd5359c6 2954(defalias 'allout-end-of-heading 'allout-end-of-current-heading)
d82979ea
EZ
2955;;;_ > allout-get-body-text ()
2956(defun allout-get-body-text ()
2957 "Return the unmangled body text of the topic immediately containing point."
2958 (save-excursion
2959 (allout-end-of-prefix)
96b83743 2960 (if (not (search-forward "\n" nil t))
d82979ea
EZ
2961 nil
2962 (backward-char 1)
2963 (let ((pre-body (point)))
2964 (if (not pre-body)
2965 nil
96b83743 2966 (allout-end-of-entry t)
d82979ea
EZ
2967 (if (not (= pre-body (point)))
2968 (buffer-substring-no-properties (1+ pre-body) (point))))
2969 )
2970 )
2971 )
2972 )
1977b8f6 2973
c567ac01 2974;;;_ - Depth-wise
fd5359c6
MR
2975;;;_ > allout-ascend-to-depth (depth)
2976(defun allout-ascend-to-depth (depth)
c567ac01 2977 "Ascend to depth DEPTH, returning depth if successful, nil if not."
fd5359c6 2978 (if (and (> depth 0)(<= depth (allout-depth)))
d8350998
CY
2979 (let (last-ascended)
2980 (while (and (< depth allout-recent-depth)
2981 (setq last-ascended (allout-ascend))))
2982 (goto-char allout-recent-prefix-beginning)
1c9b9df0 2983 (if (allout-called-interactively-p) (allout-end-of-prefix))
d8350998 2984 (and last-ascended allout-recent-depth))))
1c9b9df0 2985;;;_ > allout-ascend (&optional dont-move-if-unsuccessful)
c07583cd
KS
2986(defun allout-ascend (&optional dont-move-if-unsuccessful)
2987 "Ascend one level, returning resulting depth if successful, nil if not.
2988
2989Point is left at the beginning of the level whether or not
2990successful, unless optional DONT-MOVE-IF-UNSUCCESSFUL is set, in
2991which case point is returned to its original starting location."
2992 (if dont-move-if-unsuccessful
2993 (setq dont-move-if-unsuccessful (point)))
d82979ea
EZ
2994 (prog1
2995 (if (allout-beginning-of-level)
498a6493
CY
2996 (let ((bolevel (point))
2997 (bolevel-depth allout-recent-depth))
2998 (allout-previous-heading)
c07583cd
KS
2999 (cond ((< allout-recent-depth bolevel-depth)
3000 allout-recent-depth)
3001 ((= allout-recent-depth bolevel-depth)
3002 (if dont-move-if-unsuccessful
3003 (goto-char dont-move-if-unsuccessful))
3004 (allout-depth)
3005 nil)
3006 (t
3007 ;; some topic after very first is lower depth than first:
3008 (goto-char bolevel)
3009 (allout-depth)
3010 nil))))
1c9b9df0 3011 (if (allout-called-interactively-p) (allout-end-of-prefix))))
fd5359c6
MR
3012;;;_ > allout-descend-to-depth (depth)
3013(defun allout-descend-to-depth (depth)
19b84ba3
RS
3014 "Descend to depth DEPTH within current topic.
3015
3016Returning depth if successful, nil if not."
1977b8f6 3017 (let ((start-point (point))
fd5359c6 3018 (start-depth (allout-depth)))
1977b8f6 3019 (while
fd5359c6 3020 (and (> (allout-depth) 0)
d8350998 3021 (not (= depth allout-recent-depth)) ; ... not there yet
fd5359c6 3022 (allout-next-heading) ; ... go further
d8350998 3023 (< start-depth allout-recent-depth))) ; ... still in topic
fd5359c6 3024 (if (and (> (allout-depth) 0)
d8350998 3025 (= allout-recent-depth depth))
1977b8f6
RS
3026 depth
3027 (goto-char start-point)
3028 nil))
3029 )
d8350998
CY
3030;;;_ > allout-up-current-level (arg)
3031(defun allout-up-current-level (arg)
3032 "Move out ARG levels from current visible topic."
d82979ea 3033 (interactive "p")
8a559d4e
EZ
3034 (let ((start-point (point)))
3035 (allout-back-to-current-heading)
3036 (if (not (allout-ascend))
3037 (progn (goto-char start-point)
3038 (error "Can't ascend past outermost level"))
1c9b9df0 3039 (if (allout-called-interactively-p) (allout-end-of-prefix))
8a559d4e 3040 allout-recent-prefix-beginning)))
1977b8f6 3041
c567ac01 3042;;;_ - Linear
fd5359c6
MR
3043;;;_ > allout-next-sibling (&optional depth backward)
3044(defun allout-next-sibling (&optional depth backward)
2265e017 3045 "Like `allout-forward-current-level', but respects invisible topics.
1977b8f6 3046
c567ac01 3047Traverse at optional DEPTH, or current depth if none specified.
1977b8f6 3048
c567ac01 3049Go backward if optional arg BACKWARD is non-nil.
1977b8f6 3050
d8350998 3051Return the start point of the new topic if successful, nil otherwise."
1977b8f6 3052
d8350998 3053 (if (if backward (bobp) (eobp))
1977b8f6 3054 nil
d8350998 3055 (let ((target-depth (or depth (allout-depth)))
1977b8f6 3056 (start-point (point))
ede4ac6a 3057 (start-prefix-beginning allout-recent-prefix-beginning)
d8350998
CY
3058 (count 0)
3059 leaping
c567ac01 3060 last-depth)
d8350998
CY
3061 (while (and
3062 ;; done too few single steps to resort to the leap routine:
3063 (not leaping)
3064 ;; not at limit:
3065 (not (if backward (bobp) (eobp)))
3066 ;; still traversable:
3067 (if backward (allout-previous-heading) (allout-next-heading))
3068 ;; we're below the target depth
3069 (> (setq last-depth allout-recent-depth) target-depth))
3070 (setq count (1+ count))
3071 (if (> count 7) ; lists are commonly 7 +- 2, right?-)
3072 (setq leaping t)))
3073 (cond (leaping
3074 (or (allout-next-sibling-leap target-depth backward)
3075 (progn
3076 (goto-char start-point)
3077 (if depth (allout-depth) target-depth)
3078 nil)))
3079 ((and (not (eobp))
3080 (and (> (or last-depth (allout-depth)) 0)
ede4ac6a
KS
3081 (= allout-recent-depth target-depth))
3082 (not (= start-prefix-beginning
3083 allout-recent-prefix-beginning)))
d8350998
CY
3084 allout-recent-prefix-beginning)
3085 (t
3086 (goto-char start-point)
3087 (if depth (allout-depth) target-depth)
3088 nil)))))
3089;;;_ > allout-next-sibling-leap (&optional depth backward)
3090(defun allout-next-sibling-leap (&optional depth backward)
3091 "Like `allout-next-sibling', but by direct search for topic at depth.
3092
3093Traverse at optional DEPTH, or current depth if none specified.
3094
3095Go backward if optional arg BACKWARD is non-nil.
3096
3097Return the start point of the new topic if successful, nil otherwise.
3098
3099Costs more than regular `allout-next-sibling' for short traversals:
3100
c07583cd 3101 - we have to check the prior (next, if travelling backwards)
d8350998
CY
3102 item to confirm connectivity with the prior topic, and
3103 - if confirmed, we have to reestablish the allout-recent-* settings with
3104 some extra navigation
3105 - if confirmation fails, we have to do more work to recover
3106
3107It is an increasingly big win when there are many intervening
3108offspring before the next sibling, however, so
3109`allout-next-sibling' resorts to this if it finds itself in that
3110situation."
3111
3112 (if (if backward (bobp) (eobp))
3113 nil
3114 (let* ((start-point (point))
3115 (target-depth (or depth (allout-depth)))
3116 (search-whitespace-regexp nil)
3117 (depth-biased (- target-depth 2))
3118 (expression (if (<= target-depth 1)
3119 allout-depth-one-regexp
3120 (format allout-depth-specific-regexp
3121 depth-biased depth-biased)))
3122 found
3123 done)
3124 (while (not done)
2a1408fd
DN
3125 (setq found (save-match-data
3126 (if backward
3127 (re-search-backward expression nil 'to-limit)
3128 (forward-char 1)
3129 (re-search-forward expression nil 'to-limit))))
d8350998
CY
3130 (if (and found (allout-aberrant-container-p))
3131 (setq found nil))
3132 (setq done (or found (if backward (bobp) (eobp)))))
3133 (if (not found)
3134 (progn (goto-char start-point)
3135 nil)
3136 ;; rationale: if any intervening items were at a lower depth, we
60c7bbd2 3137 ;; would now be on the first offspring at the target depth -- ie,
97610156 3138 ;; the preceding item (per the search direction) must be at a
d8350998
CY
3139 ;; lesser depth. that's all we need to check.
3140 (if backward (allout-next-heading) (allout-previous-heading))
3141 (if (< allout-recent-depth target-depth)
3142 ;; return to start and reestablish allout-recent-*:
3143 (progn
3144 (goto-char start-point)
3145 (allout-depth)
3146 nil)
3147 (goto-char found)
3148 ;; locate cursor and set allout-recent-*:
3149 (allout-goto-prefix))))))
fd5359c6
MR
3150;;;_ > allout-previous-sibling (&optional depth backward)
3151(defun allout-previous-sibling (&optional depth backward)
d82979ea 3152 "Like `allout-forward-current-level' backwards, respecting invisible topics.
1977b8f6 3153
c567ac01 3154Optional DEPTH specifies depth to traverse, default current depth.
1977b8f6 3155
c567ac01 3156Optional BACKWARD reverses direction.
1977b8f6 3157
c567ac01 3158Return depth if successful, nil otherwise."
fd5359c6 3159 (allout-next-sibling depth (not backward))
1977b8f6 3160 )
fd5359c6
MR
3161;;;_ > allout-snug-back ()
3162(defun allout-snug-back ()
539d7736 3163 "Position cursor at end of previous topic.
19b84ba3
RS
3164
3165Presumes point is at the start of a topic prefix."
c567ac01
RS
3166 (if (or (bobp) (eobp))
3167 nil
3168 (forward-char -1))
96b83743 3169 (if (or (bobp) (not (= ?\n (preceding-char))))
c567ac01 3170 nil
96b83743 3171 (forward-char -1))
c567ac01 3172 (point))
fd5359c6
MR
3173;;;_ > allout-beginning-of-level ()
3174(defun allout-beginning-of-level ()
c567ac01 3175 "Go back to the first sibling at this level, visible or not."
fd5359c6
MR
3176 (allout-end-of-level 'backward))
3177;;;_ > allout-end-of-level (&optional backward)
3178(defun allout-end-of-level (&optional backward)
c567ac01 3179 "Go to the last sibling at this level, visible or not."
1977b8f6 3180
fd5359c6
MR
3181 (let ((depth (allout-depth)))
3182 (while (allout-previous-sibling depth nil))
d8350998 3183 (prog1 allout-recent-depth
1c9b9df0 3184 (if (allout-called-interactively-p) (allout-end-of-prefix)))))
fd5359c6
MR
3185;;;_ > allout-next-visible-heading (arg)
3186(defun allout-next-visible-heading (arg)
19b84ba3 3187 "Move to the next ARG'th visible heading line, backward if arg is negative.
c567ac01 3188
96b83743 3189Move to buffer limit in indicated direction if headings are exhausted."
1977b8f6 3190
1977b8f6 3191 (interactive "p")
0458fa80
EZ
3192 (let* ((inhibit-field-text-motion t)
3193 (backward (if (< arg 0) (setq arg (* -1 arg))))
c567ac01 3194 (step (if backward -1 1))
82b392a4 3195 (progress (allout-current-bullet-pos))
c567ac01
RS
3196 prev got)
3197
d8350998
CY
3198 (while (> arg 0)
3199 (while (and
3200 ;; Boundary condition:
3201 (not (if backward (bobp)(eobp)))
3202 ;; Move, skipping over all concealed lines in one fell swoop:
3203 (prog1 (condition-case nil (or (line-move step) t)
3204 (error nil))
82b392a4
KM
3205 (allout-beginning-of-current-line)
3206 ;; line-move can wind up on the same line if long.
3207 ;; when moving forward, that would yield no-progress
3208 (when (and (not backward)
3209 (<= (point) progress))
3210 ;; ensure progress by doing line-move from end-of-line:
3211 (end-of-line)
3212 (condition-case nil (or (line-move step) t)
3213 (error nil))
3214 (allout-beginning-of-current-line)
3215 (setq progress (point))))
d8350998 3216 ;; Deal with apparent header line:
2a1408fd
DN
3217 (save-match-data
3218 (if (not (looking-at allout-regexp))
3219 ;; not a header line, keep looking:
d8350998 3220 t
2a1408fd
DN
3221 (allout-prefix-data)
3222 (if (and (allout-do-doublecheck)
3223 (allout-aberrant-container-p))
3224 ;; skip this aberrant prospective header line:
3225 t
8dd59f01 3226 ;; this prospective headerline qualifies -- register:
2a1408fd
DN
3227 (setq got allout-recent-prefix-beginning)
3228 ;; and break the loop:
3229 nil)))))
c567ac01
RS
3230 ;; Register this got, it may be the last:
3231 (if got (setq prev got))
3232 (setq arg (1- arg)))
3233 (cond (got ; Last move was to a prefix:
d8350998 3234 (allout-end-of-prefix))
c567ac01 3235 (prev ; Last move wasn't, but prev was:
d8350998
CY
3236 (goto-char prev)
3237 (allout-end-of-prefix))
c567ac01 3238 ((not backward) (end-of-line) nil))))
fd5359c6
MR
3239;;;_ > allout-previous-visible-heading (arg)
3240(defun allout-previous-visible-heading (arg)
c567ac01 3241 "Move to the previous heading line.
1977b8f6 3242
c567ac01 3243With argument, repeats or can move forward if negative.
2265e017 3244A heading line is one that starts with a `*' (or that `allout-regexp'
c567ac01 3245matches)."
1977b8f6 3246 (interactive "p")
d8350998 3247 (prog1 (allout-next-visible-heading (- arg))
1c9b9df0 3248 (if (allout-called-interactively-p) (allout-end-of-prefix))))
fd5359c6 3249;;;_ > allout-forward-current-level (arg)
d82979ea 3250(defun allout-forward-current-level (arg)
19b84ba3 3251 "Position point at the next heading of the same level.
1977b8f6 3252
19b84ba3 3253Takes optional repeat-count, goes backward if count is negative.
1977b8f6 3254
19b84ba3 3255Returns resulting position, else nil if none found."
d82979ea 3256 (interactive "p")
fd5359c6 3257 (let ((start-depth (allout-current-depth))
19b84ba3 3258 (start-arg arg)
d8350998 3259 (backward (> 0 arg)))
19b84ba3
RS
3260 (if (= 0 start-depth)
3261 (error "No siblings, not in a topic..."))
3262 (if backward (setq arg (* -1 arg)))
d8350998
CY
3263 (allout-back-to-current-heading)
3264 (while (and (not (zerop arg))
3265 (if backward
3266 (allout-previous-sibling)
3267 (allout-next-sibling)))
3268 (setq arg (1- arg)))
1c9b9df0 3269 (if (not (allout-called-interactively-p))
d8350998
CY
3270 nil
3271 (allout-end-of-prefix)
3272 (if (not (zerop arg))
3273 (error "Hit %s level %d topic, traversed %d of %d requested"
3274 (if backward "first" "last")
3275 allout-recent-depth
3276 (- (abs start-arg) arg)
3277 (abs start-arg))))))
fd5359c6 3278;;;_ > allout-backward-current-level (arg)
d82979ea 3279(defun allout-backward-current-level (arg)
fd5359c6 3280 "Inverse of `allout-forward-current-level'."
d82979ea 3281 (interactive "p")
1c9b9df0 3282 (if (allout-called-interactively-p)
19b84ba3 3283 (let ((current-prefix-arg (* -1 arg)))
fd5359c6
MR
3284 (call-interactively 'allout-forward-current-level))
3285 (allout-forward-current-level (* -1 arg))))
c567ac01 3286
19b84ba3 3287;;;_ #5 Alteration
c567ac01
RS
3288
3289;;;_ - Fundamental
fd5359c6
MR
3290;;;_ = allout-post-goto-bullet
3291(defvar allout-post-goto-bullet nil
3292 "Outline internal var, for `allout-pre-command-business' hot-spot operation.
19b84ba3
RS
3293
3294When set, tells post-processing to reposition on topic bullet, and
2265e017 3295then unset it. Set by `allout-pre-command-business' when implementing
19b84ba3
RS
3296hot-spot operation, where literal characters typed over a topic bullet
3297are mapped to the command of the corresponding control-key on the
d3e51865 3298`allout-mode-map-value'.")
fd5359c6 3299(make-variable-buffer-local 'allout-post-goto-bullet)
48bd8440
EZ
3300;;;_ = allout-command-counter
3301(defvar allout-command-counter 0
3302 "Counter that monotonically increases in allout-mode buffers.
3303
3304Set by `allout-pre-command-business', to support allout addons in
3305coordinating with allout activity.")
3306(make-variable-buffer-local 'allout-command-counter)
3f2b07f8
KM
3307;;;_ = allout-this-command-hid-text
3308(defvar allout-this-command-hid-text nil
3309 "True if the most recent allout-mode command hid any text.")
3310(make-variable-buffer-local 'allout-this-command-hid-text)
fd5359c6
MR
3311;;;_ > allout-post-command-business ()
3312(defun allout-post-command-business ()
2265e017 3313 "Outline `post-command-hook' function.
c567ac01 3314
2265e017 3315- Implement (and clear) `allout-post-goto-bullet', for hot-spot
c567ac01
RS
3316 outline commands.
3317
3f2b07f8
KM
3318- Move the cursor to the beginning of the entry if it is hidden
3319 and collapsing activity just happened.
3320
13aa217b
KM
3321- If the command we're following was an undo, check for change in
3322 the status of encrypted items and adjust auto-save inhibitions
3323 accordingly.
3324
96b83743 3325- Decrypt topic currently being edited if it was encrypted for a save."
c567ac01 3326
fd5359c6 3327 (if (not (allout-mode-p)) ; In allout-mode.
19b84ba3 3328 nil
d82979ea 3329
13aa217b
KM
3330 (when allout-just-did-undo
3331 (setq allout-just-did-undo nil)
919d884a 3332 (run-hooks 'allout-post-undo-hook)
13aa217b
KM
3333 (cond ((and (= buffer-saved-size -1)
3334 allout-auto-save-temporarily-disabled)
3335 ;; user possibly undid a decryption, deinhibit auto-save:
3336 (allout-maybe-resume-auto-save-info-after-encryption))
3337 ((save-excursion
3338 (save-restriction
3339 (widen)
3340 (goto-char (point-min))
3341 (not (allout-next-topic-pending-encryption))))
3342 ;; plain-text encrypted items are present, inhibit auto-save:
3343 (allout-inhibit-auto-save-info-for-decryption (buffer-size)))))
3344
d82979ea
EZ
3345 (if (and (boundp 'allout-after-save-decrypt)
3346 allout-after-save-decrypt)
3347 (allout-after-saves-handler))
3348
48bd8440 3349 ;; Implement allout-post-goto-bullet, if set:
fd5359c6
MR
3350 (if (and allout-post-goto-bullet
3351 (allout-current-bullet-pos))
3352 (progn (goto-char (allout-current-bullet-pos))
3f2b07f8
KM
3353 (setq allout-post-goto-bullet nil))
3354 (when (and (allout-hidden-p) allout-this-command-hid-text)
3355 (allout-beginning-of-current-entry)))))
fd5359c6
MR
3356;;;_ > allout-pre-command-business ()
3357(defun allout-pre-command-business ()
2265e017 3358 "Outline `pre-command-hook' function for outline buffers.
48bd8440
EZ
3359
3360Among other things, implements special behavior when the cursor is on the
3361topic bullet character.
c567ac01 3362
d3e51865
KM
3363When the cursor is on the bullet character, self-insert
3364characters are reinterpreted as the corresponding
3365control-character in the `allout-mode-map-value'. The
3366`allout-mode' `post-command-hook' insures that the cursor which
3367has moved as a result of such reinterpretation is positioned on
3368the bullet character of the destination topic.
c567ac01 3369
c07583cd 3370The upshot is that you can get easy, single (ie, unmodified) key
9179616f 3371outline maneuvering operations by positioning the cursor on the bullet
58edceeb
JB
3372char. When in this mode you can use regular cursor-positioning
3373command/keystrokes to relocate the cursor off of a bullet character to
3374return to regular interpretation of self-insert characters."
d82979ea 3375
fd5359c6 3376 (if (not (allout-mode-p))
9179616f 3377 nil
48bd8440 3378 (setq allout-command-counter (1+ allout-command-counter))
3f2b07f8 3379 (setq allout-this-command-hid-text nil)
48bd8440 3380 ;; Do hot-spot navigation.
9179616f 3381 (if (and (eq this-command 'self-insert-command)
fd5359c6 3382 (eq (point)(allout-current-bullet-pos)))
01fc9422
EZ
3383 (allout-hotspot-key-handler))))
3384;;;_ > allout-hotspot-key-handler ()
3385(defun allout-hotspot-key-handler ()
3386 "Catchall handling of key bindings in hot-spots.
3387
3388Translates unmodified keystrokes to corresponding allout commands, when
3389they would qualify if prefixed with the allout-command-prefix, and sets
3390this-command accordingly.
3391
3392Returns the qualifying command, if any, else nil."
3393 (interactive)
1c9b9df0 3394 (let* ((modified (event-modifiers last-command-event))
8989a920 3395 (key-num (cond ((numberp last-command-event) last-command-event)
01fc9422
EZ
3396 ;; for XEmacs character type:
3397 ((and (fboundp 'characterp)
8989a920
GM
3398 (apply 'characterp (list last-command-event)))
3399 (apply 'char-to-int (list last-command-event)))
01fc9422 3400 (t 0)))
d8350998 3401 mapped-binding)
01fc9422
EZ
3402
3403 (if (zerop key-num)
3404 nil
3405
d8350998
CY
3406 (if (and
3407 ;; exclude control chars and escape:
1c9b9df0 3408 (not modified)
d8350998
CY
3409 (<= 33 key-num)
3410 (setq mapped-binding
642f3c5c
KM
3411 (or
3412 ;; try control-modified versions of keys:
3413 (key-binding (vconcat allout-command-prefix
3414 (vector
3415 (if (and (<= 97 key-num) ; "a"
3416 (>= 122 key-num)) ; "z"
3417 (- key-num 96) key-num)))
3418 t)
3419 ;; try non-modified versions of keys:
3420 (key-binding (vconcat allout-command-prefix
3421 (vector key-num))
3422 t))))
60c7bbd2 3423 ;; Qualified as an allout command -- do hot-spot operation.
01fc9422 3424 (setq allout-post-goto-bullet t)
1c9b9df0
KM
3425 ;; accept-defaults nil, or else we get allout-item-icon-key-handler.
3426 (setq mapped-binding (key-binding (vector key-num))))
01fc9422
EZ
3427
3428 (while (keymapp mapped-binding)
3429 (setq mapped-binding
d8350998 3430 (lookup-key mapped-binding (vector (read-char)))))
01fc9422 3431
1c9b9df0
KM
3432 (when mapped-binding
3433 (setq this-command mapped-binding)))))
01fc9422 3434
fd5359c6
MR
3435;;;_ > allout-find-file-hook ()
3436(defun allout-find-file-hook ()
4034b0e2 3437 "Activate `allout-mode' on non-nil `allout-auto-activation', `allout-layout'.
fd5359c6 3438
11c46e15 3439See `allout-auto-activation' for setup instructions."
fd5359c6
MR
3440 (if (and allout-auto-activation
3441 (not (allout-mode-p))
3442 allout-layout)
2576c28f 3443 (allout-mode)))
c567ac01
RS
3444
3445;;;_ - Topic Format Assessment
fd5359c6
MR
3446;;;_ > allout-solicit-alternate-bullet (depth &optional current-bullet)
3447(defun allout-solicit-alternate-bullet (depth &optional current-bullet)
1977b8f6 3448
19b84ba3
RS
3449 "Prompt for and return a bullet char as an alternative to the current one.
3450
3451Offer one suitable for current depth DEPTH as default."
1977b8f6 3452
9179616f 3453 (let* ((default-bullet (or (and (stringp current-bullet) current-bullet)
fd5359c6
MR
3454 (allout-bullet-for-depth depth)))
3455 (sans-escapes (regexp-sans-escapes allout-bullets-string))
9179616f
DL
3456 choice)
3457 (save-excursion
fd5359c6 3458 (goto-char (allout-current-bullet-pos))
9179616f
DL
3459 (setq choice (solicit-char-in-string
3460 (format "Select bullet: %s ('%s' default): "
3461 sans-escapes
1c9b9df0 3462 (allout-substring-no-properties default-bullet))
9179616f
DL
3463 sans-escapes
3464 t)))
3465 (message "")
1977b8f6
RS
3466 (if (string= choice "") default-bullet choice))
3467 )
fd5359c6
MR
3468;;;_ > allout-distinctive-bullet (bullet)
3469(defun allout-distinctive-bullet (bullet)
e126900f 3470 "True if BULLET is one of those on `allout-distinctive-bullets-string'."
fd5359c6
MR
3471 (string-match (regexp-quote bullet) allout-distinctive-bullets-string))
3472;;;_ > allout-numbered-type-prefix (&optional prefix)
3473(defun allout-numbered-type-prefix (&optional prefix)
c567ac01 3474 "True if current header prefix bullet is numbered bullet."
fd5359c6
MR
3475 (and allout-numbered-bullet
3476 (string= allout-numbered-bullet
1977b8f6 3477 (if prefix
fd5359c6
MR
3478 (allout-get-prefix-bullet prefix)
3479 (allout-get-bullet)))))
d82979ea
EZ
3480;;;_ > allout-encrypted-type-prefix (&optional prefix)
3481(defun allout-encrypted-type-prefix (&optional prefix)
c07583cd 3482 "True if current header prefix bullet is for an encrypted entry (body)."
d82979ea
EZ
3483 (and allout-topic-encryption-bullet
3484 (string= allout-topic-encryption-bullet
3485 (if prefix
3486 (allout-get-prefix-bullet prefix)
3487 (allout-get-bullet)))))
fd5359c6
MR
3488;;;_ > allout-bullet-for-depth (&optional depth)
3489(defun allout-bullet-for-depth (&optional depth)
19b84ba3 3490 "Return outline topic bullet suited to optional DEPTH, or current depth."
1977b8f6 3491 ;; Find bullet in plain-bullets-string modulo DEPTH.
fd5359c6
MR
3492 (if allout-stylish-prefixes
3493 (char-to-string (aref allout-plain-bullets-string
1977b8f6 3494 (% (max 0 (- depth 2))
fd5359c6
MR
3495 allout-plain-bullets-string-len)))
3496 allout-primary-bullet)
1977b8f6
RS
3497 )
3498
c567ac01 3499;;;_ - Topic Production
fd5359c6
MR
3500;;;_ > allout-make-topic-prefix (&optional prior-bullet
3501(defun allout-make-topic-prefix (&optional prior-bullet
1977b8f6
RS
3502 new
3503 depth
b6a5875b 3504 instead
1977b8f6
RS
3505 number-control
3506 index)
3507 ;; Depth null means use current depth, non-null means we're either
3508 ;; opening a new topic after current topic, lower or higher, or we're
3509 ;; changing level of current topic.
b6a5875b 3510 ;; Instead dominates specified bullet-char.
c567ac01 3511;;;_ . Doc string:
19b84ba3 3512 "Generate a topic prefix suitable for optional arg DEPTH, or current depth.
c567ac01
RS
3513
3514All the arguments are optional.
3515
3516PRIOR-BULLET indicates the bullet of the prefix being changed, or
3517nil if none. This bullet may be preserved (other options
2265e017 3518notwithstanding) if it is on the `allout-distinctive-bullets-string',
c567ac01
RS
3519for instance.
3520
3521Second arg NEW indicates that a new topic is being opened after the
3522topic at point, if non-nil. Default bullet for new topics, eg, may
3523be set (contingent to other args) to numbered bullets if previous
3524sibling is one. The implication otherwise is that the current topic
60c7bbd2 3525is being adjusted -- shifted or rebulleted -- and we don't consider
c567ac01
RS
3526bullet or previous sibling.
3527
3528Third arg DEPTH forces the topic prefix to that depth, regardless of
3529the current topics' depth.
3530
b6a5875b
KM
3531If INSTEAD is:
3532
3533- nil, then the bullet char for the context is used, per distinction or depth
2dab465b
KM
3534- a \(numeric) character, then character's string representation is used
3535- a string, then the user is asked for bullet with the first char as default
b6a5875b
KM
3536- anything else, the user is solicited with bullet char per context as default
3537
3538\(INSTEAD overrides other options, including, eg, a distinctive
3539PRIOR-BULLET.)
c567ac01 3540
fd5359c6 3541Fifth arg, NUMBER-CONTROL, matters only if `allout-numbered-bullet'
b6a5875b 3542is non-nil *and* no specific INSTEAD was specified. Then
c567ac01
RS
3543NUMBER-CONTROL non-nil forces prefix to either numbered or
3544denumbered format, depending on the value of the sixth arg, INDEX.
3545
3546\(Note that NUMBER-CONTROL does *not* apply to level 1 topics. Sorry...)
3547
3548If NUMBER-CONTROL is non-nil and sixth arg INDEX is non-nil then
3549the prefix of the topic is forced to be numbered. Non-nil
3550NUMBER-CONTROL and nil INDEX forces non-numbered format on the
3551bullet. Non-nil NUMBER-CONTROL and non-nil, non-number INDEX means
3552that the index for the numbered prefix will be derived, by counting
3553siblings back to start of level. If INDEX is a number, then that
3554number is used as the index for the numbered prefix (allowing, eg,
a0776d6b 3555sequential renumbering to not require this function counting back the
c567ac01
RS
3556index for each successive sibling)."
3557;;;_ . Code:
1977b8f6
RS
3558 ;; The options are ordered in likely frequence of use, most common
3559 ;; highest, least lowest. Ie, more likely to be doing prefix
3560 ;; adjustments than soliciting, and yet more than numbering.
3561 ;; Current prefix is least dominant, but most likely to be commonly
3562 ;; specified...
3563
3564 (let* (body
3565 numbering
3566 denumbering
fd5359c6
MR
3567 (depth (or depth (allout-depth)))
3568 (header-lead allout-header-prefix)
1977b8f6
RS
3569 (bullet-char
3570
3571 ;; Getting value for bullet char is practically the whole job:
3572
3573 (cond
60c7bbd2 3574 ; Simplest situation -- level 1:
fd5359c6 3575 ((<= depth 1) (setq header-lead "") allout-primary-bullet)
1977b8f6 3576 ; Simple, too: all asterisks:
fd5359c6 3577 (allout-old-style-prefixes
60c7bbd2 3578 ;; Cheat -- make body the whole thing, null out header-lead and
1977b8f6
RS
3579 ;; bullet-char:
3580 (setq body (make-string depth
fd5359c6 3581 (string-to-char allout-primary-bullet)))
1977b8f6
RS
3582 (setq header-lead "")
3583 "")
3584
3585 ;; (Neither level 1 nor old-style, so we're space padding.
3586 ;; Sneak it in the condition of the next case, whatever it is.)
3587
3588 ;; Solicitation overrides numbering and other cases:
3589 ((progn (setq body (make-string (- depth 2) ?\ ))
3590 ;; The actual condition:
b6a5875b 3591 instead)
2dab465b
KM
3592 (let ((got (cond ((stringp instead)
3593 (if (> (length instead) 0)
3594 (allout-solicit-alternate-bullet
3595 depth (substring instead 0 1))))
3596 ((characterp instead) (char-to-string instead))
3597 (t (allout-solicit-alternate-bullet depth)))))
1977b8f6 3598 ;; Gotta check whether we're numbering and got a numbered bullet:
fd5359c6 3599 (setq numbering (and allout-numbered-bullet
1977b8f6 3600 (not (and number-control (not index)))
fd5359c6 3601 (string= got allout-numbered-bullet)))
1977b8f6
RS
3602 ;; Now return what we got, regardless:
3603 got))
3604
3605 ;; Numbering invoked through args:
fd5359c6 3606 ((and allout-numbered-bullet number-control)
1977b8f6 3607 (if (setq numbering (not (setq denumbering (not index))))
fd5359c6 3608 allout-numbered-bullet
19b84ba3 3609 (if (and prior-bullet
fd5359c6 3610 (not (string= allout-numbered-bullet
19b84ba3
RS
3611 prior-bullet)))
3612 prior-bullet
fd5359c6 3613 (allout-bullet-for-depth depth))))
1977b8f6
RS
3614
3615 ;;; Neither soliciting nor controlled numbering ;;;
3616 ;;; (may be controlled denumbering, tho) ;;;
3617
3618 ;; Check wrt previous sibling:
3619 ((and new ; only check for new prefixes
fd5359c6
MR
3620 (<= depth (allout-depth))
3621 allout-numbered-bullet ; ... & numbering enabled
1977b8f6
RS
3622 (not denumbering)
3623 (let ((sibling-bullet
3624 (save-excursion
3625 ;; Locate correct sibling:
fd5359c6
MR
3626 (or (>= depth (allout-depth))
3627 (allout-ascend-to-depth depth))
3628 (allout-get-bullet))))
1977b8f6 3629 (if (and sibling-bullet
fd5359c6 3630 (string= allout-numbered-bullet sibling-bullet))
1977b8f6
RS
3631 (setq numbering sibling-bullet)))))
3632
3633 ;; Distinctive prior bullet?
3634 ((and prior-bullet
fd5359c6 3635 (allout-distinctive-bullet prior-bullet)
1977b8f6 3636 ;; Either non-numbered:
fd5359c6
MR
3637 (or (not (and allout-numbered-bullet
3638 (string= prior-bullet allout-numbered-bullet)))
1977b8f6
RS
3639 ;; or numbered, and not denumbering:
3640 (setq numbering (not denumbering)))
3641 ;; Here 'tis:
3642 prior-bullet))
3643
3644 ;; Else, standard bullet per depth:
fd5359c6 3645 ((allout-bullet-for-depth depth)))))
1977b8f6
RS
3646
3647 (concat header-lead
3648 body
3649 bullet-char
3650 (if numbering
3651 (format "%d" (cond ((and index (numberp index)) index)
fd5359c6
MR
3652 (new (1+ (allout-sibling-index depth)))
3653 ((allout-sibling-index))))))
1977b8f6
RS
3654 )
3655 )
96b83743
EZ
3656;;;_ > allout-open-topic (relative-depth &optional before offer-recent-bullet)
3657(defun allout-open-topic (relative-depth &optional before offer-recent-bullet)
d82979ea 3658 "Open a new topic at depth DEPTH.
19b84ba3
RS
3659
3660New topic is situated after current one, unless optional flag BEFORE
60c7bbd2
RS
3661is non-nil, or unless current line is completely empty -- lacking even
3662whitespace -- in which case open is done on the current line.
96b83743
EZ
3663
3664When adding an offspring, it will be added immediately after the parent if
3665the other offspring are exposed, or after the last child if the offspring
c07583cd 3666are hidden. (The intervening offspring will be exposed in the latter
96b83743 3667case.)
c567ac01 3668
96b83743 3669If OFFER-RECENT-BULLET is true, offer to use the bullet of the prior sibling.
9179616f 3670
c567ac01
RS
3671Nuances:
3672
3673- Creation of new topics is with respect to the visible topic
3674 containing the cursor, regardless of intervening concealed ones.
3675
3676- New headers are generally created after/before the body of a
3677 topic. However, they are created right at cursor location if the
3678 cursor is on a blank line, even if that breaks the current topic
3679 body. This is intentional, to provide a simple means for
3680 deliberately dividing topic bodies.
3681
3682- Double spacing of topic lists is preserved. Also, the first
3683 level two topic is created double-spaced (and so would be
3684 subsequent siblings, if that's left intact). Otherwise,
3685 single-spacing is used.
3686
3687- Creation of sibling or nested topics is with respect to the topic
3688 you're starting from, even when creating backwards. This way you
3689 can easily create a sibling in front of the current topic without
a0776d6b 3690 having to go to its preceding sibling, and then open forward
c567ac01 3691 from there."
1977b8f6 3692
96b83743 3693 (allout-beginning-of-current-line)
2a1408fd
DN
3694 (save-match-data
3695 (let* ((inhibit-field-text-motion t)
3696 (depth (+ (allout-current-depth) relative-depth))
3697 (opening-on-blank (if (looking-at "^\$")
3698 (not (setq before nil))))
3699 ;; bunch o vars set while computing ref-topic
3700 opening-numbered
3701 ref-depth
3702 ref-bullet
3703 (ref-topic (save-excursion
3704 (cond ((< relative-depth 0)
3705 (allout-ascend-to-depth depth))
3706 ((>= relative-depth 1) nil)
3707 (t (allout-back-to-current-heading)))
3708 (setq ref-depth allout-recent-depth)
3709 (setq ref-bullet
3710 (if (> allout-recent-prefix-end 1)
3711 (allout-recent-bullet)
3712 ""))
3713 (setq opening-numbered
3714 (save-excursion
3715 (and allout-numbered-bullet
3716 (or (<= relative-depth 0)
3717 (allout-descend-to-depth depth))
3718 (if (allout-numbered-type-prefix)
3719 allout-numbered-bullet))))
3720 (point)))
3721 dbl-space
3722 doing-beginning
3723 start end)
3724
3725 (if (not opening-on-blank)
1977b8f6 3726 ; Positioning and vertical
60c7bbd2 3727 ; padding -- only if not
1977b8f6 3728 ; opening-on-blank:
2a1408fd
DN
3729 (progn
3730 (goto-char ref-topic)
3731 (setq dbl-space ; Determine double space action:
3732 (or (and (<= relative-depth 0) ; not descending;
3733 (save-excursion
3734 ;; at b-o-b or preceded by a blank line?
3735 (or (> 0 (forward-line -1))
3736 (looking-at "^\\s-*$")
3737 (bobp)))
3738 (save-excursion
3739 ;; succeeded by a blank line?
3740 (allout-end-of-current-subtree)
3741 (looking-at "\n\n")))
3742 (and (= ref-depth 1)
3743 (or before
3744 (= depth 1)
3745 (save-excursion
3746 ;; Don't already have following
3747 ;; vertical padding:
3748 (not (allout-pre-next-prefix)))))))
3749
3750 ;; Position to prior heading, if inserting backwards, and not
3751 ;; going outwards:
3752 (if (and before (>= relative-depth 0))
3753 (progn (allout-back-to-current-heading)
3754 (setq doing-beginning (bobp))
3755 (if (not (bobp))
3756 (allout-previous-heading)))
3757 (if (and before (bobp))
3758 (open-line 1)))
3759
3760 (if (<= relative-depth 0)
3761 ;; Not going inwards, don't snug up:
3762 (if doing-beginning
3763 (if (not dbl-space)
3764 (open-line 1)
3765 (open-line 2))
3766 (if before
3767 (progn (end-of-line)
3768 (allout-pre-next-prefix)
3769 (while (and (= ?\n (following-char))
3770 (save-excursion
3771 (forward-char 1)
3772 (allout-hidden-p)))
3773 (forward-char 1))
3774 (if (not (looking-at "^$"))
3775 (open-line 1)))
3776 (allout-end-of-current-subtree)
3777 (if (looking-at "\n\n") (forward-char 1))))
8dd59f01 3778 ;; Going inwards -- double-space if first offspring is
2a1408fd
DN
3779 ;; double-spaced, otherwise snug up.
3780 (allout-end-of-entry)
3781 (if (eobp)
3782 (newline 1)
3783 (line-move 1))
3784 (allout-beginning-of-current-line)
3785 (backward-char 1)
3786 (if (bolp)
3787 ;; Blank lines between current header body and next
8dd59f01 3788 ;; header -- get to last substantive (non-white-space)
2a1408fd
DN
3789 ;; line in body:
3790 (progn (setq dbl-space t)
3791 (re-search-backward "[^ \t\n]" nil t)))
3792 (if (looking-at "\n\n")
3793 (setq dbl-space t))
3794 (if (save-excursion
3795 (allout-next-heading)
3796 (when (> allout-recent-depth ref-depth)
3797 ;; This is an offspring.
3798 (forward-line -1)
3799 (looking-at "^\\s-*$")))
3800 (progn (forward-line 1)
96b83743 3801 (open-line 1)
2a1408fd
DN
3802 (forward-line 1)))
3803 (allout-end-of-current-line))
3804
3805 ;;(if doing-beginning (goto-char doing-beginning))
3806 (if (not (bobp))
3807 ;; We insert a newline char rather than using open-line to
3808 ;; avoid rear-stickiness inheritence of read-only property.
3809 (progn (if (and (not (> depth ref-depth))
3810 (not before))
3811 (open-line 1)
3812 (if (and (not dbl-space) (> depth ref-depth))
3813 (newline 1)
3814 (if dbl-space
3815 (open-line 1)
3816 (if (not before)
3817 (newline 1)))))
3818 (if (and dbl-space (not (> relative-depth 0)))
3819 (newline 1))
3820 (if (and (not (eobp))
3821 (or (not (bolp))
3822 (and (not (bobp))
3823 ;; bolp doesnt detect concealed
3824 ;; trailing newlines, compensate:
3825 (save-excursion
3826 (forward-char -1)
3827 (allout-hidden-p)))))
3828 (forward-char 1))))
3829 ))
3830 (setq start (point))
3831 (insert (concat (allout-make-topic-prefix opening-numbered t depth)
3832 " "))
3833 (setq end (1+ (point)))
3834
3835 (allout-rebullet-heading (and offer-recent-bullet ref-bullet)
3836 depth nil nil t)
3837 (if (> relative-depth 0)
3838 (save-excursion (goto-char ref-topic)
3839 (allout-show-children)))
3840 (end-of-line)
48bd8440 3841
2a1408fd
DN
3842 (run-hook-with-args 'allout-structure-added-hook start end)
3843 )
1977b8f6
RS
3844 )
3845 )
fd5359c6
MR
3846;;;_ > allout-open-subtopic (arg)
3847(defun allout-open-subtopic (arg)
c567ac01
RS
3848 "Open new topic header at deeper level than the current one.
3849
3850Negative universal arg means to open deeper, but place the new topic
3851prior to the current one."
1977b8f6 3852 (interactive "p")
d82979ea 3853 (allout-open-topic 1 (> 0 arg) (< 1 arg)))
fd5359c6
MR
3854;;;_ > allout-open-sibtopic (arg)
3855(defun allout-open-sibtopic (arg)
19b84ba3
RS
3856 "Open new topic header at same level as the current one.
3857
9179616f
DL
3858Positive universal arg means to use the bullet of the prior sibling.
3859
19b84ba3 3860Negative universal arg means to place the new topic prior to the current
c567ac01 3861one."
1977b8f6 3862 (interactive "p")
d82979ea 3863 (allout-open-topic 0 (> 0 arg) (not (= 1 arg))))
fd5359c6
MR
3864;;;_ > allout-open-supertopic (arg)
3865(defun allout-open-supertopic (arg)
c567ac01 3866 "Open new topic header at shallower level than the current one.
19b84ba3 3867
c567ac01
RS
3868Negative universal arg means to open shallower, but place the new
3869topic prior to the current one."
1977b8f6
RS
3870
3871 (interactive "p")
d82979ea 3872 (allout-open-topic -1 (> 0 arg) (< 1 arg)))
c567ac01
RS
3873
3874;;;_ - Outline Alteration
3875;;;_ : Topic Modification
fd5359c6
MR
3876;;;_ = allout-former-auto-filler
3877(defvar allout-former-auto-filler nil
2265e017 3878 "Name of modal fill function being wrapped by `allout-auto-fill'.")
fd5359c6
MR
3879;;;_ > allout-auto-fill ()
3880(defun allout-auto-fill ()
aad94676 3881 "`allout-mode' autofill function.
19b84ba3
RS
3882
3883Maintains outline hanging topic indentation if
fd5359c6 3884`allout-use-hanging-indents' is set."
01fc9422 3885
381987c3
KM
3886 (when (and (not allout-inhibit-auto-fill)
3887 (or (not allout-inhibit-auto-fill-on-headline)
3888 (not (allout-on-current-heading-p))))
01fc9422
EZ
3889 (let ((fill-prefix (if allout-use-hanging-indents
3890 ;; Check for topic header indentation:
2a1408fd
DN
3891 (save-match-data
3892 (save-excursion
3893 (beginning-of-line)
3894 (if (looking-at allout-regexp)
3895 ;; ... construct indentation to account for
3896 ;; length of topic prefix:
3897 (make-string (progn (allout-end-of-prefix)
3898 (current-column))
3899 ?\ ))))))
7927f48d
KM
3900 (use-auto-fill-function
3901 (if (and (eq allout-outside-normal-auto-fill-function
3902 'allout-auto-fill)
3903 (eq auto-fill-function 'allout-auto-fill))
3904 'do-auto-fill
3905 (or allout-outside-normal-auto-fill-function
3906 auto-fill-function))))
01fc9422
EZ
3907 (if (or allout-former-auto-filler allout-use-hanging-indents)
3908 (funcall use-auto-fill-function)))))
fd5359c6
MR
3909;;;_ > allout-reindent-body (old-depth new-depth &optional number)
3910(defun allout-reindent-body (old-depth new-depth &optional number)
e126900f 3911 "Reindent body lines which were indented at OLD-DEPTH to NEW-DEPTH.
c567ac01
RS
3912
3913Optional arg NUMBER indicates numbering is being added, and it must
a0776d6b 3914be accommodated.
c567ac01
RS
3915
3916Note that refill of indented paragraphs is not done."
1977b8f6
RS
3917
3918 (save-excursion
fd5359c6 3919 (allout-end-of-prefix)
c567ac01
RS
3920 (let* ((new-margin (current-column))
3921 excess old-indent-begin old-indent-end
c567ac01
RS
3922 ;; We want the column where the header-prefix text started
3923 ;; *before* the prefix was changed, so we infer it relative
3924 ;; to the new margin and the shift in depth:
3925 (old-margin (+ old-depth (- new-margin new-depth))))
353e2ef2 3926
c567ac01 3927 ;; Process lines up to (but excluding) next topic header:
fd5359c6 3928 (allout-unprotected
c567ac01
RS
3929 (save-match-data
3930 (while
96b83743 3931 (and (re-search-forward "\n\\(\\s-*\\)"
c567ac01
RS
3932 nil
3933 t)
3934 ;; Register the indent data, before we reset the
353e2ef2 3935 ;; match data with a subsequent `looking-at':
c567ac01
RS
3936 (setq old-indent-begin (match-beginning 1)
3937 old-indent-end (match-end 1))
fd5359c6 3938 (not (looking-at allout-regexp)))
d82979ea
EZ
3939 (if (> 0 (setq excess (- (- old-indent-end old-indent-begin)
3940 old-margin)))
60c7bbd2 3941 ;; Text starts left of old margin -- don't adjust:
c567ac01 3942 nil
ea18b2b1 3943 ;; Text was hanging at or right of old left margin --
c567ac01
RS
3944 ;; reindent it, preserving its existing indentation
3945 ;; beyond the old margin:
3946 (delete-region old-indent-begin old-indent-end)
d82979ea 3947 (indent-to (+ new-margin excess (current-column))))))))))
fd5359c6
MR
3948;;;_ > allout-rebullet-current-heading (arg)
3949(defun allout-rebullet-current-heading (arg)
9179616f
DL
3950 "Solicit new bullet for current visible heading."
3951 (interactive "p")
3952 (let ((initial-col (current-column))
fd5359c6 3953 (on-bullet (eq (point)(allout-current-bullet-pos)))
d8350998 3954 from to
9179616f
DL
3955 (backwards (if (< arg 0)
3956 (setq arg (* arg -1)))))
3957 (while (> arg 0)
fd5359c6
MR
3958 (save-excursion (allout-back-to-current-heading)
3959 (allout-end-of-prefix)
d8350998
CY
3960 (setq from allout-recent-prefix-beginning
3961 to allout-recent-prefix-end)
b6a5875b 3962 (allout-rebullet-heading t ;;; instead
9179616f
DL
3963 nil ;;; depth
3964 nil ;;; number-control
3965 nil ;;; index
d8350998
CY
3966 t) ;;; do-successors
3967 (run-hook-with-args 'allout-exposure-change-hook
3968 from to t))
9179616f
DL
3969 (setq arg (1- arg))
3970 (if (<= arg 0)
3971 nil
3972 (setq initial-col nil) ; Override positioning back to init col
3973 (if (not backwards)
fd5359c6 3974 (allout-next-visible-heading 1)
d8350998 3975 (allout-goto-prefix-doublechecked)
fd5359c6 3976 (allout-next-visible-heading -1))))
9179616f 3977 (message "Done.")
fd5359c6 3978 (cond (on-bullet (goto-char (allout-current-bullet-pos)))
9179616f 3979 (initial-col (move-to-column initial-col)))))
b6a5875b
KM
3980;;;_ > allout-rebullet-heading (&optional instead ...)
3981(defun allout-rebullet-heading (&optional instead
1977b8f6
RS
3982 new-depth
3983 number-control
3984 index
3985 do-successors)
3986
c567ac01 3987 "Adjust bullet of current topic prefix.
1977b8f6 3988
d82979ea
EZ
3989All args are optional.
3990
b6a5875b
KM
3991If INSTEAD is:
3992- nil, then the bullet char for the context is used, per distinction or depth
2dab465b
KM
3993- a \(numeric) character, then character's string representation is used
3994- a string, then the user is asked for bullet with the first char as default
b6a5875b 3995- anything else, the user is solicited with bullet char per context as default
1977b8f6 3996
d82979ea 3997Second arg DEPTH forces the topic prefix to that depth, regardless
9179616f 3998of the topic's current depth.
1977b8f6 3999
c567ac01 4000Third arg NUMBER-CONTROL can force the prefix to or away from
fd5359c6 4001numbered form. It has effect only if `allout-numbered-bullet' is
c567ac01
RS
4002non-nil and soliciting was not explicitly invoked (via first arg).
4003Its effect, numbering or denumbering, then depends on the setting
75bcb912 4004of the fourth arg, INDEX.
1977b8f6 4005
75bcb912 4006If NUMBER-CONTROL is non-nil and fourth arg INDEX is nil, then the
c567ac01
RS
4007prefix of the topic is forced to be non-numbered. Null index and
4008non-nil NUMBER-CONTROL forces denumbering. Non-nil INDEX (and
4009non-nil NUMBER-CONTROL) forces a numbered-prefix form. If non-nil
4010INDEX is a number, then that number is used for the numbered
4011prefix. Non-nil and non-number means that the index for the
d82979ea 4012numbered prefix will be derived by allout-make-topic-prefix.
1977b8f6 4013
c567ac01
RS
4014Fifth arg DO-SUCCESSORS t means re-resolve count on succeeding
4015siblings.
1977b8f6 4016
fd5359c6
MR
4017Cf vars `allout-stylish-prefixes', `allout-old-style-prefixes',
4018and `allout-numbered-bullet', which all affect the behavior of
c567ac01 4019this function."
1977b8f6 4020
fd5359c6 4021 (let* ((current-depth (allout-depth))
1977b8f6 4022 (new-depth (or new-depth current-depth))
fd5359c6
MR
4023 (mb allout-recent-prefix-beginning)
4024 (me allout-recent-prefix-end)
d8350998 4025 (current-bullet (buffer-substring-no-properties (- me 1) me))
ede4ac6a 4026 (has-annotation (get-text-property mb 'allout-was-hidden))
fd5359c6 4027 (new-prefix (allout-make-topic-prefix current-bullet
1977b8f6
RS
4028 nil
4029 new-depth
b6a5875b 4030 instead
1977b8f6
RS
4031 number-control
4032 index)))
4033
c567ac01 4034 ;; Is new one is identical to old?
1977b8f6
RS
4035 (if (and (= current-depth new-depth)
4036 (string= current-bullet
4037 (substring new-prefix (1- (length new-prefix)))))
c567ac01 4038 ;; Nothing to do:
1977b8f6
RS
4039 t
4040
4041 ;; New prefix probably different from old:
c567ac01 4042 ; get rid of old one:
fd5359c6 4043 (allout-unprotected (delete-region mb me))
1977b8f6 4044 (goto-char mb)
c567ac01
RS
4045 ; Dispense with number if
4046 ; numbered-bullet prefix:
2a1408fd
DN
4047 (save-match-data
4048 (if (and allout-numbered-bullet
4049 (string= allout-numbered-bullet current-bullet)
4050 (looking-at "[0-9]+"))
4051 (allout-unprotected
4052 (delete-region (match-beginning 0)(match-end 0)))))
1977b8f6 4053
ede4ac6a
KS
4054 ;; convey 'allout-was-hidden annotation, if original had it:
4055 (if has-annotation
4056 (put-text-property 0 (length new-prefix) 'allout-was-hidden t
4057 new-prefix))
4058
c567ac01 4059 ; Put in new prefix:
fd5359c6 4060 (allout-unprotected (insert new-prefix))
1977b8f6 4061
d82979ea 4062 ;; Reindent the body if elected, margin changed, and not encrypted body:
fd5359c6 4063 (if (and allout-reindent-bodies
d82979ea
EZ
4064 (not (= new-depth current-depth))
4065 (not (allout-encrypted-topic-p)))
fd5359c6 4066 (allout-reindent-body current-depth new-depth))
1977b8f6 4067
e47ca23b
KM
4068 (run-hook-with-args 'allout-exposure-change-hook mb me nil)
4069
c567ac01
RS
4070 ;; Recursively rectify successive siblings of orig topic if
4071 ;; caller elected for it:
4072 (if do-successors
4073 (save-excursion
fd5359c6 4074 (while (allout-next-sibling new-depth nil)
c567ac01
RS
4075 (setq index
4076 (cond ((numberp index) (1+ index))
fd5359c6
MR
4077 ((not number-control) (allout-sibling-index))))
4078 (if (allout-numbered-type-prefix)
b6a5875b 4079 (allout-rebullet-heading nil ;;; instead
c567ac01
RS
4080 new-depth ;;; new-depth
4081 number-control;;; number-control
4082 index ;;; index
4083 nil))))) ;;;(dont!)do-successors
4084 ) ; (if (and (= current-depth new-depth)...))
fd5359c6 4085 ) ; let* ((current-depth (allout-depth))...)
c567ac01 4086 ) ; defun
fd5359c6 4087;;;_ > allout-rebullet-topic (arg)
d8350998 4088(defun allout-rebullet-topic (arg &optional sans-offspring)
d82979ea 4089 "Rebullet the visible topic containing point and all contained subtopics.
19b84ba3 4090
c567ac01 4091Descends into invisible as well as visible topics, however.
1977b8f6 4092
75bcb912 4093When optional SANS-OFFSPRING is non-nil, subtopics are not
c07583cd 4094shifted. (Shifting a topic outwards without shifting its
d8350998
CY
4095offspring is disallowed, since this would create a \"containment
4096discontinuity\", where the depth difference between a topic and
4097its immediate offspring is greater than one.)
4098
c567ac01 4099With repeat count, shift topic depth by that amount."
1977b8f6 4100 (interactive "P")
96b83743 4101 (let ((start-col (current-column)))
1977b8f6
RS
4102 (save-excursion
4103 ;; Normalize arg:
4104 (cond ((null arg) (setq arg 0))
4105 ((listp arg) (setq arg (car arg))))
4106 ;; Fill the user in, in case we're shifting a big topic:
4107 (if (not (zerop arg)) (message "Shifting..."))
fd5359c6 4108 (allout-back-to-current-heading)
d8350998 4109 (if (<= (+ allout-recent-depth arg) 0)
1977b8f6 4110 (error "Attempt to shift topic below level 1"))
d8350998 4111 (allout-rebullet-topic-grunt arg nil nil nil nil sans-offspring)
1977b8f6 4112 (if (not (zerop arg)) (message "Shifting... done.")))
c567ac01 4113 (move-to-column (max 0 (+ start-col arg)))))
d8350998 4114;;;_ > allout-rebullet-topic-grunt (&optional relative-depth ...)
fd5359c6 4115(defun allout-rebullet-topic-grunt (&optional relative-depth
1977b8f6
RS
4116 starting-depth
4117 starting-point
4118 index
d8350998
CY
4119 do-successors
4120 sans-offspring)
d82979ea
EZ
4121 "Like `allout-rebullet-topic', but on nearest containing topic
4122\(visible or not).
1977b8f6 4123
d82979ea 4124See `allout-rebullet-heading' for rebulleting behavior.
1977b8f6 4125
d82979ea 4126All arguments are optional.
1977b8f6 4127
d82979ea
EZ
4128First arg RELATIVE-DEPTH means to shift the depth of the entire
4129topic that amount.
e126900f 4130
d8350998
CY
4131Several subsequent args are for internal recursive use by the function
4132itself: STARTING-DEPTH, STARTING-POINT, and INDEX.
4133
4134Finally, if optional SANS-OFFSPRING is non-nil then the offspring
c07583cd 4135are not shifted. (Shifting a topic outwards without shifting
d8350998
CY
4136its offspring is disallowed, since this would create a
4137\"containment discontinuity\", where the depth difference between
75bcb912 4138a topic and its immediate offspring is greater than one.)"
d8350998
CY
4139
4140 ;; XXX the recursion here is peculiar, and in general the routine may
4141 ;; need simplification with refactoring.
4142
4143 (if (and sans-offspring
4144 relative-depth
4145 (< relative-depth 0))
4146 (error (concat "Attempt to shift topic outwards without offspring,"
4147 " would cause containment discontinuity.")))
1977b8f6
RS
4148
4149 (let* ((relative-depth (or relative-depth 0))
fd5359c6 4150 (new-depth (allout-depth))
1977b8f6
RS
4151 (starting-depth (or starting-depth new-depth))
4152 (on-starting-call (null starting-point))
4153 (index (or index
4154 ;; Leave index null on starting call, so rebullet-heading
4155 ;; calculates it at what might be new depth:
4156 (and (or (zerop relative-depth)
4157 (not on-starting-call))
fd5359c6 4158 (allout-sibling-index))))
d8350998 4159 (starting-index index)
1977b8f6 4160 (moving-outwards (< 0 relative-depth))
d8350998
CY
4161 (starting-point (or starting-point (point)))
4162 (local-point (point)))
1977b8f6
RS
4163
4164 ;; Sanity check for excessive promotion done only on starting call:
4165 (and on-starting-call
4166 moving-outwards
4167 (> 0 (+ starting-depth relative-depth))
d8350998 4168 (error "Attempt to shift topic out beyond level 1"))
1977b8f6
RS
4169
4170 (cond ((= starting-depth new-depth)
d8350998
CY
4171 ;; We're at depth to work on this one.
4172
4173 ;; When shifting out we work on the children before working on
4174 ;; the parent to avoid interim `allout-aberrant-container-p'
4175 ;; aberrancy, and vice-versa when shifting in:
4176 (if (>= relative-depth 0)
4177 (allout-rebullet-heading nil
4178 (+ starting-depth relative-depth)
4179 nil ;;; number
4180 index
4181 nil)) ;;; do-successors
4182 (when (not sans-offspring)
4183 ;; ... and work on subsequent ones which are at greater depth:
4184 (setq index 0)
4185 (allout-next-heading)
4186 (while (and (not (eobp))
4187 (< starting-depth (allout-depth)))
4188 (setq index (1+ index))
4189 (allout-rebullet-topic-grunt relative-depth
4190 (1+ starting-depth)
4191 starting-point
4192 index)))
4193 (when (< relative-depth 0)
4194 (save-excursion
4195 (goto-char local-point)
b6a5875b 4196 (allout-rebullet-heading nil ;;; instead
d8350998
CY
4197 (+ starting-depth relative-depth)
4198 nil ;;; number
4199 starting-index
4200 nil)))) ;;; do-successors
1977b8f6
RS
4201
4202 ((< starting-depth new-depth)
60c7bbd2 4203 ;; Rare case -- subtopic more than one level deeper than parent.
1977b8f6 4204 ;; Treat this one at an even deeper level:
d8350998
CY
4205 (allout-rebullet-topic-grunt relative-depth
4206 new-depth
4207 starting-point
4208 index
4209 sans-offspring)))
1977b8f6
RS
4210
4211 (if on-starting-call
4212 (progn
4213 ;; Rectify numbering of former siblings of the adjusted topic,
4214 ;; if topic has changed depth
4215 (if (or do-successors
4216 (and (not (zerop relative-depth))
d8350998
CY
4217 (or (= allout-recent-depth starting-depth)
4218 (= allout-recent-depth (+ starting-depth
1977b8f6 4219 relative-depth)))))
fd5359c6 4220 (allout-rebullet-heading nil nil nil nil t))
1977b8f6
RS
4221 ;; Now rectify numbering of new siblings of the adjusted topic,
4222 ;; if depth has been changed:
4223 (progn (goto-char starting-point)
4224 (if (not (zerop relative-depth))
fd5359c6 4225 (allout-rebullet-heading nil nil nil nil t)))))
1977b8f6
RS
4226 )
4227 )
fd5359c6
MR
4228;;;_ > allout-renumber-to-depth (&optional depth)
4229(defun allout-renumber-to-depth (&optional depth)
19b84ba3
RS
4230 "Renumber siblings at current depth.
4231
4232Affects superior topics if optional arg DEPTH is less than current depth.
c567ac01
RS
4233
4234Returns final depth."
4235
4236 ;; Proceed by level, processing subsequent siblings on each,
4237 ;; ascending until we get shallower than the start depth:
4238
fd5359c6 4239 (let ((ascender (allout-depth))
9179616f 4240 was-eobp)
c567ac01 4241 (while (and (not (eobp))
fd5359c6 4242 (allout-depth)
d8350998 4243 (>= allout-recent-depth depth)
c567ac01
RS
4244 (>= ascender depth))
4245 ; Skip over all topics at
4246 ; lesser depths, which can not
4247 ; have been disturbed:
9179616f 4248 (while (and (not (setq was-eobp (eobp)))
d8350998 4249 (> allout-recent-depth ascender))
fd5359c6 4250 (allout-next-heading))
c567ac01 4251 ; Prime ascender for ascension:
d8350998
CY
4252 (setq ascender (1- allout-recent-depth))
4253 (if (>= allout-recent-depth depth)
b6a5875b 4254 (allout-rebullet-heading nil ;;; instead
c567ac01
RS
4255 nil ;;; depth
4256 nil ;;; number-control
4257 nil ;;; index
9179616f
DL
4258 t)) ;;; do-successors
4259 (if was-eobp (goto-char (point-max)))))
d8350998 4260 allout-recent-depth)
fd5359c6
MR
4261;;;_ > allout-number-siblings (&optional denumber)
4262(defun allout-number-siblings (&optional denumber)
c567ac01 4263 "Assign numbered topic prefix to this topic and its siblings.
1977b8f6 4264
60c7bbd2 4265With universal argument, denumber -- assign default bullet to this
c567ac01 4266topic and its siblings.
1977b8f6 4267
c567ac01
RS
4268With repeated universal argument (`^U^U'), solicit bullet for each
4269rebulleting each topic at this level."
1977b8f6
RS
4270
4271 (interactive "P")
4272
4273 (save-excursion
fd5359c6
MR
4274 (allout-back-to-current-heading)
4275 (allout-beginning-of-level)
d8350998 4276 (let ((depth allout-recent-depth)
c567ac01 4277 (index (if (not denumber) 1))
1977b8f6
RS
4278 (use-bullet (equal '(16) denumber))
4279 (more t))
4280 (while more
b6a5875b 4281 (allout-rebullet-heading use-bullet ;;; instead
c567ac01 4282 depth ;;; depth
1977b8f6
RS
4283 t ;;; number-control
4284 index ;;; index
4285 nil) ;;; do-successors
4286 (if index (setq index (1+ index)))
fd5359c6
MR
4287 (setq more (allout-next-sibling depth nil))))))
4288;;;_ > allout-shift-in (arg)
4289(defun allout-shift-in (arg)
d8350998
CY
4290 "Increase depth of current heading and any items collapsed within it.
4291
4292With a negative argument, the item is shifted out using
4293`allout-shift-out', instead.
4294
4295With an argument greater than one, shift-in the item but not its
4296offspring, making the item into a sibling of its former children,
97610156 4297and a child of sibling that formerly preceded it.
d8350998
CY
4298
4299You are not allowed to shift the first offspring of a topic
4300inwards, because that would yield a \"containment
4301discontinuity\", where the depth difference between a topic and
4302its immediate offspring is greater than one. The first topic in
4303the file can be adjusted to any positive depth, however."
d82979ea 4304
1977b8f6 4305 (interactive "p")
d8350998
CY
4306 (if (< arg 0)
4307 (allout-shift-out (* arg -1))
4308 ;; refuse to create a containment discontinuity:
4309 (save-excursion
4310 (allout-back-to-current-heading)
4311 (if (not (bobp))
4312 (let* ((current-depth allout-recent-depth)
4313 (start-point (point))
4314 (predecessor-depth (progn
4315 (forward-char -1)
4316 (allout-goto-prefix-doublechecked)
4317 (if (< (point) start-point)
4318 allout-recent-depth
4319 0))))
4320 (if (and (> predecessor-depth 0)
4321 (> (1+ current-depth)
4322 (1+ predecessor-depth)))
4323 (error (concat "Disallowed shift deeper than"
498a6493
CY
4324 " containing topic's children."))
4325 (allout-back-to-current-heading)
4326 (if (< allout-recent-depth (1+ current-depth))
4327 (allout-show-children))))))
d8350998
CY
4328 (let ((where (point)))
4329 (allout-rebullet-topic 1 (and (> arg 1) 'sans-offspring))
4330 (run-hook-with-args 'allout-structure-shifted-hook arg where))))
fd5359c6
MR
4331;;;_ > allout-shift-out (arg)
4332(defun allout-shift-out (arg)
d82979ea 4333 "Decrease depth of current heading and any topics collapsed within it.
d8350998
CY
4334This will make the item a sibling of its former container.
4335
4336With a negative argument, the item is shifted in using
4337`allout-shift-in', instead.
4338
4339With an argument greater than one, shift-out the item's offspring
4340but not the item itself, making the former children siblings of
4341the item.
d82979ea 4342
d8350998
CY
4343With an argument greater than 1, the item's offspring are shifted
4344out without shifting the item. This will make the immediate
4345subtopics into siblings of the item."
1977b8f6 4346 (interactive "p")
d8350998
CY
4347 (if (< arg 0)
4348 (allout-shift-in (* arg -1))
4349 ;; Get proper exposure in this area:
4350 (save-excursion (if (allout-ascend)
4351 (allout-show-children)))
4352 ;; Show collapsed children if there's a successor which will become
4353 ;; their sibling:
4354 (if (and (allout-current-topic-collapsed-p)
4355 (save-excursion (allout-next-sibling)))
4356 (allout-show-children))
4357 (let ((where (and (allout-depth) allout-recent-prefix-beginning)))
4358 (save-excursion
4359 (if (> arg 1)
4360 ;; Shift the offspring but not the topic:
4361 (let ((children-chart (allout-chart-subtree 1)))
4362 (if (listp (car children-chart))
4363 ;; whoops:
4364 (setq children-chart (allout-flatten children-chart)))
4365 (save-excursion
4366 (dolist (child-point children-chart)
4367 (goto-char child-point)
4368 (allout-shift-out 1))))
4369 (allout-rebullet-topic (* arg -1))))
4370 (run-hook-with-args 'allout-structure-shifted-hook (* arg -1) where))))
c567ac01 4371;;;_ : Surgery (kill-ring) functions with special provisions for outlines:
fd5359c6
MR
4372;;;_ > allout-kill-line (&optional arg)
4373(defun allout-kill-line (&optional arg)
c567ac01 4374 "Kill line, adjusting subsequent lines suitably for outline mode."
1977b8f6
RS
4375
4376 (interactive "*P")
d82979ea 4377
96b83743
EZ
4378 (if (or (not (allout-mode-p))
4379 (not (bolp))
2a1408fd 4380 (not (save-match-data (looking-at allout-regexp))))
4034b0e2 4381 ;; Just do a regular kill:
96b83743
EZ
4382 (kill-line arg)
4383 ;; Ah, have to watch out for adjustments:
4384 (let* ((beg (point))
d8350998 4385 end
96b83743
EZ
4386 (beg-hidden (allout-hidden-p))
4387 (end-hidden (save-excursion (allout-end-of-current-line)
d8350998 4388 (setq end (point))
96b83743 4389 (allout-hidden-p)))
d8350998 4390 (depth (allout-depth)))
96b83743 4391
d8350998 4392 (allout-annotate-hidden beg end)
4ec28e14
KM
4393 (unwind-protect
4394 (if (and (not beg-hidden) (not end-hidden))
4395 (allout-unprotected (kill-line arg))
4396 (kill-line arg))
11c46e15 4397 (run-hooks 'allout-after-copy-or-kill-hook)
4ec28e14
KM
4398 (allout-deannotate-hidden beg end)
4399
4400 (if allout-numbered-bullet
4401 (save-excursion ; Renumber subsequent topics if needed:
4402 (if (not (save-match-data (looking-at allout-regexp)))
4403 (allout-next-heading))
4404 (allout-renumber-to-depth depth)))
4405 (run-hook-with-args 'allout-structure-deleted-hook depth (point))))))
1b4d97b2
CY
4406;;;_ > allout-copy-line-as-kill ()
4407(defun allout-copy-line-as-kill ()
4408 "Like allout-kill-topic, but save to kill ring instead of deleting."
4409 (interactive)
4410 (let ((buffer-read-only t))
4411 (condition-case nil
4412 (allout-kill-line)
4413 (buffer-read-only nil))))
fd5359c6
MR
4414;;;_ > allout-kill-topic ()
4415(defun allout-kill-topic ()
c567ac01
RS
4416 "Kill topic together with subtopics.
4417
96b83743
EZ
4418Trailing whitespace is killed with a topic if that whitespace:
4419
4420 - would separate the topic from a subsequent sibling
4421 - would separate the topic from the end of buffer
4422 - would not be added to whitespace already separating the topic from the
4423 previous one.
4424
d8350998 4425Topic exposure is marked with text-properties, to be used by
75bcb912 4426`allout-yank-processing' for exposure recovery."
1977b8f6
RS
4427
4428 (interactive)
0458fa80 4429 (let* ((inhibit-field-text-motion t)
96b83743 4430 (beg (prog1 (allout-back-to-current-heading) (beginning-of-line)))
ede4ac6a 4431 end
d8350998 4432 (depth allout-recent-depth))
fd5359c6 4433 (allout-end-of-current-subtree)
96b83743
EZ
4434 (if (and (/= (current-column) 0) (not (eobp)))
4435 (forward-char 1))
1977b8f6 4436 (if (not (eobp))
2a1408fd 4437 (if (and (save-match-data (looking-at "\n"))
96b83743
EZ
4438 (or (save-excursion
4439 (or (not (allout-next-heading))
d8350998 4440 (= depth allout-recent-depth)))
96b83743
EZ
4441 (and (> (- beg (point-min)) 3)
4442 (string= (buffer-substring (- beg 2) beg) "\n\n"))))
c567ac01 4443 (forward-char 1)))
353e2ef2 4444
ede4ac6a 4445 (allout-annotate-hidden beg (setq end (point)))
4ec28e14 4446 (unwind-protect ; for possible barf-if-buffer-read-only.
ede4ac6a 4447 (allout-unprotected (kill-region beg end))
4ec28e14 4448 (allout-deannotate-hidden beg end)
11c46e15 4449 (run-hooks 'allout-after-copy-or-kill-hook)
d8350998 4450
4ec28e14
KM
4451 (save-excursion
4452 (allout-renumber-to-depth depth))
4453 (run-hook-with-args 'allout-structure-deleted-hook depth (point)))))
1b4d97b2
CY
4454;;;_ > allout-copy-topic-as-kill ()
4455(defun allout-copy-topic-as-kill ()
75bcb912 4456 "Like `allout-kill-topic', but save to kill ring instead of deleting."
1b4d97b2
CY
4457 (interactive)
4458 (let ((buffer-read-only t))
4459 (condition-case nil
4460 (allout-kill-topic)
4461 (buffer-read-only (message "Topic copied...")))))
d8350998
CY
4462;;;_ > allout-annotate-hidden (begin end)
4463(defun allout-annotate-hidden (begin end)
4464 "Qualify text with properties to indicate exposure status."
4465
1b4d97b2
CY
4466 (let ((was-modified (buffer-modified-p))
4467 (buffer-read-only nil))
ede4ac6a 4468 (allout-deannotate-hidden begin end)
d8350998
CY
4469 (save-excursion
4470 (goto-char begin)
4471 (let (done next prev overlay)
4472 (while (not done)
4473 ;; at or advance to start of next hidden region:
4474 (if (not (allout-hidden-p))
4475 (setq next
1b4d97b2 4476 (max (1+ (point))
1c9b9df0
KM
4477 (allout-next-single-char-property-change (point)
4478 'invisible
4479 nil end))))
d8350998 4480 (if (or (not next) (eq prev next))
60c7bbd2 4481 ;; still not at start of hidden area -- must not be any left.
d8350998
CY
4482 (setq done t)
4483 (goto-char next)
4484 (setq prev next)
4485 (if (not (allout-hidden-p))
4486 ;; still not at start of hidden area.
4487 (setq done t)
4488 (setq overlay (allout-get-invisibility-overlay))
4489 (setq next (overlay-end overlay)
4490 prev next)
4491 ;; advance to end of this hidden area:
4492 (when next
4493 (goto-char next)
4494 (allout-unprotected
ede4ac6a
KS
4495 (let ((buffer-undo-list t))
4496 (put-text-property (overlay-start overlay) next
4497 'allout-was-hidden t)))))))))
d8350998 4498 (set-buffer-modified-p was-modified)))
ede4ac6a
KS
4499;;;_ > allout-deannotate-hidden (begin end)
4500(defun allout-deannotate-hidden (begin end)
4501 "Remove allout hidden-text annotation between BEGIN and END."
4502
4503 (allout-unprotected
4504 (let ((inhibit-read-only t)
4505 (buffer-undo-list t))
11c46e15
KM
4506 (remove-text-properties begin (min end (point-max))
4507 '(allout-was-hidden t)))))
d8350998
CY
4508;;;_ > allout-hide-by-annotation (begin end)
4509(defun allout-hide-by-annotation (begin end)
4510 "Translate text properties indicating exposure status into actual exposure."
4511 (save-excursion
4512 (goto-char begin)
4513 (let ((was-modified (buffer-modified-p))
4514 done next prev)
4515 (while (not done)
4516 ;; at or advance to start of next annotation:
4517 (if (not (get-text-property (point) 'allout-was-hidden))
1c9b9df0
KM
4518 (setq next (allout-next-single-char-property-change
4519 (point) 'allout-was-hidden nil end)))
d8350998 4520 (if (or (not next) (eq prev next))
60c7bbd2 4521 ;; no more or not advancing -- must not be any left.
d8350998
CY
4522 (setq done t)
4523 (goto-char next)
4524 (setq prev next)
4525 (if (not (get-text-property (point) 'allout-was-hidden))
4526 ;; still not at start of annotation.
4527 (setq done t)
4528 ;; advance to just after end of this annotation:
1c9b9df0
KM
4529 (setq next (allout-next-single-char-property-change
4530 (point) 'allout-was-hidden nil end))
d806ab68
KM
4531 (let ((o (make-overlay prev next nil 'front-advance)))
4532 (overlay-put o 'category 'allout-exposure-category)
4533 (overlay-put o 'evaporate t))
ede4ac6a 4534 (allout-deannotate-hidden prev next)
d8350998
CY
4535 (setq prev next)
4536 (if next (goto-char next)))))
4537 (set-buffer-modified-p was-modified))))
fd5359c6
MR
4538;;;_ > allout-yank-processing ()
4539(defun allout-yank-processing (&optional arg)
c567ac01 4540
96b83743 4541 "Incidental allout-specific business to be done just after text yanks.
19b84ba3 4542
c567ac01 4543Does depth adjustment of yanked topics, when:
1977b8f6 4544
c567ac01
RS
45451 the stuff being yanked starts with a valid outline header prefix, and
45462 it is being yanked at the end of a line which consists of only a valid
1977b8f6
RS
4547 topic prefix.
4548
a0776d6b 4549Also, adjusts numbering of subsequent siblings when appropriate.
1977b8f6 4550
c567ac01
RS
4551Depth adjustment alters the depth of all the topics being yanked
4552the amount it takes to make the first topic have the depth of the
4553header into which it's being yanked.
1977b8f6 4554
c567ac01
RS
4555The point is left in front of yanked, adjusted topics, rather than
4556at the end (and vice-versa with the mark). Non-adjusted yanks,
d82979ea 4557however, are left exactly like normal, non-allout-specific yanks."
c567ac01
RS
4558
4559 (interactive "*P")
4560 ; Get to beginning, leaving
4561 ; region around subject:
96b83743 4562 (if (< (allout-mark-marker t) (point))
c567ac01 4563 (exchange-point-and-mark))
2a1408fd
DN
4564 (save-match-data
4565 (let* ((subj-beg (point))
4566 (into-bol (bolp))
4567 (subj-end (allout-mark-marker t))
4568 ;; 'resituate' if yanking an entire topic into topic header:
4569 (resituate (and (let ((allout-inhibit-aberrance-doublecheck t))
4570 (allout-e-o-prefix-p))
4571 (looking-at allout-regexp)
4572 (allout-prefix-data)))
4573 ;; `rectify-numbering' if resituating (where several topics may
4574 ;; be resituating) or yanking a topic into a topic slot (bol):
4575 (rectify-numbering (or resituate
8dd59f01 4576 (and into-bol (looking-at allout-regexp)))))
2a1408fd 4577 (if resituate
8dd59f01 4578 ;; Yanking a topic into the start of a topic -- reconcile to fit:
2a1408fd
DN
4579 (let* ((inhibit-field-text-motion t)
4580 (prefix-len (if (not (match-end 1))
4581 1
4582 (- (match-end 1) subj-beg)))
4583 (subj-depth allout-recent-depth)
4584 (prefix-bullet (allout-recent-bullet))
4585 (adjust-to-depth
4586 ;; Nil if adjustment unnecessary, otherwise depth to which
4587 ;; adjustment should be made:
4588 (save-excursion
4589 (and (goto-char subj-end)
4590 (eolp)
4591 (goto-char subj-beg)
4592 (and (looking-at allout-regexp)
4593 (progn
4594 (beginning-of-line)
4595 (not (= (point) subj-beg)))
4596 (looking-at allout-regexp)
4597 (allout-prefix-data))
4598 allout-recent-depth)))
4599 (more t))
4600 (setq rectify-numbering allout-numbered-bullet)
4601 (if adjust-to-depth
c567ac01 4602 ; Do the adjustment:
2a1408fd
DN
4603 (progn
4604 (save-restriction
4605 (narrow-to-region subj-beg subj-end)
c567ac01
RS
4606 ; Trim off excessive blank
4607 ; line at end, if any:
2a1408fd
DN
4608 (goto-char (point-max))
4609 (if (looking-at "^$")
4610 (allout-unprotected (delete-char -1)))
c567ac01
RS
4611 ; Work backwards, with each
4612 ; shallowest level,
4613 ; successively excluding the
4614 ; last processed topic from
4615 ; the narrow region:
2a1408fd
DN
4616 (while more
4617 (allout-back-to-current-heading)
c567ac01 4618 ; go as high as we can in each bunch:
2a1408fd
DN
4619 (while (allout-ascend t))
4620 (save-excursion
4621 (allout-unprotected
4622 (allout-rebullet-topic-grunt (- adjust-to-depth
4623 subj-depth)))
4624 (allout-depth))
4625 (if (setq more (not (bobp)))
4626 (progn (widen)
4627 (forward-char -1)
4628 (narrow-to-region subj-beg (point))))))
b6a5875b
KM
4629 ;; Remove new heading prefix:
4630 (allout-unprotected
4631 (progn
4632 (delete-region (point) (+ (point)
4633 prefix-len
4634 (- adjust-to-depth
4635 subj-depth)))
c567ac01
RS
4636 ; and delete residual subj
4637 ; prefix digits and space:
b6a5875b 4638 (while (looking-at "[0-9]") (delete-char 1))
e47ca23b
KM
4639 (delete-char -1)
4640 (if (not (eolp))
4641 (forward-char))))
b6a5875b 4642 ;; Assert new topic's bullet - minimal effort if unchanged:
b0911414 4643 (allout-rebullet-heading (string-to-char prefix-bullet)))
2a1408fd
DN
4644 (exchange-point-and-mark))))
4645 (if rectify-numbering
4646 (progn
4647 (save-excursion
c567ac01 4648 ; Give some preliminary feedback:
2a1408fd 4649 (message "... reconciling numbers")
c567ac01 4650 ; ... and renumber, in case necessary:
2a1408fd
DN
4651 (goto-char subj-beg)
4652 (if (allout-goto-prefix-doublechecked)
4653 (allout-unprotected
b6a5875b 4654 (allout-rebullet-heading nil ;;; instead
2a1408fd
DN
4655 (allout-depth) ;;; depth
4656 nil ;;; number-control
4657 nil ;;; index
4658 t)))
4659 (message ""))))
4660 (if (or into-bol resituate)
4661 (allout-hide-by-annotation (point) (allout-mark-marker t))
4662 (allout-deannotate-hidden (allout-mark-marker t) (point)))
4663 (if (not resituate)
4664 (exchange-point-and-mark))
4665 (run-hook-with-args 'allout-structure-added-hook subj-beg subj-end))))
fd5359c6
MR
4666;;;_ > allout-yank (&optional arg)
4667(defun allout-yank (&optional arg)
aad94676 4668 "`allout-mode' yank, with depth and numbering adjustment of yanked topics.
19b84ba3 4669
a0776d6b 4670Non-topic yanks work no differently than normal yanks.
c567ac01
RS
4671
4672If a topic is being yanked into a bare topic prefix, the depth of the
4673yanked topic is adjusted to the depth of the topic prefix.
4674
2265e017 4675 1 we're yanking in an `allout-mode' buffer
c567ac01
RS
4676 2 the stuff being yanked starts with a valid outline header prefix, and
4677 3 it is being yanked at the end of a line which consists of only a valid
4678 topic prefix.
4679
4680If these conditions hold then the depth of the yanked topics are all
4681adjusted the amount it takes to make the first one at the depth of the
4682header into which it's being yanked.
4683
4684The point is left in front of yanked, adjusted topics, rather than
4685at the end (and vice-versa with the mark). Non-adjusted yanks,
4686however, (ones that don't qualify for adjustment) are handled
4687exactly like normal yanks.
4688
a0776d6b 4689Numbering of yanked topics, and the successive siblings at the depth
c567ac01
RS
4690into which they're being yanked, is adjusted.
4691
539d7736
JB
4692`allout-yank-pop' works with `allout-yank' just like normal `yank-pop'
4693works with normal `yank' in non-outline buffers."
1977b8f6
RS
4694
4695 (interactive "*P")
4696 (setq this-command 'yank)
48bd8440
EZ
4697 (allout-unprotected
4698 (yank arg))
fd5359c6 4699 (if (allout-mode-p)
48bd8440 4700 (allout-yank-processing)))
fd5359c6
MR
4701;;;_ > allout-yank-pop (&optional arg)
4702(defun allout-yank-pop (&optional arg)
2265e017 4703 "Yank-pop like `allout-yank' when popping to bare outline prefixes.
19b84ba3
RS
4704
4705Adapts level of popped topics to level of fresh prefix.
c567ac01 4706
60c7bbd2 4707Note -- prefix changes to distinctive bullets will stick, if followed
c567ac01 4708by pops to non-distinctive yanks. Bug..."
1977b8f6
RS
4709
4710 (interactive "*p")
1977b8f6 4711 (setq this-command 'yank)
c567ac01 4712 (yank-pop arg)
fd5359c6
MR
4713 (if (allout-mode-p)
4714 (allout-yank-processing)))
1977b8f6 4715
c567ac01
RS
4716;;;_ - Specialty bullet functions
4717;;;_ : File Cross references
fd5359c6
MR
4718;;;_ > allout-resolve-xref ()
4719(defun allout-resolve-xref ()
19b84ba3
RS
4720 "Pop to file associated with current heading, if it has an xref bullet.
4721
fd5359c6 4722\(Works according to setting of `allout-file-xref-bullet')."
1977b8f6 4723 (interactive)
fd5359c6 4724 (if (not allout-file-xref-bullet)
1977b8f6 4725 (error
60c7bbd2 4726 "Outline cross references disabled -- no `allout-file-xref-bullet'")
fd5359c6 4727 (if (not (string= (allout-current-bullet) allout-file-xref-bullet))
635ca796 4728 (error "Current heading lacks cross-reference bullet `%s'"
fd5359c6 4729 allout-file-xref-bullet)
0458fa80
EZ
4730 (let ((inhibit-field-text-motion t)
4731 file-name)
2a1408fd
DN
4732 (save-match-data
4733 (save-excursion
4734 (let* ((text-start allout-recent-prefix-end)
9b026d9f 4735 (heading-end (point-at-eol)))
2a1408fd
DN
4736 (goto-char text-start)
4737 (setq file-name
4738 (if (re-search-forward "\\s-\\(\\S-*\\)" heading-end t)
4739 (buffer-substring (match-beginning 1)
4740 (match-end 1)))))))
69027dea 4741 (setq file-name (expand-file-name file-name))
1977b8f6
RS
4742 (if (or (file-exists-p file-name)
4743 (if (file-writable-p file-name)
4744 (y-or-n-p (format "%s not there, create one? "
4745 file-name))
4746 (error "%s not found and can't be created" file-name)))
4747 (condition-case failure
4748 (find-file-other-window file-name)
d8350998 4749 (error failure))
1977b8f6
RS
4750 (error "%s not found" file-name))
4751 )
4752 )
4753 )
4754 )
19b84ba3 4755
9179616f 4756;;;_ #6 Exposure Control
c567ac01
RS
4757
4758;;;_ - Fundamental
d82979ea
EZ
4759;;;_ > allout-flag-region (from to flag)
4760(defun allout-flag-region (from to flag)
48bd8440
EZ
4761 "Conceal text between FROM and TO if FLAG is non-nil, else reveal it.
4762
4763Exposure-change hook `allout-exposure-change-hook' is run with the same
1e004a83 4764arguments as this function, after the exposure changes are made."
96b83743 4765
96b83743 4766 ;; We use outline invisibility spec.
01fc9422 4767 (remove-overlays from to 'category 'allout-exposure-category)
96b83743 4768 (when flag
2a1408fd 4769 (let ((o (make-overlay from to nil 'front-advance)))
01fc9422 4770 (overlay-put o 'category 'allout-exposure-category)
68ba37fb 4771 (overlay-put o 'evaporate t)
96b83743 4772 (when (featurep 'xemacs)
01fc9422 4773 (let ((props (symbol-plist 'allout-exposure-category)))
96b83743 4774 (while props
1c9b9df0
KM
4775 (condition-case nil
4776 ;; as of 2008-02-27, xemacs lacks modification-hooks
4777 (overlay-put o (pop props) (pop props))
3f2b07f8
KM
4778 (error nil))))))
4779 (setq allout-this-command-hid-text t))
48bd8440 4780 (run-hook-with-args 'allout-exposure-change-hook from to flag))
fd5359c6
MR
4781;;;_ > allout-flag-current-subtree (flag)
4782(defun allout-flag-current-subtree (flag)
96b83743 4783 "Conceal currently-visible topic's subtree if FLAG non-nil, else reveal it."
19b84ba3 4784
c567ac01 4785 (save-excursion
fd5359c6 4786 (allout-back-to-current-heading)
0458fa80
EZ
4787 (let ((inhibit-field-text-motion t))
4788 (end-of-line))
96b83743
EZ
4789 (allout-flag-region (point)
4790 ;; Exposing must not leave trailing blanks hidden,
4791 ;; but can leave them exposed when hiding, so we
4792 ;; can use flag's inverse as the
4793 ;; include-trailing-blank cue:
4794 (allout-end-of-current-subtree (not flag))
4795 flag)))
c567ac01 4796
c567ac01 4797;;;_ - Topic-specific
01fc9422
EZ
4798;;;_ > allout-show-entry ()
4799(defun allout-show-entry ()
4800 "Like `allout-show-current-entry', but reveals entries in hidden topics.
c567ac01
RS
4801
4802This is a way to give restricted peek at a concealed locality without the
4803expense of exposing its context, but can leave the outline with aberrant
96b83743
EZ
4804exposure. `allout-show-offshoot' should be used after the peek to rectify
4805the exposure."
c567ac01
RS
4806
4807 (interactive)
4808 (save-excursion
96b83743 4809 (let (beg end)
d8350998 4810 (allout-goto-prefix-doublechecked)
96b83743
EZ
4811 (setq beg (if (allout-hidden-p) (1- (point)) (point)))
4812 (setq end (allout-pre-next-prefix))
4813 (allout-flag-region beg end nil)
9179616f 4814 (list beg end))))
fd5359c6
MR
4815;;;_ > allout-show-children (&optional level strict)
4816(defun allout-show-children (&optional level strict)
c567ac01
RS
4817
4818 "If point is visible, show all direct subheadings of this heading.
19b84ba3 4819
2265e017 4820Otherwise, do `allout-show-to-offshoot', and then show subheadings.
c567ac01
RS
4821
4822Optional LEVEL specifies how many levels below the current level
4823should be shown, or all levels if t. Default is 1.
4824
4825Optional STRICT means don't resort to -show-to-offshoot, no matter
4826what. This is basically so -show-to-offshoot, which is called by
4827this function, can employ the pure offspring-revealing capabilities of
19b84ba3
RS
4828it.
4829
4830Returns point at end of subtree that was opened, if any. (May get a
4831point of non-opened subtree?)"
c567ac01
RS
4832
4833 (interactive "p")
96b83743 4834 (let ((start-point (point)))
19b84ba3 4835 (if (and (not strict)
96b83743 4836 (allout-hidden-p))
c567ac01 4837
96b83743
EZ
4838 (progn (allout-show-to-offshoot) ; Point's concealed, open to
4839 ; expose it.
4840 ;; Then recurse, but with "strict" set so we don't
4841 ;; infinite regress:
4842 (allout-show-children level t))
c567ac01 4843
19b84ba3 4844 (save-excursion
96b83743
EZ
4845 (allout-beginning-of-current-line)
4846 (save-restriction
d8350998 4847 (let* (depth
65e7eb3d
EZ
4848 ;; translate the level spec for this routine to the ones
4849 ;; used by -chart-subtree and -chart-to-reveal:
4850 (chart-level (cond ((not level) 1)
4851 ((eq level t) nil)
4852 (t level)))
4853 (chart (allout-chart-subtree chart-level))
4854 (to-reveal (or (allout-chart-to-reveal chart chart-level)
d8350998
CY
4855 ;; interactive, show discontinuous children:
4856 (and chart
1c9b9df0 4857 (allout-called-interactively-p)
d8350998
CY
4858 (save-excursion
4859 (allout-back-to-current-heading)
4860 (setq depth (allout-current-depth))
4861 (and (allout-next-heading)
4862 (> allout-recent-depth
4863 (1+ depth))))
4864 (message
4865 "Discontinuous offspring; use `%s %s'%s."
4866 (substitute-command-keys
4867 "\\[universal-argument]")
4868 (substitute-command-keys
4869 "\\[allout-shift-out]")
4870 " to elevate them.")
4871 (allout-chart-to-reveal
4872 chart (- allout-recent-depth depth))))))
96b83743
EZ
4873 (goto-char start-point)
4874 (when (and strict (allout-hidden-p))
4875 ;; Concealed root would already have been taken care of,
4876 ;; unless strict was set.
4877 (allout-flag-region (point) (allout-snug-back) nil)
4878 (when allout-show-bodies
4879 (goto-char (car to-reveal))
4880 (allout-show-current-entry)))
4881 (while to-reveal
4882 (goto-char (car to-reveal))
4883 (allout-flag-region (save-excursion (allout-snug-back) (point))
4884 (progn (search-forward "\n" nil t)
4885 (1- (point)))
4886 nil)
4887 (when allout-show-bodies
4888 (goto-char (car to-reveal))
4889 (allout-show-current-entry))
4890 (setq to-reveal (cdr to-reveal)))))))
4891 ;; Compensate for `save-excursion's maintenance of point
4892 ;; within invisible text:
4893 (goto-char start-point)))
fd5359c6
MR
4894;;;_ > allout-show-to-offshoot ()
4895(defun allout-show-to-offshoot ()
2265e017 4896 "Like `allout-show-entry', but reveals all concealed ancestors, as well.
c567ac01 4897
96b83743 4898Useful for coherently exposing to a random point in a hidden region."
c567ac01
RS
4899 (interactive)
4900 (save-excursion
0458fa80
EZ
4901 (let ((inhibit-field-text-motion t)
4902 (orig-pt (point))
d8350998 4903 (orig-pref (allout-goto-prefix-doublechecked))
c567ac01 4904 (last-at (point))
65e7eb3d
EZ
4905 (bag-it 0))
4906 (while (or (> bag-it 1) (allout-hidden-p))
96b83743 4907 (while (allout-hidden-p)
d8350998 4908 (move-beginning-of-line 1)
96b83743 4909 (if (allout-hidden-p) (forward-char -1)))
c567ac01 4910 (if (= last-at (setq last-at (point)))
65e7eb3d
EZ
4911 ;; Oops, we're not making any progress! Show the current topic
4912 ;; completely, and try one more time here, if we haven't already.
c567ac01 4913 (progn (beginning-of-line)
fd5359c6 4914 (allout-show-current-subtree)
c567ac01 4915 (goto-char orig-pt)
65e7eb3d
EZ
4916 (setq bag-it (1+ bag-it))
4917 (if (> bag-it 1)
4918 (error "allout-show-to-offshoot: %s"
4919 "Stumped by aberrant nesting.")))
4920 (if (> bag-it 0) (setq bag-it 0))
d8350998
CY
4921 (allout-show-children)
4922 (goto-char orig-pref)))
19b84ba3 4923 (goto-char orig-pt)))
fd5359c6
MR
4924 (if (allout-hidden-p)
4925 (allout-show-entry)))
4926;;;_ > allout-hide-current-entry ()
4927(defun allout-hide-current-entry ()
c567ac01
RS
4928 "Hide the body directly following this heading."
4929 (interactive)
fd5359c6 4930 (allout-back-to-current-heading)
c567ac01 4931 (save-excursion
0458fa80
EZ
4932 (let ((inhibit-field-text-motion t))
4933 (end-of-line))
96b83743 4934 (allout-flag-region (point)
d82979ea 4935 (progn (allout-end-of-entry) (point))
96b83743 4936 t)))
fd5359c6
MR
4937;;;_ > allout-show-current-entry (&optional arg)
4938(defun allout-show-current-entry (&optional arg)
96b83743 4939 "Show body following current heading, or hide entry with universal argument."
c567ac01
RS
4940
4941 (interactive "P")
4942 (if arg
fd5359c6 4943 (allout-hide-current-entry)
96b83743 4944 (save-excursion (allout-show-to-offshoot))
c567ac01 4945 (save-excursion
fd5359c6 4946 (allout-flag-region (point)
96b83743
EZ
4947 (progn (allout-end-of-entry t) (point))
4948 nil)
d82979ea 4949 )))
fd5359c6
MR
4950;;;_ > allout-show-current-subtree (&optional arg)
4951(defun allout-show-current-subtree (&optional arg)
75bcb912
JB
4952 "Show everything within the current topic.
4953With a repeat-count, expose this topic and its siblings."
19b84ba3
RS
4954 (interactive "P")
4955 (save-excursion
fd5359c6 4956 (if (<= (allout-current-depth) 0)
60c7bbd2 4957 ;; Outside any topics -- try to get to the first:
fd5359c6 4958 (if (not (allout-next-heading))
6a05d05f 4959 (error "No topics")
60c7bbd2
RS
4960 ;; got to first, outermost topic -- set to expose it and siblings:
4961 (message "Above outermost topic -- exposing all.")
96b83743
EZ
4962 (allout-flag-region (point-min)(point-max) nil))
4963 (allout-beginning-of-current-line)
19b84ba3 4964 (if (not arg)
96b83743 4965 (allout-flag-current-subtree nil)
fd5359c6
MR
4966 (allout-beginning-of-level)
4967 (allout-expose-topic '(* :))))))
96b83743
EZ
4968;;;_ > allout-current-topic-collapsed-p (&optional include-single-liners)
4969(defun allout-current-topic-collapsed-p (&optional include-single-liners)
4970 "True if the currently visible containing topic is already collapsed.
4971
64686e6d
RS
4972Single line topics intrinsically can be considered as being both
4973collapsed and uncollapsed. If optional INCLUDE-SINGLE-LINERS is
4974true, then single-line topics are considered to be collapsed. By
4975default, they are treated as being uncollapsed."
2a1408fd
DN
4976 (save-match-data
4977 (save-excursion
4978 (and
4979 ;; Is the topic all on one line (allowing for trailing blank line)?
4980 (>= (progn (allout-back-to-current-heading)
82b392a4
KM
4981 (let ((inhibit-field-text-motion t))
4982 (move-end-of-line 1))
2a1408fd
DN
4983 (point))
4984 (allout-end-of-current-subtree (not (looking-at "\n\n"))))
4985
4986 (or include-single-liners
4987 (progn (backward-char 1) (allout-hidden-p)))))))
fd5359c6
MR
4988;;;_ > allout-hide-current-subtree (&optional just-close)
4989(defun allout-hide-current-subtree (&optional just-close)
19b84ba3 4990 "Close the current topic, or containing topic if this one is already closed.
c567ac01 4991
19b84ba3 4992If this topic is closed and it's a top level topic, close this topic
353e2ef2 4993and its siblings.
19b84ba3 4994
96b83743 4995If optional arg JUST-CLOSE is non-nil, do not close the parent or
19b84ba3 4996siblings, even if the target topic is already closed."
c567ac01
RS
4997
4998 (interactive)
96b83743 4999 (let* ((from (point))
60c7bbd2 5000 (sibs-msg "Top-level topic already closed -- closing siblings...")
96b83743
EZ
5001 (current-exposed (not (allout-current-topic-collapsed-p t))))
5002 (cond (current-exposed (allout-flag-current-subtree t))
5003 (just-close nil)
d8350998 5004 ((allout-ascend) (allout-hide-current-subtree))
96b83743
EZ
5005 (t (goto-char 0)
5006 (message sibs-msg)
d8350998 5007 (allout-goto-prefix-doublechecked)
96b83743
EZ
5008 (allout-expose-topic '(0 :))
5009 (message (concat sibs-msg " Done."))))
5010 (goto-char from)))
2a1408fd
DN
5011;;;_ > allout-toggle-current-subtree-exposure
5012(defun allout-toggle-current-subtree-exposure ()
5013 "Show or hide the current subtree depending on its current state."
5014 ;; thanks to tassilo for suggesting this.
5015 (interactive)
5016 (save-excursion
5017 (allout-back-to-heading)
5018 (if (allout-hidden-p (point-at-eol))
5019 (allout-show-current-subtree)
5020 (allout-hide-current-subtree))))
fd5359c6
MR
5021;;;_ > allout-show-current-branches ()
5022(defun allout-show-current-branches ()
c567ac01
RS
5023 "Show all subheadings of this heading, but not their bodies."
5024 (interactive)
0458fa80
EZ
5025 (let ((inhibit-field-text-motion t))
5026 (beginning-of-line))
fd5359c6
MR
5027 (allout-show-children t))
5028;;;_ > allout-hide-current-leaves ()
5029(defun allout-hide-current-leaves ()
353e2ef2 5030 "Hide the bodies of the current topic and all its offspring."
c567ac01 5031 (interactive)
fd5359c6
MR
5032 (allout-back-to-current-heading)
5033 (allout-hide-region-body (point) (progn (allout-end-of-current-subtree)
c567ac01
RS
5034 (point))))
5035
5036;;;_ - Region and beyond
fd5359c6
MR
5037;;;_ > allout-show-all ()
5038(defun allout-show-all ()
c567ac01
RS
5039 "Show all of the text in the buffer."
5040 (interactive)
19b84ba3 5041 (message "Exposing entire buffer...")
96b83743 5042 (allout-flag-region (point-min) (point-max) nil)
19b84ba3 5043 (message "Exposing entire buffer... Done."))
fd5359c6
MR
5044;;;_ > allout-hide-bodies ()
5045(defun allout-hide-bodies ()
c567ac01
RS
5046 "Hide all of buffer except headings."
5047 (interactive)
fd5359c6
MR
5048 (allout-hide-region-body (point-min) (point-max)))
5049;;;_ > allout-hide-region-body (start end)
5050(defun allout-hide-region-body (start end)
c567ac01 5051 "Hide all body lines in the region, but not headings."
2a1408fd
DN
5052 (save-match-data
5053 (save-excursion
5054 (save-restriction
5055 (narrow-to-region start end)
5056 (goto-char (point-min))
5057 (let ((inhibit-field-text-motion t))
5058 (while (not (eobp))
5059 (end-of-line)
5060 (allout-flag-region (point) (allout-end-of-entry) t)
5061 (if (not (eobp))
5062 (forward-char
5063 (if (looking-at "\n\n")
5064 2 1)))))))))
c567ac01 5065
fd5359c6
MR
5066;;;_ > allout-expose-topic (spec)
5067(defun allout-expose-topic (spec)
19b84ba3 5068 "Apply exposure specs to successive outline topic items.
c567ac01 5069
fd5359c6
MR
5070Use the more convenient frontend, `allout-new-exposure', if you don't
5071need evaluation of the arguments, or even better, the `allout-layout'
19b84ba3
RS
5072variable-keyed mode-activation/auto-exposure feature of allout outline
5073mode. See the respective documentation strings for more details.
c567ac01
RS
5074
5075Cursor is left at start position.
5076
19b84ba3
RS
5077SPEC is either a number or a list.
5078
5079Successive specs on a list are applied to successive sibling topics.
c567ac01 5080
c07583cd 5081A simple spec (either a number, one of a few symbols, or the null
19b84ba3
RS
5082list) dictates the exposure for the corresponding topic.
5083
5084Non-null lists recursively designate exposure specs for respective
5085subtopics of the current topic.
c567ac01 5086
353e2ef2 5087The `:' repeat spec is used to specify exposure for any number of
19b84ba3 5088successive siblings, up to the trailing ones for which there are
353e2ef2 5089explicit specs following the `:'.
c567ac01
RS
5090
5091Simple (numeric and null-list) specs are interpreted as follows:
5092
19b84ba3
RS
5093 Numbers indicate the relative depth to open the corresponding topic.
5094 - negative numbers force the topic to be closed before opening to the
5095 absolute value of the number, so all siblings are open only to
5096 that level.
5097 - positive numbers open to the relative depth indicated by the
5098 number, but do not force already opened subtopics to be closed.
60c7bbd2 5099 - 0 means to close topic -- hide all offspring.
353e2ef2 5100 : - `repeat'
c567ac01 5101 apply prior element to all siblings at current level, *up to*
353e2ef2 5102 those siblings that would be covered by specs following the `:'
c567ac01 5103 on the list. Ie, apply to all topics at level but the last
c07583cd 5104 ones. (Only first of multiple colons at same level is
60c7bbd2 5105 respected -- subsequent ones are discarded.)
19b84ba3
RS
5106 * - completely opens the topic, including bodies.
5107 + - shows all the sub headers, but not the bodies
5108 - - exposes the body of the corresponding topic.
c567ac01
RS
5109
5110Examples:
fd5359c6 5111\(allout-expose-topic '(-1 : 0))
c567ac01
RS
5112 Close this and all following topics at current level, exposing
5113 only their immediate children, but close down the last topic
5114 at this current level completely.
fd5359c6 5115\(allout-expose-topic '(-1 () : 1 0))
c567ac01
RS
5116 Close current topic so only the immediate subtopics are shown;
5117 show the children in the second to last topic, and completely
5118 close the last one.
fd5359c6 5119\(allout-expose-topic '(-2 : -1 *))
c567ac01
RS
5120 Expose children and grandchildren of all topics at current
5121 level except the last two; expose children of the second to
5122 last and completely open the last one."
5123
5124 (interactive "xExposure spec: ")
19b84ba3
RS
5125 (if (not (listp spec))
5126 nil
fd5359c6 5127 (let ((depth (allout-depth))
19b84ba3
RS
5128 (max-pos 0)
5129 prev-elem curr-elem
96b83743 5130 stay)
19b84ba3
RS
5131 (while spec
5132 (setq prev-elem curr-elem
5133 curr-elem (car spec)
5134 spec (cdr spec))
5135 (cond ; Do current element:
5136 ((null curr-elem) nil)
5137 ((symbolp curr-elem)
fd5359c6
MR
5138 (cond ((eq curr-elem '*) (allout-show-current-subtree)
5139 (if (> allout-recent-end-of-subtree max-pos)
5140 (setq max-pos allout-recent-end-of-subtree)))
65e7eb3d
EZ
5141 ((eq curr-elem '+)
5142 (if (not (allout-hidden-p))
5143 (save-excursion (allout-hide-current-subtree t)))
5144 (allout-show-current-branches)
fd5359c6
MR
5145 (if (> allout-recent-end-of-subtree max-pos)
5146 (setq max-pos allout-recent-end-of-subtree)))
5147 ((eq curr-elem '-) (allout-show-current-entry))
19b84ba3
RS
5148 ((eq curr-elem ':)
5149 (setq stay t)
353e2ef2 5150 ;; Expand the `repeat' spec to an explicit version,
19b84ba3
RS
5151 ;; w.r.t. remaining siblings:
5152 (let ((residue ; = # of sibs not covered by remaining spec
60c7bbd2 5153 ;; Dang, could be nice to make use of the chart, sigh:
fd5359c6 5154 (- (length (allout-chart-siblings))
19b84ba3
RS
5155 (length spec))))
5156 (if (< 0 residue)
60c7bbd2 5157 ;; Some residue -- cover it with prev-elem:
19b84ba3
RS
5158 (setq spec (append (make-list residue prev-elem)
5159 spec)))))))
5160 ((numberp curr-elem)
96b83743 5161 (if (and (>= 0 curr-elem) (not (allout-hidden-p)))
fd5359c6 5162 (save-excursion (allout-hide-current-subtree t)
19b84ba3
RS
5163 (if (> 0 curr-elem)
5164 nil
fd5359c6 5165 (if (> allout-recent-end-of-subtree max-pos)
19b84ba3 5166 (setq max-pos
fd5359c6 5167 allout-recent-end-of-subtree)))))
19b84ba3 5168 (if (> (abs curr-elem) 0)
fd5359c6
MR
5169 (progn (allout-show-children (abs curr-elem))
5170 (if (> allout-recent-end-of-subtree max-pos)
5171 (setq max-pos allout-recent-end-of-subtree)))))
19b84ba3 5172 ((listp curr-elem)
fd5359c6
MR
5173 (if (allout-descend-to-depth (1+ depth))
5174 (let ((got (allout-expose-topic curr-elem)))
19b84ba3
RS
5175 (if (and got (> got max-pos)) (setq max-pos got))))))
5176 (cond (stay (setq stay nil))
5177 ((listp (car spec)) nil)
5178 ((> max-pos (point))
5179 ;; Capitalize on max-pos state to get us nearer next sibling:
5180 (progn (goto-char (min (point-max) max-pos))
fd5359c6
MR
5181 (allout-next-heading)))
5182 ((allout-next-sibling depth))))
19b84ba3 5183 max-pos)))
fd5359c6
MR
5184;;;_ > allout-old-expose-topic (spec &rest followers)
5185(defun allout-old-expose-topic (spec &rest followers)
d82979ea 5186
c07583cd 5187 "Deprecated. Use `allout-expose-topic' (with different schema
d82979ea
EZ
5188format) instead.
5189
5190Dictate wholesale exposure scheme for current topic, according to SPEC.
c567ac01
RS
5191
5192SPEC is either a number or a list. Optional successive args
5193dictate exposure for subsequent siblings of current topic.
5194
5195A simple spec (either a number, a special symbol, or the null list)
5196dictates the overall exposure for a topic. Non null lists are
5197composite specs whose first element dictates the overall exposure for
5198a topic, with the subsequent elements in the list interpreted as specs
5199that dictate the exposure for the successive offspring of the topic.
5200
5201Simple (numeric and null-list) specs are interpreted as follows:
5202
5203 - Numbers indicate the relative depth to open the corresponding topic:
5204 - negative numbers force the topic to be close before opening to the
5205 absolute value of the number.
5206 - positive numbers just open to the relative depth indicated by the number.
5207 - 0 just closes
353e2ef2
KH
5208 - `*' completely opens the topic, including bodies.
5209 - `+' shows all the sub headers, but not the bodies
5210 - `-' exposes the body and immediate offspring of the corresponding topic.
c567ac01
RS
5211
5212If the spec is a list, the first element must be a number, which
5213dictates the exposure depth of the topic as a whole. Subsequent
5214elements of the list are nested SPECs, dictating the specific exposure
5215for the corresponding offspring of the topic.
5216
e126900f 5217Optional FOLLOWERS arguments dictate exposure for succeeding siblings."
c567ac01
RS
5218
5219 (interactive "xExposure spec: ")
0458fa80
EZ
5220 (let ((inhibit-field-text-motion t)
5221 (depth (allout-current-depth))
c567ac01
RS
5222 max-pos)
5223 (cond ((null spec) nil)
5224 ((symbolp spec)
fd5359c6
MR
5225 (if (eq spec '*) (allout-show-current-subtree))
5226 (if (eq spec '+) (allout-show-current-branches))
5227 (if (eq spec '-) (allout-show-current-entry)))
c567ac01
RS
5228 ((numberp spec)
5229 (if (>= 0 spec)
fd5359c6 5230 (save-excursion (allout-hide-current-subtree t)
c567ac01
RS
5231 (end-of-line)
5232 (if (or (not max-pos)
5233 (> (point) max-pos))
5234 (setq max-pos (point)))
5235 (if (> 0 spec)
5236 (setq spec (* -1 spec)))))
5237 (if (> spec 0)
fd5359c6 5238 (allout-show-children spec)))
c567ac01 5239 ((listp spec)
fd5359c6 5240 ;(let ((got (allout-old-expose-topic (car spec))))
c567ac01
RS
5241 ; (if (and got (or (not max-pos) (> got max-pos)))
5242 ; (setq max-pos got)))
fd5359c6 5243 (let ((new-depth (+ (allout-current-depth) 1))
c567ac01 5244 got)
fd5359c6 5245 (setq max-pos (allout-old-expose-topic (car spec)))
c567ac01
RS
5246 (setq spec (cdr spec))
5247 (if (and spec
fd5359c6
MR
5248 (allout-descend-to-depth new-depth)
5249 (not (allout-hidden-p)))
5250 (progn (setq got (apply 'allout-old-expose-topic spec))
c567ac01
RS
5251 (if (and got (or (not max-pos) (> got max-pos)))
5252 (setq max-pos got)))))))
5253 (while (and followers
5254 (progn (if (and max-pos (< (point) max-pos))
5255 (progn (goto-char max-pos)
5256 (setq max-pos nil)))
5257 (end-of-line)
fd5359c6
MR
5258 (allout-next-sibling depth)))
5259 (allout-old-expose-topic (car followers))
c567ac01
RS
5260 (setq followers (cdr followers)))
5261 max-pos))
fd5359c6
MR
5262;;;_ > allout-new-exposure '()
5263(defmacro allout-new-exposure (&rest spec)
5264 "Literal frontend for `allout-expose-topic', doesn't evaluate arguments.
2265e017
MR
5265Some arguments that would need to be quoted in `allout-expose-topic'
5266need not be quoted in `allout-new-exposure'.
c567ac01
RS
5267
5268Cursor is left at start position.
5269
d82979ea
EZ
5270Use this instead of obsolete `allout-exposure'.
5271
c567ac01 5272Examples:
bff05d9e 5273\(allout-new-exposure (-1 () () () 1) 0)
c567ac01
RS
5274 Close current topic at current level so only the immediate
5275 subtopics are shown, except also show the children of the
5276 third subtopic; and close the next topic at the current level.
bff05d9e 5277\(allout-new-exposure : -1 0)
c567ac01
RS
5278 Close all topics at current level to expose only their
5279 immediate children, except for the last topic at the current
353e2ef2 5280 level, in which even its immediate children are hidden.
bff05d9e 5281\(allout-new-exposure -2 : -1 *)
c567ac01
RS
5282 Expose children and grandchildren of first topic at current
5283 level, and expose children of subsequent topics at current
5284 level *except* for the last, which should be opened completely."
5285 (list 'save-excursion
d8350998 5286 '(if (not (or (allout-goto-prefix-doublechecked)
fd5359c6
MR
5287 (allout-next-heading)))
5288 (error "allout-new-exposure: Can't find any outline topics"))
5289 (list 'allout-expose-topic (list 'quote spec))))
c567ac01 5290
60c7bbd2 5291;;;_ #7 Systematic outline presentation -- copying, printing, flattening
19b84ba3 5292
9179616f
DL
5293;;;_ - Mapping and processing of topics
5294;;;_ ( See also Subtree Charting, in Navigation code.)
fd5359c6
MR
5295;;;_ > allout-stringify-flat-index (flat-index)
5296(defun allout-stringify-flat-index (flat-index &optional context)
9179616f
DL
5297 "Convert list representing section/subsection/... to document string.
5298
5299Optional arg CONTEXT indicates interior levels to include."
5300 (let ((delim ".")
71296446 5301 result
9179616f
DL
5302 numstr
5303 (context-depth (or (and context 2) 1)))
5304 ;; Take care of the explicit context:
5305 (while (> context-depth 0)
5306 (setq numstr (int-to-string (car flat-index))
5307 flat-index (cdr flat-index)
5308 result (if flat-index
5309 (cons delim (cons numstr result))
5310 (cons numstr result))
5311 context-depth (if flat-index (1- context-depth) 0)))
5312 (setq delim " ")
5313 ;; Take care of the indentation:
5314 (if flat-index
5315 (progn
5316 (while flat-index
5317 (setq result
5318 (cons delim
5319 (cons (make-string
5320 (1+ (truncate (if (zerop (car flat-index))
5321 1
5322 (log10 (car flat-index)))))
5323 ? )
5324 result)))
5325 (setq flat-index (cdr flat-index)))
5326 ;; Dispose of single extra delim:
5327 (setq result (cdr result))))
5328 (apply 'concat result)))
fd5359c6
MR
5329;;;_ > allout-stringify-flat-index-plain (flat-index)
5330(defun allout-stringify-flat-index-plain (flat-index)
9179616f
DL
5331 "Convert list representing section/subsection/... to document string."
5332 (let ((delim ".")
5333 result)
5334 (while flat-index
5335 (setq result (cons (int-to-string (car flat-index))
5336 (if result
5337 (cons delim result))))
5338 (setq flat-index (cdr flat-index)))
5339 (apply 'concat result)))
fd5359c6
MR
5340;;;_ > allout-stringify-flat-index-indented (flat-index)
5341(defun allout-stringify-flat-index-indented (flat-index)
9179616f
DL
5342 "Convert list representing section/subsection/... to document string."
5343 (let ((delim ".")
71296446 5344 result
9179616f
DL
5345 numstr)
5346 ;; Take care of the explicit context:
5347 (setq numstr (int-to-string (car flat-index))
5348 flat-index (cdr flat-index)
5349 result (if flat-index
5350 (cons delim (cons numstr result))
5351 (cons numstr result)))
5352 (setq delim " ")
5353 ;; Take care of the indentation:
5354 (if flat-index
5355 (progn
5356 (while flat-index
5357 (setq result
5358 (cons delim
5359 (cons (make-string
5360 (1+ (truncate (if (zerop (car flat-index))
5361 1
5362 (log10 (car flat-index)))))
5363 ? )
5364 result)))
5365 (setq flat-index (cdr flat-index)))
5366 ;; Dispose of single extra delim:
5367 (setq result (cdr result))))
5368 (apply 'concat result)))
fd5359c6
MR
5369;;;_ > allout-listify-exposed (&optional start end format)
5370(defun allout-listify-exposed (&optional start end format)
19b84ba3 5371
9179616f 5372 "Produce a list representing exposed topics in current region.
c567ac01 5373
fd5359c6 5374This list can then be used by `allout-process-exposed' to manipulate
9179616f 5375the subject region.
353e2ef2 5376
9179616f 5377Optional START and END indicate bounds of region.
19b84ba3 5378
75bcb912 5379Optional arg, FORMAT, designates an alternate presentation form for
9179616f 5380the prefix:
c567ac01 5381
60c7bbd2 5382 list -- Present prefix as numeric section.subsection..., starting with
9179616f 5383 section indicated by the list, innermost nesting first.
60c7bbd2 5384 `indent' (symbol) -- Convert header prefixes to all white space,
9179616f 5385 except for distinctive bullets.
c567ac01 5386
9179616f
DL
5387The elements of the list produced are lists that represents a topic
5388header and body. The elements of that list are:
c567ac01 5389
9179616f
DL
5390 - a number representing the depth of the topic,
5391 - a string representing the header-prefix, including trailing whitespace and
5392 bullet.
5393 - a string representing the bullet character,
5394 - and a series of strings, each containing one line of the exposed
5395 portion of the topic entry."
c567ac01 5396
9179616f
DL
5397 (interactive "r")
5398 (save-excursion
5399 (let*
0458fa80
EZ
5400 ((inhibit-field-text-motion t)
5401 ;; state vars:
5402 strings prefix result depth new-depth out gone-out bullet beg
9179616f 5403 next done)
c567ac01 5404
9179616f
DL
5405 (goto-char start)
5406 (beginning-of-line)
97610156 5407 ;; Goto initial topic, and register preceding stuff, if any:
d8350998 5408 (if (> (allout-goto-prefix-doublechecked) start)
60c7bbd2 5409 ;; First topic follows beginning point -- register preliminary stuff:
3e2ee889
KM
5410 (setq result
5411 (list (list 0 "" nil
5412 (buffer-substring-no-properties start
5413 (1- (point)))))))
9179616f
DL
5414 (while (and (not done)
5415 (not (eobp)) ; Loop until we've covered the region.
5416 (not (> (point) end)))
d8350998 5417 (setq depth allout-recent-depth ; Current topics depth,
fd5359c6
MR
5418 bullet (allout-recent-bullet) ; ... bullet,
5419 prefix (allout-recent-prefix)
5420 beg (progn (allout-end-of-prefix t) (point))) ; and beginning.
9179616f 5421 (setq done ; The boundary for the current topic:
fd5359c6 5422 (not (allout-next-visible-heading 1)))
d8350998 5423 (setq new-depth allout-recent-depth)
9179616f
DL
5424 (setq gone-out out
5425 out (< new-depth depth))
5426 (beginning-of-line)
5427 (setq next (point))
5428 (goto-char beg)
5429 (setq strings nil)
5430 (while (> next (point)) ; Get all the exposed text in
5431 (setq strings
3e2ee889 5432 (cons (buffer-substring-no-properties
9179616f
DL
5433 beg
5434 ;To hidden text or end of line:
5435 (progn
96b83743
EZ
5436 (end-of-line)
5437 (allout-back-to-visible-text)))
9179616f 5438 strings))
96b83743 5439 (when (< (point) next) ; Resume from after hid text, if any.
1b4d97b2
CY
5440 (line-move 1)
5441 (beginning-of-line))
9179616f
DL
5442 (setq beg (point)))
5443 ;; Accumulate list for this topic:
5444 (setq strings (nreverse strings))
5445 (setq result
5446 (cons
5447 (if format
5448 (let ((special (if (string-match
5449 (regexp-quote bullet)
fd5359c6 5450 allout-distinctive-bullets-string)
9179616f
DL
5451 bullet)))
5452 (cond ((listp format)
5453 (list depth
96497653 5454 (if allout-flattened-numbering-abbreviation
fd5359c6 5455 (allout-stringify-flat-index format
9179616f 5456 gone-out)
fd5359c6 5457 (allout-stringify-flat-index-plain
9179616f
DL
5458 format))
5459 strings
5460 special))
5461 ((eq format 'indent)
5462 (if special
5463 (list depth
5464 (concat (make-string (1+ depth) ? )
5465 (substring prefix -1))
5466 strings)
5467 (list depth
5468 (make-string depth ? )
5469 strings)))
fd5359c6 5470 (t (error "allout-listify-exposed: %s %s"
9179616f
DL
5471 "invalid format" format))))
5472 (list depth prefix strings))
5473 result))
5474 ;; Reasses format, if any:
5475 (if (and format (listp format))
5476 (cond ((= new-depth depth)
5477 (setq format (cons (1+ (car format))
5478 (cdr format))))
60c7bbd2 5479 ((> new-depth depth) ; descending -- assume by 1:
9179616f
DL
5480 (setq format (cons 1 format)))
5481 (t
5482 ; Pop the residue:
5483 (while (< new-depth depth)
5484 (setq format (cdr format))
5485 (setq depth (1- depth)))
5486 ; And increment the current one:
5487 (setq format
5488 (cons (1+ (or (car format)
5489 -1))
5490 (cdr format)))))))
5491 ;; Put the list with first at front, to last at back:
5492 (nreverse result))))
c60d543d
RS
5493;;;_ > allout-region-active-p ()
5494(defmacro allout-region-active-p ()
42c3e51c
KM
5495 (cond ((fboundp 'use-region-p) '(use-region-p))
5496 ((fboundp 'region-active-p) '(region-active-p))
5497 (t 'mark-active)))
5498;;_ > allout-process-exposed (&optional func from to frombuf
9179616f 5499;;; tobuf format)
fd5359c6 5500(defun allout-process-exposed (&optional func from to frombuf tobuf
96b83743 5501 format start-num)
9179616f 5502 "Map function on exposed parts of current topic; results to another buffer.
353e2ef2 5503
d82979ea
EZ
5504All args are options; default values itemized below.
5505
5506Apply FUNCTION to exposed portions FROM position TO position in buffer
9179616f
DL
5507FROMBUF to buffer TOBUF. Sixth optional arg, FORMAT, designates an
5508alternate presentation form:
c567ac01 5509
60c7bbd2 5510 `flat' -- Present prefix as numeric section.subsection..., starting with
75bcb912 5511 section indicated by the START-NUM, innermost nesting first.
60c7bbd2 5512 X`flat-indented' -- Prefix is like `flat' for first topic at each
9179616f
DL
5513 X level, but subsequent topics have only leaf topic
5514 X number, padded with blanks to line up with first.
60c7bbd2 5515 `indent' (symbol) -- Convert header prefixes to all white space,
9179616f 5516 except for distinctive bullets.
c567ac01 5517
9179616f 5518Defaults:
d82979ea 5519 FUNCTION: `allout-insert-listified'
9179616f
DL
5520 FROM: region start, if region active, else start of buffer
5521 TO: region end, if region active, else end of buffer
5522 FROMBUF: current buffer
5523 TOBUF: buffer name derived: \"*current-buffer-name exposed*\"
5524 FORMAT: nil"
5525
5526 ; Resolve arguments,
5527 ; defaulting if necessary:
fd5359c6 5528 (if (not func) (setq func 'allout-insert-listified))
9179616f 5529 (if (not (and from to))
c60d543d 5530 (if (allout-region-active-p)
9179616f
DL
5531 (setq from (region-beginning) to (region-end))
5532 (setq from (point-min) to (point-max))))
5533 (if frombuf
5534 (if (not (bufferp frombuf))
60c7bbd2 5535 ;; Specified but not a buffer -- get it:
9179616f
DL
5536 (let ((got (get-buffer frombuf)))
5537 (if (not got)
fd5359c6 5538 (error (concat "allout-process-exposed: source buffer "
9179616f
DL
5539 frombuf
5540 " not found."))
5541 (setq frombuf got))))
60c7bbd2 5542 ;; not specified -- default it:
9179616f
DL
5543 (setq frombuf (current-buffer)))
5544 (if tobuf
5545 (if (not (bufferp tobuf))
5546 (setq tobuf (get-buffer-create tobuf)))
60c7bbd2 5547 ;; not specified -- default it:
9179616f
DL
5548 (setq tobuf (concat "*" (buffer-name frombuf) " exposed*")))
5549 (if (listp format)
5550 (nreverse format))
5551
5552 (let* ((listified
5553 (progn (set-buffer frombuf)
fd5359c6 5554 (allout-listify-exposed from to format))))
9179616f 5555 (set-buffer tobuf)
5110e166 5556 (mapc func listified)
9179616f
DL
5557 (pop-to-buffer tobuf)))
5558
5559;;;_ - Copy exposed
fd5359c6
MR
5560;;;_ > allout-insert-listified (listified)
5561(defun allout-insert-listified (listified)
9179616f
DL
5562 "Insert contents of listified outline portion in current buffer.
5563
e126900f 5564LISTIFIED is a list representing each topic header and body:
9179616f
DL
5565
5566 \`(depth prefix text)'
5567
d82979ea 5568or \`(depth prefix text bullet-plus)'
9179616f
DL
5569
5570If `bullet-plus' is specified, it is inserted just after the entire prefix."
5571 (setq listified (cdr listified))
5572 (let ((prefix (prog1
5573 (car listified)
5574 (setq listified (cdr listified))))
5575 (text (prog1
5576 (car listified)
5577 (setq listified (cdr listified))))
5578 (bullet-plus (car listified)))
eac9cf5f
PJ
5579 (insert prefix)
5580 (if bullet-plus (insert (concat " " bullet-plus)))
9179616f 5581 (while text
eac9cf5f 5582 (insert (car text))
9179616f 5583 (if (setq text (cdr text))
0949617b 5584 (insert "\n")))
eac9cf5f 5585 (insert "\n")))
fd5359c6
MR
5586;;;_ > allout-copy-exposed-to-buffer (&optional arg tobuf format)
5587(defun allout-copy-exposed-to-buffer (&optional arg tobuf format)
9179616f
DL
5588 "Duplicate exposed portions of current outline to another buffer.
5589
5590Other buffer has current buffers name with \" exposed\" appended to it.
5591
5592With repeat count, copy the exposed parts of only the current topic.
5593
5594Optional second arg TOBUF is target buffer name.
5595
5596Optional third arg FORMAT, if non-nil, symbolically designates an
5597alternate presentation format for the outline:
5598
5599 `flat' - Convert topic header prefixes to numeric
5600 section.subsection... identifiers.
5601 `indent' - Convert header prefixes to all white space, except for
5602 distinctive bullets.
5603 `indent-flat' - The best of both - only the first of each level has
5604 the full path, the rest have only the section number
5605 of the leaf, preceded by the right amount of indentation."
c567ac01
RS
5606
5607 (interactive "P")
5608 (if (not tobuf)
5609 (setq tobuf (get-buffer-create (concat "*" (buffer-name) " exposed*"))))
5610 (let* ((start-pt (point))
fd5359c6
MR
5611 (beg (if arg (allout-back-to-current-heading) (point-min)))
5612 (end (if arg (allout-end-of-current-subtree) (point-max)))
9179616f
DL
5613 (buf (current-buffer))
5614 (start-list ()))
5615 (if (eq format 'flat)
5616 (setq format (if arg (save-excursion
5617 (goto-char beg)
fd5359c6 5618 (allout-topic-flat-index))
9179616f 5619 '(1))))
7fdbcd83 5620 (with-current-buffer tobuf (erase-buffer))
fd5359c6 5621 (allout-process-exposed 'allout-insert-listified
c567ac01
RS
5622 beg
5623 end
5624 (current-buffer)
9179616f
DL
5625 tobuf
5626 format start-list)
c567ac01
RS
5627 (goto-char (point-min))
5628 (pop-to-buffer buf)
5629 (goto-char start-pt)))
fd5359c6
MR
5630;;;_ > allout-flatten-exposed-to-buffer (&optional arg tobuf)
5631(defun allout-flatten-exposed-to-buffer (&optional arg tobuf)
9179616f
DL
5632 "Present numeric outline of outline's exposed portions in another buffer.
5633
60c7bbd2 5634The resulting outline is not compatible with outline mode -- use
fd5359c6 5635`allout-copy-exposed-to-buffer' if you want that.
9179616f 5636
fd5359c6 5637Use `allout-indented-exposed-to-buffer' for indented presentation.
9179616f
DL
5638
5639With repeat count, copy the exposed portions of only current topic.
5640
e126900f 5641Other buffer has current buffer's name with \" exposed\" appended to
9179616f
DL
5642it, unless optional second arg TOBUF is specified, in which case it is
5643used verbatim."
5644 (interactive "P")
fd5359c6
MR
5645 (allout-copy-exposed-to-buffer arg tobuf 'flat))
5646;;;_ > allout-indented-exposed-to-buffer (&optional arg tobuf)
5647(defun allout-indented-exposed-to-buffer (&optional arg tobuf)
9179616f
DL
5648 "Present indented outline of outline's exposed portions in another buffer.
5649
60c7bbd2 5650The resulting outline is not compatible with outline mode -- use
fd5359c6 5651`allout-copy-exposed-to-buffer' if you want that.
9179616f 5652
fd5359c6 5653Use `allout-flatten-exposed-to-buffer' for numeric sectional presentation.
9179616f
DL
5654
5655With repeat count, copy the exposed portions of only current topic.
5656
e126900f 5657Other buffer has current buffer's name with \" exposed\" appended to
9179616f
DL
5658it, unless optional second arg TOBUF is specified, in which case it is
5659used verbatim."
5660 (interactive "P")
fd5359c6 5661 (allout-copy-exposed-to-buffer arg tobuf 'indent))
c567ac01
RS
5662
5663;;;_ - LaTeX formatting
e126900f
JB
5664;;;_ > allout-latex-verb-quote (string &optional flow)
5665(defun allout-latex-verb-quote (string &optional flow)
539d7736 5666 "Return copy of STRING for literal reproduction across LaTeX processing.
c07583cd 5667Expresses the original characters (including carriage returns) of the
539d7736 5668string across LaTeX processing."
9179616f
DL
5669 (mapconcat (function
5670 (lambda (char)
c567ac01
RS
5671 (cond ((memq char '(?\\ ?$ ?% ?# ?& ?{ ?} ?_ ?^ ?- ?*))
5672 (concat "\\char" (number-to-string char) "{}"))
5673 ((= char ?\n) "\\\\")
9179616f 5674 (t (char-to-string char)))))
e126900f 5675 string
c567ac01 5676 ""))
fd5359c6
MR
5677;;;_ > allout-latex-verbatim-quote-curr-line ()
5678(defun allout-latex-verbatim-quote-curr-line ()
c07583cd 5679 "Express line for exact (literal) representation across LaTeX processing.
19b84ba3 5680
c07583cd 5681Adjust line contents so it is unaltered (from the original line)
539d7736 5682across LaTeX processing, within the context of a `verbatim'
c567ac01 5683environment. Leaves point at the end of the line."
0458fa80
EZ
5684 (let ((inhibit-field-text-motion t))
5685 (beginning-of-line)
5686 (let ((beg (point))
626b459f 5687 (end (point-at-eol)))
2a1408fd
DN
5688 (save-match-data
5689 (while (re-search-forward "\\\\"
5690 ;;"\\\\\\|\\{\\|\\}\\|\\_\\|\\$\\|\\\"\\|\\&\\|\\^\\|\\-\\|\\*\\|#"
5691 end ; bounded by end-of-line
5692 1) ; no matches, move to end & return nil
5693 (goto-char (match-beginning 2))
5694 (insert "\\")
5695 (setq end (1+ end))
5696 (goto-char (1+ (match-end 2))))))))
e126900f
JB
5697;;;_ > allout-insert-latex-header (buffer)
5698(defun allout-insert-latex-header (buffer)
539d7736 5699 "Insert initial LaTeX commands at point in BUFFER."
c567ac01
RS
5700 ;; Much of this is being derived from the stuff in appendix of E in
5701 ;; the TeXBook, pg 421.
e126900f 5702 (set-buffer buffer)
c567ac01
RS
5703 (let ((doc-style (format "\n\\documentstyle{%s}\n"
5704 "report"))
fd5359c6 5705 (page-numbering (if allout-number-pages
c567ac01
RS
5706 "\\pagestyle{empty}\n"
5707 ""))
c567ac01 5708 (titlecmd (format "\\newcommand{\\titlecmd}[1]{{%s #1}}\n"
fd5359c6 5709 allout-title-style))
c567ac01 5710 (labelcmd (format "\\newcommand{\\labelcmd}[1]{{%s #1}}\n"
fd5359c6 5711 allout-label-style))
c567ac01 5712 (headlinecmd (format "\\newcommand{\\headlinecmd}[1]{{%s #1}}\n"
fd5359c6 5713 allout-head-line-style))
c567ac01 5714 (bodylinecmd (format "\\newcommand{\\bodylinecmd}[1]{{%s #1}}\n"
fd5359c6 5715 allout-body-line-style))
c567ac01
RS
5716 (setlength (format "%s%s%s%s"
5717 "\\newlength{\\stepsize}\n"
5718 "\\setlength{\\stepsize}{"
fd5359c6 5719 allout-indent
c567ac01
RS
5720 "}\n"))
5721 (oneheadline (format "%s%s%s%s%s%s%s"
5722 "\\newcommand{\\OneHeadLine}[3]{%\n"
5723 "\\noindent%\n"
5724 "\\hspace*{#2\\stepsize}%\n"
5725 "\\labelcmd{#1}\\hspace*{.2cm}"
5726 "\\headlinecmd{#3}\\\\["
fd5359c6 5727 allout-line-skip
c567ac01
RS
5728 "]\n}\n"))
5729 (onebodyline (format "%s%s%s%s%s%s"
5730 "\\newcommand{\\OneBodyLine}[2]{%\n"
5731 "\\noindent%\n"
5732 "\\hspace*{#1\\stepsize}%\n"
5733 "\\bodylinecmd{#2}\\\\["
fd5359c6 5734 allout-line-skip
c567ac01
RS
5735 "]\n}\n"))
5736 (begindoc "\\begin{document}\n\\begin{center}\n")
5737 (title (format "%s%s%s%s"
5738 "\\titlecmd{"
fd5359c6 5739 (allout-latex-verb-quote (if allout-title
96b83743 5740 (condition-case nil
fd5359c6 5741 (eval allout-title)
d8350998 5742 (error "<unnamed buffer>"))
c567ac01
RS
5743 "Unnamed Outline"))
5744 "}\n"
5745 "\\end{center}\n\n"))
5746 (hsize "\\hsize = 7.5 true in\n")
5747 (hoffset "\\hoffset = -1.5 true in\n")
5748 (vspace "\\vspace{.1cm}\n\n"))
5749 (insert (concat doc-style
5750 page-numbering
5751 titlecmd
5752 labelcmd
5753 headlinecmd
5754 bodylinecmd
5755 setlength
5756 oneheadline
5757 onebodyline
5758 begindoc
5759 title
5760 hsize
5761 hoffset
5762 vspace)
5763 )))
e126900f
JB
5764;;;_ > allout-insert-latex-trailer (buffer)
5765(defun allout-insert-latex-trailer (buffer)
539d7736 5766 "Insert concluding LaTeX commands at point in BUFFER."
e126900f 5767 (set-buffer buffer)
c567ac01 5768 (insert "\n\\end{document}\n"))
fd5359c6
MR
5769;;;_ > allout-latexify-one-item (depth prefix bullet text)
5770(defun allout-latexify-one-item (depth prefix bullet text)
19b84ba3
RS
5771 "Insert LaTeX commands for formatting one outline item.
5772
9179616f 5773Args are the topics numeric DEPTH, the header PREFIX lead string, the
19b84ba3 5774BULLET string, and a list of TEXT strings for the body."
c567ac01
RS
5775 (let* ((head-line (if text (car text)))
5776 (body-lines (cdr text))
5777 (curr-line)
5778 body-content bop)
5779 ; Do the head line:
71296446 5780 (insert (concat "\\OneHeadLine{\\verb\1 "
d82979ea
EZ
5781 (allout-latex-verb-quote bullet)
5782 "\1}{"
5783 depth
5784 "}{\\verb\1 "
5785 (if head-line
5786 (allout-latex-verb-quote head-line)
5787 "")
5788 "\1}\n"))
c567ac01
RS
5789 (if (not body-lines)
5790 nil
eac9cf5f
PJ
5791 ;;(insert "\\beginlines\n")
5792 (insert "\\begin{verbatim}\n")
c567ac01
RS
5793 (while body-lines
5794 (setq curr-line (car body-lines))
5795 (if (and (not body-content)
5796 (not (string-match "^\\s-*$" curr-line)))
5797 (setq body-content t))
5798 ; Mangle any occurrences of
5799 ; "\end{verbatim}" in text,
5800 ; it's special:
5801 (if (and body-content
5802 (setq bop (string-match "\\end{verbatim}" curr-line)))
5803 (setq curr-line (concat (substring curr-line 0 bop)
5804 ">"
5805 (substring curr-line bop))))
eac9cf5f
PJ
5806 ;;(insert "|" (car body-lines) "|")
5807 (insert curr-line)
fd5359c6 5808 (allout-latex-verbatim-quote-curr-line)
eac9cf5f 5809 (insert "\n")
c567ac01
RS
5810 (setq body-lines (cdr body-lines)))
5811 (if body-content
5812 (setq body-content nil)
5813 (forward-char -1)
eac9cf5f 5814 (insert "\\ ")
c567ac01 5815 (forward-char 1))
eac9cf5f
PJ
5816 ;;(insert "\\endlines\n")
5817 (insert "\\end{verbatim}\n")
c567ac01 5818 )))
fd5359c6
MR
5819;;;_ > allout-latexify-exposed (arg &optional tobuf)
5820(defun allout-latexify-exposed (arg &optional tobuf)
539d7736 5821 "Format current topics exposed portions to TOBUF for LaTeX processing.
19b84ba3
RS
5822TOBUF defaults to a buffer named the same as the current buffer, but
5823with \"*\" prepended and \" latex-formed*\" appended.
c567ac01
RS
5824
5825With repeat count, copy the exposed portions of entire buffer."
5826
5827 (interactive "P")
5828 (if (not tobuf)
5829 (setq tobuf
5830 (get-buffer-create (concat "*" (buffer-name) " latexified*"))))
5831 (let* ((start-pt (point))
fd5359c6
MR
5832 (beg (if arg (point-min) (allout-back-to-current-heading)))
5833 (end (if arg (point-max) (allout-end-of-current-subtree)))
c567ac01
RS
5834 (buf (current-buffer)))
5835 (set-buffer tobuf)
1977b8f6 5836 (erase-buffer)
fd5359c6 5837 (allout-insert-latex-header tobuf)
c567ac01 5838 (goto-char (point-max))
fd5359c6 5839 (allout-process-exposed 'allout-latexify-one-item
c567ac01
RS
5840 beg
5841 end
5842 buf
5843 tobuf)
5844 (goto-char (point-max))
fd5359c6 5845 (allout-insert-latex-trailer tobuf)
1977b8f6 5846 (goto-char (point-min))
c567ac01
RS
5847 (pop-to-buffer buf)
5848 (goto-char start-pt)))
5849
d82979ea 5850;;;_ #8 Encryption
29fac3fe
KM
5851;;;_ > allout-toggle-current-subtree-encryption (&optional keymode-cue)
5852(defun allout-toggle-current-subtree-encryption (&optional keymode-cue)
5853 "Encrypt clear or decrypt encoded topic text.
5854
5855Allout uses emacs 'epg' libary to perform encryption. Symmetric
5856and keypair encryption are supported. All encryption is ascii
5857armored.
5858
075746e2
KM
5859Entry encryption defaults to symmetric key mode unless keypair
5860recipients are associated with the file \(see
5861`epa-file-encrypt-to') or the function is invoked with a
5862\(KEYMODE-CUE) universal argument greater than 1.
5863
5864When encrypting, KEYMODE-CUE universal argument greater than 1
5865causes prompting for recipients for public-key keypair
5866encryption. Selecting no recipients results in symmetric key
29fac3fe
KM
5867encryption.
5868
075746e2
KM
5869Further, encrypting with a KEYMODE-CUE universal argument greater
5870than 4 - eg, preceded by a doubled Ctrl-U - causes association of
5871the specified recipients with the file, replacing those currently
5872associated with it. This can be used to deassociate any
5873recipients with the file, by selecting no recipients in the
5874dialog.
5875
29fac3fe
KM
5876Encrypted topic's bullets are set to a `~' to signal that the
5877contents of the topic (body and subtopics, but not heading) is
5878pending encryption or encrypted. `*' asterisk immediately after
5879the bullet signals that the body is encrypted, its absence means
5880the topic is meant to be encrypted but is not currently. When a
5881file with topics pending encryption is saved, topics pending
5882encryption are encrypted. See allout-encrypt-unencrypted-on-saves
5883for auto-encryption specifics.
0949617b 5884
6eb0a10d 5885\*NOTE WELL* that automatic encryption that happens during saves will
60c7bbd2 5886default to symmetric encryption -- you must deliberately (re)encrypt key-pair
0949617b 5887encrypted topics if you want them to continue to use the key-pair cipher.
d82979ea 5888
d8350998 5889Level-one topics, with prefix consisting solely of an `*' asterisk, cannot be
d82979ea 5890encrypted. If you want to encrypt the contents of a top-level topic, use
29fac3fe 5891\\[allout-shift-in] to increase its depth."
69027dea
EZ
5892 (interactive "P")
5893 (save-excursion
5894 (allout-back-to-current-heading)
29fac3fe
KM
5895 (allout-toggle-subtree-encryption keymode-cue)))
5896;;;_ > allout-toggle-subtree-encryption (&optional keymode-cue)
5897(defun allout-toggle-subtree-encryption (&optional keymode-cue)
c07583cd 5898 "Encrypt clear text or decrypt encoded topic contents (body and subtopics.)
69027dea 5899
075746e2
KM
5900Entry encryption defaults to symmetric key mode unless keypair
5901recipients are associated with the file \(see
5902`epa-file-encrypt-to') or the function is invoked with a
5903\(KEYMODE-CUE) universal argument greater than 1.
69027dea 5904
075746e2
KM
5905When encrypting, KEYMODE-CUE universal argument greater than 1
5906causes prompting for recipients for public-key keypair
5907encryption. Selecting no recipients results in symmetric key
5908encryption.
69027dea 5909
075746e2
KM
5910Further, encrypting with a KEYMODE-CUE universal argument greater
5911than 4 - eg, preceded by a doubled Ctrl-U - causes association of
5912the specified recipients with the file, replacing those currently
5913associated with it. This can be used to deassociate any
5914recipients with the file, by selecting no recipients in the
5915dialog.
5916
5917Encryption and decryption uses the emacs epg library.
69027dea 5918
29fac3fe 5919Encrypted text will be ascii-armored.
69027dea
EZ
5920
5921See `allout-toggle-current-subtree-encryption' for more details."
d82979ea
EZ
5922
5923 (interactive "P")
5924 (save-excursion
5925 (allout-end-of-prefix t)
5926
d8350998 5927 (if (= allout-recent-depth 1)
d82979ea
EZ
5928 (error (concat "Cannot encrypt or decrypt level 1 topics -"
5929 " shift it in to make it encryptable")))
5930
d82979ea 5931 (let* ((allout-buffer (current-buffer))
13aa217b
KM
5932 ;; for use with allout-auto-save-temporarily-disabled, if necessary:
5933 (was-buffer-saved-size buffer-saved-size)
498a6493 5934 ;; Assess location:
d8350998 5935 (bullet-pos allout-recent-prefix-beginning)
d82979ea
EZ
5936 (after-bullet-pos (point))
5937 (was-encrypted
5938 (progn (if (= (point-max) after-bullet-pos)
5939 (error "no body to encrypt"))
0949617b 5940 (allout-encrypted-topic-p)))
96b83743 5941 (was-collapsed (if (not (search-forward "\n" nil t))
d82979ea
EZ
5942 nil
5943 (backward-char 1)
96b83743 5944 (allout-hidden-p)))
d82979ea
EZ
5945 (subtree-beg (1+ (point)))
5946 (subtree-end (allout-end-of-subtree))
5947 (subject-text (buffer-substring-no-properties subtree-beg
5948 subtree-end))
5949 (subtree-end-char (char-after (1- subtree-end)))
96b83743 5950 (subtree-trailing-char (char-after subtree-end))
60c7bbd2 5951 ;; kluge -- result-text needs to be nil, but we also want to
96b83743
EZ
5952 ;; check for the error condition
5953 (result-text (if (or (string= "" subject-text)
5954 (string= "\n" subject-text))
5955 (error "No topic contents to %scrypt"
5956 (if was-encrypted "de" "en"))
5957 nil))
d82979ea 5958 ;; Assess key parameters:
498a6493
CY
5959 (was-coding-system buffer-file-coding-system))
5960
5961 (when (not was-encrypted)
5962 ;; ensure that non-ascii chars pending encryption are noticed before
116f996b 5963 ;; they're encrypted, so the coding system is set to accommodate
498a6493
CY
5964 ;; them.
5965 (setq buffer-file-coding-system
1c9b9df0 5966 (allout-select-safe-coding-system subtree-beg subtree-end))
498a6493
CY
5967 ;; if the coding system for the text being encrypted is different
5968 ;; than that prevailing, then there a real risk that the coding
5969 ;; system can't be noticed by emacs when the file is visited. to
5970 ;; mitigate that, offer to preserve the coding system using a file
5971 ;; local variable.
5972 (if (and (not (equal buffer-file-coding-system
5973 was-coding-system))
5974 (yes-or-no-p
5975 (format (concat "Register coding system %s as file local"
5976 " var? Necessary when only encrypted text"
5977 " is in that coding system. ")
5978 buffer-file-coding-system)))
5979 (allout-adjust-file-variable "buffer-file-coding-system"
5980 buffer-file-coding-system)))
d82979ea
EZ
5981
5982 (setq result-text
5983 (allout-encrypt-string subject-text was-encrypted
075746e2 5984 (current-buffer) keymode-cue))
d82979ea
EZ
5985
5986 ;; Replace the subtree with the processed product.
5987 (allout-unprotected
5988 (progn
5989 (set-buffer allout-buffer)
5990 (delete-region subtree-beg subtree-end)
5991 (insert result-text)
5992 (if was-collapsed
96b83743
EZ
5993 (allout-flag-region (1- subtree-beg) (point) t))
5994 ;; adjust trailing-blank-lines to preserve topic spacing:
d82979ea 5995 (if (not was-encrypted)
96b83743
EZ
5996 (if (and (= subtree-end-char ?\n)
5997 (= subtree-trailing-char ?\n))
5998 (insert subtree-trailing-char)))
d82979ea
EZ
5999 ;; Ensure that the item has an encrypted-entry bullet:
6000 (if (not (string= (buffer-substring-no-properties
6001 (1- after-bullet-pos) after-bullet-pos)
6002 allout-topic-encryption-bullet))
6003 (progn (goto-char (1- after-bullet-pos))
6004 (delete-char 1)
6005 (insert allout-topic-encryption-bullet)))
6006 (if was-encrypted
6007 ;; Remove the is-encrypted bullet qualifier:
6008 (progn (goto-char after-bullet-pos)
6009 (delete-char 1))
6010 ;; Add the is-encrypted bullet qualifier:
6011 (goto-char after-bullet-pos)
d8350998 6012 (insert "*"))))
13aa217b
KM
6013
6014 ;; adjust buffer's auto-save eligibility:
6015 (if was-encrypted
6016 (allout-inhibit-auto-save-info-for-decryption was-buffer-saved-size)
6017 (allout-maybe-resume-auto-save-info-after-encryption))
6018
1b4d97b2
CY
6019 (run-hook-with-args 'allout-structure-added-hook
6020 bullet-pos subtree-end))))
075746e2 6021;;;_ > allout-encrypt-string (text decrypt allout-buffer keymode-cue
29fac3fe 6022;;; &optional rejected)
075746e2 6023(defun allout-encrypt-string (text decrypt allout-buffer keymode-cue
29fac3fe 6024 &optional rejected)
0949617b 6025 "Encrypt or decrypt message TEXT.
d82979ea 6026
ff3e8c8e 6027Returns the resulting string, or nil if the transformation fails.
d82979ea 6028
ff3e8c8e 6029If DECRYPT is true (default false), then decrypt instead of encrypt.
d82979ea 6030
ff3e8c8e 6031ALLOUT-BUFFER identifies the buffer containing the text.
d82979ea 6032
075746e2
KM
6033Entry encryption defaults to symmetric key mode unless keypair
6034recipients are associated with the file \(see
6035`epa-file-encrypt-to') or the function is invoked with a
6036\(KEYMODE-CUE) universal argument greater than 1.
d82979ea 6037
075746e2
KM
6038When encrypting, KEYMODE-CUE universal argument greater than 1
6039causes prompting for recipients for public-key keypair
6040encryption. Selecting no recipients results in symmetric key
6041encryption.
0949617b 6042
075746e2
KM
6043Further, encrypting with a KEYMODE-CUE universal argument greater
6044than 4 - eg, preceded by a doubled Ctrl-U - causes association of
6045the specified recipients with the file, replacing those currently
6046associated with it. This can be used to deassociate any
6047recipients with the file, by selecting no recipients in the
6048dialog.
d82979ea 6049
29fac3fe 6050Optional REJECTED is for internal use, to convey the number of
48bd8440
EZ
6051rejections due to matches against
6052`allout-encryption-ciphertext-rejection-regexps', as limited by
6053`allout-encryption-ciphertext-rejection-ceiling'.
6054
2576c28f
KM
6055NOTE: A few GnuPG v2 versions improperly preserve incorrect
6056symmetric decryption keys, preventing entry of the correct key on
6057subsequent decryption attempts until the cache times-out. That
6058can take several minutes. \(Decryption of other entries is not
6059affected.) Upgrade your EasyPG version, if you can, and you can
6060deliberately clear your gpg-agent's cache by sending it a '-HUP'
6061signal."
48bd8440 6062
ff3e8c8e 6063 (require 'epg)
29fac3fe 6064 (require 'epa)
0949617b 6065
075746e2
KM
6066 (let* ((epg-context (let* ((context (epg-make-context nil t)))
6067 (epg-context-set-passphrase-callback
6068 context #'epa-passphrase-callback-function)
6069 context))
13aa217b 6070
498a6493
CY
6071 (encoding (with-current-buffer allout-buffer
6072 buffer-file-coding-system))
6073 (multibyte (with-current-buffer allout-buffer
ff3e8c8e 6074 enable-multibyte-characters))
29fac3fe
KM
6075 ;; "sanitization" avoids encryption results that are outline structure.
6076 (sani-regexps 'allout-encryption-plaintext-sanitization-regexps)
6077 (strip-plaintext-regexps (if (not decrypt)
6078 (allout-get-configvar-values
6079 sani-regexps)))
6080 (rejection-regexps 'allout-encryption-ciphertext-rejection-regexps)
6081 (reject-ciphertext-regexps (if (not decrypt)
6082 (allout-get-configvar-values
6083 rejection-regexps)))
48bd8440
EZ
6084 (rejected (or rejected 0))
6085 (rejections-left (- allout-encryption-ciphertext-rejection-ceiling
6086 rejected))
075746e2
KM
6087 (keypair-mode (cond (decrypt 'decrypting)
6088 ((<= (prefix-numeric-value keymode-cue) 1)
6089 'default)
6090 ((<= (prefix-numeric-value keymode-cue) 4)
6091 'prompt)
6092 ((> (prefix-numeric-value keymode-cue) 4)
6093 'prompt-save)))
6094 (keypair-message (concat "Select encryption recipients.\n"
6095 "Symmetric encryption is done if no"
6096 " recipients are selected. "))
6097 (encrypt-to (and (boundp 'epa-file-encrypt-to) epa-file-encrypt-to))
29fac3fe
KM
6098 recipients
6099 massaged-text
6100 result-text
38b2ca53 6101 )
0949617b 6102
ff3e8c8e
KM
6103 ;; Massage the subject text for encoding and filtering.
6104 (with-temp-buffer
6105 (insert text)
6106 ;; convey the text characteristics of the original buffer:
96497653 6107 (set-buffer-multibyte multibyte)
ff3e8c8e
KM
6108 (when encoding
6109 (set-buffer-file-coding-system encoding)
6110 (if (not decrypt)
6111 (encode-coding-region (point-min) (point-max) encoding)))
6112
6113 ;; remove sanitization regexps matches before encrypting:
6114 (when (and strip-plaintext-regexps (not decrypt))
6115 (dolist (re strip-plaintext-regexps)
6116 (let ((re (if (listp re) (car re) re))
6117 (replacement (if (listp re) (cadr re) "")))
6118 (goto-char (point-min))
6119 (save-match-data
6120 (while (re-search-forward re nil t)
6121 (replace-match replacement nil nil))))))
6122 (setq massaged-text (buffer-substring-no-properties (point-min)
6123 (point-max))))
075746e2
KM
6124 ;; determine key mode and, if keypair, recipients:
6125 (setq recipients
6126 (case keypair-mode
6127
6128 (decrypting nil)
6129
6130 (default (if encrypt-to (epg-list-keys epg-context encrypt-to)))
6131
6132 ((prompt prompt-save)
6133 (save-window-excursion
6134 (epa-select-keys epg-context keypair-message)))))
6135
ff3e8c8e 6136 (setq result-text
ff3e8c8e 6137 (if decrypt
2a095cec
KM
6138 (condition-case err
6139 (epg-decrypt-string epg-context
6140 (encode-coding-string massaged-text
6141 (or encoding 'utf-8)))
6142 (epg-error
6143 (signal 'egp-error
6144 (cons (concat (cadr err) " - gpg version problem?")
6145 (cddr err)))))
0af8c88b
KM
6146 (replace-regexp-in-string "\n$" ""
6147 (epg-encrypt-string epg-context
6148 (encode-coding-string massaged-text
6149 (or encoding 'utf-8))
6150 recipients))))
ff3e8c8e
KM
6151
6152 ;; validate result -- non-empty
075746e2
KM
6153 (if (not result-text)
6154 (error "%scryption failed." (if decrypt "De" "En")))
6155
29fac3fe 6156
075746e2
KM
6157 (when (eq keypair-mode 'prompt-save)
6158 ;; set epa-file-encrypt-to in the buffer:
6159 (setq epa-file-encrypt-to (mapcar (lambda (key)
6160 (epg-user-id-string
6161 (car (epg-key-user-id-list key))))
6162 recipients))
6163 ;; change the file variable:
6164 (allout-adjust-file-variable "epa-file-encrypt-to" epa-file-encrypt-to))
d82979ea 6165
075746e2 6166 (cond
29fac3fe
KM
6167 ;; Retry (within limit) if ciphertext contains rejections:
6168 ((and (not decrypt)
6169 ;; Check for disqualification of this ciphertext:
6170 (let ((regexps reject-ciphertext-regexps)
6171 reject-it)
6172 (while (and regexps (not reject-it))
6173 (setq reject-it (string-match (car regexps) result-text))
6174 (pop regexps))
6175 reject-it))
6176 (setq rejections-left (1- rejections-left))
6177 (if (<= rejections-left 0)
6178 (error (concat "Ciphertext rejected too many times"
6179 " (%s), per `%s'")
6180 allout-encryption-ciphertext-rejection-ceiling
6181 'allout-encryption-ciphertext-rejection-regexps)
6182 ;; try again (gpg-agent may have the key cached):
6183 (allout-encrypt-string text decrypt allout-buffer keypair-mode
6184 (1+ rejected))))
6185
6186 ;; Barf if encryption yields extraordinary control chars:
6187 ((and (not decrypt)
6188 (string-match "[\C-a\C-k\C-o-\C-z\C-@]"
6189 result-text))
6190 (error (concat "Encryption produced non-armored text, which"
6191 "conflicts with allout mode -- reconfigure!")))
29fac3fe 6192 (t result-text))))
13aa217b
KM
6193;;;_ > allout-inhibit-auto-save-info-for-decryption
6194(defun allout-inhibit-auto-save-info-for-decryption (was-buffer-saved-size)
6195 "Temporarily prevent auto-saves in this buffer when an item is decrypted.
6196
6197WAS-BUFFER-SAVED-SIZE is the value of buffer-saved-size *before*
6198the decryption."
6199 (when (not (or (= buffer-saved-size -1) (= was-buffer-saved-size -1)))
6200 (setq allout-auto-save-temporarily-disabled was-buffer-saved-size
6201 buffer-saved-size -1)))
6202;;;_ > allout-maybe-resume-auto-save-info-after-encryption ()
6203(defun allout-maybe-resume-auto-save-info-after-encryption ()
6204 "Restore auto-save info, *if* there are no topics pending encryption."
6205 (when (and allout-auto-save-temporarily-disabled
6206 (= buffer-saved-size -1)
6207 (save-excursion
6208 (save-restriction
6209 (widen)
6210 (goto-char (point-min))
6211 (not (allout-next-topic-pending-encryption)))))
6212 (setq buffer-saved-size allout-auto-save-temporarily-disabled
6213 allout-auto-save-temporarily-disabled nil)))
6214
d82979ea
EZ
6215;;;_ > allout-encrypted-topic-p ()
6216(defun allout-encrypted-topic-p ()
6217 "True if the current topic is encryptable and encrypted."
6218 (save-excursion
6219 (allout-end-of-prefix t)
6220 (and (string= (buffer-substring-no-properties (1- (point)) (point))
6221 allout-topic-encryption-bullet)
2a1408fd 6222 (save-match-data (looking-at "\\*")))
d82979ea
EZ
6223 )
6224 )
13aa217b
KM
6225;;;_ > allout-next-topic-pending-encryption ()
6226(defun allout-next-topic-pending-encryption ()
d82979ea
EZ
6227 "Return the point of the next topic pending encryption, or nil if none.
6228
75bcb912 6229Such a topic has the `allout-topic-encryption-bullet' without an
d82979ea
EZ
6230immediately following '*' that would mark the topic as being encrypted. It
6231must also have content."
6232 (let (done got content-beg)
2a1408fd
DN
6233 (save-match-data
6234 (while (not done)
d82979ea 6235
2a1408fd
DN
6236 (if (not (re-search-forward
6237 (format "\\(\\`\\|\n\\)%s *%s[^*]"
6238 (regexp-quote allout-header-prefix)
6239 (regexp-quote allout-topic-encryption-bullet))
6240 nil t))
6241 (setq got nil
6242 done t)
6243 (goto-char (setq got (match-beginning 0)))
6244 (if (save-match-data (looking-at "\n"))
6245 (forward-char 1))
6246 (setq got (point)))
6247
6248 (cond ((not got)
6249 (setq done t))
6250
6251 ((not (search-forward "\n"))
6252 (setq got nil
6253 done t))
6254
6255 ((eobp)
6256 (setq got nil
6257 done t))
6258
6259 (t
6260 (setq content-beg (point))
6261 (backward-char 1)
6262 (allout-end-of-subtree)
13aa217b 6263 (if (<= (point) content-beg)
2a1408fd
DN
6264 ;; Continue looking
6265 (setq got nil)
6266 ;; Got it!
6267 (setq done t)))
6268 )
6269 )
6270 (if got
6271 (goto-char got))
d82979ea 6272 )
d82979ea
EZ
6273 )
6274 )
13aa217b
KM
6275;;;_ > allout-encrypt-decrypted ()
6276(defun allout-encrypt-decrypted ()
d82979ea
EZ
6277 "Encrypt topics pending encryption except those containing exemption point.
6278
d82979ea
EZ
6279If a topic that is currently being edited was encrypted, we return a list
6280containing the location of the topic and the location of the cursor just
6281before the topic was encrypted. This can be used, eg, to decrypt the topic
6282and exactly resituate the cursor if this is being done as part of a file
6283save. See `allout-encrypt-unencrypted-on-saves' for more info."
6284
6285 (interactive "p")
2a1408fd
DN
6286 (save-match-data
6287 (save-excursion
6288 (let* ((current-mark (point-marker))
6289 (current-mark-position (marker-position current-mark))
6290 was-modified
6291 bo-subtree
6292 editing-topic editing-point)
6293 (goto-char (point-min))
13aa217b 6294 (while (allout-next-topic-pending-encryption)
2a1408fd
DN
6295 (setq was-modified (buffer-modified-p))
6296 (when (save-excursion
6297 (and (boundp 'allout-encrypt-unencrypted-on-saves)
6298 allout-encrypt-unencrypted-on-saves
6299 (setq bo-subtree (re-search-forward "$"))
6300 (not (allout-hidden-p))
6301 (>= current-mark (point))
6302 (allout-end-of-current-subtree)
6303 (<= current-mark (point))))
d82979ea
EZ
6304 (setq editing-topic (point)
6305 ;; we had to wait for this 'til now so prior topics are
6306 ;; encrypted, any relevant text shifts are in place:
96b83743
EZ
6307 editing-point (- current-mark-position
6308 (count-trailing-whitespace-region
6309 bo-subtree current-mark-position))))
2a1408fd
DN
6310 (allout-toggle-subtree-encryption)
6311 (if (not was-modified)
6312 (set-buffer-modified-p nil))
6313 )
d82979ea
EZ
6314 (if (not was-modified)
6315 (set-buffer-modified-p nil))
2a1408fd 6316 (if editing-topic (list editing-topic editing-point))
d82979ea 6317 )
d82979ea
EZ
6318 )
6319 )
6320 )
6321
6322;;;_ #9 miscellaneous
d8350998
CY
6323;;;_ : Mode:
6324;;;_ > outlineify-sticky ()
19b84ba3 6325;; outlinify-sticky is correct spelling; provide this alias for sticklers:
0949617b 6326;;;###autoload
19b84ba3 6327(defalias 'outlinify-sticky 'outlineify-sticky)
0949617b 6328;;;###autoload
c567ac01 6329(defun outlineify-sticky (&optional arg)
a0776d6b 6330 "Activate outline mode and establish file var so it is started subsequently.
1977b8f6 6331
11c46e15
KM
6332See `allout-layout' and customization of `allout-auto-activation'
6333for details on preparing emacs for automatic allout activation."
19b84ba3
RS
6334
6335 (interactive "P")
6336
2576c28f
KM
6337 (if (allout-mode-p) (allout-mode)) ; deactivate so we can re-activate...
6338 (allout-mode)
19b84ba3
RS
6339
6340 (save-excursion
6341 (goto-char (point-min))
ede4ac6a 6342 (if (allout-goto-prefix)
19b84ba3 6343 t
fd5359c6 6344 (allout-open-topic 2)
60c7bbd2 6345 (insert (concat "Dummy outline topic header -- see"
d82979ea
EZ
6346 "`allout-mode' docstring: `^Hm'."))
6347 (allout-adjust-file-variable
4034b0e2 6348 "allout-layout" (or allout-layout '(-1 : 0))))))
d8350998 6349;;;_ > allout-file-vars-section-data ()
d82979ea
EZ
6350(defun allout-file-vars-section-data ()
6351 "Return data identifying the file-vars section, or nil if none.
6352
60c7bbd2 6353Returns a list of the form (BEGINNING-POINT PREFIX-STRING SUFFIX-STRING)."
d82979ea
EZ
6354 ;; minimally gleaned from emacs 21.4 files.el hack-local-variables function.
6355 (let (beg prefix suffix)
6356 (save-excursion
1977b8f6 6357 (goto-char (point-max))
d82979ea
EZ
6358 (search-backward "\n\^L" (max (- (point-max) 3000) (point-min)) 'move)
6359 (if (let ((case-fold-search t))
6360 (not (search-forward "Local Variables:" nil t)))
6361 nil
6362 (setq beg (- (point) 16))
6363 (setq suffix (buffer-substring-no-properties
6364 (point)
96b83743 6365 (progn (if (search-forward "\n" nil t)
d82979ea
EZ
6366 (forward-char -1))
6367 (point))))
6368 (setq prefix (buffer-substring-no-properties
96b83743 6369 (progn (if (search-backward "\n" nil t)
d82979ea
EZ
6370 (forward-char 1))
6371 (point))
6372 beg))
6373 (list beg prefix suffix))
6374 )
6375 )
6376 )
d8350998 6377;;;_ > allout-adjust-file-variable (varname value)
d82979ea 6378(defun allout-adjust-file-variable (varname value)
a0a022d5 6379 "Adjust the setting of an Emacs file variable named VARNAME to VALUE.
d82979ea
EZ
6380
6381This activity is inhibited if either `enable-local-variables'
6382`allout-enable-file-variable-adjustment' are nil.
6383
6384When enabled, an entry for the variable is created if not already present,
6385or changed if established with a different value. The section for the file
6386variables, itself, is created if not already present. When created, the
c07583cd 6387section lines (including the section line) exist as second-level topics in
d82979ea
EZ
6388a top-level topic at the end of the file.
6389
a0a022d5 6390`enable-local-variables' must be true for any of this to happen."
d82979ea
EZ
6391 (if (not (and enable-local-variables
6392 allout-enable-file-variable-adjustment))
6393 nil
6394 (save-excursion
0458fa80
EZ
6395 (let ((inhibit-field-text-motion t)
6396 (section-data (allout-file-vars-section-data))
d82979ea
EZ
6397 beg prefix suffix)
6398 (if section-data
6399 (setq beg (car section-data)
6400 prefix (cadr section-data)
6401 suffix (car (cddr section-data)))
6402 ;; create the section
6403 (goto-char (point-max))
6404 (open-line 1)
6405 (allout-open-topic 0)
6406 (end-of-line)
6407 (insert "Local emacs vars.\n")
6408 (allout-open-topic 1)
6409 (setq beg (point)
6410 suffix ""
6411 prefix (buffer-substring-no-properties (progn
6412 (beginning-of-line)
6413 (point))
6414 beg))
6415 (goto-char beg)
6416 (insert "Local variables:\n")
6417 (allout-open-topic 0)
6418 (insert "End:\n")
6419 )
6420 ;; look for existing entry or create one, leaving point for insertion
6421 ;; of new value:
6422 (goto-char beg)
6423 (allout-show-to-offshoot)
6424 (if (search-forward (concat "\n" prefix varname ":") nil t)
6425 (let* ((value-beg (point))
96b83743 6426 (line-end (progn (if (search-forward "\n" nil t)
d82979ea
EZ
6427 (forward-char -1))
6428 (point)))
6429 (value-end (- line-end (length suffix))))
6430 (if (> value-end value-beg)
6431 (delete-region value-beg value-end)))
6432 (end-of-line)
6433 (open-line 1)
6434 (forward-line 1)
6435 (insert (concat prefix varname ":")))
6436 (insert (format " %S%s" value suffix))
6437 )
6438 )
6439 )
6440 )
d8350998
CY
6441;;;_ > allout-get-configvar-values (varname)
6442(defun allout-get-configvar-values (configvar-name)
6443 "Return a list of values of the symbols in list bound to CONFIGVAR-NAME.
6444
6445The user is prompted for removal of symbols that are unbound, and they
6446otherwise are ignored.
6447
6448CONFIGVAR-NAME should be the name of the configuration variable,
6449not its value."
6450
6451 (let ((configvar-value (symbol-value configvar-name))
6452 got)
6453 (dolist (sym configvar-value)
6454 (if (not (boundp sym))
60c7bbd2 6455 (if (yes-or-no-p (format "%s entry `%s' is unbound -- remove it? "
d8350998
CY
6456 configvar-name sym))
6457 (delq sym (symbol-value configvar-name)))
6458 (push (symbol-value sym) got)))
6459 (reverse got)))
6460;;;_ : Topics:
6461;;;_ > allout-mark-topic ()
6462(defun allout-mark-topic ()
6463 "Put the region around topic currently containing point."
6464 (interactive)
6465 (let ((inhibit-field-text-motion t))
6466 (beginning-of-line))
6467 (allout-goto-prefix-doublechecked)
6468 (push-mark (point))
6469 (allout-end-of-current-subtree)
6470 (exchange-point-and-mark))
6471;;;_ : UI:
6472;;;_ > solicit-char-in-string (prompt string &optional do-defaulting)
1977b8f6 6473(defun solicit-char-in-string (prompt string &optional do-defaulting)
c567ac01 6474 "Solicit (with first arg PROMPT) choice of a character from string STRING.
1977b8f6 6475
c567ac01 6476Optional arg DO-DEFAULTING indicates to accept empty input (CR)."
1977b8f6
RS
6477
6478 (let ((new-prompt prompt)
6479 got)
6480
6481 (while (not got)
6482 (message "%s" new-prompt)
6483
6484 ;; We do our own reading here, so we can circumvent, eg, special
9179616f 6485 ;; treatment for `?' character. (Oughta use minibuffer keymap instead.)
1977b8f6 6486 (setq got
c567ac01 6487 (char-to-string (let ((cursor-in-echo-area nil)) (read-char))))
1977b8f6 6488
9179616f
DL
6489 (setq got
6490 (cond ((string-match (regexp-quote got) string) got)
6491 ((and do-defaulting (string= got "\r"))
6492 ;; Return empty string to default:
6493 "")
6494 ((string= got "\C-g") (signal 'quit nil))
6495 (t
6496 (setq new-prompt (concat prompt
6497 got
6498 " ...pick from: "
6499 string
6500 ""))
6501 nil))))
60c7bbd2 6502 ;; got something out of loop -- return it:
9179616f 6503 got)
1977b8f6 6504 )
d8350998
CY
6505;;;_ : Strings:
6506;;;_ > regexp-sans-escapes (string)
c567ac01
RS
6507(defun regexp-sans-escapes (regexp &optional successive-backslashes)
6508 "Return a copy of REGEXP with all character escapes stripped out.
19b84ba3 6509
60c7bbd2 6510Representations of actual backslashes -- '\\\\\\\\' -- are left as a
c567ac01
RS
6511single backslash.
6512
d82979ea 6513Optional arg SUCCESSIVE-BACKSLASHES is used internally for recursion."
c567ac01
RS
6514
6515 (if (string= regexp "")
6516 ""
6517 ;; Set successive-backslashes to number if current char is
6518 ;; backslash, or else to nil:
6519 (setq successive-backslashes
6520 (if (= (aref regexp 0) ?\\)
6521 (if successive-backslashes (1+ successive-backslashes) 1)
6522 nil))
6523 (if (or (not successive-backslashes) (= 2 successive-backslashes))
6524 ;; Include first char:
6525 (concat (substring regexp 0 1)
6526 (regexp-sans-escapes (substring regexp 1)))
6527 ;; Exclude first char, but maintain count:
6528 (regexp-sans-escapes (substring regexp 1) successive-backslashes))))
d8350998 6529;;;_ > count-trailing-whitespace-region (beg end)
96b83743
EZ
6530(defun count-trailing-whitespace-region (beg end)
6531 "Return number of trailing whitespace chars between BEG and END.
6532
6533If BEG is bigger than END we return 0."
6534 (if (> beg end)
6535 0
2a1408fd
DN
6536 (save-match-data
6537 (save-excursion
6538 (goto-char beg)
6539 (let ((count 0))
6540 (while (re-search-forward "[ ][ ]*$" end t)
6541 (goto-char (1+ (match-beginning 2)))
6542 (setq count (1+ count)))
6543 count)))))
d8350998
CY
6544;;;_ > allout-format-quote (string)
6545(defun allout-format-quote (string)
6546 "Return a copy of string with all \"%\" characters doubled."
6547 (apply 'concat
6548 (mapcar (lambda (char) (if (= char ?%) "%%" (char-to-string char)))
6549 string)))
6550;;;_ : lists
6551;;;_ > allout-flatten (list)
6552(defun allout-flatten (list)
6553 "Return a list of all atoms in list."
6554 ;; classic.
6555 (cond ((null list) nil)
8a559d4e
EZ
6556 ((atom (car list)) (cons (car list) (allout-flatten (cdr list))))
6557 (t (append (allout-flatten (car list)) (allout-flatten (cdr list))))))
75bcb912 6558;;;_ : Compatibility:
1c9b9df0
KM
6559;;;_ : xemacs undo-in-progress provision:
6560(unless (boundp 'undo-in-progress)
6561 (defvar undo-in-progress nil
6562 "Placeholder defvar for XEmacs compatibility from allout.el.")
6563 (defadvice undo-more (around allout activate)
6564 ;; This defadvice used only in emacs that lack undo-in-progress, eg xemacs.
6565 (let ((undo-in-progress t)) ad-do-it)))
6566
d8350998 6567;;;_ > allout-mark-marker to accommodate divergent emacsen:
96b83743
EZ
6568(defun allout-mark-marker (&optional force buffer)
6569 "Accommodate the different signature for `mark-marker' across Emacsen.
6570
3bda1277 6571XEmacs takes two optional args, while Emacs does not,
96b83743
EZ
6572so pass them along when appropriate."
6573 (if (featurep 'xemacs)
6574 (apply 'mark-marker force buffer)
6575 (mark-marker)))
d8350998 6576;;;_ > subst-char-in-string if necessary
d82979ea
EZ
6577(if (not (fboundp 'subst-char-in-string))
6578 (defun subst-char-in-string (fromchar tochar string &optional inplace)
6579 "Replace FROMCHAR with TOCHAR in STRING each time it occurs.
6580Unless optional argument INPLACE is non-nil, return a new string."
6581 (let ((i (length string))
6582 (newstr (if inplace string (copy-sequence string))))
6583 (while (> i 0)
6584 (setq i (1- i))
6585 (if (eq (aref newstr i) fromchar)
6586 (aset newstr i tochar)))
6587 newstr)))
d8350998 6588;;;_ > wholenump if necessary
96b83743
EZ
6589(if (not (fboundp 'wholenump))
6590 (defalias 'wholenump 'natnump))
d8350998 6591;;;_ > remove-overlays if necessary
96b83743
EZ
6592(if (not (fboundp 'remove-overlays))
6593 (defun remove-overlays (&optional beg end name val)
6594 "Clear BEG and END of overlays whose property NAME has value VAL.
6595Overlays might be moved and/or split.
6596BEG and END default respectively to the beginning and end of buffer."
6597 (unless beg (setq beg (point-min)))
6598 (unless end (setq end (point-max)))
6599 (if (< end beg)
6600 (setq beg (prog1 end (setq end beg))))
6601 (save-excursion
6602 (dolist (o (overlays-in beg end))
6603 (when (eq (overlay-get o name) val)
6604 ;; Either push this overlay outside beg...end
6605 ;; or split it to exclude beg...end
6606 ;; or delete it entirely (if it is contained in beg...end).
6607 (if (< (overlay-start o) beg)
6608 (if (> (overlay-end o) end)
6609 (progn
6610 (move-overlay (copy-overlay o)
6611 (overlay-start o) beg)
6612 (move-overlay o end (overlay-end o)))
6613 (move-overlay o (overlay-start o) beg))
6614 (if (> (overlay-end o) end)
6615 (move-overlay o end (overlay-end o))
6616 (delete-overlay o)))))))
6617 )
60c7bbd2 6618;;;_ > copy-overlay if necessary -- xemacs ~ 21.4
96b83743
EZ
6619(if (not (fboundp 'copy-overlay))
6620 (defun copy-overlay (o)
6621 "Return a copy of overlay O."
6622 (let ((o1 (make-overlay (overlay-start o) (overlay-end o)
6623 ;; FIXME: there's no easy way to find the
6624 ;; insertion-type of the two markers.
6625 (overlay-buffer o)))
6626 (props (overlay-properties o)))
6627 (while props
6628 (overlay-put o1 (pop props) (pop props)))
6629 o1)))
60c7bbd2 6630;;;_ > add-to-invisibility-spec if necessary -- xemacs ~ 21.4
96b83743
EZ
6631(if (not (fboundp 'add-to-invisibility-spec))
6632 (defun add-to-invisibility-spec (element)
6633 "Add ELEMENT to `buffer-invisibility-spec'.
6634See documentation for `buffer-invisibility-spec' for the kind of elements
6635that can be added."
6636 (if (eq buffer-invisibility-spec t)
6637 (setq buffer-invisibility-spec (list t)))
6638 (setq buffer-invisibility-spec
6639 (cons element buffer-invisibility-spec))))
60c7bbd2 6640;;;_ > remove-from-invisibility-spec if necessary -- xemacs ~ 21.4
96b83743
EZ
6641(if (not (fboundp 'remove-from-invisibility-spec))
6642 (defun remove-from-invisibility-spec (element)
6643 "Remove ELEMENT from `buffer-invisibility-spec'."
6644 (if (consp buffer-invisibility-spec)
6645 (setq buffer-invisibility-spec (delete element
6646 buffer-invisibility-spec)))))
60c7bbd2 6647;;;_ > move-beginning-of-line if necessary -- older emacs, xemacs
96b83743
EZ
6648(if (not (fboundp 'move-beginning-of-line))
6649 (defun move-beginning-of-line (arg)
6650 "Move point to beginning of current line as displayed.
6651\(This disregards invisible newlines such as those
6652which are part of the text that an image rests on.)
6653
6654With argument ARG not nil or 1, move forward ARG - 1 lines first.
6655If point reaches the beginning or end of buffer, it stops there.
4034b0e2 6656To ignore intangibility, bind `inhibit-point-motion-hooks' to t."
96b83743
EZ
6657 (interactive "p")
6658 (or arg (setq arg 1))
6659 (if (/= arg 1)
6660 (condition-case nil (line-move (1- arg)) (error nil)))
6661
8570ce80
SM
6662 ;; Move to beginning-of-line, ignoring fields and invisibles.
6663 (skip-chars-backward "^\n")
2a1408fd
DN
6664 (while (and (not (bobp))
6665 (let ((prop
6666 (get-char-property (1- (point)) 'invisible)))
6667 (if (eq buffer-invisibility-spec t)
6668 prop
6669 (or (memq prop buffer-invisibility-spec)
6670 (assq prop buffer-invisibility-spec)))))
8570ce80
SM
6671 (goto-char (if (featurep 'xemacs)
6672 (previous-property-change (point))
6673 (previous-char-property-change (point))))
6674 (skip-chars-backward "^\n"))
6675 (vertical-motion 0))
96b83743 6676)
9b026d9f 6677;;;_ > move-end-of-line if necessary -- Emacs < 22.1, xemacs
96b83743
EZ
6678(if (not (fboundp 'move-end-of-line))
6679 (defun move-end-of-line (arg)
6680 "Move point to end of current line as displayed.
6681\(This disregards invisible newlines such as those
6682which are part of the text that an image rests on.)
6683
6684With argument ARG not nil or 1, move forward ARG - 1 lines first.
6685If point reaches the beginning or end of buffer, it stops there.
4034b0e2 6686To ignore intangibility, bind `inhibit-point-motion-hooks' to t."
96b83743
EZ
6687 (interactive "p")
6688 (or arg (setq arg 1))
8570ce80 6689 (let (done)
96b83743
EZ
6690 (while (not done)
6691 (let ((newpos
6692 (save-excursion
6693 (let ((goal-column 0))
6694 (and (condition-case nil
6695 (or (line-move arg) t)
6696 (error nil))
6697 (not (bobp))
6698 (progn
2a1408fd
DN
6699 (while
6700 (and
6701 (not (bobp))
6702 (let ((prop
6703 (get-char-property (1- (point))
6704 'invisible)))
6705 (if (eq buffer-invisibility-spec t)
6706 prop
6707 (or (memq prop
6708 buffer-invisibility-spec)
6709 (assq prop
6710 buffer-invisibility-spec)))))
8570ce80
SM
6711 (goto-char
6712 (previous-char-property-change (point))))
96b83743
EZ
6713 (backward-char 1)))
6714 (point)))))
6715 (goto-char newpos)
6716 (if (and (> (point) newpos)
6717 (eq (preceding-char) ?\n))
6718 (backward-char 1)
6719 (if (and (> (point) newpos) (not (eobp))
6720 (not (eq (following-char) ?\n)))
6721 ;; If we skipped something intangible
6722 ;; and now we're not really at eol,
6723 ;; keep going.
6724 (setq arg 1)
8570ce80 6725 (setq done t)))))))
96b83743 6726 )
1c9b9df0
KM
6727;;;_ > allout-next-single-char-property-change -- alias unless lacking
6728(defalias 'allout-next-single-char-property-change
6729 (if (fboundp 'next-single-char-property-change)
6730 'next-single-char-property-change
6731 'next-single-property-change)
6732 ;; No docstring because xemacs defalias doesn't support it.
6733 )
6734;;;_ > allout-previous-single-char-property-change -- alias unless lacking
6735(defalias 'allout-previous-single-char-property-change
6736 (if (fboundp 'previous-single-char-property-change)
6737 'previous-single-char-property-change
6738 'previous-single-property-change)
6739 ;; No docstring because xemacs defalias doesn't support it.
6740 )
1c9b9df0
KM
6741;;;_ > allout-select-safe-coding-system
6742(defalias 'allout-select-safe-coding-system
6743 (if (fboundp 'select-safe-coding-system)
6744 'select-safe-coding-system
6745 'detect-coding-region)
6746 )
6747;;;_ > allout-substring-no-properties
6748;; define as alias first, so byte compiler is happy.
6749(defalias 'allout-substring-no-properties 'substring-no-properties)
6750;; then supplant with definition if underlying alias absent.
6751(if (not (fboundp 'substring-no-properties))
6752 (defun allout-substring-no-properties (string &optional start end)
6753 (substring string (or start 0) end))
6754 )
96b83743 6755
96b83743 6756;;;_ #10 Unfinished
fd5359c6
MR
6757;;;_ > allout-bullet-isearch (&optional bullet)
6758(defun allout-bullet-isearch (&optional bullet)
c07583cd 6759 "Isearch (regexp) for topic with bullet BULLET."
c567ac01
RS
6760 (interactive)
6761 (if (not bullet)
6762 (setq bullet (solicit-char-in-string
6763 "ISearch for topic with bullet: "
fd5359c6 6764 (regexp-sans-escapes allout-bullets-string))))
353e2ef2 6765
c567ac01
RS
6766 (let ((isearch-regexp t)
6767 (isearch-string (concat "^"
fd5359c6 6768 allout-header-prefix
c567ac01
RS
6769 "[ \t]*"
6770 bullet)))
6771 (isearch-repeat 'forward)
6772 (isearch-mode t)))
96b83743 6773
60c7bbd2 6774;;;_ #11 Unit tests -- this should be last item before "Provide"
01fc9422
EZ
6775;;;_ > allout-run-unit-tests ()
6776(defun allout-run-unit-tests ()
6777 "Run the various allout unit tests."
6778 (message "Running allout tests...")
6779 (allout-test-resumptions)
6780 (message "Running allout tests... Done.")
6781 (sit-for .5))
6782;;;_ : test resumptions:
6783;;;_ > allout-tests-obliterate-variable (name)
6784(defun allout-tests-obliterate-variable (name)
6785 "Completely unbind variable with NAME."
1c9b9df0 6786 (if (local-variable-p name (current-buffer)) (kill-local-variable name))
01fc9422
EZ
6787 (while (boundp name) (makunbound name)))
6788;;;_ > allout-test-resumptions ()
6789(defvar allout-tests-globally-unbound nil
60c7bbd2 6790 "Fodder for allout resumptions tests -- defvar just for byte compiler.")
01fc9422 6791(defvar allout-tests-globally-true nil
60c7bbd2 6792 "Fodder for allout resumptions tests -- defvar just for byte compiler.")
01fc9422 6793(defvar allout-tests-locally-true nil
60c7bbd2 6794 "Fodder for allout resumptions tests -- defvar just for byte compiler.")
01fc9422
EZ
6795(defun allout-test-resumptions ()
6796 "Exercise allout resumptions."
6797 ;; for each resumption case, we also test that the right local/global
6798 ;; scopes are affected during resumption effects:
6799
6800 ;; ensure that previously unbound variables return to the unbound state.
6801 (with-temp-buffer
6802 (allout-tests-obliterate-variable 'allout-tests-globally-unbound)
6803 (allout-add-resumptions '(allout-tests-globally-unbound t))
6804 (assert (not (default-boundp 'allout-tests-globally-unbound)))
1c9b9df0 6805 (assert (local-variable-p 'allout-tests-globally-unbound (current-buffer)))
01fc9422
EZ
6806 (assert (boundp 'allout-tests-globally-unbound))
6807 (assert (equal allout-tests-globally-unbound t))
6808 (allout-do-resumptions)
1c9b9df0
KM
6809 (assert (not (local-variable-p 'allout-tests-globally-unbound
6810 (current-buffer))))
01fc9422
EZ
6811 (assert (not (boundp 'allout-tests-globally-unbound))))
6812
6813 ;; ensure that variable with prior global value is resumed
6814 (with-temp-buffer
6815 (allout-tests-obliterate-variable 'allout-tests-globally-true)
6816 (setq allout-tests-globally-true t)
6817 (allout-add-resumptions '(allout-tests-globally-true nil))
6818 (assert (equal (default-value 'allout-tests-globally-true) t))
1c9b9df0 6819 (assert (local-variable-p 'allout-tests-globally-true (current-buffer)))
01fc9422
EZ
6820 (assert (equal allout-tests-globally-true nil))
6821 (allout-do-resumptions)
1c9b9df0
KM
6822 (assert (not (local-variable-p 'allout-tests-globally-true
6823 (current-buffer))))
01fc9422
EZ
6824 (assert (boundp 'allout-tests-globally-true))
6825 (assert (equal allout-tests-globally-true t)))
6826
6827 ;; ensure that prior local value is resumed
6828 (with-temp-buffer
6829 (allout-tests-obliterate-variable 'allout-tests-locally-true)
6830 (set (make-local-variable 'allout-tests-locally-true) t)
6831 (assert (not (default-boundp 'allout-tests-locally-true))
60c7bbd2 6832 nil (concat "Test setup mistake -- variable supposed to"
01fc9422 6833 " not have global binding, but it does."))
1c9b9df0 6834 (assert (local-variable-p 'allout-tests-locally-true (current-buffer))
60c7bbd2 6835 nil (concat "Test setup mistake -- variable supposed to have"
01fc9422
EZ
6836 " local binding, but it lacks one."))
6837 (allout-add-resumptions '(allout-tests-locally-true nil))
6838 (assert (not (default-boundp 'allout-tests-locally-true)))
1c9b9df0 6839 (assert (local-variable-p 'allout-tests-locally-true (current-buffer)))
01fc9422
EZ
6840 (assert (equal allout-tests-locally-true nil))
6841 (allout-do-resumptions)
6842 (assert (boundp 'allout-tests-locally-true))
1c9b9df0 6843 (assert (local-variable-p 'allout-tests-locally-true (current-buffer)))
01fc9422
EZ
6844 (assert (equal allout-tests-locally-true t))
6845 (assert (not (default-boundp 'allout-tests-locally-true))))
6846
6847 ;; ensure that last of multiple resumptions holds, for various scopes.
6848 (with-temp-buffer
6849 (allout-tests-obliterate-variable 'allout-tests-globally-unbound)
6850 (allout-tests-obliterate-variable 'allout-tests-globally-true)
6851 (setq allout-tests-globally-true t)
6852 (allout-tests-obliterate-variable 'allout-tests-locally-true)
6853 (set (make-local-variable 'allout-tests-locally-true) t)
6854 (allout-add-resumptions '(allout-tests-globally-unbound t)
6855 '(allout-tests-globally-true nil)
6856 '(allout-tests-locally-true nil))
6857 (allout-add-resumptions '(allout-tests-globally-unbound 2)
6858 '(allout-tests-globally-true 3)
6859 '(allout-tests-locally-true 4))
6860 ;; reestablish many of the basic conditions are maintained after re-add:
6861 (assert (not (default-boundp 'allout-tests-globally-unbound)))
1c9b9df0 6862 (assert (local-variable-p 'allout-tests-globally-unbound (current-buffer)))
01fc9422
EZ
6863 (assert (equal allout-tests-globally-unbound 2))
6864 (assert (default-boundp 'allout-tests-globally-true))
1c9b9df0 6865 (assert (local-variable-p 'allout-tests-globally-true (current-buffer)))
01fc9422
EZ
6866 (assert (equal allout-tests-globally-true 3))
6867 (assert (not (default-boundp 'allout-tests-locally-true)))
1c9b9df0 6868 (assert (local-variable-p 'allout-tests-locally-true (current-buffer)))
01fc9422
EZ
6869 (assert (equal allout-tests-locally-true 4))
6870 (allout-do-resumptions)
1c9b9df0
KM
6871 (assert (not (local-variable-p 'allout-tests-globally-unbound
6872 (current-buffer))))
01fc9422 6873 (assert (not (boundp 'allout-tests-globally-unbound)))
1c9b9df0
KM
6874 (assert (not (local-variable-p 'allout-tests-globally-true
6875 (current-buffer))))
01fc9422
EZ
6876 (assert (boundp 'allout-tests-globally-true))
6877 (assert (equal allout-tests-globally-true t))
6878 (assert (boundp 'allout-tests-locally-true))
1c9b9df0 6879 (assert (local-variable-p 'allout-tests-locally-true (current-buffer)))
01fc9422
EZ
6880 (assert (equal allout-tests-locally-true t))
6881 (assert (not (default-boundp 'allout-tests-locally-true))))
6882
6883 ;; ensure that deliberately unbinding registered variables doesn't foul things
6884 (with-temp-buffer
6885 (allout-tests-obliterate-variable 'allout-tests-globally-unbound)
6886 (allout-tests-obliterate-variable 'allout-tests-globally-true)
6887 (setq allout-tests-globally-true t)
6888 (allout-tests-obliterate-variable 'allout-tests-locally-true)
6889 (set (make-local-variable 'allout-tests-locally-true) t)
6890 (allout-add-resumptions '(allout-tests-globally-unbound t)
6891 '(allout-tests-globally-true nil)
6892 '(allout-tests-locally-true nil))
6893 (allout-tests-obliterate-variable 'allout-tests-globally-unbound)
6894 (allout-tests-obliterate-variable 'allout-tests-globally-true)
6895 (allout-tests-obliterate-variable 'allout-tests-locally-true)
6896 (allout-do-resumptions))
6897 )
6898;;;_ % Run unit tests if `allout-run-unit-tests-after-load' is true:
6899(when allout-run-unit-tests-on-load
6900 (allout-run-unit-tests))
6901
6902;;;_ #12 Provide
96b83743 6903(provide 'allout)
1977b8f6
RS
6904
6905;;;_* Local emacs vars.
31468dcb
SM
6906;; The following `allout-layout' local variable setting:
6907;; - closes all topics from the first topic to just before the third-to-last,
6908;; - shows the children of the third to last (config vars)
6909;; - and the second to last (code section),
6910;; - and closes the last topic (this local-variables section).
6911;;Local variables:
6912;;allout-layout: (0 : -1 -1 0)
6913;;End:
6914
6a05d05f 6915;;; allout.el ends here