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