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