1 ;;; eieio.el --- Enhanced Implementation of Emacs Interpreted Objects
2 ;;; or maybe Eric's Implementation of Emacs Interpreted Objects
4 ;; Copyright (C) 1995-1996, 1998-2012 Free Software Foundation, Inc.
6 ;; Author: Eric M. Ludlam <zappo@gnu.org>
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;; EIEIO is a series of Lisp routines which implements a subset of
27 ;; CLOS, the Common Lisp Object System. In addition, EIEIO also adds
28 ;; a few new features which help it integrate more strongly with the
29 ;; Emacs running environment.
31 ;; See eieio.texi for complete documentation on using this package.
33 ;; Note: the implementation of the c3 algorithm is based on:
34 ;; Kim Barrett et al.: A Monotonic Superclass Linearization for Dylan
36 ;; http://192.220.96.201/dylan/linearization-oopsla96.html
38 ;; There is funny stuff going on with typep and deftype. This
39 ;; is the only way I seem to be able to make this stuff load properly.
41 ;; @TODO - fix :initform to be a form, not a quoted value
42 ;; @TODO - Prefix non-clos functions with `eieio-'.
46 (eval-when-compile (require 'cl
)) ;FIXME: Use cl-lib!
48 (defvar eieio-version
"1.3"
49 "Current version of EIEIO.")
51 (defun eieio-version ()
52 "Display the current version of EIEIO."
54 (message eieio-version
))
57 ;; About the above. EIEIO must process its own code when it compiles
58 ;; itself, thus, by eval-and-compiling ourselves, we solve the problem.
61 (if (fboundp 'compiled-function-arglist
)
63 ;; XEmacs can only access a compiled functions arglist like this:
64 (defalias 'eieio-compiled-function-arglist
'compiled-function-arglist
)
66 ;; Emacs doesn't have this function, but since FUNC is a vector, we can just
67 ;; grab the appropriate element.
68 (defun eieio-compiled-function-arglist (func)
69 "Return the argument list for the compiled function FUNC."
76 ;; Variable declarations.
79 (defvar eieio-hook nil
80 "This hook is executed, then cleared each time `defclass' is called.")
82 (defvar eieio-error-unsupported-class-tags nil
83 "Non-nil to throw an error if an encountered tag is unsupported.
84 This may prevent classes from CLOS applications from being used with EIEIO
85 since EIEIO does not support all CLOS tags.")
87 (defvar eieio-skip-typecheck nil
88 "If non-nil, skip all slot typechecking.
89 Set this to t permanently if a program is functioning well to get a
90 small speed increase. This variable is also used internally to handle
91 default setting for optimization purposes.")
93 (defvar eieio-optimize-primary-methods-flag t
94 "Non-nil means to optimize the method dispatch on primary methods.")
96 (defvar eieio-initializing-object nil
97 "Set to non-nil while initializing an object.")
99 (defconst eieio-unbound
100 (if (and (boundp 'eieio-unbound
) (symbolp eieio-unbound
))
102 (make-symbol "unbound"))
103 "Uninterned symbol representing an unbound slot in an object.")
105 ;; This is a bootstrap for eieio-default-superclass so it has a value
106 ;; while it is being built itself.
107 (defvar eieio-default-superclass nil
)
109 ;; FIXME: The constants below should have an `eieio-' prefix added!!
110 (defconst class-symbol
1 "Class's symbol (self-referencing.).")
111 (defconst class-parent
2 "Class parent slot.")
112 (defconst class-children
3 "Class children class slot.")
113 (defconst class-symbol-obarray
4 "Obarray permitting fast access to variable position indexes.")
115 ;; the word "public" here is leftovers from the very first version.
117 (defconst class-public-a
5 "Class attribute index.")
118 (defconst class-public-d
6 "Class attribute defaults index.")
119 (defconst class-public-doc
7 "Class documentation strings for attributes.")
120 (defconst class-public-type
8 "Class type for a slot.")
121 (defconst class-public-custom
9 "Class custom type for a slot.")
122 (defconst class-public-custom-label
10 "Class custom group for a slot.")
123 (defconst class-public-custom-group
11 "Class custom group for a slot.")
124 (defconst class-public-printer
12 "Printer for a slot.")
125 (defconst class-protection
13 "Class protection for a slot.")
126 (defconst class-initarg-tuples
14 "Class initarg tuples list.")
127 (defconst class-class-allocation-a
15 "Class allocated attributes.")
128 (defconst class-class-allocation-doc
16 "Class allocated documentation.")
129 (defconst class-class-allocation-type
17 "Class allocated value type.")
130 (defconst class-class-allocation-custom
18 "Class allocated custom descriptor.")
131 (defconst class-class-allocation-custom-label
19 "Class allocated custom descriptor.")
132 (defconst class-class-allocation-custom-group
20 "Class allocated custom group.")
133 (defconst class-class-allocation-printer
21 "Class allocated printer for a slot.")
134 (defconst class-class-allocation-protection
22 "Class allocated protection list.")
135 (defconst class-class-allocation-values
23 "Class allocated value vector.")
136 (defconst class-default-object-cache
24
137 "Cache index of what a newly created object would look like.
138 This will speed up instantiation time as only a `copy-sequence' will
139 be needed, instead of looping over all the values and setting them
141 (defconst class-options
25
142 "Storage location of tagged class options.
143 Stored outright without modifications or stripping.")
145 (defconst class-num-slots
26
146 "Number of slots in the class definition object.")
148 (defconst object-class
1 "Index in an object vector where the class is stored.")
149 (defconst object-name
2 "Index in an object where the name is stored.")
151 (defconst method-static
0 "Index into :static tag on a method.")
152 (defconst method-before
1 "Index into :before tag on a method.")
153 (defconst method-primary
2 "Index into :primary tag on a method.")
154 (defconst method-after
3 "Index into :after tag on a method.")
155 (defconst method-num-lists
4 "Number of indexes into methods vector in which groups of functions are kept.")
156 (defconst method-generic-before
4 "Index into generic :before tag on a method.")
157 (defconst method-generic-primary
5 "Index into generic :primary tag on a method.")
158 (defconst method-generic-after
6 "Index into generic :after tag on a method.")
159 (defconst method-num-slots
7 "Number of indexes into a method's vector.")
161 (defsubst eieio-specialized-key-to-generic-key
(key)
162 "Convert a specialized KEY into a generic method key."
163 (cond ((eq key method-static
) 0) ;; don't convert
164 ((< key method-num-lists
) (+ key
3)) ;; The conversion
165 (t key
) ;; already generic.. maybe.
169 ;;; Important macros used in eieio.
171 (defmacro class-v
(class)
172 "Internal: Return the class vector from the CLASS symbol."
173 ;; No check: If eieio gets this far, it's probably been checked already.
174 `(get ,class
'eieio-class-definition
))
176 (defmacro class-p
(class)
177 "Return t if CLASS is a valid class vector.
179 ;; this new method is faster since it doesn't waste time checking lots of
182 (eq (aref (class-v ,class
) 0) 'defclass
)
185 (defmacro eieio-object-p
(obj)
186 "Return non-nil if OBJ is an EIEIO object."
189 (and (eq (aref tobj
0) 'object
)
190 (class-p (aref tobj object-class
))))
192 (defalias 'object-p
'eieio-object-p
)
194 (defmacro class-constructor
(class)
195 "Return the symbol representing the constructor of CLASS."
196 `(aref (class-v ,class
) class-symbol
))
198 (defmacro generic-p
(method)
199 "Return t if symbol METHOD is a generic function.
200 Only methods have the symbol `eieio-method-obarray' as a property
201 \(which contains a list of all bindings to that method type.)"
202 `(and (fboundp ,method
) (get ,method
'eieio-method-obarray
)))
204 (defun generic-primary-only-p (method)
205 "Return t if symbol METHOD is a generic function with only primary methods.
206 Only methods have the symbol `eieio-method-obarray' as a property (which
207 contains a list of all bindings to that method type.)
208 Methods with only primary implementations are executed in an optimized way."
209 (and (generic-p method
)
210 (let ((M (get method
'eieio-method-tree
)))
211 (and (< 0 (length (aref M method-primary
)))
212 (not (aref M method-static
))
213 (not (aref M method-before
))
214 (not (aref M method-after
))
215 (not (aref M method-generic-before
))
216 (not (aref M method-generic-primary
))
217 (not (aref M method-generic-after
))))
220 (defun generic-primary-only-one-p (method)
221 "Return t if symbol METHOD is a generic function with only primary methods.
222 Only methods have the symbol `eieio-method-obarray' as a property (which
223 contains a list of all bindings to that method type.)
224 Methods with only primary implementations are executed in an optimized way."
225 (and (generic-p method
)
226 (let ((M (get method
'eieio-method-tree
)))
227 (and (= 1 (length (aref M method-primary
)))
228 (not (aref M method-static
))
229 (not (aref M method-before
))
230 (not (aref M method-after
))
231 (not (aref M method-generic-before
))
232 (not (aref M method-generic-primary
))
233 (not (aref M method-generic-after
))))
236 (defmacro class-option-assoc
(list option
)
237 "Return from LIST the found OPTION, or nil if it doesn't exist."
238 `(car-safe (cdr (memq ,option
,list
))))
240 (defmacro class-option
(class option
)
241 "Return the value stored for CLASS' OPTION.
242 Return nil if that option doesn't exist."
243 `(class-option-assoc (aref (class-v ,class
) class-options
) ',option
))
245 (defmacro class-abstract-p
(class)
246 "Return non-nil if CLASS is abstract.
247 Abstract classes cannot be instantiated."
248 `(class-option ,class
:abstract
))
250 (defmacro class-method-invocation-order
(class)
251 "Return the invocation order of CLASS.
252 Abstract classes cannot be instantiated."
253 `(or (class-option ,class
:method-invocation-order
)
257 ;;; Defining a new class
259 (defmacro defclass
(name superclass slots
&rest options-and-doc
)
260 "Define NAME as a new class derived from SUPERCLASS with SLOTS.
261 OPTIONS-AND-DOC is used as the class' options and base documentation.
262 SUPERCLASS is a list of superclasses to inherit from, with SLOTS
263 being the slots residing in that class definition. NOTE: Currently
264 only one slot may exist in SUPERCLASS as multiple inheritance is not
265 yet supported. Supported tags are:
267 :initform - Initializing form.
268 :initarg - Tag used during initialization.
269 :accessor - Tag used to create a function to access this slot.
270 :allocation - Specify where the value is stored.
271 Defaults to `:instance', but could also be `:class'.
272 :writer - A function symbol which will `write' an object's slot.
273 :reader - A function symbol which will `read' an object.
274 :type - The type of data allowed in this slot (see `typep').
276 - A string documenting use of this slot.
278 The following are extensions on CLOS:
279 :protection - Specify protection for this slot.
280 Defaults to `:public'. Also use `:protected', or `:private'.
281 :custom - When customizing an object, the custom :type. Public only.
282 :label - A text string label used for a slot when customizing.
283 :group - Name of a customization group this slot belongs in.
284 :printer - A function to call to print the value of a slot.
285 See `eieio-override-prin1' as an example.
287 A class can also have optional options. These options happen in place
288 of documentation (including a :documentation tag), in addition to
289 documentation, or not at all. Supported options are:
291 :documentation - The doc-string used for this class.
293 Options added to EIEIO:
295 :allow-nil-initform - Non-nil to skip typechecking of null initforms.
296 :custom-groups - List of custom group names. Organizes slots into
297 reasonable groups for customizations.
298 :abstract - Non-nil to prevent instances of this class.
299 If a string, use as an error string if someone does
300 try to make an instance.
301 :method-invocation-order
302 - Control the method invocation order if there is
303 multiple inheritance. Valid values are:
304 :breadth-first - The default.
307 Options in CLOS not supported in EIEIO:
309 :metaclass - Class to use in place of `standard-class'
310 :default-initargs - Initargs to use when initializing new objects of
313 Due to the way class options are set up, you can add any tags you wish,
314 and reference them using the function `class-option'."
315 ;; We must `eval-and-compile' this so that when we byte compile
316 ;; an eieio program, there is no need to load it ahead of time.
317 ;; It also provides lots of nice debugging errors at compile time.
319 (eieio-defclass ',name
',superclass
',slots
',options-and-doc
)))
321 (defvar eieio-defclass-autoload-map
(make-vector 7 nil
)
322 "Symbol map of superclasses we find in autoloads.")
324 ;; We autoload this because it's used in `make-autoload'.
326 (defun eieio-defclass-autoload (cname superclasses filename doc
)
327 "Create autoload symbols for the EIEIO class CNAME.
328 SUPERCLASSES are the superclasses that CNAME inherits from.
329 DOC is the docstring for CNAME.
330 This function creates a mock-class for CNAME and adds it into
331 SUPERCLASSES as children.
332 It creates an autoload function for CNAME's constructor."
333 ;; Assume we've already debugged inputs.
335 (let* ((oldc (when (class-p cname
) (class-v cname
)))
336 (newc (make-vector class-num-slots nil
))
339 nil
;; Do nothing if we already have this class.
341 ;; Create the class in NEWC, but don't fill anything else in.
342 (aset newc
0 'defclass
)
343 (aset newc class-symbol cname
)
345 (let ((clear-parent nil
))
347 (when (not superclasses
)
348 (setq superclasses
'(eieio-default-superclass)
352 ;; Hook our new class into the existing structures so we can
353 ;; autoload it later.
354 (dolist (SC superclasses
)
357 ;; TODO - If we create an autoload that is in the map, that
358 ;; map needs to be cleared!
361 ;; Does our parent exist?
362 (if (not (class-p SC
))
364 ;; Create a symbol for this parent, and then store this
365 ;; parent on that symbol.
366 (let ((sym (intern (symbol-name SC
) eieio-defclass-autoload-map
)))
367 (if (not (boundp sym
))
368 (set sym
(list cname
))
369 (add-to-list sym cname
))
372 ;; We have a parent, save the child in there.
373 (when (not (member cname
(aref (class-v SC
) class-children
)))
374 (aset (class-v SC
) class-children
375 (cons cname
(aref (class-v SC
) class-children
)))))
377 ;; save parent in child
378 (aset newc class-parent
(cons SC
(aref newc class-parent
)))
381 ;; turn this into a usable self-pointing symbol
384 ;; Store the new class vector definition into the symbol. We need to
385 ;; do this first so that we can call defmethod for the accessor.
386 ;; The vector will be updated by the following while loop and will not
387 ;; need to be stored a second time.
388 (put cname
'eieio-class-definition newc
)
391 (if clear-parent
(aset newc class-parent nil
))
393 ;; Create an autoload on top of our constructor function.
394 (autoload cname filename doc nil nil
)
395 (autoload (intern (concat (symbol-name cname
) "-p")) filename
"" nil nil
)
396 (autoload (intern (concat (symbol-name cname
) "-child-p")) filename
"" nil nil
)
397 (autoload (intern (concat (symbol-name cname
) "-list-p")) filename
"" nil nil
)
401 (defsubst eieio-class-un-autoload
(cname)
402 "If class CNAME is in an autoload state, load its file."
403 (when (eq (car-safe (symbol-function cname
)) 'autoload
)
404 (load-library (car (cdr (symbol-function cname
))))))
406 (defun eieio-defclass (cname superclasses slots options-and-doc
)
407 ;; FIXME: Most of this should be moved to the `defclass' macro.
408 "Define CNAME as a new subclass of SUPERCLASSES.
409 SLOTS are the slots residing in that class definition, and options or
410 documentation OPTIONS-AND-DOC is the toplevel documentation for this class.
411 See `defclass' for more information."
412 ;; Run our eieio-hook each time, and clear it when we are done.
413 ;; This way people can add hooks safely if they want to modify eieio
414 ;; or add definitions when eieio is loaded or something like that.
415 (run-hooks 'eieio-hook
)
416 (setq eieio-hook nil
)
418 (if (not (listp superclasses
))
419 (signal 'wrong-type-argument
'(listp superclasses
)))
421 (let* ((pname superclasses
)
422 (newc (make-vector class-num-slots nil
))
423 (oldc (when (class-p cname
) (class-v cname
)))
424 (groups nil
) ;; list of groups id'd from slots
428 (aset newc
0 'defclass
)
429 (aset newc class-symbol cname
)
431 ;; If this class already existed, and we are updating its structure,
432 ;; make sure we keep the old child list. This can cause bugs, but
433 ;; if no new slots are created, it also saves time, and prevents
434 ;; method table breakage, particularly when the users is only
435 ;; byte compiling an EIEIO file.
437 (aset newc class-children
(aref oldc class-children
))
438 ;; If the old class did not exist, but did exist in the autoload map, then adopt those children.
439 ;; This is like the above, but deals with autoloads nicely.
440 (let ((sym (intern-soft (symbol-name cname
) eieio-defclass-autoload-map
)))
443 (aset newc class-children
(symbol-value sym
))
445 (unintern (symbol-name cname
) eieio-defclass-autoload-map
)
449 (cond ((and (stringp (car options-and-doc
))
450 (/= 1 (%
(length options-and-doc
) 2)))
451 (error "Too many arguments to `defclass'"))
452 ((and (symbolp (car options-and-doc
))
453 (/= 0 (%
(length options-and-doc
) 2)))
454 (error "Too many arguments to `defclass'"))
458 (if (stringp (car options-and-doc
))
459 (cons :documentation options-and-doc
)
465 (if (and (car pname
) (symbolp (car pname
)))
466 (if (not (class-p (car pname
)))
468 (error "Given parent class %s is not a class" (car pname
))
469 ;; good parent class...
470 ;; save new child in parent
471 (when (not (member cname
(aref (class-v (car pname
)) class-children
)))
472 (aset (class-v (car pname
)) class-children
473 (cons cname
(aref (class-v (car pname
)) class-children
))))
474 ;; Get custom groups, and store them into our local copy.
475 (mapc (lambda (g) (add-to-list 'groups g
))
476 (class-option (car pname
) :custom-groups
))
477 ;; save parent in child
478 (aset newc class-parent
(cons (car pname
) (aref newc class-parent
))))
479 (error "Invalid parent class %s" pname
))
480 (setq pname
(cdr pname
)))
481 ;; Reverse the list of our parents so that they are prioritized in
482 ;; the same order as specified in the code.
483 (aset newc class-parent
(nreverse (aref newc class-parent
))) )
484 ;; If there is nothing to loop over, then inherit from the
485 ;; default superclass.
486 (unless (eq cname
'eieio-default-superclass
)
487 ;; adopt the default parent here, but clear it later...
489 ;; save new child in parent
490 (if (not (member cname
(aref (class-v 'eieio-default-superclass
) class-children
)))
491 (aset (class-v 'eieio-default-superclass
) class-children
492 (cons cname
(aref (class-v 'eieio-default-superclass
) class-children
))))
493 ;; save parent in child
494 (aset newc class-parent
(list eieio-default-superclass
))))
496 ;; turn this into a usable self-pointing symbol
499 ;; These two tests must be created right away so we can have self-
500 ;; referencing classes. ei, a class whose slot can contain only
501 ;; pointers to itself.
503 ;; Create the test function
504 (let ((csym (intern (concat (symbol-name cname
) "-p"))))
506 (list 'lambda
(list 'obj
)
507 (format "Test OBJ to see if it an object of type %s" cname
)
508 (list 'and
'(eieio-object-p obj
)
509 (list 'same-class-p
'obj cname
)))))
511 ;; Make sure the method invocation order is a valid value.
512 (let ((io (class-option-assoc options
:method-invocation-order
)))
513 (when (and io
(not (member io
'(:depth-first
:breadth-first
:c3
))))
514 (error "Method invocation order %s is not allowed" io
)
517 ;; Create a handy child test too
518 (let ((csym (intern (concat (symbol-name cname
) "-child-p"))))
522 "Test OBJ to see if it an object is a child of type %s"
524 (and (eieio-object-p obj
)
525 (object-of-class-p obj
,cname
))))
527 ;; Create a handy list of the class test too
528 (let ((csym (intern (concat (symbol-name cname
) "-list-p"))))
532 "Test OBJ to see if it a list of objects which are a child of type %s"
535 (let ((ans t
)) ;; nil is valid
536 ;; Loop over all the elements of the input list, test
537 ;; each to make sure it is a child of the desired object class.
539 (setq ans
(and (eieio-object-p (car obj
))
540 (object-of-class-p (car obj
) ,cname
)))
541 (setq obj
(cdr obj
)))
544 ;; When using typep, (typep OBJ 'myclass) returns t for objects which
545 ;; are subclasses of myclass. For our predicates, however, it is
546 ;; important for EIEIO to be backwards compatible, where
547 ;; myobject-p, and myobject-child-p are different.
548 ;; "cl" uses this technique to specify symbols with specific typep
549 ;; test, so we can let typep have the CLOS documented behavior
550 ;; while keeping our above predicate clean.
552 ;; It would be cleaner to use `defsetf' here, but that requires cl
554 (put cname
'cl-deftype-handler
555 (list 'lambda
() `(list 'satisfies
(quote ,csym
)))))
557 ;; Before adding new slots, let's add all the methods and classes
558 ;; in from the parent class.
559 (eieio-copy-parents-into-subclass newc superclasses
)
561 ;; Store the new class vector definition into the symbol. We need to
562 ;; do this first so that we can call defmethod for the accessor.
563 ;; The vector will be updated by the following while loop and will not
564 ;; need to be stored a second time.
565 (put cname
'eieio-class-definition newc
)
567 ;; Query each slot in the declaration list and mangle into the
568 ;; class structure I have defined.
570 (let* ((slot1 (car slots
))
573 (acces (plist-get slot
':accessor
))
574 (init (or (plist-get slot
':initform
)
575 (if (member ':initform slot
) nil
577 (initarg (plist-get slot
':initarg
))
578 (docstr (plist-get slot
':documentation
))
579 (prot (plist-get slot
':protection
))
580 (reader (plist-get slot
':reader
))
581 (writer (plist-get slot
':writer
))
582 (alloc (plist-get slot
':allocation
))
583 (type (plist-get slot
':type
))
584 (custom (plist-get slot
':custom
))
585 (label (plist-get slot
':label
))
586 (customg (plist-get slot
':group
))
587 (printer (plist-get slot
':printer
))
589 (skip-nil (class-option-assoc options
:allow-nil-initform
))
592 (if eieio-error-unsupported-class-tags
595 (if (not (member (car tmp
) '(:accessor
610 (signal 'invalid-slot-type
(list (car tmp
))))
611 (setq tmp
(cdr (cdr tmp
))))))
613 ;; Clean up the meaning of protection.
614 (cond ((or (eq prot
'public
) (eq prot
:public
)) (setq prot nil
))
615 ((or (eq prot
'protected
) (eq prot
:protected
)) (setq prot
'protected
))
616 ((or (eq prot
'private
) (eq prot
:private
)) (setq prot
'private
))
618 (t (signal 'invalid-slot-type
(list ':protection prot
))))
620 ;; Make sure the :allocation parameter has a valid value.
621 (if (not (or (not alloc
) (eq alloc
:class
) (eq alloc
:instance
)))
622 (signal 'invalid-slot-type
(list ':allocation alloc
)))
624 ;; The default type specifier is supposed to be t, meaning anything.
625 (if (not type
) (setq type t
))
627 ;; Label is nil, or a string
628 (if (not (or (null label
) (stringp label
)))
629 (signal 'invalid-slot-type
(list ':label label
)))
631 ;; Is there an initarg, but allocation of class?
632 (if (and initarg
(eq alloc
:class
))
633 (message "Class allocated slots do not need :initarg"))
635 ;; intern the symbol so we can use it blankly
636 (if initarg
(set initarg initarg
))
638 ;; The customgroup should be a list of symbols
639 (cond ((null customg
)
640 (setq customg
'(default)))
641 ((not (listp customg
))
642 (setq customg
(list customg
))))
643 ;; The customgroup better be a symbol, or list of symbols.
645 (if (not (symbolp cg
))
646 (signal 'invalid-slot-type
(list ':group cg
))))
649 ;; First up, add this slot into our new class.
650 (eieio-add-new-slot newc name init docstr type custom label customg printer
651 prot initarg alloc
'defaultoverride skip-nil
)
653 ;; We need to id the group, and store them in a group list attribute.
654 (mapc (lambda (cg) (add-to-list 'groups cg
)) customg
)
656 ;; Anyone can have an accessor function. This creates a function
657 ;; of the specified name, and also performs a `defsetf' if applicable
658 ;; so that users can `setf' the space returned by this function.
662 acces
(if (eq alloc
:class
) :static
:primary
) cname
665 "Retrieves the slot `%s' from an object of class `%s'"
667 (if (slot-boundp this
',name
)
668 (eieio-oref this
',name
)
669 ;; Else - Some error? nil?
672 (if (fboundp 'gv-define-setter
)
673 ;; FIXME: We should move more of eieio-defclass into the
674 ;; defclass macro so we don't have to use `eval' and require
676 (eval `(gv-define-setter ,acces
(eieio--store eieio--object
)
677 (list 'eieio-oset eieio--object
'',name
679 ;; Provide a setf method. It would be cleaner to use
680 ;; defsetf, but that would require CL at runtime.
681 (put acces
'setf-method
683 (let* ((--widget-sym-- (make-symbol "--widget--"))
684 (--store-sym-- (make-symbol "--store--")))
686 (list --widget-sym--
)
689 (list 'eieio-oset --widget-sym--
'',name
691 (list 'getfoo --widget-sym--
))))))))
693 ;; If a writer is defined, then create a generic method of that
694 ;; name whose purpose is to set the value of the slot.
698 `(lambda (this value
)
699 ,(format "Set the slot `%s' of an object of class `%s'"
701 (setf (slot-value this
',name
) value
))))
702 ;; If a reader is defined, then create a generic method
703 ;; of that name whose purpose is to access this slot value.
708 ,(format "Access the slot `%s' from object of class `%s'"
710 (slot-value this
',name
))))
712 (setq slots
(cdr slots
)))
714 ;; Now that everything has been loaded up, all our lists are backwards!
716 (aset newc class-public-a
(nreverse (aref newc class-public-a
)))
717 (aset newc class-public-d
(nreverse (aref newc class-public-d
)))
718 (aset newc class-public-doc
(nreverse (aref newc class-public-doc
)))
719 (aset newc class-public-type
720 (apply 'vector
(nreverse (aref newc class-public-type
))))
721 (aset newc class-public-custom
(nreverse (aref newc class-public-custom
)))
722 (aset newc class-public-custom-label
(nreverse (aref newc class-public-custom-label
)))
723 (aset newc class-public-custom-group
(nreverse (aref newc class-public-custom-group
)))
724 (aset newc class-public-printer
(nreverse (aref newc class-public-printer
)))
725 (aset newc class-protection
(nreverse (aref newc class-protection
)))
726 (aset newc class-initarg-tuples
(nreverse (aref newc class-initarg-tuples
)))
728 ;; The storage for class-class-allocation-type needs to be turned into
730 (aset newc class-class-allocation-type
731 (apply 'vector
(aref newc class-class-allocation-type
)))
733 ;; Also, take class allocated values, and vectorize them for speed.
734 (aset newc class-class-allocation-values
735 (apply 'vector
(aref newc class-class-allocation-values
)))
737 ;; Attach slot symbols into an obarray, and store the index of
738 ;; this slot as the variable slot in this new symbol. We need to
739 ;; know about primes, because obarrays are best set in vectors of
740 ;; prime number length, and we also need to make our vector small
741 ;; to save space, and also optimal for the number of items we have.
743 (pubsyms (aref newc class-public-a
))
744 (prots (aref newc class-protection
))
746 (vl (let ((primes '( 3 5 7 11 13 17 19 23 29 31 37 41 43 47
747 53 59 61 67 71 73 79 83 89 97 101 )))
748 (while (and primes
(< (car primes
) l
))
749 (setq primes
(cdr primes
)))
751 (oa (make-vector vl
0))
754 (setq newsym
(intern (symbol-name (car pubsyms
)) oa
))
757 (if (car prots
) (put newsym
'protection
(car prots
)))
758 (setq pubsyms
(cdr pubsyms
)
760 (aset newc class-symbol-obarray oa
)
763 ;; Create the constructor function
764 (if (class-option-assoc options
:abstract
)
765 ;; Abstract classes cannot be instantiated. Say so.
766 (let ((abs (class-option-assoc options
:abstract
)))
767 (if (not (stringp abs
))
768 (setq abs
(format "Class %s is abstract" cname
)))
770 `(lambda (&rest stuff
)
771 ,(format "You cannot create a new object of type %s" cname
)
774 ;; Non-abstract classes need a constructor.
776 `(lambda (newname &rest slots
)
777 ,(format "Create a new object with name NAME of class type %s" cname
)
778 (apply 'constructor
,cname newname slots
)))
781 ;; Set up a specialized doc string.
782 ;; Use stored value since it is calculated in a non-trivial way
783 (put cname
'variable-documentation
784 (class-option-assoc options
:documentation
))
786 ;; Save the file location where this class is defined.
787 (let ((fname (if load-in-progress
792 (when (string-match "\\.elc$" fname
)
793 (setq fname
(substring fname
0 (1- (length fname
)))))
794 (put cname
'class-location fname
)))
796 ;; We have a list of custom groups. Store them into the options.
797 (let ((g (class-option-assoc options
:custom-groups
)))
798 (mapc (lambda (cg) (add-to-list 'g cg
)) groups
)
799 (if (memq :custom-groups options
)
800 (setcar (cdr (memq :custom-groups options
)) g
)
801 (setq options
(cons :custom-groups
(cons g options
)))))
803 ;; Set up the options we have collected.
804 (aset newc class-options options
)
806 ;; if this is a superclass, clear out parent (which was set to the
807 ;; default superclass eieio-default-superclass)
808 (if clearparent
(aset newc class-parent nil
))
810 ;; Create the cached default object.
811 (let ((cache (make-vector (+ (length (aref newc class-public-a
))
813 (aset cache
0 'object
)
814 (aset cache object-class cname
)
815 (aset cache object-name
'default-cache-object
)
816 (let ((eieio-skip-typecheck t
))
817 ;; All type-checking has been done to our satisfaction
818 ;; before this call. Don't waste our time in this call..
819 (eieio-set-defaults cache t
))
820 (aset newc class-default-object-cache cache
))
822 ;; Return our new class object
827 (defun eieio-perform-slot-validation-for-default (slot spec value skipnil
)
828 "For SLOT, signal if SPEC does not match VALUE.
829 If SKIPNIL is non-nil, then if VALUE is nil return t instead."
830 (if (and (not (eieio-eval-default-p value
))
831 (not eieio-skip-typecheck
)
832 (not (and skipnil
(null value
)))
833 (not (eieio-perform-slot-validation spec value
)))
834 (signal 'invalid-slot-type
(list slot spec value
))))
836 (defun eieio-add-new-slot (newc a d doc type cust label custg print prot init alloc
837 &optional defaultoverride skipnil
)
838 "Add into NEWC attribute A.
839 If A already exists in NEWC, then do nothing. If it doesn't exist,
840 then also add in D (default), DOC, TYPE, CUST, LABEL, CUSTG, PRINT, PROT, and INIT arg.
841 Argument ALLOC specifies if the slot is allocated per instance, or per class.
842 If optional DEFAULTOVERRIDE is non-nil, then if A exists in NEWC,
843 we must override its value for a default.
844 Optional argument SKIPNIL indicates if type checking should be skipped
845 if default value is nil."
846 ;; Make sure we duplicate those items that are sequences.
848 (if (sequencep d
) (setq d
(copy-sequence d
)))
849 ;; This copy can fail on a cons cell with a non-cons in the cdr. Let's skip it if it doesn't work.
851 (if (sequencep type
) (setq type
(copy-sequence type
)))
852 (if (sequencep cust
) (setq cust
(copy-sequence cust
)))
853 (if (sequencep custg
) (setq custg
(copy-sequence custg
)))
855 ;; To prevent override information w/out specification of storage,
856 ;; we need to do this little hack.
857 (if (member a
(aref newc class-class-allocation-a
)) (setq alloc
':class
))
859 (if (or (not alloc
) (and (symbolp alloc
) (eq alloc
':instance
)))
860 ;; In this case, we modify the INSTANCE version of a given slot.
864 ;; Only add this element if it is so-far unique
865 (if (not (member a
(aref newc class-public-a
)))
867 (eieio-perform-slot-validation-for-default a type d skipnil
)
868 (aset newc class-public-a
(cons a
(aref newc class-public-a
)))
869 (aset newc class-public-d
(cons d
(aref newc class-public-d
)))
870 (aset newc class-public-doc
(cons doc
(aref newc class-public-doc
)))
871 (aset newc class-public-type
(cons type
(aref newc class-public-type
)))
872 (aset newc class-public-custom
(cons cust
(aref newc class-public-custom
)))
873 (aset newc class-public-custom-label
(cons label
(aref newc class-public-custom-label
)))
874 (aset newc class-public-custom-group
(cons custg
(aref newc class-public-custom-group
)))
875 (aset newc class-public-printer
(cons print
(aref newc class-public-printer
)))
876 (aset newc class-protection
(cons prot
(aref newc class-protection
)))
877 (aset newc class-initarg-tuples
(cons (cons init a
) (aref newc class-initarg-tuples
)))
879 ;; When defaultoverride is true, we are usually adding new local
880 ;; attributes which must override the default value of any slot
881 ;; passed in by one of the parent classes.
882 (when defaultoverride
883 ;; There is a match, and we must override the old value.
884 (let* ((ca (aref newc class-public-a
))
886 (num (- (length ca
) (length np
)))
887 (dp (if np
(nthcdr num
(aref newc class-public-d
))
889 (tp (if np
(nth num
(aref newc class-public-type
))))
892 (error "EIEIO internal error overriding default value for %s"
894 ;; If type is passed in, is it the same?
895 (if (not (eq type t
))
896 (if (not (equal type tp
))
898 "Child slot type `%s' does not match inherited type `%s' for `%s'"
900 ;; If we have a repeat, only update the initarg...
901 (unless (eq d eieio-unbound
)
902 (eieio-perform-slot-validation-for-default a tp d skipnil
)
904 ;; If we have a new initarg, check for it.
906 (let* ((inits (aref newc class-initarg-tuples
))
907 (inita (rassq a inits
)))
908 ;; Replace the CAR of the associate INITA.
909 ;;(message "Initarg: %S replace %s" inita init)
913 ;; PLN Tue Jun 26 11:57:06 2007 : The protection is
914 ;; checked and SHOULD match the superclass
915 ;; protection. Otherwise an error is thrown. However
916 ;; I wonder if a more flexible schedule might be
919 ;; EML - We used to have (if prot... here,
920 ;; but a prot of 'nil means public.
922 (let ((super-prot (nth num
(aref newc class-protection
)))
924 (if (not (eq prot super-prot
))
925 (error "Child slot protection `%s' does not match inherited protection `%s' for `%s'"
929 ;; PLN Tue Jun 26 11:57:06 2007 :
930 ;; Do a non redundant combination of ancient custom
931 ;; groups and new ones.
934 (nthcdr num
(aref newc class-public-custom-group
)))
936 (list2 (if (listp custg
) custg
(list custg
))))
937 (if (< (length list1
) (length list2
))
938 (setq list1
(prog1 list2
(setq list2 list1
))))
940 (unless (memq elt list1
)
942 (setcar groups list1
)))
945 ;; PLN Mon Jun 25 22:44:34 2007 : If a new cust is
946 ;; set, simply replaces the old one.
948 ;; (message "Custom type redefined to %s" cust)
949 (setcar (nthcdr num
(aref newc class-public-custom
)) cust
))
951 ;; If a new label is specified, it simply replaces
954 ;; (message "Custom label redefined to %s" label)
955 (setcar (nthcdr num
(aref newc class-public-custom-label
)) label
))
958 ;; PLN Sat Jun 30 17:24:42 2007 : when a new
959 ;; doc is specified, simply replaces the old one.
961 ;;(message "Documentation redefined to %s" doc)
962 (setcar (nthcdr num
(aref newc class-public-doc
))
966 ;; If a new printer is specified, it simply replaces
969 ;; (message "printer redefined to %s" print)
970 (setcar (nthcdr num
(aref newc class-public-printer
)) print
))
975 ;; CLASS ALLOCATED SLOTS
976 (let ((value (eieio-default-eval-maybe d
)))
977 (if (not (member a
(aref newc class-class-allocation-a
)))
979 (eieio-perform-slot-validation-for-default a type value skipnil
)
980 ;; Here we have found a :class version of a slot. This
981 ;; requires a very different approach.
982 (aset newc class-class-allocation-a
(cons a
(aref newc class-class-allocation-a
)))
983 (aset newc class-class-allocation-doc
(cons doc
(aref newc class-class-allocation-doc
)))
984 (aset newc class-class-allocation-type
(cons type
(aref newc class-class-allocation-type
)))
985 (aset newc class-class-allocation-custom
(cons cust
(aref newc class-class-allocation-custom
)))
986 (aset newc class-class-allocation-custom-label
(cons label
(aref newc class-class-allocation-custom-label
)))
987 (aset newc class-class-allocation-custom-group
(cons custg
(aref newc class-class-allocation-custom-group
)))
988 (aset newc class-class-allocation-protection
(cons prot
(aref newc class-class-allocation-protection
)))
989 ;; Default value is stored in the 'values section, since new objects
990 ;; can't initialize from this element.
991 (aset newc class-class-allocation-values
(cons value
(aref newc class-class-allocation-values
))))
992 (when defaultoverride
993 ;; There is a match, and we must override the old value.
994 (let* ((ca (aref newc class-class-allocation-a
))
996 (num (- (length ca
) (length np
)))
999 (aref newc class-class-allocation-values
))
1001 (tp (if np
(nth num
(aref newc class-class-allocation-type
))
1004 (error "EIEIO internal error overriding default value for %s"
1006 ;; If type is passed in, is it the same?
1007 (if (not (eq type t
))
1008 (if (not (equal type tp
))
1010 "Child slot type `%s' does not match inherited type `%s' for `%s'"
1012 ;; EML - Note: the only reason to override a class bound slot
1013 ;; is to change the default, so allow unbound in.
1015 ;; If we have a repeat, only update the value...
1016 (eieio-perform-slot-validation-for-default a tp value skipnil
)
1019 ;; PLN Tue Jun 26 11:57:06 2007 : The protection is
1020 ;; checked and SHOULD match the superclass
1021 ;; protection. Otherwise an error is thrown. However
1022 ;; I wonder if a more flexible schedule might be
1025 (car (nthcdr num
(aref newc class-class-allocation-protection
)))))
1026 (if (not (eq prot super-prot
))
1027 (error "Child slot protection `%s' does not match inherited protection `%s' for `%s'"
1028 prot super-prot a
)))
1029 ;; Do a non redundant combination of ancient custom groups
1033 (nthcdr num
(aref newc class-class-allocation-custom-group
)))
1034 (list1 (car groups
))
1035 (list2 (if (listp custg
) custg
(list custg
))))
1036 (if (< (length list1
) (length list2
))
1037 (setq list1
(prog1 list2
(setq list2 list1
))))
1039 (unless (memq elt list1
)
1041 (setcar groups list1
)))
1043 ;; PLN Sat Jun 30 17:24:42 2007 : when a new
1044 ;; doc is specified, simply replaces the old one.
1046 ;;(message "Documentation redefined to %s" doc)
1047 (setcar (nthcdr num
(aref newc class-class-allocation-doc
))
1051 ;; If a new printer is specified, it simply replaces
1054 ;; (message "printer redefined to %s" print)
1055 (setcar (nthcdr num
(aref newc class-class-allocation-printer
)) print
))
1061 (defun eieio-copy-parents-into-subclass (newc parents
)
1062 "Copy into NEWC the slots of PARENTS.
1063 Follow the rules of not overwriting early parents when applying to
1064 the new child class."
1065 (let ((ps (aref newc class-parent
))
1066 (sn (class-option-assoc (aref newc class-options
)
1067 ':allow-nil-initform
)))
1069 ;; First, duplicate all the slots of the parent.
1070 (let ((pcv (class-v (car ps
))))
1071 (let ((pa (aref pcv class-public-a
))
1072 (pd (aref pcv class-public-d
))
1073 (pdoc (aref pcv class-public-doc
))
1074 (ptype (aref pcv class-public-type
))
1075 (pcust (aref pcv class-public-custom
))
1076 (plabel (aref pcv class-public-custom-label
))
1077 (pcustg (aref pcv class-public-custom-group
))
1078 (printer (aref pcv class-public-printer
))
1079 (pprot (aref pcv class-protection
))
1080 (pinit (aref pcv class-initarg-tuples
))
1083 (eieio-add-new-slot newc
1084 (car pa
) (car pd
) (car pdoc
) (aref ptype i
)
1085 (car pcust
) (car plabel
) (car pcustg
)
1087 (car pprot
) (car-safe (car pinit
)) nil nil sn
)
1088 ;; Increment each value.
1096 printer
(cdr printer
)
1100 ;; Now duplicate all the class alloc slots.
1101 (let ((pa (aref pcv class-class-allocation-a
))
1102 (pdoc (aref pcv class-class-allocation-doc
))
1103 (ptype (aref pcv class-class-allocation-type
))
1104 (pcust (aref pcv class-class-allocation-custom
))
1105 (plabel (aref pcv class-class-allocation-custom-label
))
1106 (pcustg (aref pcv class-class-allocation-custom-group
))
1107 (printer (aref pcv class-class-allocation-printer
))
1108 (pprot (aref pcv class-class-allocation-protection
))
1109 (pval (aref pcv class-class-allocation-values
))
1112 (eieio-add-new-slot newc
1113 (car pa
) (aref pval i
) (car pdoc
) (aref ptype i
)
1114 (car pcust
) (car plabel
) (car pcustg
)
1116 (car pprot
) nil
':class sn
)
1117 ;; Increment each value.
1123 printer
(cdr printer
)
1127 ;; Loop over each parent class
1131 ;;; CLOS style implementation of object creators.
1133 (defun make-instance (class &rest initargs
)
1134 "Make a new instance of CLASS based on INITARGS.
1135 CLASS is a class symbol. For example:
1137 (make-instance 'foo)
1139 INITARGS is a property list with keywords based on the :initarg
1140 for each slot. For example:
1142 (make-instance 'foo :slot1 value1 :slotN valueN)
1146 If the first element of INITARGS is a string, it is used as the
1149 In EIEIO, the class' constructor requires a name for use when printing.
1150 `make-instance' in CLOS doesn't use names the way Emacs does, so the
1151 class is used as the name slot instead when INITARGS doesn't start with
1153 (if (and (car initargs
) (stringp (car initargs
)))
1154 (apply (class-constructor class
) initargs
)
1155 (apply (class-constructor class
)
1156 (cond ((symbolp class
) (symbol-name class
))
1157 (t (format "%S" class
)))
1161 ;;; CLOS methods and generics
1164 (put 'eieio--defalias
'byte-hunk-handler
1165 #'byte-compile-file-form-defalias
) ;;(get 'defalias 'byte-hunk-handler)
1166 (defun eieio--defalias (name body
)
1167 "Like `defalias', but with less side-effects.
1168 More specifically, it has no side-effects at all when the new function
1169 definition is the same (`eq') as the old one."
1170 (unless (and (fboundp name
)
1171 (eq (symbol-function name
) body
))
1172 (defalias name body
)))
1174 (defmacro defgeneric
(method args
&optional doc-string
)
1175 "Create a generic function METHOD.
1176 DOC-STRING is the base documentation for this class. A generic
1177 function has no body, as its purpose is to decide which method body
1178 is appropriate to use. Uses `defmethod' to create methods, and calls
1179 `defgeneric' for you. With this implementation the ARGS are
1180 currently ignored. You can use `defgeneric' to apply specialized
1181 top level documentation to a method."
1182 `(eieio--defalias ',method
1183 (eieio--defgeneric-init-form ',method
,doc-string
)))
1185 (defun eieio--defgeneric-init-form (method doc-string
)
1186 "Form to use for the initial definition of a generic."
1188 ((or (not (fboundp method
))
1189 (eq 'autoload
(car-safe (symbol-function method
))))
1190 ;; Make sure the method tables are installed.
1191 (eieiomt-install method
)
1192 ;; Construct the actual body of this function.
1193 (eieio-defgeneric-form method doc-string
))
1194 ((generic-p method
) (symbol-function method
)) ;Leave it as-is.
1195 (t (error "You cannot create a generic/method over an existing symbol: %s"
1198 (defun eieio-defgeneric-form (method doc-string
)
1199 "The lambda form that would be used as the function defined on METHOD.
1200 All methods should call the same EIEIO function for dispatch.
1201 DOC-STRING is the documentation attached to METHOD."
1202 `(lambda (&rest local-args
)
1204 (eieio-generic-call (quote ,method
) local-args
)))
1206 (defsubst eieio-defgeneric-reset-generic-form
(method)
1207 "Setup METHOD to call the generic form."
1208 (let ((doc-string (documentation method
)))
1209 (fset method
(eieio-defgeneric-form method doc-string
))))
1211 (defun eieio-defgeneric-form-primary-only (method doc-string
)
1212 "The lambda form that would be used as the function defined on METHOD.
1213 All methods should call the same EIEIO function for dispatch.
1214 DOC-STRING is the documentation attached to METHOD."
1215 `(lambda (&rest local-args
)
1217 (eieio-generic-call-primary-only (quote ,method
) local-args
)))
1219 (defsubst eieio-defgeneric-reset-generic-form-primary-only
(method)
1220 "Setup METHOD to call the generic form."
1221 (let ((doc-string (documentation method
)))
1222 (fset method
(eieio-defgeneric-form-primary-only method doc-string
))))
1224 (defun eieio-defgeneric-form-primary-only-one (method doc-string
1228 "The lambda form that would be used as the function defined on METHOD.
1229 All methods should call the same EIEIO function for dispatch.
1230 DOC-STRING is the documentation attached to METHOD.
1231 CLASS is the class symbol needed for private method access.
1232 IMPL is the symbol holding the method implementation."
1233 ;; NOTE: I tried out byte compiling this little fcn. Turns out it
1234 ;; is faster to execute this for not byte-compiled. ie, install this,
1235 ;; then measure calls going through here. I wonder why.
1237 (let ((byte-compile-warnings nil
))
1239 `(lambda (&rest local-args
)
1241 ;; This is a cool cheat. Usually we need to look up in the
1242 ;; method table to find out if there is a method or not. We can
1243 ;; instead make that determination at load time when there is
1244 ;; only one method. If the first arg is not a child of the class
1245 ;; of that one implementation, then clearly, there is no method def.
1246 (if (not (eieio-object-p (car local-args
)))
1247 ;; Not an object. Just signal.
1248 (signal 'no-method-definition
1249 (list ',method local-args
))
1251 ;; We do have an object. Make sure it is the right type.
1252 (if ,(if (eq class eieio-default-superclass
)
1253 nil
; default superclass means just an obj. Already asked.
1254 `(not (child-of-class-p (aref (car local-args
) object-class
)
1257 ;; If not the right kind of object, call no applicable
1258 (apply 'no-applicable-method
(car local-args
)
1259 ',method local-args
)
1261 ;; It is ok, do the call.
1262 ;; Fill in inter-call variables then evaluate the method.
1263 (let ((scoped-class ',class
)
1264 (eieio-generic-call-next-method-list nil
)
1265 (eieio-generic-call-key method-primary
)
1266 (eieio-generic-call-methodname ',method
)
1267 (eieio-generic-call-arglst local-args
)
1269 ,(if (< emacs-major-version
24)
1270 `(apply ,(list 'quote impl
) local-args
)
1271 `(apply #',impl local-args
))
1275 (defsubst eieio-defgeneric-reset-generic-form-primary-only-one
(method)
1276 "Setup METHOD to call the generic form."
1277 (let* ((doc-string (documentation method
))
1278 (M (get method
'eieio-method-tree
))
1279 (entry (car (aref M method-primary
)))
1281 (fset method
(eieio-defgeneric-form-primary-only-one
1287 (defun eieio-unbind-method-implementations (method)
1288 "Make the generic method METHOD have no implementations.
1289 It will leave the original generic function in place,
1290 but remove reference to all implementations of METHOD."
1291 (put method
'eieio-method-tree nil
)
1292 (put method
'eieio-method-obarray nil
))
1294 (defmacro defmethod
(method &rest args
)
1295 "Create a new METHOD through `defgeneric' with ARGS.
1297 The optional second argument KEY is a specifier that
1298 modifies how the method is called, including:
1299 :before - Method will be called before the :primary
1300 :primary - The default if not specified
1301 :after - Method will be called after the :primary
1302 :static - First arg could be an object or class
1303 The next argument is the ARGLIST. The ARGLIST specifies the arguments
1304 to the method as with `defun'. The first argument can have a type
1306 ((VARNAME CLASS) ARG2 ...)
1307 where VARNAME is the name of the local variable for the method being
1308 created. The CLASS is a class symbol for a class made with `defclass'.
1309 A DOCSTRING comes after the ARGLIST, and is optional.
1310 All the rest of the args are the BODY of the method. A method will
1311 return the value of the last form in the BODY.
1315 (defmethod mymethod [:before | :primary | :after | :static]
1316 ((typearg class-name) arg2 &optional opt &rest rest)
1319 (let* ((key (if (keywordp (car args
)) (pop args
)))
1322 (fargs (if (consp arg1
)
1323 (cons (car arg1
) (cdr params
))
1325 (class (if (consp arg1
) (nth 1 arg1
)))
1326 (code `(lambda ,fargs
,@(cdr args
))))
1328 ;; Make sure there is a generic and the byte-compiler sees it.
1329 (defgeneric ,method
,args
1330 ,(or (documentation code
)
1331 (format "Generically created method `%s'." method
)))
1332 (eieio--defmethod ',method
',key
',class
#',code
))))
1334 (defun eieio--defmethod (method kind argclass code
)
1335 "Work part of the `defmethod' macro defining METHOD with ARGS."
1337 ;; find optional keys
1338 (cond ((or (eq ':BEFORE kind
)
1341 ((or (eq ':AFTER kind
)
1344 ((or (eq ':PRIMARY kind
)
1345 (eq ':primary kind
))
1347 ((or (eq ':STATIC kind
)
1351 (t method-primary
))))
1352 ;; Make sure there is a generic (when called from defclass).
1354 method
(eieio--defgeneric-init-form
1355 method
(or (documentation code
)
1356 (format "Generically created method `%s'." method
))))
1357 ;; create symbol for property to bind to. If the first arg is of
1358 ;; the form (varname vartype) and `vartype' is a class, then
1359 ;; that class will be the type symbol. If not, then it will fall
1360 ;; under the type `primary' which is a non-specific calling of the
1363 (if (not (class-p argclass
))
1364 (error "Unknown class type %s in method parameters"
1367 (signal 'wrong-type-argument
(list :static
'non-class-arg
)))
1368 ;; generics are higher
1369 (setq key
(eieio-specialized-key-to-generic-key key
)))
1370 ;; Put this lambda into the symbol so we can find it
1371 (eieiomt-add method code key argclass
)
1374 (when eieio-optimize-primary-methods-flag
1377 ;; If this method, after this setup, only has primary methods, then
1378 ;; we can setup the generic that way.
1379 (if (generic-primary-only-p method
)
1380 ;; If there is only one primary method, then we can go one more
1381 ;; optimization step.
1382 (if (generic-primary-only-one-p method
)
1383 (eieio-defgeneric-reset-generic-form-primary-only-one method
)
1384 (eieio-defgeneric-reset-generic-form-primary-only method
))
1385 (eieio-defgeneric-reset-generic-form method
)))
1389 ;;; Slot type validation
1391 ;; This is a hideous hack for replacing `typep' from cl-macs, to avoid
1392 ;; requiring the CL library at run-time. It can be eliminated if/when
1393 ;; `typep' is merged into Emacs core.
1394 (defun eieio--typep (val type
)
1396 (cond ((get type
'cl-deftype-handler
)
1397 (eieio--typep val
(funcall (get type
'cl-deftype-handler
))))
1399 ((eq type
'null
) (null val
))
1400 ((eq type
'atom
) (atom val
))
1401 ((eq type
'float
) (and (numberp val
) (not (integerp val
))))
1402 ((eq type
'real
) (numberp val
))
1403 ((eq type
'fixnum
) (integerp val
))
1404 ((memq type
'(character string-char
)) (characterp val
))
1406 (let* ((name (symbol-name type
))
1407 (namep (intern (concat name
"p"))))
1409 (funcall `(lambda () (,namep val
)))
1410 (funcall `(lambda ()
1411 (,(intern (concat name
"-p")) val
)))))))
1412 (cond ((get (car type
) 'cl-deftype-handler
)
1413 (eieio--typep val
(apply (get (car type
) 'cl-deftype-handler
)
1415 ((memq (car type
) '(integer float real number
))
1416 (and (eieio--typep val
(car type
))
1417 (or (memq (cadr type
) '(* nil
))
1418 (if (consp (cadr type
))
1419 (> val
(car (cadr type
)))
1420 (>= val
(cadr type
))))
1421 (or (memq (caddr type
) '(* nil
))
1422 (if (consp (car (cddr type
)))
1423 (< val
(caar (cddr type
)))
1424 (<= val
(car (cddr type
)))))))
1425 ((memq (car type
) '(and or not
))
1426 (eval (cons (car type
)
1428 `(eieio--typep (quote ,val
) (quote ,x
)))
1430 ((memq (car type
) '(member member
*))
1431 (memql val
(cdr type
)))
1432 ((eq (car type
) 'satisfies
)
1433 (funcall `(lambda () (,(cadr type
) val
))))
1434 (t (error "Bad type spec: %s" type
)))))
1436 (defun eieio-perform-slot-validation (spec value
)
1437 "Return non-nil if SPEC does not match VALUE."
1438 (or (eq spec t
) ; t always passes
1439 (eq value eieio-unbound
) ; unbound always passes
1440 (eieio--typep value spec
)))
1442 (defun eieio-validate-slot-value (class slot-idx value slot
)
1443 "Make sure that for CLASS referencing SLOT-IDX, VALUE is valid.
1444 Checks the :type specifier.
1445 SLOT is the slot that is being checked, and is only used when throwing
1447 (if eieio-skip-typecheck
1449 ;; Trim off object IDX junk added in for the object index.
1450 (setq slot-idx
(- slot-idx
3))
1451 (let ((st (aref (aref (class-v class
) class-public-type
) slot-idx
)))
1452 (if (not (eieio-perform-slot-validation st value
))
1453 (signal 'invalid-slot-type
(list class slot st value
))))))
1455 (defun eieio-validate-class-slot-value (class slot-idx value slot
)
1456 "Make sure that for CLASS referencing SLOT-IDX, VALUE is valid.
1457 Checks the :type specifier.
1458 SLOT is the slot that is being checked, and is only used when throwing
1460 (if eieio-skip-typecheck
1462 (let ((st (aref (aref (class-v class
) class-class-allocation-type
)
1464 (if (not (eieio-perform-slot-validation st value
))
1465 (signal 'invalid-slot-type
(list class slot st value
))))))
1467 (defun eieio-barf-if-slot-unbound (value instance slotname fn
)
1468 "Throw a signal if VALUE is a representation of an UNBOUND slot.
1469 INSTANCE is the object being referenced. SLOTNAME is the offending
1470 slot. If the slot is ok, return VALUE.
1471 Argument FN is the function calling this verifier."
1472 (if (and (eq value eieio-unbound
) (not eieio-skip-typecheck
))
1473 (slot-unbound instance
(object-class instance
) slotname fn
)
1476 ;;; Get/Set slots in an object.
1478 (defmacro oref
(obj slot
)
1479 "Retrieve the value stored in OBJ in the slot named by SLOT.
1480 Slot is the name of the slot when created by `defclass' or the label
1481 created by the :initarg tag."
1482 `(eieio-oref ,obj
(quote ,slot
)))
1484 (defun eieio-oref (obj slot
)
1485 "Return the value in OBJ at SLOT in the object vector."
1486 (if (not (or (eieio-object-p obj
) (class-p obj
)))
1487 (signal 'wrong-type-argument
(list '(or eieio-object-p class-p
) obj
)))
1488 (if (not (symbolp slot
))
1489 (signal 'wrong-type-argument
(list 'symbolp slot
)))
1490 (if (class-p obj
) (eieio-class-un-autoload obj
))
1491 (let* ((class (if (class-p obj
) obj
(aref obj object-class
)))
1492 (c (eieio-slot-name-index class obj slot
)))
1494 ;; It might be missing because it is a :class allocated slot.
1495 ;; Let's check that info out.
1496 (if (setq c
(eieio-class-slot-name-index class slot
))
1498 (aref (aref (class-v class
) class-class-allocation-values
) c
)
1499 ;; The slot-missing method is a cool way of allowing an object author
1500 ;; to intercept missing slot definitions. Since it is also the LAST
1501 ;; thing called in this fn, its return value would be retrieved.
1502 (slot-missing obj slot
'oref
)
1503 ;;(signal 'invalid-slot-name (list (object-name obj) slot))
1505 (if (not (eieio-object-p obj
))
1506 (signal 'wrong-type-argument
(list 'eieio-object-p obj
)))
1507 (eieio-barf-if-slot-unbound (aref obj c
) obj slot
'oref
))))
1509 (defalias 'slot-value
'eieio-oref
)
1510 (defalias 'set-slot-value
'eieio-oset
)
1512 (defmacro oref-default
(obj slot
)
1513 "Get the default value of OBJ (maybe a class) for SLOT.
1514 The default value is the value installed in a class with the :initform
1515 tag. SLOT can be the slot name, or the tag specified by the :initarg
1516 tag in the `defclass' call."
1517 `(eieio-oref-default ,obj
(quote ,slot
)))
1519 (defun eieio-oref-default (obj slot
)
1520 "Do the work for the macro `oref-default' with similar parameters.
1521 Fills in OBJ's SLOT with its default value."
1522 (if (not (or (eieio-object-p obj
) (class-p obj
))) (signal 'wrong-type-argument
(list 'eieio-object-p obj
)))
1523 (if (not (symbolp slot
)) (signal 'wrong-type-argument
(list 'symbolp slot
)))
1524 (let* ((cl (if (eieio-object-p obj
) (aref obj object-class
) obj
))
1525 (c (eieio-slot-name-index cl obj slot
)))
1527 ;; It might be missing because it is a :class allocated slot.
1528 ;; Let's check that info out.
1530 (eieio-class-slot-name-index cl slot
))
1532 (aref (aref (class-v cl
) class-class-allocation-values
)
1534 (slot-missing obj slot
'oref-default
)
1535 ;;(signal 'invalid-slot-name (list (class-name cl) slot))
1537 (eieio-barf-if-slot-unbound
1538 (let ((val (nth (- c
3) (aref (class-v cl
) class-public-d
))))
1539 (eieio-default-eval-maybe val
))
1540 obj cl
'oref-default
))))
1542 (defsubst eieio-eval-default-p
(val)
1543 "Whether the default value VAL should be evaluated for use."
1544 (and (consp val
) (symbolp (car val
)) (fboundp (car val
))))
1546 (defun eieio-default-eval-maybe (val)
1547 "Check VAL, and return what `oref-default' would provide."
1549 ;; Is it a function call? If so, evaluate it.
1550 ((eieio-eval-default-p val
)
1552 ;;;; check for quoted things, and unquote them
1553 ;;((and (consp val) (eq (car val) 'quote))
1555 ;; return it verbatim
1558 ;;; Object Set macros
1560 (defmacro oset
(obj slot value
)
1561 "Set the value in OBJ for slot SLOT to VALUE.
1562 SLOT is the slot name as specified in `defclass' or the tag created
1563 with in the :initarg slot. VALUE can be any Lisp object."
1564 `(eieio-oset ,obj
(quote ,slot
) ,value
))
1566 (defun eieio-oset (obj slot value
)
1567 "Do the work for the macro `oset'.
1568 Fills in OBJ's SLOT with VALUE."
1569 (if (not (eieio-object-p obj
)) (signal 'wrong-type-argument
(list 'eieio-object-p obj
)))
1570 (if (not (symbolp slot
)) (signal 'wrong-type-argument
(list 'symbolp slot
)))
1571 (let ((c (eieio-slot-name-index (object-class-fast obj
) obj slot
)))
1573 ;; It might be missing because it is a :class allocated slot.
1574 ;; Let's check that info out.
1576 (eieio-class-slot-name-index (aref obj object-class
) slot
))
1579 (eieio-validate-class-slot-value (object-class-fast obj
) c value slot
)
1580 (aset (aref (class-v (aref obj object-class
))
1581 class-class-allocation-values
)
1583 ;; See oref for comment on `slot-missing'
1584 (slot-missing obj slot
'oset value
)
1585 ;;(signal 'invalid-slot-name (list (object-name obj) slot))
1587 (eieio-validate-slot-value (object-class-fast obj
) c value slot
)
1588 (aset obj c value
))))
1590 (defmacro oset-default
(class slot value
)
1591 "Set the default slot in CLASS for SLOT to VALUE.
1592 The default value is usually set with the :initform tag during class
1593 creation. This allows users to change the default behavior of classes
1594 after they are created."
1595 `(eieio-oset-default ,class
(quote ,slot
) ,value
))
1597 (defun eieio-oset-default (class slot value
)
1598 "Do the work for the macro `oset-default'.
1599 Fills in the default value in CLASS' in SLOT with VALUE."
1600 (if (not (class-p class
)) (signal 'wrong-type-argument
(list 'class-p class
)))
1601 (if (not (symbolp slot
)) (signal 'wrong-type-argument
(list 'symbolp slot
)))
1602 (let* ((scoped-class class
)
1603 (c (eieio-slot-name-index class nil slot
)))
1605 ;; It might be missing because it is a :class allocated slot.
1606 ;; Let's check that info out.
1607 (if (setq c
(eieio-class-slot-name-index class slot
))
1610 (eieio-validate-class-slot-value class c value slot
)
1611 (aset (aref (class-v class
) class-class-allocation-values
) c
1613 (signal 'invalid-slot-name
(list (class-name class
) slot
)))
1614 (eieio-validate-slot-value class c value slot
)
1615 ;; Set this into the storage for defaults.
1616 (setcar (nthcdr (- c
3) (aref (class-v class
) class-public-d
))
1618 ;; Take the value, and put it into our cache object.
1619 (eieio-oset (aref (class-v class
) class-default-object-cache
)
1623 ;;; Handy CLOS macros
1625 (defmacro with-slots
(spec-list object
&rest body
)
1626 "Bind SPEC-LIST lexically to slot values in OBJECT, and execute BODY.
1627 This establishes a lexical environment for referring to the slots in
1628 the instance named by the given slot-names as though they were
1629 variables. Within such a context the value of the slot can be
1630 specified by using its slot name, as if it were a lexically bound
1631 variable. Both setf and setq can be used to set the value of the
1634 SPEC-LIST is of a form similar to `let'. For example:
1641 Where each VAR is the local variable given to the associated
1642 SLOT. A slot specified without a variable name is given a
1643 variable name of the same name as the slot."
1644 (declare (indent 2))
1645 ;; Transform the spec-list into a symbol-macrolet spec-list.
1646 (let ((mappings (mapcar (lambda (entry)
1647 (let ((var (if (listp entry
) (car entry
) entry
))
1648 (slot (if (listp entry
) (cadr entry
) entry
)))
1649 (list var
`(slot-value ,object
',slot
))))
1651 (append (list 'symbol-macrolet mappings
)
1654 ;;; Simple generators, and query functions. None of these would do
1655 ;; well embedded into an object.
1657 (defmacro object-class-fast
(obj) "Return the class struct defining OBJ with no check."
1658 `(aref ,obj object-class
))
1660 (defun class-name (class) "Return a Lisp like symbol name for CLASS."
1661 (if (not (class-p class
)) (signal 'wrong-type-argument
(list 'class-p class
)))
1662 ;; I think this is supposed to return a symbol, but to me CLASS is a symbol,
1663 ;; and I wanted a string. Arg!
1664 (format "#<class %s>" (symbol-name class
)))
1666 (defun object-name (obj &optional extra
)
1667 "Return a Lisp like symbol string for object OBJ.
1668 If EXTRA, include that in the string returned to represent the symbol."
1669 (if (not (eieio-object-p obj
)) (signal 'wrong-type-argument
(list 'eieio-object-p obj
)))
1670 (format "#<%s %s%s>" (symbol-name (object-class-fast obj
))
1671 (aref obj object-name
) (or extra
"")))
1673 (defun object-name-string (obj) "Return a string which is OBJ's name."
1674 (if (not (eieio-object-p obj
)) (signal 'wrong-type-argument
(list 'eieio-object-p obj
)))
1675 (aref obj object-name
))
1677 (defun object-set-name-string (obj name
) "Set the string which is OBJ's NAME."
1678 (if (not (eieio-object-p obj
)) (signal 'wrong-type-argument
(list 'eieio-object-p obj
)))
1679 (if (not (stringp name
)) (signal 'wrong-type-argument
(list 'stringp name
)))
1680 (aset obj object-name name
))
1682 (defun object-class (obj) "Return the class struct defining OBJ."
1683 (if (not (eieio-object-p obj
)) (signal 'wrong-type-argument
(list 'eieio-object-p obj
)))
1684 (object-class-fast obj
))
1685 (defalias 'class-of
'object-class
)
1687 (defun object-class-name (obj) "Return a Lisp like symbol name for OBJ's class."
1688 (if (not (eieio-object-p obj
)) (signal 'wrong-type-argument
(list 'eieio-object-p obj
)))
1689 (class-name (object-class-fast obj
)))
1691 (defmacro class-parents-fast
(class) "Return parent classes to CLASS with no check."
1692 `(aref (class-v ,class
) class-parent
))
1694 (defun class-parents (class)
1695 "Return parent classes to CLASS. (overload of variable).
1697 The CLOS function `class-direct-superclasses' is aliased to this function."
1698 (if (not (class-p class
)) (signal 'wrong-type-argument
(list 'class-p class
)))
1699 (class-parents-fast class
))
1701 (defmacro class-children-fast
(class) "Return child classes to CLASS with no check."
1702 `(aref (class-v ,class
) class-children
))
1704 (defun class-children (class)
1705 "Return child classes to CLASS.
1707 The CLOS function `class-direct-subclasses' is aliased to this function."
1708 (if (not (class-p class
)) (signal 'wrong-type-argument
(list 'class-p class
)))
1709 (class-children-fast class
))
1711 (defun eieio-c3-candidate (class remaining-inputs
)
1712 "Returns CLASS if it can go in the result now, otherwise nil"
1713 ;; Ensure CLASS is not in any position but the first in any of the
1714 ;; element lists of REMAINING-INPUTS.
1715 (and (not (let ((found nil
))
1716 (while (and remaining-inputs
(not found
))
1717 (setq found
(member class
(cdr (car remaining-inputs
)))
1718 remaining-inputs
(cdr remaining-inputs
)))
1722 (defun eieio-c3-merge-lists (reversed-partial-result remaining-inputs
)
1723 "Merge REVERSED-PARTIAL-RESULT REMAINING-INPUTS in a consistent order, if possible.
1724 If a consistent order does not exist, signal an error."
1725 (if (let ((tail remaining-inputs
)
1727 (while (and tail
(not found
))
1728 (setq found
(car tail
) tail
(cdr tail
)))
1730 ;; If all remaining inputs are empty lists, we are done.
1731 (nreverse reversed-partial-result
)
1732 ;; Otherwise, we try to find the next element of the result. This
1733 ;; is achieved by considering the first element of each
1734 ;; (non-empty) input list and accepting a candidate if it is
1735 ;; consistent with the rests of the input lists.
1737 (tail remaining-inputs
)
1739 (while (and tail
(not found
))
1740 (setq found
(and (car tail
)
1741 (eieio-c3-candidate (caar tail
)
1746 ;; The graph is consistent so far, add NEXT to result and
1747 ;; merge input lists, dropping NEXT from their heads where
1749 (eieio-c3-merge-lists
1750 (cons next reversed-partial-result
)
1751 (mapcar (lambda (l) (if (eq (first l
) next
) (rest l
) l
))
1753 ;; The graph is inconsistent, give up
1754 (signal 'inconsistent-class-hierarchy
(list remaining-inputs
))))))
1756 (defun eieio-class-precedence-dfs (class)
1757 "Return all parents of CLASS in depth-first order."
1758 (let* ((parents (class-parents-fast class
))
1759 (classes (copy-sequence
1766 (eieio-class-precedence-dfs parent
)))
1768 '((eieio-default-superclass))))))
1770 ;; Remove duplicates.
1772 (setcdr tail
(delq (car tail
) (cdr tail
)))
1773 (setq tail
(cdr tail
)))
1776 (defun eieio-class-precedence-bfs (class)
1777 "Return all parents of CLASS in breadth-first order."
1779 (queue (or (class-parents-fast class
)
1780 '(eieio-default-superclass))))
1782 (let ((head (pop queue
)))
1783 (unless (member head result
)
1785 (unless (eq head
'eieio-default-superclass
)
1786 (setq queue
(append queue
(or (class-parents-fast head
)
1787 '(eieio-default-superclass))))))))
1788 (cons class
(nreverse result
)))
1791 (defun eieio-class-precedence-c3 (class)
1792 "Return all parents of CLASS in c3 order."
1793 (let ((parents (class-parents-fast class
)))
1794 (eieio-c3-merge-lists
1800 (eieio-class-precedence-c3 x
))
1802 '((eieio-default-superclass)))
1806 (defun class-precedence-list (class)
1807 "Return (transitively closed) list of parents of CLASS.
1808 The order, in which the parents are returned depends on the
1809 method invocation orders of the involved classes."
1810 (if (or (null class
) (eq class
'eieio-default-superclass
))
1812 (case (class-method-invocation-order class
)
1814 (eieio-class-precedence-dfs class
))
1816 (eieio-class-precedence-bfs class
))
1818 (eieio-class-precedence-c3 class
))))
1821 ;; Official CLOS functions.
1822 (defalias 'class-direct-superclasses
'class-parents
)
1823 (defalias 'class-direct-subclasses
'class-children
)
1825 (defmacro class-parent-fast
(class) "Return first parent class to CLASS with no check."
1826 `(car (class-parents-fast ,class
)))
1828 (defmacro class-parent
(class) "Return first parent class to CLASS. (overload of variable)."
1829 `(car (class-parents ,class
)))
1831 (defmacro same-class-fast-p
(obj class
) "Return t if OBJ is of class-type CLASS with no error checking."
1832 `(eq (aref ,obj object-class
) ,class
))
1834 (defun same-class-p (obj class
) "Return t if OBJ is of class-type CLASS."
1835 (if (not (class-p class
)) (signal 'wrong-type-argument
(list 'class-p class
)))
1836 (if (not (eieio-object-p obj
)) (signal 'wrong-type-argument
(list 'eieio-object-p obj
)))
1837 (same-class-fast-p obj class
))
1839 (defun object-of-class-p (obj class
)
1840 "Return non-nil if OBJ is an instance of CLASS or CLASS' subclasses."
1841 (if (not (eieio-object-p obj
)) (signal 'wrong-type-argument
(list 'eieio-object-p obj
)))
1842 ;; class will be checked one layer down
1843 (child-of-class-p (aref obj object-class
) class
))
1844 ;; Backwards compatibility
1845 (defalias 'obj-of-class-p
'object-of-class-p
)
1847 (defun child-of-class-p (child class
)
1848 "Return non-nil if CHILD class is a subclass of CLASS."
1849 (if (not (class-p class
)) (signal 'wrong-type-argument
(list 'class-p class
)))
1850 (if (not (class-p child
)) (signal 'wrong-type-argument
(list 'class-p child
)))
1852 (while (and child
(not (eq child class
)))
1853 (setq p
(append p
(aref (class-v child
) class-parent
))
1858 (defun object-slots (obj)
1859 "Return list of slots available in OBJ."
1860 (if (not (eieio-object-p obj
)) (signal 'wrong-type-argument
(list 'eieio-object-p obj
)))
1861 (aref (class-v (object-class-fast obj
)) class-public-a
))
1863 (defun class-slot-initarg (class slot
) "Fetch from CLASS, SLOT's :initarg."
1864 (if (not (class-p class
)) (signal 'wrong-type-argument
(list 'class-p class
)))
1865 (let ((ia (aref (class-v class
) class-initarg-tuples
))
1867 (while (and ia
(not f
))
1868 (if (eq (cdr (car ia
)) slot
)
1869 (setq f
(car (car ia
))))
1873 ;;; CLOS queries into classes and slots
1875 (defun slot-boundp (object slot
)
1876 "Return non-nil if OBJECT's SLOT is bound.
1877 Setting a slot's value makes it bound. Calling `slot-makeunbound' will
1878 make a slot unbound.
1879 OBJECT can be an instance or a class."
1880 ;; Skip typechecking while retrieving this value.
1881 (let ((eieio-skip-typecheck t
))
1882 ;; Return nil if the magic symbol is in there.
1884 ((eieio-object-p object
) (eieio-oref object slot
))
1885 ((class-p object
) (eieio-oref-default object slot
))
1886 (t (signal 'wrong-type-argument
(list 'eieio-object-p object
))))
1889 (defun slot-makeunbound (object slot
)
1890 "In OBJECT, make SLOT unbound."
1891 (eieio-oset object slot eieio-unbound
))
1893 (defun slot-exists-p (object-or-class slot
)
1894 "Return non-nil if OBJECT-OR-CLASS has SLOT."
1895 (let ((cv (class-v (cond ((eieio-object-p object-or-class
)
1896 (object-class object-or-class
))
1897 ((class-p object-or-class
)
1900 (or (memq slot
(aref cv class-public-a
))
1901 (memq slot
(aref cv class-class-allocation-a
)))
1904 (defun find-class (symbol &optional errorp
)
1905 "Return the class that SYMBOL represents.
1906 If there is no class, nil is returned if ERRORP is nil.
1907 If ERRORP is non-nil, `wrong-argument-type' is signaled."
1908 (if (not (class-p symbol
))
1909 (if errorp
(signal 'wrong-type-argument
(list 'class-p symbol
))
1913 ;;; Slightly more complex utility functions for objects
1915 (defun object-assoc (key slot list
)
1916 "Return an object if KEY is `equal' to SLOT's value of an object in LIST.
1917 LIST is a list of objects whose slots are searched.
1918 Objects in LIST do not need to have a slot named SLOT, nor does
1919 SLOT need to be bound. If these errors occur, those objects will
1921 (if (not (listp list
)) (signal 'wrong-type-argument
(list 'listp list
)))
1922 (while (and list
(not (condition-case nil
1923 ;; This prevents errors for missing slots.
1924 (equal key
(eieio-oref (car list
) slot
))
1926 (setq list
(cdr list
)))
1929 (defun object-assoc-list (slot list
)
1930 "Return an association list with the contents of SLOT as the key element.
1931 LIST must be a list of objects with SLOT in it.
1932 This is useful when you need to do completing read on an object group."
1933 (if (not (listp list
)) (signal 'wrong-type-argument
(list 'listp list
)))
1934 (let ((assoclist nil
))
1936 (setq assoclist
(cons (cons (eieio-oref (car list
) slot
)
1939 (setq list
(cdr list
)))
1940 (nreverse assoclist
)))
1942 (defun object-assoc-list-safe (slot list
)
1943 "Return an association list with the contents of SLOT as the key element.
1944 LIST must be a list of objects, but those objects do not need to have
1945 SLOT in it. If it does not, then that element is left out of the association
1947 (if (not (listp list
)) (signal 'wrong-type-argument
(list 'listp list
)))
1948 (let ((assoclist nil
))
1950 (if (slot-exists-p (car list
) slot
)
1951 (setq assoclist
(cons (cons (eieio-oref (car list
) slot
)
1954 (setq list
(cdr list
)))
1955 (nreverse assoclist
)))
1957 (defun object-add-to-list (object slot item
&optional append
)
1958 "In OBJECT's SLOT, add ITEM to the list of elements.
1959 Optional argument APPEND indicates we need to append to the list.
1960 If ITEM already exists in the list in SLOT, then it is not added.
1961 Comparison is done with `equal' through the `member' function call.
1962 If SLOT is unbound, bind it to the list containing ITEM."
1964 ;; Find the originating list.
1965 (if (not (slot-boundp object slot
))
1966 (setq ov
(list item
))
1967 (setq ov
(eieio-oref object slot
))
1968 ;; turn it into a list.
1970 (setq ov
(list ov
)))
1971 ;; Do the combination
1972 (if (not (member item ov
))
1975 (append ov
(list item
))
1977 ;; Set back into the slot.
1978 (eieio-oset object slot ov
)))
1980 (defun object-remove-from-list (object slot item
)
1981 "In OBJECT's SLOT, remove occurrences of ITEM.
1982 Deletion is done with `delete', which deletes by side effect,
1983 and comparisons are done with `equal'.
1984 If SLOT is unbound, do nothing."
1985 (if (not (slot-boundp object slot
))
1987 (eieio-oset object slot
(delete item
(eieio-oref object slot
)))))
1989 ;;; EIEIO internal search functions
1991 (defun eieio-slot-originating-class-p (start-class slot
)
1992 "Return non-nil if START-CLASS is the first class to define SLOT.
1993 This is for testing if `scoped-class' is the class that defines SLOT
1994 so that we can protect private slots."
1995 (let ((par (class-parents start-class
))
1999 (while (and par ret
)
2000 (if (intern-soft (symbol-name slot
)
2001 (aref (class-v (car par
))
2002 class-symbol-obarray
))
2004 (setq par
(cdr par
)))
2007 (defun eieio-slot-name-index (class obj slot
)
2008 "In CLASS for OBJ find the index of the named SLOT.
2009 The slot is a symbol which is installed in CLASS by the `defclass'
2010 call. OBJ can be nil, but if it is an object, and the slot in question
2011 is protected, access will be allowed if OBJ is a child of the currently
2013 If SLOT is the value created with :initarg instead,
2014 reverse-lookup that name, and recurse with the associated slot value."
2015 ;; Removed checks to outside this call
2016 (let* ((fsym (intern-soft (symbol-name slot
)
2017 (aref (class-v class
)
2018 class-symbol-obarray
)))
2019 (fsi (if (symbolp fsym
) (symbol-value fsym
) nil
)))
2022 ((not (get fsym
'protection
))
2024 ((and (eq (get fsym
'protection
) 'protected
)
2025 (bound-and-true-p scoped-class
)
2026 (or (child-of-class-p class scoped-class
)
2027 (and (eieio-object-p obj
)
2028 (child-of-class-p class
(object-class obj
)))))
2030 ((and (eq (get fsym
'protection
) 'private
)
2031 (or (and (bound-and-true-p scoped-class
)
2032 (eieio-slot-originating-class-p scoped-class slot
))
2033 eieio-initializing-object
))
2036 (let ((fn (eieio-initarg-to-attribute class slot
)))
2037 (if fn
(eieio-slot-name-index class obj fn
) nil
)))))
2039 (defun eieio-class-slot-name-index (class slot
)
2040 "In CLASS find the index of the named SLOT.
2041 The slot is a symbol which is installed in CLASS by the `defclass'
2042 call. If SLOT is the value created with :initarg instead,
2043 reverse-lookup that name, and recurse with the associated slot value."
2044 ;; This will happen less often, and with fewer slots. Do this the
2045 ;; storage cheap way.
2046 (let* ((a (aref (class-v class
) class-class-allocation-a
))
2050 ;; Slot # is length of the total list, minus the remaining list of
2054 ;;; CLOS generics internal function handling
2056 (defvar eieio-generic-call-methodname nil
2057 "When using `call-next-method', provides a context on how to do it.")
2058 (defvar eieio-generic-call-arglst nil
2059 "When using `call-next-method', provides a context for parameters.")
2060 (defvar eieio-generic-call-key nil
2061 "When using `call-next-method', provides a context for the current key.
2062 Keys are a number representing :before, :primary, and :after methods.")
2063 (defvar eieio-generic-call-next-method-list nil
2064 "When executing a PRIMARY or STATIC method, track the 'next-method'.
2065 During executions, the list is first generated, then as each next method
2066 is called, the next method is popped off the stack.")
2068 (defvar eieio-pre-method-execution-hooks nil
2069 "Abnormal hook run just before an EIEIO method is executed.
2070 The hook function must accept one argument, the list of forms
2071 about to be executed.")
2073 (defun eieio-generic-call (method args
)
2074 "Call METHOD with ARGS.
2075 ARGS provides the context on which implementation to use.
2076 This should only be called from a generic function."
2077 ;; We must expand our arguments first as they are always
2078 ;; passed in as quoted symbols
2079 (let ((newargs nil
) (mclass nil
) (lambdas nil
) (tlambdas nil
) (keys nil
)
2080 (eieio-generic-call-methodname method
)
2081 (eieio-generic-call-arglst args
)
2083 (primarymethodlist nil
))
2086 firstarg
(car newargs
))
2087 ;; Is the class passed in autoloaded?
2088 ;; Since class names are also constructors, they can be autoloaded
2089 ;; via the autoload command. Check for this, and load them in.
2090 ;; It's ok if it doesn't turn out to be a class. Probably want that
2091 ;; function loaded anyway.
2092 (if (and (symbolp firstarg
)
2094 (listp (symbol-function firstarg
))
2095 (eq 'autoload
(car (symbol-function firstarg
))))
2096 (load (nth 1 (symbol-function firstarg
))))
2097 ;; Determine the class to use.
2098 (cond ((eieio-object-p firstarg
)
2099 (setq mclass
(object-class-fast firstarg
)))
2101 (setq mclass firstarg
))
2103 ;; Make sure the class is a valid class
2104 ;; mclass can be nil (meaning a generic for should be used.
2105 ;; mclass cannot have a value that is not a class, however.
2106 (when (and (not (null mclass
)) (not (class-p mclass
)))
2107 (error "Cannot dispatch method %S on class %S"
2110 ;; Now create a list in reverse order of all the calls we have
2111 ;; make in order to successfully do this right. Rules:
2112 ;; 1) Only call generics if scoped-class is not defined
2113 ;; This prevents multiple calls in the case of recursion
2114 ;; 2) Only call static if this is a static method.
2115 ;; 3) Only call specifics if the definition allows for them.
2116 ;; 4) Call in order based on :before, :primary, and :after
2117 (when (eieio-object-p firstarg
)
2118 ;; Non-static calls do all this stuff.
2123 (eieiomt-method-list method method-after mclass
)
2124 (list (eieio-generic-form method method-after nil
)))
2125 ;;(or (and mclass (eieio-generic-form method method-after mclass))
2126 ;; (eieio-generic-form method method-after nil))
2128 (setq lambdas
(append tlambdas lambdas
)
2129 keys
(append (make-list (length tlambdas
) method-after
) keys
))
2133 (or (and mclass
(eieio-generic-form method method-primary mclass
))
2134 (eieio-generic-form method method-primary nil
)))
2136 (setq lambdas
(cons tlambdas lambdas
)
2137 keys
(cons method-primary keys
)
2139 (eieiomt-method-list method method-primary mclass
)))
2144 (eieiomt-method-list method method-before mclass
)
2145 (list (eieio-generic-form method method-before nil
)))
2146 ;;(or (and mclass (eieio-generic-form method method-before mclass))
2147 ;; (eieio-generic-form method method-before nil))
2149 (setq lambdas
(append tlambdas lambdas
)
2150 keys
(append (make-list (length tlambdas
) method-before
) keys
))
2154 ;; For the case of a class,
2155 ;; if there were no methods found, then there could be :static methods.
2158 (eieio-generic-form method method-static mclass
))
2159 (setq lambdas
(cons tlambdas lambdas
)
2160 keys
(cons method-static keys
)
2161 primarymethodlist
;; Re-use even with bad name here
2162 (eieiomt-method-list method method-static mclass
)))
2163 ;; For the case of no class (ie - mclass == nil) then there may
2164 ;; be a primary method.
2166 (eieio-generic-form method method-primary nil
))
2168 (setq lambdas
(cons tlambdas lambdas
)
2169 keys
(cons method-primary keys
)
2171 (eieiomt-method-list method method-primary nil
)))
2174 (run-hook-with-args 'eieio-pre-method-execution-hooks
2177 ;; Now loop through all occurrences forms which we must execute
2178 ;; (which are happily sorted now) and execute them all!
2179 (let ((rval nil
) (lastval nil
) (rvalever nil
) (found nil
))
2182 (let* ((scoped-class (cdr (car lambdas
)))
2183 (eieio-generic-call-key (car keys
))
2185 (or (= eieio-generic-call-key method-primary
)
2186 (= eieio-generic-call-key method-static
)))
2187 (eieio-generic-call-next-method-list
2188 ;; Use the cdr, as the first element is the fcn
2189 ;; we are calling right now.
2190 (when has-return-val
(cdr primarymethodlist
)))
2193 ;;(setq rval (apply (car (car lambdas)) newargs))
2194 (setq lastval
(apply (car (car lambdas
)) newargs
))
2195 (when has-return-val
2199 (setq lambdas
(cdr lambdas
)
2202 (if (eieio-object-p (car args
))
2203 (setq rval
(apply 'no-applicable-method
(car args
) method args
)
2206 'no-method-definition
2207 (list method args
))))
2208 ;; Right Here... it could be that lastval is returned when
2209 ;; rvalever is nil. Is that right?
2212 (defun eieio-generic-call-primary-only (method args
)
2213 "Call METHOD with ARGS for methods with only :PRIMARY implementations.
2214 ARGS provides the context on which implementation to use.
2215 This should only be called from a generic function.
2217 This method is like `eieio-generic-call', but only
2218 implementations in the :PRIMARY slot are queried. After many
2219 years of use, it appears that over 90% of methods in use
2220 have :PRIMARY implementations only. We can therefore optimize
2221 for this common case to improve performance."
2222 ;; We must expand our arguments first as they are always
2223 ;; passed in as quoted symbols
2224 (let ((newargs nil
) (mclass nil
) (lambdas nil
)
2225 (eieio-generic-call-methodname method
)
2226 (eieio-generic-call-arglst args
)
2228 (primarymethodlist nil
)
2232 firstarg
(car newargs
))
2234 ;; Determine the class to use.
2235 (cond ((eieio-object-p firstarg
)
2236 (setq mclass
(object-class-fast firstarg
)))
2238 (error "Method %s called on nil" method
))
2239 ((not (eieio-object-p firstarg
))
2240 (error "Primary-only method %s called on something not an object" method
))
2242 (error "EIEIO Error: Improperly classified method %s as primary only"
2245 ;; Make sure the class is a valid class
2246 ;; mclass can be nil (meaning a generic for should be used.
2247 ;; mclass cannot have a value that is not a class, however.
2249 (error "Cannot dispatch method %S on class %S" method mclass
)
2253 (setq lambdas
(eieio-generic-form method method-primary mclass
))
2254 (setq primarymethodlist
;; Re-use even with bad name here
2255 (eieiomt-method-list method method-primary mclass
))
2257 ;; Now loop through all occurrences forms which we must execute
2258 ;; (which are happily sorted now) and execute them all!
2259 (let* ((rval nil
) (lastval nil
) (rvalever nil
)
2260 (scoped-class (cdr lambdas
))
2261 (eieio-generic-call-key method-primary
)
2262 ;; Use the cdr, as the first element is the fcn
2263 ;; we are calling right now.
2264 (eieio-generic-call-next-method-list (cdr primarymethodlist
))
2267 (if (or (not lambdas
) (not (car lambdas
)))
2269 ;; No methods found for this impl...
2270 (if (eieio-object-p (car args
))
2271 (setq rval
(apply 'no-applicable-method
(car args
) method args
)
2274 'no-method-definition
2275 (list method args
)))
2277 ;; Do the regular implementation here.
2279 (run-hook-with-args 'eieio-pre-method-execution-hooks
2282 (setq lastval
(apply (car lambdas
) newargs
))
2287 ;; Right Here... it could be that lastval is returned when
2288 ;; rvalever is nil. Is that right?
2291 (defun eieiomt-method-list (method key class
)
2292 "Return an alist list of methods lambdas.
2293 METHOD is the method name.
2294 KEY represents either :before, or :after methods.
2295 CLASS is the starting class to search from in the method tree.
2296 If CLASS is nil, then an empty list of methods should be returned."
2297 ;; Note: eieiomt - the MT means MethodTree. See more comments below
2298 ;; for the rest of the eieiomt methods.
2300 ;; Collect lambda expressions stored for the class and its parent
2303 (dolist (ancestor (class-precedence-list class
))
2304 ;; Lookup the form to use for the PRIMARY object for the next level
2305 (let ((tmpl (eieio-generic-form method key ancestor
)))
2308 ;; This prevents duplicates coming out of the
2309 ;; class method optimizer. Perhaps we should
2310 ;; just not optimize before/afters?
2311 (not (member tmpl lambdas
))))
2312 (push tmpl lambdas
))))
2314 ;; Return collected lambda. For :after methods, return in current
2315 ;; order (most general class last); Otherwise, reverse order.
2316 (if (eq key method-after
)
2318 (nreverse lambdas
))))
2320 (defun next-method-p ()
2321 "Return non-nil if there is a next method.
2322 Returns a list of lambda expressions which is the `next-method'
2324 eieio-generic-call-next-method-list
)
2326 (defun call-next-method (&rest replacement-args
)
2327 "Call the superclass method from a subclass method.
2328 The superclass method is specified in the current method list,
2329 and is called the next method.
2331 If REPLACEMENT-ARGS is non-nil, then use them instead of
2332 `eieio-generic-call-arglst'. The generic arg list are the
2333 arguments passed in at the top level.
2335 Use `next-method-p' to find out if there is a next method to call."
2336 (if (not (bound-and-true-p scoped-class
))
2337 (error "`call-next-method' not called within a class specific method"))
2338 (if (and (/= eieio-generic-call-key method-primary
)
2339 (/= eieio-generic-call-key method-static
))
2340 (error "Cannot `call-next-method' except in :primary or :static methods")
2342 (let ((newargs (or replacement-args eieio-generic-call-arglst
))
2343 (next (car eieio-generic-call-next-method-list
))
2345 (if (or (not next
) (not (car next
)))
2346 (apply 'no-next-method
(car newargs
) (cdr newargs
))
2347 (let* ((eieio-generic-call-next-method-list
2348 (cdr eieio-generic-call-next-method-list
))
2349 (eieio-generic-call-arglst newargs
)
2350 (scoped-class (cdr next
))
2357 ;; eieio-method-tree : eieiomt-
2359 ;; Stored as eieio-method-tree in property list of a generic method
2361 ;; (eieio-method-tree . [BEFORE PRIMARY AFTER
2362 ;; genericBEFORE genericPRIMARY genericAFTER])
2364 ;; (eieio-method-obarray . [BEFORE PRIMARY AFTER
2365 ;; genericBEFORE genericPRIMARY genericAFTER])
2366 ;; where the association is a vector.
2367 ;; (aref 0 -- all static methods.
2368 ;; (aref 1 -- all methods classified as :before
2369 ;; (aref 2 -- all methods classified as :primary
2370 ;; (aref 3 -- all methods classified as :after
2371 ;; (aref 4 -- a generic classified as :before
2372 ;; (aref 5 -- a generic classified as :primary
2373 ;; (aref 6 -- a generic classified as :after
2375 (defvar eieiomt-optimizing-obarray nil
2376 "While mapping atoms, this contain the obarray being optimized.")
2378 (defun eieiomt-install (method-name)
2379 "Install the method tree, and obarray onto METHOD-NAME.
2380 Do not do the work if they already exist."
2381 (let ((emtv (get method-name
'eieio-method-tree
))
2382 (emto (get method-name
'eieio-method-obarray
)))
2383 (if (or (not emtv
) (not emto
))
2385 (setq emtv
(put method-name
'eieio-method-tree
2386 (make-vector method-num-slots nil
))
2387 emto
(put method-name
'eieio-method-obarray
2388 (make-vector method-num-slots nil
)))
2389 (aset emto
0 (make-vector 11 0))
2390 (aset emto
1 (make-vector 11 0))
2391 (aset emto
2 (make-vector 41 0))
2392 (aset emto
3 (make-vector 11 0))
2395 (defun eieiomt-add (method-name method key class
)
2396 "Add to METHOD-NAME the forms METHOD in a call position KEY for CLASS.
2397 METHOD-NAME is the name created by a call to `defgeneric'.
2398 METHOD are the forms for a given implementation.
2399 KEY is an integer (see comment in eieio.el near this function) which
2400 is associated with the :static :before :primary and :after tags.
2401 It also indicates if CLASS is defined or not.
2402 CLASS is the class this method is associated with."
2403 (if (or (> key method-num-slots
) (< key
0))
2404 (error "eieiomt-add: method key error!"))
2405 (let ((emtv (get method-name
'eieio-method-tree
))
2406 (emto (get method-name
'eieio-method-obarray
)))
2407 ;; Make sure the method tables are available.
2408 (if (or (not emtv
) (not emto
))
2409 (error "Programmer error: eieiomt-add"))
2410 ;; only add new cells on if it doesn't already exist!
2411 (if (assq class
(aref emtv key
))
2412 (setcdr (assq class
(aref emtv key
)) method
)
2413 (aset emtv key
(cons (cons class method
) (aref emtv key
))))
2414 ;; Add function definition into newly created symbol, and store
2415 ;; said symbol in the correct obarray, otherwise use the
2416 ;; other array to keep this stuff
2417 (if (< key method-num-lists
)
2418 (let ((nsym (intern (symbol-name class
) (aref emto key
))))
2419 (fset nsym method
)))
2420 ;; Save the defmethod file location in a symbol property.
2421 (let ((fname (if load-in-progress
2426 (when (string-match "\\.elc$" fname
)
2427 (setq fname
(substring fname
0 (1- (length fname
)))))
2428 (setq loc
(get method-name
'method-locations
))
2431 (put method-name
'method-locations loc
)))
2432 ;; Now optimize the entire obarray
2433 (if (< key method-num-lists
)
2434 (let ((eieiomt-optimizing-obarray (aref emto key
)))
2435 ;; @todo - Is this overkill? Should we just clear the symbol?
2436 (mapatoms 'eieiomt-sym-optimize eieiomt-optimizing-obarray
)))
2439 (defun eieiomt-next (class)
2440 "Return the next parent class for CLASS.
2441 If CLASS is a superclass, return variable `eieio-default-superclass'.
2442 If CLASS is variable `eieio-default-superclass' then return nil.
2443 This is different from function `class-parent' as class parent returns
2444 nil for superclasses. This function performs no type checking!"
2445 ;; No type-checking because all calls are made from functions which
2446 ;; are safe and do checking for us.
2447 (or (class-parents-fast class
)
2448 (if (eq class
'eieio-default-superclass
)
2450 '(eieio-default-superclass))))
2452 (defun eieiomt-sym-optimize (s)
2453 "Find the next class above S which has a function body for the optimizer."
2454 ;; Set the value to nil in case there is no nearest cell.
2456 ;; Find the nearest cell that has a function body. If we find one,
2457 ;; we replace the nil from above.
2458 (let ((external-symbol (intern-soft (symbol-name s
))))
2460 (dolist (ancestor (rest (class-precedence-list external-symbol
)))
2461 (let ((ov (intern-soft (symbol-name ancestor
)
2462 eieiomt-optimizing-obarray
)))
2464 (set s ov
) ;; store ov as our next symbol
2465 (throw 'done ancestor
)))))))
2467 (defun eieio-generic-form (method key class
)
2468 "Return the lambda form belonging to METHOD using KEY based upon CLASS.
2469 If CLASS is not a class then use `generic' instead. If class has
2470 no form, but has a parent class, then trace to that parent class.
2471 The first time a form is requested from a symbol, an optimized path
2472 is memorized for faster future use."
2473 (let ((emto (aref (get method
'eieio-method-obarray
)
2474 (if class key
(eieio-specialized-key-to-generic-key key
)))))
2476 ;; 1) find our symbol
2477 (let ((cs (intern-soft (symbol-name class
) emto
)))
2479 ;; 2) If there isn't one, then make one.
2480 ;; This can be slow since it only occurs once
2482 (setq cs
(intern (symbol-name class
) emto
))
2483 ;; 2.1) Cache its nearest neighbor with a quick optimize
2484 ;; which should only occur once for this call ever
2485 (let ((eieiomt-optimizing-obarray emto
))
2486 (eieiomt-sym-optimize cs
))))
2487 ;; 3) If it's bound return this one.
2489 (cons cs
(aref (class-v class
) class-symbol
))
2490 ;; 4) If it's not bound then this variable knows something
2491 (if (symbol-value cs
)
2493 ;; 4.1) This symbol holds the next class in its value
2494 (setq class
(symbol-value cs
)
2495 cs
(intern-soft (symbol-name class
) emto
))
2496 ;; 4.2) The optimizer should always have chosen a
2499 (cons cs
(aref (class-v (intern (symbol-name class
)))
2501 ;;(error "EIEIO optimizer: erratic data loss!"))
2503 ;; There never will be a funcall...
2505 ;; for a generic call, what is a list, is the function body we want.
2506 (let ((emtl (aref (get method
'eieio-method-tree
)
2507 (if class key
(eieio-specialized-key-to-generic-key key
)))))
2509 ;; The car of EMTL is supposed to be a class, which in this
2510 ;; case is nil, so skip it.
2511 (cons (cdr (car emtl
)) nil
)
2515 ;; Way to assign slots based on a list. Used for constructors, or
2516 ;; even resetting an object at run-time
2518 (defun eieio-set-defaults (obj &optional set-all
)
2519 "Take object OBJ, and reset all slots to their defaults.
2520 If SET-ALL is non-nil, then when a default is nil, that value is
2521 reset. If SET-ALL is nil, the slots are only reset if the default is
2523 (let ((scoped-class (aref obj object-class
))
2524 (eieio-initializing-object t
)
2525 (pub (aref (class-v (aref obj object-class
)) class-public-a
)))
2527 (let ((df (eieio-oref-default obj
(car pub
))))
2529 (eieio-oset obj
(car pub
) df
)))
2530 (setq pub
(cdr pub
)))))
2532 (defun eieio-initarg-to-attribute (class initarg
)
2533 "For CLASS, convert INITARG to the actual attribute name.
2534 If there is no translation, pass it in directly (so we can cheat if
2535 need be... May remove that later...)"
2536 (let ((tuple (assoc initarg
(aref (class-v class
) class-initarg-tuples
))))
2541 (defun eieio-attribute-to-initarg (class attribute
)
2542 "In CLASS, convert the ATTRIBUTE into the corresponding init argument tag.
2543 This is usually a symbol that starts with `:'."
2544 (let ((tuple (rassoc attribute
(aref (class-v class
) class-initarg-tuples
))))
2550 ;;; Here are some special types of errors
2552 (intern "no-method-definition")
2553 (put 'no-method-definition
'error-conditions
'(no-method-definition error
))
2554 (put 'no-method-definition
'error-message
"No method definition")
2556 (intern "no-next-method")
2557 (put 'no-next-method
'error-conditions
'(no-next-method error
))
2558 (put 'no-next-method
'error-message
"No next method")
2560 (intern "invalid-slot-name")
2561 (put 'invalid-slot-name
'error-conditions
'(invalid-slot-name error
))
2562 (put 'invalid-slot-name
'error-message
"Invalid slot name")
2564 (intern "invalid-slot-type")
2565 (put 'invalid-slot-type
'error-conditions
'(invalid-slot-type error nil
))
2566 (put 'invalid-slot-type
'error-message
"Invalid slot type")
2568 (intern "unbound-slot")
2569 (put 'unbound-slot
'error-conditions
'(unbound-slot error nil
))
2570 (put 'unbound-slot
'error-message
"Unbound slot")
2572 (intern "inconsistent-class-hierarchy")
2573 (put 'inconsistent-class-hierarchy
'error-conditions
2574 '(inconsistent-class-hierarchy error nil
))
2575 (put 'inconsistent-class-hierarchy
'error-message
"Inconsistent class hierarchy")
2577 ;;; Here are some CLOS items that need the CL package
2580 (defsetf eieio-oref eieio-oset
)
2582 (if (eval-when-compile (fboundp 'gv-define-expander
))
2583 ;; Not needed for Emacs>=24.3 since gv.el's setf expands macros and
2586 (defsetf slot-value eieio-oset
)
2588 ;; The below setf method was written by Arnd Kohrs <kohrs@acm.org>
2589 (define-setf-method oref
(obj slot
)
2592 (let ((obj-temp (gensym))
2593 (slot-temp (gensym))
2594 (store-temp (gensym)))
2595 (list (list obj-temp slot-temp
)
2596 (list obj
`(quote ,slot
))
2598 (list 'set-slot-value obj-temp slot-temp
2600 (list 'slot-value obj-temp slot-temp
))))))
2604 ;; We want all objects created by EIEIO to have some default set of
2605 ;; behaviors so we can create object utilities, and allow various
2606 ;; types of error checking. To do this, create the default EIEIO
2607 ;; class, and when no parent class is specified, use this as the
2608 ;; default. (But don't store it in the other classes as the default,
2609 ;; allowing for transparent support.)
2612 (defclass eieio-default-superclass nil
2614 "Default parent class for classes with no specified parent class.
2615 Its slots are automatically adopted by classes with no specified parents.
2616 This class is not stored in the `parent' slot of a class vector."
2619 (defalias 'standard-class
'eieio-default-superclass
)
2621 (defgeneric constructor
(class newname
&rest slots
)
2622 "Default constructor for CLASS `eieio-default-superclass'.")
2624 (defmethod constructor :static
2625 ((class eieio-default-superclass
) newname
&rest slots
)
2626 "Default constructor for CLASS `eieio-default-superclass'.
2627 NEWNAME is the name to be given to the constructed object.
2628 SLOTS are the initialization slots used by `shared-initialize'.
2629 This static method is called when an object is constructed.
2630 It allocates the vector used to represent an EIEIO object, and then
2631 calls `shared-initialize' on that object."
2632 (let* ((new-object (copy-sequence (aref (class-v class
)
2633 class-default-object-cache
))))
2634 ;; Update the name for the newly created object.
2635 (aset new-object object-name newname
)
2636 ;; Call the initialize method on the new object with the slots
2637 ;; that were passed down to us.
2638 (initialize-instance new-object slots
)
2639 ;; Return the created object.
2642 (defgeneric shared-initialize
(obj slots
)
2643 "Set slots of OBJ with SLOTS which is a list of name/value pairs.
2644 Called from the constructor routine.")
2646 (defmethod shared-initialize ((obj eieio-default-superclass
) slots
)
2647 "Set slots of OBJ with SLOTS which is a list of name/value pairs.
2648 Called from the constructor routine."
2649 (let ((scoped-class (aref obj object-class
)))
2651 (let ((rn (eieio-initarg-to-attribute (object-class-fast obj
)
2654 (slot-missing obj
(car slots
) 'oset
(car (cdr slots
)))
2655 (eieio-oset obj rn
(car (cdr slots
)))))
2656 (setq slots
(cdr (cdr slots
))))))
2658 (defgeneric initialize-instance
(this &optional slots
)
2659 "Construct the new object THIS based on SLOTS.")
2661 (defmethod initialize-instance ((this eieio-default-superclass
)
2663 "Construct the new object THIS based on SLOTS.
2664 SLOTS is a tagged list where odd numbered elements are tags, and
2665 even numbered elements are the values to store in the tagged slot.
2666 If you overload the `initialize-instance', there you will need to
2667 call `shared-initialize' yourself, or you can call `call-next-method'
2668 to have this constructor called automatically. If these steps are
2669 not taken, then new objects of your class will not have their values
2670 dynamically set from SLOTS."
2671 ;; First, see if any of our defaults are `lambda', and
2672 ;; re-evaluate them and apply the value to our slots.
2673 (let* ((scoped-class (class-v (aref this object-class
)))
2674 (slot (aref scoped-class class-public-a
))
2675 (defaults (aref scoped-class class-public-d
)))
2677 ;; For each slot, see if we need to evaluate it.
2679 ;; Paul Landes said in an email:
2680 ;; > CL evaluates it if it can, and otherwise, leaves it as
2681 ;; > the quoted thing as you already have. This is by the
2682 ;; > Sonya E. Keene book and other things I've look at on the
2684 (let ((dflt (eieio-default-eval-maybe (car defaults
))))
2685 (when (not (eq dflt
(car defaults
)))
2686 (eieio-oset this
(car slot
) dflt
) ))
2688 (setq slot
(cdr slot
)
2689 defaults
(cdr defaults
))))
2690 ;; Shared initialize will parse our slots for us.
2691 (shared-initialize this slots
))
2693 (defgeneric slot-missing
(object slot-name operation
&optional new-value
)
2694 "Method invoked when an attempt to access a slot in OBJECT fails.")
2696 (defmethod slot-missing ((object eieio-default-superclass
) slot-name
2697 operation
&optional new-value
)
2698 "Method invoked when an attempt to access a slot in OBJECT fails.
2699 SLOT-NAME is the name of the failed slot, OPERATION is the type of access
2700 that was requested, and optional NEW-VALUE is the value that was desired
2703 This method is called from `oref', `oset', and other functions which
2704 directly reference slots in EIEIO objects."
2705 (signal 'invalid-slot-name
(list (object-name object
)
2708 (defgeneric slot-unbound
(object class slot-name fn
)
2709 "Slot unbound is invoked during an attempt to reference an unbound slot.")
2711 (defmethod slot-unbound ((object eieio-default-superclass
)
2713 "Slot unbound is invoked during an attempt to reference an unbound slot.
2714 OBJECT is the instance of the object being reference. CLASS is the
2715 class of OBJECT, and SLOT-NAME is the offending slot. This function
2716 throws the signal `unbound-slot'. You can overload this function and
2717 return the value to use in place of the unbound value.
2718 Argument FN is the function signaling this error.
2719 Use `slot-boundp' to determine if a slot is bound or not.
2721 In CLOS, the argument list is (CLASS OBJECT SLOT-NAME), but
2722 EIEIO can only dispatch on the first argument, so the first two are swapped."
2723 (signal 'unbound-slot
(list (class-name class
) (object-name object
)
2726 (defgeneric no-applicable-method
(object method
&rest args
)
2727 "Called if there are no implementations for OBJECT in METHOD.")
2729 (defmethod no-applicable-method ((object eieio-default-superclass
)
2731 "Called if there are no implementations for OBJECT in METHOD.
2732 OBJECT is the object which has no method implementation.
2733 ARGS are the arguments that were passed to METHOD.
2735 Implement this for a class to block this signal. The return
2736 value becomes the return value of the original method call."
2737 (signal 'no-method-definition
(list method
(object-name object
)))
2740 (defgeneric no-next-method
(object &rest args
)
2741 "Called from `call-next-method' when no additional methods are available.")
2743 (defmethod no-next-method ((object eieio-default-superclass
)
2745 "Called from `call-next-method' when no additional methods are available.
2746 OBJECT is othe object being called on `call-next-method'.
2747 ARGS are the arguments it is called by.
2748 This method signals `no-next-method' by default. Override this
2749 method to not throw an error, and its return value becomes the
2750 return value of `call-next-method'."
2751 (signal 'no-next-method
(list (object-name object
) args
))
2754 (defgeneric clone
(obj &rest params
)
2755 "Make a copy of OBJ, and then supply PARAMS.
2756 PARAMS is a parameter list of the same form used by `initialize-instance'.
2758 When overloading `clone', be sure to call `call-next-method'
2759 first and modify the returned object.")
2761 (defmethod clone ((obj eieio-default-superclass
) &rest params
)
2762 "Make a copy of OBJ, and then apply PARAMS."
2763 (let ((nobj (copy-sequence obj
))
2764 (nm (aref obj object-name
))
2765 (passname (and params
(stringp (car params
))))
2767 (if params
(shared-initialize nobj
(if passname
(cdr params
) params
)))
2770 (if (string-match "-\\([0-9]+\\)" nm
)
2771 (setq num
(1+ (string-to-number (match-string 1 nm
)))
2772 nm
(substring nm
0 (match-beginning 0))))
2773 (aset nobj object-name
(concat nm
"-" (int-to-string num
))))
2774 (aset nobj object-name
(car params
)))
2777 (defgeneric destructor
(this &rest params
)
2778 "Destructor for cleaning up any dynamic links to our object.")
2780 (defmethod destructor ((this eieio-default-superclass
) &rest params
)
2781 "Destructor for cleaning up any dynamic links to our object.
2782 Argument THIS is the object being destroyed. PARAMS are additional
2783 ignored parameters."
2784 ;; No cleanup... yet.
2787 (defgeneric object-print
(this &rest strings
)
2788 "Pretty printer for object THIS. Call function `object-name' with STRINGS.
2790 It is sometimes useful to put a summary of the object into the
2791 default #<notation> string when using EIEIO browsing tools.
2792 Implement this method to customize the summary.")
2794 (defmethod object-print ((this eieio-default-superclass
) &rest strings
)
2795 "Pretty printer for object THIS. Call function `object-name' with STRINGS.
2796 The default method for printing object THIS is to use the
2797 function `object-name'.
2799 It is sometimes useful to put a summary of the object into the
2800 default #<notation> string when using EIEIO browsing tools.
2802 Implement this function and specify STRINGS in a call to
2803 `call-next-method' to provide additional summary information.
2804 When passing in extra strings from child classes, always remember
2805 to prepend a space."
2806 (object-name this
(apply 'concat strings
)))
2808 (defvar eieio-print-depth
0
2809 "When printing, keep track of the current indentation depth.")
2811 (defgeneric object-write
(this &optional comment
)
2812 "Write out object THIS to the current stream.
2813 Optional COMMENT will add comments to the beginning of the output.")
2815 (defmethod object-write ((this eieio-default-superclass
) &optional comment
)
2816 "Write object THIS out to the current stream.
2817 This writes out the vector version of this object. Complex and recursive
2818 object are discouraged from being written.
2819 If optional COMMENT is non-nil, include comments when outputting
2822 (princ ";; Object ")
2823 (princ (object-name-string this
))
2827 (let* ((cl (object-class this
))
2829 ;; Now output readable lisp to recreate this object
2830 ;; It should look like this:
2831 ;; (<constructor> <name> <slot> <slot> ... )
2832 ;; Each slot's slot is writen using its :writer.
2833 (princ (make-string (* eieio-print-depth
2) ?
))
2835 (princ (symbol-name (class-constructor (object-class this
))))
2837 (prin1 (object-name-string this
))
2839 ;; Loop over all the public slots
2840 (let ((publa (aref cv class-public-a
))
2841 (publd (aref cv class-public-d
))
2842 (publp (aref cv class-public-printer
))
2843 (eieio-print-depth (1+ eieio-print-depth
)))
2845 (when (slot-boundp this
(car publa
))
2846 (let ((i (class-slot-initarg cl
(car publa
)))
2847 (v (eieio-oref this
(car publa
)))
2849 (unless (or (not i
) (equal v
(car publd
)))
2850 (princ (make-string (* eieio-print-depth
2) ?
))
2851 (princ (symbol-name i
))
2854 ;; Use our public printer
2855 (funcall (car publp
) v
)
2856 ;; Use our generic override prin1 function.
2857 (eieio-override-prin1 v
))
2859 (setq publa
(cdr publa
) publd
(cdr publd
)
2861 (princ (make-string (* eieio-print-depth
2) ?
)))
2864 (defun eieio-override-prin1 (thing)
2865 "Perform a `prin1' on THING taking advantage of object knowledge."
2866 (cond ((eieio-object-p thing
)
2867 (object-write thing
))
2869 (eieio-list-prin1 thing
))
2871 (princ (class-name thing
)))
2873 (princ (concat "'" (symbol-name thing
))))
2876 (defun eieio-list-prin1 (list)
2877 "Display LIST where list may contain objects."
2878 (if (not (eieio-object-p (car list
)))
2883 (if (eieio-object-p (car list
)) (princ "\n "))
2885 (if (eieio-object-p (car list
))
2886 (object-write (car list
))
2890 (setq list
(cdr list
)))
2891 (princ (make-string (* eieio-print-depth
2) ?
))
2895 ;;; Unimplemented functions from CLOS
2897 (defun change-class (obj class
)
2898 "Change the class of OBJ to type CLASS.
2899 This may create or delete slots, but does not affect the return value
2901 (error "EIEIO: `change-class' is unimplemented"))
2905 ;;; Obsolete backward compatibility functions.
2906 ;; Needed to run byte-code compiled with the EIEIO of Emacs-23.
2908 (defun eieio-defmethod (method args
)
2909 "Obsolete work part of an old version of the `defmethod' macro."
2910 (let ((key nil
) (body nil
) (firstarg nil
) (argfix nil
) (argclass nil
) loopa
)
2911 ;; find optional keys
2913 (cond ((or (eq ':BEFORE
(car args
))
2914 (eq ':before
(car args
)))
2915 (setq args
(cdr args
))
2917 ((or (eq ':AFTER
(car args
))
2918 (eq ':after
(car args
)))
2919 (setq args
(cdr args
))
2921 ((or (eq ':PRIMARY
(car args
))
2922 (eq ':primary
(car args
)))
2923 (setq args
(cdr args
))
2925 ((or (eq ':STATIC
(car args
))
2926 (eq ':static
(car args
)))
2927 (setq args
(cdr args
))
2930 (t method-primary
)))
2931 ;; get body, and fix contents of args to be the arguments of the fn.
2932 (setq body
(cdr args
)
2935 ;; Create a fixed version of the arguments
2937 (setq argfix
(cons (if (listp (car loopa
)) (car (car loopa
)) (car loopa
))
2939 (setq loopa
(cdr loopa
)))
2940 ;; make sure there is a generic
2943 (if (stringp (car body
))
2944 (car body
) (format "Generically created method `%s'." method
)))
2945 ;; create symbol for property to bind to. If the first arg is of
2946 ;; the form (varname vartype) and `vartype' is a class, then
2947 ;; that class will be the type symbol. If not, then it will fall
2948 ;; under the type `primary' which is a non-specific calling of the
2950 (setq firstarg
(car args
))
2951 (if (listp firstarg
)
2953 (setq argclass
(nth 1 firstarg
))
2954 (if (not (class-p argclass
))
2955 (error "Unknown class type %s in method parameters"
2958 (signal 'wrong-type-argument
(list :static
'non-class-arg
)))
2959 ;; generics are higher
2960 (setq key
(eieio-specialized-key-to-generic-key key
)))
2961 ;; Put this lambda into the symbol so we can find it
2962 (if (byte-code-function-p (car-safe body
))
2963 (eieiomt-add method
(car-safe body
) key argclass
)
2964 (eieiomt-add method
(append (list 'lambda
(reverse argfix
)) body
)
2968 (when eieio-optimize-primary-methods-flag
2971 ;; If this method, after this setup, only has primary methods, then
2972 ;; we can setup the generic that way.
2973 (if (generic-primary-only-p method
)
2974 ;; If there is only one primary method, then we can go one more
2975 ;; optimization step.
2976 (if (generic-primary-only-one-p method
)
2977 (eieio-defgeneric-reset-generic-form-primary-only-one method
)
2978 (eieio-defgeneric-reset-generic-form-primary-only method
))
2979 (eieio-defgeneric-reset-generic-form method
)))
2982 (make-obsolete 'eieio-defmethod
'eieio--defmethod
"24.1")
2984 (defun eieio-defgeneric (method doc-string
)
2985 "Obsolete work part of an old version of the `defgeneric' macro."
2986 (if (and (fboundp method
) (not (generic-p method
))
2987 (or (byte-code-function-p (symbol-function method
))
2988 (not (eq 'autoload
(car (symbol-function method
)))))
2990 (error "You cannot create a generic/method over an existing symbol: %s"
2992 ;; Don't do this over and over.
2993 (unless (fboundp 'method
)
2994 ;; This defun tells emacs where the first definition of this
2995 ;; method is defined.
2996 `(defun ,method nil
)
2997 ;; Make sure the method tables are installed.
2998 (eieiomt-install method
)
2999 ;; Apply the actual body of this function.
3000 (fset method
(eieio-defgeneric-form method doc-string
))
3001 ;; Return the method
3003 (make-obsolete 'eieio-defgeneric nil
"24.1")
3005 ;;; Interfacing with edebug
3007 (defun eieio-edebug-prin1-to-string (object &optional noescape
)
3008 "Display EIEIO OBJECT in fancy format.
3009 Overrides the edebug default.
3010 Optional argument NOESCAPE is passed to `prin1-to-string' when appropriate."
3011 (cond ((class-p object
) (class-name object
))
3012 ((eieio-object-p object
) (object-print object
))
3013 ((and (listp object
) (or (class-p (car object
))
3014 (eieio-object-p (car object
))))
3015 (concat "(" (mapconcat 'eieio-edebug-prin1-to-string object
" ") ")"))
3016 (t (prin1-to-string object noescape
))))
3018 (add-hook 'edebug-setup-hook
3020 (def-edebug-spec defmethod
3021 (&define
; this means we are defining something
3022 [&or name
("setf" :name setf name
)]
3023 ;; ^^ This is the methods symbol
3024 [ &optional symbolp
] ; this is key :before etc
3026 [ &optional stringp
] ; documentation string
3027 def-body
; part to be debugged
3029 ;; The rest of the macros
3030 (def-edebug-spec oref
(form quote
))
3031 (def-edebug-spec oref-default
(form quote
))
3032 (def-edebug-spec oset
(form quote form
))
3033 (def-edebug-spec oset-default
(form quote form
))
3034 (def-edebug-spec class-v form
)
3035 (def-edebug-spec class-p form
)
3036 (def-edebug-spec eieio-object-p form
)
3037 (def-edebug-spec class-constructor form
)
3038 (def-edebug-spec generic-p form
)
3039 (def-edebug-spec with-slots
(list list def-body
))
3040 ;; I suspect this isn't the best way to do this, but when
3041 ;; cust-print was used on my system all my objects
3042 ;; appeared as "#1 =" which was not useful. This allows
3043 ;; edebug to print my objects in the nice way they were
3044 ;; meant to with `object-print' and `class-name'
3045 ;; (defalias 'edebug-prin1-to-string 'eieio-edebug-prin1-to-string)
3049 ;;; Interfacing with imenu in emacs lisp mode
3050 ;; (Only if the expression is defined)
3052 (if (eval-when-compile (boundp 'list-imenu-generic-expression
))
3055 (defun eieio-update-lisp-imenu-expression ()
3056 "Examine `lisp-imenu-generic-expression' and modify it to find `defmethod'."
3057 (let ((exp lisp-imenu-generic-expression
))
3059 ;; it's of the form '( ( title expr indx ) ... )
3060 (let* ((subcar (cdr (car exp
)))
3061 (substr (car subcar
)))
3062 (if (and (not (string-match "|method\\\\" substr
))
3063 (string-match "|advice\\\\" substr
))
3065 (replace-match "|advice\\|method\\" t t substr
0))))
3066 (setq exp
(cdr exp
)))))
3068 (eieio-update-lisp-imenu-expression)
3072 ;;; Autoloading some external symbols, and hooking into the help system
3076 ;;; Start of automatically extracted autoloads.
3078 ;;;### (autoloads (customize-object) "eieio-custom" "eieio-custom.el"
3079 ;;;;;; "928623502e8bf40454822355388542b5")
3080 ;;; Generated autoloads from eieio-custom.el
3082 (autoload 'customize-object
"eieio-custom" "\
3083 Customize OBJ in a custom buffer.
3084 Optional argument GROUP is the sub-group of slots to display.
3086 \(fn OBJ &optional GROUP)" nil nil
)
3090 ;;;### (autoloads (eieio-help-mode-augmentation-maybee eieio-describe-generic
3091 ;;;;;; eieio-describe-constructor eieio-describe-class eieio-browse)
3092 ;;;;;; "eieio-opt" "eieio-opt.el" "d808328f9c0156ecbd412d77ba8c569e")
3093 ;;; Generated autoloads from eieio-opt.el
3095 (autoload 'eieio-browse
"eieio-opt" "\
3096 Create an object browser window to show all objects.
3097 If optional ROOT-CLASS, then start with that, otherwise start with
3098 variable `eieio-default-superclass'.
3100 \(fn &optional ROOT-CLASS)" t nil
)
3101 (defalias 'describe-class
'eieio-describe-class
)
3103 (autoload 'eieio-describe-class
"eieio-opt" "\
3104 Describe a CLASS defined by a string or symbol.
3105 If CLASS is actually an object, then also display current values of that object.
3106 Optional HEADERFCN should be called to insert a few bits of info first.
3108 \(fn CLASS &optional HEADERFCN)" t nil
)
3110 (autoload 'eieio-describe-constructor
"eieio-opt" "\
3111 Describe the constructor function FCN.
3112 Uses `eieio-describe-class' to describe the class being constructed.
3115 (defalias 'describe-generic
'eieio-describe-generic
)
3117 (autoload 'eieio-describe-generic
"eieio-opt" "\
3118 Describe the generic function GENERIC.
3119 Also extracts information about all methods specific to this generic.
3121 \(fn GENERIC)" t nil
)
3123 (autoload 'eieio-help-mode-augmentation-maybee
"eieio-opt" "\
3124 For buffers thrown into help mode, augment for EIEIO.
3125 Arguments UNUSED are not used.
3127 \(fn &rest UNUSED)" nil nil
)
3131 ;;; End of automatically extracted autoloads.