Commit | Line | Data |
---|---|---|
6a05d05f | 1 | ;;; allout.el --- extensive outline mode for use alone and with other modes |
19b84ba3 | 2 | |
08cf18e4 GM |
3 | ;; Copyright (C) 1992, 1993, 1994, 2001, 2002, 2003, 2004, 2005, |
4 | ;; 2006, 2007, 2008, 2009 Free Software Foundation, Inc. | |
1977b8f6 | 5 | |
d82979ea EZ |
6 | ;; Author: Ken Manheimer <ken dot manheimer at gmail dot com> |
7 | ;; Maintainer: Ken Manheimer <ken dot manheimer at gmail dot com> | |
60c7bbd2 | 8 | ;; Created: Dec 1991 -- first release to usenet |
4034b0e2 | 9 | ;; Version: 2.2.1 |
d82979ea | 10 | ;; Keywords: outlines wp languages |
01fc9422 | 11 | ;; Website: http://myriadicity.net/Sundry/EmacsAllout |
1977b8f6 RS |
12 | |
13 | ;; This file is part of GNU Emacs. | |
14 | ||
eb3fa2cf | 15 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
59243403 | 16 | ;; it under the terms of the GNU General Public License as published by |
eb3fa2cf GM |
17 | ;; the Free Software Foundation, either version 3 of the License, or |
18 | ;; (at your option) any later version. | |
59243403 | 19 | |
1977b8f6 | 20 | ;; GNU Emacs is distributed in the hope that it will be useful, |
59243403 RS |
21 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
22 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
23 | ;; GNU General Public License for more details. | |
24 | ||
25 | ;; You should have received a copy of the GNU General Public License | |
eb3fa2cf | 26 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
1977b8f6 | 27 | |
6a05d05f | 28 | ;;; Commentary: |
19b84ba3 | 29 | |
96b83743 EZ |
30 | ;; Allout outline minor mode provides extensive outline formatting and |
31 | ;; and manipulation beyond standard emacs outline mode. Some features: | |
71296446 | 32 | ;; |
96b83743 EZ |
33 | ;; - Classic outline-mode topic-oriented navigation and exposure adjustment |
34 | ;; - Topic-oriented editing including coherent topic and subtopic | |
35 | ;; creation, promotion, demotion, cut/paste across depths, etc. | |
36 | ;; - Incremental search with dynamic exposure and reconcealment of text | |
60c7bbd2 | 37 | ;; - Customizable bullet format -- enables programming-language specific |
96b83743 | 38 | ;; outlining, for code-folding editing. (Allout code itself is to try it; |
60c7bbd2 | 39 | ;; formatted as an outline -- do ESC-x eval-buffer in allout.el; but |
96b83743 | 40 | ;; emacs local file variables need to be enabled when the |
60c7bbd2 | 41 | ;; file was visited -- see `enable-local-variables'.) |
96b83743 EZ |
42 | ;; - Configurable per-file initial exposure settings |
43 | ;; - Symmetric-key and key-pair topic encryption, plus symmetric passphrase | |
0949617b EZ |
44 | ;; mnemonic support, with verification against an established passphrase |
45 | ;; (using a stashed encrypted dummy string) and user-supplied hint | |
e14adf87 CY |
46 | ;; maintenance. (See allout-toggle-current-subtree-encryption docstring. |
47 | ;; Currently only GnuPG encryption is supported, and integration | |
48 | ;; with gpg-agent is not yet implemented.) | |
96b83743 EZ |
49 | ;; - Automatic topic-number maintenance |
50 | ;; - "Hot-spot" operation, for single-keystroke maneuvering and | |
d82979ea | 51 | ;; exposure control (see the allout-mode docstring) |
96b83743 | 52 | ;; - Easy rendering of exposed portions into numbered, latex, indented, etc |
d82979ea | 53 | ;; outline styles |
60c7bbd2 | 54 | ;; - Careful attention to whitespace -- enabling blank lines between items |
96b83743 EZ |
55 | ;; and maintenance of hanging indentation (in paragraph auto-fill and |
56 | ;; across topic promotion and demotion) of topic bodies consistent with | |
57 | ;; indentation of their topic header. | |
71296446 | 58 | ;; |
d82979ea | 59 | ;; and more. |
71296446 | 60 | ;; |
96b83743 | 61 | ;; See the `allout-mode' function's docstring for an introduction to the |
01fc9422 EZ |
62 | ;; mode. |
63 | ;; | |
64 | ;; The latest development version and helpful notes are available at | |
96b83743 EZ |
65 | ;; http://myriadicity.net/Sundry/EmacsAllout . |
66 | ;; | |
9179616f | 67 | ;; The outline menubar additions provide quick reference to many of |
d82979ea | 68 | ;; the features, and see the docstring of the variable `allout-init' |
a0a022d5 | 69 | ;; for instructions on priming your Emacs session for automatic |
d82979ea | 70 | ;; activation of allout-mode. |
71296446 | 71 | ;; |
fd5359c6 MR |
72 | ;; See the docstring of the variables `allout-layout' and |
73 | ;; `allout-auto-activation' for details on automatic activation of | |
d82979ea | 74 | ;; `allout-mode' as a minor mode. (It has changed since allout |
19b84ba3 | 75 | ;; 3.x, for those of you that depend on the old method.) |
c567ac01 | 76 | ;; |
60c7bbd2 | 77 | ;; Note -- the lines beginning with `;;;_' are outline topic headers. |
ac052b48 | 78 | ;; Just `ESC-x eval-buffer' to give it a whirl. |
19b84ba3 | 79 | |
d82979ea | 80 | ;; ken manheimer (ken dot manheimer at gmail dot com) |
c567ac01 | 81 | |
6a05d05f PJ |
82 | ;;; Code: |
83 | ||
d82979ea | 84 | ;;;_* Dependency autoloads |
96b83743 | 85 | (require 'overlay) |
01fc9422 EZ |
86 | (eval-when-compile |
87 | ;; Most of the requires here are for stuff covered by autoloads. | |
88 | ;; Since just byte-compiling doesn't trigger autoloads, so that | |
89 | ;; "function not found" warnings would occur without these requires. | |
08cf18e4 GM |
90 | (require 'pgg) |
91 | (require 'pgg-gpg) | |
92 | (require 'overlay) | |
93 | ;; `cl' is required for `assert'. `assert' is not covered by a standard | |
94 | ;; autoload, but it is a macro, so that eval-when-compile is sufficient | |
95 | ;; to byte-compile it in, or to do the require when the buffer evalled. | |
96 | (require 'cl) | |
97 | ) | |
d82979ea | 98 | |
19b84ba3 | 99 | ;;;_* USER CUSTOMIZATION VARIABLES: |
96b83743 EZ |
100 | |
101 | ;;;_ > defgroup allout | |
4bef9110 SE |
102 | (defgroup allout nil |
103 | "Extensive outline mode for use alone and with other modes." | |
fd5359c6 | 104 | :prefix "allout-" |
d82979ea | 105 | :group 'outlines) |
19b84ba3 RS |
106 | |
107 | ;;;_ + Layout, Mode, and Topic Header Configuration | |
108 | ||
2a1408fd DN |
109 | ;;;_ = allout-command-prefix |
110 | (defcustom allout-command-prefix "\C-c " | |
9201cc28 | 111 | "Key sequence to be used as prefix for outline mode command key bindings. |
2a1408fd DN |
112 | |
113 | Default is '\C-c<space>'; just '\C-c' is more short-and-sweet, if you're | |
114 | willing to let allout use a bunch of \C-c keybindings." | |
115 | :type 'string | |
116 | :group 'allout) | |
8dd59f01 | 117 | |
2a1408fd | 118 | ;;;_ = allout-keybindings-list |
8dd59f01 | 119 | ;;; You have to reactivate allout-mode -- `(allout-mode t)' -- to |
2a1408fd DN |
120 | ;;; institute changes to this var. |
121 | (defvar allout-keybindings-list () | |
122 | "*List of `allout-mode' key / function bindings, for `allout-mode-map'. | |
2a1408fd DN |
123 | String or vector key will be prefaced with `allout-command-prefix', |
124 | unless optional third, non-nil element is present.") | |
125 | (setq allout-keybindings-list | |
126 | '( | |
127 | ; Motion commands: | |
128 | ("\C-n" allout-next-visible-heading) | |
129 | ("\C-p" allout-previous-visible-heading) | |
130 | ("\C-u" allout-up-current-level) | |
131 | ("\C-f" allout-forward-current-level) | |
132 | ("\C-b" allout-backward-current-level) | |
133 | ("\C-a" allout-beginning-of-current-entry) | |
134 | ("\C-e" allout-end-of-entry) | |
135 | ; Exposure commands: | |
136 | ("\C-i" allout-show-children) | |
137 | ("\C-s" allout-show-current-subtree) | |
a471d898 | 138 | ("\C-h" allout-hide-current-subtree) |
2a1408fd DN |
139 | ("\C-t" allout-toggle-current-subtree-exposure) |
140 | ("h" allout-hide-current-subtree) | |
141 | ("\C-o" allout-show-current-entry) | |
142 | ("!" allout-show-all) | |
143 | ("x" allout-toggle-current-subtree-encryption) | |
144 | ; Alteration commands: | |
145 | (" " allout-open-sibtopic) | |
146 | ("." allout-open-subtopic) | |
147 | ("," allout-open-supertopic) | |
148 | ("'" allout-shift-in) | |
149 | (">" allout-shift-in) | |
150 | ("<" allout-shift-out) | |
151 | ("\C-m" allout-rebullet-topic) | |
152 | ("*" allout-rebullet-current-heading) | |
153 | ("#" allout-number-siblings) | |
154 | ("\C-k" allout-kill-line t) | |
42c3e51c | 155 | ([?\M-k] allout-copy-line-as-kill t) |
2a1408fd | 156 | ("\C-y" allout-yank t) |
42c3e51c | 157 | ([?\M-y] allout-yank-pop t) |
2a1408fd | 158 | ("\C-k" allout-kill-topic) |
42c3e51c | 159 | ([?\M-k] allout-copy-topic-as-kill) |
2a1408fd DN |
160 | ; Miscellaneous commands: |
161 | ;([?\C-\ ] allout-mark-topic) | |
162 | ("@" allout-resolve-xref) | |
163 | ("=c" allout-copy-exposed-to-buffer) | |
164 | ("=i" allout-indented-exposed-to-buffer) | |
165 | ("=t" allout-latexify-exposed) | |
166 | ("=p" allout-flatten-exposed-to-buffer))) | |
167 | ||
fd5359c6 MR |
168 | ;;;_ = allout-auto-activation |
169 | (defcustom allout-auto-activation nil | |
9201cc28 | 170 | "Regulates auto-activation modality of allout outlines -- see `allout-init'. |
19b84ba3 | 171 | |
fd5359c6 | 172 | Setq-default by `allout-init' to regulate whether or not allout |
19b84ba3 | 173 | outline mode is automatically activated when the buffer-specific |
fd5359c6 MR |
174 | variable `allout-layout' is non-nil, and whether or not the layout |
175 | dictated by `allout-layout' should be imposed on mode activation. | |
19b84ba3 | 176 | |
24190da5 | 177 | With value t, auto-mode-activation and auto-layout are enabled. |
aad94676 | 178 | \(This also depends on `allout-find-file-hook' being installed in |
95c12694 | 179 | `find-file-hook', which is also done by `allout-init'.) |
19b84ba3 RS |
180 | |
181 | With value `ask', auto-mode-activation is enabled, and endorsement for | |
182 | performing auto-layout is asked of the user each time. | |
183 | ||
71296446 | 184 | With value `activate', only auto-mode-activation is enabled, |
353e2ef2 | 185 | auto-layout is not. |
19b84ba3 | 186 | |
24190da5 | 187 | With value nil, neither auto-mode-activation nor auto-layout are |
19b84ba3 RS |
188 | enabled. |
189 | ||
fd5359c6 | 190 | See the docstring for `allout-init' for the proper interface to |
9179616f DL |
191 | this variable." |
192 | :type '(choice (const :tag "On" t) | |
193 | (const :tag "Ask about layout" "ask") | |
194 | (const :tag "Mode only" "activate") | |
195 | (const :tag "Off" nil)) | |
196 | :group 'allout) | |
4034b0e2 LT |
197 | ;;;_ = allout-default-layout |
198 | (defcustom allout-default-layout '(-2 : 0) | |
9201cc28 | 199 | "Default allout outline layout specification. |
4034b0e2 LT |
200 | |
201 | This setting specifies the outline exposure to use when | |
202 | `allout-layout' has the local value `t'. This docstring describes the | |
203 | layout specifications. | |
204 | ||
205 | A list value specifies a default layout for the current buffer, | |
206 | to be applied upon activation of `allout-mode'. Any non-nil | |
207 | value will automatically trigger `allout-mode', provided | |
208 | `allout-init' has been called to enable this behavior. | |
209 | ||
210 | The types of elements in the layout specification are: | |
211 | ||
60c7bbd2 RS |
212 | INTEGER -- dictate the relative depth to open the corresponding topic(s), |
213 | where: | |
214 | -- negative numbers force the topic to be closed before opening | |
215 | to the absolute value of the number, so all siblings are open | |
216 | only to that level. | |
217 | -- positive numbers open to the relative depth indicated by the | |
218 | number, but do not force already opened subtopics to be closed. | |
219 | -- 0 means to close topic -- hide all subitems. | |
220 | : -- repeat spec -- apply the preceeding element to all siblings at | |
221 | current level, *up to* those siblings that would be covered by specs | |
222 | following the `:' on the list. Ie, apply to all topics at level but | |
223 | trailing ones accounted for by trailing specs. (Only the first of | |
224 | multiple colons at the same level is honored -- later ones are ignored.) | |
225 | * -- completely exposes the topic, including bodies | |
226 | + -- exposes all subtopics, but not the bodies | |
227 | - -- exposes the body of the corresponding topic, but not subtopics | |
228 | LIST -- a nested layout spec, to be applied intricately to its | |
4034b0e2 | 229 | corresponding item(s) |
1977b8f6 | 230 | |
4034b0e2 | 231 | Examples: |
60c7bbd2 | 232 | (-2 : 0) |
4034b0e2 LT |
233 | Collapse the top-level topics to show their children and |
234 | grandchildren, but completely collapse the final top-level topic. | |
60c7bbd2 | 235 | (-1 () : 1 0) |
4034b0e2 LT |
236 | Close the first topic so only the immediate subtopics are shown, |
237 | leave the subsequent topics exposed as they are until the second | |
238 | second to last topic, which is exposed at least one level, and | |
239 | completely close the last topic. | |
60c7bbd2 | 240 | (-2 : -1 *) |
4034b0e2 LT |
241 | Expose children and grandchildren of all topics at current |
242 | level except the last two; expose children of the second to | |
243 | last and completely expose the last one, including its subtopics. | |
19b84ba3 | 244 | |
4034b0e2 | 245 | See `allout-expose-topic' for more about the exposure process. |
19b84ba3 | 246 | |
353e2ef2 KH |
247 | Also, allout's mode-specific provisions will make topic prefixes default |
248 | to the comment-start string, if any, of the language of the file. This | |
4034b0e2 LT |
249 | is modulo the setting of `allout-use-mode-specific-leader', which see." |
250 | :type 'allout-layout-type | |
251 | :group 'allout) | |
252 | ;;;_ : allout-layout-type | |
253 | (define-widget 'allout-layout-type 'lazy | |
254 | "Allout layout format customization basic building blocks." | |
255 | :type '(repeat | |
256 | (choice (integer :tag "integer (<= zero is strict)") | |
257 | (const :tag ": (repeat prior)" :) | |
258 | (const :tag "* (completely expose)" *) | |
259 | (const :tag "+ (expose all offspring, headlines only)" +) | |
260 | (const :tag "- (expose topic body but not offspring)" -) | |
261 | (allout-layout-type :tag "<Nested layout>")))) | |
262 | ||
2a1408fd DN |
263 | ;;;_ = allout-inhibit-auto-fill |
264 | (defcustom allout-inhibit-auto-fill nil | |
9201cc28 | 265 | "If non-nil, auto-fill will be inhibited in the allout buffers. |
2a1408fd DN |
266 | |
267 | You can customize this setting to set it for all allout buffers, or set it | |
268 | in individual buffers if you want to inhibit auto-fill only in particular | |
269 | buffers. (You could use a function on `allout-mode-hook' to inhibit | |
270 | auto-fill according, eg, to the major mode.) | |
271 | ||
272 | If you don't set this and auto-fill-mode is enabled, allout will use the | |
273 | value that `normal-auto-fill-function', if any, when allout mode starts, or | |
274 | else allout's special hanging-indent maintaining auto-fill function, | |
275 | `allout-auto-fill'." | |
276 | :type 'boolean | |
277 | :group 'allout) | |
278 | (make-variable-buffer-local 'allout-inhibit-auto-fill) | |
279 | ;;;_ = allout-use-hanging-indents | |
280 | (defcustom allout-use-hanging-indents t | |
9201cc28 | 281 | "If non-nil, topic body text auto-indent defaults to indent of the header. |
2a1408fd | 282 | Ie, it is indented to be just past the header prefix. This is |
8dd59f01 | 283 | relevant mostly for use with `indented-text-mode', or other situations |
2a1408fd DN |
284 | where auto-fill occurs." |
285 | :type 'boolean | |
286 | :group 'allout) | |
287 | (make-variable-buffer-local 'allout-use-hanging-indents) | |
288 | ;;;###autoload | |
289 | (put 'allout-use-hanging-indents 'safe-local-variable | |
290 | (if (fboundp 'booleanp) 'booleanp '(lambda (x) (member x '(t nil))))) | |
291 | ;;;_ = allout-reindent-bodies | |
292 | (defcustom allout-reindent-bodies (if allout-use-hanging-indents | |
293 | 'text) | |
9201cc28 | 294 | "Non-nil enables auto-adjust of topic body hanging indent with depth shifts. |
2a1408fd DN |
295 | |
296 | When active, topic body lines that are indented even with or beyond | |
297 | their topic header are reindented to correspond with depth shifts of | |
298 | the header. | |
299 | ||
300 | A value of t enables reindent in non-programming-code buffers, ie | |
301 | those that do not have the variable `comment-start' set. A value of | |
302 | `force' enables reindent whether or not `comment-start' is set." | |
303 | :type '(choice (const nil) (const t) (const text) (const force)) | |
304 | :group 'allout) | |
305 | ||
306 | (make-variable-buffer-local 'allout-reindent-bodies) | |
307 | ;;;###autoload | |
308 | (put 'allout-reindent-bodies 'safe-local-variable | |
309 | '(lambda (x) (memq x '(nil t text force)))) | |
310 | ||
fd5359c6 MR |
311 | ;;;_ = allout-show-bodies |
312 | (defcustom allout-show-bodies nil | |
9201cc28 | 313 | "If non-nil, show entire body when exposing a topic, rather than |
9179616f DL |
314 | just the header." |
315 | :type 'boolean | |
316 | :group 'allout) | |
fd5359c6 | 317 | (make-variable-buffer-local 'allout-show-bodies) |
8570ce80 SM |
318 | ;;;###autoload |
319 | (put 'allout-show-bodies 'safe-local-variable | |
64686e6d | 320 | (if (fboundp 'booleanp) 'booleanp '(lambda (x) (member x '(t nil))))) |
1977b8f6 | 321 | |
48bd8440 EZ |
322 | ;;;_ = allout-beginning-of-line-cycles |
323 | (defcustom allout-beginning-of-line-cycles t | |
9201cc28 | 324 | "If non-nil, \\[allout-beginning-of-line] will cycle through smart-placement options. |
48bd8440 EZ |
325 | |
326 | Cycling only happens on when the command is repeated, not when it | |
327 | follows a different command. | |
328 | ||
329 | Smart-placement means that repeated calls to this function will | |
330 | advance as follows: | |
331 | ||
332 | - if the cursor is on a non-headline body line and not on the first column: | |
333 | then it goes to the first column | |
334 | - if the cursor is on the first column of a non-headline body line: | |
335 | then it goes to the start of the headline within the item body | |
336 | - if the cursor is on the headline and not the start of the headline: | |
337 | then it goes to the start of the headline | |
338 | - if the cursor is on the start of the headline: | |
c07583cd | 339 | then it goes to the bullet character (for hotspot navigation) |
48bd8440 | 340 | - if the cursor is on the bullet character: |
c07583cd | 341 | then it goes to the first column of that line (the headline) |
48bd8440 EZ |
342 | - if the cursor is on the first column of the headline: |
343 | then it goes to the start of the headline within the item body. | |
344 | ||
345 | In this fashion, you can use the beginning-of-line command to do | |
346 | its normal job and then, when repeated, advance through the | |
347 | entry, cycling back to start. | |
348 | ||
349 | If this configuration variable is nil, then the cursor is just | |
350 | advanced to the beginning of the line and remains there on | |
351 | repeated calls." | |
352 | :type 'boolean :group 'allout) | |
353 | ;;;_ = allout-end-of-line-cycles | |
354 | (defcustom allout-end-of-line-cycles t | |
9201cc28 | 355 | "If non-nil, \\[allout-end-of-line] will cycle through smart-placement options. |
48bd8440 EZ |
356 | |
357 | Cycling only happens on when the command is repeated, not when it | |
358 | follows a different command. | |
359 | ||
60c7bbd2 | 360 | Smart placement means that repeated calls to this function will |
48bd8440 EZ |
361 | advance as follows: |
362 | ||
363 | - if the cursor is not on the end-of-line, | |
364 | then it goes to the end-of-line | |
365 | - if the cursor is on the end-of-line but not the end-of-entry, | |
366 | then it goes to the end-of-entry, exposing it if necessary | |
367 | - if the cursor is on the end-of-entry, | |
368 | then it goes to the end of the head line | |
369 | ||
370 | In this fashion, you can use the end-of-line command to do its | |
371 | normal job and then, when repeated, advance through the entry, | |
372 | cycling back to start. | |
373 | ||
374 | If this configuration variable is nil, then the cursor is just | |
375 | advanced to the end of the line and remains there on repeated | |
376 | calls." | |
377 | :type 'boolean :group 'allout) | |
378 | ||
fd5359c6 MR |
379 | ;;;_ = allout-header-prefix |
380 | (defcustom allout-header-prefix "." | |
48bd8440 EZ |
381 | ;; this string is treated as literal match. it will be `regexp-quote'd, so |
382 | ;; one cannot use regular expressions to match varying header prefixes. | |
9201cc28 | 383 | "Leading string which helps distinguish topic headers. |
19b84ba3 RS |
384 | |
385 | Outline topic header lines are identified by a leading topic | |
c567ac01 | 386 | header prefix, which mostly have the value of this var at their front. |
48bd8440 EZ |
387 | Level 1 topics are exceptions. They consist of only a single |
388 | character, which is typically set to the `allout-primary-bullet'." | |
4bef9110 SE |
389 | :type 'string |
390 | :group 'allout) | |
fd5359c6 | 391 | (make-variable-buffer-local 'allout-header-prefix) |
8570ce80 SM |
392 | ;;;###autoload |
393 | (put 'allout-header-prefix 'safe-local-variable 'stringp) | |
fd5359c6 MR |
394 | ;;;_ = allout-primary-bullet |
395 | (defcustom allout-primary-bullet "*" | |
19b84ba3 RS |
396 | "Bullet used for top-level outline topics. |
397 | ||
398 | Outline topic header lines are identified by a leading topic header | |
c567ac01 | 399 | prefix, which is concluded by bullets that includes the value of this |
fd5359c6 | 400 | var and the respective allout-*-bullets-string vars. |
c567ac01 | 401 | |
353e2ef2 | 402 | The value of an asterisk (`*') provides for backwards compatibility |
e126900f | 403 | with the original Emacs outline mode. See `allout-plain-bullets-string' |
2265e017 | 404 | and `allout-distinctive-bullets-string' for the range of available |
4bef9110 SE |
405 | bullets." |
406 | :type 'string | |
407 | :group 'allout) | |
fd5359c6 | 408 | (make-variable-buffer-local 'allout-primary-bullet) |
8570ce80 SM |
409 | ;;;###autoload |
410 | (put 'allout-primary-bullet 'safe-local-variable 'stringp) | |
fd5359c6 | 411 | ;;;_ = allout-plain-bullets-string |
d82979ea | 412 | (defcustom allout-plain-bullets-string ".," |
9201cc28 | 413 | "The bullets normally used in outline topic prefixes. |
19b84ba3 | 414 | |
fd5359c6 | 415 | See `allout-distinctive-bullets-string' for the other kind of |
c567ac01 | 416 | bullets. |
1977b8f6 | 417 | |
353e2ef2 | 418 | DO NOT include the close-square-bracket, `]', as a bullet. |
1977b8f6 | 419 | |
c567ac01 | 420 | Outline mode has to be reactivated in order for changes to the value |
4bef9110 SE |
421 | of this var to take effect." |
422 | :type 'string | |
423 | :group 'allout) | |
fd5359c6 | 424 | (make-variable-buffer-local 'allout-plain-bullets-string) |
8570ce80 SM |
425 | ;;;###autoload |
426 | (put 'allout-plain-bullets-string 'safe-local-variable 'stringp) | |
fd5359c6 | 427 | ;;;_ = allout-distinctive-bullets-string |
d82979ea | 428 | (defcustom allout-distinctive-bullets-string "*+-=>()[{}&!?#%\"X@$~_\\:;^" |
9201cc28 | 429 | "Persistent outline header bullets used to distinguish special topics. |
1977b8f6 | 430 | |
38b2ca53 NR |
431 | These bullets are distinguish topics with particular character. |
432 | They are not used by default in the topic creation routines, but | |
433 | are offered as options when you modify topic creation with a | |
434 | universal argument \(\\[universal-argument]), or during rebulleting \(\\[allout-rebullet-current-heading]). | |
435 | ||
436 | Distinctive bullets are not cycled when topics are shifted or | |
437 | otherwise automatically rebulleted, so their marking is | |
438 | persistent until deliberately changed. Their significance is | |
439 | purely by convention, however. Some conventions suggest | |
440 | themselves: | |
441 | ||
60c7bbd2 RS |
442 | `(' - open paren -- an aside or incidental point |
443 | `?' - question mark -- uncertain or outright question | |
444 | `!' - exclamation point/bang -- emphatic | |
445 | `[' - open square bracket -- meta-note, about item instead of item's subject | |
446 | `\"' - double quote -- a quotation or other citation | |
447 | `=' - equal sign -- an assignement, equating a name with some connotation | |
448 | `^' - carat -- relates to something above | |
38b2ca53 NR |
449 | |
450 | Some are more elusive, but their rationale may be recognizable: | |
451 | ||
60c7bbd2 RS |
452 | `+' - plus -- pending consideration, completion |
453 | `_' - underscore -- done, completed | |
454 | `&' - ampersand -- addendum, furthermore | |
38b2ca53 NR |
455 | |
456 | \(Some other non-plain bullets have special meaning to the | |
457 | software. By default: | |
458 | ||
60c7bbd2 RS |
459 | `~' marks encryptable topics -- see `allout-topic-encryption-bullet' |
460 | `#' marks auto-numbered bullets -- see `allout-numbered-bullet'.) | |
38b2ca53 NR |
461 | |
462 | See `allout-plain-bullets-string' for the standard, alternating | |
463 | bullets. | |
1977b8f6 | 464 | |
fd5359c6 | 465 | You must run `set-allout-regexp' in order for outline mode to |
38b2ca53 | 466 | adopt changes of this value. |
19b84ba3 | 467 | |
353e2ef2 | 468 | DO NOT include the close-square-bracket, `]', on either of the bullet |
4bef9110 SE |
469 | strings." |
470 | :type 'string | |
471 | :group 'allout) | |
fd5359c6 | 472 | (make-variable-buffer-local 'allout-distinctive-bullets-string) |
8570ce80 SM |
473 | ;;;###autoload |
474 | (put 'allout-distinctive-bullets-string 'safe-local-variable 'stringp) | |
1977b8f6 | 475 | |
fd5359c6 MR |
476 | ;;;_ = allout-use-mode-specific-leader |
477 | (defcustom allout-use-mode-specific-leader t | |
9201cc28 | 478 | "When non-nil, use mode-specific topic-header prefixes. |
19b84ba3 | 479 | |
48bd8440 EZ |
480 | Allout outline mode will use the mode-specific `allout-mode-leaders' or |
481 | comment-start string, if any, to lead the topic prefix string, so topic | |
482 | headers look like comments in the programming language. It will also use | |
483 | the comment-start string, with an '_' appended, for `allout-primary-bullet'. | |
19b84ba3 | 484 | |
48bd8440 EZ |
485 | String values are used as literals, not regular expressions, so |
486 | do not escape any regulare-expression characters. | |
19b84ba3 | 487 | |
24190da5 | 488 | Value t means to first check for assoc value in `allout-mode-leaders' |
c07583cd | 489 | alist, then use comment-start string, if any, then use default (`.'). |
9179616f | 490 | \(See note about use of comment-start strings, below.) |
19b84ba3 | 491 | |
fd5359c6 | 492 | Set to the symbol for either of `allout-mode-leaders' or |
19b84ba3 RS |
493 | `comment-start' to use only one of them, respectively. |
494 | ||
c07583cd | 495 | Value nil means to always use the default (`.') and leave |
48bd8440 EZ |
496 | `allout-primary-bullet' unaltered. |
497 | ||
498 | comment-start strings that do not end in spaces are tripled in | |
499 | the header-prefix, and an `_' underscore is tacked on the end, to | |
500 | distinguish them from regular comment strings. comment-start | |
501 | strings that do end in spaces are not tripled, but an underscore | |
75bcb912 | 502 | is substituted for the space. [This presumes that the space is |
48bd8440 EZ |
503 | for appearance, not comment syntax. You can use |
504 | `allout-mode-leaders' to override this behavior, when | |
505 | undesired.]" | |
71296446 | 506 | :type '(choice (const t) (const nil) string |
fd5359c6 | 507 | (const allout-mode-leaders) |
4bef9110 SE |
508 | (const comment-start)) |
509 | :group 'allout) | |
8570ce80 SM |
510 | ;;;###autoload |
511 | (put 'allout-use-mode-specific-leader 'safe-local-variable | |
da506f05 RS |
512 | '(lambda (x) (or (memq x '(t nil allout-mode-leaders comment-start)) |
513 | (stringp x)))) | |
fd5359c6 MR |
514 | ;;;_ = allout-mode-leaders |
515 | (defvar allout-mode-leaders '() | |
516 | "Specific allout-prefix leading strings per major modes. | |
19b84ba3 | 517 | |
48bd8440 EZ |
518 | Use this if the mode's comment-start string isn't what you |
519 | prefer, or if the mode lacks a comment-start string. See | |
520 | `allout-use-mode-specific-leader' for more details. | |
19b84ba3 RS |
521 | |
522 | If you're constructing a string that will comment-out outline | |
523 | structuring so it can be included in program code, append an extra | |
524 | character, like an \"_\" underscore, to distinguish the lead string | |
48bd8440 | 525 | from regular comments that start at the beginning-of-line.") |
19b84ba3 | 526 | |
fd5359c6 MR |
527 | ;;;_ = allout-old-style-prefixes |
528 | (defcustom allout-old-style-prefixes nil | |
9201cc28 | 529 | "When non-nil, use only old-and-crusty `outline-mode' `*' topic prefixes. |
19b84ba3 RS |
530 | |
531 | Non-nil restricts the topic creation and modification | |
c567ac01 | 532 | functions to asterix-padded prefixes, so they look exactly |
e126900f | 533 | like the original Emacs-outline style prefixes. |
1977b8f6 | 534 | |
c567ac01 | 535 | Whatever the setting of this variable, both old and new style prefixes |
4bef9110 SE |
536 | are always respected by the topic maneuvering functions." |
537 | :type 'boolean | |
538 | :group 'allout) | |
fd5359c6 | 539 | (make-variable-buffer-local 'allout-old-style-prefixes) |
8570ce80 SM |
540 | ;;;###autoload |
541 | (put 'allout-old-style-prefixes 'safe-local-variable | |
64686e6d | 542 | (if (fboundp 'booleanp) 'booleanp '(lambda (x) (member x '(t nil))))) |
60c7bbd2 | 543 | ;;;_ = allout-stylish-prefixes -- alternating bullets |
fd5359c6 | 544 | (defcustom allout-stylish-prefixes t |
9201cc28 | 545 | "Do fancy stuff with topic prefix bullets according to level, etc. |
c567ac01 | 546 | |
19b84ba3 RS |
547 | Non-nil enables topic creation, modification, and repositioning |
548 | functions to vary the topic bullet char (the char that marks the topic | |
549 | depth) just preceding the start of the topic text) according to level. | |
353e2ef2 | 550 | Otherwise, only asterisks (`*') and distinctive bullets are used. |
19b84ba3 RS |
551 | |
552 | This is how an outline can look (but sans indentation) with stylish | |
553 | prefixes: | |
c567ac01 RS |
554 | |
555 | * Top level | |
556 | .* A topic | |
557 | . + One level 3 subtopic | |
558 | . . One level 4 subtopic | |
19b84ba3 | 559 | . . A second 4 subtopic |
c567ac01 | 560 | . + Another level 3 subtopic |
19b84ba3 RS |
561 | . #1 A numbered level 4 subtopic |
562 | . #2 Another | |
563 | . ! Another level 4 subtopic with a different distinctive bullet | |
564 | . #4 And another numbered level 4 subtopic | |
c567ac01 | 565 | |
19b84ba3 RS |
566 | This would be an outline with stylish prefixes inhibited (but the |
567 | numbered and other distinctive bullets retained): | |
c567ac01 RS |
568 | |
569 | * Top level | |
570 | .* A topic | |
19b84ba3 RS |
571 | . * One level 3 subtopic |
572 | . * One level 4 subtopic | |
573 | . * A second 4 subtopic | |
574 | . * Another level 3 subtopic | |
575 | . #1 A numbered level 4 subtopic | |
576 | . #2 Another | |
577 | . ! Another level 4 subtopic with a different distinctive bullet | |
578 | . #4 And another numbered level 4 subtopic | |
c567ac01 RS |
579 | |
580 | Stylish and constant prefixes (as well as old-style prefixes) are | |
581 | always respected by the topic maneuvering functions, regardless of | |
582 | this variable setting. | |
583 | ||
2265e017 | 584 | The setting of this var is not relevant when `allout-old-style-prefixes' |
4bef9110 SE |
585 | is non-nil." |
586 | :type 'boolean | |
587 | :group 'allout) | |
fd5359c6 | 588 | (make-variable-buffer-local 'allout-stylish-prefixes) |
8570ce80 SM |
589 | ;;;###autoload |
590 | (put 'allout-stylish-prefixes 'safe-local-variable | |
64686e6d | 591 | (if (fboundp 'booleanp) 'booleanp '(lambda (x) (member x '(t nil))))) |
1977b8f6 | 592 | |
fd5359c6 MR |
593 | ;;;_ = allout-numbered-bullet |
594 | (defcustom allout-numbered-bullet "#" | |
9201cc28 | 595 | "String designating bullet of topics that have auto-numbering; nil for none. |
19b84ba3 | 596 | |
a0776d6b | 597 | Topics having this bullet have automatic maintenance of a sibling |
19b84ba3 | 598 | sequence-number tacked on, just after the bullet. Conventionally set |
c567ac01 | 599 | to \"#\", you can set it to a bullet of your choice. A nil value |
4bef9110 SE |
600 | disables numbering maintenance." |
601 | :type '(choice (const nil) string) | |
602 | :group 'allout) | |
fd5359c6 | 603 | (make-variable-buffer-local 'allout-numbered-bullet) |
8570ce80 | 604 | ;;;###autoload |
64686e6d RS |
605 | (put 'allout-numbered-bullet 'safe-local-variable |
606 | (if (fboundp 'string-or-null-p) | |
607 | 'string-or-null-p | |
608 | '(lambda (x) (or (stringp x) (null x))))) | |
fd5359c6 MR |
609 | ;;;_ = allout-file-xref-bullet |
610 | (defcustom allout-file-xref-bullet "@" | |
9201cc28 | 611 | "Bullet signifying file cross-references, for `allout-resolve-xref'. |
19b84ba3 | 612 | |
9179616f | 613 | Set this var to the bullet you want to use for file cross-references." |
4bef9110 SE |
614 | :type '(choice (const nil) string) |
615 | :group 'allout) | |
8570ce80 | 616 | ;;;###autoload |
64686e6d RS |
617 | (put 'allout-file-xref-bullet 'safe-local-variable |
618 | (if (fboundp 'string-or-null-p) | |
619 | 'string-or-null-p | |
620 | '(lambda (x) (or (stringp x) (null x))))) | |
fd5359c6 MR |
621 | ;;;_ = allout-presentation-padding |
622 | (defcustom allout-presentation-padding 2 | |
9201cc28 | 623 | "Presentation-format white-space padding factor, for greater indent." |
9179616f DL |
624 | :type 'integer |
625 | :group 'allout) | |
626 | ||
fd5359c6 | 627 | (make-variable-buffer-local 'allout-presentation-padding) |
8570ce80 SM |
628 | ;;;###autoload |
629 | (put 'allout-presentation-padding 'safe-local-variable 'integerp) | |
9179616f | 630 | |
fd5359c6 MR |
631 | ;;;_ = allout-abbreviate-flattened-numbering |
632 | (defcustom allout-abbreviate-flattened-numbering nil | |
9201cc28 | 633 | "If non-nil, `allout-flatten-exposed-to-buffer' abbreviates topic |
9179616f DL |
634 | numbers to minimal amount with some context. Otherwise, entire |
635 | numbers are always used." | |
636 | :type 'boolean | |
637 | :group 'allout) | |
638 | ||
c567ac01 | 639 | ;;;_ + LaTeX formatting |
fd5359c6 MR |
640 | ;;;_ - allout-number-pages |
641 | (defcustom allout-number-pages nil | |
9201cc28 | 642 | "Non-nil turns on page numbering for LaTeX formatting of an outline." |
4bef9110 SE |
643 | :type 'boolean |
644 | :group 'allout) | |
fd5359c6 MR |
645 | ;;;_ - allout-label-style |
646 | (defcustom allout-label-style "\\large\\bf" | |
9201cc28 | 647 | "Font and size of labels for LaTeX formatting of an outline." |
4bef9110 SE |
648 | :type 'string |
649 | :group 'allout) | |
fd5359c6 MR |
650 | ;;;_ - allout-head-line-style |
651 | (defcustom allout-head-line-style "\\large\\sl " | |
9201cc28 | 652 | "Font and size of entries for LaTeX formatting of an outline." |
4bef9110 SE |
653 | :type 'string |
654 | :group 'allout) | |
fd5359c6 MR |
655 | ;;;_ - allout-body-line-style |
656 | (defcustom allout-body-line-style " " | |
9201cc28 | 657 | "Font and size of entries for LaTeX formatting of an outline." |
4bef9110 SE |
658 | :type 'string |
659 | :group 'allout) | |
fd5359c6 MR |
660 | ;;;_ - allout-title-style |
661 | (defcustom allout-title-style "\\Large\\bf" | |
9201cc28 | 662 | "Font and size of titles for LaTeX formatting of an outline." |
4bef9110 SE |
663 | :type 'string |
664 | :group 'allout) | |
fd5359c6 | 665 | ;;;_ - allout-title |
d82979ea | 666 | (defcustom allout-title '(or buffer-file-name (buffer-name)) |
9201cc28 | 667 | "Expression to be evaluated to determine the title for LaTeX |
4bef9110 SE |
668 | formatted copy." |
669 | :type 'sexp | |
670 | :group 'allout) | |
fd5359c6 MR |
671 | ;;;_ - allout-line-skip |
672 | (defcustom allout-line-skip ".05cm" | |
9201cc28 | 673 | "Space between lines for LaTeX formatting of an outline." |
4bef9110 SE |
674 | :type 'string |
675 | :group 'allout) | |
fd5359c6 MR |
676 | ;;;_ - allout-indent |
677 | (defcustom allout-indent ".3cm" | |
9201cc28 | 678 | "LaTeX formatted depth-indent spacing." |
4bef9110 SE |
679 | :type 'string |
680 | :group 'allout) | |
c567ac01 | 681 | |
d82979ea | 682 | ;;;_ + Topic encryption |
8570ce80 SM |
683 | ;;;_ = allout-encryption group |
684 | (defgroup allout-encryption nil | |
685 | "Settings for topic encryption features of allout outliner." | |
686 | :group 'allout) | |
d82979ea EZ |
687 | ;;;_ = allout-topic-encryption-bullet |
688 | (defcustom allout-topic-encryption-bullet "~" | |
9201cc28 | 689 | "Bullet signifying encryption of the entry's body." |
d82979ea | 690 | :type '(choice (const nil) string) |
21e8bc99 | 691 | :version "22.1" |
8570ce80 | 692 | :group 'allout-encryption) |
0949617b EZ |
693 | ;;;_ = allout-passphrase-verifier-handling |
694 | (defcustom allout-passphrase-verifier-handling t | |
9201cc28 | 695 | "Enable use of symmetric encryption passphrase verifier if non-nil. |
d82979ea EZ |
696 | |
697 | See the docstring for the `allout-enable-file-variable-adjustment' | |
698 | variable for details about allout ajustment of file variables." | |
0949617b | 699 | :type 'boolean |
21e8bc99 | 700 | :version "22.1" |
8570ce80 | 701 | :group 'allout-encryption) |
0949617b EZ |
702 | (make-variable-buffer-local 'allout-passphrase-verifier-handling) |
703 | ;;;_ = allout-passphrase-hint-handling | |
704 | (defcustom allout-passphrase-hint-handling 'always | |
9201cc28 | 705 | "Dictate outline encryption passphrase reminder handling: |
d82979ea | 706 | |
60c7bbd2 RS |
707 | always -- always show reminder when prompting |
708 | needed -- show reminder on passphrase entry failure | |
709 | disabled -- never present or adjust reminder | |
d82979ea EZ |
710 | |
711 | See the docstring for the `allout-enable-file-variable-adjustment' | |
712 | variable for details about allout ajustment of file variables." | |
713 | :type '(choice (const always) | |
714 | (const needed) | |
d82979ea | 715 | (const disabled)) |
21e8bc99 | 716 | :version "22.1" |
8570ce80 | 717 | :group 'allout-encryption) |
0949617b | 718 | (make-variable-buffer-local 'allout-passphrase-hint-handling) |
d82979ea | 719 | ;;;_ = allout-encrypt-unencrypted-on-saves |
96b83743 | 720 | (defcustom allout-encrypt-unencrypted-on-saves t |
9201cc28 | 721 | "When saving, should topics pending encryption be encrypted? |
d82979ea EZ |
722 | |
723 | The idea is to prevent file-system exposure of any un-encrypted stuff, and | |
724 | mostly covers both deliberate file writes and auto-saves. | |
725 | ||
726 | - Yes: encrypt all topics pending encryption, even if it's the one | |
c07583cd | 727 | currently being edited. (In that case, the currently edited topic |
d82979ea EZ |
728 | will be automatically decrypted before any user interaction, so they |
729 | can continue editing but the copy on the file system will be | |
730 | encrypted.) | |
731 | Auto-saves will use the \"All except current topic\" mode if this | |
60c7bbd2 | 732 | one is selected, to avoid practical difficulties -- see below. |
d82979ea EZ |
733 | - All except current topic: skip the topic currently being edited, even if |
734 | it's pending encryption. This may expose the current topic on the | |
735 | file sytem, but avoids the nuisance of prompts for the encryption | |
0949617b | 736 | passphrase in the middle of editing for, eg, autosaves. |
d82979ea EZ |
737 | This mode is used for auto-saves for both this option and \"Yes\". |
738 | - No: leave it to the user to encrypt any unencrypted topics. | |
739 | ||
740 | For practical reasons, auto-saves always use the 'except-current policy | |
c07583cd | 741 | when auto-encryption is enabled. (Otherwise, spurious passphrase prompts |
0949617b EZ |
742 | and unavoidable timing collisions are too disruptive.) If security for a |
743 | file requires that even the current topic is never auto-saved in the clear, | |
d82979ea EZ |
744 | disable auto-saves for that file." |
745 | ||
746 | :type '(choice (const :tag "Yes" t) | |
747 | (const :tag "All except current topic" except-current) | |
748 | (const :tag "No" nil)) | |
21e8bc99 | 749 | :version "22.1" |
8570ce80 | 750 | :group 'allout-encryption) |
d82979ea EZ |
751 | (make-variable-buffer-local 'allout-encrypt-unencrypted-on-saves) |
752 | ||
01fc9422 EZ |
753 | ;;;_ + Developer |
754 | ;;;_ = allout-developer group | |
755 | (defgroup allout-developer nil | |
756 | "Settings for topic encryption features of allout outliner." | |
757 | :group 'allout) | |
758 | ;;;_ = allout-run-unit-tests-on-load | |
759 | (defcustom allout-run-unit-tests-on-load nil | |
9201cc28 | 760 | "When non-nil, unit tests will be run at end of loading the allout module. |
01fc9422 EZ |
761 | |
762 | Generally, allout code developers are the only ones who'll want to set this. | |
763 | ||
764 | \(If set, this makes it an even better practice to exercise changes by | |
5204800f NR |
765 | doing byte-compilation with a repeat count, so the file is loaded after |
766 | compilation.) | |
01fc9422 EZ |
767 | |
768 | See `allout-run-unit-tests' to see what's run." | |
769 | :type 'boolean | |
770 | :group 'allout-developer) | |
771 | ||
c567ac01 RS |
772 | ;;;_ + Miscellaneous customization |
773 | ||
d82979ea EZ |
774 | ;;;_ = allout-enable-file-variable-adjustment |
775 | (defcustom allout-enable-file-variable-adjustment t | |
9201cc28 | 776 | "If non-nil, some allout outline actions edit Emacs local file var text. |
d82979ea EZ |
777 | |
778 | This can range from changes to existing entries, addition of new ones, | |
779 | and creation of a new local variables section when necessary. | |
19b84ba3 | 780 | |
d82979ea EZ |
781 | Emacs file variables adjustments are also inhibited if `enable-local-variables' |
782 | is nil. | |
c567ac01 | 783 | |
d82979ea | 784 | Operations potentially causing edits include allout encryption routines. |
69027dea | 785 | For details, see `allout-toggle-current-subtree-encryption's docstring." |
4bef9110 SE |
786 | :type 'boolean |
787 | :group 'allout) | |
d82979ea | 788 | (make-variable-buffer-local 'allout-enable-file-variable-adjustment) |
c567ac01 | 789 | |
60c7bbd2 | 790 | ;;;_* CODE -- no user customizations below. |
c567ac01 | 791 | |
9179616f DL |
792 | ;;;_ #1 Internal Outline Formatting and Configuration |
793 | ;;;_ : Version | |
fd5359c6 | 794 | ;;;_ = allout-version |
4034b0e2 | 795 | (defvar allout-version "2.2.1" |
c07583cd | 796 | "Version of currently loaded outline package. (allout.el)") |
fd5359c6 MR |
797 | ;;;_ > allout-version |
798 | (defun allout-version (&optional here) | |
19b84ba3 RS |
799 | "Return string describing the loaded outline version." |
800 | (interactive "P") | |
fd5359c6 | 801 | (let ((msg (concat "Allout Outline Mode v " allout-version))) |
eac9cf5f | 802 | (if here (insert msg)) |
19b84ba3 RS |
803 | (message "%s" msg) |
804 | msg)) | |
69027dea EZ |
805 | ;;;_ : Mode activation (defined here because it's referenced early) |
806 | ;;;_ = allout-mode | |
807 | (defvar allout-mode nil "Allout outline mode minor-mode flag.") | |
808 | (make-variable-buffer-local 'allout-mode) | |
4034b0e2 | 809 | ;;;_ = allout-layout nil |
60c7bbd2 | 810 | (defvar allout-layout nil ; LEAVE GLOBAL VALUE NIL -- see docstring. |
4034b0e2 LT |
811 | "Buffer-specific setting for allout layout. |
812 | ||
c07583cd | 813 | In buffers where this is non-nil (and if `allout-init' has been run, to |
4034b0e2 LT |
814 | enable this behavior), `allout-mode' will be automatically activated. The |
815 | layout dictated by the value will be used to set the initial exposure when | |
816 | `allout-mode' is activated. | |
817 | ||
818 | \*You should not setq-default this variable non-nil unless you want every | |
819 | visited file to be treated as an allout file.* | |
820 | ||
821 | The value would typically be set by a file local variable. For | |
822 | example, the following lines at the bottom of an Emacs Lisp file: | |
823 | ||
824 | ;;;Local variables: | |
c07583cd | 825 | ;;;allout-layout: (0 : -1 -1 0) |
4034b0e2 LT |
826 | ;;;End: |
827 | ||
828 | dictate activation of `allout-mode' mode when the file is visited | |
829 | \(presuming allout-init was already run), followed by the | |
c07583cd | 830 | equivalent of `(allout-expose-topic 0 : -1 -1 0)'. (This is |
4034b0e2 LT |
831 | the layout used for the allout.el source file.) |
832 | ||
833 | `allout-default-layout' describes the specification format. | |
834 | `allout-layout' can additionally have the value `t', in which | |
835 | case the value of `allout-default-layout' is used.") | |
836 | (make-variable-buffer-local 'allout-layout) | |
b1a569b5 | 837 | ;;;###autoload |
8570ce80 | 838 | (put 'allout-layout 'safe-local-variable |
da506f05 | 839 | '(lambda (x) (or (numberp x) (listp x) (memq x '(: * + -))))) |
4034b0e2 | 840 | |
9179616f | 841 | ;;;_ : Topic header format |
fd5359c6 MR |
842 | ;;;_ = allout-regexp |
843 | (defvar allout-regexp "" | |
c567ac01 | 844 | "*Regular expression to match the beginning of a heading line. |
19b84ba3 | 845 | |
c567ac01 RS |
846 | Any line whose beginning matches this regexp is considered a |
847 | heading. This var is set according to the user configuration vars | |
2265e017 | 848 | by `set-allout-regexp'.") |
fd5359c6 MR |
849 | (make-variable-buffer-local 'allout-regexp) |
850 | ;;;_ = allout-bullets-string | |
851 | (defvar allout-bullets-string "" | |
19b84ba3 RS |
852 | "A string dictating the valid set of outline topic bullets. |
853 | ||
60c7bbd2 | 854 | This var should *not* be set by the user -- it is set by `set-allout-regexp', |
fd5359c6 MR |
855 | and is produced from the elements of `allout-plain-bullets-string' |
856 | and `allout-distinctive-bullets-string'.") | |
857 | (make-variable-buffer-local 'allout-bullets-string) | |
858 | ;;;_ = allout-bullets-string-len | |
859 | (defvar allout-bullets-string-len 0 | |
2265e017 | 860 | "Length of current buffers' `allout-plain-bullets-string'.") |
fd5359c6 | 861 | (make-variable-buffer-local 'allout-bullets-string-len) |
d8350998 CY |
862 | ;;;_ = allout-depth-specific-regexp |
863 | (defvar allout-depth-specific-regexp "" | |
864 | "*Regular expression to match a heading line prefix for a particular depth. | |
865 | ||
866 | This expression is used to search for depth-specific topic | |
867 | headers at depth 2 and greater. Use `allout-depth-one-regexp' | |
868 | for to seek topics at depth one. | |
869 | ||
870 | This var is set according to the user configuration vars by | |
871 | `set-allout-regexp'. It is prepared with format strings for two | |
872 | decimal numbers, which should each be one less than the depth of the | |
873 | topic prefix to be matched.") | |
874 | (make-variable-buffer-local 'allout-depth-specific-regexp) | |
875 | ;;;_ = allout-depth-one-regexp | |
876 | (defvar allout-depth-one-regexp "" | |
877 | "*Regular expression to match a heading line prefix for depth one. | |
878 | ||
879 | This var is set according to the user configuration vars by | |
880 | `set-allout-regexp'. It is prepared with format strings for two | |
881 | decimal numbers, which should each be one less than the depth of the | |
882 | topic prefix to be matched.") | |
883 | (make-variable-buffer-local 'allout-depth-one-regexp) | |
fd5359c6 MR |
884 | ;;;_ = allout-line-boundary-regexp |
885 | (defvar allout-line-boundary-regexp () | |
42c3e51c | 886 | "`allout-regexp' prepended with a newline for the search target. |
19b84ba3 | 887 | |
d8350998 | 888 | This is properly set by `set-allout-regexp'.") |
fd5359c6 MR |
889 | (make-variable-buffer-local 'allout-line-boundary-regexp) |
890 | ;;;_ = allout-bob-regexp | |
891 | (defvar allout-bob-regexp () | |
d8350998 | 892 | "Like `allout-line-boundary-regexp', for headers at beginning of buffer.") |
fd5359c6 MR |
893 | (make-variable-buffer-local 'allout-bob-regexp) |
894 | ;;;_ = allout-header-subtraction | |
895 | (defvar allout-header-subtraction (1- (length allout-header-prefix)) | |
896 | "Allout-header prefix length to subtract when computing topic depth.") | |
897 | (make-variable-buffer-local 'allout-header-subtraction) | |
898 | ;;;_ = allout-plain-bullets-string-len | |
899 | (defvar allout-plain-bullets-string-len (length allout-plain-bullets-string) | |
2265e017 | 900 | "Length of `allout-plain-bullets-string', updated by `set-allout-regexp'.") |
fd5359c6 MR |
901 | (make-variable-buffer-local 'allout-plain-bullets-string-len) |
902 | ||
d8350998 | 903 | ;;;_ = allout-doublecheck-at-and-shallower |
2a1408fd | 904 | (defconst allout-doublecheck-at-and-shallower 3 |
ede4ac6a | 905 | "Validate apparent topics of this depth and shallower as being non-aberrant. |
fd5359c6 | 906 | |
2a1408fd DN |
907 | Verified with `allout-aberrant-container-p'. The usefulness of |
908 | this check is limited to shallow depths, because the | |
909 | determination of aberrance is according to the mistaken item | |
910 | being followed by a legitimate item of excessively greater depth. | |
911 | ||
912 | The classic example of a mistaken item, for a standard allout | |
913 | outline configuration, is a body line that begins with an '...' | |
914 | ellipsis. This happens to contain a legitimate depth-2 header | |
915 | prefix, constituted by two '..' dots at the beginning of the | |
916 | line. The only thing that can distinguish it *in principle* from | |
917 | a legitimate one is if the following real header is at a depth | |
918 | that is discontinuous from the depth of 2 implied by the | |
919 | ellipsis, ie depth 4 or more. As the depth being tested gets | |
920 | greater, the likelihood of this kind of disqualification is | |
921 | lower, and the usefulness of this test is lower. | |
922 | ||
923 | Extending the depth of the doublecheck increases the amount it is | |
924 | applied, increasing the cost of the test - on casual estimation, | |
925 | for outlines with many deep topics, geometrically (O(n)?). | |
926 | Taken together with decreasing likelihood that the test will be | |
927 | useful at greater depths, more modest doublecheck limits are more | |
928 | suitably economical.") | |
fd5359c6 MR |
929 | ;;;_ X allout-reset-header-lead (header-lead) |
930 | (defun allout-reset-header-lead (header-lead) | |
9201cc28 | 931 | "Reset the leading string used to identify topic headers." |
1977b8f6 | 932 | (interactive "sNew lead string: ") |
fd5359c6 MR |
933 | (setq allout-header-prefix header-lead) |
934 | (setq allout-header-subtraction (1- (length allout-header-prefix))) | |
935 | (set-allout-regexp)) | |
936 | ;;;_ X allout-lead-with-comment-string (header-lead) | |
937 | (defun allout-lead-with-comment-string (&optional header-lead) | |
9201cc28 | 938 | "Set the topic-header leading string to specified string. |
19b84ba3 RS |
939 | |
940 | Useful when for encapsulating outline structure in programming | |
941 | language comments. Returns the leading string." | |
1977b8f6 RS |
942 | |
943 | (interactive "P") | |
944 | (if (not (stringp header-lead)) | |
945 | (setq header-lead (read-string | |
946 | "String prefix for topic headers: "))) | |
fd5359c6 MR |
947 | (setq allout-reindent-bodies nil) |
948 | (allout-reset-header-lead header-lead) | |
1977b8f6 | 949 | header-lead) |
48bd8440 EZ |
950 | ;;;_ > allout-infer-header-lead-and-primary-bullet () |
951 | (defun allout-infer-header-lead-and-primary-bullet () | |
952 | "Determine appropriate `allout-header-prefix' and `allout-primary-bullet'. | |
19b84ba3 RS |
953 | |
954 | Works according to settings of: | |
955 | ||
8d118843 | 956 | `comment-start' |
fd5359c6 MR |
957 | `allout-header-prefix' (default) |
958 | `allout-use-mode-specific-leader' | |
959 | and `allout-mode-leaders'. | |
19b84ba3 | 960 | |
c07583cd | 961 | Apply this via (re)activation of `allout-mode', rather than |
8d118843 | 962 | invoking it directly." |
fd5359c6 MR |
963 | (let* ((use-leader (and (boundp 'allout-use-mode-specific-leader) |
964 | (if (or (stringp allout-use-mode-specific-leader) | |
965 | (memq allout-use-mode-specific-leader | |
966 | '(allout-mode-leaders | |
19b84ba3 RS |
967 | comment-start |
968 | t))) | |
fd5359c6 | 969 | allout-use-mode-specific-leader |
60c7bbd2 | 970 | ;; Oops -- garbled value, equate with effect of t: |
19b84ba3 RS |
971 | t))) |
972 | (leader | |
973 | (cond | |
974 | ((not use-leader) nil) | |
975 | ;; Use the explicitly designated leader: | |
976 | ((stringp use-leader) use-leader) | |
fd5359c6 | 977 | (t (or (and (memq use-leader '(t allout-mode-leaders)) |
19b84ba3 | 978 | ;; Get it from outline mode leaders? |
fd5359c6 MR |
979 | (cdr (assq major-mode allout-mode-leaders))) |
980 | ;; ... didn't get from allout-mode-leaders... | |
19b84ba3 RS |
981 | (and (memq use-leader '(t comment-start)) |
982 | comment-start | |
983 | ;; Use comment-start, maybe tripled, and with | |
353e2ef2 | 984 | ;; underscore: |
19b84ba3 RS |
985 | (concat |
986 | (if (string= " " | |
987 | (substring comment-start | |
988 | (1- (length comment-start)))) | |
989 | ;; Use comment-start, sans trailing space: | |
990 | (substring comment-start 0 -1) | |
991 | (concat comment-start comment-start comment-start)) | |
992 | ;; ... and append underscore, whichever: | |
993 | "_"))))))) | |
994 | (if (not leader) | |
995 | nil | |
48bd8440 EZ |
996 | (setq allout-header-prefix leader) |
997 | (if (not allout-old-style-prefixes) | |
ea18b2b1 | 998 | ;; setting allout-primary-bullet makes the top level topics use -- |
60c7bbd2 | 999 | ;; actually, be -- the special prefix: |
48bd8440 EZ |
1000 | (setq allout-primary-bullet leader)) |
1001 | allout-header-prefix))) | |
1002 | (defalias 'allout-infer-header-lead | |
1003 | 'allout-infer-header-lead-and-primary-bullet) | |
fd5359c6 MR |
1004 | ;;;_ > allout-infer-body-reindent () |
1005 | (defun allout-infer-body-reindent () | |
1006 | "Determine proper setting for `allout-reindent-bodies'. | |
8d118843 | 1007 | |
c07583cd | 1008 | Depends on default setting of `allout-reindent-bodies' (which see) |
8d118843 RS |
1009 | and presence of setting for `comment-start', to tell whether the |
1010 | file is programming code." | |
fd5359c6 | 1011 | (if (and allout-reindent-bodies |
8d118843 | 1012 | comment-start |
fd5359c6 MR |
1013 | (not (eq 'force allout-reindent-bodies))) |
1014 | (setq allout-reindent-bodies nil))) | |
1015 | ;;;_ > set-allout-regexp () | |
1016 | (defun set-allout-regexp () | |
19b84ba3 RS |
1017 | "Generate proper topic-header regexp form for outline functions. |
1018 | ||
fd5359c6 | 1019 | Works with respect to `allout-plain-bullets-string' and |
d8350998 CY |
1020 | `allout-distinctive-bullets-string'. |
1021 | ||
1022 | Also refresh various data structures that hinge on the regexp." | |
1977b8f6 RS |
1023 | |
1024 | (interactive) | |
fd5359c6 MR |
1025 | ;; Derive allout-bullets-string from user configured components: |
1026 | (setq allout-bullets-string "") | |
1027 | (let ((strings (list 'allout-plain-bullets-string | |
1028 | 'allout-distinctive-bullets-string | |
1029 | 'allout-primary-bullet)) | |
1977b8f6 RS |
1030 | cur-string |
1031 | cur-len | |
c567ac01 | 1032 | cur-char |
96b83743 | 1033 | index) |
1977b8f6 | 1034 | (while strings |
96b83743 | 1035 | (setq index 0) |
1977b8f6 RS |
1036 | (setq cur-len (length (setq cur-string (symbol-value (car strings))))) |
1037 | (while (< index cur-len) | |
1038 | (setq cur-char (aref cur-string index)) | |
fd5359c6 MR |
1039 | (setq allout-bullets-string |
1040 | (concat allout-bullets-string | |
1977b8f6 RS |
1041 | (cond |
1042 | ; Single dash would denote a | |
1043 | ; sequence, repeated denotes | |
1044 | ; a dash: | |
1045 | ((eq cur-char ?-) "--") | |
1046 | ; literal close-square-bracket | |
1047 | ; doesn't work right in the | |
1048 | ; expr, exclude it: | |
1049 | ((eq cur-char ?\]) "") | |
1050 | (t (regexp-quote (char-to-string cur-char)))))) | |
1051 | (setq index (1+ index))) | |
1052 | (setq strings (cdr strings))) | |
1053 | ) | |
fd5359c6 MR |
1054 | ;; Derive next for repeated use in allout-pending-bullet: |
1055 | (setq allout-plain-bullets-string-len (length allout-plain-bullets-string)) | |
1056 | (setq allout-header-subtraction (1- (length allout-header-prefix))) | |
d8350998 | 1057 | |
42c3e51c | 1058 | (let (new-part old-part formfeed-part) |
d8350998 CY |
1059 | (setq new-part (concat "\\(" |
1060 | (regexp-quote allout-header-prefix) | |
1061 | "[ \t]*" | |
1062 | ;; already regexp-quoted in a custom way: | |
1063 | "[" allout-bullets-string "]" | |
1064 | "\\)") | |
1065 | old-part (concat "\\(" | |
1066 | (regexp-quote allout-primary-bullet) | |
1067 | "\\|" | |
1068 | (regexp-quote allout-header-prefix) | |
1069 | "\\)" | |
1070 | "+" | |
1071 | " ?[^" allout-primary-bullet "]") | |
42c3e51c KM |
1072 | formfeed-part "\\(\^L\\)" |
1073 | ||
d8350998 CY |
1074 | allout-regexp (concat new-part |
1075 | "\\|" | |
1076 | old-part | |
42c3e51c KM |
1077 | "\\|" |
1078 | formfeed-part) | |
d8350998 CY |
1079 | |
1080 | allout-line-boundary-regexp (concat "\n" new-part | |
1081 | "\\|" | |
42c3e51c KM |
1082 | "\n" old-part |
1083 | "\\|" | |
1084 | "\n" formfeed-part) | |
d8350998 CY |
1085 | |
1086 | allout-bob-regexp (concat "\\`" new-part | |
1087 | "\\|" | |
42c3e51c KM |
1088 | "\\`" old-part |
1089 | "\\|" | |
1090 | "\\`" formfeed-part | |
1091 | )) | |
d8350998 CY |
1092 | |
1093 | (setq allout-depth-specific-regexp | |
1094 | (concat "\\(^\\|\\`\\)" | |
1095 | "\\(" | |
1096 | ||
1097 | ;; new-style spacers-then-bullet string: | |
1098 | "\\(" | |
1099 | (allout-format-quote (regexp-quote allout-header-prefix)) | |
1100 | " \\{%s\\}" | |
1101 | "[" (allout-format-quote allout-bullets-string) "]" | |
1102 | "\\)" | |
1103 | ||
1104 | ;; old-style all-bullets string, if primary not multi-char: | |
1105 | (if (< 0 allout-header-subtraction) | |
1106 | "" | |
1107 | (concat "\\|\\(" | |
1108 | (allout-format-quote | |
1109 | (regexp-quote allout-primary-bullet)) | |
1110 | (allout-format-quote | |
1111 | (regexp-quote allout-primary-bullet)) | |
1112 | (allout-format-quote | |
1113 | (regexp-quote allout-primary-bullet)) | |
1114 | "\\{%s\\}" | |
1115 | ;; disqualify greater depths: | |
1116 | "[^" | |
1117 | (allout-format-quote allout-primary-bullet) | |
1118 | "]\\)" | |
1119 | )) | |
1120 | "\\)" | |
1121 | )) | |
1122 | (setq allout-depth-one-regexp | |
1123 | (concat "\\(^\\|\\`\\)" | |
1124 | "\\(" | |
1125 | ||
1126 | "\\(" | |
1127 | (regexp-quote allout-header-prefix) | |
1128 | ;; disqualify any bullet char following any amount of | |
1129 | ;; intervening whitespace: | |
1130 | " *" | |
1131 | (concat "[^ " allout-bullets-string "]") | |
1132 | "\\)" | |
1133 | (if (< 0 allout-header-subtraction) | |
1134 | ;; Need not support anything like the old | |
1135 | ;; bullet style if the prefix is multi-char. | |
1136 | "" | |
1137 | (concat "\\|" | |
1138 | (regexp-quote allout-primary-bullet) | |
1139 | ;; disqualify deeper primary-bullet sequences: | |
1140 | "[^" allout-primary-bullet "]")) | |
1141 | "\\)" | |
1142 | )))) | |
9179616f | 1143 | ;;;_ : Key bindings |
fd5359c6 MR |
1144 | ;;;_ = allout-mode-map |
1145 | (defvar allout-mode-map nil "Keybindings for (allout) outline minor mode.") | |
1146 | ;;;_ > produce-allout-mode-map (keymap-alist &optional base-map) | |
1147 | (defun produce-allout-mode-map (keymap-list &optional base-map) | |
75bcb912 | 1148 | "Produce keymap for use as `allout-mode-map', from KEYMAP-LIST. |
19b84ba3 RS |
1149 | |
1150 | Built on top of optional BASE-MAP, or empty sparse map if none specified. | |
75bcb912 | 1151 | See doc string for `allout-keybindings-list' for format of binding list." |
9179616f | 1152 | (let ((map (or base-map (make-sparse-keymap))) |
fd5359c6 | 1153 | (pref (list allout-command-prefix))) |
5110e166 | 1154 | (mapc (function |
2a1408fd DN |
1155 | (lambda (cell) |
1156 | (let ((add-pref (null (cdr (cdr cell)))) | |
1157 | (key-suff (list (car cell)))) | |
1158 | (apply 'define-key | |
1159 | (list map | |
1160 | (apply 'vconcat (if add-pref | |
1161 | (append pref key-suff) | |
1162 | key-suff)) | |
1163 | (car (cdr cell))))))) | |
1164 | keymap-list) | |
19b84ba3 | 1165 | map)) |
9179616f | 1166 | ;;;_ : Menu bar |
95c12694 RS |
1167 | (defvar allout-mode-exposure-menu) |
1168 | (defvar allout-mode-editing-menu) | |
1169 | (defvar allout-mode-navigation-menu) | |
1170 | (defvar allout-mode-misc-menu) | |
fd5359c6 | 1171 | (defun produce-allout-mode-menubar-entries () |
9179616f | 1172 | (require 'easymenu) |
fd5359c6 MR |
1173 | (easy-menu-define allout-mode-exposure-menu |
1174 | allout-mode-map | |
9179616f DL |
1175 | "Allout outline exposure menu." |
1176 | '("Exposure" | |
fd5359c6 MR |
1177 | ["Show Entry" allout-show-current-entry t] |
1178 | ["Show Children" allout-show-children t] | |
1179 | ["Show Subtree" allout-show-current-subtree t] | |
1180 | ["Hide Subtree" allout-hide-current-subtree t] | |
1181 | ["Hide Leaves" allout-hide-current-leaves t] | |
9179616f | 1182 | "----" |
fd5359c6 MR |
1183 | ["Show All" allout-show-all t])) |
1184 | (easy-menu-define allout-mode-editing-menu | |
1185 | allout-mode-map | |
9179616f DL |
1186 | "Allout outline editing menu." |
1187 | '("Headings" | |
fd5359c6 MR |
1188 | ["Open Sibling" allout-open-sibtopic t] |
1189 | ["Open Subtopic" allout-open-subtopic t] | |
1190 | ["Open Supertopic" allout-open-supertopic t] | |
9179616f | 1191 | "----" |
fd5359c6 MR |
1192 | ["Shift Topic In" allout-shift-in t] |
1193 | ["Shift Topic Out" allout-shift-out t] | |
1194 | ["Rebullet Topic" allout-rebullet-topic t] | |
1195 | ["Rebullet Heading" allout-rebullet-current-heading t] | |
d82979ea EZ |
1196 | ["Number Siblings" allout-number-siblings t] |
1197 | "----" | |
1198 | ["Toggle Topic Encryption" | |
1199 | allout-toggle-current-subtree-encryption | |
1200 | (> (allout-current-depth) 1)])) | |
fd5359c6 MR |
1201 | (easy-menu-define allout-mode-navigation-menu |
1202 | allout-mode-map | |
9179616f DL |
1203 | "Allout outline navigation menu." |
1204 | '("Navigation" | |
fd5359c6 | 1205 | ["Next Visible Heading" allout-next-visible-heading t] |
9179616f | 1206 | ["Previous Visible Heading" |
fd5359c6 | 1207 | allout-previous-visible-heading t] |
9179616f | 1208 | "----" |
fd5359c6 MR |
1209 | ["Up Level" allout-up-current-level t] |
1210 | ["Forward Current Level" allout-forward-current-level t] | |
9179616f | 1211 | ["Backward Current Level" |
fd5359c6 | 1212 | allout-backward-current-level t] |
9179616f DL |
1213 | "----" |
1214 | ["Beginning of Entry" | |
fd5359c6 | 1215 | allout-beginning-of-current-entry t] |
d82979ea | 1216 | ["End of Entry" allout-end-of-entry t] |
fd5359c6 MR |
1217 | ["End of Subtree" allout-end-of-current-subtree t])) |
1218 | (easy-menu-define allout-mode-misc-menu | |
1219 | allout-mode-map | |
9179616f DL |
1220 | "Allout outlines miscellaneous bindings." |
1221 | '("Misc" | |
fd5359c6 | 1222 | ["Version" allout-version t] |
9179616f | 1223 | "----" |
fd5359c6 | 1224 | ["Duplicate Exposed" allout-copy-exposed-to-buffer t] |
9179616f | 1225 | ["Duplicate Exposed, numbered" |
71296446 | 1226 | allout-flatten-exposed-to-buffer t] |
9179616f | 1227 | ["Duplicate Exposed, indented" |
71296446 | 1228 | allout-indented-exposed-to-buffer t] |
9179616f | 1229 | "----" |
fd5359c6 MR |
1230 | ["Set Header Lead" allout-reset-header-lead t] |
1231 | ["Set New Exposure" allout-expose-topic t]))) | |
01fc9422 | 1232 | ;;;_ : Allout Modal-Variables Utilities |
fd5359c6 MR |
1233 | ;;;_ = allout-mode-prior-settings |
1234 | (defvar allout-mode-prior-settings nil | |
01fc9422 | 1235 | "Internal `allout-mode' use; settings to be resumed on mode deactivation. |
1977b8f6 | 1236 | |
01fc9422 EZ |
1237 | See `allout-add-resumptions' and `allout-do-resumptions'.") |
1238 | (make-variable-buffer-local 'allout-mode-prior-settings) | |
1239 | ;;;_ > allout-add-resumptions (&rest pairs) | |
1240 | (defun allout-add-resumptions (&rest pairs) | |
48bd8440 | 1241 | "Set name/value PAIRS. |
01fc9422 EZ |
1242 | |
1243 | Old settings are preserved for later resumption using `allout-do-resumptions'. | |
1244 | ||
48bd8440 EZ |
1245 | The new values are set as a buffer local. On resumption, the prior buffer |
1246 | scope of the variable is restored along with its value. If it was a void | |
1247 | buffer-local value, then it is left as nil on resumption. | |
1248 | ||
01fc9422 | 1249 | The pairs are lists whose car is the name of the variable and car of the |
48bd8440 EZ |
1250 | cdr is the new value: '(some-var some-value)'. The pairs can actually be |
1251 | triples, where the third element qualifies the disposition of the setting, | |
1252 | as described further below. | |
01fc9422 | 1253 | |
48bd8440 EZ |
1254 | If the optional third element is the symbol 'extend, then the new value |
1255 | created by `cons'ing the second element of the pair onto the front of the | |
1256 | existing value. | |
01fc9422 | 1257 | |
48bd8440 EZ |
1258 | If the optional third element is the symbol 'append, then the new value is |
1259 | extended from the existing one by `append'ing a list containing the second | |
1260 | element of the pair onto the end of the existing value. | |
01fc9422 | 1261 | |
48bd8440 | 1262 | Extension, and resumptions in general, should not be used for hook |
60c7bbd2 | 1263 | functions -- use the 'local mode of `add-hook' for that, instead. |
01fc9422 EZ |
1264 | |
1265 | The settings are stored on `allout-mode-prior-settings'." | |
1266 | (while pairs | |
1267 | (let* ((pair (pop pairs)) | |
1268 | (name (car pair)) | |
48bd8440 EZ |
1269 | (value (cadr pair)) |
1270 | (qualifier (if (> (length pair) 2) | |
1271 | (caddr pair))) | |
1272 | prior-value) | |
01fc9422 EZ |
1273 | (if (not (symbolp name)) |
1274 | (error "Pair's name, %S, must be a symbol, not %s" | |
1275 | name (type-of name))) | |
d8350998 | 1276 | (setq prior-value (condition-case nil |
48bd8440 EZ |
1277 | (symbol-value name) |
1278 | (void-variable nil))) | |
01fc9422 EZ |
1279 | (when (not (assoc name allout-mode-prior-settings)) |
1280 | ;; Not already added as a resumption, create the prior setting entry. | |
1281 | (if (local-variable-p name) | |
60c7bbd2 | 1282 | ;; is already local variable -- preserve the prior value: |
48bd8440 | 1283 | (push (list name prior-value) allout-mode-prior-settings) |
01fc9422 EZ |
1284 | ;; wasn't local variable, indicate so for resumption by killing |
1285 | ;; local value, and make it local: | |
1286 | (push (list name) allout-mode-prior-settings) | |
1287 | (make-local-variable name))) | |
48bd8440 EZ |
1288 | (if qualifier |
1289 | (cond ((eq qualifier 'extend) | |
1290 | (if (not (listp prior-value)) | |
1291 | (error "extension of non-list prior value attempted") | |
1292 | (set name (cons value prior-value)))) | |
1293 | ((eq qualifier 'append) | |
1294 | (if (not (listp prior-value)) | |
1295 | (error "appending of non-list prior value attempted") | |
1296 | (set name (append prior-value (list value))))) | |
1297 | (t (error "unrecognized setting qualifier `%s' encountered" | |
1298 | qualifier))) | |
1299 | (set name value))))) | |
01fc9422 EZ |
1300 | ;;;_ > allout-do-resumptions () |
1301 | (defun allout-do-resumptions () | |
1302 | "Resume all name/value settings registered by `allout-add-resumptions'. | |
1303 | ||
1304 | This is used when concluding allout-mode, to resume selected variables to | |
1305 | their settings before allout-mode was started." | |
1306 | ||
1307 | (while allout-mode-prior-settings | |
1308 | (let* ((pair (pop allout-mode-prior-settings)) | |
1309 | (name (car pair)) | |
1310 | (value-cell (cdr pair))) | |
1311 | (if (not value-cell) | |
1312 | ;; Prior value was global: | |
1313 | (kill-local-variable name) | |
1314 | ;; Prior value was explicit: | |
1315 | (set name (car value-cell)))))) | |
9179616f | 1316 | ;;;_ : Mode-specific incidentals |
fd5359c6 | 1317 | ;;;_ > allout-unprotected (expr) |
d82979ea | 1318 | (defmacro allout-unprotected (expr) |
96b83743 | 1319 | "Enable internal outline operations to alter invisible text." |
1b4d97b2 | 1320 | `(let ((inhibit-read-only (if (not buffer-read-only) t)) |
0458fa80 | 1321 | (inhibit-field-text-motion t)) |
96b83743 EZ |
1322 | ,expr)) |
1323 | ;;;_ = allout-mode-hook | |
1324 | (defvar allout-mode-hook nil | |
1325 | "*Hook that's run when allout mode starts.") | |
01fc9422 EZ |
1326 | ;;;_ = allout-mode-deactivate-hook |
1327 | (defvar allout-mode-deactivate-hook nil | |
1328 | "*Hook that's run when allout mode ends.") | |
1329 | ;;;_ = allout-exposure-category | |
1330 | (defvar allout-exposure-category nil | |
1331 | "Symbol for use as allout invisible-text overlay category.") | |
da506f05 | 1332 | ;;;_ x allout-view-change-hook |
96b83743 | 1333 | (defvar allout-view-change-hook nil |
c07583cd | 1334 | "*(Deprecated) A hook run after allout outline exposure changes. |
da506f05 | 1335 | |
48bd8440 EZ |
1336 | Switch to using `allout-exposure-change-hook' instead. Both hooks are |
1337 | currently respected, but the other conveys the details of the exposure | |
1338 | change via explicit parameters, and this one will eventually be disabled in | |
1339 | a subsequent allout version.") | |
da506f05 RS |
1340 | ;;;_ = allout-exposure-change-hook |
1341 | (defvar allout-exposure-change-hook nil | |
48bd8440 EZ |
1342 | "*Hook that's run after allout outline subtree exposure changes. |
1343 | ||
1344 | It is run at the conclusion of `allout-flag-region'. | |
1345 | ||
1346 | Functions on the hook must take three arguments: | |
1347 | ||
60c7bbd2 RS |
1348 | - FROM -- integer indicating the point at the start of the change. |
1349 | - TO -- integer indicating the point of the end of the change. | |
1350 | - FLAG -- change mode: nil for exposure, otherwise concealment. | |
48bd8440 EZ |
1351 | |
1352 | This hook might be invoked multiple times by a single command. | |
1353 | ||
1354 | This hook is replacing `allout-view-change-hook', which is being deprecated | |
1355 | and eventually will not be invoked.") | |
1356 | ;;;_ = allout-structure-added-hook | |
1357 | (defvar allout-structure-added-hook nil | |
1358 | "*Hook that's run after addition of items to the outline. | |
1359 | ||
1360 | Functions on the hook should take two arguments: | |
1361 | ||
60c7bbd2 RS |
1362 | - NEW-START -- integer indicating position of start of the first new item. |
1363 | - NEW-END -- integer indicating position of end of the last new item. | |
48bd8440 | 1364 | |
60c7bbd2 | 1365 | Some edits that introduce new items may missed by this hook: |
48bd8440 EZ |
1366 | specifically edits that native allout routines do not control. |
1367 | ||
1368 | This hook might be invoked multiple times by a single command.") | |
1369 | ;;;_ = allout-structure-deleted-hook | |
1370 | (defvar allout-structure-deleted-hook nil | |
1371 | "*Hook that's run after disciplined deletion of subtrees from the outline. | |
1372 | ||
1373 | Functions on the hook must take two arguments: | |
1374 | ||
60c7bbd2 RS |
1375 | - DEPTH -- integer indicating the depth of the subtree that was deleted. |
1376 | - REMOVED-FROM -- integer indicating the point where the subtree was removed. | |
da506f05 | 1377 | |
60c7bbd2 | 1378 | Some edits that remove or invalidate items may missed by this hook: |
48bd8440 | 1379 | specifically edits that native allout routines do not control. |
96b83743 | 1380 | |
48bd8440 EZ |
1381 | This hook might be invoked multiple times by a single command.") |
1382 | ;;;_ = allout-structure-shifted-hook | |
1383 | (defvar allout-structure-shifted-hook nil | |
1384 | "*Hook that's run after shifting of items in the outline. | |
1385 | ||
1386 | Functions on the hook should take two arguments: | |
1387 | ||
60c7bbd2 RS |
1388 | - DEPTH-CHANGE -- integer indicating depth increase, negative for decrease |
1389 | - START -- integer indicating the start point of the shifted parent item. | |
48bd8440 | 1390 | |
60c7bbd2 | 1391 | Some edits that shift items can be missed by this hook: specifically edits |
48bd8440 EZ |
1392 | that native allout routines do not control. |
1393 | ||
1394 | This hook might be invoked multiple times by a single command.") | |
96b83743 EZ |
1395 | ;;;_ = allout-outside-normal-auto-fill-function |
1396 | (defvar allout-outside-normal-auto-fill-function nil | |
1397 | "Value of normal-auto-fill-function outside of allout mode. | |
1398 | ||
1399 | Used by allout-auto-fill to do the mandated normal-auto-fill-function | |
1400 | wrapped within allout's automatic fill-prefix setting.") | |
1401 | (make-variable-buffer-local 'allout-outside-normal-auto-fill-function) | |
19b84ba3 | 1402 | ;;;_ = file-var-bug hack |
fd5359c6 | 1403 | (defvar allout-v18/19-file-var-hack nil |
c567ac01 | 1404 | "Horrible hack used to prevent invalid multiple triggering of outline |
2265e017 | 1405 | mode from prop-line file-var activation. Used by `allout-mode' function |
c567ac01 | 1406 | to track repeats.") |
0949617b EZ |
1407 | ;;;_ = allout-passphrase-verifier-string |
1408 | (defvar allout-passphrase-verifier-string nil | |
1409 | "Setting used to test solicited encryption passphrases against the one | |
1410 | already associated with a file. | |
1411 | ||
1412 | It consists of an encrypted random string useful only to verify that a | |
1413 | passphrase entered by the user is effective for decryption. The passphrase | |
1414 | itself is \*not* recorded in the file anywhere, and the encrypted contents | |
1415 | are random binary characters to avoid exposing greater susceptibility to | |
1416 | search attacks. | |
d82979ea EZ |
1417 | |
1418 | The verifier string is retained as an Emacs file variable, as well as in | |
a0a022d5 | 1419 | the Emacs buffer state, if file variable adjustments are enabled. See |
d82979ea | 1420 | `allout-enable-file-variable-adjustment' for details about that.") |
0949617b | 1421 | (make-variable-buffer-local 'allout-passphrase-verifier-string) |
8570ce80 | 1422 | ;;;###autoload |
7e5d77dc | 1423 | (put 'allout-passphrase-verifier-string 'safe-local-variable 'stringp) |
0949617b EZ |
1424 | ;;;_ = allout-passphrase-hint-string |
1425 | (defvar allout-passphrase-hint-string "" | |
1426 | "Variable used to retain reminder string for file's encryption passphrase. | |
d82979ea | 1427 | |
0949617b | 1428 | See the description of `allout-passphrase-hint-handling' for details about how |
d82979ea EZ |
1429 | the reminder is deployed. |
1430 | ||
a0a022d5 | 1431 | The hint is retained as an Emacs file variable, as well as in the Emacs buffer |
d82979ea EZ |
1432 | state, if file variable adjustments are enabled. See |
1433 | `allout-enable-file-variable-adjustment' for details about that.") | |
0949617b EZ |
1434 | (make-variable-buffer-local 'allout-passphrase-hint-string) |
1435 | (setq-default allout-passphrase-hint-string "") | |
8570ce80 SM |
1436 | ;;;###autoload |
1437 | (put 'allout-passphrase-hint-string 'safe-local-variable 'stringp) | |
d82979ea EZ |
1438 | ;;;_ = allout-after-save-decrypt |
1439 | (defvar allout-after-save-decrypt nil | |
1440 | "Internal variable, is nil or has the value of two points: | |
1441 | ||
1442 | - the location of a topic to be decrypted after saving is done | |
1443 | - where to situate the cursor after the decryption is performed | |
1444 | ||
1445 | This is used to decrypt the topic that was currently being edited, if it | |
1446 | was encrypted automatically as part of a file write or autosave.") | |
1447 | (make-variable-buffer-local 'allout-after-save-decrypt) | |
48bd8440 EZ |
1448 | ;;;_ = allout-encryption-plaintext-sanitization-regexps |
1449 | (defvar allout-encryption-plaintext-sanitization-regexps nil | |
1450 | "List of regexps whose matches are removed from plaintext before encryption. | |
1451 | ||
1452 | This is for the sake of removing artifacts, like escapes, that are added on | |
1453 | and not actually part of the original plaintext. The removal is done just | |
1454 | prior to encryption. | |
1455 | ||
1456 | Entries must be symbols that are bound to the desired values. | |
1457 | ||
1458 | Each value can be a regexp or a list with a regexp followed by a | |
1459 | substitution string. If it's just a regexp, all its matches are removed | |
1460 | before the text is encrypted. If it's a regexp and a substitution, the | |
1461 | substition is used against the regexp matches, a la `replace-match'.") | |
1462 | (make-variable-buffer-local 'allout-encryption-text-removal-regexps) | |
1463 | ;;;_ = allout-encryption-ciphertext-rejection-regexps | |
1464 | (defvar allout-encryption-ciphertext-rejection-regexps nil | |
1465 | "Variable for regexps matching plaintext to remove before encryption. | |
1466 | ||
1467 | This is for the sake of redoing encryption in cases where the ciphertext | |
ea18b2b1 | 1468 | incidentally contains strings that would disrupt mode operation -- |
48bd8440 EZ |
1469 | for example, a line that happens to look like an allout-mode topic prefix. |
1470 | ||
1471 | Entries must be symbols that are bound to the desired regexp values. | |
1472 | ||
1473 | The encryption will be retried up to | |
1474 | `allout-encryption-ciphertext-rejection-limit' times, after which an error | |
1475 | is raised.") | |
1476 | ||
1477 | (make-variable-buffer-local 'allout-encryption-ciphertext-rejection-regexps) | |
1478 | ;;;_ = allout-encryption-ciphertext-rejection-ceiling | |
1479 | (defvar allout-encryption-ciphertext-rejection-ceiling 5 | |
1480 | "Limit on number of times encryption ciphertext is rejected. | |
1481 | ||
1482 | See `allout-encryption-ciphertext-rejection-regexps' for rejection reasons.") | |
1483 | (make-variable-buffer-local 'allout-encryption-ciphertext-rejection-ceiling) | |
69027dea EZ |
1484 | ;;;_ > allout-mode-p () |
1485 | ;; Must define this macro above any uses, or byte compilation will lack | |
60c7bbd2 | 1486 | ;; proper def, if file isn't loaded -- eg, during emacs build! |
69027dea EZ |
1487 | (defmacro allout-mode-p () |
1488 | "Return t if `allout-mode' is active in current buffer." | |
1489 | 'allout-mode) | |
d82979ea EZ |
1490 | ;;;_ > allout-write-file-hook-handler () |
1491 | (defun allout-write-file-hook-handler () | |
1492 | "Implement `allout-encrypt-unencrypted-on-saves' policy for file writes." | |
1493 | ||
0949617b EZ |
1494 | (if (or (not (allout-mode-p)) |
1495 | (not (boundp 'allout-encrypt-unencrypted-on-saves)) | |
d82979ea EZ |
1496 | (not allout-encrypt-unencrypted-on-saves)) |
1497 | nil | |
1498 | (let ((except-mark (and (equal allout-encrypt-unencrypted-on-saves | |
1499 | 'except-current) | |
1500 | (point-marker)))) | |
1501 | (if (save-excursion (goto-char (point-min)) | |
1502 | (allout-next-topic-pending-encryption except-mark)) | |
1503 | (progn | |
1504 | (message "auto-encrypting pending topics") | |
96b83743 | 1505 | (sit-for 0) |
d82979ea EZ |
1506 | (condition-case failure |
1507 | (setq allout-after-save-decrypt | |
1508 | (allout-encrypt-decrypted except-mark)) | |
42c3e51c KM |
1509 | (error (message |
1510 | "allout-write-file-hook-handler suppressing error %s" | |
1511 | failure) | |
1512 | (sit-for 2))))) | |
d82979ea | 1513 | )) |
42c3e51c | 1514 | nil) |
d82979ea EZ |
1515 | ;;;_ > allout-auto-save-hook-handler () |
1516 | (defun allout-auto-save-hook-handler () | |
69027dea | 1517 | "Implement `allout-encrypt-unencrypted-on-saves' policy for auto save." |
d82979ea | 1518 | |
0949617b | 1519 | (if (and (allout-mode-p) allout-encrypt-unencrypted-on-saves) |
d82979ea EZ |
1520 | ;; Always implement 'except-current policy when enabled. |
1521 | (let ((allout-encrypt-unencrypted-on-saves 'except-current)) | |
1522 | (allout-write-file-hook-handler)))) | |
1523 | ;;;_ > allout-after-saves-handler () | |
1524 | (defun allout-after-saves-handler () | |
1525 | "Decrypt topic encrypted for save, if it's currently being edited. | |
1526 | ||
1527 | Ie, if it was pending encryption and contained the point in its body before | |
1528 | the save. | |
1529 | ||
1530 | We use values stored in `allout-after-save-decrypt' to locate the topic | |
1531 | and the place for the cursor after the decryption is done." | |
1532 | (if (not (and (allout-mode-p) | |
1533 | (boundp 'allout-after-save-decrypt) | |
1534 | allout-after-save-decrypt)) | |
1535 | t | |
1536 | (goto-char (car allout-after-save-decrypt)) | |
1537 | (let ((was-modified (buffer-modified-p))) | |
69027dea | 1538 | (allout-toggle-subtree-encryption) |
d82979ea EZ |
1539 | (if (not was-modified) |
1540 | (set-buffer-modified-p nil))) | |
1541 | (goto-char (cadr allout-after-save-decrypt)) | |
1542 | (setq allout-after-save-decrypt nil)) | |
1543 | ) | |
498a6493 CY |
1544 | ;;;_ = allout-inhibit-aberrance-doublecheck nil |
1545 | ;; In some exceptional moments, disparate topic depths need to be allowed | |
1546 | ;; momentarily, eg when one topic is being yanked into another and they're | |
1547 | ;; about to be reconciled. let-binding allout-inhibit-aberrance-doublecheck | |
1548 | ;; prevents the aberrance doublecheck to allow, eg, the reconciliation | |
1549 | ;; processing to happen in the presence of such discrepancies. It should | |
1550 | ;; almost never be needed, however. | |
1551 | (defvar allout-inhibit-aberrance-doublecheck nil | |
1552 | "Internal state, for momentarily inhibits aberrance doublecheck. | |
1553 | ||
1554 | This should only be momentarily let-bound non-nil, not set | |
1555 | non-nil in a lasting way.") | |
19b84ba3 RS |
1556 | |
1557 | ;;;_ #2 Mode activation | |
fd5359c6 MR |
1558 | ;;;_ = allout-explicitly-deactivated |
1559 | (defvar allout-explicitly-deactivated nil | |
d82979ea | 1560 | "If t, `allout-mode's last deactivation was deliberate. |
2265e017 | 1561 | So `allout-post-command-business' should not reactivate it...") |
fd5359c6 MR |
1562 | (make-variable-buffer-local 'allout-explicitly-deactivated) |
1563 | ;;;_ > allout-init (&optional mode) | |
1564 | (defun allout-init (&optional mode) | |
2265e017 | 1565 | "Prime `allout-mode' to enable/disable auto-activation, wrt `allout-layout'. |
19b84ba3 | 1566 | |
8d118843 | 1567 | MODE is one of the following symbols: |
19b84ba3 | 1568 | |
c07583cd | 1569 | - nil (or no argument) deactivate auto-activation/layout; |
353e2ef2 KH |
1570 | - `activate', enable auto-activation only; |
1571 | - `ask', enable auto-activation, and enable auto-layout but with | |
a0776d6b | 1572 | confirmation for layout operation solicited from user each time; |
353e2ef2 | 1573 | - `report', just report and return the current auto-activation state; |
c07583cd | 1574 | - anything else (eg, t) for auto-activation and auto-layout, without |
8d118843 | 1575 | any confirmation check. |
19b84ba3 | 1576 | |
e126900f | 1577 | Use this function to setup your Emacs session for automatic activation |
19b84ba3 | 1578 | of allout outline mode, contingent to the buffer-specific setting of |
fd5359c6 MR |
1579 | the `allout-layout' variable. (See `allout-layout' and |
1580 | `allout-expose-topic' docstrings for more details on auto layout). | |
19b84ba3 | 1581 | |
d82979ea EZ |
1582 | `allout-init' works by setting up (or removing) the `allout-mode' |
1583 | find-file-hook, and giving `allout-auto-activation' a suitable | |
1584 | setting. | |
19b84ba3 | 1585 | |
e126900f JB |
1586 | To prime your Emacs session for full auto-outline operation, include |
1587 | the following two lines in your Emacs init file: | |
19b84ba3 RS |
1588 | |
1589 | \(require 'allout) | |
fd5359c6 | 1590 | \(allout-init t)" |
19b84ba3 | 1591 | |
d82979ea | 1592 | (interactive) |
32226619 | 1593 | (if (called-interactively-p 'interactive) |
d82979ea EZ |
1594 | (progn |
1595 | (setq mode | |
1596 | (completing-read | |
1597 | (concat "Select outline auto setup mode " | |
1598 | "(empty for report, ? for options) ") | |
1599 | '(("nil")("full")("activate")("deactivate") | |
1600 | ("ask") ("report") ("")) | |
1601 | nil | |
1602 | t)) | |
1603 | (if (string= mode "") | |
1604 | (setq mode 'report) | |
1605 | (setq mode (intern-soft mode))))) | |
8d118843 RS |
1606 | (let |
1607 | ;; convenience aliases, for consistent ref to respective vars: | |
fd5359c6 | 1608 | ((hook 'allout-find-file-hook) |
0949617b EZ |
1609 | (find-file-hook-var-name (if (boundp 'find-file-hook) |
1610 | 'find-file-hook | |
1611 | 'find-file-hooks)) | |
fd5359c6 | 1612 | (curr-mode 'allout-auto-activation)) |
353e2ef2 | 1613 | |
8d118843 | 1614 | (cond ((not mode) |
0949617b EZ |
1615 | (set find-file-hook-var-name |
1616 | (delq hook (symbol-value find-file-hook-var-name))) | |
32226619 | 1617 | (if (called-interactively-p 'interactive) |
8d118843 RS |
1618 | (message "Allout outline mode auto-activation inhibited."))) |
1619 | ((eq mode 'report) | |
0949617b | 1620 | (if (not (memq hook (symbol-value find-file-hook-var-name))) |
d82979ea EZ |
1621 | (allout-init nil) |
1622 | ;; Just punt and use the reports from each of the modes: | |
1623 | (allout-init (symbol-value curr-mode)))) | |
0949617b | 1624 | (t (add-hook find-file-hook-var-name hook) |
353e2ef2 | 1625 | (set curr-mode ; `set', not `setq'! |
8d118843 RS |
1626 | (cond ((eq mode 'activate) |
1627 | (message | |
1628 | "Outline mode auto-activation enabled.") | |
1629 | 'activate) | |
1630 | ((eq mode 'report) | |
1631 | ;; Return the current mode setting: | |
fd5359c6 | 1632 | (allout-init mode)) |
8d118843 RS |
1633 | ((eq mode 'ask) |
1634 | (message | |
1635 | (concat "Outline mode auto-activation and " | |
c07583cd | 1636 | "-layout (upon confirmation) enabled.")) |
8d118843 RS |
1637 | 'ask) |
1638 | ((message | |
1639 | "Outline mode auto-activation and -layout enabled.") | |
1640 | 'full))))))) | |
fd5359c6 MR |
1641 | ;;;_ > allout-setup-menubar () |
1642 | (defun allout-setup-menubar () | |
aad94676 | 1643 | "Populate the current buffer's menubar with `allout-mode' stuff." |
fd5359c6 MR |
1644 | (let ((menus (list allout-mode-exposure-menu |
1645 | allout-mode-editing-menu | |
1646 | allout-mode-navigation-menu | |
1647 | allout-mode-misc-menu)) | |
9179616f DL |
1648 | cur) |
1649 | (while menus | |
1650 | (setq cur (car menus) | |
1651 | menus (cdr menus)) | |
1652 | (easy-menu-add cur)))) | |
01fc9422 EZ |
1653 | ;;;_ > allout-overlay-preparations |
1654 | (defun allout-overlay-preparations () | |
1655 | "Set the properties of the allout invisible-text overlay and others." | |
1656 | (setplist 'allout-exposure-category nil) | |
1657 | (put 'allout-exposure-category 'invisible 'allout) | |
1658 | (put 'allout-exposure-category 'evaporate t) | |
96b83743 | 1659 | ;; XXX We use isearch-open-invisible *and* isearch-mode-end-hook. The |
ea18b2b1 | 1660 | ;; latter would be sufficient, but it seems that a separate behavior -- |
60c7bbd2 RS |
1661 | ;; the _transient_ opening of invisible text during isearch -- is keyed to |
1662 | ;; presence of the isearch-open-invisible property -- even though this | |
96b83743 | 1663 | ;; property controls the isearch _arrival_ behavior. This is the case at |
21e8bc99 | 1664 | ;; least in emacs 21, 22.1, and xemacs 21.4. |
01fc9422 | 1665 | (put 'allout-exposure-category 'isearch-open-invisible |
96b83743 EZ |
1666 | 'allout-isearch-end-handler) |
1667 | (if (featurep 'xemacs) | |
01fc9422 EZ |
1668 | (put 'allout-exposure-category 'start-open t) |
1669 | (put 'allout-exposure-category 'insert-in-front-hooks | |
96b83743 | 1670 | '(allout-overlay-insert-in-front-handler))) |
01fc9422 EZ |
1671 | (put 'allout-exposure-category 'modification-hooks |
1672 | '(allout-overlay-interior-modification-handler))) | |
fd5359c6 | 1673 | ;;;_ > allout-mode (&optional toggle) |
19b84ba3 | 1674 | ;;;_ : Defun: |
0949617b | 1675 | ;;;###autoload |
fd5359c6 | 1676 | (defun allout-mode (&optional toggle) |
c567ac01 | 1677 | ;;;_ . Doc string: |
19b84ba3 | 1678 | "Toggle minor mode for controlling exposure and editing of text outlines. |
96b83743 | 1679 | \\<allout-mode-map> |
19b84ba3 | 1680 | |
4837b516 GM |
1681 | Optional prefix argument TOGGLE forces the mode to re-initialize |
1682 | if it is positive, otherwise it turns the mode off. Allout | |
1683 | outline mode always runs as a minor mode. | |
19b84ba3 | 1684 | |
aad94676 | 1685 | Allout outline mode provides extensive outline oriented formatting and |
9179616f DL |
1686 | manipulation. It enables structural editing of outlines, as well as |
1687 | navigation and exposure. It also is specifically aimed at | |
c07583cd | 1688 | accommodating syntax-sensitive text like programming languages. (For |
9179616f DL |
1689 | an example, see the allout code itself, which is organized as an allout |
1690 | outline.) | |
19b84ba3 | 1691 | |
c07583cd | 1692 | In addition to typical outline navigation and exposure, allout includes: |
19b84ba3 | 1693 | |
c07583cd KS |
1694 | - topic-oriented authoring, including keystroke-based topic creation, |
1695 | repositioning, promotion/demotion, cut, and paste | |
9179616f | 1696 | - incremental search with dynamic exposure and reconcealment of hidden text |
c07583cd | 1697 | - adjustable format, so programming code can be developed in outline-structure |
d82979ea | 1698 | - easy topic encryption and decryption |
c07583cd KS |
1699 | - \"Hot-spot\" operation, for single-keystroke maneuvering and exposure control |
1700 | - integral outline layout, for automatic initial exposure when visiting a file | |
1701 | - independent extensibility, using comprehensive exposure and authoring hooks | |
9179616f DL |
1702 | |
1703 | and many other features. | |
19b84ba3 | 1704 | |
c07583cd | 1705 | Below is a description of the key bindings, and then explanation of |
2265e017 | 1706 | special `allout-mode' features and terminology. See also the outline |
9179616f | 1707 | menubar additions for quick reference to many of the features, and see |
f134d28b | 1708 | the docstring of the function `allout-init' for instructions on |
d82979ea | 1709 | priming your emacs session for automatic activation of `allout-mode'. |
9179616f | 1710 | |
c07583cd KS |
1711 | The bindings are dictated by the customizable `allout-keybindings-list' |
1712 | variable. We recommend customizing `allout-command-prefix' to use just | |
1713 | `\\C-c' as the command prefix, if the allout bindings don't conflict with | |
1714 | any personal bindings you have on \\C-c. In any case, outline structure | |
1715 | navigation and authoring is simplified by positioning the cursor on an | |
60c7bbd2 | 1716 | item's bullet character, the \"hot-spot\" -- then you can invoke allout |
c07583cd KS |
1717 | commands with just the un-prefixed, un-control-shifted command letters. |
1718 | This is described further in the HOT-SPOT Operation section. | |
1719 | ||
1720 | Exposure Control: | |
1721 | ---------------- | |
1722 | \\[allout-hide-current-subtree] `allout-hide-current-subtree' | |
1723 | \\[allout-show-children] `allout-show-children' | |
1724 | \\[allout-show-current-subtree] `allout-show-current-subtree' | |
1725 | \\[allout-show-current-entry] `allout-show-current-entry' | |
1726 | \\[allout-show-all] `allout-show-all' | |
1727 | ||
1728 | Navigation: | |
1729 | ---------- | |
1730 | \\[allout-next-visible-heading] `allout-next-visible-heading' | |
1731 | \\[allout-previous-visible-heading] `allout-previous-visible-heading' | |
1732 | \\[allout-up-current-level] `allout-up-current-level' | |
1733 | \\[allout-forward-current-level] `allout-forward-current-level' | |
1734 | \\[allout-backward-current-level] `allout-backward-current-level' | |
1735 | \\[allout-end-of-entry] `allout-end-of-entry' | |
1736 | \\[allout-beginning-of-current-entry] `allout-beginning-of-current-entry' (alternately, goes to hot-spot) | |
60c7bbd2 | 1737 | \\[allout-beginning-of-line] `allout-beginning-of-line' -- like regular beginning-of-line, but |
c07583cd KS |
1738 | if immediately repeated cycles to the beginning of the current item |
1739 | and then to the hot-spot (if `allout-beginning-of-line-cycles' is set). | |
1740 | ||
1741 | ||
1742 | Topic Header Production: | |
1743 | ----------------------- | |
1744 | \\[allout-open-sibtopic] `allout-open-sibtopic' Create a new sibling after current topic. | |
1745 | \\[allout-open-subtopic] `allout-open-subtopic' ... an offspring of current topic. | |
1746 | \\[allout-open-supertopic] `allout-open-supertopic' ... a sibling of the current topic's parent. | |
1747 | ||
1748 | Topic Level and Prefix Adjustment: | |
1749 | --------------------------------- | |
1750 | \\[allout-shift-in] `allout-shift-in' Shift current topic and all offspring deeper | |
1751 | \\[allout-shift-out] `allout-shift-out' ... less deep | |
1752 | \\[allout-rebullet-current-heading] `allout-rebullet-current-heading' Prompt for alternate bullet for | |
1753 | current topic | |
1754 | \\[allout-rebullet-topic] `allout-rebullet-topic' Reconcile bullets of topic and | |
60c7bbd2 | 1755 | its' offspring -- distinctive bullets are not changed, others |
c07583cd | 1756 | are alternated according to nesting depth. |
ea18b2b1 | 1757 | \\[allout-number-siblings] `allout-number-siblings' Number bullets of topic and siblings -- |
c07583cd KS |
1758 | the offspring are not affected. |
1759 | With repeat count, revoke numbering. | |
1760 | ||
1761 | Topic-oriented Killing and Yanking: | |
1762 | ---------------------------------- | |
1763 | \\[allout-kill-topic] `allout-kill-topic' Kill current topic, including offspring. | |
1764 | \\[allout-copy-topic-as-kill] `allout-copy-topic-as-kill' Copy current topic, including offspring. | |
1765 | \\[allout-kill-line] `allout-kill-line' kill-line, attending to outline structure. | |
1766 | \\[allout-copy-line-as-kill] `allout-copy-line-as-kill' Copy line but don't delete it. | |
1767 | \\[allout-yank] `allout-yank' Yank, adjusting depth of yanked topic to | |
1768 | depth of heading if yanking into bare topic | |
1769 | heading (ie, prefix sans text). | |
1770 | \\[allout-yank-pop] `allout-yank-pop' Is to allout-yank as yank-pop is to yank | |
1771 | ||
1772 | Topic-oriented Encryption: | |
1773 | ------------------------- | |
1774 | \\[allout-toggle-current-subtree-encryption] `allout-toggle-current-subtree-encryption' | |
1775 | Encrypt/Decrypt topic content | |
1776 | ||
1777 | Misc commands: | |
1778 | ------------- | |
1779 | M-x outlineify-sticky Activate outline mode for current buffer, | |
1780 | and establish a default file-var setting | |
1781 | for `allout-layout'. | |
1782 | \\[allout-mark-topic] `allout-mark-topic' | |
1783 | \\[allout-copy-exposed-to-buffer] `allout-copy-exposed-to-buffer' | |
1784 | Duplicate outline, sans concealed text, to | |
1785 | buffer with name derived from derived from that | |
60c7bbd2 | 1786 | of current buffer -- \"*BUFFERNAME exposed*\". |
c07583cd KS |
1787 | \\[allout-flatten-exposed-to-buffer] `allout-flatten-exposed-to-buffer' |
1788 | Like above 'copy-exposed', but convert topic | |
1789 | prefixes to section.subsection... numeric | |
1790 | format. | |
1791 | \\[eval-expression] (allout-init t) Setup Emacs session for outline mode | |
1792 | auto-activation. | |
1977b8f6 | 1793 | |
96b83743 | 1794 | Topic Encryption |
d82979ea | 1795 | |
96b83743 EZ |
1796 | Outline mode supports gpg encryption of topics, with support for |
1797 | symmetric and key-pair modes, passphrase timeout, passphrase | |
1798 | consistency checking, user-provided hinting for symmetric key | |
1799 | mode, and auto-encryption of topics pending encryption on save. | |
d82979ea | 1800 | |
c07583cd KS |
1801 | Topics pending encryption are, by default, automatically |
1802 | encrypted during file saves. If the contents of the topic | |
1803 | containing the cursor was encrypted for a save, it is | |
1804 | automatically decrypted for continued editing. | |
1805 | ||
1806 | The aim of these measures is reliable topic privacy while | |
1807 | preventing accidents like neglected encryption before saves, | |
1808 | forgetting which passphrase was used, and other practical | |
1809 | pitfalls. | |
1810 | ||
1811 | See `allout-toggle-current-subtree-encryption' function docstring | |
1812 | and `allout-encrypt-unencrypted-on-saves' customization variable | |
1813 | for details. | |
d82979ea | 1814 | |
c07583cd | 1815 | HOT-SPOT Operation |
c567ac01 | 1816 | |
19b84ba3 RS |
1817 | Hot-spot operation provides a means for easy, single-keystroke outline |
1818 | navigation and exposure control. | |
c567ac01 | 1819 | |
19b84ba3 RS |
1820 | When the text cursor is positioned directly on the bullet character of |
1821 | a topic, regular characters (a to z) invoke the commands of the | |
fd5359c6 | 1822 | corresponding allout-mode keymap control chars. For example, \"f\" |
96b83743 | 1823 | would invoke the command typically bound to \"C-c<space>C-f\" |
fd5359c6 | 1824 | \(\\[allout-forward-current-level] `allout-forward-current-level'). |
19b84ba3 | 1825 | |
96b83743 EZ |
1826 | Thus, by positioning the cursor on a topic bullet, you can |
1827 | execute the outline navigation and manipulation commands with a | |
c07583cd | 1828 | single keystroke. Regular navigation keys (eg, \\[forward-char], \\[next-line]) don't get |
96b83743 | 1829 | this special translation, so you can use them to get out of the |
c07583cd KS |
1830 | hot-spot and back to normal editing operation. |
1831 | ||
d41b91e6 | 1832 | In allout-mode, the normal beginning-of-line command (\\[allout-beginning-of-line]) is |
c07583cd KS |
1833 | replaced with one that makes it easy to get to the hot-spot. If you |
1834 | repeat it immediately it cycles (if `allout-beginning-of-line-cycles' | |
1835 | is set) to the beginning of the item and then, if you hit it again | |
1836 | immediately, to the hot-spot. Similarly, `allout-beginning-of-current-entry' | |
1837 | \(\\[allout-beginning-of-current-entry]) moves to the hot-spot when the cursor is already located | |
1838 | at the beginning of the current entry. | |
1839 | ||
1840 | Extending Allout | |
c567ac01 | 1841 | |
c07583cd KS |
1842 | Allout exposure and authoring activites all have associated |
1843 | hooks, by which independent code can cooperate with allout | |
1844 | without changes to the allout core. Here are key ones: | |
1977b8f6 | 1845 | |
c07583cd KS |
1846 | `allout-mode-hook' |
1847 | `allout-mode-deactivate-hook' | |
1848 | `allout-exposure-change-hook' | |
1849 | `allout-structure-added-hook' | |
1850 | `allout-structure-deleted-hook' | |
1851 | `allout-structure-shifted-hook' | |
1852 | ||
1853 | Terminology | |
1977b8f6 | 1854 | |
60c7bbd2 | 1855 | Topic hierarchy constituents -- TOPICS and SUBTOPICS: |
c567ac01 | 1856 | |
c07583cd KS |
1857 | ITEM: A unitary outline element, including the HEADER and ENTRY text. |
1858 | TOPIC: An ITEM and any ITEMs contained within it, ie having greater DEPTH | |
1859 | and with no intervening items of lower DEPTH than the container. | |
1860 | CURRENT ITEM: | |
1861 | The visible ITEM most immediately containing the cursor. | |
1862 | DEPTH: The degree of nesting of an ITEM; it increases with containment. | |
1863 | The DEPTH is determined by the HEADER PREFIX. The DEPTH is also | |
1864 | called the: | |
1865 | LEVEL: The same as DEPTH. | |
c567ac01 RS |
1866 | |
1867 | ANCESTORS: | |
c07583cd KS |
1868 | Those ITEMs whose TOPICs contain an ITEM. |
1869 | PARENT: An ITEM's immediate ANCESTOR. It has a DEPTH one less than that | |
1870 | of the ITEM. | |
c567ac01 | 1871 | OFFSPRING: |
c07583cd | 1872 | The ITEMs contained within an ITEM's TOPIC. |
19b84ba3 | 1873 | SUBTOPIC: |
c07583cd KS |
1874 | An OFFSPRING of its ANCESTOR TOPICs. |
1875 | CHILD: | |
1876 | An immediate SUBTOPIC of its PARENT. | |
c567ac01 | 1877 | SIBLINGS: |
c07583cd | 1878 | TOPICs having the same PARENT and DEPTH. |
353e2ef2 | 1879 | |
c567ac01 RS |
1880 | Topic text constituents: |
1881 | ||
c07583cd KS |
1882 | HEADER: The first line of an ITEM, include the ITEM PREFIX and HEADER |
1883 | text. | |
1884 | ENTRY: The text content of an ITEM, before any OFFSPRING, but including | |
1885 | the HEADER text and distinct from the ITEM PREFIX. | |
1886 | BODY: Same as ENTRY. | |
1887 | PREFIX: The leading text of an ITEM which distinguishes it from normal | |
1888 | ENTRY text. Allout recognizes the outline structure according | |
1889 | to the strict PREFIX format. It consists of a PREFIX-LEAD string, | |
1890 | PREFIX-PADDING, and a BULLET. The BULLET might be followed by a | |
d82979ea | 1891 | number, indicating the ordinal number of the topic among its |
c07583cd KS |
1892 | siblings, or an asterisk indicating encryption, plus an optional |
1893 | space. After that is the ITEM HEADER text, which is not part of | |
1894 | the PREFIX. | |
c567ac01 | 1895 | |
c07583cd KS |
1896 | The relative length of the PREFIX determines the nesting DEPTH |
1897 | of the ITEM. | |
c567ac01 | 1898 | PREFIX-LEAD: |
c07583cd KS |
1899 | The string at the beginning of a HEADER PREFIX, by default a `.'. |
1900 | It can be customized by changing the setting of | |
1901 | `allout-header-prefix' and then reinitializing `allout-mode'. | |
1902 | ||
1903 | When the PREFIX-LEAD is set to the comment-string of a | |
1904 | programming language, outline structuring can be embedded in | |
1905 | program code without interfering with processing of the text | |
1906 | (by emacs or the language processor) as program code. This | |
1907 | setting happens automatically when allout mode is used in | |
1908 | programming-mode buffers. See `allout-use-mode-specific-leader' | |
1909 | docstring for more detail. | |
c567ac01 | 1910 | PREFIX-PADDING: |
c07583cd KS |
1911 | Spaces or asterisks which separate the PREFIX-LEAD and the |
1912 | bullet, determining the ITEM's DEPTH. | |
1913 | BULLET: A character at the end of the ITEM PREFIX, it must be one of | |
1914 | the characters listed on `allout-plain-bullets-string' or | |
38b2ca53 NR |
1915 | `allout-distinctive-bullets-string'. When creating a TOPIC, |
1916 | plain BULLETs are by default used, according to the DEPTH of the | |
1917 | TOPIC. Choice among the distinctive BULLETs is offered when you | |
1918 | provide a universal argugment \(\\[universal-argument]) to the | |
1919 | TOPIC creation command, or when explictly rebulleting a TOPIC. The | |
1920 | significance of the various distinctive bullets is purely by | |
1921 | convention. See the documentation for the above bullet strings for | |
1922 | more details. | |
c567ac01 | 1923 | EXPOSURE: |
c07583cd KS |
1924 | The state of a TOPIC which determines the on-screen visibility |
1925 | of its OFFSPRING and contained ENTRY text. | |
c567ac01 | 1926 | CONCEALED: |
c07583cd KS |
1927 | TOPICs and ENTRY text whose EXPOSURE is inhibited. Concealed |
1928 | text is represented by \"...\" ellipses. | |
c567ac01 | 1929 | |
c07583cd KS |
1930 | CONCEALED TOPICs are effectively collapsed within an ANCESTOR. |
1931 | CLOSED: A TOPIC whose immediate OFFSPRING and body-text is CONCEALED. | |
1932 | OPEN: A TOPIC that is not CLOSED, though its OFFSPRING or BODY may be." | |
c567ac01 | 1933 | ;;;_ . Code |
1977b8f6 RS |
1934 | (interactive "P") |
1935 | ||
c567ac01 | 1936 | (let* ((active (and (not (equal major-mode 'outline)) |
fd5359c6 | 1937 | (allout-mode-p))) |
353e2ef2 | 1938 | ; Massage universal-arg `toggle' val: |
c567ac01 | 1939 | (toggle (and toggle |
19b84ba3 RS |
1940 | (or (and (listp toggle)(car toggle)) |
1941 | toggle))) | |
a0776d6b | 1942 | ; Activation specifically demanded? |
69027dea | 1943 | (explicit-activation (and toggle |
19b84ba3 | 1944 | (or (symbolp toggle) |
69027dea EZ |
1945 | (and (wholenump toggle) |
1946 | (not (zerop toggle)))))) | |
fd5359c6 MR |
1947 | ;; allout-mode already called once during this complex command? |
1948 | (same-complex-command (eq allout-v18/19-file-var-hack | |
19b84ba3 | 1949 | (car command-history))) |
96b83743 EZ |
1950 | (write-file-hook-var-name (cond ((boundp 'write-file-functions) |
1951 | 'write-file-functions) | |
1952 | ((boundp 'write-file-hooks) | |
1953 | 'write-file-hooks) | |
1954 | (t 'local-write-file-hooks))) | |
19b84ba3 RS |
1955 | do-layout |
1956 | ) | |
c567ac01 | 1957 | |
19b84ba3 | 1958 | ; See comments below re v19.18,.19 bug. |
fd5359c6 | 1959 | (setq allout-v18/19-file-var-hack (car command-history)) |
c567ac01 | 1960 | |
1977b8f6 RS |
1961 | (cond |
1962 | ||
ea18b2b1 | 1963 | ;; Provision for v19.18, 19.19 bug -- |
c567ac01 RS |
1964 | ;; Emacs v 19.18, 19.19 file-var code invokes prop-line-designated |
1965 | ;; modes twice when file is visited. We have to avoid toggling mode | |
1966 | ;; off on second invocation, so we detect it as best we can, and | |
1967 | ;; skip everything. | |
1968 | ((and same-complex-command ; Still in same complex command | |
d82979ea | 1969 | ; as last time `allout-mode' invoked. |
19b84ba3 RS |
1970 | active ; Already activated. |
1971 | (not explicit-activation) ; Prop-line file-vars don't have args. | |
1972 | (string-match "^19.1[89]" ; Bug only known to be in v19.18 and | |
1973 | emacs-version)); 19.19. | |
c567ac01 | 1974 | t) |
353e2ef2 | 1975 | |
19b84ba3 | 1976 | ;; Deactivation: |
c567ac01 | 1977 | ((and (not explicit-activation) |
19b84ba3 RS |
1978 | (or active toggle)) |
1979 | ; Activation not explicitly | |
1980 | ; requested, and either in | |
1981 | ; active state or *de*activation | |
1982 | ; specifically requested: | |
fd5359c6 | 1983 | (setq allout-explicitly-deactivated t) |
c567ac01 | 1984 | |
01fc9422 EZ |
1985 | (allout-do-resumptions) |
1986 | ||
96b83743 | 1987 | (remove-from-invisibility-spec '(allout . t)) |
01fc9422 EZ |
1988 | (remove-hook 'pre-command-hook 'allout-pre-command-business t) |
1989 | (remove-hook 'post-command-hook 'allout-post-command-business t) | |
d8350998 | 1990 | (remove-hook 'before-change-functions 'allout-before-change-handler t) |
01fc9422 EZ |
1991 | (remove-hook 'isearch-mode-end-hook 'allout-isearch-end-handler t) |
1992 | (remove-hook write-file-hook-var-name 'allout-write-file-hook-handler t) | |
1993 | (remove-hook 'auto-save-hook 'allout-auto-save-hook-handler t) | |
1994 | ||
1995 | (remove-overlays (point-min) (point-max) | |
1996 | 'category 'allout-exposure-category) | |
1997 | ||
48bd8440 EZ |
1998 | (setq allout-mode nil) |
1999 | (run-hooks 'allout-mode-deactivate-hook)) | |
1977b8f6 | 2000 | |
19b84ba3 | 2001 | ;; Activation: |
1977b8f6 | 2002 | ((not active) |
fd5359c6 MR |
2003 | (setq allout-explicitly-deactivated nil) |
2004 | (if allout-old-style-prefixes | |
01fc9422 | 2005 | ;; Inhibit all the fancy formatting: |
48bd8440 | 2006 | (allout-add-resumptions '(allout-primary-bullet "*"))) |
19b84ba3 | 2007 | |
01fc9422 | 2008 | (allout-overlay-preparations) ; Doesn't hurt to redo this. |
96b83743 | 2009 | |
d8350998 | 2010 | (allout-infer-header-lead-and-primary-bullet) |
fd5359c6 | 2011 | (allout-infer-body-reindent) |
19b84ba3 | 2012 | |
fd5359c6 | 2013 | (set-allout-regexp) |
48bd8440 EZ |
2014 | (allout-add-resumptions |
2015 | '(allout-encryption-ciphertext-rejection-regexps | |
2016 | allout-line-boundary-regexp | |
2017 | extend) | |
2018 | '(allout-encryption-ciphertext-rejection-regexps | |
2019 | allout-bob-regexp | |
2020 | extend)) | |
19b84ba3 | 2021 | |
01fc9422 | 2022 | ;; Produce map from current version of allout-keybindings-list: |
c07583cd | 2023 | (allout-setup-mode-map) |
01fc9422 | 2024 | (produce-allout-mode-menubar-entries) |
01fc9422 EZ |
2025 | |
2026 | ;; Include on minor-mode-map-alist, if not already there: | |
2027 | (if (not (member '(allout-mode . allout-mode-map) | |
2028 | minor-mode-map-alist)) | |
2029 | (setq minor-mode-map-alist | |
2030 | (cons '(allout-mode . allout-mode-map) | |
2031 | minor-mode-map-alist))) | |
353e2ef2 | 2032 | |
96b83743 | 2033 | (add-to-invisibility-spec '(allout . t)) |
38b2ca53 | 2034 | |
01fc9422 EZ |
2035 | (allout-add-resumptions '(line-move-ignore-invisible t)) |
2036 | (add-hook 'pre-command-hook 'allout-pre-command-business nil t) | |
2037 | (add-hook 'post-command-hook 'allout-post-command-business nil t) | |
d8350998 CY |
2038 | (add-hook 'before-change-functions 'allout-before-change-handler |
2039 | nil t) | |
01fc9422 EZ |
2040 | (add-hook 'isearch-mode-end-hook 'allout-isearch-end-handler nil t) |
2041 | (add-hook write-file-hook-var-name 'allout-write-file-hook-handler | |
2042 | nil t) | |
2043 | (add-hook 'auto-save-hook 'allout-auto-save-hook-handler | |
2044 | nil t) | |
2045 | ||
2046 | ;; Stash auto-fill settings and adjust so custom allout auto-fill | |
2047 | ;; func will be used if auto-fill is active or activated. (The | |
2048 | ;; custom func respects topic headline, maintains hanging-indents, | |
2049 | ;; etc.) | |
2050 | (if (and auto-fill-function (not allout-inhibit-auto-fill)) | |
2051 | ;; allout-auto-fill will use the stashed values and so forth. | |
2052 | (allout-add-resumptions '(auto-fill-function allout-auto-fill))) | |
2053 | (allout-add-resumptions (list 'allout-former-auto-filler | |
2054 | auto-fill-function) | |
2055 | ;; Register allout-auto-fill to be used if | |
2056 | ;; filling is active: | |
2057 | (list 'allout-outside-normal-auto-fill-function | |
2058 | normal-auto-fill-function) | |
2059 | '(normal-auto-fill-function allout-auto-fill) | |
2060 | ;; Paragraphs are broken by topic headlines. | |
2061 | (list 'paragraph-start | |
2062 | (concat paragraph-start "\\|^\\(" | |
2063 | allout-regexp "\\)")) | |
2064 | (list 'paragraph-separate | |
2065 | (concat paragraph-separate "\\|^\\(" | |
2066 | allout-regexp "\\)"))) | |
fd5359c6 | 2067 | (or (assq 'allout-mode minor-mode-alist) |
c567ac01 | 2068 | (setq minor-mode-alist |
aad94676 | 2069 | (cons '(allout-mode " Allout") minor-mode-alist))) |
19b84ba3 | 2070 | |
fd5359c6 | 2071 | (allout-setup-menubar) |
9179616f | 2072 | |
fd5359c6 | 2073 | (if allout-layout |
19b84ba3 | 2074 | (setq do-layout t)) |
1977b8f6 | 2075 | |
48bd8440 EZ |
2076 | (setq allout-mode t) |
2077 | (run-hooks 'allout-mode-hook)) | |
19b84ba3 RS |
2078 | |
2079 | ;; Reactivation: | |
8d118843 | 2080 | ((setq do-layout t) |
fd5359c6 | 2081 | (allout-infer-body-reindent)) |
01fc9422 | 2082 | ) ;; end of activation-mode cases. |
19b84ba3 | 2083 | |
01fc9422 | 2084 | ;; Do auto layout if warranted: |
4034b0e2 LT |
2085 | (let ((use-layout (if (listp allout-layout) |
2086 | allout-layout | |
2087 | allout-default-layout))) | |
2088 | (if (and do-layout | |
2089 | allout-auto-activation | |
2090 | use-layout | |
2091 | (and (not (eq allout-auto-activation 'activate)) | |
2092 | (if (eq allout-auto-activation 'ask) | |
2093 | (if (y-or-n-p (format "Expose %s with layout '%s'? " | |
2094 | (buffer-name) | |
2095 | use-layout)) | |
2096 | t | |
2097 | (message "Skipped %s layout." (buffer-name)) | |
2098 | nil) | |
2099 | t))) | |
2100 | (save-excursion | |
2101 | (message "Adjusting '%s' exposure..." (buffer-name)) | |
2102 | (goto-char 0) | |
2103 | (allout-this-or-next-heading) | |
2104 | (condition-case err | |
2105 | (progn | |
2106 | (apply 'allout-expose-topic (list use-layout)) | |
2107 | (message "Adjusting '%s' exposure... done." (buffer-name))) | |
60c7bbd2 | 2108 | ;; Problem applying exposure -- notify user, but don't |
4034b0e2 LT |
2109 | ;; interrupt, eg, file visit: |
2110 | (error (message "%s" (car (cdr err))) | |
2111 | (sit-for 1)))))) | |
fd5359c6 | 2112 | allout-mode |
c567ac01 | 2113 | ) ; let* |
19b84ba3 | 2114 | ) ; defun |
c07583cd KS |
2115 | |
2116 | (defun allout-setup-mode-map () | |
2117 | "Establish allout-mode bindings." | |
2118 | (setq-default allout-mode-map | |
2119 | (produce-allout-mode-map allout-keybindings-list)) | |
2120 | (setq allout-mode-map | |
2121 | (produce-allout-mode-map allout-keybindings-list)) | |
2122 | (substitute-key-definition 'beginning-of-line | |
2123 | 'allout-beginning-of-line | |
2124 | allout-mode-map global-map) | |
2125 | (substitute-key-definition 'move-beginning-of-line | |
2126 | 'allout-beginning-of-line | |
2127 | allout-mode-map global-map) | |
2128 | (substitute-key-definition 'end-of-line | |
2129 | 'allout-end-of-line | |
2130 | allout-mode-map global-map) | |
2131 | (substitute-key-definition 'move-end-of-line | |
2132 | 'allout-end-of-line | |
2133 | allout-mode-map global-map) | |
2134 | (fset 'allout-mode-map allout-mode-map)) | |
2135 | ||
2136 | ;; ensure that allout-mode-map has some setting even if allout-mode hasn't | |
2137 | ;; been invoked: | |
2138 | (allout-setup-mode-map) | |
2139 | ||
46771bb3 EZ |
2140 | ;;;_ > allout-minor-mode |
2141 | (defalias 'allout-minor-mode 'allout-mode) | |
773e7e48 | 2142 | |
41ff6d0a JB |
2143 | ;;;_ > allout-unload-function |
2144 | (defun allout-unload-function () | |
2145 | "Unload the allout outline library." | |
2146 | (save-current-buffer | |
2147 | (dolist (buffer (buffer-list)) | |
2148 | (set-buffer buffer) | |
2149 | (when allout-mode (allout-mode -1)))) | |
2150 | ;; continue standard unloading | |
2151 | nil) | |
2152 | ||
773e7e48 EZ |
2153 | ;;;_ - Position Assessment |
2154 | ;;;_ > allout-hidden-p (&optional pos) | |
2155 | (defsubst allout-hidden-p (&optional pos) | |
2156 | "Non-nil if the character after point is invisible." | |
da506f05 | 2157 | (eq (get-char-property (or pos (point)) 'invisible) 'allout)) |
773e7e48 | 2158 | |
96b83743 EZ |
2159 | ;;;_ > allout-overlay-insert-in-front-handler (ol after beg end |
2160 | ;;; &optional prelen) | |
2161 | (defun allout-overlay-insert-in-front-handler (ol after beg end | |
2162 | &optional prelen) | |
2a1408fd | 2163 | "Shift the overlay so stuff inserted in front of it is excluded." |
96b83743 | 2164 | (if after |
2a1408fd DN |
2165 | ;; XXX Shouldn't moving the overlay should be unnecessary, if overlay |
2166 | ;; front-advance on the overlay worked as it should? | |
96b83743 EZ |
2167 | (move-overlay ol (1+ beg) (overlay-end ol)))) |
2168 | ;;;_ > allout-overlay-interior-modification-handler (ol after beg end | |
2169 | ;;; &optional prelen) | |
2170 | (defun allout-overlay-interior-modification-handler (ol after beg end | |
2171 | &optional prelen) | |
2172 | "Get confirmation before making arbitrary changes to invisible text. | |
2173 | ||
2174 | We expose the invisible text and ask for confirmation. Refusal or | |
75bcb912 | 2175 | `keyboard-quit' abandons the changes, with keyboard-quit additionally |
96b83743 EZ |
2176 | reclosing the opened text. |
2177 | ||
60c7bbd2 | 2178 | No confirmation is necessary when `inhibit-read-only' is set -- eg, allout |
96b83743 EZ |
2179 | internal functions use this feature cohesively bunch changes." |
2180 | ||
2181 | (when (and (not inhibit-read-only) (not after)) | |
2182 | (let ((start (point)) | |
2183 | (ol-start (overlay-start ol)) | |
2184 | (ol-end (overlay-end ol)) | |
96b83743 EZ |
2185 | first) |
2186 | (goto-char beg) | |
2187 | (while (< (point) end) | |
2188 | (when (allout-hidden-p) | |
2189 | (allout-show-to-offshoot) | |
2190 | (if (allout-hidden-p) | |
2191 | (save-excursion (forward-char 1) | |
2192 | (allout-show-to-offshoot))) | |
2193 | (when (not first) | |
96b83743 EZ |
2194 | (setq first (point)))) |
2195 | (goto-char (if (featurep 'xemacs) | |
2196 | (next-property-change (1+ (point)) nil end) | |
2197 | (next-char-property-change (1+ (point)) end)))) | |
2198 | (when first | |
2199 | (goto-char first) | |
2200 | (condition-case nil | |
2201 | (if (not | |
2202 | (yes-or-no-p | |
2203 | (substitute-command-keys | |
4034b0e2 | 2204 | (concat "Modify concealed text? (\"no\" just aborts," |
96b83743 EZ |
2205 | " \\[keyboard-quit] also reconceals) ")))) |
2206 | (progn (goto-char start) | |
5a0c3f56 | 2207 | (error "Concealed-text change refused"))) |
96b83743 EZ |
2208 | (quit (allout-flag-region ol-start ol-end nil) |
2209 | (allout-flag-region ol-start ol-end t) | |
5a0c3f56 | 2210 | (error "Concealed-text change abandoned, text reconcealed")))) |
96b83743 EZ |
2211 | (goto-char start)))) |
2212 | ;;;_ > allout-before-change-handler (beg end) | |
2213 | (defun allout-before-change-handler (beg end) | |
2214 | "Protect against changes to invisible text. | |
2215 | ||
60c7bbd2 | 2216 | See `allout-overlay-interior-modification-handler' for details." |
d8350998 CY |
2217 | |
2218 | (if (and (allout-mode-p) undo-in-progress (allout-hidden-p)) | |
2219 | (allout-show-to-offshoot)) | |
2220 | ||
01fc9422 EZ |
2221 | ;; allout-overlay-interior-modification-handler on an overlay handles |
2222 | ;; this in other emacs, via `allout-exposure-category's 'modification-hooks. | |
2223 | (when (and (featurep 'xemacs) (allout-mode-p)) | |
2224 | ;; process all of the pending overlays: | |
d8350998 | 2225 | (save-excursion |
f520c6f2 | 2226 | (goto-char beg) |
d8350998 CY |
2227 | (let ((overlay (allout-get-invisibility-overlay))) |
2228 | (allout-overlay-interior-modification-handler | |
2229 | overlay nil beg end nil))))) | |
96b83743 EZ |
2230 | ;;;_ > allout-isearch-end-handler (&optional overlay) |
2231 | (defun allout-isearch-end-handler (&optional overlay) | |
2232 | "Reconcile allout outline exposure on arriving in hidden text after isearch. | |
2233 | ||
2234 | Optional OVERLAY parameter is for when this function is used by | |
2235 | `isearch-open-invisible' overlay property. It is otherwise unused, so this | |
2236 | function can also be used as an `isearch-mode-end-hook'." | |
2237 | ||
2238 | (if (and (allout-mode-p) (allout-hidden-p)) | |
2239 | (allout-show-to-offshoot))) | |
2240 | ||
60c7bbd2 | 2241 | ;;;_ #3 Internal Position State-Tracking -- "allout-recent-*" funcs |
19b84ba3 | 2242 | ;;; All the basic outline functions that directly do string matches to |
1977b8f6 | 2243 | ;;; evaluate heading prefix location set the variables |
fd5359c6 MR |
2244 | ;;; `allout-recent-prefix-beginning' and `allout-recent-prefix-end' |
2245 | ;;; when successful. Functions starting with `allout-recent-' all | |
19b84ba3 RS |
2246 | ;;; use this state, providing the means to avoid redundant searches |
2247 | ;;; for just-established data. This optimization can provide | |
2248 | ;;; significant speed improvement, but it must be employed carefully. | |
fd5359c6 MR |
2249 | ;;;_ = allout-recent-prefix-beginning |
2250 | (defvar allout-recent-prefix-beginning 0 | |
c567ac01 | 2251 | "Buffer point of the start of the last topic prefix encountered.") |
fd5359c6 MR |
2252 | (make-variable-buffer-local 'allout-recent-prefix-beginning) |
2253 | ;;;_ = allout-recent-prefix-end | |
2254 | (defvar allout-recent-prefix-end 0 | |
c567ac01 | 2255 | "Buffer point of the end of the last topic prefix encountered.") |
fd5359c6 | 2256 | (make-variable-buffer-local 'allout-recent-prefix-end) |
d8350998 CY |
2257 | ;;;_ = allout-recent-depth |
2258 | (defvar allout-recent-depth 0 | |
2259 | "Depth of the last topic prefix encountered.") | |
2260 | (make-variable-buffer-local 'allout-recent-depth) | |
fd5359c6 MR |
2261 | ;;;_ = allout-recent-end-of-subtree |
2262 | (defvar allout-recent-end-of-subtree 0 | |
2265e017 | 2263 | "Buffer point last returned by `allout-end-of-current-subtree'.") |
fd5359c6 | 2264 | (make-variable-buffer-local 'allout-recent-end-of-subtree) |
d8350998 CY |
2265 | ;;;_ > allout-prefix-data () |
2266 | (defsubst allout-prefix-data () | |
2267 | "Register allout-prefix state data. | |
fd5359c6 | 2268 | |
75bcb912 JB |
2269 | For reference by `allout-recent' funcs. Return |
2270 | the new value of `allout-recent-prefix-beginning'." | |
42c3e51c | 2271 | (setq allout-recent-prefix-end (or (match-end 1) (match-end 2) (match-end 3)) |
d8350998 | 2272 | allout-recent-prefix-beginning (or (match-beginning 1) |
42c3e51c KM |
2273 | (match-beginning 2) |
2274 | (match-beginning 3)) | |
d8350998 CY |
2275 | allout-recent-depth (max 1 (- allout-recent-prefix-end |
2276 | allout-recent-prefix-beginning | |
2277 | allout-header-subtraction))) | |
2278 | allout-recent-prefix-beginning) | |
2279 | ;;;_ > nullify-allout-prefix-data () | |
2280 | (defsubst nullify-allout-prefix-data () | |
2281 | "Mark allout prefix data as being uninformative." | |
2282 | (setq allout-recent-prefix-end (point) | |
2283 | allout-recent-prefix-beginning (point) | |
2284 | allout-recent-depth 0) | |
2285 | allout-recent-prefix-beginning) | |
fd5359c6 | 2286 | ;;;_ > allout-recent-depth () |
d8350998 | 2287 | (defsubst allout-recent-depth () |
19b84ba3 | 2288 | "Return depth of last heading encountered by an outline maneuvering function. |
c567ac01 RS |
2289 | |
2290 | All outline functions which directly do string matches to assess | |
2265e017 MR |
2291 | headings set the variables `allout-recent-prefix-beginning' and |
2292 | `allout-recent-prefix-end' if successful. This function uses those settings | |
c567ac01 RS |
2293 | to return the current depth." |
2294 | ||
d8350998 | 2295 | allout-recent-depth) |
fd5359c6 | 2296 | ;;;_ > allout-recent-prefix () |
d8350998 | 2297 | (defsubst allout-recent-prefix () |
2265e017 | 2298 | "Like `allout-recent-depth', but returns text of last encountered prefix. |
c567ac01 RS |
2299 | |
2300 | All outline functions which directly do string matches to assess | |
2265e017 MR |
2301 | headings set the variables `allout-recent-prefix-beginning' and |
2302 | `allout-recent-prefix-end' if successful. This function uses those settings | |
d8350998 CY |
2303 | to return the current prefix." |
2304 | (buffer-substring-no-properties allout-recent-prefix-beginning | |
2305 | allout-recent-prefix-end)) | |
fd5359c6 MR |
2306 | ;;;_ > allout-recent-bullet () |
2307 | (defmacro allout-recent-bullet () | |
2308 | "Like allout-recent-prefix, but returns bullet of last encountered prefix. | |
c567ac01 RS |
2309 | |
2310 | All outline functions which directly do string matches to assess | |
2265e017 MR |
2311 | headings set the variables `allout-recent-prefix-beginning' and |
2312 | `allout-recent-prefix-end' if successful. This function uses those settings | |
c567ac01 | 2313 | to return the current depth of the most recently matched topic." |
d8350998 CY |
2314 | '(buffer-substring-no-properties (1- allout-recent-prefix-end) |
2315 | allout-recent-prefix-end)) | |
c567ac01 | 2316 | |
19b84ba3 | 2317 | ;;;_ #4 Navigation |
c567ac01 | 2318 | |
46771bb3 | 2319 | ;;;_ - Position Assessment |
c567ac01 | 2320 | ;;;_ : Location Predicates |
ede4ac6a KS |
2321 | ;;;_ > allout-do-doublecheck () |
2322 | (defsubst allout-do-doublecheck () | |
2323 | "True if current item conditions qualify for checking on topic aberrance." | |
2324 | (and | |
60c7bbd2 | 2325 | ;; presume integrity of outline and yanked content during yank -- necessary |
ede4ac6a | 2326 | ;; to allow for level disparity of yank location and yanked text: |
498a6493 | 2327 | (not allout-inhibit-aberrance-doublecheck) |
ede4ac6a KS |
2328 | ;; allout-doublecheck-at-and-shallower is ceiling for doublecheck: |
2329 | (<= allout-recent-depth allout-doublecheck-at-and-shallower))) | |
2330 | ;;;_ > allout-aberrant-container-p () | |
d8350998 CY |
2331 | (defun allout-aberrant-container-p () |
2332 | "True if topic, or next sibling with children, contains them discontinuously. | |
2333 | ||
2334 | Discontinuous means an immediate offspring that is nested more | |
2335 | than one level deeper than the topic. | |
2336 | ||
2337 | If topic has no offspring, then the next sibling with offspring will | |
2338 | determine whether or not this one is determined to be aberrant. | |
2339 | ||
2340 | If true, then the allout-recent-* settings are calibrated on the | |
2341 | offspring that qaulifies it as aberrant, ie with depth that | |
2342 | exceeds the topic by more than one." | |
2343 | ||
2344 | ;; This is most clearly understood when considering standard-prefix-leader | |
2345 | ;; low-level topics, which can all too easily match text not intended as | |
2346 | ;; headers. For example, any line with a leading '.' or '*' and lacking a | |
2347 | ;; following bullet qualifies without this protection. (A sequence of | |
2348 | ;; them can occur naturally, eg a typical textual bullet list.) We | |
2349 | ;; disqualify such low-level sequences when they are followed by a | |
2350 | ;; discontinuously contained child, inferring that the sequences are not | |
2351 | ;; actually connected with their prospective context. | |
2352 | ||
2353 | (let ((depth (allout-depth)) | |
2354 | (start-point (point)) | |
2355 | done aberrant) | |
2a1408fd DN |
2356 | (save-match-data |
2357 | (save-excursion | |
2358 | (while (and (not done) | |
2359 | (re-search-forward allout-line-boundary-regexp nil 0)) | |
2360 | (allout-prefix-data) | |
2361 | (goto-char allout-recent-prefix-beginning) | |
2362 | (cond | |
8dd59f01 | 2363 | ;; sibling -- continue: |
2a1408fd | 2364 | ((eq allout-recent-depth depth)) |
8dd59f01 | 2365 | ;; first offspring is excessive -- aberrant: |
2a1408fd DN |
2366 | ((> allout-recent-depth (1+ depth)) |
2367 | (setq done t aberrant t)) | |
8dd59f01 | 2368 | ;; next non-sibling is lower-depth -- not aberrant: |
2a1408fd | 2369 | (t (setq done t)))))) |
d8350998 CY |
2370 | (if aberrant |
2371 | aberrant | |
2372 | (goto-char start-point) | |
2373 | ;; recalibrate allout-recent-* | |
2374 | (allout-depth) | |
2375 | nil))) | |
ede4ac6a KS |
2376 | ;;;_ > allout-on-current-heading-p () |
2377 | (defun allout-on-current-heading-p () | |
2378 | "Return non-nil if point is on current visible topics' header line. | |
2379 | ||
2380 | Actually, returns prefix beginning point." | |
2381 | (save-excursion | |
2382 | (allout-beginning-of-current-line) | |
2a1408fd DN |
2383 | (save-match-data |
2384 | (and (looking-at allout-regexp) | |
2385 | (allout-prefix-data) | |
2386 | (or (not (allout-do-doublecheck)) | |
2387 | (not (allout-aberrant-container-p))))))) | |
ede4ac6a KS |
2388 | ;;;_ > allout-on-heading-p () |
2389 | (defalias 'allout-on-heading-p 'allout-on-current-heading-p) | |
2390 | ;;;_ > allout-e-o-prefix-p () | |
2391 | (defun allout-e-o-prefix-p () | |
2392 | "True if point is located where current topic prefix ends, heading begins." | |
2a1408fd DN |
2393 | (and (save-match-data |
2394 | (save-excursion (let ((inhibit-field-text-motion t)) | |
2395 | (beginning-of-line)) | |
2396 | (looking-at allout-regexp)) | |
2397 | (= (point) (save-excursion (allout-end-of-prefix)(point)))))) | |
c567ac01 | 2398 | ;;;_ : Location attributes |
fd5359c6 | 2399 | ;;;_ > allout-depth () |
96b83743 EZ |
2400 | (defun allout-depth () |
2401 | "Return depth of topic most immediately containing point. | |
2402 | ||
42c3e51c KM |
2403 | Does not do doublecheck for aberrant topic header. |
2404 | ||
96b83743 EZ |
2405 | Return zero if point is not within any topic. |
2406 | ||
2407 | Like `allout-current-depth', but respects hidden as well as visible topics." | |
9179616f | 2408 | (save-excursion |
96b83743 EZ |
2409 | (let ((start-point (point))) |
2410 | (if (and (allout-goto-prefix) | |
2411 | (not (< start-point (point)))) | |
d8350998 | 2412 | allout-recent-depth |
96b83743 | 2413 | (progn |
d8350998 CY |
2414 | ;; Oops, no prefix, nullify it: |
2415 | (nullify-allout-prefix-data) | |
96b83743 EZ |
2416 | ;; ... and return 0: |
2417 | 0))))) | |
fd5359c6 | 2418 | ;;;_ > allout-current-depth () |
96b83743 EZ |
2419 | (defun allout-current-depth () |
2420 | "Return depth of visible topic most immediately containing point. | |
2421 | ||
2422 | Return zero if point is not within any topic." | |
2423 | (save-excursion | |
2424 | (if (allout-back-to-current-heading) | |
2425 | (max 1 | |
2426 | (- allout-recent-prefix-end | |
2427 | allout-recent-prefix-beginning | |
2428 | allout-header-subtraction)) | |
2429 | 0))) | |
fd5359c6 MR |
2430 | ;;;_ > allout-get-current-prefix () |
2431 | (defun allout-get-current-prefix () | |
c567ac01 | 2432 | "Topic prefix of the current topic." |
1977b8f6 | 2433 | (save-excursion |
fd5359c6 MR |
2434 | (if (allout-goto-prefix) |
2435 | (allout-recent-prefix)))) | |
2436 | ;;;_ > allout-get-bullet () | |
2437 | (defun allout-get-bullet () | |
c567ac01 | 2438 | "Return bullet of containing topic (visible or not)." |
1977b8f6 | 2439 | (save-excursion |
fd5359c6 MR |
2440 | (and (allout-goto-prefix) |
2441 | (allout-recent-bullet)))) | |
2442 | ;;;_ > allout-current-bullet () | |
2443 | (defun allout-current-bullet () | |
c567ac01 | 2444 | "Return bullet of current (visible) topic heading, or none if none found." |
96b83743 | 2445 | (condition-case nil |
1977b8f6 | 2446 | (save-excursion |
fd5359c6 | 2447 | (allout-back-to-current-heading) |
d8350998 CY |
2448 | (buffer-substring-no-properties (- allout-recent-prefix-end 1) |
2449 | allout-recent-prefix-end)) | |
1977b8f6 | 2450 | ;; Quick and dirty provision, ostensibly for missing bullet: |
d8350998 | 2451 | (args-out-of-range nil)) |
1977b8f6 | 2452 | ) |
fd5359c6 MR |
2453 | ;;;_ > allout-get-prefix-bullet (prefix) |
2454 | (defun allout-get-prefix-bullet (prefix) | |
c567ac01 | 2455 | "Return the bullet of the header prefix string PREFIX." |
1977b8f6 RS |
2456 | ;; Doesn't make sense if we're old-style prefixes, but this just |
2457 | ;; oughtn't be called then, so forget about it... | |
fd5359c6 | 2458 | (if (string-match allout-regexp prefix) |
d8350998 | 2459 | (substring prefix (1- (match-end 2)) (match-end 2)))) |
fd5359c6 MR |
2460 | ;;;_ > allout-sibling-index (&optional depth) |
2461 | (defun allout-sibling-index (&optional depth) | |
9179616f DL |
2462 | "Item number of this prospective topic among its siblings. |
2463 | ||
e126900f | 2464 | If optional arg DEPTH is greater than current depth, then we're |
9179616f DL |
2465 | opening a new level, and return 0. |
2466 | ||
2467 | If less than this depth, ascend to that depth and count..." | |
2468 | ||
2469 | (save-excursion | |
2470 | (cond ((and depth (<= depth 0) 0)) | |
65e7eb3d | 2471 | ((or (null depth) (= depth (allout-depth))) |
9179616f | 2472 | (let ((index 1)) |
d8350998 | 2473 | (while (allout-previous-sibling allout-recent-depth nil) |
9179616f DL |
2474 | (setq index (1+ index))) |
2475 | index)) | |
d8350998 | 2476 | ((< depth allout-recent-depth) |
fd5359c6 MR |
2477 | (allout-ascend-to-depth depth) |
2478 | (allout-sibling-index)) | |
9179616f | 2479 | (0)))) |
fd5359c6 MR |
2480 | ;;;_ > allout-topic-flat-index () |
2481 | (defun allout-topic-flat-index () | |
9179616f DL |
2482 | "Return a list indicating point's numeric section.subsect.subsubsect... |
2483 | Outermost is first." | |
fd5359c6 MR |
2484 | (let* ((depth (allout-depth)) |
2485 | (next-index (allout-sibling-index depth)) | |
9179616f DL |
2486 | (rev-sibls nil)) |
2487 | (while (> next-index 0) | |
2488 | (setq rev-sibls (cons next-index rev-sibls)) | |
2489 | (setq depth (1- depth)) | |
fd5359c6 | 2490 | (setq next-index (allout-sibling-index depth))) |
9179616f DL |
2491 | rev-sibls) |
2492 | ) | |
1977b8f6 | 2493 | |
96b83743 EZ |
2494 | ;;;_ - Navigation routines |
2495 | ;;;_ > allout-beginning-of-current-line () | |
2496 | (defun allout-beginning-of-current-line () | |
2497 | "Like beginning of line, but to visible text." | |
2498 | ||
5204800f NR |
2499 | ;; This combination of move-beginning-of-line and beginning-of-line is |
2500 | ;; deliberate, but the (beginning-of-line) may now be superfluous. | |
0458fa80 EZ |
2501 | (let ((inhibit-field-text-motion t)) |
2502 | (move-beginning-of-line 1) | |
96b83743 | 2503 | (beginning-of-line) |
0458fa80 EZ |
2504 | (while (and (not (bobp)) (or (not (bolp)) (allout-hidden-p))) |
2505 | (beginning-of-line) | |
2506 | (if (or (allout-hidden-p) (not (bolp))) | |
2507 | (forward-char -1))))) | |
96b83743 EZ |
2508 | ;;;_ > allout-end-of-current-line () |
2509 | (defun allout-end-of-current-line () | |
2510 | "Move to the end of line, past concealed text if any." | |
ea18b2b1 | 2511 | ;; XXX This is for symmetry with `allout-beginning-of-current-line' -- |
96b83743 EZ |
2512 | ;; `move-end-of-line' doesn't suffer the same problem as |
2513 | ;; `move-beginning-of-line'. | |
0458fa80 | 2514 | (let ((inhibit-field-text-motion t)) |
96b83743 | 2515 | (end-of-line) |
0458fa80 EZ |
2516 | (while (allout-hidden-p) |
2517 | (end-of-line) | |
2518 | (if (allout-hidden-p) (forward-char 1))))) | |
48bd8440 EZ |
2519 | ;;;_ > allout-beginning-of-line () |
2520 | (defun allout-beginning-of-line () | |
2521 | "Beginning-of-line with `allout-beginning-of-line-cycles' behavior, if set." | |
2522 | ||
2523 | (interactive) | |
2524 | ||
2525 | (if (or (not allout-beginning-of-line-cycles) | |
2526 | (not (equal last-command this-command))) | |
2a1408fd DN |
2527 | (progn |
2528 | (if (and (not (bolp)) | |
2529 | (allout-hidden-p (1- (point)))) | |
2530 | (goto-char (previous-single-char-property-change | |
2531 | (1- (point)) 'invisible))) | |
2532 | (move-beginning-of-line 1)) | |
d8350998 CY |
2533 | (allout-depth) |
2534 | (let ((beginning-of-body | |
2535 | (save-excursion | |
ede4ac6a | 2536 | (while (and (allout-do-doublecheck) |
d8350998 CY |
2537 | (allout-aberrant-container-p) |
2538 | (allout-previous-visible-heading 1))) | |
2539 | (allout-beginning-of-current-entry) | |
2540 | (point)))) | |
48bd8440 | 2541 | (cond ((= (current-column) 0) |
d8350998 | 2542 | (goto-char beginning-of-body)) |
48bd8440 EZ |
2543 | ((< (point) beginning-of-body) |
2544 | (allout-beginning-of-current-line)) | |
2545 | ((= (point) beginning-of-body) | |
2546 | (goto-char (allout-current-bullet-pos))) | |
2547 | (t (allout-beginning-of-current-line) | |
2548 | (if (< (point) beginning-of-body) | |
2549 | ;; we were on the headline after its start: | |
d8350998 | 2550 | (goto-char beginning-of-body))))))) |
48bd8440 EZ |
2551 | ;;;_ > allout-end-of-line () |
2552 | (defun allout-end-of-line () | |
2553 | "End-of-line with `allout-end-of-line-cycles' behavior, if set." | |
2554 | ||
2555 | (interactive) | |
2556 | ||
2557 | (if (or (not allout-end-of-line-cycles) | |
2558 | (not (equal last-command this-command))) | |
2559 | (allout-end-of-current-line) | |
2560 | (let ((end-of-entry (save-excursion | |
2561 | (allout-end-of-entry) | |
2562 | (point)))) | |
2563 | (cond ((not (eolp)) | |
2564 | (allout-end-of-current-line)) | |
2565 | ((or (allout-hidden-p) (save-excursion | |
2566 | (forward-char -1) | |
2567 | (allout-hidden-p))) | |
2568 | (allout-back-to-current-heading) | |
2569 | (allout-show-current-entry) | |
d8350998 | 2570 | (allout-show-children) |
48bd8440 EZ |
2571 | (allout-end-of-entry)) |
2572 | ((>= (point) end-of-entry) | |
2573 | (allout-back-to-current-heading) | |
2574 | (allout-end-of-current-line)) | |
2a1408fd DN |
2575 | (t |
2576 | (if (not (and transient-mark-mode mark-active)) | |
2577 | (push-mark)) | |
2578 | (allout-end-of-entry)))))) | |
fd5359c6 MR |
2579 | ;;;_ > allout-next-heading () |
2580 | (defsubst allout-next-heading () | |
c07583cd | 2581 | "Move to the heading for the topic (possibly invisible) after this one. |
c567ac01 | 2582 | |
d8350998 | 2583 | Returns the location of the heading, or nil if none found. |
c567ac01 | 2584 | |
75bcb912 | 2585 | We skip anomalous low-level topics, a la `allout-aberrant-container-p'." |
2a1408fd DN |
2586 | (save-match-data |
2587 | ||
2588 | (if (looking-at allout-regexp) | |
2589 | (forward-char 1)) | |
2590 | ||
2591 | (when (re-search-forward allout-line-boundary-regexp nil 0) | |
2592 | (allout-prefix-data) | |
42c3e51c KM |
2593 | (goto-char allout-recent-prefix-beginning) |
2594 | (while (not (bolp)) | |
2595 | (forward-char -1)) | |
2a1408fd DN |
2596 | (and (allout-do-doublecheck) |
2597 | ;; this will set allout-recent-* on the first non-aberrant topic, | |
2598 | ;; whether it's the current one or one that disqualifies it: | |
2599 | (allout-aberrant-container-p)) | |
42c3e51c | 2600 | ;; this may or may not be the same as above depending on doublecheck: |
2a1408fd | 2601 | (goto-char allout-recent-prefix-beginning)))) |
96b83743 | 2602 | ;;;_ > allout-this-or-next-heading |
fd5359c6 | 2603 | (defun allout-this-or-next-heading () |
8d118843 | 2604 | "Position cursor on current or next heading." |
fd5359c6 MR |
2605 | ;; A throwaway non-macro that is defined after allout-next-heading |
2606 | ;; and usable by allout-mode. | |
d8350998 | 2607 | (if (not (allout-goto-prefix-doublechecked)) (allout-next-heading))) |
fd5359c6 | 2608 | ;;;_ > allout-previous-heading () |
8a559d4e | 2609 | (defun allout-previous-heading () |
c07583cd | 2610 | "Move to the prior (possibly invisible) heading line. |
c567ac01 | 2611 | |
d8350998 CY |
2612 | Return the location of the beginning of the heading, or nil if not found. |
2613 | ||
75bcb912 | 2614 | We skip anomalous low-level topics, a la `allout-aberrant-container-p'." |
d8350998 CY |
2615 | |
2616 | (if (bobp) | |
2617 | nil | |
d8350998 | 2618 | (let ((start-point (point))) |
ede4ac6a | 2619 | ;; allout-goto-prefix-doublechecked calls us, so we can't use it here. |
d8350998 | 2620 | (allout-goto-prefix) |
2a1408fd DN |
2621 | (save-match-data |
2622 | (when (or (re-search-backward allout-line-boundary-regexp nil 0) | |
2623 | (looking-at allout-bob-regexp)) | |
2624 | (goto-char (allout-prefix-data)) | |
2625 | (if (and (allout-do-doublecheck) | |
2626 | (allout-aberrant-container-p)) | |
2627 | (or (allout-previous-heading) | |
2628 | (and (goto-char start-point) | |
2629 | ;; recalibrate allout-recent-*: | |
2630 | (allout-depth) | |
2631 | nil)) | |
2632 | (point))))))) | |
96b83743 EZ |
2633 | ;;;_ > allout-get-invisibility-overlay () |
2634 | (defun allout-get-invisibility-overlay () | |
2635 | "Return the overlay at point that dictates allout invisibility." | |
2636 | (let ((overlays (overlays-at (point))) | |
2637 | got) | |
2638 | (while (and overlays (not got)) | |
2639 | (if (equal (overlay-get (car overlays) 'invisible) 'allout) | |
d8350998 CY |
2640 | (setq got (car overlays)) |
2641 | (pop overlays))) | |
96b83743 EZ |
2642 | got)) |
2643 | ;;;_ > allout-back-to-visible-text () | |
2644 | (defun allout-back-to-visible-text () | |
2645 | "Move to most recent prior character that is visible, and return point." | |
2646 | (if (allout-hidden-p) | |
2647 | (goto-char (overlay-start (allout-get-invisibility-overlay)))) | |
2648 | (point)) | |
c567ac01 RS |
2649 | |
2650 | ;;;_ - Subtree Charting | |
2651 | ;;;_ " These routines either produce or assess charts, which are | |
2652 | ;;; nested lists of the locations of topics within a subtree. | |
2653 | ;;; | |
d8350998 CY |
2654 | ;;; Charts enable efficient subtree navigation by providing a reusable basis |
2655 | ;;; for elaborate, compound assessment and adjustment of a subtree. | |
c567ac01 | 2656 | |
48bd8440 EZ |
2657 | ;;;_ > allout-chart-subtree (&optional levels visible orig-depth prev-depth) |
2658 | (defun allout-chart-subtree (&optional levels visible orig-depth prev-depth) | |
19b84ba3 RS |
2659 | "Produce a location \"chart\" of subtopics of the containing topic. |
2660 | ||
c07583cd | 2661 | Optional argument LEVELS specifies a depth limit (relative to start |
65e7eb3d | 2662 | depth) for the chart. Null LEVELS means no limit. |
48bd8440 EZ |
2663 | |
2664 | When optional argument VISIBLE is non-nil, the chart includes | |
2665 | only the visible subelements of the charted subjects. | |
2666 | ||
65e7eb3d | 2667 | The remaining optional args are for internal use by the function. |
d82979ea EZ |
2668 | |
2669 | Point is left at the end of the subtree. | |
19b84ba3 | 2670 | |
d82979ea | 2671 | Charts are used to capture outline structure, so that outline-altering |
19b84ba3 RS |
2672 | routines need assess the structure only once, and then use the chart |
2673 | for their elaborate manipulations. | |
2674 | ||
d8350998 CY |
2675 | The chart entries for the topics are in reverse order, so the |
2676 | last topic is listed first. The entry for each topic consists of | |
2677 | an integer indicating the point at the beginning of the topic | |
2678 | prefix. Charts for offspring consists of a list containing, | |
2679 | recursively, the charts for the respective subtopics. The chart | |
2680 | for a topics' offspring precedes the entry for the topic itself. | |
19b84ba3 | 2681 | |
d82979ea EZ |
2682 | The other function parameters are for internal recursion, and should |
2683 | not be specified by external callers. ORIG-DEPTH is depth of topic at | |
2684 | starting point, and PREV-DEPTH is depth of prior topic." | |
19b84ba3 | 2685 | |
353e2ef2 | 2686 | (let ((original (not orig-depth)) ; `orig-depth' set only in recursion. |
19b84ba3 RS |
2687 | chart curr-depth) |
2688 | ||
2689 | (if original ; Just starting? | |
2690 | ; Register initial settings and | |
2691 | ; position to first offspring: | |
fd5359c6 | 2692 | (progn (setq orig-depth (allout-depth)) |
19b84ba3 | 2693 | (or prev-depth (setq prev-depth (1+ orig-depth))) |
48bd8440 EZ |
2694 | (if visible |
2695 | (allout-next-visible-heading 1) | |
2696 | (allout-next-heading)))) | |
c567ac01 | 2697 | |
19b84ba3 RS |
2698 | ;; Loop over the current levels' siblings. Besides being more |
2699 | ;; efficient than tail-recursing over a level, it avoids exceeding | |
e126900f | 2700 | ;; the typically quite constrained Emacs max-lisp-eval-depth. |
9179616f | 2701 | ;; |
19b84ba3 RS |
2702 | ;; Probably would speed things up to implement loop-based stack |
2703 | ;; operation rather than recursing for lower levels. Bah. | |
9179616f | 2704 | |
c567ac01 | 2705 | (while (and (not (eobp)) |
19b84ba3 | 2706 | ; Still within original topic? |
d8350998 | 2707 | (< orig-depth (setq curr-depth allout-recent-depth)) |
19b84ba3 RS |
2708 | (cond ((= prev-depth curr-depth) |
2709 | ;; Register this one and move on: | |
d8350998 | 2710 | (setq chart (cons allout-recent-prefix-beginning chart)) |
19b84ba3 | 2711 | (if (and levels (<= levels 1)) |
60c7bbd2 | 2712 | ;; At depth limit -- skip sublevels: |
fd5359c6 | 2713 | (or (allout-next-sibling curr-depth) |
60c7bbd2 | 2714 | ;; or no more siblings -- proceed to |
19b84ba3 | 2715 | ;; next heading at lesser depth: |
e6a9aec7 | 2716 | (while (and (<= curr-depth |
d8350998 | 2717 | allout-recent-depth) |
48bd8440 EZ |
2718 | (if visible |
2719 | (allout-next-visible-heading 1) | |
2720 | (allout-next-heading))))) | |
2721 | (if visible | |
2722 | (allout-next-visible-heading 1) | |
2723 | (allout-next-heading)))) | |
19b84ba3 RS |
2724 | |
2725 | ((and (< prev-depth curr-depth) | |
2726 | (or (not levels) | |
2727 | (> levels 0))) | |
2728 | ;; Recurse on deeper level of curr topic: | |
2729 | (setq chart | |
fd5359c6 | 2730 | (cons (allout-chart-subtree (and levels |
19b84ba3 | 2731 | (1- levels)) |
48bd8440 EZ |
2732 | visible |
2733 | orig-depth | |
2734 | curr-depth) | |
19b84ba3 RS |
2735 | chart)) |
2736 | ;; ... then continue with this one. | |
2737 | ) | |
2738 | ||
2739 | ;; ... else nil if we've ascended back to prev-depth. | |
2740 | ||
2741 | ))) | |
2742 | ||
2743 | (if original ; We're at the last sibling on | |
2744 | ; the original level. Position | |
2745 | ; to the end of it: | |
c567ac01 | 2746 | (progn (and (not (eobp)) (forward-char -1)) |
96b83743 EZ |
2747 | (and (= (preceding-char) ?\n) |
2748 | (= (aref (buffer-substring (max 1 (- (point) 3)) | |
2749 | (point)) | |
2750 | 1) | |
2751 | ?\n) | |
19b84ba3 | 2752 | (forward-char -1)) |
fd5359c6 | 2753 | (setq allout-recent-end-of-subtree (point)))) |
353e2ef2 | 2754 | |
c567ac01 RS |
2755 | chart ; (nreverse chart) not necessary, |
2756 | ; and maybe not preferable. | |
2757 | )) | |
fd5359c6 MR |
2758 | ;;;_ > allout-chart-siblings (&optional start end) |
2759 | (defun allout-chart-siblings (&optional start end) | |
c567ac01 | 2760 | "Produce a list of locations of this and succeeding sibling topics. |
fd5359c6 | 2761 | Effectively a top-level chart of siblings. See `allout-chart-subtree' |
c567ac01 RS |
2762 | for an explanation of charts." |
2763 | (save-excursion | |
d8350998 CY |
2764 | (when (allout-goto-prefix-doublechecked) |
2765 | (let ((chart (list (point)))) | |
2766 | (while (allout-next-sibling) | |
2767 | (setq chart (cons (point) chart))) | |
2768 | (if chart (setq chart (nreverse chart))))))) | |
fd5359c6 MR |
2769 | ;;;_ > allout-chart-to-reveal (chart depth) |
2770 | (defun allout-chart-to-reveal (chart depth) | |
c567ac01 | 2771 | |
19b84ba3 | 2772 | "Return a flat list of hidden points in subtree CHART, up to DEPTH. |
c567ac01 | 2773 | |
65e7eb3d EZ |
2774 | If DEPTH is nil, include hidden points at any depth. |
2775 | ||
19b84ba3 | 2776 | Note that point can be left at any of the points on chart, or at the |
c567ac01 RS |
2777 | start point." |
2778 | ||
2779 | (let (result here) | |
65e7eb3d | 2780 | (while (and (or (null depth) (> depth 0)) |
c567ac01 RS |
2781 | chart) |
2782 | (setq here (car chart)) | |
2783 | (if (listp here) | |
65e7eb3d EZ |
2784 | (let ((further (allout-chart-to-reveal here (if (null depth) |
2785 | depth | |
2786 | (1- depth))))) | |
60c7bbd2 | 2787 | ;; We're on the start of a subtree -- recurse with it, if there's |
c567ac01 RS |
2788 | ;; more depth to go: |
2789 | (if further (setq result (append further result))) | |
2790 | (setq chart (cdr chart))) | |
2791 | (goto-char here) | |
96b83743 | 2792 | (if (allout-hidden-p) |
c567ac01 RS |
2793 | (setq result (cons here result))) |
2794 | (setq chart (cdr chart)))) | |
2795 | result)) | |
fd5359c6 | 2796 | ;;;_ X allout-chart-spec (chart spec &optional exposing) |
e126900f | 2797 | ;; (defun allout-chart-spec (chart spec &optional exposing) |
c07583cd | 2798 | ;; "Not yet (if ever) implemented. |
e126900f JB |
2799 | |
2800 | ;; Produce exposure directives given topic/subtree CHART and an exposure SPEC. | |
2801 | ||
2802 | ;; Exposure spec indicates the locations to be exposed and the prescribed | |
2803 | ;; exposure status. Optional arg EXPOSING is an integer, with 0 | |
2804 | ;; indicating pending concealment, anything higher indicating depth to | |
2805 | ;; which subtopic headers should be exposed, and negative numbers | |
2806 | ;; indicating (negative of) the depth to which subtopic headers and | |
2807 | ;; bodies should be exposed. | |
2808 | ||
2809 | ;; The produced list can have two types of entries. Bare numbers | |
2810 | ;; indicate points in the buffer where topic headers that should be | |
2811 | ;; exposed reside. | |
2812 | ||
2813 | ;; - bare negative numbers indicates that the topic starting at the | |
2814 | ;; point which is the negative of the number should be opened, | |
2815 | ;; including their entries. | |
2816 | ;; - bare positive values indicate that this topic header should be | |
2817 | ;; opened. | |
2818 | ;; - Lists signify the beginning and end points of regions that should | |
c07583cd | 2819 | ;; be flagged, and the flag to employ. (For concealment: `(\?r)', and |
e126900f JB |
2820 | ;; exposure:" |
2821 | ;; (while spec | |
2822 | ;; (cond ((listp spec) | |
2823 | ;; ) | |
2824 | ;; ) | |
2825 | ;; (setq spec (cdr spec))) | |
2826 | ;; ) | |
c567ac01 RS |
2827 | |
2828 | ;;;_ - Within Topic | |
fd5359c6 MR |
2829 | ;;;_ > allout-goto-prefix () |
2830 | (defun allout-goto-prefix () | |
9179616f | 2831 | "Put point at beginning of immediately containing outline topic. |
19b84ba3 | 2832 | |
9179616f | 2833 | Goes to most immediate subsequent topic if none immediately containing. |
19b84ba3 RS |
2834 | |
2835 | Not sensitive to topic visibility. | |
c567ac01 | 2836 | |
d27081f5 | 2837 | Returns the point at the beginning of the prefix, or nil if none." |
c567ac01 | 2838 | |
2a1408fd DN |
2839 | (save-match-data |
2840 | (let (done) | |
2841 | (while (and (not done) | |
2842 | (search-backward "\n" nil 1)) | |
2843 | (forward-char 1) | |
2844 | (if (looking-at allout-regexp) | |
2845 | (setq done (allout-prefix-data)) | |
2846 | (forward-char -1))) | |
2847 | (if (bobp) | |
2848 | (cond ((looking-at allout-regexp) | |
2849 | (allout-prefix-data)) | |
2850 | ((allout-next-heading)) | |
2851 | (done)) | |
2852 | done)))) | |
d8350998 CY |
2853 | ;;;_ > allout-goto-prefix-doublechecked () |
2854 | (defun allout-goto-prefix-doublechecked () | |
2855 | "Put point at beginning of immediately containing outline topic. | |
2856 | ||
c07583cd | 2857 | Like `allout-goto-prefix', but shallow topics (according to |
65e7eb3d EZ |
2858 | `allout-doublecheck-at-and-shallower') are checked and |
2859 | disqualified for child containment discontinuity, according to | |
2860 | `allout-aberrant-container-p'." | |
ede4ac6a KS |
2861 | (if (allout-goto-prefix) |
2862 | (if (and (allout-do-doublecheck) | |
2863 | (allout-aberrant-container-p)) | |
2864 | (allout-previous-heading) | |
2865 | (point)))) | |
d8350998 | 2866 | |
fd5359c6 MR |
2867 | ;;;_ > allout-end-of-prefix () |
2868 | (defun allout-end-of-prefix (&optional ignore-decorations) | |
19b84ba3 RS |
2869 | "Position cursor at beginning of header text. |
2870 | ||
2871 | If optional IGNORE-DECORATIONS is non-nil, put just after bullet, | |
2872 | otherwise skip white space between bullet and ensuing text." | |
c567ac01 | 2873 | |
d8350998 | 2874 | (if (not (allout-goto-prefix-doublechecked)) |
1977b8f6 | 2875 | nil |
d8350998 | 2876 | (goto-char allout-recent-prefix-end) |
2a1408fd DN |
2877 | (save-match-data |
2878 | (if ignore-decorations | |
2879 | t | |
2880 | (while (looking-at "[0-9]") (forward-char 1)) | |
2881 | (if (and (not (eolp)) (looking-at "\\s-")) (forward-char 1)))) | |
1977b8f6 | 2882 | ;; Reestablish where we are: |
fd5359c6 MR |
2883 | (allout-current-depth))) |
2884 | ;;;_ > allout-current-bullet-pos () | |
2885 | (defun allout-current-bullet-pos () | |
c07583cd | 2886 | "Return position of current (visible) topic's bullet." |
c567ac01 | 2887 | |
8a559d4e | 2888 | (if (not (allout-current-depth)) |
c567ac01 | 2889 | nil |
8a559d4e | 2890 | (1- allout-recent-prefix-end))) |
fd5359c6 MR |
2891 | ;;;_ > allout-back-to-current-heading () |
2892 | (defun allout-back-to-current-heading () | |
80f66ef4 | 2893 | "Move to heading line of current topic, or beginning if not in a topic. |
96b83743 | 2894 | |
80f66ef4 CY |
2895 | If interactive, we position at the end of the prefix. |
2896 | ||
2897 | Return value of resulting point, unless we started outside | |
2898 | of (before any) topics, in which case we return nil." | |
96b83743 EZ |
2899 | |
2900 | (allout-beginning-of-current-line) | |
80f66ef4 | 2901 | (let ((bol-point (point))) |
ede4ac6a KS |
2902 | (if (allout-goto-prefix-doublechecked) |
2903 | (if (<= (point) bol-point) | |
32226619 | 2904 | (if (called-interactively-p 'interactive) |
ede4ac6a KS |
2905 | (allout-end-of-prefix) |
2906 | (point)) | |
2907 | (goto-char (point-min)) | |
2908 | nil)))) | |
fd5359c6 MR |
2909 | ;;;_ > allout-back-to-heading () |
2910 | (defalias 'allout-back-to-heading 'allout-back-to-current-heading) | |
96b83743 EZ |
2911 | ;;;_ > allout-pre-next-prefix () |
2912 | (defun allout-pre-next-prefix () | |
1977b8f6 RS |
2913 | "Skip forward to just before the next heading line. |
2914 | ||
c567ac01 | 2915 | Returns that character position." |
1977b8f6 | 2916 | |
8a559d4e EZ |
2917 | (if (allout-next-heading) |
2918 | (goto-char (1- allout-recent-prefix-beginning)))) | |
96b83743 EZ |
2919 | ;;;_ > allout-end-of-subtree (&optional current include-trailing-blank) |
2920 | (defun allout-end-of-subtree (&optional current include-trailing-blank) | |
d82979ea EZ |
2921 | "Put point at the end of the last leaf in the containing topic. |
2922 | ||
96b83743 EZ |
2923 | Optional CURRENT means put point at the end of the containing |
2924 | visible topic. | |
2925 | ||
2926 | Optional INCLUDE-TRAILING-BLANK means include a trailing blank line, if | |
2927 | any, as part of the subtree. Otherwise, that trailing blank will be | |
2928 | excluded as delimiting whitespace between topics. | |
d82979ea EZ |
2929 | |
2930 | Returns the value of point." | |
2931 | (interactive "P") | |
2932 | (if current | |
2933 | (allout-back-to-current-heading) | |
d8350998 CY |
2934 | (allout-goto-prefix-doublechecked)) |
2935 | (let ((level allout-recent-depth)) | |
fd5359c6 | 2936 | (allout-next-heading) |
1977b8f6 | 2937 | (while (and (not (eobp)) |
d8350998 | 2938 | (> allout-recent-depth level)) |
fd5359c6 | 2939 | (allout-next-heading)) |
48bd8440 EZ |
2940 | (if (eobp) |
2941 | (allout-end-of-entry) | |
2942 | (forward-char -1)) | |
96b83743 | 2943 | (if (and (not include-trailing-blank) (= ?\n (preceding-char))) |
c567ac01 | 2944 | (forward-char -1)) |
fd5359c6 | 2945 | (setq allout-recent-end-of-subtree (point)))) |
96b83743 EZ |
2946 | ;;;_ > allout-end-of-current-subtree (&optional include-trailing-blank) |
2947 | (defun allout-end-of-current-subtree (&optional include-trailing-blank) | |
2948 | ||
d82979ea EZ |
2949 | "Put point at end of last leaf in currently visible containing topic. |
2950 | ||
96b83743 EZ |
2951 | Optional INCLUDE-TRAILING-BLANK means include a trailing blank line, if |
2952 | any, as part of the subtree. Otherwise, that trailing blank will be | |
2953 | excluded as delimiting whitespace between topics. | |
2954 | ||
d82979ea EZ |
2955 | Returns the value of point." |
2956 | (interactive) | |
96b83743 | 2957 | (allout-end-of-subtree t include-trailing-blank)) |
fd5359c6 MR |
2958 | ;;;_ > allout-beginning-of-current-entry () |
2959 | (defun allout-beginning-of-current-entry () | |
d82979ea | 2960 | "When not already there, position point at beginning of current topic header. |
c567ac01 RS |
2961 | |
2962 | If already there, move cursor to bullet for hot-spot operation. | |
46771bb3 | 2963 | \(See `allout-mode' doc string for details of hot-spot operation.)" |
1977b8f6 | 2964 | (interactive) |
c567ac01 | 2965 | (let ((start-point (point))) |
46771bb3 | 2966 | (move-beginning-of-line 1) |
d8350998 CY |
2967 | (if (< 0 (allout-current-depth)) |
2968 | (goto-char allout-recent-prefix-end) | |
2969 | (goto-char (point-min))) | |
fd5359c6 | 2970 | (allout-end-of-prefix) |
32226619 | 2971 | (if (and (called-interactively-p 'interactive) |
c567ac01 | 2972 | (= (point) start-point)) |
fd5359c6 | 2973 | (goto-char (allout-current-bullet-pos))))) |
96b83743 EZ |
2974 | ;;;_ > allout-end-of-entry (&optional inclusive) |
2975 | (defun allout-end-of-entry (&optional inclusive) | |
2976 | "Position the point at the end of the current topics' entry. | |
2977 | ||
2978 | Optional INCLUSIVE means also include trailing empty line, if any. When | |
2979 | unset, whitespace between items separates them even when the items are | |
2980 | collapsed." | |
1977b8f6 | 2981 | (interactive) |
96b83743 EZ |
2982 | (allout-pre-next-prefix) |
2983 | (if (and (not inclusive) (not (bobp)) (= ?\n (preceding-char))) | |
2984 | (forward-char -1)) | |
2985 | (point)) | |
fd5359c6 MR |
2986 | ;;;_ > allout-end-of-current-heading () |
2987 | (defun allout-end-of-current-heading () | |
9179616f | 2988 | (interactive) |
fd5359c6 | 2989 | (allout-beginning-of-current-entry) |
96b83743 | 2990 | (search-forward "\n" nil t) |
d82979ea | 2991 | (forward-char -1)) |
fd5359c6 | 2992 | (defalias 'allout-end-of-heading 'allout-end-of-current-heading) |
d82979ea EZ |
2993 | ;;;_ > allout-get-body-text () |
2994 | (defun allout-get-body-text () | |
2995 | "Return the unmangled body text of the topic immediately containing point." | |
2996 | (save-excursion | |
2997 | (allout-end-of-prefix) | |
96b83743 | 2998 | (if (not (search-forward "\n" nil t)) |
d82979ea EZ |
2999 | nil |
3000 | (backward-char 1) | |
3001 | (let ((pre-body (point))) | |
3002 | (if (not pre-body) | |
3003 | nil | |
96b83743 | 3004 | (allout-end-of-entry t) |
d82979ea EZ |
3005 | (if (not (= pre-body (point))) |
3006 | (buffer-substring-no-properties (1+ pre-body) (point)))) | |
3007 | ) | |
3008 | ) | |
3009 | ) | |
3010 | ) | |
1977b8f6 | 3011 | |
c567ac01 | 3012 | ;;;_ - Depth-wise |
fd5359c6 MR |
3013 | ;;;_ > allout-ascend-to-depth (depth) |
3014 | (defun allout-ascend-to-depth (depth) | |
c567ac01 | 3015 | "Ascend to depth DEPTH, returning depth if successful, nil if not." |
fd5359c6 | 3016 | (if (and (> depth 0)(<= depth (allout-depth))) |
d8350998 CY |
3017 | (let (last-ascended) |
3018 | (while (and (< depth allout-recent-depth) | |
3019 | (setq last-ascended (allout-ascend)))) | |
3020 | (goto-char allout-recent-prefix-beginning) | |
32226619 | 3021 | (if (called-interactively-p 'interactive) (allout-end-of-prefix)) |
d8350998 | 3022 | (and last-ascended allout-recent-depth)))) |
fd5359c6 | 3023 | ;;;_ > allout-ascend () |
c07583cd KS |
3024 | (defun allout-ascend (&optional dont-move-if-unsuccessful) |
3025 | "Ascend one level, returning resulting depth if successful, nil if not. | |
3026 | ||
3027 | Point is left at the beginning of the level whether or not | |
3028 | successful, unless optional DONT-MOVE-IF-UNSUCCESSFUL is set, in | |
3029 | which case point is returned to its original starting location." | |
3030 | (if dont-move-if-unsuccessful | |
3031 | (setq dont-move-if-unsuccessful (point))) | |
d82979ea EZ |
3032 | (prog1 |
3033 | (if (allout-beginning-of-level) | |
498a6493 CY |
3034 | (let ((bolevel (point)) |
3035 | (bolevel-depth allout-recent-depth)) | |
3036 | (allout-previous-heading) | |
c07583cd KS |
3037 | (cond ((< allout-recent-depth bolevel-depth) |
3038 | allout-recent-depth) | |
3039 | ((= allout-recent-depth bolevel-depth) | |
3040 | (if dont-move-if-unsuccessful | |
3041 | (goto-char dont-move-if-unsuccessful)) | |
3042 | (allout-depth) | |
3043 | nil) | |
3044 | (t | |
3045 | ;; some topic after very first is lower depth than first: | |
3046 | (goto-char bolevel) | |
3047 | (allout-depth) | |
3048 | nil)))) | |
32226619 | 3049 | (if (called-interactively-p 'interactive) (allout-end-of-prefix)))) |
fd5359c6 MR |
3050 | ;;;_ > allout-descend-to-depth (depth) |
3051 | (defun allout-descend-to-depth (depth) | |
19b84ba3 RS |
3052 | "Descend to depth DEPTH within current topic. |
3053 | ||
3054 | Returning depth if successful, nil if not." | |
1977b8f6 | 3055 | (let ((start-point (point)) |
fd5359c6 | 3056 | (start-depth (allout-depth))) |
1977b8f6 | 3057 | (while |
fd5359c6 | 3058 | (and (> (allout-depth) 0) |
d8350998 | 3059 | (not (= depth allout-recent-depth)) ; ... not there yet |
fd5359c6 | 3060 | (allout-next-heading) ; ... go further |
d8350998 | 3061 | (< start-depth allout-recent-depth))) ; ... still in topic |
fd5359c6 | 3062 | (if (and (> (allout-depth) 0) |
d8350998 | 3063 | (= allout-recent-depth depth)) |
1977b8f6 RS |
3064 | depth |
3065 | (goto-char start-point) | |
3066 | nil)) | |
3067 | ) | |
d8350998 CY |
3068 | ;;;_ > allout-up-current-level (arg) |
3069 | (defun allout-up-current-level (arg) | |
3070 | "Move out ARG levels from current visible topic." | |
d82979ea | 3071 | (interactive "p") |
8a559d4e EZ |
3072 | (let ((start-point (point))) |
3073 | (allout-back-to-current-heading) | |
3074 | (if (not (allout-ascend)) | |
3075 | (progn (goto-char start-point) | |
3076 | (error "Can't ascend past outermost level")) | |
32226619 | 3077 | (if (called-interactively-p 'interactive) (allout-end-of-prefix)) |
8a559d4e | 3078 | allout-recent-prefix-beginning))) |
1977b8f6 | 3079 | |
c567ac01 | 3080 | ;;;_ - Linear |
fd5359c6 MR |
3081 | ;;;_ > allout-next-sibling (&optional depth backward) |
3082 | (defun allout-next-sibling (&optional depth backward) | |
2265e017 | 3083 | "Like `allout-forward-current-level', but respects invisible topics. |
1977b8f6 | 3084 | |
c567ac01 | 3085 | Traverse at optional DEPTH, or current depth if none specified. |
1977b8f6 | 3086 | |
c567ac01 | 3087 | Go backward if optional arg BACKWARD is non-nil. |
1977b8f6 | 3088 | |
d8350998 | 3089 | Return the start point of the new topic if successful, nil otherwise." |
1977b8f6 | 3090 | |
d8350998 | 3091 | (if (if backward (bobp) (eobp)) |
1977b8f6 | 3092 | nil |
d8350998 | 3093 | (let ((target-depth (or depth (allout-depth))) |
1977b8f6 | 3094 | (start-point (point)) |
ede4ac6a | 3095 | (start-prefix-beginning allout-recent-prefix-beginning) |
d8350998 CY |
3096 | (count 0) |
3097 | leaping | |
c567ac01 | 3098 | last-depth) |
d8350998 CY |
3099 | (while (and |
3100 | ;; done too few single steps to resort to the leap routine: | |
3101 | (not leaping) | |
3102 | ;; not at limit: | |
3103 | (not (if backward (bobp) (eobp))) | |
3104 | ;; still traversable: | |
3105 | (if backward (allout-previous-heading) (allout-next-heading)) | |
3106 | ;; we're below the target depth | |
3107 | (> (setq last-depth allout-recent-depth) target-depth)) | |
3108 | (setq count (1+ count)) | |
3109 | (if (> count 7) ; lists are commonly 7 +- 2, right?-) | |
3110 | (setq leaping t))) | |
3111 | (cond (leaping | |
3112 | (or (allout-next-sibling-leap target-depth backward) | |
3113 | (progn | |
3114 | (goto-char start-point) | |
3115 | (if depth (allout-depth) target-depth) | |
3116 | nil))) | |
3117 | ((and (not (eobp)) | |
3118 | (and (> (or last-depth (allout-depth)) 0) | |
ede4ac6a KS |
3119 | (= allout-recent-depth target-depth)) |
3120 | (not (= start-prefix-beginning | |
3121 | allout-recent-prefix-beginning))) | |
d8350998 CY |
3122 | allout-recent-prefix-beginning) |
3123 | (t | |
3124 | (goto-char start-point) | |
3125 | (if depth (allout-depth) target-depth) | |
3126 | nil))))) | |
3127 | ;;;_ > allout-next-sibling-leap (&optional depth backward) | |
3128 | (defun allout-next-sibling-leap (&optional depth backward) | |
3129 | "Like `allout-next-sibling', but by direct search for topic at depth. | |
3130 | ||
3131 | Traverse at optional DEPTH, or current depth if none specified. | |
3132 | ||
3133 | Go backward if optional arg BACKWARD is non-nil. | |
3134 | ||
3135 | Return the start point of the new topic if successful, nil otherwise. | |
3136 | ||
3137 | Costs more than regular `allout-next-sibling' for short traversals: | |
3138 | ||
c07583cd | 3139 | - we have to check the prior (next, if travelling backwards) |
d8350998 CY |
3140 | item to confirm connectivity with the prior topic, and |
3141 | - if confirmed, we have to reestablish the allout-recent-* settings with | |
3142 | some extra navigation | |
3143 | - if confirmation fails, we have to do more work to recover | |
3144 | ||
3145 | It is an increasingly big win when there are many intervening | |
3146 | offspring before the next sibling, however, so | |
3147 | `allout-next-sibling' resorts to this if it finds itself in that | |
3148 | situation." | |
3149 | ||
3150 | (if (if backward (bobp) (eobp)) | |
3151 | nil | |
3152 | (let* ((start-point (point)) | |
3153 | (target-depth (or depth (allout-depth))) | |
3154 | (search-whitespace-regexp nil) | |
3155 | (depth-biased (- target-depth 2)) | |
3156 | (expression (if (<= target-depth 1) | |
3157 | allout-depth-one-regexp | |
3158 | (format allout-depth-specific-regexp | |
3159 | depth-biased depth-biased))) | |
3160 | found | |
3161 | done) | |
3162 | (while (not done) | |
2a1408fd DN |
3163 | (setq found (save-match-data |
3164 | (if backward | |
3165 | (re-search-backward expression nil 'to-limit) | |
3166 | (forward-char 1) | |
3167 | (re-search-forward expression nil 'to-limit)))) | |
d8350998 CY |
3168 | (if (and found (allout-aberrant-container-p)) |
3169 | (setq found nil)) | |
3170 | (setq done (or found (if backward (bobp) (eobp))))) | |
3171 | (if (not found) | |
3172 | (progn (goto-char start-point) | |
3173 | nil) | |
3174 | ;; rationale: if any intervening items were at a lower depth, we | |
60c7bbd2 | 3175 | ;; would now be on the first offspring at the target depth -- ie, |
d8350998 CY |
3176 | ;; the preceeding item (per the search direction) must be at a |
3177 | ;; lesser depth. that's all we need to check. | |
3178 | (if backward (allout-next-heading) (allout-previous-heading)) | |
3179 | (if (< allout-recent-depth target-depth) | |
3180 | ;; return to start and reestablish allout-recent-*: | |
3181 | (progn | |
3182 | (goto-char start-point) | |
3183 | (allout-depth) | |
3184 | nil) | |
3185 | (goto-char found) | |
3186 | ;; locate cursor and set allout-recent-*: | |
3187 | (allout-goto-prefix)))))) | |
fd5359c6 MR |
3188 | ;;;_ > allout-previous-sibling (&optional depth backward) |
3189 | (defun allout-previous-sibling (&optional depth backward) | |
d82979ea | 3190 | "Like `allout-forward-current-level' backwards, respecting invisible topics. |
1977b8f6 | 3191 | |
c567ac01 | 3192 | Optional DEPTH specifies depth to traverse, default current depth. |
1977b8f6 | 3193 | |
c567ac01 | 3194 | Optional BACKWARD reverses direction. |
1977b8f6 | 3195 | |
c567ac01 | 3196 | Return depth if successful, nil otherwise." |
fd5359c6 | 3197 | (allout-next-sibling depth (not backward)) |
1977b8f6 | 3198 | ) |
fd5359c6 MR |
3199 | ;;;_ > allout-snug-back () |
3200 | (defun allout-snug-back () | |
539d7736 | 3201 | "Position cursor at end of previous topic. |
19b84ba3 RS |
3202 | |
3203 | Presumes point is at the start of a topic prefix." | |
c567ac01 RS |
3204 | (if (or (bobp) (eobp)) |
3205 | nil | |
3206 | (forward-char -1)) | |
96b83743 | 3207 | (if (or (bobp) (not (= ?\n (preceding-char)))) |
c567ac01 | 3208 | nil |
96b83743 | 3209 | (forward-char -1)) |
c567ac01 | 3210 | (point)) |
fd5359c6 MR |
3211 | ;;;_ > allout-beginning-of-level () |
3212 | (defun allout-beginning-of-level () | |
c567ac01 | 3213 | "Go back to the first sibling at this level, visible or not." |
fd5359c6 MR |
3214 | (allout-end-of-level 'backward)) |
3215 | ;;;_ > allout-end-of-level (&optional backward) | |
3216 | (defun allout-end-of-level (&optional backward) | |
c567ac01 | 3217 | "Go to the last sibling at this level, visible or not." |
1977b8f6 | 3218 | |
fd5359c6 MR |
3219 | (let ((depth (allout-depth))) |
3220 | (while (allout-previous-sibling depth nil)) | |
d8350998 | 3221 | (prog1 allout-recent-depth |
32226619 | 3222 | (if (called-interactively-p 'interactive) (allout-end-of-prefix))))) |
fd5359c6 MR |
3223 | ;;;_ > allout-next-visible-heading (arg) |
3224 | (defun allout-next-visible-heading (arg) | |
19b84ba3 | 3225 | "Move to the next ARG'th visible heading line, backward if arg is negative. |
c567ac01 | 3226 | |
96b83743 | 3227 | Move to buffer limit in indicated direction if headings are exhausted." |
1977b8f6 | 3228 | |
1977b8f6 | 3229 | (interactive "p") |
0458fa80 EZ |
3230 | (let* ((inhibit-field-text-motion t) |
3231 | (backward (if (< arg 0) (setq arg (* -1 arg)))) | |
c567ac01 RS |
3232 | (step (if backward -1 1)) |
3233 | prev got) | |
3234 | ||
d8350998 CY |
3235 | (while (> arg 0) |
3236 | (while (and | |
3237 | ;; Boundary condition: | |
3238 | (not (if backward (bobp)(eobp))) | |
3239 | ;; Move, skipping over all concealed lines in one fell swoop: | |
3240 | (prog1 (condition-case nil (or (line-move step) t) | |
3241 | (error nil)) | |
3242 | (allout-beginning-of-current-line)) | |
3243 | ;; Deal with apparent header line: | |
2a1408fd DN |
3244 | (save-match-data |
3245 | (if (not (looking-at allout-regexp)) | |
3246 | ;; not a header line, keep looking: | |
d8350998 | 3247 | t |
2a1408fd DN |
3248 | (allout-prefix-data) |
3249 | (if (and (allout-do-doublecheck) | |
3250 | (allout-aberrant-container-p)) | |
3251 | ;; skip this aberrant prospective header line: | |
3252 | t | |
8dd59f01 | 3253 | ;; this prospective headerline qualifies -- register: |
2a1408fd DN |
3254 | (setq got allout-recent-prefix-beginning) |
3255 | ;; and break the loop: | |
3256 | nil))))) | |
c567ac01 RS |
3257 | ;; Register this got, it may be the last: |
3258 | (if got (setq prev got)) | |
3259 | (setq arg (1- arg))) | |
3260 | (cond (got ; Last move was to a prefix: | |
d8350998 | 3261 | (allout-end-of-prefix)) |
c567ac01 | 3262 | (prev ; Last move wasn't, but prev was: |
d8350998 CY |
3263 | (goto-char prev) |
3264 | (allout-end-of-prefix)) | |
c567ac01 | 3265 | ((not backward) (end-of-line) nil)))) |
fd5359c6 MR |
3266 | ;;;_ > allout-previous-visible-heading (arg) |
3267 | (defun allout-previous-visible-heading (arg) | |
c567ac01 | 3268 | "Move to the previous heading line. |
1977b8f6 | 3269 | |
c567ac01 | 3270 | With argument, repeats or can move forward if negative. |
2265e017 | 3271 | A heading line is one that starts with a `*' (or that `allout-regexp' |
c567ac01 | 3272 | matches)." |
1977b8f6 | 3273 | (interactive "p") |
d8350998 | 3274 | (prog1 (allout-next-visible-heading (- arg)) |
32226619 | 3275 | (if (called-interactively-p 'interactive) (allout-end-of-prefix)))) |
fd5359c6 | 3276 | ;;;_ > allout-forward-current-level (arg) |
d82979ea | 3277 | (defun allout-forward-current-level (arg) |
19b84ba3 | 3278 | "Position point at the next heading of the same level. |
1977b8f6 | 3279 | |
19b84ba3 | 3280 | Takes optional repeat-count, goes backward if count is negative. |
1977b8f6 | 3281 | |
19b84ba3 | 3282 | Returns resulting position, else nil if none found." |
d82979ea | 3283 | (interactive "p") |
fd5359c6 | 3284 | (let ((start-depth (allout-current-depth)) |
19b84ba3 | 3285 | (start-arg arg) |
d8350998 | 3286 | (backward (> 0 arg))) |
19b84ba3 RS |
3287 | (if (= 0 start-depth) |
3288 | (error "No siblings, not in a topic...")) | |
3289 | (if backward (setq arg (* -1 arg))) | |
d8350998 CY |
3290 | (allout-back-to-current-heading) |
3291 | (while (and (not (zerop arg)) | |
3292 | (if backward | |
3293 | (allout-previous-sibling) | |
3294 | (allout-next-sibling))) | |
3295 | (setq arg (1- arg))) | |
32226619 | 3296 | (if (not (called-interactively-p 'interactive)) |
d8350998 CY |
3297 | nil |
3298 | (allout-end-of-prefix) | |
3299 | (if (not (zerop arg)) | |
3300 | (error "Hit %s level %d topic, traversed %d of %d requested" | |
3301 | (if backward "first" "last") | |
3302 | allout-recent-depth | |
3303 | (- (abs start-arg) arg) | |
3304 | (abs start-arg)))))) | |
fd5359c6 | 3305 | ;;;_ > allout-backward-current-level (arg) |
d82979ea | 3306 | (defun allout-backward-current-level (arg) |
fd5359c6 | 3307 | "Inverse of `allout-forward-current-level'." |
d82979ea | 3308 | (interactive "p") |
32226619 | 3309 | (if (called-interactively-p 'interactive) |
19b84ba3 | 3310 | (let ((current-prefix-arg (* -1 arg))) |
fd5359c6 MR |
3311 | (call-interactively 'allout-forward-current-level)) |
3312 | (allout-forward-current-level (* -1 arg)))) | |
c567ac01 | 3313 | |
19b84ba3 | 3314 | ;;;_ #5 Alteration |
c567ac01 RS |
3315 | |
3316 | ;;;_ - Fundamental | |
fd5359c6 MR |
3317 | ;;;_ = allout-post-goto-bullet |
3318 | (defvar allout-post-goto-bullet nil | |
3319 | "Outline internal var, for `allout-pre-command-business' hot-spot operation. | |
19b84ba3 RS |
3320 | |
3321 | When set, tells post-processing to reposition on topic bullet, and | |
2265e017 | 3322 | then unset it. Set by `allout-pre-command-business' when implementing |
19b84ba3 RS |
3323 | hot-spot operation, where literal characters typed over a topic bullet |
3324 | are mapped to the command of the corresponding control-key on the | |
2265e017 | 3325 | `allout-mode-map'.") |
fd5359c6 | 3326 | (make-variable-buffer-local 'allout-post-goto-bullet) |
48bd8440 EZ |
3327 | ;;;_ = allout-command-counter |
3328 | (defvar allout-command-counter 0 | |
3329 | "Counter that monotonically increases in allout-mode buffers. | |
3330 | ||
3331 | Set by `allout-pre-command-business', to support allout addons in | |
3332 | coordinating with allout activity.") | |
3333 | (make-variable-buffer-local 'allout-command-counter) | |
fd5359c6 MR |
3334 | ;;;_ > allout-post-command-business () |
3335 | (defun allout-post-command-business () | |
2265e017 | 3336 | "Outline `post-command-hook' function. |
c567ac01 | 3337 | |
2265e017 | 3338 | - Implement (and clear) `allout-post-goto-bullet', for hot-spot |
c567ac01 RS |
3339 | outline commands. |
3340 | ||
96b83743 | 3341 | - Decrypt topic currently being edited if it was encrypted for a save." |
c567ac01 RS |
3342 | |
3343 | ; Apply any external change func: | |
fd5359c6 | 3344 | (if (not (allout-mode-p)) ; In allout-mode. |
19b84ba3 | 3345 | nil |
d82979ea EZ |
3346 | |
3347 | (if (and (boundp 'allout-after-save-decrypt) | |
3348 | allout-after-save-decrypt) | |
3349 | (allout-after-saves-handler)) | |
3350 | ||
48bd8440 | 3351 | ;; Implement allout-post-goto-bullet, if set: |
fd5359c6 MR |
3352 | (if (and allout-post-goto-bullet |
3353 | (allout-current-bullet-pos)) | |
3354 | (progn (goto-char (allout-current-bullet-pos)) | |
3355 | (setq allout-post-goto-bullet nil))) | |
19b84ba3 | 3356 | )) |
fd5359c6 MR |
3357 | ;;;_ > allout-pre-command-business () |
3358 | (defun allout-pre-command-business () | |
2265e017 | 3359 | "Outline `pre-command-hook' function for outline buffers. |
48bd8440 EZ |
3360 | |
3361 | Among other things, implements special behavior when the cursor is on the | |
3362 | topic bullet character. | |
c567ac01 | 3363 | |
58edceeb JB |
3364 | When the cursor is on the bullet character, self-insert characters are |
3365 | reinterpreted as the corresponding control-character in the | |
3366 | `allout-mode-map'. The `allout-mode' `post-command-hook' insures that | |
3367 | the cursor which has moved as a result of such reinterpretation is | |
3368 | positioned on the bullet character of the destination topic. | |
c567ac01 | 3369 | |
c07583cd | 3370 | The upshot is that you can get easy, single (ie, unmodified) key |
9179616f | 3371 | outline maneuvering operations by positioning the cursor on the bullet |
58edceeb JB |
3372 | char. When in this mode you can use regular cursor-positioning |
3373 | command/keystrokes to relocate the cursor off of a bullet character to | |
3374 | return to regular interpretation of self-insert characters." | |
d82979ea | 3375 | |
fd5359c6 | 3376 | (if (not (allout-mode-p)) |
9179616f | 3377 | nil |
48bd8440 EZ |
3378 | ;; Increment allout-command-counter |
3379 | (setq allout-command-counter (1+ allout-command-counter)) | |
3380 | ;; Do hot-spot navigation. | |
9179616f | 3381 | (if (and (eq this-command 'self-insert-command) |
fd5359c6 | 3382 | (eq (point)(allout-current-bullet-pos))) |
01fc9422 EZ |
3383 | (allout-hotspot-key-handler)))) |
3384 | ;;;_ > allout-hotspot-key-handler () | |
3385 | (defun allout-hotspot-key-handler () | |
3386 | "Catchall handling of key bindings in hot-spots. | |
3387 | ||
3388 | Translates unmodified keystrokes to corresponding allout commands, when | |
3389 | they would qualify if prefixed with the allout-command-prefix, and sets | |
3390 | this-command accordingly. | |
3391 | ||
3392 | Returns the qualifying command, if any, else nil." | |
3393 | (interactive) | |
8989a920 GM |
3394 | (let* ((key-string (if (numberp last-command-event) |
3395 | (char-to-string last-command-event))) | |
3396 | (key-num (cond ((numberp last-command-event) last-command-event) | |
01fc9422 EZ |
3397 | ;; for XEmacs character type: |
3398 | ((and (fboundp 'characterp) | |
8989a920 GM |
3399 | (apply 'characterp (list last-command-event))) |
3400 | (apply 'char-to-int (list last-command-event))) | |
01fc9422 | 3401 | (t 0))) |
d8350998 | 3402 | mapped-binding) |
01fc9422 EZ |
3403 | |
3404 | (if (zerop key-num) | |
3405 | nil | |
3406 | ||
d8350998 CY |
3407 | (if (and |
3408 | ;; exclude control chars and escape: | |
3409 | (<= 33 key-num) | |
3410 | (setq mapped-binding | |
3411 | (or (and (assoc key-string allout-keybindings-list) | |
3412 | ;; translate literal membership on list: | |
3413 | (cadr (assoc key-string allout-keybindings-list))) | |
3414 | ;; translate as a keybinding: | |
2a1408fd | 3415 | (key-binding (vconcat allout-command-prefix |
01fc9422 | 3416 | (char-to-string |
d8350998 | 3417 | (if (and (<= 97 key-num) ; "a" |
01fc9422 EZ |
3418 | (>= 122 key-num)) ; "z" |
3419 | (- key-num 96) key-num))) | |
d8350998 | 3420 | t)))) |
60c7bbd2 | 3421 | ;; Qualified as an allout command -- do hot-spot operation. |
01fc9422 EZ |
3422 | (setq allout-post-goto-bullet t) |
3423 | ;; accept-defaults nil, or else we'll get allout-item-icon-key-handler. | |
3424 | (setq mapped-binding (key-binding (char-to-string key-num)))) | |
3425 | ||
3426 | (while (keymapp mapped-binding) | |
3427 | (setq mapped-binding | |
d8350998 | 3428 | (lookup-key mapped-binding (vector (read-char))))) |
01fc9422 EZ |
3429 | |
3430 | (if mapped-binding | |
5204800f | 3431 | (setq this-command mapped-binding))))) |
01fc9422 | 3432 | |
fd5359c6 MR |
3433 | ;;;_ > allout-find-file-hook () |
3434 | (defun allout-find-file-hook () | |
4034b0e2 | 3435 | "Activate `allout-mode' on non-nil `allout-auto-activation', `allout-layout'. |
fd5359c6 MR |
3436 | |
3437 | See `allout-init' for setup instructions." | |
3438 | (if (and allout-auto-activation | |
3439 | (not (allout-mode-p)) | |
3440 | allout-layout) | |
3441 | (allout-mode t))) | |
c567ac01 RS |
3442 | |
3443 | ;;;_ - Topic Format Assessment | |
fd5359c6 MR |
3444 | ;;;_ > allout-solicit-alternate-bullet (depth &optional current-bullet) |
3445 | (defun allout-solicit-alternate-bullet (depth &optional current-bullet) | |
1977b8f6 | 3446 | |
19b84ba3 RS |
3447 | "Prompt for and return a bullet char as an alternative to the current one. |
3448 | ||
3449 | Offer one suitable for current depth DEPTH as default." | |
1977b8f6 | 3450 | |
9179616f | 3451 | (let* ((default-bullet (or (and (stringp current-bullet) current-bullet) |
fd5359c6 MR |
3452 | (allout-bullet-for-depth depth))) |
3453 | (sans-escapes (regexp-sans-escapes allout-bullets-string)) | |
9179616f DL |
3454 | choice) |
3455 | (save-excursion | |
fd5359c6 | 3456 | (goto-char (allout-current-bullet-pos)) |
9179616f DL |
3457 | (setq choice (solicit-char-in-string |
3458 | (format "Select bullet: %s ('%s' default): " | |
3459 | sans-escapes | |
d8350998 | 3460 | (substring-no-properties default-bullet)) |
9179616f DL |
3461 | sans-escapes |
3462 | t))) | |
3463 | (message "") | |
1977b8f6 RS |
3464 | (if (string= choice "") default-bullet choice)) |
3465 | ) | |
fd5359c6 MR |
3466 | ;;;_ > allout-distinctive-bullet (bullet) |
3467 | (defun allout-distinctive-bullet (bullet) | |
e126900f | 3468 | "True if BULLET is one of those on `allout-distinctive-bullets-string'." |
fd5359c6 MR |
3469 | (string-match (regexp-quote bullet) allout-distinctive-bullets-string)) |
3470 | ;;;_ > allout-numbered-type-prefix (&optional prefix) | |
3471 | (defun allout-numbered-type-prefix (&optional prefix) | |
c567ac01 | 3472 | "True if current header prefix bullet is numbered bullet." |
fd5359c6 MR |
3473 | (and allout-numbered-bullet |
3474 | (string= allout-numbered-bullet | |
1977b8f6 | 3475 | (if prefix |
fd5359c6 MR |
3476 | (allout-get-prefix-bullet prefix) |
3477 | (allout-get-bullet))))) | |
d82979ea EZ |
3478 | ;;;_ > allout-encrypted-type-prefix (&optional prefix) |
3479 | (defun allout-encrypted-type-prefix (&optional prefix) | |
c07583cd | 3480 | "True if current header prefix bullet is for an encrypted entry (body)." |
d82979ea EZ |
3481 | (and allout-topic-encryption-bullet |
3482 | (string= allout-topic-encryption-bullet | |
3483 | (if prefix | |
3484 | (allout-get-prefix-bullet prefix) | |
3485 | (allout-get-bullet))))) | |
fd5359c6 MR |
3486 | ;;;_ > allout-bullet-for-depth (&optional depth) |
3487 | (defun allout-bullet-for-depth (&optional depth) | |
19b84ba3 | 3488 | "Return outline topic bullet suited to optional DEPTH, or current depth." |
1977b8f6 | 3489 | ;; Find bullet in plain-bullets-string modulo DEPTH. |
fd5359c6 MR |
3490 | (if allout-stylish-prefixes |
3491 | (char-to-string (aref allout-plain-bullets-string | |
1977b8f6 | 3492 | (% (max 0 (- depth 2)) |
fd5359c6 MR |
3493 | allout-plain-bullets-string-len))) |
3494 | allout-primary-bullet) | |
1977b8f6 RS |
3495 | ) |
3496 | ||
c567ac01 | 3497 | ;;;_ - Topic Production |
fd5359c6 MR |
3498 | ;;;_ > allout-make-topic-prefix (&optional prior-bullet |
3499 | (defun allout-make-topic-prefix (&optional prior-bullet | |
1977b8f6 RS |
3500 | new |
3501 | depth | |
3502 | solicit | |
3503 | number-control | |
3504 | index) | |
3505 | ;; Depth null means use current depth, non-null means we're either | |
3506 | ;; opening a new topic after current topic, lower or higher, or we're | |
3507 | ;; changing level of current topic. | |
3508 | ;; Solicit dominates specified bullet-char. | |
c567ac01 | 3509 | ;;;_ . Doc string: |
19b84ba3 | 3510 | "Generate a topic prefix suitable for optional arg DEPTH, or current depth. |
c567ac01 RS |
3511 | |
3512 | All the arguments are optional. | |
3513 | ||
3514 | PRIOR-BULLET indicates the bullet of the prefix being changed, or | |
3515 | nil if none. This bullet may be preserved (other options | |
2265e017 | 3516 | notwithstanding) if it is on the `allout-distinctive-bullets-string', |
c567ac01 RS |
3517 | for instance. |
3518 | ||
3519 | Second arg NEW indicates that a new topic is being opened after the | |
3520 | topic at point, if non-nil. Default bullet for new topics, eg, may | |
3521 | be set (contingent to other args) to numbered bullets if previous | |
3522 | sibling is one. The implication otherwise is that the current topic | |
60c7bbd2 | 3523 | is being adjusted -- shifted or rebulleted -- and we don't consider |
c567ac01 RS |
3524 | bullet or previous sibling. |
3525 | ||
3526 | Third arg DEPTH forces the topic prefix to that depth, regardless of | |
3527 | the current topics' depth. | |
3528 | ||
9179616f DL |
3529 | If SOLICIT is non-nil, then the choice of bullet is solicited from |
3530 | user. If it's a character, then that character is offered as the | |
c07583cd KS |
3531 | default, otherwise the one suited to the context (according to |
3532 | distinction or depth) is offered. (This overrides other options, | |
9179616f DL |
3533 | including, eg, a distinctive PRIOR-BULLET.) If non-nil, then the |
3534 | context-specific bullet is used. | |
c567ac01 | 3535 | |
fd5359c6 | 3536 | Fifth arg, NUMBER-CONTROL, matters only if `allout-numbered-bullet' |
c567ac01 RS |
3537 | is non-nil *and* soliciting was not explicitly invoked. Then |
3538 | NUMBER-CONTROL non-nil forces prefix to either numbered or | |
3539 | denumbered format, depending on the value of the sixth arg, INDEX. | |
3540 | ||
3541 | \(Note that NUMBER-CONTROL does *not* apply to level 1 topics. Sorry...) | |
3542 | ||
3543 | If NUMBER-CONTROL is non-nil and sixth arg INDEX is non-nil then | |
3544 | the prefix of the topic is forced to be numbered. Non-nil | |
3545 | NUMBER-CONTROL and nil INDEX forces non-numbered format on the | |
3546 | bullet. Non-nil NUMBER-CONTROL and non-nil, non-number INDEX means | |
3547 | that the index for the numbered prefix will be derived, by counting | |
3548 | siblings back to start of level. If INDEX is a number, then that | |
3549 | number is used as the index for the numbered prefix (allowing, eg, | |
a0776d6b | 3550 | sequential renumbering to not require this function counting back the |
c567ac01 RS |
3551 | index for each successive sibling)." |
3552 | ;;;_ . Code: | |
1977b8f6 RS |
3553 | ;; The options are ordered in likely frequence of use, most common |
3554 | ;; highest, least lowest. Ie, more likely to be doing prefix | |
3555 | ;; adjustments than soliciting, and yet more than numbering. | |
3556 | ;; Current prefix is least dominant, but most likely to be commonly | |
3557 | ;; specified... | |
3558 | ||
3559 | (let* (body | |
3560 | numbering | |
3561 | denumbering | |
fd5359c6 MR |
3562 | (depth (or depth (allout-depth))) |
3563 | (header-lead allout-header-prefix) | |
1977b8f6 RS |
3564 | (bullet-char |
3565 | ||
3566 | ;; Getting value for bullet char is practically the whole job: | |
3567 | ||
3568 | (cond | |
60c7bbd2 | 3569 | ; Simplest situation -- level 1: |
fd5359c6 | 3570 | ((<= depth 1) (setq header-lead "") allout-primary-bullet) |
1977b8f6 | 3571 | ; Simple, too: all asterisks: |
fd5359c6 | 3572 | (allout-old-style-prefixes |
60c7bbd2 | 3573 | ;; Cheat -- make body the whole thing, null out header-lead and |
1977b8f6 RS |
3574 | ;; bullet-char: |
3575 | (setq body (make-string depth | |
fd5359c6 | 3576 | (string-to-char allout-primary-bullet))) |
1977b8f6 RS |
3577 | (setq header-lead "") |
3578 | "") | |
3579 | ||
3580 | ;; (Neither level 1 nor old-style, so we're space padding. | |
3581 | ;; Sneak it in the condition of the next case, whatever it is.) | |
3582 | ||
3583 | ;; Solicitation overrides numbering and other cases: | |
3584 | ((progn (setq body (make-string (- depth 2) ?\ )) | |
3585 | ;; The actual condition: | |
3586 | solicit) | |
fd5359c6 | 3587 | (let* ((got (allout-solicit-alternate-bullet depth solicit))) |
1977b8f6 | 3588 | ;; Gotta check whether we're numbering and got a numbered bullet: |
fd5359c6 | 3589 | (setq numbering (and allout-numbered-bullet |
1977b8f6 | 3590 | (not (and number-control (not index))) |
fd5359c6 | 3591 | (string= got allout-numbered-bullet))) |
1977b8f6 RS |
3592 | ;; Now return what we got, regardless: |
3593 | got)) | |
3594 | ||
3595 | ;; Numbering invoked through args: | |
fd5359c6 | 3596 | ((and allout-numbered-bullet number-control) |
1977b8f6 | 3597 | (if (setq numbering (not (setq denumbering (not index)))) |
fd5359c6 | 3598 | allout-numbered-bullet |
19b84ba3 | 3599 | (if (and prior-bullet |
fd5359c6 | 3600 | (not (string= allout-numbered-bullet |
19b84ba3 RS |
3601 | prior-bullet))) |
3602 | prior-bullet | |
fd5359c6 | 3603 | (allout-bullet-for-depth depth)))) |
1977b8f6 RS |
3604 | |
3605 | ;;; Neither soliciting nor controlled numbering ;;; | |
3606 | ;;; (may be controlled denumbering, tho) ;;; | |
3607 | ||
3608 | ;; Check wrt previous sibling: | |
3609 | ((and new ; only check for new prefixes | |
fd5359c6 MR |
3610 | (<= depth (allout-depth)) |
3611 | allout-numbered-bullet ; ... & numbering enabled | |
1977b8f6 RS |
3612 | (not denumbering) |
3613 | (let ((sibling-bullet | |
3614 | (save-excursion | |
3615 | ;; Locate correct sibling: | |
fd5359c6 MR |
3616 | (or (>= depth (allout-depth)) |
3617 | (allout-ascend-to-depth depth)) | |
3618 | (allout-get-bullet)))) | |
1977b8f6 | 3619 | (if (and sibling-bullet |
fd5359c6 | 3620 | (string= allout-numbered-bullet sibling-bullet)) |
1977b8f6 RS |
3621 | (setq numbering sibling-bullet))))) |
3622 | ||
3623 | ;; Distinctive prior bullet? | |
3624 | ((and prior-bullet | |
fd5359c6 | 3625 | (allout-distinctive-bullet prior-bullet) |
1977b8f6 | 3626 | ;; Either non-numbered: |
fd5359c6 MR |
3627 | (or (not (and allout-numbered-bullet |
3628 | (string= prior-bullet allout-numbered-bullet))) | |
1977b8f6 RS |
3629 | ;; or numbered, and not denumbering: |
3630 | (setq numbering (not denumbering))) | |
3631 | ;; Here 'tis: | |
3632 | prior-bullet)) | |
3633 | ||
3634 | ;; Else, standard bullet per depth: | |
fd5359c6 | 3635 | ((allout-bullet-for-depth depth))))) |
1977b8f6 RS |
3636 | |
3637 | (concat header-lead | |
3638 | body | |
3639 | bullet-char | |
3640 | (if numbering | |
3641 | (format "%d" (cond ((and index (numberp index)) index) | |
fd5359c6 MR |
3642 | (new (1+ (allout-sibling-index depth))) |
3643 | ((allout-sibling-index)))))) | |
1977b8f6 RS |
3644 | ) |
3645 | ) | |
96b83743 EZ |
3646 | ;;;_ > allout-open-topic (relative-depth &optional before offer-recent-bullet) |
3647 | (defun allout-open-topic (relative-depth &optional before offer-recent-bullet) | |
d82979ea | 3648 | "Open a new topic at depth DEPTH. |
19b84ba3 RS |
3649 | |
3650 | New topic is situated after current one, unless optional flag BEFORE | |
60c7bbd2 RS |
3651 | is non-nil, or unless current line is completely empty -- lacking even |
3652 | whitespace -- in which case open is done on the current line. | |
96b83743 EZ |
3653 | |
3654 | When adding an offspring, it will be added immediately after the parent if | |
3655 | the other offspring are exposed, or after the last child if the offspring | |
c07583cd | 3656 | are hidden. (The intervening offspring will be exposed in the latter |
96b83743 | 3657 | case.) |
c567ac01 | 3658 | |
96b83743 | 3659 | If OFFER-RECENT-BULLET is true, offer to use the bullet of the prior sibling. |
9179616f | 3660 | |
c567ac01 RS |
3661 | Nuances: |
3662 | ||
3663 | - Creation of new topics is with respect to the visible topic | |
3664 | containing the cursor, regardless of intervening concealed ones. | |
3665 | ||
3666 | - New headers are generally created after/before the body of a | |
3667 | topic. However, they are created right at cursor location if the | |
3668 | cursor is on a blank line, even if that breaks the current topic | |
3669 | body. This is intentional, to provide a simple means for | |
3670 | deliberately dividing topic bodies. | |
3671 | ||
3672 | - Double spacing of topic lists is preserved. Also, the first | |
3673 | level two topic is created double-spaced (and so would be | |
3674 | subsequent siblings, if that's left intact). Otherwise, | |
3675 | single-spacing is used. | |
3676 | ||
3677 | - Creation of sibling or nested topics is with respect to the topic | |
3678 | you're starting from, even when creating backwards. This way you | |
3679 | can easily create a sibling in front of the current topic without | |
a0776d6b | 3680 | having to go to its preceding sibling, and then open forward |
c567ac01 | 3681 | from there." |
1977b8f6 | 3682 | |
96b83743 | 3683 | (allout-beginning-of-current-line) |
2a1408fd DN |
3684 | (save-match-data |
3685 | (let* ((inhibit-field-text-motion t) | |
3686 | (depth (+ (allout-current-depth) relative-depth)) | |
3687 | (opening-on-blank (if (looking-at "^\$") | |
3688 | (not (setq before nil)))) | |
3689 | ;; bunch o vars set while computing ref-topic | |
3690 | opening-numbered | |
3691 | ref-depth | |
3692 | ref-bullet | |
3693 | (ref-topic (save-excursion | |
3694 | (cond ((< relative-depth 0) | |
3695 | (allout-ascend-to-depth depth)) | |
3696 | ((>= relative-depth 1) nil) | |
3697 | (t (allout-back-to-current-heading))) | |
3698 | (setq ref-depth allout-recent-depth) | |
3699 | (setq ref-bullet | |
3700 | (if (> allout-recent-prefix-end 1) | |
3701 | (allout-recent-bullet) | |
3702 | "")) | |
3703 | (setq opening-numbered | |
3704 | (save-excursion | |
3705 | (and allout-numbered-bullet | |
3706 | (or (<= relative-depth 0) | |
3707 | (allout-descend-to-depth depth)) | |
3708 | (if (allout-numbered-type-prefix) | |
3709 | allout-numbered-bullet)))) | |
3710 | (point))) | |
3711 | dbl-space | |
3712 | doing-beginning | |
3713 | start end) | |
3714 | ||
3715 | (if (not opening-on-blank) | |
1977b8f6 | 3716 | ; Positioning and vertical |
60c7bbd2 | 3717 | ; padding -- only if not |
1977b8f6 | 3718 | ; opening-on-blank: |
2a1408fd DN |
3719 | (progn |
3720 | (goto-char ref-topic) | |
3721 | (setq dbl-space ; Determine double space action: | |
3722 | (or (and (<= relative-depth 0) ; not descending; | |
3723 | (save-excursion | |
3724 | ;; at b-o-b or preceded by a blank line? | |
3725 | (or (> 0 (forward-line -1)) | |
3726 | (looking-at "^\\s-*$") | |
3727 | (bobp))) | |
3728 | (save-excursion | |
3729 | ;; succeeded by a blank line? | |
3730 | (allout-end-of-current-subtree) | |
3731 | (looking-at "\n\n"))) | |
3732 | (and (= ref-depth 1) | |
3733 | (or before | |
3734 | (= depth 1) | |
3735 | (save-excursion | |
3736 | ;; Don't already have following | |
3737 | ;; vertical padding: | |
3738 | (not (allout-pre-next-prefix))))))) | |
3739 | ||
3740 | ;; Position to prior heading, if inserting backwards, and not | |
3741 | ;; going outwards: | |
3742 | (if (and before (>= relative-depth 0)) | |
3743 | (progn (allout-back-to-current-heading) | |
3744 | (setq doing-beginning (bobp)) | |
3745 | (if (not (bobp)) | |
3746 | (allout-previous-heading))) | |
3747 | (if (and before (bobp)) | |
3748 | (open-line 1))) | |
3749 | ||
3750 | (if (<= relative-depth 0) | |
3751 | ;; Not going inwards, don't snug up: | |
3752 | (if doing-beginning | |
3753 | (if (not dbl-space) | |
3754 | (open-line 1) | |
3755 | (open-line 2)) | |
3756 | (if before | |
3757 | (progn (end-of-line) | |
3758 | (allout-pre-next-prefix) | |
3759 | (while (and (= ?\n (following-char)) | |
3760 | (save-excursion | |
3761 | (forward-char 1) | |
3762 | (allout-hidden-p))) | |
3763 | (forward-char 1)) | |
3764 | (if (not (looking-at "^$")) | |
3765 | (open-line 1))) | |
3766 | (allout-end-of-current-subtree) | |
3767 | (if (looking-at "\n\n") (forward-char 1)))) | |
8dd59f01 | 3768 | ;; Going inwards -- double-space if first offspring is |
2a1408fd DN |
3769 | ;; double-spaced, otherwise snug up. |
3770 | (allout-end-of-entry) | |
3771 | (if (eobp) | |
3772 | (newline 1) | |
3773 | (line-move 1)) | |
3774 | (allout-beginning-of-current-line) | |
3775 | (backward-char 1) | |
3776 | (if (bolp) | |
3777 | ;; Blank lines between current header body and next | |
8dd59f01 | 3778 | ;; header -- get to last substantive (non-white-space) |
2a1408fd DN |
3779 | ;; line in body: |
3780 | (progn (setq dbl-space t) | |
3781 | (re-search-backward "[^ \t\n]" nil t))) | |
3782 | (if (looking-at "\n\n") | |
3783 | (setq dbl-space t)) | |
3784 | (if (save-excursion | |
3785 | (allout-next-heading) | |
3786 | (when (> allout-recent-depth ref-depth) | |
3787 | ;; This is an offspring. | |
3788 | (forward-line -1) | |
3789 | (looking-at "^\\s-*$"))) | |
3790 | (progn (forward-line 1) | |
96b83743 | 3791 | (open-line 1) |
2a1408fd DN |
3792 | (forward-line 1))) |
3793 | (allout-end-of-current-line)) | |
3794 | ||
3795 | ;;(if doing-beginning (goto-char doing-beginning)) | |
3796 | (if (not (bobp)) | |
3797 | ;; We insert a newline char rather than using open-line to | |
3798 | ;; avoid rear-stickiness inheritence of read-only property. | |
3799 | (progn (if (and (not (> depth ref-depth)) | |
3800 | (not before)) | |
3801 | (open-line 1) | |
3802 | (if (and (not dbl-space) (> depth ref-depth)) | |
3803 | (newline 1) | |
3804 | (if dbl-space | |
3805 | (open-line 1) | |
3806 | (if (not before) | |
3807 | (newline 1))))) | |
3808 | (if (and dbl-space (not (> relative-depth 0))) | |
3809 | (newline 1)) | |
3810 | (if (and (not (eobp)) | |
3811 | (or (not (bolp)) | |
3812 | (and (not (bobp)) | |
3813 | ;; bolp doesnt detect concealed | |
3814 | ;; trailing newlines, compensate: | |
3815 | (save-excursion | |
3816 | (forward-char -1) | |
3817 | (allout-hidden-p))))) | |
3818 | (forward-char 1)))) | |
3819 | )) | |
3820 | (setq start (point)) | |
3821 | (insert (concat (allout-make-topic-prefix opening-numbered t depth) | |
3822 | " ")) | |
3823 | (setq end (1+ (point))) | |
3824 | ||
3825 | (allout-rebullet-heading (and offer-recent-bullet ref-bullet) | |
3826 | depth nil nil t) | |
3827 | (if (> relative-depth 0) | |
3828 | (save-excursion (goto-char ref-topic) | |
3829 | (allout-show-children))) | |
3830 | (end-of-line) | |
48bd8440 | 3831 | |
2a1408fd DN |
3832 | (run-hook-with-args 'allout-structure-added-hook start end) |
3833 | ) | |
1977b8f6 RS |
3834 | ) |
3835 | ) | |
fd5359c6 MR |
3836 | ;;;_ > allout-open-subtopic (arg) |
3837 | (defun allout-open-subtopic (arg) | |
c567ac01 RS |
3838 | "Open new topic header at deeper level than the current one. |
3839 | ||
3840 | Negative universal arg means to open deeper, but place the new topic | |
3841 | prior to the current one." | |
1977b8f6 | 3842 | (interactive "p") |
d82979ea | 3843 | (allout-open-topic 1 (> 0 arg) (< 1 arg))) |
fd5359c6 MR |
3844 | ;;;_ > allout-open-sibtopic (arg) |
3845 | (defun allout-open-sibtopic (arg) | |
19b84ba3 RS |
3846 | "Open new topic header at same level as the current one. |
3847 | ||
9179616f DL |
3848 | Positive universal arg means to use the bullet of the prior sibling. |
3849 | ||
19b84ba3 | 3850 | Negative universal arg means to place the new topic prior to the current |
c567ac01 | 3851 | one." |
1977b8f6 | 3852 | (interactive "p") |
d82979ea | 3853 | (allout-open-topic 0 (> 0 arg) (not (= 1 arg)))) |
fd5359c6 MR |
3854 | ;;;_ > allout-open-supertopic (arg) |
3855 | (defun allout-open-supertopic (arg) | |
c567ac01 | 3856 | "Open new topic header at shallower level than the current one. |
19b84ba3 | 3857 | |
c567ac01 RS |
3858 | Negative universal arg means to open shallower, but place the new |
3859 | topic prior to the current one." | |
1977b8f6 RS |
3860 | |
3861 | (interactive "p") | |
d82979ea | 3862 | (allout-open-topic -1 (> 0 arg) (< 1 arg))) |
c567ac01 RS |
3863 | |
3864 | ;;;_ - Outline Alteration | |
3865 | ;;;_ : Topic Modification | |
fd5359c6 MR |
3866 | ;;;_ = allout-former-auto-filler |
3867 | (defvar allout-former-auto-filler nil | |
2265e017 | 3868 | "Name of modal fill function being wrapped by `allout-auto-fill'.") |
fd5359c6 MR |
3869 | ;;;_ > allout-auto-fill () |
3870 | (defun allout-auto-fill () | |
aad94676 | 3871 | "`allout-mode' autofill function. |
19b84ba3 RS |
3872 | |
3873 | Maintains outline hanging topic indentation if | |
fd5359c6 | 3874 | `allout-use-hanging-indents' is set." |
01fc9422 EZ |
3875 | |
3876 | (when (not allout-inhibit-auto-fill) | |
3877 | (let ((fill-prefix (if allout-use-hanging-indents | |
3878 | ;; Check for topic header indentation: | |
2a1408fd DN |
3879 | (save-match-data |
3880 | (save-excursion | |
3881 | (beginning-of-line) | |
3882 | (if (looking-at allout-regexp) | |
3883 | ;; ... construct indentation to account for | |
3884 | ;; length of topic prefix: | |
3885 | (make-string (progn (allout-end-of-prefix) | |
3886 | (current-column)) | |
3887 | ?\ )))))) | |
01fc9422 EZ |
3888 | (use-auto-fill-function (or allout-outside-normal-auto-fill-function |
3889 | auto-fill-function | |
3890 | 'do-auto-fill))) | |
3891 | (if (or allout-former-auto-filler allout-use-hanging-indents) | |
3892 | (funcall use-auto-fill-function))))) | |
fd5359c6 MR |
3893 | ;;;_ > allout-reindent-body (old-depth new-depth &optional number) |
3894 | (defun allout-reindent-body (old-depth new-depth &optional number) | |
e126900f | 3895 | "Reindent body lines which were indented at OLD-DEPTH to NEW-DEPTH. |
c567ac01 RS |
3896 | |
3897 | Optional arg NUMBER indicates numbering is being added, and it must | |
a0776d6b | 3898 | be accommodated. |
c567ac01 RS |
3899 | |
3900 | Note that refill of indented paragraphs is not done." | |
1977b8f6 RS |
3901 | |
3902 | (save-excursion | |
fd5359c6 | 3903 | (allout-end-of-prefix) |
c567ac01 RS |
3904 | (let* ((new-margin (current-column)) |
3905 | excess old-indent-begin old-indent-end | |
c567ac01 RS |
3906 | ;; We want the column where the header-prefix text started |
3907 | ;; *before* the prefix was changed, so we infer it relative | |
3908 | ;; to the new margin and the shift in depth: | |
3909 | (old-margin (+ old-depth (- new-margin new-depth)))) | |
353e2ef2 | 3910 | |
c567ac01 | 3911 | ;; Process lines up to (but excluding) next topic header: |
fd5359c6 | 3912 | (allout-unprotected |
c567ac01 RS |
3913 | (save-match-data |
3914 | (while | |
96b83743 | 3915 | (and (re-search-forward "\n\\(\\s-*\\)" |
c567ac01 RS |
3916 | nil |
3917 | t) | |
3918 | ;; Register the indent data, before we reset the | |
353e2ef2 | 3919 | ;; match data with a subsequent `looking-at': |
c567ac01 RS |
3920 | (setq old-indent-begin (match-beginning 1) |
3921 | old-indent-end (match-end 1)) | |
fd5359c6 | 3922 | (not (looking-at allout-regexp))) |
d82979ea EZ |
3923 | (if (> 0 (setq excess (- (- old-indent-end old-indent-begin) |
3924 | old-margin))) | |
60c7bbd2 | 3925 | ;; Text starts left of old margin -- don't adjust: |
c567ac01 | 3926 | nil |
ea18b2b1 | 3927 | ;; Text was hanging at or right of old left margin -- |
c567ac01 RS |
3928 | ;; reindent it, preserving its existing indentation |
3929 | ;; beyond the old margin: | |
3930 | (delete-region old-indent-begin old-indent-end) | |
d82979ea | 3931 | (indent-to (+ new-margin excess (current-column)))))))))) |
fd5359c6 MR |
3932 | ;;;_ > allout-rebullet-current-heading (arg) |
3933 | (defun allout-rebullet-current-heading (arg) | |
9179616f DL |
3934 | "Solicit new bullet for current visible heading." |
3935 | (interactive "p") | |
3936 | (let ((initial-col (current-column)) | |
fd5359c6 | 3937 | (on-bullet (eq (point)(allout-current-bullet-pos))) |
d8350998 | 3938 | from to |
9179616f DL |
3939 | (backwards (if (< arg 0) |
3940 | (setq arg (* arg -1))))) | |
3941 | (while (> arg 0) | |
fd5359c6 MR |
3942 | (save-excursion (allout-back-to-current-heading) |
3943 | (allout-end-of-prefix) | |
d8350998 CY |
3944 | (setq from allout-recent-prefix-beginning |
3945 | to allout-recent-prefix-end) | |
fd5359c6 | 3946 | (allout-rebullet-heading t ;;; solicit |
9179616f DL |
3947 | nil ;;; depth |
3948 | nil ;;; number-control | |
3949 | nil ;;; index | |
d8350998 CY |
3950 | t) ;;; do-successors |
3951 | (run-hook-with-args 'allout-exposure-change-hook | |
3952 | from to t)) | |
9179616f DL |
3953 | (setq arg (1- arg)) |
3954 | (if (<= arg 0) | |
3955 | nil | |
3956 | (setq initial-col nil) ; Override positioning back to init col | |
3957 | (if (not backwards) | |
fd5359c6 | 3958 | (allout-next-visible-heading 1) |
d8350998 | 3959 | (allout-goto-prefix-doublechecked) |
fd5359c6 | 3960 | (allout-next-visible-heading -1)))) |
9179616f | 3961 | (message "Done.") |
fd5359c6 | 3962 | (cond (on-bullet (goto-char (allout-current-bullet-pos))) |
9179616f | 3963 | (initial-col (move-to-column initial-col))))) |
fd5359c6 MR |
3964 | ;;;_ > allout-rebullet-heading (&optional solicit ...) |
3965 | (defun allout-rebullet-heading (&optional solicit | |
1977b8f6 RS |
3966 | new-depth |
3967 | number-control | |
3968 | index | |
3969 | do-successors) | |
3970 | ||
c567ac01 | 3971 | "Adjust bullet of current topic prefix. |
1977b8f6 | 3972 | |
d82979ea EZ |
3973 | All args are optional. |
3974 | ||
9179616f DL |
3975 | If SOLICIT is non-nil, then the choice of bullet is solicited from |
3976 | user. If it's a character, then that character is offered as the | |
c07583cd | 3977 | default, otherwise the one suited to the context (according to |
9179616f DL |
3978 | distinction or depth) is offered. If non-nil, then the |
3979 | context-specific bullet is just used. | |
1977b8f6 | 3980 | |
d82979ea | 3981 | Second arg DEPTH forces the topic prefix to that depth, regardless |
9179616f | 3982 | of the topic's current depth. |
1977b8f6 | 3983 | |
c567ac01 | 3984 | Third arg NUMBER-CONTROL can force the prefix to or away from |
fd5359c6 | 3985 | numbered form. It has effect only if `allout-numbered-bullet' is |
c567ac01 RS |
3986 | non-nil and soliciting was not explicitly invoked (via first arg). |
3987 | Its effect, numbering or denumbering, then depends on the setting | |
75bcb912 | 3988 | of the fourth arg, INDEX. |
1977b8f6 | 3989 | |
75bcb912 | 3990 | If NUMBER-CONTROL is non-nil and fourth arg INDEX is nil, then the |
c567ac01 RS |
3991 | prefix of the topic is forced to be non-numbered. Null index and |
3992 | non-nil NUMBER-CONTROL forces denumbering. Non-nil INDEX (and | |
3993 | non-nil NUMBER-CONTROL) forces a numbered-prefix form. If non-nil | |
3994 | INDEX is a number, then that number is used for the numbered | |
3995 | prefix. Non-nil and non-number means that the index for the | |
d82979ea | 3996 | numbered prefix will be derived by allout-make-topic-prefix. |
1977b8f6 | 3997 | |
c567ac01 RS |
3998 | Fifth arg DO-SUCCESSORS t means re-resolve count on succeeding |
3999 | siblings. | |
1977b8f6 | 4000 | |
fd5359c6 MR |
4001 | Cf vars `allout-stylish-prefixes', `allout-old-style-prefixes', |
4002 | and `allout-numbered-bullet', which all affect the behavior of | |
c567ac01 | 4003 | this function." |
1977b8f6 | 4004 | |
fd5359c6 | 4005 | (let* ((current-depth (allout-depth)) |
1977b8f6 | 4006 | (new-depth (or new-depth current-depth)) |
fd5359c6 MR |
4007 | (mb allout-recent-prefix-beginning) |
4008 | (me allout-recent-prefix-end) | |
d8350998 | 4009 | (current-bullet (buffer-substring-no-properties (- me 1) me)) |
ede4ac6a | 4010 | (has-annotation (get-text-property mb 'allout-was-hidden)) |
fd5359c6 | 4011 | (new-prefix (allout-make-topic-prefix current-bullet |
1977b8f6 RS |
4012 | nil |
4013 | new-depth | |
4014 | solicit | |
4015 | number-control | |
4016 | index))) | |
4017 | ||
c567ac01 | 4018 | ;; Is new one is identical to old? |
1977b8f6 RS |
4019 | (if (and (= current-depth new-depth) |
4020 | (string= current-bullet | |
4021 | (substring new-prefix (1- (length new-prefix))))) | |
c567ac01 | 4022 | ;; Nothing to do: |
1977b8f6 RS |
4023 | t |
4024 | ||
4025 | ;; New prefix probably different from old: | |
c567ac01 | 4026 | ; get rid of old one: |
fd5359c6 | 4027 | (allout-unprotected (delete-region mb me)) |
1977b8f6 | 4028 | (goto-char mb) |
c567ac01 RS |
4029 | ; Dispense with number if |
4030 | ; numbered-bullet prefix: | |
2a1408fd DN |
4031 | (save-match-data |
4032 | (if (and allout-numbered-bullet | |
4033 | (string= allout-numbered-bullet current-bullet) | |
4034 | (looking-at "[0-9]+")) | |
4035 | (allout-unprotected | |
4036 | (delete-region (match-beginning 0)(match-end 0))))) | |
1977b8f6 | 4037 | |
ede4ac6a KS |
4038 | ;; convey 'allout-was-hidden annotation, if original had it: |
4039 | (if has-annotation | |
4040 | (put-text-property 0 (length new-prefix) 'allout-was-hidden t | |
4041 | new-prefix)) | |
4042 | ||
c567ac01 | 4043 | ; Put in new prefix: |
fd5359c6 | 4044 | (allout-unprotected (insert new-prefix)) |
1977b8f6 | 4045 | |
d82979ea | 4046 | ;; Reindent the body if elected, margin changed, and not encrypted body: |
fd5359c6 | 4047 | (if (and allout-reindent-bodies |
d82979ea EZ |
4048 | (not (= new-depth current-depth)) |
4049 | (not (allout-encrypted-topic-p))) | |
fd5359c6 | 4050 | (allout-reindent-body current-depth new-depth)) |
1977b8f6 | 4051 | |
c567ac01 RS |
4052 | ;; Recursively rectify successive siblings of orig topic if |
4053 | ;; caller elected for it: | |
4054 | (if do-successors | |
4055 | (save-excursion | |
fd5359c6 | 4056 | (while (allout-next-sibling new-depth nil) |
c567ac01 RS |
4057 | (setq index |
4058 | (cond ((numberp index) (1+ index)) | |
fd5359c6 MR |
4059 | ((not number-control) (allout-sibling-index)))) |
4060 | (if (allout-numbered-type-prefix) | |
4061 | (allout-rebullet-heading nil ;;; solicit | |
c567ac01 RS |
4062 | new-depth ;;; new-depth |
4063 | number-control;;; number-control | |
4064 | index ;;; index | |
4065 | nil))))) ;;;(dont!)do-successors | |
4066 | ) ; (if (and (= current-depth new-depth)...)) | |
fd5359c6 | 4067 | ) ; let* ((current-depth (allout-depth))...) |
c567ac01 | 4068 | ) ; defun |
fd5359c6 | 4069 | ;;;_ > allout-rebullet-topic (arg) |
d8350998 | 4070 | (defun allout-rebullet-topic (arg &optional sans-offspring) |
d82979ea | 4071 | "Rebullet the visible topic containing point and all contained subtopics. |
19b84ba3 | 4072 | |
c567ac01 | 4073 | Descends into invisible as well as visible topics, however. |
1977b8f6 | 4074 | |
75bcb912 | 4075 | When optional SANS-OFFSPRING is non-nil, subtopics are not |
c07583cd | 4076 | shifted. (Shifting a topic outwards without shifting its |
d8350998 CY |
4077 | offspring is disallowed, since this would create a \"containment |
4078 | discontinuity\", where the depth difference between a topic and | |
4079 | its immediate offspring is greater than one.) | |
4080 | ||
c567ac01 | 4081 | With repeat count, shift topic depth by that amount." |
1977b8f6 | 4082 | (interactive "P") |
96b83743 | 4083 | (let ((start-col (current-column))) |
1977b8f6 RS |
4084 | (save-excursion |
4085 | ;; Normalize arg: | |
4086 | (cond ((null arg) (setq arg 0)) | |
4087 | ((listp arg) (setq arg (car arg)))) | |
4088 | ;; Fill the user in, in case we're shifting a big topic: | |
4089 | (if (not (zerop arg)) (message "Shifting...")) | |
fd5359c6 | 4090 | (allout-back-to-current-heading) |
d8350998 | 4091 | (if (<= (+ allout-recent-depth arg) 0) |
1977b8f6 | 4092 | (error "Attempt to shift topic below level 1")) |
d8350998 | 4093 | (allout-rebullet-topic-grunt arg nil nil nil nil sans-offspring) |
1977b8f6 | 4094 | (if (not (zerop arg)) (message "Shifting... done."))) |
c567ac01 | 4095 | (move-to-column (max 0 (+ start-col arg))))) |
d8350998 | 4096 | ;;;_ > allout-rebullet-topic-grunt (&optional relative-depth ...) |
fd5359c6 | 4097 | (defun allout-rebullet-topic-grunt (&optional relative-depth |
1977b8f6 RS |
4098 | starting-depth |
4099 | starting-point | |
4100 | index | |
d8350998 CY |
4101 | do-successors |
4102 | sans-offspring) | |
d82979ea EZ |
4103 | "Like `allout-rebullet-topic', but on nearest containing topic |
4104 | \(visible or not). | |
1977b8f6 | 4105 | |
d82979ea | 4106 | See `allout-rebullet-heading' for rebulleting behavior. |
1977b8f6 | 4107 | |
d82979ea | 4108 | All arguments are optional. |
1977b8f6 | 4109 | |
d82979ea EZ |
4110 | First arg RELATIVE-DEPTH means to shift the depth of the entire |
4111 | topic that amount. | |
e126900f | 4112 | |
d8350998 CY |
4113 | Several subsequent args are for internal recursive use by the function |
4114 | itself: STARTING-DEPTH, STARTING-POINT, and INDEX. | |
4115 | ||
4116 | Finally, if optional SANS-OFFSPRING is non-nil then the offspring | |
c07583cd | 4117 | are not shifted. (Shifting a topic outwards without shifting |
d8350998 CY |
4118 | its offspring is disallowed, since this would create a |
4119 | \"containment discontinuity\", where the depth difference between | |
75bcb912 | 4120 | a topic and its immediate offspring is greater than one.)" |
d8350998 CY |
4121 | |
4122 | ;; XXX the recursion here is peculiar, and in general the routine may | |
4123 | ;; need simplification with refactoring. | |
4124 | ||
4125 | (if (and sans-offspring | |
4126 | relative-depth | |
4127 | (< relative-depth 0)) | |
4128 | (error (concat "Attempt to shift topic outwards without offspring," | |
4129 | " would cause containment discontinuity."))) | |
1977b8f6 RS |
4130 | |
4131 | (let* ((relative-depth (or relative-depth 0)) | |
fd5359c6 | 4132 | (new-depth (allout-depth)) |
1977b8f6 RS |
4133 | (starting-depth (or starting-depth new-depth)) |
4134 | (on-starting-call (null starting-point)) | |
4135 | (index (or index | |
4136 | ;; Leave index null on starting call, so rebullet-heading | |
4137 | ;; calculates it at what might be new depth: | |
4138 | (and (or (zerop relative-depth) | |
4139 | (not on-starting-call)) | |
fd5359c6 | 4140 | (allout-sibling-index)))) |
d8350998 | 4141 | (starting-index index) |
1977b8f6 | 4142 | (moving-outwards (< 0 relative-depth)) |
d8350998 CY |
4143 | (starting-point (or starting-point (point))) |
4144 | (local-point (point))) | |
1977b8f6 RS |
4145 | |
4146 | ;; Sanity check for excessive promotion done only on starting call: | |
4147 | (and on-starting-call | |
4148 | moving-outwards | |
4149 | (> 0 (+ starting-depth relative-depth)) | |
d8350998 | 4150 | (error "Attempt to shift topic out beyond level 1")) |
1977b8f6 RS |
4151 | |
4152 | (cond ((= starting-depth new-depth) | |
d8350998 CY |
4153 | ;; We're at depth to work on this one. |
4154 | ||
4155 | ;; When shifting out we work on the children before working on | |
4156 | ;; the parent to avoid interim `allout-aberrant-container-p' | |
4157 | ;; aberrancy, and vice-versa when shifting in: | |
4158 | (if (>= relative-depth 0) | |
4159 | (allout-rebullet-heading nil | |
4160 | (+ starting-depth relative-depth) | |
4161 | nil ;;; number | |
4162 | index | |
4163 | nil)) ;;; do-successors | |
4164 | (when (not sans-offspring) | |
4165 | ;; ... and work on subsequent ones which are at greater depth: | |
4166 | (setq index 0) | |
4167 | (allout-next-heading) | |
4168 | (while (and (not (eobp)) | |
4169 | (< starting-depth (allout-depth))) | |
4170 | (setq index (1+ index)) | |
4171 | (allout-rebullet-topic-grunt relative-depth | |
4172 | (1+ starting-depth) | |
4173 | starting-point | |
4174 | index))) | |
4175 | (when (< relative-depth 0) | |
4176 | (save-excursion | |
4177 | (goto-char local-point) | |
4178 | (allout-rebullet-heading nil ;;; solicit | |
4179 | (+ starting-depth relative-depth) | |
4180 | nil ;;; number | |
4181 | starting-index | |
4182 | nil)))) ;;; do-successors | |
1977b8f6 RS |
4183 | |
4184 | ((< starting-depth new-depth) | |
60c7bbd2 | 4185 | ;; Rare case -- subtopic more than one level deeper than parent. |
1977b8f6 | 4186 | ;; Treat this one at an even deeper level: |
d8350998 CY |
4187 | (allout-rebullet-topic-grunt relative-depth |
4188 | new-depth | |
4189 | starting-point | |
4190 | index | |
4191 | sans-offspring))) | |
1977b8f6 RS |
4192 | |
4193 | (if on-starting-call | |
4194 | (progn | |
4195 | ;; Rectify numbering of former siblings of the adjusted topic, | |
4196 | ;; if topic has changed depth | |
4197 | (if (or do-successors | |
4198 | (and (not (zerop relative-depth)) | |
d8350998 CY |
4199 | (or (= allout-recent-depth starting-depth) |
4200 | (= allout-recent-depth (+ starting-depth | |
1977b8f6 | 4201 | relative-depth))))) |
fd5359c6 | 4202 | (allout-rebullet-heading nil nil nil nil t)) |
1977b8f6 RS |
4203 | ;; Now rectify numbering of new siblings of the adjusted topic, |
4204 | ;; if depth has been changed: | |
4205 | (progn (goto-char starting-point) | |
4206 | (if (not (zerop relative-depth)) | |
fd5359c6 | 4207 | (allout-rebullet-heading nil nil nil nil t))))) |
1977b8f6 RS |
4208 | ) |
4209 | ) | |
fd5359c6 MR |
4210 | ;;;_ > allout-renumber-to-depth (&optional depth) |
4211 | (defun allout-renumber-to-depth (&optional depth) | |
19b84ba3 RS |
4212 | "Renumber siblings at current depth. |
4213 | ||
4214 | Affects superior topics if optional arg DEPTH is less than current depth. | |
c567ac01 RS |
4215 | |
4216 | Returns final depth." | |
4217 | ||
4218 | ;; Proceed by level, processing subsequent siblings on each, | |
4219 | ;; ascending until we get shallower than the start depth: | |
4220 | ||
fd5359c6 | 4221 | (let ((ascender (allout-depth)) |
9179616f | 4222 | was-eobp) |
c567ac01 | 4223 | (while (and (not (eobp)) |
fd5359c6 | 4224 | (allout-depth) |
d8350998 | 4225 | (>= allout-recent-depth depth) |
c567ac01 RS |
4226 | (>= ascender depth)) |
4227 | ; Skip over all topics at | |
4228 | ; lesser depths, which can not | |
4229 | ; have been disturbed: | |
9179616f | 4230 | (while (and (not (setq was-eobp (eobp))) |
d8350998 | 4231 | (> allout-recent-depth ascender)) |
fd5359c6 | 4232 | (allout-next-heading)) |
c567ac01 | 4233 | ; Prime ascender for ascension: |
d8350998 CY |
4234 | (setq ascender (1- allout-recent-depth)) |
4235 | (if (>= allout-recent-depth depth) | |
fd5359c6 | 4236 | (allout-rebullet-heading nil ;;; solicit |
c567ac01 RS |
4237 | nil ;;; depth |
4238 | nil ;;; number-control | |
4239 | nil ;;; index | |
9179616f DL |
4240 | t)) ;;; do-successors |
4241 | (if was-eobp (goto-char (point-max))))) | |
d8350998 | 4242 | allout-recent-depth) |
fd5359c6 MR |
4243 | ;;;_ > allout-number-siblings (&optional denumber) |
4244 | (defun allout-number-siblings (&optional denumber) | |
c567ac01 | 4245 | "Assign numbered topic prefix to this topic and its siblings. |
1977b8f6 | 4246 | |
60c7bbd2 | 4247 | With universal argument, denumber -- assign default bullet to this |
c567ac01 | 4248 | topic and its siblings. |
1977b8f6 | 4249 | |
c567ac01 RS |
4250 | With repeated universal argument (`^U^U'), solicit bullet for each |
4251 | rebulleting each topic at this level." | |
1977b8f6 RS |
4252 | |
4253 | (interactive "P") | |
4254 | ||
4255 | (save-excursion | |
fd5359c6 MR |
4256 | (allout-back-to-current-heading) |
4257 | (allout-beginning-of-level) | |
d8350998 | 4258 | (let ((depth allout-recent-depth) |
c567ac01 | 4259 | (index (if (not denumber) 1)) |
1977b8f6 RS |
4260 | (use-bullet (equal '(16) denumber)) |
4261 | (more t)) | |
4262 | (while more | |
fd5359c6 | 4263 | (allout-rebullet-heading use-bullet ;;; solicit |
c567ac01 | 4264 | depth ;;; depth |
1977b8f6 RS |
4265 | t ;;; number-control |
4266 | index ;;; index | |
4267 | nil) ;;; do-successors | |
4268 | (if index (setq index (1+ index))) | |
fd5359c6 MR |
4269 | (setq more (allout-next-sibling depth nil)))))) |
4270 | ;;;_ > allout-shift-in (arg) | |
4271 | (defun allout-shift-in (arg) | |
d8350998 CY |
4272 | "Increase depth of current heading and any items collapsed within it. |
4273 | ||
4274 | With a negative argument, the item is shifted out using | |
4275 | `allout-shift-out', instead. | |
4276 | ||
4277 | With an argument greater than one, shift-in the item but not its | |
4278 | offspring, making the item into a sibling of its former children, | |
4279 | and a child of sibling that formerly preceeded it. | |
4280 | ||
4281 | You are not allowed to shift the first offspring of a topic | |
4282 | inwards, because that would yield a \"containment | |
4283 | discontinuity\", where the depth difference between a topic and | |
4284 | its immediate offspring is greater than one. The first topic in | |
4285 | the file can be adjusted to any positive depth, however." | |
d82979ea | 4286 | |
1977b8f6 | 4287 | (interactive "p") |
d8350998 CY |
4288 | (if (< arg 0) |
4289 | (allout-shift-out (* arg -1)) | |
4290 | ;; refuse to create a containment discontinuity: | |
4291 | (save-excursion | |
4292 | (allout-back-to-current-heading) | |
4293 | (if (not (bobp)) | |
4294 | (let* ((current-depth allout-recent-depth) | |
4295 | (start-point (point)) | |
4296 | (predecessor-depth (progn | |
4297 | (forward-char -1) | |
4298 | (allout-goto-prefix-doublechecked) | |
4299 | (if (< (point) start-point) | |
4300 | allout-recent-depth | |
4301 | 0)))) | |
4302 | (if (and (> predecessor-depth 0) | |
4303 | (> (1+ current-depth) | |
4304 | (1+ predecessor-depth))) | |
4305 | (error (concat "Disallowed shift deeper than" | |
498a6493 CY |
4306 | " containing topic's children.")) |
4307 | (allout-back-to-current-heading) | |
4308 | (if (< allout-recent-depth (1+ current-depth)) | |
4309 | (allout-show-children)))))) | |
d8350998 CY |
4310 | (let ((where (point))) |
4311 | (allout-rebullet-topic 1 (and (> arg 1) 'sans-offspring)) | |
4312 | (run-hook-with-args 'allout-structure-shifted-hook arg where)))) | |
fd5359c6 MR |
4313 | ;;;_ > allout-shift-out (arg) |
4314 | (defun allout-shift-out (arg) | |
d82979ea | 4315 | "Decrease depth of current heading and any topics collapsed within it. |
d8350998 CY |
4316 | This will make the item a sibling of its former container. |
4317 | ||
4318 | With a negative argument, the item is shifted in using | |
4319 | `allout-shift-in', instead. | |
4320 | ||
4321 | With an argument greater than one, shift-out the item's offspring | |
4322 | but not the item itself, making the former children siblings of | |
4323 | the item. | |
d82979ea | 4324 | |
d8350998 CY |
4325 | With an argument greater than 1, the item's offspring are shifted |
4326 | out without shifting the item. This will make the immediate | |
4327 | subtopics into siblings of the item." | |
1977b8f6 | 4328 | (interactive "p") |
d8350998 CY |
4329 | (if (< arg 0) |
4330 | (allout-shift-in (* arg -1)) | |
4331 | ;; Get proper exposure in this area: | |
4332 | (save-excursion (if (allout-ascend) | |
4333 | (allout-show-children))) | |
4334 | ;; Show collapsed children if there's a successor which will become | |
4335 | ;; their sibling: | |
4336 | (if (and (allout-current-topic-collapsed-p) | |
4337 | (save-excursion (allout-next-sibling))) | |
4338 | (allout-show-children)) | |
4339 | (let ((where (and (allout-depth) allout-recent-prefix-beginning))) | |
4340 | (save-excursion | |
4341 | (if (> arg 1) | |
4342 | ;; Shift the offspring but not the topic: | |
4343 | (let ((children-chart (allout-chart-subtree 1))) | |
4344 | (if (listp (car children-chart)) | |
4345 | ;; whoops: | |
4346 | (setq children-chart (allout-flatten children-chart))) | |
4347 | (save-excursion | |
4348 | (dolist (child-point children-chart) | |
4349 | (goto-char child-point) | |
4350 | (allout-shift-out 1)))) | |
4351 | (allout-rebullet-topic (* arg -1)))) | |
4352 | (run-hook-with-args 'allout-structure-shifted-hook (* arg -1) where)))) | |
c567ac01 | 4353 | ;;;_ : Surgery (kill-ring) functions with special provisions for outlines: |
fd5359c6 MR |
4354 | ;;;_ > allout-kill-line (&optional arg) |
4355 | (defun allout-kill-line (&optional arg) | |
c567ac01 | 4356 | "Kill line, adjusting subsequent lines suitably for outline mode." |
1977b8f6 RS |
4357 | |
4358 | (interactive "*P") | |
d82979ea | 4359 | |
96b83743 EZ |
4360 | (if (or (not (allout-mode-p)) |
4361 | (not (bolp)) | |
2a1408fd | 4362 | (not (save-match-data (looking-at allout-regexp)))) |
4034b0e2 | 4363 | ;; Just do a regular kill: |
96b83743 EZ |
4364 | (kill-line arg) |
4365 | ;; Ah, have to watch out for adjustments: | |
4366 | (let* ((beg (point)) | |
d8350998 | 4367 | end |
96b83743 EZ |
4368 | (beg-hidden (allout-hidden-p)) |
4369 | (end-hidden (save-excursion (allout-end-of-current-line) | |
d8350998 | 4370 | (setq end (point)) |
96b83743 | 4371 | (allout-hidden-p))) |
d8350998 | 4372 | (depth (allout-depth))) |
96b83743 | 4373 | |
d8350998 | 4374 | (allout-annotate-hidden beg end) |
96b83743 EZ |
4375 | (if (and (not beg-hidden) (not end-hidden)) |
4376 | (allout-unprotected (kill-line arg)) | |
4377 | (kill-line arg)) | |
ede4ac6a KS |
4378 | (allout-deannotate-hidden beg end) |
4379 | ||
96b83743 EZ |
4380 | (if allout-numbered-bullet |
4381 | (save-excursion ; Renumber subsequent topics if needed: | |
2a1408fd | 4382 | (if (not (save-match-data (looking-at allout-regexp))) |
d82979ea | 4383 | (allout-next-heading)) |
48bd8440 EZ |
4384 | (allout-renumber-to-depth depth))) |
4385 | (run-hook-with-args 'allout-structure-deleted-hook depth (point))))) | |
1b4d97b2 CY |
4386 | ;;;_ > allout-copy-line-as-kill () |
4387 | (defun allout-copy-line-as-kill () | |
4388 | "Like allout-kill-topic, but save to kill ring instead of deleting." | |
4389 | (interactive) | |
4390 | (let ((buffer-read-only t)) | |
4391 | (condition-case nil | |
4392 | (allout-kill-line) | |
4393 | (buffer-read-only nil)))) | |
fd5359c6 MR |
4394 | ;;;_ > allout-kill-topic () |
4395 | (defun allout-kill-topic () | |
c567ac01 RS |
4396 | "Kill topic together with subtopics. |
4397 | ||
96b83743 EZ |
4398 | Trailing whitespace is killed with a topic if that whitespace: |
4399 | ||
4400 | - would separate the topic from a subsequent sibling | |
4401 | - would separate the topic from the end of buffer | |
4402 | - would not be added to whitespace already separating the topic from the | |
4403 | previous one. | |
4404 | ||
d8350998 | 4405 | Topic exposure is marked with text-properties, to be used by |
75bcb912 | 4406 | `allout-yank-processing' for exposure recovery." |
1977b8f6 RS |
4407 | |
4408 | (interactive) | |
0458fa80 | 4409 | (let* ((inhibit-field-text-motion t) |
96b83743 | 4410 | (beg (prog1 (allout-back-to-current-heading) (beginning-of-line))) |
ede4ac6a | 4411 | end |
d8350998 | 4412 | (depth allout-recent-depth)) |
fd5359c6 | 4413 | (allout-end-of-current-subtree) |
96b83743 EZ |
4414 | (if (and (/= (current-column) 0) (not (eobp))) |
4415 | (forward-char 1)) | |
1977b8f6 | 4416 | (if (not (eobp)) |
2a1408fd | 4417 | (if (and (save-match-data (looking-at "\n")) |
96b83743 EZ |
4418 | (or (save-excursion |
4419 | (or (not (allout-next-heading)) | |
d8350998 | 4420 | (= depth allout-recent-depth))) |
96b83743 EZ |
4421 | (and (> (- beg (point-min)) 3) |
4422 | (string= (buffer-substring (- beg 2) beg) "\n\n")))) | |
c567ac01 | 4423 | (forward-char 1))) |
353e2ef2 | 4424 | |
ede4ac6a KS |
4425 | (allout-annotate-hidden beg (setq end (point))) |
4426 | (unwind-protect | |
4427 | (allout-unprotected (kill-region beg end)) | |
4428 | (if buffer-read-only | |
4429 | ;; eg, during copy-as-kill. | |
4430 | (allout-deannotate-hidden beg end))) | |
d8350998 | 4431 | |
1977b8f6 | 4432 | (save-excursion |
48bd8440 EZ |
4433 | (allout-renumber-to-depth depth)) |
4434 | (run-hook-with-args 'allout-structure-deleted-hook depth (point)))) | |
1b4d97b2 CY |
4435 | ;;;_ > allout-copy-topic-as-kill () |
4436 | (defun allout-copy-topic-as-kill () | |
75bcb912 | 4437 | "Like `allout-kill-topic', but save to kill ring instead of deleting." |
1b4d97b2 CY |
4438 | (interactive) |
4439 | (let ((buffer-read-only t)) | |
4440 | (condition-case nil | |
4441 | (allout-kill-topic) | |
4442 | (buffer-read-only (message "Topic copied..."))))) | |
d8350998 CY |
4443 | ;;;_ > allout-annotate-hidden (begin end) |
4444 | (defun allout-annotate-hidden (begin end) | |
4445 | "Qualify text with properties to indicate exposure status." | |
4446 | ||
1b4d97b2 CY |
4447 | (let ((was-modified (buffer-modified-p)) |
4448 | (buffer-read-only nil)) | |
ede4ac6a | 4449 | (allout-deannotate-hidden begin end) |
d8350998 CY |
4450 | (save-excursion |
4451 | (goto-char begin) | |
4452 | (let (done next prev overlay) | |
4453 | (while (not done) | |
4454 | ;; at or advance to start of next hidden region: | |
4455 | (if (not (allout-hidden-p)) | |
4456 | (setq next | |
1b4d97b2 CY |
4457 | (max (1+ (point)) |
4458 | (next-single-char-property-change (point) | |
4459 | 'invisible | |
4460 | nil end)))) | |
d8350998 | 4461 | (if (or (not next) (eq prev next)) |
60c7bbd2 | 4462 | ;; still not at start of hidden area -- must not be any left. |
d8350998 CY |
4463 | (setq done t) |
4464 | (goto-char next) | |
4465 | (setq prev next) | |
4466 | (if (not (allout-hidden-p)) | |
4467 | ;; still not at start of hidden area. | |
4468 | (setq done t) | |
4469 | (setq overlay (allout-get-invisibility-overlay)) | |
4470 | (setq next (overlay-end overlay) | |
4471 | prev next) | |
4472 | ;; advance to end of this hidden area: | |
4473 | (when next | |
4474 | (goto-char next) | |
4475 | (allout-unprotected | |
ede4ac6a KS |
4476 | (let ((buffer-undo-list t)) |
4477 | (put-text-property (overlay-start overlay) next | |
4478 | 'allout-was-hidden t))))))))) | |
d8350998 | 4479 | (set-buffer-modified-p was-modified))) |
ede4ac6a KS |
4480 | ;;;_ > allout-deannotate-hidden (begin end) |
4481 | (defun allout-deannotate-hidden (begin end) | |
4482 | "Remove allout hidden-text annotation between BEGIN and END." | |
4483 | ||
4484 | (allout-unprotected | |
4485 | (let ((inhibit-read-only t) | |
4486 | (buffer-undo-list t)) | |
4487 | ;(remove-text-properties begin end '(allout-was-hidden t)) | |
4488 | ))) | |
d8350998 CY |
4489 | ;;;_ > allout-hide-by-annotation (begin end) |
4490 | (defun allout-hide-by-annotation (begin end) | |
4491 | "Translate text properties indicating exposure status into actual exposure." | |
4492 | (save-excursion | |
4493 | (goto-char begin) | |
4494 | (let ((was-modified (buffer-modified-p)) | |
4495 | done next prev) | |
4496 | (while (not done) | |
4497 | ;; at or advance to start of next annotation: | |
4498 | (if (not (get-text-property (point) 'allout-was-hidden)) | |
4499 | (setq next (next-single-char-property-change (point) | |
4500 | 'allout-was-hidden | |
4501 | nil end))) | |
4502 | (if (or (not next) (eq prev next)) | |
60c7bbd2 | 4503 | ;; no more or not advancing -- must not be any left. |
d8350998 CY |
4504 | (setq done t) |
4505 | (goto-char next) | |
4506 | (setq prev next) | |
4507 | (if (not (get-text-property (point) 'allout-was-hidden)) | |
4508 | ;; still not at start of annotation. | |
4509 | (setq done t) | |
4510 | ;; advance to just after end of this annotation: | |
4511 | (setq next (next-single-char-property-change (point) | |
4512 | 'allout-was-hidden | |
4513 | nil end)) | |
2a1408fd | 4514 | (overlay-put (make-overlay prev next nil 'front-advance) |
d8350998 | 4515 | 'category 'allout-exposure-category) |
ede4ac6a | 4516 | (allout-deannotate-hidden prev next) |
d8350998 CY |
4517 | (setq prev next) |
4518 | (if next (goto-char next))))) | |
4519 | (set-buffer-modified-p was-modified)))) | |
fd5359c6 MR |
4520 | ;;;_ > allout-yank-processing () |
4521 | (defun allout-yank-processing (&optional arg) | |
c567ac01 | 4522 | |
96b83743 | 4523 | "Incidental allout-specific business to be done just after text yanks. |
19b84ba3 | 4524 | |
c567ac01 | 4525 | Does depth adjustment of yanked topics, when: |
1977b8f6 | 4526 | |
c567ac01 RS |
4527 | 1 the stuff being yanked starts with a valid outline header prefix, and |
4528 | 2 it is being yanked at the end of a line which consists of only a valid | |
1977b8f6 RS |
4529 | topic prefix. |
4530 | ||
a0776d6b | 4531 | Also, adjusts numbering of subsequent siblings when appropriate. |
1977b8f6 | 4532 | |
c567ac01 RS |
4533 | Depth adjustment alters the depth of all the topics being yanked |
4534 | the amount it takes to make the first topic have the depth of the | |
4535 | header into which it's being yanked. | |
1977b8f6 | 4536 | |
c567ac01 RS |
4537 | The point is left in front of yanked, adjusted topics, rather than |
4538 | at the end (and vice-versa with the mark). Non-adjusted yanks, | |
d82979ea | 4539 | however, are left exactly like normal, non-allout-specific yanks." |
c567ac01 RS |
4540 | |
4541 | (interactive "*P") | |
4542 | ; Get to beginning, leaving | |
4543 | ; region around subject: | |
96b83743 | 4544 | (if (< (allout-mark-marker t) (point)) |
c567ac01 | 4545 | (exchange-point-and-mark)) |
2a1408fd DN |
4546 | (save-match-data |
4547 | (let* ((subj-beg (point)) | |
4548 | (into-bol (bolp)) | |
4549 | (subj-end (allout-mark-marker t)) | |
4550 | ;; 'resituate' if yanking an entire topic into topic header: | |
4551 | (resituate (and (let ((allout-inhibit-aberrance-doublecheck t)) | |
4552 | (allout-e-o-prefix-p)) | |
4553 | (looking-at allout-regexp) | |
4554 | (allout-prefix-data))) | |
4555 | ;; `rectify-numbering' if resituating (where several topics may | |
4556 | ;; be resituating) or yanking a topic into a topic slot (bol): | |
4557 | (rectify-numbering (or resituate | |
8dd59f01 | 4558 | (and into-bol (looking-at allout-regexp))))) |
2a1408fd | 4559 | (if resituate |
8dd59f01 | 4560 | ;; Yanking a topic into the start of a topic -- reconcile to fit: |
2a1408fd DN |
4561 | (let* ((inhibit-field-text-motion t) |
4562 | (prefix-len (if (not (match-end 1)) | |
4563 | 1 | |
4564 | (- (match-end 1) subj-beg))) | |
4565 | (subj-depth allout-recent-depth) | |
4566 | (prefix-bullet (allout-recent-bullet)) | |
4567 | (adjust-to-depth | |
4568 | ;; Nil if adjustment unnecessary, otherwise depth to which | |
4569 | ;; adjustment should be made: | |
4570 | (save-excursion | |
4571 | (and (goto-char subj-end) | |
4572 | (eolp) | |
4573 | (goto-char subj-beg) | |
4574 | (and (looking-at allout-regexp) | |
4575 | (progn | |
4576 | (beginning-of-line) | |
4577 | (not (= (point) subj-beg))) | |
4578 | (looking-at allout-regexp) | |
4579 | (allout-prefix-data)) | |
4580 | allout-recent-depth))) | |
4581 | (more t)) | |
4582 | (setq rectify-numbering allout-numbered-bullet) | |
4583 | (if adjust-to-depth | |
c567ac01 | 4584 | ; Do the adjustment: |
2a1408fd DN |
4585 | (progn |
4586 | (save-restriction | |
4587 | (narrow-to-region subj-beg subj-end) | |
c567ac01 RS |
4588 | ; Trim off excessive blank |
4589 | ; line at end, if any: | |
2a1408fd DN |
4590 | (goto-char (point-max)) |
4591 | (if (looking-at "^$") | |
4592 | (allout-unprotected (delete-char -1))) | |
c567ac01 RS |
4593 | ; Work backwards, with each |
4594 | ; shallowest level, | |
4595 | ; successively excluding the | |
4596 | ; last processed topic from | |
4597 | ; the narrow region: | |
2a1408fd DN |
4598 | (while more |
4599 | (allout-back-to-current-heading) | |
c567ac01 | 4600 | ; go as high as we can in each bunch: |
2a1408fd DN |
4601 | (while (allout-ascend t)) |
4602 | (save-excursion | |
4603 | (allout-unprotected | |
4604 | (allout-rebullet-topic-grunt (- adjust-to-depth | |
4605 | subj-depth))) | |
4606 | (allout-depth)) | |
4607 | (if (setq more (not (bobp))) | |
4608 | (progn (widen) | |
4609 | (forward-char -1) | |
4610 | (narrow-to-region subj-beg (point)))))) | |
4611 | ;; Preserve new bullet if it's a distinctive one, otherwise | |
4612 | ;; use old one: | |
4613 | (if (string-match (regexp-quote prefix-bullet) | |
4614 | allout-distinctive-bullets-string) | |
c567ac01 RS |
4615 | ; Delete from bullet of old to |
4616 | ; before bullet of new: | |
2a1408fd DN |
4617 | (progn |
4618 | (beginning-of-line) | |
4619 | (allout-unprotected | |
4620 | (delete-region (point) subj-beg)) | |
4621 | (set-marker (allout-mark-marker t) subj-end) | |
4622 | (goto-char subj-beg) | |
4623 | (allout-end-of-prefix)) | |
c567ac01 RS |
4624 | ; Delete base subj prefix, |
4625 | ; leaving old one: | |
2a1408fd DN |
4626 | (allout-unprotected |
4627 | (progn | |
4628 | (delete-region (point) (+ (point) | |
4629 | prefix-len | |
4630 | (- adjust-to-depth | |
4631 | subj-depth))) | |
c567ac01 RS |
4632 | ; and delete residual subj |
4633 | ; prefix digits and space: | |
2a1408fd DN |
4634 | (while (looking-at "[0-9]") (delete-char 1)) |
4635 | (if (looking-at " ") | |
4636 | (delete-char 1)))))) | |
4637 | (exchange-point-and-mark)))) | |
4638 | (if rectify-numbering | |
4639 | (progn | |
4640 | (save-excursion | |
c567ac01 | 4641 | ; Give some preliminary feedback: |
2a1408fd | 4642 | (message "... reconciling numbers") |
c567ac01 | 4643 | ; ... and renumber, in case necessary: |
2a1408fd DN |
4644 | (goto-char subj-beg) |
4645 | (if (allout-goto-prefix-doublechecked) | |
4646 | (allout-unprotected | |
4647 | (allout-rebullet-heading nil ;;; solicit | |
4648 | (allout-depth) ;;; depth | |
4649 | nil ;;; number-control | |
4650 | nil ;;; index | |
4651 | t))) | |
4652 | (message "")))) | |
4653 | (if (or into-bol resituate) | |
4654 | (allout-hide-by-annotation (point) (allout-mark-marker t)) | |
4655 | (allout-deannotate-hidden (allout-mark-marker t) (point))) | |
4656 | (if (not resituate) | |
4657 | (exchange-point-and-mark)) | |
4658 | (run-hook-with-args 'allout-structure-added-hook subj-beg subj-end)))) | |
fd5359c6 MR |
4659 | ;;;_ > allout-yank (&optional arg) |
4660 | (defun allout-yank (&optional arg) | |
aad94676 | 4661 | "`allout-mode' yank, with depth and numbering adjustment of yanked topics. |
19b84ba3 | 4662 | |
a0776d6b | 4663 | Non-topic yanks work no differently than normal yanks. |
c567ac01 RS |
4664 | |
4665 | If a topic is being yanked into a bare topic prefix, the depth of the | |
4666 | yanked topic is adjusted to the depth of the topic prefix. | |
4667 | ||
2265e017 | 4668 | 1 we're yanking in an `allout-mode' buffer |
c567ac01 RS |
4669 | 2 the stuff being yanked starts with a valid outline header prefix, and |
4670 | 3 it is being yanked at the end of a line which consists of only a valid | |
4671 | topic prefix. | |
4672 | ||
4673 | If these conditions hold then the depth of the yanked topics are all | |
4674 | adjusted the amount it takes to make the first one at the depth of the | |
4675 | header into which it's being yanked. | |
4676 | ||
4677 | The point is left in front of yanked, adjusted topics, rather than | |
4678 | at the end (and vice-versa with the mark). Non-adjusted yanks, | |
4679 | however, (ones that don't qualify for adjustment) are handled | |
4680 | exactly like normal yanks. | |
4681 | ||
a0776d6b | 4682 | Numbering of yanked topics, and the successive siblings at the depth |
c567ac01 RS |
4683 | into which they're being yanked, is adjusted. |
4684 | ||
539d7736 JB |
4685 | `allout-yank-pop' works with `allout-yank' just like normal `yank-pop' |
4686 | works with normal `yank' in non-outline buffers." | |
1977b8f6 RS |
4687 | |
4688 | (interactive "*P") | |
4689 | (setq this-command 'yank) | |
48bd8440 EZ |
4690 | (allout-unprotected |
4691 | (yank arg)) | |
fd5359c6 | 4692 | (if (allout-mode-p) |
48bd8440 | 4693 | (allout-yank-processing))) |
fd5359c6 MR |
4694 | ;;;_ > allout-yank-pop (&optional arg) |
4695 | (defun allout-yank-pop (&optional arg) | |
2265e017 | 4696 | "Yank-pop like `allout-yank' when popping to bare outline prefixes. |
19b84ba3 RS |
4697 | |
4698 | Adapts level of popped topics to level of fresh prefix. | |
c567ac01 | 4699 | |
60c7bbd2 | 4700 | Note -- prefix changes to distinctive bullets will stick, if followed |
c567ac01 | 4701 | by pops to non-distinctive yanks. Bug..." |
1977b8f6 RS |
4702 | |
4703 | (interactive "*p") | |
1977b8f6 | 4704 | (setq this-command 'yank) |
c567ac01 | 4705 | (yank-pop arg) |
fd5359c6 MR |
4706 | (if (allout-mode-p) |
4707 | (allout-yank-processing))) | |
1977b8f6 | 4708 | |
c567ac01 RS |
4709 | ;;;_ - Specialty bullet functions |
4710 | ;;;_ : File Cross references | |
fd5359c6 MR |
4711 | ;;;_ > allout-resolve-xref () |
4712 | (defun allout-resolve-xref () | |
19b84ba3 RS |
4713 | "Pop to file associated with current heading, if it has an xref bullet. |
4714 | ||
fd5359c6 | 4715 | \(Works according to setting of `allout-file-xref-bullet')." |
1977b8f6 | 4716 | (interactive) |
fd5359c6 | 4717 | (if (not allout-file-xref-bullet) |
1977b8f6 | 4718 | (error |
60c7bbd2 | 4719 | "Outline cross references disabled -- no `allout-file-xref-bullet'") |
fd5359c6 | 4720 | (if (not (string= (allout-current-bullet) allout-file-xref-bullet)) |
635ca796 | 4721 | (error "Current heading lacks cross-reference bullet `%s'" |
fd5359c6 | 4722 | allout-file-xref-bullet) |
0458fa80 EZ |
4723 | (let ((inhibit-field-text-motion t) |
4724 | file-name) | |
2a1408fd DN |
4725 | (save-match-data |
4726 | (save-excursion | |
4727 | (let* ((text-start allout-recent-prefix-end) | |
4728 | (heading-end (progn (end-of-line) (point)))) | |
4729 | (goto-char text-start) | |
4730 | (setq file-name | |
4731 | (if (re-search-forward "\\s-\\(\\S-*\\)" heading-end t) | |
4732 | (buffer-substring (match-beginning 1) | |
4733 | (match-end 1))))))) | |
69027dea | 4734 | (setq file-name (expand-file-name file-name)) |
1977b8f6 RS |
4735 | (if (or (file-exists-p file-name) |
4736 | (if (file-writable-p file-name) | |
4737 | (y-or-n-p (format "%s not there, create one? " | |
4738 | file-name)) | |
4739 | (error "%s not found and can't be created" file-name))) | |
4740 | (condition-case failure | |
4741 | (find-file-other-window file-name) | |
d8350998 | 4742 | (error failure)) |
1977b8f6 RS |
4743 | (error "%s not found" file-name)) |
4744 | ) | |
4745 | ) | |
4746 | ) | |
4747 | ) | |
19b84ba3 | 4748 | |
9179616f | 4749 | ;;;_ #6 Exposure Control |
c567ac01 RS |
4750 | |
4751 | ;;;_ - Fundamental | |
d82979ea EZ |
4752 | ;;;_ > allout-flag-region (from to flag) |
4753 | (defun allout-flag-region (from to flag) | |
48bd8440 EZ |
4754 | "Conceal text between FROM and TO if FLAG is non-nil, else reveal it. |
4755 | ||
4756 | Exposure-change hook `allout-exposure-change-hook' is run with the same | |
c07583cd | 4757 | arguments as this function, after the exposure changes are made. (The old |
48bd8440 | 4758 | `allout-view-change-hook' is being deprecated, and eventually will not be |
c07583cd | 4759 | invoked.)" |
96b83743 | 4760 | |
96b83743 | 4761 | ;; We use outline invisibility spec. |
01fc9422 | 4762 | (remove-overlays from to 'category 'allout-exposure-category) |
96b83743 | 4763 | (when flag |
2a1408fd | 4764 | (let ((o (make-overlay from to nil 'front-advance))) |
01fc9422 | 4765 | (overlay-put o 'category 'allout-exposure-category) |
96b83743 | 4766 | (when (featurep 'xemacs) |
01fc9422 | 4767 | (let ((props (symbol-plist 'allout-exposure-category))) |
96b83743 EZ |
4768 | (while props |
4769 | (overlay-put o (pop props) (pop props))))))) | |
da506f05 | 4770 | (run-hooks 'allout-view-change-hook) |
48bd8440 | 4771 | (run-hook-with-args 'allout-exposure-change-hook from to flag)) |
fd5359c6 MR |
4772 | ;;;_ > allout-flag-current-subtree (flag) |
4773 | (defun allout-flag-current-subtree (flag) | |
96b83743 | 4774 | "Conceal currently-visible topic's subtree if FLAG non-nil, else reveal it." |
19b84ba3 | 4775 | |
c567ac01 | 4776 | (save-excursion |
fd5359c6 | 4777 | (allout-back-to-current-heading) |
0458fa80 EZ |
4778 | (let ((inhibit-field-text-motion t)) |
4779 | (end-of-line)) | |
96b83743 EZ |
4780 | (allout-flag-region (point) |
4781 | ;; Exposing must not leave trailing blanks hidden, | |
4782 | ;; but can leave them exposed when hiding, so we | |
4783 | ;; can use flag's inverse as the | |
4784 | ;; include-trailing-blank cue: | |
4785 | (allout-end-of-current-subtree (not flag)) | |
4786 | flag))) | |
c567ac01 | 4787 | |
c567ac01 | 4788 | ;;;_ - Topic-specific |
01fc9422 EZ |
4789 | ;;;_ > allout-show-entry () |
4790 | (defun allout-show-entry () | |
4791 | "Like `allout-show-current-entry', but reveals entries in hidden topics. | |
c567ac01 RS |
4792 | |
4793 | This is a way to give restricted peek at a concealed locality without the | |
4794 | expense of exposing its context, but can leave the outline with aberrant | |
96b83743 EZ |
4795 | exposure. `allout-show-offshoot' should be used after the peek to rectify |
4796 | the exposure." | |
c567ac01 RS |
4797 | |
4798 | (interactive) | |
4799 | (save-excursion | |
96b83743 | 4800 | (let (beg end) |
d8350998 | 4801 | (allout-goto-prefix-doublechecked) |
96b83743 EZ |
4802 | (setq beg (if (allout-hidden-p) (1- (point)) (point))) |
4803 | (setq end (allout-pre-next-prefix)) | |
4804 | (allout-flag-region beg end nil) | |
9179616f | 4805 | (list beg end)))) |
fd5359c6 MR |
4806 | ;;;_ > allout-show-children (&optional level strict) |
4807 | (defun allout-show-children (&optional level strict) | |
c567ac01 RS |
4808 | |
4809 | "If point is visible, show all direct subheadings of this heading. | |
19b84ba3 | 4810 | |
2265e017 | 4811 | Otherwise, do `allout-show-to-offshoot', and then show subheadings. |
c567ac01 RS |
4812 | |
4813 | Optional LEVEL specifies how many levels below the current level | |
4814 | should be shown, or all levels if t. Default is 1. | |
4815 | ||
4816 | Optional STRICT means don't resort to -show-to-offshoot, no matter | |
4817 | what. This is basically so -show-to-offshoot, which is called by | |
4818 | this function, can employ the pure offspring-revealing capabilities of | |
19b84ba3 RS |
4819 | it. |
4820 | ||
4821 | Returns point at end of subtree that was opened, if any. (May get a | |
4822 | point of non-opened subtree?)" | |
c567ac01 RS |
4823 | |
4824 | (interactive "p") | |
96b83743 | 4825 | (let ((start-point (point))) |
19b84ba3 | 4826 | (if (and (not strict) |
96b83743 | 4827 | (allout-hidden-p)) |
c567ac01 | 4828 | |
96b83743 EZ |
4829 | (progn (allout-show-to-offshoot) ; Point's concealed, open to |
4830 | ; expose it. | |
4831 | ;; Then recurse, but with "strict" set so we don't | |
4832 | ;; infinite regress: | |
4833 | (allout-show-children level t)) | |
c567ac01 | 4834 | |
19b84ba3 | 4835 | (save-excursion |
96b83743 EZ |
4836 | (allout-beginning-of-current-line) |
4837 | (save-restriction | |
d8350998 | 4838 | (let* (depth |
65e7eb3d EZ |
4839 | ;; translate the level spec for this routine to the ones |
4840 | ;; used by -chart-subtree and -chart-to-reveal: | |
4841 | (chart-level (cond ((not level) 1) | |
4842 | ((eq level t) nil) | |
4843 | (t level))) | |
4844 | (chart (allout-chart-subtree chart-level)) | |
4845 | (to-reveal (or (allout-chart-to-reveal chart chart-level) | |
d8350998 CY |
4846 | ;; interactive, show discontinuous children: |
4847 | (and chart | |
32226619 | 4848 | (called-interactively-p 'interactive) |
d8350998 CY |
4849 | (save-excursion |
4850 | (allout-back-to-current-heading) | |
4851 | (setq depth (allout-current-depth)) | |
4852 | (and (allout-next-heading) | |
4853 | (> allout-recent-depth | |
4854 | (1+ depth)))) | |
4855 | (message | |
4856 | "Discontinuous offspring; use `%s %s'%s." | |
4857 | (substitute-command-keys | |
4858 | "\\[universal-argument]") | |
4859 | (substitute-command-keys | |
4860 | "\\[allout-shift-out]") | |
4861 | " to elevate them.") | |
4862 | (allout-chart-to-reveal | |
4863 | chart (- allout-recent-depth depth)))))) | |
96b83743 EZ |
4864 | (goto-char start-point) |
4865 | (when (and strict (allout-hidden-p)) | |
4866 | ;; Concealed root would already have been taken care of, | |
4867 | ;; unless strict was set. | |
4868 | (allout-flag-region (point) (allout-snug-back) nil) | |
4869 | (when allout-show-bodies | |
4870 | (goto-char (car to-reveal)) | |
4871 | (allout-show-current-entry))) | |
4872 | (while to-reveal | |
4873 | (goto-char (car to-reveal)) | |
4874 | (allout-flag-region (save-excursion (allout-snug-back) (point)) | |
4875 | (progn (search-forward "\n" nil t) | |
4876 | (1- (point))) | |
4877 | nil) | |
4878 | (when allout-show-bodies | |
4879 | (goto-char (car to-reveal)) | |
4880 | (allout-show-current-entry)) | |
4881 | (setq to-reveal (cdr to-reveal))))))) | |
4882 | ;; Compensate for `save-excursion's maintenance of point | |
4883 | ;; within invisible text: | |
4884 | (goto-char start-point))) | |
fd5359c6 MR |
4885 | ;;;_ > allout-show-to-offshoot () |
4886 | (defun allout-show-to-offshoot () | |
2265e017 | 4887 | "Like `allout-show-entry', but reveals all concealed ancestors, as well. |
c567ac01 | 4888 | |
96b83743 | 4889 | Useful for coherently exposing to a random point in a hidden region." |
c567ac01 RS |
4890 | (interactive) |
4891 | (save-excursion | |
0458fa80 EZ |
4892 | (let ((inhibit-field-text-motion t) |
4893 | (orig-pt (point)) | |
d8350998 | 4894 | (orig-pref (allout-goto-prefix-doublechecked)) |
c567ac01 | 4895 | (last-at (point)) |
65e7eb3d EZ |
4896 | (bag-it 0)) |
4897 | (while (or (> bag-it 1) (allout-hidden-p)) | |
96b83743 | 4898 | (while (allout-hidden-p) |
d8350998 | 4899 | (move-beginning-of-line 1) |
96b83743 | 4900 | (if (allout-hidden-p) (forward-char -1))) |
c567ac01 | 4901 | (if (= last-at (setq last-at (point))) |
65e7eb3d EZ |
4902 | ;; Oops, we're not making any progress! Show the current topic |
4903 | ;; completely, and try one more time here, if we haven't already. | |
c567ac01 | 4904 | (progn (beginning-of-line) |
fd5359c6 | 4905 | (allout-show-current-subtree) |
c567ac01 | 4906 | (goto-char orig-pt) |
65e7eb3d EZ |
4907 | (setq bag-it (1+ bag-it)) |
4908 | (if (> bag-it 1) | |
4909 | (error "allout-show-to-offshoot: %s" | |
4910 | "Stumped by aberrant nesting."))) | |
4911 | (if (> bag-it 0) (setq bag-it 0)) | |
d8350998 CY |
4912 | (allout-show-children) |
4913 | (goto-char orig-pref))) | |
19b84ba3 | 4914 | (goto-char orig-pt))) |
fd5359c6 MR |
4915 | (if (allout-hidden-p) |
4916 | (allout-show-entry))) | |
4917 | ;;;_ > allout-hide-current-entry () | |
4918 | (defun allout-hide-current-entry () | |
c567ac01 RS |
4919 | "Hide the body directly following this heading." |
4920 | (interactive) | |
fd5359c6 | 4921 | (allout-back-to-current-heading) |
c567ac01 | 4922 | (save-excursion |
0458fa80 EZ |
4923 | (let ((inhibit-field-text-motion t)) |
4924 | (end-of-line)) | |
96b83743 | 4925 | (allout-flag-region (point) |
d82979ea | 4926 | (progn (allout-end-of-entry) (point)) |
96b83743 | 4927 | t))) |
fd5359c6 MR |
4928 | ;;;_ > allout-show-current-entry (&optional arg) |
4929 | (defun allout-show-current-entry (&optional arg) | |
96b83743 | 4930 | "Show body following current heading, or hide entry with universal argument." |
c567ac01 RS |
4931 | |
4932 | (interactive "P") | |
4933 | (if arg | |
fd5359c6 | 4934 | (allout-hide-current-entry) |
96b83743 | 4935 | (save-excursion (allout-show-to-offshoot)) |
c567ac01 | 4936 | (save-excursion |
fd5359c6 | 4937 | (allout-flag-region (point) |
96b83743 EZ |
4938 | (progn (allout-end-of-entry t) (point)) |
4939 | nil) | |
d82979ea | 4940 | ))) |
fd5359c6 MR |
4941 | ;;;_ > allout-show-current-subtree (&optional arg) |
4942 | (defun allout-show-current-subtree (&optional arg) | |
75bcb912 JB |
4943 | "Show everything within the current topic. |
4944 | With a repeat-count, expose this topic and its siblings." | |
19b84ba3 RS |
4945 | (interactive "P") |
4946 | (save-excursion | |
fd5359c6 | 4947 | (if (<= (allout-current-depth) 0) |
60c7bbd2 | 4948 | ;; Outside any topics -- try to get to the first: |
fd5359c6 | 4949 | (if (not (allout-next-heading)) |
6a05d05f | 4950 | (error "No topics") |
60c7bbd2 RS |
4951 | ;; got to first, outermost topic -- set to expose it and siblings: |
4952 | (message "Above outermost topic -- exposing all.") | |
96b83743 EZ |
4953 | (allout-flag-region (point-min)(point-max) nil)) |
4954 | (allout-beginning-of-current-line) | |
19b84ba3 | 4955 | (if (not arg) |
96b83743 | 4956 | (allout-flag-current-subtree nil) |
fd5359c6 MR |
4957 | (allout-beginning-of-level) |
4958 | (allout-expose-topic '(* :)))))) | |
96b83743 EZ |
4959 | ;;;_ > allout-current-topic-collapsed-p (&optional include-single-liners) |
4960 | (defun allout-current-topic-collapsed-p (&optional include-single-liners) | |
4961 | "True if the currently visible containing topic is already collapsed. | |
4962 | ||
64686e6d RS |
4963 | Single line topics intrinsically can be considered as being both |
4964 | collapsed and uncollapsed. If optional INCLUDE-SINGLE-LINERS is | |
4965 | true, then single-line topics are considered to be collapsed. By | |
4966 | default, they are treated as being uncollapsed." | |
2a1408fd DN |
4967 | (save-match-data |
4968 | (save-excursion | |
4969 | (and | |
4970 | ;; Is the topic all on one line (allowing for trailing blank line)? | |
4971 | (>= (progn (allout-back-to-current-heading) | |
4972 | (move-end-of-line 1) | |
4973 | (point)) | |
4974 | (allout-end-of-current-subtree (not (looking-at "\n\n")))) | |
4975 | ||
4976 | (or include-single-liners | |
4977 | (progn (backward-char 1) (allout-hidden-p))))))) | |
fd5359c6 MR |
4978 | ;;;_ > allout-hide-current-subtree (&optional just-close) |
4979 | (defun allout-hide-current-subtree (&optional just-close) | |
19b84ba3 | 4980 | "Close the current topic, or containing topic if this one is already closed. |
c567ac01 | 4981 | |
19b84ba3 | 4982 | If this topic is closed and it's a top level topic, close this topic |
353e2ef2 | 4983 | and its siblings. |
19b84ba3 | 4984 | |
96b83743 | 4985 | If optional arg JUST-CLOSE is non-nil, do not close the parent or |
19b84ba3 | 4986 | siblings, even if the target topic is already closed." |
c567ac01 RS |
4987 | |
4988 | (interactive) | |
96b83743 | 4989 | (let* ((from (point)) |
60c7bbd2 | 4990 | (sibs-msg "Top-level topic already closed -- closing siblings...") |
96b83743 EZ |
4991 | (current-exposed (not (allout-current-topic-collapsed-p t)))) |
4992 | (cond (current-exposed (allout-flag-current-subtree t)) | |
4993 | (just-close nil) | |
d8350998 | 4994 | ((allout-ascend) (allout-hide-current-subtree)) |
96b83743 EZ |
4995 | (t (goto-char 0) |
4996 | (message sibs-msg) | |
d8350998 | 4997 | (allout-goto-prefix-doublechecked) |
96b83743 EZ |
4998 | (allout-expose-topic '(0 :)) |
4999 | (message (concat sibs-msg " Done.")))) | |
5000 | (goto-char from))) | |
2a1408fd DN |
5001 | ;;;_ > allout-toggle-current-subtree-exposure |
5002 | (defun allout-toggle-current-subtree-exposure () | |
5003 | "Show or hide the current subtree depending on its current state." | |
5004 | ;; thanks to tassilo for suggesting this. | |
5005 | (interactive) | |
5006 | (save-excursion | |
5007 | (allout-back-to-heading) | |
5008 | (if (allout-hidden-p (point-at-eol)) | |
5009 | (allout-show-current-subtree) | |
5010 | (allout-hide-current-subtree)))) | |
fd5359c6 MR |
5011 | ;;;_ > allout-show-current-branches () |
5012 | (defun allout-show-current-branches () | |
c567ac01 RS |
5013 | "Show all subheadings of this heading, but not their bodies." |
5014 | (interactive) | |
0458fa80 EZ |
5015 | (let ((inhibit-field-text-motion t)) |
5016 | (beginning-of-line)) | |
fd5359c6 MR |
5017 | (allout-show-children t)) |
5018 | ;;;_ > allout-hide-current-leaves () | |
5019 | (defun allout-hide-current-leaves () | |
353e2ef2 | 5020 | "Hide the bodies of the current topic and all its offspring." |
c567ac01 | 5021 | (interactive) |
fd5359c6 MR |
5022 | (allout-back-to-current-heading) |
5023 | (allout-hide-region-body (point) (progn (allout-end-of-current-subtree) | |
c567ac01 RS |
5024 | (point)))) |
5025 | ||
5026 | ;;;_ - Region and beyond | |
fd5359c6 MR |
5027 | ;;;_ > allout-show-all () |
5028 | (defun allout-show-all () | |
c567ac01 RS |
5029 | "Show all of the text in the buffer." |
5030 | (interactive) | |
19b84ba3 | 5031 | (message "Exposing entire buffer...") |
96b83743 | 5032 | (allout-flag-region (point-min) (point-max) nil) |
19b84ba3 | 5033 | (message "Exposing entire buffer... Done.")) |
fd5359c6 MR |
5034 | ;;;_ > allout-hide-bodies () |
5035 | (defun allout-hide-bodies () | |
c567ac01 RS |
5036 | "Hide all of buffer except headings." |
5037 | (interactive) | |
fd5359c6 MR |
5038 | (allout-hide-region-body (point-min) (point-max))) |
5039 | ;;;_ > allout-hide-region-body (start end) | |
5040 | (defun allout-hide-region-body (start end) | |
c567ac01 | 5041 | "Hide all body lines in the region, but not headings." |
2a1408fd DN |
5042 | (save-match-data |
5043 | (save-excursion | |
5044 | (save-restriction | |
5045 | (narrow-to-region start end) | |
5046 | (goto-char (point-min)) | |
5047 | (let ((inhibit-field-text-motion t)) | |
5048 | (while (not (eobp)) | |
5049 | (end-of-line) | |
5050 | (allout-flag-region (point) (allout-end-of-entry) t) | |
5051 | (if (not (eobp)) | |
5052 | (forward-char | |
5053 | (if (looking-at "\n\n") | |
5054 | 2 1))))))))) | |
c567ac01 | 5055 | |
fd5359c6 MR |
5056 | ;;;_ > allout-expose-topic (spec) |
5057 | (defun allout-expose-topic (spec) | |
19b84ba3 | 5058 | "Apply exposure specs to successive outline topic items. |
c567ac01 | 5059 | |
fd5359c6 MR |
5060 | Use the more convenient frontend, `allout-new-exposure', if you don't |
5061 | need evaluation of the arguments, or even better, the `allout-layout' | |
19b84ba3 RS |
5062 | variable-keyed mode-activation/auto-exposure feature of allout outline |
5063 | mode. See the respective documentation strings for more details. | |
c567ac01 RS |
5064 | |
5065 | Cursor is left at start position. | |
5066 | ||
19b84ba3 RS |
5067 | SPEC is either a number or a list. |
5068 | ||
5069 | Successive specs on a list are applied to successive sibling topics. | |
c567ac01 | 5070 | |
c07583cd | 5071 | A simple spec (either a number, one of a few symbols, or the null |
19b84ba3 RS |
5072 | list) dictates the exposure for the corresponding topic. |
5073 | ||
5074 | Non-null lists recursively designate exposure specs for respective | |
5075 | subtopics of the current topic. | |
c567ac01 | 5076 | |
353e2ef2 | 5077 | The `:' repeat spec is used to specify exposure for any number of |
19b84ba3 | 5078 | successive siblings, up to the trailing ones for which there are |
353e2ef2 | 5079 | explicit specs following the `:'. |
c567ac01 RS |
5080 | |
5081 | Simple (numeric and null-list) specs are interpreted as follows: | |
5082 | ||
19b84ba3 RS |
5083 | Numbers indicate the relative depth to open the corresponding topic. |
5084 | - negative numbers force the topic to be closed before opening to the | |
5085 | absolute value of the number, so all siblings are open only to | |
5086 | that level. | |
5087 | - positive numbers open to the relative depth indicated by the | |
5088 | number, but do not force already opened subtopics to be closed. | |
60c7bbd2 | 5089 | - 0 means to close topic -- hide all offspring. |
353e2ef2 | 5090 | : - `repeat' |
c567ac01 | 5091 | apply prior element to all siblings at current level, *up to* |
353e2ef2 | 5092 | those siblings that would be covered by specs following the `:' |
c567ac01 | 5093 | on the list. Ie, apply to all topics at level but the last |
c07583cd | 5094 | ones. (Only first of multiple colons at same level is |
60c7bbd2 | 5095 | respected -- subsequent ones are discarded.) |
19b84ba3 RS |
5096 | * - completely opens the topic, including bodies. |
5097 | + - shows all the sub headers, but not the bodies | |
5098 | - - exposes the body of the corresponding topic. | |
c567ac01 RS |
5099 | |
5100 | Examples: | |
fd5359c6 | 5101 | \(allout-expose-topic '(-1 : 0)) |
c567ac01 RS |
5102 | Close this and all following topics at current level, exposing |
5103 | only their immediate children, but close down the last topic | |
5104 | at this current level completely. | |
fd5359c6 | 5105 | \(allout-expose-topic '(-1 () : 1 0)) |
c567ac01 RS |
5106 | Close current topic so only the immediate subtopics are shown; |
5107 | show the children in the second to last topic, and completely | |
5108 | close the last one. | |
fd5359c6 | 5109 | \(allout-expose-topic '(-2 : -1 *)) |
c567ac01 RS |
5110 | Expose children and grandchildren of all topics at current |
5111 | level except the last two; expose children of the second to | |
5112 | last and completely open the last one." | |
5113 | ||
5114 | (interactive "xExposure spec: ") | |
19b84ba3 RS |
5115 | (if (not (listp spec)) |
5116 | nil | |
fd5359c6 | 5117 | (let ((depth (allout-depth)) |
19b84ba3 RS |
5118 | (max-pos 0) |
5119 | prev-elem curr-elem | |
96b83743 | 5120 | stay) |
19b84ba3 RS |
5121 | (while spec |
5122 | (setq prev-elem curr-elem | |
5123 | curr-elem (car spec) | |
5124 | spec (cdr spec)) | |
5125 | (cond ; Do current element: | |
5126 | ((null curr-elem) nil) | |
5127 | ((symbolp curr-elem) | |
fd5359c6 MR |
5128 | (cond ((eq curr-elem '*) (allout-show-current-subtree) |
5129 | (if (> allout-recent-end-of-subtree max-pos) | |
5130 | (setq max-pos allout-recent-end-of-subtree))) | |
65e7eb3d EZ |
5131 | ((eq curr-elem '+) |
5132 | (if (not (allout-hidden-p)) | |
5133 | (save-excursion (allout-hide-current-subtree t))) | |
5134 | (allout-show-current-branches) | |
fd5359c6 MR |
5135 | (if (> allout-recent-end-of-subtree max-pos) |
5136 | (setq max-pos allout-recent-end-of-subtree))) | |
5137 | ((eq curr-elem '-) (allout-show-current-entry)) | |
19b84ba3 RS |
5138 | ((eq curr-elem ':) |
5139 | (setq stay t) | |
353e2ef2 | 5140 | ;; Expand the `repeat' spec to an explicit version, |
19b84ba3 RS |
5141 | ;; w.r.t. remaining siblings: |
5142 | (let ((residue ; = # of sibs not covered by remaining spec | |
60c7bbd2 | 5143 | ;; Dang, could be nice to make use of the chart, sigh: |
fd5359c6 | 5144 | (- (length (allout-chart-siblings)) |
19b84ba3 RS |
5145 | (length spec)))) |
5146 | (if (< 0 residue) | |
60c7bbd2 | 5147 | ;; Some residue -- cover it with prev-elem: |
19b84ba3 RS |
5148 | (setq spec (append (make-list residue prev-elem) |
5149 | spec))))))) | |
5150 | ((numberp curr-elem) | |
96b83743 | 5151 | (if (and (>= 0 curr-elem) (not (allout-hidden-p))) |
fd5359c6 | 5152 | (save-excursion (allout-hide-current-subtree t) |
19b84ba3 RS |
5153 | (if (> 0 curr-elem) |
5154 | nil | |
fd5359c6 | 5155 | (if (> allout-recent-end-of-subtree max-pos) |
19b84ba3 | 5156 | (setq max-pos |
fd5359c6 | 5157 | allout-recent-end-of-subtree))))) |
19b84ba3 | 5158 | (if (> (abs curr-elem) 0) |
fd5359c6 MR |
5159 | (progn (allout-show-children (abs curr-elem)) |
5160 | (if (> allout-recent-end-of-subtree max-pos) | |
5161 | (setq max-pos allout-recent-end-of-subtree))))) | |
19b84ba3 | 5162 | ((listp curr-elem) |
fd5359c6 MR |
5163 | (if (allout-descend-to-depth (1+ depth)) |
5164 | (let ((got (allout-expose-topic curr-elem))) | |
19b84ba3 RS |
5165 | (if (and got (> got max-pos)) (setq max-pos got)))))) |
5166 | (cond (stay (setq stay nil)) | |
5167 | ((listp (car spec)) nil) | |
5168 | ((> max-pos (point)) | |
5169 | ;; Capitalize on max-pos state to get us nearer next sibling: | |
5170 | (progn (goto-char (min (point-max) max-pos)) | |
fd5359c6 MR |
5171 | (allout-next-heading))) |
5172 | ((allout-next-sibling depth)))) | |
19b84ba3 | 5173 | max-pos))) |
fd5359c6 MR |
5174 | ;;;_ > allout-old-expose-topic (spec &rest followers) |
5175 | (defun allout-old-expose-topic (spec &rest followers) | |
d82979ea | 5176 | |
c07583cd | 5177 | "Deprecated. Use `allout-expose-topic' (with different schema |
d82979ea EZ |
5178 | format) instead. |
5179 | ||
5180 | Dictate wholesale exposure scheme for current topic, according to SPEC. | |
c567ac01 RS |
5181 | |
5182 | SPEC is either a number or a list. Optional successive args | |
5183 | dictate exposure for subsequent siblings of current topic. | |
5184 | ||
5185 | A simple spec (either a number, a special symbol, or the null list) | |
5186 | dictates the overall exposure for a topic. Non null lists are | |
5187 | composite specs whose first element dictates the overall exposure for | |
5188 | a topic, with the subsequent elements in the list interpreted as specs | |
5189 | that dictate the exposure for the successive offspring of the topic. | |
5190 | ||
5191 | Simple (numeric and null-list) specs are interpreted as follows: | |
5192 | ||
5193 | - Numbers indicate the relative depth to open the corresponding topic: | |
5194 | - negative numbers force the topic to be close before opening to the | |
5195 | absolute value of the number. | |
5196 | - positive numbers just open to the relative depth indicated by the number. | |
5197 | - 0 just closes | |
353e2ef2 KH |
5198 | - `*' completely opens the topic, including bodies. |
5199 | - `+' shows all the sub headers, but not the bodies | |
5200 | - `-' exposes the body and immediate offspring of the corresponding topic. | |
c567ac01 RS |
5201 | |
5202 | If the spec is a list, the first element must be a number, which | |
5203 | dictates the exposure depth of the topic as a whole. Subsequent | |
5204 | elements of the list are nested SPECs, dictating the specific exposure | |
5205 | for the corresponding offspring of the topic. | |
5206 | ||
e126900f | 5207 | Optional FOLLOWERS arguments dictate exposure for succeeding siblings." |
c567ac01 RS |
5208 | |
5209 | (interactive "xExposure spec: ") | |
0458fa80 EZ |
5210 | (let ((inhibit-field-text-motion t) |
5211 | (depth (allout-current-depth)) | |
c567ac01 RS |
5212 | max-pos) |
5213 | (cond ((null spec) nil) | |
5214 | ((symbolp spec) | |
fd5359c6 MR |
5215 | (if (eq spec '*) (allout-show-current-subtree)) |
5216 | (if (eq spec '+) (allout-show-current-branches)) | |
5217 | (if (eq spec '-) (allout-show-current-entry))) | |
c567ac01 RS |
5218 | ((numberp spec) |
5219 | (if (>= 0 spec) | |
fd5359c6 | 5220 | (save-excursion (allout-hide-current-subtree t) |
c567ac01 RS |
5221 | (end-of-line) |
5222 | (if (or (not max-pos) | |
5223 | (> (point) max-pos)) | |
5224 | (setq max-pos (point))) | |
5225 | (if (> 0 spec) | |
5226 | (setq spec (* -1 spec))))) | |
5227 | (if (> spec 0) | |
fd5359c6 | 5228 | (allout-show-children spec))) |
c567ac01 | 5229 | ((listp spec) |
fd5359c6 | 5230 | ;(let ((got (allout-old-expose-topic (car spec)))) |
c567ac01 RS |
5231 | ; (if (and got (or (not max-pos) (> got max-pos))) |
5232 | ; (setq max-pos got))) | |
fd5359c6 | 5233 | (let ((new-depth (+ (allout-current-depth) 1)) |
c567ac01 | 5234 | got) |
fd5359c6 | 5235 | (setq max-pos (allout-old-expose-topic (car spec))) |
c567ac01 RS |
5236 | (setq spec (cdr spec)) |
5237 | (if (and spec | |
fd5359c6 MR |
5238 | (allout-descend-to-depth new-depth) |
5239 | (not (allout-hidden-p))) | |
5240 | (progn (setq got (apply 'allout-old-expose-topic spec)) | |
c567ac01 RS |
5241 | (if (and got (or (not max-pos) (> got max-pos))) |
5242 | (setq max-pos got))))))) | |
5243 | (while (and followers | |
5244 | (progn (if (and max-pos (< (point) max-pos)) | |
5245 | (progn (goto-char max-pos) | |
5246 | (setq max-pos nil))) | |
5247 | (end-of-line) | |
fd5359c6 MR |
5248 | (allout-next-sibling depth))) |
5249 | (allout-old-expose-topic (car followers)) | |
c567ac01 RS |
5250 | (setq followers (cdr followers))) |
5251 | max-pos)) | |
fd5359c6 MR |
5252 | ;;;_ > allout-new-exposure '() |
5253 | (defmacro allout-new-exposure (&rest spec) | |
5254 | "Literal frontend for `allout-expose-topic', doesn't evaluate arguments. | |
2265e017 MR |
5255 | Some arguments that would need to be quoted in `allout-expose-topic' |
5256 | need not be quoted in `allout-new-exposure'. | |
c567ac01 RS |
5257 | |
5258 | Cursor is left at start position. | |
5259 | ||
d82979ea EZ |
5260 | Use this instead of obsolete `allout-exposure'. |
5261 | ||
c567ac01 | 5262 | Examples: |
bff05d9e | 5263 | \(allout-new-exposure (-1 () () () 1) 0) |
c567ac01 RS |
5264 | Close current topic at current level so only the immediate |
5265 | subtopics are shown, except also show the children of the | |
5266 | third subtopic; and close the next topic at the current level. | |
bff05d9e | 5267 | \(allout-new-exposure : -1 0) |
c567ac01 RS |
5268 | Close all topics at current level to expose only their |
5269 | immediate children, except for the last topic at the current | |
353e2ef2 | 5270 | level, in which even its immediate children are hidden. |
bff05d9e | 5271 | \(allout-new-exposure -2 : -1 *) |
c567ac01 RS |
5272 | Expose children and grandchildren of first topic at current |
5273 | level, and expose children of subsequent topics at current | |
5274 | level *except* for the last, which should be opened completely." | |
5275 | (list 'save-excursion | |
d8350998 | 5276 | '(if (not (or (allout-goto-prefix-doublechecked) |
fd5359c6 MR |
5277 | (allout-next-heading))) |
5278 | (error "allout-new-exposure: Can't find any outline topics")) | |
5279 | (list 'allout-expose-topic (list 'quote spec)))) | |
c567ac01 | 5280 | |
60c7bbd2 | 5281 | ;;;_ #7 Systematic outline presentation -- copying, printing, flattening |
19b84ba3 | 5282 | |
9179616f DL |
5283 | ;;;_ - Mapping and processing of topics |
5284 | ;;;_ ( See also Subtree Charting, in Navigation code.) | |
fd5359c6 MR |
5285 | ;;;_ > allout-stringify-flat-index (flat-index) |
5286 | (defun allout-stringify-flat-index (flat-index &optional context) | |
9179616f DL |
5287 | "Convert list representing section/subsection/... to document string. |
5288 | ||
5289 | Optional arg CONTEXT indicates interior levels to include." | |
5290 | (let ((delim ".") | |
71296446 | 5291 | result |
9179616f DL |
5292 | numstr |
5293 | (context-depth (or (and context 2) 1))) | |
5294 | ;; Take care of the explicit context: | |
5295 | (while (> context-depth 0) | |
5296 | (setq numstr (int-to-string (car flat-index)) | |
5297 | flat-index (cdr flat-index) | |
5298 | result (if flat-index | |
5299 | (cons delim (cons numstr result)) | |
5300 | (cons numstr result)) | |
5301 | context-depth (if flat-index (1- context-depth) 0))) | |
5302 | (setq delim " ") | |
5303 | ;; Take care of the indentation: | |
5304 | (if flat-index | |
5305 | (progn | |
5306 | (while flat-index | |
5307 | (setq result | |
5308 | (cons delim | |
5309 | (cons (make-string | |
5310 | (1+ (truncate (if (zerop (car flat-index)) | |
5311 | 1 | |
5312 | (log10 (car flat-index))))) | |
5313 | ? ) | |
5314 | result))) | |
5315 | (setq flat-index (cdr flat-index))) | |
5316 | ;; Dispose of single extra delim: | |
5317 | (setq result (cdr result)))) | |
5318 | (apply 'concat result))) | |
fd5359c6 MR |
5319 | ;;;_ > allout-stringify-flat-index-plain (flat-index) |
5320 | (defun allout-stringify-flat-index-plain (flat-index) | |
9179616f DL |
5321 | "Convert list representing section/subsection/... to document string." |
5322 | (let ((delim ".") | |
5323 | result) | |
5324 | (while flat-index | |
5325 | (setq result (cons (int-to-string (car flat-index)) | |
5326 | (if result | |
5327 | (cons delim result)))) | |
5328 | (setq flat-index (cdr flat-index))) | |
5329 | (apply 'concat result))) | |
fd5359c6 MR |
5330 | ;;;_ > allout-stringify-flat-index-indented (flat-index) |
5331 | (defun allout-stringify-flat-index-indented (flat-index) | |
9179616f DL |
5332 | "Convert list representing section/subsection/... to document string." |
5333 | (let ((delim ".") | |
71296446 | 5334 | result |
9179616f DL |
5335 | numstr) |
5336 | ;; Take care of the explicit context: | |
5337 | (setq numstr (int-to-string (car flat-index)) | |
5338 | flat-index (cdr flat-index) | |
5339 | result (if flat-index | |
5340 | (cons delim (cons numstr result)) | |
5341 | (cons numstr result))) | |
5342 | (setq delim " ") | |
5343 | ;; Take care of the indentation: | |
5344 | (if flat-index | |
5345 | (progn | |
5346 | (while flat-index | |
5347 | (setq result | |
5348 | (cons delim | |
5349 | (cons (make-string | |
5350 | (1+ (truncate (if (zerop (car flat-index)) | |
5351 | 1 | |
5352 | (log10 (car flat-index))))) | |
5353 | ? ) | |
5354 | result))) | |
5355 | (setq flat-index (cdr flat-index))) | |
5356 | ;; Dispose of single extra delim: | |
5357 | (setq result (cdr result)))) | |
5358 | (apply 'concat result))) | |
fd5359c6 MR |
5359 | ;;;_ > allout-listify-exposed (&optional start end format) |
5360 | (defun allout-listify-exposed (&optional start end format) | |
19b84ba3 | 5361 | |
9179616f | 5362 | "Produce a list representing exposed topics in current region. |
c567ac01 | 5363 | |
fd5359c6 | 5364 | This list can then be used by `allout-process-exposed' to manipulate |
9179616f | 5365 | the subject region. |
353e2ef2 | 5366 | |
9179616f | 5367 | Optional START and END indicate bounds of region. |
19b84ba3 | 5368 | |
75bcb912 | 5369 | Optional arg, FORMAT, designates an alternate presentation form for |
9179616f | 5370 | the prefix: |
c567ac01 | 5371 | |
60c7bbd2 | 5372 | list -- Present prefix as numeric section.subsection..., starting with |
9179616f | 5373 | section indicated by the list, innermost nesting first. |
60c7bbd2 | 5374 | `indent' (symbol) -- Convert header prefixes to all white space, |
9179616f | 5375 | except for distinctive bullets. |
c567ac01 | 5376 | |
9179616f DL |
5377 | The elements of the list produced are lists that represents a topic |
5378 | header and body. The elements of that list are: | |
c567ac01 | 5379 | |
9179616f DL |
5380 | - a number representing the depth of the topic, |
5381 | - a string representing the header-prefix, including trailing whitespace and | |
5382 | bullet. | |
5383 | - a string representing the bullet character, | |
5384 | - and a series of strings, each containing one line of the exposed | |
5385 | portion of the topic entry." | |
c567ac01 | 5386 | |
9179616f DL |
5387 | (interactive "r") |
5388 | (save-excursion | |
5389 | (let* | |
0458fa80 EZ |
5390 | ((inhibit-field-text-motion t) |
5391 | ;; state vars: | |
5392 | strings prefix result depth new-depth out gone-out bullet beg | |
9179616f | 5393 | next done) |
c567ac01 | 5394 | |
9179616f DL |
5395 | (goto-char start) |
5396 | (beginning-of-line) | |
5397 | ;; Goto initial topic, and register preceeding stuff, if any: | |
d8350998 | 5398 | (if (> (allout-goto-prefix-doublechecked) start) |
60c7bbd2 | 5399 | ;; First topic follows beginning point -- register preliminary stuff: |
9179616f DL |
5400 | (setq result (list (list 0 "" nil |
5401 | (buffer-substring start (1- (point))))))) | |
5402 | (while (and (not done) | |
5403 | (not (eobp)) ; Loop until we've covered the region. | |
5404 | (not (> (point) end))) | |
d8350998 | 5405 | (setq depth allout-recent-depth ; Current topics depth, |
fd5359c6 MR |
5406 | bullet (allout-recent-bullet) ; ... bullet, |
5407 | prefix (allout-recent-prefix) | |
5408 | beg (progn (allout-end-of-prefix t) (point))) ; and beginning. | |
9179616f | 5409 | (setq done ; The boundary for the current topic: |
fd5359c6 | 5410 | (not (allout-next-visible-heading 1))) |
d8350998 | 5411 | (setq new-depth allout-recent-depth) |
9179616f DL |
5412 | (setq gone-out out |
5413 | out (< new-depth depth)) | |
5414 | (beginning-of-line) | |
5415 | (setq next (point)) | |
5416 | (goto-char beg) | |
5417 | (setq strings nil) | |
5418 | (while (> next (point)) ; Get all the exposed text in | |
5419 | (setq strings | |
5420 | (cons (buffer-substring | |
5421 | beg | |
5422 | ;To hidden text or end of line: | |
5423 | (progn | |
96b83743 EZ |
5424 | (end-of-line) |
5425 | (allout-back-to-visible-text))) | |
9179616f | 5426 | strings)) |
96b83743 | 5427 | (when (< (point) next) ; Resume from after hid text, if any. |
1b4d97b2 CY |
5428 | (line-move 1) |
5429 | (beginning-of-line)) | |
9179616f DL |
5430 | (setq beg (point))) |
5431 | ;; Accumulate list for this topic: | |
5432 | (setq strings (nreverse strings)) | |
5433 | (setq result | |
5434 | (cons | |
5435 | (if format | |
5436 | (let ((special (if (string-match | |
5437 | (regexp-quote bullet) | |
fd5359c6 | 5438 | allout-distinctive-bullets-string) |
9179616f DL |
5439 | bullet))) |
5440 | (cond ((listp format) | |
5441 | (list depth | |
fd5359c6 MR |
5442 | (if allout-abbreviate-flattened-numbering |
5443 | (allout-stringify-flat-index format | |
9179616f | 5444 | gone-out) |
fd5359c6 | 5445 | (allout-stringify-flat-index-plain |
9179616f DL |
5446 | format)) |
5447 | strings | |
5448 | special)) | |
5449 | ((eq format 'indent) | |
5450 | (if special | |
5451 | (list depth | |
5452 | (concat (make-string (1+ depth) ? ) | |
5453 | (substring prefix -1)) | |
5454 | strings) | |
5455 | (list depth | |
5456 | (make-string depth ? ) | |
5457 | strings))) | |
fd5359c6 | 5458 | (t (error "allout-listify-exposed: %s %s" |
9179616f DL |
5459 | "invalid format" format)))) |
5460 | (list depth prefix strings)) | |
5461 | result)) | |
5462 | ;; Reasses format, if any: | |
5463 | (if (and format (listp format)) | |
5464 | (cond ((= new-depth depth) | |
5465 | (setq format (cons (1+ (car format)) | |
5466 | (cdr format)))) | |
60c7bbd2 | 5467 | ((> new-depth depth) ; descending -- assume by 1: |
9179616f DL |
5468 | (setq format (cons 1 format))) |
5469 | (t | |
5470 | ; Pop the residue: | |
5471 | (while (< new-depth depth) | |
5472 | (setq format (cdr format)) | |
5473 | (setq depth (1- depth))) | |
5474 | ; And increment the current one: | |
5475 | (setq format | |
5476 | (cons (1+ (or (car format) | |
5477 | -1)) | |
5478 | (cdr format))))))) | |
5479 | ;; Put the list with first at front, to last at back: | |
5480 | (nreverse result)))) | |
c60d543d RS |
5481 | ;;;_ > allout-region-active-p () |
5482 | (defmacro allout-region-active-p () | |
42c3e51c KM |
5483 | (cond ((fboundp 'use-region-p) '(use-region-p)) |
5484 | ((fboundp 'region-active-p) '(region-active-p)) | |
5485 | (t 'mark-active))) | |
5486 | ;;_ > allout-process-exposed (&optional func from to frombuf | |
9179616f | 5487 | ;;; tobuf format) |
fd5359c6 | 5488 | (defun allout-process-exposed (&optional func from to frombuf tobuf |
96b83743 | 5489 | format start-num) |
9179616f | 5490 | "Map function on exposed parts of current topic; results to another buffer. |
353e2ef2 | 5491 | |
d82979ea EZ |
5492 | All args are options; default values itemized below. |
5493 | ||
5494 | Apply FUNCTION to exposed portions FROM position TO position in buffer | |
9179616f DL |
5495 | FROMBUF to buffer TOBUF. Sixth optional arg, FORMAT, designates an |
5496 | alternate presentation form: | |
c567ac01 | 5497 | |
60c7bbd2 | 5498 | `flat' -- Present prefix as numeric section.subsection..., starting with |
75bcb912 | 5499 | section indicated by the START-NUM, innermost nesting first. |
60c7bbd2 | 5500 | X`flat-indented' -- Prefix is like `flat' for first topic at each |
9179616f DL |
5501 | X level, but subsequent topics have only leaf topic |
5502 | X number, padded with blanks to line up with first. | |
60c7bbd2 | 5503 | `indent' (symbol) -- Convert header prefixes to all white space, |
9179616f | 5504 | except for distinctive bullets. |
c567ac01 | 5505 | |
9179616f | 5506 | Defaults: |
d82979ea | 5507 | FUNCTION: `allout-insert-listified' |
9179616f DL |
5508 | FROM: region start, if region active, else start of buffer |
5509 | TO: region end, if region active, else end of buffer | |
5510 | FROMBUF: current buffer | |
5511 | TOBUF: buffer name derived: \"*current-buffer-name exposed*\" | |
5512 | FORMAT: nil" | |
5513 | ||
5514 | ; Resolve arguments, | |
5515 | ; defaulting if necessary: | |
fd5359c6 | 5516 | (if (not func) (setq func 'allout-insert-listified)) |
9179616f | 5517 | (if (not (and from to)) |
c60d543d | 5518 | (if (allout-region-active-p) |
9179616f DL |
5519 | (setq from (region-beginning) to (region-end)) |
5520 | (setq from (point-min) to (point-max)))) | |
5521 | (if frombuf | |
5522 | (if (not (bufferp frombuf)) | |
60c7bbd2 | 5523 | ;; Specified but not a buffer -- get it: |
9179616f DL |
5524 | (let ((got (get-buffer frombuf))) |
5525 | (if (not got) | |
fd5359c6 | 5526 | (error (concat "allout-process-exposed: source buffer " |
9179616f DL |
5527 | frombuf |
5528 | " not found.")) | |
5529 | (setq frombuf got)))) | |
60c7bbd2 | 5530 | ;; not specified -- default it: |
9179616f DL |
5531 | (setq frombuf (current-buffer))) |
5532 | (if tobuf | |
5533 | (if (not (bufferp tobuf)) | |
5534 | (setq tobuf (get-buffer-create tobuf))) | |
60c7bbd2 | 5535 | ;; not specified -- default it: |
9179616f DL |
5536 | (setq tobuf (concat "*" (buffer-name frombuf) " exposed*"))) |
5537 | (if (listp format) | |
5538 | (nreverse format)) | |
5539 | ||
5540 | (let* ((listified | |
5541 | (progn (set-buffer frombuf) | |
fd5359c6 | 5542 | (allout-listify-exposed from to format)))) |
9179616f | 5543 | (set-buffer tobuf) |
5110e166 | 5544 | (mapc func listified) |
9179616f DL |
5545 | (pop-to-buffer tobuf))) |
5546 | ||
5547 | ;;;_ - Copy exposed | |
fd5359c6 MR |
5548 | ;;;_ > allout-insert-listified (listified) |
5549 | (defun allout-insert-listified (listified) | |
9179616f DL |
5550 | "Insert contents of listified outline portion in current buffer. |
5551 | ||
e126900f | 5552 | LISTIFIED is a list representing each topic header and body: |
9179616f DL |
5553 | |
5554 | \`(depth prefix text)' | |
5555 | ||
d82979ea | 5556 | or \`(depth prefix text bullet-plus)' |
9179616f DL |
5557 | |
5558 | If `bullet-plus' is specified, it is inserted just after the entire prefix." | |
5559 | (setq listified (cdr listified)) | |
5560 | (let ((prefix (prog1 | |
5561 | (car listified) | |
5562 | (setq listified (cdr listified)))) | |
5563 | (text (prog1 | |
5564 | (car listified) | |
5565 | (setq listified (cdr listified)))) | |
5566 | (bullet-plus (car listified))) | |
eac9cf5f PJ |
5567 | (insert prefix) |
5568 | (if bullet-plus (insert (concat " " bullet-plus))) | |
9179616f | 5569 | (while text |
eac9cf5f | 5570 | (insert (car text)) |
9179616f | 5571 | (if (setq text (cdr text)) |
0949617b | 5572 | (insert "\n"))) |
eac9cf5f | 5573 | (insert "\n"))) |
fd5359c6 MR |
5574 | ;;;_ > allout-copy-exposed-to-buffer (&optional arg tobuf format) |
5575 | (defun allout-copy-exposed-to-buffer (&optional arg tobuf format) | |
9179616f DL |
5576 | "Duplicate exposed portions of current outline to another buffer. |
5577 | ||
5578 | Other buffer has current buffers name with \" exposed\" appended to it. | |
5579 | ||
5580 | With repeat count, copy the exposed parts of only the current topic. | |
5581 | ||
5582 | Optional second arg TOBUF is target buffer name. | |
5583 | ||
5584 | Optional third arg FORMAT, if non-nil, symbolically designates an | |
5585 | alternate presentation format for the outline: | |
5586 | ||
5587 | `flat' - Convert topic header prefixes to numeric | |
5588 | section.subsection... identifiers. | |
5589 | `indent' - Convert header prefixes to all white space, except for | |
5590 | distinctive bullets. | |
5591 | `indent-flat' - The best of both - only the first of each level has | |
5592 | the full path, the rest have only the section number | |
5593 | of the leaf, preceded by the right amount of indentation." | |
c567ac01 RS |
5594 | |
5595 | (interactive "P") | |
5596 | (if (not tobuf) | |
5597 | (setq tobuf (get-buffer-create (concat "*" (buffer-name) " exposed*")))) | |
5598 | (let* ((start-pt (point)) | |
fd5359c6 MR |
5599 | (beg (if arg (allout-back-to-current-heading) (point-min))) |
5600 | (end (if arg (allout-end-of-current-subtree) (point-max))) | |
9179616f DL |
5601 | (buf (current-buffer)) |
5602 | (start-list ())) | |
5603 | (if (eq format 'flat) | |
5604 | (setq format (if arg (save-excursion | |
5605 | (goto-char beg) | |
fd5359c6 | 5606 | (allout-topic-flat-index)) |
9179616f | 5607 | '(1)))) |
c567ac01 | 5608 | (save-excursion (set-buffer tobuf)(erase-buffer)) |
fd5359c6 | 5609 | (allout-process-exposed 'allout-insert-listified |
c567ac01 RS |
5610 | beg |
5611 | end | |
5612 | (current-buffer) | |
9179616f DL |
5613 | tobuf |
5614 | format start-list) | |
c567ac01 RS |
5615 | (goto-char (point-min)) |
5616 | (pop-to-buffer buf) | |
5617 | (goto-char start-pt))) | |
fd5359c6 MR |
5618 | ;;;_ > allout-flatten-exposed-to-buffer (&optional arg tobuf) |
5619 | (defun allout-flatten-exposed-to-buffer (&optional arg tobuf) | |
9179616f DL |
5620 | "Present numeric outline of outline's exposed portions in another buffer. |
5621 | ||
60c7bbd2 | 5622 | The resulting outline is not compatible with outline mode -- use |
fd5359c6 | 5623 | `allout-copy-exposed-to-buffer' if you want that. |
9179616f | 5624 | |
fd5359c6 | 5625 | Use `allout-indented-exposed-to-buffer' for indented presentation. |
9179616f DL |
5626 | |
5627 | With repeat count, copy the exposed portions of only current topic. | |
5628 | ||
e126900f | 5629 | Other buffer has current buffer's name with \" exposed\" appended to |
9179616f DL |
5630 | it, unless optional second arg TOBUF is specified, in which case it is |
5631 | used verbatim." | |
5632 | (interactive "P") | |
fd5359c6 MR |
5633 | (allout-copy-exposed-to-buffer arg tobuf 'flat)) |
5634 | ;;;_ > allout-indented-exposed-to-buffer (&optional arg tobuf) | |
5635 | (defun allout-indented-exposed-to-buffer (&optional arg tobuf) | |
9179616f DL |
5636 | "Present indented outline of outline's exposed portions in another buffer. |
5637 | ||
60c7bbd2 | 5638 | The resulting outline is not compatible with outline mode -- use |
fd5359c6 | 5639 | `allout-copy-exposed-to-buffer' if you want that. |
9179616f | 5640 | |
fd5359c6 | 5641 | Use `allout-flatten-exposed-to-buffer' for numeric sectional presentation. |
9179616f DL |
5642 | |
5643 | With repeat count, copy the exposed portions of only current topic. | |
5644 | ||
e126900f | 5645 | Other buffer has current buffer's name with \" exposed\" appended to |
9179616f DL |
5646 | it, unless optional second arg TOBUF is specified, in which case it is |
5647 | used verbatim." | |
5648 | (interactive "P") | |
fd5359c6 | 5649 | (allout-copy-exposed-to-buffer arg tobuf 'indent)) |
c567ac01 RS |
5650 | |
5651 | ;;;_ - LaTeX formatting | |
e126900f JB |
5652 | ;;;_ > allout-latex-verb-quote (string &optional flow) |
5653 | (defun allout-latex-verb-quote (string &optional flow) | |
539d7736 | 5654 | "Return copy of STRING for literal reproduction across LaTeX processing. |
c07583cd | 5655 | Expresses the original characters (including carriage returns) of the |
539d7736 | 5656 | string across LaTeX processing." |
9179616f DL |
5657 | (mapconcat (function |
5658 | (lambda (char) | |
c567ac01 RS |
5659 | (cond ((memq char '(?\\ ?$ ?% ?# ?& ?{ ?} ?_ ?^ ?- ?*)) |
5660 | (concat "\\char" (number-to-string char) "{}")) | |
5661 | ((= char ?\n) "\\\\") | |
9179616f | 5662 | (t (char-to-string char))))) |
e126900f | 5663 | string |
c567ac01 | 5664 | "")) |
fd5359c6 MR |
5665 | ;;;_ > allout-latex-verbatim-quote-curr-line () |
5666 | (defun allout-latex-verbatim-quote-curr-line () | |
c07583cd | 5667 | "Express line for exact (literal) representation across LaTeX processing. |
19b84ba3 | 5668 | |
c07583cd | 5669 | Adjust line contents so it is unaltered (from the original line) |
539d7736 | 5670 | across LaTeX processing, within the context of a `verbatim' |
c567ac01 | 5671 | environment. Leaves point at the end of the line." |
0458fa80 EZ |
5672 | (let ((inhibit-field-text-motion t)) |
5673 | (beginning-of-line) | |
5674 | (let ((beg (point)) | |
5675 | (end (progn (end-of-line)(point)))) | |
5676 | (goto-char beg) | |
2a1408fd DN |
5677 | (save-match-data |
5678 | (while (re-search-forward "\\\\" | |
5679 | ;;"\\\\\\|\\{\\|\\}\\|\\_\\|\\$\\|\\\"\\|\\&\\|\\^\\|\\-\\|\\*\\|#" | |
5680 | end ; bounded by end-of-line | |
5681 | 1) ; no matches, move to end & return nil | |
5682 | (goto-char (match-beginning 2)) | |
5683 | (insert "\\") | |
5684 | (setq end (1+ end)) | |
5685 | (goto-char (1+ (match-end 2)))))))) | |
e126900f JB |
5686 | ;;;_ > allout-insert-latex-header (buffer) |
5687 | (defun allout-insert-latex-header (buffer) | |
539d7736 | 5688 | "Insert initial LaTeX commands at point in BUFFER." |
c567ac01 RS |
5689 | ;; Much of this is being derived from the stuff in appendix of E in |
5690 | ;; the TeXBook, pg 421. | |
e126900f | 5691 | (set-buffer buffer) |
c567ac01 RS |
5692 | (let ((doc-style (format "\n\\documentstyle{%s}\n" |
5693 | "report")) | |
fd5359c6 | 5694 | (page-numbering (if allout-number-pages |
c567ac01 RS |
5695 | "\\pagestyle{empty}\n" |
5696 | "")) | |
c567ac01 | 5697 | (titlecmd (format "\\newcommand{\\titlecmd}[1]{{%s #1}}\n" |
fd5359c6 | 5698 | allout-title-style)) |
c567ac01 | 5699 | (labelcmd (format "\\newcommand{\\labelcmd}[1]{{%s #1}}\n" |
fd5359c6 | 5700 | allout-label-style)) |
c567ac01 | 5701 | (headlinecmd (format "\\newcommand{\\headlinecmd}[1]{{%s #1}}\n" |
fd5359c6 | 5702 | allout-head-line-style)) |
c567ac01 | 5703 | (bodylinecmd (format "\\newcommand{\\bodylinecmd}[1]{{%s #1}}\n" |
fd5359c6 | 5704 | allout-body-line-style)) |
c567ac01 RS |
5705 | (setlength (format "%s%s%s%s" |
5706 | "\\newlength{\\stepsize}\n" | |
5707 | "\\setlength{\\stepsize}{" | |
fd5359c6 | 5708 | allout-indent |
c567ac01 RS |
5709 | "}\n")) |
5710 | (oneheadline (format "%s%s%s%s%s%s%s" | |
5711 | "\\newcommand{\\OneHeadLine}[3]{%\n" | |
5712 | "\\noindent%\n" | |
5713 | "\\hspace*{#2\\stepsize}%\n" | |
5714 | "\\labelcmd{#1}\\hspace*{.2cm}" | |
5715 | "\\headlinecmd{#3}\\\\[" | |
fd5359c6 | 5716 | allout-line-skip |
c567ac01 RS |
5717 | "]\n}\n")) |
5718 | (onebodyline (format "%s%s%s%s%s%s" | |
5719 | "\\newcommand{\\OneBodyLine}[2]{%\n" | |
5720 | "\\noindent%\n" | |
5721 | "\\hspace*{#1\\stepsize}%\n" | |
5722 | "\\bodylinecmd{#2}\\\\[" | |
fd5359c6 | 5723 | allout-line-skip |
c567ac01 RS |
5724 | "]\n}\n")) |
5725 | (begindoc "\\begin{document}\n\\begin{center}\n") | |
5726 | (title (format "%s%s%s%s" | |
5727 | "\\titlecmd{" | |
fd5359c6 | 5728 | (allout-latex-verb-quote (if allout-title |
96b83743 | 5729 | (condition-case nil |
fd5359c6 | 5730 | (eval allout-title) |
d8350998 | 5731 | (error "<unnamed buffer>")) |
c567ac01 RS |
5732 | "Unnamed Outline")) |
5733 | "}\n" | |
5734 | "\\end{center}\n\n")) | |
5735 | (hsize "\\hsize = 7.5 true in\n") | |
5736 | (hoffset "\\hoffset = -1.5 true in\n") | |
5737 | (vspace "\\vspace{.1cm}\n\n")) | |
5738 | (insert (concat doc-style | |
5739 | page-numbering | |
5740 | titlecmd | |
5741 | labelcmd | |
5742 | headlinecmd | |
5743 | bodylinecmd | |
5744 | setlength | |
5745 | oneheadline | |
5746 | onebodyline | |
5747 | begindoc | |
5748 | title | |
5749 | hsize | |
5750 | hoffset | |
5751 | vspace) | |
5752 | ))) | |
e126900f JB |
5753 | ;;;_ > allout-insert-latex-trailer (buffer) |
5754 | (defun allout-insert-latex-trailer (buffer) | |
539d7736 | 5755 | "Insert concluding LaTeX commands at point in BUFFER." |
e126900f | 5756 | (set-buffer buffer) |
c567ac01 | 5757 | (insert "\n\\end{document}\n")) |
fd5359c6 MR |
5758 | ;;;_ > allout-latexify-one-item (depth prefix bullet text) |
5759 | (defun allout-latexify-one-item (depth prefix bullet text) | |
19b84ba3 RS |
5760 | "Insert LaTeX commands for formatting one outline item. |
5761 | ||
9179616f | 5762 | Args are the topics numeric DEPTH, the header PREFIX lead string, the |
19b84ba3 | 5763 | BULLET string, and a list of TEXT strings for the body." |
c567ac01 RS |
5764 | (let* ((head-line (if text (car text))) |
5765 | (body-lines (cdr text)) | |
5766 | (curr-line) | |
5767 | body-content bop) | |
5768 | ; Do the head line: | |
71296446 | 5769 | (insert (concat "\\OneHeadLine{\\verb\1 " |
d82979ea EZ |
5770 | (allout-latex-verb-quote bullet) |
5771 | "\1}{" | |
5772 | depth | |
5773 | "}{\\verb\1 " | |
5774 | (if head-line | |
5775 | (allout-latex-verb-quote head-line) | |
5776 | "") | |
5777 | "\1}\n")) | |
c567ac01 RS |
5778 | (if (not body-lines) |
5779 | nil | |
eac9cf5f PJ |
5780 | ;;(insert "\\beginlines\n") |
5781 | (insert "\\begin{verbatim}\n") | |
c567ac01 RS |
5782 | (while body-lines |
5783 | (setq curr-line (car body-lines)) | |
5784 | (if (and (not body-content) | |
5785 | (not (string-match "^\\s-*$" curr-line))) | |
5786 | (setq body-content t)) | |
5787 | ; Mangle any occurrences of | |
5788 | ; "\end{verbatim}" in text, | |
5789 | ; it's special: | |
5790 | (if (and body-content | |
5791 | (setq bop (string-match "\\end{verbatim}" curr-line))) | |
5792 | (setq curr-line (concat (substring curr-line 0 bop) | |
5793 | ">" | |
5794 | (substring curr-line bop)))) | |
eac9cf5f PJ |
5795 | ;;(insert "|" (car body-lines) "|") |
5796 | (insert curr-line) | |
fd5359c6 | 5797 | (allout-latex-verbatim-quote-curr-line) |
eac9cf5f | 5798 | (insert "\n") |
c567ac01 RS |
5799 | (setq body-lines (cdr body-lines))) |
5800 | (if body-content | |
5801 | (setq body-content nil) | |
5802 | (forward-char -1) | |
eac9cf5f | 5803 | (insert "\\ ") |
c567ac01 | 5804 | (forward-char 1)) |
eac9cf5f PJ |
5805 | ;;(insert "\\endlines\n") |
5806 | (insert "\\end{verbatim}\n") | |
c567ac01 | 5807 | ))) |
fd5359c6 MR |
5808 | ;;;_ > allout-latexify-exposed (arg &optional tobuf) |
5809 | (defun allout-latexify-exposed (arg &optional tobuf) | |
539d7736 | 5810 | "Format current topics exposed portions to TOBUF for LaTeX processing. |
19b84ba3 RS |
5811 | TOBUF defaults to a buffer named the same as the current buffer, but |
5812 | with \"*\" prepended and \" latex-formed*\" appended. | |
c567ac01 RS |
5813 | |
5814 | With repeat count, copy the exposed portions of entire buffer." | |
5815 | ||
5816 | (interactive "P") | |
5817 | (if (not tobuf) | |
5818 | (setq tobuf | |
5819 | (get-buffer-create (concat "*" (buffer-name) " latexified*")))) | |
5820 | (let* ((start-pt (point)) | |
fd5359c6 MR |
5821 | (beg (if arg (point-min) (allout-back-to-current-heading))) |
5822 | (end (if arg (point-max) (allout-end-of-current-subtree))) | |
c567ac01 RS |
5823 | (buf (current-buffer))) |
5824 | (set-buffer tobuf) | |
1977b8f6 | 5825 | (erase-buffer) |
fd5359c6 | 5826 | (allout-insert-latex-header tobuf) |
c567ac01 | 5827 | (goto-char (point-max)) |
fd5359c6 | 5828 | (allout-process-exposed 'allout-latexify-one-item |
c567ac01 RS |
5829 | beg |
5830 | end | |
5831 | buf | |
5832 | tobuf) | |
5833 | (goto-char (point-max)) | |
fd5359c6 | 5834 | (allout-insert-latex-trailer tobuf) |
1977b8f6 | 5835 | (goto-char (point-min)) |
c567ac01 RS |
5836 | (pop-to-buffer buf) |
5837 | (goto-char start-pt))) | |
5838 | ||
d82979ea | 5839 | ;;;_ #8 Encryption |
0949617b EZ |
5840 | ;;;_ > allout-toggle-current-subtree-encryption (&optional fetch-pass) |
5841 | (defun allout-toggle-current-subtree-encryption (&optional fetch-pass) | |
69027dea | 5842 | "Encrypt clear or decrypt encoded text of visibly-containing topic's contents. |
d82979ea | 5843 | |
0949617b | 5844 | Optional FETCH-PASS universal argument provokes key-pair encryption with |
c07583cd | 5845 | single universal argument. With doubled universal argument (value = 16), |
0949617b EZ |
5846 | it forces prompting for the passphrase regardless of availability from the |
5847 | passphrase cache. With no universal argument, the appropriate passphrase | |
69027dea | 5848 | is obtained from the cache, if available, else from the user. |
d82979ea | 5849 | |
6eb0a10d | 5850 | Only GnuPG encryption is supported. |
d82979ea | 5851 | |
6eb0a10d | 5852 | \*NOTE WELL* that the encrypted text must be ascii-armored. For gnupg |
d82979ea EZ |
5853 | encryption, include the option ``armor'' in your ~/.gnupg/gpg.conf file. |
5854 | ||
5855 | Both symmetric-key and key-pair encryption is implemented. Symmetric is | |
c07583cd | 5856 | the default, use a single (x4) universal argument for keypair mode. |
d82979ea EZ |
5857 | |
5858 | Encrypted topic's bullet is set to a `~' to signal that the contents of the | |
c07583cd | 5859 | topic (body and subtopics, but not heading) is pending encryption or |
0949617b EZ |
5860 | encrypted. `*' asterisk immediately after the bullet signals that the body |
5861 | is encrypted, its' absence means the topic is meant to be encrypted but is | |
5862 | not. When a file with topics pending encryption is saved, topics pending | |
5863 | encryption are encrypted. See allout-encrypt-unencrypted-on-saves for | |
5864 | auto-encryption specifics. | |
5865 | ||
6eb0a10d | 5866 | \*NOTE WELL* that automatic encryption that happens during saves will |
60c7bbd2 | 5867 | default to symmetric encryption -- you must deliberately (re)encrypt key-pair |
0949617b | 5868 | encrypted topics if you want them to continue to use the key-pair cipher. |
d82979ea | 5869 | |
d8350998 | 5870 | Level-one topics, with prefix consisting solely of an `*' asterisk, cannot be |
d82979ea EZ |
5871 | encrypted. If you want to encrypt the contents of a top-level topic, use |
5872 | \\[allout-shift-in] to increase its depth. | |
5873 | ||
0949617b EZ |
5874 | Passphrase Caching |
5875 | ||
5876 | The encryption passphrase is solicited if not currently available in the | |
5877 | passphrase cache from a recent encryption action. | |
5878 | ||
6eb0a10d EZ |
5879 | The solicited passphrase is retained for reuse in a cache, if enabled. See |
5880 | `pgg-cache-passphrase' and `pgg-passphrase-cache-expiry' for details. | |
0949617b EZ |
5881 | |
5882 | Symmetric Passphrase Hinting and Verification | |
5883 | ||
5884 | If the file previously had no associated passphrase, or had a different | |
5885 | passphrase than specified, the user is prompted to repeat the new one for | |
5886 | corroboration. A random string encrypted by the new passphrase is set on | |
5887 | the buffer-specific variable `allout-passphrase-verifier-string', for | |
5888 | confirmation of the passphrase when next obtained, before encrypting or | |
5889 | decrypting anything with it. This helps avoid mistakenly shifting between | |
5890 | keys. | |
5891 | ||
5892 | If allout customization var `allout-passphrase-verifier-handling' is | |
5893 | non-nil, an entry for `allout-passphrase-verifier-string' and its value is | |
5894 | added to an Emacs 'local variables' section at the end of the file, which | |
5895 | is created if necessary. That setting is for retention of the passphrase | |
a0a022d5 | 5896 | verifier across Emacs sessions. |
0949617b EZ |
5897 | |
5898 | Similarly, `allout-passphrase-hint-string' stores a user-provided reminder | |
5899 | about their passphrase, and `allout-passphrase-hint-handling' specifies | |
5900 | when the hint is presented, or if passphrase hints are disabled. If | |
c07583cd | 5901 | enabled (see the `allout-passphrase-hint-handling' docstring for details), |
0949617b EZ |
5902 | the hint string is stored in the local-variables section of the file, and |
5903 | solicited whenever the passphrase is changed." | |
69027dea EZ |
5904 | (interactive "P") |
5905 | (save-excursion | |
5906 | (allout-back-to-current-heading) | |
96b83743 | 5907 | (allout-toggle-subtree-encryption fetch-pass) |
69027dea EZ |
5908 | ) |
5909 | ) | |
5910 | ;;;_ > allout-toggle-subtree-encryption (&optional fetch-pass) | |
5911 | (defun allout-toggle-subtree-encryption (&optional fetch-pass) | |
c07583cd | 5912 | "Encrypt clear text or decrypt encoded topic contents (body and subtopics.) |
69027dea EZ |
5913 | |
5914 | Optional FETCH-PASS universal argument provokes key-pair encryption with | |
c07583cd | 5915 | single universal argument. With doubled universal argument (value = 16), |
69027dea EZ |
5916 | it forces prompting for the passphrase regardless of availability from the |
5917 | passphrase cache. With no universal argument, the appropriate passphrase | |
5918 | is obtained from the cache, if available, else from the user. | |
5919 | ||
e14adf87 CY |
5920 | Currently only GnuPG encryption is supported, and integration |
5921 | with gpg-agent is not yet implemented. | |
69027dea EZ |
5922 | |
5923 | \**NOTE WELL** that the encrypted text must be ascii-armored. For gnupg | |
5924 | encryption, include the option ``armor'' in your ~/.gnupg/gpg.conf file. | |
5925 | ||
5926 | See `allout-toggle-current-subtree-encryption' for more details." | |
d82979ea EZ |
5927 | |
5928 | (interactive "P") | |
5929 | (save-excursion | |
5930 | (allout-end-of-prefix t) | |
5931 | ||
d8350998 | 5932 | (if (= allout-recent-depth 1) |
d82979ea EZ |
5933 | (error (concat "Cannot encrypt or decrypt level 1 topics -" |
5934 | " shift it in to make it encryptable"))) | |
5935 | ||
d82979ea | 5936 | (let* ((allout-buffer (current-buffer)) |
498a6493 | 5937 | ;; Assess location: |
d8350998 | 5938 | (bullet-pos allout-recent-prefix-beginning) |
d82979ea EZ |
5939 | (after-bullet-pos (point)) |
5940 | (was-encrypted | |
5941 | (progn (if (= (point-max) after-bullet-pos) | |
5942 | (error "no body to encrypt")) | |
0949617b | 5943 | (allout-encrypted-topic-p))) |
96b83743 | 5944 | (was-collapsed (if (not (search-forward "\n" nil t)) |
d82979ea EZ |
5945 | nil |
5946 | (backward-char 1) | |
96b83743 | 5947 | (allout-hidden-p))) |
d82979ea EZ |
5948 | (subtree-beg (1+ (point))) |
5949 | (subtree-end (allout-end-of-subtree)) | |
5950 | (subject-text (buffer-substring-no-properties subtree-beg | |
5951 | subtree-end)) | |
5952 | (subtree-end-char (char-after (1- subtree-end))) | |
96b83743 | 5953 | (subtree-trailing-char (char-after subtree-end)) |
60c7bbd2 | 5954 | ;; kluge -- result-text needs to be nil, but we also want to |
96b83743 EZ |
5955 | ;; check for the error condition |
5956 | (result-text (if (or (string= "" subject-text) | |
5957 | (string= "\n" subject-text)) | |
5958 | (error "No topic contents to %scrypt" | |
5959 | (if was-encrypted "de" "en")) | |
5960 | nil)) | |
d82979ea | 5961 | ;; Assess key parameters: |
0949617b | 5962 | (key-info (or |
d82979ea EZ |
5963 | ;; detect the type by which it is already encrypted |
5964 | (and was-encrypted | |
0949617b EZ |
5965 | (allout-encrypted-key-info subject-text)) |
5966 | (and (member fetch-pass '(4 (4))) | |
5967 | '(keypair nil)) | |
5968 | '(symmetric nil))) | |
5969 | (for-key-type (car key-info)) | |
5970 | (for-key-identity (cadr key-info)) | |
498a6493 CY |
5971 | (fetch-pass (and fetch-pass (member fetch-pass '(16 (16))))) |
5972 | (was-coding-system buffer-file-coding-system)) | |
5973 | ||
5974 | (when (not was-encrypted) | |
5975 | ;; ensure that non-ascii chars pending encryption are noticed before | |
116f996b | 5976 | ;; they're encrypted, so the coding system is set to accommodate |
498a6493 CY |
5977 | ;; them. |
5978 | (setq buffer-file-coding-system | |
5979 | (select-safe-coding-system subtree-beg subtree-end)) | |
5980 | ;; if the coding system for the text being encrypted is different | |
5981 | ;; than that prevailing, then there a real risk that the coding | |
5982 | ;; system can't be noticed by emacs when the file is visited. to | |
5983 | ;; mitigate that, offer to preserve the coding system using a file | |
5984 | ;; local variable. | |
5985 | (if (and (not (equal buffer-file-coding-system | |
5986 | was-coding-system)) | |
5987 | (yes-or-no-p | |
5988 | (format (concat "Register coding system %s as file local" | |
5989 | " var? Necessary when only encrypted text" | |
5990 | " is in that coding system. ") | |
5991 | buffer-file-coding-system))) | |
5992 | (allout-adjust-file-variable "buffer-file-coding-system" | |
5993 | buffer-file-coding-system))) | |
d82979ea EZ |
5994 | |
5995 | (setq result-text | |
5996 | (allout-encrypt-string subject-text was-encrypted | |
0949617b EZ |
5997 | (current-buffer) |
5998 | for-key-type for-key-identity fetch-pass)) | |
d82979ea EZ |
5999 | |
6000 | ;; Replace the subtree with the processed product. | |
6001 | (allout-unprotected | |
6002 | (progn | |
6003 | (set-buffer allout-buffer) | |
6004 | (delete-region subtree-beg subtree-end) | |
6005 | (insert result-text) | |
6006 | (if was-collapsed | |
96b83743 EZ |
6007 | (allout-flag-region (1- subtree-beg) (point) t)) |
6008 | ;; adjust trailing-blank-lines to preserve topic spacing: | |
d82979ea | 6009 | (if (not was-encrypted) |
96b83743 EZ |
6010 | (if (and (= subtree-end-char ?\n) |
6011 | (= subtree-trailing-char ?\n)) | |
6012 | (insert subtree-trailing-char))) | |
d82979ea EZ |
6013 | ;; Ensure that the item has an encrypted-entry bullet: |
6014 | (if (not (string= (buffer-substring-no-properties | |
6015 | (1- after-bullet-pos) after-bullet-pos) | |
6016 | allout-topic-encryption-bullet)) | |
6017 | (progn (goto-char (1- after-bullet-pos)) | |
6018 | (delete-char 1) | |
6019 | (insert allout-topic-encryption-bullet))) | |
6020 | (if was-encrypted | |
6021 | ;; Remove the is-encrypted bullet qualifier: | |
6022 | (progn (goto-char after-bullet-pos) | |
6023 | (delete-char 1)) | |
6024 | ;; Add the is-encrypted bullet qualifier: | |
6025 | (goto-char after-bullet-pos) | |
d8350998 | 6026 | (insert "*")))) |
1b4d97b2 CY |
6027 | (run-hook-with-args 'allout-structure-added-hook |
6028 | bullet-pos subtree-end)))) | |
0949617b EZ |
6029 | ;;;_ > allout-encrypt-string (text decrypt allout-buffer key-type for-key |
6030 | ;;; fetch-pass &optional retried verifying | |
6031 | ;;; passphrase) | |
6032 | (defun allout-encrypt-string (text decrypt allout-buffer key-type for-key | |
48bd8440 EZ |
6033 | fetch-pass &optional retried rejected |
6034 | verifying passphrase) | |
0949617b | 6035 | "Encrypt or decrypt message TEXT. |
d82979ea | 6036 | |
0949617b | 6037 | If DECRYPT is true (default false), then decrypt instead of encrypt. |
d82979ea | 6038 | |
0949617b | 6039 | FETCH-PASS (default false) forces fresh prompting for the passphrase. |
d82979ea | 6040 | |
60c7bbd2 RS |
6041 | KEY-TYPE, either `symmetric' or `keypair', specifies which type |
6042 | of cypher to use. | |
d82979ea | 6043 | |
0949617b EZ |
6044 | FOR-KEY is human readable identification of the first of the user's |
6045 | eligible secret keys a keypair decryption targets, or else nil. | |
d82979ea | 6046 | |
60c7bbd2 | 6047 | Optional RETRIED is for internal use -- conveys the number of failed keys |
0949617b EZ |
6048 | that have been solicited in sequence leading to this current call. |
6049 | ||
6050 | Optional PASSPHRASE enables explicit delivery of the decryption passphrase, | |
6051 | for verification purposes. | |
d82979ea | 6052 | |
60c7bbd2 | 6053 | Optional REJECTED is for internal use -- conveys the number of |
48bd8440 EZ |
6054 | rejections due to matches against |
6055 | `allout-encryption-ciphertext-rejection-regexps', as limited by | |
6056 | `allout-encryption-ciphertext-rejection-ceiling'. | |
6057 | ||
0949617b | 6058 | Returns the resulting string, or nil if the transformation fails." |
d82979ea | 6059 | |
0949617b EZ |
6060 | (require 'pgg) |
6061 | ||
69027dea EZ |
6062 | (if (not (fboundp 'pgg-encrypt-symmetric)) |
6063 | (error "Allout encryption depends on a newer version of pgg")) | |
6064 | ||
0949617b EZ |
6065 | (let* ((scheme (upcase |
6066 | (format "%s" (or pgg-scheme pgg-default-scheme "GPG")))) | |
6067 | (for-key (and (equal key-type 'keypair) | |
6068 | (or for-key | |
6069 | (split-string (read-string | |
6070 | (format "%s message recipients: " | |
6071 | scheme)) | |
6072 | "[ \t,]+")))) | |
6073 | (target-prompt-id (if (equal key-type 'keypair) | |
6074 | (if (= (length for-key) 1) | |
6075 | (car for-key) for-key) | |
6076 | (buffer-name allout-buffer))) | |
6077 | (target-cache-id (format "%s-%s" | |
6078 | key-type | |
6079 | (if (equal key-type 'keypair) | |
6080 | target-prompt-id | |
6081 | (or (buffer-file-name allout-buffer) | |
6082 | target-prompt-id)))) | |
498a6493 CY |
6083 | (encoding (with-current-buffer allout-buffer |
6084 | buffer-file-coding-system)) | |
6085 | (multibyte (with-current-buffer allout-buffer | |
6086 | enable-multibyte-characters)) | |
48bd8440 EZ |
6087 | (strip-plaintext-regexps |
6088 | (if (not decrypt) | |
6089 | (allout-get-configvar-values | |
6090 | 'allout-encryption-plaintext-sanitization-regexps))) | |
6091 | (reject-ciphertext-regexps | |
6092 | (if (not decrypt) | |
6093 | (allout-get-configvar-values | |
6094 | 'allout-encryption-ciphertext-rejection-regexps))) | |
6095 | (rejected (or rejected 0)) | |
6096 | (rejections-left (- allout-encryption-ciphertext-rejection-ceiling | |
6097 | rejected)) | |
e14adf87 | 6098 | result-text status |
38b2ca53 | 6099 | ) |
0949617b EZ |
6100 | |
6101 | (if (and fetch-pass (not passphrase)) | |
6102 | ;; Force later fetch by evicting passphrase from the cache. | |
6103 | (pgg-remove-passphrase-from-cache target-cache-id t)) | |
6104 | ||
6105 | (catch 'encryption-failed | |
6106 | ||
6eb0a10d EZ |
6107 | ;; We handle only symmetric-key passphrase caching. |
6108 | (if (and (not passphrase) | |
6109 | (not (equal key-type 'keypair))) | |
0949617b EZ |
6110 | (setq passphrase (allout-obtain-passphrase for-key |
6111 | target-cache-id | |
6112 | target-prompt-id | |
6113 | key-type | |
6114 | allout-buffer | |
6115 | retried fetch-pass))) | |
48bd8440 | 6116 | |
0949617b EZ |
6117 | (with-temp-buffer |
6118 | ||
96b83743 | 6119 | (insert text) |
d82979ea | 6120 | |
498a6493 CY |
6121 | ;; convey the text characteristics of the original buffer: |
6122 | (set-buffer-multibyte multibyte) | |
6123 | (when encoding | |
6124 | (set-buffer-file-coding-system encoding) | |
6125 | (if (not decrypt) | |
6126 | (encode-coding-region (point-min) (point-max) encoding))) | |
6127 | ||
48bd8440 EZ |
6128 | (when (and strip-plaintext-regexps (not decrypt)) |
6129 | (dolist (re strip-plaintext-regexps) | |
6130 | (let ((re (if (listp re) (car re) re)) | |
6131 | (replacement (if (listp re) (cadr re) ""))) | |
6132 | (goto-char (point-min)) | |
2a1408fd DN |
6133 | (save-match-data |
6134 | (while (re-search-forward re nil t) | |
6135 | (replace-match replacement nil nil)))))) | |
48bd8440 | 6136 | |
0949617b | 6137 | (cond |
d82979ea | 6138 | |
0949617b EZ |
6139 | ;; symmetric: |
6140 | ((equal key-type 'symmetric) | |
6141 | (setq status | |
6142 | (if decrypt | |
6143 | ||
6144 | (pgg-decrypt (point-min) (point-max) passphrase) | |
6145 | ||
6146 | (pgg-encrypt-symmetric (point-min) (point-max) | |
6147 | passphrase))) | |
6148 | ||
6149 | (if status | |
6150 | (pgg-situate-output (point-min) (point-max)) | |
60c7bbd2 | 6151 | ;; failed -- handle passphrase caching |
0949617b EZ |
6152 | (if verifying |
6153 | (throw 'encryption-failed nil) | |
6154 | (pgg-remove-passphrase-from-cache target-cache-id t) | |
60c7bbd2 | 6155 | (error "Symmetric-cipher %scryption failed -- %s" |
48bd8440 | 6156 | (if decrypt "de" "en") |
60c7bbd2 | 6157 | "try again with different passphrase")))) |
0949617b | 6158 | |
ea18b2b1 | 6159 | ;; encrypt `keypair': |
0949617b EZ |
6160 | ((not decrypt) |
6161 | ||
6162 | (setq status | |
6163 | ||
6164 | (pgg-encrypt for-key | |
6165 | nil (point-min) (point-max) passphrase)) | |
6166 | ||
6167 | (if status | |
6168 | (pgg-situate-output (point-min) (point-max)) | |
6169 | (error (pgg-remove-passphrase-from-cache target-cache-id t) | |
6170 | (error "encryption failed")))) | |
6171 | ||
ea18b2b1 | 6172 | ;; decrypt `keypair': |
0949617b EZ |
6173 | (t |
6174 | ||
6175 | (setq status | |
6176 | (pgg-decrypt (point-min) (point-max) passphrase)) | |
6177 | ||
6178 | (if status | |
6179 | (pgg-situate-output (point-min) (point-max)) | |
6180 | (error (pgg-remove-passphrase-from-cache target-cache-id t) | |
48bd8440 | 6181 | (error "decryption failed"))))) |
0949617b EZ |
6182 | |
6183 | (setq result-text | |
d8350998 CY |
6184 | (buffer-substring-no-properties |
6185 | 1 (- (point-max) (if decrypt 0 1)))) | |
d82979ea | 6186 | ) |
48bd8440 | 6187 | |
60c7bbd2 | 6188 | ;; validate result -- non-empty |
48bd8440 EZ |
6189 | (cond ((not result-text) |
6190 | (if verifying | |
6191 | nil | |
6192 | ;; transform was fruitless, retry w/new passphrase. | |
6193 | (pgg-remove-passphrase-from-cache target-cache-id t) | |
6194 | (allout-encrypt-string text decrypt allout-buffer | |
6195 | key-type for-key nil | |
6196 | (if retried (1+ retried) 1) | |
6197 | rejected verifying nil))) | |
6198 | ||
6199 | ;; Retry (within limit) if ciphertext contains rejections: | |
6200 | ((and (not decrypt) | |
6201 | ;; Check for disqualification of this ciphertext: | |
6202 | (let ((regexps reject-ciphertext-regexps) | |
6203 | reject-it) | |
6204 | (while (and regexps (not reject-it)) | |
6205 | (setq reject-it (string-match (car regexps) | |
6206 | result-text)) | |
6207 | (pop regexps)) | |
6208 | reject-it)) | |
6209 | (setq rejections-left (1- rejections-left)) | |
6210 | (if (<= rejections-left 0) | |
6211 | (error (concat "Ciphertext rejected too many times" | |
6212 | " (%s), per `%s'") | |
6213 | allout-encryption-ciphertext-rejection-ceiling | |
6214 | 'allout-encryption-ciphertext-rejection-regexps) | |
6215 | (allout-encrypt-string text decrypt allout-buffer | |
6216 | key-type for-key nil | |
6217 | retried (1+ rejected) | |
6218 | verifying passphrase))) | |
6219 | ;; Barf if encryption yields extraordinary control chars: | |
6220 | ((and (not decrypt) | |
6221 | (string-match "[\C-a\C-k\C-o-\C-z\C-@]" | |
6222 | result-text)) | |
6223 | (error (concat "Encryption produced non-armored text, which" | |
60c7bbd2 | 6224 | "conflicts with allout mode -- reconfigure!"))) |
48bd8440 EZ |
6225 | |
6226 | ;; valid result and just verifying or non-symmetric: | |
6227 | ((or verifying (not (equal key-type 'symmetric))) | |
6228 | (if (or verifying decrypt) | |
6229 | (pgg-add-passphrase-to-cache target-cache-id | |
6230 | passphrase t)) | |
6231 | result-text) | |
6232 | ||
60c7bbd2 | 6233 | ;; valid result and regular symmetric -- "register" |
48bd8440 EZ |
6234 | ;; passphrase with mnemonic aids/cache. |
6235 | (t | |
6236 | (set-buffer allout-buffer) | |
6237 | (if passphrase | |
6238 | (pgg-add-passphrase-to-cache target-cache-id | |
6239 | passphrase t)) | |
6240 | (allout-update-passphrase-mnemonic-aids for-key passphrase | |
6241 | allout-buffer) | |
6242 | result-text) | |
6243 | ) | |
d82979ea | 6244 | ) |
d82979ea EZ |
6245 | ) |
6246 | ) | |
0949617b EZ |
6247 | ;;;_ > allout-obtain-passphrase (for-key cache-id prompt-id key-type |
6248 | ;;; allout-buffer retried fetch-pass) | |
4034b0e2 | 6249 | (defun allout-obtain-passphrase (for-key cache-id prompt-id key-type |
0949617b EZ |
6250 | allout-buffer retried fetch-pass) |
6251 | "Obtain passphrase for a key from the cache or else from the user. | |
6252 | ||
6253 | When obtaining from the user, symmetric-cipher passphrases are verified | |
6254 | against either, if available and enabled, a random string that was | |
6255 | encrypted against the passphrase, or else against repeated entry by the | |
6256 | user for corroboration. | |
6257 | ||
6258 | FOR-KEY is the key for which the passphrase is being obtained. | |
6259 | ||
6260 | CACHE-ID is the cache id of the key for the passphrase. | |
6261 | ||
6262 | PROMPT-ID is the id for use when prompting the user. | |
6263 | ||
60c7bbd2 | 6264 | KEY-TYPE is either `symmetric' or `keypair'. |
0949617b EZ |
6265 | |
6266 | ALLOUT-BUFFER is the buffer containing the entry being en/decrypted. | |
6267 | ||
6268 | RETRIED is the number of this attempt to obtain this passphrase. | |
6269 | ||
6270 | FETCH-PASS causes the passphrase to be solicited from the user, regardless | |
6271 | of the availability of a cached copy." | |
d82979ea EZ |
6272 | |
6273 | (if (not (equal key-type 'symmetric)) | |
0949617b EZ |
6274 | ;; do regular passphrase read on non-symmetric passphrase: |
6275 | (pgg-read-passphrase (format "%s passphrase%s: " | |
6276 | (upcase (format "%s" (or pgg-scheme | |
6277 | pgg-default-scheme | |
6278 | "GPG"))) | |
6279 | (if prompt-id | |
6280 | (format " for %s" prompt-id) | |
6281 | "")) | |
6282 | cache-id t) | |
d82979ea EZ |
6283 | |
6284 | ;; Symmetric hereon: | |
6285 | ||
6286 | (save-excursion | |
6287 | (set-buffer allout-buffer) | |
0949617b EZ |
6288 | (let* ((hint (if (and (not (string= allout-passphrase-hint-string "")) |
6289 | (or (equal allout-passphrase-hint-handling 'always) | |
6290 | (and (equal allout-passphrase-hint-handling | |
6291 | 'needed) | |
d82979ea | 6292 | retried))) |
0949617b | 6293 | (format " [%s]" allout-passphrase-hint-string) |
d82979ea EZ |
6294 | "")) |
6295 | (retry-message (if retried (format " (%s retry)" retried) "")) | |
0949617b EZ |
6296 | (prompt-sans-hint (format "'%s' symmetric passphrase%s: " |
6297 | prompt-id retry-message)) | |
6298 | (full-prompt (format "'%s' symmetric passphrase%s%s: " | |
6299 | prompt-id hint retry-message)) | |
d82979ea | 6300 | (prompt full-prompt) |
0949617b EZ |
6301 | (verifier-string (allout-get-encryption-passphrase-verifier)) |
6302 | ||
6303 | (cached (and (not fetch-pass) | |
6304 | (pgg-read-passphrase-from-cache cache-id t))) | |
6305 | (got-pass (or cached | |
6306 | (pgg-read-passphrase full-prompt cache-id t))) | |
d82979ea EZ |
6307 | confirmation) |
6308 | ||
0949617b | 6309 | (if (not got-pass) |
d82979ea EZ |
6310 | nil |
6311 | ||
0949617b | 6312 | ;; Duplicate our handle on the passphrase so it's not clobbered by |
d82979ea | 6313 | ;; deactivate-passwd memory clearing: |
48bd8440 | 6314 | (setq got-pass (copy-sequence got-pass)) |
d82979ea EZ |
6315 | |
6316 | (cond (verifier-string | |
0949617b EZ |
6317 | (save-window-excursion |
6318 | (if (allout-encrypt-string verifier-string 'decrypt | |
6319 | allout-buffer 'symmetric | |
48bd8440 EZ |
6320 | for-key nil 0 0 'verifying |
6321 | (copy-sequence got-pass)) | |
0949617b EZ |
6322 | (setq confirmation (format "%s" got-pass)))) |
6323 | ||
6324 | (if (and (not confirmation) | |
d82979ea | 6325 | (if (yes-or-no-p |
0949617b | 6326 | (concat "Passphrase differs from established" |
60c7bbd2 | 6327 | " -- use new one instead? ")) |
d82979ea EZ |
6328 | ;; deactivate password for subsequent |
6329 | ;; confirmation: | |
0949617b EZ |
6330 | (progn |
6331 | (pgg-remove-passphrase-from-cache cache-id t) | |
6332 | (setq prompt prompt-sans-hint) | |
6333 | nil) | |
d82979ea | 6334 | t)) |
0949617b | 6335 | (progn (pgg-remove-passphrase-from-cache cache-id t) |
5a0c3f56 | 6336 | (error "Wrong passphrase")))) |
60c7bbd2 | 6337 | ;; No verifier string -- force confirmation by repetition of |
0949617b EZ |
6338 | ;; (new) passphrase: |
6339 | ((or fetch-pass (not cached)) | |
6340 | (pgg-remove-passphrase-from-cache cache-id t)))) | |
60c7bbd2 | 6341 | ;; confirmation vs new input -- doing pgg-read-passphrase will do the |
d82979ea | 6342 | ;; right thing, in either case: |
0949617b EZ |
6343 | (if (not confirmation) |
6344 | (setq confirmation | |
6345 | (pgg-read-passphrase (concat prompt | |
6346 | " ... confirm spelling: ") | |
6347 | cache-id t))) | |
d82979ea | 6348 | (prog1 |
0949617b | 6349 | (if (equal got-pass confirmation) |
d82979ea EZ |
6350 | confirmation |
6351 | (if (yes-or-no-p (concat "spelling of original and" | |
60c7bbd2 | 6352 | " confirmation differ -- retry? ")) |
d82979ea | 6353 | (progn (setq retried (if retried (1+ retried) 1)) |
0949617b | 6354 | (pgg-remove-passphrase-from-cache cache-id t) |
d82979ea | 6355 | ;; recurse to this routine: |
0949617b EZ |
6356 | (pgg-read-passphrase prompt-sans-hint cache-id t)) |
6357 | (pgg-remove-passphrase-from-cache cache-id t) | |
5a0c3f56 | 6358 | (error "Confirmation failed")))))))) |
d82979ea EZ |
6359 | ;;;_ > allout-encrypted-topic-p () |
6360 | (defun allout-encrypted-topic-p () | |
6361 | "True if the current topic is encryptable and encrypted." | |
6362 | (save-excursion | |
6363 | (allout-end-of-prefix t) | |
6364 | (and (string= (buffer-substring-no-properties (1- (point)) (point)) | |
6365 | allout-topic-encryption-bullet) | |
2a1408fd | 6366 | (save-match-data (looking-at "\\*"))) |
d82979ea EZ |
6367 | ) |
6368 | ) | |
0949617b EZ |
6369 | ;;;_ > allout-encrypted-key-info (text) |
6370 | ;; XXX gpg-specific, alas | |
6371 | (defun allout-encrypted-key-info (text) | |
6372 | "Return a pair of the key type and identity of a recipient's secret key. | |
6373 | ||
60c7bbd2 | 6374 | The key type is one of `symmetric' or `keypair'. |
d82979ea | 6375 | |
60c7bbd2 | 6376 | If `keypair', and some of the user's secret keys are among those for which |
75bcb912 | 6377 | the message was encoded, return the identity of the first. Otherwise, |
0949617b | 6378 | return nil for the second item of the pair. |
d82979ea | 6379 | |
0949617b EZ |
6380 | An error is raised if the text is not encrypted." |
6381 | (require 'pgg-parse) | |
d82979ea | 6382 | (save-excursion |
0949617b | 6383 | (with-temp-buffer |
96b83743 | 6384 | (insert text) |
0949617b EZ |
6385 | (let* ((parsed-armor (pgg-parse-armor-region (point-min) (point-max))) |
6386 | (type (if (pgg-gpg-symmetric-key-p parsed-armor) | |
6387 | 'symmetric | |
6388 | 'keypair)) | |
6389 | secret-keys first-secret-key for-key-owner) | |
6390 | (if (equal type 'keypair) | |
6391 | (setq secret-keys (pgg-gpg-lookup-all-secret-keys) | |
6392 | first-secret-key (pgg-gpg-select-matching-key parsed-armor | |
6393 | secret-keys) | |
6394 | for-key-owner (and first-secret-key | |
6395 | (pgg-gpg-lookup-key-owner | |
6396 | first-secret-key)))) | |
6397 | (list type (pgg-gpg-key-id-from-key-owner for-key-owner)) | |
6398 | ) | |
d82979ea EZ |
6399 | ) |
6400 | ) | |
6401 | ) | |
0949617b EZ |
6402 | ;;;_ > allout-create-encryption-passphrase-verifier (passphrase) |
6403 | (defun allout-create-encryption-passphrase-verifier (passphrase) | |
6404 | "Encrypt random message for later validation of symmetric key's passphrase." | |
d82979ea EZ |
6405 | ;; use 20 random ascii characters, across the entire ascii range. |
6406 | (random t) | |
6407 | (let ((spew (make-string 20 ?\0))) | |
6408 | (dotimes (i (length spew)) | |
6409 | (aset spew i (1+ (random 254)))) | |
0949617b | 6410 | (allout-encrypt-string spew nil (current-buffer) 'symmetric |
48bd8440 | 6411 | nil nil 0 0 passphrase)) |
d82979ea | 6412 | ) |
0949617b | 6413 | ;;;_ > allout-update-passphrase-mnemonic-aids (for-key passphrase |
4034b0e2 | 6414 | ;;; outline-buffer) |
0949617b EZ |
6415 | (defun allout-update-passphrase-mnemonic-aids (for-key passphrase |
6416 | outline-buffer) | |
6417 | "Update passphrase verifier and hint strings if necessary. | |
6418 | ||
6419 | See `allout-passphrase-verifier-string' and `allout-passphrase-hint-string' | |
6420 | settings. | |
6421 | ||
75bcb912 | 6422 | PASSPHRASE is the passphrase being mnemonicized. |
0949617b EZ |
6423 | |
6424 | OUTLINE-BUFFER is the buffer of the outline being adjusted. | |
6425 | ||
6426 | These are used to help the user keep track of the passphrase they use for | |
6427 | symmetric encryption in the file. | |
6428 | ||
6429 | Behavior is governed by `allout-passphrase-verifier-handling', | |
6430 | `allout-passphrase-hint-handling', and also, controlling whether the values | |
6431 | are preserved on Emacs local file variables, | |
6432 | `allout-enable-file-variable-adjustment'." | |
6433 | ||
6434 | ;; If passphrase doesn't agree with current verifier: | |
6435 | ;; - adjust the verifier | |
6436 | ;; - if passphrase hint handling is enabled, adjust the passphrase hint | |
6437 | ;; - if file var settings are enabled, adjust the file vars | |
6438 | ||
6439 | (let* ((new-verifier-needed (not (allout-verify-passphrase | |
6440 | for-key passphrase outline-buffer))) | |
6441 | (new-verifier-string | |
6442 | (if new-verifier-needed | |
6443 | ;; Collapse to a single line and enclose in string quotes: | |
6444 | (subst-char-in-string | |
6445 | ?\n ?\C-a (allout-create-encryption-passphrase-verifier | |
6446 | passphrase)))) | |
6447 | new-hint) | |
6448 | (when new-verifier-string | |
6449 | ;; do the passphrase hint first, since it's interactive | |
6450 | (when (and allout-passphrase-hint-handling | |
6451 | (not (equal allout-passphrase-hint-handling 'disabled))) | |
6452 | (setq new-hint | |
6453 | (read-from-minibuffer "Passphrase hint to jog your memory: " | |
6454 | allout-passphrase-hint-string)) | |
6455 | (when (not (string= new-hint allout-passphrase-hint-string)) | |
6456 | (setq allout-passphrase-hint-string new-hint) | |
6457 | (allout-adjust-file-variable "allout-passphrase-hint-string" | |
6458 | allout-passphrase-hint-string))) | |
6459 | (when allout-passphrase-verifier-handling | |
6460 | (setq allout-passphrase-verifier-string new-verifier-string) | |
6461 | (allout-adjust-file-variable "allout-passphrase-verifier-string" | |
6462 | allout-passphrase-verifier-string)) | |
6463 | ) | |
d82979ea EZ |
6464 | ) |
6465 | ) | |
0949617b EZ |
6466 | ;;;_ > allout-get-encryption-passphrase-verifier () |
6467 | (defun allout-get-encryption-passphrase-verifier () | |
6468 | "Return text of the encrypt passphrase verifier, unmassaged, or nil if none. | |
d82979ea | 6469 | |
4034b0e2 | 6470 | Derived from value of `allout-passphrase-verifier-string'." |
d82979ea | 6471 | |
0949617b EZ |
6472 | (let ((verifier-string (and (boundp 'allout-passphrase-verifier-string) |
6473 | allout-passphrase-verifier-string))) | |
d82979ea EZ |
6474 | (if verifier-string |
6475 | ;; Return it uncollapsed | |
0949617b | 6476 | (subst-char-in-string ?\C-a ?\n verifier-string)) |
d82979ea EZ |
6477 | ) |
6478 | ) | |
0949617b EZ |
6479 | ;;;_ > allout-verify-passphrase (key passphrase allout-buffer) |
6480 | (defun allout-verify-passphrase (key passphrase allout-buffer) | |
6481 | "True if passphrase successfully decrypts verifier, nil otherwise. | |
d82979ea | 6482 | |
0949617b | 6483 | \"Otherwise\" includes absence of passphrase verifier." |
d82979ea EZ |
6484 | (save-excursion |
6485 | (set-buffer allout-buffer) | |
0949617b EZ |
6486 | (and (boundp 'allout-passphrase-verifier-string) |
6487 | allout-passphrase-verifier-string | |
6488 | (allout-encrypt-string (allout-get-encryption-passphrase-verifier) | |
d82979ea | 6489 | 'decrypt allout-buffer 'symmetric |
48bd8440 | 6490 | key nil 0 0 'verifying passphrase) |
d82979ea EZ |
6491 | t))) |
6492 | ;;;_ > allout-next-topic-pending-encryption (&optional except-mark) | |
6493 | (defun allout-next-topic-pending-encryption (&optional except-mark) | |
6494 | "Return the point of the next topic pending encryption, or nil if none. | |
6495 | ||
6496 | EXCEPT-MARK identifies a point whose containing topics should be excluded | |
6497 | from encryption. This supports 'except-current mode of | |
6498 | `allout-encrypt-unencrypted-on-saves'. | |
6499 | ||
75bcb912 | 6500 | Such a topic has the `allout-topic-encryption-bullet' without an |
d82979ea EZ |
6501 | immediately following '*' that would mark the topic as being encrypted. It |
6502 | must also have content." | |
6503 | (let (done got content-beg) | |
2a1408fd DN |
6504 | (save-match-data |
6505 | (while (not done) | |
d82979ea | 6506 | |
2a1408fd DN |
6507 | (if (not (re-search-forward |
6508 | (format "\\(\\`\\|\n\\)%s *%s[^*]" | |
6509 | (regexp-quote allout-header-prefix) | |
6510 | (regexp-quote allout-topic-encryption-bullet)) | |
6511 | nil t)) | |
6512 | (setq got nil | |
6513 | done t) | |
6514 | (goto-char (setq got (match-beginning 0))) | |
6515 | (if (save-match-data (looking-at "\n")) | |
6516 | (forward-char 1)) | |
6517 | (setq got (point))) | |
6518 | ||
6519 | (cond ((not got) | |
6520 | (setq done t)) | |
6521 | ||
6522 | ((not (search-forward "\n")) | |
6523 | (setq got nil | |
6524 | done t)) | |
6525 | ||
6526 | ((eobp) | |
6527 | (setq got nil | |
6528 | done t)) | |
6529 | ||
6530 | (t | |
6531 | (setq content-beg (point)) | |
6532 | (backward-char 1) | |
6533 | (allout-end-of-subtree) | |
6534 | (if (or (<= (point) content-beg) | |
6535 | (and except-mark | |
6536 | (<= content-beg except-mark) | |
6537 | (>= (point) except-mark))) | |
6538 | ;; Continue looking | |
6539 | (setq got nil) | |
6540 | ;; Got it! | |
6541 | (setq done t))) | |
6542 | ) | |
6543 | ) | |
6544 | (if got | |
6545 | (goto-char got)) | |
d82979ea | 6546 | ) |
d82979ea EZ |
6547 | ) |
6548 | ) | |
6549 | ;;;_ > allout-encrypt-decrypted (&optional except-mark) | |
6550 | (defun allout-encrypt-decrypted (&optional except-mark) | |
6551 | "Encrypt topics pending encryption except those containing exemption point. | |
6552 | ||
6553 | EXCEPT-MARK identifies a point whose containing topics should be excluded | |
60c7bbd2 | 6554 | from encryption. This supports the `except-current' mode of |
d82979ea EZ |
6555 | `allout-encrypt-unencrypted-on-saves'. |
6556 | ||
6557 | If a topic that is currently being edited was encrypted, we return a list | |
6558 | containing the location of the topic and the location of the cursor just | |
6559 | before the topic was encrypted. This can be used, eg, to decrypt the topic | |
6560 | and exactly resituate the cursor if this is being done as part of a file | |
6561 | save. See `allout-encrypt-unencrypted-on-saves' for more info." | |
6562 | ||
6563 | (interactive "p") | |
2a1408fd DN |
6564 | (save-match-data |
6565 | (save-excursion | |
6566 | (let* ((current-mark (point-marker)) | |
6567 | (current-mark-position (marker-position current-mark)) | |
6568 | was-modified | |
6569 | bo-subtree | |
6570 | editing-topic editing-point) | |
6571 | (goto-char (point-min)) | |
6572 | (while (allout-next-topic-pending-encryption except-mark) | |
6573 | (setq was-modified (buffer-modified-p)) | |
6574 | (when (save-excursion | |
6575 | (and (boundp 'allout-encrypt-unencrypted-on-saves) | |
6576 | allout-encrypt-unencrypted-on-saves | |
6577 | (setq bo-subtree (re-search-forward "$")) | |
6578 | (not (allout-hidden-p)) | |
6579 | (>= current-mark (point)) | |
6580 | (allout-end-of-current-subtree) | |
6581 | (<= current-mark (point)))) | |
d82979ea EZ |
6582 | (setq editing-topic (point) |
6583 | ;; we had to wait for this 'til now so prior topics are | |
6584 | ;; encrypted, any relevant text shifts are in place: | |
96b83743 EZ |
6585 | editing-point (- current-mark-position |
6586 | (count-trailing-whitespace-region | |
6587 | bo-subtree current-mark-position)))) | |
2a1408fd DN |
6588 | (allout-toggle-subtree-encryption) |
6589 | (if (not was-modified) | |
6590 | (set-buffer-modified-p nil)) | |
6591 | ) | |
d82979ea EZ |
6592 | (if (not was-modified) |
6593 | (set-buffer-modified-p nil)) | |
2a1408fd | 6594 | (if editing-topic (list editing-topic editing-point)) |
d82979ea | 6595 | ) |
d82979ea EZ |
6596 | ) |
6597 | ) | |
6598 | ) | |
6599 | ||
6600 | ;;;_ #9 miscellaneous | |
d8350998 CY |
6601 | ;;;_ : Mode: |
6602 | ;;;_ > outlineify-sticky () | |
19b84ba3 | 6603 | ;; outlinify-sticky is correct spelling; provide this alias for sticklers: |
0949617b | 6604 | ;;;###autoload |
19b84ba3 | 6605 | (defalias 'outlinify-sticky 'outlineify-sticky) |
0949617b | 6606 | ;;;###autoload |
c567ac01 | 6607 | (defun outlineify-sticky (&optional arg) |
a0776d6b | 6608 | "Activate outline mode and establish file var so it is started subsequently. |
1977b8f6 | 6609 | |
fd5359c6 | 6610 | See doc-string for `allout-layout' and `allout-init' for details on |
19b84ba3 RS |
6611 | setup for auto-startup." |
6612 | ||
6613 | (interactive "P") | |
6614 | ||
fd5359c6 | 6615 | (allout-mode t) |
19b84ba3 RS |
6616 | |
6617 | (save-excursion | |
6618 | (goto-char (point-min)) | |
ede4ac6a | 6619 | (if (allout-goto-prefix) |
19b84ba3 | 6620 | t |
fd5359c6 | 6621 | (allout-open-topic 2) |
60c7bbd2 | 6622 | (insert (concat "Dummy outline topic header -- see" |
d82979ea EZ |
6623 | "`allout-mode' docstring: `^Hm'.")) |
6624 | (allout-adjust-file-variable | |
4034b0e2 | 6625 | "allout-layout" (or allout-layout '(-1 : 0)))))) |
d8350998 | 6626 | ;;;_ > allout-file-vars-section-data () |
d82979ea EZ |
6627 | (defun allout-file-vars-section-data () |
6628 | "Return data identifying the file-vars section, or nil if none. | |
6629 | ||
60c7bbd2 | 6630 | Returns a list of the form (BEGINNING-POINT PREFIX-STRING SUFFIX-STRING)." |
d82979ea EZ |
6631 | ;; minimally gleaned from emacs 21.4 files.el hack-local-variables function. |
6632 | (let (beg prefix suffix) | |
6633 | (save-excursion | |
1977b8f6 | 6634 | (goto-char (point-max)) |
d82979ea EZ |
6635 | (search-backward "\n\^L" (max (- (point-max) 3000) (point-min)) 'move) |
6636 | (if (let ((case-fold-search t)) | |
6637 | (not (search-forward "Local Variables:" nil t))) | |
6638 | nil | |
6639 | (setq beg (- (point) 16)) | |
6640 | (setq suffix (buffer-substring-no-properties | |
6641 | (point) | |
96b83743 | 6642 | (progn (if (search-forward "\n" nil t) |
d82979ea EZ |
6643 | (forward-char -1)) |
6644 | (point)))) | |
6645 | (setq prefix (buffer-substring-no-properties | |
96b83743 | 6646 | (progn (if (search-backward "\n" nil t) |
d82979ea EZ |
6647 | (forward-char 1)) |
6648 | (point)) | |
6649 | beg)) | |
6650 | (list beg prefix suffix)) | |
6651 | ) | |
6652 | ) | |
6653 | ) | |
d8350998 | 6654 | ;;;_ > allout-adjust-file-variable (varname value) |
d82979ea | 6655 | (defun allout-adjust-file-variable (varname value) |
a0a022d5 | 6656 | "Adjust the setting of an Emacs file variable named VARNAME to VALUE. |
d82979ea EZ |
6657 | |
6658 | This activity is inhibited if either `enable-local-variables' | |
6659 | `allout-enable-file-variable-adjustment' are nil. | |
6660 | ||
6661 | When enabled, an entry for the variable is created if not already present, | |
6662 | or changed if established with a different value. The section for the file | |
6663 | variables, itself, is created if not already present. When created, the | |
c07583cd | 6664 | section lines (including the section line) exist as second-level topics in |
d82979ea EZ |
6665 | a top-level topic at the end of the file. |
6666 | ||
a0a022d5 | 6667 | `enable-local-variables' must be true for any of this to happen." |
d82979ea EZ |
6668 | (if (not (and enable-local-variables |
6669 | allout-enable-file-variable-adjustment)) | |
6670 | nil | |
6671 | (save-excursion | |
0458fa80 EZ |
6672 | (let ((inhibit-field-text-motion t) |
6673 | (section-data (allout-file-vars-section-data)) | |
d82979ea EZ |
6674 | beg prefix suffix) |
6675 | (if section-data | |
6676 | (setq beg (car section-data) | |
6677 | prefix (cadr section-data) | |
6678 | suffix (car (cddr section-data))) | |
6679 | ;; create the section | |
6680 | (goto-char (point-max)) | |
6681 | (open-line 1) | |
6682 | (allout-open-topic 0) | |
6683 | (end-of-line) | |
6684 | (insert "Local emacs vars.\n") | |
6685 | (allout-open-topic 1) | |
6686 | (setq beg (point) | |
6687 | suffix "" | |
6688 | prefix (buffer-substring-no-properties (progn | |
6689 | (beginning-of-line) | |
6690 | (point)) | |
6691 | beg)) | |
6692 | (goto-char beg) | |
6693 | (insert "Local variables:\n") | |
6694 | (allout-open-topic 0) | |
6695 | (insert "End:\n") | |
6696 | ) | |
6697 | ;; look for existing entry or create one, leaving point for insertion | |
6698 | ;; of new value: | |
6699 | (goto-char beg) | |
6700 | (allout-show-to-offshoot) | |
6701 | (if (search-forward (concat "\n" prefix varname ":") nil t) | |
6702 | (let* ((value-beg (point)) | |
96b83743 | 6703 | (line-end (progn (if (search-forward "\n" nil t) |
d82979ea EZ |
6704 | (forward-char -1)) |
6705 | (point))) | |
6706 | (value-end (- line-end (length suffix)))) | |
6707 | (if (> value-end value-beg) | |
6708 | (delete-region value-beg value-end))) | |
6709 | (end-of-line) | |
6710 | (open-line 1) | |
6711 | (forward-line 1) | |
6712 | (insert (concat prefix varname ":"))) | |
6713 | (insert (format " %S%s" value suffix)) | |
6714 | ) | |
6715 | ) | |
6716 | ) | |
6717 | ) | |
d8350998 CY |
6718 | ;;;_ > allout-get-configvar-values (varname) |
6719 | (defun allout-get-configvar-values (configvar-name) | |
6720 | "Return a list of values of the symbols in list bound to CONFIGVAR-NAME. | |
6721 | ||
6722 | The user is prompted for removal of symbols that are unbound, and they | |
6723 | otherwise are ignored. | |
6724 | ||
6725 | CONFIGVAR-NAME should be the name of the configuration variable, | |
6726 | not its value." | |
6727 | ||
6728 | (let ((configvar-value (symbol-value configvar-name)) | |
6729 | got) | |
6730 | (dolist (sym configvar-value) | |
6731 | (if (not (boundp sym)) | |
60c7bbd2 | 6732 | (if (yes-or-no-p (format "%s entry `%s' is unbound -- remove it? " |
d8350998 CY |
6733 | configvar-name sym)) |
6734 | (delq sym (symbol-value configvar-name))) | |
6735 | (push (symbol-value sym) got))) | |
6736 | (reverse got))) | |
6737 | ;;;_ : Topics: | |
6738 | ;;;_ > allout-mark-topic () | |
6739 | (defun allout-mark-topic () | |
6740 | "Put the region around topic currently containing point." | |
6741 | (interactive) | |
6742 | (let ((inhibit-field-text-motion t)) | |
6743 | (beginning-of-line)) | |
6744 | (allout-goto-prefix-doublechecked) | |
6745 | (push-mark (point)) | |
6746 | (allout-end-of-current-subtree) | |
6747 | (exchange-point-and-mark)) | |
6748 | ;;;_ : UI: | |
6749 | ;;;_ > solicit-char-in-string (prompt string &optional do-defaulting) | |
1977b8f6 | 6750 | (defun solicit-char-in-string (prompt string &optional do-defaulting) |
c567ac01 | 6751 | "Solicit (with first arg PROMPT) choice of a character from string STRING. |
1977b8f6 | 6752 | |
c567ac01 | 6753 | Optional arg DO-DEFAULTING indicates to accept empty input (CR)." |
1977b8f6 RS |
6754 | |
6755 | (let ((new-prompt prompt) | |
6756 | got) | |
6757 | ||
6758 | (while (not got) | |
6759 | (message "%s" new-prompt) | |
6760 | ||
6761 | ;; We do our own reading here, so we can circumvent, eg, special | |
9179616f | 6762 | ;; treatment for `?' character. (Oughta use minibuffer keymap instead.) |
1977b8f6 | 6763 | (setq got |
c567ac01 | 6764 | (char-to-string (let ((cursor-in-echo-area nil)) (read-char)))) |
1977b8f6 | 6765 | |
9179616f DL |
6766 | (setq got |
6767 | (cond ((string-match (regexp-quote got) string) got) | |
6768 | ((and do-defaulting (string= got "\r")) | |
6769 | ;; Return empty string to default: | |
6770 | "") | |
6771 | ((string= got "\C-g") (signal 'quit nil)) | |
6772 | (t | |
6773 | (setq new-prompt (concat prompt | |
6774 | got | |
6775 | " ...pick from: " | |
6776 | string | |
6777 | "")) | |
6778 | nil)))) | |
60c7bbd2 | 6779 | ;; got something out of loop -- return it: |
9179616f | 6780 | got) |
1977b8f6 | 6781 | ) |
d8350998 CY |
6782 | ;;;_ : Strings: |
6783 | ;;;_ > regexp-sans-escapes (string) | |
c567ac01 RS |
6784 | (defun regexp-sans-escapes (regexp &optional successive-backslashes) |
6785 | "Return a copy of REGEXP with all character escapes stripped out. | |
19b84ba3 | 6786 | |
60c7bbd2 | 6787 | Representations of actual backslashes -- '\\\\\\\\' -- are left as a |
c567ac01 RS |
6788 | single backslash. |
6789 | ||
d82979ea | 6790 | Optional arg SUCCESSIVE-BACKSLASHES is used internally for recursion." |
c567ac01 RS |
6791 | |
6792 | (if (string= regexp "") | |
6793 | "" | |
6794 | ;; Set successive-backslashes to number if current char is | |
6795 | ;; backslash, or else to nil: | |
6796 | (setq successive-backslashes | |
6797 | (if (= (aref regexp 0) ?\\) | |
6798 | (if successive-backslashes (1+ successive-backslashes) 1) | |
6799 | nil)) | |
6800 | (if (or (not successive-backslashes) (= 2 successive-backslashes)) | |
6801 | ;; Include first char: | |
6802 | (concat (substring regexp 0 1) | |
6803 | (regexp-sans-escapes (substring regexp 1))) | |
6804 | ;; Exclude first char, but maintain count: | |
6805 | (regexp-sans-escapes (substring regexp 1) successive-backslashes)))) | |
d8350998 | 6806 | ;;;_ > count-trailing-whitespace-region (beg end) |
96b83743 EZ |
6807 | (defun count-trailing-whitespace-region (beg end) |
6808 | "Return number of trailing whitespace chars between BEG and END. | |
6809 | ||
6810 | If BEG is bigger than END we return 0." | |
6811 | (if (> beg end) | |
6812 | 0 | |
2a1408fd DN |
6813 | (save-match-data |
6814 | (save-excursion | |
6815 | (goto-char beg) | |
6816 | (let ((count 0)) | |
6817 | (while (re-search-forward "[ ][ ]*$" end t) | |
6818 | (goto-char (1+ (match-beginning 2))) | |
6819 | (setq count (1+ count))) | |
6820 | count))))) | |
d8350998 CY |
6821 | ;;;_ > allout-format-quote (string) |
6822 | (defun allout-format-quote (string) | |
6823 | "Return a copy of string with all \"%\" characters doubled." | |
6824 | (apply 'concat | |
6825 | (mapcar (lambda (char) (if (= char ?%) "%%" (char-to-string char))) | |
6826 | string))) | |
6827 | ;;;_ : lists | |
6828 | ;;;_ > allout-flatten (list) | |
6829 | (defun allout-flatten (list) | |
6830 | "Return a list of all atoms in list." | |
6831 | ;; classic. | |
6832 | (cond ((null list) nil) | |
8a559d4e EZ |
6833 | ((atom (car list)) (cons (car list) (allout-flatten (cdr list)))) |
6834 | (t (append (allout-flatten (car list)) (allout-flatten (cdr list)))))) | |
75bcb912 | 6835 | ;;;_ : Compatibility: |
d8350998 | 6836 | ;;;_ > allout-mark-marker to accommodate divergent emacsen: |
96b83743 EZ |
6837 | (defun allout-mark-marker (&optional force buffer) |
6838 | "Accommodate the different signature for `mark-marker' across Emacsen. | |
6839 | ||
6840 | XEmacs takes two optional args, while mainline GNU Emacs does not, | |
6841 | so pass them along when appropriate." | |
6842 | (if (featurep 'xemacs) | |
6843 | (apply 'mark-marker force buffer) | |
6844 | (mark-marker))) | |
d8350998 | 6845 | ;;;_ > subst-char-in-string if necessary |
d82979ea EZ |
6846 | (if (not (fboundp 'subst-char-in-string)) |
6847 | (defun subst-char-in-string (fromchar tochar string &optional inplace) | |
6848 | "Replace FROMCHAR with TOCHAR in STRING each time it occurs. | |
6849 | Unless optional argument INPLACE is non-nil, return a new string." | |
6850 | (let ((i (length string)) | |
6851 | (newstr (if inplace string (copy-sequence string)))) | |
6852 | (while (> i 0) | |
6853 | (setq i (1- i)) | |
6854 | (if (eq (aref newstr i) fromchar) | |
6855 | (aset newstr i tochar))) | |
6856 | newstr))) | |
d8350998 | 6857 | ;;;_ > wholenump if necessary |
96b83743 EZ |
6858 | (if (not (fboundp 'wholenump)) |
6859 | (defalias 'wholenump 'natnump)) | |
d8350998 | 6860 | ;;;_ > remove-overlays if necessary |
96b83743 EZ |
6861 | (if (not (fboundp 'remove-overlays)) |
6862 | (defun remove-overlays (&optional beg end name val) | |
6863 | "Clear BEG and END of overlays whose property NAME has value VAL. | |
6864 | Overlays might be moved and/or split. | |
6865 | BEG and END default respectively to the beginning and end of buffer." | |
6866 | (unless beg (setq beg (point-min))) | |
6867 | (unless end (setq end (point-max))) | |
6868 | (if (< end beg) | |
6869 | (setq beg (prog1 end (setq end beg)))) | |
6870 | (save-excursion | |
6871 | (dolist (o (overlays-in beg end)) | |
6872 | (when (eq (overlay-get o name) val) | |
6873 | ;; Either push this overlay outside beg...end | |
6874 | ;; or split it to exclude beg...end | |
6875 | ;; or delete it entirely (if it is contained in beg...end). | |
6876 | (if (< (overlay-start o) beg) | |
6877 | (if (> (overlay-end o) end) | |
6878 | (progn | |
6879 | (move-overlay (copy-overlay o) | |
6880 | (overlay-start o) beg) | |
6881 | (move-overlay o end (overlay-end o))) | |
6882 | (move-overlay o (overlay-start o) beg)) | |
6883 | (if (> (overlay-end o) end) | |
6884 | (move-overlay o end (overlay-end o)) | |
6885 | (delete-overlay o))))))) | |
6886 | ) | |
60c7bbd2 | 6887 | ;;;_ > copy-overlay if necessary -- xemacs ~ 21.4 |
96b83743 EZ |
6888 | (if (not (fboundp 'copy-overlay)) |
6889 | (defun copy-overlay (o) | |
6890 | "Return a copy of overlay O." | |
6891 | (let ((o1 (make-overlay (overlay-start o) (overlay-end o) | |
6892 | ;; FIXME: there's no easy way to find the | |
6893 | ;; insertion-type of the two markers. | |
6894 | (overlay-buffer o))) | |
6895 | (props (overlay-properties o))) | |
6896 | (while props | |
6897 | (overlay-put o1 (pop props) (pop props))) | |
6898 | o1))) | |
60c7bbd2 | 6899 | ;;;_ > add-to-invisibility-spec if necessary -- xemacs ~ 21.4 |
96b83743 EZ |
6900 | (if (not (fboundp 'add-to-invisibility-spec)) |
6901 | (defun add-to-invisibility-spec (element) | |
6902 | "Add ELEMENT to `buffer-invisibility-spec'. | |
6903 | See documentation for `buffer-invisibility-spec' for the kind of elements | |
6904 | that can be added." | |
6905 | (if (eq buffer-invisibility-spec t) | |
6906 | (setq buffer-invisibility-spec (list t))) | |
6907 | (setq buffer-invisibility-spec | |
6908 | (cons element buffer-invisibility-spec)))) | |
60c7bbd2 | 6909 | ;;;_ > remove-from-invisibility-spec if necessary -- xemacs ~ 21.4 |
96b83743 EZ |
6910 | (if (not (fboundp 'remove-from-invisibility-spec)) |
6911 | (defun remove-from-invisibility-spec (element) | |
6912 | "Remove ELEMENT from `buffer-invisibility-spec'." | |
6913 | (if (consp buffer-invisibility-spec) | |
6914 | (setq buffer-invisibility-spec (delete element | |
6915 | buffer-invisibility-spec))))) | |
60c7bbd2 | 6916 | ;;;_ > move-beginning-of-line if necessary -- older emacs, xemacs |
96b83743 EZ |
6917 | (if (not (fboundp 'move-beginning-of-line)) |
6918 | (defun move-beginning-of-line (arg) | |
6919 | "Move point to beginning of current line as displayed. | |
6920 | \(This disregards invisible newlines such as those | |
6921 | which are part of the text that an image rests on.) | |
6922 | ||
6923 | With argument ARG not nil or 1, move forward ARG - 1 lines first. | |
6924 | If point reaches the beginning or end of buffer, it stops there. | |
4034b0e2 | 6925 | To ignore intangibility, bind `inhibit-point-motion-hooks' to t." |
96b83743 EZ |
6926 | (interactive "p") |
6927 | (or arg (setq arg 1)) | |
6928 | (if (/= arg 1) | |
6929 | (condition-case nil (line-move (1- arg)) (error nil))) | |
6930 | ||
8570ce80 SM |
6931 | ;; Move to beginning-of-line, ignoring fields and invisibles. |
6932 | (skip-chars-backward "^\n") | |
2a1408fd DN |
6933 | (while (and (not (bobp)) |
6934 | (let ((prop | |
6935 | (get-char-property (1- (point)) 'invisible))) | |
6936 | (if (eq buffer-invisibility-spec t) | |
6937 | prop | |
6938 | (or (memq prop buffer-invisibility-spec) | |
6939 | (assq prop buffer-invisibility-spec))))) | |
8570ce80 SM |
6940 | (goto-char (if (featurep 'xemacs) |
6941 | (previous-property-change (point)) | |
6942 | (previous-char-property-change (point)))) | |
6943 | (skip-chars-backward "^\n")) | |
6944 | (vertical-motion 0)) | |
96b83743 | 6945 | ) |
60c7bbd2 | 6946 | ;;;_ > move-end-of-line if necessary -- older emacs, xemacs |
96b83743 EZ |
6947 | (if (not (fboundp 'move-end-of-line)) |
6948 | (defun move-end-of-line (arg) | |
6949 | "Move point to end of current line as displayed. | |
6950 | \(This disregards invisible newlines such as those | |
6951 | which are part of the text that an image rests on.) | |
6952 | ||
6953 | With argument ARG not nil or 1, move forward ARG - 1 lines first. | |
6954 | If point reaches the beginning or end of buffer, it stops there. | |
4034b0e2 | 6955 | To ignore intangibility, bind `inhibit-point-motion-hooks' to t." |
96b83743 EZ |
6956 | (interactive "p") |
6957 | (or arg (setq arg 1)) | |
8570ce80 | 6958 | (let (done) |
96b83743 EZ |
6959 | (while (not done) |
6960 | (let ((newpos | |
6961 | (save-excursion | |
6962 | (let ((goal-column 0)) | |
6963 | (and (condition-case nil | |
6964 | (or (line-move arg) t) | |
6965 | (error nil)) | |
6966 | (not (bobp)) | |
6967 | (progn | |
2a1408fd DN |
6968 | (while |
6969 | (and | |
6970 | (not (bobp)) | |
6971 | (let ((prop | |
6972 | (get-char-property (1- (point)) | |
6973 | 'invisible))) | |
6974 | (if (eq buffer-invisibility-spec t) | |
6975 | prop | |
6976 | (or (memq prop | |
6977 | buffer-invisibility-spec) | |
6978 | (assq prop | |
6979 | buffer-invisibility-spec))))) | |
8570ce80 SM |
6980 | (goto-char |
6981 | (previous-char-property-change (point)))) | |
96b83743 EZ |
6982 | (backward-char 1))) |
6983 | (point))))) | |
6984 | (goto-char newpos) | |
6985 | (if (and (> (point) newpos) | |
6986 | (eq (preceding-char) ?\n)) | |
6987 | (backward-char 1) | |
6988 | (if (and (> (point) newpos) (not (eobp)) | |
6989 | (not (eq (following-char) ?\n))) | |
6990 | ;; If we skipped something intangible | |
6991 | ;; and now we're not really at eol, | |
6992 | ;; keep going. | |
6993 | (setq arg 1) | |
8570ce80 | 6994 | (setq done t))))))) |
96b83743 | 6995 | ) |
96b83743 | 6996 | |
96b83743 | 6997 | ;;;_ #10 Unfinished |
fd5359c6 MR |
6998 | ;;;_ > allout-bullet-isearch (&optional bullet) |
6999 | (defun allout-bullet-isearch (&optional bullet) | |
c07583cd | 7000 | "Isearch (regexp) for topic with bullet BULLET." |
c567ac01 RS |
7001 | (interactive) |
7002 | (if (not bullet) | |
7003 | (setq bullet (solicit-char-in-string | |
7004 | "ISearch for topic with bullet: " | |
fd5359c6 | 7005 | (regexp-sans-escapes allout-bullets-string)))) |
353e2ef2 | 7006 | |
c567ac01 RS |
7007 | (let ((isearch-regexp t) |
7008 | (isearch-string (concat "^" | |
fd5359c6 | 7009 | allout-header-prefix |
c567ac01 RS |
7010 | "[ \t]*" |
7011 | bullet))) | |
7012 | (isearch-repeat 'forward) | |
7013 | (isearch-mode t))) | |
96b83743 | 7014 | |
60c7bbd2 | 7015 | ;;;_ #11 Unit tests -- this should be last item before "Provide" |
01fc9422 EZ |
7016 | ;;;_ > allout-run-unit-tests () |
7017 | (defun allout-run-unit-tests () | |
7018 | "Run the various allout unit tests." | |
7019 | (message "Running allout tests...") | |
7020 | (allout-test-resumptions) | |
7021 | (message "Running allout tests... Done.") | |
7022 | (sit-for .5)) | |
7023 | ;;;_ : test resumptions: | |
7024 | ;;;_ > allout-tests-obliterate-variable (name) | |
7025 | (defun allout-tests-obliterate-variable (name) | |
7026 | "Completely unbind variable with NAME." | |
7027 | (if (local-variable-p name) (kill-local-variable name)) | |
7028 | (while (boundp name) (makunbound name))) | |
7029 | ;;;_ > allout-test-resumptions () | |
7030 | (defvar allout-tests-globally-unbound nil | |
60c7bbd2 | 7031 | "Fodder for allout resumptions tests -- defvar just for byte compiler.") |
01fc9422 | 7032 | (defvar allout-tests-globally-true nil |
60c7bbd2 | 7033 | "Fodder for allout resumptions tests -- defvar just for byte compiler.") |
01fc9422 | 7034 | (defvar allout-tests-locally-true nil |
60c7bbd2 | 7035 | "Fodder for allout resumptions tests -- defvar just for byte compiler.") |
01fc9422 EZ |
7036 | (defun allout-test-resumptions () |
7037 | "Exercise allout resumptions." | |
7038 | ;; for each resumption case, we also test that the right local/global | |
7039 | ;; scopes are affected during resumption effects: | |
7040 | ||
7041 | ;; ensure that previously unbound variables return to the unbound state. | |
7042 | (with-temp-buffer | |
7043 | (allout-tests-obliterate-variable 'allout-tests-globally-unbound) | |
7044 | (allout-add-resumptions '(allout-tests-globally-unbound t)) | |
7045 | (assert (not (default-boundp 'allout-tests-globally-unbound))) | |
7046 | (assert (local-variable-p 'allout-tests-globally-unbound)) | |
7047 | (assert (boundp 'allout-tests-globally-unbound)) | |
7048 | (assert (equal allout-tests-globally-unbound t)) | |
7049 | (allout-do-resumptions) | |
7050 | (assert (not (local-variable-p 'allout-tests-globally-unbound))) | |
7051 | (assert (not (boundp 'allout-tests-globally-unbound)))) | |
7052 | ||
7053 | ;; ensure that variable with prior global value is resumed | |
7054 | (with-temp-buffer | |
7055 | (allout-tests-obliterate-variable 'allout-tests-globally-true) | |
7056 | (setq allout-tests-globally-true t) | |
7057 | (allout-add-resumptions '(allout-tests-globally-true nil)) | |
7058 | (assert (equal (default-value 'allout-tests-globally-true) t)) | |
7059 | (assert (local-variable-p 'allout-tests-globally-true)) | |
7060 | (assert (equal allout-tests-globally-true nil)) | |
7061 | (allout-do-resumptions) | |
7062 | (assert (not (local-variable-p 'allout-tests-globally-true))) | |
7063 | (assert (boundp 'allout-tests-globally-true)) | |
7064 | (assert (equal allout-tests-globally-true t))) | |
7065 | ||
7066 | ;; ensure that prior local value is resumed | |
7067 | (with-temp-buffer | |
7068 | (allout-tests-obliterate-variable 'allout-tests-locally-true) | |
7069 | (set (make-local-variable 'allout-tests-locally-true) t) | |
7070 | (assert (not (default-boundp 'allout-tests-locally-true)) | |
60c7bbd2 | 7071 | nil (concat "Test setup mistake -- variable supposed to" |
01fc9422 EZ |
7072 | " not have global binding, but it does.")) |
7073 | (assert (local-variable-p 'allout-tests-locally-true) | |
60c7bbd2 | 7074 | nil (concat "Test setup mistake -- variable supposed to have" |
01fc9422 EZ |
7075 | " local binding, but it lacks one.")) |
7076 | (allout-add-resumptions '(allout-tests-locally-true nil)) | |
7077 | (assert (not (default-boundp 'allout-tests-locally-true))) | |
7078 | (assert (local-variable-p 'allout-tests-locally-true)) | |
7079 | (assert (equal allout-tests-locally-true nil)) | |
7080 | (allout-do-resumptions) | |
7081 | (assert (boundp 'allout-tests-locally-true)) | |
7082 | (assert (local-variable-p 'allout-tests-locally-true)) | |
7083 | (assert (equal allout-tests-locally-true t)) | |
7084 | (assert (not (default-boundp 'allout-tests-locally-true)))) | |
7085 | ||
7086 | ;; ensure that last of multiple resumptions holds, for various scopes. | |
7087 | (with-temp-buffer | |
7088 | (allout-tests-obliterate-variable 'allout-tests-globally-unbound) | |
7089 | (allout-tests-obliterate-variable 'allout-tests-globally-true) | |
7090 | (setq allout-tests-globally-true t) | |
7091 | (allout-tests-obliterate-variable 'allout-tests-locally-true) | |
7092 | (set (make-local-variable 'allout-tests-locally-true) t) | |
7093 | (allout-add-resumptions '(allout-tests-globally-unbound t) | |
7094 | '(allout-tests-globally-true nil) | |
7095 | '(allout-tests-locally-true nil)) | |
7096 | (allout-add-resumptions '(allout-tests-globally-unbound 2) | |
7097 | '(allout-tests-globally-true 3) | |
7098 | '(allout-tests-locally-true 4)) | |
7099 | ;; reestablish many of the basic conditions are maintained after re-add: | |
7100 | (assert (not (default-boundp 'allout-tests-globally-unbound))) | |
7101 | (assert (local-variable-p 'allout-tests-globally-unbound)) | |
7102 | (assert (equal allout-tests-globally-unbound 2)) | |
7103 | (assert (default-boundp 'allout-tests-globally-true)) | |
7104 | (assert (local-variable-p 'allout-tests-globally-true)) | |
7105 | (assert (equal allout-tests-globally-true 3)) | |
7106 | (assert (not (default-boundp 'allout-tests-locally-true))) | |
7107 | (assert (local-variable-p 'allout-tests-locally-true)) | |
7108 | (assert (equal allout-tests-locally-true 4)) | |
7109 | (allout-do-resumptions) | |
7110 | (assert (not (local-variable-p 'allout-tests-globally-unbound))) | |
7111 | (assert (not (boundp 'allout-tests-globally-unbound))) | |
7112 | (assert (not (local-variable-p 'allout-tests-globally-true))) | |
7113 | (assert (boundp 'allout-tests-globally-true)) | |
7114 | (assert (equal allout-tests-globally-true t)) | |
7115 | (assert (boundp 'allout-tests-locally-true)) | |
7116 | (assert (local-variable-p 'allout-tests-locally-true)) | |
7117 | (assert (equal allout-tests-locally-true t)) | |
7118 | (assert (not (default-boundp 'allout-tests-locally-true)))) | |
7119 | ||
7120 | ;; ensure that deliberately unbinding registered variables doesn't foul things | |
7121 | (with-temp-buffer | |
7122 | (allout-tests-obliterate-variable 'allout-tests-globally-unbound) | |
7123 | (allout-tests-obliterate-variable 'allout-tests-globally-true) | |
7124 | (setq allout-tests-globally-true t) | |
7125 | (allout-tests-obliterate-variable 'allout-tests-locally-true) | |
7126 | (set (make-local-variable 'allout-tests-locally-true) t) | |
7127 | (allout-add-resumptions '(allout-tests-globally-unbound t) | |
7128 | '(allout-tests-globally-true nil) | |
7129 | '(allout-tests-locally-true nil)) | |
7130 | (allout-tests-obliterate-variable 'allout-tests-globally-unbound) | |
7131 | (allout-tests-obliterate-variable 'allout-tests-globally-true) | |
7132 | (allout-tests-obliterate-variable 'allout-tests-locally-true) | |
7133 | (allout-do-resumptions)) | |
7134 | ) | |
7135 | ;;;_ % Run unit tests if `allout-run-unit-tests-after-load' is true: | |
7136 | (when allout-run-unit-tests-on-load | |
7137 | (allout-run-unit-tests)) | |
7138 | ||
7139 | ;;;_ #12 Provide | |
96b83743 | 7140 | (provide 'allout) |
1977b8f6 RS |
7141 | |
7142 | ;;;_* Local emacs vars. | |
31468dcb SM |
7143 | ;; The following `allout-layout' local variable setting: |
7144 | ;; - closes all topics from the first topic to just before the third-to-last, | |
7145 | ;; - shows the children of the third to last (config vars) | |
7146 | ;; - and the second to last (code section), | |
7147 | ;; - and closes the last topic (this local-variables section). | |
7148 | ;;Local variables: | |
7149 | ;;allout-layout: (0 : -1 -1 0) | |
7150 | ;;End: | |
7151 | ||
7152 | ;; arch-tag: cf38fbc3-c044-450f-8bff-afed8ba5681c | |
6a05d05f | 7153 | ;;; allout.el ends here |