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