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