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