* lisp/emacs-lisp/eieio*.el: Use class objects in `parent' field.
[emacs.git] / lisp / emacs-lisp / eieio.el
blob878667106c81512a94d35f4e40a5013b34475328
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 ;; There is funny stuff going on with typep and deftype. This
40 ;; is the only way I seem to be able to make this stuff load properly.
42 ;; @TODO - fix :initform to be a form, not a quoted value
43 ;; @TODO - Prefix non-clos functions with `eieio-'.
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 superclass 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 SUPERCLASS is a list of superclasses to inherit from, with SLOTS
64 being the slots residing in that class definition. NOTE: Currently
65 only one slot may exist in SUPERCLASS as multiple inheritance is not
66 yet supported. Supported tags are:
68 :initform - Initializing form.
69 :initarg - Tag used during initialization.
70 :accessor - Tag used to create a function to access this slot.
71 :allocation - Specify where the value is stored.
72 Defaults to `:instance', but could also be `:class'.
73 :writer - A function symbol which will `write' an object's slot.
74 :reader - A function symbol which will `read' an object.
75 :type - The type of data allowed in this slot (see `typep').
76 :documentation
77 - A string documenting use of this slot.
79 The following are extensions on CLOS:
80 :protection - Specify protection for this slot.
81 Defaults to `:public'. Also use `:protected', or `:private'.
82 :custom - When customizing an object, the custom :type. Public only.
83 :label - A text string label used for a slot when customizing.
84 :group - Name of a customization group this slot belongs in.
85 :printer - A function to call to print the value of a slot.
86 See `eieio-override-prin1' as an example.
88 A class can also have optional options. These options happen in place
89 of documentation (including a :documentation tag), in addition to
90 documentation, or not at all. Supported options are:
92 :documentation - The doc-string used for this class.
94 Options added to EIEIO:
96 :allow-nil-initform - Non-nil to skip typechecking of null initforms.
97 :custom-groups - List of custom group names. Organizes slots into
98 reasonable groups for customizations.
99 :abstract - Non-nil to prevent instances of this class.
100 If a string, use as an error string if someone does
101 try to make an instance.
102 :method-invocation-order
103 - Control the method invocation order if there is
104 multiple inheritance. Valid values are:
105 :breadth-first - The default.
106 :depth-first
108 Options in CLOS not supported in EIEIO:
110 :metaclass - Class to use in place of `standard-class'
111 :default-initargs - Initargs to use when initializing new objects of
112 this class.
114 Due to the way class options are set up, you can add any tags you wish,
115 and reference them using the function `class-option'."
116 (declare (doc-string 4))
117 ;; This is eval-and-compile only to silence spurious compiler warnings
118 ;; about functions and variables not known to be defined.
119 ;; When eieio-defclass code is merged here and this becomes
120 ;; transparent to the compiler, the eval-and-compile can be removed.
121 `(eval-and-compile
122 (eieio-defclass ',name ',superclass ',slots ',options-and-doc)))
125 ;;; CLOS style implementation of object creators.
127 (defun make-instance (class &rest initargs)
128 "Make a new instance of CLASS based on INITARGS.
129 CLASS is a class symbol. For example:
131 (make-instance 'foo)
133 INITARGS is a property list with keywords based on the :initarg
134 for each slot. For example:
136 (make-instance 'foo :slot1 value1 :slotN valueN)
138 Compatibility note:
140 If the first element of INITARGS is a string, it is used as the
141 name of the class.
143 In EIEIO, the class' constructor requires a name for use when printing.
144 `make-instance' in CLOS doesn't use names the way Emacs does, so the
145 class is used as the name slot instead when INITARGS doesn't start with
146 a string."
147 (apply (class-constructor class) initargs))
150 ;;; CLOS methods and generics
152 (defmacro defgeneric (method _args &optional doc-string)
153 "Create a generic function METHOD.
154 DOC-STRING is the base documentation for this class. A generic
155 function has no body, as its purpose is to decide which method body
156 is appropriate to use. Uses `defmethod' to create methods, and calls
157 `defgeneric' for you. With this implementation the ARGS are
158 currently ignored. You can use `defgeneric' to apply specialized
159 top level documentation to a method."
160 (declare (doc-string 3))
161 `(eieio--defalias ',method
162 (eieio--defgeneric-init-form ',method ,doc-string)))
164 (defmacro defmethod (method &rest args)
165 "Create a new METHOD through `defgeneric' with ARGS.
167 The optional second argument KEY is a specifier that
168 modifies how the method is called, including:
169 :before - Method will be called before the :primary
170 :primary - The default if not specified
171 :after - Method will be called after the :primary
172 :static - First arg could be an object or class
173 The next argument is the ARGLIST. The ARGLIST specifies the arguments
174 to the method as with `defun'. The first argument can have a type
175 specifier, such as:
176 ((VARNAME CLASS) ARG2 ...)
177 where VARNAME is the name of the local variable for the method being
178 created. The CLASS is a class symbol for a class made with `defclass'.
179 A DOCSTRING comes after the ARGLIST, and is optional.
180 All the rest of the args are the BODY of the method. A method will
181 return the value of the last form in the BODY.
183 Summary:
185 (defmethod mymethod [:before | :primary | :after | :static]
186 ((typearg class-name) arg2 &optional opt &rest rest)
187 \"doc-string\"
188 body)"
189 (declare (doc-string 3)
190 (debug
191 (&define ; this means we are defining something
192 [&or name ("setf" :name setf name)]
193 ;; ^^ This is the methods symbol
194 [ &optional symbolp ] ; this is key :before etc
195 list ; arguments
196 [ &optional stringp ] ; documentation string
197 def-body ; part to be debugged
199 (let* ((key (if (keywordp (car args)) (pop args)))
200 (params (car args))
201 (arg1 (car params))
202 (fargs (if (consp arg1)
203 (cons (car arg1) (cdr params))
204 params))
205 (class (if (consp arg1) (nth 1 arg1)))
206 (code `(lambda ,fargs ,@(cdr args))))
207 `(progn
208 ;; Make sure there is a generic and the byte-compiler sees it.
209 (defgeneric ,method ,args
210 ,(or (documentation code)
211 (format "Generically created method `%s'." method)))
212 (eieio--defmethod ',method ',key ',class #',code))))
214 ;;; Get/Set slots in an object.
216 (defmacro oref (obj slot)
217 "Retrieve the value stored in OBJ in the slot named by SLOT.
218 Slot is the name of the slot when created by `defclass' or the label
219 created by the :initarg tag."
220 (declare (debug (form symbolp)))
221 `(eieio-oref ,obj (quote ,slot)))
223 (defalias 'slot-value 'eieio-oref)
224 (defalias 'set-slot-value 'eieio-oset)
226 (defmacro oref-default (obj slot)
227 "Get the default value of OBJ (maybe a class) for SLOT.
228 The default value is the value installed in a class with the :initform
229 tag. SLOT can be the slot name, or the tag specified by the :initarg
230 tag in the `defclass' call."
231 (declare (debug (form symbolp)))
232 `(eieio-oref-default ,obj (quote ,slot)))
234 ;;; Handy CLOS macros
236 (defmacro with-slots (spec-list object &rest body)
237 "Bind SPEC-LIST lexically to slot values in OBJECT, and execute BODY.
238 This establishes a lexical environment for referring to the slots in
239 the instance named by the given slot-names as though they were
240 variables. Within such a context the value of the slot can be
241 specified by using its slot name, as if it were a lexically bound
242 variable. Both setf and setq can be used to set the value of the
243 slot.
245 SPEC-LIST is of a form similar to `let'. For example:
247 ((VAR1 SLOT1)
248 SLOT2
249 SLOTN
250 (VARN+1 SLOTN+1))
252 Where each VAR is the local variable given to the associated
253 SLOT. A slot specified without a variable name is given a
254 variable name of the same name as the slot."
255 (declare (indent 2) (debug (sexp sexp def-body)))
256 (require 'cl-lib)
257 ;; Transform the spec-list into a cl-symbol-macrolet spec-list.
258 (let ((mappings (mapcar (lambda (entry)
259 (let ((var (if (listp entry) (car entry) entry))
260 (slot (if (listp entry) (cadr entry) entry)))
261 (list var `(slot-value ,object ',slot))))
262 spec-list)))
263 (append (list 'cl-symbol-macrolet mappings)
264 body)))
266 ;;; Simple generators, and query functions. None of these would do
267 ;; well embedded into an object.
269 (define-obsolete-function-alias
270 'object-class-fast #'eieio--object-class-name "24.4")
272 (defun eieio-object-name (obj &optional extra)
273 "Return a Lisp like symbol string for object OBJ.
274 If EXTRA, include that in the string returned to represent the symbol."
275 (eieio--check-type eieio-object-p obj)
276 (format "#<%s %s%s>" (eieio--object-class-name obj)
277 (eieio-object-name-string obj) (or extra "")))
278 (define-obsolete-function-alias 'object-name #'eieio-object-name "24.4")
280 (defconst eieio--object-names (make-hash-table :test #'eq :weakness 'key))
282 ;; In the past, every EIEIO object had a `name' field, so we had the two method
283 ;; below "for free". Since this field is very rarely used, we got rid of it
284 ;; and instead we keep it in a weak hash-tables, for those very rare objects
285 ;; that use it.
286 (defmethod eieio-object-name-string (obj)
287 "Return a string which is OBJ's name."
288 (declare (obsolete eieio-named "25.1"))
289 (or (gethash obj eieio--object-names)
290 (symbol-name (eieio-object-class obj))))
291 (define-obsolete-function-alias
292 'object-name-string #'eieio-object-name-string "24.4")
294 (defmethod eieio-object-set-name-string (obj name)
295 "Set the string which is OBJ's NAME."
296 (declare (obsolete eieio-named "25.1"))
297 (eieio--check-type stringp name)
298 (setf (gethash obj eieio--object-names) name))
299 (define-obsolete-function-alias
300 'object-set-name-string 'eieio-object-set-name-string "24.4")
302 (defun eieio-object-class (obj)
303 "Return the class struct defining OBJ."
304 ;; FIXME: We say we return a "struct" but we return a symbol instead!
305 (eieio--check-type eieio-object-p obj)
306 (eieio--object-class-name obj))
307 (define-obsolete-function-alias 'object-class #'eieio-object-class "24.4")
308 ;; CLOS name, maybe?
309 (define-obsolete-function-alias 'class-of #'eieio-object-class "24.4")
311 (defun eieio-object-class-name (obj)
312 "Return a Lisp like symbol name for OBJ's class."
313 (eieio--check-type eieio-object-p obj)
314 (eieio-class-name (eieio--object-class-name obj)))
315 (define-obsolete-function-alias
316 'object-class-name 'eieio-object-class-name "24.4")
318 (defun eieio-class-parents (class)
319 "Return parent classes to CLASS. (overload of variable).
321 The CLOS function `class-direct-superclasses' is aliased to this function."
322 (let ((c (eieio-class-object class)))
323 (eieio--class-parent c)))
325 (define-obsolete-function-alias 'class-parents #'eieio-class-parents "24.4")
327 (defun eieio-class-children (class)
328 "Return child classes to CLASS.
329 The CLOS function `class-direct-subclasses' is aliased to this function."
330 (eieio--check-type class-p class)
331 (eieio-class-children-fast class))
332 (define-obsolete-function-alias
333 'class-children #'eieio-class-children "24.4")
335 ;; Official CLOS functions.
336 (define-obsolete-function-alias
337 'class-direct-superclasses #'eieio-class-parents "24.4")
338 (define-obsolete-function-alias
339 'class-direct-subclasses #'eieio-class-children "24.4")
341 (defmacro eieio-class-parent (class)
342 "Return first parent class to CLASS. (overload of variable)."
343 `(car (eieio-class-parents ,class)))
344 (define-obsolete-function-alias 'class-parent 'eieio-class-parent "24.4")
346 (defun same-class-p (obj class) "Return t if OBJ is of class-type CLASS."
347 (eieio--check-type class-p class)
348 (eieio--check-type eieio-object-p obj)
349 (same-class-fast-p obj class))
351 (defun object-of-class-p (obj class)
352 "Return non-nil if OBJ is an instance of CLASS or CLASS' subclasses."
353 (eieio--check-type eieio-object-p obj)
354 ;; class will be checked one layer down
355 (child-of-class-p (eieio--object-class-object obj) class))
356 ;; Backwards compatibility
357 (defalias 'obj-of-class-p 'object-of-class-p)
359 (defun child-of-class-p (child class)
360 "Return non-nil if CHILD class is a subclass of CLASS."
361 (setq child (eieio--class-object child))
362 (eieio--check-type eieio--class-p child)
363 ;; `eieio-default-superclass' is never mentioned in eieio--class-parent,
364 ;; so we have to special case it here.
365 (or (eq class 'eieio-default-superclass)
366 (let ((p nil))
367 (setq class (eieio--class-object class))
368 (eieio--check-type eieio--class-p class)
369 (while (and child (not (eq child class)))
370 (setq p (append p (eieio--class-parent child))
371 child (pop p)))
372 (if child t))))
374 (defun object-slots (obj)
375 "Return list of slots available in OBJ."
376 (eieio--check-type eieio-object-p obj)
377 (eieio--class-public-a (eieio--object-class-object obj)))
379 (defun eieio--class-slot-initarg (class slot) "Fetch from CLASS, SLOT's :initarg."
380 (eieio--check-type eieio--class-p class)
381 (let ((ia (eieio--class-initarg-tuples class))
382 (f nil))
383 (while (and ia (not f))
384 (if (eq (cdr (car ia)) slot)
385 (setq f (car (car ia))))
386 (setq ia (cdr ia)))
389 ;;; Object Set macros
391 (defmacro oset (obj slot value)
392 "Set the value in OBJ for slot SLOT to VALUE.
393 SLOT is the slot name as specified in `defclass' or the tag created
394 with in the :initarg slot. VALUE can be any Lisp object."
395 (declare (debug (form symbolp form)))
396 `(eieio-oset ,obj (quote ,slot) ,value))
398 (defmacro oset-default (class slot value)
399 "Set the default slot in CLASS for SLOT to VALUE.
400 The default value is usually set with the :initform tag during class
401 creation. This allows users to change the default behavior of classes
402 after they are created."
403 (declare (debug (form symbolp form)))
404 `(eieio-oset-default ,class (quote ,slot) ,value))
406 ;;; CLOS queries into classes and slots
408 (defun slot-boundp (object slot)
409 "Return non-nil if OBJECT's SLOT is bound.
410 Setting a slot's value makes it bound. Calling `slot-makeunbound' will
411 make a slot unbound.
412 OBJECT can be an instance or a class."
413 ;; Skip typechecking while retrieving this value.
414 (let ((eieio-skip-typecheck t))
415 ;; Return nil if the magic symbol is in there.
416 (not (eq (cond
417 ((eieio-object-p object) (eieio-oref object slot))
418 ((class-p object) (eieio-oref-default object slot))
419 (t (signal 'wrong-type-argument (list 'eieio-object-p object))))
420 eieio-unbound))))
422 (defun slot-makeunbound (object slot)
423 "In OBJECT, make SLOT unbound."
424 (eieio-oset object slot eieio-unbound))
426 (defun slot-exists-p (object-or-class slot)
427 "Return non-nil if OBJECT-OR-CLASS has SLOT."
428 (let ((cv (cond ((eieio-object-p object-or-class)
429 (eieio--object-class-object object-or-class))
430 (t (eieio-class-object object-or-class)))))
431 (or (memq slot (eieio--class-public-a cv))
432 (memq slot (eieio--class-class-allocation-a cv)))
435 (defun find-class (symbol &optional errorp)
436 "Return the class that SYMBOL represents.
437 If there is no class, nil is returned if ERRORP is nil.
438 If ERRORP is non-nil, `wrong-argument-type' is signaled."
439 (if (not (class-p symbol))
440 (if errorp (signal 'wrong-type-argument (list 'class-p symbol))
441 nil)
442 (eieio--class-v symbol)))
444 ;;; Slightly more complex utility functions for objects
446 (defun object-assoc (key slot list)
447 "Return an object if KEY is `equal' to SLOT's value of an object in LIST.
448 LIST is a list of objects whose slots are searched.
449 Objects in LIST do not need to have a slot named SLOT, nor does
450 SLOT need to be bound. If these errors occur, those objects will
451 be ignored."
452 (eieio--check-type listp list)
453 (while (and list (not (condition-case nil
454 ;; This prevents errors for missing slots.
455 (equal key (eieio-oref (car list) slot))
456 (error nil))))
457 (setq list (cdr list)))
458 (car list))
460 (defun object-assoc-list (slot list)
461 "Return an association list with the contents of SLOT as the key element.
462 LIST must be a list of objects with SLOT in it.
463 This is useful when you need to do completing read on an object group."
464 (eieio--check-type listp list)
465 (let ((assoclist nil))
466 (while list
467 (setq assoclist (cons (cons (eieio-oref (car list) slot)
468 (car list))
469 assoclist))
470 (setq list (cdr list)))
471 (nreverse assoclist)))
473 (defun object-assoc-list-safe (slot list)
474 "Return an association list with the contents of SLOT as the key element.
475 LIST must be a list of objects, but those objects do not need to have
476 SLOT in it. If it does not, then that element is left out of the association
477 list."
478 (eieio--check-type listp list)
479 (let ((assoclist nil))
480 (while list
481 (if (slot-exists-p (car list) slot)
482 (setq assoclist (cons (cons (eieio-oref (car list) slot)
483 (car list))
484 assoclist)))
485 (setq list (cdr list)))
486 (nreverse assoclist)))
488 (defun object-add-to-list (object slot item &optional append)
489 "In OBJECT's SLOT, add ITEM to the list of elements.
490 Optional argument APPEND indicates we need to append to the list.
491 If ITEM already exists in the list in SLOT, then it is not added.
492 Comparison is done with `equal' through the `member' function call.
493 If SLOT is unbound, bind it to the list containing ITEM."
494 (let (ov)
495 ;; Find the originating list.
496 (if (not (slot-boundp object slot))
497 (setq ov (list item))
498 (setq ov (eieio-oref object slot))
499 ;; turn it into a list.
500 (unless (listp ov)
501 (setq ov (list ov)))
502 ;; Do the combination
503 (if (not (member item ov))
504 (setq ov
505 (if append
506 (append ov (list item))
507 (cons item ov)))))
508 ;; Set back into the slot.
509 (eieio-oset object slot ov)))
511 (defun object-remove-from-list (object slot item)
512 "In OBJECT's SLOT, remove occurrences of ITEM.
513 Deletion is done with `delete', which deletes by side effect,
514 and comparisons are done with `equal'.
515 If SLOT is unbound, do nothing."
516 (if (not (slot-boundp object slot))
518 (eieio-oset object slot (delete item (eieio-oref object slot)))))
521 ;; Method Calling Functions
523 (defun next-method-p ()
524 "Return non-nil if there is a next method.
525 Returns a list of lambda expressions which is the `next-method'
526 order."
527 eieio-generic-call-next-method-list)
529 (defun call-next-method (&rest replacement-args)
530 "Call the superclass method from a subclass method.
531 The superclass method is specified in the current method list,
532 and is called the next method.
534 If REPLACEMENT-ARGS is non-nil, then use them instead of
535 `eieio-generic-call-arglst'. The generic arg list are the
536 arguments passed in at the top level.
538 Use `next-method-p' to find out if there is a next method to call."
539 (if (not (eieio--scoped-class))
540 (error "`call-next-method' not called within a class specific method"))
541 (if (and (/= eieio-generic-call-key eieio--method-primary)
542 (/= eieio-generic-call-key eieio--method-static))
543 (error "Cannot `call-next-method' except in :primary or :static methods")
545 (let ((newargs (or replacement-args eieio-generic-call-arglst))
546 (next (car eieio-generic-call-next-method-list))
548 (if (not (and next (car next)))
549 (apply #'no-next-method (car newargs) (cdr newargs))
550 (let* ((eieio-generic-call-next-method-list
551 (cdr eieio-generic-call-next-method-list))
552 (eieio-generic-call-arglst newargs)
553 (fcn (car next))
555 (eieio--with-scoped-class (cdr next)
556 (apply fcn newargs)) ))))
558 ;;; Here are some CLOS items that need the CL package
561 (gv-define-simple-setter eieio-oref eieio-oset)
565 ;; We want all objects created by EIEIO to have some default set of
566 ;; behaviors so we can create object utilities, and allow various
567 ;; types of error checking. To do this, create the default EIEIO
568 ;; class, and when no parent class is specified, use this as the
569 ;; default. (But don't store it in the other classes as the default,
570 ;; allowing for transparent support.)
573 (defclass eieio-default-superclass nil
575 "Default parent class for classes with no specified parent class.
576 Its slots are automatically adopted by classes with no specified parents.
577 This class is not stored in the `parent' slot of a class vector."
578 :abstract t)
580 (setq eieio-default-superclass (eieio--class-v 'eieio-default-superclass))
582 (defalias 'standard-class 'eieio-default-superclass)
584 (defgeneric eieio-constructor (class &rest slots)
585 "Default constructor for CLASS `eieio-default-superclass'.")
587 (define-obsolete-function-alias 'constructor #'eieio-constructor "25.1")
589 (defmethod eieio-constructor :static
590 ((class eieio-default-superclass) &rest slots)
591 "Default constructor for CLASS `eieio-default-superclass'.
592 SLOTS are the initialization slots used by `shared-initialize'.
593 This static method is called when an object is constructed.
594 It allocates the vector used to represent an EIEIO object, and then
595 calls `shared-initialize' on that object."
596 (let* ((new-object (copy-sequence (eieio--class-default-object-cache (eieio--class-v class)))))
597 ;; Call the initialize method on the new object with the slots
598 ;; that were passed down to us.
599 (initialize-instance new-object slots)
600 ;; Return the created object.
601 new-object))
603 (defgeneric shared-initialize (obj slots)
604 "Set slots of OBJ with SLOTS which is a list of name/value pairs.
605 Called from the constructor routine.")
607 (defmethod shared-initialize ((obj eieio-default-superclass) slots)
608 "Set slots of OBJ with SLOTS which is a list of name/value pairs.
609 Called from the constructor routine."
610 (eieio--with-scoped-class (eieio--object-class-object obj)
611 (while slots
612 (let ((rn (eieio--initarg-to-attribute (eieio--object-class-object obj)
613 (car slots))))
614 (if (not rn)
615 (slot-missing obj (car slots) 'oset (car (cdr slots)))
616 (eieio-oset obj rn (car (cdr slots)))))
617 (setq slots (cdr (cdr slots))))))
619 (defgeneric initialize-instance (this &optional slots)
620 "Construct the new object THIS based on SLOTS.")
622 (defmethod initialize-instance ((this eieio-default-superclass)
623 &optional slots)
624 "Construct the new object THIS based on SLOTS.
625 SLOTS is a tagged list where odd numbered elements are tags, and
626 even numbered elements are the values to store in the tagged slot.
627 If you overload the `initialize-instance', there you will need to
628 call `shared-initialize' yourself, or you can call `call-next-method'
629 to have this constructor called automatically. If these steps are
630 not taken, then new objects of your class will not have their values
631 dynamically set from SLOTS."
632 ;; First, see if any of our defaults are `lambda', and
633 ;; re-evaluate them and apply the value to our slots.
634 (let* ((this-class (eieio--object-class-object this))
635 (slot (eieio--class-public-a this-class))
636 (defaults (eieio--class-public-d this-class)))
637 (while slot
638 ;; For each slot, see if we need to evaluate it.
640 ;; Paul Landes said in an email:
641 ;; > CL evaluates it if it can, and otherwise, leaves it as
642 ;; > the quoted thing as you already have. This is by the
643 ;; > Sonya E. Keene book and other things I've look at on the
644 ;; > web.
645 (let ((dflt (eieio-default-eval-maybe (car defaults))))
646 (when (not (eq dflt (car defaults)))
647 (eieio-oset this (car slot) dflt) ))
648 ;; Next.
649 (setq slot (cdr slot)
650 defaults (cdr defaults))))
651 ;; Shared initialize will parse our slots for us.
652 (shared-initialize this slots))
654 (defgeneric slot-missing (object slot-name operation &optional new-value)
655 "Method invoked when an attempt to access a slot in OBJECT fails.")
657 (defmethod slot-missing ((object eieio-default-superclass) slot-name
658 _operation &optional _new-value)
659 "Method invoked when an attempt to access a slot in OBJECT fails.
660 SLOT-NAME is the name of the failed slot, OPERATION is the type of access
661 that was requested, and optional NEW-VALUE is the value that was desired
662 to be set.
664 This method is called from `oref', `oset', and other functions which
665 directly reference slots in EIEIO objects."
666 (signal 'invalid-slot-name (list (eieio-object-name object)
667 slot-name)))
669 (defgeneric slot-unbound (object class slot-name fn)
670 "Slot unbound is invoked during an attempt to reference an unbound slot.")
672 (defmethod slot-unbound ((object eieio-default-superclass)
673 class slot-name fn)
674 "Slot unbound is invoked during an attempt to reference an unbound slot.
675 OBJECT is the instance of the object being reference. CLASS is the
676 class of OBJECT, and SLOT-NAME is the offending slot. This function
677 throws the signal `unbound-slot'. You can overload this function and
678 return the value to use in place of the unbound value.
679 Argument FN is the function signaling this error.
680 Use `slot-boundp' to determine if a slot is bound or not.
682 In CLOS, the argument list is (CLASS OBJECT SLOT-NAME), but
683 EIEIO can only dispatch on the first argument, so the first two are swapped."
684 (signal 'unbound-slot (list (eieio-class-name class) (eieio-object-name object)
685 slot-name fn)))
687 (defgeneric no-applicable-method (object method &rest args)
688 "Called if there are no implementations for OBJECT in METHOD.")
690 (defmethod no-applicable-method ((object eieio-default-superclass)
691 method &rest _args)
692 "Called if there are no implementations for OBJECT in METHOD.
693 OBJECT is the object which has no method implementation.
694 ARGS are the arguments that were passed to METHOD.
696 Implement this for a class to block this signal. The return
697 value becomes the return value of the original method call."
698 (signal 'no-method-definition (list method (eieio-object-name object)))
701 (defgeneric no-next-method (object &rest args)
702 "Called from `call-next-method' when no additional methods are available.")
704 (defmethod no-next-method ((object eieio-default-superclass)
705 &rest args)
706 "Called from `call-next-method' when no additional methods are available.
707 OBJECT is othe object being called on `call-next-method'.
708 ARGS are the arguments it is called by.
709 This method signals `no-next-method' by default. Override this
710 method to not throw an error, and its return value becomes the
711 return value of `call-next-method'."
712 (signal 'no-next-method (list (eieio-object-name object) args))
715 (defgeneric clone (obj &rest params)
716 "Make a copy of OBJ, and then supply PARAMS.
717 PARAMS is a parameter list of the same form used by `initialize-instance'.
719 When overloading `clone', be sure to call `call-next-method'
720 first and modify the returned object.")
722 (defmethod clone ((obj eieio-default-superclass) &rest params)
723 "Make a copy of OBJ, and then apply PARAMS."
724 (let ((nobj (copy-sequence obj)))
725 (if (stringp (car params))
726 (message "Obsolete name %S passed to clone" (pop params)))
727 (if params (shared-initialize nobj params))
728 nobj))
730 (defgeneric destructor (this &rest params)
731 "Destructor for cleaning up any dynamic links to our object.")
733 (defmethod destructor ((_this eieio-default-superclass) &rest _params)
734 "Destructor for cleaning up any dynamic links to our object.
735 Argument THIS is the object being destroyed. PARAMS are additional
736 ignored parameters."
737 ;; No cleanup... yet.
740 (defgeneric object-print (this &rest strings)
741 "Pretty printer for object THIS. Call function `object-name' with STRINGS.
743 It is sometimes useful to put a summary of the object into the
744 default #<notation> string when using EIEIO browsing tools.
745 Implement this method to customize the summary.")
747 (defmethod object-print ((this eieio-default-superclass) &rest strings)
748 "Pretty printer for object THIS. Call function `object-name' with STRINGS.
749 The default method for printing object THIS is to use the
750 function `object-name'.
752 It is sometimes useful to put a summary of the object into the
753 default #<notation> string when using EIEIO browsing tools.
755 Implement this function and specify STRINGS in a call to
756 `call-next-method' to provide additional summary information.
757 When passing in extra strings from child classes, always remember
758 to prepend a space."
759 (eieio-object-name this (apply #'concat strings)))
761 (defvar eieio-print-depth 0
762 "When printing, keep track of the current indentation depth.")
764 (defgeneric object-write (this &optional comment)
765 "Write out object THIS to the current stream.
766 Optional COMMENT will add comments to the beginning of the output.")
768 (defmethod object-write ((this eieio-default-superclass) &optional comment)
769 "Write object THIS out to the current stream.
770 This writes out the vector version of this object. Complex and recursive
771 object are discouraged from being written.
772 If optional COMMENT is non-nil, include comments when outputting
773 this object."
774 (when comment
775 (princ ";; Object ")
776 (princ (eieio-object-name-string this))
777 (princ "\n")
778 (princ comment)
779 (princ "\n"))
780 (let* ((cl (eieio-object-class this))
781 (cv (eieio--class-v cl)))
782 ;; Now output readable lisp to recreate this object
783 ;; It should look like this:
784 ;; (<constructor> <name> <slot> <slot> ... )
785 ;; Each slot's slot is writen using its :writer.
786 (princ (make-string (* eieio-print-depth 2) ? ))
787 (princ "(")
788 (princ (symbol-name (class-constructor (eieio-object-class this))))
789 (princ " ")
790 (prin1 (eieio-object-name-string this))
791 (princ "\n")
792 ;; Loop over all the public slots
793 (let ((publa (eieio--class-public-a cv))
794 (publd (eieio--class-public-d cv))
795 (publp (eieio--class-public-printer cv))
796 (eieio-print-depth (1+ eieio-print-depth)))
797 (while publa
798 (when (slot-boundp this (car publa))
799 (let ((i (eieio--class-slot-initarg cv (car publa)))
800 (v (eieio-oref this (car publa)))
802 (unless (or (not i) (equal v (car publd)))
803 (unless (bolp)
804 (princ "\n"))
805 (princ (make-string (* eieio-print-depth 2) ? ))
806 (princ (symbol-name i))
807 (if (car publp)
808 ;; Use our public printer
809 (progn
810 (princ " ")
811 (funcall (car publp) v))
812 ;; Use our generic override prin1 function.
813 (princ (if (or (eieio-object-p v)
814 (eieio-object-p (car-safe v)))
815 "\n" " "))
816 (eieio-override-prin1 v)))))
817 (setq publa (cdr publa) publd (cdr publd)
818 publp (cdr publp))))
819 (princ ")")
820 (when (= eieio-print-depth 0)
821 (princ "\n"))))
823 (defun eieio-override-prin1 (thing)
824 "Perform a `prin1' on THING taking advantage of object knowledge."
825 (cond ((eieio-object-p thing)
826 (object-write thing))
827 ((consp thing)
828 (eieio-list-prin1 thing))
829 ((class-p thing)
830 (princ (eieio-class-name thing)))
831 ((or (keywordp thing) (booleanp thing))
832 (prin1 thing))
833 ((symbolp thing)
834 (princ (concat "'" (symbol-name thing))))
835 (t (prin1 thing))))
837 (defun eieio-list-prin1 (list)
838 "Display LIST where list may contain objects."
839 (if (not (eieio-object-p (car list)))
840 (progn
841 (princ "'")
842 (prin1 list))
843 (princ (make-string (* eieio-print-depth 2) ? ))
844 (princ "(list")
845 (let ((eieio-print-depth (1+ eieio-print-depth)))
846 (while list
847 (princ "\n")
848 (if (eieio-object-p (car list))
849 (object-write (car list))
850 (princ (make-string (* eieio-print-depth 2) ? ))
851 (eieio-override-prin1 (car list)))
852 (setq list (cdr list))))
853 (princ ")")))
856 ;;; Unimplemented functions from CLOS
858 (defun change-class (_obj _class)
859 "Change the class of OBJ to type CLASS.
860 This may create or delete slots, but does not affect the return value
861 of `eq'."
862 (error "EIEIO: `change-class' is unimplemented"))
864 ;; Hook ourselves into help system for describing classes and methods.
865 (add-hook 'help-fns-describe-function-functions 'eieio-help-generic)
866 (add-hook 'help-fns-describe-function-functions 'eieio-help-constructor)
868 ;;; Interfacing with edebug
870 (defun eieio-edebug-prin1-to-string (print-function object &optional noescape)
871 "Display EIEIO OBJECT in fancy format.
873 Used as advice around `edebug-prin1-to-string', held in the
874 variable PRINT-FUNCTION. Optional argument NOESCAPE is passed to
875 `prin1-to-string' when appropriate."
876 (cond ((eieio--class-p object) (eieio-class-name object))
877 ((eieio-object-p object) (object-print object))
878 ((and (listp object) (or (eieio--class-p (car object))
879 (eieio-object-p (car object))))
880 (concat "(" (mapconcat
881 (lambda (x) (eieio-edebug-prin1-to-string print-function x))
882 object " ")
883 ")"))
884 (t (funcall print-function object noescape))))
886 (advice-add 'edebug-prin1-to-string
887 :around #'eieio-edebug-prin1-to-string)
890 ;;; Start of automatically extracted autoloads.
892 ;;;### (autoloads nil "eieio-custom" "eieio-custom.el" "a3f314e2a27e52444df4597c6ae51458")
893 ;;; Generated autoloads from eieio-custom.el
895 (autoload 'customize-object "eieio-custom" "\
896 Customize OBJ in a custom buffer.
897 Optional argument GROUP is the sub-group of slots to display.
899 \(fn OBJ &optional GROUP)" nil nil)
901 ;;;***
903 ;;;### (autoloads nil "eieio-opt" "eieio-opt.el" "2ff7d98da3f84c6af5c873ffb781930e")
904 ;;; Generated autoloads from eieio-opt.el
906 (autoload 'eieio-browse "eieio-opt" "\
907 Create an object browser window to show all objects.
908 If optional ROOT-CLASS, then start with that, otherwise start with
909 variable `eieio-default-superclass'.
911 \(fn &optional ROOT-CLASS)" t nil)
913 (autoload 'eieio-help-class "eieio-opt" "\
914 Print help description for CLASS.
915 If CLASS is actually an object, then also display current values of that object.
917 \(fn CLASS)" nil nil)
919 (autoload 'eieio-help-constructor "eieio-opt" "\
920 Describe CTR if it is a class constructor.
922 \(fn CTR)" nil nil)
924 (autoload 'eieio-help-generic "eieio-opt" "\
925 Describe GENERIC if it is a generic function.
927 \(fn GENERIC)" nil nil)
929 ;;;***
931 ;;; End of automatically extracted autoloads.
933 (provide 'eieio)
935 ;;; eieio ends here