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