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-2011 Free Software Foundation, Inc.
6 ;; Author: Eric M. Ludlam <zappo@gnu.org>
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 ;; EIEIO is a series of Lisp routines which implements a subset of
28 ;; CLOS, the Common Lisp Object System. In addition, EIEIO also adds
29 ;; a few new features which help it integrate more strongly with the
30 ;; Emacs running environment.
32 ;; See eieio.texi for complete documentation on using this package.
34 ;; Note: the implementation of the c3 algorithm is based on:
35 ;; Kim Barrett et al.: A Monotonic Superclass Linearization for Dylan
37 ;; http://192.220.96.201/dylan/linearization-oopsla96.html
39 ;; There is funny stuff going on with typep and deftype. This
40 ;; is the only way I seem to be able to make this stuff load properly.
42 ;; @TODO - fix :initform to be a form, not a quoted value
43 ;; @TODO - Prefix non-clos functions with `eieio-'.
50 (defvar eieio-version
"1.3"
51 "Current version of EIEIO.")
53 (defun eieio-version ()
54 "Display the current version of EIEIO."
56 (message eieio-version
))
59 ;; About the above. EIEIO must process its own code when it compiles
60 ;; itself, thus, by eval-and-compiling outselves, we solve the problem.
63 (if (fboundp 'compiled-function-arglist
)
65 ;; XEmacs can only access a compiled functions arglist like this:
66 (defalias 'eieio-compiled-function-arglist
'compiled-function-arglist
)
68 ;; Emacs doesn't have this function, but since FUNC is a vector, we can just
69 ;; grab the appropriate element.
70 (defun eieio-compiled-function-arglist (func)
71 "Return the argument list for the compiled function FUNC."
78 ;; Variable declarations.
81 (defvar eieio-hook nil
82 "*This hook is executed, then cleared each time `defclass' is called.")
84 (defvar eieio-error-unsupported-class-tags nil
85 "Non-nil to throw an error if an encountered tag is unsupported.
86 This may prevent classes from CLOS applications from being used with EIEIO
87 since EIEIO does not support all CLOS tags.")
89 (defvar eieio-skip-typecheck nil
90 "*If non-nil, skip all slot typechecking.
91 Set this to t permanently if a program is functioning well to get a
92 small speed increase. This variable is also used internally to handle
93 default setting for optimization purposes.")
95 (defvar eieio-optimize-primary-methods-flag t
96 "Non-nil means to optimize the method dispatch on primary methods.")
99 ;; FIXME: These two constants below should have an `eieio-' prefix added!!
101 "Inside a method, this variable is the object in question.
102 DO NOT SET THIS YOURSELF unless you are trying to simulate friendly slots.
104 Note: Embedded methods are no longer supported. The variable THIS is
105 still set for CLOS methods for the sake of routines like
106 `call-next-method'.")
108 (defvar scoped-class nil
109 "This is set to a class when a method is running.
110 This is so we know we are allowed to check private parts or how to
111 execute a `call-next-method'. DO NOT SET THIS YOURSELF!")
113 (defvar eieio-initializing-object nil
114 "Set to non-nil while initializing an object.")
116 (defconst eieio-unbound
117 (if (and (boundp 'eieio-unbound
) (symbolp eieio-unbound
))
119 (make-symbol "unbound"))
120 "Uninterned symbol representing an unbound slot in an object.")
122 ;; This is a bootstrap for eieio-default-superclass so it has a value
123 ;; while it is being built itself.
124 (defvar eieio-default-superclass nil
)
126 ;; FIXME: The constants below should have an `eieio-' prefix added!!
127 (defconst class-symbol
1 "Class's symbol (self-referencing.).")
128 (defconst class-parent
2 "Class parent slot.")
129 (defconst class-children
3 "Class children class slot.")
130 (defconst class-symbol-obarray
4 "Obarray permitting fast access to variable position indexes.")
132 ;; the word "public" here is leftovers from the very first version.
134 (defconst class-public-a
5 "Class attribute index.")
135 (defconst class-public-d
6 "Class attribute defaults index.")
136 (defconst class-public-doc
7 "Class documentation strings for attributes.")
137 (defconst class-public-type
8 "Class type for a slot.")
138 (defconst class-public-custom
9 "Class custom type for a slot.")
139 (defconst class-public-custom-label
10 "Class custom group for a slot.")
140 (defconst class-public-custom-group
11 "Class custom group for a slot.")
141 (defconst class-public-printer
12 "Printer for a slot.")
142 (defconst class-protection
13 "Class protection for a slot.")
143 (defconst class-initarg-tuples
14 "Class initarg tuples list.")
144 (defconst class-class-allocation-a
15 "Class allocated attributes.")
145 (defconst class-class-allocation-doc
16 "Class allocated documentation.")
146 (defconst class-class-allocation-type
17 "Class allocated value type.")
147 (defconst class-class-allocation-custom
18 "Class allocated custom descriptor.")
148 (defconst class-class-allocation-custom-label
19 "Class allocated custom descriptor.")
149 (defconst class-class-allocation-custom-group
20 "Class allocated custom group.")
150 (defconst class-class-allocation-printer
21 "Class allocated printer for a slot.")
151 (defconst class-class-allocation-protection
22 "Class allocated protection list.")
152 (defconst class-class-allocation-values
23 "Class allocated value vector.")
153 (defconst class-default-object-cache
24
154 "Cache index of what a newly created object would look like.
155 This will speed up instantiation time as only a `copy-sequence' will
156 be needed, instead of looping over all the values and setting them
158 (defconst class-options
25
159 "Storage location of tagged class options.
160 Stored outright without modifications or stripping.")
162 (defconst class-num-slots
26
163 "Number of slots in the class definition object.")
165 (defconst object-class
1 "Index in an object vector where the class is stored.")
166 (defconst object-name
2 "Index in an object where the name is stored.")
168 (defconst method-static
0 "Index into :static tag on a method.")
169 (defconst method-before
1 "Index into :before tag on a method.")
170 (defconst method-primary
2 "Index into :primary tag on a method.")
171 (defconst method-after
3 "Index into :after tag on a method.")
172 (defconst method-num-lists
4 "Number of indexes into methods vector in which groups of functions are kept.")
173 (defconst method-generic-before
4 "Index into generic :before tag on a method.")
174 (defconst method-generic-primary
5 "Index into generic :primary tag on a method.")
175 (defconst method-generic-after
6 "Index into generic :after tag on a method.")
176 (defconst method-num-slots
7 "Number of indexes into a method's vector.")
178 (defsubst eieio-specialized-key-to-generic-key
(key)
179 "Convert a specialized KEY into a generic method key."
180 (cond ((eq key method-static
) 0) ;; don't convert
181 ((< key method-num-lists
) (+ key
3)) ;; The conversion
182 (t key
) ;; already generic.. maybe.
186 ;;; Important macros used in eieio.
188 (defmacro class-v
(class)
189 "Internal: Return the class vector from the CLASS symbol."
190 ;; No check: If eieio gets this far, it's probably been checked already.
191 `(get ,class
'eieio-class-definition
))
193 (defmacro class-p
(class)
194 "Return t if CLASS is a valid class vector.
196 ;; this new method is faster since it doesn't waste time checking lots of
199 (eq (aref (class-v ,class
) 0) 'defclass
)
202 (defmacro eieio-object-p
(obj)
203 "Return non-nil if OBJ is an EIEIO object."
206 (and (eq (aref tobj
0) 'object
)
207 (class-p (aref tobj object-class
))))
209 (defalias 'object-p
'eieio-object-p
)
211 (defmacro class-constructor
(class)
212 "Return the symbol representing the constructor of CLASS."
213 `(aref (class-v ,class
) class-symbol
))
215 (defmacro generic-p
(method)
216 "Return t if symbol METHOD is a generic function.
217 Only methods have the symbol `eieio-method-obarray' as a property
218 \(which contains a list of all bindings to that method type.)"
219 `(and (fboundp ,method
) (get ,method
'eieio-method-obarray
)))
221 (defun generic-primary-only-p (method)
222 "Return t if symbol METHOD is a generic function with only primary methods.
223 Only methods have the symbol `eieio-method-obarray' as a property (which
224 contains a list of all bindings to that method type.)
225 Methods with only primary implementations are executed in an optimized way."
226 (and (generic-p method
)
227 (let ((M (get method
'eieio-method-tree
)))
228 (and (< 0 (length (aref M method-primary
)))
229 (not (aref M method-static
))
230 (not (aref M method-before
))
231 (not (aref M method-after
))
232 (not (aref M method-generic-before
))
233 (not (aref M method-generic-primary
))
234 (not (aref M method-generic-after
))))
237 (defun generic-primary-only-one-p (method)
238 "Return t if symbol METHOD is a generic function with only primary methods.
239 Only methods have the symbol `eieio-method-obarray' as a property (which
240 contains a list of all bindings to that method type.)
241 Methods with only primary implementations are executed in an optimized way."
242 (and (generic-p method
)
243 (let ((M (get method
'eieio-method-tree
)))
244 (and (= 1 (length (aref M method-primary
)))
245 (not (aref M method-static
))
246 (not (aref M method-before
))
247 (not (aref M method-after
))
248 (not (aref M method-generic-before
))
249 (not (aref M method-generic-primary
))
250 (not (aref M method-generic-after
))))
253 (defmacro class-option-assoc
(list option
)
254 "Return from LIST the found OPTION, or nil if it doesn't exist."
255 `(car-safe (cdr (memq ,option
,list
))))
257 (defmacro class-option
(class option
)
258 "Return the value stored for CLASS' OPTION.
259 Return nil if that option doesn't exist."
260 `(class-option-assoc (aref (class-v ,class
) class-options
) ',option
))
262 (defmacro class-abstract-p
(class)
263 "Return non-nil if CLASS is abstract.
264 Abstract classes cannot be instantiated."
265 `(class-option ,class
:abstract
))
267 (defmacro class-method-invocation-order
(class)
268 "Return the invocation order of CLASS.
269 Abstract classes cannot be instantiated."
270 `(or (class-option ,class
:method-invocation-order
)
274 ;;; Defining a new class
276 (defmacro defclass
(name superclass slots
&rest options-and-doc
)
277 "Define NAME as a new class derived from SUPERCLASS with SLOTS.
278 OPTIONS-AND-DOC is used as the class' options and base documentation.
279 SUPERCLASS is a list of superclasses to inherit from, with SLOTS
280 being the slots residing in that class definition. NOTE: Currently
281 only one slot may exist in SUPERCLASS as multiple inheritance is not
282 yet supported. Supported tags are:
284 :initform - Initializing form.
285 :initarg - Tag used during initialization.
286 :accessor - Tag used to create a function to access this slot.
287 :allocation - Specify where the value is stored.
288 Defaults to `:instance', but could also be `:class'.
289 :writer - A function symbol which will `write' an object's slot.
290 :reader - A function symbol which will `read' an object.
291 :type - The type of data allowed in this slot (see `typep').
293 - A string documenting use of this slot.
295 The following are extensions on CLOS:
296 :protection - Specify protection for this slot.
297 Defaults to `:public'. Also use `:protected', or `:private'.
298 :custom - When customizing an object, the custom :type. Public only.
299 :label - A text string label used for a slot when customizing.
300 :group - Name of a customization group this slot belongs in.
301 :printer - A function to call to print the value of a slot.
302 See `eieio-override-prin1' as an example.
304 A class can also have optional options. These options happen in place
305 of documentation (including a :documentation tag), in addition to
306 documentation, or not at all. Supported options are:
308 :documentation - The doc-string used for this class.
310 Options added to EIEIO:
312 :allow-nil-initform - Non-nil to skip typechecking of null initforms.
313 :custom-groups - List of custom group names. Organizes slots into
314 reasonable groups for customizations.
315 :abstract - Non-nil to prevent instances of this class.
316 If a string, use as an error string if someone does
317 try to make an instance.
318 :method-invocation-order
319 - Control the method invocation order if there is
320 multiple inheritance. Valid values are:
321 :breadth-first - The default.
324 Options in CLOS not supported in EIEIO:
326 :metaclass - Class to use in place of `standard-class'
327 :default-initargs - Initargs to use when initializing new objects of
330 Due to the way class options are set up, you can add any tags you wish,
331 and reference them using the function `class-option'."
332 ;; We must `eval-and-compile' this so that when we byte compile
333 ;; an eieio program, there is no need to load it ahead of time.
334 ;; It also provides lots of nice debugging errors at compile time.
336 (eieio-defclass ',name
',superclass
',slots
',options-and-doc
)))
338 (defvar eieio-defclass-autoload-map
(make-vector 7 nil
)
339 "Symbol map of superclasses we find in autoloads.")
341 ;; We autoload this because it's used in `make-autoload'.
343 (defun eieio-defclass-autoload (cname superclasses filename doc
)
344 "Create autoload symbols for the EIEIO class CNAME.
345 SUPERCLASSES are the superclasses that CNAME inherits from.
346 DOC is the docstring for CNAME.
347 This function creates a mock-class for CNAME and adds it into
348 SUPERCLASSES as children.
349 It creates an autoload function for CNAME's constructor."
350 ;; Assume we've already debugged inputs.
352 (let* ((oldc (when (class-p cname
) (class-v cname
)))
353 (newc (make-vector class-num-slots nil
))
356 nil
;; Do nothing if we already have this class.
358 ;; Create the class in NEWC, but don't fill anything else in.
359 (aset newc
0 'defclass
)
360 (aset newc class-symbol cname
)
362 (let ((clear-parent nil
))
364 (when (not superclasses
)
365 (setq superclasses
'(eieio-default-superclass)
369 ;; Hook our new class into the existing structures so we can
370 ;; autoload it later.
371 (dolist (SC superclasses
)
374 ;; TODO - If we create an autoload that is in the map, that
375 ;; map needs to be cleared!
378 ;; Does our parent exist?
379 (if (not (class-p SC
))
381 ;; Create a symbol for this parent, and then store this
382 ;; parent on that symbol.
383 (let ((sym (intern (symbol-name SC
) eieio-defclass-autoload-map
)))
384 (if (not (boundp sym
))
385 (set sym
(list cname
))
386 (add-to-list sym cname
))
389 ;; We have a parent, save the child in there.
390 (when (not (member cname
(aref (class-v SC
) class-children
)))
391 (aset (class-v SC
) class-children
392 (cons cname
(aref (class-v SC
) class-children
)))))
394 ;; save parent in child
395 (aset newc class-parent
(cons SC
(aref newc class-parent
)))
398 ;; turn this into a useable self-pointing symbol
401 ;; Store the new class vector definition into the symbol. We need to
402 ;; do this first so that we can call defmethod for the accessor.
403 ;; The vector will be updated by the following while loop and will not
404 ;; need to be stored a second time.
405 (put cname
'eieio-class-definition newc
)
408 (if clear-parent
(aset newc class-parent nil
))
410 ;; Create an autoload on top of our constructor function.
411 (autoload cname filename doc nil nil
)
412 (autoload (intern (concat (symbol-name cname
) "-p")) filename
"" nil nil
)
413 (autoload (intern (concat (symbol-name cname
) "-child-p")) filename
"" nil nil
)
417 (defsubst eieio-class-un-autoload
(cname)
418 "If class CNAME is in an autoload state, load its file."
419 (when (eq (car-safe (symbol-function cname
)) 'autoload
)
420 (load-library (car (cdr (symbol-function cname
))))))
422 (defun eieio-defclass (cname superclasses slots options-and-doc
)
423 ;; FIXME: Most of this should be moved to the `defclass' macro.
424 "Define CNAME as a new subclass of SUPERCLASSES.
425 SLOTS are the slots residing in that class definition, and options or
426 documentation OPTIONS-AND-DOC is the toplevel documentation for this class.
427 See `defclass' for more information."
428 ;; Run our eieio-hook each time, and clear it when we are done.
429 ;; This way people can add hooks safely if they want to modify eieio
430 ;; or add definitions when eieio is loaded or something like that.
431 (run-hooks 'eieio-hook
)
432 (setq eieio-hook nil
)
434 (if (not (symbolp cname
)) (signal 'wrong-type-argument
'(symbolp cname
)))
435 (if (not (listp superclasses
)) (signal 'wrong-type-argument
'(listp superclasses
)))
437 (let* ((pname (if superclasses superclasses nil
))
438 (newc (make-vector class-num-slots nil
))
439 (oldc (when (class-p cname
) (class-v cname
)))
440 (groups nil
) ;; list of groups id'd from slots
444 (aset newc
0 'defclass
)
445 (aset newc class-symbol cname
)
447 ;; If this class already existed, and we are updating its structure,
448 ;; make sure we keep the old child list. This can cause bugs, but
449 ;; if no new slots are created, it also saves time, and prevents
450 ;; method table breakage, particularly when the users is only
451 ;; byte compiling an EIEIO file.
453 (aset newc class-children
(aref oldc class-children
))
454 ;; If the old class did not exist, but did exist in the autoload map, then adopt those children.
455 ;; This is like the above, but deals with autoloads nicely.
456 (let ((sym (intern-soft (symbol-name cname
) eieio-defclass-autoload-map
)))
459 (aset newc class-children
(symbol-value sym
))
461 (unintern (symbol-name cname
) eieio-defclass-autoload-map
)
465 (cond ((and (stringp (car options-and-doc
))
466 (/= 1 (%
(length options-and-doc
) 2)))
467 (error "Too many arguments to `defclass'"))
468 ((and (symbolp (car options-and-doc
))
469 (/= 0 (%
(length options-and-doc
) 2)))
470 (error "Too many arguments to `defclass'"))
474 (if (stringp (car options-and-doc
))
475 (cons :documentation options-and-doc
)
481 (if (and (car pname
) (symbolp (car pname
)))
482 (if (not (class-p (car pname
)))
484 (error "Given parent class %s is not a class" (car pname
))
485 ;; good parent class...
486 ;; save new child in parent
487 (when (not (member cname
(aref (class-v (car pname
)) class-children
)))
488 (aset (class-v (car pname
)) class-children
489 (cons cname
(aref (class-v (car pname
)) class-children
))))
490 ;; Get custom groups, and store them into our local copy.
491 (mapc (lambda (g) (add-to-list 'groups g
))
492 (class-option (car pname
) :custom-groups
))
493 ;; save parent in child
494 (aset newc class-parent
(cons (car pname
) (aref newc class-parent
))))
495 (error "Invalid parent class %s" pname
))
496 (setq pname
(cdr pname
)))
497 ;; Reverse the list of our parents so that they are prioritized in
498 ;; the same order as specified in the code.
499 (aset newc class-parent
(nreverse (aref newc class-parent
))) )
500 ;; If there is nothing to loop over, then inherit from the
501 ;; default superclass.
502 (unless (eq cname
'eieio-default-superclass
)
503 ;; adopt the default parent here, but clear it later...
505 ;; save new child in parent
506 (if (not (member cname
(aref (class-v 'eieio-default-superclass
) class-children
)))
507 (aset (class-v 'eieio-default-superclass
) class-children
508 (cons cname
(aref (class-v 'eieio-default-superclass
) class-children
))))
509 ;; save parent in child
510 (aset newc class-parent
(list eieio-default-superclass
))))
512 ;; turn this into a useable self-pointing symbol
515 ;; These two tests must be created right away so we can have self-
516 ;; referencing classes. ei, a class whose slot can contain only
517 ;; pointers to itself.
519 ;; Create the test function
520 (let ((csym (intern (concat (symbol-name cname
) "-p"))))
522 (list 'lambda
(list 'obj
)
523 (format "Test OBJ to see if it an object of type %s" cname
)
524 (list 'and
'(eieio-object-p obj
)
525 (list 'same-class-p
'obj cname
)))))
527 ;; Make sure the method invocation order is a valid value.
528 (let ((io (class-option-assoc options
:method-invocation-order
)))
529 (when (and io
(not (member io
'(:depth-first
:breadth-first
:c3
))))
530 (error "Method invocation order %s is not allowed" io
)
533 ;; Create a handy child test too
534 (let ((csym (intern (concat (symbol-name cname
) "-child-p"))))
538 "Test OBJ to see if it an object is a child of type %s"
540 (and (eieio-object-p obj
)
541 (object-of-class-p obj
,cname
))))
543 ;; When using typep, (typep OBJ 'myclass) returns t for objects which
544 ;; are subclasses of myclass. For our predicates, however, it is
545 ;; important for EIEIO to be backwards compatible, where
546 ;; myobject-p, and myobject-child-p are different.
547 ;; "cl" uses this technique to specify symbols with specific typep
548 ;; test, so we can let typep have the CLOS documented behavior
549 ;; while keeping our above predicate clean.
551 ;; It would be cleaner to use `defsetf' here, but that requires cl
553 (put cname
'cl-deftype-handler
554 (list 'lambda
() `(list 'satisfies
(quote ,csym
)))))
556 ;; before adding new slots, lets add all the methods and classes
557 ;; in from the parent class
558 (eieio-copy-parents-into-subclass newc superclasses
)
560 ;; Store the new class vector definition into the symbol. We need to
561 ;; do this first so that we can call defmethod for the accessor.
562 ;; The vector will be updated by the following while loop and will not
563 ;; need to be stored a second time.
564 (put cname
'eieio-class-definition newc
)
566 ;; Query each slot in the declaration list and mangle into the
567 ;; class structure I have defined.
569 (let* ((slot1 (car slots
))
572 (acces (plist-get slot
':accessor
))
573 (init (or (plist-get slot
':initform
)
574 (if (member ':initform slot
) nil
576 (initarg (plist-get slot
':initarg
))
577 (docstr (plist-get slot
':documentation
))
578 (prot (plist-get slot
':protection
))
579 (reader (plist-get slot
':reader
))
580 (writer (plist-get slot
':writer
))
581 (alloc (plist-get slot
':allocation
))
582 (type (plist-get slot
':type
))
583 (custom (plist-get slot
':custom
))
584 (label (plist-get slot
':label
))
585 (customg (plist-get slot
':group
))
586 (printer (plist-get slot
':printer
))
588 (skip-nil (class-option-assoc options
:allow-nil-initform
))
591 (if eieio-error-unsupported-class-tags
594 (if (not (member (car tmp
) '(:accessor
609 (signal 'invalid-slot-type
(list (car tmp
))))
610 (setq tmp
(cdr (cdr tmp
))))))
612 ;; Clean up the meaning of protection.
613 (cond ((or (eq prot
'public
) (eq prot
:public
)) (setq prot nil
))
614 ((or (eq prot
'protected
) (eq prot
:protected
)) (setq prot
'protected
))
615 ((or (eq prot
'private
) (eq prot
:private
)) (setq prot
'private
))
617 (t (signal 'invalid-slot-type
(list ':protection prot
))))
619 ;; Make sure the :allocation parameter has a valid value.
620 (if (not (or (not alloc
) (eq alloc
:class
) (eq alloc
:instance
)))
621 (signal 'invalid-slot-type
(list ':allocation alloc
)))
623 ;; The default type specifier is supposed to be t, meaning anything.
624 (if (not type
) (setq type t
))
626 ;; Label is nil, or a string
627 (if (not (or (null label
) (stringp label
)))
628 (signal 'invalid-slot-type
(list ':label label
)))
630 ;; Is there an initarg, but allocation of class?
631 (if (and initarg
(eq alloc
:class
))
632 (message "Class allocated slots do not need :initarg"))
634 ;; intern the symbol so we can use it blankly
635 (if initarg
(set initarg initarg
))
637 ;; The customgroup should be a list of symbols
638 (cond ((null customg
)
639 (setq customg
'(default)))
640 ((not (listp customg
))
641 (setq customg
(list customg
))))
642 ;; The customgroup better be a symbol, or list of symbols.
644 (if (not (symbolp cg
))
645 (signal 'invalid-slot-type
(list ':group cg
))))
648 ;; First up, add this slot into our new class.
649 (eieio-add-new-slot newc name init docstr type custom label customg printer
650 prot initarg alloc
'defaultoverride skip-nil
)
652 ;; We need to id the group, and store them in a group list attribute.
653 (mapc (lambda (cg) (add-to-list 'groups cg
)) customg
)
655 ;; anyone can have an accessor function. This creates a function
656 ;; of the specified name, and also performs a `defsetf' if applicable
657 ;; so that users can `setf' the space returned by this function
661 acces
(if (eq alloc
:class
) :static
:primary
) cname
664 "Retrieves the slot `%s' from an object of class `%s'"
666 (if (slot-boundp this
',name
)
667 (eieio-oref this
',name
)
668 ;; Else - Some error? nil?
671 ;; Provide a setf method. It would be cleaner to use
672 ;; defsetf, but that would require CL at runtime.
673 (put acces
'setf-method
675 (let* ((--widget-sym-- (make-symbol "--widget--"))
676 (--store-sym-- (make-symbol "--store--")))
678 (list --widget-sym--
)
681 (list 'eieio-oset --widget-sym--
'',name --store-sym--
)
682 (list 'getfoo --widget-sym--
)))))))
684 ;; If a writer is defined, then create a generic method of that
685 ;; name whose purpose is to set the value of the slot.
689 `(lambda (this value
)
690 ,(format "Set the slot `%s' of an object of class `%s'"
692 (setf (slot-value this
',name
) value
))))
693 ;; If a reader is defined, then create a generic method
694 ;; of that name whose purpose is to access this slot value.
699 ,(format "Access the slot `%s' from object of class `%s'"
701 (slot-value this
',name
))))
703 (setq slots
(cdr slots
)))
705 ;; Now that everything has been loaded up, all our lists are backwards! Fix that up now.
706 (aset newc class-public-a
(nreverse (aref newc class-public-a
)))
707 (aset newc class-public-d
(nreverse (aref newc class-public-d
)))
708 (aset newc class-public-doc
(nreverse (aref newc class-public-doc
)))
709 (aset newc class-public-type
710 (apply 'vector
(nreverse (aref newc class-public-type
))))
711 (aset newc class-public-custom
(nreverse (aref newc class-public-custom
)))
712 (aset newc class-public-custom-label
(nreverse (aref newc class-public-custom-label
)))
713 (aset newc class-public-custom-group
(nreverse (aref newc class-public-custom-group
)))
714 (aset newc class-public-printer
(nreverse (aref newc class-public-printer
)))
715 (aset newc class-protection
(nreverse (aref newc class-protection
)))
716 (aset newc class-initarg-tuples
(nreverse (aref newc class-initarg-tuples
)))
718 ;; The storage for class-class-allocation-type needs to be turned into
720 (aset newc class-class-allocation-type
721 (apply 'vector
(aref newc class-class-allocation-type
)))
723 ;; Also, take class allocated values, and vectorize them for speed.
724 (aset newc class-class-allocation-values
725 (apply 'vector
(aref newc class-class-allocation-values
)))
727 ;; Attach slot symbols into an obarray, and store the index of
728 ;; this slot as the variable slot in this new symbol. We need to
729 ;; know about primes, because obarrays are best set in vectors of
730 ;; prime number length, and we also need to make our vector small
731 ;; to save space, and also optimal for the number of items we have.
733 (pubsyms (aref newc class-public-a
))
734 (prots (aref newc class-protection
))
736 (vl (let ((primes '( 3 5 7 11 13 17 19 23 29 31 37 41 43 47
737 53 59 61 67 71 73 79 83 89 97 101 )))
738 (while (and primes
(< (car primes
) l
))
739 (setq primes
(cdr primes
)))
741 (oa (make-vector vl
0))
744 (setq newsym
(intern (symbol-name (car pubsyms
)) oa
))
747 (if (car prots
) (put newsym
'protection
(car prots
)))
748 (setq pubsyms
(cdr pubsyms
)
750 (aset newc class-symbol-obarray oa
)
753 ;; Create the constructor function
754 (if (class-option-assoc options
:abstract
)
755 ;; Abstract classes cannot be instantiated. Say so.
756 (let ((abs (class-option-assoc options
:abstract
)))
757 (if (not (stringp abs
))
758 (setq abs
(format "Class %s is abstract" cname
)))
760 `(lambda (&rest stuff
)
761 ,(format "You cannot create a new object of type %s" cname
)
764 ;; Non-abstract classes need a constructor.
766 `(lambda (newname &rest slots
)
767 ,(format "Create a new object with name NAME of class type %s" cname
)
768 (apply 'constructor
,cname newname slots
)))
771 ;; Set up a specialized doc string.
772 ;; Use stored value since it is calculated in a non-trivial way
773 (put cname
'variable-documentation
774 (class-option-assoc options
:documentation
))
776 ;; We have a list of custom groups. Store them into the options.
777 (let ((g (class-option-assoc options
:custom-groups
)))
778 (mapc (lambda (cg) (add-to-list 'g cg
)) groups
)
779 (if (memq :custom-groups options
)
780 (setcar (cdr (memq :custom-groups options
)) g
)
781 (setq options
(cons :custom-groups
(cons g options
)))))
783 ;; Set up the options we have collected.
784 (aset newc class-options options
)
786 ;; if this is a superclass, clear out parent (which was set to the
787 ;; default superclass eieio-default-superclass)
788 (if clearparent
(aset newc class-parent nil
))
790 ;; Create the cached default object.
791 (let ((cache (make-vector (+ (length (aref newc class-public-a
))
793 (aset cache
0 'object
)
794 (aset cache object-class cname
)
795 (aset cache object-name
'default-cache-object
)
796 (let ((eieio-skip-typecheck t
))
797 ;; All type-checking has been done to our satisfaction
798 ;; before this call. Don't waste our time in this call..
799 (eieio-set-defaults cache t
))
800 (aset newc class-default-object-cache cache
))
802 ;; Return our new class object
807 (defun eieio-perform-slot-validation-for-default (slot spec value skipnil
)
808 "For SLOT, signal if SPEC does not match VALUE.
809 If SKIPNIL is non-nil, then if VALUE is nil return t instead."
810 (if (and (not (eieio-eval-default-p value
))
811 (not eieio-skip-typecheck
)
812 (not (and skipnil
(null value
)))
813 (not (eieio-perform-slot-validation spec value
)))
814 (signal 'invalid-slot-type
(list slot spec value
))))
816 (defun eieio-add-new-slot (newc a d doc type cust label custg print prot init alloc
817 &optional defaultoverride skipnil
)
818 "Add into NEWC attribute A.
819 If A already exists in NEWC, then do nothing. If it doesn't exist,
820 then also add in D (default), DOC, TYPE, CUST, LABEL, CUSTG, PRINT, PROT, and INIT arg.
821 Argument ALLOC specifies if the slot is allocated per instance, or per class.
822 If optional DEFAULTOVERRIDE is non-nil, then if A exists in NEWC,
823 we must override its value for a default.
824 Optional argument SKIPNIL indicates if type checking should be skipped
825 if default value is nil."
826 ;; Make sure we duplicate those items that are sequences.
828 (if (sequencep d
) (setq d
(copy-sequence d
)))
829 ;; This copy can fail on a cons cell with a non-cons in the cdr. Lets skip it if it doesn't work.
831 (if (sequencep type
) (setq type
(copy-sequence type
)))
832 (if (sequencep cust
) (setq cust
(copy-sequence cust
)))
833 (if (sequencep custg
) (setq custg
(copy-sequence custg
)))
835 ;; To prevent override information w/out specification of storage,
836 ;; we need to do this little hack.
837 (if (member a
(aref newc class-class-allocation-a
)) (setq alloc
':class
))
839 (if (or (not alloc
) (and (symbolp alloc
) (eq alloc
':instance
)))
840 ;; In this case, we modify the INSTANCE version of a given slot.
844 ;; Only add this element if it is so-far unique
845 (if (not (member a
(aref newc class-public-a
)))
847 (eieio-perform-slot-validation-for-default a type d skipnil
)
848 (aset newc class-public-a
(cons a
(aref newc class-public-a
)))
849 (aset newc class-public-d
(cons d
(aref newc class-public-d
)))
850 (aset newc class-public-doc
(cons doc
(aref newc class-public-doc
)))
851 (aset newc class-public-type
(cons type
(aref newc class-public-type
)))
852 (aset newc class-public-custom
(cons cust
(aref newc class-public-custom
)))
853 (aset newc class-public-custom-label
(cons label
(aref newc class-public-custom-label
)))
854 (aset newc class-public-custom-group
(cons custg
(aref newc class-public-custom-group
)))
855 (aset newc class-public-printer
(cons print
(aref newc class-public-printer
)))
856 (aset newc class-protection
(cons prot
(aref newc class-protection
)))
857 (aset newc class-initarg-tuples
(cons (cons init a
) (aref newc class-initarg-tuples
)))
859 ;; When defaultoverride is true, we are usually adding new local
860 ;; attributes which must override the default value of any slot
861 ;; passed in by one of the parent classes.
862 (when defaultoverride
863 ;; There is a match, and we must override the old value.
864 (let* ((ca (aref newc class-public-a
))
866 (num (- (length ca
) (length np
)))
867 (dp (if np
(nthcdr num
(aref newc class-public-d
))
869 (tp (if np
(nth num
(aref newc class-public-type
))))
872 (error "EIEIO internal error overriding default value for %s"
874 ;; If type is passed in, is it the same?
875 (if (not (eq type t
))
876 (if (not (equal type tp
))
878 "Child slot type `%s' does not match inherited type `%s' for `%s'"
880 ;; If we have a repeat, only update the initarg...
881 (unless (eq d eieio-unbound
)
882 (eieio-perform-slot-validation-for-default a tp d skipnil
)
884 ;; If we have a new initarg, check for it.
886 (let* ((inits (aref newc class-initarg-tuples
))
887 (inita (rassq a inits
)))
888 ;; Replace the CAR of the associate INITA.
889 ;;(message "Initarg: %S replace %s" inita init)
893 ;; PLN Tue Jun 26 11:57:06 2007 : The protection is
894 ;; checked and SHOULD match the superclass
895 ;; protection. Otherwise an error is thrown. However
896 ;; I wonder if a more flexible schedule might be
899 ;; EML - We used to have (if prot... here,
900 ;; but a prot of 'nil means public.
902 (let ((super-prot (nth num
(aref newc class-protection
)))
904 (if (not (eq prot super-prot
))
905 (error "Child slot protection `%s' does not match inherited protection `%s' for `%s'"
909 ;; PLN Tue Jun 26 11:57:06 2007 :
910 ;; Do a non redundant combination of ancient custom
911 ;; groups and new ones.
914 (nthcdr num
(aref newc class-public-custom-group
)))
916 (list2 (if (listp custg
) custg
(list custg
))))
917 (if (< (length list1
) (length list2
))
918 (setq list1
(prog1 list2
(setq list2 list1
))))
920 (unless (memq elt list1
)
922 (setcar groups list1
)))
925 ;; PLN Mon Jun 25 22:44:34 2007 : If a new cust is
926 ;; set, simply replaces the old one.
928 ;; (message "Custom type redefined to %s" cust)
929 (setcar (nthcdr num
(aref newc class-public-custom
)) cust
))
931 ;; If a new label is specified, it simply replaces
934 ;; (message "Custom label redefined to %s" label)
935 (setcar (nthcdr num
(aref newc class-public-custom-label
)) label
))
938 ;; PLN Sat Jun 30 17:24:42 2007 : when a new
939 ;; doc is specified, simply replaces the old one.
941 ;;(message "Documentation redefined to %s" doc)
942 (setcar (nthcdr num
(aref newc class-public-doc
))
946 ;; If a new printer is specified, it simply replaces
949 ;; (message "printer redefined to %s" print)
950 (setcar (nthcdr num
(aref newc class-public-printer
)) print
))
955 ;; CLASS ALLOCATED SLOTS
956 (let ((value (eieio-default-eval-maybe d
)))
957 (if (not (member a
(aref newc class-class-allocation-a
)))
959 (eieio-perform-slot-validation-for-default a type value skipnil
)
960 ;; Here we have found a :class version of a slot. This
961 ;; requires a very different aproach.
962 (aset newc class-class-allocation-a
(cons a
(aref newc class-class-allocation-a
)))
963 (aset newc class-class-allocation-doc
(cons doc
(aref newc class-class-allocation-doc
)))
964 (aset newc class-class-allocation-type
(cons type
(aref newc class-class-allocation-type
)))
965 (aset newc class-class-allocation-custom
(cons cust
(aref newc class-class-allocation-custom
)))
966 (aset newc class-class-allocation-custom-label
(cons label
(aref newc class-class-allocation-custom-label
)))
967 (aset newc class-class-allocation-custom-group
(cons custg
(aref newc class-class-allocation-custom-group
)))
968 (aset newc class-class-allocation-protection
(cons prot
(aref newc class-class-allocation-protection
)))
969 ;; Default value is stored in the 'values section, since new objects
970 ;; can't initialize from this element.
971 (aset newc class-class-allocation-values
(cons value
(aref newc class-class-allocation-values
))))
972 (when defaultoverride
973 ;; There is a match, and we must override the old value.
974 (let* ((ca (aref newc class-class-allocation-a
))
976 (num (- (length ca
) (length np
)))
979 (aref newc class-class-allocation-values
))
981 (tp (if np
(nth num
(aref newc class-class-allocation-type
))
984 (error "EIEIO internal error overriding default value for %s"
986 ;; If type is passed in, is it the same?
987 (if (not (eq type t
))
988 (if (not (equal type tp
))
990 "Child slot type `%s' does not match inherited type `%s' for `%s'"
992 ;; EML - Note: the only reason to override a class bound slot
993 ;; is to change the default, so allow unbound in.
995 ;; If we have a repeat, only update the vlaue...
996 (eieio-perform-slot-validation-for-default a tp value skipnil
)
999 ;; PLN Tue Jun 26 11:57:06 2007 : The protection is
1000 ;; checked and SHOULD match the superclass
1001 ;; protection. Otherwise an error is thrown. However
1002 ;; I wonder if a more flexible schedule might be
1005 (car (nthcdr num
(aref newc class-class-allocation-protection
)))))
1006 (if (not (eq prot super-prot
))
1007 (error "Child slot protection `%s' does not match inherited protection `%s' for `%s'"
1008 prot super-prot a
)))
1009 ;; Do a non redundant combination of ancient custom groups
1013 (nthcdr num
(aref newc class-class-allocation-custom-group
)))
1014 (list1 (car groups
))
1015 (list2 (if (listp custg
) custg
(list custg
))))
1016 (if (< (length list1
) (length list2
))
1017 (setq list1
(prog1 list2
(setq list2 list1
))))
1019 (unless (memq elt list1
)
1021 (setcar groups list1
)))
1023 ;; PLN Sat Jun 30 17:24:42 2007 : when a new
1024 ;; doc is specified, simply replaces the old one.
1026 ;;(message "Documentation redefined to %s" doc)
1027 (setcar (nthcdr num
(aref newc class-class-allocation-doc
))
1031 ;; If a new printer is specified, it simply replaces
1034 ;; (message "printer redefined to %s" print)
1035 (setcar (nthcdr num
(aref newc class-class-allocation-printer
)) print
))
1041 (defun eieio-copy-parents-into-subclass (newc parents
)
1042 "Copy into NEWC the slots of PARENTS.
1043 Follow the rules of not overwriting early parents when applying to
1044 the new child class."
1045 (let ((ps (aref newc class-parent
))
1046 (sn (class-option-assoc (aref newc class-options
)
1047 ':allow-nil-initform
)))
1049 ;; First, duplicate all the slots of the parent.
1050 (let ((pcv (class-v (car ps
))))
1051 (let ((pa (aref pcv class-public-a
))
1052 (pd (aref pcv class-public-d
))
1053 (pdoc (aref pcv class-public-doc
))
1054 (ptype (aref pcv class-public-type
))
1055 (pcust (aref pcv class-public-custom
))
1056 (plabel (aref pcv class-public-custom-label
))
1057 (pcustg (aref pcv class-public-custom-group
))
1058 (printer (aref pcv class-public-printer
))
1059 (pprot (aref pcv class-protection
))
1060 (pinit (aref pcv class-initarg-tuples
))
1063 (eieio-add-new-slot newc
1064 (car pa
) (car pd
) (car pdoc
) (aref ptype i
)
1065 (car pcust
) (car plabel
) (car pcustg
)
1067 (car pprot
) (car-safe (car pinit
)) nil nil sn
)
1068 ;; Increment each value.
1076 printer
(cdr printer
)
1080 ;; Now duplicate all the class alloc slots.
1081 (let ((pa (aref pcv class-class-allocation-a
))
1082 (pdoc (aref pcv class-class-allocation-doc
))
1083 (ptype (aref pcv class-class-allocation-type
))
1084 (pcust (aref pcv class-class-allocation-custom
))
1085 (plabel (aref pcv class-class-allocation-custom-label
))
1086 (pcustg (aref pcv class-class-allocation-custom-group
))
1087 (printer (aref pcv class-class-allocation-printer
))
1088 (pprot (aref pcv class-class-allocation-protection
))
1089 (pval (aref pcv class-class-allocation-values
))
1092 (eieio-add-new-slot newc
1093 (car pa
) (aref pval i
) (car pdoc
) (aref ptype i
)
1094 (car pcust
) (car plabel
) (car pcustg
)
1096 (car pprot
) nil
':class sn
)
1097 ;; Increment each value.
1103 printer
(cdr printer
)
1107 ;; Loop over each parent class
1111 ;;; CLOS style implementation of object creators.
1113 (defun make-instance (class &rest initargs
)
1114 "Make a new instance of CLASS based on INITARGS.
1115 CLASS is a class symbol. For example:
1117 (make-instance 'foo)
1119 INITARGS is a property list with keywords based on the :initarg
1120 for each slot. For example:
1122 (make-instance 'foo :slot1 value1 :slotN valueN)
1126 If the first element of INITARGS is a string, it is used as the
1129 In EIEIO, the class' constructor requires a name for use when printing.
1130 `make-instance' in CLOS doesn't use names the way Emacs does, so the
1131 class is used as the name slot instead when INITARGS doesn't start with
1133 (if (and (car initargs
) (stringp (car initargs
)))
1134 (apply (class-constructor class
) initargs
)
1135 (apply (class-constructor class
)
1136 (cond ((symbolp class
) (symbol-name class
))
1137 (t (format "%S" class
)))
1141 ;;; CLOS methods and generics
1144 (put 'eieio--defalias
'byte-hunk-handler
1145 #'byte-compile-file-form-defalias
) ;;(get 'defalias 'byte-hunk-handler)
1146 (defun eieio--defalias (name body
)
1147 "Like `defalias', but with less side-effects.
1148 More specifically, it has no side-effects at all when the new function
1149 definition is the same (`eq') as the old one."
1150 (unless (and (fboundp name
)
1151 (eq (symbol-function name
) body
))
1152 (defalias name body
)))
1154 (defmacro defgeneric
(method args
&optional doc-string
)
1155 "Create a generic function METHOD.
1156 DOC-STRING is the base documentation for this class. A generic
1157 function has no body, as its purpose is to decide which method body
1158 is appropriate to use. Uses `defmethod' to create methods, and calls
1159 `defgeneric' for you. With this implementation the ARGS are
1160 currently ignored. You can use `defgeneric' to apply specialized
1161 top level documentation to a method."
1162 `(eieio--defalias ',method
1163 (eieio--defgeneric-init-form ',method
,doc-string
)))
1165 (defun eieio--defgeneric-init-form (method doc-string
)
1166 "Form to use for the initial definition of a generic."
1168 ((or (not (fboundp method
))
1169 (eq 'autoload
(car-safe (symbol-function method
))))
1170 ;; Make sure the method tables are installed.
1171 (eieiomt-install method
)
1172 ;; Construct the actual body of this function.
1173 (eieio-defgeneric-form method doc-string
))
1174 ((generic-p method
) (symbol-function method
)) ;Leave it as-is.
1175 (t (error "You cannot create a generic/method over an existing symbol: %s"
1178 (defun eieio-defgeneric-form (method doc-string
)
1179 "The lambda form that would be used as the function defined on METHOD.
1180 All methods should call the same EIEIO function for dispatch.
1181 DOC-STRING is the documentation attached to METHOD."
1182 `(lambda (&rest local-args
)
1184 (eieio-generic-call (quote ,method
) local-args
)))
1186 (defsubst eieio-defgeneric-reset-generic-form
(method)
1187 "Setup METHOD to call the generic form."
1188 (let ((doc-string (documentation method
)))
1189 (fset method
(eieio-defgeneric-form method doc-string
))))
1191 (defun eieio-defgeneric-form-primary-only (method doc-string
)
1192 "The lambda form that would be used as the function defined on METHOD.
1193 All methods should call the same EIEIO function for dispatch.
1194 DOC-STRING is the documentation attached to METHOD."
1195 `(lambda (&rest local-args
)
1197 (eieio-generic-call-primary-only (quote ,method
) local-args
)))
1199 (defsubst eieio-defgeneric-reset-generic-form-primary-only
(method)
1200 "Setup METHOD to call the generic form."
1201 (let ((doc-string (documentation method
)))
1202 (fset method
(eieio-defgeneric-form-primary-only method doc-string
))))
1204 (defun eieio-defgeneric-form-primary-only-one (method doc-string
1208 "The lambda form that would be used as the function defined on METHOD.
1209 All methods should call the same EIEIO function for dispatch.
1210 DOC-STRING is the documentation attached to METHOD.
1211 CLASS is the class symbol needed for private method access.
1212 IMPL is the symbol holding the method implementation."
1213 ;; NOTE: I tried out byte compiling this little fcn. Turns out it
1214 ;; is faster to execute this for not byte-compiled. ie, install this,
1215 ;; then measure calls going through here. I wonder why.
1217 (let ((byte-compile-warnings nil
))
1219 `(lambda (&rest local-args
)
1221 ;; This is a cool cheat. Usually we need to look up in the
1222 ;; method table to find out if there is a method or not. We can
1223 ;; instead make that determination at load time when there is
1224 ;; only one method. If the first arg is not a child of the class
1225 ;; of that one implementation, then clearly, there is no method def.
1226 (if (not (eieio-object-p (car local-args
)))
1227 ;; Not an object. Just signal.
1228 (signal 'no-method-definition
1229 (list ',method local-args
))
1231 ;; We do have an object. Make sure it is the right type.
1232 (if ,(if (eq class eieio-default-superclass
)
1233 nil
; default superclass means just an obj. Already asked.
1234 `(not (child-of-class-p (aref (car local-args
) object-class
)
1237 ;; If not the right kind of object, call no applicable
1238 (apply 'no-applicable-method
(car local-args
)
1239 ',method local-args
)
1241 ;; It is ok, do the call.
1242 ;; Fill in inter-call variables then evaluate the method.
1243 (let ((scoped-class ',class
)
1244 (eieio-generic-call-next-method-list nil
)
1245 (eieio-generic-call-key method-primary
)
1246 (eieio-generic-call-methodname ',method
)
1247 (eieio-generic-call-arglst local-args
)
1249 (apply #',impl local-args
)
1250 ;;(,impl local-args)
1253 (defsubst eieio-defgeneric-reset-generic-form-primary-only-one
(method)
1254 "Setup METHOD to call the generic form."
1255 (let* ((doc-string (documentation method
))
1256 (M (get method
'eieio-method-tree
))
1257 (entry (car (aref M method-primary
)))
1259 (fset method
(eieio-defgeneric-form-primary-only-one
1265 (defun eieio-unbind-method-implementations (method)
1266 "Make the generic method METHOD have no implementations.
1267 It will leave the original generic function in place,
1268 but remove reference to all implementations of METHOD."
1269 (put method
'eieio-method-tree nil
)
1270 (put method
'eieio-method-obarray nil
))
1272 (defmacro defmethod
(method &rest args
)
1273 "Create a new METHOD through `defgeneric' with ARGS.
1275 The optional second argument KEY is a specifier that
1276 modifies how the method is called, including:
1277 :before - Method will be called before the :primary
1278 :primary - The default if not specified
1279 :after - Method will be called after the :primary
1280 :static - First arg could be an object or class
1281 The next argument is the ARGLIST. The ARGLIST specifies the arguments
1282 to the method as with `defun'. The first argument can have a type
1284 ((VARNAME CLASS) ARG2 ...)
1285 where VARNAME is the name of the local variable for the method being
1286 created. The CLASS is a class symbol for a class made with `defclass'.
1287 A DOCSTRING comes after the ARGLIST, and is optional.
1288 All the rest of the args are the BODY of the method. A method will
1289 return the value of the last form in the BODY.
1293 (defmethod mymethod [:before | :primary | :after | :static]
1294 ((typearg class-name) arg2 &optional opt &rest rest)
1297 (let* ((key (if (keywordp (car args
)) (pop args
)))
1300 (fargs (if (consp arg1
)
1301 (cons (car arg1
) (cdr params
))
1303 (class (if (consp arg1
) (nth 1 arg1
)))
1304 (code `(lambda ,fargs
,@(cdr args
))))
1306 ;; Make sure there is a generic and the byte-compiler sees it.
1307 (defgeneric ,method
,args
1308 ,(or (documentation code
)
1309 (format "Generically created method `%s'." method
)))
1310 (eieio--defmethod ',method
',key
',class
#',code
))))
1312 (defun eieio--defmethod (method kind argclass code
)
1313 "Work part of the `defmethod' macro defining METHOD with ARGS."
1315 ;; find optional keys
1316 (cond ((or (eq ':BEFORE kind
)
1319 ((or (eq ':AFTER kind
)
1322 ((or (eq ':PRIMARY kind
)
1323 (eq ':primary kind
))
1325 ((or (eq ':STATIC kind
)
1329 (t method-primary
))))
1330 ;; Make sure there is a generic (when called from defclass).
1332 method
(eieio--defgeneric-init-form
1333 method
(or (documentation code
)
1334 (format "Generically created method `%s'." method
))))
1335 ;; create symbol for property to bind to. If the first arg is of
1336 ;; the form (varname vartype) and `vartype' is a class, then
1337 ;; that class will be the type symbol. If not, then it will fall
1338 ;; under the type `primary' which is a non-specific calling of the
1341 (if (not (class-p argclass
))
1342 (error "Unknown class type %s in method parameters"
1345 (signal 'wrong-type-argument
(list :static
'non-class-arg
)))
1346 ;; generics are higher
1347 (setq key
(eieio-specialized-key-to-generic-key key
)))
1348 ;; Put this lambda into the symbol so we can find it
1349 (eieiomt-add method code key argclass
)
1352 (when eieio-optimize-primary-methods-flag
1355 ;; If this method, after this setup, only has primary methods, then
1356 ;; we can setup the generic that way.
1357 (if (generic-primary-only-p method
)
1358 ;; If there is only one primary method, then we can go one more
1359 ;; optimization step.
1360 (if (generic-primary-only-one-p method
)
1361 (eieio-defgeneric-reset-generic-form-primary-only-one method
)
1362 (eieio-defgeneric-reset-generic-form-primary-only method
))
1363 (eieio-defgeneric-reset-generic-form method
)))
1367 ;;; Slot type validation
1369 ;; This is a hideous hack for replacing `typep' from cl-macs, to avoid
1370 ;; requiring the CL library at run-time. It can be eliminated if/when
1371 ;; `typep' is merged into Emacs core.
1372 (defun eieio--typep (val type
)
1374 (cond ((get type
'cl-deftype-handler
)
1375 (eieio--typep val
(funcall (get type
'cl-deftype-handler
))))
1377 ((eq type
'null
) (null val
))
1378 ((eq type
'atom
) (atom val
))
1379 ((eq type
'float
) (and (numberp val
) (not (integerp val
))))
1380 ((eq type
'real
) (numberp val
))
1381 ((eq type
'fixnum
) (integerp val
))
1382 ((memq type
'(character string-char
)) (characterp val
))
1384 (let* ((name (symbol-name type
))
1385 (namep (intern (concat name
"p"))))
1387 (funcall `(lambda () (,namep val
)))
1388 (funcall `(lambda ()
1389 (,(intern (concat name
"-p")) val
)))))))
1390 (cond ((get (car type
) 'cl-deftype-handler
)
1391 (eieio--typep val
(apply (get (car type
) 'cl-deftype-handler
)
1393 ((memq (car type
) '(integer float real number
))
1394 (and (eieio--typep val
(car type
))
1395 (or (memq (cadr type
) '(* nil
))
1396 (if (consp (cadr type
))
1397 (> val
(car (cadr type
)))
1398 (>= val
(cadr type
))))
1399 (or (memq (caddr type
) '(* nil
))
1400 (if (consp (car (cddr type
)))
1401 (< val
(caar (cddr type
)))
1402 (<= val
(car (cddr type
)))))))
1403 ((memq (car type
) '(and or not
))
1404 (eval (cons (car type
)
1406 `(eieio--typep (quote ,val
) (quote ,x
)))
1408 ((memq (car type
) '(member member
*))
1409 (memql val
(cdr type
)))
1410 ((eq (car type
) 'satisfies
)
1411 (funcall `(lambda () (,(cadr type
) val
))))
1412 (t (error "Bad type spec: %s" type
)))))
1414 (defun eieio-perform-slot-validation (spec value
)
1415 "Return non-nil if SPEC does not match VALUE."
1416 (or (eq spec t
) ; t always passes
1417 (eq value eieio-unbound
) ; unbound always passes
1418 (eieio--typep value spec
)))
1420 (defun eieio-validate-slot-value (class slot-idx value slot
)
1421 "Make sure that for CLASS referencing SLOT-IDX, VALUE is valid.
1422 Checks the :type specifier.
1423 SLOT is the slot that is being checked, and is only used when throwing
1425 (if eieio-skip-typecheck
1427 ;; Trim off object IDX junk added in for the object index.
1428 (setq slot-idx
(- slot-idx
3))
1429 (let ((st (aref (aref (class-v class
) class-public-type
) slot-idx
)))
1430 (if (not (eieio-perform-slot-validation st value
))
1431 (signal 'invalid-slot-type
(list class slot st value
))))))
1433 (defun eieio-validate-class-slot-value (class slot-idx value slot
)
1434 "Make sure that for CLASS referencing SLOT-IDX, VALUE is valid.
1435 Checks the :type specifier.
1436 SLOT is the slot that is being checked, and is only used when throwing
1438 (if eieio-skip-typecheck
1440 (let ((st (aref (aref (class-v class
) class-class-allocation-type
)
1442 (if (not (eieio-perform-slot-validation st value
))
1443 (signal 'invalid-slot-type
(list class slot st value
))))))
1445 (defun eieio-barf-if-slot-unbound (value instance slotname fn
)
1446 "Throw a signal if VALUE is a representation of an UNBOUND slot.
1447 INSTANCE is the object being referenced. SLOTNAME is the offending
1448 slot. If the slot is ok, return VALUE.
1449 Argument FN is the function calling this verifier."
1450 (if (and (eq value eieio-unbound
) (not eieio-skip-typecheck
))
1451 (slot-unbound instance
(object-class instance
) slotname fn
)
1454 ;;; Get/Set slots in an object.
1456 (defmacro oref
(obj slot
)
1457 "Retrieve the value stored in OBJ in the slot named by SLOT.
1458 Slot is the name of the slot when created by `defclass' or the label
1459 created by the :initarg tag."
1460 `(eieio-oref ,obj
(quote ,slot
)))
1462 (defun eieio-oref (obj slot
)
1463 "Return the value in OBJ at SLOT in the object vector."
1464 (if (not (or (eieio-object-p obj
) (class-p obj
)))
1465 (signal 'wrong-type-argument
(list '(or eieio-object-p class-p
) obj
)))
1466 (if (not (symbolp slot
))
1467 (signal 'wrong-type-argument
(list 'symbolp slot
)))
1468 (if (class-p obj
) (eieio-class-un-autoload obj
))
1469 (let* ((class (if (class-p obj
) obj
(aref obj object-class
)))
1470 (c (eieio-slot-name-index class obj slot
)))
1472 ;; It might be missing because it is a :class allocated slot.
1473 ;; Lets check that info out.
1474 (if (setq c
(eieio-class-slot-name-index class slot
))
1476 (aref (aref (class-v class
) class-class-allocation-values
) c
)
1477 ;; The slot-missing method is a cool way of allowing an object author
1478 ;; to intercept missing slot definitions. Since it is also the LAST
1479 ;; thing called in this fn, its return value would be retrieved.
1480 (slot-missing obj slot
'oref
)
1481 ;;(signal 'invalid-slot-name (list (object-name obj) slot))
1483 (if (not (eieio-object-p obj
))
1484 (signal 'wrong-type-argument
(list 'eieio-object-p obj
)))
1485 (eieio-barf-if-slot-unbound (aref obj c
) obj slot
'oref
))))
1487 (defalias 'slot-value
'eieio-oref
)
1488 (defalias 'set-slot-value
'eieio-oset
)
1490 (defmacro oref-default
(obj slot
)
1491 "Get the default value of OBJ (maybe a class) for SLOT.
1492 The default value is the value installed in a class with the :initform
1493 tag. SLOT can be the slot name, or the tag specified by the :initarg
1494 tag in the `defclass' call."
1495 `(eieio-oref-default ,obj
(quote ,slot
)))
1497 (defun eieio-oref-default (obj slot
)
1498 "Do the work for the macro `oref-default' with similar parameters.
1499 Fills in OBJ's SLOT with its default value."
1500 (if (not (or (eieio-object-p obj
) (class-p obj
))) (signal 'wrong-type-argument
(list 'eieio-object-p obj
)))
1501 (if (not (symbolp slot
)) (signal 'wrong-type-argument
(list 'symbolp slot
)))
1502 (let* ((cl (if (eieio-object-p obj
) (aref obj object-class
) obj
))
1503 (c (eieio-slot-name-index cl obj slot
)))
1505 ;; It might be missing because it is a :class allocated slot.
1506 ;; Lets check that info out.
1508 (eieio-class-slot-name-index cl slot
))
1510 (aref (aref (class-v cl
) class-class-allocation-values
)
1512 (slot-missing obj slot
'oref-default
)
1513 ;;(signal 'invalid-slot-name (list (class-name cl) slot))
1515 (eieio-barf-if-slot-unbound
1516 (let ((val (nth (- c
3) (aref (class-v cl
) class-public-d
))))
1517 (eieio-default-eval-maybe val
))
1518 obj cl
'oref-default
))))
1520 (defsubst eieio-eval-default-p
(val)
1521 "Whether the default value VAL should be evaluated for use."
1522 (and (consp val
) (symbolp (car val
)) (fboundp (car val
))))
1524 (defun eieio-default-eval-maybe (val)
1525 "Check VAL, and return what `oref-default' would provide."
1527 ;; Is it a function call? If so, evaluate it.
1528 ((eieio-eval-default-p val
)
1530 ;;;; check for quoted things, and unquote them
1531 ;;((and (consp val) (eq (car val) 'quote))
1533 ;; return it verbatim
1536 ;;; Object Set macros
1538 (defmacro oset
(obj slot value
)
1539 "Set the value in OBJ for slot SLOT to VALUE.
1540 SLOT is the slot name as specified in `defclass' or the tag created
1541 with in the :initarg slot. VALUE can be any Lisp object."
1542 `(eieio-oset ,obj
(quote ,slot
) ,value
))
1544 (defun eieio-oset (obj slot value
)
1545 "Do the work for the macro `oset'.
1546 Fills in OBJ's SLOT with VALUE."
1547 (if (not (eieio-object-p obj
)) (signal 'wrong-type-argument
(list 'eieio-object-p obj
)))
1548 (if (not (symbolp slot
)) (signal 'wrong-type-argument
(list 'symbolp slot
)))
1549 (let ((c (eieio-slot-name-index (object-class-fast obj
) obj slot
)))
1551 ;; It might be missing because it is a :class allocated slot.
1552 ;; Lets check that info out.
1554 (eieio-class-slot-name-index (aref obj object-class
) slot
))
1557 (eieio-validate-class-slot-value (object-class-fast obj
) c value slot
)
1558 (aset (aref (class-v (aref obj object-class
))
1559 class-class-allocation-values
)
1561 ;; See oref for comment on `slot-missing'
1562 (slot-missing obj slot
'oset value
)
1563 ;;(signal 'invalid-slot-name (list (object-name obj) slot))
1565 (eieio-validate-slot-value (object-class-fast obj
) c value slot
)
1566 (aset obj c value
))))
1568 (defmacro oset-default
(class slot value
)
1569 "Set the default slot in CLASS for SLOT to VALUE.
1570 The default value is usually set with the :initform tag during class
1571 creation. This allows users to change the default behavior of classes
1572 after they are created."
1573 `(eieio-oset-default ,class
(quote ,slot
) ,value
))
1575 (defun eieio-oset-default (class slot value
)
1576 "Do the work for the macro `oset-default'.
1577 Fills in the default value in CLASS' in SLOT with VALUE."
1578 (if (not (class-p class
)) (signal 'wrong-type-argument
(list 'class-p class
)))
1579 (if (not (symbolp slot
)) (signal 'wrong-type-argument
(list 'symbolp slot
)))
1580 (let* ((scoped-class class
)
1581 (c (eieio-slot-name-index class nil slot
)))
1583 ;; It might be missing because it is a :class allocated slot.
1584 ;; Lets check that info out.
1585 (if (setq c
(eieio-class-slot-name-index class slot
))
1588 (eieio-validate-class-slot-value class c value slot
)
1589 (aset (aref (class-v class
) class-class-allocation-values
) c
1591 (signal 'invalid-slot-name
(list (class-name class
) slot
)))
1592 (eieio-validate-slot-value class c value slot
)
1593 ;; Set this into the storage for defaults.
1594 (setcar (nthcdr (- c
3) (aref (class-v class
) class-public-d
))
1596 ;; Take the value, and put it into our cache object.
1597 (eieio-oset (aref (class-v class
) class-default-object-cache
)
1601 ;;; Handy CLOS macros
1603 (defmacro with-slots
(spec-list object
&rest body
)
1604 "Bind SPEC-LIST lexically to slot values in OBJECT, and execute BODY.
1605 This establishes a lexical environment for referring to the slots in
1606 the instance named by the given slot-names as though they were
1607 variables. Within such a context the value of the slot can be
1608 specified by using its slot name, as if it were a lexically bound
1609 variable. Both setf and setq can be used to set the value of the
1612 SPEC-LIST is of a form similar to `let'. For example:
1619 Where each VAR is the local variable given to the associated
1620 SLOT. A slot specified without a variable name is given a
1621 variable name of the same name as the slot."
1622 (declare (indent 2))
1623 ;; Transform the spec-list into a symbol-macrolet spec-list.
1624 (let ((mappings (mapcar (lambda (entry)
1625 (let ((var (if (listp entry
) (car entry
) entry
))
1626 (slot (if (listp entry
) (cadr entry
) entry
)))
1627 (list var
`(slot-value ,object
',slot
))))
1629 (append (list 'symbol-macrolet mappings
)
1632 ;;; Simple generators, and query functions. None of these would do
1633 ;; well embedded into an object.
1635 (defmacro object-class-fast
(obj) "Return the class struct defining OBJ with no check."
1636 `(aref ,obj object-class
))
1638 (defun class-name (class) "Return a Lisp like symbol name for CLASS."
1639 (if (not (class-p class
)) (signal 'wrong-type-argument
(list 'class-p class
)))
1640 ;; I think this is supposed to return a symbol, but to me CLASS is a symbol,
1641 ;; and I wanted a string. Arg!
1642 (format "#<class %s>" (symbol-name class
)))
1644 (defun object-name (obj &optional extra
)
1645 "Return a Lisp like symbol string for object OBJ.
1646 If EXTRA, include that in the string returned to represent the symbol."
1647 (if (not (eieio-object-p obj
)) (signal 'wrong-type-argument
(list 'eieio-object-p obj
)))
1648 (format "#<%s %s%s>" (symbol-name (object-class-fast obj
))
1649 (aref obj object-name
) (or extra
"")))
1651 (defun object-name-string (obj) "Return a string which is OBJ's name."
1652 (if (not (eieio-object-p obj
)) (signal 'wrong-type-argument
(list 'eieio-object-p obj
)))
1653 (aref obj object-name
))
1655 (defun object-set-name-string (obj name
) "Set the string which is OBJ's NAME."
1656 (if (not (eieio-object-p obj
)) (signal 'wrong-type-argument
(list 'eieio-object-p obj
)))
1657 (if (not (stringp name
)) (signal 'wrong-type-argument
(list 'stringp name
)))
1658 (aset obj object-name name
))
1660 (defun object-class (obj) "Return the class struct defining OBJ."
1661 (if (not (eieio-object-p obj
)) (signal 'wrong-type-argument
(list 'eieio-object-p obj
)))
1662 (object-class-fast obj
))
1663 (defalias 'class-of
'object-class
)
1665 (defun object-class-name (obj) "Return a Lisp like symbol name for OBJ's class."
1666 (if (not (eieio-object-p obj
)) (signal 'wrong-type-argument
(list 'eieio-object-p obj
)))
1667 (class-name (object-class-fast obj
)))
1669 (defmacro class-parents-fast
(class) "Return parent classes to CLASS with no check."
1670 `(aref (class-v ,class
) class-parent
))
1672 (defun class-parents (class)
1673 "Return parent classes to CLASS. (overload of variable).
1675 The CLOS function `class-direct-superclasses' is aliased to this function."
1676 (if (not (class-p class
)) (signal 'wrong-type-argument
(list 'class-p class
)))
1677 (class-parents-fast class
))
1679 (defmacro class-children-fast
(class) "Return child classes to CLASS with no check."
1680 `(aref (class-v ,class
) class-children
))
1682 (defun class-children (class)
1683 "Return child classes to CLASS.
1685 The CLOS function `class-direct-subclasses' is aliased to this function."
1686 (if (not (class-p class
)) (signal 'wrong-type-argument
(list 'class-p class
)))
1687 (class-children-fast class
))
1689 (defun eieio-c3-candidate (class remaining-inputs
)
1690 "Returns CLASS if it can go in the result now, otherwise nil"
1691 ;; Ensure CLASS is not in any position but the first in any of the
1692 ;; element lists of REMAINING-INPUTS.
1693 (and (not (let ((found nil
))
1694 (while (and remaining-inputs
(not found
))
1695 (setq found
(member class
(cdr (car remaining-inputs
)))
1696 remaining-inputs
(cdr remaining-inputs
)))
1700 (defun eieio-c3-merge-lists (reversed-partial-result remaining-inputs
)
1701 "Merge REVERSED-PARTIAL-RESULT REMAINING-INPUTS in a consistent order, if possible.
1702 If a consistent order does not exist, signal an error."
1703 (if (let ((tail remaining-inputs
)
1705 (while (and tail
(not found
))
1706 (setq found
(car tail
) tail
(cdr tail
)))
1708 ;; If all remaining inputs are empty lists, we are done.
1709 (nreverse reversed-partial-result
)
1710 ;; Otherwise, we try to find the next element of the result. This
1711 ;; is achieved by considering the first element of each
1712 ;; (non-empty) input list and accepting a candidate if it is
1713 ;; consistent with the rests of the input lists.
1715 (tail remaining-inputs
)
1717 (while (and tail
(not found
))
1718 (setq found
(and (car tail
)
1719 (eieio-c3-candidate (caar tail
)
1724 ;; The graph is consistent so far, add NEXT to result and
1725 ;; merge input lists, dropping NEXT from their heads where
1727 (eieio-c3-merge-lists
1728 (cons next reversed-partial-result
)
1729 (mapcar (lambda (l) (if (eq (first l
) next
) (rest l
) l
))
1731 ;; The graph is inconsistent, give up
1732 (signal 'inconsistent-class-hierarchy
(list remaining-inputs
))))))
1734 (defun eieio-class-precedence-dfs (class)
1735 "Return all parents of CLASS in depth-first order."
1736 (let* ((parents (class-parents-fast class
))
1737 (classes (copy-sequence
1744 (eieio-class-precedence-dfs parent
)))
1746 '((eieio-default-superclass))))))
1748 ;; Remove duplicates.
1750 (setcdr tail
(delq (car tail
) (cdr tail
)))
1751 (setq tail
(cdr tail
)))
1754 (defun eieio-class-precedence-bfs (class)
1755 "Return all parents of CLASS in breadth-first order."
1757 (queue (or (class-parents-fast class
)
1758 '(eieio-default-superclass))))
1760 (let ((head (pop queue
)))
1761 (unless (member head result
)
1763 (unless (eq head
'eieio-default-superclass
)
1764 (setq queue
(append queue
(or (class-parents-fast head
)
1765 '(eieio-default-superclass))))))))
1766 (cons class
(nreverse result
)))
1769 (defun eieio-class-precedence-c3 (class)
1770 "Return all parents of CLASS in c3 order."
1771 (let ((parents (class-parents-fast class
)))
1772 (eieio-c3-merge-lists
1778 (eieio-class-precedence-c3 x
))
1780 '((eieio-default-superclass)))
1784 (defun class-precedence-list (class)
1785 "Return (transitively closed) list of parents of CLASS.
1786 The order, in which the parents are returned depends on the
1787 method invocation orders of the involved classes."
1788 (if (or (null class
) (eq class
'eieio-default-superclass
))
1790 (case (class-method-invocation-order class
)
1792 (eieio-class-precedence-dfs class
))
1794 (eieio-class-precedence-bfs class
))
1796 (eieio-class-precedence-c3 class
))))
1799 ;; Official CLOS functions.
1800 (defalias 'class-direct-superclasses
'class-parents
)
1801 (defalias 'class-direct-subclasses
'class-children
)
1803 (defmacro class-parent-fast
(class) "Return first parent class to CLASS with no check."
1804 `(car (class-parents-fast ,class
)))
1806 (defmacro class-parent
(class) "Return first parent class to CLASS. (overload of variable)."
1807 `(car (class-parents ,class
)))
1809 (defmacro same-class-fast-p
(obj class
) "Return t if OBJ is of class-type CLASS with no error checking."
1810 `(eq (aref ,obj object-class
) ,class
))
1812 (defun same-class-p (obj class
) "Return t if OBJ is of class-type CLASS."
1813 (if (not (class-p class
)) (signal 'wrong-type-argument
(list 'class-p class
)))
1814 (if (not (eieio-object-p obj
)) (signal 'wrong-type-argument
(list 'eieio-object-p obj
)))
1815 (same-class-fast-p obj class
))
1817 (defun object-of-class-p (obj class
)
1818 "Return non-nil if OBJ is an instance of CLASS or CLASS' subclasses."
1819 (if (not (eieio-object-p obj
)) (signal 'wrong-type-argument
(list 'eieio-object-p obj
)))
1820 ;; class will be checked one layer down
1821 (child-of-class-p (aref obj object-class
) class
))
1822 ;; Backwards compatibility
1823 (defalias 'obj-of-class-p
'object-of-class-p
)
1825 (defun child-of-class-p (child class
)
1826 "Return non-nil if CHILD class is a subclass of CLASS."
1827 (if (not (class-p class
)) (signal 'wrong-type-argument
(list 'class-p class
)))
1828 (if (not (class-p child
)) (signal 'wrong-type-argument
(list 'class-p child
)))
1830 (while (and child
(not (eq child class
)))
1831 (setq p
(append p
(aref (class-v child
) class-parent
))
1836 (defun object-slots (obj)
1837 "Return list of slots available in OBJ."
1838 (if (not (eieio-object-p obj
)) (signal 'wrong-type-argument
(list 'eieio-object-p obj
)))
1839 (aref (class-v (object-class-fast obj
)) class-public-a
))
1841 (defun class-slot-initarg (class slot
) "Fetch from CLASS, SLOT's :initarg."
1842 (if (not (class-p class
)) (signal 'wrong-type-argument
(list 'class-p class
)))
1843 (let ((ia (aref (class-v class
) class-initarg-tuples
))
1845 (while (and ia
(not f
))
1846 (if (eq (cdr (car ia
)) slot
)
1847 (setq f
(car (car ia
))))
1851 ;;; CLOS queries into classes and slots
1853 (defun slot-boundp (object slot
)
1854 "Return non-nil if OBJECT's SLOT is bound.
1855 Setting a slot's value makes it bound. Calling `slot-makeunbound' will
1856 make a slot unbound.
1857 OBJECT can be an instance or a class."
1858 ;; Skip typechecking while retrieving this value.
1859 (let ((eieio-skip-typecheck t
))
1860 ;; Return nil if the magic symbol is in there.
1862 ((eieio-object-p object
) (eieio-oref object slot
))
1863 ((class-p object
) (eieio-oref-default object slot
))
1864 (t (signal 'wrong-type-argument
(list 'eieio-object-p object
))))
1867 (defun slot-makeunbound (object slot
)
1868 "In OBJECT, make SLOT unbound."
1869 (eieio-oset object slot eieio-unbound
))
1871 (defun slot-exists-p (object-or-class slot
)
1872 "Return non-nil if OBJECT-OR-CLASS has SLOT."
1873 (let ((cv (class-v (cond ((eieio-object-p object-or-class
)
1874 (object-class object-or-class
))
1875 ((class-p object-or-class
)
1878 (or (memq slot
(aref cv class-public-a
))
1879 (memq slot
(aref cv class-class-allocation-a
)))
1882 (defun find-class (symbol &optional errorp
)
1883 "Return the class that SYMBOL represents.
1884 If there is no class, nil is returned if ERRORP is nil.
1885 If ERRORP is non-nil, `wrong-argument-type' is signaled."
1886 (if (not (class-p symbol
))
1887 (if errorp
(signal 'wrong-type-argument
(list 'class-p symbol
))
1891 ;;; Slightly more complex utility functions for objects
1893 (defun object-assoc (key slot list
)
1894 "Return an object if KEY is `equal' to SLOT's value of an object in LIST.
1895 LIST is a list of objects whose slots are searched.
1896 Objects in LIST do not need to have a slot named SLOT, nor does
1897 SLOT need to be bound. If these errors occur, those objects will
1899 (if (not (listp list
)) (signal 'wrong-type-argument
(list 'listp list
)))
1900 (while (and list
(not (condition-case nil
1901 ;; This prevents errors for missing slots.
1902 (equal key
(eieio-oref (car list
) slot
))
1904 (setq list
(cdr list
)))
1907 (defun object-assoc-list (slot list
)
1908 "Return an association list with the contents of SLOT as the key element.
1909 LIST must be a list of objects with SLOT in it.
1910 This is useful when you need to do completing read on an object group."
1911 (if (not (listp list
)) (signal 'wrong-type-argument
(list 'listp list
)))
1912 (let ((assoclist nil
))
1914 (setq assoclist
(cons (cons (eieio-oref (car list
) slot
)
1917 (setq list
(cdr list
)))
1918 (nreverse assoclist
)))
1920 (defun object-assoc-list-safe (slot list
)
1921 "Return an association list with the contents of SLOT as the key element.
1922 LIST must be a list of objects, but those objects do not need to have
1923 SLOT in it. If it does not, then that element is left out of the association
1925 (if (not (listp list
)) (signal 'wrong-type-argument
(list 'listp list
)))
1926 (let ((assoclist nil
))
1928 (if (slot-exists-p (car list
) slot
)
1929 (setq assoclist
(cons (cons (eieio-oref (car list
) slot
)
1932 (setq list
(cdr list
)))
1933 (nreverse assoclist
)))
1935 (defun object-add-to-list (object slot item
&optional append
)
1936 "In OBJECT's SLOT, add ITEM to the list of elements.
1937 Optional argument APPEND indicates we need to append to the list.
1938 If ITEM already exists in the list in SLOT, then it is not added.
1939 Comparison is done with `equal' through the `member' function call.
1940 If SLOT is unbound, bind it to the list containing ITEM."
1942 ;; Find the originating list.
1943 (if (not (slot-boundp object slot
))
1944 (setq ov
(list item
))
1945 (setq ov
(eieio-oref object slot
))
1946 ;; turn it into a list.
1948 (setq ov
(list ov
)))
1949 ;; Do the combination
1950 (if (not (member item ov
))
1953 (append ov
(list item
))
1955 ;; Set back into the slot.
1956 (eieio-oset object slot ov
)))
1958 (defun object-remove-from-list (object slot item
)
1959 "In OBJECT's SLOT, remove occurrences of ITEM.
1960 Deletion is done with `delete', which deletes by side effect,
1961 and comparisons are done with `equal'.
1962 If SLOT is unbound, do nothing."
1963 (if (not (slot-boundp object slot
))
1965 (eieio-oset object slot
(delete item
(eieio-oref object slot
)))))
1967 ;;; EIEIO internal search functions
1969 (defun eieio-slot-originating-class-p (start-class slot
)
1970 "Return non-nil if START-CLASS is the first class to define SLOT.
1971 This is for testing if `scoped-class' is the class that defines SLOT
1972 so that we can protect private slots."
1973 (let ((par (class-parents start-class
))
1977 (while (and par ret
)
1978 (if (intern-soft (symbol-name slot
)
1979 (aref (class-v (car par
))
1980 class-symbol-obarray
))
1982 (setq par
(cdr par
)))
1985 (defun eieio-slot-name-index (class obj slot
)
1986 "In CLASS for OBJ find the index of the named SLOT.
1987 The slot is a symbol which is installed in CLASS by the `defclass'
1988 call. OBJ can be nil, but if it is an object, and the slot in question
1989 is protected, access will be allowed if OBJ is a child of the currently
1991 If SLOT is the value created with :initarg instead,
1992 reverse-lookup that name, and recurse with the associated slot value."
1993 ;; Removed checks to outside this call
1994 (let* ((fsym (intern-soft (symbol-name slot
)
1995 (aref (class-v class
)
1996 class-symbol-obarray
)))
1997 (fsi (if (symbolp fsym
) (symbol-value fsym
) nil
)))
2000 ((not (get fsym
'protection
))
2002 ((and (eq (get fsym
'protection
) 'protected
)
2004 (or (child-of-class-p class scoped-class
)
2005 (and (eieio-object-p obj
)
2006 (child-of-class-p class
(object-class obj
)))))
2008 ((and (eq (get fsym
'protection
) 'private
)
2009 (or (and scoped-class
2010 (eieio-slot-originating-class-p scoped-class slot
))
2011 eieio-initializing-object
))
2014 (let ((fn (eieio-initarg-to-attribute class slot
)))
2015 (if fn
(eieio-slot-name-index class obj fn
) nil
)))))
2017 (defun eieio-class-slot-name-index (class slot
)
2018 "In CLASS find the index of the named SLOT.
2019 The slot is a symbol which is installed in CLASS by the `defclass'
2020 call. If SLOT is the value created with :initarg instead,
2021 reverse-lookup that name, and recurse with the associated slot value."
2022 ;; This will happen less often, and with fewer slots. Do this the
2023 ;; storage cheap way.
2024 (let* ((a (aref (class-v class
) class-class-allocation-a
))
2028 ;; Slot # is length of the total list, minus the remaining list of
2032 ;;; CLOS generics internal function handling
2034 (defvar eieio-generic-call-methodname nil
2035 "When using `call-next-method', provides a context on how to do it.")
2036 (defvar eieio-generic-call-arglst nil
2037 "When using `call-next-method', provides a context for parameters.")
2038 (defvar eieio-generic-call-key nil
2039 "When using `call-next-method', provides a context for the current key.
2040 Keys are a number representing :before, :primary, and :after methods.")
2041 (defvar eieio-generic-call-next-method-list nil
2042 "When executing a PRIMARY or STATIC method, track the 'next-method'.
2043 During executions, the list is first generated, then as each next method
2044 is called, the next method is popped off the stack.")
2046 (defvar eieio-pre-method-execution-hooks nil
2047 "*Hooks run just before a method is executed.
2048 The hook function must accept one argument, the list of forms
2049 about to be executed.")
2051 (defun eieio-generic-call (method args
)
2052 "Call METHOD with ARGS.
2053 ARGS provides the context on which implementation to use.
2054 This should only be called from a generic function."
2055 ;; We must expand our arguments first as they are always
2056 ;; passed in as quoted symbols
2057 (let ((newargs nil
) (mclass nil
) (lambdas nil
) (tlambdas nil
) (keys nil
)
2058 (eieio-generic-call-methodname method
)
2059 (eieio-generic-call-arglst args
)
2061 (primarymethodlist nil
))
2064 firstarg
(car newargs
))
2065 ;; Is the class passed in autoloaded?
2066 ;; Since class names are also constructors, they can be autoloaded
2067 ;; via the autoload command. Check for this, and load them in.
2068 ;; It's ok if it doesn't turn out to be a class. Probably want that
2069 ;; function loaded anyway.
2070 (if (and (symbolp firstarg
)
2072 (listp (symbol-function firstarg
))
2073 (eq 'autoload
(car (symbol-function firstarg
))))
2074 (load (nth 1 (symbol-function firstarg
))))
2075 ;; Determine the class to use.
2076 (cond ((eieio-object-p firstarg
)
2077 (setq mclass
(object-class-fast firstarg
)))
2079 (setq mclass firstarg
))
2081 ;; Make sure the class is a valid class
2082 ;; mclass can be nil (meaning a generic for should be used.
2083 ;; mclass cannot have a value that is not a class, however.
2084 (when (and (not (null mclass
)) (not (class-p mclass
)))
2085 (error "Cannot dispatch method %S on class %S"
2088 ;; Now create a list in reverse order of all the calls we have
2089 ;; make in order to successfully do this right. Rules:
2090 ;; 1) Only call generics if scoped-class is not defined
2091 ;; This prevents multiple calls in the case of recursion
2092 ;; 2) Only call static if this is a static method.
2093 ;; 3) Only call specifics if the definition allows for them.
2094 ;; 4) Call in order based on :before, :primary, and :after
2095 (when (eieio-object-p firstarg
)
2096 ;; Non-static calls do all this stuff.
2101 (eieiomt-method-list method method-after mclass
)
2102 (list (eieio-generic-form method method-after nil
)))
2103 ;;(or (and mclass (eieio-generic-form method method-after mclass))
2104 ;; (eieio-generic-form method method-after nil))
2106 (setq lambdas
(append tlambdas lambdas
)
2107 keys
(append (make-list (length tlambdas
) method-after
) keys
))
2111 (or (and mclass
(eieio-generic-form method method-primary mclass
))
2112 (eieio-generic-form method method-primary nil
)))
2114 (setq lambdas
(cons tlambdas lambdas
)
2115 keys
(cons method-primary keys
)
2117 (eieiomt-method-list method method-primary mclass
)))
2122 (eieiomt-method-list method method-before mclass
)
2123 (list (eieio-generic-form method method-before nil
)))
2124 ;;(or (and mclass (eieio-generic-form method method-before mclass))
2125 ;; (eieio-generic-form method method-before nil))
2127 (setq lambdas
(append tlambdas lambdas
)
2128 keys
(append (make-list (length tlambdas
) method-before
) keys
))
2132 ;; For the case of a class,
2133 ;; if there were no methods found, then there could be :static methods.
2136 (eieio-generic-form method method-static mclass
))
2137 (setq lambdas
(cons tlambdas lambdas
)
2138 keys
(cons method-static keys
)
2139 primarymethodlist
;; Re-use even with bad name here
2140 (eieiomt-method-list method method-static mclass
)))
2141 ;; For the case of no class (ie - mclass == nil) then there may
2142 ;; be a primary method.
2144 (eieio-generic-form method method-primary nil
))
2146 (setq lambdas
(cons tlambdas lambdas
)
2147 keys
(cons method-primary keys
)
2149 (eieiomt-method-list method method-primary nil
)))
2152 (run-hook-with-args 'eieio-pre-method-execution-hooks
2155 ;; Now loop through all occurrences forms which we must execute
2156 ;; (which are happily sorted now) and execute them all!
2157 (let ((rval nil
) (lastval nil
) (rvalever nil
) (found nil
))
2160 (let* ((scoped-class (cdr (car lambdas
)))
2161 (eieio-generic-call-key (car keys
))
2163 (or (= eieio-generic-call-key method-primary
)
2164 (= eieio-generic-call-key method-static
)))
2165 (eieio-generic-call-next-method-list
2166 ;; Use the cdr, as the first element is the fcn
2167 ;; we are calling right now.
2168 (when has-return-val
(cdr primarymethodlist
)))
2171 ;;(setq rval (apply (car (car lambdas)) newargs))
2172 (setq lastval
(apply (car (car lambdas
)) newargs
))
2173 (when has-return-val
2177 (setq lambdas
(cdr lambdas
)
2180 (if (eieio-object-p (car args
))
2181 (setq rval
(apply 'no-applicable-method
(car args
) method args
)
2184 'no-method-definition
2185 (list method args
))))
2186 ;; Right Here... it could be that lastval is returned when
2187 ;; rvalever is nil. Is that right?
2190 (defun eieio-generic-call-primary-only (method args
)
2191 "Call METHOD with ARGS for methods with only :PRIMARY implementations.
2192 ARGS provides the context on which implementation to use.
2193 This should only be called from a generic function.
2195 This method is like `eieio-generic-call', but only
2196 implementations in the :PRIMARY slot are queried. After many
2197 years of use, it appears that over 90% of methods in use
2198 have :PRIMARY implementations only. We can therefore optimize
2199 for this common case to improve performance."
2200 ;; We must expand our arguments first as they are always
2201 ;; passed in as quoted symbols
2202 (let ((newargs nil
) (mclass nil
) (lambdas nil
)
2203 (eieio-generic-call-methodname method
)
2204 (eieio-generic-call-arglst args
)
2206 (primarymethodlist nil
)
2210 firstarg
(car newargs
))
2212 ;; Determine the class to use.
2213 (cond ((eieio-object-p firstarg
)
2214 (setq mclass
(object-class-fast firstarg
)))
2216 (error "Method %s called on nil" method
))
2217 ((not (eieio-object-p firstarg
))
2218 (error "Primary-only method %s called on something not an object" method
))
2220 (error "EIEIO Error: Improperly classified method %s as primary only"
2223 ;; Make sure the class is a valid class
2224 ;; mclass can be nil (meaning a generic for should be used.
2225 ;; mclass cannot have a value that is not a class, however.
2227 (error "Cannot dispatch method %S on class %S" method mclass
)
2231 (setq lambdas
(eieio-generic-form method method-primary mclass
))
2232 (setq primarymethodlist
;; Re-use even with bad name here
2233 (eieiomt-method-list method method-primary mclass
))
2235 ;; Now loop through all occurrences forms which we must execute
2236 ;; (which are happily sorted now) and execute them all!
2237 (let* ((rval nil
) (lastval nil
) (rvalever nil
)
2238 (scoped-class (cdr lambdas
))
2239 (eieio-generic-call-key method-primary
)
2240 ;; Use the cdr, as the first element is the fcn
2241 ;; we are calling right now.
2242 (eieio-generic-call-next-method-list (cdr primarymethodlist
))
2245 (if (or (not lambdas
) (not (car lambdas
)))
2247 ;; No methods found for this impl...
2248 (if (eieio-object-p (car args
))
2249 (setq rval
(apply 'no-applicable-method
(car args
) method args
)
2252 'no-method-definition
2253 (list method args
)))
2255 ;; Do the regular implementation here.
2257 (run-hook-with-args 'eieio-pre-method-execution-hooks
2260 (setq lastval
(apply (car lambdas
) newargs
))
2265 ;; Right Here... it could be that lastval is returned when
2266 ;; rvalever is nil. Is that right?
2269 (defun eieiomt-method-list (method key class
)
2270 "Return an alist list of methods lambdas.
2271 METHOD is the method name.
2272 KEY represents either :before, or :after methods.
2273 CLASS is the starting class to search from in the method tree.
2274 If CLASS is nil, then an empty list of methods should be returned."
2275 ;; Note: eieiomt - the MT means MethodTree. See more comments below
2276 ;; for the rest of the eieiomt methods.
2278 ;; Collect lambda expressions stored for the class and its parent
2281 (dolist (ancestor (class-precedence-list class
))
2282 ;; Lookup the form to use for the PRIMARY object for the next level
2283 (let ((tmpl (eieio-generic-form method key ancestor
)))
2286 ;; This prevents duplicates coming out of the
2287 ;; class method optimizer. Perhaps we should
2288 ;; just not optimize before/afters?
2289 (not (member tmpl lambdas
))))
2290 (push tmpl lambdas
))))
2292 ;; Return collected lambda. For :after methods, return in current
2293 ;; order (most general class last); Otherwise, reverse order.
2294 (if (eq key method-after
)
2296 (nreverse lambdas
))))
2298 (defun next-method-p ()
2299 "Return non-nil if there is a next method.
2300 Returns a list of lambda expressions which is the `next-method'
2302 eieio-generic-call-next-method-list
)
2304 (defun call-next-method (&rest replacement-args
)
2305 "Call the superclass method from a subclass method.
2306 The superclass method is specified in the current method list,
2307 and is called the next method.
2309 If REPLACEMENT-ARGS is non-nil, then use them instead of
2310 `eieio-generic-call-arglst'. The generic arg list are the
2311 arguments passed in at the top level.
2313 Use `next-method-p' to find out if there is a next method to call."
2314 (if (not scoped-class
)
2315 (error "`call-next-method' not called within a class specific method"))
2316 (if (and (/= eieio-generic-call-key method-primary
)
2317 (/= eieio-generic-call-key method-static
))
2318 (error "Cannot `call-next-method' except in :primary or :static methods")
2320 (let ((newargs (or replacement-args eieio-generic-call-arglst
))
2321 (next (car eieio-generic-call-next-method-list
))
2323 (if (or (not next
) (not (car next
)))
2324 (apply 'no-next-method
(car newargs
) (cdr newargs
))
2325 (let* ((eieio-generic-call-next-method-list
2326 (cdr eieio-generic-call-next-method-list
))
2327 (eieio-generic-call-arglst newargs
)
2328 (scoped-class (cdr next
))
2335 ;; eieio-method-tree : eieiomt-
2337 ;; Stored as eieio-method-tree in property list of a generic method
2339 ;; (eieio-method-tree . [BEFORE PRIMARY AFTER
2340 ;; genericBEFORE genericPRIMARY genericAFTER])
2342 ;; (eieio-method-obarray . [BEFORE PRIMARY AFTER
2343 ;; genericBEFORE genericPRIMARY genericAFTER])
2344 ;; where the association is a vector.
2345 ;; (aref 0 -- all static methods.
2346 ;; (aref 1 -- all methods classified as :before
2347 ;; (aref 2 -- all methods classified as :primary
2348 ;; (aref 3 -- all methods classified as :after
2349 ;; (aref 4 -- a generic classified as :before
2350 ;; (aref 5 -- a generic classified as :primary
2351 ;; (aref 6 -- a generic classified as :after
2353 (defvar eieiomt-optimizing-obarray nil
2354 "While mapping atoms, this contain the obarray being optimized.")
2356 (defun eieiomt-install (method-name)
2357 "Install the method tree, and obarray onto METHOD-NAME.
2358 Do not do the work if they already exist."
2359 (let ((emtv (get method-name
'eieio-method-tree
))
2360 (emto (get method-name
'eieio-method-obarray
)))
2361 (if (or (not emtv
) (not emto
))
2363 (setq emtv
(put method-name
'eieio-method-tree
2364 (make-vector method-num-slots nil
))
2365 emto
(put method-name
'eieio-method-obarray
2366 (make-vector method-num-slots nil
)))
2367 (aset emto
0 (make-vector 11 0))
2368 (aset emto
1 (make-vector 11 0))
2369 (aset emto
2 (make-vector 41 0))
2370 (aset emto
3 (make-vector 11 0))
2373 (defun eieiomt-add (method-name method key class
)
2374 "Add to METHOD-NAME the forms METHOD in a call position KEY for CLASS.
2375 METHOD-NAME is the name created by a call to `defgeneric'.
2376 METHOD are the forms for a given implementation.
2377 KEY is an integer (see comment in eieio.el near this function) which
2378 is associated with the :static :before :primary and :after tags.
2379 It also indicates if CLASS is defined or not.
2380 CLASS is the class this method is associated with."
2381 (if (or (> key method-num-slots
) (< key
0))
2382 (error "eieiomt-add: method key error!"))
2383 (let ((emtv (get method-name
'eieio-method-tree
))
2384 (emto (get method-name
'eieio-method-obarray
)))
2385 ;; Make sure the method tables are available.
2386 (if (or (not emtv
) (not emto
))
2387 (error "Programmer error: eieiomt-add"))
2388 ;; only add new cells on if it doesn't already exist!
2389 (if (assq class
(aref emtv key
))
2390 (setcdr (assq class
(aref emtv key
)) method
)
2391 (aset emtv key
(cons (cons class method
) (aref emtv key
))))
2392 ;; Add function definition into newly created symbol, and store
2393 ;; said symbol in the correct obarray, otherwise use the
2394 ;; other array to keep this stuff
2395 (if (< key method-num-lists
)
2396 (let ((nsym (intern (symbol-name class
) (aref emto key
))))
2397 (fset nsym method
)))
2398 ;; Now optimize the entire obarray
2399 (if (< key method-num-lists
)
2400 (let ((eieiomt-optimizing-obarray (aref emto key
)))
2401 ;; @todo - Is this overkill? Should we just clear the symbol?
2402 (mapatoms 'eieiomt-sym-optimize eieiomt-optimizing-obarray
)))
2405 (defun eieiomt-next (class)
2406 "Return the next parent class for CLASS.
2407 If CLASS is a superclass, return variable `eieio-default-superclass'.
2408 If CLASS is variable `eieio-default-superclass' then return nil.
2409 This is different from function `class-parent' as class parent returns
2410 nil for superclasses. This function performs no type checking!"
2411 ;; No type-checking because all calls are made from functions which
2412 ;; are safe and do checking for us.
2413 (or (class-parents-fast class
)
2414 (if (eq class
'eieio-default-superclass
)
2416 '(eieio-default-superclass))))
2418 (defun eieiomt-sym-optimize (s)
2419 "Find the next class above S which has a function body for the optimizer."
2420 ;; Set the value to nil in case there is no nearest cell.
2422 ;; Find the nearest cell that has a function body. If we find one,
2423 ;; we replace the nil from above.
2424 (let ((external-symbol (intern-soft (symbol-name s
))))
2426 (dolist (ancestor (rest (class-precedence-list external-symbol
)))
2427 (let ((ov (intern-soft (symbol-name ancestor
)
2428 eieiomt-optimizing-obarray
)))
2430 (set s ov
) ;; store ov as our next symbol
2431 (throw 'done ancestor
)))))))
2433 (defun eieio-generic-form (method key class
)
2434 "Return the lambda form belonging to METHOD using KEY based upon CLASS.
2435 If CLASS is not a class then use `generic' instead. If class has
2436 no form, but has a parent class, then trace to that parent class.
2437 The first time a form is requested from a symbol, an optimized path
2438 is memorized for faster future use."
2439 (let ((emto (aref (get method
'eieio-method-obarray
)
2440 (if class key
(eieio-specialized-key-to-generic-key key
)))))
2442 ;; 1) find our symbol
2443 (let ((cs (intern-soft (symbol-name class
) emto
)))
2445 ;; 2) If there isn't one, then make one.
2446 ;; This can be slow since it only occurs once
2448 (setq cs
(intern (symbol-name class
) emto
))
2449 ;; 2.1) Cache its nearest neighbor with a quick optimize
2450 ;; which should only occur once for this call ever
2451 (let ((eieiomt-optimizing-obarray emto
))
2452 (eieiomt-sym-optimize cs
))))
2453 ;; 3) If it's bound return this one.
2455 (cons cs
(aref (class-v class
) class-symbol
))
2456 ;; 4) If it's not bound then this variable knows something
2457 (if (symbol-value cs
)
2459 ;; 4.1) This symbol holds the next class in its value
2460 (setq class
(symbol-value cs
)
2461 cs
(intern-soft (symbol-name class
) emto
))
2462 ;; 4.2) The optimizer should always have chosen a
2465 (cons cs
(aref (class-v (intern (symbol-name class
)))
2467 ;;(error "EIEIO optimizer: erratic data loss!"))
2469 ;; There never will be a funcall...
2471 ;; for a generic call, what is a list, is the function body we want.
2472 (let ((emtl (aref (get method
'eieio-method-tree
)
2473 (if class key
(eieio-specialized-key-to-generic-key key
)))))
2475 ;; The car of EMTL is supposed to be a class, which in this
2476 ;; case is nil, so skip it.
2477 (cons (cdr (car emtl
)) nil
)
2481 ;; Way to assign slots based on a list. Used for constructors, or
2482 ;; even resetting an object at run-time
2484 (defun eieio-set-defaults (obj &optional set-all
)
2485 "Take object OBJ, and reset all slots to their defaults.
2486 If SET-ALL is non-nil, then when a default is nil, that value is
2487 reset. If SET-ALL is nil, the slots are only reset if the default is
2489 (let ((scoped-class (aref obj object-class
))
2490 (eieio-initializing-object t
)
2491 (pub (aref (class-v (aref obj object-class
)) class-public-a
)))
2493 (let ((df (eieio-oref-default obj
(car pub
))))
2495 (eieio-oset obj
(car pub
) df
)))
2496 (setq pub
(cdr pub
)))))
2498 (defun eieio-initarg-to-attribute (class initarg
)
2499 "For CLASS, convert INITARG to the actual attribute name.
2500 If there is no translation, pass it in directly (so we can cheat if
2501 need be... May remove that later...)"
2502 (let ((tuple (assoc initarg
(aref (class-v class
) class-initarg-tuples
))))
2507 (defun eieio-attribute-to-initarg (class attribute
)
2508 "In CLASS, convert the ATTRIBUTE into the corresponding init argument tag.
2509 This is usually a symbol that starts with `:'."
2510 (let ((tuple (rassoc attribute
(aref (class-v class
) class-initarg-tuples
))))
2516 ;;; Here are some special types of errors
2518 (intern "no-method-definition")
2519 (put 'no-method-definition
'error-conditions
'(no-method-definition error
))
2520 (put 'no-method-definition
'error-message
"No method definition")
2522 (intern "no-next-method")
2523 (put 'no-next-method
'error-conditions
'(no-next-method error
))
2524 (put 'no-next-method
'error-message
"No next method")
2526 (intern "invalid-slot-name")
2527 (put 'invalid-slot-name
'error-conditions
'(invalid-slot-name error
))
2528 (put 'invalid-slot-name
'error-message
"Invalid slot name")
2530 (intern "invalid-slot-type")
2531 (put 'invalid-slot-type
'error-conditions
'(invalid-slot-type error nil
))
2532 (put 'invalid-slot-type
'error-message
"Invalid slot type")
2534 (intern "unbound-slot")
2535 (put 'unbound-slot
'error-conditions
'(unbound-slot error nil
))
2536 (put 'unbound-slot
'error-message
"Unbound slot")
2538 (intern "inconsistent-class-hierarchy")
2539 (put 'inconsistent-class-hierarchy
'error-conditions
2540 '(inconsistent-class-hierarchy error nil
))
2541 (put 'inconsistent-class-hierarchy
'error-message
"Inconsistent class hierarchy")
2543 ;;; Here are some CLOS items that need the CL package
2546 (defsetf slot-value
(obj slot
) (store) (list 'eieio-oset obj slot store
))
2547 (defsetf eieio-oref
(obj slot
) (store) (list 'eieio-oset obj slot store
))
2549 ;; The below setf method was written by Arnd Kohrs <kohrs@acm.org>
2550 (define-setf-method oref
(obj slot
)
2553 (let ((obj-temp (gensym))
2554 (slot-temp (gensym))
2555 (store-temp (gensym)))
2556 (list (list obj-temp slot-temp
)
2557 (list obj
`(quote ,slot
))
2559 (list 'set-slot-value obj-temp slot-temp
2561 (list 'slot-value obj-temp slot-temp
)))))
2565 ;; We want all objects created by EIEIO to have some default set of
2566 ;; behaviours so we can create object utilities, and allow various
2567 ;; types of error checking. To do this, create the default EIEIO
2568 ;; class, and when no parent class is specified, use this as the
2569 ;; default. (But don't store it in the other classes as the default,
2570 ;; allowing for transparent support.)
2573 (defclass eieio-default-superclass nil
2575 "Default parent class for classes with no specified parent class.
2576 Its slots are automatically adopted by classes with no specified parents.
2577 This class is not stored in the `parent' slot of a class vector."
2580 (defalias 'standard-class
'eieio-default-superclass
)
2582 (defgeneric constructor
(class newname
&rest slots
)
2583 "Default constructor for CLASS `eieio-default-superclass'.")
2585 (defmethod constructor :static
2586 ((class eieio-default-superclass
) newname
&rest slots
)
2587 "Default constructor for CLASS `eieio-default-superclass'.
2588 NEWNAME is the name to be given to the constructed object.
2589 SLOTS are the initialization slots used by `shared-initialize'.
2590 This static method is called when an object is constructed.
2591 It allocates the vector used to represent an EIEIO object, and then
2592 calls `shared-initialize' on that object."
2593 (let* ((new-object (copy-sequence (aref (class-v class
)
2594 class-default-object-cache
))))
2595 ;; Update the name for the newly created object.
2596 (aset new-object object-name newname
)
2597 ;; Call the initialize method on the new object with the slots
2598 ;; that were passed down to us.
2599 (initialize-instance new-object slots
)
2600 ;; Return the created object.
2603 (defgeneric shared-initialize
(obj slots
)
2604 "Set slots of OBJ with SLOTS which is a list of name/value pairs.
2605 Called from the constructor routine.")
2607 (defmethod shared-initialize ((obj eieio-default-superclass
) slots
)
2608 "Set slots of OBJ with SLOTS which is a list of name/value pairs.
2609 Called from the constructor routine."
2610 (let ((scoped-class (aref obj object-class
)))
2612 (let ((rn (eieio-initarg-to-attribute (object-class-fast obj
)
2615 (slot-missing obj
(car slots
) 'oset
(car (cdr slots
)))
2616 (eieio-oset obj rn
(car (cdr slots
)))))
2617 (setq slots
(cdr (cdr slots
))))))
2619 (defgeneric initialize-instance
(this &optional slots
)
2620 "Construct the new object THIS based on SLOTS.")
2622 (defmethod initialize-instance ((this eieio-default-superclass
)
2624 "Construct the new object THIS based on SLOTS.
2625 SLOTS is a tagged list where odd numbered elements are tags, and
2626 even numbered elements are the values to store in the tagged slot.
2627 If you overload the `initialize-instance', there you will need to
2628 call `shared-initialize' yourself, or you can call `call-next-method'
2629 to have this constructor called automatically. If these steps are
2630 not taken, then new objects of your class will not have their values
2631 dynamically set from SLOTS."
2632 ;; First, see if any of our defaults are `lambda', and
2633 ;; re-evaluate them and apply the value to our slots.
2634 (let* ((scoped-class (class-v (aref this object-class
)))
2635 (slot (aref scoped-class class-public-a
))
2636 (defaults (aref scoped-class class-public-d
)))
2638 ;; For each slot, see if we need to evaluate it.
2640 ;; Paul Landes said in an email:
2641 ;; > CL evaluates it if it can, and otherwise, leaves it as
2642 ;; > the quoted thing as you already have. This is by the
2643 ;; > Sonya E. Keene book and other things I've look at on the
2645 (let ((dflt (eieio-default-eval-maybe (car defaults
))))
2646 (when (not (eq dflt
(car defaults
)))
2647 (eieio-oset this
(car slot
) dflt
) ))
2649 (setq slot
(cdr slot
)
2650 defaults
(cdr defaults
))))
2651 ;; Shared initialize will parse our slots for us.
2652 (shared-initialize this slots
))
2654 (defgeneric slot-missing
(object slot-name operation
&optional new-value
)
2655 "Method invoked when an attempt to access a slot in OBJECT fails.")
2657 (defmethod slot-missing ((object eieio-default-superclass
) slot-name
2658 operation
&optional new-value
)
2659 "Method invoked when an attempt to access a slot in OBJECT fails.
2660 SLOT-NAME is the name of the failed slot, OPERATION is the type of access
2661 that was requested, and optional NEW-VALUE is the value that was desired
2664 This method is called from `oref', `oset', and other functions which
2665 directly reference slots in EIEIO objects."
2666 (signal 'invalid-slot-name
(list (object-name object
)
2669 (defgeneric slot-unbound
(object class slot-name fn
)
2670 "Slot unbound is invoked during an attempt to reference an unbound slot.")
2672 (defmethod slot-unbound ((object eieio-default-superclass
)
2674 "Slot unbound is invoked during an attempt to reference an unbound slot.
2675 OBJECT is the instance of the object being reference. CLASS is the
2676 class of OBJECT, and SLOT-NAME is the offending slot. This function
2677 throws the signal `unbound-slot'. You can overload this function and
2678 return the value to use in place of the unbound value.
2679 Argument FN is the function signaling this error.
2680 Use `slot-boundp' to determine if a slot is bound or not.
2682 In CLOS, the argument list is (CLASS OBJECT SLOT-NAME), but
2683 EIEIO can only dispatch on the first argument, so the first two are swapped."
2684 (signal 'unbound-slot
(list (class-name class
) (object-name object
)
2687 (defgeneric no-applicable-method
(object method
&rest args
)
2688 "Called if there are no implementations for OBJECT in METHOD.")
2690 (defmethod no-applicable-method ((object eieio-default-superclass
)
2692 "Called if there are no implementations for OBJECT in METHOD.
2693 OBJECT is the object which has no method implementation.
2694 ARGS are the arguments that were passed to METHOD.
2696 Implement this for a class to block this signal. The return
2697 value becomes the return value of the original method call."
2698 (signal 'no-method-definition
(list method
(object-name object
)))
2701 (defgeneric no-next-method
(object &rest args
)
2702 "Called from `call-next-method' when no additional methods are available.")
2704 (defmethod no-next-method ((object eieio-default-superclass
)
2706 "Called from `call-next-method' when no additional methods are available.
2707 OBJECT is othe object being called on `call-next-method'.
2708 ARGS are the arguments it is called by.
2709 This method signals `no-next-method' by default. Override this
2710 method to not throw an error, and its return value becomes the
2711 return value of `call-next-method'."
2712 (signal 'no-next-method
(list (object-name object
) args
))
2715 (defgeneric clone
(obj &rest params
)
2716 "Make a copy of OBJ, and then supply PARAMS.
2717 PARAMS is a parameter list of the same form used by `initialize-instance'.
2719 When overloading `clone', be sure to call `call-next-method'
2720 first and modify the returned object.")
2722 (defmethod clone ((obj eieio-default-superclass
) &rest params
)
2723 "Make a copy of OBJ, and then apply PARAMS."
2724 (let ((nobj (copy-sequence obj
))
2725 (nm (aref obj object-name
))
2726 (passname (and params
(stringp (car params
))))
2728 (if params
(shared-initialize nobj
(if passname
(cdr params
) params
)))
2731 (if (string-match "-\\([0-9]+\\)" nm
)
2732 (setq num
(1+ (string-to-number (match-string 1 nm
)))
2733 nm
(substring nm
0 (match-beginning 0))))
2734 (aset nobj object-name
(concat nm
"-" (int-to-string num
))))
2735 (aset nobj object-name
(car params
)))
2738 (defgeneric destructor
(this &rest params
)
2739 "Destructor for cleaning up any dynamic links to our object.")
2741 (defmethod destructor ((this eieio-default-superclass
) &rest params
)
2742 "Destructor for cleaning up any dynamic links to our object.
2743 Argument THIS is the object being destroyed. PARAMS are additional
2744 ignored parameters."
2745 ;; No cleanup... yet.
2748 (defgeneric object-print
(this &rest strings
)
2749 "Pretty printer for object THIS. Call function `object-name' with STRINGS.
2751 It is sometimes useful to put a summary of the object into the
2752 default #<notation> string when using EIEIO browsing tools.
2753 Implement this method to customize the summary.")
2755 (defmethod object-print ((this eieio-default-superclass
) &rest strings
)
2756 "Pretty printer for object THIS. Call function `object-name' with STRINGS.
2757 The default method for printing object THIS is to use the
2758 function `object-name'.
2760 It is sometimes useful to put a summary of the object into the
2761 default #<notation> string when using EIEIO browsing tools.
2763 Implement this function and specify STRINGS in a call to
2764 `call-next-method' to provide additional summary information.
2765 When passing in extra strings from child classes, always remember
2766 to prepend a space."
2767 (object-name this
(apply 'concat strings
)))
2769 (defvar eieio-print-depth
0
2770 "When printing, keep track of the current indentation depth.")
2772 (defgeneric object-write
(this &optional comment
)
2773 "Write out object THIS to the current stream.
2774 Optional COMMENT will add comments to the beginning of the output.")
2776 (defmethod object-write ((this eieio-default-superclass
) &optional comment
)
2777 "Write object THIS out to the current stream.
2778 This writes out the vector version of this object. Complex and recursive
2779 object are discouraged from being written.
2780 If optional COMMENT is non-nil, include comments when outputting
2783 (princ ";; Object ")
2784 (princ (object-name-string this
))
2788 (let* ((cl (object-class this
))
2790 ;; Now output readable lisp to recreate this object
2791 ;; It should look like this:
2792 ;; (<constructor> <name> <slot> <slot> ... )
2793 ;; Each slot's slot is writen using its :writer.
2794 (princ (make-string (* eieio-print-depth
2) ?
))
2796 (princ (symbol-name (class-constructor (object-class this
))))
2798 (princ (object-name-string this
))
2800 ;; Loop over all the public slots
2801 (let ((publa (aref cv class-public-a
))
2802 (publd (aref cv class-public-d
))
2803 (publp (aref cv class-public-printer
))
2804 (eieio-print-depth (1+ eieio-print-depth
)))
2806 (when (slot-boundp this
(car publa
))
2807 (let ((i (class-slot-initarg cl
(car publa
)))
2808 (v (eieio-oref this
(car publa
)))
2810 (unless (or (not i
) (equal v
(car publd
)))
2811 (princ (make-string (* eieio-print-depth
2) ?
))
2812 (princ (symbol-name i
))
2815 ;; Use our public printer
2816 (funcall (car publp
) v
)
2817 ;; Use our generic override prin1 function.
2818 (eieio-override-prin1 v
))
2820 (setq publa
(cdr publa
) publd
(cdr publd
)
2822 (princ (make-string (* eieio-print-depth
2) ?
)))
2825 (defun eieio-override-prin1 (thing)
2826 "Perform a `prin1' on THING taking advantage of object knowledge."
2827 (cond ((eieio-object-p thing
)
2828 (object-write thing
))
2830 (eieio-list-prin1 thing
))
2832 (princ (class-name thing
)))
2834 (princ (concat "'" (symbol-name thing
))))
2837 (defun eieio-list-prin1 (list)
2838 "Display LIST where list may contain objects."
2839 (if (not (eieio-object-p (car list
)))
2844 (if (eieio-object-p (car list
)) (princ "\n "))
2846 (if (eieio-object-p (car list
))
2847 (object-write (car list
))
2851 (setq list
(cdr list
)))
2852 (princ (make-string (* eieio-print-depth
2) ?
))
2856 ;;; Unimplemented functions from CLOS
2858 (defun change-class (obj class
)
2859 "Change the class of OBJ to type CLASS.
2860 This may create or delete slots, but does not affect the return value
2862 (error "EIEIO: `change-class' is unimplemented"))
2867 ;;; Interfacing with edebug
2869 (defun eieio-edebug-prin1-to-string (object &optional noescape
)
2870 "Display EIEIO OBJECT in fancy format.
2871 Overrides the edebug default.
2872 Optional argument NOESCAPE is passed to `prin1-to-string' when appropriate."
2873 (cond ((class-p object
) (class-name object
))
2874 ((eieio-object-p object
) (object-print object
))
2875 ((and (listp object
) (or (class-p (car object
))
2876 (eieio-object-p (car object
))))
2877 (concat "(" (mapconcat 'eieio-edebug-prin1-to-string object
" ") ")"))
2878 (t (prin1-to-string object noescape
))))
2880 (add-hook 'edebug-setup-hook
2882 (def-edebug-spec defmethod
2883 (&define
; this means we are defining something
2884 [&or name
("setf" :name setf name
)]
2885 ;; ^^ This is the methods symbol
2886 [ &optional symbolp
] ; this is key :before etc
2888 [ &optional stringp
] ; documentation string
2889 def-body
; part to be debugged
2891 ;; The rest of the macros
2892 (def-edebug-spec oref
(form quote
))
2893 (def-edebug-spec oref-default
(form quote
))
2894 (def-edebug-spec oset
(form quote form
))
2895 (def-edebug-spec oset-default
(form quote form
))
2896 (def-edebug-spec class-v form
)
2897 (def-edebug-spec class-p form
)
2898 (def-edebug-spec eieio-object-p form
)
2899 (def-edebug-spec class-constructor form
)
2900 (def-edebug-spec generic-p form
)
2901 (def-edebug-spec with-slots
(list list def-body
))
2902 ;; I suspect this isn't the best way to do this, but when
2903 ;; cust-print was used on my system all my objects
2904 ;; appeared as "#1 =" which was not useful. This allows
2905 ;; edebug to print my objects in the nice way they were
2906 ;; meant to with `object-print' and `class-name'
2907 ;; (defalias 'edebug-prin1-to-string 'eieio-edebug-prin1-to-string)
2911 ;;; Interfacing with imenu in emacs lisp mode
2912 ;; (Only if the expression is defined)
2914 (if (eval-when-compile (boundp 'list-imenu-generic-expression
))
2917 (defun eieio-update-lisp-imenu-expression ()
2918 "Examine `lisp-imenu-generic-expression' and modify it to find `defmethod'."
2919 (let ((exp lisp-imenu-generic-expression
))
2921 ;; it's of the form '( ( title expr indx ) ... )
2922 (let* ((subcar (cdr (car exp
)))
2923 (substr (car subcar
)))
2924 (if (and (not (string-match "|method\\\\" substr
))
2925 (string-match "|advice\\\\" substr
))
2927 (replace-match "|advice\\|method\\" t t substr
0))))
2928 (setq exp
(cdr exp
)))))
2930 (eieio-update-lisp-imenu-expression)
2934 ;;; Autoloading some external symbols, and hooking into the help system
2938 ;;; Start of automatically extracted autoloads.
2940 ;;;### (autoloads (customize-object) "eieio-custom" "eieio-custom.el"
2941 ;;;;;; "cf1bd64c76a6e6406545e8c5a5530d43")
2942 ;;; Generated autoloads from eieio-custom.el
2944 (autoload 'customize-object
"eieio-custom" "\
2945 Customize OBJ in a custom buffer.
2946 Optional argument GROUP is the sub-group of slots to display.
2948 \(fn OBJ &optional GROUP)" nil nil
)
2952 ;;;### (autoloads (eieio-help-mode-augmentation-maybee eieio-describe-generic
2953 ;;;;;; eieio-describe-constructor eieio-describe-class eieio-browse)
2954 ;;;;;; "eieio-opt" "eieio-opt.el" "1bed0a56310f402683419139ebc18d7f")
2955 ;;; Generated autoloads from eieio-opt.el
2957 (autoload 'eieio-browse
"eieio-opt" "\
2958 Create an object browser window to show all objects.
2959 If optional ROOT-CLASS, then start with that, otherwise start with
2960 variable `eieio-default-superclass'.
2962 \(fn &optional ROOT-CLASS)" t nil
)
2964 (defalias 'describe-class
'eieio-describe-class
)
2966 (autoload 'eieio-describe-class
"eieio-opt" "\
2967 Describe a CLASS defined by a string or symbol.
2968 If CLASS is actually an object, then also display current values of that object.
2969 Optional HEADERFCN should be called to insert a few bits of info first.
2971 \(fn CLASS &optional HEADERFCN)" t nil
)
2973 (autoload 'eieio-describe-constructor
"eieio-opt" "\
2974 Describe the constructor function FCN.
2975 Uses `eieio-describe-class' to describe the class being constructed.
2979 (defalias 'describe-generic
'eieio-describe-generic
)
2981 (autoload 'eieio-describe-generic
"eieio-opt" "\
2982 Describe the generic function GENERIC.
2983 Also extracts information about all methods specific to this generic.
2985 \(fn GENERIC)" t nil
)
2987 (autoload 'eieio-help-mode-augmentation-maybee
"eieio-opt" "\
2988 For buffers thrown into help mode, augment for EIEIO.
2989 Arguments UNUSED are not used.
2991 \(fn &rest UNUSED)" nil nil
)
2995 ;;; End of automatically extracted autoloads.