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