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