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