EIEIO: Simplify help hyperlinks; Try and reduce hardcoding in .elc
[emacs.git] / lisp / emacs-lisp / eieio.el
blob526090954a9807074573762d003add1fe593cf4d
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 #'eieio-constructor ',name slots))))))
278 ;;; CLOS style implementation of object creators.
280 (defun make-instance (class &rest initargs)
281 "Make a new instance of CLASS based on INITARGS.
282 CLASS is a class symbol. For example:
284 (make-instance 'foo)
286 INITARGS is a property list with keywords based on the :initarg
287 for each slot. For example:
289 (make-instance 'foo :slot1 value1 :slotN valueN)
291 Compatibility note:
293 If the first element of INITARGS is a string, it is used as the
294 name of the class.
296 In EIEIO, the class' constructor requires a name for use when printing.
297 `make-instance' in CLOS doesn't use names the way Emacs does, so the
298 class is used as the name slot instead when INITARGS doesn't start with
299 a string."
300 (apply (eieio--class-constructor class) initargs))
303 ;;; Get/Set slots in an object.
305 (defmacro oref (obj slot)
306 "Retrieve the value stored in OBJ in the slot named by SLOT.
307 Slot is the name of the slot when created by `defclass' or the label
308 created by the :initarg tag."
309 (declare (debug (form symbolp)))
310 `(eieio-oref ,obj (quote ,slot)))
312 (defalias 'slot-value 'eieio-oref)
313 (defalias 'set-slot-value 'eieio-oset)
315 (defmacro oref-default (obj slot)
316 "Get the default value of OBJ (maybe a class) for SLOT.
317 The default value is the value installed in a class with the :initform
318 tag. SLOT can be the slot name, or the tag specified by the :initarg
319 tag in the `defclass' call."
320 (declare (debug (form symbolp)))
321 `(eieio-oref-default ,obj (quote ,slot)))
323 ;;; Handy CLOS macros
325 (defmacro with-slots (spec-list object &rest body)
326 "Bind SPEC-LIST lexically to slot values in OBJECT, and execute BODY.
327 This establishes a lexical environment for referring to the slots in
328 the instance named by the given slot-names as though they were
329 variables. Within such a context the value of the slot can be
330 specified by using its slot name, as if it were a lexically bound
331 variable. Both setf and setq can be used to set the value of the
332 slot.
334 SPEC-LIST is of a form similar to `let'. For example:
336 ((VAR1 SLOT1)
337 SLOT2
338 SLOTN
339 (VARN+1 SLOTN+1))
341 Where each VAR is the local variable given to the associated
342 SLOT. A slot specified without a variable name is given a
343 variable name of the same name as the slot."
344 (declare (indent 2) (debug (sexp sexp def-body)))
345 (require 'cl-lib)
346 ;; Transform the spec-list into a cl-symbol-macrolet spec-list.
347 (let ((mappings (mapcar (lambda (entry)
348 (let ((var (if (listp entry) (car entry) entry))
349 (slot (if (listp entry) (cadr entry) entry)))
350 (list var `(slot-value ,object ',slot))))
351 spec-list)))
352 (append (list 'cl-symbol-macrolet mappings)
353 body)))
355 ;;; Simple generators, and query functions. None of these would do
356 ;; well embedded into an object.
358 (define-obsolete-function-alias
359 'object-class-fast #'eieio--object-class-name "24.4")
361 (cl-defgeneric eieio-object-name-string (obj)
362 "Return a string which is OBJ's name."
363 (declare (obsolete eieio-named "25.1")))
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 (cl-check-type obj eieio-object)
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 (cl-defmethod eieio-object-name-string (obj)
380 (or (gethash obj eieio--object-names)
381 (symbol-name (eieio-object-class obj))))
382 (define-obsolete-function-alias
383 'object-name-string #'eieio-object-name-string "24.4")
385 (cl-defmethod eieio-object-set-name-string (obj name)
386 "Set the string which is OBJ's NAME."
387 (declare (obsolete eieio-named "25.1"))
388 (cl-check-type name string)
389 (setf (gethash obj eieio--object-names) name))
390 (define-obsolete-function-alias
391 'object-set-name-string 'eieio-object-set-name-string "24.4")
393 (defun eieio-object-class (obj)
394 "Return the class struct defining OBJ."
395 ;; FIXME: We say we return a "struct" but we return a symbol instead!
396 (cl-check-type obj eieio-object)
397 (eieio--object-class-name obj))
398 (define-obsolete-function-alias 'object-class #'eieio-object-class "24.4")
399 ;; CLOS name, maybe?
400 (define-obsolete-function-alias 'class-of #'eieio-object-class "24.4")
402 (defun eieio-object-class-name (obj)
403 "Return a Lisp like symbol name for OBJ's class."
404 (cl-check-type obj eieio-object)
405 (eieio-class-name (eieio--object-class-name obj)))
406 (define-obsolete-function-alias
407 'object-class-name 'eieio-object-class-name "24.4")
409 (defun eieio-class-parents (class)
410 "Return parent classes to CLASS. (overload of variable).
412 The CLOS function `class-direct-superclasses' is aliased to this function."
413 (eieio--class-parent (eieio--class-object class)))
415 (define-obsolete-function-alias 'class-parents #'eieio-class-parents "24.4")
417 (defun eieio-class-children (class)
418 "Return child classes to CLASS.
419 The CLOS function `class-direct-subclasses' is aliased to this function."
420 (cl-check-type class class)
421 (eieio--class-children (eieio--class-v class)))
422 (define-obsolete-function-alias
423 'class-children #'eieio-class-children "24.4")
425 ;; Official CLOS functions.
426 (define-obsolete-function-alias
427 'class-direct-superclasses #'eieio-class-parents "24.4")
428 (define-obsolete-function-alias
429 'class-direct-subclasses #'eieio-class-children "24.4")
431 (defmacro eieio-class-parent (class)
432 "Return first parent class to CLASS. (overload of variable)."
433 `(car (eieio-class-parents ,class)))
434 (define-obsolete-function-alias 'class-parent 'eieio-class-parent "24.4")
436 (defun same-class-p (obj class)
437 "Return t if OBJ is of class-type CLASS."
438 (setq class (eieio--class-object class))
439 (cl-check-type class eieio--class)
440 (cl-check-type obj eieio-object)
441 (eq (eieio--object-class-object obj) class))
443 (defun object-of-class-p (obj class)
444 "Return non-nil if OBJ is an instance of CLASS or CLASS' subclasses."
445 (cl-check-type obj eieio-object)
446 ;; class will be checked one layer down
447 (child-of-class-p (eieio--object-class-object obj) class))
448 ;; Backwards compatibility
449 (defalias 'obj-of-class-p 'object-of-class-p)
451 (defun child-of-class-p (child class)
452 "Return non-nil if CHILD class is a subclass of CLASS."
453 (setq child (eieio--class-object child))
454 (cl-check-type child eieio--class)
455 ;; `eieio-default-superclass' is never mentioned in eieio--class-parent,
456 ;; so we have to special case it here.
457 (or (eq class 'eieio-default-superclass)
458 (let ((p nil))
459 (setq class (eieio--class-object class))
460 (cl-check-type class eieio--class)
461 (while (and child (not (eq child class)))
462 (setq p (append p (eieio--class-parent child))
463 child (pop p)))
464 (if child t))))
466 (defun object-slots (obj)
467 "Return list of slots available in OBJ."
468 (cl-check-type obj eieio-object)
469 (eieio--class-public-a (eieio--object-class-object obj)))
471 (defun eieio--class-slot-initarg (class slot) "Fetch from CLASS, SLOT's :initarg."
472 (cl-check-type class eieio--class)
473 (let ((ia (eieio--class-initarg-tuples class))
474 (f nil))
475 (while (and ia (not f))
476 (if (eq (cdr (car ia)) slot)
477 (setq f (car (car ia))))
478 (setq ia (cdr ia)))
481 ;;; Object Set macros
483 (defmacro oset (obj slot value)
484 "Set the value in OBJ for slot SLOT to VALUE.
485 SLOT is the slot name as specified in `defclass' or the tag created
486 with in the :initarg slot. VALUE can be any Lisp object."
487 (declare (debug (form symbolp form)))
488 `(eieio-oset ,obj (quote ,slot) ,value))
490 (defmacro oset-default (class slot value)
491 "Set the default slot in CLASS for SLOT to VALUE.
492 The default value is usually set with the :initform tag during class
493 creation. This allows users to change the default behavior of classes
494 after they are created."
495 (declare (debug (form symbolp form)))
496 `(eieio-oset-default ,class (quote ,slot) ,value))
498 ;;; CLOS queries into classes and slots
500 (defun slot-boundp (object slot)
501 "Return non-nil if OBJECT's SLOT is bound.
502 Setting a slot's value makes it bound. Calling `slot-makeunbound' will
503 make a slot unbound.
504 OBJECT can be an instance or a class."
505 ;; Skip typechecking while retrieving this value.
506 (let ((eieio-skip-typecheck t))
507 ;; Return nil if the magic symbol is in there.
508 (not (eq (cond
509 ((eieio-object-p object) (eieio-oref object slot))
510 ((symbolp object) (eieio-oref-default object slot))
511 (t (signal 'wrong-type-argument (list 'eieio-object-p object))))
512 eieio-unbound))))
514 (defun slot-makeunbound (object slot)
515 "In OBJECT, make SLOT unbound."
516 (eieio-oset object slot eieio-unbound))
518 (defun slot-exists-p (object-or-class slot)
519 "Return non-nil if OBJECT-OR-CLASS has SLOT."
520 (let ((cv (cond ((eieio-object-p object-or-class)
521 (eieio--object-class-object object-or-class))
522 ((eieio--class-p object-or-class) object-or-class)
523 (t (find-class object-or-class 'error)))))
524 (or (memq slot (eieio--class-public-a cv))
525 (memq slot (eieio--class-class-allocation-a cv)))
528 (defun find-class (symbol &optional errorp)
529 "Return the class that SYMBOL represents.
530 If there is no class, nil is returned if ERRORP is nil.
531 If ERRORP is non-nil, `wrong-argument-type' is signaled."
532 (let ((class (eieio--class-v symbol)))
533 (cond
534 ((eieio--class-p class) class)
535 (errorp (signal 'wrong-type-argument (list 'class-p symbol))))))
537 ;;; Slightly more complex utility functions for objects
539 (defun object-assoc (key slot list)
540 "Return an object if KEY is `equal' to SLOT's value of an object in LIST.
541 LIST is a list of objects whose slots are searched.
542 Objects in LIST do not need to have a slot named SLOT, nor does
543 SLOT need to be bound. If these errors occur, those objects will
544 be ignored."
545 (cl-check-type list list)
546 (while (and list (not (condition-case nil
547 ;; This prevents errors for missing slots.
548 (equal key (eieio-oref (car list) slot))
549 (error nil))))
550 (setq list (cdr list)))
551 (car list))
553 (defun object-assoc-list (slot list)
554 "Return an association list with the contents of SLOT as the key element.
555 LIST must be a list of objects with SLOT in it.
556 This is useful when you need to do completing read on an object group."
557 (cl-check-type list list)
558 (let ((assoclist nil))
559 (while list
560 (setq assoclist (cons (cons (eieio-oref (car list) slot)
561 (car list))
562 assoclist))
563 (setq list (cdr list)))
564 (nreverse assoclist)))
566 (defun object-assoc-list-safe (slot list)
567 "Return an association list with the contents of SLOT as the key element.
568 LIST must be a list of objects, but those objects do not need to have
569 SLOT in it. If it does not, then that element is left out of the association
570 list."
571 (cl-check-type list list)
572 (let ((assoclist nil))
573 (while list
574 (if (slot-exists-p (car list) slot)
575 (setq assoclist (cons (cons (eieio-oref (car list) slot)
576 (car list))
577 assoclist)))
578 (setq list (cdr list)))
579 (nreverse assoclist)))
581 (defun object-add-to-list (object slot item &optional append)
582 "In OBJECT's SLOT, add ITEM to the list of elements.
583 Optional argument APPEND indicates we need to append to the list.
584 If ITEM already exists in the list in SLOT, then it is not added.
585 Comparison is done with `equal' through the `member' function call.
586 If SLOT is unbound, bind it to the list containing ITEM."
587 (let (ov)
588 ;; Find the originating list.
589 (if (not (slot-boundp object slot))
590 (setq ov (list item))
591 (setq ov (eieio-oref object slot))
592 ;; turn it into a list.
593 (unless (listp ov)
594 (setq ov (list ov)))
595 ;; Do the combination
596 (if (not (member item ov))
597 (setq ov
598 (if append
599 (append ov (list item))
600 (cons item ov)))))
601 ;; Set back into the slot.
602 (eieio-oset object slot ov)))
604 (defun object-remove-from-list (object slot item)
605 "In OBJECT's SLOT, remove occurrences of ITEM.
606 Deletion is done with `delete', which deletes by side effect,
607 and comparisons are done with `equal'.
608 If SLOT is unbound, do nothing."
609 (if (not (slot-boundp object slot))
611 (eieio-oset object slot (delete item (eieio-oref object slot)))))
613 ;;; Here are some CLOS items that need the CL package
616 (gv-define-simple-setter eieio-oref eieio-oset)
620 ;; We want all objects created by EIEIO to have some default set of
621 ;; behaviors so we can create object utilities, and allow various
622 ;; types of error checking. To do this, create the default EIEIO
623 ;; class, and when no parent class is specified, use this as the
624 ;; default. (But don't store it in the other classes as the default,
625 ;; allowing for transparent support.)
628 (defclass eieio-default-superclass nil
630 "Default parent class for classes with no specified parent class.
631 Its slots are automatically adopted by classes with no specified parents.
632 This class is not stored in the `parent' slot of a class vector."
633 :abstract t)
635 (setq eieio-default-superclass (eieio--class-v 'eieio-default-superclass))
637 (defalias 'standard-class 'eieio-default-superclass)
639 (cl-defgeneric eieio-constructor (class &rest slots)
640 "Default constructor for CLASS `eieio-default-superclass'.")
642 (define-obsolete-function-alias 'constructor #'eieio-constructor "25.1")
644 (cl-defmethod eieio-constructor
645 ((class (subclass eieio-default-superclass)) &rest slots)
646 "Default constructor for CLASS `eieio-default-superclass'.
647 SLOTS are the initialization slots used by `shared-initialize'.
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 `shared-initialize' on that object."
651 (let* ((new-object (copy-sequence (eieio--class-default-object-cache
652 (eieio--class-v 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 (cl-defgeneric shared-initialize (obj slots)
666 "Set slots of OBJ with SLOTS which is a list of name/value pairs.
667 Called from the constructor routine.")
669 (cl-defmethod shared-initialize ((obj eieio-default-superclass) slots)
670 "Set slots of OBJ with SLOTS which is a list of name/value pairs.
671 Called from the constructor routine."
672 (while slots
673 (let ((rn (eieio--initarg-to-attribute (eieio--object-class-object obj)
674 (car slots))))
675 (if (not rn)
676 (slot-missing obj (car slots) 'oset (car (cdr slots)))
677 (eieio-oset obj rn (car (cdr slots)))))
678 (setq slots (cdr (cdr slots)))))
680 (cl-defgeneric initialize-instance (this &optional slots)
681 "Construct the new object THIS based on SLOTS.")
683 (cl-defmethod initialize-instance ((this eieio-default-superclass)
684 &optional slots)
685 "Construct the new object THIS based on SLOTS.
686 SLOTS is a tagged list where odd numbered elements are tags, and
687 even numbered elements are the values to store in the tagged slot.
688 If you overload the `initialize-instance', there you will need to
689 call `shared-initialize' yourself, or you can call `call-next-method'
690 to have this constructor called automatically. If these steps are
691 not taken, then new objects of your class will not have their values
692 dynamically set from SLOTS."
693 ;; First, see if any of our defaults are `lambda', and
694 ;; re-evaluate them and apply the value to our slots.
695 (let* ((this-class (eieio--object-class-object this))
696 (slot (eieio--class-public-a this-class))
697 (defaults (eieio--class-public-d this-class)))
698 (while slot
699 ;; For each slot, see if we need to evaluate it.
701 ;; Paul Landes said in an email:
702 ;; > CL evaluates it if it can, and otherwise, leaves it as
703 ;; > the quoted thing as you already have. This is by the
704 ;; > Sonya E. Keene book and other things I've look at on the
705 ;; > web.
706 (let ((dflt (eieio-default-eval-maybe (car defaults))))
707 (when (not (eq dflt (car defaults)))
708 (eieio-oset this (car slot) dflt) ))
709 ;; Next.
710 (setq slot (cdr slot)
711 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) (eieio-object-name object)
746 slot-name fn)))
748 (cl-defgeneric clone (obj &rest params)
749 "Make a copy of OBJ, and then supply PARAMS.
750 PARAMS is a parameter list of the same form used by `initialize-instance'.
752 When overloading `clone', be sure to call `call-next-method'
753 first and modify the returned object.")
755 (cl-defmethod clone ((obj eieio-default-superclass) &rest params)
756 "Make a copy of OBJ, and then apply PARAMS."
757 (let ((nobj (copy-sequence obj)))
758 (if (stringp (car params))
759 (funcall (if eieio-backward-compatibility #'ignore #'message)
760 "Obsolete name %S passed to clone" (pop params)))
761 (if params (shared-initialize nobj params))
762 nobj))
764 (cl-defgeneric destructor (this &rest params)
765 "Destructor for cleaning up any dynamic links to our object.")
767 (cl-defmethod destructor ((_this eieio-default-superclass) &rest _params)
768 "Destructor for cleaning up any dynamic links to our object.
769 Argument THIS is the object being destroyed. PARAMS are additional
770 ignored parameters."
771 ;; No cleanup... yet.
774 (cl-defgeneric object-print (this &rest strings)
775 "Pretty printer for object THIS. Call function `object-name' with STRINGS.
777 It is sometimes useful to put a summary of the object into the
778 default #<notation> string when using EIEIO browsing tools.
779 Implement this method to customize the summary.")
781 (cl-defmethod object-print ((this eieio-default-superclass) &rest strings)
782 "Pretty printer for object THIS. Call function `object-name' with STRINGS.
783 The default method for printing object THIS is to use the
784 function `object-name'.
786 It is sometimes useful to put a summary of the object into the
787 default #<notation> string when using EIEIO browsing tools.
789 Implement this function and specify STRINGS in a call to
790 `call-next-method' to provide additional summary information.
791 When passing in extra strings from child classes, always remember
792 to prepend a space."
793 (eieio-object-name this (apply #'concat strings)))
795 (defvar eieio-print-depth 0
796 "When printing, keep track of the current indentation depth.")
798 (cl-defgeneric object-write (this &optional comment)
799 "Write out object THIS to the current stream.
800 Optional COMMENT will add comments to the beginning of the output.")
802 (cl-defmethod object-write ((this eieio-default-superclass) &optional comment)
803 "Write object THIS out to the current stream.
804 This writes out the vector version of this object. Complex and recursive
805 object are discouraged from being written.
806 If optional COMMENT is non-nil, include comments when outputting
807 this object."
808 (when comment
809 (princ ";; Object ")
810 (princ (eieio-object-name-string this))
811 (princ "\n")
812 (princ comment)
813 (princ "\n"))
814 (let* ((cl (eieio-object-class this))
815 (cv (eieio--class-v cl)))
816 ;; Now output readable lisp to recreate this object
817 ;; It should look like this:
818 ;; (<constructor> <name> <slot> <slot> ... )
819 ;; Each slot's slot is writen using its :writer.
820 (princ (make-string (* eieio-print-depth 2) ? ))
821 (princ "(")
822 (princ (symbol-name (eieio--class-constructor (eieio-object-class this))))
823 (princ " ")
824 (prin1 (eieio-object-name-string this))
825 (princ "\n")
826 ;; Loop over all the public slots
827 (let ((publa (eieio--class-public-a cv))
828 (publd (eieio--class-public-d cv))
829 (publp (eieio--class-public-printer cv))
830 (eieio-print-depth (1+ eieio-print-depth)))
831 (while publa
832 (when (slot-boundp this (car publa))
833 (let ((i (eieio--class-slot-initarg cv (car publa)))
834 (v (eieio-oref this (car publa)))
836 (unless (or (not i) (equal v (car publd)))
837 (unless (bolp)
838 (princ "\n"))
839 (princ (make-string (* eieio-print-depth 2) ? ))
840 (princ (symbol-name i))
841 (if (car publp)
842 ;; Use our public printer
843 (progn
844 (princ " ")
845 (funcall (car publp) v))
846 ;; Use our generic override prin1 function.
847 (princ (if (or (eieio-object-p v)
848 (eieio-object-p (car-safe v)))
849 "\n" " "))
850 (eieio-override-prin1 v)))))
851 (setq publa (cdr publa) publd (cdr publd)
852 publp (cdr publp))))
853 (princ ")")
854 (when (= eieio-print-depth 0)
855 (princ "\n"))))
857 (defun eieio-override-prin1 (thing)
858 "Perform a `prin1' on THING taking advantage of object knowledge."
859 (cond ((eieio-object-p thing)
860 (object-write thing))
861 ((consp thing)
862 (eieio-list-prin1 thing))
863 ((eieio--class-p thing)
864 (princ (eieio-class-name thing)))
865 (t (prin1 thing))))
867 (defun eieio-list-prin1 (list)
868 "Display LIST where list may contain objects."
869 (if (not (eieio-object-p (car list)))
870 (progn
871 (princ "'")
872 (prin1 list))
873 (princ (make-string (* eieio-print-depth 2) ? ))
874 (princ "(list")
875 (let ((eieio-print-depth (1+ eieio-print-depth)))
876 (while list
877 (princ "\n")
878 (if (eieio-object-p (car list))
879 (object-write (car list))
880 (princ (make-string (* eieio-print-depth 2) ? ))
881 (eieio-override-prin1 (car list)))
882 (setq list (cdr list))))
883 (princ ")")))
886 ;;; Unimplemented functions from CLOS
888 (defun change-class (_obj _class)
889 "Change the class of OBJ to type CLASS.
890 This may create or delete slots, but does not affect the return value
891 of `eq'."
892 (error "EIEIO: `change-class' is unimplemented"))
894 ;; Hook ourselves into help system for describing classes and methods.
895 (add-hook 'help-fns-describe-function-functions 'eieio-help-constructor)
897 ;;; Interfacing with edebug
899 (defun eieio-edebug-prin1-to-string (print-function object &optional noescape)
900 "Display EIEIO OBJECT in fancy format.
902 Used as advice around `edebug-prin1-to-string', held in the
903 variable PRINT-FUNCTION. Optional argument NOESCAPE is passed to
904 `prin1-to-string' when appropriate."
905 (cond ((eieio--class-p object) (eieio-class-name object))
906 ((eieio-object-p object) (object-print object))
907 ((and (listp object) (or (eieio--class-p (car object))
908 (eieio-object-p (car object))))
909 (concat "(" (mapconcat
910 (lambda (x) (eieio-edebug-prin1-to-string print-function x))
911 object " ")
912 ")"))
913 (t (funcall print-function object noescape))))
915 (advice-add 'edebug-prin1-to-string
916 :around #'eieio-edebug-prin1-to-string)
919 ;;; Start of automatically extracted autoloads.
921 ;;;### (autoloads nil "eieio-custom" "eieio-custom.el" "2ec91e473fcad1ff20cd76edc4aab706")
922 ;;; Generated autoloads from eieio-custom.el
924 (autoload 'customize-object "eieio-custom" "\
925 Customize OBJ in a custom buffer.
926 Optional argument GROUP is the sub-group of slots to display.
928 \(fn OBJ &optional GROUP)" nil nil)
930 ;;;***
932 ;;;### (autoloads nil "eieio-opt" "eieio-opt.el" "ff1097f185bc2c253276a7d19fe2f54a")
933 ;;; Generated autoloads from eieio-opt.el
935 (autoload 'eieio-browse "eieio-opt" "\
936 Create an object browser window to show all objects.
937 If optional ROOT-CLASS, then start with that, otherwise start with
938 variable `eieio-default-superclass'.
940 \(fn &optional ROOT-CLASS)" t nil)
942 (autoload 'eieio-help-class "eieio-opt" "\
943 Print help description for CLASS.
944 If CLASS is actually an object, then also display current values of that object.
946 \(fn CLASS)" nil nil)
948 (autoload 'eieio-help-constructor "eieio-opt" "\
949 Describe CTR if it is a class constructor.
951 \(fn CTR)" nil nil)
953 ;;;***
955 ;;; End of automatically extracted autoloads.
957 (provide 'eieio)
959 ;;; eieio ends here