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