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