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