Fix bug #13515 with processing DBCS file names on MS-Windows.
[emacs.git] / lisp / allout.el
blob22e4d0bc59cd4094ce950d3391ad779a1858cff6
1 ;;; allout.el --- extensive outline mode for use alone and with other modes
3 ;; Copyright (C) 1992-1994, 2001-2013 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 run when allout mode starts.")
1407 ;;;_ = allout-mode-deactivate-hook
1408 (define-obsolete-variable-alias 'allout-mode-deactivate-hook
1409 'allout-mode-off-hook "24.1")
1410 (defvar allout-mode-deactivate-hook nil
1411 "Hook run when allout mode ends.")
1412 ;;;_ = allout-exposure-category
1413 (defvar allout-exposure-category nil
1414 "Symbol for use as allout invisible-text overlay category.")
1416 ;;;_ = allout-exposure-change-functions
1417 (define-obsolete-variable-alias 'allout-exposure-change-hook
1418 'allout-exposure-change-functions "24.3")
1419 (defcustom allout-exposure-change-functions nil
1420 "Abnormal hook run after allout outline subtree exposure changes.
1421 It is run at the conclusion of `allout-flag-region'.
1423 Functions on the hook must take three arguments:
1425 - FROM -- integer indicating the point at the start of the change.
1426 - TO -- integer indicating the point of the end of the change.
1427 - FLAG -- change mode: nil for exposure, otherwise concealment.
1429 This hook might be invoked multiple times by a single command."
1430 :type 'hook
1431 :group 'allout
1432 :version "24.3")
1434 ;;;_ = allout-structure-added-functions
1435 (define-obsolete-variable-alias 'allout-structure-added-hook
1436 'allout-structure-added-functions "24.3")
1437 (defcustom allout-structure-added-functions nil
1438 "Abnormal hook run after adding items to an Allout outline.
1439 Functions on the hook should take two arguments:
1441 - NEW-START -- integer indicating position of start of the first new item.
1442 - NEW-END -- integer indicating position of end of the last new item.
1444 This hook might be invoked multiple times by a single command."
1445 :type 'hook
1446 :group 'allout
1447 :version "24.3")
1449 ;;;_ = allout-structure-deleted-functions
1450 (define-obsolete-variable-alias 'allout-structure-deleted-hook
1451 'allout-structure-deleted-functions "24.3")
1452 (defcustom allout-structure-deleted-functions nil
1453 "Abnormal hook run after deleting subtrees from an Allout outline.
1454 Functions on the hook must take two arguments:
1456 - DEPTH -- integer indicating the depth of the subtree that was deleted.
1457 - REMOVED-FROM -- integer indicating the point where the subtree was removed.
1459 Some edits that remove or invalidate items may be missed by this hook:
1460 specifically edits that native allout routines do not control.
1462 This hook might be invoked multiple times by a single command."
1463 :type 'hook
1464 :group 'allout
1465 :version "24.3")
1467 ;;;_ = allout-structure-shifted-functions
1468 (define-obsolete-variable-alias 'allout-structure-shifted-hook
1469 'allout-structure-shifted-functions "24.3")
1470 (defcustom allout-structure-shifted-functions nil
1471 "Abnormal hook run after shifting items in an Allout outline.
1472 Functions on the hook should take two arguments:
1474 - DEPTH-CHANGE -- integer indicating depth increase, negative for decrease
1475 - START -- integer indicating the start point of the shifted parent item.
1477 Some edits that shift items can be missed by this hook: specifically edits
1478 that native allout routines do not control.
1480 This hook might be invoked multiple times by a single command."
1481 :type 'hook
1482 :group 'allout
1483 :version "24.3")
1485 ;;;_ = allout-after-copy-or-kill-hook
1486 (defcustom allout-after-copy-or-kill-hook nil
1487 "Normal hook run after copying outline text.."
1488 :type 'hook
1489 :group 'allout
1490 :version "24.3")
1492 ;;;_ = allout-post-undo-hook
1493 (defcustom allout-post-undo-hook nil
1494 "Normal hook run after undo activity.
1495 The item that's current when the hook is run *may* be the one
1496 that was affected by the undo.."
1497 :type 'hook
1498 :group 'allout
1499 :version "24.3")
1501 ;;;_ = allout-outside-normal-auto-fill-function
1502 (defvar allout-outside-normal-auto-fill-function nil
1503 "Value of `normal-auto-fill-function' outside of allout mode.
1505 Used by `allout-auto-fill' to do the mandated `normal-auto-fill-function'
1506 wrapped within allout's automatic `fill-prefix' setting.")
1507 (make-variable-buffer-local 'allout-outside-normal-auto-fill-function)
1508 ;;;_ = prevent redundant activation by desktop mode:
1509 (add-to-list 'desktop-minor-mode-handlers '(allout-mode . nil))
1510 ;;;_ = allout-passphrase-verifier-string
1511 (defvar allout-passphrase-verifier-string nil
1512 "Setting used to test solicited encryption passphrases against the one
1513 already associated with a file.
1515 It consists of an encrypted random string useful only to verify that a
1516 passphrase entered by the user is effective for decryption. The passphrase
1517 itself is \*not* recorded in the file anywhere, and the encrypted contents
1518 are random binary characters to avoid exposing greater susceptibility to
1519 search attacks.
1521 The verifier string is retained as an Emacs file variable, as well as in
1522 the Emacs buffer state, if file variable adjustments are enabled. See
1523 `allout-enable-file-variable-adjustment' for details about that.")
1524 (make-variable-buffer-local 'allout-passphrase-verifier-string)
1525 (make-obsolete-variable 'allout-passphrase-verifier-string
1526 'allout-passphrase-verifier-string "23.3")
1527 ;;;###autoload
1528 (put 'allout-passphrase-verifier-string 'safe-local-variable 'stringp)
1529 ;;;_ = allout-passphrase-hint-string
1530 (defvar allout-passphrase-hint-string ""
1531 "Variable used to retain reminder string for file's encryption passphrase.
1533 See the description of `allout-passphrase-hint-handling' for details about how
1534 the reminder is deployed.
1536 The hint is retained as an Emacs file variable, as well as in the Emacs buffer
1537 state, if file variable adjustments are enabled. See
1538 `allout-enable-file-variable-adjustment' for details about that.")
1539 (make-variable-buffer-local 'allout-passphrase-hint-string)
1540 (setq-default allout-passphrase-hint-string "")
1541 (make-obsolete-variable 'allout-passphrase-hint-string
1542 'allout-passphrase-hint-string "23.3")
1543 ;;;###autoload
1544 (put 'allout-passphrase-hint-string 'safe-local-variable 'stringp)
1545 ;;;_ = allout-after-save-decrypt
1546 (defvar allout-after-save-decrypt nil
1547 "Internal variable, is nil or has the value of two points:
1549 - the location of a topic to be decrypted after saving is done
1550 - where to situate the cursor after the decryption is performed
1552 This is used to decrypt the topic that was currently being edited, if it
1553 was encrypted automatically as part of a file write or autosave.")
1554 (make-variable-buffer-local 'allout-after-save-decrypt)
1555 ;;;_ = allout-encryption-plaintext-sanitization-regexps
1556 (defvar allout-encryption-plaintext-sanitization-regexps nil
1557 "List of regexps whose matches are removed from plaintext before encryption.
1559 This is for the sake of removing artifacts, like escapes, that are added on
1560 and not actually part of the original plaintext. The removal is done just
1561 prior to encryption.
1563 Entries must be symbols that are bound to the desired values.
1565 Each value can be a regexp or a list with a regexp followed by a
1566 substitution string. If it's just a regexp, all its matches are removed
1567 before the text is encrypted. If it's a regexp and a substitution, the
1568 substitution is used against the regexp matches, a la `replace-match'.")
1569 (make-variable-buffer-local 'allout-encryption-text-removal-regexps)
1570 ;;;_ = allout-encryption-ciphertext-rejection-regexps
1571 (defvar allout-encryption-ciphertext-rejection-regexps nil
1572 "Variable for regexps matching plaintext to remove before encryption.
1574 This is used to detect strings in encryption results that would
1575 register as allout mode structural elements, for example, as a
1576 topic prefix.
1578 Entries must be symbols that are bound to the desired regexp values.
1580 Encryptions that result in matches will be retried, up to
1581 `allout-encryption-ciphertext-rejection-limit' times, after which
1582 an error is raised.")
1584 (make-variable-buffer-local 'allout-encryption-ciphertext-rejection-regexps)
1585 ;;;_ = allout-encryption-ciphertext-rejection-ceiling
1586 (defvar allout-encryption-ciphertext-rejection-ceiling 5
1587 "Limit on number of times encryption ciphertext is rejected.
1589 See `allout-encryption-ciphertext-rejection-regexps' for rejection reasons.")
1590 (make-variable-buffer-local 'allout-encryption-ciphertext-rejection-ceiling)
1591 ;;;_ > allout-mode-p ()
1592 ;; Must define this macro above any uses, or byte compilation will lack
1593 ;; proper def, if file isn't loaded -- eg, during emacs build!
1594 ;;;###autoload
1595 (defmacro allout-mode-p ()
1596 "Return t if `allout-mode' is active in current buffer."
1597 'allout-mode)
1598 ;;;_ > allout-write-contents-hook-handler ()
1599 (defun allout-write-contents-hook-handler ()
1600 "Implement `allout-encrypt-unencrypted-on-saves' for file writes
1602 Return nil if all goes smoothly, or else return an informative
1603 message if an error is encountered. The message will serve as a
1604 non-nil return on `write-contents-functions' to prevent saving of
1605 the buffer while it has decrypted content.
1607 This behavior depends on Emacs versions that implement the
1608 `write-contents-functions' hook."
1610 (if (or (not (allout-mode-p))
1611 (not (boundp 'allout-encrypt-unencrypted-on-saves))
1612 (not allout-encrypt-unencrypted-on-saves))
1614 (if (save-excursion (goto-char (point-min))
1615 (allout-next-topic-pending-encryption))
1616 (progn
1617 (message "auto-encrypting pending topics")
1618 (sit-for 0)
1619 (condition-case failure
1620 (progn
1621 (setq allout-after-save-decrypt
1622 (allout-encrypt-decrypted))
1623 ;; aok - return nil:
1624 nil)
1625 (error
1626 ;; whoops - probably some still-decrypted items, return non-nil:
1627 (let ((text (format (concat "%s contents write inhibited due to"
1628 " encrypted topic encryption error:"
1629 " %s")
1630 (buffer-name (current-buffer))
1631 failure)))
1632 (message text)(sit-for 2)
1633 text)))))
1635 ;;;_ > allout-after-saves-handler ()
1636 (defun allout-after-saves-handler ()
1637 "Decrypt topic encrypted for save, if it's currently being edited.
1639 Ie, if it was pending encryption and contained the point in its body before
1640 the save.
1642 We use values stored in `allout-after-save-decrypt' to locate the topic
1643 and the place for the cursor after the decryption is done."
1644 (if (not (and (allout-mode-p)
1645 (boundp 'allout-after-save-decrypt)
1646 allout-after-save-decrypt))
1648 (goto-char (car allout-after-save-decrypt))
1649 (let ((was-modified (buffer-modified-p)))
1650 (allout-toggle-subtree-encryption)
1651 (if (not was-modified)
1652 (set-buffer-modified-p nil)))
1653 (goto-char (cadr allout-after-save-decrypt))
1654 (setq allout-after-save-decrypt nil))
1656 ;;;_ > allout-called-interactively-p ()
1657 (defmacro allout-called-interactively-p ()
1658 "A version of `called-interactively-p' independent of Emacs version."
1659 ;; ... to ease maintenance of allout without betraying deprecation.
1660 (if (equal (subr-arity (symbol-function 'called-interactively-p))
1661 '(0 . 0))
1662 '(called-interactively-p)
1663 '(called-interactively-p 'interactive)))
1664 ;;;_ = allout-inhibit-aberrance-doublecheck nil
1665 ;; In some exceptional moments, disparate topic depths need to be allowed
1666 ;; momentarily, eg when one topic is being yanked into another and they're
1667 ;; about to be reconciled. let-binding allout-inhibit-aberrance-doublecheck
1668 ;; prevents the aberrance doublecheck to allow, eg, the reconciliation
1669 ;; processing to happen in the presence of such discrepancies. It should
1670 ;; almost never be needed, however.
1671 (defvar allout-inhibit-aberrance-doublecheck nil
1672 "Internal state, for momentarily inhibits aberrance doublecheck.
1674 This should only be momentarily let-bound non-nil, not set
1675 non-nil in a lasting way.")
1677 ;;;_ #2 Mode environment and activation
1678 ;;;_ = allout-explicitly-deactivated
1679 (defvar allout-explicitly-deactivated nil
1680 "If t, `allout-mode's last deactivation was deliberate.
1681 So `allout-post-command-business' should not reactivate it...")
1682 (make-variable-buffer-local 'allout-explicitly-deactivated)
1683 ;;;_ > allout-init (mode)
1684 (defun allout-init (mode)
1685 "DEPRECATED - configure allout activation by customizing
1686 `allout-auto-activation'. This function remains around, limited
1687 from what it did before, for backwards compatibility.
1689 MODE is the activation mode - see `allout-auto-activation' for
1690 valid values."
1691 (declare (obsolete allout-auto-activation "23.3"))
1692 (custom-set-variables (list 'allout-auto-activation (format "%s" mode)))
1693 (format "%s" mode))
1695 ;;;_ > allout-setup-menubar ()
1696 (defun allout-setup-menubar ()
1697 "Populate the current buffer's menubar with `allout-mode' stuff."
1698 (let ((menus (list allout-mode-exposure-menu
1699 allout-mode-editing-menu
1700 allout-mode-navigation-menu
1701 allout-mode-misc-menu))
1702 cur)
1703 (while menus
1704 (setq cur (car menus)
1705 menus (cdr menus))
1706 (easy-menu-add cur))))
1707 ;;;_ > allout-overlay-preparations
1708 (defun allout-overlay-preparations ()
1709 "Set the properties of the allout invisible-text overlay and others."
1710 (setplist 'allout-exposure-category nil)
1711 (put 'allout-exposure-category 'invisible 'allout)
1712 (put 'allout-exposure-category 'evaporate t)
1713 ;; ??? We use isearch-open-invisible *and* isearch-mode-end-hook. The
1714 ;; latter would be sufficient, but it seems that a separate behavior --
1715 ;; the _transient_ opening of invisible text during isearch -- is keyed to
1716 ;; presence of the isearch-open-invisible property -- even though this
1717 ;; property controls the isearch _arrival_ behavior. This is the case at
1718 ;; least in emacs 21, 22.1, and xemacs 21.4.
1719 (put 'allout-exposure-category 'isearch-open-invisible
1720 'allout-isearch-end-handler)
1721 (if (featurep 'xemacs)
1722 (put 'allout-exposure-category 'start-open t)
1723 (put 'allout-exposure-category 'insert-in-front-hooks
1724 '(allout-overlay-insert-in-front-handler)))
1725 (put 'allout-exposure-category 'modification-hooks
1726 '(allout-overlay-interior-modification-handler)))
1727 ;;;_ > define-minor-mode allout-mode
1728 ;;;_ : Defun:
1729 ;;;###autoload
1730 (define-minor-mode allout-mode
1731 ;;;_ . Doc string:
1732 "Toggle Allout outline mode.
1733 With a prefix argument ARG, enable Allout outline mode if ARG is
1734 positive, and disable it otherwise. If called from Lisp, enable
1735 the mode if ARG is omitted or nil.
1737 \\<allout-mode-map-value>
1738 Allout outline mode is a minor mode that provides extensive
1739 outline oriented formatting and manipulation. It enables
1740 structural editing of outlines, as well as navigation and
1741 exposure. It also is specifically aimed at accommodating
1742 syntax-sensitive text like programming languages. (For example,
1743 see the allout code itself, which is organized as an allout
1744 outline.)
1746 In addition to typical outline navigation and exposure, allout includes:
1748 - topic-oriented authoring, including keystroke-based topic creation,
1749 repositioning, promotion/demotion, cut, and paste
1750 - incremental search with dynamic exposure and reconcealment of hidden text
1751 - adjustable format, so programming code can be developed in outline-structure
1752 - easy topic encryption and decryption, symmetric or key-pair
1753 - \"Hot-spot\" operation, for single-keystroke maneuvering and exposure control
1754 - integral outline layout, for automatic initial exposure when visiting a file
1755 - independent extensibility, using comprehensive exposure and authoring hooks
1757 and many other features.
1759 Below is a description of the key bindings, and then description
1760 of special `allout-mode' features and terminology. See also the
1761 outline menubar additions for quick reference to many of the
1762 features. Customize `allout-auto-activation' to prepare your
1763 Emacs session for automatic activation of `allout-mode'.
1765 The bindings are those listed in `allout-prefixed-keybindings'
1766 and `allout-unprefixed-keybindings'. We recommend customizing
1767 `allout-command-prefix' to use just `\\C-c' as the command
1768 prefix, if the allout bindings don't conflict with any personal
1769 bindings you have on \\C-c. In any case, outline structure
1770 navigation and authoring is simplified by positioning the cursor
1771 on an item's bullet character, the \"hot-spot\" -- then you can
1772 invoke allout commands with just the un-prefixed,
1773 un-control-shifted command letters. This is described further in
1774 the HOT-SPOT Operation section.
1776 Exposure Control:
1777 ----------------
1778 \\[allout-hide-current-subtree] `allout-hide-current-subtree'
1779 \\[allout-show-children] `allout-show-children'
1780 \\[allout-show-current-subtree] `allout-show-current-subtree'
1781 \\[allout-show-current-entry] `allout-show-current-entry'
1782 \\[allout-show-all] `allout-show-all'
1784 Navigation:
1785 ----------
1786 \\[allout-next-visible-heading] `allout-next-visible-heading'
1787 \\[allout-previous-visible-heading] `allout-previous-visible-heading'
1788 \\[allout-up-current-level] `allout-up-current-level'
1789 \\[allout-forward-current-level] `allout-forward-current-level'
1790 \\[allout-backward-current-level] `allout-backward-current-level'
1791 \\[allout-end-of-entry] `allout-end-of-entry'
1792 \\[allout-beginning-of-current-entry] `allout-beginning-of-current-entry' (alternately, goes to hot-spot)
1793 \\[allout-beginning-of-line] `allout-beginning-of-line' -- like regular beginning-of-line, but
1794 if immediately repeated cycles to the beginning of the current item
1795 and then to the hot-spot (if `allout-beginning-of-line-cycles' is set).
1798 Topic Header Production:
1799 -----------------------
1800 \\[allout-open-sibtopic] `allout-open-sibtopic' Create a new sibling after current topic.
1801 \\[allout-open-subtopic] `allout-open-subtopic' ... an offspring of current topic.
1802 \\[allout-open-supertopic] `allout-open-supertopic' ... a sibling of the current topic's parent.
1804 Topic Level and Prefix Adjustment:
1805 ---------------------------------
1806 \\[allout-shift-in] `allout-shift-in' Shift current topic and all offspring deeper
1807 \\[allout-shift-out] `allout-shift-out' ... less deep
1808 \\[allout-rebullet-current-heading] `allout-rebullet-current-heading' Prompt for alternate bullet for
1809 current topic
1810 \\[allout-rebullet-topic] `allout-rebullet-topic' Reconcile bullets of topic and
1811 its offspring -- distinctive bullets are not changed, others
1812 are alternated according to nesting depth.
1813 \\[allout-number-siblings] `allout-number-siblings' Number bullets of topic and siblings --
1814 the offspring are not affected.
1815 With repeat count, revoke numbering.
1817 Topic-oriented Killing and Yanking:
1818 ----------------------------------
1819 \\[allout-kill-topic] `allout-kill-topic' Kill current topic, including offspring.
1820 \\[allout-copy-topic-as-kill] `allout-copy-topic-as-kill' Copy current topic, including offspring.
1821 \\[allout-kill-line] `allout-kill-line' Kill line, attending to outline structure.
1822 \\[allout-copy-line-as-kill] `allout-copy-line-as-kill' Copy line but don't delete it.
1823 \\[allout-yank] `allout-yank' Yank, adjusting depth of yanked topic to
1824 depth of heading if yanking into bare topic
1825 heading (ie, prefix sans text).
1826 \\[allout-yank-pop] `allout-yank-pop' Is to `allout-yank' as `yank-pop' is to `yank'.
1828 Topic-oriented Encryption:
1829 -------------------------
1830 \\[allout-toggle-current-subtree-encryption] `allout-toggle-current-subtree-encryption'
1831 Encrypt/Decrypt topic content
1833 Misc commands:
1834 -------------
1835 M-x outlineify-sticky Activate outline mode for current buffer,
1836 and establish a default file-var setting
1837 for `allout-layout'.
1838 \\[allout-mark-topic] `allout-mark-topic'
1839 \\[allout-copy-exposed-to-buffer] `allout-copy-exposed-to-buffer'
1840 Duplicate outline, sans concealed text, to
1841 buffer with name derived from derived from that
1842 of current buffer -- \"*BUFFERNAME exposed*\".
1843 \\[allout-flatten-exposed-to-buffer] `allout-flatten-exposed-to-buffer'
1844 Like above 'copy-exposed', but convert topic
1845 prefixes to section.subsection... numeric
1846 format.
1847 \\[customize-variable] allout-auto-activation
1848 Prepare Emacs session for allout outline mode
1849 auto-activation.
1851 Topic Encryption
1853 Outline mode supports gpg encryption of topics, with support for
1854 symmetric and key-pair modes, and auto-encryption of topics
1855 pending encryption on save.
1857 Topics pending encryption are, by default, automatically
1858 encrypted during file saves, including checkpoint saves, to avoid
1859 exposing the plain text of encrypted topics in the file system.
1860 If the content of the topic containing the cursor was encrypted
1861 for a save, it is automatically decrypted for continued editing.
1863 NOTE: A few GnuPG v2 versions improperly preserve incorrect
1864 symmetric decryption keys, preventing entry of the correct key on
1865 subsequent decryption attempts until the cache times-out. That
1866 can take several minutes. (Decryption of other entries is not
1867 affected.) Upgrade your EasyPG version, if you can, and you can
1868 deliberately clear your gpg-agent's cache by sending it a '-HUP'
1869 signal.
1871 See `allout-toggle-current-subtree-encryption' function docstring
1872 and `allout-encrypt-unencrypted-on-saves' customization variable
1873 for details.
1875 HOT-SPOT Operation
1877 Hot-spot operation provides a means for easy, single-keystroke outline
1878 navigation and exposure control.
1880 When the text cursor is positioned directly on the bullet character of
1881 a topic, regular characters (a to z) invoke the commands of the
1882 corresponding allout-mode keymap control chars. For example, \"f\"
1883 would invoke the command typically bound to \"C-c<space>C-f\"
1884 \(\\[allout-forward-current-level] `allout-forward-current-level').
1886 Thus, by positioning the cursor on a topic bullet, you can
1887 execute the outline navigation and manipulation commands with a
1888 single keystroke. Regular navigation keys (eg, \\[forward-char], \\[next-line]) don't get
1889 this special translation, so you can use them to get out of the
1890 hot-spot and back to normal editing operation.
1892 In allout-mode, the normal beginning-of-line command (\\[allout-beginning-of-line]) is
1893 replaced with one that makes it easy to get to the hot-spot. If you
1894 repeat it immediately it cycles (if `allout-beginning-of-line-cycles'
1895 is set) to the beginning of the item and then, if you hit it again
1896 immediately, to the hot-spot. Similarly, `allout-beginning-of-current-entry'
1897 \(\\[allout-beginning-of-current-entry]) moves to the hot-spot when the cursor is already located
1898 at the beginning of the current entry.
1900 Extending Allout
1902 Allout exposure and authoring activities all have associated
1903 hooks, by which independent code can cooperate with allout
1904 without changes to the allout core. Here are key ones:
1906 `allout-mode-hook'
1907 `allout-mode-deactivate-hook' (deprecated)
1908 `allout-mode-off-hook'
1909 `allout-exposure-change-functions'
1910 `allout-structure-added-functions'
1911 `allout-structure-deleted-functions'
1912 `allout-structure-shifted-functions'
1913 `allout-after-copy-or-kill-hook'
1914 `allout-post-undo-hook'
1916 Terminology
1918 Topic hierarchy constituents -- TOPICS and SUBTOPICS:
1920 ITEM: A unitary outline element, including the HEADER and ENTRY text.
1921 TOPIC: An ITEM and any ITEMs contained within it, ie having greater DEPTH
1922 and with no intervening items of lower DEPTH than the container.
1923 CURRENT ITEM:
1924 The visible ITEM most immediately containing the cursor.
1925 DEPTH: The degree of nesting of an ITEM; it increases with containment.
1926 The DEPTH is determined by the HEADER PREFIX. The DEPTH is also
1927 called the:
1928 LEVEL: The same as DEPTH.
1930 ANCESTORS:
1931 Those ITEMs whose TOPICs contain an ITEM.
1932 PARENT: An ITEM's immediate ANCESTOR. It has a DEPTH one less than that
1933 of the ITEM.
1934 OFFSPRING:
1935 The ITEMs contained within an ITEM's TOPIC.
1936 SUBTOPIC:
1937 An OFFSPRING of its ANCESTOR TOPICs.
1938 CHILD:
1939 An immediate SUBTOPIC of its PARENT.
1940 SIBLINGS:
1941 TOPICs having the same PARENT and DEPTH.
1943 Topic text constituents:
1945 HEADER: The first line of an ITEM, include the ITEM PREFIX and HEADER
1946 text.
1947 ENTRY: The text content of an ITEM, before any OFFSPRING, but including
1948 the HEADER text and distinct from the ITEM PREFIX.
1949 BODY: Same as ENTRY.
1950 PREFIX: The leading text of an ITEM which distinguishes it from normal
1951 ENTRY text. Allout recognizes the outline structure according
1952 to the strict PREFIX format. It consists of a PREFIX-LEAD string,
1953 PREFIX-PADDING, and a BULLET. The BULLET might be followed by a
1954 number, indicating the ordinal number of the topic among its
1955 siblings, or an asterisk indicating encryption, plus an optional
1956 space. After that is the ITEM HEADER text, which is not part of
1957 the PREFIX.
1959 The relative length of the PREFIX determines the nesting DEPTH
1960 of the ITEM.
1961 PREFIX-LEAD:
1962 The string at the beginning of a HEADER PREFIX, by default a `.'.
1963 It can be customized by changing the setting of
1964 `allout-header-prefix' and then reinitializing `allout-mode'.
1966 When the PREFIX-LEAD is set to the comment-string of a
1967 programming language, outline structuring can be embedded in
1968 program code without interfering with processing of the text
1969 (by Emacs or the language processor) as program code. This
1970 setting happens automatically when allout mode is used in
1971 programming-mode buffers. See `allout-use-mode-specific-leader'
1972 docstring for more detail.
1973 PREFIX-PADDING:
1974 Spaces or asterisks which separate the PREFIX-LEAD and the
1975 bullet, determining the ITEM's DEPTH.
1976 BULLET: A character at the end of the ITEM PREFIX, it must be one of
1977 the characters listed on `allout-plain-bullets-string' or
1978 `allout-distinctive-bullets-string'. When creating a TOPIC,
1979 plain BULLETs are by default used, according to the DEPTH of the
1980 TOPIC. Choice among the distinctive BULLETs is offered when you
1981 provide a universal argument (\\[universal-argument]) to the
1982 TOPIC creation command, or when explicitly rebulleting a TOPIC. The
1983 significance of the various distinctive bullets is purely by
1984 convention. See the documentation for the above bullet strings for
1985 more details.
1986 EXPOSURE:
1987 The state of a TOPIC which determines the on-screen visibility
1988 of its OFFSPRING and contained ENTRY text.
1989 CONCEALED:
1990 TOPICs and ENTRY text whose EXPOSURE is inhibited. Concealed
1991 text is represented by \"...\" ellipses.
1993 CONCEALED TOPICs are effectively collapsed within an ANCESTOR.
1994 CLOSED: A TOPIC whose immediate OFFSPRING and body-text is CONCEALED.
1995 OPEN: A TOPIC that is not CLOSED, though its OFFSPRING or BODY may be."
1996 ;;;_ . Code
1997 :lighter " Allout"
1998 :keymap 'allout-mode-map
2000 (let ((use-layout (if (listp allout-layout)
2001 allout-layout
2002 allout-default-layout)))
2004 (if (not (allout-mode-p))
2005 (progn
2006 ;; Deactivation:
2008 ; Activation not explicitly
2009 ; requested, and either in
2010 ; active state or *de*activation
2011 ; specifically requested:
2012 (allout-do-resumptions)
2014 (remove-from-invisibility-spec '(allout . t))
2015 (remove-hook 'pre-command-hook 'allout-pre-command-business t)
2016 (remove-hook 'post-command-hook 'allout-post-command-business t)
2017 (remove-hook 'before-change-functions 'allout-before-change-handler t)
2018 (remove-hook 'isearch-mode-end-hook 'allout-isearch-end-handler t)
2019 (remove-hook 'write-contents-functions
2020 'allout-write-contents-hook-handler t)
2022 (remove-overlays (point-min) (point-max)
2023 'category 'allout-exposure-category))
2025 ;; Activating:
2026 (if allout-old-style-prefixes
2027 ;; Inhibit all the fancy formatting:
2028 (allout-add-resumptions '(allout-primary-bullet "*")))
2030 (allout-overlay-preparations) ; Doesn't hurt to redo this.
2032 (allout-infer-header-lead-and-primary-bullet)
2033 (allout-infer-body-reindent)
2035 (set-allout-regexp)
2036 (allout-add-resumptions '(allout-encryption-ciphertext-rejection-regexps
2037 allout-line-boundary-regexp
2038 extend)
2039 '(allout-encryption-ciphertext-rejection-regexps
2040 allout-bob-regexp
2041 extend))
2043 (allout-compose-and-institute-keymap)
2044 (produce-allout-mode-menubar-entries)
2046 (add-to-invisibility-spec '(allout . t))
2048 (allout-add-resumptions '(line-move-ignore-invisible t))
2049 (add-hook 'pre-command-hook 'allout-pre-command-business nil t)
2050 (add-hook 'post-command-hook 'allout-post-command-business nil t)
2051 (add-hook 'before-change-functions 'allout-before-change-handler nil t)
2052 (add-hook 'isearch-mode-end-hook 'allout-isearch-end-handler nil t)
2053 (add-hook 'write-contents-functions 'allout-write-contents-hook-handler
2054 nil t)
2056 ;; Stash auto-fill settings and adjust so custom allout auto-fill
2057 ;; func will be used if auto-fill is active or activated. (The
2058 ;; custom func respects topic headline, maintains hanging-indents,
2059 ;; etc.)
2060 (allout-add-resumptions (list 'allout-former-auto-filler
2061 auto-fill-function)
2062 ;; Register allout-auto-fill to be used if
2063 ;; filling is active:
2064 (list 'allout-outside-normal-auto-fill-function
2065 normal-auto-fill-function)
2066 '(normal-auto-fill-function allout-auto-fill)
2067 ;; Paragraphs are broken by topic headlines.
2068 (list 'paragraph-start
2069 (concat paragraph-start "\\|^\\("
2070 allout-regexp "\\)"))
2071 (list 'paragraph-separate
2072 (concat paragraph-separate "\\|^\\("
2073 allout-regexp "\\)")))
2074 (if (and auto-fill-function (not allout-inhibit-auto-fill))
2075 ;; allout-auto-fill will use the stashed values and so forth.
2076 (allout-add-resumptions '(auto-fill-function allout-auto-fill)))
2078 (allout-setup-menubar)
2080 ;; Do auto layout if warranted:
2081 (when (and allout-layout
2082 allout-auto-activation
2083 use-layout
2084 (and (not (string= allout-auto-activation "activate"))
2085 (if (string= allout-auto-activation "ask")
2086 (if (y-or-n-p (format "Expose %s with layout '%s'? "
2087 (buffer-name)
2088 use-layout))
2090 (message "Skipped %s layout." (buffer-name))
2091 nil)
2092 t)))
2093 (save-excursion
2094 (message "Adjusting '%s' exposure..." (buffer-name))
2095 (goto-char 0)
2096 (allout-this-or-next-heading)
2097 (condition-case err
2098 (progn
2099 (apply 'allout-expose-topic (list use-layout))
2100 (message "Adjusting '%s' exposure... done."
2101 (buffer-name)))
2102 ;; Problem applying exposure -- notify user, but don't
2103 ;; interrupt, eg, file visit:
2104 (error (message "%s" (car (cdr err)))
2105 (sit-for 1))))
2106 ) ; when allout-layout
2107 ) ; if (allout-mode-p)
2108 ) ; let (())
2109 ) ; define-minor-mode
2110 ;;;_ > allout-minor-mode alias
2111 (defalias 'allout-minor-mode 'allout-mode)
2112 ;;;_ > allout-unload-function
2113 (defun allout-unload-function ()
2114 "Unload the allout outline library."
2115 (save-current-buffer
2116 (dolist (buffer (buffer-list))
2117 (set-buffer buffer)
2118 (when (allout-mode-p) (allout-mode -1))))
2119 ;; continue standard unloading
2120 nil)
2122 ;;;_ - Position Assessment
2123 ;;;_ > allout-hidden-p (&optional pos)
2124 (defsubst allout-hidden-p (&optional pos)
2125 "Non-nil if the character after point was made invisible by allout."
2126 (eq (get-char-property (or pos (point)) 'invisible) 'allout))
2128 ;;;_ > allout-overlay-insert-in-front-handler (ol after beg end
2129 ;;; &optional prelen)
2130 (defun allout-overlay-insert-in-front-handler (ol after beg end
2131 &optional prelen)
2132 "Shift the overlay so stuff inserted in front of it is excluded."
2133 (if after
2134 ;; ??? Shouldn't moving the overlay should be unnecessary, if overlay
2135 ;; front-advance on the overlay worked as expected?
2136 (move-overlay ol (1+ beg) (overlay-end ol))))
2137 ;;;_ > allout-overlay-interior-modification-handler (ol after beg end
2138 ;;; &optional prelen)
2139 (defun allout-overlay-interior-modification-handler (ol after beg end
2140 &optional prelen)
2141 "Get confirmation before making arbitrary changes to invisible text.
2143 We expose the invisible text and ask for confirmation. Refusal or
2144 `keyboard-quit' abandons the changes, with keyboard-quit additionally
2145 reclosing the opened text.
2147 No confirmation is necessary when `inhibit-read-only' is set -- eg, allout
2148 internal functions use this feature cohesively bunch changes."
2150 (when (and (not inhibit-read-only) (not after))
2151 (let ((start (point))
2152 (ol-start (overlay-start ol))
2153 (ol-end (overlay-end ol))
2154 first)
2155 (goto-char beg)
2156 (while (< (point) end)
2157 (when (allout-hidden-p)
2158 (allout-show-to-offshoot)
2159 (if (allout-hidden-p)
2160 (save-excursion (forward-char 1)
2161 (allout-show-to-offshoot)))
2162 (when (not first)
2163 (setq first (point))))
2164 (goto-char (if (featurep 'xemacs)
2165 (next-property-change (1+ (point)) nil end)
2166 (next-char-property-change (1+ (point)) end))))
2167 (when first
2168 (goto-char first)
2169 (condition-case nil
2170 (if (not
2171 (yes-or-no-p
2172 (substitute-command-keys
2173 (concat "Modify concealed text? (\"no\" just aborts,"
2174 " \\[keyboard-quit] also reconceals) "))))
2175 (progn (goto-char start)
2176 (error "Concealed-text change refused")))
2177 (quit (allout-flag-region ol-start ol-end nil)
2178 (allout-flag-region ol-start ol-end t)
2179 (error "Concealed-text change abandoned, text reconcealed"))))
2180 (goto-char start))))
2181 ;;;_ > allout-before-change-handler (beg end)
2182 (defun allout-before-change-handler (beg end)
2183 "Protect against changes to invisible text.
2185 See `allout-overlay-interior-modification-handler' for details."
2187 (when (and (allout-mode-p) undo-in-progress)
2188 (setq allout-just-did-undo t)
2189 (if (allout-hidden-p)
2190 (allout-show-children)))
2192 ;; allout-overlay-interior-modification-handler on an overlay handles
2193 ;; this in other emacs, via `allout-exposure-category's 'modification-hooks.
2194 (when (and (featurep 'xemacs) (allout-mode-p))
2195 ;; process all of the pending overlays:
2196 (save-excursion
2197 (goto-char beg)
2198 (let ((overlay (allout-get-invisibility-overlay)))
2199 (if overlay
2200 (allout-overlay-interior-modification-handler
2201 overlay nil beg end nil))))))
2202 ;;;_ > allout-isearch-end-handler (&optional overlay)
2203 (defun allout-isearch-end-handler (&optional overlay)
2204 "Reconcile allout outline exposure on arriving in hidden text after isearch.
2206 Optional OVERLAY parameter is for when this function is used by
2207 `isearch-open-invisible' overlay property. It is otherwise unused, so this
2208 function can also be used as an `isearch-mode-end-hook'."
2210 (if (and (allout-mode-p) (allout-hidden-p))
2211 (allout-show-to-offshoot)))
2213 ;;;_ #3 Internal Position State-Tracking -- "allout-recent-*" funcs
2214 ;; All the basic outline functions that directly do string matches to
2215 ;; evaluate heading prefix location set the variables
2216 ;; `allout-recent-prefix-beginning' and `allout-recent-prefix-end'
2217 ;; when successful. Functions starting with `allout-recent-' all
2218 ;; use this state, providing the means to avoid redundant searches
2219 ;; for just-established data. This optimization can provide
2220 ;; significant speed improvement, but it must be employed carefully.
2221 ;;;_ = allout-recent-prefix-beginning
2222 (defvar allout-recent-prefix-beginning 0
2223 "Buffer point of the start of the last topic prefix encountered.")
2224 (make-variable-buffer-local 'allout-recent-prefix-beginning)
2225 ;;;_ = allout-recent-prefix-end
2226 (defvar allout-recent-prefix-end 0
2227 "Buffer point of the end of the last topic prefix encountered.")
2228 (make-variable-buffer-local 'allout-recent-prefix-end)
2229 ;;;_ = allout-recent-depth
2230 (defvar allout-recent-depth 0
2231 "Depth of the last topic prefix encountered.")
2232 (make-variable-buffer-local 'allout-recent-depth)
2233 ;;;_ = allout-recent-end-of-subtree
2234 (defvar allout-recent-end-of-subtree 0
2235 "Buffer point last returned by `allout-end-of-current-subtree'.")
2236 (make-variable-buffer-local 'allout-recent-end-of-subtree)
2237 ;;;_ > allout-prefix-data ()
2238 (defsubst allout-prefix-data ()
2239 "Register allout-prefix state data.
2241 For reference by `allout-recent' funcs. Return
2242 the new value of `allout-recent-prefix-beginning'."
2243 (setq allout-recent-prefix-end (or (match-end 1) (match-end 2) (match-end 3))
2244 allout-recent-prefix-beginning (or (match-beginning 1)
2245 (match-beginning 2)
2246 (match-beginning 3))
2247 allout-recent-depth (max 1 (- allout-recent-prefix-end
2248 allout-recent-prefix-beginning
2249 allout-header-subtraction)))
2250 allout-recent-prefix-beginning)
2251 ;;;_ > nullify-allout-prefix-data ()
2252 (defsubst nullify-allout-prefix-data ()
2253 "Mark allout prefix data as being uninformative."
2254 (setq allout-recent-prefix-end (point)
2255 allout-recent-prefix-beginning (point)
2256 allout-recent-depth 0)
2257 allout-recent-prefix-beginning)
2258 ;;;_ > allout-recent-depth ()
2259 (defsubst allout-recent-depth ()
2260 "Return depth of last heading encountered by an outline maneuvering function.
2262 All outline functions which directly do string matches to assess
2263 headings set the variables `allout-recent-prefix-beginning' and
2264 `allout-recent-prefix-end' if successful. This function uses those settings
2265 to return the current depth."
2267 allout-recent-depth)
2268 ;;;_ > allout-recent-prefix ()
2269 (defsubst allout-recent-prefix ()
2270 "Like `allout-recent-depth', but returns text of last encountered prefix.
2272 All outline functions which directly do string matches to assess
2273 headings set the variables `allout-recent-prefix-beginning' and
2274 `allout-recent-prefix-end' if successful. This function uses those settings
2275 to return the current prefix."
2276 (buffer-substring-no-properties allout-recent-prefix-beginning
2277 allout-recent-prefix-end))
2278 ;;;_ > allout-recent-bullet ()
2279 (defmacro allout-recent-bullet ()
2280 "Like `allout-recent-prefix', but returns bullet of last encountered prefix.
2282 All outline functions which directly do string matches to assess
2283 headings set the variables `allout-recent-prefix-beginning' and
2284 `allout-recent-prefix-end' if successful. This function uses those settings
2285 to return the current depth of the most recently matched topic."
2286 '(buffer-substring-no-properties (1- allout-recent-prefix-end)
2287 allout-recent-prefix-end))
2289 ;;;_ #4 Navigation
2291 ;;;_ - Position Assessment
2292 ;;;_ : Location Predicates
2293 ;;;_ > allout-do-doublecheck ()
2294 (defsubst allout-do-doublecheck ()
2295 "True if current item conditions qualify for checking on topic aberrance."
2296 (and
2297 ;; presume integrity of outline and yanked content during yank -- necessary
2298 ;; to allow for level disparity of yank location and yanked text:
2299 (not allout-inhibit-aberrance-doublecheck)
2300 ;; allout-doublecheck-at-and-shallower is ceiling for doublecheck:
2301 (<= allout-recent-depth allout-doublecheck-at-and-shallower)))
2302 ;;;_ > allout-aberrant-container-p ()
2303 (defun allout-aberrant-container-p ()
2304 "True if topic, or next sibling with children, contains them discontinuously.
2306 Discontinuous means an immediate offspring that is nested more
2307 than one level deeper than the topic.
2309 If topic has no offspring, then the next sibling with offspring will
2310 determine whether or not this one is determined to be aberrant.
2312 If true, then the allout-recent-* settings are calibrated on the
2313 offspring that qualifies it as aberrant, ie with depth that
2314 exceeds the topic by more than one."
2316 ;; This is most clearly understood when considering standard-prefix-leader
2317 ;; low-level topics, which can all too easily match text not intended as
2318 ;; headers. For example, any line with a leading '.' or '*' and lacking a
2319 ;; following bullet qualifies without this protection. (A sequence of
2320 ;; them can occur naturally, eg a typical textual bullet list.) We
2321 ;; disqualify such low-level sequences when they are followed by a
2322 ;; discontinuously contained child, inferring that the sequences are not
2323 ;; actually connected with their prospective context.
2325 (let ((depth (allout-depth))
2326 (start-point (point))
2327 done aberrant)
2328 (save-match-data
2329 (save-excursion
2330 (while (and (not done)
2331 (re-search-forward allout-line-boundary-regexp nil 0))
2332 (allout-prefix-data)
2333 (goto-char allout-recent-prefix-beginning)
2334 (cond
2335 ;; sibling -- continue:
2336 ((eq allout-recent-depth depth))
2337 ;; first offspring is excessive -- aberrant:
2338 ((> allout-recent-depth (1+ depth))
2339 (setq done t aberrant t))
2340 ;; next non-sibling is lower-depth -- not aberrant:
2341 (t (setq done t))))))
2342 (if aberrant
2343 aberrant
2344 (goto-char start-point)
2345 ;; recalibrate allout-recent-*
2346 (allout-depth)
2347 nil)))
2348 ;;;_ > allout-on-current-heading-p ()
2349 (defun allout-on-current-heading-p ()
2350 "Return non-nil if point is on current visible topics' header line.
2352 Actually, returns prefix beginning point."
2353 (save-excursion
2354 (allout-beginning-of-current-line)
2355 (save-match-data
2356 (and (looking-at allout-regexp)
2357 (allout-prefix-data)
2358 (or (not (allout-do-doublecheck))
2359 (not (allout-aberrant-container-p)))))))
2360 ;;;_ > allout-on-heading-p ()
2361 (defalias 'allout-on-heading-p 'allout-on-current-heading-p)
2362 ;;;_ > allout-e-o-prefix-p ()
2363 (defun allout-e-o-prefix-p ()
2364 "True if point is located where current topic prefix ends, heading begins."
2365 (and (save-match-data
2366 (save-excursion (let ((inhibit-field-text-motion t))
2367 (beginning-of-line))
2368 (looking-at allout-regexp))
2369 (= (point) (save-excursion (allout-end-of-prefix)(point))))))
2370 ;;;_ : Location attributes
2371 ;;;_ > allout-depth ()
2372 (defun allout-depth ()
2373 "Return depth of topic most immediately containing point.
2375 Does not do doublecheck for aberrant topic header.
2377 Return zero if point is not within any topic.
2379 Like `allout-current-depth', but respects hidden as well as visible topics."
2380 (save-excursion
2381 (let ((start-point (point)))
2382 (if (and (allout-goto-prefix)
2383 (not (< start-point (point))))
2384 allout-recent-depth
2385 (progn
2386 ;; Oops, no prefix, nullify it:
2387 (nullify-allout-prefix-data)
2388 ;; ... and return 0:
2389 0)))))
2390 ;;;_ > allout-current-depth ()
2391 (defun allout-current-depth ()
2392 "Return depth of visible topic most immediately containing point.
2394 Return zero if point is not within any topic."
2395 (save-excursion
2396 (if (allout-back-to-current-heading)
2397 (max 1
2398 (- allout-recent-prefix-end
2399 allout-recent-prefix-beginning
2400 allout-header-subtraction))
2401 0)))
2402 ;;;_ > allout-get-current-prefix ()
2403 (defun allout-get-current-prefix ()
2404 "Topic prefix of the current topic."
2405 (save-excursion
2406 (if (allout-goto-prefix)
2407 (allout-recent-prefix))))
2408 ;;;_ > allout-get-bullet ()
2409 (defun allout-get-bullet ()
2410 "Return bullet of containing topic (visible or not)."
2411 (save-excursion
2412 (and (allout-goto-prefix)
2413 (allout-recent-bullet))))
2414 ;;;_ > allout-current-bullet ()
2415 (defun allout-current-bullet ()
2416 "Return bullet of current (visible) topic heading, or none if none found."
2417 (condition-case nil
2418 (save-excursion
2419 (allout-back-to-current-heading)
2420 (buffer-substring-no-properties (- allout-recent-prefix-end 1)
2421 allout-recent-prefix-end))
2422 ;; Quick and dirty provision, ostensibly for missing bullet:
2423 (args-out-of-range nil))
2425 ;;;_ > allout-get-prefix-bullet (prefix)
2426 (defun allout-get-prefix-bullet (prefix)
2427 "Return the bullet of the header prefix string PREFIX."
2428 ;; Doesn't make sense if we're old-style prefixes, but this just
2429 ;; oughtn't be called then, so forget about it...
2430 (if (string-match allout-regexp prefix)
2431 (substring prefix (1- (match-end 2)) (match-end 2))))
2432 ;;;_ > allout-sibling-index (&optional depth)
2433 (defun allout-sibling-index (&optional depth)
2434 "Item number of this prospective topic among its siblings.
2436 If optional arg DEPTH is greater than current depth, then we're
2437 opening a new level, and return 0.
2439 If less than this depth, ascend to that depth and count..."
2441 (save-excursion
2442 (cond ((and depth (<= depth 0) 0))
2443 ((or (null depth) (= depth (allout-depth)))
2444 (let ((index 1))
2445 (while (allout-previous-sibling allout-recent-depth nil)
2446 (setq index (1+ index)))
2447 index))
2448 ((< depth allout-recent-depth)
2449 (allout-ascend-to-depth depth)
2450 (allout-sibling-index))
2451 (0))))
2452 ;;;_ > allout-topic-flat-index ()
2453 (defun allout-topic-flat-index ()
2454 "Return a list indicating point's numeric section.subsect.subsubsect...
2455 Outermost is first."
2456 (let* ((depth (allout-depth))
2457 (next-index (allout-sibling-index depth))
2458 (rev-sibls nil))
2459 (while (> next-index 0)
2460 (setq rev-sibls (cons next-index rev-sibls))
2461 (setq depth (1- depth))
2462 (setq next-index (allout-sibling-index depth)))
2463 rev-sibls)
2466 ;;;_ - Navigation routines
2467 ;;;_ > allout-beginning-of-current-line ()
2468 (defun allout-beginning-of-current-line ()
2469 "Like beginning of line, but to visible text."
2471 ;; This combination of move-beginning-of-line and beginning-of-line is
2472 ;; deliberate, but the (beginning-of-line) may now be superfluous.
2473 (let ((inhibit-field-text-motion t))
2474 (move-beginning-of-line 1)
2475 (beginning-of-line)
2476 (while (and (not (bobp)) (or (not (bolp)) (allout-hidden-p)))
2477 (beginning-of-line)
2478 (if (or (allout-hidden-p) (not (bolp)))
2479 (forward-char -1)))))
2480 ;;;_ > allout-end-of-current-line ()
2481 (defun allout-end-of-current-line ()
2482 "Move to the end of line, past concealed text if any."
2483 ;; This is for symmetry with `allout-beginning-of-current-line' --
2484 ;; `move-end-of-line' doesn't suffer the same problem as
2485 ;; `move-beginning-of-line'.
2486 (let ((inhibit-field-text-motion t))
2487 (end-of-line)
2488 (while (allout-hidden-p)
2489 (end-of-line)
2490 (if (allout-hidden-p) (forward-char 1)))))
2491 ;;;_ > allout-beginning-of-line ()
2492 (defun allout-beginning-of-line ()
2493 "Beginning-of-line with `allout-beginning-of-line-cycles' behavior, if set."
2495 (interactive)
2497 (if (or (not allout-beginning-of-line-cycles)
2498 (not (equal last-command this-command)))
2499 (progn
2500 (if (and (not (bolp))
2501 (allout-hidden-p (1- (point))))
2502 (goto-char (allout-previous-single-char-property-change
2503 (1- (point)) 'invisible)))
2504 (move-beginning-of-line 1))
2505 (allout-depth)
2506 (let ((beginning-of-body
2507 (save-excursion
2508 (while (and (allout-do-doublecheck)
2509 (allout-aberrant-container-p)
2510 (allout-previous-visible-heading 1)))
2511 (allout-beginning-of-current-entry)
2512 (point))))
2513 (cond ((= (current-column) 0)
2514 (goto-char beginning-of-body))
2515 ((< (point) beginning-of-body)
2516 (allout-beginning-of-current-line))
2517 ((= (point) beginning-of-body)
2518 (goto-char (allout-current-bullet-pos)))
2519 (t (allout-beginning-of-current-line)
2520 (if (< (point) beginning-of-body)
2521 ;; we were on the headline after its start:
2522 (goto-char beginning-of-body)))))))
2523 ;;;_ > allout-end-of-line ()
2524 (defun allout-end-of-line ()
2525 "End-of-line with `allout-end-of-line-cycles' behavior, if set."
2527 (interactive)
2529 (if (or (not allout-end-of-line-cycles)
2530 (not (equal last-command this-command)))
2531 (allout-end-of-current-line)
2532 (let ((end-of-entry (save-excursion
2533 (allout-end-of-entry)
2534 (point))))
2535 (cond ((not (eolp))
2536 (allout-end-of-current-line))
2537 ((or (allout-hidden-p) (save-excursion
2538 (forward-char -1)
2539 (allout-hidden-p)))
2540 (allout-back-to-current-heading)
2541 (allout-show-current-entry)
2542 (allout-show-children)
2543 (allout-end-of-entry))
2544 ((>= (point) end-of-entry)
2545 (allout-back-to-current-heading)
2546 (allout-end-of-current-line))
2548 (if (not (allout-mark-active-p))
2549 (push-mark))
2550 (allout-end-of-entry))))))
2551 ;;;_ > allout-mark-active-p ()
2552 (defun allout-mark-active-p ()
2553 "True if the mark is currently or always active."
2554 ;; `(cond (boundp...))' (or `(if ...)') invokes special byte-compiler
2555 ;; provisions, at least in GNU Emacs to prevent warnings about lack of,
2556 ;; eg, region-active-p.
2557 (cond ((boundp 'mark-active)
2558 mark-active)
2559 ((fboundp 'region-active-p)
2560 (region-active-p))
2561 (t)))
2562 ;;;_ > allout-next-heading ()
2563 (defsubst allout-next-heading ()
2564 "Move to the heading for the topic (possibly invisible) after this one.
2566 Returns the location of the heading, or nil if none found.
2568 We skip anomalous low-level topics, a la `allout-aberrant-container-p'."
2569 (save-match-data
2571 (if (looking-at allout-regexp)
2572 (forward-char 1))
2574 (when (re-search-forward allout-line-boundary-regexp nil 0)
2575 (allout-prefix-data)
2576 (goto-char allout-recent-prefix-beginning)
2577 (while (not (bolp))
2578 (forward-char -1))
2579 (and (allout-do-doublecheck)
2580 ;; this will set allout-recent-* on the first non-aberrant topic,
2581 ;; whether it's the current one or one that disqualifies it:
2582 (allout-aberrant-container-p))
2583 ;; this may or may not be the same as above depending on doublecheck:
2584 (goto-char allout-recent-prefix-beginning))))
2585 ;;;_ > allout-this-or-next-heading
2586 (defun allout-this-or-next-heading ()
2587 "Position cursor on current or next heading."
2588 ;; A throwaway non-macro that is defined after allout-next-heading
2589 ;; and usable by allout-mode.
2590 (if (not (allout-goto-prefix-doublechecked)) (allout-next-heading)))
2591 ;;;_ > allout-previous-heading ()
2592 (defun allout-previous-heading ()
2593 "Move to the prior (possibly invisible) heading line.
2595 Return the location of the beginning of the heading, or nil if not found.
2597 We skip anomalous low-level topics, a la `allout-aberrant-container-p'."
2599 (if (bobp)
2601 (let ((start-point (point)))
2602 ;; allout-goto-prefix-doublechecked calls us, so we can't use it here.
2603 (allout-goto-prefix)
2604 (save-match-data
2605 (when (or (re-search-backward allout-line-boundary-regexp nil 0)
2606 (looking-at allout-bob-regexp))
2607 (goto-char (allout-prefix-data))
2608 (if (and (allout-do-doublecheck)
2609 (allout-aberrant-container-p))
2610 (or (allout-previous-heading)
2611 (and (goto-char start-point)
2612 ;; recalibrate allout-recent-*:
2613 (allout-depth)
2614 nil))
2615 (point)))))))
2616 ;;;_ > allout-get-invisibility-overlay ()
2617 (defun allout-get-invisibility-overlay ()
2618 "Return the overlay at point that dictates allout invisibility."
2619 (let ((overlays (overlays-at (point)))
2620 got)
2621 (while (and overlays (not got))
2622 (if (equal (overlay-get (car overlays) 'invisible) 'allout)
2623 (setq got (car overlays))
2624 (pop overlays)))
2625 got))
2626 ;;;_ > allout-back-to-visible-text ()
2627 (defun allout-back-to-visible-text ()
2628 "Move to most recent prior character that is visible, and return point."
2629 (if (allout-hidden-p)
2630 (goto-char (overlay-start (allout-get-invisibility-overlay))))
2631 (point))
2633 ;;;_ - Subtree Charting
2634 ;;;_ " These routines either produce or assess charts, which are
2635 ;;; nested lists of the locations of topics within a subtree.
2637 ;;; Charts enable efficient subtree navigation by providing a reusable basis
2638 ;;; for elaborate, compound assessment and adjustment of a subtree.
2640 ;;;_ > allout-chart-subtree (&optional levels visible orig-depth prev-depth)
2641 (defun allout-chart-subtree (&optional levels visible orig-depth prev-depth)
2642 "Produce a location \"chart\" of subtopics of the containing topic.
2644 Optional argument LEVELS specifies a depth limit (relative to start
2645 depth) for the chart. Null LEVELS means no limit.
2647 When optional argument VISIBLE is non-nil, the chart includes
2648 only the visible subelements of the charted subjects.
2650 The remaining optional args are for internal use by the function.
2652 Point is left at the end of the subtree.
2654 Charts are used to capture outline structure, so that outline-altering
2655 routines need to assess the structure only once, and then use the chart
2656 for their elaborate manipulations.
2658 The chart entries for the topics are in reverse order, so the
2659 last topic is listed first. The entry for each topic consists of
2660 an integer indicating the point at the beginning of the topic
2661 prefix. Charts for offspring consist of a list containing,
2662 recursively, the charts for the respective subtopics. The chart
2663 for a topics' offspring precedes the entry for the topic itself.
2665 The other function parameters are for internal recursion, and should
2666 not be specified by external callers. ORIG-DEPTH is depth of topic at
2667 starting point, and PREV-DEPTH is depth of prior topic."
2669 (let ((original (not orig-depth)) ; `orig-depth' set only in recursion.
2670 chart curr-depth)
2672 (if original ; Just starting?
2673 ; Register initial settings and
2674 ; position to first offspring:
2675 (progn (setq orig-depth (allout-depth))
2676 (or prev-depth (setq prev-depth (1+ orig-depth)))
2677 (if visible
2678 (allout-next-visible-heading 1)
2679 (allout-next-heading))))
2681 ;; Loop over the current levels' siblings. Besides being more
2682 ;; efficient than tail-recursing over a level, it avoids exceeding
2683 ;; the typically quite constrained Emacs max-lisp-eval-depth.
2685 ;; Probably would speed things up to implement loop-based stack
2686 ;; operation rather than recursing for lower levels. Bah.
2688 (while (and (not (eobp))
2689 ; Still within original topic?
2690 (< orig-depth (setq curr-depth allout-recent-depth))
2691 (cond ((= prev-depth curr-depth)
2692 ;; Register this one and move on:
2693 (setq chart (cons allout-recent-prefix-beginning chart))
2694 (if (and levels (<= levels 1))
2695 ;; At depth limit -- skip sublevels:
2696 (or (allout-next-sibling curr-depth)
2697 ;; or no more siblings -- proceed to
2698 ;; next heading at lesser depth:
2699 (while (and (<= curr-depth
2700 allout-recent-depth)
2701 (if visible
2702 (allout-next-visible-heading 1)
2703 (allout-next-heading)))))
2704 (if visible
2705 (allout-next-visible-heading 1)
2706 (allout-next-heading))))
2708 ((and (< prev-depth curr-depth)
2709 (or (not levels)
2710 (> levels 0)))
2711 ;; Recurse on deeper level of curr topic:
2712 (setq chart
2713 (cons (allout-chart-subtree (and levels
2714 (1- levels))
2715 visible
2716 orig-depth
2717 curr-depth)
2718 chart))
2719 ;; ... then continue with this one.
2722 ;; ... else nil if we've ascended back to prev-depth.
2726 (if original ; We're at the last sibling on
2727 ; the original level. Position
2728 ; to the end of it:
2729 (progn (and (not (eobp)) (forward-char -1))
2730 (and (= (preceding-char) ?\n)
2731 (= (aref (buffer-substring (max 1 (- (point) 3))
2732 (point))
2734 ?\n)
2735 (forward-char -1))
2736 (setq allout-recent-end-of-subtree (point))))
2738 chart ; (nreverse chart) not necessary,
2739 ; and maybe not preferable.
2741 ;;;_ > allout-chart-siblings (&optional start end)
2742 (defun allout-chart-siblings (&optional start end)
2743 "Produce a list of locations of this and succeeding sibling topics.
2744 Effectively a top-level chart of siblings. See `allout-chart-subtree'
2745 for an explanation of charts."
2746 (save-excursion
2747 (when (allout-goto-prefix-doublechecked)
2748 (let ((chart (list (point))))
2749 (while (allout-next-sibling)
2750 (setq chart (cons (point) chart)))
2751 (if chart (setq chart (nreverse chart)))))))
2752 ;;;_ > allout-chart-to-reveal (chart depth)
2753 (defun allout-chart-to-reveal (chart depth)
2755 "Return a flat list of hidden points in subtree CHART, up to DEPTH.
2757 If DEPTH is nil, include hidden points at any depth.
2759 Note that point can be left at any of the points on chart, or at the
2760 start point."
2762 (let (result here)
2763 (while (and (or (null depth) (> depth 0))
2764 chart)
2765 (setq here (car chart))
2766 (if (listp here)
2767 (let ((further (allout-chart-to-reveal here (if (null depth)
2768 depth
2769 (1- depth)))))
2770 ;; We're on the start of a subtree -- recurse with it, if there's
2771 ;; more depth to go:
2772 (if further (setq result (append further result)))
2773 (setq chart (cdr chart)))
2774 (goto-char here)
2775 (if (allout-hidden-p)
2776 (setq result (cons here result)))
2777 (setq chart (cdr chart))))
2778 result))
2779 ;;;_ X allout-chart-spec (chart spec &optional exposing)
2780 ;; (defun allout-chart-spec (chart spec &optional exposing)
2781 ;; "Not yet (if ever) implemented.
2783 ;; Produce exposure directives given topic/subtree CHART and an exposure SPEC.
2785 ;; Exposure spec indicates the locations to be exposed and the prescribed
2786 ;; exposure status. Optional arg EXPOSING is an integer, with 0
2787 ;; indicating pending concealment, anything higher indicating depth to
2788 ;; which subtopic headers should be exposed, and negative numbers
2789 ;; indicating (negative of) the depth to which subtopic headers and
2790 ;; bodies should be exposed.
2792 ;; The produced list can have two types of entries. Bare numbers
2793 ;; indicate points in the buffer where topic headers that should be
2794 ;; exposed reside.
2796 ;; - bare negative numbers indicates that the topic starting at the
2797 ;; point which is the negative of the number should be opened,
2798 ;; including their entries.
2799 ;; - bare positive values indicate that this topic header should be
2800 ;; opened.
2801 ;; - Lists signify the beginning and end points of regions that should
2802 ;; be flagged, and the flag to employ. (For concealment: `(\?r)', and
2803 ;; exposure:"
2804 ;; (while spec
2805 ;; (cond ((listp spec)
2806 ;; )
2807 ;; )
2808 ;; (setq spec (cdr spec)))
2809 ;; )
2811 ;;;_ - Within Topic
2812 ;;;_ > allout-goto-prefix ()
2813 (defun allout-goto-prefix ()
2814 "Put point at beginning of immediately containing outline topic.
2816 Goes to most immediate subsequent topic if none immediately containing.
2818 Not sensitive to topic visibility.
2820 Returns the point at the beginning of the prefix, or nil if none."
2822 (save-match-data
2823 (let (done)
2824 (while (and (not done)
2825 (search-backward "\n" nil 1))
2826 (forward-char 1)
2827 (if (looking-at allout-regexp)
2828 (setq done (allout-prefix-data))
2829 (forward-char -1)))
2830 (if (bobp)
2831 (cond ((looking-at allout-regexp)
2832 (allout-prefix-data))
2833 ((allout-next-heading))
2834 (done))
2835 done))))
2836 ;;;_ > allout-goto-prefix-doublechecked ()
2837 (defun allout-goto-prefix-doublechecked ()
2838 "Put point at beginning of immediately containing outline topic.
2840 Like `allout-goto-prefix', but shallow topics (according to
2841 `allout-doublecheck-at-and-shallower') are checked and
2842 disqualified for child containment discontinuity, according to
2843 `allout-aberrant-container-p'."
2844 (if (allout-goto-prefix)
2845 (if (and (allout-do-doublecheck)
2846 (allout-aberrant-container-p))
2847 (allout-previous-heading)
2848 (point))))
2850 ;;;_ > allout-end-of-prefix ()
2851 (defun allout-end-of-prefix (&optional ignore-decorations)
2852 "Position cursor at beginning of header text.
2854 If optional IGNORE-DECORATIONS is non-nil, put just after bullet,
2855 otherwise skip white space between bullet and ensuing text."
2857 (if (not (allout-goto-prefix-doublechecked))
2859 (goto-char allout-recent-prefix-end)
2860 (save-match-data
2861 (if ignore-decorations
2863 (while (looking-at "[0-9]") (forward-char 1))
2864 (if (and (not (eolp)) (looking-at "\\s-")) (forward-char 1))))
2865 ;; Reestablish where we are:
2866 (allout-current-depth)))
2867 ;;;_ > allout-current-bullet-pos ()
2868 (defun allout-current-bullet-pos ()
2869 "Return position of current (visible) topic's bullet."
2871 (if (not (allout-current-depth))
2873 (1- allout-recent-prefix-end)))
2874 ;;;_ > allout-back-to-current-heading (&optional interactive)
2875 (defun allout-back-to-current-heading (&optional interactive)
2876 "Move to heading line of current topic, or beginning if not in a topic.
2878 If interactive, we position at the end of the prefix.
2880 Return value of resulting point, unless we started outside
2881 of (before any) topics, in which case we return nil."
2883 (interactive "p")
2885 (allout-beginning-of-current-line)
2886 (let ((bol-point (point)))
2887 (when (allout-goto-prefix-doublechecked)
2888 (if (<= (point) bol-point)
2889 (progn
2890 (setq bol-point (point))
2891 (allout-beginning-of-current-line)
2892 (if (not (= bol-point (point)))
2893 (if (looking-at allout-regexp)
2894 (allout-prefix-data)))
2895 (if interactive
2896 (allout-end-of-prefix)
2897 (point)))
2898 (goto-char (point-min))
2899 nil))))
2900 ;;;_ > allout-back-to-heading ()
2901 (defalias 'allout-back-to-heading 'allout-back-to-current-heading)
2902 ;;;_ > allout-pre-next-prefix ()
2903 (defun allout-pre-next-prefix ()
2904 "Skip forward to just before the next heading line.
2906 Returns that character position."
2908 (if (allout-next-heading)
2909 (goto-char (1- allout-recent-prefix-beginning))))
2910 ;;;_ > allout-end-of-subtree (&optional current include-trailing-blank)
2911 (defun allout-end-of-subtree (&optional current include-trailing-blank)
2912 "Put point at the end of the last leaf in the containing topic.
2914 Optional CURRENT means put point at the end of the containing
2915 visible topic.
2917 Optional INCLUDE-TRAILING-BLANK means include a trailing blank line, if
2918 any, as part of the subtree. Otherwise, that trailing blank will be
2919 excluded as delimiting whitespace between topics.
2921 Returns the value of point."
2922 (interactive "P")
2923 (if current
2924 (allout-back-to-current-heading)
2925 (allout-goto-prefix-doublechecked))
2926 (let ((level allout-recent-depth))
2927 (allout-next-heading)
2928 (while (and (not (eobp))
2929 (> allout-recent-depth level))
2930 (allout-next-heading))
2931 (if (eobp)
2932 (allout-end-of-entry)
2933 (forward-char -1))
2934 (if (and (not include-trailing-blank) (= ?\n (preceding-char)))
2935 (forward-char -1))
2936 (setq allout-recent-end-of-subtree (point))))
2937 ;;;_ > allout-end-of-current-subtree (&optional include-trailing-blank)
2938 (defun allout-end-of-current-subtree (&optional include-trailing-blank)
2940 "Put point at end of last leaf in currently visible containing topic.
2942 Optional INCLUDE-TRAILING-BLANK means include a trailing blank line, if
2943 any, as part of the subtree. Otherwise, that trailing blank will be
2944 excluded as delimiting whitespace between topics.
2946 Returns the value of point."
2947 (interactive)
2948 (allout-end-of-subtree t include-trailing-blank))
2949 ;;;_ > allout-beginning-of-current-entry (&optional interactive)
2950 (defun allout-beginning-of-current-entry (&optional interactive)
2951 "When not already there, position point at beginning of current topic header.
2953 If already there, move cursor to bullet for hot-spot operation.
2954 \(See `allout-mode' doc string for details of hot-spot operation.)"
2955 (interactive "p")
2956 (let ((start-point (point)))
2957 (move-beginning-of-line 1)
2958 (if (< 0 (allout-current-depth))
2959 (goto-char allout-recent-prefix-end)
2960 (goto-char (point-min)))
2961 (allout-end-of-prefix)
2962 (if (and interactive
2963 (= (point) start-point))
2964 (goto-char (allout-current-bullet-pos)))))
2965 ;;;_ > allout-end-of-entry (&optional inclusive)
2966 (defun allout-end-of-entry (&optional inclusive)
2967 "Position the point at the end of the current topics' entry.
2969 Optional INCLUSIVE means also include trailing empty line, if any. When
2970 unset, whitespace between items separates them even when the items are
2971 collapsed."
2972 (interactive)
2973 (allout-pre-next-prefix)
2974 (if (and (not inclusive) (not (bobp)) (= ?\n (preceding-char)))
2975 (forward-char -1))
2976 (point))
2977 ;;;_ > allout-end-of-current-heading ()
2978 (defun allout-end-of-current-heading ()
2979 (interactive)
2980 (allout-beginning-of-current-entry)
2981 (search-forward "\n" nil t)
2982 (forward-char -1))
2983 (defalias 'allout-end-of-heading 'allout-end-of-current-heading)
2984 ;;;_ > allout-get-body-text ()
2985 (defun allout-get-body-text ()
2986 "Return the unmangled body text of the topic immediately containing point."
2987 (save-excursion
2988 (allout-end-of-prefix)
2989 (if (not (search-forward "\n" nil t))
2991 (backward-char 1)
2992 (let ((pre-body (point)))
2993 (if (not pre-body)
2995 (allout-end-of-entry t)
2996 (if (not (= pre-body (point)))
2997 (buffer-substring-no-properties (1+ pre-body) (point))))
3003 ;;;_ - Depth-wise
3004 ;;;_ > allout-ascend-to-depth (depth)
3005 (defun allout-ascend-to-depth (depth)
3006 "Ascend to depth DEPTH, returning depth if successful, nil if not."
3007 (if (and (> depth 0)(<= depth (allout-depth)))
3008 (let (last-ascended)
3009 (while (and (< depth allout-recent-depth)
3010 (setq last-ascended (allout-ascend))))
3011 (goto-char allout-recent-prefix-beginning)
3012 (if (allout-called-interactively-p) (allout-end-of-prefix))
3013 (and last-ascended allout-recent-depth))))
3014 ;;;_ > allout-ascend (&optional dont-move-if-unsuccessful)
3015 (defun allout-ascend (&optional dont-move-if-unsuccessful)
3016 "Ascend one level, returning resulting depth if successful, nil if not.
3018 Point is left at the beginning of the level whether or not
3019 successful, unless optional DONT-MOVE-IF-UNSUCCESSFUL is set, in
3020 which case point is returned to its original starting location."
3021 (if dont-move-if-unsuccessful
3022 (setq dont-move-if-unsuccessful (point)))
3023 (prog1
3024 (if (allout-beginning-of-level)
3025 (let ((bolevel (point))
3026 (bolevel-depth allout-recent-depth))
3027 (allout-previous-heading)
3028 (cond ((< allout-recent-depth bolevel-depth)
3029 allout-recent-depth)
3030 ((= allout-recent-depth bolevel-depth)
3031 (if dont-move-if-unsuccessful
3032 (goto-char dont-move-if-unsuccessful))
3033 (allout-depth)
3034 nil)
3036 ;; some topic after very first is lower depth than first:
3037 (goto-char bolevel)
3038 (allout-depth)
3039 nil))))
3040 (if (allout-called-interactively-p) (allout-end-of-prefix))))
3041 ;;;_ > allout-descend-to-depth (depth)
3042 (defun allout-descend-to-depth (depth)
3043 "Descend to depth DEPTH within current topic.
3045 Returning depth if successful, nil if not."
3046 (let ((start-point (point))
3047 (start-depth (allout-depth)))
3048 (while
3049 (and (> (allout-depth) 0)
3050 (not (= depth allout-recent-depth)) ; ... not there yet
3051 (allout-next-heading) ; ... go further
3052 (< start-depth allout-recent-depth))) ; ... still in topic
3053 (if (and (> (allout-depth) 0)
3054 (= allout-recent-depth depth))
3055 depth
3056 (goto-char start-point)
3057 nil))
3059 ;;;_ > allout-up-current-level (arg)
3060 (defun allout-up-current-level (arg)
3061 "Move out ARG levels from current visible topic."
3062 (interactive "p")
3063 (let ((start-point (point)))
3064 (allout-back-to-current-heading)
3065 (if (not (allout-ascend))
3066 (progn (goto-char start-point)
3067 (error "Can't ascend past outermost level"))
3068 (if (allout-called-interactively-p) (allout-end-of-prefix))
3069 allout-recent-prefix-beginning)))
3071 ;;;_ - Linear
3072 ;;;_ > allout-next-sibling (&optional depth backward)
3073 (defun allout-next-sibling (&optional depth backward)
3074 "Like `allout-forward-current-level', but respects invisible topics.
3076 Traverse at optional DEPTH, or current depth if none specified.
3078 Go backward if optional arg BACKWARD is non-nil.
3080 Return the start point of the new topic if successful, nil otherwise."
3082 (if (if backward (bobp) (eobp))
3084 (let ((target-depth (or depth (allout-depth)))
3085 (start-point (point))
3086 (start-prefix-beginning allout-recent-prefix-beginning)
3087 (count 0)
3088 leaping
3089 last-depth)
3090 (while (and
3091 ;; done too few single steps to resort to the leap routine:
3092 (not leaping)
3093 ;; not at limit:
3094 (not (if backward (bobp) (eobp)))
3095 ;; still traversable:
3096 (if backward (allout-previous-heading) (allout-next-heading))
3097 ;; we're below the target depth
3098 (> (setq last-depth allout-recent-depth) target-depth))
3099 (setq count (1+ count))
3100 (if (> count 7) ; lists are commonly 7 +- 2, right?-)
3101 (setq leaping t)))
3102 (cond (leaping
3103 (or (allout-next-sibling-leap target-depth backward)
3104 (progn
3105 (goto-char start-point)
3106 (if depth (allout-depth) target-depth)
3107 nil)))
3108 ((and (not (eobp))
3109 (and (> (or last-depth (allout-depth)) 0)
3110 (= allout-recent-depth target-depth))
3111 (not (= start-prefix-beginning
3112 allout-recent-prefix-beginning)))
3113 allout-recent-prefix-beginning)
3115 (goto-char start-point)
3116 (if depth (allout-depth) target-depth)
3117 nil)))))
3118 ;;;_ > allout-next-sibling-leap (&optional depth backward)
3119 (defun allout-next-sibling-leap (&optional depth backward)
3120 "Like `allout-next-sibling', but by direct search for topic at depth.
3122 Traverse at optional DEPTH, or current depth if none specified.
3124 Go backward if optional arg BACKWARD is non-nil.
3126 Return the start point of the new topic if successful, nil otherwise.
3128 Costs more than regular `allout-next-sibling' for short traversals:
3130 - we have to check the prior (next, if traveling backwards)
3131 item to confirm connectivity with the prior topic, and
3132 - if confirmed, we have to reestablish the allout-recent-* settings with
3133 some extra navigation
3134 - if confirmation fails, we have to do more work to recover
3136 It is an increasingly big win when there are many intervening
3137 offspring before the next sibling, however, so
3138 `allout-next-sibling' resorts to this if it finds itself in that
3139 situation."
3141 (if (if backward (bobp) (eobp))
3143 (let* ((start-point (point))
3144 (target-depth (or depth (allout-depth)))
3145 (search-whitespace-regexp nil)
3146 (depth-biased (- target-depth 2))
3147 (expression (if (<= target-depth 1)
3148 allout-depth-one-regexp
3149 (format allout-depth-specific-regexp
3150 depth-biased depth-biased)))
3151 found
3152 done)
3153 (while (not done)
3154 (setq found (save-match-data
3155 (if backward
3156 (re-search-backward expression nil 'to-limit)
3157 (forward-char 1)
3158 (re-search-forward expression nil 'to-limit))))
3159 (if (and found (allout-aberrant-container-p))
3160 (setq found nil))
3161 (setq done (or found (if backward (bobp) (eobp)))))
3162 (if (not found)
3163 (progn (goto-char start-point)
3164 nil)
3165 ;; rationale: if any intervening items were at a lower depth, we
3166 ;; would now be on the first offspring at the target depth -- ie,
3167 ;; the preceding item (per the search direction) must be at a
3168 ;; lesser depth. that's all we need to check.
3169 (if backward (allout-next-heading) (allout-previous-heading))
3170 (if (< allout-recent-depth target-depth)
3171 ;; return to start and reestablish allout-recent-*:
3172 (progn
3173 (goto-char start-point)
3174 (allout-depth)
3175 nil)
3176 (goto-char found)
3177 ;; locate cursor and set allout-recent-*:
3178 (allout-goto-prefix))))))
3179 ;;;_ > allout-previous-sibling (&optional depth backward)
3180 (defun allout-previous-sibling (&optional depth backward)
3181 "Like `allout-forward-current-level' backwards, respecting invisible topics.
3183 Optional DEPTH specifies depth to traverse, default current depth.
3185 Optional BACKWARD reverses direction.
3187 Return depth if successful, nil otherwise."
3188 (allout-next-sibling depth (not backward))
3190 ;;;_ > allout-snug-back ()
3191 (defun allout-snug-back ()
3192 "Position cursor at end of previous topic.
3194 Presumes point is at the start of a topic prefix."
3195 (if (or (bobp) (eobp))
3197 (forward-char -1))
3198 (if (or (bobp) (not (= ?\n (preceding-char))))
3200 (forward-char -1))
3201 (point))
3202 ;;;_ > allout-beginning-of-level ()
3203 (defun allout-beginning-of-level ()
3204 "Go back to the first sibling at this level, visible or not."
3205 (allout-end-of-level 'backward))
3206 ;;;_ > allout-end-of-level (&optional backward)
3207 (defun allout-end-of-level (&optional backward)
3208 "Go to the last sibling at this level, visible or not."
3210 (let ((depth (allout-depth)))
3211 (while (allout-previous-sibling depth nil))
3212 (prog1 allout-recent-depth
3213 (if (allout-called-interactively-p) (allout-end-of-prefix)))))
3214 ;;;_ > allout-next-visible-heading (arg)
3215 (defun allout-next-visible-heading (arg)
3216 "Move to the next ARGth visible heading line, backward if ARG is negative.
3218 Move to buffer limit in indicated direction if headings are exhausted."
3220 (interactive "p")
3221 (let* ((inhibit-field-text-motion t)
3222 (backward (if (< arg 0) (setq arg (* -1 arg))))
3223 (step (if backward -1 1))
3224 (progress (allout-current-bullet-pos))
3225 prev got)
3227 (while (> arg 0)
3228 (while (and
3229 ;; Boundary condition:
3230 (not (if backward (bobp)(eobp)))
3231 ;; Move, skipping over all concealed lines in one fell swoop:
3232 (prog1 (condition-case nil (or (line-move step) t)
3233 (error nil))
3234 (allout-beginning-of-current-line)
3235 ;; line-move can wind up on the same line if long.
3236 ;; when moving forward, that would yield no-progress
3237 (when (and (not backward)
3238 (<= (point) progress))
3239 ;; ensure progress by doing line-move from end-of-line:
3240 (end-of-line)
3241 (condition-case nil (or (line-move step) t)
3242 (error nil))
3243 (allout-beginning-of-current-line)
3244 (setq progress (point))))
3245 ;; Deal with apparent header line:
3246 (save-match-data
3247 (if (not (looking-at allout-regexp))
3248 ;; not a header line, keep looking:
3250 (allout-prefix-data)
3251 (if (and (allout-do-doublecheck)
3252 (allout-aberrant-container-p))
3253 ;; skip this aberrant prospective header line:
3255 ;; this prospective headerline qualifies -- register:
3256 (setq got allout-recent-prefix-beginning)
3257 ;; and break the loop:
3258 nil)))))
3259 ;; Register this got, it may be the last:
3260 (if got (setq prev got))
3261 (setq arg (1- arg)))
3262 (cond (got ; Last move was to a prefix:
3263 (allout-end-of-prefix))
3264 (prev ; Last move wasn't, but prev was:
3265 (goto-char prev)
3266 (allout-end-of-prefix))
3267 ((not backward) (end-of-line) nil))))
3268 ;;;_ > allout-previous-visible-heading (arg)
3269 (defun allout-previous-visible-heading (arg)
3270 "Move to the previous heading line.
3272 With argument, repeats or can move forward if negative.
3273 A heading line is one that starts with a `*' (or that `allout-regexp'
3274 matches)."
3275 (interactive "p")
3276 (prog1 (allout-next-visible-heading (- arg))
3277 (if (allout-called-interactively-p) (allout-end-of-prefix))))
3278 ;;;_ > allout-forward-current-level (arg)
3279 (defun allout-forward-current-level (arg)
3280 "Position point at the next heading of the same level.
3282 Takes optional repeat-count, goes backward if count is negative.
3284 Returns resulting position, else nil if none found."
3285 (interactive "p")
3286 (let ((start-depth (allout-current-depth))
3287 (start-arg arg)
3288 (backward (> 0 arg)))
3289 (if (= 0 start-depth)
3290 (error "No siblings, not in a topic..."))
3291 (if backward (setq arg (* -1 arg)))
3292 (allout-back-to-current-heading)
3293 (while (and (not (zerop arg))
3294 (if backward
3295 (allout-previous-sibling)
3296 (allout-next-sibling)))
3297 (setq arg (1- arg)))
3298 (if (not (allout-called-interactively-p))
3300 (allout-end-of-prefix)
3301 (if (not (zerop arg))
3302 (error "Hit %s level %d topic, traversed %d of %d requested"
3303 (if backward "first" "last")
3304 allout-recent-depth
3305 (- (abs start-arg) arg)
3306 (abs start-arg))))))
3307 ;;;_ > allout-backward-current-level (arg)
3308 (defun allout-backward-current-level (arg)
3309 "Inverse of `allout-forward-current-level'."
3310 (interactive "p")
3311 (if (allout-called-interactively-p)
3312 (let ((current-prefix-arg (* -1 arg)))
3313 (call-interactively 'allout-forward-current-level))
3314 (allout-forward-current-level (* -1 arg))))
3316 ;;;_ #5 Alteration
3318 ;;;_ - Fundamental
3319 ;;;_ = allout-post-goto-bullet
3320 (defvar allout-post-goto-bullet nil
3321 "Outline internal var, for `allout-pre-command-business' hot-spot operation.
3323 When set, tells post-processing to reposition on topic bullet, and
3324 then unset it. Set by `allout-pre-command-business' when implementing
3325 hot-spot operation, where literal characters typed over a topic bullet
3326 are mapped to the command of the corresponding control-key on the
3327 `allout-mode-map-value'.")
3328 (make-variable-buffer-local 'allout-post-goto-bullet)
3329 ;;;_ = allout-command-counter
3330 (defvar allout-command-counter 0
3331 "Counter that monotonically increases in allout-mode buffers.
3333 Set by `allout-pre-command-business', to support allout addons in
3334 coordinating with allout activity.")
3335 (make-variable-buffer-local 'allout-command-counter)
3336 ;;;_ = allout-this-command-hid-text
3337 (defvar allout-this-command-hid-text nil
3338 "True if the most recent allout-mode command hid any text.")
3339 (make-variable-buffer-local 'allout-this-command-hid-text)
3340 ;;;_ > allout-post-command-business ()
3341 (defun allout-post-command-business ()
3342 "Outline `post-command-hook' function.
3344 - Implement (and clear) `allout-post-goto-bullet', for hot-spot
3345 outline commands.
3347 - Move the cursor to the beginning of the entry if it is hidden
3348 and collapsing activity just happened.
3350 - If the command we're following was an undo, check for change in
3351 the status of encrypted items and adjust auto-save inhibitions
3352 accordingly.
3354 - Decrypt topic currently being edited if it was encrypted for a save."
3356 (if (not (allout-mode-p)) ; In allout-mode.
3359 (when allout-just-did-undo
3360 (setq allout-just-did-undo nil)
3361 (run-hooks 'allout-post-undo-hook)
3362 (cond ((and (= buffer-saved-size -1)
3363 allout-auto-save-temporarily-disabled)
3364 ;; user possibly undid a decryption, disinhibit auto-save:
3365 (allout-maybe-resume-auto-save-info-after-encryption))
3366 ((save-excursion
3367 (save-restriction
3368 (widen)
3369 (goto-char (point-min))
3370 (not (allout-next-topic-pending-encryption))))
3371 ;; plain-text encrypted items are present, inhibit auto-save:
3372 (allout-inhibit-auto-save-info-for-decryption (buffer-size)))))
3374 (if (and (boundp 'allout-after-save-decrypt)
3375 allout-after-save-decrypt)
3376 (allout-after-saves-handler))
3378 ;; Implement allout-post-goto-bullet, if set:
3379 (if (and allout-post-goto-bullet
3380 (allout-current-bullet-pos))
3381 (progn (goto-char (allout-current-bullet-pos))
3382 (setq allout-post-goto-bullet nil))
3383 (when (and (allout-hidden-p) allout-this-command-hid-text)
3384 (allout-beginning-of-current-entry)))))
3385 ;;;_ > allout-pre-command-business ()
3386 (defun allout-pre-command-business ()
3387 "Outline `pre-command-hook' function for outline buffers.
3389 Among other things, implements special behavior when the cursor is on the
3390 topic bullet character.
3392 When the cursor is on the bullet character, self-insert
3393 characters are reinterpreted as the corresponding
3394 control-character in the `allout-mode-map-value'. The
3395 `allout-mode' `post-command-hook' insures that the cursor which
3396 has moved as a result of such reinterpretation is positioned on
3397 the bullet character of the destination topic.
3399 The upshot is that you can get easy, single (ie, unmodified) key
3400 outline maneuvering operations by positioning the cursor on the bullet
3401 char. When in this mode you can use regular cursor-positioning
3402 command/keystrokes to relocate the cursor off of a bullet character to
3403 return to regular interpretation of self-insert characters."
3405 (if (not (allout-mode-p))
3407 (setq allout-command-counter (1+ allout-command-counter))
3408 (setq allout-this-command-hid-text nil)
3409 ;; Do hot-spot navigation.
3410 (if (and (eq this-command 'self-insert-command)
3411 (eq (point)(allout-current-bullet-pos)))
3412 (allout-hotspot-key-handler))))
3413 ;;;_ > allout-hotspot-key-handler ()
3414 (defun allout-hotspot-key-handler ()
3415 "Catchall handling of key bindings in hot-spots.
3417 Translates unmodified keystrokes to corresponding allout commands, when
3418 they would qualify if prefixed with the `allout-command-prefix', and sets
3419 `this-command' accordingly.
3421 Returns the qualifying command, if any, else nil."
3422 (interactive)
3423 (let* ((modified (event-modifiers last-command-event))
3424 (key-num (cond ((numberp last-command-event) last-command-event)
3425 ;; for XEmacs character type:
3426 ((and (fboundp 'characterp)
3427 (apply 'characterp (list last-command-event)))
3428 (apply 'char-to-int (list last-command-event)))
3429 (t 0)))
3430 mapped-binding)
3432 (if (zerop key-num)
3435 (if (and
3436 ;; exclude control chars and escape:
3437 (not modified)
3438 (<= 33 key-num)
3439 (setq mapped-binding
3441 ;; try control-modified versions of keys:
3442 (key-binding (vconcat allout-command-prefix
3443 (vector
3444 (if (and (<= 97 key-num) ; "a"
3445 (>= 122 key-num)) ; "z"
3446 (- key-num 96) key-num)))
3448 ;; try non-modified versions of keys:
3449 (key-binding (vconcat allout-command-prefix
3450 (vector key-num))
3451 t))))
3452 ;; Qualified as an allout command -- do hot-spot operation.
3453 (setq allout-post-goto-bullet t)
3454 ;; accept-defaults nil, or else we get allout-item-icon-key-handler.
3455 (setq mapped-binding (key-binding (vector key-num))))
3457 (while (keymapp mapped-binding)
3458 (setq mapped-binding
3459 (lookup-key mapped-binding (vector (read-char)))))
3461 (when mapped-binding
3462 (setq this-command mapped-binding)))))
3464 ;;;_ > allout-find-file-hook ()
3465 (defun allout-find-file-hook ()
3466 "Activate `allout-mode' on non-nil `allout-auto-activation', `allout-layout'.
3468 See `allout-auto-activation' for setup instructions."
3469 (if (and allout-auto-activation
3470 (not (allout-mode-p))
3471 allout-layout)
3472 (allout-mode)))
3474 ;;;_ - Topic Format Assessment
3475 ;;;_ > allout-solicit-alternate-bullet (depth &optional current-bullet)
3476 (defun allout-solicit-alternate-bullet (depth &optional current-bullet)
3478 "Prompt for and return a bullet char as an alternative to the current one.
3480 Offer one suitable for current depth DEPTH as default."
3482 (let* ((default-bullet (or (and (stringp current-bullet) current-bullet)
3483 (allout-bullet-for-depth depth)))
3484 (sans-escapes (regexp-sans-escapes allout-bullets-string))
3485 choice)
3486 (save-excursion
3487 (goto-char (allout-current-bullet-pos))
3488 (setq choice (solicit-char-in-string
3489 (format "Select bullet: %s ('%s' default): "
3490 sans-escapes
3491 (allout-substring-no-properties default-bullet))
3492 sans-escapes
3493 t)))
3494 (message "")
3495 (if (string= choice "") default-bullet choice))
3497 ;;;_ > allout-distinctive-bullet (bullet)
3498 (defun allout-distinctive-bullet (bullet)
3499 "True if BULLET is one of those on `allout-distinctive-bullets-string'."
3500 (string-match (regexp-quote bullet) allout-distinctive-bullets-string))
3501 ;;;_ > allout-numbered-type-prefix (&optional prefix)
3502 (defun allout-numbered-type-prefix (&optional prefix)
3503 "True if current header prefix bullet is numbered bullet."
3504 (and allout-numbered-bullet
3505 (string= allout-numbered-bullet
3506 (if prefix
3507 (allout-get-prefix-bullet prefix)
3508 (allout-get-bullet)))))
3509 ;;;_ > allout-encrypted-type-prefix (&optional prefix)
3510 (defun allout-encrypted-type-prefix (&optional prefix)
3511 "True if current header prefix bullet is for an encrypted entry (body)."
3512 (and allout-topic-encryption-bullet
3513 (string= allout-topic-encryption-bullet
3514 (if prefix
3515 (allout-get-prefix-bullet prefix)
3516 (allout-get-bullet)))))
3517 ;;;_ > allout-bullet-for-depth (&optional depth)
3518 (defun allout-bullet-for-depth (&optional depth)
3519 "Return outline topic bullet suited to optional DEPTH, or current depth."
3520 ;; Find bullet in plain-bullets-string modulo DEPTH.
3521 (if allout-stylish-prefixes
3522 (char-to-string (aref allout-plain-bullets-string
3523 (% (max 0 (- depth 2))
3524 allout-plain-bullets-string-len)))
3525 allout-primary-bullet)
3528 ;;;_ - Topic Production
3529 ;;;_ > allout-make-topic-prefix (&optional prior-bullet
3530 (defun allout-make-topic-prefix (&optional prior-bullet
3532 depth
3533 instead
3534 number-control
3535 index)
3536 ;; Depth null means use current depth, non-null means we're either
3537 ;; opening a new topic after current topic, lower or higher, or we're
3538 ;; changing level of current topic.
3539 ;; Instead dominates specified bullet-char.
3540 ;;;_ . Doc string:
3541 "Generate a topic prefix suitable for optional arg DEPTH, or current depth.
3543 All the arguments are optional.
3545 PRIOR-BULLET indicates the bullet of the prefix being changed, or
3546 nil if none. This bullet may be preserved (other options
3547 notwithstanding) if it is on the `allout-distinctive-bullets-string',
3548 for instance.
3550 Second arg NEW indicates that a new topic is being opened after the
3551 topic at point, if non-nil. Default bullet for new topics, eg, may
3552 be set (contingent to other args) to numbered bullets if previous
3553 sibling is one. The implication otherwise is that the current topic
3554 is being adjusted -- shifted or rebulleted -- and we don't consider
3555 bullet or previous sibling.
3557 Third arg DEPTH forces the topic prefix to that depth, regardless of
3558 the current topics' depth.
3560 If INSTEAD is:
3562 - nil, then the bullet char for the context is used, per distinction or depth
3563 - a (numeric) character, then character's string representation is used
3564 - a string, then the user is asked for bullet with the first char as default
3565 - anything else, the user is solicited with bullet char per context as default
3567 \(INSTEAD overrides other options, including, eg, a distinctive
3568 PRIOR-BULLET.)
3570 Fifth arg, NUMBER-CONTROL, matters only if `allout-numbered-bullet'
3571 is non-nil *and* no specific INSTEAD was specified. Then
3572 NUMBER-CONTROL non-nil forces prefix to either numbered or
3573 unnumbered format, depending on the value of the sixth arg, INDEX.
3575 \(Note that NUMBER-CONTROL does *not* apply to level 1 topics. Sorry...)
3577 If NUMBER-CONTROL is non-nil and sixth arg INDEX is non-nil then
3578 the prefix of the topic is forced to be numbered. Non-nil
3579 NUMBER-CONTROL and nil INDEX forces non-numbered format on the
3580 bullet. Non-nil NUMBER-CONTROL and non-nil, non-number INDEX means
3581 that the index for the numbered prefix will be derived, by counting
3582 siblings back to start of level. If INDEX is a number, then that
3583 number is used as the index for the numbered prefix (allowing, eg,
3584 sequential renumbering to not require this function counting back the
3585 index for each successive sibling)."
3586 ;;;_ . Code:
3587 ;; The options are ordered in likely frequency of use, most common
3588 ;; highest, least lowest. Ie, more likely to be doing prefix
3589 ;; adjustments than soliciting, and yet more than numbering.
3590 ;; Current prefix is least dominant, but most likely to be commonly
3591 ;; specified...
3593 (let* (body
3594 numbering
3595 denumbering
3596 (depth (or depth (allout-depth)))
3597 (header-lead allout-header-prefix)
3598 (bullet-char
3600 ;; Getting value for bullet char is practically the whole job:
3602 (cond
3603 ; Simplest situation -- level 1:
3604 ((<= depth 1) (setq header-lead "") allout-primary-bullet)
3605 ; Simple, too: all asterisks:
3606 (allout-old-style-prefixes
3607 ;; Cheat -- make body the whole thing, null out header-lead and
3608 ;; bullet-char:
3609 (setq body (make-string depth
3610 (string-to-char allout-primary-bullet)))
3611 (setq header-lead "")
3614 ;; (Neither level 1 nor old-style, so we're space padding.
3615 ;; Sneak it in the condition of the next case, whatever it is.)
3617 ;; Solicitation overrides numbering and other cases:
3618 ((progn (setq body (make-string (- depth 2) ?\ ))
3619 ;; The actual condition:
3620 instead)
3621 (let ((got (cond ((stringp instead)
3622 (if (> (length instead) 0)
3623 (allout-solicit-alternate-bullet
3624 depth (substring instead 0 1))))
3625 ((characterp instead) (char-to-string instead))
3626 (t (allout-solicit-alternate-bullet depth)))))
3627 ;; Gotta check whether we're numbering and got a numbered bullet:
3628 (setq numbering (and allout-numbered-bullet
3629 (not (and number-control (not index)))
3630 (string= got allout-numbered-bullet)))
3631 ;; Now return what we got, regardless:
3632 got))
3634 ;; Numbering invoked through args:
3635 ((and allout-numbered-bullet number-control)
3636 (if (setq numbering (not (setq denumbering (not index))))
3637 allout-numbered-bullet
3638 (if (and prior-bullet
3639 (not (string= allout-numbered-bullet
3640 prior-bullet)))
3641 prior-bullet
3642 (allout-bullet-for-depth depth))))
3644 ;;; Neither soliciting nor controlled numbering ;;;
3645 ;;; (may be controlled denumbering, tho) ;;;
3647 ;; Check wrt previous sibling:
3648 ((and new ; only check for new prefixes
3649 (<= depth (allout-depth))
3650 allout-numbered-bullet ; ... & numbering enabled
3651 (not denumbering)
3652 (let ((sibling-bullet
3653 (save-excursion
3654 ;; Locate correct sibling:
3655 (or (>= depth (allout-depth))
3656 (allout-ascend-to-depth depth))
3657 (allout-get-bullet))))
3658 (if (and sibling-bullet
3659 (string= allout-numbered-bullet sibling-bullet))
3660 (setq numbering sibling-bullet)))))
3662 ;; Distinctive prior bullet?
3663 ((and prior-bullet
3664 (allout-distinctive-bullet prior-bullet)
3665 ;; Either non-numbered:
3666 (or (not (and allout-numbered-bullet
3667 (string= prior-bullet allout-numbered-bullet)))
3668 ;; or numbered, and not denumbering:
3669 (setq numbering (not denumbering)))
3670 ;; Here 'tis:
3671 prior-bullet))
3673 ;; Else, standard bullet per depth:
3674 ((allout-bullet-for-depth depth)))))
3676 (concat header-lead
3677 body
3678 bullet-char
3679 (if numbering
3680 (format "%d" (cond ((and index (numberp index)) index)
3681 (new (1+ (allout-sibling-index depth)))
3682 ((allout-sibling-index))))))
3685 ;;;_ > allout-open-topic (relative-depth &optional before offer-recent-bullet)
3686 (defun allout-open-topic (relative-depth &optional before offer-recent-bullet)
3687 "Open a new topic at depth DEPTH.
3689 New topic is situated after current one, unless optional flag BEFORE
3690 is non-nil, or unless current line is completely empty -- lacking even
3691 whitespace -- in which case open is done on the current line.
3693 When adding an offspring, it will be added immediately after the parent if
3694 the other offspring are exposed, or after the last child if the offspring
3695 are hidden. (The intervening offspring will be exposed in the latter
3696 case.)
3698 If OFFER-RECENT-BULLET is true, offer to use the bullet of the prior sibling.
3700 Nuances:
3702 - Creation of new topics is with respect to the visible topic
3703 containing the cursor, regardless of intervening concealed ones.
3705 - New headers are generally created after/before the body of a
3706 topic. However, they are created right at cursor location if the
3707 cursor is on a blank line, even if that breaks the current topic
3708 body. This is intentional, to provide a simple means for
3709 deliberately dividing topic bodies.
3711 - Double spacing of topic lists is preserved. Also, the first
3712 level two topic is created double-spaced (and so would be
3713 subsequent siblings, if that's left intact). Otherwise,
3714 single-spacing is used.
3716 - Creation of sibling or nested topics is with respect to the topic
3717 you're starting from, even when creating backwards. This way you
3718 can easily create a sibling in front of the current topic without
3719 having to go to its preceding sibling, and then open forward
3720 from there."
3722 (allout-beginning-of-current-line)
3723 (save-match-data
3724 (let* ((inhibit-field-text-motion t)
3725 (depth (+ (allout-current-depth) relative-depth))
3726 (opening-on-blank (if (looking-at "^\$")
3727 (not (setq before nil))))
3728 ;; bunch o vars set while computing ref-topic
3729 opening-numbered
3730 ref-depth
3731 ref-bullet
3732 (ref-topic (save-excursion
3733 (cond ((< relative-depth 0)
3734 (allout-ascend-to-depth depth))
3735 ((>= relative-depth 1) nil)
3736 (t (allout-back-to-current-heading)))
3737 (setq ref-depth allout-recent-depth)
3738 (setq ref-bullet
3739 (if (> allout-recent-prefix-end 1)
3740 (allout-recent-bullet)
3741 ""))
3742 (setq opening-numbered
3743 (save-excursion
3744 (and allout-numbered-bullet
3745 (or (<= relative-depth 0)
3746 (allout-descend-to-depth depth))
3747 (if (allout-numbered-type-prefix)
3748 allout-numbered-bullet))))
3749 (point)))
3750 dbl-space
3751 doing-beginning
3752 start end)
3754 (if (not opening-on-blank)
3755 ; Positioning and vertical
3756 ; padding -- only if not
3757 ; opening-on-blank:
3758 (progn
3759 (goto-char ref-topic)
3760 (setq dbl-space ; Determine double space action:
3761 (or (and (<= relative-depth 0) ; not descending;
3762 (save-excursion
3763 ;; at b-o-b or preceded by a blank line?
3764 (or (> 0 (forward-line -1))
3765 (looking-at "^\\s-*$")
3766 (bobp)))
3767 (save-excursion
3768 ;; succeeded by a blank line?
3769 (allout-end-of-current-subtree)
3770 (looking-at "\n\n")))
3771 (and (= ref-depth 1)
3772 (or before
3773 (= depth 1)
3774 (save-excursion
3775 ;; Don't already have following
3776 ;; vertical padding:
3777 (not (allout-pre-next-prefix)))))))
3779 ;; Position to prior heading, if inserting backwards, and not
3780 ;; going outwards:
3781 (if (and before (>= relative-depth 0))
3782 (progn (allout-back-to-current-heading)
3783 (setq doing-beginning (bobp))
3784 (if (not (bobp))
3785 (allout-previous-heading)))
3786 (if (and before (bobp))
3787 (open-line 1)))
3789 (if (<= relative-depth 0)
3790 ;; Not going inwards, don't snug up:
3791 (if doing-beginning
3792 (if (not dbl-space)
3793 (open-line 1)
3794 (open-line 2))
3795 (if before
3796 (progn (end-of-line)
3797 (allout-pre-next-prefix)
3798 (while (and (= ?\n (following-char))
3799 (save-excursion
3800 (forward-char 1)
3801 (allout-hidden-p)))
3802 (forward-char 1))
3803 (if (not (looking-at "^$"))
3804 (open-line 1)))
3805 (allout-end-of-current-subtree)
3806 (if (looking-at "\n\n") (forward-char 1))))
3807 ;; Going inwards -- double-space if first offspring is
3808 ;; double-spaced, otherwise snug up.
3809 (allout-end-of-entry)
3810 (if (eobp)
3811 (newline 1)
3812 (line-move 1))
3813 (allout-beginning-of-current-line)
3814 (backward-char 1)
3815 (if (bolp)
3816 ;; Blank lines between current header body and next
3817 ;; header -- get to last substantive (non-white-space)
3818 ;; line in body:
3819 (progn (setq dbl-space t)
3820 (re-search-backward "[^ \t\n]" nil t)))
3821 (if (looking-at "\n\n")
3822 (setq dbl-space t))
3823 (if (save-excursion
3824 (allout-next-heading)
3825 (when (> allout-recent-depth ref-depth)
3826 ;; This is an offspring.
3827 (forward-line -1)
3828 (looking-at "^\\s-*$")))
3829 (progn (forward-line 1)
3830 (open-line 1)
3831 (forward-line 1)))
3832 (allout-end-of-current-line))
3834 ;;(if doing-beginning (goto-char doing-beginning))
3835 (if (not (bobp))
3836 ;; We insert a newline char rather than using open-line to
3837 ;; avoid rear-stickiness inheritance of read-only property.
3838 (progn (if (and (not (> depth ref-depth))
3839 (not before))
3840 (open-line 1)
3841 (if (and (not dbl-space) (> depth ref-depth))
3842 (newline 1)
3843 (if dbl-space
3844 (open-line 1)
3845 (if (not before)
3846 (newline 1)))))
3847 (if (and dbl-space (not (> relative-depth 0)))
3848 (newline 1))
3849 (if (and (not (eobp))
3850 (or (not (bolp))
3851 (and (not (bobp))
3852 ;; bolp doesn't detect concealed
3853 ;; trailing newlines, compensate:
3854 (save-excursion
3855 (forward-char -1)
3856 (allout-hidden-p)))))
3857 (forward-char 1))))
3859 (setq start (point))
3860 (insert (concat (allout-make-topic-prefix opening-numbered t depth)
3861 " "))
3862 (setq end (1+ (point)))
3864 (allout-rebullet-heading (and offer-recent-bullet ref-bullet)
3865 depth nil nil t)
3866 (if (> relative-depth 0)
3867 (save-excursion (goto-char ref-topic)
3868 (allout-show-children)))
3869 (end-of-line)
3871 (run-hook-with-args 'allout-structure-added-functions start end)
3875 ;;;_ > allout-open-subtopic (arg)
3876 (defun allout-open-subtopic (arg)
3877 "Open new topic header at deeper level than the current one.
3879 Negative universal ARG means to open deeper, but place the new topic
3880 prior to the current one."
3881 (interactive "p")
3882 (allout-open-topic 1 (> 0 arg) (< 1 arg)))
3883 ;;;_ > allout-open-sibtopic (arg)
3884 (defun allout-open-sibtopic (arg)
3885 "Open new topic header at same level as the current one.
3887 Positive universal ARG means to use the bullet of the prior sibling.
3889 Negative universal ARG means to place the new topic prior to the current
3890 one."
3891 (interactive "p")
3892 (allout-open-topic 0 (> 0 arg) (not (= 1 arg))))
3893 ;;;_ > allout-open-supertopic (arg)
3894 (defun allout-open-supertopic (arg)
3895 "Open new topic header at shallower level than the current one.
3897 Negative universal ARG means to open shallower, but place the new
3898 topic prior to the current one."
3900 (interactive "p")
3901 (allout-open-topic -1 (> 0 arg) (< 1 arg)))
3903 ;;;_ - Outline Alteration
3904 ;;;_ : Topic Modification
3905 ;;;_ = allout-former-auto-filler
3906 (defvar allout-former-auto-filler nil
3907 "Name of modal fill function being wrapped by `allout-auto-fill'.")
3908 ;;;_ > allout-auto-fill ()
3909 (defun allout-auto-fill ()
3910 "`allout-mode' autofill function.
3912 Maintains outline hanging topic indentation if
3913 `allout-use-hanging-indents' is set."
3915 (when (and (not allout-inhibit-auto-fill)
3916 (or (not allout-inhibit-auto-fill-on-headline)
3917 (not (allout-on-current-heading-p))))
3918 (let ((fill-prefix (if allout-use-hanging-indents
3919 ;; Check for topic header indentation:
3920 (save-match-data
3921 (save-excursion
3922 (beginning-of-line)
3923 (if (looking-at allout-regexp)
3924 ;; ... construct indentation to account for
3925 ;; length of topic prefix:
3926 (make-string (progn (allout-end-of-prefix)
3927 (current-column))
3928 ?\ ))))))
3929 (use-auto-fill-function
3930 (if (and (eq allout-outside-normal-auto-fill-function
3931 'allout-auto-fill)
3932 (eq auto-fill-function 'allout-auto-fill))
3933 'do-auto-fill
3934 (or allout-outside-normal-auto-fill-function
3935 auto-fill-function))))
3936 (if (or allout-former-auto-filler allout-use-hanging-indents)
3937 (funcall use-auto-fill-function)))))
3938 ;;;_ > allout-reindent-body (old-depth new-depth &optional number)
3939 (defun allout-reindent-body (old-depth new-depth &optional number)
3940 "Reindent body lines which were indented at OLD-DEPTH to NEW-DEPTH.
3942 Optional arg NUMBER indicates numbering is being added, and it must
3943 be accommodated.
3945 Note that refill of indented paragraphs is not done."
3947 (save-excursion
3948 (allout-end-of-prefix)
3949 (let* ((new-margin (current-column))
3950 excess old-indent-begin old-indent-end
3951 ;; We want the column where the header-prefix text started
3952 ;; *before* the prefix was changed, so we infer it relative
3953 ;; to the new margin and the shift in depth:
3954 (old-margin (+ old-depth (- new-margin new-depth))))
3956 ;; Process lines up to (but excluding) next topic header:
3957 (allout-unprotected
3958 (save-match-data
3959 (while
3960 (and (re-search-forward "\n\\(\\s-*\\)"
3963 ;; Register the indent data, before we reset the
3964 ;; match data with a subsequent `looking-at':
3965 (setq old-indent-begin (match-beginning 1)
3966 old-indent-end (match-end 1))
3967 (not (looking-at allout-regexp)))
3968 (if (> 0 (setq excess (- (- old-indent-end old-indent-begin)
3969 old-margin)))
3970 ;; Text starts left of old margin -- don't adjust:
3972 ;; Text was hanging at or right of old left margin --
3973 ;; reindent it, preserving its existing indentation
3974 ;; beyond the old margin:
3975 (delete-region old-indent-begin old-indent-end)
3976 (indent-to (+ new-margin excess (current-column))))))))))
3977 ;;;_ > allout-rebullet-current-heading (arg)
3978 (defun allout-rebullet-current-heading (arg)
3979 "Solicit new bullet for current visible heading."
3980 (interactive "p")
3981 (let ((initial-col (current-column))
3982 (on-bullet (eq (point)(allout-current-bullet-pos)))
3983 from to
3984 (backwards (if (< arg 0)
3985 (setq arg (* arg -1)))))
3986 (while (> arg 0)
3987 (save-excursion (allout-back-to-current-heading)
3988 (allout-end-of-prefix)
3989 (setq from allout-recent-prefix-beginning
3990 to allout-recent-prefix-end)
3991 (allout-rebullet-heading t ;;; instead
3992 nil ;;; depth
3993 nil ;;; number-control
3994 nil ;;; index
3995 t) ;;; do-successors
3996 (run-hook-with-args 'allout-exposure-change-functions
3997 from to t))
3998 (setq arg (1- arg))
3999 (if (<= arg 0)
4001 (setq initial-col nil) ; Override positioning back to init col
4002 (if (not backwards)
4003 (allout-next-visible-heading 1)
4004 (allout-goto-prefix-doublechecked)
4005 (allout-next-visible-heading -1))))
4006 (message "Done.")
4007 (cond (on-bullet (goto-char (allout-current-bullet-pos)))
4008 (initial-col (move-to-column initial-col)))))
4009 ;;;_ > allout-rebullet-heading (&optional instead ...)
4010 (defun allout-rebullet-heading (&optional instead
4011 new-depth
4012 number-control
4013 index
4014 do-successors)
4016 "Adjust bullet of current topic prefix.
4018 All args are optional.
4020 If INSTEAD is:
4021 - nil, then the bullet char for the context is used, per distinction or depth
4022 - a (numeric) character, then character's string representation is used
4023 - a string, then the user is asked for bullet with the first char as default
4024 - anything else, the user is solicited with bullet char per context as default
4026 Second arg DEPTH forces the topic prefix to that depth, regardless
4027 of the topic's current depth.
4029 Third arg NUMBER-CONTROL can force the prefix to or away from
4030 numbered form. It has effect only if `allout-numbered-bullet' is
4031 non-nil and soliciting was not explicitly invoked (via first arg).
4032 Its effect, numbering or denumbering, then depends on the setting
4033 of the fourth arg, INDEX.
4035 If NUMBER-CONTROL is non-nil and fourth arg INDEX is nil, then the
4036 prefix of the topic is forced to be non-numbered. Null index and
4037 non-nil NUMBER-CONTROL forces denumbering. Non-nil INDEX (and
4038 non-nil NUMBER-CONTROL) forces a numbered-prefix form. If non-nil
4039 INDEX is a number, then that number is used for the numbered
4040 prefix. Non-nil and non-number means that the index for the
4041 numbered prefix will be derived by allout-make-topic-prefix.
4043 Fifth arg DO-SUCCESSORS t means re-resolve count on succeeding
4044 siblings.
4046 Cf vars `allout-stylish-prefixes', `allout-old-style-prefixes',
4047 and `allout-numbered-bullet', which all affect the behavior of
4048 this function."
4050 (let* ((current-depth (allout-depth))
4051 (new-depth (or new-depth current-depth))
4052 (mb allout-recent-prefix-beginning)
4053 (me allout-recent-prefix-end)
4054 (current-bullet (buffer-substring-no-properties (- me 1) me))
4055 (has-annotation (get-text-property mb 'allout-was-hidden))
4056 (new-prefix (allout-make-topic-prefix current-bullet
4058 new-depth
4059 instead
4060 number-control
4061 index)))
4063 ;; Is new one identical to old?
4064 (if (and (= current-depth new-depth)
4065 (string= current-bullet
4066 (substring new-prefix (1- (length new-prefix)))))
4067 ;; Nothing to do:
4070 ;; New prefix probably different from old:
4071 ; get rid of old one:
4072 (allout-unprotected (delete-region mb me))
4073 (goto-char mb)
4074 ; Dispense with number if
4075 ; numbered-bullet prefix:
4076 (save-match-data
4077 (if (and allout-numbered-bullet
4078 (string= allout-numbered-bullet current-bullet)
4079 (looking-at "[0-9]+"))
4080 (allout-unprotected
4081 (delete-region (match-beginning 0)(match-end 0)))))
4083 ;; convey 'allout-was-hidden annotation, if original had it:
4084 (if has-annotation
4085 (put-text-property 0 (length new-prefix) 'allout-was-hidden t
4086 new-prefix))
4088 ; Put in new prefix:
4089 (allout-unprotected (insert new-prefix))
4091 ;; Reindent the body if elected, margin changed, and not encrypted body:
4092 (if (and allout-reindent-bodies
4093 (not (= new-depth current-depth))
4094 (not (allout-encrypted-topic-p)))
4095 (allout-reindent-body current-depth new-depth))
4097 (run-hook-with-args 'allout-exposure-change-functions mb me nil)
4099 ;; Recursively rectify successive siblings of orig topic if
4100 ;; caller elected for it:
4101 (if do-successors
4102 (save-excursion
4103 (while (allout-next-sibling new-depth nil)
4104 (setq index
4105 (cond ((numberp index) (1+ index))
4106 ((not number-control) (allout-sibling-index))))
4107 (if (allout-numbered-type-prefix)
4108 (allout-rebullet-heading nil ;;; instead
4109 new-depth ;;; new-depth
4110 number-control;;; number-control
4111 index ;;; index
4112 nil))))) ;;;(dont!)do-successors
4113 ) ; (if (and (= current-depth new-depth)...))
4114 ) ; let* ((current-depth (allout-depth))...)
4115 ) ; defun
4116 ;;;_ > allout-rebullet-topic (arg)
4117 (defun allout-rebullet-topic (arg &optional sans-offspring)
4118 "Rebullet the visible topic containing point and all contained subtopics.
4120 Descends into invisible as well as visible topics, however.
4122 When optional SANS-OFFSPRING is non-nil, subtopics are not
4123 shifted. (Shifting a topic outwards without shifting its
4124 offspring is disallowed, since this would create a \"containment
4125 discontinuity\", where the depth difference between a topic and
4126 its immediate offspring is greater than one.)
4128 With repeat count, shift topic depth by that amount."
4129 (interactive "P")
4130 (let ((start-col (current-column)))
4131 (save-excursion
4132 ;; Normalize arg:
4133 (cond ((null arg) (setq arg 0))
4134 ((listp arg) (setq arg (car arg))))
4135 ;; Fill the user in, in case we're shifting a big topic:
4136 (if (not (zerop arg)) (message "Shifting..."))
4137 (allout-back-to-current-heading)
4138 (if (<= (+ allout-recent-depth arg) 0)
4139 (error "Attempt to shift topic below level 1"))
4140 (allout-rebullet-topic-grunt arg nil nil nil nil sans-offspring)
4141 (if (not (zerop arg)) (message "Shifting... done.")))
4142 (move-to-column (max 0 (+ start-col arg)))))
4143 ;;;_ > allout-rebullet-topic-grunt (&optional relative-depth ...)
4144 (defun allout-rebullet-topic-grunt (&optional relative-depth
4145 starting-depth
4146 starting-point
4147 index
4148 do-successors
4149 sans-offspring)
4150 "Like `allout-rebullet-topic', but on nearest containing topic
4151 \(visible or not).
4153 See `allout-rebullet-heading' for rebulleting behavior.
4155 All arguments are optional.
4157 First arg RELATIVE-DEPTH means to shift the depth of the entire
4158 topic that amount.
4160 Several subsequent args are for internal recursive use by the function
4161 itself: STARTING-DEPTH, STARTING-POINT, and INDEX.
4163 Finally, if optional SANS-OFFSPRING is non-nil then the offspring
4164 are not shifted. (Shifting a topic outwards without shifting
4165 its offspring is disallowed, since this would create a
4166 \"containment discontinuity\", where the depth difference between
4167 a topic and its immediate offspring is greater than one.)"
4169 ;; XXX the recursion here is peculiar, and in general the routine may
4170 ;; need simplification with refactoring.
4172 (if (and sans-offspring
4173 relative-depth
4174 (< relative-depth 0))
4175 (error (concat "Attempt to shift topic outwards without offspring,"
4176 " would cause containment discontinuity.")))
4178 (let* ((relative-depth (or relative-depth 0))
4179 (new-depth (allout-depth))
4180 (starting-depth (or starting-depth new-depth))
4181 (on-starting-call (null starting-point))
4182 (index (or index
4183 ;; Leave index null on starting call, so rebullet-heading
4184 ;; calculates it at what might be new depth:
4185 (and (or (zerop relative-depth)
4186 (not on-starting-call))
4187 (allout-sibling-index))))
4188 (starting-index index)
4189 (moving-outwards (< 0 relative-depth))
4190 (starting-point (or starting-point (point)))
4191 (local-point (point)))
4193 ;; Sanity check for excessive promotion done only on starting call:
4194 (and on-starting-call
4195 moving-outwards
4196 (> 0 (+ starting-depth relative-depth))
4197 (error "Attempt to shift topic out beyond level 1"))
4199 (cond ((= starting-depth new-depth)
4200 ;; We're at depth to work on this one.
4202 ;; When shifting out we work on the children before working on
4203 ;; the parent to avoid interim `allout-aberrant-container-p'
4204 ;; aberrancy, and vice-versa when shifting in:
4205 (if (>= relative-depth 0)
4206 (allout-rebullet-heading nil
4207 (+ starting-depth relative-depth)
4208 nil ;;; number
4209 index
4210 nil)) ;;; do-successors
4211 (when (not sans-offspring)
4212 ;; ... and work on subsequent ones which are at greater depth:
4213 (setq index 0)
4214 (allout-next-heading)
4215 (while (and (not (eobp))
4216 (< starting-depth (allout-depth)))
4217 (setq index (1+ index))
4218 (allout-rebullet-topic-grunt relative-depth
4219 (1+ starting-depth)
4220 starting-point
4221 index)))
4222 (when (< relative-depth 0)
4223 (save-excursion
4224 (goto-char local-point)
4225 (allout-rebullet-heading nil ;;; instead
4226 (+ starting-depth relative-depth)
4227 nil ;;; number
4228 starting-index
4229 nil)))) ;;; do-successors
4231 ((< starting-depth new-depth)
4232 ;; Rare case -- subtopic more than one level deeper than parent.
4233 ;; Treat this one at an even deeper level:
4234 (allout-rebullet-topic-grunt relative-depth
4235 new-depth
4236 starting-point
4237 index
4238 sans-offspring)))
4240 (if on-starting-call
4241 (progn
4242 ;; Rectify numbering of former siblings of the adjusted topic,
4243 ;; if topic has changed depth
4244 (if (or do-successors
4245 (and (not (zerop relative-depth))
4246 (or (= allout-recent-depth starting-depth)
4247 (= allout-recent-depth (+ starting-depth
4248 relative-depth)))))
4249 (allout-rebullet-heading nil nil nil nil t))
4250 ;; Now rectify numbering of new siblings of the adjusted topic,
4251 ;; if depth has been changed:
4252 (progn (goto-char starting-point)
4253 (if (not (zerop relative-depth))
4254 (allout-rebullet-heading nil nil nil nil t)))))
4257 ;;;_ > allout-renumber-to-depth (&optional depth)
4258 (defun allout-renumber-to-depth (&optional depth)
4259 "Renumber siblings at current depth.
4261 Affects superior topics if optional arg DEPTH is less than current depth.
4263 Returns final depth."
4265 ;; Proceed by level, processing subsequent siblings on each,
4266 ;; ascending until we get shallower than the start depth:
4268 (let ((ascender (allout-depth))
4269 was-eobp)
4270 (while (and (not (eobp))
4271 (allout-depth)
4272 (>= allout-recent-depth depth)
4273 (>= ascender depth))
4274 ; Skip over all topics at
4275 ; lesser depths, which can not
4276 ; have been disturbed:
4277 (while (and (not (setq was-eobp (eobp)))
4278 (> allout-recent-depth ascender))
4279 (allout-next-heading))
4280 ; Prime ascender for ascension:
4281 (setq ascender (1- allout-recent-depth))
4282 (if (>= allout-recent-depth depth)
4283 (allout-rebullet-heading nil ;;; instead
4284 nil ;;; depth
4285 nil ;;; number-control
4286 nil ;;; index
4287 t)) ;;; do-successors
4288 (if was-eobp (goto-char (point-max)))))
4289 allout-recent-depth)
4290 ;;;_ > allout-number-siblings (&optional denumber)
4291 (defun allout-number-siblings (&optional denumber)
4292 "Assign numbered topic prefix to this topic and its siblings.
4294 With universal argument, denumber -- assign default bullet to this
4295 topic and its siblings.
4297 With repeated universal argument (`^U^U'), solicit bullet for each
4298 rebulleting each topic at this level."
4300 (interactive "P")
4302 (save-excursion
4303 (allout-back-to-current-heading)
4304 (allout-beginning-of-level)
4305 (let ((depth allout-recent-depth)
4306 (index (if (not denumber) 1))
4307 (use-bullet (equal '(16) denumber))
4308 (more t))
4309 (while more
4310 (allout-rebullet-heading use-bullet ;;; instead
4311 depth ;;; depth
4312 t ;;; number-control
4313 index ;;; index
4314 nil) ;;; do-successors
4315 (if index (setq index (1+ index)))
4316 (setq more (allout-next-sibling depth nil))))))
4317 ;;;_ > allout-shift-in (arg)
4318 (defun allout-shift-in (arg)
4319 "Increase depth of current heading and any items collapsed within it.
4321 With a negative argument, the item is shifted out using
4322 `allout-shift-out', instead.
4324 With an argument greater than one, shift-in the item but not its
4325 offspring, making the item into a sibling of its former children,
4326 and a child of sibling that formerly preceded it.
4328 You are not allowed to shift the first offspring of a topic
4329 inwards, because that would yield a \"containment
4330 discontinuity\", where the depth difference between a topic and
4331 its immediate offspring is greater than one. The first topic in
4332 the file can be adjusted to any positive depth, however."
4334 (interactive "p")
4335 (if (< arg 0)
4336 (allout-shift-out (* arg -1))
4337 ;; refuse to create a containment discontinuity:
4338 (save-excursion
4339 (allout-back-to-current-heading)
4340 (if (not (bobp))
4341 (let* ((current-depth allout-recent-depth)
4342 (start-point (point))
4343 (predecessor-depth (progn
4344 (forward-char -1)
4345 (allout-goto-prefix-doublechecked)
4346 (if (< (point) start-point)
4347 allout-recent-depth
4348 0))))
4349 (if (and (> predecessor-depth 0)
4350 (> (1+ current-depth)
4351 (1+ predecessor-depth)))
4352 (error (concat "Disallowed shift deeper than"
4353 " containing topic's children."))
4354 (allout-back-to-current-heading)
4355 (if (< allout-recent-depth (1+ current-depth))
4356 (allout-show-children))))))
4357 (let ((where (point)))
4358 (allout-rebullet-topic 1 (and (> arg 1) 'sans-offspring))
4359 (run-hook-with-args 'allout-structure-shifted-functions arg where))))
4360 ;;;_ > allout-shift-out (arg)
4361 (defun allout-shift-out (arg)
4362 "Decrease depth of current heading and any topics collapsed within it.
4363 This will make the item a sibling of its former container.
4365 With a negative argument, the item is shifted in using
4366 `allout-shift-in', instead.
4368 With an argument greater than one, shift-out the item's offspring
4369 but not the item itself, making the former children siblings of
4370 the item.
4372 With an argument greater than 1, the item's offspring are shifted
4373 out without shifting the item. This will make the immediate
4374 subtopics into siblings of the item."
4375 (interactive "p")
4376 (if (< arg 0)
4377 (allout-shift-in (* arg -1))
4378 ;; Get proper exposure in this area:
4379 (save-excursion (if (allout-ascend)
4380 (allout-show-children)))
4381 ;; Show collapsed children if there's a successor which will become
4382 ;; their sibling:
4383 (if (and (allout-current-topic-collapsed-p)
4384 (save-excursion (allout-next-sibling)))
4385 (allout-show-children))
4386 (let ((where (and (allout-depth) allout-recent-prefix-beginning)))
4387 (save-excursion
4388 (if (> arg 1)
4389 ;; Shift the offspring but not the topic:
4390 (let ((children-chart (allout-chart-subtree 1)))
4391 (if (listp (car children-chart))
4392 ;; whoops:
4393 (setq children-chart (allout-flatten children-chart)))
4394 (save-excursion
4395 (dolist (child-point children-chart)
4396 (goto-char child-point)
4397 (allout-shift-out 1))))
4398 (allout-rebullet-topic (* arg -1))))
4399 (run-hook-with-args 'allout-structure-shifted-functions (* arg -1) where))))
4400 ;;;_ : Surgery (kill-ring) functions with special provisions for outlines:
4401 ;;;_ > allout-kill-line (&optional arg)
4402 (defun allout-kill-line (&optional arg)
4403 "Kill line, adjusting subsequent lines suitably for outline mode."
4405 (interactive "*P")
4407 (if (or (not (allout-mode-p))
4408 (not (bolp))
4409 (not (save-match-data (looking-at allout-regexp))))
4410 ;; Just do a regular kill:
4411 (kill-line arg)
4412 ;; Ah, have to watch out for adjustments:
4413 (let* ((beg (point))
4415 (beg-hidden (allout-hidden-p))
4416 (end-hidden (save-excursion (allout-end-of-current-line)
4417 (setq end (point))
4418 (allout-hidden-p)))
4419 (depth (allout-depth)))
4421 (allout-annotate-hidden beg end)
4422 (unwind-protect
4423 (if (and (not beg-hidden) (not end-hidden))
4424 (allout-unprotected (kill-line arg))
4425 (kill-line arg))
4426 (run-hooks 'allout-after-copy-or-kill-hook)
4427 (allout-deannotate-hidden beg end)
4429 (if allout-numbered-bullet
4430 (save-excursion ; Renumber subsequent topics if needed:
4431 (if (not (save-match-data (looking-at allout-regexp)))
4432 (allout-next-heading))
4433 (allout-renumber-to-depth depth)))
4434 (run-hook-with-args 'allout-structure-deleted-functions depth (point))))))
4435 ;;;_ > allout-copy-line-as-kill ()
4436 (defun allout-copy-line-as-kill ()
4437 "Like `allout-kill-topic', but save to kill ring instead of deleting."
4438 (interactive)
4439 (let ((buffer-read-only t))
4440 (condition-case nil
4441 (allout-kill-line)
4442 (buffer-read-only nil))))
4443 ;;;_ > allout-kill-topic ()
4444 (defun allout-kill-topic ()
4445 "Kill topic together with subtopics.
4447 Trailing whitespace is killed with a topic if that whitespace:
4449 - would separate the topic from a subsequent sibling
4450 - would separate the topic from the end of buffer
4451 - would not be added to whitespace already separating the topic from the
4452 previous one.
4454 Topic exposure is marked with text-properties, to be used by
4455 `allout-yank-processing' for exposure recovery."
4457 (interactive)
4458 (let* ((inhibit-field-text-motion t)
4459 (beg (prog1 (allout-back-to-current-heading) (beginning-of-line)))
4461 (depth allout-recent-depth))
4462 (allout-end-of-current-subtree)
4463 (if (and (/= (current-column) 0) (not (eobp)))
4464 (forward-char 1))
4465 (if (not (eobp))
4466 (if (and (save-match-data (looking-at "\n"))
4467 (or (save-excursion
4468 (or (not (allout-next-heading))
4469 (= depth allout-recent-depth)))
4470 (and (> (- beg (point-min)) 3)
4471 (string= (buffer-substring (- beg 2) beg) "\n\n"))))
4472 (forward-char 1)))
4474 (allout-annotate-hidden beg (setq end (point)))
4475 (unwind-protect ; for possible barf-if-buffer-read-only.
4476 (allout-unprotected (kill-region beg end))
4477 (allout-deannotate-hidden beg end)
4478 (run-hooks 'allout-after-copy-or-kill-hook)
4480 (save-excursion
4481 (allout-renumber-to-depth depth))
4482 (run-hook-with-args 'allout-structure-deleted-functions depth (point)))))
4483 ;;;_ > allout-copy-topic-as-kill ()
4484 (defun allout-copy-topic-as-kill ()
4485 "Like `allout-kill-topic', but save to kill ring instead of deleting."
4486 (interactive)
4487 (let ((buffer-read-only t))
4488 (condition-case nil
4489 (allout-kill-topic)
4490 (buffer-read-only (message "Topic copied...")))))
4491 ;;;_ > allout-annotate-hidden (begin end)
4492 (defun allout-annotate-hidden (begin end)
4493 "Qualify text with properties to indicate exposure status."
4495 (let ((was-modified (buffer-modified-p))
4496 (buffer-read-only nil))
4497 (allout-deannotate-hidden begin end)
4498 (save-excursion
4499 (goto-char begin)
4500 (let (done next prev overlay)
4501 (while (not done)
4502 ;; at or advance to start of next hidden region:
4503 (if (not (allout-hidden-p))
4504 (setq next
4505 (max (1+ (point))
4506 (allout-next-single-char-property-change (point)
4507 'invisible
4508 nil end))))
4509 (if (or (not next) (eq prev next))
4510 ;; still not at start of hidden area -- must not be any left.
4511 (setq done t)
4512 (goto-char next)
4513 (setq prev next)
4514 (if (not (allout-hidden-p))
4515 ;; still not at start of hidden area.
4516 (setq done t)
4517 (setq overlay (allout-get-invisibility-overlay))
4518 (setq next (overlay-end overlay)
4519 prev next)
4520 ;; advance to end of this hidden area:
4521 (when next
4522 (goto-char next)
4523 (allout-unprotected
4524 (let ((buffer-undo-list t))
4525 (put-text-property (overlay-start overlay) next
4526 'allout-was-hidden t)))))))))
4527 (set-buffer-modified-p was-modified)))
4528 ;;;_ > allout-deannotate-hidden (begin end)
4529 (defun allout-deannotate-hidden (begin end)
4530 "Remove allout hidden-text annotation between BEGIN and END."
4532 (allout-unprotected
4533 (let ((inhibit-read-only t)
4534 (buffer-undo-list t))
4535 (remove-text-properties begin (min end (point-max))
4536 '(allout-was-hidden t)))))
4537 ;;;_ > allout-hide-by-annotation (begin end)
4538 (defun allout-hide-by-annotation (begin end)
4539 "Translate text properties indicating exposure status into actual exposure."
4540 (save-excursion
4541 (goto-char begin)
4542 (let ((was-modified (buffer-modified-p))
4543 done next prev)
4544 (while (not done)
4545 ;; at or advance to start of next annotation:
4546 (if (not (get-text-property (point) 'allout-was-hidden))
4547 (setq next (allout-next-single-char-property-change
4548 (point) 'allout-was-hidden nil end)))
4549 (if (or (not next) (eq prev next))
4550 ;; no more or not advancing -- must not be any left.
4551 (setq done t)
4552 (goto-char next)
4553 (setq prev next)
4554 (if (not (get-text-property (point) 'allout-was-hidden))
4555 ;; still not at start of annotation.
4556 (setq done t)
4557 ;; advance to just after end of this annotation:
4558 (setq next (allout-next-single-char-property-change
4559 (point) 'allout-was-hidden nil end))
4560 (let ((o (make-overlay prev next nil 'front-advance)))
4561 (overlay-put o 'category 'allout-exposure-category)
4562 (overlay-put o 'evaporate t))
4563 (allout-deannotate-hidden prev next)
4564 (setq prev next)
4565 (if next (goto-char next)))))
4566 (set-buffer-modified-p was-modified))))
4567 ;;;_ > allout-yank-processing ()
4568 (defun allout-yank-processing (&optional arg)
4570 "Incidental allout-specific business to be done just after text yanks.
4572 Does depth adjustment of yanked topics, when:
4574 1 the stuff being yanked starts with a valid outline header prefix, and
4575 2 it is being yanked at the end of a line which consists of only a valid
4576 topic prefix.
4578 Also, adjusts numbering of subsequent siblings when appropriate.
4580 Depth adjustment alters the depth of all the topics being yanked
4581 the amount it takes to make the first topic have the depth of the
4582 header into which it's being yanked.
4584 The point is left in front of yanked, adjusted topics, rather than
4585 at the end (and vice-versa with the mark). Non-adjusted yanks,
4586 however, are left exactly like normal, non-allout-specific yanks."
4588 (interactive "*P")
4589 ; Get to beginning, leaving
4590 ; region around subject:
4591 (if (< (allout-mark-marker t) (point))
4592 (exchange-point-and-mark))
4593 (save-match-data
4594 (let* ((subj-beg (point))
4595 (into-bol (bolp))
4596 (subj-end (allout-mark-marker t))
4597 ;; 'resituate' if yanking an entire topic into topic header:
4598 (resituate (and (let ((allout-inhibit-aberrance-doublecheck t))
4599 (allout-e-o-prefix-p))
4600 (looking-at allout-regexp)
4601 (allout-prefix-data)))
4602 ;; `rectify-numbering' if resituating (where several topics may
4603 ;; be resituating) or yanking a topic into a topic slot (bol):
4604 (rectify-numbering (or resituate
4605 (and into-bol (looking-at allout-regexp)))))
4606 (if resituate
4607 ;; Yanking a topic into the start of a topic -- reconcile to fit:
4608 (let* ((inhibit-field-text-motion t)
4609 (prefix-len (if (not (match-end 1))
4611 (- (match-end 1) subj-beg)))
4612 (subj-depth allout-recent-depth)
4613 (prefix-bullet (allout-recent-bullet))
4614 (adjust-to-depth
4615 ;; Nil if adjustment unnecessary, otherwise depth to which
4616 ;; adjustment should be made:
4617 (save-excursion
4618 (and (goto-char subj-end)
4619 (eolp)
4620 (goto-char subj-beg)
4621 (and (looking-at allout-regexp)
4622 (progn
4623 (beginning-of-line)
4624 (not (= (point) subj-beg)))
4625 (looking-at allout-regexp)
4626 (allout-prefix-data))
4627 allout-recent-depth)))
4628 (more t))
4629 (setq rectify-numbering allout-numbered-bullet)
4630 (if adjust-to-depth
4631 ; Do the adjustment:
4632 (progn
4633 (save-restriction
4634 (narrow-to-region subj-beg subj-end)
4635 ; Trim off excessive blank
4636 ; line at end, if any:
4637 (goto-char (point-max))
4638 (if (looking-at "^$")
4639 (allout-unprotected (delete-char -1)))
4640 ; Work backwards, with each
4641 ; shallowest level,
4642 ; successively excluding the
4643 ; last processed topic from
4644 ; the narrow region:
4645 (while more
4646 (allout-back-to-current-heading)
4647 ; go as high as we can in each bunch:
4648 (while (allout-ascend t))
4649 (save-excursion
4650 (allout-unprotected
4651 (allout-rebullet-topic-grunt (- adjust-to-depth
4652 subj-depth)))
4653 (allout-depth))
4654 (if (setq more (not (bobp)))
4655 (progn (widen)
4656 (forward-char -1)
4657 (narrow-to-region subj-beg (point))))))
4658 ;; Remove new heading prefix:
4659 (allout-unprotected
4660 (progn
4661 (delete-region (point) (+ (point)
4662 prefix-len
4663 (- adjust-to-depth
4664 subj-depth)))
4665 ; and delete residual subj
4666 ; prefix digits and space:
4667 (while (looking-at "[0-9]") (delete-char 1))
4668 (delete-char -1)
4669 (if (not (eolp))
4670 (forward-char))))
4671 ;; Assert new topic's bullet - minimal effort if unchanged:
4672 (allout-rebullet-heading (string-to-char prefix-bullet)))
4673 (exchange-point-and-mark))))
4674 (if rectify-numbering
4675 (progn
4676 (save-excursion
4677 ; Give some preliminary feedback:
4678 (message "... reconciling numbers")
4679 ; ... and renumber, in case necessary:
4680 (goto-char subj-beg)
4681 (if (allout-goto-prefix-doublechecked)
4682 (allout-unprotected
4683 (allout-rebullet-heading nil ;;; instead
4684 (allout-depth) ;;; depth
4685 nil ;;; number-control
4686 nil ;;; index
4687 t)))
4688 (message ""))))
4689 (if (or into-bol resituate)
4690 (allout-hide-by-annotation (point) (allout-mark-marker t))
4691 (allout-deannotate-hidden (allout-mark-marker t) (point)))
4692 (if (not resituate)
4693 (exchange-point-and-mark))
4694 (run-hook-with-args 'allout-structure-added-functions subj-beg subj-end))))
4695 ;;;_ > allout-yank (&optional arg)
4696 (defun allout-yank (&optional arg)
4697 "`allout-mode' yank, with depth and numbering adjustment of yanked topics.
4699 Non-topic yanks work no differently than normal yanks.
4701 If a topic is being yanked into a bare topic prefix, the depth of the
4702 yanked topic is adjusted to the depth of the topic prefix.
4704 1 we're yanking in an `allout-mode' buffer
4705 2 the stuff being yanked starts with a valid outline header prefix, and
4706 3 it is being yanked at the end of a line which consists of only a valid
4707 topic prefix.
4709 If these conditions hold then the depth of the yanked topics are all
4710 adjusted the amount it takes to make the first one at the depth of the
4711 header into which it's being yanked.
4713 The point is left in front of yanked, adjusted topics, rather than
4714 at the end (and vice-versa with the mark). Non-adjusted yanks,
4715 however, (ones that don't qualify for adjustment) are handled
4716 exactly like normal yanks.
4718 Numbering of yanked topics, and the successive siblings at the depth
4719 into which they're being yanked, is adjusted.
4721 `allout-yank-pop' works with `allout-yank' just like normal `yank-pop'
4722 works with normal `yank' in non-outline buffers."
4724 (interactive "*P")
4725 (setq this-command 'yank)
4726 (allout-unprotected
4727 (yank arg))
4728 (if (allout-mode-p)
4729 (allout-yank-processing)))
4730 ;;;_ > allout-yank-pop (&optional arg)
4731 (defun allout-yank-pop (&optional arg)
4732 "Yank-pop like `allout-yank' when popping to bare outline prefixes.
4734 Adapts level of popped topics to level of fresh prefix.
4736 Note -- prefix changes to distinctive bullets will stick, if followed
4737 by pops to non-distinctive yanks. Bug..."
4739 (interactive "*p")
4740 (setq this-command 'yank)
4741 (yank-pop arg)
4742 (if (allout-mode-p)
4743 (allout-yank-processing)))
4745 ;;;_ - Specialty bullet functions
4746 ;;;_ : File Cross references
4747 ;;;_ > allout-resolve-xref ()
4748 (defun allout-resolve-xref ()
4749 "Pop to file associated with current heading, if it has an xref bullet.
4751 \(Works according to setting of `allout-file-xref-bullet')."
4752 (interactive)
4753 (if (not allout-file-xref-bullet)
4754 (error
4755 "Outline cross references disabled -- no `allout-file-xref-bullet'")
4756 (if (not (string= (allout-current-bullet) allout-file-xref-bullet))
4757 (error "Current heading lacks cross-reference bullet `%s'"
4758 allout-file-xref-bullet)
4759 (let ((inhibit-field-text-motion t)
4760 file-name)
4761 (save-match-data
4762 (save-excursion
4763 (let* ((text-start allout-recent-prefix-end)
4764 (heading-end (point-at-eol)))
4765 (goto-char text-start)
4766 (setq file-name
4767 (if (re-search-forward "\\s-\\(\\S-*\\)" heading-end t)
4768 (buffer-substring (match-beginning 1)
4769 (match-end 1)))))))
4770 (setq file-name (expand-file-name file-name))
4771 (if (or (file-exists-p file-name)
4772 (if (file-writable-p file-name)
4773 (y-or-n-p (format "%s not there, create one? "
4774 file-name))
4775 (error "%s not found and can't be created" file-name)))
4776 (condition-case failure
4777 (find-file-other-window file-name)
4778 (error failure))
4779 (error "%s not found" file-name))
4785 ;;;_ #6 Exposure Control
4787 ;;;_ - Fundamental
4788 ;;;_ > allout-flag-region (from to flag)
4789 (defun allout-flag-region (from to flag)
4790 "Conceal text between FROM and TO if FLAG is non-nil, else reveal it.
4791 After the exposure changes are made, run the abnormal hook
4792 `allout-exposure-change-functions' with the same arguments as
4793 this function."
4795 ;; We use outline invisibility spec.
4796 (remove-overlays from to 'category 'allout-exposure-category)
4797 (when flag
4798 (let ((o (make-overlay from to nil 'front-advance)))
4799 (overlay-put o 'category 'allout-exposure-category)
4800 (overlay-put o 'evaporate t)
4801 (when (featurep 'xemacs)
4802 (let ((props (symbol-plist 'allout-exposure-category)))
4803 (while props
4804 (condition-case nil
4805 ;; as of 2008-02-27, xemacs lacks modification-hooks
4806 (overlay-put o (pop props) (pop props))
4807 (error nil))))))
4808 (setq allout-this-command-hid-text t))
4809 (run-hook-with-args 'allout-exposure-change-functions from to flag))
4810 ;;;_ > allout-flag-current-subtree (flag)
4811 (defun allout-flag-current-subtree (flag)
4812 "Conceal currently-visible topic's subtree if FLAG non-nil, else reveal it."
4814 (save-excursion
4815 (allout-back-to-current-heading)
4816 (let ((inhibit-field-text-motion t))
4817 (end-of-line))
4818 (allout-flag-region (point)
4819 ;; Exposing must not leave trailing blanks hidden,
4820 ;; but can leave them exposed when hiding, so we
4821 ;; can use flag's inverse as the
4822 ;; include-trailing-blank cue:
4823 (allout-end-of-current-subtree (not flag))
4824 flag)))
4826 ;;;_ - Topic-specific
4827 ;;;_ > allout-show-entry ()
4828 (defun allout-show-entry ()
4829 "Like `allout-show-current-entry', but reveals entries in hidden topics.
4831 This is a way to give restricted peek at a concealed locality without the
4832 expense of exposing its context, but can leave the outline with aberrant
4833 exposure. `allout-show-offshoot' should be used after the peek to rectify
4834 the exposure."
4836 (interactive)
4837 (save-excursion
4838 (let (beg end)
4839 (allout-goto-prefix-doublechecked)
4840 (setq beg (if (allout-hidden-p) (1- (point)) (point)))
4841 (setq end (allout-pre-next-prefix))
4842 (allout-flag-region beg end nil)
4843 (list beg end))))
4844 ;;;_ > allout-show-children (&optional level strict)
4845 (defun allout-show-children (&optional level strict)
4847 "If point is visible, show all direct subheadings of this heading.
4849 Otherwise, do `allout-show-to-offshoot', and then show subheadings.
4851 Optional LEVEL specifies how many levels below the current level
4852 should be shown, or all levels if t. Default is 1.
4854 Optional STRICT means don't resort to -show-to-offshoot, no matter
4855 what. This is basically so -show-to-offshoot, which is called by
4856 this function, can employ the pure offspring-revealing capabilities of
4859 Returns point at end of subtree that was opened, if any. (May get a
4860 point of non-opened subtree?)"
4862 (interactive "p")
4863 (let ((start-point (point)))
4864 (if (and (not strict)
4865 (allout-hidden-p))
4867 (progn (allout-show-to-offshoot) ; Point's concealed, open to
4868 ; expose it.
4869 ;; Then recurse, but with "strict" set so we don't
4870 ;; infinite regress:
4871 (allout-show-children level t))
4873 (save-excursion
4874 (allout-beginning-of-current-line)
4875 (save-restriction
4876 (let* (depth
4877 ;; translate the level spec for this routine to the ones
4878 ;; used by -chart-subtree and -chart-to-reveal:
4879 (chart-level (cond ((not level) 1)
4880 ((eq level t) nil)
4881 (t level)))
4882 (chart (allout-chart-subtree chart-level))
4883 (to-reveal (or (allout-chart-to-reveal chart chart-level)
4884 ;; interactive, show discontinuous children:
4885 (and chart
4886 (allout-called-interactively-p)
4887 (save-excursion
4888 (allout-back-to-current-heading)
4889 (setq depth (allout-current-depth))
4890 (and (allout-next-heading)
4891 (> allout-recent-depth
4892 (1+ depth))))
4893 (message
4894 "Discontinuous offspring; use `%s %s'%s."
4895 (substitute-command-keys
4896 "\\[universal-argument]")
4897 (substitute-command-keys
4898 "\\[allout-shift-out]")
4899 " to elevate them.")
4900 (allout-chart-to-reveal
4901 chart (- allout-recent-depth depth))))))
4902 (goto-char start-point)
4903 (when (and strict (allout-hidden-p))
4904 ;; Concealed root would already have been taken care of,
4905 ;; unless strict was set.
4906 (allout-flag-region (point) (allout-snug-back) nil)
4907 (when allout-show-bodies
4908 (goto-char (car to-reveal))
4909 (allout-show-current-entry)))
4910 (while to-reveal
4911 (goto-char (car to-reveal))
4912 (allout-flag-region (save-excursion (allout-snug-back) (point))
4913 (progn (search-forward "\n" nil t)
4914 (1- (point)))
4915 nil)
4916 (when allout-show-bodies
4917 (goto-char (car to-reveal))
4918 (allout-show-current-entry))
4919 (setq to-reveal (cdr to-reveal)))))))
4920 ;; Compensate for `save-excursion's maintenance of point
4921 ;; within invisible text:
4922 (goto-char start-point)))
4923 ;;;_ > allout-show-to-offshoot ()
4924 (defun allout-show-to-offshoot ()
4925 "Like `allout-show-entry', but reveals all concealed ancestors, as well.
4927 Useful for coherently exposing to a random point in a hidden region."
4928 (interactive)
4929 (save-excursion
4930 (let ((inhibit-field-text-motion t)
4931 (orig-pt (point))
4932 (orig-pref (allout-goto-prefix-doublechecked))
4933 (last-at (point))
4934 (bag-it 0))
4935 (while (or (> bag-it 1) (allout-hidden-p))
4936 (while (allout-hidden-p)
4937 (move-beginning-of-line 1)
4938 (if (allout-hidden-p) (forward-char -1)))
4939 (if (= last-at (setq last-at (point)))
4940 ;; Oops, we're not making any progress! Show the current topic
4941 ;; completely, and try one more time here, if we haven't already.
4942 (progn (beginning-of-line)
4943 (allout-show-current-subtree)
4944 (goto-char orig-pt)
4945 (setq bag-it (1+ bag-it))
4946 (if (> bag-it 1)
4947 (error "allout-show-to-offshoot: %s"
4948 "Stumped by aberrant nesting.")))
4949 (if (> bag-it 0) (setq bag-it 0))
4950 (allout-show-children)
4951 (goto-char orig-pref)))
4952 (goto-char orig-pt)))
4953 (if (allout-hidden-p)
4954 (allout-show-entry)))
4955 ;;;_ > allout-hide-current-entry ()
4956 (defun allout-hide-current-entry ()
4957 "Hide the body directly following this heading."
4958 (interactive)
4959 (allout-back-to-current-heading)
4960 (save-excursion
4961 (let ((inhibit-field-text-motion t))
4962 (end-of-line))
4963 (allout-flag-region (point)
4964 (progn (allout-end-of-entry) (point))
4965 t)))
4966 ;;;_ > allout-show-current-entry (&optional arg)
4967 (defun allout-show-current-entry (&optional arg)
4968 "Show body following current heading, or hide entry with universal argument."
4970 (interactive "P")
4971 (if arg
4972 (allout-hide-current-entry)
4973 (save-excursion (allout-show-to-offshoot))
4974 (save-excursion
4975 (allout-flag-region (point)
4976 (progn (allout-end-of-entry t) (point))
4977 nil)
4979 ;;;_ > allout-show-current-subtree (&optional arg)
4980 (defun allout-show-current-subtree (&optional arg)
4981 "Show everything within the current topic.
4982 With a repeat-count, expose this topic and its siblings."
4983 (interactive "P")
4984 (save-excursion
4985 (if (<= (allout-current-depth) 0)
4986 ;; Outside any topics -- try to get to the first:
4987 (if (not (allout-next-heading))
4988 (error "No topics")
4989 ;; got to first, outermost topic -- set to expose it and siblings:
4990 (message "Above outermost topic -- exposing all.")
4991 (allout-flag-region (point-min)(point-max) nil))
4992 (allout-beginning-of-current-line)
4993 (if (not arg)
4994 (allout-flag-current-subtree nil)
4995 (allout-beginning-of-level)
4996 (allout-expose-topic '(* :))))))
4997 ;;;_ > allout-current-topic-collapsed-p (&optional include-single-liners)
4998 (defun allout-current-topic-collapsed-p (&optional include-single-liners)
4999 "True if the currently visible containing topic is already collapsed.
5001 Single line topics intrinsically can be considered as being both
5002 collapsed and uncollapsed. If optional INCLUDE-SINGLE-LINERS is
5003 true, then single-line topics are considered to be collapsed. By
5004 default, they are treated as being uncollapsed."
5005 (save-match-data
5006 (save-excursion
5007 (and
5008 ;; Is the topic all on one line (allowing for trailing blank line)?
5009 (>= (progn (allout-back-to-current-heading)
5010 (let ((inhibit-field-text-motion t))
5011 (move-end-of-line 1))
5012 (point))
5013 (allout-end-of-current-subtree (not (looking-at "\n\n"))))
5015 (or include-single-liners
5016 (progn (backward-char 1) (allout-hidden-p)))))))
5017 ;;;_ > allout-hide-current-subtree (&optional just-close)
5018 (defun allout-hide-current-subtree (&optional just-close)
5019 "Close the current topic, or containing topic if this one is already closed.
5021 If this topic is closed and it's a top level topic, close this topic
5022 and its siblings.
5024 If optional arg JUST-CLOSE is non-nil, do not close the parent or
5025 siblings, even if the target topic is already closed."
5027 (interactive)
5028 (let* ((from (point))
5029 (sibs-msg "Top-level topic already closed -- closing siblings...")
5030 (current-exposed (not (allout-current-topic-collapsed-p t))))
5031 (cond (current-exposed (allout-flag-current-subtree t))
5032 (just-close nil)
5033 ((allout-ascend) (allout-hide-current-subtree))
5034 (t (goto-char 0)
5035 (message sibs-msg)
5036 (allout-goto-prefix-doublechecked)
5037 (allout-expose-topic '(0 :))
5038 (message (concat sibs-msg " Done."))))
5039 (goto-char from)))
5040 ;;;_ > allout-toggle-current-subtree-exposure
5041 (defun allout-toggle-current-subtree-exposure ()
5042 "Show or hide the current subtree depending on its current state."
5043 ;; thanks to tassilo for suggesting this.
5044 (interactive)
5045 (save-excursion
5046 (allout-back-to-heading)
5047 (if (allout-hidden-p (point-at-eol))
5048 (allout-show-current-subtree)
5049 (allout-hide-current-subtree))))
5050 ;;;_ > allout-show-current-branches ()
5051 (defun allout-show-current-branches ()
5052 "Show all subheadings of this heading, but not their bodies."
5053 (interactive)
5054 (let ((inhibit-field-text-motion t))
5055 (beginning-of-line))
5056 (allout-show-children t))
5057 ;;;_ > allout-hide-current-leaves ()
5058 (defun allout-hide-current-leaves ()
5059 "Hide the bodies of the current topic and all its offspring."
5060 (interactive)
5061 (allout-back-to-current-heading)
5062 (allout-hide-region-body (point) (progn (allout-end-of-current-subtree)
5063 (point))))
5065 ;;;_ - Region and beyond
5066 ;;;_ > allout-show-all ()
5067 (defun allout-show-all ()
5068 "Show all of the text in the buffer."
5069 (interactive)
5070 (message "Exposing entire buffer...")
5071 (allout-flag-region (point-min) (point-max) nil)
5072 (message "Exposing entire buffer... Done."))
5073 ;;;_ > allout-hide-bodies ()
5074 (defun allout-hide-bodies ()
5075 "Hide all of buffer except headings."
5076 (interactive)
5077 (allout-hide-region-body (point-min) (point-max)))
5078 ;;;_ > allout-hide-region-body (start end)
5079 (defun allout-hide-region-body (start end)
5080 "Hide all body lines in the region, but not headings."
5081 (save-match-data
5082 (save-excursion
5083 (save-restriction
5084 (narrow-to-region start end)
5085 (goto-char (point-min))
5086 (let ((inhibit-field-text-motion t))
5087 (while (not (eobp))
5088 (end-of-line)
5089 (allout-flag-region (point) (allout-end-of-entry) t)
5090 (if (not (eobp))
5091 (forward-char
5092 (if (looking-at "\n\n")
5093 2 1)))))))))
5095 ;;;_ > allout-expose-topic (spec)
5096 (defun allout-expose-topic (spec)
5097 "Apply exposure specs to successive outline topic items.
5099 Use the more convenient frontend, `allout-new-exposure', if you don't
5100 need evaluation of the arguments, or even better, the `allout-layout'
5101 variable-keyed mode-activation/auto-exposure feature of allout outline
5102 mode. See the respective documentation strings for more details.
5104 Cursor is left at start position.
5106 SPEC is either a number or a list.
5108 Successive specs on a list are applied to successive sibling topics.
5110 A simple spec (either a number, one of a few symbols, or the null
5111 list) dictates the exposure for the corresponding topic.
5113 Non-null lists recursively designate exposure specs for respective
5114 subtopics of the current topic.
5116 The `:' repeat spec is used to specify exposure for any number of
5117 successive siblings, up to the trailing ones for which there are
5118 explicit specs following the `:'.
5120 Simple (numeric and null-list) specs are interpreted as follows:
5122 Numbers indicate the relative depth to open the corresponding topic.
5123 - negative numbers force the topic to be closed before opening to the
5124 absolute value of the number, so all siblings are open only to
5125 that level.
5126 - positive numbers open to the relative depth indicated by the
5127 number, but do not force already opened subtopics to be closed.
5128 - 0 means to close topic -- hide all offspring.
5129 : - `repeat'
5130 apply prior element to all siblings at current level, *up to*
5131 those siblings that would be covered by specs following the `:'
5132 on the list. Ie, apply to all topics at level but the last
5133 ones. (Only first of multiple colons at same level is
5134 respected -- subsequent ones are discarded.)
5135 * - completely opens the topic, including bodies.
5136 + - shows all the sub headers, but not the bodies
5137 - - exposes the body of the corresponding topic.
5139 Examples:
5140 \(allout-expose-topic '(-1 : 0))
5141 Close this and all following topics at current level, exposing
5142 only their immediate children, but close down the last topic
5143 at this current level completely.
5144 \(allout-expose-topic '(-1 () : 1 0))
5145 Close current topic so only the immediate subtopics are shown;
5146 show the children in the second to last topic, and completely
5147 close the last one.
5148 \(allout-expose-topic '(-2 : -1 *))
5149 Expose children and grandchildren of all topics at current
5150 level except the last two; expose children of the second to
5151 last and completely open the last one."
5153 (interactive "xExposure spec: ")
5154 (if (not (listp spec))
5156 (let ((depth (allout-depth))
5157 (max-pos 0)
5158 prev-elem curr-elem
5159 stay)
5160 (while spec
5161 (setq prev-elem curr-elem
5162 curr-elem (car spec)
5163 spec (cdr spec))
5164 (cond ; Do current element:
5165 ((null curr-elem) nil)
5166 ((symbolp curr-elem)
5167 (cond ((eq curr-elem '*) (allout-show-current-subtree)
5168 (if (> allout-recent-end-of-subtree max-pos)
5169 (setq max-pos allout-recent-end-of-subtree)))
5170 ((eq curr-elem '+)
5171 (if (not (allout-hidden-p))
5172 (save-excursion (allout-hide-current-subtree t)))
5173 (allout-show-current-branches)
5174 (if (> allout-recent-end-of-subtree max-pos)
5175 (setq max-pos allout-recent-end-of-subtree)))
5176 ((eq curr-elem '-) (allout-show-current-entry))
5177 ((eq curr-elem ':)
5178 (setq stay t)
5179 ;; Expand the `repeat' spec to an explicit version,
5180 ;; w.r.t. remaining siblings:
5181 (let ((residue ; = # of sibs not covered by remaining spec
5182 ;; Dang, could be nice to make use of the chart, sigh:
5183 (- (length (allout-chart-siblings))
5184 (length spec))))
5185 (if (< 0 residue)
5186 ;; Some residue -- cover it with prev-elem:
5187 (setq spec (append (make-list residue prev-elem)
5188 spec)))))))
5189 ((numberp curr-elem)
5190 (if (and (>= 0 curr-elem) (not (allout-hidden-p)))
5191 (save-excursion (allout-hide-current-subtree t)
5192 (if (> 0 curr-elem)
5194 (if (> allout-recent-end-of-subtree max-pos)
5195 (setq max-pos
5196 allout-recent-end-of-subtree)))))
5197 (if (> (abs curr-elem) 0)
5198 (progn (allout-show-children (abs curr-elem))
5199 (if (> allout-recent-end-of-subtree max-pos)
5200 (setq max-pos allout-recent-end-of-subtree)))))
5201 ((listp curr-elem)
5202 (if (allout-descend-to-depth (1+ depth))
5203 (let ((got (allout-expose-topic curr-elem)))
5204 (if (and got (> got max-pos)) (setq max-pos got))))))
5205 (cond (stay (setq stay nil))
5206 ((listp (car spec)) nil)
5207 ((> max-pos (point))
5208 ;; Capitalize on max-pos state to get us nearer next sibling:
5209 (progn (goto-char (min (point-max) max-pos))
5210 (allout-next-heading)))
5211 ((allout-next-sibling depth))))
5212 max-pos)))
5213 ;;;_ > allout-old-expose-topic (spec &rest followers)
5214 (defun allout-old-expose-topic (spec &rest followers)
5216 "Deprecated. Use `allout-expose-topic' (with different schema
5217 format) instead.
5219 Dictate wholesale exposure scheme for current topic, according to SPEC.
5221 SPEC is either a number or a list. Optional successive args
5222 dictate exposure for subsequent siblings of current topic.
5224 A simple spec (either a number, a special symbol, or the null list)
5225 dictates the overall exposure for a topic. Non null lists are
5226 composite specs whose first element dictates the overall exposure for
5227 a topic, with the subsequent elements in the list interpreted as specs
5228 that dictate the exposure for the successive offspring of the topic.
5230 Simple (numeric and null-list) specs are interpreted as follows:
5232 - Numbers indicate the relative depth to open the corresponding topic:
5233 - negative numbers force the topic to be close before opening to the
5234 absolute value of the number.
5235 - positive numbers just open to the relative depth indicated by the number.
5236 - 0 just closes
5237 - `*' completely opens the topic, including bodies.
5238 - `+' shows all the sub headers, but not the bodies
5239 - `-' exposes the body and immediate offspring of the corresponding topic.
5241 If the spec is a list, the first element must be a number, which
5242 dictates the exposure depth of the topic as a whole. Subsequent
5243 elements of the list are nested SPECs, dictating the specific exposure
5244 for the corresponding offspring of the topic.
5246 Optional FOLLOWERS arguments dictate exposure for succeeding siblings."
5248 (interactive "xExposure spec: ")
5249 (let ((inhibit-field-text-motion t)
5250 (depth (allout-current-depth))
5251 max-pos)
5252 (cond ((null spec) nil)
5253 ((symbolp spec)
5254 (if (eq spec '*) (allout-show-current-subtree))
5255 (if (eq spec '+) (allout-show-current-branches))
5256 (if (eq spec '-) (allout-show-current-entry)))
5257 ((numberp spec)
5258 (if (>= 0 spec)
5259 (save-excursion (allout-hide-current-subtree t)
5260 (end-of-line)
5261 (if (or (not max-pos)
5262 (> (point) max-pos))
5263 (setq max-pos (point)))
5264 (if (> 0 spec)
5265 (setq spec (* -1 spec)))))
5266 (if (> spec 0)
5267 (allout-show-children spec)))
5268 ((listp spec)
5269 ;(let ((got (allout-old-expose-topic (car spec))))
5270 ; (if (and got (or (not max-pos) (> got max-pos)))
5271 ; (setq max-pos got)))
5272 (let ((new-depth (+ (allout-current-depth) 1))
5273 got)
5274 (setq max-pos (allout-old-expose-topic (car spec)))
5275 (setq spec (cdr spec))
5276 (if (and spec
5277 (allout-descend-to-depth new-depth)
5278 (not (allout-hidden-p)))
5279 (progn (setq got (apply 'allout-old-expose-topic spec))
5280 (if (and got (or (not max-pos) (> got max-pos)))
5281 (setq max-pos got)))))))
5282 (while (and followers
5283 (progn (if (and max-pos (< (point) max-pos))
5284 (progn (goto-char max-pos)
5285 (setq max-pos nil)))
5286 (end-of-line)
5287 (allout-next-sibling depth)))
5288 (allout-old-expose-topic (car followers))
5289 (setq followers (cdr followers)))
5290 max-pos))
5291 ;;;_ > allout-new-exposure '()
5292 (defmacro allout-new-exposure (&rest spec)
5293 "Literal frontend for `allout-expose-topic', doesn't evaluate arguments.
5294 Some arguments that would need to be quoted in `allout-expose-topic'
5295 need not be quoted in `allout-new-exposure'.
5297 Cursor is left at start position.
5299 Use this instead of obsolete `allout-exposure'.
5301 Examples:
5302 \(allout-new-exposure (-1 () () () 1) 0)
5303 Close current topic at current level so only the immediate
5304 subtopics are shown, except also show the children of the
5305 third subtopic; and close the next topic at the current level.
5306 \(allout-new-exposure : -1 0)
5307 Close all topics at current level to expose only their
5308 immediate children, except for the last topic at the current
5309 level, in which even its immediate children are hidden.
5310 \(allout-new-exposure -2 : -1 *)
5311 Expose children and grandchildren of first topic at current
5312 level, and expose children of subsequent topics at current
5313 level *except* for the last, which should be opened completely."
5314 `(save-excursion
5315 (if (not (or (allout-goto-prefix-doublechecked)
5316 (allout-next-heading)))
5317 (error "allout-new-exposure: Can't find any outline topics"))
5318 (allout-expose-topic ',spec)))
5320 ;;;_ #7 Systematic outline presentation -- copying, printing, flattening
5322 ;;;_ - Mapping and processing of topics
5323 ;;;_ ( See also Subtree Charting, in Navigation code.)
5324 ;;;_ > allout-stringify-flat-index (flat-index)
5325 (defun allout-stringify-flat-index (flat-index &optional context)
5326 "Convert list representing section/subsection/... to document string.
5328 Optional arg CONTEXT indicates interior levels to include."
5329 (let ((delim ".")
5330 result
5331 numstr
5332 (context-depth (or (and context 2) 1)))
5333 ;; Take care of the explicit context:
5334 (while (> context-depth 0)
5335 (setq numstr (int-to-string (car flat-index))
5336 flat-index (cdr flat-index)
5337 result (if flat-index
5338 (cons delim (cons numstr result))
5339 (cons numstr result))
5340 context-depth (if flat-index (1- context-depth) 0)))
5341 (setq delim " ")
5342 ;; Take care of the indentation:
5343 (if flat-index
5344 (progn
5345 (while flat-index
5346 (setq result
5347 (cons delim
5348 (cons (make-string
5349 (1+ (truncate (if (zerop (car flat-index))
5351 (log10 (car flat-index)))))
5353 result)))
5354 (setq flat-index (cdr flat-index)))
5355 ;; Dispose of single extra delim:
5356 (setq result (cdr result))))
5357 (apply 'concat result)))
5358 ;;;_ > allout-stringify-flat-index-plain (flat-index)
5359 (defun allout-stringify-flat-index-plain (flat-index)
5360 "Convert list representing section/subsection/... to document string."
5361 (let ((delim ".")
5362 result)
5363 (while flat-index
5364 (setq result (cons (int-to-string (car flat-index))
5365 (if result
5366 (cons delim result))))
5367 (setq flat-index (cdr flat-index)))
5368 (apply 'concat result)))
5369 ;;;_ > allout-stringify-flat-index-indented (flat-index)
5370 (defun allout-stringify-flat-index-indented (flat-index)
5371 "Convert list representing section/subsection/... to document string."
5372 (let ((delim ".")
5373 result
5374 numstr)
5375 ;; Take care of the explicit context:
5376 (setq numstr (int-to-string (car flat-index))
5377 flat-index (cdr flat-index)
5378 result (if flat-index
5379 (cons delim (cons numstr result))
5380 (cons numstr result)))
5381 (setq delim " ")
5382 ;; Take care of the indentation:
5383 (if flat-index
5384 (progn
5385 (while flat-index
5386 (setq result
5387 (cons delim
5388 (cons (make-string
5389 (1+ (truncate (if (zerop (car flat-index))
5391 (log10 (car flat-index)))))
5393 result)))
5394 (setq flat-index (cdr flat-index)))
5395 ;; Dispose of single extra delim:
5396 (setq result (cdr result))))
5397 (apply 'concat result)))
5398 ;;;_ > allout-listify-exposed (&optional start end format)
5399 (defun allout-listify-exposed (&optional start end format)
5401 "Produce a list representing exposed topics in current region.
5403 This list can then be used by `allout-process-exposed' to manipulate
5404 the subject region.
5406 Optional START and END indicate bounds of region.
5408 Optional arg, FORMAT, designates an alternate presentation form for
5409 the prefix:
5411 list -- Present prefix as numeric section.subsection..., starting with
5412 section indicated by the list, innermost nesting first.
5413 `indent' (symbol) -- Convert header prefixes to all white space,
5414 except for distinctive bullets.
5416 The elements of the list produced are lists that represents a topic
5417 header and body. The elements of that list are:
5419 - a number representing the depth of the topic,
5420 - a string representing the header-prefix, including trailing whitespace and
5421 bullet.
5422 - a string representing the bullet character,
5423 - and a series of strings, each containing one line of the exposed
5424 portion of the topic entry."
5426 (interactive "r")
5427 (save-excursion
5428 (let*
5429 ((inhibit-field-text-motion t)
5430 ;; state vars:
5431 strings prefix result depth new-depth out gone-out bullet beg
5432 next done)
5434 (goto-char start)
5435 (beginning-of-line)
5436 ;; Goto initial topic, and register preceding stuff, if any:
5437 (if (> (allout-goto-prefix-doublechecked) start)
5438 ;; First topic follows beginning point -- register preliminary stuff:
5439 (setq result
5440 (list (list 0 "" nil
5441 (buffer-substring-no-properties start
5442 (1- (point)))))))
5443 (while (and (not done)
5444 (not (eobp)) ; Loop until we've covered the region.
5445 (not (> (point) end)))
5446 (setq depth allout-recent-depth ; Current topics depth,
5447 bullet (allout-recent-bullet) ; ... bullet,
5448 prefix (allout-recent-prefix)
5449 beg (progn (allout-end-of-prefix t) (point))) ; and beginning.
5450 (setq done ; The boundary for the current topic:
5451 (not (allout-next-visible-heading 1)))
5452 (setq new-depth allout-recent-depth)
5453 (setq gone-out out
5454 out (< new-depth depth))
5455 (beginning-of-line)
5456 (setq next (point))
5457 (goto-char beg)
5458 (setq strings nil)
5459 (while (> next (point)) ; Get all the exposed text in
5460 (setq strings
5461 (cons (buffer-substring-no-properties
5463 ;To hidden text or end of line:
5464 (progn
5465 (end-of-line)
5466 (allout-back-to-visible-text)))
5467 strings))
5468 (when (< (point) next) ; Resume from after hid text, if any.
5469 (line-move 1)
5470 (beginning-of-line))
5471 (setq beg (point)))
5472 ;; Accumulate list for this topic:
5473 (setq strings (nreverse strings))
5474 (setq result
5475 (cons
5476 (if format
5477 (let ((special (if (string-match
5478 (regexp-quote bullet)
5479 allout-distinctive-bullets-string)
5480 bullet)))
5481 (cond ((listp format)
5482 (list depth
5483 (if allout-flattened-numbering-abbreviation
5484 (allout-stringify-flat-index format
5485 gone-out)
5486 (allout-stringify-flat-index-plain
5487 format))
5488 strings
5489 special))
5490 ((eq format 'indent)
5491 (if special
5492 (list depth
5493 (concat (make-string (1+ depth) ? )
5494 (substring prefix -1))
5495 strings)
5496 (list depth
5497 (make-string depth ? )
5498 strings)))
5499 (t (error "allout-listify-exposed: %s %s"
5500 "invalid format" format))))
5501 (list depth prefix strings))
5502 result))
5503 ;; Reassess format, if any:
5504 (if (and format (listp format))
5505 (cond ((= new-depth depth)
5506 (setq format (cons (1+ (car format))
5507 (cdr format))))
5508 ((> new-depth depth) ; descending -- assume by 1:
5509 (setq format (cons 1 format)))
5511 ; Pop the residue:
5512 (while (< new-depth depth)
5513 (setq format (cdr format))
5514 (setq depth (1- depth)))
5515 ; And increment the current one:
5516 (setq format
5517 (cons (1+ (or (car format)
5518 -1))
5519 (cdr format)))))))
5520 ;; Put the list with first at front, to last at back:
5521 (nreverse result))))
5522 ;;;_ > allout-region-active-p ()
5523 (defmacro allout-region-active-p ()
5524 (cond ((fboundp 'use-region-p) '(use-region-p))
5525 ((fboundp 'region-active-p) '(region-active-p))
5526 (t 'mark-active)))
5527 ;;_ > allout-process-exposed (&optional func from to frombuf
5528 ;;; tobuf format)
5529 (defun allout-process-exposed (&optional func from to frombuf tobuf
5530 format start-num)
5531 "Map function on exposed parts of current topic; results to another buffer.
5533 All args are options; default values itemized below.
5535 Apply FUNCTION to exposed portions FROM position TO position in buffer
5536 FROMBUF to buffer TOBUF. Sixth optional arg, FORMAT, designates an
5537 alternate presentation form:
5539 `flat' -- Present prefix as numeric section.subsection..., starting with
5540 section indicated by the START-NUM, innermost nesting first.
5541 X`flat-indented' -- Prefix is like `flat' for first topic at each
5542 X level, but subsequent topics have only leaf topic
5543 X number, padded with blanks to line up with first.
5544 `indent' (symbol) -- Convert header prefixes to all white space,
5545 except for distinctive bullets.
5547 Defaults:
5548 FUNCTION: `allout-insert-listified'
5549 FROM: region start, if region active, else start of buffer
5550 TO: region end, if region active, else end of buffer
5551 FROMBUF: current buffer
5552 TOBUF: buffer name derived: \"*current-buffer-name exposed*\"
5553 FORMAT: nil"
5555 ; Resolve arguments,
5556 ; defaulting if necessary:
5557 (if (not func) (setq func 'allout-insert-listified))
5558 (if (not (and from to))
5559 (if (allout-region-active-p)
5560 (setq from (region-beginning) to (region-end))
5561 (setq from (point-min) to (point-max))))
5562 (if frombuf
5563 (if (not (bufferp frombuf))
5564 ;; Specified but not a buffer -- get it:
5565 (let ((got (get-buffer frombuf)))
5566 (if (not got)
5567 (error (concat "allout-process-exposed: source buffer "
5568 frombuf
5569 " not found."))
5570 (setq frombuf got))))
5571 ;; not specified -- default it:
5572 (setq frombuf (current-buffer)))
5573 (if tobuf
5574 (if (not (bufferp tobuf))
5575 (setq tobuf (get-buffer-create tobuf)))
5576 ;; not specified -- default it:
5577 (setq tobuf (concat "*" (buffer-name frombuf) " exposed*")))
5578 (if (listp format)
5579 (nreverse format))
5581 (let* ((listified
5582 (progn (set-buffer frombuf)
5583 (allout-listify-exposed from to format))))
5584 (set-buffer tobuf)
5585 (mapc func listified)
5586 (pop-to-buffer tobuf)))
5588 ;;;_ - Copy exposed
5589 ;;;_ > allout-insert-listified (listified)
5590 (defun allout-insert-listified (listified)
5591 "Insert contents of listified outline portion in current buffer.
5593 LISTIFIED is a list representing each topic header and body:
5595 \`(depth prefix text)'
5597 or \`(depth prefix text bullet-plus)'
5599 If `bullet-plus' is specified, it is inserted just after the entire prefix."
5600 (setq listified (cdr listified))
5601 (let ((prefix (prog1
5602 (car listified)
5603 (setq listified (cdr listified))))
5604 (text (prog1
5605 (car listified)
5606 (setq listified (cdr listified))))
5607 (bullet-plus (car listified)))
5608 (insert prefix)
5609 (if bullet-plus (insert (concat " " bullet-plus)))
5610 (while text
5611 (insert (car text))
5612 (if (setq text (cdr text))
5613 (insert "\n")))
5614 (insert "\n")))
5615 ;;;_ > allout-copy-exposed-to-buffer (&optional arg tobuf format)
5616 (defun allout-copy-exposed-to-buffer (&optional arg tobuf format)
5617 "Duplicate exposed portions of current outline to another buffer.
5619 Other buffer has current buffers name with \" exposed\" appended to it.
5621 With repeat count, copy the exposed parts of only the current topic.
5623 Optional second arg TOBUF is target buffer name.
5625 Optional third arg FORMAT, if non-nil, symbolically designates an
5626 alternate presentation format for the outline:
5628 `flat' - Convert topic header prefixes to numeric
5629 section.subsection... identifiers.
5630 `indent' - Convert header prefixes to all white space, except for
5631 distinctive bullets.
5632 `indent-flat' - The best of both - only the first of each level has
5633 the full path, the rest have only the section number
5634 of the leaf, preceded by the right amount of indentation."
5636 (interactive "P")
5637 (if (not tobuf)
5638 (setq tobuf (get-buffer-create (concat "*" (buffer-name) " exposed*"))))
5639 (let* ((start-pt (point))
5640 (beg (if arg (allout-back-to-current-heading) (point-min)))
5641 (end (if arg (allout-end-of-current-subtree) (point-max)))
5642 (buf (current-buffer))
5643 (start-list ()))
5644 (if (eq format 'flat)
5645 (setq format (if arg (save-excursion
5646 (goto-char beg)
5647 (allout-topic-flat-index))
5648 '(1))))
5649 (with-current-buffer tobuf (erase-buffer))
5650 (allout-process-exposed 'allout-insert-listified
5653 (current-buffer)
5654 tobuf
5655 format start-list)
5656 (goto-char (point-min))
5657 (pop-to-buffer buf)
5658 (goto-char start-pt)))
5659 ;;;_ > allout-flatten-exposed-to-buffer (&optional arg tobuf)
5660 (defun allout-flatten-exposed-to-buffer (&optional arg tobuf)
5661 "Present numeric outline of outline's exposed portions in another buffer.
5663 The resulting outline is not compatible with outline mode -- use
5664 `allout-copy-exposed-to-buffer' if you want that.
5666 Use `allout-indented-exposed-to-buffer' for indented presentation.
5668 With repeat count, copy the exposed portions of only current topic.
5670 Other buffer has current buffer's name with \" exposed\" appended to
5671 it, unless optional second arg TOBUF is specified, in which case it is
5672 used verbatim."
5673 (interactive "P")
5674 (allout-copy-exposed-to-buffer arg tobuf 'flat))
5675 ;;;_ > allout-indented-exposed-to-buffer (&optional arg tobuf)
5676 (defun allout-indented-exposed-to-buffer (&optional arg tobuf)
5677 "Present indented outline of outline's exposed portions in another buffer.
5679 The resulting outline is not compatible with outline mode -- use
5680 `allout-copy-exposed-to-buffer' if you want that.
5682 Use `allout-flatten-exposed-to-buffer' for numeric sectional presentation.
5684 With repeat count, copy the exposed portions of only current topic.
5686 Other buffer has current buffer's name with \" exposed\" appended to
5687 it, unless optional second arg TOBUF is specified, in which case it is
5688 used verbatim."
5689 (interactive "P")
5690 (allout-copy-exposed-to-buffer arg tobuf 'indent))
5692 ;;;_ - LaTeX formatting
5693 ;;;_ > allout-latex-verb-quote (string &optional flow)
5694 (defun allout-latex-verb-quote (string &optional flow)
5695 "Return copy of STRING for literal reproduction across LaTeX processing.
5696 Expresses the original characters (including carriage returns) of the
5697 string across LaTeX processing."
5698 (mapconcat (function
5699 (lambda (char)
5700 (cond ((memq char '(?\\ ?$ ?% ?# ?& ?{ ?} ?_ ?^ ?- ?*))
5701 (concat "\\char" (number-to-string char) "{}"))
5702 ((= char ?\n) "\\\\")
5703 (t (char-to-string char)))))
5704 string
5705 ""))
5706 ;;;_ > allout-latex-verbatim-quote-curr-line ()
5707 (defun allout-latex-verbatim-quote-curr-line ()
5708 "Express line for exact (literal) representation across LaTeX processing.
5710 Adjust line contents so it is unaltered (from the original line)
5711 across LaTeX processing, within the context of a `verbatim'
5712 environment. Leaves point at the end of the line."
5713 (let ((inhibit-field-text-motion t))
5714 (beginning-of-line)
5715 (let ((beg (point))
5716 (end (point-at-eol)))
5717 (save-match-data
5718 (while (re-search-forward "\\\\"
5719 ;;"\\\\\\|\\{\\|\\}\\|\\_\\|\\$\\|\\\"\\|\\&\\|\\^\\|\\-\\|\\*\\|#"
5720 end ; bounded by end-of-line
5721 1) ; no matches, move to end & return nil
5722 (goto-char (match-beginning 2))
5723 (insert "\\")
5724 (setq end (1+ end))
5725 (goto-char (1+ (match-end 2))))))))
5726 ;;;_ > allout-insert-latex-header (buffer)
5727 (defun allout-insert-latex-header (buffer)
5728 "Insert initial LaTeX commands at point in BUFFER."
5729 ;; Much of this is being derived from the stuff in appendix of E in
5730 ;; the TeXBook, pg 421.
5731 (set-buffer buffer)
5732 (let ((doc-style (format "\n\\documentstyle{%s}\n"
5733 "report"))
5734 (page-numbering (if allout-number-pages
5735 "\\pagestyle{empty}\n"
5736 ""))
5737 (titlecmd (format "\\newcommand{\\titlecmd}[1]{{%s #1}}\n"
5738 allout-title-style))
5739 (labelcmd (format "\\newcommand{\\labelcmd}[1]{{%s #1}}\n"
5740 allout-label-style))
5741 (headlinecmd (format "\\newcommand{\\headlinecmd}[1]{{%s #1}}\n"
5742 allout-head-line-style))
5743 (bodylinecmd (format "\\newcommand{\\bodylinecmd}[1]{{%s #1}}\n"
5744 allout-body-line-style))
5745 (setlength (format "%s%s%s%s"
5746 "\\newlength{\\stepsize}\n"
5747 "\\setlength{\\stepsize}{"
5748 allout-indent
5749 "}\n"))
5750 (oneheadline (format "%s%s%s%s%s%s%s"
5751 "\\newcommand{\\OneHeadLine}[3]{%\n"
5752 "\\noindent%\n"
5753 "\\hspace*{#2\\stepsize}%\n"
5754 "\\labelcmd{#1}\\hspace*{.2cm}"
5755 "\\headlinecmd{#3}\\\\["
5756 allout-line-skip
5757 "]\n}\n"))
5758 (onebodyline (format "%s%s%s%s%s%s"
5759 "\\newcommand{\\OneBodyLine}[2]{%\n"
5760 "\\noindent%\n"
5761 "\\hspace*{#1\\stepsize}%\n"
5762 "\\bodylinecmd{#2}\\\\["
5763 allout-line-skip
5764 "]\n}\n"))
5765 (begindoc "\\begin{document}\n\\begin{center}\n")
5766 (title (format "%s%s%s%s"
5767 "\\titlecmd{"
5768 (allout-latex-verb-quote (if allout-title
5769 (condition-case nil
5770 (eval allout-title)
5771 (error "<unnamed buffer>"))
5772 "Unnamed Outline"))
5773 "}\n"
5774 "\\end{center}\n\n"))
5775 (hsize "\\hsize = 7.5 true in\n")
5776 (hoffset "\\hoffset = -1.5 true in\n")
5777 (vspace "\\vspace{.1cm}\n\n"))
5778 (insert (concat doc-style
5779 page-numbering
5780 titlecmd
5781 labelcmd
5782 headlinecmd
5783 bodylinecmd
5784 setlength
5785 oneheadline
5786 onebodyline
5787 begindoc
5788 title
5789 hsize
5790 hoffset
5791 vspace)
5793 ;;;_ > allout-insert-latex-trailer (buffer)
5794 (defun allout-insert-latex-trailer (buffer)
5795 "Insert concluding LaTeX commands at point in BUFFER."
5796 (set-buffer buffer)
5797 (insert "\n\\end{document}\n"))
5798 ;;;_ > allout-latexify-one-item (depth prefix bullet text)
5799 (defun allout-latexify-one-item (depth prefix bullet text)
5800 "Insert LaTeX commands for formatting one outline item.
5802 Args are the topics numeric DEPTH, the header PREFIX lead string, the
5803 BULLET string, and a list of TEXT strings for the body."
5804 (let* ((head-line (if text (car text)))
5805 (body-lines (cdr text))
5806 (curr-line)
5807 body-content bop)
5808 ; Do the head line:
5809 (insert (concat "\\OneHeadLine{\\verb\1 "
5810 (allout-latex-verb-quote bullet)
5811 "\1}{"
5812 depth
5813 "}{\\verb\1 "
5814 (if head-line
5815 (allout-latex-verb-quote head-line)
5817 "\1}\n"))
5818 (if (not body-lines)
5820 ;;(insert "\\beginlines\n")
5821 (insert "\\begin{verbatim}\n")
5822 (while body-lines
5823 (setq curr-line (car body-lines))
5824 (if (and (not body-content)
5825 (not (string-match "^\\s-*$" curr-line)))
5826 (setq body-content t))
5827 ; Mangle any occurrences of
5828 ; "\end{verbatim}" in text,
5829 ; it's special:
5830 (if (and body-content
5831 (setq bop (string-match "\\end{verbatim}" curr-line)))
5832 (setq curr-line (concat (substring curr-line 0 bop)
5834 (substring curr-line bop))))
5835 ;;(insert "|" (car body-lines) "|")
5836 (insert curr-line)
5837 (allout-latex-verbatim-quote-curr-line)
5838 (insert "\n")
5839 (setq body-lines (cdr body-lines)))
5840 (if body-content
5841 (setq body-content nil)
5842 (forward-char -1)
5843 (insert "\\ ")
5844 (forward-char 1))
5845 ;;(insert "\\endlines\n")
5846 (insert "\\end{verbatim}\n")
5848 ;;;_ > allout-latexify-exposed (arg &optional tobuf)
5849 (defun allout-latexify-exposed (arg &optional tobuf)
5850 "Format current topics exposed portions to TOBUF for LaTeX processing.
5851 TOBUF defaults to a buffer named the same as the current buffer, but
5852 with \"*\" prepended and \" latex-formed*\" appended.
5854 With repeat count, copy the exposed portions of entire buffer."
5856 (interactive "P")
5857 (if (not tobuf)
5858 (setq tobuf
5859 (get-buffer-create (concat "*" (buffer-name) " latexified*"))))
5860 (let* ((start-pt (point))
5861 (beg (if arg (point-min) (allout-back-to-current-heading)))
5862 (end (if arg (point-max) (allout-end-of-current-subtree)))
5863 (buf (current-buffer)))
5864 (set-buffer tobuf)
5865 (erase-buffer)
5866 (allout-insert-latex-header tobuf)
5867 (goto-char (point-max))
5868 (allout-process-exposed 'allout-latexify-one-item
5872 tobuf)
5873 (goto-char (point-max))
5874 (allout-insert-latex-trailer tobuf)
5875 (goto-char (point-min))
5876 (pop-to-buffer buf)
5877 (goto-char start-pt)))
5879 ;;;_ #8 Encryption
5880 ;;;_ > allout-toggle-current-subtree-encryption (&optional keymode-cue)
5881 (defun allout-toggle-current-subtree-encryption (&optional keymode-cue)
5882 "Encrypt clear or decrypt encoded topic text.
5884 Allout uses Emacs 'epg' library to perform encryption. Symmetric
5885 and keypair encryption are supported. All encryption is ascii
5886 armored.
5888 Entry encryption defaults to symmetric key mode unless keypair
5889 recipients are associated with the file (see
5890 `epa-file-encrypt-to') or the function is invoked with a
5891 \(KEYMODE-CUE) universal argument greater than 1.
5893 When encrypting, KEYMODE-CUE universal argument greater than 1
5894 causes prompting for recipients for public-key keypair
5895 encryption. Selecting no recipients results in symmetric key
5896 encryption.
5898 Further, encrypting with a KEYMODE-CUE universal argument greater
5899 than 4 - eg, preceded by a doubled Ctrl-U - causes association of
5900 the specified recipients with the file, replacing those currently
5901 associated with it. This can be used to dissociate any
5902 recipients with the file, by selecting no recipients in the
5903 dialog.
5905 Encrypted topic's bullets are set to a `~' to signal that the
5906 contents of the topic (body and subtopics, but not heading) is
5907 pending encryption or encrypted. `*' asterisk immediately after
5908 the bullet signals that the body is encrypted, its absence means
5909 the topic is meant to be encrypted but is not currently. When a
5910 file with topics pending encryption is saved, topics pending
5911 encryption are encrypted. See `allout-encrypt-unencrypted-on-saves'
5912 for auto-encryption specifics.
5914 \*NOTE WELL* that automatic encryption that happens during saves will
5915 default to symmetric encryption -- you must deliberately (re)encrypt key-pair
5916 encrypted topics if you want them to continue to use the key-pair cipher.
5918 Level-one topics, with prefix consisting solely of an `*' asterisk, cannot be
5919 encrypted. If you want to encrypt the contents of a top-level topic, use
5920 \\[allout-shift-in] to increase its depth."
5921 (interactive "P")
5922 (save-excursion
5923 (allout-back-to-current-heading)
5924 (allout-toggle-subtree-encryption keymode-cue)))
5925 ;;;_ > allout-toggle-subtree-encryption (&optional keymode-cue)
5926 (defun allout-toggle-subtree-encryption (&optional keymode-cue)
5927 "Encrypt clear text or decrypt encoded topic contents (body and subtopics.)
5929 Entry encryption defaults to symmetric key mode unless keypair
5930 recipients are associated with the file (see
5931 `epa-file-encrypt-to') or the function is invoked with a
5932 \(KEYMODE-CUE) universal argument greater than 1.
5934 When encrypting, KEYMODE-CUE universal argument greater than 1
5935 causes prompting for recipients for public-key keypair
5936 encryption. Selecting no recipients results in symmetric key
5937 encryption.
5939 Further, encrypting with a KEYMODE-CUE universal argument greater
5940 than 4 - eg, preceded by a doubled Ctrl-U - causes association of
5941 the specified recipients with the file, replacing those currently
5942 associated with it. This can be used to dissociate any
5943 recipients with the file, by selecting no recipients in the
5944 dialog.
5946 Encryption and decryption uses the Emacs 'epg' library.
5948 Encrypted text will be ascii-armored.
5950 See `allout-toggle-current-subtree-encryption' for more details."
5952 (interactive "P")
5953 (save-excursion
5954 (allout-end-of-prefix t)
5956 (if (= allout-recent-depth 1)
5957 (error (concat "Cannot encrypt or decrypt level 1 topics -"
5958 " shift it in to make it encryptable")))
5960 (let* ((allout-buffer (current-buffer))
5961 ;; for use with allout-auto-save-temporarily-disabled, if necessary:
5962 (was-buffer-saved-size buffer-saved-size)
5963 ;; Assess location:
5964 (bullet-pos allout-recent-prefix-beginning)
5965 (after-bullet-pos (point))
5966 (was-encrypted
5967 (progn (if (= (point-max) after-bullet-pos)
5968 (error "no body to encrypt"))
5969 (allout-encrypted-topic-p)))
5970 (was-collapsed (if (not (search-forward "\n" nil t))
5972 (backward-char 1)
5973 (allout-hidden-p)))
5974 (subtree-beg (1+ (point)))
5975 (subtree-end (allout-end-of-subtree))
5976 (subject-text (buffer-substring-no-properties subtree-beg
5977 subtree-end))
5978 (subtree-end-char (char-after (1- subtree-end)))
5979 (subtree-trailing-char (char-after subtree-end))
5980 ;; kluge -- result-text needs to be nil, but we also want to
5981 ;; check for the error condition
5982 (result-text (if (or (string= "" subject-text)
5983 (string= "\n" subject-text))
5984 (error "No topic contents to %scrypt"
5985 (if was-encrypted "de" "en"))
5986 nil))
5987 ;; Assess key parameters:
5988 (was-coding-system buffer-file-coding-system))
5990 (when (not was-encrypted)
5991 ;; ensure that non-ascii chars pending encryption are noticed before
5992 ;; they're encrypted, so the coding system is set to accommodate
5993 ;; them.
5994 (setq buffer-file-coding-system
5995 (allout-select-safe-coding-system subtree-beg subtree-end))
5996 ;; if the coding system for the text being encrypted is different
5997 ;; than that prevailing, then there a real risk that the coding
5998 ;; system can't be noticed by emacs when the file is visited. to
5999 ;; mitigate that, offer to preserve the coding system using a file
6000 ;; local variable.
6001 (if (and (not (equal buffer-file-coding-system
6002 was-coding-system))
6003 (yes-or-no-p
6004 (format (concat "Register coding system %s as file local"
6005 " var? Necessary when only encrypted text"
6006 " is in that coding system. ")
6007 buffer-file-coding-system)))
6008 (allout-adjust-file-variable "buffer-file-coding-system"
6009 buffer-file-coding-system)))
6011 (setq result-text
6012 (allout-encrypt-string subject-text was-encrypted
6013 (current-buffer) keymode-cue))
6015 ;; Replace the subtree with the processed product.
6016 (allout-unprotected
6017 (progn
6018 (set-buffer allout-buffer)
6019 (delete-region subtree-beg subtree-end)
6020 (insert result-text)
6021 (if was-collapsed
6022 (allout-flag-region (1- subtree-beg) (point) t))
6023 ;; adjust trailing-blank-lines to preserve topic spacing:
6024 (if (not was-encrypted)
6025 (if (and (= subtree-end-char ?\n)
6026 (= subtree-trailing-char ?\n))
6027 (insert subtree-trailing-char)))
6028 ;; Ensure that the item has an encrypted-entry bullet:
6029 (if (not (string= (buffer-substring-no-properties
6030 (1- after-bullet-pos) after-bullet-pos)
6031 allout-topic-encryption-bullet))
6032 (progn (goto-char (1- after-bullet-pos))
6033 (delete-char 1)
6034 (insert allout-topic-encryption-bullet)))
6035 (if was-encrypted
6036 ;; Remove the is-encrypted bullet qualifier:
6037 (progn (goto-char after-bullet-pos)
6038 (delete-char 1))
6039 ;; Add the is-encrypted bullet qualifier:
6040 (goto-char after-bullet-pos)
6041 (insert "*"))))
6043 ;; adjust buffer's auto-save eligibility:
6044 (if was-encrypted
6045 (allout-inhibit-auto-save-info-for-decryption was-buffer-saved-size)
6046 (allout-maybe-resume-auto-save-info-after-encryption))
6048 (run-hook-with-args 'allout-structure-added-functions
6049 bullet-pos subtree-end))))
6050 ;;;_ > allout-encrypt-string (text decrypt allout-buffer keymode-cue
6051 ;;; &optional rejected)
6052 (defun allout-encrypt-string (text decrypt allout-buffer keymode-cue
6053 &optional rejected)
6054 "Encrypt or decrypt message TEXT.
6056 Returns the resulting string, or nil if the transformation fails.
6058 If DECRYPT is true (default false), then decrypt instead of encrypt.
6060 ALLOUT-BUFFER identifies the buffer containing the text.
6062 Entry encryption defaults to symmetric key mode unless keypair
6063 recipients are associated with the file (see
6064 `epa-file-encrypt-to') or the function is invoked with a
6065 \(KEYMODE-CUE) universal argument greater than 1.
6067 When encrypting, KEYMODE-CUE universal argument greater than 1
6068 causes prompting for recipients for public-key keypair
6069 encryption. Selecting no recipients results in symmetric key
6070 encryption.
6072 Further, encrypting with a KEYMODE-CUE universal argument greater
6073 than 4 - eg, preceded by a doubled Ctrl-U - causes association of
6074 the specified recipients with the file, replacing those currently
6075 associated with it. This can be used to dissociate any
6076 recipients with the file, by selecting no recipients in the
6077 dialog.
6079 Optional REJECTED is for internal use, to convey the number of
6080 rejections due to matches against
6081 `allout-encryption-ciphertext-rejection-regexps', as limited by
6082 `allout-encryption-ciphertext-rejection-ceiling'.
6084 NOTE: A few GnuPG v2 versions improperly preserve incorrect
6085 symmetric decryption keys, preventing entry of the correct key on
6086 subsequent decryption attempts until the cache times-out. That
6087 can take several minutes. (Decryption of other entries is not
6088 affected.) Upgrade your EasyPG version, if you can, and you can
6089 deliberately clear your gpg-agent's cache by sending it a '-HUP'
6090 signal."
6092 (require 'epg)
6093 (require 'epa)
6095 (let* ((epg-context (let* ((context (epg-make-context nil t)))
6096 (epg-context-set-passphrase-callback
6097 context #'epa-passphrase-callback-function)
6098 context))
6100 (encoding (with-current-buffer allout-buffer
6101 buffer-file-coding-system))
6102 (multibyte (with-current-buffer allout-buffer
6103 enable-multibyte-characters))
6104 ;; "sanitization" avoids encryption results that are outline structure.
6105 (sani-regexps 'allout-encryption-plaintext-sanitization-regexps)
6106 (strip-plaintext-regexps (if (not decrypt)
6107 (allout-get-configvar-values
6108 sani-regexps)))
6109 (rejection-regexps 'allout-encryption-ciphertext-rejection-regexps)
6110 (reject-ciphertext-regexps (if (not decrypt)
6111 (allout-get-configvar-values
6112 rejection-regexps)))
6113 (rejected (or rejected 0))
6114 (rejections-left (- allout-encryption-ciphertext-rejection-ceiling
6115 rejected))
6116 (keypair-mode (cond (decrypt 'decrypting)
6117 ((<= (prefix-numeric-value keymode-cue) 1)
6118 'default)
6119 ((<= (prefix-numeric-value keymode-cue) 4)
6120 'prompt)
6121 ((> (prefix-numeric-value keymode-cue) 4)
6122 'prompt-save)))
6123 (keypair-message (concat "Select encryption recipients.\n"
6124 "Symmetric encryption is done if no"
6125 " recipients are selected. "))
6126 (encrypt-to (and (boundp 'epa-file-encrypt-to) epa-file-encrypt-to))
6127 recipients
6128 massaged-text
6129 result-text
6132 ;; Massage the subject text for encoding and filtering.
6133 (with-temp-buffer
6134 (insert text)
6135 ;; convey the text characteristics of the original buffer:
6136 (set-buffer-multibyte multibyte)
6137 (when encoding
6138 (set-buffer-file-coding-system encoding)
6139 (if (not decrypt)
6140 (encode-coding-region (point-min) (point-max) encoding)))
6142 ;; remove sanitization regexps matches before encrypting:
6143 (when (and strip-plaintext-regexps (not decrypt))
6144 (dolist (re strip-plaintext-regexps)
6145 (let ((re (if (listp re) (car re) re))
6146 (replacement (if (listp re) (cadr re) "")))
6147 (goto-char (point-min))
6148 (save-match-data
6149 (while (re-search-forward re nil t)
6150 (replace-match replacement nil nil))))))
6151 (setq massaged-text (buffer-substring-no-properties (point-min)
6152 (point-max))))
6153 ;; determine key mode and, if keypair, recipients:
6154 (setq recipients
6155 (case keypair-mode
6157 (decrypting nil)
6159 (default (if encrypt-to (epg-list-keys epg-context encrypt-to)))
6161 ((prompt prompt-save)
6162 (save-window-excursion
6163 (epa-select-keys epg-context keypair-message)))))
6165 (setq result-text
6166 (if decrypt
6167 (condition-case err
6168 (epg-decrypt-string epg-context
6169 (encode-coding-string massaged-text
6170 (or encoding 'utf-8)))
6171 (epg-error
6172 (signal 'egp-error
6173 (cons (concat (cadr err) " - gpg version problem?")
6174 (cddr err)))))
6175 (replace-regexp-in-string "\n$" ""
6176 (epg-encrypt-string epg-context
6177 (encode-coding-string massaged-text
6178 (or encoding 'utf-8))
6179 recipients))))
6181 ;; validate result -- non-empty
6182 (if (not result-text)
6183 (error "%scryption failed." (if decrypt "De" "En")))
6186 (when (eq keypair-mode 'prompt-save)
6187 ;; set epa-file-encrypt-to in the buffer:
6188 (setq epa-file-encrypt-to (mapcar (lambda (key)
6189 (epg-user-id-string
6190 (car (epg-key-user-id-list key))))
6191 recipients))
6192 ;; change the file variable:
6193 (allout-adjust-file-variable "epa-file-encrypt-to" epa-file-encrypt-to))
6195 (cond
6196 ;; Retry (within limit) if ciphertext contains rejections:
6197 ((and (not decrypt)
6198 ;; Check for disqualification of this ciphertext:
6199 (let ((regexps reject-ciphertext-regexps)
6200 reject-it)
6201 (while (and regexps (not reject-it))
6202 (setq reject-it (string-match (car regexps) result-text))
6203 (pop regexps))
6204 reject-it))
6205 (setq rejections-left (1- rejections-left))
6206 (if (<= rejections-left 0)
6207 (error (concat "Ciphertext rejected too many times"
6208 " (%s), per `%s'")
6209 allout-encryption-ciphertext-rejection-ceiling
6210 'allout-encryption-ciphertext-rejection-regexps)
6211 ;; try again (gpg-agent may have the key cached):
6212 (allout-encrypt-string text decrypt allout-buffer keypair-mode
6213 (1+ rejected))))
6215 ;; Barf if encryption yields extraordinary control chars:
6216 ((and (not decrypt)
6217 (string-match "[\C-a\C-k\C-o-\C-z\C-@]"
6218 result-text))
6219 (error (concat "Encryption produced non-armored text, which"
6220 "conflicts with allout mode -- reconfigure!")))
6221 (t result-text))))
6222 ;;;_ > allout-inhibit-auto-save-info-for-decryption
6223 (defun allout-inhibit-auto-save-info-for-decryption (was-buffer-saved-size)
6224 "Temporarily prevent auto-saves in this buffer when an item is decrypted.
6226 WAS-BUFFER-SAVED-SIZE is the value of `buffer-saved-size' *before*
6227 the decryption."
6228 (when (not (or (= buffer-saved-size -1) (= was-buffer-saved-size -1)))
6229 (setq allout-auto-save-temporarily-disabled was-buffer-saved-size
6230 buffer-saved-size -1)))
6231 ;;;_ > allout-maybe-resume-auto-save-info-after-encryption ()
6232 (defun allout-maybe-resume-auto-save-info-after-encryption ()
6233 "Restore auto-save info, *if* there are no topics pending encryption."
6234 (when (and allout-auto-save-temporarily-disabled
6235 (= buffer-saved-size -1)
6236 (save-excursion
6237 (save-restriction
6238 (widen)
6239 (goto-char (point-min))
6240 (not (allout-next-topic-pending-encryption)))))
6241 (setq buffer-saved-size allout-auto-save-temporarily-disabled
6242 allout-auto-save-temporarily-disabled nil)))
6244 ;;;_ > allout-encrypted-topic-p ()
6245 (defun allout-encrypted-topic-p ()
6246 "True if the current topic is encryptable and encrypted."
6247 (save-excursion
6248 (allout-end-of-prefix t)
6249 (and (string= (buffer-substring-no-properties (1- (point)) (point))
6250 allout-topic-encryption-bullet)
6251 (save-match-data (looking-at "\\*")))
6254 ;;;_ > allout-next-topic-pending-encryption ()
6255 (defun allout-next-topic-pending-encryption ()
6256 "Return the point of the next topic pending encryption, or nil if none.
6258 Such a topic has the `allout-topic-encryption-bullet' without an
6259 immediately following '*' that would mark the topic as being encrypted.
6260 It must also have content."
6261 (let (done got content-beg)
6262 (save-match-data
6263 (while (not done)
6265 (if (not (re-search-forward
6266 (format "\\(\\`\\|\n\\)%s *%s[^*]"
6267 (regexp-quote allout-header-prefix)
6268 (regexp-quote allout-topic-encryption-bullet))
6269 nil t))
6270 (setq got nil
6271 done t)
6272 (goto-char (setq got (match-beginning 0)))
6273 (if (save-match-data (looking-at "\n"))
6274 (forward-char 1))
6275 (setq got (point)))
6277 (cond ((not got)
6278 (setq done t))
6280 ((not (search-forward "\n"))
6281 (setq got nil
6282 done t))
6284 ((eobp)
6285 (setq got nil
6286 done t))
6289 (setq content-beg (point))
6290 (backward-char 1)
6291 (allout-end-of-subtree)
6292 (if (<= (point) content-beg)
6293 ;; Continue looking
6294 (setq got nil)
6295 ;; Got it!
6296 (setq done t)))
6299 (if got
6300 (goto-char got))
6304 ;;;_ > allout-encrypt-decrypted ()
6305 (defun allout-encrypt-decrypted ()
6306 "Encrypt topics pending encryption except those containing exemption point.
6308 If a topic that is currently being edited was encrypted, we return a list
6309 containing the location of the topic and the location of the cursor just
6310 before the topic was encrypted. This can be used, eg, to decrypt the topic
6311 and exactly resituate the cursor if this is being done as part of a file
6312 save. See `allout-encrypt-unencrypted-on-saves' for more info."
6314 (interactive "p")
6315 (save-match-data
6316 (save-excursion
6317 (let* ((current-mark (point-marker))
6318 (current-mark-position (marker-position current-mark))
6319 was-modified
6320 bo-subtree
6321 editing-topic editing-point)
6322 (goto-char (point-min))
6323 (while (allout-next-topic-pending-encryption)
6324 (setq was-modified (buffer-modified-p))
6325 (when (save-excursion
6326 (and (boundp 'allout-encrypt-unencrypted-on-saves)
6327 allout-encrypt-unencrypted-on-saves
6328 (setq bo-subtree (re-search-forward "$"))
6329 (not (allout-hidden-p))
6330 (>= current-mark (point))
6331 (allout-end-of-current-subtree)
6332 (<= current-mark (point))))
6333 (setq editing-topic (point)
6334 ;; we had to wait for this 'til now so prior topics are
6335 ;; encrypted, any relevant text shifts are in place:
6336 editing-point (- current-mark-position
6337 (count-trailing-whitespace-region
6338 bo-subtree current-mark-position))))
6339 (allout-toggle-subtree-encryption)
6340 (if (not was-modified)
6341 (set-buffer-modified-p nil))
6343 (if (not was-modified)
6344 (set-buffer-modified-p nil))
6345 (if editing-topic (list editing-topic editing-point))
6351 ;;;_ #9 miscellaneous
6352 ;;;_ : Mode:
6353 ;;;_ > outlineify-sticky ()
6354 ;; outlinify-sticky is correct spelling; provide this alias for sticklers:
6355 ;;;###autoload
6356 (defalias 'outlinify-sticky 'outlineify-sticky)
6357 ;;;###autoload
6358 (defun outlineify-sticky (&optional arg)
6359 "Activate outline mode and establish file var so it is started subsequently.
6361 See `allout-layout' and customization of `allout-auto-activation'
6362 for details on preparing Emacs for automatic allout activation."
6364 (interactive "P")
6366 (if (allout-mode-p) (allout-mode)) ; deactivate so we can re-activate...
6367 (allout-mode)
6369 (save-excursion
6370 (goto-char (point-min))
6371 (if (allout-goto-prefix)
6373 (allout-open-topic 2)
6374 (insert (concat "Dummy outline topic header -- see"
6375 "`allout-mode' docstring: `^Hm'."))
6376 (allout-adjust-file-variable
6377 "allout-layout" (or allout-layout '(-1 : 0))))))
6378 ;;;_ > allout-file-vars-section-data ()
6379 (defun allout-file-vars-section-data ()
6380 "Return data identifying the file-vars section, or nil if none.
6382 Returns a list of the form (BEGINNING-POINT PREFIX-STRING SUFFIX-STRING)."
6383 ;; minimally gleaned from emacs 21.4 files.el hack-local-variables function.
6384 (let (beg prefix suffix)
6385 (save-excursion
6386 (goto-char (point-max))
6387 (search-backward "\n\^L" (max (- (point-max) 3000) (point-min)) 'move)
6388 (if (let ((case-fold-search t))
6389 (not (search-forward "Local Variables:" nil t)))
6391 (setq beg (- (point) 16))
6392 (setq suffix (buffer-substring-no-properties
6393 (point)
6394 (progn (if (search-forward "\n" nil t)
6395 (forward-char -1))
6396 (point))))
6397 (setq prefix (buffer-substring-no-properties
6398 (progn (if (search-backward "\n" nil t)
6399 (forward-char 1))
6400 (point))
6401 beg))
6402 (list beg prefix suffix))
6406 ;;;_ > allout-adjust-file-variable (varname value)
6407 (defun allout-adjust-file-variable (varname value)
6408 "Adjust the setting of an Emacs file variable named VARNAME to VALUE.
6410 This activity is inhibited if either `enable-local-variables' or
6411 `allout-enable-file-variable-adjustment' are nil.
6413 When enabled, an entry for the variable is created if not already present,
6414 or changed if established with a different value. The section for the file
6415 variables, itself, is created if not already present. When created, the
6416 section lines (including the section line) exist as second-level topics in
6417 a top-level topic at the end of the file.
6419 `enable-local-variables' must be true for any of this to happen."
6420 (if (not (and enable-local-variables
6421 allout-enable-file-variable-adjustment))
6423 (save-excursion
6424 (let ((inhibit-field-text-motion t)
6425 (section-data (allout-file-vars-section-data))
6426 beg prefix suffix)
6427 (if section-data
6428 (setq beg (car section-data)
6429 prefix (cadr section-data)
6430 suffix (car (cddr section-data)))
6431 ;; create the section
6432 (goto-char (point-max))
6433 (open-line 1)
6434 (allout-open-topic 0)
6435 (end-of-line)
6436 (insert "Local emacs vars.\n")
6437 (allout-open-topic 1)
6438 (setq beg (point)
6439 suffix ""
6440 prefix (buffer-substring-no-properties (progn
6441 (beginning-of-line)
6442 (point))
6443 beg))
6444 (goto-char beg)
6445 (insert "Local variables:\n")
6446 (allout-open-topic 0)
6447 (insert "End:\n")
6449 ;; look for existing entry or create one, leaving point for insertion
6450 ;; of new value:
6451 (goto-char beg)
6452 (allout-show-to-offshoot)
6453 (if (search-forward (concat "\n" prefix varname ":") nil t)
6454 (let* ((value-beg (point))
6455 (line-end (progn (if (search-forward "\n" nil t)
6456 (forward-char -1))
6457 (point)))
6458 (value-end (- line-end (length suffix))))
6459 (if (> value-end value-beg)
6460 (delete-region value-beg value-end)))
6461 (end-of-line)
6462 (open-line 1)
6463 (forward-line 1)
6464 (insert (concat prefix varname ":")))
6465 (insert (format " %S%s" value suffix))
6470 ;;;_ > allout-get-configvar-values (varname)
6471 (defun allout-get-configvar-values (configvar-name)
6472 "Return a list of values of the symbols in list bound to CONFIGVAR-NAME.
6474 The user is prompted for removal of symbols that are unbound, and they
6475 otherwise are ignored.
6477 CONFIGVAR-NAME should be the name of the configuration variable,
6478 not its value."
6480 (let ((configvar-value (symbol-value configvar-name))
6481 got)
6482 (dolist (sym configvar-value)
6483 (if (not (boundp sym))
6484 (if (yes-or-no-p (format "%s entry `%s' is unbound -- remove it? "
6485 configvar-name sym))
6486 (delq sym (symbol-value configvar-name)))
6487 (push (symbol-value sym) got)))
6488 (reverse got)))
6489 ;;;_ : Topics:
6490 ;;;_ > allout-mark-topic ()
6491 (defun allout-mark-topic ()
6492 "Put the region around topic currently containing point."
6493 (interactive)
6494 (let ((inhibit-field-text-motion t))
6495 (beginning-of-line))
6496 (allout-goto-prefix-doublechecked)
6497 (push-mark (point))
6498 (allout-end-of-current-subtree)
6499 (exchange-point-and-mark))
6500 ;;;_ : UI:
6501 ;;;_ > solicit-char-in-string (prompt string &optional do-defaulting)
6502 (defun solicit-char-in-string (prompt string &optional do-defaulting)
6503 "Solicit (with first arg PROMPT) choice of a character from string STRING.
6505 Optional arg DO-DEFAULTING indicates to accept empty input (CR)."
6507 (let ((new-prompt prompt)
6508 got)
6510 (while (not got)
6511 (message "%s" new-prompt)
6513 ;; We do our own reading here, so we can circumvent, eg, special
6514 ;; treatment for `?' character. (Oughta use minibuffer keymap instead.)
6515 (setq got
6516 (char-to-string (let ((cursor-in-echo-area nil)) (read-char))))
6518 (setq got
6519 (cond ((string-match (regexp-quote got) string) got)
6520 ((and do-defaulting (string= got "\r"))
6521 ;; Return empty string to default:
6523 ((string= got "\C-g") (signal 'quit nil))
6525 (setq new-prompt (concat prompt
6527 " ...pick from: "
6528 string
6529 ""))
6530 nil))))
6531 ;; got something out of loop -- return it:
6532 got)
6534 ;;;_ : Strings:
6535 ;;;_ > regexp-sans-escapes (string)
6536 (defun regexp-sans-escapes (regexp &optional successive-backslashes)
6537 "Return a copy of REGEXP with all character escapes stripped out.
6539 Representations of actual backslashes -- '\\\\\\\\' -- are left as a
6540 single backslash.
6542 Optional arg SUCCESSIVE-BACKSLASHES is used internally for recursion."
6544 (if (string= regexp "")
6546 ;; Set successive-backslashes to number if current char is
6547 ;; backslash, or else to nil:
6548 (setq successive-backslashes
6549 (if (= (aref regexp 0) ?\\)
6550 (if successive-backslashes (1+ successive-backslashes) 1)
6551 nil))
6552 (if (or (not successive-backslashes) (= 2 successive-backslashes))
6553 ;; Include first char:
6554 (concat (substring regexp 0 1)
6555 (regexp-sans-escapes (substring regexp 1)))
6556 ;; Exclude first char, but maintain count:
6557 (regexp-sans-escapes (substring regexp 1) successive-backslashes))))
6558 ;;;_ > count-trailing-whitespace-region (beg end)
6559 (defun count-trailing-whitespace-region (beg end)
6560 "Return number of trailing whitespace chars between BEG and END.
6562 If BEG is bigger than END we return 0."
6563 (if (> beg end)
6565 (save-match-data
6566 (save-excursion
6567 (goto-char beg)
6568 (let ((count 0))
6569 (while (re-search-forward "[ ][ ]*$" end t)
6570 (goto-char (1+ (match-beginning 2)))
6571 (setq count (1+ count)))
6572 count)))))
6573 ;;;_ > allout-format-quote (string)
6574 (defun allout-format-quote (string)
6575 "Return a copy of string with all \"%\" characters doubled."
6576 (apply 'concat
6577 (mapcar (lambda (char) (if (= char ?%) "%%" (char-to-string char)))
6578 string)))
6579 ;;;_ : lists
6580 ;;;_ > allout-flatten (list)
6581 (defun allout-flatten (list)
6582 "Return a list of all atoms in list."
6583 ;; classic.
6584 (cond ((null list) nil)
6585 ((atom (car list)) (cons (car list) (allout-flatten (cdr list))))
6586 (t (append (allout-flatten (car list)) (allout-flatten (cdr list))))))
6587 ;;;_ : Compatibility:
6588 ;;;_ : xemacs undo-in-progress provision:
6589 (unless (boundp 'undo-in-progress)
6590 (defvar undo-in-progress nil
6591 "Placeholder defvar for XEmacs compatibility from allout.el.")
6592 (defadvice undo-more (around allout activate)
6593 ;; This defadvice used only in emacs that lack undo-in-progress, eg xemacs.
6594 (let ((undo-in-progress t)) ad-do-it)))
6596 ;;;_ > allout-mark-marker to accommodate divergent emacsen:
6597 (defun allout-mark-marker (&optional force buffer)
6598 "Accommodate the different signature for `mark-marker' across Emacsen.
6600 XEmacs takes two optional args, while Emacs does not,
6601 so pass them along when appropriate."
6602 (if (featurep 'xemacs)
6603 (apply 'mark-marker force buffer)
6604 (mark-marker)))
6605 ;;;_ > subst-char-in-string if necessary
6606 (if (not (fboundp 'subst-char-in-string))
6607 (defun subst-char-in-string (fromchar tochar string &optional inplace)
6608 "Replace FROMCHAR with TOCHAR in STRING each time it occurs.
6609 Unless optional argument INPLACE is non-nil, return a new string."
6610 (let ((i (length string))
6611 (newstr (if inplace string (copy-sequence string))))
6612 (while (> i 0)
6613 (setq i (1- i))
6614 (if (eq (aref newstr i) fromchar)
6615 (aset newstr i tochar)))
6616 newstr)))
6617 ;;;_ > wholenump if necessary
6618 (if (not (fboundp 'wholenump))
6619 (defalias 'wholenump 'natnump))
6620 ;;;_ > remove-overlays if necessary
6621 (if (not (fboundp 'remove-overlays))
6622 (defun remove-overlays (&optional beg end name val)
6623 "Clear BEG and END of overlays whose property NAME has value VAL.
6624 Overlays might be moved and/or split.
6625 BEG and END default respectively to the beginning and end of buffer."
6626 (unless beg (setq beg (point-min)))
6627 (unless end (setq end (point-max)))
6628 (if (< end beg)
6629 (setq beg (prog1 end (setq end beg))))
6630 (save-excursion
6631 (dolist (o (overlays-in beg end))
6632 (when (eq (overlay-get o name) val)
6633 ;; Either push this overlay outside beg...end
6634 ;; or split it to exclude beg...end
6635 ;; or delete it entirely (if it is contained in beg...end).
6636 (if (< (overlay-start o) beg)
6637 (if (> (overlay-end o) end)
6638 (progn
6639 (move-overlay (copy-overlay o)
6640 (overlay-start o) beg)
6641 (move-overlay o end (overlay-end o)))
6642 (move-overlay o (overlay-start o) beg))
6643 (if (> (overlay-end o) end)
6644 (move-overlay o end (overlay-end o))
6645 (delete-overlay o)))))))
6647 ;;;_ > copy-overlay if necessary -- xemacs ~ 21.4
6648 (if (not (fboundp 'copy-overlay))
6649 (defun copy-overlay (o)
6650 "Return a copy of overlay O."
6651 (let ((o1 (make-overlay (overlay-start o) (overlay-end o)
6652 ;; FIXME: there's no easy way to find the
6653 ;; insertion-type of the two markers.
6654 (overlay-buffer o)))
6655 (props (overlay-properties o)))
6656 (while props
6657 (overlay-put o1 (pop props) (pop props)))
6658 o1)))
6659 ;;;_ > add-to-invisibility-spec if necessary -- xemacs ~ 21.4
6660 (if (not (fboundp 'add-to-invisibility-spec))
6661 (defun add-to-invisibility-spec (element)
6662 "Add ELEMENT to `buffer-invisibility-spec'.
6663 See documentation for `buffer-invisibility-spec' for the kind of elements
6664 that can be added."
6665 (if (eq buffer-invisibility-spec t)
6666 (setq buffer-invisibility-spec (list t)))
6667 (setq buffer-invisibility-spec
6668 (cons element buffer-invisibility-spec))))
6669 ;;;_ > remove-from-invisibility-spec if necessary -- xemacs ~ 21.4
6670 (if (not (fboundp 'remove-from-invisibility-spec))
6671 (defun remove-from-invisibility-spec (element)
6672 "Remove ELEMENT from `buffer-invisibility-spec'."
6673 (if (consp buffer-invisibility-spec)
6674 (setq buffer-invisibility-spec (delete element
6675 buffer-invisibility-spec)))))
6676 ;;;_ > move-beginning-of-line if necessary -- older emacs, xemacs
6677 (if (not (fboundp 'move-beginning-of-line))
6678 (defun move-beginning-of-line (arg)
6679 "Move point to beginning of current line as displayed.
6680 \(This disregards invisible newlines such as those
6681 which are part of the text that an image rests on.)
6683 With argument ARG not nil or 1, move forward ARG - 1 lines first.
6684 If point reaches the beginning or end of buffer, it stops there.
6685 To ignore intangibility, bind `inhibit-point-motion-hooks' to t."
6686 (interactive "p")
6687 (or arg (setq arg 1))
6688 (if (/= arg 1)
6689 (condition-case nil (line-move (1- arg)) (error nil)))
6691 ;; Move to beginning-of-line, ignoring fields and invisible text.
6692 (skip-chars-backward "^\n")
6693 (while (and (not (bobp))
6694 (let ((prop
6695 (get-char-property (1- (point)) 'invisible)))
6696 (if (eq buffer-invisibility-spec t)
6697 prop
6698 (or (memq prop buffer-invisibility-spec)
6699 (assq prop buffer-invisibility-spec)))))
6700 (goto-char (if (featurep 'xemacs)
6701 (previous-property-change (point))
6702 (previous-char-property-change (point))))
6703 (skip-chars-backward "^\n"))
6704 (vertical-motion 0))
6706 ;;;_ > move-end-of-line if necessary -- Emacs < 22.1, xemacs
6707 (if (not (fboundp 'move-end-of-line))
6708 (defun move-end-of-line (arg)
6709 "Move point to end of current line as displayed.
6710 \(This disregards invisible newlines such as those
6711 which are part of the text that an image rests on.)
6713 With argument ARG not nil or 1, move forward ARG - 1 lines first.
6714 If point reaches the beginning or end of buffer, it stops there.
6715 To ignore intangibility, bind `inhibit-point-motion-hooks' to t."
6716 (interactive "p")
6717 (or arg (setq arg 1))
6718 (let (done)
6719 (while (not done)
6720 (let ((newpos
6721 (save-excursion
6722 (let ((goal-column 0))
6723 (and (condition-case nil
6724 (or (line-move arg) t)
6725 (error nil))
6726 (not (bobp))
6727 (progn
6728 (while
6729 (and
6730 (not (bobp))
6731 (let ((prop
6732 (get-char-property (1- (point))
6733 'invisible)))
6734 (if (eq buffer-invisibility-spec t)
6735 prop
6736 (or (memq prop
6737 buffer-invisibility-spec)
6738 (assq prop
6739 buffer-invisibility-spec)))))
6740 (goto-char
6741 (previous-char-property-change (point))))
6742 (backward-char 1)))
6743 (point)))))
6744 (goto-char newpos)
6745 (if (and (> (point) newpos)
6746 (eq (preceding-char) ?\n))
6747 (backward-char 1)
6748 (if (and (> (point) newpos) (not (eobp))
6749 (not (eq (following-char) ?\n)))
6750 ;; If we skipped something intangible
6751 ;; and now we're not really at eol,
6752 ;; keep going.
6753 (setq arg 1)
6754 (setq done t)))))))
6756 ;;;_ > allout-next-single-char-property-change -- alias unless lacking
6757 (defalias 'allout-next-single-char-property-change
6758 (if (fboundp 'next-single-char-property-change)
6759 'next-single-char-property-change
6760 'next-single-property-change)
6761 ;; No docstring because xemacs defalias doesn't support it.
6763 ;;;_ > allout-previous-single-char-property-change -- alias unless lacking
6764 (defalias 'allout-previous-single-char-property-change
6765 (if (fboundp 'previous-single-char-property-change)
6766 'previous-single-char-property-change
6767 'previous-single-property-change)
6768 ;; No docstring because xemacs defalias doesn't support it.
6770 ;;;_ > allout-select-safe-coding-system
6771 (defalias 'allout-select-safe-coding-system
6772 (if (fboundp 'select-safe-coding-system)
6773 'select-safe-coding-system
6774 'detect-coding-region)
6776 ;;;_ > allout-substring-no-properties
6777 ;; define as alias first, so byte compiler is happy.
6778 (defalias 'allout-substring-no-properties 'substring-no-properties)
6779 ;; then supplant with definition if underlying alias absent.
6780 (if (not (fboundp 'substring-no-properties))
6781 (defun allout-substring-no-properties (string &optional start end)
6782 (substring string (or start 0) end))
6785 ;;;_ #10 Unfinished
6786 ;;;_ > allout-bullet-isearch (&optional bullet)
6787 (defun allout-bullet-isearch (&optional bullet)
6788 "Isearch (regexp) for topic with bullet BULLET."
6789 (interactive)
6790 (if (not bullet)
6791 (setq bullet (solicit-char-in-string
6792 "ISearch for topic with bullet: "
6793 (regexp-sans-escapes allout-bullets-string))))
6795 (let ((isearch-regexp t)
6796 (isearch-string (concat "^"
6797 allout-header-prefix
6798 "[ \t]*"
6799 bullet)))
6800 (isearch-repeat 'forward)
6801 (isearch-mode t)))
6803 ;;;_ #11 Unit tests -- this should be last item before "Provide"
6804 ;;;_ > allout-run-unit-tests ()
6805 (defun allout-run-unit-tests ()
6806 "Run the various allout unit tests."
6807 (message "Running allout tests...")
6808 (allout-test-resumptions)
6809 (message "Running allout tests... Done.")
6810 (sit-for .5))
6811 ;;;_ : test resumptions:
6812 ;;;_ > allout-tests-obliterate-variable (name)
6813 (defun allout-tests-obliterate-variable (name)
6814 "Completely unbind variable with NAME."
6815 (if (local-variable-p name (current-buffer)) (kill-local-variable name))
6816 (while (boundp name) (makunbound name)))
6817 ;;;_ > allout-test-resumptions ()
6818 (defvar allout-tests-globally-unbound nil
6819 "Fodder for allout resumptions tests -- defvar just for byte compiler.")
6820 (defvar allout-tests-globally-true nil
6821 "Fodder for allout resumptions tests -- defvar just for byte compiler.")
6822 (defvar allout-tests-locally-true nil
6823 "Fodder for allout resumptions tests -- defvar just for byte compiler.")
6824 (defun allout-test-resumptions ()
6825 "Exercise allout resumptions."
6826 ;; for each resumption case, we also test that the right local/global
6827 ;; scopes are affected during resumption effects:
6829 ;; ensure that previously unbound variables return to the unbound state.
6830 (with-temp-buffer
6831 (allout-tests-obliterate-variable 'allout-tests-globally-unbound)
6832 (allout-add-resumptions '(allout-tests-globally-unbound t))
6833 (assert (not (default-boundp 'allout-tests-globally-unbound)))
6834 (assert (local-variable-p 'allout-tests-globally-unbound (current-buffer)))
6835 (assert (boundp 'allout-tests-globally-unbound))
6836 (assert (equal allout-tests-globally-unbound t))
6837 (allout-do-resumptions)
6838 (assert (not (local-variable-p 'allout-tests-globally-unbound
6839 (current-buffer))))
6840 (assert (not (boundp 'allout-tests-globally-unbound))))
6842 ;; ensure that variable with prior global value is resumed
6843 (with-temp-buffer
6844 (allout-tests-obliterate-variable 'allout-tests-globally-true)
6845 (setq allout-tests-globally-true t)
6846 (allout-add-resumptions '(allout-tests-globally-true nil))
6847 (assert (equal (default-value 'allout-tests-globally-true) t))
6848 (assert (local-variable-p 'allout-tests-globally-true (current-buffer)))
6849 (assert (equal allout-tests-globally-true nil))
6850 (allout-do-resumptions)
6851 (assert (not (local-variable-p 'allout-tests-globally-true
6852 (current-buffer))))
6853 (assert (boundp 'allout-tests-globally-true))
6854 (assert (equal allout-tests-globally-true t)))
6856 ;; ensure that prior local value is resumed
6857 (with-temp-buffer
6858 (allout-tests-obliterate-variable 'allout-tests-locally-true)
6859 (set (make-local-variable 'allout-tests-locally-true) t)
6860 (assert (not (default-boundp 'allout-tests-locally-true))
6861 nil (concat "Test setup mistake -- variable supposed to"
6862 " not have global binding, but it does."))
6863 (assert (local-variable-p 'allout-tests-locally-true (current-buffer))
6864 nil (concat "Test setup mistake -- variable supposed to have"
6865 " local binding, but it lacks one."))
6866 (allout-add-resumptions '(allout-tests-locally-true nil))
6867 (assert (not (default-boundp 'allout-tests-locally-true)))
6868 (assert (local-variable-p 'allout-tests-locally-true (current-buffer)))
6869 (assert (equal allout-tests-locally-true nil))
6870 (allout-do-resumptions)
6871 (assert (boundp 'allout-tests-locally-true))
6872 (assert (local-variable-p 'allout-tests-locally-true (current-buffer)))
6873 (assert (equal allout-tests-locally-true t))
6874 (assert (not (default-boundp 'allout-tests-locally-true))))
6876 ;; ensure that last of multiple resumptions holds, for various scopes.
6877 (with-temp-buffer
6878 (allout-tests-obliterate-variable 'allout-tests-globally-unbound)
6879 (allout-tests-obliterate-variable 'allout-tests-globally-true)
6880 (setq allout-tests-globally-true t)
6881 (allout-tests-obliterate-variable 'allout-tests-locally-true)
6882 (set (make-local-variable 'allout-tests-locally-true) t)
6883 (allout-add-resumptions '(allout-tests-globally-unbound t)
6884 '(allout-tests-globally-true nil)
6885 '(allout-tests-locally-true nil))
6886 (allout-add-resumptions '(allout-tests-globally-unbound 2)
6887 '(allout-tests-globally-true 3)
6888 '(allout-tests-locally-true 4))
6889 ;; reestablish many of the basic conditions are maintained after re-add:
6890 (assert (not (default-boundp 'allout-tests-globally-unbound)))
6891 (assert (local-variable-p 'allout-tests-globally-unbound (current-buffer)))
6892 (assert (equal allout-tests-globally-unbound 2))
6893 (assert (default-boundp 'allout-tests-globally-true))
6894 (assert (local-variable-p 'allout-tests-globally-true (current-buffer)))
6895 (assert (equal allout-tests-globally-true 3))
6896 (assert (not (default-boundp 'allout-tests-locally-true)))
6897 (assert (local-variable-p 'allout-tests-locally-true (current-buffer)))
6898 (assert (equal allout-tests-locally-true 4))
6899 (allout-do-resumptions)
6900 (assert (not (local-variable-p 'allout-tests-globally-unbound
6901 (current-buffer))))
6902 (assert (not (boundp 'allout-tests-globally-unbound)))
6903 (assert (not (local-variable-p 'allout-tests-globally-true
6904 (current-buffer))))
6905 (assert (boundp 'allout-tests-globally-true))
6906 (assert (equal allout-tests-globally-true t))
6907 (assert (boundp 'allout-tests-locally-true))
6908 (assert (local-variable-p 'allout-tests-locally-true (current-buffer)))
6909 (assert (equal allout-tests-locally-true t))
6910 (assert (not (default-boundp 'allout-tests-locally-true))))
6912 ;; ensure that deliberately unbinding registered variables doesn't foul things
6913 (with-temp-buffer
6914 (allout-tests-obliterate-variable 'allout-tests-globally-unbound)
6915 (allout-tests-obliterate-variable 'allout-tests-globally-true)
6916 (setq allout-tests-globally-true t)
6917 (allout-tests-obliterate-variable 'allout-tests-locally-true)
6918 (set (make-local-variable 'allout-tests-locally-true) t)
6919 (allout-add-resumptions '(allout-tests-globally-unbound t)
6920 '(allout-tests-globally-true nil)
6921 '(allout-tests-locally-true nil))
6922 (allout-tests-obliterate-variable 'allout-tests-globally-unbound)
6923 (allout-tests-obliterate-variable 'allout-tests-globally-true)
6924 (allout-tests-obliterate-variable 'allout-tests-locally-true)
6925 (allout-do-resumptions))
6927 ;;;_ % Run unit tests if `allout-run-unit-tests-after-load' is true:
6928 (when allout-run-unit-tests-on-load
6929 (allout-run-unit-tests))
6931 ;;;_ #12 Provide
6932 (provide 'allout)
6934 ;;;_* Local emacs vars.
6935 ;; The following `allout-layout' local variable setting:
6936 ;; - closes all topics from the first topic to just before the third-to-last,
6937 ;; - shows the children of the third to last (config vars)
6938 ;; - and the second to last (code section),
6939 ;; - and closes the last topic (this local-variables section).
6940 ;;Local variables:
6941 ;;allout-layout: (0 : -1 -1 0)
6942 ;;End:
6944 ;;; allout.el ends here