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