1 ;;; eieio.el --- Enhanced Implementation of Emacs Interpreted Objects
2 ;;; or maybe Eric's Implementation of Emacs Intrepreted Objects
4 ;;; Copyright (C) 1995, 1996, 1998, 1999, 2000, 2001, 2002, 2003,
5 ;;; 2004, 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
7 ;; Author: Eric M. Ludlam <zappo@gnu.org>
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs 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 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs 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.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
28 ;; EIEIO is a series of Lisp routines which implements a subset of
29 ;; CLOS, the Common Lisp Object System. In addition, EIEIO also adds
30 ;; a few new features which help it integrate more strongly with the
31 ;; Emacs running environment.
33 ;; See eieio.texi for complete documentation on using this package.
35 ;; There is funny stuff going on with typep and deftype. This
36 ;; is the only way I seem to be able to make this stuff load properly.
38 ;; @TODO - fix :initform to be a form, not a quoted value
39 ;; @TODO - Prefix non-clos functions with `eieio-'.
43 (defvar eieio-version "1.2"
44 "Current version of EIEIO.")
48 (defun eieio-version ()
49 "Display the current version of EIEIO."
51 (message eieio-version))
54 ;; About the above. EIEIO must process it's own code when it compiles
55 ;; itself, thus, by eval-and-compiling outselves, we solve the problem.
58 (if (fboundp 'compiled-function-arglist)
60 ;; XEmacs can only access a compiled functions arglist like this:
61 (defalias 'eieio-compiled-function-arglist 'compiled-function-arglist)
63 ;; Emacs doesn't have this function, but since FUNC is a vector, we can just
64 ;; grab the appropriate element.
65 (defun eieio-compiled-function-arglist (func)
66 "Return the argument list for the compiled function FUNC."
73 ;; Variable declarations.
76 (defvar eieio-hook nil
77 "*This hook is executed, then cleared each time `defclass' is called.")
79 (defvar eieio-error-unsupported-class-tags nil
80 "*Non nil to throw an error if an encountered tag us unsupported.
81 This may prevent classes from CLOS applications from being used with EIEIO
82 since EIEIO does not support all CLOS tags.")
84 (defvar eieio-skip-typecheck nil
85 "*If non-nil, skip all slot typechecking.
86 Set this to t permanently if a program is functioning well to get a
87 small speed increase. This variable is also used internally to handle
88 default setting for optimization purposes.")
90 (defvar eieio-optimize-primary-methods-flag t
91 "Non-nil means to optimize the method dispatch on primary methods.")
95 "Inside a method, this variable is the object in question.
96 DO NOT SET THIS YOURSELF unless you are trying to simulate friendly slots.
98 Note: Embedded methods are no longer supported. The variable THIS is
99 still set for CLOS methods for the sake of routines like
102 (defvar scoped-class nil
103 "This is set to a class when a method is running.
104 This is so we know we are allowed to check private parts or how to
105 execute a `call-next-method'. DO NOT SET THIS YOURSELF!")
107 (defvar eieio-initializing-object nil
108 "Set to non-nil while initializing an object.")
110 (defconst eieio-unbound
111 (if (and (boundp 'eieio-unbound) (symbolp eieio-unbound))
113 (make-symbol "unbound"))
114 "Uninterned symbol representing an unbound slot in an object.")
116 ;; This is a bootstrap for eieio-default-superclass so it has a value
117 ;; while it is being built itself.
118 (defvar eieio-default-superclass nil)
120 (defconst class-symbol 1 "Class's symbol (self-referencing.).")
121 (defconst class-parent 2 "Class parent slot.")
122 (defconst class-children 3 "Class children class slot.")
123 (defconst class-symbol-obarray 4 "Obarray permitting fast access to variable position indexes.")
125 ;; the word "public" here is leftovers from the very first version.
127 (defconst class-public-a 5 "Class attribute index.")
128 (defconst class-public-d 6 "Class attribute defaults index.")
129 (defconst class-public-doc 7 "Class documentation strings for attributes.")
130 (defconst class-public-type 8 "Class type for a slot.")
131 (defconst class-public-custom 9 "Class custom type for a slot.")
132 (defconst class-public-custom-label 10 "Class custom group for a slot.")
133 (defconst class-public-custom-group 11 "Class custom group for a slot.")
134 (defconst class-public-printer 12 "Printer for a slot.")
135 (defconst class-protection 13 "Class protection for a slot.")
136 (defconst class-initarg-tuples 14 "Class initarg tuples list.")
137 (defconst class-class-allocation-a 15 "Class allocated attributes.")
138 (defconst class-class-allocation-doc 16 "Class allocated documentation.")
139 (defconst class-class-allocation-type 17 "Class allocated value type.")
140 (defconst class-class-allocation-custom 18 "Class allocated custom descriptor.")
141 (defconst class-class-allocation-custom-label 19 "Class allocated custom descriptor.")
142 (defconst class-class-allocation-custom-group 20 "Class allocated custom group.")
143 (defconst class-class-allocation-printer 21 "Class allocated printer for a slot.")
144 (defconst class-class-allocation-protection 22 "Class allocated protection list.")
145 (defconst class-class-allocation-values 23 "Class allocated value vector.")
146 (defconst class-default-object-cache 24
147 "Cache index of what a newly created object would look like.
148 This will speed up instantiation time as only a `copy-sequence' will
149 be needed, instead of looping over all the values and setting them
151 (defconst class-options 25
152 "Storage location of tagged class options.
153 Stored outright without modifications or stripping.")
155 (defconst class-num-slots 26
156 "Number of slots in the class definition object.")
158 (defconst object-class 1 "Index in an object vector where the class is stored.")
159 (defconst object-name 2 "Index in an object where the name is stored.")
161 (defconst method-static 0 "Index into :static tag on a method.")
162 (defconst method-before 1 "Index into :before tag on a method.")
163 (defconst method-primary 2 "Index into :primary tag on a method.")
164 (defconst method-after 3 "Index into :after tag on a method.")
165 (defconst method-num-lists 4 "Number of indexes into methods vector in which groups of functions are kept.")
166 (defconst method-generic-before 4 "Index into generic :before tag on a method.")
167 (defconst method-generic-primary 5 "Index into generic :primary tag on a method.")
168 (defconst method-generic-after 6 "Index into generic :after tag on a method.")
169 (defconst method-num-slots 7 "Number of indexes into a method's vector.")
171 ;; How to specialty compile stuff.
172 (autoload 'byte-compile-file-form-defmethod "eieio-comp"
173 "This function is used to byte compile methods in a nice way.")
174 (put 'defmethod 'byte-hunk-handler 'byte-compile-file-form-defmethod)
176 (eval-when-compile (require 'eieio-comp))
179 ;;; Important macros used in eieio.
181 (defmacro class-v (class)
182 "Internal: Return the class vector from the CLASS symbol."
183 ;; No check: If eieio gets this far, it's probably been checked already.
184 `(get ,class 'eieio-class-definition))
186 (defmacro class-p (class)
187 "Return t if CLASS is a valid class vector.
189 ;; this new method is faster since it doesn't waste time checking lots of
192 (eq (aref (class-v ,class) 0) 'defclass)
196 (defmacro eieio-object-p (obj)
197 "Return non-nil if OBJ is an EIEIO object."
200 (and (eq (aref tobj 0) 'object)
201 (class-p (aref tobj object-class))))
203 (defalias 'object-p 'eieio-object-p)
205 (defmacro class-constructor (class)
206 "Return the symbol representing the constructor of CLASS."
207 `(aref (class-v ,class) class-symbol))
209 (defmacro generic-p (method)
210 "Return t if symbol METHOD is a generic function.
211 Only methods have the symbol `eieio-method-obarray' as a property (which
212 contains a list of all bindings to that method type.)"
213 `(and (fboundp ,method) (get ,method 'eieio-method-obarray)))
215 (defun generic-primary-only-p (method)
216 "Return t if symbol METHOD is a generic function with only primary methods.
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 Methods with only primary implementations are executed in an optimized way."
220 (and (generic-p method)
221 (let ((M (get method 'eieio-method-tree)))
222 (and (< 0 (length (aref M method-primary)))
223 (not (aref M method-static))
224 (not (aref M method-before))
225 (not (aref M method-after))
226 (not (aref M method-generic-before))
227 (not (aref M method-generic-primary))
228 (not (aref M method-generic-after))))
231 (defun generic-primary-only-one-p (method)
232 "Return t if symbol METHOD is a generic function with only primary methods.
233 Only methods have the symbol `eieio-method-obarray' as a property (which
234 contains a list of all bindings to that method type.)
235 Methods with only primary implementations are executed in an optimized way."
236 (and (generic-p method)
237 (let ((M (get method 'eieio-method-tree)))
238 (and (= 1 (length (aref M method-primary)))
239 (not (aref M method-static))
240 (not (aref M method-before))
241 (not (aref M method-after))
242 (not (aref M method-generic-before))
243 (not (aref M method-generic-primary))
244 (not (aref M method-generic-after))))
247 (defmacro class-option-assoc (list option)
248 "Return from LIST the found OPTION. Nil if it doesn't exist."
249 `(car-safe (cdr (memq ,option ,list))))
251 (defmacro class-option (class option)
252 "Return the value stored for CLASS' OPTION.
253 Return nil if that option doesn't exist."
254 `(class-option-assoc (aref (class-v ,class) class-options) ',option))
256 (defmacro class-abstract-p (class)
257 "Return non-nil if CLASS is abstract.
258 Abstract classes cannot be instantiated."
259 `(class-option ,class :abstract))
261 (defmacro class-method-invocation-order (class)
262 "Return the invocation order of CLASS.
263 Abstract classes cannot be instantiated."
264 `(or (class-option ,class :method-invocation-order)
268 ;;; Defining a new class
270 (defmacro defclass (name superclass slots &rest options-and-doc)
271 "Define NAME as a new class derived from SUPERCLASS with SLOTS.
272 OPTIONS-AND-DOC is used as the class' options and base documentation.
273 SUPERCLASS is a list of superclasses to inherit from, with SLOTS
274 being the slots residing in that class definition. NOTE: Currently
275 only one slot may exist in SUPERCLASS as multiple inheritance is not
276 yet supported. Supported tags are:
278 :initform - initializing form
279 :initarg - tag used during initialization
280 :accessor - tag used to create a function to access this slot
281 :allocation - specify where the value is stored.
282 defaults to `:instance', but could also be `:class'
283 :writer - a function symbol which will `write' an object's slot
284 :reader - a function symbol which will `read' an object
285 :type - the type of data allowed in this slot (see `typep')
287 - A string documenting use of this slot.
289 The following are extensions on CLOS:
290 :protection - Specify protection for this slot.
291 Defaults to `:public'. Also use `:protected', or `:private'
292 :custom - When customizing an object, the custom :type. Public only.
293 :label - A text string label used for a slot when customizing.
294 :group - Name of a customization group this slot belongs in.
295 :printer - A function to call to print the value of a slot.
296 See `eieio-override-prin1' as an example.
298 A class can also have optional options. These options happen in place
299 of documentation, (including a :documentation tag) in addition to
300 documentation, or not at all. Supported options are:
302 :documentation - The doc-string used for this class.
304 Options added to EIEIO:
306 :allow-nil-initform - Non-nil to skip typechecking of initforms if nil.
307 :custom-groups - List of custom group names. Organizes slots into
308 reasonable groups for customizations.
309 :abstract - Non-nil to prevent instances of this class.
310 If a string, use as an error string if someone does
311 try to make an instance.
312 :method-invocation-order
313 - Control the method invokation order if there is
314 multiple inheritance. Valid values are:
315 :breadth-first - The default.
318 Options in CLOS not supported in EIEIO:
320 :metaclass - Class to use in place of `standard-class'
321 :default-initargs - Initargs to use when initializing new objects of
324 Due to the way class options are set up, you can add any tags in you
325 wish, and reference them using the function `class-option'."
326 ;; We must `eval-and-compile' this so that when we byte compile
327 ;; an eieio program, there is no need to load it ahead of time.
328 ;; It also provides lots of nice debugging errors at compile time.
330 (eieio-defclass ',name ',superclass ',slots ',options-and-doc)))
332 (defvar eieio-defclass-autoload-map (make-vector 7 nil)
333 "Symbol map of superclasses we find in autoloads.")
336 (defun eieio-defclass-autoload (cname superclasses filename doc)
337 "Create autoload symbols for the EIEIO class CNAME.
338 SUPERCLASSES are the superclasses that CNAME inherites from.
339 DOC is the docstring for CNAME.
340 This function creates a mock-class for CNAME and adds it into
341 SUPERCLASSES as children.
342 It creates an autoload function for CNAME's constructor."
343 ;; Assume we've already debugged inputs.
345 (let* ((oldc (when (class-p cname) (class-v cname)))
346 (newc (make-vector class-num-slots nil))
349 nil ;; Do nothing if we already have this class.
351 ;; Create the class in NEWC, but don't fill anything else in.
352 (aset newc 0 'defclass)
353 (aset newc class-symbol cname)
355 (let ((clear-parent nil))
357 (when (not superclasses)
358 (setq superclasses '(eieio-default-superclass)
362 ;; Hook our new class into the existing structures so we can
363 ;; autoload it later.
364 (dolist (SC superclasses)
367 ;; TODO - If we create an autoload that is in the map, that
368 ;; map needs to be cleared!
371 ;; Does our parent exist?
372 (if (not (class-p SC))
374 ;; Create a symbol for this parent, and then store this
375 ;; parent on that symbol.
376 (let ((sym (intern (symbol-name SC) eieio-defclass-autoload-map)))
377 (if (not (boundp sym))
378 (set sym (list cname))
379 (add-to-list sym cname))
382 ;; We have a parent, save the child in there.
383 (when (not (member cname (aref (class-v SC) class-children)))
384 (aset (class-v SC) class-children
385 (cons cname (aref (class-v SC) class-children)))))
387 ;; save parent in child
388 (aset newc class-parent (cons SC (aref newc class-parent)))
391 ;; turn this into a useable self-pointing symbol
394 ;; Store the new class vector definition into the symbol. We need to
395 ;; do this first so that we can call defmethod for the accessor.
396 ;; The vector will be updated by the following while loop and will not
397 ;; need to be stored a second time.
398 (put cname 'eieio-class-definition newc)
401 (if clear-parent (aset newc class-parent nil))
403 ;; Create an autoload on top of our constructor function.
404 (autoload cname filename doc nil nil)
405 (autoload (intern (concat (symbol-name cname) "-p")) filename "" nil nil)
406 (autoload (intern (concat (symbol-name cname) "-child-p")) filename "" nil nil)
410 (defsubst eieio-class-un-autoload (cname)
411 "If class CNAME is in an autoload state, load it's file."
412 (when (eq (car-safe (symbol-function cname)) 'autoload)
413 (load-library (car (cdr (symbol-function cname))))))
415 (defun eieio-defclass (cname superclasses slots options-and-doc)
416 "See `defclass' for more information.
417 Define CNAME as a new subclass of SUPERCLASSES, with SLOTS being the
418 slots residing in that class definition, and with options or documentation
419 OPTIONS-AND-DOC as the toplevel documentation for this class."
420 ;; Run our eieio-hook each time, and clear it when we are done.
421 ;; This way people can add hooks safely if they want to modify eieio
422 ;; or add definitions when eieio is loaded or something like that.
423 (run-hooks 'eieio-hook)
424 (setq eieio-hook nil)
426 (if (not (symbolp cname)) (signal 'wrong-type-argument '(symbolp cname)))
427 (if (not (listp superclasses)) (signal 'wrong-type-argument '(listp superclasses)))
429 (let* ((pname (if superclasses superclasses nil))
430 (newc (make-vector class-num-slots nil))
431 (oldc (when (class-p cname) (class-v cname)))
432 (groups nil) ;; list of groups id'd from slots
436 (aset newc 0 'defclass)
437 (aset newc class-symbol cname)
439 ;; If this class already existed, and we are updating it's structure,
440 ;; make sure we keep the old child list. This can cause bugs, but
441 ;; if no new slots are created, it also saves time, and prevents
442 ;; method table breakage, particularly when the users is only
443 ;; byte compiling an EIEIO file.
445 (aset newc class-children (aref oldc class-children))
446 ;; If the old class did not exist, but did exist in the autoload map, then adopt those children.
447 ;; This is like the above, but deals with autoloads nicely.
448 (let ((sym (intern-soft (symbol-name cname) eieio-defclass-autoload-map)))
451 (aset newc class-children (symbol-value sym))
453 (unintern (symbol-name cname) eieio-defclass-autoload-map)
457 (cond ((and (stringp (car options-and-doc))
458 (/= 1 (% (length options-and-doc) 2)))
459 (error "Too many arguments to `defclass'"))
460 ((and (symbolp (car options-and-doc))
461 (/= 0 (% (length options-and-doc) 2)))
462 (error "Too many arguments to `defclass'"))
466 (if (stringp (car options-and-doc))
467 (cons :documentation options-and-doc)
473 (if (and (car pname) (symbolp (car pname)))
474 (if (not (class-p (car pname)))
476 (error "Given parent class %s is not a class" (car pname))
477 ;; good parent class...
478 ;; save new child in parent
479 (when (not (member cname (aref (class-v (car pname)) class-children)))
480 (aset (class-v (car pname)) class-children
481 (cons cname (aref (class-v (car pname)) class-children))))
482 ;; Get custom groups, and store them into our local copy.
483 (mapc (lambda (g) (add-to-list 'groups g))
484 (class-option (car pname) :custom-groups))
485 ;; save parent in child
486 (aset newc class-parent (cons (car pname) (aref newc class-parent))))
487 (error "Invalid parent class %s" pname))
488 (setq pname (cdr pname)))
489 ;; Reverse the list of our parents so that they are prioritized in
490 ;; the same order as specified in the code.
491 (aset newc class-parent (nreverse (aref newc class-parent))) )
492 ;; If there is nothing to loop over, then inherit from the
493 ;; default superclass.
494 (unless (eq cname 'eieio-default-superclass)
495 ;; adopt the default parent here, but clear it later...
497 ;; save new child in parent
498 (if (not (member cname (aref (class-v 'eieio-default-superclass) class-children)))
499 (aset (class-v 'eieio-default-superclass) class-children
500 (cons cname (aref (class-v 'eieio-default-superclass) class-children))))
501 ;; save parent in child
502 (aset newc class-parent (list eieio-default-superclass))))
504 ;; turn this into a useable self-pointing symbol
507 ;; These two tests must be created right away so we can have self-
508 ;; referencing classes. ei, a class whose slot can contain only
509 ;; pointers to itself.
511 ;; Create the test function
512 (let ((csym (intern (concat (symbol-name cname) "-p"))))
514 (list 'lambda (list 'obj)
515 (format "Test OBJ to see if it an object of type %s" cname)
516 (list 'and '(eieio-object-p obj)
517 (list 'same-class-p 'obj cname)))))
519 ;; Make sure the method invocation order is a valid value.
520 (let ((io (class-option-assoc options :method-invocation-order)))
521 (when (and io (not (member io '(:depth-first :breadth-first))))
522 (error "Method invocation order %s is not allowed" io)
525 ;; Create a handy child test too
526 (let ((csym (intern (concat (symbol-name cname) "-child-p"))))
530 "Test OBJ to see if it an object is a child of type %s"
532 (and (eieio-object-p obj)
533 (object-of-class-p obj ,cname))))
535 ;; When using typep, (typep OBJ 'myclass) returns t for objects which
536 ;; are subclasses of myclass. For our predicates, however, it is
537 ;; important for EIEIO to be backwards compatible, where
538 ;; myobject-p, and myobject-child-p are different.
539 ;; "cl" uses this technique to specify symbols with specific typep
540 ;; test, so we can let typep have the CLOS documented behavior
541 ;; while keeping our above predicate clean.
542 (eval `(deftype ,cname ()
544 ,(intern (concat (symbol-name cname) "-child-p")))))
548 ;; before adding new slots, lets add all the methods and classes
549 ;; in from the parent class
550 (eieio-copy-parents-into-subclass newc superclasses)
552 ;; Store the new class vector definition into the symbol. We need to
553 ;; do this first so that we can call defmethod for the accessor.
554 ;; The vector will be updated by the following while loop and will not
555 ;; need to be stored a second time.
556 (put cname 'eieio-class-definition newc)
558 ;; Query each slot in the declaration list and mangle into the
559 ;; class structure I have defined.
561 (let* ((slot1 (car slots))
564 (acces (plist-get slot ':accessor))
565 (init (or (plist-get slot ':initform)
566 (if (member ':initform slot) nil
568 (initarg (plist-get slot ':initarg))
569 (docstr (plist-get slot ':documentation))
570 (prot (plist-get slot ':protection))
571 (reader (plist-get slot ':reader))
572 (writer (plist-get slot ':writer))
573 (alloc (plist-get slot ':allocation))
574 (type (plist-get slot ':type))
575 (custom (plist-get slot ':custom))
576 (label (plist-get slot ':label))
577 (customg (plist-get slot ':group))
578 (printer (plist-get slot ':printer))
580 (skip-nil (class-option-assoc options :allow-nil-initform))
583 (if eieio-error-unsupported-class-tags
586 (if (not (member (car tmp) '(:accessor
601 (signal 'invalid-slot-type (list (car tmp))))
602 (setq tmp (cdr (cdr tmp))))))
604 ;; Clean up the meaning of protection.
605 (cond ((or (eq prot 'public) (eq prot :public)) (setq prot nil))
606 ((or (eq prot 'protected) (eq prot :protected)) (setq prot 'protected))
607 ((or (eq prot 'private) (eq prot :private)) (setq prot 'private))
609 (t (signal 'invalid-slot-type (list ':protection prot))))
611 ;; Make sure the :allocation parameter has a valid value.
612 (if (not (or (not alloc) (eq alloc :class) (eq alloc :instance)))
613 (signal 'invalid-slot-type (list ':allocation alloc)))
615 ;; The default type specifier is supposed to be t, meaning anything.
616 (if (not type) (setq type t))
618 ;; Label is nil, or a string
619 (if (not (or (null label) (stringp label)))
620 (signal 'invalid-slot-type (list ':label label)))
622 ;; Is there an initarg, but allocation of class?
623 (if (and initarg (eq alloc :class))
624 (message "Class allocated slots do not need :initarg"))
626 ;; intern the symbol so we can use it blankly
627 (if initarg (set initarg initarg))
629 ;; The customgroup should be a list of symbols
630 (cond ((null customg)
631 (setq customg '(default)))
632 ((not (listp customg))
633 (setq customg (list customg))))
634 ;; The customgroup better be a symbol, or list of symbols.
636 (if (not (symbolp cg))
637 (signal 'invalid-slot-type (list ':group cg))))
640 ;; First up, add this slot into our new class.
641 (eieio-add-new-slot newc name init docstr type custom label customg printer
642 prot initarg alloc 'defaultoverride skip-nil)
644 ;; We need to id the group, and store them in a group list attribute.
645 (mapc (lambda (cg) (add-to-list 'groups cg)) customg)
647 ;; anyone can have an accessor function. This creates a function
648 ;; of the specified name, and also performs a `defsetf' if applicable
649 ;; so that users can `setf' the space returned by this function
652 (eieio-defmethod acces
653 (list (if (eq alloc :class) :static :primary)
654 (list (list 'this cname))
656 "Retrieves the slot `%s' from an object of class `%s'"
658 (list 'if (list 'slot-boundp 'this (list 'quote name))
659 (list 'eieio-oref 'this (list 'quote name))
660 ;; Else - Some error? nil?
663 ;; Thanks Pascal Bourguignon <pjb@informatimago.com>
664 ;; For this complex macro.
666 (list 'defsetf acces '(widget) '(store)
667 (list 'list ''eieio-oset 'widget
668 (list 'quote (list 'quote name)) 'store))))
669 ;;`(defsetf ,acces (widget) (store) (eieio-oset widget ',cname store))
672 ;; If a writer is defined, then create a generic method of that
673 ;; name whose purpose is to set the value of the slot.
676 (eieio-defmethod writer
677 (list (list (list 'this cname) 'value)
678 (format "Set the slot `%s' of an object of class `%s'"
680 `(setf (slot-value this ',name) value)))
682 ;; If a reader is defined, then create a generic method
683 ;; of that name whose purpose is to access this slot value.
686 (eieio-defmethod reader
687 (list (list (list 'this cname))
688 (format "Access the slot `%s' from object of class `%s'"
690 `(slot-value this ',name)))))
692 (setq slots (cdr slots)))
694 ;; Now that everything has been loaded up, all our lists are backwards! Fix that up now.
695 (aset newc class-public-a (nreverse (aref newc class-public-a)))
696 (aset newc class-public-d (nreverse (aref newc class-public-d)))
697 (aset newc class-public-doc (nreverse (aref newc class-public-doc)))
698 (aset newc class-public-type
699 (apply 'vector (nreverse (aref newc class-public-type))))
700 (aset newc class-public-custom (nreverse (aref newc class-public-custom)))
701 (aset newc class-public-custom-label (nreverse (aref newc class-public-custom-label)))
702 (aset newc class-public-custom-group (nreverse (aref newc class-public-custom-group)))
703 (aset newc class-public-printer (nreverse (aref newc class-public-printer)))
704 (aset newc class-protection (nreverse (aref newc class-protection)))
705 (aset newc class-initarg-tuples (nreverse (aref newc class-initarg-tuples)))
707 ;; The storage for class-class-allocation-type needs to be turned into
709 (aset newc class-class-allocation-type
710 (apply 'vector (aref newc class-class-allocation-type)))
712 ;; Also, take class allocated values, and vectorize them for speed.
713 (aset newc class-class-allocation-values
714 (apply 'vector (aref newc class-class-allocation-values)))
716 ;; Attach slot symbols into an obarray, and store the index of
717 ;; this slot as the variable slot in this new symbol. We need to
718 ;; know about primes, because obarrays are best set in vectors of
719 ;; prime number length, and we also need to make our vector small
720 ;; to save space, and also optimal for the number of items we have.
722 (pubsyms (aref newc class-public-a))
723 (prots (aref newc class-protection))
725 (vl (let ((primes '( 3 5 7 11 13 17 19 23 29 31 37 41 43 47
726 53 59 61 67 71 73 79 83 89 97 101 )))
727 (while (and primes (< (car primes) l))
728 (setq primes (cdr primes)))
730 (oa (make-vector vl 0))
733 (setq newsym (intern (symbol-name (car pubsyms)) oa))
736 (if (car prots) (put newsym 'protection (car prots)))
737 (setq pubsyms (cdr pubsyms)
739 (aset newc class-symbol-obarray oa)
742 ;; Create the constructor function
743 (if (class-option-assoc options :abstract)
744 ;; Abstract classes cannot be instantiated. Say so.
745 (let ((abs (class-option-assoc options :abstract)))
746 (if (not (stringp abs))
747 (setq abs (format "Class %s is abstract" cname)))
749 `(lambda (&rest stuff)
750 ,(format "You cannot create a new object of type %s" cname)
753 ;; Non-abstract classes need a constructor.
755 `(lambda (newname &rest slots)
756 ,(format "Create a new object with name NAME of class type %s" cname)
757 (apply 'constructor ,cname newname slots)))
760 ;; Set up a specialized doc string.
761 ;; Use stored value since it is calculated in a non-trivial way
762 (put cname 'variable-documentation
763 (class-option-assoc options :documentation))
765 ;; We have a list of custom groups. Store them into the options.
766 (let ((g (class-option-assoc options :custom-groups)))
767 (mapc (lambda (cg) (add-to-list 'g cg)) groups)
768 (if (memq :custom-groups options)
769 (setcar (cdr (memq :custom-groups options)) g)
770 (setq options (cons :custom-groups (cons g options)))))
772 ;; Set up the options we have collected.
773 (aset newc class-options options)
775 ;; if this is a superclass, clear out parent (which was set to the
776 ;; default superclass eieio-default-superclass)
777 (if clearparent (aset newc class-parent nil))
779 ;; Create the cached default object.
780 (let ((cache (make-vector (+ (length (aref newc class-public-a))
782 (aset cache 0 'object)
783 (aset cache object-class cname)
784 (aset cache object-name 'default-cache-object)
785 (let ((eieio-skip-typecheck t))
786 ;; All type-checking has been done to our satisfaction
787 ;; before this call. Don't waste our time in this call..
788 (eieio-set-defaults cache t))
789 (aset newc class-default-object-cache cache))
791 ;; Return our new class object
796 (defun eieio-perform-slot-validation-for-default (slot spec value skipnil)
797 "For SLOT, signal if SPEC does not match VALUE.
798 If SKIPNIL is non-nil, then if VALUE is nil, return t."
799 (let ((val (eieio-default-eval-maybe value)))
800 (if (and (not eieio-skip-typecheck)
801 (not (and skipnil (null val)))
802 (not (eieio-perform-slot-validation spec val)))
803 (signal 'invalid-slot-type (list slot spec val)))))
805 (defun eieio-add-new-slot (newc a d doc type cust label custg print prot init alloc
806 &optional defaultoverride skipnil)
807 "Add into NEWC attribute A.
808 If A already exists in NEWC, then do nothing. If it doesn't exist,
809 then also add in D (defualt), DOC, TYPE, CUST, LABEL, CUSTG, PRINT, PROT, and INIT arg.
810 Argument ALLOC specifies if the slot is allocated per instance, or per class.
811 If optional DEFAULTOVERRIDE is non-nil, then if A exists in NEWC,
812 we must override it's value for a default.
813 Optional argument SKIPNIL indicates if type checking should be skipped
814 if default value is nil."
815 ;; Make sure we duplicate those items that are sequences.
817 (if (sequencep d) (setq d (copy-sequence d)))
818 ;; This copy can fail on a cons cell with a non-cons in the cdr. Lets skip it if it doesn't work.
820 (if (sequencep type) (setq type (copy-sequence type)))
821 (if (sequencep cust) (setq cust (copy-sequence cust)))
822 (if (sequencep custg) (setq custg (copy-sequence custg)))
824 ;; To prevent override information w/out specification of storage,
825 ;; we need to do this little hack.
826 (if (member a (aref newc class-class-allocation-a)) (setq alloc ':class))
828 (if (or (not alloc) (and (symbolp alloc) (eq alloc ':instance)))
829 ;; In this case, we modify the INSTANCE version of a given slot.
833 ;; Only add this element if it is so-far unique
834 (if (not (member a (aref newc class-public-a)))
836 (eieio-perform-slot-validation-for-default a type d skipnil)
837 (aset newc class-public-a (cons a (aref newc class-public-a)))
838 (aset newc class-public-d (cons d (aref newc class-public-d)))
839 (aset newc class-public-doc (cons doc (aref newc class-public-doc)))
840 (aset newc class-public-type (cons type (aref newc class-public-type)))
841 (aset newc class-public-custom (cons cust (aref newc class-public-custom)))
842 (aset newc class-public-custom-label (cons label (aref newc class-public-custom-label)))
843 (aset newc class-public-custom-group (cons custg (aref newc class-public-custom-group)))
844 (aset newc class-public-printer (cons print (aref newc class-public-printer)))
845 (aset newc class-protection (cons prot (aref newc class-protection)))
846 (aset newc class-initarg-tuples (cons (cons init a) (aref newc class-initarg-tuples)))
848 ;; When defaultoverride is true, we are usually adding new local
849 ;; attributes which must override the default value of any slot
850 ;; passed in by one of the parent classes.
851 (when defaultoverride
852 ;; There is a match, and we must override the old value.
853 (let* ((ca (aref newc class-public-a))
855 (num (- (length ca) (length np)))
856 (dp (if np (nthcdr num (aref newc class-public-d))
858 (tp (if np (nth num (aref newc class-public-type))))
861 (error "Eieio internal error overriding default value for %s"
863 ;; If type is passed in, is it the same?
864 (if (not (eq type t))
865 (if (not (equal type tp))
867 "Child slot type `%s' does not match inherited type `%s' for `%s'"
869 ;; If we have a repeat, only update the initarg...
870 (unless (eq d eieio-unbound)
871 (eieio-perform-slot-validation-for-default a tp d skipnil)
873 ;; If we have a new initarg, check for it.
875 (let* ((inits (aref newc class-initarg-tuples))
876 (inita (rassq a inits)))
877 ;; Replace the CAR of the associate INITA.
878 ;;(message "Initarg: %S replace %s" inita init)
882 ;; PLN Tue Jun 26 11:57:06 2007 : The protection is
883 ;; checked and SHOULD match the superclass
884 ;; protection. Otherwise an error is thrown. However
885 ;; I wonder if a more flexible schedule might be
888 ;; EML - We used to have (if prot... here,
889 ;; but a prot of 'nil means public.
891 (let ((super-prot (nth num (aref newc class-protection)))
893 (if (not (eq prot super-prot))
894 (error "Child slot protection `%s' does not match inherited protection `%s' for `%s'"
898 ;; PLN Tue Jun 26 11:57:06 2007 :
899 ;; We do a non redundant combination of ancient
900 ;; custom groups and new ones using the common lisp
904 (nthcdr num (aref newc class-public-custom-group))))
906 (union (car where-groups)
907 (if (listp custg) custg (list custg))))))
910 ;; PLN Mon Jun 25 22:44:34 2007 : If a new cust is
911 ;; set, simply replaces the old one.
913 ;; (message "Custom type redefined to %s" cust)
914 (setcar (nthcdr num (aref newc class-public-custom)) cust))
916 ;; If a new label is specified, it simply replaces
919 ;; (message "Custom label redefined to %s" label)
920 (setcar (nthcdr num (aref newc class-public-custom-label)) label))
923 ;; PLN Sat Jun 30 17:24:42 2007 : when a new
924 ;; doc is specified, simply replaces the old one.
926 ;;(message "Documentation redefined to %s" doc)
927 (setcar (nthcdr num (aref newc class-public-doc))
931 ;; If a new printer is specified, it simply replaces
934 ;; (message "printer redefined to %s" print)
935 (setcar (nthcdr num (aref newc class-public-printer)) print))
940 ;; CLASS ALLOCATED SLOTS
941 (let ((value (eieio-default-eval-maybe d)))
942 (if (not (member a (aref newc class-class-allocation-a)))
944 (eieio-perform-slot-validation-for-default a type value skipnil)
945 ;; Here we have found a :class version of a slot. This
946 ;; requires a very different aproach.
947 (aset newc class-class-allocation-a (cons a (aref newc class-class-allocation-a)))
948 (aset newc class-class-allocation-doc (cons doc (aref newc class-class-allocation-doc)))
949 (aset newc class-class-allocation-type (cons type (aref newc class-class-allocation-type)))
950 (aset newc class-class-allocation-custom (cons cust (aref newc class-class-allocation-custom)))
951 (aset newc class-class-allocation-custom-label (cons label (aref newc class-class-allocation-custom-label)))
952 (aset newc class-class-allocation-custom-group (cons custg (aref newc class-class-allocation-custom-group)))
953 (aset newc class-class-allocation-protection (cons prot (aref newc class-class-allocation-protection)))
954 ;; Default value is stored in the 'values section, since new objects
955 ;; can't initialize from this element.
956 (aset newc class-class-allocation-values (cons value (aref newc class-class-allocation-values))))
957 (when defaultoverride
958 ;; There is a match, and we must override the old value.
959 (let* ((ca (aref newc class-class-allocation-a))
961 (num (- (length ca) (length np)))
964 (aref newc class-class-allocation-values))
966 (tp (if np (nth num (aref newc class-class-allocation-type))
969 (error "Eieio internal error overriding default value for %s"
971 ;; If type is passed in, is it the same?
972 (if (not (eq type t))
973 (if (not (equal type tp))
975 "Child slot type `%s' does not match inherited type `%s' for `%s'"
977 ;; EML - Note: the only reason to override a class bound slot
978 ;; is to change the default, so allow unbound in.
980 ;; If we have a repeat, only update the vlaue...
981 (eieio-perform-slot-validation-for-default a tp value skipnil)
984 ;; PLN Tue Jun 26 11:57:06 2007 : The protection is
985 ;; checked and SHOULD match the superclass
986 ;; protection. Otherwise an error is thrown. However
987 ;; I wonder if a more flexible schedule might be
990 (car (nthcdr num (aref newc class-class-allocation-protection)))))
991 (if (not (eq prot super-prot))
992 (error "Child slot protection `%s' does not match inherited protection `%s' for `%s'"
994 ;; We do a non redundant combination of ancient
995 ;; custom groups and new ones using the common lisp
999 (nthcdr num (aref newc class-class-allocation-custom-group))))
1000 (setcar where-groups
1001 (union (car where-groups)
1002 (if (listp custg) custg (list custg))))))
1005 ;; PLN Sat Jun 30 17:24:42 2007 : when a new
1006 ;; doc is specified, simply replaces the old one.
1008 ;;(message "Documentation redefined to %s" doc)
1009 (setcar (nthcdr num (aref newc class-class-allocation-doc))
1013 ;; If a new printer is specified, it simply replaces
1016 ;; (message "printer redefined to %s" print)
1017 (setcar (nthcdr num (aref newc class-class-allocation-printer)) print))
1023 (defun eieio-copy-parents-into-subclass (newc parents)
1024 "Copy into NEWC the slots of PARENTS.
1025 Follow the rules of not overwritting early parents when applying to
1026 the new child class."
1027 (let ((ps (aref newc class-parent))
1028 (sn (class-option-assoc (aref newc class-options)
1029 ':allow-nil-initform)))
1031 ;; First, duplicate all the slots of the parent.
1032 (let ((pcv (class-v (car ps))))
1033 (let ((pa (aref pcv class-public-a))
1034 (pd (aref pcv class-public-d))
1035 (pdoc (aref pcv class-public-doc))
1036 (ptype (aref pcv class-public-type))
1037 (pcust (aref pcv class-public-custom))
1038 (plabel (aref pcv class-public-custom-label))
1039 (pcustg (aref pcv class-public-custom-group))
1040 (printer (aref pcv class-public-printer))
1041 (pprot (aref pcv class-protection))
1042 (pinit (aref pcv class-initarg-tuples))
1045 (eieio-add-new-slot newc
1046 (car pa) (car pd) (car pdoc) (aref ptype i)
1047 (car pcust) (car plabel) (car pcustg)
1049 (car pprot) (car-safe (car pinit)) nil nil sn)
1050 ;; Increment each value.
1058 printer (cdr printer)
1062 ;; Now duplicate all the class alloc slots.
1063 (let ((pa (aref pcv class-class-allocation-a))
1064 (pdoc (aref pcv class-class-allocation-doc))
1065 (ptype (aref pcv class-class-allocation-type))
1066 (pcust (aref pcv class-class-allocation-custom))
1067 (plabel (aref pcv class-class-allocation-custom-label))
1068 (pcustg (aref pcv class-class-allocation-custom-group))
1069 (printer (aref pcv class-class-allocation-printer))
1070 (pprot (aref pcv class-class-allocation-protection))
1071 (pval (aref pcv class-class-allocation-values))
1074 (eieio-add-new-slot newc
1075 (car pa) (aref pval i) (car pdoc) (aref ptype i)
1076 (car pcust) (car plabel) (car pcustg)
1078 (car pprot) nil ':class sn)
1079 ;; Increment each value.
1085 printer (cdr printer)
1089 ;; Loop over each parent class
1093 ;;; CLOS style implementation of object creators.
1095 (defun make-instance (class &rest initargs)
1096 "Make a new instance of CLASS based on INITARGS.
1097 CLASS is a class symbol. For example:
1099 (make-instance 'foo)
1101 INITARGS is a property list with keywords based on the :initarg
1102 for each slot. For example:
1104 (make-instance 'foo :slot1 value1 :slotN valueN)
1108 If the first element of INITARGS is a string, it is used as the
1111 In EIEIO, the class' constructor requires a name for use when printing.
1112 `make-instance' in CLOS doesn't use names the way Emacs does, so the
1113 class is used as the name slot instead when INITARGS doesn't start with
1115 (if (and (car initargs) (stringp (car initargs)))
1116 (apply (class-constructor class) initargs)
1117 (apply (class-constructor class)
1118 (cond ((symbolp class) (symbol-name class))
1119 (t (format "%S" class)))
1123 ;;; CLOS methods and generics
1125 (defmacro defgeneric (method args &optional doc-string)
1126 "Create a generic function METHOD. ARGS is ignored.
1127 DOC-STRING is the base documentation for this class. A generic
1128 function has no body, as it's purpose is to decide which method body
1129 is appropriate to use. Use `defmethod' to create methods, and it
1130 calls defgeneric for you. With this implementation the arguments are
1131 currently ignored. You can use `defgeneric' to apply specialized
1132 top level documentation to a method."
1133 `(eieio-defgeneric (quote ,method) ,doc-string))
1135 (defun eieio-defgeneric-form (method doc-string)
1136 "The lambda form that would be used as the function defined on METHOD.
1137 All methods should call the same EIEIO function for dispatch.
1138 DOC-STRING is the documentation attached to METHOD."
1139 `(lambda (&rest local-args)
1141 (eieio-generic-call (quote ,method) local-args)))
1143 (defsubst eieio-defgeneric-reset-generic-form (method)
1144 "Setup METHOD to call the generic form."
1145 (let ((doc-string (documentation method)))
1146 (fset method (eieio-defgeneric-form method doc-string))))
1148 (defun eieio-defgeneric-form-primary-only (method doc-string)
1149 "The lambda form that would be used as the function defined on METHOD.
1150 All methods should call the same EIEIO function for dispatch.
1151 DOC-STRING is the documentation attached to METHOD."
1152 `(lambda (&rest local-args)
1154 (eieio-generic-call-primary-only (quote ,method) local-args)))
1156 (defsubst eieio-defgeneric-reset-generic-form-primary-only (method)
1157 "Setup METHOD to call the generic form."
1158 (let ((doc-string (documentation method)))
1159 (fset method (eieio-defgeneric-form-primary-only method doc-string))))
1161 (defun eieio-defgeneric-form-primary-only-one (method doc-string
1165 "The lambda form that would be used as the function defined on METHOD.
1166 All methods should call the same EIEIO function for dispatch.
1167 DOC-STRING is the documentation attached to METHOD.
1168 CLASS is the class symbol needed for private method access.
1169 IMPL is the symbol holding the method implementation."
1170 ;; NOTE: I tried out byte compiling this little fcn. Turns out it
1171 ;; is faster to execute this for not byte-compiled. ie, install this,
1172 ;; then measure calls going through here. I wonder why.
1174 (let ((byte-compile-free-references nil)
1175 (byte-compile-warnings nil)
1177 (byte-compile-lambda
1178 `(lambda (&rest local-args)
1180 ;; This is a cool cheat. Usually we need to look up in the
1181 ;; method table to find out if there is a method or not. We can
1182 ;; instead make that determination at load time when there is
1183 ;; only one method. If the first arg is not a child of the class
1184 ;; of that one implementation, then clearly, there is no method def.
1185 (if (not (eieio-object-p (car local-args)))
1186 ;; Not an object. Just signal.
1187 (signal 'no-method-definition (list ,(list 'quote method) local-args))
1189 ;; We do have an object. Make sure it is the right type.
1190 (if ,(if (eq class eieio-default-superclass)
1191 nil ; default superclass means just an obj. Already asked.
1192 `(not (child-of-class-p (aref (car local-args) object-class)
1193 ,(list 'quote class)))
1196 ;; If not the right kind of object, call no applicable
1197 (apply 'no-applicable-method (car local-args)
1198 ,(list 'quote method) local-args)
1200 ;; It is ok, do the call.
1201 ;; Fill in inter-call variables then evaluate the method.
1202 (let ((scoped-class ,(list 'quote class))
1203 (eieio-generic-call-next-method-list nil)
1204 (eieio-generic-call-key method-primary)
1205 (eieio-generic-call-methodname ,(list 'quote method))
1206 (eieio-generic-call-arglst local-args)
1208 (apply ,(list 'quote impl) local-args)
1214 (defsubst eieio-defgeneric-reset-generic-form-primary-only-one (method)
1215 "Setup METHOD to call the generic form."
1216 (let* ((doc-string (documentation method))
1217 (M (get method 'eieio-method-tree))
1218 (entry (car (aref M method-primary)))
1220 (fset method (eieio-defgeneric-form-primary-only-one
1226 (defun eieio-defgeneric (method doc-string)
1227 "Engine part to `defgeneric' macro defining METHOD with DOC-STRING."
1228 (if (and (fboundp method) (not (generic-p method))
1229 (or (byte-code-function-p (symbol-function method))
1230 (not (eq 'autoload (car (symbol-function method)))))
1232 (error "You cannot create a generic/method over an existing symbol: %s"
1234 ;; Don't do this over and over.
1235 (unless (fboundp 'method)
1236 ;; This defun tells emacs where the first definition of this
1237 ;; method is defined.
1238 `(defun ,method nil)
1239 ;; Make sure the method tables are installed.
1240 (eieiomt-install method)
1241 ;; Apply the actual body of this function.
1242 (fset method (eieio-defgeneric-form method doc-string))
1243 ;; Return the method
1246 (defun eieio-unbind-method-implementations (method)
1247 "Make the generic method METHOD have no implementations..
1248 It will leave the original generic function in place, but remove
1249 reference to all implementations of METHOD."
1250 (put method 'eieio-method-tree nil)
1251 (put method 'eieio-method-obarray nil))
1253 (defmacro defmethod (method &rest args)
1254 "Create a new METHOD through `defgeneric' with ARGS.
1256 The second optional argument KEY is a specifier that
1257 modifies how the method is called, including:
1258 :before - Method will be called before the :primary
1259 :primary - The default if not specified.
1260 :after - Method will be called after the :primary
1261 :static - First arg could be an object or class
1262 The next argument is the ARGLIST. The ARGLIST specifies the arguments
1263 to the method as with `defun'. The first argument can have a type
1265 ((VARNAME CLASS) ARG2 ...)
1266 where VARNAME is the name of the local variable for the method being
1267 created. The CLASS is a class symbol for a class made with `defclass'.
1268 A DOCSTRING comes after the ARGLIST, and is optional.
1269 All the rest of the args are the BODY of the method. A method will
1270 return the value of the last form in the BODY.
1274 (defmethod mymethod [:before | :primary | :after | :static]
1275 ((typearg class-name) arg2 &optional opt &rest rest)
1278 `(eieio-defmethod (quote ,method) (quote ,args)))
1280 (defun eieio-defmethod (method args)
1281 "Work part of the `defmethod' macro defining METHOD with ARGS."
1282 (let ((key nil) (body nil) (firstarg nil) (argfix nil) (argclass nil) loopa)
1283 ;; find optional keys
1285 (cond ((or (eq ':BEFORE (car args))
1286 (eq ':before (car args)))
1287 (setq args (cdr args))
1289 ((or (eq ':AFTER (car args))
1290 (eq ':after (car args)))
1291 (setq args (cdr args))
1293 ((or (eq ':PRIMARY (car args))
1294 (eq ':primary (car args)))
1295 (setq args (cdr args))
1297 ((or (eq ':STATIC (car args))
1298 (eq ':static (car args)))
1299 (setq args (cdr args))
1302 (t method-primary)))
1303 ;; get body, and fix contents of args to be the arguments of the fn.
1304 (setq body (cdr args)
1307 ;; Create a fixed version of the arguments
1309 (setq argfix (cons (if (listp (car loopa)) (car (car loopa)) (car loopa))
1311 (setq loopa (cdr loopa)))
1312 ;; make sure there is a generic
1315 (if (stringp (car body))
1316 (car body) (format "Generically created method `%s'" method)))
1317 ;; create symbol for property to bind to. If the first arg is of
1318 ;; the form (varname vartype) and `vartype' is a class, then
1319 ;; that class will be the type symbol. If not, then it will fall
1320 ;; under the type `primary' which is a non-specific calling of the
1322 (setq firstarg (car args))
1323 (if (listp firstarg)
1325 (setq argclass (nth 1 firstarg))
1326 (if (not (class-p argclass))
1327 (error "Unknown class type %s in method parameters"
1330 (signal 'wrong-type-argument (list :static 'non-class-arg)))
1331 ;; generics are higher
1332 (setq key (+ key 3)))
1333 ;; Put this lambda into the symbol so we can find it
1334 (if (byte-code-function-p (car-safe body))
1335 (eieiomt-add method (car-safe body) key argclass)
1336 (eieiomt-add method (append (list 'lambda (reverse argfix)) body)
1340 (when eieio-optimize-primary-methods-flag
1343 ;; If this method, after this setup, only has primary methods, then
1344 ;; we can setup the generic that way.
1345 (if (generic-primary-only-p method)
1346 ;; If there is only one primary method, then we can go one more
1347 ;; optimization step.
1348 (if (generic-primary-only-one-p method)
1349 (eieio-defgeneric-reset-generic-form-primary-only-one method)
1350 (eieio-defgeneric-reset-generic-form-primary-only method))
1351 (eieio-defgeneric-reset-generic-form method)))
1355 ;;; Slot type validation
1357 (defun eieio-perform-slot-validation (spec value)
1358 "Return non-nil if SPEC does not match VALUE."
1359 ;; typep is in cl-macs
1360 (or (eq spec t) ; t always passes
1361 (eq value eieio-unbound) ; unbound always passes
1362 (typep value spec)))
1364 (defun eieio-validate-slot-value (class slot-idx value slot)
1365 "Make sure that for CLASS referencing SLOT-IDX, that VALUE is valid.
1366 Checks the :type specifier.
1367 SLOT is the slot that is being checked, and is only used when throwing
1369 (if eieio-skip-typecheck
1371 ;; Trim off object IDX junk added in for the object index.
1372 (setq slot-idx (- slot-idx 3))
1373 (let ((st (aref (aref (class-v class) class-public-type) slot-idx)))
1374 (if (not (eieio-perform-slot-validation st value))
1375 (signal 'invalid-slot-type (list class slot st value))))))
1377 (defun eieio-validate-class-slot-value (class slot-idx value slot)
1378 "Make sure that for CLASS referencing SLOT-IDX, that VALUE is valid.
1379 Checks the :type specifier.
1380 SLOT is the slot that is being checked, and is only used when throwing
1382 (if eieio-skip-typecheck
1384 (let ((st (aref (aref (class-v class) class-class-allocation-type)
1386 (if (not (eieio-perform-slot-validation st value))
1387 (signal 'invalid-slot-type (list class slot st value))))))
1389 (defun eieio-barf-if-slot-unbound (value instance slotname fn)
1390 "Throw a signal if VALUE is a representation of an UNBOUND slot.
1391 INSTANCE is the object being referenced. SLOTNAME is the offending
1392 slot. If the slot is ok, return VALUE.
1393 Argument FN is the function calling this verifier."
1394 (if (and (eq value eieio-unbound) (not eieio-skip-typecheck))
1395 (slot-unbound instance (object-class instance) slotname fn)
1398 ;;; Missing types that are useful to me.
1400 (defun boolean-p (bool)
1401 "Return non-nil if BOOL is nil or t."
1402 (or (null bool) (eq bool t)))
1404 ;;; Get/Set slots in an object.
1406 (defmacro oref (obj slot)
1407 "Retrieve the value stored in OBJ in the slot named by SLOT.
1408 Slot is the name of the slot when created by `defclass' or the label
1409 created by the :initarg tag."
1410 `(eieio-oref ,obj (quote ,slot)))
1412 (defun eieio-oref (obj slot)
1413 "Return the value in OBJ at SLOT in the object vector."
1414 (if (not (or (eieio-object-p obj) (class-p obj)))
1415 (signal 'wrong-type-argument (list '(or eieio-object-p class-p) obj)))
1416 (if (not (symbolp slot))
1417 (signal 'wrong-type-argument (list 'symbolp slot)))
1418 (if (class-p obj) (eieio-class-un-autoload obj))
1419 (let* ((class (if (class-p obj) obj (aref obj object-class)))
1420 (c (eieio-slot-name-index class obj slot)))
1422 ;; It might be missing because it is a :class allocated slot.
1423 ;; Lets check that info out.
1424 (if (setq c (eieio-class-slot-name-index class slot))
1426 (aref (aref (class-v class) class-class-allocation-values) c)
1427 ;; The slot-missing method is a cool way of allowing an object author
1428 ;; to intercept missing slot definitions. Since it is also the LAST
1429 ;; thing called in this fn, it's return value would be retrieved.
1430 (slot-missing obj slot 'oref)
1431 ;;(signal 'invalid-slot-name (list (object-name obj) slot))
1433 (if (not (eieio-object-p obj))
1434 (signal 'wrong-type-argument (list 'eieio-object-p obj)))
1435 (eieio-barf-if-slot-unbound (aref obj c) obj slot 'oref))))
1437 (defalias 'slot-value 'eieio-oref)
1438 (defalias 'set-slot-value 'eieio-oset)
1440 (defmacro oref-default (obj slot)
1441 "Gets the default value of OBJ (maybe a class) for SLOT.
1442 The default value is the value installed in a class with the :initform
1443 tag. SLOT can be the slot name, or the tag specified by the :initarg
1444 tag in the `defclass' call."
1445 `(eieio-oref-default ,obj (quote ,slot)))
1447 (defun eieio-oref-default (obj slot)
1448 "Does the work for the macro `oref-default' with similar parameters.
1449 Fills in OBJ's SLOT with it's default value."
1450 (if (not (or (eieio-object-p obj) (class-p obj))) (signal 'wrong-type-argument (list 'eieio-object-p obj)))
1451 (if (not (symbolp slot)) (signal 'wrong-type-argument (list 'symbolp slot)))
1452 (let* ((cl (if (eieio-object-p obj) (aref obj object-class) obj))
1453 (c (eieio-slot-name-index cl obj slot)))
1455 ;; It might be missing because it is a :class allocated slot.
1456 ;; Lets check that info out.
1458 (eieio-class-slot-name-index cl slot))
1460 (aref (aref (class-v cl) class-class-allocation-values)
1462 (slot-missing obj slot 'oref-default)
1463 ;;(signal 'invalid-slot-name (list (class-name cl) slot))
1465 (eieio-barf-if-slot-unbound
1466 (let ((val (nth (- c 3) (aref (class-v cl) class-public-d))))
1467 (eieio-default-eval-maybe val))
1468 obj cl 'oref-default))))
1470 (defun eieio-default-eval-maybe (val)
1471 "Check VAL, and return what `oref-default' would provide."
1472 ;; check for quoted things, and unquote them
1473 (if (and (listp val) (eq (car val) 'quote))
1475 ;; return it verbatim
1478 ;;; Object Set macros
1480 (defmacro oset (obj slot value)
1481 "Set the value in OBJ for slot SLOT to VALUE.
1482 SLOT is the slot name as specified in `defclass' or the tag created
1483 with in the :initarg slot. VALUE can be any Lisp object."
1484 `(eieio-oset ,obj (quote ,slot) ,value))
1486 (defun eieio-oset (obj slot value)
1487 "Does the work for the macro `oset'.
1488 Fills in OBJ's SLOT with VALUE."
1489 (if (not (eieio-object-p obj)) (signal 'wrong-type-argument (list 'eieio-object-p obj)))
1490 (if (not (symbolp slot)) (signal 'wrong-type-argument (list 'symbolp slot)))
1491 (let ((c (eieio-slot-name-index (object-class-fast obj) obj slot)))
1493 ;; It might be missing because it is a :class allocated slot.
1494 ;; Lets check that info out.
1496 (eieio-class-slot-name-index (aref obj object-class) slot))
1499 (eieio-validate-class-slot-value (object-class-fast obj) c value slot)
1500 (aset (aref (class-v (aref obj object-class))
1501 class-class-allocation-values)
1503 ;; See oref for comment on `slot-missing'
1504 (slot-missing obj slot 'oset value)
1505 ;;(signal 'invalid-slot-name (list (object-name obj) slot))
1507 (eieio-validate-slot-value (object-class-fast obj) c value slot)
1508 (aset obj c value))))
1510 (defmacro oset-default (class slot value)
1511 "Set the default slot in CLASS for SLOT to VALUE.
1512 The default value is usually set with the :initform tag during class
1513 creation. This allows users to change the default behavior of classes
1514 after they are created."
1515 `(eieio-oset-default ,class (quote ,slot) ,value))
1517 (defun eieio-oset-default (class slot value)
1518 "Does the work for the macro `oset-default'.
1519 Fills in the default value in CLASS' in SLOT with VALUE."
1520 (if (not (class-p class)) (signal 'wrong-type-argument (list 'class-p class)))
1521 (if (not (symbolp slot)) (signal 'wrong-type-argument (list 'symbolp slot)))
1522 (let* ((scoped-class class)
1523 (c (eieio-slot-name-index class nil slot)))
1525 ;; It might be missing because it is a :class allocated slot.
1526 ;; Lets check that info out.
1527 (if (setq c (eieio-class-slot-name-index class slot))
1530 (eieio-validate-class-slot-value class c value slot)
1531 (aset (aref (class-v class) class-class-allocation-values) c
1533 (signal 'invalid-slot-name (list (class-name class) slot)))
1534 (eieio-validate-slot-value class c value slot)
1535 ;; Set this into the storage for defaults.
1536 (setcar (nthcdr (- c 3) (aref (class-v class) class-public-d))
1538 ;; Take the value, and put it into our cache object.
1539 (eieio-oset (aref (class-v class) class-default-object-cache)
1543 ;;; Handy CLOS macros
1545 (defmacro with-slots (spec-list object &rest body)
1546 "Bind SPEC-LIST lexically to slot values in OBJECT, and execute BODY.
1547 This establishes a lexical environment for referring to the slots in
1548 the instance named by the given slot-names as though they were
1549 variables. Within such a context the value of the slot can be
1550 specified by using its slot name, as if it were a lexically bound
1551 variable. Both setf and setq can be used to set the value of the
1554 SPEC-LIST is of a form similar to `let'. For example:
1561 Where each VAR is the local variable given to the associated
1562 SLOT. A Slot specified without a variable name is given a
1563 variable name of the same name as the slot."
1564 ;; Transform the spec-list into a symbol-macrolet spec-list.
1565 (let ((mappings (mapcar (lambda (entry)
1566 (let ((var (if (listp entry) (car entry) entry))
1567 (slot (if (listp entry) (cadr entry) entry)))
1568 (list var `(slot-value ,object ',slot))))
1570 (append (list 'symbol-macrolet mappings)
1572 (put 'with-slots 'lisp-indent-function 2)
1575 ;;; Simple generators, and query functions. None of these would do
1576 ;; well embedded into an object.
1578 (defmacro object-class-fast (obj) "Return the class struct defining OBJ with no check."
1579 `(aref ,obj object-class))
1581 (defun class-name (class) "Return a Lisp like symbol name for CLASS."
1582 (if (not (class-p class)) (signal 'wrong-type-argument (list 'class-p class)))
1583 ;; I think this is supposed to return a symbol, but to me CLASS is a symbol,
1584 ;; and I wanted a string. Arg!
1585 (format "#<class %s>" (symbol-name class)))
1587 (defun object-name (obj &optional extra)
1588 "Return a Lisp like symbol string for object OBJ.
1589 If EXTRA, include that in the string returned to represent the symbol."
1590 (if (not (eieio-object-p obj)) (signal 'wrong-type-argument (list 'eieio-object-p obj)))
1591 (format "#<%s %s%s>" (symbol-name (object-class-fast obj))
1592 (aref obj object-name) (or extra "")))
1594 (defun object-name-string (obj) "Return a string which is OBJ's name."
1595 (if (not (eieio-object-p obj)) (signal 'wrong-type-argument (list 'eieio-object-p obj)))
1596 (aref obj object-name))
1598 (defun object-set-name-string (obj name) "Set the string which is OBJ's NAME."
1599 (if (not (eieio-object-p obj)) (signal 'wrong-type-argument (list 'eieio-object-p obj)))
1600 (if (not (stringp name)) (signal 'wrong-type-argument (list 'stringp name)))
1601 (aset obj object-name name))
1603 (defun object-class (obj) "Return the class struct defining OBJ."
1604 (if (not (eieio-object-p obj)) (signal 'wrong-type-argument (list 'eieio-object-p obj)))
1605 (object-class-fast obj))
1606 (defalias 'class-of 'object-class)
1608 (defun object-class-name (obj) "Return a Lisp like symbol name for OBJ's class."
1609 (if (not (eieio-object-p obj)) (signal 'wrong-type-argument (list 'eieio-object-p obj)))
1610 (class-name (object-class-fast obj)))
1612 (defmacro class-parents-fast (class) "Return parent classes to CLASS with no check."
1613 `(aref (class-v ,class) class-parent))
1615 (defun class-parents (class)
1616 "Return parent classes to CLASS. (overload of variable).
1618 The CLOS function `class-direct-superclasses' is aliased to this function."
1619 (if (not (class-p class)) (signal 'wrong-type-argument (list 'class-p class)))
1620 (class-parents-fast class))
1622 (defmacro class-children-fast (class) "Return child classes to CLASS with no check."
1623 `(aref (class-v ,class) class-children))
1625 (defun class-children (class)
1626 "Return child classses to CLASS.
1628 The CLOS function `class-direct-subclasses' is aliased to this function."
1629 (if (not (class-p class)) (signal 'wrong-type-argument (list 'class-p class)))
1630 (class-children-fast class))
1632 ;; Official CLOS functions.
1633 (defalias 'class-direct-superclasses 'class-parents)
1634 (defalias 'class-direct-subclasses 'class-children)
1636 (defmacro class-parent-fast (class) "Return first parent class to CLASS with no check."
1637 `(car (class-parents-fast ,class)))
1639 (defmacro class-parent (class) "Return first parent class to CLASS. (overload of variable)."
1640 `(car (class-parents ,class)))
1642 (defmacro same-class-fast-p (obj class) "Return t if OBJ is of class-type CLASS with no error checking."
1643 `(eq (aref ,obj object-class) ,class))
1645 (defun same-class-p (obj class) "Return t if OBJ is of class-type CLASS."
1646 (if (not (class-p class)) (signal 'wrong-type-argument (list 'class-p class)))
1647 (if (not (eieio-object-p obj)) (signal 'wrong-type-argument (list 'eieio-object-p obj)))
1648 (same-class-fast-p obj class))
1650 (defun object-of-class-p (obj class)
1651 "Return non-nil if OBJ is an instance of CLASS or CLASS' subclasses."
1652 (if (not (eieio-object-p obj)) (signal 'wrong-type-argument (list 'eieio-object-p obj)))
1653 ;; class will be checked one layer down
1654 (child-of-class-p (aref obj object-class) class))
1655 ;; Backwards compatibility
1656 (defalias 'obj-of-class-p 'object-of-class-p)
1658 (defun child-of-class-p (child class)
1659 "If CHILD class is a subclass of CLASS."
1660 (if (not (class-p class)) (signal 'wrong-type-argument (list 'class-p class)))
1661 (if (not (class-p child)) (signal 'wrong-type-argument (list 'class-p child)))
1663 (while (and child (not (eq child class)))
1664 (setq p (append p (aref (class-v child) class-parent))
1669 (defun object-slots (obj) "List of slots available in OBJ."
1670 (if (not (eieio-object-p obj)) (signal 'wrong-type-argument (list 'eieio-object-p obj)))
1671 (aref (class-v (object-class-fast obj)) class-public-a))
1673 (defun class-slot-initarg (class slot) "Fetch from CLASS, SLOT's :initarg."
1674 (if (not (class-p class)) (signal 'wrong-type-argument (list 'class-p class)))
1675 (let ((ia (aref (class-v class) class-initarg-tuples))
1677 (while (and ia (not f))
1678 (if (eq (cdr (car ia)) slot)
1679 (setq f (car (car ia))))
1683 ;;; CLOS queries into classes and slots
1685 (defun slot-boundp (object slot)
1686 "Non-nil if OBJECT's SLOT is bound.
1687 Setting a slot's value makes it bound. Calling `slot-makeunbound' will
1688 make a slot unbound.
1689 OBJECT can be an instance or a class."
1690 ;; Skip typechecking while retrieving this value.
1691 (let ((eieio-skip-typecheck t))
1692 ;; Return nil if the magic symbol is in there.
1693 (if (eieio-object-p object)
1694 (if (eq (eieio-oref object slot) eieio-unbound) nil t)
1695 (if (class-p object)
1696 (if (eq (eieio-oref-default object slot) eieio-unbound) nil t)
1697 (signal 'wrong-type-argument (list 'eieio-object-p object))))))
1699 (defun slot-makeunbound (object slot)
1700 "In OBJECT, make SLOT unbound."
1701 (eieio-oset object slot eieio-unbound))
1703 (defun slot-exists-p (object-or-class slot)
1704 "Non-nil if OBJECT-OR-CLASS has SLOT."
1705 (let ((cv (class-v (cond ((eieio-object-p object-or-class)
1706 (object-class object-or-class))
1707 ((class-p object-or-class)
1710 (or (memq slot (aref cv class-public-a))
1711 (memq slot (aref cv class-class-allocation-a)))
1714 (defun find-class (symbol &optional errorp)
1715 "Return the class that SYMBOL represents.
1716 If there is no class, nil is returned if ERRORP is nil.
1717 If ERRORP is non-nil, `wrong-argument-type' is signaled."
1718 (if (not (class-p symbol))
1719 (if errorp (signal 'wrong-type-argument (list 'class-p symbol))
1723 ;;; Slightly more complex utility functions for objects
1725 (defun object-assoc (key slot list)
1726 "Return an object if KEY is `equal' to SLOT's value of an object in LIST.
1727 LIST is a list of objects who's slots are searched.
1728 Objects in LIST do not need to have a slot named SLOT, nor does
1729 SLOT need to be bound. If these errors occur, those objects will
1731 (if (not (listp list)) (signal 'wrong-type-argument (list 'listp list)))
1732 (while (and list (not (condition-case nil
1733 ;; This prevents errors for missing slots.
1734 (equal key (eieio-oref (car list) slot))
1736 (setq list (cdr list)))
1739 (defun object-assoc-list (slot list)
1740 "Return an association list with the contents of SLOT as the key element.
1741 LIST must be a list of objects with SLOT in it.
1742 This is useful when you need to do completing read on an object group."
1743 (if (not (listp list)) (signal 'wrong-type-argument (list 'listp list)))
1744 (let ((assoclist nil))
1746 (setq assoclist (cons (cons (eieio-oref (car list) slot)
1749 (setq list (cdr list)))
1750 (nreverse assoclist)))
1752 (defun object-assoc-list-safe (slot list)
1753 "Return an association list with the contents of SLOT as the key element.
1754 LIST must be a list of objects, but those objects do not need to have
1755 SLOT in it. If it does not, then that element is left out of the association
1757 (if (not (listp list)) (signal 'wrong-type-argument (list 'listp list)))
1758 (let ((assoclist nil))
1760 (if (slot-exists-p (car list) slot)
1761 (setq assoclist (cons (cons (eieio-oref (car list) slot)
1764 (setq list (cdr list)))
1765 (nreverse assoclist)))
1767 (defun object-add-to-list (object slot item &optional append)
1768 "In OBJECT's SLOT, add ITEM to the list of elements.
1769 Optional argument APPEND indicates we need to append to the list.
1770 If ITEM already exists in the list in SLOT, then it is not added.
1771 Comparison is done with `equal' through the `member' function call.
1772 If SLOT is unbound, bind it to the list containing ITEM."
1774 ;; Find the originating list.
1775 (if (not (slot-boundp object slot))
1776 (setq ov (list item))
1777 (setq ov (eieio-oref object slot))
1778 ;; turn it into a list.
1780 (setq ov (list ov)))
1781 ;; Do the combination
1782 (if (not (member item ov))
1785 (append ov (list item))
1787 ;; Set back into the slot.
1788 (eieio-oset object slot ov)))
1790 (defun object-remove-from-list (object slot item)
1791 "In OBJECT's SLOT, remove occurrences of ITEM.
1792 Deletion is done with `delete', which deletes by side effect
1793 and comparisons are done with `equal'.
1794 If SLOT is unbound, do nothing."
1795 (if (not (slot-boundp object slot))
1797 (eieio-oset object slot (delete item (eieio-oref object slot)))))
1799 ;;; EIEIO internal search functions
1801 (defun eieio-slot-originating-class-p (start-class slot)
1802 "Return Non-nil if START-CLASS is the first class to define SLOT.
1803 This is for testing if `scoped-class' is the class that defines SLOT
1804 so that we can protect private slots."
1805 (let ((par (class-parents start-class))
1809 (while (and par ret)
1810 (if (intern-soft (symbol-name slot)
1811 (aref (class-v (car par))
1812 class-symbol-obarray))
1814 (setq par (cdr par)))
1817 (defun eieio-slot-name-index (class obj slot)
1818 "In CLASS for OBJ find the index of the named SLOT.
1819 The slot is a symbol which is installed in CLASS by the `defclass'
1820 call. OBJ can be nil, but if it is an object, and the slot in question
1821 is protected, access will be allowed if obj is a child of the currently
1823 If SLOT is the value created with :initarg instead,
1824 reverse-lookup that name, and recurse with the associated slot value."
1825 ;; Removed checks to outside this call
1826 (let* ((fsym (intern-soft (symbol-name slot)
1827 (aref (class-v class)
1828 class-symbol-obarray)))
1829 (fsi (if (symbolp fsym) (symbol-value fsym) nil)))
1832 ((not (get fsym 'protection))
1834 ((and (eq (get fsym 'protection) 'protected)
1836 (or (child-of-class-p class scoped-class)
1837 (and (eieio-object-p obj)
1838 (child-of-class-p class (object-class obj)))))
1840 ((and (eq (get fsym 'protection) 'private)
1841 (or (and scoped-class
1842 (eieio-slot-originating-class-p scoped-class slot))
1843 eieio-initializing-object))
1846 (let ((fn (eieio-initarg-to-attribute class slot)))
1847 (if fn (eieio-slot-name-index class obj fn) nil)))))
1849 (defun eieio-class-slot-name-index (class slot)
1850 "In CLASS find the index of the named SLOT.
1851 The slot is a symbol which is installed in CLASS by the `defclass'
1852 call. If SLOT is the value created with :initarg instead,
1853 reverse-lookup that name, and recurse with the associated slot value."
1854 ;; This will happen less often, and with fewer slots. Do this the
1855 ;; storage cheap way.
1856 (let* ((a (aref (class-v class) class-class-allocation-a))
1860 ;; Slot # is length of the total list, minus the remaining list of
1864 ;;; CLOS generics internal function handling
1866 (defvar eieio-generic-call-methodname nil
1867 "When using `call-next-method', provides a context on how to do it.")
1868 (defvar eieio-generic-call-arglst nil
1869 "When using `call-next-method', provides a context for parameters.")
1870 (defvar eieio-generic-call-key nil
1871 "When using `call-next-method', provides a context for the current key.
1872 Keys are a number representing :before, :primary, and :after methods.")
1873 (defvar eieio-generic-call-next-method-list nil
1874 "When executing a PRIMARY or STATIC method, track the 'next-method'.
1875 During executions, the list is first generated, then as each next method
1876 is called, the next method is popped off the stack.")
1878 (defvar eieio-pre-method-execution-hooks nil
1879 "*Hooks run just before a method is executed.
1880 The hook function must accept on argument, this list of forms
1881 about to be executed.")
1883 (defun eieio-generic-call (method args)
1884 "Call METHOD with ARGS.
1885 ARGS provides the context on which implementation to use.
1886 This should only be called from a generic function."
1887 ;; We must expand our arguments first as they are always
1888 ;; passed in as quoted symbols
1889 (let ((newargs nil) (mclass nil) (lambdas nil) (tlambdas nil) (keys nil)
1890 (eieio-generic-call-methodname method)
1891 (eieio-generic-call-arglst args)
1893 (primarymethodlist nil))
1896 firstarg (car newargs))
1897 ;; Is the class passed in autoloaded?
1898 ;; Since class names are also constructors, they can be autoloaded
1899 ;; via the autoload command. Check for this, and load them in.
1900 ;; It's ok if it doesn't turn out to be a class. Probably want that
1901 ;; function loaded anyway.
1902 (if (and (symbolp firstarg)
1904 (listp (symbol-function firstarg))
1905 (eq 'autoload (car (symbol-function firstarg))))
1906 (load (nth 1 (symbol-function firstarg))))
1907 ;; Determine the class to use.
1908 (cond ((eieio-object-p firstarg)
1909 (setq mclass (object-class-fast firstarg)))
1911 (setq mclass firstarg))
1913 ;; Make sure the class is a valid class
1914 ;; mclass can be nil (meaning a generic for should be used.
1915 ;; mclass cannot have a value that is not a class, however.
1916 (when (and (not (null mclass)) (not (class-p mclass)))
1917 (error "Cannot dispatch method %S on class %S"
1920 ;; Now create a list in reverse order of all the calls we have
1921 ;; make in order to successfully do this right. Rules:
1922 ;; 1) Only call generics if scoped-class is not defined
1923 ;; This prevents multiple calls in the case of recursion
1924 ;; 2) Only call static if this is a static method.
1925 ;; 3) Only call specifics if the definition allows for them.
1926 ;; 4) Call in order based on :before, :primary, and :after
1927 (when (eieio-object-p firstarg)
1928 ;; Non-static calls do all this stuff.
1933 (eieiomt-method-list method method-after mclass)
1934 (list (eieio-generic-form method method-after nil)))
1935 ;;(or (and mclass (eieio-generic-form method method-after mclass))
1936 ;; (eieio-generic-form method method-after nil))
1938 (setq lambdas (append tlambdas lambdas)
1939 keys (append (make-list (length tlambdas) method-after) keys))
1943 (or (and mclass (eieio-generic-form method method-primary mclass))
1944 (eieio-generic-form method method-primary nil)))
1946 (setq lambdas (cons tlambdas lambdas)
1947 keys (cons method-primary keys)
1949 (eieiomt-method-list method method-primary mclass)))
1954 (eieiomt-method-list method method-before mclass)
1955 (list (eieio-generic-form method method-before nil)))
1956 ;;(or (and mclass (eieio-generic-form method method-before mclass))
1957 ;; (eieio-generic-form method method-before nil))
1959 (setq lambdas (append tlambdas lambdas)
1960 keys (append (make-list (length tlambdas) method-before) keys))
1963 ;; If there were no methods found, then there could be :static methods.
1966 (eieio-generic-form method method-static mclass))
1967 (setq lambdas (cons tlambdas lambdas)
1968 keys (cons method-static keys)
1969 primarymethodlist ;; Re-use even with bad name here
1970 (eieiomt-method-list method method-static mclass)))
1972 (run-hook-with-args 'eieio-pre-method-execution-hooks
1975 ;; Now loop through all occurances forms which we must execute
1976 ;; (which are happily sorted now) and execute them all!
1977 (let ((rval nil) (lastval nil) (rvalever nil) (found nil))
1980 (let* ((scoped-class (cdr (car lambdas)))
1981 (eieio-generic-call-key (car keys))
1983 (or (= eieio-generic-call-key method-primary)
1984 (= eieio-generic-call-key method-static)))
1985 (eieio-generic-call-next-method-list
1986 ;; Use the cdr, as the first element is the fcn
1987 ;; we are calling right now.
1988 (when has-return-val (cdr primarymethodlist)))
1991 ;;(setq rval (apply (car (car lambdas)) newargs))
1992 (setq lastval (apply (car (car lambdas)) newargs))
1993 (when has-return-val
1997 (setq lambdas (cdr lambdas)
2000 (if (eieio-object-p (car args))
2001 (setq rval (apply 'no-applicable-method (car args) method args)
2004 'no-method-definition
2005 (list method args))))
2006 ;; Right Here... it could be that lastval is returned when
2007 ;; rvalever is nil. Is that right?
2010 (defun eieio-generic-call-primary-only (method args)
2011 "Call METHOD with ARGS for methods with only :PRIMARY implementations.
2012 ARGS provides the context on which implementation to use.
2013 This should only be called from a generic function.
2015 This method is like `eieio-generic-call', but only
2016 implementations in the :PRIMARY slot are queried. After many
2017 years of use, it appears that over 90% of methods in use
2018 have :PRIMARY implementations only. We can therefore optimize
2019 for this common case to improve performance."
2020 ;; We must expand our arguments first as they are always
2021 ;; passed in as quoted symbols
2022 (let ((newargs nil) (mclass nil) (lambdas nil)
2023 (eieio-generic-call-methodname method)
2024 (eieio-generic-call-arglst args)
2026 (primarymethodlist nil)
2030 firstarg (car newargs))
2032 ;; Determine the class to use.
2033 (cond ((eieio-object-p firstarg)
2034 (setq mclass (object-class-fast firstarg)))
2036 (error "Method %s called on nil" method))
2037 ((not (eieio-object-p firstarg))
2038 (error "Primary-only method %s called on something not an object" method))
2040 (error "EIEIO Error: Improperly classified method %s as primary only"
2043 ;; Make sure the class is a valid class
2044 ;; mclass can be nil (meaning a generic for should be used.
2045 ;; mclass cannot have a value that is not a class, however.
2047 (error "Cannot dispatch method %S on class %S" method mclass)
2051 (setq lambdas (eieio-generic-form method method-primary mclass))
2052 (setq primarymethodlist ;; Re-use even with bad name here
2053 (eieiomt-method-list method method-primary mclass))
2055 ;; Now loop through all occurances forms which we must execute
2056 ;; (which are happily sorted now) and execute them all!
2057 (let* ((rval nil) (lastval nil) (rvalever nil)
2058 (scoped-class (cdr lambdas))
2059 (eieio-generic-call-key method-primary)
2060 ;; Use the cdr, as the first element is the fcn
2061 ;; we are calling right now.
2062 (eieio-generic-call-next-method-list (cdr primarymethodlist))
2065 (if (or (not lambdas) (not (car lambdas)))
2067 ;; No methods found for this impl...
2068 (if (eieio-object-p (car args))
2069 (setq rval (apply 'no-applicable-method (car args) method args)
2072 'no-method-definition
2073 (list method args)))
2075 ;; Do the regular implementation here.
2077 (run-hook-with-args 'eieio-pre-method-execution-hooks
2080 (setq lastval (apply (car lambdas) newargs))
2085 ;; Right Here... it could be that lastval is returned when
2086 ;; rvalever is nil. Is that right?
2089 (defun eieiomt-method-list (method key class)
2090 "Return an alist list of methods lambdas.
2091 METHOD is the method name.
2092 KEY represents either :before, or :after methods.
2093 CLASS is the starting class to search from in the method tree.
2094 If CLASS is nil, then an empty list of methods should be returned."
2095 ;; Note: eieiomt - the MT means MethodTree. See more comments below
2096 ;; for the rest of the eieiomt methods.
2098 (mclass (list class)))
2100 ;; Note: a nil can show up in the class list once we start
2101 ;; searching through the method tree.
2103 ;; lookup the form to use for the PRIMARY object for the next level
2104 (let ((tmpl (eieio-generic-form method key (car mclass))))
2105 (when (or (not lambdas)
2106 ;; This prevents duplicates coming out of the
2107 ;; class method optimizer. Perhaps we should
2108 ;; just not optimize before/afters?
2109 (not (eq (car tmpl) (car (car lambdas)))))
2110 (setq lambdas (cons tmpl lambdas))
2111 (if (null (car lambdas))
2112 (setq lambdas (cdr lambdas))))))
2113 ;; Add new classes to mclass. Since our input might not be a class
2114 ;; protect against that.
2116 ;; If there is a class, append any methods it may provide
2117 ;; to the remainder of the class list.
2118 (let ((io (class-method-invocation-order (car mclass))))
2119 (if (eq io :depth-first)
2121 (setq mclass (append (eieiomt-next (car mclass)) (cdr mclass)))
2123 (setq mclass (append (cdr mclass) (eieiomt-next (car mclass)))))
2125 ;; Advance to next entry in mclass if it is nil.
2126 (setq mclass (cdr mclass)))
2128 (if (eq key method-after)
2130 (nreverse lambdas))))
2132 (defun next-method-p ()
2133 "Non-nil if there is a next method.
2134 Returns a list of lambda expressions which is the `next-method'
2136 eieio-generic-call-next-method-list)
2138 (defun call-next-method (&rest replacement-args)
2139 "Call the superclass method from a subclass method.
2140 The superclass method is specified in the current method list,
2141 and is called the next method.
2143 If REPLACEMENT-ARGS is non-nil, then use them instead of
2144 `eieio-generic-call-arglst'. The generic arg list are the
2145 arguments passed in at the top level.
2147 Use `next-method-p' to find out if there is a next method to call."
2148 (if (not scoped-class)
2149 (error "Call-next-method not called within a class specific method"))
2150 (if (and (/= eieio-generic-call-key method-primary)
2151 (/= eieio-generic-call-key method-static))
2152 (error "Cannot `call-next-method' except in :primary or :static methods")
2154 (let ((newargs (or replacement-args eieio-generic-call-arglst))
2155 (next (car eieio-generic-call-next-method-list))
2157 (if (or (not next) (not (car next)))
2158 (apply 'no-next-method (car newargs) (cdr newargs))
2159 (let* ((eieio-generic-call-next-method-list
2160 (cdr eieio-generic-call-next-method-list))
2161 (scoped-class (cdr next))
2168 ;; eieio-method-tree : eieiomt-
2170 ;; Stored as eieio-method-tree in property list of a generic method
2172 ;; (eieio-method-tree . [BEFORE PRIMARY AFTER
2173 ;; genericBEFORE genericPRIMARY genericAFTER])
2175 ;; (eieio-method-obarray . [BEFORE PRIMARY AFTER
2176 ;; genericBEFORE genericPRIMARY genericAFTER])
2177 ;; where the association is a vector.
2178 ;; (aref 0 -- all static methods.
2179 ;; (aref 1 -- all methods classified as :before
2180 ;; (aref 2 -- all methods classified as :primary
2181 ;; (aref 3 -- all methods classified as :after
2182 ;; (aref 4 -- a generic classified as :before
2183 ;; (aref 5 -- a generic classified as :primary
2184 ;; (aref 6 -- a generic classified as :after
2186 (defvar eieiomt-optimizing-obarray nil
2187 "While mapping atoms, this contain the obarray being optimized.")
2189 (defun eieiomt-install (method-name)
2190 "Install the method tree, and obarray onto METHOD-NAME.
2191 Do not do the work if they already exist."
2192 (let ((emtv (get method-name 'eieio-method-tree))
2193 (emto (get method-name 'eieio-method-obarray)))
2194 (if (or (not emtv) (not emto))
2196 (setq emtv (put method-name 'eieio-method-tree
2197 (make-vector method-num-slots nil))
2198 emto (put method-name 'eieio-method-obarray
2199 (make-vector method-num-slots nil)))
2200 (aset emto 0 (make-vector 11 0))
2201 (aset emto 1 (make-vector 11 0))
2202 (aset emto 2 (make-vector 41 0))
2203 (aset emto 3 (make-vector 11 0))
2206 (defun eieiomt-add (method-name method key class)
2207 "Add to METHOD-NAME the forms METHOD in a call position KEY for CLASS.
2208 METHOD-NAME is the name created by a call to `defgeneric'.
2209 METHOD are the forms for a given implementation.
2210 KEY is an integer (see comment in eieio.el near this function) which
2211 is associated with the :static :before :primary and :after tags.
2212 It also indicates if CLASS is defined or not.
2213 CLASS is the class this method is associated with."
2214 (if (or (> key method-num-slots) (< key 0))
2215 (error "Eieiomt-add: method key error!"))
2216 (let ((emtv (get method-name 'eieio-method-tree))
2217 (emto (get method-name 'eieio-method-obarray)))
2218 ;; Make sure the method tables are available.
2219 (if (or (not emtv) (not emto))
2220 (error "Programmer error: eieiomt-add"))
2221 ;; only add new cells on if it doesn't already exist!
2222 (if (assq class (aref emtv key))
2223 (setcdr (assq class (aref emtv key)) method)
2224 (aset emtv key (cons (cons class method) (aref emtv key))))
2225 ;; Add function definition into newly created symbol, and store
2226 ;; said symbol in the correct obarray, otherwise use the
2227 ;; other array to keep this stuff
2228 (if (< key method-num-lists)
2229 (let ((nsym (intern (symbol-name class) (aref emto key))))
2230 (fset nsym method)))
2231 ;; Now optimize the entire obarray
2232 (if (< key method-num-lists)
2233 (let ((eieiomt-optimizing-obarray (aref emto key)))
2234 ;; @todo - Is this overkill? Should we just clear the symbol?
2235 (mapatoms 'eieiomt-sym-optimize eieiomt-optimizing-obarray)))
2238 (defun eieiomt-next (class)
2239 "Return the next parent class for CLASS.
2240 If CLASS is a superclass, return variable `eieio-default-superclass'. If CLASS
2241 is variable `eieio-default-superclass' then return nil. This is different from
2242 function `class-parent' as class parent returns nil for superclasses. This
2243 function performs no type checking!"
2244 ;; No type-checking because all calls are made from functions which
2245 ;; are safe and do checking for us.
2246 (or (class-parents-fast class)
2247 (if (eq class 'eieio-default-superclass)
2249 '(eieio-default-superclass))))
2251 (defun eieiomt-sym-optimize (s)
2252 "Find the next class above S which has a function body for the optimizer."
2253 ;; (message "Optimizing %S" s)
2254 (let* ((es (intern-soft (symbol-name s))) ;external symbol of class
2255 (io (class-method-invocation-order es))
2258 ;; This converts ES from a single symbol to a list of parent classes.
2259 (setq es (eieiomt-next es))
2260 ;; Loop over ES, then it's children individually.
2261 ;; We can have multiple hits only at one level of the parent tree.
2262 (while (and es cont)
2263 (setq ov (intern-soft (symbol-name (car es)) eieiomt-optimizing-obarray))
2266 (set s ov) ;store ov as our next symbol
2268 (if (eq io :depth-first)
2269 ;; Pre-pend the subclasses of (car es) so we get
2270 ;; DEPTH FIRST optimization.
2271 (setq es (append (eieiomt-next (car es)) (cdr es)))
2272 ;; Else, we are breadth first.
2273 ;; (message "Class %s is breadth first" es)
2274 (setq es (append (cdr es) (eieiomt-next (car es))))
2276 ;; If there is no nearest call, then set our value to nil
2277 (if (not es) (set s nil))
2280 (defun eieio-generic-form (method key class)
2281 "Return the lambda form belonging to METHOD using KEY based upon CLASS.
2282 If CLASS is not a class then use `generic' instead. If class has no
2283 form, but has a parent class, then trace to that parent class. The
2284 first time a form is requested from a symbol, an optimized path is
2285 memoized for future faster use."
2286 (let ((emto (aref (get method 'eieio-method-obarray)
2287 (if class key (+ key 3)))))
2289 ;; 1) find our symbol
2290 (let ((cs (intern-soft (symbol-name class) emto)))
2292 ;; 2) If there isn't one, then make one.
2293 ;; This can be slow since it only occurs once
2295 (setq cs (intern (symbol-name class) emto))
2296 ;; 2.1) Cache it's nearest neighbor with a quick optimize
2297 ;; which should only occur once for this call ever
2298 (let ((eieiomt-optimizing-obarray emto))
2299 (eieiomt-sym-optimize cs))))
2300 ;; 3) If it's bound return this one.
2302 (cons cs (aref (class-v class) class-symbol))
2303 ;; 4) If it's not bound then this variable knows something
2304 (if (symbol-value cs)
2306 ;; 4.1) This symbol holds the next class in it's value
2307 (setq class (symbol-value cs)
2308 cs (intern-soft (symbol-name class) emto))
2309 ;; 4.2) The optimizer should always have chosen a
2312 (cons cs (aref (class-v (intern (symbol-name class)))
2314 ;;(error "EIEIO optimizer: erratic data loss!"))
2316 ;; There never will be a funcall...
2318 ;; for a generic call, what is a list, is the function body we want.
2319 (let ((emtl (aref (get method 'eieio-method-tree)
2320 (if class key (+ key 3)))))
2322 ;; The car of EMTL is supposed to be a class, which in this
2323 ;; case is nil, so skip it.
2324 (cons (cdr (car emtl)) nil)
2328 ;; Way to assign slots based on a list. Used for constructors, or
2329 ;; even resetting an object at run-time
2331 (defun eieio-set-defaults (obj &optional set-all)
2332 "Take object OBJ, and reset all slots to their defaults.
2333 If SET-ALL is non-nil, then when a default is nil, that value is
2334 reset. If SET-ALL is nil, the slots are only reset if the default is
2336 (let ((scoped-class (aref obj object-class))
2337 (eieio-initializing-object t)
2338 (pub (aref (class-v (aref obj object-class)) class-public-a)))
2340 (let ((df (eieio-oref-default obj (car pub))))
2342 (eieio-oset obj (car pub) df)))
2343 (setq pub (cdr pub)))))
2345 (defun eieio-initarg-to-attribute (class initarg)
2346 "For CLASS, convert INITARG to the actual attribute name.
2347 If there is no translation, pass it in directly (so we can cheat if
2348 need be.. May remove that later...)"
2349 (let ((tuple (assoc initarg (aref (class-v class) class-initarg-tuples))))
2354 (defun eieio-attribute-to-initarg (class attribute)
2355 "In CLASS, convert the ATTRIBUTE into the corresponding init argument tag.
2356 This is usually a symbol that starts with `:'."
2357 (let ((tuple (rassoc attribute (aref (class-v class) class-initarg-tuples))))
2363 ;;; Here are some special types of errors
2365 (intern "no-method-definition")
2366 (put 'no-method-definition 'error-conditions '(no-method-definition error))
2367 (put 'no-method-definition 'error-message "No method definition")
2369 (intern "no-next-method")
2370 (put 'no-next-method 'error-conditions '(no-next-method error))
2371 (put 'no-next-method 'error-message "No next method")
2373 (intern "invalid-slot-name")
2374 (put 'invalid-slot-name 'error-conditions '(invalid-slot-name error))
2375 (put 'invalid-slot-name 'error-message "Invalid slot name")
2377 (intern "invalid-slot-type")
2378 (put 'invalid-slot-type 'error-conditions '(invalid-slot-type error nil))
2379 (put 'invalid-slot-type 'error-message "Invalid slot type")
2381 (intern "unbound-slot")
2382 (put 'unbound-slot 'error-conditions '(unbound-slot error nil))
2383 (put 'unbound-slot 'error-message "Unbound slot")
2385 ;;; Here are some CLOS items that need the CL package
2388 (defsetf slot-value (obj slot) (store) (list 'eieio-oset obj slot store))
2389 (defsetf eieio-oref (obj slot) (store) (list 'eieio-oset obj slot store))
2391 ;; The below setf method was written by Arnd Kohrs <kohrs@acm.org>
2392 (define-setf-method oref (obj slot)
2393 (let ((obj-temp (gensym))
2394 (slot-temp (gensym))
2395 (store-temp (gensym)))
2396 (list (list obj-temp slot-temp)
2397 (list obj `(quote ,slot))
2399 (list 'set-slot-value obj-temp slot-temp
2401 (list 'slot-value obj-temp slot-temp))))
2405 ;; We want all objects created by EIEIO to have some default set of
2406 ;; behaviours so we can create object utilities, and allow various
2407 ;; types of error checking. To do this, create the default EIEIO
2408 ;; class, and when no parent class is specified, use this as the
2409 ;; default. (But don't store it in the other classes as the default,
2410 ;; allowing for transparent support.)
2413 (defclass eieio-default-superclass nil
2415 "Default parent class for classes with no specified parent class.
2416 Its slots are automatically adopted by classes with no specified
2417 parents. This class is not stored in the `parent' slot of a class vector."
2420 (defalias 'standard-class 'eieio-default-superclass)
2422 (defgeneric constructor (class newname &rest slots)
2423 "Default constructor for CLASS `eieio-defualt-superclass'.")
2425 (defmethod constructor :static
2426 ((class eieio-default-superclass) newname &rest slots)
2427 "Default constructor for CLASS `eieio-defualt-superclass'.
2428 NEWNAME is the name to be given to the constructed object.
2429 SLOTS are the initialization slots used by `shared-initialize'.
2430 This static method is called when an object is constructed.
2431 It allocates the vector used to represent an EIEIO object, and then
2432 calls `shared-initialize' on that object."
2433 (let* ((new-object (copy-sequence (aref (class-v class)
2434 class-default-object-cache))))
2435 ;; Update the name for the newly created object.
2436 (aset new-object object-name newname)
2437 ;; Call the initialize method on the new object with the slots
2438 ;; that were passed down to us.
2439 (initialize-instance new-object slots)
2440 ;; Return the created object.
2443 (defgeneric shared-initialize (obj slots)
2444 "Set slots of OBJ with SLOTS which is a list of name/value pairs.
2445 Called from the constructor routine.")
2447 (defmethod shared-initialize ((obj eieio-default-superclass) slots)
2448 "Set slots of OBJ with SLOTS which is a list of name/value pairs.
2449 Called from the constructor routine."
2450 (let ((scoped-class (aref obj object-class)))
2452 (let ((rn (eieio-initarg-to-attribute (object-class-fast obj)
2455 (slot-missing obj (car slots) 'oset (car (cdr slots)))
2456 (eieio-oset obj rn (car (cdr slots)))))
2457 (setq slots (cdr (cdr slots))))))
2459 (defgeneric initialize-instance (this &optional slots)
2460 "Constructs the new object THIS based on SLOTS.")
2462 (defmethod initialize-instance ((this eieio-default-superclass)
2464 "Constructs the new object THIS based on SLOTS.
2465 SLOTS is a tagged list where odd numbered elements are tags, and
2466 even numbered elements are the values to store in the tagged slot. If
2467 you overload the `initialize-instance', there you will need to call
2468 `shared-initialize' yourself, or you can call `call-next-method' to
2469 have this constructor called automatically. If these steps are not
2470 taken, then new objects of your class will not have their values
2471 dynamically set from SLOTS."
2472 ;; First, see if any of our defaults are `lambda', and
2473 ;; re-evaluate them and apply the value to our slots.
2474 (let* ((scoped-class (class-v (aref this object-class)))
2475 (slot (aref scoped-class class-public-a))
2476 (defaults (aref scoped-class class-public-d)))
2478 (setq slot (cdr slot)
2479 defaults (cdr defaults))))
2480 ;; Shared initialize will parse our slots for us.
2481 (shared-initialize this slots))
2483 (defgeneric slot-missing (object slot-name operation &optional new-value)
2484 "Method invoked when an attempt to access a slot in OBJECT fails.")
2486 (defmethod slot-missing ((object eieio-default-superclass) slot-name
2487 operation &optional new-value)
2488 "Method invoked when an attempt to access a slot in OBJECT fails.
2489 SLOT-NAME is the name of the failed slot, OPERATION is the type of access
2490 that was requested, and optional NEW-VALUE is the value that was desired
2493 This method is called from `oref', `oset', and other functions which
2494 directly reference slots in EIEIO objects."
2495 (signal 'invalid-slot-name (list (object-name object)
2498 (defgeneric slot-unbound (object class slot-name fn)
2499 "Slot unbound is invoked during an attempt to reference an unbound slot.")
2501 (defmethod slot-unbound ((object eieio-default-superclass)
2503 "Slot unbound is invoked during an attempt to reference an unbound slot.
2504 OBJECT is the instance of the object being reference. CLASS is the
2505 class of OBJECT, and SLOT-NAME is the offending slot. This function
2506 throws the signal `unbound-slot'. You can overload this function and
2507 return the value to use in place of the unbound value.
2508 Argument FN is the function signaling this error.
2509 Use `slot-boundp' to determine if a slot is bound or not.
2511 In CLOS, the argument list is (CLASS OBJECT SLOT-NAME), but
2512 EIEIO can only dispatch on the first argument, so the first two are swapped."
2513 (signal 'unbound-slot (list (class-name class) (object-name object)
2516 (defgeneric no-applicable-method (object method &rest args)
2517 "Called if there are no implementations for OBJECT in METHOD.")
2519 (defmethod no-applicable-method ((object eieio-default-superclass)
2521 "Called if there are no implementations for OBJECT in METHOD.
2522 OBJECT is the object which has no method implementation.
2523 ARGS are the arguments that were passed to METHOD.
2525 Implement this for a class to block this signal. The return
2526 value becomes the return value of the original method call."
2527 (signal 'no-method-definition (list method (object-name object)))
2530 (defgeneric no-next-method (object &rest args)
2531 "Called from `call-next-method' when no additional methods are available.")
2533 (defmethod no-next-method ((object eieio-default-superclass)
2535 "Called from `call-next-method' when no additional methods are available.
2536 OBJECT is othe object being called on `call-next-method'.
2537 ARGS are the arguments it is called by.
2538 This method signals `no-next-method' by default. Override this
2539 method to not throw an error, and it's return value becomes the
2540 return value of `call-next-method'."
2541 (signal 'no-next-method (list (object-name object) args))
2544 (defgeneric clone (obj &rest params)
2545 "Make a copy of OBJ, and then supply PARAMS.
2546 PARAMS is a parameter list of the same form used by `initialize-instance'.
2548 When overloading `clone', be sure to call `call-next-method'
2549 first and modify the returned object.")
2551 (defmethod clone ((obj eieio-default-superclass) &rest params)
2552 "Make a copy of OBJ, and then apply PARAMS."
2553 (let ((nobj (copy-sequence obj))
2554 (nm (aref obj object-name))
2555 (passname (and params (stringp (car params))))
2557 (if params (shared-initialize nobj (if passname (cdr params) params)))
2560 (if (string-match "-\\([0-9]+\\)" nm)
2561 (setq num (1+ (string-to-number (match-string 1 nm)))
2562 nm (substring nm 0 (match-beginning 0))))
2563 (aset nobj object-name (concat nm "-" (int-to-string num))))
2564 (aset nobj object-name (car params)))
2567 (defgeneric destructor (this &rest params)
2568 "Destructor for cleaning up any dynamic links to our object.")
2570 (defmethod destructor ((this eieio-default-superclass) &rest params)
2571 "Destructor for cleaning up any dynamic links to our object.
2572 Argument THIS is the object being destroyed. PARAMS are additional
2573 ignored parameters."
2574 ;; No cleanup... yet.
2577 (defgeneric object-print (this &rest strings)
2578 "Pretty printer for object THIS. Call function `object-name' with STRINGS.
2580 It is sometimes useful to put a summary of the object into the
2581 default #<notation> string when using eieio browsing tools.
2582 Implement this method to customize the summary.")
2584 (defmethod object-print ((this eieio-default-superclass) &rest strings)
2585 "Pretty printer for object THIS. Call function `object-name' with STRINGS.
2586 The default method for printing object THIS is to use the
2587 function `object-name'.
2589 It is sometimes useful to put a summary of the object into the
2590 default #<notation> string when using eieio browsing tools.
2592 Implement this function and specify STRINGS in a call to
2593 `call-next-method' to provide additional summary information.
2594 When passing in extra strings from child classes, always remember
2595 to prepend a space."
2596 (object-name this (apply 'concat strings)))
2598 (defvar eieio-print-depth 0
2599 "When printing, keep track of the current indentation depth.")
2601 (defgeneric object-write (this &optional comment)
2602 "Write out object THIS to the current stream.
2603 Optional COMMENDS will add comments to the beginning of the output.")
2605 (defmethod object-write ((this eieio-default-superclass) &optional comment)
2606 "Write object THIS out to the current stream.
2607 This writes out the vector version of this object. Complex and recursive
2608 object are discouraged from being written.
2609 If optional COMMENT is non-nil, include comments when outputting
2612 (princ ";; Object ")
2613 (princ (object-name-string this))
2617 (let* ((cl (object-class this))
2619 ;; Now output readable lisp to recreate this object
2620 ;; It should look like this:
2621 ;; (<constructor> <name> <slot> <slot> ... )
2622 ;; Each slot's slot is writen using its :writer.
2623 (princ (make-string (* eieio-print-depth 2) ? ))
2625 (princ (symbol-name (class-constructor (object-class this))))
2627 (princ (object-name-string this))
2629 ;; Loop over all the public slots
2630 (let ((publa (aref cv class-public-a))
2631 (publd (aref cv class-public-d))
2632 (publp (aref cv class-public-printer))
2633 (eieio-print-depth (1+ eieio-print-depth)))
2635 (when (slot-boundp this (car publa))
2636 (let ((i (class-slot-initarg cl (car publa)))
2637 (v (eieio-oref this (car publa)))
2639 (unless (or (not i) (equal v (car publd)))
2640 (princ (make-string (* eieio-print-depth 2) ? ))
2641 (princ (symbol-name i))
2644 ;; Use our public printer
2645 (funcall (car publp) v)
2646 ;; Use our generic override prin1 function.
2647 (eieio-override-prin1 v))
2649 (setq publa (cdr publa) publd (cdr publd)
2651 (princ (make-string (* eieio-print-depth 2) ? )))
2654 (defun eieio-override-prin1 (thing)
2655 "Perform a prin1 on THING taking advantage of object knowledge."
2656 (cond ((eieio-object-p thing)
2657 (object-write thing))
2659 (eieio-list-prin1 thing))
2661 (princ (class-name thing)))
2663 (princ (concat "'" (symbol-name thing))))
2666 (defun eieio-list-prin1 (list)
2667 "Display LIST where list may contain objects."
2668 (if (not (eieio-object-p (car list)))
2673 (if (eieio-object-p (car list)) (princ "\n "))
2675 (if (eieio-object-p (car list))
2676 (object-write (car list))
2680 (setq list (cdr list)))
2681 (princ (make-string (* eieio-print-depth 2) ? ))
2685 ;;; Unimplemented functions from CLOS
2687 (defun change-class (obj class)
2688 "Change the class of OBJ to type CLASS.
2689 This may create or delete slots, but does not affect the return value
2691 (error "Eieio: `change-class' is unimplemented"))
2696 ;;; Interfacing with edebug
2698 (defun eieio-edebug-prin1-to-string (object &optional noescape)
2699 "Display eieio OBJECT in fancy format. Overrides the edebug default.
2700 Optional argument NOESCAPE is passed to `prin1-to-string' when appropriate."
2701 (cond ((class-p object) (class-name object))
2702 ((eieio-object-p object) (object-print object))
2703 ((and (listp object) (or (class-p (car object))
2704 (eieio-object-p (car object))))
2705 (concat "(" (mapconcat 'eieio-edebug-prin1-to-string object " ") ")"))
2706 (t (prin1-to-string object noescape))))
2708 (add-hook 'edebug-setup-hook
2710 (def-edebug-spec defmethod
2711 (&define ; this means we are defining something
2712 [&or name ("setf" :name setf name)]
2713 ;; ^^ This is the methods symbol
2714 [ &optional symbolp ] ; this is key :before etc
2716 [ &optional stringp ] ; documentation string
2717 def-body ; part to be debugged
2719 ;; The rest of the macros
2720 (def-edebug-spec oref (form quote))
2721 (def-edebug-spec oref-default (form quote))
2722 (def-edebug-spec oset (form quote form))
2723 (def-edebug-spec oset-default (form quote form))
2724 (def-edebug-spec class-v form)
2725 (def-edebug-spec class-p form)
2726 (def-edebug-spec eieio-object-p form)
2727 (def-edebug-spec class-constructor form)
2728 (def-edebug-spec generic-p form)
2729 (def-edebug-spec with-slots (list list def-body))
2730 ;; I suspect this isn't the best way to do this, but when
2731 ;; cust-print was used on my system all my objects
2732 ;; appeared as "#1 =" which was not useful. This allows
2733 ;; edebug to print my objects in the nice way they were
2734 ;; meant to with `object-print' and `class-name'
2735 ;; (defalias 'edebug-prin1-to-string 'eieio-edebug-prin1-to-string)
2739 (eval-after-load "cedet-edebug"
2741 (cedet-edebug-add-print-override '(class-p object) '(class-name object) )
2742 (cedet-edebug-add-print-override '(eieio-object-p object) '(object-print object) )
2743 (cedet-edebug-add-print-override '(and (listp object)
2744 (or (class-p (car object)) (eieio-object-p (car object))))
2745 '(cedet-edebug-prin1-recurse object) )
2748 ;; Done in cedet/data-debug.el:
2749 ;; (eval-after-load "data-debug"
2750 ;; '(require 'eieio-datadebug))
2752 ;;; Interfacing with imenu in emacs lisp mode
2753 ;; (Only if the expression is defined)
2755 (if (eval-when-compile (boundp 'list-imenu-generic-expression))
2758 (defun eieio-update-lisp-imenu-expression ()
2759 "Examine `lisp-imenu-generic-expression' and modify it to find `defmethod'."
2760 (let ((exp lisp-imenu-generic-expression))
2762 ;; it's of the form '( ( title expr indx ) ... )
2763 (let* ((subcar (cdr (car exp)))
2764 (substr (car subcar)))
2765 (if (and (not (string-match "|method\\\\" substr))
2766 (string-match "|advice\\\\" substr))
2768 (replace-match "|advice\\|method\\" t t substr 0))))
2769 (setq exp (cdr exp)))))
2771 (eieio-update-lisp-imenu-expression)
2775 ;;; Autoloading some external symbols, and hooking into the help system
2778 (autoload 'eieio-help-mode-augmentation-maybee "eieio-opt" "For buffers thrown into help mode, augment for eieio.")
2779 (autoload 'eieio-browse "eieio-opt" "Create an object browser window" t)
2780 (autoload 'eieio-describe-class "eieio-opt" "Describe CLASS defined by a string or symbol" t)
2781 (autoload 'eieio-describe-constructor "eieio-opt" "Describe the constructor function FCN." t)
2782 (autoload 'describe-class "eieio-opt" "Describe CLASS defined by a string or symbol" t)
2783 (autoload 'eieio-describe-generic "eieio-opt" "Describe GENERIC defined by a string or symbol" t)
2784 (autoload 'describe-generic "eieio-opt" "Describe GENERIC defined by a string or symbol" t)
2785 (autoload 'eieiodoc-class "eieio-doc" "Create texinfo documentation about a class hierarchy." t)
2787 (autoload 'customize-object "eieio-custom" "Create a custom buffer editing OBJ.")
2789 ;; make sure this shows up after the help mode hook.
2790 (add-hook 'temp-buffer-show-hook 'eieio-help-mode-augmentation-maybee t)
2791 ;; (require 'advice)
2792 ;; (defadvice describe-variable (around eieio-describe activate)
2793 ;; "Display the full documentation of FUNCTION (a symbol).
2794 ;; Returns the documentation as a string, also."
2795 ;; (if (class-p (ad-get-arg 0))
2796 ;; (eieio-describe-class (ad-get-arg 0))
2799 ;; (defadvice describe-function (around eieio-describe activate)
2800 ;; "Display the full documentation of VARIABLE (a symbol).
2801 ;; Returns the documentation as a string, also."
2802 ;; (if (generic-p (ad-get-arg 0))
2803 ;; (eieio-describe-generic (ad-get-arg 0))
2804 ;; (if (class-p (ad-get-arg 0))
2805 ;; (eieio-describe-constructor (ad-get-arg 0))