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>
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 ;; EIEIO is a series of Lisp routines which implements a subset of
28 ;; CLOS, the Common Lisp Object System. In addition, EIEIO also adds
29 ;; a few new features which help it integrate more strongly with the
30 ;; Emacs running environment.
32 ;; See eieio.texi for complete documentation on using this package.
34 ;; Note: the implementation of the c3 algorithm is based on:
35 ;; Kim Barrett et al.: A Monotonic Superclass Linearization for Dylan
37 ;; http://192.220.96.201/dylan/linearization-oopsla96.html
39 ;; There is funny stuff going on with typep and deftype. This
40 ;; is the only way I seem to be able to make this stuff load properly.
42 ;; @TODO - fix :initform to be a form, not a quoted value
43 ;; @TODO - Prefix non-clos functions with `eieio-'.
47 (eval-when-compile (require 'cl
)) ;FIXME: Use cl-lib!
49 (defvar eieio-version
"1.3"
50 "Current version of EIEIO.")
52 (defun eieio-version ()
53 "Display the current version of EIEIO."
55 (message eieio-version
))
58 ;; About the above. EIEIO must process its own code when it compiles
59 ;; itself, thus, by eval-and-compiling ourselves, we solve the problem.
62 (if (fboundp 'compiled-function-arglist
)
64 ;; XEmacs can only access a compiled functions arglist like this:
65 (defalias 'eieio-compiled-function-arglist
'compiled-function-arglist
)
67 ;; Emacs doesn't have this function, but since FUNC is a vector, we can just
68 ;; grab the appropriate element.
69 (defun eieio-compiled-function-arglist (func)
70 "Return the argument list for the compiled function FUNC."
77 ;; Variable declarations.
80 (defvar eieio-hook nil
81 "This hook is executed, then cleared each time `defclass' is called.")
83 (defvar eieio-error-unsupported-class-tags nil
84 "Non-nil to throw an error if an encountered tag is unsupported.
85 This may prevent classes from CLOS applications from being used with EIEIO
86 since EIEIO does not support all CLOS tags.")
88 (defvar eieio-skip-typecheck nil
89 "If non-nil, skip all slot typechecking.
90 Set this to t permanently if a program is functioning well to get a
91 small speed increase. This variable is also used internally to handle
92 default setting for optimization purposes.")
94 (defvar eieio-optimize-primary-methods-flag t
95 "Non-nil means to optimize the method dispatch on primary methods.")
97 (defvar eieio-initializing-object nil
98 "Set to non-nil while initializing an object.")
100 (defconst eieio-unbound
101 (if (and (boundp 'eieio-unbound
) (symbolp eieio-unbound
))
103 (make-symbol "unbound"))
104 "Uninterned symbol representing an unbound slot in an object.")
106 ;; This is a bootstrap for eieio-default-superclass so it has a value
107 ;; while it is being built itself.
108 (defvar eieio-default-superclass nil
)
110 ;; FIXME: The constants below should have an `eieio-' prefix added!!
111 (defconst class-symbol
1 "Class's symbol (self-referencing.).")
112 (defconst class-parent
2 "Class parent slot.")
113 (defconst class-children
3 "Class children class slot.")
114 (defconst class-symbol-obarray
4 "Obarray permitting fast access to variable position indexes.")
116 ;; the word "public" here is leftovers from the very first version.
118 (defconst class-public-a
5 "Class attribute index.")
119 (defconst class-public-d
6 "Class attribute defaults index.")
120 (defconst class-public-doc
7 "Class documentation strings for attributes.")
121 (defconst class-public-type
8 "Class type for a slot.")
122 (defconst class-public-custom
9 "Class custom type for a slot.")
123 (defconst class-public-custom-label
10 "Class custom group for a slot.")
124 (defconst class-public-custom-group
11 "Class custom group for a slot.")
125 (defconst class-public-printer
12 "Printer for a slot.")
126 (defconst class-protection
13 "Class protection for a slot.")
127 (defconst class-initarg-tuples
14 "Class initarg tuples list.")
128 (defconst class-class-allocation-a
15 "Class allocated attributes.")
129 (defconst class-class-allocation-doc
16 "Class allocated documentation.")
130 (defconst class-class-allocation-type
17 "Class allocated value type.")
131 (defconst class-class-allocation-custom
18 "Class allocated custom descriptor.")
132 (defconst class-class-allocation-custom-label
19 "Class allocated custom descriptor.")
133 (defconst class-class-allocation-custom-group
20 "Class allocated custom group.")
134 (defconst class-class-allocation-printer
21 "Class allocated printer for a slot.")
135 (defconst class-class-allocation-protection
22 "Class allocated protection list.")
136 (defconst class-class-allocation-values
23 "Class allocated value vector.")
137 (defconst class-default-object-cache
24
138 "Cache index of what a newly created object would look like.
139 This will speed up instantiation time as only a `copy-sequence' will
140 be needed, instead of looping over all the values and setting them
142 (defconst class-options
25
143 "Storage location of tagged class options.
144 Stored outright without modifications or stripping.")
146 (defconst class-num-slots
26
147 "Number of slots in the class definition object.")
149 (defconst object-class
1 "Index in an object vector where the class is stored.")
150 (defconst object-name
2 "Index in an object where the name is stored.")
152 (defconst method-static
0 "Index into :static tag on a method.")
153 (defconst method-before
1 "Index into :before tag on a method.")
154 (defconst method-primary
2 "Index into :primary tag on a method.")
155 (defconst method-after
3 "Index into :after tag on a method.")
156 (defconst method-num-lists
4 "Number of indexes into methods vector in which groups of functions are kept.")
157 (defconst method-generic-before
4 "Index into generic :before tag on a method.")
158 (defconst method-generic-primary
5 "Index into generic :primary tag on a method.")
159 (defconst method-generic-after
6 "Index into generic :after tag on a method.")
160 (defconst method-num-slots
7 "Number of indexes into a method's vector.")
162 (defsubst eieio-specialized-key-to-generic-key
(key)
163 "Convert a specialized KEY into a generic method key."
164 (cond ((eq key method-static
) 0) ;; don't convert
165 ((< key method-num-lists
) (+ key
3)) ;; The conversion
166 (t key
) ;; already generic.. maybe.
170 ;;; Important macros used in eieio.
172 (defmacro class-v
(class)
173 "Internal: Return the class vector from the CLASS symbol."
174 ;; No check: If eieio gets this far, it's probably been checked already.
175 `(get ,class
'eieio-class-definition
))
177 (defmacro class-p
(class)
178 "Return t if CLASS is a valid class vector.
180 ;; this new method is faster since it doesn't waste time checking lots of
183 (eq (aref (class-v ,class
) 0) 'defclass
)
186 (defmacro eieio-object-p
(obj)
187 "Return non-nil if OBJ is an EIEIO object."
190 (and (eq (aref tobj
0) 'object
)
191 (class-p (aref tobj object-class
))))
193 (defalias 'object-p
'eieio-object-p
)
195 (defmacro class-constructor
(class)
196 "Return the symbol representing the constructor of CLASS."
197 `(aref (class-v ,class
) class-symbol
))
199 (defmacro generic-p
(method)
200 "Return t if symbol METHOD is a generic function.
201 Only methods have the symbol `eieio-method-obarray' as a property
202 \(which contains a list of all bindings to that method type.)"
203 `(and (fboundp ,method
) (get ,method
'eieio-method-obarray
)))
205 (defun generic-primary-only-p (method)
206 "Return t if symbol METHOD is a generic function with only primary methods.
207 Only methods have the symbol `eieio-method-obarray' as a property (which
208 contains a list of all bindings to that method type.)
209 Methods with only primary implementations are executed in an optimized way."
210 (and (generic-p method
)
211 (let ((M (get method
'eieio-method-tree
)))
212 (and (< 0 (length (aref M method-primary
)))
213 (not (aref M method-static
))
214 (not (aref M method-before
))
215 (not (aref M method-after
))
216 (not (aref M method-generic-before
))
217 (not (aref M method-generic-primary
))
218 (not (aref M method-generic-after
))))
221 (defun generic-primary-only-one-p (method)
222 "Return t if symbol METHOD is a generic function with only primary methods.
223 Only methods have the symbol `eieio-method-obarray' as a property (which
224 contains a list of all bindings to that method type.)
225 Methods with only primary implementations are executed in an optimized way."
226 (and (generic-p method
)
227 (let ((M (get method
'eieio-method-tree
)))
228 (and (= 1 (length (aref M method-primary
)))
229 (not (aref M method-static
))
230 (not (aref M method-before
))
231 (not (aref M method-after
))
232 (not (aref M method-generic-before
))
233 (not (aref M method-generic-primary
))
234 (not (aref M method-generic-after
))))
237 (defmacro class-option-assoc
(list option
)
238 "Return from LIST the found OPTION, or nil if it doesn't exist."
239 `(car-safe (cdr (memq ,option
,list
))))
241 (defmacro class-option
(class option
)
242 "Return the value stored for CLASS' OPTION.
243 Return nil if that option doesn't exist."
244 `(class-option-assoc (aref (class-v ,class
) class-options
) ',option
))
246 (defmacro class-abstract-p
(class)
247 "Return non-nil if CLASS is abstract.
248 Abstract classes cannot be instantiated."
249 `(class-option ,class
:abstract
))
251 (defmacro class-method-invocation-order
(class)
252 "Return the invocation order of CLASS.
253 Abstract classes cannot be instantiated."
254 `(or (class-option ,class
:method-invocation-order
)
258 ;;; Defining a new class
260 (defmacro defclass
(name superclass slots
&rest options-and-doc
)
261 "Define NAME as a new class derived from SUPERCLASS with SLOTS.
262 OPTIONS-AND-DOC is used as the class' options and base documentation.
263 SUPERCLASS is a list of superclasses to inherit from, with SLOTS
264 being the slots residing in that class definition. NOTE: Currently
265 only one slot may exist in SUPERCLASS as multiple inheritance is not
266 yet supported. Supported tags are:
268 :initform - Initializing form.
269 :initarg - Tag used during initialization.
270 :accessor - Tag used to create a function to access this slot.
271 :allocation - Specify where the value is stored.
272 Defaults to `:instance', but could also be `:class'.
273 :writer - A function symbol which will `write' an object's slot.
274 :reader - A function symbol which will `read' an object.
275 :type - The type of data allowed in this slot (see `typep').
277 - A string documenting use of this slot.
279 The following are extensions on CLOS:
280 :protection - Specify protection for this slot.
281 Defaults to `:public'. Also use `:protected', or `:private'.
282 :custom - When customizing an object, the custom :type. Public only.
283 :label - A text string label used for a slot when customizing.
284 :group - Name of a customization group this slot belongs in.
285 :printer - A function to call to print the value of a slot.
286 See `eieio-override-prin1' as an example.
288 A class can also have optional options. These options happen in place
289 of documentation (including a :documentation tag), in addition to
290 documentation, or not at all. Supported options are:
292 :documentation - The doc-string used for this class.
294 Options added to EIEIO:
296 :allow-nil-initform - Non-nil to skip typechecking of null initforms.
297 :custom-groups - List of custom group names. Organizes slots into
298 reasonable groups for customizations.
299 :abstract - Non-nil to prevent instances of this class.
300 If a string, use as an error string if someone does
301 try to make an instance.
302 :method-invocation-order
303 - Control the method invocation order if there is
304 multiple inheritance. Valid values are:
305 :breadth-first - The default.
308 Options in CLOS not supported in EIEIO:
310 :metaclass - Class to use in place of `standard-class'
311 :default-initargs - Initargs to use when initializing new objects of
314 Due to the way class options are set up, you can add any tags you wish,
315 and reference them using the function `class-option'."
316 ;; We must `eval-and-compile' this so that when we byte compile
317 ;; an eieio program, there is no need to load it ahead of time.
318 ;; It also provides lots of nice debugging errors at compile time.
320 (eieio-defclass ',name
',superclass
',slots
',options-and-doc
)))
322 (defvar eieio-defclass-autoload-map
(make-vector 7 nil
)
323 "Symbol map of superclasses we find in autoloads.")
325 ;; We autoload this because it's used in `make-autoload'.
327 (defun eieio-defclass-autoload (cname superclasses filename doc
)
328 "Create autoload symbols for the EIEIO class CNAME.
329 SUPERCLASSES are the superclasses that CNAME inherits from.
330 DOC is the docstring for CNAME.
331 This function creates a mock-class for CNAME and adds it into
332 SUPERCLASSES as children.
333 It creates an autoload function for CNAME's constructor."
334 ;; Assume we've already debugged inputs.
336 (let* ((oldc (when (class-p cname
) (class-v cname
)))
337 (newc (make-vector class-num-slots nil
))
340 nil
;; Do nothing if we already have this class.
342 ;; Create the class in NEWC, but don't fill anything else in.
343 (aset newc
0 'defclass
)
344 (aset newc class-symbol cname
)
346 (let ((clear-parent nil
))
348 (when (not superclasses
)
349 (setq superclasses
'(eieio-default-superclass)
353 ;; Hook our new class into the existing structures so we can
354 ;; autoload it later.
355 (dolist (SC superclasses
)
358 ;; TODO - If we create an autoload that is in the map, that
359 ;; map needs to be cleared!
362 ;; Does our parent exist?
363 (if (not (class-p SC
))
365 ;; Create a symbol for this parent, and then store this
366 ;; parent on that symbol.
367 (let ((sym (intern (symbol-name SC
) eieio-defclass-autoload-map
)))
368 (if (not (boundp sym
))
369 (set sym
(list cname
))
370 (add-to-list sym cname
))
373 ;; We have a parent, save the child in there.
374 (when (not (member cname
(aref (class-v SC
) class-children
)))
375 (aset (class-v SC
) class-children
376 (cons cname
(aref (class-v SC
) class-children
)))))
378 ;; save parent in child
379 (aset newc class-parent
(cons SC
(aref newc class-parent
)))
382 ;; turn this into a usable self-pointing symbol
385 ;; Store the new class vector definition into the symbol. We need to
386 ;; do this first so that we can call defmethod for the accessor.
387 ;; The vector will be updated by the following while loop and will not
388 ;; need to be stored a second time.
389 (put cname
'eieio-class-definition newc
)
392 (if clear-parent
(aset newc class-parent nil
))
394 ;; Create an autoload on top of our constructor function.
395 (autoload cname filename doc nil nil
)
396 (autoload (intern (concat (symbol-name cname
) "-p")) filename
"" nil nil
)
397 (autoload (intern (concat (symbol-name cname
) "-child-p")) filename
"" nil nil
)
398 (autoload (intern (concat (symbol-name cname
) "-list-p")) filename
"" nil nil
)
402 (defsubst eieio-class-un-autoload
(cname)
403 "If class CNAME is in an autoload state, load its file."
404 (when (eq (car-safe (symbol-function cname
)) 'autoload
)
405 (load-library (car (cdr (symbol-function cname
))))))
407 (defun eieio-defclass (cname superclasses slots options-and-doc
)
408 ;; FIXME: Most of this should be moved to the `defclass' macro.
409 "Define CNAME as a new subclass of SUPERCLASSES.
410 SLOTS are the slots residing in that class definition, and options or
411 documentation OPTIONS-AND-DOC is the toplevel documentation for this class.
412 See `defclass' for more information."
413 ;; Run our eieio-hook each time, and clear it when we are done.
414 ;; This way people can add hooks safely if they want to modify eieio
415 ;; or add definitions when eieio is loaded or something like that.
416 (run-hooks 'eieio-hook
)
417 (setq eieio-hook nil
)
419 (if (not (listp superclasses
))
420 (signal 'wrong-type-argument
'(listp superclasses
)))
422 (let* ((pname superclasses
)
423 (newc (make-vector class-num-slots nil
))
424 (oldc (when (class-p cname
) (class-v cname
)))
425 (groups nil
) ;; list of groups id'd from slots
429 (aset newc
0 'defclass
)
430 (aset newc class-symbol cname
)
432 ;; If this class already existed, and we are updating its structure,
433 ;; make sure we keep the old child list. This can cause bugs, but
434 ;; if no new slots are created, it also saves time, and prevents
435 ;; method table breakage, particularly when the users is only
436 ;; byte compiling an EIEIO file.
438 (aset newc class-children
(aref oldc class-children
))
439 ;; If the old class did not exist, but did exist in the autoload map, then adopt those children.
440 ;; This is like the above, but deals with autoloads nicely.
441 (let ((sym (intern-soft (symbol-name cname
) eieio-defclass-autoload-map
)))
444 (aset newc class-children
(symbol-value sym
))
446 (unintern (symbol-name cname
) eieio-defclass-autoload-map
)
450 (cond ((and (stringp (car options-and-doc
))
451 (/= 1 (%
(length options-and-doc
) 2)))
452 (error "Too many arguments to `defclass'"))
453 ((and (symbolp (car options-and-doc
))
454 (/= 0 (%
(length options-and-doc
) 2)))
455 (error "Too many arguments to `defclass'"))
459 (if (stringp (car options-and-doc
))
460 (cons :documentation options-and-doc
)
466 (if (and (car pname
) (symbolp (car pname
)))
467 (if (not (class-p (car pname
)))
469 (error "Given parent class %s is not a class" (car pname
))
470 ;; good parent class...
471 ;; save new child in parent
472 (when (not (member cname
(aref (class-v (car pname
)) class-children
)))
473 (aset (class-v (car pname
)) class-children
474 (cons cname
(aref (class-v (car pname
)) class-children
))))
475 ;; Get custom groups, and store them into our local copy.
476 (mapc (lambda (g) (add-to-list 'groups g
))
477 (class-option (car pname
) :custom-groups
))
478 ;; save parent in child
479 (aset newc class-parent
(cons (car pname
) (aref newc class-parent
))))
480 (error "Invalid parent class %s" pname
))
481 (setq pname
(cdr pname
)))
482 ;; Reverse the list of our parents so that they are prioritized in
483 ;; the same order as specified in the code.
484 (aset newc class-parent
(nreverse (aref newc class-parent
))) )
485 ;; If there is nothing to loop over, then inherit from the
486 ;; default superclass.
487 (unless (eq cname
'eieio-default-superclass
)
488 ;; adopt the default parent here, but clear it later...
490 ;; save new child in parent
491 (if (not (member cname
(aref (class-v 'eieio-default-superclass
) class-children
)))
492 (aset (class-v 'eieio-default-superclass
) class-children
493 (cons cname
(aref (class-v 'eieio-default-superclass
) class-children
))))
494 ;; save parent in child
495 (aset newc class-parent
(list eieio-default-superclass
))))
497 ;; turn this into a usable self-pointing symbol
500 ;; These two tests must be created right away so we can have self-
501 ;; referencing classes. ei, a class whose slot can contain only
502 ;; pointers to itself.
504 ;; Create the test function
505 (let ((csym (intern (concat (symbol-name cname
) "-p"))))
507 (list 'lambda
(list 'obj
)
508 (format "Test OBJ to see if it an object of type %s" cname
)
509 (list 'and
'(eieio-object-p obj
)
510 (list 'same-class-p
'obj cname
)))))
512 ;; Make sure the method invocation order is a valid value.
513 (let ((io (class-option-assoc options
:method-invocation-order
)))
514 (when (and io
(not (member io
'(:depth-first
:breadth-first
:c3
))))
515 (error "Method invocation order %s is not allowed" io
)
518 ;; Create a handy child test too
519 (let ((csym (intern (concat (symbol-name cname
) "-child-p"))))
523 "Test OBJ to see if it an object is a child of type %s"
525 (and (eieio-object-p obj
)
526 (object-of-class-p obj
,cname
))))
528 ;; Create a handy list of the class test too
529 (let ((csym (intern (concat (symbol-name cname
) "-list-p"))))
533 "Test OBJ to see if it a list of objects which are a child of type %s"
536 (let ((ans t
)) ;; nil is valid
537 ;; Loop over all the elements of the input list, test
538 ;; each to make sure it is a child of the desired object class.
540 (setq ans
(and (eieio-object-p (car obj
))
541 (object-of-class-p (car obj
) ,cname
)))
542 (setq obj
(cdr obj
)))
545 ;; When using typep, (typep OBJ 'myclass) returns t for objects which
546 ;; are subclasses of myclass. For our predicates, however, it is
547 ;; important for EIEIO to be backwards compatible, where
548 ;; myobject-p, and myobject-child-p are different.
549 ;; "cl" uses this technique to specify symbols with specific typep
550 ;; test, so we can let typep have the CLOS documented behavior
551 ;; while keeping our above predicate clean.
553 ;; It would be cleaner to use `defsetf' here, but that requires cl
555 (put cname
'cl-deftype-handler
556 (list 'lambda
() `(list 'satisfies
(quote ,csym
)))))
558 ;; Before adding new slots, let's add all the methods and classes
559 ;; in from the parent class.
560 (eieio-copy-parents-into-subclass newc superclasses
)
562 ;; Store the new class vector definition into the symbol. We need to
563 ;; do this first so that we can call defmethod for the accessor.
564 ;; The vector will be updated by the following while loop and will not
565 ;; need to be stored a second time.
566 (put cname
'eieio-class-definition newc
)
568 ;; Query each slot in the declaration list and mangle into the
569 ;; class structure I have defined.
571 (let* ((slot1 (car slots
))
574 (acces (plist-get slot
':accessor
))
575 (init (or (plist-get slot
':initform
)
576 (if (member ':initform slot
) nil
578 (initarg (plist-get slot
':initarg
))
579 (docstr (plist-get slot
':documentation
))
580 (prot (plist-get slot
':protection
))
581 (reader (plist-get slot
':reader
))
582 (writer (plist-get slot
':writer
))
583 (alloc (plist-get slot
':allocation
))
584 (type (plist-get slot
':type
))
585 (custom (plist-get slot
':custom
))
586 (label (plist-get slot
':label
))
587 (customg (plist-get slot
':group
))
588 (printer (plist-get slot
':printer
))
590 (skip-nil (class-option-assoc options
:allow-nil-initform
))
593 (if eieio-error-unsupported-class-tags
596 (if (not (member (car tmp
) '(:accessor
611 (signal 'invalid-slot-type
(list (car tmp
))))
612 (setq tmp
(cdr (cdr tmp
))))))
614 ;; Clean up the meaning of protection.
615 (cond ((or (eq prot
'public
) (eq prot
:public
)) (setq prot nil
))
616 ((or (eq prot
'protected
) (eq prot
:protected
)) (setq prot
'protected
))
617 ((or (eq prot
'private
) (eq prot
:private
)) (setq prot
'private
))
619 (t (signal 'invalid-slot-type
(list ':protection prot
))))
621 ;; Make sure the :allocation parameter has a valid value.
622 (if (not (or (not alloc
) (eq alloc
:class
) (eq alloc
:instance
)))
623 (signal 'invalid-slot-type
(list ':allocation alloc
)))
625 ;; The default type specifier is supposed to be t, meaning anything.
626 (if (not type
) (setq type t
))
628 ;; Label is nil, or a string
629 (if (not (or (null label
) (stringp label
)))
630 (signal 'invalid-slot-type
(list ':label label
)))
632 ;; Is there an initarg, but allocation of class?
633 (if (and initarg
(eq alloc
:class
))
634 (message "Class allocated slots do not need :initarg"))
636 ;; intern the symbol so we can use it blankly
637 (if initarg
(set initarg initarg
))
639 ;; The customgroup should be a list of symbols
640 (cond ((null customg
)
641 (setq customg
'(default)))
642 ((not (listp customg
))
643 (setq customg
(list customg
))))
644 ;; The customgroup better be a symbol, or list of symbols.
646 (if (not (symbolp cg
))
647 (signal 'invalid-slot-type
(list ':group cg
))))
650 ;; First up, add this slot into our new class.
651 (eieio-add-new-slot newc name init docstr type custom label customg printer
652 prot initarg alloc
'defaultoverride skip-nil
)
654 ;; We need to id the group, and store them in a group list attribute.
655 (mapc (lambda (cg) (add-to-list 'groups cg
)) customg
)
657 ;; Anyone can have an accessor function. This creates a function
658 ;; of the specified name, and also performs a `defsetf' if applicable
659 ;; so that users can `setf' the space returned by this function.
663 acces
(if (eq alloc
:class
) :static
:primary
) cname
666 "Retrieves the slot `%s' from an object of class `%s'"
668 (if (slot-boundp this
',name
)
669 (eieio-oref this
',name
)
670 ;; Else - Some error? nil?
673 (if (fboundp 'gv-define-setter
)
674 ;; FIXME: We should move more of eieio-defclass into the
675 ;; defclass macro so we don't have to use `eval' and require
677 (eval `(gv-define-setter ,acces
(eieio--store eieio--object
)
678 (list 'eieio-oset eieio--object
'',name
680 ;; Provide a setf method. It would be cleaner to use
681 ;; defsetf, but that would require CL at runtime.
682 (put acces
'setf-method
684 (let* ((--widget-sym-- (make-symbol "--widget--"))
685 (--store-sym-- (make-symbol "--store--")))
687 (list --widget-sym--
)
690 (list 'eieio-oset --widget-sym--
'',name
692 (list 'getfoo --widget-sym--
))))))))
694 ;; If a writer is defined, then create a generic method of that
695 ;; name whose purpose is to set the value of the slot.
699 `(lambda (this value
)
700 ,(format "Set the slot `%s' of an object of class `%s'"
702 (setf (slot-value this
',name
) value
))))
703 ;; If a reader is defined, then create a generic method
704 ;; of that name whose purpose is to access this slot value.
709 ,(format "Access the slot `%s' from object of class `%s'"
711 (slot-value this
',name
))))
713 (setq slots
(cdr slots
)))
715 ;; Now that everything has been loaded up, all our lists are backwards!
717 (aset newc class-public-a
(nreverse (aref newc class-public-a
)))
718 (aset newc class-public-d
(nreverse (aref newc class-public-d
)))
719 (aset newc class-public-doc
(nreverse (aref newc class-public-doc
)))
720 (aset newc class-public-type
721 (apply 'vector
(nreverse (aref newc class-public-type
))))
722 (aset newc class-public-custom
(nreverse (aref newc class-public-custom
)))
723 (aset newc class-public-custom-label
(nreverse (aref newc class-public-custom-label
)))
724 (aset newc class-public-custom-group
(nreverse (aref newc class-public-custom-group
)))
725 (aset newc class-public-printer
(nreverse (aref newc class-public-printer
)))
726 (aset newc class-protection
(nreverse (aref newc class-protection
)))
727 (aset newc class-initarg-tuples
(nreverse (aref newc class-initarg-tuples
)))
729 ;; The storage for class-class-allocation-type needs to be turned into
731 (aset newc class-class-allocation-type
732 (apply 'vector
(aref newc class-class-allocation-type
)))
734 ;; Also, take class allocated values, and vectorize them for speed.
735 (aset newc class-class-allocation-values
736 (apply 'vector
(aref newc class-class-allocation-values
)))
738 ;; Attach slot symbols into an obarray, and store the index of
739 ;; this slot as the variable slot in this new symbol. We need to
740 ;; know about primes, because obarrays are best set in vectors of
741 ;; prime number length, and we also need to make our vector small
742 ;; to save space, and also optimal for the number of items we have.
744 (pubsyms (aref newc class-public-a
))
745 (prots (aref newc class-protection
))
747 (vl (let ((primes '( 3 5 7 11 13 17 19 23 29 31 37 41 43 47
748 53 59 61 67 71 73 79 83 89 97 101 )))
749 (while (and primes
(< (car primes
) l
))
750 (setq primes
(cdr primes
)))
752 (oa (make-vector vl
0))
755 (setq newsym
(intern (symbol-name (car pubsyms
)) oa
))
758 (if (car prots
) (put newsym
'protection
(car prots
)))
759 (setq pubsyms
(cdr pubsyms
)
761 (aset newc class-symbol-obarray oa
)
764 ;; Create the constructor function
765 (if (class-option-assoc options
:abstract
)
766 ;; Abstract classes cannot be instantiated. Say so.
767 (let ((abs (class-option-assoc options
:abstract
)))
768 (if (not (stringp abs
))
769 (setq abs
(format "Class %s is abstract" cname
)))
771 `(lambda (&rest stuff
)
772 ,(format "You cannot create a new object of type %s" cname
)
775 ;; Non-abstract classes need a constructor.
777 `(lambda (newname &rest slots
)
778 ,(format "Create a new object with name NAME of class type %s" cname
)
779 (apply 'constructor
,cname newname slots
)))
782 ;; Set up a specialized doc string.
783 ;; Use stored value since it is calculated in a non-trivial way
784 (put cname
'variable-documentation
785 (class-option-assoc options
:documentation
))
787 ;; Save the file location where this class is defined.
788 (let ((fname (if load-in-progress
793 (when (string-match "\\.elc$" fname
)
794 (setq fname
(substring fname
0 (1- (length fname
)))))
795 (put cname
'class-location fname
)))
797 ;; We have a list of custom groups. Store them into the options.
798 (let ((g (class-option-assoc options
:custom-groups
)))
799 (mapc (lambda (cg) (add-to-list 'g cg
)) groups
)
800 (if (memq :custom-groups options
)
801 (setcar (cdr (memq :custom-groups options
)) g
)
802 (setq options
(cons :custom-groups
(cons g options
)))))
804 ;; Set up the options we have collected.
805 (aset newc class-options options
)
807 ;; if this is a superclass, clear out parent (which was set to the
808 ;; default superclass eieio-default-superclass)
809 (if clearparent
(aset newc class-parent nil
))
811 ;; Create the cached default object.
812 (let ((cache (make-vector (+ (length (aref newc class-public-a
))
814 (aset cache
0 'object
)
815 (aset cache object-class cname
)
816 (aset cache object-name
'default-cache-object
)
817 (let ((eieio-skip-typecheck t
))
818 ;; All type-checking has been done to our satisfaction
819 ;; before this call. Don't waste our time in this call..
820 (eieio-set-defaults cache t
))
821 (aset newc class-default-object-cache cache
))
823 ;; Return our new class object
828 (defun eieio-perform-slot-validation-for-default (slot spec value skipnil
)
829 "For SLOT, signal if SPEC does not match VALUE.
830 If SKIPNIL is non-nil, then if VALUE is nil return t instead."
831 (if (and (not (eieio-eval-default-p value
))
832 (not eieio-skip-typecheck
)
833 (not (and skipnil
(null value
)))
834 (not (eieio-perform-slot-validation spec value
)))
835 (signal 'invalid-slot-type
(list slot spec value
))))
837 (defun eieio-add-new-slot (newc a d doc type cust label custg print prot init alloc
838 &optional defaultoverride skipnil
)
839 "Add into NEWC attribute A.
840 If A already exists in NEWC, then do nothing. If it doesn't exist,
841 then also add in D (default), DOC, TYPE, CUST, LABEL, CUSTG, PRINT, PROT, and INIT arg.
842 Argument ALLOC specifies if the slot is allocated per instance, or per class.
843 If optional DEFAULTOVERRIDE is non-nil, then if A exists in NEWC,
844 we must override its value for a default.
845 Optional argument SKIPNIL indicates if type checking should be skipped
846 if default value is nil."
847 ;; Make sure we duplicate those items that are sequences.
849 (if (sequencep d
) (setq d
(copy-sequence d
)))
850 ;; This copy can fail on a cons cell with a non-cons in the cdr. Let's skip it if it doesn't work.
852 (if (sequencep type
) (setq type
(copy-sequence type
)))
853 (if (sequencep cust
) (setq cust
(copy-sequence cust
)))
854 (if (sequencep custg
) (setq custg
(copy-sequence custg
)))
856 ;; To prevent override information w/out specification of storage,
857 ;; we need to do this little hack.
858 (if (member a
(aref newc class-class-allocation-a
)) (setq alloc
':class
))
860 (if (or (not alloc
) (and (symbolp alloc
) (eq alloc
':instance
)))
861 ;; In this case, we modify the INSTANCE version of a given slot.
865 ;; Only add this element if it is so-far unique
866 (if (not (member a
(aref newc class-public-a
)))
868 (eieio-perform-slot-validation-for-default a type d skipnil
)
869 (aset newc class-public-a
(cons a
(aref newc class-public-a
)))
870 (aset newc class-public-d
(cons d
(aref newc class-public-d
)))
871 (aset newc class-public-doc
(cons doc
(aref newc class-public-doc
)))
872 (aset newc class-public-type
(cons type
(aref newc class-public-type
)))
873 (aset newc class-public-custom
(cons cust
(aref newc class-public-custom
)))
874 (aset newc class-public-custom-label
(cons label
(aref newc class-public-custom-label
)))
875 (aset newc class-public-custom-group
(cons custg
(aref newc class-public-custom-group
)))
876 (aset newc class-public-printer
(cons print
(aref newc class-public-printer
)))
877 (aset newc class-protection
(cons prot
(aref newc class-protection
)))
878 (aset newc class-initarg-tuples
(cons (cons init a
) (aref newc class-initarg-tuples
)))
880 ;; When defaultoverride is true, we are usually adding new local
881 ;; attributes which must override the default value of any slot
882 ;; passed in by one of the parent classes.
883 (when defaultoverride
884 ;; There is a match, and we must override the old value.
885 (let* ((ca (aref newc class-public-a
))
887 (num (- (length ca
) (length np
)))
888 (dp (if np
(nthcdr num
(aref newc class-public-d
))
890 (tp (if np
(nth num
(aref newc class-public-type
))))
893 (error "EIEIO internal error overriding default value for %s"
895 ;; If type is passed in, is it the same?
896 (if (not (eq type t
))
897 (if (not (equal type tp
))
899 "Child slot type `%s' does not match inherited type `%s' for `%s'"
901 ;; If we have a repeat, only update the initarg...
902 (unless (eq d eieio-unbound
)
903 (eieio-perform-slot-validation-for-default a tp d skipnil
)
905 ;; If we have a new initarg, check for it.
907 (let* ((inits (aref newc class-initarg-tuples
))
908 (inita (rassq a inits
)))
909 ;; Replace the CAR of the associate INITA.
910 ;;(message "Initarg: %S replace %s" inita init)
914 ;; PLN Tue Jun 26 11:57:06 2007 : The protection is
915 ;; checked and SHOULD match the superclass
916 ;; protection. Otherwise an error is thrown. However
917 ;; I wonder if a more flexible schedule might be
920 ;; EML - We used to have (if prot... here,
921 ;; but a prot of 'nil means public.
923 (let ((super-prot (nth num
(aref newc class-protection
)))
925 (if (not (eq prot super-prot
))
926 (error "Child slot protection `%s' does not match inherited protection `%s' for `%s'"
930 ;; PLN Tue Jun 26 11:57:06 2007 :
931 ;; Do a non redundant combination of ancient custom
932 ;; groups and new ones.
935 (nthcdr num
(aref newc class-public-custom-group
)))
937 (list2 (if (listp custg
) custg
(list custg
))))
938 (if (< (length list1
) (length list2
))
939 (setq list1
(prog1 list2
(setq list2 list1
))))
941 (unless (memq elt list1
)
943 (setcar groups list1
)))
946 ;; PLN Mon Jun 25 22:44:34 2007 : If a new cust is
947 ;; set, simply replaces the old one.
949 ;; (message "Custom type redefined to %s" cust)
950 (setcar (nthcdr num
(aref newc class-public-custom
)) cust
))
952 ;; If a new label is specified, it simply replaces
955 ;; (message "Custom label redefined to %s" label)
956 (setcar (nthcdr num
(aref newc class-public-custom-label
)) label
))
959 ;; PLN Sat Jun 30 17:24:42 2007 : when a new
960 ;; doc is specified, simply replaces the old one.
962 ;;(message "Documentation redefined to %s" doc)
963 (setcar (nthcdr num
(aref newc class-public-doc
))
967 ;; If a new printer is specified, it simply replaces
970 ;; (message "printer redefined to %s" print)
971 (setcar (nthcdr num
(aref newc class-public-printer
)) print
))
976 ;; CLASS ALLOCATED SLOTS
977 (let ((value (eieio-default-eval-maybe d
)))
978 (if (not (member a
(aref newc class-class-allocation-a
)))
980 (eieio-perform-slot-validation-for-default a type value skipnil
)
981 ;; Here we have found a :class version of a slot. This
982 ;; requires a very different approach.
983 (aset newc class-class-allocation-a
(cons a
(aref newc class-class-allocation-a
)))
984 (aset newc class-class-allocation-doc
(cons doc
(aref newc class-class-allocation-doc
)))
985 (aset newc class-class-allocation-type
(cons type
(aref newc class-class-allocation-type
)))
986 (aset newc class-class-allocation-custom
(cons cust
(aref newc class-class-allocation-custom
)))
987 (aset newc class-class-allocation-custom-label
(cons label
(aref newc class-class-allocation-custom-label
)))
988 (aset newc class-class-allocation-custom-group
(cons custg
(aref newc class-class-allocation-custom-group
)))
989 (aset newc class-class-allocation-protection
(cons prot
(aref newc class-class-allocation-protection
)))
990 ;; Default value is stored in the 'values section, since new objects
991 ;; can't initialize from this element.
992 (aset newc class-class-allocation-values
(cons value
(aref newc class-class-allocation-values
))))
993 (when defaultoverride
994 ;; There is a match, and we must override the old value.
995 (let* ((ca (aref newc class-class-allocation-a
))
997 (num (- (length ca
) (length np
)))
1000 (aref newc class-class-allocation-values
))
1002 (tp (if np
(nth num
(aref newc class-class-allocation-type
))
1005 (error "EIEIO internal error overriding default value for %s"
1007 ;; If type is passed in, is it the same?
1008 (if (not (eq type t
))
1009 (if (not (equal type tp
))
1011 "Child slot type `%s' does not match inherited type `%s' for `%s'"
1013 ;; EML - Note: the only reason to override a class bound slot
1014 ;; is to change the default, so allow unbound in.
1016 ;; If we have a repeat, only update the value...
1017 (eieio-perform-slot-validation-for-default a tp value skipnil
)
1020 ;; PLN Tue Jun 26 11:57:06 2007 : The protection is
1021 ;; checked and SHOULD match the superclass
1022 ;; protection. Otherwise an error is thrown. However
1023 ;; I wonder if a more flexible schedule might be
1026 (car (nthcdr num
(aref newc class-class-allocation-protection
)))))
1027 (if (not (eq prot super-prot
))
1028 (error "Child slot protection `%s' does not match inherited protection `%s' for `%s'"
1029 prot super-prot a
)))
1030 ;; Do a non redundant combination of ancient custom groups
1034 (nthcdr num
(aref newc class-class-allocation-custom-group
)))
1035 (list1 (car groups
))
1036 (list2 (if (listp custg
) custg
(list custg
))))
1037 (if (< (length list1
) (length list2
))
1038 (setq list1
(prog1 list2
(setq list2 list1
))))
1040 (unless (memq elt list1
)
1042 (setcar groups list1
)))
1044 ;; PLN Sat Jun 30 17:24:42 2007 : when a new
1045 ;; doc is specified, simply replaces the old one.
1047 ;;(message "Documentation redefined to %s" doc)
1048 (setcar (nthcdr num
(aref newc class-class-allocation-doc
))
1052 ;; If a new printer is specified, it simply replaces
1055 ;; (message "printer redefined to %s" print)
1056 (setcar (nthcdr num
(aref newc class-class-allocation-printer
)) print
))
1062 (defun eieio-copy-parents-into-subclass (newc parents
)
1063 "Copy into NEWC the slots of PARENTS.
1064 Follow the rules of not overwriting early parents when applying to
1065 the new child class."
1066 (let ((ps (aref newc class-parent
))
1067 (sn (class-option-assoc (aref newc class-options
)
1068 ':allow-nil-initform
)))
1070 ;; First, duplicate all the slots of the parent.
1071 (let ((pcv (class-v (car ps
))))
1072 (let ((pa (aref pcv class-public-a
))
1073 (pd (aref pcv class-public-d
))
1074 (pdoc (aref pcv class-public-doc
))
1075 (ptype (aref pcv class-public-type
))
1076 (pcust (aref pcv class-public-custom
))
1077 (plabel (aref pcv class-public-custom-label
))
1078 (pcustg (aref pcv class-public-custom-group
))
1079 (printer (aref pcv class-public-printer
))
1080 (pprot (aref pcv class-protection
))
1081 (pinit (aref pcv class-initarg-tuples
))
1084 (eieio-add-new-slot newc
1085 (car pa
) (car pd
) (car pdoc
) (aref ptype i
)
1086 (car pcust
) (car plabel
) (car pcustg
)
1088 (car pprot
) (car-safe (car pinit
)) nil nil sn
)
1089 ;; Increment each value.
1097 printer
(cdr printer
)
1101 ;; Now duplicate all the class alloc slots.
1102 (let ((pa (aref pcv class-class-allocation-a
))
1103 (pdoc (aref pcv class-class-allocation-doc
))
1104 (ptype (aref pcv class-class-allocation-type
))
1105 (pcust (aref pcv class-class-allocation-custom
))
1106 (plabel (aref pcv class-class-allocation-custom-label
))
1107 (pcustg (aref pcv class-class-allocation-custom-group
))
1108 (printer (aref pcv class-class-allocation-printer
))
1109 (pprot (aref pcv class-class-allocation-protection
))
1110 (pval (aref pcv class-class-allocation-values
))
1113 (eieio-add-new-slot newc
1114 (car pa
) (aref pval i
) (car pdoc
) (aref ptype i
)
1115 (car pcust
) (car plabel
) (car pcustg
)
1117 (car pprot
) nil
':class sn
)
1118 ;; Increment each value.
1124 printer
(cdr printer
)
1128 ;; Loop over each parent class
1132 ;;; CLOS style implementation of object creators.
1134 (defun make-instance (class &rest initargs
)
1135 "Make a new instance of CLASS based on INITARGS.
1136 CLASS is a class symbol. For example:
1138 (make-instance 'foo)
1140 INITARGS is a property list with keywords based on the :initarg
1141 for each slot. For example:
1143 (make-instance 'foo :slot1 value1 :slotN valueN)
1147 If the first element of INITARGS is a string, it is used as the
1150 In EIEIO, the class' constructor requires a name for use when printing.
1151 `make-instance' in CLOS doesn't use names the way Emacs does, so the
1152 class is used as the name slot instead when INITARGS doesn't start with
1154 (if (and (car initargs
) (stringp (car initargs
)))
1155 (apply (class-constructor class
) initargs
)
1156 (apply (class-constructor class
)
1157 (cond ((symbolp class
) (symbol-name class
))
1158 (t (format "%S" class
)))
1162 ;;; CLOS methods and generics
1165 (put 'eieio--defalias
'byte-hunk-handler
1166 #'byte-compile-file-form-defalias
) ;;(get 'defalias 'byte-hunk-handler)
1167 (defun eieio--defalias (name body
)
1168 "Like `defalias', but with less side-effects.
1169 More specifically, it has no side-effects at all when the new function
1170 definition is the same (`eq') as the old one."
1171 (unless (and (fboundp name
)
1172 (eq (symbol-function name
) body
))
1173 (defalias name body
)))
1175 (defmacro defgeneric
(method args
&optional doc-string
)
1176 "Create a generic function METHOD.
1177 DOC-STRING is the base documentation for this class. A generic
1178 function has no body, as its purpose is to decide which method body
1179 is appropriate to use. Uses `defmethod' to create methods, and calls
1180 `defgeneric' for you. With this implementation the ARGS are
1181 currently ignored. You can use `defgeneric' to apply specialized
1182 top level documentation to a method."
1183 `(eieio--defalias ',method
1184 (eieio--defgeneric-init-form ',method
,doc-string
)))
1186 (defun eieio--defgeneric-init-form (method doc-string
)
1187 "Form to use for the initial definition of a generic."
1189 ((or (not (fboundp method
))
1190 (eq 'autoload
(car-safe (symbol-function method
))))
1191 ;; Make sure the method tables are installed.
1192 (eieiomt-install method
)
1193 ;; Construct the actual body of this function.
1194 (eieio-defgeneric-form method doc-string
))
1195 ((generic-p method
) (symbol-function method
)) ;Leave it as-is.
1196 (t (error "You cannot create a generic/method over an existing symbol: %s"
1199 (defun eieio-defgeneric-form (method doc-string
)
1200 "The lambda form that would be used as the function defined on METHOD.
1201 All methods should call the same EIEIO function for dispatch.
1202 DOC-STRING is the documentation attached to METHOD."
1203 `(lambda (&rest local-args
)
1205 (eieio-generic-call (quote ,method
) local-args
)))
1207 (defsubst eieio-defgeneric-reset-generic-form
(method)
1208 "Setup METHOD to call the generic form."
1209 (let ((doc-string (documentation method
)))
1210 (fset method
(eieio-defgeneric-form method doc-string
))))
1212 (defun eieio-defgeneric-form-primary-only (method doc-string
)
1213 "The lambda form that would be used as the function defined on METHOD.
1214 All methods should call the same EIEIO function for dispatch.
1215 DOC-STRING is the documentation attached to METHOD."
1216 `(lambda (&rest local-args
)
1218 (eieio-generic-call-primary-only (quote ,method
) local-args
)))
1220 (defsubst eieio-defgeneric-reset-generic-form-primary-only
(method)
1221 "Setup METHOD to call the generic form."
1222 (let ((doc-string (documentation method
)))
1223 (fset method
(eieio-defgeneric-form-primary-only method doc-string
))))
1225 (defun eieio-defgeneric-form-primary-only-one (method doc-string
1229 "The lambda form that would be used as the function defined on METHOD.
1230 All methods should call the same EIEIO function for dispatch.
1231 DOC-STRING is the documentation attached to METHOD.
1232 CLASS is the class symbol needed for private method access.
1233 IMPL is the symbol holding the method implementation."
1234 ;; NOTE: I tried out byte compiling this little fcn. Turns out it
1235 ;; is faster to execute this for not byte-compiled. ie, install this,
1236 ;; then measure calls going through here. I wonder why.
1238 (let ((byte-compile-warnings nil
))
1240 `(lambda (&rest local-args
)
1242 ;; This is a cool cheat. Usually we need to look up in the
1243 ;; method table to find out if there is a method or not. We can
1244 ;; instead make that determination at load time when there is
1245 ;; only one method. If the first arg is not a child of the class
1246 ;; of that one implementation, then clearly, there is no method def.
1247 (if (not (eieio-object-p (car local-args
)))
1248 ;; Not an object. Just signal.
1249 (signal 'no-method-definition
1250 (list ',method local-args
))
1252 ;; We do have an object. Make sure it is the right type.
1253 (if ,(if (eq class eieio-default-superclass
)
1254 nil
; default superclass means just an obj. Already asked.
1255 `(not (child-of-class-p (aref (car local-args
) object-class
)
1258 ;; If not the right kind of object, call no applicable
1259 (apply 'no-applicable-method
(car local-args
)
1260 ',method local-args
)
1262 ;; It is ok, do the call.
1263 ;; Fill in inter-call variables then evaluate the method.
1264 (let ((scoped-class ',class
)
1265 (eieio-generic-call-next-method-list nil
)
1266 (eieio-generic-call-key method-primary
)
1267 (eieio-generic-call-methodname ',method
)
1268 (eieio-generic-call-arglst local-args
)
1270 ,(if (< emacs-major-version
24)
1271 `(apply ,(list 'quote impl
) local-args
)
1272 `(apply #',impl local-args
))
1276 (defsubst eieio-defgeneric-reset-generic-form-primary-only-one
(method)
1277 "Setup METHOD to call the generic form."
1278 (let* ((doc-string (documentation method
))
1279 (M (get method
'eieio-method-tree
))
1280 (entry (car (aref M method-primary
)))
1282 (fset method
(eieio-defgeneric-form-primary-only-one
1288 (defun eieio-unbind-method-implementations (method)
1289 "Make the generic method METHOD have no implementations.
1290 It will leave the original generic function in place,
1291 but remove reference to all implementations of METHOD."
1292 (put method
'eieio-method-tree nil
)
1293 (put method
'eieio-method-obarray nil
))
1295 (defmacro defmethod
(method &rest args
)
1296 "Create a new METHOD through `defgeneric' with ARGS.
1298 The optional second argument KEY is a specifier that
1299 modifies how the method is called, including:
1300 :before - Method will be called before the :primary
1301 :primary - The default if not specified
1302 :after - Method will be called after the :primary
1303 :static - First arg could be an object or class
1304 The next argument is the ARGLIST. The ARGLIST specifies the arguments
1305 to the method as with `defun'. The first argument can have a type
1307 ((VARNAME CLASS) ARG2 ...)
1308 where VARNAME is the name of the local variable for the method being
1309 created. The CLASS is a class symbol for a class made with `defclass'.
1310 A DOCSTRING comes after the ARGLIST, and is optional.
1311 All the rest of the args are the BODY of the method. A method will
1312 return the value of the last form in the BODY.
1316 (defmethod mymethod [:before | :primary | :after | :static]
1317 ((typearg class-name) arg2 &optional opt &rest rest)
1320 (let* ((key (if (keywordp (car args
)) (pop args
)))
1323 (fargs (if (consp arg1
)
1324 (cons (car arg1
) (cdr params
))
1326 (class (if (consp arg1
) (nth 1 arg1
)))
1327 (code `(lambda ,fargs
,@(cdr args
))))
1329 ;; Make sure there is a generic and the byte-compiler sees it.
1330 (defgeneric ,method
,args
1331 ,(or (documentation code
)
1332 (format "Generically created method `%s'." method
)))
1333 (eieio--defmethod ',method
',key
',class
#',code
))))
1335 (defun eieio--defmethod (method kind argclass code
)
1336 "Work part of the `defmethod' macro defining METHOD with ARGS."
1338 ;; find optional keys
1339 (cond ((or (eq ':BEFORE kind
)
1342 ((or (eq ':AFTER kind
)
1345 ((or (eq ':PRIMARY kind
)
1346 (eq ':primary kind
))
1348 ((or (eq ':STATIC kind
)
1352 (t method-primary
))))
1353 ;; Make sure there is a generic (when called from defclass).
1355 method
(eieio--defgeneric-init-form
1356 method
(or (documentation code
)
1357 (format "Generically created method `%s'." method
))))
1358 ;; create symbol for property to bind to. If the first arg is of
1359 ;; the form (varname vartype) and `vartype' is a class, then
1360 ;; that class will be the type symbol. If not, then it will fall
1361 ;; under the type `primary' which is a non-specific calling of the
1364 (if (not (class-p argclass
))
1365 (error "Unknown class type %s in method parameters"
1368 (signal 'wrong-type-argument
(list :static
'non-class-arg
)))
1369 ;; generics are higher
1370 (setq key
(eieio-specialized-key-to-generic-key key
)))
1371 ;; Put this lambda into the symbol so we can find it
1372 (eieiomt-add method code key argclass
)
1375 (when eieio-optimize-primary-methods-flag
1378 ;; If this method, after this setup, only has primary methods, then
1379 ;; we can setup the generic that way.
1380 (if (generic-primary-only-p method
)
1381 ;; If there is only one primary method, then we can go one more
1382 ;; optimization step.
1383 (if (generic-primary-only-one-p method
)
1384 (eieio-defgeneric-reset-generic-form-primary-only-one method
)
1385 (eieio-defgeneric-reset-generic-form-primary-only method
))
1386 (eieio-defgeneric-reset-generic-form method
)))
1390 ;;; Slot type validation
1392 ;; This is a hideous hack for replacing `typep' from cl-macs, to avoid
1393 ;; requiring the CL library at run-time. It can be eliminated if/when
1394 ;; `typep' is merged into Emacs core.
1395 (defun eieio--typep (val type
)
1397 (cond ((get type
'cl-deftype-handler
)
1398 (eieio--typep val
(funcall (get type
'cl-deftype-handler
))))
1400 ((eq type
'null
) (null val
))
1401 ((eq type
'atom
) (atom val
))
1402 ((eq type
'float
) (and (numberp val
) (not (integerp val
))))
1403 ((eq type
'real
) (numberp val
))
1404 ((eq type
'fixnum
) (integerp val
))
1405 ((memq type
'(character string-char
)) (characterp val
))
1407 (let* ((name (symbol-name type
))
1408 (namep (intern (concat name
"p"))))
1410 (funcall `(lambda () (,namep val
)))
1411 (funcall `(lambda ()
1412 (,(intern (concat name
"-p")) val
)))))))
1413 (cond ((get (car type
) 'cl-deftype-handler
)
1414 (eieio--typep val
(apply (get (car type
) 'cl-deftype-handler
)
1416 ((memq (car type
) '(integer float real number
))
1417 (and (eieio--typep val
(car type
))
1418 (or (memq (cadr type
) '(* nil
))
1419 (if (consp (cadr type
))
1420 (> val
(car (cadr type
)))
1421 (>= val
(cadr type
))))
1422 (or (memq (caddr type
) '(* nil
))
1423 (if (consp (car (cddr type
)))
1424 (< val
(caar (cddr type
)))
1425 (<= val
(car (cddr type
)))))))
1426 ((memq (car type
) '(and or not
))
1427 (eval (cons (car type
)
1429 `(eieio--typep (quote ,val
) (quote ,x
)))
1431 ((memq (car type
) '(member member
*))
1432 (memql val
(cdr type
)))
1433 ((eq (car type
) 'satisfies
)
1434 (funcall `(lambda () (,(cadr type
) val
))))
1435 (t (error "Bad type spec: %s" type
)))))
1437 (defun eieio-perform-slot-validation (spec value
)
1438 "Return non-nil if SPEC does not match VALUE."
1439 (or (eq spec t
) ; t always passes
1440 (eq value eieio-unbound
) ; unbound always passes
1441 (eieio--typep value spec
)))
1443 (defun eieio-validate-slot-value (class slot-idx value slot
)
1444 "Make sure that for CLASS referencing SLOT-IDX, VALUE is valid.
1445 Checks the :type specifier.
1446 SLOT is the slot that is being checked, and is only used when throwing
1448 (if eieio-skip-typecheck
1450 ;; Trim off object IDX junk added in for the object index.
1451 (setq slot-idx
(- slot-idx
3))
1452 (let ((st (aref (aref (class-v class
) class-public-type
) slot-idx
)))
1453 (if (not (eieio-perform-slot-validation st value
))
1454 (signal 'invalid-slot-type
(list class slot st value
))))))
1456 (defun eieio-validate-class-slot-value (class slot-idx value slot
)
1457 "Make sure that for CLASS referencing SLOT-IDX, VALUE is valid.
1458 Checks the :type specifier.
1459 SLOT is the slot that is being checked, and is only used when throwing
1461 (if eieio-skip-typecheck
1463 (let ((st (aref (aref (class-v class
) class-class-allocation-type
)
1465 (if (not (eieio-perform-slot-validation st value
))
1466 (signal 'invalid-slot-type
(list class slot st value
))))))
1468 (defun eieio-barf-if-slot-unbound (value instance slotname fn
)
1469 "Throw a signal if VALUE is a representation of an UNBOUND slot.
1470 INSTANCE is the object being referenced. SLOTNAME is the offending
1471 slot. If the slot is ok, return VALUE.
1472 Argument FN is the function calling this verifier."
1473 (if (and (eq value eieio-unbound
) (not eieio-skip-typecheck
))
1474 (slot-unbound instance
(object-class instance
) slotname fn
)
1477 ;;; Get/Set slots in an object.
1479 (defmacro oref
(obj slot
)
1480 "Retrieve the value stored in OBJ in the slot named by SLOT.
1481 Slot is the name of the slot when created by `defclass' or the label
1482 created by the :initarg tag."
1483 `(eieio-oref ,obj
(quote ,slot
)))
1485 (defun eieio-oref (obj slot
)
1486 "Return the value in OBJ at SLOT in the object vector."
1487 (if (not (or (eieio-object-p obj
) (class-p obj
)))
1488 (signal 'wrong-type-argument
(list '(or eieio-object-p class-p
) obj
)))
1489 (if (not (symbolp slot
))
1490 (signal 'wrong-type-argument
(list 'symbolp slot
)))
1491 (if (class-p obj
) (eieio-class-un-autoload obj
))
1492 (let* ((class (if (class-p obj
) obj
(aref obj object-class
)))
1493 (c (eieio-slot-name-index class obj slot
)))
1495 ;; It might be missing because it is a :class allocated slot.
1496 ;; Let's check that info out.
1497 (if (setq c
(eieio-class-slot-name-index class slot
))
1499 (aref (aref (class-v class
) class-class-allocation-values
) c
)
1500 ;; The slot-missing method is a cool way of allowing an object author
1501 ;; to intercept missing slot definitions. Since it is also the LAST
1502 ;; thing called in this fn, its return value would be retrieved.
1503 (slot-missing obj slot
'oref
)
1504 ;;(signal 'invalid-slot-name (list (object-name obj) slot))
1506 (if (not (eieio-object-p obj
))
1507 (signal 'wrong-type-argument
(list 'eieio-object-p obj
)))
1508 (eieio-barf-if-slot-unbound (aref obj c
) obj slot
'oref
))))
1510 (defalias 'slot-value
'eieio-oref
)
1511 (defalias 'set-slot-value
'eieio-oset
)
1513 (defmacro oref-default
(obj slot
)
1514 "Get the default value of OBJ (maybe a class) for SLOT.
1515 The default value is the value installed in a class with the :initform
1516 tag. SLOT can be the slot name, or the tag specified by the :initarg
1517 tag in the `defclass' call."
1518 `(eieio-oref-default ,obj
(quote ,slot
)))
1520 (defun eieio-oref-default (obj slot
)
1521 "Do the work for the macro `oref-default' with similar parameters.
1522 Fills in OBJ's SLOT with its default value."
1523 (if (not (or (eieio-object-p obj
) (class-p obj
))) (signal 'wrong-type-argument
(list 'eieio-object-p obj
)))
1524 (if (not (symbolp slot
)) (signal 'wrong-type-argument
(list 'symbolp slot
)))
1525 (let* ((cl (if (eieio-object-p obj
) (aref obj object-class
) obj
))
1526 (c (eieio-slot-name-index cl obj slot
)))
1528 ;; It might be missing because it is a :class allocated slot.
1529 ;; Let's check that info out.
1531 (eieio-class-slot-name-index cl slot
))
1533 (aref (aref (class-v cl
) class-class-allocation-values
)
1535 (slot-missing obj slot
'oref-default
)
1536 ;;(signal 'invalid-slot-name (list (class-name cl) slot))
1538 (eieio-barf-if-slot-unbound
1539 (let ((val (nth (- c
3) (aref (class-v cl
) class-public-d
))))
1540 (eieio-default-eval-maybe val
))
1541 obj cl
'oref-default
))))
1543 (defsubst eieio-eval-default-p
(val)
1544 "Whether the default value VAL should be evaluated for use."
1545 (and (consp val
) (symbolp (car val
)) (fboundp (car val
))))
1547 (defun eieio-default-eval-maybe (val)
1548 "Check VAL, and return what `oref-default' would provide."
1550 ;; Is it a function call? If so, evaluate it.
1551 ((eieio-eval-default-p val
)
1553 ;;;; check for quoted things, and unquote them
1554 ;;((and (consp val) (eq (car val) 'quote))
1556 ;; return it verbatim
1559 ;;; Handy CLOS macros
1561 (defmacro with-slots
(spec-list object
&rest body
)
1562 "Bind SPEC-LIST lexically to slot values in OBJECT, and execute BODY.
1563 This establishes a lexical environment for referring to the slots in
1564 the instance named by the given slot-names as though they were
1565 variables. Within such a context the value of the slot can be
1566 specified by using its slot name, as if it were a lexically bound
1567 variable. Both setf and setq can be used to set the value of the
1570 SPEC-LIST is of a form similar to `let'. For example:
1577 Where each VAR is the local variable given to the associated
1578 SLOT. A slot specified without a variable name is given a
1579 variable name of the same name as the slot."
1580 (declare (indent 2))
1581 ;; Transform the spec-list into a symbol-macrolet spec-list.
1582 (let ((mappings (mapcar (lambda (entry)
1583 (let ((var (if (listp entry
) (car entry
) entry
))
1584 (slot (if (listp entry
) (cadr entry
) entry
)))
1585 (list var
`(slot-value ,object
',slot
))))
1587 (append (list 'symbol-macrolet mappings
)
1590 ;;; Simple generators, and query functions. None of these would do
1591 ;; well embedded into an object.
1593 (defmacro object-class-fast
(obj) "Return the class struct defining OBJ with no check."
1594 `(aref ,obj object-class
))
1596 (defun class-name (class) "Return a Lisp like symbol name for CLASS."
1597 (if (not (class-p class
)) (signal 'wrong-type-argument
(list 'class-p class
)))
1598 ;; I think this is supposed to return a symbol, but to me CLASS is a symbol,
1599 ;; and I wanted a string. Arg!
1600 (format "#<class %s>" (symbol-name class
)))
1602 (defun object-name (obj &optional extra
)
1603 "Return a Lisp like symbol string for object OBJ.
1604 If EXTRA, include that in the string returned to represent the symbol."
1605 (if (not (eieio-object-p obj
)) (signal 'wrong-type-argument
(list 'eieio-object-p obj
)))
1606 (format "#<%s %s%s>" (symbol-name (object-class-fast obj
))
1607 (aref obj object-name
) (or extra
"")))
1609 (defun object-name-string (obj) "Return a string which is OBJ's name."
1610 (if (not (eieio-object-p obj
)) (signal 'wrong-type-argument
(list 'eieio-object-p obj
)))
1611 (aref obj object-name
))
1613 (defun object-set-name-string (obj name
) "Set the string which is OBJ's NAME."
1614 (if (not (eieio-object-p obj
)) (signal 'wrong-type-argument
(list 'eieio-object-p obj
)))
1615 (if (not (stringp name
)) (signal 'wrong-type-argument
(list 'stringp name
)))
1616 (aset obj object-name name
))
1618 (defun object-class (obj) "Return the class struct defining OBJ."
1619 (if (not (eieio-object-p obj
)) (signal 'wrong-type-argument
(list 'eieio-object-p obj
)))
1620 (object-class-fast obj
))
1621 (defalias 'class-of
'object-class
)
1623 (defun object-class-name (obj) "Return a Lisp like symbol name for OBJ's class."
1624 (if (not (eieio-object-p obj
)) (signal 'wrong-type-argument
(list 'eieio-object-p obj
)))
1625 (class-name (object-class-fast obj
)))
1627 (defmacro class-parents-fast
(class) "Return parent classes to CLASS with no check."
1628 `(aref (class-v ,class
) class-parent
))
1630 (defun class-parents (class)
1631 "Return parent classes to CLASS. (overload of variable).
1633 The CLOS function `class-direct-superclasses' is aliased to this function."
1634 (if (not (class-p class
)) (signal 'wrong-type-argument
(list 'class-p class
)))
1635 (class-parents-fast class
))
1637 (defmacro class-children-fast
(class) "Return child classes to CLASS with no check."
1638 `(aref (class-v ,class
) class-children
))
1640 (defun class-children (class)
1641 "Return child classes to CLASS.
1643 The CLOS function `class-direct-subclasses' is aliased to this function."
1644 (if (not (class-p class
)) (signal 'wrong-type-argument
(list 'class-p class
)))
1645 (class-children-fast class
))
1647 (defun eieio-c3-candidate (class remaining-inputs
)
1648 "Returns CLASS if it can go in the result now, otherwise nil"
1649 ;; Ensure CLASS is not in any position but the first in any of the
1650 ;; element lists of REMAINING-INPUTS.
1651 (and (not (let ((found nil
))
1652 (while (and remaining-inputs
(not found
))
1653 (setq found
(member class
(cdr (car remaining-inputs
)))
1654 remaining-inputs
(cdr remaining-inputs
)))
1658 (defun eieio-c3-merge-lists (reversed-partial-result remaining-inputs
)
1659 "Merge REVERSED-PARTIAL-RESULT REMAINING-INPUTS in a consistent order, if possible.
1660 If a consistent order does not exist, signal an error."
1661 (if (let ((tail remaining-inputs
)
1663 (while (and tail
(not found
))
1664 (setq found
(car tail
) tail
(cdr tail
)))
1666 ;; If all remaining inputs are empty lists, we are done.
1667 (nreverse reversed-partial-result
)
1668 ;; Otherwise, we try to find the next element of the result. This
1669 ;; is achieved by considering the first element of each
1670 ;; (non-empty) input list and accepting a candidate if it is
1671 ;; consistent with the rests of the input lists.
1673 (tail remaining-inputs
)
1675 (while (and tail
(not found
))
1676 (setq found
(and (car tail
)
1677 (eieio-c3-candidate (caar tail
)
1682 ;; The graph is consistent so far, add NEXT to result and
1683 ;; merge input lists, dropping NEXT from their heads where
1685 (eieio-c3-merge-lists
1686 (cons next reversed-partial-result
)
1687 (mapcar (lambda (l) (if (eq (first l
) next
) (rest l
) l
))
1689 ;; The graph is inconsistent, give up
1690 (signal 'inconsistent-class-hierarchy
(list remaining-inputs
))))))
1692 (defun eieio-class-precedence-dfs (class)
1693 "Return all parents of CLASS in depth-first order."
1694 (let* ((parents (class-parents-fast class
))
1695 (classes (copy-sequence
1702 (eieio-class-precedence-dfs parent
)))
1704 '((eieio-default-superclass))))))
1706 ;; Remove duplicates.
1708 (setcdr tail
(delq (car tail
) (cdr tail
)))
1709 (setq tail
(cdr tail
)))
1712 (defun eieio-class-precedence-bfs (class)
1713 "Return all parents of CLASS in breadth-first order."
1715 (queue (or (class-parents-fast class
)
1716 '(eieio-default-superclass))))
1718 (let ((head (pop queue
)))
1719 (unless (member head result
)
1721 (unless (eq head
'eieio-default-superclass
)
1722 (setq queue
(append queue
(or (class-parents-fast head
)
1723 '(eieio-default-superclass))))))))
1724 (cons class
(nreverse result
)))
1727 (defun eieio-class-precedence-c3 (class)
1728 "Return all parents of CLASS in c3 order."
1729 (let ((parents (class-parents-fast class
)))
1730 (eieio-c3-merge-lists
1736 (eieio-class-precedence-c3 x
))
1738 '((eieio-default-superclass)))
1742 (defun class-precedence-list (class)
1743 "Return (transitively closed) list of parents of CLASS.
1744 The order, in which the parents are returned depends on the
1745 method invocation orders of the involved classes."
1746 (if (or (null class
) (eq class
'eieio-default-superclass
))
1748 (case (class-method-invocation-order class
)
1750 (eieio-class-precedence-dfs class
))
1752 (eieio-class-precedence-bfs class
))
1754 (eieio-class-precedence-c3 class
))))
1757 ;; Official CLOS functions.
1758 (defalias 'class-direct-superclasses
'class-parents
)
1759 (defalias 'class-direct-subclasses
'class-children
)
1761 (defmacro class-parent-fast
(class) "Return first parent class to CLASS with no check."
1762 `(car (class-parents-fast ,class
)))
1764 (defmacro class-parent
(class) "Return first parent class to CLASS. (overload of variable)."
1765 `(car (class-parents ,class
)))
1767 (defmacro same-class-fast-p
(obj class
) "Return t if OBJ is of class-type CLASS with no error checking."
1768 `(eq (aref ,obj object-class
) ,class
))
1770 (defun same-class-p (obj class
) "Return t if OBJ is of class-type CLASS."
1771 (if (not (class-p class
)) (signal 'wrong-type-argument
(list 'class-p class
)))
1772 (if (not (eieio-object-p obj
)) (signal 'wrong-type-argument
(list 'eieio-object-p obj
)))
1773 (same-class-fast-p obj class
))
1775 (defun object-of-class-p (obj class
)
1776 "Return non-nil if OBJ is an instance of CLASS or CLASS' subclasses."
1777 (if (not (eieio-object-p obj
)) (signal 'wrong-type-argument
(list 'eieio-object-p obj
)))
1778 ;; class will be checked one layer down
1779 (child-of-class-p (aref obj object-class
) class
))
1780 ;; Backwards compatibility
1781 (defalias 'obj-of-class-p
'object-of-class-p
)
1783 (defun child-of-class-p (child class
)
1784 "Return non-nil if CHILD class is a subclass of CLASS."
1785 (if (not (class-p class
)) (signal 'wrong-type-argument
(list 'class-p class
)))
1786 (if (not (class-p child
)) (signal 'wrong-type-argument
(list 'class-p child
)))
1788 (while (and child
(not (eq child class
)))
1789 (setq p
(append p
(aref (class-v child
) class-parent
))
1794 (defun object-slots (obj)
1795 "Return list of slots available in OBJ."
1796 (if (not (eieio-object-p obj
)) (signal 'wrong-type-argument
(list 'eieio-object-p obj
)))
1797 (aref (class-v (object-class-fast obj
)) class-public-a
))
1799 (defun class-slot-initarg (class slot
) "Fetch from CLASS, SLOT's :initarg."
1800 (if (not (class-p class
)) (signal 'wrong-type-argument
(list 'class-p class
)))
1801 (let ((ia (aref (class-v class
) class-initarg-tuples
))
1803 (while (and ia
(not f
))
1804 (if (eq (cdr (car ia
)) slot
)
1805 (setq f
(car (car ia
))))
1809 ;;; Object Set macros
1811 (defmacro oset
(obj slot value
)
1812 "Set the value in OBJ for slot SLOT to VALUE.
1813 SLOT is the slot name as specified in `defclass' or the tag created
1814 with in the :initarg slot. VALUE can be any Lisp object."
1815 `(eieio-oset ,obj
(quote ,slot
) ,value
))
1817 (defun eieio-oset (obj slot value
)
1818 "Do the work for the macro `oset'.
1819 Fills in OBJ's SLOT with VALUE."
1820 (if (not (eieio-object-p obj
)) (signal 'wrong-type-argument
(list 'eieio-object-p obj
)))
1821 (if (not (symbolp slot
)) (signal 'wrong-type-argument
(list 'symbolp slot
)))
1822 (let ((c (eieio-slot-name-index (object-class-fast obj
) obj slot
)))
1824 ;; It might be missing because it is a :class allocated slot.
1825 ;; Let's check that info out.
1827 (eieio-class-slot-name-index (aref obj object-class
) slot
))
1830 (eieio-validate-class-slot-value (object-class-fast obj
) c value slot
)
1831 (aset (aref (class-v (aref obj object-class
))
1832 class-class-allocation-values
)
1834 ;; See oref for comment on `slot-missing'
1835 (slot-missing obj slot
'oset value
)
1836 ;;(signal 'invalid-slot-name (list (object-name obj) slot))
1838 (eieio-validate-slot-value (object-class-fast obj
) c value slot
)
1839 (aset obj c value
))))
1841 (defmacro oset-default
(class slot value
)
1842 "Set the default slot in CLASS for SLOT to VALUE.
1843 The default value is usually set with the :initform tag during class
1844 creation. This allows users to change the default behavior of classes
1845 after they are created."
1846 `(eieio-oset-default ,class
(quote ,slot
) ,value
))
1848 (defun eieio-oset-default (class slot value
)
1849 "Do the work for the macro `oset-default'.
1850 Fills in the default value in CLASS' in SLOT with VALUE."
1851 (if (not (class-p class
)) (signal 'wrong-type-argument
(list 'class-p class
)))
1852 (if (not (symbolp slot
)) (signal 'wrong-type-argument
(list 'symbolp slot
)))
1853 (let* ((scoped-class class
)
1854 (c (eieio-slot-name-index class nil slot
)))
1856 ;; It might be missing because it is a :class allocated slot.
1857 ;; Let's check that info out.
1858 (if (setq c
(eieio-class-slot-name-index class slot
))
1861 (eieio-validate-class-slot-value class c value slot
)
1862 (aset (aref (class-v class
) class-class-allocation-values
) c
1864 (signal 'invalid-slot-name
(list (class-name class
) slot
)))
1865 (eieio-validate-slot-value class c value slot
)
1866 ;; Set this into the storage for defaults.
1867 (setcar (nthcdr (- c
3) (aref (class-v class
) class-public-d
))
1869 ;; Take the value, and put it into our cache object.
1870 (eieio-oset (aref (class-v class
) class-default-object-cache
)
1874 ;;; CLOS queries into classes and slots
1876 (defun slot-boundp (object slot
)
1877 "Return non-nil if OBJECT's SLOT is bound.
1878 Setting a slot's value makes it bound. Calling `slot-makeunbound' will
1879 make a slot unbound.
1880 OBJECT can be an instance or a class."
1881 ;; Skip typechecking while retrieving this value.
1882 (let ((eieio-skip-typecheck t
))
1883 ;; Return nil if the magic symbol is in there.
1885 ((eieio-object-p object
) (eieio-oref object slot
))
1886 ((class-p object
) (eieio-oref-default object slot
))
1887 (t (signal 'wrong-type-argument
(list 'eieio-object-p object
))))
1890 (defun slot-makeunbound (object slot
)
1891 "In OBJECT, make SLOT unbound."
1892 (eieio-oset object slot eieio-unbound
))
1894 (defun slot-exists-p (object-or-class slot
)
1895 "Return non-nil if OBJECT-OR-CLASS has SLOT."
1896 (let ((cv (class-v (cond ((eieio-object-p object-or-class
)
1897 (object-class object-or-class
))
1898 ((class-p object-or-class
)
1901 (or (memq slot
(aref cv class-public-a
))
1902 (memq slot
(aref cv class-class-allocation-a
)))
1905 (defun find-class (symbol &optional errorp
)
1906 "Return the class that SYMBOL represents.
1907 If there is no class, nil is returned if ERRORP is nil.
1908 If ERRORP is non-nil, `wrong-argument-type' is signaled."
1909 (if (not (class-p symbol
))
1910 (if errorp
(signal 'wrong-type-argument
(list 'class-p symbol
))
1914 ;;; Slightly more complex utility functions for objects
1916 (defun object-assoc (key slot list
)
1917 "Return an object if KEY is `equal' to SLOT's value of an object in LIST.
1918 LIST is a list of objects whose slots are searched.
1919 Objects in LIST do not need to have a slot named SLOT, nor does
1920 SLOT need to be bound. If these errors occur, those objects will
1922 (if (not (listp list
)) (signal 'wrong-type-argument
(list 'listp list
)))
1923 (while (and list
(not (condition-case nil
1924 ;; This prevents errors for missing slots.
1925 (equal key
(eieio-oref (car list
) slot
))
1927 (setq list
(cdr list
)))
1930 (defun object-assoc-list (slot list
)
1931 "Return an association list with the contents of SLOT as the key element.
1932 LIST must be a list of objects with SLOT in it.
1933 This is useful when you need to do completing read on an object group."
1934 (if (not (listp list
)) (signal 'wrong-type-argument
(list 'listp list
)))
1935 (let ((assoclist nil
))
1937 (setq assoclist
(cons (cons (eieio-oref (car list
) slot
)
1940 (setq list
(cdr list
)))
1941 (nreverse assoclist
)))
1943 (defun object-assoc-list-safe (slot list
)
1944 "Return an association list with the contents of SLOT as the key element.
1945 LIST must be a list of objects, but those objects do not need to have
1946 SLOT in it. If it does not, then that element is left out of the association
1948 (if (not (listp list
)) (signal 'wrong-type-argument
(list 'listp list
)))
1949 (let ((assoclist nil
))
1951 (if (slot-exists-p (car list
) slot
)
1952 (setq assoclist
(cons (cons (eieio-oref (car list
) slot
)
1955 (setq list
(cdr list
)))
1956 (nreverse assoclist
)))
1958 (defun object-add-to-list (object slot item
&optional append
)
1959 "In OBJECT's SLOT, add ITEM to the list of elements.
1960 Optional argument APPEND indicates we need to append to the list.
1961 If ITEM already exists in the list in SLOT, then it is not added.
1962 Comparison is done with `equal' through the `member' function call.
1963 If SLOT is unbound, bind it to the list containing ITEM."
1965 ;; Find the originating list.
1966 (if (not (slot-boundp object slot
))
1967 (setq ov
(list item
))
1968 (setq ov
(eieio-oref object slot
))
1969 ;; turn it into a list.
1971 (setq ov
(list ov
)))
1972 ;; Do the combination
1973 (if (not (member item ov
))
1976 (append ov
(list item
))
1978 ;; Set back into the slot.
1979 (eieio-oset object slot ov
)))
1981 (defun object-remove-from-list (object slot item
)
1982 "In OBJECT's SLOT, remove occurrences of ITEM.
1983 Deletion is done with `delete', which deletes by side effect,
1984 and comparisons are done with `equal'.
1985 If SLOT is unbound, do nothing."
1986 (if (not (slot-boundp object slot
))
1988 (eieio-oset object slot
(delete item
(eieio-oref object slot
)))))
1990 ;;; EIEIO internal search functions
1992 (defun eieio-slot-originating-class-p (start-class slot
)
1993 "Return non-nil if START-CLASS is the first class to define SLOT.
1994 This is for testing if `scoped-class' is the class that defines SLOT
1995 so that we can protect private slots."
1996 (let ((par (class-parents start-class
))
2000 (while (and par ret
)
2001 (if (intern-soft (symbol-name slot
)
2002 (aref (class-v (car par
))
2003 class-symbol-obarray
))
2005 (setq par
(cdr par
)))
2008 (defun eieio-slot-name-index (class obj slot
)
2009 "In CLASS for OBJ find the index of the named SLOT.
2010 The slot is a symbol which is installed in CLASS by the `defclass'
2011 call. OBJ can be nil, but if it is an object, and the slot in question
2012 is protected, access will be allowed if OBJ is a child of the currently
2014 If SLOT is the value created with :initarg instead,
2015 reverse-lookup that name, and recurse with the associated slot value."
2016 ;; Removed checks to outside this call
2017 (let* ((fsym (intern-soft (symbol-name slot
)
2018 (aref (class-v class
)
2019 class-symbol-obarray
)))
2020 (fsi (if (symbolp fsym
) (symbol-value fsym
) nil
)))
2023 ((not (get fsym
'protection
))
2025 ((and (eq (get fsym
'protection
) 'protected
)
2026 (bound-and-true-p scoped-class
)
2027 (or (child-of-class-p class scoped-class
)
2028 (and (eieio-object-p obj
)
2029 (child-of-class-p class
(object-class obj
)))))
2031 ((and (eq (get fsym
'protection
) 'private
)
2032 (or (and (bound-and-true-p scoped-class
)
2033 (eieio-slot-originating-class-p scoped-class slot
))
2034 eieio-initializing-object
))
2037 (let ((fn (eieio-initarg-to-attribute class slot
)))
2038 (if fn
(eieio-slot-name-index class obj fn
) nil
)))))
2040 (defun eieio-class-slot-name-index (class slot
)
2041 "In CLASS find the index of the named SLOT.
2042 The slot is a symbol which is installed in CLASS by the `defclass'
2043 call. If SLOT is the value created with :initarg instead,
2044 reverse-lookup that name, and recurse with the associated slot value."
2045 ;; This will happen less often, and with fewer slots. Do this the
2046 ;; storage cheap way.
2047 (let* ((a (aref (class-v class
) class-class-allocation-a
))
2051 ;; Slot # is length of the total list, minus the remaining list of
2055 ;;; CLOS generics internal function handling
2057 (defvar eieio-generic-call-methodname nil
2058 "When using `call-next-method', provides a context on how to do it.")
2059 (defvar eieio-generic-call-arglst nil
2060 "When using `call-next-method', provides a context for parameters.")
2061 (defvar eieio-generic-call-key nil
2062 "When using `call-next-method', provides a context for the current key.
2063 Keys are a number representing :before, :primary, and :after methods.")
2064 (defvar eieio-generic-call-next-method-list nil
2065 "When executing a PRIMARY or STATIC method, track the 'next-method'.
2066 During executions, the list is first generated, then as each next method
2067 is called, the next method is popped off the stack.")
2069 (define-obsolete-variable-alias 'eieio-pre-method-execution-hooks
2070 'eieio-pre-method-execution-functions
"24.3")
2071 (defvar eieio-pre-method-execution-functions nil
2072 "Abnormal hook run just before an EIEIO method is executed.
2073 The hook function must accept one argument, the list of forms
2074 about to be executed.")
2076 (defun eieio-generic-call (method args
)
2077 "Call METHOD with ARGS.
2078 ARGS provides the context on which implementation to use.
2079 This should only be called from a generic function."
2080 ;; We must expand our arguments first as they are always
2081 ;; passed in as quoted symbols
2082 (let ((newargs nil
) (mclass nil
) (lambdas nil
) (tlambdas nil
) (keys nil
)
2083 (eieio-generic-call-methodname method
)
2084 (eieio-generic-call-arglst args
)
2086 (primarymethodlist nil
))
2089 firstarg
(car newargs
))
2090 ;; Is the class passed in autoloaded?
2091 ;; Since class names are also constructors, they can be autoloaded
2092 ;; via the autoload command. Check for this, and load them in.
2093 ;; It's ok if it doesn't turn out to be a class. Probably want that
2094 ;; function loaded anyway.
2095 (if (and (symbolp firstarg
)
2097 (listp (symbol-function firstarg
))
2098 (eq 'autoload
(car (symbol-function firstarg
))))
2099 (load (nth 1 (symbol-function firstarg
))))
2100 ;; Determine the class to use.
2101 (cond ((eieio-object-p firstarg
)
2102 (setq mclass
(object-class-fast firstarg
)))
2104 (setq mclass firstarg
))
2106 ;; Make sure the class is a valid class
2107 ;; mclass can be nil (meaning a generic for should be used.
2108 ;; mclass cannot have a value that is not a class, however.
2109 (when (and (not (null mclass
)) (not (class-p mclass
)))
2110 (error "Cannot dispatch method %S on class %S"
2113 ;; Now create a list in reverse order of all the calls we have
2114 ;; make in order to successfully do this right. Rules:
2115 ;; 1) Only call generics if scoped-class is not defined
2116 ;; This prevents multiple calls in the case of recursion
2117 ;; 2) Only call static if this is a static method.
2118 ;; 3) Only call specifics if the definition allows for them.
2119 ;; 4) Call in order based on :before, :primary, and :after
2120 (when (eieio-object-p firstarg
)
2121 ;; Non-static calls do all this stuff.
2126 (eieiomt-method-list method method-after mclass
)
2127 (list (eieio-generic-form method method-after nil
)))
2128 ;;(or (and mclass (eieio-generic-form method method-after mclass))
2129 ;; (eieio-generic-form method method-after nil))
2131 (setq lambdas
(append tlambdas lambdas
)
2132 keys
(append (make-list (length tlambdas
) method-after
) keys
))
2136 (or (and mclass
(eieio-generic-form method method-primary mclass
))
2137 (eieio-generic-form method method-primary nil
)))
2139 (setq lambdas
(cons tlambdas lambdas
)
2140 keys
(cons method-primary keys
)
2142 (eieiomt-method-list method method-primary mclass
)))
2147 (eieiomt-method-list method method-before mclass
)
2148 (list (eieio-generic-form method method-before nil
)))
2149 ;;(or (and mclass (eieio-generic-form method method-before mclass))
2150 ;; (eieio-generic-form method method-before nil))
2152 (setq lambdas
(append tlambdas lambdas
)
2153 keys
(append (make-list (length tlambdas
) method-before
) keys
))
2157 ;; For the case of a class,
2158 ;; if there were no methods found, then there could be :static methods.
2161 (eieio-generic-form method method-static mclass
))
2162 (setq lambdas
(cons tlambdas lambdas
)
2163 keys
(cons method-static keys
)
2164 primarymethodlist
;; Re-use even with bad name here
2165 (eieiomt-method-list method method-static mclass
)))
2166 ;; For the case of no class (ie - mclass == nil) then there may
2167 ;; be a primary method.
2169 (eieio-generic-form method method-primary nil
))
2171 (setq lambdas
(cons tlambdas lambdas
)
2172 keys
(cons method-primary keys
)
2174 (eieiomt-method-list method method-primary nil
)))
2177 (run-hook-with-args 'eieio-pre-method-execution-functions
2180 ;; Now loop through all occurrences forms which we must execute
2181 ;; (which are happily sorted now) and execute them all!
2182 (let ((rval nil
) (lastval nil
) (rvalever nil
) (found nil
))
2185 (let* ((scoped-class (cdr (car lambdas
)))
2186 (eieio-generic-call-key (car keys
))
2188 (or (= eieio-generic-call-key method-primary
)
2189 (= eieio-generic-call-key method-static
)))
2190 (eieio-generic-call-next-method-list
2191 ;; Use the cdr, as the first element is the fcn
2192 ;; we are calling right now.
2193 (when has-return-val
(cdr primarymethodlist
)))
2196 ;;(setq rval (apply (car (car lambdas)) newargs))
2197 (setq lastval
(apply (car (car lambdas
)) newargs
))
2198 (when has-return-val
2202 (setq lambdas
(cdr lambdas
)
2205 (if (eieio-object-p (car args
))
2206 (setq rval
(apply 'no-applicable-method
(car args
) method args
)
2209 'no-method-definition
2210 (list method args
))))
2211 ;; Right Here... it could be that lastval is returned when
2212 ;; rvalever is nil. Is that right?
2215 (defun eieio-generic-call-primary-only (method args
)
2216 "Call METHOD with ARGS for methods with only :PRIMARY implementations.
2217 ARGS provides the context on which implementation to use.
2218 This should only be called from a generic function.
2220 This method is like `eieio-generic-call', but only
2221 implementations in the :PRIMARY slot are queried. After many
2222 years of use, it appears that over 90% of methods in use
2223 have :PRIMARY implementations only. We can therefore optimize
2224 for this common case to improve performance."
2225 ;; We must expand our arguments first as they are always
2226 ;; passed in as quoted symbols
2227 (let ((newargs nil
) (mclass nil
) (lambdas nil
)
2228 (eieio-generic-call-methodname method
)
2229 (eieio-generic-call-arglst args
)
2231 (primarymethodlist nil
)
2235 firstarg
(car newargs
))
2237 ;; Determine the class to use.
2238 (cond ((eieio-object-p firstarg
)
2239 (setq mclass
(object-class-fast firstarg
)))
2241 (error "Method %s called on nil" method
))
2242 ((not (eieio-object-p firstarg
))
2243 (error "Primary-only method %s called on something not an object" method
))
2245 (error "EIEIO Error: Improperly classified method %s as primary only"
2248 ;; Make sure the class is a valid class
2249 ;; mclass can be nil (meaning a generic for should be used.
2250 ;; mclass cannot have a value that is not a class, however.
2252 (error "Cannot dispatch method %S on class %S" method mclass
)
2256 (setq lambdas
(eieio-generic-form method method-primary mclass
))
2257 (setq primarymethodlist
;; Re-use even with bad name here
2258 (eieiomt-method-list method method-primary mclass
))
2260 ;; Now loop through all occurrences forms which we must execute
2261 ;; (which are happily sorted now) and execute them all!
2262 (let* ((rval nil
) (lastval nil
) (rvalever nil
)
2263 (scoped-class (cdr lambdas
))
2264 (eieio-generic-call-key method-primary
)
2265 ;; Use the cdr, as the first element is the fcn
2266 ;; we are calling right now.
2267 (eieio-generic-call-next-method-list (cdr primarymethodlist
))
2270 (if (or (not lambdas
) (not (car lambdas
)))
2272 ;; No methods found for this impl...
2273 (if (eieio-object-p (car args
))
2274 (setq rval
(apply 'no-applicable-method
(car args
) method args
)
2277 'no-method-definition
2278 (list method args
)))
2280 ;; Do the regular implementation here.
2282 (run-hook-with-args 'eieio-pre-method-execution-functions
2285 (setq lastval
(apply (car lambdas
) newargs
))
2290 ;; Right Here... it could be that lastval is returned when
2291 ;; rvalever is nil. Is that right?
2294 (defun eieiomt-method-list (method key class
)
2295 "Return an alist list of methods lambdas.
2296 METHOD is the method name.
2297 KEY represents either :before, or :after methods.
2298 CLASS is the starting class to search from in the method tree.
2299 If CLASS is nil, then an empty list of methods should be returned."
2300 ;; Note: eieiomt - the MT means MethodTree. See more comments below
2301 ;; for the rest of the eieiomt methods.
2303 ;; Collect lambda expressions stored for the class and its parent
2306 (dolist (ancestor (class-precedence-list class
))
2307 ;; Lookup the form to use for the PRIMARY object for the next level
2308 (let ((tmpl (eieio-generic-form method key ancestor
)))
2311 ;; This prevents duplicates coming out of the
2312 ;; class method optimizer. Perhaps we should
2313 ;; just not optimize before/afters?
2314 (not (member tmpl lambdas
))))
2315 (push tmpl lambdas
))))
2317 ;; Return collected lambda. For :after methods, return in current
2318 ;; order (most general class last); Otherwise, reverse order.
2319 (if (eq key method-after
)
2321 (nreverse lambdas
))))
2323 (defun next-method-p ()
2324 "Return non-nil if there is a next method.
2325 Returns a list of lambda expressions which is the `next-method'
2327 eieio-generic-call-next-method-list
)
2329 (defun call-next-method (&rest replacement-args
)
2330 "Call the superclass method from a subclass method.
2331 The superclass method is specified in the current method list,
2332 and is called the next method.
2334 If REPLACEMENT-ARGS is non-nil, then use them instead of
2335 `eieio-generic-call-arglst'. The generic arg list are the
2336 arguments passed in at the top level.
2338 Use `next-method-p' to find out if there is a next method to call."
2339 (if (not (bound-and-true-p scoped-class
))
2340 (error "`call-next-method' not called within a class specific method"))
2341 (if (and (/= eieio-generic-call-key method-primary
)
2342 (/= eieio-generic-call-key method-static
))
2343 (error "Cannot `call-next-method' except in :primary or :static methods")
2345 (let ((newargs (or replacement-args eieio-generic-call-arglst
))
2346 (next (car eieio-generic-call-next-method-list
))
2348 (if (or (not next
) (not (car next
)))
2349 (apply 'no-next-method
(car newargs
) (cdr newargs
))
2350 (let* ((eieio-generic-call-next-method-list
2351 (cdr eieio-generic-call-next-method-list
))
2352 (eieio-generic-call-arglst newargs
)
2353 (scoped-class (cdr next
))
2360 ;; eieio-method-tree : eieiomt-
2362 ;; Stored as eieio-method-tree in property list of a generic method
2364 ;; (eieio-method-tree . [BEFORE PRIMARY AFTER
2365 ;; genericBEFORE genericPRIMARY genericAFTER])
2367 ;; (eieio-method-obarray . [BEFORE PRIMARY AFTER
2368 ;; genericBEFORE genericPRIMARY genericAFTER])
2369 ;; where the association is a vector.
2370 ;; (aref 0 -- all static methods.
2371 ;; (aref 1 -- all methods classified as :before
2372 ;; (aref 2 -- all methods classified as :primary
2373 ;; (aref 3 -- all methods classified as :after
2374 ;; (aref 4 -- a generic classified as :before
2375 ;; (aref 5 -- a generic classified as :primary
2376 ;; (aref 6 -- a generic classified as :after
2378 (defvar eieiomt-optimizing-obarray nil
2379 "While mapping atoms, this contain the obarray being optimized.")
2381 (defun eieiomt-install (method-name)
2382 "Install the method tree, and obarray onto METHOD-NAME.
2383 Do not do the work if they already exist."
2384 (let ((emtv (get method-name
'eieio-method-tree
))
2385 (emto (get method-name
'eieio-method-obarray
)))
2386 (if (or (not emtv
) (not emto
))
2388 (setq emtv
(put method-name
'eieio-method-tree
2389 (make-vector method-num-slots nil
))
2390 emto
(put method-name
'eieio-method-obarray
2391 (make-vector method-num-slots nil
)))
2392 (aset emto
0 (make-vector 11 0))
2393 (aset emto
1 (make-vector 11 0))
2394 (aset emto
2 (make-vector 41 0))
2395 (aset emto
3 (make-vector 11 0))
2398 (defun eieiomt-add (method-name method key class
)
2399 "Add to METHOD-NAME the forms METHOD in a call position KEY for CLASS.
2400 METHOD-NAME is the name created by a call to `defgeneric'.
2401 METHOD are the forms for a given implementation.
2402 KEY is an integer (see comment in eieio.el near this function) which
2403 is associated with the :static :before :primary and :after tags.
2404 It also indicates if CLASS is defined or not.
2405 CLASS is the class this method is associated with."
2406 (if (or (> key method-num-slots
) (< key
0))
2407 (error "eieiomt-add: method key error!"))
2408 (let ((emtv (get method-name
'eieio-method-tree
))
2409 (emto (get method-name
'eieio-method-obarray
)))
2410 ;; Make sure the method tables are available.
2411 (if (or (not emtv
) (not emto
))
2412 (error "Programmer error: eieiomt-add"))
2413 ;; only add new cells on if it doesn't already exist!
2414 (if (assq class
(aref emtv key
))
2415 (setcdr (assq class
(aref emtv key
)) method
)
2416 (aset emtv key
(cons (cons class method
) (aref emtv key
))))
2417 ;; Add function definition into newly created symbol, and store
2418 ;; said symbol in the correct obarray, otherwise use the
2419 ;; other array to keep this stuff
2420 (if (< key method-num-lists
)
2421 (let ((nsym (intern (symbol-name class
) (aref emto key
))))
2422 (fset nsym method
)))
2423 ;; Save the defmethod file location in a symbol property.
2424 (let ((fname (if load-in-progress
2429 (when (string-match "\\.elc$" fname
)
2430 (setq fname
(substring fname
0 (1- (length fname
)))))
2431 (setq loc
(get method-name
'method-locations
))
2434 (put method-name
'method-locations loc
)))
2435 ;; Now optimize the entire obarray
2436 (if (< key method-num-lists
)
2437 (let ((eieiomt-optimizing-obarray (aref emto key
)))
2438 ;; @todo - Is this overkill? Should we just clear the symbol?
2439 (mapatoms 'eieiomt-sym-optimize eieiomt-optimizing-obarray
)))
2442 (defun eieiomt-next (class)
2443 "Return the next parent class for CLASS.
2444 If CLASS is a superclass, return variable `eieio-default-superclass'.
2445 If CLASS is variable `eieio-default-superclass' then return nil.
2446 This is different from function `class-parent' as class parent returns
2447 nil for superclasses. This function performs no type checking!"
2448 ;; No type-checking because all calls are made from functions which
2449 ;; are safe and do checking for us.
2450 (or (class-parents-fast class
)
2451 (if (eq class
'eieio-default-superclass
)
2453 '(eieio-default-superclass))))
2455 (defun eieiomt-sym-optimize (s)
2456 "Find the next class above S which has a function body for the optimizer."
2457 ;; Set the value to nil in case there is no nearest cell.
2459 ;; Find the nearest cell that has a function body. If we find one,
2460 ;; we replace the nil from above.
2461 (let ((external-symbol (intern-soft (symbol-name s
))))
2463 (dolist (ancestor (rest (class-precedence-list external-symbol
)))
2464 (let ((ov (intern-soft (symbol-name ancestor
)
2465 eieiomt-optimizing-obarray
)))
2467 (set s ov
) ;; store ov as our next symbol
2468 (throw 'done ancestor
)))))))
2470 (defun eieio-generic-form (method key class
)
2471 "Return the lambda form belonging to METHOD using KEY based upon CLASS.
2472 If CLASS is not a class then use `generic' instead. If class has
2473 no form, but has a parent class, then trace to that parent class.
2474 The first time a form is requested from a symbol, an optimized path
2475 is memorized for faster future use."
2476 (let ((emto (aref (get method
'eieio-method-obarray
)
2477 (if class key
(eieio-specialized-key-to-generic-key key
)))))
2479 ;; 1) find our symbol
2480 (let ((cs (intern-soft (symbol-name class
) emto
)))
2482 ;; 2) If there isn't one, then make one.
2483 ;; This can be slow since it only occurs once
2485 (setq cs
(intern (symbol-name class
) emto
))
2486 ;; 2.1) Cache its nearest neighbor with a quick optimize
2487 ;; which should only occur once for this call ever
2488 (let ((eieiomt-optimizing-obarray emto
))
2489 (eieiomt-sym-optimize cs
))))
2490 ;; 3) If it's bound return this one.
2492 (cons cs
(aref (class-v class
) class-symbol
))
2493 ;; 4) If it's not bound then this variable knows something
2494 (if (symbol-value cs
)
2496 ;; 4.1) This symbol holds the next class in its value
2497 (setq class
(symbol-value cs
)
2498 cs
(intern-soft (symbol-name class
) emto
))
2499 ;; 4.2) The optimizer should always have chosen a
2502 (cons cs
(aref (class-v (intern (symbol-name class
)))
2504 ;;(error "EIEIO optimizer: erratic data loss!"))
2506 ;; There never will be a funcall...
2508 ;; for a generic call, what is a list, is the function body we want.
2509 (let ((emtl (aref (get method
'eieio-method-tree
)
2510 (if class key
(eieio-specialized-key-to-generic-key key
)))))
2512 ;; The car of EMTL is supposed to be a class, which in this
2513 ;; case is nil, so skip it.
2514 (cons (cdr (car emtl
)) nil
)
2518 ;; Way to assign slots based on a list. Used for constructors, or
2519 ;; even resetting an object at run-time
2521 (defun eieio-set-defaults (obj &optional set-all
)
2522 "Take object OBJ, and reset all slots to their defaults.
2523 If SET-ALL is non-nil, then when a default is nil, that value is
2524 reset. If SET-ALL is nil, the slots are only reset if the default is
2526 (let ((scoped-class (aref obj object-class
))
2527 (eieio-initializing-object t
)
2528 (pub (aref (class-v (aref obj object-class
)) class-public-a
)))
2530 (let ((df (eieio-oref-default obj
(car pub
))))
2532 (eieio-oset obj
(car pub
) df
)))
2533 (setq pub
(cdr pub
)))))
2535 (defun eieio-initarg-to-attribute (class initarg
)
2536 "For CLASS, convert INITARG to the actual attribute name.
2537 If there is no translation, pass it in directly (so we can cheat if
2538 need be... May remove that later...)"
2539 (let ((tuple (assoc initarg
(aref (class-v class
) class-initarg-tuples
))))
2544 (defun eieio-attribute-to-initarg (class attribute
)
2545 "In CLASS, convert the ATTRIBUTE into the corresponding init argument tag.
2546 This is usually a symbol that starts with `:'."
2547 (let ((tuple (rassoc attribute
(aref (class-v class
) class-initarg-tuples
))))
2553 ;;; Here are some special types of errors
2555 (intern "no-method-definition")
2556 (put 'no-method-definition
'error-conditions
'(no-method-definition error
))
2557 (put 'no-method-definition
'error-message
"No method definition")
2559 (intern "no-next-method")
2560 (put 'no-next-method
'error-conditions
'(no-next-method error
))
2561 (put 'no-next-method
'error-message
"No next method")
2563 (intern "invalid-slot-name")
2564 (put 'invalid-slot-name
'error-conditions
'(invalid-slot-name error
))
2565 (put 'invalid-slot-name
'error-message
"Invalid slot name")
2567 (intern "invalid-slot-type")
2568 (put 'invalid-slot-type
'error-conditions
'(invalid-slot-type error nil
))
2569 (put 'invalid-slot-type
'error-message
"Invalid slot type")
2571 (intern "unbound-slot")
2572 (put 'unbound-slot
'error-conditions
'(unbound-slot error nil
))
2573 (put 'unbound-slot
'error-message
"Unbound slot")
2575 (intern "inconsistent-class-hierarchy")
2576 (put 'inconsistent-class-hierarchy
'error-conditions
2577 '(inconsistent-class-hierarchy error nil
))
2578 (put 'inconsistent-class-hierarchy
'error-message
"Inconsistent class hierarchy")
2580 ;;; Here are some CLOS items that need the CL package
2583 (defsetf eieio-oref eieio-oset
)
2585 (if (eval-when-compile (fboundp 'gv-define-expander
))
2586 ;; Not needed for Emacs>=24.3 since gv.el's setf expands macros and
2589 (defsetf slot-value eieio-oset
)
2591 ;; The below setf method was written by Arnd Kohrs <kohrs@acm.org>
2592 (define-setf-method oref
(obj slot
)
2595 (let ((obj-temp (gensym))
2596 (slot-temp (gensym))
2597 (store-temp (gensym)))
2598 (list (list obj-temp slot-temp
)
2599 (list obj
`(quote ,slot
))
2601 (list 'set-slot-value obj-temp slot-temp
2603 (list 'slot-value obj-temp slot-temp
))))))
2607 ;; We want all objects created by EIEIO to have some default set of
2608 ;; behaviors so we can create object utilities, and allow various
2609 ;; types of error checking. To do this, create the default EIEIO
2610 ;; class, and when no parent class is specified, use this as the
2611 ;; default. (But don't store it in the other classes as the default,
2612 ;; allowing for transparent support.)
2615 (defclass eieio-default-superclass nil
2617 "Default parent class for classes with no specified parent class.
2618 Its slots are automatically adopted by classes with no specified parents.
2619 This class is not stored in the `parent' slot of a class vector."
2622 (defalias 'standard-class
'eieio-default-superclass
)
2624 (defgeneric constructor
(class newname
&rest slots
)
2625 "Default constructor for CLASS `eieio-default-superclass'.")
2627 (defmethod constructor :static
2628 ((class eieio-default-superclass
) newname
&rest slots
)
2629 "Default constructor for CLASS `eieio-default-superclass'.
2630 NEWNAME is the name to be given to the constructed object.
2631 SLOTS are the initialization slots used by `shared-initialize'.
2632 This static method is called when an object is constructed.
2633 It allocates the vector used to represent an EIEIO object, and then
2634 calls `shared-initialize' on that object."
2635 (let* ((new-object (copy-sequence (aref (class-v class
)
2636 class-default-object-cache
))))
2637 ;; Update the name for the newly created object.
2638 (aset new-object object-name newname
)
2639 ;; Call the initialize method on the new object with the slots
2640 ;; that were passed down to us.
2641 (initialize-instance new-object slots
)
2642 ;; Return the created object.
2645 (defgeneric shared-initialize
(obj slots
)
2646 "Set slots of OBJ with SLOTS which is a list of name/value pairs.
2647 Called from the constructor routine.")
2649 (defmethod shared-initialize ((obj eieio-default-superclass
) slots
)
2650 "Set slots of OBJ with SLOTS which is a list of name/value pairs.
2651 Called from the constructor routine."
2652 (let ((scoped-class (aref obj object-class
)))
2654 (let ((rn (eieio-initarg-to-attribute (object-class-fast obj
)
2657 (slot-missing obj
(car slots
) 'oset
(car (cdr slots
)))
2658 (eieio-oset obj rn
(car (cdr slots
)))))
2659 (setq slots
(cdr (cdr slots
))))))
2661 (defgeneric initialize-instance
(this &optional slots
)
2662 "Construct the new object THIS based on SLOTS.")
2664 (defmethod initialize-instance ((this eieio-default-superclass
)
2666 "Construct the new object THIS based on SLOTS.
2667 SLOTS is a tagged list where odd numbered elements are tags, and
2668 even numbered elements are the values to store in the tagged slot.
2669 If you overload the `initialize-instance', there you will need to
2670 call `shared-initialize' yourself, or you can call `call-next-method'
2671 to have this constructor called automatically. If these steps are
2672 not taken, then new objects of your class will not have their values
2673 dynamically set from SLOTS."
2674 ;; First, see if any of our defaults are `lambda', and
2675 ;; re-evaluate them and apply the value to our slots.
2676 (let* ((scoped-class (class-v (aref this object-class
)))
2677 (slot (aref scoped-class class-public-a
))
2678 (defaults (aref scoped-class class-public-d
)))
2680 ;; For each slot, see if we need to evaluate it.
2682 ;; Paul Landes said in an email:
2683 ;; > CL evaluates it if it can, and otherwise, leaves it as
2684 ;; > the quoted thing as you already have. This is by the
2685 ;; > Sonya E. Keene book and other things I've look at on the
2687 (let ((dflt (eieio-default-eval-maybe (car defaults
))))
2688 (when (not (eq dflt
(car defaults
)))
2689 (eieio-oset this
(car slot
) dflt
) ))
2691 (setq slot
(cdr slot
)
2692 defaults
(cdr defaults
))))
2693 ;; Shared initialize will parse our slots for us.
2694 (shared-initialize this slots
))
2696 (defgeneric slot-missing
(object slot-name operation
&optional new-value
)
2697 "Method invoked when an attempt to access a slot in OBJECT fails.")
2699 (defmethod slot-missing ((object eieio-default-superclass
) slot-name
2700 operation
&optional new-value
)
2701 "Method invoked when an attempt to access a slot in OBJECT fails.
2702 SLOT-NAME is the name of the failed slot, OPERATION is the type of access
2703 that was requested, and optional NEW-VALUE is the value that was desired
2706 This method is called from `oref', `oset', and other functions which
2707 directly reference slots in EIEIO objects."
2708 (signal 'invalid-slot-name
(list (object-name object
)
2711 (defgeneric slot-unbound
(object class slot-name fn
)
2712 "Slot unbound is invoked during an attempt to reference an unbound slot.")
2714 (defmethod slot-unbound ((object eieio-default-superclass
)
2716 "Slot unbound is invoked during an attempt to reference an unbound slot.
2717 OBJECT is the instance of the object being reference. CLASS is the
2718 class of OBJECT, and SLOT-NAME is the offending slot. This function
2719 throws the signal `unbound-slot'. You can overload this function and
2720 return the value to use in place of the unbound value.
2721 Argument FN is the function signaling this error.
2722 Use `slot-boundp' to determine if a slot is bound or not.
2724 In CLOS, the argument list is (CLASS OBJECT SLOT-NAME), but
2725 EIEIO can only dispatch on the first argument, so the first two are swapped."
2726 (signal 'unbound-slot
(list (class-name class
) (object-name object
)
2729 (defgeneric no-applicable-method
(object method
&rest args
)
2730 "Called if there are no implementations for OBJECT in METHOD.")
2732 (defmethod no-applicable-method ((object eieio-default-superclass
)
2734 "Called if there are no implementations for OBJECT in METHOD.
2735 OBJECT is the object which has no method implementation.
2736 ARGS are the arguments that were passed to METHOD.
2738 Implement this for a class to block this signal. The return
2739 value becomes the return value of the original method call."
2740 (signal 'no-method-definition
(list method
(object-name object
)))
2743 (defgeneric no-next-method
(object &rest args
)
2744 "Called from `call-next-method' when no additional methods are available.")
2746 (defmethod no-next-method ((object eieio-default-superclass
)
2748 "Called from `call-next-method' when no additional methods are available.
2749 OBJECT is othe object being called on `call-next-method'.
2750 ARGS are the arguments it is called by.
2751 This method signals `no-next-method' by default. Override this
2752 method to not throw an error, and its return value becomes the
2753 return value of `call-next-method'."
2754 (signal 'no-next-method
(list (object-name object
) args
))
2757 (defgeneric clone
(obj &rest params
)
2758 "Make a copy of OBJ, and then supply PARAMS.
2759 PARAMS is a parameter list of the same form used by `initialize-instance'.
2761 When overloading `clone', be sure to call `call-next-method'
2762 first and modify the returned object.")
2764 (defmethod clone ((obj eieio-default-superclass
) &rest params
)
2765 "Make a copy of OBJ, and then apply PARAMS."
2766 (let ((nobj (copy-sequence obj
))
2767 (nm (aref obj object-name
))
2768 (passname (and params
(stringp (car params
))))
2770 (if params
(shared-initialize nobj
(if passname
(cdr params
) params
)))
2773 (if (string-match "-\\([0-9]+\\)" nm
)
2774 (setq num
(1+ (string-to-number (match-string 1 nm
)))
2775 nm
(substring nm
0 (match-beginning 0))))
2776 (aset nobj object-name
(concat nm
"-" (int-to-string num
))))
2777 (aset nobj object-name
(car params
)))
2780 (defgeneric destructor
(this &rest params
)
2781 "Destructor for cleaning up any dynamic links to our object.")
2783 (defmethod destructor ((this eieio-default-superclass
) &rest params
)
2784 "Destructor for cleaning up any dynamic links to our object.
2785 Argument THIS is the object being destroyed. PARAMS are additional
2786 ignored parameters."
2787 ;; No cleanup... yet.
2790 (defgeneric object-print
(this &rest strings
)
2791 "Pretty printer for object THIS. Call function `object-name' with STRINGS.
2793 It is sometimes useful to put a summary of the object into the
2794 default #<notation> string when using EIEIO browsing tools.
2795 Implement this method to customize the summary.")
2797 (defmethod object-print ((this eieio-default-superclass
) &rest strings
)
2798 "Pretty printer for object THIS. Call function `object-name' with STRINGS.
2799 The default method for printing object THIS is to use the
2800 function `object-name'.
2802 It is sometimes useful to put a summary of the object into the
2803 default #<notation> string when using EIEIO browsing tools.
2805 Implement this function and specify STRINGS in a call to
2806 `call-next-method' to provide additional summary information.
2807 When passing in extra strings from child classes, always remember
2808 to prepend a space."
2809 (object-name this
(apply 'concat strings
)))
2811 (defvar eieio-print-depth
0
2812 "When printing, keep track of the current indentation depth.")
2814 (defgeneric object-write
(this &optional comment
)
2815 "Write out object THIS to the current stream.
2816 Optional COMMENT will add comments to the beginning of the output.")
2818 (defmethod object-write ((this eieio-default-superclass
) &optional comment
)
2819 "Write object THIS out to the current stream.
2820 This writes out the vector version of this object. Complex and recursive
2821 object are discouraged from being written.
2822 If optional COMMENT is non-nil, include comments when outputting
2825 (princ ";; Object ")
2826 (princ (object-name-string this
))
2830 (let* ((cl (object-class this
))
2832 ;; Now output readable lisp to recreate this object
2833 ;; It should look like this:
2834 ;; (<constructor> <name> <slot> <slot> ... )
2835 ;; Each slot's slot is writen using its :writer.
2836 (princ (make-string (* eieio-print-depth
2) ?
))
2838 (princ (symbol-name (class-constructor (object-class this
))))
2840 (prin1 (object-name-string this
))
2842 ;; Loop over all the public slots
2843 (let ((publa (aref cv class-public-a
))
2844 (publd (aref cv class-public-d
))
2845 (publp (aref cv class-public-printer
))
2846 (eieio-print-depth (1+ eieio-print-depth
)))
2848 (when (slot-boundp this
(car publa
))
2849 (let ((i (class-slot-initarg cl
(car publa
)))
2850 (v (eieio-oref this
(car publa
)))
2852 (unless (or (not i
) (equal v
(car publd
)))
2855 (princ (make-string (* eieio-print-depth
2) ?
))
2856 (princ (symbol-name i
))
2858 ;; Use our public printer
2861 (funcall (car publp
) v
))
2862 ;; Use our generic override prin1 function.
2863 (princ (if (or (eieio-object-p v
)
2864 (eieio-object-p (car-safe v
)))
2866 (eieio-override-prin1 v
)))))
2867 (setq publa
(cdr publa
) publd
(cdr publd
)
2868 publp
(cdr publp
))))
2870 (when (= eieio-print-depth
0)
2873 (defun eieio-override-prin1 (thing)
2874 "Perform a `prin1' on THING taking advantage of object knowledge."
2875 (cond ((eieio-object-p thing
)
2876 (object-write thing
))
2878 (eieio-list-prin1 thing
))
2880 (princ (class-name thing
)))
2881 ((or (keywordp thing
) (booleanp thing
))
2884 (princ (concat "'" (symbol-name thing
))))
2887 (defun eieio-list-prin1 (list)
2888 "Display LIST where list may contain objects."
2889 (if (not (eieio-object-p (car list
)))
2893 (princ (make-string (* eieio-print-depth
2) ?
))
2895 (let ((eieio-print-depth (1+ eieio-print-depth
)))
2898 (if (eieio-object-p (car list
))
2899 (object-write (car list
))
2900 (princ (make-string (* eieio-print-depth
2) ?
))
2901 (eieio-override-prin1 (car list
)))
2902 (setq list
(cdr list
))))
2906 ;;; Unimplemented functions from CLOS
2908 (defun change-class (obj class
)
2909 "Change the class of OBJ to type CLASS.
2910 This may create or delete slots, but does not affect the return value
2912 (error "EIEIO: `change-class' is unimplemented"))
2916 ;;; Obsolete backward compatibility functions.
2917 ;; Needed to run byte-code compiled with the EIEIO of Emacs-23.
2919 (defun eieio-defmethod (method args
)
2920 "Obsolete work part of an old version of the `defmethod' macro."
2921 (let ((key nil
) (body nil
) (firstarg nil
) (argfix nil
) (argclass nil
) loopa
)
2922 ;; find optional keys
2924 (cond ((or (eq ':BEFORE
(car args
))
2925 (eq ':before
(car args
)))
2926 (setq args
(cdr args
))
2928 ((or (eq ':AFTER
(car args
))
2929 (eq ':after
(car args
)))
2930 (setq args
(cdr args
))
2932 ((or (eq ':PRIMARY
(car args
))
2933 (eq ':primary
(car args
)))
2934 (setq args
(cdr args
))
2936 ((or (eq ':STATIC
(car args
))
2937 (eq ':static
(car args
)))
2938 (setq args
(cdr args
))
2941 (t method-primary
)))
2942 ;; get body, and fix contents of args to be the arguments of the fn.
2943 (setq body
(cdr args
)
2946 ;; Create a fixed version of the arguments
2948 (setq argfix
(cons (if (listp (car loopa
)) (car (car loopa
)) (car loopa
))
2950 (setq loopa
(cdr loopa
)))
2951 ;; make sure there is a generic
2954 (if (stringp (car body
))
2955 (car body
) (format "Generically created method `%s'." method
)))
2956 ;; create symbol for property to bind to. If the first arg is of
2957 ;; the form (varname vartype) and `vartype' is a class, then
2958 ;; that class will be the type symbol. If not, then it will fall
2959 ;; under the type `primary' which is a non-specific calling of the
2961 (setq firstarg
(car args
))
2962 (if (listp firstarg
)
2964 (setq argclass
(nth 1 firstarg
))
2965 (if (not (class-p argclass
))
2966 (error "Unknown class type %s in method parameters"
2969 (signal 'wrong-type-argument
(list :static
'non-class-arg
)))
2970 ;; generics are higher
2971 (setq key
(eieio-specialized-key-to-generic-key key
)))
2972 ;; Put this lambda into the symbol so we can find it
2973 (if (byte-code-function-p (car-safe body
))
2974 (eieiomt-add method
(car-safe body
) key argclass
)
2975 (eieiomt-add method
(append (list 'lambda
(reverse argfix
)) body
)
2979 (when eieio-optimize-primary-methods-flag
2982 ;; If this method, after this setup, only has primary methods, then
2983 ;; we can setup the generic that way.
2984 (if (generic-primary-only-p method
)
2985 ;; If there is only one primary method, then we can go one more
2986 ;; optimization step.
2987 (if (generic-primary-only-one-p method
)
2988 (eieio-defgeneric-reset-generic-form-primary-only-one method
)
2989 (eieio-defgeneric-reset-generic-form-primary-only method
))
2990 (eieio-defgeneric-reset-generic-form method
)))
2993 (make-obsolete 'eieio-defmethod
'eieio--defmethod
"24.1")
2995 (defun eieio-defgeneric (method doc-string
)
2996 "Obsolete work part of an old version of the `defgeneric' macro."
2997 (if (and (fboundp method
) (not (generic-p method
))
2998 (or (byte-code-function-p (symbol-function method
))
2999 (not (eq 'autoload
(car (symbol-function method
)))))
3001 (error "You cannot create a generic/method over an existing symbol: %s"
3003 ;; Don't do this over and over.
3004 (unless (fboundp 'method
)
3005 ;; This defun tells emacs where the first definition of this
3006 ;; method is defined.
3007 `(defun ,method nil
)
3008 ;; Make sure the method tables are installed.
3009 (eieiomt-install method
)
3010 ;; Apply the actual body of this function.
3011 (fset method
(eieio-defgeneric-form method doc-string
))
3012 ;; Return the method
3014 (make-obsolete 'eieio-defgeneric nil
"24.1")
3016 ;;; Interfacing with edebug
3018 (defun eieio-edebug-prin1-to-string (object &optional noescape
)
3019 "Display EIEIO OBJECT in fancy format.
3020 Overrides the edebug default.
3021 Optional argument NOESCAPE is passed to `prin1-to-string' when appropriate."
3022 (cond ((class-p object
) (class-name object
))
3023 ((eieio-object-p object
) (object-print object
))
3024 ((and (listp object
) (or (class-p (car object
))
3025 (eieio-object-p (car object
))))
3026 (concat "(" (mapconcat 'eieio-edebug-prin1-to-string object
" ") ")"))
3027 (t (prin1-to-string object noescape
))))
3029 (add-hook 'edebug-setup-hook
3031 (def-edebug-spec defmethod
3032 (&define
; this means we are defining something
3033 [&or name
("setf" :name setf name
)]
3034 ;; ^^ This is the methods symbol
3035 [ &optional symbolp
] ; this is key :before etc
3037 [ &optional stringp
] ; documentation string
3038 def-body
; part to be debugged
3040 ;; The rest of the macros
3041 (def-edebug-spec oref
(form quote
))
3042 (def-edebug-spec oref-default
(form quote
))
3043 (def-edebug-spec oset
(form quote form
))
3044 (def-edebug-spec oset-default
(form quote form
))
3045 (def-edebug-spec class-v form
)
3046 (def-edebug-spec class-p form
)
3047 (def-edebug-spec eieio-object-p form
)
3048 (def-edebug-spec class-constructor form
)
3049 (def-edebug-spec generic-p form
)
3050 (def-edebug-spec with-slots
(list list def-body
))
3051 ;; I suspect this isn't the best way to do this, but when
3052 ;; cust-print was used on my system all my objects
3053 ;; appeared as "#1 =" which was not useful. This allows
3054 ;; edebug to print my objects in the nice way they were
3055 ;; meant to with `object-print' and `class-name'
3056 ;; (defalias 'edebug-prin1-to-string 'eieio-edebug-prin1-to-string)
3060 ;;; Autoloading some external symbols, and hooking into the help system
3064 ;;; Start of automatically extracted autoloads.
3066 ;;;### (autoloads (customize-object) "eieio-custom" "eieio-custom.el"
3067 ;;;;;; "928623502e8bf40454822355388542b5")
3068 ;;; Generated autoloads from eieio-custom.el
3070 (autoload 'customize-object
"eieio-custom" "\
3071 Customize OBJ in a custom buffer.
3072 Optional argument GROUP is the sub-group of slots to display.
3074 \(fn OBJ &optional GROUP)" nil nil
)
3078 ;;;### (autoloads (eieio-help-mode-augmentation-maybee eieio-describe-generic
3079 ;;;;;; eieio-describe-constructor eieio-describe-class eieio-browse)
3080 ;;;;;; "eieio-opt" "eieio-opt.el" "d808328f9c0156ecbd412d77ba8c569e")
3081 ;;; Generated autoloads from eieio-opt.el
3083 (autoload 'eieio-browse
"eieio-opt" "\
3084 Create an object browser window to show all objects.
3085 If optional ROOT-CLASS, then start with that, otherwise start with
3086 variable `eieio-default-superclass'.
3088 \(fn &optional ROOT-CLASS)" t nil
)
3089 (defalias 'describe-class
'eieio-describe-class
)
3091 (autoload 'eieio-describe-class
"eieio-opt" "\
3092 Describe a CLASS defined by a string or symbol.
3093 If CLASS is actually an object, then also display current values of that object.
3094 Optional HEADERFCN should be called to insert a few bits of info first.
3096 \(fn CLASS &optional HEADERFCN)" t nil
)
3098 (autoload 'eieio-describe-constructor
"eieio-opt" "\
3099 Describe the constructor function FCN.
3100 Uses `eieio-describe-class' to describe the class being constructed.
3103 (defalias 'describe-generic
'eieio-describe-generic
)
3105 (autoload 'eieio-describe-generic
"eieio-opt" "\
3106 Describe the generic function GENERIC.
3107 Also extracts information about all methods specific to this generic.
3109 \(fn GENERIC)" t nil
)
3111 (autoload 'eieio-help-mode-augmentation-maybee
"eieio-opt" "\
3112 For buffers thrown into help mode, augment for EIEIO.
3113 Arguments UNUSED are not used.
3115 \(fn &rest UNUSED)" nil nil
)
3119 ;;; End of automatically extracted autoloads.