1 ;; allout-widgets.el --- Visually highlight allout outline structure.
3 ;; Copyright (C) 2005-2012 Free Software Foundation, Inc.
5 ;; Author: Ken Manheimer <ken dot manheimer at gmail...>
6 ;; Maintainer: Ken Manheimer <ken dot manheimer at gmail...>
10 ;; Website: http://myriadicity.net/software-and-systems/craft/emacs-allout
12 ;; This file is part of GNU Emacs.
14 ;; GNU Emacs is free software: you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation, either version 3 of the License, or
17 ;; (at your option) any later version.
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
29 ;; This is an allout outline-mode add-on that highlights outline structure
30 ;; with graphical widgets.
32 ;; To activate, customize `allout-widgets-auto-activation'. You can also
33 ;; invoke allout-widgets-mode in a particular allout buffer. When
34 ;; auto-enabled, you can inhibit widget operation in particular allout
35 ;; buffers by setting the variable `allout-widgets-mode-inhibit' non-nil in
36 ;; that file's buffer. Use emacs *file local variables* to generally
37 ;; inhibit for a file.
39 ;; See the `allout-widgets-mode' docstring for more details.
41 ;; Info about allout and allout-widgets development are available at
42 ;; http://myriadicity.net/Sundry/EmacsAllout
44 ;; The graphics include:
46 ;; - icons for item bullets, varying to distinguish whether the item either
47 ;; lacks any subitems, the subitems are currently collapsed within the
48 ;; item, or the item is currently expanded.
50 ;; - guide lines connecting item bullet-icons with those of their subitems.
52 ;; - cue area between the bullet-icon and the start of the body headline,
53 ;; for item numbering, encryption indicator, and distinctive bullets.
55 ;; The bullet-icon and guide line graphics provide keybindings and mouse
56 ;; bindings for easy outline navigation and exposure control, extending
57 ;; outline hot-spot navigation (see `allout-mode' docstring for details).
59 ;; Developers note: Our use of emacs widgets is unconventional. We
60 ;; decorate existing text rather than substituting for it, to
61 ;; piggy-back on existing allout operation. This employs the C-coded
62 ;; efficiencies of widget-apply, widget-get, and widget-put, along
63 ;; with the basic object-oriented organization of widget-create, to
64 ;; systematically couple overlays, graphics, and other features with
65 ;; allout-governed text.
69 ;;;_ : General Environment
80 ;;;_ : internal variables needed before user-customization variables
81 ;;; In order to enable activation of allout-widgets-mode via customization,
82 ;;; allout-widgets-auto-activation uses a setting function. That function
83 ;;; is invoked when the customization variable definition is evaluated,
84 ;;; during file load, so the involved code must reside above that
85 ;;; definition in the file.
86 ;;;_ = allout-widgets-mode
87 (defvar allout-widgets-mode nil
88 "Allout mode enhanced with graphical widgets.")
89 (make-variable-buffer-local 'allout-widgets-mode
)
91 ;;;_ : USER CUSTOMIZATION VARIABLES and incidental functions:
92 ;;;_ > defgroup allout-widgets
94 (defgroup allout-widgets nil
95 "Allout extension that highlights outline structure graphically.
97 Customize `allout-widgets-auto-activation' to activate allout-widgets
100 ;;;_ > defgroup allout-widgets-developer
101 (defgroup allout-widgets-developer nil
102 "Settings for development of allout widgets extension."
103 :group
'allout-widgets
)
104 ;;;_ ; some functions a bit early, for allout-auto-activation dependency:
105 ;;;_ > allout-widgets-mode-enable
106 (defun allout-widgets-mode-enable ()
107 "Enable allout-widgets-mode in allout-mode buffers.
109 See `allout-widgets-mode-inhibit' for per-file/per-buffer
110 inhibition of allout-widgets-mode."
111 (add-hook 'allout-mode-off-hook
'allout-widgets-mode-off
)
112 (add-hook 'allout-mode-on-hook
'allout-widgets-mode-on
)
114 ;;;_ > allout-widgets-mode-disable
115 (defun allout-widgets-mode-disable ()
116 "Disable allout-widgets-mode in allout-mode buffers.
118 See `allout-widgets-mode-inhibit' for per-file/per-buffer
119 inhibition of allout-widgets-mode."
120 (remove-hook 'allout-mode-off-hook
'allout-widgets-mode-off
)
121 (remove-hook 'allout-mode-on-hook
'allout-widgets-mode-on
)
123 ;;;_ > allout-widgets-setup (varname value)
125 (defun allout-widgets-setup (varname value
)
126 "Commission or decommission allout-widgets-mode along with allout-mode.
128 Meant to be used by customization of `allout-widgets-auto-activation'."
129 (set-default varname value
)
130 (if allout-widgets-auto-activation
131 (allout-widgets-mode-enable)
132 (allout-widgets-mode-disable)))
133 ;;;_ = allout-widgets-auto-activation
135 (defcustom allout-widgets-auto-activation nil
136 "Activate to enable allout icon graphics wherever allout mode is active.
138 Also enable `allout-auto-activation' for this to take effect upon
141 When this is set you can disable allout widgets in select files
142 by setting `allout-widgets-mode-inhibit'
144 Instead of setting `allout-widgets-auto-activation' you can
145 explicitly invoke `allout-widgets-mode' in allout buffers where
146 you want allout widgets operation.
148 See `allout-widgets-mode' for allout widgets mode features."
151 :group
'allout-widgets
152 :set
'allout-widgets-setup
154 ;; ;;;_ = allout-widgets-allow-unruly-edits
155 ;; (defcustom allout-widgets-allow-unruly-edits nil
156 ;; "Control whether manual edits are restricted to maintain outline integrity.
158 ;; When nil, manual edits must either be within an item's body or encompass
159 ;; one or more items completely - eg, killing topics as entities, rather than
160 ;; deleting from the middle of one to the middle of another.
162 ;; If you only occasionally need to make unrestricted change, you can set this
163 ;; variable in the specific buffer using set-variable, or just deactivate
164 ;; `allout-mode' temporarily. You can customize this to always allow unruly
165 ;; edits, but you will be able to create outlines that are unnavigable in
166 ;; principle, and not just for allout's navigation and exposure mechanisms."
168 ;; :group allout-widgets)
169 ;; (make-variable-buffer-local 'allout-widgets-allow-unruly-edits)
170 ;;;_ = allout-widgets-auto-activation - below, for eval-order dependencies
171 ;;;_ = allout-widgets-icons-dark-subdir
172 (defcustom allout-widgets-icons-dark-subdir
"icons/allout-widgets/dark-bg/"
173 "Directory on `image-load-path' holding allout icons for dark backgrounds."
176 :group
'allout-widgets
)
177 ;;;_ = allout-widgets-icons-light-subdir
178 (defcustom allout-widgets-icons-light-subdir
"icons/allout-widgets/light-bg/"
179 "Directory on `image-load-path' holding allout icons for light backgrounds."
182 :group
'allout-widgets
)
183 ;;;_ = allout-widgets-icon-types
184 (defcustom allout-widgets-icon-types
'(xpm png
)
185 "File extensions for the icon graphic format types, in order of preference."
187 :type
'(repeat symbol
)
188 :group
'allout-widgets
)
190 ;;;_ . Decoration format
191 ;;;_ = allout-widgets-theme-dark-background
192 (defcustom allout-widgets-theme-dark-background
"allout-dark-bg"
193 "Identify the outline's icon theme to use with a dark background."
196 :group
'allout-widgets
)
197 ;;;_ = allout-widgets-theme-light-background
198 (defcustom allout-widgets-theme-light-background
"allout-light-bg"
199 "Identify the outline's icon theme to use with a light background."
202 :group
'allout-widgets
)
203 ;;;_ = allout-widgets-item-image-properties-emacs
204 (defcustom allout-widgets-item-image-properties-emacs
205 '(:ascent center
:mask
(heuristic t
))
206 "Default properties item widget images in mainline Emacs."
209 :group
'allout-widgets
)
210 ;;;_ = allout-widgets-item-image-properties-xemacs
211 (defcustom allout-widgets-item-image-properties-xemacs
213 "Default properties item widget images in XEmacs."
216 :group
'allout-widgets
)
218 ;;;_ = allout-widgets-run-unit-tests-on-load
219 (defcustom allout-widgets-run-unit-tests-on-load nil
220 "When non-nil, unit tests will be run at end of loading allout-widgets.
222 Generally, allout widgets code developers are the only ones who'll want to
225 \(If set, this makes it an even better practice to exercise changes by
226 doing byte-compilation with a repeat count, so the file is loaded after
229 See `allout-widgets-run-unit-tests' to see what's run."
232 :group
'allout-widgets-developer
)
233 ;;;_ = allout-widgets-time-decoration-activity
234 (defcustom allout-widgets-time-decoration-activity nil
235 "Retain timing info of the last cooperative redecoration.
237 The details are retained as the value of
238 `allout-widgets-last-decoration-timing'.
240 Generally, allout widgets code developers are the only ones who'll want to
244 :group
'allout-widgets-developer
)
245 ;;;_ = allout-widgets-hook-error-post-time 0
246 (defcustom allout-widgets-hook-error-post-time
0
247 "Amount of time to sit showing hook error messages.
249 0 is minimal, or nil to not post to the message area.
251 This is for debugging purposes."
254 :group
'allout-widgets-developer
)
255 ;;;_ = allout-widgets-maintain-tally nil
256 (defcustom allout-widgets-maintain-tally nil
257 "If non-nil, maintain a collection of widgets, `allout-widgets-tally'.
259 This is for debugging purposes.
261 The tally shows the total number of item widgets in the current
262 buffer, and tracking increases as new widgets are added and
263 decreases as obsolete widgets are garbage collected."
266 :group
'allout-widgets-developer
)
267 (defvar allout-widgets-tally nil
268 "Hash-table of existing allout widgets, for debugging.
270 Table is maintained iff `allout-widgets-maintain-tally' is non-nil.
272 The table contents will be out of sync if any widgets are created
273 or deleted while this variable is nil.")
274 (make-variable-buffer-local 'allout-widgets-tally
)
275 (defvar allout-widgets-mode-inhibit
) ; defined below
276 ;;;_ > allout-widgets-tally-string
277 (defun allout-widgets-tally-string ()
278 "Return a string giving the number of tracked widgets, or empty string if not tracking.
280 The string is formed for appending to the allout-mode mode-line lighter.
282 An empty string is also returned if tracking is inhibited or
283 widgets are locally inhibited.
285 The number varies according to the evanescence of objects on a
286 hash table with weak keys, so tracking of widget erasures is often delayed."
287 (when (and allout-widgets-maintain-tally
288 (not allout-widgets-mode-inhibit
)
289 allout-widgets-tally
)
290 (format ":%s" (hash-table-count allout-widgets-tally
))))
291 ;;;_ = allout-widgets-track-decoration nil
292 (defcustom allout-widgets-track-decoration nil
293 "If non-nil, show cursor position of each item decoration.
295 This is for debugging purposes, and generally set at need in a
296 buffer rather than as a prevailing configuration \(but it's handy
297 to publicize it by making it a customization variable\)."
300 :group
'allout-widgets-developer
)
301 (make-variable-buffer-local 'allout-widgets-track-decoration
)
303 ;;;_ : Mode context - variables, hookup, and hooks
304 ;;;_ . internal mode variables
305 ;;;_ , Mode activation and environment
306 ;;;_ = allout-widgets-version
307 (defvar allout-widgets-version
"1.0"
308 "Version of currently loaded allout-widgets extension.")
309 ;;;_ > allout-widgets-version
310 (defun allout-widgets-version (&optional here
)
311 "Return string describing the loaded outline version."
313 (let ((msg (concat "Allout Outline Widgets Extension v "
314 allout-widgets-version
)))
315 (if here
(insert msg
))
318 ;;;_ = allout-widgets-mode-inhibit
319 (defvar allout-widgets-mode-inhibit nil
320 "Inhibit `allout-widgets-mode' from activating widgets.
322 This also inhibits automatic adjustment of widgets to track allout outline
325 You can use this as a file local variable setting to disable
326 allout widgets enhancements in selected buffers while generally
327 enabling widgets by customizing `allout-widgets-auto-activation'.
329 In addition, you can invoked `allout-widgets-mode' allout-mode
330 buffers where this is set to enable and disable widget
331 enhancements, directly.")
333 (put 'allout-widgets-mode-inhibit
'safe-local-variable
334 (if (fboundp 'booleanp
) 'booleanp
(lambda (x) (member x
'(t nil
)))))
335 (make-variable-buffer-local 'allout-widgets-mode-inhibit
)
336 ;;;_ = allout-inhibit-body-modification-hook
337 (defvar allout-inhibit-body-modification-hook nil
338 "Override de-escaping of text-prefixes in item bodies during specific changes.
340 This is used by `allout-buffer-modification-handler' to signal such changes
341 to `allout-body-modification-handler', and is always reset by
342 `allout-post-command-business'.")
343 (make-variable-buffer-local 'allout-inhibit-body-modification-hook
)
344 ;;;_ = allout-widgets-icons-cache
345 (defvar allout-widgets-icons-cache nil
346 "Cache allout icon images, as an association list.
348 `allout-fetch-icon-image' uses this cache transparently, keying
349 images with lists containing the name of the icon directory \(as
350 found on the `load-path') and the icon name.
352 Set this variable to `nil' to empty the cache, and have it replenish from the
354 ;;;_ = allout-widgets-unset-inhibit-read-only
355 (defvar allout-widgets-unset-inhibit-read-only nil
356 "Tell `allout-widgets-post-command-business' to unset `inhibit-read-only'.
358 Used by `allout-graphics-modification-handler'")
359 ;;;_ = allout-widgets-reenable-before-change-handler
360 (defvar allout-widgets-reenable-before-change-handler nil
361 "Tell `allout-widgets-post-command-business' to reequip the handler.
363 Necessary because the handler sometimes deliberately raises an
364 error, causing it to be disabled.")
365 ;;;_ , State for hooks
366 ;;;_ = allout-unresolved-body-mod-workroster
367 (defvar allout-unresolved-body-mod-workroster
(make-hash-table :size
16)
368 "List of body-overlays that did before-change business but not after-change.
370 See `allout-post-command-business' and `allout-body-modification-handler'.")
371 ;;;_ = allout-structure-unruly-deletion-message
372 (defvar allout-structure-unruly-deletion-message
373 "Unruly edit prevented --
374 To change the bullet character: \\[allout-rebullet-current-heading]
375 To promote this item: \\[allout-shift-out]
376 To demote it: \\[allout-shift-in]
377 To delete it and offspring: \\[allout-kill-topic]
378 See \\[describe-mode] for many more options."
379 "Informative message presented on improper editing of outline structure.
381 The structure includes the guides lines, bullet, and bullet cue.")
382 ;;;_ = allout-widgets-changes-record
383 (defvar allout-widgets-changes-record nil
384 "Record outline changes for processing by post-command hook.
386 Entries on the list are lists whose first element is a symbol indicating
387 the change type and subsequent elements are data specific to that change
390 'exposure `allout-exposure-from' `allout-exposure-to' `allout-exposure-flag'
392 The changes are recorded in reverse order, with new values pushed
394 (make-variable-buffer-local 'allout-widgets-changes-record
)
395 ;;;_ = allout-widgets-undo-exposure-record
396 (defvar allout-widgets-undo-exposure-record nil
397 "Record outline undo traces for processing by post-command hook.
399 The changes are recorded in reverse order, with new values pushed
401 (make-variable-buffer-local 'allout-widgets-undo-exposure-record
)
402 ;;;_ = allout-widgets-last-hook-error
403 (defvar allout-widgets-last-hook-error nil
404 "String holding last error string, for debugging purposes.")
405 ;;;_ = allout-widgets-adjust-message-length-threshold 100
406 (defvar allout-widgets-adjust-message-length-threshold
100
407 "Display \"Adjusting widgets\" message above this number of pending changes."
409 ;;;_ = allout-widgets-adjust-message-size-threshold 10000
410 (defvar allout-widgets-adjust-message-size-threshold
10000
411 "Display \"Adjusting widgets\" message above this size of pending changes."
413 ;;;_ = allout-doing-exposure-undo-processor nil
414 (defvar allout-undo-exposure-in-progress nil
415 "Maintained true during `allout-widgets-exposure-undo-processor'")
416 ;;;_ , Widget-specific outline text format
417 ;;;_ = allout-escaped-prefix-regexp
418 (defvar allout-escaped-prefix-regexp
""
419 "Regular expression for body text that would look like an item prefix if
420 not altered with an escape sequence.")
421 (make-variable-buffer-local 'allout-escaped-prefix-regexp
)
422 ;;;_ , Widget element formatting
423 ;;;_ = allout-item-icon-keymap
424 (defvar allout-item-icon-keymap
425 (let ((km (make-sparse-keymap)))
426 (dolist (digit '("0" "1" "2" "3"
427 "4" "5" "6" "7" "8" "9"))
428 (define-key km digit
'digit-argument
))
429 (define-key km
"-" 'negative-argument
)
430 ;; (define-key km [(return)] 'allout-tree-expand-command)
431 ;; (define-key km [(meta return)] 'allout-toggle-torso-command)
432 ;; (define-key km [(down-mouse-1)] 'allout-item-button-click)
433 ;; (define-key km [(down-mouse-2)] 'allout-toggle-torso-event-command)
434 ;; Override underlying mouse-1 and mouse-2 bindings in icon territory:
435 (define-key km
[(mouse-1)] (lambda () (interactive) nil
))
436 (define-key km
[(mouse-2)] (lambda () (interactive) nil
))
438 ;; Catchall, handles actual keybindings, dynamically doing keymap lookups:
439 (define-key km
[t] 'allout-item-icon-key-handler)
442 "General tree-node key bindings.")
443 ;;;_ = allout-item-body-keymap
444 (defvar allout-item-body-keymap
445 (let ((km (make-sparse-keymap))
446 (local-map (current-local-map)))
447 ;; (define-key km [(control return)] 'allout-tree-expand-command)
448 ;; (define-key km [(meta return)] 'allout-toggle-torso-command)
449 ;; XXX We need to reset this per buffer's mode; we do so in
450 ;; allout-widgets-mode.
452 (set-keymap-parent km local-map))
455 "General key bindings for the text content of outline items.")
456 (make-variable-buffer-local 'allout-item-body-keymap)
457 ;;;_ = allout-body-span-category
458 (defvar allout-body-span-category nil
459 "Symbol carrying allout body-text overlay properties.")
460 ;;;_ = allout-cue-span-keymap
461 (defvar allout-cue-span-keymap
462 (let ((km (make-sparse-keymap)))
463 (set-keymap-parent km allout-item-icon-keymap)
465 "Keymap used in the item cue area - the space between the icon and headline.")
466 ;;;_ = allout-escapes-category
467 (defvar allout-escapes-category nil
468 "Symbol for category of text property used to hide escapes of prefix-like
469 text in allout item bodies.")
470 ;;;_ = allout-guides-category
471 (defvar allout-guides-category nil
472 "Symbol carrying allout icon-guides overlay properties.")
473 ;;;_ = allout-guides-span-category
474 (defvar allout-guides-span-category nil
475 "Symbol carrying allout icon and guide lines overlay properties.")
476 ;;;_ = allout-icon-span-category
477 (defvar allout-icon-span-category nil
478 "Symbol carrying allout icon and guide lines overlay properties.")
479 ;;;_ = allout-cue-span-category
480 (defvar allout-cue-span-category nil
481 "Symbol carrying common properties of the space following the outline icon.
483 \(That space is used to convey selected cues indicating body qualities,
484 including things like:
487 - indirect reference '@'
488 - distinctive bullets - see `allout-distinctive-bullets-string'.\)")
489 ;;;_ = allout-span-to-category
490 (defvar allout-span-to-category
491 '((:guides-span . allout-guides-span-category)
492 (:cue-span . allout-cue-span-category)
493 (:icon-span . allout-icon-span-category)
494 (:body-span . allout-body-span-category))
495 "Association list mapping span identifier to category identifier.")
496 ;;;_ = allout-trailing-category
497 (defvar allout-trailing-category nil
498 "Symbol carrying common properties of an overlay's trailing newline.")
500 (defvar allout-widgets-last-decoration-timing nil
501 "Timing details for the last cooperative decoration action.
503 This is maintained when `allout-widgets-time-decoration-activity' is set.
505 The value is a list containing two elements:
506 - the elapsed time as a number of seconds
507 - the list of changes processed, a la `allout-widgets-changes-record'.
509 When active, the value is revised each time automatic decoration activity
510 happens in the buffer.")
511 (make-variable-buffer-local 'allout-widgets-last-decoration-timing)
513 ;;;_ > define-minor-mode allout-widgets-mode (arg)
515 (define-minor-mode allout-widgets-mode
516 "Toggle Allout Widgets mode.
517 With a prefix argument ARG, enable Allout Widgets mode if ARG is
518 positive, and disable it otherwise. If called from Lisp, enable
519 the mode if ARG is omitted or nil.
521 Allout Widgets mode is an extension of Allout mode that provides
522 graphical decoration of outline structure. It is meant to
523 operate along with `allout-mode', via `allout-mode-hook'.
525 The graphics include:
527 - guide lines connecting item bullet-icons with those of their subitems.
529 - icons for item bullets, varying to indicate whether or not the item
530 has subitems, and if so, whether or not the item is expanded.
532 - cue area between the bullet-icon and the start of the body headline,
533 for item numbering, encryption indicator, and distinctive bullets.
535 The bullet-icon and guide line graphics provide keybindings and mouse
536 bindings for easy outline navigation and exposure control, extending
537 outline hot-spot navigation \(see `allout-mode')."
542 ;; define-minor-mode handles any provided argument according to emacs
543 ;; minor-mode conventions - '(elisp) Minor Mode Conventions' - and sets
544 ;; allout-widgets-mode accordingly *before* running the body code, so we
546 (if allout-widgets-mode
549 (allout-add-resumptions
550 ;; XXX user may need say in line-truncation/hscrolling - an option
551 ;; that abstracts mode.
552 ;; truncate text lines to keep guide lines intact:
554 ;; and enable autoscrolling to ease view of text
555 '(auto-hscroll-mode t)
556 '(line-move-ignore-fields t)
557 '(widget-push-button-prefix "")
558 '(widget-push-button-suffix "")
559 ;; allout-escaped-prefix-regexp depends on allout-regexp:
560 (list 'allout-escaped-prefix-regexp (concat "\\(\\\\\\)"
561 "\\(" allout-regexp "\\)")))
562 (allout-add-resumptions
563 (list 'allout-widgets-tally allout-widgets-tally)
564 (list 'allout-widgets-escapes-sanitization-regexp-pair
565 (list (concat "\\(\n\\|\\`\\)"
566 allout-escaped-prefix-regexp
568 ;; Include everything but the escape symbol.
572 (add-hook 'after-change-functions 'allout-widgets-after-change-handler
575 (allout-setup-text-properties)
576 (add-to-invisibility-spec '(allout-torso . t))
577 (add-to-invisibility-spec 'allout-escapes)
579 (if (current-local-map)
580 (set-keymap-parent allout-item-body-keymap (current-local-map)))
582 (add-hook 'allout-exposure-change-functions
583 'allout-widgets-exposure-change-recorder nil 'local)
584 (add-hook 'allout-structure-added-functions
585 'allout-widgets-additions-recorder nil 'local)
586 (add-hook 'allout-structure-deleted-functions
587 'allout-widgets-deletions-recorder nil 'local)
588 (add-hook 'allout-structure-shifted-functions
589 'allout-widgets-shifts-recorder nil 'local)
590 (add-hook 'allout-after-copy-or-kill-hook
591 'allout-widgets-after-copy-or-kill-function nil 'local)
592 (add-hook 'allout-post-undo-hook
593 'allout-widgets-after-undo-function nil 'local)
595 (add-hook 'before-change-functions 'allout-widgets-before-change-handler
597 (add-hook 'post-command-hook 'allout-widgets-post-command-business
599 (add-hook 'pre-command-hook 'allout-widgets-pre-command-business
602 ;; init the widgets tally for debugging:
603 (if (not allout-widgets-tally)
604 (setq allout-widgets-tally (make-hash-table
605 :test 'eq :weakness 'key)))
606 ;; add tally count display on minor-mode-alist just after
607 ;; allout-mode entry.
608 ;; (we use ternary condition form to keep condition simple for deletion.)
609 (let* ((mode-line-entry '(allout-widgets-mode-inhibit ""
610 (:eval (allout-widgets-tally-string))))
611 (associated (assoc (car mode-line-entry) minor-mode-alist))
612 ;; need location for it only if not already present:
613 (after (and (not associated)
614 (memq (assq 'allout-mode minor-mode-alist) minor-mode-alist))))
616 (rplacd after (cons mode-line-entry (cdr after)))))
617 (allout-widgets-prepopulate-buffer)
620 (let ((inhibit-read-only t)
621 (was-modified (buffer-modified-p)))
623 (allout-widgets-undecorate-region (point-min)(point-max))
624 (remove-from-invisibility-spec '(allout-torso . t))
625 (remove-from-invisibility-spec 'allout-escapes)
627 (remove-hook 'after-change-functions
628 'allout-widgets-after-change-handler 'local)
629 (remove-hook 'allout-exposure-change-functions
630 'allout-widgets-exposure-change-recorder 'local)
631 (remove-hook 'allout-structure-added-functions
632 'allout-widgets-additions-recorder 'local)
633 (remove-hook 'allout-structure-deleted-functions
634 'allout-widgets-deletions-recorder 'local)
635 (remove-hook 'allout-structure-shifted-functions
636 'allout-widgets-shifts-recorder 'local)
637 (remove-hook 'allout-after-copy-or-kill-hook
638 'allout-widgets-after-copy-or-kill-function 'local)
639 (remove-hook 'before-change-functions
640 'allout-widgets-before-change-handler 'local)
641 (remove-hook 'post-command-hook
642 'allout-widgets-post-command-business 'local)
643 (remove-hook 'pre-command-hook
644 'allout-widgets-pre-command-business 'local)
645 (assq-delete-all 'allout-widgets-mode-inhibit minor-mode-alist)
646 (set-buffer-modified-p was-modified))))
647 ;;;_ > allout-widgets-mode-off
648 (defun allout-widgets-mode-off ()
649 "Explicitly disable allout-widgets-mode."
650 (allout-widgets-mode -1))
651 ;;;_ > allout-widgets-mode-off
652 (defun allout-widgets-mode-on ()
653 "Explicitly disable allout-widgets-mode."
654 (allout-widgets-mode 1))
655 ;;;_ > allout-setup-text-properties ()
656 (defun allout-setup-text-properties ()
657 "Configure category and literal text properties."
659 ;; XXX body - before-change, entry, keymap
661 (setplist 'allout-guides-span-category nil)
662 (put 'allout-guides-span-category
663 'modification-hooks '(allout-graphics-modification-handler))
664 (put 'allout-guides-span-category 'local-map allout-item-icon-keymap)
665 (put 'allout-guides-span-category 'mouse-face widget-button-face)
666 (put 'allout-guides-span-category 'field 'structure)
667 ;; (put 'allout-guides-span-category 'face 'widget-button)
669 (setplist 'allout-icon-span-category
670 (allout-widgets-copy-list (symbol-plist
671 'allout-guides-span-category)))
672 (put 'allout-icon-span-category 'field 'structure)
674 ;; XXX for body text we're instead going to use the buffer-wide
675 ;; resources, like before/after-change-functions hooks and the
676 ;; buffer's key map. that way we won't have to do painful provisions
677 ;; to fixup things after edits, catch outlier interstitial
678 ;; characters, like newline and empty lines after hidden subitems,
680 (setplist 'allout-body-span-category nil)
681 (put 'allout-body-span-category 'evaporate t)
682 (put 'allout-body-span-category 'local-map allout-item-body-keymap)
683 ;;(put 'allout-body-span-category
684 ;; 'modification-hooks '(allout-body-modification-handler))
685 ;;(put 'allout-body-span-category 'field 'body)
687 (setplist 'allout-cue-span-category nil)
688 (put 'allout-cue-span-category 'evaporate t)
689 (put 'allout-cue-span-category
690 'modification-hooks '(allout-body-modification-handler))
691 (put 'allout-cue-span-category 'local-map allout-cue-span-keymap)
692 (put 'allout-cue-span-category 'mouse-face widget-button-face)
693 (put 'allout-cue-span-category 'pointer 'arrow)
694 (put 'allout-cue-span-category 'field 'structure)
696 (setplist 'allout-trailing-category nil)
697 (put 'allout-trailing-category 'evaporate t)
698 (put 'allout-trailing-category 'local-map allout-item-body-keymap)
700 (setplist 'allout-escapes-category nil)
701 (put 'allout-escapes-category 'invisible 'allout-escapes)
702 (put 'allout-escapes-category 'evaporate t))
703 ;;;_ > allout-widgets-prepopulate-buffer ()
704 (defun allout-widgets-prepopulate-buffer ()
705 "Step over the current buffers exposed items to do initial widgetizing."
706 (if (not allout-widgets-mode-inhibit)
708 (goto-char (point-min))
709 (while (allout-next-visible-heading 1)
710 (when (not (widget-at (point)))
711 (allout-get-or-create-item-widget))))))
712 ;;;_ . settings context
713 ;;;_ = allout-container-item
714 (defvar allout-container-item-widget nil
715 "A widget for the current outline's overarching container as an item.
717 The item has settings \(of the file/connection\) and maybe a body, but no
719 (make-variable-buffer-local 'allout-container-item-widget)
720 ;;;_ . Hooks and hook helpers
721 ;;;_ , major command-loop business:
722 ;;;_ > allout-widgets-pre-command-business (&optional recursing)
723 (defun allout-widgets-pre-command-business (&optional recursing)
724 "Handle actions pending before allout-mode activity."
726 ;;;_ > allout-widgets-post-command-business (&optional recursing)
727 (defun allout-widgets-post-command-business (&optional recursing)
728 "Handle actions pending after any allout-mode commands.
730 Optional RECURSING is for internal use, to limit recursion."
731 ;; - check changed text for nesting discontinuities and escape anything
732 ;; that's: (1) asterisks at bol or (2) excessively nested.
733 (condition-case failure
735 (when (and (boundp 'allout-mode) allout-mode)
737 (if allout-widgets-unset-inhibit-read-only
738 (setq inhibit-read-only nil
739 allout-widgets-unset-inhibit-read-only nil))
741 (when allout-widgets-reenable-before-change-handler
742 (add-hook 'before-change-functions
743 'allout-widgets-before-change-handler
745 (setq allout-widgets-reenable-before-change-handler nil))
747 (when (or allout-widgets-undo-exposure-record
748 allout-widgets-changes-record)
749 (let* ((debug-on-signal t)
751 ;; inhibit recording new undo records when processing
752 ;; effects of undo-exposure:
753 (debugger 'allout-widgets-hook-error-handler)
754 (adjusting-message " Adjusting widgets...")
755 (replaced-message (allout-widgets-adjusting-message
757 (start-time (current-time)))
759 (if allout-widgets-undo-exposure-record
760 ;; inhibit undo recording iff undoing exposure stuff.
761 ;; XXX we might need to inhibit per respective
762 ;; change-record, rather than assuming that some undo
763 ;; activity during a command is all undo activity.
764 (let ((buffer-undo-list t))
765 (allout-widgets-exposure-undo-processor)
766 (allout-widgets-changes-dispatcher))
767 (allout-widgets-exposure-undo-processor)
768 (allout-widgets-changes-dispatcher))
770 (if allout-widgets-time-decoration-activity
771 (setq allout-widgets-last-decoration-timing
772 (list (allout-elapsed-time-seconds (current-time)
774 allout-widgets-changes-record)))
776 (setq allout-widgets-changes-record nil)
779 (if (stringp replaced-message)
780 (message replaced-message)
783 ;; alas, decorated intermediate matches are not easily undecorated
784 ;; when they're automatically rehidden by isearch, so we're
785 ;; dropping this nicety.
786 ;; ;; Detect undecorated items, eg during isearch into previously
787 ;; ;; unexposed topics, and decorate "economically". Some
788 ;; ;; undecorated stuff is often exposed, to reduce lag, but the
789 ;; ;; item containing the cursor is decorated. We constrain
790 ;; ;; recursion to avoid being trapped by unexpectedly undecoratable
792 ;; (when (and (not recursing)
793 ;; (not (allout-current-decorated-p))
794 ;; (or (not (equal (allout-depth) 0))
795 ;; (not allout-container-item-widget)))
796 ;; (let ((buffer-undo-list t))
797 ;; (allout-widgets-exposure-change-recorder
798 ;; allout-recent-prefix-beginning allout-recent-prefix-end nil)
799 ;; (allout-widgets-post-command-business 'recursing)))
801 ;; Detect and rectify fouled outline structure - decorated item
802 ;; not at beginning of line.
803 (let ((this-widget (or (widget-at (point))
804 ;; XXX we really should be checking across
805 ;; edited span, not just point and point+1
806 (and (not (eq (point) (point-max)))
807 (widget-at (1+ (point))))))
811 (goto-char (widget-get this-widget :from))
816 (concat "Misplaced item won't be recognizable "
817 " as part of outline - rectify? "))
820 (if (allout-hidden-p (max (1- (point)) 1))
822 (goto-char (max (1- (point)) 1))
823 (allout-show-to-offshoot)))
824 (allout-widgets-undecorate-item this-widget))
825 ;; expose any hidden intervening items, so resulting
826 ;; position is clear:
827 (setq inserted-at (point))
828 (allout-unprotected (insert-before-markers "\n"))
830 ;; ensure the inserted newline is visible:
831 (allout-flag-region inserted-at (1+ inserted-at) nil)
832 (allout-widgets-post-command-business 'recursing)
833 (message (concat "outline structure corrected - item"
834 " moved to beginning of new line"))
835 ;; preserve cursor position in some cases:
837 (> (point) inserted-at))
838 (forward-char -1)))))))
841 ;; zero work list so we don't get stuck futilely retrying.
842 ;; error recording done by allout-widgets-hook-error-handler.
843 (setq allout-widgets-changes-record nil))))
844 ;;;_ , major change handlers:
845 ;;;_ > allout-widgets-before-change-handler
846 (defun allout-widgets-before-change-handler (beg end)
847 "Business to be done before changes in a widgetized allout outline."
848 ;; protect against unruly edits to structure:
850 (undo-in-progress (when (eq (get-text-property beg 'category)
851 'allout-icon-span-category)
854 (let* ((item-widget (allout-get-item-widget)))
856 (allout-widgets-exposure-undo-recorder
858 (inhibit-read-only t)
859 ((not (and (boundp 'allout-mode) allout-mode)) t)
860 ((equal this-command 'quoted-insert) t)
861 ((not (text-property-any beg (if (equal end beg)
862 (min (1+ beg) (point-max))
866 ((yes-or-no-p "Unruly edit of outline structure - allow? ")
867 (setq allout-widgets-unset-inhibit-read-only (not inhibit-read-only)
868 inhibit-read-only t))
870 ;; tell the allout-widgets-post-command-business to reestablish the hook:
871 (setq allout-widgets-reenable-before-change-handler t)
872 ;; and raise an error to prevent the edit (and disable the hook):
874 (substitute-command-keys allout-structure-unruly-deletion-message)))))
875 ;;;_ > allout-widgets-after-change-handler
876 (defun allout-widgets-after-change-handler (beg end prelength)
877 "Reconcile what needs to be reconciled for allout widgets after edits."
879 ;;;_ > allout-current-decorated-p ()
880 (defun allout-current-decorated-p ()
881 "True if the current item is not decorated"
883 (if (allout-back-to-current-heading)
884 (if (> allout-recent-depth 0)
885 (and (allout-get-item-widget) t)
886 allout-container-item-widget))))
888 ;;;_ > allout-widgets-hook-error-handler
889 (defun allout-widgets-hook-error-handler (mode args)
890 "Process errors which occurred in the course of command hook operation.
892 We store a backtrace of the error information in the variable,
893 `allout-widgets-last-hook-error', unset the error handlers, and
894 reraise the error, so that processing continues to the
895 encompassing condition-case."
896 ;; first deconstruct special error environment so errors here propagate
897 ;; to encompassing condition-case:
898 (setq debugger 'debug
901 (let* ((bt (with-output-to-string (backtrace)))
902 (this "allout-widgets-hook-error-handler")
904 (format "allout-widgets-last-hook-error stored, %s/%s %s %s"
906 (format-time-string "%e-%b-%Y %r" (current-time)))))
907 ;; post to *Messages* then immediately replace with more compact notice:
908 (message "%s" (setq allout-widgets-last-hook-error
909 (format "%s:\n%s" header bt)))
910 (message header) (sit-for allout-widgets-hook-error-post-time)
911 ;; reraise the error, or one concerning this function if unexpected:
912 (if (equal mode 'error)
914 (error "%s: unexpected mode, %s %s" this mode args))))
915 ;;;_ > allout-widgets-changes-exceed-threshold-p ()
916 (defun allout-widgets-adjusting-message (message)
917 "Post MESSAGE when pending are likely to make a big enough delay.
919 If posting of the MESSAGE is warranted and there already is a
920 `current-message' in the minibuffer, the MESSAGE is appended to
921 the current one, and the previously pending `current-message' is
922 returned for later posting on completion.
924 If posting of the MESSAGE is warranted, but no `current-message'
925 is pending, then t is returned to indicate that case.
927 If posting of the MESSAGE is not warranted, then nil is returned.
929 See `allout-widgets-adjust-message-length-threshold',
930 `allout-widgets-adjust-message-size-threshold' for message
931 posting threshold criteria."
932 (if (or (> (length allout-widgets-changes-record)
933 allout-widgets-adjust-message-length-threshold)
934 ;; for size, use distance from start of first to end of last:
935 (let ((min (point-max))
938 (mapc (function (lambda (entry)
939 (if (eq :undone-exposure (car entry))
941 (setq first (cadr entry)
942 second (caddr entry))
943 (if (< (min first second) min)
944 (setq min (min first second)))
945 (if (> (max first second) max)
946 (setq max (max first second))))))
947 allout-widgets-changes-record)
948 (> (- max min) allout-widgets-adjust-message-size-threshold)))
949 (let ((prior (current-message)))
950 (message (if prior (concat prior " - " message) message))
952 ;;;_ > allout-widgets-changes-dispatcher ()
953 (defun allout-widgets-changes-dispatcher ()
954 "Dispatch CHANGES-RECORD items to respective widgets change processors."
956 (if (not allout-widgets-mode-inhibit)
957 (let* ((changes-record allout-widgets-changes-record)
958 (changes-pending (and changes-record t))
965 (when changes-pending
966 (while changes-record
967 (setq entry (pop changes-record))
969 (:exposed (push entry exposures))
970 (:added (push entry additions))
971 (:deleted (push entry deletions))
972 (:shifted (push entry shifts))))
975 (allout-widgets-exposure-change-processor exposures))
977 (allout-widgets-additions-processor additions))
979 (allout-widgets-deletions-processor deletions))
981 (allout-widgets-shifts-processor shifts))))
982 (when (not (equal allout-widgets-mode-inhibit 'undecorated))
983 (allout-widgets-undecorate-region (point-min)(point-max))
984 (setq allout-widgets-mode-inhibit 'undecorated))))
985 ;;;_ > allout-widgets-exposure-change-recorder (from to flag)
986 (defun allout-widgets-exposure-change-recorder (from to flag)
987 "Record allout exposure changes for tracking during post-command processing.
989 Records changes in `allout-widgets-changes-record'."
990 (push (list :exposed from to flag) allout-widgets-changes-record))
991 ;;;_ > allout-widgets-exposure-change-processor (changes)
992 (defun allout-widgets-exposure-change-processor (changes)
993 "Widgetize and adjust item widgets tracking allout outline exposure changes.
995 Generally invoked via `allout-exposure-change-functions'."
997 (let ((changes (sort changes (function (lambda (this next)
998 (< (cadr this) (cadr next))))))
999 ;; have to distinguish between concealing and exposing so that, eg,
1000 ;; `allout-expose-topic's mix is handled properly.
1006 (dolist (change changes)
1008 (from (cadr change))
1011 (flag (cadddr change))
1014 ;; swap from and to:
1015 (if (< to from) (setq bucket to
1019 ;; have we already handled exposure changes in this region?
1020 (setq handling (if flag 'handled-conceal 'handled-expose)
1021 got (allout-range-overlaps from to (symbol-value handling))
1023 (set handling (cadr got))
1032 (allout-widgets-undecorate-region from to)
1033 (allout-beginning-of-current-line)
1034 (let ((widget (allout-get-item-widget)))
1036 (allout-get-or-create-item-widget)
1037 (widget-apply widget :redecorate))))
1041 (while (< (point) to)
1042 (allout-beginning-of-current-line)
1043 (setq parent (allout-get-item-widget))
1045 (setq parent (allout-get-or-create-item-widget))
1046 (widget-apply parent :redecorate))
1047 (allout-next-visible-heading 1)
1048 (if (widget-get parent :has-subitems)
1049 (allout-redecorate-visible-subtree parent))
1051 ;; subtree may be well beyond to - incorporate in ranges:
1052 (setq handled-expose
1053 (allout-range-overlaps from (point) handled-expose)
1054 covered (car handled-expose)
1055 handled-expose (cadr handled-expose)))
1056 (allout-next-visible-heading 1))))))))))
1058 ;;;_ > allout-widgets-additions-recorder (from to)
1059 (defun allout-widgets-additions-recorder (from to)
1060 "Record allout item additions for tracking during post-command processing.
1062 Intended for use on `allout-structure-added-functions'.
1064 FROM point at the start of the first new item and TO is point at the start
1067 Records changes in `allout-widgets-changes-record'."
1068 (push (list :added from to) allout-widgets-changes-record))
1069 ;;;_ > allout-widgets-additions-processor (changes)
1070 (defun allout-widgets-additions-processor (changes)
1071 "Widgetize and adjust items tracking allout outline structure additions.
1073 Dispatched by `allout-widgets-post-command-business' in response to
1074 :added entries recorded by `allout-widgets-additions-recorder'."
1078 (dolist (change changes)
1079 (let ((from (cadr change))
1081 (to (caddr change)))
1082 (if (< to from) (setq bucket to to from from bucket))
1083 ;; have we already handled exposure changes in this region?
1084 (setq handled (allout-range-overlaps from to handled)
1085 covered (car handled)
1086 handled (cadr handled))
1089 ;; Prior sibling and parent can both be affected.
1091 (allout-redecorate-visible-subtree
1092 (allout-get-or-create-item-widget 'redecorate)))
1093 (if (< (point) from)
1095 (while (and (< (point) to) (not (eobp)))
1096 (allout-beginning-of-current-line)
1097 (allout-redecorate-visible-subtree
1098 (allout-get-or-create-item-widget))
1099 (allout-next-visible-heading 1))
1101 ;; subtree may be well beyond to - incorporate in ranges:
1102 (setq handled (allout-range-overlaps from (point) handled)
1103 covered (car handled)
1104 handled (cadr handled)))))))))
1106 ;;;_ > allout-widgets-deletions-recorder (depth from)
1107 (defun allout-widgets-deletions-recorder (depth from)
1108 "Record allout item deletions for tracking during post-command processing.
1109 Intended for use on `allout-structure-deleted-functions'.
1111 DEPTH is the depth of the deleted subtree, and FROM is the point from which
1112 the subtree was deleted.
1114 Records changes in `allout-widgets-changes-record'."
1115 (push (list :deleted depth from) allout-widgets-changes-record))
1116 ;;;_ > allout-widgets-deletions-processor (changes)
1117 (defun allout-widgets-deletions-processor (changes)
1118 "Adjust items tracking allout outline structure deletions.
1120 Dispatched by `allout-widgets-post-command-business' in response to
1121 :deleted entries recorded by `allout-widgets-deletions-recorder'."
1123 (dolist (change changes)
1124 (let ((depth (cadr change))
1125 (from (caddr change)))
1127 (when (allout-previous-visible-heading 1)
1129 (allout-ascend-to-depth (1- depth)))
1130 (allout-redecorate-visible-subtree
1131 (allout-get-or-create-item-widget 'redecorate)))))))
1133 ;;;_ > allout-widgets-shifts-recorder (shifted-amount at)
1134 (defun allout-widgets-shifts-recorder (shifted-amount at)
1135 "Record outline subtree shifts for tracking during post-command processing.
1136 Intended for use on `allout-structure-shifted-functions'.
1138 SHIFTED-AMOUNT is the depth change and AT is the point at the start of the
1139 subtree that's been shifted.
1141 Records changes in `allout-widgets-changes-record'."
1142 (push (list :shifted shifted-amount at) allout-widgets-changes-record))
1143 ;;;_ > allout-widgets-shifts-processor (changes)
1144 (defun allout-widgets-shifts-processor (changes)
1145 "Widgetize and adjust items tracking allout outline structure additions.
1147 Dispatched by `allout-widgets-post-command-business' in response to
1148 :shifted entries recorded by `allout-widgets-shifts-recorder'."
1150 (dolist (change changes)
1151 (goto-char (caddr change))
1153 (allout-redecorate-visible-subtree))))
1154 ;;;_ > allout-widgets-after-copy-or-kill-function ()
1155 (defun allout-widgets-after-copy-or-kill-function ()
1156 "Do allout-widgets processing of text just placed in the kill ring.
1158 Intended for use on allout-after-copy-or-kill-hook."
1160 (setcar kill-ring (allout-widgets-undecorate-text (car kill-ring)))))
1161 ;;;_ > allout-widgets-after-undo-function ()
1162 (defun allout-widgets-after-undo-function ()
1163 "Do allout-widgets processing of text after an undo.
1165 Intended for use on allout-post-undo-hook."
1167 (if (allout-goto-prefix)
1168 (allout-redecorate-item (allout-get-or-create-item-widget)))))
1170 ;;;_ > allout-widgets-exposure-undo-recorder (widget from-state)
1171 (defun allout-widgets-exposure-undo-recorder (widget)
1172 "Record outline exposure undo for tracking during post-command processing.
1174 Intended for use by `allout-graphics-modification-handler'.
1176 WIDGET is the widget being changed.
1178 Records changes in `allout-widgets-changes-record'."
1179 ;; disregard the events if we're currently processing them.
1180 (if (not allout-undo-exposure-in-progress)
1181 (push widget allout-widgets-undo-exposure-record)))
1182 ;;;_ > allout-widgets-exposure-undo-processor ()
1183 (defun allout-widgets-exposure-undo-processor ()
1184 "Adjust items tracking undo of allout outline structure exposure.
1186 Dispatched by `allout-widgets-post-command-business' in response to
1187 :undone-exposure entries recorded by `allout-widgets-exposure-undo-recorder'."
1188 (let* ((allout-undo-exposure-in-progress t)
1189 ;; inhibit undo recording while twiddling exposure to track undo:
1190 (widgets allout-widgets-undo-exposure-record)
1191 widget widget-start-marker widget-end-marker
1192 from-state icon-start-point to-state
1194 (setq allout-widgets-undo-exposure-record nil)
1196 (dolist (widget widgets)
1197 (setq widget-start-marker (widget-get widget :from)
1198 widget-end-marker (widget-get widget :to)
1199 from-state (widget-get widget :icon-state)
1200 icon-start-point (widget-apply widget :actual-position
1202 to-state (get-text-property icon-start-point
1204 (setq handled (allout-range-overlaps widget-start-marker
1207 covered (car handled)
1208 handled (cadr handled))
1210 (goto-char (widget-get widget :from))
1211 (when (not (allout-hidden-p))
1212 ;; adjust actual exposure to that of to-state viz from-state
1213 (cond ((and (eq to-state 'closed) (eq from-state 'opened))
1214 (allout-hide-current-subtree)
1215 (allout-decorate-item-and-context widget))
1216 ((and (eq to-state 'opened) (eq from-state 'closed))
1219 (expose-to (allout-chart-exposure-contour-by-icon))
1220 (goto-char expose-to)
1221 (allout-show-to-offshoot)))))))))))
1222 ;;;_ > allout-chart-exposure-contour-by-icon (&optional from-depth)
1223 (defun allout-chart-exposure-contour-by-icon (&optional from-depth)
1224 "Return points of subtree items to which exposure should be extended.
1226 The qualifying items are ones with a widget icon that is in the closed or
1227 empty state, or items with undecorated subitems.
1229 The resulting list of points is in reverse order.
1231 Optional FROM-DEPTH is for internal use."
1232 ;; During internal recursion, we return a pair: (at-end . result)
1233 ;; Otherwise we just return the result.
1234 (let ((from-depth from-depth)
1240 (setq level-depth (allout-depth))
1241 ;; at containing item:
1242 (setq start-point (point))
1243 (setq from-depth (allout-depth))
1244 (setq at-end (not (allout-next-heading))
1245 level-depth allout-recent-depth))
1247 ;; traverse the level, recursing on deeper levels:
1248 (while (and (not at-end)
1249 (> allout-recent-depth from-depth)
1250 (setq this-widget (allout-get-item-widget)))
1251 (if (< level-depth allout-recent-depth)
1254 (setq subgot (allout-chart-exposure-contour-by-icon level-depth)
1256 subgot (cdr subgot))
1257 (if subgot (setq got (append subgot got))))
1258 ;; progress at this level:
1259 (when (memq (widget-get this-widget :icon-state) '(closed empty))
1261 (allout-end-of-subtree))
1262 (setq at-end (not (allout-next-heading)))))
1264 ;; tailor result depending on whether or not we're a recursion:
1265 (if (not start-point)
1267 (goto-char start-point)
1269 ;;;_ > allout-range-overlaps (from to ranges)
1270 (defun allout-range-overlaps (from to ranges)
1271 "Return a pair indicating overlap of FROM and TO subtree range in RANGES.
1273 First element of result indicates whether candidate range FROM, TO
1274 overlapped any of the existing ranges.
1276 Second element of result is a new version of RANGES incorporating the
1277 candidate range with overlaps consolidated.
1279 FROM and TO must be in increasing order, as must be the pairs in RANGES."
1280 ;; to append to the end: (rplacd next-to-last-cdr (list 'f))
1283 ;; the start of the range that includes the candidate from:
1285 ;; the end of the range that includes the candidate to:
1287 ;; the candidates were inserted:
1289 (while (and ranges (not done))
1290 (setq entry (car ranges)
1291 ranges (cdr ranges))
1296 ;; some entry included the candidate from.
1297 (cond ((> (car entry) to)
1298 ;; current entry exceeds end of candidate range - done.
1299 (push (list included-from to) new-ranges)
1300 (push entry new-ranges)
1301 (setq included-to to
1303 ((>= (cadr entry) to)
1304 ;; current entry includes end of candidate range - done.
1305 (push (list included-from (cadr entry)) new-ranges)
1306 (setq included-to (cadr entry)
1308 ;; current entry contained in candidate range - ditch, continue:
1312 ;; current entry start exceeds candidate end - done, placed as new entry
1313 (push (list from to) new-ranges)
1314 (push entry new-ranges)
1315 (setq included-to to
1318 ((>= (car entry) from)
1319 ;; current entry start is above candidate start, but not above
1320 ;; candidate end (by prior case).
1321 (setq included-from from)
1322 ;; now we have to check on whether this entry contains to, or continue:
1323 (when (>= (cadr entry) to)
1324 ;; current entry contains only candidate end - done:
1325 (push (list included-from (cadr entry)) new-ranges)
1326 (setq included-to (cadr entry)
1328 ;; otherwise, we will continue to look for placement of candidate end.
1331 ((>= (cadr entry) to)
1332 ;; current entry properly contains candidate range.
1333 (push entry new-ranges)
1334 (setq included-from (car entry)
1335 included-to (cadr entry)
1338 ((>= (cadr entry) from)
1339 ;; current entry contains start of candidate range.
1340 (setq included-from (car entry)))
1343 ;; current entry is below the candidate range.
1344 (push entry new-ranges))))
1346 (cond ((and included-from included-to)
1347 ;; candidates placed.
1349 ((not (or included-from included-to))
1350 ;; candidates found no place, must be at the end:
1351 (push (list from to) new-ranges))
1353 ;; candidate start placed but end not:
1354 (push (list included-from to) new-ranges))
1355 ;; might be included-to and not included-from, indicating new entry.
1357 (setq new-ranges (nreverse new-ranges))
1358 (if ranges (setq new-ranges (append new-ranges ranges)))
1359 (list (if included-from t) new-ranges)))
1360 ;;;_ > allout-test-range-overlaps ()
1361 (defun allout-test-range-overlaps ()
1362 "allout-range-overlaps unit tests."
1365 (try (lambda (from to)
1366 (setq got (allout-range-overlaps from to ranges))
1367 (setq ranges (cadr got))
1370 ;; (setq ranges nil)
1371 ;; ;; ~ .02 to .1 seconds for just repeated listing args instead of funcall
1372 ;; ;; ~ 13 seconds for doing repeated funcall
1373 ;; (message "time-trial: %s, resulting size %s"
1375 ;; '(let ((size 10000)
1377 ;; (dotimes (count size)
1378 ;; (setq doing (random size))
1379 ;; (funcall try doing (+ doing (random 5)))
1380 ;; ;;(list doing (+ doing (random 5)))
1387 (assert (equal (funcall try 3 5) '(nil ((3 5)))))
1388 ;; add range at end:
1389 (assert (equal (funcall try 10 12) '(nil ((3 5) (10 12)))))
1390 ;; add range at beginning:
1391 (assert (equal (funcall try 1 2) '(nil ((1 2) (3 5) (10 12)))))
1392 ;; insert range somewhere in the middle:
1393 (assert (equal (funcall try 7 9) '(nil ((1 2) (3 5) (7 9) (10 12)))))
1394 ;; consolidate some:
1395 (assert (equal (funcall try 5 8) '(t ((1 2) (3 9) (10 12)))))
1397 (assert (equal (funcall try 15 17) '(nil ((1 2) (3 9) (10 12) (15 17)))))
1399 (assert (equal (funcall try 20 22)
1400 '(nil ((1 2) (3 9) (10 12) (15 17) (20 22)))))
1402 (assert (equal (funcall try 4 11) '(t ((1 2) (3 12) (15 17) (20 22)))))
1404 (assert (equal (funcall try 2 25) '(t ((1 25)))))
1408 (assert (equal (funcall try 20 25) '(nil ((20 25)))))
1409 (assert (equal (funcall try 30 35) '(nil ((20 25) (30 35)))))
1410 (assert (equal (funcall try 26 28) '(nil ((20 25) (26 28) (30 35)))))
1411 (assert (equal (funcall try 15 20) '(t ((15 25) (26 28) (30 35)))))
1412 (assert (equal (funcall try 10 30) '(t ((10 35)))))
1413 (assert (equal (funcall try 5 6) '(nil ((5 6) (10 35)))))
1414 (assert (equal (funcall try 2 100) '(t ((2 100)))))
1418 ;;;_ > allout-widgetize-buffer (&optional doing)
1419 (defun allout-widgetize-buffer (&optional doing)
1420 "EXAMPLE FUNCTION. Widgetize items in buffer using allout-chart-subtree.
1422 We economize by just focusing on the first of local-maximum depth siblings.
1424 Optional DOING is for internal use - a chart of the current level, for
1425 recursive operation."
1431 (goto-char (point-min))
1432 ;; Construct the chart by scanning the siblings:
1433 (dolist (top-level-sibling (allout-chart-siblings))
1434 (goto-char top-level-sibling)
1435 (let ((subchart (allout-chart-subtree)))
1437 (allout-widgetize-buffer subchart)))))
1439 ;; save-excursion was done on recursion entry, not necessary here.
1440 (let (have-sublists)
1441 (dolist (sibling doing)
1442 (when (listp sibling)
1443 (setq have-sublists t)
1444 (allout-widgetize-buffer sibling)))
1445 (when (and (not have-sublists) (not (widget-at (car doing))))
1446 (goto-char (car doing))
1447 (allout-get-or-create-item-widget)))))
1449 ;;;_ : Item widget and constructors
1451 ;;;_ $ allout-item-widget
1452 (define-widget 'allout-item-widget 'default
1453 "A widget presenting an allout outline item."
1456 ;; widget-field-at respects this to get item if 'field is unused.
1457 ;; we don't use field to avoid collision with end-of-line, etc, on which
1464 ;; tailor the widget for a specific item
1465 :create 'allout-decorate-item-and-context
1466 :value-delete 'allout-widgets-undecorate-item
1467 ;; Not Yet Converted (from original, tree-widget stab)
1468 :expander 'allout-tree-event-dispatcher ; get children when nil :args
1469 :expander-p 'identity ; always engage the :expander
1470 :action 'allout-tree-widget-action
1471 ;; :notify "when item changes"
1473 ;; force decoration of item but not context, unless already done this tick:
1474 :redecorate 'allout-redecorate-item
1475 :last-decorated-tick nil
1476 ;; recognize the actual situation of the item's text:
1477 :parse-item 'allout-parse-item-at-point
1478 ;; decorate the entirety of the item, sans offspring:
1479 :decorate-item-span 'allout-decorate-item-span
1480 ;; decorate the various item elements:
1481 :decorate-guides 'allout-decorate-item-guides
1482 :decorate-icon 'allout-decorate-item-icon
1483 :decorate-cue 'allout-decorate-item-cue
1484 :decorate-body 'allout-decorate-item-body
1485 :actual-position 'allout-item-actual-position
1487 ;; Layout parameters:
1488 :is-container nil ; is this actually the encompassing file/connection?
1490 :from nil ; item beginning - marker
1491 :to nil ; item end - marker
1492 :span-overlay nil ; overlay by which actual position is determined
1494 ;; also serves as guide-end:
1497 :distinctive-start nil
1498 ;; also serves as cue-start:
1499 :distinctive-end nil
1500 ;; also serves as cue-end:
1505 :was-has-subitems 'init
1511 :does-encrypt nil ; pending encryption when :is-encrypted false.
1514 ;; the actual location of the item text:
1515 :location 'allout-item-location
1517 :button-keymap allout-item-icon-keymap ; XEmacs
1518 :keymap allout-item-icon-keymap ; Emacs
1528 :body-brevity-p 'allout-body-brevity-p
1530 ;; :guide-column-flags indicate (in reverse order) whether or not the
1531 ;; item's ancestor at the depth corresponding to the column has a
1532 ;; subsequent sibling - ie, whether or not the corresponding column needs
1533 ;; a descender line to connect that ancestor with its sibling.
1534 :guide-column-flags nil
1535 :was-guide-column-flags 'init
1537 ;; ie, has subitems:
1538 :populous-p 'allout-item-populous-p
1539 :help-echo 'allout-tree-widget-help-echo
1541 ;;;_ > allout-new-item-widget ()
1542 (defsubst allout-new-item-widget ()
1543 "create a new item widget, not yet situated anywhere."
1544 (if allout-widgets-maintain-tally
1545 ;; all the extra overhead is incurred only when doing the
1546 ;; maintenance, except the condition, which can't be avoided.
1547 (let ((widget (widget-convert 'allout-item-widget)))
1548 (puthash widget nil allout-widgets-tally)
1550 (widget-convert 'allout-item-widget)))
1551 ;;;_ : Item decoration
1552 ;;;_ > allout-decorate-item-and-context (item-widget &optional redecorate
1553 ;;; blank-container parent)
1554 (defun allout-decorate-item-and-context (item-widget &optional redecorate
1555 blank-container parent)
1556 "Create or adjust widget decorations for ITEM-WIDGET and neighbors at point.
1558 The neighbors include its siblings and parent.
1560 ITEM-WIDGET can be a created or converted allout-item-widget.
1562 If you're only trying to get or create a widget for an item, use
1563 `allout-get-or-create-item-widget'. If you have the item-widget, applying
1564 :redecorate will do the right thing.
1566 Optional BLANK-CONTAINER is for internal use. It is used to fabricate a
1567 container widget for an empty-bodied container, in the course of decorating
1568 a proper \(non-container\) item which starts at the beginning of the file.
1570 Optional REDECORATE causes redecoration of the item-widget and
1571 its siblings, even if already decorated in this cycle of the command loop.
1573 Optional PARENT, when provided, bypasses some navigation and computation
1574 necessary to obtain the parent of the items being processed.
1576 We return the item-widget corresponding to the item at point."
1578 (when (or redecorate
1579 (not (equal (widget-get item-widget :last-decorated-tick)
1580 allout-command-counter)))
1581 (let* ((allout-inhibit-body-modification-hook t)
1582 (was-modified (buffer-modified-p))
1585 (is-container (or blank-container
1586 (not (setq prefix-start (allout-goto-prefix)))
1587 (< was-point prefix-start)))
1588 ;; steady-point (set in two steps) is reliable across parent
1590 (steady-point (progn (if is-container (goto-char 1))
1592 (steady-point (progn (set-marker-insertion-type steady-point t)
1594 (parent (and (not is-container)
1595 (allout-get-or-create-parent-widget)))
1596 parent-flags parent-depth
1602 reverse-siblings-chart
1603 (buffer-undo-list t))
1605 ;; At this point the parent is decorated and parent-flags indicate
1606 ;; its guide lines. We will iterate over the siblings according to a
1607 ;; chart we create at the start, and going from last to first so we
1608 ;; don't have to worry about text displacement caused by widgetizing.
1611 (progn (widget-put item-widget :is-container t)
1612 (setq reverse-siblings-chart (list 1)))
1613 (goto-char (widget-apply parent :actual-position :from))
1614 (if (widget-get parent :is-container)
1615 ;; `allout-goto-prefix' will go to first non-container item:
1616 (allout-goto-prefix)
1617 (allout-next-heading))
1618 (setq depth (allout-recent-depth))
1619 (setq reverse-siblings-chart (list allout-recent-prefix-beginning))
1620 (while (allout-next-sibling)
1621 (push allout-recent-prefix-beginning reverse-siblings-chart)))
1623 (dolist (doing-at reverse-siblings-chart)
1624 (goto-char doing-at)
1625 (when allout-widgets-track-decoration
1628 (setq doing-item (if (= doing-at steady-point)
1630 (or (allout-get-item-widget)
1631 (allout-new-item-widget))))
1633 (when (or redecorate (not (equal (widget-get doing-item
1634 :last-decorated-tick)
1635 allout-command-counter)))
1636 (widget-apply doing-item :parse-item t blank-container)
1637 (widget-apply doing-item :decorate-item-span)
1639 (widget-apply doing-item :decorate-guides
1640 parent (and successor-sibling t))
1641 (widget-apply doing-item :decorate-icon)
1642 (widget-apply doing-item :decorate-cue)
1643 (widget-apply doing-item :decorate-body)
1645 (widget-put doing-item :last-decorated-tick allout-command-counter))
1647 (setq successor-sibling doing-at))
1649 (set-buffer-modified-p was-modified)
1650 (goto-char steady-point)
1651 ;; must null the marker or the buffer gets clogged with impedance:
1652 (set-marker steady-point nil)
1655 ;;;_ > allout-redecorate-item (item)
1656 (defun allout-redecorate-item (item-widget)
1657 "Resituate ITEM-WIDGET decorations, disregarding context.
1659 Use this to redecorate only the item, when you know that its
1660 situation with respect to siblings, parent, and offspring is
1661 unchanged from its last decoration. Use
1662 `allout-decorate-item-and-context' instead to reassess and adjust
1663 relevant context, when suitable."
1664 (if (not (equal (widget-get item-widget :last-decorated-tick)
1665 allout-command-counter))
1666 (let ((was-modified (buffer-modified-p))
1667 (buffer-undo-list t))
1668 (widget-apply item-widget :parse-item)
1669 (widget-apply item-widget :decorate-guides)
1670 (widget-apply item-widget :decorate-icon)
1671 (widget-apply item-widget :decorate-cue)
1672 (widget-apply item-widget :decorate-body)
1673 (set-buffer-modified-p was-modified))))
1674 ;;;_ > allout-redecorate-visible-subtree (&optional parent-widget
1676 (defun allout-redecorate-visible-subtree (&optional parent-widget depth chart)
1677 "Redecorate all visible items in subtree at point.
1679 Optional PARENT-WIDGET is for optimization, when the parent
1680 widget is already available.
1682 Optional DEPTH restricts the excursion depth of covered.
1684 Optional CHART is for internal recursion, to carry a chart of the
1687 Point is left at the last sibling in the visible subtree."
1688 ;; using a treatment that takes care of all the siblings on a level, we
1689 ;; only need apply it to the first sibling on the level, and we can
1690 ;; collect and pass the parent of the lower levels to recursive calls as
1692 (let ((parent-widget
1693 (if (and parent-widget (widget-apply parent-widget
1694 :actual-position :from))
1695 (progn (goto-char (widget-apply parent-widget
1696 :actual-position :from))
1698 (let ((got (allout-get-item-widget)))
1700 (allout-decorate-item-and-context got 'redecorate)
1701 (allout-get-or-create-item-widget 'redecorate)))))
1702 (pending-chart (or chart (allout-chart-subtree nil 'visible)))
1704 previous-sibling-point
1706 recent-sibling-point)
1707 (setq pending-chart (nreverse pending-chart))
1708 (dolist (sibling-point pending-chart)
1709 (cond ((integerp sibling-point)
1710 (when (not previous-sibling-point)
1711 (goto-char sibling-point)
1712 (if (setq item-widget (allout-get-item-widget nil))
1713 (allout-decorate-item-and-context item-widget 'redecorate
1715 (allout-get-or-create-item-widget)))
1716 (setq previous-sibling-point sibling-point
1717 recent-sibling-point sibling-point))
1718 ((listp sibling-point)
1721 (allout-redecorate-visible-subtree
1722 (if (not previous-sibling-point)
1723 ;; containment discontinuity - sigh
1725 (allout-get-or-create-item-widget 'redecorate))
1726 (if depth (1- depth))
1728 (if (and recent-sibling-point (< (point) recent-sibling-point))
1729 (goto-char recent-sibling-point))))
1730 ;;;_ > allout-parse-item-at-point (item-widget &optional at-beginning
1731 ;;; blank-container)
1732 (defun allout-parse-item-at-point (item-widget &optional at-beginning
1734 "Set widget ITEM-WIDGET layout parameters per item-at-point's actual layout.
1736 If optional AT-BEGINNING is t, then point is assumed to be at the start of
1739 If optional BLANK-CONTAINER is true, then the parameters of a container
1740 which has an empty body are set. \(Though the body is blank, the object
1741 may have subitems.\)"
1743 ;; Uncomment this sit-for to notice where decoration is happening:
1745 (let* ((depth (allout-depth))
1746 (depth (if blank-container 0 depth))
1747 (is-container (or blank-container (zerop depth)))
1749 (does-encrypt (and (not is-container)
1750 (allout-encrypted-type-prefix)))
1751 (is-encrypted (and does-encrypt (allout-encrypted-topic-p)))
1752 (icon-end allout-recent-prefix-end)
1753 (icon-start (1- icon-end))
1758 (contents-depth (1+ depth))
1760 (widget-put item-widget :depth depth)
1764 (widget-put item-widget :from (allout-set-boundary-marker
1766 (widget-get item-widget :from)))
1767 (widget-put item-widget :icon-end nil)
1768 (widget-put item-widget :icon-start nil)
1769 (setq body-start (widget-put item-widget :body-start 1)))
1773 (widget-put item-widget :from (allout-set-boundary-marker
1774 :from (if at-beginning
1776 allout-recent-prefix-beginning)
1777 (widget-get item-widget :from)))
1778 (widget-put item-widget :icon-start icon-start)
1779 (widget-put item-widget :icon-end icon-end)
1781 (widget-put item-widget :does-encrypt t)
1782 (widget-put item-widget :is-encrypted is-encrypted))
1785 (setq body-start icon-end)
1786 (widget-put item-widget :bullet (setq bullet (allout-get-bullet)))
1787 (if (equal (char-after body-start) ? )
1788 (setq body-start (1+ body-start)))
1789 (widget-put item-widget :body-start body-start)
1792 ;; Both container and regular items:
1794 ;; :body-end (doesn't include a trailing blank line, if any) -
1795 (widget-put item-widget :body-end (setq body-end
1798 (allout-end-of-entry))))
1800 (widget-put item-widget :to (allout-set-boundary-marker
1801 :to (if blank-container
1803 (or (allout-pre-next-prefix)
1804 (goto-char (point-max))))
1805 (widget-get item-widget :to)))
1806 (widget-put item-widget :has-subitems
1809 ;; has a subsequent item:
1810 (not (= body-end (point-max)))
1811 ;; subsequent item is deeper:
1812 (< depth (setq contents-depth (allout-recent-depth))))))
1813 ;; note :expanded - true if widget item's content is currently visible?
1814 (widget-put item-widget :expanded
1816 ;; subsequent item is or isn't visible:
1818 (goto-char allout-recent-prefix-beginning)
1819 (not (allout-hidden-p)))))))
1820 ;;;_ > allout-set-boundary-marker (boundary position &optional current-marker)
1821 (defun allout-set-boundary-marker (boundary position &optional current-marker)
1822 "Set or create item widget BOUNDARY type marker at POSITION.
1824 Optional CURRENT-MARKER is the marker currently being used for
1825 the boundary, if any.
1827 BOUNDARY type is either :from or :to, determining the marker insertion type."
1828 (if (not position) (setq position (point)))
1830 (set-marker current-marker position)
1831 (let ((marker (make-marker)))
1832 ;; XXX dang - would like for :from boundary to advance after inserted
1833 ;; text, but that would omit new header prefixes when allout
1834 ;; relevels, etc. this competes with ad-hoc edits, which would
1835 ;; better be omitted
1836 (set-marker-insertion-type marker nil)
1837 (set-marker marker position))))
1838 ;;;_ > allout-decorate-item-span (item-widget)
1839 (defun allout-decorate-item-span (item-widget)
1840 "Equip the item with a span, as an entirety.
1842 This span is implemented so it can be used to detect displacement
1843 of the widget in absolute terms, and provides an offset bias for
1844 the various element spans."
1846 (if (and (widget-get item-widget :is-container)
1847 ;; the only case where the span could be empty.
1848 (eq (widget-get item-widget :from)
1849 (widget-get item-widget :to)))
1851 (allout-item-span item-widget
1852 (widget-get item-widget :from)
1853 (widget-get item-widget :to))))
1854 ;;;_ > allout-decorate-item-guides (item-widget
1855 ;;; &optional parent-widget has-successor)
1856 (defun allout-decorate-item-guides (item-widget
1857 &optional parent-widget has-successor)
1858 "Add ITEM-WIDGET guide icon-prefix descender and connector text properties.
1860 Optional arguments provide context for deriving the guides. In
1861 their absence, the current guide column flags are used.
1863 Optional PARENT-WIDGET is the widget for the item's parent item.
1865 Optional HAS-SUCCESSOR is true iff the item is followed by a sibling.
1867 We also hide the header-prefix string.
1869 Guides are established according to the item-widget's :guide-column-flags,
1870 when different than :was-guide-column-flags. Changing that property and
1871 reapplying this method will rectify the glyphs."
1873 (when (not (widget-get item-widget :is-container))
1874 (let* ((depth (widget-get item-widget :depth))
1875 (parent-depth (and parent-widget
1876 (widget-get parent-widget :depth)))
1877 (parent-flags (and parent-widget
1878 (widget-get parent-widget :guide-column-flags)))
1879 (parent-flags-depth (length parent-flags))
1880 (extender-length (- depth (+ parent-flags-depth 2)))
1881 (flags (or (and (> depth 1)
1883 (widget-put item-widget :guide-column-flags
1884 (append (list has-successor)
1885 (if (< 0 extender-length)
1886 (make-list extender-length
1889 (widget-get item-widget :guide-column-flags)))
1890 (was-flags (widget-get item-widget :was-guide-column-flags))
1891 (guides-start (widget-get item-widget :from))
1892 (guides-end (widget-get item-widget :icon-start))
1893 (position guides-start)
1894 (increment (length allout-header-prefix))
1897 extenders paint-extenders
1898 (inhibit-read-only t))
1900 (when (not (equal was-flags flags))
1902 (setq reverse-flags (reverse flags))
1903 (while reverse-flags
1905 (cond ((null (cdr reverse-flags))
1906 (if (car reverse-flags)
1909 ((eq (car reverse-flags) '-)
1910 ;; accumulate extenders tally, to be painted on next
1911 ;; non-extender flag, according to the flag type.
1912 (setq extenders (1+ (or extenders 0)))
1914 ((car reverse-flags)
1916 (t 'skip-descender)))
1918 (put-text-property position (setq position (+ position increment))
1919 'display (allout-fetch-icon-image guide-name))
1920 (if (> increment 1) (setq increment 1))
1922 ;; paint extenders after a connector, else leave spaces.
1923 (dotimes (i extenders)
1925 position (setq position (1+ position))
1926 'display (allout-fetch-icon-image
1927 (if (memq guide-name '(mid-connector end-connector))
1930 (setq extenders nil)))
1931 (setq reverse-flags (cdr reverse-flags)))
1932 (widget-put item-widget :was-guide-column-flags flags))
1934 (allout-item-element-span-is item-widget :guides-span
1935 guides-start guides-end))))
1936 ;;;_ > allout-decorate-item-icon (item-widget)
1937 (defun allout-decorate-item-icon (item-widget)
1938 "Add item icon glyph and distinctive bullet text properties to ITEM-WIDGET."
1940 (when (not (widget-get item-widget :is-container))
1941 (let* ((icon-start (widget-get item-widget :icon-start))
1942 (icon-end (widget-get item-widget :icon-end))
1943 (bullet (widget-get item-widget :bullet))
1945 (was-bullet (widget-get item-widget :was-bullet))
1946 (distinctive (allout-distinctive-bullet bullet))
1947 (distinctive-start (widget-get item-widget :distinctive-start))
1948 (distinctive-end (widget-get item-widget :distinctive-end))
1949 (does-encrypt (widget-get item-widget :does-encrypt))
1950 (is-encrypted (and does-encrypt (widget-get item-widget
1952 (expanded (widget-get item-widget :expanded))
1953 (has-subitems (widget-get item-widget :has-subitems))
1954 (inhibit-read-only t)
1957 (when (not (and (equal (widget-get item-widget :was-expanded) expanded)
1958 (equal (widget-get item-widget :was-has-subitems)
1960 (equal (widget-get item-widget :was-does-encrypt)
1962 (equal (widget-get item-widget :was-is-encrypted)
1966 (cond (does-encrypt (if is-encrypted
1968 'unlocked-encrypted))
1970 (has-subitems 'closed)
1972 (put-text-property icon-start (1+ icon-start)
1973 'display (allout-fetch-icon-image icon-state))
1974 (widget-put item-widget :was-expanded expanded)
1975 (widget-put item-widget :was-has-subitems has-subitems)
1976 (widget-put item-widget :was-does-encrypt does-encrypt)
1977 (widget-put item-widget :was-is-encrypted is-encrypted)
1978 ;; preserve as a widget property to track last known:
1979 (widget-put item-widget :icon-state icon-state)
1980 ;; preserve as a text property to track undo:
1981 (put-text-property icon-start icon-end :icon-state icon-state))
1982 (allout-item-element-span-is item-widget :icon-span
1983 icon-start icon-end)
1984 (when (not (string= was-bullet bullet))
1985 (cond ((not distinctive)
1986 ;; XXX we strip the prior properties without even checking if
1987 ;; the prior bullet was distinctive, because the widget
1988 ;; provisions to convey that info is disappearing, sigh.
1989 (remove-text-properties icon-end (1+ icon-end) '(display))
1990 (setq distinctive-start icon-end distinctive-end icon-end)
1991 (widget-put item-widget :distinctive-start distinctive-start)
1992 (widget-put item-widget :distinctive-end distinctive-end))
1994 ((not (string= bullet allout-numbered-bullet))
1995 (setq distinctive-start icon-end distinctive-end (+ icon-end 1)))
1998 (setq distinctive-start icon-end distinctive-end (+ icon-end 1)))
2001 (goto-char icon-end)
2002 (looking-at "[0-9]+")
2003 (setq use-bullet (buffer-substring icon-end (match-end 0)))
2004 (setq distinctive-start icon-end
2005 distinctive-end (match-end 0))))
2006 (put-text-property distinctive-start distinctive-end 'display
2008 (widget-put item-widget :was-bullet bullet)
2009 (widget-put item-widget :distinctive-start distinctive-start)
2010 (widget-put item-widget :distinctive-end distinctive-end)))))
2011 ;;;_ > allout-decorate-item-cue (item-widget)
2012 (defun allout-decorate-item-cue (item-widget)
2013 "Incorporate space between bullet icon and body to the ITEM-WIDGET."
2014 ;; NOTE: most of the cue-area
2016 (when (not (widget-get item-widget :is-container))
2017 (let* ((cue-start (or (widget-get item-widget :distinctive-end)
2018 (widget-get item-widget :icon-end)))
2019 (body-start (widget-get item-widget :body-start))
2020 (expanded (widget-get item-widget :expanded))
2021 (has-subitems (widget-get item-widget :has-subitems))
2022 (inhibit-read-only t))
2024 (allout-item-element-span-is item-widget :cue-span cue-start body-start)
2025 (put-text-property (1- body-start) body-start 'rear-nonsticky t))))
2026 ;;;_ > allout-decorate-item-body (item-widget &optional force)
2027 (defun allout-decorate-item-body (item-widget &optional force)
2028 "Incorporate item body text as part the ITEM-WIDGET.
2030 Optional FORCE means force reassignment of the region property."
2032 (let* ((allout-inhibit-body-modification-hook t)
2033 (body-start (widget-get item-widget :body-start))
2034 (body-end (widget-get item-widget :body-end))
2035 (body-text-end body-end)
2036 (inhibit-read-only t))
2038 (allout-item-element-span-is item-widget :body-span
2039 body-start (min (1+ body-end) (point-max))
2041 ;;;_ > allout-item-actual-position (item-widget field)
2042 (defun allout-item-actual-position (item-widget field)
2043 "Return ITEM-WIDGET FIELD position taking item displacement into account."
2045 ;; The item's sub-element positions (:icon-end, :body-start, etc) are
2046 ;; accurate when the item is parsed, but some offsets from the start
2047 ;; drift with text added in the body.
2049 ;; Rather than reparse an item with every change (inefficient), or derive
2050 ;; every position from a distinct field marker/overlay (prohibitive as
2051 ;; the number of items grows), we use the displacement tracking of the
2052 ;; :span-overlay's markers, against the registered :from or :body-end
2053 ;; (depending on whether the requested field value is before or after the
2054 ;; item body), to bias the registered values.
2056 ;; This is not necessary/useful when the item is being decorated, because
2057 ;; that always must be preceded by a fresh item parse.
2059 (if (not (eq field :body-end))
2060 (widget-get item-widget :from)
2062 (let* ((span-overlay (widget-get item-widget :span-overlay))
2063 (body-end-position (widget-get item-widget :body-end))
2064 (ref-marker-position (and span-overlay
2065 (overlay-end span-overlay)))
2066 (offset (and body-end-position span-overlay
2067 (- (or ref-marker-position 0)
2068 body-end-position))))
2069 (+ (widget-get item-widget field) (or offset 0)))))
2070 ;;;_ : Item undecoration
2071 ;;;_ > allout-widgets-undecorate-region (start end)
2072 (defun allout-widgets-undecorate-region (start end)
2073 "Eliminate widgets and decorations for all items in region from START to END."
2078 (while (< (setq next (next-single-char-property-change next
2084 (when (setq widget (allout-get-item-widget))
2085 ;; if the next-property/overly progression got us to a widget:
2086 (allout-widgets-undecorate-item widget t))))))
2087 ;;;_ > allout-widgets-undecorate-text (text)
2088 (defun allout-widgets-undecorate-text (text)
2089 "Eliminate widgets and decorations for all items in TEXT."
2090 (remove-text-properties 0 (length text)
2091 '(display nil :icon-state nil rear-nonsticky nil
2092 category nil button nil field nil)
2095 ;;;_ > allout-widgets-undecorate-item (item-widget &optional no-expose)
2096 (defun allout-widgets-undecorate-item (item-widget &optional no-expose)
2097 "Remove widget decorations from ITEM-WIDGET.
2099 Any concealed content head lines and item body is exposed, unless
2100 optional NO-EXPOSE is non-nil."
2101 (let ((from (widget-get item-widget :from))
2102 (to (widget-get item-widget :to))
2103 (text-properties-to-remove '(display nil
2109 (span-overlay (widget-get item-widget :span-overlay))
2110 (button-overlay (widget-get item-widget :button))
2111 (was-modified (buffer-modified-p))
2112 (buffer-undo-list t)
2113 (inhibit-read-only t))
2115 (allout-flag-region from to nil))
2117 (remove-text-properties from to text-properties-to-remove))
2119 (delete-overlay span-overlay) (widget-put item-widget :span-overlay nil))
2120 (when button-overlay
2121 (delete-overlay button-overlay) (widget-put item-widget :button nil))
2122 (set-marker from nil)
2124 (if (not was-modified)
2125 (set-buffer-modified-p nil))))
2127 ;;;_ : Item decoration support
2128 ;;;_ > allout-item-span (item-widget &optional start end)
2129 (defun allout-item-span (item-widget &optional start end)
2130 "Return or register the location of an ITEM-WIDGET's actual START and END.
2132 If START and END are not passed in, return either a dotted pair
2133 of the current span, if established, or nil if not yet set.
2135 When the START and END are passed, return the distance that the
2136 start of the item moved. We return 0 if the span was not
2137 previously established or is not moved."
2138 (let ((overlay (widget-get item-widget :span-overlay))
2141 (cond ((not overlay) (when start
2142 (setq overlay (make-overlay start end nil t nil))
2143 (overlay-put overlay 'button item-widget)
2144 (overlay-put overlay 'evaporate t)
2145 (widget-put item-widget :span-overlay overlay)
2148 ((not start) (cons (overlay-start overlay) (overlay-end overlay)))
2150 ((or (not (equal (overlay-start overlay) start))
2151 (not (equal (overlay-end overlay) end)))
2152 (move-overlay overlay start end)
2154 ;; specified span already set:
2156 ;;;_ > allout-item-element-span-is (item-widget element
2157 ;;; &optional start end force)
2158 (defun allout-item-element-span-is (item-widget element
2159 &optional start end force)
2160 "Return or register the location of the indicated ITEM-WIDGET ELEMENT.
2162 ELEMENT is one of :guides-span, :icon-span, :cue-span, or :body-span.
2164 When optional START is specified, optional END must also be.
2166 START and END are the actual bounds of the region, if provided.
2168 If START and END are not passed in, we return either a dotted
2169 pair of the current span, if established, or nil if not yet set.
2171 When the START and END are passed, we return t if the region
2172 changed or nil if not.
2174 Optional FORCE means force assignment of the region's text
2175 property, even if it's already set."
2176 (let ((span (widget-get item-widget element)))
2177 (cond ((or (not span) force)
2179 (widget-put item-widget element (cons start end))
2180 (put-text-property start end 'category
2182 allout-span-to-category)))
2186 ;; move if necessary:
2187 ((not (and (eq (car span) start)
2188 (eq (cdr span) end)))
2189 (widget-put item-widget element span)
2191 ;; specified span already set:
2193 ;;;_ : Item widget retrieval (/ high-level creation):
2194 ;;;_ > allout-get-item-widget (&optional container)
2195 (defun allout-get-item-widget (&optional container)
2196 "Return the widget for the item at point, or nil if no widget yet exists.
2198 Point must be situated *before* the start of the target item's
2199 body, so we don't get an existing containing item when we're in
2200 the process of creating an item in the middle of another.
2202 Optional CONTAINER is used to obtain the container item."
2203 (if (or container (zerop (allout-depth)))
2204 allout-container-item-widget
2205 ;; allout-recent-* are calibrated by (allout-depth) if we got here.
2206 (let ((got (widget-at allout-recent-prefix-beginning)))
2207 (if (and got (listp got))
2208 (if (marker-position (widget-get got :from))
2210 (>= (point) (widget-apply got :actual-position :from))
2211 (<= (point) (widget-apply got :actual-position :body-start))
2213 ;; a wacky residual item - undecorate and disregard:
2214 (allout-widgets-undecorate-item got)
2216 ;;;_ > allout-get-or-create-item-widget (&optional redecorate blank-container)
2217 (defun allout-get-or-create-item-widget (&optional redecorate blank-container)
2218 "Return a widget for the item at point, creating the widget if necessary.
2220 When creating a widget, we assume there has been a context change
2221 and decorate its siblings and parent, as well.
2223 Optional BLANK-CONTAINER is for internal use, to fabricate a
2224 meta-container item with an empty body when the first proper
2225 \(non-container\) item starts at the beginning of the file.
2227 Optional REDECORATE, if non-nil, means to redecorate the widget
2228 if it already exists."
2229 (let ((widget (allout-get-item-widget blank-container))
2230 (buffer-undo-list t))
2231 (cond (widget (if redecorate
2232 (allout-redecorate-item widget))
2234 ((or blank-container (zerop (allout-depth)))
2235 (or allout-container-item-widget
2236 (setq allout-container-item-widget
2237 (allout-decorate-item-and-context
2238 (widget-convert 'allout-item-widget)
2239 nil blank-container))))
2240 ;; create a widget for a regular/non-container item:
2241 (t (allout-decorate-item-and-context (widget-convert
2242 'allout-item-widget))))))
2243 ;;;_ > allout-get-or-create-parent-widget (&optional redecorate)
2244 (defun allout-get-or-create-parent-widget (&optional redecorate)
2245 "Return widget for parent of item at point, decorating it if necessary.
2247 We return the container widget if we're above the first proper item in the
2250 Optional REDECORATE, if non-nil, means to redecorate the widget if it
2253 Point will wind up positioned on the beginning of the parent or beginning
2255 ;; use existing widget, if there, else establish it
2256 (if (or (bobp) (and (not (allout-ascend))
2257 (looking-at allout-regexp)))
2258 (allout-get-or-create-item-widget redecorate 'blank-container)
2259 (allout-get-or-create-item-widget redecorate)))
2260 ;;;_ : X- Item ancillaries
2261 ;;;_ >X allout-body-modification-handler (beg end)
2262 (defun allout-body-modification-handler (beg end)
2263 "Do routine processing of body text before and after modification.
2265 Operation is inhibited by `allout-inhibit-body-modification-handler'."
2267 ;; The primary duties are:
2269 ;; - marking of escaped prefix-like text for delayed cleanup of escapes
2270 ;; - removal and replacement of the settings
2271 ;; - maintenance of beginning-of-line guide lines
2273 ;; ?? Escapes removal \(before changes\) is not done when edits span multiple
2274 ;; items, recognizing that item structure is being preserved, including
2275 ;; escaping of item-prefix-like text within bodies. See
2276 ;; `allout-before-modification-handler' and
2277 ;; `allout-inhibit-body-modification-handler'.
2279 ;; Adds the overlay to the `allout-unresolved-body-mod-workhash' during
2280 ;; before-change operation, and removes from that list during after-change
2282 (cond (allout-inhibit-body-modification-hook nil)))
2283 ;;;_ >X allout-graphics-modification-handler (beg end)
2284 (defun allout-graphics-modification-handler (beg end)
2285 "Protect against incoherent deletion of decoration graphics.
2287 Deletes allowed only when inhibit-read-only is t."
2289 (undo-in-progress (when (eq (get-text-property beg 'category)
2290 'allout-icon-span-category)
2293 (let* ((item-widget (allout-get-item-widget)))
2295 (allout-widgets-exposure-undo-recorder
2297 (inhibit-read-only t)
2298 ((not (and (boundp 'allout-mode) allout-mode)) t)
2299 ((equal this-command 'quoted-insert) t)
2300 ((yes-or-no-p "Unruly edit of outline structure - allow? ")
2301 (setq allout-widgets-unset-inhibit-read-only (not inhibit-read-only)
2302 inhibit-read-only t))
2304 (substitute-command-keys allout-structure-unruly-deletion-message)))))
2305 ;;;_ > allout-item-icon-key-handler ()
2306 (defun allout-item-icon-key-handler ()
2307 "Catchall handling of key bindings in item icon/cue hot-spots.
2309 Applies `allout-hotspot-key-handler' and calls the result, if any, as an
2310 interactive command."
2313 (let* ((mapped-binding (allout-hotspot-key-handler)))
2314 (when mapped-binding
2315 (call-interactively mapped-binding))))
2318 ;;;_ . allout-item-location (item-widget)
2319 (defun allout-item-location (item-widget)
2320 "Location of the start of the item's text."
2321 (overlay-start (widget-get item-widget :span-overlay)))
2323 ;;;_ : Icon management
2324 ;;;_ > allout-fetch-icon-image (name)
2325 (defun allout-fetch-icon-image (name)
2326 "Fetch allout icon for symbol NAME.
2328 We use a caching strategy, so the caller doesn't need to do so."
2329 (let* ((types allout-widgets-icon-types)
2330 (use-dir (if (equal (allout-frame-property nil 'background-mode)
2332 allout-widgets-icons-light-subdir
2333 allout-widgets-icons-dark-subdir))
2334 (key (list name use-dir))
2335 (got (assoc key allout-widgets-icons-cache)))
2337 ;; display system shows only first of subsequent adjacent
2338 ;; `eq'-identical repeats - use copies to avoid this problem.
2339 (allout-widgets-copy-list (cadr got))
2340 (while (and types (not got))
2343 (list (append (list :type (car types)
2344 :file (concat use-dir
2348 (if (featurep 'xemacs)
2349 allout-widgets-item-image-properties-xemacs
2350 allout-widgets-item-image-properties-emacs)
2352 (setq types (cdr types)))
2354 (push (list key got) allout-widgets-icons-cache))
2357 ;;;_ : Miscellaneous
2358 ;;;_ > allout-elapsed-time-seconds (triple)
2359 (defun allout-elapsed-time-seconds (end start)
2360 "Return seconds between `current-time' style time START/END triples."
2361 (let ((elapsed (time-subtract end start)))
2362 (float-time elapsed)))
2363 ;;;_ > allout-frame-property (frame property)
2364 (defalias 'allout-frame-property
2365 (cond ((fboundp 'frame-parameter)
2367 ((fboundp 'frame-property)
2370 ;;;_ > allout-find-image (specs)
2371 (defalias 'allout-find-image
2372 (if (fboundp 'find-image)
2374 nil) ; aka, not-yet-implemented for xemacs.
2376 ;;;_ > allout-widgets-copy-list (list)
2377 (defun allout-widgets-copy-list (list)
2378 ;; duplicated from cl.el 'copy-list' as of 2008-08-17
2379 "Return a copy of LIST, which may be a dotted list.
2380 The elements of LIST are not copied, just the list structure itself."
2383 (while (consp list) (push (pop list) res))
2384 (prog1 (nreverse res) (setcdr res list)))
2386 ;;;_ . allout-widgets-count-buttons-in-region (start end)
2387 (defun allout-widgets-count-buttons-in-region (start end)
2388 "Debugging/diagnostic tool - count overlays with 'button' property in region."
2390 (setq start (or start (point-min))
2391 end (or end (point-max)))
2392 (if (> start end) (let ((interim start)) (setq start end end interim)))
2393 (let ((button-overlays (delq nil
2394 (mapcar (function (lambda (o)
2395 (if (overlay-get o 'button)
2397 (overlays-in start end)))))
2398 (length button-overlays)))
2400 ;;;_ : Run unit tests:
2401 (defun allout-widgets-run-unit-tests ()
2402 (message "Running allout-widget tests...")
2404 (allout-test-range-overlaps)
2406 (message "Running allout-widget tests... Done.")
2409 (when allout-widgets-run-unit-tests-on-load
2410 (allout-widgets-run-unit-tests))
2413 (provide 'allout-widgets)
2415 ;;;_. Local emacs vars.
2416 ;;;_ , Local variables:
2417 ;;;_ , allout-layout: (-1 : 0)