* src/buffer.c (Foverlays_at): Add argument `sorted'.
[emacs.git] / lisp / allout.el
blob288fb7140b80e1a3a7e5f2a872af26d726288aad
1 ;;; allout.el --- extensive outline mode for use alone and with other modes
3 ;; Copyright (C) 1992-1994, 2001-2014 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 "Expose %s with layout '%s'? "
2084 (buffer-name)
2085 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 "Select bullet: %s ('%s' default): "
3487 sans-escapes
3488 (allout-substring-no-properties default-bullet))
3489 sans-escapes
3490 t)))
3491 (message "")
3492 (if (string= choice "") default-bullet choice))
3494 ;;;_ > allout-distinctive-bullet (bullet)
3495 (defun allout-distinctive-bullet (bullet)
3496 "True if BULLET is one of those on `allout-distinctive-bullets-string'."
3497 (string-match (regexp-quote bullet) allout-distinctive-bullets-string))
3498 ;;;_ > allout-numbered-type-prefix (&optional prefix)
3499 (defun allout-numbered-type-prefix (&optional prefix)
3500 "True if current header prefix bullet is numbered bullet."
3501 (and allout-numbered-bullet
3502 (string= allout-numbered-bullet
3503 (if prefix
3504 (allout-get-prefix-bullet prefix)
3505 (allout-get-bullet)))))
3506 ;;;_ > allout-encrypted-type-prefix (&optional prefix)
3507 (defun allout-encrypted-type-prefix (&optional prefix)
3508 "True if current header prefix bullet is for an encrypted entry (body)."
3509 (and allout-topic-encryption-bullet
3510 (string= allout-topic-encryption-bullet
3511 (if prefix
3512 (allout-get-prefix-bullet prefix)
3513 (allout-get-bullet)))))
3514 ;;;_ > allout-bullet-for-depth (&optional depth)
3515 (defun allout-bullet-for-depth (&optional depth)
3516 "Return outline topic bullet suited to optional DEPTH, or current depth."
3517 ;; Find bullet in plain-bullets-string modulo DEPTH.
3518 (if allout-stylish-prefixes
3519 (char-to-string (aref allout-plain-bullets-string
3520 (% (max 0 (- depth 2))
3521 allout-plain-bullets-string-len)))
3522 allout-primary-bullet)
3525 ;;;_ - Topic Production
3526 ;;;_ > allout-make-topic-prefix (&optional prior-bullet
3527 (defun allout-make-topic-prefix (&optional prior-bullet
3529 depth
3530 instead
3531 number-control
3532 index)
3533 ;; Depth null means use current depth, non-null means we're either
3534 ;; opening a new topic after current topic, lower or higher, or we're
3535 ;; changing level of current topic.
3536 ;; Instead dominates specified bullet-char.
3537 ;;;_ . Doc string:
3538 "Generate a topic prefix suitable for optional arg DEPTH, or current depth.
3540 All the arguments are optional.
3542 PRIOR-BULLET indicates the bullet of the prefix being changed, or
3543 nil if none. This bullet may be preserved (other options
3544 notwithstanding) if it is on the `allout-distinctive-bullets-string',
3545 for instance.
3547 Second arg NEW indicates that a new topic is being opened after the
3548 topic at point, if non-nil. Default bullet for new topics, eg, may
3549 be set (contingent to other args) to numbered bullets if previous
3550 sibling is one. The implication otherwise is that the current topic
3551 is being adjusted -- shifted or rebulleted -- and we don't consider
3552 bullet or previous sibling.
3554 Third arg DEPTH forces the topic prefix to that depth, regardless of
3555 the current topics' depth.
3557 If INSTEAD is:
3559 - nil, then the bullet char for the context is used, per distinction or depth
3560 - a (numeric) character, then character's string representation is used
3561 - a string, then the user is asked for bullet with the first char as default
3562 - anything else, the user is solicited with bullet char per context as default
3564 \(INSTEAD overrides other options, including, eg, a distinctive
3565 PRIOR-BULLET.)
3567 Fifth arg, NUMBER-CONTROL, matters only if `allout-numbered-bullet'
3568 is non-nil *and* no specific INSTEAD was specified. Then
3569 NUMBER-CONTROL non-nil forces prefix to either numbered or
3570 unnumbered format, depending on the value of the sixth arg, INDEX.
3572 \(Note that NUMBER-CONTROL does *not* apply to level 1 topics. Sorry...)
3574 If NUMBER-CONTROL is non-nil and sixth arg INDEX is non-nil then
3575 the prefix of the topic is forced to be numbered. Non-nil
3576 NUMBER-CONTROL and nil INDEX forces non-numbered format on the
3577 bullet. Non-nil NUMBER-CONTROL and non-nil, non-number INDEX means
3578 that the index for the numbered prefix will be derived, by counting
3579 siblings back to start of level. If INDEX is a number, then that
3580 number is used as the index for the numbered prefix (allowing, eg,
3581 sequential renumbering to not require this function counting back the
3582 index for each successive sibling)."
3583 ;;;_ . Code:
3584 ;; The options are ordered in likely frequency of use, most common
3585 ;; highest, least lowest. Ie, more likely to be doing prefix
3586 ;; adjustments than soliciting, and yet more than numbering.
3587 ;; Current prefix is least dominant, but most likely to be commonly
3588 ;; specified...
3590 (let* (body
3591 numbering
3592 denumbering
3593 (depth (or depth (allout-depth)))
3594 (header-lead allout-header-prefix)
3595 (bullet-char
3597 ;; Getting value for bullet char is practically the whole job:
3599 (cond
3600 ; Simplest situation -- level 1:
3601 ((<= depth 1) (setq header-lead "") allout-primary-bullet)
3602 ; Simple, too: all asterisks:
3603 (allout-old-style-prefixes
3604 ;; Cheat -- make body the whole thing, null out header-lead and
3605 ;; bullet-char:
3606 (setq body (make-string depth
3607 (string-to-char allout-primary-bullet)))
3608 (setq header-lead "")
3611 ;; (Neither level 1 nor old-style, so we're space padding.
3612 ;; Sneak it in the condition of the next case, whatever it is.)
3614 ;; Solicitation overrides numbering and other cases:
3615 ((progn (setq body (make-string (- depth 2) ?\ ))
3616 ;; The actual condition:
3617 instead)
3618 (let ((got (cond ((stringp instead)
3619 (if (> (length instead) 0)
3620 (allout-solicit-alternate-bullet
3621 depth (substring instead 0 1))))
3622 ((characterp instead) (char-to-string instead))
3623 (t (allout-solicit-alternate-bullet depth)))))
3624 ;; Gotta check whether we're numbering and got a numbered bullet:
3625 (setq numbering (and allout-numbered-bullet
3626 (not (and number-control (not index)))
3627 (string= got allout-numbered-bullet)))
3628 ;; Now return what we got, regardless:
3629 got))
3631 ;; Numbering invoked through args:
3632 ((and allout-numbered-bullet number-control)
3633 (if (setq numbering (not (setq denumbering (not index))))
3634 allout-numbered-bullet
3635 (if (and prior-bullet
3636 (not (string= allout-numbered-bullet
3637 prior-bullet)))
3638 prior-bullet
3639 (allout-bullet-for-depth depth))))
3641 ;;; Neither soliciting nor controlled numbering ;;;
3642 ;;; (may be controlled denumbering, tho) ;;;
3644 ;; Check wrt previous sibling:
3645 ((and new ; only check for new prefixes
3646 (<= depth (allout-depth))
3647 allout-numbered-bullet ; ... & numbering enabled
3648 (not denumbering)
3649 (let ((sibling-bullet
3650 (save-excursion
3651 ;; Locate correct sibling:
3652 (or (>= depth (allout-depth))
3653 (allout-ascend-to-depth depth))
3654 (allout-get-bullet))))
3655 (if (and sibling-bullet
3656 (string= allout-numbered-bullet sibling-bullet))
3657 (setq numbering sibling-bullet)))))
3659 ;; Distinctive prior bullet?
3660 ((and prior-bullet
3661 (allout-distinctive-bullet prior-bullet)
3662 ;; Either non-numbered:
3663 (or (not (and allout-numbered-bullet
3664 (string= prior-bullet allout-numbered-bullet)))
3665 ;; or numbered, and not denumbering:
3666 (setq numbering (not denumbering)))
3667 ;; Here 'tis:
3668 prior-bullet))
3670 ;; Else, standard bullet per depth:
3671 ((allout-bullet-for-depth depth)))))
3673 (concat header-lead
3674 body
3675 bullet-char
3676 (if numbering
3677 (format "%d" (cond ((and index (numberp index)) index)
3678 (new (1+ (allout-sibling-index depth)))
3679 ((allout-sibling-index))))))
3682 ;;;_ > allout-open-topic (relative-depth &optional before offer-recent-bullet)
3683 (defun allout-open-topic (relative-depth &optional before offer-recent-bullet)
3684 "Open a new topic at depth DEPTH.
3686 New topic is situated after current one, unless optional flag BEFORE
3687 is non-nil, or unless current line is completely empty -- lacking even
3688 whitespace -- in which case open is done on the current line.
3690 When adding an offspring, it will be added immediately after the parent if
3691 the other offspring are exposed, or after the last child if the offspring
3692 are hidden. (The intervening offspring will be exposed in the latter
3693 case.)
3695 If OFFER-RECENT-BULLET is true, offer to use the bullet of the prior sibling.
3697 Nuances:
3699 - Creation of new topics is with respect to the visible topic
3700 containing the cursor, regardless of intervening concealed ones.
3702 - New headers are generally created after/before the body of a
3703 topic. However, they are created right at cursor location if the
3704 cursor is on a blank line, even if that breaks the current topic
3705 body. This is intentional, to provide a simple means for
3706 deliberately dividing topic bodies.
3708 - Double spacing of topic lists is preserved. Also, the first
3709 level two topic is created double-spaced (and so would be
3710 subsequent siblings, if that's left intact). Otherwise,
3711 single-spacing is used.
3713 - Creation of sibling or nested topics is with respect to the topic
3714 you're starting from, even when creating backwards. This way you
3715 can easily create a sibling in front of the current topic without
3716 having to go to its preceding sibling, and then open forward
3717 from there."
3719 (allout-beginning-of-current-line)
3720 (save-match-data
3721 (let* ((inhibit-field-text-motion t)
3722 (depth (+ (allout-current-depth) relative-depth))
3723 (opening-on-blank (if (looking-at "^\$")
3724 (not (setq before nil))))
3725 ;; bunch o vars set while computing ref-topic
3726 opening-numbered
3727 ref-depth
3728 ref-bullet
3729 (ref-topic (save-excursion
3730 (cond ((< relative-depth 0)
3731 (allout-ascend-to-depth depth))
3732 ((>= relative-depth 1) nil)
3733 (t (allout-back-to-current-heading)))
3734 (setq ref-depth allout-recent-depth)
3735 (setq ref-bullet
3736 (if (> allout-recent-prefix-end 1)
3737 (allout-recent-bullet)
3738 ""))
3739 (setq opening-numbered
3740 (save-excursion
3741 (and allout-numbered-bullet
3742 (or (<= relative-depth 0)
3743 (allout-descend-to-depth depth))
3744 (if (allout-numbered-type-prefix)
3745 allout-numbered-bullet))))
3746 (point)))
3747 dbl-space
3748 doing-beginning
3749 start end)
3751 (if (not opening-on-blank)
3752 ; Positioning and vertical
3753 ; padding -- only if not
3754 ; opening-on-blank:
3755 (progn
3756 (goto-char ref-topic)
3757 (setq dbl-space ; Determine double space action:
3758 (or (and (<= relative-depth 0) ; not descending;
3759 (save-excursion
3760 ;; at b-o-b or preceded by a blank line?
3761 (or (> 0 (forward-line -1))
3762 (looking-at "^\\s-*$")
3763 (bobp)))
3764 (save-excursion
3765 ;; succeeded by a blank line?
3766 (allout-end-of-current-subtree)
3767 (looking-at "\n\n")))
3768 (and (= ref-depth 1)
3769 (or before
3770 (= depth 1)
3771 (save-excursion
3772 ;; Don't already have following
3773 ;; vertical padding:
3774 (not (allout-pre-next-prefix)))))))
3776 ;; Position to prior heading, if inserting backwards, and not
3777 ;; going outwards:
3778 (if (and before (>= relative-depth 0))
3779 (progn (allout-back-to-current-heading)
3780 (setq doing-beginning (bobp))
3781 (if (not (bobp))
3782 (allout-previous-heading)))
3783 (if (and before (bobp))
3784 (open-line 1)))
3786 (if (<= relative-depth 0)
3787 ;; Not going inwards, don't snug up:
3788 (if doing-beginning
3789 (if (not dbl-space)
3790 (open-line 1)
3791 (open-line 2))
3792 (if before
3793 (progn (end-of-line)
3794 (allout-pre-next-prefix)
3795 (while (and (= ?\n (following-char))
3796 (save-excursion
3797 (forward-char 1)
3798 (allout-hidden-p)))
3799 (forward-char 1))
3800 (if (not (looking-at "^$"))
3801 (open-line 1)))
3802 (allout-end-of-current-subtree)
3803 (if (looking-at "\n\n") (forward-char 1))))
3804 ;; Going inwards -- double-space if first offspring is
3805 ;; double-spaced, otherwise snug up.
3806 (allout-end-of-entry)
3807 (if (eobp)
3808 (newline 1)
3809 (line-move 1))
3810 (allout-beginning-of-current-line)
3811 (backward-char 1)
3812 (if (bolp)
3813 ;; Blank lines between current header body and next
3814 ;; header -- get to last substantive (non-white-space)
3815 ;; line in body:
3816 (progn (setq dbl-space t)
3817 (re-search-backward "[^ \t\n]" nil t)))
3818 (if (looking-at "\n\n")
3819 (setq dbl-space t))
3820 (if (save-excursion
3821 (allout-next-heading)
3822 (when (> allout-recent-depth ref-depth)
3823 ;; This is an offspring.
3824 (forward-line -1)
3825 (looking-at "^\\s-*$")))
3826 (progn (forward-line 1)
3827 (open-line 1)
3828 (forward-line 1)))
3829 (allout-end-of-current-line))
3831 ;;(if doing-beginning (goto-char doing-beginning))
3832 (if (not (bobp))
3833 ;; We insert a newline char rather than using open-line to
3834 ;; avoid rear-stickiness inheritance of read-only property.
3835 (progn (if (and (not (> depth ref-depth))
3836 (not before))
3837 (open-line 1)
3838 (if (and (not dbl-space) (> depth ref-depth))
3839 (newline 1)
3840 (if dbl-space
3841 (open-line 1)
3842 (if (not before)
3843 (newline 1)))))
3844 (if (and dbl-space (not (> relative-depth 0)))
3845 (newline 1))
3846 (if (and (not (eobp))
3847 (or (not (bolp))
3848 (and (not (bobp))
3849 ;; bolp doesn't detect concealed
3850 ;; trailing newlines, compensate:
3851 (save-excursion
3852 (forward-char -1)
3853 (allout-hidden-p)))))
3854 (forward-char 1))))
3856 (setq start (point))
3857 (insert (concat (allout-make-topic-prefix opening-numbered t depth)
3858 " "))
3859 (setq end (1+ (point)))
3861 (allout-rebullet-heading (and offer-recent-bullet ref-bullet)
3862 depth nil nil t)
3863 (if (> relative-depth 0)
3864 (save-excursion (goto-char ref-topic)
3865 (allout-show-children)))
3866 (end-of-line)
3868 (run-hook-with-args 'allout-structure-added-functions start end)
3872 ;;;_ > allout-open-subtopic (arg)
3873 (defun allout-open-subtopic (arg)
3874 "Open new topic header at deeper level than the current one.
3876 Negative universal ARG means to open deeper, but place the new topic
3877 prior to the current one."
3878 (interactive "p")
3879 (allout-open-topic 1 (> 0 arg) (< 1 arg)))
3880 ;;;_ > allout-open-sibtopic (arg)
3881 (defun allout-open-sibtopic (arg)
3882 "Open new topic header at same level as the current one.
3884 Positive universal ARG means to use the bullet of the prior sibling.
3886 Negative universal ARG means to place the new topic prior to the current
3887 one."
3888 (interactive "p")
3889 (allout-open-topic 0 (> 0 arg) (not (= 1 arg))))
3890 ;;;_ > allout-open-supertopic (arg)
3891 (defun allout-open-supertopic (arg)
3892 "Open new topic header at shallower level than the current one.
3894 Negative universal ARG means to open shallower, but place the new
3895 topic prior to the current one."
3897 (interactive "p")
3898 (allout-open-topic -1 (> 0 arg) (< 1 arg)))
3900 ;;;_ - Outline Alteration
3901 ;;;_ : Topic Modification
3902 ;;;_ = allout-former-auto-filler
3903 (defvar allout-former-auto-filler nil
3904 "Name of modal fill function being wrapped by `allout-auto-fill'.")
3905 ;;;_ > allout-auto-fill ()
3906 (defun allout-auto-fill ()
3907 "`allout-mode' autofill function.
3909 Maintains outline hanging topic indentation if
3910 `allout-use-hanging-indents' is set."
3912 (when (and (not allout-inhibit-auto-fill)
3913 (or (not allout-inhibit-auto-fill-on-headline)
3914 (not (allout-on-current-heading-p))))
3915 (let ((fill-prefix (if allout-use-hanging-indents
3916 ;; Check for topic header indentation:
3917 (save-match-data
3918 (save-excursion
3919 (beginning-of-line)
3920 (if (looking-at allout-regexp)
3921 ;; ... construct indentation to account for
3922 ;; length of topic prefix:
3923 (make-string (progn (allout-end-of-prefix)
3924 (current-column))
3925 ?\ ))))))
3926 (use-auto-fill-function
3927 (if (and (eq allout-outside-normal-auto-fill-function
3928 'allout-auto-fill)
3929 (eq auto-fill-function 'allout-auto-fill))
3930 'do-auto-fill
3931 (or allout-outside-normal-auto-fill-function
3932 auto-fill-function))))
3933 (if (or allout-former-auto-filler allout-use-hanging-indents)
3934 (funcall use-auto-fill-function)))))
3935 ;;;_ > allout-reindent-body (old-depth new-depth &optional number)
3936 (defun allout-reindent-body (old-depth new-depth &optional _number)
3937 "Reindent body lines which were indented at OLD-DEPTH to NEW-DEPTH.
3939 Optional arg NUMBER indicates numbering is being added, and it must
3940 be accommodated.
3942 Note that refill of indented paragraphs is not done."
3944 (save-excursion
3945 (allout-end-of-prefix)
3946 (let* ((new-margin (current-column))
3947 excess old-indent-begin old-indent-end
3948 ;; We want the column where the header-prefix text started
3949 ;; *before* the prefix was changed, so we infer it relative
3950 ;; to the new margin and the shift in depth:
3951 (old-margin (+ old-depth (- new-margin new-depth))))
3953 ;; Process lines up to (but excluding) next topic header:
3954 (allout-unprotected
3955 (save-match-data
3956 (while
3957 (and (re-search-forward "\n\\(\\s-*\\)"
3960 ;; Register the indent data, before we reset the
3961 ;; match data with a subsequent `looking-at':
3962 (setq old-indent-begin (match-beginning 1)
3963 old-indent-end (match-end 1))
3964 (not (looking-at allout-regexp)))
3965 (if (> 0 (setq excess (- (- old-indent-end old-indent-begin)
3966 old-margin)))
3967 ;; Text starts left of old margin -- don't adjust:
3969 ;; Text was hanging at or right of old left margin --
3970 ;; reindent it, preserving its existing indentation
3971 ;; beyond the old margin:
3972 (delete-region old-indent-begin old-indent-end)
3973 (indent-to (+ new-margin excess (current-column))))))))))
3974 ;;;_ > allout-rebullet-current-heading (arg)
3975 (defun allout-rebullet-current-heading (arg)
3976 "Solicit new bullet for current visible heading."
3977 (interactive "p")
3978 (let ((initial-col (current-column))
3979 (on-bullet (eq (point)(allout-current-bullet-pos)))
3980 from to
3981 (backwards (if (< arg 0)
3982 (setq arg (* arg -1)))))
3983 (while (> arg 0)
3984 (save-excursion (allout-back-to-current-heading)
3985 (allout-end-of-prefix)
3986 (setq from allout-recent-prefix-beginning
3987 to allout-recent-prefix-end)
3988 (allout-rebullet-heading t ;;; instead
3989 nil ;;; depth
3990 nil ;;; number-control
3991 nil ;;; index
3992 t) ;;; do-successors
3993 (run-hook-with-args 'allout-exposure-change-functions
3994 from to t))
3995 (setq arg (1- arg))
3996 (if (<= arg 0)
3998 (setq initial-col nil) ; Override positioning back to init col
3999 (if (not backwards)
4000 (allout-next-visible-heading 1)
4001 (allout-goto-prefix-doublechecked)
4002 (allout-next-visible-heading -1))))
4003 (message "Done.")
4004 (cond (on-bullet (goto-char (allout-current-bullet-pos)))
4005 (initial-col (move-to-column initial-col)))))
4006 ;;;_ > allout-rebullet-heading (&optional instead ...)
4007 (defun allout-rebullet-heading (&optional instead
4008 new-depth
4009 number-control
4010 index
4011 do-successors)
4013 "Adjust bullet of current topic prefix.
4015 All args are optional.
4017 If INSTEAD is:
4018 - nil, then the bullet char for the context is used, per distinction or depth
4019 - a (numeric) character, then character's string representation is used
4020 - a string, then the user is asked for bullet with the first char as default
4021 - anything else, the user is solicited with bullet char per context as default
4023 Second arg DEPTH forces the topic prefix to that depth, regardless
4024 of the topic's current depth.
4026 Third arg NUMBER-CONTROL can force the prefix to or away from
4027 numbered form. It has effect only if `allout-numbered-bullet' is
4028 non-nil and soliciting was not explicitly invoked (via first arg).
4029 Its effect, numbering or denumbering, then depends on the setting
4030 of the fourth arg, INDEX.
4032 If NUMBER-CONTROL is non-nil and fourth arg INDEX is nil, then the
4033 prefix of the topic is forced to be non-numbered. Null index and
4034 non-nil NUMBER-CONTROL forces denumbering. Non-nil INDEX (and
4035 non-nil NUMBER-CONTROL) forces a numbered-prefix form. If non-nil
4036 INDEX is a number, then that number is used for the numbered
4037 prefix. Non-nil and non-number means that the index for the
4038 numbered prefix will be derived by allout-make-topic-prefix.
4040 Fifth arg DO-SUCCESSORS t means re-resolve count on succeeding
4041 siblings.
4043 Cf vars `allout-stylish-prefixes', `allout-old-style-prefixes',
4044 and `allout-numbered-bullet', which all affect the behavior of
4045 this function."
4047 (let* ((current-depth (allout-depth))
4048 (new-depth (or new-depth current-depth))
4049 (mb allout-recent-prefix-beginning)
4050 (me allout-recent-prefix-end)
4051 (current-bullet (buffer-substring-no-properties (- me 1) me))
4052 (has-annotation (get-text-property mb 'allout-was-hidden))
4053 (new-prefix (allout-make-topic-prefix current-bullet
4055 new-depth
4056 instead
4057 number-control
4058 index)))
4060 ;; Is new one identical to old?
4061 (if (and (= current-depth new-depth)
4062 (string= current-bullet
4063 (substring new-prefix (1- (length new-prefix)))))
4064 ;; Nothing to do:
4067 ;; New prefix probably different from old:
4068 ; get rid of old one:
4069 (allout-unprotected (delete-region mb me))
4070 (goto-char mb)
4071 ; Dispense with number if
4072 ; numbered-bullet prefix:
4073 (save-match-data
4074 (if (and allout-numbered-bullet
4075 (string= allout-numbered-bullet current-bullet)
4076 (looking-at "[0-9]+"))
4077 (allout-unprotected
4078 (delete-region (match-beginning 0)(match-end 0)))))
4080 ;; convey 'allout-was-hidden annotation, if original had it:
4081 (if has-annotation
4082 (put-text-property 0 (length new-prefix) 'allout-was-hidden t
4083 new-prefix))
4085 ; Put in new prefix:
4086 (allout-unprotected (insert new-prefix))
4088 ;; Reindent the body if elected, margin changed, and not encrypted body:
4089 (if (and allout-reindent-bodies
4090 (not (= new-depth current-depth))
4091 (not (allout-encrypted-topic-p)))
4092 (allout-reindent-body current-depth new-depth))
4094 (run-hook-with-args 'allout-exposure-change-functions mb me nil)
4096 ;; Recursively rectify successive siblings of orig topic if
4097 ;; caller elected for it:
4098 (if do-successors
4099 (save-excursion
4100 (while (allout-next-sibling new-depth nil)
4101 (setq index
4102 (cond ((numberp index) (1+ index))
4103 ((not number-control) (allout-sibling-index))))
4104 (if (allout-numbered-type-prefix)
4105 (allout-rebullet-heading nil ;;; instead
4106 new-depth ;;; new-depth
4107 number-control;;; number-control
4108 index ;;; index
4109 nil))))) ;;;(dont!)do-successors
4110 ) ; (if (and (= current-depth new-depth)...))
4111 ) ; let* ((current-depth (allout-depth))...)
4112 ) ; defun
4113 ;;;_ > allout-rebullet-topic (arg)
4114 (defun allout-rebullet-topic (arg &optional sans-offspring)
4115 "Rebullet the visible topic containing point and all contained subtopics.
4117 Descends into invisible as well as visible topics, however.
4119 When optional SANS-OFFSPRING is non-nil, subtopics are not
4120 shifted. (Shifting a topic outwards without shifting its
4121 offspring is disallowed, since this would create a \"containment
4122 discontinuity\", where the depth difference between a topic and
4123 its immediate offspring is greater than one.)
4125 With repeat count, shift topic depth by that amount."
4126 (interactive "P")
4127 (let ((start-col (current-column)))
4128 (save-excursion
4129 ;; Normalize arg:
4130 (cond ((null arg) (setq arg 0))
4131 ((listp arg) (setq arg (car arg))))
4132 ;; Fill the user in, in case we're shifting a big topic:
4133 (if (not (zerop arg)) (message "Shifting..."))
4134 (allout-back-to-current-heading)
4135 (if (<= (+ allout-recent-depth arg) 0)
4136 (error "Attempt to shift topic below level 1"))
4137 (allout-rebullet-topic-grunt arg nil nil nil nil sans-offspring)
4138 (if (not (zerop arg)) (message "Shifting... done.")))
4139 (move-to-column (max 0 (+ start-col arg)))))
4140 ;;;_ > allout-rebullet-topic-grunt (&optional relative-depth ...)
4141 (defun allout-rebullet-topic-grunt (&optional relative-depth
4142 starting-depth
4143 starting-point
4144 index
4145 do-successors
4146 sans-offspring)
4147 "Like `allout-rebullet-topic', but on nearest containing topic
4148 \(visible or not).
4150 See `allout-rebullet-heading' for rebulleting behavior.
4152 All arguments are optional.
4154 First arg RELATIVE-DEPTH means to shift the depth of the entire
4155 topic that amount.
4157 Several subsequent args are for internal recursive use by the function
4158 itself: STARTING-DEPTH, STARTING-POINT, and INDEX.
4160 Finally, if optional SANS-OFFSPRING is non-nil then the offspring
4161 are not shifted. (Shifting a topic outwards without shifting
4162 its offspring is disallowed, since this would create a
4163 \"containment discontinuity\", where the depth difference between
4164 a topic and its immediate offspring is greater than one.)"
4166 ;; XXX the recursion here is peculiar, and in general the routine may
4167 ;; need simplification with refactoring.
4169 (if (and sans-offspring
4170 relative-depth
4171 (< relative-depth 0))
4172 (error (concat "Attempt to shift topic outwards without offspring,"
4173 " would cause containment discontinuity.")))
4175 (let* ((relative-depth (or relative-depth 0))
4176 (new-depth (allout-depth))
4177 (starting-depth (or starting-depth new-depth))
4178 (on-starting-call (null starting-point))
4179 (index (or index
4180 ;; Leave index null on starting call, so rebullet-heading
4181 ;; calculates it at what might be new depth:
4182 (and (or (zerop relative-depth)
4183 (not on-starting-call))
4184 (allout-sibling-index))))
4185 (starting-index index)
4186 (moving-outwards (< 0 relative-depth))
4187 (starting-point (or starting-point (point)))
4188 (local-point (point)))
4190 ;; Sanity check for excessive promotion done only on starting call:
4191 (and on-starting-call
4192 moving-outwards
4193 (> 0 (+ starting-depth relative-depth))
4194 (error "Attempt to shift topic out beyond level 1"))
4196 (cond ((= starting-depth new-depth)
4197 ;; We're at depth to work on this one.
4199 ;; When shifting out we work on the children before working on
4200 ;; the parent to avoid interim `allout-aberrant-container-p'
4201 ;; aberrancy, and vice-versa when shifting in:
4202 (if (>= relative-depth 0)
4203 (allout-rebullet-heading nil
4204 (+ starting-depth relative-depth)
4205 nil ;;; number
4206 index
4207 nil)) ;;; do-successors
4208 (when (not sans-offspring)
4209 ;; ... and work on subsequent ones which are at greater depth:
4210 (setq index 0)
4211 (allout-next-heading)
4212 (while (and (not (eobp))
4213 (< starting-depth (allout-depth)))
4214 (setq index (1+ index))
4215 (allout-rebullet-topic-grunt relative-depth
4216 (1+ starting-depth)
4217 starting-point
4218 index)))
4219 (when (< relative-depth 0)
4220 (save-excursion
4221 (goto-char local-point)
4222 (allout-rebullet-heading nil ;;; instead
4223 (+ starting-depth relative-depth)
4224 nil ;;; number
4225 starting-index
4226 nil)))) ;;; do-successors
4228 ((< starting-depth new-depth)
4229 ;; Rare case -- subtopic more than one level deeper than parent.
4230 ;; Treat this one at an even deeper level:
4231 (allout-rebullet-topic-grunt relative-depth
4232 new-depth
4233 starting-point
4234 index
4235 sans-offspring)))
4237 (if on-starting-call
4238 (progn
4239 ;; Rectify numbering of former siblings of the adjusted topic,
4240 ;; if topic has changed depth
4241 (if (or do-successors
4242 (and (not (zerop relative-depth))
4243 (or (= allout-recent-depth starting-depth)
4244 (= allout-recent-depth (+ starting-depth
4245 relative-depth)))))
4246 (allout-rebullet-heading nil nil nil nil t))
4247 ;; Now rectify numbering of new siblings of the adjusted topic,
4248 ;; if depth has been changed:
4249 (progn (goto-char starting-point)
4250 (if (not (zerop relative-depth))
4251 (allout-rebullet-heading nil nil nil nil t)))))
4254 ;;;_ > allout-renumber-to-depth (&optional depth)
4255 (defun allout-renumber-to-depth (&optional depth)
4256 "Renumber siblings at current depth.
4258 Affects superior topics if optional arg DEPTH is less than current depth.
4260 Returns final depth."
4262 ;; Proceed by level, processing subsequent siblings on each,
4263 ;; ascending until we get shallower than the start depth:
4265 (let ((ascender (allout-depth))
4266 was-eobp)
4267 (while (and (not (eobp))
4268 (allout-depth)
4269 (>= allout-recent-depth depth)
4270 (>= ascender depth))
4271 ; Skip over all topics at
4272 ; lesser depths, which can not
4273 ; have been disturbed:
4274 (while (and (not (setq was-eobp (eobp)))
4275 (> allout-recent-depth ascender))
4276 (allout-next-heading))
4277 ; Prime ascender for ascension:
4278 (setq ascender (1- allout-recent-depth))
4279 (if (>= allout-recent-depth depth)
4280 (allout-rebullet-heading nil ;;; instead
4281 nil ;;; depth
4282 nil ;;; number-control
4283 nil ;;; index
4284 t)) ;;; do-successors
4285 (if was-eobp (goto-char (point-max)))))
4286 allout-recent-depth)
4287 ;;;_ > allout-number-siblings (&optional denumber)
4288 (defun allout-number-siblings (&optional denumber)
4289 "Assign numbered topic prefix to this topic and its siblings.
4291 With universal argument, denumber -- assign default bullet to this
4292 topic and its siblings.
4294 With repeated universal argument (`^U^U'), solicit bullet for each
4295 rebulleting each topic at this level."
4297 (interactive "P")
4299 (save-excursion
4300 (allout-back-to-current-heading)
4301 (allout-beginning-of-level)
4302 (let ((depth allout-recent-depth)
4303 (index (if (not denumber) 1))
4304 (use-bullet (equal '(16) denumber))
4305 (more t))
4306 (while more
4307 (allout-rebullet-heading use-bullet ;;; instead
4308 depth ;;; depth
4309 t ;;; number-control
4310 index ;;; index
4311 nil) ;;; do-successors
4312 (if index (setq index (1+ index)))
4313 (setq more (allout-next-sibling depth nil))))))
4314 ;;;_ > allout-shift-in (arg)
4315 (defun allout-shift-in (arg)
4316 "Increase depth of current heading and any items collapsed within it.
4318 With a negative argument, the item is shifted out using
4319 `allout-shift-out', instead.
4321 With an argument greater than one, shift-in the item but not its
4322 offspring, making the item into a sibling of its former children,
4323 and a child of sibling that formerly preceded it.
4325 You are not allowed to shift the first offspring of a topic
4326 inwards, because that would yield a \"containment
4327 discontinuity\", where the depth difference between a topic and
4328 its immediate offspring is greater than one. The first topic in
4329 the file can be adjusted to any positive depth, however."
4331 (interactive "p")
4332 (if (< arg 0)
4333 (allout-shift-out (* arg -1))
4334 ;; refuse to create a containment discontinuity:
4335 (save-excursion
4336 (allout-back-to-current-heading)
4337 (if (not (bobp))
4338 (let* ((current-depth allout-recent-depth)
4339 (start-point (point))
4340 (predecessor-depth (progn
4341 (forward-char -1)
4342 (allout-goto-prefix-doublechecked)
4343 (if (< (point) start-point)
4344 allout-recent-depth
4345 0))))
4346 (if (and (> predecessor-depth 0)
4347 (> (1+ current-depth)
4348 (1+ predecessor-depth)))
4349 (error (concat "Disallowed shift deeper than"
4350 " containing topic's children."))
4351 (allout-back-to-current-heading)
4352 (if (< allout-recent-depth (1+ current-depth))
4353 (allout-show-children))))))
4354 (let ((where (point)))
4355 (allout-rebullet-topic 1 (and (> arg 1) 'sans-offspring))
4356 (run-hook-with-args 'allout-structure-shifted-functions arg where))))
4357 ;;;_ > allout-shift-out (arg)
4358 (defun allout-shift-out (arg)
4359 "Decrease depth of current heading and any topics collapsed within it.
4360 This will make the item a sibling of its former container.
4362 With a negative argument, the item is shifted in using
4363 `allout-shift-in', instead.
4365 With an argument greater than one, shift-out the item's offspring
4366 but not the item itself, making the former children siblings of
4367 the item.
4369 With an argument greater than 1, the item's offspring are shifted
4370 out without shifting the item. This will make the immediate
4371 subtopics into siblings of the item."
4372 (interactive "p")
4373 (if (< arg 0)
4374 (allout-shift-in (* arg -1))
4375 ;; Get proper exposure in this area:
4376 (save-excursion (if (allout-ascend)
4377 (allout-show-children)))
4378 ;; Show collapsed children if there's a successor which will become
4379 ;; their sibling:
4380 (if (and (allout-current-topic-collapsed-p)
4381 (save-excursion (allout-next-sibling)))
4382 (allout-show-children))
4383 (let ((where (and (allout-depth) allout-recent-prefix-beginning)))
4384 (save-excursion
4385 (if (> arg 1)
4386 ;; Shift the offspring but not the topic:
4387 (let ((children-chart (allout-chart-subtree 1)))
4388 (if (listp (car children-chart))
4389 ;; whoops:
4390 (setq children-chart (allout-flatten children-chart)))
4391 (save-excursion
4392 (dolist (child-point children-chart)
4393 (goto-char child-point)
4394 (allout-shift-out 1))))
4395 (allout-rebullet-topic (* arg -1))))
4396 (run-hook-with-args 'allout-structure-shifted-functions (* arg -1) where))))
4397 ;;;_ : Surgery (kill-ring) functions with special provisions for outlines:
4398 ;;;_ > allout-kill-line (&optional arg)
4399 (defun allout-kill-line (&optional arg)
4400 "Kill line, adjusting subsequent lines suitably for outline mode."
4402 (interactive "*P")
4404 (if (or (not (allout-mode-p))
4405 (not (bolp))
4406 (not (save-match-data (looking-at allout-regexp))))
4407 ;; Just do a regular kill:
4408 (kill-line arg)
4409 ;; Ah, have to watch out for adjustments:
4410 (let* ((beg (point))
4412 (beg-hidden (allout-hidden-p))
4413 (end-hidden (save-excursion (allout-end-of-current-line)
4414 (setq end (point))
4415 (allout-hidden-p)))
4416 (depth (allout-depth)))
4418 (allout-annotate-hidden beg end)
4419 (unwind-protect
4420 (if (and (not beg-hidden) (not end-hidden))
4421 (allout-unprotected (kill-line arg))
4422 (kill-line arg))
4423 (run-hooks 'allout-after-copy-or-kill-hook)
4424 (allout-deannotate-hidden beg end)
4426 (if allout-numbered-bullet
4427 (save-excursion ; Renumber subsequent topics if needed:
4428 (if (not (save-match-data (looking-at allout-regexp)))
4429 (allout-next-heading))
4430 (allout-renumber-to-depth depth)))
4431 (run-hook-with-args 'allout-structure-deleted-functions depth (point))))))
4432 ;;;_ > allout-copy-line-as-kill ()
4433 (defun allout-copy-line-as-kill ()
4434 "Like `allout-kill-topic', but save to kill ring instead of deleting."
4435 (interactive)
4436 (let ((buffer-read-only t))
4437 (condition-case nil
4438 (allout-kill-line)
4439 (buffer-read-only nil))))
4440 ;;;_ > allout-kill-topic ()
4441 (defun allout-kill-topic ()
4442 "Kill topic together with subtopics.
4444 Trailing whitespace is killed with a topic if that whitespace:
4446 - would separate the topic from a subsequent sibling
4447 - would separate the topic from the end of buffer
4448 - would not be added to whitespace already separating the topic from the
4449 previous one.
4451 Topic exposure is marked with text-properties, to be used by
4452 `allout-yank-processing' for exposure recovery."
4454 (interactive)
4455 (let* ((inhibit-field-text-motion t)
4456 (beg (prog1 (allout-back-to-current-heading) (beginning-of-line)))
4458 (depth allout-recent-depth))
4459 (allout-end-of-current-subtree)
4460 (if (and (/= (current-column) 0) (not (eobp)))
4461 (forward-char 1))
4462 (if (not (eobp))
4463 (if (and (save-match-data (looking-at "\n"))
4464 (or (save-excursion
4465 (or (not (allout-next-heading))
4466 (= depth allout-recent-depth)))
4467 (and (> (- beg (point-min)) 3)
4468 (string= (buffer-substring (- beg 2) beg) "\n\n"))))
4469 (forward-char 1)))
4471 (allout-annotate-hidden beg (setq end (point)))
4472 (unwind-protect ; for possible barf-if-buffer-read-only.
4473 (allout-unprotected (kill-region beg end))
4474 (allout-deannotate-hidden beg end)
4475 (run-hooks 'allout-after-copy-or-kill-hook)
4477 (save-excursion
4478 (allout-renumber-to-depth depth))
4479 (run-hook-with-args 'allout-structure-deleted-functions depth (point)))))
4480 ;;;_ > allout-copy-topic-as-kill ()
4481 (defun allout-copy-topic-as-kill ()
4482 "Like `allout-kill-topic', but save to kill ring instead of deleting."
4483 (interactive)
4484 (let ((buffer-read-only t))
4485 (condition-case nil
4486 (allout-kill-topic)
4487 (buffer-read-only (message "Topic copied...")))))
4488 ;;;_ > allout-annotate-hidden (begin end)
4489 (defun allout-annotate-hidden (begin end)
4490 "Qualify text with properties to indicate exposure status."
4492 (let ((was-modified (buffer-modified-p))
4493 (buffer-read-only nil))
4494 (allout-deannotate-hidden begin end)
4495 (save-excursion
4496 (goto-char begin)
4497 (let (done next prev overlay)
4498 (while (not done)
4499 ;; at or advance to start of next hidden region:
4500 (if (not (allout-hidden-p))
4501 (setq next
4502 (max (1+ (point))
4503 (allout-next-single-char-property-change (point)
4504 'invisible
4505 nil end))))
4506 (if (or (not next) (eq prev next))
4507 ;; still not at start of hidden area -- must not be any left.
4508 (setq done t)
4509 (goto-char next)
4510 (setq prev next)
4511 (if (not (allout-hidden-p))
4512 ;; still not at start of hidden area.
4513 (setq done t)
4514 (setq overlay (allout-get-invisibility-overlay))
4515 (setq next (overlay-end overlay)
4516 prev next)
4517 ;; advance to end of this hidden area:
4518 (when next
4519 (goto-char next)
4520 (allout-unprotected
4521 (let ((buffer-undo-list t))
4522 (put-text-property (overlay-start overlay) next
4523 'allout-was-hidden t)))))))))
4524 (set-buffer-modified-p was-modified)))
4525 ;;;_ > allout-deannotate-hidden (begin end)
4526 (defun allout-deannotate-hidden (begin end)
4527 "Remove allout hidden-text annotation between BEGIN and END."
4529 (allout-unprotected
4530 (let ((inhibit-read-only t)
4531 (buffer-undo-list t))
4532 (remove-text-properties begin (min end (point-max))
4533 '(allout-was-hidden t)))))
4534 ;;;_ > allout-hide-by-annotation (begin end)
4535 (defun allout-hide-by-annotation (begin end)
4536 "Translate text properties indicating exposure status into actual exposure."
4537 (save-excursion
4538 (goto-char begin)
4539 (let ((was-modified (buffer-modified-p))
4540 done next prev)
4541 (while (not done)
4542 ;; at or advance to start of next annotation:
4543 (if (not (get-text-property (point) 'allout-was-hidden))
4544 (setq next (allout-next-single-char-property-change
4545 (point) 'allout-was-hidden nil end)))
4546 (if (or (not next) (eq prev next))
4547 ;; no more or not advancing -- must not be any left.
4548 (setq done t)
4549 (goto-char next)
4550 (setq prev next)
4551 (if (not (get-text-property (point) 'allout-was-hidden))
4552 ;; still not at start of annotation.
4553 (setq done t)
4554 ;; advance to just after end of this annotation:
4555 (setq next (allout-next-single-char-property-change
4556 (point) 'allout-was-hidden nil end))
4557 (let ((o (make-overlay prev next nil 'front-advance)))
4558 (overlay-put o 'category 'allout-exposure-category)
4559 (overlay-put o 'evaporate t))
4560 (allout-deannotate-hidden prev next)
4561 (setq prev next)
4562 (if next (goto-char next)))))
4563 (set-buffer-modified-p was-modified))))
4564 ;;;_ > allout-yank-processing ()
4565 (defun allout-yank-processing (&optional _arg)
4567 "Incidental allout-specific business to be done just after text yanks.
4569 Does depth adjustment of yanked topics, when:
4571 1 the stuff being yanked starts with a valid outline header prefix, and
4572 2 it is being yanked at the end of a line which consists of only a valid
4573 topic prefix.
4575 Also, adjusts numbering of subsequent siblings when appropriate.
4577 Depth adjustment alters the depth of all the topics being yanked
4578 the amount it takes to make the first topic have the depth of the
4579 header into which it's being yanked.
4581 The point is left in front of yanked, adjusted topics, rather than
4582 at the end (and vice-versa with the mark). Non-adjusted yanks,
4583 however, are left exactly like normal, non-allout-specific yanks."
4585 (interactive "*P")
4586 ; Get to beginning, leaving
4587 ; region around subject:
4588 (if (< (allout-mark-marker t) (point))
4589 (exchange-point-and-mark))
4590 (save-match-data
4591 (let* ((subj-beg (point))
4592 (into-bol (bolp))
4593 (subj-end (allout-mark-marker t))
4594 ;; 'resituate' if yanking an entire topic into topic header:
4595 (resituate (and (let ((allout-inhibit-aberrance-doublecheck t))
4596 (allout-e-o-prefix-p))
4597 (looking-at allout-regexp)
4598 (allout-prefix-data)))
4599 ;; `rectify-numbering' if resituating (where several topics may
4600 ;; be resituating) or yanking a topic into a topic slot (bol):
4601 (rectify-numbering (or resituate
4602 (and into-bol (looking-at allout-regexp)))))
4603 (if resituate
4604 ;; Yanking a topic into the start of a topic -- reconcile to fit:
4605 (let* ((inhibit-field-text-motion t)
4606 (prefix-len (if (not (match-end 1))
4608 (- (match-end 1) subj-beg)))
4609 (subj-depth allout-recent-depth)
4610 (prefix-bullet (allout-recent-bullet))
4611 (adjust-to-depth
4612 ;; Nil if adjustment unnecessary, otherwise depth to which
4613 ;; adjustment should be made:
4614 (save-excursion
4615 (and (goto-char subj-end)
4616 (eolp)
4617 (goto-char subj-beg)
4618 (and (looking-at allout-regexp)
4619 (progn
4620 (beginning-of-line)
4621 (not (= (point) subj-beg)))
4622 (looking-at allout-regexp)
4623 (allout-prefix-data))
4624 allout-recent-depth)))
4625 (more t))
4626 (setq rectify-numbering allout-numbered-bullet)
4627 (if adjust-to-depth
4628 ; Do the adjustment:
4629 (progn
4630 (save-restriction
4631 (narrow-to-region subj-beg subj-end)
4632 ; Trim off excessive blank
4633 ; line at end, if any:
4634 (goto-char (point-max))
4635 (if (looking-at "^$")
4636 (allout-unprotected (delete-char -1)))
4637 ; Work backwards, with each
4638 ; shallowest level,
4639 ; successively excluding the
4640 ; last processed topic from
4641 ; the narrow region:
4642 (while more
4643 (allout-back-to-current-heading)
4644 ; go as high as we can in each bunch:
4645 (while (allout-ascend t))
4646 (save-excursion
4647 (allout-unprotected
4648 (allout-rebullet-topic-grunt (- adjust-to-depth
4649 subj-depth)))
4650 (allout-depth))
4651 (if (setq more (not (bobp)))
4652 (progn (widen)
4653 (forward-char -1)
4654 (narrow-to-region subj-beg (point))))))
4655 ;; Remove new heading prefix:
4656 (allout-unprotected
4657 (progn
4658 (delete-region (point) (+ (point)
4659 prefix-len
4660 (- adjust-to-depth
4661 subj-depth)))
4662 ; and delete residual subj
4663 ; prefix digits and space:
4664 (while (looking-at "[0-9]") (delete-char 1))
4665 (delete-char -1)
4666 (if (not (eolp))
4667 (forward-char))))
4668 ;; Assert new topic's bullet - minimal effort if unchanged:
4669 (allout-rebullet-heading (string-to-char prefix-bullet)))
4670 (exchange-point-and-mark))))
4671 (if rectify-numbering
4672 (progn
4673 (save-excursion
4674 ; Give some preliminary feedback:
4675 (message "... reconciling numbers")
4676 ; ... and renumber, in case necessary:
4677 (goto-char subj-beg)
4678 (if (allout-goto-prefix-doublechecked)
4679 (allout-unprotected
4680 (allout-rebullet-heading nil ;;; instead
4681 (allout-depth) ;;; depth
4682 nil ;;; number-control
4683 nil ;;; index
4684 t)))
4685 (message ""))))
4686 (if (or into-bol resituate)
4687 (allout-hide-by-annotation (point) (allout-mark-marker t))
4688 (allout-deannotate-hidden (allout-mark-marker t) (point)))
4689 (if (not resituate)
4690 (exchange-point-and-mark))
4691 (run-hook-with-args 'allout-structure-added-functions subj-beg subj-end))))
4692 ;;;_ > allout-yank (&optional arg)
4693 (defun allout-yank (&optional arg)
4694 "`allout-mode' yank, with depth and numbering adjustment of yanked topics.
4696 Non-topic yanks work no differently than normal yanks.
4698 If a topic is being yanked into a bare topic prefix, the depth of the
4699 yanked topic is adjusted to the depth of the topic prefix.
4701 1 we're yanking in an `allout-mode' buffer
4702 2 the stuff being yanked starts with a valid outline header prefix, and
4703 3 it is being yanked at the end of a line which consists of only a valid
4704 topic prefix.
4706 If these conditions hold then the depth of the yanked topics are all
4707 adjusted the amount it takes to make the first one at the depth of the
4708 header into which it's being yanked.
4710 The point is left in front of yanked, adjusted topics, rather than
4711 at the end (and vice-versa with the mark). Non-adjusted yanks,
4712 however, (ones that don't qualify for adjustment) are handled
4713 exactly like normal yanks.
4715 Numbering of yanked topics, and the successive siblings at the depth
4716 into which they're being yanked, is adjusted.
4718 `allout-yank-pop' works with `allout-yank' just like normal `yank-pop'
4719 works with normal `yank' in non-outline buffers."
4721 (interactive "*P")
4722 (setq this-command 'yank)
4723 (allout-unprotected
4724 (yank arg))
4725 (if (allout-mode-p)
4726 (allout-yank-processing)))
4727 ;;;_ > allout-yank-pop (&optional arg)
4728 (defun allout-yank-pop (&optional arg)
4729 "Yank-pop like `allout-yank' when popping to bare outline prefixes.
4731 Adapts level of popped topics to level of fresh prefix.
4733 Note -- prefix changes to distinctive bullets will stick, if followed
4734 by pops to non-distinctive yanks. Bug..."
4736 (interactive "*p")
4737 (setq this-command 'yank)
4738 (yank-pop arg)
4739 (if (allout-mode-p)
4740 (allout-yank-processing)))
4742 ;;;_ - Specialty bullet functions
4743 ;;;_ : File Cross references
4744 ;;;_ > allout-resolve-xref ()
4745 (defun allout-resolve-xref ()
4746 "Pop to file associated with current heading, if it has an xref bullet.
4748 \(Works according to setting of `allout-file-xref-bullet')."
4749 (interactive)
4750 (if (not allout-file-xref-bullet)
4751 (error
4752 "Outline cross references disabled -- no `allout-file-xref-bullet'")
4753 (if (not (string= (allout-current-bullet) allout-file-xref-bullet))
4754 (error "Current heading lacks cross-reference bullet `%s'"
4755 allout-file-xref-bullet)
4756 (let ((inhibit-field-text-motion t)
4757 file-name)
4758 (save-match-data
4759 (save-excursion
4760 (let* ((text-start allout-recent-prefix-end)
4761 (heading-end (point-at-eol)))
4762 (goto-char text-start)
4763 (setq file-name
4764 (if (re-search-forward "\\s-\\(\\S-*\\)" heading-end t)
4765 (buffer-substring (match-beginning 1)
4766 (match-end 1)))))))
4767 (setq file-name (expand-file-name file-name))
4768 (if (or (file-exists-p file-name)
4769 (if (file-writable-p file-name)
4770 (y-or-n-p (format "%s not there, create one? "
4771 file-name))
4772 (error "%s not found and can't be created" file-name)))
4773 (condition-case failure
4774 (find-file-other-window file-name)
4775 (error failure))
4776 (error "%s not found" file-name))
4782 ;;;_ #6 Exposure Control
4784 ;;;_ - Fundamental
4785 ;;;_ > allout-flag-region (from to flag)
4786 (defun allout-flag-region (from to flag)
4787 "Conceal text between FROM and TO if FLAG is non-nil, else reveal it.
4788 After the exposure changes are made, run the abnormal hook
4789 `allout-exposure-change-functions' with the same arguments as
4790 this function."
4792 ;; We use outline invisibility spec.
4793 (remove-overlays from to 'category 'allout-exposure-category)
4794 (when flag
4795 (let ((o (make-overlay from to nil 'front-advance)))
4796 (overlay-put o 'category 'allout-exposure-category)
4797 (overlay-put o 'evaporate t)
4798 (when (featurep 'xemacs)
4799 (let ((props (symbol-plist 'allout-exposure-category)))
4800 (while props
4801 (condition-case nil
4802 ;; as of 2008-02-27, xemacs lacks modification-hooks
4803 (overlay-put o (pop props) (pop props))
4804 (error nil))))))
4805 (setq allout-this-command-hid-text t))
4806 (run-hook-with-args 'allout-exposure-change-functions from to flag))
4807 ;;;_ > allout-flag-current-subtree (flag)
4808 (defun allout-flag-current-subtree (flag)
4809 "Conceal currently-visible topic's subtree if FLAG non-nil, else reveal it."
4811 (save-excursion
4812 (allout-back-to-current-heading)
4813 (let ((inhibit-field-text-motion t))
4814 (end-of-line))
4815 (allout-flag-region (point)
4816 ;; Exposing must not leave trailing blanks hidden,
4817 ;; but can leave them exposed when hiding, so we
4818 ;; can use flag's inverse as the
4819 ;; include-trailing-blank cue:
4820 (allout-end-of-current-subtree (not flag))
4821 flag)))
4823 ;;;_ - Topic-specific
4824 ;;;_ > allout-show-entry ()
4825 (defun allout-show-entry ()
4826 "Like `allout-show-current-entry', but reveals entries in hidden topics.
4828 This is a way to give restricted peek at a concealed locality without the
4829 expense of exposing its context, but can leave the outline with aberrant
4830 exposure. `allout-show-offshoot' should be used after the peek to rectify
4831 the exposure."
4833 (interactive)
4834 (save-excursion
4835 (let (beg end)
4836 (allout-goto-prefix-doublechecked)
4837 (setq beg (if (allout-hidden-p) (1- (point)) (point)))
4838 (setq end (allout-pre-next-prefix))
4839 (allout-flag-region beg end nil)
4840 (list beg end))))
4841 ;;;_ > allout-show-children (&optional level strict)
4842 (defun allout-show-children (&optional level strict)
4844 "If point is visible, show all direct subheadings of this heading.
4846 Otherwise, do `allout-show-to-offshoot', and then show subheadings.
4848 Optional LEVEL specifies how many levels below the current level
4849 should be shown, or all levels if t. Default is 1.
4851 Optional STRICT means don't resort to -show-to-offshoot, no matter
4852 what. This is basically so -show-to-offshoot, which is called by
4853 this function, can employ the pure offspring-revealing capabilities of
4856 Returns point at end of subtree that was opened, if any. (May get a
4857 point of non-opened subtree?)"
4859 (interactive "p")
4860 (let ((start-point (point)))
4861 (if (and (not strict)
4862 (allout-hidden-p))
4864 (progn (allout-show-to-offshoot) ; Point's concealed, open to
4865 ; expose it.
4866 ;; Then recurse, but with "strict" set so we don't
4867 ;; infinite regress:
4868 (allout-show-children level t))
4870 (save-excursion
4871 (allout-beginning-of-current-line)
4872 (save-restriction
4873 (let* (depth
4874 ;; translate the level spec for this routine to the ones
4875 ;; used by -chart-subtree and -chart-to-reveal:
4876 (chart-level (cond ((not level) 1)
4877 ((eq level t) nil)
4878 (t level)))
4879 (chart (allout-chart-subtree chart-level))
4880 (to-reveal (or (allout-chart-to-reveal chart chart-level)
4881 ;; interactive, show discontinuous children:
4882 (and chart
4883 (allout-called-interactively-p)
4884 (save-excursion
4885 (allout-back-to-current-heading)
4886 (setq depth (allout-current-depth))
4887 (and (allout-next-heading)
4888 (> allout-recent-depth
4889 (1+ depth))))
4890 (message
4891 "Discontinuous offspring; use `%s %s'%s."
4892 (substitute-command-keys
4893 "\\[universal-argument]")
4894 (substitute-command-keys
4895 "\\[allout-shift-out]")
4896 " to elevate them.")
4897 (allout-chart-to-reveal
4898 chart (- allout-recent-depth depth))))))
4899 (goto-char start-point)
4900 (when (and strict (allout-hidden-p))
4901 ;; Concealed root would already have been taken care of,
4902 ;; unless strict was set.
4903 (allout-flag-region (point) (allout-snug-back) nil)
4904 (when allout-show-bodies
4905 (goto-char (car to-reveal))
4906 (allout-show-current-entry)))
4907 (while to-reveal
4908 (goto-char (car to-reveal))
4909 (allout-flag-region (save-excursion (allout-snug-back) (point))
4910 (progn (search-forward "\n" nil t)
4911 (1- (point)))
4912 nil)
4913 (when allout-show-bodies
4914 (goto-char (car to-reveal))
4915 (allout-show-current-entry))
4916 (setq to-reveal (cdr to-reveal)))))))
4917 ;; Compensate for `save-excursion's maintenance of point
4918 ;; within invisible text:
4919 (goto-char start-point)))
4920 ;;;_ > allout-show-to-offshoot ()
4921 (defun allout-show-to-offshoot ()
4922 "Like `allout-show-entry', but reveals all concealed ancestors, as well.
4924 Useful for coherently exposing to a random point in a hidden region."
4925 (interactive)
4926 (save-excursion
4927 (let ((inhibit-field-text-motion t)
4928 (orig-pt (point))
4929 (orig-pref (allout-goto-prefix-doublechecked))
4930 (last-at (point))
4931 (bag-it 0))
4932 (while (or (> bag-it 1) (allout-hidden-p))
4933 (while (allout-hidden-p)
4934 (move-beginning-of-line 1)
4935 (if (allout-hidden-p) (forward-char -1)))
4936 (if (= last-at (setq last-at (point)))
4937 ;; Oops, we're not making any progress! Show the current topic
4938 ;; completely, and try one more time here, if we haven't already.
4939 (progn (beginning-of-line)
4940 (allout-show-current-subtree)
4941 (goto-char orig-pt)
4942 (setq bag-it (1+ bag-it))
4943 (if (> bag-it 1)
4944 (error "allout-show-to-offshoot: %s"
4945 "Stumped by aberrant nesting.")))
4946 (if (> bag-it 0) (setq bag-it 0))
4947 (allout-show-children)
4948 (goto-char orig-pref)))
4949 (goto-char orig-pt)))
4950 (if (allout-hidden-p)
4951 (allout-show-entry)))
4952 ;;;_ > allout-hide-current-entry ()
4953 (defun allout-hide-current-entry ()
4954 "Hide the body directly following this heading."
4955 (interactive)
4956 (allout-back-to-current-heading)
4957 (save-excursion
4958 (let ((inhibit-field-text-motion t))
4959 (end-of-line))
4960 (allout-flag-region (point)
4961 (progn (allout-end-of-entry) (point))
4962 t)))
4963 ;;;_ > allout-show-current-entry (&optional arg)
4964 (defun allout-show-current-entry (&optional arg)
4965 "Show body following current heading, or hide entry with universal argument."
4967 (interactive "P")
4968 (if arg
4969 (allout-hide-current-entry)
4970 (save-excursion (allout-show-to-offshoot))
4971 (save-excursion
4972 (allout-flag-region (point)
4973 (progn (allout-end-of-entry t) (point))
4974 nil)
4976 ;;;_ > allout-show-current-subtree (&optional arg)
4977 (defun allout-show-current-subtree (&optional arg)
4978 "Show everything within the current topic.
4979 With a repeat-count, expose this topic and its siblings."
4980 (interactive "P")
4981 (save-excursion
4982 (if (<= (allout-current-depth) 0)
4983 ;; Outside any topics -- try to get to the first:
4984 (if (not (allout-next-heading))
4985 (error "No topics")
4986 ;; got to first, outermost topic -- set to expose it and siblings:
4987 (message "Above outermost topic -- exposing all.")
4988 (allout-flag-region (point-min)(point-max) nil))
4989 (allout-beginning-of-current-line)
4990 (if (not arg)
4991 (allout-flag-current-subtree nil)
4992 (allout-beginning-of-level)
4993 (allout-expose-topic '(* :))))))
4994 ;;;_ > allout-current-topic-collapsed-p (&optional include-single-liners)
4995 (defun allout-current-topic-collapsed-p (&optional include-single-liners)
4996 "True if the currently visible containing topic is already collapsed.
4998 Single line topics intrinsically can be considered as being both
4999 collapsed and uncollapsed. If optional INCLUDE-SINGLE-LINERS is
5000 true, then single-line topics are considered to be collapsed. By
5001 default, they are treated as being uncollapsed."
5002 (save-match-data
5003 (save-excursion
5004 (and
5005 ;; Is the topic all on one line (allowing for trailing blank line)?
5006 (>= (progn (allout-back-to-current-heading)
5007 (let ((inhibit-field-text-motion t))
5008 (move-end-of-line 1))
5009 (point))
5010 (allout-end-of-current-subtree (not (looking-at "\n\n"))))
5012 (or include-single-liners
5013 (progn (backward-char 1) (allout-hidden-p)))))))
5014 ;;;_ > allout-hide-current-subtree (&optional just-close)
5015 (defun allout-hide-current-subtree (&optional just-close)
5016 "Close the current topic, or containing topic if this one is already closed.
5018 If this topic is closed and it's a top level topic, close this topic
5019 and its siblings.
5021 If optional arg JUST-CLOSE is non-nil, do not close the parent or
5022 siblings, even if the target topic is already closed."
5024 (interactive)
5025 (let* ((from (point))
5026 (sibs-msg "Top-level topic already closed -- closing siblings...")
5027 (current-exposed (not (allout-current-topic-collapsed-p t))))
5028 (cond (current-exposed (allout-flag-current-subtree t))
5029 (just-close nil)
5030 ((allout-ascend) (allout-hide-current-subtree))
5031 (t (goto-char 0)
5032 (message sibs-msg)
5033 (allout-goto-prefix-doublechecked)
5034 (allout-expose-topic '(0 :))
5035 (message (concat sibs-msg " Done."))))
5036 (goto-char from)))
5037 ;;;_ > allout-toggle-current-subtree-exposure
5038 (defun allout-toggle-current-subtree-exposure ()
5039 "Show or hide the current subtree depending on its current state."
5040 ;; thanks to tassilo for suggesting this.
5041 (interactive)
5042 (save-excursion
5043 (allout-back-to-heading)
5044 (if (allout-hidden-p (point-at-eol))
5045 (allout-show-current-subtree)
5046 (allout-hide-current-subtree))))
5047 ;;;_ > allout-show-current-branches ()
5048 (defun allout-show-current-branches ()
5049 "Show all subheadings of this heading, but not their bodies."
5050 (interactive)
5051 (let ((inhibit-field-text-motion t))
5052 (beginning-of-line))
5053 (allout-show-children t))
5054 ;;;_ > allout-hide-current-leaves ()
5055 (defun allout-hide-current-leaves ()
5056 "Hide the bodies of the current topic and all its offspring."
5057 (interactive)
5058 (allout-back-to-current-heading)
5059 (allout-hide-region-body (point) (progn (allout-end-of-current-subtree)
5060 (point))))
5062 ;;;_ - Region and beyond
5063 ;;;_ > allout-show-all ()
5064 (defun allout-show-all ()
5065 "Show all of the text in the buffer."
5066 (interactive)
5067 (message "Exposing entire buffer...")
5068 (allout-flag-region (point-min) (point-max) nil)
5069 (message "Exposing entire buffer... Done."))
5070 ;;;_ > allout-hide-bodies ()
5071 (defun allout-hide-bodies ()
5072 "Hide all of buffer except headings."
5073 (interactive)
5074 (allout-hide-region-body (point-min) (point-max)))
5075 ;;;_ > allout-hide-region-body (start end)
5076 (defun allout-hide-region-body (start end)
5077 "Hide all body lines in the region, but not headings."
5078 (save-match-data
5079 (save-excursion
5080 (save-restriction
5081 (narrow-to-region start end)
5082 (goto-char (point-min))
5083 (let ((inhibit-field-text-motion t))
5084 (while (not (eobp))
5085 (end-of-line)
5086 (allout-flag-region (point) (allout-end-of-entry) t)
5087 (if (not (eobp))
5088 (forward-char
5089 (if (looking-at "\n\n")
5090 2 1)))))))))
5092 ;;;_ > allout-expose-topic (spec)
5093 (defun allout-expose-topic (spec)
5094 "Apply exposure specs to successive outline topic items.
5096 Use the more convenient frontend, `allout-new-exposure', if you don't
5097 need evaluation of the arguments, or even better, the `allout-layout'
5098 variable-keyed mode-activation/auto-exposure feature of allout outline
5099 mode. See the respective documentation strings for more details.
5101 Cursor is left at start position.
5103 SPEC is either a number or a list.
5105 Successive specs on a list are applied to successive sibling topics.
5107 A simple spec (either a number, one of a few symbols, or the null
5108 list) dictates the exposure for the corresponding topic.
5110 Non-null lists recursively designate exposure specs for respective
5111 subtopics of the current topic.
5113 The `:' repeat spec is used to specify exposure for any number of
5114 successive siblings, up to the trailing ones for which there are
5115 explicit specs following the `:'.
5117 Simple (numeric and null-list) specs are interpreted as follows:
5119 Numbers indicate the relative depth to open the corresponding topic.
5120 - negative numbers force the topic to be closed before opening to the
5121 absolute value of the number, so all siblings are open only to
5122 that level.
5123 - positive numbers open to the relative depth indicated by the
5124 number, but do not force already opened subtopics to be closed.
5125 - 0 means to close topic -- hide all offspring.
5126 : - `repeat'
5127 apply prior element to all siblings at current level, *up to*
5128 those siblings that would be covered by specs following the `:'
5129 on the list. Ie, apply to all topics at level but the last
5130 ones. (Only first of multiple colons at same level is
5131 respected -- subsequent ones are discarded.)
5132 * - completely opens the topic, including bodies.
5133 + - shows all the sub headers, but not the bodies
5134 - - exposes the body of the corresponding topic.
5136 Examples:
5137 \(allout-expose-topic '(-1 : 0))
5138 Close this and all following topics at current level, exposing
5139 only their immediate children, but close down the last topic
5140 at this current level completely.
5141 \(allout-expose-topic '(-1 () : 1 0))
5142 Close current topic so only the immediate subtopics are shown;
5143 show the children in the second to last topic, and completely
5144 close the last one.
5145 \(allout-expose-topic '(-2 : -1 *))
5146 Expose children and grandchildren of all topics at current
5147 level except the last two; expose children of the second to
5148 last and completely open the last one."
5150 (interactive "xExposure spec: ")
5151 (if (not (listp spec))
5153 (let ((depth (allout-depth))
5154 (max-pos 0)
5155 prev-elem curr-elem
5156 stay)
5157 (while spec
5158 (setq prev-elem curr-elem
5159 curr-elem (car spec)
5160 spec (cdr spec))
5161 (cond ; Do current element:
5162 ((null curr-elem) nil)
5163 ((symbolp curr-elem)
5164 (cond ((eq curr-elem '*) (allout-show-current-subtree)
5165 (if (> allout-recent-end-of-subtree max-pos)
5166 (setq max-pos allout-recent-end-of-subtree)))
5167 ((eq curr-elem '+)
5168 (if (not (allout-hidden-p))
5169 (save-excursion (allout-hide-current-subtree t)))
5170 (allout-show-current-branches)
5171 (if (> allout-recent-end-of-subtree max-pos)
5172 (setq max-pos allout-recent-end-of-subtree)))
5173 ((eq curr-elem '-) (allout-show-current-entry))
5174 ((eq curr-elem ':)
5175 (setq stay t)
5176 ;; Expand the `repeat' spec to an explicit version,
5177 ;; w.r.t. remaining siblings:
5178 (let ((residue ; = # of sibs not covered by remaining spec
5179 ;; Dang, could be nice to make use of the chart, sigh:
5180 (- (length (allout-chart-siblings))
5181 (length spec))))
5182 (if (< 0 residue)
5183 ;; Some residue -- cover it with prev-elem:
5184 (setq spec (append (make-list residue prev-elem)
5185 spec)))))))
5186 ((numberp curr-elem)
5187 (if (and (>= 0 curr-elem) (not (allout-hidden-p)))
5188 (save-excursion (allout-hide-current-subtree t)
5189 (if (> 0 curr-elem)
5191 (if (> allout-recent-end-of-subtree max-pos)
5192 (setq max-pos
5193 allout-recent-end-of-subtree)))))
5194 (if (> (abs curr-elem) 0)
5195 (progn (allout-show-children (abs curr-elem))
5196 (if (> allout-recent-end-of-subtree max-pos)
5197 (setq max-pos allout-recent-end-of-subtree)))))
5198 ((listp curr-elem)
5199 (if (allout-descend-to-depth (1+ depth))
5200 (let ((got (allout-expose-topic curr-elem)))
5201 (if (and got (> got max-pos)) (setq max-pos got))))))
5202 (cond (stay (setq stay nil))
5203 ((listp (car spec)) nil)
5204 ((> max-pos (point))
5205 ;; Capitalize on max-pos state to get us nearer next sibling:
5206 (progn (goto-char (min (point-max) max-pos))
5207 (allout-next-heading)))
5208 ((allout-next-sibling depth))))
5209 max-pos)))
5210 ;;;_ > allout-old-expose-topic (spec &rest followers)
5211 (defun allout-old-expose-topic (spec &rest followers)
5213 "Deprecated. Use `allout-expose-topic' (with different schema
5214 format) instead.
5216 Dictate wholesale exposure scheme for current topic, according to SPEC.
5218 SPEC is either a number or a list. Optional successive args
5219 dictate exposure for subsequent siblings of current topic.
5221 A simple spec (either a number, a special symbol, or the null list)
5222 dictates the overall exposure for a topic. Non null lists are
5223 composite specs whose first element dictates the overall exposure for
5224 a topic, with the subsequent elements in the list interpreted as specs
5225 that dictate the exposure for the successive offspring of the topic.
5227 Simple (numeric and null-list) specs are interpreted as follows:
5229 - Numbers indicate the relative depth to open the corresponding topic:
5230 - negative numbers force the topic to be close before opening to the
5231 absolute value of the number.
5232 - positive numbers just open to the relative depth indicated by the number.
5233 - 0 just closes
5234 - `*' completely opens the topic, including bodies.
5235 - `+' shows all the sub headers, but not the bodies
5236 - `-' exposes the body and immediate offspring of the corresponding topic.
5238 If the spec is a list, the first element must be a number, which
5239 dictates the exposure depth of the topic as a whole. Subsequent
5240 elements of the list are nested SPECs, dictating the specific exposure
5241 for the corresponding offspring of the topic.
5243 Optional FOLLOWERS arguments dictate exposure for succeeding siblings."
5245 (interactive "xExposure spec: ")
5246 (let ((inhibit-field-text-motion t)
5247 (depth (allout-current-depth))
5248 max-pos)
5249 (cond ((null spec) nil)
5250 ((symbolp spec)
5251 (if (eq spec '*) (allout-show-current-subtree))
5252 (if (eq spec '+) (allout-show-current-branches))
5253 (if (eq spec '-) (allout-show-current-entry)))
5254 ((numberp spec)
5255 (if (>= 0 spec)
5256 (save-excursion (allout-hide-current-subtree t)
5257 (end-of-line)
5258 (if (or (not max-pos)
5259 (> (point) max-pos))
5260 (setq max-pos (point)))
5261 (if (> 0 spec)
5262 (setq spec (* -1 spec)))))
5263 (if (> spec 0)
5264 (allout-show-children spec)))
5265 ((listp spec)
5266 ;(let ((got (allout-old-expose-topic (car spec))))
5267 ; (if (and got (or (not max-pos) (> got max-pos)))
5268 ; (setq max-pos got)))
5269 (let ((new-depth (+ (allout-current-depth) 1))
5270 got)
5271 (setq max-pos (allout-old-expose-topic (car spec)))
5272 (setq spec (cdr spec))
5273 (if (and spec
5274 (allout-descend-to-depth new-depth)
5275 (not (allout-hidden-p)))
5276 (progn (setq got (apply 'allout-old-expose-topic spec))
5277 (if (and got (or (not max-pos) (> got max-pos)))
5278 (setq max-pos got)))))))
5279 (while (and followers
5280 (progn (if (and max-pos (< (point) max-pos))
5281 (progn (goto-char max-pos)
5282 (setq max-pos nil)))
5283 (end-of-line)
5284 (allout-next-sibling depth)))
5285 (allout-old-expose-topic (car followers))
5286 (setq followers (cdr followers)))
5287 max-pos))
5288 ;;;_ > allout-new-exposure '()
5289 (defmacro allout-new-exposure (&rest spec)
5290 "Literal frontend for `allout-expose-topic', doesn't evaluate arguments.
5291 Some arguments that would need to be quoted in `allout-expose-topic'
5292 need not be quoted in `allout-new-exposure'.
5294 Cursor is left at start position.
5296 Use this instead of obsolete `allout-exposure'.
5298 Examples:
5299 \(allout-new-exposure (-1 () () () 1) 0)
5300 Close current topic at current level so only the immediate
5301 subtopics are shown, except also show the children of the
5302 third subtopic; and close the next topic at the current level.
5303 \(allout-new-exposure : -1 0)
5304 Close all topics at current level to expose only their
5305 immediate children, except for the last topic at the current
5306 level, in which even its immediate children are hidden.
5307 \(allout-new-exposure -2 : -1 *)
5308 Expose children and grandchildren of first topic at current
5309 level, and expose children of subsequent topics at current
5310 level *except* for the last, which should be opened completely."
5311 `(save-excursion
5312 (if (not (or (allout-goto-prefix-doublechecked)
5313 (allout-next-heading)))
5314 (error "allout-new-exposure: Can't find any outline topics"))
5315 (allout-expose-topic ',spec)))
5317 ;;;_ #7 Systematic outline presentation -- copying, printing, flattening
5319 ;;;_ - Mapping and processing of topics
5320 ;;;_ ( See also Subtree Charting, in Navigation code.)
5321 ;;;_ > allout-stringify-flat-index (flat-index)
5322 (defun allout-stringify-flat-index (flat-index &optional context)
5323 "Convert list representing section/subsection/... to document string.
5325 Optional arg CONTEXT indicates interior levels to include."
5326 (let ((delim ".")
5327 result
5328 numstr
5329 (context-depth (or (and context 2) 1)))
5330 ;; Take care of the explicit context:
5331 (while (> context-depth 0)
5332 (setq numstr (int-to-string (car flat-index))
5333 flat-index (cdr flat-index)
5334 result (if flat-index
5335 (cons delim (cons numstr result))
5336 (cons numstr result))
5337 context-depth (if flat-index (1- context-depth) 0)))
5338 (setq delim " ")
5339 ;; Take care of the indentation:
5340 (if flat-index
5341 (progn
5342 (while flat-index
5343 (setq result
5344 (cons delim
5345 (cons (make-string
5346 (1+ (truncate (if (zerop (car flat-index))
5348 (log (car flat-index) 10))))
5350 result)))
5351 (setq flat-index (cdr flat-index)))
5352 ;; Dispose of single extra delim:
5353 (setq result (cdr result))))
5354 (apply 'concat result)))
5355 ;;;_ > allout-stringify-flat-index-plain (flat-index)
5356 (defun allout-stringify-flat-index-plain (flat-index)
5357 "Convert list representing section/subsection/... to document string."
5358 (let ((delim ".")
5359 result)
5360 (while flat-index
5361 (setq result (cons (int-to-string (car flat-index))
5362 (if result
5363 (cons delim result))))
5364 (setq flat-index (cdr flat-index)))
5365 (apply 'concat result)))
5366 ;;;_ > allout-stringify-flat-index-indented (flat-index)
5367 (defun allout-stringify-flat-index-indented (flat-index)
5368 "Convert list representing section/subsection/... to document string."
5369 (let ((delim ".")
5370 result
5371 numstr)
5372 ;; Take care of the explicit context:
5373 (setq numstr (int-to-string (car flat-index))
5374 flat-index (cdr flat-index)
5375 result (if flat-index
5376 (cons delim (cons numstr result))
5377 (cons numstr result)))
5378 (setq delim " ")
5379 ;; Take care of the indentation:
5380 (if flat-index
5381 (progn
5382 (while flat-index
5383 (setq result
5384 (cons delim
5385 (cons (make-string
5386 (1+ (truncate (if (zerop (car flat-index))
5388 (log (car flat-index) 10))))
5390 result)))
5391 (setq flat-index (cdr flat-index)))
5392 ;; Dispose of single extra delim:
5393 (setq result (cdr result))))
5394 (apply 'concat result)))
5395 ;;;_ > allout-listify-exposed (&optional start end format)
5396 (defun allout-listify-exposed (&optional start end format)
5398 "Produce a list representing exposed topics in current region.
5400 This list can then be used by `allout-process-exposed' to manipulate
5401 the subject region.
5403 Optional START and END indicate bounds of region.
5405 Optional arg, FORMAT, designates an alternate presentation form for
5406 the prefix:
5408 list -- Present prefix as numeric section.subsection..., starting with
5409 section indicated by the list, innermost nesting first.
5410 `indent' (symbol) -- Convert header prefixes to all white space,
5411 except for distinctive bullets.
5413 The elements of the list produced are lists that represents a topic
5414 header and body. The elements of that list are:
5416 - a number representing the depth of the topic,
5417 - a string representing the header-prefix, including trailing whitespace and
5418 bullet.
5419 - a string representing the bullet character,
5420 - and a series of strings, each containing one line of the exposed
5421 portion of the topic entry."
5423 (interactive "r")
5424 (save-excursion
5425 (let*
5426 ((inhibit-field-text-motion t)
5427 ;; state vars:
5428 strings prefix result depth new-depth out gone-out bullet beg
5429 next done)
5431 (goto-char start)
5432 (beginning-of-line)
5433 ;; Goto initial topic, and register preceding stuff, if any:
5434 (if (> (allout-goto-prefix-doublechecked) start)
5435 ;; First topic follows beginning point -- register preliminary stuff:
5436 (setq result
5437 (list (list 0 "" nil
5438 (buffer-substring-no-properties start
5439 (1- (point)))))))
5440 (while (and (not done)
5441 (not (eobp)) ; Loop until we've covered the region.
5442 (not (> (point) end)))
5443 (setq depth allout-recent-depth ; Current topics depth,
5444 bullet (allout-recent-bullet) ; ... bullet,
5445 prefix (allout-recent-prefix)
5446 beg (progn (allout-end-of-prefix t) (point))) ; and beginning.
5447 (setq done ; The boundary for the current topic:
5448 (not (allout-next-visible-heading 1)))
5449 (setq new-depth allout-recent-depth)
5450 (setq gone-out out
5451 out (< new-depth depth))
5452 (beginning-of-line)
5453 (setq next (point))
5454 (goto-char beg)
5455 (setq strings nil)
5456 (while (> next (point)) ; Get all the exposed text in
5457 (setq strings
5458 (cons (buffer-substring-no-properties
5460 ;To hidden text or end of line:
5461 (progn
5462 (end-of-line)
5463 (allout-back-to-visible-text)))
5464 strings))
5465 (when (< (point) next) ; Resume from after hid text, if any.
5466 (line-move 1)
5467 (beginning-of-line))
5468 (setq beg (point)))
5469 ;; Accumulate list for this topic:
5470 (setq strings (nreverse strings))
5471 (setq result
5472 (cons
5473 (if format
5474 (let ((special (if (string-match
5475 (regexp-quote bullet)
5476 allout-distinctive-bullets-string)
5477 bullet)))
5478 (cond ((listp format)
5479 (list depth
5480 (if allout-flattened-numbering-abbreviation
5481 (allout-stringify-flat-index format
5482 gone-out)
5483 (allout-stringify-flat-index-plain
5484 format))
5485 strings
5486 special))
5487 ((eq format 'indent)
5488 (if special
5489 (list depth
5490 (concat (make-string (1+ depth) ? )
5491 (substring prefix -1))
5492 strings)
5493 (list depth
5494 (make-string depth ? )
5495 strings)))
5496 (t (error "allout-listify-exposed: %s %s"
5497 "invalid format" format))))
5498 (list depth prefix strings))
5499 result))
5500 ;; Reassess format, if any:
5501 (if (and format (listp format))
5502 (cond ((= new-depth depth)
5503 (setq format (cons (1+ (car format))
5504 (cdr format))))
5505 ((> new-depth depth) ; descending -- assume by 1:
5506 (setq format (cons 1 format)))
5508 ; Pop the residue:
5509 (while (< new-depth depth)
5510 (setq format (cdr format))
5511 (setq depth (1- depth)))
5512 ; And increment the current one:
5513 (setq format
5514 (cons (1+ (or (car format)
5515 -1))
5516 (cdr format)))))))
5517 ;; Put the list with first at front, to last at back:
5518 (nreverse result))))
5519 ;;;_ > allout-region-active-p ()
5520 (defmacro allout-region-active-p ()
5521 (cond ((fboundp 'use-region-p) '(use-region-p))
5522 ((fboundp 'region-active-p) '(region-active-p))
5523 (t 'mark-active)))
5524 ;;_ > allout-process-exposed (&optional func from to frombuf
5525 ;;; tobuf format)
5526 (defun allout-process-exposed (&optional func from to frombuf tobuf
5527 format _start-num)
5528 "Map function on exposed parts of current topic; results to another buffer.
5530 All args are options; default values itemized below.
5532 Apply FUNCTION to exposed portions FROM position TO position in buffer
5533 FROMBUF to buffer TOBUF. Sixth optional arg, FORMAT, designates an
5534 alternate presentation form:
5536 `flat' -- Present prefix as numeric section.subsection..., starting with
5537 section indicated by the START-NUM, innermost nesting first.
5538 X`flat-indented' -- Prefix is like `flat' for first topic at each
5539 X level, but subsequent topics have only leaf topic
5540 X number, padded with blanks to line up with first.
5541 `indent' (symbol) -- Convert header prefixes to all white space,
5542 except for distinctive bullets.
5544 Defaults:
5545 FUNCTION: `allout-insert-listified'
5546 FROM: region start, if region active, else start of buffer
5547 TO: region end, if region active, else end of buffer
5548 FROMBUF: current buffer
5549 TOBUF: buffer name derived: \"*current-buffer-name exposed*\"
5550 FORMAT: nil"
5552 ; Resolve arguments,
5553 ; defaulting if necessary:
5554 (if (not func) (setq func 'allout-insert-listified))
5555 (if (not (and from to))
5556 (if (allout-region-active-p)
5557 (setq from (region-beginning) to (region-end))
5558 (setq from (point-min) to (point-max))))
5559 (if frombuf
5560 (if (not (bufferp frombuf))
5561 ;; Specified but not a buffer -- get it:
5562 (let ((got (get-buffer frombuf)))
5563 (if (not got)
5564 (error (concat "allout-process-exposed: source buffer "
5565 frombuf
5566 " not found."))
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))
6055 (declare-function epg-key-user-id-list "epg" (key))
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 (concat "Dummy outline topic header -- see"
6382 "`allout-mode' docstring: `^Hm'."))
6383 (allout-adjust-file-variable
6384 "allout-layout" (or allout-layout '(-1 : 0))))))
6385 ;;;_ > allout-file-vars-section-data ()
6386 (defun allout-file-vars-section-data ()
6387 "Return data identifying the file-vars section, or nil if none.
6389 Returns a list of the form (BEGINNING-POINT PREFIX-STRING SUFFIX-STRING)."
6390 ;; minimally gleaned from emacs 21.4 files.el hack-local-variables function.
6391 (let (beg prefix suffix)
6392 (save-excursion
6393 (goto-char (point-max))
6394 (search-backward "\n\^L" (max (- (point-max) 3000) (point-min)) 'move)
6395 (if (let ((case-fold-search t))
6396 (not (search-forward "Local Variables:" nil t)))
6398 (setq beg (- (point) 16))
6399 (setq suffix (buffer-substring-no-properties
6400 (point)
6401 (progn (if (search-forward "\n" nil t)
6402 (forward-char -1))
6403 (point))))
6404 (setq prefix (buffer-substring-no-properties
6405 (progn (if (search-backward "\n" nil t)
6406 (forward-char 1))
6407 (point))
6408 beg))
6409 (list beg prefix suffix))
6413 ;;;_ > allout-adjust-file-variable (varname value)
6414 (defun allout-adjust-file-variable (varname value)
6415 "Adjust the setting of an Emacs file variable named VARNAME to VALUE.
6417 This activity is inhibited if either `enable-local-variables' or
6418 `allout-enable-file-variable-adjustment' are nil.
6420 When enabled, an entry for the variable is created if not already present,
6421 or changed if established with a different value. The section for the file
6422 variables, itself, is created if not already present. When created, the
6423 section lines (including the section line) exist as second-level topics in
6424 a top-level topic at the end of the file.
6426 `enable-local-variables' must be true for any of this to happen."
6427 (if (not (and enable-local-variables
6428 allout-enable-file-variable-adjustment))
6430 (save-excursion
6431 (let ((inhibit-field-text-motion t)
6432 (section-data (allout-file-vars-section-data))
6433 beg prefix suffix)
6434 (if section-data
6435 (setq beg (car section-data)
6436 prefix (cadr section-data)
6437 suffix (car (cddr section-data)))
6438 ;; create the section
6439 (goto-char (point-max))
6440 (open-line 1)
6441 (allout-open-topic 0)
6442 (end-of-line)
6443 (insert "Local emacs vars.\n")
6444 (allout-open-topic 1)
6445 (setq beg (point)
6446 suffix ""
6447 prefix (buffer-substring-no-properties (progn
6448 (beginning-of-line)
6449 (point))
6450 beg))
6451 (goto-char beg)
6452 (insert "Local variables:\n")
6453 (allout-open-topic 0)
6454 (insert "End:\n")
6456 ;; look for existing entry or create one, leaving point for insertion
6457 ;; of new value:
6458 (goto-char beg)
6459 (allout-show-to-offshoot)
6460 (if (search-forward (concat "\n" prefix varname ":") nil t)
6461 (let* ((value-beg (point))
6462 (line-end (progn (if (search-forward "\n" nil t)
6463 (forward-char -1))
6464 (point)))
6465 (value-end (- line-end (length suffix))))
6466 (if (> value-end value-beg)
6467 (delete-region value-beg value-end)))
6468 (end-of-line)
6469 (open-line 1)
6470 (forward-line 1)
6471 (insert (concat prefix varname ":")))
6472 (insert (format " %S%s" value suffix))
6477 ;;;_ > allout-get-configvar-values (varname)
6478 (defun allout-get-configvar-values (configvar-name)
6479 "Return a list of values of the symbols in list bound to CONFIGVAR-NAME.
6481 The user is prompted for removal of symbols that are unbound, and they
6482 otherwise are ignored.
6484 CONFIGVAR-NAME should be the name of the configuration variable,
6485 not its value."
6487 (let ((configvar-value (symbol-value configvar-name))
6488 got)
6489 (dolist (sym configvar-value)
6490 (if (not (boundp sym))
6491 (if (yes-or-no-p (format "%s entry `%s' is unbound -- remove it? "
6492 configvar-name sym))
6493 (delq sym (symbol-value configvar-name)))
6494 (push (symbol-value sym) got)))
6495 (reverse got)))
6496 ;;;_ : Topics:
6497 ;;;_ > allout-mark-topic ()
6498 (defun allout-mark-topic ()
6499 "Put the region around topic currently containing point."
6500 (interactive)
6501 (let ((inhibit-field-text-motion t))
6502 (beginning-of-line))
6503 (allout-goto-prefix-doublechecked)
6504 (push-mark (point))
6505 (allout-end-of-current-subtree)
6506 (exchange-point-and-mark))
6507 ;;;_ : UI:
6508 ;;;_ > solicit-char-in-string (prompt string &optional do-defaulting)
6509 (defun solicit-char-in-string (prompt string &optional do-defaulting)
6510 "Solicit (with first arg PROMPT) choice of a character from string STRING.
6512 Optional arg DO-DEFAULTING indicates to accept empty input (CR)."
6514 (let ((new-prompt prompt)
6515 got)
6517 (while (not got)
6518 (message "%s" new-prompt)
6520 ;; We do our own reading here, so we can circumvent, eg, special
6521 ;; treatment for `?' character. (Oughta use minibuffer keymap instead.)
6522 (setq got
6523 (char-to-string (let ((cursor-in-echo-area nil)) (read-char))))
6525 (setq got
6526 (cond ((string-match (regexp-quote got) string) got)
6527 ((and do-defaulting (string= got "\r"))
6528 ;; Return empty string to default:
6530 ((string= got "\C-g") (signal 'quit nil))
6532 (setq new-prompt (concat prompt
6534 " ...pick from: "
6535 string
6536 ""))
6537 nil))))
6538 ;; got something out of loop -- return it:
6539 got)
6541 ;;;_ : Strings:
6542 ;;;_ > regexp-sans-escapes (string)
6543 (defun regexp-sans-escapes (regexp &optional successive-backslashes)
6544 "Return a copy of REGEXP with all character escapes stripped out.
6546 Representations of actual backslashes -- '\\\\\\\\' -- are left as a
6547 single backslash.
6549 Optional arg SUCCESSIVE-BACKSLASHES is used internally for recursion."
6551 (if (string= regexp "")
6553 ;; Set successive-backslashes to number if current char is
6554 ;; backslash, or else to nil:
6555 (setq successive-backslashes
6556 (if (= (aref regexp 0) ?\\)
6557 (if successive-backslashes (1+ successive-backslashes) 1)
6558 nil))
6559 (if (or (not successive-backslashes) (= 2 successive-backslashes))
6560 ;; Include first char:
6561 (concat (substring regexp 0 1)
6562 (regexp-sans-escapes (substring regexp 1)))
6563 ;; Exclude first char, but maintain count:
6564 (regexp-sans-escapes (substring regexp 1) successive-backslashes))))
6565 ;;;_ > count-trailing-whitespace-region (beg end)
6566 (defun count-trailing-whitespace-region (beg end)
6567 "Return number of trailing whitespace chars between BEG and END.
6569 If BEG is bigger than END we return 0."
6570 (if (> beg end)
6572 (save-match-data
6573 (save-excursion
6574 (goto-char beg)
6575 (let ((count 0))
6576 (while (re-search-forward "[ ][ ]*$" end t)
6577 (goto-char (1+ (match-beginning 2)))
6578 (setq count (1+ count)))
6579 count)))))
6580 ;;;_ > allout-format-quote (string)
6581 (defun allout-format-quote (string)
6582 "Return a copy of string with all \"%\" characters doubled."
6583 (apply 'concat
6584 (mapcar (lambda (char) (if (= char ?%) "%%" (char-to-string char)))
6585 string)))
6586 ;;;_ : lists
6587 ;;;_ > allout-flatten (list)
6588 (defun allout-flatten (list)
6589 "Return a list of all atoms in list."
6590 ;; classic.
6591 (cond ((null list) nil)
6592 ((atom (car list)) (cons (car list) (allout-flatten (cdr list))))
6593 (t (append (allout-flatten (car list)) (allout-flatten (cdr list))))))
6594 ;;;_ : Compatibility:
6595 ;;;_ : xemacs undo-in-progress provision:
6596 (unless (boundp 'undo-in-progress)
6597 (defvar undo-in-progress nil
6598 "Placeholder defvar for XEmacs compatibility from allout.el.")
6599 (defadvice undo-more (around allout activate)
6600 ;; This defadvice used only in emacs that lack undo-in-progress, eg xemacs.
6601 (let ((undo-in-progress t)) ad-do-it)))
6603 ;;;_ > allout-mark-marker to accommodate divergent emacsen:
6604 (defun allout-mark-marker (&optional force buffer)
6605 "Accommodate the different signature for `mark-marker' across Emacsen.
6607 XEmacs takes two optional args, while Emacs does not,
6608 so pass them along when appropriate."
6609 (if (featurep 'xemacs)
6610 (apply 'mark-marker force buffer)
6611 (mark-marker)))
6612 ;;;_ > subst-char-in-string if necessary
6613 (if (not (fboundp 'subst-char-in-string))
6614 (defun subst-char-in-string (fromchar tochar string &optional inplace)
6615 "Replace FROMCHAR with TOCHAR in STRING each time it occurs.
6616 Unless optional argument INPLACE is non-nil, return a new string."
6617 (let ((i (length string))
6618 (newstr (if inplace string (copy-sequence string))))
6619 (while (> i 0)
6620 (setq i (1- i))
6621 (if (eq (aref newstr i) fromchar)
6622 (aset newstr i tochar)))
6623 newstr)))
6624 ;;;_ > wholenump if necessary
6625 (if (not (fboundp 'wholenump))
6626 (defalias 'wholenump 'natnump))
6627 ;;;_ > remove-overlays if necessary
6628 (if (not (fboundp 'remove-overlays))
6629 (defun remove-overlays (&optional beg end name val)
6630 "Clear BEG and END of overlays whose property NAME has value VAL.
6631 Overlays might be moved and/or split.
6632 BEG and END default respectively to the beginning and end of buffer."
6633 (unless beg (setq beg (point-min)))
6634 (unless end (setq end (point-max)))
6635 (if (< end beg)
6636 (setq beg (prog1 end (setq end beg))))
6637 (save-excursion
6638 (dolist (o (overlays-in beg end))
6639 (when (eq (overlay-get o name) val)
6640 ;; Either push this overlay outside beg...end
6641 ;; or split it to exclude beg...end
6642 ;; or delete it entirely (if it is contained in beg...end).
6643 (if (< (overlay-start o) beg)
6644 (if (> (overlay-end o) end)
6645 (progn
6646 (move-overlay (copy-overlay o)
6647 (overlay-start o) beg)
6648 (move-overlay o end (overlay-end o)))
6649 (move-overlay o (overlay-start o) beg))
6650 (if (> (overlay-end o) end)
6651 (move-overlay o end (overlay-end o))
6652 (delete-overlay o)))))))
6654 ;;;_ > copy-overlay if necessary -- xemacs ~ 21.4
6655 (if (not (fboundp 'copy-overlay))
6656 (defun copy-overlay (o)
6657 "Return a copy of overlay O."
6658 (let ((o1 (make-overlay (overlay-start o) (overlay-end o)
6659 ;; FIXME: there's no easy way to find the
6660 ;; insertion-type of the two markers.
6661 (overlay-buffer o)))
6662 (props (overlay-properties o)))
6663 (while props
6664 (overlay-put o1 (pop props) (pop props)))
6665 o1)))
6666 ;;;_ > add-to-invisibility-spec if necessary -- xemacs ~ 21.4
6667 (if (not (fboundp 'add-to-invisibility-spec))
6668 (defun add-to-invisibility-spec (element)
6669 "Add ELEMENT to `buffer-invisibility-spec'.
6670 See documentation for `buffer-invisibility-spec' for the kind of elements
6671 that can be added."
6672 (if (eq buffer-invisibility-spec t)
6673 (setq buffer-invisibility-spec (list t)))
6674 (setq buffer-invisibility-spec
6675 (cons element buffer-invisibility-spec))))
6676 ;;;_ > remove-from-invisibility-spec if necessary -- xemacs ~ 21.4
6677 (if (not (fboundp 'remove-from-invisibility-spec))
6678 (defun remove-from-invisibility-spec (element)
6679 "Remove ELEMENT from `buffer-invisibility-spec'."
6680 (if (consp buffer-invisibility-spec)
6681 (setq buffer-invisibility-spec (delete element
6682 buffer-invisibility-spec)))))
6683 ;;;_ > move-beginning-of-line if necessary -- older emacs, xemacs
6684 (if (not (fboundp 'move-beginning-of-line))
6685 (defun move-beginning-of-line (arg)
6686 "Move point to beginning of current line as displayed.
6687 \(This disregards invisible newlines such as those
6688 which are part of the text that an image rests on.)
6690 With argument ARG not nil or 1, move forward ARG - 1 lines first.
6691 If point reaches the beginning or end of buffer, it stops there.
6692 To ignore intangibility, bind `inhibit-point-motion-hooks' to t."
6693 (interactive "p")
6694 (or arg (setq arg 1))
6695 (if (/= arg 1)
6696 (condition-case nil (line-move (1- arg)) (error nil)))
6698 ;; Move to beginning-of-line, ignoring fields and invisible text.
6699 (skip-chars-backward "^\n")
6700 (while (and (not (bobp))
6701 (let ((prop
6702 (get-char-property (1- (point)) 'invisible)))
6703 (if (eq buffer-invisibility-spec t)
6704 prop
6705 (or (memq prop buffer-invisibility-spec)
6706 (assq prop buffer-invisibility-spec)))))
6707 (goto-char (if (featurep 'xemacs)
6708 (previous-property-change (point))
6709 (previous-char-property-change (point))))
6710 (skip-chars-backward "^\n"))
6711 (vertical-motion 0))
6713 ;;;_ > move-end-of-line if necessary -- Emacs < 22.1, xemacs
6714 (if (not (fboundp 'move-end-of-line))
6715 (defun move-end-of-line (arg)
6716 "Move point to end of current line as displayed.
6717 \(This disregards invisible newlines such as those
6718 which are part of the text that an image rests on.)
6720 With argument ARG not nil or 1, move forward ARG - 1 lines first.
6721 If point reaches the beginning or end of buffer, it stops there.
6722 To ignore intangibility, bind `inhibit-point-motion-hooks' to t."
6723 (interactive "p")
6724 (or arg (setq arg 1))
6725 (let (done)
6726 (while (not done)
6727 (let ((newpos
6728 (save-excursion
6729 (let ((goal-column 0))
6730 (and (condition-case nil
6731 (or (line-move arg) t)
6732 (error nil))
6733 (not (bobp))
6734 (progn
6735 (while
6736 (and
6737 (not (bobp))
6738 (let ((prop
6739 (get-char-property (1- (point))
6740 'invisible)))
6741 (if (eq buffer-invisibility-spec t)
6742 prop
6743 (or (memq prop
6744 buffer-invisibility-spec)
6745 (assq prop
6746 buffer-invisibility-spec)))))
6747 (goto-char
6748 (previous-char-property-change (point))))
6749 (backward-char 1)))
6750 (point)))))
6751 (goto-char newpos)
6752 (if (and (> (point) newpos)
6753 (eq (preceding-char) ?\n))
6754 (backward-char 1)
6755 (if (and (> (point) newpos) (not (eobp))
6756 (not (eq (following-char) ?\n)))
6757 ;; If we skipped something intangible
6758 ;; and now we're not really at eol,
6759 ;; keep going.
6760 (setq arg 1)
6761 (setq done t)))))))
6763 ;;;_ > allout-next-single-char-property-change -- alias unless lacking
6764 (defalias 'allout-next-single-char-property-change
6765 (if (fboundp 'next-single-char-property-change)
6766 'next-single-char-property-change
6767 'next-single-property-change)
6768 ;; No docstring because xemacs defalias doesn't support it.
6770 ;;;_ > allout-previous-single-char-property-change -- alias unless lacking
6771 (defalias 'allout-previous-single-char-property-change
6772 (if (fboundp 'previous-single-char-property-change)
6773 'previous-single-char-property-change
6774 'previous-single-property-change)
6775 ;; No docstring because xemacs defalias doesn't support it.
6777 ;;;_ > allout-select-safe-coding-system
6778 (defalias 'allout-select-safe-coding-system
6779 (if (fboundp 'select-safe-coding-system)
6780 'select-safe-coding-system
6781 'detect-coding-region)
6783 ;;;_ > allout-substring-no-properties
6784 ;; define as alias first, so byte compiler is happy.
6785 (defalias 'allout-substring-no-properties 'substring-no-properties)
6786 ;; then supplant with definition if underlying alias absent.
6787 (if (not (fboundp 'substring-no-properties))
6788 (defun allout-substring-no-properties (string &optional start end)
6789 (substring string (or start 0) end))
6792 ;;;_ #10 Unfinished
6793 ;;;_ > allout-bullet-isearch (&optional bullet)
6794 (defun allout-bullet-isearch (&optional bullet)
6795 "Isearch (regexp) for topic with bullet BULLET."
6796 (interactive)
6797 (if (not bullet)
6798 (setq bullet (solicit-char-in-string
6799 "ISearch for topic with bullet: "
6800 (regexp-sans-escapes allout-bullets-string))))
6802 (let ((isearch-regexp t)
6803 (isearch-string (concat "^"
6804 allout-header-prefix
6805 "[ \t]*"
6806 bullet)))
6807 (isearch-repeat 'forward)
6808 (isearch-mode t)))
6810 ;;;_ #11 Unit tests -- this should be last item before "Provide"
6811 ;;;_ > allout-run-unit-tests ()
6812 (defun allout-run-unit-tests ()
6813 "Run the various allout unit tests."
6814 (message "Running allout tests...")
6815 (allout-test-resumptions)
6816 (message "Running allout tests... Done.")
6817 (sit-for .5))
6818 ;;;_ : test resumptions:
6819 ;;;_ > allout-tests-obliterate-variable (name)
6820 (defun allout-tests-obliterate-variable (name)
6821 "Completely unbind variable with NAME."
6822 (if (local-variable-p name (current-buffer)) (kill-local-variable name))
6823 (while (boundp name) (makunbound name)))
6824 ;;;_ > allout-test-resumptions ()
6825 (defvar allout-tests-globally-unbound nil
6826 "Fodder for allout resumptions tests -- defvar just for byte compiler.")
6827 (defvar allout-tests-globally-true nil
6828 "Fodder for allout resumptions tests -- defvar just for byte compiler.")
6829 (defvar allout-tests-locally-true nil
6830 "Fodder for allout resumptions tests -- defvar just for byte compiler.")
6831 (defun allout-test-resumptions ()
6832 "Exercise allout resumptions."
6833 ;; for each resumption case, we also test that the right local/global
6834 ;; scopes are affected during resumption effects:
6836 ;; ensure that previously unbound variables return to the unbound state.
6837 (with-temp-buffer
6838 (allout-tests-obliterate-variable 'allout-tests-globally-unbound)
6839 (allout-add-resumptions '(allout-tests-globally-unbound t))
6840 (assert (not (default-boundp 'allout-tests-globally-unbound)))
6841 (assert (local-variable-p 'allout-tests-globally-unbound (current-buffer)))
6842 (assert (boundp 'allout-tests-globally-unbound))
6843 (assert (equal allout-tests-globally-unbound t))
6844 (allout-do-resumptions)
6845 (assert (not (local-variable-p 'allout-tests-globally-unbound
6846 (current-buffer))))
6847 (assert (not (boundp 'allout-tests-globally-unbound))))
6849 ;; ensure that variable with prior global value is resumed
6850 (with-temp-buffer
6851 (allout-tests-obliterate-variable 'allout-tests-globally-true)
6852 (setq allout-tests-globally-true t)
6853 (allout-add-resumptions '(allout-tests-globally-true nil))
6854 (assert (equal (default-value 'allout-tests-globally-true) t))
6855 (assert (local-variable-p 'allout-tests-globally-true (current-buffer)))
6856 (assert (equal allout-tests-globally-true nil))
6857 (allout-do-resumptions)
6858 (assert (not (local-variable-p 'allout-tests-globally-true
6859 (current-buffer))))
6860 (assert (boundp 'allout-tests-globally-true))
6861 (assert (equal allout-tests-globally-true t)))
6863 ;; ensure that prior local value is resumed
6864 (with-temp-buffer
6865 (allout-tests-obliterate-variable 'allout-tests-locally-true)
6866 (set (make-local-variable 'allout-tests-locally-true) t)
6867 (assert (not (default-boundp 'allout-tests-locally-true))
6868 nil (concat "Test setup mistake -- variable supposed to"
6869 " not have global binding, but it does."))
6870 (assert (local-variable-p 'allout-tests-locally-true (current-buffer))
6871 nil (concat "Test setup mistake -- variable supposed to have"
6872 " local binding, but it lacks one."))
6873 (allout-add-resumptions '(allout-tests-locally-true nil))
6874 (assert (not (default-boundp 'allout-tests-locally-true)))
6875 (assert (local-variable-p 'allout-tests-locally-true (current-buffer)))
6876 (assert (equal allout-tests-locally-true nil))
6877 (allout-do-resumptions)
6878 (assert (boundp 'allout-tests-locally-true))
6879 (assert (local-variable-p 'allout-tests-locally-true (current-buffer)))
6880 (assert (equal allout-tests-locally-true t))
6881 (assert (not (default-boundp 'allout-tests-locally-true))))
6883 ;; ensure that last of multiple resumptions holds, for various scopes.
6884 (with-temp-buffer
6885 (allout-tests-obliterate-variable 'allout-tests-globally-unbound)
6886 (allout-tests-obliterate-variable 'allout-tests-globally-true)
6887 (setq allout-tests-globally-true t)
6888 (allout-tests-obliterate-variable 'allout-tests-locally-true)
6889 (set (make-local-variable 'allout-tests-locally-true) t)
6890 (allout-add-resumptions '(allout-tests-globally-unbound t)
6891 '(allout-tests-globally-true nil)
6892 '(allout-tests-locally-true nil))
6893 (allout-add-resumptions '(allout-tests-globally-unbound 2)
6894 '(allout-tests-globally-true 3)
6895 '(allout-tests-locally-true 4))
6896 ;; reestablish many of the basic conditions are maintained after re-add:
6897 (assert (not (default-boundp 'allout-tests-globally-unbound)))
6898 (assert (local-variable-p 'allout-tests-globally-unbound (current-buffer)))
6899 (assert (equal allout-tests-globally-unbound 2))
6900 (assert (default-boundp 'allout-tests-globally-true))
6901 (assert (local-variable-p 'allout-tests-globally-true (current-buffer)))
6902 (assert (equal allout-tests-globally-true 3))
6903 (assert (not (default-boundp 'allout-tests-locally-true)))
6904 (assert (local-variable-p 'allout-tests-locally-true (current-buffer)))
6905 (assert (equal allout-tests-locally-true 4))
6906 (allout-do-resumptions)
6907 (assert (not (local-variable-p 'allout-tests-globally-unbound
6908 (current-buffer))))
6909 (assert (not (boundp 'allout-tests-globally-unbound)))
6910 (assert (not (local-variable-p 'allout-tests-globally-true
6911 (current-buffer))))
6912 (assert (boundp 'allout-tests-globally-true))
6913 (assert (equal allout-tests-globally-true t))
6914 (assert (boundp 'allout-tests-locally-true))
6915 (assert (local-variable-p 'allout-tests-locally-true (current-buffer)))
6916 (assert (equal allout-tests-locally-true t))
6917 (assert (not (default-boundp 'allout-tests-locally-true))))
6919 ;; ensure that deliberately unbinding registered variables doesn't foul things
6920 (with-temp-buffer
6921 (allout-tests-obliterate-variable 'allout-tests-globally-unbound)
6922 (allout-tests-obliterate-variable 'allout-tests-globally-true)
6923 (setq allout-tests-globally-true t)
6924 (allout-tests-obliterate-variable 'allout-tests-locally-true)
6925 (set (make-local-variable 'allout-tests-locally-true) t)
6926 (allout-add-resumptions '(allout-tests-globally-unbound t)
6927 '(allout-tests-globally-true nil)
6928 '(allout-tests-locally-true nil))
6929 (allout-tests-obliterate-variable 'allout-tests-globally-unbound)
6930 (allout-tests-obliterate-variable 'allout-tests-globally-true)
6931 (allout-tests-obliterate-variable 'allout-tests-locally-true)
6932 (allout-do-resumptions))
6934 ;;;_ % Run unit tests if `allout-run-unit-tests-after-load' is true:
6935 (when allout-run-unit-tests-on-load
6936 (allout-run-unit-tests))
6938 ;;;_ #12 Provide
6939 (provide 'allout)
6941 ;;;_* Local emacs vars.
6942 ;; The following `allout-layout' local variable setting:
6943 ;; - closes all topics from the first topic to just before the third-to-last,
6944 ;; - shows the children of the third to last (config vars)
6945 ;; - and the second to last (code section),
6946 ;; - and closes the last topic (this local-variables section).
6947 ;;Local variables:
6948 ;;allout-layout: (0 : -1 -1 0)
6949 ;;End:
6951 ;;; allout.el ends here