Make sure x_default_search_path is always set even
[emacs.git] / lisp / allout.el
blobb38d38d9e87af64637e1b2af58123e2c84173be9
1 ;;; allout.el --- extensive outline mode for use alone and with other modes
3 ;; Copyright (C) 1992, 1993, 1994, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006 Free Software Foundation, Inc.
6 ;; Author: Ken Manheimer <ken dot manheimer at gmail dot com>
7 ;; Maintainer: Ken Manheimer <ken dot manheimer at gmail dot com>
8 ;; Created: Dec 1991 - first release to usenet
9 ;; Version: 2.2.1
10 ;; Keywords: outlines wp languages
11 ;; Website: http://myriadicity.net/Sundry/EmacsAllout
13 ;; This file is part of GNU Emacs.
15 ;; GNU Emacs is free software; you can redistribute it and/or modify
16 ;; it under the terms of the GNU General Public License as published by
17 ;; the Free Software Foundation; either version 2, or (at your option)
18 ;; any later version.
20 ;; GNU Emacs is distributed in the hope that it will be useful,
21 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
22 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 ;; GNU General Public License for more details.
25 ;; You should have received a copy of the GNU General Public License
26 ;; along with GNU Emacs; see the file COPYING. If not, write to the
27 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
28 ;; Boston, MA 02110-1301, USA.
30 ;;; Commentary:
32 ;; Allout outline minor mode provides extensive outline formatting and
33 ;; and manipulation beyond standard emacs outline mode. Some features:
35 ;; - Classic outline-mode topic-oriented navigation and exposure adjustment
36 ;; - Topic-oriented editing including coherent topic and subtopic
37 ;; creation, promotion, demotion, cut/paste across depths, etc.
38 ;; - Incremental search with dynamic exposure and reconcealment of text
39 ;; - Customizable bullet format - enables programming-language specific
40 ;; outlining, for code-folding editing. (Allout code itself is to try it;
41 ;; formatted as an outline - do ESC-x eval-buffer in allout.el; but
42 ;; emacs local file variables need to be enabled when the
43 ;; file was visited - see `enable-local-variables'.)
44 ;; - Configurable per-file initial exposure settings
45 ;; - Symmetric-key and key-pair topic encryption, plus symmetric passphrase
46 ;; mnemonic support, with verification against an established passphrase
47 ;; (using a stashed encrypted dummy string) and user-supplied hint
48 ;; maintenance. (See allout-toggle-current-subtree-encryption docstring.)
49 ;; - Automatic topic-number maintenance
50 ;; - "Hot-spot" operation, for single-keystroke maneuvering and
51 ;; exposure control (see the allout-mode docstring)
52 ;; - Easy rendering of exposed portions into numbered, latex, indented, etc
53 ;; outline styles
54 ;; - Careful attention to whitespace - enabling blank lines between items
55 ;; and maintenance of hanging indentation (in paragraph auto-fill and
56 ;; across topic promotion and demotion) of topic bodies consistent with
57 ;; indentation of their topic header.
59 ;; and more.
61 ;; See the `allout-mode' function's docstring for an introduction to the
62 ;; mode.
64 ;; The latest development version and helpful notes are available at
65 ;; http://myriadicity.net/Sundry/EmacsAllout .
67 ;; The outline menubar additions provide quick reference to many of
68 ;; the features, and see the docstring of the variable `allout-init'
69 ;; for instructions on priming your emacs session for automatic
70 ;; activation of allout-mode.
72 ;; See the docstring of the variables `allout-layout' and
73 ;; `allout-auto-activation' for details on automatic activation of
74 ;; `allout-mode' as a minor mode. (It has changed since allout
75 ;; 3.x, for those of you that depend on the old method.)
77 ;; Note - the lines beginning with `;;;_' are outline topic headers.
78 ;; Just `ESC-x eval-buffer' to give it a whirl.
80 ;; ken manheimer (ken dot manheimer at gmail dot com)
82 ;;; Code:
84 ;;;_* Dependency autoloads
85 (require 'overlay)
86 (eval-when-compile
87 ;; Most of the requires here are for stuff covered by autoloads.
88 ;; Since just byte-compiling doesn't trigger autoloads, so that
89 ;; "function not found" warnings would occur without these requires.
90 (progn
91 (require 'pgg)
92 (require 'pgg-gpg)
93 (require 'overlay)
94 ;; `cl' is required for `assert'. `assert' is not covered by a standard
95 ;; autoload, but it is a macro, so that eval-when-compile is sufficient
96 ;; to byte-compile it in, or to do the require when the buffer evalled.
97 (require 'cl)
100 ;;;_* USER CUSTOMIZATION VARIABLES:
102 ;;;_ > defgroup allout
103 (defgroup allout nil
104 "Extensive outline mode for use alone and with other modes."
105 :prefix "allout-"
106 :group 'outlines)
108 ;;;_ + Layout, Mode, and Topic Header Configuration
110 ;;;_ = allout-auto-activation
111 (defcustom allout-auto-activation nil
112 "*Regulates auto-activation modality of allout outlines - see `allout-init'.
114 Setq-default by `allout-init' to regulate whether or not allout
115 outline mode is automatically activated when the buffer-specific
116 variable `allout-layout' is non-nil, and whether or not the layout
117 dictated by `allout-layout' should be imposed on mode activation.
119 With value t, auto-mode-activation and auto-layout are enabled.
120 \(This also depends on `allout-find-file-hook' being installed in
121 `find-file-hook', which is also done by `allout-init'.)
123 With value `ask', auto-mode-activation is enabled, and endorsement for
124 performing auto-layout is asked of the user each time.
126 With value `activate', only auto-mode-activation is enabled,
127 auto-layout is not.
129 With value nil, neither auto-mode-activation nor auto-layout are
130 enabled.
132 See the docstring for `allout-init' for the proper interface to
133 this variable."
134 :type '(choice (const :tag "On" t)
135 (const :tag "Ask about layout" "ask")
136 (const :tag "Mode only" "activate")
137 (const :tag "Off" nil))
138 :group 'allout)
139 ;;;_ = allout-default-layout
140 (defcustom allout-default-layout '(-2 : 0)
141 "*Default allout outline layout specification.
143 This setting specifies the outline exposure to use when
144 `allout-layout' has the local value `t'. This docstring describes the
145 layout specifications.
147 A list value specifies a default layout for the current buffer,
148 to be applied upon activation of `allout-mode'. Any non-nil
149 value will automatically trigger `allout-mode', provided
150 `allout-init' has been called to enable this behavior.
152 The types of elements in the layout specification are:
154 integer - dictate the relative depth to open the corresponding topic(s),
155 where:
156 - negative numbers force the topic to be closed before opening
157 to the absolute value of the number, so all siblings are open
158 only to that level.
159 - positive numbers open to the relative depth indicated by the
160 number, but do not force already opened subtopics to be closed.
161 - 0 means to close topic - hide all subitems.
162 : - repeat spec - apply the preceeding element to all siblings at
163 current level, *up to* those siblings that would be covered by specs
164 following the `:' on the list. Ie, apply to all topics at level but
165 trailing ones accounted for by trailing specs. \(Only the first of
166 multiple colons at the same level is honored - later ones are ignored.)
167 * - completely exposes the topic, including bodies
168 + - exposes all subtopics, but not the bodies
169 - - exposes the body of the corresponding topic, but not subtopics
170 list - a nested layout spec, to be applied intricately to its
171 corresponding item(s)
173 Examples:
174 '(-2 : 0)
175 Collapse the top-level topics to show their children and
176 grandchildren, but completely collapse the final top-level topic.
177 '(-1 () : 1 0)
178 Close the first topic so only the immediate subtopics are shown,
179 leave the subsequent topics exposed as they are until the second
180 second to last topic, which is exposed at least one level, and
181 completely close the last topic.
182 '(-2 : -1 *)
183 Expose children and grandchildren of all topics at current
184 level except the last two; expose children of the second to
185 last and completely expose the last one, including its subtopics.
187 See `allout-expose-topic' for more about the exposure process.
189 Also, allout's mode-specific provisions will make topic prefixes default
190 to the comment-start string, if any, of the language of the file. This
191 is modulo the setting of `allout-use-mode-specific-leader', which see."
192 :type 'allout-layout-type
193 :group 'allout)
194 ;;;_ : allout-layout-type
195 (define-widget 'allout-layout-type 'lazy
196 "Allout layout format customization basic building blocks."
197 :type '(repeat
198 (choice (integer :tag "integer (<= zero is strict)")
199 (const :tag ": (repeat prior)" :)
200 (const :tag "* (completely expose)" *)
201 (const :tag "+ (expose all offspring, headlines only)" +)
202 (const :tag "- (expose topic body but not offspring)" -)
203 (allout-layout-type :tag "<Nested layout>"))))
205 ;;;_ = allout-show-bodies
206 (defcustom allout-show-bodies nil
207 "*If non-nil, show entire body when exposing a topic, rather than
208 just the header."
209 :type 'boolean
210 :group 'allout)
211 (make-variable-buffer-local 'allout-show-bodies)
212 ;;;###autoload
213 (put 'allout-show-bodies 'safe-local-variable
214 (if (fboundp 'booleanp) 'booleanp '(lambda (x) (member x '(t nil)))))
216 ;;;_ = allout-beginning-of-line-cycles
217 (defcustom allout-beginning-of-line-cycles t
218 "*If non-nil, \\[allout-beginning-of-line] will cycle through smart-placement options.
220 Cycling only happens on when the command is repeated, not when it
221 follows a different command.
223 Smart-placement means that repeated calls to this function will
224 advance as follows:
226 - if the cursor is on a non-headline body line and not on the first column:
227 then it goes to the first column
228 - if the cursor is on the first column of a non-headline body line:
229 then it goes to the start of the headline within the item body
230 - if the cursor is on the headline and not the start of the headline:
231 then it goes to the start of the headline
232 - if the cursor is on the start of the headline:
233 then it goes to the bullet character \(for hotspot navigation\)
234 - if the cursor is on the bullet character:
235 then it goes to the first column of that line \(the headline\)
236 - if the cursor is on the first column of the headline:
237 then it goes to the start of the headline within the item body.
239 In this fashion, you can use the beginning-of-line command to do
240 its normal job and then, when repeated, advance through the
241 entry, cycling back to start.
243 If this configuration variable is nil, then the cursor is just
244 advanced to the beginning of the line and remains there on
245 repeated calls."
246 :type 'boolean :group 'allout)
247 ;;;_ = allout-end-of-line-cycles
248 (defcustom allout-end-of-line-cycles t
249 "*If non-nil, \\[allout-end-of-line] will cycle through smart-placement options.
251 Cycling only happens on when the command is repeated, not when it
252 follows a different command.
254 Smart-placement means that repeated calls to this function will
255 advance as follows:
257 - if the cursor is not on the end-of-line,
258 then it goes to the end-of-line
259 - if the cursor is on the end-of-line but not the end-of-entry,
260 then it goes to the end-of-entry, exposing it if necessary
261 - if the cursor is on the end-of-entry,
262 then it goes to the end of the head line
264 In this fashion, you can use the end-of-line command to do its
265 normal job and then, when repeated, advance through the entry,
266 cycling back to start.
268 If this configuration variable is nil, then the cursor is just
269 advanced to the end of the line and remains there on repeated
270 calls."
271 :type 'boolean :group 'allout)
273 ;;;_ = allout-header-prefix
274 (defcustom allout-header-prefix "."
275 ;; this string is treated as literal match. it will be `regexp-quote'd, so
276 ;; one cannot use regular expressions to match varying header prefixes.
277 "*Leading string which helps distinguish topic headers.
279 Outline topic header lines are identified by a leading topic
280 header prefix, which mostly have the value of this var at their front.
281 Level 1 topics are exceptions. They consist of only a single
282 character, which is typically set to the `allout-primary-bullet'."
283 :type 'string
284 :group 'allout)
285 (make-variable-buffer-local 'allout-header-prefix)
286 ;;;###autoload
287 (put 'allout-header-prefix 'safe-local-variable 'stringp)
288 ;;;_ = allout-primary-bullet
289 (defcustom allout-primary-bullet "*"
290 "Bullet used for top-level outline topics.
292 Outline topic header lines are identified by a leading topic header
293 prefix, which is concluded by bullets that includes the value of this
294 var and the respective allout-*-bullets-string vars.
296 The value of an asterisk (`*') provides for backwards compatibility
297 with the original Emacs outline mode. See `allout-plain-bullets-string'
298 and `allout-distinctive-bullets-string' for the range of available
299 bullets."
300 :type 'string
301 :group 'allout)
302 (make-variable-buffer-local 'allout-primary-bullet)
303 ;;;###autoload
304 (put 'allout-primary-bullet 'safe-local-variable 'stringp)
305 ;;;_ = allout-plain-bullets-string
306 (defcustom allout-plain-bullets-string ".,"
307 "*The bullets normally used in outline topic prefixes.
309 See `allout-distinctive-bullets-string' for the other kind of
310 bullets.
312 DO NOT include the close-square-bracket, `]', as a bullet.
314 Outline mode has to be reactivated in order for changes to the value
315 of this var to take effect."
316 :type 'string
317 :group 'allout)
318 (make-variable-buffer-local 'allout-plain-bullets-string)
319 ;;;###autoload
320 (put 'allout-plain-bullets-string 'safe-local-variable 'stringp)
321 ;;;_ = allout-distinctive-bullets-string
322 (defcustom allout-distinctive-bullets-string "*+-=>()[{}&!?#%\"X@$~_\\:;^"
323 "*Persistent outline header bullets used to distinguish special topics.
325 These bullets are used to distinguish topics from the run-of-the-mill
326 ones. They are not used in the standard topic headers created by
327 the topic-opening, shifting, and rebulleting \(eg, on topic shift,
328 topic paste, blanket rebulleting) routines, but are offered among the
329 choices for rebulleting. They are not altered by the above automatic
330 rebulleting, so they can be used to characterize topics, eg:
332 `?' question topics
333 `\(' parenthetic comment \(with a matching close paren inside)
334 `[' meta-note \(with a matching close ] inside)
335 `\"' a quotation
336 `=' value settings
337 `~' \"more or less\"
338 `^' see above
340 ... for example. (`#' typically has a special meaning to the software,
341 according to the value of `allout-numbered-bullet'.)
343 See `allout-plain-bullets-string' for the selection of
344 alternating bullets.
346 You must run `set-allout-regexp' in order for outline mode to
347 reconcile to changes of this value.
349 DO NOT include the close-square-bracket, `]', on either of the bullet
350 strings."
351 :type 'string
352 :group 'allout)
353 (make-variable-buffer-local 'allout-distinctive-bullets-string)
354 ;;;###autoload
355 (put 'allout-distinctive-bullets-string 'safe-local-variable 'stringp)
357 ;;;_ = allout-use-mode-specific-leader
358 (defcustom allout-use-mode-specific-leader t
359 "*When non-nil, use mode-specific topic-header prefixes.
361 Allout outline mode will use the mode-specific `allout-mode-leaders' or
362 comment-start string, if any, to lead the topic prefix string, so topic
363 headers look like comments in the programming language. It will also use
364 the comment-start string, with an '_' appended, for `allout-primary-bullet'.
366 String values are used as literals, not regular expressions, so
367 do not escape any regulare-expression characters.
369 Value t means to first check for assoc value in `allout-mode-leaders'
370 alist, then use comment-start string, if any, then use default \(`.').
371 \(See note about use of comment-start strings, below.)
373 Set to the symbol for either of `allout-mode-leaders' or
374 `comment-start' to use only one of them, respectively.
376 Value nil means to always use the default \(`.') and leave
377 `allout-primary-bullet' unaltered.
379 comment-start strings that do not end in spaces are tripled in
380 the header-prefix, and an `_' underscore is tacked on the end, to
381 distinguish them from regular comment strings. comment-start
382 strings that do end in spaces are not tripled, but an underscore
383 is substituted for the space. [This presumes that the space is
384 for appearance, not comment syntax. You can use
385 `allout-mode-leaders' to override this behavior, when
386 undesired.]"
387 :type '(choice (const t) (const nil) string
388 (const allout-mode-leaders)
389 (const comment-start))
390 :group 'allout)
391 ;;;###autoload
392 (put 'allout-use-mode-specific-leader 'safe-local-variable
393 '(lambda (x) (or (memq x '(t nil allout-mode-leaders comment-start))
394 (stringp x))))
395 ;;;_ = allout-mode-leaders
396 (defvar allout-mode-leaders '()
397 "Specific allout-prefix leading strings per major modes.
399 Use this if the mode's comment-start string isn't what you
400 prefer, or if the mode lacks a comment-start string. See
401 `allout-use-mode-specific-leader' for more details.
403 If you're constructing a string that will comment-out outline
404 structuring so it can be included in program code, append an extra
405 character, like an \"_\" underscore, to distinguish the lead string
406 from regular comments that start at the beginning-of-line.")
408 ;;;_ = allout-old-style-prefixes
409 (defcustom allout-old-style-prefixes nil
410 "*When non-nil, use only old-and-crusty `outline-mode' `*' topic prefixes.
412 Non-nil restricts the topic creation and modification
413 functions to asterix-padded prefixes, so they look exactly
414 like the original Emacs-outline style prefixes.
416 Whatever the setting of this variable, both old and new style prefixes
417 are always respected by the topic maneuvering functions."
418 :type 'boolean
419 :group 'allout)
420 (make-variable-buffer-local 'allout-old-style-prefixes)
421 ;;;###autoload
422 (put 'allout-old-style-prefixes 'safe-local-variable
423 (if (fboundp 'booleanp) 'booleanp '(lambda (x) (member x '(t nil)))))
424 ;;;_ = allout-stylish-prefixes - alternating bullets
425 (defcustom allout-stylish-prefixes t
426 "*Do fancy stuff with topic prefix bullets according to level, etc.
428 Non-nil enables topic creation, modification, and repositioning
429 functions to vary the topic bullet char (the char that marks the topic
430 depth) just preceding the start of the topic text) according to level.
431 Otherwise, only asterisks (`*') and distinctive bullets are used.
433 This is how an outline can look (but sans indentation) with stylish
434 prefixes:
436 * Top level
437 .* A topic
438 . + One level 3 subtopic
439 . . One level 4 subtopic
440 . . A second 4 subtopic
441 . + Another level 3 subtopic
442 . #1 A numbered level 4 subtopic
443 . #2 Another
444 . ! Another level 4 subtopic with a different distinctive bullet
445 . #4 And another numbered level 4 subtopic
447 This would be an outline with stylish prefixes inhibited (but the
448 numbered and other distinctive bullets retained):
450 * Top level
451 .* A topic
452 . * One level 3 subtopic
453 . * One level 4 subtopic
454 . * A second 4 subtopic
455 . * Another level 3 subtopic
456 . #1 A numbered level 4 subtopic
457 . #2 Another
458 . ! Another level 4 subtopic with a different distinctive bullet
459 . #4 And another numbered level 4 subtopic
461 Stylish and constant prefixes (as well as old-style prefixes) are
462 always respected by the topic maneuvering functions, regardless of
463 this variable setting.
465 The setting of this var is not relevant when `allout-old-style-prefixes'
466 is non-nil."
467 :type 'boolean
468 :group 'allout)
469 (make-variable-buffer-local 'allout-stylish-prefixes)
470 ;;;###autoload
471 (put 'allout-stylish-prefixes 'safe-local-variable
472 (if (fboundp 'booleanp) 'booleanp '(lambda (x) (member x '(t nil)))))
474 ;;;_ = allout-numbered-bullet
475 (defcustom allout-numbered-bullet "#"
476 "*String designating bullet of topics that have auto-numbering; nil for none.
478 Topics having this bullet have automatic maintenance of a sibling
479 sequence-number tacked on, just after the bullet. Conventionally set
480 to \"#\", you can set it to a bullet of your choice. A nil value
481 disables numbering maintenance."
482 :type '(choice (const nil) string)
483 :group 'allout)
484 (make-variable-buffer-local 'allout-numbered-bullet)
485 ;;;###autoload
486 (put 'allout-numbered-bullet 'safe-local-variable
487 (if (fboundp 'string-or-null-p)
488 'string-or-null-p
489 '(lambda (x) (or (stringp x) (null x)))))
490 ;;;_ = allout-file-xref-bullet
491 (defcustom allout-file-xref-bullet "@"
492 "*Bullet signifying file cross-references, for `allout-resolve-xref'.
494 Set this var to the bullet you want to use for file cross-references."
495 :type '(choice (const nil) string)
496 :group 'allout)
497 ;;;###autoload
498 (put 'allout-file-xref-bullet 'safe-local-variable
499 (if (fboundp 'string-or-null-p)
500 'string-or-null-p
501 '(lambda (x) (or (stringp x) (null x)))))
502 ;;;_ = allout-presentation-padding
503 (defcustom allout-presentation-padding 2
504 "*Presentation-format white-space padding factor, for greater indent."
505 :type 'integer
506 :group 'allout)
508 (make-variable-buffer-local 'allout-presentation-padding)
509 ;;;###autoload
510 (put 'allout-presentation-padding 'safe-local-variable 'integerp)
512 ;;;_ = allout-abbreviate-flattened-numbering
513 (defcustom allout-abbreviate-flattened-numbering nil
514 "*If non-nil, `allout-flatten-exposed-to-buffer' abbreviates topic
515 numbers to minimal amount with some context. Otherwise, entire
516 numbers are always used."
517 :type 'boolean
518 :group 'allout)
520 ;;;_ + LaTeX formatting
521 ;;;_ - allout-number-pages
522 (defcustom allout-number-pages nil
523 "*Non-nil turns on page numbering for LaTeX formatting of an outline."
524 :type 'boolean
525 :group 'allout)
526 ;;;_ - allout-label-style
527 (defcustom allout-label-style "\\large\\bf"
528 "*Font and size of labels for LaTeX formatting of an outline."
529 :type 'string
530 :group 'allout)
531 ;;;_ - allout-head-line-style
532 (defcustom allout-head-line-style "\\large\\sl "
533 "*Font and size of entries for LaTeX formatting of an outline."
534 :type 'string
535 :group 'allout)
536 ;;;_ - allout-body-line-style
537 (defcustom allout-body-line-style " "
538 "*Font and size of entries for LaTeX formatting of an outline."
539 :type 'string
540 :group 'allout)
541 ;;;_ - allout-title-style
542 (defcustom allout-title-style "\\Large\\bf"
543 "*Font and size of titles for LaTeX formatting of an outline."
544 :type 'string
545 :group 'allout)
546 ;;;_ - allout-title
547 (defcustom allout-title '(or buffer-file-name (buffer-name))
548 "*Expression to be evaluated to determine the title for LaTeX
549 formatted copy."
550 :type 'sexp
551 :group 'allout)
552 ;;;_ - allout-line-skip
553 (defcustom allout-line-skip ".05cm"
554 "*Space between lines for LaTeX formatting of an outline."
555 :type 'string
556 :group 'allout)
557 ;;;_ - allout-indent
558 (defcustom allout-indent ".3cm"
559 "*LaTeX formatted depth-indent spacing."
560 :type 'string
561 :group 'allout)
563 ;;;_ + Topic encryption
564 ;;;_ = allout-encryption group
565 (defgroup allout-encryption nil
566 "Settings for topic encryption features of allout outliner."
567 :group 'allout)
568 ;;;_ = allout-topic-encryption-bullet
569 (defcustom allout-topic-encryption-bullet "~"
570 "*Bullet signifying encryption of the entry's body."
571 :type '(choice (const nil) string)
572 :version "22.0"
573 :group 'allout-encryption)
574 ;;;_ = allout-passphrase-verifier-handling
575 (defcustom allout-passphrase-verifier-handling t
576 "*Enable use of symmetric encryption passphrase verifier if non-nil.
578 See the docstring for the `allout-enable-file-variable-adjustment'
579 variable for details about allout ajustment of file variables."
580 :type 'boolean
581 :version "22.0"
582 :group 'allout-encryption)
583 (make-variable-buffer-local 'allout-passphrase-verifier-handling)
584 ;;;_ = allout-passphrase-hint-handling
585 (defcustom allout-passphrase-hint-handling 'always
586 "*Dictate outline encryption passphrase reminder handling:
588 always - always show reminder when prompting
589 needed - show reminder on passphrase entry failure
590 disabled - never present or adjust reminder
592 See the docstring for the `allout-enable-file-variable-adjustment'
593 variable for details about allout ajustment of file variables."
594 :type '(choice (const always)
595 (const needed)
596 (const disabled))
597 :version "22.0"
598 :group 'allout-encryption)
599 (make-variable-buffer-local 'allout-passphrase-hint-handling)
600 ;;;_ = allout-encrypt-unencrypted-on-saves
601 (defcustom allout-encrypt-unencrypted-on-saves t
602 "*When saving, should topics pending encryption be encrypted?
604 The idea is to prevent file-system exposure of any un-encrypted stuff, and
605 mostly covers both deliberate file writes and auto-saves.
607 - Yes: encrypt all topics pending encryption, even if it's the one
608 currently being edited. \(In that case, the currently edited topic
609 will be automatically decrypted before any user interaction, so they
610 can continue editing but the copy on the file system will be
611 encrypted.)
612 Auto-saves will use the \"All except current topic\" mode if this
613 one is selected, to avoid practical difficulties - see below.
614 - All except current topic: skip the topic currently being edited, even if
615 it's pending encryption. This may expose the current topic on the
616 file sytem, but avoids the nuisance of prompts for the encryption
617 passphrase in the middle of editing for, eg, autosaves.
618 This mode is used for auto-saves for both this option and \"Yes\".
619 - No: leave it to the user to encrypt any unencrypted topics.
621 For practical reasons, auto-saves always use the 'except-current policy
622 when auto-encryption is enabled. \(Otherwise, spurious passphrase prompts
623 and unavoidable timing collisions are too disruptive.) If security for a
624 file requires that even the current topic is never auto-saved in the clear,
625 disable auto-saves for that file."
627 :type '(choice (const :tag "Yes" t)
628 (const :tag "All except current topic" except-current)
629 (const :tag "No" nil))
630 :version "22.0"
631 :group 'allout-encryption)
632 (make-variable-buffer-local 'allout-encrypt-unencrypted-on-saves)
634 ;;;_ + Developer
635 ;;;_ = allout-developer group
636 (defgroup allout-developer nil
637 "Settings for topic encryption features of allout outliner."
638 :group 'allout)
639 ;;;_ = allout-run-unit-tests-on-load
640 (defcustom allout-run-unit-tests-on-load nil
641 "*When non-nil, unit tests will be run at end of loading the allout module.
643 Generally, allout code developers are the only ones who'll want to set this.
645 \(If set, this makes it an even better practice to exercise changes by
646 doing byte-compilation with a repeat count, so the file is loaded after
647 compilation.)
649 See `allout-run-unit-tests' to see what's run."
650 :type 'boolean
651 :group 'allout-developer)
653 ;;;_ + Miscellaneous customization
655 ;;;_ = allout-command-prefix
656 (defcustom allout-command-prefix "\C-c "
657 "*Key sequence to be used as prefix for outline mode command key bindings.
659 Default is '\C-c<space>'; just '\C-c' is more short-and-sweet, if you're
660 willing to let allout use a bunch of \C-c keybindings."
661 :type 'string
662 :group 'allout)
664 ;;;_ = allout-keybindings-list
665 ;;; You have to reactivate allout-mode - `(allout-mode t)' - to
666 ;;; institute changes to this var.
667 (defvar allout-keybindings-list ()
668 "*List of `allout-mode' key / function bindings, for `allout-mode-map'.
670 String or vector key will be prefaced with `allout-command-prefix',
671 unless optional third, non-nil element is present.")
672 (setq allout-keybindings-list
674 ; Motion commands:
675 ("\C-n" allout-next-visible-heading)
676 ("\C-p" allout-previous-visible-heading)
677 ("\C-u" allout-up-current-level)
678 ("\C-f" allout-forward-current-level)
679 ("\C-b" allout-backward-current-level)
680 ("\C-a" allout-beginning-of-current-entry)
681 ("\C-e" allout-end-of-entry)
682 ; Exposure commands:
683 ("\C-i" allout-show-children)
684 ("\C-s" allout-show-current-subtree)
685 ("\C-h" allout-hide-current-subtree)
686 ("h" allout-hide-current-subtree)
687 ("\C-o" allout-show-current-entry)
688 ("!" allout-show-all)
689 ("x" allout-toggle-current-subtree-encryption)
690 ; Alteration commands:
691 (" " allout-open-sibtopic)
692 ("." allout-open-subtopic)
693 ("," allout-open-supertopic)
694 ("'" allout-shift-in)
695 (">" allout-shift-in)
696 ("<" allout-shift-out)
697 ("\C-m" allout-rebullet-topic)
698 ("*" allout-rebullet-current-heading)
699 ("#" allout-number-siblings)
700 ("\C-k" allout-kill-line t)
701 ("\M-k" allout-copy-line-as-kill t)
702 ("\C-y" allout-yank t)
703 ("\M-y" allout-yank-pop t)
704 ("\C-k" allout-kill-topic)
705 ("\M-k" allout-copy-topic-as-kill)
706 ; Miscellaneous commands:
707 ;([?\C-\ ] allout-mark-topic)
708 ("@" allout-resolve-xref)
709 ("=c" allout-copy-exposed-to-buffer)
710 ("=i" allout-indented-exposed-to-buffer)
711 ("=t" allout-latexify-exposed)
712 ("=p" allout-flatten-exposed-to-buffer)))
714 ;;;_ = allout-inhibit-auto-fill
715 (defcustom allout-inhibit-auto-fill nil
716 "*If non-nil, auto-fill will be inhibited in the allout buffers.
718 You can customize this setting to set it for all allout buffers, or set it
719 in individual buffers if you want to inhibit auto-fill only in particular
720 buffers. \(You could use a function on `allout-mode-hook' to inhibit
721 auto-fill according, eg, to the major mode.\)
723 If you don't set this and auto-fill-mode is enabled, allout will use the
724 value that `normal-auto-fill-function', if any, when allout mode starts, or
725 else allout's special hanging-indent maintaining auto-fill function,
726 `allout-auto-fill'."
727 :type 'boolean
728 :group 'allout)
729 (make-variable-buffer-local 'allout-inhibit-auto-fill)
731 ;;;_ = allout-use-hanging-indents
732 (defcustom allout-use-hanging-indents t
733 "*If non-nil, topic body text auto-indent defaults to indent of the header.
734 Ie, it is indented to be just past the header prefix. This is
735 relevant mostly for use with indented-text-mode, or other situations
736 where auto-fill occurs."
737 :type 'boolean
738 :group 'allout)
739 (make-variable-buffer-local 'allout-use-hanging-indents)
740 ;;;###autoload
741 (put 'allout-use-hanging-indents 'safe-local-variable
742 (if (fboundp 'booleanp) 'booleanp '(lambda (x) (member x '(t nil)))))
744 ;;;_ = allout-reindent-bodies
745 (defcustom allout-reindent-bodies (if allout-use-hanging-indents
746 'text)
747 "*Non-nil enables auto-adjust of topic body hanging indent with depth shifts.
749 When active, topic body lines that are indented even with or beyond
750 their topic header are reindented to correspond with depth shifts of
751 the header.
753 A value of t enables reindent in non-programming-code buffers, ie
754 those that do not have the variable `comment-start' set. A value of
755 `force' enables reindent whether or not `comment-start' is set."
756 :type '(choice (const nil) (const t) (const text) (const force))
757 :group 'allout)
759 (make-variable-buffer-local 'allout-reindent-bodies)
760 ;;;###autoload
761 (put 'allout-reindent-bodies 'safe-local-variable
762 '(lambda (x) (memq x '(nil t text force))))
764 ;;;_ = allout-enable-file-variable-adjustment
765 (defcustom allout-enable-file-variable-adjustment t
766 "*If non-nil, some allout outline actions edit Emacs local file var text.
768 This can range from changes to existing entries, addition of new ones,
769 and creation of a new local variables section when necessary.
771 Emacs file variables adjustments are also inhibited if `enable-local-variables'
772 is nil.
774 Operations potentially causing edits include allout encryption routines.
775 For details, see `allout-toggle-current-subtree-encryption's docstring."
776 :type 'boolean
777 :group 'allout)
778 (make-variable-buffer-local 'allout-enable-file-variable-adjustment)
780 ;;;_* CODE - no user customizations below.
782 ;;;_ #1 Internal Outline Formatting and Configuration
783 ;;;_ : Version
784 ;;;_ = allout-version
785 (defvar allout-version "2.2.1"
786 "Version of currently loaded outline package. \(allout.el)")
787 ;;;_ > allout-version
788 (defun allout-version (&optional here)
789 "Return string describing the loaded outline version."
790 (interactive "P")
791 (let ((msg (concat "Allout Outline Mode v " allout-version)))
792 (if here (insert msg))
793 (message "%s" msg)
794 msg))
795 ;;;_ : Mode activation (defined here because it's referenced early)
796 ;;;_ = allout-mode
797 (defvar allout-mode nil "Allout outline mode minor-mode flag.")
798 (make-variable-buffer-local 'allout-mode)
799 ;;;_ = allout-layout nil
800 (defvar allout-layout nil ; LEAVE GLOBAL VALUE NIL - see docstring.
801 "Buffer-specific setting for allout layout.
803 In buffers where this is non-nil \(and if `allout-init' has been run, to
804 enable this behavior), `allout-mode' will be automatically activated. The
805 layout dictated by the value will be used to set the initial exposure when
806 `allout-mode' is activated.
808 \*You should not setq-default this variable non-nil unless you want every
809 visited file to be treated as an allout file.*
811 The value would typically be set by a file local variable. For
812 example, the following lines at the bottom of an Emacs Lisp file:
814 ;;;Local variables:
815 ;;;allout-layout: \(0 : -1 -1 0)
816 ;;;End:
818 dictate activation of `allout-mode' mode when the file is visited
819 \(presuming allout-init was already run), followed by the
820 equivalent of `\(allout-expose-topic 0 : -1 -1 0)'. \(This is
821 the layout used for the allout.el source file.)
823 `allout-default-layout' describes the specification format.
824 `allout-layout' can additionally have the value `t', in which
825 case the value of `allout-default-layout' is used.")
826 (make-variable-buffer-local 'allout-layout)
827 ;;;###autoload
828 (put 'allout-layout 'safe-local-variable
829 '(lambda (x) (or (numberp x) (listp x) (memq x '(: * + -)))))
831 ;;;_ : Topic header format
832 ;;;_ = allout-regexp
833 (defvar allout-regexp ""
834 "*Regular expression to match the beginning of a heading line.
836 Any line whose beginning matches this regexp is considered a
837 heading. This var is set according to the user configuration vars
838 by `set-allout-regexp'.")
839 (make-variable-buffer-local 'allout-regexp)
840 ;;;_ = allout-bullets-string
841 (defvar allout-bullets-string ""
842 "A string dictating the valid set of outline topic bullets.
844 This var should *not* be set by the user - it is set by `set-allout-regexp',
845 and is produced from the elements of `allout-plain-bullets-string'
846 and `allout-distinctive-bullets-string'.")
847 (make-variable-buffer-local 'allout-bullets-string)
848 ;;;_ = allout-bullets-string-len
849 (defvar allout-bullets-string-len 0
850 "Length of current buffers' `allout-plain-bullets-string'.")
851 (make-variable-buffer-local 'allout-bullets-string-len)
852 ;;;_ = allout-depth-specific-regexp
853 (defvar allout-depth-specific-regexp ""
854 "*Regular expression to match a heading line prefix for a particular depth.
856 This expression is used to search for depth-specific topic
857 headers at depth 2 and greater. Use `allout-depth-one-regexp'
858 for to seek topics at depth one.
860 This var is set according to the user configuration vars by
861 `set-allout-regexp'. It is prepared with format strings for two
862 decimal numbers, which should each be one less than the depth of the
863 topic prefix to be matched.")
864 (make-variable-buffer-local 'allout-depth-specific-regexp)
865 ;;;_ = allout-depth-one-regexp
866 (defvar allout-depth-one-regexp ""
867 "*Regular expression to match a heading line prefix for depth one.
869 This var is set according to the user configuration vars by
870 `set-allout-regexp'. It is prepared with format strings for two
871 decimal numbers, which should each be one less than the depth of the
872 topic prefix to be matched.")
873 (make-variable-buffer-local 'allout-depth-one-regexp)
874 ;;;_ = allout-line-boundary-regexp
875 (defvar allout-line-boundary-regexp ()
876 "`allout-regexp' with outline style beginning-of-line anchor.
878 This is properly set by `set-allout-regexp'.")
879 (make-variable-buffer-local 'allout-line-boundary-regexp)
880 ;;;_ = allout-bob-regexp
881 (defvar allout-bob-regexp ()
882 "Like `allout-line-boundary-regexp', for headers at beginning of buffer.")
883 (make-variable-buffer-local 'allout-bob-regexp)
884 ;;;_ = allout-header-subtraction
885 (defvar allout-header-subtraction (1- (length allout-header-prefix))
886 "Allout-header prefix length to subtract when computing topic depth.")
887 (make-variable-buffer-local 'allout-header-subtraction)
888 ;;;_ = allout-plain-bullets-string-len
889 (defvar allout-plain-bullets-string-len (length allout-plain-bullets-string)
890 "Length of `allout-plain-bullets-string', updated by `set-allout-regexp'.")
891 (make-variable-buffer-local 'allout-plain-bullets-string-len)
893 ;;;_ = allout-doublecheck-at-and-shallower
894 (defconst allout-doublecheck-at-and-shallower 3
895 "Verify apparent topics of this depth and shallower as being non-aberrant.
897 Verified with `allout-aberrant-container-p'. This check's usefulness is
898 limited to shallow prospects, because the determination of aberrance
899 depends on the mistaken item being followed by a legitimate item of
900 excessively greater depth.")
901 ;;;_ X allout-reset-header-lead (header-lead)
902 (defun allout-reset-header-lead (header-lead)
903 "*Reset the leading string used to identify topic headers."
904 (interactive "sNew lead string: ")
905 (setq allout-header-prefix header-lead)
906 (setq allout-header-subtraction (1- (length allout-header-prefix)))
907 (set-allout-regexp))
908 ;;;_ X allout-lead-with-comment-string (header-lead)
909 (defun allout-lead-with-comment-string (&optional header-lead)
910 "*Set the topic-header leading string to specified string.
912 Useful when for encapsulating outline structure in programming
913 language comments. Returns the leading string."
915 (interactive "P")
916 (if (not (stringp header-lead))
917 (setq header-lead (read-string
918 "String prefix for topic headers: ")))
919 (setq allout-reindent-bodies nil)
920 (allout-reset-header-lead header-lead)
921 header-lead)
922 ;;;_ > allout-infer-header-lead-and-primary-bullet ()
923 (defun allout-infer-header-lead-and-primary-bullet ()
924 "Determine appropriate `allout-header-prefix' and `allout-primary-bullet'.
926 Works according to settings of:
928 `comment-start'
929 `allout-header-prefix' (default)
930 `allout-use-mode-specific-leader'
931 and `allout-mode-leaders'.
933 Apply this via \(re)activation of `allout-mode', rather than
934 invoking it directly."
935 (let* ((use-leader (and (boundp 'allout-use-mode-specific-leader)
936 (if (or (stringp allout-use-mode-specific-leader)
937 (memq allout-use-mode-specific-leader
938 '(allout-mode-leaders
939 comment-start
940 t)))
941 allout-use-mode-specific-leader
942 ;; Oops - garbled value, equate with effect of 't:
943 t)))
944 (leader
945 (cond
946 ((not use-leader) nil)
947 ;; Use the explicitly designated leader:
948 ((stringp use-leader) use-leader)
949 (t (or (and (memq use-leader '(t allout-mode-leaders))
950 ;; Get it from outline mode leaders?
951 (cdr (assq major-mode allout-mode-leaders)))
952 ;; ... didn't get from allout-mode-leaders...
953 (and (memq use-leader '(t comment-start))
954 comment-start
955 ;; Use comment-start, maybe tripled, and with
956 ;; underscore:
957 (concat
958 (if (string= " "
959 (substring comment-start
960 (1- (length comment-start))))
961 ;; Use comment-start, sans trailing space:
962 (substring comment-start 0 -1)
963 (concat comment-start comment-start comment-start))
964 ;; ... and append underscore, whichever:
965 "_")))))))
966 (if (not leader)
968 (setq allout-header-prefix leader)
969 (if (not allout-old-style-prefixes)
970 ;; setting allout-primary-bullet makes the top level topics use -
971 ;; actually, be - the special prefix:
972 (setq allout-primary-bullet leader))
973 allout-header-prefix)))
974 (defalias 'allout-infer-header-lead
975 'allout-infer-header-lead-and-primary-bullet)
976 ;;;_ > allout-infer-body-reindent ()
977 (defun allout-infer-body-reindent ()
978 "Determine proper setting for `allout-reindent-bodies'.
980 Depends on default setting of `allout-reindent-bodies' \(which see)
981 and presence of setting for `comment-start', to tell whether the
982 file is programming code."
983 (if (and allout-reindent-bodies
984 comment-start
985 (not (eq 'force allout-reindent-bodies)))
986 (setq allout-reindent-bodies nil)))
987 ;;;_ > set-allout-regexp ()
988 (defun set-allout-regexp ()
989 "Generate proper topic-header regexp form for outline functions.
991 Works with respect to `allout-plain-bullets-string' and
992 `allout-distinctive-bullets-string'.
994 Also refresh various data structures that hinge on the regexp."
996 (interactive)
997 ;; Derive allout-bullets-string from user configured components:
998 (setq allout-bullets-string "")
999 (let ((strings (list 'allout-plain-bullets-string
1000 'allout-distinctive-bullets-string
1001 'allout-primary-bullet))
1002 cur-string
1003 cur-len
1004 cur-char
1005 index)
1006 (while strings
1007 (setq index 0)
1008 (setq cur-len (length (setq cur-string (symbol-value (car strings)))))
1009 (while (< index cur-len)
1010 (setq cur-char (aref cur-string index))
1011 (setq allout-bullets-string
1012 (concat allout-bullets-string
1013 (cond
1014 ; Single dash would denote a
1015 ; sequence, repeated denotes
1016 ; a dash:
1017 ((eq cur-char ?-) "--")
1018 ; literal close-square-bracket
1019 ; doesn't work right in the
1020 ; expr, exclude it:
1021 ((eq cur-char ?\]) "")
1022 (t (regexp-quote (char-to-string cur-char))))))
1023 (setq index (1+ index)))
1024 (setq strings (cdr strings)))
1026 ;; Derive next for repeated use in allout-pending-bullet:
1027 (setq allout-plain-bullets-string-len (length allout-plain-bullets-string))
1028 (setq allout-header-subtraction (1- (length allout-header-prefix)))
1030 (let (new-part old-part)
1031 (setq new-part (concat "\\("
1032 (regexp-quote allout-header-prefix)
1033 "[ \t]*"
1034 ;; already regexp-quoted in a custom way:
1035 "[" allout-bullets-string "]"
1036 "\\)")
1037 old-part (concat "\\("
1038 (regexp-quote allout-primary-bullet)
1039 "\\|"
1040 (regexp-quote allout-header-prefix)
1041 "\\)"
1043 " ?[^" allout-primary-bullet "]")
1044 allout-regexp (concat new-part
1045 "\\|"
1046 old-part
1047 "\\|\^l")
1049 allout-line-boundary-regexp (concat "\n" new-part
1050 "\\|"
1051 "\n" old-part)
1053 allout-bob-regexp (concat "\\`" new-part
1054 "\\|"
1055 "\\`" old-part))
1057 (setq allout-depth-specific-regexp
1058 (concat "\\(^\\|\\`\\)"
1059 "\\("
1061 ;; new-style spacers-then-bullet string:
1062 "\\("
1063 (allout-format-quote (regexp-quote allout-header-prefix))
1064 " \\{%s\\}"
1065 "[" (allout-format-quote allout-bullets-string) "]"
1066 "\\)"
1068 ;; old-style all-bullets string, if primary not multi-char:
1069 (if (< 0 allout-header-subtraction)
1071 (concat "\\|\\("
1072 (allout-format-quote
1073 (regexp-quote allout-primary-bullet))
1074 (allout-format-quote
1075 (regexp-quote allout-primary-bullet))
1076 (allout-format-quote
1077 (regexp-quote allout-primary-bullet))
1078 "\\{%s\\}"
1079 ;; disqualify greater depths:
1080 "[^"
1081 (allout-format-quote allout-primary-bullet)
1082 "]\\)"
1084 "\\)"
1086 (setq allout-depth-one-regexp
1087 (concat "\\(^\\|\\`\\)"
1088 "\\("
1090 "\\("
1091 (regexp-quote allout-header-prefix)
1092 ;; disqualify any bullet char following any amount of
1093 ;; intervening whitespace:
1094 " *"
1095 (concat "[^ " allout-bullets-string "]")
1096 "\\)"
1097 (if (< 0 allout-header-subtraction)
1098 ;; Need not support anything like the old
1099 ;; bullet style if the prefix is multi-char.
1101 (concat "\\|"
1102 (regexp-quote allout-primary-bullet)
1103 ;; disqualify deeper primary-bullet sequences:
1104 "[^" allout-primary-bullet "]"))
1105 "\\)"
1106 ))))
1107 ;;;_ : Key bindings
1108 ;;;_ = allout-mode-map
1109 (defvar allout-mode-map nil "Keybindings for (allout) outline minor mode.")
1110 ;;;_ > produce-allout-mode-map (keymap-alist &optional base-map)
1111 (defun produce-allout-mode-map (keymap-list &optional base-map)
1112 "Produce keymap for use as allout-mode-map, from KEYMAP-LIST.
1114 Built on top of optional BASE-MAP, or empty sparse map if none specified.
1115 See doc string for allout-keybindings-list for format of binding list."
1116 (let ((map (or base-map (make-sparse-keymap)))
1117 (pref (list allout-command-prefix)))
1118 (mapcar (function
1119 (lambda (cell)
1120 (let ((add-pref (null (cdr (cdr cell))))
1121 (key-suff (list (car cell))))
1122 (apply 'define-key
1123 (list map
1124 (apply 'concat (if add-pref
1125 (append pref key-suff)
1126 key-suff))
1127 (car (cdr cell)))))))
1128 keymap-list)
1129 map))
1130 ;;;_ : Menu bar
1131 (defvar allout-mode-exposure-menu)
1132 (defvar allout-mode-editing-menu)
1133 (defvar allout-mode-navigation-menu)
1134 (defvar allout-mode-misc-menu)
1135 (defun produce-allout-mode-menubar-entries ()
1136 (require 'easymenu)
1137 (easy-menu-define allout-mode-exposure-menu
1138 allout-mode-map
1139 "Allout outline exposure menu."
1140 '("Exposure"
1141 ["Show Entry" allout-show-current-entry t]
1142 ["Show Children" allout-show-children t]
1143 ["Show Subtree" allout-show-current-subtree t]
1144 ["Hide Subtree" allout-hide-current-subtree t]
1145 ["Hide Leaves" allout-hide-current-leaves t]
1146 "----"
1147 ["Show All" allout-show-all t]))
1148 (easy-menu-define allout-mode-editing-menu
1149 allout-mode-map
1150 "Allout outline editing menu."
1151 '("Headings"
1152 ["Open Sibling" allout-open-sibtopic t]
1153 ["Open Subtopic" allout-open-subtopic t]
1154 ["Open Supertopic" allout-open-supertopic t]
1155 "----"
1156 ["Shift Topic In" allout-shift-in t]
1157 ["Shift Topic Out" allout-shift-out t]
1158 ["Rebullet Topic" allout-rebullet-topic t]
1159 ["Rebullet Heading" allout-rebullet-current-heading t]
1160 ["Number Siblings" allout-number-siblings t]
1161 "----"
1162 ["Toggle Topic Encryption"
1163 allout-toggle-current-subtree-encryption
1164 (> (allout-current-depth) 1)]))
1165 (easy-menu-define allout-mode-navigation-menu
1166 allout-mode-map
1167 "Allout outline navigation menu."
1168 '("Navigation"
1169 ["Next Visible Heading" allout-next-visible-heading t]
1170 ["Previous Visible Heading"
1171 allout-previous-visible-heading t]
1172 "----"
1173 ["Up Level" allout-up-current-level t]
1174 ["Forward Current Level" allout-forward-current-level t]
1175 ["Backward Current Level"
1176 allout-backward-current-level t]
1177 "----"
1178 ["Beginning of Entry"
1179 allout-beginning-of-current-entry t]
1180 ["End of Entry" allout-end-of-entry t]
1181 ["End of Subtree" allout-end-of-current-subtree t]))
1182 (easy-menu-define allout-mode-misc-menu
1183 allout-mode-map
1184 "Allout outlines miscellaneous bindings."
1185 '("Misc"
1186 ["Version" allout-version t]
1187 "----"
1188 ["Duplicate Exposed" allout-copy-exposed-to-buffer t]
1189 ["Duplicate Exposed, numbered"
1190 allout-flatten-exposed-to-buffer t]
1191 ["Duplicate Exposed, indented"
1192 allout-indented-exposed-to-buffer t]
1193 "----"
1194 ["Set Header Lead" allout-reset-header-lead t]
1195 ["Set New Exposure" allout-expose-topic t])))
1196 ;;;_ : Allout Modal-Variables Utilities
1197 ;;;_ = allout-mode-prior-settings
1198 (defvar allout-mode-prior-settings nil
1199 "Internal `allout-mode' use; settings to be resumed on mode deactivation.
1201 See `allout-add-resumptions' and `allout-do-resumptions'.")
1202 (make-variable-buffer-local 'allout-mode-prior-settings)
1203 ;;;_ > allout-add-resumptions (&rest pairs)
1204 (defun allout-add-resumptions (&rest pairs)
1205 "Set name/value PAIRS.
1207 Old settings are preserved for later resumption using `allout-do-resumptions'.
1209 The new values are set as a buffer local. On resumption, the prior buffer
1210 scope of the variable is restored along with its value. If it was a void
1211 buffer-local value, then it is left as nil on resumption.
1213 The pairs are lists whose car is the name of the variable and car of the
1214 cdr is the new value: '(some-var some-value)'. The pairs can actually be
1215 triples, where the third element qualifies the disposition of the setting,
1216 as described further below.
1218 If the optional third element is the symbol 'extend, then the new value
1219 created by `cons'ing the second element of the pair onto the front of the
1220 existing value.
1222 If the optional third element is the symbol 'append, then the new value is
1223 extended from the existing one by `append'ing a list containing the second
1224 element of the pair onto the end of the existing value.
1226 Extension, and resumptions in general, should not be used for hook
1227 functions - use the 'local mode of `add-hook' for that, instead.
1229 The settings are stored on `allout-mode-prior-settings'."
1230 (while pairs
1231 (let* ((pair (pop pairs))
1232 (name (car pair))
1233 (value (cadr pair))
1234 (qualifier (if (> (length pair) 2)
1235 (caddr pair)))
1236 prior-value)
1237 (if (not (symbolp name))
1238 (error "Pair's name, %S, must be a symbol, not %s"
1239 name (type-of name)))
1240 (setq prior-value (condition-case nil
1241 (symbol-value name)
1242 (void-variable nil)))
1243 (when (not (assoc name allout-mode-prior-settings))
1244 ;; Not already added as a resumption, create the prior setting entry.
1245 (if (local-variable-p name)
1246 ;; is already local variable - preserve the prior value:
1247 (push (list name prior-value) allout-mode-prior-settings)
1248 ;; wasn't local variable, indicate so for resumption by killing
1249 ;; local value, and make it local:
1250 (push (list name) allout-mode-prior-settings)
1251 (make-local-variable name)))
1252 (if qualifier
1253 (cond ((eq qualifier 'extend)
1254 (if (not (listp prior-value))
1255 (error "extension of non-list prior value attempted")
1256 (set name (cons value prior-value))))
1257 ((eq qualifier 'append)
1258 (if (not (listp prior-value))
1259 (error "appending of non-list prior value attempted")
1260 (set name (append prior-value (list value)))))
1261 (t (error "unrecognized setting qualifier `%s' encountered"
1262 qualifier)))
1263 (set name value)))))
1264 ;;;_ > allout-do-resumptions ()
1265 (defun allout-do-resumptions ()
1266 "Resume all name/value settings registered by `allout-add-resumptions'.
1268 This is used when concluding allout-mode, to resume selected variables to
1269 their settings before allout-mode was started."
1271 (while allout-mode-prior-settings
1272 (let* ((pair (pop allout-mode-prior-settings))
1273 (name (car pair))
1274 (value-cell (cdr pair)))
1275 (if (not value-cell)
1276 ;; Prior value was global:
1277 (kill-local-variable name)
1278 ;; Prior value was explicit:
1279 (set name (car value-cell))))))
1280 ;;;_ : Mode-specific incidentals
1281 ;;;_ > allout-unprotected (expr)
1282 (defmacro allout-unprotected (expr)
1283 "Enable internal outline operations to alter invisible text."
1284 `(let ((inhibit-read-only (if (not buffer-read-only) t))
1285 (inhibit-field-text-motion t))
1286 ,expr))
1287 ;;;_ = allout-mode-hook
1288 (defvar allout-mode-hook nil
1289 "*Hook that's run when allout mode starts.")
1290 ;;;_ = allout-mode-deactivate-hook
1291 (defvar allout-mode-deactivate-hook nil
1292 "*Hook that's run when allout mode ends.")
1293 ;;;_ = allout-exposure-category
1294 (defvar allout-exposure-category nil
1295 "Symbol for use as allout invisible-text overlay category.")
1296 ;;;_ x allout-view-change-hook
1297 (defvar allout-view-change-hook nil
1298 "*\(Deprecated\) A hook run after allout outline exposure changes.
1300 Switch to using `allout-exposure-change-hook' instead. Both hooks are
1301 currently respected, but the other conveys the details of the exposure
1302 change via explicit parameters, and this one will eventually be disabled in
1303 a subsequent allout version.")
1304 ;;;_ = allout-exposure-change-hook
1305 (defvar allout-exposure-change-hook nil
1306 "*Hook that's run after allout outline subtree exposure changes.
1308 It is run at the conclusion of `allout-flag-region'.
1310 Functions on the hook must take three arguments:
1312 - from - integer indicating the point at the start of the change.
1313 - to - integer indicating the point of the end of the change.
1314 - flag - change mode: nil for exposure, otherwise concealment.
1316 This hook might be invoked multiple times by a single command.
1318 This hook is replacing `allout-view-change-hook', which is being deprecated
1319 and eventually will not be invoked.")
1320 ;;;_ = allout-structure-added-hook
1321 (defvar allout-structure-added-hook nil
1322 "*Hook that's run after addition of items to the outline.
1324 Functions on the hook should take two arguments:
1326 - new-start - integer indicating the point at the start of the first new item.
1327 - new-end - integer indicating the point of the end of the last new item.
1329 Some edits that introduce new items may missed by this hook -
1330 specifically edits that native allout routines do not control.
1332 This hook might be invoked multiple times by a single command.")
1333 ;;;_ = allout-structure-deleted-hook
1334 (defvar allout-structure-deleted-hook nil
1335 "*Hook that's run after disciplined deletion of subtrees from the outline.
1337 Functions on the hook must take two arguments:
1339 - depth - integer indicating the depth of the subtree that was deleted.
1340 - removed-from - integer indicating the point where the subtree was removed.
1342 Some edits that remove or invalidate items may missed by this hook -
1343 specifically edits that native allout routines do not control.
1345 This hook might be invoked multiple times by a single command.")
1346 ;;;_ = allout-structure-shifted-hook
1347 (defvar allout-structure-shifted-hook nil
1348 "*Hook that's run after shifting of items in the outline.
1350 Functions on the hook should take two arguments:
1352 - depth-change - integer indicating depth increase, negative for decrease
1353 - start - integer indicating the start point of the shifted parent item.
1355 Some edits that shift items can be missed by this hook - specifically edits
1356 that native allout routines do not control.
1358 This hook might be invoked multiple times by a single command.")
1359 ;;;_ = allout-outside-normal-auto-fill-function
1360 (defvar allout-outside-normal-auto-fill-function nil
1361 "Value of normal-auto-fill-function outside of allout mode.
1363 Used by allout-auto-fill to do the mandated normal-auto-fill-function
1364 wrapped within allout's automatic fill-prefix setting.")
1365 (make-variable-buffer-local 'allout-outside-normal-auto-fill-function)
1366 ;;;_ = file-var-bug hack
1367 (defvar allout-v18/19-file-var-hack nil
1368 "Horrible hack used to prevent invalid multiple triggering of outline
1369 mode from prop-line file-var activation. Used by `allout-mode' function
1370 to track repeats.")
1371 ;;;_ = allout-passphrase-verifier-string
1372 (defvar allout-passphrase-verifier-string nil
1373 "Setting used to test solicited encryption passphrases against the one
1374 already associated with a file.
1376 It consists of an encrypted random string useful only to verify that a
1377 passphrase entered by the user is effective for decryption. The passphrase
1378 itself is \*not* recorded in the file anywhere, and the encrypted contents
1379 are random binary characters to avoid exposing greater susceptibility to
1380 search attacks.
1382 The verifier string is retained as an Emacs file variable, as well as in
1383 the emacs buffer state, if file variable adjustments are enabled. See
1384 `allout-enable-file-variable-adjustment' for details about that.")
1385 (make-variable-buffer-local 'allout-passphrase-verifier-string)
1386 ;;;###autoload
1387 (put 'allout-passphrase-verifier-string 'safe-local-variable 'stringp)
1388 ;;;_ = allout-passphrase-hint-string
1389 (defvar allout-passphrase-hint-string ""
1390 "Variable used to retain reminder string for file's encryption passphrase.
1392 See the description of `allout-passphrase-hint-handling' for details about how
1393 the reminder is deployed.
1395 The hint is retained as an Emacs file variable, as well as in the emacs buffer
1396 state, if file variable adjustments are enabled. See
1397 `allout-enable-file-variable-adjustment' for details about that.")
1398 (make-variable-buffer-local 'allout-passphrase-hint-string)
1399 (setq-default allout-passphrase-hint-string "")
1400 ;;;###autoload
1401 (put 'allout-passphrase-hint-string 'safe-local-variable 'stringp)
1402 ;;;_ = allout-after-save-decrypt
1403 (defvar allout-after-save-decrypt nil
1404 "Internal variable, is nil or has the value of two points:
1406 - the location of a topic to be decrypted after saving is done
1407 - where to situate the cursor after the decryption is performed
1409 This is used to decrypt the topic that was currently being edited, if it
1410 was encrypted automatically as part of a file write or autosave.")
1411 (make-variable-buffer-local 'allout-after-save-decrypt)
1412 ;;;_ = allout-encryption-plaintext-sanitization-regexps
1413 (defvar allout-encryption-plaintext-sanitization-regexps nil
1414 "List of regexps whose matches are removed from plaintext before encryption.
1416 This is for the sake of removing artifacts, like escapes, that are added on
1417 and not actually part of the original plaintext. The removal is done just
1418 prior to encryption.
1420 Entries must be symbols that are bound to the desired values.
1422 Each value can be a regexp or a list with a regexp followed by a
1423 substitution string. If it's just a regexp, all its matches are removed
1424 before the text is encrypted. If it's a regexp and a substitution, the
1425 substition is used against the regexp matches, a la `replace-match'.")
1426 (make-variable-buffer-local 'allout-encryption-text-removal-regexps)
1427 ;;;_ = allout-encryption-ciphertext-rejection-regexps
1428 (defvar allout-encryption-ciphertext-rejection-regexps nil
1429 "Variable for regexps matching plaintext to remove before encryption.
1431 This is for the sake of redoing encryption in cases where the ciphertext
1432 incidentally contains strings that would disrupt mode operation -
1433 for example, a line that happens to look like an allout-mode topic prefix.
1435 Entries must be symbols that are bound to the desired regexp values.
1437 The encryption will be retried up to
1438 `allout-encryption-ciphertext-rejection-limit' times, after which an error
1439 is raised.")
1441 (make-variable-buffer-local 'allout-encryption-ciphertext-rejection-regexps)
1442 ;;;_ = allout-encryption-ciphertext-rejection-ceiling
1443 (defvar allout-encryption-ciphertext-rejection-ceiling 5
1444 "Limit on number of times encryption ciphertext is rejected.
1446 See `allout-encryption-ciphertext-rejection-regexps' for rejection reasons.")
1447 (make-variable-buffer-local 'allout-encryption-ciphertext-rejection-ceiling)
1448 ;;;_ > allout-mode-p ()
1449 ;; Must define this macro above any uses, or byte compilation will lack
1450 ;; proper def, if file isn't loaded - eg, during emacs build!
1451 (defmacro allout-mode-p ()
1452 "Return t if `allout-mode' is active in current buffer."
1453 'allout-mode)
1454 ;;;_ > allout-write-file-hook-handler ()
1455 (defun allout-write-file-hook-handler ()
1456 "Implement `allout-encrypt-unencrypted-on-saves' policy for file writes."
1458 (if (or (not (allout-mode-p))
1459 (not (boundp 'allout-encrypt-unencrypted-on-saves))
1460 (not allout-encrypt-unencrypted-on-saves))
1462 (let ((except-mark (and (equal allout-encrypt-unencrypted-on-saves
1463 'except-current)
1464 (point-marker))))
1465 (if (save-excursion (goto-char (point-min))
1466 (allout-next-topic-pending-encryption except-mark))
1467 (progn
1468 (message "auto-encrypting pending topics")
1469 (sit-for 0)
1470 (condition-case failure
1471 (setq allout-after-save-decrypt
1472 (allout-encrypt-decrypted except-mark))
1473 (error (progn
1474 (message
1475 "allout-write-file-hook-handler suppressing error %s"
1476 failure)
1477 (sit-for 2))))))
1479 nil)
1480 ;;;_ > allout-auto-save-hook-handler ()
1481 (defun allout-auto-save-hook-handler ()
1482 "Implement `allout-encrypt-unencrypted-on-saves' policy for auto save."
1484 (if (and (allout-mode-p) allout-encrypt-unencrypted-on-saves)
1485 ;; Always implement 'except-current policy when enabled.
1486 (let ((allout-encrypt-unencrypted-on-saves 'except-current))
1487 (allout-write-file-hook-handler))))
1488 ;;;_ > allout-after-saves-handler ()
1489 (defun allout-after-saves-handler ()
1490 "Decrypt topic encrypted for save, if it's currently being edited.
1492 Ie, if it was pending encryption and contained the point in its body before
1493 the save.
1495 We use values stored in `allout-after-save-decrypt' to locate the topic
1496 and the place for the cursor after the decryption is done."
1497 (if (not (and (allout-mode-p)
1498 (boundp 'allout-after-save-decrypt)
1499 allout-after-save-decrypt))
1501 (goto-char (car allout-after-save-decrypt))
1502 (let ((was-modified (buffer-modified-p)))
1503 (allout-toggle-subtree-encryption)
1504 (if (not was-modified)
1505 (set-buffer-modified-p nil)))
1506 (goto-char (cadr allout-after-save-decrypt))
1507 (setq allout-after-save-decrypt nil))
1510 ;;;_ #2 Mode activation
1511 ;;;_ = allout-explicitly-deactivated
1512 (defvar allout-explicitly-deactivated nil
1513 "If t, `allout-mode's last deactivation was deliberate.
1514 So `allout-post-command-business' should not reactivate it...")
1515 (make-variable-buffer-local 'allout-explicitly-deactivated)
1516 ;;;_ > allout-init (&optional mode)
1517 (defun allout-init (&optional mode)
1518 "Prime `allout-mode' to enable/disable auto-activation, wrt `allout-layout'.
1520 MODE is one of the following symbols:
1522 - nil \(or no argument) deactivate auto-activation/layout;
1523 - `activate', enable auto-activation only;
1524 - `ask', enable auto-activation, and enable auto-layout but with
1525 confirmation for layout operation solicited from user each time;
1526 - `report', just report and return the current auto-activation state;
1527 - anything else \(eg, t) for auto-activation and auto-layout, without
1528 any confirmation check.
1530 Use this function to setup your Emacs session for automatic activation
1531 of allout outline mode, contingent to the buffer-specific setting of
1532 the `allout-layout' variable. (See `allout-layout' and
1533 `allout-expose-topic' docstrings for more details on auto layout).
1535 `allout-init' works by setting up (or removing) the `allout-mode'
1536 find-file-hook, and giving `allout-auto-activation' a suitable
1537 setting.
1539 To prime your Emacs session for full auto-outline operation, include
1540 the following two lines in your Emacs init file:
1542 \(require 'allout)
1543 \(allout-init t)"
1545 (interactive)
1546 (if (interactive-p)
1547 (progn
1548 (setq mode
1549 (completing-read
1550 (concat "Select outline auto setup mode "
1551 "(empty for report, ? for options) ")
1552 '(("nil")("full")("activate")("deactivate")
1553 ("ask") ("report") (""))
1556 (if (string= mode "")
1557 (setq mode 'report)
1558 (setq mode (intern-soft mode)))))
1559 (let
1560 ;; convenience aliases, for consistent ref to respective vars:
1561 ((hook 'allout-find-file-hook)
1562 (find-file-hook-var-name (if (boundp 'find-file-hook)
1563 'find-file-hook
1564 'find-file-hooks))
1565 (curr-mode 'allout-auto-activation))
1567 (cond ((not mode)
1568 (set find-file-hook-var-name
1569 (delq hook (symbol-value find-file-hook-var-name)))
1570 (if (interactive-p)
1571 (message "Allout outline mode auto-activation inhibited.")))
1572 ((eq mode 'report)
1573 (if (not (memq hook (symbol-value find-file-hook-var-name)))
1574 (allout-init nil)
1575 ;; Just punt and use the reports from each of the modes:
1576 (allout-init (symbol-value curr-mode))))
1577 (t (add-hook find-file-hook-var-name hook)
1578 (set curr-mode ; `set', not `setq'!
1579 (cond ((eq mode 'activate)
1580 (message
1581 "Outline mode auto-activation enabled.")
1582 'activate)
1583 ((eq mode 'report)
1584 ;; Return the current mode setting:
1585 (allout-init mode))
1586 ((eq mode 'ask)
1587 (message
1588 (concat "Outline mode auto-activation and "
1589 "-layout \(upon confirmation) enabled."))
1590 'ask)
1591 ((message
1592 "Outline mode auto-activation and -layout enabled.")
1593 'full)))))))
1594 ;;;_ > allout-setup-menubar ()
1595 (defun allout-setup-menubar ()
1596 "Populate the current buffer's menubar with `allout-mode' stuff."
1597 (let ((menus (list allout-mode-exposure-menu
1598 allout-mode-editing-menu
1599 allout-mode-navigation-menu
1600 allout-mode-misc-menu))
1601 cur)
1602 (while menus
1603 (setq cur (car menus)
1604 menus (cdr menus))
1605 (easy-menu-add cur))))
1606 ;;;_ > allout-overlay-preparations
1607 (defun allout-overlay-preparations ()
1608 "Set the properties of the allout invisible-text overlay and others."
1609 (setplist 'allout-exposure-category nil)
1610 (put 'allout-exposure-category 'invisible 'allout)
1611 (put 'allout-exposure-category 'evaporate t)
1612 ;; XXX We use isearch-open-invisible *and* isearch-mode-end-hook. The
1613 ;; latter would be sufficient, but it seems that a separate behavior -
1614 ;; the _transient_ opening of invisible text during isearch - is keyed to
1615 ;; presence of the isearch-open-invisible property - even though this
1616 ;; property controls the isearch _arrival_ behavior. This is the case at
1617 ;; least in emacs 21, 22.0, and xemacs 21.4.
1618 (put 'allout-exposure-category 'isearch-open-invisible
1619 'allout-isearch-end-handler)
1620 (if (featurep 'xemacs)
1621 (put 'allout-exposure-category 'start-open t)
1622 (put 'allout-exposure-category 'insert-in-front-hooks
1623 '(allout-overlay-insert-in-front-handler)))
1624 (put 'allout-exposure-category 'modification-hooks
1625 '(allout-overlay-interior-modification-handler)))
1626 ;;;_ > allout-mode (&optional toggle)
1627 ;;;_ : Defun:
1628 ;;;###autoload
1629 (defun allout-mode (&optional toggle)
1630 ;;;_ . Doc string:
1631 "Toggle minor mode for controlling exposure and editing of text outlines.
1632 \\<allout-mode-map>
1634 Optional arg forces mode to re-initialize iff arg is positive num or
1635 symbol. Allout outline mode always runs as a minor mode.
1637 Allout outline mode provides extensive outline oriented formatting and
1638 manipulation. It enables structural editing of outlines, as well as
1639 navigation and exposure. It also is specifically aimed at
1640 accommodating syntax-sensitive text like programming languages. \(For
1641 an example, see the allout code itself, which is organized as an allout
1642 outline.)
1644 In addition to outline navigation and exposure, allout includes:
1646 - topic-oriented repositioning, promotion/demotion, cut, and paste
1647 - integral outline exposure-layout
1648 - incremental search with dynamic exposure and reconcealment of hidden text
1649 - automatic topic-number maintenance
1650 - easy topic encryption and decryption
1651 - \"Hot-spot\" operation, for single-keystroke maneuvering and
1652 exposure control. \(See the allout-mode docstring.)
1654 and many other features.
1656 Below is a description of the bindings, and then explanation of
1657 special `allout-mode' features and terminology. See also the outline
1658 menubar additions for quick reference to many of the features, and see
1659 the docstring of the function `allout-init' for instructions on
1660 priming your emacs session for automatic activation of `allout-mode'.
1663 The bindings are dictated by the `allout-keybindings-list' and
1664 `allout-command-prefix' variables.
1666 Navigation: Exposure Control:
1667 ---------- ----------------
1668 \\[allout-next-visible-heading] allout-next-visible-heading | \\[allout-hide-current-subtree] allout-hide-current-subtree
1669 \\[allout-previous-visible-heading] allout-previous-visible-heading | \\[allout-show-children] allout-show-children
1670 \\[allout-up-current-level] allout-up-current-level | \\[allout-show-current-subtree] allout-show-current-subtree
1671 \\[allout-forward-current-level] allout-forward-current-level | \\[allout-show-current-entry] allout-show-current-entry
1672 \\[allout-backward-current-level] allout-backward-current-level | \\[allout-show-all] allout-show-all
1673 \\[allout-end-of-entry] allout-end-of-entry
1674 \\[allout-beginning-of-current-entry] allout-beginning-of-current-entry, alternately, goes to hot-spot
1676 Topic Header Production:
1677 -----------------------
1678 \\[allout-open-sibtopic] allout-open-sibtopic Create a new sibling after current topic.
1679 \\[allout-open-subtopic] allout-open-subtopic ... an offspring of current topic.
1680 \\[allout-open-supertopic] allout-open-supertopic ... a sibling of the current topic's parent.
1682 Topic Level and Prefix Adjustment:
1683 ---------------------------------
1684 \\[allout-shift-in] allout-shift-in Shift current topic and all offspring deeper.
1685 \\[allout-shift-out] allout-shift-out ... less deep.
1686 \\[allout-rebullet-current-heading] allout-rebullet-current-heading Prompt for alternate bullet for
1687 current topic.
1688 \\[allout-rebullet-topic] allout-rebullet-topic Reconcile bullets of topic and its offspring
1689 - distinctive bullets are not changed, others
1690 alternated according to nesting depth.
1691 \\[allout-number-siblings] allout-number-siblings Number bullets of topic and siblings - the
1692 offspring are not affected. With repeat
1693 count, revoke numbering.
1695 Topic-oriented Killing and Yanking:
1696 ----------------------------------
1697 \\[allout-kill-topic] allout-kill-topic Kill current topic, including offspring.
1698 \\[allout-copy-topic-as-kill] allout-copy-topic-as-kill Copy current topic, including offspring.
1699 \\[allout-kill-line] allout-kill-line kill-line, attending to outline structure.
1700 \\[allout-copy-line-as-kill] allout-copy-line-as-kill Copy line but don't delete it.
1701 \\[allout-yank] allout-yank Yank, adjusting depth of yanked topic to
1702 depth of heading if yanking into bare topic
1703 heading (ie, prefix sans text).
1704 \\[allout-yank-pop] allout-yank-pop Is to allout-yank as yank-pop is to yank
1706 Topic-oriented Encryption:
1707 -------------------------
1708 \\[allout-toggle-current-subtree-encryption] allout-toggle-current-subtree-encryption Encrypt/Decrypt topic content
1710 Misc commands:
1711 -------------
1712 M-x outlineify-sticky Activate outline mode for current buffer,
1713 and establish a default file-var setting
1714 for `allout-layout'.
1715 \\[allout-mark-topic] allout-mark-topic
1716 \\[allout-copy-exposed-to-buffer] allout-copy-exposed-to-buffer
1717 Duplicate outline, sans concealed text, to
1718 buffer with name derived from derived from that
1719 of current buffer - \"*BUFFERNAME exposed*\".
1720 \\[allout-flatten-exposed-to-buffer] allout-flatten-exposed-to-buffer
1721 Like above 'copy-exposed', but convert topic
1722 prefixes to section.subsection... numeric
1723 format.
1724 \\[eval-expression] (allout-init t) Setup Emacs session for outline mode
1725 auto-activation.
1727 Topic Encryption
1729 Outline mode supports gpg encryption of topics, with support for
1730 symmetric and key-pair modes, passphrase timeout, passphrase
1731 consistency checking, user-provided hinting for symmetric key
1732 mode, and auto-encryption of topics pending encryption on save.
1733 \(Topics pending encryption are, by default, automatically
1734 encrypted during file saves; if you're editing the contents of
1735 such a topic, it is automatically decrypted for continued
1736 editing.) The aim is reliable topic privacy while preventing
1737 accidents like neglected encryption before saves, forgetting
1738 which passphrase was used, and other practical pitfalls.
1740 See `allout-toggle-current-subtree-encryption' function docstring and
1741 `allout-encrypt-unencrypted-on-saves' customization variable for details.
1743 HOT-SPOT Operation
1745 Hot-spot operation provides a means for easy, single-keystroke outline
1746 navigation and exposure control.
1748 When the text cursor is positioned directly on the bullet character of
1749 a topic, regular characters (a to z) invoke the commands of the
1750 corresponding allout-mode keymap control chars. For example, \"f\"
1751 would invoke the command typically bound to \"C-c<space>C-f\"
1752 \(\\[allout-forward-current-level] `allout-forward-current-level').
1754 Thus, by positioning the cursor on a topic bullet, you can
1755 execute the outline navigation and manipulation commands with a
1756 single keystroke. Regular navigation keys (eg, \\[forward-char], \\[next-line]) never get
1757 this special translation, so you can use them to get out of the
1758 hot-spot and back to normal operation.
1760 Note that the command `allout-beginning-of-current-entry' \(\\[allout-beginning-of-current-entry]\)
1761 will move to the hot-spot when the cursor is already located at the
1762 beginning of the current entry, so you usually can hit \\[allout-beginning-of-current-entry]
1763 twice in a row to get to the hot-spot.
1765 Terminology
1767 Topic hierarchy constituents - TOPICS and SUBTOPICS:
1769 TOPIC: A basic, coherent component of an Emacs outline. It can
1770 contain and be contained by other topics.
1771 CURRENT topic:
1772 The visible topic most immediately containing the cursor.
1773 DEPTH: The degree of nesting of a topic; it increases with
1774 containment. Also called the:
1775 LEVEL: The same as DEPTH.
1777 ANCESTORS:
1778 The topics that contain a topic.
1779 PARENT: A topic's immediate ancestor. It has a depth one less than
1780 the topic.
1781 OFFSPRING:
1782 The topics contained by a topic;
1783 SUBTOPIC:
1784 An immediate offspring of a topic;
1785 CHILDREN:
1786 The immediate offspring of a topic.
1787 SIBLINGS:
1788 Topics having the same parent and depth.
1790 Topic text constituents:
1792 HEADER: The first line of a topic, include the topic PREFIX and header
1793 text.
1794 PREFIX: The leading text of a topic which distinguishes it from normal
1795 text. It has a strict form, which consists of a prefix-lead
1796 string, padding, and a bullet. The bullet may be followed by a
1797 number, indicating the ordinal number of the topic among its
1798 siblings, a space, and then the header text.
1800 The relative length of the PREFIX determines the nesting depth
1801 of the topic.
1802 PREFIX-LEAD:
1803 The string at the beginning of a topic prefix, normally a `.'.
1804 It can be customized by changing the setting of
1805 `allout-header-prefix' and then reinitializing `allout-mode'.
1807 By setting the prefix-lead to the comment-string of a
1808 programming language, you can embed outline structuring in
1809 program code without interfering with the language processing
1810 of that code. See `allout-use-mode-specific-leader'
1811 docstring for more detail.
1812 PREFIX-PADDING:
1813 Spaces or asterisks which separate the prefix-lead and the
1814 bullet, determining the depth of the topic.
1815 BULLET: A character at the end of the topic prefix, it must be one of
1816 the characters listed on `allout-plain-bullets-string' or
1817 `allout-distinctive-bullets-string'. (See the documentation
1818 for these variables for more details.) The default choice of
1819 bullet when generating topics varies in a cycle with the depth of
1820 the topic.
1821 ENTRY: The text contained in a topic before any offspring.
1822 BODY: Same as ENTRY.
1825 EXPOSURE:
1826 The state of a topic which determines the on-screen visibility
1827 of its offspring and contained text.
1828 CONCEALED:
1829 Topics and entry text whose display is inhibited. Contiguous
1830 units of concealed text is represented by `...' ellipses.
1832 Concealed topics are effectively collapsed within an ancestor.
1833 CLOSED: A topic whose immediate offspring and body-text is concealed.
1834 OPEN: A topic that is not closed, though its offspring or body may be."
1835 ;;;_ . Code
1836 (interactive "P")
1838 (let* ((active (and (not (equal major-mode 'outline))
1839 (allout-mode-p)))
1840 ; Massage universal-arg `toggle' val:
1841 (toggle (and toggle
1842 (or (and (listp toggle)(car toggle))
1843 toggle)))
1844 ; Activation specifically demanded?
1845 (explicit-activation (and toggle
1846 (or (symbolp toggle)
1847 (and (wholenump toggle)
1848 (not (zerop toggle))))))
1849 ;; allout-mode already called once during this complex command?
1850 (same-complex-command (eq allout-v18/19-file-var-hack
1851 (car command-history)))
1852 (write-file-hook-var-name (cond ((boundp 'write-file-functions)
1853 'write-file-functions)
1854 ((boundp 'write-file-hooks)
1855 'write-file-hooks)
1856 (t 'local-write-file-hooks)))
1857 do-layout
1860 ; See comments below re v19.18,.19 bug.
1861 (setq allout-v18/19-file-var-hack (car command-history))
1863 (cond
1865 ;; Provision for v19.18, 19.19 bug -
1866 ;; Emacs v 19.18, 19.19 file-var code invokes prop-line-designated
1867 ;; modes twice when file is visited. We have to avoid toggling mode
1868 ;; off on second invocation, so we detect it as best we can, and
1869 ;; skip everything.
1870 ((and same-complex-command ; Still in same complex command
1871 ; as last time `allout-mode' invoked.
1872 active ; Already activated.
1873 (not explicit-activation) ; Prop-line file-vars don't have args.
1874 (string-match "^19.1[89]" ; Bug only known to be in v19.18 and
1875 emacs-version)); 19.19.
1878 ;; Deactivation:
1879 ((and (not explicit-activation)
1880 (or active toggle))
1881 ; Activation not explicitly
1882 ; requested, and either in
1883 ; active state or *de*activation
1884 ; specifically requested:
1885 (setq allout-explicitly-deactivated t)
1887 (allout-do-resumptions)
1889 (remove-from-invisibility-spec '(allout . t))
1890 (remove-hook 'pre-command-hook 'allout-pre-command-business t)
1891 (remove-hook 'post-command-hook 'allout-post-command-business t)
1892 (remove-hook 'before-change-functions 'allout-before-change-handler t)
1893 (remove-hook 'isearch-mode-end-hook 'allout-isearch-end-handler t)
1894 (remove-hook write-file-hook-var-name 'allout-write-file-hook-handler t)
1895 (remove-hook 'auto-save-hook 'allout-auto-save-hook-handler t)
1897 (remove-overlays (point-min) (point-max)
1898 'category 'allout-exposure-category)
1900 (setq allout-mode nil)
1901 (run-hooks 'allout-mode-deactivate-hook))
1903 ;; Activation:
1904 ((not active)
1905 (setq allout-explicitly-deactivated nil)
1906 (if allout-old-style-prefixes
1907 ;; Inhibit all the fancy formatting:
1908 (allout-add-resumptions '(allout-primary-bullet "*")))
1910 (allout-overlay-preparations) ; Doesn't hurt to redo this.
1912 (allout-infer-header-lead-and-primary-bullet)
1913 (allout-infer-body-reindent)
1915 (set-allout-regexp)
1916 (allout-add-resumptions
1917 '(allout-encryption-ciphertext-rejection-regexps
1918 allout-line-boundary-regexp
1919 extend)
1920 '(allout-encryption-ciphertext-rejection-regexps
1921 allout-bob-regexp
1922 extend))
1924 ;; Produce map from current version of allout-keybindings-list:
1925 (setq allout-mode-map
1926 (produce-allout-mode-map allout-keybindings-list))
1927 (substitute-key-definition 'beginning-of-line
1928 'allout-beginning-of-line
1929 allout-mode-map global-map)
1930 (substitute-key-definition 'move-beginning-of-line
1931 'allout-beginning-of-line
1932 allout-mode-map global-map)
1933 (substitute-key-definition 'end-of-line
1934 'allout-end-of-line
1935 allout-mode-map global-map)
1936 (substitute-key-definition 'move-end-of-line
1937 'allout-end-of-line
1938 allout-mode-map global-map)
1939 (produce-allout-mode-menubar-entries)
1940 (fset 'allout-mode-map allout-mode-map)
1942 ;; Include on minor-mode-map-alist, if not already there:
1943 (if (not (member '(allout-mode . allout-mode-map)
1944 minor-mode-map-alist))
1945 (setq minor-mode-map-alist
1946 (cons '(allout-mode . allout-mode-map)
1947 minor-mode-map-alist)))
1949 (add-to-invisibility-spec '(allout . t))
1950 (allout-add-resumptions '(line-move-ignore-invisible t))
1951 (add-hook 'pre-command-hook 'allout-pre-command-business nil t)
1952 (add-hook 'post-command-hook 'allout-post-command-business nil t)
1953 (add-hook 'before-change-functions 'allout-before-change-handler
1954 nil t)
1955 (add-hook 'isearch-mode-end-hook 'allout-isearch-end-handler nil t)
1956 (add-hook write-file-hook-var-name 'allout-write-file-hook-handler
1957 nil t)
1958 (add-hook 'auto-save-hook 'allout-auto-save-hook-handler
1959 nil t)
1961 ;; Stash auto-fill settings and adjust so custom allout auto-fill
1962 ;; func will be used if auto-fill is active or activated. (The
1963 ;; custom func respects topic headline, maintains hanging-indents,
1964 ;; etc.)
1965 (if (and auto-fill-function (not allout-inhibit-auto-fill))
1966 ;; allout-auto-fill will use the stashed values and so forth.
1967 (allout-add-resumptions '(auto-fill-function allout-auto-fill)))
1968 (allout-add-resumptions (list 'allout-former-auto-filler
1969 auto-fill-function)
1970 ;; Register allout-auto-fill to be used if
1971 ;; filling is active:
1972 (list 'allout-outside-normal-auto-fill-function
1973 normal-auto-fill-function)
1974 '(normal-auto-fill-function allout-auto-fill)
1975 ;; Paragraphs are broken by topic headlines.
1976 (list 'paragraph-start
1977 (concat paragraph-start "\\|^\\("
1978 allout-regexp "\\)"))
1979 (list 'paragraph-separate
1980 (concat paragraph-separate "\\|^\\("
1981 allout-regexp "\\)")))
1982 (or (assq 'allout-mode minor-mode-alist)
1983 (setq minor-mode-alist
1984 (cons '(allout-mode " Allout") minor-mode-alist)))
1986 (allout-setup-menubar)
1988 (if allout-layout
1989 (setq do-layout t))
1991 (setq allout-mode t)
1992 (run-hooks 'allout-mode-hook))
1994 ;; Reactivation:
1995 ((setq do-layout t)
1996 (allout-infer-body-reindent))
1997 ) ;; end of activation-mode cases.
1999 ;; Do auto layout if warranted:
2000 (let ((use-layout (if (listp allout-layout)
2001 allout-layout
2002 allout-default-layout)))
2003 (if (and do-layout
2004 allout-auto-activation
2005 use-layout
2006 (and (not (eq allout-auto-activation 'activate))
2007 (if (eq allout-auto-activation 'ask)
2008 (if (y-or-n-p (format "Expose %s with layout '%s'? "
2009 (buffer-name)
2010 use-layout))
2012 (message "Skipped %s layout." (buffer-name))
2013 nil)
2014 t)))
2015 (save-excursion
2016 (message "Adjusting '%s' exposure..." (buffer-name))
2017 (goto-char 0)
2018 (allout-this-or-next-heading)
2019 (condition-case err
2020 (progn
2021 (apply 'allout-expose-topic (list use-layout))
2022 (message "Adjusting '%s' exposure... done." (buffer-name)))
2023 ;; Problem applying exposure - notify user, but don't
2024 ;; interrupt, eg, file visit:
2025 (error (message "%s" (car (cdr err)))
2026 (sit-for 1))))))
2027 allout-mode
2028 ) ; let*
2029 ) ; defun
2030 ;;;_ > allout-minor-mode
2031 (defalias 'allout-minor-mode 'allout-mode)
2033 ;;;_ - Position Assessment
2034 ;;;_ > allout-hidden-p (&optional pos)
2035 (defsubst allout-hidden-p (&optional pos)
2036 "Non-nil if the character after point is invisible."
2037 (eq (get-char-property (or pos (point)) 'invisible) 'allout))
2039 ;;;_ > allout-overlay-insert-in-front-handler (ol after beg end
2040 ;;; &optional prelen)
2041 (defun allout-overlay-insert-in-front-handler (ol after beg end
2042 &optional prelen)
2043 "Shift the overlay so stuff inserted in front of it are excluded."
2044 (if after
2045 (move-overlay ol (1+ beg) (overlay-end ol))))
2046 ;;;_ > allout-overlay-interior-modification-handler (ol after beg end
2047 ;;; &optional prelen)
2048 (defun allout-overlay-interior-modification-handler (ol after beg end
2049 &optional prelen)
2050 "Get confirmation before making arbitrary changes to invisible text.
2052 We expose the invisible text and ask for confirmation. Refusal or
2053 keyboard-quit abandons the changes, with keyboard-quit additionally
2054 reclosing the opened text.
2056 No confirmation is necessary when inhibit-read-only is set - eg, allout
2057 internal functions use this feature cohesively bunch changes."
2059 (when (and (not inhibit-read-only) (not after))
2060 (let ((start (point))
2061 (ol-start (overlay-start ol))
2062 (ol-end (overlay-end ol))
2063 first)
2064 (goto-char beg)
2065 (while (< (point) end)
2066 (when (allout-hidden-p)
2067 (allout-show-to-offshoot)
2068 (if (allout-hidden-p)
2069 (save-excursion (forward-char 1)
2070 (allout-show-to-offshoot)))
2071 (when (not first)
2072 (setq first (point))))
2073 (goto-char (if (featurep 'xemacs)
2074 (next-property-change (1+ (point)) nil end)
2075 (next-char-property-change (1+ (point)) end))))
2076 (when first
2077 (goto-char first)
2078 (condition-case nil
2079 (if (not
2080 (yes-or-no-p
2081 (substitute-command-keys
2082 (concat "Modify concealed text? (\"no\" just aborts,"
2083 " \\[keyboard-quit] also reconceals) "))))
2084 (progn (goto-char start)
2085 (error "Concealed-text change refused.")))
2086 (quit (allout-flag-region ol-start ol-end nil)
2087 (allout-flag-region ol-start ol-end t)
2088 (error "Concealed-text change abandoned, text reconcealed."))))
2089 (goto-char start))))
2090 ;;;_ > allout-before-change-handler (beg end)
2091 (defun allout-before-change-handler (beg end)
2092 "Protect against changes to invisible text.
2094 See allout-overlay-interior-modification-handler for details."
2096 (if (and (allout-mode-p) undo-in-progress (allout-hidden-p))
2097 (allout-show-to-offshoot))
2099 ;; allout-overlay-interior-modification-handler on an overlay handles
2100 ;; this in other emacs, via `allout-exposure-category's 'modification-hooks.
2101 (when (and (featurep 'xemacs) (allout-mode-p))
2102 ;; process all of the pending overlays:
2103 (save-excursion
2104 (got-char beg)
2105 (let ((overlay (allout-get-invisibility-overlay)))
2106 (allout-overlay-interior-modification-handler
2107 overlay nil beg end nil)))))
2108 ;;;_ > allout-isearch-end-handler (&optional overlay)
2109 (defun allout-isearch-end-handler (&optional overlay)
2110 "Reconcile allout outline exposure on arriving in hidden text after isearch.
2112 Optional OVERLAY parameter is for when this function is used by
2113 `isearch-open-invisible' overlay property. It is otherwise unused, so this
2114 function can also be used as an `isearch-mode-end-hook'."
2116 (if (and (allout-mode-p) (allout-hidden-p))
2117 (allout-show-to-offshoot)))
2119 ;;;_ #3 Internal Position State-Tracking - "allout-recent-*" funcs
2120 ;;; All the basic outline functions that directly do string matches to
2121 ;;; evaluate heading prefix location set the variables
2122 ;;; `allout-recent-prefix-beginning' and `allout-recent-prefix-end'
2123 ;;; when successful. Functions starting with `allout-recent-' all
2124 ;;; use this state, providing the means to avoid redundant searches
2125 ;;; for just-established data. This optimization can provide
2126 ;;; significant speed improvement, but it must be employed carefully.
2127 ;;;_ = allout-recent-prefix-beginning
2128 (defvar allout-recent-prefix-beginning 0
2129 "Buffer point of the start of the last topic prefix encountered.")
2130 (make-variable-buffer-local 'allout-recent-prefix-beginning)
2131 ;;;_ = allout-recent-prefix-end
2132 (defvar allout-recent-prefix-end 0
2133 "Buffer point of the end of the last topic prefix encountered.")
2134 (make-variable-buffer-local 'allout-recent-prefix-end)
2135 ;;;_ = allout-recent-depth
2136 (defvar allout-recent-depth 0
2137 "Depth of the last topic prefix encountered.")
2138 (make-variable-buffer-local 'allout-recent-depth)
2139 ;;;_ = allout-recent-end-of-subtree
2140 (defvar allout-recent-end-of-subtree 0
2141 "Buffer point last returned by `allout-end-of-current-subtree'.")
2142 (make-variable-buffer-local 'allout-recent-end-of-subtree)
2143 ;;;_ > allout-prefix-data ()
2144 (defsubst allout-prefix-data ()
2145 "Register allout-prefix state data.
2147 For reference by `allout-recent' funcs. Returns BEGINNING."
2148 (setq allout-recent-prefix-end (or (match-end 1) (match-end 2))
2149 allout-recent-prefix-beginning (or (match-beginning 1)
2150 (match-beginning 2))
2151 allout-recent-depth (max 1 (- allout-recent-prefix-end
2152 allout-recent-prefix-beginning
2153 allout-header-subtraction)))
2154 allout-recent-prefix-beginning)
2155 ;;;_ > nullify-allout-prefix-data ()
2156 (defsubst nullify-allout-prefix-data ()
2157 "Mark allout prefix data as being uninformative."
2158 (setq allout-recent-prefix-end (point)
2159 allout-recent-prefix-beginning (point)
2160 allout-recent-depth 0)
2161 allout-recent-prefix-beginning)
2162 ;;;_ > allout-recent-depth ()
2163 (defsubst allout-recent-depth ()
2164 "Return depth of last heading encountered by an outline maneuvering function.
2166 All outline functions which directly do string matches to assess
2167 headings set the variables `allout-recent-prefix-beginning' and
2168 `allout-recent-prefix-end' if successful. This function uses those settings
2169 to return the current depth."
2171 allout-recent-depth)
2172 ;;;_ > allout-recent-prefix ()
2173 (defsubst allout-recent-prefix ()
2174 "Like `allout-recent-depth', but returns text of last encountered prefix.
2176 All outline functions which directly do string matches to assess
2177 headings set the variables `allout-recent-prefix-beginning' and
2178 `allout-recent-prefix-end' if successful. This function uses those settings
2179 to return the current prefix."
2180 (buffer-substring-no-properties allout-recent-prefix-beginning
2181 allout-recent-prefix-end))
2182 ;;;_ > allout-recent-bullet ()
2183 (defmacro allout-recent-bullet ()
2184 "Like allout-recent-prefix, but returns bullet of last encountered prefix.
2186 All outline functions which directly do string matches to assess
2187 headings set the variables `allout-recent-prefix-beginning' and
2188 `allout-recent-prefix-end' if successful. This function uses those settings
2189 to return the current depth of the most recently matched topic."
2190 '(buffer-substring-no-properties (1- allout-recent-prefix-end)
2191 allout-recent-prefix-end))
2193 ;;;_ #4 Navigation
2195 ;;;_ - Position Assessment
2196 ;;;_ : Location Predicates
2197 ;;;_ > allout-on-current-heading-p ()
2198 (defun allout-on-current-heading-p ()
2199 "Return non-nil if point is on current visible topics' header line.
2201 Actually, returns prefix beginning point."
2202 (save-excursion
2203 (allout-beginning-of-current-line)
2204 (and (looking-at allout-regexp)
2205 (allout-prefix-data)
2206 (or (> allout-recent-depth allout-doublecheck-at-and-shallower)
2207 (not (allout-aberrant-container-p))))))
2208 ;;;_ > allout-on-heading-p ()
2209 (defalias 'allout-on-heading-p 'allout-on-current-heading-p)
2210 ;;;_ > allout-e-o-prefix-p ()
2211 (defun allout-e-o-prefix-p ()
2212 "True if point is located where current topic prefix ends, heading begins."
2213 (and (save-excursion (let ((inhibit-field-text-motion t))
2214 (beginning-of-line))
2215 (looking-at allout-regexp))
2216 (= (point)(save-excursion (allout-end-of-prefix)(point)))))
2217 ;;;_ > allout-aberrant-container-p ()
2218 (defun allout-aberrant-container-p ()
2219 "True if topic, or next sibling with children, contains them discontinuously.
2221 Discontinuous means an immediate offspring that is nested more
2222 than one level deeper than the topic.
2224 If topic has no offspring, then the next sibling with offspring will
2225 determine whether or not this one is determined to be aberrant.
2227 If true, then the allout-recent-* settings are calibrated on the
2228 offspring that qaulifies it as aberrant, ie with depth that
2229 exceeds the topic by more than one."
2231 ;; This is most clearly understood when considering standard-prefix-leader
2232 ;; low-level topics, which can all too easily match text not intended as
2233 ;; headers. For example, any line with a leading '.' or '*' and lacking a
2234 ;; following bullet qualifies without this protection. (A sequence of
2235 ;; them can occur naturally, eg a typical textual bullet list.) We
2236 ;; disqualify such low-level sequences when they are followed by a
2237 ;; discontinuously contained child, inferring that the sequences are not
2238 ;; actually connected with their prospective context.
2240 (let ((depth (allout-depth))
2241 (start-point (point))
2242 done aberrant)
2243 (save-excursion
2244 (while (and (not done)
2245 (re-search-forward allout-line-boundary-regexp nil 0))
2246 (allout-prefix-data)
2247 (goto-char allout-recent-prefix-beginning)
2248 (cond
2249 ;; sibling - continue:
2250 ((eq allout-recent-depth depth))
2251 ;; first offspring is excessive - aberrant:
2252 ((> allout-recent-depth (1+ depth))
2253 (setq done t aberrant t))
2254 ;; next non-sibling is lower-depth - not aberrant:
2255 (t (setq done t)))))
2256 (if aberrant
2257 aberrant
2258 (goto-char start-point)
2259 ;; recalibrate allout-recent-*
2260 (allout-depth)
2261 nil)))
2262 ;;;_ : Location attributes
2263 ;;;_ > allout-depth ()
2264 (defun allout-depth ()
2265 "Return depth of topic most immediately containing point.
2267 Return zero if point is not within any topic.
2269 Like `allout-current-depth', but respects hidden as well as visible topics."
2270 (save-excursion
2271 (let ((start-point (point)))
2272 (if (and (allout-goto-prefix)
2273 (not (< start-point (point))))
2274 allout-recent-depth
2275 (progn
2276 ;; Oops, no prefix, nullify it:
2277 (nullify-allout-prefix-data)
2278 ;; ... and return 0:
2279 0)))))
2280 ;;;_ > allout-current-depth ()
2281 (defun allout-current-depth ()
2282 "Return depth of visible topic most immediately containing point.
2284 Return zero if point is not within any topic."
2285 (save-excursion
2286 (if (allout-back-to-current-heading)
2287 (max 1
2288 (- allout-recent-prefix-end
2289 allout-recent-prefix-beginning
2290 allout-header-subtraction))
2291 0)))
2292 ;;;_ > allout-get-current-prefix ()
2293 (defun allout-get-current-prefix ()
2294 "Topic prefix of the current topic."
2295 (save-excursion
2296 (if (allout-goto-prefix)
2297 (allout-recent-prefix))))
2298 ;;;_ > allout-get-bullet ()
2299 (defun allout-get-bullet ()
2300 "Return bullet of containing topic (visible or not)."
2301 (save-excursion
2302 (and (allout-goto-prefix)
2303 (allout-recent-bullet))))
2304 ;;;_ > allout-current-bullet ()
2305 (defun allout-current-bullet ()
2306 "Return bullet of current (visible) topic heading, or none if none found."
2307 (condition-case nil
2308 (save-excursion
2309 (allout-back-to-current-heading)
2310 (buffer-substring-no-properties (- allout-recent-prefix-end 1)
2311 allout-recent-prefix-end))
2312 ;; Quick and dirty provision, ostensibly for missing bullet:
2313 (args-out-of-range nil))
2315 ;;;_ > allout-get-prefix-bullet (prefix)
2316 (defun allout-get-prefix-bullet (prefix)
2317 "Return the bullet of the header prefix string PREFIX."
2318 ;; Doesn't make sense if we're old-style prefixes, but this just
2319 ;; oughtn't be called then, so forget about it...
2320 (if (string-match allout-regexp prefix)
2321 (substring prefix (1- (match-end 2)) (match-end 2))))
2322 ;;;_ > allout-sibling-index (&optional depth)
2323 (defun allout-sibling-index (&optional depth)
2324 "Item number of this prospective topic among its siblings.
2326 If optional arg DEPTH is greater than current depth, then we're
2327 opening a new level, and return 0.
2329 If less than this depth, ascend to that depth and count..."
2331 (save-excursion
2332 (cond ((and depth (<= depth 0) 0))
2333 ((or (null depth) (= depth (allout-depth)))
2334 (let ((index 1))
2335 (while (allout-previous-sibling allout-recent-depth nil)
2336 (setq index (1+ index)))
2337 index))
2338 ((< depth allout-recent-depth)
2339 (allout-ascend-to-depth depth)
2340 (allout-sibling-index))
2341 (0))))
2342 ;;;_ > allout-topic-flat-index ()
2343 (defun allout-topic-flat-index ()
2344 "Return a list indicating point's numeric section.subsect.subsubsect...
2345 Outermost is first."
2346 (let* ((depth (allout-depth))
2347 (next-index (allout-sibling-index depth))
2348 (rev-sibls nil))
2349 (while (> next-index 0)
2350 (setq rev-sibls (cons next-index rev-sibls))
2351 (setq depth (1- depth))
2352 (setq next-index (allout-sibling-index depth)))
2353 rev-sibls)
2356 ;;;_ - Navigation routines
2357 ;;;_ > allout-beginning-of-current-line ()
2358 (defun allout-beginning-of-current-line ()
2359 "Like beginning of line, but to visible text."
2361 ;; This combination of move-beginning-of-line and beginning-of-line is
2362 ;; deliberate, but the (beginning-of-line) may now be superfluous.
2363 (let ((inhibit-field-text-motion t))
2364 (move-beginning-of-line 1)
2365 (beginning-of-line)
2366 (while (and (not (bobp)) (or (not (bolp)) (allout-hidden-p)))
2367 (beginning-of-line)
2368 (if (or (allout-hidden-p) (not (bolp)))
2369 (forward-char -1)))))
2370 ;;;_ > allout-end-of-current-line ()
2371 (defun allout-end-of-current-line ()
2372 "Move to the end of line, past concealed text if any."
2373 ;; XXX This is for symmetry with `allout-beginning-of-current-line' -
2374 ;; `move-end-of-line' doesn't suffer the same problem as
2375 ;; `move-beginning-of-line'.
2376 (let ((inhibit-field-text-motion t))
2377 (end-of-line)
2378 (while (allout-hidden-p)
2379 (end-of-line)
2380 (if (allout-hidden-p) (forward-char 1)))))
2381 ;;;_ > allout-beginning-of-line ()
2382 (defun allout-beginning-of-line ()
2383 "Beginning-of-line with `allout-beginning-of-line-cycles' behavior, if set."
2385 (interactive)
2387 (if (or (not allout-beginning-of-line-cycles)
2388 (not (equal last-command this-command)))
2389 (move-beginning-of-line 1)
2390 (allout-depth)
2391 (let ((beginning-of-body
2392 (save-excursion
2393 (while (and (<= allout-recent-depth
2394 allout-doublecheck-at-and-shallower)
2395 (allout-aberrant-container-p)
2396 (allout-previous-visible-heading 1)))
2397 (allout-beginning-of-current-entry)
2398 (point))))
2399 (cond ((= (current-column) 0)
2400 (goto-char beginning-of-body))
2401 ((< (point) beginning-of-body)
2402 (allout-beginning-of-current-line))
2403 ((= (point) beginning-of-body)
2404 (goto-char (allout-current-bullet-pos)))
2405 (t (allout-beginning-of-current-line)
2406 (if (< (point) beginning-of-body)
2407 ;; we were on the headline after its start:
2408 (goto-char beginning-of-body)))))))
2409 ;;;_ > allout-end-of-line ()
2410 (defun allout-end-of-line ()
2411 "End-of-line with `allout-end-of-line-cycles' behavior, if set."
2413 (interactive)
2415 (if (or (not allout-end-of-line-cycles)
2416 (not (equal last-command this-command)))
2417 (allout-end-of-current-line)
2418 (let ((end-of-entry (save-excursion
2419 (allout-end-of-entry)
2420 (point))))
2421 (cond ((not (eolp))
2422 (allout-end-of-current-line))
2423 ((or (allout-hidden-p) (save-excursion
2424 (forward-char -1)
2425 (allout-hidden-p)))
2426 (allout-back-to-current-heading)
2427 (allout-show-current-entry)
2428 (allout-show-children)
2429 (allout-end-of-entry))
2430 ((>= (point) end-of-entry)
2431 (allout-back-to-current-heading)
2432 (allout-end-of-current-line))
2433 (t (allout-end-of-entry))))))
2434 ;;;_ > allout-next-heading ()
2435 (defsubst allout-next-heading ()
2436 "Move to the heading for the topic \(possibly invisible) after this one.
2438 Returns the location of the heading, or nil if none found.
2440 We skip anomolous low-level topics, a la `allout-aberrant-container-p'."
2441 (if (looking-at allout-regexp)
2442 (forward-char 1))
2444 (when (re-search-forward allout-line-boundary-regexp nil 0)
2445 (allout-prefix-data)
2446 (and (<= allout-recent-depth allout-doublecheck-at-and-shallower)
2447 ;; this will set allout-recent-* on the first non-aberrant topic,
2448 ;; whether it's the current one or one that disqualifies it:
2449 (allout-aberrant-container-p))
2450 (goto-char allout-recent-prefix-beginning)))
2451 ;;;_ > allout-this-or-next-heading
2452 (defun allout-this-or-next-heading ()
2453 "Position cursor on current or next heading."
2454 ;; A throwaway non-macro that is defined after allout-next-heading
2455 ;; and usable by allout-mode.
2456 (if (not (allout-goto-prefix-doublechecked)) (allout-next-heading)))
2457 ;;;_ > allout-previous-heading ()
2458 (defun allout-previous-heading ()
2459 "Move to the prior \(possibly invisible) heading line.
2461 Return the location of the beginning of the heading, or nil if not found.
2463 We skip anomolous low-level topics, a la `allout-aberrant-container-p'."
2465 (if (bobp)
2467 ;; allout-goto-prefix-doublechecked calls us, so we can't use it here.
2468 (let ((start-point (point)))
2469 (allout-goto-prefix)
2470 (when (or (re-search-backward allout-line-boundary-regexp nil 0)
2471 (looking-at allout-bob-regexp))
2472 (goto-char (allout-prefix-data))
2473 (if (and (<= allout-recent-depth allout-doublecheck-at-and-shallower)
2474 (allout-aberrant-container-p))
2475 (or (allout-previous-heading)
2476 (and (goto-char start-point)
2477 ;; recalibrate allout-recent-*:
2478 (allout-depth)
2479 nil))
2480 (point))))))
2481 ;;;_ > allout-get-invisibility-overlay ()
2482 (defun allout-get-invisibility-overlay ()
2483 "Return the overlay at point that dictates allout invisibility."
2484 (let ((overlays (overlays-at (point)))
2485 got)
2486 (while (and overlays (not got))
2487 (if (equal (overlay-get (car overlays) 'invisible) 'allout)
2488 (setq got (car overlays))
2489 (pop overlays)))
2490 got))
2491 ;;;_ > allout-back-to-visible-text ()
2492 (defun allout-back-to-visible-text ()
2493 "Move to most recent prior character that is visible, and return point."
2494 (if (allout-hidden-p)
2495 (goto-char (overlay-start (allout-get-invisibility-overlay))))
2496 (point))
2498 ;;;_ - Subtree Charting
2499 ;;;_ " These routines either produce or assess charts, which are
2500 ;;; nested lists of the locations of topics within a subtree.
2502 ;;; Charts enable efficient subtree navigation by providing a reusable basis
2503 ;;; for elaborate, compound assessment and adjustment of a subtree.
2505 ;;;_ > allout-chart-subtree (&optional levels visible orig-depth prev-depth)
2506 (defun allout-chart-subtree (&optional levels visible orig-depth prev-depth)
2507 "Produce a location \"chart\" of subtopics of the containing topic.
2509 Optional argument LEVELS specifies a depth limit \(relative to start
2510 depth) for the chart. Null LEVELS means no limit.
2512 When optional argument VISIBLE is non-nil, the chart includes
2513 only the visible subelements of the charted subjects.
2515 The remaining optional args are for internal use by the function.
2517 Point is left at the end of the subtree.
2519 Charts are used to capture outline structure, so that outline-altering
2520 routines need assess the structure only once, and then use the chart
2521 for their elaborate manipulations.
2523 The chart entries for the topics are in reverse order, so the
2524 last topic is listed first. The entry for each topic consists of
2525 an integer indicating the point at the beginning of the topic
2526 prefix. Charts for offspring consists of a list containing,
2527 recursively, the charts for the respective subtopics. The chart
2528 for a topics' offspring precedes the entry for the topic itself.
2530 The other function parameters are for internal recursion, and should
2531 not be specified by external callers. ORIG-DEPTH is depth of topic at
2532 starting point, and PREV-DEPTH is depth of prior topic."
2534 (let ((original (not orig-depth)) ; `orig-depth' set only in recursion.
2535 chart curr-depth)
2537 (if original ; Just starting?
2538 ; Register initial settings and
2539 ; position to first offspring:
2540 (progn (setq orig-depth (allout-depth))
2541 (or prev-depth (setq prev-depth (1+ orig-depth)))
2542 (if visible
2543 (allout-next-visible-heading 1)
2544 (allout-next-heading))))
2546 ;; Loop over the current levels' siblings. Besides being more
2547 ;; efficient than tail-recursing over a level, it avoids exceeding
2548 ;; the typically quite constrained Emacs max-lisp-eval-depth.
2550 ;; Probably would speed things up to implement loop-based stack
2551 ;; operation rather than recursing for lower levels. Bah.
2553 (while (and (not (eobp))
2554 ; Still within original topic?
2555 (< orig-depth (setq curr-depth allout-recent-depth))
2556 (cond ((= prev-depth curr-depth)
2557 ;; Register this one and move on:
2558 (setq chart (cons allout-recent-prefix-beginning chart))
2559 (if (and levels (<= levels 1))
2560 ;; At depth limit - skip sublevels:
2561 (or (allout-next-sibling curr-depth)
2562 ;; or no more siblings - proceed to
2563 ;; next heading at lesser depth:
2564 (while (and (<= curr-depth
2565 allout-recent-depth)
2566 (if visible
2567 (allout-next-visible-heading 1)
2568 (allout-next-heading)))))
2569 (if visible
2570 (allout-next-visible-heading 1)
2571 (allout-next-heading))))
2573 ((and (< prev-depth curr-depth)
2574 (or (not levels)
2575 (> levels 0)))
2576 ;; Recurse on deeper level of curr topic:
2577 (setq chart
2578 (cons (allout-chart-subtree (and levels
2579 (1- levels))
2580 visible
2581 orig-depth
2582 curr-depth)
2583 chart))
2584 ;; ... then continue with this one.
2587 ;; ... else nil if we've ascended back to prev-depth.
2591 (if original ; We're at the last sibling on
2592 ; the original level. Position
2593 ; to the end of it:
2594 (progn (and (not (eobp)) (forward-char -1))
2595 (and (= (preceding-char) ?\n)
2596 (= (aref (buffer-substring (max 1 (- (point) 3))
2597 (point))
2599 ?\n)
2600 (forward-char -1))
2601 (setq allout-recent-end-of-subtree (point))))
2603 chart ; (nreverse chart) not necessary,
2604 ; and maybe not preferable.
2606 ;;;_ > allout-chart-siblings (&optional start end)
2607 (defun allout-chart-siblings (&optional start end)
2608 "Produce a list of locations of this and succeeding sibling topics.
2609 Effectively a top-level chart of siblings. See `allout-chart-subtree'
2610 for an explanation of charts."
2611 (save-excursion
2612 (when (allout-goto-prefix-doublechecked)
2613 (let ((chart (list (point))))
2614 (while (allout-next-sibling)
2615 (setq chart (cons (point) chart)))
2616 (if chart (setq chart (nreverse chart)))))))
2617 ;;;_ > allout-chart-to-reveal (chart depth)
2618 (defun allout-chart-to-reveal (chart depth)
2620 "Return a flat list of hidden points in subtree CHART, up to DEPTH.
2622 If DEPTH is nil, include hidden points at any depth.
2624 Note that point can be left at any of the points on chart, or at the
2625 start point."
2627 (let (result here)
2628 (while (and (or (null depth) (> depth 0))
2629 chart)
2630 (setq here (car chart))
2631 (if (listp here)
2632 (let ((further (allout-chart-to-reveal here (if (null depth)
2633 depth
2634 (1- depth)))))
2635 ;; We're on the start of a subtree - recurse with it, if there's
2636 ;; more depth to go:
2637 (if further (setq result (append further result)))
2638 (setq chart (cdr chart)))
2639 (goto-char here)
2640 (if (allout-hidden-p)
2641 (setq result (cons here result)))
2642 (setq chart (cdr chart))))
2643 result))
2644 ;;;_ X allout-chart-spec (chart spec &optional exposing)
2645 ;; (defun allout-chart-spec (chart spec &optional exposing)
2646 ;; "Not yet \(if ever) implemented.
2648 ;; Produce exposure directives given topic/subtree CHART and an exposure SPEC.
2650 ;; Exposure spec indicates the locations to be exposed and the prescribed
2651 ;; exposure status. Optional arg EXPOSING is an integer, with 0
2652 ;; indicating pending concealment, anything higher indicating depth to
2653 ;; which subtopic headers should be exposed, and negative numbers
2654 ;; indicating (negative of) the depth to which subtopic headers and
2655 ;; bodies should be exposed.
2657 ;; The produced list can have two types of entries. Bare numbers
2658 ;; indicate points in the buffer where topic headers that should be
2659 ;; exposed reside.
2661 ;; - bare negative numbers indicates that the topic starting at the
2662 ;; point which is the negative of the number should be opened,
2663 ;; including their entries.
2664 ;; - bare positive values indicate that this topic header should be
2665 ;; opened.
2666 ;; - Lists signify the beginning and end points of regions that should
2667 ;; be flagged, and the flag to employ. (For concealment: `\(\?r\)', and
2668 ;; exposure:"
2669 ;; (while spec
2670 ;; (cond ((listp spec)
2671 ;; )
2672 ;; )
2673 ;; (setq spec (cdr spec)))
2674 ;; )
2676 ;;;_ - Within Topic
2677 ;;;_ > allout-goto-prefix ()
2678 (defun allout-goto-prefix ()
2679 "Put point at beginning of immediately containing outline topic.
2681 Goes to most immediate subsequent topic if none immediately containing.
2683 Not sensitive to topic visibility.
2685 Returns the point at the beginning of the prefix, or nil if none."
2687 (let (done)
2688 (while (and (not done)
2689 (search-backward "\n" nil 1))
2690 (forward-char 1)
2691 (if (looking-at allout-regexp)
2692 (setq done (allout-prefix-data))
2693 (forward-char -1)))
2694 (if (bobp)
2695 (cond ((looking-at allout-regexp)
2696 (allout-prefix-data))
2697 ((allout-next-heading))
2698 (done))
2699 done)))
2700 ;;;_ > allout-goto-prefix-doublechecked ()
2701 (defun allout-goto-prefix-doublechecked ()
2702 "Put point at beginning of immediately containing outline topic.
2704 Like `allout-goto-prefix', but shallow topics \(according to
2705 `allout-doublecheck-at-and-shallower') are checked and
2706 disqualified for child containment discontinuity, according to
2707 `allout-aberrant-container-p'."
2708 (allout-goto-prefix)
2709 (if (and (<= allout-recent-depth allout-doublecheck-at-and-shallower)
2710 (allout-aberrant-container-p))
2711 (allout-previous-heading)
2712 (point)))
2714 ;;;_ > allout-end-of-prefix ()
2715 (defun allout-end-of-prefix (&optional ignore-decorations)
2716 "Position cursor at beginning of header text.
2718 If optional IGNORE-DECORATIONS is non-nil, put just after bullet,
2719 otherwise skip white space between bullet and ensuing text."
2721 (if (not (allout-goto-prefix-doublechecked))
2723 (goto-char allout-recent-prefix-end)
2724 (if ignore-decorations
2726 (while (looking-at "[0-9]") (forward-char 1))
2727 (if (and (not (eolp)) (looking-at "\\s-")) (forward-char 1)))
2728 ;; Reestablish where we are:
2729 (allout-current-depth)))
2730 ;;;_ > allout-current-bullet-pos ()
2731 (defun allout-current-bullet-pos ()
2732 "Return position of current \(visible) topic's bullet."
2734 (if (not (allout-current-depth))
2736 (1- allout-recent-prefix-end)))
2737 ;;;_ > allout-back-to-current-heading ()
2738 (defun allout-back-to-current-heading ()
2739 "Move to heading line of current topic, or beginning if not in a topic.
2741 If interactive, we position at the end of the prefix.
2743 Return value of resulting point, unless we started outside
2744 of (before any) topics, in which case we return nil."
2746 (allout-beginning-of-current-line)
2747 (let ((bol-point (point)))
2748 (allout-goto-prefix-doublechecked)
2749 (if (<= (point) bol-point)
2750 (if (interactive-p)
2751 (allout-end-of-prefix)
2752 (point))
2753 (goto-char (point-min))
2754 nil)))
2755 ;;;_ > allout-back-to-heading ()
2756 (defalias 'allout-back-to-heading 'allout-back-to-current-heading)
2757 ;;;_ > allout-pre-next-prefix ()
2758 (defun allout-pre-next-prefix ()
2759 "Skip forward to just before the next heading line.
2761 Returns that character position."
2763 (if (allout-next-heading)
2764 (goto-char (1- allout-recent-prefix-beginning))))
2765 ;;;_ > allout-end-of-subtree (&optional current include-trailing-blank)
2766 (defun allout-end-of-subtree (&optional current include-trailing-blank)
2767 "Put point at the end of the last leaf in the containing topic.
2769 Optional CURRENT means put point at the end of the containing
2770 visible topic.
2772 Optional INCLUDE-TRAILING-BLANK means include a trailing blank line, if
2773 any, as part of the subtree. Otherwise, that trailing blank will be
2774 excluded as delimiting whitespace between topics.
2776 Returns the value of point."
2777 (interactive "P")
2778 (if current
2779 (allout-back-to-current-heading)
2780 (allout-goto-prefix-doublechecked))
2781 (let ((level allout-recent-depth))
2782 (allout-next-heading)
2783 (while (and (not (eobp))
2784 (> allout-recent-depth level))
2785 (allout-next-heading))
2786 (if (eobp)
2787 (allout-end-of-entry)
2788 (forward-char -1))
2789 (if (and (not include-trailing-blank) (= ?\n (preceding-char)))
2790 (forward-char -1))
2791 (setq allout-recent-end-of-subtree (point))))
2792 ;;;_ > allout-end-of-current-subtree (&optional include-trailing-blank)
2793 (defun allout-end-of-current-subtree (&optional include-trailing-blank)
2795 "Put point at end of last leaf in currently visible containing topic.
2797 Optional INCLUDE-TRAILING-BLANK means include a trailing blank line, if
2798 any, as part of the subtree. Otherwise, that trailing blank will be
2799 excluded as delimiting whitespace between topics.
2801 Returns the value of point."
2802 (interactive)
2803 (allout-end-of-subtree t include-trailing-blank))
2804 ;;;_ > allout-beginning-of-current-entry ()
2805 (defun allout-beginning-of-current-entry ()
2806 "When not already there, position point at beginning of current topic header.
2808 If already there, move cursor to bullet for hot-spot operation.
2809 \(See `allout-mode' doc string for details of hot-spot operation.)"
2810 (interactive)
2811 (let ((start-point (point)))
2812 (move-beginning-of-line 1)
2813 (if (< 0 (allout-current-depth))
2814 (goto-char allout-recent-prefix-end)
2815 (goto-char (point-min)))
2816 (allout-end-of-prefix)
2817 (if (and (interactive-p)
2818 (= (point) start-point))
2819 (goto-char (allout-current-bullet-pos)))))
2820 ;;;_ > allout-end-of-entry (&optional inclusive)
2821 (defun allout-end-of-entry (&optional inclusive)
2822 "Position the point at the end of the current topics' entry.
2824 Optional INCLUSIVE means also include trailing empty line, if any. When
2825 unset, whitespace between items separates them even when the items are
2826 collapsed."
2827 (interactive)
2828 (allout-pre-next-prefix)
2829 (if (and (not inclusive) (not (bobp)) (= ?\n (preceding-char)))
2830 (forward-char -1))
2831 (point))
2832 ;;;_ > allout-end-of-current-heading ()
2833 (defun allout-end-of-current-heading ()
2834 (interactive)
2835 (allout-beginning-of-current-entry)
2836 (search-forward "\n" nil t)
2837 (forward-char -1))
2838 (defalias 'allout-end-of-heading 'allout-end-of-current-heading)
2839 ;;;_ > allout-get-body-text ()
2840 (defun allout-get-body-text ()
2841 "Return the unmangled body text of the topic immediately containing point."
2842 (save-excursion
2843 (allout-end-of-prefix)
2844 (if (not (search-forward "\n" nil t))
2846 (backward-char 1)
2847 (let ((pre-body (point)))
2848 (if (not pre-body)
2850 (allout-end-of-entry t)
2851 (if (not (= pre-body (point)))
2852 (buffer-substring-no-properties (1+ pre-body) (point))))
2858 ;;;_ - Depth-wise
2859 ;;;_ > allout-ascend-to-depth (depth)
2860 (defun allout-ascend-to-depth (depth)
2861 "Ascend to depth DEPTH, returning depth if successful, nil if not."
2862 (if (and (> depth 0)(<= depth (allout-depth)))
2863 (let (last-ascended)
2864 (while (and (< depth allout-recent-depth)
2865 (setq last-ascended (allout-ascend))))
2866 (goto-char allout-recent-prefix-beginning)
2867 (if (interactive-p) (allout-end-of-prefix))
2868 (and last-ascended allout-recent-depth))))
2869 ;;;_ > allout-ascend ()
2870 (defun allout-ascend ()
2871 "Ascend one level, returning t if successful, nil if not."
2872 (prog1
2873 (if (allout-beginning-of-level)
2874 (allout-previous-heading))
2875 (if (interactive-p) (allout-end-of-prefix))))
2876 ;;;_ > allout-descend-to-depth (depth)
2877 (defun allout-descend-to-depth (depth)
2878 "Descend to depth DEPTH within current topic.
2880 Returning depth if successful, nil if not."
2881 (let ((start-point (point))
2882 (start-depth (allout-depth)))
2883 (while
2884 (and (> (allout-depth) 0)
2885 (not (= depth allout-recent-depth)) ; ... not there yet
2886 (allout-next-heading) ; ... go further
2887 (< start-depth allout-recent-depth))) ; ... still in topic
2888 (if (and (> (allout-depth) 0)
2889 (= allout-recent-depth depth))
2890 depth
2891 (goto-char start-point)
2892 nil))
2894 ;;;_ > allout-up-current-level (arg)
2895 (defun allout-up-current-level (arg)
2896 "Move out ARG levels from current visible topic."
2897 (interactive "p")
2898 (let ((start-point (point)))
2899 (allout-back-to-current-heading)
2900 (if (not (allout-ascend))
2901 (progn (goto-char start-point)
2902 (error "Can't ascend past outermost level"))
2903 (if (interactive-p) (allout-end-of-prefix))
2904 allout-recent-prefix-beginning)))
2906 ;;;_ - Linear
2907 ;;;_ > allout-next-sibling (&optional depth backward)
2908 (defun allout-next-sibling (&optional depth backward)
2909 "Like `allout-forward-current-level', but respects invisible topics.
2911 Traverse at optional DEPTH, or current depth if none specified.
2913 Go backward if optional arg BACKWARD is non-nil.
2915 Return the start point of the new topic if successful, nil otherwise."
2917 (if (if backward (bobp) (eobp))
2919 (let ((target-depth (or depth (allout-depth)))
2920 (start-point (point))
2921 (count 0)
2922 leaping
2923 last-depth)
2924 (while (and
2925 ;; done too few single steps to resort to the leap routine:
2926 (not leaping)
2927 ;; not at limit:
2928 (not (if backward (bobp) (eobp)))
2929 ;; still traversable:
2930 (if backward (allout-previous-heading) (allout-next-heading))
2931 ;; we're below the target depth
2932 (> (setq last-depth allout-recent-depth) target-depth))
2933 (setq count (1+ count))
2934 (if (> count 7) ; lists are commonly 7 +- 2, right?-)
2935 (setq leaping t)))
2936 (cond (leaping
2937 (or (allout-next-sibling-leap target-depth backward)
2938 (progn
2939 (goto-char start-point)
2940 (if depth (allout-depth) target-depth)
2941 nil)))
2942 ((and (not (eobp))
2943 (and (> (or last-depth (allout-depth)) 0)
2944 (= allout-recent-depth target-depth)))
2945 allout-recent-prefix-beginning)
2947 (goto-char start-point)
2948 (if depth (allout-depth) target-depth)
2949 nil)))))
2950 ;;;_ > allout-next-sibling-leap (&optional depth backward)
2951 (defun allout-next-sibling-leap (&optional depth backward)
2952 "Like `allout-next-sibling', but by direct search for topic at depth.
2954 Traverse at optional DEPTH, or current depth if none specified.
2956 Go backward if optional arg BACKWARD is non-nil.
2958 Return the start point of the new topic if successful, nil otherwise.
2960 Costs more than regular `allout-next-sibling' for short traversals:
2962 - we have to check the prior \(next, if travelling backwards)
2963 item to confirm connectivity with the prior topic, and
2964 - if confirmed, we have to reestablish the allout-recent-* settings with
2965 some extra navigation
2966 - if confirmation fails, we have to do more work to recover
2968 It is an increasingly big win when there are many intervening
2969 offspring before the next sibling, however, so
2970 `allout-next-sibling' resorts to this if it finds itself in that
2971 situation."
2973 (if (if backward (bobp) (eobp))
2975 (let* ((start-point (point))
2976 (target-depth (or depth (allout-depth)))
2977 (search-whitespace-regexp nil)
2978 (depth-biased (- target-depth 2))
2979 (expression (if (<= target-depth 1)
2980 allout-depth-one-regexp
2981 (format allout-depth-specific-regexp
2982 depth-biased depth-biased)))
2983 found
2984 done)
2985 (while (not done)
2986 (setq found (if backward
2987 (re-search-backward expression nil 'to-limit)
2988 (forward-char 1)
2989 (re-search-forward expression nil 'to-limit)))
2990 (if (and found (allout-aberrant-container-p))
2991 (setq found nil))
2992 (setq done (or found (if backward (bobp) (eobp)))))
2993 (if (not found)
2994 (progn (goto-char start-point)
2995 nil)
2996 ;; rationale: if any intervening items were at a lower depth, we
2997 ;; would now be on the first offspring at the target depth - ie,
2998 ;; the preceeding item (per the search direction) must be at a
2999 ;; lesser depth. that's all we need to check.
3000 (if backward (allout-next-heading) (allout-previous-heading))
3001 (if (< allout-recent-depth target-depth)
3002 ;; return to start and reestablish allout-recent-*:
3003 (progn
3004 (goto-char start-point)
3005 (allout-depth)
3006 nil)
3007 (goto-char found)
3008 ;; locate cursor and set allout-recent-*:
3009 (allout-goto-prefix))))))
3010 ;;;_ > allout-previous-sibling (&optional depth backward)
3011 (defun allout-previous-sibling (&optional depth backward)
3012 "Like `allout-forward-current-level' backwards, respecting invisible topics.
3014 Optional DEPTH specifies depth to traverse, default current depth.
3016 Optional BACKWARD reverses direction.
3018 Return depth if successful, nil otherwise."
3019 (allout-next-sibling depth (not backward))
3021 ;;;_ > allout-snug-back ()
3022 (defun allout-snug-back ()
3023 "Position cursor at end of previous topic.
3025 Presumes point is at the start of a topic prefix."
3026 (if (or (bobp) (eobp))
3028 (forward-char -1))
3029 (if (or (bobp) (not (= ?\n (preceding-char))))
3031 (forward-char -1))
3032 (point))
3033 ;;;_ > allout-beginning-of-level ()
3034 (defun allout-beginning-of-level ()
3035 "Go back to the first sibling at this level, visible or not."
3036 (allout-end-of-level 'backward))
3037 ;;;_ > allout-end-of-level (&optional backward)
3038 (defun allout-end-of-level (&optional backward)
3039 "Go to the last sibling at this level, visible or not."
3041 (let ((depth (allout-depth)))
3042 (while (allout-previous-sibling depth nil))
3043 (prog1 allout-recent-depth
3044 (if (interactive-p) (allout-end-of-prefix)))))
3045 ;;;_ > allout-next-visible-heading (arg)
3046 (defun allout-next-visible-heading (arg)
3047 "Move to the next ARG'th visible heading line, backward if arg is negative.
3049 Move to buffer limit in indicated direction if headings are exhausted."
3051 (interactive "p")
3052 (let* ((inhibit-field-text-motion t)
3053 (backward (if (< arg 0) (setq arg (* -1 arg))))
3054 (step (if backward -1 1))
3055 prev got)
3057 (while (> arg 0)
3058 (while (and
3059 ;; Boundary condition:
3060 (not (if backward (bobp)(eobp)))
3061 ;; Move, skipping over all concealed lines in one fell swoop:
3062 (prog1 (condition-case nil (or (line-move step) t)
3063 (error nil))
3064 (allout-beginning-of-current-line))
3065 ;; Deal with apparent header line:
3066 (if (not (looking-at allout-regexp))
3067 ;; not a header line, keep looking:
3069 (allout-prefix-data)
3070 (if (and (<= allout-recent-depth
3071 allout-doublecheck-at-and-shallower)
3072 (allout-aberrant-container-p))
3073 ;; skip this aberrant prospective header line:
3075 ;; this prospective headerline qualifies - register:
3076 (setq got allout-recent-prefix-beginning)
3077 ;; and break the loop:
3078 nil))))
3079 ;; Register this got, it may be the last:
3080 (if got (setq prev got))
3081 (setq arg (1- arg)))
3082 (cond (got ; Last move was to a prefix:
3083 (allout-end-of-prefix))
3084 (prev ; Last move wasn't, but prev was:
3085 (goto-char prev)
3086 (allout-end-of-prefix))
3087 ((not backward) (end-of-line) nil))))
3088 ;;;_ > allout-previous-visible-heading (arg)
3089 (defun allout-previous-visible-heading (arg)
3090 "Move to the previous heading line.
3092 With argument, repeats or can move forward if negative.
3093 A heading line is one that starts with a `*' (or that `allout-regexp'
3094 matches)."
3095 (interactive "p")
3096 (prog1 (allout-next-visible-heading (- arg))
3097 (if (interactive-p) (allout-end-of-prefix))))
3098 ;;;_ > allout-forward-current-level (arg)
3099 (defun allout-forward-current-level (arg)
3100 "Position point at the next heading of the same level.
3102 Takes optional repeat-count, goes backward if count is negative.
3104 Returns resulting position, else nil if none found."
3105 (interactive "p")
3106 (let ((start-depth (allout-current-depth))
3107 (start-arg arg)
3108 (backward (> 0 arg)))
3109 (if (= 0 start-depth)
3110 (error "No siblings, not in a topic..."))
3111 (if backward (setq arg (* -1 arg)))
3112 (allout-back-to-current-heading)
3113 (while (and (not (zerop arg))
3114 (if backward
3115 (allout-previous-sibling)
3116 (allout-next-sibling)))
3117 (setq arg (1- arg)))
3118 (if (not (interactive-p))
3120 (allout-end-of-prefix)
3121 (if (not (zerop arg))
3122 (error "Hit %s level %d topic, traversed %d of %d requested"
3123 (if backward "first" "last")
3124 allout-recent-depth
3125 (- (abs start-arg) arg)
3126 (abs start-arg))))))
3127 ;;;_ > allout-backward-current-level (arg)
3128 (defun allout-backward-current-level (arg)
3129 "Inverse of `allout-forward-current-level'."
3130 (interactive "p")
3131 (if (interactive-p)
3132 (let ((current-prefix-arg (* -1 arg)))
3133 (call-interactively 'allout-forward-current-level))
3134 (allout-forward-current-level (* -1 arg))))
3136 ;;;_ #5 Alteration
3138 ;;;_ - Fundamental
3139 ;;;_ = allout-post-goto-bullet
3140 (defvar allout-post-goto-bullet nil
3141 "Outline internal var, for `allout-pre-command-business' hot-spot operation.
3143 When set, tells post-processing to reposition on topic bullet, and
3144 then unset it. Set by `allout-pre-command-business' when implementing
3145 hot-spot operation, where literal characters typed over a topic bullet
3146 are mapped to the command of the corresponding control-key on the
3147 `allout-mode-map'.")
3148 (make-variable-buffer-local 'allout-post-goto-bullet)
3149 ;;;_ = allout-command-counter
3150 (defvar allout-command-counter 0
3151 "Counter that monotonically increases in allout-mode buffers.
3153 Set by `allout-pre-command-business', to support allout addons in
3154 coordinating with allout activity.")
3155 (make-variable-buffer-local 'allout-command-counter)
3156 ;;;_ > allout-post-command-business ()
3157 (defun allout-post-command-business ()
3158 "Outline `post-command-hook' function.
3160 - Implement (and clear) `allout-post-goto-bullet', for hot-spot
3161 outline commands.
3163 - Decrypt topic currently being edited if it was encrypted for a save."
3165 ; Apply any external change func:
3166 (if (not (allout-mode-p)) ; In allout-mode.
3169 (if (and (boundp 'allout-after-save-decrypt)
3170 allout-after-save-decrypt)
3171 (allout-after-saves-handler))
3173 ;; Implement allout-post-goto-bullet, if set:
3174 (if (and allout-post-goto-bullet
3175 (allout-current-bullet-pos))
3176 (progn (goto-char (allout-current-bullet-pos))
3177 (setq allout-post-goto-bullet nil)))
3179 ;;;_ > allout-pre-command-business ()
3180 (defun allout-pre-command-business ()
3181 "Outline `pre-command-hook' function for outline buffers.
3183 Among other things, implements special behavior when the cursor is on the
3184 topic bullet character.
3186 When the cursor is on the bullet character, self-insert characters are
3187 reinterpreted as the corresponding control-character in the
3188 `allout-mode-map'. The `allout-mode' `post-command-hook' insures that
3189 the cursor which has moved as a result of such reinterpretation is
3190 positioned on the bullet character of the destination topic.
3192 The upshot is that you can get easy, single \(ie, unmodified\) key
3193 outline maneuvering operations by positioning the cursor on the bullet
3194 char. When in this mode you can use regular cursor-positioning
3195 command/keystrokes to relocate the cursor off of a bullet character to
3196 return to regular interpretation of self-insert characters."
3198 (if (not (allout-mode-p))
3200 ;; Increment allout-command-counter
3201 (setq allout-command-counter (1+ allout-command-counter))
3202 ;; Do hot-spot navigation.
3203 (if (and (eq this-command 'self-insert-command)
3204 (eq (point)(allout-current-bullet-pos)))
3205 (allout-hotspot-key-handler))))
3206 ;;;_ > allout-hotspot-key-handler ()
3207 (defun allout-hotspot-key-handler ()
3208 "Catchall handling of key bindings in hot-spots.
3210 Translates unmodified keystrokes to corresponding allout commands, when
3211 they would qualify if prefixed with the allout-command-prefix, and sets
3212 this-command accordingly.
3214 Returns the qualifying command, if any, else nil."
3215 (interactive)
3216 (let* ((key-string (if (numberp last-command-char)
3217 (char-to-string last-command-char)))
3218 (key-num (cond ((numberp last-command-char) last-command-char)
3219 ;; for XEmacs character type:
3220 ((and (fboundp 'characterp)
3221 (apply 'characterp (list last-command-char)))
3222 (apply 'char-to-int (list last-command-char)))
3223 (t 0)))
3224 mapped-binding)
3226 (if (zerop key-num)
3229 (if (and
3230 ;; exclude control chars and escape:
3231 (<= 33 key-num)
3232 (setq mapped-binding
3233 (or (and (assoc key-string allout-keybindings-list)
3234 ;; translate literal membership on list:
3235 (cadr (assoc key-string allout-keybindings-list)))
3236 ;; translate as a keybinding:
3237 (key-binding (concat allout-command-prefix
3238 (char-to-string
3239 (if (and (<= 97 key-num) ; "a"
3240 (>= 122 key-num)) ; "z"
3241 (- key-num 96) key-num)))
3242 t))))
3243 ;; Qualified as an allout command - do hot-spot operation.
3244 (setq allout-post-goto-bullet t)
3245 ;; accept-defaults nil, or else we'll get allout-item-icon-key-handler.
3246 (setq mapped-binding (key-binding (char-to-string key-num))))
3248 (while (keymapp mapped-binding)
3249 (setq mapped-binding
3250 (lookup-key mapped-binding (vector (read-char)))))
3252 (if mapped-binding
3253 (setq this-command mapped-binding)))))
3255 ;;;_ > allout-find-file-hook ()
3256 (defun allout-find-file-hook ()
3257 "Activate `allout-mode' on non-nil `allout-auto-activation', `allout-layout'.
3259 See `allout-init' for setup instructions."
3260 (if (and allout-auto-activation
3261 (not (allout-mode-p))
3262 allout-layout)
3263 (allout-mode t)))
3265 ;;;_ - Topic Format Assessment
3266 ;;;_ > allout-solicit-alternate-bullet (depth &optional current-bullet)
3267 (defun allout-solicit-alternate-bullet (depth &optional current-bullet)
3269 "Prompt for and return a bullet char as an alternative to the current one.
3271 Offer one suitable for current depth DEPTH as default."
3273 (let* ((default-bullet (or (and (stringp current-bullet) current-bullet)
3274 (allout-bullet-for-depth depth)))
3275 (sans-escapes (regexp-sans-escapes allout-bullets-string))
3276 choice)
3277 (save-excursion
3278 (goto-char (allout-current-bullet-pos))
3279 (setq choice (solicit-char-in-string
3280 (format "Select bullet: %s ('%s' default): "
3281 sans-escapes
3282 (substring-no-properties default-bullet))
3283 sans-escapes
3284 t)))
3285 (message "")
3286 (if (string= choice "") default-bullet choice))
3288 ;;;_ > allout-distinctive-bullet (bullet)
3289 (defun allout-distinctive-bullet (bullet)
3290 "True if BULLET is one of those on `allout-distinctive-bullets-string'."
3291 (string-match (regexp-quote bullet) allout-distinctive-bullets-string))
3292 ;;;_ > allout-numbered-type-prefix (&optional prefix)
3293 (defun allout-numbered-type-prefix (&optional prefix)
3294 "True if current header prefix bullet is numbered bullet."
3295 (and allout-numbered-bullet
3296 (string= allout-numbered-bullet
3297 (if prefix
3298 (allout-get-prefix-bullet prefix)
3299 (allout-get-bullet)))))
3300 ;;;_ > allout-encrypted-type-prefix (&optional prefix)
3301 (defun allout-encrypted-type-prefix (&optional prefix)
3302 "True if current header prefix bullet is for an encrypted entry \(body)."
3303 (and allout-topic-encryption-bullet
3304 (string= allout-topic-encryption-bullet
3305 (if prefix
3306 (allout-get-prefix-bullet prefix)
3307 (allout-get-bullet)))))
3308 ;;;_ > allout-bullet-for-depth (&optional depth)
3309 (defun allout-bullet-for-depth (&optional depth)
3310 "Return outline topic bullet suited to optional DEPTH, or current depth."
3311 ;; Find bullet in plain-bullets-string modulo DEPTH.
3312 (if allout-stylish-prefixes
3313 (char-to-string (aref allout-plain-bullets-string
3314 (% (max 0 (- depth 2))
3315 allout-plain-bullets-string-len)))
3316 allout-primary-bullet)
3319 ;;;_ - Topic Production
3320 ;;;_ > allout-make-topic-prefix (&optional prior-bullet
3321 (defun allout-make-topic-prefix (&optional prior-bullet
3323 depth
3324 solicit
3325 number-control
3326 index)
3327 ;; Depth null means use current depth, non-null means we're either
3328 ;; opening a new topic after current topic, lower or higher, or we're
3329 ;; changing level of current topic.
3330 ;; Solicit dominates specified bullet-char.
3331 ;;;_ . Doc string:
3332 "Generate a topic prefix suitable for optional arg DEPTH, or current depth.
3334 All the arguments are optional.
3336 PRIOR-BULLET indicates the bullet of the prefix being changed, or
3337 nil if none. This bullet may be preserved (other options
3338 notwithstanding) if it is on the `allout-distinctive-bullets-string',
3339 for instance.
3341 Second arg NEW indicates that a new topic is being opened after the
3342 topic at point, if non-nil. Default bullet for new topics, eg, may
3343 be set (contingent to other args) to numbered bullets if previous
3344 sibling is one. The implication otherwise is that the current topic
3345 is being adjusted - shifted or rebulleted - and we don't consider
3346 bullet or previous sibling.
3348 Third arg DEPTH forces the topic prefix to that depth, regardless of
3349 the current topics' depth.
3351 If SOLICIT is non-nil, then the choice of bullet is solicited from
3352 user. If it's a character, then that character is offered as the
3353 default, otherwise the one suited to the context \(according to
3354 distinction or depth) is offered. \(This overrides other options,
3355 including, eg, a distinctive PRIOR-BULLET.) If non-nil, then the
3356 context-specific bullet is used.
3358 Fifth arg, NUMBER-CONTROL, matters only if `allout-numbered-bullet'
3359 is non-nil *and* soliciting was not explicitly invoked. Then
3360 NUMBER-CONTROL non-nil forces prefix to either numbered or
3361 denumbered format, depending on the value of the sixth arg, INDEX.
3363 \(Note that NUMBER-CONTROL does *not* apply to level 1 topics. Sorry...)
3365 If NUMBER-CONTROL is non-nil and sixth arg INDEX is non-nil then
3366 the prefix of the topic is forced to be numbered. Non-nil
3367 NUMBER-CONTROL and nil INDEX forces non-numbered format on the
3368 bullet. Non-nil NUMBER-CONTROL and non-nil, non-number INDEX means
3369 that the index for the numbered prefix will be derived, by counting
3370 siblings back to start of level. If INDEX is a number, then that
3371 number is used as the index for the numbered prefix (allowing, eg,
3372 sequential renumbering to not require this function counting back the
3373 index for each successive sibling)."
3374 ;;;_ . Code:
3375 ;; The options are ordered in likely frequence of use, most common
3376 ;; highest, least lowest. Ie, more likely to be doing prefix
3377 ;; adjustments than soliciting, and yet more than numbering.
3378 ;; Current prefix is least dominant, but most likely to be commonly
3379 ;; specified...
3381 (let* (body
3382 numbering
3383 denumbering
3384 (depth (or depth (allout-depth)))
3385 (header-lead allout-header-prefix)
3386 (bullet-char
3388 ;; Getting value for bullet char is practically the whole job:
3390 (cond
3391 ; Simplest situation - level 1:
3392 ((<= depth 1) (setq header-lead "") allout-primary-bullet)
3393 ; Simple, too: all asterisks:
3394 (allout-old-style-prefixes
3395 ;; Cheat - make body the whole thing, null out header-lead and
3396 ;; bullet-char:
3397 (setq body (make-string depth
3398 (string-to-char allout-primary-bullet)))
3399 (setq header-lead "")
3402 ;; (Neither level 1 nor old-style, so we're space padding.
3403 ;; Sneak it in the condition of the next case, whatever it is.)
3405 ;; Solicitation overrides numbering and other cases:
3406 ((progn (setq body (make-string (- depth 2) ?\ ))
3407 ;; The actual condition:
3408 solicit)
3409 (let* ((got (allout-solicit-alternate-bullet depth solicit)))
3410 ;; Gotta check whether we're numbering and got a numbered bullet:
3411 (setq numbering (and allout-numbered-bullet
3412 (not (and number-control (not index)))
3413 (string= got allout-numbered-bullet)))
3414 ;; Now return what we got, regardless:
3415 got))
3417 ;; Numbering invoked through args:
3418 ((and allout-numbered-bullet number-control)
3419 (if (setq numbering (not (setq denumbering (not index))))
3420 allout-numbered-bullet
3421 (if (and prior-bullet
3422 (not (string= allout-numbered-bullet
3423 prior-bullet)))
3424 prior-bullet
3425 (allout-bullet-for-depth depth))))
3427 ;;; Neither soliciting nor controlled numbering ;;;
3428 ;;; (may be controlled denumbering, tho) ;;;
3430 ;; Check wrt previous sibling:
3431 ((and new ; only check for new prefixes
3432 (<= depth (allout-depth))
3433 allout-numbered-bullet ; ... & numbering enabled
3434 (not denumbering)
3435 (let ((sibling-bullet
3436 (save-excursion
3437 ;; Locate correct sibling:
3438 (or (>= depth (allout-depth))
3439 (allout-ascend-to-depth depth))
3440 (allout-get-bullet))))
3441 (if (and sibling-bullet
3442 (string= allout-numbered-bullet sibling-bullet))
3443 (setq numbering sibling-bullet)))))
3445 ;; Distinctive prior bullet?
3446 ((and prior-bullet
3447 (allout-distinctive-bullet prior-bullet)
3448 ;; Either non-numbered:
3449 (or (not (and allout-numbered-bullet
3450 (string= prior-bullet allout-numbered-bullet)))
3451 ;; or numbered, and not denumbering:
3452 (setq numbering (not denumbering)))
3453 ;; Here 'tis:
3454 prior-bullet))
3456 ;; Else, standard bullet per depth:
3457 ((allout-bullet-for-depth depth)))))
3459 (concat header-lead
3460 body
3461 bullet-char
3462 (if numbering
3463 (format "%d" (cond ((and index (numberp index)) index)
3464 (new (1+ (allout-sibling-index depth)))
3465 ((allout-sibling-index))))))
3468 ;;;_ > allout-open-topic (relative-depth &optional before offer-recent-bullet)
3469 (defun allout-open-topic (relative-depth &optional before offer-recent-bullet)
3470 "Open a new topic at depth DEPTH.
3472 New topic is situated after current one, unless optional flag BEFORE
3473 is non-nil, or unless current line is completely empty - lacking even
3474 whitespace - in which case open is done on the current line.
3476 When adding an offspring, it will be added immediately after the parent if
3477 the other offspring are exposed, or after the last child if the offspring
3478 are hidden. \(The intervening offspring will be exposed in the latter
3479 case.)
3481 If OFFER-RECENT-BULLET is true, offer to use the bullet of the prior sibling.
3483 Runs
3485 Nuances:
3487 - Creation of new topics is with respect to the visible topic
3488 containing the cursor, regardless of intervening concealed ones.
3490 - New headers are generally created after/before the body of a
3491 topic. However, they are created right at cursor location if the
3492 cursor is on a blank line, even if that breaks the current topic
3493 body. This is intentional, to provide a simple means for
3494 deliberately dividing topic bodies.
3496 - Double spacing of topic lists is preserved. Also, the first
3497 level two topic is created double-spaced (and so would be
3498 subsequent siblings, if that's left intact). Otherwise,
3499 single-spacing is used.
3501 - Creation of sibling or nested topics is with respect to the topic
3502 you're starting from, even when creating backwards. This way you
3503 can easily create a sibling in front of the current topic without
3504 having to go to its preceding sibling, and then open forward
3505 from there."
3507 (allout-beginning-of-current-line)
3508 (let* ((inhibit-field-text-motion t)
3509 (depth (+ (allout-current-depth) relative-depth))
3510 (opening-on-blank (if (looking-at "^\$")
3511 (not (setq before nil))))
3512 ;; bunch o vars set while computing ref-topic
3513 opening-numbered
3514 ref-depth
3515 ref-bullet
3516 (ref-topic (save-excursion
3517 (cond ((< relative-depth 0)
3518 (allout-ascend-to-depth depth))
3519 ((>= relative-depth 1) nil)
3520 (t (allout-back-to-current-heading)))
3521 (setq ref-depth allout-recent-depth)
3522 (setq ref-bullet
3523 (if (> allout-recent-prefix-end 1)
3524 (allout-recent-bullet)
3525 ""))
3526 (setq opening-numbered
3527 (save-excursion
3528 (and allout-numbered-bullet
3529 (or (<= relative-depth 0)
3530 (allout-descend-to-depth depth))
3531 (if (allout-numbered-type-prefix)
3532 allout-numbered-bullet))))
3533 (point)))
3534 dbl-space
3535 doing-beginning
3536 start end)
3538 (if (not opening-on-blank)
3539 ; Positioning and vertical
3540 ; padding - only if not
3541 ; opening-on-blank:
3542 (progn
3543 (goto-char ref-topic)
3544 (setq dbl-space ; Determine double space action:
3545 (or (and (<= relative-depth 0) ; not descending;
3546 (save-excursion
3547 ;; at b-o-b or preceded by a blank line?
3548 (or (> 0 (forward-line -1))
3549 (looking-at "^\\s-*$")
3550 (bobp)))
3551 (save-excursion
3552 ;; succeeded by a blank line?
3553 (allout-end-of-current-subtree)
3554 (looking-at "\n\n")))
3555 (and (= ref-depth 1)
3556 (or before
3557 (= depth 1)
3558 (save-excursion
3559 ;; Don't already have following
3560 ;; vertical padding:
3561 (not (allout-pre-next-prefix)))))))
3563 ;; Position to prior heading, if inserting backwards, and not
3564 ;; going outwards:
3565 (if (and before (>= relative-depth 0))
3566 (progn (allout-back-to-current-heading)
3567 (setq doing-beginning (bobp))
3568 (if (not (bobp))
3569 (allout-previous-heading)))
3570 (if (and before (bobp))
3571 (open-line 1)))
3573 (if (<= relative-depth 0)
3574 ;; Not going inwards, don't snug up:
3575 (if doing-beginning
3576 (if (not dbl-space)
3577 (open-line 1)
3578 (open-line 2))
3579 (if before
3580 (progn (end-of-line)
3581 (allout-pre-next-prefix)
3582 (while (and (= ?\n (following-char))
3583 (save-excursion
3584 (forward-char 1)
3585 (allout-hidden-p)))
3586 (forward-char 1))
3587 (if (not (looking-at "^$"))
3588 (open-line 1)))
3589 (allout-end-of-current-subtree)
3590 (if (looking-at "\n\n") (forward-char 1))))
3591 ;; Going inwards - double-space if first offspring is
3592 ;; double-spaced, otherwise snug up.
3593 (allout-end-of-entry)
3594 (if (eobp)
3595 (newline 1)
3596 (line-move 1))
3597 (allout-beginning-of-current-line)
3598 (backward-char 1)
3599 (if (bolp)
3600 ;; Blank lines between current header body and next
3601 ;; header - get to last substantive (non-white-space)
3602 ;; line in body:
3603 (progn (setq dbl-space t)
3604 (re-search-backward "[^ \t\n]" nil t)))
3605 (if (looking-at "\n\n")
3606 (setq dbl-space t))
3607 (if (save-excursion
3608 (allout-next-heading)
3609 (when (> allout-recent-depth ref-depth)
3610 ;; This is an offspring.
3611 (forward-line -1)
3612 (looking-at "^\\s-*$")))
3613 (progn (forward-line 1)
3614 (open-line 1)
3615 (forward-line 1)))
3616 (allout-end-of-current-line))
3618 ;;(if doing-beginning (goto-char doing-beginning))
3619 (if (not (bobp))
3620 ;; We insert a newline char rather than using open-line to
3621 ;; avoid rear-stickiness inheritence of read-only property.
3622 (progn (if (and (not (> depth ref-depth))
3623 (not before))
3624 (open-line 1)
3625 (if (and (not dbl-space) (> depth ref-depth))
3626 (newline 1)
3627 (if dbl-space
3628 (open-line 1)
3629 (if (not before)
3630 (newline 1)))))
3631 (if (and dbl-space (not (> relative-depth 0)))
3632 (newline 1))
3633 (if (and (not (eobp))
3634 (or (not (bolp))
3635 (and (not (bobp))
3636 ;; bolp doesnt detect concealed
3637 ;; trailing newlines, compensate:
3638 (save-excursion
3639 (forward-char -1)
3640 (allout-hidden-p)))))
3641 (forward-char 1))))
3643 (setq start (point))
3644 (insert (concat (allout-make-topic-prefix opening-numbered t depth)
3645 " "))
3646 (setq end (1+ (point)))
3648 (allout-rebullet-heading (and offer-recent-bullet ref-bullet)
3649 depth nil nil t)
3650 (if (> relative-depth 0)
3651 (save-excursion (goto-char ref-topic)
3652 (allout-show-children)))
3653 (end-of-line)
3655 (run-hook-with-args 'allout-structure-added-hook start end)
3658 ;;;_ > allout-open-subtopic (arg)
3659 (defun allout-open-subtopic (arg)
3660 "Open new topic header at deeper level than the current one.
3662 Negative universal arg means to open deeper, but place the new topic
3663 prior to the current one."
3664 (interactive "p")
3665 (allout-open-topic 1 (> 0 arg) (< 1 arg)))
3666 ;;;_ > allout-open-sibtopic (arg)
3667 (defun allout-open-sibtopic (arg)
3668 "Open new topic header at same level as the current one.
3670 Positive universal arg means to use the bullet of the prior sibling.
3672 Negative universal arg means to place the new topic prior to the current
3673 one."
3674 (interactive "p")
3675 (allout-open-topic 0 (> 0 arg) (not (= 1 arg))))
3676 ;;;_ > allout-open-supertopic (arg)
3677 (defun allout-open-supertopic (arg)
3678 "Open new topic header at shallower level than the current one.
3680 Negative universal arg means to open shallower, but place the new
3681 topic prior to the current one."
3683 (interactive "p")
3684 (allout-open-topic -1 (> 0 arg) (< 1 arg)))
3686 ;;;_ - Outline Alteration
3687 ;;;_ : Topic Modification
3688 ;;;_ = allout-former-auto-filler
3689 (defvar allout-former-auto-filler nil
3690 "Name of modal fill function being wrapped by `allout-auto-fill'.")
3691 ;;;_ > allout-auto-fill ()
3692 (defun allout-auto-fill ()
3693 "`allout-mode' autofill function.
3695 Maintains outline hanging topic indentation if
3696 `allout-use-hanging-indents' is set."
3698 (when (not allout-inhibit-auto-fill)
3699 (let ((fill-prefix (if allout-use-hanging-indents
3700 ;; Check for topic header indentation:
3701 (save-excursion
3702 (beginning-of-line)
3703 (if (looking-at allout-regexp)
3704 ;; ... construct indentation to account for
3705 ;; length of topic prefix:
3706 (make-string (progn (allout-end-of-prefix)
3707 (current-column))
3708 ?\ )))))
3709 (use-auto-fill-function (or allout-outside-normal-auto-fill-function
3710 auto-fill-function
3711 'do-auto-fill)))
3712 (if (or allout-former-auto-filler allout-use-hanging-indents)
3713 (funcall use-auto-fill-function)))))
3714 ;;;_ > allout-reindent-body (old-depth new-depth &optional number)
3715 (defun allout-reindent-body (old-depth new-depth &optional number)
3716 "Reindent body lines which were indented at OLD-DEPTH to NEW-DEPTH.
3718 Optional arg NUMBER indicates numbering is being added, and it must
3719 be accommodated.
3721 Note that refill of indented paragraphs is not done."
3723 (save-excursion
3724 (allout-end-of-prefix)
3725 (let* ((new-margin (current-column))
3726 excess old-indent-begin old-indent-end
3727 ;; We want the column where the header-prefix text started
3728 ;; *before* the prefix was changed, so we infer it relative
3729 ;; to the new margin and the shift in depth:
3730 (old-margin (+ old-depth (- new-margin new-depth))))
3732 ;; Process lines up to (but excluding) next topic header:
3733 (allout-unprotected
3734 (save-match-data
3735 (while
3736 (and (re-search-forward "\n\\(\\s-*\\)"
3739 ;; Register the indent data, before we reset the
3740 ;; match data with a subsequent `looking-at':
3741 (setq old-indent-begin (match-beginning 1)
3742 old-indent-end (match-end 1))
3743 (not (looking-at allout-regexp)))
3744 (if (> 0 (setq excess (- (- old-indent-end old-indent-begin)
3745 old-margin)))
3746 ;; Text starts left of old margin - don't adjust:
3748 ;; Text was hanging at or right of old left margin -
3749 ;; reindent it, preserving its existing indentation
3750 ;; beyond the old margin:
3751 (delete-region old-indent-begin old-indent-end)
3752 (indent-to (+ new-margin excess (current-column))))))))))
3753 ;;;_ > allout-rebullet-current-heading (arg)
3754 (defun allout-rebullet-current-heading (arg)
3755 "Solicit new bullet for current visible heading."
3756 (interactive "p")
3757 (let ((initial-col (current-column))
3758 (on-bullet (eq (point)(allout-current-bullet-pos)))
3759 from to
3760 (backwards (if (< arg 0)
3761 (setq arg (* arg -1)))))
3762 (while (> arg 0)
3763 (save-excursion (allout-back-to-current-heading)
3764 (allout-end-of-prefix)
3765 (setq from allout-recent-prefix-beginning
3766 to allout-recent-prefix-end)
3767 (allout-rebullet-heading t ;;; solicit
3768 nil ;;; depth
3769 nil ;;; number-control
3770 nil ;;; index
3771 t) ;;; do-successors
3772 (run-hook-with-args 'allout-exposure-change-hook
3773 from to t))
3774 (setq arg (1- arg))
3775 (if (<= arg 0)
3777 (setq initial-col nil) ; Override positioning back to init col
3778 (if (not backwards)
3779 (allout-next-visible-heading 1)
3780 (allout-goto-prefix-doublechecked)
3781 (allout-next-visible-heading -1))))
3782 (message "Done.")
3783 (cond (on-bullet (goto-char (allout-current-bullet-pos)))
3784 (initial-col (move-to-column initial-col)))))
3785 ;;;_ > allout-rebullet-heading (&optional solicit ...)
3786 (defun allout-rebullet-heading (&optional solicit
3787 new-depth
3788 number-control
3789 index
3790 do-successors)
3792 "Adjust bullet of current topic prefix.
3794 All args are optional.
3796 If SOLICIT is non-nil, then the choice of bullet is solicited from
3797 user. If it's a character, then that character is offered as the
3798 default, otherwise the one suited to the context \(according to
3799 distinction or depth) is offered. If non-nil, then the
3800 context-specific bullet is just used.
3802 Second arg DEPTH forces the topic prefix to that depth, regardless
3803 of the topic's current depth.
3805 Third arg NUMBER-CONTROL can force the prefix to or away from
3806 numbered form. It has effect only if `allout-numbered-bullet' is
3807 non-nil and soliciting was not explicitly invoked (via first arg).
3808 Its effect, numbering or denumbering, then depends on the setting
3809 of the forth arg, INDEX.
3811 If NUMBER-CONTROL is non-nil and forth arg INDEX is nil, then the
3812 prefix of the topic is forced to be non-numbered. Null index and
3813 non-nil NUMBER-CONTROL forces denumbering. Non-nil INDEX (and
3814 non-nil NUMBER-CONTROL) forces a numbered-prefix form. If non-nil
3815 INDEX is a number, then that number is used for the numbered
3816 prefix. Non-nil and non-number means that the index for the
3817 numbered prefix will be derived by allout-make-topic-prefix.
3819 Fifth arg DO-SUCCESSORS t means re-resolve count on succeeding
3820 siblings.
3822 Cf vars `allout-stylish-prefixes', `allout-old-style-prefixes',
3823 and `allout-numbered-bullet', which all affect the behavior of
3824 this function."
3826 (let* ((current-depth (allout-depth))
3827 (new-depth (or new-depth current-depth))
3828 (mb allout-recent-prefix-beginning)
3829 (me allout-recent-prefix-end)
3830 (current-bullet (buffer-substring-no-properties (- me 1) me))
3831 (new-prefix (allout-make-topic-prefix current-bullet
3833 new-depth
3834 solicit
3835 number-control
3836 index)))
3838 ;; Is new one is identical to old?
3839 (if (and (= current-depth new-depth)
3840 (string= current-bullet
3841 (substring new-prefix (1- (length new-prefix)))))
3842 ;; Nothing to do:
3845 ;; New prefix probably different from old:
3846 ; get rid of old one:
3847 (allout-unprotected (delete-region mb me))
3848 (goto-char mb)
3849 ; Dispense with number if
3850 ; numbered-bullet prefix:
3851 (if (and allout-numbered-bullet
3852 (string= allout-numbered-bullet current-bullet)
3853 (looking-at "[0-9]+"))
3854 (allout-unprotected
3855 (delete-region (match-beginning 0)(match-end 0))))
3857 ; Put in new prefix:
3858 (allout-unprotected (insert new-prefix))
3860 ;; Reindent the body if elected, margin changed, and not encrypted body:
3861 (if (and allout-reindent-bodies
3862 (not (= new-depth current-depth))
3863 (not (allout-encrypted-topic-p)))
3864 (allout-reindent-body current-depth new-depth))
3866 ;; Recursively rectify successive siblings of orig topic if
3867 ;; caller elected for it:
3868 (if do-successors
3869 (save-excursion
3870 (while (allout-next-sibling new-depth nil)
3871 (setq index
3872 (cond ((numberp index) (1+ index))
3873 ((not number-control) (allout-sibling-index))))
3874 (if (allout-numbered-type-prefix)
3875 (allout-rebullet-heading nil ;;; solicit
3876 new-depth ;;; new-depth
3877 number-control;;; number-control
3878 index ;;; index
3879 nil))))) ;;;(dont!)do-successors
3880 ) ; (if (and (= current-depth new-depth)...))
3881 ) ; let* ((current-depth (allout-depth))...)
3882 ) ; defun
3883 ;;;_ > allout-rebullet-topic (arg)
3884 (defun allout-rebullet-topic (arg &optional sans-offspring)
3885 "Rebullet the visible topic containing point and all contained subtopics.
3887 Descends into invisible as well as visible topics, however.
3889 When optional sans-offspring is non-nil, subtopics are not
3890 shifted. \(Shifting a topic outwards without shifting its
3891 offspring is disallowed, since this would create a \"containment
3892 discontinuity\", where the depth difference between a topic and
3893 its immediate offspring is greater than one.)
3895 With repeat count, shift topic depth by that amount."
3896 (interactive "P")
3897 (let ((start-col (current-column)))
3898 (save-excursion
3899 ;; Normalize arg:
3900 (cond ((null arg) (setq arg 0))
3901 ((listp arg) (setq arg (car arg))))
3902 ;; Fill the user in, in case we're shifting a big topic:
3903 (if (not (zerop arg)) (message "Shifting..."))
3904 (allout-back-to-current-heading)
3905 (if (<= (+ allout-recent-depth arg) 0)
3906 (error "Attempt to shift topic below level 1"))
3907 (allout-rebullet-topic-grunt arg nil nil nil nil sans-offspring)
3908 (if (not (zerop arg)) (message "Shifting... done.")))
3909 (move-to-column (max 0 (+ start-col arg)))))
3910 ;;;_ > allout-rebullet-topic-grunt (&optional relative-depth ...)
3911 (defun allout-rebullet-topic-grunt (&optional relative-depth
3912 starting-depth
3913 starting-point
3914 index
3915 do-successors
3916 sans-offspring)
3917 "Like `allout-rebullet-topic', but on nearest containing topic
3918 \(visible or not).
3920 See `allout-rebullet-heading' for rebulleting behavior.
3922 All arguments are optional.
3924 First arg RELATIVE-DEPTH means to shift the depth of the entire
3925 topic that amount.
3927 Several subsequent args are for internal recursive use by the function
3928 itself: STARTING-DEPTH, STARTING-POINT, and INDEX.
3930 Finally, if optional SANS-OFFSPRING is non-nil then the offspring
3931 are not shifted. \(Shifting a topic outwards without shifting
3932 its offspring is disallowed, since this would create a
3933 \"containment discontinuity\", where the depth difference between
3934 a topic and its immediate offspring is greater than one..)"
3936 ;; XXX the recursion here is peculiar, and in general the routine may
3937 ;; need simplification with refactoring.
3939 (if (and sans-offspring
3940 relative-depth
3941 (< relative-depth 0))
3942 (error (concat "Attempt to shift topic outwards without offspring,"
3943 " would cause containment discontinuity.")))
3945 (let* ((relative-depth (or relative-depth 0))
3946 (new-depth (allout-depth))
3947 (starting-depth (or starting-depth new-depth))
3948 (on-starting-call (null starting-point))
3949 (index (or index
3950 ;; Leave index null on starting call, so rebullet-heading
3951 ;; calculates it at what might be new depth:
3952 (and (or (zerop relative-depth)
3953 (not on-starting-call))
3954 (allout-sibling-index))))
3955 (starting-index index)
3956 (moving-outwards (< 0 relative-depth))
3957 (starting-point (or starting-point (point)))
3958 (local-point (point)))
3960 ;; Sanity check for excessive promotion done only on starting call:
3961 (and on-starting-call
3962 moving-outwards
3963 (> 0 (+ starting-depth relative-depth))
3964 (error "Attempt to shift topic out beyond level 1"))
3966 (cond ((= starting-depth new-depth)
3967 ;; We're at depth to work on this one.
3969 ;; When shifting out we work on the children before working on
3970 ;; the parent to avoid interim `allout-aberrant-container-p'
3971 ;; aberrancy, and vice-versa when shifting in:
3972 (if (>= relative-depth 0)
3973 (allout-rebullet-heading nil
3974 (+ starting-depth relative-depth)
3975 nil ;;; number
3976 index
3977 nil)) ;;; do-successors
3978 (when (not sans-offspring)
3979 ;; ... and work on subsequent ones which are at greater depth:
3980 (setq index 0)
3981 (allout-next-heading)
3982 (while (and (not (eobp))
3983 (< starting-depth (allout-depth)))
3984 (setq index (1+ index))
3985 (allout-rebullet-topic-grunt relative-depth
3986 (1+ starting-depth)
3987 starting-point
3988 index)))
3989 (when (< relative-depth 0)
3990 (save-excursion
3991 (goto-char local-point)
3992 (allout-rebullet-heading nil ;;; solicit
3993 (+ starting-depth relative-depth)
3994 nil ;;; number
3995 starting-index
3996 nil)))) ;;; do-successors
3998 ((< starting-depth new-depth)
3999 ;; Rare case - subtopic more than one level deeper than parent.
4000 ;; Treat this one at an even deeper level:
4001 (allout-rebullet-topic-grunt relative-depth
4002 new-depth
4003 starting-point
4004 index
4005 sans-offspring)))
4007 (if on-starting-call
4008 (progn
4009 ;; Rectify numbering of former siblings of the adjusted topic,
4010 ;; if topic has changed depth
4011 (if (or do-successors
4012 (and (not (zerop relative-depth))
4013 (or (= allout-recent-depth starting-depth)
4014 (= allout-recent-depth (+ starting-depth
4015 relative-depth)))))
4016 (allout-rebullet-heading nil nil nil nil t))
4017 ;; Now rectify numbering of new siblings of the adjusted topic,
4018 ;; if depth has been changed:
4019 (progn (goto-char starting-point)
4020 (if (not (zerop relative-depth))
4021 (allout-rebullet-heading nil nil nil nil t)))))
4024 ;;;_ > allout-renumber-to-depth (&optional depth)
4025 (defun allout-renumber-to-depth (&optional depth)
4026 "Renumber siblings at current depth.
4028 Affects superior topics if optional arg DEPTH is less than current depth.
4030 Returns final depth."
4032 ;; Proceed by level, processing subsequent siblings on each,
4033 ;; ascending until we get shallower than the start depth:
4035 (let ((ascender (allout-depth))
4036 was-eobp)
4037 (while (and (not (eobp))
4038 (allout-depth)
4039 (>= allout-recent-depth depth)
4040 (>= ascender depth))
4041 ; Skip over all topics at
4042 ; lesser depths, which can not
4043 ; have been disturbed:
4044 (while (and (not (setq was-eobp (eobp)))
4045 (> allout-recent-depth ascender))
4046 (allout-next-heading))
4047 ; Prime ascender for ascension:
4048 (setq ascender (1- allout-recent-depth))
4049 (if (>= allout-recent-depth depth)
4050 (allout-rebullet-heading nil ;;; solicit
4051 nil ;;; depth
4052 nil ;;; number-control
4053 nil ;;; index
4054 t)) ;;; do-successors
4055 (if was-eobp (goto-char (point-max)))))
4056 allout-recent-depth)
4057 ;;;_ > allout-number-siblings (&optional denumber)
4058 (defun allout-number-siblings (&optional denumber)
4059 "Assign numbered topic prefix to this topic and its siblings.
4061 With universal argument, denumber - assign default bullet to this
4062 topic and its siblings.
4064 With repeated universal argument (`^U^U'), solicit bullet for each
4065 rebulleting each topic at this level."
4067 (interactive "P")
4069 (save-excursion
4070 (allout-back-to-current-heading)
4071 (allout-beginning-of-level)
4072 (let ((depth allout-recent-depth)
4073 (index (if (not denumber) 1))
4074 (use-bullet (equal '(16) denumber))
4075 (more t))
4076 (while more
4077 (allout-rebullet-heading use-bullet ;;; solicit
4078 depth ;;; depth
4079 t ;;; number-control
4080 index ;;; index
4081 nil) ;;; do-successors
4082 (if index (setq index (1+ index)))
4083 (setq more (allout-next-sibling depth nil))))))
4084 ;;;_ > allout-shift-in (arg)
4085 (defun allout-shift-in (arg)
4086 "Increase depth of current heading and any items collapsed within it.
4088 With a negative argument, the item is shifted out using
4089 `allout-shift-out', instead.
4091 With an argument greater than one, shift-in the item but not its
4092 offspring, making the item into a sibling of its former children,
4093 and a child of sibling that formerly preceeded it.
4095 You are not allowed to shift the first offspring of a topic
4096 inwards, because that would yield a \"containment
4097 discontinuity\", where the depth difference between a topic and
4098 its immediate offspring is greater than one. The first topic in
4099 the file can be adjusted to any positive depth, however."
4101 (interactive "p")
4102 (if (< arg 0)
4103 (allout-shift-out (* arg -1))
4104 ;; refuse to create a containment discontinuity:
4105 (save-excursion
4106 (allout-back-to-current-heading)
4107 (if (not (bobp))
4108 (let* ((current-depth allout-recent-depth)
4109 (start-point (point))
4110 (predecessor-depth (progn
4111 (forward-char -1)
4112 (allout-goto-prefix-doublechecked)
4113 (if (< (point) start-point)
4114 allout-recent-depth
4115 0))))
4116 (if (and (> predecessor-depth 0)
4117 (> (1+ current-depth)
4118 (1+ predecessor-depth)))
4119 (error (concat "Disallowed shift deeper than"
4120 " containing topic's children."))))))
4121 (let ((where (point)))
4122 (allout-rebullet-topic 1 (and (> arg 1) 'sans-offspring))
4123 (run-hook-with-args 'allout-structure-shifted-hook arg where))))
4124 ;;;_ > allout-shift-out (arg)
4125 (defun allout-shift-out (arg)
4126 "Decrease depth of current heading and any topics collapsed within it.
4127 This will make the item a sibling of its former container.
4129 With a negative argument, the item is shifted in using
4130 `allout-shift-in', instead.
4132 With an argument greater than one, shift-out the item's offspring
4133 but not the item itself, making the former children siblings of
4134 the item.
4136 With an argument greater than 1, the item's offspring are shifted
4137 out without shifting the item. This will make the immediate
4138 subtopics into siblings of the item."
4139 (interactive "p")
4140 (if (< arg 0)
4141 (allout-shift-in (* arg -1))
4142 ;; Get proper exposure in this area:
4143 (save-excursion (if (allout-ascend)
4144 (allout-show-children)))
4145 ;; Show collapsed children if there's a successor which will become
4146 ;; their sibling:
4147 (if (and (allout-current-topic-collapsed-p)
4148 (save-excursion (allout-next-sibling)))
4149 (allout-show-children))
4150 (let ((where (and (allout-depth) allout-recent-prefix-beginning)))
4151 (save-excursion
4152 (if (> arg 1)
4153 ;; Shift the offspring but not the topic:
4154 (let ((children-chart (allout-chart-subtree 1)))
4155 (if (listp (car children-chart))
4156 ;; whoops:
4157 (setq children-chart (allout-flatten children-chart)))
4158 (save-excursion
4159 (dolist (child-point children-chart)
4160 (goto-char child-point)
4161 (allout-shift-out 1))))
4162 (allout-rebullet-topic (* arg -1))))
4163 (run-hook-with-args 'allout-structure-shifted-hook (* arg -1) where))))
4164 ;;;_ : Surgery (kill-ring) functions with special provisions for outlines:
4165 ;;;_ > allout-kill-line (&optional arg)
4166 (defun allout-kill-line (&optional arg)
4167 "Kill line, adjusting subsequent lines suitably for outline mode."
4169 (interactive "*P")
4171 (if (or (not (allout-mode-p))
4172 (not (bolp))
4173 (not (looking-at allout-regexp)))
4174 ;; Just do a regular kill:
4175 (kill-line arg)
4176 ;; Ah, have to watch out for adjustments:
4177 (let* ((beg (point))
4179 (beg-hidden (allout-hidden-p))
4180 (end-hidden (save-excursion (allout-end-of-current-line)
4181 (setq end (point))
4182 (allout-hidden-p)))
4183 (depth (allout-depth)))
4185 (allout-annotate-hidden beg end)
4187 (if (and (not beg-hidden) (not end-hidden))
4188 (allout-unprotected (kill-line arg))
4189 (kill-line arg))
4190 (if allout-numbered-bullet
4191 (save-excursion ; Renumber subsequent topics if needed:
4192 (if (not (looking-at allout-regexp))
4193 (allout-next-heading))
4194 (allout-renumber-to-depth depth)))
4195 (run-hook-with-args 'allout-structure-deleted-hook depth (point)))))
4196 ;;;_ > allout-copy-line-as-kill ()
4197 (defun allout-copy-line-as-kill ()
4198 "Like allout-kill-topic, but save to kill ring instead of deleting."
4199 (interactive)
4200 (let ((buffer-read-only t))
4201 (condition-case nil
4202 (allout-kill-line)
4203 (buffer-read-only nil))))
4204 ;;;_ > allout-kill-topic ()
4205 (defun allout-kill-topic ()
4206 "Kill topic together with subtopics.
4208 Trailing whitespace is killed with a topic if that whitespace:
4210 - would separate the topic from a subsequent sibling
4211 - would separate the topic from the end of buffer
4212 - would not be added to whitespace already separating the topic from the
4213 previous one.
4215 Topic exposure is marked with text-properties, to be used by
4216 allout-yank-processing for exposure recovery."
4218 (interactive)
4219 (let* ((inhibit-field-text-motion t)
4220 (beg (prog1 (allout-back-to-current-heading) (beginning-of-line)))
4221 (depth allout-recent-depth))
4222 (allout-end-of-current-subtree)
4223 (if (and (/= (current-column) 0) (not (eobp)))
4224 (forward-char 1))
4225 (if (not (eobp))
4226 (if (and (looking-at "\n")
4227 (or (save-excursion
4228 (or (not (allout-next-heading))
4229 (= depth allout-recent-depth)))
4230 (and (> (- beg (point-min)) 3)
4231 (string= (buffer-substring (- beg 2) beg) "\n\n"))))
4232 (forward-char 1)))
4234 (allout-annotate-hidden beg (point))
4236 (allout-unprotected (kill-region beg (point)))
4237 (save-excursion
4238 (allout-renumber-to-depth depth))
4239 (run-hook-with-args 'allout-structure-deleted-hook depth (point))))
4240 ;;;_ > allout-copy-topic-as-kill ()
4241 (defun allout-copy-topic-as-kill ()
4242 "Like allout-kill-topic, but save to kill ring instead of deleting."
4243 (interactive)
4244 (let ((buffer-read-only t))
4245 (condition-case nil
4246 (allout-kill-topic)
4247 (buffer-read-only (message "Topic copied...")))))
4248 ;;;_ > allout-annotate-hidden (begin end)
4249 (defun allout-annotate-hidden (begin end)
4250 "Qualify text with properties to indicate exposure status."
4252 (let ((was-modified (buffer-modified-p))
4253 (buffer-read-only nil))
4254 (allout-unprotected
4255 (remove-text-properties begin end '(allout-was-hidden t)))
4256 (save-excursion
4257 (goto-char begin)
4258 (let (done next prev overlay)
4259 (while (not done)
4260 ;; at or advance to start of next hidden region:
4261 (if (not (allout-hidden-p))
4262 (setq next
4263 (max (1+ (point))
4264 (next-single-char-property-change (point)
4265 'invisible
4266 nil end))))
4267 (if (or (not next) (eq prev next))
4268 ;; still not at start of hidden area - must not be any left.
4269 (setq done t)
4270 (goto-char next)
4271 (setq prev next)
4272 (if (not (allout-hidden-p))
4273 ;; still not at start of hidden area.
4274 (setq done t)
4275 (setq overlay (allout-get-invisibility-overlay))
4276 (setq next (overlay-end overlay)
4277 prev next)
4278 ;; advance to end of this hidden area:
4279 (when next
4280 (goto-char next)
4281 (allout-unprotected
4282 (put-text-property (overlay-start overlay) next
4283 'allout-was-hidden t))))))))
4284 (set-buffer-modified-p was-modified)))
4285 ;;;_ > allout-hide-by-annotation (begin end)
4286 (defun allout-hide-by-annotation (begin end)
4287 "Translate text properties indicating exposure status into actual exposure."
4288 (save-excursion
4289 (goto-char begin)
4290 (let ((was-modified (buffer-modified-p))
4291 done next prev)
4292 (while (not done)
4293 ;; at or advance to start of next annotation:
4294 (if (not (get-text-property (point) 'allout-was-hidden))
4295 (setq next (next-single-char-property-change (point)
4296 'allout-was-hidden
4297 nil end)))
4298 (if (or (not next) (eq prev next))
4299 ;; no more or not advancing - must not be any left.
4300 (setq done t)
4301 (goto-char next)
4302 (setq prev next)
4303 (if (not (get-text-property (point) 'allout-was-hidden))
4304 ;; still not at start of annotation.
4305 (setq done t)
4306 ;; advance to just after end of this annotation:
4307 (setq next (next-single-char-property-change (point)
4308 'allout-was-hidden
4309 nil end))
4310 (overlay-put (make-overlay prev next)
4311 'category 'allout-exposure-category)
4312 (allout-unprotected
4313 (remove-text-properties prev next '(allout-was-hidden t)))
4314 (setq prev next)
4315 (if next (goto-char next)))))
4316 (set-buffer-modified-p was-modified))))
4317 ;;;_ > allout-remove-exposure-annotation (begin end)
4318 (defun allout-remove-exposure-annotation (begin end)
4319 "Remove text properties indicating exposure status."
4320 (remove-text-properties begin end '(allout-was-hidden t)))
4322 ;;;_ > allout-yank-processing ()
4323 (defun allout-yank-processing (&optional arg)
4325 "Incidental allout-specific business to be done just after text yanks.
4327 Does depth adjustment of yanked topics, when:
4329 1 the stuff being yanked starts with a valid outline header prefix, and
4330 2 it is being yanked at the end of a line which consists of only a valid
4331 topic prefix.
4333 Also, adjusts numbering of subsequent siblings when appropriate.
4335 Depth adjustment alters the depth of all the topics being yanked
4336 the amount it takes to make the first topic have the depth of the
4337 header into which it's being yanked.
4339 The point is left in front of yanked, adjusted topics, rather than
4340 at the end (and vice-versa with the mark). Non-adjusted yanks,
4341 however, are left exactly like normal, non-allout-specific yanks."
4343 (interactive "*P")
4344 ; Get to beginning, leaving
4345 ; region around subject:
4346 (if (< (allout-mark-marker t) (point))
4347 (exchange-point-and-mark))
4348 (allout-unprotected
4349 (let* ((subj-beg (point))
4350 (into-bol (bolp))
4351 (subj-end (allout-mark-marker t))
4352 ;; 'resituate' if yanking an entire topic into topic header:
4353 (resituate (and (allout-e-o-prefix-p)
4354 (looking-at allout-regexp)
4355 (allout-prefix-data)))
4356 ;; `rectify-numbering' if resituating (where several topics may
4357 ;; be resituating) or yanking a topic into a topic slot (bol):
4358 (rectify-numbering (or resituate
4359 (and into-bol (looking-at allout-regexp)))))
4360 (if resituate
4361 ; The yanked stuff is a topic:
4362 (let* ((prefix-len (- (match-end 1) subj-beg))
4363 (subj-depth allout-recent-depth)
4364 (prefix-bullet (allout-recent-bullet))
4365 (adjust-to-depth
4366 ;; Nil if adjustment unnecessary, otherwise depth to which
4367 ;; adjustment should be made:
4368 (save-excursion
4369 (and (goto-char subj-end)
4370 (eolp)
4371 (goto-char subj-beg)
4372 (and (looking-at allout-regexp)
4373 (progn
4374 (beginning-of-line)
4375 (not (= (point) subj-beg)))
4376 (looking-at allout-regexp)
4377 (allout-prefix-data))
4378 allout-recent-depth)))
4379 (more t))
4380 (setq rectify-numbering allout-numbered-bullet)
4381 (if adjust-to-depth
4382 ; Do the adjustment:
4383 (progn
4384 (save-restriction
4385 (narrow-to-region subj-beg subj-end)
4386 ; Trim off excessive blank
4387 ; line at end, if any:
4388 (goto-char (point-max))
4389 (if (looking-at "^$")
4390 (allout-unprotected (delete-char -1)))
4391 ; Work backwards, with each
4392 ; shallowest level,
4393 ; successively excluding the
4394 ; last processed topic from
4395 ; the narrow region:
4396 (while more
4397 (allout-back-to-current-heading)
4398 ; go as high as we can in each bunch:
4399 (while (allout-ascend))
4400 (save-excursion
4401 (allout-rebullet-topic-grunt (- adjust-to-depth
4402 subj-depth))
4403 (allout-depth))
4404 (if (setq more (not (bobp)))
4405 (progn (widen)
4406 (forward-char -1)
4407 (narrow-to-region subj-beg (point))))))
4408 ;; Preserve new bullet if it's a distinctive one, otherwise
4409 ;; use old one:
4410 (if (string-match (regexp-quote prefix-bullet)
4411 allout-distinctive-bullets-string)
4412 ; Delete from bullet of old to
4413 ; before bullet of new:
4414 (progn
4415 (beginning-of-line)
4416 (delete-region (point) subj-beg)
4417 (set-marker (allout-mark-marker t) subj-end)
4418 (goto-char subj-beg)
4419 (allout-end-of-prefix))
4420 ; Delete base subj prefix,
4421 ; leaving old one:
4422 (delete-region (point) (+ (point)
4423 prefix-len
4424 (- adjust-to-depth subj-depth)))
4425 ; and delete residual subj
4426 ; prefix digits and space:
4427 (while (looking-at "[0-9]") (delete-char 1))
4428 (if (looking-at " ") (delete-char 1))))
4429 (exchange-point-and-mark))))
4430 (if rectify-numbering
4431 (progn
4432 (save-excursion
4433 ; Give some preliminary feedback:
4434 (message "... reconciling numbers")
4435 ; ... and renumber, in case necessary:
4436 (goto-char subj-beg)
4437 (if (allout-goto-prefix-doublechecked)
4438 (allout-rebullet-heading nil ;;; solicit
4439 (allout-depth) ;;; depth
4440 nil ;;; number-control
4441 nil ;;; index
4443 (message ""))))
4444 (if (or into-bol resituate)
4445 (allout-hide-by-annotation (point) (allout-mark-marker t))
4446 (allout-remove-exposure-annotation (allout-mark-marker t) (point)))
4447 (if (not resituate)
4448 (exchange-point-and-mark))
4449 (run-hook-with-args 'allout-structure-added-hook subj-beg subj-end))))
4450 ;;;_ > allout-yank (&optional arg)
4451 (defun allout-yank (&optional arg)
4452 "`allout-mode' yank, with depth and numbering adjustment of yanked topics.
4454 Non-topic yanks work no differently than normal yanks.
4456 If a topic is being yanked into a bare topic prefix, the depth of the
4457 yanked topic is adjusted to the depth of the topic prefix.
4459 1 we're yanking in an `allout-mode' buffer
4460 2 the stuff being yanked starts with a valid outline header prefix, and
4461 3 it is being yanked at the end of a line which consists of only a valid
4462 topic prefix.
4464 If these conditions hold then the depth of the yanked topics are all
4465 adjusted the amount it takes to make the first one at the depth of the
4466 header into which it's being yanked.
4468 The point is left in front of yanked, adjusted topics, rather than
4469 at the end (and vice-versa with the mark). Non-adjusted yanks,
4470 however, (ones that don't qualify for adjustment) are handled
4471 exactly like normal yanks.
4473 Numbering of yanked topics, and the successive siblings at the depth
4474 into which they're being yanked, is adjusted.
4476 `allout-yank-pop' works with `allout-yank' just like normal `yank-pop'
4477 works with normal `yank' in non-outline buffers."
4479 (interactive "*P")
4480 (setq this-command 'yank)
4481 (allout-unprotected
4482 (yank arg))
4483 (if (allout-mode-p)
4484 (allout-yank-processing)))
4485 ;;;_ > allout-yank-pop (&optional arg)
4486 (defun allout-yank-pop (&optional arg)
4487 "Yank-pop like `allout-yank' when popping to bare outline prefixes.
4489 Adapts level of popped topics to level of fresh prefix.
4491 Note - prefix changes to distinctive bullets will stick, if followed
4492 by pops to non-distinctive yanks. Bug..."
4494 (interactive "*p")
4495 (setq this-command 'yank)
4496 (yank-pop arg)
4497 (if (allout-mode-p)
4498 (allout-yank-processing)))
4500 ;;;_ - Specialty bullet functions
4501 ;;;_ : File Cross references
4502 ;;;_ > allout-resolve-xref ()
4503 (defun allout-resolve-xref ()
4504 "Pop to file associated with current heading, if it has an xref bullet.
4506 \(Works according to setting of `allout-file-xref-bullet')."
4507 (interactive)
4508 (if (not allout-file-xref-bullet)
4509 (error
4510 "Outline cross references disabled - no `allout-file-xref-bullet'")
4511 (if (not (string= (allout-current-bullet) allout-file-xref-bullet))
4512 (error "Current heading lacks cross-reference bullet `%s'"
4513 allout-file-xref-bullet)
4514 (let ((inhibit-field-text-motion t)
4515 file-name)
4516 (save-excursion
4517 (let* ((text-start allout-recent-prefix-end)
4518 (heading-end (progn (end-of-line) (point))))
4519 (goto-char text-start)
4520 (setq file-name
4521 (if (re-search-forward "\\s-\\(\\S-*\\)" heading-end t)
4522 (buffer-substring (match-beginning 1) (match-end 1))))))
4523 (setq file-name (expand-file-name file-name))
4524 (if (or (file-exists-p file-name)
4525 (if (file-writable-p file-name)
4526 (y-or-n-p (format "%s not there, create one? "
4527 file-name))
4528 (error "%s not found and can't be created" file-name)))
4529 (condition-case failure
4530 (find-file-other-window file-name)
4531 (error failure))
4532 (error "%s not found" file-name))
4538 ;;;_ #6 Exposure Control
4540 ;;;_ - Fundamental
4541 ;;;_ > allout-flag-region (from to flag)
4542 (defun allout-flag-region (from to flag)
4543 "Conceal text between FROM and TO if FLAG is non-nil, else reveal it.
4545 Exposure-change hook `allout-exposure-change-hook' is run with the same
4546 arguments as this function, after the exposure changes are made. \(The old
4547 `allout-view-change-hook' is being deprecated, and eventually will not be
4548 invoked.\)"
4550 ;; We use outline invisibility spec.
4551 (remove-overlays from to 'category 'allout-exposure-category)
4552 (when flag
4553 (let ((o (make-overlay from to)))
4554 (overlay-put o 'category 'allout-exposure-category)
4555 (when (featurep 'xemacs)
4556 (let ((props (symbol-plist 'allout-exposure-category)))
4557 (while props
4558 (overlay-put o (pop props) (pop props)))))))
4559 (run-hooks 'allout-view-change-hook)
4560 (run-hook-with-args 'allout-exposure-change-hook from to flag))
4561 ;;;_ > allout-flag-current-subtree (flag)
4562 (defun allout-flag-current-subtree (flag)
4563 "Conceal currently-visible topic's subtree if FLAG non-nil, else reveal it."
4565 (save-excursion
4566 (allout-back-to-current-heading)
4567 (let ((inhibit-field-text-motion t))
4568 (end-of-line))
4569 (allout-flag-region (point)
4570 ;; Exposing must not leave trailing blanks hidden,
4571 ;; but can leave them exposed when hiding, so we
4572 ;; can use flag's inverse as the
4573 ;; include-trailing-blank cue:
4574 (allout-end-of-current-subtree (not flag))
4575 flag)))
4577 ;;;_ - Topic-specific
4578 ;;;_ > allout-show-entry ()
4579 (defun allout-show-entry ()
4580 "Like `allout-show-current-entry', but reveals entries in hidden topics.
4582 This is a way to give restricted peek at a concealed locality without the
4583 expense of exposing its context, but can leave the outline with aberrant
4584 exposure. `allout-show-offshoot' should be used after the peek to rectify
4585 the exposure."
4587 (interactive)
4588 (save-excursion
4589 (let (beg end)
4590 (allout-goto-prefix-doublechecked)
4591 (setq beg (if (allout-hidden-p) (1- (point)) (point)))
4592 (setq end (allout-pre-next-prefix))
4593 (allout-flag-region beg end nil)
4594 (list beg end))))
4595 ;;;_ > allout-show-children (&optional level strict)
4596 (defun allout-show-children (&optional level strict)
4598 "If point is visible, show all direct subheadings of this heading.
4600 Otherwise, do `allout-show-to-offshoot', and then show subheadings.
4602 Optional LEVEL specifies how many levels below the current level
4603 should be shown, or all levels if t. Default is 1.
4605 Optional STRICT means don't resort to -show-to-offshoot, no matter
4606 what. This is basically so -show-to-offshoot, which is called by
4607 this function, can employ the pure offspring-revealing capabilities of
4610 Returns point at end of subtree that was opened, if any. (May get a
4611 point of non-opened subtree?)"
4613 (interactive "p")
4614 (let ((start-point (point)))
4615 (if (and (not strict)
4616 (allout-hidden-p))
4618 (progn (allout-show-to-offshoot) ; Point's concealed, open to
4619 ; expose it.
4620 ;; Then recurse, but with "strict" set so we don't
4621 ;; infinite regress:
4622 (allout-show-children level t))
4624 (save-excursion
4625 (allout-beginning-of-current-line)
4626 (save-restriction
4627 (let* (depth
4628 ;; translate the level spec for this routine to the ones
4629 ;; used by -chart-subtree and -chart-to-reveal:
4630 (chart-level (cond ((not level) 1)
4631 ((eq level t) nil)
4632 (t level)))
4633 (chart (allout-chart-subtree chart-level))
4634 (to-reveal (or (allout-chart-to-reveal chart chart-level)
4635 ;; interactive, show discontinuous children:
4636 (and chart
4637 (interactive-p)
4638 (save-excursion
4639 (allout-back-to-current-heading)
4640 (setq depth (allout-current-depth))
4641 (and (allout-next-heading)
4642 (> allout-recent-depth
4643 (1+ depth))))
4644 (message
4645 "Discontinuous offspring; use `%s %s'%s."
4646 (substitute-command-keys
4647 "\\[universal-argument]")
4648 (substitute-command-keys
4649 "\\[allout-shift-out]")
4650 " to elevate them.")
4651 (allout-chart-to-reveal
4652 chart (- allout-recent-depth depth))))))
4653 (goto-char start-point)
4654 (when (and strict (allout-hidden-p))
4655 ;; Concealed root would already have been taken care of,
4656 ;; unless strict was set.
4657 (allout-flag-region (point) (allout-snug-back) nil)
4658 (when allout-show-bodies
4659 (goto-char (car to-reveal))
4660 (allout-show-current-entry)))
4661 (while to-reveal
4662 (goto-char (car to-reveal))
4663 (allout-flag-region (save-excursion (allout-snug-back) (point))
4664 (progn (search-forward "\n" nil t)
4665 (1- (point)))
4666 nil)
4667 (when allout-show-bodies
4668 (goto-char (car to-reveal))
4669 (allout-show-current-entry))
4670 (setq to-reveal (cdr to-reveal)))))))
4671 ;; Compensate for `save-excursion's maintenance of point
4672 ;; within invisible text:
4673 (goto-char start-point)))
4674 ;;;_ > allout-show-to-offshoot ()
4675 (defun allout-show-to-offshoot ()
4676 "Like `allout-show-entry', but reveals all concealed ancestors, as well.
4678 Useful for coherently exposing to a random point in a hidden region."
4679 (interactive)
4680 (save-excursion
4681 (let ((inhibit-field-text-motion t)
4682 (orig-pt (point))
4683 (orig-pref (allout-goto-prefix-doublechecked))
4684 (last-at (point))
4685 (bag-it 0))
4686 (while (or (> bag-it 1) (allout-hidden-p))
4687 (while (allout-hidden-p)
4688 (move-beginning-of-line 1)
4689 (if (allout-hidden-p) (forward-char -1)))
4690 (if (= last-at (setq last-at (point)))
4691 ;; Oops, we're not making any progress! Show the current topic
4692 ;; completely, and try one more time here, if we haven't already.
4693 (progn (beginning-of-line)
4694 (allout-show-current-subtree)
4695 (goto-char orig-pt)
4696 (setq bag-it (1+ bag-it))
4697 (if (> bag-it 1)
4698 (error "allout-show-to-offshoot: %s"
4699 "Stumped by aberrant nesting.")))
4700 (if (> bag-it 0) (setq bag-it 0))
4701 (allout-show-children)
4702 (goto-char orig-pref)))
4703 (goto-char orig-pt)))
4704 (if (allout-hidden-p)
4705 (allout-show-entry)))
4706 ;;;_ > allout-hide-current-entry ()
4707 (defun allout-hide-current-entry ()
4708 "Hide the body directly following this heading."
4709 (interactive)
4710 (allout-back-to-current-heading)
4711 (save-excursion
4712 (let ((inhibit-field-text-motion t))
4713 (end-of-line))
4714 (allout-flag-region (point)
4715 (progn (allout-end-of-entry) (point))
4716 t)))
4717 ;;;_ > allout-show-current-entry (&optional arg)
4718 (defun allout-show-current-entry (&optional arg)
4719 "Show body following current heading, or hide entry with universal argument."
4721 (interactive "P")
4722 (if arg
4723 (allout-hide-current-entry)
4724 (save-excursion (allout-show-to-offshoot))
4725 (save-excursion
4726 (allout-flag-region (point)
4727 (progn (allout-end-of-entry t) (point))
4728 nil)
4730 ;;;_ > allout-show-current-subtree (&optional arg)
4731 (defun allout-show-current-subtree (&optional arg)
4732 "Show everything within the current topic. With a repeat-count,
4733 expose this topic and its siblings."
4734 (interactive "P")
4735 (save-excursion
4736 (if (<= (allout-current-depth) 0)
4737 ;; Outside any topics - try to get to the first:
4738 (if (not (allout-next-heading))
4739 (error "No topics")
4740 ;; got to first, outermost topic - set to expose it and siblings:
4741 (message "Above outermost topic - exposing all.")
4742 (allout-flag-region (point-min)(point-max) nil))
4743 (allout-beginning-of-current-line)
4744 (if (not arg)
4745 (allout-flag-current-subtree nil)
4746 (allout-beginning-of-level)
4747 (allout-expose-topic '(* :))))))
4748 ;;;_ > allout-current-topic-collapsed-p (&optional include-single-liners)
4749 (defun allout-current-topic-collapsed-p (&optional include-single-liners)
4750 "True if the currently visible containing topic is already collapsed.
4752 Single line topics intrinsically can be considered as being both
4753 collapsed and uncollapsed. If optional INCLUDE-SINGLE-LINERS is
4754 true, then single-line topics are considered to be collapsed. By
4755 default, they are treated as being uncollapsed."
4756 (save-excursion
4757 (and
4758 ;; Is the topic all on one line (allowing for trailing blank line)?
4759 (>= (progn (allout-back-to-current-heading)
4760 (move-end-of-line 1)
4761 (point))
4762 (allout-end-of-current-subtree (not (looking-at "\n\n"))))
4764 (or include-single-liners
4765 (progn (backward-char 1) (allout-hidden-p))))))
4766 ;;;_ > allout-hide-current-subtree (&optional just-close)
4767 (defun allout-hide-current-subtree (&optional just-close)
4768 "Close the current topic, or containing topic if this one is already closed.
4770 If this topic is closed and it's a top level topic, close this topic
4771 and its siblings.
4773 If optional arg JUST-CLOSE is non-nil, do not close the parent or
4774 siblings, even if the target topic is already closed."
4776 (interactive)
4777 (let* ((from (point))
4778 (sibs-msg "Top-level topic already closed - closing siblings...")
4779 (current-exposed (not (allout-current-topic-collapsed-p t))))
4780 (cond (current-exposed (allout-flag-current-subtree t))
4781 (just-close nil)
4782 ((allout-ascend) (allout-hide-current-subtree))
4783 (t (goto-char 0)
4784 (message sibs-msg)
4785 (allout-goto-prefix-doublechecked)
4786 (allout-expose-topic '(0 :))
4787 (message (concat sibs-msg " Done."))))
4788 (goto-char from)))
4789 ;;;_ > allout-show-current-branches ()
4790 (defun allout-show-current-branches ()
4791 "Show all subheadings of this heading, but not their bodies."
4792 (interactive)
4793 (let ((inhibit-field-text-motion t))
4794 (beginning-of-line))
4795 (allout-show-children t))
4796 ;;;_ > allout-hide-current-leaves ()
4797 (defun allout-hide-current-leaves ()
4798 "Hide the bodies of the current topic and all its offspring."
4799 (interactive)
4800 (allout-back-to-current-heading)
4801 (allout-hide-region-body (point) (progn (allout-end-of-current-subtree)
4802 (point))))
4804 ;;;_ - Region and beyond
4805 ;;;_ > allout-show-all ()
4806 (defun allout-show-all ()
4807 "Show all of the text in the buffer."
4808 (interactive)
4809 (message "Exposing entire buffer...")
4810 (allout-flag-region (point-min) (point-max) nil)
4811 (message "Exposing entire buffer... Done."))
4812 ;;;_ > allout-hide-bodies ()
4813 (defun allout-hide-bodies ()
4814 "Hide all of buffer except headings."
4815 (interactive)
4816 (allout-hide-region-body (point-min) (point-max)))
4817 ;;;_ > allout-hide-region-body (start end)
4818 (defun allout-hide-region-body (start end)
4819 "Hide all body lines in the region, but not headings."
4820 (save-excursion
4821 (save-restriction
4822 (narrow-to-region start end)
4823 (goto-char (point-min))
4824 (let ((inhibit-field-text-motion t))
4825 (while (not (eobp))
4826 (end-of-line)
4827 (allout-flag-region (point) (allout-end-of-entry) t)
4828 (if (not (eobp))
4829 (forward-char
4830 (if (looking-at "\n\n")
4831 2 1))))))))
4833 ;;;_ > allout-expose-topic (spec)
4834 (defun allout-expose-topic (spec)
4835 "Apply exposure specs to successive outline topic items.
4837 Use the more convenient frontend, `allout-new-exposure', if you don't
4838 need evaluation of the arguments, or even better, the `allout-layout'
4839 variable-keyed mode-activation/auto-exposure feature of allout outline
4840 mode. See the respective documentation strings for more details.
4842 Cursor is left at start position.
4844 SPEC is either a number or a list.
4846 Successive specs on a list are applied to successive sibling topics.
4848 A simple spec \(either a number, one of a few symbols, or the null
4849 list) dictates the exposure for the corresponding topic.
4851 Non-null lists recursively designate exposure specs for respective
4852 subtopics of the current topic.
4854 The `:' repeat spec is used to specify exposure for any number of
4855 successive siblings, up to the trailing ones for which there are
4856 explicit specs following the `:'.
4858 Simple (numeric and null-list) specs are interpreted as follows:
4860 Numbers indicate the relative depth to open the corresponding topic.
4861 - negative numbers force the topic to be closed before opening to the
4862 absolute value of the number, so all siblings are open only to
4863 that level.
4864 - positive numbers open to the relative depth indicated by the
4865 number, but do not force already opened subtopics to be closed.
4866 - 0 means to close topic - hide all offspring.
4867 : - `repeat'
4868 apply prior element to all siblings at current level, *up to*
4869 those siblings that would be covered by specs following the `:'
4870 on the list. Ie, apply to all topics at level but the last
4871 ones. \(Only first of multiple colons at same level is
4872 respected - subsequent ones are discarded.)
4873 * - completely opens the topic, including bodies.
4874 + - shows all the sub headers, but not the bodies
4875 - - exposes the body of the corresponding topic.
4877 Examples:
4878 \(allout-expose-topic '(-1 : 0))
4879 Close this and all following topics at current level, exposing
4880 only their immediate children, but close down the last topic
4881 at this current level completely.
4882 \(allout-expose-topic '(-1 () : 1 0))
4883 Close current topic so only the immediate subtopics are shown;
4884 show the children in the second to last topic, and completely
4885 close the last one.
4886 \(allout-expose-topic '(-2 : -1 *))
4887 Expose children and grandchildren of all topics at current
4888 level except the last two; expose children of the second to
4889 last and completely open the last one."
4891 (interactive "xExposure spec: ")
4892 (if (not (listp spec))
4894 (let ((depth (allout-depth))
4895 (max-pos 0)
4896 prev-elem curr-elem
4897 stay)
4898 (while spec
4899 (setq prev-elem curr-elem
4900 curr-elem (car spec)
4901 spec (cdr spec))
4902 (cond ; Do current element:
4903 ((null curr-elem) nil)
4904 ((symbolp curr-elem)
4905 (cond ((eq curr-elem '*) (allout-show-current-subtree)
4906 (if (> allout-recent-end-of-subtree max-pos)
4907 (setq max-pos allout-recent-end-of-subtree)))
4908 ((eq curr-elem '+)
4909 (if (not (allout-hidden-p))
4910 (save-excursion (allout-hide-current-subtree t)))
4911 (allout-show-current-branches)
4912 (if (> allout-recent-end-of-subtree max-pos)
4913 (setq max-pos allout-recent-end-of-subtree)))
4914 ((eq curr-elem '-) (allout-show-current-entry))
4915 ((eq curr-elem ':)
4916 (setq stay t)
4917 ;; Expand the `repeat' spec to an explicit version,
4918 ;; w.r.t. remaining siblings:
4919 (let ((residue ; = # of sibs not covered by remaining spec
4920 ;; Dang - could be nice to make use of the chart, sigh:
4921 (- (length (allout-chart-siblings))
4922 (length spec))))
4923 (if (< 0 residue)
4924 ;; Some residue - cover it with prev-elem:
4925 (setq spec (append (make-list residue prev-elem)
4926 spec)))))))
4927 ((numberp curr-elem)
4928 (if (and (>= 0 curr-elem) (not (allout-hidden-p)))
4929 (save-excursion (allout-hide-current-subtree t)
4930 (if (> 0 curr-elem)
4932 (if (> allout-recent-end-of-subtree max-pos)
4933 (setq max-pos
4934 allout-recent-end-of-subtree)))))
4935 (if (> (abs curr-elem) 0)
4936 (progn (allout-show-children (abs curr-elem))
4937 (if (> allout-recent-end-of-subtree max-pos)
4938 (setq max-pos allout-recent-end-of-subtree)))))
4939 ((listp curr-elem)
4940 (if (allout-descend-to-depth (1+ depth))
4941 (let ((got (allout-expose-topic curr-elem)))
4942 (if (and got (> got max-pos)) (setq max-pos got))))))
4943 (cond (stay (setq stay nil))
4944 ((listp (car spec)) nil)
4945 ((> max-pos (point))
4946 ;; Capitalize on max-pos state to get us nearer next sibling:
4947 (progn (goto-char (min (point-max) max-pos))
4948 (allout-next-heading)))
4949 ((allout-next-sibling depth))))
4950 max-pos)))
4951 ;;;_ > allout-old-expose-topic (spec &rest followers)
4952 (defun allout-old-expose-topic (spec &rest followers)
4954 "Deprecated. Use `allout-expose-topic' \(with different schema
4955 format) instead.
4957 Dictate wholesale exposure scheme for current topic, according to SPEC.
4959 SPEC is either a number or a list. Optional successive args
4960 dictate exposure for subsequent siblings of current topic.
4962 A simple spec (either a number, a special symbol, or the null list)
4963 dictates the overall exposure for a topic. Non null lists are
4964 composite specs whose first element dictates the overall exposure for
4965 a topic, with the subsequent elements in the list interpreted as specs
4966 that dictate the exposure for the successive offspring of the topic.
4968 Simple (numeric and null-list) specs are interpreted as follows:
4970 - Numbers indicate the relative depth to open the corresponding topic:
4971 - negative numbers force the topic to be close before opening to the
4972 absolute value of the number.
4973 - positive numbers just open to the relative depth indicated by the number.
4974 - 0 just closes
4975 - `*' completely opens the topic, including bodies.
4976 - `+' shows all the sub headers, but not the bodies
4977 - `-' exposes the body and immediate offspring of the corresponding topic.
4979 If the spec is a list, the first element must be a number, which
4980 dictates the exposure depth of the topic as a whole. Subsequent
4981 elements of the list are nested SPECs, dictating the specific exposure
4982 for the corresponding offspring of the topic.
4984 Optional FOLLOWERS arguments dictate exposure for succeeding siblings."
4986 (interactive "xExposure spec: ")
4987 (let ((inhibit-field-text-motion t)
4988 (depth (allout-current-depth))
4989 max-pos)
4990 (cond ((null spec) nil)
4991 ((symbolp spec)
4992 (if (eq spec '*) (allout-show-current-subtree))
4993 (if (eq spec '+) (allout-show-current-branches))
4994 (if (eq spec '-) (allout-show-current-entry)))
4995 ((numberp spec)
4996 (if (>= 0 spec)
4997 (save-excursion (allout-hide-current-subtree t)
4998 (end-of-line)
4999 (if (or (not max-pos)
5000 (> (point) max-pos))
5001 (setq max-pos (point)))
5002 (if (> 0 spec)
5003 (setq spec (* -1 spec)))))
5004 (if (> spec 0)
5005 (allout-show-children spec)))
5006 ((listp spec)
5007 ;(let ((got (allout-old-expose-topic (car spec))))
5008 ; (if (and got (or (not max-pos) (> got max-pos)))
5009 ; (setq max-pos got)))
5010 (let ((new-depth (+ (allout-current-depth) 1))
5011 got)
5012 (setq max-pos (allout-old-expose-topic (car spec)))
5013 (setq spec (cdr spec))
5014 (if (and spec
5015 (allout-descend-to-depth new-depth)
5016 (not (allout-hidden-p)))
5017 (progn (setq got (apply 'allout-old-expose-topic spec))
5018 (if (and got (or (not max-pos) (> got max-pos)))
5019 (setq max-pos got)))))))
5020 (while (and followers
5021 (progn (if (and max-pos (< (point) max-pos))
5022 (progn (goto-char max-pos)
5023 (setq max-pos nil)))
5024 (end-of-line)
5025 (allout-next-sibling depth)))
5026 (allout-old-expose-topic (car followers))
5027 (setq followers (cdr followers)))
5028 max-pos))
5029 ;;;_ > allout-new-exposure '()
5030 (defmacro allout-new-exposure (&rest spec)
5031 "Literal frontend for `allout-expose-topic', doesn't evaluate arguments.
5032 Some arguments that would need to be quoted in `allout-expose-topic'
5033 need not be quoted in `allout-new-exposure'.
5035 Cursor is left at start position.
5037 Use this instead of obsolete `allout-exposure'.
5039 Examples:
5040 \(allout-new-exposure (-1 () () () 1) 0)
5041 Close current topic at current level so only the immediate
5042 subtopics are shown, except also show the children of the
5043 third subtopic; and close the next topic at the current level.
5044 \(allout-new-exposure : -1 0)
5045 Close all topics at current level to expose only their
5046 immediate children, except for the last topic at the current
5047 level, in which even its immediate children are hidden.
5048 \(allout-new-exposure -2 : -1 *)
5049 Expose children and grandchildren of first topic at current
5050 level, and expose children of subsequent topics at current
5051 level *except* for the last, which should be opened completely."
5052 (list 'save-excursion
5053 '(if (not (or (allout-goto-prefix-doublechecked)
5054 (allout-next-heading)))
5055 (error "allout-new-exposure: Can't find any outline topics"))
5056 (list 'allout-expose-topic (list 'quote spec))))
5058 ;;;_ #7 Systematic outline presentation - copying, printing, flattening
5060 ;;;_ - Mapping and processing of topics
5061 ;;;_ ( See also Subtree Charting, in Navigation code.)
5062 ;;;_ > allout-stringify-flat-index (flat-index)
5063 (defun allout-stringify-flat-index (flat-index &optional context)
5064 "Convert list representing section/subsection/... to document string.
5066 Optional arg CONTEXT indicates interior levels to include."
5067 (let ((delim ".")
5068 result
5069 numstr
5070 (context-depth (or (and context 2) 1)))
5071 ;; Take care of the explicit context:
5072 (while (> context-depth 0)
5073 (setq numstr (int-to-string (car flat-index))
5074 flat-index (cdr flat-index)
5075 result (if flat-index
5076 (cons delim (cons numstr result))
5077 (cons numstr result))
5078 context-depth (if flat-index (1- context-depth) 0)))
5079 (setq delim " ")
5080 ;; Take care of the indentation:
5081 (if flat-index
5082 (progn
5083 (while flat-index
5084 (setq result
5085 (cons delim
5086 (cons (make-string
5087 (1+ (truncate (if (zerop (car flat-index))
5089 (log10 (car flat-index)))))
5091 result)))
5092 (setq flat-index (cdr flat-index)))
5093 ;; Dispose of single extra delim:
5094 (setq result (cdr result))))
5095 (apply 'concat result)))
5096 ;;;_ > allout-stringify-flat-index-plain (flat-index)
5097 (defun allout-stringify-flat-index-plain (flat-index)
5098 "Convert list representing section/subsection/... to document string."
5099 (let ((delim ".")
5100 result)
5101 (while flat-index
5102 (setq result (cons (int-to-string (car flat-index))
5103 (if result
5104 (cons delim result))))
5105 (setq flat-index (cdr flat-index)))
5106 (apply 'concat result)))
5107 ;;;_ > allout-stringify-flat-index-indented (flat-index)
5108 (defun allout-stringify-flat-index-indented (flat-index)
5109 "Convert list representing section/subsection/... to document string."
5110 (let ((delim ".")
5111 result
5112 numstr)
5113 ;; Take care of the explicit context:
5114 (setq numstr (int-to-string (car flat-index))
5115 flat-index (cdr flat-index)
5116 result (if flat-index
5117 (cons delim (cons numstr result))
5118 (cons numstr result)))
5119 (setq delim " ")
5120 ;; Take care of the indentation:
5121 (if flat-index
5122 (progn
5123 (while flat-index
5124 (setq result
5125 (cons delim
5126 (cons (make-string
5127 (1+ (truncate (if (zerop (car flat-index))
5129 (log10 (car flat-index)))))
5131 result)))
5132 (setq flat-index (cdr flat-index)))
5133 ;; Dispose of single extra delim:
5134 (setq result (cdr result))))
5135 (apply 'concat result)))
5136 ;;;_ > allout-listify-exposed (&optional start end format)
5137 (defun allout-listify-exposed (&optional start end format)
5139 "Produce a list representing exposed topics in current region.
5141 This list can then be used by `allout-process-exposed' to manipulate
5142 the subject region.
5144 Optional START and END indicate bounds of region.
5146 optional arg, FORMAT, designates an alternate presentation form for
5147 the prefix:
5149 list - Present prefix as numeric section.subsection..., starting with
5150 section indicated by the list, innermost nesting first.
5151 `indent' \(symbol) - Convert header prefixes to all white space,
5152 except for distinctive bullets.
5154 The elements of the list produced are lists that represents a topic
5155 header and body. The elements of that list are:
5157 - a number representing the depth of the topic,
5158 - a string representing the header-prefix, including trailing whitespace and
5159 bullet.
5160 - a string representing the bullet character,
5161 - and a series of strings, each containing one line of the exposed
5162 portion of the topic entry."
5164 (interactive "r")
5165 (save-excursion
5166 (let*
5167 ((inhibit-field-text-motion t)
5168 ;; state vars:
5169 strings prefix result depth new-depth out gone-out bullet beg
5170 next done)
5172 (goto-char start)
5173 (beginning-of-line)
5174 ;; Goto initial topic, and register preceeding stuff, if any:
5175 (if (> (allout-goto-prefix-doublechecked) start)
5176 ;; First topic follows beginning point - register preliminary stuff:
5177 (setq result (list (list 0 "" nil
5178 (buffer-substring start (1- (point)))))))
5179 (while (and (not done)
5180 (not (eobp)) ; Loop until we've covered the region.
5181 (not (> (point) end)))
5182 (setq depth allout-recent-depth ; Current topics depth,
5183 bullet (allout-recent-bullet) ; ... bullet,
5184 prefix (allout-recent-prefix)
5185 beg (progn (allout-end-of-prefix t) (point))) ; and beginning.
5186 (setq done ; The boundary for the current topic:
5187 (not (allout-next-visible-heading 1)))
5188 (setq new-depth allout-recent-depth)
5189 (setq gone-out out
5190 out (< new-depth depth))
5191 (beginning-of-line)
5192 (setq next (point))
5193 (goto-char beg)
5194 (setq strings nil)
5195 (while (> next (point)) ; Get all the exposed text in
5196 (setq strings
5197 (cons (buffer-substring
5199 ;To hidden text or end of line:
5200 (progn
5201 (end-of-line)
5202 (allout-back-to-visible-text)))
5203 strings))
5204 (when (< (point) next) ; Resume from after hid text, if any.
5205 (line-move 1)
5206 (beginning-of-line))
5207 (setq beg (point)))
5208 ;; Accumulate list for this topic:
5209 (setq strings (nreverse strings))
5210 (setq result
5211 (cons
5212 (if format
5213 (let ((special (if (string-match
5214 (regexp-quote bullet)
5215 allout-distinctive-bullets-string)
5216 bullet)))
5217 (cond ((listp format)
5218 (list depth
5219 (if allout-abbreviate-flattened-numbering
5220 (allout-stringify-flat-index format
5221 gone-out)
5222 (allout-stringify-flat-index-plain
5223 format))
5224 strings
5225 special))
5226 ((eq format 'indent)
5227 (if special
5228 (list depth
5229 (concat (make-string (1+ depth) ? )
5230 (substring prefix -1))
5231 strings)
5232 (list depth
5233 (make-string depth ? )
5234 strings)))
5235 (t (error "allout-listify-exposed: %s %s"
5236 "invalid format" format))))
5237 (list depth prefix strings))
5238 result))
5239 ;; Reasses format, if any:
5240 (if (and format (listp format))
5241 (cond ((= new-depth depth)
5242 (setq format (cons (1+ (car format))
5243 (cdr format))))
5244 ((> new-depth depth) ; descending - assume by 1:
5245 (setq format (cons 1 format)))
5247 ; Pop the residue:
5248 (while (< new-depth depth)
5249 (setq format (cdr format))
5250 (setq depth (1- depth)))
5251 ; And increment the current one:
5252 (setq format
5253 (cons (1+ (or (car format)
5254 -1))
5255 (cdr format)))))))
5256 ;; Put the list with first at front, to last at back:
5257 (nreverse result))))
5258 ;;;_ > my-region-active-p ()
5259 (defmacro my-region-active-p ()
5260 (if (fboundp 'region-active-p)
5261 '(region-active-p)
5262 'mark-active))
5263 ;;;_ > allout-process-exposed (&optional func from to frombuf
5264 ;;; tobuf format)
5265 (defun allout-process-exposed (&optional func from to frombuf tobuf
5266 format start-num)
5267 "Map function on exposed parts of current topic; results to another buffer.
5269 All args are options; default values itemized below.
5271 Apply FUNCTION to exposed portions FROM position TO position in buffer
5272 FROMBUF to buffer TOBUF. Sixth optional arg, FORMAT, designates an
5273 alternate presentation form:
5275 `flat' - Present prefix as numeric section.subsection..., starting with
5276 section indicated by the start-num, innermost nesting first.
5277 X`flat-indented' - Prefix is like `flat' for first topic at each
5278 X level, but subsequent topics have only leaf topic
5279 X number, padded with blanks to line up with first.
5280 `indent' \(symbol) - Convert header prefixes to all white space,
5281 except for distinctive bullets.
5283 Defaults:
5284 FUNCTION: `allout-insert-listified'
5285 FROM: region start, if region active, else start of buffer
5286 TO: region end, if region active, else end of buffer
5287 FROMBUF: current buffer
5288 TOBUF: buffer name derived: \"*current-buffer-name exposed*\"
5289 FORMAT: nil"
5291 ; Resolve arguments,
5292 ; defaulting if necessary:
5293 (if (not func) (setq func 'allout-insert-listified))
5294 (if (not (and from to))
5295 (if (my-region-active-p)
5296 (setq from (region-beginning) to (region-end))
5297 (setq from (point-min) to (point-max))))
5298 (if frombuf
5299 (if (not (bufferp frombuf))
5300 ;; Specified but not a buffer - get it:
5301 (let ((got (get-buffer frombuf)))
5302 (if (not got)
5303 (error (concat "allout-process-exposed: source buffer "
5304 frombuf
5305 " not found."))
5306 (setq frombuf got))))
5307 ;; not specified - default it:
5308 (setq frombuf (current-buffer)))
5309 (if tobuf
5310 (if (not (bufferp tobuf))
5311 (setq tobuf (get-buffer-create tobuf)))
5312 ;; not specified - default it:
5313 (setq tobuf (concat "*" (buffer-name frombuf) " exposed*")))
5314 (if (listp format)
5315 (nreverse format))
5317 (let* ((listified
5318 (progn (set-buffer frombuf)
5319 (allout-listify-exposed from to format))))
5320 (set-buffer tobuf)
5321 (mapcar func listified)
5322 (pop-to-buffer tobuf)))
5324 ;;;_ - Copy exposed
5325 ;;;_ > allout-insert-listified (listified)
5326 (defun allout-insert-listified (listified)
5327 "Insert contents of listified outline portion in current buffer.
5329 LISTIFIED is a list representing each topic header and body:
5331 \`(depth prefix text)'
5333 or \`(depth prefix text bullet-plus)'
5335 If `bullet-plus' is specified, it is inserted just after the entire prefix."
5336 (setq listified (cdr listified))
5337 (let ((prefix (prog1
5338 (car listified)
5339 (setq listified (cdr listified))))
5340 (text (prog1
5341 (car listified)
5342 (setq listified (cdr listified))))
5343 (bullet-plus (car listified)))
5344 (insert prefix)
5345 (if bullet-plus (insert (concat " " bullet-plus)))
5346 (while text
5347 (insert (car text))
5348 (if (setq text (cdr text))
5349 (insert "\n")))
5350 (insert "\n")))
5351 ;;;_ > allout-copy-exposed-to-buffer (&optional arg tobuf format)
5352 (defun allout-copy-exposed-to-buffer (&optional arg tobuf format)
5353 "Duplicate exposed portions of current outline to another buffer.
5355 Other buffer has current buffers name with \" exposed\" appended to it.
5357 With repeat count, copy the exposed parts of only the current topic.
5359 Optional second arg TOBUF is target buffer name.
5361 Optional third arg FORMAT, if non-nil, symbolically designates an
5362 alternate presentation format for the outline:
5364 `flat' - Convert topic header prefixes to numeric
5365 section.subsection... identifiers.
5366 `indent' - Convert header prefixes to all white space, except for
5367 distinctive bullets.
5368 `indent-flat' - The best of both - only the first of each level has
5369 the full path, the rest have only the section number
5370 of the leaf, preceded by the right amount of indentation."
5372 (interactive "P")
5373 (if (not tobuf)
5374 (setq tobuf (get-buffer-create (concat "*" (buffer-name) " exposed*"))))
5375 (let* ((start-pt (point))
5376 (beg (if arg (allout-back-to-current-heading) (point-min)))
5377 (end (if arg (allout-end-of-current-subtree) (point-max)))
5378 (buf (current-buffer))
5379 (start-list ()))
5380 (if (eq format 'flat)
5381 (setq format (if arg (save-excursion
5382 (goto-char beg)
5383 (allout-topic-flat-index))
5384 '(1))))
5385 (save-excursion (set-buffer tobuf)(erase-buffer))
5386 (allout-process-exposed 'allout-insert-listified
5389 (current-buffer)
5390 tobuf
5391 format start-list)
5392 (goto-char (point-min))
5393 (pop-to-buffer buf)
5394 (goto-char start-pt)))
5395 ;;;_ > allout-flatten-exposed-to-buffer (&optional arg tobuf)
5396 (defun allout-flatten-exposed-to-buffer (&optional arg tobuf)
5397 "Present numeric outline of outline's exposed portions in another buffer.
5399 The resulting outline is not compatible with outline mode - use
5400 `allout-copy-exposed-to-buffer' if you want that.
5402 Use `allout-indented-exposed-to-buffer' for indented presentation.
5404 With repeat count, copy the exposed portions of only current topic.
5406 Other buffer has current buffer's name with \" exposed\" appended to
5407 it, unless optional second arg TOBUF is specified, in which case it is
5408 used verbatim."
5409 (interactive "P")
5410 (allout-copy-exposed-to-buffer arg tobuf 'flat))
5411 ;;;_ > allout-indented-exposed-to-buffer (&optional arg tobuf)
5412 (defun allout-indented-exposed-to-buffer (&optional arg tobuf)
5413 "Present indented outline of outline's exposed portions in another buffer.
5415 The resulting outline is not compatible with outline mode - use
5416 `allout-copy-exposed-to-buffer' if you want that.
5418 Use `allout-flatten-exposed-to-buffer' for numeric sectional presentation.
5420 With repeat count, copy the exposed portions of only current topic.
5422 Other buffer has current buffer's name with \" exposed\" appended to
5423 it, unless optional second arg TOBUF is specified, in which case it is
5424 used verbatim."
5425 (interactive "P")
5426 (allout-copy-exposed-to-buffer arg tobuf 'indent))
5428 ;;;_ - LaTeX formatting
5429 ;;;_ > allout-latex-verb-quote (string &optional flow)
5430 (defun allout-latex-verb-quote (string &optional flow)
5431 "Return copy of STRING for literal reproduction across LaTeX processing.
5432 Expresses the original characters \(including carriage returns) of the
5433 string across LaTeX processing."
5434 (mapconcat (function
5435 (lambda (char)
5436 (cond ((memq char '(?\\ ?$ ?% ?# ?& ?{ ?} ?_ ?^ ?- ?*))
5437 (concat "\\char" (number-to-string char) "{}"))
5438 ((= char ?\n) "\\\\")
5439 (t (char-to-string char)))))
5440 string
5441 ""))
5442 ;;;_ > allout-latex-verbatim-quote-curr-line ()
5443 (defun allout-latex-verbatim-quote-curr-line ()
5444 "Express line for exact \(literal) representation across LaTeX processing.
5446 Adjust line contents so it is unaltered \(from the original line)
5447 across LaTeX processing, within the context of a `verbatim'
5448 environment. Leaves point at the end of the line."
5449 (let ((inhibit-field-text-motion t))
5450 (beginning-of-line)
5451 (let ((beg (point))
5452 (end (progn (end-of-line)(point))))
5453 (goto-char beg)
5454 (while (re-search-forward "\\\\"
5455 ;;"\\\\\\|\\{\\|\\}\\|\\_\\|\\$\\|\\\"\\|\\&\\|\\^\\|\\-\\|\\*\\|#"
5456 end ; bounded by end-of-line
5457 1) ; no matches, move to end & return nil
5458 (goto-char (match-beginning 2))
5459 (insert "\\")
5460 (setq end (1+ end))
5461 (goto-char (1+ (match-end 2)))))))
5462 ;;;_ > allout-insert-latex-header (buffer)
5463 (defun allout-insert-latex-header (buffer)
5464 "Insert initial LaTeX commands at point in BUFFER."
5465 ;; Much of this is being derived from the stuff in appendix of E in
5466 ;; the TeXBook, pg 421.
5467 (set-buffer buffer)
5468 (let ((doc-style (format "\n\\documentstyle{%s}\n"
5469 "report"))
5470 (page-numbering (if allout-number-pages
5471 "\\pagestyle{empty}\n"
5472 ""))
5473 (titlecmd (format "\\newcommand{\\titlecmd}[1]{{%s #1}}\n"
5474 allout-title-style))
5475 (labelcmd (format "\\newcommand{\\labelcmd}[1]{{%s #1}}\n"
5476 allout-label-style))
5477 (headlinecmd (format "\\newcommand{\\headlinecmd}[1]{{%s #1}}\n"
5478 allout-head-line-style))
5479 (bodylinecmd (format "\\newcommand{\\bodylinecmd}[1]{{%s #1}}\n"
5480 allout-body-line-style))
5481 (setlength (format "%s%s%s%s"
5482 "\\newlength{\\stepsize}\n"
5483 "\\setlength{\\stepsize}{"
5484 allout-indent
5485 "}\n"))
5486 (oneheadline (format "%s%s%s%s%s%s%s"
5487 "\\newcommand{\\OneHeadLine}[3]{%\n"
5488 "\\noindent%\n"
5489 "\\hspace*{#2\\stepsize}%\n"
5490 "\\labelcmd{#1}\\hspace*{.2cm}"
5491 "\\headlinecmd{#3}\\\\["
5492 allout-line-skip
5493 "]\n}\n"))
5494 (onebodyline (format "%s%s%s%s%s%s"
5495 "\\newcommand{\\OneBodyLine}[2]{%\n"
5496 "\\noindent%\n"
5497 "\\hspace*{#1\\stepsize}%\n"
5498 "\\bodylinecmd{#2}\\\\["
5499 allout-line-skip
5500 "]\n}\n"))
5501 (begindoc "\\begin{document}\n\\begin{center}\n")
5502 (title (format "%s%s%s%s"
5503 "\\titlecmd{"
5504 (allout-latex-verb-quote (if allout-title
5505 (condition-case nil
5506 (eval allout-title)
5507 (error "<unnamed buffer>"))
5508 "Unnamed Outline"))
5509 "}\n"
5510 "\\end{center}\n\n"))
5511 (hsize "\\hsize = 7.5 true in\n")
5512 (hoffset "\\hoffset = -1.5 true in\n")
5513 (vspace "\\vspace{.1cm}\n\n"))
5514 (insert (concat doc-style
5515 page-numbering
5516 titlecmd
5517 labelcmd
5518 headlinecmd
5519 bodylinecmd
5520 setlength
5521 oneheadline
5522 onebodyline
5523 begindoc
5524 title
5525 hsize
5526 hoffset
5527 vspace)
5529 ;;;_ > allout-insert-latex-trailer (buffer)
5530 (defun allout-insert-latex-trailer (buffer)
5531 "Insert concluding LaTeX commands at point in BUFFER."
5532 (set-buffer buffer)
5533 (insert "\n\\end{document}\n"))
5534 ;;;_ > allout-latexify-one-item (depth prefix bullet text)
5535 (defun allout-latexify-one-item (depth prefix bullet text)
5536 "Insert LaTeX commands for formatting one outline item.
5538 Args are the topics numeric DEPTH, the header PREFIX lead string, the
5539 BULLET string, and a list of TEXT strings for the body."
5540 (let* ((head-line (if text (car text)))
5541 (body-lines (cdr text))
5542 (curr-line)
5543 body-content bop)
5544 ; Do the head line:
5545 (insert (concat "\\OneHeadLine{\\verb\1 "
5546 (allout-latex-verb-quote bullet)
5547 "\1}{"
5548 depth
5549 "}{\\verb\1 "
5550 (if head-line
5551 (allout-latex-verb-quote head-line)
5553 "\1}\n"))
5554 (if (not body-lines)
5556 ;;(insert "\\beginlines\n")
5557 (insert "\\begin{verbatim}\n")
5558 (while body-lines
5559 (setq curr-line (car body-lines))
5560 (if (and (not body-content)
5561 (not (string-match "^\\s-*$" curr-line)))
5562 (setq body-content t))
5563 ; Mangle any occurrences of
5564 ; "\end{verbatim}" in text,
5565 ; it's special:
5566 (if (and body-content
5567 (setq bop (string-match "\\end{verbatim}" curr-line)))
5568 (setq curr-line (concat (substring curr-line 0 bop)
5570 (substring curr-line bop))))
5571 ;;(insert "|" (car body-lines) "|")
5572 (insert curr-line)
5573 (allout-latex-verbatim-quote-curr-line)
5574 (insert "\n")
5575 (setq body-lines (cdr body-lines)))
5576 (if body-content
5577 (setq body-content nil)
5578 (forward-char -1)
5579 (insert "\\ ")
5580 (forward-char 1))
5581 ;;(insert "\\endlines\n")
5582 (insert "\\end{verbatim}\n")
5584 ;;;_ > allout-latexify-exposed (arg &optional tobuf)
5585 (defun allout-latexify-exposed (arg &optional tobuf)
5586 "Format current topics exposed portions to TOBUF for LaTeX processing.
5587 TOBUF defaults to a buffer named the same as the current buffer, but
5588 with \"*\" prepended and \" latex-formed*\" appended.
5590 With repeat count, copy the exposed portions of entire buffer."
5592 (interactive "P")
5593 (if (not tobuf)
5594 (setq tobuf
5595 (get-buffer-create (concat "*" (buffer-name) " latexified*"))))
5596 (let* ((start-pt (point))
5597 (beg (if arg (point-min) (allout-back-to-current-heading)))
5598 (end (if arg (point-max) (allout-end-of-current-subtree)))
5599 (buf (current-buffer)))
5600 (set-buffer tobuf)
5601 (erase-buffer)
5602 (allout-insert-latex-header tobuf)
5603 (goto-char (point-max))
5604 (allout-process-exposed 'allout-latexify-one-item
5608 tobuf)
5609 (goto-char (point-max))
5610 (allout-insert-latex-trailer tobuf)
5611 (goto-char (point-min))
5612 (pop-to-buffer buf)
5613 (goto-char start-pt)))
5615 ;;;_ #8 Encryption
5616 ;;;_ > allout-toggle-current-subtree-encryption (&optional fetch-pass)
5617 (defun allout-toggle-current-subtree-encryption (&optional fetch-pass)
5618 "Encrypt clear or decrypt encoded text of visibly-containing topic's contents.
5620 Optional FETCH-PASS universal argument provokes key-pair encryption with
5621 single universal argument. With doubled universal argument \(value = 16),
5622 it forces prompting for the passphrase regardless of availability from the
5623 passphrase cache. With no universal argument, the appropriate passphrase
5624 is obtained from the cache, if available, else from the user.
5626 Currently only GnuPG encryption is supported.
5628 \**NOTE WELL** that the encrypted text must be ascii-armored. For gnupg
5629 encryption, include the option ``armor'' in your ~/.gnupg/gpg.conf file.
5631 Both symmetric-key and key-pair encryption is implemented. Symmetric is
5632 the default, use a single \(x4) universal argument for keypair mode.
5634 Encrypted topic's bullet is set to a `~' to signal that the contents of the
5635 topic \(body and subtopics, but not heading) is pending encryption or
5636 encrypted. `*' asterisk immediately after the bullet signals that the body
5637 is encrypted, its' absence means the topic is meant to be encrypted but is
5638 not. When a file with topics pending encryption is saved, topics pending
5639 encryption are encrypted. See allout-encrypt-unencrypted-on-saves for
5640 auto-encryption specifics.
5642 \**NOTE WELL** that automatic encryption that happens during saves will
5643 default to symmetric encryption - you must manually \(re)encrypt key-pair
5644 encrypted topics if you want them to continue to use the key-pair cipher.
5646 Level-one topics, with prefix consisting solely of an `*' asterisk, cannot be
5647 encrypted. If you want to encrypt the contents of a top-level topic, use
5648 \\[allout-shift-in] to increase its depth.
5650 Passphrase Caching
5652 The encryption passphrase is solicited if not currently available in the
5653 passphrase cache from a recent encryption action.
5655 The solicited passphrase is retained for reuse in a buffer-specific cache
5656 for some set period of time \(default, 60 seconds), after which the string
5657 is nulled. The passphrase cache timeout is customized by setting
5658 `pgg-passphrase-cache-expiry'.
5660 Symmetric Passphrase Hinting and Verification
5662 If the file previously had no associated passphrase, or had a different
5663 passphrase than specified, the user is prompted to repeat the new one for
5664 corroboration. A random string encrypted by the new passphrase is set on
5665 the buffer-specific variable `allout-passphrase-verifier-string', for
5666 confirmation of the passphrase when next obtained, before encrypting or
5667 decrypting anything with it. This helps avoid mistakenly shifting between
5668 keys.
5670 If allout customization var `allout-passphrase-verifier-handling' is
5671 non-nil, an entry for `allout-passphrase-verifier-string' and its value is
5672 added to an Emacs 'local variables' section at the end of the file, which
5673 is created if necessary. That setting is for retention of the passphrase
5674 verifier across emacs sessions.
5676 Similarly, `allout-passphrase-hint-string' stores a user-provided reminder
5677 about their passphrase, and `allout-passphrase-hint-handling' specifies
5678 when the hint is presented, or if passphrase hints are disabled. If
5679 enabled \(see the `allout-passphrase-hint-handling' docstring for details),
5680 the hint string is stored in the local-variables section of the file, and
5681 solicited whenever the passphrase is changed."
5682 (interactive "P")
5683 (save-excursion
5684 (allout-back-to-current-heading)
5685 (allout-toggle-subtree-encryption fetch-pass)
5688 ;;;_ > allout-toggle-subtree-encryption (&optional fetch-pass)
5689 (defun allout-toggle-subtree-encryption (&optional fetch-pass)
5690 "Encrypt clear text or decrypt encoded topic contents \(body and subtopics.)
5692 Optional FETCH-PASS universal argument provokes key-pair encryption with
5693 single universal argument. With doubled universal argument \(value = 16),
5694 it forces prompting for the passphrase regardless of availability from the
5695 passphrase cache. With no universal argument, the appropriate passphrase
5696 is obtained from the cache, if available, else from the user.
5698 Currently only GnuPG encryption is supported.
5700 \**NOTE WELL** that the encrypted text must be ascii-armored. For gnupg
5701 encryption, include the option ``armor'' in your ~/.gnupg/gpg.conf file.
5703 See `allout-toggle-current-subtree-encryption' for more details."
5705 (interactive "P")
5706 (save-excursion
5707 (allout-end-of-prefix t)
5709 (if (= allout-recent-depth 1)
5710 (error (concat "Cannot encrypt or decrypt level 1 topics -"
5711 " shift it in to make it encryptable")))
5713 (let* ((allout-buffer (current-buffer))
5714 ;; Asses location:
5715 (bullet-pos allout-recent-prefix-beginning)
5716 (after-bullet-pos (point))
5717 (was-encrypted
5718 (progn (if (= (point-max) after-bullet-pos)
5719 (error "no body to encrypt"))
5720 (allout-encrypted-topic-p)))
5721 (was-collapsed (if (not (search-forward "\n" nil t))
5723 (backward-char 1)
5724 (allout-hidden-p)))
5725 (subtree-beg (1+ (point)))
5726 (subtree-end (allout-end-of-subtree))
5727 (subject-text (buffer-substring-no-properties subtree-beg
5728 subtree-end))
5729 (subtree-end-char (char-after (1- subtree-end)))
5730 (subtree-trailing-char (char-after subtree-end))
5731 ;; kluge - result-text needs to be nil, but we also want to
5732 ;; check for the error condition
5733 (result-text (if (or (string= "" subject-text)
5734 (string= "\n" subject-text))
5735 (error "No topic contents to %scrypt"
5736 (if was-encrypted "de" "en"))
5737 nil))
5738 ;; Assess key parameters:
5739 (key-info (or
5740 ;; detect the type by which it is already encrypted
5741 (and was-encrypted
5742 (allout-encrypted-key-info subject-text))
5743 (and (member fetch-pass '(4 (4)))
5744 '(keypair nil))
5745 '(symmetric nil)))
5746 (for-key-type (car key-info))
5747 (for-key-identity (cadr key-info))
5748 (fetch-pass (and fetch-pass (member fetch-pass '(16 (16))))))
5750 (setq result-text
5751 (allout-encrypt-string subject-text was-encrypted
5752 (current-buffer)
5753 for-key-type for-key-identity fetch-pass))
5755 ;; Replace the subtree with the processed product.
5756 (allout-unprotected
5757 (progn
5758 (set-buffer allout-buffer)
5759 (delete-region subtree-beg subtree-end)
5760 (insert result-text)
5761 (if was-collapsed
5762 (allout-flag-region (1- subtree-beg) (point) t))
5763 ;; adjust trailing-blank-lines to preserve topic spacing:
5764 (if (not was-encrypted)
5765 (if (and (= subtree-end-char ?\n)
5766 (= subtree-trailing-char ?\n))
5767 (insert subtree-trailing-char)))
5768 ;; Ensure that the item has an encrypted-entry bullet:
5769 (if (not (string= (buffer-substring-no-properties
5770 (1- after-bullet-pos) after-bullet-pos)
5771 allout-topic-encryption-bullet))
5772 (progn (goto-char (1- after-bullet-pos))
5773 (delete-char 1)
5774 (insert allout-topic-encryption-bullet)))
5775 (if was-encrypted
5776 ;; Remove the is-encrypted bullet qualifier:
5777 (progn (goto-char after-bullet-pos)
5778 (delete-char 1))
5779 ;; Add the is-encrypted bullet qualifier:
5780 (goto-char after-bullet-pos)
5781 (insert "*"))))
5782 (run-hook-with-args 'allout-structure-added-hook
5783 bullet-pos subtree-end))))
5784 ;;;_ > allout-encrypt-string (text decrypt allout-buffer key-type for-key
5785 ;;; fetch-pass &optional retried verifying
5786 ;;; passphrase)
5787 (defun allout-encrypt-string (text decrypt allout-buffer key-type for-key
5788 fetch-pass &optional retried rejected
5789 verifying passphrase)
5790 "Encrypt or decrypt message TEXT.
5792 If DECRYPT is true (default false), then decrypt instead of encrypt.
5794 FETCH-PASS (default false) forces fresh prompting for the passphrase.
5796 KEY-TYPE indicates whether to use a 'symmetric or 'keypair cipher.
5798 FOR-KEY is human readable identification of the first of the user's
5799 eligible secret keys a keypair decryption targets, or else nil.
5801 Optional RETRIED is for internal use - conveys the number of failed keys
5802 that have been solicited in sequence leading to this current call.
5804 Optional PASSPHRASE enables explicit delivery of the decryption passphrase,
5805 for verification purposes.
5807 Optional REJECTED is for internal use - conveys the number of
5808 rejections due to matches against
5809 `allout-encryption-ciphertext-rejection-regexps', as limited by
5810 `allout-encryption-ciphertext-rejection-ceiling'.
5812 Returns the resulting string, or nil if the transformation fails."
5814 (require 'pgg)
5816 (if (not (fboundp 'pgg-encrypt-symmetric))
5817 (error "Allout encryption depends on a newer version of pgg"))
5819 (let* ((scheme (upcase
5820 (format "%s" (or pgg-scheme pgg-default-scheme "GPG"))))
5821 (for-key (and (equal key-type 'keypair)
5822 (or for-key
5823 (split-string (read-string
5824 (format "%s message recipients: "
5825 scheme))
5826 "[ \t,]+"))))
5827 (target-prompt-id (if (equal key-type 'keypair)
5828 (if (= (length for-key) 1)
5829 (car for-key) for-key)
5830 (buffer-name allout-buffer)))
5831 (target-cache-id (format "%s-%s"
5832 key-type
5833 (if (equal key-type 'keypair)
5834 target-prompt-id
5835 (or (buffer-file-name allout-buffer)
5836 target-prompt-id))))
5837 (strip-plaintext-regexps
5838 (if (not decrypt)
5839 (allout-get-configvar-values
5840 'allout-encryption-plaintext-sanitization-regexps)))
5841 (reject-ciphertext-regexps
5842 (if (not decrypt)
5843 (allout-get-configvar-values
5844 'allout-encryption-ciphertext-rejection-regexps)))
5845 (rejected (or rejected 0))
5846 (rejections-left (- allout-encryption-ciphertext-rejection-ceiling
5847 rejected))
5848 result-text status)
5850 (if (and fetch-pass (not passphrase))
5851 ;; Force later fetch by evicting passphrase from the cache.
5852 (pgg-remove-passphrase-from-cache target-cache-id t))
5854 (catch 'encryption-failed
5856 ;; Obtain the passphrase if we don't already have one and we're not
5857 ;; doing a keypair encryption:
5858 (if (not (or passphrase
5859 (and (equal key-type 'keypair)
5860 (not decrypt))))
5862 (setq passphrase (allout-obtain-passphrase for-key
5863 target-cache-id
5864 target-prompt-id
5865 key-type
5866 allout-buffer
5867 retried fetch-pass)))
5869 (with-temp-buffer
5871 (insert text)
5873 (when (and strip-plaintext-regexps (not decrypt))
5874 (dolist (re strip-plaintext-regexps)
5875 (let ((re (if (listp re) (car re) re))
5876 (replacement (if (listp re) (cadr re) "")))
5877 (goto-char (point-min))
5878 (while (re-search-forward re nil t)
5879 (replace-match replacement nil nil)))))
5881 (cond
5883 ;; symmetric:
5884 ((equal key-type 'symmetric)
5885 (setq status
5886 (if decrypt
5888 (pgg-decrypt (point-min) (point-max) passphrase)
5890 (pgg-encrypt-symmetric (point-min) (point-max)
5891 passphrase)))
5893 (if status
5894 (pgg-situate-output (point-min) (point-max))
5895 ;; failed - handle passphrase caching
5896 (if verifying
5897 (throw 'encryption-failed nil)
5898 (pgg-remove-passphrase-from-cache target-cache-id t)
5899 (error "Symmetric-cipher %scryption failed - %s"
5900 (if decrypt "de" "en")
5901 "try again with different passphrase."))))
5903 ;; encrypt 'keypair:
5904 ((not decrypt)
5906 (setq status
5908 (pgg-encrypt for-key
5909 nil (point-min) (point-max) passphrase))
5911 (if status
5912 (pgg-situate-output (point-min) (point-max))
5913 (error (pgg-remove-passphrase-from-cache target-cache-id t)
5914 (error "encryption failed"))))
5916 ;; decrypt 'keypair:
5919 (setq status
5920 (pgg-decrypt (point-min) (point-max) passphrase))
5922 (if status
5923 (pgg-situate-output (point-min) (point-max))
5924 (error (pgg-remove-passphrase-from-cache target-cache-id t)
5925 (error "decryption failed")))))
5927 (setq result-text
5928 (buffer-substring-no-properties
5929 1 (- (point-max) (if decrypt 0 1))))
5932 ;; validate result - non-empty
5933 (cond ((not result-text)
5934 (if verifying
5936 ;; transform was fruitless, retry w/new passphrase.
5937 (pgg-remove-passphrase-from-cache target-cache-id t)
5938 (allout-encrypt-string text decrypt allout-buffer
5939 key-type for-key nil
5940 (if retried (1+ retried) 1)
5941 rejected verifying nil)))
5943 ;; Retry (within limit) if ciphertext contains rejections:
5944 ((and (not decrypt)
5945 ;; Check for disqualification of this ciphertext:
5946 (let ((regexps reject-ciphertext-regexps)
5947 reject-it)
5948 (while (and regexps (not reject-it))
5949 (setq reject-it (string-match (car regexps)
5950 result-text))
5951 (pop regexps))
5952 reject-it))
5953 (setq rejections-left (1- rejections-left))
5954 (if (<= rejections-left 0)
5955 (error (concat "Ciphertext rejected too many times"
5956 " (%s), per `%s'")
5957 allout-encryption-ciphertext-rejection-ceiling
5958 'allout-encryption-ciphertext-rejection-regexps)
5959 (allout-encrypt-string text decrypt allout-buffer
5960 key-type for-key nil
5961 retried (1+ rejected)
5962 verifying passphrase)))
5963 ;; Barf if encryption yields extraordinary control chars:
5964 ((and (not decrypt)
5965 (string-match "[\C-a\C-k\C-o-\C-z\C-@]"
5966 result-text))
5967 (error (concat "Encryption produced non-armored text, which"
5968 "conflicts with allout mode - reconfigure!")))
5970 ;; valid result and just verifying or non-symmetric:
5971 ((or verifying (not (equal key-type 'symmetric)))
5972 (if (or verifying decrypt)
5973 (pgg-add-passphrase-to-cache target-cache-id
5974 passphrase t))
5975 result-text)
5977 ;; valid result and regular symmetric - "register"
5978 ;; passphrase with mnemonic aids/cache.
5980 (set-buffer allout-buffer)
5981 (if passphrase
5982 (pgg-add-passphrase-to-cache target-cache-id
5983 passphrase t))
5984 (allout-update-passphrase-mnemonic-aids for-key passphrase
5985 allout-buffer)
5986 result-text)
5991 ;;;_ > allout-obtain-passphrase (for-key cache-id prompt-id key-type
5992 ;;; allout-buffer retried fetch-pass)
5993 (defun allout-obtain-passphrase (for-key cache-id prompt-id key-type
5994 allout-buffer retried fetch-pass)
5995 "Obtain passphrase for a key from the cache or else from the user.
5997 When obtaining from the user, symmetric-cipher passphrases are verified
5998 against either, if available and enabled, a random string that was
5999 encrypted against the passphrase, or else against repeated entry by the
6000 user for corroboration.
6002 FOR-KEY is the key for which the passphrase is being obtained.
6004 CACHE-ID is the cache id of the key for the passphrase.
6006 PROMPT-ID is the id for use when prompting the user.
6008 KEY-TYPE is either 'symmetric or 'keypair.
6010 ALLOUT-BUFFER is the buffer containing the entry being en/decrypted.
6012 RETRIED is the number of this attempt to obtain this passphrase.
6014 FETCH-PASS causes the passphrase to be solicited from the user, regardless
6015 of the availability of a cached copy."
6017 (if (not (equal key-type 'symmetric))
6018 ;; do regular passphrase read on non-symmetric passphrase:
6019 (pgg-read-passphrase (format "%s passphrase%s: "
6020 (upcase (format "%s" (or pgg-scheme
6021 pgg-default-scheme
6022 "GPG")))
6023 (if prompt-id
6024 (format " for %s" prompt-id)
6025 ""))
6026 cache-id t)
6028 ;; Symmetric hereon:
6030 (save-excursion
6031 (set-buffer allout-buffer)
6032 (let* ((hint (if (and (not (string= allout-passphrase-hint-string ""))
6033 (or (equal allout-passphrase-hint-handling 'always)
6034 (and (equal allout-passphrase-hint-handling
6035 'needed)
6036 retried)))
6037 (format " [%s]" allout-passphrase-hint-string)
6038 ""))
6039 (retry-message (if retried (format " (%s retry)" retried) ""))
6040 (prompt-sans-hint (format "'%s' symmetric passphrase%s: "
6041 prompt-id retry-message))
6042 (full-prompt (format "'%s' symmetric passphrase%s%s: "
6043 prompt-id hint retry-message))
6044 (prompt full-prompt)
6045 (verifier-string (allout-get-encryption-passphrase-verifier))
6047 (cached (and (not fetch-pass)
6048 (pgg-read-passphrase-from-cache cache-id t)))
6049 (got-pass (or cached
6050 (pgg-read-passphrase full-prompt cache-id t)))
6051 confirmation)
6053 (if (not got-pass)
6056 ;; Duplicate our handle on the passphrase so it's not clobbered by
6057 ;; deactivate-passwd memory clearing:
6058 (setq got-pass (copy-sequence got-pass))
6060 (cond (verifier-string
6061 (save-window-excursion
6062 (if (allout-encrypt-string verifier-string 'decrypt
6063 allout-buffer 'symmetric
6064 for-key nil 0 0 'verifying
6065 (copy-sequence got-pass))
6066 (setq confirmation (format "%s" got-pass))))
6068 (if (and (not confirmation)
6069 (if (yes-or-no-p
6070 (concat "Passphrase differs from established"
6071 " - use new one instead? "))
6072 ;; deactivate password for subsequent
6073 ;; confirmation:
6074 (progn
6075 (pgg-remove-passphrase-from-cache cache-id t)
6076 (setq prompt prompt-sans-hint)
6077 nil)
6079 (progn (pgg-remove-passphrase-from-cache cache-id t)
6080 (error "Wrong passphrase."))))
6081 ;; No verifier string - force confirmation by repetition of
6082 ;; (new) passphrase:
6083 ((or fetch-pass (not cached))
6084 (pgg-remove-passphrase-from-cache cache-id t))))
6085 ;; confirmation vs new input - doing pgg-read-passphrase will do the
6086 ;; right thing, in either case:
6087 (if (not confirmation)
6088 (setq confirmation
6089 (pgg-read-passphrase (concat prompt
6090 " ... confirm spelling: ")
6091 cache-id t)))
6092 (prog1
6093 (if (equal got-pass confirmation)
6094 confirmation
6095 (if (yes-or-no-p (concat "spelling of original and"
6096 " confirmation differ - retry? "))
6097 (progn (setq retried (if retried (1+ retried) 1))
6098 (pgg-remove-passphrase-from-cache cache-id t)
6099 ;; recurse to this routine:
6100 (pgg-read-passphrase prompt-sans-hint cache-id t))
6101 (pgg-remove-passphrase-from-cache cache-id t)
6102 (error "Confirmation failed."))))))))
6103 ;;;_ > allout-encrypted-topic-p ()
6104 (defun allout-encrypted-topic-p ()
6105 "True if the current topic is encryptable and encrypted."
6106 (save-excursion
6107 (allout-end-of-prefix t)
6108 (and (string= (buffer-substring-no-properties (1- (point)) (point))
6109 allout-topic-encryption-bullet)
6110 (looking-at "\\*"))
6113 ;;;_ > allout-encrypted-key-info (text)
6114 ;; XXX gpg-specific, alas
6115 (defun allout-encrypted-key-info (text)
6116 "Return a pair of the key type and identity of a recipient's secret key.
6118 The key type is one of 'symmetric or 'keypair.
6120 if 'keypair, and some of the user's secret keys are among those for which
6121 the message was encoded, return the identity of the first. otherwise,
6122 return nil for the second item of the pair.
6124 An error is raised if the text is not encrypted."
6125 (require 'pgg-parse)
6126 (save-excursion
6127 (with-temp-buffer
6128 (insert text)
6129 (let* ((parsed-armor (pgg-parse-armor-region (point-min) (point-max)))
6130 (type (if (pgg-gpg-symmetric-key-p parsed-armor)
6131 'symmetric
6132 'keypair))
6133 secret-keys first-secret-key for-key-owner)
6134 (if (equal type 'keypair)
6135 (setq secret-keys (pgg-gpg-lookup-all-secret-keys)
6136 first-secret-key (pgg-gpg-select-matching-key parsed-armor
6137 secret-keys)
6138 for-key-owner (and first-secret-key
6139 (pgg-gpg-lookup-key-owner
6140 first-secret-key))))
6141 (list type (pgg-gpg-key-id-from-key-owner for-key-owner))
6146 ;;;_ > allout-create-encryption-passphrase-verifier (passphrase)
6147 (defun allout-create-encryption-passphrase-verifier (passphrase)
6148 "Encrypt random message for later validation of symmetric key's passphrase."
6149 ;; use 20 random ascii characters, across the entire ascii range.
6150 (random t)
6151 (let ((spew (make-string 20 ?\0)))
6152 (dotimes (i (length spew))
6153 (aset spew i (1+ (random 254))))
6154 (allout-encrypt-string spew nil (current-buffer) 'symmetric
6155 nil nil 0 0 passphrase))
6157 ;;;_ > allout-update-passphrase-mnemonic-aids (for-key passphrase
6158 ;;; outline-buffer)
6159 (defun allout-update-passphrase-mnemonic-aids (for-key passphrase
6160 outline-buffer)
6161 "Update passphrase verifier and hint strings if necessary.
6163 See `allout-passphrase-verifier-string' and `allout-passphrase-hint-string'
6164 settings.
6166 PASSPHRASE is the passphrase being mnemonicized
6168 OUTLINE-BUFFER is the buffer of the outline being adjusted.
6170 These are used to help the user keep track of the passphrase they use for
6171 symmetric encryption in the file.
6173 Behavior is governed by `allout-passphrase-verifier-handling',
6174 `allout-passphrase-hint-handling', and also, controlling whether the values
6175 are preserved on Emacs local file variables,
6176 `allout-enable-file-variable-adjustment'."
6178 ;; If passphrase doesn't agree with current verifier:
6179 ;; - adjust the verifier
6180 ;; - if passphrase hint handling is enabled, adjust the passphrase hint
6181 ;; - if file var settings are enabled, adjust the file vars
6183 (let* ((new-verifier-needed (not (allout-verify-passphrase
6184 for-key passphrase outline-buffer)))
6185 (new-verifier-string
6186 (if new-verifier-needed
6187 ;; Collapse to a single line and enclose in string quotes:
6188 (subst-char-in-string
6189 ?\n ?\C-a (allout-create-encryption-passphrase-verifier
6190 passphrase))))
6191 new-hint)
6192 (when new-verifier-string
6193 ;; do the passphrase hint first, since it's interactive
6194 (when (and allout-passphrase-hint-handling
6195 (not (equal allout-passphrase-hint-handling 'disabled)))
6196 (setq new-hint
6197 (read-from-minibuffer "Passphrase hint to jog your memory: "
6198 allout-passphrase-hint-string))
6199 (when (not (string= new-hint allout-passphrase-hint-string))
6200 (setq allout-passphrase-hint-string new-hint)
6201 (allout-adjust-file-variable "allout-passphrase-hint-string"
6202 allout-passphrase-hint-string)))
6203 (when allout-passphrase-verifier-handling
6204 (setq allout-passphrase-verifier-string new-verifier-string)
6205 (allout-adjust-file-variable "allout-passphrase-verifier-string"
6206 allout-passphrase-verifier-string))
6210 ;;;_ > allout-get-encryption-passphrase-verifier ()
6211 (defun allout-get-encryption-passphrase-verifier ()
6212 "Return text of the encrypt passphrase verifier, unmassaged, or nil if none.
6214 Derived from value of `allout-passphrase-verifier-string'."
6216 (let ((verifier-string (and (boundp 'allout-passphrase-verifier-string)
6217 allout-passphrase-verifier-string)))
6218 (if verifier-string
6219 ;; Return it uncollapsed
6220 (subst-char-in-string ?\C-a ?\n verifier-string))
6223 ;;;_ > allout-verify-passphrase (key passphrase allout-buffer)
6224 (defun allout-verify-passphrase (key passphrase allout-buffer)
6225 "True if passphrase successfully decrypts verifier, nil otherwise.
6227 \"Otherwise\" includes absence of passphrase verifier."
6228 (save-excursion
6229 (set-buffer allout-buffer)
6230 (and (boundp 'allout-passphrase-verifier-string)
6231 allout-passphrase-verifier-string
6232 (allout-encrypt-string (allout-get-encryption-passphrase-verifier)
6233 'decrypt allout-buffer 'symmetric
6234 key nil 0 0 'verifying passphrase)
6235 t)))
6236 ;;;_ > allout-next-topic-pending-encryption (&optional except-mark)
6237 (defun allout-next-topic-pending-encryption (&optional except-mark)
6238 "Return the point of the next topic pending encryption, or nil if none.
6240 EXCEPT-MARK identifies a point whose containing topics should be excluded
6241 from encryption. This supports 'except-current mode of
6242 `allout-encrypt-unencrypted-on-saves'.
6244 Such a topic has the allout-topic-encryption-bullet without an
6245 immediately following '*' that would mark the topic as being encrypted. It
6246 must also have content."
6247 (let (done got content-beg)
6248 (while (not done)
6250 (if (not (re-search-forward
6251 (format "\\(\\`\\|\n\\)%s *%s[^*]"
6252 (regexp-quote allout-header-prefix)
6253 (regexp-quote allout-topic-encryption-bullet))
6254 nil t))
6255 (setq got nil
6256 done t)
6257 (goto-char (setq got (match-beginning 0)))
6258 (if (looking-at "\n")
6259 (forward-char 1))
6260 (setq got (point)))
6262 (cond ((not got)
6263 (setq done t))
6265 ((not (search-forward "\n"))
6266 (setq got nil
6267 done t))
6269 ((eobp)
6270 (setq got nil
6271 done t))
6274 (setq content-beg (point))
6275 (backward-char 1)
6276 (allout-end-of-subtree)
6277 (if (or (<= (point) content-beg)
6278 (and except-mark
6279 (<= content-beg except-mark)
6280 (>= (point) except-mark)))
6281 ;; Continue looking
6282 (setq got nil)
6283 ;; Got it!
6284 (setq done t)))
6287 (if got
6288 (goto-char got))
6291 ;;;_ > allout-encrypt-decrypted (&optional except-mark)
6292 (defun allout-encrypt-decrypted (&optional except-mark)
6293 "Encrypt topics pending encryption except those containing exemption point.
6295 EXCEPT-MARK identifies a point whose containing topics should be excluded
6296 from encryption. This supports 'except-current mode of
6297 `allout-encrypt-unencrypted-on-saves'.
6299 If a topic that is currently being edited was encrypted, we return a list
6300 containing the location of the topic and the location of the cursor just
6301 before the topic was encrypted. This can be used, eg, to decrypt the topic
6302 and exactly resituate the cursor if this is being done as part of a file
6303 save. See `allout-encrypt-unencrypted-on-saves' for more info."
6305 (interactive "p")
6306 (save-excursion
6307 (let* ((current-mark (point-marker))
6308 (current-mark-position (marker-position current-mark))
6309 was-modified
6310 bo-subtree
6311 editing-topic editing-point)
6312 (goto-char (point-min))
6313 (while (allout-next-topic-pending-encryption except-mark)
6314 (setq was-modified (buffer-modified-p))
6315 (when (save-excursion
6316 (and (boundp 'allout-encrypt-unencrypted-on-saves)
6317 allout-encrypt-unencrypted-on-saves
6318 (setq bo-subtree (re-search-forward "$"))
6319 (not (allout-hidden-p))
6320 (>= current-mark (point))
6321 (allout-end-of-current-subtree)
6322 (<= current-mark (point))))
6323 (setq editing-topic (point)
6324 ;; we had to wait for this 'til now so prior topics are
6325 ;; encrypted, any relevant text shifts are in place:
6326 editing-point (- current-mark-position
6327 (count-trailing-whitespace-region
6328 bo-subtree current-mark-position))))
6329 (allout-toggle-subtree-encryption)
6330 (if (not was-modified)
6331 (set-buffer-modified-p nil))
6333 (if (not was-modified)
6334 (set-buffer-modified-p nil))
6335 (if editing-topic (list editing-topic editing-point))
6340 ;;;_ #9 miscellaneous
6341 ;;;_ : Mode:
6342 ;;;_ > outlineify-sticky ()
6343 ;; outlinify-sticky is correct spelling; provide this alias for sticklers:
6344 ;;;###autoload
6345 (defalias 'outlinify-sticky 'outlineify-sticky)
6346 ;;;###autoload
6347 (defun outlineify-sticky (&optional arg)
6348 "Activate outline mode and establish file var so it is started subsequently.
6350 See doc-string for `allout-layout' and `allout-init' for details on
6351 setup for auto-startup."
6353 (interactive "P")
6355 (allout-mode t)
6357 (save-excursion
6358 (goto-char (point-min))
6359 (if (looking-at allout-regexp)
6361 (allout-open-topic 2)
6362 (insert (concat "Dummy outline topic header - see"
6363 "`allout-mode' docstring: `^Hm'."))
6364 (allout-adjust-file-variable
6365 "allout-layout" (or allout-layout '(-1 : 0))))))
6366 ;;;_ > allout-file-vars-section-data ()
6367 (defun allout-file-vars-section-data ()
6368 "Return data identifying the file-vars section, or nil if none.
6370 Returns list `(beginning-point prefix-string suffix-string)'."
6371 ;; minimally gleaned from emacs 21.4 files.el hack-local-variables function.
6372 (let (beg prefix suffix)
6373 (save-excursion
6374 (goto-char (point-max))
6375 (search-backward "\n\^L" (max (- (point-max) 3000) (point-min)) 'move)
6376 (if (let ((case-fold-search t))
6377 (not (search-forward "Local Variables:" nil t)))
6379 (setq beg (- (point) 16))
6380 (setq suffix (buffer-substring-no-properties
6381 (point)
6382 (progn (if (search-forward "\n" nil t)
6383 (forward-char -1))
6384 (point))))
6385 (setq prefix (buffer-substring-no-properties
6386 (progn (if (search-backward "\n" nil t)
6387 (forward-char 1))
6388 (point))
6389 beg))
6390 (list beg prefix suffix))
6394 ;;;_ > allout-adjust-file-variable (varname value)
6395 (defun allout-adjust-file-variable (varname value)
6396 "Adjust the setting of an emacs file variable named VARNAME to VALUE.
6398 This activity is inhibited if either `enable-local-variables'
6399 `allout-enable-file-variable-adjustment' are nil.
6401 When enabled, an entry for the variable is created if not already present,
6402 or changed if established with a different value. The section for the file
6403 variables, itself, is created if not already present. When created, the
6404 section lines \(including the section line) exist as second-level topics in
6405 a top-level topic at the end of the file.
6407 enable-local-variables must be true for any of this to happen."
6408 (if (not (and enable-local-variables
6409 allout-enable-file-variable-adjustment))
6411 (save-excursion
6412 (let ((inhibit-field-text-motion t)
6413 (section-data (allout-file-vars-section-data))
6414 beg prefix suffix)
6415 (if section-data
6416 (setq beg (car section-data)
6417 prefix (cadr section-data)
6418 suffix (car (cddr section-data)))
6419 ;; create the section
6420 (goto-char (point-max))
6421 (open-line 1)
6422 (allout-open-topic 0)
6423 (end-of-line)
6424 (insert "Local emacs vars.\n")
6425 (allout-open-topic 1)
6426 (setq beg (point)
6427 suffix ""
6428 prefix (buffer-substring-no-properties (progn
6429 (beginning-of-line)
6430 (point))
6431 beg))
6432 (goto-char beg)
6433 (insert "Local variables:\n")
6434 (allout-open-topic 0)
6435 (insert "End:\n")
6437 ;; look for existing entry or create one, leaving point for insertion
6438 ;; of new value:
6439 (goto-char beg)
6440 (allout-show-to-offshoot)
6441 (if (search-forward (concat "\n" prefix varname ":") nil t)
6442 (let* ((value-beg (point))
6443 (line-end (progn (if (search-forward "\n" nil t)
6444 (forward-char -1))
6445 (point)))
6446 (value-end (- line-end (length suffix))))
6447 (if (> value-end value-beg)
6448 (delete-region value-beg value-end)))
6449 (end-of-line)
6450 (open-line 1)
6451 (forward-line 1)
6452 (insert (concat prefix varname ":")))
6453 (insert (format " %S%s" value suffix))
6458 ;;;_ > allout-get-configvar-values (varname)
6459 (defun allout-get-configvar-values (configvar-name)
6460 "Return a list of values of the symbols in list bound to CONFIGVAR-NAME.
6462 The user is prompted for removal of symbols that are unbound, and they
6463 otherwise are ignored.
6465 CONFIGVAR-NAME should be the name of the configuration variable,
6466 not its value."
6468 (let ((configvar-value (symbol-value configvar-name))
6469 got)
6470 (dolist (sym configvar-value)
6471 (if (not (boundp sym))
6472 (if (yes-or-no-p (format "%s entry `%s' is unbound - remove it? "
6473 configvar-name sym))
6474 (delq sym (symbol-value configvar-name)))
6475 (push (symbol-value sym) got)))
6476 (reverse got)))
6477 ;;;_ : Topics:
6478 ;;;_ > allout-mark-topic ()
6479 (defun allout-mark-topic ()
6480 "Put the region around topic currently containing point."
6481 (interactive)
6482 (let ((inhibit-field-text-motion t))
6483 (beginning-of-line))
6484 (allout-goto-prefix-doublechecked)
6485 (push-mark (point))
6486 (allout-end-of-current-subtree)
6487 (exchange-point-and-mark))
6488 ;;;_ : UI:
6489 ;;;_ > solicit-char-in-string (prompt string &optional do-defaulting)
6490 (defun solicit-char-in-string (prompt string &optional do-defaulting)
6491 "Solicit (with first arg PROMPT) choice of a character from string STRING.
6493 Optional arg DO-DEFAULTING indicates to accept empty input (CR)."
6495 (let ((new-prompt prompt)
6496 got)
6498 (while (not got)
6499 (message "%s" new-prompt)
6501 ;; We do our own reading here, so we can circumvent, eg, special
6502 ;; treatment for `?' character. (Oughta use minibuffer keymap instead.)
6503 (setq got
6504 (char-to-string (let ((cursor-in-echo-area nil)) (read-char))))
6506 (setq got
6507 (cond ((string-match (regexp-quote got) string) got)
6508 ((and do-defaulting (string= got "\r"))
6509 ;; Return empty string to default:
6511 ((string= got "\C-g") (signal 'quit nil))
6513 (setq new-prompt (concat prompt
6515 " ...pick from: "
6516 string
6517 ""))
6518 nil))))
6519 ;; got something out of loop - return it:
6520 got)
6522 ;;;_ : Strings:
6523 ;;;_ > regexp-sans-escapes (string)
6524 (defun regexp-sans-escapes (regexp &optional successive-backslashes)
6525 "Return a copy of REGEXP with all character escapes stripped out.
6527 Representations of actual backslashes - '\\\\\\\\' - are left as a
6528 single backslash.
6530 Optional arg SUCCESSIVE-BACKSLASHES is used internally for recursion."
6532 (if (string= regexp "")
6534 ;; Set successive-backslashes to number if current char is
6535 ;; backslash, or else to nil:
6536 (setq successive-backslashes
6537 (if (= (aref regexp 0) ?\\)
6538 (if successive-backslashes (1+ successive-backslashes) 1)
6539 nil))
6540 (if (or (not successive-backslashes) (= 2 successive-backslashes))
6541 ;; Include first char:
6542 (concat (substring regexp 0 1)
6543 (regexp-sans-escapes (substring regexp 1)))
6544 ;; Exclude first char, but maintain count:
6545 (regexp-sans-escapes (substring regexp 1) successive-backslashes))))
6546 ;;;_ > count-trailing-whitespace-region (beg end)
6547 (defun count-trailing-whitespace-region (beg end)
6548 "Return number of trailing whitespace chars between BEG and END.
6550 If BEG is bigger than END we return 0."
6551 (if (> beg end)
6553 (save-excursion
6554 (goto-char beg)
6555 (let ((count 0))
6556 (while (re-search-forward "[ ][ ]*$" end t)
6557 (goto-char (1+ (match-beginning 2)))
6558 (setq count (1+ count)))
6559 count))))
6560 ;;;_ > allout-format-quote (string)
6561 (defun allout-format-quote (string)
6562 "Return a copy of string with all \"%\" characters doubled."
6563 (apply 'concat
6564 (mapcar (lambda (char) (if (= char ?%) "%%" (char-to-string char)))
6565 string)))
6566 ;;;_ : lists
6567 ;;;_ > allout-flatten (list)
6568 (defun allout-flatten (list)
6569 "Return a list of all atoms in list."
6570 ;; classic.
6571 (cond ((null list) nil)
6572 ((atom (car list)) (cons (car list) (allout-flatten (cdr list))))
6573 (t (append (allout-flatten (car list)) (allout-flatten (cdr list))))))
6574 ;;;_ : Compatability:
6575 ;;;_ > allout-mark-marker to accommodate divergent emacsen:
6576 (defun allout-mark-marker (&optional force buffer)
6577 "Accommodate the different signature for `mark-marker' across Emacsen.
6579 XEmacs takes two optional args, while mainline GNU Emacs does not,
6580 so pass them along when appropriate."
6581 (if (featurep 'xemacs)
6582 (apply 'mark-marker force buffer)
6583 (mark-marker)))
6584 ;;;_ > subst-char-in-string if necessary
6585 (if (not (fboundp 'subst-char-in-string))
6586 (defun subst-char-in-string (fromchar tochar string &optional inplace)
6587 "Replace FROMCHAR with TOCHAR in STRING each time it occurs.
6588 Unless optional argument INPLACE is non-nil, return a new string."
6589 (let ((i (length string))
6590 (newstr (if inplace string (copy-sequence string))))
6591 (while (> i 0)
6592 (setq i (1- i))
6593 (if (eq (aref newstr i) fromchar)
6594 (aset newstr i tochar)))
6595 newstr)))
6596 ;;;_ > wholenump if necessary
6597 (if (not (fboundp 'wholenump))
6598 (defalias 'wholenump 'natnump))
6599 ;;;_ > remove-overlays if necessary
6600 (if (not (fboundp 'remove-overlays))
6601 (defun remove-overlays (&optional beg end name val)
6602 "Clear BEG and END of overlays whose property NAME has value VAL.
6603 Overlays might be moved and/or split.
6604 BEG and END default respectively to the beginning and end of buffer."
6605 (unless beg (setq beg (point-min)))
6606 (unless end (setq end (point-max)))
6607 (if (< end beg)
6608 (setq beg (prog1 end (setq end beg))))
6609 (save-excursion
6610 (dolist (o (overlays-in beg end))
6611 (when (eq (overlay-get o name) val)
6612 ;; Either push this overlay outside beg...end
6613 ;; or split it to exclude beg...end
6614 ;; or delete it entirely (if it is contained in beg...end).
6615 (if (< (overlay-start o) beg)
6616 (if (> (overlay-end o) end)
6617 (progn
6618 (move-overlay (copy-overlay o)
6619 (overlay-start o) beg)
6620 (move-overlay o end (overlay-end o)))
6621 (move-overlay o (overlay-start o) beg))
6622 (if (> (overlay-end o) end)
6623 (move-overlay o end (overlay-end o))
6624 (delete-overlay o)))))))
6626 ;;;_ > copy-overlay if necessary - xemacs ~ 21.4
6627 (if (not (fboundp 'copy-overlay))
6628 (defun copy-overlay (o)
6629 "Return a copy of overlay O."
6630 (let ((o1 (make-overlay (overlay-start o) (overlay-end o)
6631 ;; FIXME: there's no easy way to find the
6632 ;; insertion-type of the two markers.
6633 (overlay-buffer o)))
6634 (props (overlay-properties o)))
6635 (while props
6636 (overlay-put o1 (pop props) (pop props)))
6637 o1)))
6638 ;;;_ > add-to-invisibility-spec if necessary - xemacs ~ 21.4
6639 (if (not (fboundp 'add-to-invisibility-spec))
6640 (defun add-to-invisibility-spec (element)
6641 "Add ELEMENT to `buffer-invisibility-spec'.
6642 See documentation for `buffer-invisibility-spec' for the kind of elements
6643 that can be added."
6644 (if (eq buffer-invisibility-spec t)
6645 (setq buffer-invisibility-spec (list t)))
6646 (setq buffer-invisibility-spec
6647 (cons element buffer-invisibility-spec))))
6648 ;;;_ > remove-from-invisibility-spec if necessary - xemacs ~ 21.4
6649 (if (not (fboundp 'remove-from-invisibility-spec))
6650 (defun remove-from-invisibility-spec (element)
6651 "Remove ELEMENT from `buffer-invisibility-spec'."
6652 (if (consp buffer-invisibility-spec)
6653 (setq buffer-invisibility-spec (delete element
6654 buffer-invisibility-spec)))))
6655 ;;;_ > move-beginning-of-line if necessary - older emacs, xemacs
6656 (if (not (fboundp 'move-beginning-of-line))
6657 (defun move-beginning-of-line (arg)
6658 "Move point to beginning of current line as displayed.
6659 \(This disregards invisible newlines such as those
6660 which are part of the text that an image rests on.)
6662 With argument ARG not nil or 1, move forward ARG - 1 lines first.
6663 If point reaches the beginning or end of buffer, it stops there.
6664 To ignore intangibility, bind `inhibit-point-motion-hooks' to t."
6665 (interactive "p")
6666 (or arg (setq arg 1))
6667 (if (/= arg 1)
6668 (condition-case nil (line-move (1- arg)) (error nil)))
6670 ;; Move to beginning-of-line, ignoring fields and invisibles.
6671 (skip-chars-backward "^\n")
6672 (while (and (not (bobp)) (line-move-invisible-p (1- (point))))
6673 (goto-char (if (featurep 'xemacs)
6674 (previous-property-change (point))
6675 (previous-char-property-change (point))))
6676 (skip-chars-backward "^\n"))
6677 (vertical-motion 0))
6679 ;;;_ > move-end-of-line if necessary - older emacs, xemacs
6680 (if (not (fboundp 'move-end-of-line))
6681 (defun move-end-of-line (arg)
6682 "Move point to end of current line as displayed.
6683 \(This disregards invisible newlines such as those
6684 which are part of the text that an image rests on.)
6686 With argument ARG not nil or 1, move forward ARG - 1 lines first.
6687 If point reaches the beginning or end of buffer, it stops there.
6688 To ignore intangibility, bind `inhibit-point-motion-hooks' to t."
6689 (interactive "p")
6690 (or arg (setq arg 1))
6691 (let (done)
6692 (while (not done)
6693 (let ((newpos
6694 (save-excursion
6695 (let ((goal-column 0))
6696 (and (condition-case nil
6697 (or (line-move arg) t)
6698 (error nil))
6699 (not (bobp))
6700 (progn
6701 (while (and (not (bobp))
6702 (line-move-invisible-p (1- (point))))
6703 (goto-char
6704 (previous-char-property-change (point))))
6705 (backward-char 1)))
6706 (point)))))
6707 (goto-char newpos)
6708 (if (and (> (point) newpos)
6709 (eq (preceding-char) ?\n))
6710 (backward-char 1)
6711 (if (and (> (point) newpos) (not (eobp))
6712 (not (eq (following-char) ?\n)))
6713 ;; If we skipped something intangible
6714 ;; and now we're not really at eol,
6715 ;; keep going.
6716 (setq arg 1)
6717 (setq done t)))))))
6719 ;;;_ > line-move-invisible-p if necessary
6720 (if (not (fboundp 'line-move-invisible-p))
6721 (defun line-move-invisible-p (pos)
6722 "Return non-nil if the character after POS is currently invisible."
6723 (let ((prop
6724 (get-char-property pos 'invisible)))
6725 (if (eq buffer-invisibility-spec t)
6726 prop
6727 (or (memq prop buffer-invisibility-spec)
6728 (assq prop buffer-invisibility-spec))))))
6730 ;;;_ #10 Unfinished
6731 ;;;_ > allout-bullet-isearch (&optional bullet)
6732 (defun allout-bullet-isearch (&optional bullet)
6733 "Isearch \(regexp) for topic with bullet BULLET."
6734 (interactive)
6735 (if (not bullet)
6736 (setq bullet (solicit-char-in-string
6737 "ISearch for topic with bullet: "
6738 (regexp-sans-escapes allout-bullets-string))))
6740 (let ((isearch-regexp t)
6741 (isearch-string (concat "^"
6742 allout-header-prefix
6743 "[ \t]*"
6744 bullet)))
6745 (isearch-repeat 'forward)
6746 (isearch-mode t)))
6748 ;;;_ #11 Unit tests - this should be last item before "Provide"
6749 ;;;_ > allout-run-unit-tests ()
6750 (defun allout-run-unit-tests ()
6751 "Run the various allout unit tests."
6752 (message "Running allout tests...")
6753 (allout-test-resumptions)
6754 (message "Running allout tests... Done.")
6755 (sit-for .5))
6756 ;;;_ : test resumptions:
6757 ;;;_ > allout-tests-obliterate-variable (name)
6758 (defun allout-tests-obliterate-variable (name)
6759 "Completely unbind variable with NAME."
6760 (if (local-variable-p name) (kill-local-variable name))
6761 (while (boundp name) (makunbound name)))
6762 ;;;_ > allout-test-resumptions ()
6763 (defvar allout-tests-globally-unbound nil
6764 "Fodder for allout resumptions tests - defvar just for byte compiler.")
6765 (defvar allout-tests-globally-true nil
6766 "Fodder for allout resumptions tests - defvar just just for byte compiler.")
6767 (defvar allout-tests-locally-true nil
6768 "Fodder for allout resumptions tests - defvar just for byte compiler.")
6769 (defun allout-test-resumptions ()
6770 "Exercise allout resumptions."
6771 ;; for each resumption case, we also test that the right local/global
6772 ;; scopes are affected during resumption effects:
6774 ;; ensure that previously unbound variables return to the unbound state.
6775 (with-temp-buffer
6776 (allout-tests-obliterate-variable 'allout-tests-globally-unbound)
6777 (allout-add-resumptions '(allout-tests-globally-unbound t))
6778 (assert (not (default-boundp 'allout-tests-globally-unbound)))
6779 (assert (local-variable-p 'allout-tests-globally-unbound))
6780 (assert (boundp 'allout-tests-globally-unbound))
6781 (assert (equal allout-tests-globally-unbound t))
6782 (allout-do-resumptions)
6783 (assert (not (local-variable-p 'allout-tests-globally-unbound)))
6784 (assert (not (boundp 'allout-tests-globally-unbound))))
6786 ;; ensure that variable with prior global value is resumed
6787 (with-temp-buffer
6788 (allout-tests-obliterate-variable 'allout-tests-globally-true)
6789 (setq allout-tests-globally-true t)
6790 (allout-add-resumptions '(allout-tests-globally-true nil))
6791 (assert (equal (default-value 'allout-tests-globally-true) t))
6792 (assert (local-variable-p 'allout-tests-globally-true))
6793 (assert (equal allout-tests-globally-true nil))
6794 (allout-do-resumptions)
6795 (assert (not (local-variable-p 'allout-tests-globally-true)))
6796 (assert (boundp 'allout-tests-globally-true))
6797 (assert (equal allout-tests-globally-true t)))
6799 ;; ensure that prior local value is resumed
6800 (with-temp-buffer
6801 (allout-tests-obliterate-variable 'allout-tests-locally-true)
6802 (set (make-local-variable 'allout-tests-locally-true) t)
6803 (assert (not (default-boundp 'allout-tests-locally-true))
6804 nil (concat "Test setup mistake - variable supposed to"
6805 " not have global binding, but it does."))
6806 (assert (local-variable-p 'allout-tests-locally-true)
6807 nil (concat "Test setup mistake - variable supposed to have"
6808 " local binding, but it lacks one."))
6809 (allout-add-resumptions '(allout-tests-locally-true nil))
6810 (assert (not (default-boundp 'allout-tests-locally-true)))
6811 (assert (local-variable-p 'allout-tests-locally-true))
6812 (assert (equal allout-tests-locally-true nil))
6813 (allout-do-resumptions)
6814 (assert (boundp 'allout-tests-locally-true))
6815 (assert (local-variable-p 'allout-tests-locally-true))
6816 (assert (equal allout-tests-locally-true t))
6817 (assert (not (default-boundp 'allout-tests-locally-true))))
6819 ;; ensure that last of multiple resumptions holds, for various scopes.
6820 (with-temp-buffer
6821 (allout-tests-obliterate-variable 'allout-tests-globally-unbound)
6822 (allout-tests-obliterate-variable 'allout-tests-globally-true)
6823 (setq allout-tests-globally-true t)
6824 (allout-tests-obliterate-variable 'allout-tests-locally-true)
6825 (set (make-local-variable 'allout-tests-locally-true) t)
6826 (allout-add-resumptions '(allout-tests-globally-unbound t)
6827 '(allout-tests-globally-true nil)
6828 '(allout-tests-locally-true nil))
6829 (allout-add-resumptions '(allout-tests-globally-unbound 2)
6830 '(allout-tests-globally-true 3)
6831 '(allout-tests-locally-true 4))
6832 ;; reestablish many of the basic conditions are maintained after re-add:
6833 (assert (not (default-boundp 'allout-tests-globally-unbound)))
6834 (assert (local-variable-p 'allout-tests-globally-unbound))
6835 (assert (equal allout-tests-globally-unbound 2))
6836 (assert (default-boundp 'allout-tests-globally-true))
6837 (assert (local-variable-p 'allout-tests-globally-true))
6838 (assert (equal allout-tests-globally-true 3))
6839 (assert (not (default-boundp 'allout-tests-locally-true)))
6840 (assert (local-variable-p 'allout-tests-locally-true))
6841 (assert (equal allout-tests-locally-true 4))
6842 (allout-do-resumptions)
6843 (assert (not (local-variable-p 'allout-tests-globally-unbound)))
6844 (assert (not (boundp 'allout-tests-globally-unbound)))
6845 (assert (not (local-variable-p 'allout-tests-globally-true)))
6846 (assert (boundp 'allout-tests-globally-true))
6847 (assert (equal allout-tests-globally-true t))
6848 (assert (boundp 'allout-tests-locally-true))
6849 (assert (local-variable-p 'allout-tests-locally-true))
6850 (assert (equal allout-tests-locally-true t))
6851 (assert (not (default-boundp 'allout-tests-locally-true))))
6853 ;; ensure that deliberately unbinding registered variables doesn't foul things
6854 (with-temp-buffer
6855 (allout-tests-obliterate-variable 'allout-tests-globally-unbound)
6856 (allout-tests-obliterate-variable 'allout-tests-globally-true)
6857 (setq allout-tests-globally-true t)
6858 (allout-tests-obliterate-variable 'allout-tests-locally-true)
6859 (set (make-local-variable 'allout-tests-locally-true) t)
6860 (allout-add-resumptions '(allout-tests-globally-unbound t)
6861 '(allout-tests-globally-true nil)
6862 '(allout-tests-locally-true nil))
6863 (allout-tests-obliterate-variable 'allout-tests-globally-unbound)
6864 (allout-tests-obliterate-variable 'allout-tests-globally-true)
6865 (allout-tests-obliterate-variable 'allout-tests-locally-true)
6866 (allout-do-resumptions))
6868 ;;;_ % Run unit tests if `allout-run-unit-tests-after-load' is true:
6869 (when allout-run-unit-tests-on-load
6870 (allout-run-unit-tests))
6872 ;;;_ #12 Provide
6873 (provide 'allout)
6875 ;;;_* Local emacs vars.
6876 ;; The following `allout-layout' local variable setting:
6877 ;; - closes all topics from the first topic to just before the third-to-last,
6878 ;; - shows the children of the third to last (config vars)
6879 ;; - and the second to last (code section),
6880 ;; - and closes the last topic (this local-variables section).
6881 ;;Local variables:
6882 ;;allout-layout: (0 : -1 -1 0)
6883 ;;End:
6885 ;; arch-tag: cf38fbc3-c044-450f-8bff-afed8ba5681c
6886 ;;; allout.el ends here