eieio-custom.el 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. ;;; eieio-custom.el -- eieio object customization
  2. ;; Copyright (C) 1999-2001, 2005, 2007-2012 Free Software Foundation, Inc.
  3. ;; Author: Eric M. Ludlam <zappo@gnu.org>
  4. ;; Version: 0.2
  5. ;; Keywords: OO, lisp
  6. ;; Package: eieio
  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 contains support customization of eieio objects. Enabling
  21. ;; your object to be customizable requires use of the slot attribute
  22. ;; `:custom'.
  23. (require 'eieio)
  24. (require 'widget)
  25. (require 'wid-edit)
  26. (require 'custom)
  27. ;;; Compatibility
  28. ;; (eval-and-compile
  29. ;; (if (featurep 'xemacs)
  30. ;; (defalias 'eieio-overlay-lists (lambda () (list (extent-list))))
  31. ;; (defalias 'eieio-overlay-lists 'overlay-lists)))
  32. ;;; Code:
  33. (defclass eieio-widget-test-class nil
  34. ((a-string :initarg :a-string
  35. :initform "The moose is loose"
  36. :custom string
  37. :label "Amorphous String"
  38. :group (default foo)
  39. :documentation "A string for testing custom.
  40. This is the next line of documentation.")
  41. (listostuff :initarg :listostuff
  42. :initform ("1" "2" "3")
  43. :type list
  44. :custom (repeat (string :tag "Stuff"))
  45. :label "List of Strings"
  46. :group foo
  47. :documentation "A list of stuff.")
  48. (uninitialized :initarg :uninitialized
  49. :type string
  50. :custom string
  51. :documentation "This slot is not initialized.
  52. Used to make sure that custom doesn't barf when it encounters one
  53. of these.")
  54. (a-number :initarg :a-number
  55. :initform 2
  56. :custom integer
  57. :documentation "A number of thingies."))
  58. "A class for testing the widget on.")
  59. (defcustom eieio-widget-test (eieio-widget-test-class "Foo")
  60. "Test variable for editing an object."
  61. :type 'object
  62. :group 'eieio)
  63. (defface eieio-custom-slot-tag-face '((((class color)
  64. (background dark))
  65. (:foreground "light blue"))
  66. (((class color)
  67. (background light))
  68. (:foreground "blue"))
  69. (t (:italic t)))
  70. "Face used for unpushable variable tags."
  71. :group 'custom-faces)
  72. (defvar eieio-wo nil
  73. "Buffer local variable in object customize buffers for the current widget.")
  74. (defvar eieio-co nil
  75. "Buffer local variable in object customize buffers for the current obj.")
  76. (defvar eieio-cog nil
  77. "Buffer local variable in object customize buffers for the current group.")
  78. (defvar eieio-custom-ignore-eieio-co nil
  79. "When true, all customizable slots of the current object are updated.
  80. Updates occur regardless of the current customization group.")
  81. (define-widget 'object-slot 'group
  82. "Abstractly modify a single slot in an object."
  83. :tag "Slot"
  84. :format "%t %v%h\n"
  85. :convert-widget 'widget-types-convert-widget
  86. :value-create 'eieio-slot-value-create
  87. :value-get 'eieio-slot-value-get
  88. :value-delete 'widget-children-value-delete
  89. :validate 'widget-children-validate
  90. :match 'eieio-object-match ;; same
  91. )
  92. (defun eieio-slot-value-create (widget)
  93. "Create the value of WIDGET."
  94. (let ((chil nil))
  95. (setq chil (cons
  96. (widget-create-child-and-convert
  97. widget (widget-get widget :childtype)
  98. :tag ""
  99. :value (widget-get widget :value))
  100. chil))
  101. (widget-put widget :children chil)))
  102. (defun eieio-slot-value-get (widget)
  103. "Get the value of WIDGET."
  104. (widget-value (car (widget-get widget :children))))
  105. (defun eieio-custom-toggle-hide (widget)
  106. "Toggle visibility of WIDGET."
  107. (let ((vc (car (widget-get widget :children))))
  108. (cond ((eq (widget-get vc :eieio-custom-state) 'hidden)
  109. (widget-put vc :eieio-custom-state 'visible)
  110. (widget-put vc :value-face (widget-get vc :orig-face)))
  111. (t
  112. (widget-put vc :eieio-custom-state 'hidden)
  113. (widget-put vc :orig-face (widget-get vc :value-face))
  114. (widget-put vc :value-face 'invisible)
  115. ))
  116. (widget-value-set vc (widget-value vc))))
  117. (defun eieio-custom-toggle-parent (widget &rest ignore)
  118. "Toggle visibility of parent of WIDGET.
  119. Optional argument IGNORE is an extraneous parameter."
  120. (eieio-custom-toggle-hide (widget-get widget :parent)))
  121. (define-widget 'object-edit 'group
  122. "Abstractly modify a CLOS object."
  123. :tag "Object"
  124. :format "%v"
  125. :convert-widget 'widget-types-convert-widget
  126. :value-create 'eieio-object-value-create
  127. :value-get 'eieio-object-value-get
  128. :value-delete 'widget-children-value-delete
  129. :validate 'widget-children-validate
  130. :match 'eieio-object-match
  131. :clone-object-children nil
  132. )
  133. (defun eieio-object-match (widget value)
  134. "Match info for WIDGET against VALUE."
  135. ;; Write me
  136. t)
  137. (defun eieio-filter-slot-type (widget slottype)
  138. "Filter WIDGETs SLOTTYPE."
  139. (if (widget-get widget :clone-object-children)
  140. slottype
  141. (cond ((eq slottype 'object)
  142. 'object-edit)
  143. ((and (listp slottype)
  144. (eq (car slottype) 'object))
  145. (cons 'object-edit (cdr slottype)))
  146. ((equal slottype '(repeat object))
  147. '(repeat object-edit))
  148. ((and (listp slottype)
  149. (equal (car slottype) 'repeat)
  150. (listp (car (cdr slottype)))
  151. (equal (car (car (cdr slottype))) 'object))
  152. (list 'repeat
  153. (cons 'object-edit
  154. (cdr (car (cdr slottype))))))
  155. (t slottype))))
  156. (defun eieio-object-value-create (widget)
  157. "Create the value of WIDGET."
  158. (if (not (widget-get widget :value))
  159. (widget-put widget
  160. :value (cond ((widget-get widget :objecttype)
  161. (funcall (class-constructor
  162. (widget-get widget :objecttype))
  163. "Custom-new"))
  164. ((widget-get widget :objectcreatefcn)
  165. (funcall (widget-get widget :objectcreatefcn)))
  166. (t (error "No create method specified")))))
  167. (let* ((chil nil)
  168. (obj (widget-get widget :value))
  169. (master-group (widget-get widget :eieio-group))
  170. (cv (class-v (object-class-fast obj)))
  171. (slots (aref cv class-public-a))
  172. (flabel (aref cv class-public-custom-label))
  173. (fgroup (aref cv class-public-custom-group))
  174. (fdoc (aref cv class-public-doc))
  175. (fcust (aref cv class-public-custom)))
  176. ;; First line describes the object, but may not editable.
  177. (if (widget-get widget :eieio-show-name)
  178. (setq chil (cons (widget-create-child-and-convert
  179. widget 'string :tag "Object "
  180. :sample-face 'bold
  181. (object-name-string obj))
  182. chil)))
  183. ;; Display information about the group being shown
  184. (when master-group
  185. (let ((groups (class-option (object-class-fast obj) :custom-groups)))
  186. (widget-insert "Groups:")
  187. (while groups
  188. (widget-insert " ")
  189. (if (eq (car groups) master-group)
  190. (widget-insert "*" (capitalize (symbol-name master-group)) "*")
  191. (widget-create 'push-button
  192. :thing (cons obj (car groups))
  193. :notify (lambda (widget &rest stuff)
  194. (eieio-customize-object
  195. (car (widget-get widget :thing))
  196. (cdr (widget-get widget :thing))))
  197. (capitalize (symbol-name (car groups)))))
  198. (setq groups (cdr groups)))
  199. (widget-insert "\n\n")))
  200. ;; Loop over all the slots, creating child widgets.
  201. (while slots
  202. ;; Output this slot if it has a customize flag associated with it.
  203. (when (and (car fcust)
  204. (or (not master-group) (member master-group (car fgroup)))
  205. (slot-boundp obj (car slots)))
  206. ;; In this case, this slot has a custom type. Create its
  207. ;; children widgets.
  208. (let ((type (eieio-filter-slot-type widget (car fcust)))
  209. (stuff nil))
  210. ;; This next bit is an evil hack to get some EDE functions
  211. ;; working the way I like.
  212. (if (and (listp type)
  213. (setq stuff (member :slotofchoices type)))
  214. (let ((choices (eieio-oref obj (car (cdr stuff))))
  215. (newtype nil))
  216. (while (not (eq (car type) :slotofchoices))
  217. (setq newtype (cons (car type) newtype)
  218. type (cdr type)))
  219. (while choices
  220. (setq newtype (cons (list 'const (car choices))
  221. newtype)
  222. choices (cdr choices)))
  223. (setq type (nreverse newtype))))
  224. (setq chil (cons (widget-create-child-and-convert
  225. widget 'object-slot
  226. :childtype type
  227. :sample-face 'eieio-custom-slot-tag-face
  228. :tag
  229. (concat
  230. (make-string
  231. (or (widget-get widget :indent) 0)
  232. ? )
  233. (if (car flabel)
  234. (car flabel)
  235. (let ((s (symbol-name
  236. (or
  237. (class-slot-initarg
  238. (object-class-fast obj)
  239. (car slots))
  240. (car slots)))))
  241. (capitalize
  242. (if (string-match "^:" s)
  243. (substring s (match-end 0))
  244. s)))))
  245. :value (slot-value obj (car slots))
  246. :doc (if (car fdoc) (car fdoc)
  247. "Slot not Documented.")
  248. :eieio-custom-visibility 'visible
  249. )
  250. chil))
  251. )
  252. )
  253. (setq slots (cdr slots)
  254. fdoc (cdr fdoc)
  255. fcust (cdr fcust)
  256. flabel (cdr flabel)
  257. fgroup (cdr fgroup)))
  258. (widget-put widget :children (nreverse chil))
  259. ))
  260. (defun eieio-object-value-get (widget)
  261. "Get the value of WIDGET."
  262. (let* ((obj (widget-get widget :value))
  263. (master-group eieio-cog)
  264. (cv (class-v (object-class-fast obj)))
  265. (fgroup (aref cv class-public-custom-group))
  266. (wids (widget-get widget :children))
  267. (name (if (widget-get widget :eieio-show-name)
  268. (car (widget-apply (car wids) :value-inline))
  269. nil))
  270. (chil (if (widget-get widget :eieio-show-name)
  271. (nthcdr 1 wids) wids))
  272. (cv (class-v (object-class-fast obj)))
  273. (slots (aref cv class-public-a))
  274. (fcust (aref cv class-public-custom)))
  275. ;; If there are any prefix widgets, clear them.
  276. ;; -- None yet
  277. ;; Create a batch of initargs for each slot.
  278. (while (and slots chil)
  279. (if (and (car fcust)
  280. (or eieio-custom-ignore-eieio-co
  281. (not master-group) (member master-group (car fgroup)))
  282. (slot-boundp obj (car slots)))
  283. (progn
  284. ;; Only customized slots have widgets
  285. (let ((eieio-custom-ignore-eieio-co t))
  286. (eieio-oset obj (car slots)
  287. (car (widget-apply (car chil) :value-inline))))
  288. (setq chil (cdr chil))))
  289. (setq slots (cdr slots)
  290. fgroup (cdr fgroup)
  291. fcust (cdr fcust)))
  292. ;; Set any name updates on it.
  293. (if name (aset obj object-name name))
  294. ;; This is the same object we had before.
  295. obj))
  296. (defmethod eieio-done-customizing ((obj eieio-default-superclass))
  297. "When applying change to a widget, call this method.
  298. This method is called by the default widget-edit commands.
  299. User made commands should also call this method when applying changes.
  300. Argument OBJ is the object that has been customized."
  301. nil)
  302. ;;;###autoload
  303. (defun customize-object (obj &optional group)
  304. "Customize OBJ in a custom buffer.
  305. Optional argument GROUP is the sub-group of slots to display."
  306. (eieio-customize-object obj group))
  307. (defmethod eieio-customize-object ((obj eieio-default-superclass)
  308. &optional group)
  309. "Customize OBJ in a specialized custom buffer.
  310. To override call the `eieio-custom-widget-insert' to just insert the
  311. object widget.
  312. Optional argument GROUP specifies a subgroup of slots to edit as a symbol.
  313. These groups are specified with the `:group' slot flag."
  314. ;; Insert check for multiple edits here.
  315. (let* ((g (or group 'default)))
  316. (switch-to-buffer (get-buffer-create
  317. (concat "*CUSTOMIZE "
  318. (object-name obj) " "
  319. (symbol-name g) "*")))
  320. (toggle-read-only -1)
  321. (kill-all-local-variables)
  322. (erase-buffer)
  323. (let ((all (overlay-lists)))
  324. ;; Delete all the overlays.
  325. (mapc 'delete-overlay (car all))
  326. (mapc 'delete-overlay (cdr all)))
  327. ;; Add an apply reset option at the top of the buffer.
  328. (eieio-custom-object-apply-reset obj)
  329. (widget-insert "\n\n")
  330. (widget-insert "Edit object " (object-name obj) "\n\n")
  331. ;; Create the widget editing the object.
  332. (make-local-variable 'eieio-wo)
  333. (setq eieio-wo (eieio-custom-widget-insert obj :eieio-group g))
  334. ;;Now generate the apply buttons
  335. (widget-insert "\n")
  336. (eieio-custom-object-apply-reset obj)
  337. ;; Now initialize the buffer
  338. (use-local-map widget-keymap)
  339. (widget-setup)
  340. ;;(widget-minor-mode)
  341. (goto-char (point-min))
  342. (widget-forward 3)
  343. (make-local-variable 'eieio-co)
  344. (setq eieio-co obj)
  345. (make-local-variable 'eieio-cog)
  346. (setq eieio-cog group)))
  347. (defmethod eieio-custom-object-apply-reset ((obj eieio-default-superclass))
  348. "Insert an Apply and Reset button into the object editor.
  349. Argument OBJ is the object being customized."
  350. (widget-create 'push-button
  351. :notify (lambda (&rest ignore)
  352. (widget-apply eieio-wo :value-get)
  353. (eieio-done-customizing eieio-co)
  354. (bury-buffer))
  355. "Accept")
  356. (widget-insert " ")
  357. (widget-create 'push-button
  358. :notify (lambda (&rest ignore)
  359. ;; I think the act of getting it sets
  360. ;; its value through the get function.
  361. (message "Applying Changes...")
  362. (widget-apply eieio-wo :value-get)
  363. (eieio-done-customizing eieio-co)
  364. (message "Applying Changes...Done"))
  365. "Apply")
  366. (widget-insert " ")
  367. (widget-create 'push-button
  368. :notify (lambda (&rest ignore)
  369. (message "Resetting")
  370. (eieio-customize-object eieio-co eieio-cog))
  371. "Reset")
  372. (widget-insert " ")
  373. (widget-create 'push-button
  374. :notify (lambda (&rest ignore)
  375. (bury-buffer))
  376. "Cancel"))
  377. (defmethod eieio-custom-widget-insert ((obj eieio-default-superclass)
  378. &rest flags)
  379. "Insert the widget used for editing object OBJ in the current buffer.
  380. Arguments FLAGS are widget compatible flags.
  381. Must return the created widget."
  382. (apply 'widget-create 'object-edit :value obj flags))
  383. (define-widget 'object 'object-edit
  384. "Instance of a CLOS class."
  385. :format "%{%t%}:\n%v"
  386. :value-to-internal 'eieio-object-value-to-abstract
  387. :value-to-external 'eieio-object-abstract-to-value
  388. :clone-object-children t
  389. )
  390. (defun eieio-object-value-to-abstract (widget value)
  391. "For WIDGET, convert VALUE to an abstract /safe/ representation."
  392. (if (eieio-object-p value) value
  393. (if (null value) value
  394. nil)))
  395. (defun eieio-object-abstract-to-value (widget value)
  396. "For WIDGET, convert VALUE from an abstract /safe/ representation."
  397. value)
  398. ;;; customization group functions
  399. ;;
  400. ;; These functions provide the ability to create dynamic menus to
  401. ;; customize specific sections of an object. They do not hook directly
  402. ;; into a filter, but can be used to create easymenu vectors.
  403. (defmethod eieio-customize-object-group ((obj eieio-default-superclass))
  404. "Create a list of vectors for customizing sections of OBJ."
  405. (mapcar (lambda (group)
  406. (vector (concat "Group " (symbol-name group))
  407. (list 'customize-object obj (list 'quote group))
  408. t))
  409. (class-option (object-class-fast obj) :custom-groups)))
  410. (defvar eieio-read-custom-group-history nil
  411. "History for the custom group reader.")
  412. (defmethod eieio-read-customization-group ((obj eieio-default-superclass))
  413. "Do a completing read on the name of a customization group in OBJ.
  414. Return the symbol for the group, or nil"
  415. (let ((g (class-option (object-class-fast obj) :custom-groups)))
  416. (if (= (length g) 1)
  417. (car g)
  418. ;; Make the association list
  419. (setq g (mapcar (lambda (g) (cons (symbol-name g) g)) g))
  420. (cdr (assoc
  421. (completing-read (concat (oref obj name) " Custom Group: ")
  422. g nil t nil 'eieio-read-custom-group-history)
  423. g)))))
  424. (provide 'eieio-custom)
  425. ;; Local variables:
  426. ;; generated-autoload-file: "eieio.el"
  427. ;; End:
  428. ;;; eieio-custom.el ends here