Improve skeleton docstrings
[emacs.git] / lisp / tree-widget.el
blob0ffb8dc96dc1d97dbf3282830bb4655c09ffbbe4
1 ;;; tree-widget.el --- Tree widget
3 ;; Copyright (C) 2004-2016 Free Software Foundation, Inc.
5 ;; Author: David Ponce <david@dponce.com>
6 ;; Maintainer: David Ponce <david@dponce.com>
7 ;; Created: 16 Feb 2001
8 ;; Keywords: extensions
10 ;; This file is part of GNU Emacs
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;; Commentary:
27 ;; This library provide a tree widget useful to display data
28 ;; structures organized in a hierarchical order.
30 ;; The following properties are specific to the tree widget:
32 ;; :open
33 ;; Set to non-nil to expand the tree. By default the tree is
34 ;; collapsed.
36 ;; :node
37 ;; Specify the widget used to represent the value of a tree node.
38 ;; By default this is an `item' widget which displays the
39 ;; tree-widget :tag property value if defined, or a string
40 ;; representation of the tree-widget value.
42 ;; :keep
43 ;; Specify a list of properties to keep when the tree is collapsed
44 ;; so they can be recovered when the tree is expanded. This
45 ;; property can be used in child widgets too.
47 ;; :expander (obsoletes :dynargs)
48 ;; Specify a function to be called to dynamically provide the
49 ;; tree's children in response to an expand request. This function
50 ;; will be passed the tree widget and must return a list of child
51 ;; widgets. Child widgets returned by the :expander function are
52 ;; stored in the :args property of the tree widget.
54 ;; :expander-p
55 ;; Specify a predicate which must return non-nil to indicate that
56 ;; the :expander function above has to be called. By default, to
57 ;; speed up successive expand requests, the :expander-p predicate
58 ;; return non-nil when the :args value is nil. So, by default, to
59 ;; refresh child values, it is necessary to set the :args property
60 ;; to nil, then redraw the tree.
62 ;; :open-icon (default `tree-widget-open-icon')
63 ;; :close-icon (default `tree-widget-close-icon')
64 ;; :empty-icon (default `tree-widget-empty-icon')
65 ;; :leaf-icon (default `tree-widget-leaf-icon')
66 ;; Those properties define the icon widgets associated to tree
67 ;; nodes. Icon widgets must derive from the `tree-widget-icon'
68 ;; widget. The :tag and :glyph-name property values are
69 ;; respectively used when drawing the text and graphic
70 ;; representation of the tree. The :tag value must be a string
71 ;; that represent a node icon, like "[+]" for example. The
72 ;; :glyph-name value must the name of an image found in the current
73 ;; theme, like "close" for example (see also the variable
74 ;; `tree-widget-theme').
76 ;; :guide (default `tree-widget-guide')
77 ;; :end-guide (default `tree-widget-end-guide')
78 ;; :no-guide (default `tree-widget-no-guide')
79 ;; :handle (default `tree-widget-handle')
80 ;; :no-handle (default `tree-widget-no-handle')
81 ;; Those properties define `item'-like widgets used to draw the
82 ;; tree guide lines. The :tag property value is used when drawing
83 ;; the text representation of the tree. The graphic look and feel
84 ;; is given by the images named "guide", "no-guide", "end-guide",
85 ;; "handle", and "no-handle" found in the current theme (see also
86 ;; the variable `tree-widget-theme').
88 ;; These are the default :tag values for icons, and guide lines:
90 ;; open-icon "[-]"
91 ;; close-icon "[+]"
92 ;; empty-icon "[X]"
93 ;; leaf-icon ""
94 ;; guide " |"
95 ;; no-guide " "
96 ;; end-guide " `"
97 ;; handle "-"
98 ;; no-handle " "
100 ;; The text representation of a tree looks like this:
102 ;; [-] 1 (open-icon :node)
103 ;; |-[+] 1.0 (guide+handle+close-icon :node)
104 ;; |-[X] 1.1 (guide+handle+empty-icon :node)
105 ;; `-[-] 1.2 (end-guide+handle+open-icon :node)
106 ;; |- 1.2.1 (no-guide+no-handle+guide+handle+leaf-icon leaf)
107 ;; `- 1.2.2 (no-guide+no-handle+end-guide+handle+leaf-icon leaf)
109 ;; By default, images will be used instead of strings to draw a
110 ;; nice-looking tree. See the `tree-widget-image-enable',
111 ;; `tree-widget-themes-directory', and `tree-widget-theme' options for
112 ;; more details.
114 ;;; History:
117 ;;; Code:
118 (require 'wid-edit)
120 ;;; Customization
122 (defgroup tree-widget nil
123 "Customization support for the Tree Widget library."
124 :version "22.1"
125 :group 'widgets)
127 (defcustom tree-widget-image-enable (if (fboundp 'display-images-p)
128 (display-images-p))
129 "Non-nil means that tree-widget will try to use images."
130 :type 'boolean
131 :group 'tree-widget)
133 (defvar tree-widget-themes-load-path
134 '(load-path
135 (let ((dir (if (fboundp 'locate-data-directory)
136 (locate-data-directory "tree-widget") ;; XEmacs
137 data-directory)))
138 (and dir (list dir (expand-file-name "images" dir))))
140 "List of locations in which to search for the themes sub-directory.
141 Each element is an expression that will be recursively evaluated until
142 it returns a single directory or a list of directories.
143 The default is to search in the `load-path' first, then in the
144 \"images\" sub directory in the data directory, then in the data
145 directory.
146 The data directory is the value of the variable `data-directory' on
147 Emacs, and what `(locate-data-directory \"tree-widget\")' returns on
148 XEmacs.")
150 (defcustom tree-widget-themes-directory "tree-widget"
151 "Name of the directory in which to look for an image theme.
152 When nil use the directory where the tree-widget library is located.
153 When it is a relative name, search in all occurrences of that sub
154 directory in the path specified by `tree-widget-themes-load-path'.
155 The default is to use the \"tree-widget\" relative name."
156 :type '(choice (const :tag "Default" "tree-widget")
157 (const :tag "Where is this library" nil)
158 (directory :format "%{%t%}:\n%v"))
159 :group 'tree-widget)
161 (defcustom tree-widget-theme nil
162 "Name of the theme in which to look for images.
163 This is a sub directory of the themes directory specified by the
164 `tree-widget-themes-directory' option.
165 The default theme is \"default\". When an image is not found in a
166 theme, it is searched in its parent theme.
168 A complete theme must at least contain images with these file names
169 with a supported extension (see also `tree-widget-image-formats'):
171 \"guide\"
172 A vertical guide line.
173 \"no-guide\"
174 An invisible vertical guide line.
175 \"end-guide\"
176 End of a vertical guide line.
177 \"handle\"
178 Horizontal guide line that joins the vertical guide line to an icon.
179 \"no-handle\"
180 An invisible handle.
182 Plus images whose name is given by the :glyph-name property of the
183 icon widgets used to draw the tree. By default these images are used:
185 \"open\"
186 Icon associated to an expanded tree.
187 \"close\"
188 Icon associated to a collapsed tree.
189 \"empty\"
190 Icon associated to an expanded tree with no child.
191 \"leaf\"
192 Icon associated to a leaf node."
193 :type '(choice (const :tag "Default" nil)
194 (string :tag "Name"))
195 :group 'tree-widget)
197 (defcustom tree-widget-image-properties-emacs
198 '(:ascent center :mask (heuristic t))
199 "Default properties of Emacs images."
200 :type 'plist
201 :group 'tree-widget)
203 (defcustom tree-widget-image-properties-xemacs
205 "Default properties of XEmacs images."
206 :type 'plist
207 :group 'tree-widget)
209 (defcustom tree-widget-space-width 0.5
210 "Amount of space between an icon image and a node widget.
211 Must be a valid space :width display property.
212 See Info node `(elisp)Specified Space'."
213 :group 'tree-widget
214 :type '(choice (number :tag "Multiple of normal character width")
215 sexp))
217 ;;; Image support
219 (eval-and-compile ;; Emacs/XEmacs compatibility stuff
220 (cond
221 ;; XEmacs
222 ((featurep 'xemacs)
223 (defsubst tree-widget-use-image-p ()
224 "Return non-nil if image support is currently enabled."
225 (and tree-widget-image-enable
226 widget-glyph-enable
227 (console-on-window-system-p)))
228 (defsubst tree-widget-create-image (type file &optional props)
229 "Create an image of type TYPE from FILE, and return it.
230 Give the image the specified properties PROPS."
231 (apply 'make-glyph `([,type :file ,file ,@props])))
232 (defsubst tree-widget-image-formats ()
233 "Return the alist of image formats/file name extensions.
234 See also the option `widget-image-file-name-suffixes'."
235 (delq nil
236 (mapcar
237 #'(lambda (fmt)
238 (and (valid-image-instantiator-format-p (car fmt)) fmt))
239 widget-image-file-name-suffixes)))
241 ;; Emacs
243 (defsubst tree-widget-use-image-p ()
244 "Return non-nil if image support is currently enabled."
245 (and tree-widget-image-enable
246 widget-image-enable
247 (display-images-p)))
248 (defsubst tree-widget-create-image (type file &optional props)
249 "Create an image of type TYPE from FILE, and return it.
250 Give the image the specified properties PROPS."
251 (apply 'create-image `(,file ,type nil ,@props)))
252 (defsubst tree-widget-image-formats ()
253 "Return the alist of image formats/file name extensions.
254 See also the option `widget-image-conversion'."
255 (delq nil
256 (mapcar
257 #'(lambda (fmt)
258 (and (image-type-available-p (car fmt)) fmt))
259 widget-image-conversion)))
263 ;; Buffer local cache of theme data.
264 (defvar tree-widget--theme nil)
266 (defsubst tree-widget-theme-name ()
267 "Return the current theme name, or nil if no theme is active."
268 (and tree-widget--theme (car (aref tree-widget--theme 0))))
270 (defsubst tree-widget-set-parent-theme (name)
271 "Set to NAME the parent theme of the current theme.
272 The default parent theme is the \"default\" theme."
273 (unless (member name (aref tree-widget--theme 0))
274 (aset tree-widget--theme 0
275 (append (aref tree-widget--theme 0) (list name)))
276 ;; Load the theme setup from the first directory where the theme
277 ;; is found.
278 (catch 'found
279 (dolist (dir (tree-widget-themes-path))
280 (setq dir (expand-file-name name dir))
281 (when (file-accessible-directory-p dir)
282 (throw 'found
283 (load (expand-file-name
284 "tree-widget-theme-setup" dir) t)))))))
286 (defun tree-widget-set-theme (&optional name)
287 "In the current buffer, set the theme to use for images.
288 The current buffer must be where the tree widget is drawn.
289 Optional argument NAME is the name of the theme to use. It defaults
290 to the value of the variable `tree-widget-theme'.
291 Does nothing if NAME is already the current theme.
293 If there is a \"tree-widget-theme-setup\" library in the theme
294 directory, load it to setup a parent theme or the images properties.
295 Typically it should contain something like this:
297 (tree-widget-set-parent-theme \"my-parent-theme\")
298 (tree-widget-set-image-properties
299 (if (featurep \\='xemacs)
300 \\='(:ascent center)
301 \\='(:ascent center :mask (heuristic t))
303 (or name (setq name (or tree-widget-theme "default")))
304 (unless (string-equal name (tree-widget-theme-name))
305 (set (make-local-variable 'tree-widget--theme)
306 (make-vector 4 nil))
307 (tree-widget-set-parent-theme name)
308 (tree-widget-set-parent-theme "default")))
310 (defun tree-widget--locate-sub-directory (name path &optional found)
311 "Locate all occurrences of the sub-directory NAME in PATH.
312 Return a list of absolute directory names in reverse order, or nil if
313 not found."
314 (condition-case err
315 (dolist (elt path)
316 (setq elt (eval elt))
317 (cond
318 ((stringp elt)
319 (and (file-accessible-directory-p
320 (setq elt (expand-file-name name elt)))
321 (push elt found)))
322 (elt
323 (setq found (tree-widget--locate-sub-directory
324 name (if (atom elt) (list elt) elt) found)))))
325 (error
326 (message "In tree-widget--locate-sub-directory: %s"
327 (error-message-string err))))
328 found)
330 (defun tree-widget-themes-path ()
331 "Return the path where to search for a theme.
332 It is specified in variable `tree-widget-themes-directory'.
333 Return a list of absolute directory names, or nil when no directory
334 has been found accessible."
335 (let ((path (aref tree-widget--theme 1)))
336 (cond
337 ;; No directory was found.
338 ((eq path 'void) nil)
339 ;; The list of directories is available in the cache.
340 (path)
341 ;; Use the directory where this library is located.
342 ((null tree-widget-themes-directory)
343 (when (setq path (locate-library "tree-widget"))
344 (setq path (file-name-directory path))
345 (setq path (and (file-accessible-directory-p path)
346 (list path)))
347 ;; Store the result in the cache for later use.
348 (aset tree-widget--theme 1 (or path 'void))
349 path))
350 ;; Check accessibility of absolute directory name.
351 ((file-name-absolute-p tree-widget-themes-directory)
352 (setq path (expand-file-name tree-widget-themes-directory))
353 (setq path (and (file-accessible-directory-p path)
354 (list path)))
355 ;; Store the result in the cache for later use.
356 (aset tree-widget--theme 1 (or path 'void))
357 path)
358 ;; Locate a sub-directory in `tree-widget-themes-load-path'.
360 (setq path (nreverse (tree-widget--locate-sub-directory
361 tree-widget-themes-directory
362 tree-widget-themes-load-path)))
363 ;; Store the result in the cache for later use.
364 (aset tree-widget--theme 1 (or path 'void))
365 path))))
367 (defconst tree-widget--cursors
368 ;; Pointer shapes when the mouse pointer is over inactive
369 ;; tree-widget images. This feature works since Emacs 22, and
370 ;; ignored on older versions, and XEmacs.
372 ("guide" . arrow)
373 ("no-guide" . arrow)
374 ("end-guide" . arrow)
375 ("handle" . arrow)
376 ("no-handle" . arrow)
379 (defsubst tree-widget-set-image-properties (props)
380 "In current theme, set images properties to PROPS.
381 Does nothing if images properties have already been set for that
382 theme."
383 (or (aref tree-widget--theme 2)
384 (aset tree-widget--theme 2 props)))
386 (defsubst tree-widget-image-properties (name)
387 "Return the properties of image NAME in current theme.
388 Default global properties are provided for respectively Emacs and
389 XEmacs in the variables `tree-widget-image-properties-emacs', and
390 `tree-widget-image-properties-xemacs'."
391 ;; Add the pointer shape
392 (cons :pointer
393 (cons (or (cdr (assoc name tree-widget--cursors)) 'hand)
394 (tree-widget-set-image-properties
395 (if (featurep 'xemacs)
396 tree-widget-image-properties-xemacs
397 tree-widget-image-properties-emacs)))))
399 (defun tree-widget-lookup-image (name)
400 "Look up in current theme for an image with NAME.
401 Search first in current theme, then in parent themes (see also the
402 function `tree-widget-set-parent-theme').
403 Return the first image found having a supported format, or nil if not
404 found."
405 (let (file)
406 (catch 'found
407 (dolist (default-directory (tree-widget-themes-path))
408 (dolist (dir (aref tree-widget--theme 0))
409 (dolist (fmt (tree-widget-image-formats))
410 (dolist (ext (cdr fmt))
411 (setq file (expand-file-name (concat name ext) dir))
412 (and (file-readable-p file)
413 (file-regular-p file)
414 (throw 'found
415 (tree-widget-create-image
416 (car fmt) file
417 (tree-widget-image-properties name))))))))
418 nil)))
420 (defun tree-widget-find-image (name)
421 "Find the image with NAME in current theme.
422 NAME is an image file name sans extension.
423 Return the image found, or nil if not found."
424 (when (tree-widget-use-image-p)
425 ;; Ensure there is an active theme.
426 (tree-widget-set-theme (tree-widget-theme-name))
427 (let ((image (assoc name (aref tree-widget--theme 3))))
428 ;; The image NAME is found in the cache.
429 (if image
430 (cdr image)
431 ;; Search the image in current, and default themes.
432 (prog1
433 (setq image (tree-widget-lookup-image name))
434 ;; Store image reference in the cache for later use.
435 (push (cons name image) (aref tree-widget--theme 3))))
438 ;;; Widgets
440 (defun tree-widget-button-click (event)
441 "Move to the position clicked on, and if it is a button, invoke it.
442 EVENT is the mouse event received."
443 (interactive "e")
444 (mouse-set-point event)
445 (let ((pos (widget-event-point event)))
446 (if (get-char-property pos 'button)
447 (widget-button-click event))))
449 (defvar tree-widget-button-keymap
450 (let ((km (make-sparse-keymap)))
451 (if (boundp 'widget-button-keymap)
452 ;; XEmacs
453 (progn
454 (set-keymap-parent km widget-button-keymap)
455 (define-key km [button1] 'tree-widget-button-click))
456 ;; Emacs
457 (set-keymap-parent km widget-keymap)
458 (define-key km [down-mouse-1] 'tree-widget-button-click))
460 "Keymap used inside node buttons.
461 Handle mouse button 1 click on buttons.")
463 (define-widget 'tree-widget-icon 'push-button
464 "Basic widget other tree-widget icons are derived from."
465 :format "%[%t%]"
466 :button-keymap tree-widget-button-keymap ; XEmacs
467 :keymap tree-widget-button-keymap ; Emacs
468 :create 'tree-widget-icon-create
469 :action 'tree-widget-icon-action
470 :help-echo 'tree-widget-icon-help-echo
473 (define-widget 'tree-widget-open-icon 'tree-widget-icon
474 "Icon for an expanded tree-widget node."
475 :tag "[-]"
476 :glyph-name "open"
479 (define-widget 'tree-widget-empty-icon 'tree-widget-icon
480 "Icon for an expanded tree-widget node with no child."
481 :tag "[X]"
482 :glyph-name "empty"
485 (define-widget 'tree-widget-close-icon 'tree-widget-icon
486 "Icon for a collapsed tree-widget node."
487 :tag "[+]"
488 :glyph-name "close"
491 (define-widget 'tree-widget-leaf-icon 'tree-widget-icon
492 "Icon for a tree-widget leaf node."
493 :tag ""
494 :glyph-name "leaf"
495 :button-face 'default
498 (define-widget 'tree-widget-guide 'item
499 "Vertical guide line."
500 :tag " |"
501 ;;:tag-glyph (tree-widget-find-image "guide")
502 :format "%t"
505 (define-widget 'tree-widget-end-guide 'item
506 "End of a vertical guide line."
507 :tag " `"
508 ;;:tag-glyph (tree-widget-find-image "end-guide")
509 :format "%t"
512 (define-widget 'tree-widget-no-guide 'item
513 "Invisible vertical guide line."
514 :tag " "
515 ;;:tag-glyph (tree-widget-find-image "no-guide")
516 :format "%t"
519 (define-widget 'tree-widget-handle 'item
520 "Horizontal guide line that joins a vertical guide line to a node."
521 :tag "-"
522 ;;:tag-glyph (tree-widget-find-image "handle")
523 :format "%t"
526 (define-widget 'tree-widget-no-handle 'item
527 "Invisible handle."
528 :tag " "
529 ;;:tag-glyph (tree-widget-find-image "no-handle")
530 :format "%t"
533 (define-widget 'tree-widget 'default
534 "Tree widget."
535 :format "%v"
536 :convert-widget 'tree-widget-convert-widget
537 :value-get 'widget-value-value-get
538 :value-delete 'widget-children-value-delete
539 :value-create 'tree-widget-value-create
540 :action 'tree-widget-action
541 :help-echo 'tree-widget-help-echo
542 :expander-p 'tree-widget-expander-p
543 :open-icon 'tree-widget-open-icon
544 :close-icon 'tree-widget-close-icon
545 :empty-icon 'tree-widget-empty-icon
546 :leaf-icon 'tree-widget-leaf-icon
547 :guide 'tree-widget-guide
548 :end-guide 'tree-widget-end-guide
549 :no-guide 'tree-widget-no-guide
550 :handle 'tree-widget-handle
551 :no-handle 'tree-widget-no-handle
554 ;;; Widget support functions
556 (defun tree-widget-p (widget)
557 "Return non-nil if WIDGET is a tree-widget."
558 (let ((type (widget-type widget)))
559 (while (and type (not (eq type 'tree-widget)))
560 (setq type (widget-type (get type 'widget-type))))
561 (eq type 'tree-widget)))
563 (defun tree-widget-node (widget)
564 "Return WIDGET's :node child widget.
565 If not found, setup an `item' widget as default.
566 Signal an error if the :node widget is a tree-widget.
567 WIDGET is, or derives from, a tree-widget."
568 (let ((node (widget-get widget :node)))
569 (if node
570 ;; Check that the :node widget is not a tree-widget.
571 (and (tree-widget-p node)
572 (error "Invalid tree-widget :node %S" node))
573 ;; Setup an item widget as default :node.
574 (setq node `(item :tag ,(or (widget-get widget :tag)
575 (widget-princ-to-string
576 (widget-value widget)))))
577 (widget-put widget :node node))
578 node))
580 (defun tree-widget-keep (arg widget)
581 "Save in ARG the WIDGET's properties specified by :keep."
582 (dolist (prop (widget-get widget :keep))
583 (widget-put arg prop (widget-get widget prop))))
585 (defun tree-widget-children-value-save (widget &optional args node)
586 "Save WIDGET children values.
587 WIDGET is, or derives from, a tree-widget.
588 Children properties and values are saved in ARGS if non-nil, else in
589 WIDGET's :args property value. Properties and values of the
590 WIDGET's :node sub-widget are saved in NODE if non-nil, else in
591 WIDGET's :node sub-widget."
592 (let ((args (cons (or node (widget-get widget :node))
593 (or args (widget-get widget :args))))
594 (children (widget-get widget :children))
595 arg child)
596 (while (and args children)
597 (setq arg (car args)
598 args (cdr args)
599 child (car children)
600 children (cdr children))
601 (if (tree-widget-p child)
602 ;;;; The child is a tree node.
603 (progn
604 ;; Backtrack :args and :node properties.
605 (widget-put arg :args (widget-get child :args))
606 (widget-put arg :node (widget-get child :node))
607 ;; Save :open property.
608 (widget-put arg :open (widget-get child :open))
609 ;; The node is open.
610 (when (widget-get child :open)
611 ;; Save the widget value.
612 (widget-put arg :value (widget-value child))
613 ;; Save properties specified in :keep.
614 (tree-widget-keep arg child)
615 ;; Save children.
616 (tree-widget-children-value-save
617 child (widget-get arg :args) (widget-get arg :node))))
618 ;;;; Another non tree node.
619 ;; Save the widget value.
620 (widget-put arg :value (widget-value child))
621 ;; Save properties specified in :keep.
622 (tree-widget-keep arg child)))))
624 ;;; Widget creation
626 (defvar tree-widget-before-create-icon-functions nil
627 "Hooks run before to create a tree-widget icon.
628 Each function is passed the icon widget not yet created.
629 The value of the icon widget :node property is a tree :node widget or
630 a leaf node widget, not yet created.
631 This hook can be used to dynamically change properties of the icon and
632 associated node widgets. For example, to dynamically change the look
633 and feel of the tree-widget by changing the values of the :tag
634 and :glyph-name properties of the icon widget.
635 This hook should be local in the buffer setup to display widgets.")
637 (defun tree-widget-icon-create (icon)
638 "Create the ICON widget."
639 (run-hook-with-args 'tree-widget-before-create-icon-functions icon)
640 (widget-put icon :tag-glyph
641 (tree-widget-find-image (widget-get icon :glyph-name)))
642 ;; Ensure there is at least one char to display the image.
643 (and (widget-get icon :tag-glyph)
644 (equal "" (or (widget-get icon :tag) ""))
645 (widget-put icon :tag " "))
646 (widget-default-create icon)
647 ;; Insert space between the icon and the node widget.
648 (insert-char ? 1)
649 (put-text-property
650 (1- (point)) (point)
651 'display (list 'space :width tree-widget-space-width)))
653 (defun tree-widget-convert-widget (widget)
654 "Convert :args as widget types in WIDGET."
655 (let ((tree (widget-types-convert-widget widget)))
656 ;; Compatibility
657 (widget-put tree :expander (or (widget-get tree :expander)
658 (widget-get tree :dynargs)))
659 tree))
661 (defvar widget-glyph-enable) ; XEmacs
663 (defun tree-widget-value-create (tree)
664 "Create the TREE tree-widget."
665 (let* ((node (tree-widget-node tree))
666 (flags (widget-get tree :tree-widget--guide-flags))
667 (indent (widget-get tree :indent))
668 ;; Setup widget's image support. Looking up for images, and
669 ;; setting widgets' :tag-glyph is done here, to allow us to
670 ;; dynamically change the image theme.
671 (widget-image-enable (tree-widget-use-image-p)) ; Emacs
672 (widget-glyph-enable widget-image-enable) ; XEmacs
673 children buttons)
674 (and indent (not (widget-get tree :parent))
675 (insert-char ?\ indent))
676 (if (widget-get tree :open)
677 ;;;; Expanded node.
678 (let ((args (widget-get tree :args))
679 (guide (widget-get tree :guide))
680 (noguide (widget-get tree :no-guide))
681 (endguide (widget-get tree :end-guide))
682 (handle (widget-get tree :handle))
683 (nohandle (widget-get tree :no-handle))
684 (guidi (tree-widget-find-image "guide"))
685 (noguidi (tree-widget-find-image "no-guide"))
686 (endguidi (tree-widget-find-image "end-guide"))
687 (handli (tree-widget-find-image "handle"))
688 (nohandli (tree-widget-find-image "no-handle")))
689 ;; Request children at run time, when requested.
690 (when (and (widget-get tree :expander)
691 (widget-apply tree :expander-p))
692 (setq args (mapcar 'widget-convert
693 (widget-apply tree :expander)))
694 (widget-put tree :args args))
695 ;; Defer the node widget creation after icon creation.
696 (widget-put tree :node (widget-convert node))
697 ;; Create the icon widget for the expanded tree.
698 (push (widget-create-child-and-convert
699 tree (widget-get tree (if args :open-icon :empty-icon))
700 ;; Pass the node widget to child.
701 :node (widget-get tree :node))
702 buttons)
703 ;; Create the tree node widget.
704 (push (widget-create-child tree (widget-get tree :node))
705 children)
706 ;; Update the icon :node with the created node widget.
707 (widget-put (car buttons) :node (car children))
708 ;; Create the tree children.
709 (while args
710 (setq node (car args)
711 args (cdr args))
712 (and indent (insert-char ?\ indent))
713 ;; Insert guide lines elements from previous levels.
714 (dolist (f (reverse flags))
715 (widget-create-child-and-convert
716 tree (if f guide noguide)
717 :tag-glyph (if f guidi noguidi))
718 (widget-create-child-and-convert
719 tree nohandle :tag-glyph nohandli))
720 ;; Insert guide line element for this level.
721 (widget-create-child-and-convert
722 tree (if args guide endguide)
723 :tag-glyph (if args guidi endguidi))
724 ;; Insert the node handle line
725 (widget-create-child-and-convert
726 tree handle :tag-glyph handli)
727 (if (tree-widget-p node)
728 ;; Create a sub-tree node.
729 (push (widget-create-child-and-convert
730 tree node :tree-widget--guide-flags
731 (cons (if args t) flags))
732 children)
733 ;; Create the icon widget for a leaf node.
734 (push (widget-create-child-and-convert
735 tree (widget-get tree :leaf-icon)
736 ;; At this point the node widget isn't yet created.
737 :node (setq node (widget-convert
738 node :tree-widget--guide-flags
739 (cons (if args t) flags)))
740 :tree-widget--leaf-flag t)
741 buttons)
742 ;; Create the leaf node widget.
743 (push (widget-create-child tree node) children)
744 ;; Update the icon :node with the created node widget.
745 (widget-put (car buttons) :node (car children)))))
746 ;;;; Collapsed node.
747 ;; Defer the node widget creation after icon creation.
748 (widget-put tree :node (widget-convert node))
749 ;; Create the icon widget for the collapsed tree.
750 (push (widget-create-child-and-convert
751 tree (widget-get tree :close-icon)
752 ;; Pass the node widget to child.
753 :node (widget-get tree :node))
754 buttons)
755 ;; Create the tree node widget.
756 (push (widget-create-child tree (widget-get tree :node))
757 children)
758 ;; Update the icon :node with the created node widget.
759 (widget-put (car buttons) :node (car children)))
760 ;; Save widget children and buttons. The tree-widget :node child
761 ;; is the first element in :children.
762 (widget-put tree :children (nreverse children))
763 (widget-put tree :buttons buttons)))
765 ;;; Widget callbacks
767 (defsubst tree-widget-leaf-node-icon-p (icon)
768 "Return non-nil if ICON is a leaf node icon.
769 That is, if its :node property value is a leaf node widget."
770 (widget-get icon :tree-widget--leaf-flag))
772 (defun tree-widget-icon-action (icon &optional event)
773 "Handle the ICON widget :action.
774 If ICON :node is a leaf node it handles the :action. The tree-widget
775 parent of ICON handles the :action otherwise.
776 Pass the received EVENT to :action."
777 (let ((node (widget-get icon (if (tree-widget-leaf-node-icon-p icon)
778 :node :parent))))
779 (widget-apply node :action event)))
781 (defun tree-widget-icon-help-echo (icon)
782 "Return the help-echo string of ICON.
783 If ICON :node is a leaf node it handles the :help-echo. The tree-widget
784 parent of ICON handles the :help-echo otherwise."
785 (let* ((node (widget-get icon (if (tree-widget-leaf-node-icon-p icon)
786 :node :parent)))
787 (help-echo (widget-get node :help-echo)))
788 (if (functionp help-echo)
789 (funcall help-echo node)
790 help-echo)))
792 (defvar tree-widget-after-toggle-functions nil
793 "Hooks run after toggling a tree-widget expansion.
794 Each function is passed a tree-widget. If the value of the :open
795 property is non-nil the tree has been expanded, else collapsed.
796 This hook should be local in the buffer setup to display widgets.")
798 (defun tree-widget-action (tree &optional _event)
799 "Handle the :action of the TREE tree-widget.
800 That is, toggle expansion of the TREE tree-widget.
801 Ignore the EVENT argument."
802 (let ((open (not (widget-get tree :open))))
803 (or open
804 ;; Before to collapse the node, save children values so next
805 ;; open can recover them.
806 (tree-widget-children-value-save tree))
807 (widget-put tree :open open)
808 (widget-value-set tree open)
809 (run-hook-with-args 'tree-widget-after-toggle-functions tree)))
811 (defun tree-widget-help-echo (tree)
812 "Return the help-echo string of the TREE tree-widget."
813 (if (widget-get tree :open)
814 "Collapse node"
815 "Expand node"))
817 (defun tree-widget-expander-p (tree)
818 "Return non-nil if the TREE tree-widget :expander has to be called.
819 That is, if TREE :args is nil."
820 (null (widget-get tree :args)))
822 (provide 'tree-widget)
824 ;;; tree-widget.el ends here