1 ;;; eieio-custom.el -- eieio object customization
3 ;; Copyright (C) 1999-2001, 2005, 2007-2013 Free Software Foundation,
6 ;; Author: Eric M. Ludlam <zappo@gnu.org>
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
28 ;; This contains support customization of eieio objects. Enabling
29 ;; your object to be customizable requires use of the slot attribute
40 ;; (if (featurep 'xemacs)
41 ;; (defalias 'eieio-overlay-lists (lambda () (list (extent-list))))
42 ;; (defalias 'eieio-overlay-lists 'overlay-lists)))
45 (defclass eieio-widget-test-class nil
46 ((a-string :initarg
:a-string
47 :initform
"The moose is loose"
49 :label
"Amorphous String"
51 :documentation
"A string for testing custom.
52 This is the next line of documentation.")
53 (listostuff :initarg
:listostuff
54 :initform
("1" "2" "3")
56 :custom
(repeat (string :tag
"Stuff"))
57 :label
"List of Strings"
59 :documentation
"A list of stuff.")
60 (uninitialized :initarg
:uninitialized
63 :documentation
"This slot is not initialized.
64 Used to make sure that custom doesn't barf when it encounters one
66 (a-number :initarg
:a-number
69 :documentation
"A number of thingies."))
70 "A class for testing the widget on.")
72 (defcustom eieio-widget-test
(eieio-widget-test-class "Foo")
73 "Test variable for editing an object."
77 (defface eieio-custom-slot-tag-face
'((((class color
)
79 (:foreground
"light blue"))
84 "Face used for unpushable variable tags."
88 "Buffer local variable in object customize buffers for the current widget.")
90 "Buffer local variable in object customize buffers for the current obj.")
92 "Buffer local variable in object customize buffers for the current group.")
94 (defvar eieio-custom-ignore-eieio-co nil
95 "When true, all customizable slots of the current object are updated.
96 Updates occur regardless of the current customization group.")
98 (define-widget 'object-slot
'group
99 "Abstractly modify a single slot in an object."
102 :convert-widget
'widget-types-convert-widget
103 :value-create
'eieio-slot-value-create
104 :value-get
'eieio-slot-value-get
105 :value-delete
'widget-children-value-delete
106 :validate
'widget-children-validate
107 :match
'eieio-object-match
;; same
110 (defun eieio-slot-value-create (widget)
111 "Create the value of WIDGET."
114 (widget-create-child-and-convert
115 widget
(widget-get widget
:childtype
)
117 :value
(widget-get widget
:value
))
119 (widget-put widget
:children chil
)))
121 (defun eieio-slot-value-get (widget)
122 "Get the value of WIDGET."
123 (widget-value (car (widget-get widget
:children
))))
125 (defun eieio-custom-toggle-hide (widget)
126 "Toggle visibility of WIDGET."
127 (let ((vc (car (widget-get widget
:children
))))
128 (cond ((eq (widget-get vc
:eieio-custom-state
) 'hidden
)
129 (widget-put vc
:eieio-custom-state
'visible
)
130 (widget-put vc
:value-face
(widget-get vc
:orig-face
)))
132 (widget-put vc
:eieio-custom-state
'hidden
)
133 (widget-put vc
:orig-face
(widget-get vc
:value-face
))
134 (widget-put vc
:value-face
'invisible
)
136 (widget-value-set vc
(widget-value vc
))))
138 (defun eieio-custom-toggle-parent (widget &rest ignore
)
139 "Toggle visibility of parent of WIDGET.
140 Optional argument IGNORE is an extraneous parameter."
141 (eieio-custom-toggle-hide (widget-get widget
:parent
)))
143 (define-widget 'object-edit
'group
144 "Abstractly modify a CLOS object."
147 :convert-widget
'widget-types-convert-widget
148 :value-create
'eieio-object-value-create
149 :value-get
'eieio-object-value-get
150 :value-delete
'widget-children-value-delete
151 :validate
'widget-children-validate
152 :match
'eieio-object-match
153 :clone-object-children nil
156 (defun eieio-object-match (widget value
)
157 "Match info for WIDGET against VALUE."
161 (defun eieio-filter-slot-type (widget slottype
)
162 "Filter WIDGETs SLOTTYPE."
163 (if (widget-get widget
:clone-object-children
)
165 (cond ((eq slottype
'object
)
167 ((and (listp slottype
)
168 (eq (car slottype
) 'object
))
169 (cons 'object-edit
(cdr slottype
)))
170 ((equal slottype
'(repeat object
))
171 '(repeat object-edit
))
172 ((and (listp slottype
)
173 (equal (car slottype
) 'repeat
)
174 (listp (car (cdr slottype
)))
175 (equal (car (car (cdr slottype
))) 'object
))
178 (cdr (car (cdr slottype
))))))
181 (defun eieio-object-value-create (widget)
182 "Create the value of WIDGET."
183 (if (not (widget-get widget
:value
))
185 :value
(cond ((widget-get widget
:objecttype
)
186 (funcall (class-constructor
187 (widget-get widget
:objecttype
))
189 ((widget-get widget
:objectcreatefcn
)
190 (funcall (widget-get widget
:objectcreatefcn
)))
191 (t (error "No create method specified")))))
193 (obj (widget-get widget
:value
))
194 (master-group (widget-get widget
:eieio-group
))
195 (cv (class-v (eieio--object-class obj
)))
196 (slots (eieio--class-public-a cv
))
197 (flabel (eieio--class-public-custom-label cv
))
198 (fgroup (eieio--class-public-custom-group cv
))
199 (fdoc (eieio--class-public-doc cv
))
200 (fcust (eieio--class-public-custom cv
)))
201 ;; First line describes the object, but may not editable.
202 (if (widget-get widget
:eieio-show-name
)
203 (setq chil
(cons (widget-create-child-and-convert
204 widget
'string
:tag
"Object "
206 (eieio-object-name-string obj
))
208 ;; Display information about the group being shown
210 (let ((groups (class-option (eieio--object-class obj
) :custom-groups
)))
211 (widget-insert "Groups:")
214 (if (eq (car groups
) master-group
)
215 (widget-insert "*" (capitalize (symbol-name master-group
)) "*")
216 (widget-create 'push-button
217 :thing
(cons obj
(car groups
))
218 :notify
(lambda (widget &rest stuff
)
219 (eieio-customize-object
220 (car (widget-get widget
:thing
))
221 (cdr (widget-get widget
:thing
))))
222 (capitalize (symbol-name (car groups
)))))
223 (setq groups
(cdr groups
)))
224 (widget-insert "\n\n")))
225 ;; Loop over all the slots, creating child widgets.
227 ;; Output this slot if it has a customize flag associated with it.
228 (when (and (car fcust
)
229 (or (not master-group
) (member master-group
(car fgroup
)))
230 (slot-boundp obj
(car slots
)))
231 ;; In this case, this slot has a custom type. Create its
233 (let ((type (eieio-filter-slot-type widget
(car fcust
)))
235 ;; This next bit is an evil hack to get some EDE functions
236 ;; working the way I like.
237 (if (and (listp type
)
238 (setq stuff
(member :slotofchoices type
)))
239 (let ((choices (eieio-oref obj
(car (cdr stuff
))))
241 (while (not (eq (car type
) :slotofchoices
))
242 (setq newtype
(cons (car type
) newtype
)
245 (setq newtype
(cons (list 'const
(car choices
))
247 choices
(cdr choices
)))
248 (setq type
(nreverse newtype
))))
249 (setq chil
(cons (widget-create-child-and-convert
252 :sample-face
'eieio-custom-slot-tag-face
256 (or (widget-get widget
:indent
) 0)
260 (let ((s (symbol-name
263 (eieio--object-class obj
)
267 (if (string-match "^:" s
)
268 (substring s
(match-end 0))
270 :value
(slot-value obj
(car slots
))
271 :doc
(if (car fdoc
) (car fdoc
)
272 "Slot not Documented.")
273 :eieio-custom-visibility
'visible
278 (setq slots
(cdr slots
)
282 fgroup
(cdr fgroup
)))
283 (widget-put widget
:children
(nreverse chil
))
286 (defun eieio-object-value-get (widget)
287 "Get the value of WIDGET."
288 (let* ((obj (widget-get widget
:value
))
289 (master-group eieio-cog
)
290 (cv (class-v (eieio--object-class obj
)))
291 (fgroup (eieio--class-public-custom-group cv
))
292 (wids (widget-get widget
:children
))
293 (name (if (widget-get widget
:eieio-show-name
)
294 (car (widget-apply (car wids
) :value-inline
))
296 (chil (if (widget-get widget
:eieio-show-name
)
297 (nthcdr 1 wids
) wids
))
298 (cv (class-v (eieio--object-class obj
)))
299 (slots (eieio--class-public-a cv
))
300 (fcust (eieio--class-public-custom cv
)))
301 ;; If there are any prefix widgets, clear them.
303 ;; Create a batch of initargs for each slot.
304 (while (and slots chil
)
306 (or eieio-custom-ignore-eieio-co
307 (not master-group
) (member master-group
(car fgroup
)))
308 (slot-boundp obj
(car slots
)))
310 ;; Only customized slots have widgets
311 (let ((eieio-custom-ignore-eieio-co t
))
312 (eieio-oset obj
(car slots
)
313 (car (widget-apply (car chil
) :value-inline
))))
314 (setq chil
(cdr chil
))))
315 (setq slots
(cdr slots
)
318 ;; Set any name updates on it.
319 (if name
(setf (eieio--object-name obj
) name
))
320 ;; This is the same object we had before.
323 (defmethod eieio-done-customizing ((obj eieio-default-superclass
))
324 "When applying change to a widget, call this method.
325 This method is called by the default widget-edit commands.
326 User made commands should also call this method when applying changes.
327 Argument OBJ is the object that has been customized."
331 (defun customize-object (obj &optional group
)
332 "Customize OBJ in a custom buffer.
333 Optional argument GROUP is the sub-group of slots to display."
334 (eieio-customize-object obj group
))
336 (defvar eieio-custom-mode-map
337 (let ((map (make-sparse-keymap)))
338 (set-keymap-parent map widget-keymap
)
340 "Keymap for EIEIO Custom mode")
342 (define-derived-mode eieio-custom-mode fundamental-mode
"EIEIO Custom"
343 "Major mode for customizing EIEIO objects.
344 \\{eieio-custom-mode-map}")
346 (defmethod eieio-customize-object ((obj eieio-default-superclass
)
348 "Customize OBJ in a specialized custom buffer.
349 To override call the `eieio-custom-widget-insert' to just insert the
351 Optional argument GROUP specifies a subgroup of slots to edit as a symbol.
352 These groups are specified with the `:group' slot flag."
353 ;; Insert check for multiple edits here.
354 (let* ((g (or group
'default
)))
355 (switch-to-buffer (get-buffer-create
356 (concat "*CUSTOMIZE "
357 (eieio-object-name obj
) " "
358 (symbol-name g
) "*")))
359 (setq buffer-read-only nil
)
360 (kill-all-local-variables)
363 (let ((all (overlay-lists)))
364 ;; Delete all the overlays.
365 (mapc 'delete-overlay
(car all
))
366 (mapc 'delete-overlay
(cdr all
)))
367 ;; Add an apply reset option at the top of the buffer.
368 (eieio-custom-object-apply-reset obj
)
369 (widget-insert "\n\n")
370 (widget-insert "Edit object " (eieio-object-name obj
) "\n\n")
371 ;; Create the widget editing the object.
372 (make-local-variable 'eieio-wo
)
373 (setq eieio-wo
(eieio-custom-widget-insert obj
:eieio-group g
))
374 ;;Now generate the apply buttons
376 (eieio-custom-object-apply-reset obj
)
377 ;; Now initialize the buffer
379 ;;(widget-minor-mode)
380 (goto-char (point-min))
382 (make-local-variable 'eieio-co
)
384 (make-local-variable 'eieio-cog
)
385 (setq eieio-cog group
)))
387 (defmethod eieio-custom-object-apply-reset ((obj eieio-default-superclass
))
388 "Insert an Apply and Reset button into the object editor.
389 Argument OBJ is the object being customized."
390 (widget-create 'push-button
391 :notify
(lambda (&rest ignore
)
392 (widget-apply eieio-wo
:value-get
)
393 (eieio-done-customizing eieio-co
)
397 (widget-create 'push-button
398 :notify
(lambda (&rest ignore
)
399 ;; I think the act of getting it sets
400 ;; its value through the get function.
401 (message "Applying Changes...")
402 (widget-apply eieio-wo
:value-get
)
403 (eieio-done-customizing eieio-co
)
404 (message "Applying Changes...Done"))
407 (widget-create 'push-button
408 :notify
(lambda (&rest ignore
)
409 (message "Resetting")
410 (eieio-customize-object eieio-co eieio-cog
))
413 (widget-create 'push-button
414 :notify
(lambda (&rest ignore
)
418 (defmethod eieio-custom-widget-insert ((obj eieio-default-superclass
)
420 "Insert the widget used for editing object OBJ in the current buffer.
421 Arguments FLAGS are widget compatible flags.
422 Must return the created widget."
423 (apply 'widget-create
'object-edit
:value obj flags
))
425 (define-widget 'object
'object-edit
426 "Instance of a CLOS class."
427 :format
"%{%t%}:\n%v"
428 :value-to-internal
'eieio-object-value-to-abstract
429 :value-to-external
'eieio-object-abstract-to-value
430 :clone-object-children t
433 (defun eieio-object-value-to-abstract (widget value
)
434 "For WIDGET, convert VALUE to an abstract /safe/ representation."
435 (if (eieio-object-p value
) value
436 (if (null value
) value
439 (defun eieio-object-abstract-to-value (widget value
)
440 "For WIDGET, convert VALUE from an abstract /safe/ representation."
444 ;;; customization group functions
446 ;; These functions provide the ability to create dynamic menus to
447 ;; customize specific sections of an object. They do not hook directly
448 ;; into a filter, but can be used to create easymenu vectors.
449 (defmethod eieio-customize-object-group ((obj eieio-default-superclass
))
450 "Create a list of vectors for customizing sections of OBJ."
451 (mapcar (lambda (group)
452 (vector (concat "Group " (symbol-name group
))
453 (list 'customize-object obj
(list 'quote group
))
455 (class-option (eieio--object-class obj
) :custom-groups
)))
457 (defvar eieio-read-custom-group-history nil
458 "History for the custom group reader.")
460 (defmethod eieio-read-customization-group ((obj eieio-default-superclass
))
461 "Do a completing read on the name of a customization group in OBJ.
462 Return the symbol for the group, or nil"
463 (let ((g (class-option (eieio--object-class obj
) :custom-groups
)))
466 ;; Make the association list
467 (setq g
(mapcar (lambda (g) (cons (symbol-name g
) g
)) g
))
469 (completing-read (concat (oref obj name
) " Custom Group: ")
470 g nil t nil
'eieio-read-custom-group-history
)
473 (provide 'eieio-custom
)
475 ;;; eieio-custom.el ends here