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