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