eieio-custom.el 17 KB

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