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