]> code.delx.au - gnu-emacs/blob - lisp/emacs-lisp/eieio-custom.el
Merge from origin/emacs-24
[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-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 "
206 :sample-face 'bold
207 (eieio-object-name-string obj))
208 chil)))
209 ;; Display information about the group being shown
210 (when master-group
211 (let ((groups (eieio--class-option (eieio--object-class-object obj)
212 :custom-groups)))
213 (widget-insert "Groups:")
214 (while groups
215 (widget-insert " ")
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.
228 (while slots
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
234 ;; children widgets.
235 (let ((type (eieio-filter-slot-type widget (car fcust)))
236 (stuff nil))
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))))
242 (newtype nil))
243 (while (not (eq (car type) :slotofchoices))
244 (setq newtype (cons (car type) newtype)
245 type (cdr type)))
246 (while choices
247 (setq newtype (cons (list 'const (car choices))
248 newtype)
249 choices (cdr choices)))
250 (setq type (nreverse newtype))))
251 (setq chil (cons (widget-create-child-and-convert
252 widget 'object-slot
253 :childtype type
254 :sample-face 'eieio-custom-slot-tag-face
255 :tag
256 (concat
257 (make-string
258 (or (widget-get widget :indent) 0)
259 ? )
260 (if (car flabel)
261 (car flabel)
262 (let ((s (symbol-name
263 (or
264 (eieio--class-slot-initarg
265 (eieio--object-class-object obj)
266 (car slots))
267 (car slots)))))
268 (capitalize
269 (if (string-match "^:" s)
270 (substring s (match-end 0))
271 s)))))
272 :value (slot-value obj (car slots))
273 :doc (if (car fdoc) (car fdoc)
274 "Slot not Documented.")
275 :eieio-custom-visibility 'visible
276 )
277 chil))
278 )
279 )
280 (setq slots (cdr slots)
281 fdoc (cdr fdoc)
282 fcust (cdr fcust)
283 flabel (cdr flabel)
284 fgroup (cdr fgroup)))
285 (widget-put widget :children (nreverse chil))
286 ))
287
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))
297 nil))
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.
304 ;; -- None yet
305 ;; Create a batch of initargs for each slot.
306 (while (and slots chil)
307 (if (and (car fcust)
308 (or eieio-custom-ignore-eieio-co
309 (not master-group) (member master-group (car fgroup)))
310 (slot-boundp obj (car slots)))
311 (progn
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)
318 fgroup (cdr fgroup)
319 fcust (cdr fcust)))
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.
323 obj))
324
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."
330 nil)
331
332 ;;;###autoload
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))
337
338 (defvar eieio-custom-mode-map
339 (let ((map (make-sparse-keymap)))
340 (set-keymap-parent map widget-keymap)
341 map)
342 "Keymap for EIEIO Custom mode")
343
344 (define-derived-mode eieio-custom-mode fundamental-mode "EIEIO Custom"
345 "Major mode for customizing EIEIO objects.
346 \\{eieio-custom-mode-map}")
347
348 (cl-defmethod eieio-customize-object ((obj eieio-default-superclass)
349 &optional group)
350 "Customize OBJ in a specialized custom buffer.
351 To override call the `eieio-custom-widget-insert' to just insert the
352 object widget.
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)
363 (eieio-custom-mode)
364 (erase-buffer)
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
377 (widget-insert "\n")
378 (eieio-custom-object-apply-reset obj)
379 ;; Now initialize the buffer
380 (widget-setup)
381 ;;(widget-minor-mode)
382 (goto-char (point-min))
383 (widget-forward 3)
384 (make-local-variable 'eieio-co)
385 (setq eieio-co obj)
386 (make-local-variable 'eieio-cog)
387 (setq eieio-cog g)))
388
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)
396 (bury-buffer))
397 "Accept")
398 (widget-insert " ")
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"))
407 "Apply")
408 (widget-insert " ")
409 (widget-create 'push-button
410 :notify (lambda (&rest _)
411 (message "Resetting")
412 (eieio-customize-object eieio-co eieio-cog))
413 "Reset")
414 (widget-insert " ")
415 (widget-create 'push-button
416 :notify (lambda (&rest _)
417 (bury-buffer))
418 "Cancel"))
419
420 (cl-defmethod eieio-custom-widget-insert ((obj eieio-default-superclass)
421 &rest flags)
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))
426
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
433 )
434
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))
438
439 (defun eieio-object-abstract-to-value (_widget value)
440 "For WIDGET, convert VALUE from an abstract /safe/ representation."
441 value)
442
443 \f
444 ;;; customization group functions
445 ;;
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))
454 t))
455 (eieio--class-option (eieio--object-class-object obj) :custom-groups)))
456
457 (defvar eieio-read-custom-group-history nil
458 "History for the custom group reader.")
459
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)
464 :custom-groups)))
465 (if (= (length g) 1)
466 (car g)
467 ;; Make the association list
468 (setq g (mapcar (lambda (g) (cons (symbol-name g) g)) g))
469 (cdr (assoc
470 (completing-read (concat (oref obj name) " Custom Group: ")
471 g nil t nil 'eieio-read-custom-group-history)
472 g)))))
473
474 (provide 'eieio-custom)
475
476 ;; Local variables:
477 ;; generated-autoload-file: "eieio.el"
478 ;; End:
479
480 ;;; eieio-custom.el ends here