e7ec547af9b09e3e52293b6c5ee5a23a1316395c
[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 Interpreted Objects
3
4 ;; Copyright (C) 1995-1996, 1998-2013 Free Software Foundation, Inc.
5
6 ;; Author: Eric M. Ludlam <zappo@gnu.org>
7 ;; Version: 1.4
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 (require 'cl)) ;FIXME: Use cl-lib!
48
49 (defvar eieio-version "1.4"
50 "Current version of EIEIO.")
51
52 (defun eieio-version ()
53 "Display the current version of EIEIO."
54 (interactive)
55 (message eieio-version))
56
57 (require 'eieio-core)
58
59 \f
60 ;;; Defining a new class
61 ;;
62 (defmacro defclass (name superclass slots &rest options-and-doc)
63 "Define NAME as a new class derived from SUPERCLASS with SLOTS.
64 OPTIONS-AND-DOC is used as the class' options and base documentation.
65 SUPERCLASS is a list of superclasses to inherit from, with SLOTS
66 being the slots residing in that class definition. NOTE: Currently
67 only one slot may exist in SUPERCLASS as multiple inheritance is not
68 yet supported. Supported tags are:
69
70 :initform - Initializing form.
71 :initarg - Tag used during initialization.
72 :accessor - Tag used to create a function to access this slot.
73 :allocation - Specify where the value is stored.
74 Defaults to `:instance', but could also be `:class'.
75 :writer - A function symbol which will `write' an object's slot.
76 :reader - A function symbol which will `read' an object.
77 :type - The type of data allowed in this slot (see `typep').
78 :documentation
79 - A string documenting use of this slot.
80
81 The following are extensions on CLOS:
82 :protection - Specify protection for this slot.
83 Defaults to `:public'. Also use `:protected', or `:private'.
84 :custom - When customizing an object, the custom :type. Public only.
85 :label - A text string label used for a slot when customizing.
86 :group - Name of a customization group this slot belongs in.
87 :printer - A function to call to print the value of a slot.
88 See `eieio-override-prin1' as an example.
89
90 A class can also have optional options. These options happen in place
91 of documentation (including a :documentation tag), in addition to
92 documentation, or not at all. Supported options are:
93
94 :documentation - The doc-string used for this class.
95
96 Options added to EIEIO:
97
98 :allow-nil-initform - Non-nil to skip typechecking of null initforms.
99 :custom-groups - List of custom group names. Organizes slots into
100 reasonable groups for customizations.
101 :abstract - Non-nil to prevent instances of this class.
102 If a string, use as an error string if someone does
103 try to make an instance.
104 :method-invocation-order
105 - Control the method invocation order if there is
106 multiple inheritance. Valid values are:
107 :breadth-first - The default.
108 :depth-first
109
110 Options in CLOS not supported in EIEIO:
111
112 :metaclass - Class to use in place of `standard-class'
113 :default-initargs - Initargs to use when initializing new objects of
114 this class.
115
116 Due to the way class options are set up, you can add any tags you wish,
117 and reference them using the function `class-option'."
118 ;; This is eval-and-compile only to silence spurious compiler warnings
119 ;; about functions and variables not known to be defined.
120 ;; When eieio-defclass code is merged here and this becomes
121 ;; transparent to the compiler, the eval-and-compile can be removed.
122 `(eval-and-compile
123 (eieio-defclass ',name ',superclass ',slots ',options-and-doc)))
124
125
126 ;;; CLOS style implementation of object creators.
127 ;;
128 (defun make-instance (class &rest initargs)
129 "Make a new instance of CLASS based on INITARGS.
130 CLASS is a class symbol. For example:
131
132 (make-instance 'foo)
133
134 INITARGS is a property list with keywords based on the :initarg
135 for each slot. For example:
136
137 (make-instance 'foo :slot1 value1 :slotN valueN)
138
139 Compatibility note:
140
141 If the first element of INITARGS is a string, it is used as the
142 name of the class.
143
144 In EIEIO, the class' constructor requires a name for use when printing.
145 `make-instance' in CLOS doesn't use names the way Emacs does, so the
146 class is used as the name slot instead when INITARGS doesn't start with
147 a string."
148 (if (and (car initargs) (stringp (car initargs)))
149 (apply (class-constructor class) initargs)
150 (apply (class-constructor class)
151 (cond ((symbolp class) (symbol-name class))
152 (t (format "%S" class)))
153 initargs)))
154
155 \f
156 ;;; CLOS methods and generics
157 ;;
158 (defmacro defgeneric (method args &optional doc-string)
159 "Create a generic function METHOD.
160 DOC-STRING is the base documentation for this class. A generic
161 function has no body, as its purpose is to decide which method body
162 is appropriate to use. Uses `defmethod' to create methods, and calls
163 `defgeneric' for you. With this implementation the ARGS are
164 currently ignored. You can use `defgeneric' to apply specialized
165 top level documentation to a method."
166 `(eieio--defalias ',method
167 (eieio--defgeneric-init-form ',method ,doc-string)))
168
169 (defmacro defmethod (method &rest args)
170 "Create a new METHOD through `defgeneric' with ARGS.
171
172 The optional second argument KEY is a specifier that
173 modifies how the method is called, including:
174 :before - Method will be called before the :primary
175 :primary - The default if not specified
176 :after - Method will be called after the :primary
177 :static - First arg could be an object or class
178 The next argument is the ARGLIST. The ARGLIST specifies the arguments
179 to the method as with `defun'. The first argument can have a type
180 specifier, such as:
181 ((VARNAME CLASS) ARG2 ...)
182 where VARNAME is the name of the local variable for the method being
183 created. The CLASS is a class symbol for a class made with `defclass'.
184 A DOCSTRING comes after the ARGLIST, and is optional.
185 All the rest of the args are the BODY of the method. A method will
186 return the value of the last form in the BODY.
187
188 Summary:
189
190 (defmethod mymethod [:before | :primary | :after | :static]
191 ((typearg class-name) arg2 &optional opt &rest rest)
192 \"doc-string\"
193 body)"
194 (let* ((key (if (keywordp (car args)) (pop args)))
195 (params (car args))
196 (arg1 (car params))
197 (fargs (if (consp arg1)
198 (cons (car arg1) (cdr params))
199 params))
200 (class (if (consp arg1) (nth 1 arg1)))
201 (code `(lambda ,fargs ,@(cdr args))))
202 `(progn
203 ;; Make sure there is a generic and the byte-compiler sees it.
204 (defgeneric ,method ,args
205 ,(or (documentation code)
206 (format "Generically created method `%s'." method)))
207 (eieio--defmethod ',method ',key ',class #',code))))
208
209 ;;; Get/Set slots in an object.
210 ;;
211 (defmacro oref (obj slot)
212 "Retrieve the value stored in OBJ in the slot named by SLOT.
213 Slot is the name of the slot when created by `defclass' or the label
214 created by the :initarg tag."
215 `(eieio-oref ,obj (quote ,slot)))
216
217 (defalias 'slot-value 'eieio-oref)
218 (defalias 'set-slot-value 'eieio-oset)
219
220 (defmacro oref-default (obj slot)
221 "Get the default value of OBJ (maybe a class) for SLOT.
222 The default value is the value installed in a class with the :initform
223 tag. SLOT can be the slot name, or the tag specified by the :initarg
224 tag in the `defclass' call."
225 `(eieio-oref-default ,obj (quote ,slot)))
226
227 ;;; Handy CLOS macros
228 ;;
229 (defmacro with-slots (spec-list object &rest body)
230 "Bind SPEC-LIST lexically to slot values in OBJECT, and execute BODY.
231 This establishes a lexical environment for referring to the slots in
232 the instance named by the given slot-names as though they were
233 variables. Within such a context the value of the slot can be
234 specified by using its slot name, as if it were a lexically bound
235 variable. Both setf and setq can be used to set the value of the
236 slot.
237
238 SPEC-LIST is of a form similar to `let'. For example:
239
240 ((VAR1 SLOT1)
241 SLOT2
242 SLOTN
243 (VARN+1 SLOTN+1))
244
245 Where each VAR is the local variable given to the associated
246 SLOT. A slot specified without a variable name is given a
247 variable name of the same name as the slot."
248 (declare (indent 2))
249 ;; Transform the spec-list into a symbol-macrolet spec-list.
250 (let ((mappings (mapcar (lambda (entry)
251 (let ((var (if (listp entry) (car entry) entry))
252 (slot (if (listp entry) (cadr entry) entry)))
253 (list var `(slot-value ,object ',slot))))
254 spec-list)))
255 (append (list 'symbol-macrolet mappings)
256 body)))
257 \f
258 ;;; Simple generators, and query functions. None of these would do
259 ;; well embedded into an object.
260 ;;
261 (define-obsolete-function-alias
262 'object-class-fast #'eieio--object-class "24.4")
263
264 (defun eieio-object-name (obj &optional extra)
265 "Return a Lisp like symbol string for object OBJ.
266 If EXTRA, include that in the string returned to represent the symbol."
267 (eieio--check-type eieio-object-p obj)
268 (format "#<%s %s%s>" (symbol-name (eieio--object-class obj))
269 (eieio--object-name obj) (or extra "")))
270 (define-obsolete-function-alias 'object-name #'eieio-object-name "24.4")
271
272 (defun eieio-object-name-string (obj) "Return a string which is OBJ's name."
273 (eieio--check-type eieio-object-p obj)
274 (eieio--object-name obj))
275 (define-obsolete-function-alias
276 'object-name-string #'eieio-object-name-string "24.4")
277
278 (defun eieio-object-set-name-string (obj name)
279 "Set the string which is OBJ's NAME."
280 (eieio--check-type eieio-object-p obj)
281 (eieio--check-type stringp name)
282 (setf (eieio--object-name obj) name))
283 (define-obsolete-function-alias
284 'object-set-name-string 'eieio-object-set-name-string "24.4")
285
286 (defun eieio-object-class (obj) "Return the class struct defining OBJ."
287 (eieio--check-type eieio-object-p obj)
288 (eieio--object-class obj))
289 (define-obsolete-function-alias 'object-class #'eieio-object-class "24.4")
290 ;; CLOS name, maybe?
291 (define-obsolete-function-alias 'class-of #'eieio-object-class "24.4")
292
293 (defun eieio-object-class-name (obj)
294 "Return a Lisp like symbol name for OBJ's class."
295 (eieio--check-type eieio-object-p obj)
296 (eieio-class-name (eieio--object-class obj)))
297 (define-obsolete-function-alias
298 'object-class-name 'eieio-object-class-name "24.4")
299
300 (defun eieio-class-parents (class)
301 "Return parent classes to CLASS. (overload of variable).
302
303 The CLOS function `class-direct-superclasses' is aliased to this function."
304 (eieio--check-type class-p class)
305 (eieio-class-parents-fast class))
306 (define-obsolete-function-alias 'class-parents #'eieio-class-parents "24.4")
307
308 (defun eieio-class-children (class)
309 "Return child classes to CLASS.
310 The CLOS function `class-direct-subclasses' is aliased to this function."
311 (eieio--check-type class-p class)
312 (eieio-class-children-fast class))
313 (define-obsolete-function-alias
314 'class-children #'eieio-class-children "24.4")
315
316 ;; Official CLOS functions.
317 (define-obsolete-function-alias
318 'class-direct-superclasses #'eieio-class-parents "24.4")
319 (define-obsolete-function-alias
320 'class-direct-subclasses #'eieio-class-children "24.4")
321
322 (defmacro eieio-class-parent (class)
323 "Return first parent class to CLASS. (overload of variable)."
324 `(car (eieio-class-parents ,class)))
325
326 (defmacro class-parent (class)
327 (declare (obsolete eieio-class-parent "24.4"))
328 '(eieio-class-parent class))
329
330 (defun same-class-p (obj class) "Return t if OBJ is of class-type CLASS."
331 (eieio--check-type class-p class)
332 (eieio--check-type eieio-object-p obj)
333 (same-class-fast-p obj class))
334
335 (defun object-of-class-p (obj class)
336 "Return non-nil if OBJ is an instance of CLASS or CLASS' subclasses."
337 (eieio--check-type eieio-object-p obj)
338 ;; class will be checked one layer down
339 (child-of-class-p (eieio--object-class obj) class))
340 ;; Backwards compatibility
341 (defalias 'obj-of-class-p 'object-of-class-p)
342
343 (defun child-of-class-p (child class)
344 "Return non-nil if CHILD class is a subclass of CLASS."
345 (eieio--check-type class-p class)
346 (eieio--check-type class-p child)
347 (let ((p nil))
348 (while (and child (not (eq child class)))
349 (setq p (append p (eieio--class-parent (class-v child)))
350 child (car p)
351 p (cdr p)))
352 (if child t)))
353
354 (defun object-slots (obj)
355 "Return list of slots available in OBJ."
356 (eieio--check-type eieio-object-p obj)
357 (eieio--class-public-a (class-v (eieio--object-class obj))))
358
359 (defun class-slot-initarg (class slot) "Fetch from CLASS, SLOT's :initarg."
360 (eieio--check-type class-p class)
361 (let ((ia (eieio--class-initarg-tuples (class-v class)))
362 (f nil))
363 (while (and ia (not f))
364 (if (eq (cdr (car ia)) slot)
365 (setq f (car (car ia))))
366 (setq ia (cdr ia)))
367 f))
368
369 ;;; Object Set macros
370 ;;
371 (defmacro oset (obj slot value)
372 "Set the value in OBJ for slot SLOT to VALUE.
373 SLOT is the slot name as specified in `defclass' or the tag created
374 with in the :initarg slot. VALUE can be any Lisp object."
375 `(eieio-oset ,obj (quote ,slot) ,value))
376
377 (defmacro oset-default (class slot value)
378 "Set the default slot in CLASS for SLOT to VALUE.
379 The default value is usually set with the :initform tag during class
380 creation. This allows users to change the default behavior of classes
381 after they are created."
382 `(eieio-oset-default ,class (quote ,slot) ,value))
383
384 ;;; CLOS queries into classes and slots
385 ;;
386 (defun slot-boundp (object slot)
387 "Return non-nil if OBJECT's SLOT is bound.
388 Setting a slot's value makes it bound. Calling `slot-makeunbound' will
389 make a slot unbound.
390 OBJECT can be an instance or a class."
391 ;; Skip typechecking while retrieving this value.
392 (let ((eieio-skip-typecheck t))
393 ;; Return nil if the magic symbol is in there.
394 (not (eq (cond
395 ((eieio-object-p object) (eieio-oref object slot))
396 ((class-p object) (eieio-oref-default object slot))
397 (t (signal 'wrong-type-argument (list 'eieio-object-p object))))
398 eieio-unbound))))
399
400 (defun slot-makeunbound (object slot)
401 "In OBJECT, make SLOT unbound."
402 (eieio-oset object slot eieio-unbound))
403
404 (defun slot-exists-p (object-or-class slot)
405 "Return non-nil if OBJECT-OR-CLASS has SLOT."
406 (let ((cv (class-v (cond ((eieio-object-p object-or-class)
407 (eieio-object-class object-or-class))
408 ((class-p object-or-class)
409 object-or-class))
410 )))
411 (or (memq slot (eieio--class-public-a cv))
412 (memq slot (eieio--class-class-allocation-a cv)))
413 ))
414
415 (defun find-class (symbol &optional errorp)
416 "Return the class that SYMBOL represents.
417 If there is no class, nil is returned if ERRORP is nil.
418 If ERRORP is non-nil, `wrong-argument-type' is signaled."
419 (if (not (class-p symbol))
420 (if errorp (signal 'wrong-type-argument (list 'class-p symbol))
421 nil)
422 (class-v symbol)))
423
424 ;;; Slightly more complex utility functions for objects
425 ;;
426 (defun object-assoc (key slot list)
427 "Return an object if KEY is `equal' to SLOT's value of an object in LIST.
428 LIST is a list of objects whose slots are searched.
429 Objects in LIST do not need to have a slot named SLOT, nor does
430 SLOT need to be bound. If these errors occur, those objects will
431 be ignored."
432 (eieio--check-type listp list)
433 (while (and list (not (condition-case nil
434 ;; This prevents errors for missing slots.
435 (equal key (eieio-oref (car list) slot))
436 (error nil))))
437 (setq list (cdr list)))
438 (car list))
439
440 (defun object-assoc-list (slot list)
441 "Return an association list with the contents of SLOT as the key element.
442 LIST must be a list of objects with SLOT in it.
443 This is useful when you need to do completing read on an object group."
444 (eieio--check-type listp list)
445 (let ((assoclist nil))
446 (while list
447 (setq assoclist (cons (cons (eieio-oref (car list) slot)
448 (car list))
449 assoclist))
450 (setq list (cdr list)))
451 (nreverse assoclist)))
452
453 (defun object-assoc-list-safe (slot list)
454 "Return an association list with the contents of SLOT as the key element.
455 LIST must be a list of objects, but those objects do not need to have
456 SLOT in it. If it does not, then that element is left out of the association
457 list."
458 (eieio--check-type listp list)
459 (let ((assoclist nil))
460 (while list
461 (if (slot-exists-p (car list) slot)
462 (setq assoclist (cons (cons (eieio-oref (car list) slot)
463 (car list))
464 assoclist)))
465 (setq list (cdr list)))
466 (nreverse assoclist)))
467
468 (defun object-add-to-list (object slot item &optional append)
469 "In OBJECT's SLOT, add ITEM to the list of elements.
470 Optional argument APPEND indicates we need to append to the list.
471 If ITEM already exists in the list in SLOT, then it is not added.
472 Comparison is done with `equal' through the `member' function call.
473 If SLOT is unbound, bind it to the list containing ITEM."
474 (let (ov)
475 ;; Find the originating list.
476 (if (not (slot-boundp object slot))
477 (setq ov (list item))
478 (setq ov (eieio-oref object slot))
479 ;; turn it into a list.
480 (unless (listp ov)
481 (setq ov (list ov)))
482 ;; Do the combination
483 (if (not (member item ov))
484 (setq ov
485 (if append
486 (append ov (list item))
487 (cons item ov)))))
488 ;; Set back into the slot.
489 (eieio-oset object slot ov)))
490
491 (defun object-remove-from-list (object slot item)
492 "In OBJECT's SLOT, remove occurrences of ITEM.
493 Deletion is done with `delete', which deletes by side effect,
494 and comparisons are done with `equal'.
495 If SLOT is unbound, do nothing."
496 (if (not (slot-boundp object slot))
497 nil
498 (eieio-oset object slot (delete item (eieio-oref object slot)))))
499
500 ;;;
501 ;; Method Calling Functions
502
503 (defun next-method-p ()
504 "Return non-nil if there is a next method.
505 Returns a list of lambda expressions which is the `next-method'
506 order."
507 eieio-generic-call-next-method-list)
508
509 (defun call-next-method (&rest replacement-args)
510 "Call the superclass method from a subclass method.
511 The superclass method is specified in the current method list,
512 and is called the next method.
513
514 If REPLACEMENT-ARGS is non-nil, then use them instead of
515 `eieio-generic-call-arglst'. The generic arg list are the
516 arguments passed in at the top level.
517
518 Use `next-method-p' to find out if there is a next method to call."
519 (if (not (eieio--scoped-class))
520 (error "`call-next-method' not called within a class specific method"))
521 (if (and (/= eieio-generic-call-key method-primary)
522 (/= eieio-generic-call-key method-static))
523 (error "Cannot `call-next-method' except in :primary or :static methods")
524 )
525 (let ((newargs (or replacement-args eieio-generic-call-arglst))
526 (next (car eieio-generic-call-next-method-list))
527 )
528 (if (or (not next) (not (car next)))
529 (apply 'no-next-method (car newargs) (cdr newargs))
530 (let* ((eieio-generic-call-next-method-list
531 (cdr eieio-generic-call-next-method-list))
532 (eieio-generic-call-arglst newargs)
533 (fcn (car next))
534 )
535 (eieio--with-scoped-class (cdr next)
536 (apply fcn newargs)) ))))
537
538 ;;; Here are some CLOS items that need the CL package
539 ;;
540
541 (defsetf eieio-oref eieio-oset)
542
543 (if (eval-when-compile (fboundp 'gv-define-expander))
544 ;; Not needed for Emacs>=24.3 since gv.el's setf expands macros and
545 ;; follows aliases.
546 nil
547 (defsetf slot-value eieio-oset)
548
549 ;; The below setf method was written by Arnd Kohrs <kohrs@acm.org>
550 (define-setf-method oref (obj slot)
551 (with-no-warnings
552 (require 'cl)
553 (let ((obj-temp (gensym))
554 (slot-temp (gensym))
555 (store-temp (gensym)))
556 (list (list obj-temp slot-temp)
557 (list obj `(quote ,slot))
558 (list store-temp)
559 (list 'set-slot-value obj-temp slot-temp
560 store-temp)
561 (list 'slot-value obj-temp slot-temp))))))
562
563 \f
564 ;;;
565 ;; We want all objects created by EIEIO to have some default set of
566 ;; behaviors so we can create object utilities, and allow various
567 ;; types of error checking. To do this, create the default EIEIO
568 ;; class, and when no parent class is specified, use this as the
569 ;; default. (But don't store it in the other classes as the default,
570 ;; allowing for transparent support.)
571 ;;
572
573 (defclass eieio-default-superclass nil
574 nil
575 "Default parent class for classes with no specified parent class.
576 Its slots are automatically adopted by classes with no specified parents.
577 This class is not stored in the `parent' slot of a class vector."
578 :abstract t)
579
580 (defalias 'standard-class 'eieio-default-superclass)
581
582 (defgeneric constructor (class newname &rest slots)
583 "Default constructor for CLASS `eieio-default-superclass'.")
584
585 (defmethod constructor :static
586 ((class eieio-default-superclass) newname &rest slots)
587 "Default constructor for CLASS `eieio-default-superclass'.
588 NEWNAME is the name to be given to the constructed object.
589 SLOTS are the initialization slots used by `shared-initialize'.
590 This static method is called when an object is constructed.
591 It allocates the vector used to represent an EIEIO object, and then
592 calls `shared-initialize' on that object."
593 (let* ((new-object (copy-sequence (eieio--class-default-object-cache (class-v class)))))
594 ;; Update the name for the newly created object.
595 (setf (eieio--object-name new-object) newname)
596 ;; Call the initialize method on the new object with the slots
597 ;; that were passed down to us.
598 (initialize-instance new-object slots)
599 ;; Return the created object.
600 new-object))
601
602 (defgeneric shared-initialize (obj slots)
603 "Set slots of OBJ with SLOTS which is a list of name/value pairs.
604 Called from the constructor routine.")
605
606 (defmethod shared-initialize ((obj eieio-default-superclass) slots)
607 "Set slots of OBJ with SLOTS which is a list of name/value pairs.
608 Called from the constructor routine."
609 (eieio--with-scoped-class (eieio--object-class obj)
610 (while slots
611 (let ((rn (eieio-initarg-to-attribute (eieio--object-class obj)
612 (car slots))))
613 (if (not rn)
614 (slot-missing obj (car slots) 'oset (car (cdr slots)))
615 (eieio-oset obj rn (car (cdr slots)))))
616 (setq slots (cdr (cdr slots))))))
617
618 (defgeneric initialize-instance (this &optional slots)
619 "Construct the new object THIS based on SLOTS.")
620
621 (defmethod initialize-instance ((this eieio-default-superclass)
622 &optional slots)
623 "Construct the new object THIS based on SLOTS.
624 SLOTS is a tagged list where odd numbered elements are tags, and
625 even numbered elements are the values to store in the tagged slot.
626 If you overload the `initialize-instance', there you will need to
627 call `shared-initialize' yourself, or you can call `call-next-method'
628 to have this constructor called automatically. If these steps are
629 not taken, then new objects of your class will not have their values
630 dynamically set from SLOTS."
631 ;; First, see if any of our defaults are `lambda', and
632 ;; re-evaluate them and apply the value to our slots.
633 (let* ((this-class (class-v (eieio--object-class this)))
634 (slot (eieio--class-public-a this-class))
635 (defaults (eieio--class-public-d this-class)))
636 (while slot
637 ;; For each slot, see if we need to evaluate it.
638 ;;
639 ;; Paul Landes said in an email:
640 ;; > CL evaluates it if it can, and otherwise, leaves it as
641 ;; > the quoted thing as you already have. This is by the
642 ;; > Sonya E. Keene book and other things I've look at on the
643 ;; > web.
644 (let ((dflt (eieio-default-eval-maybe (car defaults))))
645 (when (not (eq dflt (car defaults)))
646 (eieio-oset this (car slot) dflt) ))
647 ;; Next.
648 (setq slot (cdr slot)
649 defaults (cdr defaults))))
650 ;; Shared initialize will parse our slots for us.
651 (shared-initialize this slots))
652
653 (defgeneric slot-missing (object slot-name operation &optional new-value)
654 "Method invoked when an attempt to access a slot in OBJECT fails.")
655
656 (defmethod slot-missing ((object eieio-default-superclass) slot-name
657 operation &optional new-value)
658 "Method invoked when an attempt to access a slot in OBJECT fails.
659 SLOT-NAME is the name of the failed slot, OPERATION is the type of access
660 that was requested, and optional NEW-VALUE is the value that was desired
661 to be set.
662
663 This method is called from `oref', `oset', and other functions which
664 directly reference slots in EIEIO objects."
665 (signal 'invalid-slot-name (list (eieio-object-name object)
666 slot-name)))
667
668 (defgeneric slot-unbound (object class slot-name fn)
669 "Slot unbound is invoked during an attempt to reference an unbound slot.")
670
671 (defmethod slot-unbound ((object eieio-default-superclass)
672 class slot-name fn)
673 "Slot unbound is invoked during an attempt to reference an unbound slot.
674 OBJECT is the instance of the object being reference. CLASS is the
675 class of OBJECT, and SLOT-NAME is the offending slot. This function
676 throws the signal `unbound-slot'. You can overload this function and
677 return the value to use in place of the unbound value.
678 Argument FN is the function signaling this error.
679 Use `slot-boundp' to determine if a slot is bound or not.
680
681 In CLOS, the argument list is (CLASS OBJECT SLOT-NAME), but
682 EIEIO can only dispatch on the first argument, so the first two are swapped."
683 (signal 'unbound-slot (list (eieio-class-name class) (eieio-object-name object)
684 slot-name fn)))
685
686 (defgeneric no-applicable-method (object method &rest args)
687 "Called if there are no implementations for OBJECT in METHOD.")
688
689 (defmethod no-applicable-method ((object eieio-default-superclass)
690 method &rest args)
691 "Called if there are no implementations for OBJECT in METHOD.
692 OBJECT is the object which has no method implementation.
693 ARGS are the arguments that were passed to METHOD.
694
695 Implement this for a class to block this signal. The return
696 value becomes the return value of the original method call."
697 (signal 'no-method-definition (list method (eieio-object-name object)))
698 )
699
700 (defgeneric no-next-method (object &rest args)
701 "Called from `call-next-method' when no additional methods are available.")
702
703 (defmethod no-next-method ((object eieio-default-superclass)
704 &rest args)
705 "Called from `call-next-method' when no additional methods are available.
706 OBJECT is othe object being called on `call-next-method'.
707 ARGS are the arguments it is called by.
708 This method signals `no-next-method' by default. Override this
709 method to not throw an error, and its return value becomes the
710 return value of `call-next-method'."
711 (signal 'no-next-method (list (eieio-object-name object) args))
712 )
713
714 (defgeneric clone (obj &rest params)
715 "Make a copy of OBJ, and then supply PARAMS.
716 PARAMS is a parameter list of the same form used by `initialize-instance'.
717
718 When overloading `clone', be sure to call `call-next-method'
719 first and modify the returned object.")
720
721 (defmethod clone ((obj eieio-default-superclass) &rest params)
722 "Make a copy of OBJ, and then apply PARAMS."
723 (let ((nobj (copy-sequence obj))
724 (nm (eieio--object-name obj))
725 (passname (and params (stringp (car params))))
726 (num 1))
727 (if params (shared-initialize nobj (if passname (cdr params) params)))
728 (if (not passname)
729 (save-match-data
730 (if (string-match "-\\([0-9]+\\)" nm)
731 (setq num (1+ (string-to-number (match-string 1 nm)))
732 nm (substring nm 0 (match-beginning 0))))
733 (setf (eieio--object-name nobj) (concat nm "-" (int-to-string num))))
734 (setf (eieio--object-name nobj) (car params)))
735 nobj))
736
737 (defgeneric destructor (this &rest params)
738 "Destructor for cleaning up any dynamic links to our object.")
739
740 (defmethod destructor ((this eieio-default-superclass) &rest params)
741 "Destructor for cleaning up any dynamic links to our object.
742 Argument THIS is the object being destroyed. PARAMS are additional
743 ignored parameters."
744 ;; No cleanup... yet.
745 )
746
747 (defgeneric object-print (this &rest strings)
748 "Pretty printer for object THIS. Call function `object-name' with STRINGS.
749
750 It is sometimes useful to put a summary of the object into the
751 default #<notation> string when using EIEIO browsing tools.
752 Implement this method to customize the summary.")
753
754 (defmethod object-print ((this eieio-default-superclass) &rest strings)
755 "Pretty printer for object THIS. Call function `object-name' with STRINGS.
756 The default method for printing object THIS is to use the
757 function `object-name'.
758
759 It is sometimes useful to put a summary of the object into the
760 default #<notation> string when using EIEIO browsing tools.
761
762 Implement this function and specify STRINGS in a call to
763 `call-next-method' to provide additional summary information.
764 When passing in extra strings from child classes, always remember
765 to prepend a space."
766 (eieio-object-name this (apply 'concat strings)))
767
768 (defvar eieio-print-depth 0
769 "When printing, keep track of the current indentation depth.")
770
771 (defgeneric object-write (this &optional comment)
772 "Write out object THIS to the current stream.
773 Optional COMMENT will add comments to the beginning of the output.")
774
775 (defmethod object-write ((this eieio-default-superclass) &optional comment)
776 "Write object THIS out to the current stream.
777 This writes out the vector version of this object. Complex and recursive
778 object are discouraged from being written.
779 If optional COMMENT is non-nil, include comments when outputting
780 this object."
781 (when comment
782 (princ ";; Object ")
783 (princ (eieio-object-name-string this))
784 (princ "\n")
785 (princ comment)
786 (princ "\n"))
787 (let* ((cl (eieio-object-class this))
788 (cv (class-v cl)))
789 ;; Now output readable lisp to recreate this object
790 ;; It should look like this:
791 ;; (<constructor> <name> <slot> <slot> ... )
792 ;; Each slot's slot is writen using its :writer.
793 (princ (make-string (* eieio-print-depth 2) ? ))
794 (princ "(")
795 (princ (symbol-name (class-constructor (eieio-object-class this))))
796 (princ " ")
797 (prin1 (eieio-object-name-string this))
798 (princ "\n")
799 ;; Loop over all the public slots
800 (let ((publa (eieio--class-public-a cv))
801 (publd (eieio--class-public-d cv))
802 (publp (eieio--class-public-printer cv))
803 (eieio-print-depth (1+ eieio-print-depth)))
804 (while publa
805 (when (slot-boundp this (car publa))
806 (let ((i (class-slot-initarg cl (car publa)))
807 (v (eieio-oref this (car publa)))
808 )
809 (unless (or (not i) (equal v (car publd)))
810 (unless (bolp)
811 (princ "\n"))
812 (princ (make-string (* eieio-print-depth 2) ? ))
813 (princ (symbol-name i))
814 (if (car publp)
815 ;; Use our public printer
816 (progn
817 (princ " ")
818 (funcall (car publp) v))
819 ;; Use our generic override prin1 function.
820 (princ (if (or (eieio-object-p v)
821 (eieio-object-p (car-safe v)))
822 "\n" " "))
823 (eieio-override-prin1 v)))))
824 (setq publa (cdr publa) publd (cdr publd)
825 publp (cdr publp))))
826 (princ ")")
827 (when (= eieio-print-depth 0)
828 (princ "\n"))))
829
830 (defun eieio-override-prin1 (thing)
831 "Perform a `prin1' on THING taking advantage of object knowledge."
832 (cond ((eieio-object-p thing)
833 (object-write thing))
834 ((consp thing)
835 (eieio-list-prin1 thing))
836 ((class-p thing)
837 (princ (eieio-class-name thing)))
838 ((or (keywordp thing) (booleanp thing))
839 (prin1 thing))
840 ((symbolp thing)
841 (princ (concat "'" (symbol-name thing))))
842 (t (prin1 thing))))
843
844 (defun eieio-list-prin1 (list)
845 "Display LIST where list may contain objects."
846 (if (not (eieio-object-p (car list)))
847 (progn
848 (princ "'")
849 (prin1 list))
850 (princ (make-string (* eieio-print-depth 2) ? ))
851 (princ "(list")
852 (let ((eieio-print-depth (1+ eieio-print-depth)))
853 (while list
854 (princ "\n")
855 (if (eieio-object-p (car list))
856 (object-write (car list))
857 (princ (make-string (* eieio-print-depth 2) ? ))
858 (eieio-override-prin1 (car list)))
859 (setq list (cdr list))))
860 (princ ")")))
861
862 \f
863 ;;; Unimplemented functions from CLOS
864 ;;
865 (defun change-class (obj class)
866 "Change the class of OBJ to type CLASS.
867 This may create or delete slots, but does not affect the return value
868 of `eq'."
869 (error "EIEIO: `change-class' is unimplemented"))
870
871 ;;; Interfacing with edebug
872 ;;
873 (defun eieio-edebug-prin1-to-string (object &optional noescape)
874 "Display EIEIO OBJECT in fancy format.
875 Overrides the edebug default.
876 Optional argument NOESCAPE is passed to `prin1-to-string' when appropriate."
877 (cond ((class-p object) (eieio-class-name object))
878 ((eieio-object-p object) (object-print object))
879 ((and (listp object) (or (class-p (car object))
880 (eieio-object-p (car object))))
881 (concat "(" (mapconcat 'eieio-edebug-prin1-to-string object " ") ")"))
882 (t (prin1-to-string object noescape))))
883
884 (add-hook 'edebug-setup-hook
885 (lambda ()
886 (def-edebug-spec defmethod
887 (&define ; this means we are defining something
888 [&or name ("setf" :name setf name)]
889 ;; ^^ This is the methods symbol
890 [ &optional symbolp ] ; this is key :before etc
891 list ; arguments
892 [ &optional stringp ] ; documentation string
893 def-body ; part to be debugged
894 ))
895 ;; The rest of the macros
896 (def-edebug-spec oref (form quote))
897 (def-edebug-spec oref-default (form quote))
898 (def-edebug-spec oset (form quote form))
899 (def-edebug-spec oset-default (form quote form))
900 (def-edebug-spec class-v form)
901 (def-edebug-spec class-p form)
902 (def-edebug-spec eieio-object-p form)
903 (def-edebug-spec class-constructor form)
904 (def-edebug-spec generic-p form)
905 (def-edebug-spec with-slots (list list def-body))
906 ;; I suspect this isn't the best way to do this, but when
907 ;; cust-print was used on my system all my objects
908 ;; appeared as "#1 =" which was not useful. This allows
909 ;; edebug to print my objects in the nice way they were
910 ;; meant to with `object-print' and `class-name'
911 ;; (defalias 'edebug-prin1-to-string 'eieio-edebug-prin1-to-string)
912 )
913 )
914
915 ;;; Autoloading some external symbols, and hooking into the help system
916 ;;
917
918 \f
919 ;;; Start of automatically extracted autoloads.
920 \f
921 ;;;### (autoloads (customize-object) "eieio-custom" "eieio-custom.el"
922 ;;;;;; "928623502e8bf40454822355388542b5")
923 ;;; Generated autoloads from eieio-custom.el
924
925 (autoload 'customize-object "eieio-custom" "\
926 Customize OBJ in a custom buffer.
927 Optional argument GROUP is the sub-group of slots to display.
928
929 \(fn OBJ &optional GROUP)" nil nil)
930
931 ;;;***
932 \f
933 ;;;### (autoloads (eieio-help-mode-augmentation-maybee eieio-describe-generic
934 ;;;;;; eieio-describe-constructor eieio-describe-class eieio-browse)
935 ;;;;;; "eieio-opt" "eieio-opt.el" "d808328f9c0156ecbd412d77ba8c569e")
936 ;;; Generated autoloads from eieio-opt.el
937
938 (autoload 'eieio-browse "eieio-opt" "\
939 Create an object browser window to show all objects.
940 If optional ROOT-CLASS, then start with that, otherwise start with
941 variable `eieio-default-superclass'.
942
943 \(fn &optional ROOT-CLASS)" t nil)
944 (defalias 'describe-class 'eieio-describe-class)
945
946 (autoload 'eieio-describe-class "eieio-opt" "\
947 Describe a CLASS defined by a string or symbol.
948 If CLASS is actually an object, then also display current values of that object.
949 Optional HEADERFCN should be called to insert a few bits of info first.
950
951 \(fn CLASS &optional HEADERFCN)" t nil)
952
953 (autoload 'eieio-describe-constructor "eieio-opt" "\
954 Describe the constructor function FCN.
955 Uses `eieio-describe-class' to describe the class being constructed.
956
957 \(fn FCN)" t nil)
958 (defalias 'describe-generic 'eieio-describe-generic)
959
960 (autoload 'eieio-describe-generic "eieio-opt" "\
961 Describe the generic function GENERIC.
962 Also extracts information about all methods specific to this generic.
963
964 \(fn GENERIC)" t nil)
965
966 (autoload 'eieio-help-mode-augmentation-maybee "eieio-opt" "\
967 For buffers thrown into help mode, augment for EIEIO.
968 Arguments UNUSED are not used.
969
970 \(fn &rest UNUSED)" nil nil)
971
972 ;;;***
973 \f
974 ;;; End of automatically extracted autoloads.
975
976 (provide 'eieio)
977
978 ;;; eieio ends here