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