Regenerate autotools stuff, grumble.
[emacs.git] / lisp / allout-widgets.el
blobcc5fd6d96fa142bafc2f5cb17129f75a9d374f53
1 ;; allout-widgets.el --- Show allout outline structure with graphical widgets.
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>
7 ;; Version: 1.0
8 ;; Created: Dec 2005
9 ;; Version: 1.0
10 ;; Keywords: outlines
11 ;; Website: http://myriadicity.net/Sundry/EmacsAllout
13 ;;; Commentary:
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
56 (require 'allout)
57 (require 'widget)
58 (require 'wid-edit)
60 (eval-when-compile
61 (progn
62 (require 'overlay)
63 (require 'cl)
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
79 ;;;###autoload
80 (defgroup allout-widgets nil
81 "Allout extension that highlights outline structure graphically.
83 Customize `allout-widgets-auto-activation' to activate allout-widgets
84 with allout-mode."
85 :group 'allout)
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)
110 ;;;###autoload
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
120 ;;;###autoload
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
125 visiting an outline.
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."
135 :type 'boolean
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."
152 ;; :type 'boolean
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."
159 :type 'string
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."
164 :type 'string
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."
176 :type '(string)
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."
181 :type '(string)
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."
187 :type 'plist
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."
193 :type 'plist
194 :group 'allout-widgets)
195 ;;;_ . Developer
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
201 set this.
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
205 compilation.)
207 See `allout-widgets-run-unit-tests' to see what's run."
208 :type 'boolean
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
218 set this."
219 :type 'boolean
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."
228 :type 'integer
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."
239 :type 'boolean
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 ;;;_ > allout-widgets-tally-string
250 (defun allout-widgets-tally-string ()
251 "Return a string giving the number of tracked widgets, or empty string if not tracking.
253 The string is formed for appending to the allout-mode mode-line lighter.
255 An empty string is also returned if tracking is inhibited or
256 widgets are locally inhibited.
258 The number varies according to the evanescence of objects on a
259 hash table with weak keys, so tracking of widget erasures is often delayed."
260 (when (and allout-widgets-maintain-tally (not allout-widgets-mode-inhibit))
261 (format ":%s" (hash-table-count allout-widgets-tally))))
262 ;;;_ = allout-widgets-track-decoration nil
263 (defcustom allout-widgets-track-decoration nil
264 "*If non-nil, show cursor position of each item decoration.
266 This is for debugging purposes, and generally set at need in a
267 buffer rather than as a prevailing configuration \(but it's handy
268 to publicize it by making it a customization variable\)."
269 :type 'boolean
270 :group 'allout-widgets-developer)
271 (make-variable-buffer-local 'allout-widgets-track-decoration)
273 ;;;_ : Mode context - variables, hookup, and hooks
274 ;;;_ . internal mode variables
275 ;;;_ , Mode activation and environment
276 ;;;_ = allout-widgets-version
277 (defvar allout-widgets-version "1.0"
278 "Version of currently loaded allout-widgets extension.")
279 ;;;_ > allout-widgets-version
280 (defun allout-widgets-version (&optional here)
281 "Return string describing the loaded outline version."
282 (interactive "P")
283 (let ((msg (concat "Allout Outline Widgets Extension v "
284 allout-widgets-version)))
285 (if here (insert msg))
286 (message "%s" msg)
287 msg))
288 ;;;_ = allout-widgets-mode-inhibit
289 (defvar allout-widgets-mode-inhibit nil
290 "Inhibit `allout-widgets-mode' from activating widgets.
292 This also inhibits automatic adjustment of widgets to track allout outline
293 changes.
295 You can use this as a file local variable setting to disable
296 allout widgets enhancements in selected buffers while generally
297 enabling widgets by customizing `allout-widgets-auto-activation'.
299 In addition, you can invoked `allout-widgets-mode' allout-mode
300 buffers where this is set to enable and disable widget
301 enhancements, directly.")
302 ;;;###autoload
303 (put 'allout-widgets-mode-inhibit 'safe-local-variable
304 (if (fboundp 'booleanp) 'booleanp '(lambda (x) (member x '(t nil)))))
305 (make-variable-buffer-local 'allout-widgets-mode-inhibit)
306 ;;;_ = allout-inhibit-body-modification-hook
307 (defvar allout-inhibit-body-modification-hook nil
308 "Override de-escaping of text-prefixes in item bodies during specific changes.
310 This is used by `allout-buffer-modification-handler' to signal such changes
311 to `allout-body-modification-handler', and is always reset by
312 `allout-post-command-business'.")
313 (make-variable-buffer-local 'allout-inhibit-body-modification-hook)
314 ;;;_ = allout-widgets-icons-cache
315 (defvar allout-widgets-icons-cache nil
316 "Cache allout icon images, as an association list.
318 `allout-fetch-icon-image' uses this cache transparently, keying
319 images with lists containing the name of the icon directory \(as
320 found on the `load-path') and the icon name.
322 Set this variable to `nil' to empty the cache, and have it replenish from the
323 filesystem.")
324 ;;;_ = allout-widgets-unset-inhibit-read-only
325 (defvar allout-widgets-unset-inhibit-read-only nil
326 "Tell `allout-widgets-post-command-business' to unset `inhibit-read-only'.
328 Used by `allout-graphics-modification-handler'")
329 ;;;_ = allout-widgets-reenable-before-change-handler
330 (defvar allout-widgets-reenable-before-change-handler nil
331 "Tell `allout-widgets-post-command-business' to reequip the handler.
333 Necessary because the handler sometimes deliberately raises an
334 error, causing it to be disabled.")
335 ;;;_ , State for hooks
336 ;;;_ = allout-unresolved-body-mod-workroster
337 (defvar allout-unresolved-body-mod-workroster (make-hash-table :size 16)
338 "List of body-overlays that did before-change business but not after-change.
340 See `allout-post-command-business' and `allout-body-modification-handler'.")
341 ;;;_ = allout-structure-unruly-deletion-message
342 (defvar allout-structure-unruly-deletion-message
343 "Unruly edit prevented --
344 To change the bullet character: \\[allout-rebullet-current-heading]
345 To promote this item: \\[allout-shift-out]
346 To demote it: \\[allout-shift-in]
347 To delete it and offspring: \\[allout-kill-topic]
348 See \\[describe-mode] for many more options."
349 "Informative message presented on improper editing of outline structure.
351 The structure includes the guides lines, bullet, and bullet cue.")
352 ;;;_ = allout-widgets-changes-record
353 (defvar allout-widgets-changes-record nil
354 "Record outline changes for processing by post-command hook.
356 Entries on the list are lists whose first element is a symbol indicating
357 the change type and subsequent elements are data specific to that change
358 type. Specifically:
360 'exposure `allout-exposure-from' `allout-exposure-to' `allout-exposure-flag'
362 The changes are recorded in reverse order, with new values pushed
363 onto the front.")
364 (make-variable-buffer-local 'allout-widgets-changes-record)
365 ;;;_ = allout-widgets-undo-exposure-record
366 (defvar allout-widgets-undo-exposure-record nil
367 "Record outline undo traces for processing by post-command hook.
369 The changes are recorded in reverse order, with new values pushed
370 onto the front.")
371 (make-variable-buffer-local 'allout-widgets-undo-exposure-record)
372 ;;;_ = allout-widgets-last-hook-error
373 (defvar allout-widgets-last-hook-error nil
374 "String holding last error string, for debugging purposes.")
375 ;;;_ = allout-widgets-adjust-message-length-threshold 100
376 (defvar allout-widgets-adjust-message-length-threshold 100
377 "Display \"Adjusting widgets\" message above this number of pending changes."
379 ;;;_ = allout-widgets-adjust-message-size-threshold 10000
380 (defvar allout-widgets-adjust-message-size-threshold 10000
381 "Display \"Adjusting widgets\" message above this size of pending changes."
383 ;;;_ = allout-doing-exposure-undo-processor nil
384 (defvar allout-undo-exposure-in-progress nil
385 "Maintained true during `allout-widgets-exposure-undo-processor'")
386 ;;;_ , Widget-specific outline text format
387 ;;;_ = allout-escaped-prefix-regexp
388 (defvar allout-escaped-prefix-regexp ""
389 "*Regular expression for body text that would look like an item prefix if
390 not altered with an escape sequence.")
391 (make-variable-buffer-local 'allout-escaped-prefix-regexp)
392 ;;;_ , Widget element formatting
393 ;;;_ = allout-item-icon-keymap
394 (defvar allout-item-icon-keymap
395 (let ((km (make-sparse-keymap)))
396 (dolist (digit '("0" "1" "2" "3"
397 "4" "5" "6" "7" "8" "9"))
398 (define-key km digit 'digit-argument))
399 (define-key km "-" 'negative-argument)
400 ;; (define-key km [(return)] 'allout-tree-expand-command)
401 ;; (define-key km [(meta return)] 'allout-toggle-torso-command)
402 ;; (define-key km [(down-mouse-1)] 'allout-item-button-click)
403 ;; (define-key km [(down-mouse-2)] 'allout-toggle-torso-event-command)
404 ;; Override underlying mouse-1 and mouse-2 bindings in icon territory:
405 (define-key km [(mouse-1)] (lambda () (interactive) nil))
406 (define-key km [(mouse-2)] (lambda () (interactive) nil))
408 ;; Catchall, handles actual keybindings, dynamically doing keymap lookups:
409 (define-key km [t] 'allout-item-icon-key-handler)
412 "General tree-node key bindings.")
413 ;;;_ = allout-item-body-keymap
414 (defvar allout-item-body-keymap
415 (let ((km (make-sparse-keymap))
416 (local-map (current-local-map)))
417 ;; (define-key km [(control return)] 'allout-tree-expand-command)
418 ;; (define-key km [(meta return)] 'allout-toggle-torso-command)
419 ;; XXX We need to reset this per buffer's mode; we do so in
420 ;; allout-widgets-mode.
421 (if local-map
422 (set-keymap-parent km local-map))
425 "General key bindings for the text content of outline items.")
426 (make-variable-buffer-local 'allout-item-body-keymap)
427 ;;;_ = allout-body-span-category
428 (defvar allout-body-span-category nil
429 "Symbol carrying allout body-text overlay properties.")
430 ;;;_ = allout-cue-span-keymap
431 (defvar allout-cue-span-keymap
432 (let ((km (make-sparse-keymap)))
433 (set-keymap-parent km allout-item-icon-keymap)
435 "Keymap used in the item cue area - the space between the icon and headline.")
436 ;;;_ = allout-escapes-category
437 (defvar allout-escapes-category nil
438 "Symbol for category of text property used to hide escapes of prefix-like
439 text in allout item bodies.")
440 ;;;_ = allout-guides-category
441 (defvar allout-guides-category nil
442 "Symbol carrying allout icon-guides overlay properties.")
443 ;;;_ = allout-guides-span-category
444 (defvar allout-guides-span-category nil
445 "Symbol carrying allout icon and guide lines overlay properties.")
446 ;;;_ = allout-icon-span-category
447 (defvar allout-icon-span-category nil
448 "Symbol carrying allout icon and guide lines overlay properties.")
449 ;;;_ = allout-cue-span-category
450 (defvar allout-cue-span-category nil
451 "Symbol carrying common properties of the space following the outline icon.
453 \(That space is used to convey selected cues indicating body qualities,
454 including things like:
455 - encryption '~'
456 - numbering '#'
457 - indirect reference '@'
458 - distinctive bullets - see `allout-distinctive-bullets-string'.\)")
459 ;;;_ = allout-span-to-category
460 (defvar allout-span-to-category
461 '((:guides-span . allout-guides-span-category)
462 (:cue-span . allout-cue-span-category)
463 (:icon-span . allout-icon-span-category)
464 (:body-span . allout-body-span-category))
465 "Association list mapping span identifier to category identifier.")
466 ;;;_ = allout-trailing-category
467 (defvar allout-trailing-category nil
468 "Symbol carrying common properties of an overlay's trailing newline.")
469 ;;;_ , Developer
470 (defvar allout-widgets-last-decoration-timing nil
471 "Timing details for the last cooperative decoration action.
473 This is maintained when `allout-widgets-time-decoration-activity' is set.
475 The value is a list containing two elements:
476 - the elapsed time as a number of seconds
477 - the list of changes processed, a la `allout-widgets-changes-record'.
479 When active, the value is revised each time automatic decoration activity
480 happens in the buffer.")
481 (make-variable-buffer-local 'allout-widgets-last-decoration-timing)
482 ;;;_ . mode hookup
483 ;;;_ > define-minor-mode allout-widgets-mode (arg)
484 ;;;###autoload
485 (define-minor-mode allout-widgets-mode
486 "Allout-mode extension, providing graphical decoration of outline structure.
488 This is meant to operate along with allout-mode, via `allout-mode-hook'.
490 If optional argument ARG is greater than 0, enable.
491 If optional argument ARG is less than 0, disable.
492 Anything else, toggle between active and inactive.
494 The graphics include:
496 - guide lines connecting item bullet-icons with those of their subitems.
498 - icons for item bullets, varying to indicate whether or not the item
499 has subitems, and if so, whether or not the item is expanded.
501 - cue area between the bullet-icon and the start of the body headline,
502 for item numbering, encryption indicator, and distinctive bullets.
504 The bullet-icon and guide line graphics provide keybindings and mouse
505 bindings for easy outline navigation and exposure control, extending
506 outline hot-spot navigation \(see `allout-mode')."
508 :lighter nil
509 :keymap nil
511 ;; define-minor-mode handles any provided argument according to emacs
512 ;; minor-mode conventions - '(elisp) Minor Mode Conventions' - and sets
513 ;; allout-widgets-mode accordingly *before* running the body code, so we
514 ;; cue on that.
515 (if allout-widgets-mode
516 ;; Activating:
517 (progn
518 (allout-add-resumptions
519 ;; XXX user may need say in line-truncation/hscrolling - an option
520 ;; that abstracts mode.
521 ;; truncate text lines to keep guide lines intact:
522 '(truncate-lines t)
523 ;; and enable autoscrolling to ease view of text
524 '(auto-hscroll-mode t)
525 '(line-move-ignore-fields t)
526 '(widget-push-button-prefix "")
527 '(widget-push-button-suffix "")
528 ;; allout-escaped-prefix-regexp depends on allout-regexp:
529 (list 'allout-escaped-prefix-regexp (concat "\\(\\\\\\)"
530 "\\(" allout-regexp "\\)")))
531 (allout-add-resumptions
532 (list 'allout-widgets-tally allout-widgets-tally)
533 (list 'allout-widgets-escapes-sanitization-regexp-pair
534 (list (concat "\\(\n\\|\\`\\)"
535 allout-escaped-prefix-regexp
537 ;; Include everything but the escape symbol.
538 "\\1\\3"))
541 (add-hook 'after-change-functions 'allout-widgets-after-change-handler
542 nil t)
544 (allout-setup-text-properties)
545 (add-to-invisibility-spec '(allout-torso . t))
546 (add-to-invisibility-spec 'allout-escapes)
548 (if (current-local-map)
549 (set-keymap-parent allout-item-body-keymap (current-local-map)))
551 (add-hook 'allout-exposure-change-hook
552 'allout-widgets-exposure-change-recorder nil 'local)
553 (add-hook 'allout-structure-added-hook
554 'allout-widgets-additions-recorder nil 'local)
555 (add-hook 'allout-structure-deleted-hook
556 'allout-widgets-deletions-recorder nil 'local)
557 (add-hook 'allout-structure-shifted-hook
558 'allout-widgets-shifts-recorder nil 'local)
559 (add-hook 'allout-after-copy-or-kill-hook
560 'allout-widgets-after-copy-or-kill-function nil 'local)
562 (add-hook 'before-change-functions 'allout-widgets-before-change-handler
563 nil 'local)
564 (add-hook 'post-command-hook 'allout-widgets-post-command-business
565 nil 'local)
566 (add-hook 'pre-command-hook 'allout-widgets-pre-command-business
567 nil 'local)
569 ;; init the widgets tally for debugging:
570 (if (not allout-widgets-tally)
571 (setq allout-widgets-tally (make-hash-table
572 :test 'eq :weakness 'key)))
573 ;; add tally count display on minor-mode-alist just after
574 ;; allout-mode entry.
575 ;; (we use ternary condition form to keep condition simple for deletion.)
576 (let* ((mode-line-entry '(allout-widgets-mode-inhibit ""
577 (:eval (allout-widgets-tally-string))))
578 (associated (assoc (car mode-line-entry) minor-mode-alist))
579 ;; need location for it only if not already present:
580 (after (and (not associated)
581 (memq (assq 'allout-mode minor-mode-alist) minor-mode-alist))))
582 (if after
583 (rplacd after (cons mode-line-entry (cdr after)))))
584 (allout-widgets-prepopulate-buffer)
586 ;; Deactivating:
587 (let ((inhibit-read-only t)
588 (was-modified (buffer-modified-p)))
590 (allout-widgets-undecorate-region (point-min)(point-max))
591 (remove-from-invisibility-spec '(allout-torso . t))
592 (remove-from-invisibility-spec 'allout-escapes)
594 (remove-hook 'after-change-functions
595 'allout-widgets-after-change-handler 'local)
596 (remove-hook 'allout-exposure-change-hook
597 'allout-widgets-exposure-change-recorder 'local)
598 (remove-hook 'allout-structure-added-hook
599 'allout-widgets-additions-recorder 'local)
600 (remove-hook 'allout-structure-deleted-hook
601 'allout-widgets-deletions-recorder 'local)
602 (remove-hook 'allout-structure-shifted-hook
603 'allout-widgets-shifts-recorder 'local)
604 (remove-hook 'allout-after-copy-or-kill-hook
605 'allout-widgets-after-copy-or-kill-function 'local)
606 (remove-hook 'before-change-functions
607 'allout-widgets-before-change-handler 'local)
608 (remove-hook 'post-command-hook
609 'allout-widgets-post-command-business 'local)
610 (remove-hook 'pre-command-hook
611 'allout-widgets-pre-command-business 'local)
612 (assq-delete-all 'allout-widgets-mode-inhibit minor-mode-alist)
613 (set-buffer-modified-p was-modified))))
614 ;;;_ > allout-widgets-mode-off
615 (defun allout-widgets-mode-off ()
616 "Explicitly disable allout-widgets-mode."
617 (allout-widgets-mode -1))
618 ;;;_ > allout-widgets-mode-off
619 (defun allout-widgets-mode-on ()
620 "Explicitly disable allout-widgets-mode."
621 (allout-widgets-mode 1))
622 ;;;_ > allout-setup-text-properties ()
623 (defun allout-setup-text-properties ()
624 "Configure category and literal text properties."
626 ;; XXX body - before-change, entry, keymap
628 (setplist 'allout-guides-span-category nil)
629 (put 'allout-guides-span-category
630 'modification-hooks '(allout-graphics-modification-handler))
631 (put 'allout-guides-span-category 'local-map allout-item-icon-keymap)
632 (put 'allout-guides-span-category 'mouse-face widget-button-face)
633 (put 'allout-guides-span-category 'field 'structure)
634 ;; (put 'allout-guides-span-category 'face 'widget-button)
636 (setplist 'allout-icon-span-category
637 (allout-widgets-copy-list (symbol-plist
638 'allout-guides-span-category)))
639 (put 'allout-icon-span-category 'field 'structure)
641 ;; XXX for body text we're instead going to use the buffer-wide
642 ;; resources, like before/after-change-functions hooks and the
643 ;; buffer's key map. that way we won't have to do painful provisions
644 ;; to fixup things after edits, catch outlier interstitial
645 ;; characters, like newline and empty lines after hidden subitems,
646 ;; etc.
647 (setplist 'allout-body-span-category nil)
648 (put 'allout-body-span-category 'evaporate t)
649 (put 'allout-body-span-category 'local-map allout-item-body-keymap)
650 ;;(put 'allout-body-span-category
651 ;; 'modification-hooks '(allout-body-modification-handler))
652 ;;(put 'allout-body-span-category 'field 'body)
654 (setplist 'allout-cue-span-category nil)
655 (put 'allout-cue-span-category 'evaporate t)
656 (put 'allout-cue-span-category
657 'modification-hooks '(allout-body-modification-handler))
658 (put 'allout-cue-span-category 'local-map allout-cue-span-keymap)
659 (put 'allout-cue-span-category 'mouse-face widget-button-face)
660 (put 'allout-cue-span-category 'pointer 'arrow)
661 (put 'allout-cue-span-category 'field 'structure)
663 (setplist 'allout-trailing-category nil)
664 (put 'allout-trailing-category 'evaporate t)
665 (put 'allout-trailing-category 'local-map allout-item-body-keymap)
667 (setplist 'allout-escapes-category nil)
668 (put 'allout-escapes-category 'invisible 'allout-escapes)
669 (put 'allout-escapes-category 'evaporate t))
670 ;;;_ > allout-widgets-prepopulate-buffer ()
671 (defun allout-widgets-prepopulate-buffer ()
672 "Step over the current buffers exposed items to do initial widgetizing."
673 (if (not allout-widgets-mode-inhibit)
674 (save-excursion
675 (goto-char (point-min))
676 (while (allout-next-visible-heading 1)
677 (when (not (widget-at (point)))
678 (allout-get-or-create-item-widget))))))
679 ;;;_ . settings context
680 ;;;_ = allout-container-item
681 (defvar allout-container-item-widget nil
682 "A widget for the current outline's overarching container as an item.
684 The item has settings \(of the file/connection\) and maybe a body, but no
685 icon/bullet.")
686 (make-variable-buffer-local 'allout-container-item-widget)
687 ;;;_ . Hooks and hook helpers
688 ;;;_ , major command-loop business:
689 ;;;_ > allout-widgets-pre-command-business (&optional recursing)
690 (defun allout-widgets-pre-command-business (&optional recursing)
691 "Handle actions pending before allout-mode activity."
693 ;;;_ > allout-widgets-post-command-business (&optional recursing)
694 (defun allout-widgets-post-command-business (&optional recursing)
695 "Handle actions pending after any allout-mode commands.
697 Optional RECURSING is for internal use, to limit recursion."
698 ;; - check changed text for nesting discontinuities and escape anything
699 ;; that's: (1) asterisks at bol or (2) excessively nested.
700 (condition-case failure
702 (when (and (boundp 'allout-mode) allout-mode)
704 (if allout-widgets-unset-inhibit-read-only
705 (setq inhibit-read-only nil
706 allout-widgets-unset-inhibit-read-only nil))
708 (when allout-widgets-reenable-before-change-handler
709 (add-hook 'before-change-functions
710 'allout-widgets-before-change-handler
711 nil 'local)
712 (setq allout-widgets-reenable-before-change-handler nil))
714 (when (or allout-widgets-undo-exposure-record
715 allout-widgets-changes-record)
716 (let* ((debug-on-signal t)
717 (debug-on-error t)
718 ;; inhibit recording new undo records when processing
719 ;; effects of undo-exposure:
720 (debugger 'allout-widgets-hook-error-handler)
721 (adjusting-message " Adjusting widgets...")
722 (replaced-message (allout-widgets-adjusting-message
723 adjusting-message))
724 (start-time (current-time)))
726 (if allout-widgets-undo-exposure-record
727 ;; inhibit undo recording iff undoing exposure stuff.
728 ;; XXX we might need to inhibit per respective
729 ;; change-record, rather than assuming that some undo
730 ;; activity during a command is all undo activity.
731 (let ((buffer-undo-list t))
732 (allout-widgets-exposure-undo-processor)
733 (allout-widgets-changes-dispatcher))
734 (allout-widgets-exposure-undo-processor)
735 (allout-widgets-changes-dispatcher))
737 (if allout-widgets-time-decoration-activity
738 (setq allout-widgets-last-decoration-timing
739 (list (allout-elapsed-time-seconds (current-time)
740 start-time)
741 allout-widgets-changes-record)))
743 (setq allout-widgets-changes-record nil)
745 (if replaced-message
746 (if (stringp replaced-message)
747 (message replaced-message)
748 (message "")))))
750 ;; Detect undecorated items, eg during isearch into previously
751 ;; unexposed topics, and decorate "economically". Some
752 ;; undecorated stuff is often exposed, to reduce lag, but the
753 ;; item containing the cursor is decorated. We constrain
754 ;; recursion to avoid being trapped by unexpectedly undecoratable
755 ;; items.
756 (when (and (not recursing)
757 (not (allout-current-decorated-p))
758 (or (not (equal (allout-depth) 0))
759 (not allout-container-item-widget)))
760 (let ((buffer-undo-list t))
761 (allout-widgets-exposure-change-recorder
762 allout-recent-prefix-beginning allout-recent-prefix-end nil)
763 (allout-widgets-post-command-business 'recursing)))
765 ;; Detect and rectify fouled outline structure - decorated item
766 ;; not at beginning of line.
767 (let ((this-widget (or (widget-at (point))
768 ;; XXX we really should be checking across
769 ;; edited span, not just point and point+1
770 (and (not (eq (point) (point-max)))
771 (widget-at (1+ (point))))))
772 inserted-at)
773 (save-excursion
774 (if (and this-widget
775 (goto-char (widget-get this-widget :from))
776 (not (bolp)))
777 (if (not
778 (condition-case err
779 (yes-or-no-p
780 (concat "Misplaced item won't be recognizable "
781 " as part of outline - rectify? "))
782 (quit nil)))
783 (progn
784 (if (allout-hidden-p (max (1- (point)) 1))
785 (save-excursion
786 (goto-char (max (1- (point)) 1))
787 (allout-show-to-offshoot)))
788 (allout-widgets-undecorate-item this-widget))
789 ;; expose any hidden intervening items, so resulting
790 ;; position is clear:
791 (setq inserted-at (point))
792 (allout-unprotected (insert-before-markers "\n"))
793 (forward-char -1)
794 ;; ensure the inserted newline is visible:
795 (allout-flag-region inserted-at (1+ inserted-at) nil)
796 (allout-widgets-post-command-business 'recursing)
797 (message (concat "outline structure corrected - item"
798 " moved to beginning of new line"))
799 ;; preserve cursor position in some cases:
800 (if (and inserted-at
801 (> (point) inserted-at))
802 (forward-char -1)))))))
804 (error
805 ;; zero work list so we don't get stuck futily retrying.
806 ;; error recording done by allout-widgets-hook-error-handler.
807 (setq allout-widgets-changes-record nil))))
808 ;;;_ , major change handlers:
809 ;;;_ > allout-widgets-before-change-handler
810 (defun allout-widgets-before-change-handler (beg end)
811 "Business to be done before changes in a widgetized allout outline."
812 ;; protect against unruly edits to structure:
813 (cond
814 (undo-in-progress (when (eq (get-text-property beg 'category)
815 'allout-icon-span-category)
816 (save-excursion
817 (goto-char beg)
818 (let* ((item-widget (allout-get-item-widget)))
819 (if item-widget
820 (allout-widgets-exposure-undo-recorder
821 item-widget))))))
822 (inhibit-read-only t)
823 ((not (and (boundp 'allout-mode) allout-mode)) t)
824 ((equal this-command 'quoted-insert) t)
825 ((not (text-property-any beg (if (equal end beg)
826 (min (1+ beg) (point-max))
827 end)
828 'field 'structure))
830 ((yes-or-no-p "Unruly edit of outline structure - allow? ")
831 (setq allout-widgets-unset-inhibit-read-only (not inhibit-read-only)
832 inhibit-read-only t))
834 ;; tell the allout-widgets-post-command-business to reestablish the hook:
835 (setq allout-widgets-reenable-before-change-handler t)
836 ;; and raise an error to prevent the edit (and disable the hook):
837 (error
838 (substitute-command-keys allout-structure-unruly-deletion-message)))))
839 ;;;_ > allout-widgets-after-change-handler
840 (defun allout-widgets-after-change-handler (beg end prelength)
841 "Reconcile what needs to be reconciled for allout widgets after edits."
843 ;;;_ > allout-current-decorated-p ()
844 (defun allout-current-decorated-p ()
845 "True if the current item is not decorated"
846 (save-excursion
847 (if (allout-back-to-current-heading)
848 (if (> allout-recent-depth 0)
849 (and (allout-get-item-widget) t)
850 allout-container-item-widget))))
852 ;;;_ > allout-widgets-hook-error-handler
853 (defun allout-widgets-hook-error-handler (mode args)
854 "Process errors which occurred in the course of command hook operation.
856 We store a backtrace of the error information in the variable,
857 `allout-widgets-last-hook-error', unset the error handlers, and
858 reraise the error, so that processing continues to the
859 encompassing condition-case."
860 ;; first deconstruct special error environment so errors here propagate
861 ;; to encompassing condition-case:
862 (setq debugger 'debug
863 debug-on-error nil
864 debug-on-signal nil)
865 (let* ((bt (with-output-to-string (backtrace)))
866 (this "allout-widgets-hook-error-handler")
867 (header
868 (format "allout-widgets-last-hook-error stored, %s/%s %s %s"
869 this mode args
870 (format-time-string "%e-%b-%Y %r" (current-time)))))
871 ;; post to *Messages* then immediately replace with more compact notice:
872 (message "%s" (setq allout-widgets-last-hook-error
873 (format "%s:\n%s" header bt)))
874 (message header) (sit-for allout-widgets-hook-error-post-time)
875 ;; reraise the error, or one concerning this function if unexpected:
876 (if (equal mode 'error)
877 (apply 'signal args)
878 (error "%s: unexpected mode, %s %s" this mode args))))
879 ;;;_ > allout-widgets-changes-exceed-threshold-p ()
880 (defun allout-widgets-adjusting-message (message)
881 "Post MESSAGE when pending are likely to make a big enough delay.
883 If posting of the MESSAGE is warranted and there already is a
884 `current-message' in the minibuffer, the MESSAGE is appended to
885 the current one, and the previously pending `current-message' is
886 returned for later posting on completion.
888 If posting of the MESSAGE is warranted, but no `current-message'
889 is pending, then t is returned to indicate that case.
891 If posting of the MESSAGE is not warranted, then nil is returned.
893 See `allout-widgets-adjust-message-length-threshold',
894 `allout-widgets-adjust-message-size-threshold' for message
895 posting threshold criteria."
896 (if (or (> (length allout-widgets-changes-record)
897 allout-widgets-adjust-message-length-threshold)
898 ;; for size, use distance from start of first to end of last:
899 (let ((min (point-max))
900 (max 0)
901 first second)
902 (mapc (function (lambda (entry)
903 (if (eq :undone-exposure (car entry))
905 (setq first (cadr entry)
906 second (caddr entry))
907 (if (< (min first second) min)
908 (setq min (min first second)))
909 (if (> (max first second) max)
910 (setq max (max first second))))))
911 allout-widgets-changes-record)
912 (> (- max min) allout-widgets-adjust-message-size-threshold)))
913 (let ((prior (current-message)))
914 (message (if prior (concat prior " - " message) message))
915 (or prior t))))
916 ;;;_ > allout-widgets-changes-dispatcher ()
917 (defun allout-widgets-changes-dispatcher ()
918 "Dispatch CHANGES-RECORD items to respective widgets change processors."
920 (if (not allout-widgets-mode-inhibit)
921 (let* ((changes-record allout-widgets-changes-record)
922 (changes-pending (and changes-record t))
923 entry
924 exposures
925 additions
926 deletions
927 shifts)
929 (when changes-pending
930 (while changes-record
931 (setq entry (pop changes-record))
932 (case (car entry)
933 (:exposed (push entry exposures))
934 (:added (push entry additions))
935 (:deleted (push entry deletions))
936 (:shifted (push entry shifts))))
938 (if exposures
939 (allout-widgets-exposure-change-processor exposures))
940 (if additions
941 (allout-widgets-additions-processor additions))
942 (if deletions
943 (allout-widgets-deletions-processor deletions))
944 (if shifts
945 (allout-widgets-shifts-processor shifts))))
946 (when (not (equal allout-widgets-mode-inhibit 'undecorated))
947 (allout-widgets-undecorate-region (point-min)(point-max))
948 (setq allout-widgets-mode-inhibit 'undecorated))))
949 ;;;_ > allout-widgets-exposure-change-recorder (from to flag)
950 (defun allout-widgets-exposure-change-recorder (from to flag)
951 "Record allout exposure changes for tracking during post-command processing.
953 Records changes in `allout-widgets-changes-record'."
954 (push (list :exposed from to flag) allout-widgets-changes-record))
955 ;;;_ > allout-widgets-exposure-change-processor (changes)
956 (defun allout-widgets-exposure-change-processor (changes)
957 "Widgetize and adjust item widgets tracking allout outline exposure changes.
959 Generally invoked via `allout-exposure-change-hook'."
961 (let ((changes (sort changes (function (lambda (this next)
962 (< (cadr this) (cadr next))))))
963 ;; have to distinguish between concealing and exposing so that, eg,
964 ;; `allout-expose-topic's mix is handled properly.
965 handled-expose
966 handled-conceal
967 covered
968 deactivate-mark)
970 (dolist (change changes)
971 (let (handling
972 (from (cadr change))
973 bucket got
974 (to (caddr change))
975 (flag (cadddr change))
976 parent)
978 ;; swap from and to:
979 (if (< to from) (setq bucket to
980 to from
981 from bucket))
983 ;; have we already handled exposure changes in this region?
984 (setq handling (if flag 'handled-conceal 'handled-expose)
985 got (allout-range-overlaps from to (symbol-value handling))
986 covered (car got))
987 (set handling (cadr got))
989 (when (not covered)
990 (save-excursion
991 (goto-char from)
992 (cond
994 ;; collapsing:
995 (flag
996 (allout-widgets-undecorate-region from to)
997 (allout-beginning-of-current-line)
998 (let ((widget (allout-get-item-widget)))
999 (if (not widget)
1000 (allout-get-or-create-item-widget)
1001 (widget-apply widget :redecorate))))
1003 ;; expanding:
1005 (while (< (point) to)
1006 (allout-beginning-of-current-line)
1007 (setq parent (allout-get-item-widget))
1008 (if (not parent)
1009 (setq parent (allout-get-or-create-item-widget))
1010 (widget-apply parent :redecorate))
1011 (allout-next-visible-heading 1)
1012 (if (widget-get parent :has-subitems)
1013 (allout-redecorate-visible-subtree parent))
1014 (if (> (point) to)
1015 ;; subtree may be well beyond to - incorporate in ranges:
1016 (setq handled-expose
1017 (allout-range-overlaps from (point) handled-expose)
1018 covered (car handled-expose)
1019 handled-expose (cadr handled-expose)))
1020 (allout-next-visible-heading 1))))))))))
1022 ;;;_ > allout-widgets-additions-recorder (from to)
1023 (defun allout-widgets-additions-recorder (from to)
1024 "Record allout item additions for tracking during post-command processing.
1026 Intended for use on `allout-structure-added-hook'.
1028 FROM point at the start of the first new item and TO is point at the start
1029 of the last one.
1031 Records changes in `allout-widgets-changes-record'."
1032 (push (list :added from to) allout-widgets-changes-record))
1033 ;;;_ > allout-widgets-additions-processor (changes)
1034 (defun allout-widgets-additions-processor (changes)
1035 "Widgetize and adjust items tracking allout outline structure additions.
1037 Dispatched by `allout-widgets-post-command-business' in response to
1038 :added entries recorded by `allout-widgets-additions-recorder'."
1039 (save-excursion
1040 (let (handled
1041 covered)
1042 (dolist (change changes)
1043 (let ((from (cadr change))
1044 bucket
1045 (to (caddr change)))
1046 (if (< to from) (setq bucket to to from from bucket))
1047 ;; have we already handled exposure changes in this region?
1048 (setq handled (allout-range-overlaps from to handled)
1049 covered (car handled)
1050 handled (cadr handled))
1051 (when (not covered)
1052 (goto-char from)
1053 ;; Prior sibling and parent can both be affected.
1054 (if (allout-ascend)
1055 (allout-redecorate-visible-subtree
1056 (allout-get-or-create-item-widget 'redecorate)))
1057 (if (< (point) from)
1058 (goto-char from))
1059 (while (and (< (point) to) (not (eobp)))
1060 (allout-beginning-of-current-line)
1061 (allout-redecorate-visible-subtree
1062 (allout-get-or-create-item-widget))
1063 (allout-next-visible-heading 1))
1064 (if (> (point) to)
1065 ;; subtree may be well beyond to - incorporate in ranges:
1066 (setq handled (allout-range-overlaps from (point) handled)
1067 covered (car handled)
1068 handled (cadr handled)))))))))
1070 ;;;_ > allout-widgets-deletions-recorder (depth from)
1071 (defun allout-widgets-deletions-recorder (depth from)
1072 "Record allout item deletions for tracking during post-command processing.
1074 Intended for use on `allout-structure-deleted-hook'.
1076 DEPTH is the depth of the deleted subtree, and FROM is the point from which
1077 the subtree was deleted.
1079 Records changes in `allout-widgets-changes-record'."
1080 (push (list :deleted depth from) allout-widgets-changes-record))
1081 ;;;_ > allout-widgets-deletions-processor (changes)
1082 (defun allout-widgets-deletions-processor (changes)
1083 "Adjust items tracking allout outline structure deletions.
1085 Dispatched by `allout-widgets-post-command-business' in response to
1086 :deleted entries recorded by `allout-widgets-deletions-recorder'."
1087 (save-excursion
1088 (dolist (change changes)
1089 (let ((depth (cadr change))
1090 (from (caddr change)))
1091 (goto-char from)
1092 (when (allout-previous-visible-heading 1)
1093 (if (> depth 1)
1094 (allout-ascend-to-depth (1- depth)))
1095 (allout-redecorate-visible-subtree
1096 (allout-get-or-create-item-widget 'redecorate)))))))
1098 ;;;_ > allout-widgets-shifts-recorder (shifted-amount at)
1099 (defun allout-widgets-shifts-recorder (shifted-amount at)
1100 "Record outline subtree shifts for tracking during post-command processing.
1102 Intended for use on `allout-structure-shifted-hook'.
1104 SHIFTED-AMOUNT is the depth change and AT is the point at the start of the
1105 subtree that's been shifted.
1107 Records changes in `allout-widgets-changes-record'."
1108 (push (list :shifted shifted-amount at) allout-widgets-changes-record))
1109 ;;;_ > allout-widgets-shifts-processor (changes)
1110 (defun allout-widgets-shifts-processor (changes)
1111 "Widgetize and adjust items tracking allout outline structure additions.
1113 Dispatched by `allout-widgets-post-command-business' in response to
1114 :shifted entries recorded by `allout-widgets-shifts-recorder'."
1115 (save-excursion
1116 (dolist (change changes)
1117 (goto-char (caddr change))
1118 (allout-ascend)
1119 (allout-redecorate-visible-subtree))))
1120 ;;;_ > allout-widgets-after-copy-or-kill-function ()
1121 (defun allout-widgets-after-copy-or-kill-function ()
1122 "Do allout-widgets processing of text just placed in the kill ring.
1124 Intended for use on allout-after-copy-or-kill-hook."
1125 (if (car kill-ring)
1126 (setcar kill-ring (allout-widgets-undecorate-text (car kill-ring)))))
1128 ;;;_ > allout-widgets-exposure-undo-recorder (widget from-state)
1129 (defun allout-widgets-exposure-undo-recorder (widget)
1130 "Record outline exposure undo for tracking during post-command processing.
1132 Intended for use by `allout-graphics-modification-handler'.
1134 WIDGET is the widget being changed.
1136 Records changes in `allout-widgets-changes-record'."
1137 ;; disregard the events if we're currently processing them.
1138 (if (not allout-undo-exposure-in-progress)
1139 (push widget allout-widgets-undo-exposure-record)))
1140 ;;;_ > allout-widgets-exposure-undo-processor ()
1141 (defun allout-widgets-exposure-undo-processor ()
1142 "Adjust items tracking undo of allout outline structure exposure.
1144 Dispatched by `allout-widgets-post-command-business' in response to
1145 :undone-exposure entries recorded by `allout-widgets-exposure-undo-recorder'."
1146 (let* ((allout-undo-exposure-in-progress t)
1147 ;; inhibit undo recording while twiddling exposure to track undo:
1148 (widgets allout-widgets-undo-exposure-record)
1149 widget widget-start-marker widget-end-marker
1150 from-state icon-start-point to-state
1151 handled covered)
1152 (setq allout-widgets-undo-exposure-record nil)
1153 (save-excursion
1154 (dolist (widget widgets)
1155 (setq widget-start-marker (widget-get widget :from)
1156 widget-end-marker (widget-get widget :to)
1157 from-state (widget-get widget :icon-state)
1158 icon-start-point (widget-apply widget :actual-position
1159 :icon-start)
1160 to-state (get-text-property icon-start-point
1161 :icon-state))
1162 (setq handled (allout-range-overlaps widget-start-marker
1163 widget-end-marker
1164 handled)
1165 covered (car handled)
1166 handled (cadr handled))
1167 (when (not covered)
1168 (goto-char (widget-get widget :from))
1169 (when (not (allout-hidden-p))
1170 ;; adjust actual exposure to that of to-state viz from-state
1171 (cond ((and (eq to-state 'closed) (eq from-state 'opened))
1172 (allout-hide-current-subtree)
1173 (allout-decorate-item-and-context widget))
1174 ((and (eq to-state 'opened) (eq from-state 'closed))
1175 (save-excursion
1176 (dolist
1177 (expose-to (allout-chart-exposure-contour-by-icon))
1178 (goto-char expose-to)
1179 (allout-show-to-offshoot)))))))))))
1180 ;;;_ > allout-chart-exposure-contour-by-icon (&optional from-depth)
1181 (defun allout-chart-exposure-contour-by-icon (&optional from-depth)
1182 "Return points of subtree items to which exposure should be extended.
1184 The qualifying items are ones with a widget icon that is in the closed or
1185 empty state, or items with undecorated subitems.
1187 The resulting list of points is in reverse order.
1189 Optional FROM-DEPTH is for internal use."
1190 ;; During internal recursion, we return a pair: (at-end . result)
1191 ;; Otherwise we just return the result.
1192 (let ((from-depth from-depth)
1193 start-point
1194 at-end level-depth
1195 this-widget
1196 got subgot)
1197 (if from-depth
1198 (setq level-depth (allout-depth))
1199 ;; at containing item:
1200 (setq start-point (point))
1201 (setq from-depth (allout-depth))
1202 (setq at-end (not (allout-next-heading))
1203 level-depth allout-recent-depth))
1205 ;; traverse the level, recursing on deeper levels:
1206 (while (and (not at-end)
1207 (> allout-recent-depth from-depth)
1208 (setq this-widget (allout-get-item-widget)))
1209 (if (< level-depth allout-recent-depth)
1210 ;; recurse:
1211 (progn
1212 (setq subgot (allout-chart-exposure-contour-by-icon level-depth)
1213 at-end (car subgot)
1214 subgot (cdr subgot))
1215 (if subgot (setq got (append subgot got))))
1216 ;; progress at this level:
1217 (when (memq (widget-get this-widget :icon-state) '(closed empty))
1218 (push (point) got)
1219 (allout-end-of-subtree))
1220 (setq at-end (not (allout-next-heading)))))
1222 ;; tailor result depending on whether or not we're a recursion:
1223 (if (not start-point)
1224 (cons at-end got)
1225 (goto-char start-point)
1226 got)))
1227 ;;;_ > allout-range-overlaps (from to ranges)
1228 (defun allout-range-overlaps (from to ranges)
1229 "Return a pair indicating overlap of FROM and TO subtree range in RANGES.
1231 First element of result indicates whether candadate range FROM, TO
1232 overlapped any of the existing ranges.
1234 Second element of result is a new version of RANGES incorporating the
1235 candidate range with overlaps consolidated.
1237 FROM and TO must be in increasing order, as must be the pairs in RANGES."
1238 ;; to append to the end: (rplacd next-to-last-cdr (list 'f))
1239 (let (new-ranges
1240 entry
1241 ;; the start of the range that includes the candidate from:
1242 included-from
1243 ;; the end of the range that includes the candidate to:
1244 included-to
1245 ;; the candidates were inserted:
1246 done)
1247 (while (and ranges (not done))
1248 (setq entry (car ranges)
1249 ranges (cdr ranges))
1251 (cond
1253 (included-from
1254 ;; some entry included the candidate from.
1255 (cond ((> (car entry) to)
1256 ;; current entry exceeds end of candidate range - done.
1257 (push (list included-from to) new-ranges)
1258 (push entry new-ranges)
1259 (setq included-to to
1260 done t))
1261 ((>= (cadr entry) to)
1262 ;; current entry includes end of candidate range - done.
1263 (push (list included-from (cadr entry)) new-ranges)
1264 (setq included-to (cadr entry)
1265 done t))
1266 ;; current entry contained in candidate range - ditch, continue:
1267 (t nil)))
1269 ((> (car entry) to)
1270 ;; current entry start exceeds candidate end - done, placed as new entry
1271 (push (list from to) new-ranges)
1272 (push entry new-ranges)
1273 (setq included-to to
1274 done t))
1276 ((>= (car entry) from)
1277 ;; current entry start is above candidate start, but not above
1278 ;; candidate end (by prior case).
1279 (setq included-from from)
1280 ;; now we have to check on whether this entry contains to, or continue:
1281 (when (>= (cadr entry) to)
1282 ;; current entry contains only candidate end - done:
1283 (push (list included-from (cadr entry)) new-ranges)
1284 (setq included-to (cadr entry)
1285 done t))
1286 ;; otherwise, we will continue to look for placement of candidate end.
1289 ((>= (cadr entry) to)
1290 ;; current entry properly contains candidate range.
1291 (push entry new-ranges)
1292 (setq included-from (car entry)
1293 included-to (cadr entry)
1294 done t))
1296 ((>= (cadr entry) from)
1297 ;; current entry contains start of candidate range.
1298 (setq included-from (car entry)))
1301 ;; current entry is below the candidate range.
1302 (push entry new-ranges))))
1304 (cond ((and included-from included-to)
1305 ;; candidates placed.
1306 nil)
1307 ((not (or included-from included-to))
1308 ;; candidates found no place, must be at the end:
1309 (push (list from to) new-ranges))
1310 (included-from
1311 ;; candidate start placed but end not:
1312 (push (list included-from to) new-ranges))
1313 ;; might be included-to and not included-from, indicating new entry.
1315 (setq new-ranges (nreverse new-ranges))
1316 (if ranges (setq new-ranges (append new-ranges ranges)))
1317 (list (if included-from t) new-ranges)))
1318 ;;;_ > allout-test-range-overlaps ()
1319 (defun allout-test-range-overlaps ()
1320 "allout-range-overlaps unit tests."
1321 (let* (ranges
1323 (try (lambda (from to)
1324 (setq got (allout-range-overlaps from to ranges))
1325 (setq ranges (cadr got))
1326 got)))
1327 ;; ;; biggie:
1328 ;; (setq ranges nil)
1329 ;; ;; ~ .02 to .1 seconds for just repeated listing args instead of funcall
1330 ;; ;; ~ 13 seconds for doing repeated funcall
1331 ;; (message "time-trial: %s, resulting size %s"
1332 ;; (time-trial
1333 ;; '(let ((size 10000)
1334 ;; doing)
1335 ;; (random t)
1336 ;; (dotimes (count size)
1337 ;; (setq doing (random size))
1338 ;; (funcall try doing (+ doing (random 5)))
1339 ;; ;;(list doing (+ doing (random 5)))
1340 ;; )))
1341 ;; (length ranges))
1342 ;; (sit-for 2)
1344 ;; fresh:
1345 (setq ranges nil)
1346 (assert (equal (funcall try 3 5) '(nil ((3 5)))))
1347 ;; add range at end:
1348 (assert (equal (funcall try 10 12) '(nil ((3 5) (10 12)))))
1349 ;; add range at beginning:
1350 (assert (equal (funcall try 1 2) '(nil ((1 2) (3 5) (10 12)))))
1351 ;; insert range somewhere in the middle:
1352 (assert (equal (funcall try 7 9) '(nil ((1 2) (3 5) (7 9) (10 12)))))
1353 ;; consolidate some:
1354 (assert (equal (funcall try 5 8) '(t ((1 2) (3 9) (10 12)))))
1355 ;; add more:
1356 (assert (equal (funcall try 15 17) '(nil ((1 2) (3 9) (10 12) (15 17)))))
1357 ;; add more:
1358 (assert (equal (funcall try 20 22)
1359 '(nil ((1 2) (3 9) (10 12) (15 17) (20 22)))))
1360 ;; encompass more:
1361 (assert (equal (funcall try 4 11) '(t ((1 2) (3 12) (15 17) (20 22)))))
1362 ;; encompass all:
1363 (assert (equal (funcall try 2 25) '(t ((1 25)))))
1365 ;; fresh slate:
1366 (setq ranges nil)
1367 (assert (equal (funcall try 20 25) '(nil ((20 25)))))
1368 (assert (equal (funcall try 30 35) '(nil ((20 25) (30 35)))))
1369 (assert (equal (funcall try 26 28) '(nil ((20 25) (26 28) (30 35)))))
1370 (assert (equal (funcall try 15 20) '(t ((15 25) (26 28) (30 35)))))
1371 (assert (equal (funcall try 10 30) '(t ((10 35)))))
1372 (assert (equal (funcall try 5 6) '(nil ((5 6) (10 35)))))
1373 (assert (equal (funcall try 2 100) '(t ((2 100)))))
1375 (setq ranges nil)
1377 ;;;_ > allout-widgetize-buffer (&optional doing)
1378 (defun allout-widgetize-buffer (&optional doing)
1379 "EXAMPLE FUNCTION. Widgetize items in buffer using allout-chart-subtree.
1381 We economize by just focusing on the first of local-maximum depth siblings.
1383 Optional DOING is for internal use - a chart of the current level, for
1384 recursive operation."
1386 (interactive)
1387 (if (not doing)
1389 (save-excursion
1390 (goto-char (point-min))
1391 ;; Construct the chart by scanning the siblings:
1392 (dolist (top-level-sibling (allout-chart-siblings))
1393 (goto-char top-level-sibling)
1394 (let ((subchart (allout-chart-subtree)))
1395 (if subchart
1396 (allout-widgetize-buffer subchart)))))
1398 ;; save-excursion was done on recursion entry, not necessary here.
1399 (let (have-sublists)
1400 (dolist (sibling doing)
1401 (when (listp sibling)
1402 (setq have-sublists t)
1403 (allout-widgetize-buffer sibling)))
1404 (when (and (not have-sublists) (not (widget-at (car doing))))
1405 (goto-char (car doing))
1406 (allout-get-or-create-item-widget)))))
1408 ;;;_ : Item widget and constructors
1410 ;;;_ $ allout-item-widget
1411 (define-widget 'allout-item-widget 'default
1412 "A widget presenting an allout outline item."
1414 'button nil
1415 ;; widget-field-at respects this to get item if 'field is unused.
1416 ;; we don't use field to avoid collision with end-of-line, etc, on which
1417 ;; allout depends.
1418 'real-field nil
1420 ;; data fields:
1423 ;; tailor the widget for a specific item
1424 :create 'allout-decorate-item-and-context
1425 :value-delete 'allout-widgets-undecorate-item
1426 ;; Not Yet Converted (from original, tree-widget stab)
1427 :expander 'allout-tree-event-dispatcher ; get children when nil :args
1428 :expander-p 'identity ; always engage the :expander
1429 :action 'allout-tree-widget-action
1430 ;; :notify "when item changes"
1432 ;; force decoration of item but not context, unless already done this tick:
1433 :redecorate 'allout-redecorate-item
1434 :last-decorated-tick nil
1435 ;; recognize the actual situation of the item's text:
1436 :parse-item 'allout-parse-item-at-point
1437 ;; decorate the entirety of the item, sans offspring:
1438 :decorate-item-span 'allout-decorate-item-span
1439 ;; decorate the various item elements:
1440 :decorate-guides 'allout-decorate-item-guides
1441 :decorate-icon 'allout-decorate-item-icon
1442 :decorate-cue 'allout-decorate-item-cue
1443 :decorate-body 'allout-decorate-item-body
1444 :actual-position 'allout-item-actual-position
1446 ;; Layout parameters:
1447 :is-container nil ; is this actually the encompassing file/connection?
1449 :from nil ; item beginning - marker
1450 :to nil ; item end - marker
1451 :span-overlay nil ; overlay by which actual postion is determined
1453 ;; also serves as guide-end:
1454 :icon-start nil
1455 :icon-end nil
1456 :distinctive-start nil
1457 ;; also serves as cue-start:
1458 :distinctive-end nil
1459 ;; also serves as cue-end:
1460 :body-start nil
1461 :body-end nil
1462 :depth nil
1463 :has-subitems nil
1464 :was-has-subitems 'init
1465 :expanded nil
1466 :was-expanded 'init
1467 :brief nil
1468 :was-brief 'init
1470 :does-encrypt nil ; pending encryption when :is-encrypted false.
1471 :is-encrypted nil
1473 ;; the actual location of the item text:
1474 :location 'allout-item-location
1476 :button-keymap allout-item-icon-keymap ; XEmacs
1477 :keymap allout-item-icon-keymap ; Emacs
1479 ;; Element regions:
1480 :guides-span nil
1481 :icon-span nil
1482 :cue-span nil
1483 :bullet nil
1484 :was-bullet nil
1485 :body-span nil
1487 :body-brevity-p 'allout-body-brevity-p
1489 ;; :guide-column-flags indicate (in reverse order) whether or not the
1490 ;; item's ancestor at the depth corresponding to the column has a
1491 ;; subsequent sibling - ie, whether or not the corresponding column needs
1492 ;; a descender line to connect that ancestor with its sibling.
1493 :guide-column-flags nil
1494 :was-guide-column-flags 'init
1496 ;; ie, has subitems:
1497 :populous-p 'allout-item-populous-p
1498 :help-echo 'allout-tree-widget-help-echo
1500 ;;;_ > allout-new-item-widget ()
1501 (defsubst allout-new-item-widget ()
1502 "create a new item widget, not yet situated anywhere."
1503 (if allout-widgets-maintain-tally
1504 ;; all the extra overhead is incurred only when doing the
1505 ;; maintenance, except the condition, which can't be avoided.
1506 (let ((widget (widget-convert 'allout-item-widget)))
1507 (puthash widget nil allout-widgets-tally)
1508 widget)
1509 (widget-convert 'allout-item-widget)))
1510 ;;;_ : Item decoration
1511 ;;;_ > allout-decorate-item-and-context (item-widget &optional redecorate
1512 ;;; blank-container parent)
1513 (defun allout-decorate-item-and-context (item-widget &optional redecorate
1514 blank-container parent)
1515 "Create or adjust widget decorations for ITEM-WIDGET and neighbors at point.
1517 The neighbors include its siblings and parent.
1519 ITEM-WIDGET can be a created or converted allout-item-widget.
1521 If you're only trying to get or create a widget for an item, use
1522 `allout-get-or-create-item-widget'. If you have the item-widget, applying
1523 :redecorate will do the right thing.
1525 Optional BLANK-CONTAINER is for internal use. It is used to fabricate a
1526 container widget for an empty-bodied container, in the course of decorating
1527 a proper \(non-container\) item which starts at the beginning of the file.
1529 Optional REDECORATE causes redecoration of the item-widget and
1530 its siblings, even if already decorated in this cycle of the command loop.
1532 Optional PARENT, when provided, bypasses some navigation and computation
1533 necessary to obtain the parent of the items being processed.
1535 We return the item-widget corresponding to the item at point."
1537 (when (or redecorate
1538 (not (equal (widget-get item-widget :last-decorated-tick)
1539 allout-command-counter)))
1540 (let* ((allout-inhibit-body-modification-hook t)
1541 (was-modified (buffer-modified-p))
1542 (was-point (point))
1543 prefix-start
1544 (is-container (or blank-container
1545 (not (setq prefix-start (allout-goto-prefix)))
1546 (< was-point prefix-start)))
1547 ;; steady-point (set in two steps) is reliable across parent
1548 ;; widget-creation.
1549 (steady-point (progn (if is-container (goto-char 1))
1550 (point-marker)))
1551 (steady-point (progn (set-marker-insertion-type steady-point t)
1552 steady-point))
1553 (parent (and (not is-container)
1554 (allout-get-or-create-parent-widget)))
1555 parent-flags parent-depth
1556 successor-sibling
1557 body
1558 doing-item
1559 sub-item-widget
1560 depth
1561 reverse-siblings-chart
1562 (buffer-undo-list t))
1564 ;; At this point the parent is decorated and parent-flags indicate
1565 ;; its guide lines. We will iterate over the siblings according to a
1566 ;; chart we create at the start, and going from last to first so we
1567 ;; don't have to worry about text displacement caused by widgetizing.
1569 (if is-container
1570 (progn (widget-put item-widget :is-container t)
1571 (setq reverse-siblings-chart (list 1)))
1572 (goto-char (widget-apply parent :actual-position :from))
1573 (if (widget-get parent :is-container)
1574 ;; `allout-goto-prefix' will go to first non-container item:
1575 (allout-goto-prefix)
1576 (allout-next-heading))
1577 (setq depth (allout-recent-depth))
1578 (setq reverse-siblings-chart (list allout-recent-prefix-beginning))
1579 (while (allout-next-sibling)
1580 (push allout-recent-prefix-beginning reverse-siblings-chart)))
1582 (dolist (doing-at reverse-siblings-chart)
1583 (goto-char doing-at)
1584 (when allout-widgets-track-decoration
1585 (sit-for 0))
1587 (setq doing-item (if (= doing-at steady-point)
1588 item-widget
1589 (or (allout-get-item-widget)
1590 (allout-new-item-widget))))
1592 (when (or redecorate (not (equal (widget-get doing-item
1593 :last-decorated-tick)
1594 allout-command-counter)))
1595 (widget-apply doing-item :parse-item t blank-container)
1596 (widget-apply doing-item :decorate-item-span)
1598 (widget-apply doing-item :decorate-guides
1599 parent (and successor-sibling t))
1600 (widget-apply doing-item :decorate-icon)
1601 (widget-apply doing-item :decorate-cue)
1602 (widget-apply doing-item :decorate-body)
1604 (widget-put doing-item :last-decorated-tick allout-command-counter))
1606 (setq successor-sibling doing-at))
1608 (set-buffer-modified-p was-modified)
1609 (goto-char steady-point)
1610 ;; must null the marker or the buffer gets clogged with impedence:
1611 (set-marker steady-point nil)
1613 item-widget)))
1614 ;;;_ > allout-redecorate-item (item)
1615 (defun allout-redecorate-item (item-widget)
1616 "Resituate ITEM-WIDGET decorations, disregarding context.
1618 Use this to redecorate only the item, when you know that it's
1619 situation with respect to siblings, parent, and offspring is
1620 unchanged from its last decoration. Use
1621 `allout-decorate-item-and-context' instead to reassess and adjust
1622 relevent context, when suitable."
1623 (if (not (equal (widget-get item-widget :last-decorated-tick)
1624 allout-command-counter))
1625 (let ((was-modified (buffer-modified-p))
1626 (buffer-undo-list t))
1627 (widget-apply item-widget :parse-item)
1628 (widget-apply item-widget :decorate-guides)
1629 (widget-apply item-widget :decorate-icon)
1630 (widget-apply item-widget :decorate-cue)
1631 (widget-apply item-widget :decorate-body)
1632 (set-buffer-modified-p was-modified))))
1633 ;;;_ > allout-redecorate-visible-subtree (&optional parent-widget
1634 ;;; depth chart)
1635 (defun allout-redecorate-visible-subtree (&optional parent-widget depth chart)
1636 "Redecorate all visible items in subtree at point.
1638 Optional PARENT-WIDGET is for optimization, when the parent
1639 widget is already available.
1641 Optional DEPTH restricts the excursion depth of covered.
1643 Optional CHART is for internal recursion, to carry a chart of the
1644 target items.
1646 Point is left at the last sibling in the visible subtree."
1647 ;; using a treatment that takes care of all the siblings on a level, we
1648 ;; only need apply it to the first sibling on the level, and we can
1649 ;; collect and pass the parent of the lower levels to recursive calls as
1650 ;; we go.
1651 (let ((parent-widget
1652 (if (and parent-widget (widget-apply parent-widget
1653 :actual-position :from))
1654 (progn (goto-char (widget-apply parent-widget
1655 :actual-position :from))
1656 parent-widget)
1657 (let ((got (allout-get-item-widget)))
1658 (if got
1659 (allout-decorate-item-and-context got 'redecorate)
1660 (allout-get-or-create-item-widget 'redecorate)))))
1661 (pending-chart (or chart (allout-chart-subtree nil 'visible)))
1662 item-widget
1663 previous-sibling-point
1664 previous-sibling
1665 recent-sibling-point)
1666 (setq pending-chart (nreverse pending-chart))
1667 (dolist (sibling-point pending-chart)
1668 (cond ((integerp sibling-point)
1669 (when (not previous-sibling-point)
1670 (goto-char sibling-point)
1671 (if (setq item-widget (allout-get-item-widget nil))
1672 (allout-decorate-item-and-context item-widget 'redecorate
1673 nil parent-widget)
1674 (allout-get-or-create-item-widget)))
1675 (setq previous-sibling-point sibling-point
1676 recent-sibling-point sibling-point))
1677 ((listp sibling-point)
1678 (if (or (not depth)
1679 (> depth 1))
1680 (allout-redecorate-visible-subtree
1681 (if (not previous-sibling-point)
1682 ;; containment discontinuity - sigh
1683 parent-widget
1684 (allout-get-or-create-item-widget 'redecorate))
1685 (if depth (1- depth))
1686 sibling-point)))))
1687 (if (and recent-sibling-point (< (point) recent-sibling-point))
1688 (goto-char recent-sibling-point))))
1689 ;;;_ > allout-parse-item-at-point (item-widget &optional at-beginning
1690 ;;; blank-container)
1691 (defun allout-parse-item-at-point (item-widget &optional at-beginning
1692 blank-container)
1693 "Set widget ITEM-WIDGET layout parameters per item-at-point's actual layout.
1695 If optional AT-BEGINNING is t, then point is assumed to be at the start of
1696 the item prefix.
1698 If optional BLANK-CONTAINER is true, then the parameters of a container
1699 which has an empty body are set. \(Though the body is blank, the object
1700 may have subitems.\)"
1702 ;; Uncomment this sit-for to notice where decoration is happening:
1703 ;; (sit-for .1)
1704 (let* ((depth (allout-depth))
1705 (depth (if blank-container 0 depth))
1706 (is-container (or blank-container (zerop depth)))
1708 (does-encrypt (and (not is-container)
1709 (allout-encrypted-type-prefix)))
1710 (is-encrypted (and does-encrypt (allout-encrypted-topic-p)))
1711 (icon-end allout-recent-prefix-end)
1712 (icon-start (1- icon-end))
1713 body-start
1714 body-end
1715 bullet
1716 has-subitems
1717 (contents-depth (1+ depth))
1719 (widget-put item-widget :depth depth)
1720 (if is-container
1722 (progn
1723 (widget-put item-widget :from (allout-set-boundary-marker
1724 :from (point-min)
1725 (widget-get item-widget :from)))
1726 (widget-put item-widget :icon-end nil)
1727 (widget-put item-widget :icon-start nil)
1728 (setq body-start (widget-put item-widget :body-start 1)))
1730 ;; not container:
1732 (widget-put item-widget :from (allout-set-boundary-marker
1733 :from (if at-beginning
1734 (point)
1735 allout-recent-prefix-beginning)
1736 (widget-get item-widget :from)))
1737 (widget-put item-widget :icon-start icon-start)
1738 (widget-put item-widget :icon-end icon-end)
1739 (when does-encrypt
1740 (widget-put item-widget :does-encrypt t)
1741 (widget-put item-widget :is-encrypted is-encrypted))
1743 ;; cue area:
1744 (setq body-start icon-end)
1745 (widget-put item-widget :bullet (setq bullet (allout-get-bullet)))
1746 (if (equal (char-after body-start) ? )
1747 (setq body-start (1+ body-start)))
1748 (widget-put item-widget :body-start body-start)
1751 ;; Both container and regular items:
1753 ;; :body-end (doesn't include a trailing blank line, if any) -
1754 (widget-put item-widget :body-end (setq body-end
1755 (if blank-container
1757 (allout-end-of-entry))))
1759 (widget-put item-widget :to (allout-set-boundary-marker
1760 :to (if blank-container
1761 (point-min)
1762 (or (allout-pre-next-prefix)
1763 (goto-char (point-max))))
1764 (widget-get item-widget :to)))
1765 (widget-put item-widget :has-subitems
1766 (setq has-subitems
1767 (and
1768 ;; has a subsequent item:
1769 (not (= body-end (point-max)))
1770 ;; subsequent item is deeper:
1771 (< depth (setq contents-depth (allout-recent-depth))))))
1772 ;; note :expanded - true if widget item's content is currently visible?
1773 (widget-put item-widget :expanded
1774 (and has-subitems
1775 ;; subsequent item is or isn't visible:
1776 (save-excursion
1777 (goto-char allout-recent-prefix-beginning)
1778 (not (allout-hidden-p)))))))
1779 ;;;_ > allout-set-boundary-marker (boundary position &optional current-marker)
1780 (defun allout-set-boundary-marker (boundary position &optional current-marker)
1781 "Set or create item widget BOUNDARY type marker at POSITION.
1783 Optional CURRENT-MARKER is the marker currently being used for
1784 the boundary, if any.
1786 BOUNDARY type is either :from or :to, determining the marker insertion type."
1787 (if (not position) (setq position (point)))
1788 (if current-marker
1789 (set-marker current-marker position)
1790 (let ((marker (make-marker)))
1791 ;; XXX dang - would like for :from boundary to advance after inserted
1792 ;; text, but that would omit new header prefixes when allout
1793 ;; relevels, etc. this competes with ad-hoc edits, which would
1794 ;; better be omitted
1795 (set-marker-insertion-type marker nil)
1796 (set-marker marker position))))
1797 ;;;_ > allout-decorate-item-span (item-widget)
1798 (defun allout-decorate-item-span (item-widget)
1799 "Equip the item with a span, as an entirety.
1801 This span is implemented so it can be used to detect displacement
1802 of the widget in absolute terms, and provides an offset bias for
1803 the various element spans."
1805 (if (and (widget-get item-widget :is-container)
1806 ;; the only case where the span could be empty.
1807 (eq (widget-get item-widget :from)
1808 (widget-get item-widget :to)))
1810 (allout-item-span item-widget
1811 (widget-get item-widget :from)
1812 (widget-get item-widget :to))))
1813 ;;;_ > allout-decorate-item-guides (item-widget
1814 ;;; &optional parent-widget has-successor)
1815 (defun allout-decorate-item-guides (item-widget
1816 &optional parent-widget has-successor)
1817 "Add ITEM-WIDGET guide icon-prefix descender and connector text properties.
1819 Optional arguments provide context for deriving the guides. In
1820 their absence, the current guide column flags are used.
1822 Optional PARENT-WIDGET is the widget for the item's parent item.
1824 Optional HAS-SUCCESSOR is true iff the item is followed by a sibling.
1826 We also hide the header-prefix string.
1828 Guides are established according to the item-widget's :guide-column-flags,
1829 when different than :was-guide-column-flags. Changing that property and
1830 reapplying this method will rectify the glyphs."
1832 (when (not (widget-get item-widget :is-container))
1833 (let* ((depth (widget-get item-widget :depth))
1834 (parent-depth (and parent-widget
1835 (widget-get parent-widget :depth)))
1836 (parent-flags (and parent-widget
1837 (widget-get parent-widget :guide-column-flags)))
1838 (parent-flags-depth (length parent-flags))
1839 (extender-length (- depth (+ parent-flags-depth 2)))
1840 (flags (or (and (> depth 1)
1841 parent-widget
1842 (widget-put item-widget :guide-column-flags
1843 (append (list has-successor)
1844 (if (< 0 extender-length)
1845 (make-list extender-length
1846 '-))
1847 parent-flags)))
1848 (widget-get item-widget :guide-column-flags)))
1849 (was-flags (widget-get item-widget :was-guide-column-flags))
1850 (guides-start (widget-get item-widget :from))
1851 (guides-end (widget-get item-widget :icon-start))
1852 (position guides-start)
1853 (increment (length allout-header-prefix))
1854 reverse-flags
1855 guide-name
1856 extenders paint-extenders
1857 (inhibit-read-only t))
1859 (when (not (equal was-flags flags))
1861 (setq reverse-flags (reverse flags))
1862 (while reverse-flags
1863 (setq guide-name
1864 (cond ((null (cdr reverse-flags))
1865 (if (car reverse-flags)
1866 'mid-connector
1867 'end-connector))
1868 ((eq (car reverse-flags) '-)
1869 ;; accumulate extenders tally, to be painted on next
1870 ;; non-extender flag, according to the flag type.
1871 (setq extenders (1+ (or extenders 0)))
1872 nil)
1873 ((car reverse-flags)
1874 'through-descender)
1875 (t 'skip-descender)))
1876 (when guide-name
1877 (put-text-property position (setq position (+ position increment))
1878 'display (allout-fetch-icon-image guide-name))
1879 (if (> increment 1) (setq increment 1))
1880 (when extenders
1881 ;; paint extenders after a connector, else leave spaces.
1882 (dotimes (i extenders)
1883 (put-text-property
1884 position (setq position (1+ position))
1885 'display (allout-fetch-icon-image
1886 (if (memq guide-name '(mid-connector end-connector))
1887 'extender-connector
1888 'skip-descender))))
1889 (setq extenders nil)))
1890 (setq reverse-flags (cdr reverse-flags)))
1891 (widget-put item-widget :was-guide-column-flags flags))
1893 (allout-item-element-span-is item-widget :guides-span
1894 guides-start guides-end))))
1895 ;;;_ > allout-decorate-item-icon (item-widget)
1896 (defun allout-decorate-item-icon (item-widget)
1897 "Add item icon glyph and distinctive bullet text properties to ITEM-WIDGET."
1899 (when (not (widget-get item-widget :is-container))
1900 (let* ((icon-start (widget-get item-widget :icon-start))
1901 (icon-end (widget-get item-widget :icon-end))
1902 (bullet (widget-get item-widget :bullet))
1903 (use-bullet bullet)
1904 (was-bullet (widget-get item-widget :was-bullet))
1905 (distinctive (allout-distinctive-bullet bullet))
1906 (distinctive-start (widget-get item-widget :distinctive-start))
1907 (distinctive-end (widget-get item-widget :distinctive-end))
1908 (does-encrypt (widget-get item-widget :does-encrypt))
1909 (is-encrypted (and does-encrypt (widget-get item-widget
1910 :is-encrypted)))
1911 (expanded (widget-get item-widget :expanded))
1912 (has-subitems (widget-get item-widget :has-subitems))
1913 (inhibit-read-only t)
1914 icon-state)
1916 (when (not (and (equal (widget-get item-widget :was-expanded) expanded)
1917 (equal (widget-get item-widget :was-has-subitems)
1918 has-subitems)
1919 (equal (widget-get item-widget :was-does-encrypt)
1920 does-encrypt)
1921 (equal (widget-get item-widget :was-is-encrypted)
1922 is-encrypted)))
1924 (setq icon-state
1925 (cond (does-encrypt (if is-encrypted
1926 'locked-encrypted
1927 'unlocked-encrypted))
1928 (expanded 'opened)
1929 (has-subitems 'closed)
1930 (t 'empty)))
1931 (put-text-property icon-start (1+ icon-start)
1932 'display (allout-fetch-icon-image icon-state))
1933 (widget-put item-widget :was-expanded expanded)
1934 (widget-put item-widget :was-has-subitems has-subitems)
1935 (widget-put item-widget :was-does-encrypt does-encrypt)
1936 (widget-put item-widget :was-is-encrypted is-encrypted)
1937 ;; preserve as a widget property to track last known:
1938 (widget-put item-widget :icon-state icon-state)
1939 ;; preserve as a text property to track undo:
1940 (put-text-property icon-start icon-end :icon-state icon-state))
1941 (allout-item-element-span-is item-widget :icon-span
1942 icon-start icon-end)
1943 (when (not (string= was-bullet bullet))
1944 (cond ((not distinctive)
1945 ;; XXX we strip the prior properties without even checking if
1946 ;; the prior bullet was distinctive, because the widget
1947 ;; provisions to convey that info is disappearing, sigh.
1948 (remove-text-properties icon-end (1+ icon-end) '(display))
1949 (setq distinctive-start icon-end distinctive-end icon-end)
1950 (widget-put item-widget :distinctive-start distinctive-start)
1951 (widget-put item-widget :distinctive-end distinctive-end))
1953 ((not (string= bullet allout-numbered-bullet))
1954 (setq distinctive-start icon-end distinctive-end (+ icon-end 1)))
1956 (does-encrypt
1957 (setq distinctive-start icon-end distinctive-end (+ icon-end 1)))
1960 (goto-char icon-end)
1961 (looking-at "[0-9]+")
1962 (setq use-bullet (buffer-substring icon-end (match-end 0)))
1963 (setq distinctive-start icon-end
1964 distinctive-end (match-end 0))))
1965 (put-text-property distinctive-start distinctive-end 'display
1966 use-bullet)
1967 (widget-put item-widget :was-bullet bullet)
1968 (widget-put item-widget :distinctive-start distinctive-start)
1969 (widget-put item-widget :distinctive-end distinctive-end)))))
1970 ;;;_ > allout-decorate-item-cue (item-widget)
1971 (defun allout-decorate-item-cue (item-widget)
1972 "Incorporate space between bullet icon and body to the ITEM-WIDGET."
1973 ;; NOTE: most of the cue-area
1975 (when (not (widget-get item-widget :is-container))
1976 (let* ((cue-start (or (widget-get item-widget :distinctive-end)
1977 (widget-get item-widget :icon-end)))
1978 (body-start (widget-get item-widget :body-start))
1979 (expanded (widget-get item-widget :expanded))
1980 (has-subitems (widget-get item-widget :has-subitems))
1981 (inhibit-read-only t))
1983 (allout-item-element-span-is item-widget :cue-span cue-start body-start)
1984 (put-text-property (1- body-start) body-start 'rear-nonsticky t))))
1985 ;;;_ > allout-decorate-item-body (item-widget &optional force)
1986 (defun allout-decorate-item-body (item-widget &optional force)
1987 "Incorporate item body text as part the ITEM-WIDGET.
1989 Optional FORCE means force reassignment of the region property."
1991 (let* ((allout-inhibit-body-modification-hook t)
1992 (body-start (widget-get item-widget :body-start))
1993 (body-end (widget-get item-widget :body-end))
1994 (body-text-end body-end)
1995 (inhibit-read-only t))
1997 (allout-item-element-span-is item-widget :body-span
1998 body-start (min (1+ body-end) (point-max))
1999 force)))
2000 ;;;_ > allout-item-actual-position (item-widget field)
2001 (defun allout-item-actual-position (item-widget field)
2002 "Return ITEM-WIDGET FIELD position taking item displacement into account."
2004 ;; The item's sub-element positions (:icon-end, :body-start, etc) are
2005 ;; accurate when the item is parsed, but some offsets from the start
2006 ;; drift with text added in the body.
2008 ;; Rather than reparse an item with every change (inefficient), or derive
2009 ;; every position from a distinct field marker/overlay (prohibitive as
2010 ;; the number of items grows), we use the displacement tracking of the
2011 ;; :span-overlay's markers, against the registered :from or :body-end
2012 ;; (depending on whether the requested field value is before or after the
2013 ;; item body), to bias the registered values.
2015 ;; This is not necessary/useful when the item is being decorated, because
2016 ;; that always must be preceded by a fresh item parse.
2018 (if (not (eq field :body-end))
2019 (widget-get item-widget :from)
2021 (let* ((span-overlay (widget-get item-widget :span-overlay))
2022 (body-end-position (widget-get item-widget :body-end))
2023 (ref-marker-position (and span-overlay
2024 (overlay-end span-overlay)))
2025 (offset (and body-end-position span-overlay
2026 (- (or ref-marker-position 0)
2027 body-end-position))))
2028 (+ (widget-get item-widget field) (or offset 0)))))
2029 ;;;_ : Item undecoration
2030 ;;;_ > allout-widgets-undecorate-region (start end)
2031 (defun allout-widgets-undecorate-region (start end)
2032 "Eliminate widgets and decorations for all items in region from START to END."
2033 (let ((next start)
2034 widget)
2035 (save-excursion
2036 (goto-char start)
2037 (while (< (setq next (next-single-char-property-change next
2038 'display
2039 (current-buffer)
2040 end))
2041 end)
2042 (goto-char next)
2043 (when (setq widget (allout-get-item-widget))
2044 ;; if the next-property/overly progression got us to a widget:
2045 (allout-widgets-undecorate-item widget t))))))
2046 ;;;_ > allout-widgets-undecorate-text (text)
2047 (defun allout-widgets-undecorate-text (text)
2048 "Eliminate widgets and decorations for all items in TEXT."
2049 (remove-text-properties 0 (length text)
2050 '(display nil :icon-state nil rear-nonsticky nil
2051 category nil button nil field nil)
2052 text)
2053 text)
2054 ;;;_ > allout-widgets-undecorate-item (item-widget &optional no-expose)
2055 (defun allout-widgets-undecorate-item (item-widget &optional no-expose)
2056 "Remove widget decorations from ITEM-WIDGET.
2058 Any concealed content head lines and item body is exposed, unless
2059 optional NO-EXPOSE is non-nil."
2060 (let ((from (widget-get item-widget :from))
2061 (to (widget-get item-widget :to))
2062 (text-properties-to-remove '(display nil
2063 :icon-state nil
2064 rear-nonsticky nil
2065 category nil
2066 button nil
2067 field nil))
2068 (span-overlay (widget-get item-widget :span-overlay))
2069 (button-overlay (widget-get item-widget :button))
2070 (was-modified (buffer-modified-p))
2071 (buffer-undo-list t)
2072 (inhibit-read-only t))
2073 (if (not no-expose)
2074 (allout-flag-region from to nil))
2075 (allout-unprotected
2076 (remove-text-properties from to text-properties-to-remove))
2077 (when span-overlay
2078 (delete-overlay span-overlay) (widget-put item-widget :span-overlay nil))
2079 (when button-overlay
2080 (delete-overlay button-overlay) (widget-put item-widget :button nil))
2081 (set-marker from nil)
2082 (set-marker to nil)
2083 (if (not was-modified)
2084 (set-buffer-modified-p nil))))
2086 ;;;_ : Item decoration support
2087 ;;;_ > allout-item-span (item-widget &optional start end)
2088 (defun allout-item-span (item-widget &optional start end)
2089 "Return or register the location of an ITEM-WIDGET's actual START and END.
2091 If START and END are not passed in, return either a dotted pair
2092 of the current span, if established, or nil if not yet set.
2094 When the START and END are passed, return the distance that the
2095 start of the item moved. We return 0 if the span was not
2096 previously established or is not moved."
2097 (let ((overlay (widget-get item-widget :span-overlay))
2098 was-start was-end
2099 changed)
2100 (cond ((not overlay) (when start
2101 (setq overlay (make-overlay start end nil t nil))
2102 (overlay-put overlay 'button item-widget)
2103 (widget-put item-widget :span-overlay overlay)
2105 ;; report:
2106 ((not start) (cons (overlay-start overlay) (overlay-end overlay)))
2107 ;; move:
2108 ((or (not (equal (overlay-start overlay) start))
2109 (not (equal (overlay-end overlay) end)))
2110 (move-overlay overlay start end)
2112 ;; specified span already set:
2113 (t nil))))
2114 ;;;_ > allout-item-element-span-is (item-widget element
2115 ;;; &optional start end force)
2116 (defun allout-item-element-span-is (item-widget element
2117 &optional start end force)
2118 "Return or register the location of the indicated ITEM-WIDGET ELEMENT.
2120 ELEMENT is one of :guides-span, :icon-span, :cue-span, or :body-span.
2122 When optional START is specified, optional END must also be.
2124 START and END are the actual bounds of the region, if provided.
2126 If START and END are not passed in, we return either a dotted
2127 pair of the current span, if established, or nil if not yet set.
2129 When the START and END are passed, we return t if the region
2130 changed or nil if not.
2132 Optional FORCE means force assignment of the region's text
2133 property, even if it's already set."
2134 (let ((span (widget-get item-widget element)))
2135 (cond ((or (not span) force)
2136 (when start
2137 (widget-put item-widget element (cons start end))
2138 (put-text-property start end 'category
2139 (cdr (assoc element
2140 allout-span-to-category)))
2142 ;; report:
2143 ((not start) span)
2144 ;; move if necessary:
2145 ((not (and (eq (car span) start)
2146 (eq (cdr span) end)))
2147 (widget-put item-widget element span)
2149 ;; specified span already set:
2150 (t nil))))
2151 ;;;_ : Item widget retrieval (/ high-level creation):
2152 ;;;_ > allout-get-item-widget (&optional container)
2153 (defun allout-get-item-widget (&optional container)
2154 "Return the widget for the item at point, or nil if no widget yet exists.
2156 Point must be situated *before* the start of the target item's
2157 body, so we don't get an existing containing item when we're in
2158 the process of creating an item in the middle of another.
2160 Optional CONTAINER is used to obtain the container item."
2161 (if (or container (zerop (allout-depth)))
2162 allout-container-item-widget
2163 ;; allout-recent-* are calibrated by (allout-depth) if we got here.
2164 (let ((got (widget-at allout-recent-prefix-beginning)))
2165 (if (and got (listp got))
2166 (if (marker-position (widget-get got :from))
2167 (and
2168 (>= (point) (widget-apply got :actual-position :from))
2169 (<= (point) (widget-apply got :actual-position :body-start))
2170 got)
2171 ;; a wacky residual item - undecorate and disregard:
2172 (allout-widgets-undecorate-item got)
2173 nil)))))
2174 ;;;_ > allout-get-or-create-item-widget (&optional redecorate blank-container)
2175 (defun allout-get-or-create-item-widget (&optional redecorate blank-container)
2176 "Return a widget for the item at point, creating the widget if necessary.
2178 When creating a widget, we assume there has been a context change
2179 and decorate its siblings and parent, as well.
2181 Optional BLANK-CONTAINER is for internal use, to fabricate a
2182 meta-container item with an empty body when the first proper
2183 \(non-container\) item starts at the beginning of the file.
2185 Optional REDECORATE, if non-nil, means to redecorate the widget
2186 if it already exists."
2187 (let ((widget (allout-get-item-widget blank-container))
2188 (buffer-undo-list t))
2189 (cond (widget (if redecorate
2190 (allout-redecorate-item widget))
2191 widget)
2192 ((or blank-container (zerop (allout-depth)))
2193 (or allout-container-item-widget
2194 (setq allout-container-item-widget
2195 (allout-decorate-item-and-context
2196 (widget-convert 'allout-item-widget)
2197 nil blank-container))))
2198 ;; create a widget for a regular/non-container item:
2199 (t (allout-decorate-item-and-context (widget-convert
2200 'allout-item-widget))))))
2201 ;;;_ > allout-get-or-create-parent-widget (&optional redecorate)
2202 (defun allout-get-or-create-parent-widget (&optional redecorate)
2203 "Return widget for parent of item at point, decorating it if necessary.
2205 We return the container widget if we're above the first proper item in the
2206 file.
2208 Optional REDECORATE, if non-nil, means to redecorate the widget if it
2209 already exists.
2211 Point will wind up positioned on the beginning of the parent or beginning
2212 of the buffer."
2213 ;; use existing widget, if there, else establish it
2214 (if (or (bobp) (and (not (allout-ascend))
2215 (looking-at allout-regexp)))
2216 (allout-get-or-create-item-widget redecorate 'blank-container)
2217 (allout-get-or-create-item-widget redecorate)))
2218 ;;;_ : X- Item ancillaries
2219 ;;;_ >X allout-body-modification-handler (beg end)
2220 (defun allout-body-modification-handler (beg end)
2221 "Do routine processing of body text before and after modification.
2223 Operation is inhibited by `allout-inhibit-body-modification-handler'."
2225 ;; The primary duties are:
2227 ;; - marking of escaped prefix-like text for delayed cleanup of escapes
2228 ;; - removal and replacement of the settings
2229 ;; - maintenance of beginning-of-line guide lines
2231 ;; ?? Escapes removal \(before changes\) is not done when edits span multiple
2232 ;; items, recognizing that item structure is being preserved, including
2233 ;; escaping of item-prefix-like text within bodies. See
2234 ;; `allout-before-modification-handler' and
2235 ;; `allout-inhibit-body-modification-handler'.
2237 ;; Adds the overlay to the `allout-unresolved-body-mod-workhash' during
2238 ;; before-change operation, and removes from that list during after-change
2239 ;; operation.
2240 (cond (allout-inhibit-body-modification-hook nil)))
2241 ;;;_ >X allout-graphics-modification-handler (beg end)
2242 (defun allout-graphics-modification-handler (beg end)
2243 "Protect against incoherent deletion of decoration graphics.
2245 Deletes allowed only when inhibit-read-only is t."
2246 (cond
2247 (undo-in-progress (when (eq (get-text-property beg 'category)
2248 'allout-icon-span-category)
2249 (save-excursion
2250 (goto-char beg)
2251 (let* ((item-widget (allout-get-item-widget)))
2252 (if item-widget
2253 (allout-widgets-exposure-undo-recorder
2254 item-widget))))))
2255 (inhibit-read-only t)
2256 ((not (and (boundp 'allout-mode) allout-mode)) t)
2257 ((equal this-command 'quoted-insert) t)
2258 ((yes-or-no-p "Unruly edit of outline structure - allow? ")
2259 (setq allout-widgets-unset-inhibit-read-only (not inhibit-read-only)
2260 inhibit-read-only t))
2261 (t (error
2262 (substitute-command-keys allout-structure-unruly-deletion-message)))))
2263 ;;;_ > allout-item-icon-key-handler ()
2264 (defun allout-item-icon-key-handler ()
2265 "Catchall handling of key bindings in item icon/cue hot-spots.
2267 Applies `allout-hotspot-key-handler' and calls the result, if any, as an
2268 interactive command."
2270 (interactive)
2271 (let* ((mapped-binding (allout-hotspot-key-handler)))
2272 (when mapped-binding
2273 (call-interactively mapped-binding))))
2275 ;;;_ : Status
2276 ;;;_ . allout-item-location (item-widget)
2277 (defun allout-item-location (item-widget)
2278 "Location of the start of the item's text."
2279 (overlay-start (widget-get item-widget :span-overlay)))
2281 ;;;_ : Icon management
2282 ;;;_ > allout-fetch-icon-image (name)
2283 (defun allout-fetch-icon-image (name)
2284 "Fetch allout icon for symbol NAME.
2286 We use a caching strategy, so the caller doesn't need to do so."
2287 (let* ((types allout-widgets-icon-types)
2288 (use-dir (if (equal (allout-frame-property nil 'background-mode)
2289 'light)
2290 allout-widgets-icons-light-subdir
2291 allout-widgets-icons-dark-subdir))
2292 (key (list name use-dir))
2293 (got (assoc key allout-widgets-icons-cache)))
2294 (if got
2295 ;; display system shows only first of subsequent adjacent
2296 ;; `eq'-identical repeats - use copies to avoid this problem.
2297 (allout-widgets-copy-list (cadr got))
2298 (while (and types (not got))
2299 (setq got
2300 (allout-find-image
2301 (list (append (list :type (car types)
2302 :file (concat use-dir
2303 (symbol-name name)
2304 "." (symbol-name
2305 (car types))))
2306 (if (featurep 'xemacs)
2307 allout-widgets-item-image-properties-xemacs
2308 allout-widgets-item-image-properties-emacs)
2309 ))))
2310 (setq types (cdr types)))
2311 (if got
2312 (push (list key got) allout-widgets-icons-cache))
2313 got)))
2315 ;;;_ : Miscellaneous
2316 ;;;_ > allout-elapsed-time-seconds (triple)
2317 (defun allout-elapsed-time-seconds (end start)
2318 "Return seconds between `current-time' style time START/END triples."
2319 (let ((elapsed (time-subtract end start)))
2320 (+ (* (car elapsed) (expt 2.0 16))
2321 (cadr elapsed)
2322 (/ (caddr elapsed) (expt 10.0 6)))))
2323 ;;;_ > allout-frame-property (frame property)
2324 (defalias 'allout-frame-property
2325 (cond ((fboundp 'frame-parameter)
2326 'frame-parameter)
2327 ((fboundp 'frame-property)
2328 'frame-property)
2329 (t nil)))
2330 ;;;_ > allout-find-image (specs)
2331 (defalias 'allout-find-image
2332 (if (fboundp 'find-image)
2333 'find-image
2334 nil) ; aka, not-yet-implemented for xemacs.
2336 ;;;_ > allout-widgets-copy-list (list)
2337 (defun allout-widgets-copy-list (list)
2338 ;; duplicated from cl.el 'copy-list' as of 2008-08-17
2339 "Return a copy of LIST, which may be a dotted list.
2340 The elements of LIST are not copied, just the list structure itself."
2341 (if (consp list)
2342 (let ((res nil))
2343 (while (consp list) (push (pop list) res))
2344 (prog1 (nreverse res) (setcdr res list)))
2345 (car list)))
2347 ;;;_ : Run unit tests:
2348 (defun allout-widgets-run-unit-tests ()
2349 (message "Running allout-widget tests...")
2351 (allout-test-range-overlaps)
2353 (message "Running allout-widget tests... Done.")
2354 (sit-for .5))
2356 (when allout-widgets-run-unit-tests-on-load
2357 (allout-widgets-run-unit-tests))
2359 ;;;_ : provide
2360 (provide 'allout-widgets)
2362 ;;;_. Local emacs vars.
2363 ;;;_ , Local variables:
2364 ;;;_ , allout-layout: (-1 : 0)
2365 ;;;_ , End: