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