1 ;; allout-widgets.el --- Visually highlight allout outline structure.
3 ;; Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011 Ken Manheimer
5 ;; Author: Ken Manheimer <ken dot manheimer at gmail dot com>
6 ;; Maintainer: Ken Manheimer <ken dot manheimer at gmail dot com>
11 ;; Website: http://myriadicity.net/Sundry/EmacsAllout
15 ;; This is an allout outline-mode add-on that highlights outline structure
16 ;; with graphical widgets.
18 ;; To activate, customize `allout-widgets-auto-activation'. You can also
19 ;; invoke allout-widgets-mode in a particular allout buffer. When
20 ;; auto-enabled, you can inhibit widget operation in particular allout
21 ;; buffers by setting the variable `allout-widgets-mode-inhibit' non-nil in
22 ;; that file's buffer. Use emacs *file local variables* to generally
23 ;; inhibit for a file.
25 ;; See the `allout-widgets-mode' docstring for more details.
27 ;; Info about allout and allout-widgets development are available at
28 ;; http://myriadicity.net/Sundry/EmacsAllout
30 ;; The graphics include:
32 ;; - icons for item bullets, varying to distinguish whether the item either
33 ;; lacks any subitems, the subitems are currently collapsed within the
34 ;; item, or the item is currently expanded.
36 ;; - guide lines connecting item bullet-icons with those of their subitems.
38 ;; - cue area between the bullet-icon and the start of the body headline,
39 ;; for item numbering, encryption indicator, and distinctive bullets.
41 ;; The bullet-icon and guide line graphics provide keybindings and mouse
42 ;; bindings for easy outline navigation and exposure control, extending
43 ;; outline hot-spot navigation (see `allout-mode' docstring for details).
45 ;; Developers note: Our use of emacs widgets is unconventional. We
46 ;; decorate existing text rather than substituting for it, to
47 ;; piggy-back on existing allout operation. This employs the C-coded
48 ;; efficiencies of widget-apply, widget-get, and widget-put, along
49 ;; with the basic object-oriented organization of widget-create, to
50 ;; systematically couple overlays, graphics, and other features with
51 ;; allout-governed text.
53 ;;;_: Code (structured with comments that delinieate an allout outline)
55 ;;;_ : General Environment
66 ;;;_ : internal variables needed before user-customization variables
67 ;;; In order to enable activation of allout-widgets-mode via customization,
68 ;;; allout-widgets-auto-activation uses a setting function. That function
69 ;;; is invoked when the customization variable definition is evaluated,
70 ;;; during file load, so the involved code must reside above that
71 ;;; definition in the file.
72 ;;;_ = allout-widgets-mode
73 (defvar allout-widgets-mode nil
74 "Allout mode enhanced with graphical widgets.")
75 (make-variable-buffer-local 'allout-widgets-mode
)
77 ;;;_ : USER CUSTOMIZATION VARIABLES and incidental functions:
78 ;;;_ > defgroup allout-widgets
80 (defgroup allout-widgets nil
81 "Allout extension that highlights outline structure graphically.
83 Customize `allout-widgets-auto-activation' to activate allout-widgets
86 ;;;_ > defgroup allout-widgets-developer
87 (defgroup allout-widgets-developer nil
88 "Settings for development of allout widgets extension."
89 :group
'allout-widgets
)
90 ;;;_ ; some functions a bit early, for allout-auto-activation dependency:
91 ;;;_ > allout-widgets-mode-enable
92 (defun allout-widgets-mode-enable ()
93 "Enable allout-widgets-mode in allout-mode buffers.
95 See `allout-widgets-mode-inhibit' for per-file/per-buffer
96 inhibition of allout-widgets-mode."
97 (add-hook 'allout-mode-off-hook
'allout-widgets-mode-off
)
98 (add-hook 'allout-mode-on-hook
'allout-widgets-mode-on
)
100 ;;;_ > allout-widgets-mode-disable
101 (defun allout-widgets-mode-disable ()
102 "Disable allout-widgets-mode in allout-mode buffers.
104 See `allout-widgets-mode-inhibit' for per-file/per-buffer
105 inhibition of allout-widgets-mode."
106 (remove-hook 'allout-mode-off-hook
'allout-widgets-mode-off
)
107 (remove-hook 'allout-mode-on-hook
'allout-widgets-mode-on
)
109 ;;;_ > allout-widgets-setup (varname value)
111 (defun allout-widgets-setup (varname value
)
112 "Commission or decommision allout-widgets-mode along with allout-mode.
114 Meant to be used by customization of `allout-widgets-auto-activation'."
115 (set-default varname value
)
116 (if allout-widgets-auto-activation
117 (allout-widgets-mode-enable)
118 (allout-widgets-mode-disable)))
119 ;;;_ = allout-widgets-auto-activation
121 (defcustom allout-widgets-auto-activation nil
122 "Activate to enable allout icon graphics wherever allout mode is active.
124 Also enable `allout-auto-activation' for this to take effect upon
127 When this is set you can disable allout widgets in select files
128 by setting `allout-widgets-mode-inhibit'
130 Instead of setting `allout-widgets-auto-activation' you can
131 explicitly invoke `allout-widgets-mode' in allout buffers where
132 you want allout widgets operation.
134 See `allout-widgets-mode' for allout widgets mode features."
136 :group
'allout-widgets
137 :set
'allout-widgets-setup
139 ;; ;;;_ = allout-widgets-allow-unruly-edits
140 ;; (defcustom allout-widgets-allow-unruly-edits nil
141 ;; "*Control whether manual edits are restricted to maintain outline integrity.
143 ;; When nil, manual edits must either be within an item's body or encompass
144 ;; one or more items completely - eg, killing topics as entities, rather than
145 ;; deleting from the middle of one to the middle of another.
147 ;; If you only occasionally need to make unrestricted change, you can set this
148 ;; variable in the specific buffer using set-variable, or just deactivate
149 ;; `allout-mode' temporarily. You can customize this to always allow unruly
150 ;; edits, but you will be able to create outlines that are unnavigable in
151 ;; principle, and not just for allout's navigation and exposure mechanisms."
153 ;; :group allout-widgets)
154 ;; (make-variable-buffer-local 'allout-widgets-allow-unruly-edits)
155 ;;;_ = allout-widgets-auto-activation - below, for eval-order dependencies
156 ;;;_ = allout-widgets-icons-dark-subdir
157 (defcustom allout-widgets-icons-dark-subdir
"icons/allout-widgets/dark-bg/"
158 "Directory on `image-load-path' holding allout icons for dark backgrounds."
160 :group
'allout-widgets
)
161 ;;;_ = allout-widgets-icons-light-subdir
162 (defcustom allout-widgets-icons-light-subdir
"icons/allout-widgets/light-bg/"
163 "Directory on `image-load-path' holding allout icons for light backgrounds."
165 :group
'allout-widgets
)
166 ;;;_ = allout-widgets-icon-types
167 (defcustom allout-widgets-icon-types
'(xpm png
)
168 "File extensions for the icon graphic format types, in order of preference."
169 :type
'(repeat symbol
)
170 :group
'allout-widgets
)
172 ;;;_ . Decoration format
173 ;;;_ = allout-widgets-theme-dark-background
174 (defcustom allout-widgets-theme-dark-background
"allout-dark-bg"
175 "Identify the outline's icon theme to use with a dark background."
177 :group
'allout-widgets
)
178 ;;;_ = allout-widgets-theme-light-background
179 (defcustom allout-widgets-theme-light-background
"allout-light-bg"
180 "Identify the outline's icon theme to use with a light background."
182 :group
'allout-widgets
)
183 ;;;_ = allout-widgets-item-image-properties-emacs
184 (defcustom allout-widgets-item-image-properties-emacs
185 '(:ascent center
:mask
(heuristic t
))
186 "*Default properties item widget images in mainline Emacs."
188 :group
'allout-widgets
)
189 ;;;_ = allout-widgets-item-image-properties-xemacs
190 (defcustom allout-widgets-item-image-properties-xemacs
192 "*Default properties item widget images in XEmacs."
194 :group
'allout-widgets
)
196 ;;;_ = allout-widgets-run-unit-tests-on-load
197 (defcustom allout-widgets-run-unit-tests-on-load nil
198 "*When non-nil, unit tests will be run at end of loading allout-widgets.
200 Generally, allout widgets code developers are the only ones who'll want to
203 \(If set, this makes it an even better practice to exercise changes by
204 doing byte-compilation with a repeat count, so the file is loaded after
207 See `allout-widgets-run-unit-tests' to see what's run."
209 :group
'allout-widgets-developer
)
210 ;;;_ = allout-widgets-time-decoration-activity
211 (defcustom allout-widgets-time-decoration-activity nil
212 "*Retain timing info of the last cooperative redecoration.
214 The details are retained as the value of
215 `allout-widgets-last-decoration-timing'.
217 Generally, allout widgets code developers are the only ones who'll want to
220 :group
'allout-widgets-developer
)
221 ;;;_ = allout-widgets-hook-error-post-time 0
222 (defcustom allout-widgets-hook-error-post-time
0
223 "*Amount of time to sit showing hook error messages.
225 0 is minimal, or nil to not post to the message area.
227 This is for debugging purposes."
229 :group
'allout-widgets-developer
)
230 ;;;_ = allout-widgets-maintain-tally nil
231 (defcustom allout-widgets-maintain-tally nil
232 "*If non-nil, maintain a collection of widgets, `allout-widgets-tally'.
234 This is for debugging purposes.
236 The tally shows the total number of item widgets in the current
237 buffer, and tracking increases as new widgets are added and
238 decreases as obsolete widgets are garbage collected."
240 :group
'allout-widgets-developer
)
241 (defvar allout-widgets-tally nil
242 "Hash-table of existing allout widgets, for debugging.
244 Table is maintained iff `allout-widgets-maintain-tally' is non-nil.
246 The table contents will be out of sync if any widgets are created
247 or deleted while this variable is nil.")
248 (make-variable-buffer-local 'allout-widgets-tally
)
249 (defvar allout-widgets-mode-inhibit
) ; defined below
250 ;;;_ > allout-widgets-tally-string
251 (defun allout-widgets-tally-string ()
252 "Return a string giving the number of tracked widgets, or empty string if not tracking.
254 The string is formed for appending to the allout-mode mode-line lighter.
256 An empty string is also returned if tracking is inhibited or
257 widgets are locally inhibited.
259 The number varies according to the evanescence of objects on a
260 hash table with weak keys, so tracking of widget erasures is often delayed."
261 (when (and allout-widgets-maintain-tally
262 (not allout-widgets-mode-inhibit
)
263 allout-widgets-tally
)
264 (format ":%s" (hash-table-count allout-widgets-tally
))))
265 ;;;_ = allout-widgets-track-decoration nil
266 (defcustom allout-widgets-track-decoration nil
267 "*If non-nil, show cursor position of each item decoration.
269 This is for debugging purposes, and generally set at need in a
270 buffer rather than as a prevailing configuration \(but it's handy
271 to publicize it by making it a customization variable\)."
273 :group
'allout-widgets-developer
)
274 (make-variable-buffer-local 'allout-widgets-track-decoration
)
276 ;;;_ : Mode context - variables, hookup, and hooks
277 ;;;_ . internal mode variables
278 ;;;_ , Mode activation and environment
279 ;;;_ = allout-widgets-version
280 (defvar allout-widgets-version
"1.0"
281 "Version of currently loaded allout-widgets extension.")
282 ;;;_ > allout-widgets-version
283 (defun allout-widgets-version (&optional here
)
284 "Return string describing the loaded outline version."
286 (let ((msg (concat "Allout Outline Widgets Extension v "
287 allout-widgets-version
)))
288 (if here
(insert msg
))
291 ;;;_ = allout-widgets-mode-inhibit
292 (defvar allout-widgets-mode-inhibit nil
293 "Inhibit `allout-widgets-mode' from activating widgets.
295 This also inhibits automatic adjustment of widgets to track allout outline
298 You can use this as a file local variable setting to disable
299 allout widgets enhancements in selected buffers while generally
300 enabling widgets by customizing `allout-widgets-auto-activation'.
302 In addition, you can invoked `allout-widgets-mode' allout-mode
303 buffers where this is set to enable and disable widget
304 enhancements, directly.")
306 (put 'allout-widgets-mode-inhibit
'safe-local-variable
307 (if (fboundp 'booleanp
) 'booleanp
(lambda (x) (member x
'(t nil
)))))
308 (make-variable-buffer-local 'allout-widgets-mode-inhibit
)
309 ;;;_ = allout-inhibit-body-modification-hook
310 (defvar allout-inhibit-body-modification-hook nil
311 "Override de-escaping of text-prefixes in item bodies during specific changes.
313 This is used by `allout-buffer-modification-handler' to signal such changes
314 to `allout-body-modification-handler', and is always reset by
315 `allout-post-command-business'.")
316 (make-variable-buffer-local 'allout-inhibit-body-modification-hook
)
317 ;;;_ = allout-widgets-icons-cache
318 (defvar allout-widgets-icons-cache nil
319 "Cache allout icon images, as an association list.
321 `allout-fetch-icon-image' uses this cache transparently, keying
322 images with lists containing the name of the icon directory \(as
323 found on the `load-path') and the icon name.
325 Set this variable to `nil' to empty the cache, and have it replenish from the
327 ;;;_ = allout-widgets-unset-inhibit-read-only
328 (defvar allout-widgets-unset-inhibit-read-only nil
329 "Tell `allout-widgets-post-command-business' to unset `inhibit-read-only'.
331 Used by `allout-graphics-modification-handler'")
332 ;;;_ = allout-widgets-reenable-before-change-handler
333 (defvar allout-widgets-reenable-before-change-handler nil
334 "Tell `allout-widgets-post-command-business' to reequip the handler.
336 Necessary because the handler sometimes deliberately raises an
337 error, causing it to be disabled.")
338 ;;;_ , State for hooks
339 ;;;_ = allout-unresolved-body-mod-workroster
340 (defvar allout-unresolved-body-mod-workroster
(make-hash-table :size
16)
341 "List of body-overlays that did before-change business but not after-change.
343 See `allout-post-command-business' and `allout-body-modification-handler'.")
344 ;;;_ = allout-structure-unruly-deletion-message
345 (defvar allout-structure-unruly-deletion-message
346 "Unruly edit prevented --
347 To change the bullet character: \\[allout-rebullet-current-heading]
348 To promote this item: \\[allout-shift-out]
349 To demote it: \\[allout-shift-in]
350 To delete it and offspring: \\[allout-kill-topic]
351 See \\[describe-mode] for many more options."
352 "Informative message presented on improper editing of outline structure.
354 The structure includes the guides lines, bullet, and bullet cue.")
355 ;;;_ = allout-widgets-changes-record
356 (defvar allout-widgets-changes-record nil
357 "Record outline changes for processing by post-command hook.
359 Entries on the list are lists whose first element is a symbol indicating
360 the change type and subsequent elements are data specific to that change
363 'exposure `allout-exposure-from' `allout-exposure-to' `allout-exposure-flag'
365 The changes are recorded in reverse order, with new values pushed
367 (make-variable-buffer-local 'allout-widgets-changes-record
)
368 ;;;_ = allout-widgets-undo-exposure-record
369 (defvar allout-widgets-undo-exposure-record nil
370 "Record outline undo traces for processing by post-command hook.
372 The changes are recorded in reverse order, with new values pushed
374 (make-variable-buffer-local 'allout-widgets-undo-exposure-record
)
375 ;;;_ = allout-widgets-last-hook-error
376 (defvar allout-widgets-last-hook-error nil
377 "String holding last error string, for debugging purposes.")
378 ;;;_ = allout-widgets-adjust-message-length-threshold 100
379 (defvar allout-widgets-adjust-message-length-threshold
100
380 "Display \"Adjusting widgets\" message above this number of pending changes."
382 ;;;_ = allout-widgets-adjust-message-size-threshold 10000
383 (defvar allout-widgets-adjust-message-size-threshold
10000
384 "Display \"Adjusting widgets\" message above this size of pending changes."
386 ;;;_ = allout-doing-exposure-undo-processor nil
387 (defvar allout-undo-exposure-in-progress nil
388 "Maintained true during `allout-widgets-exposure-undo-processor'")
389 ;;;_ , Widget-specific outline text format
390 ;;;_ = allout-escaped-prefix-regexp
391 (defvar allout-escaped-prefix-regexp
""
392 "*Regular expression for body text that would look like an item prefix if
393 not altered with an escape sequence.")
394 (make-variable-buffer-local 'allout-escaped-prefix-regexp
)
395 ;;;_ , Widget element formatting
396 ;;;_ = allout-item-icon-keymap
397 (defvar allout-item-icon-keymap
398 (let ((km (make-sparse-keymap)))
399 (dolist (digit '("0" "1" "2" "3"
400 "4" "5" "6" "7" "8" "9"))
401 (define-key km digit
'digit-argument
))
402 (define-key km
"-" 'negative-argument
)
403 ;; (define-key km [(return)] 'allout-tree-expand-command)
404 ;; (define-key km [(meta return)] 'allout-toggle-torso-command)
405 ;; (define-key km [(down-mouse-1)] 'allout-item-button-click)
406 ;; (define-key km [(down-mouse-2)] 'allout-toggle-torso-event-command)
407 ;; Override underlying mouse-1 and mouse-2 bindings in icon territory:
408 (define-key km
[(mouse-1)] (lambda () (interactive) nil
))
409 (define-key km
[(mouse-2)] (lambda () (interactive) nil
))
411 ;; Catchall, handles actual keybindings, dynamically doing keymap lookups:
412 (define-key km
[t] 'allout-item-icon-key-handler)
415 "General tree-node key bindings.")
416 ;;;_ = allout-item-body-keymap
417 (defvar allout-item-body-keymap
418 (let ((km (make-sparse-keymap))
419 (local-map (current-local-map)))
420 ;; (define-key km [(control return)] 'allout-tree-expand-command)
421 ;; (define-key km [(meta return)] 'allout-toggle-torso-command)
422 ;; XXX We need to reset this per buffer's mode; we do so in
423 ;; allout-widgets-mode.
425 (set-keymap-parent km local-map))
428 "General key bindings for the text content of outline items.")
429 (make-variable-buffer-local 'allout-item-body-keymap)
430 ;;;_ = allout-body-span-category
431 (defvar allout-body-span-category nil
432 "Symbol carrying allout body-text overlay properties.")
433 ;;;_ = allout-cue-span-keymap
434 (defvar allout-cue-span-keymap
435 (let ((km (make-sparse-keymap)))
436 (set-keymap-parent km allout-item-icon-keymap)
438 "Keymap used in the item cue area - the space between the icon and headline.")
439 ;;;_ = allout-escapes-category
440 (defvar allout-escapes-category nil
441 "Symbol for category of text property used to hide escapes of prefix-like
442 text in allout item bodies.")
443 ;;;_ = allout-guides-category
444 (defvar allout-guides-category nil
445 "Symbol carrying allout icon-guides overlay properties.")
446 ;;;_ = allout-guides-span-category
447 (defvar allout-guides-span-category nil
448 "Symbol carrying allout icon and guide lines overlay properties.")
449 ;;;_ = allout-icon-span-category
450 (defvar allout-icon-span-category nil
451 "Symbol carrying allout icon and guide lines overlay properties.")
452 ;;;_ = allout-cue-span-category
453 (defvar allout-cue-span-category nil
454 "Symbol carrying common properties of the space following the outline icon.
456 \(That space is used to convey selected cues indicating body qualities,
457 including things like:
460 - indirect reference '@'
461 - distinctive bullets - see `allout-distinctive-bullets-string'.\)")
462 ;;;_ = allout-span-to-category
463 (defvar allout-span-to-category
464 '((:guides-span . allout-guides-span-category)
465 (:cue-span . allout-cue-span-category)
466 (:icon-span . allout-icon-span-category)
467 (:body-span . allout-body-span-category))
468 "Association list mapping span identifier to category identifier.")
469 ;;;_ = allout-trailing-category
470 (defvar allout-trailing-category nil
471 "Symbol carrying common properties of an overlay's trailing newline.")
473 (defvar allout-widgets-last-decoration-timing nil
474 "Timing details for the last cooperative decoration action.
476 This is maintained when `allout-widgets-time-decoration-activity' is set.
478 The value is a list containing two elements:
479 - the elapsed time as a number of seconds
480 - the list of changes processed, a la `allout-widgets-changes-record'.
482 When active, the value is revised each time automatic decoration activity
483 happens in the buffer.")
484 (make-variable-buffer-local 'allout-widgets-last-decoration-timing)
486 ;;;_ > define-minor-mode allout-widgets-mode (arg)
488 (define-minor-mode allout-widgets-mode
489 "Allout-mode extension, providing graphical decoration of outline structure.
491 This is meant to operate along with allout-mode, via `allout-mode-hook'.
493 If optional argument ARG is greater than 0, enable.
494 If optional argument ARG is less than 0, disable.
495 Anything else, toggle between active and inactive.
497 The graphics include:
499 - guide lines connecting item bullet-icons with those of their subitems.
501 - icons for item bullets, varying to indicate whether or not the item
502 has subitems, and if so, whether or not the item is expanded.
504 - cue area between the bullet-icon and the start of the body headline,
505 for item numbering, encryption indicator, and distinctive bullets.
507 The bullet-icon and guide line graphics provide keybindings and mouse
508 bindings for easy outline navigation and exposure control, extending
509 outline hot-spot navigation \(see `allout-mode')."
514 ;; define-minor-mode handles any provided argument according to emacs
515 ;; minor-mode conventions - '(elisp) Minor Mode Conventions' - and sets
516 ;; allout-widgets-mode accordingly *before* running the body code, so we
518 (if allout-widgets-mode
521 (allout-add-resumptions
522 ;; XXX user may need say in line-truncation/hscrolling - an option
523 ;; that abstracts mode.
524 ;; truncate text lines to keep guide lines intact:
526 ;; and enable autoscrolling to ease view of text
527 '(auto-hscroll-mode t)
528 '(line-move-ignore-fields t)
529 '(widget-push-button-prefix "")
530 '(widget-push-button-suffix "")
531 ;; allout-escaped-prefix-regexp depends on allout-regexp:
532 (list 'allout-escaped-prefix-regexp (concat "\\(\\\\\\)"
533 "\\(" allout-regexp "\\)")))
534 (allout-add-resumptions
535 (list 'allout-widgets-tally allout-widgets-tally)
536 (list 'allout-widgets-escapes-sanitization-regexp-pair
537 (list (concat "\\(\n\\|\\`\\)"
538 allout-escaped-prefix-regexp
540 ;; Include everything but the escape symbol.
544 (add-hook 'after-change-functions 'allout-widgets-after-change-handler
547 (allout-setup-text-properties)
548 (add-to-invisibility-spec '(allout-torso . t))
549 (add-to-invisibility-spec 'allout-escapes)
551 (if (current-local-map)
552 (set-keymap-parent allout-item-body-keymap (current-local-map)))
554 (add-hook 'allout-exposure-change-hook
555 'allout-widgets-exposure-change-recorder nil 'local)
556 (add-hook 'allout-structure-added-hook
557 'allout-widgets-additions-recorder nil 'local)
558 (add-hook 'allout-structure-deleted-hook
559 'allout-widgets-deletions-recorder nil 'local)
560 (add-hook 'allout-structure-shifted-hook
561 'allout-widgets-shifts-recorder nil 'local)
562 (add-hook 'allout-after-copy-or-kill-hook
563 'allout-widgets-after-copy-or-kill-function nil 'local)
565 (add-hook 'before-change-functions 'allout-widgets-before-change-handler
567 (add-hook 'post-command-hook 'allout-widgets-post-command-business
569 (add-hook 'pre-command-hook 'allout-widgets-pre-command-business
572 ;; init the widgets tally for debugging:
573 (if (not allout-widgets-tally)
574 (setq allout-widgets-tally (make-hash-table
575 :test 'eq :weakness 'key)))
576 ;; add tally count display on minor-mode-alist just after
577 ;; allout-mode entry.
578 ;; (we use ternary condition form to keep condition simple for deletion.)
579 (let* ((mode-line-entry '(allout-widgets-mode-inhibit ""
580 (:eval (allout-widgets-tally-string))))
581 (associated (assoc (car mode-line-entry) minor-mode-alist))
582 ;; need location for it only if not already present:
583 (after (and (not associated)
584 (memq (assq 'allout-mode minor-mode-alist) minor-mode-alist))))
586 (rplacd after (cons mode-line-entry (cdr after)))))
587 (allout-widgets-prepopulate-buffer)
590 (let ((inhibit-read-only t)
591 (was-modified (buffer-modified-p)))
593 (allout-widgets-undecorate-region (point-min)(point-max))
594 (remove-from-invisibility-spec '(allout-torso . t))
595 (remove-from-invisibility-spec 'allout-escapes)
597 (remove-hook 'after-change-functions
598 'allout-widgets-after-change-handler 'local)
599 (remove-hook 'allout-exposure-change-hook
600 'allout-widgets-exposure-change-recorder 'local)
601 (remove-hook 'allout-structure-added-hook
602 'allout-widgets-additions-recorder 'local)
603 (remove-hook 'allout-structure-deleted-hook
604 'allout-widgets-deletions-recorder 'local)
605 (remove-hook 'allout-structure-shifted-hook
606 'allout-widgets-shifts-recorder 'local)
607 (remove-hook 'allout-after-copy-or-kill-hook
608 'allout-widgets-after-copy-or-kill-function 'local)
609 (remove-hook 'before-change-functions
610 'allout-widgets-before-change-handler 'local)
611 (remove-hook 'post-command-hook
612 'allout-widgets-post-command-business 'local)
613 (remove-hook 'pre-command-hook
614 'allout-widgets-pre-command-business 'local)
615 (assq-delete-all 'allout-widgets-mode-inhibit minor-mode-alist)
616 (set-buffer-modified-p was-modified))))
617 ;;;_ > allout-widgets-mode-off
618 (defun allout-widgets-mode-off ()
619 "Explicitly disable allout-widgets-mode."
620 (allout-widgets-mode -1))
621 ;;;_ > allout-widgets-mode-off
622 (defun allout-widgets-mode-on ()
623 "Explicitly disable allout-widgets-mode."
624 (allout-widgets-mode 1))
625 ;;;_ > allout-setup-text-properties ()
626 (defun allout-setup-text-properties ()
627 "Configure category and literal text properties."
629 ;; XXX body - before-change, entry, keymap
631 (setplist 'allout-guides-span-category nil)
632 (put 'allout-guides-span-category
633 'modification-hooks '(allout-graphics-modification-handler))
634 (put 'allout-guides-span-category 'local-map allout-item-icon-keymap)
635 (put 'allout-guides-span-category 'mouse-face widget-button-face)
636 (put 'allout-guides-span-category 'field 'structure)
637 ;; (put 'allout-guides-span-category 'face 'widget-button)
639 (setplist 'allout-icon-span-category
640 (allout-widgets-copy-list (symbol-plist
641 'allout-guides-span-category)))
642 (put 'allout-icon-span-category 'field 'structure)
644 ;; XXX for body text we're instead going to use the buffer-wide
645 ;; resources, like before/after-change-functions hooks and the
646 ;; buffer's key map. that way we won't have to do painful provisions
647 ;; to fixup things after edits, catch outlier interstitial
648 ;; characters, like newline and empty lines after hidden subitems,
650 (setplist 'allout-body-span-category nil)
651 (put 'allout-body-span-category 'evaporate t)
652 (put 'allout-body-span-category 'local-map allout-item-body-keymap)
653 ;;(put 'allout-body-span-category
654 ;; 'modification-hooks '(allout-body-modification-handler))
655 ;;(put 'allout-body-span-category 'field 'body)
657 (setplist 'allout-cue-span-category nil)
658 (put 'allout-cue-span-category 'evaporate t)
659 (put 'allout-cue-span-category
660 'modification-hooks '(allout-body-modification-handler))
661 (put 'allout-cue-span-category 'local-map allout-cue-span-keymap)
662 (put 'allout-cue-span-category 'mouse-face widget-button-face)
663 (put 'allout-cue-span-category 'pointer 'arrow)
664 (put 'allout-cue-span-category 'field 'structure)
666 (setplist 'allout-trailing-category nil)
667 (put 'allout-trailing-category 'evaporate t)
668 (put 'allout-trailing-category 'local-map allout-item-body-keymap)
670 (setplist 'allout-escapes-category nil)
671 (put 'allout-escapes-category 'invisible 'allout-escapes)
672 (put 'allout-escapes-category 'evaporate t))
673 ;;;_ > allout-widgets-prepopulate-buffer ()
674 (defun allout-widgets-prepopulate-buffer ()
675 "Step over the current buffers exposed items to do initial widgetizing."
676 (if (not allout-widgets-mode-inhibit)
678 (goto-char (point-min))
679 (while (allout-next-visible-heading 1)
680 (when (not (widget-at (point)))
681 (allout-get-or-create-item-widget))))))
682 ;;;_ . settings context
683 ;;;_ = allout-container-item
684 (defvar allout-container-item-widget nil
685 "A widget for the current outline's overarching container as an item.
687 The item has settings \(of the file/connection\) and maybe a body, but no
689 (make-variable-buffer-local 'allout-container-item-widget)
690 ;;;_ . Hooks and hook helpers
691 ;;;_ , major command-loop business:
692 ;;;_ > allout-widgets-pre-command-business (&optional recursing)
693 (defun allout-widgets-pre-command-business (&optional recursing)
694 "Handle actions pending before allout-mode activity."
696 ;;;_ > allout-widgets-post-command-business (&optional recursing)
697 (defun allout-widgets-post-command-business (&optional recursing)
698 "Handle actions pending after any allout-mode commands.
700 Optional RECURSING is for internal use, to limit recursion."
701 ;; - check changed text for nesting discontinuities and escape anything
702 ;; that's: (1) asterisks at bol or (2) excessively nested.
703 (condition-case failure
705 (when (and (boundp 'allout-mode) allout-mode)
707 (if allout-widgets-unset-inhibit-read-only
708 (setq inhibit-read-only nil
709 allout-widgets-unset-inhibit-read-only nil))
711 (when allout-widgets-reenable-before-change-handler
712 (add-hook 'before-change-functions
713 'allout-widgets-before-change-handler
715 (setq allout-widgets-reenable-before-change-handler nil))
717 (when (or allout-widgets-undo-exposure-record
718 allout-widgets-changes-record)
719 (let* ((debug-on-signal t)
721 ;; inhibit recording new undo records when processing
722 ;; effects of undo-exposure:
723 (debugger 'allout-widgets-hook-error-handler)
724 (adjusting-message " Adjusting widgets...")
725 (replaced-message (allout-widgets-adjusting-message
727 (start-time (current-time)))
729 (if allout-widgets-undo-exposure-record
730 ;; inhibit undo recording iff undoing exposure stuff.
731 ;; XXX we might need to inhibit per respective
732 ;; change-record, rather than assuming that some undo
733 ;; activity during a command is all undo activity.
734 (let ((buffer-undo-list t))
735 (allout-widgets-exposure-undo-processor)
736 (allout-widgets-changes-dispatcher))
737 (allout-widgets-exposure-undo-processor)
738 (allout-widgets-changes-dispatcher))
740 (if allout-widgets-time-decoration-activity
741 (setq allout-widgets-last-decoration-timing
742 (list (allout-elapsed-time-seconds (current-time)
744 allout-widgets-changes-record)))
746 (setq allout-widgets-changes-record nil)
749 (if (stringp replaced-message)
750 (message replaced-message)
753 ;; alas, decorated intermediate matches are not easily undecorated
754 ;; when they're automatically rehidden by isearch, so we're
755 ;; dropping this nicety.
756 ;; ;; Detect undecorated items, eg during isearch into previously
757 ;; ;; unexposed topics, and decorate "economically". Some
758 ;; ;; undecorated stuff is often exposed, to reduce lag, but the
759 ;; ;; item containing the cursor is decorated. We constrain
760 ;; ;; recursion to avoid being trapped by unexpectedly undecoratable
762 ;; (when (and (not recursing)
763 ;; (not (allout-current-decorated-p))
764 ;; (or (not (equal (allout-depth) 0))
765 ;; (not allout-container-item-widget)))
766 ;; (let ((buffer-undo-list t))
767 ;; (allout-widgets-exposure-change-recorder
768 ;; allout-recent-prefix-beginning allout-recent-prefix-end nil)
769 ;; (allout-widgets-post-command-business 'recursing)))
771 ;; Detect and rectify fouled outline structure - decorated item
772 ;; not at beginning of line.
773 (let ((this-widget (or (widget-at (point))
774 ;; XXX we really should be checking across
775 ;; edited span, not just point and point+1
776 (and (not (eq (point) (point-max)))
777 (widget-at (1+ (point))))))
781 (goto-char (widget-get this-widget :from))
786 (concat "Misplaced item won't be recognizable "
787 " as part of outline - rectify? "))
790 (if (allout-hidden-p (max (1- (point)) 1))
792 (goto-char (max (1- (point)) 1))
793 (allout-show-to-offshoot)))
794 (allout-widgets-undecorate-item this-widget))
795 ;; expose any hidden intervening items, so resulting
796 ;; position is clear:
797 (setq inserted-at (point))
798 (allout-unprotected (insert-before-markers "\n"))
800 ;; ensure the inserted newline is visible:
801 (allout-flag-region inserted-at (1+ inserted-at) nil)
802 (allout-widgets-post-command-business 'recursing)
803 (message (concat "outline structure corrected - item"
804 " moved to beginning of new line"))
805 ;; preserve cursor position in some cases:
807 (> (point) inserted-at))
808 (forward-char -1)))))))
811 ;; zero work list so we don't get stuck futily retrying.
812 ;; error recording done by allout-widgets-hook-error-handler.
813 (setq allout-widgets-changes-record nil))))
814 ;;;_ , major change handlers:
815 ;;;_ > allout-widgets-before-change-handler
816 (defun allout-widgets-before-change-handler (beg end)
817 "Business to be done before changes in a widgetized allout outline."
818 ;; protect against unruly edits to structure:
820 (undo-in-progress (when (eq (get-text-property beg 'category)
821 'allout-icon-span-category)
824 (let* ((item-widget (allout-get-item-widget)))
826 (allout-widgets-exposure-undo-recorder
828 (inhibit-read-only t)
829 ((not (and (boundp 'allout-mode) allout-mode)) t)
830 ((equal this-command 'quoted-insert) t)
831 ((not (text-property-any beg (if (equal end beg)
832 (min (1+ beg) (point-max))
836 ((yes-or-no-p "Unruly edit of outline structure - allow? ")
837 (setq allout-widgets-unset-inhibit-read-only (not inhibit-read-only)
838 inhibit-read-only t))
840 ;; tell the allout-widgets-post-command-business to reestablish the hook:
841 (setq allout-widgets-reenable-before-change-handler t)
842 ;; and raise an error to prevent the edit (and disable the hook):
844 (substitute-command-keys allout-structure-unruly-deletion-message)))))
845 ;;;_ > allout-widgets-after-change-handler
846 (defun allout-widgets-after-change-handler (beg end prelength)
847 "Reconcile what needs to be reconciled for allout widgets after edits."
849 ;;;_ > allout-current-decorated-p ()
850 (defun allout-current-decorated-p ()
851 "True if the current item is not decorated"
853 (if (allout-back-to-current-heading)
854 (if (> allout-recent-depth 0)
855 (and (allout-get-item-widget) t)
856 allout-container-item-widget))))
858 ;;;_ > allout-widgets-hook-error-handler
859 (defun allout-widgets-hook-error-handler (mode args)
860 "Process errors which occurred in the course of command hook operation.
862 We store a backtrace of the error information in the variable,
863 `allout-widgets-last-hook-error', unset the error handlers, and
864 reraise the error, so that processing continues to the
865 encompassing condition-case."
866 ;; first deconstruct special error environment so errors here propagate
867 ;; to encompassing condition-case:
868 (setq debugger 'debug
871 (let* ((bt (with-output-to-string (backtrace)))
872 (this "allout-widgets-hook-error-handler")
874 (format "allout-widgets-last-hook-error stored, %s/%s %s %s"
876 (format-time-string "%e-%b-%Y %r" (current-time)))))
877 ;; post to *Messages* then immediately replace with more compact notice:
878 (message "%s" (setq allout-widgets-last-hook-error
879 (format "%s:\n%s" header bt)))
880 (message header) (sit-for allout-widgets-hook-error-post-time)
881 ;; reraise the error, or one concerning this function if unexpected:
882 (if (equal mode 'error)
884 (error "%s: unexpected mode, %s %s" this mode args))))
885 ;;;_ > allout-widgets-changes-exceed-threshold-p ()
886 (defun allout-widgets-adjusting-message (message)
887 "Post MESSAGE when pending are likely to make a big enough delay.
889 If posting of the MESSAGE is warranted and there already is a
890 `current-message' in the minibuffer, the MESSAGE is appended to
891 the current one, and the previously pending `current-message' is
892 returned for later posting on completion.
894 If posting of the MESSAGE is warranted, but no `current-message'
895 is pending, then t is returned to indicate that case.
897 If posting of the MESSAGE is not warranted, then nil is returned.
899 See `allout-widgets-adjust-message-length-threshold',
900 `allout-widgets-adjust-message-size-threshold' for message
901 posting threshold criteria."
902 (if (or (> (length allout-widgets-changes-record)
903 allout-widgets-adjust-message-length-threshold)
904 ;; for size, use distance from start of first to end of last:
905 (let ((min (point-max))
908 (mapc (function (lambda (entry)
909 (if (eq :undone-exposure (car entry))
911 (setq first (cadr entry)
912 second (caddr entry))
913 (if (< (min first second) min)
914 (setq min (min first second)))
915 (if (> (max first second) max)
916 (setq max (max first second))))))
917 allout-widgets-changes-record)
918 (> (- max min) allout-widgets-adjust-message-size-threshold)))
919 (let ((prior (current-message)))
920 (message (if prior (concat prior " - " message) message))
922 ;;;_ > allout-widgets-changes-dispatcher ()
923 (defun allout-widgets-changes-dispatcher ()
924 "Dispatch CHANGES-RECORD items to respective widgets change processors."
926 (if (not allout-widgets-mode-inhibit)
927 (let* ((changes-record allout-widgets-changes-record)
928 (changes-pending (and changes-record t))
935 (when changes-pending
936 (while changes-record
937 (setq entry (pop changes-record))
939 (:exposed (push entry exposures))
940 (:added (push entry additions))
941 (:deleted (push entry deletions))
942 (:shifted (push entry shifts))))
945 (allout-widgets-exposure-change-processor exposures))
947 (allout-widgets-additions-processor additions))
949 (allout-widgets-deletions-processor deletions))
951 (allout-widgets-shifts-processor shifts))))
952 (when (not (equal allout-widgets-mode-inhibit 'undecorated))
953 (allout-widgets-undecorate-region (point-min)(point-max))
954 (setq allout-widgets-mode-inhibit 'undecorated))))
955 ;;;_ > allout-widgets-exposure-change-recorder (from to flag)
956 (defun allout-widgets-exposure-change-recorder (from to flag)
957 "Record allout exposure changes for tracking during post-command processing.
959 Records changes in `allout-widgets-changes-record'."
960 (push (list :exposed from to flag) allout-widgets-changes-record))
961 ;;;_ > allout-widgets-exposure-change-processor (changes)
962 (defun allout-widgets-exposure-change-processor (changes)
963 "Widgetize and adjust item widgets tracking allout outline exposure changes.
965 Generally invoked via `allout-exposure-change-hook'."
967 (let ((changes (sort changes (function (lambda (this next)
968 (< (cadr this) (cadr next))))))
969 ;; have to distinguish between concealing and exposing so that, eg,
970 ;; `allout-expose-topic's mix is handled properly.
976 (dolist (change changes)
981 (flag (cadddr change))
985 (if (< to from) (setq bucket to
989 ;; have we already handled exposure changes in this region?
990 (setq handling (if flag 'handled-conceal 'handled-expose)
991 got (allout-range-overlaps from to (symbol-value handling))
993 (set handling (cadr got))
1002 (allout-widgets-undecorate-region from to)
1003 (allout-beginning-of-current-line)
1004 (let ((widget (allout-get-item-widget)))
1006 (allout-get-or-create-item-widget)
1007 (widget-apply widget :redecorate))))
1011 (while (< (point) to)
1012 (allout-beginning-of-current-line)
1013 (setq parent (allout-get-item-widget))
1015 (setq parent (allout-get-or-create-item-widget))
1016 (widget-apply parent :redecorate))
1017 (allout-next-visible-heading 1)
1018 (if (widget-get parent :has-subitems)
1019 (allout-redecorate-visible-subtree parent))
1021 ;; subtree may be well beyond to - incorporate in ranges:
1022 (setq handled-expose
1023 (allout-range-overlaps from (point) handled-expose)
1024 covered (car handled-expose)
1025 handled-expose (cadr handled-expose)))
1026 (allout-next-visible-heading 1))))))))))
1028 ;;;_ > allout-widgets-additions-recorder (from to)
1029 (defun allout-widgets-additions-recorder (from to)
1030 "Record allout item additions for tracking during post-command processing.
1032 Intended for use on `allout-structure-added-hook'.
1034 FROM point at the start of the first new item and TO is point at the start
1037 Records changes in `allout-widgets-changes-record'."
1038 (push (list :added from to) allout-widgets-changes-record))
1039 ;;;_ > allout-widgets-additions-processor (changes)
1040 (defun allout-widgets-additions-processor (changes)
1041 "Widgetize and adjust items tracking allout outline structure additions.
1043 Dispatched by `allout-widgets-post-command-business' in response to
1044 :added entries recorded by `allout-widgets-additions-recorder'."
1048 (dolist (change changes)
1049 (let ((from (cadr change))
1051 (to (caddr change)))
1052 (if (< to from) (setq bucket to to from from bucket))
1053 ;; have we already handled exposure changes in this region?
1054 (setq handled (allout-range-overlaps from to handled)
1055 covered (car handled)
1056 handled (cadr handled))
1059 ;; Prior sibling and parent can both be affected.
1061 (allout-redecorate-visible-subtree
1062 (allout-get-or-create-item-widget 'redecorate)))
1063 (if (< (point) from)
1065 (while (and (< (point) to) (not (eobp)))
1066 (allout-beginning-of-current-line)
1067 (allout-redecorate-visible-subtree
1068 (allout-get-or-create-item-widget))
1069 (allout-next-visible-heading 1))
1071 ;; subtree may be well beyond to - incorporate in ranges:
1072 (setq handled (allout-range-overlaps from (point) handled)
1073 covered (car handled)
1074 handled (cadr handled)))))))))
1076 ;;;_ > allout-widgets-deletions-recorder (depth from)
1077 (defun allout-widgets-deletions-recorder (depth from)
1078 "Record allout item deletions for tracking during post-command processing.
1080 Intended for use on `allout-structure-deleted-hook'.
1082 DEPTH is the depth of the deleted subtree, and FROM is the point from which
1083 the subtree was deleted.
1085 Records changes in `allout-widgets-changes-record'."
1086 (push (list :deleted depth from) allout-widgets-changes-record))
1087 ;;;_ > allout-widgets-deletions-processor (changes)
1088 (defun allout-widgets-deletions-processor (changes)
1089 "Adjust items tracking allout outline structure deletions.
1091 Dispatched by `allout-widgets-post-command-business' in response to
1092 :deleted entries recorded by `allout-widgets-deletions-recorder'."
1094 (dolist (change changes)
1095 (let ((depth (cadr change))
1096 (from (caddr change)))
1098 (when (allout-previous-visible-heading 1)
1100 (allout-ascend-to-depth (1- depth)))
1101 (allout-redecorate-visible-subtree
1102 (allout-get-or-create-item-widget 'redecorate)))))))
1104 ;;;_ > allout-widgets-shifts-recorder (shifted-amount at)
1105 (defun allout-widgets-shifts-recorder (shifted-amount at)
1106 "Record outline subtree shifts for tracking during post-command processing.
1108 Intended for use on `allout-structure-shifted-hook'.
1110 SHIFTED-AMOUNT is the depth change and AT is the point at the start of the
1111 subtree that's been shifted.
1113 Records changes in `allout-widgets-changes-record'."
1114 (push (list :shifted shifted-amount at) allout-widgets-changes-record))
1115 ;;;_ > allout-widgets-shifts-processor (changes)
1116 (defun allout-widgets-shifts-processor (changes)
1117 "Widgetize and adjust items tracking allout outline structure additions.
1119 Dispatched by `allout-widgets-post-command-business' in response to
1120 :shifted entries recorded by `allout-widgets-shifts-recorder'."
1122 (dolist (change changes)
1123 (goto-char (caddr change))
1125 (allout-redecorate-visible-subtree))))
1126 ;;;_ > allout-widgets-after-copy-or-kill-function ()
1127 (defun allout-widgets-after-copy-or-kill-function ()
1128 "Do allout-widgets processing of text just placed in the kill ring.
1130 Intended for use on allout-after-copy-or-kill-hook."
1132 (setcar kill-ring (allout-widgets-undecorate-text (car kill-ring)))))
1134 ;;;_ > allout-widgets-exposure-undo-recorder (widget from-state)
1135 (defun allout-widgets-exposure-undo-recorder (widget)
1136 "Record outline exposure undo for tracking during post-command processing.
1138 Intended for use by `allout-graphics-modification-handler'.
1140 WIDGET is the widget being changed.
1142 Records changes in `allout-widgets-changes-record'."
1143 ;; disregard the events if we're currently processing them.
1144 (if (not allout-undo-exposure-in-progress)
1145 (push widget allout-widgets-undo-exposure-record)))
1146 ;;;_ > allout-widgets-exposure-undo-processor ()
1147 (defun allout-widgets-exposure-undo-processor ()
1148 "Adjust items tracking undo of allout outline structure exposure.
1150 Dispatched by `allout-widgets-post-command-business' in response to
1151 :undone-exposure entries recorded by `allout-widgets-exposure-undo-recorder'."
1152 (let* ((allout-undo-exposure-in-progress t)
1153 ;; inhibit undo recording while twiddling exposure to track undo:
1154 (widgets allout-widgets-undo-exposure-record)
1155 widget widget-start-marker widget-end-marker
1156 from-state icon-start-point to-state
1158 (setq allout-widgets-undo-exposure-record nil)
1160 (dolist (widget widgets)
1161 (setq widget-start-marker (widget-get widget :from)
1162 widget-end-marker (widget-get widget :to)
1163 from-state (widget-get widget :icon-state)
1164 icon-start-point (widget-apply widget :actual-position
1166 to-state (get-text-property icon-start-point
1168 (setq handled (allout-range-overlaps widget-start-marker
1171 covered (car handled)
1172 handled (cadr handled))
1174 (goto-char (widget-get widget :from))
1175 (when (not (allout-hidden-p))
1176 ;; adjust actual exposure to that of to-state viz from-state
1177 (cond ((and (eq to-state 'closed) (eq from-state 'opened))
1178 (allout-hide-current-subtree)
1179 (allout-decorate-item-and-context widget))
1180 ((and (eq to-state 'opened) (eq from-state 'closed))
1183 (expose-to (allout-chart-exposure-contour-by-icon))
1184 (goto-char expose-to)
1185 (allout-show-to-offshoot)))))))))))
1186 ;;;_ > allout-chart-exposure-contour-by-icon (&optional from-depth)
1187 (defun allout-chart-exposure-contour-by-icon (&optional from-depth)
1188 "Return points of subtree items to which exposure should be extended.
1190 The qualifying items are ones with a widget icon that is in the closed or
1191 empty state, or items with undecorated subitems.
1193 The resulting list of points is in reverse order.
1195 Optional FROM-DEPTH is for internal use."
1196 ;; During internal recursion, we return a pair: (at-end . result)
1197 ;; Otherwise we just return the result.
1198 (let ((from-depth from-depth)
1204 (setq level-depth (allout-depth))
1205 ;; at containing item:
1206 (setq start-point (point))
1207 (setq from-depth (allout-depth))
1208 (setq at-end (not (allout-next-heading))
1209 level-depth allout-recent-depth))
1211 ;; traverse the level, recursing on deeper levels:
1212 (while (and (not at-end)
1213 (> allout-recent-depth from-depth)
1214 (setq this-widget (allout-get-item-widget)))
1215 (if (< level-depth allout-recent-depth)
1218 (setq subgot (allout-chart-exposure-contour-by-icon level-depth)
1220 subgot (cdr subgot))
1221 (if subgot (setq got (append subgot got))))
1222 ;; progress at this level:
1223 (when (memq (widget-get this-widget :icon-state) '(closed empty))
1225 (allout-end-of-subtree))
1226 (setq at-end (not (allout-next-heading)))))
1228 ;; tailor result depending on whether or not we're a recursion:
1229 (if (not start-point)
1231 (goto-char start-point)
1233 ;;;_ > allout-range-overlaps (from to ranges)
1234 (defun allout-range-overlaps (from to ranges)
1235 "Return a pair indicating overlap of FROM and TO subtree range in RANGES.
1237 First element of result indicates whether candadate range FROM, TO
1238 overlapped any of the existing ranges.
1240 Second element of result is a new version of RANGES incorporating the
1241 candidate range with overlaps consolidated.
1243 FROM and TO must be in increasing order, as must be the pairs in RANGES."
1244 ;; to append to the end: (rplacd next-to-last-cdr (list 'f))
1247 ;; the start of the range that includes the candidate from:
1249 ;; the end of the range that includes the candidate to:
1251 ;; the candidates were inserted:
1253 (while (and ranges (not done))
1254 (setq entry (car ranges)
1255 ranges (cdr ranges))
1260 ;; some entry included the candidate from.
1261 (cond ((> (car entry) to)
1262 ;; current entry exceeds end of candidate range - done.
1263 (push (list included-from to) new-ranges)
1264 (push entry new-ranges)
1265 (setq included-to to
1267 ((>= (cadr entry) to)
1268 ;; current entry includes end of candidate range - done.
1269 (push (list included-from (cadr entry)) new-ranges)
1270 (setq included-to (cadr entry)
1272 ;; current entry contained in candidate range - ditch, continue:
1276 ;; current entry start exceeds candidate end - done, placed as new entry
1277 (push (list from to) new-ranges)
1278 (push entry new-ranges)
1279 (setq included-to to
1282 ((>= (car entry) from)
1283 ;; current entry start is above candidate start, but not above
1284 ;; candidate end (by prior case).
1285 (setq included-from from)
1286 ;; now we have to check on whether this entry contains to, or continue:
1287 (when (>= (cadr entry) to)
1288 ;; current entry contains only candidate end - done:
1289 (push (list included-from (cadr entry)) new-ranges)
1290 (setq included-to (cadr entry)
1292 ;; otherwise, we will continue to look for placement of candidate end.
1295 ((>= (cadr entry) to)
1296 ;; current entry properly contains candidate range.
1297 (push entry new-ranges)
1298 (setq included-from (car entry)
1299 included-to (cadr entry)
1302 ((>= (cadr entry) from)
1303 ;; current entry contains start of candidate range.
1304 (setq included-from (car entry)))
1307 ;; current entry is below the candidate range.
1308 (push entry new-ranges))))
1310 (cond ((and included-from included-to)
1311 ;; candidates placed.
1313 ((not (or included-from included-to))
1314 ;; candidates found no place, must be at the end:
1315 (push (list from to) new-ranges))
1317 ;; candidate start placed but end not:
1318 (push (list included-from to) new-ranges))
1319 ;; might be included-to and not included-from, indicating new entry.
1321 (setq new-ranges (nreverse new-ranges))
1322 (if ranges (setq new-ranges (append new-ranges ranges)))
1323 (list (if included-from t) new-ranges)))
1324 ;;;_ > allout-test-range-overlaps ()
1325 (defun allout-test-range-overlaps ()
1326 "allout-range-overlaps unit tests."
1329 (try (lambda (from to)
1330 (setq got (allout-range-overlaps from to ranges))
1331 (setq ranges (cadr got))
1334 ;; (setq ranges nil)
1335 ;; ;; ~ .02 to .1 seconds for just repeated listing args instead of funcall
1336 ;; ;; ~ 13 seconds for doing repeated funcall
1337 ;; (message "time-trial: %s, resulting size %s"
1339 ;; '(let ((size 10000)
1342 ;; (dotimes (count size)
1343 ;; (setq doing (random size))
1344 ;; (funcall try doing (+ doing (random 5)))
1345 ;; ;;(list doing (+ doing (random 5)))
1352 (assert (equal (funcall try 3 5) '(nil ((3 5)))))
1353 ;; add range at end:
1354 (assert (equal (funcall try 10 12) '(nil ((3 5) (10 12)))))
1355 ;; add range at beginning:
1356 (assert (equal (funcall try 1 2) '(nil ((1 2) (3 5) (10 12)))))
1357 ;; insert range somewhere in the middle:
1358 (assert (equal (funcall try 7 9) '(nil ((1 2) (3 5) (7 9) (10 12)))))
1359 ;; consolidate some:
1360 (assert (equal (funcall try 5 8) '(t ((1 2) (3 9) (10 12)))))
1362 (assert (equal (funcall try 15 17) '(nil ((1 2) (3 9) (10 12) (15 17)))))
1364 (assert (equal (funcall try 20 22)
1365 '(nil ((1 2) (3 9) (10 12) (15 17) (20 22)))))
1367 (assert (equal (funcall try 4 11) '(t ((1 2) (3 12) (15 17) (20 22)))))
1369 (assert (equal (funcall try 2 25) '(t ((1 25)))))
1373 (assert (equal (funcall try 20 25) '(nil ((20 25)))))
1374 (assert (equal (funcall try 30 35) '(nil ((20 25) (30 35)))))
1375 (assert (equal (funcall try 26 28) '(nil ((20 25) (26 28) (30 35)))))
1376 (assert (equal (funcall try 15 20) '(t ((15 25) (26 28) (30 35)))))
1377 (assert (equal (funcall try 10 30) '(t ((10 35)))))
1378 (assert (equal (funcall try 5 6) '(nil ((5 6) (10 35)))))
1379 (assert (equal (funcall try 2 100) '(t ((2 100)))))
1383 ;;;_ > allout-widgetize-buffer (&optional doing)
1384 (defun allout-widgetize-buffer (&optional doing)
1385 "EXAMPLE FUNCTION. Widgetize items in buffer using allout-chart-subtree.
1387 We economize by just focusing on the first of local-maximum depth siblings.
1389 Optional DOING is for internal use - a chart of the current level, for
1390 recursive operation."
1396 (goto-char (point-min))
1397 ;; Construct the chart by scanning the siblings:
1398 (dolist (top-level-sibling (allout-chart-siblings))
1399 (goto-char top-level-sibling)
1400 (let ((subchart (allout-chart-subtree)))
1402 (allout-widgetize-buffer subchart)))))
1404 ;; save-excursion was done on recursion entry, not necessary here.
1405 (let (have-sublists)
1406 (dolist (sibling doing)
1407 (when (listp sibling)
1408 (setq have-sublists t)
1409 (allout-widgetize-buffer sibling)))
1410 (when (and (not have-sublists) (not (widget-at (car doing))))
1411 (goto-char (car doing))
1412 (allout-get-or-create-item-widget)))))
1414 ;;;_ : Item widget and constructors
1416 ;;;_ $ allout-item-widget
1417 (define-widget 'allout-item-widget 'default
1418 "A widget presenting an allout outline item."
1421 ;; widget-field-at respects this to get item if 'field is unused.
1422 ;; we don't use field to avoid collision with end-of-line, etc, on which
1429 ;; tailor the widget for a specific item
1430 :create 'allout-decorate-item-and-context
1431 :value-delete 'allout-widgets-undecorate-item
1432 ;; Not Yet Converted (from original, tree-widget stab)
1433 :expander 'allout-tree-event-dispatcher ; get children when nil :args
1434 :expander-p 'identity ; always engage the :expander
1435 :action 'allout-tree-widget-action
1436 ;; :notify "when item changes"
1438 ;; force decoration of item but not context, unless already done this tick:
1439 :redecorate 'allout-redecorate-item
1440 :last-decorated-tick nil
1441 ;; recognize the actual situation of the item's text:
1442 :parse-item 'allout-parse-item-at-point
1443 ;; decorate the entirety of the item, sans offspring:
1444 :decorate-item-span 'allout-decorate-item-span
1445 ;; decorate the various item elements:
1446 :decorate-guides 'allout-decorate-item-guides
1447 :decorate-icon 'allout-decorate-item-icon
1448 :decorate-cue 'allout-decorate-item-cue
1449 :decorate-body 'allout-decorate-item-body
1450 :actual-position 'allout-item-actual-position
1452 ;; Layout parameters:
1453 :is-container nil ; is this actually the encompassing file/connection?
1455 :from nil ; item beginning - marker
1456 :to nil ; item end - marker
1457 :span-overlay nil ; overlay by which actual postion is determined
1459 ;; also serves as guide-end:
1462 :distinctive-start nil
1463 ;; also serves as cue-start:
1464 :distinctive-end nil
1465 ;; also serves as cue-end:
1470 :was-has-subitems 'init
1476 :does-encrypt nil ; pending encryption when :is-encrypted false.
1479 ;; the actual location of the item text:
1480 :location 'allout-item-location
1482 :button-keymap allout-item-icon-keymap ; XEmacs
1483 :keymap allout-item-icon-keymap ; Emacs
1493 :body-brevity-p 'allout-body-brevity-p
1495 ;; :guide-column-flags indicate (in reverse order) whether or not the
1496 ;; item's ancestor at the depth corresponding to the column has a
1497 ;; subsequent sibling - ie, whether or not the corresponding column needs
1498 ;; a descender line to connect that ancestor with its sibling.
1499 :guide-column-flags nil
1500 :was-guide-column-flags 'init
1502 ;; ie, has subitems:
1503 :populous-p 'allout-item-populous-p
1504 :help-echo 'allout-tree-widget-help-echo
1506 ;;;_ > allout-new-item-widget ()
1507 (defsubst allout-new-item-widget ()
1508 "create a new item widget, not yet situated anywhere."
1509 (if allout-widgets-maintain-tally
1510 ;; all the extra overhead is incurred only when doing the
1511 ;; maintenance, except the condition, which can't be avoided.
1512 (let ((widget (widget-convert 'allout-item-widget)))
1513 (puthash widget nil allout-widgets-tally)
1515 (widget-convert 'allout-item-widget)))
1516 ;;;_ : Item decoration
1517 ;;;_ > allout-decorate-item-and-context (item-widget &optional redecorate
1518 ;;; blank-container parent)
1519 (defun allout-decorate-item-and-context (item-widget &optional redecorate
1520 blank-container parent)
1521 "Create or adjust widget decorations for ITEM-WIDGET and neighbors at point.
1523 The neighbors include its siblings and parent.
1525 ITEM-WIDGET can be a created or converted allout-item-widget.
1527 If you're only trying to get or create a widget for an item, use
1528 `allout-get-or-create-item-widget'. If you have the item-widget, applying
1529 :redecorate will do the right thing.
1531 Optional BLANK-CONTAINER is for internal use. It is used to fabricate a
1532 container widget for an empty-bodied container, in the course of decorating
1533 a proper \(non-container\) item which starts at the beginning of the file.
1535 Optional REDECORATE causes redecoration of the item-widget and
1536 its siblings, even if already decorated in this cycle of the command loop.
1538 Optional PARENT, when provided, bypasses some navigation and computation
1539 necessary to obtain the parent of the items being processed.
1541 We return the item-widget corresponding to the item at point."
1543 (when (or redecorate
1544 (not (equal (widget-get item-widget :last-decorated-tick)
1545 allout-command-counter)))
1546 (let* ((allout-inhibit-body-modification-hook t)
1547 (was-modified (buffer-modified-p))
1550 (is-container (or blank-container
1551 (not (setq prefix-start (allout-goto-prefix)))
1552 (< was-point prefix-start)))
1553 ;; steady-point (set in two steps) is reliable across parent
1555 (steady-point (progn (if is-container (goto-char 1))
1557 (steady-point (progn (set-marker-insertion-type steady-point t)
1559 (parent (and (not is-container)
1560 (allout-get-or-create-parent-widget)))
1561 parent-flags parent-depth
1567 reverse-siblings-chart
1568 (buffer-undo-list t))
1570 ;; At this point the parent is decorated and parent-flags indicate
1571 ;; its guide lines. We will iterate over the siblings according to a
1572 ;; chart we create at the start, and going from last to first so we
1573 ;; don't have to worry about text displacement caused by widgetizing.
1576 (progn (widget-put item-widget :is-container t)
1577 (setq reverse-siblings-chart (list 1)))
1578 (goto-char (widget-apply parent :actual-position :from))
1579 (if (widget-get parent :is-container)
1580 ;; `allout-goto-prefix' will go to first non-container item:
1581 (allout-goto-prefix)
1582 (allout-next-heading))
1583 (setq depth (allout-recent-depth))
1584 (setq reverse-siblings-chart (list allout-recent-prefix-beginning))
1585 (while (allout-next-sibling)
1586 (push allout-recent-prefix-beginning reverse-siblings-chart)))
1588 (dolist (doing-at reverse-siblings-chart)
1589 (goto-char doing-at)
1590 (when allout-widgets-track-decoration
1593 (setq doing-item (if (= doing-at steady-point)
1595 (or (allout-get-item-widget)
1596 (allout-new-item-widget))))
1598 (when (or redecorate (not (equal (widget-get doing-item
1599 :last-decorated-tick)
1600 allout-command-counter)))
1601 (widget-apply doing-item :parse-item t blank-container)
1602 (widget-apply doing-item :decorate-item-span)
1604 (widget-apply doing-item :decorate-guides
1605 parent (and successor-sibling t))
1606 (widget-apply doing-item :decorate-icon)
1607 (widget-apply doing-item :decorate-cue)
1608 (widget-apply doing-item :decorate-body)
1610 (widget-put doing-item :last-decorated-tick allout-command-counter))
1612 (setq successor-sibling doing-at))
1614 (set-buffer-modified-p was-modified)
1615 (goto-char steady-point)
1616 ;; must null the marker or the buffer gets clogged with impedence:
1617 (set-marker steady-point nil)
1620 ;;;_ > allout-redecorate-item (item)
1621 (defun allout-redecorate-item (item-widget)
1622 "Resituate ITEM-WIDGET decorations, disregarding context.
1624 Use this to redecorate only the item, when you know that it's
1625 situation with respect to siblings, parent, and offspring is
1626 unchanged from its last decoration. Use
1627 `allout-decorate-item-and-context' instead to reassess and adjust
1628 relevent context, when suitable."
1629 (if (not (equal (widget-get item-widget :last-decorated-tick)
1630 allout-command-counter))
1631 (let ((was-modified (buffer-modified-p))
1632 (buffer-undo-list t))
1633 (widget-apply item-widget :parse-item)
1634 (widget-apply item-widget :decorate-guides)
1635 (widget-apply item-widget :decorate-icon)
1636 (widget-apply item-widget :decorate-cue)
1637 (widget-apply item-widget :decorate-body)
1638 (set-buffer-modified-p was-modified))))
1639 ;;;_ > allout-redecorate-visible-subtree (&optional parent-widget
1641 (defun allout-redecorate-visible-subtree (&optional parent-widget depth chart)
1642 "Redecorate all visible items in subtree at point.
1644 Optional PARENT-WIDGET is for optimization, when the parent
1645 widget is already available.
1647 Optional DEPTH restricts the excursion depth of covered.
1649 Optional CHART is for internal recursion, to carry a chart of the
1652 Point is left at the last sibling in the visible subtree."
1653 ;; using a treatment that takes care of all the siblings on a level, we
1654 ;; only need apply it to the first sibling on the level, and we can
1655 ;; collect and pass the parent of the lower levels to recursive calls as
1657 (let ((parent-widget
1658 (if (and parent-widget (widget-apply parent-widget
1659 :actual-position :from))
1660 (progn (goto-char (widget-apply parent-widget
1661 :actual-position :from))
1663 (let ((got (allout-get-item-widget)))
1665 (allout-decorate-item-and-context got 'redecorate)
1666 (allout-get-or-create-item-widget 'redecorate)))))
1667 (pending-chart (or chart (allout-chart-subtree nil 'visible)))
1669 previous-sibling-point
1671 recent-sibling-point)
1672 (setq pending-chart (nreverse pending-chart))
1673 (dolist (sibling-point pending-chart)
1674 (cond ((integerp sibling-point)
1675 (when (not previous-sibling-point)
1676 (goto-char sibling-point)
1677 (if (setq item-widget (allout-get-item-widget nil))
1678 (allout-decorate-item-and-context item-widget 'redecorate
1680 (allout-get-or-create-item-widget)))
1681 (setq previous-sibling-point sibling-point
1682 recent-sibling-point sibling-point))
1683 ((listp sibling-point)
1686 (allout-redecorate-visible-subtree
1687 (if (not previous-sibling-point)
1688 ;; containment discontinuity - sigh
1690 (allout-get-or-create-item-widget 'redecorate))
1691 (if depth (1- depth))
1693 (if (and recent-sibling-point (< (point) recent-sibling-point))
1694 (goto-char recent-sibling-point))))
1695 ;;;_ > allout-parse-item-at-point (item-widget &optional at-beginning
1696 ;;; blank-container)
1697 (defun allout-parse-item-at-point (item-widget &optional at-beginning
1699 "Set widget ITEM-WIDGET layout parameters per item-at-point's actual layout.
1701 If optional AT-BEGINNING is t, then point is assumed to be at the start of
1704 If optional BLANK-CONTAINER is true, then the parameters of a container
1705 which has an empty body are set. \(Though the body is blank, the object
1706 may have subitems.\)"
1708 ;; Uncomment this sit-for to notice where decoration is happening:
1710 (let* ((depth (allout-depth))
1711 (depth (if blank-container 0 depth))
1712 (is-container (or blank-container (zerop depth)))
1714 (does-encrypt (and (not is-container)
1715 (allout-encrypted-type-prefix)))
1716 (is-encrypted (and does-encrypt (allout-encrypted-topic-p)))
1717 (icon-end allout-recent-prefix-end)
1718 (icon-start (1- icon-end))
1723 (contents-depth (1+ depth))
1725 (widget-put item-widget :depth depth)
1729 (widget-put item-widget :from (allout-set-boundary-marker
1731 (widget-get item-widget :from)))
1732 (widget-put item-widget :icon-end nil)
1733 (widget-put item-widget :icon-start nil)
1734 (setq body-start (widget-put item-widget :body-start 1)))
1738 (widget-put item-widget :from (allout-set-boundary-marker
1739 :from (if at-beginning
1741 allout-recent-prefix-beginning)
1742 (widget-get item-widget :from)))
1743 (widget-put item-widget :icon-start icon-start)
1744 (widget-put item-widget :icon-end icon-end)
1746 (widget-put item-widget :does-encrypt t)
1747 (widget-put item-widget :is-encrypted is-encrypted))
1750 (setq body-start icon-end)
1751 (widget-put item-widget :bullet (setq bullet (allout-get-bullet)))
1752 (if (equal (char-after body-start) ? )
1753 (setq body-start (1+ body-start)))
1754 (widget-put item-widget :body-start body-start)
1757 ;; Both container and regular items:
1759 ;; :body-end (doesn't include a trailing blank line, if any) -
1760 (widget-put item-widget :body-end (setq body-end
1763 (allout-end-of-entry))))
1765 (widget-put item-widget :to (allout-set-boundary-marker
1766 :to (if blank-container
1768 (or (allout-pre-next-prefix)
1769 (goto-char (point-max))))
1770 (widget-get item-widget :to)))
1771 (widget-put item-widget :has-subitems
1774 ;; has a subsequent item:
1775 (not (= body-end (point-max)))
1776 ;; subsequent item is deeper:
1777 (< depth (setq contents-depth (allout-recent-depth))))))
1778 ;; note :expanded - true if widget item's content is currently visible?
1779 (widget-put item-widget :expanded
1781 ;; subsequent item is or isn't visible:
1783 (goto-char allout-recent-prefix-beginning)
1784 (not (allout-hidden-p)))))))
1785 ;;;_ > allout-set-boundary-marker (boundary position &optional current-marker)
1786 (defun allout-set-boundary-marker (boundary position &optional current-marker)
1787 "Set or create item widget BOUNDARY type marker at POSITION.
1789 Optional CURRENT-MARKER is the marker currently being used for
1790 the boundary, if any.
1792 BOUNDARY type is either :from or :to, determining the marker insertion type."
1793 (if (not position) (setq position (point)))
1795 (set-marker current-marker position)
1796 (let ((marker (make-marker)))
1797 ;; XXX dang - would like for :from boundary to advance after inserted
1798 ;; text, but that would omit new header prefixes when allout
1799 ;; relevels, etc. this competes with ad-hoc edits, which would
1800 ;; better be omitted
1801 (set-marker-insertion-type marker nil)
1802 (set-marker marker position))))
1803 ;;;_ > allout-decorate-item-span (item-widget)
1804 (defun allout-decorate-item-span (item-widget)
1805 "Equip the item with a span, as an entirety.
1807 This span is implemented so it can be used to detect displacement
1808 of the widget in absolute terms, and provides an offset bias for
1809 the various element spans."
1811 (if (and (widget-get item-widget :is-container)
1812 ;; the only case where the span could be empty.
1813 (eq (widget-get item-widget :from)
1814 (widget-get item-widget :to)))
1816 (allout-item-span item-widget
1817 (widget-get item-widget :from)
1818 (widget-get item-widget :to))))
1819 ;;;_ > allout-decorate-item-guides (item-widget
1820 ;;; &optional parent-widget has-successor)
1821 (defun allout-decorate-item-guides (item-widget
1822 &optional parent-widget has-successor)
1823 "Add ITEM-WIDGET guide icon-prefix descender and connector text properties.
1825 Optional arguments provide context for deriving the guides. In
1826 their absence, the current guide column flags are used.
1828 Optional PARENT-WIDGET is the widget for the item's parent item.
1830 Optional HAS-SUCCESSOR is true iff the item is followed by a sibling.
1832 We also hide the header-prefix string.
1834 Guides are established according to the item-widget's :guide-column-flags,
1835 when different than :was-guide-column-flags. Changing that property and
1836 reapplying this method will rectify the glyphs."
1838 (when (not (widget-get item-widget :is-container))
1839 (let* ((depth (widget-get item-widget :depth))
1840 (parent-depth (and parent-widget
1841 (widget-get parent-widget :depth)))
1842 (parent-flags (and parent-widget
1843 (widget-get parent-widget :guide-column-flags)))
1844 (parent-flags-depth (length parent-flags))
1845 (extender-length (- depth (+ parent-flags-depth 2)))
1846 (flags (or (and (> depth 1)
1848 (widget-put item-widget :guide-column-flags
1849 (append (list has-successor)
1850 (if (< 0 extender-length)
1851 (make-list extender-length
1854 (widget-get item-widget :guide-column-flags)))
1855 (was-flags (widget-get item-widget :was-guide-column-flags))
1856 (guides-start (widget-get item-widget :from))
1857 (guides-end (widget-get item-widget :icon-start))
1858 (position guides-start)
1859 (increment (length allout-header-prefix))
1862 extenders paint-extenders
1863 (inhibit-read-only t))
1865 (when (not (equal was-flags flags))
1867 (setq reverse-flags (reverse flags))
1868 (while reverse-flags
1870 (cond ((null (cdr reverse-flags))
1871 (if (car reverse-flags)
1874 ((eq (car reverse-flags) '-)
1875 ;; accumulate extenders tally, to be painted on next
1876 ;; non-extender flag, according to the flag type.
1877 (setq extenders (1+ (or extenders 0)))
1879 ((car reverse-flags)
1881 (t 'skip-descender)))
1883 (put-text-property position (setq position (+ position increment))
1884 'display (allout-fetch-icon-image guide-name))
1885 (if (> increment 1) (setq increment 1))
1887 ;; paint extenders after a connector, else leave spaces.
1888 (dotimes (i extenders)
1890 position (setq position (1+ position))
1891 'display (allout-fetch-icon-image
1892 (if (memq guide-name '(mid-connector end-connector))
1895 (setq extenders nil)))
1896 (setq reverse-flags (cdr reverse-flags)))
1897 (widget-put item-widget :was-guide-column-flags flags))
1899 (allout-item-element-span-is item-widget :guides-span
1900 guides-start guides-end))))
1901 ;;;_ > allout-decorate-item-icon (item-widget)
1902 (defun allout-decorate-item-icon (item-widget)
1903 "Add item icon glyph and distinctive bullet text properties to ITEM-WIDGET."
1905 (when (not (widget-get item-widget :is-container))
1906 (let* ((icon-start (widget-get item-widget :icon-start))
1907 (icon-end (widget-get item-widget :icon-end))
1908 (bullet (widget-get item-widget :bullet))
1910 (was-bullet (widget-get item-widget :was-bullet))
1911 (distinctive (allout-distinctive-bullet bullet))
1912 (distinctive-start (widget-get item-widget :distinctive-start))
1913 (distinctive-end (widget-get item-widget :distinctive-end))
1914 (does-encrypt (widget-get item-widget :does-encrypt))
1915 (is-encrypted (and does-encrypt (widget-get item-widget
1917 (expanded (widget-get item-widget :expanded))
1918 (has-subitems (widget-get item-widget :has-subitems))
1919 (inhibit-read-only t)
1922 (when (not (and (equal (widget-get item-widget :was-expanded) expanded)
1923 (equal (widget-get item-widget :was-has-subitems)
1925 (equal (widget-get item-widget :was-does-encrypt)
1927 (equal (widget-get item-widget :was-is-encrypted)
1931 (cond (does-encrypt (if is-encrypted
1933 'unlocked-encrypted))
1935 (has-subitems 'closed)
1937 (put-text-property icon-start (1+ icon-start)
1938 'display (allout-fetch-icon-image icon-state))
1939 (widget-put item-widget :was-expanded expanded)
1940 (widget-put item-widget :was-has-subitems has-subitems)
1941 (widget-put item-widget :was-does-encrypt does-encrypt)
1942 (widget-put item-widget :was-is-encrypted is-encrypted)
1943 ;; preserve as a widget property to track last known:
1944 (widget-put item-widget :icon-state icon-state)
1945 ;; preserve as a text property to track undo:
1946 (put-text-property icon-start icon-end :icon-state icon-state))
1947 (allout-item-element-span-is item-widget :icon-span
1948 icon-start icon-end)
1949 (when (not (string= was-bullet bullet))
1950 (cond ((not distinctive)
1951 ;; XXX we strip the prior properties without even checking if
1952 ;; the prior bullet was distinctive, because the widget
1953 ;; provisions to convey that info is disappearing, sigh.
1954 (remove-text-properties icon-end (1+ icon-end) '(display))
1955 (setq distinctive-start icon-end distinctive-end icon-end)
1956 (widget-put item-widget :distinctive-start distinctive-start)
1957 (widget-put item-widget :distinctive-end distinctive-end))
1959 ((not (string= bullet allout-numbered-bullet))
1960 (setq distinctive-start icon-end distinctive-end (+ icon-end 1)))
1963 (setq distinctive-start icon-end distinctive-end (+ icon-end 1)))
1966 (goto-char icon-end)
1967 (looking-at "[0-9]+")
1968 (setq use-bullet (buffer-substring icon-end (match-end 0)))
1969 (setq distinctive-start icon-end
1970 distinctive-end (match-end 0))))
1971 (put-text-property distinctive-start distinctive-end 'display
1973 (widget-put item-widget :was-bullet bullet)
1974 (widget-put item-widget :distinctive-start distinctive-start)
1975 (widget-put item-widget :distinctive-end distinctive-end)))))
1976 ;;;_ > allout-decorate-item-cue (item-widget)
1977 (defun allout-decorate-item-cue (item-widget)
1978 "Incorporate space between bullet icon and body to the ITEM-WIDGET."
1979 ;; NOTE: most of the cue-area
1981 (when (not (widget-get item-widget :is-container))
1982 (let* ((cue-start (or (widget-get item-widget :distinctive-end)
1983 (widget-get item-widget :icon-end)))
1984 (body-start (widget-get item-widget :body-start))
1985 (expanded (widget-get item-widget :expanded))
1986 (has-subitems (widget-get item-widget :has-subitems))
1987 (inhibit-read-only t))
1989 (allout-item-element-span-is item-widget :cue-span cue-start body-start)
1990 (put-text-property (1- body-start) body-start 'rear-nonsticky t))))
1991 ;;;_ > allout-decorate-item-body (item-widget &optional force)
1992 (defun allout-decorate-item-body (item-widget &optional force)
1993 "Incorporate item body text as part the ITEM-WIDGET.
1995 Optional FORCE means force reassignment of the region property."
1997 (let* ((allout-inhibit-body-modification-hook t)
1998 (body-start (widget-get item-widget :body-start))
1999 (body-end (widget-get item-widget :body-end))
2000 (body-text-end body-end)
2001 (inhibit-read-only t))
2003 (allout-item-element-span-is item-widget :body-span
2004 body-start (min (1+ body-end) (point-max))
2006 ;;;_ > allout-item-actual-position (item-widget field)
2007 (defun allout-item-actual-position (item-widget field)
2008 "Return ITEM-WIDGET FIELD position taking item displacement into account."
2010 ;; The item's sub-element positions (:icon-end, :body-start, etc) are
2011 ;; accurate when the item is parsed, but some offsets from the start
2012 ;; drift with text added in the body.
2014 ;; Rather than reparse an item with every change (inefficient), or derive
2015 ;; every position from a distinct field marker/overlay (prohibitive as
2016 ;; the number of items grows), we use the displacement tracking of the
2017 ;; :span-overlay's markers, against the registered :from or :body-end
2018 ;; (depending on whether the requested field value is before or after the
2019 ;; item body), to bias the registered values.
2021 ;; This is not necessary/useful when the item is being decorated, because
2022 ;; that always must be preceded by a fresh item parse.
2024 (if (not (eq field :body-end))
2025 (widget-get item-widget :from)
2027 (let* ((span-overlay (widget-get item-widget :span-overlay))
2028 (body-end-position (widget-get item-widget :body-end))
2029 (ref-marker-position (and span-overlay
2030 (overlay-end span-overlay)))
2031 (offset (and body-end-position span-overlay
2032 (- (or ref-marker-position 0)
2033 body-end-position))))
2034 (+ (widget-get item-widget field) (or offset 0)))))
2035 ;;;_ : Item undecoration
2036 ;;;_ > allout-widgets-undecorate-region (start end)
2037 (defun allout-widgets-undecorate-region (start end)
2038 "Eliminate widgets and decorations for all items in region from START to END."
2043 (while (< (setq next (next-single-char-property-change next
2049 (when (setq widget (allout-get-item-widget))
2050 ;; if the next-property/overly progression got us to a widget:
2051 (allout-widgets-undecorate-item widget t))))))
2052 ;;;_ > allout-widgets-undecorate-text (text)
2053 (defun allout-widgets-undecorate-text (text)
2054 "Eliminate widgets and decorations for all items in TEXT."
2055 (remove-text-properties 0 (length text)
2056 '(display nil :icon-state nil rear-nonsticky nil
2057 category nil button nil field nil)
2060 ;;;_ > allout-widgets-undecorate-item (item-widget &optional no-expose)
2061 (defun allout-widgets-undecorate-item (item-widget &optional no-expose)
2062 "Remove widget decorations from ITEM-WIDGET.
2064 Any concealed content head lines and item body is exposed, unless
2065 optional NO-EXPOSE is non-nil."
2066 (let ((from (widget-get item-widget :from))
2067 (to (widget-get item-widget :to))
2068 (text-properties-to-remove '(display nil
2074 (span-overlay (widget-get item-widget :span-overlay))
2075 (button-overlay (widget-get item-widget :button))
2076 (was-modified (buffer-modified-p))
2077 (buffer-undo-list t)
2078 (inhibit-read-only t))
2080 (allout-flag-region from to nil))
2082 (remove-text-properties from to text-properties-to-remove))
2084 (delete-overlay span-overlay) (widget-put item-widget :span-overlay nil))
2085 (when button-overlay
2086 (delete-overlay button-overlay) (widget-put item-widget :button nil))
2087 (set-marker from nil)
2089 (if (not was-modified)
2090 (set-buffer-modified-p nil))))
2092 ;;;_ : Item decoration support
2093 ;;;_ > allout-item-span (item-widget &optional start end)
2094 (defun allout-item-span (item-widget &optional start end)
2095 "Return or register the location of an ITEM-WIDGET's actual START and END.
2097 If START and END are not passed in, return either a dotted pair
2098 of the current span, if established, or nil if not yet set.
2100 When the START and END are passed, return the distance that the
2101 start of the item moved. We return 0 if the span was not
2102 previously established or is not moved."
2103 (let ((overlay (widget-get item-widget :span-overlay))
2106 (cond ((not overlay) (when start
2107 (setq overlay (make-overlay start end nil t nil))
2108 (overlay-put overlay 'button item-widget)
2109 (overlay-put overlay 'evaporate t)
2110 (widget-put item-widget :span-overlay overlay)
2113 ((not start) (cons (overlay-start overlay) (overlay-end overlay)))
2115 ((or (not (equal (overlay-start overlay) start))
2116 (not (equal (overlay-end overlay) end)))
2117 (move-overlay overlay start end)
2119 ;; specified span already set:
2121 ;;;_ > allout-item-element-span-is (item-widget element
2122 ;;; &optional start end force)
2123 (defun allout-item-element-span-is (item-widget element
2124 &optional start end force)
2125 "Return or register the location of the indicated ITEM-WIDGET ELEMENT.
2127 ELEMENT is one of :guides-span, :icon-span, :cue-span, or :body-span.
2129 When optional START is specified, optional END must also be.
2131 START and END are the actual bounds of the region, if provided.
2133 If START and END are not passed in, we return either a dotted
2134 pair of the current span, if established, or nil if not yet set.
2136 When the START and END are passed, we return t if the region
2137 changed or nil if not.
2139 Optional FORCE means force assignment of the region's text
2140 property, even if it's already set."
2141 (let ((span (widget-get item-widget element)))
2142 (cond ((or (not span) force)
2144 (widget-put item-widget element (cons start end))
2145 (put-text-property start end 'category
2147 allout-span-to-category)))
2151 ;; move if necessary:
2152 ((not (and (eq (car span) start)
2153 (eq (cdr span) end)))
2154 (widget-put item-widget element span)
2156 ;; specified span already set:
2158 ;;;_ : Item widget retrieval (/ high-level creation):
2159 ;;;_ > allout-get-item-widget (&optional container)
2160 (defun allout-get-item-widget (&optional container)
2161 "Return the widget for the item at point, or nil if no widget yet exists.
2163 Point must be situated *before* the start of the target item's
2164 body, so we don't get an existing containing item when we're in
2165 the process of creating an item in the middle of another.
2167 Optional CONTAINER is used to obtain the container item."
2168 (if (or container (zerop (allout-depth)))
2169 allout-container-item-widget
2170 ;; allout-recent-* are calibrated by (allout-depth) if we got here.
2171 (let ((got (widget-at allout-recent-prefix-beginning)))
2172 (if (and got (listp got))
2173 (if (marker-position (widget-get got :from))
2175 (>= (point) (widget-apply got :actual-position :from))
2176 (<= (point) (widget-apply got :actual-position :body-start))
2178 ;; a wacky residual item - undecorate and disregard:
2179 (allout-widgets-undecorate-item got)
2181 ;;;_ > allout-get-or-create-item-widget (&optional redecorate blank-container)
2182 (defun allout-get-or-create-item-widget (&optional redecorate blank-container)
2183 "Return a widget for the item at point, creating the widget if necessary.
2185 When creating a widget, we assume there has been a context change
2186 and decorate its siblings and parent, as well.
2188 Optional BLANK-CONTAINER is for internal use, to fabricate a
2189 meta-container item with an empty body when the first proper
2190 \(non-container\) item starts at the beginning of the file.
2192 Optional REDECORATE, if non-nil, means to redecorate the widget
2193 if it already exists."
2194 (let ((widget (allout-get-item-widget blank-container))
2195 (buffer-undo-list t))
2196 (cond (widget (if redecorate
2197 (allout-redecorate-item widget))
2199 ((or blank-container (zerop (allout-depth)))
2200 (or allout-container-item-widget
2201 (setq allout-container-item-widget
2202 (allout-decorate-item-and-context
2203 (widget-convert 'allout-item-widget)
2204 nil blank-container))))
2205 ;; create a widget for a regular/non-container item:
2206 (t (allout-decorate-item-and-context (widget-convert
2207 'allout-item-widget))))))
2208 ;;;_ > allout-get-or-create-parent-widget (&optional redecorate)
2209 (defun allout-get-or-create-parent-widget (&optional redecorate)
2210 "Return widget for parent of item at point, decorating it if necessary.
2212 We return the container widget if we're above the first proper item in the
2215 Optional REDECORATE, if non-nil, means to redecorate the widget if it
2218 Point will wind up positioned on the beginning of the parent or beginning
2220 ;; use existing widget, if there, else establish it
2221 (if (or (bobp) (and (not (allout-ascend))
2222 (looking-at allout-regexp)))
2223 (allout-get-or-create-item-widget redecorate 'blank-container)
2224 (allout-get-or-create-item-widget redecorate)))
2225 ;;;_ : X- Item ancillaries
2226 ;;;_ >X allout-body-modification-handler (beg end)
2227 (defun allout-body-modification-handler (beg end)
2228 "Do routine processing of body text before and after modification.
2230 Operation is inhibited by `allout-inhibit-body-modification-handler'."
2232 ;; The primary duties are:
2234 ;; - marking of escaped prefix-like text for delayed cleanup of escapes
2235 ;; - removal and replacement of the settings
2236 ;; - maintenance of beginning-of-line guide lines
2238 ;; ?? Escapes removal \(before changes\) is not done when edits span multiple
2239 ;; items, recognizing that item structure is being preserved, including
2240 ;; escaping of item-prefix-like text within bodies. See
2241 ;; `allout-before-modification-handler' and
2242 ;; `allout-inhibit-body-modification-handler'.
2244 ;; Adds the overlay to the `allout-unresolved-body-mod-workhash' during
2245 ;; before-change operation, and removes from that list during after-change
2247 (cond (allout-inhibit-body-modification-hook nil)))
2248 ;;;_ >X allout-graphics-modification-handler (beg end)
2249 (defun allout-graphics-modification-handler (beg end)
2250 "Protect against incoherent deletion of decoration graphics.
2252 Deletes allowed only when inhibit-read-only is t."
2254 (undo-in-progress (when (eq (get-text-property beg 'category)
2255 'allout-icon-span-category)
2258 (let* ((item-widget (allout-get-item-widget)))
2260 (allout-widgets-exposure-undo-recorder
2262 (inhibit-read-only t)
2263 ((not (and (boundp 'allout-mode) allout-mode)) t)
2264 ((equal this-command 'quoted-insert) t)
2265 ((yes-or-no-p "Unruly edit of outline structure - allow? ")
2266 (setq allout-widgets-unset-inhibit-read-only (not inhibit-read-only)
2267 inhibit-read-only t))
2269 (substitute-command-keys allout-structure-unruly-deletion-message)))))
2270 ;;;_ > allout-item-icon-key-handler ()
2271 (defun allout-item-icon-key-handler ()
2272 "Catchall handling of key bindings in item icon/cue hot-spots.
2274 Applies `allout-hotspot-key-handler' and calls the result, if any, as an
2275 interactive command."
2278 (let* ((mapped-binding (allout-hotspot-key-handler)))
2279 (when mapped-binding
2280 (call-interactively mapped-binding))))
2283 ;;;_ . allout-item-location (item-widget)
2284 (defun allout-item-location (item-widget)
2285 "Location of the start of the item's text."
2286 (overlay-start (widget-get item-widget :span-overlay)))
2288 ;;;_ : Icon management
2289 ;;;_ > allout-fetch-icon-image (name)
2290 (defun allout-fetch-icon-image (name)
2291 "Fetch allout icon for symbol NAME.
2293 We use a caching strategy, so the caller doesn't need to do so."
2294 (let* ((types allout-widgets-icon-types)
2295 (use-dir (if (equal (allout-frame-property nil 'background-mode)
2297 allout-widgets-icons-light-subdir
2298 allout-widgets-icons-dark-subdir))
2299 (key (list name use-dir))
2300 (got (assoc key allout-widgets-icons-cache)))
2302 ;; display system shows only first of subsequent adjacent
2303 ;; `eq'-identical repeats - use copies to avoid this problem.
2304 (allout-widgets-copy-list (cadr got))
2305 (while (and types (not got))
2308 (list (append (list :type (car types)
2309 :file (concat use-dir
2313 (if (featurep 'xemacs)
2314 allout-widgets-item-image-properties-xemacs
2315 allout-widgets-item-image-properties-emacs)
2317 (setq types (cdr types)))
2319 (push (list key got) allout-widgets-icons-cache))
2322 ;;;_ : Miscellaneous
2323 ;;;_ > allout-elapsed-time-seconds (triple)
2324 (defun allout-elapsed-time-seconds (end start)
2325 "Return seconds between `current-time' style time START/END triples."
2326 (let ((elapsed (time-subtract end start)))
2327 (+ (* (car elapsed) (expt 2.0 16))
2329 (/ (caddr elapsed) (expt 10.0 6)))))
2330 ;;;_ > allout-frame-property (frame property)
2331 (defalias 'allout-frame-property
2332 (cond ((fboundp 'frame-parameter)
2334 ((fboundp 'frame-property)
2337 ;;;_ > allout-find-image (specs)
2338 (defalias 'allout-find-image
2339 (if (fboundp 'find-image)
2341 nil) ; aka, not-yet-implemented for xemacs.
2343 ;;;_ > allout-widgets-copy-list (list)
2344 (defun allout-widgets-copy-list (list)
2345 ;; duplicated from cl.el 'copy-list' as of 2008-08-17
2346 "Return a copy of LIST, which may be a dotted list.
2347 The elements of LIST are not copied, just the list structure itself."
2350 (while (consp list) (push (pop list) res))
2351 (prog1 (nreverse res) (setcdr res list)))
2353 ;;;_ . allout-widgets-count-buttons-in-region (start end)
2354 (defun allout-widgets-count-buttons-in-region (start end)
2355 "Debugging/diagnostic tool - count overlays with 'button' property in region."
2357 (setq start (or start (point-min))
2358 end (or end (point-max)))
2359 (if (> start end) (let ((interim start)) (setq start end end interim)))
2360 (let ((button-overlays (delq nil
2361 (mapcar (function (lambda (o)
2362 (if (overlay-get o 'button)
2364 (overlays-in start end)))))
2365 (length button-overlays)))
2367 ;;;_ : Run unit tests:
2368 (defun allout-widgets-run-unit-tests ()
2369 (message "Running allout-widget tests...")
2371 (allout-test-range-overlaps)
2373 (message "Running allout-widget tests... Done.")
2376 (when allout-widgets-run-unit-tests-on-load
2377 (allout-widgets-run-unit-tests))
2380 (provide 'allout-widgets)
2382 ;;;_. Local emacs vars.
2383 ;;;_ , Local variables:
2384 ;;;_ , allout-layout: (-1 : 0)