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