Spelling fixes.
[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
06e21633
CY
489 "Toggle Allout Widgets mode.
490With a prefix argument ARG, enable Allout Widgets mode if ARG is
491positive, and disable it otherwise. If called from Lisp, enable
492the mode if ARG is omitted or nil.
493
494Allout Widgets mode is an extension of Allout mode that provides
495graphical decoration of outline structure. It is meant to
496operate along with `allout-mode', via `allout-mode-hook'.
aac7a935
KM
497
498The graphics include:
499
500- guide lines connecting item bullet-icons with those of their subitems.
501
502- icons for item bullets, varying to indicate whether or not the item
503 has subitems, and if so, whether or not the item is expanded.
504
505- cue area between the bullet-icon and the start of the body headline,
506 for item numbering, encryption indicator, and distinctive bullets.
507
508The bullet-icon and guide line graphics provide keybindings and mouse
509bindings for easy outline navigation and exposure control, extending
510outline hot-spot navigation \(see `allout-mode')."
511
512 :lighter nil
513 :keymap nil
514
515 ;; define-minor-mode handles any provided argument according to emacs
516 ;; minor-mode conventions - '(elisp) Minor Mode Conventions' - and sets
517 ;; allout-widgets-mode accordingly *before* running the body code, so we
518 ;; cue on that.
519 (if allout-widgets-mode
520 ;; Activating:
521 (progn
522 (allout-add-resumptions
523 ;; XXX user may need say in line-truncation/hscrolling - an option
524 ;; that abstracts mode.
525 ;; truncate text lines to keep guide lines intact:
526 '(truncate-lines t)
527 ;; and enable autoscrolling to ease view of text
528 '(auto-hscroll-mode t)
529 '(line-move-ignore-fields t)
530 '(widget-push-button-prefix "")
531 '(widget-push-button-suffix "")
532 ;; allout-escaped-prefix-regexp depends on allout-regexp:
533 (list 'allout-escaped-prefix-regexp (concat "\\(\\\\\\)"
534 "\\(" allout-regexp "\\)")))
535 (allout-add-resumptions
536 (list 'allout-widgets-tally allout-widgets-tally)
537 (list 'allout-widgets-escapes-sanitization-regexp-pair
538 (list (concat "\\(\n\\|\\`\\)"
539 allout-escaped-prefix-regexp
540 )
541 ;; Include everything but the escape symbol.
542 "\\1\\3"))
543 )
544
545 (add-hook 'after-change-functions 'allout-widgets-after-change-handler
546 nil t)
547
548 (allout-setup-text-properties)
549 (add-to-invisibility-spec '(allout-torso . t))
550 (add-to-invisibility-spec 'allout-escapes)
551
552 (if (current-local-map)
553 (set-keymap-parent allout-item-body-keymap (current-local-map)))
554
555 (add-hook 'allout-exposure-change-hook
556 'allout-widgets-exposure-change-recorder nil 'local)
557 (add-hook 'allout-structure-added-hook
558 'allout-widgets-additions-recorder nil 'local)
559 (add-hook 'allout-structure-deleted-hook
560 'allout-widgets-deletions-recorder nil 'local)
561 (add-hook 'allout-structure-shifted-hook
562 'allout-widgets-shifts-recorder nil 'local)
563 (add-hook 'allout-after-copy-or-kill-hook
564 'allout-widgets-after-copy-or-kill-function nil 'local)
919d884a
KM
565 (add-hook 'allout-post-undo-hook
566 'allout-widgets-after-undo-function nil 'local)
aac7a935
KM
567
568 (add-hook 'before-change-functions 'allout-widgets-before-change-handler
569 nil 'local)
570 (add-hook 'post-command-hook 'allout-widgets-post-command-business
571 nil 'local)
572 (add-hook 'pre-command-hook 'allout-widgets-pre-command-business
573 nil 'local)
574
575 ;; init the widgets tally for debugging:
576 (if (not allout-widgets-tally)
577 (setq allout-widgets-tally (make-hash-table
578 :test 'eq :weakness 'key)))
579 ;; add tally count display on minor-mode-alist just after
580 ;; allout-mode entry.
581 ;; (we use ternary condition form to keep condition simple for deletion.)
582 (let* ((mode-line-entry '(allout-widgets-mode-inhibit ""
583 (:eval (allout-widgets-tally-string))))
584 (associated (assoc (car mode-line-entry) minor-mode-alist))
585 ;; need location for it only if not already present:
586 (after (and (not associated)
587 (memq (assq 'allout-mode minor-mode-alist) minor-mode-alist))))
588 (if after
589 (rplacd after (cons mode-line-entry (cdr after)))))
590 (allout-widgets-prepopulate-buffer)
591 t)
592 ;; Deactivating:
593 (let ((inhibit-read-only t)
594 (was-modified (buffer-modified-p)))
595
596 (allout-widgets-undecorate-region (point-min)(point-max))
597 (remove-from-invisibility-spec '(allout-torso . t))
598 (remove-from-invisibility-spec 'allout-escapes)
599
600 (remove-hook 'after-change-functions
601 'allout-widgets-after-change-handler 'local)
602 (remove-hook 'allout-exposure-change-hook
603 'allout-widgets-exposure-change-recorder 'local)
604 (remove-hook 'allout-structure-added-hook
605 'allout-widgets-additions-recorder 'local)
606 (remove-hook 'allout-structure-deleted-hook
607 'allout-widgets-deletions-recorder 'local)
608 (remove-hook 'allout-structure-shifted-hook
609 'allout-widgets-shifts-recorder 'local)
610 (remove-hook 'allout-after-copy-or-kill-hook
611 'allout-widgets-after-copy-or-kill-function 'local)
612 (remove-hook 'before-change-functions
613 'allout-widgets-before-change-handler 'local)
614 (remove-hook 'post-command-hook
615 'allout-widgets-post-command-business 'local)
616 (remove-hook 'pre-command-hook
617 'allout-widgets-pre-command-business 'local)
618 (assq-delete-all 'allout-widgets-mode-inhibit minor-mode-alist)
619 (set-buffer-modified-p was-modified))))
620;;;_ > allout-widgets-mode-off
621(defun allout-widgets-mode-off ()
622 "Explicitly disable allout-widgets-mode."
623 (allout-widgets-mode -1))
624;;;_ > allout-widgets-mode-off
625(defun allout-widgets-mode-on ()
626 "Explicitly disable allout-widgets-mode."
627 (allout-widgets-mode 1))
628;;;_ > allout-setup-text-properties ()
629(defun allout-setup-text-properties ()
630 "Configure category and literal text properties."
631
632 ;; XXX body - before-change, entry, keymap
633
634 (setplist 'allout-guides-span-category nil)
635 (put 'allout-guides-span-category
636 'modification-hooks '(allout-graphics-modification-handler))
637 (put 'allout-guides-span-category 'local-map allout-item-icon-keymap)
638 (put 'allout-guides-span-category 'mouse-face widget-button-face)
639 (put 'allout-guides-span-category 'field 'structure)
640;; (put 'allout-guides-span-category 'face 'widget-button)
641
642 (setplist 'allout-icon-span-category
643 (allout-widgets-copy-list (symbol-plist
644 'allout-guides-span-category)))
645 (put 'allout-icon-span-category 'field 'structure)
646
647 ;; XXX for body text we're instead going to use the buffer-wide
648 ;; resources, like before/after-change-functions hooks and the
649 ;; buffer's key map. that way we won't have to do painful provisions
650 ;; to fixup things after edits, catch outlier interstitial
651 ;; characters, like newline and empty lines after hidden subitems,
652 ;; etc.
653 (setplist 'allout-body-span-category nil)
654 (put 'allout-body-span-category 'evaporate t)
655 (put 'allout-body-span-category 'local-map allout-item-body-keymap)
656 ;;(put 'allout-body-span-category
657 ;; 'modification-hooks '(allout-body-modification-handler))
658 ;;(put 'allout-body-span-category 'field 'body)
659
660 (setplist 'allout-cue-span-category nil)
661 (put 'allout-cue-span-category 'evaporate t)
662 (put 'allout-cue-span-category
663 'modification-hooks '(allout-body-modification-handler))
664 (put 'allout-cue-span-category 'local-map allout-cue-span-keymap)
665 (put 'allout-cue-span-category 'mouse-face widget-button-face)
666 (put 'allout-cue-span-category 'pointer 'arrow)
667 (put 'allout-cue-span-category 'field 'structure)
668
669 (setplist 'allout-trailing-category nil)
670 (put 'allout-trailing-category 'evaporate t)
671 (put 'allout-trailing-category 'local-map allout-item-body-keymap)
672
673 (setplist 'allout-escapes-category nil)
674 (put 'allout-escapes-category 'invisible 'allout-escapes)
675 (put 'allout-escapes-category 'evaporate t))
676;;;_ > allout-widgets-prepopulate-buffer ()
677(defun allout-widgets-prepopulate-buffer ()
678 "Step over the current buffers exposed items to do initial widgetizing."
679 (if (not allout-widgets-mode-inhibit)
680 (save-excursion
681 (goto-char (point-min))
682 (while (allout-next-visible-heading 1)
683 (when (not (widget-at (point)))
684 (allout-get-or-create-item-widget))))))
685;;;_ . settings context
686;;;_ = allout-container-item
687(defvar allout-container-item-widget nil
688 "A widget for the current outline's overarching container as an item.
689
690The item has settings \(of the file/connection\) and maybe a body, but no
691icon/bullet.")
692(make-variable-buffer-local 'allout-container-item-widget)
693;;;_ . Hooks and hook helpers
694;;;_ , major command-loop business:
695;;;_ > allout-widgets-pre-command-business (&optional recursing)
696(defun allout-widgets-pre-command-business (&optional recursing)
697 "Handle actions pending before allout-mode activity."
698)
699;;;_ > allout-widgets-post-command-business (&optional recursing)
700(defun allout-widgets-post-command-business (&optional recursing)
701 "Handle actions pending after any allout-mode commands.
702
703Optional RECURSING is for internal use, to limit recursion."
704 ;; - check changed text for nesting discontinuities and escape anything
705 ;; that's: (1) asterisks at bol or (2) excessively nested.
706 (condition-case failure
707
708 (when (and (boundp 'allout-mode) allout-mode)
709
710 (if allout-widgets-unset-inhibit-read-only
711 (setq inhibit-read-only nil
712 allout-widgets-unset-inhibit-read-only nil))
713
714 (when allout-widgets-reenable-before-change-handler
715 (add-hook 'before-change-functions
716 'allout-widgets-before-change-handler
717 nil 'local)
718 (setq allout-widgets-reenable-before-change-handler nil))
719
720 (when (or allout-widgets-undo-exposure-record
721 allout-widgets-changes-record)
722 (let* ((debug-on-signal t)
723 (debug-on-error t)
724 ;; inhibit recording new undo records when processing
725 ;; effects of undo-exposure:
726 (debugger 'allout-widgets-hook-error-handler)
727 (adjusting-message " Adjusting widgets...")
728 (replaced-message (allout-widgets-adjusting-message
729 adjusting-message))
730 (start-time (current-time)))
731
732 (if allout-widgets-undo-exposure-record
733 ;; inhibit undo recording iff undoing exposure stuff.
734 ;; XXX we might need to inhibit per respective
735 ;; change-record, rather than assuming that some undo
736 ;; activity during a command is all undo activity.
737 (let ((buffer-undo-list t))
738 (allout-widgets-exposure-undo-processor)
739 (allout-widgets-changes-dispatcher))
740 (allout-widgets-exposure-undo-processor)
741 (allout-widgets-changes-dispatcher))
742
743 (if allout-widgets-time-decoration-activity
744 (setq allout-widgets-last-decoration-timing
745 (list (allout-elapsed-time-seconds (current-time)
746 start-time)
747 allout-widgets-changes-record)))
748
749 (setq allout-widgets-changes-record nil)
750
751 (if replaced-message
752 (if (stringp replaced-message)
753 (message replaced-message)
754 (message "")))))
755
6b5ccddf
KM
756 ;; alas, decorated intermediate matches are not easily undecorated
757 ;; when they're automatically rehidden by isearch, so we're
758 ;; dropping this nicety.
759 ;; ;; Detect undecorated items, eg during isearch into previously
760 ;; ;; unexposed topics, and decorate "economically". Some
761 ;; ;; undecorated stuff is often exposed, to reduce lag, but the
762 ;; ;; item containing the cursor is decorated. We constrain
763 ;; ;; recursion to avoid being trapped by unexpectedly undecoratable
764 ;; ;; items.
765 ;; (when (and (not recursing)
766 ;; (not (allout-current-decorated-p))
767 ;; (or (not (equal (allout-depth) 0))
768 ;; (not allout-container-item-widget)))
769 ;; (let ((buffer-undo-list t))
770 ;; (allout-widgets-exposure-change-recorder
771 ;; allout-recent-prefix-beginning allout-recent-prefix-end nil)
772 ;; (allout-widgets-post-command-business 'recursing)))
aac7a935
KM
773
774 ;; Detect and rectify fouled outline structure - decorated item
775 ;; not at beginning of line.
776 (let ((this-widget (or (widget-at (point))
777 ;; XXX we really should be checking across
778 ;; edited span, not just point and point+1
779 (and (not (eq (point) (point-max)))
780 (widget-at (1+ (point))))))
781 inserted-at)
782 (save-excursion
783 (if (and this-widget
784 (goto-char (widget-get this-widget :from))
785 (not (bolp)))
786 (if (not
787 (condition-case err
788 (yes-or-no-p
789 (concat "Misplaced item won't be recognizable "
790 " as part of outline - rectify? "))
791 (quit nil)))
792 (progn
793 (if (allout-hidden-p (max (1- (point)) 1))
794 (save-excursion
795 (goto-char (max (1- (point)) 1))
796 (allout-show-to-offshoot)))
797 (allout-widgets-undecorate-item this-widget))
798 ;; expose any hidden intervening items, so resulting
799 ;; position is clear:
800 (setq inserted-at (point))
801 (allout-unprotected (insert-before-markers "\n"))
802 (forward-char -1)
803 ;; ensure the inserted newline is visible:
804 (allout-flag-region inserted-at (1+ inserted-at) nil)
805 (allout-widgets-post-command-business 'recursing)
806 (message (concat "outline structure corrected - item"
807 " moved to beginning of new line"))
808 ;; preserve cursor position in some cases:
809 (if (and inserted-at
810 (> (point) inserted-at))
811 (forward-char -1)))))))
812
813 (error
814 ;; zero work list so we don't get stuck futily retrying.
815 ;; error recording done by allout-widgets-hook-error-handler.
816 (setq allout-widgets-changes-record nil))))
817;;;_ , major change handlers:
818;;;_ > allout-widgets-before-change-handler
819(defun allout-widgets-before-change-handler (beg end)
820 "Business to be done before changes in a widgetized allout outline."
821 ;; protect against unruly edits to structure:
822 (cond
823 (undo-in-progress (when (eq (get-text-property beg 'category)
824 'allout-icon-span-category)
825 (save-excursion
826 (goto-char beg)
827 (let* ((item-widget (allout-get-item-widget)))
828 (if item-widget
829 (allout-widgets-exposure-undo-recorder
830 item-widget))))))
831 (inhibit-read-only t)
832 ((not (and (boundp 'allout-mode) allout-mode)) t)
833 ((equal this-command 'quoted-insert) t)
834 ((not (text-property-any beg (if (equal end beg)
835 (min (1+ beg) (point-max))
836 end)
837 'field 'structure))
838 t)
839 ((yes-or-no-p "Unruly edit of outline structure - allow? ")
840 (setq allout-widgets-unset-inhibit-read-only (not inhibit-read-only)
841 inhibit-read-only t))
842 (t
843 ;; tell the allout-widgets-post-command-business to reestablish the hook:
844 (setq allout-widgets-reenable-before-change-handler t)
845 ;; and raise an error to prevent the edit (and disable the hook):
846 (error
847 (substitute-command-keys allout-structure-unruly-deletion-message)))))
848;;;_ > allout-widgets-after-change-handler
849(defun allout-widgets-after-change-handler (beg end prelength)
850 "Reconcile what needs to be reconciled for allout widgets after edits."
851 )
852;;;_ > allout-current-decorated-p ()
853(defun allout-current-decorated-p ()
854 "True if the current item is not decorated"
855 (save-excursion
856 (if (allout-back-to-current-heading)
857 (if (> allout-recent-depth 0)
858 (and (allout-get-item-widget) t)
859 allout-container-item-widget))))
860
861;;;_ > allout-widgets-hook-error-handler
862(defun allout-widgets-hook-error-handler (mode args)
863 "Process errors which occurred in the course of command hook operation.
864
865We store a backtrace of the error information in the variable,
866`allout-widgets-last-hook-error', unset the error handlers, and
867reraise the error, so that processing continues to the
868encompassing condition-case."
869 ;; first deconstruct special error environment so errors here propagate
870 ;; to encompassing condition-case:
871 (setq debugger 'debug
872 debug-on-error nil
873 debug-on-signal nil)
874 (let* ((bt (with-output-to-string (backtrace)))
875 (this "allout-widgets-hook-error-handler")
876 (header
877 (format "allout-widgets-last-hook-error stored, %s/%s %s %s"
878 this mode args
879 (format-time-string "%e-%b-%Y %r" (current-time)))))
880 ;; post to *Messages* then immediately replace with more compact notice:
881 (message "%s" (setq allout-widgets-last-hook-error
882 (format "%s:\n%s" header bt)))
883 (message header) (sit-for allout-widgets-hook-error-post-time)
884 ;; reraise the error, or one concerning this function if unexpected:
885 (if (equal mode 'error)
886 (apply 'signal args)
887 (error "%s: unexpected mode, %s %s" this mode args))))
888;;;_ > allout-widgets-changes-exceed-threshold-p ()
889(defun allout-widgets-adjusting-message (message)
890 "Post MESSAGE when pending are likely to make a big enough delay.
891
892If posting of the MESSAGE is warranted and there already is a
893`current-message' in the minibuffer, the MESSAGE is appended to
894the current one, and the previously pending `current-message' is
895returned for later posting on completion.
896
897If posting of the MESSAGE is warranted, but no `current-message'
898is pending, then t is returned to indicate that case.
899
900If posting of the MESSAGE is not warranted, then nil is returned.
901
902See `allout-widgets-adjust-message-length-threshold',
903`allout-widgets-adjust-message-size-threshold' for message
904posting threshold criteria."
905 (if (or (> (length allout-widgets-changes-record)
906 allout-widgets-adjust-message-length-threshold)
907 ;; for size, use distance from start of first to end of last:
908 (let ((min (point-max))
909 (max 0)
910 first second)
911 (mapc (function (lambda (entry)
912 (if (eq :undone-exposure (car entry))
913 nil
914 (setq first (cadr entry)
915 second (caddr entry))
916 (if (< (min first second) min)
917 (setq min (min first second)))
918 (if (> (max first second) max)
919 (setq max (max first second))))))
920 allout-widgets-changes-record)
921 (> (- max min) allout-widgets-adjust-message-size-threshold)))
922 (let ((prior (current-message)))
923 (message (if prior (concat prior " - " message) message))
924 (or prior t))))
925;;;_ > allout-widgets-changes-dispatcher ()
926(defun allout-widgets-changes-dispatcher ()
927 "Dispatch CHANGES-RECORD items to respective widgets change processors."
928
929 (if (not allout-widgets-mode-inhibit)
930 (let* ((changes-record allout-widgets-changes-record)
931 (changes-pending (and changes-record t))
932 entry
933 exposures
934 additions
935 deletions
936 shifts)
937
938 (when changes-pending
939 (while changes-record
940 (setq entry (pop changes-record))
941 (case (car entry)
942 (:exposed (push entry exposures))
943 (:added (push entry additions))
944 (:deleted (push entry deletions))
945 (:shifted (push entry shifts))))
946
947 (if exposures
948 (allout-widgets-exposure-change-processor exposures))
949 (if additions
950 (allout-widgets-additions-processor additions))
951 (if deletions
952 (allout-widgets-deletions-processor deletions))
953 (if shifts
954 (allout-widgets-shifts-processor shifts))))
955 (when (not (equal allout-widgets-mode-inhibit 'undecorated))
956 (allout-widgets-undecorate-region (point-min)(point-max))
957 (setq allout-widgets-mode-inhibit 'undecorated))))
958;;;_ > allout-widgets-exposure-change-recorder (from to flag)
959(defun allout-widgets-exposure-change-recorder (from to flag)
960 "Record allout exposure changes for tracking during post-command processing.
961
962Records changes in `allout-widgets-changes-record'."
963 (push (list :exposed from to flag) allout-widgets-changes-record))
964;;;_ > allout-widgets-exposure-change-processor (changes)
965(defun allout-widgets-exposure-change-processor (changes)
966 "Widgetize and adjust item widgets tracking allout outline exposure changes.
967
968Generally invoked via `allout-exposure-change-hook'."
969
970 (let ((changes (sort changes (function (lambda (this next)
971 (< (cadr this) (cadr next))))))
972 ;; have to distinguish between concealing and exposing so that, eg,
973 ;; `allout-expose-topic's mix is handled properly.
974 handled-expose
975 handled-conceal
976 covered
977 deactivate-mark)
978
979 (dolist (change changes)
980 (let (handling
981 (from (cadr change))
982 bucket got
983 (to (caddr change))
984 (flag (cadddr change))
985 parent)
986
987 ;; swap from and to:
988 (if (< to from) (setq bucket to
989 to from
990 from bucket))
991
992 ;; have we already handled exposure changes in this region?
993 (setq handling (if flag 'handled-conceal 'handled-expose)
994 got (allout-range-overlaps from to (symbol-value handling))
995 covered (car got))
996 (set handling (cadr got))
997
998 (when (not covered)
999 (save-excursion
1000 (goto-char from)
1001 (cond
1002
1003 ;; collapsing:
1004 (flag
1005 (allout-widgets-undecorate-region from to)
1006 (allout-beginning-of-current-line)
1007 (let ((widget (allout-get-item-widget)))
1008 (if (not widget)
1009 (allout-get-or-create-item-widget)
1010 (widget-apply widget :redecorate))))
1011
1012 ;; expanding:
1013 (t
1014 (while (< (point) to)
1015 (allout-beginning-of-current-line)
1016 (setq parent (allout-get-item-widget))
1017 (if (not parent)
1018 (setq parent (allout-get-or-create-item-widget))
1019 (widget-apply parent :redecorate))
1020 (allout-next-visible-heading 1)
1021 (if (widget-get parent :has-subitems)
1022 (allout-redecorate-visible-subtree parent))
1023 (if (> (point) to)
1024 ;; subtree may be well beyond to - incorporate in ranges:
1025 (setq handled-expose
1026 (allout-range-overlaps from (point) handled-expose)
1027 covered (car handled-expose)
1028 handled-expose (cadr handled-expose)))
1029 (allout-next-visible-heading 1))))))))))
1030
1031;;;_ > allout-widgets-additions-recorder (from to)
1032(defun allout-widgets-additions-recorder (from to)
1033 "Record allout item additions for tracking during post-command processing.
1034
1035Intended for use on `allout-structure-added-hook'.
1036
1037FROM point at the start of the first new item and TO is point at the start
1038of the last one.
1039
1040Records changes in `allout-widgets-changes-record'."
1041 (push (list :added from to) allout-widgets-changes-record))
1042;;;_ > allout-widgets-additions-processor (changes)
1043(defun allout-widgets-additions-processor (changes)
1044 "Widgetize and adjust items tracking allout outline structure additions.
1045
1046Dispatched by `allout-widgets-post-command-business' in response to
1047:added entries recorded by `allout-widgets-additions-recorder'."
1048 (save-excursion
1049 (let (handled
1050 covered)
1051 (dolist (change changes)
1052 (let ((from (cadr change))
1053 bucket
1054 (to (caddr change)))
1055 (if (< to from) (setq bucket to to from from bucket))
1056 ;; have we already handled exposure changes in this region?
1057 (setq handled (allout-range-overlaps from to handled)
1058 covered (car handled)
1059 handled (cadr handled))
1060 (when (not covered)
1061 (goto-char from)
1062 ;; Prior sibling and parent can both be affected.
1063 (if (allout-ascend)
1064 (allout-redecorate-visible-subtree
1065 (allout-get-or-create-item-widget 'redecorate)))
1066 (if (< (point) from)
1067 (goto-char from))
1068 (while (and (< (point) to) (not (eobp)))
1069 (allout-beginning-of-current-line)
1070 (allout-redecorate-visible-subtree
1071 (allout-get-or-create-item-widget))
1072 (allout-next-visible-heading 1))
1073 (if (> (point) to)
1074 ;; subtree may be well beyond to - incorporate in ranges:
1075 (setq handled (allout-range-overlaps from (point) handled)
1076 covered (car handled)
1077 handled (cadr handled)))))))))
1078
1079;;;_ > allout-widgets-deletions-recorder (depth from)
1080(defun allout-widgets-deletions-recorder (depth from)
1081 "Record allout item deletions for tracking during post-command processing.
1082
1083Intended for use on `allout-structure-deleted-hook'.
1084
1085DEPTH is the depth of the deleted subtree, and FROM is the point from which
1086the subtree was deleted.
1087
1088Records changes in `allout-widgets-changes-record'."
1089 (push (list :deleted depth from) allout-widgets-changes-record))
1090;;;_ > allout-widgets-deletions-processor (changes)
1091(defun allout-widgets-deletions-processor (changes)
1092 "Adjust items tracking allout outline structure deletions.
1093
1094Dispatched by `allout-widgets-post-command-business' in response to
1095:deleted entries recorded by `allout-widgets-deletions-recorder'."
1096 (save-excursion
1097 (dolist (change changes)
1098 (let ((depth (cadr change))
1099 (from (caddr change)))
1100 (goto-char from)
1101 (when (allout-previous-visible-heading 1)
1102 (if (> depth 1)
1103 (allout-ascend-to-depth (1- depth)))
1104 (allout-redecorate-visible-subtree
1105 (allout-get-or-create-item-widget 'redecorate)))))))
1106
1107;;;_ > allout-widgets-shifts-recorder (shifted-amount at)
1108(defun allout-widgets-shifts-recorder (shifted-amount at)
1109 "Record outline subtree shifts for tracking during post-command processing.
1110
1111Intended for use on `allout-structure-shifted-hook'.
1112
1113SHIFTED-AMOUNT is the depth change and AT is the point at the start of the
1114subtree that's been shifted.
1115
1116Records changes in `allout-widgets-changes-record'."
1117 (push (list :shifted shifted-amount at) allout-widgets-changes-record))
1118;;;_ > allout-widgets-shifts-processor (changes)
1119(defun allout-widgets-shifts-processor (changes)
1120 "Widgetize and adjust items tracking allout outline structure additions.
1121
1122Dispatched by `allout-widgets-post-command-business' in response to
1123:shifted entries recorded by `allout-widgets-shifts-recorder'."
1124 (save-excursion
1125 (dolist (change changes)
1126 (goto-char (caddr change))
1127 (allout-ascend)
1128 (allout-redecorate-visible-subtree))))
1129;;;_ > allout-widgets-after-copy-or-kill-function ()
1130(defun allout-widgets-after-copy-or-kill-function ()
1131 "Do allout-widgets processing of text just placed in the kill ring.
1132
1133Intended for use on allout-after-copy-or-kill-hook."
1134 (if (car kill-ring)
1135 (setcar kill-ring (allout-widgets-undecorate-text (car kill-ring)))))
919d884a
KM
1136;;;_ > allout-widgets-after-undo-function ()
1137(defun allout-widgets-after-undo-function ()
1138 "Do allout-widgets processing of text after an undo.
1139
1140Intended for use on allout-post-undo-hook."
1141 (save-excursion
1142 (if (allout-goto-prefix)
1143 (allout-redecorate-item (allout-get-or-create-item-widget)))))
aac7a935
KM
1144
1145;;;_ > allout-widgets-exposure-undo-recorder (widget from-state)
1146(defun allout-widgets-exposure-undo-recorder (widget)
1147 "Record outline exposure undo for tracking during post-command processing.
1148
1149Intended for use by `allout-graphics-modification-handler'.
1150
1151WIDGET is the widget being changed.
1152
1153Records changes in `allout-widgets-changes-record'."
1154 ;; disregard the events if we're currently processing them.
1155 (if (not allout-undo-exposure-in-progress)
1156 (push widget allout-widgets-undo-exposure-record)))
1157;;;_ > allout-widgets-exposure-undo-processor ()
1158(defun allout-widgets-exposure-undo-processor ()
1159 "Adjust items tracking undo of allout outline structure exposure.
1160
1161Dispatched by `allout-widgets-post-command-business' in response to
1162:undone-exposure entries recorded by `allout-widgets-exposure-undo-recorder'."
1163 (let* ((allout-undo-exposure-in-progress t)
1164 ;; inhibit undo recording while twiddling exposure to track undo:
1165 (widgets allout-widgets-undo-exposure-record)
1166 widget widget-start-marker widget-end-marker
1167 from-state icon-start-point to-state
1168 handled covered)
1169 (setq allout-widgets-undo-exposure-record nil)
1170 (save-excursion
1171 (dolist (widget widgets)
1172 (setq widget-start-marker (widget-get widget :from)
1173 widget-end-marker (widget-get widget :to)
1174 from-state (widget-get widget :icon-state)
1175 icon-start-point (widget-apply widget :actual-position
1176 :icon-start)
1177 to-state (get-text-property icon-start-point
1178 :icon-state))
1179 (setq handled (allout-range-overlaps widget-start-marker
1180 widget-end-marker
1181 handled)
1182 covered (car handled)
1183 handled (cadr handled))
1184 (when (not covered)
1185 (goto-char (widget-get widget :from))
1186 (when (not (allout-hidden-p))
1187 ;; adjust actual exposure to that of to-state viz from-state
1188 (cond ((and (eq to-state 'closed) (eq from-state 'opened))
1189 (allout-hide-current-subtree)
1190 (allout-decorate-item-and-context widget))
1191 ((and (eq to-state 'opened) (eq from-state 'closed))
1192 (save-excursion
1193 (dolist
1194 (expose-to (allout-chart-exposure-contour-by-icon))
1195 (goto-char expose-to)
1196 (allout-show-to-offshoot)))))))))))
1197;;;_ > allout-chart-exposure-contour-by-icon (&optional from-depth)
1198(defun allout-chart-exposure-contour-by-icon (&optional from-depth)
1199 "Return points of subtree items to which exposure should be extended.
1200
1201The qualifying items are ones with a widget icon that is in the closed or
1202empty state, or items with undecorated subitems.
1203
1204The resulting list of points is in reverse order.
1205
1206Optional FROM-DEPTH is for internal use."
1207 ;; During internal recursion, we return a pair: (at-end . result)
1208 ;; Otherwise we just return the result.
1209 (let ((from-depth from-depth)
1210 start-point
1211 at-end level-depth
1212 this-widget
1213 got subgot)
1214 (if from-depth
1215 (setq level-depth (allout-depth))
1216 ;; at containing item:
1217 (setq start-point (point))
1218 (setq from-depth (allout-depth))
1219 (setq at-end (not (allout-next-heading))
1220 level-depth allout-recent-depth))
1221
1222 ;; traverse the level, recursing on deeper levels:
1223 (while (and (not at-end)
1224 (> allout-recent-depth from-depth)
1225 (setq this-widget (allout-get-item-widget)))
1226 (if (< level-depth allout-recent-depth)
1227 ;; recurse:
1228 (progn
1229 (setq subgot (allout-chart-exposure-contour-by-icon level-depth)
1230 at-end (car subgot)
1231 subgot (cdr subgot))
1232 (if subgot (setq got (append subgot got))))
1233 ;; progress at this level:
1234 (when (memq (widget-get this-widget :icon-state) '(closed empty))
1235 (push (point) got)
1236 (allout-end-of-subtree))
1237 (setq at-end (not (allout-next-heading)))))
1238
1239 ;; tailor result depending on whether or not we're a recursion:
1240 (if (not start-point)
1241 (cons at-end got)
1242 (goto-char start-point)
1243 got)))
1244;;;_ > allout-range-overlaps (from to ranges)
1245(defun allout-range-overlaps (from to ranges)
1246 "Return a pair indicating overlap of FROM and TO subtree range in RANGES.
1247
1248First element of result indicates whether candadate range FROM, TO
1249overlapped any of the existing ranges.
1250
1251Second element of result is a new version of RANGES incorporating the
1252candidate range with overlaps consolidated.
1253
1254FROM and TO must be in increasing order, as must be the pairs in RANGES."
1255 ;; to append to the end: (rplacd next-to-last-cdr (list 'f))
1256 (let (new-ranges
1257 entry
1258 ;; the start of the range that includes the candidate from:
1259 included-from
1260 ;; the end of the range that includes the candidate to:
1261 included-to
1262 ;; the candidates were inserted:
1263 done)
1264 (while (and ranges (not done))
1265 (setq entry (car ranges)
1266 ranges (cdr ranges))
1267
1268 (cond
1269
1270 (included-from
1271 ;; some entry included the candidate from.
1272 (cond ((> (car entry) to)
1273 ;; current entry exceeds end of candidate range - done.
1274 (push (list included-from to) new-ranges)
1275 (push entry new-ranges)
1276 (setq included-to to
1277 done t))
1278 ((>= (cadr entry) to)
1279 ;; current entry includes end of candidate range - done.
1280 (push (list included-from (cadr entry)) new-ranges)
1281 (setq included-to (cadr entry)
1282 done t))
1283 ;; current entry contained in candidate range - ditch, continue:
1284 (t nil)))
1285
1286 ((> (car entry) to)
1287 ;; current entry start exceeds candidate end - done, placed as new entry
1288 (push (list from to) new-ranges)
1289 (push entry new-ranges)
1290 (setq included-to to
1291 done t))
1292
1293 ((>= (car entry) from)
1294 ;; current entry start is above candidate start, but not above
1295 ;; candidate end (by prior case).
1296 (setq included-from from)
1297 ;; now we have to check on whether this entry contains to, or continue:
1298 (when (>= (cadr entry) to)
1299 ;; current entry contains only candidate end - done:
1300 (push (list included-from (cadr entry)) new-ranges)
1301 (setq included-to (cadr entry)
1302 done t))
1303 ;; otherwise, we will continue to look for placement of candidate end.
1304 )
1305
1306 ((>= (cadr entry) to)
1307 ;; current entry properly contains candidate range.
1308 (push entry new-ranges)
1309 (setq included-from (car entry)
1310 included-to (cadr entry)
1311 done t))
1312
1313 ((>= (cadr entry) from)
1314 ;; current entry contains start of candidate range.
1315 (setq included-from (car entry)))
1316
1317 (t
1318 ;; current entry is below the candidate range.
1319 (push entry new-ranges))))
1320
1321 (cond ((and included-from included-to)
1322 ;; candidates placed.
1323 nil)
1324 ((not (or included-from included-to))
1325 ;; candidates found no place, must be at the end:
1326 (push (list from to) new-ranges))
1327 (included-from
1328 ;; candidate start placed but end not:
1329 (push (list included-from to) new-ranges))
1330 ;; might be included-to and not included-from, indicating new entry.
1331 )
1332 (setq new-ranges (nreverse new-ranges))
1333 (if ranges (setq new-ranges (append new-ranges ranges)))
1334 (list (if included-from t) new-ranges)))
1335;;;_ > allout-test-range-overlaps ()
1336(defun allout-test-range-overlaps ()
1337 "allout-range-overlaps unit tests."
1338 (let* (ranges
1339 got
1340 (try (lambda (from to)
1341 (setq got (allout-range-overlaps from to ranges))
1342 (setq ranges (cadr got))
1343 got)))
1344;; ;; biggie:
1345;; (setq ranges nil)
1346;; ;; ~ .02 to .1 seconds for just repeated listing args instead of funcall
1347;; ;; ~ 13 seconds for doing repeated funcall
1348;; (message "time-trial: %s, resulting size %s"
1349;; (time-trial
1350;; '(let ((size 10000)
1351;; doing)
1352;; (random t)
1353;; (dotimes (count size)
1354;; (setq doing (random size))
1355;; (funcall try doing (+ doing (random 5)))
1356;; ;;(list doing (+ doing (random 5)))
1357;; )))
1358;; (length ranges))
1359;; (sit-for 2)
1360
1361 ;; fresh:
1362 (setq ranges nil)
1363 (assert (equal (funcall try 3 5) '(nil ((3 5)))))
1364 ;; add range at end:
1365 (assert (equal (funcall try 10 12) '(nil ((3 5) (10 12)))))
1366 ;; add range at beginning:
1367 (assert (equal (funcall try 1 2) '(nil ((1 2) (3 5) (10 12)))))
1368 ;; insert range somewhere in the middle:
1369 (assert (equal (funcall try 7 9) '(nil ((1 2) (3 5) (7 9) (10 12)))))
1370 ;; consolidate some:
1371 (assert (equal (funcall try 5 8) '(t ((1 2) (3 9) (10 12)))))
1372 ;; add more:
1373 (assert (equal (funcall try 15 17) '(nil ((1 2) (3 9) (10 12) (15 17)))))
1374 ;; add more:
1375 (assert (equal (funcall try 20 22)
1376 '(nil ((1 2) (3 9) (10 12) (15 17) (20 22)))))
1377 ;; encompass more:
1378 (assert (equal (funcall try 4 11) '(t ((1 2) (3 12) (15 17) (20 22)))))
1379 ;; encompass all:
1380 (assert (equal (funcall try 2 25) '(t ((1 25)))))
1381
1382 ;; fresh slate:
1383 (setq ranges nil)
1384 (assert (equal (funcall try 20 25) '(nil ((20 25)))))
1385 (assert (equal (funcall try 30 35) '(nil ((20 25) (30 35)))))
1386 (assert (equal (funcall try 26 28) '(nil ((20 25) (26 28) (30 35)))))
1387 (assert (equal (funcall try 15 20) '(t ((15 25) (26 28) (30 35)))))
1388 (assert (equal (funcall try 10 30) '(t ((10 35)))))
1389 (assert (equal (funcall try 5 6) '(nil ((5 6) (10 35)))))
1390 (assert (equal (funcall try 2 100) '(t ((2 100)))))
1391
1392 (setq ranges nil)
1393 ))
1394;;;_ > allout-widgetize-buffer (&optional doing)
1395(defun allout-widgetize-buffer (&optional doing)
1396 "EXAMPLE FUNCTION. Widgetize items in buffer using allout-chart-subtree.
1397
1398We economize by just focusing on the first of local-maximum depth siblings.
1399
1400Optional DOING is for internal use - a chart of the current level, for
1401recursive operation."
1402
1403 (interactive)
1404 (if (not doing)
1405
1406 (save-excursion
1407 (goto-char (point-min))
1408 ;; Construct the chart by scanning the siblings:
1409 (dolist (top-level-sibling (allout-chart-siblings))
1410 (goto-char top-level-sibling)
1411 (let ((subchart (allout-chart-subtree)))
1412 (if subchart
1413 (allout-widgetize-buffer subchart)))))
1414
1415 ;; save-excursion was done on recursion entry, not necessary here.
1416 (let (have-sublists)
1417 (dolist (sibling doing)
1418 (when (listp sibling)
1419 (setq have-sublists t)
1420 (allout-widgetize-buffer sibling)))
1421 (when (and (not have-sublists) (not (widget-at (car doing))))
1422 (goto-char (car doing))
1423 (allout-get-or-create-item-widget)))))
1424
1425;;;_ : Item widget and constructors
1426
1427;;;_ $ allout-item-widget
1428(define-widget 'allout-item-widget 'default
1429 "A widget presenting an allout outline item."
1430
1431 'button nil
1432 ;; widget-field-at respects this to get item if 'field is unused.
1433 ;; we don't use field to avoid collision with end-of-line, etc, on which
1434 ;; allout depends.
1435 'real-field nil
1436
1437 ;; data fields:
1438
1439
1440 ;; tailor the widget for a specific item
1441 :create 'allout-decorate-item-and-context
1442 :value-delete 'allout-widgets-undecorate-item
1443 ;; Not Yet Converted (from original, tree-widget stab)
1444 :expander 'allout-tree-event-dispatcher ; get children when nil :args
1445 :expander-p 'identity ; always engage the :expander
1446 :action 'allout-tree-widget-action
1447 ;; :notify "when item changes"
1448
1449 ;; force decoration of item but not context, unless already done this tick:
1450 :redecorate 'allout-redecorate-item
1451 :last-decorated-tick nil
1452 ;; recognize the actual situation of the item's text:
1453 :parse-item 'allout-parse-item-at-point
1454 ;; decorate the entirety of the item, sans offspring:
1455 :decorate-item-span 'allout-decorate-item-span
1456 ;; decorate the various item elements:
1457 :decorate-guides 'allout-decorate-item-guides
1458 :decorate-icon 'allout-decorate-item-icon
1459 :decorate-cue 'allout-decorate-item-cue
1460 :decorate-body 'allout-decorate-item-body
1461 :actual-position 'allout-item-actual-position
1462
1463 ;; Layout parameters:
1464 :is-container nil ; is this actually the encompassing file/connection?
1465
1466 :from nil ; item beginning - marker
1467 :to nil ; item end - marker
1468 :span-overlay nil ; overlay by which actual postion is determined
1469
1470 ;; also serves as guide-end:
1471 :icon-start nil
1472 :icon-end nil
1473 :distinctive-start nil
1474 ;; also serves as cue-start:
1475 :distinctive-end nil
1476 ;; also serves as cue-end:
1477 :body-start nil
1478 :body-end nil
1479 :depth nil
1480 :has-subitems nil
1481 :was-has-subitems 'init
1482 :expanded nil
1483 :was-expanded 'init
1484 :brief nil
1485 :was-brief 'init
1486
1487 :does-encrypt nil ; pending encryption when :is-encrypted false.
1488 :is-encrypted nil
1489
1490 ;; the actual location of the item text:
1491 :location 'allout-item-location
1492
1493 :button-keymap allout-item-icon-keymap ; XEmacs
1494 :keymap allout-item-icon-keymap ; Emacs
1495
1496 ;; Element regions:
1497 :guides-span nil
1498 :icon-span nil
1499 :cue-span nil
1500 :bullet nil
1501 :was-bullet nil
1502 :body-span nil
1503
1504 :body-brevity-p 'allout-body-brevity-p
1505
1506 ;; :guide-column-flags indicate (in reverse order) whether or not the
1507 ;; item's ancestor at the depth corresponding to the column has a
1508 ;; subsequent sibling - ie, whether or not the corresponding column needs
1509 ;; a descender line to connect that ancestor with its sibling.
1510 :guide-column-flags nil
1511 :was-guide-column-flags 'init
1512
1513 ;; ie, has subitems:
1514 :populous-p 'allout-item-populous-p
1515 :help-echo 'allout-tree-widget-help-echo
1516 )
1517;;;_ > allout-new-item-widget ()
1518(defsubst allout-new-item-widget ()
1519 "create a new item widget, not yet situated anywhere."
1520 (if allout-widgets-maintain-tally
1521 ;; all the extra overhead is incurred only when doing the
1522 ;; maintenance, except the condition, which can't be avoided.
1523 (let ((widget (widget-convert 'allout-item-widget)))
1524 (puthash widget nil allout-widgets-tally)
1525 widget)
1526 (widget-convert 'allout-item-widget)))
1527;;;_ : Item decoration
1528;;;_ > allout-decorate-item-and-context (item-widget &optional redecorate
1529;;; blank-container parent)
1530(defun allout-decorate-item-and-context (item-widget &optional redecorate
1531 blank-container parent)
1532 "Create or adjust widget decorations for ITEM-WIDGET and neighbors at point.
1533
1534The neighbors include its siblings and parent.
1535
1536ITEM-WIDGET can be a created or converted allout-item-widget.
1537
1538If you're only trying to get or create a widget for an item, use
1539`allout-get-or-create-item-widget'. If you have the item-widget, applying
1540:redecorate will do the right thing.
1541
1542Optional BLANK-CONTAINER is for internal use. It is used to fabricate a
1543container widget for an empty-bodied container, in the course of decorating
1544a proper \(non-container\) item which starts at the beginning of the file.
1545
1546Optional REDECORATE causes redecoration of the item-widget and
1547its siblings, even if already decorated in this cycle of the command loop.
1548
1549Optional PARENT, when provided, bypasses some navigation and computation
1550necessary to obtain the parent of the items being processed.
1551
1552We return the item-widget corresponding to the item at point."
1553
1554 (when (or redecorate
1555 (not (equal (widget-get item-widget :last-decorated-tick)
1556 allout-command-counter)))
1557 (let* ((allout-inhibit-body-modification-hook t)
1558 (was-modified (buffer-modified-p))
1559 (was-point (point))
1560 prefix-start
1561 (is-container (or blank-container
1562 (not (setq prefix-start (allout-goto-prefix)))
1563 (< was-point prefix-start)))
1564 ;; steady-point (set in two steps) is reliable across parent
1565 ;; widget-creation.
1566 (steady-point (progn (if is-container (goto-char 1))
1567 (point-marker)))
1568 (steady-point (progn (set-marker-insertion-type steady-point t)
1569 steady-point))
1570 (parent (and (not is-container)
1571 (allout-get-or-create-parent-widget)))
1572 parent-flags parent-depth
1573 successor-sibling
1574 body
1575 doing-item
1576 sub-item-widget
1577 depth
1578 reverse-siblings-chart
1579 (buffer-undo-list t))
1580
1581 ;; At this point the parent is decorated and parent-flags indicate
1582 ;; its guide lines. We will iterate over the siblings according to a
1583 ;; chart we create at the start, and going from last to first so we
1584 ;; don't have to worry about text displacement caused by widgetizing.
1585
1586 (if is-container
1587 (progn (widget-put item-widget :is-container t)
1588 (setq reverse-siblings-chart (list 1)))
1589 (goto-char (widget-apply parent :actual-position :from))
1590 (if (widget-get parent :is-container)
1591 ;; `allout-goto-prefix' will go to first non-container item:
1592 (allout-goto-prefix)
1593 (allout-next-heading))
1594 (setq depth (allout-recent-depth))
1595 (setq reverse-siblings-chart (list allout-recent-prefix-beginning))
1596 (while (allout-next-sibling)
1597 (push allout-recent-prefix-beginning reverse-siblings-chart)))
1598
1599 (dolist (doing-at reverse-siblings-chart)
1600 (goto-char doing-at)
1601 (when allout-widgets-track-decoration
1602 (sit-for 0))
1603
1604 (setq doing-item (if (= doing-at steady-point)
1605 item-widget
1606 (or (allout-get-item-widget)
1607 (allout-new-item-widget))))
1608
1609 (when (or redecorate (not (equal (widget-get doing-item
1610 :last-decorated-tick)
1611 allout-command-counter)))
1612 (widget-apply doing-item :parse-item t blank-container)
1613 (widget-apply doing-item :decorate-item-span)
1614
1615 (widget-apply doing-item :decorate-guides
1616 parent (and successor-sibling t))
1617 (widget-apply doing-item :decorate-icon)
1618 (widget-apply doing-item :decorate-cue)
1619 (widget-apply doing-item :decorate-body)
1620
1621 (widget-put doing-item :last-decorated-tick allout-command-counter))
1622
1623 (setq successor-sibling doing-at))
1624
1625 (set-buffer-modified-p was-modified)
1626 (goto-char steady-point)
1627 ;; must null the marker or the buffer gets clogged with impedence:
1628 (set-marker steady-point nil)
1629
1630 item-widget)))
1631;;;_ > allout-redecorate-item (item)
1632(defun allout-redecorate-item (item-widget)
1633 "Resituate ITEM-WIDGET decorations, disregarding context.
1634
09e80d9f 1635Use this to redecorate only the item, when you know that its
aac7a935
KM
1636situation with respect to siblings, parent, and offspring is
1637unchanged from its last decoration. Use
1638`allout-decorate-item-and-context' instead to reassess and adjust
1639relevent context, when suitable."
1640 (if (not (equal (widget-get item-widget :last-decorated-tick)
1641 allout-command-counter))
1642 (let ((was-modified (buffer-modified-p))
1643 (buffer-undo-list t))
1644 (widget-apply item-widget :parse-item)
1645 (widget-apply item-widget :decorate-guides)
1646 (widget-apply item-widget :decorate-icon)
1647 (widget-apply item-widget :decorate-cue)
1648 (widget-apply item-widget :decorate-body)
1649 (set-buffer-modified-p was-modified))))
1650;;;_ > allout-redecorate-visible-subtree (&optional parent-widget
1651;;; depth chart)
1652(defun allout-redecorate-visible-subtree (&optional parent-widget depth chart)
1653 "Redecorate all visible items in subtree at point.
1654
1655Optional PARENT-WIDGET is for optimization, when the parent
1656widget is already available.
1657
1658Optional DEPTH restricts the excursion depth of covered.
1659
1660Optional CHART is for internal recursion, to carry a chart of the
1661target items.
1662
1663Point is left at the last sibling in the visible subtree."
1664 ;; using a treatment that takes care of all the siblings on a level, we
1665 ;; only need apply it to the first sibling on the level, and we can
1666 ;; collect and pass the parent of the lower levels to recursive calls as
1667 ;; we go.
1668 (let ((parent-widget
1669 (if (and parent-widget (widget-apply parent-widget
1670 :actual-position :from))
1671 (progn (goto-char (widget-apply parent-widget
1672 :actual-position :from))
1673 parent-widget)
1674 (let ((got (allout-get-item-widget)))
1675 (if got
1676 (allout-decorate-item-and-context got 'redecorate)
1677 (allout-get-or-create-item-widget 'redecorate)))))
1678 (pending-chart (or chart (allout-chart-subtree nil 'visible)))
1679 item-widget
1680 previous-sibling-point
1681 previous-sibling
1682 recent-sibling-point)
1683 (setq pending-chart (nreverse pending-chart))
1684 (dolist (sibling-point pending-chart)
1685 (cond ((integerp sibling-point)
1686 (when (not previous-sibling-point)
1687 (goto-char sibling-point)
1688 (if (setq item-widget (allout-get-item-widget nil))
1689 (allout-decorate-item-and-context item-widget 'redecorate
1690 nil parent-widget)
1691 (allout-get-or-create-item-widget)))
1692 (setq previous-sibling-point sibling-point
1693 recent-sibling-point sibling-point))
1694 ((listp sibling-point)
1695 (if (or (not depth)
1696 (> depth 1))
1697 (allout-redecorate-visible-subtree
1698 (if (not previous-sibling-point)
1699 ;; containment discontinuity - sigh
1700 parent-widget
1701 (allout-get-or-create-item-widget 'redecorate))
1702 (if depth (1- depth))
1703 sibling-point)))))
1704 (if (and recent-sibling-point (< (point) recent-sibling-point))
1705 (goto-char recent-sibling-point))))
1706;;;_ > allout-parse-item-at-point (item-widget &optional at-beginning
1707;;; blank-container)
1708(defun allout-parse-item-at-point (item-widget &optional at-beginning
1709 blank-container)
1710 "Set widget ITEM-WIDGET layout parameters per item-at-point's actual layout.
1711
1712If optional AT-BEGINNING is t, then point is assumed to be at the start of
1713the item prefix.
1714
1715If optional BLANK-CONTAINER is true, then the parameters of a container
1716which has an empty body are set. \(Though the body is blank, the object
1717may have subitems.\)"
1718
1719 ;; Uncomment this sit-for to notice where decoration is happening:
1720;; (sit-for .1)
1721 (let* ((depth (allout-depth))
1722 (depth (if blank-container 0 depth))
1723 (is-container (or blank-container (zerop depth)))
1724
1725 (does-encrypt (and (not is-container)
1726 (allout-encrypted-type-prefix)))
1727 (is-encrypted (and does-encrypt (allout-encrypted-topic-p)))
1728 (icon-end allout-recent-prefix-end)
1729 (icon-start (1- icon-end))
1730 body-start
1731 body-end
1732 bullet
1733 has-subitems
1734 (contents-depth (1+ depth))
1735 )
1736 (widget-put item-widget :depth depth)
1737 (if is-container
1738
1739 (progn
1740 (widget-put item-widget :from (allout-set-boundary-marker
1741 :from (point-min)
1742 (widget-get item-widget :from)))
1743 (widget-put item-widget :icon-end nil)
1744 (widget-put item-widget :icon-start nil)
1745 (setq body-start (widget-put item-widget :body-start 1)))
1746
1747 ;; not container:
1748
1749 (widget-put item-widget :from (allout-set-boundary-marker
1750 :from (if at-beginning
1751 (point)
1752 allout-recent-prefix-beginning)
1753 (widget-get item-widget :from)))
1754 (widget-put item-widget :icon-start icon-start)
1755 (widget-put item-widget :icon-end icon-end)
1756 (when does-encrypt
1757 (widget-put item-widget :does-encrypt t)
1758 (widget-put item-widget :is-encrypted is-encrypted))
1759
1760 ;; cue area:
1761 (setq body-start icon-end)
1762 (widget-put item-widget :bullet (setq bullet (allout-get-bullet)))
1763 (if (equal (char-after body-start) ? )
1764 (setq body-start (1+ body-start)))
1765 (widget-put item-widget :body-start body-start)
1766 )
1767
1768 ;; Both container and regular items:
1769
1770 ;; :body-end (doesn't include a trailing blank line, if any) -
1771 (widget-put item-widget :body-end (setq body-end
1772 (if blank-container
1773 1
1774 (allout-end-of-entry))))
1775
1776 (widget-put item-widget :to (allout-set-boundary-marker
1777 :to (if blank-container
1778 (point-min)
1779 (or (allout-pre-next-prefix)
1780 (goto-char (point-max))))
1781 (widget-get item-widget :to)))
1782 (widget-put item-widget :has-subitems
1783 (setq has-subitems
1784 (and
1785 ;; has a subsequent item:
1786 (not (= body-end (point-max)))
1787 ;; subsequent item is deeper:
1788 (< depth (setq contents-depth (allout-recent-depth))))))
1789 ;; note :expanded - true if widget item's content is currently visible?
1790 (widget-put item-widget :expanded
1791 (and has-subitems
1792 ;; subsequent item is or isn't visible:
1793 (save-excursion
1794 (goto-char allout-recent-prefix-beginning)
1795 (not (allout-hidden-p)))))))
1796;;;_ > allout-set-boundary-marker (boundary position &optional current-marker)
1797(defun allout-set-boundary-marker (boundary position &optional current-marker)
1798 "Set or create item widget BOUNDARY type marker at POSITION.
1799
1800Optional CURRENT-MARKER is the marker currently being used for
1801the boundary, if any.
1802
1803BOUNDARY type is either :from or :to, determining the marker insertion type."
1804 (if (not position) (setq position (point)))
1805 (if current-marker
1806 (set-marker current-marker position)
1807 (let ((marker (make-marker)))
1808 ;; XXX dang - would like for :from boundary to advance after inserted
1809 ;; text, but that would omit new header prefixes when allout
1810 ;; relevels, etc. this competes with ad-hoc edits, which would
1811 ;; better be omitted
1812 (set-marker-insertion-type marker nil)
1813 (set-marker marker position))))
1814;;;_ > allout-decorate-item-span (item-widget)
1815(defun allout-decorate-item-span (item-widget)
1816 "Equip the item with a span, as an entirety.
1817
1818This span is implemented so it can be used to detect displacement
1819of the widget in absolute terms, and provides an offset bias for
1820the various element spans."
1821
1822 (if (and (widget-get item-widget :is-container)
1823 ;; the only case where the span could be empty.
1824 (eq (widget-get item-widget :from)
1825 (widget-get item-widget :to)))
1826 nil
1827 (allout-item-span item-widget
1828 (widget-get item-widget :from)
1829 (widget-get item-widget :to))))
1830;;;_ > allout-decorate-item-guides (item-widget
1831;;; &optional parent-widget has-successor)
1832(defun allout-decorate-item-guides (item-widget
1833 &optional parent-widget has-successor)
1834 "Add ITEM-WIDGET guide icon-prefix descender and connector text properties.
1835
1836Optional arguments provide context for deriving the guides. In
1837their absence, the current guide column flags are used.
1838
1839Optional PARENT-WIDGET is the widget for the item's parent item.
1840
1841Optional HAS-SUCCESSOR is true iff the item is followed by a sibling.
1842
1843We also hide the header-prefix string.
1844
1845Guides are established according to the item-widget's :guide-column-flags,
1846when different than :was-guide-column-flags. Changing that property and
1847reapplying this method will rectify the glyphs."
1848
1849 (when (not (widget-get item-widget :is-container))
1850 (let* ((depth (widget-get item-widget :depth))
1851 (parent-depth (and parent-widget
1852 (widget-get parent-widget :depth)))
1853 (parent-flags (and parent-widget
1854 (widget-get parent-widget :guide-column-flags)))
1855 (parent-flags-depth (length parent-flags))
1856 (extender-length (- depth (+ parent-flags-depth 2)))
1857 (flags (or (and (> depth 1)
1858 parent-widget
1859 (widget-put item-widget :guide-column-flags
1860 (append (list has-successor)
1861 (if (< 0 extender-length)
1862 (make-list extender-length
1863 '-))
1864 parent-flags)))
1865 (widget-get item-widget :guide-column-flags)))
1866 (was-flags (widget-get item-widget :was-guide-column-flags))
1867 (guides-start (widget-get item-widget :from))
1868 (guides-end (widget-get item-widget :icon-start))
1869 (position guides-start)
1870 (increment (length allout-header-prefix))
1871 reverse-flags
1872 guide-name
1873 extenders paint-extenders
1874 (inhibit-read-only t))
1875
1876 (when (not (equal was-flags flags))
1877
1878 (setq reverse-flags (reverse flags))
1879 (while reverse-flags
1880 (setq guide-name
1881 (cond ((null (cdr reverse-flags))
1882 (if (car reverse-flags)
1883 'mid-connector
1884 'end-connector))
1885 ((eq (car reverse-flags) '-)
1886 ;; accumulate extenders tally, to be painted on next
1887 ;; non-extender flag, according to the flag type.
1888 (setq extenders (1+ (or extenders 0)))
1889 nil)
1890 ((car reverse-flags)
1891 'through-descender)
1892 (t 'skip-descender)))
1893 (when guide-name
1894 (put-text-property position (setq position (+ position increment))
1895 'display (allout-fetch-icon-image guide-name))
1896 (if (> increment 1) (setq increment 1))
1897 (when extenders
1898 ;; paint extenders after a connector, else leave spaces.
1899 (dotimes (i extenders)
1900 (put-text-property
1901 position (setq position (1+ position))
1902 'display (allout-fetch-icon-image
1903 (if (memq guide-name '(mid-connector end-connector))
1904 'extender-connector
1905 'skip-descender))))
1906 (setq extenders nil)))
1907 (setq reverse-flags (cdr reverse-flags)))
1908 (widget-put item-widget :was-guide-column-flags flags))
1909
1910 (allout-item-element-span-is item-widget :guides-span
1911 guides-start guides-end))))
1912;;;_ > allout-decorate-item-icon (item-widget)
1913(defun allout-decorate-item-icon (item-widget)
1914 "Add item icon glyph and distinctive bullet text properties to ITEM-WIDGET."
1915
1916 (when (not (widget-get item-widget :is-container))
1917 (let* ((icon-start (widget-get item-widget :icon-start))
1918 (icon-end (widget-get item-widget :icon-end))
1919 (bullet (widget-get item-widget :bullet))
1920 (use-bullet bullet)
1921 (was-bullet (widget-get item-widget :was-bullet))
1922 (distinctive (allout-distinctive-bullet bullet))
1923 (distinctive-start (widget-get item-widget :distinctive-start))
1924 (distinctive-end (widget-get item-widget :distinctive-end))
1925 (does-encrypt (widget-get item-widget :does-encrypt))
1926 (is-encrypted (and does-encrypt (widget-get item-widget
1927 :is-encrypted)))
1928 (expanded (widget-get item-widget :expanded))
1929 (has-subitems (widget-get item-widget :has-subitems))
1930 (inhibit-read-only t)
1931 icon-state)
1932
1933 (when (not (and (equal (widget-get item-widget :was-expanded) expanded)
1934 (equal (widget-get item-widget :was-has-subitems)
1935 has-subitems)
1936 (equal (widget-get item-widget :was-does-encrypt)
1937 does-encrypt)
1938 (equal (widget-get item-widget :was-is-encrypted)
1939 is-encrypted)))
1940
1941 (setq icon-state
1942 (cond (does-encrypt (if is-encrypted
3a00a363
KM
1943 'locked-encrypted
1944 'unlocked-encrypted))
aac7a935
KM
1945 (expanded 'opened)
1946 (has-subitems 'closed)
1947 (t 'empty)))
1948 (put-text-property icon-start (1+ icon-start)
1949 'display (allout-fetch-icon-image icon-state))
1950 (widget-put item-widget :was-expanded expanded)
1951 (widget-put item-widget :was-has-subitems has-subitems)
1952 (widget-put item-widget :was-does-encrypt does-encrypt)
1953 (widget-put item-widget :was-is-encrypted is-encrypted)
1954 ;; preserve as a widget property to track last known:
1955 (widget-put item-widget :icon-state icon-state)
1956 ;; preserve as a text property to track undo:
1957 (put-text-property icon-start icon-end :icon-state icon-state))
1958 (allout-item-element-span-is item-widget :icon-span
1959 icon-start icon-end)
1960 (when (not (string= was-bullet bullet))
1961 (cond ((not distinctive)
1962 ;; XXX we strip the prior properties without even checking if
1963 ;; the prior bullet was distinctive, because the widget
1964 ;; provisions to convey that info is disappearing, sigh.
1965 (remove-text-properties icon-end (1+ icon-end) '(display))
1966 (setq distinctive-start icon-end distinctive-end icon-end)
1967 (widget-put item-widget :distinctive-start distinctive-start)
1968 (widget-put item-widget :distinctive-end distinctive-end))
1969
1970 ((not (string= bullet allout-numbered-bullet))
1971 (setq distinctive-start icon-end distinctive-end (+ icon-end 1)))
1972
1973 (does-encrypt
1974 (setq distinctive-start icon-end distinctive-end (+ icon-end 1)))
1975
1976 (t
1977 (goto-char icon-end)
1978 (looking-at "[0-9]+")
1979 (setq use-bullet (buffer-substring icon-end (match-end 0)))
1980 (setq distinctive-start icon-end
1981 distinctive-end (match-end 0))))
1982 (put-text-property distinctive-start distinctive-end 'display
1983 use-bullet)
1984 (widget-put item-widget :was-bullet bullet)
1985 (widget-put item-widget :distinctive-start distinctive-start)
1986 (widget-put item-widget :distinctive-end distinctive-end)))))
1987;;;_ > allout-decorate-item-cue (item-widget)
1988(defun allout-decorate-item-cue (item-widget)
1989 "Incorporate space between bullet icon and body to the ITEM-WIDGET."
1990 ;; NOTE: most of the cue-area
1991
1992 (when (not (widget-get item-widget :is-container))
1993 (let* ((cue-start (or (widget-get item-widget :distinctive-end)
1994 (widget-get item-widget :icon-end)))
1995 (body-start (widget-get item-widget :body-start))
1996 (expanded (widget-get item-widget :expanded))
1997 (has-subitems (widget-get item-widget :has-subitems))
1998 (inhibit-read-only t))
1999
2000 (allout-item-element-span-is item-widget :cue-span cue-start body-start)
2001 (put-text-property (1- body-start) body-start 'rear-nonsticky t))))
2002;;;_ > allout-decorate-item-body (item-widget &optional force)
2003(defun allout-decorate-item-body (item-widget &optional force)
2004 "Incorporate item body text as part the ITEM-WIDGET.
2005
2006Optional FORCE means force reassignment of the region property."
2007
2008 (let* ((allout-inhibit-body-modification-hook t)
2009 (body-start (widget-get item-widget :body-start))
2010 (body-end (widget-get item-widget :body-end))
2011 (body-text-end body-end)
2012 (inhibit-read-only t))
2013
2014 (allout-item-element-span-is item-widget :body-span
2015 body-start (min (1+ body-end) (point-max))
2016 force)))
2017;;;_ > allout-item-actual-position (item-widget field)
2018(defun allout-item-actual-position (item-widget field)
2019 "Return ITEM-WIDGET FIELD position taking item displacement into account."
2020
2021 ;; The item's sub-element positions (:icon-end, :body-start, etc) are
2022 ;; accurate when the item is parsed, but some offsets from the start
2023 ;; drift with text added in the body.
2024 ;;
2025 ;; Rather than reparse an item with every change (inefficient), or derive
2026 ;; every position from a distinct field marker/overlay (prohibitive as
2027 ;; the number of items grows), we use the displacement tracking of the
2028 ;; :span-overlay's markers, against the registered :from or :body-end
2029 ;; (depending on whether the requested field value is before or after the
2030 ;; item body), to bias the registered values.
2031 ;;
2032 ;; This is not necessary/useful when the item is being decorated, because
0d327994 2033 ;; that always must be preceded by a fresh item parse.
aac7a935
KM
2034
2035 (if (not (eq field :body-end))
2036 (widget-get item-widget :from)
2037
2038 (let* ((span-overlay (widget-get item-widget :span-overlay))
2039 (body-end-position (widget-get item-widget :body-end))
2040 (ref-marker-position (and span-overlay
2041 (overlay-end span-overlay)))
2042 (offset (and body-end-position span-overlay
2043 (- (or ref-marker-position 0)
2044 body-end-position))))
2045 (+ (widget-get item-widget field) (or offset 0)))))
2046;;;_ : Item undecoration
2047;;;_ > allout-widgets-undecorate-region (start end)
2048(defun allout-widgets-undecorate-region (start end)
2049 "Eliminate widgets and decorations for all items in region from START to END."
2050 (let ((next start)
2051 widget)
2052 (save-excursion
2053 (goto-char start)
2054 (while (< (setq next (next-single-char-property-change next
2055 'display
2056 (current-buffer)
2057 end))
2058 end)
2059 (goto-char next)
2060 (when (setq widget (allout-get-item-widget))
2061 ;; if the next-property/overly progression got us to a widget:
2062 (allout-widgets-undecorate-item widget t))))))
2063;;;_ > allout-widgets-undecorate-text (text)
2064(defun allout-widgets-undecorate-text (text)
2065 "Eliminate widgets and decorations for all items in TEXT."
2066 (remove-text-properties 0 (length text)
2067 '(display nil :icon-state nil rear-nonsticky nil
2068 category nil button nil field nil)
2069 text)
2070 text)
2071;;;_ > allout-widgets-undecorate-item (item-widget &optional no-expose)
2072(defun allout-widgets-undecorate-item (item-widget &optional no-expose)
2073 "Remove widget decorations from ITEM-WIDGET.
2074
2075Any concealed content head lines and item body is exposed, unless
2076optional NO-EXPOSE is non-nil."
2077 (let ((from (widget-get item-widget :from))
2078 (to (widget-get item-widget :to))
2079 (text-properties-to-remove '(display nil
2080 :icon-state nil
2081 rear-nonsticky nil
2082 category nil
2083 button nil
2084 field nil))
2085 (span-overlay (widget-get item-widget :span-overlay))
2086 (button-overlay (widget-get item-widget :button))
2087 (was-modified (buffer-modified-p))
2088 (buffer-undo-list t)
2089 (inhibit-read-only t))
2090 (if (not no-expose)
2091 (allout-flag-region from to nil))
2092 (allout-unprotected
2093 (remove-text-properties from to text-properties-to-remove))
2094 (when span-overlay
2095 (delete-overlay span-overlay) (widget-put item-widget :span-overlay nil))
2096 (when button-overlay
2097 (delete-overlay button-overlay) (widget-put item-widget :button nil))
2098 (set-marker from nil)
2099 (set-marker to nil)
2100 (if (not was-modified)
2101 (set-buffer-modified-p nil))))
2102
2103;;;_ : Item decoration support
2104;;;_ > allout-item-span (item-widget &optional start end)
2105(defun allout-item-span (item-widget &optional start end)
2106 "Return or register the location of an ITEM-WIDGET's actual START and END.
2107
2108If START and END are not passed in, return either a dotted pair
2109of the current span, if established, or nil if not yet set.
2110
2111When the START and END are passed, return the distance that the
2112start of the item moved. We return 0 if the span was not
2113previously established or is not moved."
2114 (let ((overlay (widget-get item-widget :span-overlay))
2115 was-start was-end
2116 changed)
2117 (cond ((not overlay) (when start
2118 (setq overlay (make-overlay start end nil t nil))
2119 (overlay-put overlay 'button item-widget)
d806ab68 2120 (overlay-put overlay 'evaporate t)
aac7a935
KM
2121 (widget-put item-widget :span-overlay overlay)
2122 t))
2123 ;; report:
2124 ((not start) (cons (overlay-start overlay) (overlay-end overlay)))
2125 ;; move:
2126 ((or (not (equal (overlay-start overlay) start))
2127 (not (equal (overlay-end overlay) end)))
2128 (move-overlay overlay start end)
2129 t)
2130 ;; specified span already set:
2131 (t nil))))
2132;;;_ > allout-item-element-span-is (item-widget element
2133;;; &optional start end force)
2134(defun allout-item-element-span-is (item-widget element
2135 &optional start end force)
2136 "Return or register the location of the indicated ITEM-WIDGET ELEMENT.
2137
2138ELEMENT is one of :guides-span, :icon-span, :cue-span, or :body-span.
2139
2140When optional START is specified, optional END must also be.
2141
2142START and END are the actual bounds of the region, if provided.
2143
2144If START and END are not passed in, we return either a dotted
2145pair of the current span, if established, or nil if not yet set.
2146
2147When the START and END are passed, we return t if the region
2148changed or nil if not.
2149
2150Optional FORCE means force assignment of the region's text
2151property, even if it's already set."
2152 (let ((span (widget-get item-widget element)))
2153 (cond ((or (not span) force)
2154 (when start
2155 (widget-put item-widget element (cons start end))
2156 (put-text-property start end 'category
2157 (cdr (assoc element
2158 allout-span-to-category)))
2159 t))
2160 ;; report:
2161 ((not start) span)
2162 ;; move if necessary:
2163 ((not (and (eq (car span) start)
2164 (eq (cdr span) end)))
2165 (widget-put item-widget element span)
2166 t)
2167 ;; specified span already set:
2168 (t nil))))
2169;;;_ : Item widget retrieval (/ high-level creation):
2170;;;_ > allout-get-item-widget (&optional container)
2171(defun allout-get-item-widget (&optional container)
2172 "Return the widget for the item at point, or nil if no widget yet exists.
2173
2174Point must be situated *before* the start of the target item's
2175body, so we don't get an existing containing item when we're in
2176the process of creating an item in the middle of another.
2177
2178Optional CONTAINER is used to obtain the container item."
2179 (if (or container (zerop (allout-depth)))
2180 allout-container-item-widget
2181 ;; allout-recent-* are calibrated by (allout-depth) if we got here.
2182 (let ((got (widget-at allout-recent-prefix-beginning)))
2183 (if (and got (listp got))
2184 (if (marker-position (widget-get got :from))
2185 (and
2186 (>= (point) (widget-apply got :actual-position :from))
2187 (<= (point) (widget-apply got :actual-position :body-start))
2188 got)
2189 ;; a wacky residual item - undecorate and disregard:
2190 (allout-widgets-undecorate-item got)
2191 nil)))))
2192;;;_ > allout-get-or-create-item-widget (&optional redecorate blank-container)
2193(defun allout-get-or-create-item-widget (&optional redecorate blank-container)
2194 "Return a widget for the item at point, creating the widget if necessary.
2195
2196When creating a widget, we assume there has been a context change
2197and decorate its siblings and parent, as well.
2198
2199Optional BLANK-CONTAINER is for internal use, to fabricate a
2200meta-container item with an empty body when the first proper
2201\(non-container\) item starts at the beginning of the file.
2202
2203Optional REDECORATE, if non-nil, means to redecorate the widget
2204if it already exists."
2205 (let ((widget (allout-get-item-widget blank-container))
2206 (buffer-undo-list t))
2207 (cond (widget (if redecorate
2208 (allout-redecorate-item widget))
2209 widget)
2210 ((or blank-container (zerop (allout-depth)))
2211 (or allout-container-item-widget
2212 (setq allout-container-item-widget
2213 (allout-decorate-item-and-context
2214 (widget-convert 'allout-item-widget)
2215 nil blank-container))))
2216 ;; create a widget for a regular/non-container item:
2217 (t (allout-decorate-item-and-context (widget-convert
2218 'allout-item-widget))))))
2219;;;_ > allout-get-or-create-parent-widget (&optional redecorate)
2220(defun allout-get-or-create-parent-widget (&optional redecorate)
2221 "Return widget for parent of item at point, decorating it if necessary.
2222
2223We return the container widget if we're above the first proper item in the
2224file.
2225
2226Optional REDECORATE, if non-nil, means to redecorate the widget if it
2227already exists.
2228
2229Point will wind up positioned on the beginning of the parent or beginning
2230of the buffer."
2231 ;; use existing widget, if there, else establish it
2232 (if (or (bobp) (and (not (allout-ascend))
2233 (looking-at allout-regexp)))
2234 (allout-get-or-create-item-widget redecorate 'blank-container)
2235 (allout-get-or-create-item-widget redecorate)))
2236;;;_ : X- Item ancillaries
2237;;;_ >X allout-body-modification-handler (beg end)
2238(defun allout-body-modification-handler (beg end)
2239 "Do routine processing of body text before and after modification.
2240
2241Operation is inhibited by `allout-inhibit-body-modification-handler'."
2242
2243;; The primary duties are:
2244;;
2245;; - marking of escaped prefix-like text for delayed cleanup of escapes
2246;; - removal and replacement of the settings
2247;; - maintenance of beginning-of-line guide lines
2248;;
2249;; ?? Escapes removal \(before changes\) is not done when edits span multiple
2250;; items, recognizing that item structure is being preserved, including
2251;; escaping of item-prefix-like text within bodies. See
2252;; `allout-before-modification-handler' and
2253;; `allout-inhibit-body-modification-handler'.
2254;;
2255;; Adds the overlay to the `allout-unresolved-body-mod-workhash' during
2256;; before-change operation, and removes from that list during after-change
2257;; operation.
2258 (cond (allout-inhibit-body-modification-hook nil)))
2259;;;_ >X allout-graphics-modification-handler (beg end)
2260(defun allout-graphics-modification-handler (beg end)
2261 "Protect against incoherent deletion of decoration graphics.
2262
2263Deletes allowed only when inhibit-read-only is t."
2264 (cond
2265 (undo-in-progress (when (eq (get-text-property beg 'category)
2266 'allout-icon-span-category)
2267 (save-excursion
2268 (goto-char beg)
2269 (let* ((item-widget (allout-get-item-widget)))
2270 (if item-widget
2271 (allout-widgets-exposure-undo-recorder
2272 item-widget))))))
2273 (inhibit-read-only t)
2274 ((not (and (boundp 'allout-mode) allout-mode)) t)
2275 ((equal this-command 'quoted-insert) t)
2276 ((yes-or-no-p "Unruly edit of outline structure - allow? ")
2277 (setq allout-widgets-unset-inhibit-read-only (not inhibit-read-only)
2278 inhibit-read-only t))
2279 (t (error
2280 (substitute-command-keys allout-structure-unruly-deletion-message)))))
2281;;;_ > allout-item-icon-key-handler ()
2282(defun allout-item-icon-key-handler ()
2283 "Catchall handling of key bindings in item icon/cue hot-spots.
2284
2285Applies `allout-hotspot-key-handler' and calls the result, if any, as an
2286interactive command."
2287
2288 (interactive)
2289 (let* ((mapped-binding (allout-hotspot-key-handler)))
2290 (when mapped-binding
2291 (call-interactively mapped-binding))))
2292
2293;;;_ : Status
2294;;;_ . allout-item-location (item-widget)
2295(defun allout-item-location (item-widget)
2296 "Location of the start of the item's text."
2297 (overlay-start (widget-get item-widget :span-overlay)))
2298
2299;;;_ : Icon management
2300;;;_ > allout-fetch-icon-image (name)
2301(defun allout-fetch-icon-image (name)
2302 "Fetch allout icon for symbol NAME.
2303
2304We use a caching strategy, so the caller doesn't need to do so."
2305 (let* ((types allout-widgets-icon-types)
2306 (use-dir (if (equal (allout-frame-property nil 'background-mode)
2307 'light)
2308 allout-widgets-icons-light-subdir
2309 allout-widgets-icons-dark-subdir))
2310 (key (list name use-dir))
2311 (got (assoc key allout-widgets-icons-cache)))
2312 (if got
2313 ;; display system shows only first of subsequent adjacent
2314 ;; `eq'-identical repeats - use copies to avoid this problem.
2315 (allout-widgets-copy-list (cadr got))
2316 (while (and types (not got))
2317 (setq got
2318 (allout-find-image
2319 (list (append (list :type (car types)
2320 :file (concat use-dir
2321 (symbol-name name)
2322 "." (symbol-name
2323 (car types))))
2324 (if (featurep 'xemacs)
2325 allout-widgets-item-image-properties-xemacs
2326 allout-widgets-item-image-properties-emacs)
2327 ))))
2328 (setq types (cdr types)))
2329 (if got
2330 (push (list key got) allout-widgets-icons-cache))
2331 got)))
2332
2333;;;_ : Miscellaneous
2334;;;_ > allout-elapsed-time-seconds (triple)
2335(defun allout-elapsed-time-seconds (end start)
2336 "Return seconds between `current-time' style time START/END triples."
2337 (let ((elapsed (time-subtract end start)))
35837f51 2338 (float-time elapsed)))
aac7a935
KM
2339;;;_ > allout-frame-property (frame property)
2340(defalias 'allout-frame-property
2341 (cond ((fboundp 'frame-parameter)
2342 'frame-parameter)
2343 ((fboundp 'frame-property)
2344 'frame-property)
2345 (t nil)))
2346;;;_ > allout-find-image (specs)
2347(defalias 'allout-find-image
2348 (if (fboundp 'find-image)
2349 'find-image
2350 nil) ; aka, not-yet-implemented for xemacs.
2351)
2352;;;_ > allout-widgets-copy-list (list)
2353(defun allout-widgets-copy-list (list)
2354 ;; duplicated from cl.el 'copy-list' as of 2008-08-17
2355 "Return a copy of LIST, which may be a dotted list.
2356The elements of LIST are not copied, just the list structure itself."
2357 (if (consp list)
2358 (let ((res nil))
2359 (while (consp list) (push (pop list) res))
2360 (prog1 (nreverse res) (setcdr res list)))
2361 (car list)))
d806ab68
KM
2362;;;_ . allout-widgets-count-buttons-in-region (start end)
2363(defun allout-widgets-count-buttons-in-region (start end)
2364 "Debugging/diagnostic tool - count overlays with 'button' property in region."
2365 (interactive "r")
2366 (setq start (or start (point-min))
2367 end (or end (point-max)))
2368 (if (> start end) (let ((interim start)) (setq start end end interim)))
2369 (let ((button-overlays (delq nil
2370 (mapcar (function (lambda (o)
2371 (if (overlay-get o 'button)
2372 o)))
2373 (overlays-in start end)))))
2374 (length button-overlays)))
aac7a935
KM
2375
2376;;;_ : Run unit tests:
2377(defun allout-widgets-run-unit-tests ()
2378 (message "Running allout-widget tests...")
2379
2380 (allout-test-range-overlaps)
2381
2382 (message "Running allout-widget tests... Done.")
2383 (sit-for .5))
2384
2385(when allout-widgets-run-unit-tests-on-load
2386 (allout-widgets-run-unit-tests))
2387
2388;;;_ : provide
2389(provide 'allout-widgets)
2390
2391;;;_. Local emacs vars.
2392;;;_ , Local variables:
2393;;;_ , allout-layout: (-1 : 0)
2394;;;_ , End: