1 ;;; eieio-custom.el -- eieio object customization -*- lexical-binding:t -*-
3 ;; Copyright (C) 1999-2001, 2005, 2007-2016 Free Software Foundation,
6 ;; Author: Eric M. Ludlam <zappo@gnu.org>
7 ;; Old-Version: 0.2 (using "Version:" made Emacs think this is package
12 ;; This file is part of GNU Emacs.
14 ;; GNU Emacs is free software: you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation, either version 3 of the License, or
17 ;; (at your option) any later version.
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
29 ;; This contains support customization of eieio objects. Enabling
30 ;; your object to be customizable requires use of the slot attribute
41 ;; (if (featurep 'xemacs)
42 ;; (defalias 'eieio-overlay-lists (lambda () (list (extent-list))))
43 ;; (defalias 'eieio-overlay-lists 'overlay-lists)))
46 (defclass eieio-widget-test-class nil
47 ((a-string :initarg
:a-string
48 :initform
"The moose is loose"
50 :label
"Amorphous String"
52 :documentation
"A string for testing custom.
53 This is the next line of documentation.")
54 (listostuff :initarg
:listostuff
55 :initform
("1" "2" "3")
57 :custom
(repeat (string :tag
"Stuff"))
58 :label
"List of Strings"
60 :documentation
"A list of stuff.")
61 (uninitialized :initarg
:uninitialized
64 :documentation
"This slot is not initialized.
65 Used to make sure that custom doesn't barf when it encounters one
67 (a-number :initarg
:a-number
70 :documentation
"A number of thingies."))
71 "A class for testing the widget on.")
73 (defcustom eieio-widget-test
(eieio-widget-test-class)
74 "Test variable for editing an object."
78 (defface eieio-custom-slot-tag-face
'((((class color
)
80 (:foreground
"light blue"))
85 "Face used for unpushable variable tags."
89 "Buffer local variable in object customize buffers for the current widget.")
91 "Buffer local variable in object customize buffers for the current obj.")
93 "Buffer local variable in object customize buffers for the current group.")
95 (defvar eieio-custom-ignore-eieio-co nil
96 "When true, all customizable slots of the current object are updated.
97 Updates occur regardless of the current customization group.")
99 (define-widget 'object-slot
'group
100 "Abstractly modify a single slot in an object."
103 :convert-widget
'widget-types-convert-widget
104 :value-create
'eieio-slot-value-create
105 :value-get
'eieio-slot-value-get
106 :value-delete
'widget-children-value-delete
107 :validate
'widget-children-validate
108 :match
'eieio-object-match
;; same
111 (defun eieio-slot-value-create (widget)
112 "Create the value of WIDGET."
115 (widget-create-child-and-convert
116 widget
(widget-get widget
:childtype
)
118 :value
(widget-get widget
:value
))
120 (widget-put widget
:children chil
)))
122 (defun eieio-slot-value-get (widget)
123 "Get the value of WIDGET."
124 (widget-value (car (widget-get widget
:children
))))
126 (defun eieio-custom-toggle-hide (widget)
127 "Toggle visibility of WIDGET."
128 (let ((vc (car (widget-get widget
:children
))))
129 (cond ((eq (widget-get vc
:eieio-custom-state
) 'hidden
)
130 (widget-put vc
:eieio-custom-state
'visible
)
131 (widget-put vc
:value-face
(widget-get vc
:orig-face
)))
133 (widget-put vc
:eieio-custom-state
'hidden
)
134 (widget-put vc
:orig-face
(widget-get vc
:value-face
))
135 (widget-put vc
:value-face
'invisible
)
137 (widget-value-set vc
(widget-value vc
))))
139 (defun eieio-custom-toggle-parent (widget &rest _
)
140 "Toggle visibility of parent of WIDGET.
141 Optional argument IGNORE is an extraneous parameter."
142 (eieio-custom-toggle-hide (widget-get widget
:parent
)))
144 (define-widget 'object-edit
'group
145 "Abstractly modify a CLOS object."
148 :convert-widget
'widget-types-convert-widget
149 :value-create
'eieio-object-value-create
150 :value-get
'eieio-object-value-get
151 :value-delete
'widget-children-value-delete
152 :validate
'widget-children-validate
153 :match
'eieio-object-match
154 :clone-object-children nil
157 (defun eieio-object-match (_widget _value
)
158 "Match info for WIDGET against VALUE."
162 (defun eieio-filter-slot-type (widget slottype
)
163 "Filter WIDGETs SLOTTYPE."
164 (if (widget-get widget
:clone-object-children
)
166 (cond ((eq slottype
'object
)
168 ((and (listp slottype
)
169 (eq (car slottype
) 'object
))
170 (cons 'object-edit
(cdr slottype
)))
171 ((equal slottype
'(repeat object
))
172 '(repeat object-edit
))
173 ((and (listp slottype
)
174 (equal (car slottype
) 'repeat
)
175 (listp (car (cdr slottype
)))
176 (equal (car (car (cdr slottype
))) 'object
))
179 (cdr (car (cdr slottype
))))))
182 (defun eieio-object-value-create (widget)
183 "Create the value of WIDGET."
184 (if (not (widget-get widget
:value
))
186 :value
(cond ((widget-get widget
:objecttype
)
187 (funcall (eieio--class-constructor
188 (widget-get widget
:objecttype
))
190 ((widget-get widget
:objectcreatefcn
)
191 (funcall (widget-get widget
:objectcreatefcn
)))
192 (t (error "No create method specified")))))
194 (obj (widget-get widget
:value
))
195 (master-group (widget-get widget
:eieio-group
))
196 (cv (eieio--object-class obj
))
197 (slots (eieio--class-slots cv
)))
198 ;; First line describes the object, but may not editable.
199 (if (widget-get widget
:eieio-show-name
)
200 (setq chil
(cons (widget-create-child-and-convert
201 widget
'string
:tag
"Object "
203 (eieio-object-name-string obj
))
205 ;; Display information about the group being shown
207 (let ((groups (eieio--class-option (eieio--object-class obj
)
209 (widget-insert "Groups:")
212 (if (eq (car groups
) master-group
)
213 (widget-insert "*" (capitalize (symbol-name master-group
)) "*")
214 (widget-create 'push-button
215 :thing
(cons obj
(car groups
))
216 :notify
(lambda (widget &rest _
)
217 (eieio-customize-object
218 (car (widget-get widget
:thing
))
219 (cdr (widget-get widget
:thing
))))
220 (capitalize (symbol-name (car groups
)))))
221 (setq groups
(cdr groups
)))
222 (widget-insert "\n\n")))
223 ;; Loop over all the slots, creating child widgets.
224 (dotimes (i (length slots
))
225 (let* ((slot (aref slots i
))
226 (sname (eieio-slot-descriptor-name slot
))
227 (props (cl--slot-descriptor-props slot
)))
228 ;; Output this slot if it has a customize flag associated with it.
229 (when (and (alist-get :custom props
)
230 (or (not master-group
)
231 (member master-group
(alist-get :group props
)))
232 (slot-boundp obj
(cl--slot-descriptor-name slot
)))
233 ;; In this case, this slot has a custom type. Create its
235 (let ((type (eieio-filter-slot-type widget
(alist-get :custom props
)))
237 ;; This next bit is an evil hack to get some EDE functions
238 ;; working the way I like.
239 (if (and (listp type
)
240 (setq stuff
(member :slotofchoices type
)))
241 (let ((choices (eieio-oref obj
(car (cdr stuff
))))
243 (while (not (eq (car type
) :slotofchoices
))
244 (setq newtype
(cons (car type
) newtype
)
247 (setq newtype
(cons (list 'const
(car choices
))
249 choices
(cdr choices
)))
250 (setq type
(nreverse newtype
))))
251 (setq chil
(cons (widget-create-child-and-convert
254 :sample-face
'eieio-custom-slot-tag-face
258 (or (widget-get widget
:indent
) 0)
260 (or (alist-get :label props
)
261 (let ((s (symbol-name
263 (eieio--class-slot-initarg
264 (eieio--object-class obj
)
268 (if (string-match "^:" s
)
269 (substring s
(match-end 0))
271 :value
(slot-value obj sname
)
272 :doc
(or (alist-get :documentation props
)
273 "Slot not Documented.")
274 :eieio-custom-visibility
'visible
278 (widget-put widget
:children
(nreverse chil
))
281 (defun eieio-object-value-get (widget)
282 "Get the value of WIDGET."
283 (let* ((obj (widget-get widget
:value
))
284 (master-group eieio-cog
)
285 (wids (widget-get widget
:children
))
286 (name (if (widget-get widget
:eieio-show-name
)
287 (car (widget-apply (car wids
) :value-inline
))
289 (chil (if (widget-get widget
:eieio-show-name
)
290 (nthcdr 1 wids
) wids
))
291 (cv (eieio--object-class obj
))
293 (slots (eieio--class-slots cv
)))
294 ;; If there are any prefix widgets, clear them.
296 ;; Create a batch of initargs for each slot.
297 (while (and (< i
(length slots
)) chil
)
298 (let* ((slot (aref slots i
))
299 (props (cl--slot-descriptor-props slot
))
300 (cust (alist-get :custom props
)))
302 ;; Shouldn't I be incremented unconditionally? Or
303 ;; better shouldn't we simply mapc on the slots vector
304 ;; avoiding use of this integer variable? PLN Sat May
309 (or eieio-custom-ignore-eieio-co
311 (member master-group
(alist-get :group props
)))
312 (slot-boundp obj
(cl--slot-descriptor-name slot
)))
314 ;; Only customized slots have widgets
315 (let ((eieio-custom-ignore-eieio-co t
))
316 (eieio-oset obj
(cl--slot-descriptor-name slot
)
317 (car (widget-apply (car chil
) :value-inline
))))
318 (setq chil
(cdr chil
))))))
319 ;; Set any name updates on it.
320 (if name
(eieio-object-set-name-string obj name
))
321 ;; This is the same object we had before.
324 (cl-defmethod eieio-done-customizing ((_obj eieio-default-superclass
))
325 "When applying change to a widget, call this method.
326 This method is called by the default widget-edit commands.
327 User made commands should also call this method when applying changes.
328 Argument OBJ is the object that has been customized."
332 (defun customize-object (obj &optional group
)
333 "Customize OBJ in a custom buffer.
334 Optional argument GROUP is the sub-group of slots to display."
335 (eieio-customize-object obj group
))
337 (defvar eieio-custom-mode-map
338 (let ((map (make-sparse-keymap)))
339 (set-keymap-parent map widget-keymap
)
341 "Keymap for EIEIO Custom mode")
343 (define-derived-mode eieio-custom-mode fundamental-mode
"EIEIO Custom"
344 "Major mode for customizing EIEIO objects.
345 \\{eieio-custom-mode-map}")
347 (cl-defmethod eieio-customize-object ((obj eieio-default-superclass
)
349 "Customize OBJ in a specialized custom buffer.
350 To override call the `eieio-custom-widget-insert' to just insert the
352 Optional argument GROUP specifies a subgroup of slots to edit as a symbol.
353 These groups are specified with the `:group' slot flag."
354 ;; Insert check for multiple edits here.
355 (let* ((g (or group
'default
)))
356 (switch-to-buffer (get-buffer-create
357 (concat "*CUSTOMIZE "
358 (eieio-object-name obj
) " "
359 (symbol-name g
) "*")))
360 (setq buffer-read-only nil
)
361 (kill-all-local-variables)
364 (let ((all (overlay-lists)))
365 ;; Delete all the overlays.
366 (mapc 'delete-overlay
(car all
))
367 (mapc 'delete-overlay
(cdr all
)))
368 ;; Add an apply reset option at the top of the buffer.
369 (eieio-custom-object-apply-reset obj
)
370 (widget-insert "\n\n")
371 (widget-insert "Edit object " (eieio-object-name obj
) "\n\n")
372 ;; Create the widget editing the object.
373 (make-local-variable 'eieio-wo
)
374 (setq eieio-wo
(eieio-custom-widget-insert obj
:eieio-group g
))
375 ;;Now generate the apply buttons
377 (eieio-custom-object-apply-reset obj
)
378 ;; Now initialize the buffer
380 ;;(widget-minor-mode)
381 (goto-char (point-min))
383 (make-local-variable 'eieio-co
)
385 (make-local-variable 'eieio-cog
)
388 (cl-defmethod eieio-custom-object-apply-reset ((_obj eieio-default-superclass
))
389 "Insert an Apply and Reset button into the object editor.
390 Argument OBJ is the object being customized."
391 (widget-create 'push-button
392 :notify
(lambda (&rest _
)
393 (widget-apply eieio-wo
:value-get
)
394 (eieio-done-customizing eieio-co
)
398 (widget-create 'push-button
399 :notify
(lambda (&rest _
)
400 ;; I think the act of getting it sets
401 ;; its value through the get function.
402 (message "Applying Changes...")
403 (widget-apply eieio-wo
:value-get
)
404 (eieio-done-customizing eieio-co
)
405 (message "Applying Changes...Done"))
408 (widget-create 'push-button
409 :notify
(lambda (&rest _
)
410 (message "Resetting")
411 (eieio-customize-object eieio-co eieio-cog
))
414 (widget-create 'push-button
415 :notify
(lambda (&rest _
)
419 (cl-defmethod eieio-custom-widget-insert ((obj eieio-default-superclass
)
421 "Insert the widget used for editing object OBJ in the current buffer.
422 Arguments FLAGS are widget compatible flags.
423 Must return the created widget."
424 (apply 'widget-create
'object-edit
:value obj flags
))
426 (define-widget 'object
'object-edit
427 "Instance of a CLOS class."
428 :format
"%{%t%}:\n%v"
429 :value-to-internal
'eieio-object-value-to-abstract
430 :value-to-external
'eieio-object-abstract-to-value
431 :clone-object-children t
434 (defun eieio-object-value-to-abstract (_widget value
)
435 "For WIDGET, convert VALUE to an abstract /safe/ representation."
436 (if (eieio-object-p value
) value
))
438 (defun eieio-object-abstract-to-value (_widget value
)
439 "For WIDGET, convert VALUE from an abstract /safe/ representation."
443 ;;; customization group functions
445 ;; These functions provide the ability to create dynamic menus to
446 ;; customize specific sections of an object. They do not hook directly
447 ;; into a filter, but can be used to create easymenu vectors.
448 (cl-defmethod eieio-customize-object-group ((obj eieio-default-superclass
))
449 "Create a list of vectors for customizing sections of OBJ."
450 (mapcar (lambda (group)
451 (vector (concat "Group " (symbol-name group
))
452 (list 'customize-object obj
(list 'quote group
))
454 (eieio--class-option (eieio--object-class obj
) :custom-groups
)))
456 (defvar eieio-read-custom-group-history nil
457 "History for the custom group reader.")
459 (cl-defmethod eieio-read-customization-group ((obj eieio-default-superclass
))
460 "Do a completing read on the name of a customization group in OBJ.
461 Return the symbol for the group, or nil"
462 (let ((g (eieio--class-option (eieio--object-class obj
)
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
)
476 ;; generated-autoload-file: "eieio-loaddefs.el"
479 ;;; eieio-custom.el ends here