Initial Commit
[packages] / xemacs-packages / eieio / eieio-custom.el
1 ;;; eieio-custom.el -- eieio object customization
2
3 ;;; Copyright (C) 1999, 2000, 2001, 2005, 2007 Eric M. Ludlam
4 ;;
5 ;; Author: <zappo@gnu.org>
6 ;; RCS: $Id: eieio-custom.el,v 1.4 2007-11-26 15:01:04 michaels Exp $
7 ;; Keywords: OO, lisp
8 ;;                                                                          
9 ;; This program is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; any later version.
13 ;;           
14 ;; This program is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 ;; GNU General Public License for more details.
18 ;;
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
21 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 ;; Boston, MA 02110-1301, USA.
23 ;;
24 ;; Please send bug reports, etc. to zappo@gnu.org
25
26 ;;; Commentary:
27 ;;
28 ;;   This contains support customization of eieio objects.  Enabling
29 ;; your object to be customizable requires use of the slot attirbute
30 ;; `:custom'.
31
32 (require 'eieio)
33 (require 'widget)
34 (require 'wid-edit)
35 (require 'custom)
36
37 ;;; Code:
38 (defclass eieio-widget-test-class nil
39   ((a-string :initarg :a-string
40              :initform "The moose is loose"
41              :custom string
42              :label "Amorphous String"
43              :group (default foo)
44              :documentation "A string for testing custom.
45 This is the next line of documentation.")
46    (listostuff :initarg :listostuff
47                :initform ("1" "2" "3")
48                :type list
49                :custom (repeat (string :tag "Stuff"))
50                :label "List of Strings"
51                :group foo
52                :documentation "A list of stuff.")
53    (uninitialized :initarg :uninitialized
54                   :type string
55                   :custom string
56                   :documentation "This slot is not initialized.
57 Used to make sure that custom doesn't barf when it encounters one
58 of these.")
59    (a-number :initarg :a-number
60              :initform 2
61              :custom integer
62              :documentation "A number of thingies."))
63   "A class for testing the widget on.")
64
65 (defcustom eieio-widget-test (eieio-widget-test-class "Foo")
66   "Test variable for editing an object."
67   :type 'object
68   :group 'eieio)
69
70 (defface eieio-custom-slot-tag-face '((((class color)
71                                         (background dark))
72                                        (:foreground "light blue"))
73                                       (((class color)
74                                         (background light))
75                                        (:foreground "blue"))
76                                       (t (:italic t)))
77   "Face used for unpushable variable tags."
78   :group 'custom-faces)
79
80 (defvar eieio-wo nil
81   "Buffer local variable in object customize buffers for the current widget.")
82 (defvar eieio-co nil
83   "Buffer local variable in object customize buffers for the current obj.")
84 (defvar eieio-cog nil
85   "Buffer local variable in object customize buffers for the current group.")
86
87  (defvar eieio-custom-ignore-eieio-co  nil
88    "When true, all customizable fields of the current object are updated.
89 Updates occur regardless of the current customization group.")
90
91 (define-widget 'object-slot 'group
92   "Abstractly modify a single slot in an object."
93   :tag "Slot"
94   :format "%t %v%h\n"
95   :convert-widget 'widget-types-convert-widget
96   :value-create 'eieio-slot-value-create
97   :value-get 'eieio-slot-value-get
98   :value-delete 'widget-children-value-delete
99   :validate 'widget-children-validate
100   :match 'eieio-object-match ;; same
101   )
102
103 (defun eieio-slot-value-create (widget)
104   "Create the value of WIDGET."
105   (let ((chil nil)
106         )
107 ;    (setq chil (cons (widget-create-child-and-convert
108 ;                     widget 'visibility
109 ;                     :help-echo "Hide the value of this option."
110 ;                     :action 'eieio-custom-toggle-parent
111 ;                     t)
112 ;                    chil))
113     (setq chil (cons
114                 (widget-create-child-and-convert
115                  widget (widget-get widget :childtype)
116                  :tag ""
117                  :value (widget-get widget :value))
118                 chil))
119     (widget-put widget :children chil)))
120
121 (defun eieio-slot-value-get (widget)
122   "Get the value of WIDGET."
123   (widget-value (car (widget-get widget :children))))
124
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)))
131           (t
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)
135            ))
136     (widget-value-set vc (widget-value vc))))
137
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)))
142
143 (define-widget 'object-edit 'group
144   "Abstractly modify a CLOS object."
145   :tag "Object"
146   :format "%v"
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
154   )
155
156 (defun eieio-object-match (widget value)
157   "Match info for WIDGET against VALUE."
158   ;; Write me
159   t)
160
161 (defun eieio-filter-slot-type (widget slottype)
162   "Filter WIDGETs SLOTTYPE."
163   (if (widget-get widget :clone-object-children)
164       slottype
165     (cond ((eq slottype 'object)
166            'object-edit)
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))
176            (list 'repeat
177                  (cons 'object-edit
178                        (cdr (car (cdr slottype))))))
179           (t slottype))))
180
181 (defun eieio-object-value-create (widget)
182   "Create the value of WIDGET."
183   (if (not (widget-get widget :value))
184       (widget-put widget
185                   :value (cond ((widget-get widget :objecttype)
186                                 (funcall (class-constructor
187                                           (widget-get widget :objecttype))
188                                          "Custom-new"))
189                                ((widget-get widget :objectcreatefcn)
190                                 (funcall (widget-get widget :objectcreatefcn)))
191                                (t (error "No create method specified")))))
192   (let* ((chil nil)
193          (obj (widget-get widget :value))
194          (master-group (widget-get widget :eieio-group))
195          (cv (class-v (object-class-fast obj)))
196          (fields (aref cv class-public-a))
197          (flabel (aref cv class-public-custom-label))
198          (fgroup (aref cv class-public-custom-group))
199          (fdoc (aref cv class-public-doc))
200          (fcust (aref cv class-public-custom)))
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 "
205                           :sample-face 'bold
206                           (object-name-string obj))
207                          chil)))
208     ;; Display information about the group being shown
209     (when master-group
210       (let ((groups (class-option (object-class-fast obj) :custom-groups)))
211         (widget-insert "Groups:")
212         (while groups
213           (widget-insert "  ")
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 fields, creating child widgets.
226     (while fields
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 fields)))
231         ;; In this case, this field has a custom type.  Create it's
232         ;; children widgets.
233         (let ((type (eieio-filter-slot-type widget (car fcust)))
234               (stuff nil))
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))))
240                     (newtype nil))
241                 (while (not (eq (car type) :slotofchoices))
242                   (setq newtype (cons (car type) newtype)
243                         type (cdr type)))
244                 (while choices
245                   (setq newtype (cons (list 'const (car choices))
246                                       newtype)
247                         choices (cdr choices)))
248                 (setq type (nreverse newtype))))
249           (setq chil (cons (widget-create-child-and-convert
250                             widget 'object-slot
251                             :childtype type
252                             :sample-face 'eieio-custom-slot-tag-face
253                             :tag
254                             (concat
255                              (make-string
256                               (or (widget-get widget :indent) 0)
257                               ? )
258                              (if (car flabel)
259                                  (car flabel)
260                                (let ((s (symbol-name
261                                          (or
262                                           (class-slot-initarg
263                                            (object-class-fast obj)
264                                            (car fields))
265                                           (car fields)))))
266                                  (capitalize
267                                   (if (string-match "^:" s)
268                                       (substring s (match-end 0))
269                                     s)))))
270                             :value (slot-value obj (car fields))
271                             :doc  (if (car fdoc) (car fdoc)
272                                     "Slot not Documented.")
273                             :eieio-custom-visibility 'visible
274                             )
275                            chil))
276           )
277         )
278       (setq fields (cdr fields)
279             fdoc (cdr fdoc)
280             fcust (cdr fcust)
281             flabel (cdr flabel)
282             fgroup (cdr fgroup)))
283     (widget-put widget :children (nreverse chil))
284     ))
285
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 (object-class-fast obj)))
291          (fgroup (aref cv class-public-custom-group))
292          (wids (widget-get widget :children))
293          (name (if (widget-get widget :eieio-show-name)
294                    (car (widget-apply (car wids) :value-inline))
295                  nil))
296          (chil (if (widget-get widget :eieio-show-name)
297                    (nthcdr 1 wids) wids))
298          (cv (class-v (object-class-fast obj)))
299          (fields (aref cv class-public-a))
300          (fcust (aref cv class-public-custom)))
301     ;; If there are any prefix widgets, clear them.
302     ;; -- None yet
303     ;; Create a batch of initargs for each slot.
304     (while (and fields chil)
305       (if (and (car fcust)
306                (or eieio-custom-ignore-eieio-co
307                    (not master-group) (member master-group (car fgroup)))
308                (slot-boundp obj (car fields)))
309           (progn
310             ;; Only customized fields have widgets
311             (let ((eieio-custom-ignore-eieio-co t))
312               (eieio-oset obj (car fields)
313                           (car (widget-apply (car chil) :value-inline))))
314             (setq chil (cdr chil))))
315       (setq fields (cdr fields)
316             fgroup (cdr fgroup)
317             fcust (cdr fcust)))
318     ;; Set any name updates on it.
319     (if name (aset obj object-name name))
320     ;; This is the same object we had before.
321     obj))
322
323 (defmethod eieio-done-customizing ((obj eieio-default-superclass))
324   "When a applying change to a widget, call this method.
325 This method is called by the default widget-edit commands.  User made
326 commands should also call this method when applying changes.
327 Argument OBJ is the object that has been customized."
328   nil)
329
330 (defun customize-object (obj &optional group)
331   "Customize OBJ in a custom buffer.
332 Optional argument GROUP is the sub-group of slots to display."
333   (eieio-customize-object obj group))
334
335 (defmethod eieio-customize-object ((obj eieio-default-superclass)
336                                    &optional group)
337   "Customize OBJ in a specialized custom buffer.
338 To override call the `eieio-custom-widget-insert' to just insert the
339 object widget.
340 Optional argument GROUP specifies a subgroup of slots to edit as a symbol.
341 These groups are specified with the `:group' slot flag."
342   ;; Insert check for multiple edits here.
343   (let* ((g (or group 'default))
344          (b (switch-to-buffer (get-buffer-create
345                                (concat "*CUSTOMIZE "
346                                        (object-name obj) " "
347                                        (symbol-name g) "*")))))
348     (toggle-read-only -1)
349     (kill-all-local-variables)
350     (erase-buffer)
351     (let ((all (overlay-lists)))
352       ;; Delete all the overlays.
353       (mapcar 'delete-overlay (car all))
354       (mapcar 'delete-overlay (cdr all)))
355     ;; Add an apply reset option at the top of the buffer.
356     (eieio-custom-object-apply-reset obj)
357     (widget-insert "\n\n")
358     (widget-insert "Edit object " (object-name obj) "\n\n")
359     ;; Create the widget editing the object.
360     (make-local-variable 'eieio-wo)
361     (setq eieio-wo (eieio-custom-widget-insert obj :eieio-group g))
362     ;;Now generate the apply buttons
363     (widget-insert "\n")
364     (eieio-custom-object-apply-reset obj)
365     ;; Now initialize the buffer
366     (use-local-map widget-keymap)
367     (widget-setup)
368                                         ;(widget-minor-mode)
369     (goto-char (point-min))
370     (widget-forward 3)
371     (make-local-variable 'eieio-co)
372     (setq eieio-co obj)
373     (make-local-variable 'eieio-cog)
374     (setq eieio-cog group)))
375
376 (defmethod eieio-custom-object-apply-reset ((obj eieio-default-superclass))
377   "Insert an Apply and Reset button into the object editor.
378 Argument OBJ os the object being customized."
379   (widget-create 'push-button
380                  :notify (lambda (&rest ignore)
381                            (widget-apply eieio-wo :value-get)
382                            (eieio-done-customizing eieio-co)
383                            (bury-buffer))
384                  "Accept")
385   (widget-insert "   ")
386   (widget-create 'push-button
387                  :notify (lambda (&rest ignore)
388                            ;; I think the act of getting it sets
389                            ;; it's value through the get function.
390                            (message "Applying Changes...")
391                            (widget-apply eieio-wo :value-get)
392                            (eieio-done-customizing eieio-co)
393                            (message "Applying Changes...Done."))
394                  "Apply")
395   (widget-insert "   ")
396   (widget-create 'push-button
397                  :notify (lambda (&rest ignore)
398                            (message "Resetting.")
399                            (eieio-customize-object eieio-co eieio-cog))
400                  "Reset")
401   (widget-insert "   ")
402   (widget-create 'push-button
403                  :notify (lambda (&rest ignore)
404                            (bury-buffer))
405                  "Cancel"))
406
407 (defmethod eieio-custom-widget-insert ((obj eieio-default-superclass)
408                                        &rest flags)
409   "Insert the widget used for editing object OBJ in the current buffer.
410 Arguments FLAGS are widget compatible flags.
411 Must return the created widget."
412   (apply 'widget-create 'object-edit :value obj flags))
413
414 (define-widget 'object 'object-edit
415   "Instance of a CLOS class."
416   :format "%{%t%}:\n%v"
417   :value-to-internal 'eieio-object-value-to-abstract
418   :value-to-external 'eieio-object-abstract-to-value
419   :clone-object-children t
420   )
421
422 (defun eieio-object-value-to-abstract (widget value)
423   "For WIDGET, convert VALUE to an abstract /safe/ representation."
424   (if (object-p value) value
425     (if (null value) value
426       nil)))
427
428 (defun eieio-object-abstract-to-value (widget value)
429   "For WIDGET, convert VALUE to an abstract /safe/ representation."
430   value)
431
432 \f
433 ;;; customization group functions
434 ;;
435 ;; These functions provide the ability to create dynamic menus to
436 ;; customize specific sections of an object.  The do not hook directly
437 ;; into a filter, but can be used to create easymenu vectors.
438 (defmethod eieio-customize-object-group ((obj eieio-default-superclass))
439   "Create a list of vectors for customizing sections of OBJ."
440   (mapcar (lambda (group)
441             (vector (concat "Group " (symbol-name group))
442                     (list 'customize-object obj (list 'quote group))
443                     t))
444           (class-option (object-class-fast obj) :custom-groups)))
445
446 (defvar eieio-read-custom-group-history nil
447   "History for the custom group reader.")
448
449 (defmethod eieio-read-customization-group ((obj eieio-default-superclass))
450   "Do a completing read on the name of a customization group in OBJ.
451 Return the symbol for the group, or nil"
452   (let ((g (class-option (object-class-fast obj) :custom-groups)))
453     (if (= (length g) 1)
454         (car g)
455       ;; Make the association list
456       (setq g (mapcar (lambda (g) (cons (symbol-name g) g)) g))
457       (cdr (assoc
458             (completing-read (concat (oref obj name)  " Custom Group: ")
459                              g nil t nil 'eieio-read-custom-group-history)
460             g)))))
461
462 (provide 'eieio-custom)
463
464 ;;; eieio-custom.el ends here
465 ;;