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