(allout-topic-encryption-bullet)
[emacs.git] / lisp / allout.el
blob9c90e702bbf5fcd5fde07d706efa90c030a8379c
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, 2007, 2008 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 3, 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 ;; Currently only GnuPG encryption is supported, and integration
50 ;; with gpg-agent is not yet implemented.)
51 ;; - Automatic topic-number maintenance
52 ;; - "Hot-spot" operation, for single-keystroke maneuvering and
53 ;; exposure control (see the allout-mode docstring)
54 ;; - Easy rendering of exposed portions into numbered, latex, indented, etc
55 ;; outline styles
56 ;; - Careful attention to whitespace -- enabling blank lines between items
57 ;; and maintenance of hanging indentation (in paragraph auto-fill and
58 ;; across topic promotion and demotion) of topic bodies consistent with
59 ;; indentation of their topic header.
61 ;; and more.
63 ;; See the `allout-mode' function's docstring for an introduction to the
64 ;; mode.
66 ;; The latest development version and helpful notes are available at
67 ;; http://myriadicity.net/Sundry/EmacsAllout .
69 ;; The outline menubar additions provide quick reference to many of
70 ;; the features, and see the docstring of the variable `allout-init'
71 ;; for instructions on priming your Emacs session for automatic
72 ;; activation of allout-mode.
74 ;; See the docstring of the variables `allout-layout' and
75 ;; `allout-auto-activation' for details on automatic activation of
76 ;; `allout-mode' as a minor mode. (It has changed since allout
77 ;; 3.x, for those of you that depend on the old method.)
79 ;; Note -- the lines beginning with `;;;_' are outline topic headers.
80 ;; Just `ESC-x eval-buffer' to give it a whirl.
82 ;; ken manheimer (ken dot manheimer at gmail dot com)
84 ;;; Code:
86 ;;;_* Dependency autoloads
87 (require 'overlay)
88 (eval-when-compile
89 ;; Most of the requires here are for stuff covered by autoloads.
90 ;; Since just byte-compiling doesn't trigger autoloads, so that
91 ;; "function not found" warnings would occur without these requires.
92 (progn
93 (require 'pgg)
94 (require 'pgg-gpg)
95 (require 'overlay)
96 ;; `cl' is required for `assert'. `assert' is not covered by a standard
97 ;; autoload, but it is a macro, so that eval-when-compile is sufficient
98 ;; to byte-compile it in, or to do the require when the buffer evalled.
99 (require 'cl)
102 ;;;_* USER CUSTOMIZATION VARIABLES:
104 ;;;_ > defgroup allout
105 (defgroup allout nil
106 "Extensive outline mode for use alone and with other modes."
107 :prefix "allout-"
108 :group 'outlines)
110 ;;;_ + Layout, Mode, and Topic Header Configuration
112 ;;;_ = allout-command-prefix
113 (defcustom allout-command-prefix "\C-c "
114 "*Key sequence to be used as prefix for outline mode command key bindings.
116 Default is '\C-c<space>'; just '\C-c' is more short-and-sweet, if you're
117 willing to let allout use a bunch of \C-c keybindings."
118 :type 'string
119 :group 'allout)
121 ;;;_ = allout-keybindings-list
122 ;;; You have to reactivate allout-mode -- `(allout-mode t)' -- to
123 ;;; institute changes to this var.
124 (defvar allout-keybindings-list ()
125 "*List of `allout-mode' key / function bindings, for `allout-mode-map'.
126 String or vector key will be prefaced with `allout-command-prefix',
127 unless optional third, non-nil element is present.")
128 (setq allout-keybindings-list
130 ; Motion commands:
131 ("\C-n" allout-next-visible-heading)
132 ("\C-p" allout-previous-visible-heading)
133 ("\C-u" allout-up-current-level)
134 ("\C-f" allout-forward-current-level)
135 ("\C-b" allout-backward-current-level)
136 ("\C-a" allout-beginning-of-current-entry)
137 ("\C-e" allout-end-of-entry)
138 ; Exposure commands:
139 ("\C-i" allout-show-children)
140 ("\C-s" allout-show-current-subtree)
141 ("\C-h" allout-hide-current-subtree)
142 ("\C-t" allout-toggle-current-subtree-exposure)
143 ("h" allout-hide-current-subtree)
144 ("\C-o" allout-show-current-entry)
145 ("!" allout-show-all)
146 ("x" allout-toggle-current-subtree-encryption)
147 ; Alteration commands:
148 (" " allout-open-sibtopic)
149 ("." allout-open-subtopic)
150 ("," allout-open-supertopic)
151 ("'" allout-shift-in)
152 (">" allout-shift-in)
153 ("<" allout-shift-out)
154 ("\C-m" allout-rebullet-topic)
155 ("*" allout-rebullet-current-heading)
156 ("#" allout-number-siblings)
157 ("\C-k" allout-kill-line t)
158 ([?\M-k] allout-copy-line-as-kill t)
159 ("\C-y" allout-yank t)
160 ([?\M-y] allout-yank-pop t)
161 ("\C-k" allout-kill-topic)
162 ([?\M-k] allout-copy-topic-as-kill)
163 ; Miscellaneous commands:
164 ;([?\C-\ ] allout-mark-topic)
165 ("@" allout-resolve-xref)
166 ("=c" allout-copy-exposed-to-buffer)
167 ("=i" allout-indented-exposed-to-buffer)
168 ("=t" allout-latexify-exposed)
169 ("=p" allout-flatten-exposed-to-buffer)))
171 ;;;_ = allout-auto-activation
172 (defcustom allout-auto-activation nil
173 "*Regulates auto-activation modality of allout outlines -- see `allout-init'.
175 Setq-default by `allout-init' to regulate whether or not allout
176 outline mode is automatically activated when the buffer-specific
177 variable `allout-layout' is non-nil, and whether or not the layout
178 dictated by `allout-layout' should be imposed on mode activation.
180 With value t, auto-mode-activation and auto-layout are enabled.
181 \(This also depends on `allout-find-file-hook' being installed in
182 `find-file-hook', which is also done by `allout-init'.)
184 With value `ask', auto-mode-activation is enabled, and endorsement for
185 performing auto-layout is asked of the user each time.
187 With value `activate', only auto-mode-activation is enabled,
188 auto-layout is not.
190 With value nil, neither auto-mode-activation nor auto-layout are
191 enabled.
193 See the docstring for `allout-init' for the proper interface to
194 this variable."
195 :type '(choice (const :tag "On" t)
196 (const :tag "Ask about layout" "ask")
197 (const :tag "Mode only" "activate")
198 (const :tag "Off" nil))
199 :group 'allout)
200 ;;;_ = allout-default-layout
201 (defcustom allout-default-layout '(-2 : 0)
202 "*Default allout outline layout specification.
204 This setting specifies the outline exposure to use when
205 `allout-layout' has the local value `t'. This docstring describes the
206 layout specifications.
208 A list value specifies a default layout for the current buffer,
209 to be applied upon activation of `allout-mode'. Any non-nil
210 value will automatically trigger `allout-mode', provided
211 `allout-init' has been called to enable this behavior.
213 The types of elements in the layout specification are:
215 INTEGER -- dictate the relative depth to open the corresponding topic(s),
216 where:
217 -- negative numbers force the topic to be closed before opening
218 to the absolute value of the number, so all siblings are open
219 only to that level.
220 -- positive numbers open to the relative depth indicated by the
221 number, but do not force already opened subtopics to be closed.
222 -- 0 means to close topic -- hide all subitems.
223 : -- repeat spec -- apply the preceeding element to all siblings at
224 current level, *up to* those siblings that would be covered by specs
225 following the `:' on the list. Ie, apply to all topics at level but
226 trailing ones accounted for by trailing specs. (Only the first of
227 multiple colons at the same level is honored -- later ones are ignored.)
228 * -- completely exposes the topic, including bodies
229 + -- exposes all subtopics, but not the bodies
230 - -- exposes the body of the corresponding topic, but not subtopics
231 LIST -- a nested layout spec, to be applied intricately to its
232 corresponding item(s)
234 Examples:
235 (-2 : 0)
236 Collapse the top-level topics to show their children and
237 grandchildren, but completely collapse the final top-level topic.
238 (-1 () : 1 0)
239 Close the first topic so only the immediate subtopics are shown,
240 leave the subsequent topics exposed as they are until the second
241 second to last topic, which is exposed at least one level, and
242 completely close the last topic.
243 (-2 : -1 *)
244 Expose children and grandchildren of all topics at current
245 level except the last two; expose children of the second to
246 last and completely expose the last one, including its subtopics.
248 See `allout-expose-topic' for more about the exposure process.
250 Also, allout's mode-specific provisions will make topic prefixes default
251 to the comment-start string, if any, of the language of the file. This
252 is modulo the setting of `allout-use-mode-specific-leader', which see."
253 :type 'allout-layout-type
254 :group 'allout)
255 ;;;_ : allout-layout-type
256 (define-widget 'allout-layout-type 'lazy
257 "Allout layout format customization basic building blocks."
258 :type '(repeat
259 (choice (integer :tag "integer (<= zero is strict)")
260 (const :tag ": (repeat prior)" :)
261 (const :tag "* (completely expose)" *)
262 (const :tag "+ (expose all offspring, headlines only)" +)
263 (const :tag "- (expose topic body but not offspring)" -)
264 (allout-layout-type :tag "<Nested layout>"))))
266 ;;;_ = allout-inhibit-auto-fill
267 (defcustom allout-inhibit-auto-fill nil
268 "*If non-nil, auto-fill will be inhibited in the allout buffers.
270 You can customize this setting to set it for all allout buffers, or set it
271 in individual buffers if you want to inhibit auto-fill only in particular
272 buffers. (You could use a function on `allout-mode-hook' to inhibit
273 auto-fill according, eg, to the major mode.)
275 If you don't set this and auto-fill-mode is enabled, allout will use the
276 value that `normal-auto-fill-function', if any, when allout mode starts, or
277 else allout's special hanging-indent maintaining auto-fill function,
278 `allout-auto-fill'."
279 :type 'boolean
280 :group 'allout)
281 (make-variable-buffer-local 'allout-inhibit-auto-fill)
282 ;;;_ = allout-use-hanging-indents
283 (defcustom allout-use-hanging-indents t
284 "*If non-nil, topic body text auto-indent defaults to indent of the header.
285 Ie, it is indented to be just past the header prefix. This is
286 relevant mostly for use with `indented-text-mode', or other situations
287 where auto-fill occurs."
288 :type 'boolean
289 :group 'allout)
290 (make-variable-buffer-local 'allout-use-hanging-indents)
291 ;;;###autoload
292 (put 'allout-use-hanging-indents 'safe-local-variable
293 (if (fboundp 'booleanp) 'booleanp '(lambda (x) (member x '(t nil)))))
294 ;;;_ = allout-reindent-bodies
295 (defcustom allout-reindent-bodies (if allout-use-hanging-indents
296 'text)
297 "*Non-nil enables auto-adjust of topic body hanging indent with depth shifts.
299 When active, topic body lines that are indented even with or beyond
300 their topic header are reindented to correspond with depth shifts of
301 the header.
303 A value of t enables reindent in non-programming-code buffers, ie
304 those that do not have the variable `comment-start' set. A value of
305 `force' enables reindent whether or not `comment-start' is set."
306 :type '(choice (const nil) (const t) (const text) (const force))
307 :group 'allout)
309 (make-variable-buffer-local 'allout-reindent-bodies)
310 ;;;###autoload
311 (put 'allout-reindent-bodies 'safe-local-variable
312 '(lambda (x) (memq x '(nil t text force))))
314 ;;;_ = allout-show-bodies
315 (defcustom allout-show-bodies nil
316 "*If non-nil, show entire body when exposing a topic, rather than
317 just the header."
318 :type 'boolean
319 :group 'allout)
320 (make-variable-buffer-local 'allout-show-bodies)
321 ;;;###autoload
322 (put 'allout-show-bodies 'safe-local-variable
323 (if (fboundp 'booleanp) 'booleanp '(lambda (x) (member x '(t nil)))))
325 ;;;_ = allout-beginning-of-line-cycles
326 (defcustom allout-beginning-of-line-cycles t
327 "*If non-nil, \\[allout-beginning-of-line] will cycle through smart-placement options.
329 Cycling only happens on when the command is repeated, not when it
330 follows a different command.
332 Smart-placement means that repeated calls to this function will
333 advance as follows:
335 - if the cursor is on a non-headline body line and not on the first column:
336 then it goes to the first column
337 - if the cursor is on the first column of a non-headline body line:
338 then it goes to the start of the headline within the item body
339 - if the cursor is on the headline and not the start of the headline:
340 then it goes to the start of the headline
341 - if the cursor is on the start of the headline:
342 then it goes to the bullet character (for hotspot navigation)
343 - if the cursor is on the bullet character:
344 then it goes to the first column of that line (the headline)
345 - if the cursor is on the first column of the headline:
346 then it goes to the start of the headline within the item body.
348 In this fashion, you can use the beginning-of-line command to do
349 its normal job and then, when repeated, advance through the
350 entry, cycling back to start.
352 If this configuration variable is nil, then the cursor is just
353 advanced to the beginning of the line and remains there on
354 repeated calls."
355 :type 'boolean :group 'allout)
356 ;;;_ = allout-end-of-line-cycles
357 (defcustom allout-end-of-line-cycles t
358 "*If non-nil, \\[allout-end-of-line] will cycle through smart-placement options.
360 Cycling only happens on when the command is repeated, not when it
361 follows a different command.
363 Smart placement means that repeated calls to this function will
364 advance as follows:
366 - if the cursor is not on the end-of-line,
367 then it goes to the end-of-line
368 - if the cursor is on the end-of-line but not the end-of-entry,
369 then it goes to the end-of-entry, exposing it if necessary
370 - if the cursor is on the end-of-entry,
371 then it goes to the end of the head line
373 In this fashion, you can use the end-of-line command to do its
374 normal job and then, when repeated, advance through the entry,
375 cycling back to start.
377 If this configuration variable is nil, then the cursor is just
378 advanced to the end of the line and remains there on repeated
379 calls."
380 :type 'boolean :group 'allout)
382 ;;;_ = allout-header-prefix
383 (defcustom allout-header-prefix "."
384 ;; this string is treated as literal match. it will be `regexp-quote'd, so
385 ;; one cannot use regular expressions to match varying header prefixes.
386 "*Leading string which helps distinguish topic headers.
388 Outline topic header lines are identified by a leading topic
389 header prefix, which mostly have the value of this var at their front.
390 Level 1 topics are exceptions. They consist of only a single
391 character, which is typically set to the `allout-primary-bullet'."
392 :type 'string
393 :group 'allout)
394 (make-variable-buffer-local 'allout-header-prefix)
395 ;;;###autoload
396 (put 'allout-header-prefix 'safe-local-variable 'stringp)
397 ;;;_ = allout-primary-bullet
398 (defcustom allout-primary-bullet "*"
399 "Bullet used for top-level outline topics.
401 Outline topic header lines are identified by a leading topic header
402 prefix, which is concluded by bullets that includes the value of this
403 var and the respective allout-*-bullets-string vars.
405 The value of an asterisk (`*') provides for backwards compatibility
406 with the original Emacs outline mode. See `allout-plain-bullets-string'
407 and `allout-distinctive-bullets-string' for the range of available
408 bullets."
409 :type 'string
410 :group 'allout)
411 (make-variable-buffer-local 'allout-primary-bullet)
412 ;;;###autoload
413 (put 'allout-primary-bullet 'safe-local-variable 'stringp)
414 ;;;_ = allout-plain-bullets-string
415 (defcustom allout-plain-bullets-string ".,"
416 "*The bullets normally used in outline topic prefixes.
418 See `allout-distinctive-bullets-string' for the other kind of
419 bullets.
421 DO NOT include the close-square-bracket, `]', as a bullet.
423 Outline mode has to be reactivated in order for changes to the value
424 of this var to take effect."
425 :type 'string
426 :group 'allout)
427 (make-variable-buffer-local 'allout-plain-bullets-string)
428 ;;;###autoload
429 (put 'allout-plain-bullets-string 'safe-local-variable 'stringp)
430 ;;;_ = allout-distinctive-bullets-string
431 (defcustom allout-distinctive-bullets-string "*+-=>()[{}&!?#%\"X@$~_\\:;^"
432 "*Persistent outline header bullets used to distinguish special topics.
434 These bullets are distinguish topics with particular character.
435 They are not used by default in the topic creation routines, but
436 are offered as options when you modify topic creation with a
437 universal argument \(\\[universal-argument]), or during rebulleting \(\\[allout-rebullet-current-heading]).
439 Distinctive bullets are not cycled when topics are shifted or
440 otherwise automatically rebulleted, so their marking is
441 persistent until deliberately changed. Their significance is
442 purely by convention, however. Some conventions suggest
443 themselves:
445 `(' - open paren -- an aside or incidental point
446 `?' - question mark -- uncertain or outright question
447 `!' - exclamation point/bang -- emphatic
448 `[' - open square bracket -- meta-note, about item instead of item's subject
449 `\"' - double quote -- a quotation or other citation
450 `=' - equal sign -- an assignement, equating a name with some connotation
451 `^' - carat -- relates to something above
453 Some are more elusive, but their rationale may be recognizable:
455 `+' - plus -- pending consideration, completion
456 `_' - underscore -- done, completed
457 `&' - ampersand -- addendum, furthermore
459 \(Some other non-plain bullets have special meaning to the
460 software. By default:
462 `~' marks encryptable topics -- see `allout-topic-encryption-bullet'
463 `#' marks auto-numbered bullets -- see `allout-numbered-bullet'.)
465 See `allout-plain-bullets-string' for the standard, alternating
466 bullets.
468 You must run `set-allout-regexp' in order for outline mode to
469 adopt changes of this value.
471 DO NOT include the close-square-bracket, `]', on either of the bullet
472 strings."
473 :type 'string
474 :group 'allout)
475 (make-variable-buffer-local 'allout-distinctive-bullets-string)
476 ;;;###autoload
477 (put 'allout-distinctive-bullets-string 'safe-local-variable 'stringp)
479 ;;;_ = allout-use-mode-specific-leader
480 (defcustom allout-use-mode-specific-leader t
481 "*When non-nil, use mode-specific topic-header prefixes.
483 Allout outline mode will use the mode-specific `allout-mode-leaders' or
484 comment-start string, if any, to lead the topic prefix string, so topic
485 headers look like comments in the programming language. It will also use
486 the comment-start string, with an '_' appended, for `allout-primary-bullet'.
488 String values are used as literals, not regular expressions, so
489 do not escape any regulare-expression characters.
491 Value t means to first check for assoc value in `allout-mode-leaders'
492 alist, then use comment-start string, if any, then use default (`.').
493 \(See note about use of comment-start strings, below.)
495 Set to the symbol for either of `allout-mode-leaders' or
496 `comment-start' to use only one of them, respectively.
498 Value nil means to always use the default (`.') and leave
499 `allout-primary-bullet' unaltered.
501 comment-start strings that do not end in spaces are tripled in
502 the header-prefix, and an `_' underscore is tacked on the end, to
503 distinguish them from regular comment strings. comment-start
504 strings that do end in spaces are not tripled, but an underscore
505 is substituted for the space. [This presumes that the space is
506 for appearance, not comment syntax. You can use
507 `allout-mode-leaders' to override this behavior, when
508 undesired.]"
509 :type '(choice (const t) (const nil) string
510 (const allout-mode-leaders)
511 (const comment-start))
512 :group 'allout)
513 ;;;###autoload
514 (put 'allout-use-mode-specific-leader 'safe-local-variable
515 '(lambda (x) (or (memq x '(t nil allout-mode-leaders comment-start))
516 (stringp x))))
517 ;;;_ = allout-mode-leaders
518 (defvar allout-mode-leaders '()
519 "Specific allout-prefix leading strings per major modes.
521 Use this if the mode's comment-start string isn't what you
522 prefer, or if the mode lacks a comment-start string. See
523 `allout-use-mode-specific-leader' for more details.
525 If you're constructing a string that will comment-out outline
526 structuring so it can be included in program code, append an extra
527 character, like an \"_\" underscore, to distinguish the lead string
528 from regular comments that start at the beginning-of-line.")
530 ;;;_ = allout-old-style-prefixes
531 (defcustom allout-old-style-prefixes nil
532 "*When non-nil, use only old-and-crusty `outline-mode' `*' topic prefixes.
534 Non-nil restricts the topic creation and modification
535 functions to asterix-padded prefixes, so they look exactly
536 like the original Emacs-outline style prefixes.
538 Whatever the setting of this variable, both old and new style prefixes
539 are always respected by the topic maneuvering functions."
540 :type 'boolean
541 :group 'allout)
542 (make-variable-buffer-local 'allout-old-style-prefixes)
543 ;;;###autoload
544 (put 'allout-old-style-prefixes 'safe-local-variable
545 (if (fboundp 'booleanp) 'booleanp '(lambda (x) (member x '(t nil)))))
546 ;;;_ = allout-stylish-prefixes -- alternating bullets
547 (defcustom allout-stylish-prefixes t
548 "*Do fancy stuff with topic prefix bullets according to level, etc.
550 Non-nil enables topic creation, modification, and repositioning
551 functions to vary the topic bullet char (the char that marks the topic
552 depth) just preceding the start of the topic text) according to level.
553 Otherwise, only asterisks (`*') and distinctive bullets are used.
555 This is how an outline can look (but sans indentation) with stylish
556 prefixes:
558 * Top level
559 .* A topic
560 . + One level 3 subtopic
561 . . One level 4 subtopic
562 . . A second 4 subtopic
563 . + Another level 3 subtopic
564 . #1 A numbered level 4 subtopic
565 . #2 Another
566 . ! Another level 4 subtopic with a different distinctive bullet
567 . #4 And another numbered level 4 subtopic
569 This would be an outline with stylish prefixes inhibited (but the
570 numbered and other distinctive bullets retained):
572 * Top level
573 .* A topic
574 . * One level 3 subtopic
575 . * One level 4 subtopic
576 . * A second 4 subtopic
577 . * Another level 3 subtopic
578 . #1 A numbered level 4 subtopic
579 . #2 Another
580 . ! Another level 4 subtopic with a different distinctive bullet
581 . #4 And another numbered level 4 subtopic
583 Stylish and constant prefixes (as well as old-style prefixes) are
584 always respected by the topic maneuvering functions, regardless of
585 this variable setting.
587 The setting of this var is not relevant when `allout-old-style-prefixes'
588 is non-nil."
589 :type 'boolean
590 :group 'allout)
591 (make-variable-buffer-local 'allout-stylish-prefixes)
592 ;;;###autoload
593 (put 'allout-stylish-prefixes 'safe-local-variable
594 (if (fboundp 'booleanp) 'booleanp '(lambda (x) (member x '(t nil)))))
596 ;;;_ = allout-numbered-bullet
597 (defcustom allout-numbered-bullet "#"
598 "*String designating bullet of topics that have auto-numbering; nil for none.
600 Topics having this bullet have automatic maintenance of a sibling
601 sequence-number tacked on, just after the bullet. Conventionally set
602 to \"#\", you can set it to a bullet of your choice. A nil value
603 disables numbering maintenance."
604 :type '(choice (const nil) string)
605 :group 'allout)
606 (make-variable-buffer-local 'allout-numbered-bullet)
607 ;;;###autoload
608 (put 'allout-numbered-bullet 'safe-local-variable
609 (if (fboundp 'string-or-null-p)
610 'string-or-null-p
611 '(lambda (x) (or (stringp x) (null x)))))
612 ;;;_ = allout-file-xref-bullet
613 (defcustom allout-file-xref-bullet "@"
614 "*Bullet signifying file cross-references, for `allout-resolve-xref'.
616 Set this var to the bullet you want to use for file cross-references."
617 :type '(choice (const nil) string)
618 :group 'allout)
619 ;;;###autoload
620 (put 'allout-file-xref-bullet 'safe-local-variable
621 (if (fboundp 'string-or-null-p)
622 'string-or-null-p
623 '(lambda (x) (or (stringp x) (null x)))))
624 ;;;_ = allout-presentation-padding
625 (defcustom allout-presentation-padding 2
626 "*Presentation-format white-space padding factor, for greater indent."
627 :type 'integer
628 :group 'allout)
630 (make-variable-buffer-local 'allout-presentation-padding)
631 ;;;###autoload
632 (put 'allout-presentation-padding 'safe-local-variable 'integerp)
634 ;;;_ = allout-abbreviate-flattened-numbering
635 (defcustom allout-abbreviate-flattened-numbering nil
636 "*If non-nil, `allout-flatten-exposed-to-buffer' abbreviates topic
637 numbers to minimal amount with some context. Otherwise, entire
638 numbers are always used."
639 :type 'boolean
640 :group 'allout)
642 ;;;_ + LaTeX formatting
643 ;;;_ - allout-number-pages
644 (defcustom allout-number-pages nil
645 "*Non-nil turns on page numbering for LaTeX formatting of an outline."
646 :type 'boolean
647 :group 'allout)
648 ;;;_ - allout-label-style
649 (defcustom allout-label-style "\\large\\bf"
650 "*Font and size of labels for LaTeX formatting of an outline."
651 :type 'string
652 :group 'allout)
653 ;;;_ - allout-head-line-style
654 (defcustom allout-head-line-style "\\large\\sl "
655 "*Font and size of entries for LaTeX formatting of an outline."
656 :type 'string
657 :group 'allout)
658 ;;;_ - allout-body-line-style
659 (defcustom allout-body-line-style " "
660 "*Font and size of entries for LaTeX formatting of an outline."
661 :type 'string
662 :group 'allout)
663 ;;;_ - allout-title-style
664 (defcustom allout-title-style "\\Large\\bf"
665 "*Font and size of titles for LaTeX formatting of an outline."
666 :type 'string
667 :group 'allout)
668 ;;;_ - allout-title
669 (defcustom allout-title '(or buffer-file-name (buffer-name))
670 "*Expression to be evaluated to determine the title for LaTeX
671 formatted copy."
672 :type 'sexp
673 :group 'allout)
674 ;;;_ - allout-line-skip
675 (defcustom allout-line-skip ".05cm"
676 "*Space between lines for LaTeX formatting of an outline."
677 :type 'string
678 :group 'allout)
679 ;;;_ - allout-indent
680 (defcustom allout-indent ".3cm"
681 "*LaTeX formatted depth-indent spacing."
682 :type 'string
683 :group 'allout)
685 ;;;_ + Topic encryption
686 ;;;_ = allout-encryption group
687 (defgroup allout-encryption nil
688 "Settings for topic encryption features of allout outliner."
689 :group 'allout)
690 ;;;_ = allout-topic-encryption-bullet
691 (defcustom allout-topic-encryption-bullet "~"
692 "*Bullet signifying encryption of the entry's body."
693 :type '(choice (const nil) string)
694 :version "22.1"
695 :group 'allout-encryption)
696 ;;;_ = allout-passphrase-verifier-handling
697 (defcustom allout-passphrase-verifier-handling t
698 "*Enable use of symmetric encryption passphrase verifier if non-nil.
700 See the docstring for the `allout-enable-file-variable-adjustment'
701 variable for details about allout ajustment of file variables."
702 :type 'boolean
703 :version "22.1"
704 :group 'allout-encryption)
705 (make-variable-buffer-local 'allout-passphrase-verifier-handling)
706 ;;;_ = allout-passphrase-hint-handling
707 (defcustom allout-passphrase-hint-handling 'always
708 "*Dictate outline encryption passphrase reminder handling:
710 always -- always show reminder when prompting
711 needed -- show reminder on passphrase entry failure
712 disabled -- never present or adjust reminder
714 See the docstring for the `allout-enable-file-variable-adjustment'
715 variable for details about allout ajustment of file variables."
716 :type '(choice (const always)
717 (const needed)
718 (const disabled))
719 :version "22.1"
720 :group 'allout-encryption)
721 (make-variable-buffer-local 'allout-passphrase-hint-handling)
722 ;;;_ = allout-encrypt-unencrypted-on-saves
723 (defcustom allout-encrypt-unencrypted-on-saves t
724 "*When saving, should topics pending encryption be encrypted?
726 The idea is to prevent file-system exposure of any un-encrypted stuff, and
727 mostly covers both deliberate file writes and auto-saves.
729 - Yes: encrypt all topics pending encryption, even if it's the one
730 currently being edited. (In that case, the currently edited topic
731 will be automatically decrypted before any user interaction, so they
732 can continue editing but the copy on the file system will be
733 encrypted.)
734 Auto-saves will use the \"All except current topic\" mode if this
735 one is selected, to avoid practical difficulties -- see below.
736 - All except current topic: skip the topic currently being edited, even if
737 it's pending encryption. This may expose the current topic on the
738 file sytem, but avoids the nuisance of prompts for the encryption
739 passphrase in the middle of editing for, eg, autosaves.
740 This mode is used for auto-saves for both this option and \"Yes\".
741 - No: leave it to the user to encrypt any unencrypted topics.
743 For practical reasons, auto-saves always use the 'except-current policy
744 when auto-encryption is enabled. (Otherwise, spurious passphrase prompts
745 and unavoidable timing collisions are too disruptive.) If security for a
746 file requires that even the current topic is never auto-saved in the clear,
747 disable auto-saves for that file."
749 :type '(choice (const :tag "Yes" t)
750 (const :tag "All except current topic" except-current)
751 (const :tag "No" nil))
752 :version "22.1"
753 :group 'allout-encryption)
754 (make-variable-buffer-local 'allout-encrypt-unencrypted-on-saves)
756 ;;;_ + Developer
757 ;;;_ = allout-developer group
758 (defgroup allout-developer nil
759 "Settings for topic encryption features of allout outliner."
760 :group 'allout)
761 ;;;_ = allout-run-unit-tests-on-load
762 (defcustom allout-run-unit-tests-on-load nil
763 "*When non-nil, unit tests will be run at end of loading the allout module.
765 Generally, allout code developers are the only ones who'll want to set this.
767 \(If set, this makes it an even better practice to exercise changes by
768 doing byte-compilation with a repeat count, so the file is loaded after
769 compilation.)
771 See `allout-run-unit-tests' to see what's run."
772 :type 'boolean
773 :group 'allout-developer)
775 ;;;_ + Miscellaneous customization
777 ;;;_ = allout-enable-file-variable-adjustment
778 (defcustom allout-enable-file-variable-adjustment t
779 "*If non-nil, some allout outline actions edit Emacs local file var text.
781 This can range from changes to existing entries, addition of new ones,
782 and creation of a new local variables section when necessary.
784 Emacs file variables adjustments are also inhibited if `enable-local-variables'
785 is nil.
787 Operations potentially causing edits include allout encryption routines.
788 For details, see `allout-toggle-current-subtree-encryption's docstring."
789 :type 'boolean
790 :group 'allout)
791 (make-variable-buffer-local 'allout-enable-file-variable-adjustment)
793 ;;;_* CODE -- no user customizations below.
795 ;;;_ #1 Internal Outline Formatting and Configuration
796 ;;;_ : Version
797 ;;;_ = allout-version
798 (defvar allout-version "2.2.1"
799 "Version of currently loaded outline package. (allout.el)")
800 ;;;_ > allout-version
801 (defun allout-version (&optional here)
802 "Return string describing the loaded outline version."
803 (interactive "P")
804 (let ((msg (concat "Allout Outline Mode v " allout-version)))
805 (if here (insert msg))
806 (message "%s" msg)
807 msg))
808 ;;;_ : Mode activation (defined here because it's referenced early)
809 ;;;_ = allout-mode
810 (defvar allout-mode nil "Allout outline mode minor-mode flag.")
811 (make-variable-buffer-local 'allout-mode)
812 ;;;_ = allout-layout nil
813 (defvar allout-layout nil ; LEAVE GLOBAL VALUE NIL -- see docstring.
814 "Buffer-specific setting for allout layout.
816 In buffers where this is non-nil (and if `allout-init' has been run, to
817 enable this behavior), `allout-mode' will be automatically activated. The
818 layout dictated by the value will be used to set the initial exposure when
819 `allout-mode' is activated.
821 \*You should not setq-default this variable non-nil unless you want every
822 visited file to be treated as an allout file.*
824 The value would typically be set by a file local variable. For
825 example, the following lines at the bottom of an Emacs Lisp file:
827 ;;;Local variables:
828 ;;;allout-layout: (0 : -1 -1 0)
829 ;;;End:
831 dictate activation of `allout-mode' mode when the file is visited
832 \(presuming allout-init was already run), followed by the
833 equivalent of `(allout-expose-topic 0 : -1 -1 0)'. (This is
834 the layout used for the allout.el source file.)
836 `allout-default-layout' describes the specification format.
837 `allout-layout' can additionally have the value `t', in which
838 case the value of `allout-default-layout' is used.")
839 (make-variable-buffer-local 'allout-layout)
840 ;;;###autoload
841 (put 'allout-layout 'safe-local-variable
842 '(lambda (x) (or (numberp x) (listp x) (memq x '(: * + -)))))
844 ;;;_ : Topic header format
845 ;;;_ = allout-regexp
846 (defvar allout-regexp ""
847 "*Regular expression to match the beginning of a heading line.
849 Any line whose beginning matches this regexp is considered a
850 heading. This var is set according to the user configuration vars
851 by `set-allout-regexp'.")
852 (make-variable-buffer-local 'allout-regexp)
853 ;;;_ = allout-bullets-string
854 (defvar allout-bullets-string ""
855 "A string dictating the valid set of outline topic bullets.
857 This var should *not* be set by the user -- it is set by `set-allout-regexp',
858 and is produced from the elements of `allout-plain-bullets-string'
859 and `allout-distinctive-bullets-string'.")
860 (make-variable-buffer-local 'allout-bullets-string)
861 ;;;_ = allout-bullets-string-len
862 (defvar allout-bullets-string-len 0
863 "Length of current buffers' `allout-plain-bullets-string'.")
864 (make-variable-buffer-local 'allout-bullets-string-len)
865 ;;;_ = allout-depth-specific-regexp
866 (defvar allout-depth-specific-regexp ""
867 "*Regular expression to match a heading line prefix for a particular depth.
869 This expression is used to search for depth-specific topic
870 headers at depth 2 and greater. Use `allout-depth-one-regexp'
871 for to seek topics at depth one.
873 This var is set according to the user configuration vars by
874 `set-allout-regexp'. It is prepared with format strings for two
875 decimal numbers, which should each be one less than the depth of the
876 topic prefix to be matched.")
877 (make-variable-buffer-local 'allout-depth-specific-regexp)
878 ;;;_ = allout-depth-one-regexp
879 (defvar allout-depth-one-regexp ""
880 "*Regular expression to match a heading line prefix for depth one.
882 This var is set according to the user configuration vars by
883 `set-allout-regexp'. It is prepared with format strings for two
884 decimal numbers, which should each be one less than the depth of the
885 topic prefix to be matched.")
886 (make-variable-buffer-local 'allout-depth-one-regexp)
887 ;;;_ = allout-line-boundary-regexp
888 (defvar allout-line-boundary-regexp ()
889 "`allout-regexp' prepended with a newline for the search target.
891 This is properly set by `set-allout-regexp'.")
892 (make-variable-buffer-local 'allout-line-boundary-regexp)
893 ;;;_ = allout-bob-regexp
894 (defvar allout-bob-regexp ()
895 "Like `allout-line-boundary-regexp', for headers at beginning of buffer.")
896 (make-variable-buffer-local 'allout-bob-regexp)
897 ;;;_ = allout-header-subtraction
898 (defvar allout-header-subtraction (1- (length allout-header-prefix))
899 "Allout-header prefix length to subtract when computing topic depth.")
900 (make-variable-buffer-local 'allout-header-subtraction)
901 ;;;_ = allout-plain-bullets-string-len
902 (defvar allout-plain-bullets-string-len (length allout-plain-bullets-string)
903 "Length of `allout-plain-bullets-string', updated by `set-allout-regexp'.")
904 (make-variable-buffer-local 'allout-plain-bullets-string-len)
906 ;;;_ = allout-doublecheck-at-and-shallower
907 (defconst allout-doublecheck-at-and-shallower 3
908 "Validate apparent topics of this depth and shallower as being non-aberrant.
910 Verified with `allout-aberrant-container-p'. The usefulness of
911 this check is limited to shallow depths, because the
912 determination of aberrance is according to the mistaken item
913 being followed by a legitimate item of excessively greater depth.
915 The classic example of a mistaken item, for a standard allout
916 outline configuration, is a body line that begins with an '...'
917 ellipsis. This happens to contain a legitimate depth-2 header
918 prefix, constituted by two '..' dots at the beginning of the
919 line. The only thing that can distinguish it *in principle* from
920 a legitimate one is if the following real header is at a depth
921 that is discontinuous from the depth of 2 implied by the
922 ellipsis, ie depth 4 or more. As the depth being tested gets
923 greater, the likelihood of this kind of disqualification is
924 lower, and the usefulness of this test is lower.
926 Extending the depth of the doublecheck increases the amount it is
927 applied, increasing the cost of the test - on casual estimation,
928 for outlines with many deep topics, geometrically (O(n)?).
929 Taken together with decreasing likelihood that the test will be
930 useful at greater depths, more modest doublecheck limits are more
931 suitably economical.")
932 ;;;_ X allout-reset-header-lead (header-lead)
933 (defun allout-reset-header-lead (header-lead)
934 "*Reset the leading string used to identify topic headers."
935 (interactive "sNew lead string: ")
936 (setq allout-header-prefix header-lead)
937 (setq allout-header-subtraction (1- (length allout-header-prefix)))
938 (set-allout-regexp))
939 ;;;_ X allout-lead-with-comment-string (header-lead)
940 (defun allout-lead-with-comment-string (&optional header-lead)
941 "*Set the topic-header leading string to specified string.
943 Useful when for encapsulating outline structure in programming
944 language comments. Returns the leading string."
946 (interactive "P")
947 (if (not (stringp header-lead))
948 (setq header-lead (read-string
949 "String prefix for topic headers: ")))
950 (setq allout-reindent-bodies nil)
951 (allout-reset-header-lead header-lead)
952 header-lead)
953 ;;;_ > allout-infer-header-lead-and-primary-bullet ()
954 (defun allout-infer-header-lead-and-primary-bullet ()
955 "Determine appropriate `allout-header-prefix' and `allout-primary-bullet'.
957 Works according to settings of:
959 `comment-start'
960 `allout-header-prefix' (default)
961 `allout-use-mode-specific-leader'
962 and `allout-mode-leaders'.
964 Apply this via (re)activation of `allout-mode', rather than
965 invoking it directly."
966 (let* ((use-leader (and (boundp 'allout-use-mode-specific-leader)
967 (if (or (stringp allout-use-mode-specific-leader)
968 (memq allout-use-mode-specific-leader
969 '(allout-mode-leaders
970 comment-start
971 t)))
972 allout-use-mode-specific-leader
973 ;; Oops -- garbled value, equate with effect of t:
974 t)))
975 (leader
976 (cond
977 ((not use-leader) nil)
978 ;; Use the explicitly designated leader:
979 ((stringp use-leader) use-leader)
980 (t (or (and (memq use-leader '(t allout-mode-leaders))
981 ;; Get it from outline mode leaders?
982 (cdr (assq major-mode allout-mode-leaders)))
983 ;; ... didn't get from allout-mode-leaders...
984 (and (memq use-leader '(t comment-start))
985 comment-start
986 ;; Use comment-start, maybe tripled, and with
987 ;; underscore:
988 (concat
989 (if (string= " "
990 (substring comment-start
991 (1- (length comment-start))))
992 ;; Use comment-start, sans trailing space:
993 (substring comment-start 0 -1)
994 (concat comment-start comment-start comment-start))
995 ;; ... and append underscore, whichever:
996 "_")))))))
997 (if (not leader)
999 (setq allout-header-prefix leader)
1000 (if (not allout-old-style-prefixes)
1001 ;; setting allout-primary-bullet makes the top level topics use --
1002 ;; actually, be -- the special prefix:
1003 (setq allout-primary-bullet leader))
1004 allout-header-prefix)))
1005 (defalias 'allout-infer-header-lead
1006 'allout-infer-header-lead-and-primary-bullet)
1007 ;;;_ > allout-infer-body-reindent ()
1008 (defun allout-infer-body-reindent ()
1009 "Determine proper setting for `allout-reindent-bodies'.
1011 Depends on default setting of `allout-reindent-bodies' (which see)
1012 and presence of setting for `comment-start', to tell whether the
1013 file is programming code."
1014 (if (and allout-reindent-bodies
1015 comment-start
1016 (not (eq 'force allout-reindent-bodies)))
1017 (setq allout-reindent-bodies nil)))
1018 ;;;_ > set-allout-regexp ()
1019 (defun set-allout-regexp ()
1020 "Generate proper topic-header regexp form for outline functions.
1022 Works with respect to `allout-plain-bullets-string' and
1023 `allout-distinctive-bullets-string'.
1025 Also refresh various data structures that hinge on the regexp."
1027 (interactive)
1028 ;; Derive allout-bullets-string from user configured components:
1029 (setq allout-bullets-string "")
1030 (let ((strings (list 'allout-plain-bullets-string
1031 'allout-distinctive-bullets-string
1032 'allout-primary-bullet))
1033 cur-string
1034 cur-len
1035 cur-char
1036 index)
1037 (while strings
1038 (setq index 0)
1039 (setq cur-len (length (setq cur-string (symbol-value (car strings)))))
1040 (while (< index cur-len)
1041 (setq cur-char (aref cur-string index))
1042 (setq allout-bullets-string
1043 (concat allout-bullets-string
1044 (cond
1045 ; Single dash would denote a
1046 ; sequence, repeated denotes
1047 ; a dash:
1048 ((eq cur-char ?-) "--")
1049 ; literal close-square-bracket
1050 ; doesn't work right in the
1051 ; expr, exclude it:
1052 ((eq cur-char ?\]) "")
1053 (t (regexp-quote (char-to-string cur-char))))))
1054 (setq index (1+ index)))
1055 (setq strings (cdr strings)))
1057 ;; Derive next for repeated use in allout-pending-bullet:
1058 (setq allout-plain-bullets-string-len (length allout-plain-bullets-string))
1059 (setq allout-header-subtraction (1- (length allout-header-prefix)))
1061 (let (new-part old-part formfeed-part)
1062 (setq new-part (concat "\\("
1063 (regexp-quote allout-header-prefix)
1064 "[ \t]*"
1065 ;; already regexp-quoted in a custom way:
1066 "[" allout-bullets-string "]"
1067 "\\)")
1068 old-part (concat "\\("
1069 (regexp-quote allout-primary-bullet)
1070 "\\|"
1071 (regexp-quote allout-header-prefix)
1072 "\\)"
1074 " ?[^" allout-primary-bullet "]")
1075 formfeed-part "\\(\^L\\)"
1077 allout-regexp (concat new-part
1078 "\\|"
1079 old-part
1080 "\\|"
1081 formfeed-part)
1083 allout-line-boundary-regexp (concat "\n" new-part
1084 "\\|"
1085 "\n" old-part
1086 "\\|"
1087 "\n" formfeed-part)
1089 allout-bob-regexp (concat "\\`" new-part
1090 "\\|"
1091 "\\`" old-part
1092 "\\|"
1093 "\\`" formfeed-part
1096 (setq allout-depth-specific-regexp
1097 (concat "\\(^\\|\\`\\)"
1098 "\\("
1100 ;; new-style spacers-then-bullet string:
1101 "\\("
1102 (allout-format-quote (regexp-quote allout-header-prefix))
1103 " \\{%s\\}"
1104 "[" (allout-format-quote allout-bullets-string) "]"
1105 "\\)"
1107 ;; old-style all-bullets string, if primary not multi-char:
1108 (if (< 0 allout-header-subtraction)
1110 (concat "\\|\\("
1111 (allout-format-quote
1112 (regexp-quote allout-primary-bullet))
1113 (allout-format-quote
1114 (regexp-quote allout-primary-bullet))
1115 (allout-format-quote
1116 (regexp-quote allout-primary-bullet))
1117 "\\{%s\\}"
1118 ;; disqualify greater depths:
1119 "[^"
1120 (allout-format-quote allout-primary-bullet)
1121 "]\\)"
1123 "\\)"
1125 (setq allout-depth-one-regexp
1126 (concat "\\(^\\|\\`\\)"
1127 "\\("
1129 "\\("
1130 (regexp-quote allout-header-prefix)
1131 ;; disqualify any bullet char following any amount of
1132 ;; intervening whitespace:
1133 " *"
1134 (concat "[^ " allout-bullets-string "]")
1135 "\\)"
1136 (if (< 0 allout-header-subtraction)
1137 ;; Need not support anything like the old
1138 ;; bullet style if the prefix is multi-char.
1140 (concat "\\|"
1141 (regexp-quote allout-primary-bullet)
1142 ;; disqualify deeper primary-bullet sequences:
1143 "[^" allout-primary-bullet "]"))
1144 "\\)"
1145 ))))
1146 ;;;_ : Key bindings
1147 ;;;_ = allout-mode-map
1148 (defvar allout-mode-map nil "Keybindings for (allout) outline minor mode.")
1149 ;;;_ > produce-allout-mode-map (keymap-alist &optional base-map)
1150 (defun produce-allout-mode-map (keymap-list &optional base-map)
1151 "Produce keymap for use as `allout-mode-map', from KEYMAP-LIST.
1153 Built on top of optional BASE-MAP, or empty sparse map if none specified.
1154 See doc string for `allout-keybindings-list' for format of binding list."
1155 (let ((map (or base-map (make-sparse-keymap)))
1156 (pref (list allout-command-prefix)))
1157 (mapc (function
1158 (lambda (cell)
1159 (let ((add-pref (null (cdr (cdr cell))))
1160 (key-suff (list (car cell))))
1161 (apply 'define-key
1162 (list map
1163 (apply 'vconcat (if add-pref
1164 (append pref key-suff)
1165 key-suff))
1166 (car (cdr cell)))))))
1167 keymap-list)
1168 map))
1169 ;;;_ : Menu bar
1170 (defvar allout-mode-exposure-menu)
1171 (defvar allout-mode-editing-menu)
1172 (defvar allout-mode-navigation-menu)
1173 (defvar allout-mode-misc-menu)
1174 (defun produce-allout-mode-menubar-entries ()
1175 (require 'easymenu)
1176 (easy-menu-define allout-mode-exposure-menu
1177 allout-mode-map
1178 "Allout outline exposure menu."
1179 '("Exposure"
1180 ["Show Entry" allout-show-current-entry t]
1181 ["Show Children" allout-show-children t]
1182 ["Show Subtree" allout-show-current-subtree t]
1183 ["Hide Subtree" allout-hide-current-subtree t]
1184 ["Hide Leaves" allout-hide-current-leaves t]
1185 "----"
1186 ["Show All" allout-show-all t]))
1187 (easy-menu-define allout-mode-editing-menu
1188 allout-mode-map
1189 "Allout outline editing menu."
1190 '("Headings"
1191 ["Open Sibling" allout-open-sibtopic t]
1192 ["Open Subtopic" allout-open-subtopic t]
1193 ["Open Supertopic" allout-open-supertopic t]
1194 "----"
1195 ["Shift Topic In" allout-shift-in t]
1196 ["Shift Topic Out" allout-shift-out t]
1197 ["Rebullet Topic" allout-rebullet-topic t]
1198 ["Rebullet Heading" allout-rebullet-current-heading t]
1199 ["Number Siblings" allout-number-siblings t]
1200 "----"
1201 ["Toggle Topic Encryption"
1202 allout-toggle-current-subtree-encryption
1203 (> (allout-current-depth) 1)]))
1204 (easy-menu-define allout-mode-navigation-menu
1205 allout-mode-map
1206 "Allout outline navigation menu."
1207 '("Navigation"
1208 ["Next Visible Heading" allout-next-visible-heading t]
1209 ["Previous Visible Heading"
1210 allout-previous-visible-heading t]
1211 "----"
1212 ["Up Level" allout-up-current-level t]
1213 ["Forward Current Level" allout-forward-current-level t]
1214 ["Backward Current Level"
1215 allout-backward-current-level t]
1216 "----"
1217 ["Beginning of Entry"
1218 allout-beginning-of-current-entry t]
1219 ["End of Entry" allout-end-of-entry t]
1220 ["End of Subtree" allout-end-of-current-subtree t]))
1221 (easy-menu-define allout-mode-misc-menu
1222 allout-mode-map
1223 "Allout outlines miscellaneous bindings."
1224 '("Misc"
1225 ["Version" allout-version t]
1226 "----"
1227 ["Duplicate Exposed" allout-copy-exposed-to-buffer t]
1228 ["Duplicate Exposed, numbered"
1229 allout-flatten-exposed-to-buffer t]
1230 ["Duplicate Exposed, indented"
1231 allout-indented-exposed-to-buffer t]
1232 "----"
1233 ["Set Header Lead" allout-reset-header-lead t]
1234 ["Set New Exposure" allout-expose-topic t])))
1235 ;;;_ : Allout Modal-Variables Utilities
1236 ;;;_ = allout-mode-prior-settings
1237 (defvar allout-mode-prior-settings nil
1238 "Internal `allout-mode' use; settings to be resumed on mode deactivation.
1240 See `allout-add-resumptions' and `allout-do-resumptions'.")
1241 (make-variable-buffer-local 'allout-mode-prior-settings)
1242 ;;;_ > allout-add-resumptions (&rest pairs)
1243 (defun allout-add-resumptions (&rest pairs)
1244 "Set name/value PAIRS.
1246 Old settings are preserved for later resumption using `allout-do-resumptions'.
1248 The new values are set as a buffer local. On resumption, the prior buffer
1249 scope of the variable is restored along with its value. If it was a void
1250 buffer-local value, then it is left as nil on resumption.
1252 The pairs are lists whose car is the name of the variable and car of the
1253 cdr is the new value: '(some-var some-value)'. The pairs can actually be
1254 triples, where the third element qualifies the disposition of the setting,
1255 as described further below.
1257 If the optional third element is the symbol 'extend, then the new value
1258 created by `cons'ing the second element of the pair onto the front of the
1259 existing value.
1261 If the optional third element is the symbol 'append, then the new value is
1262 extended from the existing one by `append'ing a list containing the second
1263 element of the pair onto the end of the existing value.
1265 Extension, and resumptions in general, should not be used for hook
1266 functions -- use the 'local mode of `add-hook' for that, instead.
1268 The settings are stored on `allout-mode-prior-settings'."
1269 (while pairs
1270 (let* ((pair (pop pairs))
1271 (name (car pair))
1272 (value (cadr pair))
1273 (qualifier (if (> (length pair) 2)
1274 (caddr pair)))
1275 prior-value)
1276 (if (not (symbolp name))
1277 (error "Pair's name, %S, must be a symbol, not %s"
1278 name (type-of name)))
1279 (setq prior-value (condition-case nil
1280 (symbol-value name)
1281 (void-variable nil)))
1282 (when (not (assoc name allout-mode-prior-settings))
1283 ;; Not already added as a resumption, create the prior setting entry.
1284 (if (local-variable-p name)
1285 ;; is already local variable -- preserve the prior value:
1286 (push (list name prior-value) allout-mode-prior-settings)
1287 ;; wasn't local variable, indicate so for resumption by killing
1288 ;; local value, and make it local:
1289 (push (list name) allout-mode-prior-settings)
1290 (make-local-variable name)))
1291 (if qualifier
1292 (cond ((eq qualifier 'extend)
1293 (if (not (listp prior-value))
1294 (error "extension of non-list prior value attempted")
1295 (set name (cons value prior-value))))
1296 ((eq qualifier 'append)
1297 (if (not (listp prior-value))
1298 (error "appending of non-list prior value attempted")
1299 (set name (append prior-value (list value)))))
1300 (t (error "unrecognized setting qualifier `%s' encountered"
1301 qualifier)))
1302 (set name value)))))
1303 ;;;_ > allout-do-resumptions ()
1304 (defun allout-do-resumptions ()
1305 "Resume all name/value settings registered by `allout-add-resumptions'.
1307 This is used when concluding allout-mode, to resume selected variables to
1308 their settings before allout-mode was started."
1310 (while allout-mode-prior-settings
1311 (let* ((pair (pop allout-mode-prior-settings))
1312 (name (car pair))
1313 (value-cell (cdr pair)))
1314 (if (not value-cell)
1315 ;; Prior value was global:
1316 (kill-local-variable name)
1317 ;; Prior value was explicit:
1318 (set name (car value-cell))))))
1319 ;;;_ : Mode-specific incidentals
1320 ;;;_ > allout-unprotected (expr)
1321 (defmacro allout-unprotected (expr)
1322 "Enable internal outline operations to alter invisible text."
1323 `(let ((inhibit-read-only (if (not buffer-read-only) t))
1324 (inhibit-field-text-motion t))
1325 ,expr))
1326 ;;;_ = allout-mode-hook
1327 (defvar allout-mode-hook nil
1328 "*Hook that's run when allout mode starts.")
1329 ;;;_ = allout-mode-deactivate-hook
1330 (defvar allout-mode-deactivate-hook nil
1331 "*Hook that's run when allout mode ends.")
1332 ;;;_ = allout-exposure-category
1333 (defvar allout-exposure-category nil
1334 "Symbol for use as allout invisible-text overlay category.")
1335 ;;;_ x allout-view-change-hook
1336 (defvar allout-view-change-hook nil
1337 "*(Deprecated) A hook run after allout outline exposure changes.
1339 Switch to using `allout-exposure-change-hook' instead. Both hooks are
1340 currently respected, but the other conveys the details of the exposure
1341 change via explicit parameters, and this one will eventually be disabled in
1342 a subsequent allout version.")
1343 ;;;_ = allout-exposure-change-hook
1344 (defvar allout-exposure-change-hook nil
1345 "*Hook that's run after allout outline subtree exposure changes.
1347 It is run at the conclusion of `allout-flag-region'.
1349 Functions on the hook must take three arguments:
1351 - FROM -- integer indicating the point at the start of the change.
1352 - TO -- integer indicating the point of the end of the change.
1353 - FLAG -- change mode: nil for exposure, otherwise concealment.
1355 This hook might be invoked multiple times by a single command.
1357 This hook is replacing `allout-view-change-hook', which is being deprecated
1358 and eventually will not be invoked.")
1359 ;;;_ = allout-structure-added-hook
1360 (defvar allout-structure-added-hook nil
1361 "*Hook that's run after addition of items to the outline.
1363 Functions on the hook should take two arguments:
1365 - NEW-START -- integer indicating position of start of the first new item.
1366 - NEW-END -- integer indicating position of end of the last new item.
1368 Some edits that introduce new items may missed by this hook:
1369 specifically edits that native allout routines do not control.
1371 This hook might be invoked multiple times by a single command.")
1372 ;;;_ = allout-structure-deleted-hook
1373 (defvar allout-structure-deleted-hook nil
1374 "*Hook that's run after disciplined deletion of subtrees from the outline.
1376 Functions on the hook must take two arguments:
1378 - DEPTH -- integer indicating the depth of the subtree that was deleted.
1379 - REMOVED-FROM -- integer indicating the point where the subtree was removed.
1381 Some edits that remove or invalidate items may missed by this hook:
1382 specifically edits that native allout routines do not control.
1384 This hook might be invoked multiple times by a single command.")
1385 ;;;_ = allout-structure-shifted-hook
1386 (defvar allout-structure-shifted-hook nil
1387 "*Hook that's run after shifting of items in the outline.
1389 Functions on the hook should take two arguments:
1391 - DEPTH-CHANGE -- integer indicating depth increase, negative for decrease
1392 - START -- integer indicating the start point of the shifted parent item.
1394 Some edits that shift items can be missed by this hook: specifically edits
1395 that native allout routines do not control.
1397 This hook might be invoked multiple times by a single command.")
1398 ;;;_ = allout-outside-normal-auto-fill-function
1399 (defvar allout-outside-normal-auto-fill-function nil
1400 "Value of normal-auto-fill-function outside of allout mode.
1402 Used by allout-auto-fill to do the mandated normal-auto-fill-function
1403 wrapped within allout's automatic fill-prefix setting.")
1404 (make-variable-buffer-local 'allout-outside-normal-auto-fill-function)
1405 ;;;_ = file-var-bug hack
1406 (defvar allout-v18/19-file-var-hack nil
1407 "Horrible hack used to prevent invalid multiple triggering of outline
1408 mode from prop-line file-var activation. Used by `allout-mode' function
1409 to track repeats.")
1410 ;;;_ = allout-passphrase-verifier-string
1411 (defvar allout-passphrase-verifier-string nil
1412 "Setting used to test solicited encryption passphrases against the one
1413 already associated with a file.
1415 It consists of an encrypted random string useful only to verify that a
1416 passphrase entered by the user is effective for decryption. The passphrase
1417 itself is \*not* recorded in the file anywhere, and the encrypted contents
1418 are random binary characters to avoid exposing greater susceptibility to
1419 search attacks.
1421 The verifier string is retained as an Emacs file variable, as well as in
1422 the Emacs buffer state, if file variable adjustments are enabled. See
1423 `allout-enable-file-variable-adjustment' for details about that.")
1424 (make-variable-buffer-local 'allout-passphrase-verifier-string)
1425 ;;;###autoload
1426 (put 'allout-passphrase-verifier-string 'safe-local-variable 'stringp)
1427 ;;;_ = allout-passphrase-hint-string
1428 (defvar allout-passphrase-hint-string ""
1429 "Variable used to retain reminder string for file's encryption passphrase.
1431 See the description of `allout-passphrase-hint-handling' for details about how
1432 the reminder is deployed.
1434 The hint is retained as an Emacs file variable, as well as in the Emacs buffer
1435 state, if file variable adjustments are enabled. See
1436 `allout-enable-file-variable-adjustment' for details about that.")
1437 (make-variable-buffer-local 'allout-passphrase-hint-string)
1438 (setq-default allout-passphrase-hint-string "")
1439 ;;;###autoload
1440 (put 'allout-passphrase-hint-string 'safe-local-variable 'stringp)
1441 ;;;_ = allout-after-save-decrypt
1442 (defvar allout-after-save-decrypt nil
1443 "Internal variable, is nil or has the value of two points:
1445 - the location of a topic to be decrypted after saving is done
1446 - where to situate the cursor after the decryption is performed
1448 This is used to decrypt the topic that was currently being edited, if it
1449 was encrypted automatically as part of a file write or autosave.")
1450 (make-variable-buffer-local 'allout-after-save-decrypt)
1451 ;;;_ = allout-encryption-plaintext-sanitization-regexps
1452 (defvar allout-encryption-plaintext-sanitization-regexps nil
1453 "List of regexps whose matches are removed from plaintext before encryption.
1455 This is for the sake of removing artifacts, like escapes, that are added on
1456 and not actually part of the original plaintext. The removal is done just
1457 prior to encryption.
1459 Entries must be symbols that are bound to the desired values.
1461 Each value can be a regexp or a list with a regexp followed by a
1462 substitution string. If it's just a regexp, all its matches are removed
1463 before the text is encrypted. If it's a regexp and a substitution, the
1464 substition is used against the regexp matches, a la `replace-match'.")
1465 (make-variable-buffer-local 'allout-encryption-text-removal-regexps)
1466 ;;;_ = allout-encryption-ciphertext-rejection-regexps
1467 (defvar allout-encryption-ciphertext-rejection-regexps nil
1468 "Variable for regexps matching plaintext to remove before encryption.
1470 This is for the sake of redoing encryption in cases where the ciphertext
1471 incidentally contains strings that would disrupt mode operation --
1472 for example, a line that happens to look like an allout-mode topic prefix.
1474 Entries must be symbols that are bound to the desired regexp values.
1476 The encryption will be retried up to
1477 `allout-encryption-ciphertext-rejection-limit' times, after which an error
1478 is raised.")
1480 (make-variable-buffer-local 'allout-encryption-ciphertext-rejection-regexps)
1481 ;;;_ = allout-encryption-ciphertext-rejection-ceiling
1482 (defvar allout-encryption-ciphertext-rejection-ceiling 5
1483 "Limit on number of times encryption ciphertext is rejected.
1485 See `allout-encryption-ciphertext-rejection-regexps' for rejection reasons.")
1486 (make-variable-buffer-local 'allout-encryption-ciphertext-rejection-ceiling)
1487 ;;;_ > allout-mode-p ()
1488 ;; Must define this macro above any uses, or byte compilation will lack
1489 ;; proper def, if file isn't loaded -- eg, during emacs build!
1490 (defmacro allout-mode-p ()
1491 "Return t if `allout-mode' is active in current buffer."
1492 'allout-mode)
1493 ;;;_ > allout-write-file-hook-handler ()
1494 (defun allout-write-file-hook-handler ()
1495 "Implement `allout-encrypt-unencrypted-on-saves' policy for file writes."
1497 (if (or (not (allout-mode-p))
1498 (not (boundp 'allout-encrypt-unencrypted-on-saves))
1499 (not allout-encrypt-unencrypted-on-saves))
1501 (let ((except-mark (and (equal allout-encrypt-unencrypted-on-saves
1502 'except-current)
1503 (point-marker))))
1504 (if (save-excursion (goto-char (point-min))
1505 (allout-next-topic-pending-encryption except-mark))
1506 (progn
1507 (message "auto-encrypting pending topics")
1508 (sit-for 0)
1509 (condition-case failure
1510 (setq allout-after-save-decrypt
1511 (allout-encrypt-decrypted except-mark))
1512 (error (message
1513 "allout-write-file-hook-handler suppressing error %s"
1514 failure)
1515 (sit-for 2)))))
1517 nil)
1518 ;;;_ > allout-auto-save-hook-handler ()
1519 (defun allout-auto-save-hook-handler ()
1520 "Implement `allout-encrypt-unencrypted-on-saves' policy for auto save."
1522 (if (and (allout-mode-p) allout-encrypt-unencrypted-on-saves)
1523 ;; Always implement 'except-current policy when enabled.
1524 (let ((allout-encrypt-unencrypted-on-saves 'except-current))
1525 (allout-write-file-hook-handler))))
1526 ;;;_ > allout-after-saves-handler ()
1527 (defun allout-after-saves-handler ()
1528 "Decrypt topic encrypted for save, if it's currently being edited.
1530 Ie, if it was pending encryption and contained the point in its body before
1531 the save.
1533 We use values stored in `allout-after-save-decrypt' to locate the topic
1534 and the place for the cursor after the decryption is done."
1535 (if (not (and (allout-mode-p)
1536 (boundp 'allout-after-save-decrypt)
1537 allout-after-save-decrypt))
1539 (goto-char (car allout-after-save-decrypt))
1540 (let ((was-modified (buffer-modified-p)))
1541 (allout-toggle-subtree-encryption)
1542 (if (not was-modified)
1543 (set-buffer-modified-p nil)))
1544 (goto-char (cadr allout-after-save-decrypt))
1545 (setq allout-after-save-decrypt nil))
1547 ;;;_ = allout-inhibit-aberrance-doublecheck nil
1548 ;; In some exceptional moments, disparate topic depths need to be allowed
1549 ;; momentarily, eg when one topic is being yanked into another and they're
1550 ;; about to be reconciled. let-binding allout-inhibit-aberrance-doublecheck
1551 ;; prevents the aberrance doublecheck to allow, eg, the reconciliation
1552 ;; processing to happen in the presence of such discrepancies. It should
1553 ;; almost never be needed, however.
1554 (defvar allout-inhibit-aberrance-doublecheck nil
1555 "Internal state, for momentarily inhibits aberrance doublecheck.
1557 This should only be momentarily let-bound non-nil, not set
1558 non-nil in a lasting way.")
1560 ;;;_ #2 Mode activation
1561 ;;;_ = allout-explicitly-deactivated
1562 (defvar allout-explicitly-deactivated nil
1563 "If t, `allout-mode's last deactivation was deliberate.
1564 So `allout-post-command-business' should not reactivate it...")
1565 (make-variable-buffer-local 'allout-explicitly-deactivated)
1566 ;;;_ > allout-init (&optional mode)
1567 (defun allout-init (&optional mode)
1568 "Prime `allout-mode' to enable/disable auto-activation, wrt `allout-layout'.
1570 MODE is one of the following symbols:
1572 - nil (or no argument) deactivate auto-activation/layout;
1573 - `activate', enable auto-activation only;
1574 - `ask', enable auto-activation, and enable auto-layout but with
1575 confirmation for layout operation solicited from user each time;
1576 - `report', just report and return the current auto-activation state;
1577 - anything else (eg, t) for auto-activation and auto-layout, without
1578 any confirmation check.
1580 Use this function to setup your Emacs session for automatic activation
1581 of allout outline mode, contingent to the buffer-specific setting of
1582 the `allout-layout' variable. (See `allout-layout' and
1583 `allout-expose-topic' docstrings for more details on auto layout).
1585 `allout-init' works by setting up (or removing) the `allout-mode'
1586 find-file-hook, and giving `allout-auto-activation' a suitable
1587 setting.
1589 To prime your Emacs session for full auto-outline operation, include
1590 the following two lines in your Emacs init file:
1592 \(require 'allout)
1593 \(allout-init t)"
1595 (interactive)
1596 (if (interactive-p)
1597 (progn
1598 (setq mode
1599 (completing-read
1600 (concat "Select outline auto setup mode "
1601 "(empty for report, ? for options) ")
1602 '(("nil")("full")("activate")("deactivate")
1603 ("ask") ("report") (""))
1606 (if (string= mode "")
1607 (setq mode 'report)
1608 (setq mode (intern-soft mode)))))
1609 (let
1610 ;; convenience aliases, for consistent ref to respective vars:
1611 ((hook 'allout-find-file-hook)
1612 (find-file-hook-var-name (if (boundp 'find-file-hook)
1613 'find-file-hook
1614 'find-file-hooks))
1615 (curr-mode 'allout-auto-activation))
1617 (cond ((not mode)
1618 (set find-file-hook-var-name
1619 (delq hook (symbol-value find-file-hook-var-name)))
1620 (if (interactive-p)
1621 (message "Allout outline mode auto-activation inhibited.")))
1622 ((eq mode 'report)
1623 (if (not (memq hook (symbol-value find-file-hook-var-name)))
1624 (allout-init nil)
1625 ;; Just punt and use the reports from each of the modes:
1626 (allout-init (symbol-value curr-mode))))
1627 (t (add-hook find-file-hook-var-name hook)
1628 (set curr-mode ; `set', not `setq'!
1629 (cond ((eq mode 'activate)
1630 (message
1631 "Outline mode auto-activation enabled.")
1632 'activate)
1633 ((eq mode 'report)
1634 ;; Return the current mode setting:
1635 (allout-init mode))
1636 ((eq mode 'ask)
1637 (message
1638 (concat "Outline mode auto-activation and "
1639 "-layout (upon confirmation) enabled."))
1640 'ask)
1641 ((message
1642 "Outline mode auto-activation and -layout enabled.")
1643 'full)))))))
1644 ;;;_ > allout-setup-menubar ()
1645 (defun allout-setup-menubar ()
1646 "Populate the current buffer's menubar with `allout-mode' stuff."
1647 (let ((menus (list allout-mode-exposure-menu
1648 allout-mode-editing-menu
1649 allout-mode-navigation-menu
1650 allout-mode-misc-menu))
1651 cur)
1652 (while menus
1653 (setq cur (car menus)
1654 menus (cdr menus))
1655 (easy-menu-add cur))))
1656 ;;;_ > allout-overlay-preparations
1657 (defun allout-overlay-preparations ()
1658 "Set the properties of the allout invisible-text overlay and others."
1659 (setplist 'allout-exposure-category nil)
1660 (put 'allout-exposure-category 'invisible 'allout)
1661 (put 'allout-exposure-category 'evaporate t)
1662 ;; XXX We use isearch-open-invisible *and* isearch-mode-end-hook. The
1663 ;; latter would be sufficient, but it seems that a separate behavior --
1664 ;; the _transient_ opening of invisible text during isearch -- is keyed to
1665 ;; presence of the isearch-open-invisible property -- even though this
1666 ;; property controls the isearch _arrival_ behavior. This is the case at
1667 ;; least in emacs 21, 22.1, and xemacs 21.4.
1668 (put 'allout-exposure-category 'isearch-open-invisible
1669 'allout-isearch-end-handler)
1670 (if (featurep 'xemacs)
1671 (put 'allout-exposure-category 'start-open t)
1672 (put 'allout-exposure-category 'insert-in-front-hooks
1673 '(allout-overlay-insert-in-front-handler)))
1674 (put 'allout-exposure-category 'modification-hooks
1675 '(allout-overlay-interior-modification-handler)))
1676 ;;;_ > allout-mode (&optional toggle)
1677 ;;;_ : Defun:
1678 ;;;###autoload
1679 (defun allout-mode (&optional toggle)
1680 ;;;_ . Doc string:
1681 "Toggle minor mode for controlling exposure and editing of text outlines.
1682 \\<allout-mode-map>
1684 Optional prefix argument TOGGLE forces the mode to re-initialize
1685 if it is positive, otherwise it turns the mode off. Allout
1686 outline mode always runs as a minor mode.
1688 Allout outline mode provides extensive outline oriented formatting and
1689 manipulation. It enables structural editing of outlines, as well as
1690 navigation and exposure. It also is specifically aimed at
1691 accommodating syntax-sensitive text like programming languages. (For
1692 an example, see the allout code itself, which is organized as an allout
1693 outline.)
1695 In addition to typical outline navigation and exposure, allout includes:
1697 - topic-oriented authoring, including keystroke-based topic creation,
1698 repositioning, promotion/demotion, cut, and paste
1699 - incremental search with dynamic exposure and reconcealment of hidden text
1700 - adjustable format, so programming code can be developed in outline-structure
1701 - easy topic encryption and decryption
1702 - \"Hot-spot\" operation, for single-keystroke maneuvering and exposure control
1703 - integral outline layout, for automatic initial exposure when visiting a file
1704 - independent extensibility, using comprehensive exposure and authoring hooks
1706 and many other features.
1708 Below is a description of the key bindings, and then explanation of
1709 special `allout-mode' features and terminology. See also the outline
1710 menubar additions for quick reference to many of the features, and see
1711 the docstring of the function `allout-init' for instructions on
1712 priming your emacs session for automatic activation of `allout-mode'.
1714 The bindings are dictated by the customizable `allout-keybindings-list'
1715 variable. We recommend customizing `allout-command-prefix' to use just
1716 `\\C-c' as the command prefix, if the allout bindings don't conflict with
1717 any personal bindings you have on \\C-c. In any case, outline structure
1718 navigation and authoring is simplified by positioning the cursor on an
1719 item's bullet character, the \"hot-spot\" -- then you can invoke allout
1720 commands with just the un-prefixed, un-control-shifted command letters.
1721 This is described further in the HOT-SPOT Operation section.
1723 Exposure Control:
1724 ----------------
1725 \\[allout-hide-current-subtree] `allout-hide-current-subtree'
1726 \\[allout-show-children] `allout-show-children'
1727 \\[allout-show-current-subtree] `allout-show-current-subtree'
1728 \\[allout-show-current-entry] `allout-show-current-entry'
1729 \\[allout-show-all] `allout-show-all'
1731 Navigation:
1732 ----------
1733 \\[allout-next-visible-heading] `allout-next-visible-heading'
1734 \\[allout-previous-visible-heading] `allout-previous-visible-heading'
1735 \\[allout-up-current-level] `allout-up-current-level'
1736 \\[allout-forward-current-level] `allout-forward-current-level'
1737 \\[allout-backward-current-level] `allout-backward-current-level'
1738 \\[allout-end-of-entry] `allout-end-of-entry'
1739 \\[allout-beginning-of-current-entry] `allout-beginning-of-current-entry' (alternately, goes to hot-spot)
1740 \\[allout-beginning-of-line] `allout-beginning-of-line' -- like regular beginning-of-line, but
1741 if immediately repeated cycles to the beginning of the current item
1742 and then to the hot-spot (if `allout-beginning-of-line-cycles' is set).
1745 Topic Header Production:
1746 -----------------------
1747 \\[allout-open-sibtopic] `allout-open-sibtopic' Create a new sibling after current topic.
1748 \\[allout-open-subtopic] `allout-open-subtopic' ... an offspring of current topic.
1749 \\[allout-open-supertopic] `allout-open-supertopic' ... a sibling of the current topic's parent.
1751 Topic Level and Prefix Adjustment:
1752 ---------------------------------
1753 \\[allout-shift-in] `allout-shift-in' Shift current topic and all offspring deeper
1754 \\[allout-shift-out] `allout-shift-out' ... less deep
1755 \\[allout-rebullet-current-heading] `allout-rebullet-current-heading' Prompt for alternate bullet for
1756 current topic
1757 \\[allout-rebullet-topic] `allout-rebullet-topic' Reconcile bullets of topic and
1758 its' offspring -- distinctive bullets are not changed, others
1759 are alternated according to nesting depth.
1760 \\[allout-number-siblings] `allout-number-siblings' Number bullets of topic and siblings --
1761 the offspring are not affected.
1762 With repeat count, revoke numbering.
1764 Topic-oriented Killing and Yanking:
1765 ----------------------------------
1766 \\[allout-kill-topic] `allout-kill-topic' Kill current topic, including offspring.
1767 \\[allout-copy-topic-as-kill] `allout-copy-topic-as-kill' Copy current topic, including offspring.
1768 \\[allout-kill-line] `allout-kill-line' kill-line, attending to outline structure.
1769 \\[allout-copy-line-as-kill] `allout-copy-line-as-kill' Copy line but don't delete it.
1770 \\[allout-yank] `allout-yank' Yank, adjusting depth of yanked topic to
1771 depth of heading if yanking into bare topic
1772 heading (ie, prefix sans text).
1773 \\[allout-yank-pop] `allout-yank-pop' Is to allout-yank as yank-pop is to yank
1775 Topic-oriented Encryption:
1776 -------------------------
1777 \\[allout-toggle-current-subtree-encryption] `allout-toggle-current-subtree-encryption'
1778 Encrypt/Decrypt topic content
1780 Misc commands:
1781 -------------
1782 M-x outlineify-sticky Activate outline mode for current buffer,
1783 and establish a default file-var setting
1784 for `allout-layout'.
1785 \\[allout-mark-topic] `allout-mark-topic'
1786 \\[allout-copy-exposed-to-buffer] `allout-copy-exposed-to-buffer'
1787 Duplicate outline, sans concealed text, to
1788 buffer with name derived from derived from that
1789 of current buffer -- \"*BUFFERNAME exposed*\".
1790 \\[allout-flatten-exposed-to-buffer] `allout-flatten-exposed-to-buffer'
1791 Like above 'copy-exposed', but convert topic
1792 prefixes to section.subsection... numeric
1793 format.
1794 \\[eval-expression] (allout-init t) Setup Emacs session for outline mode
1795 auto-activation.
1797 Topic Encryption
1799 Outline mode supports gpg encryption of topics, with support for
1800 symmetric and key-pair modes, passphrase timeout, passphrase
1801 consistency checking, user-provided hinting for symmetric key
1802 mode, and auto-encryption of topics pending encryption on save.
1804 Topics pending encryption are, by default, automatically
1805 encrypted during file saves. If the contents of the topic
1806 containing the cursor was encrypted for a save, it is
1807 automatically decrypted for continued editing.
1809 The aim of these measures is reliable topic privacy while
1810 preventing accidents like neglected encryption before saves,
1811 forgetting which passphrase was used, and other practical
1812 pitfalls.
1814 See `allout-toggle-current-subtree-encryption' function docstring
1815 and `allout-encrypt-unencrypted-on-saves' customization variable
1816 for details.
1818 HOT-SPOT Operation
1820 Hot-spot operation provides a means for easy, single-keystroke outline
1821 navigation and exposure control.
1823 When the text cursor is positioned directly on the bullet character of
1824 a topic, regular characters (a to z) invoke the commands of the
1825 corresponding allout-mode keymap control chars. For example, \"f\"
1826 would invoke the command typically bound to \"C-c<space>C-f\"
1827 \(\\[allout-forward-current-level] `allout-forward-current-level').
1829 Thus, by positioning the cursor on a topic bullet, you can
1830 execute the outline navigation and manipulation commands with a
1831 single keystroke. Regular navigation keys (eg, \\[forward-char], \\[next-line]) don't get
1832 this special translation, so you can use them to get out of the
1833 hot-spot and back to normal editing operation.
1835 In allout-mode, the normal beginning-of-line command (\\[allout-beginning-of-line]]) is
1836 replaced with one that makes it easy to get to the hot-spot. If you
1837 repeat it immediately it cycles (if `allout-beginning-of-line-cycles'
1838 is set) to the beginning of the item and then, if you hit it again
1839 immediately, to the hot-spot. Similarly, `allout-beginning-of-current-entry'
1840 \(\\[allout-beginning-of-current-entry]) moves to the hot-spot when the cursor is already located
1841 at the beginning of the current entry.
1843 Extending Allout
1845 Allout exposure and authoring activites all have associated
1846 hooks, by which independent code can cooperate with allout
1847 without changes to the allout core. Here are key ones:
1849 `allout-mode-hook'
1850 `allout-mode-deactivate-hook'
1851 `allout-exposure-change-hook'
1852 `allout-structure-added-hook'
1853 `allout-structure-deleted-hook'
1854 `allout-structure-shifted-hook'
1856 Terminology
1858 Topic hierarchy constituents -- TOPICS and SUBTOPICS:
1860 ITEM: A unitary outline element, including the HEADER and ENTRY text.
1861 TOPIC: An ITEM and any ITEMs contained within it, ie having greater DEPTH
1862 and with no intervening items of lower DEPTH than the container.
1863 CURRENT ITEM:
1864 The visible ITEM most immediately containing the cursor.
1865 DEPTH: The degree of nesting of an ITEM; it increases with containment.
1866 The DEPTH is determined by the HEADER PREFIX. The DEPTH is also
1867 called the:
1868 LEVEL: The same as DEPTH.
1870 ANCESTORS:
1871 Those ITEMs whose TOPICs contain an ITEM.
1872 PARENT: An ITEM's immediate ANCESTOR. It has a DEPTH one less than that
1873 of the ITEM.
1874 OFFSPRING:
1875 The ITEMs contained within an ITEM's TOPIC.
1876 SUBTOPIC:
1877 An OFFSPRING of its ANCESTOR TOPICs.
1878 CHILD:
1879 An immediate SUBTOPIC of its PARENT.
1880 SIBLINGS:
1881 TOPICs having the same PARENT and DEPTH.
1883 Topic text constituents:
1885 HEADER: The first line of an ITEM, include the ITEM PREFIX and HEADER
1886 text.
1887 ENTRY: The text content of an ITEM, before any OFFSPRING, but including
1888 the HEADER text and distinct from the ITEM PREFIX.
1889 BODY: Same as ENTRY.
1890 PREFIX: The leading text of an ITEM which distinguishes it from normal
1891 ENTRY text. Allout recognizes the outline structure according
1892 to the strict PREFIX format. It consists of a PREFIX-LEAD string,
1893 PREFIX-PADDING, and a BULLET. The BULLET might be followed by a
1894 number, indicating the ordinal number of the topic among its
1895 siblings, or an asterisk indicating encryption, plus an optional
1896 space. After that is the ITEM HEADER text, which is not part of
1897 the PREFIX.
1899 The relative length of the PREFIX determines the nesting DEPTH
1900 of the ITEM.
1901 PREFIX-LEAD:
1902 The string at the beginning of a HEADER PREFIX, by default a `.'.
1903 It can be customized by changing the setting of
1904 `allout-header-prefix' and then reinitializing `allout-mode'.
1906 When the PREFIX-LEAD is set to the comment-string of a
1907 programming language, outline structuring can be embedded in
1908 program code without interfering with processing of the text
1909 (by emacs or the language processor) as program code. This
1910 setting happens automatically when allout mode is used in
1911 programming-mode buffers. See `allout-use-mode-specific-leader'
1912 docstring for more detail.
1913 PREFIX-PADDING:
1914 Spaces or asterisks which separate the PREFIX-LEAD and the
1915 bullet, determining the ITEM's DEPTH.
1916 BULLET: A character at the end of the ITEM PREFIX, it must be one of
1917 the characters listed on `allout-plain-bullets-string' or
1918 `allout-distinctive-bullets-string'. When creating a TOPIC,
1919 plain BULLETs are by default used, according to the DEPTH of the
1920 TOPIC. Choice among the distinctive BULLETs is offered when you
1921 provide a universal argugment \(\\[universal-argument]) to the
1922 TOPIC creation command, or when explictly rebulleting a TOPIC. The
1923 significance of the various distinctive bullets is purely by
1924 convention. See the documentation for the above bullet strings for
1925 more details.
1926 EXPOSURE:
1927 The state of a TOPIC which determines the on-screen visibility
1928 of its OFFSPRING and contained ENTRY text.
1929 CONCEALED:
1930 TOPICs and ENTRY text whose EXPOSURE is inhibited. Concealed
1931 text is represented by \"...\" ellipses.
1933 CONCEALED TOPICs are effectively collapsed within an ANCESTOR.
1934 CLOSED: A TOPIC whose immediate OFFSPRING and body-text is CONCEALED.
1935 OPEN: A TOPIC that is not CLOSED, though its OFFSPRING or BODY may be."
1936 ;;;_ . Code
1937 (interactive "P")
1939 (let* ((active (and (not (equal major-mode 'outline))
1940 (allout-mode-p)))
1941 ; Massage universal-arg `toggle' val:
1942 (toggle (and toggle
1943 (or (and (listp toggle)(car toggle))
1944 toggle)))
1945 ; Activation specifically demanded?
1946 (explicit-activation (and toggle
1947 (or (symbolp toggle)
1948 (and (wholenump toggle)
1949 (not (zerop toggle))))))
1950 ;; allout-mode already called once during this complex command?
1951 (same-complex-command (eq allout-v18/19-file-var-hack
1952 (car command-history)))
1953 (write-file-hook-var-name (cond ((boundp 'write-file-functions)
1954 'write-file-functions)
1955 ((boundp 'write-file-hooks)
1956 'write-file-hooks)
1957 (t 'local-write-file-hooks)))
1958 do-layout
1961 ; See comments below re v19.18,.19 bug.
1962 (setq allout-v18/19-file-var-hack (car command-history))
1964 (cond
1966 ;; Provision for v19.18, 19.19 bug --
1967 ;; Emacs v 19.18, 19.19 file-var code invokes prop-line-designated
1968 ;; modes twice when file is visited. We have to avoid toggling mode
1969 ;; off on second invocation, so we detect it as best we can, and
1970 ;; skip everything.
1971 ((and same-complex-command ; Still in same complex command
1972 ; as last time `allout-mode' invoked.
1973 active ; Already activated.
1974 (not explicit-activation) ; Prop-line file-vars don't have args.
1975 (string-match "^19.1[89]" ; Bug only known to be in v19.18 and
1976 emacs-version)); 19.19.
1979 ;; Deactivation:
1980 ((and (not explicit-activation)
1981 (or active toggle))
1982 ; Activation not explicitly
1983 ; requested, and either in
1984 ; active state or *de*activation
1985 ; specifically requested:
1986 (setq allout-explicitly-deactivated t)
1988 (allout-do-resumptions)
1990 (remove-from-invisibility-spec '(allout . t))
1991 (remove-hook 'pre-command-hook 'allout-pre-command-business t)
1992 (remove-hook 'post-command-hook 'allout-post-command-business t)
1993 (remove-hook 'before-change-functions 'allout-before-change-handler t)
1994 (remove-hook 'isearch-mode-end-hook 'allout-isearch-end-handler t)
1995 (remove-hook write-file-hook-var-name 'allout-write-file-hook-handler t)
1996 (remove-hook 'auto-save-hook 'allout-auto-save-hook-handler t)
1998 (remove-overlays (point-min) (point-max)
1999 'category 'allout-exposure-category)
2001 (setq allout-mode nil)
2002 (run-hooks 'allout-mode-deactivate-hook))
2004 ;; Activation:
2005 ((not active)
2006 (setq allout-explicitly-deactivated nil)
2007 (if allout-old-style-prefixes
2008 ;; Inhibit all the fancy formatting:
2009 (allout-add-resumptions '(allout-primary-bullet "*")))
2011 (allout-overlay-preparations) ; Doesn't hurt to redo this.
2013 (allout-infer-header-lead-and-primary-bullet)
2014 (allout-infer-body-reindent)
2016 (set-allout-regexp)
2017 (allout-add-resumptions
2018 '(allout-encryption-ciphertext-rejection-regexps
2019 allout-line-boundary-regexp
2020 extend)
2021 '(allout-encryption-ciphertext-rejection-regexps
2022 allout-bob-regexp
2023 extend))
2025 ;; Produce map from current version of allout-keybindings-list:
2026 (allout-setup-mode-map)
2027 (produce-allout-mode-menubar-entries)
2029 ;; Include on minor-mode-map-alist, if not already there:
2030 (if (not (member '(allout-mode . allout-mode-map)
2031 minor-mode-map-alist))
2032 (setq minor-mode-map-alist
2033 (cons '(allout-mode . allout-mode-map)
2034 minor-mode-map-alist)))
2036 (add-to-invisibility-spec '(allout . t))
2038 (allout-add-resumptions '(line-move-ignore-invisible t))
2039 (add-hook 'pre-command-hook 'allout-pre-command-business nil t)
2040 (add-hook 'post-command-hook 'allout-post-command-business nil t)
2041 (add-hook 'before-change-functions 'allout-before-change-handler
2042 nil t)
2043 (add-hook 'isearch-mode-end-hook 'allout-isearch-end-handler nil t)
2044 (add-hook write-file-hook-var-name 'allout-write-file-hook-handler
2045 nil t)
2046 (add-hook 'auto-save-hook 'allout-auto-save-hook-handler
2047 nil t)
2049 ;; Stash auto-fill settings and adjust so custom allout auto-fill
2050 ;; func will be used if auto-fill is active or activated. (The
2051 ;; custom func respects topic headline, maintains hanging-indents,
2052 ;; etc.)
2053 (if (and auto-fill-function (not allout-inhibit-auto-fill))
2054 ;; allout-auto-fill will use the stashed values and so forth.
2055 (allout-add-resumptions '(auto-fill-function allout-auto-fill)))
2056 (allout-add-resumptions (list 'allout-former-auto-filler
2057 auto-fill-function)
2058 ;; Register allout-auto-fill to be used if
2059 ;; filling is active:
2060 (list 'allout-outside-normal-auto-fill-function
2061 normal-auto-fill-function)
2062 '(normal-auto-fill-function allout-auto-fill)
2063 ;; Paragraphs are broken by topic headlines.
2064 (list 'paragraph-start
2065 (concat paragraph-start "\\|^\\("
2066 allout-regexp "\\)"))
2067 (list 'paragraph-separate
2068 (concat paragraph-separate "\\|^\\("
2069 allout-regexp "\\)")))
2070 (or (assq 'allout-mode minor-mode-alist)
2071 (setq minor-mode-alist
2072 (cons '(allout-mode " Allout") minor-mode-alist)))
2074 (allout-setup-menubar)
2076 (if allout-layout
2077 (setq do-layout t))
2079 (setq allout-mode t)
2080 (run-hooks 'allout-mode-hook))
2082 ;; Reactivation:
2083 ((setq do-layout t)
2084 (allout-infer-body-reindent))
2085 ) ;; end of activation-mode cases.
2087 ;; Do auto layout if warranted:
2088 (let ((use-layout (if (listp allout-layout)
2089 allout-layout
2090 allout-default-layout)))
2091 (if (and do-layout
2092 allout-auto-activation
2093 use-layout
2094 (and (not (eq allout-auto-activation 'activate))
2095 (if (eq allout-auto-activation 'ask)
2096 (if (y-or-n-p (format "Expose %s with layout '%s'? "
2097 (buffer-name)
2098 use-layout))
2100 (message "Skipped %s layout." (buffer-name))
2101 nil)
2102 t)))
2103 (save-excursion
2104 (message "Adjusting '%s' exposure..." (buffer-name))
2105 (goto-char 0)
2106 (allout-this-or-next-heading)
2107 (condition-case err
2108 (progn
2109 (apply 'allout-expose-topic (list use-layout))
2110 (message "Adjusting '%s' exposure... done." (buffer-name)))
2111 ;; Problem applying exposure -- notify user, but don't
2112 ;; interrupt, eg, file visit:
2113 (error (message "%s" (car (cdr err)))
2114 (sit-for 1))))))
2115 allout-mode
2116 ) ; let*
2117 ) ; defun
2119 (defun allout-setup-mode-map ()
2120 "Establish allout-mode bindings."
2121 (setq-default allout-mode-map
2122 (produce-allout-mode-map allout-keybindings-list))
2123 (setq allout-mode-map
2124 (produce-allout-mode-map allout-keybindings-list))
2125 (substitute-key-definition 'beginning-of-line
2126 'allout-beginning-of-line
2127 allout-mode-map global-map)
2128 (substitute-key-definition 'move-beginning-of-line
2129 'allout-beginning-of-line
2130 allout-mode-map global-map)
2131 (substitute-key-definition 'end-of-line
2132 'allout-end-of-line
2133 allout-mode-map global-map)
2134 (substitute-key-definition 'move-end-of-line
2135 'allout-end-of-line
2136 allout-mode-map global-map)
2137 (fset 'allout-mode-map allout-mode-map))
2139 ;; ensure that allout-mode-map has some setting even if allout-mode hasn't
2140 ;; been invoked:
2141 (allout-setup-mode-map)
2143 ;;;_ > allout-minor-mode
2144 (defalias 'allout-minor-mode 'allout-mode)
2146 ;;;_ > allout-unload-function
2147 (defun allout-unload-function ()
2148 "Unload the allout outline library."
2149 (save-current-buffer
2150 (dolist (buffer (buffer-list))
2151 (set-buffer buffer)
2152 (when allout-mode (allout-mode -1))))
2153 ;; continue standard unloading
2154 nil)
2156 ;;;_ - Position Assessment
2157 ;;;_ > allout-hidden-p (&optional pos)
2158 (defsubst allout-hidden-p (&optional pos)
2159 "Non-nil if the character after point is invisible."
2160 (eq (get-char-property (or pos (point)) 'invisible) 'allout))
2162 ;;;_ > allout-overlay-insert-in-front-handler (ol after beg end
2163 ;;; &optional prelen)
2164 (defun allout-overlay-insert-in-front-handler (ol after beg end
2165 &optional prelen)
2166 "Shift the overlay so stuff inserted in front of it is excluded."
2167 (if after
2168 ;; XXX Shouldn't moving the overlay should be unnecessary, if overlay
2169 ;; front-advance on the overlay worked as it should?
2170 (move-overlay ol (1+ beg) (overlay-end ol))))
2171 ;;;_ > allout-overlay-interior-modification-handler (ol after beg end
2172 ;;; &optional prelen)
2173 (defun allout-overlay-interior-modification-handler (ol after beg end
2174 &optional prelen)
2175 "Get confirmation before making arbitrary changes to invisible text.
2177 We expose the invisible text and ask for confirmation. Refusal or
2178 `keyboard-quit' abandons the changes, with keyboard-quit additionally
2179 reclosing the opened text.
2181 No confirmation is necessary when `inhibit-read-only' is set -- eg, allout
2182 internal functions use this feature cohesively bunch changes."
2184 (when (and (not inhibit-read-only) (not after))
2185 (let ((start (point))
2186 (ol-start (overlay-start ol))
2187 (ol-end (overlay-end ol))
2188 first)
2189 (goto-char beg)
2190 (while (< (point) end)
2191 (when (allout-hidden-p)
2192 (allout-show-to-offshoot)
2193 (if (allout-hidden-p)
2194 (save-excursion (forward-char 1)
2195 (allout-show-to-offshoot)))
2196 (when (not first)
2197 (setq first (point))))
2198 (goto-char (if (featurep 'xemacs)
2199 (next-property-change (1+ (point)) nil end)
2200 (next-char-property-change (1+ (point)) end))))
2201 (when first
2202 (goto-char first)
2203 (condition-case nil
2204 (if (not
2205 (yes-or-no-p
2206 (substitute-command-keys
2207 (concat "Modify concealed text? (\"no\" just aborts,"
2208 " \\[keyboard-quit] also reconceals) "))))
2209 (progn (goto-char start)
2210 (error "Concealed-text change refused.")))
2211 (quit (allout-flag-region ol-start ol-end nil)
2212 (allout-flag-region ol-start ol-end t)
2213 (error "Concealed-text change abandoned, text reconcealed."))))
2214 (goto-char start))))
2215 ;;;_ > allout-before-change-handler (beg end)
2216 (defun allout-before-change-handler (beg end)
2217 "Protect against changes to invisible text.
2219 See `allout-overlay-interior-modification-handler' for details."
2221 (if (and (allout-mode-p) undo-in-progress (allout-hidden-p))
2222 (allout-show-to-offshoot))
2224 ;; allout-overlay-interior-modification-handler on an overlay handles
2225 ;; this in other emacs, via `allout-exposure-category's 'modification-hooks.
2226 (when (and (featurep 'xemacs) (allout-mode-p))
2227 ;; process all of the pending overlays:
2228 (save-excursion
2229 (goto-char beg)
2230 (let ((overlay (allout-get-invisibility-overlay)))
2231 (allout-overlay-interior-modification-handler
2232 overlay nil beg end nil)))))
2233 ;;;_ > allout-isearch-end-handler (&optional overlay)
2234 (defun allout-isearch-end-handler (&optional overlay)
2235 "Reconcile allout outline exposure on arriving in hidden text after isearch.
2237 Optional OVERLAY parameter is for when this function is used by
2238 `isearch-open-invisible' overlay property. It is otherwise unused, so this
2239 function can also be used as an `isearch-mode-end-hook'."
2241 (if (and (allout-mode-p) (allout-hidden-p))
2242 (allout-show-to-offshoot)))
2244 ;;;_ #3 Internal Position State-Tracking -- "allout-recent-*" funcs
2245 ;;; All the basic outline functions that directly do string matches to
2246 ;;; evaluate heading prefix location set the variables
2247 ;;; `allout-recent-prefix-beginning' and `allout-recent-prefix-end'
2248 ;;; when successful. Functions starting with `allout-recent-' all
2249 ;;; use this state, providing the means to avoid redundant searches
2250 ;;; for just-established data. This optimization can provide
2251 ;;; significant speed improvement, but it must be employed carefully.
2252 ;;;_ = allout-recent-prefix-beginning
2253 (defvar allout-recent-prefix-beginning 0
2254 "Buffer point of the start of the last topic prefix encountered.")
2255 (make-variable-buffer-local 'allout-recent-prefix-beginning)
2256 ;;;_ = allout-recent-prefix-end
2257 (defvar allout-recent-prefix-end 0
2258 "Buffer point of the end of the last topic prefix encountered.")
2259 (make-variable-buffer-local 'allout-recent-prefix-end)
2260 ;;;_ = allout-recent-depth
2261 (defvar allout-recent-depth 0
2262 "Depth of the last topic prefix encountered.")
2263 (make-variable-buffer-local 'allout-recent-depth)
2264 ;;;_ = allout-recent-end-of-subtree
2265 (defvar allout-recent-end-of-subtree 0
2266 "Buffer point last returned by `allout-end-of-current-subtree'.")
2267 (make-variable-buffer-local 'allout-recent-end-of-subtree)
2268 ;;;_ > allout-prefix-data ()
2269 (defsubst allout-prefix-data ()
2270 "Register allout-prefix state data.
2272 For reference by `allout-recent' funcs. Return
2273 the new value of `allout-recent-prefix-beginning'."
2274 (setq allout-recent-prefix-end (or (match-end 1) (match-end 2) (match-end 3))
2275 allout-recent-prefix-beginning (or (match-beginning 1)
2276 (match-beginning 2)
2277 (match-beginning 3))
2278 allout-recent-depth (max 1 (- allout-recent-prefix-end
2279 allout-recent-prefix-beginning
2280 allout-header-subtraction)))
2281 allout-recent-prefix-beginning)
2282 ;;;_ > nullify-allout-prefix-data ()
2283 (defsubst nullify-allout-prefix-data ()
2284 "Mark allout prefix data as being uninformative."
2285 (setq allout-recent-prefix-end (point)
2286 allout-recent-prefix-beginning (point)
2287 allout-recent-depth 0)
2288 allout-recent-prefix-beginning)
2289 ;;;_ > allout-recent-depth ()
2290 (defsubst allout-recent-depth ()
2291 "Return depth of last heading encountered by an outline maneuvering function.
2293 All outline functions which directly do string matches to assess
2294 headings set the variables `allout-recent-prefix-beginning' and
2295 `allout-recent-prefix-end' if successful. This function uses those settings
2296 to return the current depth."
2298 allout-recent-depth)
2299 ;;;_ > allout-recent-prefix ()
2300 (defsubst allout-recent-prefix ()
2301 "Like `allout-recent-depth', but returns text of last encountered prefix.
2303 All outline functions which directly do string matches to assess
2304 headings set the variables `allout-recent-prefix-beginning' and
2305 `allout-recent-prefix-end' if successful. This function uses those settings
2306 to return the current prefix."
2307 (buffer-substring-no-properties allout-recent-prefix-beginning
2308 allout-recent-prefix-end))
2309 ;;;_ > allout-recent-bullet ()
2310 (defmacro allout-recent-bullet ()
2311 "Like allout-recent-prefix, but returns bullet of last encountered prefix.
2313 All outline functions which directly do string matches to assess
2314 headings set the variables `allout-recent-prefix-beginning' and
2315 `allout-recent-prefix-end' if successful. This function uses those settings
2316 to return the current depth of the most recently matched topic."
2317 '(buffer-substring-no-properties (1- allout-recent-prefix-end)
2318 allout-recent-prefix-end))
2320 ;;;_ #4 Navigation
2322 ;;;_ - Position Assessment
2323 ;;;_ : Location Predicates
2324 ;;;_ > allout-do-doublecheck ()
2325 (defsubst allout-do-doublecheck ()
2326 "True if current item conditions qualify for checking on topic aberrance."
2327 (and
2328 ;; presume integrity of outline and yanked content during yank -- necessary
2329 ;; to allow for level disparity of yank location and yanked text:
2330 (not allout-inhibit-aberrance-doublecheck)
2331 ;; allout-doublecheck-at-and-shallower is ceiling for doublecheck:
2332 (<= allout-recent-depth allout-doublecheck-at-and-shallower)))
2333 ;;;_ > allout-aberrant-container-p ()
2334 (defun allout-aberrant-container-p ()
2335 "True if topic, or next sibling with children, contains them discontinuously.
2337 Discontinuous means an immediate offspring that is nested more
2338 than one level deeper than the topic.
2340 If topic has no offspring, then the next sibling with offspring will
2341 determine whether or not this one is determined to be aberrant.
2343 If true, then the allout-recent-* settings are calibrated on the
2344 offspring that qaulifies it as aberrant, ie with depth that
2345 exceeds the topic by more than one."
2347 ;; This is most clearly understood when considering standard-prefix-leader
2348 ;; low-level topics, which can all too easily match text not intended as
2349 ;; headers. For example, any line with a leading '.' or '*' and lacking a
2350 ;; following bullet qualifies without this protection. (A sequence of
2351 ;; them can occur naturally, eg a typical textual bullet list.) We
2352 ;; disqualify such low-level sequences when they are followed by a
2353 ;; discontinuously contained child, inferring that the sequences are not
2354 ;; actually connected with their prospective context.
2356 (let ((depth (allout-depth))
2357 (start-point (point))
2358 done aberrant)
2359 (save-match-data
2360 (save-excursion
2361 (while (and (not done)
2362 (re-search-forward allout-line-boundary-regexp nil 0))
2363 (allout-prefix-data)
2364 (goto-char allout-recent-prefix-beginning)
2365 (cond
2366 ;; sibling -- continue:
2367 ((eq allout-recent-depth depth))
2368 ;; first offspring is excessive -- aberrant:
2369 ((> allout-recent-depth (1+ depth))
2370 (setq done t aberrant t))
2371 ;; next non-sibling is lower-depth -- not aberrant:
2372 (t (setq done t))))))
2373 (if aberrant
2374 aberrant
2375 (goto-char start-point)
2376 ;; recalibrate allout-recent-*
2377 (allout-depth)
2378 nil)))
2379 ;;;_ > allout-on-current-heading-p ()
2380 (defun allout-on-current-heading-p ()
2381 "Return non-nil if point is on current visible topics' header line.
2383 Actually, returns prefix beginning point."
2384 (save-excursion
2385 (allout-beginning-of-current-line)
2386 (save-match-data
2387 (and (looking-at allout-regexp)
2388 (allout-prefix-data)
2389 (or (not (allout-do-doublecheck))
2390 (not (allout-aberrant-container-p)))))))
2391 ;;;_ > allout-on-heading-p ()
2392 (defalias 'allout-on-heading-p 'allout-on-current-heading-p)
2393 ;;;_ > allout-e-o-prefix-p ()
2394 (defun allout-e-o-prefix-p ()
2395 "True if point is located where current topic prefix ends, heading begins."
2396 (and (save-match-data
2397 (save-excursion (let ((inhibit-field-text-motion t))
2398 (beginning-of-line))
2399 (looking-at allout-regexp))
2400 (= (point) (save-excursion (allout-end-of-prefix)(point))))))
2401 ;;;_ : Location attributes
2402 ;;;_ > allout-depth ()
2403 (defun allout-depth ()
2404 "Return depth of topic most immediately containing point.
2406 Does not do doublecheck for aberrant topic header.
2408 Return zero if point is not within any topic.
2410 Like `allout-current-depth', but respects hidden as well as visible topics."
2411 (save-excursion
2412 (let ((start-point (point)))
2413 (if (and (allout-goto-prefix)
2414 (not (< start-point (point))))
2415 allout-recent-depth
2416 (progn
2417 ;; Oops, no prefix, nullify it:
2418 (nullify-allout-prefix-data)
2419 ;; ... and return 0:
2420 0)))))
2421 ;;;_ > allout-current-depth ()
2422 (defun allout-current-depth ()
2423 "Return depth of visible topic most immediately containing point.
2425 Return zero if point is not within any topic."
2426 (save-excursion
2427 (if (allout-back-to-current-heading)
2428 (max 1
2429 (- allout-recent-prefix-end
2430 allout-recent-prefix-beginning
2431 allout-header-subtraction))
2432 0)))
2433 ;;;_ > allout-get-current-prefix ()
2434 (defun allout-get-current-prefix ()
2435 "Topic prefix of the current topic."
2436 (save-excursion
2437 (if (allout-goto-prefix)
2438 (allout-recent-prefix))))
2439 ;;;_ > allout-get-bullet ()
2440 (defun allout-get-bullet ()
2441 "Return bullet of containing topic (visible or not)."
2442 (save-excursion
2443 (and (allout-goto-prefix)
2444 (allout-recent-bullet))))
2445 ;;;_ > allout-current-bullet ()
2446 (defun allout-current-bullet ()
2447 "Return bullet of current (visible) topic heading, or none if none found."
2448 (condition-case nil
2449 (save-excursion
2450 (allout-back-to-current-heading)
2451 (buffer-substring-no-properties (- allout-recent-prefix-end 1)
2452 allout-recent-prefix-end))
2453 ;; Quick and dirty provision, ostensibly for missing bullet:
2454 (args-out-of-range nil))
2456 ;;;_ > allout-get-prefix-bullet (prefix)
2457 (defun allout-get-prefix-bullet (prefix)
2458 "Return the bullet of the header prefix string PREFIX."
2459 ;; Doesn't make sense if we're old-style prefixes, but this just
2460 ;; oughtn't be called then, so forget about it...
2461 (if (string-match allout-regexp prefix)
2462 (substring prefix (1- (match-end 2)) (match-end 2))))
2463 ;;;_ > allout-sibling-index (&optional depth)
2464 (defun allout-sibling-index (&optional depth)
2465 "Item number of this prospective topic among its siblings.
2467 If optional arg DEPTH is greater than current depth, then we're
2468 opening a new level, and return 0.
2470 If less than this depth, ascend to that depth and count..."
2472 (save-excursion
2473 (cond ((and depth (<= depth 0) 0))
2474 ((or (null depth) (= depth (allout-depth)))
2475 (let ((index 1))
2476 (while (allout-previous-sibling allout-recent-depth nil)
2477 (setq index (1+ index)))
2478 index))
2479 ((< depth allout-recent-depth)
2480 (allout-ascend-to-depth depth)
2481 (allout-sibling-index))
2482 (0))))
2483 ;;;_ > allout-topic-flat-index ()
2484 (defun allout-topic-flat-index ()
2485 "Return a list indicating point's numeric section.subsect.subsubsect...
2486 Outermost is first."
2487 (let* ((depth (allout-depth))
2488 (next-index (allout-sibling-index depth))
2489 (rev-sibls nil))
2490 (while (> next-index 0)
2491 (setq rev-sibls (cons next-index rev-sibls))
2492 (setq depth (1- depth))
2493 (setq next-index (allout-sibling-index depth)))
2494 rev-sibls)
2497 ;;;_ - Navigation routines
2498 ;;;_ > allout-beginning-of-current-line ()
2499 (defun allout-beginning-of-current-line ()
2500 "Like beginning of line, but to visible text."
2502 ;; This combination of move-beginning-of-line and beginning-of-line is
2503 ;; deliberate, but the (beginning-of-line) may now be superfluous.
2504 (let ((inhibit-field-text-motion t))
2505 (move-beginning-of-line 1)
2506 (beginning-of-line)
2507 (while (and (not (bobp)) (or (not (bolp)) (allout-hidden-p)))
2508 (beginning-of-line)
2509 (if (or (allout-hidden-p) (not (bolp)))
2510 (forward-char -1)))))
2511 ;;;_ > allout-end-of-current-line ()
2512 (defun allout-end-of-current-line ()
2513 "Move to the end of line, past concealed text if any."
2514 ;; XXX This is for symmetry with `allout-beginning-of-current-line' --
2515 ;; `move-end-of-line' doesn't suffer the same problem as
2516 ;; `move-beginning-of-line'.
2517 (let ((inhibit-field-text-motion t))
2518 (end-of-line)
2519 (while (allout-hidden-p)
2520 (end-of-line)
2521 (if (allout-hidden-p) (forward-char 1)))))
2522 ;;;_ > allout-beginning-of-line ()
2523 (defun allout-beginning-of-line ()
2524 "Beginning-of-line with `allout-beginning-of-line-cycles' behavior, if set."
2526 (interactive)
2528 (if (or (not allout-beginning-of-line-cycles)
2529 (not (equal last-command this-command)))
2530 (progn
2531 (if (and (not (bolp))
2532 (allout-hidden-p (1- (point))))
2533 (goto-char (previous-single-char-property-change
2534 (1- (point)) 'invisible)))
2535 (move-beginning-of-line 1))
2536 (allout-depth)
2537 (let ((beginning-of-body
2538 (save-excursion
2539 (while (and (allout-do-doublecheck)
2540 (allout-aberrant-container-p)
2541 (allout-previous-visible-heading 1)))
2542 (allout-beginning-of-current-entry)
2543 (point))))
2544 (cond ((= (current-column) 0)
2545 (goto-char beginning-of-body))
2546 ((< (point) beginning-of-body)
2547 (allout-beginning-of-current-line))
2548 ((= (point) beginning-of-body)
2549 (goto-char (allout-current-bullet-pos)))
2550 (t (allout-beginning-of-current-line)
2551 (if (< (point) beginning-of-body)
2552 ;; we were on the headline after its start:
2553 (goto-char beginning-of-body)))))))
2554 ;;;_ > allout-end-of-line ()
2555 (defun allout-end-of-line ()
2556 "End-of-line with `allout-end-of-line-cycles' behavior, if set."
2558 (interactive)
2560 (if (or (not allout-end-of-line-cycles)
2561 (not (equal last-command this-command)))
2562 (allout-end-of-current-line)
2563 (let ((end-of-entry (save-excursion
2564 (allout-end-of-entry)
2565 (point))))
2566 (cond ((not (eolp))
2567 (allout-end-of-current-line))
2568 ((or (allout-hidden-p) (save-excursion
2569 (forward-char -1)
2570 (allout-hidden-p)))
2571 (allout-back-to-current-heading)
2572 (allout-show-current-entry)
2573 (allout-show-children)
2574 (allout-end-of-entry))
2575 ((>= (point) end-of-entry)
2576 (allout-back-to-current-heading)
2577 (allout-end-of-current-line))
2579 (if (not (and transient-mark-mode mark-active))
2580 (push-mark))
2581 (allout-end-of-entry))))))
2582 ;;;_ > allout-next-heading ()
2583 (defsubst allout-next-heading ()
2584 "Move to the heading for the topic (possibly invisible) after this one.
2586 Returns the location of the heading, or nil if none found.
2588 We skip anomalous low-level topics, a la `allout-aberrant-container-p'."
2589 (save-match-data
2591 (if (looking-at allout-regexp)
2592 (forward-char 1))
2594 (when (re-search-forward allout-line-boundary-regexp nil 0)
2595 (allout-prefix-data)
2596 (goto-char allout-recent-prefix-beginning)
2597 (while (not (bolp))
2598 (forward-char -1))
2599 (and (allout-do-doublecheck)
2600 ;; this will set allout-recent-* on the first non-aberrant topic,
2601 ;; whether it's the current one or one that disqualifies it:
2602 (allout-aberrant-container-p))
2603 ;; this may or may not be the same as above depending on doublecheck:
2604 (goto-char allout-recent-prefix-beginning))))
2605 ;;;_ > allout-this-or-next-heading
2606 (defun allout-this-or-next-heading ()
2607 "Position cursor on current or next heading."
2608 ;; A throwaway non-macro that is defined after allout-next-heading
2609 ;; and usable by allout-mode.
2610 (if (not (allout-goto-prefix-doublechecked)) (allout-next-heading)))
2611 ;;;_ > allout-previous-heading ()
2612 (defun allout-previous-heading ()
2613 "Move to the prior (possibly invisible) heading line.
2615 Return the location of the beginning of the heading, or nil if not found.
2617 We skip anomalous low-level topics, a la `allout-aberrant-container-p'."
2619 (if (bobp)
2621 (let ((start-point (point)))
2622 ;; allout-goto-prefix-doublechecked calls us, so we can't use it here.
2623 (allout-goto-prefix)
2624 (save-match-data
2625 (when (or (re-search-backward allout-line-boundary-regexp nil 0)
2626 (looking-at allout-bob-regexp))
2627 (goto-char (allout-prefix-data))
2628 (if (and (allout-do-doublecheck)
2629 (allout-aberrant-container-p))
2630 (or (allout-previous-heading)
2631 (and (goto-char start-point)
2632 ;; recalibrate allout-recent-*:
2633 (allout-depth)
2634 nil))
2635 (point)))))))
2636 ;;;_ > allout-get-invisibility-overlay ()
2637 (defun allout-get-invisibility-overlay ()
2638 "Return the overlay at point that dictates allout invisibility."
2639 (let ((overlays (overlays-at (point)))
2640 got)
2641 (while (and overlays (not got))
2642 (if (equal (overlay-get (car overlays) 'invisible) 'allout)
2643 (setq got (car overlays))
2644 (pop overlays)))
2645 got))
2646 ;;;_ > allout-back-to-visible-text ()
2647 (defun allout-back-to-visible-text ()
2648 "Move to most recent prior character that is visible, and return point."
2649 (if (allout-hidden-p)
2650 (goto-char (overlay-start (allout-get-invisibility-overlay))))
2651 (point))
2653 ;;;_ - Subtree Charting
2654 ;;;_ " These routines either produce or assess charts, which are
2655 ;;; nested lists of the locations of topics within a subtree.
2657 ;;; Charts enable efficient subtree navigation by providing a reusable basis
2658 ;;; for elaborate, compound assessment and adjustment of a subtree.
2660 ;;;_ > allout-chart-subtree (&optional levels visible orig-depth prev-depth)
2661 (defun allout-chart-subtree (&optional levels visible orig-depth prev-depth)
2662 "Produce a location \"chart\" of subtopics of the containing topic.
2664 Optional argument LEVELS specifies a depth limit (relative to start
2665 depth) for the chart. Null LEVELS means no limit.
2667 When optional argument VISIBLE is non-nil, the chart includes
2668 only the visible subelements of the charted subjects.
2670 The remaining optional args are for internal use by the function.
2672 Point is left at the end of the subtree.
2674 Charts are used to capture outline structure, so that outline-altering
2675 routines need assess the structure only once, and then use the chart
2676 for their elaborate manipulations.
2678 The chart entries for the topics are in reverse order, so the
2679 last topic is listed first. The entry for each topic consists of
2680 an integer indicating the point at the beginning of the topic
2681 prefix. Charts for offspring consists of a list containing,
2682 recursively, the charts for the respective subtopics. The chart
2683 for a topics' offspring precedes the entry for the topic itself.
2685 The other function parameters are for internal recursion, and should
2686 not be specified by external callers. ORIG-DEPTH is depth of topic at
2687 starting point, and PREV-DEPTH is depth of prior topic."
2689 (let ((original (not orig-depth)) ; `orig-depth' set only in recursion.
2690 chart curr-depth)
2692 (if original ; Just starting?
2693 ; Register initial settings and
2694 ; position to first offspring:
2695 (progn (setq orig-depth (allout-depth))
2696 (or prev-depth (setq prev-depth (1+ orig-depth)))
2697 (if visible
2698 (allout-next-visible-heading 1)
2699 (allout-next-heading))))
2701 ;; Loop over the current levels' siblings. Besides being more
2702 ;; efficient than tail-recursing over a level, it avoids exceeding
2703 ;; the typically quite constrained Emacs max-lisp-eval-depth.
2705 ;; Probably would speed things up to implement loop-based stack
2706 ;; operation rather than recursing for lower levels. Bah.
2708 (while (and (not (eobp))
2709 ; Still within original topic?
2710 (< orig-depth (setq curr-depth allout-recent-depth))
2711 (cond ((= prev-depth curr-depth)
2712 ;; Register this one and move on:
2713 (setq chart (cons allout-recent-prefix-beginning chart))
2714 (if (and levels (<= levels 1))
2715 ;; At depth limit -- skip sublevels:
2716 (or (allout-next-sibling curr-depth)
2717 ;; or no more siblings -- proceed to
2718 ;; next heading at lesser depth:
2719 (while (and (<= curr-depth
2720 allout-recent-depth)
2721 (if visible
2722 (allout-next-visible-heading 1)
2723 (allout-next-heading)))))
2724 (if visible
2725 (allout-next-visible-heading 1)
2726 (allout-next-heading))))
2728 ((and (< prev-depth curr-depth)
2729 (or (not levels)
2730 (> levels 0)))
2731 ;; Recurse on deeper level of curr topic:
2732 (setq chart
2733 (cons (allout-chart-subtree (and levels
2734 (1- levels))
2735 visible
2736 orig-depth
2737 curr-depth)
2738 chart))
2739 ;; ... then continue with this one.
2742 ;; ... else nil if we've ascended back to prev-depth.
2746 (if original ; We're at the last sibling on
2747 ; the original level. Position
2748 ; to the end of it:
2749 (progn (and (not (eobp)) (forward-char -1))
2750 (and (= (preceding-char) ?\n)
2751 (= (aref (buffer-substring (max 1 (- (point) 3))
2752 (point))
2754 ?\n)
2755 (forward-char -1))
2756 (setq allout-recent-end-of-subtree (point))))
2758 chart ; (nreverse chart) not necessary,
2759 ; and maybe not preferable.
2761 ;;;_ > allout-chart-siblings (&optional start end)
2762 (defun allout-chart-siblings (&optional start end)
2763 "Produce a list of locations of this and succeeding sibling topics.
2764 Effectively a top-level chart of siblings. See `allout-chart-subtree'
2765 for an explanation of charts."
2766 (save-excursion
2767 (when (allout-goto-prefix-doublechecked)
2768 (let ((chart (list (point))))
2769 (while (allout-next-sibling)
2770 (setq chart (cons (point) chart)))
2771 (if chart (setq chart (nreverse chart)))))))
2772 ;;;_ > allout-chart-to-reveal (chart depth)
2773 (defun allout-chart-to-reveal (chart depth)
2775 "Return a flat list of hidden points in subtree CHART, up to DEPTH.
2777 If DEPTH is nil, include hidden points at any depth.
2779 Note that point can be left at any of the points on chart, or at the
2780 start point."
2782 (let (result here)
2783 (while (and (or (null depth) (> depth 0))
2784 chart)
2785 (setq here (car chart))
2786 (if (listp here)
2787 (let ((further (allout-chart-to-reveal here (if (null depth)
2788 depth
2789 (1- depth)))))
2790 ;; We're on the start of a subtree -- recurse with it, if there's
2791 ;; more depth to go:
2792 (if further (setq result (append further result)))
2793 (setq chart (cdr chart)))
2794 (goto-char here)
2795 (if (allout-hidden-p)
2796 (setq result (cons here result)))
2797 (setq chart (cdr chart))))
2798 result))
2799 ;;;_ X allout-chart-spec (chart spec &optional exposing)
2800 ;; (defun allout-chart-spec (chart spec &optional exposing)
2801 ;; "Not yet (if ever) implemented.
2803 ;; Produce exposure directives given topic/subtree CHART and an exposure SPEC.
2805 ;; Exposure spec indicates the locations to be exposed and the prescribed
2806 ;; exposure status. Optional arg EXPOSING is an integer, with 0
2807 ;; indicating pending concealment, anything higher indicating depth to
2808 ;; which subtopic headers should be exposed, and negative numbers
2809 ;; indicating (negative of) the depth to which subtopic headers and
2810 ;; bodies should be exposed.
2812 ;; The produced list can have two types of entries. Bare numbers
2813 ;; indicate points in the buffer where topic headers that should be
2814 ;; exposed reside.
2816 ;; - bare negative numbers indicates that the topic starting at the
2817 ;; point which is the negative of the number should be opened,
2818 ;; including their entries.
2819 ;; - bare positive values indicate that this topic header should be
2820 ;; opened.
2821 ;; - Lists signify the beginning and end points of regions that should
2822 ;; be flagged, and the flag to employ. (For concealment: `(\?r)', and
2823 ;; exposure:"
2824 ;; (while spec
2825 ;; (cond ((listp spec)
2826 ;; )
2827 ;; )
2828 ;; (setq spec (cdr spec)))
2829 ;; )
2831 ;;;_ - Within Topic
2832 ;;;_ > allout-goto-prefix ()
2833 (defun allout-goto-prefix ()
2834 "Put point at beginning of immediately containing outline topic.
2836 Goes to most immediate subsequent topic if none immediately containing.
2838 Not sensitive to topic visibility.
2840 Returns the point at the beginning of the prefix, or nil if none."
2842 (save-match-data
2843 (let (done)
2844 (while (and (not done)
2845 (search-backward "\n" nil 1))
2846 (forward-char 1)
2847 (if (looking-at allout-regexp)
2848 (setq done (allout-prefix-data))
2849 (forward-char -1)))
2850 (if (bobp)
2851 (cond ((looking-at allout-regexp)
2852 (allout-prefix-data))
2853 ((allout-next-heading))
2854 (done))
2855 done))))
2856 ;;;_ > allout-goto-prefix-doublechecked ()
2857 (defun allout-goto-prefix-doublechecked ()
2858 "Put point at beginning of immediately containing outline topic.
2860 Like `allout-goto-prefix', but shallow topics (according to
2861 `allout-doublecheck-at-and-shallower') are checked and
2862 disqualified for child containment discontinuity, according to
2863 `allout-aberrant-container-p'."
2864 (if (allout-goto-prefix)
2865 (if (and (allout-do-doublecheck)
2866 (allout-aberrant-container-p))
2867 (allout-previous-heading)
2868 (point))))
2870 ;;;_ > allout-end-of-prefix ()
2871 (defun allout-end-of-prefix (&optional ignore-decorations)
2872 "Position cursor at beginning of header text.
2874 If optional IGNORE-DECORATIONS is non-nil, put just after bullet,
2875 otherwise skip white space between bullet and ensuing text."
2877 (if (not (allout-goto-prefix-doublechecked))
2879 (goto-char allout-recent-prefix-end)
2880 (save-match-data
2881 (if ignore-decorations
2883 (while (looking-at "[0-9]") (forward-char 1))
2884 (if (and (not (eolp)) (looking-at "\\s-")) (forward-char 1))))
2885 ;; Reestablish where we are:
2886 (allout-current-depth)))
2887 ;;;_ > allout-current-bullet-pos ()
2888 (defun allout-current-bullet-pos ()
2889 "Return position of current (visible) topic's bullet."
2891 (if (not (allout-current-depth))
2893 (1- allout-recent-prefix-end)))
2894 ;;;_ > allout-back-to-current-heading ()
2895 (defun allout-back-to-current-heading ()
2896 "Move to heading line of current topic, or beginning if not in a topic.
2898 If interactive, we position at the end of the prefix.
2900 Return value of resulting point, unless we started outside
2901 of (before any) topics, in which case we return nil."
2903 (allout-beginning-of-current-line)
2904 (let ((bol-point (point)))
2905 (if (allout-goto-prefix-doublechecked)
2906 (if (<= (point) bol-point)
2907 (if (interactive-p)
2908 (allout-end-of-prefix)
2909 (point))
2910 (goto-char (point-min))
2911 nil))))
2912 ;;;_ > allout-back-to-heading ()
2913 (defalias 'allout-back-to-heading 'allout-back-to-current-heading)
2914 ;;;_ > allout-pre-next-prefix ()
2915 (defun allout-pre-next-prefix ()
2916 "Skip forward to just before the next heading line.
2918 Returns that character position."
2920 (if (allout-next-heading)
2921 (goto-char (1- allout-recent-prefix-beginning))))
2922 ;;;_ > allout-end-of-subtree (&optional current include-trailing-blank)
2923 (defun allout-end-of-subtree (&optional current include-trailing-blank)
2924 "Put point at the end of the last leaf in the containing topic.
2926 Optional CURRENT means put point at the end of the containing
2927 visible topic.
2929 Optional INCLUDE-TRAILING-BLANK means include a trailing blank line, if
2930 any, as part of the subtree. Otherwise, that trailing blank will be
2931 excluded as delimiting whitespace between topics.
2933 Returns the value of point."
2934 (interactive "P")
2935 (if current
2936 (allout-back-to-current-heading)
2937 (allout-goto-prefix-doublechecked))
2938 (let ((level allout-recent-depth))
2939 (allout-next-heading)
2940 (while (and (not (eobp))
2941 (> allout-recent-depth level))
2942 (allout-next-heading))
2943 (if (eobp)
2944 (allout-end-of-entry)
2945 (forward-char -1))
2946 (if (and (not include-trailing-blank) (= ?\n (preceding-char)))
2947 (forward-char -1))
2948 (setq allout-recent-end-of-subtree (point))))
2949 ;;;_ > allout-end-of-current-subtree (&optional include-trailing-blank)
2950 (defun allout-end-of-current-subtree (&optional include-trailing-blank)
2952 "Put point at end of last leaf in currently visible containing topic.
2954 Optional INCLUDE-TRAILING-BLANK means include a trailing blank line, if
2955 any, as part of the subtree. Otherwise, that trailing blank will be
2956 excluded as delimiting whitespace between topics.
2958 Returns the value of point."
2959 (interactive)
2960 (allout-end-of-subtree t include-trailing-blank))
2961 ;;;_ > allout-beginning-of-current-entry ()
2962 (defun allout-beginning-of-current-entry ()
2963 "When not already there, position point at beginning of current topic header.
2965 If already there, move cursor to bullet for hot-spot operation.
2966 \(See `allout-mode' doc string for details of hot-spot operation.)"
2967 (interactive)
2968 (let ((start-point (point)))
2969 (move-beginning-of-line 1)
2970 (if (< 0 (allout-current-depth))
2971 (goto-char allout-recent-prefix-end)
2972 (goto-char (point-min)))
2973 (allout-end-of-prefix)
2974 (if (and (interactive-p)
2975 (= (point) start-point))
2976 (goto-char (allout-current-bullet-pos)))))
2977 ;;;_ > allout-end-of-entry (&optional inclusive)
2978 (defun allout-end-of-entry (&optional inclusive)
2979 "Position the point at the end of the current topics' entry.
2981 Optional INCLUSIVE means also include trailing empty line, if any. When
2982 unset, whitespace between items separates them even when the items are
2983 collapsed."
2984 (interactive)
2985 (allout-pre-next-prefix)
2986 (if (and (not inclusive) (not (bobp)) (= ?\n (preceding-char)))
2987 (forward-char -1))
2988 (point))
2989 ;;;_ > allout-end-of-current-heading ()
2990 (defun allout-end-of-current-heading ()
2991 (interactive)
2992 (allout-beginning-of-current-entry)
2993 (search-forward "\n" nil t)
2994 (forward-char -1))
2995 (defalias 'allout-end-of-heading 'allout-end-of-current-heading)
2996 ;;;_ > allout-get-body-text ()
2997 (defun allout-get-body-text ()
2998 "Return the unmangled body text of the topic immediately containing point."
2999 (save-excursion
3000 (allout-end-of-prefix)
3001 (if (not (search-forward "\n" nil t))
3003 (backward-char 1)
3004 (let ((pre-body (point)))
3005 (if (not pre-body)
3007 (allout-end-of-entry t)
3008 (if (not (= pre-body (point)))
3009 (buffer-substring-no-properties (1+ pre-body) (point))))
3015 ;;;_ - Depth-wise
3016 ;;;_ > allout-ascend-to-depth (depth)
3017 (defun allout-ascend-to-depth (depth)
3018 "Ascend to depth DEPTH, returning depth if successful, nil if not."
3019 (if (and (> depth 0)(<= depth (allout-depth)))
3020 (let (last-ascended)
3021 (while (and (< depth allout-recent-depth)
3022 (setq last-ascended (allout-ascend))))
3023 (goto-char allout-recent-prefix-beginning)
3024 (if (interactive-p) (allout-end-of-prefix))
3025 (and last-ascended allout-recent-depth))))
3026 ;;;_ > allout-ascend ()
3027 (defun allout-ascend (&optional dont-move-if-unsuccessful)
3028 "Ascend one level, returning resulting depth if successful, nil if not.
3030 Point is left at the beginning of the level whether or not
3031 successful, unless optional DONT-MOVE-IF-UNSUCCESSFUL is set, in
3032 which case point is returned to its original starting location."
3033 (if dont-move-if-unsuccessful
3034 (setq dont-move-if-unsuccessful (point)))
3035 (prog1
3036 (if (allout-beginning-of-level)
3037 (let ((bolevel (point))
3038 (bolevel-depth allout-recent-depth))
3039 (allout-previous-heading)
3040 (cond ((< allout-recent-depth bolevel-depth)
3041 allout-recent-depth)
3042 ((= allout-recent-depth bolevel-depth)
3043 (if dont-move-if-unsuccessful
3044 (goto-char dont-move-if-unsuccessful))
3045 (allout-depth)
3046 nil)
3048 ;; some topic after very first is lower depth than first:
3049 (goto-char bolevel)
3050 (allout-depth)
3051 nil))))
3052 (if (interactive-p) (allout-end-of-prefix))))
3053 ;;;_ > allout-descend-to-depth (depth)
3054 (defun allout-descend-to-depth (depth)
3055 "Descend to depth DEPTH within current topic.
3057 Returning depth if successful, nil if not."
3058 (let ((start-point (point))
3059 (start-depth (allout-depth)))
3060 (while
3061 (and (> (allout-depth) 0)
3062 (not (= depth allout-recent-depth)) ; ... not there yet
3063 (allout-next-heading) ; ... go further
3064 (< start-depth allout-recent-depth))) ; ... still in topic
3065 (if (and (> (allout-depth) 0)
3066 (= allout-recent-depth depth))
3067 depth
3068 (goto-char start-point)
3069 nil))
3071 ;;;_ > allout-up-current-level (arg)
3072 (defun allout-up-current-level (arg)
3073 "Move out ARG levels from current visible topic."
3074 (interactive "p")
3075 (let ((start-point (point)))
3076 (allout-back-to-current-heading)
3077 (if (not (allout-ascend))
3078 (progn (goto-char start-point)
3079 (error "Can't ascend past outermost level"))
3080 (if (interactive-p) (allout-end-of-prefix))
3081 allout-recent-prefix-beginning)))
3083 ;;;_ - Linear
3084 ;;;_ > allout-next-sibling (&optional depth backward)
3085 (defun allout-next-sibling (&optional depth backward)
3086 "Like `allout-forward-current-level', but respects invisible topics.
3088 Traverse at optional DEPTH, or current depth if none specified.
3090 Go backward if optional arg BACKWARD is non-nil.
3092 Return the start point of the new topic if successful, nil otherwise."
3094 (if (if backward (bobp) (eobp))
3096 (let ((target-depth (or depth (allout-depth)))
3097 (start-point (point))
3098 (start-prefix-beginning allout-recent-prefix-beginning)
3099 (count 0)
3100 leaping
3101 last-depth)
3102 (while (and
3103 ;; done too few single steps to resort to the leap routine:
3104 (not leaping)
3105 ;; not at limit:
3106 (not (if backward (bobp) (eobp)))
3107 ;; still traversable:
3108 (if backward (allout-previous-heading) (allout-next-heading))
3109 ;; we're below the target depth
3110 (> (setq last-depth allout-recent-depth) target-depth))
3111 (setq count (1+ count))
3112 (if (> count 7) ; lists are commonly 7 +- 2, right?-)
3113 (setq leaping t)))
3114 (cond (leaping
3115 (or (allout-next-sibling-leap target-depth backward)
3116 (progn
3117 (goto-char start-point)
3118 (if depth (allout-depth) target-depth)
3119 nil)))
3120 ((and (not (eobp))
3121 (and (> (or last-depth (allout-depth)) 0)
3122 (= allout-recent-depth target-depth))
3123 (not (= start-prefix-beginning
3124 allout-recent-prefix-beginning)))
3125 allout-recent-prefix-beginning)
3127 (goto-char start-point)
3128 (if depth (allout-depth) target-depth)
3129 nil)))))
3130 ;;;_ > allout-next-sibling-leap (&optional depth backward)
3131 (defun allout-next-sibling-leap (&optional depth backward)
3132 "Like `allout-next-sibling', but by direct search for topic at depth.
3134 Traverse at optional DEPTH, or current depth if none specified.
3136 Go backward if optional arg BACKWARD is non-nil.
3138 Return the start point of the new topic if successful, nil otherwise.
3140 Costs more than regular `allout-next-sibling' for short traversals:
3142 - we have to check the prior (next, if travelling backwards)
3143 item to confirm connectivity with the prior topic, and
3144 - if confirmed, we have to reestablish the allout-recent-* settings with
3145 some extra navigation
3146 - if confirmation fails, we have to do more work to recover
3148 It is an increasingly big win when there are many intervening
3149 offspring before the next sibling, however, so
3150 `allout-next-sibling' resorts to this if it finds itself in that
3151 situation."
3153 (if (if backward (bobp) (eobp))
3155 (let* ((start-point (point))
3156 (target-depth (or depth (allout-depth)))
3157 (search-whitespace-regexp nil)
3158 (depth-biased (- target-depth 2))
3159 (expression (if (<= target-depth 1)
3160 allout-depth-one-regexp
3161 (format allout-depth-specific-regexp
3162 depth-biased depth-biased)))
3163 found
3164 done)
3165 (while (not done)
3166 (setq found (save-match-data
3167 (if backward
3168 (re-search-backward expression nil 'to-limit)
3169 (forward-char 1)
3170 (re-search-forward expression nil 'to-limit))))
3171 (if (and found (allout-aberrant-container-p))
3172 (setq found nil))
3173 (setq done (or found (if backward (bobp) (eobp)))))
3174 (if (not found)
3175 (progn (goto-char start-point)
3176 nil)
3177 ;; rationale: if any intervening items were at a lower depth, we
3178 ;; would now be on the first offspring at the target depth -- ie,
3179 ;; the preceeding item (per the search direction) must be at a
3180 ;; lesser depth. that's all we need to check.
3181 (if backward (allout-next-heading) (allout-previous-heading))
3182 (if (< allout-recent-depth target-depth)
3183 ;; return to start and reestablish allout-recent-*:
3184 (progn
3185 (goto-char start-point)
3186 (allout-depth)
3187 nil)
3188 (goto-char found)
3189 ;; locate cursor and set allout-recent-*:
3190 (allout-goto-prefix))))))
3191 ;;;_ > allout-previous-sibling (&optional depth backward)
3192 (defun allout-previous-sibling (&optional depth backward)
3193 "Like `allout-forward-current-level' backwards, respecting invisible topics.
3195 Optional DEPTH specifies depth to traverse, default current depth.
3197 Optional BACKWARD reverses direction.
3199 Return depth if successful, nil otherwise."
3200 (allout-next-sibling depth (not backward))
3202 ;;;_ > allout-snug-back ()
3203 (defun allout-snug-back ()
3204 "Position cursor at end of previous topic.
3206 Presumes point is at the start of a topic prefix."
3207 (if (or (bobp) (eobp))
3209 (forward-char -1))
3210 (if (or (bobp) (not (= ?\n (preceding-char))))
3212 (forward-char -1))
3213 (point))
3214 ;;;_ > allout-beginning-of-level ()
3215 (defun allout-beginning-of-level ()
3216 "Go back to the first sibling at this level, visible or not."
3217 (allout-end-of-level 'backward))
3218 ;;;_ > allout-end-of-level (&optional backward)
3219 (defun allout-end-of-level (&optional backward)
3220 "Go to the last sibling at this level, visible or not."
3222 (let ((depth (allout-depth)))
3223 (while (allout-previous-sibling depth nil))
3224 (prog1 allout-recent-depth
3225 (if (interactive-p) (allout-end-of-prefix)))))
3226 ;;;_ > allout-next-visible-heading (arg)
3227 (defun allout-next-visible-heading (arg)
3228 "Move to the next ARG'th visible heading line, backward if arg is negative.
3230 Move to buffer limit in indicated direction if headings are exhausted."
3232 (interactive "p")
3233 (let* ((inhibit-field-text-motion t)
3234 (backward (if (< arg 0) (setq arg (* -1 arg))))
3235 (step (if backward -1 1))
3236 prev got)
3238 (while (> arg 0)
3239 (while (and
3240 ;; Boundary condition:
3241 (not (if backward (bobp)(eobp)))
3242 ;; Move, skipping over all concealed lines in one fell swoop:
3243 (prog1 (condition-case nil (or (line-move step) t)
3244 (error nil))
3245 (allout-beginning-of-current-line))
3246 ;; Deal with apparent header line:
3247 (save-match-data
3248 (if (not (looking-at allout-regexp))
3249 ;; not a header line, keep looking:
3251 (allout-prefix-data)
3252 (if (and (allout-do-doublecheck)
3253 (allout-aberrant-container-p))
3254 ;; skip this aberrant prospective header line:
3256 ;; this prospective headerline qualifies -- register:
3257 (setq got allout-recent-prefix-beginning)
3258 ;; and break the loop:
3259 nil)))))
3260 ;; Register this got, it may be the last:
3261 (if got (setq prev got))
3262 (setq arg (1- arg)))
3263 (cond (got ; Last move was to a prefix:
3264 (allout-end-of-prefix))
3265 (prev ; Last move wasn't, but prev was:
3266 (goto-char prev)
3267 (allout-end-of-prefix))
3268 ((not backward) (end-of-line) nil))))
3269 ;;;_ > allout-previous-visible-heading (arg)
3270 (defun allout-previous-visible-heading (arg)
3271 "Move to the previous heading line.
3273 With argument, repeats or can move forward if negative.
3274 A heading line is one that starts with a `*' (or that `allout-regexp'
3275 matches)."
3276 (interactive "p")
3277 (prog1 (allout-next-visible-heading (- arg))
3278 (if (interactive-p) (allout-end-of-prefix))))
3279 ;;;_ > allout-forward-current-level (arg)
3280 (defun allout-forward-current-level (arg)
3281 "Position point at the next heading of the same level.
3283 Takes optional repeat-count, goes backward if count is negative.
3285 Returns resulting position, else nil if none found."
3286 (interactive "p")
3287 (let ((start-depth (allout-current-depth))
3288 (start-arg arg)
3289 (backward (> 0 arg)))
3290 (if (= 0 start-depth)
3291 (error "No siblings, not in a topic..."))
3292 (if backward (setq arg (* -1 arg)))
3293 (allout-back-to-current-heading)
3294 (while (and (not (zerop arg))
3295 (if backward
3296 (allout-previous-sibling)
3297 (allout-next-sibling)))
3298 (setq arg (1- arg)))
3299 (if (not (interactive-p))
3301 (allout-end-of-prefix)
3302 (if (not (zerop arg))
3303 (error "Hit %s level %d topic, traversed %d of %d requested"
3304 (if backward "first" "last")
3305 allout-recent-depth
3306 (- (abs start-arg) arg)
3307 (abs start-arg))))))
3308 ;;;_ > allout-backward-current-level (arg)
3309 (defun allout-backward-current-level (arg)
3310 "Inverse of `allout-forward-current-level'."
3311 (interactive "p")
3312 (if (interactive-p)
3313 (let ((current-prefix-arg (* -1 arg)))
3314 (call-interactively 'allout-forward-current-level))
3315 (allout-forward-current-level (* -1 arg))))
3317 ;;;_ #5 Alteration
3319 ;;;_ - Fundamental
3320 ;;;_ = allout-post-goto-bullet
3321 (defvar allout-post-goto-bullet nil
3322 "Outline internal var, for `allout-pre-command-business' hot-spot operation.
3324 When set, tells post-processing to reposition on topic bullet, and
3325 then unset it. Set by `allout-pre-command-business' when implementing
3326 hot-spot operation, where literal characters typed over a topic bullet
3327 are mapped to the command of the corresponding control-key on the
3328 `allout-mode-map'.")
3329 (make-variable-buffer-local 'allout-post-goto-bullet)
3330 ;;;_ = allout-command-counter
3331 (defvar allout-command-counter 0
3332 "Counter that monotonically increases in allout-mode buffers.
3334 Set by `allout-pre-command-business', to support allout addons in
3335 coordinating with allout activity.")
3336 (make-variable-buffer-local 'allout-command-counter)
3337 ;;;_ > allout-post-command-business ()
3338 (defun allout-post-command-business ()
3339 "Outline `post-command-hook' function.
3341 - Implement (and clear) `allout-post-goto-bullet', for hot-spot
3342 outline commands.
3344 - Decrypt topic currently being edited if it was encrypted for a save."
3346 ; Apply any external change func:
3347 (if (not (allout-mode-p)) ; In allout-mode.
3350 (if (and (boundp 'allout-after-save-decrypt)
3351 allout-after-save-decrypt)
3352 (allout-after-saves-handler))
3354 ;; Implement allout-post-goto-bullet, if set:
3355 (if (and allout-post-goto-bullet
3356 (allout-current-bullet-pos))
3357 (progn (goto-char (allout-current-bullet-pos))
3358 (setq allout-post-goto-bullet nil)))
3360 ;;;_ > allout-pre-command-business ()
3361 (defun allout-pre-command-business ()
3362 "Outline `pre-command-hook' function for outline buffers.
3364 Among other things, implements special behavior when the cursor is on the
3365 topic bullet character.
3367 When the cursor is on the bullet character, self-insert characters are
3368 reinterpreted as the corresponding control-character in the
3369 `allout-mode-map'. The `allout-mode' `post-command-hook' insures that
3370 the cursor which has moved as a result of such reinterpretation is
3371 positioned on the bullet character of the destination topic.
3373 The upshot is that you can get easy, single (ie, unmodified) key
3374 outline maneuvering operations by positioning the cursor on the bullet
3375 char. When in this mode you can use regular cursor-positioning
3376 command/keystrokes to relocate the cursor off of a bullet character to
3377 return to regular interpretation of self-insert characters."
3379 (if (not (allout-mode-p))
3381 ;; Increment allout-command-counter
3382 (setq allout-command-counter (1+ allout-command-counter))
3383 ;; Do hot-spot navigation.
3384 (if (and (eq this-command 'self-insert-command)
3385 (eq (point)(allout-current-bullet-pos)))
3386 (allout-hotspot-key-handler))))
3387 ;;;_ > allout-hotspot-key-handler ()
3388 (defun allout-hotspot-key-handler ()
3389 "Catchall handling of key bindings in hot-spots.
3391 Translates unmodified keystrokes to corresponding allout commands, when
3392 they would qualify if prefixed with the allout-command-prefix, and sets
3393 this-command accordingly.
3395 Returns the qualifying command, if any, else nil."
3396 (interactive)
3397 (let* ((key-string (if (numberp last-command-char)
3398 (char-to-string last-command-char)))
3399 (key-num (cond ((numberp last-command-char) last-command-char)
3400 ;; for XEmacs character type:
3401 ((and (fboundp 'characterp)
3402 (apply 'characterp (list last-command-char)))
3403 (apply 'char-to-int (list last-command-char)))
3404 (t 0)))
3405 mapped-binding)
3407 (if (zerop key-num)
3410 (if (and
3411 ;; exclude control chars and escape:
3412 (<= 33 key-num)
3413 (setq mapped-binding
3414 (or (and (assoc key-string allout-keybindings-list)
3415 ;; translate literal membership on list:
3416 (cadr (assoc key-string allout-keybindings-list)))
3417 ;; translate as a keybinding:
3418 (key-binding (vconcat allout-command-prefix
3419 (char-to-string
3420 (if (and (<= 97 key-num) ; "a"
3421 (>= 122 key-num)) ; "z"
3422 (- key-num 96) key-num)))
3423 t))))
3424 ;; Qualified as an allout command -- do hot-spot operation.
3425 (setq allout-post-goto-bullet t)
3426 ;; accept-defaults nil, or else we'll get allout-item-icon-key-handler.
3427 (setq mapped-binding (key-binding (char-to-string key-num))))
3429 (while (keymapp mapped-binding)
3430 (setq mapped-binding
3431 (lookup-key mapped-binding (vector (read-char)))))
3433 (if mapped-binding
3434 (setq this-command mapped-binding)))))
3436 ;;;_ > allout-find-file-hook ()
3437 (defun allout-find-file-hook ()
3438 "Activate `allout-mode' on non-nil `allout-auto-activation', `allout-layout'.
3440 See `allout-init' for setup instructions."
3441 (if (and allout-auto-activation
3442 (not (allout-mode-p))
3443 allout-layout)
3444 (allout-mode t)))
3446 ;;;_ - Topic Format Assessment
3447 ;;;_ > allout-solicit-alternate-bullet (depth &optional current-bullet)
3448 (defun allout-solicit-alternate-bullet (depth &optional current-bullet)
3450 "Prompt for and return a bullet char as an alternative to the current one.
3452 Offer one suitable for current depth DEPTH as default."
3454 (let* ((default-bullet (or (and (stringp current-bullet) current-bullet)
3455 (allout-bullet-for-depth depth)))
3456 (sans-escapes (regexp-sans-escapes allout-bullets-string))
3457 choice)
3458 (save-excursion
3459 (goto-char (allout-current-bullet-pos))
3460 (setq choice (solicit-char-in-string
3461 (format "Select bullet: %s ('%s' default): "
3462 sans-escapes
3463 (substring-no-properties default-bullet))
3464 sans-escapes
3465 t)))
3466 (message "")
3467 (if (string= choice "") default-bullet choice))
3469 ;;;_ > allout-distinctive-bullet (bullet)
3470 (defun allout-distinctive-bullet (bullet)
3471 "True if BULLET is one of those on `allout-distinctive-bullets-string'."
3472 (string-match (regexp-quote bullet) allout-distinctive-bullets-string))
3473 ;;;_ > allout-numbered-type-prefix (&optional prefix)
3474 (defun allout-numbered-type-prefix (&optional prefix)
3475 "True if current header prefix bullet is numbered bullet."
3476 (and allout-numbered-bullet
3477 (string= allout-numbered-bullet
3478 (if prefix
3479 (allout-get-prefix-bullet prefix)
3480 (allout-get-bullet)))))
3481 ;;;_ > allout-encrypted-type-prefix (&optional prefix)
3482 (defun allout-encrypted-type-prefix (&optional prefix)
3483 "True if current header prefix bullet is for an encrypted entry (body)."
3484 (and allout-topic-encryption-bullet
3485 (string= allout-topic-encryption-bullet
3486 (if prefix
3487 (allout-get-prefix-bullet prefix)
3488 (allout-get-bullet)))))
3489 ;;;_ > allout-bullet-for-depth (&optional depth)
3490 (defun allout-bullet-for-depth (&optional depth)
3491 "Return outline topic bullet suited to optional DEPTH, or current depth."
3492 ;; Find bullet in plain-bullets-string modulo DEPTH.
3493 (if allout-stylish-prefixes
3494 (char-to-string (aref allout-plain-bullets-string
3495 (% (max 0 (- depth 2))
3496 allout-plain-bullets-string-len)))
3497 allout-primary-bullet)
3500 ;;;_ - Topic Production
3501 ;;;_ > allout-make-topic-prefix (&optional prior-bullet
3502 (defun allout-make-topic-prefix (&optional prior-bullet
3504 depth
3505 solicit
3506 number-control
3507 index)
3508 ;; Depth null means use current depth, non-null means we're either
3509 ;; opening a new topic after current topic, lower or higher, or we're
3510 ;; changing level of current topic.
3511 ;; Solicit dominates specified bullet-char.
3512 ;;;_ . Doc string:
3513 "Generate a topic prefix suitable for optional arg DEPTH, or current depth.
3515 All the arguments are optional.
3517 PRIOR-BULLET indicates the bullet of the prefix being changed, or
3518 nil if none. This bullet may be preserved (other options
3519 notwithstanding) if it is on the `allout-distinctive-bullets-string',
3520 for instance.
3522 Second arg NEW indicates that a new topic is being opened after the
3523 topic at point, if non-nil. Default bullet for new topics, eg, may
3524 be set (contingent to other args) to numbered bullets if previous
3525 sibling is one. The implication otherwise is that the current topic
3526 is being adjusted -- shifted or rebulleted -- and we don't consider
3527 bullet or previous sibling.
3529 Third arg DEPTH forces the topic prefix to that depth, regardless of
3530 the current topics' depth.
3532 If SOLICIT is non-nil, then the choice of bullet is solicited from
3533 user. If it's a character, then that character is offered as the
3534 default, otherwise the one suited to the context (according to
3535 distinction or depth) is offered. (This overrides other options,
3536 including, eg, a distinctive PRIOR-BULLET.) If non-nil, then the
3537 context-specific bullet is used.
3539 Fifth arg, NUMBER-CONTROL, matters only if `allout-numbered-bullet'
3540 is non-nil *and* soliciting was not explicitly invoked. Then
3541 NUMBER-CONTROL non-nil forces prefix to either numbered or
3542 denumbered format, depending on the value of the sixth arg, INDEX.
3544 \(Note that NUMBER-CONTROL does *not* apply to level 1 topics. Sorry...)
3546 If NUMBER-CONTROL is non-nil and sixth arg INDEX is non-nil then
3547 the prefix of the topic is forced to be numbered. Non-nil
3548 NUMBER-CONTROL and nil INDEX forces non-numbered format on the
3549 bullet. Non-nil NUMBER-CONTROL and non-nil, non-number INDEX means
3550 that the index for the numbered prefix will be derived, by counting
3551 siblings back to start of level. If INDEX is a number, then that
3552 number is used as the index for the numbered prefix (allowing, eg,
3553 sequential renumbering to not require this function counting back the
3554 index for each successive sibling)."
3555 ;;;_ . Code:
3556 ;; The options are ordered in likely frequence of use, most common
3557 ;; highest, least lowest. Ie, more likely to be doing prefix
3558 ;; adjustments than soliciting, and yet more than numbering.
3559 ;; Current prefix is least dominant, but most likely to be commonly
3560 ;; specified...
3562 (let* (body
3563 numbering
3564 denumbering
3565 (depth (or depth (allout-depth)))
3566 (header-lead allout-header-prefix)
3567 (bullet-char
3569 ;; Getting value for bullet char is practically the whole job:
3571 (cond
3572 ; Simplest situation -- level 1:
3573 ((<= depth 1) (setq header-lead "") allout-primary-bullet)
3574 ; Simple, too: all asterisks:
3575 (allout-old-style-prefixes
3576 ;; Cheat -- make body the whole thing, null out header-lead and
3577 ;; bullet-char:
3578 (setq body (make-string depth
3579 (string-to-char allout-primary-bullet)))
3580 (setq header-lead "")
3583 ;; (Neither level 1 nor old-style, so we're space padding.
3584 ;; Sneak it in the condition of the next case, whatever it is.)
3586 ;; Solicitation overrides numbering and other cases:
3587 ((progn (setq body (make-string (- depth 2) ?\ ))
3588 ;; The actual condition:
3589 solicit)
3590 (let* ((got (allout-solicit-alternate-bullet depth solicit)))
3591 ;; Gotta check whether we're numbering and got a numbered bullet:
3592 (setq numbering (and allout-numbered-bullet
3593 (not (and number-control (not index)))
3594 (string= got allout-numbered-bullet)))
3595 ;; Now return what we got, regardless:
3596 got))
3598 ;; Numbering invoked through args:
3599 ((and allout-numbered-bullet number-control)
3600 (if (setq numbering (not (setq denumbering (not index))))
3601 allout-numbered-bullet
3602 (if (and prior-bullet
3603 (not (string= allout-numbered-bullet
3604 prior-bullet)))
3605 prior-bullet
3606 (allout-bullet-for-depth depth))))
3608 ;;; Neither soliciting nor controlled numbering ;;;
3609 ;;; (may be controlled denumbering, tho) ;;;
3611 ;; Check wrt previous sibling:
3612 ((and new ; only check for new prefixes
3613 (<= depth (allout-depth))
3614 allout-numbered-bullet ; ... & numbering enabled
3615 (not denumbering)
3616 (let ((sibling-bullet
3617 (save-excursion
3618 ;; Locate correct sibling:
3619 (or (>= depth (allout-depth))
3620 (allout-ascend-to-depth depth))
3621 (allout-get-bullet))))
3622 (if (and sibling-bullet
3623 (string= allout-numbered-bullet sibling-bullet))
3624 (setq numbering sibling-bullet)))))
3626 ;; Distinctive prior bullet?
3627 ((and prior-bullet
3628 (allout-distinctive-bullet prior-bullet)
3629 ;; Either non-numbered:
3630 (or (not (and allout-numbered-bullet
3631 (string= prior-bullet allout-numbered-bullet)))
3632 ;; or numbered, and not denumbering:
3633 (setq numbering (not denumbering)))
3634 ;; Here 'tis:
3635 prior-bullet))
3637 ;; Else, standard bullet per depth:
3638 ((allout-bullet-for-depth depth)))))
3640 (concat header-lead
3641 body
3642 bullet-char
3643 (if numbering
3644 (format "%d" (cond ((and index (numberp index)) index)
3645 (new (1+ (allout-sibling-index depth)))
3646 ((allout-sibling-index))))))
3649 ;;;_ > allout-open-topic (relative-depth &optional before offer-recent-bullet)
3650 (defun allout-open-topic (relative-depth &optional before offer-recent-bullet)
3651 "Open a new topic at depth DEPTH.
3653 New topic is situated after current one, unless optional flag BEFORE
3654 is non-nil, or unless current line is completely empty -- lacking even
3655 whitespace -- in which case open is done on the current line.
3657 When adding an offspring, it will be added immediately after the parent if
3658 the other offspring are exposed, or after the last child if the offspring
3659 are hidden. (The intervening offspring will be exposed in the latter
3660 case.)
3662 If OFFER-RECENT-BULLET is true, offer to use the bullet of the prior sibling.
3664 Nuances:
3666 - Creation of new topics is with respect to the visible topic
3667 containing the cursor, regardless of intervening concealed ones.
3669 - New headers are generally created after/before the body of a
3670 topic. However, they are created right at cursor location if the
3671 cursor is on a blank line, even if that breaks the current topic
3672 body. This is intentional, to provide a simple means for
3673 deliberately dividing topic bodies.
3675 - Double spacing of topic lists is preserved. Also, the first
3676 level two topic is created double-spaced (and so would be
3677 subsequent siblings, if that's left intact). Otherwise,
3678 single-spacing is used.
3680 - Creation of sibling or nested topics is with respect to the topic
3681 you're starting from, even when creating backwards. This way you
3682 can easily create a sibling in front of the current topic without
3683 having to go to its preceding sibling, and then open forward
3684 from there."
3686 (allout-beginning-of-current-line)
3687 (save-match-data
3688 (let* ((inhibit-field-text-motion t)
3689 (depth (+ (allout-current-depth) relative-depth))
3690 (opening-on-blank (if (looking-at "^\$")
3691 (not (setq before nil))))
3692 ;; bunch o vars set while computing ref-topic
3693 opening-numbered
3694 ref-depth
3695 ref-bullet
3696 (ref-topic (save-excursion
3697 (cond ((< relative-depth 0)
3698 (allout-ascend-to-depth depth))
3699 ((>= relative-depth 1) nil)
3700 (t (allout-back-to-current-heading)))
3701 (setq ref-depth allout-recent-depth)
3702 (setq ref-bullet
3703 (if (> allout-recent-prefix-end 1)
3704 (allout-recent-bullet)
3705 ""))
3706 (setq opening-numbered
3707 (save-excursion
3708 (and allout-numbered-bullet
3709 (or (<= relative-depth 0)
3710 (allout-descend-to-depth depth))
3711 (if (allout-numbered-type-prefix)
3712 allout-numbered-bullet))))
3713 (point)))
3714 dbl-space
3715 doing-beginning
3716 start end)
3718 (if (not opening-on-blank)
3719 ; Positioning and vertical
3720 ; padding -- only if not
3721 ; opening-on-blank:
3722 (progn
3723 (goto-char ref-topic)
3724 (setq dbl-space ; Determine double space action:
3725 (or (and (<= relative-depth 0) ; not descending;
3726 (save-excursion
3727 ;; at b-o-b or preceded by a blank line?
3728 (or (> 0 (forward-line -1))
3729 (looking-at "^\\s-*$")
3730 (bobp)))
3731 (save-excursion
3732 ;; succeeded by a blank line?
3733 (allout-end-of-current-subtree)
3734 (looking-at "\n\n")))
3735 (and (= ref-depth 1)
3736 (or before
3737 (= depth 1)
3738 (save-excursion
3739 ;; Don't already have following
3740 ;; vertical padding:
3741 (not (allout-pre-next-prefix)))))))
3743 ;; Position to prior heading, if inserting backwards, and not
3744 ;; going outwards:
3745 (if (and before (>= relative-depth 0))
3746 (progn (allout-back-to-current-heading)
3747 (setq doing-beginning (bobp))
3748 (if (not (bobp))
3749 (allout-previous-heading)))
3750 (if (and before (bobp))
3751 (open-line 1)))
3753 (if (<= relative-depth 0)
3754 ;; Not going inwards, don't snug up:
3755 (if doing-beginning
3756 (if (not dbl-space)
3757 (open-line 1)
3758 (open-line 2))
3759 (if before
3760 (progn (end-of-line)
3761 (allout-pre-next-prefix)
3762 (while (and (= ?\n (following-char))
3763 (save-excursion
3764 (forward-char 1)
3765 (allout-hidden-p)))
3766 (forward-char 1))
3767 (if (not (looking-at "^$"))
3768 (open-line 1)))
3769 (allout-end-of-current-subtree)
3770 (if (looking-at "\n\n") (forward-char 1))))
3771 ;; Going inwards -- double-space if first offspring is
3772 ;; double-spaced, otherwise snug up.
3773 (allout-end-of-entry)
3774 (if (eobp)
3775 (newline 1)
3776 (line-move 1))
3777 (allout-beginning-of-current-line)
3778 (backward-char 1)
3779 (if (bolp)
3780 ;; Blank lines between current header body and next
3781 ;; header -- get to last substantive (non-white-space)
3782 ;; line in body:
3783 (progn (setq dbl-space t)
3784 (re-search-backward "[^ \t\n]" nil t)))
3785 (if (looking-at "\n\n")
3786 (setq dbl-space t))
3787 (if (save-excursion
3788 (allout-next-heading)
3789 (when (> allout-recent-depth ref-depth)
3790 ;; This is an offspring.
3791 (forward-line -1)
3792 (looking-at "^\\s-*$")))
3793 (progn (forward-line 1)
3794 (open-line 1)
3795 (forward-line 1)))
3796 (allout-end-of-current-line))
3798 ;;(if doing-beginning (goto-char doing-beginning))
3799 (if (not (bobp))
3800 ;; We insert a newline char rather than using open-line to
3801 ;; avoid rear-stickiness inheritence of read-only property.
3802 (progn (if (and (not (> depth ref-depth))
3803 (not before))
3804 (open-line 1)
3805 (if (and (not dbl-space) (> depth ref-depth))
3806 (newline 1)
3807 (if dbl-space
3808 (open-line 1)
3809 (if (not before)
3810 (newline 1)))))
3811 (if (and dbl-space (not (> relative-depth 0)))
3812 (newline 1))
3813 (if (and (not (eobp))
3814 (or (not (bolp))
3815 (and (not (bobp))
3816 ;; bolp doesnt detect concealed
3817 ;; trailing newlines, compensate:
3818 (save-excursion
3819 (forward-char -1)
3820 (allout-hidden-p)))))
3821 (forward-char 1))))
3823 (setq start (point))
3824 (insert (concat (allout-make-topic-prefix opening-numbered t depth)
3825 " "))
3826 (setq end (1+ (point)))
3828 (allout-rebullet-heading (and offer-recent-bullet ref-bullet)
3829 depth nil nil t)
3830 (if (> relative-depth 0)
3831 (save-excursion (goto-char ref-topic)
3832 (allout-show-children)))
3833 (end-of-line)
3835 (run-hook-with-args 'allout-structure-added-hook start end)
3839 ;;;_ > allout-open-subtopic (arg)
3840 (defun allout-open-subtopic (arg)
3841 "Open new topic header at deeper level than the current one.
3843 Negative universal arg means to open deeper, but place the new topic
3844 prior to the current one."
3845 (interactive "p")
3846 (allout-open-topic 1 (> 0 arg) (< 1 arg)))
3847 ;;;_ > allout-open-sibtopic (arg)
3848 (defun allout-open-sibtopic (arg)
3849 "Open new topic header at same level as the current one.
3851 Positive universal arg means to use the bullet of the prior sibling.
3853 Negative universal arg means to place the new topic prior to the current
3854 one."
3855 (interactive "p")
3856 (allout-open-topic 0 (> 0 arg) (not (= 1 arg))))
3857 ;;;_ > allout-open-supertopic (arg)
3858 (defun allout-open-supertopic (arg)
3859 "Open new topic header at shallower level than the current one.
3861 Negative universal arg means to open shallower, but place the new
3862 topic prior to the current one."
3864 (interactive "p")
3865 (allout-open-topic -1 (> 0 arg) (< 1 arg)))
3867 ;;;_ - Outline Alteration
3868 ;;;_ : Topic Modification
3869 ;;;_ = allout-former-auto-filler
3870 (defvar allout-former-auto-filler nil
3871 "Name of modal fill function being wrapped by `allout-auto-fill'.")
3872 ;;;_ > allout-auto-fill ()
3873 (defun allout-auto-fill ()
3874 "`allout-mode' autofill function.
3876 Maintains outline hanging topic indentation if
3877 `allout-use-hanging-indents' is set."
3879 (when (not allout-inhibit-auto-fill)
3880 (let ((fill-prefix (if allout-use-hanging-indents
3881 ;; Check for topic header indentation:
3882 (save-match-data
3883 (save-excursion
3884 (beginning-of-line)
3885 (if (looking-at allout-regexp)
3886 ;; ... construct indentation to account for
3887 ;; length of topic prefix:
3888 (make-string (progn (allout-end-of-prefix)
3889 (current-column))
3890 ?\ ))))))
3891 (use-auto-fill-function (or allout-outside-normal-auto-fill-function
3892 auto-fill-function
3893 'do-auto-fill)))
3894 (if (or allout-former-auto-filler allout-use-hanging-indents)
3895 (funcall use-auto-fill-function)))))
3896 ;;;_ > allout-reindent-body (old-depth new-depth &optional number)
3897 (defun allout-reindent-body (old-depth new-depth &optional number)
3898 "Reindent body lines which were indented at OLD-DEPTH to NEW-DEPTH.
3900 Optional arg NUMBER indicates numbering is being added, and it must
3901 be accommodated.
3903 Note that refill of indented paragraphs is not done."
3905 (save-excursion
3906 (allout-end-of-prefix)
3907 (let* ((new-margin (current-column))
3908 excess old-indent-begin old-indent-end
3909 ;; We want the column where the header-prefix text started
3910 ;; *before* the prefix was changed, so we infer it relative
3911 ;; to the new margin and the shift in depth:
3912 (old-margin (+ old-depth (- new-margin new-depth))))
3914 ;; Process lines up to (but excluding) next topic header:
3915 (allout-unprotected
3916 (save-match-data
3917 (while
3918 (and (re-search-forward "\n\\(\\s-*\\)"
3921 ;; Register the indent data, before we reset the
3922 ;; match data with a subsequent `looking-at':
3923 (setq old-indent-begin (match-beginning 1)
3924 old-indent-end (match-end 1))
3925 (not (looking-at allout-regexp)))
3926 (if (> 0 (setq excess (- (- old-indent-end old-indent-begin)
3927 old-margin)))
3928 ;; Text starts left of old margin -- don't adjust:
3930 ;; Text was hanging at or right of old left margin --
3931 ;; reindent it, preserving its existing indentation
3932 ;; beyond the old margin:
3933 (delete-region old-indent-begin old-indent-end)
3934 (indent-to (+ new-margin excess (current-column))))))))))
3935 ;;;_ > allout-rebullet-current-heading (arg)
3936 (defun allout-rebullet-current-heading (arg)
3937 "Solicit new bullet for current visible heading."
3938 (interactive "p")
3939 (let ((initial-col (current-column))
3940 (on-bullet (eq (point)(allout-current-bullet-pos)))
3941 from to
3942 (backwards (if (< arg 0)
3943 (setq arg (* arg -1)))))
3944 (while (> arg 0)
3945 (save-excursion (allout-back-to-current-heading)
3946 (allout-end-of-prefix)
3947 (setq from allout-recent-prefix-beginning
3948 to allout-recent-prefix-end)
3949 (allout-rebullet-heading t ;;; solicit
3950 nil ;;; depth
3951 nil ;;; number-control
3952 nil ;;; index
3953 t) ;;; do-successors
3954 (run-hook-with-args 'allout-exposure-change-hook
3955 from to t))
3956 (setq arg (1- arg))
3957 (if (<= arg 0)
3959 (setq initial-col nil) ; Override positioning back to init col
3960 (if (not backwards)
3961 (allout-next-visible-heading 1)
3962 (allout-goto-prefix-doublechecked)
3963 (allout-next-visible-heading -1))))
3964 (message "Done.")
3965 (cond (on-bullet (goto-char (allout-current-bullet-pos)))
3966 (initial-col (move-to-column initial-col)))))
3967 ;;;_ > allout-rebullet-heading (&optional solicit ...)
3968 (defun allout-rebullet-heading (&optional solicit
3969 new-depth
3970 number-control
3971 index
3972 do-successors)
3974 "Adjust bullet of current topic prefix.
3976 All args are optional.
3978 If SOLICIT is non-nil, then the choice of bullet is solicited from
3979 user. If it's a character, then that character is offered as the
3980 default, otherwise the one suited to the context (according to
3981 distinction or depth) is offered. If non-nil, then the
3982 context-specific bullet is just used.
3984 Second arg DEPTH forces the topic prefix to that depth, regardless
3985 of the topic's current depth.
3987 Third arg NUMBER-CONTROL can force the prefix to or away from
3988 numbered form. It has effect only if `allout-numbered-bullet' is
3989 non-nil and soliciting was not explicitly invoked (via first arg).
3990 Its effect, numbering or denumbering, then depends on the setting
3991 of the fourth arg, INDEX.
3993 If NUMBER-CONTROL is non-nil and fourth arg INDEX is nil, then the
3994 prefix of the topic is forced to be non-numbered. Null index and
3995 non-nil NUMBER-CONTROL forces denumbering. Non-nil INDEX (and
3996 non-nil NUMBER-CONTROL) forces a numbered-prefix form. If non-nil
3997 INDEX is a number, then that number is used for the numbered
3998 prefix. Non-nil and non-number means that the index for the
3999 numbered prefix will be derived by allout-make-topic-prefix.
4001 Fifth arg DO-SUCCESSORS t means re-resolve count on succeeding
4002 siblings.
4004 Cf vars `allout-stylish-prefixes', `allout-old-style-prefixes',
4005 and `allout-numbered-bullet', which all affect the behavior of
4006 this function."
4008 (let* ((current-depth (allout-depth))
4009 (new-depth (or new-depth current-depth))
4010 (mb allout-recent-prefix-beginning)
4011 (me allout-recent-prefix-end)
4012 (current-bullet (buffer-substring-no-properties (- me 1) me))
4013 (has-annotation (get-text-property mb 'allout-was-hidden))
4014 (new-prefix (allout-make-topic-prefix current-bullet
4016 new-depth
4017 solicit
4018 number-control
4019 index)))
4021 ;; Is new one is identical to old?
4022 (if (and (= current-depth new-depth)
4023 (string= current-bullet
4024 (substring new-prefix (1- (length new-prefix)))))
4025 ;; Nothing to do:
4028 ;; New prefix probably different from old:
4029 ; get rid of old one:
4030 (allout-unprotected (delete-region mb me))
4031 (goto-char mb)
4032 ; Dispense with number if
4033 ; numbered-bullet prefix:
4034 (save-match-data
4035 (if (and allout-numbered-bullet
4036 (string= allout-numbered-bullet current-bullet)
4037 (looking-at "[0-9]+"))
4038 (allout-unprotected
4039 (delete-region (match-beginning 0)(match-end 0)))))
4041 ;; convey 'allout-was-hidden annotation, if original had it:
4042 (if has-annotation
4043 (put-text-property 0 (length new-prefix) 'allout-was-hidden t
4044 new-prefix))
4046 ; Put in new prefix:
4047 (allout-unprotected (insert new-prefix))
4049 ;; Reindent the body if elected, margin changed, and not encrypted body:
4050 (if (and allout-reindent-bodies
4051 (not (= new-depth current-depth))
4052 (not (allout-encrypted-topic-p)))
4053 (allout-reindent-body current-depth new-depth))
4055 ;; Recursively rectify successive siblings of orig topic if
4056 ;; caller elected for it:
4057 (if do-successors
4058 (save-excursion
4059 (while (allout-next-sibling new-depth nil)
4060 (setq index
4061 (cond ((numberp index) (1+ index))
4062 ((not number-control) (allout-sibling-index))))
4063 (if (allout-numbered-type-prefix)
4064 (allout-rebullet-heading nil ;;; solicit
4065 new-depth ;;; new-depth
4066 number-control;;; number-control
4067 index ;;; index
4068 nil))))) ;;;(dont!)do-successors
4069 ) ; (if (and (= current-depth new-depth)...))
4070 ) ; let* ((current-depth (allout-depth))...)
4071 ) ; defun
4072 ;;;_ > allout-rebullet-topic (arg)
4073 (defun allout-rebullet-topic (arg &optional sans-offspring)
4074 "Rebullet the visible topic containing point and all contained subtopics.
4076 Descends into invisible as well as visible topics, however.
4078 When optional SANS-OFFSPRING is non-nil, subtopics are not
4079 shifted. (Shifting a topic outwards without shifting its
4080 offspring is disallowed, since this would create a \"containment
4081 discontinuity\", where the depth difference between a topic and
4082 its immediate offspring is greater than one.)
4084 With repeat count, shift topic depth by that amount."
4085 (interactive "P")
4086 (let ((start-col (current-column)))
4087 (save-excursion
4088 ;; Normalize arg:
4089 (cond ((null arg) (setq arg 0))
4090 ((listp arg) (setq arg (car arg))))
4091 ;; Fill the user in, in case we're shifting a big topic:
4092 (if (not (zerop arg)) (message "Shifting..."))
4093 (allout-back-to-current-heading)
4094 (if (<= (+ allout-recent-depth arg) 0)
4095 (error "Attempt to shift topic below level 1"))
4096 (allout-rebullet-topic-grunt arg nil nil nil nil sans-offspring)
4097 (if (not (zerop arg)) (message "Shifting... done.")))
4098 (move-to-column (max 0 (+ start-col arg)))))
4099 ;;;_ > allout-rebullet-topic-grunt (&optional relative-depth ...)
4100 (defun allout-rebullet-topic-grunt (&optional relative-depth
4101 starting-depth
4102 starting-point
4103 index
4104 do-successors
4105 sans-offspring)
4106 "Like `allout-rebullet-topic', but on nearest containing topic
4107 \(visible or not).
4109 See `allout-rebullet-heading' for rebulleting behavior.
4111 All arguments are optional.
4113 First arg RELATIVE-DEPTH means to shift the depth of the entire
4114 topic that amount.
4116 Several subsequent args are for internal recursive use by the function
4117 itself: STARTING-DEPTH, STARTING-POINT, and INDEX.
4119 Finally, if optional SANS-OFFSPRING is non-nil then the offspring
4120 are not shifted. (Shifting a topic outwards without shifting
4121 its offspring is disallowed, since this would create a
4122 \"containment discontinuity\", where the depth difference between
4123 a topic and its immediate offspring is greater than one.)"
4125 ;; XXX the recursion here is peculiar, and in general the routine may
4126 ;; need simplification with refactoring.
4128 (if (and sans-offspring
4129 relative-depth
4130 (< relative-depth 0))
4131 (error (concat "Attempt to shift topic outwards without offspring,"
4132 " would cause containment discontinuity.")))
4134 (let* ((relative-depth (or relative-depth 0))
4135 (new-depth (allout-depth))
4136 (starting-depth (or starting-depth new-depth))
4137 (on-starting-call (null starting-point))
4138 (index (or index
4139 ;; Leave index null on starting call, so rebullet-heading
4140 ;; calculates it at what might be new depth:
4141 (and (or (zerop relative-depth)
4142 (not on-starting-call))
4143 (allout-sibling-index))))
4144 (starting-index index)
4145 (moving-outwards (< 0 relative-depth))
4146 (starting-point (or starting-point (point)))
4147 (local-point (point)))
4149 ;; Sanity check for excessive promotion done only on starting call:
4150 (and on-starting-call
4151 moving-outwards
4152 (> 0 (+ starting-depth relative-depth))
4153 (error "Attempt to shift topic out beyond level 1"))
4155 (cond ((= starting-depth new-depth)
4156 ;; We're at depth to work on this one.
4158 ;; When shifting out we work on the children before working on
4159 ;; the parent to avoid interim `allout-aberrant-container-p'
4160 ;; aberrancy, and vice-versa when shifting in:
4161 (if (>= relative-depth 0)
4162 (allout-rebullet-heading nil
4163 (+ starting-depth relative-depth)
4164 nil ;;; number
4165 index
4166 nil)) ;;; do-successors
4167 (when (not sans-offspring)
4168 ;; ... and work on subsequent ones which are at greater depth:
4169 (setq index 0)
4170 (allout-next-heading)
4171 (while (and (not (eobp))
4172 (< starting-depth (allout-depth)))
4173 (setq index (1+ index))
4174 (allout-rebullet-topic-grunt relative-depth
4175 (1+ starting-depth)
4176 starting-point
4177 index)))
4178 (when (< relative-depth 0)
4179 (save-excursion
4180 (goto-char local-point)
4181 (allout-rebullet-heading nil ;;; solicit
4182 (+ starting-depth relative-depth)
4183 nil ;;; number
4184 starting-index
4185 nil)))) ;;; do-successors
4187 ((< starting-depth new-depth)
4188 ;; Rare case -- subtopic more than one level deeper than parent.
4189 ;; Treat this one at an even deeper level:
4190 (allout-rebullet-topic-grunt relative-depth
4191 new-depth
4192 starting-point
4193 index
4194 sans-offspring)))
4196 (if on-starting-call
4197 (progn
4198 ;; Rectify numbering of former siblings of the adjusted topic,
4199 ;; if topic has changed depth
4200 (if (or do-successors
4201 (and (not (zerop relative-depth))
4202 (or (= allout-recent-depth starting-depth)
4203 (= allout-recent-depth (+ starting-depth
4204 relative-depth)))))
4205 (allout-rebullet-heading nil nil nil nil t))
4206 ;; Now rectify numbering of new siblings of the adjusted topic,
4207 ;; if depth has been changed:
4208 (progn (goto-char starting-point)
4209 (if (not (zerop relative-depth))
4210 (allout-rebullet-heading nil nil nil nil t)))))
4213 ;;;_ > allout-renumber-to-depth (&optional depth)
4214 (defun allout-renumber-to-depth (&optional depth)
4215 "Renumber siblings at current depth.
4217 Affects superior topics if optional arg DEPTH is less than current depth.
4219 Returns final depth."
4221 ;; Proceed by level, processing subsequent siblings on each,
4222 ;; ascending until we get shallower than the start depth:
4224 (let ((ascender (allout-depth))
4225 was-eobp)
4226 (while (and (not (eobp))
4227 (allout-depth)
4228 (>= allout-recent-depth depth)
4229 (>= ascender depth))
4230 ; Skip over all topics at
4231 ; lesser depths, which can not
4232 ; have been disturbed:
4233 (while (and (not (setq was-eobp (eobp)))
4234 (> allout-recent-depth ascender))
4235 (allout-next-heading))
4236 ; Prime ascender for ascension:
4237 (setq ascender (1- allout-recent-depth))
4238 (if (>= allout-recent-depth depth)
4239 (allout-rebullet-heading nil ;;; solicit
4240 nil ;;; depth
4241 nil ;;; number-control
4242 nil ;;; index
4243 t)) ;;; do-successors
4244 (if was-eobp (goto-char (point-max)))))
4245 allout-recent-depth)
4246 ;;;_ > allout-number-siblings (&optional denumber)
4247 (defun allout-number-siblings (&optional denumber)
4248 "Assign numbered topic prefix to this topic and its siblings.
4250 With universal argument, denumber -- assign default bullet to this
4251 topic and its siblings.
4253 With repeated universal argument (`^U^U'), solicit bullet for each
4254 rebulleting each topic at this level."
4256 (interactive "P")
4258 (save-excursion
4259 (allout-back-to-current-heading)
4260 (allout-beginning-of-level)
4261 (let ((depth allout-recent-depth)
4262 (index (if (not denumber) 1))
4263 (use-bullet (equal '(16) denumber))
4264 (more t))
4265 (while more
4266 (allout-rebullet-heading use-bullet ;;; solicit
4267 depth ;;; depth
4268 t ;;; number-control
4269 index ;;; index
4270 nil) ;;; do-successors
4271 (if index (setq index (1+ index)))
4272 (setq more (allout-next-sibling depth nil))))))
4273 ;;;_ > allout-shift-in (arg)
4274 (defun allout-shift-in (arg)
4275 "Increase depth of current heading and any items collapsed within it.
4277 With a negative argument, the item is shifted out using
4278 `allout-shift-out', instead.
4280 With an argument greater than one, shift-in the item but not its
4281 offspring, making the item into a sibling of its former children,
4282 and a child of sibling that formerly preceeded it.
4284 You are not allowed to shift the first offspring of a topic
4285 inwards, because that would yield a \"containment
4286 discontinuity\", where the depth difference between a topic and
4287 its immediate offspring is greater than one. The first topic in
4288 the file can be adjusted to any positive depth, however."
4290 (interactive "p")
4291 (if (< arg 0)
4292 (allout-shift-out (* arg -1))
4293 ;; refuse to create a containment discontinuity:
4294 (save-excursion
4295 (allout-back-to-current-heading)
4296 (if (not (bobp))
4297 (let* ((current-depth allout-recent-depth)
4298 (start-point (point))
4299 (predecessor-depth (progn
4300 (forward-char -1)
4301 (allout-goto-prefix-doublechecked)
4302 (if (< (point) start-point)
4303 allout-recent-depth
4304 0))))
4305 (if (and (> predecessor-depth 0)
4306 (> (1+ current-depth)
4307 (1+ predecessor-depth)))
4308 (error (concat "Disallowed shift deeper than"
4309 " containing topic's children."))
4310 (allout-back-to-current-heading)
4311 (if (< allout-recent-depth (1+ current-depth))
4312 (allout-show-children))))))
4313 (let ((where (point)))
4314 (allout-rebullet-topic 1 (and (> arg 1) 'sans-offspring))
4315 (run-hook-with-args 'allout-structure-shifted-hook arg where))))
4316 ;;;_ > allout-shift-out (arg)
4317 (defun allout-shift-out (arg)
4318 "Decrease depth of current heading and any topics collapsed within it.
4319 This will make the item a sibling of its former container.
4321 With a negative argument, the item is shifted in using
4322 `allout-shift-in', instead.
4324 With an argument greater than one, shift-out the item's offspring
4325 but not the item itself, making the former children siblings of
4326 the item.
4328 With an argument greater than 1, the item's offspring are shifted
4329 out without shifting the item. This will make the immediate
4330 subtopics into siblings of the item."
4331 (interactive "p")
4332 (if (< arg 0)
4333 (allout-shift-in (* arg -1))
4334 ;; Get proper exposure in this area:
4335 (save-excursion (if (allout-ascend)
4336 (allout-show-children)))
4337 ;; Show collapsed children if there's a successor which will become
4338 ;; their sibling:
4339 (if (and (allout-current-topic-collapsed-p)
4340 (save-excursion (allout-next-sibling)))
4341 (allout-show-children))
4342 (let ((where (and (allout-depth) allout-recent-prefix-beginning)))
4343 (save-excursion
4344 (if (> arg 1)
4345 ;; Shift the offspring but not the topic:
4346 (let ((children-chart (allout-chart-subtree 1)))
4347 (if (listp (car children-chart))
4348 ;; whoops:
4349 (setq children-chart (allout-flatten children-chart)))
4350 (save-excursion
4351 (dolist (child-point children-chart)
4352 (goto-char child-point)
4353 (allout-shift-out 1))))
4354 (allout-rebullet-topic (* arg -1))))
4355 (run-hook-with-args 'allout-structure-shifted-hook (* arg -1) where))))
4356 ;;;_ : Surgery (kill-ring) functions with special provisions for outlines:
4357 ;;;_ > allout-kill-line (&optional arg)
4358 (defun allout-kill-line (&optional arg)
4359 "Kill line, adjusting subsequent lines suitably for outline mode."
4361 (interactive "*P")
4363 (if (or (not (allout-mode-p))
4364 (not (bolp))
4365 (not (save-match-data (looking-at allout-regexp))))
4366 ;; Just do a regular kill:
4367 (kill-line arg)
4368 ;; Ah, have to watch out for adjustments:
4369 (let* ((beg (point))
4371 (beg-hidden (allout-hidden-p))
4372 (end-hidden (save-excursion (allout-end-of-current-line)
4373 (setq end (point))
4374 (allout-hidden-p)))
4375 (depth (allout-depth)))
4377 (allout-annotate-hidden beg end)
4378 (if (and (not beg-hidden) (not end-hidden))
4379 (allout-unprotected (kill-line arg))
4380 (kill-line arg))
4381 (allout-deannotate-hidden beg end)
4383 (if allout-numbered-bullet
4384 (save-excursion ; Renumber subsequent topics if needed:
4385 (if (not (save-match-data (looking-at allout-regexp)))
4386 (allout-next-heading))
4387 (allout-renumber-to-depth depth)))
4388 (run-hook-with-args 'allout-structure-deleted-hook depth (point)))))
4389 ;;;_ > allout-copy-line-as-kill ()
4390 (defun allout-copy-line-as-kill ()
4391 "Like allout-kill-topic, but save to kill ring instead of deleting."
4392 (interactive)
4393 (let ((buffer-read-only t))
4394 (condition-case nil
4395 (allout-kill-line)
4396 (buffer-read-only nil))))
4397 ;;;_ > allout-kill-topic ()
4398 (defun allout-kill-topic ()
4399 "Kill topic together with subtopics.
4401 Trailing whitespace is killed with a topic if that whitespace:
4403 - would separate the topic from a subsequent sibling
4404 - would separate the topic from the end of buffer
4405 - would not be added to whitespace already separating the topic from the
4406 previous one.
4408 Topic exposure is marked with text-properties, to be used by
4409 `allout-yank-processing' for exposure recovery."
4411 (interactive)
4412 (let* ((inhibit-field-text-motion t)
4413 (beg (prog1 (allout-back-to-current-heading) (beginning-of-line)))
4415 (depth allout-recent-depth))
4416 (allout-end-of-current-subtree)
4417 (if (and (/= (current-column) 0) (not (eobp)))
4418 (forward-char 1))
4419 (if (not (eobp))
4420 (if (and (save-match-data (looking-at "\n"))
4421 (or (save-excursion
4422 (or (not (allout-next-heading))
4423 (= depth allout-recent-depth)))
4424 (and (> (- beg (point-min)) 3)
4425 (string= (buffer-substring (- beg 2) beg) "\n\n"))))
4426 (forward-char 1)))
4428 (allout-annotate-hidden beg (setq end (point)))
4429 (unwind-protect
4430 (allout-unprotected (kill-region beg end))
4431 (if buffer-read-only
4432 ;; eg, during copy-as-kill.
4433 (allout-deannotate-hidden beg end)))
4435 (save-excursion
4436 (allout-renumber-to-depth depth))
4437 (run-hook-with-args 'allout-structure-deleted-hook depth (point))))
4438 ;;;_ > allout-copy-topic-as-kill ()
4439 (defun allout-copy-topic-as-kill ()
4440 "Like `allout-kill-topic', but save to kill ring instead of deleting."
4441 (interactive)
4442 (let ((buffer-read-only t))
4443 (condition-case nil
4444 (allout-kill-topic)
4445 (buffer-read-only (message "Topic copied...")))))
4446 ;;;_ > allout-annotate-hidden (begin end)
4447 (defun allout-annotate-hidden (begin end)
4448 "Qualify text with properties to indicate exposure status."
4450 (let ((was-modified (buffer-modified-p))
4451 (buffer-read-only nil))
4452 (allout-deannotate-hidden begin end)
4453 (save-excursion
4454 (goto-char begin)
4455 (let (done next prev overlay)
4456 (while (not done)
4457 ;; at or advance to start of next hidden region:
4458 (if (not (allout-hidden-p))
4459 (setq next
4460 (max (1+ (point))
4461 (next-single-char-property-change (point)
4462 'invisible
4463 nil end))))
4464 (if (or (not next) (eq prev next))
4465 ;; still not at start of hidden area -- must not be any left.
4466 (setq done t)
4467 (goto-char next)
4468 (setq prev next)
4469 (if (not (allout-hidden-p))
4470 ;; still not at start of hidden area.
4471 (setq done t)
4472 (setq overlay (allout-get-invisibility-overlay))
4473 (setq next (overlay-end overlay)
4474 prev next)
4475 ;; advance to end of this hidden area:
4476 (when next
4477 (goto-char next)
4478 (allout-unprotected
4479 (let ((buffer-undo-list t))
4480 (put-text-property (overlay-start overlay) next
4481 'allout-was-hidden t)))))))))
4482 (set-buffer-modified-p was-modified)))
4483 ;;;_ > allout-deannotate-hidden (begin end)
4484 (defun allout-deannotate-hidden (begin end)
4485 "Remove allout hidden-text annotation between BEGIN and END."
4487 (allout-unprotected
4488 (let ((inhibit-read-only t)
4489 (buffer-undo-list t))
4490 ;(remove-text-properties begin end '(allout-was-hidden t))
4492 ;;;_ > allout-hide-by-annotation (begin end)
4493 (defun allout-hide-by-annotation (begin end)
4494 "Translate text properties indicating exposure status into actual exposure."
4495 (save-excursion
4496 (goto-char begin)
4497 (let ((was-modified (buffer-modified-p))
4498 done next prev)
4499 (while (not done)
4500 ;; at or advance to start of next annotation:
4501 (if (not (get-text-property (point) 'allout-was-hidden))
4502 (setq next (next-single-char-property-change (point)
4503 'allout-was-hidden
4504 nil end)))
4505 (if (or (not next) (eq prev next))
4506 ;; no more or not advancing -- must not be any left.
4507 (setq done t)
4508 (goto-char next)
4509 (setq prev next)
4510 (if (not (get-text-property (point) 'allout-was-hidden))
4511 ;; still not at start of annotation.
4512 (setq done t)
4513 ;; advance to just after end of this annotation:
4514 (setq next (next-single-char-property-change (point)
4515 'allout-was-hidden
4516 nil end))
4517 (overlay-put (make-overlay prev next nil 'front-advance)
4518 'category 'allout-exposure-category)
4519 (allout-deannotate-hidden prev next)
4520 (setq prev next)
4521 (if next (goto-char next)))))
4522 (set-buffer-modified-p was-modified))))
4523 ;;;_ > allout-yank-processing ()
4524 (defun allout-yank-processing (&optional arg)
4526 "Incidental allout-specific business to be done just after text yanks.
4528 Does depth adjustment of yanked topics, when:
4530 1 the stuff being yanked starts with a valid outline header prefix, and
4531 2 it is being yanked at the end of a line which consists of only a valid
4532 topic prefix.
4534 Also, adjusts numbering of subsequent siblings when appropriate.
4536 Depth adjustment alters the depth of all the topics being yanked
4537 the amount it takes to make the first topic have the depth of the
4538 header into which it's being yanked.
4540 The point is left in front of yanked, adjusted topics, rather than
4541 at the end (and vice-versa with the mark). Non-adjusted yanks,
4542 however, are left exactly like normal, non-allout-specific yanks."
4544 (interactive "*P")
4545 ; Get to beginning, leaving
4546 ; region around subject:
4547 (if (< (allout-mark-marker t) (point))
4548 (exchange-point-and-mark))
4549 (save-match-data
4550 (let* ((subj-beg (point))
4551 (into-bol (bolp))
4552 (subj-end (allout-mark-marker t))
4553 ;; 'resituate' if yanking an entire topic into topic header:
4554 (resituate (and (let ((allout-inhibit-aberrance-doublecheck t))
4555 (allout-e-o-prefix-p))
4556 (looking-at allout-regexp)
4557 (allout-prefix-data)))
4558 ;; `rectify-numbering' if resituating (where several topics may
4559 ;; be resituating) or yanking a topic into a topic slot (bol):
4560 (rectify-numbering (or resituate
4561 (and into-bol (looking-at allout-regexp)))))
4562 (if resituate
4563 ;; Yanking a topic into the start of a topic -- reconcile to fit:
4564 (let* ((inhibit-field-text-motion t)
4565 (prefix-len (if (not (match-end 1))
4567 (- (match-end 1) subj-beg)))
4568 (subj-depth allout-recent-depth)
4569 (prefix-bullet (allout-recent-bullet))
4570 (adjust-to-depth
4571 ;; Nil if adjustment unnecessary, otherwise depth to which
4572 ;; adjustment should be made:
4573 (save-excursion
4574 (and (goto-char subj-end)
4575 (eolp)
4576 (goto-char subj-beg)
4577 (and (looking-at allout-regexp)
4578 (progn
4579 (beginning-of-line)
4580 (not (= (point) subj-beg)))
4581 (looking-at allout-regexp)
4582 (allout-prefix-data))
4583 allout-recent-depth)))
4584 (more t))
4585 (setq rectify-numbering allout-numbered-bullet)
4586 (if adjust-to-depth
4587 ; Do the adjustment:
4588 (progn
4589 (save-restriction
4590 (narrow-to-region subj-beg subj-end)
4591 ; Trim off excessive blank
4592 ; line at end, if any:
4593 (goto-char (point-max))
4594 (if (looking-at "^$")
4595 (allout-unprotected (delete-char -1)))
4596 ; Work backwards, with each
4597 ; shallowest level,
4598 ; successively excluding the
4599 ; last processed topic from
4600 ; the narrow region:
4601 (while more
4602 (allout-back-to-current-heading)
4603 ; go as high as we can in each bunch:
4604 (while (allout-ascend t))
4605 (save-excursion
4606 (allout-unprotected
4607 (allout-rebullet-topic-grunt (- adjust-to-depth
4608 subj-depth)))
4609 (allout-depth))
4610 (if (setq more (not (bobp)))
4611 (progn (widen)
4612 (forward-char -1)
4613 (narrow-to-region subj-beg (point))))))
4614 ;; Preserve new bullet if it's a distinctive one, otherwise
4615 ;; use old one:
4616 (if (string-match (regexp-quote prefix-bullet)
4617 allout-distinctive-bullets-string)
4618 ; Delete from bullet of old to
4619 ; before bullet of new:
4620 (progn
4621 (beginning-of-line)
4622 (allout-unprotected
4623 (delete-region (point) subj-beg))
4624 (set-marker (allout-mark-marker t) subj-end)
4625 (goto-char subj-beg)
4626 (allout-end-of-prefix))
4627 ; Delete base subj prefix,
4628 ; leaving old one:
4629 (allout-unprotected
4630 (progn
4631 (delete-region (point) (+ (point)
4632 prefix-len
4633 (- adjust-to-depth
4634 subj-depth)))
4635 ; and delete residual subj
4636 ; prefix digits and space:
4637 (while (looking-at "[0-9]") (delete-char 1))
4638 (if (looking-at " ")
4639 (delete-char 1))))))
4640 (exchange-point-and-mark))))
4641 (if rectify-numbering
4642 (progn
4643 (save-excursion
4644 ; Give some preliminary feedback:
4645 (message "... reconciling numbers")
4646 ; ... and renumber, in case necessary:
4647 (goto-char subj-beg)
4648 (if (allout-goto-prefix-doublechecked)
4649 (allout-unprotected
4650 (allout-rebullet-heading nil ;;; solicit
4651 (allout-depth) ;;; depth
4652 nil ;;; number-control
4653 nil ;;; index
4654 t)))
4655 (message ""))))
4656 (if (or into-bol resituate)
4657 (allout-hide-by-annotation (point) (allout-mark-marker t))
4658 (allout-deannotate-hidden (allout-mark-marker t) (point)))
4659 (if (not resituate)
4660 (exchange-point-and-mark))
4661 (run-hook-with-args 'allout-structure-added-hook subj-beg subj-end))))
4662 ;;;_ > allout-yank (&optional arg)
4663 (defun allout-yank (&optional arg)
4664 "`allout-mode' yank, with depth and numbering adjustment of yanked topics.
4666 Non-topic yanks work no differently than normal yanks.
4668 If a topic is being yanked into a bare topic prefix, the depth of the
4669 yanked topic is adjusted to the depth of the topic prefix.
4671 1 we're yanking in an `allout-mode' buffer
4672 2 the stuff being yanked starts with a valid outline header prefix, and
4673 3 it is being yanked at the end of a line which consists of only a valid
4674 topic prefix.
4676 If these conditions hold then the depth of the yanked topics are all
4677 adjusted the amount it takes to make the first one at the depth of the
4678 header into which it's being yanked.
4680 The point is left in front of yanked, adjusted topics, rather than
4681 at the end (and vice-versa with the mark). Non-adjusted yanks,
4682 however, (ones that don't qualify for adjustment) are handled
4683 exactly like normal yanks.
4685 Numbering of yanked topics, and the successive siblings at the depth
4686 into which they're being yanked, is adjusted.
4688 `allout-yank-pop' works with `allout-yank' just like normal `yank-pop'
4689 works with normal `yank' in non-outline buffers."
4691 (interactive "*P")
4692 (setq this-command 'yank)
4693 (allout-unprotected
4694 (yank arg))
4695 (if (allout-mode-p)
4696 (allout-yank-processing)))
4697 ;;;_ > allout-yank-pop (&optional arg)
4698 (defun allout-yank-pop (&optional arg)
4699 "Yank-pop like `allout-yank' when popping to bare outline prefixes.
4701 Adapts level of popped topics to level of fresh prefix.
4703 Note -- prefix changes to distinctive bullets will stick, if followed
4704 by pops to non-distinctive yanks. Bug..."
4706 (interactive "*p")
4707 (setq this-command 'yank)
4708 (yank-pop arg)
4709 (if (allout-mode-p)
4710 (allout-yank-processing)))
4712 ;;;_ - Specialty bullet functions
4713 ;;;_ : File Cross references
4714 ;;;_ > allout-resolve-xref ()
4715 (defun allout-resolve-xref ()
4716 "Pop to file associated with current heading, if it has an xref bullet.
4718 \(Works according to setting of `allout-file-xref-bullet')."
4719 (interactive)
4720 (if (not allout-file-xref-bullet)
4721 (error
4722 "Outline cross references disabled -- no `allout-file-xref-bullet'")
4723 (if (not (string= (allout-current-bullet) allout-file-xref-bullet))
4724 (error "Current heading lacks cross-reference bullet `%s'"
4725 allout-file-xref-bullet)
4726 (let ((inhibit-field-text-motion t)
4727 file-name)
4728 (save-match-data
4729 (save-excursion
4730 (let* ((text-start allout-recent-prefix-end)
4731 (heading-end (progn (end-of-line) (point))))
4732 (goto-char text-start)
4733 (setq file-name
4734 (if (re-search-forward "\\s-\\(\\S-*\\)" heading-end t)
4735 (buffer-substring (match-beginning 1)
4736 (match-end 1)))))))
4737 (setq file-name (expand-file-name file-name))
4738 (if (or (file-exists-p file-name)
4739 (if (file-writable-p file-name)
4740 (y-or-n-p (format "%s not there, create one? "
4741 file-name))
4742 (error "%s not found and can't be created" file-name)))
4743 (condition-case failure
4744 (find-file-other-window file-name)
4745 (error failure))
4746 (error "%s not found" file-name))
4752 ;;;_ #6 Exposure Control
4754 ;;;_ - Fundamental
4755 ;;;_ > allout-flag-region (from to flag)
4756 (defun allout-flag-region (from to flag)
4757 "Conceal text between FROM and TO if FLAG is non-nil, else reveal it.
4759 Exposure-change hook `allout-exposure-change-hook' is run with the same
4760 arguments as this function, after the exposure changes are made. (The old
4761 `allout-view-change-hook' is being deprecated, and eventually will not be
4762 invoked.)"
4764 ;; We use outline invisibility spec.
4765 (remove-overlays from to 'category 'allout-exposure-category)
4766 (when flag
4767 (let ((o (make-overlay from to nil 'front-advance)))
4768 (overlay-put o 'category 'allout-exposure-category)
4769 (when (featurep 'xemacs)
4770 (let ((props (symbol-plist 'allout-exposure-category)))
4771 (while props
4772 (overlay-put o (pop props) (pop props)))))))
4773 (run-hooks 'allout-view-change-hook)
4774 (run-hook-with-args 'allout-exposure-change-hook from to flag))
4775 ;;;_ > allout-flag-current-subtree (flag)
4776 (defun allout-flag-current-subtree (flag)
4777 "Conceal currently-visible topic's subtree if FLAG non-nil, else reveal it."
4779 (save-excursion
4780 (allout-back-to-current-heading)
4781 (let ((inhibit-field-text-motion t))
4782 (end-of-line))
4783 (allout-flag-region (point)
4784 ;; Exposing must not leave trailing blanks hidden,
4785 ;; but can leave them exposed when hiding, so we
4786 ;; can use flag's inverse as the
4787 ;; include-trailing-blank cue:
4788 (allout-end-of-current-subtree (not flag))
4789 flag)))
4791 ;;;_ - Topic-specific
4792 ;;;_ > allout-show-entry ()
4793 (defun allout-show-entry ()
4794 "Like `allout-show-current-entry', but reveals entries in hidden topics.
4796 This is a way to give restricted peek at a concealed locality without the
4797 expense of exposing its context, but can leave the outline with aberrant
4798 exposure. `allout-show-offshoot' should be used after the peek to rectify
4799 the exposure."
4801 (interactive)
4802 (save-excursion
4803 (let (beg end)
4804 (allout-goto-prefix-doublechecked)
4805 (setq beg (if (allout-hidden-p) (1- (point)) (point)))
4806 (setq end (allout-pre-next-prefix))
4807 (allout-flag-region beg end nil)
4808 (list beg end))))
4809 ;;;_ > allout-show-children (&optional level strict)
4810 (defun allout-show-children (&optional level strict)
4812 "If point is visible, show all direct subheadings of this heading.
4814 Otherwise, do `allout-show-to-offshoot', and then show subheadings.
4816 Optional LEVEL specifies how many levels below the current level
4817 should be shown, or all levels if t. Default is 1.
4819 Optional STRICT means don't resort to -show-to-offshoot, no matter
4820 what. This is basically so -show-to-offshoot, which is called by
4821 this function, can employ the pure offspring-revealing capabilities of
4824 Returns point at end of subtree that was opened, if any. (May get a
4825 point of non-opened subtree?)"
4827 (interactive "p")
4828 (let ((start-point (point)))
4829 (if (and (not strict)
4830 (allout-hidden-p))
4832 (progn (allout-show-to-offshoot) ; Point's concealed, open to
4833 ; expose it.
4834 ;; Then recurse, but with "strict" set so we don't
4835 ;; infinite regress:
4836 (allout-show-children level t))
4838 (save-excursion
4839 (allout-beginning-of-current-line)
4840 (save-restriction
4841 (let* (depth
4842 ;; translate the level spec for this routine to the ones
4843 ;; used by -chart-subtree and -chart-to-reveal:
4844 (chart-level (cond ((not level) 1)
4845 ((eq level t) nil)
4846 (t level)))
4847 (chart (allout-chart-subtree chart-level))
4848 (to-reveal (or (allout-chart-to-reveal chart chart-level)
4849 ;; interactive, show discontinuous children:
4850 (and chart
4851 (interactive-p)
4852 (save-excursion
4853 (allout-back-to-current-heading)
4854 (setq depth (allout-current-depth))
4855 (and (allout-next-heading)
4856 (> allout-recent-depth
4857 (1+ depth))))
4858 (message
4859 "Discontinuous offspring; use `%s %s'%s."
4860 (substitute-command-keys
4861 "\\[universal-argument]")
4862 (substitute-command-keys
4863 "\\[allout-shift-out]")
4864 " to elevate them.")
4865 (allout-chart-to-reveal
4866 chart (- allout-recent-depth depth))))))
4867 (goto-char start-point)
4868 (when (and strict (allout-hidden-p))
4869 ;; Concealed root would already have been taken care of,
4870 ;; unless strict was set.
4871 (allout-flag-region (point) (allout-snug-back) nil)
4872 (when allout-show-bodies
4873 (goto-char (car to-reveal))
4874 (allout-show-current-entry)))
4875 (while to-reveal
4876 (goto-char (car to-reveal))
4877 (allout-flag-region (save-excursion (allout-snug-back) (point))
4878 (progn (search-forward "\n" nil t)
4879 (1- (point)))
4880 nil)
4881 (when allout-show-bodies
4882 (goto-char (car to-reveal))
4883 (allout-show-current-entry))
4884 (setq to-reveal (cdr to-reveal)))))))
4885 ;; Compensate for `save-excursion's maintenance of point
4886 ;; within invisible text:
4887 (goto-char start-point)))
4888 ;;;_ > allout-show-to-offshoot ()
4889 (defun allout-show-to-offshoot ()
4890 "Like `allout-show-entry', but reveals all concealed ancestors, as well.
4892 Useful for coherently exposing to a random point in a hidden region."
4893 (interactive)
4894 (save-excursion
4895 (let ((inhibit-field-text-motion t)
4896 (orig-pt (point))
4897 (orig-pref (allout-goto-prefix-doublechecked))
4898 (last-at (point))
4899 (bag-it 0))
4900 (while (or (> bag-it 1) (allout-hidden-p))
4901 (while (allout-hidden-p)
4902 (move-beginning-of-line 1)
4903 (if (allout-hidden-p) (forward-char -1)))
4904 (if (= last-at (setq last-at (point)))
4905 ;; Oops, we're not making any progress! Show the current topic
4906 ;; completely, and try one more time here, if we haven't already.
4907 (progn (beginning-of-line)
4908 (allout-show-current-subtree)
4909 (goto-char orig-pt)
4910 (setq bag-it (1+ bag-it))
4911 (if (> bag-it 1)
4912 (error "allout-show-to-offshoot: %s"
4913 "Stumped by aberrant nesting.")))
4914 (if (> bag-it 0) (setq bag-it 0))
4915 (allout-show-children)
4916 (goto-char orig-pref)))
4917 (goto-char orig-pt)))
4918 (if (allout-hidden-p)
4919 (allout-show-entry)))
4920 ;;;_ > allout-hide-current-entry ()
4921 (defun allout-hide-current-entry ()
4922 "Hide the body directly following this heading."
4923 (interactive)
4924 (allout-back-to-current-heading)
4925 (save-excursion
4926 (let ((inhibit-field-text-motion t))
4927 (end-of-line))
4928 (allout-flag-region (point)
4929 (progn (allout-end-of-entry) (point))
4930 t)))
4931 ;;;_ > allout-show-current-entry (&optional arg)
4932 (defun allout-show-current-entry (&optional arg)
4933 "Show body following current heading, or hide entry with universal argument."
4935 (interactive "P")
4936 (if arg
4937 (allout-hide-current-entry)
4938 (save-excursion (allout-show-to-offshoot))
4939 (save-excursion
4940 (allout-flag-region (point)
4941 (progn (allout-end-of-entry t) (point))
4942 nil)
4944 ;;;_ > allout-show-current-subtree (&optional arg)
4945 (defun allout-show-current-subtree (&optional arg)
4946 "Show everything within the current topic.
4947 With a repeat-count, expose this topic and its siblings."
4948 (interactive "P")
4949 (save-excursion
4950 (if (<= (allout-current-depth) 0)
4951 ;; Outside any topics -- try to get to the first:
4952 (if (not (allout-next-heading))
4953 (error "No topics")
4954 ;; got to first, outermost topic -- set to expose it and siblings:
4955 (message "Above outermost topic -- exposing all.")
4956 (allout-flag-region (point-min)(point-max) nil))
4957 (allout-beginning-of-current-line)
4958 (if (not arg)
4959 (allout-flag-current-subtree nil)
4960 (allout-beginning-of-level)
4961 (allout-expose-topic '(* :))))))
4962 ;;;_ > allout-current-topic-collapsed-p (&optional include-single-liners)
4963 (defun allout-current-topic-collapsed-p (&optional include-single-liners)
4964 "True if the currently visible containing topic is already collapsed.
4966 Single line topics intrinsically can be considered as being both
4967 collapsed and uncollapsed. If optional INCLUDE-SINGLE-LINERS is
4968 true, then single-line topics are considered to be collapsed. By
4969 default, they are treated as being uncollapsed."
4970 (save-match-data
4971 (save-excursion
4972 (and
4973 ;; Is the topic all on one line (allowing for trailing blank line)?
4974 (>= (progn (allout-back-to-current-heading)
4975 (move-end-of-line 1)
4976 (point))
4977 (allout-end-of-current-subtree (not (looking-at "\n\n"))))
4979 (or include-single-liners
4980 (progn (backward-char 1) (allout-hidden-p)))))))
4981 ;;;_ > allout-hide-current-subtree (&optional just-close)
4982 (defun allout-hide-current-subtree (&optional just-close)
4983 "Close the current topic, or containing topic if this one is already closed.
4985 If this topic is closed and it's a top level topic, close this topic
4986 and its siblings.
4988 If optional arg JUST-CLOSE is non-nil, do not close the parent or
4989 siblings, even if the target topic is already closed."
4991 (interactive)
4992 (let* ((from (point))
4993 (sibs-msg "Top-level topic already closed -- closing siblings...")
4994 (current-exposed (not (allout-current-topic-collapsed-p t))))
4995 (cond (current-exposed (allout-flag-current-subtree t))
4996 (just-close nil)
4997 ((allout-ascend) (allout-hide-current-subtree))
4998 (t (goto-char 0)
4999 (message sibs-msg)
5000 (allout-goto-prefix-doublechecked)
5001 (allout-expose-topic '(0 :))
5002 (message (concat sibs-msg " Done."))))
5003 (goto-char from)))
5004 ;;;_ > allout-toggle-current-subtree-exposure
5005 (defun allout-toggle-current-subtree-exposure ()
5006 "Show or hide the current subtree depending on its current state."
5007 ;; thanks to tassilo for suggesting this.
5008 (interactive)
5009 (save-excursion
5010 (allout-back-to-heading)
5011 (if (allout-hidden-p (point-at-eol))
5012 (allout-show-current-subtree)
5013 (allout-hide-current-subtree))))
5014 ;;;_ > allout-show-current-branches ()
5015 (defun allout-show-current-branches ()
5016 "Show all subheadings of this heading, but not their bodies."
5017 (interactive)
5018 (let ((inhibit-field-text-motion t))
5019 (beginning-of-line))
5020 (allout-show-children t))
5021 ;;;_ > allout-hide-current-leaves ()
5022 (defun allout-hide-current-leaves ()
5023 "Hide the bodies of the current topic and all its offspring."
5024 (interactive)
5025 (allout-back-to-current-heading)
5026 (allout-hide-region-body (point) (progn (allout-end-of-current-subtree)
5027 (point))))
5029 ;;;_ - Region and beyond
5030 ;;;_ > allout-show-all ()
5031 (defun allout-show-all ()
5032 "Show all of the text in the buffer."
5033 (interactive)
5034 (message "Exposing entire buffer...")
5035 (allout-flag-region (point-min) (point-max) nil)
5036 (message "Exposing entire buffer... Done."))
5037 ;;;_ > allout-hide-bodies ()
5038 (defun allout-hide-bodies ()
5039 "Hide all of buffer except headings."
5040 (interactive)
5041 (allout-hide-region-body (point-min) (point-max)))
5042 ;;;_ > allout-hide-region-body (start end)
5043 (defun allout-hide-region-body (start end)
5044 "Hide all body lines in the region, but not headings."
5045 (save-match-data
5046 (save-excursion
5047 (save-restriction
5048 (narrow-to-region start end)
5049 (goto-char (point-min))
5050 (let ((inhibit-field-text-motion t))
5051 (while (not (eobp))
5052 (end-of-line)
5053 (allout-flag-region (point) (allout-end-of-entry) t)
5054 (if (not (eobp))
5055 (forward-char
5056 (if (looking-at "\n\n")
5057 2 1)))))))))
5059 ;;;_ > allout-expose-topic (spec)
5060 (defun allout-expose-topic (spec)
5061 "Apply exposure specs to successive outline topic items.
5063 Use the more convenient frontend, `allout-new-exposure', if you don't
5064 need evaluation of the arguments, or even better, the `allout-layout'
5065 variable-keyed mode-activation/auto-exposure feature of allout outline
5066 mode. See the respective documentation strings for more details.
5068 Cursor is left at start position.
5070 SPEC is either a number or a list.
5072 Successive specs on a list are applied to successive sibling topics.
5074 A simple spec (either a number, one of a few symbols, or the null
5075 list) dictates the exposure for the corresponding topic.
5077 Non-null lists recursively designate exposure specs for respective
5078 subtopics of the current topic.
5080 The `:' repeat spec is used to specify exposure for any number of
5081 successive siblings, up to the trailing ones for which there are
5082 explicit specs following the `:'.
5084 Simple (numeric and null-list) specs are interpreted as follows:
5086 Numbers indicate the relative depth to open the corresponding topic.
5087 - negative numbers force the topic to be closed before opening to the
5088 absolute value of the number, so all siblings are open only to
5089 that level.
5090 - positive numbers open to the relative depth indicated by the
5091 number, but do not force already opened subtopics to be closed.
5092 - 0 means to close topic -- hide all offspring.
5093 : - `repeat'
5094 apply prior element to all siblings at current level, *up to*
5095 those siblings that would be covered by specs following the `:'
5096 on the list. Ie, apply to all topics at level but the last
5097 ones. (Only first of multiple colons at same level is
5098 respected -- subsequent ones are discarded.)
5099 * - completely opens the topic, including bodies.
5100 + - shows all the sub headers, but not the bodies
5101 - - exposes the body of the corresponding topic.
5103 Examples:
5104 \(allout-expose-topic '(-1 : 0))
5105 Close this and all following topics at current level, exposing
5106 only their immediate children, but close down the last topic
5107 at this current level completely.
5108 \(allout-expose-topic '(-1 () : 1 0))
5109 Close current topic so only the immediate subtopics are shown;
5110 show the children in the second to last topic, and completely
5111 close the last one.
5112 \(allout-expose-topic '(-2 : -1 *))
5113 Expose children and grandchildren of all topics at current
5114 level except the last two; expose children of the second to
5115 last and completely open the last one."
5117 (interactive "xExposure spec: ")
5118 (if (not (listp spec))
5120 (let ((depth (allout-depth))
5121 (max-pos 0)
5122 prev-elem curr-elem
5123 stay)
5124 (while spec
5125 (setq prev-elem curr-elem
5126 curr-elem (car spec)
5127 spec (cdr spec))
5128 (cond ; Do current element:
5129 ((null curr-elem) nil)
5130 ((symbolp curr-elem)
5131 (cond ((eq curr-elem '*) (allout-show-current-subtree)
5132 (if (> allout-recent-end-of-subtree max-pos)
5133 (setq max-pos allout-recent-end-of-subtree)))
5134 ((eq curr-elem '+)
5135 (if (not (allout-hidden-p))
5136 (save-excursion (allout-hide-current-subtree t)))
5137 (allout-show-current-branches)
5138 (if (> allout-recent-end-of-subtree max-pos)
5139 (setq max-pos allout-recent-end-of-subtree)))
5140 ((eq curr-elem '-) (allout-show-current-entry))
5141 ((eq curr-elem ':)
5142 (setq stay t)
5143 ;; Expand the `repeat' spec to an explicit version,
5144 ;; w.r.t. remaining siblings:
5145 (let ((residue ; = # of sibs not covered by remaining spec
5146 ;; Dang, could be nice to make use of the chart, sigh:
5147 (- (length (allout-chart-siblings))
5148 (length spec))))
5149 (if (< 0 residue)
5150 ;; Some residue -- cover it with prev-elem:
5151 (setq spec (append (make-list residue prev-elem)
5152 spec)))))))
5153 ((numberp curr-elem)
5154 (if (and (>= 0 curr-elem) (not (allout-hidden-p)))
5155 (save-excursion (allout-hide-current-subtree t)
5156 (if (> 0 curr-elem)
5158 (if (> allout-recent-end-of-subtree max-pos)
5159 (setq max-pos
5160 allout-recent-end-of-subtree)))))
5161 (if (> (abs curr-elem) 0)
5162 (progn (allout-show-children (abs curr-elem))
5163 (if (> allout-recent-end-of-subtree max-pos)
5164 (setq max-pos allout-recent-end-of-subtree)))))
5165 ((listp curr-elem)
5166 (if (allout-descend-to-depth (1+ depth))
5167 (let ((got (allout-expose-topic curr-elem)))
5168 (if (and got (> got max-pos)) (setq max-pos got))))))
5169 (cond (stay (setq stay nil))
5170 ((listp (car spec)) nil)
5171 ((> max-pos (point))
5172 ;; Capitalize on max-pos state to get us nearer next sibling:
5173 (progn (goto-char (min (point-max) max-pos))
5174 (allout-next-heading)))
5175 ((allout-next-sibling depth))))
5176 max-pos)))
5177 ;;;_ > allout-old-expose-topic (spec &rest followers)
5178 (defun allout-old-expose-topic (spec &rest followers)
5180 "Deprecated. Use `allout-expose-topic' (with different schema
5181 format) instead.
5183 Dictate wholesale exposure scheme for current topic, according to SPEC.
5185 SPEC is either a number or a list. Optional successive args
5186 dictate exposure for subsequent siblings of current topic.
5188 A simple spec (either a number, a special symbol, or the null list)
5189 dictates the overall exposure for a topic. Non null lists are
5190 composite specs whose first element dictates the overall exposure for
5191 a topic, with the subsequent elements in the list interpreted as specs
5192 that dictate the exposure for the successive offspring of the topic.
5194 Simple (numeric and null-list) specs are interpreted as follows:
5196 - Numbers indicate the relative depth to open the corresponding topic:
5197 - negative numbers force the topic to be close before opening to the
5198 absolute value of the number.
5199 - positive numbers just open to the relative depth indicated by the number.
5200 - 0 just closes
5201 - `*' completely opens the topic, including bodies.
5202 - `+' shows all the sub headers, but not the bodies
5203 - `-' exposes the body and immediate offspring of the corresponding topic.
5205 If the spec is a list, the first element must be a number, which
5206 dictates the exposure depth of the topic as a whole. Subsequent
5207 elements of the list are nested SPECs, dictating the specific exposure
5208 for the corresponding offspring of the topic.
5210 Optional FOLLOWERS arguments dictate exposure for succeeding siblings."
5212 (interactive "xExposure spec: ")
5213 (let ((inhibit-field-text-motion t)
5214 (depth (allout-current-depth))
5215 max-pos)
5216 (cond ((null spec) nil)
5217 ((symbolp spec)
5218 (if (eq spec '*) (allout-show-current-subtree))
5219 (if (eq spec '+) (allout-show-current-branches))
5220 (if (eq spec '-) (allout-show-current-entry)))
5221 ((numberp spec)
5222 (if (>= 0 spec)
5223 (save-excursion (allout-hide-current-subtree t)
5224 (end-of-line)
5225 (if (or (not max-pos)
5226 (> (point) max-pos))
5227 (setq max-pos (point)))
5228 (if (> 0 spec)
5229 (setq spec (* -1 spec)))))
5230 (if (> spec 0)
5231 (allout-show-children spec)))
5232 ((listp spec)
5233 ;(let ((got (allout-old-expose-topic (car spec))))
5234 ; (if (and got (or (not max-pos) (> got max-pos)))
5235 ; (setq max-pos got)))
5236 (let ((new-depth (+ (allout-current-depth) 1))
5237 got)
5238 (setq max-pos (allout-old-expose-topic (car spec)))
5239 (setq spec (cdr spec))
5240 (if (and spec
5241 (allout-descend-to-depth new-depth)
5242 (not (allout-hidden-p)))
5243 (progn (setq got (apply 'allout-old-expose-topic spec))
5244 (if (and got (or (not max-pos) (> got max-pos)))
5245 (setq max-pos got)))))))
5246 (while (and followers
5247 (progn (if (and max-pos (< (point) max-pos))
5248 (progn (goto-char max-pos)
5249 (setq max-pos nil)))
5250 (end-of-line)
5251 (allout-next-sibling depth)))
5252 (allout-old-expose-topic (car followers))
5253 (setq followers (cdr followers)))
5254 max-pos))
5255 ;;;_ > allout-new-exposure '()
5256 (defmacro allout-new-exposure (&rest spec)
5257 "Literal frontend for `allout-expose-topic', doesn't evaluate arguments.
5258 Some arguments that would need to be quoted in `allout-expose-topic'
5259 need not be quoted in `allout-new-exposure'.
5261 Cursor is left at start position.
5263 Use this instead of obsolete `allout-exposure'.
5265 Examples:
5266 \(allout-new-exposure (-1 () () () 1) 0)
5267 Close current topic at current level so only the immediate
5268 subtopics are shown, except also show the children of the
5269 third subtopic; and close the next topic at the current level.
5270 \(allout-new-exposure : -1 0)
5271 Close all topics at current level to expose only their
5272 immediate children, except for the last topic at the current
5273 level, in which even its immediate children are hidden.
5274 \(allout-new-exposure -2 : -1 *)
5275 Expose children and grandchildren of first topic at current
5276 level, and expose children of subsequent topics at current
5277 level *except* for the last, which should be opened completely."
5278 (list 'save-excursion
5279 '(if (not (or (allout-goto-prefix-doublechecked)
5280 (allout-next-heading)))
5281 (error "allout-new-exposure: Can't find any outline topics"))
5282 (list 'allout-expose-topic (list 'quote spec))))
5284 ;;;_ #7 Systematic outline presentation -- copying, printing, flattening
5286 ;;;_ - Mapping and processing of topics
5287 ;;;_ ( See also Subtree Charting, in Navigation code.)
5288 ;;;_ > allout-stringify-flat-index (flat-index)
5289 (defun allout-stringify-flat-index (flat-index &optional context)
5290 "Convert list representing section/subsection/... to document string.
5292 Optional arg CONTEXT indicates interior levels to include."
5293 (let ((delim ".")
5294 result
5295 numstr
5296 (context-depth (or (and context 2) 1)))
5297 ;; Take care of the explicit context:
5298 (while (> context-depth 0)
5299 (setq numstr (int-to-string (car flat-index))
5300 flat-index (cdr flat-index)
5301 result (if flat-index
5302 (cons delim (cons numstr result))
5303 (cons numstr result))
5304 context-depth (if flat-index (1- context-depth) 0)))
5305 (setq delim " ")
5306 ;; Take care of the indentation:
5307 (if flat-index
5308 (progn
5309 (while flat-index
5310 (setq result
5311 (cons delim
5312 (cons (make-string
5313 (1+ (truncate (if (zerop (car flat-index))
5315 (log10 (car flat-index)))))
5317 result)))
5318 (setq flat-index (cdr flat-index)))
5319 ;; Dispose of single extra delim:
5320 (setq result (cdr result))))
5321 (apply 'concat result)))
5322 ;;;_ > allout-stringify-flat-index-plain (flat-index)
5323 (defun allout-stringify-flat-index-plain (flat-index)
5324 "Convert list representing section/subsection/... to document string."
5325 (let ((delim ".")
5326 result)
5327 (while flat-index
5328 (setq result (cons (int-to-string (car flat-index))
5329 (if result
5330 (cons delim result))))
5331 (setq flat-index (cdr flat-index)))
5332 (apply 'concat result)))
5333 ;;;_ > allout-stringify-flat-index-indented (flat-index)
5334 (defun allout-stringify-flat-index-indented (flat-index)
5335 "Convert list representing section/subsection/... to document string."
5336 (let ((delim ".")
5337 result
5338 numstr)
5339 ;; Take care of the explicit context:
5340 (setq numstr (int-to-string (car flat-index))
5341 flat-index (cdr flat-index)
5342 result (if flat-index
5343 (cons delim (cons numstr result))
5344 (cons numstr result)))
5345 (setq delim " ")
5346 ;; Take care of the indentation:
5347 (if flat-index
5348 (progn
5349 (while flat-index
5350 (setq result
5351 (cons delim
5352 (cons (make-string
5353 (1+ (truncate (if (zerop (car flat-index))
5355 (log10 (car flat-index)))))
5357 result)))
5358 (setq flat-index (cdr flat-index)))
5359 ;; Dispose of single extra delim:
5360 (setq result (cdr result))))
5361 (apply 'concat result)))
5362 ;;;_ > allout-listify-exposed (&optional start end format)
5363 (defun allout-listify-exposed (&optional start end format)
5365 "Produce a list representing exposed topics in current region.
5367 This list can then be used by `allout-process-exposed' to manipulate
5368 the subject region.
5370 Optional START and END indicate bounds of region.
5372 Optional arg, FORMAT, designates an alternate presentation form for
5373 the prefix:
5375 list -- Present prefix as numeric section.subsection..., starting with
5376 section indicated by the list, innermost nesting first.
5377 `indent' (symbol) -- Convert header prefixes to all white space,
5378 except for distinctive bullets.
5380 The elements of the list produced are lists that represents a topic
5381 header and body. The elements of that list are:
5383 - a number representing the depth of the topic,
5384 - a string representing the header-prefix, including trailing whitespace and
5385 bullet.
5386 - a string representing the bullet character,
5387 - and a series of strings, each containing one line of the exposed
5388 portion of the topic entry."
5390 (interactive "r")
5391 (save-excursion
5392 (let*
5393 ((inhibit-field-text-motion t)
5394 ;; state vars:
5395 strings prefix result depth new-depth out gone-out bullet beg
5396 next done)
5398 (goto-char start)
5399 (beginning-of-line)
5400 ;; Goto initial topic, and register preceeding stuff, if any:
5401 (if (> (allout-goto-prefix-doublechecked) start)
5402 ;; First topic follows beginning point -- register preliminary stuff:
5403 (setq result (list (list 0 "" nil
5404 (buffer-substring start (1- (point)))))))
5405 (while (and (not done)
5406 (not (eobp)) ; Loop until we've covered the region.
5407 (not (> (point) end)))
5408 (setq depth allout-recent-depth ; Current topics depth,
5409 bullet (allout-recent-bullet) ; ... bullet,
5410 prefix (allout-recent-prefix)
5411 beg (progn (allout-end-of-prefix t) (point))) ; and beginning.
5412 (setq done ; The boundary for the current topic:
5413 (not (allout-next-visible-heading 1)))
5414 (setq new-depth allout-recent-depth)
5415 (setq gone-out out
5416 out (< new-depth depth))
5417 (beginning-of-line)
5418 (setq next (point))
5419 (goto-char beg)
5420 (setq strings nil)
5421 (while (> next (point)) ; Get all the exposed text in
5422 (setq strings
5423 (cons (buffer-substring
5425 ;To hidden text or end of line:
5426 (progn
5427 (end-of-line)
5428 (allout-back-to-visible-text)))
5429 strings))
5430 (when (< (point) next) ; Resume from after hid text, if any.
5431 (line-move 1)
5432 (beginning-of-line))
5433 (setq beg (point)))
5434 ;; Accumulate list for this topic:
5435 (setq strings (nreverse strings))
5436 (setq result
5437 (cons
5438 (if format
5439 (let ((special (if (string-match
5440 (regexp-quote bullet)
5441 allout-distinctive-bullets-string)
5442 bullet)))
5443 (cond ((listp format)
5444 (list depth
5445 (if allout-abbreviate-flattened-numbering
5446 (allout-stringify-flat-index format
5447 gone-out)
5448 (allout-stringify-flat-index-plain
5449 format))
5450 strings
5451 special))
5452 ((eq format 'indent)
5453 (if special
5454 (list depth
5455 (concat (make-string (1+ depth) ? )
5456 (substring prefix -1))
5457 strings)
5458 (list depth
5459 (make-string depth ? )
5460 strings)))
5461 (t (error "allout-listify-exposed: %s %s"
5462 "invalid format" format))))
5463 (list depth prefix strings))
5464 result))
5465 ;; Reasses format, if any:
5466 (if (and format (listp format))
5467 (cond ((= new-depth depth)
5468 (setq format (cons (1+ (car format))
5469 (cdr format))))
5470 ((> new-depth depth) ; descending -- assume by 1:
5471 (setq format (cons 1 format)))
5473 ; Pop the residue:
5474 (while (< new-depth depth)
5475 (setq format (cdr format))
5476 (setq depth (1- depth)))
5477 ; And increment the current one:
5478 (setq format
5479 (cons (1+ (or (car format)
5480 -1))
5481 (cdr format)))))))
5482 ;; Put the list with first at front, to last at back:
5483 (nreverse result))))
5484 ;;;_ > allout-region-active-p ()
5485 (defmacro allout-region-active-p ()
5486 (cond ((fboundp 'use-region-p) '(use-region-p))
5487 ((fboundp 'region-active-p) '(region-active-p))
5488 (t 'mark-active)))
5489 ;;_ > allout-process-exposed (&optional func from to frombuf
5490 ;;; tobuf format)
5491 (defun allout-process-exposed (&optional func from to frombuf tobuf
5492 format start-num)
5493 "Map function on exposed parts of current topic; results to another buffer.
5495 All args are options; default values itemized below.
5497 Apply FUNCTION to exposed portions FROM position TO position in buffer
5498 FROMBUF to buffer TOBUF. Sixth optional arg, FORMAT, designates an
5499 alternate presentation form:
5501 `flat' -- Present prefix as numeric section.subsection..., starting with
5502 section indicated by the START-NUM, innermost nesting first.
5503 X`flat-indented' -- Prefix is like `flat' for first topic at each
5504 X level, but subsequent topics have only leaf topic
5505 X number, padded with blanks to line up with first.
5506 `indent' (symbol) -- Convert header prefixes to all white space,
5507 except for distinctive bullets.
5509 Defaults:
5510 FUNCTION: `allout-insert-listified'
5511 FROM: region start, if region active, else start of buffer
5512 TO: region end, if region active, else end of buffer
5513 FROMBUF: current buffer
5514 TOBUF: buffer name derived: \"*current-buffer-name exposed*\"
5515 FORMAT: nil"
5517 ; Resolve arguments,
5518 ; defaulting if necessary:
5519 (if (not func) (setq func 'allout-insert-listified))
5520 (if (not (and from to))
5521 (if (allout-region-active-p)
5522 (setq from (region-beginning) to (region-end))
5523 (setq from (point-min) to (point-max))))
5524 (if frombuf
5525 (if (not (bufferp frombuf))
5526 ;; Specified but not a buffer -- get it:
5527 (let ((got (get-buffer frombuf)))
5528 (if (not got)
5529 (error (concat "allout-process-exposed: source buffer "
5530 frombuf
5531 " not found."))
5532 (setq frombuf got))))
5533 ;; not specified -- default it:
5534 (setq frombuf (current-buffer)))
5535 (if tobuf
5536 (if (not (bufferp tobuf))
5537 (setq tobuf (get-buffer-create tobuf)))
5538 ;; not specified -- default it:
5539 (setq tobuf (concat "*" (buffer-name frombuf) " exposed*")))
5540 (if (listp format)
5541 (nreverse format))
5543 (let* ((listified
5544 (progn (set-buffer frombuf)
5545 (allout-listify-exposed from to format))))
5546 (set-buffer tobuf)
5547 (mapc func listified)
5548 (pop-to-buffer tobuf)))
5550 ;;;_ - Copy exposed
5551 ;;;_ > allout-insert-listified (listified)
5552 (defun allout-insert-listified (listified)
5553 "Insert contents of listified outline portion in current buffer.
5555 LISTIFIED is a list representing each topic header and body:
5557 \`(depth prefix text)'
5559 or \`(depth prefix text bullet-plus)'
5561 If `bullet-plus' is specified, it is inserted just after the entire prefix."
5562 (setq listified (cdr listified))
5563 (let ((prefix (prog1
5564 (car listified)
5565 (setq listified (cdr listified))))
5566 (text (prog1
5567 (car listified)
5568 (setq listified (cdr listified))))
5569 (bullet-plus (car listified)))
5570 (insert prefix)
5571 (if bullet-plus (insert (concat " " bullet-plus)))
5572 (while text
5573 (insert (car text))
5574 (if (setq text (cdr text))
5575 (insert "\n")))
5576 (insert "\n")))
5577 ;;;_ > allout-copy-exposed-to-buffer (&optional arg tobuf format)
5578 (defun allout-copy-exposed-to-buffer (&optional arg tobuf format)
5579 "Duplicate exposed portions of current outline to another buffer.
5581 Other buffer has current buffers name with \" exposed\" appended to it.
5583 With repeat count, copy the exposed parts of only the current topic.
5585 Optional second arg TOBUF is target buffer name.
5587 Optional third arg FORMAT, if non-nil, symbolically designates an
5588 alternate presentation format for the outline:
5590 `flat' - Convert topic header prefixes to numeric
5591 section.subsection... identifiers.
5592 `indent' - Convert header prefixes to all white space, except for
5593 distinctive bullets.
5594 `indent-flat' - The best of both - only the first of each level has
5595 the full path, the rest have only the section number
5596 of the leaf, preceded by the right amount of indentation."
5598 (interactive "P")
5599 (if (not tobuf)
5600 (setq tobuf (get-buffer-create (concat "*" (buffer-name) " exposed*"))))
5601 (let* ((start-pt (point))
5602 (beg (if arg (allout-back-to-current-heading) (point-min)))
5603 (end (if arg (allout-end-of-current-subtree) (point-max)))
5604 (buf (current-buffer))
5605 (start-list ()))
5606 (if (eq format 'flat)
5607 (setq format (if arg (save-excursion
5608 (goto-char beg)
5609 (allout-topic-flat-index))
5610 '(1))))
5611 (save-excursion (set-buffer tobuf)(erase-buffer))
5612 (allout-process-exposed 'allout-insert-listified
5615 (current-buffer)
5616 tobuf
5617 format start-list)
5618 (goto-char (point-min))
5619 (pop-to-buffer buf)
5620 (goto-char start-pt)))
5621 ;;;_ > allout-flatten-exposed-to-buffer (&optional arg tobuf)
5622 (defun allout-flatten-exposed-to-buffer (&optional arg tobuf)
5623 "Present numeric outline of outline's exposed portions in another buffer.
5625 The resulting outline is not compatible with outline mode -- use
5626 `allout-copy-exposed-to-buffer' if you want that.
5628 Use `allout-indented-exposed-to-buffer' for indented presentation.
5630 With repeat count, copy the exposed portions of only current topic.
5632 Other buffer has current buffer's name with \" exposed\" appended to
5633 it, unless optional second arg TOBUF is specified, in which case it is
5634 used verbatim."
5635 (interactive "P")
5636 (allout-copy-exposed-to-buffer arg tobuf 'flat))
5637 ;;;_ > allout-indented-exposed-to-buffer (&optional arg tobuf)
5638 (defun allout-indented-exposed-to-buffer (&optional arg tobuf)
5639 "Present indented outline of outline's exposed portions in another buffer.
5641 The resulting outline is not compatible with outline mode -- use
5642 `allout-copy-exposed-to-buffer' if you want that.
5644 Use `allout-flatten-exposed-to-buffer' for numeric sectional presentation.
5646 With repeat count, copy the exposed portions of only current topic.
5648 Other buffer has current buffer's name with \" exposed\" appended to
5649 it, unless optional second arg TOBUF is specified, in which case it is
5650 used verbatim."
5651 (interactive "P")
5652 (allout-copy-exposed-to-buffer arg tobuf 'indent))
5654 ;;;_ - LaTeX formatting
5655 ;;;_ > allout-latex-verb-quote (string &optional flow)
5656 (defun allout-latex-verb-quote (string &optional flow)
5657 "Return copy of STRING for literal reproduction across LaTeX processing.
5658 Expresses the original characters (including carriage returns) of the
5659 string across LaTeX processing."
5660 (mapconcat (function
5661 (lambda (char)
5662 (cond ((memq char '(?\\ ?$ ?% ?# ?& ?{ ?} ?_ ?^ ?- ?*))
5663 (concat "\\char" (number-to-string char) "{}"))
5664 ((= char ?\n) "\\\\")
5665 (t (char-to-string char)))))
5666 string
5667 ""))
5668 ;;;_ > allout-latex-verbatim-quote-curr-line ()
5669 (defun allout-latex-verbatim-quote-curr-line ()
5670 "Express line for exact (literal) representation across LaTeX processing.
5672 Adjust line contents so it is unaltered (from the original line)
5673 across LaTeX processing, within the context of a `verbatim'
5674 environment. Leaves point at the end of the line."
5675 (let ((inhibit-field-text-motion t))
5676 (beginning-of-line)
5677 (let ((beg (point))
5678 (end (progn (end-of-line)(point))))
5679 (goto-char beg)
5680 (save-match-data
5681 (while (re-search-forward "\\\\"
5682 ;;"\\\\\\|\\{\\|\\}\\|\\_\\|\\$\\|\\\"\\|\\&\\|\\^\\|\\-\\|\\*\\|#"
5683 end ; bounded by end-of-line
5684 1) ; no matches, move to end & return nil
5685 (goto-char (match-beginning 2))
5686 (insert "\\")
5687 (setq end (1+ end))
5688 (goto-char (1+ (match-end 2))))))))
5689 ;;;_ > allout-insert-latex-header (buffer)
5690 (defun allout-insert-latex-header (buffer)
5691 "Insert initial LaTeX commands at point in BUFFER."
5692 ;; Much of this is being derived from the stuff in appendix of E in
5693 ;; the TeXBook, pg 421.
5694 (set-buffer buffer)
5695 (let ((doc-style (format "\n\\documentstyle{%s}\n"
5696 "report"))
5697 (page-numbering (if allout-number-pages
5698 "\\pagestyle{empty}\n"
5699 ""))
5700 (titlecmd (format "\\newcommand{\\titlecmd}[1]{{%s #1}}\n"
5701 allout-title-style))
5702 (labelcmd (format "\\newcommand{\\labelcmd}[1]{{%s #1}}\n"
5703 allout-label-style))
5704 (headlinecmd (format "\\newcommand{\\headlinecmd}[1]{{%s #1}}\n"
5705 allout-head-line-style))
5706 (bodylinecmd (format "\\newcommand{\\bodylinecmd}[1]{{%s #1}}\n"
5707 allout-body-line-style))
5708 (setlength (format "%s%s%s%s"
5709 "\\newlength{\\stepsize}\n"
5710 "\\setlength{\\stepsize}{"
5711 allout-indent
5712 "}\n"))
5713 (oneheadline (format "%s%s%s%s%s%s%s"
5714 "\\newcommand{\\OneHeadLine}[3]{%\n"
5715 "\\noindent%\n"
5716 "\\hspace*{#2\\stepsize}%\n"
5717 "\\labelcmd{#1}\\hspace*{.2cm}"
5718 "\\headlinecmd{#3}\\\\["
5719 allout-line-skip
5720 "]\n}\n"))
5721 (onebodyline (format "%s%s%s%s%s%s"
5722 "\\newcommand{\\OneBodyLine}[2]{%\n"
5723 "\\noindent%\n"
5724 "\\hspace*{#1\\stepsize}%\n"
5725 "\\bodylinecmd{#2}\\\\["
5726 allout-line-skip
5727 "]\n}\n"))
5728 (begindoc "\\begin{document}\n\\begin{center}\n")
5729 (title (format "%s%s%s%s"
5730 "\\titlecmd{"
5731 (allout-latex-verb-quote (if allout-title
5732 (condition-case nil
5733 (eval allout-title)
5734 (error "<unnamed buffer>"))
5735 "Unnamed Outline"))
5736 "}\n"
5737 "\\end{center}\n\n"))
5738 (hsize "\\hsize = 7.5 true in\n")
5739 (hoffset "\\hoffset = -1.5 true in\n")
5740 (vspace "\\vspace{.1cm}\n\n"))
5741 (insert (concat doc-style
5742 page-numbering
5743 titlecmd
5744 labelcmd
5745 headlinecmd
5746 bodylinecmd
5747 setlength
5748 oneheadline
5749 onebodyline
5750 begindoc
5751 title
5752 hsize
5753 hoffset
5754 vspace)
5756 ;;;_ > allout-insert-latex-trailer (buffer)
5757 (defun allout-insert-latex-trailer (buffer)
5758 "Insert concluding LaTeX commands at point in BUFFER."
5759 (set-buffer buffer)
5760 (insert "\n\\end{document}\n"))
5761 ;;;_ > allout-latexify-one-item (depth prefix bullet text)
5762 (defun allout-latexify-one-item (depth prefix bullet text)
5763 "Insert LaTeX commands for formatting one outline item.
5765 Args are the topics numeric DEPTH, the header PREFIX lead string, the
5766 BULLET string, and a list of TEXT strings for the body."
5767 (let* ((head-line (if text (car text)))
5768 (body-lines (cdr text))
5769 (curr-line)
5770 body-content bop)
5771 ; Do the head line:
5772 (insert (concat "\\OneHeadLine{\\verb\1 "
5773 (allout-latex-verb-quote bullet)
5774 "\1}{"
5775 depth
5776 "}{\\verb\1 "
5777 (if head-line
5778 (allout-latex-verb-quote head-line)
5780 "\1}\n"))
5781 (if (not body-lines)
5783 ;;(insert "\\beginlines\n")
5784 (insert "\\begin{verbatim}\n")
5785 (while body-lines
5786 (setq curr-line (car body-lines))
5787 (if (and (not body-content)
5788 (not (string-match "^\\s-*$" curr-line)))
5789 (setq body-content t))
5790 ; Mangle any occurrences of
5791 ; "\end{verbatim}" in text,
5792 ; it's special:
5793 (if (and body-content
5794 (setq bop (string-match "\\end{verbatim}" curr-line)))
5795 (setq curr-line (concat (substring curr-line 0 bop)
5797 (substring curr-line bop))))
5798 ;;(insert "|" (car body-lines) "|")
5799 (insert curr-line)
5800 (allout-latex-verbatim-quote-curr-line)
5801 (insert "\n")
5802 (setq body-lines (cdr body-lines)))
5803 (if body-content
5804 (setq body-content nil)
5805 (forward-char -1)
5806 (insert "\\ ")
5807 (forward-char 1))
5808 ;;(insert "\\endlines\n")
5809 (insert "\\end{verbatim}\n")
5811 ;;;_ > allout-latexify-exposed (arg &optional tobuf)
5812 (defun allout-latexify-exposed (arg &optional tobuf)
5813 "Format current topics exposed portions to TOBUF for LaTeX processing.
5814 TOBUF defaults to a buffer named the same as the current buffer, but
5815 with \"*\" prepended and \" latex-formed*\" appended.
5817 With repeat count, copy the exposed portions of entire buffer."
5819 (interactive "P")
5820 (if (not tobuf)
5821 (setq tobuf
5822 (get-buffer-create (concat "*" (buffer-name) " latexified*"))))
5823 (let* ((start-pt (point))
5824 (beg (if arg (point-min) (allout-back-to-current-heading)))
5825 (end (if arg (point-max) (allout-end-of-current-subtree)))
5826 (buf (current-buffer)))
5827 (set-buffer tobuf)
5828 (erase-buffer)
5829 (allout-insert-latex-header tobuf)
5830 (goto-char (point-max))
5831 (allout-process-exposed 'allout-latexify-one-item
5835 tobuf)
5836 (goto-char (point-max))
5837 (allout-insert-latex-trailer tobuf)
5838 (goto-char (point-min))
5839 (pop-to-buffer buf)
5840 (goto-char start-pt)))
5842 ;;;_ #8 Encryption
5843 ;;;_ > allout-toggle-current-subtree-encryption (&optional fetch-pass)
5844 (defun allout-toggle-current-subtree-encryption (&optional fetch-pass)
5845 "Encrypt clear or decrypt encoded text of visibly-containing topic's contents.
5847 Optional FETCH-PASS universal argument provokes key-pair encryption with
5848 single universal argument. With doubled universal argument (value = 16),
5849 it forces prompting for the passphrase regardless of availability from the
5850 passphrase cache. With no universal argument, the appropriate passphrase
5851 is obtained from the cache, if available, else from the user.
5853 Only GnuPG encryption is supported.
5855 \*NOTE WELL* that the encrypted text must be ascii-armored. For gnupg
5856 encryption, include the option ``armor'' in your ~/.gnupg/gpg.conf file.
5858 Both symmetric-key and key-pair encryption is implemented. Symmetric is
5859 the default, use a single (x4) universal argument for keypair mode.
5861 Encrypted topic's bullet is set to a `~' to signal that the contents of the
5862 topic (body and subtopics, but not heading) is pending encryption or
5863 encrypted. `*' asterisk immediately after the bullet signals that the body
5864 is encrypted, its' absence means the topic is meant to be encrypted but is
5865 not. When a file with topics pending encryption is saved, topics pending
5866 encryption are encrypted. See allout-encrypt-unencrypted-on-saves for
5867 auto-encryption specifics.
5869 \*NOTE WELL* that automatic encryption that happens during saves will
5870 default to symmetric encryption -- you must deliberately (re)encrypt key-pair
5871 encrypted topics if you want them to continue to use the key-pair cipher.
5873 Level-one topics, with prefix consisting solely of an `*' asterisk, cannot be
5874 encrypted. If you want to encrypt the contents of a top-level topic, use
5875 \\[allout-shift-in] to increase its depth.
5877 Passphrase Caching
5879 The encryption passphrase is solicited if not currently available in the
5880 passphrase cache from a recent encryption action.
5882 The solicited passphrase is retained for reuse in a cache, if enabled. See
5883 `pgg-cache-passphrase' and `pgg-passphrase-cache-expiry' for details.
5885 Symmetric Passphrase Hinting and Verification
5887 If the file previously had no associated passphrase, or had a different
5888 passphrase than specified, the user is prompted to repeat the new one for
5889 corroboration. A random string encrypted by the new passphrase is set on
5890 the buffer-specific variable `allout-passphrase-verifier-string', for
5891 confirmation of the passphrase when next obtained, before encrypting or
5892 decrypting anything with it. This helps avoid mistakenly shifting between
5893 keys.
5895 If allout customization var `allout-passphrase-verifier-handling' is
5896 non-nil, an entry for `allout-passphrase-verifier-string' and its value is
5897 added to an Emacs 'local variables' section at the end of the file, which
5898 is created if necessary. That setting is for retention of the passphrase
5899 verifier across Emacs sessions.
5901 Similarly, `allout-passphrase-hint-string' stores a user-provided reminder
5902 about their passphrase, and `allout-passphrase-hint-handling' specifies
5903 when the hint is presented, or if passphrase hints are disabled. If
5904 enabled (see the `allout-passphrase-hint-handling' docstring for details),
5905 the hint string is stored in the local-variables section of the file, and
5906 solicited whenever the passphrase is changed."
5907 (interactive "P")
5908 (save-excursion
5909 (allout-back-to-current-heading)
5910 (allout-toggle-subtree-encryption fetch-pass)
5913 ;;;_ > allout-toggle-subtree-encryption (&optional fetch-pass)
5914 (defun allout-toggle-subtree-encryption (&optional fetch-pass)
5915 "Encrypt clear text or decrypt encoded topic contents (body and subtopics.)
5917 Optional FETCH-PASS universal argument provokes key-pair encryption with
5918 single universal argument. With doubled universal argument (value = 16),
5919 it forces prompting for the passphrase regardless of availability from the
5920 passphrase cache. With no universal argument, the appropriate passphrase
5921 is obtained from the cache, if available, else from the user.
5923 Currently only GnuPG encryption is supported, and integration
5924 with gpg-agent is not yet implemented.
5926 \**NOTE WELL** that the encrypted text must be ascii-armored. For gnupg
5927 encryption, include the option ``armor'' in your ~/.gnupg/gpg.conf file.
5929 See `allout-toggle-current-subtree-encryption' for more details."
5931 (interactive "P")
5932 (save-excursion
5933 (allout-end-of-prefix t)
5935 (if (= allout-recent-depth 1)
5936 (error (concat "Cannot encrypt or decrypt level 1 topics -"
5937 " shift it in to make it encryptable")))
5939 (let* ((allout-buffer (current-buffer))
5940 ;; Assess location:
5941 (bullet-pos allout-recent-prefix-beginning)
5942 (after-bullet-pos (point))
5943 (was-encrypted
5944 (progn (if (= (point-max) after-bullet-pos)
5945 (error "no body to encrypt"))
5946 (allout-encrypted-topic-p)))
5947 (was-collapsed (if (not (search-forward "\n" nil t))
5949 (backward-char 1)
5950 (allout-hidden-p)))
5951 (subtree-beg (1+ (point)))
5952 (subtree-end (allout-end-of-subtree))
5953 (subject-text (buffer-substring-no-properties subtree-beg
5954 subtree-end))
5955 (subtree-end-char (char-after (1- subtree-end)))
5956 (subtree-trailing-char (char-after subtree-end))
5957 ;; kluge -- result-text needs to be nil, but we also want to
5958 ;; check for the error condition
5959 (result-text (if (or (string= "" subject-text)
5960 (string= "\n" subject-text))
5961 (error "No topic contents to %scrypt"
5962 (if was-encrypted "de" "en"))
5963 nil))
5964 ;; Assess key parameters:
5965 (key-info (or
5966 ;; detect the type by which it is already encrypted
5967 (and was-encrypted
5968 (allout-encrypted-key-info subject-text))
5969 (and (member fetch-pass '(4 (4)))
5970 '(keypair nil))
5971 '(symmetric nil)))
5972 (for-key-type (car key-info))
5973 (for-key-identity (cadr key-info))
5974 (fetch-pass (and fetch-pass (member fetch-pass '(16 (16)))))
5975 (was-coding-system buffer-file-coding-system))
5977 (when (not was-encrypted)
5978 ;; ensure that non-ascii chars pending encryption are noticed before
5979 ;; they're encrypted, so the coding system is set to accomodate
5980 ;; them.
5981 (setq buffer-file-coding-system
5982 (select-safe-coding-system subtree-beg subtree-end))
5983 ;; if the coding system for the text being encrypted is different
5984 ;; than that prevailing, then there a real risk that the coding
5985 ;; system can't be noticed by emacs when the file is visited. to
5986 ;; mitigate that, offer to preserve the coding system using a file
5987 ;; local variable.
5988 (if (and (not (equal buffer-file-coding-system
5989 was-coding-system))
5990 (yes-or-no-p
5991 (format (concat "Register coding system %s as file local"
5992 " var? Necessary when only encrypted text"
5993 " is in that coding system. ")
5994 buffer-file-coding-system)))
5995 (allout-adjust-file-variable "buffer-file-coding-system"
5996 buffer-file-coding-system)))
5998 (setq result-text
5999 (allout-encrypt-string subject-text was-encrypted
6000 (current-buffer)
6001 for-key-type for-key-identity fetch-pass))
6003 ;; Replace the subtree with the processed product.
6004 (allout-unprotected
6005 (progn
6006 (set-buffer allout-buffer)
6007 (delete-region subtree-beg subtree-end)
6008 (insert result-text)
6009 (if was-collapsed
6010 (allout-flag-region (1- subtree-beg) (point) t))
6011 ;; adjust trailing-blank-lines to preserve topic spacing:
6012 (if (not was-encrypted)
6013 (if (and (= subtree-end-char ?\n)
6014 (= subtree-trailing-char ?\n))
6015 (insert subtree-trailing-char)))
6016 ;; Ensure that the item has an encrypted-entry bullet:
6017 (if (not (string= (buffer-substring-no-properties
6018 (1- after-bullet-pos) after-bullet-pos)
6019 allout-topic-encryption-bullet))
6020 (progn (goto-char (1- after-bullet-pos))
6021 (delete-char 1)
6022 (insert allout-topic-encryption-bullet)))
6023 (if was-encrypted
6024 ;; Remove the is-encrypted bullet qualifier:
6025 (progn (goto-char after-bullet-pos)
6026 (delete-char 1))
6027 ;; Add the is-encrypted bullet qualifier:
6028 (goto-char after-bullet-pos)
6029 (insert "*"))))
6030 (run-hook-with-args 'allout-structure-added-hook
6031 bullet-pos subtree-end))))
6032 ;;;_ > allout-encrypt-string (text decrypt allout-buffer key-type for-key
6033 ;;; fetch-pass &optional retried verifying
6034 ;;; passphrase)
6035 (defun allout-encrypt-string (text decrypt allout-buffer key-type for-key
6036 fetch-pass &optional retried rejected
6037 verifying passphrase)
6038 "Encrypt or decrypt message TEXT.
6040 If DECRYPT is true (default false), then decrypt instead of encrypt.
6042 FETCH-PASS (default false) forces fresh prompting for the passphrase.
6044 KEY-TYPE, either `symmetric' or `keypair', specifies which type
6045 of cypher to use.
6047 FOR-KEY is human readable identification of the first of the user's
6048 eligible secret keys a keypair decryption targets, or else nil.
6050 Optional RETRIED is for internal use -- conveys the number of failed keys
6051 that have been solicited in sequence leading to this current call.
6053 Optional PASSPHRASE enables explicit delivery of the decryption passphrase,
6054 for verification purposes.
6056 Optional REJECTED is for internal use -- conveys the number of
6057 rejections due to matches against
6058 `allout-encryption-ciphertext-rejection-regexps', as limited by
6059 `allout-encryption-ciphertext-rejection-ceiling'.
6061 Returns the resulting string, or nil if the transformation fails."
6063 (require 'pgg)
6065 (if (not (fboundp 'pgg-encrypt-symmetric))
6066 (error "Allout encryption depends on a newer version of pgg"))
6068 (let* ((scheme (upcase
6069 (format "%s" (or pgg-scheme pgg-default-scheme "GPG"))))
6070 (for-key (and (equal key-type 'keypair)
6071 (or for-key
6072 (split-string (read-string
6073 (format "%s message recipients: "
6074 scheme))
6075 "[ \t,]+"))))
6076 (target-prompt-id (if (equal key-type 'keypair)
6077 (if (= (length for-key) 1)
6078 (car for-key) for-key)
6079 (buffer-name allout-buffer)))
6080 (target-cache-id (format "%s-%s"
6081 key-type
6082 (if (equal key-type 'keypair)
6083 target-prompt-id
6084 (or (buffer-file-name allout-buffer)
6085 target-prompt-id))))
6086 (encoding (with-current-buffer allout-buffer
6087 buffer-file-coding-system))
6088 (multibyte (with-current-buffer allout-buffer
6089 enable-multibyte-characters))
6090 (strip-plaintext-regexps
6091 (if (not decrypt)
6092 (allout-get-configvar-values
6093 'allout-encryption-plaintext-sanitization-regexps)))
6094 (reject-ciphertext-regexps
6095 (if (not decrypt)
6096 (allout-get-configvar-values
6097 'allout-encryption-ciphertext-rejection-regexps)))
6098 (rejected (or rejected 0))
6099 (rejections-left (- allout-encryption-ciphertext-rejection-ceiling
6100 rejected))
6101 result-text status
6104 (if (and fetch-pass (not passphrase))
6105 ;; Force later fetch by evicting passphrase from the cache.
6106 (pgg-remove-passphrase-from-cache target-cache-id t))
6108 (catch 'encryption-failed
6110 ;; We handle only symmetric-key passphrase caching.
6111 (if (and (not passphrase)
6112 (not (equal key-type 'keypair)))
6113 (setq passphrase (allout-obtain-passphrase for-key
6114 target-cache-id
6115 target-prompt-id
6116 key-type
6117 allout-buffer
6118 retried fetch-pass)))
6120 (with-temp-buffer
6122 (insert text)
6124 ;; convey the text characteristics of the original buffer:
6125 (set-buffer-multibyte multibyte)
6126 (when encoding
6127 (set-buffer-file-coding-system encoding)
6128 (if (not decrypt)
6129 (encode-coding-region (point-min) (point-max) encoding)))
6131 (when (and strip-plaintext-regexps (not decrypt))
6132 (dolist (re strip-plaintext-regexps)
6133 (let ((re (if (listp re) (car re) re))
6134 (replacement (if (listp re) (cadr re) "")))
6135 (goto-char (point-min))
6136 (save-match-data
6137 (while (re-search-forward re nil t)
6138 (replace-match replacement nil nil))))))
6140 (cond
6142 ;; symmetric:
6143 ((equal key-type 'symmetric)
6144 (setq status
6145 (if decrypt
6147 (pgg-decrypt (point-min) (point-max) passphrase)
6149 (pgg-encrypt-symmetric (point-min) (point-max)
6150 passphrase)))
6152 (if status
6153 (pgg-situate-output (point-min) (point-max))
6154 ;; failed -- handle passphrase caching
6155 (if verifying
6156 (throw 'encryption-failed nil)
6157 (pgg-remove-passphrase-from-cache target-cache-id t)
6158 (error "Symmetric-cipher %scryption failed -- %s"
6159 (if decrypt "de" "en")
6160 "try again with different passphrase"))))
6162 ;; encrypt `keypair':
6163 ((not decrypt)
6165 (setq status
6167 (pgg-encrypt for-key
6168 nil (point-min) (point-max) passphrase))
6170 (if status
6171 (pgg-situate-output (point-min) (point-max))
6172 (error (pgg-remove-passphrase-from-cache target-cache-id t)
6173 (error "encryption failed"))))
6175 ;; decrypt `keypair':
6178 (setq status
6179 (pgg-decrypt (point-min) (point-max) passphrase))
6181 (if status
6182 (pgg-situate-output (point-min) (point-max))
6183 (error (pgg-remove-passphrase-from-cache target-cache-id t)
6184 (error "decryption failed")))))
6186 (setq result-text
6187 (buffer-substring-no-properties
6188 1 (- (point-max) (if decrypt 0 1))))
6191 ;; validate result -- non-empty
6192 (cond ((not result-text)
6193 (if verifying
6195 ;; transform was fruitless, retry w/new passphrase.
6196 (pgg-remove-passphrase-from-cache target-cache-id t)
6197 (allout-encrypt-string text decrypt allout-buffer
6198 key-type for-key nil
6199 (if retried (1+ retried) 1)
6200 rejected verifying nil)))
6202 ;; Retry (within limit) if ciphertext contains rejections:
6203 ((and (not decrypt)
6204 ;; Check for disqualification of this ciphertext:
6205 (let ((regexps reject-ciphertext-regexps)
6206 reject-it)
6207 (while (and regexps (not reject-it))
6208 (setq reject-it (string-match (car regexps)
6209 result-text))
6210 (pop regexps))
6211 reject-it))
6212 (setq rejections-left (1- rejections-left))
6213 (if (<= rejections-left 0)
6214 (error (concat "Ciphertext rejected too many times"
6215 " (%s), per `%s'")
6216 allout-encryption-ciphertext-rejection-ceiling
6217 'allout-encryption-ciphertext-rejection-regexps)
6218 (allout-encrypt-string text decrypt allout-buffer
6219 key-type for-key nil
6220 retried (1+ rejected)
6221 verifying passphrase)))
6222 ;; Barf if encryption yields extraordinary control chars:
6223 ((and (not decrypt)
6224 (string-match "[\C-a\C-k\C-o-\C-z\C-@]"
6225 result-text))
6226 (error (concat "Encryption produced non-armored text, which"
6227 "conflicts with allout mode -- reconfigure!")))
6229 ;; valid result and just verifying or non-symmetric:
6230 ((or verifying (not (equal key-type 'symmetric)))
6231 (if (or verifying decrypt)
6232 (pgg-add-passphrase-to-cache target-cache-id
6233 passphrase t))
6234 result-text)
6236 ;; valid result and regular symmetric -- "register"
6237 ;; passphrase with mnemonic aids/cache.
6239 (set-buffer allout-buffer)
6240 (if passphrase
6241 (pgg-add-passphrase-to-cache target-cache-id
6242 passphrase t))
6243 (allout-update-passphrase-mnemonic-aids for-key passphrase
6244 allout-buffer)
6245 result-text)
6250 ;;;_ > allout-obtain-passphrase (for-key cache-id prompt-id key-type
6251 ;;; allout-buffer retried fetch-pass)
6252 (defun allout-obtain-passphrase (for-key cache-id prompt-id key-type
6253 allout-buffer retried fetch-pass)
6254 "Obtain passphrase for a key from the cache or else from the user.
6256 When obtaining from the user, symmetric-cipher passphrases are verified
6257 against either, if available and enabled, a random string that was
6258 encrypted against the passphrase, or else against repeated entry by the
6259 user for corroboration.
6261 FOR-KEY is the key for which the passphrase is being obtained.
6263 CACHE-ID is the cache id of the key for the passphrase.
6265 PROMPT-ID is the id for use when prompting the user.
6267 KEY-TYPE is either `symmetric' or `keypair'.
6269 ALLOUT-BUFFER is the buffer containing the entry being en/decrypted.
6271 RETRIED is the number of this attempt to obtain this passphrase.
6273 FETCH-PASS causes the passphrase to be solicited from the user, regardless
6274 of the availability of a cached copy."
6276 (if (not (equal key-type 'symmetric))
6277 ;; do regular passphrase read on non-symmetric passphrase:
6278 (pgg-read-passphrase (format "%s passphrase%s: "
6279 (upcase (format "%s" (or pgg-scheme
6280 pgg-default-scheme
6281 "GPG")))
6282 (if prompt-id
6283 (format " for %s" prompt-id)
6284 ""))
6285 cache-id t)
6287 ;; Symmetric hereon:
6289 (save-excursion
6290 (set-buffer allout-buffer)
6291 (let* ((hint (if (and (not (string= allout-passphrase-hint-string ""))
6292 (or (equal allout-passphrase-hint-handling 'always)
6293 (and (equal allout-passphrase-hint-handling
6294 'needed)
6295 retried)))
6296 (format " [%s]" allout-passphrase-hint-string)
6297 ""))
6298 (retry-message (if retried (format " (%s retry)" retried) ""))
6299 (prompt-sans-hint (format "'%s' symmetric passphrase%s: "
6300 prompt-id retry-message))
6301 (full-prompt (format "'%s' symmetric passphrase%s%s: "
6302 prompt-id hint retry-message))
6303 (prompt full-prompt)
6304 (verifier-string (allout-get-encryption-passphrase-verifier))
6306 (cached (and (not fetch-pass)
6307 (pgg-read-passphrase-from-cache cache-id t)))
6308 (got-pass (or cached
6309 (pgg-read-passphrase full-prompt cache-id t)))
6310 confirmation)
6312 (if (not got-pass)
6315 ;; Duplicate our handle on the passphrase so it's not clobbered by
6316 ;; deactivate-passwd memory clearing:
6317 (setq got-pass (copy-sequence got-pass))
6319 (cond (verifier-string
6320 (save-window-excursion
6321 (if (allout-encrypt-string verifier-string 'decrypt
6322 allout-buffer 'symmetric
6323 for-key nil 0 0 'verifying
6324 (copy-sequence got-pass))
6325 (setq confirmation (format "%s" got-pass))))
6327 (if (and (not confirmation)
6328 (if (yes-or-no-p
6329 (concat "Passphrase differs from established"
6330 " -- use new one instead? "))
6331 ;; deactivate password for subsequent
6332 ;; confirmation:
6333 (progn
6334 (pgg-remove-passphrase-from-cache cache-id t)
6335 (setq prompt prompt-sans-hint)
6336 nil)
6338 (progn (pgg-remove-passphrase-from-cache cache-id t)
6339 (error "Wrong passphrase."))))
6340 ;; No verifier string -- force confirmation by repetition of
6341 ;; (new) passphrase:
6342 ((or fetch-pass (not cached))
6343 (pgg-remove-passphrase-from-cache cache-id t))))
6344 ;; confirmation vs new input -- doing pgg-read-passphrase will do the
6345 ;; right thing, in either case:
6346 (if (not confirmation)
6347 (setq confirmation
6348 (pgg-read-passphrase (concat prompt
6349 " ... confirm spelling: ")
6350 cache-id t)))
6351 (prog1
6352 (if (equal got-pass confirmation)
6353 confirmation
6354 (if (yes-or-no-p (concat "spelling of original and"
6355 " confirmation differ -- retry? "))
6356 (progn (setq retried (if retried (1+ retried) 1))
6357 (pgg-remove-passphrase-from-cache cache-id t)
6358 ;; recurse to this routine:
6359 (pgg-read-passphrase prompt-sans-hint cache-id t))
6360 (pgg-remove-passphrase-from-cache cache-id t)
6361 (error "Confirmation failed."))))))))
6362 ;;;_ > allout-encrypted-topic-p ()
6363 (defun allout-encrypted-topic-p ()
6364 "True if the current topic is encryptable and encrypted."
6365 (save-excursion
6366 (allout-end-of-prefix t)
6367 (and (string= (buffer-substring-no-properties (1- (point)) (point))
6368 allout-topic-encryption-bullet)
6369 (save-match-data (looking-at "\\*")))
6372 ;;;_ > allout-encrypted-key-info (text)
6373 ;; XXX gpg-specific, alas
6374 (defun allout-encrypted-key-info (text)
6375 "Return a pair of the key type and identity of a recipient's secret key.
6377 The key type is one of `symmetric' or `keypair'.
6379 If `keypair', and some of the user's secret keys are among those for which
6380 the message was encoded, return the identity of the first. Otherwise,
6381 return nil for the second item of the pair.
6383 An error is raised if the text is not encrypted."
6384 (require 'pgg-parse)
6385 (save-excursion
6386 (with-temp-buffer
6387 (insert text)
6388 (let* ((parsed-armor (pgg-parse-armor-region (point-min) (point-max)))
6389 (type (if (pgg-gpg-symmetric-key-p parsed-armor)
6390 'symmetric
6391 'keypair))
6392 secret-keys first-secret-key for-key-owner)
6393 (if (equal type 'keypair)
6394 (setq secret-keys (pgg-gpg-lookup-all-secret-keys)
6395 first-secret-key (pgg-gpg-select-matching-key parsed-armor
6396 secret-keys)
6397 for-key-owner (and first-secret-key
6398 (pgg-gpg-lookup-key-owner
6399 first-secret-key))))
6400 (list type (pgg-gpg-key-id-from-key-owner for-key-owner))
6405 ;;;_ > allout-create-encryption-passphrase-verifier (passphrase)
6406 (defun allout-create-encryption-passphrase-verifier (passphrase)
6407 "Encrypt random message for later validation of symmetric key's passphrase."
6408 ;; use 20 random ascii characters, across the entire ascii range.
6409 (random t)
6410 (let ((spew (make-string 20 ?\0)))
6411 (dotimes (i (length spew))
6412 (aset spew i (1+ (random 254))))
6413 (allout-encrypt-string spew nil (current-buffer) 'symmetric
6414 nil nil 0 0 passphrase))
6416 ;;;_ > allout-update-passphrase-mnemonic-aids (for-key passphrase
6417 ;;; outline-buffer)
6418 (defun allout-update-passphrase-mnemonic-aids (for-key passphrase
6419 outline-buffer)
6420 "Update passphrase verifier and hint strings if necessary.
6422 See `allout-passphrase-verifier-string' and `allout-passphrase-hint-string'
6423 settings.
6425 PASSPHRASE is the passphrase being mnemonicized.
6427 OUTLINE-BUFFER is the buffer of the outline being adjusted.
6429 These are used to help the user keep track of the passphrase they use for
6430 symmetric encryption in the file.
6432 Behavior is governed by `allout-passphrase-verifier-handling',
6433 `allout-passphrase-hint-handling', and also, controlling whether the values
6434 are preserved on Emacs local file variables,
6435 `allout-enable-file-variable-adjustment'."
6437 ;; If passphrase doesn't agree with current verifier:
6438 ;; - adjust the verifier
6439 ;; - if passphrase hint handling is enabled, adjust the passphrase hint
6440 ;; - if file var settings are enabled, adjust the file vars
6442 (let* ((new-verifier-needed (not (allout-verify-passphrase
6443 for-key passphrase outline-buffer)))
6444 (new-verifier-string
6445 (if new-verifier-needed
6446 ;; Collapse to a single line and enclose in string quotes:
6447 (subst-char-in-string
6448 ?\n ?\C-a (allout-create-encryption-passphrase-verifier
6449 passphrase))))
6450 new-hint)
6451 (when new-verifier-string
6452 ;; do the passphrase hint first, since it's interactive
6453 (when (and allout-passphrase-hint-handling
6454 (not (equal allout-passphrase-hint-handling 'disabled)))
6455 (setq new-hint
6456 (read-from-minibuffer "Passphrase hint to jog your memory: "
6457 allout-passphrase-hint-string))
6458 (when (not (string= new-hint allout-passphrase-hint-string))
6459 (setq allout-passphrase-hint-string new-hint)
6460 (allout-adjust-file-variable "allout-passphrase-hint-string"
6461 allout-passphrase-hint-string)))
6462 (when allout-passphrase-verifier-handling
6463 (setq allout-passphrase-verifier-string new-verifier-string)
6464 (allout-adjust-file-variable "allout-passphrase-verifier-string"
6465 allout-passphrase-verifier-string))
6469 ;;;_ > allout-get-encryption-passphrase-verifier ()
6470 (defun allout-get-encryption-passphrase-verifier ()
6471 "Return text of the encrypt passphrase verifier, unmassaged, or nil if none.
6473 Derived from value of `allout-passphrase-verifier-string'."
6475 (let ((verifier-string (and (boundp 'allout-passphrase-verifier-string)
6476 allout-passphrase-verifier-string)))
6477 (if verifier-string
6478 ;; Return it uncollapsed
6479 (subst-char-in-string ?\C-a ?\n verifier-string))
6482 ;;;_ > allout-verify-passphrase (key passphrase allout-buffer)
6483 (defun allout-verify-passphrase (key passphrase allout-buffer)
6484 "True if passphrase successfully decrypts verifier, nil otherwise.
6486 \"Otherwise\" includes absence of passphrase verifier."
6487 (save-excursion
6488 (set-buffer allout-buffer)
6489 (and (boundp 'allout-passphrase-verifier-string)
6490 allout-passphrase-verifier-string
6491 (allout-encrypt-string (allout-get-encryption-passphrase-verifier)
6492 'decrypt allout-buffer 'symmetric
6493 key nil 0 0 'verifying passphrase)
6494 t)))
6495 ;;;_ > allout-next-topic-pending-encryption (&optional except-mark)
6496 (defun allout-next-topic-pending-encryption (&optional except-mark)
6497 "Return the point of the next topic pending encryption, or nil if none.
6499 EXCEPT-MARK identifies a point whose containing topics should be excluded
6500 from encryption. This supports 'except-current mode of
6501 `allout-encrypt-unencrypted-on-saves'.
6503 Such a topic has the `allout-topic-encryption-bullet' without an
6504 immediately following '*' that would mark the topic as being encrypted. It
6505 must also have content."
6506 (let (done got content-beg)
6507 (save-match-data
6508 (while (not done)
6510 (if (not (re-search-forward
6511 (format "\\(\\`\\|\n\\)%s *%s[^*]"
6512 (regexp-quote allout-header-prefix)
6513 (regexp-quote allout-topic-encryption-bullet))
6514 nil t))
6515 (setq got nil
6516 done t)
6517 (goto-char (setq got (match-beginning 0)))
6518 (if (save-match-data (looking-at "\n"))
6519 (forward-char 1))
6520 (setq got (point)))
6522 (cond ((not got)
6523 (setq done t))
6525 ((not (search-forward "\n"))
6526 (setq got nil
6527 done t))
6529 ((eobp)
6530 (setq got nil
6531 done t))
6534 (setq content-beg (point))
6535 (backward-char 1)
6536 (allout-end-of-subtree)
6537 (if (or (<= (point) content-beg)
6538 (and except-mark
6539 (<= content-beg except-mark)
6540 (>= (point) except-mark)))
6541 ;; Continue looking
6542 (setq got nil)
6543 ;; Got it!
6544 (setq done t)))
6547 (if got
6548 (goto-char got))
6552 ;;;_ > allout-encrypt-decrypted (&optional except-mark)
6553 (defun allout-encrypt-decrypted (&optional except-mark)
6554 "Encrypt topics pending encryption except those containing exemption point.
6556 EXCEPT-MARK identifies a point whose containing topics should be excluded
6557 from encryption. This supports the `except-current' mode of
6558 `allout-encrypt-unencrypted-on-saves'.
6560 If a topic that is currently being edited was encrypted, we return a list
6561 containing the location of the topic and the location of the cursor just
6562 before the topic was encrypted. This can be used, eg, to decrypt the topic
6563 and exactly resituate the cursor if this is being done as part of a file
6564 save. See `allout-encrypt-unencrypted-on-saves' for more info."
6566 (interactive "p")
6567 (save-match-data
6568 (save-excursion
6569 (let* ((current-mark (point-marker))
6570 (current-mark-position (marker-position current-mark))
6571 was-modified
6572 bo-subtree
6573 editing-topic editing-point)
6574 (goto-char (point-min))
6575 (while (allout-next-topic-pending-encryption except-mark)
6576 (setq was-modified (buffer-modified-p))
6577 (when (save-excursion
6578 (and (boundp 'allout-encrypt-unencrypted-on-saves)
6579 allout-encrypt-unencrypted-on-saves
6580 (setq bo-subtree (re-search-forward "$"))
6581 (not (allout-hidden-p))
6582 (>= current-mark (point))
6583 (allout-end-of-current-subtree)
6584 (<= current-mark (point))))
6585 (setq editing-topic (point)
6586 ;; we had to wait for this 'til now so prior topics are
6587 ;; encrypted, any relevant text shifts are in place:
6588 editing-point (- current-mark-position
6589 (count-trailing-whitespace-region
6590 bo-subtree current-mark-position))))
6591 (allout-toggle-subtree-encryption)
6592 (if (not was-modified)
6593 (set-buffer-modified-p nil))
6595 (if (not was-modified)
6596 (set-buffer-modified-p nil))
6597 (if editing-topic (list editing-topic editing-point))
6603 ;;;_ #9 miscellaneous
6604 ;;;_ : Mode:
6605 ;;;_ > outlineify-sticky ()
6606 ;; outlinify-sticky is correct spelling; provide this alias for sticklers:
6607 ;;;###autoload
6608 (defalias 'outlinify-sticky 'outlineify-sticky)
6609 ;;;###autoload
6610 (defun outlineify-sticky (&optional arg)
6611 "Activate outline mode and establish file var so it is started subsequently.
6613 See doc-string for `allout-layout' and `allout-init' for details on
6614 setup for auto-startup."
6616 (interactive "P")
6618 (allout-mode t)
6620 (save-excursion
6621 (goto-char (point-min))
6622 (if (allout-goto-prefix)
6624 (allout-open-topic 2)
6625 (insert (concat "Dummy outline topic header -- see"
6626 "`allout-mode' docstring: `^Hm'."))
6627 (allout-adjust-file-variable
6628 "allout-layout" (or allout-layout '(-1 : 0))))))
6629 ;;;_ > allout-file-vars-section-data ()
6630 (defun allout-file-vars-section-data ()
6631 "Return data identifying the file-vars section, or nil if none.
6633 Returns a list of the form (BEGINNING-POINT PREFIX-STRING SUFFIX-STRING)."
6634 ;; minimally gleaned from emacs 21.4 files.el hack-local-variables function.
6635 (let (beg prefix suffix)
6636 (save-excursion
6637 (goto-char (point-max))
6638 (search-backward "\n\^L" (max (- (point-max) 3000) (point-min)) 'move)
6639 (if (let ((case-fold-search t))
6640 (not (search-forward "Local Variables:" nil t)))
6642 (setq beg (- (point) 16))
6643 (setq suffix (buffer-substring-no-properties
6644 (point)
6645 (progn (if (search-forward "\n" nil t)
6646 (forward-char -1))
6647 (point))))
6648 (setq prefix (buffer-substring-no-properties
6649 (progn (if (search-backward "\n" nil t)
6650 (forward-char 1))
6651 (point))
6652 beg))
6653 (list beg prefix suffix))
6657 ;;;_ > allout-adjust-file-variable (varname value)
6658 (defun allout-adjust-file-variable (varname value)
6659 "Adjust the setting of an Emacs file variable named VARNAME to VALUE.
6661 This activity is inhibited if either `enable-local-variables'
6662 `allout-enable-file-variable-adjustment' are nil.
6664 When enabled, an entry for the variable is created if not already present,
6665 or changed if established with a different value. The section for the file
6666 variables, itself, is created if not already present. When created, the
6667 section lines (including the section line) exist as second-level topics in
6668 a top-level topic at the end of the file.
6670 `enable-local-variables' must be true for any of this to happen."
6671 (if (not (and enable-local-variables
6672 allout-enable-file-variable-adjustment))
6674 (save-excursion
6675 (let ((inhibit-field-text-motion t)
6676 (section-data (allout-file-vars-section-data))
6677 beg prefix suffix)
6678 (if section-data
6679 (setq beg (car section-data)
6680 prefix (cadr section-data)
6681 suffix (car (cddr section-data)))
6682 ;; create the section
6683 (goto-char (point-max))
6684 (open-line 1)
6685 (allout-open-topic 0)
6686 (end-of-line)
6687 (insert "Local emacs vars.\n")
6688 (allout-open-topic 1)
6689 (setq beg (point)
6690 suffix ""
6691 prefix (buffer-substring-no-properties (progn
6692 (beginning-of-line)
6693 (point))
6694 beg))
6695 (goto-char beg)
6696 (insert "Local variables:\n")
6697 (allout-open-topic 0)
6698 (insert "End:\n")
6700 ;; look for existing entry or create one, leaving point for insertion
6701 ;; of new value:
6702 (goto-char beg)
6703 (allout-show-to-offshoot)
6704 (if (search-forward (concat "\n" prefix varname ":") nil t)
6705 (let* ((value-beg (point))
6706 (line-end (progn (if (search-forward "\n" nil t)
6707 (forward-char -1))
6708 (point)))
6709 (value-end (- line-end (length suffix))))
6710 (if (> value-end value-beg)
6711 (delete-region value-beg value-end)))
6712 (end-of-line)
6713 (open-line 1)
6714 (forward-line 1)
6715 (insert (concat prefix varname ":")))
6716 (insert (format " %S%s" value suffix))
6721 ;;;_ > allout-get-configvar-values (varname)
6722 (defun allout-get-configvar-values (configvar-name)
6723 "Return a list of values of the symbols in list bound to CONFIGVAR-NAME.
6725 The user is prompted for removal of symbols that are unbound, and they
6726 otherwise are ignored.
6728 CONFIGVAR-NAME should be the name of the configuration variable,
6729 not its value."
6731 (let ((configvar-value (symbol-value configvar-name))
6732 got)
6733 (dolist (sym configvar-value)
6734 (if (not (boundp sym))
6735 (if (yes-or-no-p (format "%s entry `%s' is unbound -- remove it? "
6736 configvar-name sym))
6737 (delq sym (symbol-value configvar-name)))
6738 (push (symbol-value sym) got)))
6739 (reverse got)))
6740 ;;;_ : Topics:
6741 ;;;_ > allout-mark-topic ()
6742 (defun allout-mark-topic ()
6743 "Put the region around topic currently containing point."
6744 (interactive)
6745 (let ((inhibit-field-text-motion t))
6746 (beginning-of-line))
6747 (allout-goto-prefix-doublechecked)
6748 (push-mark (point))
6749 (allout-end-of-current-subtree)
6750 (exchange-point-and-mark))
6751 ;;;_ : UI:
6752 ;;;_ > solicit-char-in-string (prompt string &optional do-defaulting)
6753 (defun solicit-char-in-string (prompt string &optional do-defaulting)
6754 "Solicit (with first arg PROMPT) choice of a character from string STRING.
6756 Optional arg DO-DEFAULTING indicates to accept empty input (CR)."
6758 (let ((new-prompt prompt)
6759 got)
6761 (while (not got)
6762 (message "%s" new-prompt)
6764 ;; We do our own reading here, so we can circumvent, eg, special
6765 ;; treatment for `?' character. (Oughta use minibuffer keymap instead.)
6766 (setq got
6767 (char-to-string (let ((cursor-in-echo-area nil)) (read-char))))
6769 (setq got
6770 (cond ((string-match (regexp-quote got) string) got)
6771 ((and do-defaulting (string= got "\r"))
6772 ;; Return empty string to default:
6774 ((string= got "\C-g") (signal 'quit nil))
6776 (setq new-prompt (concat prompt
6778 " ...pick from: "
6779 string
6780 ""))
6781 nil))))
6782 ;; got something out of loop -- return it:
6783 got)
6785 ;;;_ : Strings:
6786 ;;;_ > regexp-sans-escapes (string)
6787 (defun regexp-sans-escapes (regexp &optional successive-backslashes)
6788 "Return a copy of REGEXP with all character escapes stripped out.
6790 Representations of actual backslashes -- '\\\\\\\\' -- are left as a
6791 single backslash.
6793 Optional arg SUCCESSIVE-BACKSLASHES is used internally for recursion."
6795 (if (string= regexp "")
6797 ;; Set successive-backslashes to number if current char is
6798 ;; backslash, or else to nil:
6799 (setq successive-backslashes
6800 (if (= (aref regexp 0) ?\\)
6801 (if successive-backslashes (1+ successive-backslashes) 1)
6802 nil))
6803 (if (or (not successive-backslashes) (= 2 successive-backslashes))
6804 ;; Include first char:
6805 (concat (substring regexp 0 1)
6806 (regexp-sans-escapes (substring regexp 1)))
6807 ;; Exclude first char, but maintain count:
6808 (regexp-sans-escapes (substring regexp 1) successive-backslashes))))
6809 ;;;_ > count-trailing-whitespace-region (beg end)
6810 (defun count-trailing-whitespace-region (beg end)
6811 "Return number of trailing whitespace chars between BEG and END.
6813 If BEG is bigger than END we return 0."
6814 (if (> beg end)
6816 (save-match-data
6817 (save-excursion
6818 (goto-char beg)
6819 (let ((count 0))
6820 (while (re-search-forward "[ ][ ]*$" end t)
6821 (goto-char (1+ (match-beginning 2)))
6822 (setq count (1+ count)))
6823 count)))))
6824 ;;;_ > allout-format-quote (string)
6825 (defun allout-format-quote (string)
6826 "Return a copy of string with all \"%\" characters doubled."
6827 (apply 'concat
6828 (mapcar (lambda (char) (if (= char ?%) "%%" (char-to-string char)))
6829 string)))
6830 ;;;_ : lists
6831 ;;;_ > allout-flatten (list)
6832 (defun allout-flatten (list)
6833 "Return a list of all atoms in list."
6834 ;; classic.
6835 (cond ((null list) nil)
6836 ((atom (car list)) (cons (car list) (allout-flatten (cdr list))))
6837 (t (append (allout-flatten (car list)) (allout-flatten (cdr list))))))
6838 ;;;_ : Compatibility:
6839 ;;;_ > allout-mark-marker to accommodate divergent emacsen:
6840 (defun allout-mark-marker (&optional force buffer)
6841 "Accommodate the different signature for `mark-marker' across Emacsen.
6843 XEmacs takes two optional args, while mainline GNU Emacs does not,
6844 so pass them along when appropriate."
6845 (if (featurep 'xemacs)
6846 (apply 'mark-marker force buffer)
6847 (mark-marker)))
6848 ;;;_ > subst-char-in-string if necessary
6849 (if (not (fboundp 'subst-char-in-string))
6850 (defun subst-char-in-string (fromchar tochar string &optional inplace)
6851 "Replace FROMCHAR with TOCHAR in STRING each time it occurs.
6852 Unless optional argument INPLACE is non-nil, return a new string."
6853 (let ((i (length string))
6854 (newstr (if inplace string (copy-sequence string))))
6855 (while (> i 0)
6856 (setq i (1- i))
6857 (if (eq (aref newstr i) fromchar)
6858 (aset newstr i tochar)))
6859 newstr)))
6860 ;;;_ > wholenump if necessary
6861 (if (not (fboundp 'wholenump))
6862 (defalias 'wholenump 'natnump))
6863 ;;;_ > remove-overlays if necessary
6864 (if (not (fboundp 'remove-overlays))
6865 (defun remove-overlays (&optional beg end name val)
6866 "Clear BEG and END of overlays whose property NAME has value VAL.
6867 Overlays might be moved and/or split.
6868 BEG and END default respectively to the beginning and end of buffer."
6869 (unless beg (setq beg (point-min)))
6870 (unless end (setq end (point-max)))
6871 (if (< end beg)
6872 (setq beg (prog1 end (setq end beg))))
6873 (save-excursion
6874 (dolist (o (overlays-in beg end))
6875 (when (eq (overlay-get o name) val)
6876 ;; Either push this overlay outside beg...end
6877 ;; or split it to exclude beg...end
6878 ;; or delete it entirely (if it is contained in beg...end).
6879 (if (< (overlay-start o) beg)
6880 (if (> (overlay-end o) end)
6881 (progn
6882 (move-overlay (copy-overlay o)
6883 (overlay-start o) beg)
6884 (move-overlay o end (overlay-end o)))
6885 (move-overlay o (overlay-start o) beg))
6886 (if (> (overlay-end o) end)
6887 (move-overlay o end (overlay-end o))
6888 (delete-overlay o)))))))
6890 ;;;_ > copy-overlay if necessary -- xemacs ~ 21.4
6891 (if (not (fboundp 'copy-overlay))
6892 (defun copy-overlay (o)
6893 "Return a copy of overlay O."
6894 (let ((o1 (make-overlay (overlay-start o) (overlay-end o)
6895 ;; FIXME: there's no easy way to find the
6896 ;; insertion-type of the two markers.
6897 (overlay-buffer o)))
6898 (props (overlay-properties o)))
6899 (while props
6900 (overlay-put o1 (pop props) (pop props)))
6901 o1)))
6902 ;;;_ > add-to-invisibility-spec if necessary -- xemacs ~ 21.4
6903 (if (not (fboundp 'add-to-invisibility-spec))
6904 (defun add-to-invisibility-spec (element)
6905 "Add ELEMENT to `buffer-invisibility-spec'.
6906 See documentation for `buffer-invisibility-spec' for the kind of elements
6907 that can be added."
6908 (if (eq buffer-invisibility-spec t)
6909 (setq buffer-invisibility-spec (list t)))
6910 (setq buffer-invisibility-spec
6911 (cons element buffer-invisibility-spec))))
6912 ;;;_ > remove-from-invisibility-spec if necessary -- xemacs ~ 21.4
6913 (if (not (fboundp 'remove-from-invisibility-spec))
6914 (defun remove-from-invisibility-spec (element)
6915 "Remove ELEMENT from `buffer-invisibility-spec'."
6916 (if (consp buffer-invisibility-spec)
6917 (setq buffer-invisibility-spec (delete element
6918 buffer-invisibility-spec)))))
6919 ;;;_ > move-beginning-of-line if necessary -- older emacs, xemacs
6920 (if (not (fboundp 'move-beginning-of-line))
6921 (defun move-beginning-of-line (arg)
6922 "Move point to beginning of current line as displayed.
6923 \(This disregards invisible newlines such as those
6924 which are part of the text that an image rests on.)
6926 With argument ARG not nil or 1, move forward ARG - 1 lines first.
6927 If point reaches the beginning or end of buffer, it stops there.
6928 To ignore intangibility, bind `inhibit-point-motion-hooks' to t."
6929 (interactive "p")
6930 (or arg (setq arg 1))
6931 (if (/= arg 1)
6932 (condition-case nil (line-move (1- arg)) (error nil)))
6934 ;; Move to beginning-of-line, ignoring fields and invisibles.
6935 (skip-chars-backward "^\n")
6936 (while (and (not (bobp))
6937 (let ((prop
6938 (get-char-property (1- (point)) 'invisible)))
6939 (if (eq buffer-invisibility-spec t)
6940 prop
6941 (or (memq prop buffer-invisibility-spec)
6942 (assq prop buffer-invisibility-spec)))))
6943 (goto-char (if (featurep 'xemacs)
6944 (previous-property-change (point))
6945 (previous-char-property-change (point))))
6946 (skip-chars-backward "^\n"))
6947 (vertical-motion 0))
6949 ;;;_ > move-end-of-line if necessary -- older emacs, xemacs
6950 (if (not (fboundp 'move-end-of-line))
6951 (defun move-end-of-line (arg)
6952 "Move point to end of current line as displayed.
6953 \(This disregards invisible newlines such as those
6954 which are part of the text that an image rests on.)
6956 With argument ARG not nil or 1, move forward ARG - 1 lines first.
6957 If point reaches the beginning or end of buffer, it stops there.
6958 To ignore intangibility, bind `inhibit-point-motion-hooks' to t."
6959 (interactive "p")
6960 (or arg (setq arg 1))
6961 (let (done)
6962 (while (not done)
6963 (let ((newpos
6964 (save-excursion
6965 (let ((goal-column 0))
6966 (and (condition-case nil
6967 (or (line-move arg) t)
6968 (error nil))
6969 (not (bobp))
6970 (progn
6971 (while
6972 (and
6973 (not (bobp))
6974 (let ((prop
6975 (get-char-property (1- (point))
6976 'invisible)))
6977 (if (eq buffer-invisibility-spec t)
6978 prop
6979 (or (memq prop
6980 buffer-invisibility-spec)
6981 (assq prop
6982 buffer-invisibility-spec)))))
6983 (goto-char
6984 (previous-char-property-change (point))))
6985 (backward-char 1)))
6986 (point)))))
6987 (goto-char newpos)
6988 (if (and (> (point) newpos)
6989 (eq (preceding-char) ?\n))
6990 (backward-char 1)
6991 (if (and (> (point) newpos) (not (eobp))
6992 (not (eq (following-char) ?\n)))
6993 ;; If we skipped something intangible
6994 ;; and now we're not really at eol,
6995 ;; keep going.
6996 (setq arg 1)
6997 (setq done t)))))))
7000 ;;;_ #10 Unfinished
7001 ;;;_ > allout-bullet-isearch (&optional bullet)
7002 (defun allout-bullet-isearch (&optional bullet)
7003 "Isearch (regexp) for topic with bullet BULLET."
7004 (interactive)
7005 (if (not bullet)
7006 (setq bullet (solicit-char-in-string
7007 "ISearch for topic with bullet: "
7008 (regexp-sans-escapes allout-bullets-string))))
7010 (let ((isearch-regexp t)
7011 (isearch-string (concat "^"
7012 allout-header-prefix
7013 "[ \t]*"
7014 bullet)))
7015 (isearch-repeat 'forward)
7016 (isearch-mode t)))
7018 ;;;_ #11 Unit tests -- this should be last item before "Provide"
7019 ;;;_ > allout-run-unit-tests ()
7020 (defun allout-run-unit-tests ()
7021 "Run the various allout unit tests."
7022 (message "Running allout tests...")
7023 (allout-test-resumptions)
7024 (message "Running allout tests... Done.")
7025 (sit-for .5))
7026 ;;;_ : test resumptions:
7027 ;;;_ > allout-tests-obliterate-variable (name)
7028 (defun allout-tests-obliterate-variable (name)
7029 "Completely unbind variable with NAME."
7030 (if (local-variable-p name) (kill-local-variable name))
7031 (while (boundp name) (makunbound name)))
7032 ;;;_ > allout-test-resumptions ()
7033 (defvar allout-tests-globally-unbound nil
7034 "Fodder for allout resumptions tests -- defvar just for byte compiler.")
7035 (defvar allout-tests-globally-true nil
7036 "Fodder for allout resumptions tests -- defvar just for byte compiler.")
7037 (defvar allout-tests-locally-true nil
7038 "Fodder for allout resumptions tests -- defvar just for byte compiler.")
7039 (defun allout-test-resumptions ()
7040 "Exercise allout resumptions."
7041 ;; for each resumption case, we also test that the right local/global
7042 ;; scopes are affected during resumption effects:
7044 ;; ensure that previously unbound variables return to the unbound state.
7045 (with-temp-buffer
7046 (allout-tests-obliterate-variable 'allout-tests-globally-unbound)
7047 (allout-add-resumptions '(allout-tests-globally-unbound t))
7048 (assert (not (default-boundp 'allout-tests-globally-unbound)))
7049 (assert (local-variable-p 'allout-tests-globally-unbound))
7050 (assert (boundp 'allout-tests-globally-unbound))
7051 (assert (equal allout-tests-globally-unbound t))
7052 (allout-do-resumptions)
7053 (assert (not (local-variable-p 'allout-tests-globally-unbound)))
7054 (assert (not (boundp 'allout-tests-globally-unbound))))
7056 ;; ensure that variable with prior global value is resumed
7057 (with-temp-buffer
7058 (allout-tests-obliterate-variable 'allout-tests-globally-true)
7059 (setq allout-tests-globally-true t)
7060 (allout-add-resumptions '(allout-tests-globally-true nil))
7061 (assert (equal (default-value 'allout-tests-globally-true) t))
7062 (assert (local-variable-p 'allout-tests-globally-true))
7063 (assert (equal allout-tests-globally-true nil))
7064 (allout-do-resumptions)
7065 (assert (not (local-variable-p 'allout-tests-globally-true)))
7066 (assert (boundp 'allout-tests-globally-true))
7067 (assert (equal allout-tests-globally-true t)))
7069 ;; ensure that prior local value is resumed
7070 (with-temp-buffer
7071 (allout-tests-obliterate-variable 'allout-tests-locally-true)
7072 (set (make-local-variable 'allout-tests-locally-true) t)
7073 (assert (not (default-boundp 'allout-tests-locally-true))
7074 nil (concat "Test setup mistake -- variable supposed to"
7075 " not have global binding, but it does."))
7076 (assert (local-variable-p 'allout-tests-locally-true)
7077 nil (concat "Test setup mistake -- variable supposed to have"
7078 " local binding, but it lacks one."))
7079 (allout-add-resumptions '(allout-tests-locally-true nil))
7080 (assert (not (default-boundp 'allout-tests-locally-true)))
7081 (assert (local-variable-p 'allout-tests-locally-true))
7082 (assert (equal allout-tests-locally-true nil))
7083 (allout-do-resumptions)
7084 (assert (boundp 'allout-tests-locally-true))
7085 (assert (local-variable-p 'allout-tests-locally-true))
7086 (assert (equal allout-tests-locally-true t))
7087 (assert (not (default-boundp 'allout-tests-locally-true))))
7089 ;; ensure that last of multiple resumptions holds, for various scopes.
7090 (with-temp-buffer
7091 (allout-tests-obliterate-variable 'allout-tests-globally-unbound)
7092 (allout-tests-obliterate-variable 'allout-tests-globally-true)
7093 (setq allout-tests-globally-true t)
7094 (allout-tests-obliterate-variable 'allout-tests-locally-true)
7095 (set (make-local-variable 'allout-tests-locally-true) t)
7096 (allout-add-resumptions '(allout-tests-globally-unbound t)
7097 '(allout-tests-globally-true nil)
7098 '(allout-tests-locally-true nil))
7099 (allout-add-resumptions '(allout-tests-globally-unbound 2)
7100 '(allout-tests-globally-true 3)
7101 '(allout-tests-locally-true 4))
7102 ;; reestablish many of the basic conditions are maintained after re-add:
7103 (assert (not (default-boundp 'allout-tests-globally-unbound)))
7104 (assert (local-variable-p 'allout-tests-globally-unbound))
7105 (assert (equal allout-tests-globally-unbound 2))
7106 (assert (default-boundp 'allout-tests-globally-true))
7107 (assert (local-variable-p 'allout-tests-globally-true))
7108 (assert (equal allout-tests-globally-true 3))
7109 (assert (not (default-boundp 'allout-tests-locally-true)))
7110 (assert (local-variable-p 'allout-tests-locally-true))
7111 (assert (equal allout-tests-locally-true 4))
7112 (allout-do-resumptions)
7113 (assert (not (local-variable-p 'allout-tests-globally-unbound)))
7114 (assert (not (boundp 'allout-tests-globally-unbound)))
7115 (assert (not (local-variable-p 'allout-tests-globally-true)))
7116 (assert (boundp 'allout-tests-globally-true))
7117 (assert (equal allout-tests-globally-true t))
7118 (assert (boundp 'allout-tests-locally-true))
7119 (assert (local-variable-p 'allout-tests-locally-true))
7120 (assert (equal allout-tests-locally-true t))
7121 (assert (not (default-boundp 'allout-tests-locally-true))))
7123 ;; ensure that deliberately unbinding registered variables doesn't foul things
7124 (with-temp-buffer
7125 (allout-tests-obliterate-variable 'allout-tests-globally-unbound)
7126 (allout-tests-obliterate-variable 'allout-tests-globally-true)
7127 (setq allout-tests-globally-true t)
7128 (allout-tests-obliterate-variable 'allout-tests-locally-true)
7129 (set (make-local-variable 'allout-tests-locally-true) t)
7130 (allout-add-resumptions '(allout-tests-globally-unbound t)
7131 '(allout-tests-globally-true nil)
7132 '(allout-tests-locally-true nil))
7133 (allout-tests-obliterate-variable 'allout-tests-globally-unbound)
7134 (allout-tests-obliterate-variable 'allout-tests-globally-true)
7135 (allout-tests-obliterate-variable 'allout-tests-locally-true)
7136 (allout-do-resumptions))
7138 ;;;_ % Run unit tests if `allout-run-unit-tests-after-load' is true:
7139 (when allout-run-unit-tests-on-load
7140 (allout-run-unit-tests))
7142 ;;;_ #12 Provide
7143 (provide 'allout)
7145 ;;;_* Local emacs vars.
7146 ;; The following `allout-layout' local variable setting:
7147 ;; - closes all topics from the first topic to just before the third-to-last,
7148 ;; - shows the children of the third to last (config vars)
7149 ;; - and the second to last (code section),
7150 ;; - and closes the last topic (this local-variables section).
7151 ;;Local variables:
7152 ;;allout-layout: (0 : -1 -1 0)
7153 ;;End:
7155 ;; arch-tag: cf38fbc3-c044-450f-8bff-afed8ba5681c
7156 ;;; allout.el ends here