1 ;;; eieio-custom.el -- eieio object customization -*- lexical-binding:t -*-
3 ;; Copyright (C) 1999-2001, 2005, 2007-2015 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-object obj
))
197 (slots (eieio--class-public-a cv
))
198 (flabel (eieio--class-public-custom-label cv
))
199 (fgroup (eieio--class-public-custom-group cv
))
200 (fdoc (eieio--class-public-doc cv
))
201 (fcust (eieio--class-public-custom cv
)))
202 ;; First line describes the object, but may not editable.
203 (if (widget-get widget
:eieio-show-name
)
204 (setq chil
(cons (widget-create-child-and-convert
205 widget
'string
:tag
"Object "
207 (eieio-object-name-string obj
))
209 ;; Display information about the group being shown
211 (let ((groups (eieio--class-option (eieio--object-class-object obj
)
213 (widget-insert "Groups:")
216 (if (eq (car groups
) master-group
)
217 (widget-insert "*" (capitalize (symbol-name master-group
)) "*")
218 (widget-create 'push-button
219 :thing
(cons obj
(car groups
))
220 :notify
(lambda (widget &rest _
)
221 (eieio-customize-object
222 (car (widget-get widget
:thing
))
223 (cdr (widget-get widget
:thing
))))
224 (capitalize (symbol-name (car groups
)))))
225 (setq groups
(cdr groups
)))
226 (widget-insert "\n\n")))
227 ;; Loop over all the slots, creating child widgets.
229 ;; Output this slot if it has a customize flag associated with it.
230 (when (and (car fcust
)
231 (or (not master-group
) (member master-group
(car fgroup
)))
232 (slot-boundp obj
(car slots
)))
233 ;; In this case, this slot has a custom type. Create its
235 (let ((type (eieio-filter-slot-type widget
(car fcust
)))
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)
262 (let ((s (symbol-name
264 (eieio--class-slot-initarg
265 (eieio--object-class-object obj
)
269 (if (string-match "^:" s
)
270 (substring s
(match-end 0))
272 :value
(slot-value obj
(car slots
))
273 :doc
(if (car fdoc
) (car fdoc
)
274 "Slot not Documented.")
275 :eieio-custom-visibility
'visible
280 (setq slots
(cdr slots
)
284 fgroup
(cdr fgroup
)))
285 (widget-put widget
:children
(nreverse chil
))
288 (defun eieio-object-value-get (widget)
289 "Get the value of WIDGET."
290 (let* ((obj (widget-get widget
:value
))
291 (master-group eieio-cog
)
292 (cv (eieio--object-class-object obj
))
293 (fgroup (eieio--class-public-custom-group cv
))
294 (wids (widget-get widget
:children
))
295 (name (if (widget-get widget
:eieio-show-name
)
296 (car (widget-apply (car wids
) :value-inline
))
298 (chil (if (widget-get widget
:eieio-show-name
)
299 (nthcdr 1 wids
) wids
))
300 (cv (eieio--object-class-object obj
))
301 (slots (eieio--class-public-a cv
))
302 (fcust (eieio--class-public-custom cv
)))
303 ;; If there are any prefix widgets, clear them.
305 ;; Create a batch of initargs for each slot.
306 (while (and slots chil
)
308 (or eieio-custom-ignore-eieio-co
309 (not master-group
) (member master-group
(car fgroup
)))
310 (slot-boundp obj
(car slots
)))
312 ;; Only customized slots have widgets
313 (let ((eieio-custom-ignore-eieio-co t
))
314 (eieio-oset obj
(car slots
)
315 (car (widget-apply (car chil
) :value-inline
))))
316 (setq chil
(cdr chil
))))
317 (setq slots
(cdr slots
)
320 ;; Set any name updates on it.
321 (if name
(eieio-object-set-name-string obj name
))
322 ;; This is the same object we had before.
325 (cl-defmethod eieio-done-customizing ((_obj eieio-default-superclass
))
326 "When applying change to a widget, call this method.
327 This method is called by the default widget-edit commands.
328 User made commands should also call this method when applying changes.
329 Argument OBJ is the object that has been customized."
333 (defun customize-object (obj &optional group
)
334 "Customize OBJ in a custom buffer.
335 Optional argument GROUP is the sub-group of slots to display."
336 (eieio-customize-object obj group
))
338 (defvar eieio-custom-mode-map
339 (let ((map (make-sparse-keymap)))
340 (set-keymap-parent map widget-keymap
)
342 "Keymap for EIEIO Custom mode")
344 (define-derived-mode eieio-custom-mode fundamental-mode
"EIEIO Custom"
345 "Major mode for customizing EIEIO objects.
346 \\{eieio-custom-mode-map}")
348 (cl-defmethod eieio-customize-object ((obj eieio-default-superclass
)
350 "Customize OBJ in a specialized custom buffer.
351 To override call the `eieio-custom-widget-insert' to just insert the
353 Optional argument GROUP specifies a subgroup of slots to edit as a symbol.
354 These groups are specified with the `:group' slot flag."
355 ;; Insert check for multiple edits here.
356 (let* ((g (or group
'default
)))
357 (switch-to-buffer (get-buffer-create
358 (concat "*CUSTOMIZE "
359 (eieio-object-name obj
) " "
360 (symbol-name g
) "*")))
361 (setq buffer-read-only nil
)
362 (kill-all-local-variables)
365 (let ((all (overlay-lists)))
366 ;; Delete all the overlays.
367 (mapc 'delete-overlay
(car all
))
368 (mapc 'delete-overlay
(cdr all
)))
369 ;; Add an apply reset option at the top of the buffer.
370 (eieio-custom-object-apply-reset obj
)
371 (widget-insert "\n\n")
372 (widget-insert "Edit object " (eieio-object-name obj
) "\n\n")
373 ;; Create the widget editing the object.
374 (make-local-variable 'eieio-wo
)
375 (setq eieio-wo
(eieio-custom-widget-insert obj
:eieio-group g
))
376 ;;Now generate the apply buttons
378 (eieio-custom-object-apply-reset obj
)
379 ;; Now initialize the buffer
381 ;;(widget-minor-mode)
382 (goto-char (point-min))
384 (make-local-variable 'eieio-co
)
386 (make-local-variable 'eieio-cog
)
389 (cl-defmethod eieio-custom-object-apply-reset ((_obj eieio-default-superclass
))
390 "Insert an Apply and Reset button into the object editor.
391 Argument OBJ is the object being customized."
392 (widget-create 'push-button
393 :notify
(lambda (&rest _
)
394 (widget-apply eieio-wo
:value-get
)
395 (eieio-done-customizing eieio-co
)
399 (widget-create 'push-button
400 :notify
(lambda (&rest _
)
401 ;; I think the act of getting it sets
402 ;; its value through the get function.
403 (message "Applying Changes...")
404 (widget-apply eieio-wo
:value-get
)
405 (eieio-done-customizing eieio-co
)
406 (message "Applying Changes...Done"))
409 (widget-create 'push-button
410 :notify
(lambda (&rest _
)
411 (message "Resetting")
412 (eieio-customize-object eieio-co eieio-cog
))
415 (widget-create 'push-button
416 :notify
(lambda (&rest _
)
420 (cl-defmethod eieio-custom-widget-insert ((obj eieio-default-superclass
)
422 "Insert the widget used for editing object OBJ in the current buffer.
423 Arguments FLAGS are widget compatible flags.
424 Must return the created widget."
425 (apply 'widget-create
'object-edit
:value obj flags
))
427 (define-widget 'object
'object-edit
428 "Instance of a CLOS class."
429 :format
"%{%t%}:\n%v"
430 :value-to-internal
'eieio-object-value-to-abstract
431 :value-to-external
'eieio-object-abstract-to-value
432 :clone-object-children t
435 (defun eieio-object-value-to-abstract (_widget value
)
436 "For WIDGET, convert VALUE to an abstract /safe/ representation."
437 (if (eieio-object-p 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 (cl-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 (eieio--class-option (eieio--object-class-object obj
) :custom-groups
)))
457 (defvar eieio-read-custom-group-history nil
458 "History for the custom group reader.")
460 (cl-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 (eieio--class-option (eieio--object-class-object obj
)
467 ;; Make the association list
468 (setq g
(mapcar (lambda (g) (cons (symbol-name g
) g
)) g
))
470 (completing-read (concat (oref obj name
) " Custom Group: ")
471 g nil t nil
'eieio-read-custom-group-history
)
474 (provide 'eieio-custom
)
477 ;; generated-autoload-file: "eieio.el"
480 ;;; eieio-custom.el ends here