1 ;; allout.el - An extensive outline-mode for Emacs.
2 ;; Copyright (C) 1992, 1993, 1994 Free Software Foundation, Inc.
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
10 ;; This file is part of GNU Emacs.
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)
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.
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.
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.
33 ;;;_ + LCD Archive Entry
36 ;; A full-fledged outline mode, based on the original rudimentary
37 ;; GNU emacs outline functionality.
39 ;;Ken Manheimer 301 975-3539
40 ;;ken.manheimer@nist.gov FAX: 301 963-9137
42 ;;Computer Systems and Communications Division
44 ;; Nat'l Institute of Standards and Technology
46 ;; Gaithersburg, MD 20899
48 ;;;_* User Customization variables
50 ;;;_ + Topic Header configuration
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)
60 ;;;_ = outline-mode-leaders
61 (defvar outline-mode-leaders
62 '((emacs-lisp-mode . "\;\;\;_")
63 (lisp-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
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.
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
89 (make-variable-buffer-local 'outline-primary-bullet)
91 ;;;_ = outline-plain-bullets-string
92 (defvar outline-plain-bullets-string (concat outline-primary-bullet
94 "*The bullets normally used in outline topic prefixes. See
95 'outline-distinctive-bullets-string' for the other kind of
98 DO NOT include the close-square-bracket, ']', as a bullet.
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)
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.
112 DO NOT include the close-square-bracket, ']', among any bullets.
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)
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.
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)
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.
136 This is how an outline can look with stylish prefixes:
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
148 This would be an outline with stylish prefixes inhibited:
152 .! A distinctive (but measly) subtopic
153 . * A sub-subtopic - no bullets from outline-plain-bullets-string but '*'
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.
159 The setting of this var is not relevant when outline-old-style-prefixes
161 (make-variable-buffer-local 'outline-stylish-prefixes)
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)
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.")
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.")
193 (defvar outline-title '(or buffer-file-name (current-buffer-name))
194 "*Expression to be evaluated to determine the title for LaTeX
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.")
203 ;;;_ + Miscellaneous customization
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
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)
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)))
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.")
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.)
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
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.")
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
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)
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
286 (make-variable-buffer-local 'outline-reindent-bodies)
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)
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.
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
308 ;;;_* Code - no user customizations below.
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)
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."
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)
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."
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))
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
403 ; Single dash would denote a
404 ; sequence, repeated denotes
406 ((eq cur-char ?-) "--")
407 ; literal close-square-bracket
408 ; doesn't work right in the
410 ((eq cur-char ?\]) "")
411 (t (regexp-quote (char-to-string cur-char))))))
412 (setq index (1+ index)))
413 (setq strings (cdr strings)))
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
422 outline-bullets-string
424 outline-primary-bullet
426 (setq outline-line-boundary-regexp
427 (concat "\\([\n\r]\\)\\(" outline-regexp "\\)"))
428 (setq outline-bob-regexp
429 (concat "\\(\\`\\)\\(" outline-regexp "\\)"))
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
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
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)
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
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.
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
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))))
484 ;; Oops, not registered - leave it be:
487 ;; Some registration:
489 (setq prior-capsule (car (cdr on-list)))
491 (set name (car prior-capsule)) ; Some prior value - reestablish it.
492 (makunbound name)) ; Previously unbound - demolish var.
493 ; Remove registration:
495 (while outline-mode-prior-settings
496 (if (not (eq (car outline-mode-prior-settings)
499 (cons (car outline-mode-prior-settings)
501 (setq outline-mode-prior-settings
502 (cdr outline-mode-prior-settings)))
503 (setq outline-mode-prior-settings rebuild)))))
506 ;;;_ = outline-version
507 (defvar outline-version
508 (let ((rcs-rev "$Revision: 3.39 $"))
511 (string-match "\\$Revision: \\([0-9]+\\.[0-9]+\\)" rcs-rev)
512 (substring rcs-rev (match-beginning 1) (match-end 1)))
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."
520 (let ((msg (concat "Allout Outline Mode v " outline-version)))
521 (if here (insert-string msg))
525 ;;;_ - Mode activation
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))
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.")
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)
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
561 (` (let ((outline-override-protect t))
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)
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
581 (list (car cell) (car (cdr cell))))))
584 ;;;_ > outline-mode (&optional toggle)
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
590 (defun outline-mode (&optional toggle)
592 "Toggle minor mode for controlling exposure of and editing text
593 outlines. Optional arg forces mode activation iff arg is positive.
595 Look below the description of the bindings for explanation of the
596 terminology use in outline-mode commands.
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
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
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
621 C-c # outline-number-siblings Number bullets of topic and siblings - the
622 offspring are not affected. With repeat
623 count, revoke numbering.
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
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.
645 HOT-SPOT Operation (Not available in Emacs v18.)
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-
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
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.
667 Topic hierarchy constituents - TOPICS and SUBTOPICS:
669 TOPIC: A basic, coherent component of an emacs outline. It can
670 contain other topics, and it can be subsumed by other topics,
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.
678 The topics that contain a topic.
679 PARENT: A topic's immediate ancestor. It has a depth one less than
682 The topics contained by a topic,
684 The immediate offspring of a topic.
686 Topics having the same parent.
688 Topic text constituents:
690 HEADER: The first line of a topic, include the topic PREFIX and header
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.
698 The relative length of the PREFIX determines the nesting depth
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.
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
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
718 ENTRY: The text contained in a topic before any offspring.
723 The state of a topic which determines the on-screen visibility
724 of its' offspring and contained text.
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.)
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."
737 (let* ((active (and (not (equal major-mode 'outline))
739 ; Massage universal-arg 'toggle' val:
741 (or (and (listp toggle)(car toggle))
743 ; Activation specficially demanded?
744 (explicit-activation (or
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))))
754 ; See comments below re v19.18,.19 bug.
755 (setq outline-v18/9-file-var-hack (car command-history))
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
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.
773 ((and (not explicit-activation)
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
782 (let ((curr-loc (current-local-map)))
783 (mapcar '(lambda (cell)
784 (if (eq (lookup-key curr-loc (car 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)))
792 (if outline-old-style-prefixes
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
801 (setq post-command-hook (delq 'outline-post-command-business
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)
810 'auto-fill-function))
811 (outline-resumptions 'outline-former-auto-filler)
812 (setq outline-mode nil))
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 '(()))))
821 ; Produce map from current version
822 ; of outline-keybindings-list:
823 (if (boundp 'minor-mode-map-alist)
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))))
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)))
846 (use-local-map (produce-outline-mode-map outline-keybindings-list
847 (current-local-map)))
850 ; selective-display is the
851 ; emacs conditional exposure
853 (outline-resumptions 'selective-display '(t))
854 (if outline-inhibit-protection
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)
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 "\\)")))
885 (or (assq 'outline-mode minor-mode-alist)
886 (setq minor-mode-alist
887 (cons '(outline-mode " Outl") minor-mode-alist)))
889 (if outline-enwrap-isearch-mode
890 (outline-enwrap-isearch))
891 (run-hooks 'outline-mode-hook)
892 (setq outline-mode t))
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
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."
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.
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
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))
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."
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
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."
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."
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
992 (if (outline-goto-prefix)
993 (outline-recent-depth)
995 ;; Oops, no prefix, zero prefix data:
996 (outline-prefix-data (point)(point))
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."
1004 (if (outline-back-to-current-heading)
1006 (- outline-recent-prefix-end
1007 outline-recent-prefix-beginning
1008 outline-header-subtraction))
1010 ;;;_ > outline-get-current-prefix ()
1011 (defun outline-get-current-prefix ()
1012 "Topic prefix of the current topic."
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)."
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."
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))
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))))
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.
1046 Returns the location of the heading, or nil if none found."
1048 '(if (and (bobp) (not (eobp)))
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.
1061 Return the location of the beginning of the heading, or nil if not found."
1065 (outline-goto-prefix)
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))))))
1076 ;;;_ - Subtree Charting
1077 ;;;_ " These routines either produce or assess charts, which are
1078 ;;; nested lists of the locations of topics within a subtree.
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.
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.
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.
1099 The function parameters are for internal recursion, and should not be
1100 designated by external callers."
1102 ;; We're constantly looking ahead. Impressive, huh?
1104 (let ((original (not orig-level)) ; 'here' passed only during recursion.
1106 ; Initialize if not passed in:
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)))
1114 ;; Loop, rather than recurse, over the current levels' siblings, to
1115 ;; avoid overloading the typically quite constrained emacs max-lisp-
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))
1125 ((< prev-level level) ; Do higher level, then
1126 ; continue with this one:
1127 (setq chart (cons (outline-chart-subtree orig-level
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))
1139 (forward-char -1))))
1141 chart ; (nreverse chart) not necessary,
1142 ; and maybe not preferable.
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."
1149 '(if (outline-goto-prefix)
1150 (let ((here (point))
1151 (subtree (outline-chart-subtree orig-level prev-level)))
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."
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))))))
1167 ;;;_ > outline-chart-to-reveal (chart depth)
1168 (defun outline-chart-to-reveal (chart depth)
1170 "Return a flat list of the points in subtree CHART, up to DEPTH, which
1173 Note that point can wind up at any of the points on chart, or at the
1177 (while (and (or (eq depth t) (> depth 0))
1179 (setq here (car chart))
1181 (let ((further (outline-chart-to-reveal here (or (eq depth t)
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)))
1188 (if (= (preceding-char) ?\r)
1189 (setq result (cons here result)))
1190 (setq chart (cdr chart))))
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.
1202 The produced list can have two types of entries. Bare numbers
1203 indicate points in the buffer where topic headers that should be
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
1210 - Lists signify the beginning and end points of regions that should
1211 be flagged, and the flag to employ. (For concealment: '\(\?r\)', and
1217 (setq spec (cdr spec)))
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.
1226 Returns a the point at the beginning of the prefix, or nil if none."
1228 (if (= (point-min)(point-max))
1231 (while (and (not done)
1232 (re-search-backward "[\n\r]" nil 1))
1234 (if (looking-at outline-regexp)
1235 (setq done (outline-prefix-data (match-beginning 0)
1239 (looking-at outline-regexp))
1240 (outline-prefix-data (match-beginning 0)(match-end 0))
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."
1247 (if (not (outline-goto-prefix))
1249 (let ((match-data (match-data)))
1250 (goto-char (match-end 0))
1251 (if ignore-decorations
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."
1262 (if (not (outline-current-depth))
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."
1270 (prog1 (or (outline-on-current-heading-p)
1271 (and (re-search-backward (concat "^\\(" outline-regexp "\\)")
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.
1280 Returns that character position."
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."
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)
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.
1307 If already there, move cursor to bullet for hot-spot operation.
1308 \(See outline-mode doc string for details on hot-spot operation.)"
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."
1319 (outline-show-entry)
1320 (prog1 (outline-pre-next-preface)
1321 (if (and (not (bobp))(looking-at "^$"))
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)
1338 (goto-char last-good)
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)))
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))
1355 (goto-char start-point)
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."
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)
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)))
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)))
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.
1389 Traverse at optional DEPTH, or current depth if none specified.
1391 Go backward if optional arg BACKWARD is non-nil.
1393 Return depth if successful, nil otherwise."
1395 (if (and backward (bobp))
1397 (let ((start-depth (or depth (outline-depth)))
1398 (start-point (point))
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)
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
1416 Optional DEPTH specifies depth to traverse, default current depth.
1418 Optional BACKWARD reverses direction.
1420 Return depth if successful, nil otherwise."
1421 (outline-next-sibling depth (not backward))
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))
1430 (if (or (bobp) (not (memq (preceding-char) '(?\n ?\^M))))
1433 (if (or (bobp) (not (memq (preceding-char) '(?\n ?\^M))))
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."
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.
1453 With argument, repeats, backward if negative."
1456 (let* ((backward (if (< arg 0) (setq arg (* -1 arg))))
1457 (step (if backward -1 1))
1460 (while (> arg 0) ; limit condition
1461 (while (and (not (if backward (bobp)(eobp))) ; boundary condition
1463 ;; Move, skipping over all those concealed lines:
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.
1479 With argument, repeats or can move forward if negative.
1480 A heading line is one that starts with a `*' (or that outline-regexp
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.
1489 Non-nil optional arg BACKWARD reverses direction.
1491 Returns that position, else nil if is not found."
1493 (if (and (< arg 0) (bobp))
1495 (let ((start-depth (save-excursion
1496 (outline-back-to-current-heading)
1498 (start-point (point))
1499 (backward (> 0 arg))
1501 (last-good (point)))
1502 (if backward (setq arg (* -1 arg)))
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)
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))
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.
1528 Returns that position, else nil if is not found."
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)))
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.
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.
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.)
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.)
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
1571 (save-excursion (if (memq this-command '(newline open-line))
1572 ;; Compensate for stupid emacs {new,
1573 ;; open-}line display optimization:
1577 (or (outline-hidden-p)
1578 (and (not (= beg end))
1580 (outline-hidden-p))))))
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
1588 ;; - Undo may be users' only recourse in protection faults.
1589 ;; So, expose what getting changed:
1590 (progn (message "Undo! - exposing concealed target...")
1592 (if (outline-hidden-p)
1593 (outline-show-children))
1597 (rehide-completely (save-excursion (outline-goto-prefix)
1598 (outline-hidden-p)))
1602 (if (condition-case err
1603 ;; Condition case to catch keyboard quits during reads.
1605 ; Give them a peek where
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:
1616 (message (concat "Change inside concealed"
1618 "(n or 'y'/'r'eclose)"))
1619 (setq response (read-char))
1621 (cond ((memq response '(?r ?R))
1622 (setq response 'reclose))
1623 ((memq response '(?y ?Y ? ))
1625 ((memq response '(?n ?N 127))
1630 "'r' means 'yes, then reclose")
1632 (t (message "Please answer y, n, or r")
1638 (if (eq response 'reclose)
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)))
1648 (if rehide-completely
1650 (if rehide-place (goto-char rehide-place))
1651 (outline-hide-current-entry-completely))
1652 (outline-hide-current-entry))
1654 "Change within concealed region prevented.")))))))
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.
1670 - Nulls outline-override-protect, so it's not left open.
1672 - Implements (and clears) outline-post-goto-bullet, for hot-spot
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."
1679 ; Apply any external change func:
1680 (if (outline-mode-p) ; In outline-mode.
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.
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))
1704 ;; prev contents denote fewer than aggregate-limit
1706 (numberp (setq prev-from (car prev-cell)))
1707 (numberp (setq prev-to (cdr prev-cell)))
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)))
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.
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
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."
1734 (if (and (eq this-command 'self-insert-command)
1735 (eq (point)(outline-current-bullet-pos)))
1737 (let* ((this-key-num (if (numberp last-command-event)
1738 last-command-event))
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))))))
1753 (setq outline-post-goto-bullet t
1754 this-command mapped-binding)))))
1756 ;;;_ - Topic Format Assessment
1757 ;;;_ > outline-solicit-alternate-bullet (depth &optional current-bullet)
1758 (defun outline-solicit-alternate-bullet (depth &optional current-bullet)
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."
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): "
1772 (if (string= choice "") default-bullet choice))
1774 ;;;_ > outline-sibling-index (&optional depth)
1775 (defun outline-sibling-index (&optional depth)
1776 "Item number of this prospective topic among its siblings.
1778 If optional arg depth is greater than current depth, then we're
1779 opening a new level, and return 0.
1781 If less than this depth, ascend to that depth and count..."
1784 (cond ((and depth (<= depth 0) 0))
1785 ((or (not depth) (= depth (outline-depth)))
1787 (while (outline-previous-sibling (outline-recent-depth) nil)
1788 (setq index (1+ index)))
1790 ((< depth (outline-recent-depth))
1791 (outline-ascend-to-depth depth)
1792 (outline-sibling-index))
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
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
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)
1818 ;;;_ - Topic Production
1819 ;;;_ > outline-make-topic-prefix (&optional prior-bullet
1820 (defun outline-make-topic-prefix (&optional prior-bullet
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.
1831 "Generate a topic prefix suitable for optional arg DEPTH, or current
1832 depth if not specified.
1834 All the arguments are optional.
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,
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.
1848 Third arg DEPTH forces the topic prefix to that depth, regardless of
1849 the current topics' depth.
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.)
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.
1860 \(Note that NUMBER-CONTROL does *not* apply to level 1 topics. Sorry...)
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)."
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
1881 (depth (or depth (outline-depth)))
1882 (header-lead outline-header-prefix)
1885 ;; Getting value for bullet char is practically the whole job:
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
1894 (setq body (make-string depth
1895 (string-to-char outline-primary-bullet)))
1896 (setq header-lead "")
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.)
1902 ;; Solicitation overrides numbering and other cases:
1903 ((progn (setq body (make-string (- depth 2) ?\ ))
1904 ;; The actual condition:
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:
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
1922 (outline-bullet-for-depth depth))))
1924 ;;; Neither soliciting nor controlled numbering ;;;
1925 ;;; (may be controlled denumbering, tho) ;;;
1927 ;; Check wrt previous sibling:
1928 ((and new ; only check for new prefixes
1929 (<= depth (outline-depth))
1930 outline-numbered-bullet ; ... & numbering enabled
1932 (let ((sibling-bullet
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)))))
1942 ;; Distinctive 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)))
1953 ;; Else, standard bullet per depth:
1954 ((outline-bullet-for-depth depth)))))
1960 (format "%d" (cond ((and index (numberp index)) index)
1961 (new (1+ (outline-sibling-index depth)))
1962 ((outline-sibling-index))))))
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
1974 - Creation of new topics is with respect to the visible topic
1975 containing the cursor, regardless of intervening concealed ones.
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.
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.
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
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
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))))
2016 (if (not opening-on-blank)
2017 ; Positioning and vertical
2018 ; padding - only if not
2021 (goto-char ref-topic)
2022 (setq dbl-space ; Determine double space action:
2023 (or (and (<= relative-depth 0) ; not descending;
2025 ;; at b-o-b or preceeded by a blank line?
2026 (or (> 0 (forward-line -1))
2027 (looking-at "^\\s-*$")
2030 ;; succeeded by a blank line?
2031 (outline-end-of-current-subtree)
2033 (and (= ref-depth 1)
2037 ;; Don't already have following
2038 ;; vertical padding:
2039 (not (outline-pre-next-preface)))))))
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))
2048 (outline-previous-heading)))
2049 (if (and before (bobp))
2050 (outline-unprotected (open-line 1))))
2052 (if (<= relative-depth 0)
2053 ;; Not going inwards, don't snug up:
2055 (outline-unprotected (open-line (if dbl-space 2 1)))
2057 (progn (end-of-line)
2058 (outline-pre-next-preface)
2059 (while (= ?\r (following-char))
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)
2069 ;; Blank lines between current header body and next
2070 ;; header - get to last substantive (non-white-space)
2072 (re-search-backward "[^ \t\n]" nil t))
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))))
2082 ;;(if doing-beginning (goto-char doing-beginning))
2084 (progn (if (and (not (> depth ref-depth))
2086 (outline-unprotected (open-line 1))
2087 (if (> depth ref-depth)
2088 (outline-unprotected (newline 1))
2090 (outline-unprotected (open-line 1))
2092 (outline-unprotected (newline 1))))))
2094 (outline-unprotected (newline 1)))
2095 (if (and (not (eobp))
2099 (insert-string (concat (outline-make-topic-prefix opening-numbered
2104 ;;(if doing-beginning (save-excursion (newline (if dbl-space 2 1))))
2107 (outline-rebullet-heading nil ;;; solicit
2109 nil ;;; number-control
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
2128 ;;;_ > outline-open-subtopic (arg)
2129 (defun outline-open-subtopic (arg)
2130 "Open new topic header at deeper level than the current one.
2132 Negative universal arg means to open deeper, but place the new topic
2133 prior to the current one."
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
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."
2150 (outline-open-topic -1 (> 0 arg)))
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:
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)
2171 (if (or outline-former-auto-filler outline-use-hanging-indents)
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.
2177 Optional arg NUMBER indicates numbering is being added, and it must
2180 Note that refill of indented paragraphs is not done."
2183 (outline-end-of-prefix)
2184 (let* ((new-margin (current-column))
2185 excess old-indent-begin old-indent-end
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))))
2192 ;; Process lines up to (but excluding) next topic header:
2193 (outline-unprotected
2196 (and (re-search-forward "[\n\r]\\(\\s-*\\)"
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)
2206 ;; Text starts left of old margin - don't adjust:
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.
2218 With repeat count, solicit for bullet."
2220 (save-excursion (outline-back-to-current-heading)
2221 (outline-end-of-prefix)
2222 (outline-rebullet-heading (not arg) ;;; solicit
2224 nil ;;; number-control
2226 t) ;;; do-successors
2229 ;;;_ > outline-rebullet-heading (&optional solicit ...)
2230 (defun outline-rebullet-heading (&optional solicit
2236 "Adjust bullet of current topic prefix.
2238 All args are optional.
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.
2244 Second arg DEPTH forces the topic prefix to that depth, regardless
2245 of the topics current depth.
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.
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.
2261 Fifth arg DO-SUCCESSORS t means re-resolve count on succeeding
2264 Cf vars 'outline-stylish-prefixes', 'outline-old-style-prefixes',
2265 and 'outline-numbered-bullet', which all affect the behavior of
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
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)))))
2287 ;; New prefix probably different from old:
2288 ; get rid of old one:
2289 (outline-unprotected (delete-region mb me))
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))))
2299 ; Put in new prefix:
2300 (outline-unprotected (insert-string new-prefix))
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))
2307 ;; Recursively rectify successive siblings of orig topic if
2308 ;; caller elected for it:
2311 (while (outline-next-sibling new-depth nil)
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
2320 nil))))) ;;;(dont!)do-successors
2321 ) ; (if (and (= current-depth new-depth)...))
2322 ) ; let* ((current-depth (outline-depth))...)
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.
2329 With repeat count, shift topic depth by that amount."
2331 (let ((start-col (current-column))
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
2352 "Rebullet the topic at point, visible or invisible, and all
2353 contained subtopics. See outline-rebullet-heading for rebulleting
2356 All arguments are optional.
2358 First arg RELATIVE-DEPTH means to shift the depth of the entire
2361 The rest of the args are for internal recursive use by the function
2362 itself. The are STARTING-DEPTH, STARTING-POINT, and INDEX."
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))
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))))
2377 ;; Sanity check for excessive promotion done only on starting call:
2378 (and on-starting-call
2380 (> 0 (+ starting-depth relative-depth))
2381 (error "Attempt to shift topic out beyond level 1.")) ;;; ====>
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
2390 ;; Every contained topic will get hit,
2391 ;; and we have to get to outside ones
2393 nil) ;;; do-successors
2394 ;; ... and work on subsequent ones which are at greater depth:
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
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
2413 (if on-starting-call
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
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)))))
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.
2435 Returns final depth."
2437 ;; Proceed by level, processing subsequent siblings on each,
2438 ;; ascending until we get shallower than the start depth:
2440 (let ((ascender (outline-depth)))
2441 (while (and (not (eobp))
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
2456 nil ;;; number-control
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.
2464 With universal argument, denumber - assign default bullet to this
2465 topic and its siblings.
2467 With repeated universal argument (`^U^U'), solicit bullet for each
2468 rebulleting each topic at this level."
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))
2480 (outline-rebullet-heading use-bullet ;;; solicit
2482 t ;;; number-control
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
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
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."
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:
2511 ;; Ah, have to watch out for adjustments:
2512 (let* ((depth (outline-depth)))
2515 ; Provide some feedback:
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.
2527 Leaves primary topic's trailing vertical whitespace, if any."
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.
2536 (let* ((beg (prog1 (outline-back-to-current-heading)(beginning-of-line)))
2537 (depth (outline-recent-depth)))
2538 (outline-end-of-current-subtree)
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:
2546 (and (outline-next-heading)
2547 (>= (outline-recent-depth) depth))))
2550 (kill-region beg (point))
2553 (outline-renumber-to-depth depth))))
2554 ;;;_ > outline-yank-processing ()
2555 (defun outline-yank-processing (&optional arg)
2557 "Incidental outline-specific business to be done just after text yanks.
2558 Does depth adjustment of yanked topics, when:
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
2564 Also, adjusts numbering of subsequent siblings when appropropriate.
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.
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."
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)
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)))))
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))
2596 ;; Nil if adjustment unnecessary, otherwise depth to which
2597 ;; adjustment should be made:
2599 (and (goto-char subj-end)
2601 (goto-char subj-beg)
2602 (and (looking-at outline-regexp)
2605 (not (= (point) subj-beg)))
2606 (looking-at outline-regexp)
2607 (outline-prefix-data (match-beginning 0)
2609 (outline-recent-depth))))
2612 (setq rectify-numbering outline-numbered-bullet)
2614 ; Do the adjustment:
2616 (message "... yanking") (sit-for 0)
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
2626 ; successively excluding the
2627 ; last processed topic from
2628 ; the narrow region:
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))))
2634 (outline-rebullet-topic-grunt (- adjust-to-depth
2637 (if (setq more (not (bobp)))
2640 (narrow-to-region subj-beg (point))))))
2642 ;; Preserve new bullet if it's a distinctive one, otherwise
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:
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,
2656 (delete-region (point) (+ (point)
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
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
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.
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.
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
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.
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.
2702 Numbering of yanked topics, and the succesive siblings at the depth
2703 into which they're being yanked, is adjusted.
2705 Outline-yank-pop works with outline-yank just like normal yank-pop
2706 works with normal yank in non-outline buffers."
2709 (setq this-command 'yank)
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.
2719 Note - prefix changes to distinctive bullets will stick, if followed
2720 by pops to non-distinctive yanks. Bug..."
2723 (setq this-command 'yank)
2725 (if (outline-mode-p)
2726 (outline-yank-processing)))
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')."
2735 (if (not outline-file-xref-bullet)
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)
2743 (let* ((text-start outline-recent-prefix-end)
2744 (heading-end (progn (outline-pre-next-preface)
2746 (goto-char text-start)
2748 (if (re-search-forward "\\s-\\(\\S-*\\)" heading-end t)
2749 (buffer-substring (match-beginning 1) (match-end 1))))))
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? "
2762 (error "%s not found and can't be created" file-name)))
2763 (condition-case failure
2764 (find-file-other-window file-name)
2766 (error "%s not found" file-name))
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.
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))
2787 (> (outline-depth) curr-entry-level))))
2788 (if next-entry-exists
2789 (progn (beginning-of-line) (point))
2790 (goto-char (point-max))))
2792 ;;; Outline topic prefix and level adjustment funcs:
2794 ;;;_ #5 Exposure Control and Processing
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.
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)
2809 ;;;_ > outline-flag-current-subtree (flag)
2810 (defun outline-flag-current-subtree (flag)
2812 (outline-back-to-current-heading)
2813 (outline-flag-region (point)
2814 (progn (outline-end-of-current-subtree) (1- (point)))
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)
2822 "Produce a list representing exposed topics in current region.
2823 This list can then be used by 'outline-process-exposed' to manipulate
2826 List is composed of elements that may themselves be lists representing
2827 exposed components in subtopic.
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."
2838 (let* (strings pad result depth bullet beg next done) ; State vars.
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)))
2855 (while (> next (point)) ; Get all the exposed text in
2857 (cons (buffer-substring
2859 ;To hidden text or end of line:
2861 (search-forward "\r"
2862 (save-excursion (end-of-line)
2865 (if (= (preceding-char) ?\r)
2869 (if (< (point) next) ; Resume from after hid text, if any.
2872 ;; Accumulate list for this topic:
2874 (cons (append (list depth
2875 outline-header-prefix
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).
2889 The function must as its arguments the elements of the list
2890 representations of topic entries produced by outline-listify-exposed."
2892 ; Resolve arguments,
2893 ; defaulting if necessary:
2894 (if (not func) (setq func 'outline-insert-listified))
2895 (if (not (and from to))
2897 (setq from (region-beginning) to (region-end))
2898 (setq from (point-min) to (point-max))))
2900 (if (not (bufferp frombuf))
2901 ;; Specified but not a buffer - get it:
2902 (let ((got (get-buffer frombuf)))
2904 (error (concat "outline-process-exposed: source buffer "
2907 (setq frombuf got))))
2908 ;; not specified - default it:
2909 (setq frombuf (current-buffer)))
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*")))
2916 (let* ((listified (progn (set-buffer frombuf)
2917 (outline-listify-exposed from to)))
2918 (prefix outline-header-prefix) ; ... as set in frombuf.
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)))
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.
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."
2944 (outline-goto-prefix)
2945 (outline-flag-region (if (bobp) (point) (1- (point)))
2946 (or (outline-pre-next-preface) (point))
2948 ;;;_ > outline-show-children (&optional level strict)
2949 (defun outline-show-children (&optional level strict)
2951 "If point is visible, show all direct subheadings of this heading.
2952 Otherwise, do outline-show-to-offshoot, and then show subheadings.
2954 Optional LEVEL specifies how many levels below the current level
2955 should be shown, or all levels if t. Default is 1.
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
2963 (if (and (not strict)
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))
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))
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.
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."
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
3009 Specifically intended for aberrant exposure states, like entries that were
3010 exposed by outline-show-entry but are within otherwise concealed regions."
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))
3020 ;;;_ > outline-show-to-offshoot ()
3021 (defun outline-show-to-offshoot ()
3022 "Like outline-show-entry, but reveals opens all concealed ancestors,
3025 Like outline-hide-current-entry-completely, useful for rectifying aberrant
3026 exposure states produced by outline-show-entry."
3030 (let ((orig-pt (point))
3031 (orig-pref (outline-goto-prefix))
3034 (while (or bag-it (= (preceding-char) ?\r))
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)
3045 "outline-show-to-offshoot: "
3046 "Aberrant nesting encountered.")))
3047 (outline-show-children)
3048 (goto-char orig-pref))
3050 (outline-show-entry))))
3051 ;;;_ > outline-hide-current-entry ()
3052 (defun outline-hide-current-entry ()
3053 "Hide the body directly following this heading."
3055 (outline-back-to-current-heading)
3057 (outline-flag-region (point)
3058 (progn (outline-end-of-current-entry) (point))
3060 ;;;_ > outline-show-current-entry (&optional arg)
3061 (defun outline-show-current-entry (&optional arg)
3063 "Show body following current heading, or hide the entry if repeat
3068 (outline-hide-current-entry)
3070 (outline-flag-region (point)
3071 (progn (outline-end-of-current-entry) (point))
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.
3078 Specifically intended for aberrant exposure states, like entries that were
3079 exposed by outline-show-entry but are within otherwise concealed regions."
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))
3089 ;;;_ > outline-show-current-subtree ()
3090 (defun outline-show-current-subtree ()
3091 "Show everything after this heading at deeper levels."
3093 (outline-flag-current-subtree ?\n))
3094 ;;;_ > outline-hide-current-subtree (&optional just-close)
3095 (defun outline-hide-current-subtree (&optional just-close)
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
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)
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."
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."
3122 (outline-back-to-current-heading)
3123 (outline-hide-region-body (point) (progn (outline-end-of-current-subtree)
3126 ;;;_ - Region and beyond
3127 ;;;_ > outline-show-all ()
3128 (defun outline-show-all ()
3129 "Show all of the text in the buffer."
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."
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."
3142 (narrow-to-region start end)
3143 (goto-char (point-min))
3145 (outline-flag-region (point)
3146 (progn (outline-pre-next-preface) (point)) ?\^M)
3149 (if (looking-at "[\n\r][\n\r]")
3151 ;;;_ > outline-expose-topic (spec &optional prev-spec)
3152 (defun outline-expose-topic (spec &optional prev-spec)
3154 "Dictate wholesale exposure scheme for current level.
3156 Unless you want the args to be evaluated, you probably want to use the
3157 frontend `outline-new-exposure', instead.
3159 Cursor is left at start position.
3161 SPEC is either a number or, recursively, a list.
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.
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 ':'.
3171 Simple (numeric and null-list) specs are interpreted as follows:
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
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.
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.
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.
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
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."
3209 (interactive "xExposure spec: ")
3210 (let ((depth (outline-current-depth))
3213 (cond ((null spec) nil)
3215 (cond ((eq spec '*) (outline-show-current-subtree))
3216 ((eq spec '+) (outline-show-current-branches))
3217 ((eq spec '-) (outline-show-current-entry))
3219 ;; Whoops. ':' should have been caught at superior
3222 "outline-expose-topic: improper exposure spec - bare ':'"))))
3225 (save-excursion (outline-hide-current-subtree t)
3227 (if (or (not max-pos)
3228 (> (point) max-pos))
3229 (setq max-pos (point)))
3231 (setq spec (* -1 spec)))))
3233 (outline-show-children spec)))
3235 (if (eq (car 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))))
3245 ;; remaining spec covers all - just use it:
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)
3251 (setq prev-spec (car spec))
3252 (setq spec (cdr spec))
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))))))
3263 ;;;_ > outline-old-expose-topic (spec &rest followers)
3264 (defun outline-old-expose-topic (spec &rest followers)
3266 "Dictate wholesale exposure scheme for current topic, according to SPEC.
3268 SPEC is either a number or a list. Optional successive args
3269 dictate exposure for subsequent siblings of current topic.
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.
3277 Simple (numeric and null-list) specs are interpreted as follows:
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.
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.
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.
3293 Optional FOLLOWER arguments dictate exposure for succeeding siblings."
3295 (interactive "xExposure spec: ")
3296 (let ((depth (outline-current-depth))
3299 (cond ((null spec) nil)
3301 (if (eq spec '*) (outline-show-current-subtree))
3302 (if (eq spec '+) (outline-show-current-branches))
3303 (if (eq spec '-) (outline-show-current-entry)))
3306 (save-excursion (outline-hide-current-subtree t)
3308 (if (or (not max-pos)
3309 (> (point) max-pos))
3310 (setq max-pos (point)))
3312 (setq spec (* -1 spec)))))
3314 (outline-show-children 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))
3321 (setq max-pos (outline-old-expose-topic (car spec)))
3322 (setq spec (cdr 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)))
3334 (outline-next-sibling depth)))
3335 (outline-old-expose-topic (car followers))
3336 (setq followers (cdr followers)))
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.
3344 Cursor is left at start position.
3346 Use this instead of obsolete 'outline-exposure'.
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.
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))))
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.")
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
3399 The function checks to ensure that the rebindings are done only once."
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))
3406 ;; ... no - skip this all:
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'"
3418 (car (cdr (cdr error))))
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:
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
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.
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."
3460 (if (outline-mode-p)
3461 (setq outline-search-reconceal
3462 (if (outline-hidden-p)
3464 (if (re-search-backward outline-line-boundary-regexp nil 1)
3465 ;; Nil value means we got to b-o-b - wouldn't need
3468 ; We'll return point or list
3469 ; containing point, depending
3470 ; on concealment state of
3472 (prog1 (if (outline-hidden-p) (point) (list (point)))
3473 ; And reveal the current
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)
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 ()
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)
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:
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:
3513 (progn (forward-char -1) ; newline
3514 (if (eq ?\r (preceding-char))
3515 (outline-flag-region (1- (point))
3520 (outline-ascend-to-depth (1- (outline-recent-depth)))
3521 (outline-show-children)))
3522 (if (and (boundp 'outline-search-quitting)
3523 outline-search-quitting)
3525 ; We're concluding abort:
3526 (outline-isearch-arrival-business)
3527 (outline-show-children))))
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)
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)))))
3546 ;;;_ #7 Copying and printing
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) ?\ )
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.
3564 With repeat count, copy the exposed portions of entire buffer."
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
3579 (goto-char (point-min))
3581 (goto-char start-pt)))
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))))
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."
3603 (end (progn (end-of-line)(point))))
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 "\\")
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.
3619 (let ((doc-style (format "\n\\documentstyle{%s}\n"
3621 (page-numbering (if outline-number-pages
3622 "\\pagestyle{empty}\n"
3624 (linesdef (concat "\\def\\beginlines{"
3625 "\\par\\begingroup\\nobreak\\medskip"
3627 " \\kern1pt\\nobreak \\obeylines \\obeyspaces "
3628 "\\everypar{\\strut}}\n"
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}{"
3644 (oneheadline (format "%s%s%s%s%s%s%s"
3645 "\\newcommand{\\OneHeadLine}[3]{%\n"
3647 "\\hspace*{#2\\stepsize}%\n"
3648 "\\labelcmd{#1}\\hspace*{.2cm}"
3649 "\\headlinecmd{#3}\\\\["
3652 (onebodyline (format "%s%s%s%s%s%s"
3653 "\\newcommand{\\OneBodyLine}[2]{%\n"
3655 "\\hspace*{#1\\stepsize}%\n"
3656 "\\bodylinecmd{#2}\\\\["
3659 (begindoc "\\begin{document}\n\\begin{center}\n")
3660 (title (format "%s%s%s%s"
3662 (outline-latex-verb-quote (if outline-title
3664 (eval outline-title)
3665 (error "<unnamed buffer>"))
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
3687 ;;;_ > outline-insert-latex-trailer (buf)
3688 (defun outline-insert-latex-trailer (buf)
3689 "Insert concluding latex commands at point in BUFFER."
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))
3703 (insert-string (concat "\\OneHeadLine{\\verb\1 "
3704 (outline-latex-verb-quote bullet)
3709 (outline-latex-verb-quote head-line)
3712 (if (not body-lines)
3714 ;;(insert-string "\\beginlines\n")
3715 (insert-string "\\begin{verbatim}\n")
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,
3724 (if (and body-content
3725 (setq bop (string-match "\\end{verbatim}" curr-line)))
3726 (setq curr-line (concat (substring curr-line 0 bop)
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)))
3735 (setq body-content nil)
3737 (insert-string "\\ ")
3739 ;;(insert-string "\\endlines\n")
3740 (insert-string "\\end{verbatim}\n")
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*\"
3749 With repeat count, copy the exposed portions of entire buffer."
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)))
3761 (outline-insert-latex-header tobuf)
3762 (goto-char (point-max))
3763 (outline-process-exposed 'outline-latexify-one-item
3768 (goto-char (point-max))
3769 (outline-insert-latex-trailer tobuf)
3770 (goto-char (point-min))
3772 (goto-char start-pt)))
3775 ;;;_ #8 miscellaneous
3776 ;;;_ > outline-mark-topic ()
3777 (defun outline-mark-topic ()
3778 "Put the region around topic currently containing point."
3781 (outline-goto-prefix)
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.
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."
3794 (interactive "P") (outline-mode t)
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))))
3805 (outline-lead-with-comment-string (cdr leader-cell))
3806 (setq arg (cdr leader-cell)))))
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
3815 "'(This topic sets initial outline exposure"
3816 "of the file when loaded by emacs,"
3817 "Encapsulate it in comments if"
3819 "otherwise ignore it,")))
3822 ; Put a topic at the top, if
3823 ; none there already:
3824 (goto-char (point-min))
3825 (if (not (looking-at outline-regexp))
3827 (if (not arg) outline-primary-bullet
3828 (format "%s%s\n" outline-header-prefix outline-primary-bullet))))
3830 ; File-vars stuff, at the bottom:
3831 (goto-char (point-max))
3833 (insert-string (format "\n\n%s\n%s %s %s\n%s %s\n"
3840 ; Insert outline-mode activation:
3842 (format "\t %s\n\t\t%s\n\t\t\t%s\n"
3843 "(condition-case err"
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"
3851 ; Insert ammouncement and
3854 (format "\t\t\t%s %s\n\t\t\t%s %s\n\t\t%s %s"
3855 "(message \"Adjusting '%s' exposure\""
3858 "(outline-exposure -1 0))"
3860 "\"Failed file var 'allout' provisions\")))"))
3862 (insert-string (format "\n%s End:\n)\n"
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.
3868 Optional arg DO-DEFAULTING indicates to accept empty input (CR)."
3870 (let ((new-prompt prompt)
3874 (message "%s" new-prompt)
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.)
3880 (char-to-string (let ((cursor-in-echo-area nil)) (read-char))))
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:
3886 ;; Failed match and not defaulting,
3887 ;; set the prompt to give feedback,
3888 (setq new-prompt (concat prompt
3893 ;; and set loop to try again:
3895 ;; Got a match - give feedback:
3897 ;; got something out of loop - return it:
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
3906 Optional arg SUCCESSIVE-BACKSLASHES is used internally for recursion."
3908 (if (string= regexp "")
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)
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)))
3940 (nconc (symbol-value hook) (list function))
3941 (cons function (symbol-value hook)))))))
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."
3949 (setq bullet (solicit-char-in-string
3950 "ISearch for topic with bullet: "
3951 (regexp-sans-escapes outline-bullets-string))))
3953 (let ((isearch-regexp t)
3954 (isearch-string (concat "^"
3955 outline-header-prefix
3958 (isearch-repeat 'forward)
3960 ;;;_ - Re hooking up with isearch - use isearch-op-fun rather than
3961 wrapping the isearch functions.
3963 ;;;_* Local emacs vars.
3966 eval: (save-excursion
3967 (if (not (condition-case err (outline-mode t)
3968 (wrong-number-of-arguments nil)))
3971 "Allout outline-mode not loaded, not adjusting buffer exposure")
3973 (message "Adjusting '%s' exposure" (buffer-name))
3974 (outline-lead-with-comment-string "\;\;\;_")
3976 (outline-new-exposure 0 : -1 -1 0)))