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