Merge from origin/emacs-24
[emacs.git] / lisp / emacs-lisp / eieio.el
blobcdf1992f9a51ae68ac4ec8b6205f393e16a4e58e
1 ;;; eieio.el --- Enhanced Implementation of Emacs Interpreted Objects -*- lexical-binding:t -*-
2 ;;; or maybe Eric's Implementation of Emacs Interpreted Objects
4 ;; Copyright (C) 1995-1996, 1998-2015 Free Software Foundation, Inc.
6 ;; Author: Eric M. Ludlam <zappo@gnu.org>
7 ;; Version: 1.4
8 ;; Keywords: OO, lisp
10 ;; This file is part of GNU Emacs.
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.
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.
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/>.
25 ;;; Commentary:
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.
32 ;; See eieio.texi for complete documentation on using this package.
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
39 ;; @TODO - fix :initform to be a form, not a quoted value
40 ;; @TODO - Prefix non-clos functions with `eieio-'.
42 ;; TODO: better integrate CL's defstructs and classes. E.g. make it possible
43 ;; to create a new class that inherits from a struct.
45 ;;; Code:
47 (defvar eieio-version "1.4"
48 "Current version of EIEIO.")
50 (defun eieio-version ()
51 "Display the current version of EIEIO."
52 (interactive)
53 (message eieio-version))
55 (require 'eieio-core)
58 ;;; Defining a new class
60 (defmacro defclass (name superclasses slots &rest options-and-doc)
61 "Define NAME as a new class derived from SUPERCLASS with SLOTS.
62 OPTIONS-AND-DOC is used as the class' options and base documentation.
63 SUPERCLASSES is a list of superclasses to inherit from, with SLOTS
64 being the slots residing in that class definition. Supported tags are:
66 :initform - Initializing form.
67 :initarg - Tag used during initialization.
68 :accessor - Tag used to create a function to access this slot.
69 :allocation - Specify where the value is stored.
70 Defaults to `:instance', but could also be `:class'.
71 :writer - A function symbol which will `write' an object's slot.
72 :reader - A function symbol which will `read' an object.
73 :type - The type of data allowed in this slot (see `typep').
74 :documentation
75 - A string documenting use of this slot.
77 The following are extensions on CLOS:
78 :custom - When customizing an object, the custom :type. Public only.
79 :label - A text string label used for a slot when customizing.
80 :group - Name of a customization group this slot belongs in.
81 :printer - A function to call to print the value of a slot.
82 See `eieio-override-prin1' as an example.
84 A class can also have optional options. These options happen in place
85 of documentation (including a :documentation tag), in addition to
86 documentation, or not at all. Supported options are:
88 :documentation - The doc-string used for this class.
90 Options added to EIEIO:
92 :allow-nil-initform - Non-nil to skip typechecking of null initforms.
93 :custom-groups - List of custom group names. Organizes slots into
94 reasonable groups for customizations.
95 :abstract - Non-nil to prevent instances of this class.
96 If a string, use as an error string if someone does
97 try to make an instance.
98 :method-invocation-order
99 - Control the method invocation order if there is
100 multiple inheritance. Valid values are:
101 :breadth-first - The default.
102 :depth-first
104 Options in CLOS not supported in EIEIO:
106 :metaclass - Class to use in place of `standard-class'
107 :default-initargs - Initargs to use when initializing new objects of
108 this class.
110 Due to the way class options are set up, you can add any tags you wish,
111 and reference them using the function `class-option'."
112 (declare (doc-string 4))
113 (cl-check-type superclasses list)
115 (cond ((and (stringp (car options-and-doc))
116 (/= 1 (% (length options-and-doc) 2)))
117 (error "Too many arguments to `defclass'"))
118 ((and (symbolp (car options-and-doc))
119 (/= 0 (% (length options-and-doc) 2)))
120 (error "Too many arguments to `defclass'")))
122 (if (stringp (car options-and-doc))
123 (setq options-and-doc
124 (cons :documentation options-and-doc)))
126 ;; Make sure the method invocation order is a valid value.
127 (let ((io (eieio--class-option-assoc options-and-doc
128 :method-invocation-order)))
129 (when (and io (not (member io '(:depth-first :breadth-first :c3))))
130 (error "Method invocation order %s is not allowed" io)))
132 (let ((testsym1 (intern (concat (symbol-name name) "-p")))
133 (testsym2 (intern (format "eieio--childp--%s" name)))
134 (accessors ()))
136 ;; Collect the accessors we need to define.
137 (pcase-dolist (`(,sname . ,soptions) slots)
138 (let* ((acces (plist-get soptions :accessor))
139 (initarg (plist-get soptions :initarg))
140 (reader (plist-get soptions :reader))
141 (writer (plist-get soptions :writer))
142 (alloc (plist-get soptions :allocation))
143 (label (plist-get soptions :label)))
145 (if eieio-error-unsupported-class-tags
146 (let ((tmp soptions))
147 (while tmp
148 (if (not (member (car tmp) '(:accessor
149 :initform
150 :initarg
151 :documentation
152 :protection
153 :reader
154 :writer
155 :allocation
156 :type
157 :custom
158 :label
159 :group
160 :printer
161 :allow-nil-initform
162 :custom-groups)))
163 (signal 'invalid-slot-type (list (car tmp))))
164 (setq tmp (cdr (cdr tmp))))))
166 ;; Make sure the :allocation parameter has a valid value.
167 (if (not (memq alloc '(nil :class :instance)))
168 (signal 'invalid-slot-type (list :allocation alloc)))
170 ;; Label is nil, or a string
171 (if (not (or (null label) (stringp label)))
172 (signal 'invalid-slot-type (list :label label)))
174 ;; Is there an initarg, but allocation of class?
175 (if (and initarg (eq alloc :class))
176 (message "Class allocated slots do not need :initarg"))
178 ;; Anyone can have an accessor function. This creates a function
179 ;; of the specified name, and also performs a `defsetf' if applicable
180 ;; so that users can `setf' the space returned by this function.
181 (when acces
182 (push `(cl-defmethod (setf ,acces) (value (this ,name))
183 (eieio-oset this ',sname value))
184 accessors)
185 (push `(cl-defmethod ,acces ((this ,name))
186 ,(format
187 "Retrieve the slot `%S' from an object of class `%S'."
188 sname name)
189 ;; FIXME: Why is this different from the :reader case?
190 (if (slot-boundp this ',sname) (eieio-oref this ',sname)))
191 accessors)
192 (when (and eieio-backward-compatibility (eq alloc :class))
193 ;; FIXME: How could I declare this *method* as obsolete.
194 (push `(cl-defmethod ,acces ((this (subclass ,name)))
195 ,(format
196 "Retrieve the class slot `%S' from a class `%S'.
197 This method is obsolete."
198 sname name)
199 (if (slot-boundp this ',sname)
200 (eieio-oref-default this ',sname)))
201 accessors)))
203 ;; If a writer is defined, then create a generic method of that
204 ;; name whose purpose is to set the value of the slot.
205 (if writer
206 (push `(cl-defmethod ,writer ((this ,name) value)
207 ,(format "Set the slot `%S' of an object of class `%S'."
208 sname name)
209 (setf (slot-value this ',sname) value))
210 accessors))
211 ;; If a reader is defined, then create a generic method
212 ;; of that name whose purpose is to access this slot value.
213 (if reader
214 (push `(cl-defmethod ,reader ((this ,name))
215 ,(format "Access the slot `%S' from object of class `%S'."
216 sname name)
217 (slot-value this ',sname))
218 accessors))
221 `(progn
222 ;; This test must be created right away so we can have self-
223 ;; referencing classes. ei, a class whose slot can contain only
224 ;; pointers to itself.
226 ;; Create the test functions.
227 (defalias ',testsym1 (eieio-make-class-predicate ',name))
228 (defalias ',testsym2 (eieio-make-child-predicate ',name))
230 ,@(when eieio-backward-compatibility
231 (let ((f (intern (format "%s-child-p" name))))
232 `((defalias ',f ',testsym2)
233 (make-obsolete
234 ',f ,(format "use (cl-typep ... '%s) instead" name) "25.1"))))
236 ;; When using typep, (typep OBJ 'myclass) returns t for objects which
237 ;; are subclasses of myclass. For our predicates, however, it is
238 ;; important for EIEIO to be backwards compatible, where
239 ;; myobject-p, and myobject-child-p are different.
240 ;; "cl" uses this technique to specify symbols with specific typep
241 ;; test, so we can let typep have the CLOS documented behavior
242 ;; while keeping our above predicate clean.
244 (put ',name 'cl-deftype-satisfies #',testsym2)
246 (eieio-defclass-internal ',name ',superclasses ',slots ',options-and-doc)
248 ,@accessors
250 ;; Create the constructor function
251 ,(if (eieio--class-option-assoc options-and-doc :abstract)
252 ;; Abstract classes cannot be instantiated. Say so.
253 (let ((abs (eieio--class-option-assoc options-and-doc :abstract)))
254 (if (not (stringp abs))
255 (setq abs (format "Class %s is abstract" name)))
256 `(defun ,name (&rest _)
257 ,(format "You cannot create a new object of type %S." name)
258 (error ,abs)))
260 ;; Non-abstract classes need a constructor.
261 `(defun ,name (&rest slots)
262 ,(format "Create a new object with name NAME of class type %S."
263 name)
264 (declare (compiler-macro
265 (lambda (whole)
266 (if (not (stringp (car slots)))
267 whole
268 (macroexp--warn-and-return
269 (format "Obsolete name arg %S to constructor %S"
270 (car slots) (car whole))
271 ;; Keep the name arg, for backward compatibility,
272 ;; but hide it so we don't trigger indefinitely.
273 `(,(car whole) (identity ,(car slots))
274 ,@(cdr slots)))))))
275 (apply #'make-instance ',name slots))))))
278 ;;; Get/Set slots in an object.
280 (defmacro oref (obj slot)
281 "Retrieve the value stored in OBJ in the slot named by SLOT.
282 Slot is the name of the slot when created by `defclass' or the label
283 created by the :initarg tag."
284 (declare (debug (form symbolp)))
285 `(eieio-oref ,obj (quote ,slot)))
287 (defalias 'slot-value 'eieio-oref)
288 (defalias 'set-slot-value 'eieio-oset)
289 (make-obsolete 'set-slot-value "use (setf (slot-value ..) ..) instead" "25.1")
291 (defmacro oref-default (obj slot)
292 "Get the default value of OBJ (maybe a class) for SLOT.
293 The default value is the value installed in a class with the :initform
294 tag. SLOT can be the slot name, or the tag specified by the :initarg
295 tag in the `defclass' call."
296 (declare (debug (form symbolp)))
297 `(eieio-oref-default ,obj (quote ,slot)))
299 ;;; Handy CLOS macros
301 (defmacro with-slots (spec-list object &rest body)
302 "Bind SPEC-LIST lexically to slot values in OBJECT, and execute BODY.
303 This establishes a lexical environment for referring to the slots in
304 the instance named by the given slot-names as though they were
305 variables. Within such a context the value of the slot can be
306 specified by using its slot name, as if it were a lexically bound
307 variable. Both setf and setq can be used to set the value of the
308 slot.
310 SPEC-LIST is of a form similar to `let'. For example:
312 ((VAR1 SLOT1)
313 SLOT2
314 SLOTN
315 (VARN+1 SLOTN+1))
317 Where each VAR is the local variable given to the associated
318 SLOT. A slot specified without a variable name is given a
319 variable name of the same name as the slot."
320 (declare (indent 2) (debug (sexp sexp def-body)))
321 (require 'cl-lib)
322 ;; Transform the spec-list into a cl-symbol-macrolet spec-list.
323 (let ((mappings (mapcar (lambda (entry)
324 (let ((var (if (listp entry) (car entry) entry))
325 (slot (if (listp entry) (cadr entry) entry)))
326 (list var `(slot-value ,object ',slot))))
327 spec-list)))
328 (append (list 'cl-symbol-macrolet mappings)
329 body)))
331 ;;; Simple generators, and query functions. None of these would do
332 ;; well embedded into an object.
334 (define-obsolete-function-alias
335 'object-class-fast #'eieio--object-class-name "24.4")
337 (cl-defgeneric eieio-object-name-string (obj)
338 "Return a string which is OBJ's name."
339 (declare (obsolete eieio-named "25.1")))
341 (defun eieio-object-name (obj &optional extra)
342 "Return a printed representation for object OBJ.
343 If EXTRA, include that in the string returned to represent the symbol."
344 (cl-check-type obj eieio-object)
345 (format "#<%s %s%s>" (eieio--object-class-name obj)
346 (eieio-object-name-string obj) (or extra "")))
347 (define-obsolete-function-alias 'object-name #'eieio-object-name "24.4")
349 (defconst eieio--object-names (make-hash-table :test #'eq :weakness 'key))
351 ;; In the past, every EIEIO object had a `name' field, so we had the two method
352 ;; below "for free". Since this field is very rarely used, we got rid of it
353 ;; and instead we keep it in a weak hash-tables, for those very rare objects
354 ;; that use it.
355 (cl-defmethod eieio-object-name-string (obj)
356 (or (gethash obj eieio--object-names)
357 (symbol-name (eieio-object-class obj))))
358 (define-obsolete-function-alias
359 'object-name-string #'eieio-object-name-string "24.4")
361 (cl-defmethod eieio-object-set-name-string (obj name)
362 "Set the string which is OBJ's NAME."
363 (declare (obsolete eieio-named "25.1"))
364 (cl-check-type name string)
365 (setf (gethash obj eieio--object-names) name))
366 (define-obsolete-function-alias
367 'object-set-name-string 'eieio-object-set-name-string "24.4")
369 (defun eieio-object-class (obj)
370 "Return the class struct defining OBJ."
371 ;; FIXME: We say we return a "struct" but we return a symbol instead!
372 (cl-check-type obj eieio-object)
373 (eieio--object-class-name obj))
374 (define-obsolete-function-alias 'object-class #'eieio-object-class "24.4")
375 ;; CLOS name, maybe?
376 (define-obsolete-function-alias 'class-of #'eieio-object-class "24.4")
378 (defun eieio-object-class-name (obj)
379 "Return a Lisp like symbol name for OBJ's class."
380 (cl-check-type obj eieio-object)
381 (eieio-class-name (eieio--object-class-object obj)))
382 (define-obsolete-function-alias
383 'object-class-name 'eieio-object-class-name "24.4")
385 (defun eieio-class-parents (class)
386 "Return parent classes to CLASS. (overload of variable).
388 The CLOS function `class-direct-superclasses' is aliased to this function."
389 (eieio--class-parent (eieio--class-object class)))
391 (define-obsolete-function-alias 'class-parents #'eieio-class-parents "24.4")
393 (defun eieio-class-children (class)
394 "Return child classes to CLASS.
395 The CLOS function `class-direct-subclasses' is aliased to this function."
396 (cl-check-type class class)
397 (eieio--class-children (eieio--class-v class)))
398 (define-obsolete-function-alias
399 'class-children #'eieio-class-children "24.4")
401 ;; Official CLOS functions.
402 (define-obsolete-function-alias
403 'class-direct-superclasses #'eieio-class-parents "24.4")
404 (define-obsolete-function-alias
405 'class-direct-subclasses #'eieio-class-children "24.4")
407 (defmacro eieio-class-parent (class)
408 "Return first parent class to CLASS. (overload of variable)."
409 `(car (eieio-class-parents ,class)))
410 (define-obsolete-function-alias 'class-parent 'eieio-class-parent "24.4")
412 (defun same-class-p (obj class)
413 "Return t if OBJ is of class-type CLASS."
414 (setq class (eieio--class-object class))
415 (cl-check-type class eieio--class)
416 (cl-check-type obj eieio-object)
417 (eq (eieio--object-class-object obj) class))
419 (defun object-of-class-p (obj class)
420 "Return non-nil if OBJ is an instance of CLASS or CLASS' subclasses."
421 (cl-check-type obj eieio-object)
422 ;; class will be checked one layer down
423 (child-of-class-p (eieio--object-class-object obj) class))
424 ;; Backwards compatibility
425 (defalias 'obj-of-class-p 'object-of-class-p)
427 (defun child-of-class-p (child class)
428 "Return non-nil if CHILD class is a subclass of CLASS."
429 (setq child (eieio--class-object child))
430 (cl-check-type child eieio--class)
431 ;; `eieio-default-superclass' is never mentioned in eieio--class-parent,
432 ;; so we have to special case it here.
433 (or (eq class 'eieio-default-superclass)
434 (let ((p nil))
435 (setq class (eieio--class-object class))
436 (cl-check-type class eieio--class)
437 (while (and child (not (eq child class)))
438 (setq p (append p (eieio--class-parent child))
439 child (pop p)))
440 (if child t))))
442 (defun eieio-slot-descriptor-name (slot) slot)
444 (defun eieio-class-slots (class)
445 "Return list of slots available in instances of CLASS."
446 ;; FIXME: This only gives the instance slots and ignores the
447 ;; class-allocated slots.
448 ;; FIXME: It only gives the slot's *names* rather than actual
449 ;; slot descriptors.
450 (setq class (eieio--class-object class))
451 (cl-check-type class eieio--class)
452 (eieio--class-public-a class))
454 (defun object-slots (obj)
455 "Return list of slots available in OBJ."
456 (declare (obsolete eieio-class-slots "25.1"))
457 (cl-check-type obj eieio-object)
458 (eieio-class-slots (eieio--object-class-object obj)))
460 (defun eieio--class-slot-initarg (class slot) "Fetch from CLASS, SLOT's :initarg."
461 (cl-check-type class eieio--class)
462 (let ((ia (eieio--class-initarg-tuples class))
463 (f nil))
464 (while (and ia (not f))
465 (if (eq (cdr (car ia)) slot)
466 (setq f (car (car ia))))
467 (setq ia (cdr ia)))
470 ;;; Object Set macros
472 (defmacro oset (obj slot value)
473 "Set the value in OBJ for slot SLOT to VALUE.
474 SLOT is the slot name as specified in `defclass' or the tag created
475 with in the :initarg slot. VALUE can be any Lisp object."
476 (declare (debug (form symbolp form)))
477 `(eieio-oset ,obj (quote ,slot) ,value))
479 (defmacro oset-default (class slot value)
480 "Set the default slot in CLASS for SLOT to VALUE.
481 The default value is usually set with the :initform tag during class
482 creation. This allows users to change the default behavior of classes
483 after they are created."
484 (declare (debug (form symbolp form)))
485 `(eieio-oset-default ,class (quote ,slot) ,value))
487 ;;; CLOS queries into classes and slots
489 (defun slot-boundp (object slot)
490 "Return non-nil if OBJECT's SLOT is bound.
491 Setting a slot's value makes it bound. Calling `slot-makeunbound' will
492 make a slot unbound.
493 OBJECT can be an instance or a class."
494 ;; Skip typechecking while retrieving this value.
495 (let ((eieio-skip-typecheck t))
496 ;; Return nil if the magic symbol is in there.
497 (not (eq (cond
498 ((eieio-object-p object) (eieio-oref object slot))
499 ((symbolp object) (eieio-oref-default object slot))
500 (t (signal 'wrong-type-argument (list 'eieio-object-p object))))
501 eieio-unbound))))
503 (defun slot-makeunbound (object slot)
504 "In OBJECT, make SLOT unbound."
505 (eieio-oset object slot eieio-unbound))
507 (defun slot-exists-p (object-or-class slot)
508 "Return non-nil if OBJECT-OR-CLASS has SLOT."
509 (let ((cv (cond ((eieio-object-p object-or-class)
510 (eieio--object-class-object object-or-class))
511 ((eieio--class-p object-or-class) object-or-class)
512 (t (find-class object-or-class 'error)))))
513 (or (memq slot (eieio--class-public-a cv))
514 (memq slot (eieio--class-class-allocation-a cv)))
517 (defun find-class (symbol &optional errorp)
518 "Return the class that SYMBOL represents.
519 If there is no class, nil is returned if ERRORP is nil.
520 If ERRORP is non-nil, `wrong-argument-type' is signaled."
521 (let ((class (eieio--class-v symbol)))
522 (cond
523 ((eieio--class-p class) class)
524 (errorp (signal 'wrong-type-argument (list 'class-p symbol))))))
526 ;;; Slightly more complex utility functions for objects
528 (defun object-assoc (key slot list)
529 "Return an object if KEY is `equal' to SLOT's value of an object in LIST.
530 LIST is a list of objects whose slots are searched.
531 Objects in LIST do not need to have a slot named SLOT, nor does
532 SLOT need to be bound. If these errors occur, those objects will
533 be ignored."
534 (cl-check-type list list)
535 (while (and list (not (condition-case nil
536 ;; This prevents errors for missing slots.
537 (equal key (eieio-oref (car list) slot))
538 (error nil))))
539 (setq list (cdr list)))
540 (car list))
542 (defun object-assoc-list (slot list)
543 "Return an association list with the contents of SLOT as the key element.
544 LIST must be a list of objects with SLOT in it.
545 This is useful when you need to do completing read on an object group."
546 (cl-check-type list list)
547 (let ((assoclist nil))
548 (while list
549 (setq assoclist (cons (cons (eieio-oref (car list) slot)
550 (car list))
551 assoclist))
552 (setq list (cdr list)))
553 (nreverse assoclist)))
555 (defun object-assoc-list-safe (slot list)
556 "Return an association list with the contents of SLOT as the key element.
557 LIST must be a list of objects, but those objects do not need to have
558 SLOT in it. If it does not, then that element is left out of the association
559 list."
560 (cl-check-type list list)
561 (let ((assoclist nil))
562 (while list
563 (if (slot-exists-p (car list) slot)
564 (setq assoclist (cons (cons (eieio-oref (car list) slot)
565 (car list))
566 assoclist)))
567 (setq list (cdr list)))
568 (nreverse assoclist)))
570 (defun object-add-to-list (object slot item &optional append)
571 "In OBJECT's SLOT, add ITEM to the list of elements.
572 Optional argument APPEND indicates we need to append to the list.
573 If ITEM already exists in the list in SLOT, then it is not added.
574 Comparison is done with `equal' through the `member' function call.
575 If SLOT is unbound, bind it to the list containing ITEM."
576 (let (ov)
577 ;; Find the originating list.
578 (if (not (slot-boundp object slot))
579 (setq ov (list item))
580 (setq ov (eieio-oref object slot))
581 ;; turn it into a list.
582 (unless (listp ov)
583 (setq ov (list ov)))
584 ;; Do the combination
585 (if (not (member item ov))
586 (setq ov
587 (if append
588 (append ov (list item))
589 (cons item ov)))))
590 ;; Set back into the slot.
591 (eieio-oset object slot ov)))
593 (defun object-remove-from-list (object slot item)
594 "In OBJECT's SLOT, remove occurrences of ITEM.
595 Deletion is done with `delete', which deletes by side effect,
596 and comparisons are done with `equal'.
597 If SLOT is unbound, do nothing."
598 (if (not (slot-boundp object slot))
600 (eieio-oset object slot (delete item (eieio-oref object slot)))))
602 ;;; Here are some CLOS items that need the CL package
605 ;; FIXME: Shouldn't this be a more complex gv-expander which extracts the
606 ;; common code between oref and oset, so as to reduce the redundant work done
607 ;; in (push foo (oref bar baz)), like we do for the `nth' expander?
608 (gv-define-simple-setter eieio-oref eieio-oset)
612 ;; We want all objects created by EIEIO to have some default set of
613 ;; behaviors so we can create object utilities, and allow various
614 ;; types of error checking. To do this, create the default EIEIO
615 ;; class, and when no parent class is specified, use this as the
616 ;; default. (But don't store it in the other classes as the default,
617 ;; allowing for transparent support.)
620 (defclass eieio-default-superclass nil
622 "Default parent class for classes with no specified parent class.
623 Its slots are automatically adopted by classes with no specified parents.
624 This class is not stored in the `parent' slot of a class vector."
625 :abstract t)
627 (setq eieio-default-superclass (eieio--class-v 'eieio-default-superclass))
629 (defalias 'standard-class 'eieio-default-superclass)
631 (cl-defgeneric make-instance (class &rest initargs)
632 "Make a new instance of CLASS based on INITARGS.
633 For example:
635 (make-instance 'foo)
637 INITARGS is a property list with keywords based on the `:initarg'
638 for each slot. For example:
640 (make-instance 'foo :slot1 value1 :slotN valueN)")
642 (define-obsolete-function-alias 'constructor #'make-instance "25.1")
644 (cl-defmethod make-instance
645 ((class (subclass eieio-default-superclass)) &rest slots)
646 "Default constructor for CLASS `eieio-default-superclass'.
647 SLOTS are the initialization slots used by `initialize-instance'.
648 This static method is called when an object is constructed.
649 It allocates the vector used to represent an EIEIO object, and then
650 calls `initialize-instance' on that object."
651 (let* ((new-object (copy-sequence (eieio--class-default-object-cache
652 (eieio--class-object class)))))
653 (if (and slots
654 (let ((x (car slots)))
655 (or (stringp x) (null x))))
656 (funcall (if eieio-backward-compatibility #'ignore #'message)
657 "Obsolete name %S passed to %S constructor"
658 (pop slots) class))
659 ;; Call the initialize method on the new object with the slots
660 ;; that were passed down to us.
661 (initialize-instance new-object slots)
662 ;; Return the created object.
663 new-object))
665 ;; FIXME: CLOS uses "&rest INITARGS" instead.
666 (cl-defgeneric shared-initialize (obj slots)
667 "Set slots of OBJ with SLOTS which is a list of name/value pairs.
668 Called from the constructor routine.")
670 (cl-defmethod shared-initialize ((obj eieio-default-superclass) slots)
671 "Set slots of OBJ with SLOTS which is a list of name/value pairs.
672 Called from the constructor routine."
673 (while slots
674 (let ((rn (eieio--initarg-to-attribute (eieio--object-class-object obj)
675 (car slots))))
676 (if (not rn)
677 (slot-missing obj (car slots) 'oset (car (cdr slots)))
678 (eieio-oset obj rn (car (cdr slots)))))
679 (setq slots (cdr (cdr slots)))))
681 ;; FIXME: CLOS uses "&rest INITARGS" instead.
682 (cl-defgeneric initialize-instance (this &optional slots)
683 "Construct the new object THIS based on SLOTS.")
685 (cl-defmethod initialize-instance ((this eieio-default-superclass)
686 &optional slots)
687 "Construct the new object THIS based on SLOTS.
688 SLOTS is a tagged list where odd numbered elements are tags, and
689 even numbered elements are the values to store in the tagged slot.
690 If you overload the `initialize-instance', there you will need to
691 call `shared-initialize' yourself, or you can call `call-next-method'
692 to have this constructor called automatically. If these steps are
693 not taken, then new objects of your class will not have their values
694 dynamically set from SLOTS."
695 ;; First, see if any of our defaults are `lambda', and
696 ;; re-evaluate them and apply the value to our slots.
697 (let* ((this-class (eieio--object-class-object this))
698 (defaults (eieio--class-public-d this-class)))
699 (dolist (slot (eieio--class-public-a this-class))
700 ;; For each slot, see if we need to evaluate it.
702 ;; Paul Landes said in an email:
703 ;; > CL evaluates it if it can, and otherwise, leaves it as
704 ;; > the quoted thing as you already have. This is by the
705 ;; > Sonya E. Keene book and other things I've look at on the
706 ;; > web.
707 (let ((dflt (eieio-default-eval-maybe (car defaults))))
708 (when (not (eq dflt (car defaults)))
709 (eieio-oset this slot dflt) ))
710 ;; Next.
711 (setq defaults (cdr defaults))))
712 ;; Shared initialize will parse our slots for us.
713 (shared-initialize this slots))
715 (cl-defgeneric slot-missing (object slot-name operation &optional new-value)
716 "Method invoked when an attempt to access a slot in OBJECT fails.")
718 (cl-defmethod slot-missing ((object eieio-default-superclass) slot-name
719 _operation &optional _new-value)
720 "Method invoked when an attempt to access a slot in OBJECT fails.
721 SLOT-NAME is the name of the failed slot, OPERATION is the type of access
722 that was requested, and optional NEW-VALUE is the value that was desired
723 to be set.
725 This method is called from `oref', `oset', and other functions which
726 directly reference slots in EIEIO objects."
727 (signal 'invalid-slot-name (list (eieio-object-name object)
728 slot-name)))
730 (cl-defgeneric slot-unbound (object class slot-name fn)
731 "Slot unbound is invoked during an attempt to reference an unbound slot.")
733 (cl-defmethod slot-unbound ((object eieio-default-superclass)
734 class slot-name fn)
735 "Slot unbound is invoked during an attempt to reference an unbound slot.
736 OBJECT is the instance of the object being reference. CLASS is the
737 class of OBJECT, and SLOT-NAME is the offending slot. This function
738 throws the signal `unbound-slot'. You can overload this function and
739 return the value to use in place of the unbound value.
740 Argument FN is the function signaling this error.
741 Use `slot-boundp' to determine if a slot is bound or not.
743 In CLOS, the argument list is (CLASS OBJECT SLOT-NAME), but
744 EIEIO can only dispatch on the first argument, so the first two are swapped."
745 (signal 'unbound-slot (list (eieio-class-name class)
746 (eieio-object-name object)
747 slot-name fn)))
749 (cl-defgeneric clone (obj &rest params)
750 "Make a copy of OBJ, and then supply PARAMS.
751 PARAMS is a parameter list of the same form used by `initialize-instance'.
753 When overloading `clone', be sure to call `call-next-method'
754 first and modify the returned object.")
756 (cl-defmethod clone ((obj eieio-default-superclass) &rest params)
757 "Make a copy of OBJ, and then apply PARAMS."
758 (let ((nobj (copy-sequence obj)))
759 (if (stringp (car params))
760 (funcall (if eieio-backward-compatibility #'ignore #'message)
761 "Obsolete name %S passed to clone" (pop params)))
762 (if params (shared-initialize nobj params))
763 nobj))
765 (cl-defgeneric destructor (this &rest params)
766 "Destructor for cleaning up any dynamic links to our object.")
768 (cl-defmethod destructor ((_this eieio-default-superclass) &rest _params)
769 "Destructor for cleaning up any dynamic links to our object.
770 Argument THIS is the object being destroyed. PARAMS are additional
771 ignored parameters."
772 ;; No cleanup... yet.
775 (cl-defgeneric object-print (this &rest strings)
776 "Pretty printer for object THIS. Call function `object-name' with STRINGS.
778 It is sometimes useful to put a summary of the object into the
779 default #<notation> string when using EIEIO browsing tools.
780 Implement this method to customize the summary.")
782 (cl-defmethod object-print ((this eieio-default-superclass) &rest strings)
783 "Pretty printer for object THIS. Call function `object-name' with STRINGS.
784 The default method for printing object THIS is to use the
785 function `object-name'.
787 It is sometimes useful to put a summary of the object into the
788 default #<notation> string when using EIEIO browsing tools.
790 Implement this function and specify STRINGS in a call to
791 `call-next-method' to provide additional summary information.
792 When passing in extra strings from child classes, always remember
793 to prepend a space."
794 (eieio-object-name this (apply #'concat strings)))
796 (defvar eieio-print-depth 0
797 "When printing, keep track of the current indentation depth.")
799 (cl-defgeneric object-write (this &optional comment)
800 "Write out object THIS to the current stream.
801 Optional COMMENT will add comments to the beginning of the output.")
803 (cl-defmethod object-write ((this eieio-default-superclass) &optional comment)
804 "Write object THIS out to the current stream.
805 This writes out the vector version of this object. Complex and recursive
806 object are discouraged from being written.
807 If optional COMMENT is non-nil, include comments when outputting
808 this object."
809 (when comment
810 (princ ";; Object ")
811 (princ (eieio-object-name-string this))
812 (princ "\n")
813 (princ comment)
814 (princ "\n"))
815 (let* ((cl (eieio-object-class this))
816 (cv (eieio--class-v cl)))
817 ;; Now output readable lisp to recreate this object
818 ;; It should look like this:
819 ;; (<constructor> <name> <slot> <slot> ... )
820 ;; Each slot's slot is writen using its :writer.
821 (princ (make-string (* eieio-print-depth 2) ? ))
822 (princ "(")
823 (princ (symbol-name (eieio--class-constructor (eieio-object-class this))))
824 (princ " ")
825 (prin1 (eieio-object-name-string this))
826 (princ "\n")
827 ;; Loop over all the public slots
828 (let ((publa (eieio--class-public-a cv))
829 (publd (eieio--class-public-d cv))
830 (publp (eieio--class-public-printer cv))
831 (eieio-print-depth (1+ eieio-print-depth)))
832 (while publa
833 (when (slot-boundp this (car publa))
834 (let ((i (eieio--class-slot-initarg cv (car publa)))
835 (v (eieio-oref this (car publa)))
837 (unless (or (not i) (equal v (car publd)))
838 (unless (bolp)
839 (princ "\n"))
840 (princ (make-string (* eieio-print-depth 2) ? ))
841 (princ (symbol-name i))
842 (if (car publp)
843 ;; Use our public printer
844 (progn
845 (princ " ")
846 (funcall (car publp) v))
847 ;; Use our generic override prin1 function.
848 (princ (if (or (eieio-object-p v)
849 (eieio-object-p (car-safe v)))
850 "\n" " "))
851 (eieio-override-prin1 v)))))
852 (setq publa (cdr publa) publd (cdr publd)
853 publp (cdr publp))))
854 (princ ")")
855 (when (= eieio-print-depth 0)
856 (princ "\n"))))
858 (defun eieio-override-prin1 (thing)
859 "Perform a `prin1' on THING taking advantage of object knowledge."
860 (cond ((eieio-object-p thing)
861 (object-write thing))
862 ((consp thing)
863 (eieio-list-prin1 thing))
864 ((eieio--class-p thing)
865 (princ (eieio--class-print-name thing)))
866 (t (prin1 thing))))
868 (defun eieio-list-prin1 (list)
869 "Display LIST where list may contain objects."
870 (if (not (eieio-object-p (car list)))
871 (progn
872 (princ "'")
873 (prin1 list))
874 (princ (make-string (* eieio-print-depth 2) ? ))
875 (princ "(list")
876 (let ((eieio-print-depth (1+ eieio-print-depth)))
877 (while list
878 (princ "\n")
879 (if (eieio-object-p (car list))
880 (object-write (car list))
881 (princ (make-string (* eieio-print-depth 2) ? ))
882 (eieio-override-prin1 (car list)))
883 (setq list (cdr list))))
884 (princ ")")))
887 ;;; Unimplemented functions from CLOS
889 (defun change-class (_obj _class)
890 "Change the class of OBJ to type CLASS.
891 This may create or delete slots, but does not affect the return value
892 of `eq'."
893 (error "EIEIO: `change-class' is unimplemented"))
895 ;; Hook ourselves into help system for describing classes and methods.
896 (add-hook 'help-fns-describe-function-functions 'eieio-help-constructor)
898 ;;; Interfacing with edebug
900 (defun eieio-edebug-prin1-to-string (print-function object &optional noescape)
901 "Display EIEIO OBJECT in fancy format.
903 Used as advice around `edebug-prin1-to-string', held in the
904 variable PRINT-FUNCTION. Optional argument NOESCAPE is passed to
905 `prin1-to-string' when appropriate."
906 (cond ((eieio--class-p object) (eieio--class-print-name object))
907 ((eieio-object-p object) (object-print object))
908 ((and (listp object) (or (eieio--class-p (car object))
909 (eieio-object-p (car object))))
910 (concat "(" (mapconcat
911 (lambda (x) (eieio-edebug-prin1-to-string print-function x))
912 object " ")
913 ")"))
914 (t (funcall print-function object noescape))))
916 (advice-add 'edebug-prin1-to-string
917 :around #'eieio-edebug-prin1-to-string)
920 ;;; Start of automatically extracted autoloads.
922 ;;;### (autoloads nil "eieio-custom" "eieio-custom.el" "2ec91e473fcad1ff20cd76edc4aab706")
923 ;;; Generated autoloads from eieio-custom.el
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.
929 \(fn OBJ &optional GROUP)" nil nil)
931 ;;;***
933 ;;;### (autoloads nil "eieio-opt" "eieio-opt.el" "d1910eb455f102989fc33bb3f5a9b614")
934 ;;; Generated autoloads from eieio-opt.el
936 (autoload 'eieio-browse "eieio-opt" "\
937 Create an object browser window to show all objects.
938 If optional ROOT-CLASS, then start with that, otherwise start with
939 variable `eieio-default-superclass'.
941 \(fn &optional ROOT-CLASS)" t nil)
943 (autoload 'eieio-help-class "eieio-opt" "\
944 Print help description for CLASS.
945 If CLASS is actually an object, then also display current values of that object.
947 \(fn CLASS)" nil nil)
949 (autoload 'eieio-help-constructor "eieio-opt" "\
950 Describe CTR if it is a class constructor.
952 \(fn CTR)" nil nil)
954 ;;;***
956 ;;; End of automatically extracted autoloads.
958 (provide 'eieio)
960 ;;; eieio ends here