tree-widget.el 31 KB

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