Cleanup some of EIEIO's namespace.
[emacs.git] / lisp / emacs-lisp / eieio.el
blob37b1ec5fa942686a70c079a517038448027cb247
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-2013 Free Software Foundation, Inc.
6 ;; Author: Eric M. Ludlam <zappo@gnu.org>
7 ;; Version: 1.4
8 ;; Keywords: OO, lisp
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/>.
25 ;;; Commentary:
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
36 ;; Retrieved from:
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-'.
45 ;;; Code:
47 (eval-when-compile (require 'cl)) ;FIXME: Use cl-lib!
49 (defvar eieio-version "1.4"
50 "Current version of EIEIO.")
52 (defun eieio-version ()
53 "Display the current version of EIEIO."
54 (interactive)
55 (message eieio-version))
57 (eval-and-compile
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.
61 ;; Compatibility
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."
71 (aref func 0))
76 ;;;
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))
102 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 (defmacro eieio--define-field-accessors (prefix fields)
111 (declare (indent 1))
112 (let ((index 0)
113 (defs '()))
114 (dolist (field fields)
115 (let ((doc (if (listp field)
116 (prog1 (cadr field) (setq field (car field))))))
117 (push `(defmacro ,(intern (format "eieio--%s-%s" prefix field)) (x)
118 ,@(if doc (list (format (if (string-match "\n" doc)
119 "Return %s" "Return %s of a %s.")
120 doc prefix)))
121 (list 'aref x ,index))
122 defs)
123 (setq index (1+ index))))
124 `(eval-and-compile
125 ,@(nreverse defs)
126 (defconst ,(intern (format "eieio--%s-num-slots" prefix)) ,index))))
128 (eieio--define-field-accessors class
129 (-unused-0 ;;FIXME: not sure, but at least there was no accessor!
130 (symbol "symbol (self-referencing)")
131 parent children
132 (symbol-obarray "obarray permitting fast access to variable position indexes")
133 ;; @todo
134 ;; the word "public" here is leftovers from the very first version.
135 ;; Get rid of it!
136 (public-a "class attribute index")
137 (public-d "class attribute defaults index")
138 (public-doc "class documentation strings for attributes")
139 (public-type "class type for a slot")
140 (public-custom "class custom type for a slot")
141 (public-custom-label "class custom group for a slot")
142 (public-custom-group "class custom group for a slot")
143 (public-printer "printer for a slot")
144 (protection "protection for a slot")
145 (initarg-tuples "initarg tuples list")
146 (class-allocation-a "class allocated attributes")
147 (class-allocation-doc "class allocated documentation")
148 (class-allocation-type "class allocated value type")
149 (class-allocation-custom "class allocated custom descriptor")
150 (class-allocation-custom-label "class allocated custom descriptor")
151 (class-allocation-custom-group "class allocated custom group")
152 (class-allocation-printer "class allocated printer for a slot")
153 (class-allocation-protection "class allocated protection list")
154 (class-allocation-values "class allocated value vector")
155 (default-object-cache "what a newly created object would look like.
156 This will speed up instantiation time as only a `copy-sequence' will
157 be needed, instead of looping over all the values and setting them
158 from the default.")
159 (options "storage location of tagged class options.
160 Stored outright without modifications or stripping.")))
162 (eieio--define-field-accessors object
163 (-unused-0 ;;FIXME: not sure, but at least there was no accessor!
164 (class "class struct defining OBJ")
165 name))
167 (eval-and-compile
168 ;; FIXME: The constants below should have an `eieio-' prefix added!!
170 (defconst method-static 0 "Index into :static tag on a method.")
171 (defconst method-before 1 "Index into :before tag on a method.")
172 (defconst method-primary 2 "Index into :primary tag on a method.")
173 (defconst method-after 3 "Index into :after tag on a method.")
174 (defconst method-num-lists 4 "Number of indexes into methods vector in which groups of functions are kept.")
175 (defconst method-generic-before 4 "Index into generic :before tag on a method.")
176 (defconst method-generic-primary 5 "Index into generic :primary tag on a method.")
177 (defconst method-generic-after 6 "Index into generic :after tag on a method.")
178 (defconst method-num-slots 7 "Number of indexes into a method's vector.")
180 (defsubst eieio-specialized-key-to-generic-key (key)
181 "Convert a specialized KEY into a generic method key."
182 (cond ((eq key method-static) 0) ;; don't convert
183 ((< key method-num-lists) (+ key 3)) ;; The conversion
184 (t key) ;; already generic.. maybe.
188 ;;; Important macros used in eieio.
190 (defmacro class-v (class)
191 "Internal: Return the class vector from the CLASS symbol."
192 ;; No check: If eieio gets this far, it's probably been checked already.
193 `(get ,class 'eieio-class-definition))
195 (defmacro class-p (class)
196 "Return t if CLASS is a valid class vector.
197 CLASS is a symbol."
198 ;; this new method is faster since it doesn't waste time checking lots of
199 ;; things.
200 `(condition-case nil
201 (eq (aref (class-v ,class) 0) 'defclass)
202 (error nil)))
204 (defmacro eieio-object-p (obj)
205 "Return non-nil if OBJ is an EIEIO object."
206 `(condition-case nil
207 (let ((tobj ,obj))
208 (and (eq (aref tobj 0) 'object)
209 (class-p (eieio--object-class tobj))))
210 (error nil)))
211 (defalias 'object-p 'eieio-object-p)
213 (defmacro class-constructor (class)
214 "Return the symbol representing the constructor of CLASS."
215 `(eieio--class-symbol (class-v ,class)))
217 (defmacro generic-p (method)
218 "Return t if symbol METHOD is a generic function.
219 Only methods have the symbol `eieio-method-obarray' as a property
220 \(which contains a list of all bindings to that method type.)"
221 `(and (fboundp ,method) (get ,method 'eieio-method-obarray)))
223 (defun generic-primary-only-p (method)
224 "Return t if symbol METHOD is a generic function with only primary methods.
225 Only methods have the symbol `eieio-method-obarray' as a property (which
226 contains a list of all bindings to that method type.)
227 Methods with only primary implementations are executed in an optimized way."
228 (and (generic-p method)
229 (let ((M (get method 'eieio-method-tree)))
230 (and (< 0 (length (aref M method-primary)))
231 (not (aref M method-static))
232 (not (aref M method-before))
233 (not (aref M method-after))
234 (not (aref M method-generic-before))
235 (not (aref M method-generic-primary))
236 (not (aref M method-generic-after))))
239 (defun generic-primary-only-one-p (method)
240 "Return t if symbol METHOD is a generic function with only primary methods.
241 Only methods have the symbol `eieio-method-obarray' as a property (which
242 contains a list of all bindings to that method type.)
243 Methods with only primary implementations are executed in an optimized way."
244 (and (generic-p method)
245 (let ((M (get method 'eieio-method-tree)))
246 (and (= 1 (length (aref M method-primary)))
247 (not (aref M method-static))
248 (not (aref M method-before))
249 (not (aref M method-after))
250 (not (aref M method-generic-before))
251 (not (aref M method-generic-primary))
252 (not (aref M method-generic-after))))
255 (defmacro class-option-assoc (list option)
256 "Return from LIST the found OPTION, or nil if it doesn't exist."
257 `(car-safe (cdr (memq ,option ,list))))
259 (defmacro class-option (class option)
260 "Return the value stored for CLASS' OPTION.
261 Return nil if that option doesn't exist."
262 `(class-option-assoc (eieio--class-options (class-v ,class)) ',option))
264 (defmacro class-abstract-p (class)
265 "Return non-nil if CLASS is abstract.
266 Abstract classes cannot be instantiated."
267 `(class-option ,class :abstract))
269 (defmacro class-method-invocation-order (class)
270 "Return the invocation order of CLASS.
271 Abstract classes cannot be instantiated."
272 `(or (class-option ,class :method-invocation-order)
273 :breadth-first))
276 ;;; Defining a new class
278 (defmacro defclass (name superclass slots &rest options-and-doc)
279 "Define NAME as a new class derived from SUPERCLASS with SLOTS.
280 OPTIONS-AND-DOC is used as the class' options and base documentation.
281 SUPERCLASS is a list of superclasses to inherit from, with SLOTS
282 being the slots residing in that class definition. NOTE: Currently
283 only one slot may exist in SUPERCLASS as multiple inheritance is not
284 yet supported. Supported tags are:
286 :initform - Initializing form.
287 :initarg - Tag used during initialization.
288 :accessor - Tag used to create a function to access this slot.
289 :allocation - Specify where the value is stored.
290 Defaults to `:instance', but could also be `:class'.
291 :writer - A function symbol which will `write' an object's slot.
292 :reader - A function symbol which will `read' an object.
293 :type - The type of data allowed in this slot (see `typep').
294 :documentation
295 - A string documenting use of this slot.
297 The following are extensions on CLOS:
298 :protection - Specify protection for this slot.
299 Defaults to `:public'. Also use `:protected', or `:private'.
300 :custom - When customizing an object, the custom :type. Public only.
301 :label - A text string label used for a slot when customizing.
302 :group - Name of a customization group this slot belongs in.
303 :printer - A function to call to print the value of a slot.
304 See `eieio-override-prin1' as an example.
306 A class can also have optional options. These options happen in place
307 of documentation (including a :documentation tag), in addition to
308 documentation, or not at all. Supported options are:
310 :documentation - The doc-string used for this class.
312 Options added to EIEIO:
314 :allow-nil-initform - Non-nil to skip typechecking of null initforms.
315 :custom-groups - List of custom group names. Organizes slots into
316 reasonable groups for customizations.
317 :abstract - Non-nil to prevent instances of this class.
318 If a string, use as an error string if someone does
319 try to make an instance.
320 :method-invocation-order
321 - Control the method invocation order if there is
322 multiple inheritance. Valid values are:
323 :breadth-first - The default.
324 :depth-first
326 Options in CLOS not supported in EIEIO:
328 :metaclass - Class to use in place of `standard-class'
329 :default-initargs - Initargs to use when initializing new objects of
330 this class.
332 Due to the way class options are set up, you can add any tags you wish,
333 and reference them using the function `class-option'."
334 ;; We must `eval-and-compile' this so that when we byte compile
335 ;; an eieio program, there is no need to load it ahead of time.
336 ;; It also provides lots of nice debugging errors at compile time.
337 `(eval-and-compile
338 (eieio-defclass ',name ',superclass ',slots ',options-and-doc)))
340 (defvar eieio-defclass-autoload-map (make-vector 7 nil)
341 "Symbol map of superclasses we find in autoloads.")
343 ;; We autoload this because it's used in `make-autoload'.
344 ;;;###autoload
345 (defun eieio-defclass-autoload (cname superclasses filename doc)
346 "Create autoload symbols for the EIEIO class CNAME.
347 SUPERCLASSES are the superclasses that CNAME inherits from.
348 DOC is the docstring for CNAME.
349 This function creates a mock-class for CNAME and adds it into
350 SUPERCLASSES as children.
351 It creates an autoload function for CNAME's constructor."
352 ;; Assume we've already debugged inputs.
354 (let* ((oldc (when (class-p cname) (class-v cname)))
355 (newc (make-vector eieio--class-num-slots nil))
357 (if oldc
358 nil ;; Do nothing if we already have this class.
360 ;; Create the class in NEWC, but don't fill anything else in.
361 (aset newc 0 'defclass)
362 (setf (eieio--class-symbol newc) cname)
364 (let ((clear-parent nil))
365 ;; No parents?
366 (when (not superclasses)
367 (setq superclasses '(eieio-default-superclass)
368 clear-parent t)
371 ;; Hook our new class into the existing structures so we can
372 ;; autoload it later.
373 (dolist (SC superclasses)
376 ;; TODO - If we create an autoload that is in the map, that
377 ;; map needs to be cleared!
380 ;; Does our parent exist?
381 (if (not (class-p SC))
383 ;; Create a symbol for this parent, and then store this
384 ;; parent on that symbol.
385 (let ((sym (intern (symbol-name SC) eieio-defclass-autoload-map)))
386 (if (not (boundp sym))
387 (set sym (list cname))
388 (add-to-list sym cname))
391 ;; We have a parent, save the child in there.
392 (when (not (member cname (eieio--class-children (class-v SC))))
393 (setf (eieio--class-children (class-v SC))
394 (cons cname (eieio--class-children (class-v SC))))))
396 ;; save parent in child
397 (setf (eieio--class-parent newc) (cons SC (eieio--class-parent newc)))
400 ;; turn this into a usable self-pointing symbol
401 (set cname cname)
403 ;; Store the new class vector definition into the symbol. We need to
404 ;; do this first so that we can call defmethod for the accessor.
405 ;; The vector will be updated by the following while loop and will not
406 ;; need to be stored a second time.
407 (put cname 'eieio-class-definition newc)
409 ;; Clear the parent
410 (if clear-parent (setf (eieio--class-parent newc) nil))
412 ;; Create an autoload on top of our constructor function.
413 (autoload cname filename doc nil nil)
414 (autoload (intern (concat (symbol-name cname) "-p")) filename "" nil nil)
415 (autoload (intern (concat (symbol-name cname) "-child-p")) filename "" nil nil)
416 (autoload (intern (concat (symbol-name cname) "-list-p")) filename "" nil nil)
418 ))))
420 (defsubst eieio-class-un-autoload (cname)
421 "If class CNAME is in an autoload state, load its file."
422 (when (eq (car-safe (symbol-function cname)) 'autoload)
423 (load-library (car (cdr (symbol-function cname))))))
425 (defmacro eieio--check-type (type obj)
426 (unless (symbolp obj)
427 (error "eieio--check-type wants OBJ to be a variable"))
428 `(if (not ,(cond
429 ((eq 'or (car-safe type))
430 `(or ,@(mapcar (lambda (type) `(,type ,obj)) (cdr type))))
431 (t `(,type ,obj))))
432 (signal 'wrong-type-argument (list ',type ,obj))))
434 (defun eieio-defclass (cname superclasses slots options-and-doc)
435 ;; FIXME: Most of this should be moved to the `defclass' macro.
436 "Define CNAME as a new subclass of SUPERCLASSES.
437 SLOTS are the slots residing in that class definition, and options or
438 documentation OPTIONS-AND-DOC is the toplevel documentation for this class.
439 See `defclass' for more information."
440 ;; Run our eieio-hook each time, and clear it when we are done.
441 ;; This way people can add hooks safely if they want to modify eieio
442 ;; or add definitions when eieio is loaded or something like that.
443 (run-hooks 'eieio-hook)
444 (setq eieio-hook nil)
446 (eieio--check-type listp superclasses)
448 (let* ((pname superclasses)
449 (newc (make-vector eieio--class-num-slots nil))
450 (oldc (when (class-p cname) (class-v cname)))
451 (groups nil) ;; list of groups id'd from slots
452 (options nil)
453 (clearparent nil))
455 (aset newc 0 'defclass)
456 (setf (eieio--class-symbol newc) cname)
458 ;; If this class already existed, and we are updating its structure,
459 ;; make sure we keep the old child list. This can cause bugs, but
460 ;; if no new slots are created, it also saves time, and prevents
461 ;; method table breakage, particularly when the users is only
462 ;; byte compiling an EIEIO file.
463 (if oldc
464 (setf (eieio--class-children newc) (eieio--class-children oldc))
465 ;; If the old class did not exist, but did exist in the autoload map, then adopt those children.
466 ;; This is like the above, but deals with autoloads nicely.
467 (let ((sym (intern-soft (symbol-name cname) eieio-defclass-autoload-map)))
468 (when sym
469 (condition-case nil
470 (setf (eieio--class-children newc) (symbol-value sym))
471 (error nil))
472 (unintern (symbol-name cname) eieio-defclass-autoload-map)
476 (cond ((and (stringp (car options-and-doc))
477 (/= 1 (% (length options-and-doc) 2)))
478 (error "Too many arguments to `defclass'"))
479 ((and (symbolp (car options-and-doc))
480 (/= 0 (% (length options-and-doc) 2)))
481 (error "Too many arguments to `defclass'"))
484 (setq options
485 (if (stringp (car options-and-doc))
486 (cons :documentation options-and-doc)
487 options-and-doc))
489 (if pname
490 (progn
491 (while pname
492 (if (and (car pname) (symbolp (car pname)))
493 (if (not (class-p (car pname)))
494 ;; bad class
495 (error "Given parent class %s is not a class" (car pname))
496 ;; good parent class...
497 ;; save new child in parent
498 (when (not (member cname (eieio--class-children (class-v (car pname)))))
499 (setf (eieio--class-children (class-v (car pname)))
500 (cons cname (eieio--class-children (class-v (car pname))))))
501 ;; Get custom groups, and store them into our local copy.
502 (mapc (lambda (g) (add-to-list 'groups g))
503 (class-option (car pname) :custom-groups))
504 ;; save parent in child
505 (setf (eieio--class-parent newc) (cons (car pname) (eieio--class-parent newc))))
506 (error "Invalid parent class %s" pname))
507 (setq pname (cdr pname)))
508 ;; Reverse the list of our parents so that they are prioritized in
509 ;; the same order as specified in the code.
510 (setf (eieio--class-parent newc) (nreverse (eieio--class-parent newc))) )
511 ;; If there is nothing to loop over, then inherit from the
512 ;; default superclass.
513 (unless (eq cname 'eieio-default-superclass)
514 ;; adopt the default parent here, but clear it later...
515 (setq clearparent t)
516 ;; save new child in parent
517 (if (not (member cname (eieio--class-children (class-v 'eieio-default-superclass))))
518 (setf (eieio--class-children (class-v 'eieio-default-superclass))
519 (cons cname (eieio--class-children (class-v 'eieio-default-superclass)))))
520 ;; save parent in child
521 (setf (eieio--class-parent newc) (list eieio-default-superclass))))
523 ;; turn this into a usable self-pointing symbol
524 (set cname cname)
526 ;; These two tests must be created right away so we can have self-
527 ;; referencing classes. ei, a class whose slot can contain only
528 ;; pointers to itself.
530 ;; Create the test function
531 (let ((csym (intern (concat (symbol-name cname) "-p"))))
532 (fset csym
533 (list 'lambda (list 'obj)
534 (format "Test OBJ to see if it an object of type %s" cname)
535 (list 'and '(eieio-object-p obj)
536 (list 'same-class-p 'obj cname)))))
538 ;; Make sure the method invocation order is a valid value.
539 (let ((io (class-option-assoc options :method-invocation-order)))
540 (when (and io (not (member io '(:depth-first :breadth-first :c3))))
541 (error "Method invocation order %s is not allowed" io)
544 ;; Create a handy child test too
545 (let ((csym (intern (concat (symbol-name cname) "-child-p"))))
546 (fset csym
547 `(lambda (obj)
548 ,(format
549 "Test OBJ to see if it an object is a child of type %s"
550 cname)
551 (and (eieio-object-p obj)
552 (object-of-class-p obj ,cname))))
554 ;; Create a handy list of the class test too
555 (let ((csym (intern (concat (symbol-name cname) "-list-p"))))
556 (fset csym
557 `(lambda (obj)
558 ,(format
559 "Test OBJ to see if it a list of objects which are a child of type %s"
560 cname)
561 (when (listp obj)
562 (let ((ans t)) ;; nil is valid
563 ;; Loop over all the elements of the input list, test
564 ;; each to make sure it is a child of the desired object class.
565 (while (and obj ans)
566 (setq ans (and (eieio-object-p (car obj))
567 (object-of-class-p (car obj) ,cname)))
568 (setq obj (cdr obj)))
569 ans)))))
571 ;; When using typep, (typep OBJ 'myclass) returns t for objects which
572 ;; are subclasses of myclass. For our predicates, however, it is
573 ;; important for EIEIO to be backwards compatible, where
574 ;; myobject-p, and myobject-child-p are different.
575 ;; "cl" uses this technique to specify symbols with specific typep
576 ;; test, so we can let typep have the CLOS documented behavior
577 ;; while keeping our above predicate clean.
579 ;; It would be cleaner to use `defsetf' here, but that requires cl
580 ;; at runtime.
581 (put cname 'cl-deftype-handler
582 (list 'lambda () `(list 'satisfies (quote ,csym)))))
584 ;; Before adding new slots, let's add all the methods and classes
585 ;; in from the parent class.
586 (eieio-copy-parents-into-subclass newc superclasses)
588 ;; Store the new class vector definition into the symbol. We need to
589 ;; do this first so that we can call defmethod for the accessor.
590 ;; The vector will be updated by the following while loop and will not
591 ;; need to be stored a second time.
592 (put cname 'eieio-class-definition newc)
594 ;; Query each slot in the declaration list and mangle into the
595 ;; class structure I have defined.
596 (while slots
597 (let* ((slot1 (car slots))
598 (name (car slot1))
599 (slot (cdr slot1))
600 (acces (plist-get slot ':accessor))
601 (init (or (plist-get slot ':initform)
602 (if (member ':initform slot) nil
603 eieio-unbound)))
604 (initarg (plist-get slot ':initarg))
605 (docstr (plist-get slot ':documentation))
606 (prot (plist-get slot ':protection))
607 (reader (plist-get slot ':reader))
608 (writer (plist-get slot ':writer))
609 (alloc (plist-get slot ':allocation))
610 (type (plist-get slot ':type))
611 (custom (plist-get slot ':custom))
612 (label (plist-get slot ':label))
613 (customg (plist-get slot ':group))
614 (printer (plist-get slot ':printer))
616 (skip-nil (class-option-assoc options :allow-nil-initform))
619 (if eieio-error-unsupported-class-tags
620 (let ((tmp slot))
621 (while tmp
622 (if (not (member (car tmp) '(:accessor
623 :initform
624 :initarg
625 :documentation
626 :protection
627 :reader
628 :writer
629 :allocation
630 :type
631 :custom
632 :label
633 :group
634 :printer
635 :allow-nil-initform
636 :custom-groups)))
637 (signal 'invalid-slot-type (list (car tmp))))
638 (setq tmp (cdr (cdr tmp))))))
640 ;; Clean up the meaning of protection.
641 (cond ((or (eq prot 'public) (eq prot :public)) (setq prot nil))
642 ((or (eq prot 'protected) (eq prot :protected)) (setq prot 'protected))
643 ((or (eq prot 'private) (eq prot :private)) (setq prot 'private))
644 ((eq prot nil) nil)
645 (t (signal 'invalid-slot-type (list ':protection prot))))
647 ;; Make sure the :allocation parameter has a valid value.
648 (if (not (or (not alloc) (eq alloc :class) (eq alloc :instance)))
649 (signal 'invalid-slot-type (list ':allocation alloc)))
651 ;; The default type specifier is supposed to be t, meaning anything.
652 (if (not type) (setq type t))
654 ;; Label is nil, or a string
655 (if (not (or (null label) (stringp label)))
656 (signal 'invalid-slot-type (list ':label label)))
658 ;; Is there an initarg, but allocation of class?
659 (if (and initarg (eq alloc :class))
660 (message "Class allocated slots do not need :initarg"))
662 ;; intern the symbol so we can use it blankly
663 (if initarg (set initarg initarg))
665 ;; The customgroup should be a list of symbols
666 (cond ((null customg)
667 (setq customg '(default)))
668 ((not (listp customg))
669 (setq customg (list customg))))
670 ;; The customgroup better be a symbol, or list of symbols.
671 (mapc (lambda (cg)
672 (if (not (symbolp cg))
673 (signal 'invalid-slot-type (list ':group cg))))
674 customg)
676 ;; First up, add this slot into our new class.
677 (eieio-add-new-slot newc name init docstr type custom label customg printer
678 prot initarg alloc 'defaultoverride skip-nil)
680 ;; We need to id the group, and store them in a group list attribute.
681 (mapc (lambda (cg) (add-to-list 'groups cg)) customg)
683 ;; Anyone can have an accessor function. This creates a function
684 ;; of the specified name, and also performs a `defsetf' if applicable
685 ;; so that users can `setf' the space returned by this function.
686 (if acces
687 (progn
688 (eieio--defmethod
689 acces (if (eq alloc :class) :static :primary) cname
690 `(lambda (this)
691 ,(format
692 "Retrieves the slot `%s' from an object of class `%s'"
693 name cname)
694 (if (slot-boundp this ',name)
695 (eieio-oref this ',name)
696 ;; Else - Some error? nil?
697 nil)))
699 (if (fboundp 'gv-define-setter)
700 ;; FIXME: We should move more of eieio-defclass into the
701 ;; defclass macro so we don't have to use `eval' and require
702 ;; `gv' at run-time.
703 (eval `(gv-define-setter ,acces (eieio--store eieio--object)
704 (list 'eieio-oset eieio--object '',name
705 eieio--store)))
706 ;; Provide a setf method. It would be cleaner to use
707 ;; defsetf, but that would require CL at runtime.
708 (put acces 'setf-method
709 `(lambda (widget)
710 (let* ((--widget-sym-- (make-symbol "--widget--"))
711 (--store-sym-- (make-symbol "--store--")))
712 (list
713 (list --widget-sym--)
714 (list widget)
715 (list --store-sym--)
716 (list 'eieio-oset --widget-sym-- '',name
717 --store-sym--)
718 (list 'getfoo --widget-sym--))))))))
720 ;; If a writer is defined, then create a generic method of that
721 ;; name whose purpose is to set the value of the slot.
722 (if writer
723 (eieio--defmethod
724 writer nil cname
725 `(lambda (this value)
726 ,(format "Set the slot `%s' of an object of class `%s'"
727 name cname)
728 (setf (slot-value this ',name) value))))
729 ;; If a reader is defined, then create a generic method
730 ;; of that name whose purpose is to access this slot value.
731 (if reader
732 (eieio--defmethod
733 reader nil cname
734 `(lambda (this)
735 ,(format "Access the slot `%s' from object of class `%s'"
736 name cname)
737 (slot-value this ',name))))
739 (setq slots (cdr slots)))
741 ;; Now that everything has been loaded up, all our lists are backwards!
742 ;; Fix that up now.
743 (setf (eieio--class-public-a newc) (nreverse (eieio--class-public-a newc)))
744 (setf (eieio--class-public-d newc) (nreverse (eieio--class-public-d newc)))
745 (setf (eieio--class-public-doc newc) (nreverse (eieio--class-public-doc newc)))
746 (setf (eieio--class-public-type newc)
747 (apply 'vector (nreverse (eieio--class-public-type newc))))
748 (setf (eieio--class-public-custom newc) (nreverse (eieio--class-public-custom newc)))
749 (setf (eieio--class-public-custom-label newc) (nreverse (eieio--class-public-custom-label newc)))
750 (setf (eieio--class-public-custom-group newc) (nreverse (eieio--class-public-custom-group newc)))
751 (setf (eieio--class-public-printer newc) (nreverse (eieio--class-public-printer newc)))
752 (setf (eieio--class-protection newc) (nreverse (eieio--class-protection newc)))
753 (setf (eieio--class-initarg-tuples newc) (nreverse (eieio--class-initarg-tuples newc)))
755 ;; The storage for class-class-allocation-type needs to be turned into
756 ;; a vector now.
757 (setf (eieio--class-class-allocation-type newc)
758 (apply 'vector (eieio--class-class-allocation-type newc)))
760 ;; Also, take class allocated values, and vectorize them for speed.
761 (setf (eieio--class-class-allocation-values newc)
762 (apply 'vector (eieio--class-class-allocation-values newc)))
764 ;; Attach slot symbols into an obarray, and store the index of
765 ;; this slot as the variable slot in this new symbol. We need to
766 ;; know about primes, because obarrays are best set in vectors of
767 ;; prime number length, and we also need to make our vector small
768 ;; to save space, and also optimal for the number of items we have.
769 (let* ((cnt 0)
770 (pubsyms (eieio--class-public-a newc))
771 (prots (eieio--class-protection newc))
772 (l (length pubsyms))
773 (vl (let ((primes '( 3 5 7 11 13 17 19 23 29 31 37 41 43 47
774 53 59 61 67 71 73 79 83 89 97 101 )))
775 (while (and primes (< (car primes) l))
776 (setq primes (cdr primes)))
777 (car primes)))
778 (oa (make-vector vl 0))
779 (newsym))
780 (while pubsyms
781 (setq newsym (intern (symbol-name (car pubsyms)) oa))
782 (set newsym cnt)
783 (setq cnt (1+ cnt))
784 (if (car prots) (put newsym 'protection (car prots)))
785 (setq pubsyms (cdr pubsyms)
786 prots (cdr prots)))
787 (setf (eieio--class-symbol-obarray newc) oa)
790 ;; Create the constructor function
791 (if (class-option-assoc options :abstract)
792 ;; Abstract classes cannot be instantiated. Say so.
793 (let ((abs (class-option-assoc options :abstract)))
794 (if (not (stringp abs))
795 (setq abs (format "Class %s is abstract" cname)))
796 (fset cname
797 `(lambda (&rest stuff)
798 ,(format "You cannot create a new object of type %s" cname)
799 (error ,abs))))
801 ;; Non-abstract classes need a constructor.
802 (fset cname
803 `(lambda (newname &rest slots)
804 ,(format "Create a new object with name NAME of class type %s" cname)
805 (apply 'constructor ,cname newname slots)))
808 ;; Set up a specialized doc string.
809 ;; Use stored value since it is calculated in a non-trivial way
810 (put cname 'variable-documentation
811 (class-option-assoc options :documentation))
813 ;; Save the file location where this class is defined.
814 (let ((fname (if load-in-progress
815 load-file-name
816 buffer-file-name))
817 loc)
818 (when fname
819 (when (string-match "\\.elc\\'" fname)
820 (setq fname (substring fname 0 (1- (length fname)))))
821 (put cname 'class-location fname)))
823 ;; We have a list of custom groups. Store them into the options.
824 (let ((g (class-option-assoc options :custom-groups)))
825 (mapc (lambda (cg) (add-to-list 'g cg)) groups)
826 (if (memq :custom-groups options)
827 (setcar (cdr (memq :custom-groups options)) g)
828 (setq options (cons :custom-groups (cons g options)))))
830 ;; Set up the options we have collected.
831 (setf (eieio--class-options newc) options)
833 ;; if this is a superclass, clear out parent (which was set to the
834 ;; default superclass eieio-default-superclass)
835 (if clearparent (setf (eieio--class-parent newc) nil))
837 ;; Create the cached default object.
838 (let ((cache (make-vector (+ (length (eieio--class-public-a newc)) 3)
839 nil)))
840 (aset cache 0 'object)
841 (setf (eieio--object-class cache) cname)
842 (setf (eieio--object-name cache) 'default-cache-object)
843 (let ((eieio-skip-typecheck t))
844 ;; All type-checking has been done to our satisfaction
845 ;; before this call. Don't waste our time in this call..
846 (eieio-set-defaults cache t))
847 (setf (eieio--class-default-object-cache newc) cache))
849 ;; Return our new class object
850 ;; newc
851 cname
854 (defun eieio-perform-slot-validation-for-default (slot spec value skipnil)
855 "For SLOT, signal if SPEC does not match VALUE.
856 If SKIPNIL is non-nil, then if VALUE is nil return t instead."
857 (if (and (not (eieio-eval-default-p value))
858 (not eieio-skip-typecheck)
859 (not (and skipnil (null value)))
860 (not (eieio-perform-slot-validation spec value)))
861 (signal 'invalid-slot-type (list slot spec value))))
863 (defun eieio-add-new-slot (newc a d doc type cust label custg print prot init alloc
864 &optional defaultoverride skipnil)
865 "Add into NEWC attribute A.
866 If A already exists in NEWC, then do nothing. If it doesn't exist,
867 then also add in D (default), DOC, TYPE, CUST, LABEL, CUSTG, PRINT, PROT, and INIT arg.
868 Argument ALLOC specifies if the slot is allocated per instance, or per class.
869 If optional DEFAULTOVERRIDE is non-nil, then if A exists in NEWC,
870 we must override its value for a default.
871 Optional argument SKIPNIL indicates if type checking should be skipped
872 if default value is nil."
873 ;; Make sure we duplicate those items that are sequences.
874 (condition-case nil
875 (if (sequencep d) (setq d (copy-sequence d)))
876 ;; This copy can fail on a cons cell with a non-cons in the cdr. Let's skip it if it doesn't work.
877 (error nil))
878 (if (sequencep type) (setq type (copy-sequence type)))
879 (if (sequencep cust) (setq cust (copy-sequence cust)))
880 (if (sequencep custg) (setq custg (copy-sequence custg)))
882 ;; To prevent override information w/out specification of storage,
883 ;; we need to do this little hack.
884 (if (member a (eieio--class-class-allocation-a newc)) (setq alloc ':class))
886 (if (or (not alloc) (and (symbolp alloc) (eq alloc ':instance)))
887 ;; In this case, we modify the INSTANCE version of a given slot.
889 (progn
891 ;; Only add this element if it is so-far unique
892 (if (not (member a (eieio--class-public-a newc)))
893 (progn
894 (eieio-perform-slot-validation-for-default a type d skipnil)
895 (setf (eieio--class-public-a newc) (cons a (eieio--class-public-a newc)))
896 (setf (eieio--class-public-d newc) (cons d (eieio--class-public-d newc)))
897 (setf (eieio--class-public-doc newc) (cons doc (eieio--class-public-doc newc)))
898 (setf (eieio--class-public-type newc) (cons type (eieio--class-public-type newc)))
899 (setf (eieio--class-public-custom newc) (cons cust (eieio--class-public-custom newc)))
900 (setf (eieio--class-public-custom-label newc) (cons label (eieio--class-public-custom-label newc)))
901 (setf (eieio--class-public-custom-group newc) (cons custg (eieio--class-public-custom-group newc)))
902 (setf (eieio--class-public-printer newc) (cons print (eieio--class-public-printer newc)))
903 (setf (eieio--class-protection newc) (cons prot (eieio--class-protection newc)))
904 (setf (eieio--class-initarg-tuples newc) (cons (cons init a) (eieio--class-initarg-tuples newc)))
906 ;; When defaultoverride is true, we are usually adding new local
907 ;; attributes which must override the default value of any slot
908 ;; passed in by one of the parent classes.
909 (when defaultoverride
910 ;; There is a match, and we must override the old value.
911 (let* ((ca (eieio--class-public-a newc))
912 (np (member a ca))
913 (num (- (length ca) (length np)))
914 (dp (if np (nthcdr num (eieio--class-public-d newc))
915 nil))
916 (tp (if np (nth num (eieio--class-public-type newc))))
918 (if (not np)
919 (error "EIEIO internal error overriding default value for %s"
921 ;; If type is passed in, is it the same?
922 (if (not (eq type t))
923 (if (not (equal type tp))
924 (error
925 "Child slot type `%s' does not match inherited type `%s' for `%s'"
926 type tp a)))
927 ;; If we have a repeat, only update the initarg...
928 (unless (eq d eieio-unbound)
929 (eieio-perform-slot-validation-for-default a tp d skipnil)
930 (setcar dp d))
931 ;; If we have a new initarg, check for it.
932 (when init
933 (let* ((inits (eieio--class-initarg-tuples newc))
934 (inita (rassq a inits)))
935 ;; Replace the CAR of the associate INITA.
936 ;;(message "Initarg: %S replace %s" inita init)
937 (setcar inita init)
940 ;; PLN Tue Jun 26 11:57:06 2007 : The protection is
941 ;; checked and SHOULD match the superclass
942 ;; protection. Otherwise an error is thrown. However
943 ;; I wonder if a more flexible schedule might be
944 ;; implemented.
946 ;; EML - We used to have (if prot... here,
947 ;; but a prot of 'nil means public.
949 (let ((super-prot (nth num (eieio--class-protection newc)))
951 (if (not (eq prot super-prot))
952 (error "Child slot protection `%s' does not match inherited protection `%s' for `%s'"
953 prot super-prot a)))
954 ;; End original PLN
956 ;; PLN Tue Jun 26 11:57:06 2007 :
957 ;; Do a non redundant combination of ancient custom
958 ;; groups and new ones.
959 (when custg
960 (let* ((groups
961 (nthcdr num (eieio--class-public-custom-group newc)))
962 (list1 (car groups))
963 (list2 (if (listp custg) custg (list custg))))
964 (if (< (length list1) (length list2))
965 (setq list1 (prog1 list2 (setq list2 list1))))
966 (dolist (elt list2)
967 (unless (memq elt list1)
968 (push elt list1)))
969 (setcar groups list1)))
970 ;; End PLN
972 ;; PLN Mon Jun 25 22:44:34 2007 : If a new cust is
973 ;; set, simply replaces the old one.
974 (when cust
975 ;; (message "Custom type redefined to %s" cust)
976 (setcar (nthcdr num (eieio--class-public-custom newc)) cust))
978 ;; If a new label is specified, it simply replaces
979 ;; the old one.
980 (when label
981 ;; (message "Custom label redefined to %s" label)
982 (setcar (nthcdr num (eieio--class-public-custom-label newc)) label))
983 ;; End PLN
985 ;; PLN Sat Jun 30 17:24:42 2007 : when a new
986 ;; doc is specified, simply replaces the old one.
987 (when doc
988 ;;(message "Documentation redefined to %s" doc)
989 (setcar (nthcdr num (eieio--class-public-doc newc))
990 doc))
991 ;; End PLN
993 ;; If a new printer is specified, it simply replaces
994 ;; the old one.
995 (when print
996 ;; (message "printer redefined to %s" print)
997 (setcar (nthcdr num (eieio--class-public-printer newc)) print))
1002 ;; CLASS ALLOCATED SLOTS
1003 (let ((value (eieio-default-eval-maybe d)))
1004 (if (not (member a (eieio--class-class-allocation-a newc)))
1005 (progn
1006 (eieio-perform-slot-validation-for-default a type value skipnil)
1007 ;; Here we have found a :class version of a slot. This
1008 ;; requires a very different approach.
1009 (setf (eieio--class-class-allocation-a newc) (cons a (eieio--class-class-allocation-a newc)))
1010 (setf (eieio--class-class-allocation-doc newc) (cons doc (eieio--class-class-allocation-doc newc)))
1011 (setf (eieio--class-class-allocation-type newc) (cons type (eieio--class-class-allocation-type newc)))
1012 (setf (eieio--class-class-allocation-custom newc) (cons cust (eieio--class-class-allocation-custom newc)))
1013 (setf (eieio--class-class-allocation-custom-label newc) (cons label (eieio--class-class-allocation-custom-label newc)))
1014 (setf (eieio--class-class-allocation-custom-group newc) (cons custg (eieio--class-class-allocation-custom-group newc)))
1015 (setf (eieio--class-class-allocation-protection newc) (cons prot (eieio--class-class-allocation-protection newc)))
1016 ;; Default value is stored in the 'values section, since new objects
1017 ;; can't initialize from this element.
1018 (setf (eieio--class-class-allocation-values newc) (cons value (eieio--class-class-allocation-values newc))))
1019 (when defaultoverride
1020 ;; There is a match, and we must override the old value.
1021 (let* ((ca (eieio--class-class-allocation-a newc))
1022 (np (member a ca))
1023 (num (- (length ca) (length np)))
1024 (dp (if np
1025 (nthcdr num
1026 (eieio--class-class-allocation-values newc))
1027 nil))
1028 (tp (if np (nth num (eieio--class-class-allocation-type newc))
1029 nil)))
1030 (if (not np)
1031 (error "EIEIO internal error overriding default value for %s"
1033 ;; If type is passed in, is it the same?
1034 (if (not (eq type t))
1035 (if (not (equal type tp))
1036 (error
1037 "Child slot type `%s' does not match inherited type `%s' for `%s'"
1038 type tp a)))
1039 ;; EML - Note: the only reason to override a class bound slot
1040 ;; is to change the default, so allow unbound in.
1042 ;; If we have a repeat, only update the value...
1043 (eieio-perform-slot-validation-for-default a tp value skipnil)
1044 (setcar dp value))
1046 ;; PLN Tue Jun 26 11:57:06 2007 : The protection is
1047 ;; checked and SHOULD match the superclass
1048 ;; protection. Otherwise an error is thrown. However
1049 ;; I wonder if a more flexible schedule might be
1050 ;; implemented.
1051 (let ((super-prot
1052 (car (nthcdr num (eieio--class-class-allocation-protection newc)))))
1053 (if (not (eq prot super-prot))
1054 (error "Child slot protection `%s' does not match inherited protection `%s' for `%s'"
1055 prot super-prot a)))
1056 ;; Do a non redundant combination of ancient custom groups
1057 ;; and new ones.
1058 (when custg
1059 (let* ((groups
1060 (nthcdr num (eieio--class-class-allocation-custom-group newc)))
1061 (list1 (car groups))
1062 (list2 (if (listp custg) custg (list custg))))
1063 (if (< (length list1) (length list2))
1064 (setq list1 (prog1 list2 (setq list2 list1))))
1065 (dolist (elt list2)
1066 (unless (memq elt list1)
1067 (push elt list1)))
1068 (setcar groups list1)))
1070 ;; PLN Sat Jun 30 17:24:42 2007 : when a new
1071 ;; doc is specified, simply replaces the old one.
1072 (when doc
1073 ;;(message "Documentation redefined to %s" doc)
1074 (setcar (nthcdr num (eieio--class-class-allocation-doc newc))
1075 doc))
1076 ;; End PLN
1078 ;; If a new printer is specified, it simply replaces
1079 ;; the old one.
1080 (when print
1081 ;; (message "printer redefined to %s" print)
1082 (setcar (nthcdr num (eieio--class-class-allocation-printer newc)) print))
1088 (defun eieio-copy-parents-into-subclass (newc parents)
1089 "Copy into NEWC the slots of PARENTS.
1090 Follow the rules of not overwriting early parents when applying to
1091 the new child class."
1092 (let ((ps (eieio--class-parent newc))
1093 (sn (class-option-assoc (eieio--class-options newc)
1094 ':allow-nil-initform)))
1095 (while ps
1096 ;; First, duplicate all the slots of the parent.
1097 (let ((pcv (class-v (car ps))))
1098 (let ((pa (eieio--class-public-a pcv))
1099 (pd (eieio--class-public-d pcv))
1100 (pdoc (eieio--class-public-doc pcv))
1101 (ptype (eieio--class-public-type pcv))
1102 (pcust (eieio--class-public-custom pcv))
1103 (plabel (eieio--class-public-custom-label pcv))
1104 (pcustg (eieio--class-public-custom-group pcv))
1105 (printer (eieio--class-public-printer pcv))
1106 (pprot (eieio--class-protection pcv))
1107 (pinit (eieio--class-initarg-tuples pcv))
1108 (i 0))
1109 (while pa
1110 (eieio-add-new-slot newc
1111 (car pa) (car pd) (car pdoc) (aref ptype i)
1112 (car pcust) (car plabel) (car pcustg)
1113 (car printer)
1114 (car pprot) (car-safe (car pinit)) nil nil sn)
1115 ;; Increment each value.
1116 (setq pa (cdr pa)
1117 pd (cdr pd)
1118 pdoc (cdr pdoc)
1119 i (1+ i)
1120 pcust (cdr pcust)
1121 plabel (cdr plabel)
1122 pcustg (cdr pcustg)
1123 printer (cdr printer)
1124 pprot (cdr pprot)
1125 pinit (cdr pinit))
1126 )) ;; while/let
1127 ;; Now duplicate all the class alloc slots.
1128 (let ((pa (eieio--class-class-allocation-a pcv))
1129 (pdoc (eieio--class-class-allocation-doc pcv))
1130 (ptype (eieio--class-class-allocation-type pcv))
1131 (pcust (eieio--class-class-allocation-custom pcv))
1132 (plabel (eieio--class-class-allocation-custom-label pcv))
1133 (pcustg (eieio--class-class-allocation-custom-group pcv))
1134 (printer (eieio--class-class-allocation-printer pcv))
1135 (pprot (eieio--class-class-allocation-protection pcv))
1136 (pval (eieio--class-class-allocation-values pcv))
1137 (i 0))
1138 (while pa
1139 (eieio-add-new-slot newc
1140 (car pa) (aref pval i) (car pdoc) (aref ptype i)
1141 (car pcust) (car plabel) (car pcustg)
1142 (car printer)
1143 (car pprot) nil ':class sn)
1144 ;; Increment each value.
1145 (setq pa (cdr pa)
1146 pdoc (cdr pdoc)
1147 pcust (cdr pcust)
1148 plabel (cdr plabel)
1149 pcustg (cdr pcustg)
1150 printer (cdr printer)
1151 pprot (cdr pprot)
1152 i (1+ i))
1153 ))) ;; while/let
1154 ;; Loop over each parent class
1155 (setq ps (cdr ps)))
1158 ;;; CLOS style implementation of object creators.
1160 (defun make-instance (class &rest initargs)
1161 "Make a new instance of CLASS based on INITARGS.
1162 CLASS is a class symbol. For example:
1164 (make-instance 'foo)
1166 INITARGS is a property list with keywords based on the :initarg
1167 for each slot. For example:
1169 (make-instance 'foo :slot1 value1 :slotN valueN)
1171 Compatibility note:
1173 If the first element of INITARGS is a string, it is used as the
1174 name of the class.
1176 In EIEIO, the class' constructor requires a name for use when printing.
1177 `make-instance' in CLOS doesn't use names the way Emacs does, so the
1178 class is used as the name slot instead when INITARGS doesn't start with
1179 a string."
1180 (if (and (car initargs) (stringp (car initargs)))
1181 (apply (class-constructor class) initargs)
1182 (apply (class-constructor class)
1183 (cond ((symbolp class) (symbol-name class))
1184 (t (format "%S" class)))
1185 initargs)))
1188 ;;; CLOS methods and generics
1191 (put 'eieio--defalias 'byte-hunk-handler
1192 #'byte-compile-file-form-defalias) ;;(get 'defalias 'byte-hunk-handler)
1193 (defun eieio--defalias (name body)
1194 "Like `defalias', but with less side-effects.
1195 More specifically, it has no side-effects at all when the new function
1196 definition is the same (`eq') as the old one."
1197 (unless (and (fboundp name)
1198 (eq (symbol-function name) body))
1199 (defalias name body)))
1201 (defmacro defgeneric (method args &optional doc-string)
1202 "Create a generic function METHOD.
1203 DOC-STRING is the base documentation for this class. A generic
1204 function has no body, as its purpose is to decide which method body
1205 is appropriate to use. Uses `defmethod' to create methods, and calls
1206 `defgeneric' for you. With this implementation the ARGS are
1207 currently ignored. You can use `defgeneric' to apply specialized
1208 top level documentation to a method."
1209 `(eieio--defalias ',method
1210 (eieio--defgeneric-init-form ',method ,doc-string)))
1212 (defun eieio--defgeneric-init-form (method doc-string)
1213 "Form to use for the initial definition of a generic."
1214 (cond
1215 ((or (not (fboundp method))
1216 (eq 'autoload (car-safe (symbol-function method))))
1217 ;; Make sure the method tables are installed.
1218 (eieiomt-install method)
1219 ;; Construct the actual body of this function.
1220 (eieio-defgeneric-form method doc-string))
1221 ((generic-p method) (symbol-function method)) ;Leave it as-is.
1222 (t (error "You cannot create a generic/method over an existing symbol: %s"
1223 method))))
1225 (defun eieio-defgeneric-form (method doc-string)
1226 "The lambda form that would be used as the function defined on METHOD.
1227 All methods should call the same EIEIO function for dispatch.
1228 DOC-STRING is the documentation attached to METHOD."
1229 `(lambda (&rest local-args)
1230 ,doc-string
1231 (eieio-generic-call (quote ,method) local-args)))
1233 (defsubst eieio-defgeneric-reset-generic-form (method)
1234 "Setup METHOD to call the generic form."
1235 (let ((doc-string (documentation method)))
1236 (fset method (eieio-defgeneric-form method doc-string))))
1238 (defun eieio-defgeneric-form-primary-only (method doc-string)
1239 "The lambda form that would be used as the function defined on METHOD.
1240 All methods should call the same EIEIO function for dispatch.
1241 DOC-STRING is the documentation attached to METHOD."
1242 `(lambda (&rest local-args)
1243 ,doc-string
1244 (eieio-generic-call-primary-only (quote ,method) local-args)))
1246 (defsubst eieio-defgeneric-reset-generic-form-primary-only (method)
1247 "Setup METHOD to call the generic form."
1248 (let ((doc-string (documentation method)))
1249 (fset method (eieio-defgeneric-form-primary-only method doc-string))))
1251 (defun eieio-defgeneric-form-primary-only-one (method doc-string
1252 class
1253 impl
1255 "The lambda form that would be used as the function defined on METHOD.
1256 All methods should call the same EIEIO function for dispatch.
1257 DOC-STRING is the documentation attached to METHOD.
1258 CLASS is the class symbol needed for private method access.
1259 IMPL is the symbol holding the method implementation."
1260 ;; NOTE: I tried out byte compiling this little fcn. Turns out it
1261 ;; is faster to execute this for not byte-compiled. ie, install this,
1262 ;; then measure calls going through here. I wonder why.
1263 (require 'bytecomp)
1264 (let ((byte-compile-warnings nil))
1265 (byte-compile
1266 `(lambda (&rest local-args)
1267 ,doc-string
1268 ;; This is a cool cheat. Usually we need to look up in the
1269 ;; method table to find out if there is a method or not. We can
1270 ;; instead make that determination at load time when there is
1271 ;; only one method. If the first arg is not a child of the class
1272 ;; of that one implementation, then clearly, there is no method def.
1273 (if (not (eieio-object-p (car local-args)))
1274 ;; Not an object. Just signal.
1275 (signal 'no-method-definition
1276 (list ',method local-args))
1278 ;; We do have an object. Make sure it is the right type.
1279 (if ,(if (eq class eieio-default-superclass)
1280 nil ; default superclass means just an obj. Already asked.
1281 `(not (child-of-class-p (eieio--object-class (car local-args))
1282 ',class)))
1284 ;; If not the right kind of object, call no applicable
1285 (apply 'no-applicable-method (car local-args)
1286 ',method local-args)
1288 ;; It is ok, do the call.
1289 ;; Fill in inter-call variables then evaluate the method.
1290 (let ((scoped-class ',class)
1291 (eieio-generic-call-next-method-list nil)
1292 (eieio-generic-call-key method-primary)
1293 (eieio-generic-call-methodname ',method)
1294 (eieio-generic-call-arglst local-args)
1296 ,(if (< emacs-major-version 24)
1297 `(apply ,(list 'quote impl) local-args)
1298 `(apply #',impl local-args))
1299 ;(,impl local-args)
1300 )))))))
1302 (defsubst eieio-defgeneric-reset-generic-form-primary-only-one (method)
1303 "Setup METHOD to call the generic form."
1304 (let* ((doc-string (documentation method))
1305 (M (get method 'eieio-method-tree))
1306 (entry (car (aref M method-primary)))
1308 (fset method (eieio-defgeneric-form-primary-only-one
1309 method doc-string
1310 (car entry)
1311 (cdr entry)
1312 ))))
1314 (defun eieio-unbind-method-implementations (method)
1315 "Make the generic method METHOD have no implementations.
1316 It will leave the original generic function in place,
1317 but remove reference to all implementations of METHOD."
1318 (put method 'eieio-method-tree nil)
1319 (put method 'eieio-method-obarray nil))
1321 (defmacro defmethod (method &rest args)
1322 "Create a new METHOD through `defgeneric' with ARGS.
1324 The optional second argument KEY is a specifier that
1325 modifies how the method is called, including:
1326 :before - Method will be called before the :primary
1327 :primary - The default if not specified
1328 :after - Method will be called after the :primary
1329 :static - First arg could be an object or class
1330 The next argument is the ARGLIST. The ARGLIST specifies the arguments
1331 to the method as with `defun'. The first argument can have a type
1332 specifier, such as:
1333 ((VARNAME CLASS) ARG2 ...)
1334 where VARNAME is the name of the local variable for the method being
1335 created. The CLASS is a class symbol for a class made with `defclass'.
1336 A DOCSTRING comes after the ARGLIST, and is optional.
1337 All the rest of the args are the BODY of the method. A method will
1338 return the value of the last form in the BODY.
1340 Summary:
1342 (defmethod mymethod [:before | :primary | :after | :static]
1343 ((typearg class-name) arg2 &optional opt &rest rest)
1344 \"doc-string\"
1345 body)"
1346 (let* ((key (if (keywordp (car args)) (pop args)))
1347 (params (car args))
1348 (arg1 (car params))
1349 (fargs (if (consp arg1)
1350 (cons (car arg1) (cdr params))
1351 params))
1352 (class (if (consp arg1) (nth 1 arg1)))
1353 (code `(lambda ,fargs ,@(cdr args))))
1354 `(progn
1355 ;; Make sure there is a generic and the byte-compiler sees it.
1356 (defgeneric ,method ,args
1357 ,(or (documentation code)
1358 (format "Generically created method `%s'." method)))
1359 (eieio--defmethod ',method ',key ',class #',code))))
1361 (defun eieio--defmethod (method kind argclass code)
1362 "Work part of the `defmethod' macro defining METHOD with ARGS."
1363 (let ((key
1364 ;; Find optional keys.
1365 (cond ((memq kind '(:BEFORE :before)) method-before)
1366 ((memq kind '(:AFTER :after)) method-after)
1367 ((memq kind '(:STATIC :static)) method-static)
1368 ((memq kind '(:PRIMARY :primary nil)) method-primary)
1369 ;; Primary key.
1370 ;; (t method-primary)
1371 (t (error "Unknown method kind %S" kind)))))
1372 ;; Make sure there is a generic (when called from defclass).
1373 (eieio--defalias
1374 method (eieio--defgeneric-init-form
1375 method (or (documentation code)
1376 (format "Generically created method `%s'." method))))
1377 ;; Create symbol for property to bind to. If the first arg is of
1378 ;; the form (varname vartype) and `vartype' is a class, then
1379 ;; that class will be the type symbol. If not, then it will fall
1380 ;; under the type `primary' which is a non-specific calling of the
1381 ;; function.
1382 (if argclass
1383 (if (not (class-p argclass))
1384 (error "Unknown class type %s in method parameters"
1385 argclass))
1386 ;; Generics are higher.
1387 (setq key (eieio-specialized-key-to-generic-key key)))
1388 ;; Put this lambda into the symbol so we can find it.
1389 (eieiomt-add method code key argclass)
1392 (when eieio-optimize-primary-methods-flag
1393 ;; Optimizing step:
1395 ;; If this method, after this setup, only has primary methods, then
1396 ;; we can setup the generic that way.
1397 (if (generic-primary-only-p method)
1398 ;; If there is only one primary method, then we can go one more
1399 ;; optimization step.
1400 (if (generic-primary-only-one-p method)
1401 (eieio-defgeneric-reset-generic-form-primary-only-one method)
1402 (eieio-defgeneric-reset-generic-form-primary-only method))
1403 (eieio-defgeneric-reset-generic-form method)))
1405 method)
1407 ;;; Slot type validation
1409 ;; This is a hideous hack for replacing `typep' from cl-macs, to avoid
1410 ;; requiring the CL library at run-time. It can be eliminated if/when
1411 ;; `typep' is merged into Emacs core.
1412 (defun eieio--typep (val type)
1413 (if (symbolp type)
1414 (cond ((get type 'cl-deftype-handler)
1415 (eieio--typep val (funcall (get type 'cl-deftype-handler))))
1416 ((eq type t) t)
1417 ((eq type 'null) (null val))
1418 ((eq type 'atom) (atom val))
1419 ((eq type 'float) (and (numberp val) (not (integerp val))))
1420 ((eq type 'real) (numberp val))
1421 ((eq type 'fixnum) (integerp val))
1422 ((memq type '(character string-char)) (characterp val))
1424 (let* ((name (symbol-name type))
1425 (namep (intern (concat name "p"))))
1426 (if (fboundp namep)
1427 (funcall `(lambda () (,namep val)))
1428 (funcall `(lambda ()
1429 (,(intern (concat name "-p")) val)))))))
1430 (cond ((get (car type) 'cl-deftype-handler)
1431 (eieio--typep val (apply (get (car type) 'cl-deftype-handler)
1432 (cdr type))))
1433 ((memq (car type) '(integer float real number))
1434 (and (eieio--typep val (car type))
1435 (or (memq (cadr type) '(* nil))
1436 (if (consp (cadr type))
1437 (> val (car (cadr type)))
1438 (>= val (cadr type))))
1439 (or (memq (caddr type) '(* nil))
1440 (if (consp (car (cddr type)))
1441 (< val (caar (cddr type)))
1442 (<= val (car (cddr type)))))))
1443 ((memq (car type) '(and or not))
1444 (eval (cons (car type)
1445 (mapcar (lambda (x)
1446 `(eieio--typep (quote ,val) (quote ,x)))
1447 (cdr type)))))
1448 ((memq (car type) '(member member*))
1449 (memql val (cdr type)))
1450 ((eq (car type) 'satisfies)
1451 (funcall `(lambda () (,(cadr type) val))))
1452 (t (error "Bad type spec: %s" type)))))
1454 (defun eieio-perform-slot-validation (spec value)
1455 "Return non-nil if SPEC does not match VALUE."
1456 (or (eq spec t) ; t always passes
1457 (eq value eieio-unbound) ; unbound always passes
1458 (eieio--typep value spec)))
1460 (defun eieio-validate-slot-value (class slot-idx value slot)
1461 "Make sure that for CLASS referencing SLOT-IDX, VALUE is valid.
1462 Checks the :type specifier.
1463 SLOT is the slot that is being checked, and is only used when throwing
1464 an error."
1465 (if eieio-skip-typecheck
1467 ;; Trim off object IDX junk added in for the object index.
1468 (setq slot-idx (- slot-idx 3))
1469 (let ((st (aref (eieio--class-public-type (class-v class)) slot-idx)))
1470 (if (not (eieio-perform-slot-validation st value))
1471 (signal 'invalid-slot-type (list class slot st value))))))
1473 (defun eieio-validate-class-slot-value (class slot-idx value slot)
1474 "Make sure that for CLASS referencing SLOT-IDX, VALUE is valid.
1475 Checks the :type specifier.
1476 SLOT is the slot that is being checked, and is only used when throwing
1477 an error."
1478 (if eieio-skip-typecheck
1480 (let ((st (aref (eieio--class-class-allocation-type (class-v class))
1481 slot-idx)))
1482 (if (not (eieio-perform-slot-validation st value))
1483 (signal 'invalid-slot-type (list class slot st value))))))
1485 (defun eieio-barf-if-slot-unbound (value instance slotname fn)
1486 "Throw a signal if VALUE is a representation of an UNBOUND slot.
1487 INSTANCE is the object being referenced. SLOTNAME is the offending
1488 slot. If the slot is ok, return VALUE.
1489 Argument FN is the function calling this verifier."
1490 (if (and (eq value eieio-unbound) (not eieio-skip-typecheck))
1491 (slot-unbound instance (eieio-object-class instance) slotname fn)
1492 value))
1494 ;;; Get/Set slots in an object.
1496 (defmacro oref (obj slot)
1497 "Retrieve the value stored in OBJ in the slot named by SLOT.
1498 Slot is the name of the slot when created by `defclass' or the label
1499 created by the :initarg tag."
1500 `(eieio-oref ,obj (quote ,slot)))
1502 (defun eieio-oref (obj slot)
1503 "Return the value in OBJ at SLOT in the object vector."
1504 (eieio--check-type (or eieio-object-p class-p) obj)
1505 (eieio--check-type symbolp slot)
1506 (if (class-p obj) (eieio-class-un-autoload obj))
1507 (let* ((class (if (class-p obj) obj (eieio--object-class obj)))
1508 (c (eieio-slot-name-index class obj slot)))
1509 (if (not c)
1510 ;; It might be missing because it is a :class allocated slot.
1511 ;; Let's check that info out.
1512 (if (setq c (eieio-class-slot-name-index class slot))
1513 ;; Oref that slot.
1514 (aref (eieio--class-class-allocation-values (class-v class)) c)
1515 ;; The slot-missing method is a cool way of allowing an object author
1516 ;; to intercept missing slot definitions. Since it is also the LAST
1517 ;; thing called in this fn, its return value would be retrieved.
1518 (slot-missing obj slot 'oref)
1519 ;;(signal 'invalid-slot-name (list (eieio-object-name obj) slot))
1521 (eieio--check-type eieio-object-p obj)
1522 (eieio-barf-if-slot-unbound (aref obj c) obj slot 'oref))))
1524 (defalias 'slot-value 'eieio-oref)
1525 (defalias 'set-slot-value 'eieio-oset)
1527 (defmacro oref-default (obj slot)
1528 "Get the default value of OBJ (maybe a class) for SLOT.
1529 The default value is the value installed in a class with the :initform
1530 tag. SLOT can be the slot name, or the tag specified by the :initarg
1531 tag in the `defclass' call."
1532 `(eieio-oref-default ,obj (quote ,slot)))
1534 (defun eieio-oref-default (obj slot)
1535 "Do the work for the macro `oref-default' with similar parameters.
1536 Fills in OBJ's SLOT with its default value."
1537 (eieio--check-type (or eieio-object-p class-p) obj)
1538 (eieio--check-type symbolp slot)
1539 (let* ((cl (if (eieio-object-p obj) (eieio--object-class obj) obj))
1540 (c (eieio-slot-name-index cl obj slot)))
1541 (if (not c)
1542 ;; It might be missing because it is a :class allocated slot.
1543 ;; Let's check that info out.
1544 (if (setq c
1545 (eieio-class-slot-name-index cl slot))
1546 ;; Oref that slot.
1547 (aref (eieio--class-class-allocation-values (class-v cl))
1549 (slot-missing obj slot 'oref-default)
1550 ;;(signal 'invalid-slot-name (list (class-name cl) slot))
1552 (eieio-barf-if-slot-unbound
1553 (let ((val (nth (- c 3) (eieio--class-public-d (class-v cl)))))
1554 (eieio-default-eval-maybe val))
1555 obj cl 'oref-default))))
1557 (defsubst eieio-eval-default-p (val)
1558 "Whether the default value VAL should be evaluated for use."
1559 (and (consp val) (symbolp (car val)) (fboundp (car val))))
1561 (defun eieio-default-eval-maybe (val)
1562 "Check VAL, and return what `oref-default' would provide."
1563 (cond
1564 ;; Is it a function call? If so, evaluate it.
1565 ((eieio-eval-default-p val)
1566 (eval val))
1567 ;;;; check for quoted things, and unquote them
1568 ;;((and (consp val) (eq (car val) 'quote))
1569 ;; (car (cdr val)))
1570 ;; return it verbatim
1571 (t val)))
1573 ;;; Handy CLOS macros
1575 (defmacro with-slots (spec-list object &rest body)
1576 "Bind SPEC-LIST lexically to slot values in OBJECT, and execute BODY.
1577 This establishes a lexical environment for referring to the slots in
1578 the instance named by the given slot-names as though they were
1579 variables. Within such a context the value of the slot can be
1580 specified by using its slot name, as if it were a lexically bound
1581 variable. Both setf and setq can be used to set the value of the
1582 slot.
1584 SPEC-LIST is of a form similar to `let'. For example:
1586 ((VAR1 SLOT1)
1587 SLOT2
1588 SLOTN
1589 (VARN+1 SLOTN+1))
1591 Where each VAR is the local variable given to the associated
1592 SLOT. A slot specified without a variable name is given a
1593 variable name of the same name as the slot."
1594 (declare (indent 2))
1595 ;; Transform the spec-list into a symbol-macrolet spec-list.
1596 (let ((mappings (mapcar (lambda (entry)
1597 (let ((var (if (listp entry) (car entry) entry))
1598 (slot (if (listp entry) (cadr entry) entry)))
1599 (list var `(slot-value ,object ',slot))))
1600 spec-list)))
1601 (append (list 'symbol-macrolet mappings)
1602 body)))
1604 ;;; Simple generators, and query functions. None of these would do
1605 ;; well embedded into an object.
1607 (define-obsolete-function-alias
1608 'object-class-fast #'eieio--object-class "24.4")
1610 (defun eieio-class-name (class) "Return a Lisp like symbol name for CLASS."
1611 (eieio--check-type class-p class)
1612 ;; I think this is supposed to return a symbol, but to me CLASS is a symbol,
1613 ;; and I wanted a string. Arg!
1614 (format "#<class %s>" (symbol-name class)))
1615 (define-obsolete-function-alias 'class-name #'eieio-class-name "24.4")
1617 (defun eieio-object-name (obj &optional extra)
1618 "Return a Lisp like symbol string for object OBJ.
1619 If EXTRA, include that in the string returned to represent the symbol."
1620 (eieio--check-type eieio-object-p obj)
1621 (format "#<%s %s%s>" (symbol-name (eieio--object-class obj))
1622 (eieio--object-name obj) (or extra "")))
1623 (define-obsolete-function-alias 'object-name #'eieio-object-name "24.4")
1625 (defun eieio-object-name-string (obj) "Return a string which is OBJ's name."
1626 (eieio--check-type eieio-object-p obj)
1627 (eieio--object-name obj))
1628 (define-obsolete-function-alias
1629 'object-name-string #'eieio-object-name-string "24.4")
1631 (defun eieio-object-set-name-string (obj name)
1632 "Set the string which is OBJ's NAME."
1633 (eieio--check-type eieio-object-p obj)
1634 (eieio--check-type stringp name)
1635 (setf (eieio--object-name obj) name))
1636 (define-obsolete-function-alias
1637 'object-set-name-string 'eieio-object-set-name-string "24.4")
1639 (defun eieio-object-class (obj) "Return the class struct defining OBJ."
1640 (eieio--check-type eieio-object-p obj)
1641 (eieio--object-class obj))
1642 (define-obsolete-function-alias 'object-class #'eieio-object-class "24.4")
1643 ;; CLOS name, maybe?
1644 (define-obsolete-function-alias 'class-of #'eieio-object-class "24.4")
1646 (defun eieio-object-class-name (obj)
1647 "Return a Lisp like symbol name for OBJ's class."
1648 (eieio--check-type eieio-object-p obj)
1649 (eieio-class-name (eieio--object-class obj)))
1650 (define-obsolete-function-alias
1651 'object-class-name 'eieio-object-class-name "24.4")
1653 (defmacro eieio-class-parents-fast (class)
1654 "Return parent classes to CLASS with no check."
1655 `(eieio--class-parent (class-v ,class)))
1657 (defun eieio-class-parents (class)
1658 "Return parent classes to CLASS. (overload of variable).
1660 The CLOS function `class-direct-superclasses' is aliased to this function."
1661 (eieio--check-type class-p class)
1662 (eieio-class-parents-fast class))
1663 (define-obsolete-function-alias 'class-parents #'eieio-class-parents "24.4")
1665 (defmacro eieio-class-children-fast (class) "Return child classes to CLASS with no check."
1666 `(eieio--class-children (class-v ,class)))
1668 (defun eieio-class-children (class)
1669 "Return child classes to CLASS.
1671 The CLOS function `class-direct-subclasses' is aliased to this function."
1672 (eieio--check-type class-p class)
1673 (eieio-class-children-fast class))
1674 (define-obsolete-function-alias
1675 'class-children #'eieio-class-children "24.4")
1677 (defun eieio-c3-candidate (class remaining-inputs)
1678 "Return CLASS if it can go in the result now, otherwise nil"
1679 ;; Ensure CLASS is not in any position but the first in any of the
1680 ;; element lists of REMAINING-INPUTS.
1681 (and (not (let ((found nil))
1682 (while (and remaining-inputs (not found))
1683 (setq found (member class (cdr (car remaining-inputs)))
1684 remaining-inputs (cdr remaining-inputs)))
1685 found))
1686 class))
1688 (defun eieio-c3-merge-lists (reversed-partial-result remaining-inputs)
1689 "Merge REVERSED-PARTIAL-RESULT REMAINING-INPUTS in a consistent order, if possible.
1690 If a consistent order does not exist, signal an error."
1691 (if (let ((tail remaining-inputs)
1692 (found nil))
1693 (while (and tail (not found))
1694 (setq found (car tail) tail (cdr tail)))
1695 (not found))
1696 ;; If all remaining inputs are empty lists, we are done.
1697 (nreverse reversed-partial-result)
1698 ;; Otherwise, we try to find the next element of the result. This
1699 ;; is achieved by considering the first element of each
1700 ;; (non-empty) input list and accepting a candidate if it is
1701 ;; consistent with the rests of the input lists.
1702 (let* ((found nil)
1703 (tail remaining-inputs)
1704 (next (progn
1705 (while (and tail (not found))
1706 (setq found (and (car tail)
1707 (eieio-c3-candidate (caar tail)
1708 remaining-inputs))
1709 tail (cdr tail)))
1710 found)))
1711 (if next
1712 ;; The graph is consistent so far, add NEXT to result and
1713 ;; merge input lists, dropping NEXT from their heads where
1714 ;; applicable.
1715 (eieio-c3-merge-lists
1716 (cons next reversed-partial-result)
1717 (mapcar (lambda (l) (if (eq (first l) next) (rest l) l))
1718 remaining-inputs))
1719 ;; The graph is inconsistent, give up
1720 (signal 'inconsistent-class-hierarchy (list remaining-inputs))))))
1722 (defun eieio-class-precedence-dfs (class)
1723 "Return all parents of CLASS in depth-first order."
1724 (let* ((parents (eieio-class-parents-fast class))
1725 (classes (copy-sequence
1726 (apply #'append
1727 (list class)
1729 (mapcar
1730 (lambda (parent)
1731 (cons parent
1732 (eieio-class-precedence-dfs parent)))
1733 parents)
1734 '((eieio-default-superclass))))))
1735 (tail classes))
1736 ;; Remove duplicates.
1737 (while tail
1738 (setcdr tail (delq (car tail) (cdr tail)))
1739 (setq tail (cdr tail)))
1740 classes))
1742 (defun eieio-class-precedence-bfs (class)
1743 "Return all parents of CLASS in breadth-first order."
1744 (let ((result)
1745 (queue (or (eieio-class-parents-fast class)
1746 '(eieio-default-superclass))))
1747 (while queue
1748 (let ((head (pop queue)))
1749 (unless (member head result)
1750 (push head result)
1751 (unless (eq head 'eieio-default-superclass)
1752 (setq queue (append queue (or (eieio-class-parents-fast head)
1753 '(eieio-default-superclass))))))))
1754 (cons class (nreverse result)))
1757 (defun eieio-class-precedence-c3 (class)
1758 "Return all parents of CLASS in c3 order."
1759 (let ((parents (eieio-class-parents-fast class)))
1760 (eieio-c3-merge-lists
1761 (list class)
1762 (append
1764 (mapcar
1765 (lambda (x)
1766 (eieio-class-precedence-c3 x))
1767 parents)
1768 '((eieio-default-superclass)))
1769 (list parents))))
1772 (defun eieio-class-precedence-list (class)
1773 "Return (transitively closed) list of parents of CLASS.
1774 The order, in which the parents are returned depends on the
1775 method invocation orders of the involved classes."
1776 (if (or (null class) (eq class 'eieio-default-superclass))
1778 (case (class-method-invocation-order class)
1779 (:depth-first
1780 (eieio-class-precedence-dfs class))
1781 (:breadth-first
1782 (eieio-class-precedence-bfs class))
1783 (:c3
1784 (eieio-class-precedence-c3 class))))
1786 (define-obsolete-function-alias
1787 'class-precedence-list 'eieio-class-precedence-list "24.4")
1789 ;; Official CLOS functions.
1790 (define-obsolete-function-alias
1791 'class-direct-superclasses #'eieio-class-parents "24.4")
1792 (define-obsolete-function-alias
1793 'class-direct-subclasses #'eieio-class-children "24.4")
1795 (defmacro eieio-class-parent (class)
1796 "Return first parent class to CLASS. (overload of variable)."
1797 `(car (eieio-class-parents ,class)))
1798 (define-obsolete-function-alias 'class-parent #'eieio-class-parent "24.4")
1800 (defmacro same-class-fast-p (obj class)
1801 "Return t if OBJ is of class-type CLASS with no error checking."
1802 `(eq (eieio--object-class ,obj) ,class))
1804 (defun same-class-p (obj class) "Return t if OBJ is of class-type CLASS."
1805 (eieio--check-type class-p class)
1806 (eieio--check-type eieio-object-p obj)
1807 (same-class-fast-p obj class))
1809 (defun object-of-class-p (obj class)
1810 "Return non-nil if OBJ is an instance of CLASS or CLASS' subclasses."
1811 (eieio--check-type eieio-object-p obj)
1812 ;; class will be checked one layer down
1813 (child-of-class-p (eieio--object-class obj) class))
1814 ;; Backwards compatibility
1815 (defalias 'obj-of-class-p 'object-of-class-p)
1817 (defun child-of-class-p (child class)
1818 "Return non-nil if CHILD class is a subclass of CLASS."
1819 (eieio--check-type class-p class)
1820 (eieio--check-type class-p child)
1821 (let ((p nil))
1822 (while (and child (not (eq child class)))
1823 (setq p (append p (eieio--class-parent (class-v child)))
1824 child (car p)
1825 p (cdr p)))
1826 (if child t)))
1828 (defun object-slots (obj)
1829 "Return list of slots available in OBJ."
1830 (eieio--check-type eieio-object-p obj)
1831 (eieio--class-public-a (class-v (eieio--object-class obj))))
1833 (defun class-slot-initarg (class slot) "Fetch from CLASS, SLOT's :initarg."
1834 (eieio--check-type class-p class)
1835 (let ((ia (eieio--class-initarg-tuples (class-v class)))
1836 (f nil))
1837 (while (and ia (not f))
1838 (if (eq (cdr (car ia)) slot)
1839 (setq f (car (car ia))))
1840 (setq ia (cdr ia)))
1843 ;;; Object Set macros
1845 (defmacro oset (obj slot value)
1846 "Set the value in OBJ for slot SLOT to VALUE.
1847 SLOT is the slot name as specified in `defclass' or the tag created
1848 with in the :initarg slot. VALUE can be any Lisp object."
1849 `(eieio-oset ,obj (quote ,slot) ,value))
1851 (defun eieio-oset (obj slot value)
1852 "Do the work for the macro `oset'.
1853 Fills in OBJ's SLOT with VALUE."
1854 (eieio--check-type eieio-object-p obj)
1855 (eieio--check-type symbolp slot)
1856 (let ((c (eieio-slot-name-index (eieio--object-class obj) obj slot)))
1857 (if (not c)
1858 ;; It might be missing because it is a :class allocated slot.
1859 ;; Let's check that info out.
1860 (if (setq c
1861 (eieio-class-slot-name-index (eieio--object-class obj) slot))
1862 ;; Oset that slot.
1863 (progn
1864 (eieio-validate-class-slot-value (eieio--object-class obj) c value slot)
1865 (aset (eieio--class-class-allocation-values (class-v (eieio--object-class obj)))
1866 c value))
1867 ;; See oref for comment on `slot-missing'
1868 (slot-missing obj slot 'oset value)
1869 ;;(signal 'invalid-slot-name (list (eieio-object-name obj) slot))
1871 (eieio-validate-slot-value (eieio--object-class obj) c value slot)
1872 (aset obj c value))))
1874 (defmacro oset-default (class slot value)
1875 "Set the default slot in CLASS for SLOT to VALUE.
1876 The default value is usually set with the :initform tag during class
1877 creation. This allows users to change the default behavior of classes
1878 after they are created."
1879 `(eieio-oset-default ,class (quote ,slot) ,value))
1881 (defun eieio-oset-default (class slot value)
1882 "Do the work for the macro `oset-default'.
1883 Fills in the default value in CLASS' in SLOT with VALUE."
1884 (eieio--check-type class-p class)
1885 (eieio--check-type symbolp slot)
1886 (let* ((scoped-class class)
1887 (c (eieio-slot-name-index class nil slot)))
1888 (if (not c)
1889 ;; It might be missing because it is a :class allocated slot.
1890 ;; Let's check that info out.
1891 (if (setq c (eieio-class-slot-name-index class slot))
1892 (progn
1893 ;; Oref that slot.
1894 (eieio-validate-class-slot-value class c value slot)
1895 (aset (eieio--class-class-allocation-values (class-v class)) c
1896 value))
1897 (signal 'invalid-slot-name (list (eieio-class-name class) slot)))
1898 (eieio-validate-slot-value class c value slot)
1899 ;; Set this into the storage for defaults.
1900 (setcar (nthcdr (- c 3) (eieio--class-public-d (class-v class)))
1901 value)
1902 ;; Take the value, and put it into our cache object.
1903 (eieio-oset (eieio--class-default-object-cache (class-v class))
1904 slot value)
1907 ;;; CLOS queries into classes and slots
1909 (defun slot-boundp (object slot)
1910 "Return non-nil if OBJECT's SLOT is bound.
1911 Setting a slot's value makes it bound. Calling `slot-makeunbound' will
1912 make a slot unbound.
1913 OBJECT can be an instance or a class."
1914 ;; Skip typechecking while retrieving this value.
1915 (let ((eieio-skip-typecheck t))
1916 ;; Return nil if the magic symbol is in there.
1917 (not (eq (cond
1918 ((eieio-object-p object) (eieio-oref object slot))
1919 ((class-p object) (eieio-oref-default object slot))
1920 (t (signal 'wrong-type-argument (list 'eieio-object-p object))))
1921 eieio-unbound))))
1923 (defun slot-makeunbound (object slot)
1924 "In OBJECT, make SLOT unbound."
1925 (eieio-oset object slot eieio-unbound))
1927 (defun slot-exists-p (object-or-class slot)
1928 "Return non-nil if OBJECT-OR-CLASS has SLOT."
1929 (let ((cv (class-v (cond ((eieio-object-p object-or-class)
1930 (eieio-object-class object-or-class))
1931 ((class-p object-or-class)
1932 object-or-class))
1934 (or (memq slot (eieio--class-public-a cv))
1935 (memq slot (eieio--class-class-allocation-a cv)))
1938 (defun find-class (symbol &optional errorp)
1939 "Return the class that SYMBOL represents.
1940 If there is no class, nil is returned if ERRORP is nil.
1941 If ERRORP is non-nil, `wrong-argument-type' is signaled."
1942 (if (not (class-p symbol))
1943 (if errorp (signal 'wrong-type-argument (list 'class-p symbol))
1944 nil)
1945 (class-v symbol)))
1947 ;;; Slightly more complex utility functions for objects
1949 (defun object-assoc (key slot list)
1950 "Return an object if KEY is `equal' to SLOT's value of an object in LIST.
1951 LIST is a list of objects whose slots are searched.
1952 Objects in LIST do not need to have a slot named SLOT, nor does
1953 SLOT need to be bound. If these errors occur, those objects will
1954 be ignored."
1955 (eieio--check-type listp list)
1956 (while (and list (not (condition-case nil
1957 ;; This prevents errors for missing slots.
1958 (equal key (eieio-oref (car list) slot))
1959 (error nil))))
1960 (setq list (cdr list)))
1961 (car list))
1963 (defun object-assoc-list (slot list)
1964 "Return an association list with the contents of SLOT as the key element.
1965 LIST must be a list of objects with SLOT in it.
1966 This is useful when you need to do completing read on an object group."
1967 (eieio--check-type listp list)
1968 (let ((assoclist nil))
1969 (while list
1970 (setq assoclist (cons (cons (eieio-oref (car list) slot)
1971 (car list))
1972 assoclist))
1973 (setq list (cdr list)))
1974 (nreverse assoclist)))
1976 (defun object-assoc-list-safe (slot list)
1977 "Return an association list with the contents of SLOT as the key element.
1978 LIST must be a list of objects, but those objects do not need to have
1979 SLOT in it. If it does not, then that element is left out of the association
1980 list."
1981 (eieio--check-type listp list)
1982 (let ((assoclist nil))
1983 (while list
1984 (if (slot-exists-p (car list) slot)
1985 (setq assoclist (cons (cons (eieio-oref (car list) slot)
1986 (car list))
1987 assoclist)))
1988 (setq list (cdr list)))
1989 (nreverse assoclist)))
1991 (defun object-add-to-list (object slot item &optional append)
1992 "In OBJECT's SLOT, add ITEM to the list of elements.
1993 Optional argument APPEND indicates we need to append to the list.
1994 If ITEM already exists in the list in SLOT, then it is not added.
1995 Comparison is done with `equal' through the `member' function call.
1996 If SLOT is unbound, bind it to the list containing ITEM."
1997 (let (ov)
1998 ;; Find the originating list.
1999 (if (not (slot-boundp object slot))
2000 (setq ov (list item))
2001 (setq ov (eieio-oref object slot))
2002 ;; turn it into a list.
2003 (unless (listp ov)
2004 (setq ov (list ov)))
2005 ;; Do the combination
2006 (if (not (member item ov))
2007 (setq ov
2008 (if append
2009 (append ov (list item))
2010 (cons item ov)))))
2011 ;; Set back into the slot.
2012 (eieio-oset object slot ov)))
2014 (defun object-remove-from-list (object slot item)
2015 "In OBJECT's SLOT, remove occurrences of ITEM.
2016 Deletion is done with `delete', which deletes by side effect,
2017 and comparisons are done with `equal'.
2018 If SLOT is unbound, do nothing."
2019 (if (not (slot-boundp object slot))
2021 (eieio-oset object slot (delete item (eieio-oref object slot)))))
2023 ;;; EIEIO internal search functions
2025 (defun eieio-slot-originating-class-p (start-class slot)
2026 "Return non-nil if START-CLASS is the first class to define SLOT.
2027 This is for testing if `scoped-class' is the class that defines SLOT
2028 so that we can protect private slots."
2029 (let ((par (eieio-class-parents start-class))
2030 (ret t))
2031 (if (not par)
2033 (while (and par ret)
2034 (if (intern-soft (symbol-name slot)
2035 (eieio--class-symbol-obarray (class-v (car par))))
2036 (setq ret nil))
2037 (setq par (cdr par)))
2038 ret)))
2040 (defun eieio-slot-name-index (class obj slot)
2041 "In CLASS for OBJ find the index of the named SLOT.
2042 The slot is a symbol which is installed in CLASS by the `defclass'
2043 call. OBJ can be nil, but if it is an object, and the slot in question
2044 is protected, access will be allowed if OBJ is a child of the currently
2045 `scoped-class'.
2046 If SLOT is the value created with :initarg instead,
2047 reverse-lookup that name, and recurse with the associated slot value."
2048 ;; Removed checks to outside this call
2049 (let* ((fsym (intern-soft (symbol-name slot)
2050 (eieio--class-symbol-obarray (class-v class))))
2051 (fsi (if (symbolp fsym) (symbol-value fsym) nil)))
2052 (if (integerp fsi)
2053 (cond
2054 ((not (get fsym 'protection))
2055 (+ 3 fsi))
2056 ((and (eq (get fsym 'protection) 'protected)
2057 (bound-and-true-p scoped-class)
2058 (or (child-of-class-p class scoped-class)
2059 (and (eieio-object-p obj)
2060 (child-of-class-p class (eieio-object-class obj)))))
2061 (+ 3 fsi))
2062 ((and (eq (get fsym 'protection) 'private)
2063 (or (and (bound-and-true-p scoped-class)
2064 (eieio-slot-originating-class-p scoped-class slot))
2065 eieio-initializing-object))
2066 (+ 3 fsi))
2067 (t nil))
2068 (let ((fn (eieio-initarg-to-attribute class slot)))
2069 (if fn (eieio-slot-name-index class obj fn) nil)))))
2071 (defun eieio-class-slot-name-index (class slot)
2072 "In CLASS find the index of the named SLOT.
2073 The slot is a symbol which is installed in CLASS by the `defclass'
2074 call. If SLOT is the value created with :initarg instead,
2075 reverse-lookup that name, and recurse with the associated slot value."
2076 ;; This will happen less often, and with fewer slots. Do this the
2077 ;; storage cheap way.
2078 (let* ((a (eieio--class-class-allocation-a (class-v class)))
2079 (l1 (length a))
2080 (af (memq slot a))
2081 (l2 (length af)))
2082 ;; Slot # is length of the total list, minus the remaining list of
2083 ;; the found slot.
2084 (if af (- l1 l2))))
2086 ;;; CLOS generics internal function handling
2088 (defvar eieio-generic-call-methodname nil
2089 "When using `call-next-method', provides a context on how to do it.")
2090 (defvar eieio-generic-call-arglst nil
2091 "When using `call-next-method', provides a context for parameters.")
2092 (defvar eieio-generic-call-key nil
2093 "When using `call-next-method', provides a context for the current key.
2094 Keys are a number representing :before, :primary, and :after methods.")
2095 (defvar eieio-generic-call-next-method-list nil
2096 "When executing a PRIMARY or STATIC method, track the 'next-method'.
2097 During executions, the list is first generated, then as each next method
2098 is called, the next method is popped off the stack.")
2100 (define-obsolete-variable-alias 'eieio-pre-method-execution-hooks
2101 'eieio-pre-method-execution-functions "24.3")
2102 (defvar eieio-pre-method-execution-functions nil
2103 "Abnormal hook run just before an EIEIO method is executed.
2104 The hook function must accept one argument, the list of forms
2105 about to be executed.")
2107 (defun eieio-generic-call (method args)
2108 "Call METHOD with ARGS.
2109 ARGS provides the context on which implementation to use.
2110 This should only be called from a generic function."
2111 ;; We must expand our arguments first as they are always
2112 ;; passed in as quoted symbols
2113 (let ((newargs nil) (mclass nil) (lambdas nil) (tlambdas nil) (keys nil)
2114 (eieio-generic-call-methodname method)
2115 (eieio-generic-call-arglst args)
2116 (firstarg nil)
2117 (primarymethodlist nil))
2118 ;; get a copy
2119 (setq newargs args
2120 firstarg (car newargs))
2121 ;; Is the class passed in autoloaded?
2122 ;; Since class names are also constructors, they can be autoloaded
2123 ;; via the autoload command. Check for this, and load them in.
2124 ;; It's ok if it doesn't turn out to be a class. Probably want that
2125 ;; function loaded anyway.
2126 (if (and (symbolp firstarg)
2127 (fboundp firstarg)
2128 (listp (symbol-function firstarg))
2129 (eq 'autoload (car (symbol-function firstarg))))
2130 (load (nth 1 (symbol-function firstarg))))
2131 ;; Determine the class to use.
2132 (cond ((eieio-object-p firstarg)
2133 (setq mclass (eieio--object-class firstarg)))
2134 ((class-p firstarg)
2135 (setq mclass firstarg))
2137 ;; Make sure the class is a valid class
2138 ;; mclass can be nil (meaning a generic for should be used.
2139 ;; mclass cannot have a value that is not a class, however.
2140 (when (and (not (null mclass)) (not (class-p mclass)))
2141 (error "Cannot dispatch method %S on class %S"
2142 method mclass)
2144 ;; Now create a list in reverse order of all the calls we have
2145 ;; make in order to successfully do this right. Rules:
2146 ;; 1) Only call generics if scoped-class is not defined
2147 ;; This prevents multiple calls in the case of recursion
2148 ;; 2) Only call static if this is a static method.
2149 ;; 3) Only call specifics if the definition allows for them.
2150 ;; 4) Call in order based on :before, :primary, and :after
2151 (when (eieio-object-p firstarg)
2152 ;; Non-static calls do all this stuff.
2154 ;; :after methods
2155 (setq tlambdas
2156 (if mclass
2157 (eieiomt-method-list method method-after mclass)
2158 (list (eieio-generic-form method method-after nil)))
2159 ;;(or (and mclass (eieio-generic-form method method-after mclass))
2160 ;; (eieio-generic-form method method-after nil))
2162 (setq lambdas (append tlambdas lambdas)
2163 keys (append (make-list (length tlambdas) method-after) keys))
2165 ;; :primary methods
2166 (setq tlambdas
2167 (or (and mclass (eieio-generic-form method method-primary mclass))
2168 (eieio-generic-form method method-primary nil)))
2169 (when tlambdas
2170 (setq lambdas (cons tlambdas lambdas)
2171 keys (cons method-primary keys)
2172 primarymethodlist
2173 (eieiomt-method-list method method-primary mclass)))
2175 ;; :before methods
2176 (setq tlambdas
2177 (if mclass
2178 (eieiomt-method-list method method-before mclass)
2179 (list (eieio-generic-form method method-before nil)))
2180 ;;(or (and mclass (eieio-generic-form method method-before mclass))
2181 ;; (eieio-generic-form method method-before nil))
2183 (setq lambdas (append tlambdas lambdas)
2184 keys (append (make-list (length tlambdas) method-before) keys))
2187 (if mclass
2188 ;; For the case of a class,
2189 ;; if there were no methods found, then there could be :static methods.
2190 (when (not lambdas)
2191 (setq tlambdas
2192 (eieio-generic-form method method-static mclass))
2193 (setq lambdas (cons tlambdas lambdas)
2194 keys (cons method-static keys)
2195 primarymethodlist ;; Re-use even with bad name here
2196 (eieiomt-method-list method method-static mclass)))
2197 ;; For the case of no class (ie - mclass == nil) then there may
2198 ;; be a primary method.
2199 (setq tlambdas
2200 (eieio-generic-form method method-primary nil))
2201 (when tlambdas
2202 (setq lambdas (cons tlambdas lambdas)
2203 keys (cons method-primary keys)
2204 primarymethodlist
2205 (eieiomt-method-list method method-primary nil)))
2208 (run-hook-with-args 'eieio-pre-method-execution-functions
2209 primarymethodlist)
2211 ;; Now loop through all occurrences forms which we must execute
2212 ;; (which are happily sorted now) and execute them all!
2213 (let ((rval nil) (lastval nil) (rvalever nil) (found nil))
2214 (while lambdas
2215 (if (car lambdas)
2216 (let* ((scoped-class (cdr (car lambdas)))
2217 (eieio-generic-call-key (car keys))
2218 (has-return-val
2219 (or (= eieio-generic-call-key method-primary)
2220 (= eieio-generic-call-key method-static)))
2221 (eieio-generic-call-next-method-list
2222 ;; Use the cdr, as the first element is the fcn
2223 ;; we are calling right now.
2224 (when has-return-val (cdr primarymethodlist)))
2226 (setq found t)
2227 ;;(setq rval (apply (car (car lambdas)) newargs))
2228 (setq lastval (apply (car (car lambdas)) newargs))
2229 (when has-return-val
2230 (setq rval lastval
2231 rvalever t))
2233 (setq lambdas (cdr lambdas)
2234 keys (cdr keys)))
2235 (if (not found)
2236 (if (eieio-object-p (car args))
2237 (setq rval (apply 'no-applicable-method (car args) method args)
2238 rvalever t)
2239 (signal
2240 'no-method-definition
2241 (list method args))))
2242 ;; Right Here... it could be that lastval is returned when
2243 ;; rvalever is nil. Is that right?
2244 rval)))
2246 (defun eieio-generic-call-primary-only (method args)
2247 "Call METHOD with ARGS for methods with only :PRIMARY implementations.
2248 ARGS provides the context on which implementation to use.
2249 This should only be called from a generic function.
2251 This method is like `eieio-generic-call', but only
2252 implementations in the :PRIMARY slot are queried. After many
2253 years of use, it appears that over 90% of methods in use
2254 have :PRIMARY implementations only. We can therefore optimize
2255 for this common case to improve performance."
2256 ;; We must expand our arguments first as they are always
2257 ;; passed in as quoted symbols
2258 (let ((newargs nil) (mclass nil) (lambdas nil)
2259 (eieio-generic-call-methodname method)
2260 (eieio-generic-call-arglst args)
2261 (firstarg nil)
2262 (primarymethodlist nil)
2264 ;; get a copy
2265 (setq newargs args
2266 firstarg (car newargs))
2268 ;; Determine the class to use.
2269 (cond ((eieio-object-p firstarg)
2270 (setq mclass (eieio--object-class firstarg)))
2271 ((not firstarg)
2272 (error "Method %s called on nil" method))
2273 ((not (eieio-object-p firstarg))
2274 (error "Primary-only method %s called on something not an object" method))
2276 (error "EIEIO Error: Improperly classified method %s as primary only"
2277 method)
2279 ;; Make sure the class is a valid class
2280 ;; mclass can be nil (meaning a generic for should be used.
2281 ;; mclass cannot have a value that is not a class, however.
2282 (when (null mclass)
2283 (error "Cannot dispatch method %S on class %S" method mclass)
2286 ;; :primary methods
2287 (setq lambdas (eieio-generic-form method method-primary mclass))
2288 (setq primarymethodlist ;; Re-use even with bad name here
2289 (eieiomt-method-list method method-primary mclass))
2291 ;; Now loop through all occurrences forms which we must execute
2292 ;; (which are happily sorted now) and execute them all!
2293 (let* ((rval nil) (lastval nil) (rvalever nil)
2294 (scoped-class (cdr lambdas))
2295 (eieio-generic-call-key method-primary)
2296 ;; Use the cdr, as the first element is the fcn
2297 ;; we are calling right now.
2298 (eieio-generic-call-next-method-list (cdr primarymethodlist))
2301 (if (or (not lambdas) (not (car lambdas)))
2303 ;; No methods found for this impl...
2304 (if (eieio-object-p (car args))
2305 (setq rval (apply 'no-applicable-method (car args) method args)
2306 rvalever t)
2307 (signal
2308 'no-method-definition
2309 (list method args)))
2311 ;; Do the regular implementation here.
2313 (run-hook-with-args 'eieio-pre-method-execution-functions
2314 lambdas)
2316 (setq lastval (apply (car lambdas) newargs))
2317 (setq rval lastval
2318 rvalever t)
2321 ;; Right Here... it could be that lastval is returned when
2322 ;; rvalever is nil. Is that right?
2323 rval)))
2325 (defun eieiomt-method-list (method key class)
2326 "Return an alist list of methods lambdas.
2327 METHOD is the method name.
2328 KEY represents either :before, or :after methods.
2329 CLASS is the starting class to search from in the method tree.
2330 If CLASS is nil, then an empty list of methods should be returned."
2331 ;; Note: eieiomt - the MT means MethodTree. See more comments below
2332 ;; for the rest of the eieiomt methods.
2334 ;; Collect lambda expressions stored for the class and its parent
2335 ;; classes.
2336 (let (lambdas)
2337 (dolist (ancestor (eieio-class-precedence-list class))
2338 ;; Lookup the form to use for the PRIMARY object for the next level
2339 (let ((tmpl (eieio-generic-form method key ancestor)))
2340 (when (and tmpl
2341 (or (not lambdas)
2342 ;; This prevents duplicates coming out of the
2343 ;; class method optimizer. Perhaps we should
2344 ;; just not optimize before/afters?
2345 (not (member tmpl lambdas))))
2346 (push tmpl lambdas))))
2348 ;; Return collected lambda. For :after methods, return in current
2349 ;; order (most general class last); Otherwise, reverse order.
2350 (if (eq key method-after)
2351 lambdas
2352 (nreverse lambdas))))
2354 (defun next-method-p ()
2355 "Return non-nil if there is a next method.
2356 Returns a list of lambda expressions which is the `next-method'
2357 order."
2358 eieio-generic-call-next-method-list)
2360 (defun call-next-method (&rest replacement-args)
2361 "Call the superclass method from a subclass method.
2362 The superclass method is specified in the current method list,
2363 and is called the next method.
2365 If REPLACEMENT-ARGS is non-nil, then use them instead of
2366 `eieio-generic-call-arglst'. The generic arg list are the
2367 arguments passed in at the top level.
2369 Use `next-method-p' to find out if there is a next method to call."
2370 (if (not (bound-and-true-p scoped-class))
2371 (error "`call-next-method' not called within a class specific method"))
2372 (if (and (/= eieio-generic-call-key method-primary)
2373 (/= eieio-generic-call-key method-static))
2374 (error "Cannot `call-next-method' except in :primary or :static methods")
2376 (let ((newargs (or replacement-args eieio-generic-call-arglst))
2377 (next (car eieio-generic-call-next-method-list))
2379 (if (or (not next) (not (car next)))
2380 (apply 'no-next-method (car newargs) (cdr newargs))
2381 (let* ((eieio-generic-call-next-method-list
2382 (cdr eieio-generic-call-next-method-list))
2383 (eieio-generic-call-arglst newargs)
2384 (scoped-class (cdr next))
2385 (fcn (car next))
2387 (apply fcn newargs)
2388 ))))
2391 ;; eieio-method-tree : eieiomt-
2393 ;; Stored as eieio-method-tree in property list of a generic method
2395 ;; (eieio-method-tree . [BEFORE PRIMARY AFTER
2396 ;; genericBEFORE genericPRIMARY genericAFTER])
2397 ;; and
2398 ;; (eieio-method-obarray . [BEFORE PRIMARY AFTER
2399 ;; genericBEFORE genericPRIMARY genericAFTER])
2400 ;; where the association is a vector.
2401 ;; (aref 0 -- all static methods.
2402 ;; (aref 1 -- all methods classified as :before
2403 ;; (aref 2 -- all methods classified as :primary
2404 ;; (aref 3 -- all methods classified as :after
2405 ;; (aref 4 -- a generic classified as :before
2406 ;; (aref 5 -- a generic classified as :primary
2407 ;; (aref 6 -- a generic classified as :after
2409 (defvar eieiomt-optimizing-obarray nil
2410 "While mapping atoms, this contain the obarray being optimized.")
2412 (defun eieiomt-install (method-name)
2413 "Install the method tree, and obarray onto METHOD-NAME.
2414 Do not do the work if they already exist."
2415 (let ((emtv (get method-name 'eieio-method-tree))
2416 (emto (get method-name 'eieio-method-obarray)))
2417 (if (or (not emtv) (not emto))
2418 (progn
2419 (setq emtv (put method-name 'eieio-method-tree
2420 (make-vector method-num-slots nil))
2421 emto (put method-name 'eieio-method-obarray
2422 (make-vector method-num-slots nil)))
2423 (aset emto 0 (make-vector 11 0))
2424 (aset emto 1 (make-vector 11 0))
2425 (aset emto 2 (make-vector 41 0))
2426 (aset emto 3 (make-vector 11 0))
2427 ))))
2429 (defun eieiomt-add (method-name method key class)
2430 "Add to METHOD-NAME the forms METHOD in a call position KEY for CLASS.
2431 METHOD-NAME is the name created by a call to `defgeneric'.
2432 METHOD are the forms for a given implementation.
2433 KEY is an integer (see comment in eieio.el near this function) which
2434 is associated with the :static :before :primary and :after tags.
2435 It also indicates if CLASS is defined or not.
2436 CLASS is the class this method is associated with."
2437 (if (or (> key method-num-slots) (< key 0))
2438 (error "eieiomt-add: method key error!"))
2439 (let ((emtv (get method-name 'eieio-method-tree))
2440 (emto (get method-name 'eieio-method-obarray)))
2441 ;; Make sure the method tables are available.
2442 (if (or (not emtv) (not emto))
2443 (error "Programmer error: eieiomt-add"))
2444 ;; only add new cells on if it doesn't already exist!
2445 (if (assq class (aref emtv key))
2446 (setcdr (assq class (aref emtv key)) method)
2447 (aset emtv key (cons (cons class method) (aref emtv key))))
2448 ;; Add function definition into newly created symbol, and store
2449 ;; said symbol in the correct obarray, otherwise use the
2450 ;; other array to keep this stuff
2451 (if (< key method-num-lists)
2452 (let ((nsym (intern (symbol-name class) (aref emto key))))
2453 (fset nsym method)))
2454 ;; Save the defmethod file location in a symbol property.
2455 (let ((fname (if load-in-progress
2456 load-file-name
2457 buffer-file-name))
2458 loc)
2459 (when fname
2460 (when (string-match "\\.elc$" fname)
2461 (setq fname (substring fname 0 (1- (length fname)))))
2462 (setq loc (get method-name 'method-locations))
2463 (add-to-list 'loc
2464 (list class fname))
2465 (put method-name 'method-locations loc)))
2466 ;; Now optimize the entire obarray
2467 (if (< key method-num-lists)
2468 (let ((eieiomt-optimizing-obarray (aref emto key)))
2469 ;; @todo - Is this overkill? Should we just clear the symbol?
2470 (mapatoms 'eieiomt-sym-optimize eieiomt-optimizing-obarray)))
2473 (defun eieiomt-next (class)
2474 "Return the next parent class for CLASS.
2475 If CLASS is a superclass, return variable `eieio-default-superclass'.
2476 If CLASS is variable `eieio-default-superclass' then return nil.
2477 This is different from function `class-parent' as class parent returns
2478 nil for superclasses. This function performs no type checking!"
2479 ;; No type-checking because all calls are made from functions which
2480 ;; are safe and do checking for us.
2481 (or (eieio-class-parents-fast class)
2482 (if (eq class 'eieio-default-superclass)
2484 '(eieio-default-superclass))))
2486 (defun eieiomt-sym-optimize (s)
2487 "Find the next class above S which has a function body for the optimizer."
2488 ;; Set the value to nil in case there is no nearest cell.
2489 (set s nil)
2490 ;; Find the nearest cell that has a function body. If we find one,
2491 ;; we replace the nil from above.
2492 (let ((external-symbol (intern-soft (symbol-name s))))
2493 (catch 'done
2494 (dolist (ancestor (rest (eieio-class-precedence-list external-symbol)))
2495 (let ((ov (intern-soft (symbol-name ancestor)
2496 eieiomt-optimizing-obarray)))
2497 (when (fboundp ov)
2498 (set s ov) ;; store ov as our next symbol
2499 (throw 'done ancestor)))))))
2501 (defun eieio-generic-form (method key class)
2502 "Return the lambda form belonging to METHOD using KEY based upon CLASS.
2503 If CLASS is not a class then use `generic' instead. If class has
2504 no form, but has a parent class, then trace to that parent class.
2505 The first time a form is requested from a symbol, an optimized path
2506 is memorized for faster future use."
2507 (let ((emto (aref (get method 'eieio-method-obarray)
2508 (if class key (eieio-specialized-key-to-generic-key key)))))
2509 (if (class-p class)
2510 ;; 1) find our symbol
2511 (let ((cs (intern-soft (symbol-name class) emto)))
2512 (if (not cs)
2513 ;; 2) If there isn't one, then make one.
2514 ;; This can be slow since it only occurs once
2515 (progn
2516 (setq cs (intern (symbol-name class) emto))
2517 ;; 2.1) Cache its nearest neighbor with a quick optimize
2518 ;; which should only occur once for this call ever
2519 (let ((eieiomt-optimizing-obarray emto))
2520 (eieiomt-sym-optimize cs))))
2521 ;; 3) If it's bound return this one.
2522 (if (fboundp cs)
2523 (cons cs (eieio--class-symbol (class-v class)))
2524 ;; 4) If it's not bound then this variable knows something
2525 (if (symbol-value cs)
2526 (progn
2527 ;; 4.1) This symbol holds the next class in its value
2528 (setq class (symbol-value cs)
2529 cs (intern-soft (symbol-name class) emto))
2530 ;; 4.2) The optimizer should always have chosen a
2531 ;; function-symbol
2532 ;;(if (fboundp cs)
2533 (cons cs (eieio--class-symbol (class-v (intern (symbol-name class)))))
2534 ;;(error "EIEIO optimizer: erratic data loss!"))
2536 ;; There never will be a funcall...
2537 nil)))
2538 ;; for a generic call, what is a list, is the function body we want.
2539 (let ((emtl (aref (get method 'eieio-method-tree)
2540 (if class key (eieio-specialized-key-to-generic-key key)))))
2541 (if emtl
2542 ;; The car of EMTL is supposed to be a class, which in this
2543 ;; case is nil, so skip it.
2544 (cons (cdr (car emtl)) nil)
2545 nil)))))
2548 ;; Way to assign slots based on a list. Used for constructors, or
2549 ;; even resetting an object at run-time
2551 (defun eieio-set-defaults (obj &optional set-all)
2552 "Take object OBJ, and reset all slots to their defaults.
2553 If SET-ALL is non-nil, then when a default is nil, that value is
2554 reset. If SET-ALL is nil, the slots are only reset if the default is
2555 not nil."
2556 (let ((scoped-class (eieio--object-class obj))
2557 (eieio-initializing-object t)
2558 (pub (eieio--class-public-a (class-v (eieio--object-class obj)))))
2559 (while pub
2560 (let ((df (eieio-oref-default obj (car pub))))
2561 (if (or df set-all)
2562 (eieio-oset obj (car pub) df)))
2563 (setq pub (cdr pub)))))
2565 (defun eieio-initarg-to-attribute (class initarg)
2566 "For CLASS, convert INITARG to the actual attribute name.
2567 If there is no translation, pass it in directly (so we can cheat if
2568 need be... May remove that later...)"
2569 (let ((tuple (assoc initarg (eieio--class-initarg-tuples (class-v class)))))
2570 (if tuple
2571 (cdr tuple)
2572 nil)))
2574 (defun eieio-attribute-to-initarg (class attribute)
2575 "In CLASS, convert the ATTRIBUTE into the corresponding init argument tag.
2576 This is usually a symbol that starts with `:'."
2577 (let ((tuple (rassoc attribute (eieio--class-initarg-tuples (class-v class)))))
2578 (if tuple
2579 (car tuple)
2580 nil)))
2583 ;;; Here are some special types of errors
2585 (intern "no-method-definition")
2586 (put 'no-method-definition 'error-conditions '(no-method-definition error))
2587 (put 'no-method-definition 'error-message "No method definition")
2589 (intern "no-next-method")
2590 (put 'no-next-method 'error-conditions '(no-next-method error))
2591 (put 'no-next-method 'error-message "No next method")
2593 (intern "invalid-slot-name")
2594 (put 'invalid-slot-name 'error-conditions '(invalid-slot-name error))
2595 (put 'invalid-slot-name 'error-message "Invalid slot name")
2597 (intern "invalid-slot-type")
2598 (put 'invalid-slot-type 'error-conditions '(invalid-slot-type error nil))
2599 (put 'invalid-slot-type 'error-message "Invalid slot type")
2601 (intern "unbound-slot")
2602 (put 'unbound-slot 'error-conditions '(unbound-slot error nil))
2603 (put 'unbound-slot 'error-message "Unbound slot")
2605 (intern "inconsistent-class-hierarchy")
2606 (put 'inconsistent-class-hierarchy 'error-conditions
2607 '(inconsistent-class-hierarchy error nil))
2608 (put 'inconsistent-class-hierarchy 'error-message "Inconsistent class hierarchy")
2610 ;;; Here are some CLOS items that need the CL package
2613 (defsetf eieio-oref eieio-oset)
2615 (if (eval-when-compile (fboundp 'gv-define-expander))
2616 ;; Not needed for Emacs>=24.3 since gv.el's setf expands macros and
2617 ;; follows aliases.
2619 (defsetf slot-value eieio-oset)
2621 ;; The below setf method was written by Arnd Kohrs <kohrs@acm.org>
2622 (define-setf-method oref (obj slot)
2623 (with-no-warnings
2624 (require 'cl)
2625 (let ((obj-temp (gensym))
2626 (slot-temp (gensym))
2627 (store-temp (gensym)))
2628 (list (list obj-temp slot-temp)
2629 (list obj `(quote ,slot))
2630 (list store-temp)
2631 (list 'set-slot-value obj-temp slot-temp
2632 store-temp)
2633 (list 'slot-value obj-temp slot-temp))))))
2637 ;; We want all objects created by EIEIO to have some default set of
2638 ;; behaviors so we can create object utilities, and allow various
2639 ;; types of error checking. To do this, create the default EIEIO
2640 ;; class, and when no parent class is specified, use this as the
2641 ;; default. (But don't store it in the other classes as the default,
2642 ;; allowing for transparent support.)
2645 (defclass eieio-default-superclass nil
2647 "Default parent class for classes with no specified parent class.
2648 Its slots are automatically adopted by classes with no specified parents.
2649 This class is not stored in the `parent' slot of a class vector."
2650 :abstract t)
2652 (defalias 'standard-class 'eieio-default-superclass)
2654 (defgeneric constructor (class newname &rest slots)
2655 "Default constructor for CLASS `eieio-default-superclass'.")
2657 (defmethod constructor :static
2658 ((class eieio-default-superclass) newname &rest slots)
2659 "Default constructor for CLASS `eieio-default-superclass'.
2660 NEWNAME is the name to be given to the constructed object.
2661 SLOTS are the initialization slots used by `shared-initialize'.
2662 This static method is called when an object is constructed.
2663 It allocates the vector used to represent an EIEIO object, and then
2664 calls `shared-initialize' on that object."
2665 (let* ((new-object (copy-sequence (eieio--class-default-object-cache (class-v class)))))
2666 ;; Update the name for the newly created object.
2667 (setf (eieio--object-name new-object) newname)
2668 ;; Call the initialize method on the new object with the slots
2669 ;; that were passed down to us.
2670 (initialize-instance new-object slots)
2671 ;; Return the created object.
2672 new-object))
2674 (defgeneric shared-initialize (obj slots)
2675 "Set slots of OBJ with SLOTS which is a list of name/value pairs.
2676 Called from the constructor routine.")
2678 (defmethod shared-initialize ((obj eieio-default-superclass) slots)
2679 "Set slots of OBJ with SLOTS which is a list of name/value pairs.
2680 Called from the constructor routine."
2681 (let ((scoped-class (eieio--object-class obj)))
2682 (while slots
2683 (let ((rn (eieio-initarg-to-attribute (eieio--object-class obj)
2684 (car slots))))
2685 (if (not rn)
2686 (slot-missing obj (car slots) 'oset (car (cdr slots)))
2687 (eieio-oset obj rn (car (cdr slots)))))
2688 (setq slots (cdr (cdr slots))))))
2690 (defgeneric initialize-instance (this &optional slots)
2691 "Construct the new object THIS based on SLOTS.")
2693 (defmethod initialize-instance ((this eieio-default-superclass)
2694 &optional slots)
2695 "Construct the new object THIS based on SLOTS.
2696 SLOTS is a tagged list where odd numbered elements are tags, and
2697 even numbered elements are the values to store in the tagged slot.
2698 If you overload the `initialize-instance', there you will need to
2699 call `shared-initialize' yourself, or you can call `call-next-method'
2700 to have this constructor called automatically. If these steps are
2701 not taken, then new objects of your class will not have their values
2702 dynamically set from SLOTS."
2703 ;; First, see if any of our defaults are `lambda', and
2704 ;; re-evaluate them and apply the value to our slots.
2705 (let* ((scoped-class (class-v (eieio--object-class this)))
2706 (slot (eieio--class-public-a scoped-class))
2707 (defaults (eieio--class-public-d scoped-class)))
2708 (while slot
2709 ;; For each slot, see if we need to evaluate it.
2711 ;; Paul Landes said in an email:
2712 ;; > CL evaluates it if it can, and otherwise, leaves it as
2713 ;; > the quoted thing as you already have. This is by the
2714 ;; > Sonya E. Keene book and other things I've look at on the
2715 ;; > web.
2716 (let ((dflt (eieio-default-eval-maybe (car defaults))))
2717 (when (not (eq dflt (car defaults)))
2718 (eieio-oset this (car slot) dflt) ))
2719 ;; Next.
2720 (setq slot (cdr slot)
2721 defaults (cdr defaults))))
2722 ;; Shared initialize will parse our slots for us.
2723 (shared-initialize this slots))
2725 (defgeneric slot-missing (object slot-name operation &optional new-value)
2726 "Method invoked when an attempt to access a slot in OBJECT fails.")
2728 (defmethod slot-missing ((object eieio-default-superclass) slot-name
2729 operation &optional new-value)
2730 "Method invoked when an attempt to access a slot in OBJECT fails.
2731 SLOT-NAME is the name of the failed slot, OPERATION is the type of access
2732 that was requested, and optional NEW-VALUE is the value that was desired
2733 to be set.
2735 This method is called from `oref', `oset', and other functions which
2736 directly reference slots in EIEIO objects."
2737 (signal 'invalid-slot-name (list (eieio-object-name object)
2738 slot-name)))
2740 (defgeneric slot-unbound (object class slot-name fn)
2741 "Slot unbound is invoked during an attempt to reference an unbound slot.")
2743 (defmethod slot-unbound ((object eieio-default-superclass)
2744 class slot-name fn)
2745 "Slot unbound is invoked during an attempt to reference an unbound slot.
2746 OBJECT is the instance of the object being reference. CLASS is the
2747 class of OBJECT, and SLOT-NAME is the offending slot. This function
2748 throws the signal `unbound-slot'. You can overload this function and
2749 return the value to use in place of the unbound value.
2750 Argument FN is the function signaling this error.
2751 Use `slot-boundp' to determine if a slot is bound or not.
2753 In CLOS, the argument list is (CLASS OBJECT SLOT-NAME), but
2754 EIEIO can only dispatch on the first argument, so the first two are swapped."
2755 (signal 'unbound-slot (list (eieio-class-name class) (eieio-object-name object)
2756 slot-name fn)))
2758 (defgeneric no-applicable-method (object method &rest args)
2759 "Called if there are no implementations for OBJECT in METHOD.")
2761 (defmethod no-applicable-method ((object eieio-default-superclass)
2762 method &rest args)
2763 "Called if there are no implementations for OBJECT in METHOD.
2764 OBJECT is the object which has no method implementation.
2765 ARGS are the arguments that were passed to METHOD.
2767 Implement this for a class to block this signal. The return
2768 value becomes the return value of the original method call."
2769 (signal 'no-method-definition (list method (eieio-object-name object)))
2772 (defgeneric no-next-method (object &rest args)
2773 "Called from `call-next-method' when no additional methods are available.")
2775 (defmethod no-next-method ((object eieio-default-superclass)
2776 &rest args)
2777 "Called from `call-next-method' when no additional methods are available.
2778 OBJECT is othe object being called on `call-next-method'.
2779 ARGS are the arguments it is called by.
2780 This method signals `no-next-method' by default. Override this
2781 method to not throw an error, and its return value becomes the
2782 return value of `call-next-method'."
2783 (signal 'no-next-method (list (eieio-object-name object) args))
2786 (defgeneric clone (obj &rest params)
2787 "Make a copy of OBJ, and then supply PARAMS.
2788 PARAMS is a parameter list of the same form used by `initialize-instance'.
2790 When overloading `clone', be sure to call `call-next-method'
2791 first and modify the returned object.")
2793 (defmethod clone ((obj eieio-default-superclass) &rest params)
2794 "Make a copy of OBJ, and then apply PARAMS."
2795 (let ((nobj (copy-sequence obj))
2796 (nm (eieio--object-name obj))
2797 (passname (and params (stringp (car params))))
2798 (num 1))
2799 (if params (shared-initialize nobj (if passname (cdr params) params)))
2800 (if (not passname)
2801 (save-match-data
2802 (if (string-match "-\\([0-9]+\\)" nm)
2803 (setq num (1+ (string-to-number (match-string 1 nm)))
2804 nm (substring nm 0 (match-beginning 0))))
2805 (setf (eieio--object-name nobj) (concat nm "-" (int-to-string num))))
2806 (setf (eieio--object-name nobj) (car params)))
2807 nobj))
2809 (defgeneric destructor (this &rest params)
2810 "Destructor for cleaning up any dynamic links to our object.")
2812 (defmethod destructor ((this eieio-default-superclass) &rest params)
2813 "Destructor for cleaning up any dynamic links to our object.
2814 Argument THIS is the object being destroyed. PARAMS are additional
2815 ignored parameters."
2816 ;; No cleanup... yet.
2819 (defgeneric object-print (this &rest strings)
2820 "Pretty printer for object THIS. Call function `object-name' with STRINGS.
2822 It is sometimes useful to put a summary of the object into the
2823 default #<notation> string when using EIEIO browsing tools.
2824 Implement this method to customize the summary.")
2826 (defmethod object-print ((this eieio-default-superclass) &rest strings)
2827 "Pretty printer for object THIS. Call function `object-name' with STRINGS.
2828 The default method for printing object THIS is to use the
2829 function `object-name'.
2831 It is sometimes useful to put a summary of the object into the
2832 default #<notation> string when using EIEIO browsing tools.
2834 Implement this function and specify STRINGS in a call to
2835 `call-next-method' to provide additional summary information.
2836 When passing in extra strings from child classes, always remember
2837 to prepend a space."
2838 (eieio-object-name this (apply 'concat strings)))
2840 (defvar eieio-print-depth 0
2841 "When printing, keep track of the current indentation depth.")
2843 (defgeneric object-write (this &optional comment)
2844 "Write out object THIS to the current stream.
2845 Optional COMMENT will add comments to the beginning of the output.")
2847 (defmethod object-write ((this eieio-default-superclass) &optional comment)
2848 "Write object THIS out to the current stream.
2849 This writes out the vector version of this object. Complex and recursive
2850 object are discouraged from being written.
2851 If optional COMMENT is non-nil, include comments when outputting
2852 this object."
2853 (when comment
2854 (princ ";; Object ")
2855 (princ (eieio-object-name-string this))
2856 (princ "\n")
2857 (princ comment)
2858 (princ "\n"))
2859 (let* ((cl (eieio-object-class this))
2860 (cv (class-v cl)))
2861 ;; Now output readable lisp to recreate this object
2862 ;; It should look like this:
2863 ;; (<constructor> <name> <slot> <slot> ... )
2864 ;; Each slot's slot is writen using its :writer.
2865 (princ (make-string (* eieio-print-depth 2) ? ))
2866 (princ "(")
2867 (princ (symbol-name (class-constructor (eieio-object-class this))))
2868 (princ " ")
2869 (prin1 (eieio-object-name-string this))
2870 (princ "\n")
2871 ;; Loop over all the public slots
2872 (let ((publa (eieio--class-public-a cv))
2873 (publd (eieio--class-public-d cv))
2874 (publp (eieio--class-public-printer cv))
2875 (eieio-print-depth (1+ eieio-print-depth)))
2876 (while publa
2877 (when (slot-boundp this (car publa))
2878 (let ((i (class-slot-initarg cl (car publa)))
2879 (v (eieio-oref this (car publa)))
2881 (unless (or (not i) (equal v (car publd)))
2882 (unless (bolp)
2883 (princ "\n"))
2884 (princ (make-string (* eieio-print-depth 2) ? ))
2885 (princ (symbol-name i))
2886 (if (car publp)
2887 ;; Use our public printer
2888 (progn
2889 (princ " ")
2890 (funcall (car publp) v))
2891 ;; Use our generic override prin1 function.
2892 (princ (if (or (eieio-object-p v)
2893 (eieio-object-p (car-safe v)))
2894 "\n" " "))
2895 (eieio-override-prin1 v)))))
2896 (setq publa (cdr publa) publd (cdr publd)
2897 publp (cdr publp))))
2898 (princ ")")
2899 (when (= eieio-print-depth 0)
2900 (princ "\n"))))
2902 (defun eieio-override-prin1 (thing)
2903 "Perform a `prin1' on THING taking advantage of object knowledge."
2904 (cond ((eieio-object-p thing)
2905 (object-write thing))
2906 ((consp thing)
2907 (eieio-list-prin1 thing))
2908 ((class-p thing)
2909 (princ (eieio-class-name thing)))
2910 ((or (keywordp thing) (booleanp thing))
2911 (prin1 thing))
2912 ((symbolp thing)
2913 (princ (concat "'" (symbol-name thing))))
2914 (t (prin1 thing))))
2916 (defun eieio-list-prin1 (list)
2917 "Display LIST where list may contain objects."
2918 (if (not (eieio-object-p (car list)))
2919 (progn
2920 (princ "'")
2921 (prin1 list))
2922 (princ (make-string (* eieio-print-depth 2) ? ))
2923 (princ "(list")
2924 (let ((eieio-print-depth (1+ eieio-print-depth)))
2925 (while list
2926 (princ "\n")
2927 (if (eieio-object-p (car list))
2928 (object-write (car list))
2929 (princ (make-string (* eieio-print-depth 2) ? ))
2930 (eieio-override-prin1 (car list)))
2931 (setq list (cdr list))))
2932 (princ ")")))
2935 ;;; Unimplemented functions from CLOS
2937 (defun change-class (obj class)
2938 "Change the class of OBJ to type CLASS.
2939 This may create or delete slots, but does not affect the return value
2940 of `eq'."
2941 (error "EIEIO: `change-class' is unimplemented"))
2945 ;;; Obsolete backward compatibility functions.
2946 ;; Needed to run byte-code compiled with the EIEIO of Emacs-23.
2948 (defun eieio-defmethod (method args)
2949 "Obsolete work part of an old version of the `defmethod' macro."
2950 (let ((key nil) (body nil) (firstarg nil) (argfix nil) (argclass nil) loopa)
2951 ;; find optional keys
2952 (setq key
2953 (cond ((memq (car args) '(:BEFORE :before))
2954 (setq args (cdr args))
2955 method-before)
2956 ((memq (car args) '(:AFTER :after))
2957 (setq args (cdr args))
2958 method-after)
2959 ((memq (car args) '(:STATIC :static))
2960 (setq args (cdr args))
2961 method-static)
2962 ((memq (car args) '(:PRIMARY :primary))
2963 (setq args (cdr args))
2964 method-primary)
2965 ;; Primary key.
2966 (t method-primary)))
2967 ;; Get body, and fix contents of args to be the arguments of the fn.
2968 (setq body (cdr args)
2969 args (car args))
2970 (setq loopa args)
2971 ;; Create a fixed version of the arguments.
2972 (while loopa
2973 (setq argfix (cons (if (listp (car loopa)) (car (car loopa)) (car loopa))
2974 argfix))
2975 (setq loopa (cdr loopa)))
2976 ;; Make sure there is a generic.
2977 (eieio-defgeneric
2978 method
2979 (if (stringp (car body))
2980 (car body) (format "Generically created method `%s'." method)))
2981 ;; create symbol for property to bind to. If the first arg is of
2982 ;; the form (varname vartype) and `vartype' is a class, then
2983 ;; that class will be the type symbol. If not, then it will fall
2984 ;; under the type `primary' which is a non-specific calling of the
2985 ;; function.
2986 (setq firstarg (car args))
2987 (if (listp firstarg)
2988 (progn
2989 (setq argclass (nth 1 firstarg))
2990 (if (not (class-p argclass))
2991 (error "Unknown class type %s in method parameters"
2992 (nth 1 firstarg))))
2993 ;; Generics are higher.
2994 (setq key (eieio-specialized-key-to-generic-key key)))
2995 ;; Put this lambda into the symbol so we can find it.
2996 (if (byte-code-function-p (car-safe body))
2997 (eieiomt-add method (car-safe body) key argclass)
2998 (eieiomt-add method (append (list 'lambda (reverse argfix)) body)
2999 key argclass))
3002 (when eieio-optimize-primary-methods-flag
3003 ;; Optimizing step:
3005 ;; If this method, after this setup, only has primary methods, then
3006 ;; we can setup the generic that way.
3007 (if (generic-primary-only-p method)
3008 ;; If there is only one primary method, then we can go one more
3009 ;; optimization step.
3010 (if (generic-primary-only-one-p method)
3011 (eieio-defgeneric-reset-generic-form-primary-only-one method)
3012 (eieio-defgeneric-reset-generic-form-primary-only method))
3013 (eieio-defgeneric-reset-generic-form method)))
3015 method)
3016 (make-obsolete 'eieio-defmethod 'eieio--defmethod "24.1")
3018 (defun eieio-defgeneric (method doc-string)
3019 "Obsolete work part of an old version of the `defgeneric' macro."
3020 (if (and (fboundp method) (not (generic-p method))
3021 (or (byte-code-function-p (symbol-function method))
3022 (not (eq 'autoload (car (symbol-function method)))))
3024 (error "You cannot create a generic/method over an existing symbol: %s"
3025 method))
3026 ;; Don't do this over and over.
3027 (unless (fboundp 'method)
3028 ;; This defun tells emacs where the first definition of this
3029 ;; method is defined.
3030 `(defun ,method nil)
3031 ;; Make sure the method tables are installed.
3032 (eieiomt-install method)
3033 ;; Apply the actual body of this function.
3034 (fset method (eieio-defgeneric-form method doc-string))
3035 ;; Return the method
3036 'method))
3037 (make-obsolete 'eieio-defgeneric nil "24.1")
3039 ;;; Interfacing with edebug
3041 (defun eieio-edebug-prin1-to-string (object &optional noescape)
3042 "Display EIEIO OBJECT in fancy format.
3043 Overrides the edebug default.
3044 Optional argument NOESCAPE is passed to `prin1-to-string' when appropriate."
3045 (cond ((class-p object) (eieio-class-name object))
3046 ((eieio-object-p object) (object-print object))
3047 ((and (listp object) (or (class-p (car object))
3048 (eieio-object-p (car object))))
3049 (concat "(" (mapconcat 'eieio-edebug-prin1-to-string object " ") ")"))
3050 (t (prin1-to-string object noescape))))
3052 (add-hook 'edebug-setup-hook
3053 (lambda ()
3054 (def-edebug-spec defmethod
3055 (&define ; this means we are defining something
3056 [&or name ("setf" :name setf name)]
3057 ;; ^^ This is the methods symbol
3058 [ &optional symbolp ] ; this is key :before etc
3059 list ; arguments
3060 [ &optional stringp ] ; documentation string
3061 def-body ; part to be debugged
3063 ;; The rest of the macros
3064 (def-edebug-spec oref (form quote))
3065 (def-edebug-spec oref-default (form quote))
3066 (def-edebug-spec oset (form quote form))
3067 (def-edebug-spec oset-default (form quote form))
3068 (def-edebug-spec class-v form)
3069 (def-edebug-spec class-p form)
3070 (def-edebug-spec eieio-object-p form)
3071 (def-edebug-spec class-constructor form)
3072 (def-edebug-spec generic-p form)
3073 (def-edebug-spec with-slots (list list def-body))
3074 ;; I suspect this isn't the best way to do this, but when
3075 ;; cust-print was used on my system all my objects
3076 ;; appeared as "#1 =" which was not useful. This allows
3077 ;; edebug to print my objects in the nice way they were
3078 ;; meant to with `object-print' and `class-name'
3079 ;; (defalias 'edebug-prin1-to-string 'eieio-edebug-prin1-to-string)
3083 ;;; Autoloading some external symbols, and hooking into the help system
3087 ;;; Start of automatically extracted autoloads.
3089 ;;;### (autoloads (customize-object) "eieio-custom" "eieio-custom.el"
3090 ;;;;;; "928623502e8bf40454822355388542b5")
3091 ;;; Generated autoloads from eieio-custom.el
3093 (autoload 'customize-object "eieio-custom" "\
3094 Customize OBJ in a custom buffer.
3095 Optional argument GROUP is the sub-group of slots to display.
3097 \(fn OBJ &optional GROUP)" nil nil)
3099 ;;;***
3101 ;;;### (autoloads (eieio-help-mode-augmentation-maybee eieio-describe-generic
3102 ;;;;;; eieio-describe-constructor eieio-describe-class eieio-browse)
3103 ;;;;;; "eieio-opt" "eieio-opt.el" "d808328f9c0156ecbd412d77ba8c569e")
3104 ;;; Generated autoloads from eieio-opt.el
3106 (autoload 'eieio-browse "eieio-opt" "\
3107 Create an object browser window to show all objects.
3108 If optional ROOT-CLASS, then start with that, otherwise start with
3109 variable `eieio-default-superclass'.
3111 \(fn &optional ROOT-CLASS)" t nil)
3112 (defalias 'describe-class 'eieio-describe-class)
3114 (autoload 'eieio-describe-class "eieio-opt" "\
3115 Describe a CLASS defined by a string or symbol.
3116 If CLASS is actually an object, then also display current values of that object.
3117 Optional HEADERFCN should be called to insert a few bits of info first.
3119 \(fn CLASS &optional HEADERFCN)" t nil)
3121 (autoload 'eieio-describe-constructor "eieio-opt" "\
3122 Describe the constructor function FCN.
3123 Uses `eieio-describe-class' to describe the class being constructed.
3125 \(fn FCN)" t nil)
3126 (defalias 'describe-generic 'eieio-describe-generic)
3128 (autoload 'eieio-describe-generic "eieio-opt" "\
3129 Describe the generic function GENERIC.
3130 Also extracts information about all methods specific to this generic.
3132 \(fn GENERIC)" t nil)
3134 (autoload 'eieio-help-mode-augmentation-maybee "eieio-opt" "\
3135 For buffers thrown into help mode, augment for EIEIO.
3136 Arguments UNUSED are not used.
3138 \(fn &rest UNUSED)" nil nil)
3140 ;;;***
3142 ;;; End of automatically extracted autoloads.
3144 (provide 'eieio)
3146 ;;; eieio ends here