Initial Commit
[packages] / xemacs-packages / eieio / eieio.el.upstream
1 ;;; eieio.el --- Enhanced Implementation of Emacs Interpreted Objects
2 ;;               or maybe Eric's Implementation of Emacs Intrepreted Objects
3
4 ;;;
5 ;; Copyright (C) 95,96,98,99,2000,01,02,03,04,05,06,07 Eric M. Ludlam
6 ;;
7 ;; Author: <zappo@gnu.org>
8 ;; RCS: $Id: eieio.el.upstream,v 1.1 2007-11-26 15:01:06 michaels Exp $
9 ;; Keywords: OO, lisp
10 (defvar eieio-version "1.0"
11   "Current version of EIEIO.")
12 ;;
13 ;; This program is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; any later version.
17 ;;
18 ;; This program is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 ;; GNU General Public License for more details.
22 ;;
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
25 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
26 ;; Boston, MA 02110-1301, USA.
27 ;;
28 ;; Please send bug reports, etc. to zappo@gnu.org
29
30 ;;; Commentary:
31 ;;
32 ;; EIEIO is a series of Lisp routines which implements a subset of
33 ;; CLOS, the Common Lisp Object System.  In addition, EIEIO also adds
34 ;; a few new features which help it integrate more strongly with the
35 ;; Emacs running environment.
36 ;;
37 ;; See eieio.texi for complete documentation on using this package.
38
39 ;; There is funny stuff going on with typep and deftype.  This
40 ;; is the only way I seem to be able to make this stuff load properly.
41 (require 'cl)
42 (load "cl-macs" nil t) ; No provide in this file.
43
44 ;;; Code:
45 (defun eieio-version ()
46   "Display the current version of EIEIO."
47   (interactive)
48   (message eieio-version))
49
50 (require 'inversion)
51
52 (defun eieio-require-version (major minor &optional beta)
53   "Non-nil if this version of EIEIO does not satisfy a specific version.
54 Arguments can be:
55
56   (MAJOR MINOR &optional BETA)
57
58   Values MAJOR and MINOR must be integers.  BETA can be an integer, or
59 excluded if a released version is required.
60
61 It is assumed that if the current version is newer than that specified,
62 everything passes.  Exceptions occur when known incompatibilities are
63 introduced."
64   (inversion-test 'eieio
65                   (format "%s.%s%s" major minor
66                           (if beta (format "beta%s" beta) ""))))
67
68 (eval-and-compile
69 ;; Abount the above.  EIEIO must process it's own code when it compiles
70 ;; itself, thus, by eval-and-compiling outselves, we solve the problem.
71
72 ;; Compatibility
73 (if (fboundp 'compiled-function-arglist)
74
75     ;; XEmacs can only access a compiled functions arglist like this:
76     (defalias 'eieio-compiled-function-arglist 'compiled-function-arglist)
77
78   ;; Emacs doesn't have this function, but since FUNC is a vector, we can just
79   ;; grab the appropriate element.
80   (defun eieio-compiled-function-arglist (func)
81     "Return the argument list for the compiled function FUNC."
82     (aref func 0))
83
84   )
85
86 \f
87 ;;;
88 ;; Variable declarations.
89 ;;
90
91 (defvar eieio-hook nil
92   "*This hook is executed, then cleared each time `defclass' is called.
93 The immediate effect is that I can safely keep track of common-lisp
94 `setf' definitions regardless of the order.  Users can add hooks to
95 this variable without worrying about weather this package has been
96 loaded or not.")
97
98 (defvar eieio-error-unsupported-class-tags nil
99   "*Non nil to throw an error if an encountered tag us unsupported.
100 This may prevent classes from CLOS applications from being used with EIEIO
101 since EIEIO does not support all CLOS tags.")
102
103 (defvar eieio-skip-typecheck nil
104   "*If non-nil, skip all slot typechecking.
105 Set this to t permanently if a program is functioning well to get a
106 small speed increase.  This variable is also used internally to handle
107 default setting for optimization purposes.")
108
109 ;; State Variables
110 (defvar this nil
111   "Inside a method, this variable is the object in question.
112 DO NOT SET THIS YOURSELF unless you are trying to simulate friendly fields.
113
114 Note: Embedded methods are no longer supported.  The variable THIS is
115 still set for CLOS methods for the sake of routines like
116 `call-next-method'")
117
118 (defvar scoped-class nil
119   "This is set to a class when a method is running.
120 This is so we know we are allowed to check private parts or how to
121 execute a `call-next-method'.  DO NOT SET THIS YOURSELF!")
122
123 (defvar eieio-initializing-object  nil
124   "Set to non-nil while initializing an object.")
125
126 (defconst eieio-unbound (make-symbol "unbound")
127   "Uninterned symbol representing an unbound slot in an object.")
128
129 ;; This is a bootstrap for eieio-default-superclass so it has a value
130 ;; while it is being built itself.
131 (defvar eieio-default-superclass nil)
132
133 (defconst class-symbol 1 "Class's symbol (self-referencing.).")
134 (defconst class-parent 2 "Class parent field.")
135 (defconst class-children 3 "Class children class field.")
136 (defconst class-symbol-obarray 4 "Obarray permitting fast access to variable position indexes.")
137 (defconst class-public-a 5 "Class public attribute index.")
138 (defconst class-public-d 6 "Class public attribute defaults index.")
139 (defconst class-public-doc 7 "Class public documentation strings for attributes.")
140 (defconst class-public-type 8 "Class public type for a slot.")
141 (defconst class-public-custom 9 "Class public custom type for a slot.")
142 (defconst class-public-custom-label 10 "Class public custom group for a slot.")
143 (defconst class-public-custom-group 11 "Class public custom group for a slot.")
144 (defconst class-protection 12 "Class protection for a slot.")
145 (defconst class-initarg-tuples 13 "Class initarg tuples list.")
146 (defconst class-class-allocation-a 14 "Class allocated attributes.")
147 (defconst class-class-allocation-doc 15 "Class allocated documentation.")
148 (defconst class-class-allocation-type 16 "Class allocated value type.")
149 (defconst class-class-allocation-custom 17 "Class allocated custom descriptor.")
150 (defconst class-class-allocation-custom-label 18 "Class allocated custom descriptor.")
151 (defconst class-class-allocation-custom-group 19 "Class allocated custom group.")
152 (defconst class-class-allocation-protection 20 "Class allocated protection list.")
153 (defconst class-class-allocation-values 21 "Class allocated value vector.")
154 (defconst class-default-object-cache 22
155   "Cache index of what a newly created object would look like.
156 This will speed up instantiation time as only a `copy-sequence' will
157 be needed, instead of looping over all the values and setting them
158 from the default.")
159 (defconst class-options 23
160   "Storage location of tagged class options.
161 Stored outright without modifications or stripping.")
162
163 (defconst class-num-fields 24
164   "Number of fields in the class definition object.")
165
166 (defconst object-class 1 "Index in an object vector where the class is stored.")
167 (defconst object-name 2 "Index in an object where the name is stored.")
168
169 (defconst method-static 0 "Index into :STATIC tag on a method.")
170 (defconst method-before 1 "Index into :BEFORE tag on a method.")
171 (defconst method-primary 2 "Index into :PRIMARY tag on a method.")
172 (defconst method-after 3 "Index into :AFTER tag on a method.")
173 (defconst method-num-lists 4 "Number of indexes into methods vector in which groups of functions are kept.")
174 (defconst method-generic-before 4 "Index into generic :BEFORE tag on a method.")
175 (defconst method-generic-primary 5 "Index into generic :PRIMARY tag on a method.")
176 (defconst method-generic-after 6 "Index into generic :AFTER tag on a method.")
177 (defconst method-num-fields 7 "Number of indexes into a method's vector.")
178
179 ;; How to specialty compile stuff.
180 (autoload 'byte-compile-file-form-defmethod "eieio-comp"
181   "This function is used to byte compile methods in a nice way.")
182 (put 'defmethod 'byte-hunk-handler 'byte-compile-file-form-defmethod)
183
184 (eval-when-compile (require 'eieio-comp))
185
186 \f
187 ;;; Important macros used in eieio.
188 ;;
189 (defmacro class-v (class) "Internal: Return the class vector from the CLASS symbol."
190   ;; No check: If eieio gets this far, it's probably been checked already.
191   `(get ,class `eieio-class-definition))
192
193 (defmacro class-p (class)
194   "Return t if CLASS is a valid class vector.
195 CLASS is a symbol.  Defclass will assign the class symbol to itself, so
196 the shortcut (class-p foo) will work.  The form (class-p 'foo) is more
197 robust."
198   ;; this new method is faster since it doesn't waste time checking lots of
199   ;; things.
200   `(condition-case nil
201        (eq (aref (class-v ,class) 0) 'defclass)
202      (error nil)))
203
204 (defmacro object-p (obj) "Return t if OBJ is an object vector."
205   `(condition-case nil
206        (let ((tobj ,obj))
207          (and (eq (aref tobj 0) 'object)
208               (class-p (aref tobj object-class))))
209      (error nil)))
210
211 (defmacro class-constructor (class)
212   "Return the symbol representing the constructor of CLASS."
213   `(aref (class-v ,class) class-symbol))
214
215 (defmacro generic-p (method)
216   "Return t if symbol METHOD is a generic function.
217 Only methods have the symbol `eieio-method-obarray' as a property (which
218 contains a list of all bindings to that method type.)"
219   `(and (fboundp ,method) (get ,method 'eieio-method-obarray)))
220
221 (defmacro class-option-assoc (list option)
222   "Return from LIST the found OPTION.  Nil if it doesn't exist."
223   `(car-safe (cdr (memq ,option ,list))))
224
225 (defmacro class-option (class option)
226   "Return the value stored for CLASS' OPTION.
227 Return nil if that option doesn't exist."
228   `(class-option-assoc (aref (class-v ,class) class-options) ',option))
229
230 (defmacro class-abstract-p (class)
231   "Return non-nil if CLASS is abstract.
232 Abstract classes cannot be instantiated."
233   `(class-option ,class :abstract))
234
235 \f
236 ;;; Defining a new class
237 ;;
238 (defmacro defclass (name superclass fields &rest options-and-doc)
239   "Define NAME as a new class derived from SUPERCLASS with FIELDS.
240 OPTIONS-AND-DOC is used as the class' options and base documentation.
241 SUPERCLASS is a list of superclasses to inherit from, with FIELDS
242 being the fields residing in that class definition.  NOTE: Currently
243 only one field may exist in SUPERCLASS as multiple inheritance is not
244 yet supported.  Supported tags are:
245
246   :initform   - initializing form
247   :initarg    - tag used during initialization
248   :accessor   - tag used to create a function to access this field
249   :allocation - specify where the value is stored.
250                 defaults to `:instance', but could also be `:class'
251   :writer     - a function symbol which will `write' an object's slot
252   :reader     - a function symbol which will `read' an object
253   :type       - the type of data allowed in this slot (see `typep')
254   :documentation
255               - A string documenting use of this slot.
256
257 The following are extensions on CLOS:
258   :protection - Specify protection for this slot.
259                 Defaults to `:public'.  Also use `:protected', or `:private'
260   :custom     - When customizing an object, the custom :type.  Public only.
261   :label      - A text string label used for a slot when customizing.
262   :group      - Name of a customization group this slot belongs in.
263
264 A class can also have optional options.  These options happen in place
265 of documentation, (including a :documentation tag) in addition to
266 documentation, or not at all.  Supported options are:
267
268   :documentation - The doc-string used for this class.
269
270 Options added to EIEIO:
271
272   :allow-nil-initform - Non-nil to skip typechecking of initforms if nil.
273   :custom-groups      - List of custom group names.  Organizes slots into
274                         reasonable groups for customizations.
275   :abstract           - Non-nil to prevent instances of this class.
276                         If a string, use as an error string if someone does
277                         try to make an instance.
278
279 Options in CLOS not supported in EIEIO:
280
281   :metaclass - Class to use in place of `standard-class'
282   :default-initargs - Initargs to use when initializing new objects of
283                       this class.
284
285 Due to the way class options are set up, you can add any tags in you
286 wish, and reference them using the function `class-option'."
287   ;; We must `eval-and-compile' this so that when we byte compile
288   ;; an eieio program, there is no need to load it ahead of time.
289   ;; It also provides lots of nice debugging errors at compile time.
290   `(eval-and-compile
291      (eieio-defclass ',name ',superclass ',fields ',options-and-doc)))
292
293 (defun eieio-defclass (cname superclasses fields options-and-doc)
294   "See `defclass' for more information.
295 Define CNAME as a new subclass of SUPERCLASSES, with FIELDS being the
296 fields residing in that class definition, and with options or documentation
297 OPTIONS-AND-DOC as the toplevel documentation for this class."
298   ;; Run our eieio-hook each time, and clear it when we are done.
299   ;; This way people can add hooks safely if they want to modify eieio
300   ;; or add definitions when eieio is loaded or something like that.
301   (run-hooks 'eieio-hook)
302   (setq eieio-hook nil)
303
304   (if (not (symbolp cname)) (signal 'wrong-type-argument '(symbolp cname)))
305   (if (not (listp superclasses)) (signal 'wrong-type-argument '(listp superclasses)))
306
307   (let* ((pname (if superclasses superclasses nil))
308          (newc (make-vector class-num-fields nil))
309          (oldc (when (class-p cname) (class-v cname)))
310          (groups nil) ;; list of groups id'd from slots
311          (options nil)
312          (clearparent nil))
313
314     (aset newc 0 'defclass)
315     (aset newc class-symbol cname)
316
317     ;; If this class already existed, and we are updating it's structure,
318     ;; make sure we keep the old child list.  This can cause bugs, but
319     ;; if no new slots are created, it also saves time, and prevents
320     ;; method table breakage, particularly when the users is only
321     ;; byte compiling an EIEIO file.
322     (when oldc
323       (aset newc class-children (aref oldc class-children)))
324
325     (cond ((and (stringp (car options-and-doc))
326                 (/= 1 (% (length options-and-doc) 2)))
327            (error "Too many arguments to `defclass'"))
328           ((and (symbolp (car options-and-doc))
329                 (/= 0 (% (length options-and-doc) 2)))
330            (error "Too many arguments to `defclass'"))
331           )
332
333     (setq options
334           (if (stringp (car options-and-doc))
335               (cons :documentation options-and-doc)
336             options-and-doc))
337
338     (if pname
339         (progn
340           (while pname
341             (if (and (car pname) (symbolp (car pname)))
342                 (if (not (class-p (car pname)))
343                     ;; bad class
344                     (error "Given parent class %s is not a class" (car pname))
345                   ;; good parent class...
346                   ;; save new child in parent
347                   (if (not (member cname (aref (class-v (car pname)) class-children)))
348                       (aset (class-v (car pname)) class-children
349                             (cons cname (aref (class-v (car pname)) class-children))))
350                   ;; Get custom groups, and store them into our local copy.
351                   (mapcar (lambda (g) (add-to-list 'groups g))
352                           (class-option (car pname) :custom-groups))
353                   ;; save parent in child
354                   (aset newc class-parent (cons (car pname) (aref newc class-parent))))
355               (error "Invalid parent class %s" pname))
356             (setq pname (cdr pname)))
357           ;; Reverse the list of our parents so that they are prioritized in
358           ;; the same order as specified in the code.
359           (aset newc class-parent (nreverse (aref newc class-parent))) )
360       ;; If there is nothing to loop over, then inherit from the
361       ;; default superclass.
362       (unless (eq cname 'eieio-default-superclass)
363         ;; adopt the default parent here, but clear it later...
364         (setq clearparent t)
365         ;; save new child in parent
366         (if (not (member cname (aref (class-v 'eieio-default-superclass) class-children)))
367             (aset (class-v 'eieio-default-superclass) class-children
368                   (cons cname (aref (class-v 'eieio-default-superclass) class-children))))
369         ;; save parent in child
370         (aset newc class-parent (list eieio-default-superclass))))
371     
372     ;; turn this into a useable self-pointing symbol
373     (set cname cname)
374
375     ;; These two tests must be created right away so we can have self-
376     ;; referencing classes.  ei, a class whose slot can contain only
377     ;; pointers to itself.
378
379     ;; Create the test function
380     (let ((csym (intern (concat (symbol-name cname) "-p"))))
381       (fset csym
382             (list 'lambda (list 'obj)
383                   (format "Test OBJ to see if it an object of type %s" cname)
384                   (list 'and '(object-p obj)
385                         (list 'same-class-p 'obj cname)))))
386
387     ;; Create a handy child test too
388     (let ((csym (intern (concat (symbol-name cname) "-child-p"))))
389       (fset csym
390             `(lambda (obj)
391                ,(format
392                   "Test OBJ to see if it an object is a child of type %s"
393                   cname)
394                (and (object-p obj)
395                     (object-of-class-p obj ,cname))))
396     
397       ;; When using typep, (typep OBJ 'myclass) returns t for objects which
398       ;; are subclasses of myclass.  For our predicates, however, it is
399       ;; important for EIEIO to be backwards compatible, where
400       ;; myobject-p, and myobject-child-p are different.
401       ;; "cl" uses this technique to specify symbols with specific typep
402       ;; test, so we can let typep have the CLOS documented behavior
403       ;; while keeping our above predicate clean.
404       (eval `(deftype ,cname ()
405                '(satisfies
406                  ,(intern (concat (symbol-name cname) "-child-p")))))
407
408       )
409
410     ;; before adding new fields, lets add all the methods and classes
411     ;; in from the parent class
412     (eieio-copy-parents-into-subclass newc superclasses)
413
414     ;; Store the new class vector definition into the symbol.  We need to
415     ;; do this first so that we can call defmethod for the accessor.
416     ;; The vector will be updated by the following while loop and will not
417     ;; need to be stored a second time.
418     (put cname 'eieio-class-definition newc)
419
420     ;; Query each field in the declaration list and mangle into the
421     ;; class structure I have defined.
422     (while fields
423       (let* ((field1  (car fields))
424              (name    (car field1))
425              (field   (cdr field1))
426              (acces   (plist-get field ':accessor))
427              (init    (or (plist-get field ':initform)
428                           (if (member ':initform field) nil
429                             eieio-unbound)))
430              (initarg (plist-get field ':initarg))
431              (docstr  (plist-get field ':documentation))
432              (prot    (plist-get field ':protection))
433              (reader  (plist-get field ':reader))
434              (writer  (plist-get field ':writer))
435              (alloc   (plist-get field ':allocation))
436              (type    (plist-get field ':type))
437              (custom  (plist-get field ':custom))
438              (label   (plist-get field ':label))
439              (customg (plist-get field ':group))
440              
441              (skip-nil (class-option-assoc options :allow-nil-initform))
442              )
443
444         (if eieio-error-unsupported-class-tags
445             (let ((tmp field))
446               (while tmp
447                 (if (not (member (car tmp) '(:accessor
448                                              :initform
449                                              :initarg
450                                              :documentation
451                                              :protection
452                                              :reader
453                                              :writer
454                                              :allocation
455                                              :type
456                                              :custom
457                                              :label
458                                              :group
459                                              :allow-nil-initform
460                                              :custom-groups)))
461                     (signal 'invalid-slot-type (list (car tmp))))
462                 (setq tmp (cdr (cdr tmp))))))
463
464         ;; Clean up the meaning of protection.
465         (cond ((or (eq prot 'public) (eq prot :public)) (setq prot nil))
466               ((or (eq prot 'protected) (eq prot :protected)) (setq prot 'protected))
467               ((or (eq prot 'private) (eq prot :private)) (setq prot 'private))
468               ((eq prot nil) nil)
469               (t (signal 'invalid-slot-type (list ':protection prot))))
470
471         ;; Make sure the :allocation parameter has a valid value.
472         (if (not (or (not alloc) (eq alloc :class) (eq alloc :instance)))
473             (signal 'invalid-slot-type (list ':allocation alloc)))
474
475         ;; The default type specifier is supposed to be t, meaning anything.
476         (if (not type) (setq type t))
477
478         ;; Label is nil, or a string
479         (if (not (or (null label) (stringp label)))
480             (signal 'invalid-slot-type (list ':label label)))
481         
482         ;; Is there an initarg, but allocation of class?
483         (if (and initarg (eq alloc :class))
484             (message "Class allocated slots do not need :initarg"))
485
486         ;; intern the symbol so we can use it blankly
487         (if initarg (set initarg initarg))
488
489         ;; The customgroup should be a list of symbols
490         (cond ((null customg)
491                (setq customg '(default)))
492               ((not (listp customg))
493                (setq customg (list customg))))
494         ;; The customgroup better be a symbol, or list o symbols.
495         (mapcar (lambda (cg)
496                   (if (not (symbolp cg))
497                       (signal 'invalid-slot-type (list ':group cg))))
498                 customg)
499
500         ;; First up, add this field into our new class.
501         (eieio-add-new-field newc name init docstr type custom label customg
502                              prot initarg alloc 'defaultoverride skip-nil)
503
504         ;; We need to id the group, and store them in a group list attribute.
505         (mapcar (lambda (cg) (add-to-list 'groups cg)) customg)
506
507         ;; anyone can have an accessor function.  This creates a function
508         ;; of the specified name, and also performs a `defsetf' if applicable
509         ;; so that users can `setf' the space returned by this function
510         (if acces
511             (progn
512               (eieio-defmethod acces
513                 (list (if (eq alloc :class) :STATIC :PRIMARY)
514                       (list (list 'this cname))
515                       (format
516                        "Retrieves the slot `%s' from an object of class `%s'"
517                        name cname)
518                       (list 'eieio-oref 'this (list 'quote name))))
519               ;; Thanks Pascal Bourguignon <pjb@informatimago.com>
520               ;; For this complex macro.
521               (eval (macroexpand
522                      (list  'defsetf acces '(widget) '(store)
523                             (list 'list ''eieio-oset 'widget
524                                   (list 'quote (list 'quote name)) 'store))))
525               ;;`(defsetf ,acces (widget) (store) (eieio-oset widget ',cname store))
526               )
527           )
528         ;; If a writer is defined, then create a generic method of that
529         ;; name whose purpose is to set the value of the slot.
530         (if writer
531             (progn
532               (eieio-defmethod writer
533                 (list (list (list 'this cname) 'value)
534                       (format "Set the slot `%s' of an object of class `%s'"
535                               name cname)
536                       `(setf (slot-value this ',name) value)))
537               ))
538         ;; If a reader is defined, then create a generic method
539         ;; of that name whose purpose is to access this slot value.
540         (if reader
541             (progn
542               (eieio-defmethod reader
543                 (list (list (list 'this cname))
544                       (format "Access the slot `%s' from object of class `%s'"
545                               name cname)
546                       `(slot-value this ',name)))))
547         )
548       (setq fields (cdr fields)))
549
550     ;; Now that everything has been loaded up, all our lists are backwards!  Fix that up now.
551     (aset newc class-public-a (nreverse (aref newc class-public-a)))
552     (aset newc class-public-d (nreverse (aref newc class-public-d)))
553     (aset newc class-public-doc (nreverse (aref newc class-public-doc)))
554     (aset newc class-public-type
555           (apply 'vector (nreverse (aref newc class-public-type))))
556     (aset newc class-public-custom (nreverse (aref newc class-public-custom)))
557     (aset newc class-public-custom-label (nreverse (aref newc class-public-custom-label)))
558     (aset newc class-public-custom-group (nreverse (aref newc class-public-custom-group)))
559     (aset newc class-protection (nreverse (aref newc class-protection)))
560     (aset newc class-initarg-tuples (nreverse (aref newc class-initarg-tuples)))
561
562     ;; The storage for class-class-allocation-type needs to be turned into
563     ;; a vector now.
564     (aset newc class-class-allocation-type
565           (apply 'vector (aref newc class-class-allocation-type)))
566
567     ;; Also, take class allocated values, and vectorize them for speed.
568     (aset newc class-class-allocation-values
569           (apply 'vector (aref newc class-class-allocation-values)))
570
571     ;; Attach field symbols into an obarray, and store the index of
572     ;; this field as the variable slot in this new symbol.  We need to
573     ;; know about primes, because obarrays are best set in vectors of
574     ;; prime number length, and we also need to make our vector small
575     ;; to save space, and also optimal for the number of items we have.
576     (let* ((cnt 0)
577            (pubsyms (aref newc class-public-a))
578            (prots (aref newc class-protection))
579            (l (length pubsyms))
580            (vl (let ((primes '( 3 5 7 11 13 17 19 23 29 31 37 41 43 47
581                                   53 59 61 67 71 73 79 83 89 97 101 )))
582                  (while (and primes (< (car primes) l))
583                    (setq primes (cdr primes)))
584                  (car primes)))
585            (oa (make-vector vl 0))
586            (newsym))
587       (while pubsyms
588         (setq newsym (intern (symbol-name (car pubsyms)) oa))
589         (set newsym cnt)
590         (setq cnt (1+ cnt))
591         (if (car prots) (put newsym 'protection (car prots)))
592         (setq pubsyms (cdr pubsyms)
593               prots (cdr prots)))
594       (aset newc class-symbol-obarray oa)
595       )
596
597     ;; Create the constructor function
598     (if (class-option-assoc options :abstract)
599         ;; Abstract classes cannot be instantiated.  Say so.
600         (let ((abs (class-option-assoc options :abstract)))
601           (if (not (stringp abs))
602               (setq abs (format "Class %s is abstract" cname)))
603           (fset cname
604                 `(lambda (&rest stuff)
605                    ,(format "You cannot create a new object of type %s" cname)
606                    (error ,abs))))
607
608       ;; Non-abstract classes need a constructor.
609       (fset cname
610             `(lambda (newname &rest fields)
611                ,(format "Create a new object with name NAME of class type %s" cname)
612                (apply 'constructor ,cname newname fields)))
613       )
614
615     ;; Set up a specialized doc string.
616     ;; Use stored value since it is calculated in a non-trivial way
617     (put cname 'variable-documentation
618          (class-option-assoc options :documentation))
619
620     ;; We have a list of custom groups.  Store them into the options.
621     (let ((g (class-option-assoc options :custom-groups)))
622       (mapcar (lambda (cg) (add-to-list 'g cg)) groups)
623       (if (memq :custom-groups options)
624           (setcar (cdr (memq :custom-groups options)) g)
625         (setq options (cons :custom-groups (cons g options)))))
626
627     ;; Set up the options we have collected.
628     (aset newc class-options options)
629
630     ;; if this is a superclass, clear out parent (which was set to the
631     ;; default superclass eieio-default-superclass)
632     (if clearparent (aset newc class-parent nil))
633
634     ;; Create the cached default object.
635     (let ((cache (make-vector (+ (length (aref newc class-public-a))
636                                  3) nil)))
637       (aset cache 0 'object)
638       (aset cache object-class cname)
639       (aset cache object-name 'default-cache-object)
640       (let ((eieio-skip-typecheck t))
641         ;; All type-checking has been done to our satisfaction
642         ;; before this call.  Don't waste our time in this call..
643         (eieio-set-defaults cache t))
644       (aset newc class-default-object-cache cache))
645
646     ;; Return our new class object
647     newc
648     ))
649
650 (defun eieio-perform-slot-validation-for-default (field spec value skipnil)
651   "For FIELD, signal if SPEC does not match VALUE.
652 If SKIPNIL is non-nil, then if VALUE is nil, return t."
653   (let ((val (eieio-default-eval-maybe value)))
654     (if (and (not eieio-skip-typecheck)
655              (not (and skipnil (null val)))
656              (not (eieio-perform-slot-validation spec val)))
657         (signal 'invalid-slot-type (list field spec val)))))
658
659 (defun eieio-add-new-field (newc a d doc type cust label custg prot init alloc
660                                  &optional defaultoverride skipnil)
661   "Add into NEWC attribute A.
662 If A already exists in NEWC, then do nothing.  If it doesn't exist,
663 then also add in D (defualt), DOC, TYPE, CUST, LABEL, CUSTG, PROT, and INIT arg.
664 Argument ALLOC specifies if the field is allocated per instance, or per class.
665 If optional DEFAULTOVERRIDE is non-nil, then if A exists in NEWC,
666 we must override it's value for a default.
667 Optional argument SKIPNIL indicates if type checking should be skipped
668 if default value is nil."
669   ;; Make sure we duplicate those items that are sequences.
670   (if (sequencep d) (setq d (copy-sequence d)))
671   (if (sequencep type) (setq type (copy-sequence type)))
672   (if (sequencep cust) (setq cust (copy-sequence cust)))
673   (if (sequencep custg) (setq custg (copy-sequence custg)))
674
675   ;; To prevent override information w/out specification of storage,
676   ;; we need to do this little hack.
677   (if (member a (aref newc class-class-allocation-a)) (setq alloc ':class))
678
679   (if (or (not alloc) (and (symbolp alloc) (eq alloc ':instance)))
680       ;; In this case, we modify the INSTANCE version of a given slot.
681       ;; Only add this element if it is so-far unique
682       (if (not (member a (aref newc class-public-a)))
683           (progn
684             (eieio-perform-slot-validation-for-default a type d skipnil)
685             (aset newc class-public-a (cons a (aref newc class-public-a)))
686             (aset newc class-public-d (cons d (aref newc class-public-d)))
687             (aset newc class-public-doc (cons doc (aref newc class-public-doc)))
688             (aset newc class-public-type (cons type (aref newc class-public-type)))
689             (aset newc class-public-custom (cons cust (aref newc class-public-custom)))
690             (aset newc class-public-custom-label (cons label (aref newc class-public-custom-label)))
691             (aset newc class-public-custom-group (cons custg (aref newc class-public-custom-group)))
692             (aset newc class-protection (cons prot (aref newc class-protection)))
693             (aset newc class-initarg-tuples (cons (cons init a) (aref newc class-initarg-tuples)))
694             )
695         ;; When defaultoverride is true, we are usually adding new local
696         ;; attributes which must override the default value of any field
697         ;; passed in by one of the parent classes.
698         (if defaultoverride
699             (progn
700               ;; There is a match, and we must override the old value.
701               (let* ((ca (aref newc class-public-a))
702                      (np (member a ca))
703                      (num (- (length ca) (length np)))
704                      (dp (if np (nthcdr num (aref newc class-public-d))
705                            nil))
706                      (tp (if np (nth num (aref newc class-public-type))))
707                      )
708                 (if (not np)
709                     (error "Eieio internal error overriding default value for %s"
710                            a)
711                   ;; If type is passed in, is it the same?
712                   (if (not (eq type t))
713                       (if (not (equal type tp))
714                           (error
715                            "Child slot type `%s' does not match inherited type `%s' for `%s'"
716                            type tp a)))
717                   ;; If we have a repeat, only update the initarg...
718                   (eieio-perform-slot-validation-for-default a tp d skipnil)
719                   (setcar dp d)
720                   ;; If we have a new initarg, check for it.
721                   (when init
722                     (let* ((inits (aref newc class-initarg-tuples))
723                            (inita (rassq a inits)))
724                       ;; Replace the CAR of the associate INITA.
725                       ;;(message "Initarg: %S replace %s" inita init)
726                       (setcar inita init)
727                       ))
728                   ;; TODO:
729                   ;;  For other slots (protection, etc) we should get the
730                   ;;  original value, and make sure each is equal to the
731                   ;;  last value and throw an error, or accept it.
732                   )))))
733     (let ((value (eieio-default-eval-maybe d)))
734       (if (not (member a (aref newc class-class-allocation-a)))
735           (progn
736             (eieio-perform-slot-validation-for-default a type value skipnil)
737             ;; Here we have found a :class version of a slot.  This
738             ;; requires a very different aproach.
739             (aset newc class-class-allocation-a (cons a (aref newc class-class-allocation-a)))
740             (aset newc class-class-allocation-doc (cons doc (aref newc class-class-allocation-doc)))
741             (aset newc class-class-allocation-type (cons type (aref newc class-class-allocation-type)))
742             (aset newc class-class-allocation-custom (cons cust (aref newc class-class-allocation-custom)))
743             (aset newc class-class-allocation-custom-label (cons label (aref newc class-class-allocation-custom-label)))
744             (aset newc class-class-allocation-custom-group (cons custg (aref newc class-class-allocation-custom-group)))
745             (aset newc class-class-allocation-protection (cons prot (aref newc class-class-allocation-protection)))
746   ;; Default value is stored in the 'values section, since new objects
747             ;; can't initialize from this element.
748             (aset newc class-class-allocation-values (cons value (aref newc class-class-allocation-values))))
749         (if defaultoverride
750             (progn
751               ;; There is a match, and we must override the old value.
752               (let* ((ca (aref newc class-class-allocation-a))
753                      (np (member a ca))
754                      (num (- (length ca) (length np)))
755                      (dp (if np
756                              (nthcdr num
757                                      (aref newc class-class-allocation-values))
758                            nil))
759                      (tp (if np (nth num (aref newc class-class-allocation-type))
760                            nil)))
761                 (if (not np)
762                     (error "Eieio internal error overriding default value for %s"
763                            a)
764                   ;; If type is passed in, is it the same?
765                   (if (not (eq type t))
766                       (if (not (equal type tp))
767                           (error
768                            "Child slot type `%s' does not match inherited type `%s' for `%s'"
769                            type tp a)))
770                   ;; If we have a repeat, only update the vlaue...
771                   (eieio-perform-slot-validation-for-default a tp value skipnil)
772                   (setcar dp value))
773                 )))))
774     ))
775
776 (defun eieio-copy-parents-into-subclass (newc parents)
777   "Copy into NEWC the fields of PARENTS.
778 Follow the rules of not overwritting early parents when applying to
779 the new child class."
780   (let ((ps (aref newc class-parent))
781         (sn (class-option-assoc (aref newc class-options)
782                                 ':allow-nil-initform)))
783     (while ps
784       ;; First, duplicate all the fields of the parent.
785       (let ((pcv (class-v (car ps))))
786         (let ((pa (aref pcv class-public-a))
787               (pd (aref pcv class-public-d))
788               (pdoc (aref pcv class-public-doc))
789               (ptype (aref pcv class-public-type))
790               (pcust (aref pcv class-public-custom))
791               (plabel (aref pcv class-public-custom-label))
792               (pcustg (aref pcv class-public-custom-group))
793               (pprot (aref pcv class-protection))
794               (pinit (aref pcv class-initarg-tuples))
795               (i 0))
796           (while pa
797             (eieio-add-new-field newc
798                                  (car pa) (car pd) (car pdoc) (aref ptype i)
799                                  (car pcust) (car plabel) (car pcustg)
800                                  (car pprot) (car-safe (car pinit)) nil nil sn)
801             ;; Increment each value.
802             (setq pa (cdr pa)
803                   pd (cdr pd)
804                   pdoc (cdr pdoc)
805                   i (1+ i)
806                   pcust (cdr pcust)
807                   plabel (cdr plabel)
808                   pcustg (cdr pcustg)
809                   pprot (cdr pprot)
810                   pinit (cdr pinit))
811             )) ;; while/let
812         ;; Now duplicate all the class alloc fields.
813         (let ((pa (aref pcv class-class-allocation-a))
814               (pdoc (aref pcv class-class-allocation-doc))
815               (ptype (aref pcv class-class-allocation-type))
816               (pcust (aref pcv class-class-allocation-custom))
817               (plabel (aref pcv class-class-allocation-custom-label))
818               (pcustg (aref pcv class-class-allocation-custom-group))
819               (pprot (aref pcv class-class-allocation-protection))
820               (pval (aref pcv class-class-allocation-values))
821               (i 0))
822           (while pa
823             (eieio-add-new-field newc
824                                  (car pa) (aref pval i) (car pdoc) (aref ptype i)
825                                  (car pcust) (car plabel) (car pcustg)
826                                  (car pprot) nil ':class sn)
827             ;; Increment each value.
828             (setq pa (cdr pa)
829                   pdoc (cdr pdoc)
830                   pcust (cdr pcust)
831                   plabel (cdr plabel)
832                   pcustg (cdr pcustg)
833                   pprot (cdr pprot)
834                   i (1+ i))
835             ))) ;; while/let
836       ;; Loop over each parent class
837       (setq ps (cdr ps)))
838     ))
839
840 ;;; CLOS style implementation of object creators.
841 ;;
842 (defun make-instance (class &rest initargs)
843   "Make a new instance of CLASS with NAME and initialization based on INITARGS.
844 The class' constructor requires a name for use when printing.
845 `make-instance' in CLOS doesn't use names the way Emacs does, so the
846 class is used as the name slot instead when INITARGS doesn't start with
847 a string.  The rest of INITARGS are label/value pairs.  The label's
848 are the symbols created with the :initarg tag from the `defclass' call.
849 The value is the value stored in that slot.
850 CLASS is a symbol.  Defclass will assign the class symbol to itself, so
851 the shortcut (make-instance foo) will work.  The form (make-instance 'foo)
852 is more robust."
853   (if (and (car initargs) (stringp (car initargs)))
854       (apply (class-constructor class) initargs)
855     (apply  (class-constructor class)
856             (cond ((symbolp class) (symbol-name class))
857                   (t (format "%S" class)))
858             initargs)))
859
860 \f
861 ;;; CLOS methods and generics
862 ;;
863 (defmacro defgeneric (method args &optional doc-string)
864   "Create a generic function METHOD.  ARGS is ignored.
865 DOC-STRING is the base documentation for this class.  A generic
866 function has no body, as it's purpose is to decide which method body
867 is appropriate to use.  Use `defmethod' to create methods, and it
868 calls defgeneric for you.  With this implementation the arguments are
869 currently ignored.  You can use `defgeneric' to apply specialized
870 top level documentation to a method."
871   `(eieio-defgeneric (quote ,method) ,doc-string))
872
873 (defun eieio-defgeneric-form (method doc-string)
874   "The lambda form that would be used as the function defined on METHOD.
875 All methods should call the same EIEIO function for dispatch.
876 DOC-STRING is the documentation attached to METHOD."
877   `(lambda (&rest local-args)
878      ,doc-string
879      (eieio-generic-call (quote ,method) local-args)))
880
881 (defun eieio-defgeneric (method doc-string)
882   "Engine part to `defgeneric' macro defining METHOD with DOC-STRING."
883   (if (and (fboundp method) (not (generic-p method))
884            (or (byte-code-function-p (symbol-function method))
885                (not (eq 'autoload (car (symbol-function method)))))
886            )
887       (error "You cannot create a generic/method over an existing symbol: %s"
888              method))
889   ;; Don't do this over and over.
890   (unless (fboundp 'method)
891     ;; This defun tells emacs where the first definition of this
892     ;; method is defined.
893     `(defun ,method nil)
894     ;; Apply the actual body of this function.
895     (fset method (eieio-defgeneric-form method doc-string))
896     ;; Make sure the method tables are installed.
897     (eieiomt-install method)
898     ;; Return the method
899     'method))
900
901 (defun eieio-unbind-method-implementations (method)
902   "Make the generic method METHOD have no implementations..
903 It will leave the original generic function in place, but remove
904 reference to all implementations of METHOD."
905   (put method 'eieio-method-tree nil)
906   (put method 'eieio-method-obarray nil))
907
908 (defmacro defmethod (method &rest args)
909   "Create a new METHOD through `defgeneric' with ARGS.
910 ARGS lists any keys (such as :BEFORE, :PRIMARY, :AFTER, or :STATIC),
911 the arglst, and doc string, and eventually the body, such as:
912
913  (defmethod mymethod [:BEFORE | :PRIMARY | :AFTER | :STATIC] (args)
914     doc-string body)"
915   `(eieio-defmethod (quote ,method) (quote ,args)))
916
917 (defun eieio-defmethod (method args)
918   "Work part of the `defmethod' macro defining METHOD with ARGS."
919   (let ((key nil) (body nil) (firstarg nil) (argfix nil) (argclass nil) loopa)
920     ;; find optional keys
921     (setq key
922           (cond ((eq ':BEFORE (car args))
923                  (setq args (cdr args))
924                  method-before)
925                 ((eq ':AFTER (car args))
926                  (setq args (cdr args))
927                  method-after)
928                 ((eq ':PRIMARY (car args))
929                  (setq args (cdr args))
930                  method-primary)
931                 ((eq ':STATIC (car args))
932                  (setq args (cdr args))
933                  method-static)
934                 ;; Primary key
935                 (t method-primary)))
936     ;; get body, and fix contents of args to be the arguments of the fn.
937     (setq body (cdr args)
938           args (car args))
939     (setq loopa args)
940     ;; Create a fixed version of the arguments
941     (while loopa
942       (setq argfix (cons (if (listp (car loopa)) (car (car loopa)) (car loopa))
943                          argfix))
944       (setq loopa (cdr loopa)))
945     ;; make sure there is a generic
946     (eieio-defgeneric
947      method
948      (if (stringp (car body))
949          (car body) (format "Generically created method `%s'" method)))
950     ;; create symbol for property to bind to.  If the first arg is of
951     ;; the form (varname vartype) and `vartype' is a class, then
952     ;; that class will be the type symbol.  If not, then it will fall
953     ;; under the type `primary' which is a non-specific calling of the
954     ;; function.
955     (setq firstarg (car args))
956     (if (listp firstarg)
957         (progn
958           (setq argclass  (nth 1 firstarg))
959           (if (not (class-p argclass))
960               (error "Unknown class type %s in method parameters"
961                      (nth 1 firstarg))))
962       (if (= key -1)
963           (signal 'wrong-type-argument (list :STATIC 'non-class-arg)))
964       ;; generics are higher
965       (setq key (+ key 3)))
966     ;; Put this lambda into the symbol so we can find it
967     (if (byte-code-function-p (car-safe body))
968         (eieiomt-add method (car-safe body) key argclass)
969       (eieiomt-add method (append (list 'lambda (reverse argfix)) body)
970                    key argclass))
971     )
972   method)
973
974 ;;; Slot type validation
975 ;;
976 (defun eieio-perform-slot-validation (spec value)
977   "Return non-nil if SPEC does not match VALUE."
978   ;; typep is in cl-macs
979   (or (eq spec t)                       ; t always passes
980       (eq value eieio-unbound)          ; unbound always passes
981       (typep value spec)))
982
983 (defun eieio-validate-slot-value (class field-idx value field)
984   "Make sure that for CLASS referencing FIELD-IDX, that VALUE is valid.
985 Checks the :type specifier.
986 FIELD is the field that is being checked, and is only used when throwing
987 and error."
988   (if eieio-skip-typecheck
989       nil
990     ;; Trim off object IDX junk added in for the object index.
991     (setq field-idx (- field-idx 3))
992     (let ((st (aref (aref (class-v class) class-public-type) field-idx)))
993       (if (not (eieio-perform-slot-validation st value))
994           (signal 'invalid-slot-type (list class field st value))))))
995
996 (defun eieio-validate-class-slot-value (class field-idx value field)
997   "Make sure that for CLASS referencing FIELD-IDX, that VALUE is valid.
998 Checks the :type specifier.
999 FIELD is the field that is being checked, and is only used when throwing
1000 and error."
1001   (if eieio-skip-typecheck
1002       nil
1003     (let ((st (aref (aref (class-v class) class-class-allocation-type)
1004                     field-idx)))
1005       (if (not (eieio-perform-slot-validation st value))
1006           (signal 'invalid-slot-type (list class field st value))))))
1007
1008 (defun eieio-barf-if-slot-unbound (value instance slotname fn)
1009   "Throw a signal if VALUE is a representation of an UNBOUND slot.
1010 INSTANCE is the object being referenced.  SLOTNAME is the offending
1011 slot.  If the slot is ok, return VALUE.
1012 Argument FN is the function calling this verifier."
1013   (if (and (eq value eieio-unbound) (not eieio-skip-typecheck))
1014       (slot-unbound instance (object-class instance) slotname fn)
1015     value))
1016
1017 ;;; Missing types that are useful to me.
1018 ;;
1019 (defun boolean-p (bool)
1020   "Return non-nil if BOOL is nil or t."
1021   (or (null bool) (eq bool t)))
1022
1023 ;;; Get/Set slots in an object.
1024 ;;
1025 (defmacro oref (obj field)
1026   "Retrieve the value stored in OBJ in the slot named by FIELD.
1027 Field is the name of the slot when created by `defclass' or the label
1028 created by the :initarg tag."
1029   `(eieio-oref ,obj (quote ,field)))
1030
1031 (defun eieio-oref (obj field)
1032   "Return the value in OBJ at FIELD in the object vector."
1033   (if (not (or (object-p obj) (class-p obj)))
1034       (signal 'wrong-type-argument (list '(or object-p class-p) obj)))
1035   (if (not (symbolp field))
1036       (signal 'wrong-type-argument (list 'symbolp field)))
1037   (let* ((class (if (class-p obj) obj (aref obj object-class)))
1038          (c (eieio-field-name-index class obj field)))
1039     (if (not c)
1040         ;; It might be missing because it is a :class allocated field.
1041         ;; Lets check that info out.
1042         (if (setq c (eieio-class-field-name-index class field))
1043             ;; Oref that slot.
1044             (aref (aref (class-v class) class-class-allocation-values) c)
1045           ;; The slot-missing method is a cool way of allowing an object author
1046           ;; to intercept missing slot definitions.  Since it is also the LAST
1047           ;; thing called in this fn, it's return value would be retrieved.
1048           (slot-missing obj field 'oref)
1049           ;;(signal 'invalid-slot-name (list (object-name obj) field))
1050           )
1051       (if (not (object-p obj))
1052           (signal 'wrong-type-argument (list 'object-p obj)))
1053       (eieio-barf-if-slot-unbound (aref obj c) obj field 'oref))))
1054
1055 (defalias 'slot-value 'eieio-oref)
1056 (defalias 'set-slot-value 'eieio-oset)
1057
1058 ;; This alias is needed so that functions can be written
1059 ;; for defaults, but still behave like lambdas.
1060 (defmacro lambda-default (&rest cdr)
1061   "The same as `lambda' but is used as a default value in `defclass'.
1062 As such, the form (lambda-default ARGS DOCSTRING INTERACTIVE BODY) is
1063 self quoting.  This macro is meant for the sole purpose of quoting
1064 lambda expressions into class defaults.  Any `lambda-default'
1065 expression is automatically transformed into a `lambda' expression
1066 when copied from the defaults into a new object.  The use of
1067 `oref-default', however, will return a `lambda-default' expression.
1068 CDR is function definition and body."
1069   ;; This definition is copied directly from subr.el for lambda
1070   (list 'function (cons 'lambda-default cdr)))
1071
1072 (put 'lambda-default 'lisp-indent-function 'defun)
1073 (put 'lambda-default 'byte-compile 'byte-compile-lambda-form)
1074
1075 (defmacro oref-default (obj field)
1076   "Gets the default value of OBJ (maybe a class) for FIELD.
1077 The default value is the value installed in a class with the :initform
1078 tag.  FIELD can be the slot name, or the tag specified by the :initarg
1079 tag in the `defclass' call."
1080   `(eieio-oref-default ,obj (quote ,field)))
1081
1082 (defun eieio-oref-default (obj field)
1083   "Does the work for the macro `oref-default' with similar parameters.
1084 Fills in OBJ's FIELD with it's default value."
1085   (if (not (or (object-p obj) (class-p obj))) (signal 'wrong-type-argument (list 'object-p obj)))
1086   (if (not (symbolp field)) (signal 'wrong-type-argument (list 'symbolp field)))
1087   (let* ((cl (if (object-p obj) (aref obj object-class) obj))
1088          (c (eieio-field-name-index cl obj field)))
1089     (if (not c)
1090         ;; It might be missing because it is a :class allocated field.
1091         ;; Lets check that info out.
1092         (if (setq c
1093                   (eieio-class-field-name-index cl field))
1094             ;; Oref that slot.
1095             (aref (aref (class-v cl) class-class-allocation-values)
1096                   c)
1097           (slot-missing obj field 'oref-default)
1098           ;;(signal 'invalid-slot-name (list (class-name cl) field))
1099           )
1100       (eieio-barf-if-slot-unbound
1101        (let ((val (nth (- c 3) (aref (class-v cl) class-public-d))))
1102          (eieio-default-eval-maybe val))
1103        obj cl 'oref-default))))
1104
1105 (defun eieio-default-eval-maybe (val)
1106   "Check VAL, and return what `oref-default' would provide."
1107   ;; check for functions to evaluate
1108   (if (and (listp val) (equal (car val) 'lambda))
1109       (funcall val)
1110     ;; check for quoted things, and unquote them
1111     (if (and (listp val) (eq (car val) 'quote))
1112         (car (cdr val))
1113       ;; return it verbatim
1114       (if (and (listp val) (eq (car val) 'lambda-default))
1115           (let ((s (copy-sequence val)))
1116             (setcar s 'lambda)
1117             s)
1118         val))))
1119
1120 ;;; Object Set macros
1121 ;;
1122 (defmacro oset (obj field value)
1123   "Set the value in OBJ for slot FIELD to VALUE.
1124 FIELD is the slot name as specified in `defclass' or the tag created
1125 with in the :initarg slot.  VALUE can be any Lisp object."
1126   `(eieio-oset ,obj (quote ,field) ,value))
1127
1128 (defun eieio-oset (obj field value)
1129   "Does the work for the macro `oset'.
1130 Fills in OBJ's FIELD with VALUE."
1131   (if (not (object-p obj)) (signal 'wrong-type-argument (list 'object-p obj)))
1132   (if (not (symbolp field)) (signal 'wrong-type-argument (list 'symbolp field)))
1133   (let ((c (eieio-field-name-index (object-class-fast obj) obj field)))
1134     (if (not c)
1135         ;; It might be missing because it is a :class allocated field.
1136         ;; Lets check that info out.
1137         (if (setq c
1138                   (eieio-class-field-name-index (aref obj object-class) field))
1139             ;; Oset that slot.
1140             (progn
1141               (eieio-validate-class-slot-value (object-class-fast obj) c value field)
1142               (aset (aref (class-v (aref obj object-class))
1143                           class-class-allocation-values)
1144                     c value))
1145           ;; See oref for comment on `slot-missing'
1146           (slot-missing obj field 'oset value)
1147           ;;(signal 'invalid-slot-name (list (object-name obj) field))
1148           )
1149       (eieio-validate-slot-value (object-class-fast obj) c value field)
1150       (aset obj c value))))
1151
1152 (defmacro oset-default (class field value)
1153   "Set the default slot in CLASS for FIELD to VALUE.
1154 The default value is usually set with the :initform tag during class
1155 creation.  This allows users to change the default behavior of classes
1156 after they are created."
1157   `(eieio-oset-default ,class (quote ,field) ,value))
1158
1159 (defun eieio-oset-default (class field value)
1160   "Does the work for the macro `oset-default'.
1161 Fills in the default value in CLASS' in FIELD with VALUE."
1162   (if (not (class-p class)) (signal 'wrong-type-argument (list 'class-p class)))
1163   (if (not (symbolp field)) (signal 'wrong-type-argument (list 'symbolp field)))
1164   (let* ((scoped-class class)
1165          (c (eieio-field-name-index class nil field)))
1166     (if (not c)
1167         ;; It might be missing because it is a :class allocated field.
1168         ;; Lets check that info out.
1169         (if (setq c (eieio-class-field-name-index class field))
1170             (progn
1171               ;; Oref that slot.
1172               (eieio-validate-class-slot-value class c value field)
1173               (aset (aref (class-v class) class-class-allocation-values) c
1174                     value))
1175           (signal 'invalid-slot-name (list (class-name class) field)))
1176       (eieio-validate-slot-value class c value field)
1177       ;; Set this into the storage for defaults.
1178       (setcar (nthcdr (- c 3) (aref (class-v class) class-public-d))
1179               value)
1180       ;; Take the value, and put it into our cache object.
1181       (eieio-oset (aref (class-v class) class-default-object-cache)
1182                   field value)
1183       )))
1184
1185 ;;; Handy CLOS macros
1186 ;;
1187 (defmacro with-slots (spec-list object &rest body)
1188   "The macro with-slots establishes a lexical environment for
1189 referring to the slots in the instance named by the given
1190 slot-names as though they were variables. Within such a context
1191 the value of the slot can be specified by using its slot name,
1192 as if it were a lexically bound variable. Both setf and setq
1193 can be used to set the value of the slot."
1194   ;; Transform the spec-list into a symbol-macrolet spec-list.
1195   (let ((mappings (mapcar (lambda (entry)
1196                             (let ((var  (if (listp entry) (car entry) entry))
1197                                   (slot (if (listp entry) (cadr entry) entry)))
1198                               (list var `(slot-value ,object ',slot))))
1199                           spec-list)))
1200     (append (list 'symbol-macrolet mappings)
1201             body)))
1202 (put 'with-slots 'lisp-indent-function 2)
1203
1204 \f
1205 ;;; Simple generators, and query functions.  None of these would do
1206 ;;  well embedded into an object.
1207 ;;
1208 (defmacro object-class-fast (obj) "Return the class struct defining OBJ with no check."
1209   `(aref ,obj object-class))
1210   
1211 (defun class-name (class) "Return a Lisp like symbol name for CLASS."
1212   (if (not (class-p class)) (signal 'wrong-type-argument (list 'class-p class)))
1213   ;; I think this is supposed to return a symbol, but to me CLASS is a symbol,
1214   ;; and I wanted a string.  Arg!
1215   (format "#<class %s>" (symbol-name class)))
1216
1217 (defun object-name (obj &optional extra)
1218   "Return a Lisp like symbol string for object OBJ.
1219 If EXTRA, include that in the string returned to represent the symbol."
1220   (if (not (object-p obj)) (signal 'wrong-type-argument (list 'object-p obj)))
1221   (format "#<%s %s%s>" (symbol-name (object-class-fast obj))
1222           (aref obj object-name) (or extra "")))
1223
1224 (defun object-name-string (obj) "Return a string which is OBJ's name."
1225   (if (not (object-p obj)) (signal 'wrong-type-argument (list 'object-p obj)))
1226   (aref obj object-name))
1227
1228 (defun object-set-name-string (obj name) "Set the string which is OBJ's NAME."
1229   (if (not (object-p obj)) (signal 'wrong-type-argument (list 'object-p obj)))
1230   (if (not (stringp name)) (signal 'wrong-type-argument (list 'stringp name)))
1231   (aset obj object-name name))
1232
1233 (defun object-class (obj) "Return the class struct defining OBJ."
1234   (if (not (object-p obj)) (signal 'wrong-type-argument (list 'object-p obj)))
1235   (object-class-fast obj))
1236 (defalias 'class-of 'object-class)
1237
1238 (defun object-class-name (obj) "Return a Lisp like symbol name for OBJ's class."
1239   (if (not (object-p obj)) (signal 'wrong-type-argument (list 'object-p obj)))
1240   (class-name (object-class-fast obj)))
1241
1242 (defmacro class-parents-fast (class) "Return parent classes to CLASS with no check."
1243   `(aref (class-v ,class) class-parent))
1244
1245 (defun class-parents (class) "Return parent classes to CLASS.  (overload of variable)."
1246   (if (not (class-p class)) (signal 'wrong-type-argument (list 'class-p class)))
1247   (class-parents-fast class))
1248
1249 (defmacro class-children-fast (class) "Return child classes to CLASS with no check."
1250   `(aref (class-v ,class) class-children))
1251
1252 (defun class-children (class) "Return child classses to CLASS."
1253   (if (not (class-p class)) (signal 'wrong-type-argument (list 'class-p class)))
1254   (class-children-fast class))
1255
1256 (defmacro class-parent-fast (class) "Return first parent class to CLASS with no check."
1257   `(car (class-parents-fast ,class)))
1258
1259 (defmacro class-parent (class) "Return first parent class to CLASS.  (overload of variable)."
1260   `(car (class-parents ,class)))
1261
1262 (defmacro same-class-fast-p (obj class) "Return t if OBJ is of class-type CLASS with no error checking."
1263   `(eq (aref ,obj object-class) ,class))
1264
1265 (defun same-class-p (obj class) "Return t if OBJ is of class-type CLASS."
1266   (if (not (class-p class)) (signal 'wrong-type-argument (list 'class-p class)))
1267   (if (not (object-p obj)) (signal 'wrong-type-argument (list 'object-p obj)))
1268   (same-class-fast-p obj class))
1269
1270 (defun object-of-class-p (obj class)
1271   "Return non-nil if OBJ is an instance of CLASS or CLASS' subclasses."
1272   (if (not (object-p obj)) (signal 'wrong-type-argument (list 'object-p obj)))
1273   ;; class will be checked one layer down
1274   (child-of-class-p (aref obj object-class) class))
1275 ;; Backwards compatibility
1276 (defalias 'obj-of-class-p 'object-of-class-p)
1277
1278 (defun child-of-class-p (child class)
1279   "If CHILD class is a subclass of CLASS."
1280   (if (not (class-p class)) (signal 'wrong-type-argument (list 'class-p class)))
1281   (if (not (class-p child)) (signal 'wrong-type-argument (list 'class-p child)))
1282   (let ((p nil))
1283     (while (and child (not (eq child class)))
1284       (setq p (append p (aref (class-v child) class-parent))
1285             child (car p)
1286             p (cdr p)))
1287     (if child t)))
1288
1289 (defun object-slots (obj) "List of slots available in OBJ."
1290   (if (not (object-p obj)) (signal 'wrong-type-argument (list 'object-p obj)))
1291   (aref (class-v (object-class-fast obj)) class-public-a))
1292
1293 (defun class-slot-initarg (class slot) "Fetch from CLASS, SLOT's :initarg."
1294   (if (not (class-p class)) (signal 'wrong-type-argument (list 'class-p class)))
1295   (let ((ia (aref (class-v class) class-initarg-tuples))
1296         (f nil))
1297     (while (and ia (not f))
1298       (if (eq (cdr (car ia)) slot)
1299           (setq f (car (car ia))))
1300       (setq ia (cdr ia)))
1301     f))
1302
1303 ;;; CLOS queries into classes and slots
1304 ;;
1305 (defun slot-boundp (object slot)
1306   "Non-nil if OBJECT's SLOT is bound.
1307 Setting a slot's value makes it bound.  Calling `slot-makeunbound' will
1308 make a slot unbound.
1309 OBJECT can be an instance or a class."
1310   ;; Skip typechecking while retrieving this value.
1311   (let ((eieio-skip-typecheck t))
1312     ;; Return nil if the magic symbol is in there.
1313     (if (object-p object)
1314         (if (eq (eieio-oref object slot) eieio-unbound) nil t)
1315       (if (class-p object)
1316           (if (eq (eieio-oref-default object slot) eieio-unbound) nil t)
1317         (signal 'wrong-type-argument (list 'object-p object))))))
1318
1319 (defun slot-makeunbound (object slot)
1320   "In OBJECT, make SLOT unbound."
1321   (eieio-oset object slot eieio-unbound))
1322
1323 (defun slot-exists-p (object-or-class slot)
1324   "Non-nil if OBJECT-OR-CLASS has SLOT."
1325   (let ((cv (class-v (cond ((object-p object-or-class)
1326                             (object-class object-or-class))
1327                            ((class-p object-or-class)
1328                             object-or-class))
1329                      )))
1330     (or (memq slot (aref cv class-public-a))
1331         (memq slot (aref cv class-class-allocation-a)))
1332     ))
1333
1334 (defun find-class (symbol &optional errorp)
1335   "Return the class that SYMBOL represents. (CLOS function)
1336 If there is no class, nil is returned if ERRORP is nil."
1337   (if (not (class-p symbol))
1338       (if errorp (signal 'wrong-type-argument (list 'class-p symbol))
1339         nil)
1340     (class-v symbol)))
1341
1342 ;;; Slightly more complex utility functions for objects
1343 ;;
1344 (defun object-assoc (key field list)
1345   "Return non-nil if KEY is `equal' to the FIELD of the car of objects in LIST.
1346 The value is actually the element of LIST whose field equals KEY."
1347   (if (not (listp list)) (signal 'wrong-type-argument (list 'listp list)))
1348   (while (and list (not (condition-case nil
1349                             ;; This prevents errors for missing slots.
1350                             (equal key (eieio-oref (car list) field))
1351                           (error nil))))
1352     (setq list (cdr list)))
1353   (car list))
1354
1355 (defun object-assoc-list (field list)
1356   "Return an association list with the contents of FIELD as the key element.
1357 LIST must be a list of objects with FIELD in it.
1358 This is useful when you need to do completing read on an object group."
1359   (if (not (listp list)) (signal 'wrong-type-argument (list 'listp list)))
1360   (let ((assoclist nil))
1361     (while list
1362       (setq assoclist (cons (cons (eieio-oref (car list) field)
1363                                   (car list))
1364                             assoclist))
1365       (setq list (cdr list)))
1366     (nreverse assoclist)))
1367
1368 (defun object-assoc-list-safe (field list)
1369   "Return an association list with the contents of FIELD as the key element.
1370 LIST must be a list of objects, but those objects do not need to have
1371 FIELD in it.  If it does not, then that element is left out of the association
1372 list."
1373   (if (not (listp list)) (signal 'wrong-type-argument (list 'listp list)))
1374   (let ((assoclist nil))
1375     (while list
1376       (if (slot-exists-p (car list) field)
1377           (setq assoclist (cons (cons (eieio-oref (car list) field)
1378                                       (car list))
1379                                 assoclist)))
1380       (setq list (cdr list)))
1381     (nreverse assoclist)))
1382
1383 (defun object-add-to-list (object slot item &optional append)
1384   "In OBJECT's SLOT, add ITEM to the pre-existing list of elements.
1385 Optional argument APPEND indicates we need to append to the list.
1386 If ITEM already exists in the list in SLOT, then it is not added.
1387 Comparison is done with `equal' through the `member' function call.
1388 If SLOT is unbound, bind it to the list containing ITEM."
1389   (let (ov)
1390     ;; Find the originating list.
1391     (if (not (slot-boundp object slot))
1392         (setq ov (list item))
1393       (setq ov (eieio-oref object slot))
1394       ;; turn it into a list.
1395       (unless (listp ov)
1396         (setq ov (list ov)))
1397       ;; Do the combination
1398       (if (not (member item ov))
1399           (setq ov
1400                 (if append
1401                     (append ov (list item))
1402                   (cons item ov)))))
1403     ;; Set back into the slot.
1404     (eieio-oset object slot ov)))
1405
1406 (defun object-remove-from-list (object slot item)
1407   "In OBJECT's SLOT, remove occurrences ITEM.
1408 If ITEM already exists in the list in SLOT, then it is not added.
1409 Comparison is done with `equal' through the `delete' function call.
1410 If SLOT is unbound, do nothing."
1411   (if (not (slot-boundp object slot))
1412       nil
1413     (eieio-oset object slot (delete item (eieio-oref object slot)))))
1414 \f
1415 ;;; EIEIO internal search functions
1416 ;;
1417 (defun eieio-field-originating-class-p (start-class field)
1418   "Return Non-nil if START-CLASS is the first class to define FIELD.
1419 This is for testing if `scoped-class' is the class that defines FIELD
1420 so that we can protect private slots."
1421   (let ((par (class-parents start-class))
1422         (ret t))
1423     (if (not par)
1424         t
1425       (while (and par ret)
1426         (if (intern-soft (symbol-name field)
1427                          (aref (class-v (car par))
1428                                class-symbol-obarray))
1429             (setq ret nil))
1430         (setq par (cdr par)))
1431       ret)))
1432
1433 (defun eieio-field-name-index (class obj field)
1434   "In CLASS for OBJ find the index of the named FIELD.
1435 The field is a symbol which is installed in CLASS by the `defclass'
1436 call.  OBJ can be nil, but if it is an object, and the slot in question
1437 is protected, access will be allowed if obj is a child of the currently
1438 `scoped-class'.
1439 If FIELD is the value created with :initarg instead,
1440 reverse-lookup that name, and recurse with the associated slot value."
1441   ;; Removed checks to outside this call
1442   (let* ((fsym (intern-soft (symbol-name field)
1443                             (aref (class-v class)
1444                                   class-symbol-obarray)))
1445          (fsi (if (symbolp fsym) (symbol-value fsym) nil)))
1446     (if (integerp fsi)
1447         (cond
1448          ((not (get fsym 'protection))
1449           (+ 3 fsi))
1450          ((and (eq (get fsym 'protection) 'protected)
1451                scoped-class
1452                (or (child-of-class-p class scoped-class)
1453                    (and (object-p obj)
1454                         (child-of-class-p class (object-class obj)))))
1455           (+ 3 fsi))
1456          ((and (eq (get fsym 'protection) 'private)
1457                (or (and scoped-class
1458                         (eieio-field-originating-class-p scoped-class field))
1459                    eieio-initializing-object))
1460           (+ 3 fsi))
1461          (t nil))
1462       (let ((fn (eieio-initarg-to-attribute class field)))
1463         (if fn (eieio-field-name-index class obj fn) nil)))))
1464
1465 (defun eieio-class-field-name-index (class field)
1466   "In CLASS find the index of the named FIELD.
1467 The field is a symbol which is installed in CLASS by the `defclass'
1468 call.  If FIELD is the value created with :initarg instead,
1469 reverse-lookup that name, and recurse with the associated slot value."
1470   ;; This will happen less often, and with fewer slots.  Do this the
1471   ;; storage cheap way.
1472   (let* ((a (aref (class-v class) class-class-allocation-a))
1473          (l1 (length a))
1474          (af (memq field a))
1475          (l2 (length af)))
1476     ;; Slot # is length of the total list, minus the remaining list of
1477     ;; the found slot.
1478     (if af (- l1 l2))))
1479 \f
1480 ;;; CLOS generics internal function handling
1481 ;;
1482 (defvar eieio-generic-call-methodname nil
1483   "When using `call-next-method', provides a context on how to do it.")
1484 (defvar eieio-generic-call-arglst nil
1485   "When using `call-next-method', provides a context for parameters.")
1486 (defvar eieio-generic-call-key nil
1487   "When using `call-next-method', provides a context for the current key.
1488 Keys are a number representing :BEFORE, :PRIMARY, and :AFTER methods.")
1489 (defvar eieio-generic-call-next-method-list nil
1490   "When executing a PRIMARY or STATIC method, track the 'next-method'.
1491 During executions, the list is first generated, then as each next method
1492 is called, the next method is popped off the stack.")
1493
1494 (defun eieio-generic-call (method args)
1495   "Call METHOD with ARGS.
1496 ARGS provides the context on which implementation to use.
1497 This should only be called from a generic function."
1498   ;; We must expand our arguments first as they are always
1499   ;; passed in as quoted symbols
1500   (let ((newargs nil) (mclass nil)  (lambdas nil) (tlambdas nil) (keys nil)
1501         (eieio-generic-call-methodname method)
1502         (eieio-generic-call-arglst args)
1503         (firstarg nil)
1504         (primarymethodlist nil))
1505     ;; get a copy
1506     (setq newargs args
1507           firstarg (car newargs))
1508     ;; Is the class passed in autoloaded?
1509     ;; Since class names are also constructors, they can be autoloaded
1510     ;; via the autoload command.  Check for this, and load them in.
1511     ;; It's ok if it doesn't turn out to be a class.  Probably want that
1512     ;; function loaded anyway.
1513     (if (and (symbolp firstarg)
1514              (fboundp firstarg)
1515              (listp (symbol-function firstarg))
1516              (eq 'autoload (car (symbol-function firstarg))))
1517         (load (nth 1 (symbol-function firstarg))))
1518     ;; lookup the forms to use
1519     (cond ((object-p firstarg)
1520            (setq mclass (object-class-fast firstarg)))
1521           ((class-p firstarg)
1522            (setq mclass firstarg
1523                  )))
1524     ;; Now create a list in reverse order of all the calls we have
1525     ;; make in order to successfully do this right.  Rules:
1526     ;; 1) Only call generics if scoped-class is not defined
1527     ;;    This prevents multiple calls in the case of recursion
1528     ;; 2) Only call static if this is a static method.
1529     ;; 3) Only call specifics if the definition allows for them.
1530     ;; 4) Call in order based on :BEFORE, :PRIMARY, and :AFTER
1531     (when (object-p firstarg)
1532       ;; Non-static calls do all this stuff.
1533
1534       ;; :AFTER methods
1535       (setq tlambdas
1536             (if mclass
1537                 (eieiomt-method-list method method-after mclass)
1538               (list (eieio-generic-form method method-after nil)))
1539             ;;(or (and mclass (eieio-generic-form method method-after mclass))
1540             ;;  (eieio-generic-form method method-after nil))
1541             )
1542       (setq lambdas (append tlambdas lambdas)
1543             keys (append (make-list (length tlambdas) method-after) keys))
1544       
1545       ;; :PRIMARY methods
1546       (setq tlambdas
1547             (or (and mclass (eieio-generic-form method method-primary mclass))
1548                 (eieio-generic-form method method-primary nil)))
1549       (when tlambdas
1550         (setq lambdas (cons tlambdas lambdas)
1551               keys (cons method-primary keys)
1552               primarymethodlist
1553               (eieiomt-method-list method method-primary mclass)))
1554
1555       ;; :BEFORE methods
1556       (setq tlambdas
1557             (if mclass
1558                 (eieiomt-method-list method method-before mclass)
1559               (list (eieio-generic-form method method-before nil)))
1560             ;;(or (and mclass (eieio-generic-form method method-before mclass))
1561             ;;  (eieio-generic-form method method-before nil))
1562             )
1563       (setq lambdas (append tlambdas lambdas)
1564             keys (append (make-list (length tlambdas) method-before) keys))
1565       )
1566
1567     ;; If there were no methods found, then there could be :STATIC methods.
1568     (when (not lambdas)
1569       (setq tlambdas
1570             (eieio-generic-form method method-static mclass))
1571       (setq lambdas (cons tlambdas lambdas)
1572             keys (cons method-static keys)
1573             primarymethodlist  ;; Re-use even with bad name here
1574             (eieiomt-method-list method method-static mclass)))
1575
1576     ;; Now loop through all occurances forms which we must execute
1577     ;; (which are happily sorted now) and execute them all!
1578     (let ((rval nil) (lastval nil) (rvalever nil) (found nil))
1579       (while lambdas
1580         (if (car lambdas)
1581             (let* ((scoped-class (cdr (car lambdas)))
1582                    (eieio-generic-call-key (car keys))
1583                    (has-return-val
1584                     (or (= eieio-generic-call-key method-primary)
1585                         (= eieio-generic-call-key method-static)))
1586                    (eieio-generic-call-next-method-list
1587                     ;; Use the cdr, as the first element is the fcn
1588                     ;; we are calling right now.
1589                     (when has-return-val (cdr primarymethodlist)))
1590                    )
1591               (setq found t)
1592               ;;(setq rval (apply (car (car lambdas)) newargs))
1593               (setq lastval (apply (car (car lambdas)) newargs))
1594               (when has-return-val
1595                 (setq rval lastval
1596                       rvalever t))
1597               ))
1598         (setq lambdas (cdr lambdas)
1599               keys (cdr keys)))
1600       (if (not found)
1601           (if (object-p (car args))
1602               (setq rval (no-applicable-method (car args) method)
1603                     rvalever t)
1604             (signal
1605              'no-method-definition
1606              (list method args))))
1607       ;; Right Here... it could be that lastval is returned when
1608       ;; rvalever is nil.  Is that right?
1609       rval)))
1610
1611 (defun eieiomt-method-list (method key class)
1612   "Return an alist list of methods lambdas.
1613 METHOD is the method name.
1614 KEY represents either :BEFORE, or :AFTER methods.
1615 CLASS is the starting class to search from in the method tree."
1616   (let ((lambdas nil)
1617         (mclass (list class)))
1618     (while mclass
1619       (when (car mclass)
1620         ;; lookup the form to use for the PRIMARY object for the next level
1621         (let ((tmpl (eieio-generic-form method key (car mclass))))
1622           (when (or (not lambdas) 
1623                     ;; This prevents duplicates coming out of the
1624                     ;; class method optimizer.  Perhaps we should
1625                     ;; just not optimize before/afters?
1626                     (not (eq (car tmpl) (car (car lambdas)))))
1627             (setq lambdas (cons tmpl lambdas))
1628             (if (null (car lambdas))
1629                 (setq lambdas (cdr lambdas))))))
1630       ;; Add new classes to mclass
1631       (setq mclass (append (cdr mclass) (eieiomt-next (car mclass))))
1632       )
1633     (if (eq key method-after)
1634         lambdas
1635       (nreverse lambdas))))
1636
1637 (defun next-method-p ()
1638   "Return a list of lambdas which qualify as the `next-method'."
1639   eieio-generic-call-next-method-list)
1640
1641 (defun call-next-method (&rest replacement-args)
1642   "Call the next logical method from another method.
1643 The next logical method is the method belong to the parent class of
1644 the currently running method.  If REPLACEMENT-ARGS is non-nil, then
1645 use them instead of `eieio-generic-call-arglst'.  The generic arg list
1646 are the arguments passed in at the top level."
1647   (if (not scoped-class)
1648       (error "Call-next-method not called within a class specific method"))
1649   (if (and (/= eieio-generic-call-key method-primary)
1650            (/= eieio-generic-call-key method-static))
1651       (error "Cannot `call-next-method' except in :PRIMARY or :STATIC methods")
1652     )
1653   (let ((newargs (or replacement-args eieio-generic-call-arglst))
1654         (next (car eieio-generic-call-next-method-list))
1655         (returnval nil)
1656         )
1657     (if (or (not next) (not (car next)))
1658         (no-next-method (car newargs))
1659       (let* ((eieio-generic-call-next-method-list
1660               (cdr eieio-generic-call-next-method-list))
1661              (scoped-class (cdr next))
1662              (fcn (car next))
1663              )
1664         (apply fcn newargs)
1665         ))))
1666 \f
1667 ;;;
1668 ;; eieio-method-tree : eieiomt-
1669 ;;
1670 ;; Stored as eieio-method-tree in property list of a generic method
1671 ;;
1672 ;; (eieio-method-tree . [BEFORE PRIMARY AFTER
1673 ;;                       genericBEFORE genericPRIMARY genericAFTER])
1674 ;; and
1675 ;; (eieio-method-obarray . [BEFORE PRIMARY AFTER
1676 ;;                          genericBEFORE genericPRIMARY genericAFTER])
1677 ;;    where the association is a vector.
1678 ;;    (aref 0  -- all static methods.
1679 ;;    (aref 1  -- all methods classified as :BEFORE
1680 ;;    (aref 2  -- all methods classified as :PRIMARY
1681 ;;    (aref 3  -- all methods classified as :AFTER
1682 ;;    (aref 4  -- a generic classified as :BEFORE
1683 ;;    (aref 5  -- a generic classified as :PRIMARY
1684 ;;    (aref 6  -- a generic classified as :AFTER
1685 ;;
1686 (defvar eieiomt-optimizing-obarray nil
1687   "While mapping atoms, this contain the obarray being optimized.")
1688
1689 (defun eieiomt-install (method-name)
1690   "Install the method tree, and obarray onto METHOD-NAME.
1691 Do not do the work if they already exist."
1692   (let ((emtv (get method-name 'eieio-method-tree))
1693         (emto (get method-name 'eieio-method-obarray)))
1694     (if (or (not emtv) (not emto))
1695         (progn
1696           (setq emtv (put method-name 'eieio-method-tree
1697                           (make-vector method-num-fields nil))
1698                 emto (put method-name 'eieio-method-obarray
1699                           (make-vector method-num-fields nil)))
1700           (aset emto 0 (make-vector 11 0))
1701           (aset emto 1 (make-vector 11 0))
1702           (aset emto 2 (make-vector 41 0))
1703           (aset emto 3 (make-vector 11 0))
1704           ))))
1705
1706 (defun eieiomt-add (method-name method key class)
1707   "Add to METHOD-NAME the forms METHOD in a call position KEY for CLASS.
1708 METHOD-NAME is the name created by a call to `defgeneric'.
1709 METHOD are the forms for a given implementation.
1710 KEY is an integer (see comment in eieio.el near this function) which
1711 is associated with the :STATIC :BEFORE :PRIMARY and :AFTER tags.
1712 It also indicates if CLASS is defined or not.
1713 CLASS is the class this method is associated with."
1714   (if (or (> key method-num-fields) (< key 0))
1715       (error "Eieiomt-add: method key error!"))
1716   (let ((emtv (get method-name 'eieio-method-tree))
1717         (emto (get method-name 'eieio-method-obarray)))
1718     ;; Make sure the method tables are available.
1719     (if (or (not emtv) (not emto))
1720         (error "Programmer error: eieiomt-add"))
1721     ;; only add new cells on if it doesn't already exist!
1722     (if (assq class (aref emtv key))
1723         (setcdr (assq class (aref emtv key)) method)
1724       (aset emtv key (cons (cons class method) (aref emtv key))))
1725     ;; Add function definition into newly created symbol, and store
1726     ;; said symbol in the correct obarray, otherwise use the
1727     ;; other array to keep this stuff
1728     (if (< key method-num-lists)
1729         (let ((nsym (intern (symbol-name class) (aref emto key))))
1730           (fset nsym method)))
1731     ;; Now optimize the entire obarray
1732     (if (< key method-num-lists)
1733         (let ((eieiomt-optimizing-obarray (aref emto key)))
1734           (mapatoms 'eieiomt-sym-optimize eieiomt-optimizing-obarray)))
1735     ))
1736
1737 (defun eieiomt-next (class)
1738   "Return the next parent class for CLASS.
1739 If CLASS is a superclass, return variable `eieio-default-superclass'.  If CLASS
1740 is variable `eieio-default-superclass' then return nil.  This is different from
1741 function `class-parent' as class parent returns nil for superclasses.  This
1742 function performs no type checking!"
1743   ;; No type-checking because all calls are made from functions which
1744   ;; are safe and do checking for us.
1745   (or (class-parents-fast class)
1746       (if (eq class 'eieio-default-superclass)
1747           nil
1748         '(eieio-default-superclass))))
1749
1750 (defun eieiomt-sym-optimize (s)
1751   "Find the next class above S which has a function body for the optimizer."
1752   ;; (message "Optimizing %S" s)
1753   (let ((es (intern-soft (symbol-name s))) ;external symbol of class
1754         (ov nil)
1755         (cont t))
1756     ;; This converts ES from a single symbol to a list of parent classes.
1757     (setq es (eieiomt-next es))
1758     ;; Loop over ES, then it's children individually.
1759     ;; We can have multiple hits only at one level of the parent tree.
1760     (while (and es cont)
1761       (setq ov (intern-soft (symbol-name (car es)) eieiomt-optimizing-obarray))
1762       (if (fboundp ov)
1763           (progn
1764             (set s ov)                  ;store ov as our next symbol
1765             (setq cont nil))
1766         (setq es (append (cdr es) (eieiomt-next (car es))))))
1767     ;; If there is no nearest call, then set our value to nil
1768     (if (not es) (set s nil))
1769     ))
1770
1771 (defun eieio-generic-form (method key class)
1772  "Return the lambda form belonging to METHOD using KEY based upon CLASS.
1773 If CLASS is not a class then use `generic' instead.  If class has no
1774 form, but has a parent class, then trace to that parent class.  The
1775 first time a form is requested from a symbol, an optimized path is
1776 memoized for future faster use."
1777  (let ((emto (aref (get method 'eieio-method-obarray)
1778                    (if class key (+ key 3)))))
1779    (if (class-p class)
1780        ;; 1) find our symbol
1781        (let ((cs (intern-soft (symbol-name class) emto)))
1782          (if (not cs)
1783              ;; 2) If there isn't one, then make one.
1784              ;;    This can be slow since it only occurs once
1785              (progn
1786                (setq cs (intern (symbol-name class) emto))
1787                ;; 2.1) Cache it's nearest neighbor with a quick optimize
1788                ;;      which should only occur once for this call ever
1789                (let ((eieiomt-optimizing-obarray emto))
1790                  (eieiomt-sym-optimize cs))))
1791          ;; 3) If it's bound return this one.
1792          (if (fboundp  cs)
1793              (cons cs (aref (class-v class) class-symbol))
1794            ;; 4) If it's not bound then this variable knows something
1795            (if (symbol-value cs)
1796                (progn
1797                  ;; 4.1) This symbol holds the next class in it's value
1798                  (setq class (symbol-value cs)
1799                        cs (intern-soft (symbol-name class) emto))
1800                  ;; 4.2) The optimizer should always have chosen a
1801                  ;;      function-symbol
1802                  ;;(if (fboundp cs)
1803                  (cons cs (aref (class-v (intern (symbol-name class)))
1804                                 class-symbol))
1805                    ;;(error "EIEIO optimizer: erratic data loss!"))
1806                  )
1807                ;; There never will be a funcall...
1808                nil)))
1809      ;; for a generic call, what is a list, is the function body we want.
1810      (let ((emtl (aref (get method 'eieio-method-tree)
1811                        (if class key (+ key 3)))))
1812        (if emtl
1813            ;; The car of EMTL is supposed to be a class, which in this
1814            ;; case is nil, so skip it.
1815            (cons (cdr (car emtl)) nil)
1816          nil)))))
1817
1818 ;;;
1819 ;; Way to assign fields based on a list.  Used for constructors, or
1820 ;; even resetting an object at run-time
1821 ;;
1822 (defun eieio-set-defaults (obj &optional set-all)
1823   "Take object OBJ, and reset all fields to their defaults.
1824 If SET-ALL is non-nil, then when a default is nil, that value is
1825 reset.  If SET-ALL is nil, the fields are only reset if the default is
1826 not nil."
1827   (let ((scoped-class (aref obj object-class))
1828         (eieio-initializing-object t)
1829         (pub (aref (class-v (aref obj object-class)) class-public-a)))
1830     (while pub
1831       (let ((df (eieio-oref-default obj (car pub))))
1832         (if (and (listp df) (eq (car df) 'lambda-default))
1833             (progn
1834               (setq df (copy-sequence df))
1835               (setcar df 'lambda)))
1836         (if (or df set-all)
1837             (eieio-oset obj (car pub) df)))
1838       (setq pub (cdr pub)))))
1839
1840 (defun eieio-initarg-to-attribute (class initarg)
1841   "For CLASS, convert INITARG to the actual attribute name.
1842 If there is no translation, pass it in directly (so we can cheat if
1843 need be.. May remove that later...)"
1844   (let ((tuple (assoc initarg (aref (class-v class) class-initarg-tuples))))
1845     (if tuple
1846         (cdr tuple)
1847       nil)))
1848
1849 (defun eieio-attribute-to-initarg (class attribute)
1850   "In CLASS, convert the ATTRIBUTE into the corresponding init argument tag.
1851 This is usually a symbol that starts with `:'."
1852   (let ((tuple (rassoc attribute (aref (class-v class) class-initarg-tuples))))
1853     (if tuple
1854         (car tuple)
1855       nil)))
1856
1857 \f
1858 ;;; Here are some special types of errors
1859 ;;
1860 (intern "no-method-definition")
1861 (put 'no-method-definition 'error-conditions '(no-method-definition error))
1862 (put 'no-method-definition 'error-message "No method definition")
1863
1864 (intern "no-next-method")
1865 (put 'no-next-method 'error-conditions '(no-next-method error))
1866 (put 'no-next-method 'error-message "No next method")
1867
1868 (intern "invalid-slot-name")
1869 (put 'invalid-slot-name 'error-conditions '(invalid-slot-name error))
1870 (put 'invalid-slot-name 'error-message "Invalid slot name")
1871
1872 (intern "invalid-slot-type")
1873 (put 'invalid-slot-type 'error-conditions '(invalid-slot-type error nil))
1874 (put 'invalid-slot-type 'error-message "Invalid slot type")
1875
1876 (intern "unbound-slot")
1877 (put 'unbound-slot 'error-conditions '(unbound-slot error nil))
1878 (put 'unbound-slot 'error-message "Unbound slot")
1879
1880 ;;; Here are some CLOS items that need the CL package
1881 ;;
1882
1883 (defsetf slot-value (obj field) (store) (list 'eieio-oset obj field store))
1884 (defsetf eieio-oref (obj field) (store) (list 'eieio-oset obj field store))
1885
1886 ;; The below setf method was written by Arnd Kohrs <kohrs@acm.org>
1887 (define-setf-method oref (obj field) 
1888   (let ((obj-temp (gensym)) 
1889         (field-temp (gensym)) 
1890         (store-temp (gensym))) 
1891     (list (list obj-temp field-temp) 
1892           (list obj `(quote ,field)) 
1893           (list store-temp) 
1894           (list 'set-slot-value obj-temp field-temp
1895                 store-temp)
1896           (list 'slot-value obj-temp field-temp))))
1897
1898 \f
1899 ;;;
1900 ;; We want all objects created by EIEIO to have some default set of
1901 ;; behaviours so we can create object utilities, and allow various
1902 ;; types of error checking.  To do this, create the default EIEIO
1903 ;; class, and when no parent class is specified, use this as the
1904 ;; default.  (But don't store it in the other classes as the default,
1905 ;; allowing for transparent support.)
1906 ;;
1907
1908 (defclass eieio-default-superclass nil
1909   nil
1910   "Default class used as parent class for superclasses.
1911 Its fields are automatically adopted by such superclasses but not
1912 stored in the `parent' field.  When searching for attributes or
1913 methods, when the last parent is found, the search will recurse to
1914 this class."
1915   :abstract t)
1916
1917 (defalias 'standard-class 'eieio-default-superclass)
1918
1919 (defmethod constructor :STATIC
1920   ((class eieio-default-superclass) newname &rest fields)
1921   "Default constructor for CLASS `eieio-defualt-superclass'.
1922 NEWNAME is the name to be given to the constructed object.
1923 FIELDS are the initialization fields used by `shared-initialize'.
1924 This static method is called when an object is constructed.
1925 It allocates the vector used to represent an EIEIO object, and then
1926 calls `shared-initialize' on that object."
1927   (let* ((new-object (copy-sequence (aref (class-v class)
1928                                           class-default-object-cache))))
1929     ;; Update the name for the newly created object.
1930     (aset new-object object-name newname)
1931     ;; Call the initialize method on the new object with the fields
1932     ;; that were passed down to us.
1933     (initialize-instance new-object fields)
1934     ;; Return the created object.
1935     new-object))
1936
1937 (defmethod shared-initialize ((obj eieio-default-superclass) fields)
1938   "Set fields of OBJ with FIELDS which is a list of name/value pairs.
1939 Called from the constructor routine."
1940   (let ((scoped-class (aref obj object-class)))
1941     (while fields
1942       (let ((rn (eieio-initarg-to-attribute (object-class-fast obj)
1943                                             (car fields))))
1944         (if (not rn)
1945             (slot-missing obj (car fields) 'oset (car (cdr fields))))
1946         (eieio-oset obj rn (car (cdr fields))))
1947       (setq fields (cdr (cdr fields))))))
1948
1949 (defmethod initialize-instance ((this eieio-default-superclass)
1950                                 &optional fields)
1951     "Constructs the new object THIS based on FIELDS.
1952 FIELDS is a tagged list where odd numbered elements are tags, and
1953 even numbered elements are the values to store in the tagged slot.  If
1954 you overload the `initialize-instance', there you will need to call
1955 `shared-initialize' yourself, or you can call `call-next-method' to
1956 have this constructor called automatically.  If these steps are not
1957 taken, then new objects of your class will not have their values
1958 dynamically set from FIELDS."
1959     ;; First, see if any of our defaults are `lambda', and
1960     ;; re-evaluate them and apply the value to our slots.
1961     (let* ((scoped-class (class-v (aref this object-class)))
1962            (slot (aref scoped-class class-public-a))
1963            (defaults (aref scoped-class class-public-d)))
1964       (while slot
1965         (if (and (listp (car defaults))
1966                  (eq 'lambda (car (car defaults))))
1967             (eieio-oset this (car slot) (funcall (car defaults))))
1968         (setq slot (cdr slot)
1969               defaults (cdr defaults))))
1970     ;; Shared initialize will parse our fields for us.
1971     (shared-initialize this fields))
1972
1973 (defmethod slot-missing ((object eieio-default-superclass) slot-name
1974                          operation &optional new-value)
1975   "Slot missing is invoked when an attempt to access a slot in OBJECT fails.
1976 SLOT-NAME is the name of the failed slot, OPERATION is the type of access
1977 that was requested, and optional NEW-VALUE is the value that was desired
1978 to be set."
1979   (signal 'invalid-slot-name (list (object-name object)
1980                                    slot-name)))
1981
1982 (defmethod slot-unbound ((object eieio-default-superclass)
1983                          class slot-name fn)
1984   "Slot unbound is invoked during an attempt to reference an unbound slot.
1985 OBJECT is the instance of the object being reference.  CLASS is the
1986 class of OBJECT, and SLOT-NAME is the offending slot.  This function
1987 throws the signal `unbound-slot'.  You can overload this function and
1988 return the value to use in place of the unbound value.
1989 Argument FN is the function signaling this error.
1990 Use `slot-boundp' to determine if a slot is bound or not."
1991   (signal 'unbound-slot (list (class-name class) (object-name object)
1992                               slot-name fn)))
1993
1994 (defmethod no-applicable-method ((object eieio-default-superclass)
1995                                  method)
1996   "Called if there are no implementations for OBJECT in METHOD.
1997 OBJECT is the object which has no method implementation."
1998   (signal 'no-method-definition (list method (object-name object)))
1999   )
2000
2001 (defmethod no-next-method ((object eieio-default-superclass)
2002                            &rest args)
2003   "Called from `call-next-method' when no additional methods are available.
2004 OBJECT is othe object being called on `call-next-method'.
2005 ARGS are the  arguments it is called by.
2006 This method throws `no-next-method' by default.  Override this
2007 method to not throw an error, and it's return value becomes the
2008 return value of `call-next-method'."
2009   (signal 'no-next-method (list (object-name object) args))
2010 )
2011
2012 (defmethod clone ((obj eieio-default-superclass) &rest params)
2013   "Make a deep copy of OBJ, and then apply PARAMS.
2014 PARAMS is a parameter list of the same form as INITIALIZE-INSTANCE
2015 which are applied to change the object.  When overloading `clone', be
2016 sure to call `call-next-method' first and modify the returned object."
2017   (let ((nobj (copy-sequence obj))
2018         (nm (aref obj object-name))
2019         (passname (and params (stringp (car params))))
2020         (num 1))
2021     (if params (shared-initialize nobj (if passname (cdr params) params)))
2022     (if (not passname)
2023         (save-match-data
2024           (if (string-match "-\\([0-9]+\\)" nm)
2025               (setq num (1+ (string-to-number (match-string 1 nm)))
2026                     nm (substring nm 0 (match-beginning 0))))
2027           (aset nobj object-name (concat nm "-" (int-to-string num))))
2028       (aset nobj object-name (car params)))
2029     nobj))
2030
2031 (defmethod destructor ((this eieio-default-superclass) &rest params)
2032   "Destructor for cleaning up any dynamic links to our object.
2033 Argument THIS is the object being destroyed.  PARAMS are additional
2034 ignored parameters."
2035   ;; No cleanup... yet.
2036   )
2037
2038 (defmethod object-print ((this eieio-default-superclass) &rest strings)
2039   "Pretty printer for object THIS.  Call function `object-name' with STRINGS.
2040 The default method for printing object THIS is to use the
2041 function `object-name'.  At times it could be useful to put a summary
2042 of the object into the default #<notation> string.  Overload this
2043 function to allow summaries of your objects to be used by eieio
2044 browsing tools.  The optional parameter STRINGS is for additional
2045 summary parts to put into the name string.  When passing in extra
2046 strings from child classes, always remember to prepend a space."
2047   (object-name this (apply 'concat strings)))
2048
2049 (defvar eieio-print-depth 0
2050   "When printing, keep track of the current indentation depth.")
2051
2052 (defmethod object-write ((this eieio-default-superclass) &optional comment)
2053   "Write object THIS out to the current stream.
2054 This writes out the vector version of this object.  Complex and recursive
2055 object are discouraged from being written.
2056   If optional COMMENT is non-nil, include comments when outputting
2057 this object."
2058   (when comment
2059     (princ ";; Object ")
2060     (princ (object-name-string this))
2061     (princ "\n")
2062     (princ comment)
2063     (princ "\n"))
2064   (let* ((cl (object-class this))
2065          (cv (class-v cl)))
2066     ;; Now output readable lisp to recreate this object
2067     ;; It should look like this:
2068     ;; (<constructor> <name> <slot> <field> ... )
2069     ;; Each slot's field is writen using its :writer.
2070     (princ (make-string (* eieio-print-depth 2) ? ))
2071     (princ "(")
2072     (princ (symbol-name (class-constructor (object-class this))))
2073     (princ " \"")
2074     (princ (object-name-string this))
2075     (princ "\"\n")
2076     ;; Loop over all the public slots
2077     (let ((publa (aref cv class-public-a))
2078           (publd (aref cv class-public-d))
2079           (eieio-print-depth (1+ eieio-print-depth)))
2080       (while publa
2081         (when (slot-boundp this (car publa))
2082           (let ((i (class-slot-initarg cl (car publa)))
2083                 (v (eieio-oref this (car publa))))
2084             (unless (or (not i) (equal v (car publd)))
2085               (princ (make-string (* eieio-print-depth 2) ? ))
2086               (princ (symbol-name i))
2087               (princ " ")
2088               (eieio-override-prin1 v)
2089               (princ "\n"))))
2090         (setq publa (cdr publa) publd (cdr publd)))
2091       (princ (make-string (* eieio-print-depth 2) ? )))
2092     (princ ")\n")))
2093
2094 (defun eieio-override-prin1 (thing)
2095   "Perform a prin1 on THING taking advantage of object knowledge."
2096   (cond ((object-p thing)
2097          (object-write thing))
2098         ((listp thing)
2099          (eieio-list-prin1 thing))
2100         ((class-p thing)
2101          (princ (class-name thing)))
2102         ((symbolp thing)
2103          (princ (concat "'" (symbol-name thing))))
2104         (t (prin1 thing))))
2105
2106 (defun eieio-list-prin1 (list)
2107   "Display LIST where list may contain objects."
2108   (if (not (object-p (car list)))
2109       (progn
2110         (princ "'")
2111         (prin1 list))
2112     (princ "(list ")
2113     (if (object-p (car list)) (princ "\n "))
2114     (while list
2115       (if (object-p (car list))
2116           (object-write (car list))
2117         (princ "'")
2118         (prin1 (car list)))
2119       (princ " ")
2120       (setq list (cdr list)))
2121     (princ (make-string (* eieio-print-depth 2) ? ))
2122     (princ ")")))
2123
2124 \f
2125 ;;; Unimplemented functions from CLOS
2126 ;;
2127 (defun change-class (obj class)
2128   "Change the class of OBJ to type CLASS.
2129 This may create or delete slots, but does not affect the return value
2130 of `eq'."
2131   (error "Eieio: `change-class' is unimplemented"))
2132
2133 )
2134
2135 \f
2136 ;;; Interfacing with edebug
2137 ;;
2138 (defun eieio-edebug-prin1-to-string (object &optional noescape)
2139   "Display eieio OBJECT in fancy format.  Overrides the edebug default.
2140 Optional argument NOESCAPE is passed to `prin1-to-string' when appropriate."
2141   (cond ((class-p object) (class-name object))
2142         ((object-p object) (object-print object))
2143         ((and (listp object) (or (class-p (car object))
2144                                  (object-p (car object))))
2145          (concat "(" (mapconcat 'eieio-edebug-prin1-to-string object " ") ")"))
2146         (t (prin1-to-string object noescape))))
2147
2148 (add-hook 'edebug-setup-hook
2149           (lambda ()
2150             (def-edebug-spec defmethod
2151               (&define                  ; this means we are defining something
2152                [&or name ("setf" :name setf name)]
2153                ;; ^^ This is the methods symbol
2154                [ &optional symbolp ]    ; this is key :BEFORE etc
2155                list              ; arguments
2156                [ &optional stringp ]    ; documentation string
2157                def-body                 ; part to be debugged
2158                ))
2159             ;; The rest of the macros
2160             (def-edebug-spec oref (form quote))
2161             (def-edebug-spec oref-default (form quote))
2162             (def-edebug-spec oset (form quote form))
2163             (def-edebug-spec oset-default (form quote form))
2164             (def-edebug-spec class-v form)
2165             (def-edebug-spec class-p form)
2166             (def-edebug-spec object-p form)
2167             (def-edebug-spec class-constructor form)
2168             (def-edebug-spec generic-p form)
2169             (def-edebug-spec with-slots (list list def-body))
2170             ;; I suspect this isn't the best way to do this, but when
2171             ;; cust-print was used on my system all my objects
2172             ;; appeared as "#1 =" which was not useful.  This allows
2173             ;; edebug to print my objects in the nice way they were
2174             ;; meant to with `object-print' and `class-name'
2175             ;; (defalias 'edebug-prin1-to-string 'eieio-edebug-prin1-to-string)
2176             )
2177           )
2178
2179 (eval-after-load "cedet-edebug"
2180   '(progn
2181      (cedet-edebug-add-print-override '(class-p object) '(class-name object) )
2182      (cedet-edebug-add-print-override '(object-p object) '(object-print object) )
2183      (cedet-edebug-add-print-override '(and (listp object)
2184                                             (or (class-p (car object)) (object-p (car object))))
2185                                       '(cedet-edebug-prin1-recurse object) )
2186      ))
2187
2188 ;;; Interfacing with imenu in emacs lisp mode
2189 ;;    (Only if the expression is defined)
2190 ;;
2191 (if (eval-when-compile (boundp 'list-imenu-generic-expression))
2192 (progn
2193
2194 (defun eieio-update-lisp-imenu-expression ()
2195   "Examine `lisp-imenu-generic-expression' and modify it to find `defmethod'."
2196   (let ((exp lisp-imenu-generic-expression))
2197     (while exp
2198       ;; it's of the form '( ( title expr indx ) ... )
2199       (let* ((subcar (cdr (car exp)))
2200              (substr (car subcar)))
2201         (if (and (not (string-match "|method\\\\" substr))
2202                  (string-match "|advice\\\\" substr))
2203             (setcar subcar
2204                     (replace-match "|advice\\|method\\" t t substr 0))))
2205       (setq exp (cdr exp)))))
2206
2207 (eieio-update-lisp-imenu-expression)
2208
2209 ))
2210
2211 ;;; Autoloading some external symbols, and hooking into the help system
2212 ;;
2213
2214 (autoload 'eieio-help-mode-augmentation-maybee "eieio-opt" "For buffers thrown into help mode, augment for eieio.")
2215 (autoload 'eieio-browse "eieio-opt" "Create an object browser window" t)
2216 (autoload 'eieio-describe-class "eieio-opt" "Describe CLASS defined by a string or symbol" t)
2217 (autoload 'describe-class "eieio-opt" "Describe CLASS defined by a string or symbol" t)
2218 (autoload 'eieio-describe-generic "eieio-opt" "Describe GENERIC defined by a string or symbol" t)
2219 (autoload 'describe-generic "eieio-opt" "Describe GENERIC defined by a string or symbol" t)
2220 (autoload 'eieiodoc-class "eieio-doc" "Create texinfo documentation about a class hierarchy." t)
2221
2222 (autoload 'customize-object "eieio-custom" "Create a custom buffer editing OBJ.")
2223
2224 ;; make sure this shows up after the help mode hook.
2225 (add-hook 'temp-buffer-show-hook 'eieio-help-mode-augmentation-maybee t)
2226 (require 'advice)
2227 (defadvice describe-variable (around eieio-describe activate)
2228   "Display the full documentation of FUNCTION (a symbol).
2229 Returns the documentation as a string, also."
2230   (if (class-p (ad-get-arg 0))
2231       (eieio-describe-class (ad-get-arg 0))
2232     ad-do-it))
2233
2234 (defadvice describe-function (around eieio-describe activate)
2235   "Display the full documentation of VARIABLE (a symbol).
2236 Returns the documentation as a string, also."
2237   (if (generic-p (ad-get-arg 0))
2238       (eieio-describe-generic (ad-get-arg 0))
2239     ad-do-it))
2240
2241 (provide 'eieio)
2242 ;;; eieio ends here