]> code.delx.au - gnu-emacs/blob - lisp/allout.el
Doc fixes.
[gnu-emacs] / lisp / allout.el
1 ;; allout.el - An extensive outline-mode for Emacs.
2 ;; Copyright (C) 1992, 1993, 1994 Free Software Foundation, Inc.
3
4 ;; Author: Ken Manheimer <klm@nist.gov>
5 ;; Maintainer: Ken Manheimer <klm@nist.gov>
6 ;; Created: Dec 1991 - first release to usenet
7 ;; Version: $Id: allout.el,v 3.39 1994/03/05 17:39:51 klm Exp klm $||
8 ;; Keywords: outline mode
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to
24 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
25
26 ;;; Note - the lines beginning with ';;;_' are outline topic headers.
27 ;;; Load this file (or 'eval-current-buffer') and revisit the
28 ;;; file to give it a whirl.
29
30 ;;;_* Provide
31 (provide 'outline)
32
33 ;;;_ + LCD Archive Entry
34
35 ;;;_ + Description
36 ;; A full-fledged outline mode, based on the original rudimentary
37 ;; GNU emacs outline functionality.
38 ;;
39 ;;Ken Manheimer 301 975-3539
40 ;;ken.manheimer@nist.gov FAX: 301 963-9137
41 ;;
42 ;;Computer Systems and Communications Division
43 ;;
44 ;; Nat'l Institute of Standards and Technology
45 ;; Technology A151
46 ;; Gaithersburg, MD 20899
47
48 ;;;_* User Customization variables
49
50 ;;;_ + Topic Header configuration
51
52 ;;;_ = outline-header-prefix
53 (defvar outline-header-prefix "."
54 "*Outline topic header lines are identified by a leading topic
55 header prefix, which mostly have the value of this var at their front.
56 \(Level 1 topics are exceptions. They consist of only a single
57 character, which is typically set to the outline-primary-bullet.")
58 (make-variable-buffer-local 'outline-header-prefix)
59
60 ;;;_ = outline-mode-leaders
61 (defvar outline-mode-leaders
62 '((emacs-lisp-mode . "\;\;\;_")
63 (lisp-mode . "\;\;\;_")
64 (awk-mode . "#")
65 (csh-mode . "#")
66 (sh-mode . "#")
67 (tcl-mode . "#")
68 (perl-mode . "#")
69 (c++-mode "//_")
70 (c-mode "/*_"))
71 "Respective outline-prefix leading strings per major modes. The
72 strings should begin with a comment string for the mode. Preferably,
73 they would have an extra character, eg an \"_\" underscore, to
74 distinguish the lead string from regular comments that start at bol.
75 \'#'-commented script modes, however, may need to use a bar \'#' in
76 order for the script magic number \'#!' to serve as the top-level
77 topic.")
78
79 ;;;_ = outline-primary-bullet
80 (defvar outline-primary-bullet "*"
81 "Outline topic header lines are identified by a leading topic header
82 prefix, which is concluded by bullets that includes the value of this
83 var and the respective outline-*-bullets-string vars.
84
85 The value of an asterisk ('*') provides for backwards compatability
86 with the original emacs outline mode. See outline-plain-bullets-string
87 and outline-distinctive-bullets-string for the range of available
88 bullets.")
89 (make-variable-buffer-local 'outline-primary-bullet)
90
91 ;;;_ = outline-plain-bullets-string
92 (defvar outline-plain-bullets-string (concat outline-primary-bullet
93 "+-:.;,")
94 "*The bullets normally used in outline topic prefixes. See
95 'outline-distinctive-bullets-string' for the other kind of
96 bullets.
97
98 DO NOT include the close-square-bracket, ']', as a bullet.
99
100 Outline mode has to be reactivated in order for changes to the value
101 of this var to take effect.")
102 (make-variable-buffer-local 'outline-plain-bullets-string)
103
104 ;;;_ = outline-distinctive-bullets-string
105 (defvar outline-distinctive-bullets-string "=>([{}&!?#%\"X@$~\\"
106 "*The bullets used for distinguishing outline topics. These
107 bullets are not offered among the regular rotation, and are not
108 changed when automatically rebulleting, as when shifting the
109 level of a topic. See 'outline-plain-bullets-string' for the
110 other kind of bullets.
111
112 DO NOT include the close-square-bracket, ']', among any bullets.
113
114 You must run 'set-outline-regexp' in order for changes
115 to the value of this var to effect outline-mode operation.")
116 (make-variable-buffer-local 'outline-distinctive-bullets-string)
117
118 ;;;_ = outline-old-style-prefixes
119 (defvar outline-old-style-prefixes nil
120 "*Non-nil restricts the topic creation and modification
121 functions to asterix-padded prefixes, so they look exactly
122 like the original emacs-outline style prefixes.
123
124 Whatever the setting of this variable, both old and new style prefixes
125 are always respected by the topic maneuvering functions.")
126 (make-variable-buffer-local 'outline-old-style-prefixes)
127
128 ;;;_ = outline-stylish-prefixes - new fangled topic prefixes
129 (defvar outline-stylish-prefixes t
130 "*Non-nil allows the topic creation and modification
131 functions to vary the topic bullet char (the char that marks
132 the topic depth) just preceding the start of the topic text)
133 according to level. Otherwise, only asterisks ('*') and
134 distinctive bullets are used.
135
136 This is how an outline can look with stylish prefixes:
137
138 * Top level
139 .* A topic
140 . + One level 3 subtopic
141 . . One level 4 subtopic
142 . + Another level 3 subtopic
143 . . A level 4 subtopic
144 . #2 A distinguished, numbered level 4 subtopic
145 . ! A distinguished ('!') level 4 subtopic
146 . #4 Another numbered level 4 subtopic
147
148 This would be an outline with stylish prefixes inhibited:
149
150 * Top level
151 .* A topic
152 .! A distinctive (but measly) subtopic
153 . * A sub-subtopic - no bullets from outline-plain-bullets-string but '*'
154
155 Stylish and constant prefixes (as well as old-style prefixes) are
156 always respected by the topic maneuvering functions, regardless of
157 this variable setting.
158
159 The setting of this var is not relevant when outline-old-style-prefixes
160 is non-nil.")
161 (make-variable-buffer-local 'outline-stylish-prefixes)
162
163 ;;;_ = outline-numbered-bullet
164 (defvar outline-numbered-bullet "#"
165 "*Topics having this bullet have automatic maintainence of a sibling
166 sequence number tacked on just after the bullet. Conventionally set
167 to \"#\", you can set it to a bullet of your choice. A nil value
168 disables numbering maintainence.")
169 (make-variable-buffer-local 'outline-numbered-bullet)
170
171 ;;;_ = outline-file-xref-bullet
172 (defvar outline-file-xref-bullet "@"
173 "*Set this var to the bullet you want to use for file cross-references.
174 Set it 'nil' if you want to inhibit this capability.")
175
176 ;;;_ + LaTeX formatting
177 ;;;_ - outline-number-pages
178 (defvar outline-number-pages nil
179 "*Non-nil turns on page numbering for LaTeX formatting of an outline.")
180 ;;;_ - outline-label-style
181 (defvar outline-label-style "\\large\\bf"
182 "*Font and size of labels for LaTeX formatting of an outline.")
183 ;;;_ - outline-head-line-style
184 (defvar outline-head-line-style "\\large\\sl "
185 "*Font and size of entries for LaTeX formatting of an outline.")
186 ;;;_ - outline-body-line-style
187 (defvar outline-body-line-style " "
188 "*Font and size of entries for LaTeX formatting of an outline.")
189 ;;;_ - outline-title-style
190 (defvar outline-title-style "\\Large\\bf"
191 "*Font and size of titles for LaTeX formatting of an outline.")
192 ;;;_ - outline-title
193 (defvar outline-title '(or buffer-file-name (current-buffer-name))
194 "*Expression to be evaluated to determine the title for LaTeX
195 formatted copy.")
196 ;;;_ - outline-line-skip
197 (defvar outline-line-skip ".05cm"
198 "*Space between lines for LaTeX formatting of an outline.")
199 ;;;_ - outline-indent
200 (defvar outline-indent ".3cm"
201 "*LaTeX formatted depth-indent spacing.")
202
203 ;;;_ + Miscellaneous customization
204
205 ;;;_ = outline-keybindings-list
206 ;;; You have to reactivate outline-mode - '(outline-mode t)' - to
207 ;;; institute changes to this var.
208 (defvar outline-keybindings-list ()
209 "*List of outline-mode key / function bindings, they will be locally
210 bound on the outline-mode-map. The keys will be prefixed by
211 outline-command-prefix unless the cell contains a third, no-nil
212 element, in which case the initial string will be used as is.")
213 (setq outline-keybindings-list
214 '(
215 ; Motion commands:
216 ("?t" outline-latexify-exposed)
217 ("\C-n" outline-next-visible-heading)
218 ("\C-p" outline-previous-visible-heading)
219 ("\C-u" outline-up-current-level)
220 ("\C-f" outline-forward-current-level)
221 ("\C-b" outline-backward-current-level)
222 ("\C-a" outline-beginning-of-current-entry)
223 ("\C-e" outline-end-of-current-entry)
224 ;;("\C-n" outline-next-line-or-topic)
225 ;;("\C-p" outline-previous-line-or-topic)
226 ; Exposure commands:
227 ("\C-i" outline-show-children)
228 ("\C-s" outline-show-current-subtree)
229 ("\C-h" outline-hide-current-subtree)
230 ("\C-o" outline-show-current-entry)
231 ("!" outline-show-all)
232 ; Alteration commands:
233 (" " outline-open-sibtopic)
234 ("." outline-open-subtopic)
235 ("," outline-open-supertopic)
236 ("'" outline-shift-in)
237 (">" outline-shift-in)
238 ("<" outline-shift-out)
239 ("\C-m" outline-rebullet-topic)
240 ("b" outline-rebullet-current-heading)
241 ("#" outline-number-siblings)
242 ("\C-k" outline-kill-line t)
243 ("\C-y" outline-yank t)
244 ("\M-y" outline-yank-pop t)
245 ("\C-k" outline-kill-topic)
246 ; Miscellaneous commands:
247 ("\C-@" outline-mark-topic)
248 ("@" outline-resolve-xref)
249 ("?c" outline-copy-exposed)))
250
251 ;;;_ = outline-command-prefix
252 (defvar outline-command-prefix "\C-c"
253 "*Key sequence to be used as prefix for outline mode command key bindings.")
254
255 ;;;_ = outline-enwrap-isearch-mode - any non-nil value fine in Emacs 19.
256 (defvar outline-enwrap-isearch-mode "isearch-mode.el"
257 "*Set this var non-nil if you're using Emacs 19 or Lucid emacs, or
258 you're using Dan LaLiberte's 'isearch-mode' stuff. (If you have
259 LaLiberte's package available but its' typically loaded, set the
260 var to the name of the text, not the byte-compiled, load file.)
261
262 The new isearch is required if you want isearches to expose hidden
263 stuff encountered in the course of a search, and to reconceal it if
264 you go past.
265
266 Set the var nil if you're not using emacs 19 and you don't have the
267 elisp-archive package, or if want to disable this feature.")
268
269 ;;;_ = outline-use-hanging-indents
270 (defvar outline-use-hanging-indents t
271 "*When non-nil, the default auto-indent for text of topic bodies is
272 set to be even with the leading text of the header. Ie, it is
273 indented to be just past the header prefix. This is relevant mostly
274 for use with indented-text-mode, or other situations where auto-fill
275 occurs.
276
277 [This feature no longer depends in any way on the 'filladapt.el'
278 lisp-archive package.]")
279 (make-variable-buffer-local 'outline-use-hanging-indents)
280
281 ;;;_ = outline-reindent-bodies
282 (defvar outline-reindent-bodies outline-use-hanging-indents
283 "*Set this var non-nil if you want topic depth adjustments to
284 reindent hanging bodies so they remain even with the beginning
285 of heading text.")
286 (make-variable-buffer-local 'outline-reindent-bodies)
287
288 ;;;_ = outline-sticky-header-motion
289 (defvar outline-sticky-header-motion t
290 "*Non-nil means that outline-{next,previous}-line or topic, bound
291 to keys typically dedicated to {next,previous}-line, will move by
292 topics when the cursor is moving from the first character of topic-
293 header text. You can always move the cursor off of that first-char
294 \"hot spot\" when you want to do regular next/previous line motions.")
295 (make-variable-buffer-local 'outline-sticky-header-motion)
296
297 ;;;_ = outline-inhibit-protection
298 (defvar outline-inhibit-protection nil
299 "*Outline mode uses emacs change-triggered functions (not available
300 before emacs 19) to detect unruly changes to concealed regions. Set
301 this var non-nil to disable the protection, potentially increasing
302 text-entry responsiveness a bit.
303
304 The effect of this var occurs at outline-mode activation, so you may
305 have to deactivate and then reactivate if you want to toggle the
306 behavior.")
307
308 ;;;_* Code - no user customizations below.
309
310 ;;;_ #1 Outline Format, Internal Configuration, and Mode Activation
311 ;;;_ - Topic header format
312 ;;;_ = outline-regexp
313 (defvar outline-regexp ""
314 "*Regular expression to match the beginning of a heading line.
315 Any line whose beginning matches this regexp is considered a
316 heading. This var is set according to the user configuration vars
317 by set-outline-regexp.")
318 (make-variable-buffer-local 'outline-regexp)
319 ;;;_ = outline-bullets-string
320 (defvar outline-bullets-string ""
321 "A string dictating the valid set of outline topic bullets. This
322 var should *not* be set by the user - it is set by 'set-outline-regexp',
323 and is composed from the elements of 'outline-plain-bullets-string'
324 and 'outline-distinctive-bullets-string'.")
325 (make-variable-buffer-local 'outline-bullets-string)
326 ;;;_ = outline-bullets-string-len
327 (defvar outline-bullets-string-len 0
328 "Length of current buffers' outline-plain-bullets-string.")
329 (make-variable-buffer-local 'outline-bullets-string-len)
330 ;;;_ = outline-line-boundary-regexp
331 (defvar outline-line-boundary-regexp ()
332 "outline-regexp with outline-style beginning of line anchor (ie,
333 C-j, *or* C-m, for prefixes of hidden topics). This is properly
334 set when outline-regexp is produced by 'set-outline-regexp', so
335 that (match-beginning 2) and (match-end 2) delimit the prefix.")
336 (make-variable-buffer-local 'outline-line-boundary-regexp)
337 ;;;_ = outline-bob-regexp
338 (defvar outline-bob-regexp ()
339 "Like outline-line-boundary-regexp, this is an outline-regexp for
340 outline headers at the beginning of the buffer. (match-beginning 2)
341 and (match-end 2) delimit the prefix.")
342 (make-variable-buffer-local 'outline-bob-regexp)
343 ;;;_ = current-bullet
344 (defvar current-bullet nil
345 "Variable local to outline-rebullet-heading,but referenced by
346 outline-make-topic-prefix, also. Should be resolved with explicitly
347 parameterized communication between the two, if suitable.")
348 ;;;_ = outline-header-subtraction
349 (defvar outline-header-subtraction (1- (length outline-header-prefix))
350 "Length of outline-header prefix to subtract when computing depth
351 from prefix length.")
352 (make-variable-buffer-local 'outline-header-subtraction)
353 ;;;_ = outline-plain-bullets-string-len
354 (defvar outline-plain-bullets-string-len (length outline-plain-bullets-string)
355 "Length of outline-plain-bullets-string, updated by set-outline-regexp.")
356 (make-variable-buffer-local 'outline-plain-bullets-string-len)
357
358
359 ;;;_ > outline-reset-header-lead (header-lead)
360 (defun outline-reset-header-lead (header-lead)
361 "*Reset the leading string used to identify topic headers."
362 (interactive "sNew lead string: ")
363 (setq outline-header-prefix header-lead)
364 (setq outline-header-subtraction (1- (length outline-header-prefix)))
365 (set-outline-regexp))
366 ;;;_ > outline-lead-with-comment-string (header-lead)
367 (defun outline-lead-with-comment-string (&optional header-lead)
368 "*Set the topic-header leading string to specified string. Useful
369 when for encapsulating outline structure in programming language
370 comments. Returns the leading string."
371
372 (interactive "P")
373 (if (not (stringp header-lead))
374 (setq header-lead (read-string
375 "String prefix for topic headers: ")))
376 (setq outline-reindent-bodies nil)
377 (outline-reset-header-lead header-lead)
378 header-lead)
379 ;;;_ > set-outline-regexp ()
380 (defun set-outline-regexp ()
381 "Generate proper topic-header regexp form for outline functions, from
382 outline-plain-bullets-string and outline-distinctive-bullets-string."
383
384 (interactive)
385 ;; Derive outline-bullets-string from user configured components:
386 (setq outline-bullets-string "")
387 (let ((strings (list 'outline-plain-bullets-string
388 'outline-distinctive-bullets-string))
389 cur-string
390 cur-len
391 cur-char
392 cur-char-string
393 index
394 new-string)
395 (while strings
396 (setq new-string "") (setq index 0)
397 (setq cur-len (length (setq cur-string (symbol-value (car strings)))))
398 (while (< index cur-len)
399 (setq cur-char (aref cur-string index))
400 (setq outline-bullets-string
401 (concat outline-bullets-string
402 (cond
403 ; Single dash would denote a
404 ; sequence, repeated denotes
405 ; a dash:
406 ((eq cur-char ?-) "--")
407 ; literal close-square-bracket
408 ; doesn't work right in the
409 ; expr, exclude it:
410 ((eq cur-char ?\]) "")
411 (t (regexp-quote (char-to-string cur-char))))))
412 (setq index (1+ index)))
413 (setq strings (cdr strings)))
414 )
415 ;; Derive next for repeated use in outline-pending-bullet:
416 (setq outline-plain-bullets-string-len (length outline-plain-bullets-string))
417 (setq outline-header-subtraction (1- (length outline-header-prefix)))
418 ;; Produce the new outline-regexp:
419 (setq outline-regexp (concat "\\(\\"
420 outline-header-prefix
421 "[ \t]*["
422 outline-bullets-string
423 "]\\)\\|\\"
424 outline-primary-bullet
425 "+\\|\^l"))
426 (setq outline-line-boundary-regexp
427 (concat "\\([\n\r]\\)\\(" outline-regexp "\\)"))
428 (setq outline-bob-regexp
429 (concat "\\(\\`\\)\\(" outline-regexp "\\)"))
430 )
431 ;;;_ - Key bindings
432 ;;;_ = outline-prior-bindings
433 (defvar outline-prior-bindings nil
434 "Variable for use in V18, with outline-added-bindings, for
435 resurrecting, on mode deactivation, bindings that existed before
436 activation.")
437 ;;;_ = outline-added-bindings
438 (defvar outline-added-bindings nil
439 "Variable for use in V18, with outline-prior-bindings, for
440 resurrecting, on mode deactivation, bindings that existed before
441 activation.")
442 ;;;_ - Mode-Specific Variable Maintenance Utilities
443 ;;;_ = outline-mode-prior-settings
444 (defvar outline-mode-prior-settings nil
445 "For internal use by outline mode, registers settings to be resumed
446 on mode deactivation.")
447 (make-variable-buffer-local 'outline-mode-prior-settings)
448 ;;;_ > outline-resumptions (name &optional value)
449 (defun outline-resumptions (name &optional value)
450
451 "Registers information for later reference, or performs resumption of
452 outline-mode specific values. First arg is NAME of variable affected.
453 optional second arg is list containing outline-mode-specific VALUE to
454 be imposed on named variable, and to be registered. (It's a list so you
455 can specify registrations of null values.) If no value is specified,
456 the registered value is returned (encapsulated in the list, so the
457 caller can distinguish nil vs no value), and the registration is popped
458 from the list."
459
460 (let ((on-list (assq name outline-mode-prior-settings))
461 prior-capsule ; By 'capsule' i mean a list
462 ; containing a value, so we can
463 ; distinguish nil from no value.
464 )
465
466 (if value
467
468 ;; Registering:
469 (progn
470 (if on-list
471 nil ; Already preserved prior value - don't mess with it.
472 ;; Register the old value, or nil if previously unbound:
473 (setq outline-mode-prior-settings
474 (cons (list name
475 (if (boundp name) (list (symbol-value name))))
476 outline-mode-prior-settings)))
477 ; And impose the new value, locally:
478 (progn (make-local-variable name)
479 (set name (car value))))
480
481 ;; Relinquishing:
482 (if (not on-list)
483
484 ;; Oops, not registered - leave it be:
485 nil
486
487 ;; Some registration:
488 ; reestablish it:
489 (setq prior-capsule (car (cdr on-list)))
490 (if prior-capsule
491 (set name (car prior-capsule)) ; Some prior value - reestablish it.
492 (makunbound name)) ; Previously unbound - demolish var.
493 ; Remove registration:
494 (let (rebuild)
495 (while outline-mode-prior-settings
496 (if (not (eq (car outline-mode-prior-settings)
497 on-list))
498 (setq rebuild
499 (cons (car outline-mode-prior-settings)
500 rebuild)))
501 (setq outline-mode-prior-settings
502 (cdr outline-mode-prior-settings)))
503 (setq outline-mode-prior-settings rebuild)))))
504 )
505 ;;;_ - Version
506 ;;;_ = outline-version
507 (defvar outline-version
508 (let ((rcs-rev "$Revision: 3.39 $"))
509 (condition-case err
510 (save-match-data
511 (string-match "\\$Revision: \\([0-9]+\\.[0-9]+\\)" rcs-rev)
512 (substring rcs-rev (match-beginning 1) (match-end 1)))
513 (error rcs-rev)))
514 "Revision number of currently loaded outline package. (Currently
515 specific to allout.el.)")
516 ;;;_ > outline-version
517 (defun outline-version (&optional here)
518 "Return string describing the loaded outline version."
519 (interactive "P")
520 (let ((msg (concat "Allout Outline Mode v " outline-version)))
521 (if here (insert-string msg))
522 (message "%s" msg)
523 msg))
524
525 ;;;_ - Mode activation
526 ;;;_ = outline-mode
527 (defvar outline-mode () "Allout outline mode minor-mode flag.")
528 (make-variable-buffer-local 'outline-mode)
529 ;;;_ = outline-mode-map
530 (defvar outline-mode-map nil "Keybindings for (allout) outline minor mode.")
531 ;;;_ > outline-mode-p ()
532 (defmacro outline-mode-p ()
533 '(and (boundp 'outline-mode) outline-mode))
534
535 ;;;_ = outline-during-write-cue nil
536 (defvar outline-during-write-cue nil
537 "Indication, for outline-post-command-business, that we are in the
538 process of writing a file, and need to inhibit change protection. See
539 also, outline-write-file-hook, outline-before-change-protect,
540 outline-post-command-business functions.")
541
542 ;;;_ > outline-write-file-hook ()
543 (defun outline-write-file-hook ()
544 "In outline mode, run as a local-write-file-hooks activity.
545 Currently just sets 'outline-during-write-cue', so outline-change-
546 protection knows to keep inactive during file write."
547 (setq outline-during-write-cue t)
548 nil)
549
550 ;;;_ = outline-override-protect nil
551 (defvar outline-override-protect nil
552 "In emacs v19 &c, outline-allout mode regulates alteration of concealed text
553 so it's affected as a unit, or not at all. This is for use by competant
554 (eg, native outline) functions to temporarily override that protection. It's
555 automatically reset to nil after every buffer modification.")
556 (make-variable-buffer-local 'outline-override-protect)
557 ;;;_ > outline-unprotected (expr)
558 (defmacro outline-unprotected (expr)
559 "Evaluate EXPRESSION with outline-override-protect
560 let-bound 't'."
561 (` (let ((outline-override-protect t))
562 (, expr))))
563 ;;;_ = outline-undo-aggregation
564 (defvar outline-undo-aggregation 30
565 "Amount of successive self-insert actions to bunch together per undo.
566 This is purely a kludge variable, regulating the compensation for a bug in
567 the way that before-change-function and undo interact.")
568 (make-variable-buffer-local 'outline-undo-aggregation)
569
570 ;;;_ > produce-outline-mode-map (keymap-alist &optional base-map)
571 (defun produce-outline-mode-map (keymap-list &optional base-map)
572 "Produce keymap for use as outline-mode-map, from keymap-list.
573 Built on top of optional BASE-MAP, or empty sparse map if none specified.
574 See doc string for outline-keybindings-list for format of binding list."
575 (let ((map (or base-map (make-sparse-keymap))))
576 (mapcar (lambda (cell)
577 (apply 'define-key map (if (null (cdr (cdr cell)))
578 (cons (concat outline-command-prefix
579 (car cell))
580 (cdr cell))
581 (list (car cell) (car (cdr cell))))))
582 keymap-list)
583 map))
584 ;;;_ > outline-mode (&optional toggle)
585 ;;;_ . Defun:
586 (defvar outline-v18/9-file-var-hack nil
587 "Horrible hack used to prevent invalid multiple triggering of outline
588 mode from prop-line file-var activation. Used by outline-mode function
589 to track repeats.")
590 (defun outline-mode (&optional toggle)
591 ;;;_ . Doc string:
592 "Toggle minor mode for controlling exposure of and editing text
593 outlines. Optional arg forces mode activation iff arg is positive.
594
595 Look below the description of the bindings for explanation of the
596 terminology use in outline-mode commands.
597
598 Exposure Commands Movement Commands
599 C-c C-h outline-hide-current-subtree C-c C-n outline-next-visible-heading
600 C-c C-i outline-show-children C-c C-p outline-previous-visible-heading
601 C-c C-s outline-show-current-subtree C-c C-u outline-up-current-level
602 C-c C-o outline-show-current-entry C-c C-f outline-forward-current-level
603 C-c ! outline-show-all C-c C-b outline-backward-current-level
604 outline-hide-current-leaves C-c C-e outline-end-of-current-entry
605 C-c C-a outline-beginning-of-current-entry
606
607
608 Topic Header Generation Commands
609 C-c<SP> outline-open-sibtopic Create a new sibling after current topic
610 C-c . outline-open-subtopic ... an offspring of current topic
611 C-c , outline-open-supertopic ... a sibling of the current topics' parent
612
613 Level and Prefix Adjustment Commands
614 C-c > outline-shift-in Shift current topic and all offspring deeper
615 C-c < outline-shift-out ... less deep
616 C-c<CR> outline-rebullet-topic Reconcile bullets of topic and its offspring
617 - distinctive bullets are not changed, all
618 others set suitable according to depth
619 C-c b outline-rebullet-current-heading Prompt for alternate bullet for
620 current topic
621 C-c # outline-number-siblings Number bullets of topic and siblings - the
622 offspring are not affected. With repeat
623 count, revoke numbering.
624
625 Killing and Yanking - all keep siblings numbering reconciled as appropriate
626 C-k outline-kill-line Regular kill line, but respects numbering ,etc
627 C-c C-k outline-kill-topic Kill current topic, including offspring
628 C-y outline-yank Yank, adjusting depth of yanked topic to
629 depth of heading if yanking into bare topic
630 heading (ie, prefix sans text)
631 M-y outline-yank-pop Is to outline-yank as yank-pop is to yank
632
633 Misc commands
634 C-c @ outline-resolve-xref pop-to-buffer named by xref (cf
635 outline-file-xref-bullet)
636 C-c c outline-copy-exposed Copy outline sans all hidden stuff to
637 another buffer whose name is derived
638 from the current one - \"XXX exposed\"
639 M-x outlineify-sticky Activate outline mode for current buffer
640 and establish -*- outline -*- mode specifier
641 as well as file local vars to automatically
642 set exposure. Try it.
643
644 \\<outline-mode-map>
645 HOT-SPOT Operation (Not available in Emacs v18.)
646
647 Hot-spot operation enables succinct outline operation. When the
648 cursor is located on the bullet character of a topic, literal
649 characters invoke the commands of the corresponding control chars in
650 the outline-mode keymap. Thus, 'f' would invoke the command bound to
651 <outline-command-prefix>-\C-f \(typically 'outline-forward-current-
652 level').
653
654 Thus, by positioning the cursor on a topic bullet, you can do each of
655 the outline navigation and manipulation commands with a single
656 keystroke. Non-literal char do not get this special interpretation,
657 even on the hot-spot, so you can use them to get off of it, and back
658 to normal operation.
659
660 Note that the command outline-beginning-of-current-entry \(\\[outline-beginning-of-current-entry]\)
661 will move to the hot-spot when the cursor is already located at the
662 beginning of the current entry, so you can simply hit \\[outline-beginning-of-current-entry]
663 twice in a row to get to the hot-spot.
664
665 Terminology
666
667 Topic hierarchy constituents - TOPICS and SUBTOPICS:
668
669 TOPIC: A basic, coherent component of an emacs outline. It can
670 contain other topics, and it can be subsumed by other topics,
671 CURRENT topic:
672 The visible topic most immediately containing the cursor.
673 DEPTH: The degree of nesting of a topic, it increases with
674 containment. Also called the
675 LEVEL: The same as DEPTH.
676
677 ANCESTORS:
678 The topics that contain a topic.
679 PARENT: A topic's immediate ancestor. It has a depth one less than
680 the topic.
681 OFFSPRING:
682 The topics contained by a topic,
683 CHILDREN:
684 The immediate offspring of a topic.
685 SIBLINGS:
686 Topics having the same parent.
687
688 Topic text constituents:
689
690 HEADER: The first line of a topic, include the topic PREFIX and header
691 text.
692 PREFIX: The leading text of a topic which which distinguishes it from
693 normal text. It has a strict form, which consists of a
694 prefix-lead string, padding, and a bullet. The bullet may be
695 followed by a number, indicating the ordinal number of the
696 topic among its siblings, a space, and then the header text.
697
698 The relative length of the PREFIX determines the nesting depth
699 of the topic.
700 PREFIX-LEAD:
701 The string at the beginning of a topic prefix, normally a '.'.
702 It can be customized by changing the setting of
703 'outline-header-prefix' and then reinitializing outline-mode.
704
705 By setting the prefix-lead to the comment-string of a
706 programming language, you can embed outline-structuring in
707 program code without interfering with the language processing
708 of that code.
709 PREFIX-PADDING:
710 Spaces or asterisks which separate the prefix-lead and the
711 bullet, according to the depth of the topic.
712 BULLET: A character at the end of the topic prefix, it must be one of
713 the characters listed on 'outline-plain-bullets-string' or
714 'outline-distinctive-bullets-string'. (See the documentation
715 for these variables for more details.) The default choice of
716 bullet when generating varies in a cycle with the depth of the
717 topic.
718 ENTRY: The text contained in a topic before any offspring.
719 BODY: Same as ENTRY.
720
721
722 EXPOSURE:
723 The state of a topic which determines the on-screen visibility
724 of its' offspring and contained text.
725 CONCEALED:
726 Topics and entry text whose display is inhibited. Contiguous
727 units of concealed text is represented by '...' ellipses.
728 (Ref the 'selective-display' var.)
729
730 Concealed topics are effectively collapsed within an ancestor.
731 CLOSED: A topic whose immediate offspring and body-text is concealed.
732 OPEN: A topic that is not closed."
733
734 ;;;_ . Code
735 (interactive "P")
736
737 (let* ((active (and (not (equal major-mode 'outline))
738 (outline-mode-p)))
739 ; Massage universal-arg 'toggle' val:
740 (toggle (and toggle
741 (or (and (listp toggle)(car toggle))
742 toggle)))
743 ; Activation specficially demanded?
744 (explicit-activation (or
745 ;;
746 (and toggle
747 (or (symbolp toggle)
748 (and (natnump toggle)
749 (not (zerop toggle)))))))
750 ;; outline-mode already called once during this complex command?
751 (same-complex-command (eq outline-v18/9-file-var-hack
752 (car command-history))))
753
754 ; See comments below re v19.18,.19 bug.
755 (setq outline-v18/9-file-var-hack (car command-history))
756
757 (cond
758
759 ;; Hitting v19.18, 19.19 bug?
760 ;; Emacs v 19.18, 19.19 file-var code invokes prop-line-designated
761 ;; modes twice when file is visited. We have to avoid toggling mode
762 ;; off on second invocation, so we detect it as best we can, and
763 ;; skip everything.
764 ((and same-complex-command ; Still in same complex command
765 ; as last time outline-mode invoked.
766 active ; Already activated.
767 (not explicit-activation) ; Prop-line file-vars don't have args.
768 (string-match "^19.1[89]" ; Bug only known to be in v19.18 and
769 emacs-version)); 19.19.
770 t)
771
772 ;; Deactivate?
773 ((and (not explicit-activation)
774 (or active toggle))
775 ; Activation not explicitly
776 ; requested, and either in
777 ; active state or *de*activation
778 ; specifically requested:
779 (if (string-match "^18\." emacs-version)
780 ; Revoke those keys that remain
781 ; as we set them:
782 (let ((curr-loc (current-local-map)))
783 (mapcar '(lambda (cell)
784 (if (eq (lookup-key curr-loc (car cell))
785 (car (cdr cell)))
786 (define-key curr-loc (car cell)
787 (assq (car cell) outline-prior-bindings))))
788 outline-added-bindings)
789 (outline-resumptions 'outline-added-bindings)
790 (outline-resumptions 'outline-prior-bindings)))
791
792 (if outline-old-style-prefixes
793 (progn
794 (outline-resumptions 'outline-primary-bullet)
795 (outline-resumptions 'outline-old-style-prefixes)))
796 (outline-resumptions 'selective-display)
797 (if (and (boundp 'before-change-function) before-change-function)
798 (outline-resumptions 'before-change-function))
799 (setq pre-command-hook (delq 'outline-pre-command-business
800 pre-command-hook))
801 (setq post-command-hook (delq 'outline-post-command-business
802 post-command-hook))
803 (setq local-write-file-hooks
804 (delq 'outline-write-file-hook
805 local-write-file-hooks))
806 (outline-resumptions 'paragraph-start)
807 (outline-resumptions 'paragraph-separate)
808 (outline-resumptions (if (string-match "^18" emacs-version)
809 'auto-fill-hook
810 'auto-fill-function))
811 (outline-resumptions 'outline-former-auto-filler)
812 (setq outline-mode nil))
813
814 ;; Activate?
815 ((not active)
816 (if outline-old-style-prefixes
817 (progn ; Inhibit all the fancy formatting:
818 (outline-resumptions 'outline-primary-bullet '("*"))
819 (outline-resumptions 'outline-old-style-prefixes '(()))))
820 (set-outline-regexp)
821 ; Produce map from current version
822 ; of outline-keybindings-list:
823 (if (boundp 'minor-mode-map-alist)
824
825 (progn ; V19, and maybe lucid and
826 ; epoch, minor-mode key bindings:
827 (setq outline-mode-map
828 (produce-outline-mode-map outline-keybindings-list))
829 (fset 'outline-mode-map outline-mode-map)
830 ; Include on minor-mode-map-alist,
831 ; if not already there:
832 (if (not (member '(outline-mode . outline-mode-map)
833 minor-mode-map-alist))
834 (setq minor-mode-map-alist
835 (cons '(outline-mode . outline-mode-map)
836 minor-mode-map-alist))))
837
838 ; V18 minor-mode key bindings:
839 ; Stash record of added bindings
840 ; for later revocation:
841 (outline-resumptions 'outline-added-bindings
842 (list outline-keybindings-list))
843 (outline-resumptions 'outline-prior-bindings
844 (list (current-local-map)))
845 ; and add them:
846 (use-local-map (produce-outline-mode-map outline-keybindings-list
847 (current-local-map)))
848 )
849
850 ; selective-display is the
851 ; emacs conditional exposure
852 ; mechanism:
853 (outline-resumptions 'selective-display '(t))
854 (if outline-inhibit-protection
855 t
856 (outline-resumptions 'before-change-function
857 '(outline-before-change-protect)))
858 (add-hook 'post-command-hook 'outline-post-command-business)
859 (add-hook 'pre-command-hook 'outline-pre-command-business)
860 ; Temporarily set by any outline
861 ; functions that can be trusted to
862 ; deal properly with concealed text.
863 (add-hook 'local-write-file-hooks 'outline-write-file-hook)
864 ; Custom auto-fill func, to support
865 ; respect for topic headline,
866 ; hanging-indents, etc:
867 (let* ((fill-func-var (if (string-match "^18" emacs-version)
868 'auto-fill-hook
869 'auto-fill-function))
870 (fill-func (symbol-value fill-func-var)))
871 ;; Register prevailing fill func for use by outline-auto-fill:
872 (outline-resumptions 'outline-former-auto-filler (list fill-func))
873 ;; Register outline-auto-fill to be used if filling is active:
874 (outline-resumptions fill-func-var '(outline-auto-fill)))
875 ;; Paragraphs are broken by topic headlines.
876 (make-local-variable 'paragraph-start)
877 (outline-resumptions 'paragraph-start
878 (list (concat paragraph-start "\\|^\\("
879 outline-regexp "\\)")))
880 (make-local-variable 'paragraph-separate)
881 (outline-resumptions 'paragraph-separate
882 (list (concat paragraph-separate "\\|^\\("
883 outline-regexp "\\)")))
884
885 (or (assq 'outline-mode minor-mode-alist)
886 (setq minor-mode-alist
887 (cons '(outline-mode " Outl") minor-mode-alist)))
888
889 (if outline-enwrap-isearch-mode
890 (outline-enwrap-isearch))
891 (run-hooks 'outline-mode-hook)
892 (setq outline-mode t))
893 ) ; cond
894 outline-mode
895 ) ; let*
896 ) ; defun
897
898
899 ;;;_ #2 Internal Position State-Tracking Variables
900 ;;; All basic outline functions which directly do string matches to
901 ;;; evaluate heading prefix location set the variables
902 ;;; outline-recent-prefix-beginning and outline-recent-prefix-end when
903 ;;; successful. Functions starting with 'outline-recent-' all use
904 ;;; this state, providing the means to avoid redundant searches for
905 ;;; just established data. This optimization can provide significant
906 ;;; speed improvement, but it must be employed carefully.
907 ;;;_ = outline-recent-prefix-beginning
908 (defvar outline-recent-prefix-beginning 0
909 "Buffer point of the start of the last topic prefix encountered.")
910 (make-variable-buffer-local 'outline-recent-prefix-beginning)
911 ;;;_ = outline-recent-prefix-end
912 (defvar outline-recent-prefix-end 0
913 "Buffer point of the end of the last topic prefix encountered.")
914 (make-variable-buffer-local 'outline-recent-prefix-end)
915 ;;;_ > outline-prefix-data (beg end)
916 (defmacro outline-prefix-data (beg end)
917 "Register outline-prefix state data - BEGINNING and END of prefix -
918 for reference by 'outline-recent' funcs. Returns BEGINNING."
919 (` (setq outline-recent-prefix-end (, end)
920 outline-recent-prefix-beginning (, beg))))
921 ;;;_ > outline-recent-depth ()
922 (defmacro outline-recent-depth ()
923 "Return depth of last heading encountered by an outline maneuvering
924 function.
925
926 All outline functions which directly do string matches to assess
927 headings set the variables outline-recent-prefix-beginning and
928 outline-recent-prefix-end if successful. This function uses those settings
929 to return the current depth."
930
931 '(max 1 (- outline-recent-prefix-end
932 outline-recent-prefix-beginning
933 outline-header-subtraction)))
934 ;;;_ > outline-recent-prefix ()
935 (defmacro outline-recent-prefix ()
936 "Like outline-recent-depth, but returns text of last encountered prefix.
937
938 All outline functions which directly do string matches to assess
939 headings set the variables outline-recent-prefix-beginning and
940 outline-recent-prefix-end if successful. This function uses those settings
941 to return the current depth."
942 '(buffer-substring outline-recent-prefix-beginning
943 outline-recent-prefix-end))
944 ;;;_ > outline-recent-bullet ()
945 (defmacro outline-recent-bullet ()
946 "Like outline-recent-prefix, but returns bullet of last encountered
947 prefix.
948
949 All outline functions which directly do string matches to assess
950 headings set the variables outline-recent-prefix-beginning and
951 outline-recent-prefix-end if successful. This function uses those settings
952 to return the current depth of the most recently matched topic."
953 '(buffer-substring (1- outline-recent-prefix-end)
954 outline-recent-prefix-end))
955
956 ;;;_ #3 Navigation
957
958 ;;;_ - Position Assessment
959 ;;;_ : Location Predicates
960 ;;;_ > outline-on-current-heading-p ()
961 (defun outline-on-current-heading-p ()
962 "Return prefix beginning point if point is on same line as current
963 visible topics' header line."
964 (save-excursion
965 (beginning-of-line)
966 (and (looking-at outline-regexp)
967 (outline-prefix-data (match-beginning 0) (match-end 0)))))
968 ;;;_ > outline-e-o-prefix-p ()
969 (defun outline-e-o-prefix-p ()
970 "True if point is located where current topic prefix ends, heading
971 begins."
972 (and (save-excursion (beginning-of-line)
973 (looking-at outline-regexp))
974 (= (point)(save-excursion (outline-end-of-prefix)(point)))))
975 ;;;_ > outline-hidden-p ()
976 (defmacro outline-hidden-p ()
977 "True if point is in hidden text."
978 '(save-excursion
979 (and (re-search-backward "[\n\r]" () t)
980 (= ?\r (following-char)))))
981 ;;;_ > outline-visible-p ()
982 (defmacro outline-visible-p ()
983 "True if point is not in hidden text."
984 (interactive)
985 '(not (outline-hidden-p)))
986 ;;;_ : Location attributes
987 ;;;_ > outline-depth ()
988 (defmacro outline-depth ()
989 "Like outline-current-depth, but respects hidden as well as visible
990 topics."
991 '(save-excursion
992 (if (outline-goto-prefix)
993 (outline-recent-depth)
994 (progn
995 ;; Oops, no prefix, zero prefix data:
996 (outline-prefix-data (point)(point))
997 ;; ... and return 0:
998 0))))
999 ;;;_ > outline-current-depth ()
1000 (defmacro outline-current-depth ()
1001 "Return the depth to which the current containing visible topic is
1002 nested in the outline."
1003 '(save-excursion
1004 (if (outline-back-to-current-heading)
1005 (max 1
1006 (- outline-recent-prefix-end
1007 outline-recent-prefix-beginning
1008 outline-header-subtraction))
1009 0)))
1010 ;;;_ > outline-get-current-prefix ()
1011 (defun outline-get-current-prefix ()
1012 "Topic prefix of the current topic."
1013 (save-excursion
1014 (if (outline-goto-prefix)
1015 (outline-recent-prefix))))
1016 ;;;_ > outline-get-bullet ()
1017 (defun outline-get-bullet ()
1018 "Return bullet of containing topic (visible or not)."
1019 (save-excursion
1020 (and (outline-goto-prefix)
1021 (outline-recent-bullet))))
1022 ;;;_ > outline-current-bullet ()
1023 (defun outline-current-bullet ()
1024 "Return bullet of current (visible) topic heading, or none if none found."
1025 (condition-case err
1026 (save-excursion
1027 (outline-back-to-current-heading)
1028 (buffer-substring (- outline-recent-prefix-end 1)
1029 outline-recent-prefix-end))
1030 ;; Quick and dirty provision, ostensibly for missing bullet:
1031 (args-out-of-range nil))
1032 )
1033 ;;;_ > outline-get-prefix-bullet (prefix)
1034 (defun outline-get-prefix-bullet (prefix)
1035 "Return the bullet of the header prefix string PREFIX."
1036 ;; Doesn't make sense if we're old-style prefixes, but this just
1037 ;; oughtn't be called then, so forget about it...
1038 (if (string-match outline-regexp prefix)
1039 (substring prefix (1- (match-end 0)) (match-end 0))))
1040
1041 ;;;_ - Navigation macros
1042 ;;;_ > outline-next-heading ()
1043 (defmacro outline-next-heading ()
1044 "Move to the heading for the topic \(possibly invisible) before this one.
1045
1046 Returns the location of the heading, or nil if none found."
1047
1048 '(if (and (bobp) (not (eobp)))
1049 (forward-char 1))
1050
1051 '(if (re-search-forward outline-line-boundary-regexp nil 0)
1052 (progn ; Got valid location state - set vars:
1053 (outline-prefix-data
1054 (goto-char (or (match-beginning 2)
1055 outline-recent-prefix-beginning))
1056 (or (match-end 2) outline-recent-prefix-end)))))
1057 ;;;_ > outline-previous-heading ()
1058 (defmacro outline-previous-heading ()
1059 "Move to the next \(possibly invisible) heading line.
1060
1061 Return the location of the beginning of the heading, or nil if not found."
1062
1063 '(if (bobp)
1064 nil
1065 (outline-goto-prefix)
1066 (if
1067 ;; searches are unbounded and return nil if failed:
1068 (or (re-search-backward outline-line-boundary-regexp nil 0)
1069 (looking-at outline-bob-regexp))
1070 (progn;; Got some valid location state - set vars:
1071 (outline-prefix-data
1072 (goto-char (or (match-beginning 2)
1073 outline-recent-prefix-beginning))
1074 (or (match-end 2) outline-recent-prefix-end))))))
1075
1076 ;;;_ - Subtree Charting
1077 ;;;_ " These routines either produce or assess charts, which are
1078 ;;; nested lists of the locations of topics within a subtree.
1079 ;;;
1080 ;;; Use of charts enables efficient navigation of subtrees, by
1081 ;;; requiring only a single regexp-search based traversal, to scope
1082 ;;; out the subtopic locations. The chart then serves as the basis
1083 ;;; for whatever assessment or adjustment of the subtree that is
1084 ;;; required, without requiring redundant topic-traversal procedures.
1085
1086 ;;;_ > outline-chart-subtree (&optional orig-level prev-level)
1087 (defun outline-chart-subtree (&optional orig-level prev-level)
1088 "Produce a location 'chart' of subtopics of the containing topic.
1089 The entries for each immediate child consists of an integer for the
1090 point of the beginning of the topic, followed by a 'chart' of the
1091 immediate offspring of the subtopic, if any.
1092
1093 Use of charts enables efficient navigation of subtrees, by requiring
1094 only a single regexp-search based traversal, to scope out the subtopic
1095 locations. The chart then serves as the basis for whatever assessment
1096 or adjustment of the subtree that is required, without requiring
1097 redundant topic-traversal procedures.
1098
1099 The function parameters are for internal recursion, and should not be
1100 designated by external callers."
1101
1102 ;; We're constantly looking ahead. Impressive, huh?
1103
1104 (let ((original (not orig-level)) ; 'here' passed only during recursion.
1105 chart here level)
1106 ; Initialize if not passed in:
1107 (if original
1108 (progn (setq orig-level (outline-depth))
1109 (outline-next-heading)))
1110 ; Consider only contents of orig topic:
1111 (if (not prev-level)
1112 (setq prev-level (1+ orig-level)))
1113
1114 ;; Loop, rather than recurse, over the current levels' siblings, to
1115 ;; avoid overloading the typically quite constrained emacs max-lisp-
1116 ;; eval-depth.
1117 (while (and (not (eobp))
1118 (< orig-level (setq level (outline-recent-depth)))
1119 ; Still within original topic:
1120 (cond ((= prev-level level)
1121 (setq chart ; Register this one and move on:
1122 (cons (point) chart))
1123 (outline-next-heading))
1124
1125 ((< prev-level level) ; Do higher level, then
1126 ; continue with this one:
1127 (setq chart (cons (outline-chart-subtree orig-level
1128 level)
1129 chart))))))
1130
1131 (if original ; We're at the end of the level...
1132 ; Position to the end of the branch:
1133 (progn (and (not (eobp)) (forward-char -1))
1134 (and (memq (preceding-char) '(?\n ?\^M))
1135 (memq (aref (buffer-substring (max 1 (- (point) 3))
1136 (point))
1137 1)
1138 '(?\n ?\^M))
1139 (forward-char -1))))
1140
1141 chart ; (nreverse chart) not necessary,
1142 ; and maybe not preferable.
1143 ))
1144 ;;;_ > outline-chart-topic (&optional orig-level prev-level)
1145 (defmacro outline-chart-topic ()
1146 "Return a location 'chart' for the current topic and its subtopics,if any.
1147 See 'outline-chart-subtree' for an explanation of charts."
1148
1149 '(if (outline-goto-prefix)
1150 (let ((here (point))
1151 (subtree (outline-chart-subtree orig-level prev-level)))
1152 (if subtree
1153 (list here subtree)
1154 (list here)))))
1155 ;;;_ > outline-chart-siblings (&optional start end)
1156 (defun outline-chart-siblings (&optional start end)
1157 "Produce a list of locations of this and succeeding sibling topics.
1158 Effectively a top-level chart of siblings. See 'outline-chart-subtree'
1159 for an explanation of charts."
1160 (save-excursion
1161 (if (outline-goto-prefix)
1162 (let ((chart (list (point))))
1163 (while (outline-next-sibling)
1164 (setq chart (cons (point) chart)))
1165 (if chart (setq chart (nreverse chart))))))
1166 )
1167 ;;;_ > outline-chart-to-reveal (chart depth)
1168 (defun outline-chart-to-reveal (chart depth)
1169
1170 "Return a flat list of the points in subtree CHART, up to DEPTH, which
1171 are hidden.
1172
1173 Note that point can wind up at any of the points on chart, or at the
1174 start point."
1175
1176 (let (result here)
1177 (while (and (or (eq depth t) (> depth 0))
1178 chart)
1179 (setq here (car chart))
1180 (if (listp here)
1181 (let ((further (outline-chart-to-reveal here (or (eq depth t)
1182 (1- depth)))))
1183 ;; We're on the start of a subtree - recurse with it, if there's
1184 ;; more depth to go:
1185 (if further (setq result (append further result)))
1186 (setq chart (cdr chart)))
1187 (goto-char here)
1188 (if (= (preceding-char) ?\r)
1189 (setq result (cons here result)))
1190 (setq chart (cdr chart))))
1191 result))
1192 ;;;_ > outline-chart-spec (chart spec &optional exposing)
1193 (defun outline-chart-spec (chart spec &optional exposing)
1194 "Given a topic/subtree CHART and an exposure SPEC, produce a list of
1195 exposure directive, indicating the locations to be exposed and the
1196 prescribed exposure status. Optional arg EXPOSING is an integer, with
1197 0 indicating pending concealment, anything higher indicating depth to
1198 which subtopic headers should be exposed, and negative numbers
1199 indicating (negative of) the depth to which subtopic headers and
1200 bodies should be exposed.
1201
1202 The produced list can have two types of entries. Bare numbers
1203 indicate points in the buffer where topic headers that should be
1204 exposed reside.
1205 - bare negative numbers indicates that the topic starting at the
1206 point which is the negative of the number should be opened,
1207 including their entries.
1208 - bare positive values indicate that this topic header should be
1209 openned.
1210 - Lists signify the beginning and end points of regions that should
1211 be flagged, and the flag to employ. (For concealment: '\(\?r\)', and
1212 exposure:"
1213 (while spec
1214 (cond ((listp spec)
1215 )
1216 )
1217 (setq spec (cdr spec)))
1218 )
1219
1220 ;;;_ - Within Topic
1221 ;;;_ > outline-goto-prefix ()
1222 (defun outline-goto-prefix ()
1223 "Put point at beginning of outline prefix for immediately containing
1224 topic, visible or not.
1225
1226 Returns a the point at the beginning of the prefix, or nil if none."
1227
1228 (if (= (point-min)(point-max))
1229 nil
1230 (let (done)
1231 (while (and (not done)
1232 (re-search-backward "[\n\r]" nil 1))
1233 (forward-char 1)
1234 (if (looking-at outline-regexp)
1235 (setq done (outline-prefix-data (match-beginning 0)
1236 (match-end 0)))
1237 (forward-char -1)))
1238 (if (and (bobp)
1239 (looking-at outline-regexp))
1240 (outline-prefix-data (match-beginning 0)(match-end 0))
1241 done))))
1242 ;;;_ > outline-end-of-prefix ()
1243 (defun outline-end-of-prefix (&optional ignore-decorations)
1244 "Position cursor at beginning of header text, or just after bullet
1245 if optional IGNORE-DECORATIONS non-nil."
1246
1247 (if (not (outline-goto-prefix))
1248 nil
1249 (let ((match-data (match-data)))
1250 (goto-char (match-end 0))
1251 (if ignore-decorations
1252 t
1253 (while (looking-at "[0-9]") (forward-char 1))
1254 (if (and (not (eolp)) (looking-at "\\s-")) (forward-char 1)))
1255 (store-match-data match-data))
1256 ;; Reestablish where we are:
1257 (outline-current-depth)))
1258 ;;;_ > outline-current-bullet-pos ()
1259 (defun outline-current-bullet-pos ()
1260 "Return position of current \(visible) topic's bullet."
1261
1262 (if (not (outline-current-depth))
1263 nil
1264 (1- (match-end 0))))
1265 ;;;_ > outline-back-to-current-heading ()
1266 (defun outline-back-to-current-heading ()
1267 "Move to heading line of current visible topic, or beginning of heading
1268 if already on visible heading line."
1269 (beginning-of-line)
1270 (prog1 (or (outline-on-current-heading-p)
1271 (and (re-search-backward (concat "^\\(" outline-regexp "\\)")
1272 nil
1273 'move)
1274 (outline-prefix-data (match-beginning 1)(match-end 1))))
1275 (if (interactive-p) (outline-end-of-prefix))))
1276 ;;;_ > outline-pre-next-preface ()
1277 (defun outline-pre-next-preface ()
1278 "Skip forward to just before the next heading line.
1279
1280 Returns that character position."
1281
1282 (if (re-search-forward outline-line-boundary-regexp nil 'move)
1283 (prog1 (goto-char (match-beginning 0))
1284 (outline-prefix-data (match-beginning 2)(match-end 2)))))
1285 ;;;_ > outline-end-of-current-subtree ()
1286 (defun outline-end-of-current-subtree ()
1287 "Put point at the end of the last leaf in the currently visible topic."
1288 (interactive)
1289 (outline-back-to-current-heading)
1290 (let ((opoint (point))
1291 (level (outline-recent-depth)))
1292 (outline-next-heading)
1293 (while (and (not (eobp))
1294 (> (outline-recent-depth) level))
1295 (outline-next-heading))
1296 (and (not (eobp)) (forward-char -1))
1297 (and (memq (preceding-char) '(?\n ?\^M))
1298 (memq (aref (buffer-substring (max 1 (- (point) 3)) (point)) 1)
1299 '(?\n ?\^M))
1300 (forward-char -1))
1301 (point)))
1302 ;;;_ > outline-beginning-of-current-entry ()
1303 (defun outline-beginning-of-current-entry ()
1304 "When not already there, position the point at the beginning of the
1305 body of the current topic.
1306
1307 If already there, move cursor to bullet for hot-spot operation.
1308 \(See outline-mode doc string for details on hot-spot operation.)"
1309 (interactive)
1310 (let ((start-point (point)))
1311 (outline-end-of-prefix)
1312 (if (and (interactive-p)
1313 (= (point) start-point))
1314 (goto-char (outline-current-bullet-pos)))))
1315 ;;;_ > outline-end-of-current-entry ()
1316 (defun outline-end-of-current-entry ()
1317 "Position the point at the end of the current topics' entry."
1318 (interactive)
1319 (outline-show-entry)
1320 (prog1 (outline-pre-next-preface)
1321 (if (and (not (bobp))(looking-at "^$"))
1322 (forward-char -1)))
1323 )
1324
1325 ;;;_ - Depth-wise
1326 ;;;_ > outline-ascend-to-depth (depth)
1327 (defun outline-ascend-to-depth (depth)
1328 "Ascend to depth DEPTH, returning depth if successful, nil if not."
1329 (if (and (> depth 0)(<= depth (outline-depth)))
1330 (let ((last-good (point)))
1331 (while (and (< depth (outline-depth))
1332 (setq last-good (point))
1333 (outline-beginning-of-level)
1334 (outline-previous-heading)))
1335 (if (= (outline-recent-depth) depth)
1336 (progn (goto-char outline-recent-prefix-beginning)
1337 depth)
1338 (goto-char last-good)
1339 nil))
1340 (if (interactive-p) (outline-end-of-prefix))))
1341 ;;;_ > outline-descend-to-depth (depth)
1342 (defun outline-descend-to-depth (depth)
1343 "Descend to depth DEPTH within current topic, returning depth if
1344 successful, nil if not."
1345 (let ((start-point (point))
1346 (start-depth (outline-depth)))
1347 (while
1348 (and (> (outline-depth) 0)
1349 (not (= depth (outline-recent-depth))) ; ... not there yet
1350 (outline-next-heading) ; ... go further
1351 (< start-depth (outline-recent-depth)))) ; ... still in topic
1352 (if (and (> (outline-depth) 0)
1353 (= (outline-recent-depth) depth))
1354 depth
1355 (goto-char start-point)
1356 nil))
1357 )
1358 ;;;_ > outline-up-current-level (arg &optional dont-complain)
1359 (defun outline-up-current-level (arg &optional dont-complain)
1360 "Move to the heading line of which the present line is a subheading.
1361 With argument, move up ARG levels. Don't return an error if
1362 second, optional argument DONT-COMPLAIN, is non-nil."
1363 (interactive "p")
1364 (outline-back-to-current-heading)
1365 (let ((present-level (outline-recent-depth)))
1366 ;; Loop for iterating arg:
1367 (while (and (> (outline-recent-depth) 1)
1368 (> arg 0)
1369 (not (bobp)))
1370 ;; Loop for going back over current or greater depth:
1371 (while (and (not (< (outline-recent-depth) present-level))
1372 (outline-previous-visible-heading 1)))
1373 (setq present-level (outline-current-depth))
1374 (setq arg (- arg 1)))
1375 )
1376 (prog1 (if (<= arg 0)
1377 outline-recent-prefix-beginning
1378 (if (interactive-p) (outline-end-of-prefix))
1379 (if (not dont-complain)
1380 (error "Can't ascend past outermost level.")))
1381 (if (interactive-p) (outline-end-of-prefix)))
1382 )
1383
1384 ;;;_ - Linear
1385 ;;;_ > outline-next-sibling (&optional depth backward)
1386 (defun outline-next-sibling (&optional depth backward)
1387 "Like outline-forward-current-level, but respects invisible topics.
1388
1389 Traverse at optional DEPTH, or current depth if none specified.
1390
1391 Go backward if optional arg BACKWARD is non-nil.
1392
1393 Return depth if successful, nil otherwise."
1394
1395 (if (and backward (bobp))
1396 nil
1397 (let ((start-depth (or depth (outline-depth)))
1398 (start-point (point))
1399 last-depth)
1400 (while (and (not (if backward (bobp) (eobp)))
1401 (if backward (outline-previous-heading)
1402 (outline-next-heading))
1403 (> (setq last-depth (outline-recent-depth)) start-depth)))
1404 (if (and (not (eobp))
1405 (and (> (or last-depth (outline-depth)) 0)
1406 (= (outline-recent-depth) start-depth)))
1407 outline-recent-prefix-beginning
1408 (goto-char start-point)
1409 (if depth (outline-depth) start-depth)
1410 nil))))
1411 ;;;_ > outline-previous-sibling (&optional depth backward)
1412 (defun outline-previous-sibling (&optional depth backward)
1413 "Like outline-forward-current-level, but goes backwards and respects
1414 invisible topics.
1415
1416 Optional DEPTH specifies depth to traverse, default current depth.
1417
1418 Optional BACKWARD reverses direction.
1419
1420 Return depth if successful, nil otherwise."
1421 (outline-next-sibling depth (not backward))
1422 )
1423 ;;;_ > outline-snug-back ()
1424 (defun outline-snug-back ()
1425 "Position cursor at end of previous topic, presuming that we are at
1426 the start of a topic prefix."
1427 (if (or (bobp) (eobp))
1428 nil
1429 (forward-char -1))
1430 (if (or (bobp) (not (memq (preceding-char) '(?\n ?\^M))))
1431 nil
1432 (forward-char -1)
1433 (if (or (bobp) (not (memq (preceding-char) '(?\n ?\^M))))
1434 (forward-char -1)))
1435 (point))
1436 ;;;_ > outline-beginning-of-level ()
1437 (defun outline-beginning-of-level ()
1438 "Go back to the first sibling at this level, visible or not."
1439 (outline-end-of-level 'backward))
1440 ;;;_ > outline-end-of-level (&optional backward)
1441 (defun outline-end-of-level (&optional backward)
1442 "Go to the last sibling at this level, visible or not."
1443
1444 (let ((depth (outline-depth)))
1445 (while (outline-previous-sibling depth nil))
1446 (prog1 (outline-recent-depth)
1447 (if (interactive-p) (outline-end-of-prefix)))))
1448 ;;;_ > outline-next-visible-heading (arg)
1449 (defun outline-next-visible-heading (arg)
1450 "Move to the next visible heading line, or as far as possible in
1451 indicated direction if no more headings to be found.
1452
1453 With argument, repeats, backward if negative."
1454
1455 (interactive "p")
1456 (let* ((backward (if (< arg 0) (setq arg (* -1 arg))))
1457 (step (if backward -1 1))
1458 prev got)
1459
1460 (while (> arg 0) ; limit condition
1461 (while (and (not (if backward (bobp)(eobp))) ; boundary condition
1462 (progn
1463 ;; Move, skipping over all those concealed lines:
1464 (forward-line step)
1465 (not (setq got (looking-at outline-regexp))))))
1466 ;; Register this got, it may be the last:
1467 (if got (setq prev got))
1468 (setq arg (1- arg)))
1469 (cond (got ; Last move was to a prefix:
1470 (outline-prefix-data (match-beginning 0) (match-end 0))
1471 (outline-end-of-prefix))
1472 (prev ; Last move wasn't, but prev was:
1473 (outline-prefix-data (match-beginning 0) (match-end 0)))
1474 ((not backward) (end-of-line) nil))))
1475 ;;;_ > outline-previous-visible-heading (arg)
1476 (defun outline-previous-visible-heading (arg)
1477 "Move to the previous heading line.
1478
1479 With argument, repeats or can move forward if negative.
1480 A heading line is one that starts with a `*' (or that outline-regexp
1481 matches)."
1482 (interactive "p")
1483 (outline-next-visible-heading (- arg)))
1484 ;;;_ > outline-forward-current-level (arg)
1485 (defun outline-forward-current-level (arg)
1486 "Position the point at the next heading of the same level, taking
1487 optional repeat-count.
1488
1489 Non-nil optional arg BACKWARD reverses direction.
1490
1491 Returns that position, else nil if is not found."
1492 (interactive "p")
1493 (if (and (< arg 0) (bobp))
1494 nil
1495 (let ((start-depth (save-excursion
1496 (outline-back-to-current-heading)
1497 (outline-depth)))
1498 (start-point (point))
1499 (backward (> 0 arg))
1500 last-depth
1501 (last-good (point)))
1502 (if backward (setq arg (* -1 arg)))
1503 (while (> arg 0)
1504 (while (and (not (if backward (bobp) (eobp)))
1505 (if backward (outline-previous-visible-heading 1)
1506 (outline-next-visible-heading 1))
1507 (> (setq last-depth (outline-recent-depth)) start-depth)))
1508 (if (and last-depth (= last-depth start-depth) )
1509 (setq last-good (point)
1510 arg (1- arg))
1511 (setq arg -1)))
1512 (if (and (not (eobp))
1513 (and (> (or last-depth (outline-depth)) 0)
1514 (= (outline-recent-depth) start-depth)))
1515 outline-recent-prefix-beginning
1516 (goto-char last-good)
1517 (if (not (interactive-p))
1518 nil
1519 (outline-end-of-prefix)
1520 (error "This is the %s topic on level %d."
1521 (if backward "first" "last")
1522 (outline-recent-depth)))))))
1523 ;;;_ > outline-backward-current-level (arg)
1524 (defun outline-backward-current-level (arg)
1525 "Position the point at the previous heading of the same level, taking
1526 optional repeat-count.
1527
1528 Returns that position, else nil if is not found."
1529 (interactive "p")
1530 (unwind-protect
1531 (if (interactive-p)
1532 (let ((current-prefix-arg (* -1 arg)))
1533 (call-interactively 'outline-forward-current-level))
1534 (outline-forward-current-level (* -1 arg)))
1535 (outline-end-of-prefix)))
1536
1537 ;;;_ #4 Alteration
1538
1539 ;;;_ - Fundamental
1540 ;;;_ > outline-before-change-protect (beg end)
1541 (defun outline-before-change-protect (beg end)
1542 "Reveal concealed text pending improper (non-integral) changes, and
1543 offer user choice to commit or forego the change. Unchanged text is
1544 reconcealed. User has option to have changed text reconcealed.
1545
1546 Undo commands are specially treated - the user is not prompted for
1547 choice, the undoes are always committed (based on presumption that the
1548 things being undone were already subject to this regulation routine),
1549 and undoes always leave the changed stuff exposed.
1550
1551 Changes to concealed regions are ignored while file is being written.
1552 \(This is for the sake of functions that do change the file during
1553 writes, like crypt and zip modes.)
1554
1555 Locally bound in outline buffers to 'before-change-function', which
1556 in emacs 19 is run before any change to the buffer. (Has no effect
1557 in Emacs 18, which doesn't support before-change-function.)
1558
1559 Any functions which set ['this-command' to 'undo', or which set]
1560 'outline-override-protect' non-nil (as does, eg, outline-flag-chars)
1561 are exempt from this restriction."
1562 (if (and (outline-mode-p)
1563 ; outline-override-protect
1564 ; set by functions that know what
1565 ; they're doing, eg outline internals:
1566 (not outline-override-protect)
1567 (not outline-during-write-cue)
1568 (save-match-data ; Preserve operation position state.
1569 ; Both beginning and end chars must
1570 ; be exposed:
1571 (save-excursion (if (memq this-command '(newline open-line))
1572 ;; Compensate for stupid emacs {new,
1573 ;; open-}line display optimization:
1574 (setq beg (1+ beg)
1575 end (1+ end)))
1576 (goto-char beg)
1577 (or (outline-hidden-p)
1578 (and (not (= beg end))
1579 (goto-char end)
1580 (outline-hidden-p))))))
1581 (save-match-data
1582 (if (equal this-command 'undo)
1583 ;; Allow undo without inhibition.
1584 ;; - Undoing new and open-line hits stupid emacs redisplay
1585 ;; optimization (em 19 cmds.c, ~ line 200).
1586 ;; - Presumably, undoing what was properly protected when
1587 ;; done.
1588 ;; - Undo may be users' only recourse in protection faults.
1589 ;; So, expose what getting changed:
1590 (progn (message "Undo! - exposing concealed target...")
1591 (sit-for 0)
1592 (if (outline-hidden-p)
1593 (outline-show-children))
1594 (message "Undo!")
1595 (sit-for 0))
1596 (let (response
1597 (rehide-completely (save-excursion (outline-goto-prefix)
1598 (outline-hidden-p)))
1599 rehide-place)
1600
1601 (save-excursion
1602 (if (condition-case err
1603 ;; Condition case to catch keyboard quits during reads.
1604 (progn
1605 ; Give them a peek where
1606 (save-excursion
1607 (if (eolp) (setq rehide-place
1608 (outline-goto-prefix)))
1609 (outline-show-entry))
1610 ; Present the message, but...
1611 ; leave the cursor at the location
1612 ; until they respond:
1613 ; Then interpret the response:
1614 (while
1615 (progn
1616 (message (concat "Change inside concealed"
1617 " region - do it? "
1618 "(n or 'y'/'r'eclose)"))
1619 (setq response (read-char))
1620 (not
1621 (cond ((memq response '(?r ?R))
1622 (setq response 'reclose))
1623 ((memq response '(?y ?Y ? ))
1624 (setq response t))
1625 ((memq response '(?n ?N 127))
1626 (setq response nil)
1627 t)
1628 ((eq response ??)
1629 (message
1630 "'r' means 'yes, then reclose")
1631 nil)
1632 (t (message "Please answer y, n, or r")
1633 (sit-for 1)
1634 nil)))))
1635 response)
1636 (quit nil))
1637 ; Continue:
1638 (if (eq response 'reclose)
1639 (save-excursion
1640 (if rehide-place (goto-char rehide-place))
1641 (if rehide-completely
1642 (outline-hide-current-entry-completely)
1643 (outline-hide-current-entry)))
1644 (if (outline-ascend-to-depth (1- (outline-recent-depth)))
1645 (outline-show-children)
1646 (outline-show-to-offshoot)))
1647 ; Prevent:
1648 (if rehide-completely
1649 (save-excursion
1650 (if rehide-place (goto-char rehide-place))
1651 (outline-hide-current-entry-completely))
1652 (outline-hide-current-entry))
1653 (error (concat
1654 "Change within concealed region prevented.")))))))
1655 ) ; if
1656 ) ; defun
1657 ;;;_ = outline-post-goto-bullet
1658 (defvar outline-post-goto-bullet nil
1659 "Outline internal var, when set tells post-processing to reposition
1660 on topic bullet, and then unset it. Set by outline-pre-command-
1661 business when implementing hot-spot operation, where literal
1662 characters typed over a topic bullet are mapped to the command
1663 of the corresponding control-key on the outline-mode-map.")
1664 (make-variable-buffer-local 'outline-post-goto-bullet)
1665 ;;;_ > outline-post-command-business ()
1666 (defun outline-post-command-business ()
1667 "A post-command-hook function for outline buffers, takes care of some
1668 loose ends left by outline-before-change-protect.
1669
1670 - Nulls outline-override-protect, so it's not left open.
1671
1672 - Implements (and clears) outline-post-goto-bullet, for hot-spot
1673 outline commands.
1674
1675 - Massages buffer-undo-list so successive, standard character self-inserts are
1676 aggregated. This kludge compensates for lack of undo bunching when
1677 before-change-function is used."
1678
1679 ; Apply any external change func:
1680 (if (outline-mode-p) ; In outline-mode.
1681 (progn
1682 (setq outline-override-protect nil)
1683 (and outline-during-write-cue
1684 (setq outline-during-write-cue nil))
1685 ;; Undo bunching business:
1686 (if (and (listp buffer-undo-list) ; Undo history being kept.
1687 (equal this-command 'self-insert-command)
1688 (equal last-command 'self-insert-command))
1689 (let* ((prev-stuff (cdr buffer-undo-list))
1690 (before-prev-stuff (cdr (cdr prev-stuff)))
1691 cur-cell cur-from cur-to
1692 prev-cell prev-from prev-to)
1693 (if (and before-prev-stuff ; Goes back far enough to bother,
1694 (not (car prev-stuff)) ; and break before current,
1695 (not (car before-prev-stuff)) ; !and break before prev!
1696 (setq prev-cell (car (cdr prev-stuff))) ; contents now,
1697 (setq cur-cell (car buffer-undo-list)) ; contents prev.
1698
1699 ;; cur contents denote a single char insertion:
1700 (numberp (setq cur-from (car cur-cell)))
1701 (numberp (setq cur-to (cdr cur-cell)))
1702 (= 1 (- cur-to cur-from))
1703
1704 ;; prev contents denote fewer than aggregate-limit
1705 ;; insertions:
1706 (numberp (setq prev-from (car prev-cell)))
1707 (numberp (setq prev-to (cdr prev-cell)))
1708 ; Below threshold:
1709 (> outline-undo-aggregation (- prev-to prev-from)))
1710 (setq buffer-undo-list
1711 (cons (cons prev-from cur-to)
1712 (cdr (cdr (cdr buffer-undo-list))))))))
1713 ;; Implement -post-goto-bullet, if set: (must be after undo business)
1714 (if (and outline-post-goto-bullet
1715 (outline-current-bullet-pos))
1716 (progn (goto-char (outline-current-bullet-pos))
1717 (setq outline-post-goto-bullet nil)))
1718 )))
1719 ;;;_ > outline-pre-command-business ()
1720 (defun outline-pre-command-business ()
1721 "A pre-command-hook function for outline buffers. Implements
1722 special behavior when cursor is on bullet char.
1723
1724 Self-insert characters are reinterpreted control-character references
1725 into the outline-mode-map. The outline-mode post-command hook will
1726 position a cursor that has moved as a result of such reinterpretation,
1727 on the destination topic's bullet, when the cursor wound up in the
1728
1729 The upshot is that you can get easy, single (unmodified) key outline
1730 maneuvering and general operations by positioning the cursor on the
1731 bullet char, and it continues until you deliberately some non-outline
1732 motion command to relocate the cursor off of a bullet char."
1733
1734 (if (and (eq this-command 'self-insert-command)
1735 (eq (point)(outline-current-bullet-pos)))
1736
1737 (let* ((this-key-num (if (numberp last-command-event)
1738 last-command-event))
1739 mapped-binding)
1740
1741 ; Map upper-register literals
1742 ; to lower register:
1743 (if (<= 96 this-key-num)
1744 (setq this-key-num (- this-key-num 32)))
1745 ; Check if we have a literal:
1746 (if (and (<= 64 this-key-num)
1747 (>= 96 this-key-num))
1748 (setq mapped-binding
1749 (lookup-key 'outline-mode-map
1750 (concat outline-command-prefix
1751 (char-to-string (- this-key-num 64))))))
1752 (if mapped-binding
1753 (setq outline-post-goto-bullet t
1754 this-command mapped-binding)))))
1755
1756 ;;;_ - Topic Format Assessment
1757 ;;;_ > outline-solicit-alternate-bullet (depth &optional current-bullet)
1758 (defun outline-solicit-alternate-bullet (depth &optional current-bullet)
1759
1760 "Prompt for and return a bullet char as an alternative to the
1761 current one. Offer one suitable for current depth DEPTH as default."
1762
1763 (let* ((default-bullet (or current-bullet
1764 (outline-bullet-for-depth depth)))
1765 (sans-escapes (regexp-sans-escapes outline-bullets-string))
1766 (choice (solicit-char-in-string
1767 (format "Select bullet: %s ('%s' default): "
1768 sans-escapes
1769 default-bullet)
1770 sans-escapes
1771 t)))
1772 (if (string= choice "") default-bullet choice))
1773 )
1774 ;;;_ > outline-sibling-index (&optional depth)
1775 (defun outline-sibling-index (&optional depth)
1776 "Item number of this prospective topic among its siblings.
1777
1778 If optional arg depth is greater than current depth, then we're
1779 opening a new level, and return 0.
1780
1781 If less than this depth, ascend to that depth and count..."
1782
1783 (save-excursion
1784 (cond ((and depth (<= depth 0) 0))
1785 ((or (not depth) (= depth (outline-depth)))
1786 (let ((index 1))
1787 (while (outline-previous-sibling (outline-recent-depth) nil)
1788 (setq index (1+ index)))
1789 index))
1790 ((< depth (outline-recent-depth))
1791 (outline-ascend-to-depth depth)
1792 (outline-sibling-index))
1793 (0))))
1794 ;;;_ > outline-distinctive-bullet (bullet)
1795 (defun outline-distinctive-bullet (bullet)
1796 "True if bullet is one of those on outline-distinctive-bullets-string."
1797 (string-match (regexp-quote bullet) outline-distinctive-bullets-string))
1798 ;;;_ > outline-numbered-type-prefix (&optional prefix)
1799 (defun outline-numbered-type-prefix (&optional prefix)
1800 "True if current header prefix bullet is numbered bullet."
1801 (and outline-numbered-bullet
1802 (string= outline-numbered-bullet
1803 (if prefix
1804 (outline-get-prefix-bullet prefix)
1805 (outline-get-bullet)))))
1806 ;;;_ > outline-bullet-for-depth (&optional depth)
1807 (defun outline-bullet-for-depth (&optional depth)
1808 "Return outline topic bullet suited to DEPTH, or for current depth if none
1809 specified."
1810 ;; Find bullet in plain-bullets-string modulo DEPTH.
1811 (if outline-stylish-prefixes
1812 (char-to-string (aref outline-plain-bullets-string
1813 (% (max 0 (- depth 2))
1814 outline-plain-bullets-string-len)))
1815 outline-primary-bullet)
1816 )
1817
1818 ;;;_ - Topic Production
1819 ;;;_ > outline-make-topic-prefix (&optional prior-bullet
1820 (defun outline-make-topic-prefix (&optional prior-bullet
1821 new
1822 depth
1823 solicit
1824 number-control
1825 index)
1826 ;; Depth null means use current depth, non-null means we're either
1827 ;; opening a new topic after current topic, lower or higher, or we're
1828 ;; changing level of current topic.
1829 ;; Solicit dominates specified bullet-char.
1830 ;;;_ . Doc string:
1831 "Generate a topic prefix suitable for optional arg DEPTH, or current
1832 depth if not specified.
1833
1834 All the arguments are optional.
1835
1836 PRIOR-BULLET indicates the bullet of the prefix being changed, or
1837 nil if none. This bullet may be preserved (other options
1838 notwithstanding) if it is on the outline-distinctive-bullets-string,
1839 for instance.
1840
1841 Second arg NEW indicates that a new topic is being opened after the
1842 topic at point, if non-nil. Default bullet for new topics, eg, may
1843 be set (contingent to other args) to numbered bullets if previous
1844 sibling is one. The implication otherwise is that the current topic
1845 is being adjusted - shifted or rebulleted - and we don't consider
1846 bullet or previous sibling.
1847
1848 Third arg DEPTH forces the topic prefix to that depth, regardless of
1849 the current topics' depth.
1850
1851 Fourth arg SOLICIT non-nil provokes solicitation from the user of a
1852 choice among the valid bullets. (This overrides other all the
1853 options, including, eg, a distinctive PRIOR-BULLET.)
1854
1855 Fifth arg, NUMBER-CONTROL, matters only if 'outline-numbered-bullet'
1856 is non-nil *and* soliciting was not explicitly invoked. Then
1857 NUMBER-CONTROL non-nil forces prefix to either numbered or
1858 denumbered format, depending on the value of the sixth arg, INDEX.
1859
1860 \(Note that NUMBER-CONTROL does *not* apply to level 1 topics. Sorry...)
1861
1862 If NUMBER-CONTROL is non-nil and sixth arg INDEX is non-nil then
1863 the prefix of the topic is forced to be numbered. Non-nil
1864 NUMBER-CONTROL and nil INDEX forces non-numbered format on the
1865 bullet. Non-nil NUMBER-CONTROL and non-nil, non-number INDEX means
1866 that the index for the numbered prefix will be derived, by counting
1867 siblings back to start of level. If INDEX is a number, then that
1868 number is used as the index for the numbered prefix (allowing, eg,
1869 sequential renumbering to not requre this function counting back the
1870 index for each successive sibling)."
1871 ;;;_ . Code:
1872 ;; The options are ordered in likely frequence of use, most common
1873 ;; highest, least lowest. Ie, more likely to be doing prefix
1874 ;; adjustments than soliciting, and yet more than numbering.
1875 ;; Current prefix is least dominant, but most likely to be commonly
1876 ;; specified...
1877
1878 (let* (body
1879 numbering
1880 denumbering
1881 (depth (or depth (outline-depth)))
1882 (header-lead outline-header-prefix)
1883 (bullet-char
1884
1885 ;; Getting value for bullet char is practically the whole job:
1886
1887 (cond
1888 ; Simplest situation - level 1:
1889 ((<= depth 1) (setq header-lead "") outline-primary-bullet)
1890 ; Simple, too: all asterisks:
1891 (outline-old-style-prefixes
1892 ;; Cheat - make body the whole thing, null out header-lead and
1893 ;; bullet-char:
1894 (setq body (make-string depth
1895 (string-to-char outline-primary-bullet)))
1896 (setq header-lead "")
1897 "")
1898
1899 ;; (Neither level 1 nor old-style, so we're space padding.
1900 ;; Sneak it in the condition of the next case, whatever it is.)
1901
1902 ;; Solicitation overrides numbering and other cases:
1903 ((progn (setq body (make-string (- depth 2) ?\ ))
1904 ;; The actual condition:
1905 solicit)
1906 (let* ((got (outline-solicit-alternate-bullet depth)))
1907 ;; Gotta check whether we're numbering and got a numbered bullet:
1908 (setq numbering (and outline-numbered-bullet
1909 (not (and number-control (not index)))
1910 (string= got outline-numbered-bullet)))
1911 ;; Now return what we got, regardless:
1912 got))
1913
1914 ;; Numbering invoked through args:
1915 ((and outline-numbered-bullet number-control)
1916 (if (setq numbering (not (setq denumbering (not index))))
1917 outline-numbered-bullet
1918 (if (and current-bullet
1919 (not (string= outline-numbered-bullet
1920 current-bullet)))
1921 current-bullet
1922 (outline-bullet-for-depth depth))))
1923
1924 ;;; Neither soliciting nor controlled numbering ;;;
1925 ;;; (may be controlled denumbering, tho) ;;;
1926
1927 ;; Check wrt previous sibling:
1928 ((and new ; only check for new prefixes
1929 (<= depth (outline-depth))
1930 outline-numbered-bullet ; ... & numbering enabled
1931 (not denumbering)
1932 (let ((sibling-bullet
1933 (save-excursion
1934 ;; Locate correct sibling:
1935 (or (>= depth (outline-depth))
1936 (outline-ascend-to-depth depth))
1937 (outline-get-bullet))))
1938 (if (and sibling-bullet
1939 (string= outline-numbered-bullet sibling-bullet))
1940 (setq numbering sibling-bullet)))))
1941
1942 ;; Distinctive prior bullet?
1943 ((and prior-bullet
1944 (outline-distinctive-bullet prior-bullet)
1945 ;; Either non-numbered:
1946 (or (not (and outline-numbered-bullet
1947 (string= prior-bullet outline-numbered-bullet)))
1948 ;; or numbered, and not denumbering:
1949 (setq numbering (not denumbering)))
1950 ;; Here 'tis:
1951 prior-bullet))
1952
1953 ;; Else, standard bullet per depth:
1954 ((outline-bullet-for-depth depth)))))
1955
1956 (concat header-lead
1957 body
1958 bullet-char
1959 (if numbering
1960 (format "%d" (cond ((and index (numberp index)) index)
1961 (new (1+ (outline-sibling-index depth)))
1962 ((outline-sibling-index))))))
1963 )
1964 )
1965 ;;;_ > outline-open-topic (relative-depth &optional before)
1966 (defun outline-open-topic (relative-depth &optional before)
1967 "Open a new topic at depth DEPTH. New topic is situated after current
1968 one, unless optional flag BEFORE is non-nil, or unless current line
1969 is complete empty (not even whitespace), in which case open is done
1970 on current line.
1971
1972 Nuances:
1973
1974 - Creation of new topics is with respect to the visible topic
1975 containing the cursor, regardless of intervening concealed ones.
1976
1977 - New headers are generally created after/before the body of a
1978 topic. However, they are created right at cursor location if the
1979 cursor is on a blank line, even if that breaks the current topic
1980 body. This is intentional, to provide a simple means for
1981 deliberately dividing topic bodies.
1982
1983 - Double spacing of topic lists is preserved. Also, the first
1984 level two topic is created double-spaced (and so would be
1985 subsequent siblings, if that's left intact). Otherwise,
1986 single-spacing is used.
1987
1988 - Creation of sibling or nested topics is with respect to the topic
1989 you're starting from, even when creating backwards. This way you
1990 can easily create a sibling in front of the current topic without
1991 having to go to its preceeding sibling, and then open forward
1992 from there."
1993
1994 (let* ((depth (+ (outline-current-depth) relative-depth))
1995 (opening-on-blank (if (looking-at "^\$")
1996 (not (setq before nil))))
1997 opening-numbered ; Will get while computing ref-topic, below
1998 ref-depth ; Will get while computing ref-topic, next
1999 (ref-topic (save-excursion
2000 (cond ((< relative-depth 0)
2001 (outline-ascend-to-depth depth))
2002 ((>= relative-depth 1) nil)
2003 (t (outline-back-to-current-heading)))
2004 (setq ref-depth (outline-recent-depth))
2005 (setq opening-numbered
2006 (save-excursion
2007 (and outline-numbered-bullet
2008 (or (<= relative-depth 0)
2009 (outline-descend-to-depth depth))
2010 (if (outline-numbered-type-prefix)
2011 outline-numbered-bullet))))
2012 (point)))
2013 dbl-space
2014 doing-beginning)
2015
2016 (if (not opening-on-blank)
2017 ; Positioning and vertical
2018 ; padding - only if not
2019 ; opening-on-blank:
2020 (progn
2021 (goto-char ref-topic)
2022 (setq dbl-space ; Determine double space action:
2023 (or (and (<= relative-depth 0) ; not descending;
2024 (save-excursion
2025 ;; at b-o-b or preceeded by a blank line?
2026 (or (> 0 (forward-line -1))
2027 (looking-at "^\\s-*$")
2028 (bobp)))
2029 (save-excursion
2030 ;; succeeded by a blank line?
2031 (outline-end-of-current-subtree)
2032 (bolp)))
2033 (and (= ref-depth 1)
2034 (or before
2035 (= depth 1)
2036 (save-excursion
2037 ;; Don't already have following
2038 ;; vertical padding:
2039 (not (outline-pre-next-preface)))))))
2040
2041 ; Position to prior heading,
2042 ; if inserting backwards, and
2043 ; not going outwards:
2044 (if (and before (>= relative-depth 0))
2045 (progn (outline-back-to-current-heading)
2046 (setq doing-beginning (bobp))
2047 (if (not (bobp))
2048 (outline-previous-heading)))
2049 (if (and before (bobp))
2050 (outline-unprotected (open-line 1))))
2051
2052 (if (<= relative-depth 0)
2053 ;; Not going inwards, don't snug up:
2054 (if doing-beginning
2055 (outline-unprotected (open-line (if dbl-space 2 1)))
2056 (if before
2057 (progn (end-of-line)
2058 (outline-pre-next-preface)
2059 (while (= ?\r (following-char))
2060 (forward-char 1))
2061 (if (not (looking-at "^$"))
2062 (outline-unprotected (open-line 1))))
2063 (outline-end-of-current-subtree)))
2064 ;; Going inwards - double-space if first offspring is,
2065 ;; otherwise snug up.
2066 (end-of-line) ; So we skip any concealed progeny.
2067 (outline-pre-next-preface)
2068 (if (bolp)
2069 ;; Blank lines between current header body and next
2070 ;; header - get to last substantive (non-white-space)
2071 ;; line in body:
2072 (re-search-backward "[^ \t\n]" nil t))
2073 (if (save-excursion
2074 (outline-next-heading)
2075 (if (> (outline-recent-depth) ref-depth)
2076 ;; This is an offspring.
2077 (progn (forward-line -1)
2078 (looking-at "^\\s-*$"))))
2079 (progn (forward-line 1)
2080 (outline-unprotected (open-line 1))))
2081 (end-of-line))
2082 ;;(if doing-beginning (goto-char doing-beginning))
2083 (if (not (bobp))
2084 (progn (if (and (not (> depth ref-depth))
2085 (not before))
2086 (outline-unprotected (open-line 1))
2087 (if (> depth ref-depth)
2088 (outline-unprotected (newline 1))
2089 (if dbl-space
2090 (outline-unprotected (open-line 1))
2091 (if (not before)
2092 (outline-unprotected (newline 1))))))
2093 (if dbl-space
2094 (outline-unprotected (newline 1)))
2095 (if (and (not (eobp))
2096 (not (bolp)))
2097 (forward-char 1))))
2098 ))
2099 (insert-string (concat (outline-make-topic-prefix opening-numbered
2100 t
2101 depth)
2102 " "))
2103
2104 ;;(if doing-beginning (save-excursion (newline (if dbl-space 2 1))))
2105
2106
2107 (outline-rebullet-heading nil ;;; solicit
2108 depth ;;; depth
2109 nil ;;; number-control
2110 nil ;;; index
2111 t) (end-of-line)
2112 )
2113 )
2114 ;;;_ . open-topic contingencies
2115 ;;;_ ; base topic - one from which open was issued
2116 ;;;_ , beginning char
2117 ;;;_ , amount of space before will be used, unless openning in place
2118 ;;;_ , end char will be used, unless opening before (and it still may)
2119 ;;;_ ; absolute depth of new topic
2120 ;;;_ ! insert in place - overrides most stuff
2121 ;;;_ ; relative depth of new re base
2122 ;;;_ ; before or after base topic
2123 ;;;_ ; spacing around topic, if any, prior to new topic and at same depth
2124 ;;;_ ; buffer boundaries - special provisions for beginning and end ob
2125 ;;;_ ; level 1 topics have special provisions also - double space.
2126 ;;;_ ; location of new topic
2127 ;;;_ .
2128 ;;;_ > outline-open-subtopic (arg)
2129 (defun outline-open-subtopic (arg)
2130 "Open new topic header at deeper level than the current one.
2131
2132 Negative universal arg means to open deeper, but place the new topic
2133 prior to the current one."
2134 (interactive "p")
2135 (outline-open-topic 1 (> 0 arg)))
2136 ;;;_ > outline-open-sibtopic (arg)
2137 (defun outline-open-sibtopic (arg)
2138 "Open new topic header at same level as the current one. Negative
2139 universal arg means to place the new topic prior to the current
2140 one."
2141 (interactive "p")
2142 (outline-open-topic 0 (> 0 arg)))
2143 ;;;_ > outline-open-supertopic (arg)
2144 (defun outline-open-supertopic (arg)
2145 "Open new topic header at shallower level than the current one.
2146 Negative universal arg means to open shallower, but place the new
2147 topic prior to the current one."
2148
2149 (interactive "p")
2150 (outline-open-topic -1 (> 0 arg)))
2151
2152 ;;;_ - Outline Alteration
2153 ;;;_ : Topic Modification
2154 ;;;_ = outline-former-auto-filler
2155 (defvar outline-former-auto-filler nil
2156 "Name of modal fill function being wrapped by outline-auto-fill.")
2157 ;;;_ > outline-auto-fill ()
2158 (defun outline-auto-fill ()
2159 "Do normal autofill, maintaining outline hanging topic indentation
2160 if outline-use-hanging-indents is set."
2161 (let ((fill-prefix (if outline-use-hanging-indents
2162 ;; Check for topic header indentation:
2163 (save-excursion
2164 (beginning-of-line)
2165 (if (looking-at outline-regexp)
2166 ;; ... construct indentation to account for
2167 ;; length of topic prefix:
2168 (make-string (progn (outline-end-of-prefix)
2169 (current-column))
2170 ?\ ))))))
2171 (if (or outline-former-auto-filler outline-use-hanging-indents)
2172 (do-auto-fill))))
2173 ;;;_ > outline-reindent-body (old-depth new-depth &optional number)
2174 (defun outline-reindent-body (old-depth new-depth &optional number)
2175 "Reindent body lines which were indented at old-depth to new-depth.
2176
2177 Optional arg NUMBER indicates numbering is being added, and it must
2178 be accomodated.
2179
2180 Note that refill of indented paragraphs is not done."
2181
2182 (save-excursion
2183 (outline-end-of-prefix)
2184 (let* ((new-margin (current-column))
2185 excess old-indent-begin old-indent-end
2186 curr-ind
2187 ;; We want the column where the header-prefix text started
2188 ;; *before* the prefix was changed, so we infer it relative
2189 ;; to the new margin and the shift in depth:
2190 (old-margin (+ old-depth (- new-margin new-depth))))
2191
2192 ;; Process lines up to (but excluding) next topic header:
2193 (outline-unprotected
2194 (save-match-data
2195 (while
2196 (and (re-search-forward "[\n\r]\\(\\s-*\\)"
2197 nil
2198 t)
2199 ;; Register the indent data, before we reset the
2200 ;; match data with a subsequent 'looking-at':
2201 (setq old-indent-begin (match-beginning 1)
2202 old-indent-end (match-end 1))
2203 (not (looking-at outline-regexp)))
2204 (if (> 0 (setq excess (- (current-column)
2205 old-margin)))
2206 ;; Text starts left of old margin - don't adjust:
2207 nil
2208 ;; Text was hanging at or right of old left margin -
2209 ;; reindent it, preserving its existing indentation
2210 ;; beyond the old margin:
2211 (delete-region old-indent-begin old-indent-end)
2212 (indent-to (+ new-margin excess)))))))))
2213 ;;;_ > outline-rebullet-current-heading (arg)
2214 (defun outline-rebullet-current-heading (arg)
2215 "Like non-interactive version 'outline-rebullet-heading', but work on
2216 \(only) visible heading containing point.
2217
2218 With repeat count, solicit for bullet."
2219 (interactive "P")
2220 (save-excursion (outline-back-to-current-heading)
2221 (outline-end-of-prefix)
2222 (outline-rebullet-heading (not arg) ;;; solicit
2223 nil ;;; depth
2224 nil ;;; number-control
2225 nil ;;; index
2226 t) ;;; do-successors
2227 )
2228 )
2229 ;;;_ > outline-rebullet-heading (&optional solicit ...)
2230 (defun outline-rebullet-heading (&optional solicit
2231 new-depth
2232 number-control
2233 index
2234 do-successors)
2235
2236 "Adjust bullet of current topic prefix.
2237
2238 All args are optional.
2239
2240 If SOLICIT is non-nil then the choice of bullet is solicited from
2241 user. Otherwise the distinctiveness of the bullet or the topic
2242 depth determines it.
2243
2244 Second arg DEPTH forces the topic prefix to that depth, regardless
2245 of the topics current depth.
2246
2247 Third arg NUMBER-CONTROL can force the prefix to or away from
2248 numbered form. It has effect only if 'outline-numbered-bullet' is
2249 non-nil and soliciting was not explicitly invoked (via first arg).
2250 Its effect, numbering or denumbering, then depends on the setting
2251 of the forth arg, INDEX.
2252
2253 If NUMBER-CONTROL is non-nil and forth arg INDEX is nil, then the
2254 prefix of the topic is forced to be non-numbered. Null index and
2255 non-nil NUMBER-CONTROL forces denumbering. Non-nil INDEX (and
2256 non-nil NUMBER-CONTROL) forces a numbered-prefix form. If non-nil
2257 INDEX is a number, then that number is used for the numbered
2258 prefix. Non-nil and non-number means that the index for the
2259 numbered prefix will be derived by outline-make-topic-prefix.
2260
2261 Fifth arg DO-SUCCESSORS t means re-resolve count on succeeding
2262 siblings.
2263
2264 Cf vars 'outline-stylish-prefixes', 'outline-old-style-prefixes',
2265 and 'outline-numbered-bullet', which all affect the behavior of
2266 this function."
2267
2268 (let* ((current-depth (outline-depth))
2269 (new-depth (or new-depth current-depth))
2270 (mb outline-recent-prefix-beginning)
2271 (me outline-recent-prefix-end)
2272 (current-bullet (buffer-substring (- me 1) me))
2273 (new-prefix (outline-make-topic-prefix current-bullet
2274 nil
2275 new-depth
2276 solicit
2277 number-control
2278 index)))
2279
2280 ;; Is new one is identical to old?
2281 (if (and (= current-depth new-depth)
2282 (string= current-bullet
2283 (substring new-prefix (1- (length new-prefix)))))
2284 ;; Nothing to do:
2285 t
2286
2287 ;; New prefix probably different from old:
2288 ; get rid of old one:
2289 (outline-unprotected (delete-region mb me))
2290 (goto-char mb)
2291 ; Dispense with number if
2292 ; numbered-bullet prefix:
2293 (if (and outline-numbered-bullet
2294 (string= outline-numbered-bullet current-bullet)
2295 (looking-at "[0-9]+"))
2296 (outline-unprotected
2297 (delete-region (match-beginning 0)(match-end 0))))
2298
2299 ; Put in new prefix:
2300 (outline-unprotected (insert-string new-prefix))
2301
2302 ;; Reindent the body if elected and margin changed:
2303 (if (and outline-reindent-bodies
2304 (not (= new-depth current-depth)))
2305 (outline-reindent-body current-depth new-depth))
2306
2307 ;; Recursively rectify successive siblings of orig topic if
2308 ;; caller elected for it:
2309 (if do-successors
2310 (save-excursion
2311 (while (outline-next-sibling new-depth nil)
2312 (setq index
2313 (cond ((numberp index) (1+ index))
2314 ((not number-control) (outline-sibling-index))))
2315 (if (outline-numbered-type-prefix)
2316 (outline-rebullet-heading nil ;;; solicit
2317 new-depth ;;; new-depth
2318 number-control;;; number-control
2319 index ;;; index
2320 nil))))) ;;;(dont!)do-successors
2321 ) ; (if (and (= current-depth new-depth)...))
2322 ) ; let* ((current-depth (outline-depth))...)
2323 ) ; defun
2324 ;;;_ > outline-rebullet-topic (arg)
2325 (defun outline-rebullet-topic (arg)
2326 "Like outline-rebullet-topic-grunt, but start from topic visible at point.
2327 Descends into invisible as well as visible topics, however.
2328
2329 With repeat count, shift topic depth by that amount."
2330 (interactive "P")
2331 (let ((start-col (current-column))
2332 (was-eol (eolp)))
2333 (save-excursion
2334 ;; Normalize arg:
2335 (cond ((null arg) (setq arg 0))
2336 ((listp arg) (setq arg (car arg))))
2337 ;; Fill the user in, in case we're shifting a big topic:
2338 (if (not (zerop arg)) (message "Shifting..."))
2339 (outline-back-to-current-heading)
2340 (if (<= (+ (outline-recent-depth) arg) 0)
2341 (error "Attempt to shift topic below level 1"))
2342 (outline-rebullet-topic-grunt arg)
2343 (if (not (zerop arg)) (message "Shifting... done.")))
2344 (move-to-column (max 0 (+ start-col arg)))))
2345 ;;;_ > outline-rebullet-topic-grunt (&optional relative-depth ...)
2346 (defun outline-rebullet-topic-grunt (&optional relative-depth
2347 starting-depth
2348 starting-point
2349 index
2350 do-successors)
2351
2352 "Rebullet the topic at point, visible or invisible, and all
2353 contained subtopics. See outline-rebullet-heading for rebulleting
2354 behavior.
2355
2356 All arguments are optional.
2357
2358 First arg RELATIVE-DEPTH means to shift the depth of the entire
2359 topic that amount.
2360
2361 The rest of the args are for internal recursive use by the function
2362 itself. The are STARTING-DEPTH, STARTING-POINT, and INDEX."
2363
2364 (let* ((relative-depth (or relative-depth 0))
2365 (new-depth (outline-depth))
2366 (starting-depth (or starting-depth new-depth))
2367 (on-starting-call (null starting-point))
2368 (index (or index
2369 ;; Leave index null on starting call, so rebullet-heading
2370 ;; calculates it at what might be new depth:
2371 (and (or (zerop relative-depth)
2372 (not on-starting-call))
2373 (outline-sibling-index))))
2374 (moving-outwards (< 0 relative-depth))
2375 (starting-point (or starting-point (point))))
2376
2377 ;; Sanity check for excessive promotion done only on starting call:
2378 (and on-starting-call
2379 moving-outwards
2380 (> 0 (+ starting-depth relative-depth))
2381 (error "Attempt to shift topic out beyond level 1.")) ;;; ====>
2382
2383 (cond ((= starting-depth new-depth)
2384 ;; We're at depth to work on this one:
2385 (outline-rebullet-heading nil ;;; solicit
2386 (+ starting-depth ;;; starting-depth
2387 relative-depth)
2388 nil ;;; number
2389 index ;;; index
2390 ;; Every contained topic will get hit,
2391 ;; and we have to get to outside ones
2392 ;; deliberately:
2393 nil) ;;; do-successors
2394 ;; ... and work on subsequent ones which are at greater depth:
2395 (setq index 0)
2396 (outline-next-heading)
2397 (while (and (not (eobp))
2398 (< starting-depth (outline-recent-depth)))
2399 (setq index (1+ index))
2400 (outline-rebullet-topic-grunt relative-depth ;;; relative-depth
2401 (1+ starting-depth);;;starting-depth
2402 starting-point ;;; starting-point
2403 index))) ;;; index
2404
2405 ((< starting-depth new-depth)
2406 ;; Rare case - subtopic more than one level deeper than parent.
2407 ;; Treat this one at an even deeper level:
2408 (outline-rebullet-topic-grunt relative-depth ;;; relative-depth
2409 new-depth ;;; starting-depth
2410 starting-point ;;; starting-point
2411 index))) ;;; index
2412
2413 (if on-starting-call
2414 (progn
2415 ;; Rectify numbering of former siblings of the adjusted topic,
2416 ;; if topic has changed depth
2417 (if (or do-successors
2418 (and (not (zerop relative-depth))
2419 (or (= (outline-recent-depth) starting-depth)
2420 (= (outline-recent-depth) (+ starting-depth
2421 relative-depth)))))
2422 (outline-rebullet-heading nil nil nil nil t))
2423 ;; Now rectify numbering of new siblings of the adjusted topic,
2424 ;; if depth has been changed:
2425 (progn (goto-char starting-point)
2426 (if (not (zerop relative-depth))
2427 (outline-rebullet-heading nil nil nil nil t)))))
2428 )
2429 )
2430 ;;;_ > outline-renumber-to-depth (&optional depth)
2431 (defun outline-renumber-to-depth (&optional depth)
2432 "Renumber siblings at current depth, from point, and shallower
2433 if optional arg DEPTH is less than current depth.
2434
2435 Returns final depth."
2436
2437 ;; Proceed by level, processing subsequent siblings on each,
2438 ;; ascending until we get shallower than the start depth:
2439
2440 (let ((ascender (outline-depth)))
2441 (while (and (not (eobp))
2442 (outline-depth)
2443 (>= (outline-recent-depth) depth)
2444 (>= ascender depth))
2445 ; Skip over all topics at
2446 ; lesser depths, which can not
2447 ; have been disturbed:
2448 (while (and (not (eobp))
2449 (> (outline-recent-depth) ascender))
2450 (outline-next-heading))
2451 ; Prime ascender for ascension:
2452 (setq ascender (1- (outline-recent-depth)))
2453 (if (>= (outline-recent-depth) depth)
2454 (outline-rebullet-heading nil ;;; solicit
2455 nil ;;; depth
2456 nil ;;; number-control
2457 nil ;;; index
2458 t))));;; do-successors
2459 (outline-recent-depth))
2460 ;;;_ > outline-number-siblings (&optional denumber)
2461 (defun outline-number-siblings (&optional denumber)
2462 "Assign numbered topic prefix to this topic and its siblings.
2463
2464 With universal argument, denumber - assign default bullet to this
2465 topic and its siblings.
2466
2467 With repeated universal argument (`^U^U'), solicit bullet for each
2468 rebulleting each topic at this level."
2469
2470 (interactive "P")
2471
2472 (save-excursion
2473 (outline-back-to-current-heading)
2474 (outline-beginning-of-level)
2475 (let ((depth (outline-recent-depth))
2476 (index (if (not denumber) 1))
2477 (use-bullet (equal '(16) denumber))
2478 (more t))
2479 (while more
2480 (outline-rebullet-heading use-bullet ;;; solicit
2481 depth ;;; depth
2482 t ;;; number-control
2483 index ;;; index
2484 nil) ;;; do-successors
2485 (if index (setq index (1+ index)))
2486 (setq more (outline-next-sibling depth nil))))))
2487 ;;;_ > outline-shift-in (arg)
2488 (defun outline-shift-in (arg)
2489 "Decrease prefix depth of current heading and any topics collapsed
2490 within it."
2491 (interactive "p")
2492 (outline-rebullet-topic arg))
2493 ;;;_ > outline-shift-out (arg)
2494 (defun outline-shift-out (arg)
2495 "Decrease prefix depth of current heading and any topics collapsed
2496 within it."
2497 (interactive "p")
2498 (outline-rebullet-topic (* arg -1)))
2499 ;;;_ : Surgery (kill-ring) functions with special provisions for outlines:
2500 ;;;_ > outline-kill-line (&optional arg)
2501 (defun outline-kill-line (&optional arg)
2502 "Kill line, adjusting subsequent lines suitably for outline mode."
2503
2504 (interactive "*P")
2505 (if (not (and (outline-mode-p) ; active outline mode,
2506 outline-numbered-bullet ; numbers may need adjustment,
2507 (bolp) ; may be clipping topic head,
2508 (looking-at outline-regexp))) ; are clipping topic head.
2509 ;; Above conditions do not obtain - just do a regular kill:
2510 (kill-line arg)
2511 ;; Ah, have to watch out for adjustments:
2512 (let* ((depth (outline-depth)))
2513 ; Do the kill:
2514 (kill-line arg)
2515 ; Provide some feedback:
2516 (sit-for 0)
2517 (save-excursion
2518 ; Start with the topic
2519 ; following killed line:
2520 (if (not (looking-at outline-regexp))
2521 (outline-next-heading))
2522 (outline-renumber-to-depth depth)))))
2523 ;;;_ > outline-kill-topic ()
2524 (defun outline-kill-topic ()
2525 "Kill topic together with subtopics.
2526
2527 Leaves primary topic's trailing vertical whitespace, if any."
2528
2529 ;; Some finagling is done to make complex topic kills appear faster
2530 ;; than they actually are. A redisplay is performed immediately
2531 ;; after the region is disposed of, though the renumbering process
2532 ;; has yet to be performed. This means that there may appear to be
2533 ;; a lag *after* the kill has been performed.
2534
2535 (interactive)
2536 (let* ((beg (prog1 (outline-back-to-current-heading)(beginning-of-line)))
2537 (depth (outline-recent-depth)))
2538 (outline-end-of-current-subtree)
2539 (if (not (eobp))
2540 (if (or (not (looking-at "^$"))
2541 ;; A blank line - cut it with this topic *unless* this
2542 ;; is the last topic at this level, in which case
2543 ;; we'll leave the blank line as part of the
2544 ;; containing topic:
2545 (save-excursion
2546 (and (outline-next-heading)
2547 (>= (outline-recent-depth) depth))))
2548 (forward-char 1)))
2549
2550 (kill-region beg (point))
2551 (sit-for 0)
2552 (save-excursion
2553 (outline-renumber-to-depth depth))))
2554 ;;;_ > outline-yank-processing ()
2555 (defun outline-yank-processing (&optional arg)
2556
2557 "Incidental outline-specific business to be done just after text yanks.
2558 Does depth adjustment of yanked topics, when:
2559
2560 1 the stuff being yanked starts with a valid outline header prefix, and
2561 2 it is being yanked at the end of a line which consists of only a valid
2562 topic prefix.
2563
2564 Also, adjusts numbering of subsequent siblings when appropropriate.
2565
2566 Depth adjustment alters the depth of all the topics being yanked
2567 the amount it takes to make the first topic have the depth of the
2568 header into which it's being yanked.
2569
2570 The point is left in front of yanked, adjusted topics, rather than
2571 at the end (and vice-versa with the mark). Non-adjusted yanks,
2572 however, are left exactly like normal, non-outline-specific yanks."
2573
2574 (interactive "*P")
2575 ; Get to beginning, leaving
2576 ; region around subject:
2577 (if (< (mark-marker) (point))
2578 (exchange-point-and-mark))
2579 (let* ((subj-beg (point))
2580 (subj-end (mark-marker))
2581 ;; 'resituate' if yanking an entire topic into topic header:
2582 (resituate (and (outline-e-o-prefix-p)
2583 (looking-at (concat "\\(" outline-regexp "\\)"))
2584 (outline-prefix-data (match-beginning 1)
2585 (match-end 1))))
2586 ;; 'rectify-numbering' if resituating (where several topics may
2587 ;; be resituating) or yanking a topic into a topic slot (bol):
2588 (rectify-numbering (or resituate
2589 (and (bolp) (looking-at outline-regexp)))))
2590 (if resituate
2591 ; The yanked stuff is a topic:
2592 (let* ((prefix-len (- (match-end 1) subj-beg))
2593 (subj-depth (outline-recent-depth))
2594 (prefix-bullet (outline-recent-bullet))
2595 (adjust-to-depth
2596 ;; Nil if adjustment unnecessary, otherwise depth to which
2597 ;; adjustment should be made:
2598 (save-excursion
2599 (and (goto-char subj-end)
2600 (eolp)
2601 (goto-char subj-beg)
2602 (and (looking-at outline-regexp)
2603 (progn
2604 (beginning-of-line)
2605 (not (= (point) subj-beg)))
2606 (looking-at outline-regexp)
2607 (outline-prefix-data (match-beginning 0)
2608 (match-end 0)))
2609 (outline-recent-depth))))
2610 done
2611 (more t))
2612 (setq rectify-numbering outline-numbered-bullet)
2613 (if adjust-to-depth
2614 ; Do the adjustment:
2615 (progn
2616 (message "... yanking") (sit-for 0)
2617 (save-restriction
2618 (narrow-to-region subj-beg subj-end)
2619 ; Trim off excessive blank
2620 ; line at end, if any:
2621 (goto-char (point-max))
2622 (if (looking-at "^$")
2623 (outline-unprotected (delete-char -1)))
2624 ; Work backwards, with each
2625 ; shallowest level,
2626 ; successively excluding the
2627 ; last processed topic from
2628 ; the narrow region:
2629 (while more
2630 (outline-back-to-current-heading)
2631 ; go as high as we can in each bunch:
2632 (while (outline-ascend-to-depth (1- (outline-depth))))
2633 (save-excursion
2634 (outline-rebullet-topic-grunt (- adjust-to-depth
2635 subj-depth))
2636 (outline-depth))
2637 (if (setq more (not (bobp)))
2638 (progn (widen)
2639 (forward-char -1)
2640 (narrow-to-region subj-beg (point))))))
2641 (message "")
2642 ;; Preserve new bullet if it's a distinctive one, otherwise
2643 ;; use old one:
2644 (if (string-match (regexp-quote prefix-bullet)
2645 outline-distinctive-bullets-string)
2646 ; Delete from bullet of old to
2647 ; before bullet of new:
2648 (progn
2649 (beginning-of-line)
2650 (delete-region (point) subj-beg)
2651 (set-marker (mark-marker) subj-end)
2652 (goto-char subj-beg)
2653 (outline-end-of-prefix))
2654 ; Delete base subj prefix,
2655 ; leaving old one:
2656 (delete-region (point) (+ (point)
2657 prefix-len
2658 (- adjust-to-depth subj-depth)))
2659 ; and delete residual subj
2660 ; prefix digits and space:
2661 (while (looking-at "[0-9]") (delete-char 1))
2662 (if (looking-at " ") (delete-char 1))))
2663 (exchange-point-and-mark))))
2664 (if rectify-numbering
2665 (progn
2666 (save-excursion
2667 ; Give some preliminary feedback:
2668 (message "... reconciling numbers") (sit-for 0)
2669 ; ... and renumber, in case necessary:
2670 (goto-char subj-beg)
2671 (if (outline-goto-prefix)
2672 (outline-rebullet-heading nil ;;; solicit
2673 (outline-depth) ;;; depth
2674 nil ;;; number-control
2675 nil ;;; index
2676 t))
2677 (message ""))))
2678 (if (not resituate)
2679 (exchange-point-and-mark))))
2680 ;;;_ > outline-yank (&optional arg)
2681 (defun outline-yank (&optional arg)
2682 "Like yank, with depth and numbering adjustment of yanked topics in
2683 outline mode. Non-topic yanks work no differntly than normal yanks.
2684
2685 If a topic is being yanked into a bare topic prefix, the depth of the
2686 yanked topic is adjusted to the depth of the topic prefix.
2687
2688 1 we're yanking in an outline-mode buffer
2689 2 the stuff being yanked starts with a valid outline header prefix, and
2690 3 it is being yanked at the end of a line which consists of only a valid
2691 topic prefix.
2692
2693 If these conditions hold then the depth of the yanked topics are all
2694 adjusted the amount it takes to make the first one at the depth of the
2695 header into which it's being yanked.
2696
2697 The point is left in front of yanked, adjusted topics, rather than
2698 at the end (and vice-versa with the mark). Non-adjusted yanks,
2699 however, (ones that don't qualify for adjustment) are handled
2700 exactly like normal yanks.
2701
2702 Numbering of yanked topics, and the succesive siblings at the depth
2703 into which they're being yanked, is adjusted.
2704
2705 Outline-yank-pop works with outline-yank just like normal yank-pop
2706 works with normal yank in non-outline buffers."
2707
2708 (interactive "*P")
2709 (setq this-command 'yank)
2710 (yank arg)
2711 (if (outline-mode-p)
2712 (outline-yank-processing)))
2713 ;;;_ > outline-yank-pop (&optional arg)
2714 (defun outline-yank-pop (&optional arg)
2715 "Just like yank-pop, but works like outline-yank when popping
2716 topics just after fresh outline prefixes. Adapts level of popped
2717 stuff to level of fresh prefix.
2718
2719 Note - prefix changes to distinctive bullets will stick, if followed
2720 by pops to non-distinctive yanks. Bug..."
2721
2722 (interactive "*p")
2723 (setq this-command 'yank)
2724 (yank-pop arg)
2725 (if (outline-mode-p)
2726 (outline-yank-processing)))
2727
2728 ;;;_ - Specialty bullet functions
2729 ;;;_ : File Cross references
2730 ;;;_ > outline-resolve-xref ()
2731 (defun outline-resolve-xref ()
2732 "Pop to file associated with current heading, if it has an xref bullet
2733 \(according to setting of 'outline-file-xref-bullet')."
2734 (interactive)
2735 (if (not outline-file-xref-bullet)
2736 (error
2737 "outline cross references disabled - no 'outline-file-xref-bullet'")
2738 (if (not (string= (outline-current-bullet) outline-file-xref-bullet))
2739 (error "current heading lacks cross-reference bullet '%s'"
2740 outline-file-xref-bullet)
2741 (let (file-name)
2742 (save-excursion
2743 (let* ((text-start outline-recent-prefix-end)
2744 (heading-end (progn (outline-pre-next-preface)
2745 (point))))
2746 (goto-char text-start)
2747 (setq file-name
2748 (if (re-search-forward "\\s-\\(\\S-*\\)" heading-end t)
2749 (buffer-substring (match-beginning 1) (match-end 1))))))
2750 (setq file-name
2751 (if (not (= (aref file-name 0) ?:))
2752 (expand-file-name file-name)
2753 ; A registry-files ref, strip the ':'
2754 ; and try to follow it:
2755 (let ((reg-ref (reference-registered-file
2756 (substring file-name 1) nil t)))
2757 (if reg-ref (car (cdr reg-ref))))))
2758 (if (or (file-exists-p file-name)
2759 (if (file-writable-p file-name)
2760 (y-or-n-p (format "%s not there, create one? "
2761 file-name))
2762 (error "%s not found and can't be created" file-name)))
2763 (condition-case failure
2764 (find-file-other-window file-name)
2765 (error failure))
2766 (error "%s not found" file-name))
2767 )
2768 )
2769 )
2770 )
2771 ;;;_ > outline-to-entry-end - Unmaintained compatability - ignore this!
2772 ;-------------------------------------------------------------------
2773 ; Something added solely for use by a "smart menu" package someone got
2774 ; off the net. I have no idea whether this is appropriate code.
2775
2776 (defvar next-entry-exists nil "Used by outline-to-entry-end, dunno why.")
2777 (defun outline-to-entry-end (&optional include-sub-entries curr-entry-level)
2778 "Go to end of whole entry if optional INCLUDE-SUB-ENTRIES is non-nil.
2779 CURR-ENTRY-LEVEL is an integer representing the length of the current level
2780 string which matched to 'outline-regexp'. If INCLUDE-SUB-ENTRIES is nil,
2781 CURR-ENTRY-LEVEL is not needed."
2782 (while (and (setq next-entry-exists
2783 (re-search-forward outline-regexp nil t))
2784 include-sub-entries
2785 (save-excursion
2786 (beginning-of-line)
2787 (> (outline-depth) curr-entry-level))))
2788 (if next-entry-exists
2789 (progn (beginning-of-line) (point))
2790 (goto-char (point-max))))
2791
2792 ;;; Outline topic prefix and level adjustment funcs:
2793
2794 ;;;_ #5 Exposure Control and Processing
2795
2796 ;;;_ - Fundamental
2797 ;;;_ > outline-flag-region (from to flag)
2798 (defmacro outline-flag-region (from to flag)
2799 "Hides or shows lines from FROM to TO, according to
2800 emacs selective-display FLAG char. Ie, text following flag C-m
2801 \(carriage-return) is hidden until the next C-j (newline) char.
2802
2803 Returns nil iff no changes were effected."
2804 (` (let ((buffer-read-only nil)
2805 (outline-override-protect t))
2806 (subst-char-in-region (, from) (, to)
2807 (if (= (, flag) ?\n) ?\r ?\n)
2808 (, flag) t))))
2809 ;;;_ > outline-flag-current-subtree (flag)
2810 (defun outline-flag-current-subtree (flag)
2811 (save-excursion
2812 (outline-back-to-current-heading)
2813 (outline-flag-region (point)
2814 (progn (outline-end-of-current-subtree) (1- (point)))
2815 flag)))
2816
2817 ;;;_ - Mapping and processing of topics
2818 ;;;_ " See also chart functions, in navigation
2819 ;;;_ > outline-listify-exposed (&optional start end)
2820 (defun outline-listify-exposed (&optional start end)
2821
2822 "Produce a list representing exposed topics in current region.
2823 This list can then be used by 'outline-process-exposed' to manipulate
2824 the subject region.
2825
2826 List is composed of elements that may themselves be lists representing
2827 exposed components in subtopic.
2828
2829 Each component list contains:
2830 - a number representing the depth of the topic,
2831 - a string representing the header-prefix (ref. 'outline-header-prefix'),
2832 - a string representing the bullet character,
2833 - and a series of strings, each containing one line of the exposed
2834 portion of the topic entry."
2835
2836 (interactive "r")
2837 (save-excursion
2838 (let* (strings pad result depth bullet beg next done) ; State vars.
2839 (goto-char start)
2840 (beginning-of-line)
2841 (if (not (outline-goto-prefix)) ; Get initial position within a topic:
2842 (outline-next-visible-heading 1))
2843 (while (and (not done)
2844 (not (eobp)) ; Loop until we've covered the region.
2845 (not (> (point) end)))
2846 (setq depth (outline-recent-depth) ; Current topics' depth,
2847 bullet (outline-recent-bullet) ; ... bullet,
2848 beg (progn (outline-end-of-prefix t) (point))) ; and beginning.
2849 (setq done ; The boundary for the current topic:
2850 (not (outline-next-visible-heading 1)))
2851 (beginning-of-line)
2852 (setq next (point))
2853 (goto-char beg)
2854 (setq strings nil)
2855 (while (> next (point)) ; Get all the exposed text in
2856 (setq strings
2857 (cons (buffer-substring
2858 beg
2859 ;To hidden text or end of line:
2860 (progn
2861 (search-forward "\r"
2862 (save-excursion (end-of-line)
2863 (point))
2864 1)
2865 (if (= (preceding-char) ?\r)
2866 (1- (point))
2867 (point))))
2868 strings))
2869 (if (< (point) next) ; Resume from after hid text, if any.
2870 (forward-line 1))
2871 (setq beg (point)))
2872 ;; Accumulate list for this topic:
2873 (setq result
2874 (cons (append (list depth
2875 outline-header-prefix
2876 bullet)
2877 (nreverse strings))
2878 result)))
2879 ;; Put the list with first at front, to last at back:
2880 (nreverse result))))
2881 ;;;_ > outline-process-exposed (arg &optional tobuf)
2882 (defun outline-process-exposed (&optional func from to frombuf tobuf)
2883 "Apply FUNCTION \(default 'outline-insert-listified) to exposed
2884 portions FROM position TO position \(default region, or the entire
2885 buffer if no region active) in buffer FROMBUF \(default current
2886 buffer) to buffer TOBUF \(default is buffer named like frombuf but
2887 with \"*\" prepended and \" exposed*\" appended).
2888
2889 The function must as its arguments the elements of the list
2890 representations of topic entries produced by outline-listify-exposed."
2891
2892 ; Resolve arguments,
2893 ; defaulting if necessary:
2894 (if (not func) (setq func 'outline-insert-listified))
2895 (if (not (and from to))
2896 (if mark-active
2897 (setq from (region-beginning) to (region-end))
2898 (setq from (point-min) to (point-max))))
2899 (if frombuf
2900 (if (not (bufferp frombuf))
2901 ;; Specified but not a buffer - get it:
2902 (let ((got (get-buffer frombuf)))
2903 (if (not got)
2904 (error (concat "outline-process-exposed: source buffer "
2905 frombuf
2906 " not found."))
2907 (setq frombuf got))))
2908 ;; not specified - default it:
2909 (setq frombuf (current-buffer)))
2910 (if tobuf
2911 (if (not (bufferp tobuf))
2912 (setq tobuf (get-buffer-create tobuf)))
2913 ;; not specified - default it:
2914 (setq tobuf (concat "*" (buffer-name frombuf) " exposed*")))
2915
2916 (let* ((listified (progn (set-buffer frombuf)
2917 (outline-listify-exposed from to)))
2918 (prefix outline-header-prefix) ; ... as set in frombuf.
2919 curr)
2920 (set-buffer tobuf)
2921 (while listified
2922 (setq curr (car listified))
2923 (setq listified (cdr listified))
2924 (apply func (list (car curr) ; depth
2925 (car (cdr curr)) ; header-prefix
2926 (car (cdr (cdr curr))) ; bullet
2927 (cdr (cdr (cdr curr)))))) ; list of text lines
2928 (pop-to-buffer tobuf)))
2929
2930 ;;;_ - Topic-specific
2931 ;;;_ > outline-show-entry ()
2932 ; outline-show-entry basically for isearch dynamic exposure, as is...
2933 (defun outline-show-entry ()
2934 "Like outline-show-current-entry, but reveals an entry that is nested
2935 within hidden topics.
2936
2937 This is a way to give restricted peek at a concealed locality without the
2938 expense of exposing its context, but can leave the outline with aberrant
2939 exposure. outline-hide-current-entry-completely or outline-show-offshoot
2940 should be used after the peek to rectify the exposure."
2941
2942 (interactive)
2943 (save-excursion
2944 (outline-goto-prefix)
2945 (outline-flag-region (if (bobp) (point) (1- (point)))
2946 (or (outline-pre-next-preface) (point))
2947 ?\n)))
2948 ;;;_ > outline-show-children (&optional level strict)
2949 (defun outline-show-children (&optional level strict)
2950
2951 "If point is visible, show all direct subheadings of this heading.
2952 Otherwise, do outline-show-to-offshoot, and then show subheadings.
2953
2954 Optional LEVEL specifies how many levels below the current level
2955 should be shown, or all levels if t. Default is 1.
2956
2957 Optional STRICT means don't resort to -show-to-offshoot, no matter
2958 what. This is basically so -show-to-offshoot, which is called by
2959 this function, can employ the pure offspring-revealing capabilities of
2960 it."
2961
2962 (interactive "p")
2963 (if (and (not strict)
2964 (outline-hidden-p))
2965
2966 (progn (outline-show-to-offshoot) ; Point's concealed, open to expose it.
2967 ;; Then recurse, but with "strict" set so we don't
2968 ;; infinite regress:
2969 (outline-show-children level t))
2970
2971 (save-excursion
2972 (save-restriction
2973 (let* ((start-pt (point))
2974 (chart (outline-chart-subtree))
2975 (e-o-subtree (point))
2976 (to-reveal (outline-chart-to-reveal chart (or level 1))))
2977 (goto-char start-pt)
2978 (if (and strict (= (preceding-char) ?\r))
2979 ;; Concealed root would already have been taken care of,
2980 ;; unless strict was set.
2981 (outline-flag-region (point) (outline-snug-back) ?\n))
2982 (while to-reveal
2983 (goto-char (car to-reveal))
2984 (outline-flag-region (point) (outline-snug-back) ?\n)
2985 (setq to-reveal (cdr to-reveal))))))))
2986 ;;;_ x outline-show-current-children (&optional level strict)
2987 (defun outline-show-current-children (&optional level strict)
2988 "This command was misnamed, 'outline-show-children' is the proper
2989 name. Use it instead.
2990
2991 \(The \"current\" in the name is supposed to imply that it works on
2992 the visible topic containing point, while it really works with respect
2993 to the most immediate topic, concealed or not. I'll leave this old
2994 name around for a bit, but i'll soon activate an annoying message to
2995 warn people about the change, and then deprecate this alias."
2996
2997 (interactive "p")
2998 ;;(beep)
2999 ;;(message (format "Use '%s' instead of '%s' (%s)."
3000 ;; "outline-show-children"
3001 ;; "outline-show-current-children"
3002 ;; (buffer-name (current-buffer))))
3003 (outline-show-children level strict))
3004 ;;;_ > outline-hide-point-reconcile ()
3005 (defun outline-hide-reconcile ()
3006 "Like outline-hide-current-entry, but hides completely if contained within
3007 hidden region.
3008
3009 Specifically intended for aberrant exposure states, like entries that were
3010 exposed by outline-show-entry but are within otherwise concealed regions."
3011 (interactive)
3012 (save-excursion
3013 (outline-goto-prefix)
3014 (outline-flag-region (if (not (bobp)) (1- (point)) (point))
3015 (progn (outline-pre-next-preface)
3016 (if (= ?\r (following-char))
3017 (point)
3018 (1- (point))))
3019 ?\r)))
3020 ;;;_ > outline-show-to-offshoot ()
3021 (defun outline-show-to-offshoot ()
3022 "Like outline-show-entry, but reveals opens all concealed ancestors,
3023 as well.
3024
3025 Like outline-hide-current-entry-completely, useful for rectifying aberrant
3026 exposure states produced by outline-show-entry."
3027
3028 (interactive)
3029 (save-excursion
3030 (let ((orig-pt (point))
3031 (orig-pref (outline-goto-prefix))
3032 (last-at (point))
3033 bag-it)
3034 (while (or bag-it (= (preceding-char) ?\r))
3035 (beginning-of-line)
3036 (if (= last-at (setq last-at (point)))
3037 ;; Oops, we're not making any progress! Show the current
3038 ;; topic completely, and bag this try.
3039 (progn (beginning-of-line)
3040 (outline-show-current-subtree)
3041 (goto-char orig-pt)
3042 (setq bag-it t)
3043 (beep)
3044 (message "%s: %s"
3045 "outline-show-to-offshoot: "
3046 "Aberrant nesting encountered.")))
3047 (outline-show-children)
3048 (goto-char orig-pref))
3049 (goto-char orig-pt)
3050 (outline-show-entry))))
3051 ;;;_ > outline-hide-current-entry ()
3052 (defun outline-hide-current-entry ()
3053 "Hide the body directly following this heading."
3054 (interactive)
3055 (outline-back-to-current-heading)
3056 (save-excursion
3057 (outline-flag-region (point)
3058 (progn (outline-end-of-current-entry) (point))
3059 ?\^M)))
3060 ;;;_ > outline-show-current-entry (&optional arg)
3061 (defun outline-show-current-entry (&optional arg)
3062
3063 "Show body following current heading, or hide the entry if repeat
3064 count."
3065
3066 (interactive "P")
3067 (if arg
3068 (outline-hide-current-entry)
3069 (save-excursion
3070 (outline-flag-region (point)
3071 (progn (outline-end-of-current-entry) (point))
3072 ?\n))))
3073 ;;;_ > outline-hide-current-entry-completely ()
3074 ; ... outline-hide-current-entry-completely also for isearch dynamic exposure:
3075 (defun outline-hide-current-entry-completely ()
3076 "Like outline-hide-current-entry, but conceal topic completely.
3077
3078 Specifically intended for aberrant exposure states, like entries that were
3079 exposed by outline-show-entry but are within otherwise concealed regions."
3080 (interactive)
3081 (save-excursion
3082 (outline-goto-prefix)
3083 (outline-flag-region (if (not (bobp)) (1- (point)) (point))
3084 (progn (outline-pre-next-preface)
3085 (if (= ?\r (following-char))
3086 (point)
3087 (1- (point))))
3088 ?\r)))
3089 ;;;_ > outline-show-current-subtree ()
3090 (defun outline-show-current-subtree ()
3091 "Show everything after this heading at deeper levels."
3092 (interactive)
3093 (outline-flag-current-subtree ?\n))
3094 ;;;_ > outline-hide-current-subtree (&optional just-close)
3095 (defun outline-hide-current-subtree (&optional just-close)
3096
3097 "Hide everything after this heading at deeper levels, or if it's
3098 already closed, and optional arg JUST-CLOSE is nil, hide the current
3099 level."
3100
3101 (interactive)
3102 (let ((orig-eol (save-excursion
3103 (end-of-line)(outline-goto-prefix)(end-of-line)(point))))
3104 (outline-flag-current-subtree ?\^M)
3105 (if (and (= orig-eol (save-excursion (goto-char orig-eol)
3106 (end-of-line)
3107 (point)))
3108 ;; Structure didn't change - try hiding current level:
3109 (if (not just-close)
3110 (outline-up-current-level 1 t)))
3111 (outline-hide-current-subtree))))
3112 ;;;_ > outline-show-current-branches ()
3113 (defun outline-show-current-branches ()
3114 "Show all subheadings of this heading, but not their bodies."
3115 (interactive)
3116 (beginning-of-line)
3117 (outline-show-children t))
3118 ;;;_ > outline-hide-current-leaves ()
3119 (defun outline-hide-current-leaves ()
3120 "Hide the bodies of the current topic and all its' offspring."
3121 (interactive)
3122 (outline-back-to-current-heading)
3123 (outline-hide-region-body (point) (progn (outline-end-of-current-subtree)
3124 (point))))
3125
3126 ;;;_ - Region and beyond
3127 ;;;_ > outline-show-all ()
3128 (defun outline-show-all ()
3129 "Show all of the text in the buffer."
3130 (interactive)
3131 (outline-flag-region (point-min) (point-max) ?\n))
3132 ;;;_ > outline-hide-bodies ()
3133 (defun outline-hide-bodies ()
3134 "Hide all of buffer except headings."
3135 (interactive)
3136 (outline-hide-region-body (point-min) (point-max)))
3137 ;;;_ > outline-hide-region-body (start end)
3138 (defun outline-hide-region-body (start end)
3139 "Hide all body lines in the region, but not headings."
3140 (save-excursion
3141 (save-restriction
3142 (narrow-to-region start end)
3143 (goto-char (point-min))
3144 (while (not (eobp))
3145 (outline-flag-region (point)
3146 (progn (outline-pre-next-preface) (point)) ?\^M)
3147 (if (not (eobp))
3148 (forward-char
3149 (if (looking-at "[\n\r][\n\r]")
3150 2 1)))))))
3151 ;;;_ > outline-expose-topic (spec &optional prev-spec)
3152 (defun outline-expose-topic (spec &optional prev-spec)
3153
3154 "Dictate wholesale exposure scheme for current level.
3155
3156 Unless you want the args to be evaluated, you probably want to use the
3157 frontend `outline-new-exposure', instead.
3158
3159 Cursor is left at start position.
3160
3161 SPEC is either a number or, recursively, a list.
3162
3163 A simple spec \(either a number, one of a few symbols, or the null
3164 list) dictates the overall exposure for the current topic.
3165
3166 Non null lists are complex specs, designating exposure for the current
3167 topic and its respective siblings. The ':' repeat spec is used to
3168 specify exposure for any number of successive siblings, up to the
3169 trailing ones for which there are explicit specs following the ':'.
3170
3171 Simple (numeric and null-list) specs are interpreted as follows:
3172
3173 - Numbers indicate the relative depth to open the corresponding topic.
3174 - negative numbers force the topic to be closed before opening to the
3175 absolute value of the number, so all siblings are open only to
3176 that level.
3177 - positive numbers open to the relative depth indicated by the
3178 number, but do not force already opened subtopics to be closed.
3179 - 0 means to close topic - hide all offspring.
3180 - ':' 'repeat'
3181 apply prior element to all siblings at current level, *up to*
3182 those siblings that would be covered by specs following the ':'
3183 on the list. Ie, apply to all topics at level but the last
3184 ones. \(Only first of multiple colons at same level is
3185 respected - subsequent ones are discarded.)
3186 - '*' completely opens the topic, including bodies.
3187 - '+' shows all the sub headers, but not the bodies
3188 - '-' exposes the body and immediate offspring of the corresponding topic.
3189
3190 If the spec is a list, the first element must be a number, which
3191 dictates the exposure depth of the topic as a whole. Subsequent
3192 elements of the list are nested SPECs, dictating the specific exposure
3193 for the corresponding offspring of the topic.
3194
3195 Examples:
3196 \(outline-expose-topic '(-1 : 0))
3197 Close this and all following topics at current level, exposing
3198 only their immediate children, but close down the last topic
3199 at this current level completely.
3200 \(outline-expose-topic '(-1 () : 1 0))
3201 Close current topic so only the immediate subtopics are shown;
3202 show the children in the second to last topic, and completely
3203 close the last one.
3204 \(outline-expose-topic -2 ': -1 '*))
3205 Expose children and grandchildren of all topics at current
3206 level except the last two; expose children of the second to
3207 last and completely open the last one."
3208
3209 (interactive "xExposure spec: ")
3210 (let ((depth (outline-current-depth))
3211 done
3212 max-pos)
3213 (cond ((null spec) nil)
3214 ((symbolp spec)
3215 (cond ((eq spec '*) (outline-show-current-subtree))
3216 ((eq spec '+) (outline-show-current-branches))
3217 ((eq spec '-) (outline-show-current-entry))
3218 ((eq spec ':)
3219 ;; Whoops. ':' should have been caught at superior
3220 ;; level.
3221 (error
3222 "outline-expose-topic: improper exposure spec - bare ':'"))))
3223 ((numberp spec)
3224 (if (>= 0 spec)
3225 (save-excursion (outline-hide-current-subtree t)
3226 (end-of-line)
3227 (if (or (not max-pos)
3228 (> (point) max-pos))
3229 (setq max-pos (point)))
3230 (if (> 0 spec)
3231 (setq spec (* -1 spec)))))
3232 (if (> spec 0)
3233 (outline-show-children spec)))
3234 ((listp spec)
3235 (if (eq (car spec) ':)
3236 (setq spec
3237 ;; Expand the 'repeat' spec to an explicit version,
3238 ;; w.r.t. remaining siblings:
3239 (let* (;; Assign rest-spec to preserve first elem in cdr.
3240 (rest-spec (delq ': (cdr spec)))
3241 ;; residue: # of sibs not covered by remaining spec
3242 (residue (- (length (outline-chart-siblings))
3243 (length rest-spec))))
3244 (if (>= 0 residue)
3245 ;; remaining spec covers all - just use it:
3246 rest-spec
3247 ;; cover residue by prev-spec, rest by rest-spec:
3248 (nconc (make-list residue prev-spec) rest-spec)))))
3249 (setq max-pos (or (outline-expose-topic (car spec) prev-spec)
3250 max-pos))
3251 (setq prev-spec (car spec))
3252 (setq spec (cdr spec))
3253 (and
3254 (if max-pos
3255 ;; Capitalize on max-pos state to get us nearer next sibling:
3256 (progn (goto-char (min (point-max) max-pos))
3257 (outline-next-heading))
3258 (outline-next-sibling depth))
3259 (let ((got (outline-expose-topic spec prev-spec)))
3260 (if (and got (or (not max-pos) (> got max-pos)))
3261 (setq max-pos got))))))
3262 max-pos))
3263 ;;;_ > outline-old-expose-topic (spec &rest followers)
3264 (defun outline-old-expose-topic (spec &rest followers)
3265
3266 "Dictate wholesale exposure scheme for current topic, according to SPEC.
3267
3268 SPEC is either a number or a list. Optional successive args
3269 dictate exposure for subsequent siblings of current topic.
3270
3271 A simple spec (either a number, a special symbol, or the null list)
3272 dictates the overall exposure for a topic. Non null lists are
3273 composite specs whose first element dictates the overall exposure for
3274 a topic, with the subsequent elements in the list interpreted as specs
3275 that dictate the exposure for the successive offspring of the topic.
3276
3277 Simple (numeric and null-list) specs are interpreted as follows:
3278
3279 - Numbers indicate the relative depth to open the corresponding topic:
3280 - negative numbers force the topic to be close before opening to the
3281 absolute value of the number.
3282 - positive numbers just open to the relative depth indicated by the number.
3283 - 0 just closes
3284 - '*' completely opens the topic, including bodies.
3285 - '+' shows all the sub headers, but not the bodies
3286 - '-' exposes the body and immediate offspring of the corresponding topic.
3287
3288 If the spec is a list, the first element must be a number, which
3289 dictates the exposure depth of the topic as a whole. Subsequent
3290 elements of the list are nested SPECs, dictating the specific exposure
3291 for the corresponding offspring of the topic.
3292
3293 Optional FOLLOWER arguments dictate exposure for succeeding siblings."
3294
3295 (interactive "xExposure spec: ")
3296 (let ((depth (outline-current-depth))
3297 done
3298 max-pos)
3299 (cond ((null spec) nil)
3300 ((symbolp spec)
3301 (if (eq spec '*) (outline-show-current-subtree))
3302 (if (eq spec '+) (outline-show-current-branches))
3303 (if (eq spec '-) (outline-show-current-entry)))
3304 ((numberp spec)
3305 (if (>= 0 spec)
3306 (save-excursion (outline-hide-current-subtree t)
3307 (end-of-line)
3308 (if (or (not max-pos)
3309 (> (point) max-pos))
3310 (setq max-pos (point)))
3311 (if (> 0 spec)
3312 (setq spec (* -1 spec)))))
3313 (if (> spec 0)
3314 (outline-show-children spec)))
3315 ((listp spec)
3316 ;(let ((got (outline-old-expose-topic (car spec))))
3317 ; (if (and got (or (not max-pos) (> got max-pos)))
3318 ; (setq max-pos got)))
3319 (let ((new-depth (+ (outline-current-depth) 1))
3320 got)
3321 (setq max-pos (outline-old-expose-topic (car spec)))
3322 (setq spec (cdr spec))
3323 (if (and spec
3324 (outline-descend-to-depth new-depth)
3325 (not (outline-hidden-p)))
3326 (progn (setq got (apply 'outline-old-expose-topic spec))
3327 (if (and got (or (not max-pos) (> got max-pos)))
3328 (setq max-pos got)))))))
3329 (while (and followers
3330 (progn (if (and max-pos (< (point) max-pos))
3331 (progn (goto-char max-pos)
3332 (setq max-pos nil)))
3333 (end-of-line)
3334 (outline-next-sibling depth)))
3335 (outline-old-expose-topic (car followers))
3336 (setq followers (cdr followers)))
3337 max-pos))
3338 ;;;_ > outline-new-exposure '()
3339 (defmacro outline-new-exposure (&rest spec)
3340 "Literal frontend for `outline-expose-topic', doesn't evaluate arguments.
3341 All arguments that would need to be quoted in outline-expose-topic need not
3342 be in outline-exposure.
3343
3344 Cursor is left at start position.
3345
3346 Use this instead of obsolete 'outline-exposure'.
3347
3348 Examples:
3349 \(outline-exposure (-1 () () () 1) 0)
3350 Close current topic at current level so only the immediate
3351 subtopics are shown, except also show the children of the
3352 third subtopic; and close the next topic at the current level.
3353 \(outline-exposure : -1 0)
3354 Close all topics at current level to expose only their
3355 immediate children, except for the last topic at the current
3356 level, in which even its' immediate children are hidden.
3357 \(outline-exposure -2 : -1 *)
3358 Expose children and grandchildren of first topic at current
3359 level, and expose children of subsequent topics at current
3360 level *except* for the last, which should be opened completely."
3361 (list 'save-excursion
3362 '(if (not (or (outline-goto-prefix)
3363 (outline-next-heading)))
3364 (error "outline-exposure: Can't find any outline topics."))
3365 (list 'outline-expose-topic (list 'quote spec))))
3366 ;;;_ > outline-exposure '()
3367 (defmacro outline-exposure (&rest spec)
3368 "Being deprecated - use more recent 'outline-new-exposure' instead.
3369
3370 Literal frontend for `outline-old-expose-topic', doesn't evaluate arguments
3371 and retains start position."
3372 (list 'save-excursion
3373 '(if (not (or (outline-goto-prefix)
3374 (outline-next-heading)))
3375 (error "Can't find any outline topics."))
3376 (cons 'outline-old-expose-topic
3377 (mapcar '(lambda (x) (list 'quote x)) spec))))
3378
3379 ;;;_ #6 Search with Dynamic Exposure (requires v19 isearch or isearch-mode)
3380 ;;;_ = outline-search-reconceal
3381 (defvar outline-search-reconceal nil
3382 "Used for outline isearch provisions, to track whether current search
3383 match was concealed outside of search. The value is the location of the
3384 match, if it was concealed, regular if the entire topic was concealed, in
3385 a list if the entry was concealed.")
3386 ;;;_ = outline-search-quitting
3387 (defconst outline-search-quitting nil
3388 "Variable used by isearch-terminate/outline-provisions and
3389 isearch-done/outline-provisions to distinguish between a conclusion
3390 and cancellation of a search.")
3391
3392
3393 ;;;_ > outline-enwrap-isearch ()
3394 (defun outline-enwrap-isearch ()
3395 "Impose isearch-mode wrappers so isearch progressively exposes and
3396 reconceals hidden topics when working in outline mode, but works
3397 elsewhere.
3398
3399 The function checks to ensure that the rebindings are done only once."
3400
3401 ; Should isearch-mode be employed,
3402 (if (or (not outline-enwrap-isearch-mode)
3403 ; or are preparations already done?
3404 (fboundp 'real-isearch-terminate))
3405
3406 ;; ... no - skip this all:
3407 nil
3408
3409 ;; ... yes:
3410
3411 ; Ensure load of isearch-mode:
3412 (if (or (and (fboundp 'isearch-mode)
3413 (fboundp 'isearch-quote-char))
3414 (condition-case error
3415 (load-library outline-enwrap-isearch-mode)
3416 (file-error (message "Skipping isearch-mode provisions - %s '%s'"
3417 (car (cdr error))
3418 (car (cdr (cdr error))))
3419 (sit-for 1)
3420 ;; Inhibit subsequent tries and return nil:
3421 (setq outline-enwrap-isearch-mode nil))))
3422 ;; Isearch-mode loaded, encapsulate specific entry points for
3423 ;; outline dynamic-exposure business:
3424 (progn
3425
3426 ; stash crucial isearch-mode
3427 ; funcs under known, private
3428 ; names, then register wrapper
3429 ; functions under the old
3430 ; names, in their stead:
3431 ; 'isearch-quit' is pre v 1.2:
3432 (fset 'real-isearch-terminate
3433 ; 'isearch-quit is pre v 1.2:
3434 (or (if (fboundp 'isearch-quit)
3435 (symbol-function 'isearch-quit))
3436 (if (fboundp 'isearch-abort)
3437 ; 'isearch-abort' is v 1.2 and on:
3438 (symbol-function 'isearch-abort))))
3439 (fset 'isearch-quit 'isearch-terminate/outline-provisions)
3440 (fset 'isearch-abort 'isearch-terminate/outline-provisions)
3441 (fset 'real-isearch-done (symbol-function 'isearch-done))
3442 (fset 'isearch-done 'isearch-done/outline-provisions)
3443 (fset 'real-isearch-update (symbol-function 'isearch-update))
3444 (fset 'isearch-update 'isearch-update/outline-provisions)
3445 (make-variable-buffer-local 'outline-search-reconceal)))))
3446 ;;;_ > outline-isearch-arrival-business ()
3447 (defun outline-isearch-arrival-business ()
3448 "Do outline business like exposing current point, if necessary,
3449 registering reconcealment requirements in outline-search-reconceal
3450 accordingly.
3451
3452 Set outline-search-reconceal to nil if current point is not
3453 concealed, to value of point if entire topic is concealed, and a
3454 list containing point if only the topic body is concealed.
3455
3456 This will be used to determine whether outline-hide-current-entry
3457 or outline-hide-current-entry-completely will be necessary to
3458 restore the prior concealment state."
3459
3460 (if (outline-mode-p)
3461 (setq outline-search-reconceal
3462 (if (outline-hidden-p)
3463 (save-excursion
3464 (if (re-search-backward outline-line-boundary-regexp nil 1)
3465 ;; Nil value means we got to b-o-b - wouldn't need
3466 ;; to advance.
3467 (forward-char 1))
3468 ; We'll return point or list
3469 ; containing point, depending
3470 ; on concealment state of
3471 ; topic prefix.
3472 (prog1 (if (outline-hidden-p) (point) (list (point)))
3473 ; And reveal the current
3474 ; search target:
3475 (outline-show-entry)))))))
3476 ;;;_ > outline-isearch-advancing-business ()
3477 (defun outline-isearch-advancing-business ()
3478 "Do outline business like deexposing current point, if necessary,
3479 according to reconceal state registration."
3480 (if (and (outline-mode-p) outline-search-reconceal)
3481 (save-excursion
3482 (if (listp outline-search-reconceal)
3483 ;; Leave the topic visible:
3484 (progn (goto-char (car outline-search-reconceal))
3485 (outline-hide-current-entry))
3486 ;; Rehide the entire topic:
3487 (goto-char outline-search-reconceal)
3488 (outline-hide-current-entry-completely)))))
3489 ;;;_ > isearch-terminate/outline-provisions ()
3490 (defun isearch-terminate/outline-provisions ()
3491 (interactive)
3492 (if (and (outline-mode-p) outline-enwrap-isearch-mode)
3493 (outline-isearch-advancing-business))
3494 (let ((outline-search-quitting t)
3495 (outline-search-reconceal nil))
3496 (real-isearch-terminate)))
3497 ;;;_ > isearch-done/outline-provisions ()
3498 (defun isearch-done/outline-provisions (&optional nopush)
3499 (interactive)
3500 (if (and (outline-mode-p) outline-enwrap-isearch-mode)
3501 (progn (if (and outline-search-reconceal
3502 (not (listp outline-search-reconceal)))
3503 ;; The topic was concealed - reveal it, its siblings,
3504 ;; and any ancestors that are still concealed:
3505 (save-excursion
3506 (message "(exposing destination)")(sit-for 0)
3507 (outline-goto-prefix)
3508 ; There may be a closed blank
3509 ; line between prior and
3510 ; current topic that would be
3511 ; missed - provide for it:
3512 (if (not (bobp))
3513 (progn (forward-char -1) ; newline
3514 (if (eq ?\r (preceding-char))
3515 (outline-flag-region (1- (point))
3516 (point)
3517 ?\n))
3518 (forward-char 1)))
3519 ; Goto parent
3520 (outline-ascend-to-depth (1- (outline-recent-depth)))
3521 (outline-show-children)))
3522 (if (and (boundp 'outline-search-quitting)
3523 outline-search-quitting)
3524 nil
3525 ; We're concluding abort:
3526 (outline-isearch-arrival-business)
3527 (outline-show-children))))
3528 (if nopush
3529 ;; isearch-done in newer version of isearch mode takes arg:
3530 (real-isearch-done nopush)
3531 (real-isearch-done)))
3532 ;;;_ > isearch-update/outline-provisions ()
3533 (defun isearch-update/outline-provisions ()
3534 "Wrapper around isearch which exposes and conceals hidden outline
3535 portions encountered in the course of searching."
3536 (if (not (and (outline-mode-p) outline-enwrap-isearch-mode))
3537 ;; Just do the plain business:
3538 (real-isearch-update)
3539
3540 ;; Ah - provide for outline conditions:
3541 (outline-isearch-advancing-business)
3542 (real-isearch-update)
3543 (cond (isearch-success (outline-isearch-arrival-business))
3544 ((not isearch-success) (outline-isearch-advancing-business)))))
3545
3546 ;;;_ #7 Copying and printing
3547
3548 ;;;_ - Copy exposed
3549 ;;;_ > outline-insert-listified (depth prefix bullet text)
3550 (defun outline-insert-listified (depth prefix bullet text)
3551 (insert-string (concat (if (> depth 1) prefix "")
3552 (make-string (1- depth) ?\ )
3553 bullet))
3554 (while text
3555 (insert-string (car text))
3556 (if (setq text (cdr text))
3557 (insert-string "\n")))
3558 (insert-string "\n"))
3559 ;;;_ > outline-copy-exposed (arg &optional tobuf)
3560 (defun outline-copy-exposed (arg &optional tobuf)
3561 "Duplicate exposed portions of current topic to buffer with
3562 current buffers' name with \" exposed\" appended to it.
3563
3564 With repeat count, copy the exposed portions of entire buffer."
3565
3566 (interactive "P")
3567 (if (not tobuf)
3568 (setq tobuf (get-buffer-create (concat "*" (buffer-name) " exposed*"))))
3569 (let* ((start-pt (point))
3570 (beg (if arg (point-min) (outline-back-to-current-heading)))
3571 (end (if arg (point-max) (outline-end-of-current-subtree)))
3572 (buf (current-buffer)))
3573 (save-excursion (set-buffer tobuf)(erase-buffer))
3574 (outline-process-exposed 'outline-insert-listified
3575 beg
3576 end
3577 (current-buffer)
3578 tobuf)
3579 (goto-char (point-min))
3580 (pop-to-buffer buf)
3581 (goto-char start-pt)))
3582
3583 ;;;_ - LaTeX formatting
3584 ;;;_ > outline-latex-verb-quote (str &optional flow)
3585 (defun outline-latex-verb-quote (str &optional flow)
3586 "Return copy of STRING which expresses the original characters
3587 \(including carriage returns) of the string across latex processing."
3588 (mapconcat '(lambda (char)
3589 ;;;mess: (cond ((memq char '(?"" ?$ ?% ?# ?& ?- ?" ?` ?^ ?- ?*));;;"))))
3590 (cond ((memq char '(?\\ ?$ ?% ?# ?& ?{ ?} ?_ ?^ ?- ?*))
3591 (concat "\\char" (number-to-string char) "{}"))
3592 ((= char ?\n) "\\\\")
3593 (t (char-to-string char))))
3594 str
3595 ""))
3596 ;;;_ > outline-latex-verbatim-quote-curr-line ()
3597 (defun outline-latex-verbatim-quote-curr-line ()
3598 "Adjust line contents so it is unaltered \(from the original line)
3599 across latex processing, within the context of a 'verbatim'
3600 environment. Leaves point at the end of the line."
3601 (beginning-of-line)
3602 (let ((beg (point))
3603 (end (progn (end-of-line)(point))))
3604 (goto-char beg)
3605 (while (re-search-forward "\\\\"
3606 ;;"\\\\\\|\\{\\|\\}\\|\\_\\|\\$\\|\\\"\\|\\&\\|\\^\\|\\-\\|\\*\\|#"
3607 end ; bounded by end-of-line
3608 1) ; no matches, move to end & return nil
3609 (goto-char (match-beginning 0))
3610 (insert-string "\\")
3611 (setq end (1+ end))
3612 (goto-char (1+ (match-end 0))))))
3613 ;;;_ > outline-insert-latex-header (buf)
3614 (defun outline-insert-latex-header (buf)
3615 "Insert initial latex commands at point in BUFFER."
3616 ;; Much of this is being derived from the stuff in appendix of E in
3617 ;; the TeXBook, pg 421.
3618 (set-buffer buf)
3619 (let ((doc-style (format "\n\\documentstyle{%s}\n"
3620 "report"))
3621 (page-numbering (if outline-number-pages
3622 "\\pagestyle{empty}\n"
3623 ""))
3624 (linesdef (concat "\\def\\beginlines{"
3625 "\\par\\begingroup\\nobreak\\medskip"
3626 "\\parindent=0pt\n"
3627 " \\kern1pt\\nobreak \\obeylines \\obeyspaces "
3628 "\\everypar{\\strut}}\n"
3629 "\\def\\endlines{"
3630 "\\kern1pt\\endgroup\\medbreak\\noindent}\n"))
3631 (titlecmd (format "\\newcommand{\\titlecmd}[1]{{%s #1}}\n"
3632 outline-title-style))
3633 (labelcmd (format "\\newcommand{\\labelcmd}[1]{{%s #1}}\n"
3634 outline-label-style))
3635 (headlinecmd (format "\\newcommand{\\headlinecmd}[1]{{%s #1}}\n"
3636 outline-head-line-style))
3637 (bodylinecmd (format "\\newcommand{\\bodylinecmd}[1]{{%s #1}}\n"
3638 outline-body-line-style))
3639 (setlength (format "%s%s%s%s"
3640 "\\newlength{\\stepsize}\n"
3641 "\\setlength{\\stepsize}{"
3642 outline-indent
3643 "}\n"))
3644 (oneheadline (format "%s%s%s%s%s%s%s"
3645 "\\newcommand{\\OneHeadLine}[3]{%\n"
3646 "\\noindent%\n"
3647 "\\hspace*{#2\\stepsize}%\n"
3648 "\\labelcmd{#1}\\hspace*{.2cm}"
3649 "\\headlinecmd{#3}\\\\["
3650 outline-line-skip
3651 "]\n}\n"))
3652 (onebodyline (format "%s%s%s%s%s%s"
3653 "\\newcommand{\\OneBodyLine}[2]{%\n"
3654 "\\noindent%\n"
3655 "\\hspace*{#1\\stepsize}%\n"
3656 "\\bodylinecmd{#2}\\\\["
3657 outline-line-skip
3658 "]\n}\n"))
3659 (begindoc "\\begin{document}\n\\begin{center}\n")
3660 (title (format "%s%s%s%s"
3661 "\\titlecmd{"
3662 (outline-latex-verb-quote (if outline-title
3663 (condition-case err
3664 (eval outline-title)
3665 (error "<unnamed buffer>"))
3666 "Unnamed Outline"))
3667 "}\n"
3668 "\\end{center}\n\n"))
3669 (hsize "\\hsize = 7.5 true in\n")
3670 (hoffset "\\hoffset = -1.5 true in\n")
3671 (vspace "\\vspace{.1cm}\n\n"))
3672 (insert (concat doc-style
3673 page-numbering
3674 titlecmd
3675 labelcmd
3676 headlinecmd
3677 bodylinecmd
3678 setlength
3679 oneheadline
3680 onebodyline
3681 begindoc
3682 title
3683 hsize
3684 hoffset
3685 vspace)
3686 )))
3687 ;;;_ > outline-insert-latex-trailer (buf)
3688 (defun outline-insert-latex-trailer (buf)
3689 "Insert concluding latex commands at point in BUFFER."
3690 (set-buffer buf)
3691 (insert "\n\\end{document}\n"))
3692 ;;;_ > outline-latexify-one-item (depth prefix bullet text)
3693 (defun outline-latexify-one-item (depth prefix bullet text)
3694 "Insert LaTeX commands for formatting one item - a topic header and
3695 its' body - of an outline. Args are the topics' numeric DEPTH, the
3696 header PREFIX lead string, the BULLET string, and a list of TEXT
3697 strings for the body."
3698 (let* ((head-line (if text (car text)))
3699 (body-lines (cdr text))
3700 (curr-line)
3701 body-content bop)
3702 ; Do the head line:
3703 (insert-string (concat "\\OneHeadLine{\\verb\1 "
3704 (outline-latex-verb-quote bullet)
3705 "\1}{"
3706 depth
3707 "}{\\verb\1 "
3708 (if head-line
3709 (outline-latex-verb-quote head-line)
3710 "")
3711 "\1}\n"))
3712 (if (not body-lines)
3713 nil
3714 ;;(insert-string "\\beginlines\n")
3715 (insert-string "\\begin{verbatim}\n")
3716 (while body-lines
3717 (setq curr-line (car body-lines))
3718 (if (and (not body-content)
3719 (not (string-match "^\\s-*$" curr-line)))
3720 (setq body-content t))
3721 ; Mangle any occurrences of
3722 ; "\end{verbatim}" in text,
3723 ; it's special:
3724 (if (and body-content
3725 (setq bop (string-match "\\end{verbatim}" curr-line)))
3726 (setq curr-line (concat (substring curr-line 0 bop)
3727 ">"
3728 (substring curr-line bop))))
3729 ;;(insert-string "|" (car body-lines) "|")
3730 (insert-string curr-line)
3731 (outline-latex-verbatim-quote-curr-line)
3732 (insert-string "\n")
3733 (setq body-lines (cdr body-lines)))
3734 (if body-content
3735 (setq body-content nil)
3736 (forward-char -1)
3737 (insert-string "\\ ")
3738 (forward-char 1))
3739 ;;(insert-string "\\endlines\n")
3740 (insert-string "\\end{verbatim}\n")
3741 )))
3742 ;;;_ > outline-latexify-exposed (arg &optional tobuf)
3743 (defun outline-latexify-exposed (arg &optional tobuf)
3744 "Copy exposed portions of current topic to TOBUF, formatted for
3745 latex processing. tobuf defaults to a buffer named the same as the
3746 current buffer, but with \"*\" prepended and \" latex-formed*\"
3747 appended.
3748
3749 With repeat count, copy the exposed portions of entire buffer."
3750
3751 (interactive "P")
3752 (if (not tobuf)
3753 (setq tobuf
3754 (get-buffer-create (concat "*" (buffer-name) " latexified*"))))
3755 (let* ((start-pt (point))
3756 (beg (if arg (point-min) (outline-back-to-current-heading)))
3757 (end (if arg (point-max) (outline-end-of-current-subtree)))
3758 (buf (current-buffer)))
3759 (set-buffer tobuf)
3760 (erase-buffer)
3761 (outline-insert-latex-header tobuf)
3762 (goto-char (point-max))
3763 (outline-process-exposed 'outline-latexify-one-item
3764 beg
3765 end
3766 buf
3767 tobuf)
3768 (goto-char (point-max))
3769 (outline-insert-latex-trailer tobuf)
3770 (goto-char (point-min))
3771 (pop-to-buffer buf)
3772 (goto-char start-pt)))
3773
3774
3775 ;;;_ #8 miscellaneous
3776 ;;;_ > outline-mark-topic ()
3777 (defun outline-mark-topic ()
3778 "Put the region around topic currently containing point."
3779 (interactive)
3780 (beginning-of-line)
3781 (outline-goto-prefix)
3782 (push-mark (point))
3783 (outline-end-of-current-subtree)
3784 (exchange-point-and-mark))
3785 ;;;_ > outlineify-sticky ()
3786 (defun outlineify-sticky (&optional arg)
3787 "Activate outline mode and establish file eval to set initial exposure.
3788
3789 Invoke with a string argument to designate a string to prepend to
3790 topic prefixs, or with a universal argument to be prompted for the
3791 string to be used. Suitable defaults are provided for lisp,
3792 emacs-lisp, c, c++, awk, sh, csh, and perl modes."
3793
3794 (interactive "P") (outline-mode t)
3795
3796
3797 (let ((leader-cell (assoc major-mode outline-mode-leaders)))
3798 (cond (arg (if (stringp arg)
3799 ;; Use arg as the header-prefix:
3800 (outline-lead-with-comment-string arg)
3801 ;; Otherwise, let function solicit string:
3802 (setq arg (outline-lead-with-comment-string))))
3803
3804 (leader-cell
3805 (outline-lead-with-comment-string (cdr leader-cell))
3806 (setq arg (cdr leader-cell)))))
3807
3808 (let* ((lead-prefix (format "%s%s"
3809 (concat outline-header-prefix (if arg " " ""))
3810 outline-primary-bullet))
3811 (lead-line (format "%s%s %s\n%s %s\n %s %s %s"
3812 (if arg outline-header-prefix "")
3813 outline-primary-bullet
3814 "Local emacs vars."
3815 "'(This topic sets initial outline exposure"
3816 "of the file when loaded by emacs,"
3817 "Encapsulate it in comments if"
3818 "file is a program"
3819 "otherwise ignore it,")))
3820
3821 (save-excursion
3822 ; Put a topic at the top, if
3823 ; none there already:
3824 (goto-char (point-min))
3825 (if (not (looking-at outline-regexp))
3826 (insert-string
3827 (if (not arg) outline-primary-bullet
3828 (format "%s%s\n" outline-header-prefix outline-primary-bullet))))
3829
3830 ; File-vars stuff, at the bottom:
3831 (goto-char (point-max))
3832 ; Insert preamble:
3833 (insert-string (format "\n\n%s\n%s %s %s\n%s %s\n"
3834 lead-line
3835 lead-prefix
3836 "local"
3837 "variables:"
3838 lead-prefix
3839 "eval:"))
3840 ; Insert outline-mode activation:
3841 (insert-string
3842 (format "\t %s\n\t\t%s\n\t\t\t%s\n"
3843 "(condition-case err"
3844 "(save-excursion"
3845 "(outline-mode t)"))
3846 ; Conditionally insert prefix
3847 ; leader customization:
3848 (if arg (insert-string (format "\t\t\t(%s \"%s\")\n"
3849 "outline-lead-with-comment-string"
3850 arg)))
3851 ; Insert ammouncement and
3852 ; exposure control:
3853 (insert-string
3854 (format "\t\t\t%s %s\n\t\t\t%s %s\n\t\t%s %s"
3855 "(message \"Adjusting '%s' exposure\""
3856 "(buffer-name))"
3857 "(goto-char 0)"
3858 "(outline-exposure -1 0))"
3859 "(error (message "
3860 "\"Failed file var 'allout' provisions\")))"))
3861 ; Insert postamble:
3862 (insert-string (format "\n%s End:\n)\n"
3863 lead-prefix)))))
3864 ;;;_ > solicit-char-in-string (prompt string &optional do-defaulting)
3865 (defun solicit-char-in-string (prompt string &optional do-defaulting)
3866 "Solicit (with first arg PROMPT) choice of a character from string STRING.
3867
3868 Optional arg DO-DEFAULTING indicates to accept empty input (CR)."
3869
3870 (let ((new-prompt prompt)
3871 got)
3872
3873 (while (not got)
3874 (message "%s" new-prompt)
3875
3876 ;; We do our own reading here, so we can circumvent, eg, special
3877 ;; treatment for '?' character. (Might oughta change minibuffer
3878 ;; keymap instead, oh well.)
3879 (setq got
3880 (char-to-string (let ((cursor-in-echo-area nil)) (read-char))))
3881
3882 (if (null (string-match (regexp-quote got) string))
3883 (if (and do-defaulting (string= got "\^M"))
3884 ;; We're defaulting, return null string to indicate that:
3885 (setq got "")
3886 ;; Failed match and not defaulting,
3887 ;; set the prompt to give feedback,
3888 (setq new-prompt (concat prompt
3889 got
3890 " ...pick from: "
3891 string
3892 ""))
3893 ;; and set loop to try again:
3894 (setq got nil))
3895 ;; Got a match - give feedback:
3896 (message "")))
3897 ;; got something out of loop - return it:
3898 got)
3899 )
3900 ;;;_ > regexp-sans-escapes (string)
3901 (defun regexp-sans-escapes (regexp &optional successive-backslashes)
3902 "Return a copy of REGEXP with all character escapes stripped out.
3903 Representations of actual backslashes - '\\\\\\\\' - are left as a
3904 single backslash.
3905
3906 Optional arg SUCCESSIVE-BACKSLASHES is used internally for recursion."
3907
3908 (if (string= regexp "")
3909 ""
3910 ;; Set successive-backslashes to number if current char is
3911 ;; backslash, or else to nil:
3912 (setq successive-backslashes
3913 (if (= (aref regexp 0) ?\\)
3914 (if successive-backslashes (1+ successive-backslashes) 1)
3915 nil))
3916 (if (or (not successive-backslashes) (= 2 successive-backslashes))
3917 ;; Include first char:
3918 (concat (substring regexp 0 1)
3919 (regexp-sans-escapes (substring regexp 1)))
3920 ;; Exclude first char, but maintain count:
3921 (regexp-sans-escapes (substring regexp 1) successive-backslashes))))
3922 ;;;_ - add-hook definition for v18
3923 ;;;_ > add-hook (hook function &optional append)
3924 (if (not (fboundp 'add-hook))
3925 (defun add-hook (hook function &optional append)
3926 "Add to the value of HOOK the function FUNCTION unless already present (it
3927 becomes the first hook on the list unless optional APPEND is non-nil, in
3928 which case it becomes the last). HOOK should be a symbol, and FUNCTION may be
3929 any valid function. HOOK's value should be a list of functions, not a single
3930 function. If HOOK is void, it is first set to nil."
3931 (or (boundp hook) (set hook nil))
3932 (or (if (consp function)
3933 ;; Clever way to tell whether a given lambda-expression
3934 ;; is equal to anything in the hook.
3935 (let ((tail (assoc (cdr function) (symbol-value hook))))
3936 (equal function tail))
3937 (memq function (symbol-value hook)))
3938 (set hook
3939 (if append
3940 (nconc (symbol-value hook) (list function))
3941 (cons function (symbol-value hook)))))))
3942
3943 ;;;_ #9 Under development
3944 ;;;_ > outline-bullet-isearch (&optional bullet)
3945 (defun outline-bullet-isearch (&optional bullet)
3946 "Isearch \(regexp\) for topic with bullet BULLET."
3947 (interactive)
3948 (if (not bullet)
3949 (setq bullet (solicit-char-in-string
3950 "ISearch for topic with bullet: "
3951 (regexp-sans-escapes outline-bullets-string))))
3952
3953 (let ((isearch-regexp t)
3954 (isearch-string (concat "^"
3955 outline-header-prefix
3956 "[ \t]*"
3957 bullet)))
3958 (isearch-repeat 'forward)
3959 (isearch-mode t)))
3960 ;;;_ - Re hooking up with isearch - use isearch-op-fun rather than
3961 wrapping the isearch functions.
3962
3963 ;;;_* Local emacs vars.
3964 '(
3965 Local variables:
3966 eval: (save-excursion
3967 (if (not (condition-case err (outline-mode t)
3968 (wrong-number-of-arguments nil)))
3969 (progn
3970 (message
3971 "Allout outline-mode not loaded, not adjusting buffer exposure")
3972 (sit-for 1))
3973 (message "Adjusting '%s' exposure" (buffer-name))
3974 (outline-lead-with-comment-string "\;\;\;_")
3975 (goto-char 0)
3976 (outline-new-exposure 0 : -1 -1 0)))
3977 End:)
3978