merge master
[emacs.git] / lisp / emacs-lisp / eieio.el
blobc5597b83170ef2e0dd07ce4ead966b6fd174ddb1
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)
56 (require 'eieio-generic)
59 ;;; Defining a new class
61 (defmacro defclass (name superclasses slots &rest options-and-doc)
62 "Define NAME as a new class derived from SUPERCLASS with SLOTS.
63 OPTIONS-AND-DOC is used as the class' options and base documentation.
64 SUPERCLASSES is a list of superclasses to inherit from, with SLOTS
65 being the slots residing in that class definition. Supported tags are:
67 :initform - Initializing form.
68 :initarg - Tag used during initialization.
69 :accessor - Tag used to create a function to access this slot.
70 :allocation - Specify where the value is stored.
71 Defaults to `:instance', but could also be `:class'.
72 :writer - A function symbol which will `write' an object's slot.
73 :reader - A function symbol which will `read' an object.
74 :type - The type of data allowed in this slot (see `typep').
75 :documentation
76 - A string documenting use of this slot.
78 The following are extensions on CLOS:
79 :custom - When customizing an object, the custom :type. Public only.
80 :label - A text string label used for a slot when customizing.
81 :group - Name of a customization group this slot belongs in.
82 :printer - A function to call to print the value of a slot.
83 See `eieio-override-prin1' as an example.
85 A class can also have optional options. These options happen in place
86 of documentation (including a :documentation tag), in addition to
87 documentation, or not at all. Supported options are:
89 :documentation - The doc-string used for this class.
91 Options added to EIEIO:
93 :allow-nil-initform - Non-nil to skip typechecking of null initforms.
94 :custom-groups - List of custom group names. Organizes slots into
95 reasonable groups for customizations.
96 :abstract - Non-nil to prevent instances of this class.
97 If a string, use as an error string if someone does
98 try to make an instance.
99 :method-invocation-order
100 - Control the method invocation order if there is
101 multiple inheritance. Valid values are:
102 :breadth-first - The default.
103 :depth-first
105 Options in CLOS not supported in EIEIO:
107 :metaclass - Class to use in place of `standard-class'
108 :default-initargs - Initargs to use when initializing new objects of
109 this class.
111 Due to the way class options are set up, you can add any tags you wish,
112 and reference them using the function `class-option'."
113 (declare (doc-string 4))
114 (eieio--check-type listp superclasses)
116 (cond ((and (stringp (car options-and-doc))
117 (/= 1 (% (length options-and-doc) 2)))
118 (error "Too many arguments to `defclass'"))
119 ((and (symbolp (car options-and-doc))
120 (/= 0 (% (length options-and-doc) 2)))
121 (error "Too many arguments to `defclass'")))
123 (if (stringp (car options-and-doc))
124 (setq options-and-doc
125 (cons :documentation options-and-doc)))
127 ;; Make sure the method invocation order is a valid value.
128 (let ((io (eieio--class-option-assoc options-and-doc
129 :method-invocation-order)))
130 (when (and io (not (member io '(:depth-first :breadth-first :c3))))
131 (error "Method invocation order %s is not allowed" io)))
133 (let ((testsym1 (intern (concat (symbol-name name) "-p")))
134 (testsym2 (intern (format "eieio--childp--%s" name)))
135 (accessors ()))
137 ;; Collect the accessors we need to define.
138 (pcase-dolist (`(,sname . ,soptions) slots)
139 (let* ((acces (plist-get soptions :accessor))
140 (initarg (plist-get soptions :initarg))
141 (reader (plist-get soptions :reader))
142 (writer (plist-get soptions :writer))
143 (alloc (plist-get soptions :allocation))
144 (label (plist-get soptions :label)))
146 (if eieio-error-unsupported-class-tags
147 (let ((tmp soptions))
148 (while tmp
149 (if (not (member (car tmp) '(:accessor
150 :initform
151 :initarg
152 :documentation
153 :protection
154 :reader
155 :writer
156 :allocation
157 :type
158 :custom
159 :label
160 :group
161 :printer
162 :allow-nil-initform
163 :custom-groups)))
164 (signal 'invalid-slot-type (list (car tmp))))
165 (setq tmp (cdr (cdr tmp))))))
167 ;; Make sure the :allocation parameter has a valid value.
168 (if (not (memq alloc '(nil :class :instance)))
169 (signal 'invalid-slot-type (list :allocation alloc)))
171 ;; Label is nil, or a string
172 (if (not (or (null label) (stringp label)))
173 (signal 'invalid-slot-type (list :label label)))
175 ;; Is there an initarg, but allocation of class?
176 (if (and initarg (eq alloc :class))
177 (message "Class allocated slots do not need :initarg"))
179 ;; Anyone can have an accessor function. This creates a function
180 ;; of the specified name, and also performs a `defsetf' if applicable
181 ;; so that users can `setf' the space returned by this function.
182 (when acces
183 ;; FIXME: The defmethod below only defines a part of the generic
184 ;; function (good), but the define-setter below affects the whole
185 ;; generic function (bad)!
186 (push `(gv-define-setter ,acces (store object)
187 ;; Apparently, eieio-oset-default doesn't work like
188 ;; oref-default and only accept class arguments!
189 (list ',(if nil ;; (eq alloc :class)
190 'eieio-oset-default
191 'eieio-oset)
192 object '',sname store))
193 accessors)
194 (push `(defmethod ,acces ,(if (eq alloc :class) :static :primary)
195 ((this ,name))
196 ,(format
197 "Retrieve the slot `%S' from an object of class `%S'."
198 sname name)
199 (if (slot-boundp this ',sname)
200 ;; Use oref-default for :class allocated slots, since
201 ;; these also accept the use of a class argument instead
202 ;; of an object argument.
203 (,(if (eq alloc :class) 'eieio-oref-default 'eieio-oref)
204 this ',sname)
205 ;; Else - Some error? nil?
206 nil))
207 accessors))
209 ;; If a writer is defined, then create a generic method of that
210 ;; name whose purpose is to set the value of the slot.
211 (if writer
212 (push `(defmethod ,writer ((this ,name) value)
213 ,(format "Set the slot `%S' of an object of class `%S'."
214 sname name)
215 (setf (slot-value this ',sname) value))
216 accessors))
217 ;; If a reader is defined, then create a generic method
218 ;; of that name whose purpose is to access this slot value.
219 (if reader
220 (push `(defmethod ,reader ((this ,name))
221 ,(format "Access the slot `%S' from object of class `%S'."
222 sname name)
223 (slot-value this ',sname))
224 accessors))
227 `(progn
228 ;; This test must be created right away so we can have self-
229 ;; referencing classes. ei, a class whose slot can contain only
230 ;; pointers to itself.
232 ;; Create the test function.
233 (defun ,testsym1 (obj)
234 ,(format "Test OBJ to see if it an object of type %S." name)
235 (and (eieio-object-p obj)
236 (same-class-p obj ',name)))
238 (defun ,testsym2 (obj)
239 ,(format
240 "Test OBJ to see if it an object is a child of type %S."
241 name)
242 (and (eieio-object-p obj)
243 (object-of-class-p obj ',name)))
245 ,@(when eieio-backward-compatibility
246 (let ((f (intern (format "%s-child-p" name))))
247 `((defalias ',f ',testsym2)
248 (make-obsolete
249 ',f ,(format "use (cl-typep ... '%s) instead" name) "25.1"))))
251 ;; When using typep, (typep OBJ 'myclass) returns t for objects which
252 ;; are subclasses of myclass. For our predicates, however, it is
253 ;; important for EIEIO to be backwards compatible, where
254 ;; myobject-p, and myobject-child-p are different.
255 ;; "cl" uses this technique to specify symbols with specific typep
256 ;; test, so we can let typep have the CLOS documented behavior
257 ;; while keeping our above predicate clean.
259 (put ',name 'cl-deftype-satisfies #',testsym2)
261 (eieio-defclass-internal ',name ',superclasses ',slots ',options-and-doc)
263 ,@accessors
265 ;; Create the constructor function
266 ,(if (eieio--class-option-assoc options-and-doc :abstract)
267 ;; Abstract classes cannot be instantiated. Say so.
268 (let ((abs (eieio--class-option-assoc options-and-doc :abstract)))
269 (if (not (stringp abs))
270 (setq abs (format "Class %s is abstract" name)))
271 `(defun ,name (&rest _)
272 ,(format "You cannot create a new object of type %S." name)
273 (error ,abs)))
275 ;; Non-abstract classes need a constructor.
276 `(defun ,name (&rest slots)
277 ,(format "Create a new object with name NAME of class type %S."
278 name)
279 (apply #'eieio-constructor ',name slots))))))
282 ;;; CLOS style implementation of object creators.
284 (defun make-instance (class &rest initargs)
285 "Make a new instance of CLASS based on INITARGS.
286 CLASS is a class symbol. For example:
288 (make-instance 'foo)
290 INITARGS is a property list with keywords based on the :initarg
291 for each slot. For example:
293 (make-instance 'foo :slot1 value1 :slotN valueN)
295 Compatibility note:
297 If the first element of INITARGS is a string, it is used as the
298 name of the class.
300 In EIEIO, the class' constructor requires a name for use when printing.
301 `make-instance' in CLOS doesn't use names the way Emacs does, so the
302 class is used as the name slot instead when INITARGS doesn't start with
303 a string."
304 (apply (eieio--class-constructor class) initargs))
307 ;;; Get/Set slots in an object.
309 (defmacro oref (obj slot)
310 "Retrieve the value stored in OBJ in the slot named by SLOT.
311 Slot is the name of the slot when created by `defclass' or the label
312 created by the :initarg tag."
313 (declare (debug (form symbolp)))
314 `(eieio-oref ,obj (quote ,slot)))
316 (defalias 'slot-value 'eieio-oref)
317 (defalias 'set-slot-value 'eieio-oset)
319 (defmacro oref-default (obj slot)
320 "Get the default value of OBJ (maybe a class) for SLOT.
321 The default value is the value installed in a class with the :initform
322 tag. SLOT can be the slot name, or the tag specified by the :initarg
323 tag in the `defclass' call."
324 (declare (debug (form symbolp)))
325 `(eieio-oref-default ,obj (quote ,slot)))
327 ;;; Handy CLOS macros
329 (defmacro with-slots (spec-list object &rest body)
330 "Bind SPEC-LIST lexically to slot values in OBJECT, and execute BODY.
331 This establishes a lexical environment for referring to the slots in
332 the instance named by the given slot-names as though they were
333 variables. Within such a context the value of the slot can be
334 specified by using its slot name, as if it were a lexically bound
335 variable. Both setf and setq can be used to set the value of the
336 slot.
338 SPEC-LIST is of a form similar to `let'. For example:
340 ((VAR1 SLOT1)
341 SLOT2
342 SLOTN
343 (VARN+1 SLOTN+1))
345 Where each VAR is the local variable given to the associated
346 SLOT. A slot specified without a variable name is given a
347 variable name of the same name as the slot."
348 (declare (indent 2) (debug (sexp sexp def-body)))
349 (require 'cl-lib)
350 ;; Transform the spec-list into a cl-symbol-macrolet spec-list.
351 (let ((mappings (mapcar (lambda (entry)
352 (let ((var (if (listp entry) (car entry) entry))
353 (slot (if (listp entry) (cadr entry) entry)))
354 (list var `(slot-value ,object ',slot))))
355 spec-list)))
356 (append (list 'cl-symbol-macrolet mappings)
357 body)))
359 ;;; Simple generators, and query functions. None of these would do
360 ;; well embedded into an object.
362 (define-obsolete-function-alias
363 'object-class-fast #'eieio--object-class-name "24.4")
365 (defun eieio-object-name (obj &optional extra)
366 "Return a Lisp like symbol string for object OBJ.
367 If EXTRA, include that in the string returned to represent the symbol."
368 (eieio--check-type eieio-object-p obj)
369 (format "#<%s %s%s>" (eieio--object-class-name obj)
370 (eieio-object-name-string obj) (or extra "")))
371 (define-obsolete-function-alias 'object-name #'eieio-object-name "24.4")
373 (defconst eieio--object-names (make-hash-table :test #'eq :weakness 'key))
375 ;; In the past, every EIEIO object had a `name' field, so we had the two method
376 ;; below "for free". Since this field is very rarely used, we got rid of it
377 ;; and instead we keep it in a weak hash-tables, for those very rare objects
378 ;; that use it.
379 (defmethod eieio-object-name-string (obj)
380 "Return a string which is OBJ's name."
381 (declare (obsolete eieio-named "25.1"))
382 (or (gethash obj eieio--object-names)
383 (symbol-name (eieio-object-class obj))))
384 (define-obsolete-function-alias
385 'object-name-string #'eieio-object-name-string "24.4")
387 (defmethod eieio-object-set-name-string (obj name)
388 "Set the string which is OBJ's NAME."
389 (declare (obsolete eieio-named "25.1"))
390 (eieio--check-type stringp name)
391 (setf (gethash obj eieio--object-names) name))
392 (define-obsolete-function-alias
393 'object-set-name-string 'eieio-object-set-name-string "24.4")
395 (defun eieio-object-class (obj)
396 "Return the class struct defining OBJ."
397 ;; FIXME: We say we return a "struct" but we return a symbol instead!
398 (eieio--check-type eieio-object-p obj)
399 (eieio--object-class-name obj))
400 (define-obsolete-function-alias 'object-class #'eieio-object-class "24.4")
401 ;; CLOS name, maybe?
402 (define-obsolete-function-alias 'class-of #'eieio-object-class "24.4")
404 (defun eieio-object-class-name (obj)
405 "Return a Lisp like symbol name for OBJ's class."
406 (eieio--check-type eieio-object-p obj)
407 (eieio-class-name (eieio--object-class-name obj)))
408 (define-obsolete-function-alias
409 'object-class-name 'eieio-object-class-name "24.4")
411 (defun eieio-class-parents (class)
412 "Return parent classes to CLASS. (overload of variable).
414 The CLOS function `class-direct-superclasses' is aliased to this function."
415 (let ((c (eieio-class-object class)))
416 (eieio--class-parent c)))
418 (define-obsolete-function-alias 'class-parents #'eieio-class-parents "24.4")
420 (defun eieio-class-children (class)
421 "Return child classes to CLASS.
422 The CLOS function `class-direct-subclasses' is aliased to this function."
423 (eieio--check-type class-p class)
424 (eieio--class-children (eieio--class-v class)))
425 (define-obsolete-function-alias
426 'class-children #'eieio-class-children "24.4")
428 ;; Official CLOS functions.
429 (define-obsolete-function-alias
430 'class-direct-superclasses #'eieio-class-parents "24.4")
431 (define-obsolete-function-alias
432 'class-direct-subclasses #'eieio-class-children "24.4")
434 (defmacro eieio-class-parent (class)
435 "Return first parent class to CLASS. (overload of variable)."
436 `(car (eieio-class-parents ,class)))
437 (define-obsolete-function-alias 'class-parent 'eieio-class-parent "24.4")
439 (defun same-class-p (obj class)
440 "Return t if OBJ is of class-type CLASS."
441 (setq class (eieio--class-object class))
442 (eieio--check-type eieio--class-p class)
443 (eieio--check-type eieio-object-p obj)
444 (eq (eieio--object-class-object obj) class))
446 (defun object-of-class-p (obj class)
447 "Return non-nil if OBJ is an instance of CLASS or CLASS' subclasses."
448 (eieio--check-type eieio-object-p obj)
449 ;; class will be checked one layer down
450 (child-of-class-p (eieio--object-class-object obj) class))
451 ;; Backwards compatibility
452 (defalias 'obj-of-class-p 'object-of-class-p)
454 (defun child-of-class-p (child class)
455 "Return non-nil if CHILD class is a subclass of CLASS."
456 (setq child (eieio--class-object child))
457 (eieio--check-type eieio--class-p child)
458 ;; `eieio-default-superclass' is never mentioned in eieio--class-parent,
459 ;; so we have to special case it here.
460 (or (eq class 'eieio-default-superclass)
461 (let ((p nil))
462 (setq class (eieio--class-object class))
463 (eieio--check-type eieio--class-p class)
464 (while (and child (not (eq child class)))
465 (setq p (append p (eieio--class-parent child))
466 child (pop p)))
467 (if child t))))
469 (defun object-slots (obj)
470 "Return list of slots available in OBJ."
471 (eieio--check-type eieio-object-p obj)
472 (eieio--class-public-a (eieio--object-class-object obj)))
474 (defun eieio--class-slot-initarg (class slot) "Fetch from CLASS, SLOT's :initarg."
475 (eieio--check-type eieio--class-p class)
476 (let ((ia (eieio--class-initarg-tuples class))
477 (f nil))
478 (while (and ia (not f))
479 (if (eq (cdr (car ia)) slot)
480 (setq f (car (car ia))))
481 (setq ia (cdr ia)))
484 ;;; Object Set macros
486 (defmacro oset (obj slot value)
487 "Set the value in OBJ for slot SLOT to VALUE.
488 SLOT is the slot name as specified in `defclass' or the tag created
489 with in the :initarg slot. VALUE can be any Lisp object."
490 (declare (debug (form symbolp form)))
491 `(eieio-oset ,obj (quote ,slot) ,value))
493 (defmacro oset-default (class slot value)
494 "Set the default slot in CLASS for SLOT to VALUE.
495 The default value is usually set with the :initform tag during class
496 creation. This allows users to change the default behavior of classes
497 after they are created."
498 (declare (debug (form symbolp form)))
499 `(eieio-oset-default ,class (quote ,slot) ,value))
501 ;;; CLOS queries into classes and slots
503 (defun slot-boundp (object slot)
504 "Return non-nil if OBJECT's SLOT is bound.
505 Setting a slot's value makes it bound. Calling `slot-makeunbound' will
506 make a slot unbound.
507 OBJECT can be an instance or a class."
508 ;; Skip typechecking while retrieving this value.
509 (let ((eieio-skip-typecheck t))
510 ;; Return nil if the magic symbol is in there.
511 (not (eq (cond
512 ((eieio-object-p object) (eieio-oref object slot))
513 ((class-p object) (eieio-oref-default object slot))
514 (t (signal 'wrong-type-argument (list 'eieio-object-p object))))
515 eieio-unbound))))
517 (defun slot-makeunbound (object slot)
518 "In OBJECT, make SLOT unbound."
519 (eieio-oset object slot eieio-unbound))
521 (defun slot-exists-p (object-or-class slot)
522 "Return non-nil if OBJECT-OR-CLASS has SLOT."
523 (let ((cv (cond ((eieio-object-p object-or-class)
524 (eieio--object-class-object object-or-class))
525 (t (eieio-class-object object-or-class)))))
526 (or (memq slot (eieio--class-public-a cv))
527 (memq slot (eieio--class-class-allocation-a cv)))
530 (defun find-class (symbol &optional errorp)
531 "Return the class that SYMBOL represents.
532 If there is no class, nil is returned if ERRORP is nil.
533 If ERRORP is non-nil, `wrong-argument-type' is signaled."
534 (if (not (class-p symbol))
535 (if errorp (signal 'wrong-type-argument (list 'class-p symbol))
536 nil)
537 (eieio--class-v symbol)))
539 ;;; Slightly more complex utility functions for objects
541 (defun object-assoc (key slot list)
542 "Return an object if KEY is `equal' to SLOT's value of an object in LIST.
543 LIST is a list of objects whose slots are searched.
544 Objects in LIST do not need to have a slot named SLOT, nor does
545 SLOT need to be bound. If these errors occur, those objects will
546 be ignored."
547 (eieio--check-type listp list)
548 (while (and list (not (condition-case nil
549 ;; This prevents errors for missing slots.
550 (equal key (eieio-oref (car list) slot))
551 (error nil))))
552 (setq list (cdr list)))
553 (car list))
555 (defun object-assoc-list (slot list)
556 "Return an association list with the contents of SLOT as the key element.
557 LIST must be a list of objects with SLOT in it.
558 This is useful when you need to do completing read on an object group."
559 (eieio--check-type listp list)
560 (let ((assoclist nil))
561 (while list
562 (setq assoclist (cons (cons (eieio-oref (car list) slot)
563 (car list))
564 assoclist))
565 (setq list (cdr list)))
566 (nreverse assoclist)))
568 (defun object-assoc-list-safe (slot list)
569 "Return an association list with the contents of SLOT as the key element.
570 LIST must be a list of objects, but those objects do not need to have
571 SLOT in it. If it does not, then that element is left out of the association
572 list."
573 (eieio--check-type listp list)
574 (let ((assoclist nil))
575 (while list
576 (if (slot-exists-p (car list) slot)
577 (setq assoclist (cons (cons (eieio-oref (car list) slot)
578 (car list))
579 assoclist)))
580 (setq list (cdr list)))
581 (nreverse assoclist)))
583 (defun object-add-to-list (object slot item &optional append)
584 "In OBJECT's SLOT, add ITEM to the list of elements.
585 Optional argument APPEND indicates we need to append to the list.
586 If ITEM already exists in the list in SLOT, then it is not added.
587 Comparison is done with `equal' through the `member' function call.
588 If SLOT is unbound, bind it to the list containing ITEM."
589 (let (ov)
590 ;; Find the originating list.
591 (if (not (slot-boundp object slot))
592 (setq ov (list item))
593 (setq ov (eieio-oref object slot))
594 ;; turn it into a list.
595 (unless (listp ov)
596 (setq ov (list ov)))
597 ;; Do the combination
598 (if (not (member item ov))
599 (setq ov
600 (if append
601 (append ov (list item))
602 (cons item ov)))))
603 ;; Set back into the slot.
604 (eieio-oset object slot ov)))
606 (defun object-remove-from-list (object slot item)
607 "In OBJECT's SLOT, remove occurrences of ITEM.
608 Deletion is done with `delete', which deletes by side effect,
609 and comparisons are done with `equal'.
610 If SLOT is unbound, do nothing."
611 (if (not (slot-boundp object slot))
613 (eieio-oset object slot (delete item (eieio-oref object slot)))))
615 ;;; Here are some CLOS items that need the CL package
618 (gv-define-simple-setter eieio-oref eieio-oset)
622 ;; We want all objects created by EIEIO to have some default set of
623 ;; behaviors so we can create object utilities, and allow various
624 ;; types of error checking. To do this, create the default EIEIO
625 ;; class, and when no parent class is specified, use this as the
626 ;; default. (But don't store it in the other classes as the default,
627 ;; allowing for transparent support.)
630 (defclass eieio-default-superclass nil
632 "Default parent class for classes with no specified parent class.
633 Its slots are automatically adopted by classes with no specified parents.
634 This class is not stored in the `parent' slot of a class vector."
635 :abstract t)
637 (setq eieio-default-superclass (eieio--class-v 'eieio-default-superclass))
639 (defalias 'standard-class 'eieio-default-superclass)
641 (defgeneric eieio-constructor (class &rest slots)
642 "Default constructor for CLASS `eieio-default-superclass'.")
644 (define-obsolete-function-alias 'constructor #'eieio-constructor "25.1")
646 (defmethod eieio-constructor :static
647 ((class eieio-default-superclass) &rest slots)
648 "Default constructor for CLASS `eieio-default-superclass'.
649 SLOTS are the initialization slots used by `shared-initialize'.
650 This static method is called when an object is constructed.
651 It allocates the vector used to represent an EIEIO object, and then
652 calls `shared-initialize' on that object."
653 (let* ((new-object (copy-sequence (eieio--class-default-object-cache
654 (eieio--class-v class)))))
655 (if (and slots
656 (let ((x (car slots)))
657 (or (stringp x) (null x))))
658 (funcall (if eieio-backward-compatibility #'ignore #'message)
659 "Obsolete name %S passed to %S constructor"
660 (pop slots) class))
661 ;; Call the initialize method on the new object with the slots
662 ;; that were passed down to us.
663 (initialize-instance new-object slots)
664 ;; Return the created object.
665 new-object))
667 (defgeneric shared-initialize (obj slots)
668 "Set slots of OBJ with SLOTS which is a list of name/value pairs.
669 Called from the constructor routine.")
671 (defmethod shared-initialize ((obj eieio-default-superclass) slots)
672 "Set slots of OBJ with SLOTS which is a list of name/value pairs.
673 Called from the constructor routine."
674 (while slots
675 (let ((rn (eieio--initarg-to-attribute (eieio--object-class-object obj)
676 (car slots))))
677 (if (not rn)
678 (slot-missing obj (car slots) 'oset (car (cdr slots)))
679 (eieio-oset obj rn (car (cdr slots)))))
680 (setq slots (cdr (cdr slots)))))
682 (defgeneric initialize-instance (this &optional slots)
683 "Construct the new object THIS based on SLOTS.")
685 (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 (slot (eieio--class-public-a this-class))
699 (defaults (eieio--class-public-d this-class)))
700 (while slot
701 ;; For each slot, see if we need to evaluate it.
703 ;; Paul Landes said in an email:
704 ;; > CL evaluates it if it can, and otherwise, leaves it as
705 ;; > the quoted thing as you already have. This is by the
706 ;; > Sonya E. Keene book and other things I've look at on the
707 ;; > web.
708 (let ((dflt (eieio-default-eval-maybe (car defaults))))
709 (when (not (eq dflt (car defaults)))
710 (eieio-oset this (car slot) dflt) ))
711 ;; Next.
712 (setq slot (cdr slot)
713 defaults (cdr defaults))))
714 ;; Shared initialize will parse our slots for us.
715 (shared-initialize this slots))
717 (defgeneric slot-missing (object slot-name operation &optional new-value)
718 "Method invoked when an attempt to access a slot in OBJECT fails.")
720 (defmethod slot-missing ((object eieio-default-superclass) slot-name
721 _operation &optional _new-value)
722 "Method invoked when an attempt to access a slot in OBJECT fails.
723 SLOT-NAME is the name of the failed slot, OPERATION is the type of access
724 that was requested, and optional NEW-VALUE is the value that was desired
725 to be set.
727 This method is called from `oref', `oset', and other functions which
728 directly reference slots in EIEIO objects."
729 (signal 'invalid-slot-name (list (eieio-object-name object)
730 slot-name)))
732 (defgeneric slot-unbound (object class slot-name fn)
733 "Slot unbound is invoked during an attempt to reference an unbound slot.")
735 (defmethod slot-unbound ((object eieio-default-superclass)
736 class slot-name fn)
737 "Slot unbound is invoked during an attempt to reference an unbound slot.
738 OBJECT is the instance of the object being reference. CLASS is the
739 class of OBJECT, and SLOT-NAME is the offending slot. This function
740 throws the signal `unbound-slot'. You can overload this function and
741 return the value to use in place of the unbound value.
742 Argument FN is the function signaling this error.
743 Use `slot-boundp' to determine if a slot is bound or not.
745 In CLOS, the argument list is (CLASS OBJECT SLOT-NAME), but
746 EIEIO can only dispatch on the first argument, so the first two are swapped."
747 (signal 'unbound-slot (list (eieio-class-name class) (eieio-object-name object)
748 slot-name fn)))
750 (defgeneric clone (obj &rest params)
751 "Make a copy of OBJ, and then supply PARAMS.
752 PARAMS is a parameter list of the same form used by `initialize-instance'.
754 When overloading `clone', be sure to call `call-next-method'
755 first and modify the returned object.")
757 (defmethod clone ((obj eieio-default-superclass) &rest params)
758 "Make a copy of OBJ, and then apply PARAMS."
759 (let ((nobj (copy-sequence obj)))
760 (if (stringp (car params))
761 (funcall (if eieio-backward-compatibility #'ignore #'message)
762 "Obsolete name %S passed to clone" (pop params)))
763 (if params (shared-initialize nobj params))
764 nobj))
766 (defgeneric destructor (this &rest params)
767 "Destructor for cleaning up any dynamic links to our object.")
769 (defmethod destructor ((_this eieio-default-superclass) &rest _params)
770 "Destructor for cleaning up any dynamic links to our object.
771 Argument THIS is the object being destroyed. PARAMS are additional
772 ignored parameters."
773 ;; No cleanup... yet.
776 (defgeneric object-print (this &rest strings)
777 "Pretty printer for object THIS. Call function `object-name' with STRINGS.
779 It is sometimes useful to put a summary of the object into the
780 default #<notation> string when using EIEIO browsing tools.
781 Implement this method to customize the summary.")
783 (defmethod object-print ((this eieio-default-superclass) &rest strings)
784 "Pretty printer for object THIS. Call function `object-name' with STRINGS.
785 The default method for printing object THIS is to use the
786 function `object-name'.
788 It is sometimes useful to put a summary of the object into the
789 default #<notation> string when using EIEIO browsing tools.
791 Implement this function and specify STRINGS in a call to
792 `call-next-method' to provide additional summary information.
793 When passing in extra strings from child classes, always remember
794 to prepend a space."
795 (eieio-object-name this (apply #'concat strings)))
797 (defvar eieio-print-depth 0
798 "When printing, keep track of the current indentation depth.")
800 (defgeneric object-write (this &optional comment)
801 "Write out object THIS to the current stream.
802 Optional COMMENT will add comments to the beginning of the output.")
804 (defmethod object-write ((this eieio-default-superclass) &optional comment)
805 "Write object THIS out to the current stream.
806 This writes out the vector version of this object. Complex and recursive
807 object are discouraged from being written.
808 If optional COMMENT is non-nil, include comments when outputting
809 this object."
810 (when comment
811 (princ ";; Object ")
812 (princ (eieio-object-name-string this))
813 (princ "\n")
814 (princ comment)
815 (princ "\n"))
816 (let* ((cl (eieio-object-class this))
817 (cv (eieio--class-v cl)))
818 ;; Now output readable lisp to recreate this object
819 ;; It should look like this:
820 ;; (<constructor> <name> <slot> <slot> ... )
821 ;; Each slot's slot is writen using its :writer.
822 (princ (make-string (* eieio-print-depth 2) ? ))
823 (princ "(")
824 (princ (symbol-name (eieio--class-constructor (eieio-object-class this))))
825 (princ " ")
826 (prin1 (eieio-object-name-string this))
827 (princ "\n")
828 ;; Loop over all the public slots
829 (let ((publa (eieio--class-public-a cv))
830 (publd (eieio--class-public-d cv))
831 (publp (eieio--class-public-printer cv))
832 (eieio-print-depth (1+ eieio-print-depth)))
833 (while publa
834 (when (slot-boundp this (car publa))
835 (let ((i (eieio--class-slot-initarg cv (car publa)))
836 (v (eieio-oref this (car publa)))
838 (unless (or (not i) (equal v (car publd)))
839 (unless (bolp)
840 (princ "\n"))
841 (princ (make-string (* eieio-print-depth 2) ? ))
842 (princ (symbol-name i))
843 (if (car publp)
844 ;; Use our public printer
845 (progn
846 (princ " ")
847 (funcall (car publp) v))
848 ;; Use our generic override prin1 function.
849 (princ (if (or (eieio-object-p v)
850 (eieio-object-p (car-safe v)))
851 "\n" " "))
852 (eieio-override-prin1 v)))))
853 (setq publa (cdr publa) publd (cdr publd)
854 publp (cdr publp))))
855 (princ ")")
856 (when (= eieio-print-depth 0)
857 (princ "\n"))))
859 (defun eieio-override-prin1 (thing)
860 "Perform a `prin1' on THING taking advantage of object knowledge."
861 (cond ((eieio-object-p thing)
862 (object-write thing))
863 ((consp thing)
864 (eieio-list-prin1 thing))
865 ((class-p thing)
866 (princ (eieio-class-name thing)))
867 ((or (keywordp thing) (booleanp thing))
868 (prin1 thing))
869 ((symbolp thing)
870 (princ (concat "'" (symbol-name thing))))
871 (t (prin1 thing))))
873 (defun eieio-list-prin1 (list)
874 "Display LIST where list may contain objects."
875 (if (not (eieio-object-p (car list)))
876 (progn
877 (princ "'")
878 (prin1 list))
879 (princ (make-string (* eieio-print-depth 2) ? ))
880 (princ "(list")
881 (let ((eieio-print-depth (1+ eieio-print-depth)))
882 (while list
883 (princ "\n")
884 (if (eieio-object-p (car list))
885 (object-write (car list))
886 (princ (make-string (* eieio-print-depth 2) ? ))
887 (eieio-override-prin1 (car list)))
888 (setq list (cdr list))))
889 (princ ")")))
892 ;;; Unimplemented functions from CLOS
894 (defun change-class (_obj _class)
895 "Change the class of OBJ to type CLASS.
896 This may create or delete slots, but does not affect the return value
897 of `eq'."
898 (error "EIEIO: `change-class' is unimplemented"))
900 ;; Hook ourselves into help system for describing classes and methods.
901 (add-hook 'help-fns-describe-function-functions 'eieio-help-constructor)
903 ;;; Interfacing with edebug
905 (defun eieio-edebug-prin1-to-string (print-function object &optional noescape)
906 "Display EIEIO OBJECT in fancy format.
908 Used as advice around `edebug-prin1-to-string', held in the
909 variable PRINT-FUNCTION. Optional argument NOESCAPE is passed to
910 `prin1-to-string' when appropriate."
911 (cond ((eieio--class-p object) (eieio-class-name object))
912 ((eieio-object-p object) (object-print object))
913 ((and (listp object) (or (eieio--class-p (car object))
914 (eieio-object-p (car object))))
915 (concat "(" (mapconcat
916 (lambda (x) (eieio-edebug-prin1-to-string print-function x))
917 object " ")
918 ")"))
919 (t (funcall print-function object noescape))))
921 (advice-add 'edebug-prin1-to-string
922 :around #'eieio-edebug-prin1-to-string)
925 ;;; Start of automatically extracted autoloads.
927 ;;;### (autoloads nil "eieio-custom" "eieio-custom.el" "6baa78cfc590cc0422e12b7eb55abf24")
928 ;;; Generated autoloads from eieio-custom.el
930 (autoload 'customize-object "eieio-custom" "\
931 Customize OBJ in a custom buffer.
932 Optional argument GROUP is the sub-group of slots to display.
934 \(fn OBJ &optional GROUP)" nil nil)
936 ;;;***
938 ;;;### (autoloads nil "eieio-opt" "eieio-opt.el" "e922bf7ebc7dcb272480c4ba148da1ac")
939 ;;; Generated autoloads from eieio-opt.el
941 (autoload 'eieio-browse "eieio-opt" "\
942 Create an object browser window to show all objects.
943 If optional ROOT-CLASS, then start with that, otherwise start with
944 variable `eieio-default-superclass'.
946 \(fn &optional ROOT-CLASS)" t nil)
948 (autoload 'eieio-help-class "eieio-opt" "\
949 Print help description for CLASS.
950 If CLASS is actually an object, then also display current values of that object.
952 \(fn CLASS)" nil nil)
954 (autoload 'eieio-help-constructor "eieio-opt" "\
955 Describe CTR if it is a class constructor.
957 \(fn CTR)" nil nil)
959 ;;;***
961 ;;; End of automatically extracted autoloads.
963 (provide 'eieio)
965 ;;; eieio ends here