* calc/calc-misc.el (calc-help): Avoid wrapping help message.
[emacs.git] / lisp / allout-widgets.el
blob715879534b023a96d09180a7ba17591bb054a082
1 ;; allout-widgets.el --- Visually highlight allout outline structure.
3 ;; Copyright (C) 2005-2011 Free Software Foundation, Inc.
5 ;; Author: Ken Manheimer <ken dot manheimer at gmail...>
6 ;; Maintainer: Ken Manheimer <ken dot manheimer at gmail...>
7 ;; Version: 1.0
8 ;; Created: Dec 2005
9 ;; Keywords: outlines
10 ;; Website: http://myriadicity.net/software-and-systems/craft/emacs-allout
12 ;;; Commentary:
14 ;; This is an allout outline-mode add-on that highlights outline structure
15 ;; with graphical widgets.
17 ;; To activate, customize `allout-widgets-auto-activation'. You can also
18 ;; invoke allout-widgets-mode in a particular allout buffer. When
19 ;; auto-enabled, you can inhibit widget operation in particular allout
20 ;; buffers by setting the variable `allout-widgets-mode-inhibit' non-nil in
21 ;; that file's buffer. Use emacs *file local variables* to generally
22 ;; inhibit for a file.
24 ;; See the `allout-widgets-mode' docstring for more details.
26 ;; Info about allout and allout-widgets development are available at
27 ;; http://myriadicity.net/Sundry/EmacsAllout
29 ;; The graphics include:
31 ;; - icons for item bullets, varying to distinguish whether the item either
32 ;; lacks any subitems, the subitems are currently collapsed within the
33 ;; item, or the item is currently expanded.
35 ;; - guide lines connecting item bullet-icons with those of their subitems.
37 ;; - cue area between the bullet-icon and the start of the body headline,
38 ;; for item numbering, encryption indicator, and distinctive bullets.
40 ;; The bullet-icon and guide line graphics provide keybindings and mouse
41 ;; bindings for easy outline navigation and exposure control, extending
42 ;; outline hot-spot navigation (see `allout-mode' docstring for details).
44 ;; Developers note: Our use of emacs widgets is unconventional. We
45 ;; decorate existing text rather than substituting for it, to
46 ;; piggy-back on existing allout operation. This employs the C-coded
47 ;; efficiencies of widget-apply, widget-get, and widget-put, along
48 ;; with the basic object-oriented organization of widget-create, to
49 ;; systematically couple overlays, graphics, and other features with
50 ;; allout-governed text.
52 ;;;_: Code (structured with comments that delineate an allout outline)
54 ;;;_ : General Environment
55 (require 'allout)
56 (require 'widget)
57 (require 'wid-edit)
59 (eval-when-compile
60 (progn
61 (require 'overlay)
62 (require 'cl)
65 ;;;_ : internal variables needed before user-customization variables
66 ;;; In order to enable activation of allout-widgets-mode via customization,
67 ;;; allout-widgets-auto-activation uses a setting function. That function
68 ;;; is invoked when the customization variable definition is evaluated,
69 ;;; during file load, so the involved code must reside above that
70 ;;; definition in the file.
71 ;;;_ = allout-widgets-mode
72 (defvar allout-widgets-mode nil
73 "Allout mode enhanced with graphical widgets.")
74 (make-variable-buffer-local 'allout-widgets-mode)
76 ;;;_ : USER CUSTOMIZATION VARIABLES and incidental functions:
77 ;;;_ > defgroup allout-widgets
78 ;;;###autoload
79 (defgroup allout-widgets nil
80 "Allout extension that highlights outline structure graphically.
82 Customize `allout-widgets-auto-activation' to activate allout-widgets
83 with allout-mode."
84 :group 'allout)
85 ;;;_ > defgroup allout-widgets-developer
86 (defgroup allout-widgets-developer nil
87 "Settings for development of allout widgets extension."
88 :group 'allout-widgets)
89 ;;;_ ; some functions a bit early, for allout-auto-activation dependency:
90 ;;;_ > allout-widgets-mode-enable
91 (defun allout-widgets-mode-enable ()
92 "Enable allout-widgets-mode in allout-mode buffers.
94 See `allout-widgets-mode-inhibit' for per-file/per-buffer
95 inhibition of allout-widgets-mode."
96 (add-hook 'allout-mode-off-hook 'allout-widgets-mode-off)
97 (add-hook 'allout-mode-on-hook 'allout-widgets-mode-on)
99 ;;;_ > allout-widgets-mode-disable
100 (defun allout-widgets-mode-disable ()
101 "Disable allout-widgets-mode in allout-mode buffers.
103 See `allout-widgets-mode-inhibit' for per-file/per-buffer
104 inhibition of allout-widgets-mode."
105 (remove-hook 'allout-mode-off-hook 'allout-widgets-mode-off)
106 (remove-hook 'allout-mode-on-hook 'allout-widgets-mode-on)
108 ;;;_ > allout-widgets-setup (varname value)
109 ;;;###autoload
110 (defun allout-widgets-setup (varname value)
111 "Commission or decommission allout-widgets-mode along with allout-mode.
113 Meant to be used by customization of `allout-widgets-auto-activation'."
114 (set-default varname value)
115 (if allout-widgets-auto-activation
116 (allout-widgets-mode-enable)
117 (allout-widgets-mode-disable)))
118 ;;;_ = allout-widgets-auto-activation
119 ;;;###autoload
120 (defcustom allout-widgets-auto-activation nil
121 "Activate to enable allout icon graphics wherever allout mode is active.
123 Also enable `allout-auto-activation' for this to take effect upon
124 visiting an outline.
126 When this is set you can disable allout widgets in select files
127 by setting `allout-widgets-mode-inhibit'
129 Instead of setting `allout-widgets-auto-activation' you can
130 explicitly invoke `allout-widgets-mode' in allout buffers where
131 you want allout widgets operation.
133 See `allout-widgets-mode' for allout widgets mode features."
134 :type 'boolean
135 :group 'allout-widgets
136 :set 'allout-widgets-setup
138 ;; ;;;_ = allout-widgets-allow-unruly-edits
139 ;; (defcustom allout-widgets-allow-unruly-edits nil
140 ;; "*Control whether manual edits are restricted to maintain outline integrity.
142 ;; When nil, manual edits must either be within an item's body or encompass
143 ;; one or more items completely - eg, killing topics as entities, rather than
144 ;; deleting from the middle of one to the middle of another.
146 ;; If you only occasionally need to make unrestricted change, you can set this
147 ;; variable in the specific buffer using set-variable, or just deactivate
148 ;; `allout-mode' temporarily. You can customize this to always allow unruly
149 ;; edits, but you will be able to create outlines that are unnavigable in
150 ;; principle, and not just for allout's navigation and exposure mechanisms."
151 ;; :type 'boolean
152 ;; :group allout-widgets)
153 ;; (make-variable-buffer-local 'allout-widgets-allow-unruly-edits)
154 ;;;_ = allout-widgets-auto-activation - below, for eval-order dependencies
155 ;;;_ = allout-widgets-icons-dark-subdir
156 (defcustom allout-widgets-icons-dark-subdir "icons/allout-widgets/dark-bg/"
157 "Directory on `image-load-path' holding allout icons for dark backgrounds."
158 :type 'string
159 :group 'allout-widgets)
160 ;;;_ = allout-widgets-icons-light-subdir
161 (defcustom allout-widgets-icons-light-subdir "icons/allout-widgets/light-bg/"
162 "Directory on `image-load-path' holding allout icons for light backgrounds."
163 :type 'string
164 :group 'allout-widgets)
165 ;;;_ = allout-widgets-icon-types
166 (defcustom allout-widgets-icon-types '(xpm png)
167 "File extensions for the icon graphic format types, in order of preference."
168 :type '(repeat symbol)
169 :group 'allout-widgets)
171 ;;;_ . Decoration format
172 ;;;_ = allout-widgets-theme-dark-background
173 (defcustom allout-widgets-theme-dark-background "allout-dark-bg"
174 "Identify the outline's icon theme to use with a dark background."
175 :type '(string)
176 :group 'allout-widgets)
177 ;;;_ = allout-widgets-theme-light-background
178 (defcustom allout-widgets-theme-light-background "allout-light-bg"
179 "Identify the outline's icon theme to use with a light background."
180 :type '(string)
181 :group 'allout-widgets)
182 ;;;_ = allout-widgets-item-image-properties-emacs
183 (defcustom allout-widgets-item-image-properties-emacs
184 '(:ascent center :mask (heuristic t))
185 "*Default properties item widget images in mainline Emacs."
186 :type 'plist
187 :group 'allout-widgets)
188 ;;;_ = allout-widgets-item-image-properties-xemacs
189 (defcustom allout-widgets-item-image-properties-xemacs
191 "*Default properties item widget images in XEmacs."
192 :type 'plist
193 :group 'allout-widgets)
194 ;;;_ . Developer
195 ;;;_ = allout-widgets-run-unit-tests-on-load
196 (defcustom allout-widgets-run-unit-tests-on-load nil
197 "*When non-nil, unit tests will be run at end of loading allout-widgets.
199 Generally, allout widgets code developers are the only ones who'll want to
200 set this.
202 \(If set, this makes it an even better practice to exercise changes by
203 doing byte-compilation with a repeat count, so the file is loaded after
204 compilation.)
206 See `allout-widgets-run-unit-tests' to see what's run."
207 :type 'boolean
208 :group 'allout-widgets-developer)
209 ;;;_ = allout-widgets-time-decoration-activity
210 (defcustom allout-widgets-time-decoration-activity nil
211 "*Retain timing info of the last cooperative redecoration.
213 The details are retained as the value of
214 `allout-widgets-last-decoration-timing'.
216 Generally, allout widgets code developers are the only ones who'll want to
217 set this."
218 :type 'boolean
219 :group 'allout-widgets-developer)
220 ;;;_ = allout-widgets-hook-error-post-time 0
221 (defcustom allout-widgets-hook-error-post-time 0
222 "*Amount of time to sit showing hook error messages.
224 0 is minimal, or nil to not post to the message area.
226 This is for debugging purposes."
227 :type 'integer
228 :group 'allout-widgets-developer)
229 ;;;_ = allout-widgets-maintain-tally nil
230 (defcustom allout-widgets-maintain-tally nil
231 "*If non-nil, maintain a collection of widgets, `allout-widgets-tally'.
233 This is for debugging purposes.
235 The tally shows the total number of item widgets in the current
236 buffer, and tracking increases as new widgets are added and
237 decreases as obsolete widgets are garbage collected."
238 :type 'boolean
239 :group 'allout-widgets-developer)
240 (defvar allout-widgets-tally nil
241 "Hash-table of existing allout widgets, for debugging.
243 Table is maintained iff `allout-widgets-maintain-tally' is non-nil.
245 The table contents will be out of sync if any widgets are created
246 or deleted while this variable is nil.")
247 (make-variable-buffer-local 'allout-widgets-tally)
248 (defvar allout-widgets-mode-inhibit) ; defined below
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
261 (not allout-widgets-mode-inhibit)
262 allout-widgets-tally)
263 (format ":%s" (hash-table-count allout-widgets-tally))))
264 ;;;_ = allout-widgets-track-decoration nil
265 (defcustom allout-widgets-track-decoration nil
266 "*If non-nil, show cursor position of each item decoration.
268 This is for debugging purposes, and generally set at need in a
269 buffer rather than as a prevailing configuration \(but it's handy
270 to publicize it by making it a customization variable\)."
271 :type 'boolean
272 :group 'allout-widgets-developer)
273 (make-variable-buffer-local 'allout-widgets-track-decoration)
275 ;;;_ : Mode context - variables, hookup, and hooks
276 ;;;_ . internal mode variables
277 ;;;_ , Mode activation and environment
278 ;;;_ = allout-widgets-version
279 (defvar allout-widgets-version "1.0"
280 "Version of currently loaded allout-widgets extension.")
281 ;;;_ > allout-widgets-version
282 (defun allout-widgets-version (&optional here)
283 "Return string describing the loaded outline version."
284 (interactive "P")
285 (let ((msg (concat "Allout Outline Widgets Extension v "
286 allout-widgets-version)))
287 (if here (insert msg))
288 (message "%s" msg)
289 msg))
290 ;;;_ = allout-widgets-mode-inhibit
291 (defvar allout-widgets-mode-inhibit nil
292 "Inhibit `allout-widgets-mode' from activating widgets.
294 This also inhibits automatic adjustment of widgets to track allout outline
295 changes.
297 You can use this as a file local variable setting to disable
298 allout widgets enhancements in selected buffers while generally
299 enabling widgets by customizing `allout-widgets-auto-activation'.
301 In addition, you can invoked `allout-widgets-mode' allout-mode
302 buffers where this is set to enable and disable widget
303 enhancements, directly.")
304 ;;;###autoload
305 (put 'allout-widgets-mode-inhibit 'safe-local-variable
306 (if (fboundp 'booleanp) 'booleanp (lambda (x) (member x '(t nil)))))
307 (make-variable-buffer-local 'allout-widgets-mode-inhibit)
308 ;;;_ = allout-inhibit-body-modification-hook
309 (defvar allout-inhibit-body-modification-hook nil
310 "Override de-escaping of text-prefixes in item bodies during specific changes.
312 This is used by `allout-buffer-modification-handler' to signal such changes
313 to `allout-body-modification-handler', and is always reset by
314 `allout-post-command-business'.")
315 (make-variable-buffer-local 'allout-inhibit-body-modification-hook)
316 ;;;_ = allout-widgets-icons-cache
317 (defvar allout-widgets-icons-cache nil
318 "Cache allout icon images, as an association list.
320 `allout-fetch-icon-image' uses this cache transparently, keying
321 images with lists containing the name of the icon directory \(as
322 found on the `load-path') and the icon name.
324 Set this variable to `nil' to empty the cache, and have it replenish from the
325 filesystem.")
326 ;;;_ = allout-widgets-unset-inhibit-read-only
327 (defvar allout-widgets-unset-inhibit-read-only nil
328 "Tell `allout-widgets-post-command-business' to unset `inhibit-read-only'.
330 Used by `allout-graphics-modification-handler'")
331 ;;;_ = allout-widgets-reenable-before-change-handler
332 (defvar allout-widgets-reenable-before-change-handler nil
333 "Tell `allout-widgets-post-command-business' to reequip the handler.
335 Necessary because the handler sometimes deliberately raises an
336 error, causing it to be disabled.")
337 ;;;_ , State for hooks
338 ;;;_ = allout-unresolved-body-mod-workroster
339 (defvar allout-unresolved-body-mod-workroster (make-hash-table :size 16)
340 "List of body-overlays that did before-change business but not after-change.
342 See `allout-post-command-business' and `allout-body-modification-handler'.")
343 ;;;_ = allout-structure-unruly-deletion-message
344 (defvar allout-structure-unruly-deletion-message
345 "Unruly edit prevented --
346 To change the bullet character: \\[allout-rebullet-current-heading]
347 To promote this item: \\[allout-shift-out]
348 To demote it: \\[allout-shift-in]
349 To delete it and offspring: \\[allout-kill-topic]
350 See \\[describe-mode] for many more options."
351 "Informative message presented on improper editing of outline structure.
353 The structure includes the guides lines, bullet, and bullet cue.")
354 ;;;_ = allout-widgets-changes-record
355 (defvar allout-widgets-changes-record nil
356 "Record outline changes for processing by post-command hook.
358 Entries on the list are lists whose first element is a symbol indicating
359 the change type and subsequent elements are data specific to that change
360 type. Specifically:
362 'exposure `allout-exposure-from' `allout-exposure-to' `allout-exposure-flag'
364 The changes are recorded in reverse order, with new values pushed
365 onto the front.")
366 (make-variable-buffer-local 'allout-widgets-changes-record)
367 ;;;_ = allout-widgets-undo-exposure-record
368 (defvar allout-widgets-undo-exposure-record nil
369 "Record outline undo traces for processing by post-command hook.
371 The changes are recorded in reverse order, with new values pushed
372 onto the front.")
373 (make-variable-buffer-local 'allout-widgets-undo-exposure-record)
374 ;;;_ = allout-widgets-last-hook-error
375 (defvar allout-widgets-last-hook-error nil
376 "String holding last error string, for debugging purposes.")
377 ;;;_ = allout-widgets-adjust-message-length-threshold 100
378 (defvar allout-widgets-adjust-message-length-threshold 100
379 "Display \"Adjusting widgets\" message above this number of pending changes."
381 ;;;_ = allout-widgets-adjust-message-size-threshold 10000
382 (defvar allout-widgets-adjust-message-size-threshold 10000
383 "Display \"Adjusting widgets\" message above this size of pending changes."
385 ;;;_ = allout-doing-exposure-undo-processor nil
386 (defvar allout-undo-exposure-in-progress nil
387 "Maintained true during `allout-widgets-exposure-undo-processor'")
388 ;;;_ , Widget-specific outline text format
389 ;;;_ = allout-escaped-prefix-regexp
390 (defvar allout-escaped-prefix-regexp ""
391 "*Regular expression for body text that would look like an item prefix if
392 not altered with an escape sequence.")
393 (make-variable-buffer-local 'allout-escaped-prefix-regexp)
394 ;;;_ , Widget element formatting
395 ;;;_ = allout-item-icon-keymap
396 (defvar allout-item-icon-keymap
397 (let ((km (make-sparse-keymap)))
398 (dolist (digit '("0" "1" "2" "3"
399 "4" "5" "6" "7" "8" "9"))
400 (define-key km digit 'digit-argument))
401 (define-key km "-" 'negative-argument)
402 ;; (define-key km [(return)] 'allout-tree-expand-command)
403 ;; (define-key km [(meta return)] 'allout-toggle-torso-command)
404 ;; (define-key km [(down-mouse-1)] 'allout-item-button-click)
405 ;; (define-key km [(down-mouse-2)] 'allout-toggle-torso-event-command)
406 ;; Override underlying mouse-1 and mouse-2 bindings in icon territory:
407 (define-key km [(mouse-1)] (lambda () (interactive) nil))
408 (define-key km [(mouse-2)] (lambda () (interactive) nil))
410 ;; Catchall, handles actual keybindings, dynamically doing keymap lookups:
411 (define-key km [t] 'allout-item-icon-key-handler)
414 "General tree-node key bindings.")
415 ;;;_ = allout-item-body-keymap
416 (defvar allout-item-body-keymap
417 (let ((km (make-sparse-keymap))
418 (local-map (current-local-map)))
419 ;; (define-key km [(control return)] 'allout-tree-expand-command)
420 ;; (define-key km [(meta return)] 'allout-toggle-torso-command)
421 ;; XXX We need to reset this per buffer's mode; we do so in
422 ;; allout-widgets-mode.
423 (if local-map
424 (set-keymap-parent km local-map))
427 "General key bindings for the text content of outline items.")
428 (make-variable-buffer-local 'allout-item-body-keymap)
429 ;;;_ = allout-body-span-category
430 (defvar allout-body-span-category nil
431 "Symbol carrying allout body-text overlay properties.")
432 ;;;_ = allout-cue-span-keymap
433 (defvar allout-cue-span-keymap
434 (let ((km (make-sparse-keymap)))
435 (set-keymap-parent km allout-item-icon-keymap)
437 "Keymap used in the item cue area - the space between the icon and headline.")
438 ;;;_ = allout-escapes-category
439 (defvar allout-escapes-category nil
440 "Symbol for category of text property used to hide escapes of prefix-like
441 text in allout item bodies.")
442 ;;;_ = allout-guides-category
443 (defvar allout-guides-category nil
444 "Symbol carrying allout icon-guides overlay properties.")
445 ;;;_ = allout-guides-span-category
446 (defvar allout-guides-span-category nil
447 "Symbol carrying allout icon and guide lines overlay properties.")
448 ;;;_ = allout-icon-span-category
449 (defvar allout-icon-span-category nil
450 "Symbol carrying allout icon and guide lines overlay properties.")
451 ;;;_ = allout-cue-span-category
452 (defvar allout-cue-span-category nil
453 "Symbol carrying common properties of the space following the outline icon.
455 \(That space is used to convey selected cues indicating body qualities,
456 including things like:
457 - encryption '~'
458 - numbering '#'
459 - indirect reference '@'
460 - distinctive bullets - see `allout-distinctive-bullets-string'.\)")
461 ;;;_ = allout-span-to-category
462 (defvar allout-span-to-category
463 '((:guides-span . allout-guides-span-category)
464 (:cue-span . allout-cue-span-category)
465 (:icon-span . allout-icon-span-category)
466 (:body-span . allout-body-span-category))
467 "Association list mapping span identifier to category identifier.")
468 ;;;_ = allout-trailing-category
469 (defvar allout-trailing-category nil
470 "Symbol carrying common properties of an overlay's trailing newline.")
471 ;;;_ , Developer
472 (defvar allout-widgets-last-decoration-timing nil
473 "Timing details for the last cooperative decoration action.
475 This is maintained when `allout-widgets-time-decoration-activity' is set.
477 The value is a list containing two elements:
478 - the elapsed time as a number of seconds
479 - the list of changes processed, a la `allout-widgets-changes-record'.
481 When active, the value is revised each time automatic decoration activity
482 happens in the buffer.")
483 (make-variable-buffer-local 'allout-widgets-last-decoration-timing)
484 ;;;_ . mode hookup
485 ;;;_ > define-minor-mode allout-widgets-mode (arg)
486 ;;;###autoload
487 (define-minor-mode allout-widgets-mode
488 "Toggle Allout Widgets mode.
489 With a prefix argument ARG, enable Allout Widgets mode if ARG is
490 positive, and disable it otherwise. If called from Lisp, enable
491 the mode if ARG is omitted or nil.
493 Allout Widgets mode is an extension of Allout mode that provides
494 graphical decoration of outline structure. It is meant to
495 operate along with `allout-mode', via `allout-mode-hook'.
497 The graphics include:
499 - guide lines connecting item bullet-icons with those of their subitems.
501 - icons for item bullets, varying to indicate whether or not the item
502 has subitems, and if so, whether or not the item is expanded.
504 - cue area between the bullet-icon and the start of the body headline,
505 for item numbering, encryption indicator, and distinctive bullets.
507 The bullet-icon and guide line graphics provide keybindings and mouse
508 bindings for easy outline navigation and exposure control, extending
509 outline hot-spot navigation \(see `allout-mode')."
511 :lighter nil
512 :keymap nil
514 ;; define-minor-mode handles any provided argument according to emacs
515 ;; minor-mode conventions - '(elisp) Minor Mode Conventions' - and sets
516 ;; allout-widgets-mode accordingly *before* running the body code, so we
517 ;; cue on that.
518 (if allout-widgets-mode
519 ;; Activating:
520 (progn
521 (allout-add-resumptions
522 ;; XXX user may need say in line-truncation/hscrolling - an option
523 ;; that abstracts mode.
524 ;; truncate text lines to keep guide lines intact:
525 '(truncate-lines t)
526 ;; and enable autoscrolling to ease view of text
527 '(auto-hscroll-mode t)
528 '(line-move-ignore-fields t)
529 '(widget-push-button-prefix "")
530 '(widget-push-button-suffix "")
531 ;; allout-escaped-prefix-regexp depends on allout-regexp:
532 (list 'allout-escaped-prefix-regexp (concat "\\(\\\\\\)"
533 "\\(" allout-regexp "\\)")))
534 (allout-add-resumptions
535 (list 'allout-widgets-tally allout-widgets-tally)
536 (list 'allout-widgets-escapes-sanitization-regexp-pair
537 (list (concat "\\(\n\\|\\`\\)"
538 allout-escaped-prefix-regexp
540 ;; Include everything but the escape symbol.
541 "\\1\\3"))
544 (add-hook 'after-change-functions 'allout-widgets-after-change-handler
545 nil t)
547 (allout-setup-text-properties)
548 (add-to-invisibility-spec '(allout-torso . t))
549 (add-to-invisibility-spec 'allout-escapes)
551 (if (current-local-map)
552 (set-keymap-parent allout-item-body-keymap (current-local-map)))
554 (add-hook 'allout-exposure-change-hook
555 'allout-widgets-exposure-change-recorder nil 'local)
556 (add-hook 'allout-structure-added-hook
557 'allout-widgets-additions-recorder nil 'local)
558 (add-hook 'allout-structure-deleted-hook
559 'allout-widgets-deletions-recorder nil 'local)
560 (add-hook 'allout-structure-shifted-hook
561 'allout-widgets-shifts-recorder nil 'local)
562 (add-hook 'allout-after-copy-or-kill-hook
563 'allout-widgets-after-copy-or-kill-function nil 'local)
564 (add-hook 'allout-post-undo-hook
565 'allout-widgets-after-undo-function nil 'local)
567 (add-hook 'before-change-functions 'allout-widgets-before-change-handler
568 nil 'local)
569 (add-hook 'post-command-hook 'allout-widgets-post-command-business
570 nil 'local)
571 (add-hook 'pre-command-hook 'allout-widgets-pre-command-business
572 nil 'local)
574 ;; init the widgets tally for debugging:
575 (if (not allout-widgets-tally)
576 (setq allout-widgets-tally (make-hash-table
577 :test 'eq :weakness 'key)))
578 ;; add tally count display on minor-mode-alist just after
579 ;; allout-mode entry.
580 ;; (we use ternary condition form to keep condition simple for deletion.)
581 (let* ((mode-line-entry '(allout-widgets-mode-inhibit ""
582 (:eval (allout-widgets-tally-string))))
583 (associated (assoc (car mode-line-entry) minor-mode-alist))
584 ;; need location for it only if not already present:
585 (after (and (not associated)
586 (memq (assq 'allout-mode minor-mode-alist) minor-mode-alist))))
587 (if after
588 (rplacd after (cons mode-line-entry (cdr after)))))
589 (allout-widgets-prepopulate-buffer)
591 ;; Deactivating:
592 (let ((inhibit-read-only t)
593 (was-modified (buffer-modified-p)))
595 (allout-widgets-undecorate-region (point-min)(point-max))
596 (remove-from-invisibility-spec '(allout-torso . t))
597 (remove-from-invisibility-spec 'allout-escapes)
599 (remove-hook 'after-change-functions
600 'allout-widgets-after-change-handler 'local)
601 (remove-hook 'allout-exposure-change-hook
602 'allout-widgets-exposure-change-recorder 'local)
603 (remove-hook 'allout-structure-added-hook
604 'allout-widgets-additions-recorder 'local)
605 (remove-hook 'allout-structure-deleted-hook
606 'allout-widgets-deletions-recorder 'local)
607 (remove-hook 'allout-structure-shifted-hook
608 'allout-widgets-shifts-recorder 'local)
609 (remove-hook 'allout-after-copy-or-kill-hook
610 'allout-widgets-after-copy-or-kill-function 'local)
611 (remove-hook 'before-change-functions
612 'allout-widgets-before-change-handler 'local)
613 (remove-hook 'post-command-hook
614 'allout-widgets-post-command-business 'local)
615 (remove-hook 'pre-command-hook
616 'allout-widgets-pre-command-business 'local)
617 (assq-delete-all 'allout-widgets-mode-inhibit minor-mode-alist)
618 (set-buffer-modified-p was-modified))))
619 ;;;_ > allout-widgets-mode-off
620 (defun allout-widgets-mode-off ()
621 "Explicitly disable allout-widgets-mode."
622 (allout-widgets-mode -1))
623 ;;;_ > allout-widgets-mode-off
624 (defun allout-widgets-mode-on ()
625 "Explicitly disable allout-widgets-mode."
626 (allout-widgets-mode 1))
627 ;;;_ > allout-setup-text-properties ()
628 (defun allout-setup-text-properties ()
629 "Configure category and literal text properties."
631 ;; XXX body - before-change, entry, keymap
633 (setplist 'allout-guides-span-category nil)
634 (put 'allout-guides-span-category
635 'modification-hooks '(allout-graphics-modification-handler))
636 (put 'allout-guides-span-category 'local-map allout-item-icon-keymap)
637 (put 'allout-guides-span-category 'mouse-face widget-button-face)
638 (put 'allout-guides-span-category 'field 'structure)
639 ;; (put 'allout-guides-span-category 'face 'widget-button)
641 (setplist 'allout-icon-span-category
642 (allout-widgets-copy-list (symbol-plist
643 'allout-guides-span-category)))
644 (put 'allout-icon-span-category 'field 'structure)
646 ;; XXX for body text we're instead going to use the buffer-wide
647 ;; resources, like before/after-change-functions hooks and the
648 ;; buffer's key map. that way we won't have to do painful provisions
649 ;; to fixup things after edits, catch outlier interstitial
650 ;; characters, like newline and empty lines after hidden subitems,
651 ;; etc.
652 (setplist 'allout-body-span-category nil)
653 (put 'allout-body-span-category 'evaporate t)
654 (put 'allout-body-span-category 'local-map allout-item-body-keymap)
655 ;;(put 'allout-body-span-category
656 ;; 'modification-hooks '(allout-body-modification-handler))
657 ;;(put 'allout-body-span-category 'field 'body)
659 (setplist 'allout-cue-span-category nil)
660 (put 'allout-cue-span-category 'evaporate t)
661 (put 'allout-cue-span-category
662 'modification-hooks '(allout-body-modification-handler))
663 (put 'allout-cue-span-category 'local-map allout-cue-span-keymap)
664 (put 'allout-cue-span-category 'mouse-face widget-button-face)
665 (put 'allout-cue-span-category 'pointer 'arrow)
666 (put 'allout-cue-span-category 'field 'structure)
668 (setplist 'allout-trailing-category nil)
669 (put 'allout-trailing-category 'evaporate t)
670 (put 'allout-trailing-category 'local-map allout-item-body-keymap)
672 (setplist 'allout-escapes-category nil)
673 (put 'allout-escapes-category 'invisible 'allout-escapes)
674 (put 'allout-escapes-category 'evaporate t))
675 ;;;_ > allout-widgets-prepopulate-buffer ()
676 (defun allout-widgets-prepopulate-buffer ()
677 "Step over the current buffers exposed items to do initial widgetizing."
678 (if (not allout-widgets-mode-inhibit)
679 (save-excursion
680 (goto-char (point-min))
681 (while (allout-next-visible-heading 1)
682 (when (not (widget-at (point)))
683 (allout-get-or-create-item-widget))))))
684 ;;;_ . settings context
685 ;;;_ = allout-container-item
686 (defvar allout-container-item-widget nil
687 "A widget for the current outline's overarching container as an item.
689 The item has settings \(of the file/connection\) and maybe a body, but no
690 icon/bullet.")
691 (make-variable-buffer-local 'allout-container-item-widget)
692 ;;;_ . Hooks and hook helpers
693 ;;;_ , major command-loop business:
694 ;;;_ > allout-widgets-pre-command-business (&optional recursing)
695 (defun allout-widgets-pre-command-business (&optional recursing)
696 "Handle actions pending before allout-mode activity."
698 ;;;_ > allout-widgets-post-command-business (&optional recursing)
699 (defun allout-widgets-post-command-business (&optional recursing)
700 "Handle actions pending after any allout-mode commands.
702 Optional RECURSING is for internal use, to limit recursion."
703 ;; - check changed text for nesting discontinuities and escape anything
704 ;; that's: (1) asterisks at bol or (2) excessively nested.
705 (condition-case failure
707 (when (and (boundp 'allout-mode) allout-mode)
709 (if allout-widgets-unset-inhibit-read-only
710 (setq inhibit-read-only nil
711 allout-widgets-unset-inhibit-read-only nil))
713 (when allout-widgets-reenable-before-change-handler
714 (add-hook 'before-change-functions
715 'allout-widgets-before-change-handler
716 nil 'local)
717 (setq allout-widgets-reenable-before-change-handler nil))
719 (when (or allout-widgets-undo-exposure-record
720 allout-widgets-changes-record)
721 (let* ((debug-on-signal t)
722 (debug-on-error t)
723 ;; inhibit recording new undo records when processing
724 ;; effects of undo-exposure:
725 (debugger 'allout-widgets-hook-error-handler)
726 (adjusting-message " Adjusting widgets...")
727 (replaced-message (allout-widgets-adjusting-message
728 adjusting-message))
729 (start-time (current-time)))
731 (if allout-widgets-undo-exposure-record
732 ;; inhibit undo recording iff undoing exposure stuff.
733 ;; XXX we might need to inhibit per respective
734 ;; change-record, rather than assuming that some undo
735 ;; activity during a command is all undo activity.
736 (let ((buffer-undo-list t))
737 (allout-widgets-exposure-undo-processor)
738 (allout-widgets-changes-dispatcher))
739 (allout-widgets-exposure-undo-processor)
740 (allout-widgets-changes-dispatcher))
742 (if allout-widgets-time-decoration-activity
743 (setq allout-widgets-last-decoration-timing
744 (list (allout-elapsed-time-seconds (current-time)
745 start-time)
746 allout-widgets-changes-record)))
748 (setq allout-widgets-changes-record nil)
750 (if replaced-message
751 (if (stringp replaced-message)
752 (message replaced-message)
753 (message "")))))
755 ;; alas, decorated intermediate matches are not easily undecorated
756 ;; when they're automatically rehidden by isearch, so we're
757 ;; dropping this nicety.
758 ;; ;; Detect undecorated items, eg during isearch into previously
759 ;; ;; unexposed topics, and decorate "economically". Some
760 ;; ;; undecorated stuff is often exposed, to reduce lag, but the
761 ;; ;; item containing the cursor is decorated. We constrain
762 ;; ;; recursion to avoid being trapped by unexpectedly undecoratable
763 ;; ;; items.
764 ;; (when (and (not recursing)
765 ;; (not (allout-current-decorated-p))
766 ;; (or (not (equal (allout-depth) 0))
767 ;; (not allout-container-item-widget)))
768 ;; (let ((buffer-undo-list t))
769 ;; (allout-widgets-exposure-change-recorder
770 ;; allout-recent-prefix-beginning allout-recent-prefix-end nil)
771 ;; (allout-widgets-post-command-business 'recursing)))
773 ;; Detect and rectify fouled outline structure - decorated item
774 ;; not at beginning of line.
775 (let ((this-widget (or (widget-at (point))
776 ;; XXX we really should be checking across
777 ;; edited span, not just point and point+1
778 (and (not (eq (point) (point-max)))
779 (widget-at (1+ (point))))))
780 inserted-at)
781 (save-excursion
782 (if (and this-widget
783 (goto-char (widget-get this-widget :from))
784 (not (bolp)))
785 (if (not
786 (condition-case err
787 (yes-or-no-p
788 (concat "Misplaced item won't be recognizable "
789 " as part of outline - rectify? "))
790 (quit nil)))
791 (progn
792 (if (allout-hidden-p (max (1- (point)) 1))
793 (save-excursion
794 (goto-char (max (1- (point)) 1))
795 (allout-show-to-offshoot)))
796 (allout-widgets-undecorate-item this-widget))
797 ;; expose any hidden intervening items, so resulting
798 ;; position is clear:
799 (setq inserted-at (point))
800 (allout-unprotected (insert-before-markers "\n"))
801 (forward-char -1)
802 ;; ensure the inserted newline is visible:
803 (allout-flag-region inserted-at (1+ inserted-at) nil)
804 (allout-widgets-post-command-business 'recursing)
805 (message (concat "outline structure corrected - item"
806 " moved to beginning of new line"))
807 ;; preserve cursor position in some cases:
808 (if (and inserted-at
809 (> (point) inserted-at))
810 (forward-char -1)))))))
812 (error
813 ;; zero work list so we don't get stuck futilely retrying.
814 ;; error recording done by allout-widgets-hook-error-handler.
815 (setq allout-widgets-changes-record nil))))
816 ;;;_ , major change handlers:
817 ;;;_ > allout-widgets-before-change-handler
818 (defun allout-widgets-before-change-handler (beg end)
819 "Business to be done before changes in a widgetized allout outline."
820 ;; protect against unruly edits to structure:
821 (cond
822 (undo-in-progress (when (eq (get-text-property beg 'category)
823 'allout-icon-span-category)
824 (save-excursion
825 (goto-char beg)
826 (let* ((item-widget (allout-get-item-widget)))
827 (if item-widget
828 (allout-widgets-exposure-undo-recorder
829 item-widget))))))
830 (inhibit-read-only t)
831 ((not (and (boundp 'allout-mode) allout-mode)) t)
832 ((equal this-command 'quoted-insert) t)
833 ((not (text-property-any beg (if (equal end beg)
834 (min (1+ beg) (point-max))
835 end)
836 'field 'structure))
838 ((yes-or-no-p "Unruly edit of outline structure - allow? ")
839 (setq allout-widgets-unset-inhibit-read-only (not inhibit-read-only)
840 inhibit-read-only t))
842 ;; tell the allout-widgets-post-command-business to reestablish the hook:
843 (setq allout-widgets-reenable-before-change-handler t)
844 ;; and raise an error to prevent the edit (and disable the hook):
845 (error
846 (substitute-command-keys allout-structure-unruly-deletion-message)))))
847 ;;;_ > allout-widgets-after-change-handler
848 (defun allout-widgets-after-change-handler (beg end prelength)
849 "Reconcile what needs to be reconciled for allout widgets after edits."
851 ;;;_ > allout-current-decorated-p ()
852 (defun allout-current-decorated-p ()
853 "True if the current item is not decorated"
854 (save-excursion
855 (if (allout-back-to-current-heading)
856 (if (> allout-recent-depth 0)
857 (and (allout-get-item-widget) t)
858 allout-container-item-widget))))
860 ;;;_ > allout-widgets-hook-error-handler
861 (defun allout-widgets-hook-error-handler (mode args)
862 "Process errors which occurred in the course of command hook operation.
864 We store a backtrace of the error information in the variable,
865 `allout-widgets-last-hook-error', unset the error handlers, and
866 reraise the error, so that processing continues to the
867 encompassing condition-case."
868 ;; first deconstruct special error environment so errors here propagate
869 ;; to encompassing condition-case:
870 (setq debugger 'debug
871 debug-on-error nil
872 debug-on-signal nil)
873 (let* ((bt (with-output-to-string (backtrace)))
874 (this "allout-widgets-hook-error-handler")
875 (header
876 (format "allout-widgets-last-hook-error stored, %s/%s %s %s"
877 this mode args
878 (format-time-string "%e-%b-%Y %r" (current-time)))))
879 ;; post to *Messages* then immediately replace with more compact notice:
880 (message "%s" (setq allout-widgets-last-hook-error
881 (format "%s:\n%s" header bt)))
882 (message header) (sit-for allout-widgets-hook-error-post-time)
883 ;; reraise the error, or one concerning this function if unexpected:
884 (if (equal mode 'error)
885 (apply 'signal args)
886 (error "%s: unexpected mode, %s %s" this mode args))))
887 ;;;_ > allout-widgets-changes-exceed-threshold-p ()
888 (defun allout-widgets-adjusting-message (message)
889 "Post MESSAGE when pending are likely to make a big enough delay.
891 If posting of the MESSAGE is warranted and there already is a
892 `current-message' in the minibuffer, the MESSAGE is appended to
893 the current one, and the previously pending `current-message' is
894 returned for later posting on completion.
896 If posting of the MESSAGE is warranted, but no `current-message'
897 is pending, then t is returned to indicate that case.
899 If posting of the MESSAGE is not warranted, then nil is returned.
901 See `allout-widgets-adjust-message-length-threshold',
902 `allout-widgets-adjust-message-size-threshold' for message
903 posting threshold criteria."
904 (if (or (> (length allout-widgets-changes-record)
905 allout-widgets-adjust-message-length-threshold)
906 ;; for size, use distance from start of first to end of last:
907 (let ((min (point-max))
908 (max 0)
909 first second)
910 (mapc (function (lambda (entry)
911 (if (eq :undone-exposure (car entry))
913 (setq first (cadr entry)
914 second (caddr entry))
915 (if (< (min first second) min)
916 (setq min (min first second)))
917 (if (> (max first second) max)
918 (setq max (max first second))))))
919 allout-widgets-changes-record)
920 (> (- max min) allout-widgets-adjust-message-size-threshold)))
921 (let ((prior (current-message)))
922 (message (if prior (concat prior " - " message) message))
923 (or prior t))))
924 ;;;_ > allout-widgets-changes-dispatcher ()
925 (defun allout-widgets-changes-dispatcher ()
926 "Dispatch CHANGES-RECORD items to respective widgets change processors."
928 (if (not allout-widgets-mode-inhibit)
929 (let* ((changes-record allout-widgets-changes-record)
930 (changes-pending (and changes-record t))
931 entry
932 exposures
933 additions
934 deletions
935 shifts)
937 (when changes-pending
938 (while changes-record
939 (setq entry (pop changes-record))
940 (case (car entry)
941 (:exposed (push entry exposures))
942 (:added (push entry additions))
943 (:deleted (push entry deletions))
944 (:shifted (push entry shifts))))
946 (if exposures
947 (allout-widgets-exposure-change-processor exposures))
948 (if additions
949 (allout-widgets-additions-processor additions))
950 (if deletions
951 (allout-widgets-deletions-processor deletions))
952 (if shifts
953 (allout-widgets-shifts-processor shifts))))
954 (when (not (equal allout-widgets-mode-inhibit 'undecorated))
955 (allout-widgets-undecorate-region (point-min)(point-max))
956 (setq allout-widgets-mode-inhibit 'undecorated))))
957 ;;;_ > allout-widgets-exposure-change-recorder (from to flag)
958 (defun allout-widgets-exposure-change-recorder (from to flag)
959 "Record allout exposure changes for tracking during post-command processing.
961 Records changes in `allout-widgets-changes-record'."
962 (push (list :exposed from to flag) allout-widgets-changes-record))
963 ;;;_ > allout-widgets-exposure-change-processor (changes)
964 (defun allout-widgets-exposure-change-processor (changes)
965 "Widgetize and adjust item widgets tracking allout outline exposure changes.
967 Generally invoked via `allout-exposure-change-hook'."
969 (let ((changes (sort changes (function (lambda (this next)
970 (< (cadr this) (cadr next))))))
971 ;; have to distinguish between concealing and exposing so that, eg,
972 ;; `allout-expose-topic's mix is handled properly.
973 handled-expose
974 handled-conceal
975 covered
976 deactivate-mark)
978 (dolist (change changes)
979 (let (handling
980 (from (cadr change))
981 bucket got
982 (to (caddr change))
983 (flag (cadddr change))
984 parent)
986 ;; swap from and to:
987 (if (< to from) (setq bucket to
988 to from
989 from bucket))
991 ;; have we already handled exposure changes in this region?
992 (setq handling (if flag 'handled-conceal 'handled-expose)
993 got (allout-range-overlaps from to (symbol-value handling))
994 covered (car got))
995 (set handling (cadr got))
997 (when (not covered)
998 (save-excursion
999 (goto-char from)
1000 (cond
1002 ;; collapsing:
1003 (flag
1004 (allout-widgets-undecorate-region from to)
1005 (allout-beginning-of-current-line)
1006 (let ((widget (allout-get-item-widget)))
1007 (if (not widget)
1008 (allout-get-or-create-item-widget)
1009 (widget-apply widget :redecorate))))
1011 ;; expanding:
1013 (while (< (point) to)
1014 (allout-beginning-of-current-line)
1015 (setq parent (allout-get-item-widget))
1016 (if (not parent)
1017 (setq parent (allout-get-or-create-item-widget))
1018 (widget-apply parent :redecorate))
1019 (allout-next-visible-heading 1)
1020 (if (widget-get parent :has-subitems)
1021 (allout-redecorate-visible-subtree parent))
1022 (if (> (point) to)
1023 ;; subtree may be well beyond to - incorporate in ranges:
1024 (setq handled-expose
1025 (allout-range-overlaps from (point) handled-expose)
1026 covered (car handled-expose)
1027 handled-expose (cadr handled-expose)))
1028 (allout-next-visible-heading 1))))))))))
1030 ;;;_ > allout-widgets-additions-recorder (from to)
1031 (defun allout-widgets-additions-recorder (from to)
1032 "Record allout item additions for tracking during post-command processing.
1034 Intended for use on `allout-structure-added-hook'.
1036 FROM point at the start of the first new item and TO is point at the start
1037 of the last one.
1039 Records changes in `allout-widgets-changes-record'."
1040 (push (list :added from to) allout-widgets-changes-record))
1041 ;;;_ > allout-widgets-additions-processor (changes)
1042 (defun allout-widgets-additions-processor (changes)
1043 "Widgetize and adjust items tracking allout outline structure additions.
1045 Dispatched by `allout-widgets-post-command-business' in response to
1046 :added entries recorded by `allout-widgets-additions-recorder'."
1047 (save-excursion
1048 (let (handled
1049 covered)
1050 (dolist (change changes)
1051 (let ((from (cadr change))
1052 bucket
1053 (to (caddr change)))
1054 (if (< to from) (setq bucket to to from from bucket))
1055 ;; have we already handled exposure changes in this region?
1056 (setq handled (allout-range-overlaps from to handled)
1057 covered (car handled)
1058 handled (cadr handled))
1059 (when (not covered)
1060 (goto-char from)
1061 ;; Prior sibling and parent can both be affected.
1062 (if (allout-ascend)
1063 (allout-redecorate-visible-subtree
1064 (allout-get-or-create-item-widget 'redecorate)))
1065 (if (< (point) from)
1066 (goto-char from))
1067 (while (and (< (point) to) (not (eobp)))
1068 (allout-beginning-of-current-line)
1069 (allout-redecorate-visible-subtree
1070 (allout-get-or-create-item-widget))
1071 (allout-next-visible-heading 1))
1072 (if (> (point) to)
1073 ;; subtree may be well beyond to - incorporate in ranges:
1074 (setq handled (allout-range-overlaps from (point) handled)
1075 covered (car handled)
1076 handled (cadr handled)))))))))
1078 ;;;_ > allout-widgets-deletions-recorder (depth from)
1079 (defun allout-widgets-deletions-recorder (depth from)
1080 "Record allout item deletions for tracking during post-command processing.
1082 Intended for use on `allout-structure-deleted-hook'.
1084 DEPTH is the depth of the deleted subtree, and FROM is the point from which
1085 the subtree was deleted.
1087 Records changes in `allout-widgets-changes-record'."
1088 (push (list :deleted depth from) allout-widgets-changes-record))
1089 ;;;_ > allout-widgets-deletions-processor (changes)
1090 (defun allout-widgets-deletions-processor (changes)
1091 "Adjust items tracking allout outline structure deletions.
1093 Dispatched by `allout-widgets-post-command-business' in response to
1094 :deleted entries recorded by `allout-widgets-deletions-recorder'."
1095 (save-excursion
1096 (dolist (change changes)
1097 (let ((depth (cadr change))
1098 (from (caddr change)))
1099 (goto-char from)
1100 (when (allout-previous-visible-heading 1)
1101 (if (> depth 1)
1102 (allout-ascend-to-depth (1- depth)))
1103 (allout-redecorate-visible-subtree
1104 (allout-get-or-create-item-widget 'redecorate)))))))
1106 ;;;_ > allout-widgets-shifts-recorder (shifted-amount at)
1107 (defun allout-widgets-shifts-recorder (shifted-amount at)
1108 "Record outline subtree shifts for tracking during post-command processing.
1110 Intended for use on `allout-structure-shifted-hook'.
1112 SHIFTED-AMOUNT is the depth change and AT is the point at the start of the
1113 subtree that's been shifted.
1115 Records changes in `allout-widgets-changes-record'."
1116 (push (list :shifted shifted-amount at) allout-widgets-changes-record))
1117 ;;;_ > allout-widgets-shifts-processor (changes)
1118 (defun allout-widgets-shifts-processor (changes)
1119 "Widgetize and adjust items tracking allout outline structure additions.
1121 Dispatched by `allout-widgets-post-command-business' in response to
1122 :shifted entries recorded by `allout-widgets-shifts-recorder'."
1123 (save-excursion
1124 (dolist (change changes)
1125 (goto-char (caddr change))
1126 (allout-ascend)
1127 (allout-redecorate-visible-subtree))))
1128 ;;;_ > allout-widgets-after-copy-or-kill-function ()
1129 (defun allout-widgets-after-copy-or-kill-function ()
1130 "Do allout-widgets processing of text just placed in the kill ring.
1132 Intended for use on allout-after-copy-or-kill-hook."
1133 (if (car kill-ring)
1134 (setcar kill-ring (allout-widgets-undecorate-text (car kill-ring)))))
1135 ;;;_ > allout-widgets-after-undo-function ()
1136 (defun allout-widgets-after-undo-function ()
1137 "Do allout-widgets processing of text after an undo.
1139 Intended for use on allout-post-undo-hook."
1140 (save-excursion
1141 (if (allout-goto-prefix)
1142 (allout-redecorate-item (allout-get-or-create-item-widget)))))
1144 ;;;_ > allout-widgets-exposure-undo-recorder (widget from-state)
1145 (defun allout-widgets-exposure-undo-recorder (widget)
1146 "Record outline exposure undo for tracking during post-command processing.
1148 Intended for use by `allout-graphics-modification-handler'.
1150 WIDGET is the widget being changed.
1152 Records changes in `allout-widgets-changes-record'."
1153 ;; disregard the events if we're currently processing them.
1154 (if (not allout-undo-exposure-in-progress)
1155 (push widget allout-widgets-undo-exposure-record)))
1156 ;;;_ > allout-widgets-exposure-undo-processor ()
1157 (defun allout-widgets-exposure-undo-processor ()
1158 "Adjust items tracking undo of allout outline structure exposure.
1160 Dispatched by `allout-widgets-post-command-business' in response to
1161 :undone-exposure entries recorded by `allout-widgets-exposure-undo-recorder'."
1162 (let* ((allout-undo-exposure-in-progress t)
1163 ;; inhibit undo recording while twiddling exposure to track undo:
1164 (widgets allout-widgets-undo-exposure-record)
1165 widget widget-start-marker widget-end-marker
1166 from-state icon-start-point to-state
1167 handled covered)
1168 (setq allout-widgets-undo-exposure-record nil)
1169 (save-excursion
1170 (dolist (widget widgets)
1171 (setq widget-start-marker (widget-get widget :from)
1172 widget-end-marker (widget-get widget :to)
1173 from-state (widget-get widget :icon-state)
1174 icon-start-point (widget-apply widget :actual-position
1175 :icon-start)
1176 to-state (get-text-property icon-start-point
1177 :icon-state))
1178 (setq handled (allout-range-overlaps widget-start-marker
1179 widget-end-marker
1180 handled)
1181 covered (car handled)
1182 handled (cadr handled))
1183 (when (not covered)
1184 (goto-char (widget-get widget :from))
1185 (when (not (allout-hidden-p))
1186 ;; adjust actual exposure to that of to-state viz from-state
1187 (cond ((and (eq to-state 'closed) (eq from-state 'opened))
1188 (allout-hide-current-subtree)
1189 (allout-decorate-item-and-context widget))
1190 ((and (eq to-state 'opened) (eq from-state 'closed))
1191 (save-excursion
1192 (dolist
1193 (expose-to (allout-chart-exposure-contour-by-icon))
1194 (goto-char expose-to)
1195 (allout-show-to-offshoot)))))))))))
1196 ;;;_ > allout-chart-exposure-contour-by-icon (&optional from-depth)
1197 (defun allout-chart-exposure-contour-by-icon (&optional from-depth)
1198 "Return points of subtree items to which exposure should be extended.
1200 The qualifying items are ones with a widget icon that is in the closed or
1201 empty state, or items with undecorated subitems.
1203 The resulting list of points is in reverse order.
1205 Optional FROM-DEPTH is for internal use."
1206 ;; During internal recursion, we return a pair: (at-end . result)
1207 ;; Otherwise we just return the result.
1208 (let ((from-depth from-depth)
1209 start-point
1210 at-end level-depth
1211 this-widget
1212 got subgot)
1213 (if from-depth
1214 (setq level-depth (allout-depth))
1215 ;; at containing item:
1216 (setq start-point (point))
1217 (setq from-depth (allout-depth))
1218 (setq at-end (not (allout-next-heading))
1219 level-depth allout-recent-depth))
1221 ;; traverse the level, recursing on deeper levels:
1222 (while (and (not at-end)
1223 (> allout-recent-depth from-depth)
1224 (setq this-widget (allout-get-item-widget)))
1225 (if (< level-depth allout-recent-depth)
1226 ;; recurse:
1227 (progn
1228 (setq subgot (allout-chart-exposure-contour-by-icon level-depth)
1229 at-end (car subgot)
1230 subgot (cdr subgot))
1231 (if subgot (setq got (append subgot got))))
1232 ;; progress at this level:
1233 (when (memq (widget-get this-widget :icon-state) '(closed empty))
1234 (push (point) got)
1235 (allout-end-of-subtree))
1236 (setq at-end (not (allout-next-heading)))))
1238 ;; tailor result depending on whether or not we're a recursion:
1239 (if (not start-point)
1240 (cons at-end got)
1241 (goto-char start-point)
1242 got)))
1243 ;;;_ > allout-range-overlaps (from to ranges)
1244 (defun allout-range-overlaps (from to ranges)
1245 "Return a pair indicating overlap of FROM and TO subtree range in RANGES.
1247 First element of result indicates whether candidate range FROM, TO
1248 overlapped any of the existing ranges.
1250 Second element of result is a new version of RANGES incorporating the
1251 candidate range with overlaps consolidated.
1253 FROM and TO must be in increasing order, as must be the pairs in RANGES."
1254 ;; to append to the end: (rplacd next-to-last-cdr (list 'f))
1255 (let (new-ranges
1256 entry
1257 ;; the start of the range that includes the candidate from:
1258 included-from
1259 ;; the end of the range that includes the candidate to:
1260 included-to
1261 ;; the candidates were inserted:
1262 done)
1263 (while (and ranges (not done))
1264 (setq entry (car ranges)
1265 ranges (cdr ranges))
1267 (cond
1269 (included-from
1270 ;; some entry included the candidate from.
1271 (cond ((> (car entry) to)
1272 ;; current entry exceeds end of candidate range - done.
1273 (push (list included-from to) new-ranges)
1274 (push entry new-ranges)
1275 (setq included-to to
1276 done t))
1277 ((>= (cadr entry) to)
1278 ;; current entry includes end of candidate range - done.
1279 (push (list included-from (cadr entry)) new-ranges)
1280 (setq included-to (cadr entry)
1281 done t))
1282 ;; current entry contained in candidate range - ditch, continue:
1283 (t nil)))
1285 ((> (car entry) to)
1286 ;; current entry start exceeds candidate end - done, placed as new entry
1287 (push (list from to) new-ranges)
1288 (push entry new-ranges)
1289 (setq included-to to
1290 done t))
1292 ((>= (car entry) from)
1293 ;; current entry start is above candidate start, but not above
1294 ;; candidate end (by prior case).
1295 (setq included-from from)
1296 ;; now we have to check on whether this entry contains to, or continue:
1297 (when (>= (cadr entry) to)
1298 ;; current entry contains only candidate end - done:
1299 (push (list included-from (cadr entry)) new-ranges)
1300 (setq included-to (cadr entry)
1301 done t))
1302 ;; otherwise, we will continue to look for placement of candidate end.
1305 ((>= (cadr entry) to)
1306 ;; current entry properly contains candidate range.
1307 (push entry new-ranges)
1308 (setq included-from (car entry)
1309 included-to (cadr entry)
1310 done t))
1312 ((>= (cadr entry) from)
1313 ;; current entry contains start of candidate range.
1314 (setq included-from (car entry)))
1317 ;; current entry is below the candidate range.
1318 (push entry new-ranges))))
1320 (cond ((and included-from included-to)
1321 ;; candidates placed.
1322 nil)
1323 ((not (or included-from included-to))
1324 ;; candidates found no place, must be at the end:
1325 (push (list from to) new-ranges))
1326 (included-from
1327 ;; candidate start placed but end not:
1328 (push (list included-from to) new-ranges))
1329 ;; might be included-to and not included-from, indicating new entry.
1331 (setq new-ranges (nreverse new-ranges))
1332 (if ranges (setq new-ranges (append new-ranges ranges)))
1333 (list (if included-from t) new-ranges)))
1334 ;;;_ > allout-test-range-overlaps ()
1335 (defun allout-test-range-overlaps ()
1336 "allout-range-overlaps unit tests."
1337 (let* (ranges
1339 (try (lambda (from to)
1340 (setq got (allout-range-overlaps from to ranges))
1341 (setq ranges (cadr got))
1342 got)))
1343 ;; ;; biggie:
1344 ;; (setq ranges nil)
1345 ;; ;; ~ .02 to .1 seconds for just repeated listing args instead of funcall
1346 ;; ;; ~ 13 seconds for doing repeated funcall
1347 ;; (message "time-trial: %s, resulting size %s"
1348 ;; (time-trial
1349 ;; '(let ((size 10000)
1350 ;; doing)
1351 ;; (random t)
1352 ;; (dotimes (count size)
1353 ;; (setq doing (random size))
1354 ;; (funcall try doing (+ doing (random 5)))
1355 ;; ;;(list doing (+ doing (random 5)))
1356 ;; )))
1357 ;; (length ranges))
1358 ;; (sit-for 2)
1360 ;; fresh:
1361 (setq ranges nil)
1362 (assert (equal (funcall try 3 5) '(nil ((3 5)))))
1363 ;; add range at end:
1364 (assert (equal (funcall try 10 12) '(nil ((3 5) (10 12)))))
1365 ;; add range at beginning:
1366 (assert (equal (funcall try 1 2) '(nil ((1 2) (3 5) (10 12)))))
1367 ;; insert range somewhere in the middle:
1368 (assert (equal (funcall try 7 9) '(nil ((1 2) (3 5) (7 9) (10 12)))))
1369 ;; consolidate some:
1370 (assert (equal (funcall try 5 8) '(t ((1 2) (3 9) (10 12)))))
1371 ;; add more:
1372 (assert (equal (funcall try 15 17) '(nil ((1 2) (3 9) (10 12) (15 17)))))
1373 ;; add more:
1374 (assert (equal (funcall try 20 22)
1375 '(nil ((1 2) (3 9) (10 12) (15 17) (20 22)))))
1376 ;; encompass more:
1377 (assert (equal (funcall try 4 11) '(t ((1 2) (3 12) (15 17) (20 22)))))
1378 ;; encompass all:
1379 (assert (equal (funcall try 2 25) '(t ((1 25)))))
1381 ;; fresh slate:
1382 (setq ranges nil)
1383 (assert (equal (funcall try 20 25) '(nil ((20 25)))))
1384 (assert (equal (funcall try 30 35) '(nil ((20 25) (30 35)))))
1385 (assert (equal (funcall try 26 28) '(nil ((20 25) (26 28) (30 35)))))
1386 (assert (equal (funcall try 15 20) '(t ((15 25) (26 28) (30 35)))))
1387 (assert (equal (funcall try 10 30) '(t ((10 35)))))
1388 (assert (equal (funcall try 5 6) '(nil ((5 6) (10 35)))))
1389 (assert (equal (funcall try 2 100) '(t ((2 100)))))
1391 (setq ranges nil)
1393 ;;;_ > allout-widgetize-buffer (&optional doing)
1394 (defun allout-widgetize-buffer (&optional doing)
1395 "EXAMPLE FUNCTION. Widgetize items in buffer using allout-chart-subtree.
1397 We economize by just focusing on the first of local-maximum depth siblings.
1399 Optional DOING is for internal use - a chart of the current level, for
1400 recursive operation."
1402 (interactive)
1403 (if (not doing)
1405 (save-excursion
1406 (goto-char (point-min))
1407 ;; Construct the chart by scanning the siblings:
1408 (dolist (top-level-sibling (allout-chart-siblings))
1409 (goto-char top-level-sibling)
1410 (let ((subchart (allout-chart-subtree)))
1411 (if subchart
1412 (allout-widgetize-buffer subchart)))))
1414 ;; save-excursion was done on recursion entry, not necessary here.
1415 (let (have-sublists)
1416 (dolist (sibling doing)
1417 (when (listp sibling)
1418 (setq have-sublists t)
1419 (allout-widgetize-buffer sibling)))
1420 (when (and (not have-sublists) (not (widget-at (car doing))))
1421 (goto-char (car doing))
1422 (allout-get-or-create-item-widget)))))
1424 ;;;_ : Item widget and constructors
1426 ;;;_ $ allout-item-widget
1427 (define-widget 'allout-item-widget 'default
1428 "A widget presenting an allout outline item."
1430 'button nil
1431 ;; widget-field-at respects this to get item if 'field is unused.
1432 ;; we don't use field to avoid collision with end-of-line, etc, on which
1433 ;; allout depends.
1434 'real-field nil
1436 ;; data fields:
1439 ;; tailor the widget for a specific item
1440 :create 'allout-decorate-item-and-context
1441 :value-delete 'allout-widgets-undecorate-item
1442 ;; Not Yet Converted (from original, tree-widget stab)
1443 :expander 'allout-tree-event-dispatcher ; get children when nil :args
1444 :expander-p 'identity ; always engage the :expander
1445 :action 'allout-tree-widget-action
1446 ;; :notify "when item changes"
1448 ;; force decoration of item but not context, unless already done this tick:
1449 :redecorate 'allout-redecorate-item
1450 :last-decorated-tick nil
1451 ;; recognize the actual situation of the item's text:
1452 :parse-item 'allout-parse-item-at-point
1453 ;; decorate the entirety of the item, sans offspring:
1454 :decorate-item-span 'allout-decorate-item-span
1455 ;; decorate the various item elements:
1456 :decorate-guides 'allout-decorate-item-guides
1457 :decorate-icon 'allout-decorate-item-icon
1458 :decorate-cue 'allout-decorate-item-cue
1459 :decorate-body 'allout-decorate-item-body
1460 :actual-position 'allout-item-actual-position
1462 ;; Layout parameters:
1463 :is-container nil ; is this actually the encompassing file/connection?
1465 :from nil ; item beginning - marker
1466 :to nil ; item end - marker
1467 :span-overlay nil ; overlay by which actual position is determined
1469 ;; also serves as guide-end:
1470 :icon-start nil
1471 :icon-end nil
1472 :distinctive-start nil
1473 ;; also serves as cue-start:
1474 :distinctive-end nil
1475 ;; also serves as cue-end:
1476 :body-start nil
1477 :body-end nil
1478 :depth nil
1479 :has-subitems nil
1480 :was-has-subitems 'init
1481 :expanded nil
1482 :was-expanded 'init
1483 :brief nil
1484 :was-brief 'init
1486 :does-encrypt nil ; pending encryption when :is-encrypted false.
1487 :is-encrypted nil
1489 ;; the actual location of the item text:
1490 :location 'allout-item-location
1492 :button-keymap allout-item-icon-keymap ; XEmacs
1493 :keymap allout-item-icon-keymap ; Emacs
1495 ;; Element regions:
1496 :guides-span nil
1497 :icon-span nil
1498 :cue-span nil
1499 :bullet nil
1500 :was-bullet nil
1501 :body-span nil
1503 :body-brevity-p 'allout-body-brevity-p
1505 ;; :guide-column-flags indicate (in reverse order) whether or not the
1506 ;; item's ancestor at the depth corresponding to the column has a
1507 ;; subsequent sibling - ie, whether or not the corresponding column needs
1508 ;; a descender line to connect that ancestor with its sibling.
1509 :guide-column-flags nil
1510 :was-guide-column-flags 'init
1512 ;; ie, has subitems:
1513 :populous-p 'allout-item-populous-p
1514 :help-echo 'allout-tree-widget-help-echo
1516 ;;;_ > allout-new-item-widget ()
1517 (defsubst allout-new-item-widget ()
1518 "create a new item widget, not yet situated anywhere."
1519 (if allout-widgets-maintain-tally
1520 ;; all the extra overhead is incurred only when doing the
1521 ;; maintenance, except the condition, which can't be avoided.
1522 (let ((widget (widget-convert 'allout-item-widget)))
1523 (puthash widget nil allout-widgets-tally)
1524 widget)
1525 (widget-convert 'allout-item-widget)))
1526 ;;;_ : Item decoration
1527 ;;;_ > allout-decorate-item-and-context (item-widget &optional redecorate
1528 ;;; blank-container parent)
1529 (defun allout-decorate-item-and-context (item-widget &optional redecorate
1530 blank-container parent)
1531 "Create or adjust widget decorations for ITEM-WIDGET and neighbors at point.
1533 The neighbors include its siblings and parent.
1535 ITEM-WIDGET can be a created or converted allout-item-widget.
1537 If you're only trying to get or create a widget for an item, use
1538 `allout-get-or-create-item-widget'. If you have the item-widget, applying
1539 :redecorate will do the right thing.
1541 Optional BLANK-CONTAINER is for internal use. It is used to fabricate a
1542 container widget for an empty-bodied container, in the course of decorating
1543 a proper \(non-container\) item which starts at the beginning of the file.
1545 Optional REDECORATE causes redecoration of the item-widget and
1546 its siblings, even if already decorated in this cycle of the command loop.
1548 Optional PARENT, when provided, bypasses some navigation and computation
1549 necessary to obtain the parent of the items being processed.
1551 We return the item-widget corresponding to the item at point."
1553 (when (or redecorate
1554 (not (equal (widget-get item-widget :last-decorated-tick)
1555 allout-command-counter)))
1556 (let* ((allout-inhibit-body-modification-hook t)
1557 (was-modified (buffer-modified-p))
1558 (was-point (point))
1559 prefix-start
1560 (is-container (or blank-container
1561 (not (setq prefix-start (allout-goto-prefix)))
1562 (< was-point prefix-start)))
1563 ;; steady-point (set in two steps) is reliable across parent
1564 ;; widget-creation.
1565 (steady-point (progn (if is-container (goto-char 1))
1566 (point-marker)))
1567 (steady-point (progn (set-marker-insertion-type steady-point t)
1568 steady-point))
1569 (parent (and (not is-container)
1570 (allout-get-or-create-parent-widget)))
1571 parent-flags parent-depth
1572 successor-sibling
1573 body
1574 doing-item
1575 sub-item-widget
1576 depth
1577 reverse-siblings-chart
1578 (buffer-undo-list t))
1580 ;; At this point the parent is decorated and parent-flags indicate
1581 ;; its guide lines. We will iterate over the siblings according to a
1582 ;; chart we create at the start, and going from last to first so we
1583 ;; don't have to worry about text displacement caused by widgetizing.
1585 (if is-container
1586 (progn (widget-put item-widget :is-container t)
1587 (setq reverse-siblings-chart (list 1)))
1588 (goto-char (widget-apply parent :actual-position :from))
1589 (if (widget-get parent :is-container)
1590 ;; `allout-goto-prefix' will go to first non-container item:
1591 (allout-goto-prefix)
1592 (allout-next-heading))
1593 (setq depth (allout-recent-depth))
1594 (setq reverse-siblings-chart (list allout-recent-prefix-beginning))
1595 (while (allout-next-sibling)
1596 (push allout-recent-prefix-beginning reverse-siblings-chart)))
1598 (dolist (doing-at reverse-siblings-chart)
1599 (goto-char doing-at)
1600 (when allout-widgets-track-decoration
1601 (sit-for 0))
1603 (setq doing-item (if (= doing-at steady-point)
1604 item-widget
1605 (or (allout-get-item-widget)
1606 (allout-new-item-widget))))
1608 (when (or redecorate (not (equal (widget-get doing-item
1609 :last-decorated-tick)
1610 allout-command-counter)))
1611 (widget-apply doing-item :parse-item t blank-container)
1612 (widget-apply doing-item :decorate-item-span)
1614 (widget-apply doing-item :decorate-guides
1615 parent (and successor-sibling t))
1616 (widget-apply doing-item :decorate-icon)
1617 (widget-apply doing-item :decorate-cue)
1618 (widget-apply doing-item :decorate-body)
1620 (widget-put doing-item :last-decorated-tick allout-command-counter))
1622 (setq successor-sibling doing-at))
1624 (set-buffer-modified-p was-modified)
1625 (goto-char steady-point)
1626 ;; must null the marker or the buffer gets clogged with impedance:
1627 (set-marker steady-point nil)
1629 item-widget)))
1630 ;;;_ > allout-redecorate-item (item)
1631 (defun allout-redecorate-item (item-widget)
1632 "Resituate ITEM-WIDGET decorations, disregarding context.
1634 Use this to redecorate only the item, when you know that its
1635 situation with respect to siblings, parent, and offspring is
1636 unchanged from its last decoration. Use
1637 `allout-decorate-item-and-context' instead to reassess and adjust
1638 relevent context, when suitable."
1639 (if (not (equal (widget-get item-widget :last-decorated-tick)
1640 allout-command-counter))
1641 (let ((was-modified (buffer-modified-p))
1642 (buffer-undo-list t))
1643 (widget-apply item-widget :parse-item)
1644 (widget-apply item-widget :decorate-guides)
1645 (widget-apply item-widget :decorate-icon)
1646 (widget-apply item-widget :decorate-cue)
1647 (widget-apply item-widget :decorate-body)
1648 (set-buffer-modified-p was-modified))))
1649 ;;;_ > allout-redecorate-visible-subtree (&optional parent-widget
1650 ;;; depth chart)
1651 (defun allout-redecorate-visible-subtree (&optional parent-widget depth chart)
1652 "Redecorate all visible items in subtree at point.
1654 Optional PARENT-WIDGET is for optimization, when the parent
1655 widget is already available.
1657 Optional DEPTH restricts the excursion depth of covered.
1659 Optional CHART is for internal recursion, to carry a chart of the
1660 target items.
1662 Point is left at the last sibling in the visible subtree."
1663 ;; using a treatment that takes care of all the siblings on a level, we
1664 ;; only need apply it to the first sibling on the level, and we can
1665 ;; collect and pass the parent of the lower levels to recursive calls as
1666 ;; we go.
1667 (let ((parent-widget
1668 (if (and parent-widget (widget-apply parent-widget
1669 :actual-position :from))
1670 (progn (goto-char (widget-apply parent-widget
1671 :actual-position :from))
1672 parent-widget)
1673 (let ((got (allout-get-item-widget)))
1674 (if got
1675 (allout-decorate-item-and-context got 'redecorate)
1676 (allout-get-or-create-item-widget 'redecorate)))))
1677 (pending-chart (or chart (allout-chart-subtree nil 'visible)))
1678 item-widget
1679 previous-sibling-point
1680 previous-sibling
1681 recent-sibling-point)
1682 (setq pending-chart (nreverse pending-chart))
1683 (dolist (sibling-point pending-chart)
1684 (cond ((integerp sibling-point)
1685 (when (not previous-sibling-point)
1686 (goto-char sibling-point)
1687 (if (setq item-widget (allout-get-item-widget nil))
1688 (allout-decorate-item-and-context item-widget 'redecorate
1689 nil parent-widget)
1690 (allout-get-or-create-item-widget)))
1691 (setq previous-sibling-point sibling-point
1692 recent-sibling-point sibling-point))
1693 ((listp sibling-point)
1694 (if (or (not depth)
1695 (> depth 1))
1696 (allout-redecorate-visible-subtree
1697 (if (not previous-sibling-point)
1698 ;; containment discontinuity - sigh
1699 parent-widget
1700 (allout-get-or-create-item-widget 'redecorate))
1701 (if depth (1- depth))
1702 sibling-point)))))
1703 (if (and recent-sibling-point (< (point) recent-sibling-point))
1704 (goto-char recent-sibling-point))))
1705 ;;;_ > allout-parse-item-at-point (item-widget &optional at-beginning
1706 ;;; blank-container)
1707 (defun allout-parse-item-at-point (item-widget &optional at-beginning
1708 blank-container)
1709 "Set widget ITEM-WIDGET layout parameters per item-at-point's actual layout.
1711 If optional AT-BEGINNING is t, then point is assumed to be at the start of
1712 the item prefix.
1714 If optional BLANK-CONTAINER is true, then the parameters of a container
1715 which has an empty body are set. \(Though the body is blank, the object
1716 may have subitems.\)"
1718 ;; Uncomment this sit-for to notice where decoration is happening:
1719 ;; (sit-for .1)
1720 (let* ((depth (allout-depth))
1721 (depth (if blank-container 0 depth))
1722 (is-container (or blank-container (zerop depth)))
1724 (does-encrypt (and (not is-container)
1725 (allout-encrypted-type-prefix)))
1726 (is-encrypted (and does-encrypt (allout-encrypted-topic-p)))
1727 (icon-end allout-recent-prefix-end)
1728 (icon-start (1- icon-end))
1729 body-start
1730 body-end
1731 bullet
1732 has-subitems
1733 (contents-depth (1+ depth))
1735 (widget-put item-widget :depth depth)
1736 (if is-container
1738 (progn
1739 (widget-put item-widget :from (allout-set-boundary-marker
1740 :from (point-min)
1741 (widget-get item-widget :from)))
1742 (widget-put item-widget :icon-end nil)
1743 (widget-put item-widget :icon-start nil)
1744 (setq body-start (widget-put item-widget :body-start 1)))
1746 ;; not container:
1748 (widget-put item-widget :from (allout-set-boundary-marker
1749 :from (if at-beginning
1750 (point)
1751 allout-recent-prefix-beginning)
1752 (widget-get item-widget :from)))
1753 (widget-put item-widget :icon-start icon-start)
1754 (widget-put item-widget :icon-end icon-end)
1755 (when does-encrypt
1756 (widget-put item-widget :does-encrypt t)
1757 (widget-put item-widget :is-encrypted is-encrypted))
1759 ;; cue area:
1760 (setq body-start icon-end)
1761 (widget-put item-widget :bullet (setq bullet (allout-get-bullet)))
1762 (if (equal (char-after body-start) ? )
1763 (setq body-start (1+ body-start)))
1764 (widget-put item-widget :body-start body-start)
1767 ;; Both container and regular items:
1769 ;; :body-end (doesn't include a trailing blank line, if any) -
1770 (widget-put item-widget :body-end (setq body-end
1771 (if blank-container
1773 (allout-end-of-entry))))
1775 (widget-put item-widget :to (allout-set-boundary-marker
1776 :to (if blank-container
1777 (point-min)
1778 (or (allout-pre-next-prefix)
1779 (goto-char (point-max))))
1780 (widget-get item-widget :to)))
1781 (widget-put item-widget :has-subitems
1782 (setq has-subitems
1783 (and
1784 ;; has a subsequent item:
1785 (not (= body-end (point-max)))
1786 ;; subsequent item is deeper:
1787 (< depth (setq contents-depth (allout-recent-depth))))))
1788 ;; note :expanded - true if widget item's content is currently visible?
1789 (widget-put item-widget :expanded
1790 (and has-subitems
1791 ;; subsequent item is or isn't visible:
1792 (save-excursion
1793 (goto-char allout-recent-prefix-beginning)
1794 (not (allout-hidden-p)))))))
1795 ;;;_ > allout-set-boundary-marker (boundary position &optional current-marker)
1796 (defun allout-set-boundary-marker (boundary position &optional current-marker)
1797 "Set or create item widget BOUNDARY type marker at POSITION.
1799 Optional CURRENT-MARKER is the marker currently being used for
1800 the boundary, if any.
1802 BOUNDARY type is either :from or :to, determining the marker insertion type."
1803 (if (not position) (setq position (point)))
1804 (if current-marker
1805 (set-marker current-marker position)
1806 (let ((marker (make-marker)))
1807 ;; XXX dang - would like for :from boundary to advance after inserted
1808 ;; text, but that would omit new header prefixes when allout
1809 ;; relevels, etc. this competes with ad-hoc edits, which would
1810 ;; better be omitted
1811 (set-marker-insertion-type marker nil)
1812 (set-marker marker position))))
1813 ;;;_ > allout-decorate-item-span (item-widget)
1814 (defun allout-decorate-item-span (item-widget)
1815 "Equip the item with a span, as an entirety.
1817 This span is implemented so it can be used to detect displacement
1818 of the widget in absolute terms, and provides an offset bias for
1819 the various element spans."
1821 (if (and (widget-get item-widget :is-container)
1822 ;; the only case where the span could be empty.
1823 (eq (widget-get item-widget :from)
1824 (widget-get item-widget :to)))
1826 (allout-item-span item-widget
1827 (widget-get item-widget :from)
1828 (widget-get item-widget :to))))
1829 ;;;_ > allout-decorate-item-guides (item-widget
1830 ;;; &optional parent-widget has-successor)
1831 (defun allout-decorate-item-guides (item-widget
1832 &optional parent-widget has-successor)
1833 "Add ITEM-WIDGET guide icon-prefix descender and connector text properties.
1835 Optional arguments provide context for deriving the guides. In
1836 their absence, the current guide column flags are used.
1838 Optional PARENT-WIDGET is the widget for the item's parent item.
1840 Optional HAS-SUCCESSOR is true iff the item is followed by a sibling.
1842 We also hide the header-prefix string.
1844 Guides are established according to the item-widget's :guide-column-flags,
1845 when different than :was-guide-column-flags. Changing that property and
1846 reapplying this method will rectify the glyphs."
1848 (when (not (widget-get item-widget :is-container))
1849 (let* ((depth (widget-get item-widget :depth))
1850 (parent-depth (and parent-widget
1851 (widget-get parent-widget :depth)))
1852 (parent-flags (and parent-widget
1853 (widget-get parent-widget :guide-column-flags)))
1854 (parent-flags-depth (length parent-flags))
1855 (extender-length (- depth (+ parent-flags-depth 2)))
1856 (flags (or (and (> depth 1)
1857 parent-widget
1858 (widget-put item-widget :guide-column-flags
1859 (append (list has-successor)
1860 (if (< 0 extender-length)
1861 (make-list extender-length
1862 '-))
1863 parent-flags)))
1864 (widget-get item-widget :guide-column-flags)))
1865 (was-flags (widget-get item-widget :was-guide-column-flags))
1866 (guides-start (widget-get item-widget :from))
1867 (guides-end (widget-get item-widget :icon-start))
1868 (position guides-start)
1869 (increment (length allout-header-prefix))
1870 reverse-flags
1871 guide-name
1872 extenders paint-extenders
1873 (inhibit-read-only t))
1875 (when (not (equal was-flags flags))
1877 (setq reverse-flags (reverse flags))
1878 (while reverse-flags
1879 (setq guide-name
1880 (cond ((null (cdr reverse-flags))
1881 (if (car reverse-flags)
1882 'mid-connector
1883 'end-connector))
1884 ((eq (car reverse-flags) '-)
1885 ;; accumulate extenders tally, to be painted on next
1886 ;; non-extender flag, according to the flag type.
1887 (setq extenders (1+ (or extenders 0)))
1888 nil)
1889 ((car reverse-flags)
1890 'through-descender)
1891 (t 'skip-descender)))
1892 (when guide-name
1893 (put-text-property position (setq position (+ position increment))
1894 'display (allout-fetch-icon-image guide-name))
1895 (if (> increment 1) (setq increment 1))
1896 (when extenders
1897 ;; paint extenders after a connector, else leave spaces.
1898 (dotimes (i extenders)
1899 (put-text-property
1900 position (setq position (1+ position))
1901 'display (allout-fetch-icon-image
1902 (if (memq guide-name '(mid-connector end-connector))
1903 'extender-connector
1904 'skip-descender))))
1905 (setq extenders nil)))
1906 (setq reverse-flags (cdr reverse-flags)))
1907 (widget-put item-widget :was-guide-column-flags flags))
1909 (allout-item-element-span-is item-widget :guides-span
1910 guides-start guides-end))))
1911 ;;;_ > allout-decorate-item-icon (item-widget)
1912 (defun allout-decorate-item-icon (item-widget)
1913 "Add item icon glyph and distinctive bullet text properties to ITEM-WIDGET."
1915 (when (not (widget-get item-widget :is-container))
1916 (let* ((icon-start (widget-get item-widget :icon-start))
1917 (icon-end (widget-get item-widget :icon-end))
1918 (bullet (widget-get item-widget :bullet))
1919 (use-bullet bullet)
1920 (was-bullet (widget-get item-widget :was-bullet))
1921 (distinctive (allout-distinctive-bullet bullet))
1922 (distinctive-start (widget-get item-widget :distinctive-start))
1923 (distinctive-end (widget-get item-widget :distinctive-end))
1924 (does-encrypt (widget-get item-widget :does-encrypt))
1925 (is-encrypted (and does-encrypt (widget-get item-widget
1926 :is-encrypted)))
1927 (expanded (widget-get item-widget :expanded))
1928 (has-subitems (widget-get item-widget :has-subitems))
1929 (inhibit-read-only t)
1930 icon-state)
1932 (when (not (and (equal (widget-get item-widget :was-expanded) expanded)
1933 (equal (widget-get item-widget :was-has-subitems)
1934 has-subitems)
1935 (equal (widget-get item-widget :was-does-encrypt)
1936 does-encrypt)
1937 (equal (widget-get item-widget :was-is-encrypted)
1938 is-encrypted)))
1940 (setq icon-state
1941 (cond (does-encrypt (if is-encrypted
1942 'locked-encrypted
1943 'unlocked-encrypted))
1944 (expanded 'opened)
1945 (has-subitems 'closed)
1946 (t 'empty)))
1947 (put-text-property icon-start (1+ icon-start)
1948 'display (allout-fetch-icon-image icon-state))
1949 (widget-put item-widget :was-expanded expanded)
1950 (widget-put item-widget :was-has-subitems has-subitems)
1951 (widget-put item-widget :was-does-encrypt does-encrypt)
1952 (widget-put item-widget :was-is-encrypted is-encrypted)
1953 ;; preserve as a widget property to track last known:
1954 (widget-put item-widget :icon-state icon-state)
1955 ;; preserve as a text property to track undo:
1956 (put-text-property icon-start icon-end :icon-state icon-state))
1957 (allout-item-element-span-is item-widget :icon-span
1958 icon-start icon-end)
1959 (when (not (string= was-bullet bullet))
1960 (cond ((not distinctive)
1961 ;; XXX we strip the prior properties without even checking if
1962 ;; the prior bullet was distinctive, because the widget
1963 ;; provisions to convey that info is disappearing, sigh.
1964 (remove-text-properties icon-end (1+ icon-end) '(display))
1965 (setq distinctive-start icon-end distinctive-end icon-end)
1966 (widget-put item-widget :distinctive-start distinctive-start)
1967 (widget-put item-widget :distinctive-end distinctive-end))
1969 ((not (string= bullet allout-numbered-bullet))
1970 (setq distinctive-start icon-end distinctive-end (+ icon-end 1)))
1972 (does-encrypt
1973 (setq distinctive-start icon-end distinctive-end (+ icon-end 1)))
1976 (goto-char icon-end)
1977 (looking-at "[0-9]+")
1978 (setq use-bullet (buffer-substring icon-end (match-end 0)))
1979 (setq distinctive-start icon-end
1980 distinctive-end (match-end 0))))
1981 (put-text-property distinctive-start distinctive-end 'display
1982 use-bullet)
1983 (widget-put item-widget :was-bullet bullet)
1984 (widget-put item-widget :distinctive-start distinctive-start)
1985 (widget-put item-widget :distinctive-end distinctive-end)))))
1986 ;;;_ > allout-decorate-item-cue (item-widget)
1987 (defun allout-decorate-item-cue (item-widget)
1988 "Incorporate space between bullet icon and body to the ITEM-WIDGET."
1989 ;; NOTE: most of the cue-area
1991 (when (not (widget-get item-widget :is-container))
1992 (let* ((cue-start (or (widget-get item-widget :distinctive-end)
1993 (widget-get item-widget :icon-end)))
1994 (body-start (widget-get item-widget :body-start))
1995 (expanded (widget-get item-widget :expanded))
1996 (has-subitems (widget-get item-widget :has-subitems))
1997 (inhibit-read-only t))
1999 (allout-item-element-span-is item-widget :cue-span cue-start body-start)
2000 (put-text-property (1- body-start) body-start 'rear-nonsticky t))))
2001 ;;;_ > allout-decorate-item-body (item-widget &optional force)
2002 (defun allout-decorate-item-body (item-widget &optional force)
2003 "Incorporate item body text as part the ITEM-WIDGET.
2005 Optional FORCE means force reassignment of the region property."
2007 (let* ((allout-inhibit-body-modification-hook t)
2008 (body-start (widget-get item-widget :body-start))
2009 (body-end (widget-get item-widget :body-end))
2010 (body-text-end body-end)
2011 (inhibit-read-only t))
2013 (allout-item-element-span-is item-widget :body-span
2014 body-start (min (1+ body-end) (point-max))
2015 force)))
2016 ;;;_ > allout-item-actual-position (item-widget field)
2017 (defun allout-item-actual-position (item-widget field)
2018 "Return ITEM-WIDGET FIELD position taking item displacement into account."
2020 ;; The item's sub-element positions (:icon-end, :body-start, etc) are
2021 ;; accurate when the item is parsed, but some offsets from the start
2022 ;; drift with text added in the body.
2024 ;; Rather than reparse an item with every change (inefficient), or derive
2025 ;; every position from a distinct field marker/overlay (prohibitive as
2026 ;; the number of items grows), we use the displacement tracking of the
2027 ;; :span-overlay's markers, against the registered :from or :body-end
2028 ;; (depending on whether the requested field value is before or after the
2029 ;; item body), to bias the registered values.
2031 ;; This is not necessary/useful when the item is being decorated, because
2032 ;; that always must be preceded by a fresh item parse.
2034 (if (not (eq field :body-end))
2035 (widget-get item-widget :from)
2037 (let* ((span-overlay (widget-get item-widget :span-overlay))
2038 (body-end-position (widget-get item-widget :body-end))
2039 (ref-marker-position (and span-overlay
2040 (overlay-end span-overlay)))
2041 (offset (and body-end-position span-overlay
2042 (- (or ref-marker-position 0)
2043 body-end-position))))
2044 (+ (widget-get item-widget field) (or offset 0)))))
2045 ;;;_ : Item undecoration
2046 ;;;_ > allout-widgets-undecorate-region (start end)
2047 (defun allout-widgets-undecorate-region (start end)
2048 "Eliminate widgets and decorations for all items in region from START to END."
2049 (let ((next start)
2050 widget)
2051 (save-excursion
2052 (goto-char start)
2053 (while (< (setq next (next-single-char-property-change next
2054 'display
2055 (current-buffer)
2056 end))
2057 end)
2058 (goto-char next)
2059 (when (setq widget (allout-get-item-widget))
2060 ;; if the next-property/overly progression got us to a widget:
2061 (allout-widgets-undecorate-item widget t))))))
2062 ;;;_ > allout-widgets-undecorate-text (text)
2063 (defun allout-widgets-undecorate-text (text)
2064 "Eliminate widgets and decorations for all items in TEXT."
2065 (remove-text-properties 0 (length text)
2066 '(display nil :icon-state nil rear-nonsticky nil
2067 category nil button nil field nil)
2068 text)
2069 text)
2070 ;;;_ > allout-widgets-undecorate-item (item-widget &optional no-expose)
2071 (defun allout-widgets-undecorate-item (item-widget &optional no-expose)
2072 "Remove widget decorations from ITEM-WIDGET.
2074 Any concealed content head lines and item body is exposed, unless
2075 optional NO-EXPOSE is non-nil."
2076 (let ((from (widget-get item-widget :from))
2077 (to (widget-get item-widget :to))
2078 (text-properties-to-remove '(display nil
2079 :icon-state nil
2080 rear-nonsticky nil
2081 category nil
2082 button nil
2083 field nil))
2084 (span-overlay (widget-get item-widget :span-overlay))
2085 (button-overlay (widget-get item-widget :button))
2086 (was-modified (buffer-modified-p))
2087 (buffer-undo-list t)
2088 (inhibit-read-only t))
2089 (if (not no-expose)
2090 (allout-flag-region from to nil))
2091 (allout-unprotected
2092 (remove-text-properties from to text-properties-to-remove))
2093 (when span-overlay
2094 (delete-overlay span-overlay) (widget-put item-widget :span-overlay nil))
2095 (when button-overlay
2096 (delete-overlay button-overlay) (widget-put item-widget :button nil))
2097 (set-marker from nil)
2098 (set-marker to nil)
2099 (if (not was-modified)
2100 (set-buffer-modified-p nil))))
2102 ;;;_ : Item decoration support
2103 ;;;_ > allout-item-span (item-widget &optional start end)
2104 (defun allout-item-span (item-widget &optional start end)
2105 "Return or register the location of an ITEM-WIDGET's actual START and END.
2107 If START and END are not passed in, return either a dotted pair
2108 of the current span, if established, or nil if not yet set.
2110 When the START and END are passed, return the distance that the
2111 start of the item moved. We return 0 if the span was not
2112 previously established or is not moved."
2113 (let ((overlay (widget-get item-widget :span-overlay))
2114 was-start was-end
2115 changed)
2116 (cond ((not overlay) (when start
2117 (setq overlay (make-overlay start end nil t nil))
2118 (overlay-put overlay 'button item-widget)
2119 (overlay-put overlay 'evaporate t)
2120 (widget-put item-widget :span-overlay overlay)
2122 ;; report:
2123 ((not start) (cons (overlay-start overlay) (overlay-end overlay)))
2124 ;; move:
2125 ((or (not (equal (overlay-start overlay) start))
2126 (not (equal (overlay-end overlay) end)))
2127 (move-overlay overlay start end)
2129 ;; specified span already set:
2130 (t nil))))
2131 ;;;_ > allout-item-element-span-is (item-widget element
2132 ;;; &optional start end force)
2133 (defun allout-item-element-span-is (item-widget element
2134 &optional start end force)
2135 "Return or register the location of the indicated ITEM-WIDGET ELEMENT.
2137 ELEMENT is one of :guides-span, :icon-span, :cue-span, or :body-span.
2139 When optional START is specified, optional END must also be.
2141 START and END are the actual bounds of the region, if provided.
2143 If START and END are not passed in, we return either a dotted
2144 pair of the current span, if established, or nil if not yet set.
2146 When the START and END are passed, we return t if the region
2147 changed or nil if not.
2149 Optional FORCE means force assignment of the region's text
2150 property, even if it's already set."
2151 (let ((span (widget-get item-widget element)))
2152 (cond ((or (not span) force)
2153 (when start
2154 (widget-put item-widget element (cons start end))
2155 (put-text-property start end 'category
2156 (cdr (assoc element
2157 allout-span-to-category)))
2159 ;; report:
2160 ((not start) span)
2161 ;; move if necessary:
2162 ((not (and (eq (car span) start)
2163 (eq (cdr span) end)))
2164 (widget-put item-widget element span)
2166 ;; specified span already set:
2167 (t nil))))
2168 ;;;_ : Item widget retrieval (/ high-level creation):
2169 ;;;_ > allout-get-item-widget (&optional container)
2170 (defun allout-get-item-widget (&optional container)
2171 "Return the widget for the item at point, or nil if no widget yet exists.
2173 Point must be situated *before* the start of the target item's
2174 body, so we don't get an existing containing item when we're in
2175 the process of creating an item in the middle of another.
2177 Optional CONTAINER is used to obtain the container item."
2178 (if (or container (zerop (allout-depth)))
2179 allout-container-item-widget
2180 ;; allout-recent-* are calibrated by (allout-depth) if we got here.
2181 (let ((got (widget-at allout-recent-prefix-beginning)))
2182 (if (and got (listp got))
2183 (if (marker-position (widget-get got :from))
2184 (and
2185 (>= (point) (widget-apply got :actual-position :from))
2186 (<= (point) (widget-apply got :actual-position :body-start))
2187 got)
2188 ;; a wacky residual item - undecorate and disregard:
2189 (allout-widgets-undecorate-item got)
2190 nil)))))
2191 ;;;_ > allout-get-or-create-item-widget (&optional redecorate blank-container)
2192 (defun allout-get-or-create-item-widget (&optional redecorate blank-container)
2193 "Return a widget for the item at point, creating the widget if necessary.
2195 When creating a widget, we assume there has been a context change
2196 and decorate its siblings and parent, as well.
2198 Optional BLANK-CONTAINER is for internal use, to fabricate a
2199 meta-container item with an empty body when the first proper
2200 \(non-container\) item starts at the beginning of the file.
2202 Optional REDECORATE, if non-nil, means to redecorate the widget
2203 if it already exists."
2204 (let ((widget (allout-get-item-widget blank-container))
2205 (buffer-undo-list t))
2206 (cond (widget (if redecorate
2207 (allout-redecorate-item widget))
2208 widget)
2209 ((or blank-container (zerop (allout-depth)))
2210 (or allout-container-item-widget
2211 (setq allout-container-item-widget
2212 (allout-decorate-item-and-context
2213 (widget-convert 'allout-item-widget)
2214 nil blank-container))))
2215 ;; create a widget for a regular/non-container item:
2216 (t (allout-decorate-item-and-context (widget-convert
2217 'allout-item-widget))))))
2218 ;;;_ > allout-get-or-create-parent-widget (&optional redecorate)
2219 (defun allout-get-or-create-parent-widget (&optional redecorate)
2220 "Return widget for parent of item at point, decorating it if necessary.
2222 We return the container widget if we're above the first proper item in the
2223 file.
2225 Optional REDECORATE, if non-nil, means to redecorate the widget if it
2226 already exists.
2228 Point will wind up positioned on the beginning of the parent or beginning
2229 of the buffer."
2230 ;; use existing widget, if there, else establish it
2231 (if (or (bobp) (and (not (allout-ascend))
2232 (looking-at allout-regexp)))
2233 (allout-get-or-create-item-widget redecorate 'blank-container)
2234 (allout-get-or-create-item-widget redecorate)))
2235 ;;;_ : X- Item ancillaries
2236 ;;;_ >X allout-body-modification-handler (beg end)
2237 (defun allout-body-modification-handler (beg end)
2238 "Do routine processing of body text before and after modification.
2240 Operation is inhibited by `allout-inhibit-body-modification-handler'."
2242 ;; The primary duties are:
2244 ;; - marking of escaped prefix-like text for delayed cleanup of escapes
2245 ;; - removal and replacement of the settings
2246 ;; - maintenance of beginning-of-line guide lines
2248 ;; ?? Escapes removal \(before changes\) is not done when edits span multiple
2249 ;; items, recognizing that item structure is being preserved, including
2250 ;; escaping of item-prefix-like text within bodies. See
2251 ;; `allout-before-modification-handler' and
2252 ;; `allout-inhibit-body-modification-handler'.
2254 ;; Adds the overlay to the `allout-unresolved-body-mod-workhash' during
2255 ;; before-change operation, and removes from that list during after-change
2256 ;; operation.
2257 (cond (allout-inhibit-body-modification-hook nil)))
2258 ;;;_ >X allout-graphics-modification-handler (beg end)
2259 (defun allout-graphics-modification-handler (beg end)
2260 "Protect against incoherent deletion of decoration graphics.
2262 Deletes allowed only when inhibit-read-only is t."
2263 (cond
2264 (undo-in-progress (when (eq (get-text-property beg 'category)
2265 'allout-icon-span-category)
2266 (save-excursion
2267 (goto-char beg)
2268 (let* ((item-widget (allout-get-item-widget)))
2269 (if item-widget
2270 (allout-widgets-exposure-undo-recorder
2271 item-widget))))))
2272 (inhibit-read-only t)
2273 ((not (and (boundp 'allout-mode) allout-mode)) t)
2274 ((equal this-command 'quoted-insert) t)
2275 ((yes-or-no-p "Unruly edit of outline structure - allow? ")
2276 (setq allout-widgets-unset-inhibit-read-only (not inhibit-read-only)
2277 inhibit-read-only t))
2278 (t (error
2279 (substitute-command-keys allout-structure-unruly-deletion-message)))))
2280 ;;;_ > allout-item-icon-key-handler ()
2281 (defun allout-item-icon-key-handler ()
2282 "Catchall handling of key bindings in item icon/cue hot-spots.
2284 Applies `allout-hotspot-key-handler' and calls the result, if any, as an
2285 interactive command."
2287 (interactive)
2288 (let* ((mapped-binding (allout-hotspot-key-handler)))
2289 (when mapped-binding
2290 (call-interactively mapped-binding))))
2292 ;;;_ : Status
2293 ;;;_ . allout-item-location (item-widget)
2294 (defun allout-item-location (item-widget)
2295 "Location of the start of the item's text."
2296 (overlay-start (widget-get item-widget :span-overlay)))
2298 ;;;_ : Icon management
2299 ;;;_ > allout-fetch-icon-image (name)
2300 (defun allout-fetch-icon-image (name)
2301 "Fetch allout icon for symbol NAME.
2303 We use a caching strategy, so the caller doesn't need to do so."
2304 (let* ((types allout-widgets-icon-types)
2305 (use-dir (if (equal (allout-frame-property nil 'background-mode)
2306 'light)
2307 allout-widgets-icons-light-subdir
2308 allout-widgets-icons-dark-subdir))
2309 (key (list name use-dir))
2310 (got (assoc key allout-widgets-icons-cache)))
2311 (if got
2312 ;; display system shows only first of subsequent adjacent
2313 ;; `eq'-identical repeats - use copies to avoid this problem.
2314 (allout-widgets-copy-list (cadr got))
2315 (while (and types (not got))
2316 (setq got
2317 (allout-find-image
2318 (list (append (list :type (car types)
2319 :file (concat use-dir
2320 (symbol-name name)
2321 "." (symbol-name
2322 (car types))))
2323 (if (featurep 'xemacs)
2324 allout-widgets-item-image-properties-xemacs
2325 allout-widgets-item-image-properties-emacs)
2326 ))))
2327 (setq types (cdr types)))
2328 (if got
2329 (push (list key got) allout-widgets-icons-cache))
2330 got)))
2332 ;;;_ : Miscellaneous
2333 ;;;_ > allout-elapsed-time-seconds (triple)
2334 (defun allout-elapsed-time-seconds (end start)
2335 "Return seconds between `current-time' style time START/END triples."
2336 (let ((elapsed (time-subtract end start)))
2337 (float-time elapsed)))
2338 ;;;_ > allout-frame-property (frame property)
2339 (defalias 'allout-frame-property
2340 (cond ((fboundp 'frame-parameter)
2341 'frame-parameter)
2342 ((fboundp 'frame-property)
2343 'frame-property)
2344 (t nil)))
2345 ;;;_ > allout-find-image (specs)
2346 (defalias 'allout-find-image
2347 (if (fboundp 'find-image)
2348 'find-image
2349 nil) ; aka, not-yet-implemented for xemacs.
2351 ;;;_ > allout-widgets-copy-list (list)
2352 (defun allout-widgets-copy-list (list)
2353 ;; duplicated from cl.el 'copy-list' as of 2008-08-17
2354 "Return a copy of LIST, which may be a dotted list.
2355 The elements of LIST are not copied, just the list structure itself."
2356 (if (consp list)
2357 (let ((res nil))
2358 (while (consp list) (push (pop list) res))
2359 (prog1 (nreverse res) (setcdr res list)))
2360 (car list)))
2361 ;;;_ . allout-widgets-count-buttons-in-region (start end)
2362 (defun allout-widgets-count-buttons-in-region (start end)
2363 "Debugging/diagnostic tool - count overlays with 'button' property in region."
2364 (interactive "r")
2365 (setq start (or start (point-min))
2366 end (or end (point-max)))
2367 (if (> start end) (let ((interim start)) (setq start end end interim)))
2368 (let ((button-overlays (delq nil
2369 (mapcar (function (lambda (o)
2370 (if (overlay-get o 'button)
2371 o)))
2372 (overlays-in start end)))))
2373 (length button-overlays)))
2375 ;;;_ : Run unit tests:
2376 (defun allout-widgets-run-unit-tests ()
2377 (message "Running allout-widget tests...")
2379 (allout-test-range-overlaps)
2381 (message "Running allout-widget tests... Done.")
2382 (sit-for .5))
2384 (when allout-widgets-run-unit-tests-on-load
2385 (allout-widgets-run-unit-tests))
2387 ;;;_ : provide
2388 (provide 'allout-widgets)
2390 ;;;_. Local emacs vars.
2391 ;;;_ , Local variables:
2392 ;;;_ , allout-layout: (-1 : 0)
2393 ;;;_ , End: