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