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