tree-widget.el 31 KB

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