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
(not allout-widgets-mode-inhibit
))
262 (format ":%s" (hash-table-count allout-widgets-tally
))))
263 ;;;_ = allout-widgets-track-decoration nil
264 (defcustom allout-widgets-track-decoration nil
265 "*If non-nil, show cursor position of each item decoration.
267 This is for debugging purposes, and generally set at need in a
268 buffer rather than as a prevailing configuration \(but it's handy
269 to publicize it by making it a customization variable\)."
271 :group
'allout-widgets-developer
)
272 (make-variable-buffer-local 'allout-widgets-track-decoration
)
274 ;;;_ : Mode context - variables, hookup, and hooks
275 ;;;_ . internal mode variables
276 ;;;_ , Mode activation and environment
277 ;;;_ = allout-widgets-version
278 (defvar allout-widgets-version
"1.0"
279 "Version of currently loaded allout-widgets extension.")
280 ;;;_ > allout-widgets-version
281 (defun allout-widgets-version (&optional here
)
282 "Return string describing the loaded outline version."
284 (let ((msg (concat "Allout Outline Widgets Extension v "
285 allout-widgets-version
)))
286 (if here
(insert msg
))
289 ;;;_ = allout-widgets-mode-inhibit
290 (defvar allout-widgets-mode-inhibit nil
291 "Inhibit `allout-widgets-mode' from activating widgets.
293 This also inhibits automatic adjustment of widgets to track allout outline
296 You can use this as a file local variable setting to disable
297 allout widgets enhancements in selected buffers while generally
298 enabling widgets by customizing `allout-widgets-auto-activation'.
300 In addition, you can invoked `allout-widgets-mode' allout-mode
301 buffers where this is set to enable and disable widget
302 enhancements, directly.")
304 (put 'allout-widgets-mode-inhibit
'safe-local-variable
305 (if (fboundp 'booleanp
) 'booleanp
(lambda (x) (member x
'(t nil
)))))
306 (make-variable-buffer-local 'allout-widgets-mode-inhibit
)
307 ;;;_ = allout-inhibit-body-modification-hook
308 (defvar allout-inhibit-body-modification-hook nil
309 "Override de-escaping of text-prefixes in item bodies during specific changes.
311 This is used by `allout-buffer-modification-handler' to signal such changes
312 to `allout-body-modification-handler', and is always reset by
313 `allout-post-command-business'.")
314 (make-variable-buffer-local 'allout-inhibit-body-modification-hook
)
315 ;;;_ = allout-widgets-icons-cache
316 (defvar allout-widgets-icons-cache nil
317 "Cache allout icon images, as an association list.
319 `allout-fetch-icon-image' uses this cache transparently, keying
320 images with lists containing the name of the icon directory \(as
321 found on the `load-path') and the icon name.
323 Set this variable to `nil' to empty the cache, and have it replenish from the
325 ;;;_ = allout-widgets-unset-inhibit-read-only
326 (defvar allout-widgets-unset-inhibit-read-only nil
327 "Tell `allout-widgets-post-command-business' to unset `inhibit-read-only'.
329 Used by `allout-graphics-modification-handler'")
330 ;;;_ = allout-widgets-reenable-before-change-handler
331 (defvar allout-widgets-reenable-before-change-handler nil
332 "Tell `allout-widgets-post-command-business' to reequip the handler.
334 Necessary because the handler sometimes deliberately raises an
335 error, causing it to be disabled.")
336 ;;;_ , State for hooks
337 ;;;_ = allout-unresolved-body-mod-workroster
338 (defvar allout-unresolved-body-mod-workroster
(make-hash-table :size
16)
339 "List of body-overlays that did before-change business but not after-change.
341 See `allout-post-command-business' and `allout-body-modification-handler'.")
342 ;;;_ = allout-structure-unruly-deletion-message
343 (defvar allout-structure-unruly-deletion-message
344 "Unruly edit prevented --
345 To change the bullet character: \\[allout-rebullet-current-heading]
346 To promote this item: \\[allout-shift-out]
347 To demote it: \\[allout-shift-in]
348 To delete it and offspring: \\[allout-kill-topic]
349 See \\[describe-mode] for many more options."
350 "Informative message presented on improper editing of outline structure.
352 The structure includes the guides lines, bullet, and bullet cue.")
353 ;;;_ = allout-widgets-changes-record
354 (defvar allout-widgets-changes-record nil
355 "Record outline changes for processing by post-command hook.
357 Entries on the list are lists whose first element is a symbol indicating
358 the change type and subsequent elements are data specific to that change
361 'exposure `allout-exposure-from' `allout-exposure-to' `allout-exposure-flag'
363 The changes are recorded in reverse order, with new values pushed
365 (make-variable-buffer-local 'allout-widgets-changes-record
)
366 ;;;_ = allout-widgets-undo-exposure-record
367 (defvar allout-widgets-undo-exposure-record nil
368 "Record outline undo traces for processing by post-command hook.
370 The changes are recorded in reverse order, with new values pushed
372 (make-variable-buffer-local 'allout-widgets-undo-exposure-record
)
373 ;;;_ = allout-widgets-last-hook-error
374 (defvar allout-widgets-last-hook-error nil
375 "String holding last error string, for debugging purposes.")
376 ;;;_ = allout-widgets-adjust-message-length-threshold 100
377 (defvar allout-widgets-adjust-message-length-threshold
100
378 "Display \"Adjusting widgets\" message above this number of pending changes."
380 ;;;_ = allout-widgets-adjust-message-size-threshold 10000
381 (defvar allout-widgets-adjust-message-size-threshold
10000
382 "Display \"Adjusting widgets\" message above this size of pending changes."
384 ;;;_ = allout-doing-exposure-undo-processor nil
385 (defvar allout-undo-exposure-in-progress nil
386 "Maintained true during `allout-widgets-exposure-undo-processor'")
387 ;;;_ , Widget-specific outline text format
388 ;;;_ = allout-escaped-prefix-regexp
389 (defvar allout-escaped-prefix-regexp
""
390 "*Regular expression for body text that would look like an item prefix if
391 not altered with an escape sequence.")
392 (make-variable-buffer-local 'allout-escaped-prefix-regexp
)
393 ;;;_ , Widget element formatting
394 ;;;_ = allout-item-icon-keymap
395 (defvar allout-item-icon-keymap
396 (let ((km (make-sparse-keymap)))
397 (dolist (digit '("0" "1" "2" "3"
398 "4" "5" "6" "7" "8" "9"))
399 (define-key km digit
'digit-argument
))
400 (define-key km
"-" 'negative-argument
)
401 ;; (define-key km [(return)] 'allout-tree-expand-command)
402 ;; (define-key km [(meta return)] 'allout-toggle-torso-command)
403 ;; (define-key km [(down-mouse-1)] 'allout-item-button-click)
404 ;; (define-key km [(down-mouse-2)] 'allout-toggle-torso-event-command)
405 ;; Override underlying mouse-1 and mouse-2 bindings in icon territory:
406 (define-key km
[(mouse-1)] (lambda () (interactive) nil
))
407 (define-key km
[(mouse-2)] (lambda () (interactive) nil
))
409 ;; Catchall, handles actual keybindings, dynamically doing keymap lookups:
410 (define-key km
[t] 'allout-item-icon-key-handler)
413 "General tree-node key bindings.")
414 ;;;_ = allout-item-body-keymap
415 (defvar allout-item-body-keymap
416 (let ((km (make-sparse-keymap))
417 (local-map (current-local-map)))
418 ;; (define-key km [(control return)] 'allout-tree-expand-command)
419 ;; (define-key km [(meta return)] 'allout-toggle-torso-command)
420 ;; XXX We need to reset this per buffer's mode; we do so in
421 ;; allout-widgets-mode.
423 (set-keymap-parent km local-map))
426 "General key bindings for the text content of outline items.")
427 (make-variable-buffer-local 'allout-item-body-keymap)
428 ;;;_ = allout-body-span-category
429 (defvar allout-body-span-category nil
430 "Symbol carrying allout body-text overlay properties.")
431 ;;;_ = allout-cue-span-keymap
432 (defvar allout-cue-span-keymap
433 (let ((km (make-sparse-keymap)))
434 (set-keymap-parent km allout-item-icon-keymap)
436 "Keymap used in the item cue area - the space between the icon and headline.")
437 ;;;_ = allout-escapes-category
438 (defvar allout-escapes-category nil
439 "Symbol for category of text property used to hide escapes of prefix-like
440 text in allout item bodies.")
441 ;;;_ = allout-guides-category
442 (defvar allout-guides-category nil
443 "Symbol carrying allout icon-guides overlay properties.")
444 ;;;_ = allout-guides-span-category
445 (defvar allout-guides-span-category nil
446 "Symbol carrying allout icon and guide lines overlay properties.")
447 ;;;_ = allout-icon-span-category
448 (defvar allout-icon-span-category nil
449 "Symbol carrying allout icon and guide lines overlay properties.")
450 ;;;_ = allout-cue-span-category
451 (defvar allout-cue-span-category nil
452 "Symbol carrying common properties of the space following the outline icon.
454 \(That space is used to convey selected cues indicating body qualities,
455 including things like:
458 - indirect reference '@'
459 - distinctive bullets - see `allout-distinctive-bullets-string'.\)")
460 ;;;_ = allout-span-to-category
461 (defvar allout-span-to-category
462 '((:guides-span . allout-guides-span-category)
463 (:cue-span . allout-cue-span-category)
464 (:icon-span . allout-icon-span-category)
465 (:body-span . allout-body-span-category))
466 "Association list mapping span identifier to category identifier.")
467 ;;;_ = allout-trailing-category
468 (defvar allout-trailing-category nil
469 "Symbol carrying common properties of an overlay's trailing newline.")
471 (defvar allout-widgets-last-decoration-timing nil
472 "Timing details for the last cooperative decoration action.
474 This is maintained when `allout-widgets-time-decoration-activity' is set.
476 The value is a list containing two elements:
477 - the elapsed time as a number of seconds
478 - the list of changes processed, a la `allout-widgets-changes-record'.
480 When active, the value is revised each time automatic decoration activity
481 happens in the buffer.")
482 (make-variable-buffer-local 'allout-widgets-last-decoration-timing)
484 ;;;_ > define-minor-mode allout-widgets-mode (arg)
486 (define-minor-mode allout-widgets-mode
487 "Allout-mode extension, providing graphical decoration of outline structure.
489 This is meant to operate along with allout-mode, via `allout-mode-hook'.
491 If optional argument ARG is greater than 0, enable.
492 If optional argument ARG is less than 0, disable.
493 Anything else, toggle between active and inactive.
495 The graphics include:
497 - guide lines connecting item bullet-icons with those of their subitems.
499 - icons for item bullets, varying to indicate whether or not the item
500 has subitems, and if so, whether or not the item is expanded.
502 - cue area between the bullet-icon and the start of the body headline,
503 for item numbering, encryption indicator, and distinctive bullets.
505 The bullet-icon and guide line graphics provide keybindings and mouse
506 bindings for easy outline navigation and exposure control, extending
507 outline hot-spot navigation \(see `allout-mode')."
512 ;; define-minor-mode handles any provided argument according to emacs
513 ;; minor-mode conventions - '(elisp) Minor Mode Conventions' - and sets
514 ;; allout-widgets-mode accordingly *before* running the body code, so we
516 (if allout-widgets-mode
519 (allout-add-resumptions
520 ;; XXX user may need say in line-truncation/hscrolling - an option
521 ;; that abstracts mode.
522 ;; truncate text lines to keep guide lines intact:
524 ;; and enable autoscrolling to ease view of text
525 '(auto-hscroll-mode t)
526 '(line-move-ignore-fields t)
527 '(widget-push-button-prefix "")
528 '(widget-push-button-suffix "")
529 ;; allout-escaped-prefix-regexp depends on allout-regexp:
530 (list 'allout-escaped-prefix-regexp (concat "\\(\\\\\\)"
531 "\\(" allout-regexp "\\)")))
532 (allout-add-resumptions
533 (list 'allout-widgets-tally allout-widgets-tally)
534 (list 'allout-widgets-escapes-sanitization-regexp-pair
535 (list (concat "\\(\n\\|\\`\\)"
536 allout-escaped-prefix-regexp
538 ;; Include everything but the escape symbol.
542 (add-hook 'after-change-functions 'allout-widgets-after-change-handler
545 (allout-setup-text-properties)
546 (add-to-invisibility-spec '(allout-torso . t))
547 (add-to-invisibility-spec 'allout-escapes)
549 (if (current-local-map)
550 (set-keymap-parent allout-item-body-keymap (current-local-map)))
552 (add-hook 'allout-exposure-change-hook
553 'allout-widgets-exposure-change-recorder nil 'local)
554 (add-hook 'allout-structure-added-hook
555 'allout-widgets-additions-recorder nil 'local)
556 (add-hook 'allout-structure-deleted-hook
557 'allout-widgets-deletions-recorder nil 'local)
558 (add-hook 'allout-structure-shifted-hook
559 'allout-widgets-shifts-recorder nil 'local)
560 (add-hook 'allout-after-copy-or-kill-hook
561 'allout-widgets-after-copy-or-kill-function nil 'local)
563 (add-hook 'before-change-functions 'allout-widgets-before-change-handler
565 (add-hook 'post-command-hook 'allout-widgets-post-command-business
567 (add-hook 'pre-command-hook 'allout-widgets-pre-command-business
570 ;; init the widgets tally for debugging:
571 (if (not allout-widgets-tally)
572 (setq allout-widgets-tally (make-hash-table
573 :test 'eq :weakness 'key)))
574 ;; add tally count display on minor-mode-alist just after
575 ;; allout-mode entry.
576 ;; (we use ternary condition form to keep condition simple for deletion.)
577 (let* ((mode-line-entry '(allout-widgets-mode-inhibit ""
578 (:eval (allout-widgets-tally-string))))
579 (associated (assoc (car mode-line-entry) minor-mode-alist))
580 ;; need location for it only if not already present:
581 (after (and (not associated)
582 (memq (assq 'allout-mode minor-mode-alist) minor-mode-alist))))
584 (rplacd after (cons mode-line-entry (cdr after)))))
585 (allout-widgets-prepopulate-buffer)
588 (let ((inhibit-read-only t)
589 (was-modified (buffer-modified-p)))
591 (allout-widgets-undecorate-region (point-min)(point-max))
592 (remove-from-invisibility-spec '(allout-torso . t))
593 (remove-from-invisibility-spec 'allout-escapes)
595 (remove-hook 'after-change-functions
596 'allout-widgets-after-change-handler 'local)
597 (remove-hook 'allout-exposure-change-hook
598 'allout-widgets-exposure-change-recorder 'local)
599 (remove-hook 'allout-structure-added-hook
600 'allout-widgets-additions-recorder 'local)
601 (remove-hook 'allout-structure-deleted-hook
602 'allout-widgets-deletions-recorder 'local)
603 (remove-hook 'allout-structure-shifted-hook
604 'allout-widgets-shifts-recorder 'local)
605 (remove-hook 'allout-after-copy-or-kill-hook
606 'allout-widgets-after-copy-or-kill-function 'local)
607 (remove-hook 'before-change-functions
608 'allout-widgets-before-change-handler 'local)
609 (remove-hook 'post-command-hook
610 'allout-widgets-post-command-business 'local)
611 (remove-hook 'pre-command-hook
612 'allout-widgets-pre-command-business 'local)
613 (assq-delete-all 'allout-widgets-mode-inhibit minor-mode-alist)
614 (set-buffer-modified-p was-modified))))
615 ;;;_ > allout-widgets-mode-off
616 (defun allout-widgets-mode-off ()
617 "Explicitly disable allout-widgets-mode."
618 (allout-widgets-mode -1))
619 ;;;_ > allout-widgets-mode-off
620 (defun allout-widgets-mode-on ()
621 "Explicitly disable allout-widgets-mode."
622 (allout-widgets-mode 1))
623 ;;;_ > allout-setup-text-properties ()
624 (defun allout-setup-text-properties ()
625 "Configure category and literal text properties."
627 ;; XXX body - before-change, entry, keymap
629 (setplist 'allout-guides-span-category nil)
630 (put 'allout-guides-span-category
631 'modification-hooks '(allout-graphics-modification-handler))
632 (put 'allout-guides-span-category 'local-map allout-item-icon-keymap)
633 (put 'allout-guides-span-category 'mouse-face widget-button-face)
634 (put 'allout-guides-span-category 'field 'structure)
635 ;; (put 'allout-guides-span-category 'face 'widget-button)
637 (setplist 'allout-icon-span-category
638 (allout-widgets-copy-list (symbol-plist
639 'allout-guides-span-category)))
640 (put 'allout-icon-span-category 'field 'structure)
642 ;; XXX for body text we're instead going to use the buffer-wide
643 ;; resources, like before/after-change-functions hooks and the
644 ;; buffer's key map. that way we won't have to do painful provisions
645 ;; to fixup things after edits, catch outlier interstitial
646 ;; characters, like newline and empty lines after hidden subitems,
648 (setplist 'allout-body-span-category nil)
649 (put 'allout-body-span-category 'evaporate t)
650 (put 'allout-body-span-category 'local-map allout-item-body-keymap)
651 ;;(put 'allout-body-span-category
652 ;; 'modification-hooks '(allout-body-modification-handler))
653 ;;(put 'allout-body-span-category 'field 'body)
655 (setplist 'allout-cue-span-category nil)
656 (put 'allout-cue-span-category 'evaporate t)
657 (put 'allout-cue-span-category
658 'modification-hooks '(allout-body-modification-handler))
659 (put 'allout-cue-span-category 'local-map allout-cue-span-keymap)
660 (put 'allout-cue-span-category 'mouse-face widget-button-face)
661 (put 'allout-cue-span-category 'pointer 'arrow)
662 (put 'allout-cue-span-category 'field 'structure)
664 (setplist 'allout-trailing-category nil)
665 (put 'allout-trailing-category 'evaporate t)
666 (put 'allout-trailing-category 'local-map allout-item-body-keymap)
668 (setplist 'allout-escapes-category nil)
669 (put 'allout-escapes-category 'invisible 'allout-escapes)
670 (put 'allout-escapes-category 'evaporate t))
671 ;;;_ > allout-widgets-prepopulate-buffer ()
672 (defun allout-widgets-prepopulate-buffer ()
673 "Step over the current buffers exposed items to do initial widgetizing."
674 (if (not allout-widgets-mode-inhibit)
676 (goto-char (point-min))
677 (while (allout-next-visible-heading 1)
678 (when (not (widget-at (point)))
679 (allout-get-or-create-item-widget))))))
680 ;;;_ . settings context
681 ;;;_ = allout-container-item
682 (defvar allout-container-item-widget nil
683 "A widget for the current outline's overarching container as an item.
685 The item has settings \(of the file/connection\) and maybe a body, but no
687 (make-variable-buffer-local 'allout-container-item-widget)
688 ;;;_ . Hooks and hook helpers
689 ;;;_ , major command-loop business:
690 ;;;_ > allout-widgets-pre-command-business (&optional recursing)
691 (defun allout-widgets-pre-command-business (&optional recursing)
692 "Handle actions pending before allout-mode activity."
694 ;;;_ > allout-widgets-post-command-business (&optional recursing)
695 (defun allout-widgets-post-command-business (&optional recursing)
696 "Handle actions pending after any allout-mode commands.
698 Optional RECURSING is for internal use, to limit recursion."
699 ;; - check changed text for nesting discontinuities and escape anything
700 ;; that's: (1) asterisks at bol or (2) excessively nested.
701 (condition-case failure
703 (when (and (boundp 'allout-mode) allout-mode)
705 (if allout-widgets-unset-inhibit-read-only
706 (setq inhibit-read-only nil
707 allout-widgets-unset-inhibit-read-only nil))
709 (when allout-widgets-reenable-before-change-handler
710 (add-hook 'before-change-functions
711 'allout-widgets-before-change-handler
713 (setq allout-widgets-reenable-before-change-handler nil))
715 (when (or allout-widgets-undo-exposure-record
716 allout-widgets-changes-record)
717 (let* ((debug-on-signal t)
719 ;; inhibit recording new undo records when processing
720 ;; effects of undo-exposure:
721 (debugger 'allout-widgets-hook-error-handler)
722 (adjusting-message " Adjusting widgets...")
723 (replaced-message (allout-widgets-adjusting-message
725 (start-time (current-time)))
727 (if allout-widgets-undo-exposure-record
728 ;; inhibit undo recording iff undoing exposure stuff.
729 ;; XXX we might need to inhibit per respective
730 ;; change-record, rather than assuming that some undo
731 ;; activity during a command is all undo activity.
732 (let ((buffer-undo-list t))
733 (allout-widgets-exposure-undo-processor)
734 (allout-widgets-changes-dispatcher))
735 (allout-widgets-exposure-undo-processor)
736 (allout-widgets-changes-dispatcher))
738 (if allout-widgets-time-decoration-activity
739 (setq allout-widgets-last-decoration-timing
740 (list (allout-elapsed-time-seconds (current-time)
742 allout-widgets-changes-record)))
744 (setq allout-widgets-changes-record nil)
747 (if (stringp replaced-message)
748 (message replaced-message)
751 ;; Detect undecorated items, eg during isearch into previously
752 ;; unexposed topics, and decorate "economically". Some
753 ;; undecorated stuff is often exposed, to reduce lag, but the
754 ;; item containing the cursor is decorated. We constrain
755 ;; recursion to avoid being trapped by unexpectedly undecoratable
757 (when (and (not recursing)
758 (not (allout-current-decorated-p))
759 (or (not (equal (allout-depth) 0))
760 (not allout-container-item-widget)))
761 (let ((buffer-undo-list t))
762 (allout-widgets-exposure-change-recorder
763 allout-recent-prefix-beginning allout-recent-prefix-end nil)
764 (allout-widgets-post-command-business 'recursing)))
766 ;; Detect and rectify fouled outline structure - decorated item
767 ;; not at beginning of line.
768 (let ((this-widget (or (widget-at (point))
769 ;; XXX we really should be checking across
770 ;; edited span, not just point and point+1
771 (and (not (eq (point) (point-max)))
772 (widget-at (1+ (point))))))
776 (goto-char (widget-get this-widget :from))
781 (concat "Misplaced item won't be recognizable "
782 " as part of outline - rectify? "))
785 (if (allout-hidden-p (max (1- (point)) 1))
787 (goto-char (max (1- (point)) 1))
788 (allout-show-to-offshoot)))
789 (allout-widgets-undecorate-item this-widget))
790 ;; expose any hidden intervening items, so resulting
791 ;; position is clear:
792 (setq inserted-at (point))
793 (allout-unprotected (insert-before-markers "\n"))
795 ;; ensure the inserted newline is visible:
796 (allout-flag-region inserted-at (1+ inserted-at) nil)
797 (allout-widgets-post-command-business 'recursing)
798 (message (concat "outline structure corrected - item"
799 " moved to beginning of new line"))
800 ;; preserve cursor position in some cases:
802 (> (point) inserted-at))
803 (forward-char -1)))))))
806 ;; zero work list so we don't get stuck futily retrying.
807 ;; error recording done by allout-widgets-hook-error-handler.
808 (setq allout-widgets-changes-record nil))))
809 ;;;_ , major change handlers:
810 ;;;_ > allout-widgets-before-change-handler
811 (defun allout-widgets-before-change-handler (beg end)
812 "Business to be done before changes in a widgetized allout outline."
813 ;; protect against unruly edits to structure:
815 (undo-in-progress (when (eq (get-text-property beg 'category)
816 'allout-icon-span-category)
819 (let* ((item-widget (allout-get-item-widget)))
821 (allout-widgets-exposure-undo-recorder
823 (inhibit-read-only t)
824 ((not (and (boundp 'allout-mode) allout-mode)) t)
825 ((equal this-command 'quoted-insert) t)
826 ((not (text-property-any beg (if (equal end beg)
827 (min (1+ beg) (point-max))
831 ((yes-or-no-p "Unruly edit of outline structure - allow? ")
832 (setq allout-widgets-unset-inhibit-read-only (not inhibit-read-only)
833 inhibit-read-only t))
835 ;; tell the allout-widgets-post-command-business to reestablish the hook:
836 (setq allout-widgets-reenable-before-change-handler t)
837 ;; and raise an error to prevent the edit (and disable the hook):
839 (substitute-command-keys allout-structure-unruly-deletion-message)))))
840 ;;;_ > allout-widgets-after-change-handler
841 (defun allout-widgets-after-change-handler (beg end prelength)
842 "Reconcile what needs to be reconciled for allout widgets after edits."
844 ;;;_ > allout-current-decorated-p ()
845 (defun allout-current-decorated-p ()
846 "True if the current item is not decorated"
848 (if (allout-back-to-current-heading)
849 (if (> allout-recent-depth 0)
850 (and (allout-get-item-widget) t)
851 allout-container-item-widget))))
853 ;;;_ > allout-widgets-hook-error-handler
854 (defun allout-widgets-hook-error-handler (mode args)
855 "Process errors which occurred in the course of command hook operation.
857 We store a backtrace of the error information in the variable,
858 `allout-widgets-last-hook-error', unset the error handlers, and
859 reraise the error, so that processing continues to the
860 encompassing condition-case."
861 ;; first deconstruct special error environment so errors here propagate
862 ;; to encompassing condition-case:
863 (setq debugger 'debug
866 (let* ((bt (with-output-to-string (backtrace)))
867 (this "allout-widgets-hook-error-handler")
869 (format "allout-widgets-last-hook-error stored, %s/%s %s %s"
871 (format-time-string "%e-%b-%Y %r" (current-time)))))
872 ;; post to *Messages* then immediately replace with more compact notice:
873 (message "%s" (setq allout-widgets-last-hook-error
874 (format "%s:\n%s" header bt)))
875 (message header) (sit-for allout-widgets-hook-error-post-time)
876 ;; reraise the error, or one concerning this function if unexpected:
877 (if (equal mode 'error)
879 (error "%s: unexpected mode, %s %s" this mode args))))
880 ;;;_ > allout-widgets-changes-exceed-threshold-p ()
881 (defun allout-widgets-adjusting-message (message)
882 "Post MESSAGE when pending are likely to make a big enough delay.
884 If posting of the MESSAGE is warranted and there already is a
885 `current-message' in the minibuffer, the MESSAGE is appended to
886 the current one, and the previously pending `current-message' is
887 returned for later posting on completion.
889 If posting of the MESSAGE is warranted, but no `current-message'
890 is pending, then t is returned to indicate that case.
892 If posting of the MESSAGE is not warranted, then nil is returned.
894 See `allout-widgets-adjust-message-length-threshold',
895 `allout-widgets-adjust-message-size-threshold' for message
896 posting threshold criteria."
897 (if (or (> (length allout-widgets-changes-record)
898 allout-widgets-adjust-message-length-threshold)
899 ;; for size, use distance from start of first to end of last:
900 (let ((min (point-max))
903 (mapc (function (lambda (entry)
904 (if (eq :undone-exposure (car entry))
906 (setq first (cadr entry)
907 second (caddr entry))
908 (if (< (min first second) min)
909 (setq min (min first second)))
910 (if (> (max first second) max)
911 (setq max (max first second))))))
912 allout-widgets-changes-record)
913 (> (- max min) allout-widgets-adjust-message-size-threshold)))
914 (let ((prior (current-message)))
915 (message (if prior (concat prior " - " message) message))
917 ;;;_ > allout-widgets-changes-dispatcher ()
918 (defun allout-widgets-changes-dispatcher ()
919 "Dispatch CHANGES-RECORD items to respective widgets change processors."
921 (if (not allout-widgets-mode-inhibit)
922 (let* ((changes-record allout-widgets-changes-record)
923 (changes-pending (and changes-record t))
930 (when changes-pending
931 (while changes-record
932 (setq entry (pop changes-record))
934 (:exposed (push entry exposures))
935 (:added (push entry additions))
936 (:deleted (push entry deletions))
937 (:shifted (push entry shifts))))
940 (allout-widgets-exposure-change-processor exposures))
942 (allout-widgets-additions-processor additions))
944 (allout-widgets-deletions-processor deletions))
946 (allout-widgets-shifts-processor shifts))))
947 (when (not (equal allout-widgets-mode-inhibit 'undecorated))
948 (allout-widgets-undecorate-region (point-min)(point-max))
949 (setq allout-widgets-mode-inhibit 'undecorated))))
950 ;;;_ > allout-widgets-exposure-change-recorder (from to flag)
951 (defun allout-widgets-exposure-change-recorder (from to flag)
952 "Record allout exposure changes for tracking during post-command processing.
954 Records changes in `allout-widgets-changes-record'."
955 (push (list :exposed from to flag) allout-widgets-changes-record))
956 ;;;_ > allout-widgets-exposure-change-processor (changes)
957 (defun allout-widgets-exposure-change-processor (changes)
958 "Widgetize and adjust item widgets tracking allout outline exposure changes.
960 Generally invoked via `allout-exposure-change-hook'."
962 (let ((changes (sort changes (function (lambda (this next)
963 (< (cadr this) (cadr next))))))
964 ;; have to distinguish between concealing and exposing so that, eg,
965 ;; `allout-expose-topic's mix is handled properly.
971 (dolist (change changes)
976 (flag (cadddr change))
980 (if (< to from) (setq bucket to
984 ;; have we already handled exposure changes in this region?
985 (setq handling (if flag 'handled-conceal 'handled-expose)
986 got (allout-range-overlaps from to (symbol-value handling))
988 (set handling (cadr got))
997 (allout-widgets-undecorate-region from to)
998 (allout-beginning-of-current-line)
999 (let ((widget (allout-get-item-widget)))
1001 (allout-get-or-create-item-widget)
1002 (widget-apply widget :redecorate))))
1006 (while (< (point) to)
1007 (allout-beginning-of-current-line)
1008 (setq parent (allout-get-item-widget))
1010 (setq parent (allout-get-or-create-item-widget))
1011 (widget-apply parent :redecorate))
1012 (allout-next-visible-heading 1)
1013 (if (widget-get parent :has-subitems)
1014 (allout-redecorate-visible-subtree parent))
1016 ;; subtree may be well beyond to - incorporate in ranges:
1017 (setq handled-expose
1018 (allout-range-overlaps from (point) handled-expose)
1019 covered (car handled-expose)
1020 handled-expose (cadr handled-expose)))
1021 (allout-next-visible-heading 1))))))))))
1023 ;;;_ > allout-widgets-additions-recorder (from to)
1024 (defun allout-widgets-additions-recorder (from to)
1025 "Record allout item additions for tracking during post-command processing.
1027 Intended for use on `allout-structure-added-hook'.
1029 FROM point at the start of the first new item and TO is point at the start
1032 Records changes in `allout-widgets-changes-record'."
1033 (push (list :added from to) allout-widgets-changes-record))
1034 ;;;_ > allout-widgets-additions-processor (changes)
1035 (defun allout-widgets-additions-processor (changes)
1036 "Widgetize and adjust items tracking allout outline structure additions.
1038 Dispatched by `allout-widgets-post-command-business' in response to
1039 :added entries recorded by `allout-widgets-additions-recorder'."
1043 (dolist (change changes)
1044 (let ((from (cadr change))
1046 (to (caddr change)))
1047 (if (< to from) (setq bucket to to from from bucket))
1048 ;; have we already handled exposure changes in this region?
1049 (setq handled (allout-range-overlaps from to handled)
1050 covered (car handled)
1051 handled (cadr handled))
1054 ;; Prior sibling and parent can both be affected.
1056 (allout-redecorate-visible-subtree
1057 (allout-get-or-create-item-widget 'redecorate)))
1058 (if (< (point) from)
1060 (while (and (< (point) to) (not (eobp)))
1061 (allout-beginning-of-current-line)
1062 (allout-redecorate-visible-subtree
1063 (allout-get-or-create-item-widget))
1064 (allout-next-visible-heading 1))
1066 ;; subtree may be well beyond to - incorporate in ranges:
1067 (setq handled (allout-range-overlaps from (point) handled)
1068 covered (car handled)
1069 handled (cadr handled)))))))))
1071 ;;;_ > allout-widgets-deletions-recorder (depth from)
1072 (defun allout-widgets-deletions-recorder (depth from)
1073 "Record allout item deletions for tracking during post-command processing.
1075 Intended for use on `allout-structure-deleted-hook'.
1077 DEPTH is the depth of the deleted subtree, and FROM is the point from which
1078 the subtree was deleted.
1080 Records changes in `allout-widgets-changes-record'."
1081 (push (list :deleted depth from) allout-widgets-changes-record))
1082 ;;;_ > allout-widgets-deletions-processor (changes)
1083 (defun allout-widgets-deletions-processor (changes)
1084 "Adjust items tracking allout outline structure deletions.
1086 Dispatched by `allout-widgets-post-command-business' in response to
1087 :deleted entries recorded by `allout-widgets-deletions-recorder'."
1089 (dolist (change changes)
1090 (let ((depth (cadr change))
1091 (from (caddr change)))
1093 (when (allout-previous-visible-heading 1)
1095 (allout-ascend-to-depth (1- depth)))
1096 (allout-redecorate-visible-subtree
1097 (allout-get-or-create-item-widget 'redecorate)))))))
1099 ;;;_ > allout-widgets-shifts-recorder (shifted-amount at)
1100 (defun allout-widgets-shifts-recorder (shifted-amount at)
1101 "Record outline subtree shifts for tracking during post-command processing.
1103 Intended for use on `allout-structure-shifted-hook'.
1105 SHIFTED-AMOUNT is the depth change and AT is the point at the start of the
1106 subtree that's been shifted.
1108 Records changes in `allout-widgets-changes-record'."
1109 (push (list :shifted shifted-amount at) allout-widgets-changes-record))
1110 ;;;_ > allout-widgets-shifts-processor (changes)
1111 (defun allout-widgets-shifts-processor (changes)
1112 "Widgetize and adjust items tracking allout outline structure additions.
1114 Dispatched by `allout-widgets-post-command-business' in response to
1115 :shifted entries recorded by `allout-widgets-shifts-recorder'."
1117 (dolist (change changes)
1118 (goto-char (caddr change))
1120 (allout-redecorate-visible-subtree))))
1121 ;;;_ > allout-widgets-after-copy-or-kill-function ()
1122 (defun allout-widgets-after-copy-or-kill-function ()
1123 "Do allout-widgets processing of text just placed in the kill ring.
1125 Intended for use on allout-after-copy-or-kill-hook."
1127 (setcar kill-ring (allout-widgets-undecorate-text (car kill-ring)))))
1129 ;;;_ > allout-widgets-exposure-undo-recorder (widget from-state)
1130 (defun allout-widgets-exposure-undo-recorder (widget)
1131 "Record outline exposure undo for tracking during post-command processing.
1133 Intended for use by `allout-graphics-modification-handler'.
1135 WIDGET is the widget being changed.
1137 Records changes in `allout-widgets-changes-record'."
1138 ;; disregard the events if we're currently processing them.
1139 (if (not allout-undo-exposure-in-progress)
1140 (push widget allout-widgets-undo-exposure-record)))
1141 ;;;_ > allout-widgets-exposure-undo-processor ()
1142 (defun allout-widgets-exposure-undo-processor ()
1143 "Adjust items tracking undo of allout outline structure exposure.
1145 Dispatched by `allout-widgets-post-command-business' in response to
1146 :undone-exposure entries recorded by `allout-widgets-exposure-undo-recorder'."
1147 (let* ((allout-undo-exposure-in-progress t)
1148 ;; inhibit undo recording while twiddling exposure to track undo:
1149 (widgets allout-widgets-undo-exposure-record)
1150 widget widget-start-marker widget-end-marker
1151 from-state icon-start-point to-state
1153 (setq allout-widgets-undo-exposure-record nil)
1155 (dolist (widget widgets)
1156 (setq widget-start-marker (widget-get widget :from)
1157 widget-end-marker (widget-get widget :to)
1158 from-state (widget-get widget :icon-state)
1159 icon-start-point (widget-apply widget :actual-position
1161 to-state (get-text-property icon-start-point
1163 (setq handled (allout-range-overlaps widget-start-marker
1166 covered (car handled)
1167 handled (cadr handled))
1169 (goto-char (widget-get widget :from))
1170 (when (not (allout-hidden-p))
1171 ;; adjust actual exposure to that of to-state viz from-state
1172 (cond ((and (eq to-state 'closed) (eq from-state 'opened))
1173 (allout-hide-current-subtree)
1174 (allout-decorate-item-and-context widget))
1175 ((and (eq to-state 'opened) (eq from-state 'closed))
1178 (expose-to (allout-chart-exposure-contour-by-icon))
1179 (goto-char expose-to)
1180 (allout-show-to-offshoot)))))))))))
1181 ;;;_ > allout-chart-exposure-contour-by-icon (&optional from-depth)
1182 (defun allout-chart-exposure-contour-by-icon (&optional from-depth)
1183 "Return points of subtree items to which exposure should be extended.
1185 The qualifying items are ones with a widget icon that is in the closed or
1186 empty state, or items with undecorated subitems.
1188 The resulting list of points is in reverse order.
1190 Optional FROM-DEPTH is for internal use."
1191 ;; During internal recursion, we return a pair: (at-end . result)
1192 ;; Otherwise we just return the result.
1193 (let ((from-depth from-depth)
1199 (setq level-depth (allout-depth))
1200 ;; at containing item:
1201 (setq start-point (point))
1202 (setq from-depth (allout-depth))
1203 (setq at-end (not (allout-next-heading))
1204 level-depth allout-recent-depth))
1206 ;; traverse the level, recursing on deeper levels:
1207 (while (and (not at-end)
1208 (> allout-recent-depth from-depth)
1209 (setq this-widget (allout-get-item-widget)))
1210 (if (< level-depth allout-recent-depth)
1213 (setq subgot (allout-chart-exposure-contour-by-icon level-depth)
1215 subgot (cdr subgot))
1216 (if subgot (setq got (append subgot got))))
1217 ;; progress at this level:
1218 (when (memq (widget-get this-widget :icon-state) '(closed empty))
1220 (allout-end-of-subtree))
1221 (setq at-end (not (allout-next-heading)))))
1223 ;; tailor result depending on whether or not we're a recursion:
1224 (if (not start-point)
1226 (goto-char start-point)
1228 ;;;_ > allout-range-overlaps (from to ranges)
1229 (defun allout-range-overlaps (from to ranges)
1230 "Return a pair indicating overlap of FROM and TO subtree range in RANGES.
1232 First element of result indicates whether candadate range FROM, TO
1233 overlapped any of the existing ranges.
1235 Second element of result is a new version of RANGES incorporating the
1236 candidate range with overlaps consolidated.
1238 FROM and TO must be in increasing order, as must be the pairs in RANGES."
1239 ;; to append to the end: (rplacd next-to-last-cdr (list 'f))
1242 ;; the start of the range that includes the candidate from:
1244 ;; the end of the range that includes the candidate to:
1246 ;; the candidates were inserted:
1248 (while (and ranges (not done))
1249 (setq entry (car ranges)
1250 ranges (cdr ranges))
1255 ;; some entry included the candidate from.
1256 (cond ((> (car entry) to)
1257 ;; current entry exceeds end of candidate range - done.
1258 (push (list included-from to) new-ranges)
1259 (push entry new-ranges)
1260 (setq included-to to
1262 ((>= (cadr entry) to)
1263 ;; current entry includes end of candidate range - done.
1264 (push (list included-from (cadr entry)) new-ranges)
1265 (setq included-to (cadr entry)
1267 ;; current entry contained in candidate range - ditch, continue:
1271 ;; current entry start exceeds candidate end - done, placed as new entry
1272 (push (list from to) new-ranges)
1273 (push entry new-ranges)
1274 (setq included-to to
1277 ((>= (car entry) from)
1278 ;; current entry start is above candidate start, but not above
1279 ;; candidate end (by prior case).
1280 (setq included-from from)
1281 ;; now we have to check on whether this entry contains to, or continue:
1282 (when (>= (cadr entry) to)
1283 ;; current entry contains only candidate end - done:
1284 (push (list included-from (cadr entry)) new-ranges)
1285 (setq included-to (cadr entry)
1287 ;; otherwise, we will continue to look for placement of candidate end.
1290 ((>= (cadr entry) to)
1291 ;; current entry properly contains candidate range.
1292 (push entry new-ranges)
1293 (setq included-from (car entry)
1294 included-to (cadr entry)
1297 ((>= (cadr entry) from)
1298 ;; current entry contains start of candidate range.
1299 (setq included-from (car entry)))
1302 ;; current entry is below the candidate range.
1303 (push entry new-ranges))))
1305 (cond ((and included-from included-to)
1306 ;; candidates placed.
1308 ((not (or included-from included-to))
1309 ;; candidates found no place, must be at the end:
1310 (push (list from to) new-ranges))
1312 ;; candidate start placed but end not:
1313 (push (list included-from to) new-ranges))
1314 ;; might be included-to and not included-from, indicating new entry.
1316 (setq new-ranges (nreverse new-ranges))
1317 (if ranges (setq new-ranges (append new-ranges ranges)))
1318 (list (if included-from t) new-ranges)))
1319 ;;;_ > allout-test-range-overlaps ()
1320 (defun allout-test-range-overlaps ()
1321 "allout-range-overlaps unit tests."
1324 (try (lambda (from to)
1325 (setq got (allout-range-overlaps from to ranges))
1326 (setq ranges (cadr got))
1329 ;; (setq ranges nil)
1330 ;; ;; ~ .02 to .1 seconds for just repeated listing args instead of funcall
1331 ;; ;; ~ 13 seconds for doing repeated funcall
1332 ;; (message "time-trial: %s, resulting size %s"
1334 ;; '(let ((size 10000)
1337 ;; (dotimes (count size)
1338 ;; (setq doing (random size))
1339 ;; (funcall try doing (+ doing (random 5)))
1340 ;; ;;(list doing (+ doing (random 5)))
1347 (assert (equal (funcall try 3 5) '(nil ((3 5)))))
1348 ;; add range at end:
1349 (assert (equal (funcall try 10 12) '(nil ((3 5) (10 12)))))
1350 ;; add range at beginning:
1351 (assert (equal (funcall try 1 2) '(nil ((1 2) (3 5) (10 12)))))
1352 ;; insert range somewhere in the middle:
1353 (assert (equal (funcall try 7 9) '(nil ((1 2) (3 5) (7 9) (10 12)))))
1354 ;; consolidate some:
1355 (assert (equal (funcall try 5 8) '(t ((1 2) (3 9) (10 12)))))
1357 (assert (equal (funcall try 15 17) '(nil ((1 2) (3 9) (10 12) (15 17)))))
1359 (assert (equal (funcall try 20 22)
1360 '(nil ((1 2) (3 9) (10 12) (15 17) (20 22)))))
1362 (assert (equal (funcall try 4 11) '(t ((1 2) (3 12) (15 17) (20 22)))))
1364 (assert (equal (funcall try 2 25) '(t ((1 25)))))
1368 (assert (equal (funcall try 20 25) '(nil ((20 25)))))
1369 (assert (equal (funcall try 30 35) '(nil ((20 25) (30 35)))))
1370 (assert (equal (funcall try 26 28) '(nil ((20 25) (26 28) (30 35)))))
1371 (assert (equal (funcall try 15 20) '(t ((15 25) (26 28) (30 35)))))
1372 (assert (equal (funcall try 10 30) '(t ((10 35)))))
1373 (assert (equal (funcall try 5 6) '(nil ((5 6) (10 35)))))
1374 (assert (equal (funcall try 2 100) '(t ((2 100)))))
1378 ;;;_ > allout-widgetize-buffer (&optional doing)
1379 (defun allout-widgetize-buffer (&optional doing)
1380 "EXAMPLE FUNCTION. Widgetize items in buffer using allout-chart-subtree.
1382 We economize by just focusing on the first of local-maximum depth siblings.
1384 Optional DOING is for internal use - a chart of the current level, for
1385 recursive operation."
1391 (goto-char (point-min))
1392 ;; Construct the chart by scanning the siblings:
1393 (dolist (top-level-sibling (allout-chart-siblings))
1394 (goto-char top-level-sibling)
1395 (let ((subchart (allout-chart-subtree)))
1397 (allout-widgetize-buffer subchart)))))
1399 ;; save-excursion was done on recursion entry, not necessary here.
1400 (let (have-sublists)
1401 (dolist (sibling doing)
1402 (when (listp sibling)
1403 (setq have-sublists t)
1404 (allout-widgetize-buffer sibling)))
1405 (when (and (not have-sublists) (not (widget-at (car doing))))
1406 (goto-char (car doing))
1407 (allout-get-or-create-item-widget)))))
1409 ;;;_ : Item widget and constructors
1411 ;;;_ $ allout-item-widget
1412 (define-widget 'allout-item-widget 'default
1413 "A widget presenting an allout outline item."
1416 ;; widget-field-at respects this to get item if 'field is unused.
1417 ;; we don't use field to avoid collision with end-of-line, etc, on which
1424 ;; tailor the widget for a specific item
1425 :create 'allout-decorate-item-and-context
1426 :value-delete 'allout-widgets-undecorate-item
1427 ;; Not Yet Converted (from original, tree-widget stab)
1428 :expander 'allout-tree-event-dispatcher ; get children when nil :args
1429 :expander-p 'identity ; always engage the :expander
1430 :action 'allout-tree-widget-action
1431 ;; :notify "when item changes"
1433 ;; force decoration of item but not context, unless already done this tick:
1434 :redecorate 'allout-redecorate-item
1435 :last-decorated-tick nil
1436 ;; recognize the actual situation of the item's text:
1437 :parse-item 'allout-parse-item-at-point
1438 ;; decorate the entirety of the item, sans offspring:
1439 :decorate-item-span 'allout-decorate-item-span
1440 ;; decorate the various item elements:
1441 :decorate-guides 'allout-decorate-item-guides
1442 :decorate-icon 'allout-decorate-item-icon
1443 :decorate-cue 'allout-decorate-item-cue
1444 :decorate-body 'allout-decorate-item-body
1445 :actual-position 'allout-item-actual-position
1447 ;; Layout parameters:
1448 :is-container nil ; is this actually the encompassing file/connection?
1450 :from nil ; item beginning - marker
1451 :to nil ; item end - marker
1452 :span-overlay nil ; overlay by which actual postion is determined
1454 ;; also serves as guide-end:
1457 :distinctive-start nil
1458 ;; also serves as cue-start:
1459 :distinctive-end nil
1460 ;; also serves as cue-end:
1465 :was-has-subitems 'init
1471 :does-encrypt nil ; pending encryption when :is-encrypted false.
1474 ;; the actual location of the item text:
1475 :location 'allout-item-location
1477 :button-keymap allout-item-icon-keymap ; XEmacs
1478 :keymap allout-item-icon-keymap ; Emacs
1488 :body-brevity-p 'allout-body-brevity-p
1490 ;; :guide-column-flags indicate (in reverse order) whether or not the
1491 ;; item's ancestor at the depth corresponding to the column has a
1492 ;; subsequent sibling - ie, whether or not the corresponding column needs
1493 ;; a descender line to connect that ancestor with its sibling.
1494 :guide-column-flags nil
1495 :was-guide-column-flags 'init
1497 ;; ie, has subitems:
1498 :populous-p 'allout-item-populous-p
1499 :help-echo 'allout-tree-widget-help-echo
1501 ;;;_ > allout-new-item-widget ()
1502 (defsubst allout-new-item-widget ()
1503 "create a new item widget, not yet situated anywhere."
1504 (if allout-widgets-maintain-tally
1505 ;; all the extra overhead is incurred only when doing the
1506 ;; maintenance, except the condition, which can't be avoided.
1507 (let ((widget (widget-convert 'allout-item-widget)))
1508 (puthash widget nil allout-widgets-tally)
1510 (widget-convert 'allout-item-widget)))
1511 ;;;_ : Item decoration
1512 ;;;_ > allout-decorate-item-and-context (item-widget &optional redecorate
1513 ;;; blank-container parent)
1514 (defun allout-decorate-item-and-context (item-widget &optional redecorate
1515 blank-container parent)
1516 "Create or adjust widget decorations for ITEM-WIDGET and neighbors at point.
1518 The neighbors include its siblings and parent.
1520 ITEM-WIDGET can be a created or converted allout-item-widget.
1522 If you're only trying to get or create a widget for an item, use
1523 `allout-get-or-create-item-widget'. If you have the item-widget, applying
1524 :redecorate will do the right thing.
1526 Optional BLANK-CONTAINER is for internal use. It is used to fabricate a
1527 container widget for an empty-bodied container, in the course of decorating
1528 a proper \(non-container\) item which starts at the beginning of the file.
1530 Optional REDECORATE causes redecoration of the item-widget and
1531 its siblings, even if already decorated in this cycle of the command loop.
1533 Optional PARENT, when provided, bypasses some navigation and computation
1534 necessary to obtain the parent of the items being processed.
1536 We return the item-widget corresponding to the item at point."
1538 (when (or redecorate
1539 (not (equal (widget-get item-widget :last-decorated-tick)
1540 allout-command-counter)))
1541 (let* ((allout-inhibit-body-modification-hook t)
1542 (was-modified (buffer-modified-p))
1545 (is-container (or blank-container
1546 (not (setq prefix-start (allout-goto-prefix)))
1547 (< was-point prefix-start)))
1548 ;; steady-point (set in two steps) is reliable across parent
1550 (steady-point (progn (if is-container (goto-char 1))
1552 (steady-point (progn (set-marker-insertion-type steady-point t)
1554 (parent (and (not is-container)
1555 (allout-get-or-create-parent-widget)))
1556 parent-flags parent-depth
1562 reverse-siblings-chart
1563 (buffer-undo-list t))
1565 ;; At this point the parent is decorated and parent-flags indicate
1566 ;; its guide lines. We will iterate over the siblings according to a
1567 ;; chart we create at the start, and going from last to first so we
1568 ;; don't have to worry about text displacement caused by widgetizing.
1571 (progn (widget-put item-widget :is-container t)
1572 (setq reverse-siblings-chart (list 1)))
1573 (goto-char (widget-apply parent :actual-position :from))
1574 (if (widget-get parent :is-container)
1575 ;; `allout-goto-prefix' will go to first non-container item:
1576 (allout-goto-prefix)
1577 (allout-next-heading))
1578 (setq depth (allout-recent-depth))
1579 (setq reverse-siblings-chart (list allout-recent-prefix-beginning))
1580 (while (allout-next-sibling)
1581 (push allout-recent-prefix-beginning reverse-siblings-chart)))
1583 (dolist (doing-at reverse-siblings-chart)
1584 (goto-char doing-at)
1585 (when allout-widgets-track-decoration
1588 (setq doing-item (if (= doing-at steady-point)
1590 (or (allout-get-item-widget)
1591 (allout-new-item-widget))))
1593 (when (or redecorate (not (equal (widget-get doing-item
1594 :last-decorated-tick)
1595 allout-command-counter)))
1596 (widget-apply doing-item :parse-item t blank-container)
1597 (widget-apply doing-item :decorate-item-span)
1599 (widget-apply doing-item :decorate-guides
1600 parent (and successor-sibling t))
1601 (widget-apply doing-item :decorate-icon)
1602 (widget-apply doing-item :decorate-cue)
1603 (widget-apply doing-item :decorate-body)
1605 (widget-put doing-item :last-decorated-tick allout-command-counter))
1607 (setq successor-sibling doing-at))
1609 (set-buffer-modified-p was-modified)
1610 (goto-char steady-point)
1611 ;; must null the marker or the buffer gets clogged with impedence:
1612 (set-marker steady-point nil)
1615 ;;;_ > allout-redecorate-item (item)
1616 (defun allout-redecorate-item (item-widget)
1617 "Resituate ITEM-WIDGET decorations, disregarding context.
1619 Use this to redecorate only the item, when you know that it's
1620 situation with respect to siblings, parent, and offspring is
1621 unchanged from its last decoration. Use
1622 `allout-decorate-item-and-context' instead to reassess and adjust
1623 relevent context, when suitable."
1624 (if (not (equal (widget-get item-widget :last-decorated-tick)
1625 allout-command-counter))
1626 (let ((was-modified (buffer-modified-p))
1627 (buffer-undo-list t))
1628 (widget-apply item-widget :parse-item)
1629 (widget-apply item-widget :decorate-guides)
1630 (widget-apply item-widget :decorate-icon)
1631 (widget-apply item-widget :decorate-cue)
1632 (widget-apply item-widget :decorate-body)
1633 (set-buffer-modified-p was-modified))))
1634 ;;;_ > allout-redecorate-visible-subtree (&optional parent-widget
1636 (defun allout-redecorate-visible-subtree (&optional parent-widget depth chart)
1637 "Redecorate all visible items in subtree at point.
1639 Optional PARENT-WIDGET is for optimization, when the parent
1640 widget is already available.
1642 Optional DEPTH restricts the excursion depth of covered.
1644 Optional CHART is for internal recursion, to carry a chart of the
1647 Point is left at the last sibling in the visible subtree."
1648 ;; using a treatment that takes care of all the siblings on a level, we
1649 ;; only need apply it to the first sibling on the level, and we can
1650 ;; collect and pass the parent of the lower levels to recursive calls as
1652 (let ((parent-widget
1653 (if (and parent-widget (widget-apply parent-widget
1654 :actual-position :from))
1655 (progn (goto-char (widget-apply parent-widget
1656 :actual-position :from))
1658 (let ((got (allout-get-item-widget)))
1660 (allout-decorate-item-and-context got 'redecorate)
1661 (allout-get-or-create-item-widget 'redecorate)))))
1662 (pending-chart (or chart (allout-chart-subtree nil 'visible)))
1664 previous-sibling-point
1666 recent-sibling-point)
1667 (setq pending-chart (nreverse pending-chart))
1668 (dolist (sibling-point pending-chart)
1669 (cond ((integerp sibling-point)
1670 (when (not previous-sibling-point)
1671 (goto-char sibling-point)
1672 (if (setq item-widget (allout-get-item-widget nil))
1673 (allout-decorate-item-and-context item-widget 'redecorate
1675 (allout-get-or-create-item-widget)))
1676 (setq previous-sibling-point sibling-point
1677 recent-sibling-point sibling-point))
1678 ((listp sibling-point)
1681 (allout-redecorate-visible-subtree
1682 (if (not previous-sibling-point)
1683 ;; containment discontinuity - sigh
1685 (allout-get-or-create-item-widget 'redecorate))
1686 (if depth (1- depth))
1688 (if (and recent-sibling-point (< (point) recent-sibling-point))
1689 (goto-char recent-sibling-point))))
1690 ;;;_ > allout-parse-item-at-point (item-widget &optional at-beginning
1691 ;;; blank-container)
1692 (defun allout-parse-item-at-point (item-widget &optional at-beginning
1694 "Set widget ITEM-WIDGET layout parameters per item-at-point's actual layout.
1696 If optional AT-BEGINNING is t, then point is assumed to be at the start of
1699 If optional BLANK-CONTAINER is true, then the parameters of a container
1700 which has an empty body are set. \(Though the body is blank, the object
1701 may have subitems.\)"
1703 ;; Uncomment this sit-for to notice where decoration is happening:
1705 (let* ((depth (allout-depth))
1706 (depth (if blank-container 0 depth))
1707 (is-container (or blank-container (zerop depth)))
1709 (does-encrypt (and (not is-container)
1710 (allout-encrypted-type-prefix)))
1711 (is-encrypted (and does-encrypt (allout-encrypted-topic-p)))
1712 (icon-end allout-recent-prefix-end)
1713 (icon-start (1- icon-end))
1718 (contents-depth (1+ depth))
1720 (widget-put item-widget :depth depth)
1724 (widget-put item-widget :from (allout-set-boundary-marker
1726 (widget-get item-widget :from)))
1727 (widget-put item-widget :icon-end nil)
1728 (widget-put item-widget :icon-start nil)
1729 (setq body-start (widget-put item-widget :body-start 1)))
1733 (widget-put item-widget :from (allout-set-boundary-marker
1734 :from (if at-beginning
1736 allout-recent-prefix-beginning)
1737 (widget-get item-widget :from)))
1738 (widget-put item-widget :icon-start icon-start)
1739 (widget-put item-widget :icon-end icon-end)
1741 (widget-put item-widget :does-encrypt t)
1742 (widget-put item-widget :is-encrypted is-encrypted))
1745 (setq body-start icon-end)
1746 (widget-put item-widget :bullet (setq bullet (allout-get-bullet)))
1747 (if (equal (char-after body-start) ? )
1748 (setq body-start (1+ body-start)))
1749 (widget-put item-widget :body-start body-start)
1752 ;; Both container and regular items:
1754 ;; :body-end (doesn't include a trailing blank line, if any) -
1755 (widget-put item-widget :body-end (setq body-end
1758 (allout-end-of-entry))))
1760 (widget-put item-widget :to (allout-set-boundary-marker
1761 :to (if blank-container
1763 (or (allout-pre-next-prefix)
1764 (goto-char (point-max))))
1765 (widget-get item-widget :to)))
1766 (widget-put item-widget :has-subitems
1769 ;; has a subsequent item:
1770 (not (= body-end (point-max)))
1771 ;; subsequent item is deeper:
1772 (< depth (setq contents-depth (allout-recent-depth))))))
1773 ;; note :expanded - true if widget item's content is currently visible?
1774 (widget-put item-widget :expanded
1776 ;; subsequent item is or isn't visible:
1778 (goto-char allout-recent-prefix-beginning)
1779 (not (allout-hidden-p)))))))
1780 ;;;_ > allout-set-boundary-marker (boundary position &optional current-marker)
1781 (defun allout-set-boundary-marker (boundary position &optional current-marker)
1782 "Set or create item widget BOUNDARY type marker at POSITION.
1784 Optional CURRENT-MARKER is the marker currently being used for
1785 the boundary, if any.
1787 BOUNDARY type is either :from or :to, determining the marker insertion type."
1788 (if (not position) (setq position (point)))
1790 (set-marker current-marker position)
1791 (let ((marker (make-marker)))
1792 ;; XXX dang - would like for :from boundary to advance after inserted
1793 ;; text, but that would omit new header prefixes when allout
1794 ;; relevels, etc. this competes with ad-hoc edits, which would
1795 ;; better be omitted
1796 (set-marker-insertion-type marker nil)
1797 (set-marker marker position))))
1798 ;;;_ > allout-decorate-item-span (item-widget)
1799 (defun allout-decorate-item-span (item-widget)
1800 "Equip the item with a span, as an entirety.
1802 This span is implemented so it can be used to detect displacement
1803 of the widget in absolute terms, and provides an offset bias for
1804 the various element spans."
1806 (if (and (widget-get item-widget :is-container)
1807 ;; the only case where the span could be empty.
1808 (eq (widget-get item-widget :from)
1809 (widget-get item-widget :to)))
1811 (allout-item-span item-widget
1812 (widget-get item-widget :from)
1813 (widget-get item-widget :to))))
1814 ;;;_ > allout-decorate-item-guides (item-widget
1815 ;;; &optional parent-widget has-successor)
1816 (defun allout-decorate-item-guides (item-widget
1817 &optional parent-widget has-successor)
1818 "Add ITEM-WIDGET guide icon-prefix descender and connector text properties.
1820 Optional arguments provide context for deriving the guides. In
1821 their absence, the current guide column flags are used.
1823 Optional PARENT-WIDGET is the widget for the item's parent item.
1825 Optional HAS-SUCCESSOR is true iff the item is followed by a sibling.
1827 We also hide the header-prefix string.
1829 Guides are established according to the item-widget's :guide-column-flags,
1830 when different than :was-guide-column-flags. Changing that property and
1831 reapplying this method will rectify the glyphs."
1833 (when (not (widget-get item-widget :is-container))
1834 (let* ((depth (widget-get item-widget :depth))
1835 (parent-depth (and parent-widget
1836 (widget-get parent-widget :depth)))
1837 (parent-flags (and parent-widget
1838 (widget-get parent-widget :guide-column-flags)))
1839 (parent-flags-depth (length parent-flags))
1840 (extender-length (- depth (+ parent-flags-depth 2)))
1841 (flags (or (and (> depth 1)
1843 (widget-put item-widget :guide-column-flags
1844 (append (list has-successor)
1845 (if (< 0 extender-length)
1846 (make-list extender-length
1849 (widget-get item-widget :guide-column-flags)))
1850 (was-flags (widget-get item-widget :was-guide-column-flags))
1851 (guides-start (widget-get item-widget :from))
1852 (guides-end (widget-get item-widget :icon-start))
1853 (position guides-start)
1854 (increment (length allout-header-prefix))
1857 extenders paint-extenders
1858 (inhibit-read-only t))
1860 (when (not (equal was-flags flags))
1862 (setq reverse-flags (reverse flags))
1863 (while reverse-flags
1865 (cond ((null (cdr reverse-flags))
1866 (if (car reverse-flags)
1869 ((eq (car reverse-flags) '-)
1870 ;; accumulate extenders tally, to be painted on next
1871 ;; non-extender flag, according to the flag type.
1872 (setq extenders (1+ (or extenders 0)))
1874 ((car reverse-flags)
1876 (t 'skip-descender)))
1878 (put-text-property position (setq position (+ position increment))
1879 'display (allout-fetch-icon-image guide-name))
1880 (if (> increment 1) (setq increment 1))
1882 ;; paint extenders after a connector, else leave spaces.
1883 (dotimes (i extenders)
1885 position (setq position (1+ position))
1886 'display (allout-fetch-icon-image
1887 (if (memq guide-name '(mid-connector end-connector))
1890 (setq extenders nil)))
1891 (setq reverse-flags (cdr reverse-flags)))
1892 (widget-put item-widget :was-guide-column-flags flags))
1894 (allout-item-element-span-is item-widget :guides-span
1895 guides-start guides-end))))
1896 ;;;_ > allout-decorate-item-icon (item-widget)
1897 (defun allout-decorate-item-icon (item-widget)
1898 "Add item icon glyph and distinctive bullet text properties to ITEM-WIDGET."
1900 (when (not (widget-get item-widget :is-container))
1901 (let* ((icon-start (widget-get item-widget :icon-start))
1902 (icon-end (widget-get item-widget :icon-end))
1903 (bullet (widget-get item-widget :bullet))
1905 (was-bullet (widget-get item-widget :was-bullet))
1906 (distinctive (allout-distinctive-bullet bullet))
1907 (distinctive-start (widget-get item-widget :distinctive-start))
1908 (distinctive-end (widget-get item-widget :distinctive-end))
1909 (does-encrypt (widget-get item-widget :does-encrypt))
1910 (is-encrypted (and does-encrypt (widget-get item-widget
1912 (expanded (widget-get item-widget :expanded))
1913 (has-subitems (widget-get item-widget :has-subitems))
1914 (inhibit-read-only t)
1917 (when (not (and (equal (widget-get item-widget :was-expanded) expanded)
1918 (equal (widget-get item-widget :was-has-subitems)
1920 (equal (widget-get item-widget :was-does-encrypt)
1922 (equal (widget-get item-widget :was-is-encrypted)
1926 (cond (does-encrypt (if is-encrypted
1928 'unlocked-encrypted))
1930 (has-subitems 'closed)
1932 (put-text-property icon-start (1+ icon-start)
1933 'display (allout-fetch-icon-image icon-state))
1934 (widget-put item-widget :was-expanded expanded)
1935 (widget-put item-widget :was-has-subitems has-subitems)
1936 (widget-put item-widget :was-does-encrypt does-encrypt)
1937 (widget-put item-widget :was-is-encrypted is-encrypted)
1938 ;; preserve as a widget property to track last known:
1939 (widget-put item-widget :icon-state icon-state)
1940 ;; preserve as a text property to track undo:
1941 (put-text-property icon-start icon-end :icon-state icon-state))
1942 (allout-item-element-span-is item-widget :icon-span
1943 icon-start icon-end)
1944 (when (not (string= was-bullet bullet))
1945 (cond ((not distinctive)
1946 ;; XXX we strip the prior properties without even checking if
1947 ;; the prior bullet was distinctive, because the widget
1948 ;; provisions to convey that info is disappearing, sigh.
1949 (remove-text-properties icon-end (1+ icon-end) '(display))
1950 (setq distinctive-start icon-end distinctive-end icon-end)
1951 (widget-put item-widget :distinctive-start distinctive-start)
1952 (widget-put item-widget :distinctive-end distinctive-end))
1954 ((not (string= bullet allout-numbered-bullet))
1955 (setq distinctive-start icon-end distinctive-end (+ icon-end 1)))
1958 (setq distinctive-start icon-end distinctive-end (+ icon-end 1)))
1961 (goto-char icon-end)
1962 (looking-at "[0-9]+")
1963 (setq use-bullet (buffer-substring icon-end (match-end 0)))
1964 (setq distinctive-start icon-end
1965 distinctive-end (match-end 0))))
1966 (put-text-property distinctive-start distinctive-end 'display
1968 (widget-put item-widget :was-bullet bullet)
1969 (widget-put item-widget :distinctive-start distinctive-start)
1970 (widget-put item-widget :distinctive-end distinctive-end)))))
1971 ;;;_ > allout-decorate-item-cue (item-widget)
1972 (defun allout-decorate-item-cue (item-widget)
1973 "Incorporate space between bullet icon and body to the ITEM-WIDGET."
1974 ;; NOTE: most of the cue-area
1976 (when (not (widget-get item-widget :is-container))
1977 (let* ((cue-start (or (widget-get item-widget :distinctive-end)
1978 (widget-get item-widget :icon-end)))
1979 (body-start (widget-get item-widget :body-start))
1980 (expanded (widget-get item-widget :expanded))
1981 (has-subitems (widget-get item-widget :has-subitems))
1982 (inhibit-read-only t))
1984 (allout-item-element-span-is item-widget :cue-span cue-start body-start)
1985 (put-text-property (1- body-start) body-start 'rear-nonsticky t))))
1986 ;;;_ > allout-decorate-item-body (item-widget &optional force)
1987 (defun allout-decorate-item-body (item-widget &optional force)
1988 "Incorporate item body text as part the ITEM-WIDGET.
1990 Optional FORCE means force reassignment of the region property."
1992 (let* ((allout-inhibit-body-modification-hook t)
1993 (body-start (widget-get item-widget :body-start))
1994 (body-end (widget-get item-widget :body-end))
1995 (body-text-end body-end)
1996 (inhibit-read-only t))
1998 (allout-item-element-span-is item-widget :body-span
1999 body-start (min (1+ body-end) (point-max))
2001 ;;;_ > allout-item-actual-position (item-widget field)
2002 (defun allout-item-actual-position (item-widget field)
2003 "Return ITEM-WIDGET FIELD position taking item displacement into account."
2005 ;; The item's sub-element positions (:icon-end, :body-start, etc) are
2006 ;; accurate when the item is parsed, but some offsets from the start
2007 ;; drift with text added in the body.
2009 ;; Rather than reparse an item with every change (inefficient), or derive
2010 ;; every position from a distinct field marker/overlay (prohibitive as
2011 ;; the number of items grows), we use the displacement tracking of the
2012 ;; :span-overlay's markers, against the registered :from or :body-end
2013 ;; (depending on whether the requested field value is before or after the
2014 ;; item body), to bias the registered values.
2016 ;; This is not necessary/useful when the item is being decorated, because
2017 ;; that always must be preceded by a fresh item parse.
2019 (if (not (eq field :body-end))
2020 (widget-get item-widget :from)
2022 (let* ((span-overlay (widget-get item-widget :span-overlay))
2023 (body-end-position (widget-get item-widget :body-end))
2024 (ref-marker-position (and span-overlay
2025 (overlay-end span-overlay)))
2026 (offset (and body-end-position span-overlay
2027 (- (or ref-marker-position 0)
2028 body-end-position))))
2029 (+ (widget-get item-widget field) (or offset 0)))))
2030 ;;;_ : Item undecoration
2031 ;;;_ > allout-widgets-undecorate-region (start end)
2032 (defun allout-widgets-undecorate-region (start end)
2033 "Eliminate widgets and decorations for all items in region from START to END."
2038 (while (< (setq next (next-single-char-property-change next
2044 (when (setq widget (allout-get-item-widget))
2045 ;; if the next-property/overly progression got us to a widget:
2046 (allout-widgets-undecorate-item widget t))))))
2047 ;;;_ > allout-widgets-undecorate-text (text)
2048 (defun allout-widgets-undecorate-text (text)
2049 "Eliminate widgets and decorations for all items in TEXT."
2050 (remove-text-properties 0 (length text)
2051 '(display nil :icon-state nil rear-nonsticky nil
2052 category nil button nil field nil)
2055 ;;;_ > allout-widgets-undecorate-item (item-widget &optional no-expose)
2056 (defun allout-widgets-undecorate-item (item-widget &optional no-expose)
2057 "Remove widget decorations from ITEM-WIDGET.
2059 Any concealed content head lines and item body is exposed, unless
2060 optional NO-EXPOSE is non-nil."
2061 (let ((from (widget-get item-widget :from))
2062 (to (widget-get item-widget :to))
2063 (text-properties-to-remove '(display nil
2069 (span-overlay (widget-get item-widget :span-overlay))
2070 (button-overlay (widget-get item-widget :button))
2071 (was-modified (buffer-modified-p))
2072 (buffer-undo-list t)
2073 (inhibit-read-only t))
2075 (allout-flag-region from to nil))
2077 (remove-text-properties from to text-properties-to-remove))
2079 (delete-overlay span-overlay) (widget-put item-widget :span-overlay nil))
2080 (when button-overlay
2081 (delete-overlay button-overlay) (widget-put item-widget :button nil))
2082 (set-marker from nil)
2084 (if (not was-modified)
2085 (set-buffer-modified-p nil))))
2087 ;;;_ : Item decoration support
2088 ;;;_ > allout-item-span (item-widget &optional start end)
2089 (defun allout-item-span (item-widget &optional start end)
2090 "Return or register the location of an ITEM-WIDGET's actual START and END.
2092 If START and END are not passed in, return either a dotted pair
2093 of the current span, if established, or nil if not yet set.
2095 When the START and END are passed, return the distance that the
2096 start of the item moved. We return 0 if the span was not
2097 previously established or is not moved."
2098 (let ((overlay (widget-get item-widget :span-overlay))
2101 (cond ((not overlay) (when start
2102 (setq overlay (make-overlay start end nil t nil))
2103 (overlay-put overlay 'button item-widget)
2104 (overlay-put overlay 'evaporate t)
2105 (widget-put item-widget :span-overlay overlay)
2108 ((not start) (cons (overlay-start overlay) (overlay-end overlay)))
2110 ((or (not (equal (overlay-start overlay) start))
2111 (not (equal (overlay-end overlay) end)))
2112 (move-overlay overlay start end)
2114 ;; specified span already set:
2116 ;;;_ > allout-item-element-span-is (item-widget element
2117 ;;; &optional start end force)
2118 (defun allout-item-element-span-is (item-widget element
2119 &optional start end force)
2120 "Return or register the location of the indicated ITEM-WIDGET ELEMENT.
2122 ELEMENT is one of :guides-span, :icon-span, :cue-span, or :body-span.
2124 When optional START is specified, optional END must also be.
2126 START and END are the actual bounds of the region, if provided.
2128 If START and END are not passed in, we return either a dotted
2129 pair of the current span, if established, or nil if not yet set.
2131 When the START and END are passed, we return t if the region
2132 changed or nil if not.
2134 Optional FORCE means force assignment of the region's text
2135 property, even if it's already set."
2136 (let ((span (widget-get item-widget element)))
2137 (cond ((or (not span) force)
2139 (widget-put item-widget element (cons start end))
2140 (put-text-property start end 'category
2142 allout-span-to-category)))
2146 ;; move if necessary:
2147 ((not (and (eq (car span) start)
2148 (eq (cdr span) end)))
2149 (widget-put item-widget element span)
2151 ;; specified span already set:
2153 ;;;_ : Item widget retrieval (/ high-level creation):
2154 ;;;_ > allout-get-item-widget (&optional container)
2155 (defun allout-get-item-widget (&optional container)
2156 "Return the widget for the item at point, or nil if no widget yet exists.
2158 Point must be situated *before* the start of the target item's
2159 body, so we don't get an existing containing item when we're in
2160 the process of creating an item in the middle of another.
2162 Optional CONTAINER is used to obtain the container item."
2163 (if (or container (zerop (allout-depth)))
2164 allout-container-item-widget
2165 ;; allout-recent-* are calibrated by (allout-depth) if we got here.
2166 (let ((got (widget-at allout-recent-prefix-beginning)))
2167 (if (and got (listp got))
2168 (if (marker-position (widget-get got :from))
2170 (>= (point) (widget-apply got :actual-position :from))
2171 (<= (point) (widget-apply got :actual-position :body-start))
2173 ;; a wacky residual item - undecorate and disregard:
2174 (allout-widgets-undecorate-item got)
2176 ;;;_ > allout-get-or-create-item-widget (&optional redecorate blank-container)
2177 (defun allout-get-or-create-item-widget (&optional redecorate blank-container)
2178 "Return a widget for the item at point, creating the widget if necessary.
2180 When creating a widget, we assume there has been a context change
2181 and decorate its siblings and parent, as well.
2183 Optional BLANK-CONTAINER is for internal use, to fabricate a
2184 meta-container item with an empty body when the first proper
2185 \(non-container\) item starts at the beginning of the file.
2187 Optional REDECORATE, if non-nil, means to redecorate the widget
2188 if it already exists."
2189 (let ((widget (allout-get-item-widget blank-container))
2190 (buffer-undo-list t))
2191 (cond (widget (if redecorate
2192 (allout-redecorate-item widget))
2194 ((or blank-container (zerop (allout-depth)))
2195 (or allout-container-item-widget
2196 (setq allout-container-item-widget
2197 (allout-decorate-item-and-context
2198 (widget-convert 'allout-item-widget)
2199 nil blank-container))))
2200 ;; create a widget for a regular/non-container item:
2201 (t (allout-decorate-item-and-context (widget-convert
2202 'allout-item-widget))))))
2203 ;;;_ > allout-get-or-create-parent-widget (&optional redecorate)
2204 (defun allout-get-or-create-parent-widget (&optional redecorate)
2205 "Return widget for parent of item at point, decorating it if necessary.
2207 We return the container widget if we're above the first proper item in the
2210 Optional REDECORATE, if non-nil, means to redecorate the widget if it
2213 Point will wind up positioned on the beginning of the parent or beginning
2215 ;; use existing widget, if there, else establish it
2216 (if (or (bobp) (and (not (allout-ascend))
2217 (looking-at allout-regexp)))
2218 (allout-get-or-create-item-widget redecorate 'blank-container)
2219 (allout-get-or-create-item-widget redecorate)))
2220 ;;;_ : X- Item ancillaries
2221 ;;;_ >X allout-body-modification-handler (beg end)
2222 (defun allout-body-modification-handler (beg end)
2223 "Do routine processing of body text before and after modification.
2225 Operation is inhibited by `allout-inhibit-body-modification-handler'."
2227 ;; The primary duties are:
2229 ;; - marking of escaped prefix-like text for delayed cleanup of escapes
2230 ;; - removal and replacement of the settings
2231 ;; - maintenance of beginning-of-line guide lines
2233 ;; ?? Escapes removal \(before changes\) is not done when edits span multiple
2234 ;; items, recognizing that item structure is being preserved, including
2235 ;; escaping of item-prefix-like text within bodies. See
2236 ;; `allout-before-modification-handler' and
2237 ;; `allout-inhibit-body-modification-handler'.
2239 ;; Adds the overlay to the `allout-unresolved-body-mod-workhash' during
2240 ;; before-change operation, and removes from that list during after-change
2242 (cond (allout-inhibit-body-modification-hook nil)))
2243 ;;;_ >X allout-graphics-modification-handler (beg end)
2244 (defun allout-graphics-modification-handler (beg end)
2245 "Protect against incoherent deletion of decoration graphics.
2247 Deletes allowed only when inhibit-read-only is t."
2249 (undo-in-progress (when (eq (get-text-property beg 'category)
2250 'allout-icon-span-category)
2253 (let* ((item-widget (allout-get-item-widget)))
2255 (allout-widgets-exposure-undo-recorder
2257 (inhibit-read-only t)
2258 ((not (and (boundp 'allout-mode) allout-mode)) t)
2259 ((equal this-command 'quoted-insert) t)
2260 ((yes-or-no-p "Unruly edit of outline structure - allow? ")
2261 (setq allout-widgets-unset-inhibit-read-only (not inhibit-read-only)
2262 inhibit-read-only t))
2264 (substitute-command-keys allout-structure-unruly-deletion-message)))))
2265 ;;;_ > allout-item-icon-key-handler ()
2266 (defun allout-item-icon-key-handler ()
2267 "Catchall handling of key bindings in item icon/cue hot-spots.
2269 Applies `allout-hotspot-key-handler' and calls the result, if any, as an
2270 interactive command."
2273 (let* ((mapped-binding (allout-hotspot-key-handler)))
2274 (when mapped-binding
2275 (call-interactively mapped-binding))))
2278 ;;;_ . allout-item-location (item-widget)
2279 (defun allout-item-location (item-widget)
2280 "Location of the start of the item's text."
2281 (overlay-start (widget-get item-widget :span-overlay)))
2283 ;;;_ : Icon management
2284 ;;;_ > allout-fetch-icon-image (name)
2285 (defun allout-fetch-icon-image (name)
2286 "Fetch allout icon for symbol NAME.
2288 We use a caching strategy, so the caller doesn't need to do so."
2289 (let* ((types allout-widgets-icon-types)
2290 (use-dir (if (equal (allout-frame-property nil 'background-mode)
2292 allout-widgets-icons-light-subdir
2293 allout-widgets-icons-dark-subdir))
2294 (key (list name use-dir))
2295 (got (assoc key allout-widgets-icons-cache)))
2297 ;; display system shows only first of subsequent adjacent
2298 ;; `eq'-identical repeats - use copies to avoid this problem.
2299 (allout-widgets-copy-list (cadr got))
2300 (while (and types (not got))
2303 (list (append (list :type (car types)
2304 :file (concat use-dir
2308 (if (featurep 'xemacs)
2309 allout-widgets-item-image-properties-xemacs
2310 allout-widgets-item-image-properties-emacs)
2312 (setq types (cdr types)))
2314 (push (list key got) allout-widgets-icons-cache))
2317 ;;;_ : Miscellaneous
2318 ;;;_ > allout-elapsed-time-seconds (triple)
2319 (defun allout-elapsed-time-seconds (end start)
2320 "Return seconds between `current-time' style time START/END triples."
2321 (let ((elapsed (time-subtract end start)))
2322 (+ (* (car elapsed) (expt 2.0 16))
2324 (/ (caddr elapsed) (expt 10.0 6)))))
2325 ;;;_ > allout-frame-property (frame property)
2326 (defalias 'allout-frame-property
2327 (cond ((fboundp 'frame-parameter)
2329 ((fboundp 'frame-property)
2332 ;;;_ > allout-find-image (specs)
2333 (defalias 'allout-find-image
2334 (if (fboundp 'find-image)
2336 nil) ; aka, not-yet-implemented for xemacs.
2338 ;;;_ > allout-widgets-copy-list (list)
2339 (defun allout-widgets-copy-list (list)
2340 ;; duplicated from cl.el 'copy-list' as of 2008-08-17
2341 "Return a copy of LIST, which may be a dotted list.
2342 The elements of LIST are not copied, just the list structure itself."
2345 (while (consp list) (push (pop list) res))
2346 (prog1 (nreverse res) (setcdr res list)))
2348 ;;;_ . allout-widgets-count-buttons-in-region (start end)
2349 (defun allout-widgets-count-buttons-in-region (start end)
2350 "Debugging/diagnostic tool - count overlays with 'button' property in region."
2352 (setq start (or start (point-min))
2353 end (or end (point-max)))
2354 (if (> start end) (let ((interim start)) (setq start end end interim)))
2355 (let ((button-overlays (delq nil
2356 (mapcar (function (lambda (o)
2357 (if (overlay-get o 'button)
2359 (overlays-in start end)))))
2360 (length button-overlays)))
2362 ;;;_ : Run unit tests:
2363 (defun allout-widgets-run-unit-tests ()
2364 (message "Running allout-widget tests...")
2366 (allout-test-range-overlaps)
2368 (message "Running allout-widget tests... Done.")
2371 (when allout-widgets-run-unit-tests-on-load
2372 (allout-widgets-run-unit-tests))
2375 (provide 'allout-widgets)
2377 ;;;_. Local emacs vars.
2378 ;;;_ , Local variables:
2379 ;;;_ , allout-layout: (-1 : 0)