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