1 ;; allout-widgets.el --- Visually highlight allout outline structure.
3 ;; Copyright (C) 2005-2012 Free Software Foundation, Inc.
5 ;; Author: Ken Manheimer <ken dot manheimer at gmail...>
6 ;; Maintainer: Ken Manheimer <ken dot manheimer at gmail...>
10 ;; Website: http://myriadicity.net/software-and-systems/craft/emacs-allout
14 ;; This is an allout outline-mode add-on that highlights outline structure
15 ;; with graphical widgets.
17 ;; To activate, customize `allout-widgets-auto-activation'. You can also
18 ;; invoke allout-widgets-mode in a particular allout buffer. When
19 ;; auto-enabled, you can inhibit widget operation in particular allout
20 ;; buffers by setting the variable `allout-widgets-mode-inhibit' non-nil in
21 ;; that file's buffer. Use emacs *file local variables* to generally
22 ;; inhibit for a file.
24 ;; See the `allout-widgets-mode' docstring for more details.
26 ;; Info about allout and allout-widgets development are available at
27 ;; http://myriadicity.net/Sundry/EmacsAllout
29 ;; The graphics include:
31 ;; - icons for item bullets, varying to distinguish whether the item either
32 ;; lacks any subitems, the subitems are currently collapsed within the
33 ;; item, or the item is currently expanded.
35 ;; - guide lines connecting item bullet-icons with those of their subitems.
37 ;; - cue area between the bullet-icon and the start of the body headline,
38 ;; for item numbering, encryption indicator, and distinctive bullets.
40 ;; The bullet-icon and guide line graphics provide keybindings and mouse
41 ;; bindings for easy outline navigation and exposure control, extending
42 ;; outline hot-spot navigation (see `allout-mode' docstring for details).
44 ;; Developers note: Our use of emacs widgets is unconventional. We
45 ;; decorate existing text rather than substituting for it, to
46 ;; piggy-back on existing allout operation. This employs the C-coded
47 ;; efficiencies of widget-apply, widget-get, and widget-put, along
48 ;; with the basic object-oriented organization of widget-create, to
49 ;; systematically couple overlays, graphics, and other features with
50 ;; allout-governed text.
52 ;;;_: Code (structured with comments that delineate an allout outline)
54 ;;;_ : General Environment
65 ;;;_ : internal variables needed before user-customization variables
66 ;;; In order to enable activation of allout-widgets-mode via customization,
67 ;;; allout-widgets-auto-activation uses a setting function. That function
68 ;;; is invoked when the customization variable definition is evaluated,
69 ;;; during file load, so the involved code must reside above that
70 ;;; definition in the file.
71 ;;;_ = allout-widgets-mode
72 (defvar allout-widgets-mode nil
73 "Allout mode enhanced with graphical widgets.")
74 (make-variable-buffer-local 'allout-widgets-mode
)
76 ;;;_ : USER CUSTOMIZATION VARIABLES and incidental functions:
77 ;;;_ > defgroup allout-widgets
79 (defgroup allout-widgets nil
80 "Allout extension that highlights outline structure graphically.
82 Customize `allout-widgets-auto-activation' to activate allout-widgets
85 ;;;_ > defgroup allout-widgets-developer
86 (defgroup allout-widgets-developer nil
87 "Settings for development of allout widgets extension."
88 :group
'allout-widgets
)
89 ;;;_ ; some functions a bit early, for allout-auto-activation dependency:
90 ;;;_ > allout-widgets-mode-enable
91 (defun allout-widgets-mode-enable ()
92 "Enable allout-widgets-mode in allout-mode buffers.
94 See `allout-widgets-mode-inhibit' for per-file/per-buffer
95 inhibition of allout-widgets-mode."
96 (add-hook 'allout-mode-off-hook
'allout-widgets-mode-off
)
97 (add-hook 'allout-mode-on-hook
'allout-widgets-mode-on
)
99 ;;;_ > allout-widgets-mode-disable
100 (defun allout-widgets-mode-disable ()
101 "Disable allout-widgets-mode in allout-mode buffers.
103 See `allout-widgets-mode-inhibit' for per-file/per-buffer
104 inhibition of allout-widgets-mode."
105 (remove-hook 'allout-mode-off-hook
'allout-widgets-mode-off
)
106 (remove-hook 'allout-mode-on-hook
'allout-widgets-mode-on
)
108 ;;;_ > allout-widgets-setup (varname value)
110 (defun allout-widgets-setup (varname value
)
111 "Commission or decommission allout-widgets-mode along with allout-mode.
113 Meant to be used by customization of `allout-widgets-auto-activation'."
114 (set-default varname value
)
115 (if allout-widgets-auto-activation
116 (allout-widgets-mode-enable)
117 (allout-widgets-mode-disable)))
118 ;;;_ = allout-widgets-auto-activation
120 (defcustom allout-widgets-auto-activation nil
121 "Activate to enable allout icon graphics wherever allout mode is active.
123 Also enable `allout-auto-activation' for this to take effect upon
126 When this is set you can disable allout widgets in select files
127 by setting `allout-widgets-mode-inhibit'
129 Instead of setting `allout-widgets-auto-activation' you can
130 explicitly invoke `allout-widgets-mode' in allout buffers where
131 you want allout widgets operation.
133 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."
161 :group
'allout-widgets
)
162 ;;;_ = allout-widgets-icons-light-subdir
163 (defcustom allout-widgets-icons-light-subdir
"icons/allout-widgets/light-bg/"
164 "Directory on `image-load-path' holding allout icons for light backgrounds."
167 :group
'allout-widgets
)
168 ;;;_ = allout-widgets-icon-types
169 (defcustom allout-widgets-icon-types
'(xpm png
)
170 "File extensions for the icon graphic format types, in order of preference."
172 :type
'(repeat symbol
)
173 :group
'allout-widgets
)
175 ;;;_ . Decoration format
176 ;;;_ = allout-widgets-theme-dark-background
177 (defcustom allout-widgets-theme-dark-background
"allout-dark-bg"
178 "Identify the outline's icon theme to use with a dark background."
181 :group
'allout-widgets
)
182 ;;;_ = allout-widgets-theme-light-background
183 (defcustom allout-widgets-theme-light-background
"allout-light-bg"
184 "Identify the outline's icon theme to use with a light background."
187 :group
'allout-widgets
)
188 ;;;_ = allout-widgets-item-image-properties-emacs
189 (defcustom allout-widgets-item-image-properties-emacs
190 '(:ascent center
:mask
(heuristic t
))
191 "*Default properties item widget images in mainline Emacs."
194 :group
'allout-widgets
)
195 ;;;_ = allout-widgets-item-image-properties-xemacs
196 (defcustom allout-widgets-item-image-properties-xemacs
198 "*Default properties item widget images in XEmacs."
201 :group
'allout-widgets
)
203 ;;;_ = allout-widgets-run-unit-tests-on-load
204 (defcustom allout-widgets-run-unit-tests-on-load nil
205 "*When non-nil, unit tests will be run at end of loading allout-widgets.
207 Generally, allout widgets code developers are the only ones who'll want to
210 \(If set, this makes it an even better practice to exercise changes by
211 doing byte-compilation with a repeat count, so the file is loaded after
214 See `allout-widgets-run-unit-tests' to see what's run."
217 :group
'allout-widgets-developer
)
218 ;;;_ = allout-widgets-time-decoration-activity
219 (defcustom allout-widgets-time-decoration-activity nil
220 "*Retain timing info of the last cooperative redecoration.
222 The details are retained as the value of
223 `allout-widgets-last-decoration-timing'.
225 Generally, allout widgets code developers are the only ones who'll want to
229 :group
'allout-widgets-developer
)
230 ;;;_ = allout-widgets-hook-error-post-time 0
231 (defcustom allout-widgets-hook-error-post-time
0
232 "*Amount of time to sit showing hook error messages.
234 0 is minimal, or nil to not post to the message area.
236 This is for debugging purposes."
239 :group
'allout-widgets-developer
)
240 ;;;_ = allout-widgets-maintain-tally nil
241 (defcustom allout-widgets-maintain-tally nil
242 "*If non-nil, maintain a collection of widgets, `allout-widgets-tally'.
244 This is for debugging purposes.
246 The tally shows the total number of item widgets in the current
247 buffer, and tracking increases as new widgets are added and
248 decreases as obsolete widgets are garbage collected."
251 :group
'allout-widgets-developer
)
252 (defvar allout-widgets-tally nil
253 "Hash-table of existing allout widgets, for debugging.
255 Table is maintained iff `allout-widgets-maintain-tally' is non-nil.
257 The table contents will be out of sync if any widgets are created
258 or deleted while this variable is nil.")
259 (make-variable-buffer-local 'allout-widgets-tally
)
260 (defvar allout-widgets-mode-inhibit
) ; defined below
261 ;;;_ > allout-widgets-tally-string
262 (defun allout-widgets-tally-string ()
263 "Return a string giving the number of tracked widgets, or empty string if not tracking.
265 The string is formed for appending to the allout-mode mode-line lighter.
267 An empty string is also returned if tracking is inhibited or
268 widgets are locally inhibited.
270 The number varies according to the evanescence of objects on a
271 hash table with weak keys, so tracking of widget erasures is often delayed."
272 (when (and allout-widgets-maintain-tally
273 (not allout-widgets-mode-inhibit
)
274 allout-widgets-tally
)
275 (format ":%s" (hash-table-count allout-widgets-tally
))))
276 ;;;_ = allout-widgets-track-decoration nil
277 (defcustom allout-widgets-track-decoration nil
278 "*If non-nil, show cursor position of each item decoration.
280 This is for debugging purposes, and generally set at need in a
281 buffer rather than as a prevailing configuration \(but it's handy
282 to publicize it by making it a customization variable\)."
285 :group
'allout-widgets-developer
)
286 (make-variable-buffer-local 'allout-widgets-track-decoration
)
288 ;;;_ : Mode context - variables, hookup, and hooks
289 ;;;_ . internal mode variables
290 ;;;_ , Mode activation and environment
291 ;;;_ = allout-widgets-version
292 (defvar allout-widgets-version
"1.0"
293 "Version of currently loaded allout-widgets extension.")
294 ;;;_ > allout-widgets-version
295 (defun allout-widgets-version (&optional here
)
296 "Return string describing the loaded outline version."
298 (let ((msg (concat "Allout Outline Widgets Extension v "
299 allout-widgets-version
)))
300 (if here
(insert msg
))
303 ;;;_ = allout-widgets-mode-inhibit
304 (defvar allout-widgets-mode-inhibit nil
305 "Inhibit `allout-widgets-mode' from activating widgets.
307 This also inhibits automatic adjustment of widgets to track allout outline
310 You can use this as a file local variable setting to disable
311 allout widgets enhancements in selected buffers while generally
312 enabling widgets by customizing `allout-widgets-auto-activation'.
314 In addition, you can invoked `allout-widgets-mode' allout-mode
315 buffers where this is set to enable and disable widget
316 enhancements, directly.")
318 (put 'allout-widgets-mode-inhibit
'safe-local-variable
319 (if (fboundp 'booleanp
) 'booleanp
(lambda (x) (member x
'(t nil
)))))
320 (make-variable-buffer-local 'allout-widgets-mode-inhibit
)
321 ;;;_ = allout-inhibit-body-modification-hook
322 (defvar allout-inhibit-body-modification-hook nil
323 "Override de-escaping of text-prefixes in item bodies during specific changes.
325 This is used by `allout-buffer-modification-handler' to signal such changes
326 to `allout-body-modification-handler', and is always reset by
327 `allout-post-command-business'.")
328 (make-variable-buffer-local 'allout-inhibit-body-modification-hook
)
329 ;;;_ = allout-widgets-icons-cache
330 (defvar allout-widgets-icons-cache nil
331 "Cache allout icon images, as an association list.
333 `allout-fetch-icon-image' uses this cache transparently, keying
334 images with lists containing the name of the icon directory \(as
335 found on the `load-path') and the icon name.
337 Set this variable to `nil' to empty the cache, and have it replenish from the
339 ;;;_ = allout-widgets-unset-inhibit-read-only
340 (defvar allout-widgets-unset-inhibit-read-only nil
341 "Tell `allout-widgets-post-command-business' to unset `inhibit-read-only'.
343 Used by `allout-graphics-modification-handler'")
344 ;;;_ = allout-widgets-reenable-before-change-handler
345 (defvar allout-widgets-reenable-before-change-handler nil
346 "Tell `allout-widgets-post-command-business' to reequip the handler.
348 Necessary because the handler sometimes deliberately raises an
349 error, causing it to be disabled.")
350 ;;;_ , State for hooks
351 ;;;_ = allout-unresolved-body-mod-workroster
352 (defvar allout-unresolved-body-mod-workroster
(make-hash-table :size
16)
353 "List of body-overlays that did before-change business but not after-change.
355 See `allout-post-command-business' and `allout-body-modification-handler'.")
356 ;;;_ = allout-structure-unruly-deletion-message
357 (defvar allout-structure-unruly-deletion-message
358 "Unruly edit prevented --
359 To change the bullet character: \\[allout-rebullet-current-heading]
360 To promote this item: \\[allout-shift-out]
361 To demote it: \\[allout-shift-in]
362 To delete it and offspring: \\[allout-kill-topic]
363 See \\[describe-mode] for many more options."
364 "Informative message presented on improper editing of outline structure.
366 The structure includes the guides lines, bullet, and bullet cue.")
367 ;;;_ = allout-widgets-changes-record
368 (defvar allout-widgets-changes-record nil
369 "Record outline changes for processing by post-command hook.
371 Entries on the list are lists whose first element is a symbol indicating
372 the change type and subsequent elements are data specific to that change
375 'exposure `allout-exposure-from' `allout-exposure-to' `allout-exposure-flag'
377 The changes are recorded in reverse order, with new values pushed
379 (make-variable-buffer-local 'allout-widgets-changes-record
)
380 ;;;_ = allout-widgets-undo-exposure-record
381 (defvar allout-widgets-undo-exposure-record nil
382 "Record outline undo traces for processing by post-command hook.
384 The changes are recorded in reverse order, with new values pushed
386 (make-variable-buffer-local 'allout-widgets-undo-exposure-record
)
387 ;;;_ = allout-widgets-last-hook-error
388 (defvar allout-widgets-last-hook-error nil
389 "String holding last error string, for debugging purposes.")
390 ;;;_ = allout-widgets-adjust-message-length-threshold 100
391 (defvar allout-widgets-adjust-message-length-threshold
100
392 "Display \"Adjusting widgets\" message above this number of pending changes."
394 ;;;_ = allout-widgets-adjust-message-size-threshold 10000
395 (defvar allout-widgets-adjust-message-size-threshold
10000
396 "Display \"Adjusting widgets\" message above this size of pending changes."
398 ;;;_ = allout-doing-exposure-undo-processor nil
399 (defvar allout-undo-exposure-in-progress nil
400 "Maintained true during `allout-widgets-exposure-undo-processor'")
401 ;;;_ , Widget-specific outline text format
402 ;;;_ = allout-escaped-prefix-regexp
403 (defvar allout-escaped-prefix-regexp
""
404 "*Regular expression for body text that would look like an item prefix if
405 not altered with an escape sequence.")
406 (make-variable-buffer-local 'allout-escaped-prefix-regexp
)
407 ;;;_ , Widget element formatting
408 ;;;_ = allout-item-icon-keymap
409 (defvar allout-item-icon-keymap
410 (let ((km (make-sparse-keymap)))
411 (dolist (digit '("0" "1" "2" "3"
412 "4" "5" "6" "7" "8" "9"))
413 (define-key km digit
'digit-argument
))
414 (define-key km
"-" 'negative-argument
)
415 ;; (define-key km [(return)] 'allout-tree-expand-command)
416 ;; (define-key km [(meta return)] 'allout-toggle-torso-command)
417 ;; (define-key km [(down-mouse-1)] 'allout-item-button-click)
418 ;; (define-key km [(down-mouse-2)] 'allout-toggle-torso-event-command)
419 ;; Override underlying mouse-1 and mouse-2 bindings in icon territory:
420 (define-key km
[(mouse-1)] (lambda () (interactive) nil
))
421 (define-key km
[(mouse-2)] (lambda () (interactive) nil
))
423 ;; Catchall, handles actual keybindings, dynamically doing keymap lookups:
424 (define-key km
[t] 'allout-item-icon-key-handler)
427 "General tree-node key bindings.")
428 ;;;_ = allout-item-body-keymap
429 (defvar allout-item-body-keymap
430 (let ((km (make-sparse-keymap))
431 (local-map (current-local-map)))
432 ;; (define-key km [(control return)] 'allout-tree-expand-command)
433 ;; (define-key km [(meta return)] 'allout-toggle-torso-command)
434 ;; XXX We need to reset this per buffer's mode; we do so in
435 ;; allout-widgets-mode.
437 (set-keymap-parent km local-map))
440 "General key bindings for the text content of outline items.")
441 (make-variable-buffer-local 'allout-item-body-keymap)
442 ;;;_ = allout-body-span-category
443 (defvar allout-body-span-category nil
444 "Symbol carrying allout body-text overlay properties.")
445 ;;;_ = allout-cue-span-keymap
446 (defvar allout-cue-span-keymap
447 (let ((km (make-sparse-keymap)))
448 (set-keymap-parent km allout-item-icon-keymap)
450 "Keymap used in the item cue area - the space between the icon and headline.")
451 ;;;_ = allout-escapes-category
452 (defvar allout-escapes-category nil
453 "Symbol for category of text property used to hide escapes of prefix-like
454 text in allout item bodies.")
455 ;;;_ = allout-guides-category
456 (defvar allout-guides-category nil
457 "Symbol carrying allout icon-guides overlay properties.")
458 ;;;_ = allout-guides-span-category
459 (defvar allout-guides-span-category nil
460 "Symbol carrying allout icon and guide lines overlay properties.")
461 ;;;_ = allout-icon-span-category
462 (defvar allout-icon-span-category nil
463 "Symbol carrying allout icon and guide lines overlay properties.")
464 ;;;_ = allout-cue-span-category
465 (defvar allout-cue-span-category nil
466 "Symbol carrying common properties of the space following the outline icon.
468 \(That space is used to convey selected cues indicating body qualities,
469 including things like:
472 - indirect reference '@'
473 - distinctive bullets - see `allout-distinctive-bullets-string'.\)")
474 ;;;_ = allout-span-to-category
475 (defvar allout-span-to-category
476 '((:guides-span . allout-guides-span-category)
477 (:cue-span . allout-cue-span-category)
478 (:icon-span . allout-icon-span-category)
479 (:body-span . allout-body-span-category))
480 "Association list mapping span identifier to category identifier.")
481 ;;;_ = allout-trailing-category
482 (defvar allout-trailing-category nil
483 "Symbol carrying common properties of an overlay's trailing newline.")
485 (defvar allout-widgets-last-decoration-timing nil
486 "Timing details for the last cooperative decoration action.
488 This is maintained when `allout-widgets-time-decoration-activity' is set.
490 The value is a list containing two elements:
491 - the elapsed time as a number of seconds
492 - the list of changes processed, a la `allout-widgets-changes-record'.
494 When active, the value is revised each time automatic decoration activity
495 happens in the buffer.")
496 (make-variable-buffer-local 'allout-widgets-last-decoration-timing)
498 ;;;_ > define-minor-mode allout-widgets-mode (arg)
500 (define-minor-mode allout-widgets-mode
501 "Toggle Allout Widgets mode.
502 With a prefix argument ARG, enable Allout Widgets mode if ARG is
503 positive, and disable it otherwise. If called from Lisp, enable
504 the mode if ARG is omitted or nil.
506 Allout Widgets mode is an extension of Allout mode that provides
507 graphical decoration of outline structure. It is meant to
508 operate along with `allout-mode', via `allout-mode-hook'.
510 The graphics include:
512 - guide lines connecting item bullet-icons with those of their subitems.
514 - icons for item bullets, varying to indicate whether or not the item
515 has subitems, and if so, whether or not the item is expanded.
517 - cue area between the bullet-icon and the start of the body headline,
518 for item numbering, encryption indicator, and distinctive bullets.
520 The bullet-icon and guide line graphics provide keybindings and mouse
521 bindings for easy outline navigation and exposure control, extending
522 outline hot-spot navigation \(see `allout-mode')."
527 ;; define-minor-mode handles any provided argument according to emacs
528 ;; minor-mode conventions - '(elisp) Minor Mode Conventions' - and sets
529 ;; allout-widgets-mode accordingly *before* running the body code, so we
531 (if allout-widgets-mode
534 (allout-add-resumptions
535 ;; XXX user may need say in line-truncation/hscrolling - an option
536 ;; that abstracts mode.
537 ;; truncate text lines to keep guide lines intact:
539 ;; and enable autoscrolling to ease view of text
540 '(auto-hscroll-mode t)
541 '(line-move-ignore-fields t)
542 '(widget-push-button-prefix "")
543 '(widget-push-button-suffix "")
544 ;; allout-escaped-prefix-regexp depends on allout-regexp:
545 (list 'allout-escaped-prefix-regexp (concat "\\(\\\\\\)"
546 "\\(" allout-regexp "\\)")))
547 (allout-add-resumptions
548 (list 'allout-widgets-tally allout-widgets-tally)
549 (list 'allout-widgets-escapes-sanitization-regexp-pair
550 (list (concat "\\(\n\\|\\`\\)"
551 allout-escaped-prefix-regexp
553 ;; Include everything but the escape symbol.
557 (add-hook 'after-change-functions 'allout-widgets-after-change-handler
560 (allout-setup-text-properties)
561 (add-to-invisibility-spec '(allout-torso . t))
562 (add-to-invisibility-spec 'allout-escapes)
564 (if (current-local-map)
565 (set-keymap-parent allout-item-body-keymap (current-local-map)))
567 (add-hook 'allout-exposure-change-hook
568 'allout-widgets-exposure-change-recorder nil 'local)
569 (add-hook 'allout-structure-added-hook
570 'allout-widgets-additions-recorder nil 'local)
571 (add-hook 'allout-structure-deleted-hook
572 'allout-widgets-deletions-recorder nil 'local)
573 (add-hook 'allout-structure-shifted-hook
574 'allout-widgets-shifts-recorder nil 'local)
575 (add-hook 'allout-after-copy-or-kill-hook
576 'allout-widgets-after-copy-or-kill-function nil 'local)
577 (add-hook 'allout-post-undo-hook
578 'allout-widgets-after-undo-function nil 'local)
580 (add-hook 'before-change-functions 'allout-widgets-before-change-handler
582 (add-hook 'post-command-hook 'allout-widgets-post-command-business
584 (add-hook 'pre-command-hook 'allout-widgets-pre-command-business
587 ;; init the widgets tally for debugging:
588 (if (not allout-widgets-tally)
589 (setq allout-widgets-tally (make-hash-table
590 :test 'eq :weakness 'key)))
591 ;; add tally count display on minor-mode-alist just after
592 ;; allout-mode entry.
593 ;; (we use ternary condition form to keep condition simple for deletion.)
594 (let* ((mode-line-entry '(allout-widgets-mode-inhibit ""
595 (:eval (allout-widgets-tally-string))))
596 (associated (assoc (car mode-line-entry) minor-mode-alist))
597 ;; need location for it only if not already present:
598 (after (and (not associated)
599 (memq (assq 'allout-mode minor-mode-alist) minor-mode-alist))))
601 (rplacd after (cons mode-line-entry (cdr after)))))
602 (allout-widgets-prepopulate-buffer)
605 (let ((inhibit-read-only t)
606 (was-modified (buffer-modified-p)))
608 (allout-widgets-undecorate-region (point-min)(point-max))
609 (remove-from-invisibility-spec '(allout-torso . t))
610 (remove-from-invisibility-spec 'allout-escapes)
612 (remove-hook 'after-change-functions
613 'allout-widgets-after-change-handler 'local)
614 (remove-hook 'allout-exposure-change-hook
615 'allout-widgets-exposure-change-recorder 'local)
616 (remove-hook 'allout-structure-added-hook
617 'allout-widgets-additions-recorder 'local)
618 (remove-hook 'allout-structure-deleted-hook
619 'allout-widgets-deletions-recorder 'local)
620 (remove-hook 'allout-structure-shifted-hook
621 'allout-widgets-shifts-recorder 'local)
622 (remove-hook 'allout-after-copy-or-kill-hook
623 'allout-widgets-after-copy-or-kill-function 'local)
624 (remove-hook 'before-change-functions
625 'allout-widgets-before-change-handler 'local)
626 (remove-hook 'post-command-hook
627 'allout-widgets-post-command-business 'local)
628 (remove-hook 'pre-command-hook
629 'allout-widgets-pre-command-business 'local)
630 (assq-delete-all 'allout-widgets-mode-inhibit minor-mode-alist)
631 (set-buffer-modified-p was-modified))))
632 ;;;_ > allout-widgets-mode-off
633 (defun allout-widgets-mode-off ()
634 "Explicitly disable allout-widgets-mode."
635 (allout-widgets-mode -1))
636 ;;;_ > allout-widgets-mode-off
637 (defun allout-widgets-mode-on ()
638 "Explicitly disable allout-widgets-mode."
639 (allout-widgets-mode 1))
640 ;;;_ > allout-setup-text-properties ()
641 (defun allout-setup-text-properties ()
642 "Configure category and literal text properties."
644 ;; XXX body - before-change, entry, keymap
646 (setplist 'allout-guides-span-category nil)
647 (put 'allout-guides-span-category
648 'modification-hooks '(allout-graphics-modification-handler))
649 (put 'allout-guides-span-category 'local-map allout-item-icon-keymap)
650 (put 'allout-guides-span-category 'mouse-face widget-button-face)
651 (put 'allout-guides-span-category 'field 'structure)
652 ;; (put 'allout-guides-span-category 'face 'widget-button)
654 (setplist 'allout-icon-span-category
655 (allout-widgets-copy-list (symbol-plist
656 'allout-guides-span-category)))
657 (put 'allout-icon-span-category 'field 'structure)
659 ;; XXX for body text we're instead going to use the buffer-wide
660 ;; resources, like before/after-change-functions hooks and the
661 ;; buffer's key map. that way we won't have to do painful provisions
662 ;; to fixup things after edits, catch outlier interstitial
663 ;; characters, like newline and empty lines after hidden subitems,
665 (setplist 'allout-body-span-category nil)
666 (put 'allout-body-span-category 'evaporate t)
667 (put 'allout-body-span-category 'local-map allout-item-body-keymap)
668 ;;(put 'allout-body-span-category
669 ;; 'modification-hooks '(allout-body-modification-handler))
670 ;;(put 'allout-body-span-category 'field 'body)
672 (setplist 'allout-cue-span-category nil)
673 (put 'allout-cue-span-category 'evaporate t)
674 (put 'allout-cue-span-category
675 'modification-hooks '(allout-body-modification-handler))
676 (put 'allout-cue-span-category 'local-map allout-cue-span-keymap)
677 (put 'allout-cue-span-category 'mouse-face widget-button-face)
678 (put 'allout-cue-span-category 'pointer 'arrow)
679 (put 'allout-cue-span-category 'field 'structure)
681 (setplist 'allout-trailing-category nil)
682 (put 'allout-trailing-category 'evaporate t)
683 (put 'allout-trailing-category 'local-map allout-item-body-keymap)
685 (setplist 'allout-escapes-category nil)
686 (put 'allout-escapes-category 'invisible 'allout-escapes)
687 (put 'allout-escapes-category 'evaporate t))
688 ;;;_ > allout-widgets-prepopulate-buffer ()
689 (defun allout-widgets-prepopulate-buffer ()
690 "Step over the current buffers exposed items to do initial widgetizing."
691 (if (not allout-widgets-mode-inhibit)
693 (goto-char (point-min))
694 (while (allout-next-visible-heading 1)
695 (when (not (widget-at (point)))
696 (allout-get-or-create-item-widget))))))
697 ;;;_ . settings context
698 ;;;_ = allout-container-item
699 (defvar allout-container-item-widget nil
700 "A widget for the current outline's overarching container as an item.
702 The item has settings \(of the file/connection\) and maybe a body, but no
704 (make-variable-buffer-local 'allout-container-item-widget)
705 ;;;_ . Hooks and hook helpers
706 ;;;_ , major command-loop business:
707 ;;;_ > allout-widgets-pre-command-business (&optional recursing)
708 (defun allout-widgets-pre-command-business (&optional recursing)
709 "Handle actions pending before allout-mode activity."
711 ;;;_ > allout-widgets-post-command-business (&optional recursing)
712 (defun allout-widgets-post-command-business (&optional recursing)
713 "Handle actions pending after any allout-mode commands.
715 Optional RECURSING is for internal use, to limit recursion."
716 ;; - check changed text for nesting discontinuities and escape anything
717 ;; that's: (1) asterisks at bol or (2) excessively nested.
718 (condition-case failure
720 (when (and (boundp 'allout-mode) allout-mode)
722 (if allout-widgets-unset-inhibit-read-only
723 (setq inhibit-read-only nil
724 allout-widgets-unset-inhibit-read-only nil))
726 (when allout-widgets-reenable-before-change-handler
727 (add-hook 'before-change-functions
728 'allout-widgets-before-change-handler
730 (setq allout-widgets-reenable-before-change-handler nil))
732 (when (or allout-widgets-undo-exposure-record
733 allout-widgets-changes-record)
734 (let* ((debug-on-signal t)
736 ;; inhibit recording new undo records when processing
737 ;; effects of undo-exposure:
738 (debugger 'allout-widgets-hook-error-handler)
739 (adjusting-message " Adjusting widgets...")
740 (replaced-message (allout-widgets-adjusting-message
742 (start-time (current-time)))
744 (if allout-widgets-undo-exposure-record
745 ;; inhibit undo recording iff undoing exposure stuff.
746 ;; XXX we might need to inhibit per respective
747 ;; change-record, rather than assuming that some undo
748 ;; activity during a command is all undo activity.
749 (let ((buffer-undo-list t))
750 (allout-widgets-exposure-undo-processor)
751 (allout-widgets-changes-dispatcher))
752 (allout-widgets-exposure-undo-processor)
753 (allout-widgets-changes-dispatcher))
755 (if allout-widgets-time-decoration-activity
756 (setq allout-widgets-last-decoration-timing
757 (list (allout-elapsed-time-seconds (current-time)
759 allout-widgets-changes-record)))
761 (setq allout-widgets-changes-record nil)
764 (if (stringp replaced-message)
765 (message replaced-message)
768 ;; alas, decorated intermediate matches are not easily undecorated
769 ;; when they're automatically rehidden by isearch, so we're
770 ;; dropping this nicety.
771 ;; ;; Detect undecorated items, eg during isearch into previously
772 ;; ;; unexposed topics, and decorate "economically". Some
773 ;; ;; undecorated stuff is often exposed, to reduce lag, but the
774 ;; ;; item containing the cursor is decorated. We constrain
775 ;; ;; recursion to avoid being trapped by unexpectedly undecoratable
777 ;; (when (and (not recursing)
778 ;; (not (allout-current-decorated-p))
779 ;; (or (not (equal (allout-depth) 0))
780 ;; (not allout-container-item-widget)))
781 ;; (let ((buffer-undo-list t))
782 ;; (allout-widgets-exposure-change-recorder
783 ;; allout-recent-prefix-beginning allout-recent-prefix-end nil)
784 ;; (allout-widgets-post-command-business 'recursing)))
786 ;; Detect and rectify fouled outline structure - decorated item
787 ;; not at beginning of line.
788 (let ((this-widget (or (widget-at (point))
789 ;; XXX we really should be checking across
790 ;; edited span, not just point and point+1
791 (and (not (eq (point) (point-max)))
792 (widget-at (1+ (point))))))
796 (goto-char (widget-get this-widget :from))
801 (concat "Misplaced item won't be recognizable "
802 " as part of outline - rectify? "))
805 (if (allout-hidden-p (max (1- (point)) 1))
807 (goto-char (max (1- (point)) 1))
808 (allout-show-to-offshoot)))
809 (allout-widgets-undecorate-item this-widget))
810 ;; expose any hidden intervening items, so resulting
811 ;; position is clear:
812 (setq inserted-at (point))
813 (allout-unprotected (insert-before-markers "\n"))
815 ;; ensure the inserted newline is visible:
816 (allout-flag-region inserted-at (1+ inserted-at) nil)
817 (allout-widgets-post-command-business 'recursing)
818 (message (concat "outline structure corrected - item"
819 " moved to beginning of new line"))
820 ;; preserve cursor position in some cases:
822 (> (point) inserted-at))
823 (forward-char -1)))))))
826 ;; zero work list so we don't get stuck futilely retrying.
827 ;; error recording done by allout-widgets-hook-error-handler.
828 (setq allout-widgets-changes-record nil))))
829 ;;;_ , major change handlers:
830 ;;;_ > allout-widgets-before-change-handler
831 (defun allout-widgets-before-change-handler (beg end)
832 "Business to be done before changes in a widgetized allout outline."
833 ;; protect against unruly edits to structure:
835 (undo-in-progress (when (eq (get-text-property beg 'category)
836 'allout-icon-span-category)
839 (let* ((item-widget (allout-get-item-widget)))
841 (allout-widgets-exposure-undo-recorder
843 (inhibit-read-only t)
844 ((not (and (boundp 'allout-mode) allout-mode)) t)
845 ((equal this-command 'quoted-insert) t)
846 ((not (text-property-any beg (if (equal end beg)
847 (min (1+ beg) (point-max))
851 ((yes-or-no-p "Unruly edit of outline structure - allow? ")
852 (setq allout-widgets-unset-inhibit-read-only (not inhibit-read-only)
853 inhibit-read-only t))
855 ;; tell the allout-widgets-post-command-business to reestablish the hook:
856 (setq allout-widgets-reenable-before-change-handler t)
857 ;; and raise an error to prevent the edit (and disable the hook):
859 (substitute-command-keys allout-structure-unruly-deletion-message)))))
860 ;;;_ > allout-widgets-after-change-handler
861 (defun allout-widgets-after-change-handler (beg end prelength)
862 "Reconcile what needs to be reconciled for allout widgets after edits."
864 ;;;_ > allout-current-decorated-p ()
865 (defun allout-current-decorated-p ()
866 "True if the current item is not decorated"
868 (if (allout-back-to-current-heading)
869 (if (> allout-recent-depth 0)
870 (and (allout-get-item-widget) t)
871 allout-container-item-widget))))
873 ;;;_ > allout-widgets-hook-error-handler
874 (defun allout-widgets-hook-error-handler (mode args)
875 "Process errors which occurred in the course of command hook operation.
877 We store a backtrace of the error information in the variable,
878 `allout-widgets-last-hook-error', unset the error handlers, and
879 reraise the error, so that processing continues to the
880 encompassing condition-case."
881 ;; first deconstruct special error environment so errors here propagate
882 ;; to encompassing condition-case:
883 (setq debugger 'debug
886 (let* ((bt (with-output-to-string (backtrace)))
887 (this "allout-widgets-hook-error-handler")
889 (format "allout-widgets-last-hook-error stored, %s/%s %s %s"
891 (format-time-string "%e-%b-%Y %r" (current-time)))))
892 ;; post to *Messages* then immediately replace with more compact notice:
893 (message "%s" (setq allout-widgets-last-hook-error
894 (format "%s:\n%s" header bt)))
895 (message header) (sit-for allout-widgets-hook-error-post-time)
896 ;; reraise the error, or one concerning this function if unexpected:
897 (if (equal mode 'error)
899 (error "%s: unexpected mode, %s %s" this mode args))))
900 ;;;_ > allout-widgets-changes-exceed-threshold-p ()
901 (defun allout-widgets-adjusting-message (message)
902 "Post MESSAGE when pending are likely to make a big enough delay.
904 If posting of the MESSAGE is warranted and there already is a
905 `current-message' in the minibuffer, the MESSAGE is appended to
906 the current one, and the previously pending `current-message' is
907 returned for later posting on completion.
909 If posting of the MESSAGE is warranted, but no `current-message'
910 is pending, then t is returned to indicate that case.
912 If posting of the MESSAGE is not warranted, then nil is returned.
914 See `allout-widgets-adjust-message-length-threshold',
915 `allout-widgets-adjust-message-size-threshold' for message
916 posting threshold criteria."
917 (if (or (> (length allout-widgets-changes-record)
918 allout-widgets-adjust-message-length-threshold)
919 ;; for size, use distance from start of first to end of last:
920 (let ((min (point-max))
923 (mapc (function (lambda (entry)
924 (if (eq :undone-exposure (car entry))
926 (setq first (cadr entry)
927 second (caddr entry))
928 (if (< (min first second) min)
929 (setq min (min first second)))
930 (if (> (max first second) max)
931 (setq max (max first second))))))
932 allout-widgets-changes-record)
933 (> (- max min) allout-widgets-adjust-message-size-threshold)))
934 (let ((prior (current-message)))
935 (message (if prior (concat prior " - " message) message))
937 ;;;_ > allout-widgets-changes-dispatcher ()
938 (defun allout-widgets-changes-dispatcher ()
939 "Dispatch CHANGES-RECORD items to respective widgets change processors."
941 (if (not allout-widgets-mode-inhibit)
942 (let* ((changes-record allout-widgets-changes-record)
943 (changes-pending (and changes-record t))
950 (when changes-pending
951 (while changes-record
952 (setq entry (pop changes-record))
954 (:exposed (push entry exposures))
955 (:added (push entry additions))
956 (:deleted (push entry deletions))
957 (:shifted (push entry shifts))))
960 (allout-widgets-exposure-change-processor exposures))
962 (allout-widgets-additions-processor additions))
964 (allout-widgets-deletions-processor deletions))
966 (allout-widgets-shifts-processor shifts))))
967 (when (not (equal allout-widgets-mode-inhibit 'undecorated))
968 (allout-widgets-undecorate-region (point-min)(point-max))
969 (setq allout-widgets-mode-inhibit 'undecorated))))
970 ;;;_ > allout-widgets-exposure-change-recorder (from to flag)
971 (defun allout-widgets-exposure-change-recorder (from to flag)
972 "Record allout exposure changes for tracking during post-command processing.
974 Records changes in `allout-widgets-changes-record'."
975 (push (list :exposed from to flag) allout-widgets-changes-record))
976 ;;;_ > allout-widgets-exposure-change-processor (changes)
977 (defun allout-widgets-exposure-change-processor (changes)
978 "Widgetize and adjust item widgets tracking allout outline exposure changes.
980 Generally invoked via `allout-exposure-change-hook'."
982 (let ((changes (sort changes (function (lambda (this next)
983 (< (cadr this) (cadr next))))))
984 ;; have to distinguish between concealing and exposing so that, eg,
985 ;; `allout-expose-topic's mix is handled properly.
991 (dolist (change changes)
996 (flag (cadddr change))
1000 (if (< to from) (setq bucket to
1004 ;; have we already handled exposure changes in this region?
1005 (setq handling (if flag 'handled-conceal 'handled-expose)
1006 got (allout-range-overlaps from to (symbol-value handling))
1008 (set handling (cadr got))
1017 (allout-widgets-undecorate-region from to)
1018 (allout-beginning-of-current-line)
1019 (let ((widget (allout-get-item-widget)))
1021 (allout-get-or-create-item-widget)
1022 (widget-apply widget :redecorate))))
1026 (while (< (point) to)
1027 (allout-beginning-of-current-line)
1028 (setq parent (allout-get-item-widget))
1030 (setq parent (allout-get-or-create-item-widget))
1031 (widget-apply parent :redecorate))
1032 (allout-next-visible-heading 1)
1033 (if (widget-get parent :has-subitems)
1034 (allout-redecorate-visible-subtree parent))
1036 ;; subtree may be well beyond to - incorporate in ranges:
1037 (setq handled-expose
1038 (allout-range-overlaps from (point) handled-expose)
1039 covered (car handled-expose)
1040 handled-expose (cadr handled-expose)))
1041 (allout-next-visible-heading 1))))))))))
1043 ;;;_ > allout-widgets-additions-recorder (from to)
1044 (defun allout-widgets-additions-recorder (from to)
1045 "Record allout item additions for tracking during post-command processing.
1047 Intended for use on `allout-structure-added-hook'.
1049 FROM point at the start of the first new item and TO is point at the start
1052 Records changes in `allout-widgets-changes-record'."
1053 (push (list :added from to) allout-widgets-changes-record))
1054 ;;;_ > allout-widgets-additions-processor (changes)
1055 (defun allout-widgets-additions-processor (changes)
1056 "Widgetize and adjust items tracking allout outline structure additions.
1058 Dispatched by `allout-widgets-post-command-business' in response to
1059 :added entries recorded by `allout-widgets-additions-recorder'."
1063 (dolist (change changes)
1064 (let ((from (cadr change))
1066 (to (caddr change)))
1067 (if (< to from) (setq bucket to to from from bucket))
1068 ;; have we already handled exposure changes in this region?
1069 (setq handled (allout-range-overlaps from to handled)
1070 covered (car handled)
1071 handled (cadr handled))
1074 ;; Prior sibling and parent can both be affected.
1076 (allout-redecorate-visible-subtree
1077 (allout-get-or-create-item-widget 'redecorate)))
1078 (if (< (point) from)
1080 (while (and (< (point) to) (not (eobp)))
1081 (allout-beginning-of-current-line)
1082 (allout-redecorate-visible-subtree
1083 (allout-get-or-create-item-widget))
1084 (allout-next-visible-heading 1))
1086 ;; subtree may be well beyond to - incorporate in ranges:
1087 (setq handled (allout-range-overlaps from (point) handled)
1088 covered (car handled)
1089 handled (cadr handled)))))))))
1091 ;;;_ > allout-widgets-deletions-recorder (depth from)
1092 (defun allout-widgets-deletions-recorder (depth from)
1093 "Record allout item deletions for tracking during post-command processing.
1095 Intended for use on `allout-structure-deleted-hook'.
1097 DEPTH is the depth of the deleted subtree, and FROM is the point from which
1098 the subtree was deleted.
1100 Records changes in `allout-widgets-changes-record'."
1101 (push (list :deleted depth from) allout-widgets-changes-record))
1102 ;;;_ > allout-widgets-deletions-processor (changes)
1103 (defun allout-widgets-deletions-processor (changes)
1104 "Adjust items tracking allout outline structure deletions.
1106 Dispatched by `allout-widgets-post-command-business' in response to
1107 :deleted entries recorded by `allout-widgets-deletions-recorder'."
1109 (dolist (change changes)
1110 (let ((depth (cadr change))
1111 (from (caddr change)))
1113 (when (allout-previous-visible-heading 1)
1115 (allout-ascend-to-depth (1- depth)))
1116 (allout-redecorate-visible-subtree
1117 (allout-get-or-create-item-widget 'redecorate)))))))
1119 ;;;_ > allout-widgets-shifts-recorder (shifted-amount at)
1120 (defun allout-widgets-shifts-recorder (shifted-amount at)
1121 "Record outline subtree shifts for tracking during post-command processing.
1123 Intended for use on `allout-structure-shifted-hook'.
1125 SHIFTED-AMOUNT is the depth change and AT is the point at the start of the
1126 subtree that's been shifted.
1128 Records changes in `allout-widgets-changes-record'."
1129 (push (list :shifted shifted-amount at) allout-widgets-changes-record))
1130 ;;;_ > allout-widgets-shifts-processor (changes)
1131 (defun allout-widgets-shifts-processor (changes)
1132 "Widgetize and adjust items tracking allout outline structure additions.
1134 Dispatched by `allout-widgets-post-command-business' in response to
1135 :shifted entries recorded by `allout-widgets-shifts-recorder'."
1137 (dolist (change changes)
1138 (goto-char (caddr change))
1140 (allout-redecorate-visible-subtree))))
1141 ;;;_ > allout-widgets-after-copy-or-kill-function ()
1142 (defun allout-widgets-after-copy-or-kill-function ()
1143 "Do allout-widgets processing of text just placed in the kill ring.
1145 Intended for use on allout-after-copy-or-kill-hook."
1147 (setcar kill-ring (allout-widgets-undecorate-text (car kill-ring)))))
1148 ;;;_ > allout-widgets-after-undo-function ()
1149 (defun allout-widgets-after-undo-function ()
1150 "Do allout-widgets processing of text after an undo.
1152 Intended for use on allout-post-undo-hook."
1154 (if (allout-goto-prefix)
1155 (allout-redecorate-item (allout-get-or-create-item-widget)))))
1157 ;;;_ > allout-widgets-exposure-undo-recorder (widget from-state)
1158 (defun allout-widgets-exposure-undo-recorder (widget)
1159 "Record outline exposure undo for tracking during post-command processing.
1161 Intended for use by `allout-graphics-modification-handler'.
1163 WIDGET is the widget being changed.
1165 Records changes in `allout-widgets-changes-record'."
1166 ;; disregard the events if we're currently processing them.
1167 (if (not allout-undo-exposure-in-progress)
1168 (push widget allout-widgets-undo-exposure-record)))
1169 ;;;_ > allout-widgets-exposure-undo-processor ()
1170 (defun allout-widgets-exposure-undo-processor ()
1171 "Adjust items tracking undo of allout outline structure exposure.
1173 Dispatched by `allout-widgets-post-command-business' in response to
1174 :undone-exposure entries recorded by `allout-widgets-exposure-undo-recorder'."
1175 (let* ((allout-undo-exposure-in-progress t)
1176 ;; inhibit undo recording while twiddling exposure to track undo:
1177 (widgets allout-widgets-undo-exposure-record)
1178 widget widget-start-marker widget-end-marker
1179 from-state icon-start-point to-state
1181 (setq allout-widgets-undo-exposure-record nil)
1183 (dolist (widget widgets)
1184 (setq widget-start-marker (widget-get widget :from)
1185 widget-end-marker (widget-get widget :to)
1186 from-state (widget-get widget :icon-state)
1187 icon-start-point (widget-apply widget :actual-position
1189 to-state (get-text-property icon-start-point
1191 (setq handled (allout-range-overlaps widget-start-marker
1194 covered (car handled)
1195 handled (cadr handled))
1197 (goto-char (widget-get widget :from))
1198 (when (not (allout-hidden-p))
1199 ;; adjust actual exposure to that of to-state viz from-state
1200 (cond ((and (eq to-state 'closed) (eq from-state 'opened))
1201 (allout-hide-current-subtree)
1202 (allout-decorate-item-and-context widget))
1203 ((and (eq to-state 'opened) (eq from-state 'closed))
1206 (expose-to (allout-chart-exposure-contour-by-icon))
1207 (goto-char expose-to)
1208 (allout-show-to-offshoot)))))))))))
1209 ;;;_ > allout-chart-exposure-contour-by-icon (&optional from-depth)
1210 (defun allout-chart-exposure-contour-by-icon (&optional from-depth)
1211 "Return points of subtree items to which exposure should be extended.
1213 The qualifying items are ones with a widget icon that is in the closed or
1214 empty state, or items with undecorated subitems.
1216 The resulting list of points is in reverse order.
1218 Optional FROM-DEPTH is for internal use."
1219 ;; During internal recursion, we return a pair: (at-end . result)
1220 ;; Otherwise we just return the result.
1221 (let ((from-depth from-depth)
1227 (setq level-depth (allout-depth))
1228 ;; at containing item:
1229 (setq start-point (point))
1230 (setq from-depth (allout-depth))
1231 (setq at-end (not (allout-next-heading))
1232 level-depth allout-recent-depth))
1234 ;; traverse the level, recursing on deeper levels:
1235 (while (and (not at-end)
1236 (> allout-recent-depth from-depth)
1237 (setq this-widget (allout-get-item-widget)))
1238 (if (< level-depth allout-recent-depth)
1241 (setq subgot (allout-chart-exposure-contour-by-icon level-depth)
1243 subgot (cdr subgot))
1244 (if subgot (setq got (append subgot got))))
1245 ;; progress at this level:
1246 (when (memq (widget-get this-widget :icon-state) '(closed empty))
1248 (allout-end-of-subtree))
1249 (setq at-end (not (allout-next-heading)))))
1251 ;; tailor result depending on whether or not we're a recursion:
1252 (if (not start-point)
1254 (goto-char start-point)
1256 ;;;_ > allout-range-overlaps (from to ranges)
1257 (defun allout-range-overlaps (from to ranges)
1258 "Return a pair indicating overlap of FROM and TO subtree range in RANGES.
1260 First element of result indicates whether candidate range FROM, TO
1261 overlapped any of the existing ranges.
1263 Second element of result is a new version of RANGES incorporating the
1264 candidate range with overlaps consolidated.
1266 FROM and TO must be in increasing order, as must be the pairs in RANGES."
1267 ;; to append to the end: (rplacd next-to-last-cdr (list 'f))
1270 ;; the start of the range that includes the candidate from:
1272 ;; the end of the range that includes the candidate to:
1274 ;; the candidates were inserted:
1276 (while (and ranges (not done))
1277 (setq entry (car ranges)
1278 ranges (cdr ranges))
1283 ;; some entry included the candidate from.
1284 (cond ((> (car entry) to)
1285 ;; current entry exceeds end of candidate range - done.
1286 (push (list included-from to) new-ranges)
1287 (push entry new-ranges)
1288 (setq included-to to
1290 ((>= (cadr entry) to)
1291 ;; current entry includes end of candidate range - done.
1292 (push (list included-from (cadr entry)) new-ranges)
1293 (setq included-to (cadr entry)
1295 ;; current entry contained in candidate range - ditch, continue:
1299 ;; current entry start exceeds candidate end - done, placed as new entry
1300 (push (list from to) new-ranges)
1301 (push entry new-ranges)
1302 (setq included-to to
1305 ((>= (car entry) from)
1306 ;; current entry start is above candidate start, but not above
1307 ;; candidate end (by prior case).
1308 (setq included-from from)
1309 ;; now we have to check on whether this entry contains to, or continue:
1310 (when (>= (cadr entry) to)
1311 ;; current entry contains only candidate end - done:
1312 (push (list included-from (cadr entry)) new-ranges)
1313 (setq included-to (cadr entry)
1315 ;; otherwise, we will continue to look for placement of candidate end.
1318 ((>= (cadr entry) to)
1319 ;; current entry properly contains candidate range.
1320 (push entry new-ranges)
1321 (setq included-from (car entry)
1322 included-to (cadr entry)
1325 ((>= (cadr entry) from)
1326 ;; current entry contains start of candidate range.
1327 (setq included-from (car entry)))
1330 ;; current entry is below the candidate range.
1331 (push entry new-ranges))))
1333 (cond ((and included-from included-to)
1334 ;; candidates placed.
1336 ((not (or included-from included-to))
1337 ;; candidates found no place, must be at the end:
1338 (push (list from to) new-ranges))
1340 ;; candidate start placed but end not:
1341 (push (list included-from to) new-ranges))
1342 ;; might be included-to and not included-from, indicating new entry.
1344 (setq new-ranges (nreverse new-ranges))
1345 (if ranges (setq new-ranges (append new-ranges ranges)))
1346 (list (if included-from t) new-ranges)))
1347 ;;;_ > allout-test-range-overlaps ()
1348 (defun allout-test-range-overlaps ()
1349 "allout-range-overlaps unit tests."
1352 (try (lambda (from to)
1353 (setq got (allout-range-overlaps from to ranges))
1354 (setq ranges (cadr got))
1357 ;; (setq ranges nil)
1358 ;; ;; ~ .02 to .1 seconds for just repeated listing args instead of funcall
1359 ;; ;; ~ 13 seconds for doing repeated funcall
1360 ;; (message "time-trial: %s, resulting size %s"
1362 ;; '(let ((size 10000)
1365 ;; (dotimes (count size)
1366 ;; (setq doing (random size))
1367 ;; (funcall try doing (+ doing (random 5)))
1368 ;; ;;(list doing (+ doing (random 5)))
1375 (assert (equal (funcall try 3 5) '(nil ((3 5)))))
1376 ;; add range at end:
1377 (assert (equal (funcall try 10 12) '(nil ((3 5) (10 12)))))
1378 ;; add range at beginning:
1379 (assert (equal (funcall try 1 2) '(nil ((1 2) (3 5) (10 12)))))
1380 ;; insert range somewhere in the middle:
1381 (assert (equal (funcall try 7 9) '(nil ((1 2) (3 5) (7 9) (10 12)))))
1382 ;; consolidate some:
1383 (assert (equal (funcall try 5 8) '(t ((1 2) (3 9) (10 12)))))
1385 (assert (equal (funcall try 15 17) '(nil ((1 2) (3 9) (10 12) (15 17)))))
1387 (assert (equal (funcall try 20 22)
1388 '(nil ((1 2) (3 9) (10 12) (15 17) (20 22)))))
1390 (assert (equal (funcall try 4 11) '(t ((1 2) (3 12) (15 17) (20 22)))))
1392 (assert (equal (funcall try 2 25) '(t ((1 25)))))
1396 (assert (equal (funcall try 20 25) '(nil ((20 25)))))
1397 (assert (equal (funcall try 30 35) '(nil ((20 25) (30 35)))))
1398 (assert (equal (funcall try 26 28) '(nil ((20 25) (26 28) (30 35)))))
1399 (assert (equal (funcall try 15 20) '(t ((15 25) (26 28) (30 35)))))
1400 (assert (equal (funcall try 10 30) '(t ((10 35)))))
1401 (assert (equal (funcall try 5 6) '(nil ((5 6) (10 35)))))
1402 (assert (equal (funcall try 2 100) '(t ((2 100)))))
1406 ;;;_ > allout-widgetize-buffer (&optional doing)
1407 (defun allout-widgetize-buffer (&optional doing)
1408 "EXAMPLE FUNCTION. Widgetize items in buffer using allout-chart-subtree.
1410 We economize by just focusing on the first of local-maximum depth siblings.
1412 Optional DOING is for internal use - a chart of the current level, for
1413 recursive operation."
1419 (goto-char (point-min))
1420 ;; Construct the chart by scanning the siblings:
1421 (dolist (top-level-sibling (allout-chart-siblings))
1422 (goto-char top-level-sibling)
1423 (let ((subchart (allout-chart-subtree)))
1425 (allout-widgetize-buffer subchart)))))
1427 ;; save-excursion was done on recursion entry, not necessary here.
1428 (let (have-sublists)
1429 (dolist (sibling doing)
1430 (when (listp sibling)
1431 (setq have-sublists t)
1432 (allout-widgetize-buffer sibling)))
1433 (when (and (not have-sublists) (not (widget-at (car doing))))
1434 (goto-char (car doing))
1435 (allout-get-or-create-item-widget)))))
1437 ;;;_ : Item widget and constructors
1439 ;;;_ $ allout-item-widget
1440 (define-widget 'allout-item-widget 'default
1441 "A widget presenting an allout outline item."
1444 ;; widget-field-at respects this to get item if 'field is unused.
1445 ;; we don't use field to avoid collision with end-of-line, etc, on which
1452 ;; tailor the widget for a specific item
1453 :create 'allout-decorate-item-and-context
1454 :value-delete 'allout-widgets-undecorate-item
1455 ;; Not Yet Converted (from original, tree-widget stab)
1456 :expander 'allout-tree-event-dispatcher ; get children when nil :args
1457 :expander-p 'identity ; always engage the :expander
1458 :action 'allout-tree-widget-action
1459 ;; :notify "when item changes"
1461 ;; force decoration of item but not context, unless already done this tick:
1462 :redecorate 'allout-redecorate-item
1463 :last-decorated-tick nil
1464 ;; recognize the actual situation of the item's text:
1465 :parse-item 'allout-parse-item-at-point
1466 ;; decorate the entirety of the item, sans offspring:
1467 :decorate-item-span 'allout-decorate-item-span
1468 ;; decorate the various item elements:
1469 :decorate-guides 'allout-decorate-item-guides
1470 :decorate-icon 'allout-decorate-item-icon
1471 :decorate-cue 'allout-decorate-item-cue
1472 :decorate-body 'allout-decorate-item-body
1473 :actual-position 'allout-item-actual-position
1475 ;; Layout parameters:
1476 :is-container nil ; is this actually the encompassing file/connection?
1478 :from nil ; item beginning - marker
1479 :to nil ; item end - marker
1480 :span-overlay nil ; overlay by which actual position is determined
1482 ;; also serves as guide-end:
1485 :distinctive-start nil
1486 ;; also serves as cue-start:
1487 :distinctive-end nil
1488 ;; also serves as cue-end:
1493 :was-has-subitems 'init
1499 :does-encrypt nil ; pending encryption when :is-encrypted false.
1502 ;; the actual location of the item text:
1503 :location 'allout-item-location
1505 :button-keymap allout-item-icon-keymap ; XEmacs
1506 :keymap allout-item-icon-keymap ; Emacs
1516 :body-brevity-p 'allout-body-brevity-p
1518 ;; :guide-column-flags indicate (in reverse order) whether or not the
1519 ;; item's ancestor at the depth corresponding to the column has a
1520 ;; subsequent sibling - ie, whether or not the corresponding column needs
1521 ;; a descender line to connect that ancestor with its sibling.
1522 :guide-column-flags nil
1523 :was-guide-column-flags 'init
1525 ;; ie, has subitems:
1526 :populous-p 'allout-item-populous-p
1527 :help-echo 'allout-tree-widget-help-echo
1529 ;;;_ > allout-new-item-widget ()
1530 (defsubst allout-new-item-widget ()
1531 "create a new item widget, not yet situated anywhere."
1532 (if allout-widgets-maintain-tally
1533 ;; all the extra overhead is incurred only when doing the
1534 ;; maintenance, except the condition, which can't be avoided.
1535 (let ((widget (widget-convert 'allout-item-widget)))
1536 (puthash widget nil allout-widgets-tally)
1538 (widget-convert 'allout-item-widget)))
1539 ;;;_ : Item decoration
1540 ;;;_ > allout-decorate-item-and-context (item-widget &optional redecorate
1541 ;;; blank-container parent)
1542 (defun allout-decorate-item-and-context (item-widget &optional redecorate
1543 blank-container parent)
1544 "Create or adjust widget decorations for ITEM-WIDGET and neighbors at point.
1546 The neighbors include its siblings and parent.
1548 ITEM-WIDGET can be a created or converted allout-item-widget.
1550 If you're only trying to get or create a widget for an item, use
1551 `allout-get-or-create-item-widget'. If you have the item-widget, applying
1552 :redecorate will do the right thing.
1554 Optional BLANK-CONTAINER is for internal use. It is used to fabricate a
1555 container widget for an empty-bodied container, in the course of decorating
1556 a proper \(non-container\) item which starts at the beginning of the file.
1558 Optional REDECORATE causes redecoration of the item-widget and
1559 its siblings, even if already decorated in this cycle of the command loop.
1561 Optional PARENT, when provided, bypasses some navigation and computation
1562 necessary to obtain the parent of the items being processed.
1564 We return the item-widget corresponding to the item at point."
1566 (when (or redecorate
1567 (not (equal (widget-get item-widget :last-decorated-tick)
1568 allout-command-counter)))
1569 (let* ((allout-inhibit-body-modification-hook t)
1570 (was-modified (buffer-modified-p))
1573 (is-container (or blank-container
1574 (not (setq prefix-start (allout-goto-prefix)))
1575 (< was-point prefix-start)))
1576 ;; steady-point (set in two steps) is reliable across parent
1578 (steady-point (progn (if is-container (goto-char 1))
1580 (steady-point (progn (set-marker-insertion-type steady-point t)
1582 (parent (and (not is-container)
1583 (allout-get-or-create-parent-widget)))
1584 parent-flags parent-depth
1590 reverse-siblings-chart
1591 (buffer-undo-list t))
1593 ;; At this point the parent is decorated and parent-flags indicate
1594 ;; its guide lines. We will iterate over the siblings according to a
1595 ;; chart we create at the start, and going from last to first so we
1596 ;; don't have to worry about text displacement caused by widgetizing.
1599 (progn (widget-put item-widget :is-container t)
1600 (setq reverse-siblings-chart (list 1)))
1601 (goto-char (widget-apply parent :actual-position :from))
1602 (if (widget-get parent :is-container)
1603 ;; `allout-goto-prefix' will go to first non-container item:
1604 (allout-goto-prefix)
1605 (allout-next-heading))
1606 (setq depth (allout-recent-depth))
1607 (setq reverse-siblings-chart (list allout-recent-prefix-beginning))
1608 (while (allout-next-sibling)
1609 (push allout-recent-prefix-beginning reverse-siblings-chart)))
1611 (dolist (doing-at reverse-siblings-chart)
1612 (goto-char doing-at)
1613 (when allout-widgets-track-decoration
1616 (setq doing-item (if (= doing-at steady-point)
1618 (or (allout-get-item-widget)
1619 (allout-new-item-widget))))
1621 (when (or redecorate (not (equal (widget-get doing-item
1622 :last-decorated-tick)
1623 allout-command-counter)))
1624 (widget-apply doing-item :parse-item t blank-container)
1625 (widget-apply doing-item :decorate-item-span)
1627 (widget-apply doing-item :decorate-guides
1628 parent (and successor-sibling t))
1629 (widget-apply doing-item :decorate-icon)
1630 (widget-apply doing-item :decorate-cue)
1631 (widget-apply doing-item :decorate-body)
1633 (widget-put doing-item :last-decorated-tick allout-command-counter))
1635 (setq successor-sibling doing-at))
1637 (set-buffer-modified-p was-modified)
1638 (goto-char steady-point)
1639 ;; must null the marker or the buffer gets clogged with impedance:
1640 (set-marker steady-point nil)
1643 ;;;_ > allout-redecorate-item (item)
1644 (defun allout-redecorate-item (item-widget)
1645 "Resituate ITEM-WIDGET decorations, disregarding context.
1647 Use this to redecorate only the item, when you know that its
1648 situation with respect to siblings, parent, and offspring is
1649 unchanged from its last decoration. Use
1650 `allout-decorate-item-and-context' instead to reassess and adjust
1651 relevant context, when suitable."
1652 (if (not (equal (widget-get item-widget :last-decorated-tick)
1653 allout-command-counter))
1654 (let ((was-modified (buffer-modified-p))
1655 (buffer-undo-list t))
1656 (widget-apply item-widget :parse-item)
1657 (widget-apply item-widget :decorate-guides)
1658 (widget-apply item-widget :decorate-icon)
1659 (widget-apply item-widget :decorate-cue)
1660 (widget-apply item-widget :decorate-body)
1661 (set-buffer-modified-p was-modified))))
1662 ;;;_ > allout-redecorate-visible-subtree (&optional parent-widget
1664 (defun allout-redecorate-visible-subtree (&optional parent-widget depth chart)
1665 "Redecorate all visible items in subtree at point.
1667 Optional PARENT-WIDGET is for optimization, when the parent
1668 widget is already available.
1670 Optional DEPTH restricts the excursion depth of covered.
1672 Optional CHART is for internal recursion, to carry a chart of the
1675 Point is left at the last sibling in the visible subtree."
1676 ;; using a treatment that takes care of all the siblings on a level, we
1677 ;; only need apply it to the first sibling on the level, and we can
1678 ;; collect and pass the parent of the lower levels to recursive calls as
1680 (let ((parent-widget
1681 (if (and parent-widget (widget-apply parent-widget
1682 :actual-position :from))
1683 (progn (goto-char (widget-apply parent-widget
1684 :actual-position :from))
1686 (let ((got (allout-get-item-widget)))
1688 (allout-decorate-item-and-context got 'redecorate)
1689 (allout-get-or-create-item-widget 'redecorate)))))
1690 (pending-chart (or chart (allout-chart-subtree nil 'visible)))
1692 previous-sibling-point
1694 recent-sibling-point)
1695 (setq pending-chart (nreverse pending-chart))
1696 (dolist (sibling-point pending-chart)
1697 (cond ((integerp sibling-point)
1698 (when (not previous-sibling-point)
1699 (goto-char sibling-point)
1700 (if (setq item-widget (allout-get-item-widget nil))
1701 (allout-decorate-item-and-context item-widget 'redecorate
1703 (allout-get-or-create-item-widget)))
1704 (setq previous-sibling-point sibling-point
1705 recent-sibling-point sibling-point))
1706 ((listp sibling-point)
1709 (allout-redecorate-visible-subtree
1710 (if (not previous-sibling-point)
1711 ;; containment discontinuity - sigh
1713 (allout-get-or-create-item-widget 'redecorate))
1714 (if depth (1- depth))
1716 (if (and recent-sibling-point (< (point) recent-sibling-point))
1717 (goto-char recent-sibling-point))))
1718 ;;;_ > allout-parse-item-at-point (item-widget &optional at-beginning
1719 ;;; blank-container)
1720 (defun allout-parse-item-at-point (item-widget &optional at-beginning
1722 "Set widget ITEM-WIDGET layout parameters per item-at-point's actual layout.
1724 If optional AT-BEGINNING is t, then point is assumed to be at the start of
1727 If optional BLANK-CONTAINER is true, then the parameters of a container
1728 which has an empty body are set. \(Though the body is blank, the object
1729 may have subitems.\)"
1731 ;; Uncomment this sit-for to notice where decoration is happening:
1733 (let* ((depth (allout-depth))
1734 (depth (if blank-container 0 depth))
1735 (is-container (or blank-container (zerop depth)))
1737 (does-encrypt (and (not is-container)
1738 (allout-encrypted-type-prefix)))
1739 (is-encrypted (and does-encrypt (allout-encrypted-topic-p)))
1740 (icon-end allout-recent-prefix-end)
1741 (icon-start (1- icon-end))
1746 (contents-depth (1+ depth))
1748 (widget-put item-widget :depth depth)
1752 (widget-put item-widget :from (allout-set-boundary-marker
1754 (widget-get item-widget :from)))
1755 (widget-put item-widget :icon-end nil)
1756 (widget-put item-widget :icon-start nil)
1757 (setq body-start (widget-put item-widget :body-start 1)))
1761 (widget-put item-widget :from (allout-set-boundary-marker
1762 :from (if at-beginning
1764 allout-recent-prefix-beginning)
1765 (widget-get item-widget :from)))
1766 (widget-put item-widget :icon-start icon-start)
1767 (widget-put item-widget :icon-end icon-end)
1769 (widget-put item-widget :does-encrypt t)
1770 (widget-put item-widget :is-encrypted is-encrypted))
1773 (setq body-start icon-end)
1774 (widget-put item-widget :bullet (setq bullet (allout-get-bullet)))
1775 (if (equal (char-after body-start) ? )
1776 (setq body-start (1+ body-start)))
1777 (widget-put item-widget :body-start body-start)
1780 ;; Both container and regular items:
1782 ;; :body-end (doesn't include a trailing blank line, if any) -
1783 (widget-put item-widget :body-end (setq body-end
1786 (allout-end-of-entry))))
1788 (widget-put item-widget :to (allout-set-boundary-marker
1789 :to (if blank-container
1791 (or (allout-pre-next-prefix)
1792 (goto-char (point-max))))
1793 (widget-get item-widget :to)))
1794 (widget-put item-widget :has-subitems
1797 ;; has a subsequent item:
1798 (not (= body-end (point-max)))
1799 ;; subsequent item is deeper:
1800 (< depth (setq contents-depth (allout-recent-depth))))))
1801 ;; note :expanded - true if widget item's content is currently visible?
1802 (widget-put item-widget :expanded
1804 ;; subsequent item is or isn't visible:
1806 (goto-char allout-recent-prefix-beginning)
1807 (not (allout-hidden-p)))))))
1808 ;;;_ > allout-set-boundary-marker (boundary position &optional current-marker)
1809 (defun allout-set-boundary-marker (boundary position &optional current-marker)
1810 "Set or create item widget BOUNDARY type marker at POSITION.
1812 Optional CURRENT-MARKER is the marker currently being used for
1813 the boundary, if any.
1815 BOUNDARY type is either :from or :to, determining the marker insertion type."
1816 (if (not position) (setq position (point)))
1818 (set-marker current-marker position)
1819 (let ((marker (make-marker)))
1820 ;; XXX dang - would like for :from boundary to advance after inserted
1821 ;; text, but that would omit new header prefixes when allout
1822 ;; relevels, etc. this competes with ad-hoc edits, which would
1823 ;; better be omitted
1824 (set-marker-insertion-type marker nil)
1825 (set-marker marker position))))
1826 ;;;_ > allout-decorate-item-span (item-widget)
1827 (defun allout-decorate-item-span (item-widget)
1828 "Equip the item with a span, as an entirety.
1830 This span is implemented so it can be used to detect displacement
1831 of the widget in absolute terms, and provides an offset bias for
1832 the various element spans."
1834 (if (and (widget-get item-widget :is-container)
1835 ;; the only case where the span could be empty.
1836 (eq (widget-get item-widget :from)
1837 (widget-get item-widget :to)))
1839 (allout-item-span item-widget
1840 (widget-get item-widget :from)
1841 (widget-get item-widget :to))))
1842 ;;;_ > allout-decorate-item-guides (item-widget
1843 ;;; &optional parent-widget has-successor)
1844 (defun allout-decorate-item-guides (item-widget
1845 &optional parent-widget has-successor)
1846 "Add ITEM-WIDGET guide icon-prefix descender and connector text properties.
1848 Optional arguments provide context for deriving the guides. In
1849 their absence, the current guide column flags are used.
1851 Optional PARENT-WIDGET is the widget for the item's parent item.
1853 Optional HAS-SUCCESSOR is true iff the item is followed by a sibling.
1855 We also hide the header-prefix string.
1857 Guides are established according to the item-widget's :guide-column-flags,
1858 when different than :was-guide-column-flags. Changing that property and
1859 reapplying this method will rectify the glyphs."
1861 (when (not (widget-get item-widget :is-container))
1862 (let* ((depth (widget-get item-widget :depth))
1863 (parent-depth (and parent-widget
1864 (widget-get parent-widget :depth)))
1865 (parent-flags (and parent-widget
1866 (widget-get parent-widget :guide-column-flags)))
1867 (parent-flags-depth (length parent-flags))
1868 (extender-length (- depth (+ parent-flags-depth 2)))
1869 (flags (or (and (> depth 1)
1871 (widget-put item-widget :guide-column-flags
1872 (append (list has-successor)
1873 (if (< 0 extender-length)
1874 (make-list extender-length
1877 (widget-get item-widget :guide-column-flags)))
1878 (was-flags (widget-get item-widget :was-guide-column-flags))
1879 (guides-start (widget-get item-widget :from))
1880 (guides-end (widget-get item-widget :icon-start))
1881 (position guides-start)
1882 (increment (length allout-header-prefix))
1885 extenders paint-extenders
1886 (inhibit-read-only t))
1888 (when (not (equal was-flags flags))
1890 (setq reverse-flags (reverse flags))
1891 (while reverse-flags
1893 (cond ((null (cdr reverse-flags))
1894 (if (car reverse-flags)
1897 ((eq (car reverse-flags) '-)
1898 ;; accumulate extenders tally, to be painted on next
1899 ;; non-extender flag, according to the flag type.
1900 (setq extenders (1+ (or extenders 0)))
1902 ((car reverse-flags)
1904 (t 'skip-descender)))
1906 (put-text-property position (setq position (+ position increment))
1907 'display (allout-fetch-icon-image guide-name))
1908 (if (> increment 1) (setq increment 1))
1910 ;; paint extenders after a connector, else leave spaces.
1911 (dotimes (i extenders)
1913 position (setq position (1+ position))
1914 'display (allout-fetch-icon-image
1915 (if (memq guide-name '(mid-connector end-connector))
1918 (setq extenders nil)))
1919 (setq reverse-flags (cdr reverse-flags)))
1920 (widget-put item-widget :was-guide-column-flags flags))
1922 (allout-item-element-span-is item-widget :guides-span
1923 guides-start guides-end))))
1924 ;;;_ > allout-decorate-item-icon (item-widget)
1925 (defun allout-decorate-item-icon (item-widget)
1926 "Add item icon glyph and distinctive bullet text properties to ITEM-WIDGET."
1928 (when (not (widget-get item-widget :is-container))
1929 (let* ((icon-start (widget-get item-widget :icon-start))
1930 (icon-end (widget-get item-widget :icon-end))
1931 (bullet (widget-get item-widget :bullet))
1933 (was-bullet (widget-get item-widget :was-bullet))
1934 (distinctive (allout-distinctive-bullet bullet))
1935 (distinctive-start (widget-get item-widget :distinctive-start))
1936 (distinctive-end (widget-get item-widget :distinctive-end))
1937 (does-encrypt (widget-get item-widget :does-encrypt))
1938 (is-encrypted (and does-encrypt (widget-get item-widget
1940 (expanded (widget-get item-widget :expanded))
1941 (has-subitems (widget-get item-widget :has-subitems))
1942 (inhibit-read-only t)
1945 (when (not (and (equal (widget-get item-widget :was-expanded) expanded)
1946 (equal (widget-get item-widget :was-has-subitems)
1948 (equal (widget-get item-widget :was-does-encrypt)
1950 (equal (widget-get item-widget :was-is-encrypted)
1954 (cond (does-encrypt (if is-encrypted
1956 'unlocked-encrypted))
1958 (has-subitems 'closed)
1960 (put-text-property icon-start (1+ icon-start)
1961 'display (allout-fetch-icon-image icon-state))
1962 (widget-put item-widget :was-expanded expanded)
1963 (widget-put item-widget :was-has-subitems has-subitems)
1964 (widget-put item-widget :was-does-encrypt does-encrypt)
1965 (widget-put item-widget :was-is-encrypted is-encrypted)
1966 ;; preserve as a widget property to track last known:
1967 (widget-put item-widget :icon-state icon-state)
1968 ;; preserve as a text property to track undo:
1969 (put-text-property icon-start icon-end :icon-state icon-state))
1970 (allout-item-element-span-is item-widget :icon-span
1971 icon-start icon-end)
1972 (when (not (string= was-bullet bullet))
1973 (cond ((not distinctive)
1974 ;; XXX we strip the prior properties without even checking if
1975 ;; the prior bullet was distinctive, because the widget
1976 ;; provisions to convey that info is disappearing, sigh.
1977 (remove-text-properties icon-end (1+ icon-end) '(display))
1978 (setq distinctive-start icon-end distinctive-end icon-end)
1979 (widget-put item-widget :distinctive-start distinctive-start)
1980 (widget-put item-widget :distinctive-end distinctive-end))
1982 ((not (string= bullet allout-numbered-bullet))
1983 (setq distinctive-start icon-end distinctive-end (+ icon-end 1)))
1986 (setq distinctive-start icon-end distinctive-end (+ icon-end 1)))
1989 (goto-char icon-end)
1990 (looking-at "[0-9]+")
1991 (setq use-bullet (buffer-substring icon-end (match-end 0)))
1992 (setq distinctive-start icon-end
1993 distinctive-end (match-end 0))))
1994 (put-text-property distinctive-start distinctive-end 'display
1996 (widget-put item-widget :was-bullet bullet)
1997 (widget-put item-widget :distinctive-start distinctive-start)
1998 (widget-put item-widget :distinctive-end distinctive-end)))))
1999 ;;;_ > allout-decorate-item-cue (item-widget)
2000 (defun allout-decorate-item-cue (item-widget)
2001 "Incorporate space between bullet icon and body to the ITEM-WIDGET."
2002 ;; NOTE: most of the cue-area
2004 (when (not (widget-get item-widget :is-container))
2005 (let* ((cue-start (or (widget-get item-widget :distinctive-end)
2006 (widget-get item-widget :icon-end)))
2007 (body-start (widget-get item-widget :body-start))
2008 (expanded (widget-get item-widget :expanded))
2009 (has-subitems (widget-get item-widget :has-subitems))
2010 (inhibit-read-only t))
2012 (allout-item-element-span-is item-widget :cue-span cue-start body-start)
2013 (put-text-property (1- body-start) body-start 'rear-nonsticky t))))
2014 ;;;_ > allout-decorate-item-body (item-widget &optional force)
2015 (defun allout-decorate-item-body (item-widget &optional force)
2016 "Incorporate item body text as part the ITEM-WIDGET.
2018 Optional FORCE means force reassignment of the region property."
2020 (let* ((allout-inhibit-body-modification-hook t)
2021 (body-start (widget-get item-widget :body-start))
2022 (body-end (widget-get item-widget :body-end))
2023 (body-text-end body-end)
2024 (inhibit-read-only t))
2026 (allout-item-element-span-is item-widget :body-span
2027 body-start (min (1+ body-end) (point-max))
2029 ;;;_ > allout-item-actual-position (item-widget field)
2030 (defun allout-item-actual-position (item-widget field)
2031 "Return ITEM-WIDGET FIELD position taking item displacement into account."
2033 ;; The item's sub-element positions (:icon-end, :body-start, etc) are
2034 ;; accurate when the item is parsed, but some offsets from the start
2035 ;; drift with text added in the body.
2037 ;; Rather than reparse an item with every change (inefficient), or derive
2038 ;; every position from a distinct field marker/overlay (prohibitive as
2039 ;; the number of items grows), we use the displacement tracking of the
2040 ;; :span-overlay's markers, against the registered :from or :body-end
2041 ;; (depending on whether the requested field value is before or after the
2042 ;; item body), to bias the registered values.
2044 ;; This is not necessary/useful when the item is being decorated, because
2045 ;; that always must be preceded by a fresh item parse.
2047 (if (not (eq field :body-end))
2048 (widget-get item-widget :from)
2050 (let* ((span-overlay (widget-get item-widget :span-overlay))
2051 (body-end-position (widget-get item-widget :body-end))
2052 (ref-marker-position (and span-overlay
2053 (overlay-end span-overlay)))
2054 (offset (and body-end-position span-overlay
2055 (- (or ref-marker-position 0)
2056 body-end-position))))
2057 (+ (widget-get item-widget field) (or offset 0)))))
2058 ;;;_ : Item undecoration
2059 ;;;_ > allout-widgets-undecorate-region (start end)
2060 (defun allout-widgets-undecorate-region (start end)
2061 "Eliminate widgets and decorations for all items in region from START to END."
2066 (while (< (setq next (next-single-char-property-change next
2072 (when (setq widget (allout-get-item-widget))
2073 ;; if the next-property/overly progression got us to a widget:
2074 (allout-widgets-undecorate-item widget t))))))
2075 ;;;_ > allout-widgets-undecorate-text (text)
2076 (defun allout-widgets-undecorate-text (text)
2077 "Eliminate widgets and decorations for all items in TEXT."
2078 (remove-text-properties 0 (length text)
2079 '(display nil :icon-state nil rear-nonsticky nil
2080 category nil button nil field nil)
2083 ;;;_ > allout-widgets-undecorate-item (item-widget &optional no-expose)
2084 (defun allout-widgets-undecorate-item (item-widget &optional no-expose)
2085 "Remove widget decorations from ITEM-WIDGET.
2087 Any concealed content head lines and item body is exposed, unless
2088 optional NO-EXPOSE is non-nil."
2089 (let ((from (widget-get item-widget :from))
2090 (to (widget-get item-widget :to))
2091 (text-properties-to-remove '(display nil
2097 (span-overlay (widget-get item-widget :span-overlay))
2098 (button-overlay (widget-get item-widget :button))
2099 (was-modified (buffer-modified-p))
2100 (buffer-undo-list t)
2101 (inhibit-read-only t))
2103 (allout-flag-region from to nil))
2105 (remove-text-properties from to text-properties-to-remove))
2107 (delete-overlay span-overlay) (widget-put item-widget :span-overlay nil))
2108 (when button-overlay
2109 (delete-overlay button-overlay) (widget-put item-widget :button nil))
2110 (set-marker from nil)
2112 (if (not was-modified)
2113 (set-buffer-modified-p nil))))
2115 ;;;_ : Item decoration support
2116 ;;;_ > allout-item-span (item-widget &optional start end)
2117 (defun allout-item-span (item-widget &optional start end)
2118 "Return or register the location of an ITEM-WIDGET's actual START and END.
2120 If START and END are not passed in, return either a dotted pair
2121 of the current span, if established, or nil if not yet set.
2123 When the START and END are passed, return the distance that the
2124 start of the item moved. We return 0 if the span was not
2125 previously established or is not moved."
2126 (let ((overlay (widget-get item-widget :span-overlay))
2129 (cond ((not overlay) (when start
2130 (setq overlay (make-overlay start end nil t nil))
2131 (overlay-put overlay 'button item-widget)
2132 (overlay-put overlay 'evaporate t)
2133 (widget-put item-widget :span-overlay overlay)
2136 ((not start) (cons (overlay-start overlay) (overlay-end overlay)))
2138 ((or (not (equal (overlay-start overlay) start))
2139 (not (equal (overlay-end overlay) end)))
2140 (move-overlay overlay start end)
2142 ;; specified span already set:
2144 ;;;_ > allout-item-element-span-is (item-widget element
2145 ;;; &optional start end force)
2146 (defun allout-item-element-span-is (item-widget element
2147 &optional start end force)
2148 "Return or register the location of the indicated ITEM-WIDGET ELEMENT.
2150 ELEMENT is one of :guides-span, :icon-span, :cue-span, or :body-span.
2152 When optional START is specified, optional END must also be.
2154 START and END are the actual bounds of the region, if provided.
2156 If START and END are not passed in, we return either a dotted
2157 pair of the current span, if established, or nil if not yet set.
2159 When the START and END are passed, we return t if the region
2160 changed or nil if not.
2162 Optional FORCE means force assignment of the region's text
2163 property, even if it's already set."
2164 (let ((span (widget-get item-widget element)))
2165 (cond ((or (not span) force)
2167 (widget-put item-widget element (cons start end))
2168 (put-text-property start end 'category
2170 allout-span-to-category)))
2174 ;; move if necessary:
2175 ((not (and (eq (car span) start)
2176 (eq (cdr span) end)))
2177 (widget-put item-widget element span)
2179 ;; specified span already set:
2181 ;;;_ : Item widget retrieval (/ high-level creation):
2182 ;;;_ > allout-get-item-widget (&optional container)
2183 (defun allout-get-item-widget (&optional container)
2184 "Return the widget for the item at point, or nil if no widget yet exists.
2186 Point must be situated *before* the start of the target item's
2187 body, so we don't get an existing containing item when we're in
2188 the process of creating an item in the middle of another.
2190 Optional CONTAINER is used to obtain the container item."
2191 (if (or container (zerop (allout-depth)))
2192 allout-container-item-widget
2193 ;; allout-recent-* are calibrated by (allout-depth) if we got here.
2194 (let ((got (widget-at allout-recent-prefix-beginning)))
2195 (if (and got (listp got))
2196 (if (marker-position (widget-get got :from))
2198 (>= (point) (widget-apply got :actual-position :from))
2199 (<= (point) (widget-apply got :actual-position :body-start))
2201 ;; a wacky residual item - undecorate and disregard:
2202 (allout-widgets-undecorate-item got)
2204 ;;;_ > allout-get-or-create-item-widget (&optional redecorate blank-container)
2205 (defun allout-get-or-create-item-widget (&optional redecorate blank-container)
2206 "Return a widget for the item at point, creating the widget if necessary.
2208 When creating a widget, we assume there has been a context change
2209 and decorate its siblings and parent, as well.
2211 Optional BLANK-CONTAINER is for internal use, to fabricate a
2212 meta-container item with an empty body when the first proper
2213 \(non-container\) item starts at the beginning of the file.
2215 Optional REDECORATE, if non-nil, means to redecorate the widget
2216 if it already exists."
2217 (let ((widget (allout-get-item-widget blank-container))
2218 (buffer-undo-list t))
2219 (cond (widget (if redecorate
2220 (allout-redecorate-item widget))
2222 ((or blank-container (zerop (allout-depth)))
2223 (or allout-container-item-widget
2224 (setq allout-container-item-widget
2225 (allout-decorate-item-and-context
2226 (widget-convert 'allout-item-widget)
2227 nil blank-container))))
2228 ;; create a widget for a regular/non-container item:
2229 (t (allout-decorate-item-and-context (widget-convert
2230 'allout-item-widget))))))
2231 ;;;_ > allout-get-or-create-parent-widget (&optional redecorate)
2232 (defun allout-get-or-create-parent-widget (&optional redecorate)
2233 "Return widget for parent of item at point, decorating it if necessary.
2235 We return the container widget if we're above the first proper item in the
2238 Optional REDECORATE, if non-nil, means to redecorate the widget if it
2241 Point will wind up positioned on the beginning of the parent or beginning
2243 ;; use existing widget, if there, else establish it
2244 (if (or (bobp) (and (not (allout-ascend))
2245 (looking-at allout-regexp)))
2246 (allout-get-or-create-item-widget redecorate 'blank-container)
2247 (allout-get-or-create-item-widget redecorate)))
2248 ;;;_ : X- Item ancillaries
2249 ;;;_ >X allout-body-modification-handler (beg end)
2250 (defun allout-body-modification-handler (beg end)
2251 "Do routine processing of body text before and after modification.
2253 Operation is inhibited by `allout-inhibit-body-modification-handler'."
2255 ;; The primary duties are:
2257 ;; - marking of escaped prefix-like text for delayed cleanup of escapes
2258 ;; - removal and replacement of the settings
2259 ;; - maintenance of beginning-of-line guide lines
2261 ;; ?? Escapes removal \(before changes\) is not done when edits span multiple
2262 ;; items, recognizing that item structure is being preserved, including
2263 ;; escaping of item-prefix-like text within bodies. See
2264 ;; `allout-before-modification-handler' and
2265 ;; `allout-inhibit-body-modification-handler'.
2267 ;; Adds the overlay to the `allout-unresolved-body-mod-workhash' during
2268 ;; before-change operation, and removes from that list during after-change
2270 (cond (allout-inhibit-body-modification-hook nil)))
2271 ;;;_ >X allout-graphics-modification-handler (beg end)
2272 (defun allout-graphics-modification-handler (beg end)
2273 "Protect against incoherent deletion of decoration graphics.
2275 Deletes allowed only when inhibit-read-only is t."
2277 (undo-in-progress (when (eq (get-text-property beg 'category)
2278 'allout-icon-span-category)
2281 (let* ((item-widget (allout-get-item-widget)))
2283 (allout-widgets-exposure-undo-recorder
2285 (inhibit-read-only t)
2286 ((not (and (boundp 'allout-mode) allout-mode)) t)
2287 ((equal this-command 'quoted-insert) t)
2288 ((yes-or-no-p "Unruly edit of outline structure - allow? ")
2289 (setq allout-widgets-unset-inhibit-read-only (not inhibit-read-only)
2290 inhibit-read-only t))
2292 (substitute-command-keys allout-structure-unruly-deletion-message)))))
2293 ;;;_ > allout-item-icon-key-handler ()
2294 (defun allout-item-icon-key-handler ()
2295 "Catchall handling of key bindings in item icon/cue hot-spots.
2297 Applies `allout-hotspot-key-handler' and calls the result, if any, as an
2298 interactive command."
2301 (let* ((mapped-binding (allout-hotspot-key-handler)))
2302 (when mapped-binding
2303 (call-interactively mapped-binding))))
2306 ;;;_ . allout-item-location (item-widget)
2307 (defun allout-item-location (item-widget)
2308 "Location of the start of the item's text."
2309 (overlay-start (widget-get item-widget :span-overlay)))
2311 ;;;_ : Icon management
2312 ;;;_ > allout-fetch-icon-image (name)
2313 (defun allout-fetch-icon-image (name)
2314 "Fetch allout icon for symbol NAME.
2316 We use a caching strategy, so the caller doesn't need to do so."
2317 (let* ((types allout-widgets-icon-types)
2318 (use-dir (if (equal (allout-frame-property nil 'background-mode)
2320 allout-widgets-icons-light-subdir
2321 allout-widgets-icons-dark-subdir))
2322 (key (list name use-dir))
2323 (got (assoc key allout-widgets-icons-cache)))
2325 ;; display system shows only first of subsequent adjacent
2326 ;; `eq'-identical repeats - use copies to avoid this problem.
2327 (allout-widgets-copy-list (cadr got))
2328 (while (and types (not got))
2331 (list (append (list :type (car types)
2332 :file (concat use-dir
2336 (if (featurep 'xemacs)
2337 allout-widgets-item-image-properties-xemacs
2338 allout-widgets-item-image-properties-emacs)
2340 (setq types (cdr types)))
2342 (push (list key got) allout-widgets-icons-cache))
2345 ;;;_ : Miscellaneous
2346 ;;;_ > allout-elapsed-time-seconds (triple)
2347 (defun allout-elapsed-time-seconds (end start)
2348 "Return seconds between `current-time' style time START/END triples."
2349 (let ((elapsed (time-subtract end start)))
2350 (float-time elapsed)))
2351 ;;;_ > allout-frame-property (frame property)
2352 (defalias 'allout-frame-property
2353 (cond ((fboundp 'frame-parameter)
2355 ((fboundp 'frame-property)
2358 ;;;_ > allout-find-image (specs)
2359 (defalias 'allout-find-image
2360 (if (fboundp 'find-image)
2362 nil) ; aka, not-yet-implemented for xemacs.
2364 ;;;_ > allout-widgets-copy-list (list)
2365 (defun allout-widgets-copy-list (list)
2366 ;; duplicated from cl.el 'copy-list' as of 2008-08-17
2367 "Return a copy of LIST, which may be a dotted list.
2368 The elements of LIST are not copied, just the list structure itself."
2371 (while (consp list) (push (pop list) res))
2372 (prog1 (nreverse res) (setcdr res list)))
2374 ;;;_ . allout-widgets-count-buttons-in-region (start end)
2375 (defun allout-widgets-count-buttons-in-region (start end)
2376 "Debugging/diagnostic tool - count overlays with 'button' property in region."
2378 (setq start (or start (point-min))
2379 end (or end (point-max)))
2380 (if (> start end) (let ((interim start)) (setq start end end interim)))
2381 (let ((button-overlays (delq nil
2382 (mapcar (function (lambda (o)
2383 (if (overlay-get o 'button)
2385 (overlays-in start end)))))
2386 (length button-overlays)))
2388 ;;;_ : Run unit tests:
2389 (defun allout-widgets-run-unit-tests ()
2390 (message "Running allout-widget tests...")
2392 (allout-test-range-overlaps)
2394 (message "Running allout-widget tests... Done.")
2397 (when allout-widgets-run-unit-tests-on-load
2398 (allout-widgets-run-unit-tests))
2401 (provide 'allout-widgets)
2403 ;;;_. Local emacs vars.
2404 ;;;_ , Local variables:
2405 ;;;_ , allout-layout: (-1 : 0)