Fix bug #8487 with invisible text at EOB under bidi.
[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 "Define CNAME as a new subclass of SUPERCLASSES.
424 SLOTS are the slots residing in that class definition, and options or
425 documentation OPTIONS-AND-DOC is the toplevel documentation for this class.
426 See `defclass' for more information."
427 ;; Run our eieio-hook each time, and clear it when we are done.
428 ;; This way people can add hooks safely if they want to modify eieio
429 ;; or add definitions when eieio is loaded or something like that.
430 (run-hooks 'eieio-hook)
431 (setq eieio-hook nil)
432
433 (if (not (symbolp cname)) (signal 'wrong-type-argument '(symbolp cname)))
434 (if (not (listp superclasses)) (signal 'wrong-type-argument '(listp superclasses)))
435
436 (let* ((pname (if superclasses superclasses nil))
437 (newc (make-vector class-num-slots nil))
438 (oldc (when (class-p cname) (class-v cname)))
439 (groups nil) ;; list of groups id'd from slots
440 (options nil)
441 (clearparent nil))
442
443 (aset newc 0 'defclass)
444 (aset newc class-symbol cname)
445
446 ;; If this class already existed, and we are updating its structure,
447 ;; make sure we keep the old child list. This can cause bugs, but
448 ;; if no new slots are created, it also saves time, and prevents
449 ;; method table breakage, particularly when the users is only
450 ;; byte compiling an EIEIO file.
451 (if oldc
452 (aset newc class-children (aref oldc class-children))
453 ;; If the old class did not exist, but did exist in the autoload map, then adopt those children.
454 ;; This is like the above, but deals with autoloads nicely.
455 (let ((sym (intern-soft (symbol-name cname) eieio-defclass-autoload-map)))
456 (when sym
457 (condition-case nil
458 (aset newc class-children (symbol-value sym))
459 (error nil))
460 (unintern (symbol-name cname) eieio-defclass-autoload-map)
461 ))
462 )
463
464 (cond ((and (stringp (car options-and-doc))
465 (/= 1 (% (length options-and-doc) 2)))
466 (error "Too many arguments to `defclass'"))
467 ((and (symbolp (car options-and-doc))
468 (/= 0 (% (length options-and-doc) 2)))
469 (error "Too many arguments to `defclass'"))
470 )
471
472 (setq options
473 (if (stringp (car options-and-doc))
474 (cons :documentation options-and-doc)
475 options-and-doc))
476
477 (if pname
478 (progn
479 (while pname
480 (if (and (car pname) (symbolp (car pname)))
481 (if (not (class-p (car pname)))
482 ;; bad class
483 (error "Given parent class %s is not a class" (car pname))
484 ;; good parent class...
485 ;; save new child in parent
486 (when (not (member cname (aref (class-v (car pname)) class-children)))
487 (aset (class-v (car pname)) class-children
488 (cons cname (aref (class-v (car pname)) class-children))))
489 ;; Get custom groups, and store them into our local copy.
490 (mapc (lambda (g) (add-to-list 'groups g))
491 (class-option (car pname) :custom-groups))
492 ;; save parent in child
493 (aset newc class-parent (cons (car pname) (aref newc class-parent))))
494 (error "Invalid parent class %s" pname))
495 (setq pname (cdr pname)))
496 ;; Reverse the list of our parents so that they are prioritized in
497 ;; the same order as specified in the code.
498 (aset newc class-parent (nreverse (aref newc class-parent))) )
499 ;; If there is nothing to loop over, then inherit from the
500 ;; default superclass.
501 (unless (eq cname 'eieio-default-superclass)
502 ;; adopt the default parent here, but clear it later...
503 (setq clearparent t)
504 ;; save new child in parent
505 (if (not (member cname (aref (class-v 'eieio-default-superclass) class-children)))
506 (aset (class-v 'eieio-default-superclass) class-children
507 (cons cname (aref (class-v 'eieio-default-superclass) class-children))))
508 ;; save parent in child
509 (aset newc class-parent (list eieio-default-superclass))))
510
511 ;; turn this into a useable self-pointing symbol
512 (set cname cname)
513
514 ;; These two tests must be created right away so we can have self-
515 ;; referencing classes. ei, a class whose slot can contain only
516 ;; pointers to itself.
517
518 ;; Create the test function
519 (let ((csym (intern (concat (symbol-name cname) "-p"))))
520 (fset csym
521 (list 'lambda (list 'obj)
522 (format "Test OBJ to see if it an object of type %s" cname)
523 (list 'and '(eieio-object-p obj)
524 (list 'same-class-p 'obj cname)))))
525
526 ;; Make sure the method invocation order is a valid value.
527 (let ((io (class-option-assoc options :method-invocation-order)))
528 (when (and io (not (member io '(:depth-first :breadth-first :c3))))
529 (error "Method invocation order %s is not allowed" io)
530 ))
531
532 ;; Create a handy child test too
533 (let ((csym (intern (concat (symbol-name cname) "-child-p"))))
534 (fset csym
535 `(lambda (obj)
536 ,(format
537 "Test OBJ to see if it an object is a child of type %s"
538 cname)
539 (and (eieio-object-p obj)
540 (object-of-class-p obj ,cname))))
541
542 ;; When using typep, (typep OBJ 'myclass) returns t for objects which
543 ;; are subclasses of myclass. For our predicates, however, it is
544 ;; important for EIEIO to be backwards compatible, where
545 ;; myobject-p, and myobject-child-p are different.
546 ;; "cl" uses this technique to specify symbols with specific typep
547 ;; test, so we can let typep have the CLOS documented behavior
548 ;; while keeping our above predicate clean.
549
550 ;; It would be cleaner to use `defsetf' here, but that requires cl
551 ;; at runtime.
552 (put cname 'cl-deftype-handler
553 (list 'lambda () `(list 'satisfies (quote ,csym)))))
554
555 ;; before adding new slots, lets add all the methods and classes
556 ;; in from the parent class
557 (eieio-copy-parents-into-subclass newc superclasses)
558
559 ;; Store the new class vector definition into the symbol. We need to
560 ;; do this first so that we can call defmethod for the accessor.
561 ;; The vector will be updated by the following while loop and will not
562 ;; need to be stored a second time.
563 (put cname 'eieio-class-definition newc)
564
565 ;; Query each slot in the declaration list and mangle into the
566 ;; class structure I have defined.
567 (while slots
568 (let* ((slot1 (car slots))
569 (name (car slot1))
570 (slot (cdr slot1))
571 (acces (plist-get slot ':accessor))
572 (init (or (plist-get slot ':initform)
573 (if (member ':initform slot) nil
574 eieio-unbound)))
575 (initarg (plist-get slot ':initarg))
576 (docstr (plist-get slot ':documentation))
577 (prot (plist-get slot ':protection))
578 (reader (plist-get slot ':reader))
579 (writer (plist-get slot ':writer))
580 (alloc (plist-get slot ':allocation))
581 (type (plist-get slot ':type))
582 (custom (plist-get slot ':custom))
583 (label (plist-get slot ':label))
584 (customg (plist-get slot ':group))
585 (printer (plist-get slot ':printer))
586
587 (skip-nil (class-option-assoc options :allow-nil-initform))
588 )
589
590 (if eieio-error-unsupported-class-tags
591 (let ((tmp slot))
592 (while tmp
593 (if (not (member (car tmp) '(:accessor
594 :initform
595 :initarg
596 :documentation
597 :protection
598 :reader
599 :writer
600 :allocation
601 :type
602 :custom
603 :label
604 :group
605 :printer
606 :allow-nil-initform
607 :custom-groups)))
608 (signal 'invalid-slot-type (list (car tmp))))
609 (setq tmp (cdr (cdr tmp))))))
610
611 ;; Clean up the meaning of protection.
612 (cond ((or (eq prot 'public) (eq prot :public)) (setq prot nil))
613 ((or (eq prot 'protected) (eq prot :protected)) (setq prot 'protected))
614 ((or (eq prot 'private) (eq prot :private)) (setq prot 'private))
615 ((eq prot nil) nil)
616 (t (signal 'invalid-slot-type (list ':protection prot))))
617
618 ;; Make sure the :allocation parameter has a valid value.
619 (if (not (or (not alloc) (eq alloc :class) (eq alloc :instance)))
620 (signal 'invalid-slot-type (list ':allocation alloc)))
621
622 ;; The default type specifier is supposed to be t, meaning anything.
623 (if (not type) (setq type t))
624
625 ;; Label is nil, or a string
626 (if (not (or (null label) (stringp label)))
627 (signal 'invalid-slot-type (list ':label label)))
628
629 ;; Is there an initarg, but allocation of class?
630 (if (and initarg (eq alloc :class))
631 (message "Class allocated slots do not need :initarg"))
632
633 ;; intern the symbol so we can use it blankly
634 (if initarg (set initarg initarg))
635
636 ;; The customgroup should be a list of symbols
637 (cond ((null customg)
638 (setq customg '(default)))
639 ((not (listp customg))
640 (setq customg (list customg))))
641 ;; The customgroup better be a symbol, or list of symbols.
642 (mapc (lambda (cg)
643 (if (not (symbolp cg))
644 (signal 'invalid-slot-type (list ':group cg))))
645 customg)
646
647 ;; First up, add this slot into our new class.
648 (eieio-add-new-slot newc name init docstr type custom label customg printer
649 prot initarg alloc 'defaultoverride skip-nil)
650
651 ;; We need to id the group, and store them in a group list attribute.
652 (mapc (lambda (cg) (add-to-list 'groups cg)) customg)
653
654 ;; anyone can have an accessor function. This creates a function
655 ;; of the specified name, and also performs a `defsetf' if applicable
656 ;; so that users can `setf' the space returned by this function
657 (if acces
658 (progn
659 (eieio-defmethod acces
660 (list (if (eq alloc :class) :static :primary)
661 (list (list 'this cname))
662 (format
663 "Retrieves the slot `%s' from an object of class `%s'"
664 name cname)
665 (list 'if (list 'slot-boundp 'this (list 'quote name))
666 (list 'eieio-oref 'this (list 'quote name))
667 ;; Else - Some error? nil?
668 nil)))
669
670 ;; Provide a setf method. It would be cleaner to use
671 ;; defsetf, but that would require CL at runtime.
672 (put acces 'setf-method
673 `(lambda (widget)
674 (let* ((--widget-sym-- (make-symbol "--widget--"))
675 (--store-sym-- (make-symbol "--store--")))
676 (list
677 (list --widget-sym--)
678 (list widget)
679 (list --store-sym--)
680 (list 'eieio-oset --widget-sym-- '',name --store-sym--)
681 (list 'getfoo --widget-sym--)))))))
682
683 ;; If a writer is defined, then create a generic method of that
684 ;; name whose purpose is to set the value of the slot.
685 (if writer
686 (progn
687 (eieio-defmethod writer
688 (list (list (list 'this cname) 'value)
689 (format "Set the slot `%s' of an object of class `%s'"
690 name cname)
691 `(setf (slot-value this ',name) value)))
692 ))
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 (progn
697 (eieio-defmethod reader
698 (list (list (list 'this cname))
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 (defmacro defgeneric (method args &optional doc-string)
1144 "Create a generic function METHOD.
1145 DOC-STRING is the base documentation for this class. A generic
1146 function has no body, as its purpose is to decide which method body
1147 is appropriate to use. Uses `defmethod' to create methods, and calls
1148 `defgeneric' for you. With this implementation the ARGS are
1149 currently ignored. You can use `defgeneric' to apply specialized
1150 top level documentation to a method."
1151 `(eieio-defgeneric (quote ,method) ,doc-string))
1152
1153 (defun eieio-defgeneric-form (method doc-string)
1154 "The lambda form that would be used as the function defined on METHOD.
1155 All methods should call the same EIEIO function for dispatch.
1156 DOC-STRING is the documentation attached to METHOD."
1157 `(lambda (&rest local-args)
1158 ,doc-string
1159 (eieio-generic-call (quote ,method) local-args)))
1160
1161 (defsubst eieio-defgeneric-reset-generic-form (method)
1162 "Setup METHOD to call the generic form."
1163 (let ((doc-string (documentation method)))
1164 (fset method (eieio-defgeneric-form method doc-string))))
1165
1166 (defun eieio-defgeneric-form-primary-only (method doc-string)
1167 "The lambda form that would be used as the function defined on METHOD.
1168 All methods should call the same EIEIO function for dispatch.
1169 DOC-STRING is the documentation attached to METHOD."
1170 `(lambda (&rest local-args)
1171 ,doc-string
1172 (eieio-generic-call-primary-only (quote ,method) local-args)))
1173
1174 (defsubst eieio-defgeneric-reset-generic-form-primary-only (method)
1175 "Setup METHOD to call the generic form."
1176 (let ((doc-string (documentation method)))
1177 (fset method (eieio-defgeneric-form-primary-only method doc-string))))
1178
1179 (defun eieio-defgeneric-form-primary-only-one (method doc-string
1180 class
1181 impl
1182 )
1183 "The lambda form that would be used as the function defined on METHOD.
1184 All methods should call the same EIEIO function for dispatch.
1185 DOC-STRING is the documentation attached to METHOD.
1186 CLASS is the class symbol needed for private method access.
1187 IMPL is the symbol holding the method implementation."
1188 ;; NOTE: I tried out byte compiling this little fcn. Turns out it
1189 ;; is faster to execute this for not byte-compiled. ie, install this,
1190 ;; then measure calls going through here. I wonder why.
1191 (require 'bytecomp)
1192 (let ((byte-compile-warnings nil))
1193 (byte-compile
1194 `(lambda (&rest local-args)
1195 ,doc-string
1196 ;; This is a cool cheat. Usually we need to look up in the
1197 ;; method table to find out if there is a method or not. We can
1198 ;; instead make that determination at load time when there is
1199 ;; only one method. If the first arg is not a child of the class
1200 ;; of that one implementation, then clearly, there is no method def.
1201 (if (not (eieio-object-p (car local-args)))
1202 ;; Not an object. Just signal.
1203 (signal 'no-method-definition
1204 (list ,(list 'quote method) local-args))
1205
1206 ;; We do have an object. Make sure it is the right type.
1207 (if ,(if (eq class eieio-default-superclass)
1208 nil ; default superclass means just an obj. Already asked.
1209 `(not (child-of-class-p (aref (car local-args) object-class)
1210 ,(list 'quote class)))
1211 )
1212
1213 ;; If not the right kind of object, call no applicable
1214 (apply 'no-applicable-method (car local-args)
1215 ,(list 'quote method) local-args)
1216
1217 ;; It is ok, do the call.
1218 ;; Fill in inter-call variables then evaluate the method.
1219 (let ((scoped-class ,(list 'quote class))
1220 (eieio-generic-call-next-method-list nil)
1221 (eieio-generic-call-key method-primary)
1222 (eieio-generic-call-methodname ,(list 'quote method))
1223 (eieio-generic-call-arglst local-args)
1224 )
1225 (apply ,(list 'quote impl) local-args)
1226 ;(,impl local-args)
1227 )))))))
1228
1229 (defsubst eieio-defgeneric-reset-generic-form-primary-only-one (method)
1230 "Setup METHOD to call the generic form."
1231 (let* ((doc-string (documentation method))
1232 (M (get method 'eieio-method-tree))
1233 (entry (car (aref M method-primary)))
1234 )
1235 (fset method (eieio-defgeneric-form-primary-only-one
1236 method doc-string
1237 (car entry)
1238 (cdr entry)
1239 ))))
1240
1241 (defun eieio-defgeneric (method doc-string)
1242 "Engine part to `defgeneric' macro defining METHOD with DOC-STRING."
1243 (if (and (fboundp method) (not (generic-p method))
1244 (or (byte-code-function-p (symbol-function method))
1245 (not (eq 'autoload (car (symbol-function method)))))
1246 )
1247 (error "You cannot create a generic/method over an existing symbol: %s"
1248 method))
1249 ;; Don't do this over and over.
1250 (unless (fboundp 'method)
1251 ;; This defun tells emacs where the first definition of this
1252 ;; method is defined.
1253 `(defun ,method nil)
1254 ;; Make sure the method tables are installed.
1255 (eieiomt-install method)
1256 ;; Apply the actual body of this function.
1257 (fset method (eieio-defgeneric-form method doc-string))
1258 ;; Return the method
1259 'method))
1260
1261 (defun eieio-unbind-method-implementations (method)
1262 "Make the generic method METHOD have no implementations.
1263 It will leave the original generic function in place,
1264 but remove reference to all implementations of METHOD."
1265 (put method 'eieio-method-tree nil)
1266 (put method 'eieio-method-obarray nil))
1267
1268 (defmacro defmethod (method &rest args)
1269 "Create a new METHOD through `defgeneric' with ARGS.
1270
1271 The optional second argument KEY is a specifier that
1272 modifies how the method is called, including:
1273 :before - Method will be called before the :primary
1274 :primary - The default if not specified
1275 :after - Method will be called after the :primary
1276 :static - First arg could be an object or class
1277 The next argument is the ARGLIST. The ARGLIST specifies the arguments
1278 to the method as with `defun'. The first argument can have a type
1279 specifier, such as:
1280 ((VARNAME CLASS) ARG2 ...)
1281 where VARNAME is the name of the local variable for the method being
1282 created. The CLASS is a class symbol for a class made with `defclass'.
1283 A DOCSTRING comes after the ARGLIST, and is optional.
1284 All the rest of the args are the BODY of the method. A method will
1285 return the value of the last form in the BODY.
1286
1287 Summary:
1288
1289 (defmethod mymethod [:before | :primary | :after | :static]
1290 ((typearg class-name) arg2 &optional opt &rest rest)
1291 \"doc-string\"
1292 body)"
1293 (let* ((key (cond ((or (eq ':BEFORE (car args))
1294 (eq ':before (car args)))
1295 (setq args (cdr args))
1296 :before)
1297 ((or (eq ':AFTER (car args))
1298 (eq ':after (car args)))
1299 (setq args (cdr args))
1300 :after)
1301 ((or (eq ':PRIMARY (car args))
1302 (eq ':primary (car args)))
1303 (setq args (cdr args))
1304 :primary)
1305 ((or (eq ':STATIC (car args))
1306 (eq ':static (car args)))
1307 (setq args (cdr args))
1308 :static)
1309 (t nil)))
1310 (params (car args))
1311 (lamparams
1312 (mapcar (lambda (param) (if (listp param) (car param) param))
1313 params))
1314 (arg1 (car params))
1315 (class (if (listp arg1) (nth 1 arg1) nil)))
1316 `(eieio-defmethod ',method
1317 '(,@(if key (list key))
1318 ,params)
1319 (lambda ,lamparams ,@(cdr args)))))
1320
1321 (defun eieio-defmethod (method args &optional code)
1322 "Work part of the `defmethod' macro defining METHOD with ARGS."
1323 (let ((key nil) (body nil) (firstarg nil) (argfix nil) (argclass nil) loopa)
1324 ;; find optional keys
1325 (setq key
1326 (cond ((or (eq ':BEFORE (car args))
1327 (eq ':before (car args)))
1328 (setq args (cdr args))
1329 method-before)
1330 ((or (eq ':AFTER (car args))
1331 (eq ':after (car args)))
1332 (setq args (cdr args))
1333 method-after)
1334 ((or (eq ':PRIMARY (car args))
1335 (eq ':primary (car args)))
1336 (setq args (cdr args))
1337 method-primary)
1338 ((or (eq ':STATIC (car args))
1339 (eq ':static (car args)))
1340 (setq args (cdr args))
1341 method-static)
1342 ;; Primary key
1343 (t method-primary)))
1344 ;; get body, and fix contents of args to be the arguments of the fn.
1345 (setq body (cdr args)
1346 args (car args))
1347 (setq loopa args)
1348 ;; Create a fixed version of the arguments
1349 (while loopa
1350 (setq argfix (cons (if (listp (car loopa)) (car (car loopa)) (car loopa))
1351 argfix))
1352 (setq loopa (cdr loopa)))
1353 ;; make sure there is a generic
1354 (eieio-defgeneric
1355 method
1356 (if (stringp (car body))
1357 (car body) (format "Generically created method `%s'." method)))
1358 ;; create symbol for property to bind to. If the first arg is of
1359 ;; the form (varname vartype) and `vartype' is a class, then
1360 ;; that class will be the type symbol. If not, then it will fall
1361 ;; under the type `primary' which is a non-specific calling of the
1362 ;; function.
1363 (setq firstarg (car args))
1364 (if (listp firstarg)
1365 (progn
1366 (setq argclass (nth 1 firstarg))
1367 (if (not (class-p argclass))
1368 (error "Unknown class type %s in method parameters"
1369 (nth 1 firstarg))))
1370 (if (= key -1)
1371 (signal 'wrong-type-argument (list :static 'non-class-arg)))
1372 ;; generics are higher
1373 (setq key (eieio-specialized-key-to-generic-key key)))
1374 ;; Put this lambda into the symbol so we can find it
1375 (eieiomt-add method code key argclass)
1376 )
1377
1378 (when eieio-optimize-primary-methods-flag
1379 ;; Optimizing step:
1380 ;;
1381 ;; If this method, after this setup, only has primary methods, then
1382 ;; we can setup the generic that way.
1383 (if (generic-primary-only-p method)
1384 ;; If there is only one primary method, then we can go one more
1385 ;; optimization step.
1386 (if (generic-primary-only-one-p method)
1387 (eieio-defgeneric-reset-generic-form-primary-only-one method)
1388 (eieio-defgeneric-reset-generic-form-primary-only method))
1389 (eieio-defgeneric-reset-generic-form method)))
1390
1391 method)
1392
1393 ;;; Slot type validation
1394
1395 ;; This is a hideous hack for replacing `typep' from cl-macs, to avoid
1396 ;; requiring the CL library at run-time. It can be eliminated if/when
1397 ;; `typep' is merged into Emacs core.
1398 (defun eieio--typep (val type)
1399 (if (symbolp type)
1400 (cond ((get type 'cl-deftype-handler)
1401 (eieio--typep val (funcall (get type 'cl-deftype-handler))))
1402 ((eq type t) t)
1403 ((eq type 'null) (null val))
1404 ((eq type 'atom) (atom val))
1405 ((eq type 'float) (and (numberp val) (not (integerp val))))
1406 ((eq type 'real) (numberp val))
1407 ((eq type 'fixnum) (integerp val))
1408 ((memq type '(character string-char)) (characterp val))
1409 (t
1410 (let* ((name (symbol-name type))
1411 (namep (intern (concat name "p"))))
1412 (if (fboundp namep)
1413 (funcall `(lambda () (,namep val)))
1414 (funcall `(lambda ()
1415 (,(intern (concat name "-p")) val)))))))
1416 (cond ((get (car type) 'cl-deftype-handler)
1417 (eieio--typep val (apply (get (car type) 'cl-deftype-handler)
1418 (cdr type))))
1419 ((memq (car type) '(integer float real number))
1420 (and (eieio--typep val (car type))
1421 (or (memq (cadr type) '(* nil))
1422 (if (consp (cadr type))
1423 (> val (car (cadr type)))
1424 (>= val (cadr type))))
1425 (or (memq (caddr type) '(* nil))
1426 (if (consp (car (cddr type)))
1427 (< val (caar (cddr type)))
1428 (<= val (car (cddr type)))))))
1429 ((memq (car type) '(and or not))
1430 (eval (cons (car type)
1431 (mapcar (lambda (x)
1432 `(eieio--typep (quote ,val) (quote ,x)))
1433 (cdr type)))))
1434 ((memq (car type) '(member member*))
1435 (memql val (cdr type)))
1436 ((eq (car type) 'satisfies)
1437 (funcall `(lambda () (,(cadr type) val))))
1438 (t (error "Bad type spec: %s" type)))))
1439
1440 (defun eieio-perform-slot-validation (spec value)
1441 "Return non-nil if SPEC does not match VALUE."
1442 (or (eq spec t) ; t always passes
1443 (eq value eieio-unbound) ; unbound always passes
1444 (eieio--typep value spec)))
1445
1446 (defun eieio-validate-slot-value (class slot-idx value slot)
1447 "Make sure that for CLASS referencing SLOT-IDX, VALUE is valid.
1448 Checks the :type specifier.
1449 SLOT is the slot that is being checked, and is only used when throwing
1450 an error."
1451 (if eieio-skip-typecheck
1452 nil
1453 ;; Trim off object IDX junk added in for the object index.
1454 (setq slot-idx (- slot-idx 3))
1455 (let ((st (aref (aref (class-v class) class-public-type) slot-idx)))
1456 (if (not (eieio-perform-slot-validation st value))
1457 (signal 'invalid-slot-type (list class slot st value))))))
1458
1459 (defun eieio-validate-class-slot-value (class slot-idx value slot)
1460 "Make sure that for CLASS referencing SLOT-IDX, VALUE is valid.
1461 Checks the :type specifier.
1462 SLOT is the slot that is being checked, and is only used when throwing
1463 an error."
1464 (if eieio-skip-typecheck
1465 nil
1466 (let ((st (aref (aref (class-v class) class-class-allocation-type)
1467 slot-idx)))
1468 (if (not (eieio-perform-slot-validation st value))
1469 (signal 'invalid-slot-type (list class slot st value))))))
1470
1471 (defun eieio-barf-if-slot-unbound (value instance slotname fn)
1472 "Throw a signal if VALUE is a representation of an UNBOUND slot.
1473 INSTANCE is the object being referenced. SLOTNAME is the offending
1474 slot. If the slot is ok, return VALUE.
1475 Argument FN is the function calling this verifier."
1476 (if (and (eq value eieio-unbound) (not eieio-skip-typecheck))
1477 (slot-unbound instance (object-class instance) slotname fn)
1478 value))
1479
1480 ;;; Get/Set slots in an object.
1481 ;;
1482 (defmacro oref (obj slot)
1483 "Retrieve the value stored in OBJ in the slot named by SLOT.
1484 Slot is the name of the slot when created by `defclass' or the label
1485 created by the :initarg tag."
1486 `(eieio-oref ,obj (quote ,slot)))
1487
1488 (defun eieio-oref (obj slot)
1489 "Return the value in OBJ at SLOT in the object vector."
1490 (if (not (or (eieio-object-p obj) (class-p obj)))
1491 (signal 'wrong-type-argument (list '(or eieio-object-p class-p) obj)))
1492 (if (not (symbolp slot))
1493 (signal 'wrong-type-argument (list 'symbolp slot)))
1494 (if (class-p obj) (eieio-class-un-autoload obj))
1495 (let* ((class (if (class-p obj) obj (aref obj object-class)))
1496 (c (eieio-slot-name-index class obj slot)))
1497 (if (not c)
1498 ;; It might be missing because it is a :class allocated slot.
1499 ;; Lets check that info out.
1500 (if (setq c (eieio-class-slot-name-index class slot))
1501 ;; Oref that slot.
1502 (aref (aref (class-v class) class-class-allocation-values) c)
1503 ;; The slot-missing method is a cool way of allowing an object author
1504 ;; to intercept missing slot definitions. Since it is also the LAST
1505 ;; thing called in this fn, its return value would be retrieved.
1506 (slot-missing obj slot 'oref)
1507 ;;(signal 'invalid-slot-name (list (object-name obj) slot))
1508 )
1509 (if (not (eieio-object-p obj))
1510 (signal 'wrong-type-argument (list 'eieio-object-p obj)))
1511 (eieio-barf-if-slot-unbound (aref obj c) obj slot 'oref))))
1512
1513 (defalias 'slot-value 'eieio-oref)
1514 (defalias 'set-slot-value 'eieio-oset)
1515
1516 (defmacro oref-default (obj slot)
1517 "Get the default value of OBJ (maybe a class) for SLOT.
1518 The default value is the value installed in a class with the :initform
1519 tag. SLOT can be the slot name, or the tag specified by the :initarg
1520 tag in the `defclass' call."
1521 `(eieio-oref-default ,obj (quote ,slot)))
1522
1523 (defun eieio-oref-default (obj slot)
1524 "Do the work for the macro `oref-default' with similar parameters.
1525 Fills in OBJ's SLOT with its default value."
1526 (if (not (or (eieio-object-p obj) (class-p obj))) (signal 'wrong-type-argument (list 'eieio-object-p obj)))
1527 (if (not (symbolp slot)) (signal 'wrong-type-argument (list 'symbolp slot)))
1528 (let* ((cl (if (eieio-object-p obj) (aref obj object-class) obj))
1529 (c (eieio-slot-name-index cl obj slot)))
1530 (if (not c)
1531 ;; It might be missing because it is a :class allocated slot.
1532 ;; Lets check that info out.
1533 (if (setq c
1534 (eieio-class-slot-name-index cl slot))
1535 ;; Oref that slot.
1536 (aref (aref (class-v cl) class-class-allocation-values)
1537 c)
1538 (slot-missing obj slot 'oref-default)
1539 ;;(signal 'invalid-slot-name (list (class-name cl) slot))
1540 )
1541 (eieio-barf-if-slot-unbound
1542 (let ((val (nth (- c 3) (aref (class-v cl) class-public-d))))
1543 (eieio-default-eval-maybe val))
1544 obj cl 'oref-default))))
1545
1546 (defsubst eieio-eval-default-p (val)
1547 "Whether the default value VAL should be evaluated for use."
1548 (and (consp val) (symbolp (car val)) (fboundp (car val))))
1549
1550 (defun eieio-default-eval-maybe (val)
1551 "Check VAL, and return what `oref-default' would provide."
1552 (cond
1553 ;; Is it a function call? If so, evaluate it.
1554 ((eieio-eval-default-p val)
1555 (eval val))
1556 ;;;; check for quoted things, and unquote them
1557 ;;((and (consp val) (eq (car val) 'quote))
1558 ;; (car (cdr val)))
1559 ;; return it verbatim
1560 (t val)))
1561
1562 ;;; Object Set macros
1563 ;;
1564 (defmacro oset (obj slot value)
1565 "Set the value in OBJ for slot SLOT to VALUE.
1566 SLOT is the slot name as specified in `defclass' or the tag created
1567 with in the :initarg slot. VALUE can be any Lisp object."
1568 `(eieio-oset ,obj (quote ,slot) ,value))
1569
1570 (defun eieio-oset (obj slot value)
1571 "Do the work for the macro `oset'.
1572 Fills in OBJ's SLOT with VALUE."
1573 (if (not (eieio-object-p obj)) (signal 'wrong-type-argument (list 'eieio-object-p obj)))
1574 (if (not (symbolp slot)) (signal 'wrong-type-argument (list 'symbolp slot)))
1575 (let ((c (eieio-slot-name-index (object-class-fast obj) obj slot)))
1576 (if (not c)
1577 ;; It might be missing because it is a :class allocated slot.
1578 ;; Lets check that info out.
1579 (if (setq c
1580 (eieio-class-slot-name-index (aref obj object-class) slot))
1581 ;; Oset that slot.
1582 (progn
1583 (eieio-validate-class-slot-value (object-class-fast obj) c value slot)
1584 (aset (aref (class-v (aref obj object-class))
1585 class-class-allocation-values)
1586 c value))
1587 ;; See oref for comment on `slot-missing'
1588 (slot-missing obj slot 'oset value)
1589 ;;(signal 'invalid-slot-name (list (object-name obj) slot))
1590 )
1591 (eieio-validate-slot-value (object-class-fast obj) c value slot)
1592 (aset obj c value))))
1593
1594 (defmacro oset-default (class slot value)
1595 "Set the default slot in CLASS for SLOT to VALUE.
1596 The default value is usually set with the :initform tag during class
1597 creation. This allows users to change the default behavior of classes
1598 after they are created."
1599 `(eieio-oset-default ,class (quote ,slot) ,value))
1600
1601 (defun eieio-oset-default (class slot value)
1602 "Do the work for the macro `oset-default'.
1603 Fills in the default value in CLASS' in SLOT with VALUE."
1604 (if (not (class-p class)) (signal 'wrong-type-argument (list 'class-p class)))
1605 (if (not (symbolp slot)) (signal 'wrong-type-argument (list 'symbolp slot)))
1606 (let* ((scoped-class class)
1607 (c (eieio-slot-name-index class nil slot)))
1608 (if (not c)
1609 ;; It might be missing because it is a :class allocated slot.
1610 ;; Lets check that info out.
1611 (if (setq c (eieio-class-slot-name-index class slot))
1612 (progn
1613 ;; Oref that slot.
1614 (eieio-validate-class-slot-value class c value slot)
1615 (aset (aref (class-v class) class-class-allocation-values) c
1616 value))
1617 (signal 'invalid-slot-name (list (class-name class) slot)))
1618 (eieio-validate-slot-value class c value slot)
1619 ;; Set this into the storage for defaults.
1620 (setcar (nthcdr (- c 3) (aref (class-v class) class-public-d))
1621 value)
1622 ;; Take the value, and put it into our cache object.
1623 (eieio-oset (aref (class-v class) class-default-object-cache)
1624 slot value)
1625 )))
1626
1627 ;;; Handy CLOS macros
1628 ;;
1629 (defmacro with-slots (spec-list object &rest body)
1630 "Bind SPEC-LIST lexically to slot values in OBJECT, and execute BODY.
1631 This establishes a lexical environment for referring to the slots in
1632 the instance named by the given slot-names as though they were
1633 variables. Within such a context the value of the slot can be
1634 specified by using its slot name, as if it were a lexically bound
1635 variable. Both setf and setq can be used to set the value of the
1636 slot.
1637
1638 SPEC-LIST is of a form similar to `let'. For example:
1639
1640 ((VAR1 SLOT1)
1641 SLOT2
1642 SLOTN
1643 (VARN+1 SLOTN+1))
1644
1645 Where each VAR is the local variable given to the associated
1646 SLOT. A slot specified without a variable name is given a
1647 variable name of the same name as the slot."
1648 (declare (indent 2))
1649 ;; Transform the spec-list into a symbol-macrolet spec-list.
1650 (let ((mappings (mapcar (lambda (entry)
1651 (let ((var (if (listp entry) (car entry) entry))
1652 (slot (if (listp entry) (cadr entry) entry)))
1653 (list var `(slot-value ,object ',slot))))
1654 spec-list)))
1655 (append (list 'symbol-macrolet mappings)
1656 body)))
1657 \f
1658 ;;; Simple generators, and query functions. None of these would do
1659 ;; well embedded into an object.
1660 ;;
1661 (defmacro object-class-fast (obj) "Return the class struct defining OBJ with no check."
1662 `(aref ,obj object-class))
1663
1664 (defun class-name (class) "Return a Lisp like symbol name for CLASS."
1665 (if (not (class-p class)) (signal 'wrong-type-argument (list 'class-p class)))
1666 ;; I think this is supposed to return a symbol, but to me CLASS is a symbol,
1667 ;; and I wanted a string. Arg!
1668 (format "#<class %s>" (symbol-name class)))
1669
1670 (defun object-name (obj &optional extra)
1671 "Return a Lisp like symbol string for object OBJ.
1672 If EXTRA, include that in the string returned to represent the symbol."
1673 (if (not (eieio-object-p obj)) (signal 'wrong-type-argument (list 'eieio-object-p obj)))
1674 (format "#<%s %s%s>" (symbol-name (object-class-fast obj))
1675 (aref obj object-name) (or extra "")))
1676
1677 (defun object-name-string (obj) "Return a string which is OBJ's name."
1678 (if (not (eieio-object-p obj)) (signal 'wrong-type-argument (list 'eieio-object-p obj)))
1679 (aref obj object-name))
1680
1681 (defun object-set-name-string (obj name) "Set the string which is OBJ's NAME."
1682 (if (not (eieio-object-p obj)) (signal 'wrong-type-argument (list 'eieio-object-p obj)))
1683 (if (not (stringp name)) (signal 'wrong-type-argument (list 'stringp name)))
1684 (aset obj object-name name))
1685
1686 (defun object-class (obj) "Return the class struct defining OBJ."
1687 (if (not (eieio-object-p obj)) (signal 'wrong-type-argument (list 'eieio-object-p obj)))
1688 (object-class-fast obj))
1689 (defalias 'class-of 'object-class)
1690
1691 (defun object-class-name (obj) "Return a Lisp like symbol name for OBJ's class."
1692 (if (not (eieio-object-p obj)) (signal 'wrong-type-argument (list 'eieio-object-p obj)))
1693 (class-name (object-class-fast obj)))
1694
1695 (defmacro class-parents-fast (class) "Return parent classes to CLASS with no check."
1696 `(aref (class-v ,class) class-parent))
1697
1698 (defun class-parents (class)
1699 "Return parent classes to CLASS. (overload of variable).
1700
1701 The CLOS function `class-direct-superclasses' is aliased to this function."
1702 (if (not (class-p class)) (signal 'wrong-type-argument (list 'class-p class)))
1703 (class-parents-fast class))
1704
1705 (defmacro class-children-fast (class) "Return child classes to CLASS with no check."
1706 `(aref (class-v ,class) class-children))
1707
1708 (defun class-children (class)
1709 "Return child classes to CLASS.
1710
1711 The CLOS function `class-direct-subclasses' is aliased to this function."
1712 (if (not (class-p class)) (signal 'wrong-type-argument (list 'class-p class)))
1713 (class-children-fast class))
1714
1715 (defun eieio-c3-candidate (class remaining-inputs)
1716 "Returns CLASS if it can go in the result now, otherwise nil"
1717 ;; Ensure CLASS is not in any position but the first in any of the
1718 ;; element lists of REMAINING-INPUTS.
1719 (and (not (let ((found nil))
1720 (while (and remaining-inputs (not found))
1721 (setq found (member class (cdr (car remaining-inputs)))
1722 remaining-inputs (cdr remaining-inputs)))
1723 found))
1724 class))
1725
1726 (defun eieio-c3-merge-lists (reversed-partial-result remaining-inputs)
1727 "Merge REVERSED-PARTIAL-RESULT REMAINING-INPUTS in a consistent order, if possible.
1728 If a consistent order does not exist, signal an error."
1729 (if (let ((tail remaining-inputs)
1730 (found nil))
1731 (while (and tail (not found))
1732 (setq found (car tail) tail (cdr tail)))
1733 (not found))
1734 ;; If all remaining inputs are empty lists, we are done.
1735 (nreverse reversed-partial-result)
1736 ;; Otherwise, we try to find the next element of the result. This
1737 ;; is achieved by considering the first element of each
1738 ;; (non-empty) input list and accepting a candidate if it is
1739 ;; consistent with the rests of the input lists.
1740 (let* ((found nil)
1741 (tail remaining-inputs)
1742 (next (progn
1743 (while (and tail (not found))
1744 (setq found (and (car tail)
1745 (eieio-c3-candidate (caar tail)
1746 remaining-inputs))
1747 tail (cdr tail)))
1748 found)))
1749 (if next
1750 ;; The graph is consistent so far, add NEXT to result and
1751 ;; merge input lists, dropping NEXT from their heads where
1752 ;; applicable.
1753 (eieio-c3-merge-lists
1754 (cons next reversed-partial-result)
1755 (mapcar (lambda (l) (if (eq (first l) next) (rest l) l))
1756 remaining-inputs))
1757 ;; The graph is inconsistent, give up
1758 (signal 'inconsistent-class-hierarchy (list remaining-inputs))))))
1759
1760 (defun eieio-class-precedence-dfs (class)
1761 "Return all parents of CLASS in depth-first order."
1762 (let* ((parents (class-parents-fast class))
1763 (classes (copy-sequence
1764 (apply #'append
1765 (list class)
1766 (or
1767 (mapcar
1768 (lambda (parent)
1769 (cons parent
1770 (eieio-class-precedence-dfs parent)))
1771 parents)
1772 '((eieio-default-superclass))))))
1773 (tail classes))
1774 ;; Remove duplicates.
1775 (while tail
1776 (setcdr tail (delq (car tail) (cdr tail)))
1777 (setq tail (cdr tail)))
1778 classes))
1779
1780 (defun eieio-class-precedence-bfs (class)
1781 "Return all parents of CLASS in breadth-first order."
1782 (let ((result)
1783 (queue (or (class-parents-fast class)
1784 '(eieio-default-superclass))))
1785 (while queue
1786 (let ((head (pop queue)))
1787 (unless (member head result)
1788 (push head result)
1789 (unless (eq head 'eieio-default-superclass)
1790 (setq queue (append queue (or (class-parents-fast head)
1791 '(eieio-default-superclass))))))))
1792 (cons class (nreverse result)))
1793 )
1794
1795 (defun eieio-class-precedence-c3 (class)
1796 "Return all parents of CLASS in c3 order."
1797 (let ((parents (class-parents-fast class)))
1798 (eieio-c3-merge-lists
1799 (list class)
1800 (append
1801 (or
1802 (mapcar
1803 (lambda (x)
1804 (eieio-class-precedence-c3 x))
1805 parents)
1806 '((eieio-default-superclass)))
1807 (list parents))))
1808 )
1809
1810 (defun class-precedence-list (class)
1811 "Return (transitively closed) list of parents of CLASS.
1812 The order, in which the parents are returned depends on the
1813 method invocation orders of the involved classes."
1814 (if (or (null class) (eq class 'eieio-default-superclass))
1815 nil
1816 (case (class-method-invocation-order class)
1817 (:depth-first
1818 (eieio-class-precedence-dfs class))
1819 (:breadth-first
1820 (eieio-class-precedence-bfs class))
1821 (:c3
1822 (eieio-class-precedence-c3 class))))
1823 )
1824
1825 ;; Official CLOS functions.
1826 (defalias 'class-direct-superclasses 'class-parents)
1827 (defalias 'class-direct-subclasses 'class-children)
1828
1829 (defmacro class-parent-fast (class) "Return first parent class to CLASS with no check."
1830 `(car (class-parents-fast ,class)))
1831
1832 (defmacro class-parent (class) "Return first parent class to CLASS. (overload of variable)."
1833 `(car (class-parents ,class)))
1834
1835 (defmacro same-class-fast-p (obj class) "Return t if OBJ is of class-type CLASS with no error checking."
1836 `(eq (aref ,obj object-class) ,class))
1837
1838 (defun same-class-p (obj class) "Return t if OBJ is of class-type CLASS."
1839 (if (not (class-p class)) (signal 'wrong-type-argument (list 'class-p class)))
1840 (if (not (eieio-object-p obj)) (signal 'wrong-type-argument (list 'eieio-object-p obj)))
1841 (same-class-fast-p obj class))
1842
1843 (defun object-of-class-p (obj class)
1844 "Return non-nil if OBJ is an instance of CLASS or CLASS' subclasses."
1845 (if (not (eieio-object-p obj)) (signal 'wrong-type-argument (list 'eieio-object-p obj)))
1846 ;; class will be checked one layer down
1847 (child-of-class-p (aref obj object-class) class))
1848 ;; Backwards compatibility
1849 (defalias 'obj-of-class-p 'object-of-class-p)
1850
1851 (defun child-of-class-p (child class)
1852 "Return non-nil if CHILD class is a subclass of CLASS."
1853 (if (not (class-p class)) (signal 'wrong-type-argument (list 'class-p class)))
1854 (if (not (class-p child)) (signal 'wrong-type-argument (list 'class-p child)))
1855 (let ((p nil))
1856 (while (and child (not (eq child class)))
1857 (setq p (append p (aref (class-v child) class-parent))
1858 child (car p)
1859 p (cdr p)))
1860 (if child t)))
1861
1862 (defun object-slots (obj)
1863 "Return list of slots available in OBJ."
1864 (if (not (eieio-object-p obj)) (signal 'wrong-type-argument (list 'eieio-object-p obj)))
1865 (aref (class-v (object-class-fast obj)) class-public-a))
1866
1867 (defun class-slot-initarg (class slot) "Fetch from CLASS, SLOT's :initarg."
1868 (if (not (class-p class)) (signal 'wrong-type-argument (list 'class-p class)))
1869 (let ((ia (aref (class-v class) class-initarg-tuples))
1870 (f nil))
1871 (while (and ia (not f))
1872 (if (eq (cdr (car ia)) slot)
1873 (setq f (car (car ia))))
1874 (setq ia (cdr ia)))
1875 f))
1876
1877 ;;; CLOS queries into classes and slots
1878 ;;
1879 (defun slot-boundp (object slot)
1880 "Return non-nil if OBJECT's SLOT is bound.
1881 Setting a slot's value makes it bound. Calling `slot-makeunbound' will
1882 make a slot unbound.
1883 OBJECT can be an instance or a class."
1884 ;; Skip typechecking while retrieving this value.
1885 (let ((eieio-skip-typecheck t))
1886 ;; Return nil if the magic symbol is in there.
1887 (if (eieio-object-p object)
1888 (if (eq (eieio-oref object slot) eieio-unbound) nil t)
1889 (if (class-p object)
1890 (if (eq (eieio-oref-default object slot) eieio-unbound) nil t)
1891 (signal 'wrong-type-argument (list 'eieio-object-p object))))))
1892
1893 (defun slot-makeunbound (object slot)
1894 "In OBJECT, make SLOT unbound."
1895 (eieio-oset object slot eieio-unbound))
1896
1897 (defun slot-exists-p (object-or-class slot)
1898 "Return non-nil if OBJECT-OR-CLASS has SLOT."
1899 (let ((cv (class-v (cond ((eieio-object-p object-or-class)
1900 (object-class object-or-class))
1901 ((class-p object-or-class)
1902 object-or-class))
1903 )))
1904 (or (memq slot (aref cv class-public-a))
1905 (memq slot (aref cv class-class-allocation-a)))
1906 ))
1907
1908 (defun find-class (symbol &optional errorp)
1909 "Return the class that SYMBOL represents.
1910 If there is no class, nil is returned if ERRORP is nil.
1911 If ERRORP is non-nil, `wrong-argument-type' is signaled."
1912 (if (not (class-p symbol))
1913 (if errorp (signal 'wrong-type-argument (list 'class-p symbol))
1914 nil)
1915 (class-v symbol)))
1916
1917 ;;; Slightly more complex utility functions for objects
1918 ;;
1919 (defun object-assoc (key slot list)
1920 "Return an object if KEY is `equal' to SLOT's value of an object in LIST.
1921 LIST is a list of objects whose slots are searched.
1922 Objects in LIST do not need to have a slot named SLOT, nor does
1923 SLOT need to be bound. If these errors occur, those objects will
1924 be ignored."
1925 (if (not (listp list)) (signal 'wrong-type-argument (list 'listp list)))
1926 (while (and list (not (condition-case nil
1927 ;; This prevents errors for missing slots.
1928 (equal key (eieio-oref (car list) slot))
1929 (error nil))))
1930 (setq list (cdr list)))
1931 (car list))
1932
1933 (defun object-assoc-list (slot list)
1934 "Return an association list with the contents of SLOT as the key element.
1935 LIST must be a list of objects with SLOT in it.
1936 This is useful when you need to do completing read on an object group."
1937 (if (not (listp list)) (signal 'wrong-type-argument (list 'listp list)))
1938 (let ((assoclist nil))
1939 (while list
1940 (setq assoclist (cons (cons (eieio-oref (car list) slot)
1941 (car list))
1942 assoclist))
1943 (setq list (cdr list)))
1944 (nreverse assoclist)))
1945
1946 (defun object-assoc-list-safe (slot list)
1947 "Return an association list with the contents of SLOT as the key element.
1948 LIST must be a list of objects, but those objects do not need to have
1949 SLOT in it. If it does not, then that element is left out of the association
1950 list."
1951 (if (not (listp list)) (signal 'wrong-type-argument (list 'listp list)))
1952 (let ((assoclist nil))
1953 (while list
1954 (if (slot-exists-p (car list) slot)
1955 (setq assoclist (cons (cons (eieio-oref (car list) slot)
1956 (car list))
1957 assoclist)))
1958 (setq list (cdr list)))
1959 (nreverse assoclist)))
1960
1961 (defun object-add-to-list (object slot item &optional append)
1962 "In OBJECT's SLOT, add ITEM to the list of elements.
1963 Optional argument APPEND indicates we need to append to the list.
1964 If ITEM already exists in the list in SLOT, then it is not added.
1965 Comparison is done with `equal' through the `member' function call.
1966 If SLOT is unbound, bind it to the list containing ITEM."
1967 (let (ov)
1968 ;; Find the originating list.
1969 (if (not (slot-boundp object slot))
1970 (setq ov (list item))
1971 (setq ov (eieio-oref object slot))
1972 ;; turn it into a list.
1973 (unless (listp ov)
1974 (setq ov (list ov)))
1975 ;; Do the combination
1976 (if (not (member item ov))
1977 (setq ov
1978 (if append
1979 (append ov (list item))
1980 (cons item ov)))))
1981 ;; Set back into the slot.
1982 (eieio-oset object slot ov)))
1983
1984 (defun object-remove-from-list (object slot item)
1985 "In OBJECT's SLOT, remove occurrences of ITEM.
1986 Deletion is done with `delete', which deletes by side effect,
1987 and comparisons are done with `equal'.
1988 If SLOT is unbound, do nothing."
1989 (if (not (slot-boundp object slot))
1990 nil
1991 (eieio-oset object slot (delete item (eieio-oref object slot)))))
1992 \f
1993 ;;; EIEIO internal search functions
1994 ;;
1995 (defun eieio-slot-originating-class-p (start-class slot)
1996 "Return non-nil if START-CLASS is the first class to define SLOT.
1997 This is for testing if `scoped-class' is the class that defines SLOT
1998 so that we can protect private slots."
1999 (let ((par (class-parents start-class))
2000 (ret t))
2001 (if (not par)
2002 t
2003 (while (and par ret)
2004 (if (intern-soft (symbol-name slot)
2005 (aref (class-v (car par))
2006 class-symbol-obarray))
2007 (setq ret nil))
2008 (setq par (cdr par)))
2009 ret)))
2010
2011 (defun eieio-slot-name-index (class obj slot)
2012 "In CLASS for OBJ find the index of the named SLOT.
2013 The slot is a symbol which is installed in CLASS by the `defclass'
2014 call. OBJ can be nil, but if it is an object, and the slot in question
2015 is protected, access will be allowed if OBJ is a child of the currently
2016 `scoped-class'.
2017 If SLOT is the value created with :initarg instead,
2018 reverse-lookup that name, and recurse with the associated slot value."
2019 ;; Removed checks to outside this call
2020 (let* ((fsym (intern-soft (symbol-name slot)
2021 (aref (class-v class)
2022 class-symbol-obarray)))
2023 (fsi (if (symbolp fsym) (symbol-value fsym) nil)))
2024 (if (integerp fsi)
2025 (cond
2026 ((not (get fsym 'protection))
2027 (+ 3 fsi))
2028 ((and (eq (get fsym 'protection) 'protected)
2029 scoped-class
2030 (or (child-of-class-p class scoped-class)
2031 (and (eieio-object-p obj)
2032 (child-of-class-p class (object-class obj)))))
2033 (+ 3 fsi))
2034 ((and (eq (get fsym 'protection) 'private)
2035 (or (and scoped-class
2036 (eieio-slot-originating-class-p scoped-class slot))
2037 eieio-initializing-object))
2038 (+ 3 fsi))
2039 (t nil))
2040 (let ((fn (eieio-initarg-to-attribute class slot)))
2041 (if fn (eieio-slot-name-index class obj fn) nil)))))
2042
2043 (defun eieio-class-slot-name-index (class slot)
2044 "In CLASS find the index of the named SLOT.
2045 The slot is a symbol which is installed in CLASS by the `defclass'
2046 call. If SLOT is the value created with :initarg instead,
2047 reverse-lookup that name, and recurse with the associated slot value."
2048 ;; This will happen less often, and with fewer slots. Do this the
2049 ;; storage cheap way.
2050 (let* ((a (aref (class-v class) class-class-allocation-a))
2051 (l1 (length a))
2052 (af (memq slot a))
2053 (l2 (length af)))
2054 ;; Slot # is length of the total list, minus the remaining list of
2055 ;; the found slot.
2056 (if af (- l1 l2))))
2057 \f
2058 ;;; CLOS generics internal function handling
2059 ;;
2060 (defvar eieio-generic-call-methodname nil
2061 "When using `call-next-method', provides a context on how to do it.")
2062 (defvar eieio-generic-call-arglst nil
2063 "When using `call-next-method', provides a context for parameters.")
2064 (defvar eieio-generic-call-key nil
2065 "When using `call-next-method', provides a context for the current key.
2066 Keys are a number representing :before, :primary, and :after methods.")
2067 (defvar eieio-generic-call-next-method-list nil
2068 "When executing a PRIMARY or STATIC method, track the 'next-method'.
2069 During executions, the list is first generated, then as each next method
2070 is called, the next method is popped off the stack.")
2071
2072 (defvar eieio-pre-method-execution-hooks nil
2073 "*Hooks run just before a method is executed.
2074 The hook function must accept one argument, the list of forms
2075 about to be executed.")
2076
2077 (defun eieio-generic-call (method args)
2078 "Call METHOD with ARGS.
2079 ARGS provides the context on which implementation to use.
2080 This should only be called from a generic function."
2081 ;; We must expand our arguments first as they are always
2082 ;; passed in as quoted symbols
2083 (let ((newargs nil) (mclass nil) (lambdas nil) (tlambdas nil) (keys nil)
2084 (eieio-generic-call-methodname method)
2085 (eieio-generic-call-arglst args)
2086 (firstarg nil)
2087 (primarymethodlist nil))
2088 ;; get a copy
2089 (setq newargs args
2090 firstarg (car newargs))
2091 ;; Is the class passed in autoloaded?
2092 ;; Since class names are also constructors, they can be autoloaded
2093 ;; via the autoload command. Check for this, and load them in.
2094 ;; It's ok if it doesn't turn out to be a class. Probably want that
2095 ;; function loaded anyway.
2096 (if (and (symbolp firstarg)
2097 (fboundp firstarg)
2098 (listp (symbol-function firstarg))
2099 (eq 'autoload (car (symbol-function firstarg))))
2100 (load (nth 1 (symbol-function firstarg))))
2101 ;; Determine the class to use.
2102 (cond ((eieio-object-p firstarg)
2103 (setq mclass (object-class-fast firstarg)))
2104 ((class-p firstarg)
2105 (setq mclass firstarg))
2106 )
2107 ;; Make sure the class is a valid class
2108 ;; mclass can be nil (meaning a generic for should be used.
2109 ;; mclass cannot have a value that is not a class, however.
2110 (when (and (not (null mclass)) (not (class-p mclass)))
2111 (error "Cannot dispatch method %S on class %S"
2112 method mclass)
2113 )
2114 ;; Now create a list in reverse order of all the calls we have
2115 ;; make in order to successfully do this right. Rules:
2116 ;; 1) Only call generics if scoped-class is not defined
2117 ;; This prevents multiple calls in the case of recursion
2118 ;; 2) Only call static if this is a static method.
2119 ;; 3) Only call specifics if the definition allows for them.
2120 ;; 4) Call in order based on :before, :primary, and :after
2121 (when (eieio-object-p firstarg)
2122 ;; Non-static calls do all this stuff.
2123
2124 ;; :after methods
2125 (setq tlambdas
2126 (if mclass
2127 (eieiomt-method-list method method-after mclass)
2128 (list (eieio-generic-form method method-after nil)))
2129 ;;(or (and mclass (eieio-generic-form method method-after mclass))
2130 ;; (eieio-generic-form method method-after nil))
2131 )
2132 (setq lambdas (append tlambdas lambdas)
2133 keys (append (make-list (length tlambdas) method-after) keys))
2134
2135 ;; :primary methods
2136 (setq tlambdas
2137 (or (and mclass (eieio-generic-form method method-primary mclass))
2138 (eieio-generic-form method method-primary nil)))
2139 (when tlambdas
2140 (setq lambdas (cons tlambdas lambdas)
2141 keys (cons method-primary keys)
2142 primarymethodlist
2143 (eieiomt-method-list method method-primary mclass)))
2144
2145 ;; :before methods
2146 (setq tlambdas
2147 (if mclass
2148 (eieiomt-method-list method method-before mclass)
2149 (list (eieio-generic-form method method-before nil)))
2150 ;;(or (and mclass (eieio-generic-form method method-before mclass))
2151 ;; (eieio-generic-form method method-before nil))
2152 )
2153 (setq lambdas (append tlambdas lambdas)
2154 keys (append (make-list (length tlambdas) method-before) keys))
2155 )
2156
2157 (if mclass
2158 ;; For the case of a class,
2159 ;; if there were no methods found, then there could be :static methods.
2160 (when (not lambdas)
2161 (setq tlambdas
2162 (eieio-generic-form method method-static mclass))
2163 (setq lambdas (cons tlambdas lambdas)
2164 keys (cons method-static keys)
2165 primarymethodlist ;; Re-use even with bad name here
2166 (eieiomt-method-list method method-static mclass)))
2167 ;; For the case of no class (ie - mclass == nil) then there may
2168 ;; be a primary method.
2169 (setq tlambdas
2170 (eieio-generic-form method method-primary nil))
2171 (when tlambdas
2172 (setq lambdas (cons tlambdas lambdas)
2173 keys (cons method-primary keys)
2174 primarymethodlist
2175 (eieiomt-method-list method method-primary nil)))
2176 )
2177
2178 (run-hook-with-args 'eieio-pre-method-execution-hooks
2179 primarymethodlist)
2180
2181 ;; Now loop through all occurrences forms which we must execute
2182 ;; (which are happily sorted now) and execute them all!
2183 (let ((rval nil) (lastval nil) (rvalever nil) (found nil))
2184 (while lambdas
2185 (if (car lambdas)
2186 (let* ((scoped-class (cdr (car lambdas)))
2187 (eieio-generic-call-key (car keys))
2188 (has-return-val
2189 (or (= eieio-generic-call-key method-primary)
2190 (= eieio-generic-call-key method-static)))
2191 (eieio-generic-call-next-method-list
2192 ;; Use the cdr, as the first element is the fcn
2193 ;; we are calling right now.
2194 (when has-return-val (cdr primarymethodlist)))
2195 )
2196 (setq found t)
2197 ;;(setq rval (apply (car (car lambdas)) newargs))
2198 (setq lastval (apply (car (car lambdas)) newargs))
2199 (when has-return-val
2200 (setq rval lastval
2201 rvalever t))
2202 ))
2203 (setq lambdas (cdr lambdas)
2204 keys (cdr keys)))
2205 (if (not found)
2206 (if (eieio-object-p (car args))
2207 (setq rval (apply 'no-applicable-method (car args) method args)
2208 rvalever t)
2209 (signal
2210 'no-method-definition
2211 (list method args))))
2212 ;; Right Here... it could be that lastval is returned when
2213 ;; rvalever is nil. Is that right?
2214 rval)))
2215
2216 (defun eieio-generic-call-primary-only (method args)
2217 "Call METHOD with ARGS for methods with only :PRIMARY implementations.
2218 ARGS provides the context on which implementation to use.
2219 This should only be called from a generic function.
2220
2221 This method is like `eieio-generic-call', but only
2222 implementations in the :PRIMARY slot are queried. After many
2223 years of use, it appears that over 90% of methods in use
2224 have :PRIMARY implementations only. We can therefore optimize
2225 for this common case to improve performance."
2226 ;; We must expand our arguments first as they are always
2227 ;; passed in as quoted symbols
2228 (let ((newargs nil) (mclass nil) (lambdas nil)
2229 (eieio-generic-call-methodname method)
2230 (eieio-generic-call-arglst args)
2231 (firstarg nil)
2232 (primarymethodlist nil)
2233 )
2234 ;; get a copy
2235 (setq newargs args
2236 firstarg (car newargs))
2237
2238 ;; Determine the class to use.
2239 (cond ((eieio-object-p firstarg)
2240 (setq mclass (object-class-fast firstarg)))
2241 ((not firstarg)
2242 (error "Method %s called on nil" method))
2243 ((not (eieio-object-p firstarg))
2244 (error "Primary-only method %s called on something not an object" method))
2245 (t
2246 (error "EIEIO Error: Improperly classified method %s as primary only"
2247 method)
2248 ))
2249 ;; Make sure the class is a valid class
2250 ;; mclass can be nil (meaning a generic for should be used.
2251 ;; mclass cannot have a value that is not a class, however.
2252 (when (null mclass)
2253 (error "Cannot dispatch method %S on class %S" method mclass)
2254 )
2255
2256 ;; :primary methods
2257 (setq lambdas (eieio-generic-form method method-primary mclass))
2258 (setq primarymethodlist ;; Re-use even with bad name here
2259 (eieiomt-method-list method method-primary mclass))
2260
2261 ;; Now loop through all occurrences forms which we must execute
2262 ;; (which are happily sorted now) and execute them all!
2263 (let* ((rval nil) (lastval nil) (rvalever nil)
2264 (scoped-class (cdr lambdas))
2265 (eieio-generic-call-key method-primary)
2266 ;; Use the cdr, as the first element is the fcn
2267 ;; we are calling right now.
2268 (eieio-generic-call-next-method-list (cdr primarymethodlist))
2269 )
2270
2271 (if (or (not lambdas) (not (car lambdas)))
2272
2273 ;; No methods found for this impl...
2274 (if (eieio-object-p (car args))
2275 (setq rval (apply 'no-applicable-method (car args) method args)
2276 rvalever t)
2277 (signal
2278 'no-method-definition
2279 (list method args)))
2280
2281 ;; Do the regular implementation here.
2282
2283 (run-hook-with-args 'eieio-pre-method-execution-hooks
2284 lambdas)
2285
2286 (setq lastval (apply (car lambdas) newargs))
2287 (setq rval lastval
2288 rvalever t)
2289 )
2290
2291 ;; Right Here... it could be that lastval is returned when
2292 ;; rvalever is nil. Is that right?
2293 rval)))
2294
2295 (defun eieiomt-method-list (method key class)
2296 "Return an alist list of methods lambdas.
2297 METHOD is the method name.
2298 KEY represents either :before, or :after methods.
2299 CLASS is the starting class to search from in the method tree.
2300 If CLASS is nil, then an empty list of methods should be returned."
2301 ;; Note: eieiomt - the MT means MethodTree. See more comments below
2302 ;; for the rest of the eieiomt methods.
2303
2304 ;; Collect lambda expressions stored for the class and its parent
2305 ;; classes.
2306 (let (lambdas)
2307 (dolist (ancestor (class-precedence-list class))
2308 ;; Lookup the form to use for the PRIMARY object for the next level
2309 (let ((tmpl (eieio-generic-form method key ancestor)))
2310 (when (and tmpl
2311 (or (not lambdas)
2312 ;; This prevents duplicates coming out of the
2313 ;; class method optimizer. Perhaps we should
2314 ;; just not optimize before/afters?
2315 (not (member tmpl lambdas))))
2316 (push tmpl lambdas))))
2317
2318 ;; Return collected lambda. For :after methods, return in current
2319 ;; order (most general class last); Otherwise, reverse order.
2320 (if (eq key method-after)
2321 lambdas
2322 (nreverse lambdas))))
2323
2324 (defun next-method-p ()
2325 "Return non-nil if there is a next method.
2326 Returns a list of lambda expressions which is the `next-method'
2327 order."
2328 eieio-generic-call-next-method-list)
2329
2330 (defun call-next-method (&rest replacement-args)
2331 "Call the superclass method from a subclass method.
2332 The superclass method is specified in the current method list,
2333 and is called the next method.
2334
2335 If REPLACEMENT-ARGS is non-nil, then use them instead of
2336 `eieio-generic-call-arglst'. The generic arg list are the
2337 arguments passed in at the top level.
2338
2339 Use `next-method-p' to find out if there is a next method to call."
2340 (if (not scoped-class)
2341 (error "`call-next-method' not called within a class specific method"))
2342 (if (and (/= eieio-generic-call-key method-primary)
2343 (/= eieio-generic-call-key method-static))
2344 (error "Cannot `call-next-method' except in :primary or :static methods")
2345 )
2346 (let ((newargs (or replacement-args eieio-generic-call-arglst))
2347 (next (car eieio-generic-call-next-method-list))
2348 )
2349 (if (or (not next) (not (car next)))
2350 (apply 'no-next-method (car newargs) (cdr newargs))
2351 (let* ((eieio-generic-call-next-method-list
2352 (cdr eieio-generic-call-next-method-list))
2353 (eieio-generic-call-arglst newargs)
2354 (scoped-class (cdr next))
2355 (fcn (car next))
2356 )
2357 (apply fcn newargs)
2358 ))))
2359 \f
2360 ;;;
2361 ;; eieio-method-tree : eieiomt-
2362 ;;
2363 ;; Stored as eieio-method-tree in property list of a generic method
2364 ;;
2365 ;; (eieio-method-tree . [BEFORE PRIMARY AFTER
2366 ;; genericBEFORE genericPRIMARY genericAFTER])
2367 ;; and
2368 ;; (eieio-method-obarray . [BEFORE PRIMARY AFTER
2369 ;; genericBEFORE genericPRIMARY genericAFTER])
2370 ;; where the association is a vector.
2371 ;; (aref 0 -- all static methods.
2372 ;; (aref 1 -- all methods classified as :before
2373 ;; (aref 2 -- all methods classified as :primary
2374 ;; (aref 3 -- all methods classified as :after
2375 ;; (aref 4 -- a generic classified as :before
2376 ;; (aref 5 -- a generic classified as :primary
2377 ;; (aref 6 -- a generic classified as :after
2378 ;;
2379 (defvar eieiomt-optimizing-obarray nil
2380 "While mapping atoms, this contain the obarray being optimized.")
2381
2382 (defun eieiomt-install (method-name)
2383 "Install the method tree, and obarray onto METHOD-NAME.
2384 Do not do the work if they already exist."
2385 (let ((emtv (get method-name 'eieio-method-tree))
2386 (emto (get method-name 'eieio-method-obarray)))
2387 (if (or (not emtv) (not emto))
2388 (progn
2389 (setq emtv (put method-name 'eieio-method-tree
2390 (make-vector method-num-slots nil))
2391 emto (put method-name 'eieio-method-obarray
2392 (make-vector method-num-slots nil)))
2393 (aset emto 0 (make-vector 11 0))
2394 (aset emto 1 (make-vector 11 0))
2395 (aset emto 2 (make-vector 41 0))
2396 (aset emto 3 (make-vector 11 0))
2397 ))))
2398
2399 (defun eieiomt-add (method-name method key class)
2400 "Add to METHOD-NAME the forms METHOD in a call position KEY for CLASS.
2401 METHOD-NAME is the name created by a call to `defgeneric'.
2402 METHOD are the forms for a given implementation.
2403 KEY is an integer (see comment in eieio.el near this function) which
2404 is associated with the :static :before :primary and :after tags.
2405 It also indicates if CLASS is defined or not.
2406 CLASS is the class this method is associated with."
2407 (if (or (> key method-num-slots) (< key 0))
2408 (error "eieiomt-add: method key error!"))
2409 (let ((emtv (get method-name 'eieio-method-tree))
2410 (emto (get method-name 'eieio-method-obarray)))
2411 ;; Make sure the method tables are available.
2412 (if (or (not emtv) (not emto))
2413 (error "Programmer error: eieiomt-add"))
2414 ;; only add new cells on if it doesn't already exist!
2415 (if (assq class (aref emtv key))
2416 (setcdr (assq class (aref emtv key)) method)
2417 (aset emtv key (cons (cons class method) (aref emtv key))))
2418 ;; Add function definition into newly created symbol, and store
2419 ;; said symbol in the correct obarray, otherwise use the
2420 ;; other array to keep this stuff
2421 (if (< key method-num-lists)
2422 (let ((nsym (intern (symbol-name class) (aref emto key))))
2423 (fset nsym method)))
2424 ;; Now optimize the entire obarray
2425 (if (< key method-num-lists)
2426 (let ((eieiomt-optimizing-obarray (aref emto key)))
2427 ;; @todo - Is this overkill? Should we just clear the symbol?
2428 (mapatoms 'eieiomt-sym-optimize eieiomt-optimizing-obarray)))
2429 ))
2430
2431 (defun eieiomt-next (class)
2432 "Return the next parent class for CLASS.
2433 If CLASS is a superclass, return variable `eieio-default-superclass'.
2434 If CLASS is variable `eieio-default-superclass' then return nil.
2435 This is different from function `class-parent' as class parent returns
2436 nil for superclasses. This function performs no type checking!"
2437 ;; No type-checking because all calls are made from functions which
2438 ;; are safe and do checking for us.
2439 (or (class-parents-fast class)
2440 (if (eq class 'eieio-default-superclass)
2441 nil
2442 '(eieio-default-superclass))))
2443
2444 (defun eieiomt-sym-optimize (s)
2445 "Find the next class above S which has a function body for the optimizer."
2446 ;; Set the value to nil in case there is no nearest cell.
2447 (set s nil)
2448 ;; Find the nearest cell that has a function body. If we find one,
2449 ;; we replace the nil from above.
2450 (let ((external-symbol (intern-soft (symbol-name s))))
2451 (catch 'done
2452 (dolist (ancestor (rest (class-precedence-list external-symbol)))
2453 (let ((ov (intern-soft (symbol-name ancestor)
2454 eieiomt-optimizing-obarray)))
2455 (when (fboundp ov)
2456 (set s ov) ;; store ov as our next symbol
2457 (throw 'done ancestor)))))))
2458
2459 (defun eieio-generic-form (method key class)
2460 "Return the lambda form belonging to METHOD using KEY based upon CLASS.
2461 If CLASS is not a class then use `generic' instead. If class has
2462 no form, but has a parent class, then trace to that parent class.
2463 The first time a form is requested from a symbol, an optimized path
2464 is memorized for faster future use."
2465 (let ((emto (aref (get method 'eieio-method-obarray)
2466 (if class key (eieio-specialized-key-to-generic-key key)))))
2467 (if (class-p class)
2468 ;; 1) find our symbol
2469 (let ((cs (intern-soft (symbol-name class) emto)))
2470 (if (not cs)
2471 ;; 2) If there isn't one, then make one.
2472 ;; This can be slow since it only occurs once
2473 (progn
2474 (setq cs (intern (symbol-name class) emto))
2475 ;; 2.1) Cache its nearest neighbor with a quick optimize
2476 ;; which should only occur once for this call ever
2477 (let ((eieiomt-optimizing-obarray emto))
2478 (eieiomt-sym-optimize cs))))
2479 ;; 3) If it's bound return this one.
2480 (if (fboundp cs)
2481 (cons cs (aref (class-v class) class-symbol))
2482 ;; 4) If it's not bound then this variable knows something
2483 (if (symbol-value cs)
2484 (progn
2485 ;; 4.1) This symbol holds the next class in its value
2486 (setq class (symbol-value cs)
2487 cs (intern-soft (symbol-name class) emto))
2488 ;; 4.2) The optimizer should always have chosen a
2489 ;; function-symbol
2490 ;;(if (fboundp cs)
2491 (cons cs (aref (class-v (intern (symbol-name class)))
2492 class-symbol))
2493 ;;(error "EIEIO optimizer: erratic data loss!"))
2494 )
2495 ;; There never will be a funcall...
2496 nil)))
2497 ;; for a generic call, what is a list, is the function body we want.
2498 (let ((emtl (aref (get method 'eieio-method-tree)
2499 (if class key (eieio-specialized-key-to-generic-key key)))))
2500 (if emtl
2501 ;; The car of EMTL is supposed to be a class, which in this
2502 ;; case is nil, so skip it.
2503 (cons (cdr (car emtl)) nil)
2504 nil)))))
2505
2506 ;;;
2507 ;; Way to assign slots based on a list. Used for constructors, or
2508 ;; even resetting an object at run-time
2509 ;;
2510 (defun eieio-set-defaults (obj &optional set-all)
2511 "Take object OBJ, and reset all slots to their defaults.
2512 If SET-ALL is non-nil, then when a default is nil, that value is
2513 reset. If SET-ALL is nil, the slots are only reset if the default is
2514 not nil."
2515 (let ((scoped-class (aref obj object-class))
2516 (eieio-initializing-object t)
2517 (pub (aref (class-v (aref obj object-class)) class-public-a)))
2518 (while pub
2519 (let ((df (eieio-oref-default obj (car pub))))
2520 (if (or df set-all)
2521 (eieio-oset obj (car pub) df)))
2522 (setq pub (cdr pub)))))
2523
2524 (defun eieio-initarg-to-attribute (class initarg)
2525 "For CLASS, convert INITARG to the actual attribute name.
2526 If there is no translation, pass it in directly (so we can cheat if
2527 need be... May remove that later...)"
2528 (let ((tuple (assoc initarg (aref (class-v class) class-initarg-tuples))))
2529 (if tuple
2530 (cdr tuple)
2531 nil)))
2532
2533 (defun eieio-attribute-to-initarg (class attribute)
2534 "In CLASS, convert the ATTRIBUTE into the corresponding init argument tag.
2535 This is usually a symbol that starts with `:'."
2536 (let ((tuple (rassoc attribute (aref (class-v class) class-initarg-tuples))))
2537 (if tuple
2538 (car tuple)
2539 nil)))
2540
2541 \f
2542 ;;; Here are some special types of errors
2543 ;;
2544 (intern "no-method-definition")
2545 (put 'no-method-definition 'error-conditions '(no-method-definition error))
2546 (put 'no-method-definition 'error-message "No method definition")
2547
2548 (intern "no-next-method")
2549 (put 'no-next-method 'error-conditions '(no-next-method error))
2550 (put 'no-next-method 'error-message "No next method")
2551
2552 (intern "invalid-slot-name")
2553 (put 'invalid-slot-name 'error-conditions '(invalid-slot-name error))
2554 (put 'invalid-slot-name 'error-message "Invalid slot name")
2555
2556 (intern "invalid-slot-type")
2557 (put 'invalid-slot-type 'error-conditions '(invalid-slot-type error nil))
2558 (put 'invalid-slot-type 'error-message "Invalid slot type")
2559
2560 (intern "unbound-slot")
2561 (put 'unbound-slot 'error-conditions '(unbound-slot error nil))
2562 (put 'unbound-slot 'error-message "Unbound slot")
2563
2564 (intern "inconsistent-class-hierarchy")
2565 (put 'inconsistent-class-hierarchy 'error-conditions
2566 '(inconsistent-class-hierarchy error nil))
2567 (put 'inconsistent-class-hierarchy 'error-message "Inconsistent class hierarchy")
2568
2569 ;;; Here are some CLOS items that need the CL package
2570 ;;
2571
2572 (defsetf slot-value (obj slot) (store) (list 'eieio-oset obj slot store))
2573 (defsetf eieio-oref (obj slot) (store) (list 'eieio-oset obj slot store))
2574
2575 ;; The below setf method was written by Arnd Kohrs <kohrs@acm.org>
2576 (define-setf-method oref (obj slot)
2577 (with-no-warnings
2578 (require 'cl)
2579 (let ((obj-temp (gensym))
2580 (slot-temp (gensym))
2581 (store-temp (gensym)))
2582 (list (list obj-temp slot-temp)
2583 (list obj `(quote ,slot))
2584 (list store-temp)
2585 (list 'set-slot-value obj-temp slot-temp
2586 store-temp)
2587 (list 'slot-value obj-temp slot-temp)))))
2588
2589 \f
2590 ;;;
2591 ;; We want all objects created by EIEIO to have some default set of
2592 ;; behaviours so we can create object utilities, and allow various
2593 ;; types of error checking. To do this, create the default EIEIO
2594 ;; class, and when no parent class is specified, use this as the
2595 ;; default. (But don't store it in the other classes as the default,
2596 ;; allowing for transparent support.)
2597 ;;
2598
2599 (defclass eieio-default-superclass nil
2600 nil
2601 "Default parent class for classes with no specified parent class.
2602 Its slots are automatically adopted by classes with no specified parents.
2603 This class is not stored in the `parent' slot of a class vector."
2604 :abstract t)
2605
2606 (defalias 'standard-class 'eieio-default-superclass)
2607
2608 (defgeneric constructor (class newname &rest slots)
2609 "Default constructor for CLASS `eieio-default-superclass'.")
2610
2611 (defmethod constructor :static
2612 ((class eieio-default-superclass) newname &rest slots)
2613 "Default constructor for CLASS `eieio-default-superclass'.
2614 NEWNAME is the name to be given to the constructed object.
2615 SLOTS are the initialization slots used by `shared-initialize'.
2616 This static method is called when an object is constructed.
2617 It allocates the vector used to represent an EIEIO object, and then
2618 calls `shared-initialize' on that object."
2619 (let* ((new-object (copy-sequence (aref (class-v class)
2620 class-default-object-cache))))
2621 ;; Update the name for the newly created object.
2622 (aset new-object object-name newname)
2623 ;; Call the initialize method on the new object with the slots
2624 ;; that were passed down to us.
2625 (initialize-instance new-object slots)
2626 ;; Return the created object.
2627 new-object))
2628
2629 (defgeneric shared-initialize (obj slots)
2630 "Set slots of OBJ with SLOTS which is a list of name/value pairs.
2631 Called from the constructor routine.")
2632
2633 (defmethod shared-initialize ((obj eieio-default-superclass) slots)
2634 "Set slots of OBJ with SLOTS which is a list of name/value pairs.
2635 Called from the constructor routine."
2636 (let ((scoped-class (aref obj object-class)))
2637 (while slots
2638 (let ((rn (eieio-initarg-to-attribute (object-class-fast obj)
2639 (car slots))))
2640 (if (not rn)
2641 (slot-missing obj (car slots) 'oset (car (cdr slots)))
2642 (eieio-oset obj rn (car (cdr slots)))))
2643 (setq slots (cdr (cdr slots))))))
2644
2645 (defgeneric initialize-instance (this &optional slots)
2646 "Construct the new object THIS based on SLOTS.")
2647
2648 (defmethod initialize-instance ((this eieio-default-superclass)
2649 &optional slots)
2650 "Construct the new object THIS based on SLOTS.
2651 SLOTS is a tagged list where odd numbered elements are tags, and
2652 even numbered elements are the values to store in the tagged slot.
2653 If you overload the `initialize-instance', there you will need to
2654 call `shared-initialize' yourself, or you can call `call-next-method'
2655 to have this constructor called automatically. If these steps are
2656 not taken, then new objects of your class will not have their values
2657 dynamically set from SLOTS."
2658 ;; First, see if any of our defaults are `lambda', and
2659 ;; re-evaluate them and apply the value to our slots.
2660 (let* ((scoped-class (class-v (aref this object-class)))
2661 (slot (aref scoped-class class-public-a))
2662 (defaults (aref scoped-class class-public-d)))
2663 (while slot
2664 ;; For each slot, see if we need to evaluate it.
2665 ;;
2666 ;; Paul Landes said in an email:
2667 ;; > CL evaluates it if it can, and otherwise, leaves it as
2668 ;; > the quoted thing as you already have. This is by the
2669 ;; > Sonya E. Keene book and other things I've look at on the
2670 ;; > web.
2671 (let ((dflt (eieio-default-eval-maybe (car defaults))))
2672 (when (not (eq dflt (car defaults)))
2673 (eieio-oset this (car slot) dflt) ))
2674 ;; Next.
2675 (setq slot (cdr slot)
2676 defaults (cdr defaults))))
2677 ;; Shared initialize will parse our slots for us.
2678 (shared-initialize this slots))
2679
2680 (defgeneric slot-missing (object slot-name operation &optional new-value)
2681 "Method invoked when an attempt to access a slot in OBJECT fails.")
2682
2683 (defmethod slot-missing ((object eieio-default-superclass) slot-name
2684 operation &optional new-value)
2685 "Method invoked when an attempt to access a slot in OBJECT fails.
2686 SLOT-NAME is the name of the failed slot, OPERATION is the type of access
2687 that was requested, and optional NEW-VALUE is the value that was desired
2688 to be set.
2689
2690 This method is called from `oref', `oset', and other functions which
2691 directly reference slots in EIEIO objects."
2692 (signal 'invalid-slot-name (list (object-name object)
2693 slot-name)))
2694
2695 (defgeneric slot-unbound (object class slot-name fn)
2696 "Slot unbound is invoked during an attempt to reference an unbound slot.")
2697
2698 (defmethod slot-unbound ((object eieio-default-superclass)
2699 class slot-name fn)
2700 "Slot unbound is invoked during an attempt to reference an unbound slot.
2701 OBJECT is the instance of the object being reference. CLASS is the
2702 class of OBJECT, and SLOT-NAME is the offending slot. This function
2703 throws the signal `unbound-slot'. You can overload this function and
2704 return the value to use in place of the unbound value.
2705 Argument FN is the function signaling this error.
2706 Use `slot-boundp' to determine if a slot is bound or not.
2707
2708 In CLOS, the argument list is (CLASS OBJECT SLOT-NAME), but
2709 EIEIO can only dispatch on the first argument, so the first two are swapped."
2710 (signal 'unbound-slot (list (class-name class) (object-name object)
2711 slot-name fn)))
2712
2713 (defgeneric no-applicable-method (object method &rest args)
2714 "Called if there are no implementations for OBJECT in METHOD.")
2715
2716 (defmethod no-applicable-method ((object eieio-default-superclass)
2717 method &rest args)
2718 "Called if there are no implementations for OBJECT in METHOD.
2719 OBJECT is the object which has no method implementation.
2720 ARGS are the arguments that were passed to METHOD.
2721
2722 Implement this for a class to block this signal. The return
2723 value becomes the return value of the original method call."
2724 (signal 'no-method-definition (list method (object-name object)))
2725 )
2726
2727 (defgeneric no-next-method (object &rest args)
2728 "Called from `call-next-method' when no additional methods are available.")
2729
2730 (defmethod no-next-method ((object eieio-default-superclass)
2731 &rest args)
2732 "Called from `call-next-method' when no additional methods are available.
2733 OBJECT is othe object being called on `call-next-method'.
2734 ARGS are the arguments it is called by.
2735 This method signals `no-next-method' by default. Override this
2736 method to not throw an error, and its return value becomes the
2737 return value of `call-next-method'."
2738 (signal 'no-next-method (list (object-name object) args))
2739 )
2740
2741 (defgeneric clone (obj &rest params)
2742 "Make a copy of OBJ, and then supply PARAMS.
2743 PARAMS is a parameter list of the same form used by `initialize-instance'.
2744
2745 When overloading `clone', be sure to call `call-next-method'
2746 first and modify the returned object.")
2747
2748 (defmethod clone ((obj eieio-default-superclass) &rest params)
2749 "Make a copy of OBJ, and then apply PARAMS."
2750 (let ((nobj (copy-sequence obj))
2751 (nm (aref obj object-name))
2752 (passname (and params (stringp (car params))))
2753 (num 1))
2754 (if params (shared-initialize nobj (if passname (cdr params) params)))
2755 (if (not passname)
2756 (save-match-data
2757 (if (string-match "-\\([0-9]+\\)" nm)
2758 (setq num (1+ (string-to-number (match-string 1 nm)))
2759 nm (substring nm 0 (match-beginning 0))))
2760 (aset nobj object-name (concat nm "-" (int-to-string num))))
2761 (aset nobj object-name (car params)))
2762 nobj))
2763
2764 (defgeneric destructor (this &rest params)
2765 "Destructor for cleaning up any dynamic links to our object.")
2766
2767 (defmethod destructor ((this eieio-default-superclass) &rest params)
2768 "Destructor for cleaning up any dynamic links to our object.
2769 Argument THIS is the object being destroyed. PARAMS are additional
2770 ignored parameters."
2771 ;; No cleanup... yet.
2772 )
2773
2774 (defgeneric object-print (this &rest strings)
2775 "Pretty printer for object THIS. Call function `object-name' with STRINGS.
2776
2777 It is sometimes useful to put a summary of the object into the
2778 default #<notation> string when using EIEIO browsing tools.
2779 Implement this method to customize the summary.")
2780
2781 (defmethod object-print ((this eieio-default-superclass) &rest strings)
2782 "Pretty printer for object THIS. Call function `object-name' with STRINGS.
2783 The default method for printing object THIS is to use the
2784 function `object-name'.
2785
2786 It is sometimes useful to put a summary of the object into the
2787 default #<notation> string when using EIEIO browsing tools.
2788
2789 Implement this function and specify STRINGS in a call to
2790 `call-next-method' to provide additional summary information.
2791 When passing in extra strings from child classes, always remember
2792 to prepend a space."
2793 (object-name this (apply 'concat strings)))
2794
2795 (defvar eieio-print-depth 0
2796 "When printing, keep track of the current indentation depth.")
2797
2798 (defgeneric object-write (this &optional comment)
2799 "Write out object THIS to the current stream.
2800 Optional COMMENT will add comments to the beginning of the output.")
2801
2802 (defmethod object-write ((this eieio-default-superclass) &optional comment)
2803 "Write object THIS out to the current stream.
2804 This writes out the vector version of this object. Complex and recursive
2805 object are discouraged from being written.
2806 If optional COMMENT is non-nil, include comments when outputting
2807 this object."
2808 (when comment
2809 (princ ";; Object ")
2810 (princ (object-name-string this))
2811 (princ "\n")
2812 (princ comment)
2813 (princ "\n"))
2814 (let* ((cl (object-class this))
2815 (cv (class-v cl)))
2816 ;; Now output readable lisp to recreate this object
2817 ;; It should look like this:
2818 ;; (<constructor> <name> <slot> <slot> ... )
2819 ;; Each slot's slot is writen using its :writer.
2820 (princ (make-string (* eieio-print-depth 2) ? ))
2821 (princ "(")
2822 (princ (symbol-name (class-constructor (object-class this))))
2823 (princ " \"")
2824 (princ (object-name-string this))
2825 (princ "\"\n")
2826 ;; Loop over all the public slots
2827 (let ((publa (aref cv class-public-a))
2828 (publd (aref cv class-public-d))
2829 (publp (aref cv class-public-printer))
2830 (eieio-print-depth (1+ eieio-print-depth)))
2831 (while publa
2832 (when (slot-boundp this (car publa))
2833 (let ((i (class-slot-initarg cl (car publa)))
2834 (v (eieio-oref this (car publa)))
2835 )
2836 (unless (or (not i) (equal v (car publd)))
2837 (princ (make-string (* eieio-print-depth 2) ? ))
2838 (princ (symbol-name i))
2839 (princ " ")
2840 (if (car publp)
2841 ;; Use our public printer
2842 (funcall (car publp) v)
2843 ;; Use our generic override prin1 function.
2844 (eieio-override-prin1 v))
2845 (princ "\n"))))
2846 (setq publa (cdr publa) publd (cdr publd)
2847 publp (cdr publp)))
2848 (princ (make-string (* eieio-print-depth 2) ? )))
2849 (princ ")\n")))
2850
2851 (defun eieio-override-prin1 (thing)
2852 "Perform a `prin1' on THING taking advantage of object knowledge."
2853 (cond ((eieio-object-p thing)
2854 (object-write thing))
2855 ((listp thing)
2856 (eieio-list-prin1 thing))
2857 ((class-p thing)
2858 (princ (class-name thing)))
2859 ((symbolp thing)
2860 (princ (concat "'" (symbol-name thing))))
2861 (t (prin1 thing))))
2862
2863 (defun eieio-list-prin1 (list)
2864 "Display LIST where list may contain objects."
2865 (if (not (eieio-object-p (car list)))
2866 (progn
2867 (princ "'")
2868 (prin1 list))
2869 (princ "(list ")
2870 (if (eieio-object-p (car list)) (princ "\n "))
2871 (while list
2872 (if (eieio-object-p (car list))
2873 (object-write (car list))
2874 (princ "'")
2875 (prin1 (car list)))
2876 (princ " ")
2877 (setq list (cdr list)))
2878 (princ (make-string (* eieio-print-depth 2) ? ))
2879 (princ ")")))
2880
2881 \f
2882 ;;; Unimplemented functions from CLOS
2883 ;;
2884 (defun change-class (obj class)
2885 "Change the class of OBJ to type CLASS.
2886 This may create or delete slots, but does not affect the return value
2887 of `eq'."
2888 (error "EIEIO: `change-class' is unimplemented"))
2889
2890 )
2891
2892 \f
2893 ;;; Interfacing with edebug
2894 ;;
2895 (defun eieio-edebug-prin1-to-string (object &optional noescape)
2896 "Display EIEIO OBJECT in fancy format.
2897 Overrides the edebug default.
2898 Optional argument NOESCAPE is passed to `prin1-to-string' when appropriate."
2899 (cond ((class-p object) (class-name object))
2900 ((eieio-object-p object) (object-print object))
2901 ((and (listp object) (or (class-p (car object))
2902 (eieio-object-p (car object))))
2903 (concat "(" (mapconcat 'eieio-edebug-prin1-to-string object " ") ")"))
2904 (t (prin1-to-string object noescape))))
2905
2906 (add-hook 'edebug-setup-hook
2907 (lambda ()
2908 (def-edebug-spec defmethod
2909 (&define ; this means we are defining something
2910 [&or name ("setf" :name setf name)]
2911 ;; ^^ This is the methods symbol
2912 [ &optional symbolp ] ; this is key :before etc
2913 list ; arguments
2914 [ &optional stringp ] ; documentation string
2915 def-body ; part to be debugged
2916 ))
2917 ;; The rest of the macros
2918 (def-edebug-spec oref (form quote))
2919 (def-edebug-spec oref-default (form quote))
2920 (def-edebug-spec oset (form quote form))
2921 (def-edebug-spec oset-default (form quote form))
2922 (def-edebug-spec class-v form)
2923 (def-edebug-spec class-p form)
2924 (def-edebug-spec eieio-object-p form)
2925 (def-edebug-spec class-constructor form)
2926 (def-edebug-spec generic-p form)
2927 (def-edebug-spec with-slots (list list def-body))
2928 ;; I suspect this isn't the best way to do this, but when
2929 ;; cust-print was used on my system all my objects
2930 ;; appeared as "#1 =" which was not useful. This allows
2931 ;; edebug to print my objects in the nice way they were
2932 ;; meant to with `object-print' and `class-name'
2933 ;; (defalias 'edebug-prin1-to-string 'eieio-edebug-prin1-to-string)
2934 )
2935 )
2936
2937 ;;; Interfacing with imenu in emacs lisp mode
2938 ;; (Only if the expression is defined)
2939 ;;
2940 (if (eval-when-compile (boundp 'list-imenu-generic-expression))
2941 (progn
2942
2943 (defun eieio-update-lisp-imenu-expression ()
2944 "Examine `lisp-imenu-generic-expression' and modify it to find `defmethod'."
2945 (let ((exp lisp-imenu-generic-expression))
2946 (while exp
2947 ;; it's of the form '( ( title expr indx ) ... )
2948 (let* ((subcar (cdr (car exp)))
2949 (substr (car subcar)))
2950 (if (and (not (string-match "|method\\\\" substr))
2951 (string-match "|advice\\\\" substr))
2952 (setcar subcar
2953 (replace-match "|advice\\|method\\" t t substr 0))))
2954 (setq exp (cdr exp)))))
2955
2956 (eieio-update-lisp-imenu-expression)
2957
2958 ))
2959
2960 ;;; Autoloading some external symbols, and hooking into the help system
2961 ;;
2962
2963 \f
2964 ;;; Start of automatically extracted autoloads.
2965 \f
2966 ;;;### (autoloads (customize-object) "eieio-custom" "eieio-custom.el"
2967 ;;;;;; "cf1bd64c76a6e6406545e8c5a5530d43")
2968 ;;; Generated autoloads from eieio-custom.el
2969
2970 (autoload 'customize-object "eieio-custom" "\
2971 Customize OBJ in a custom buffer.
2972 Optional argument GROUP is the sub-group of slots to display.
2973
2974 \(fn OBJ &optional GROUP)" nil nil)
2975
2976 ;;;***
2977 \f
2978 ;;;### (autoloads (eieio-help-mode-augmentation-maybee eieio-describe-generic
2979 ;;;;;; eieio-describe-constructor eieio-describe-class eieio-browse)
2980 ;;;;;; "eieio-opt" "eieio-opt.el" "1bed0a56310f402683419139ebc18d7f")
2981 ;;; Generated autoloads from eieio-opt.el
2982
2983 (autoload 'eieio-browse "eieio-opt" "\
2984 Create an object browser window to show all objects.
2985 If optional ROOT-CLASS, then start with that, otherwise start with
2986 variable `eieio-default-superclass'.
2987
2988 \(fn &optional ROOT-CLASS)" t nil)
2989
2990 (defalias 'describe-class 'eieio-describe-class)
2991
2992 (autoload 'eieio-describe-class "eieio-opt" "\
2993 Describe a CLASS defined by a string or symbol.
2994 If CLASS is actually an object, then also display current values of that object.
2995 Optional HEADERFCN should be called to insert a few bits of info first.
2996
2997 \(fn CLASS &optional HEADERFCN)" t nil)
2998
2999 (autoload 'eieio-describe-constructor "eieio-opt" "\
3000 Describe the constructor function FCN.
3001 Uses `eieio-describe-class' to describe the class being constructed.
3002
3003 \(fn FCN)" t nil)
3004
3005 (defalias 'describe-generic 'eieio-describe-generic)
3006
3007 (autoload 'eieio-describe-generic "eieio-opt" "\
3008 Describe the generic function GENERIC.
3009 Also extracts information about all methods specific to this generic.
3010
3011 \(fn GENERIC)" t nil)
3012
3013 (autoload 'eieio-help-mode-augmentation-maybee "eieio-opt" "\
3014 For buffers thrown into help mode, augment for EIEIO.
3015 Arguments UNUSED are not used.
3016
3017 \(fn &rest UNUSED)" nil nil)
3018
3019 ;;;***
3020 \f
3021 ;;; End of automatically extracted autoloads.
3022
3023 (provide 'eieio)
3024
3025 ;;; eieio ends here