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