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