tree-widget.el 31 KB

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