Auto-commit of loaddefs files.
[emacs.git] / lisp / emacs-lisp / eieio.el
blob91469b4b96cf211b5992696f5ff141865bf158f1
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 (eieio--check-type listp superclasses)
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 function.
227 (defun ,testsym1 (obj)
228 ,(format "Test OBJ to see if it an object of type %S." name)
229 (and (eieio-object-p obj)
230 (same-class-p obj ',name)))
232 (defun ,testsym2 (obj)
233 ,(format
234 "Test OBJ to see if it an object is a child of type %S."
235 name)
236 (and (eieio-object-p obj)
237 (object-of-class-p obj ',name)))
239 ,@(when eieio-backward-compatibility
240 (let ((f (intern (format "%s-child-p" name))))
241 `((defalias ',f ',testsym2)
242 (make-obsolete
243 ',f ,(format "use (cl-typep ... '%s) instead" name) "25.1"))))
245 ;; When using typep, (typep OBJ 'myclass) returns t for objects which
246 ;; are subclasses of myclass. For our predicates, however, it is
247 ;; important for EIEIO to be backwards compatible, where
248 ;; myobject-p, and myobject-child-p are different.
249 ;; "cl" uses this technique to specify symbols with specific typep
250 ;; test, so we can let typep have the CLOS documented behavior
251 ;; while keeping our above predicate clean.
253 (put ',name 'cl-deftype-satisfies #',testsym2)
255 (eieio-defclass-internal ',name ',superclasses ',slots ',options-and-doc)
257 ,@accessors
259 ;; Create the constructor function
260 ,(if (eieio--class-option-assoc options-and-doc :abstract)
261 ;; Abstract classes cannot be instantiated. Say so.
262 (let ((abs (eieio--class-option-assoc options-and-doc :abstract)))
263 (if (not (stringp abs))
264 (setq abs (format "Class %s is abstract" name)))
265 `(defun ,name (&rest _)
266 ,(format "You cannot create a new object of type %S." name)
267 (error ,abs)))
269 ;; Non-abstract classes need a constructor.
270 `(defun ,name (&rest slots)
271 ,(format "Create a new object with name NAME of class type %S."
272 name)
273 (declare (compiler-macro
274 (lambda (whole)
275 (if (not (stringp (car slots)))
276 whole
277 (macroexp--warn-and-return
278 (format "Obsolete name arg %S to constructor %S"
279 (car slots) (car whole))
280 ;; Keep the name arg, for backward compatibility,
281 ;; but hide it so we don't trigger indefinitely.
282 `(,(car whole) (identity ,(car slots))
283 ,@(cdr slots)))))))
284 (apply #'eieio-constructor ',name slots))))))
287 ;;; CLOS style implementation of object creators.
289 (defun make-instance (class &rest initargs)
290 "Make a new instance of CLASS based on INITARGS.
291 CLASS is a class symbol. For example:
293 (make-instance 'foo)
295 INITARGS is a property list with keywords based on the :initarg
296 for each slot. For example:
298 (make-instance 'foo :slot1 value1 :slotN valueN)
300 Compatibility note:
302 If the first element of INITARGS is a string, it is used as the
303 name of the class.
305 In EIEIO, the class' constructor requires a name for use when printing.
306 `make-instance' in CLOS doesn't use names the way Emacs does, so the
307 class is used as the name slot instead when INITARGS doesn't start with
308 a string."
309 (apply (eieio--class-constructor class) initargs))
312 ;;; Get/Set slots in an object.
314 (defmacro oref (obj slot)
315 "Retrieve the value stored in OBJ in the slot named by SLOT.
316 Slot is the name of the slot when created by `defclass' or the label
317 created by the :initarg tag."
318 (declare (debug (form symbolp)))
319 `(eieio-oref ,obj (quote ,slot)))
321 (defalias 'slot-value 'eieio-oref)
322 (defalias 'set-slot-value 'eieio-oset)
324 (defmacro oref-default (obj slot)
325 "Get the default value of OBJ (maybe a class) for SLOT.
326 The default value is the value installed in a class with the :initform
327 tag. SLOT can be the slot name, or the tag specified by the :initarg
328 tag in the `defclass' call."
329 (declare (debug (form symbolp)))
330 `(eieio-oref-default ,obj (quote ,slot)))
332 ;;; Handy CLOS macros
334 (defmacro with-slots (spec-list object &rest body)
335 "Bind SPEC-LIST lexically to slot values in OBJECT, and execute BODY.
336 This establishes a lexical environment for referring to the slots in
337 the instance named by the given slot-names as though they were
338 variables. Within such a context the value of the slot can be
339 specified by using its slot name, as if it were a lexically bound
340 variable. Both setf and setq can be used to set the value of the
341 slot.
343 SPEC-LIST is of a form similar to `let'. For example:
345 ((VAR1 SLOT1)
346 SLOT2
347 SLOTN
348 (VARN+1 SLOTN+1))
350 Where each VAR is the local variable given to the associated
351 SLOT. A slot specified without a variable name is given a
352 variable name of the same name as the slot."
353 (declare (indent 2) (debug (sexp sexp def-body)))
354 (require 'cl-lib)
355 ;; Transform the spec-list into a cl-symbol-macrolet spec-list.
356 (let ((mappings (mapcar (lambda (entry)
357 (let ((var (if (listp entry) (car entry) entry))
358 (slot (if (listp entry) (cadr entry) entry)))
359 (list var `(slot-value ,object ',slot))))
360 spec-list)))
361 (append (list 'cl-symbol-macrolet mappings)
362 body)))
364 ;;; Simple generators, and query functions. None of these would do
365 ;; well embedded into an object.
367 (define-obsolete-function-alias
368 'object-class-fast #'eieio--object-class-name "24.4")
370 (cl-defgeneric eieio-object-name-string (obj)
371 "Return a string which is OBJ's name."
372 (declare (obsolete eieio-named "25.1")))
374 (defun eieio-object-name (obj &optional extra)
375 "Return a Lisp like symbol string for object OBJ.
376 If EXTRA, include that in the string returned to represent the symbol."
377 (eieio--check-type eieio-object-p obj)
378 (format "#<%s %s%s>" (eieio--object-class-name obj)
379 (eieio-object-name-string obj) (or extra "")))
380 (define-obsolete-function-alias 'object-name #'eieio-object-name "24.4")
382 (defconst eieio--object-names (make-hash-table :test #'eq :weakness 'key))
384 ;; In the past, every EIEIO object had a `name' field, so we had the two method
385 ;; below "for free". Since this field is very rarely used, we got rid of it
386 ;; and instead we keep it in a weak hash-tables, for those very rare objects
387 ;; that use it.
388 (cl-defmethod eieio-object-name-string (obj)
389 (or (gethash obj eieio--object-names)
390 (symbol-name (eieio-object-class obj))))
391 (define-obsolete-function-alias
392 'object-name-string #'eieio-object-name-string "24.4")
394 (cl-defmethod eieio-object-set-name-string (obj name)
395 "Set the string which is OBJ's NAME."
396 (declare (obsolete eieio-named "25.1"))
397 (eieio--check-type stringp name)
398 (setf (gethash obj eieio--object-names) name))
399 (define-obsolete-function-alias
400 'object-set-name-string 'eieio-object-set-name-string "24.4")
402 (defun eieio-object-class (obj)
403 "Return the class struct defining OBJ."
404 ;; FIXME: We say we return a "struct" but we return a symbol instead!
405 (eieio--check-type eieio-object-p obj)
406 (eieio--object-class-name obj))
407 (define-obsolete-function-alias 'object-class #'eieio-object-class "24.4")
408 ;; CLOS name, maybe?
409 (define-obsolete-function-alias 'class-of #'eieio-object-class "24.4")
411 (defun eieio-object-class-name (obj)
412 "Return a Lisp like symbol name for OBJ's class."
413 (eieio--check-type eieio-object-p obj)
414 (eieio-class-name (eieio--object-class-name obj)))
415 (define-obsolete-function-alias
416 'object-class-name 'eieio-object-class-name "24.4")
418 (defun eieio-class-parents (class)
419 "Return parent classes to CLASS. (overload of variable).
421 The CLOS function `class-direct-superclasses' is aliased to this function."
422 (let ((c (eieio-class-object class)))
423 (eieio--class-parent c)))
425 (define-obsolete-function-alias 'class-parents #'eieio-class-parents "24.4")
427 (defun eieio-class-children (class)
428 "Return child classes to CLASS.
429 The CLOS function `class-direct-subclasses' is aliased to this function."
430 (eieio--check-type class-p class)
431 (eieio--class-children (eieio--class-v class)))
432 (define-obsolete-function-alias
433 'class-children #'eieio-class-children "24.4")
435 ;; Official CLOS functions.
436 (define-obsolete-function-alias
437 'class-direct-superclasses #'eieio-class-parents "24.4")
438 (define-obsolete-function-alias
439 'class-direct-subclasses #'eieio-class-children "24.4")
441 (defmacro eieio-class-parent (class)
442 "Return first parent class to CLASS. (overload of variable)."
443 `(car (eieio-class-parents ,class)))
444 (define-obsolete-function-alias 'class-parent 'eieio-class-parent "24.4")
446 (defun same-class-p (obj class)
447 "Return t if OBJ is of class-type CLASS."
448 (setq class (eieio--class-object class))
449 (eieio--check-type eieio--class-p class)
450 (eieio--check-type eieio-object-p obj)
451 (eq (eieio--object-class-object obj) class))
453 (defun object-of-class-p (obj class)
454 "Return non-nil if OBJ is an instance of CLASS or CLASS' subclasses."
455 (eieio--check-type eieio-object-p obj)
456 ;; class will be checked one layer down
457 (child-of-class-p (eieio--object-class-object obj) class))
458 ;; Backwards compatibility
459 (defalias 'obj-of-class-p 'object-of-class-p)
461 (defun child-of-class-p (child class)
462 "Return non-nil if CHILD class is a subclass of CLASS."
463 (setq child (eieio--class-object child))
464 (eieio--check-type eieio--class-p child)
465 ;; `eieio-default-superclass' is never mentioned in eieio--class-parent,
466 ;; so we have to special case it here.
467 (or (eq class 'eieio-default-superclass)
468 (let ((p nil))
469 (setq class (eieio--class-object class))
470 (eieio--check-type eieio--class-p class)
471 (while (and child (not (eq child class)))
472 (setq p (append p (eieio--class-parent child))
473 child (pop p)))
474 (if child t))))
476 (defun object-slots (obj)
477 "Return list of slots available in OBJ."
478 (eieio--check-type eieio-object-p obj)
479 (eieio--class-public-a (eieio--object-class-object obj)))
481 (defun eieio--class-slot-initarg (class slot) "Fetch from CLASS, SLOT's :initarg."
482 (eieio--check-type eieio--class-p class)
483 (let ((ia (eieio--class-initarg-tuples class))
484 (f nil))
485 (while (and ia (not f))
486 (if (eq (cdr (car ia)) slot)
487 (setq f (car (car ia))))
488 (setq ia (cdr ia)))
491 ;;; Object Set macros
493 (defmacro oset (obj slot value)
494 "Set the value in OBJ for slot SLOT to VALUE.
495 SLOT is the slot name as specified in `defclass' or the tag created
496 with in the :initarg slot. VALUE can be any Lisp object."
497 (declare (debug (form symbolp form)))
498 `(eieio-oset ,obj (quote ,slot) ,value))
500 (defmacro oset-default (class slot value)
501 "Set the default slot in CLASS for SLOT to VALUE.
502 The default value is usually set with the :initform tag during class
503 creation. This allows users to change the default behavior of classes
504 after they are created."
505 (declare (debug (form symbolp form)))
506 `(eieio-oset-default ,class (quote ,slot) ,value))
508 ;;; CLOS queries into classes and slots
510 (defun slot-boundp (object slot)
511 "Return non-nil if OBJECT's SLOT is bound.
512 Setting a slot's value makes it bound. Calling `slot-makeunbound' will
513 make a slot unbound.
514 OBJECT can be an instance or a class."
515 ;; Skip typechecking while retrieving this value.
516 (let ((eieio-skip-typecheck t))
517 ;; Return nil if the magic symbol is in there.
518 (not (eq (cond
519 ((eieio-object-p object) (eieio-oref object slot))
520 ((class-p object) (eieio-oref-default object slot))
521 (t (signal 'wrong-type-argument (list 'eieio-object-p object))))
522 eieio-unbound))))
524 (defun slot-makeunbound (object slot)
525 "In OBJECT, make SLOT unbound."
526 (eieio-oset object slot eieio-unbound))
528 (defun slot-exists-p (object-or-class slot)
529 "Return non-nil if OBJECT-OR-CLASS has SLOT."
530 (let ((cv (cond ((eieio-object-p object-or-class)
531 (eieio--object-class-object object-or-class))
532 (t (eieio-class-object object-or-class)))))
533 (or (memq slot (eieio--class-public-a cv))
534 (memq slot (eieio--class-class-allocation-a cv)))
537 (defun find-class (symbol &optional errorp)
538 "Return the class that SYMBOL represents.
539 If there is no class, nil is returned if ERRORP is nil.
540 If ERRORP is non-nil, `wrong-argument-type' is signaled."
541 (if (not (class-p symbol))
542 (if errorp (signal 'wrong-type-argument (list 'class-p symbol))
543 nil)
544 (eieio--class-v symbol)))
546 ;;; Slightly more complex utility functions for objects
548 (defun object-assoc (key slot list)
549 "Return an object if KEY is `equal' to SLOT's value of an object in LIST.
550 LIST is a list of objects whose slots are searched.
551 Objects in LIST do not need to have a slot named SLOT, nor does
552 SLOT need to be bound. If these errors occur, those objects will
553 be ignored."
554 (eieio--check-type listp list)
555 (while (and list (not (condition-case nil
556 ;; This prevents errors for missing slots.
557 (equal key (eieio-oref (car list) slot))
558 (error nil))))
559 (setq list (cdr list)))
560 (car list))
562 (defun object-assoc-list (slot list)
563 "Return an association list with the contents of SLOT as the key element.
564 LIST must be a list of objects with SLOT in it.
565 This is useful when you need to do completing read on an object group."
566 (eieio--check-type listp list)
567 (let ((assoclist nil))
568 (while list
569 (setq assoclist (cons (cons (eieio-oref (car list) slot)
570 (car list))
571 assoclist))
572 (setq list (cdr list)))
573 (nreverse assoclist)))
575 (defun object-assoc-list-safe (slot list)
576 "Return an association list with the contents of SLOT as the key element.
577 LIST must be a list of objects, but those objects do not need to have
578 SLOT in it. If it does not, then that element is left out of the association
579 list."
580 (eieio--check-type listp list)
581 (let ((assoclist nil))
582 (while list
583 (if (slot-exists-p (car list) slot)
584 (setq assoclist (cons (cons (eieio-oref (car list) slot)
585 (car list))
586 assoclist)))
587 (setq list (cdr list)))
588 (nreverse assoclist)))
590 (defun object-add-to-list (object slot item &optional append)
591 "In OBJECT's SLOT, add ITEM to the list of elements.
592 Optional argument APPEND indicates we need to append to the list.
593 If ITEM already exists in the list in SLOT, then it is not added.
594 Comparison is done with `equal' through the `member' function call.
595 If SLOT is unbound, bind it to the list containing ITEM."
596 (let (ov)
597 ;; Find the originating list.
598 (if (not (slot-boundp object slot))
599 (setq ov (list item))
600 (setq ov (eieio-oref object slot))
601 ;; turn it into a list.
602 (unless (listp ov)
603 (setq ov (list ov)))
604 ;; Do the combination
605 (if (not (member item ov))
606 (setq ov
607 (if append
608 (append ov (list item))
609 (cons item ov)))))
610 ;; Set back into the slot.
611 (eieio-oset object slot ov)))
613 (defun object-remove-from-list (object slot item)
614 "In OBJECT's SLOT, remove occurrences of ITEM.
615 Deletion is done with `delete', which deletes by side effect,
616 and comparisons are done with `equal'.
617 If SLOT is unbound, do nothing."
618 (if (not (slot-boundp object slot))
620 (eieio-oset object slot (delete item (eieio-oref object slot)))))
622 ;;; Here are some CLOS items that need the CL package
625 (gv-define-simple-setter eieio-oref eieio-oset)
629 ;; We want all objects created by EIEIO to have some default set of
630 ;; behaviors so we can create object utilities, and allow various
631 ;; types of error checking. To do this, create the default EIEIO
632 ;; class, and when no parent class is specified, use this as the
633 ;; default. (But don't store it in the other classes as the default,
634 ;; allowing for transparent support.)
637 (defclass eieio-default-superclass nil
639 "Default parent class for classes with no specified parent class.
640 Its slots are automatically adopted by classes with no specified parents.
641 This class is not stored in the `parent' slot of a class vector."
642 :abstract t)
644 (setq eieio-default-superclass (eieio--class-v 'eieio-default-superclass))
646 (defalias 'standard-class 'eieio-default-superclass)
648 (cl-defgeneric eieio-constructor (class &rest slots)
649 "Default constructor for CLASS `eieio-default-superclass'.")
651 (define-obsolete-function-alias 'constructor #'eieio-constructor "25.1")
653 (cl-defmethod eieio-constructor
654 ((class (subclass eieio-default-superclass)) &rest slots)
655 "Default constructor for CLASS `eieio-default-superclass'.
656 SLOTS are the initialization slots used by `shared-initialize'.
657 This static method is called when an object is constructed.
658 It allocates the vector used to represent an EIEIO object, and then
659 calls `shared-initialize' on that object."
660 (let* ((new-object (copy-sequence (eieio--class-default-object-cache
661 (eieio--class-v class)))))
662 (if (and slots
663 (let ((x (car slots)))
664 (or (stringp x) (null x))))
665 (funcall (if eieio-backward-compatibility #'ignore #'message)
666 "Obsolete name %S passed to %S constructor"
667 (pop slots) class))
668 ;; Call the initialize method on the new object with the slots
669 ;; that were passed down to us.
670 (initialize-instance new-object slots)
671 ;; Return the created object.
672 new-object))
674 (cl-defgeneric shared-initialize (obj slots)
675 "Set slots of OBJ with SLOTS which is a list of name/value pairs.
676 Called from the constructor routine.")
678 (cl-defmethod shared-initialize ((obj eieio-default-superclass) slots)
679 "Set slots of OBJ with SLOTS which is a list of name/value pairs.
680 Called from the constructor routine."
681 (while slots
682 (let ((rn (eieio--initarg-to-attribute (eieio--object-class-object obj)
683 (car slots))))
684 (if (not rn)
685 (slot-missing obj (car slots) 'oset (car (cdr slots)))
686 (eieio-oset obj rn (car (cdr slots)))))
687 (setq slots (cdr (cdr slots)))))
689 (cl-defgeneric initialize-instance (this &optional slots)
690 "Construct the new object THIS based on SLOTS.")
692 (cl-defmethod initialize-instance ((this eieio-default-superclass)
693 &optional slots)
694 "Construct the new object THIS based on SLOTS.
695 SLOTS is a tagged list where odd numbered elements are tags, and
696 even numbered elements are the values to store in the tagged slot.
697 If you overload the `initialize-instance', there you will need to
698 call `shared-initialize' yourself, or you can call `call-next-method'
699 to have this constructor called automatically. If these steps are
700 not taken, then new objects of your class will not have their values
701 dynamically set from SLOTS."
702 ;; First, see if any of our defaults are `lambda', and
703 ;; re-evaluate them and apply the value to our slots.
704 (let* ((this-class (eieio--object-class-object this))
705 (slot (eieio--class-public-a this-class))
706 (defaults (eieio--class-public-d this-class)))
707 (while slot
708 ;; For each slot, see if we need to evaluate it.
710 ;; Paul Landes said in an email:
711 ;; > CL evaluates it if it can, and otherwise, leaves it as
712 ;; > the quoted thing as you already have. This is by the
713 ;; > Sonya E. Keene book and other things I've look at on the
714 ;; > web.
715 (let ((dflt (eieio-default-eval-maybe (car defaults))))
716 (when (not (eq dflt (car defaults)))
717 (eieio-oset this (car slot) dflt) ))
718 ;; Next.
719 (setq slot (cdr slot)
720 defaults (cdr defaults))))
721 ;; Shared initialize will parse our slots for us.
722 (shared-initialize this slots))
724 (cl-defgeneric slot-missing (object slot-name operation &optional new-value)
725 "Method invoked when an attempt to access a slot in OBJECT fails.")
727 (cl-defmethod slot-missing ((object eieio-default-superclass) slot-name
728 _operation &optional _new-value)
729 "Method invoked when an attempt to access a slot in OBJECT fails.
730 SLOT-NAME is the name of the failed slot, OPERATION is the type of access
731 that was requested, and optional NEW-VALUE is the value that was desired
732 to be set.
734 This method is called from `oref', `oset', and other functions which
735 directly reference slots in EIEIO objects."
736 (signal 'invalid-slot-name (list (eieio-object-name object)
737 slot-name)))
739 (cl-defgeneric slot-unbound (object class slot-name fn)
740 "Slot unbound is invoked during an attempt to reference an unbound slot.")
742 (cl-defmethod slot-unbound ((object eieio-default-superclass)
743 class slot-name fn)
744 "Slot unbound is invoked during an attempt to reference an unbound slot.
745 OBJECT is the instance of the object being reference. CLASS is the
746 class of OBJECT, and SLOT-NAME is the offending slot. This function
747 throws the signal `unbound-slot'. You can overload this function and
748 return the value to use in place of the unbound value.
749 Argument FN is the function signaling this error.
750 Use `slot-boundp' to determine if a slot is bound or not.
752 In CLOS, the argument list is (CLASS OBJECT SLOT-NAME), but
753 EIEIO can only dispatch on the first argument, so the first two are swapped."
754 (signal 'unbound-slot (list (eieio-class-name class) (eieio-object-name object)
755 slot-name fn)))
757 (cl-defgeneric clone (obj &rest params)
758 "Make a copy of OBJ, and then supply PARAMS.
759 PARAMS is a parameter list of the same form used by `initialize-instance'.
761 When overloading `clone', be sure to call `call-next-method'
762 first and modify the returned object.")
764 (cl-defmethod clone ((obj eieio-default-superclass) &rest params)
765 "Make a copy of OBJ, and then apply PARAMS."
766 (let ((nobj (copy-sequence obj)))
767 (if (stringp (car params))
768 (funcall (if eieio-backward-compatibility #'ignore #'message)
769 "Obsolete name %S passed to clone" (pop params)))
770 (if params (shared-initialize nobj params))
771 nobj))
773 (cl-defgeneric destructor (this &rest params)
774 "Destructor for cleaning up any dynamic links to our object.")
776 (cl-defmethod destructor ((_this eieio-default-superclass) &rest _params)
777 "Destructor for cleaning up any dynamic links to our object.
778 Argument THIS is the object being destroyed. PARAMS are additional
779 ignored parameters."
780 ;; No cleanup... yet.
783 (cl-defgeneric object-print (this &rest strings)
784 "Pretty printer for object THIS. Call function `object-name' with STRINGS.
786 It is sometimes useful to put a summary of the object into the
787 default #<notation> string when using EIEIO browsing tools.
788 Implement this method to customize the summary.")
790 (cl-defmethod object-print ((this eieio-default-superclass) &rest strings)
791 "Pretty printer for object THIS. Call function `object-name' with STRINGS.
792 The default method for printing object THIS is to use the
793 function `object-name'.
795 It is sometimes useful to put a summary of the object into the
796 default #<notation> string when using EIEIO browsing tools.
798 Implement this function and specify STRINGS in a call to
799 `call-next-method' to provide additional summary information.
800 When passing in extra strings from child classes, always remember
801 to prepend a space."
802 (eieio-object-name this (apply #'concat strings)))
804 (defvar eieio-print-depth 0
805 "When printing, keep track of the current indentation depth.")
807 (cl-defgeneric object-write (this &optional comment)
808 "Write out object THIS to the current stream.
809 Optional COMMENT will add comments to the beginning of the output.")
811 (cl-defmethod object-write ((this eieio-default-superclass) &optional comment)
812 "Write object THIS out to the current stream.
813 This writes out the vector version of this object. Complex and recursive
814 object are discouraged from being written.
815 If optional COMMENT is non-nil, include comments when outputting
816 this object."
817 (when comment
818 (princ ";; Object ")
819 (princ (eieio-object-name-string this))
820 (princ "\n")
821 (princ comment)
822 (princ "\n"))
823 (let* ((cl (eieio-object-class this))
824 (cv (eieio--class-v cl)))
825 ;; Now output readable lisp to recreate this object
826 ;; It should look like this:
827 ;; (<constructor> <name> <slot> <slot> ... )
828 ;; Each slot's slot is writen using its :writer.
829 (princ (make-string (* eieio-print-depth 2) ? ))
830 (princ "(")
831 (princ (symbol-name (eieio--class-constructor (eieio-object-class this))))
832 (princ " ")
833 (prin1 (eieio-object-name-string this))
834 (princ "\n")
835 ;; Loop over all the public slots
836 (let ((publa (eieio--class-public-a cv))
837 (publd (eieio--class-public-d cv))
838 (publp (eieio--class-public-printer cv))
839 (eieio-print-depth (1+ eieio-print-depth)))
840 (while publa
841 (when (slot-boundp this (car publa))
842 (let ((i (eieio--class-slot-initarg cv (car publa)))
843 (v (eieio-oref this (car publa)))
845 (unless (or (not i) (equal v (car publd)))
846 (unless (bolp)
847 (princ "\n"))
848 (princ (make-string (* eieio-print-depth 2) ? ))
849 (princ (symbol-name i))
850 (if (car publp)
851 ;; Use our public printer
852 (progn
853 (princ " ")
854 (funcall (car publp) v))
855 ;; Use our generic override prin1 function.
856 (princ (if (or (eieio-object-p v)
857 (eieio-object-p (car-safe v)))
858 "\n" " "))
859 (eieio-override-prin1 v)))))
860 (setq publa (cdr publa) publd (cdr publd)
861 publp (cdr publp))))
862 (princ ")")
863 (when (= eieio-print-depth 0)
864 (princ "\n"))))
866 (defun eieio-override-prin1 (thing)
867 "Perform a `prin1' on THING taking advantage of object knowledge."
868 (cond ((eieio-object-p thing)
869 (object-write thing))
870 ((consp thing)
871 (eieio-list-prin1 thing))
872 ((class-p thing)
873 (princ (eieio-class-name thing)))
874 ((or (keywordp thing) (booleanp thing))
875 (prin1 thing))
876 ((symbolp thing)
877 (princ (concat "'" (symbol-name thing))))
878 (t (prin1 thing))))
880 (defun eieio-list-prin1 (list)
881 "Display LIST where list may contain objects."
882 (if (not (eieio-object-p (car list)))
883 (progn
884 (princ "'")
885 (prin1 list))
886 (princ (make-string (* eieio-print-depth 2) ? ))
887 (princ "(list")
888 (let ((eieio-print-depth (1+ eieio-print-depth)))
889 (while list
890 (princ "\n")
891 (if (eieio-object-p (car list))
892 (object-write (car list))
893 (princ (make-string (* eieio-print-depth 2) ? ))
894 (eieio-override-prin1 (car list)))
895 (setq list (cdr list))))
896 (princ ")")))
899 ;;; Unimplemented functions from CLOS
901 (defun change-class (_obj _class)
902 "Change the class of OBJ to type CLASS.
903 This may create or delete slots, but does not affect the return value
904 of `eq'."
905 (error "EIEIO: `change-class' is unimplemented"))
907 ;; Hook ourselves into help system for describing classes and methods.
908 (add-hook 'help-fns-describe-function-functions 'eieio-help-constructor)
910 ;;; Interfacing with edebug
912 (defun eieio-edebug-prin1-to-string (print-function object &optional noescape)
913 "Display EIEIO OBJECT in fancy format.
915 Used as advice around `edebug-prin1-to-string', held in the
916 variable PRINT-FUNCTION. Optional argument NOESCAPE is passed to
917 `prin1-to-string' when appropriate."
918 (cond ((eieio--class-p object) (eieio-class-name object))
919 ((eieio-object-p object) (object-print object))
920 ((and (listp object) (or (eieio--class-p (car object))
921 (eieio-object-p (car object))))
922 (concat "(" (mapconcat
923 (lambda (x) (eieio-edebug-prin1-to-string print-function x))
924 object " ")
925 ")"))
926 (t (funcall print-function object noescape))))
928 (advice-add 'edebug-prin1-to-string
929 :around #'eieio-edebug-prin1-to-string)
932 ;;; Start of automatically extracted autoloads.
934 ;;;### (autoloads nil "eieio-custom" "eieio-custom.el" "2ec91e473fcad1ff20cd76edc4aab706")
935 ;;; Generated autoloads from eieio-custom.el
937 (autoload 'customize-object "eieio-custom" "\
938 Customize OBJ in a custom buffer.
939 Optional argument GROUP is the sub-group of slots to display.
941 \(fn OBJ &optional GROUP)" nil nil)
943 ;;;***
945 ;;;### (autoloads nil "eieio-opt" "eieio-opt.el" "b849f8bf1312d5ef57e53d02173e4b5a")
946 ;;; Generated autoloads from eieio-opt.el
948 (autoload 'eieio-browse "eieio-opt" "\
949 Create an object browser window to show all objects.
950 If optional ROOT-CLASS, then start with that, otherwise start with
951 variable `eieio-default-superclass'.
953 \(fn &optional ROOT-CLASS)" t nil)
955 (autoload 'eieio-help-class "eieio-opt" "\
956 Print help description for CLASS.
957 If CLASS is actually an object, then also display current values of that object.
959 \(fn CLASS)" nil nil)
961 (autoload 'eieio-help-constructor "eieio-opt" "\
962 Describe CTR if it is a class constructor.
964 \(fn CTR)" nil nil)
966 ;;;***
968 ;;; End of automatically extracted autoloads.
970 (provide 'eieio)
972 ;;; eieio ends here