.gitignore tweaks
[emacs.git] / lisp / emacs-lisp / eieio.el
Commit [+]AuthorDateLineData
94250173 Stefan Monnier2014-10-17 01:09:24 -04001;;; eieio.el --- Enhanced Implementation of Emacs Interpreted Objects -*- lexical-binding:t -*-
f6b1b0a8 Paul Eggert2011-11-12 23:48:23 -08002;;; or maybe Eric's Implementation of Emacs Interpreted Objects
6dd12ef2 Chong Yidong2009-09-28 00:49:54 +00003
7e09ef09 Paul Eggert2015-01-01 14:26:41 -08004;; Copyright (C) 1995-1996, 1998-2015 Free Software Foundation, Inc.
6dd12ef2 Chong Yidong2009-09-28 00:49:54 +00005
9ffe3f52 Glenn Morris2009-10-01 02:50:00 +00006;; Author: Eric M. Ludlam <zappo@gnu.org>
be798504 David Engster2013-01-31 20:58:56 +01007;; Version: 1.4
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +00008;; Keywords: OO, lisp
9
10;; This file is part of GNU Emacs.
11
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.
16
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.
21
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/>.
24
25;;; Commentary:
26;;
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.
31;;
32;; See eieio.texi for complete documentation on using this package.
a2930e43
EL
Eric M. Ludlam2010-09-19 00:23:57 -040033;;
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
6dd12ef2 Chong Yidong2009-09-28 00:49:54 +000038
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +000039;; @TODO - fix :initform to be a form, not a quoted value
40;; @TODO - Prefix non-clos functions with `eieio-'.
41
909126de
SM
Stefan Monnier2015-01-17 22:50:50 -050042;; TODO: better integrate CL's defstructs and classes. E.g. make it possible
43;; to create a new class that inherits from a struct.
44
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +000045;;; Code:
46
be798504 David Engster2013-01-31 20:58:56 +010047(defvar eieio-version "1.4"
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +000048 "Current version of EIEIO.")
49
50(defun eieio-version ()
51 "Display the current version of EIEIO."
52 (interactive)
53 (message eieio-version))
54
890f7890 David Engster2013-06-02 15:33:09 +020055(require 'eieio-core)
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +000056
57\f
58;;; Defining a new class
59;;
6a67b20d Stefan Monnier2015-01-08 15:47:32 -050060(defmacro defclass (name superclasses slots &rest options-and-doc)
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +000061 "Define NAME as a new class derived from SUPERCLASS with SLOTS.
62OPTIONS-AND-DOC is used as the class' options and base documentation.
6a67b20d
SM
Stefan Monnier2015-01-08 15:47:32 -050063SUPERCLASSES is a list of superclasses to inherit from, with SLOTS
64being the slots residing in that class definition. Supported tags are:
6dd12ef2 Chong Yidong2009-09-28 00:49:54 +000065
a8f316ca
JB
Juanma Barranquero2009-10-05 15:32:08 +000066 :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').
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +000074 :documentation
75 - A string documenting use of this slot.
76
77The following are extensions on CLOS:
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +000078 :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.
83
84A class can also have optional options. These options happen in place
a8f316ca Juanma Barranquero2009-10-05 15:32:08 +000085of documentation (including a :documentation tag), in addition to
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +000086documentation, or not at all. Supported options are:
87
88 :documentation - The doc-string used for this class.
89
90Options added to EIEIO:
91
a8f316ca Juanma Barranquero2009-10-05 15:32:08 +000092 :allow-nil-initform - Non-nil to skip typechecking of null initforms.
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +000093 :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
9ffe3f52 Glenn Morris2009-10-01 02:50:00 +000099 - Control the method invocation order if there is
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +0000100 multiple inheritance. Valid values are:
101 :breadth-first - The default.
102 :depth-first
103
104Options in CLOS not supported in EIEIO:
105
106 :metaclass - Class to use in place of `standard-class'
107 :default-initargs - Initargs to use when initializing new objects of
108 this class.
109
a8f316ca
JB
Juanma Barranquero2009-10-05 15:32:08 +0000110Due to the way class options are set up, you can add any tags you wish,
111and reference them using the function `class-option'."
94250173 Stefan Monnier2014-10-17 01:09:24 -0400112 (declare (doc-string 4))
e0be229d Stefan Monnier2015-01-31 00:48:14 -0500113 (cl-check-type superclasses list)
6a67b20d
SM
Stefan Monnier2015-01-08 15:47:32 -0500114
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'")))
121
122 (if (stringp (car options-and-doc))
123 (setq options-and-doc
124 (cons :documentation options-and-doc)))
125
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)))
131
132 (let ((testsym1 (intern (concat (symbol-name name) "-p")))
8fd527eb Stefan Monnier2015-03-26 13:32:45 -0400133 (testsym2 (intern (format "%s--eieio-childp" name)))
6a67b20d
SM
Stefan Monnier2015-01-08 15:47:32 -0500134 (accessors ()))
135
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)))
144
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))))))
165
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)))
169
170 ;; Label is nil, or a string
171 (if (not (or (null label) (stringp label)))
172 (signal 'invalid-slot-type (list :label label)))
173
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"))
177
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
59e7fe6d
SM
Stefan Monnier2015-01-21 14:39:06 -0500182 (push `(cl-defmethod (setf ,acces) (value (this ,name))
183 (eieio-oset this ',sname value))
6a67b20d Stefan Monnier2015-01-08 15:47:32 -0500184 accessors)
59e7fe6d Stefan Monnier2015-01-21 14:39:06 -0500185 (push `(cl-defmethod ,acces ((this ,name))
6a67b20d
SM
Stefan Monnier2015-01-08 15:47:32 -0500186 ,(format
187 "Retrieve the slot `%S' from an object of class `%S'."
188 sname name)
59e7fe6d
SM
Stefan Monnier2015-01-21 14:39:06 -0500189 ;; 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'.
197This method is obsolete."
198 sname name)
199 (if (slot-boundp this ',sname)
200 (eieio-oref-default this ',sname)))
201 accessors)))
6a67b20d
SM
Stefan Monnier2015-01-08 15:47:32 -0500202
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
59e7fe6d Stefan Monnier2015-01-21 14:39:06 -0500206 (push `(cl-defmethod ,writer ((this ,name) value)
6a67b20d
SM
Stefan Monnier2015-01-08 15:47:32 -0500207 ,(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
59e7fe6d Stefan Monnier2015-01-21 14:39:06 -0500214 (push `(cl-defmethod ,reader ((this ,name))
6a67b20d
SM
Stefan Monnier2015-01-08 15:47:32 -0500215 ,(format "Access the slot `%S' from object of class `%S'."
216 sname name)
217 (slot-value this ',sname))
218 accessors))
219 ))
220
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.
225
e0be229d
SM
Stefan Monnier2015-01-31 00:48:14 -0500226 ;; Create the test functions.
227 (defalias ',testsym1 (eieio-make-class-predicate ',name))
228 (defalias ',testsym2 (eieio-make-child-predicate ',name))
6a67b20d
SM
Stefan Monnier2015-01-08 15:47:32 -0500229
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"))))
235
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.
243
244 (put ',name 'cl-deftype-satisfies #',testsym2)
245
246 (eieio-defclass-internal ',name ',superclasses ',slots ',options-and-doc)
247
248 ,@accessors
249
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)))
259
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)
2a61bd00
SM
Stefan Monnier2015-01-18 12:24:43 -0500264 (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)))))))
c4e2be45 Stefan Monnier2015-02-16 02:22:46 -0500275 (apply #'make-instance ',name slots))))))
6dd12ef2 Chong Yidong2009-09-28 00:49:54 +0000276
6dd12ef2 Chong Yidong2009-09-28 00:49:54 +0000277
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +0000278;;; Get/Set slots in an object.
279;;
280(defmacro oref (obj slot)
281 "Retrieve the value stored in OBJ in the slot named by SLOT.
282Slot is the name of the slot when created by `defclass' or the label
283created by the :initarg tag."
d4a12e7a Stefan Monnier2014-12-22 15:46:16 -0500284 (declare (debug (form symbolp)))
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +0000285 `(eieio-oref ,obj (quote ,slot)))
286
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +0000287(defalias 'slot-value 'eieio-oref)
288(defalias 'set-slot-value 'eieio-oset)
c4e2be45 Stefan Monnier2015-02-16 02:22:46 -0500289(make-obsolete 'set-slot-value "use (setf (slot-value ..) ..) instead" "25.1")
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +0000290
291(defmacro oref-default (obj slot)
a8f316ca Juanma Barranquero2009-10-05 15:32:08 +0000292 "Get the default value of OBJ (maybe a class) for SLOT.
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +0000293The default value is the value installed in a class with the :initform
294tag. SLOT can be the slot name, or the tag specified by the :initarg
295tag in the `defclass' call."
d4a12e7a Stefan Monnier2014-12-22 15:46:16 -0500296 (declare (debug (form symbolp)))
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +0000297 `(eieio-oref-default ,obj (quote ,slot)))
298
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +0000299;;; Handy CLOS macros
300;;
301(defmacro with-slots (spec-list object &rest body)
302 "Bind SPEC-LIST lexically to slot values in OBJECT, and execute BODY.
303This establishes a lexical environment for referring to the slots in
304the instance named by the given slot-names as though they were
305variables. Within such a context the value of the slot can be
306specified by using its slot name, as if it were a lexically bound
307variable. Both setf and setq can be used to set the value of the
308slot.
309
310SPEC-LIST is of a form similar to `let'. For example:
311
312 ((VAR1 SLOT1)
313 SLOT2
314 SLOTN
315 (VARN+1 SLOTN+1))
316
317Where each VAR is the local variable given to the associated
a8f316ca Juanma Barranquero2009-10-05 15:32:08 +0000318SLOT. A slot specified without a variable name is given a
6dd12ef2 Chong Yidong2009-09-28 00:49:54 +0000319variable name of the same name as the slot."
d4a12e7a Stefan Monnier2014-12-22 15:46:16 -0500320 (declare (indent 2) (debug (sexp sexp def-body)))
94250173 Stefan Monnier2014-10-17 01:09:24 -0400321 (require 'cl-lib)
08dfa0b7 Jonas Bernoulli2014-03-12 16:02:45 +0100322 ;; Transform the spec-list into a cl-symbol-macrolet spec-list.
50c117fe
SM
Stefan Monnier2015-03-18 23:02:26 -0400323 (macroexp-let2 nil object object
324 `(cl-symbol-macrolet
325 ,(mapcar (lambda (entry)
326 (let ((var (if (listp entry) (car entry) entry))
327 (slot (if (listp entry) (cadr entry) entry)))
328 (list var `(slot-value ,object ',slot))))
329 spec-list)
330 ,@body)))
ae277259
SM
Stefan Monnier2015-03-23 18:24:30 -0400331
332;; Keep it as a non-inlined function, so the internals of object don't get
333;; hard-coded in random .elc files.
334(defun eieio-pcase-slot-index-table (obj)
335 "Return some data structure from which can be extracted the slot offset."
336 (eieio--class-index-table
337 (symbol-value (eieio--object-class-tag obj))))
338
339(defun eieio-pcase-slot-index-from-index-table (index-table slot)
340 "Find the index to pass to `aref' to access SLOT."
341 (let ((index (gethash slot index-table)))
342 (if index (+ (eval-when-compile
343 (length (cl-struct-slot-info 'eieio--object)))
344 index))))
345
346(pcase-defmacro eieio (&rest fields)
347 "Pcase patterns to match EIEIO objects.
348Elements of FIELDS can be of the form (NAME UPAT) in which case the contents of
349field NAME is matched against UPAT, or they can be of the form NAME which
350is a shorthand for (NAME NAME)."
2e47de36 Johan Bockgård2015-04-12 16:26:52 +0200351 (declare (debug (&rest [&or (sexp pcase-UPAT) sexp])))
ae277259
SM
Stefan Monnier2015-03-23 18:24:30 -0400352 (let ((is (make-symbol "table")))
353 ;; FIXME: This generates a horrendous mess of redundant let bindings.
354 ;; `pcase' needs to be improved somehow to introduce let-bindings more
355 ;; sparingly, or the byte-compiler needs to be taught to optimize
356 ;; them away.
357 ;; FIXME: `pcase' does not do a good job here of sharing tests&code among
358 ;; various branches.
359 `(and (pred eieio-object-p)
360 (app eieio-pcase-slot-index-table ,is)
361 ,@(mapcar (lambda (field)
362 (let* ((name (if (consp field) (car field) field))
363 (pat (if (consp field) (cadr field) field))
364 (i (make-symbol "index")))
365 `(and (let (and ,i (pred natnump))
366 (eieio-pcase-slot-index-from-index-table
367 ,is ',name))
368 (app (pcase--flip aref ,i) ,pat))))
369 fields))))
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +0000370\f
371;;; Simple generators, and query functions. None of these would do
372;; well embedded into an object.
373;;
50c117fe Stefan Monnier2015-03-18 23:02:26 -0400374
8ca4f1e0 Stefan Monnier2013-02-18 21:57:04 -0500375(define-obsolete-function-alias
50c117fe Stefan Monnier2015-03-18 23:02:26 -0400376 'object-class-fast #'eieio-object-class "24.4")
6dd12ef2 Chong Yidong2009-09-28 00:49:54 +0000377
59e7fe6d
SM
Stefan Monnier2015-01-21 14:39:06 -0500378(cl-defgeneric eieio-object-name-string (obj)
379 "Return a string which is OBJ's name."
380 (declare (obsolete eieio-named "25.1")))
381
8ca4f1e0 Stefan Monnier2013-02-18 21:57:04 -0500382(defun eieio-object-name (obj &optional extra)
c4e2be45 Stefan Monnier2015-02-16 02:22:46 -0500383 "Return a printed representation for object OBJ.
6dd12ef2 Chong Yidong2009-09-28 00:49:54 +0000384If EXTRA, include that in the string returned to represent the symbol."
e0be229d Stefan Monnier2015-01-31 00:48:14 -0500385 (cl-check-type obj eieio-object)
50c117fe Stefan Monnier2015-03-18 23:02:26 -0400386 (format "#<%s %s%s>" (eieio-object-class obj)
ee93d7ad Stefan Monnier2014-12-22 22:05:46 -0500387 (eieio-object-name-string obj) (or extra "")))
8ca4f1e0
SM
Stefan Monnier2013-02-18 21:57:04 -0500388(define-obsolete-function-alias 'object-name #'eieio-object-name "24.4")
389
ee93d7ad
SM
Stefan Monnier2014-12-22 22:05:46 -0500390(defconst eieio--object-names (make-hash-table :test #'eq :weakness 'key))
391
392;; In the past, every EIEIO object had a `name' field, so we had the two method
393;; below "for free". Since this field is very rarely used, we got rid of it
394;; and instead we keep it in a weak hash-tables, for those very rare objects
395;; that use it.
59e7fe6d Stefan Monnier2015-01-21 14:39:06 -0500396(cl-defmethod eieio-object-name-string (obj)
ee93d7ad
SM
Stefan Monnier2014-12-22 22:05:46 -0500397 (or (gethash obj eieio--object-names)
398 (symbol-name (eieio-object-class obj))))
8ca4f1e0
SM
Stefan Monnier2013-02-18 21:57:04 -0500399(define-obsolete-function-alias
400 'object-name-string #'eieio-object-name-string "24.4")
401
59e7fe6d Stefan Monnier2015-01-21 14:39:06 -0500402(cl-defmethod eieio-object-set-name-string (obj name)
8ca4f1e0 Stefan Monnier2013-02-18 21:57:04 -0500403 "Set the string which is OBJ's NAME."
ee93d7ad Stefan Monnier2014-12-22 22:05:46 -0500404 (declare (obsolete eieio-named "25.1"))
e0be229d Stefan Monnier2015-01-31 00:48:14 -0500405 (cl-check-type name string)
ee93d7ad Stefan Monnier2014-12-22 22:05:46 -0500406 (setf (gethash obj eieio--object-names) name))
8ca4f1e0
SM
Stefan Monnier2013-02-18 21:57:04 -0500407(define-obsolete-function-alias
408 'object-set-name-string 'eieio-object-set-name-string "24.4")
409
232823a1
SM
Stefan Monnier2014-12-29 12:11:09 -0500410(defun eieio-object-class (obj)
411 "Return the class struct defining OBJ."
412 ;; FIXME: We say we return a "struct" but we return a symbol instead!
e0be229d Stefan Monnier2015-01-31 00:48:14 -0500413 (cl-check-type obj eieio-object)
50c117fe Stefan Monnier2015-03-18 23:02:26 -0400414 (eieio--class-name (eieio--object-class obj)))
8ca4f1e0
SM
Stefan Monnier2013-02-18 21:57:04 -0500415(define-obsolete-function-alias 'object-class #'eieio-object-class "24.4")
416;; CLOS name, maybe?
417(define-obsolete-function-alias 'class-of #'eieio-object-class "24.4")
418
419(defun eieio-object-class-name (obj)
420 "Return a Lisp like symbol name for OBJ's class."
e0be229d Stefan Monnier2015-01-31 00:48:14 -0500421 (cl-check-type obj eieio-object)
50c117fe Stefan Monnier2015-03-18 23:02:26 -0400422 (eieio-class-name (eieio--object-class obj)))
8ca4f1e0
SM
Stefan Monnier2013-02-18 21:57:04 -0500423(define-obsolete-function-alias
424 'object-class-name 'eieio-object-class-name "24.4")
425
8ca4f1e0 Stefan Monnier2013-02-18 21:57:04 -0500426(defun eieio-class-parents (class)
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +0000427 "Return parent classes to CLASS. (overload of variable).
428
429The CLOS function `class-direct-superclasses' is aliased to this function."
50c117fe Stefan Monnier2015-03-18 23:02:26 -0400430 (eieio--class-parents (eieio--class-object class)))
cb4db863 Stefan Monnier2015-01-04 23:11:37 -0500431
8ca4f1e0 Stefan Monnier2013-02-18 21:57:04 -0500432(define-obsolete-function-alias 'class-parents #'eieio-class-parents "24.4")
6dd12ef2 Chong Yidong2009-09-28 00:49:54 +0000433
8ca4f1e0
SM
Stefan Monnier2013-02-18 21:57:04 -0500434(defun eieio-class-children (class)
435 "Return child classes to CLASS.
6dd12ef2 Chong Yidong2009-09-28 00:49:54 +0000436The CLOS function `class-direct-subclasses' is aliased to this function."
e0be229d Stefan Monnier2015-01-31 00:48:14 -0500437 (cl-check-type class class)
1599688e Stefan Monnier2015-01-07 23:11:58 -0500438 (eieio--class-children (eieio--class-v class)))
8ca4f1e0
SM
Stefan Monnier2013-02-18 21:57:04 -0500439(define-obsolete-function-alias
440 'class-children #'eieio-class-children "24.4")
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +0000441
442;; Official CLOS functions.
8ca4f1e0
SM
Stefan Monnier2013-02-18 21:57:04 -0500443(define-obsolete-function-alias
444 'class-direct-superclasses #'eieio-class-parents "24.4")
445(define-obsolete-function-alias
446 'class-direct-subclasses #'eieio-class-children "24.4")
6dd12ef2 Chong Yidong2009-09-28 00:49:54 +0000447
8ca4f1e0
SM
Stefan Monnier2013-02-18 21:57:04 -0500448(defmacro eieio-class-parent (class)
449 "Return first parent class to CLASS. (overload of variable)."
450 `(car (eieio-class-parents ,class)))
9a0289a2 Glenn Morris2013-09-19 16:11:48 -0400451(define-obsolete-function-alias 'class-parent 'eieio-class-parent "24.4")
6dd12ef2 Chong Yidong2009-09-28 00:49:54 +0000452
1599688e
SM
Stefan Monnier2015-01-07 23:11:58 -0500453(defun same-class-p (obj class)
454 "Return t if OBJ is of class-type CLASS."
455 (setq class (eieio--class-object class))
e0be229d
SM
Stefan Monnier2015-01-31 00:48:14 -0500456 (cl-check-type class eieio--class)
457 (cl-check-type obj eieio-object)
50c117fe Stefan Monnier2015-03-18 23:02:26 -0400458 (eq (eieio--object-class obj) class))
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +0000459
460(defun object-of-class-p (obj class)
461 "Return non-nil if OBJ is an instance of CLASS or CLASS' subclasses."
e0be229d Stefan Monnier2015-01-31 00:48:14 -0500462 (cl-check-type obj eieio-object)
6dd12ef2 Chong Yidong2009-09-28 00:49:54 +0000463 ;; class will be checked one layer down
50c117fe Stefan Monnier2015-03-18 23:02:26 -0400464 (child-of-class-p (eieio--object-class obj) class))
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +0000465;; Backwards compatibility
466(defalias 'obj-of-class-p 'object-of-class-p)
467
468(defun child-of-class-p (child class)
a8f316ca Juanma Barranquero2009-10-05 15:32:08 +0000469 "Return non-nil if CHILD class is a subclass of CLASS."
232823a1 Stefan Monnier2014-12-29 12:11:09 -0500470 (setq child (eieio--class-object child))
e0be229d Stefan Monnier2015-01-31 00:48:14 -0500471 (cl-check-type child eieio--class)
50c117fe Stefan Monnier2015-03-18 23:02:26 -0400472 ;; `eieio-default-superclass' is never mentioned in eieio--class-parents,
bcebc831
SM
Stefan Monnier2014-12-22 15:13:02 -0500473 ;; so we have to special case it here.
474 (or (eq class 'eieio-default-superclass)
475 (let ((p nil))
232823a1 Stefan Monnier2014-12-29 12:11:09 -0500476 (setq class (eieio--class-object class))
e0be229d Stefan Monnier2015-01-31 00:48:14 -0500477 (cl-check-type class eieio--class)
bcebc831 Stefan Monnier2014-12-22 15:13:02 -0500478 (while (and child (not (eq child class)))
50c117fe Stefan Monnier2015-03-18 23:02:26 -0400479 (setq p (append p (eieio--class-parents child))
cb4db863 Stefan Monnier2015-01-04 23:11:37 -0500480 child (pop p)))
bcebc831 Stefan Monnier2014-12-22 15:13:02 -0500481 (if child t))))
6dd12ef2 Chong Yidong2009-09-28 00:49:54 +0000482
50c117fe
SM
Stefan Monnier2015-03-18 23:02:26 -0400483(defun eieio-slot-descriptor-name (slot)
484 (cl--slot-descriptor-name slot))
c4e2be45
SM
Stefan Monnier2015-02-16 02:22:46 -0500485
486(defun eieio-class-slots (class)
487 "Return list of slots available in instances of CLASS."
488 ;; FIXME: This only gives the instance slots and ignores the
489 ;; class-allocated slots.
c4e2be45
SM
Stefan Monnier2015-02-16 02:22:46 -0500490 (setq class (eieio--class-object class))
491 (cl-check-type class eieio--class)
50c117fe Stefan Monnier2015-03-18 23:02:26 -0400492 (mapcar #'identity (eieio--class-slots class)))
c4e2be45 Stefan Monnier2015-02-16 02:22:46 -0500493
a2930e43 Eric M. Ludlam2010-09-19 00:23:57 -0400494(defun object-slots (obj)
29f7f98b Stefan Monnier2015-03-19 10:35:52 -0400495 "Return list of slot names available in OBJ."
c4e2be45 Stefan Monnier2015-02-16 02:22:46 -0500496 (declare (obsolete eieio-class-slots "25.1"))
e0be229d Stefan Monnier2015-01-31 00:48:14 -0500497 (cl-check-type obj eieio-object)
29f7f98b
SM
Stefan Monnier2015-03-19 10:35:52 -0400498 (mapcar #'cl--slot-descriptor-name
499 (eieio-class-slots (eieio--object-class obj))))
6dd12ef2 Chong Yidong2009-09-28 00:49:54 +0000500
50c117fe
SM
Stefan Monnier2015-03-18 23:02:26 -0400501(defun eieio--class-slot-initarg (class slot)
502 "Fetch from CLASS, SLOT's :initarg."
e0be229d Stefan Monnier2015-01-31 00:48:14 -0500503 (cl-check-type class eieio--class)
cb4db863 Stefan Monnier2015-01-04 23:11:37 -0500504 (let ((ia (eieio--class-initarg-tuples class))
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +0000505 (f nil))
506 (while (and ia (not f))
507 (if (eq (cdr (car ia)) slot)
508 (setq f (car (car ia))))
509 (setq ia (cdr ia)))
510 f))
511
9760c73c
SM
Stefan Monnier2012-10-13 21:42:48 -0400512;;; Object Set macros
513;;
514(defmacro oset (obj slot value)
515 "Set the value in OBJ for slot SLOT to VALUE.
516SLOT is the slot name as specified in `defclass' or the tag created
517with in the :initarg slot. VALUE can be any Lisp object."
d4a12e7a Stefan Monnier2014-12-22 15:46:16 -0500518 (declare (debug (form symbolp form)))
9760c73c
SM
Stefan Monnier2012-10-13 21:42:48 -0400519 `(eieio-oset ,obj (quote ,slot) ,value))
520
9760c73c
SM
Stefan Monnier2012-10-13 21:42:48 -0400521(defmacro oset-default (class slot value)
522 "Set the default slot in CLASS for SLOT to VALUE.
523The default value is usually set with the :initform tag during class
524creation. This allows users to change the default behavior of classes
525after they are created."
d4a12e7a Stefan Monnier2014-12-22 15:46:16 -0500526 (declare (debug (form symbolp form)))
9760c73c
SM
Stefan Monnier2012-10-13 21:42:48 -0400527 `(eieio-oset-default ,class (quote ,slot) ,value))
528
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +0000529;;; CLOS queries into classes and slots
530;;
531(defun slot-boundp (object slot)
a8f316ca Juanma Barranquero2009-10-05 15:32:08 +0000532 "Return non-nil if OBJECT's SLOT is bound.
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +0000533Setting a slot's value makes it bound. Calling `slot-makeunbound' will
534make a slot unbound.
535OBJECT can be an instance or a class."
536 ;; Skip typechecking while retrieving this value.
537 (let ((eieio-skip-typecheck t))
538 ;; Return nil if the magic symbol is in there.
9869b3ae
SM
Stefan Monnier2011-05-05 00:42:09 -0300539 (not (eq (cond
540 ((eieio-object-p object) (eieio-oref object slot))
e0be229d Stefan Monnier2015-01-31 00:48:14 -0500541 ((symbolp object) (eieio-oref-default object slot))
9869b3ae
SM
Stefan Monnier2011-05-05 00:42:09 -0300542 (t (signal 'wrong-type-argument (list 'eieio-object-p object))))
543 eieio-unbound))))
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +0000544
545(defun slot-makeunbound (object slot)
546 "In OBJECT, make SLOT unbound."
547 (eieio-oset object slot eieio-unbound))
548
549(defun slot-exists-p (object-or-class slot)
a8f316ca Juanma Barranquero2009-10-05 15:32:08 +0000550 "Return non-nil if OBJECT-OR-CLASS has SLOT."
cb4db863 Stefan Monnier2015-01-04 23:11:37 -0500551 (let ((cv (cond ((eieio-object-p object-or-class)
50c117fe Stefan Monnier2015-03-18 23:02:26 -0400552 (eieio--object-class object-or-class))
e0be229d
SM
Stefan Monnier2015-01-31 00:48:14 -0500553 ((eieio--class-p object-or-class) object-or-class)
554 (t (find-class object-or-class 'error)))))
50c117fe
SM
Stefan Monnier2015-03-18 23:02:26 -0400555 (or (gethash slot (eieio--class-index-table cv))
556 ;; FIXME: We could speed this up by adding class slots into the
557 ;; index-table (e.g. with a negative index?).
558 (let ((cs (eieio--class-class-slots cv))
559 found)
560 (dotimes (i (length cs))
561 (if (eq slot (cl--slot-descriptor-name (aref cs i)))
562 (setq found t)))
563 found))))
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +0000564
565(defun find-class (symbol &optional errorp)
566 "Return the class that SYMBOL represents.
567If there is no class, nil is returned if ERRORP is nil.
568If ERRORP is non-nil, `wrong-argument-type' is signaled."
e0be229d
SM
Stefan Monnier2015-01-31 00:48:14 -0500569 (let ((class (eieio--class-v symbol)))
570 (cond
571 ((eieio--class-p class) class)
572 (errorp (signal 'wrong-type-argument (list 'class-p symbol))))))
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +0000573
574;;; Slightly more complex utility functions for objects
575;;
576(defun object-assoc (key slot list)
577 "Return an object if KEY is `equal' to SLOT's value of an object in LIST.
a8f316ca Juanma Barranquero2009-10-05 15:32:08 +0000578LIST is a list of objects whose slots are searched.
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +0000579Objects in LIST do not need to have a slot named SLOT, nor does
580SLOT need to be bound. If these errors occur, those objects will
581be ignored."
e0be229d Stefan Monnier2015-01-31 00:48:14 -0500582 (cl-check-type list list)
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +0000583 (while (and list (not (condition-case nil
584 ;; This prevents errors for missing slots.
585 (equal key (eieio-oref (car list) slot))
586 (error nil))))
587 (setq list (cdr list)))
588 (car list))
589
590(defun object-assoc-list (slot list)
591 "Return an association list with the contents of SLOT as the key element.
592LIST must be a list of objects with SLOT in it.
593This is useful when you need to do completing read on an object group."
e0be229d Stefan Monnier2015-01-31 00:48:14 -0500594 (cl-check-type list list)
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +0000595 (let ((assoclist nil))
596 (while list
597 (setq assoclist (cons (cons (eieio-oref (car list) slot)
598 (car list))
599 assoclist))
600 (setq list (cdr list)))
601 (nreverse assoclist)))
602
603(defun object-assoc-list-safe (slot list)
604 "Return an association list with the contents of SLOT as the key element.
605LIST must be a list of objects, but those objects do not need to have
606SLOT in it. If it does not, then that element is left out of the association
607list."
e0be229d Stefan Monnier2015-01-31 00:48:14 -0500608 (cl-check-type list list)
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +0000609 (let ((assoclist nil))
610 (while list
611 (if (slot-exists-p (car list) slot)
612 (setq assoclist (cons (cons (eieio-oref (car list) slot)
613 (car list))
614 assoclist)))
615 (setq list (cdr list)))
616 (nreverse assoclist)))
617
618(defun object-add-to-list (object slot item &optional append)
619 "In OBJECT's SLOT, add ITEM to the list of elements.
620Optional argument APPEND indicates we need to append to the list.
621If ITEM already exists in the list in SLOT, then it is not added.
622Comparison is done with `equal' through the `member' function call.
623If SLOT is unbound, bind it to the list containing ITEM."
624 (let (ov)
625 ;; Find the originating list.
626 (if (not (slot-boundp object slot))
627 (setq ov (list item))
628 (setq ov (eieio-oref object slot))
629 ;; turn it into a list.
630 (unless (listp ov)
631 (setq ov (list ov)))
632 ;; Do the combination
633 (if (not (member item ov))
634 (setq ov
635 (if append
636 (append ov (list item))
637 (cons item ov)))))
638 ;; Set back into the slot.
639 (eieio-oset object slot ov)))
640
641(defun object-remove-from-list (object slot item)
642 "In OBJECT's SLOT, remove occurrences of ITEM.
a8f316ca Juanma Barranquero2009-10-05 15:32:08 +0000643Deletion is done with `delete', which deletes by side effect,
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +0000644and comparisons are done with `equal'.
645If SLOT is unbound, do nothing."
646 (if (not (slot-boundp object slot))
647 nil
648 (eieio-oset object slot (delete item (eieio-oref object slot)))))
6dd12ef2 Chong Yidong2009-09-28 00:49:54 +0000649
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +0000650;;; Here are some CLOS items that need the CL package
651;;
652
c4e2be45
SM
Stefan Monnier2015-02-16 02:22:46 -0500653;; FIXME: Shouldn't this be a more complex gv-expander which extracts the
654;; common code between oref and oset, so as to reduce the redundant work done
655;; in (push foo (oref bar baz)), like we do for the `nth' expander?
94250173 Stefan Monnier2014-10-17 01:09:24 -0400656(gv-define-simple-setter eieio-oref eieio-oset)
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +0000657
658\f
659;;;
660;; We want all objects created by EIEIO to have some default set of
91af3942 Paul Eggert2011-11-14 15:59:56 -0800661;; behaviors so we can create object utilities, and allow various
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +0000662;; types of error checking. To do this, create the default EIEIO
663;; class, and when no parent class is specified, use this as the
664;; default. (But don't store it in the other classes as the default,
665;; allowing for transparent support.)
666;;
667
668(defclass eieio-default-superclass nil
669 nil
670 "Default parent class for classes with no specified parent class.
a8f316ca
JB
Juanma Barranquero2009-10-05 15:32:08 +0000671Its slots are automatically adopted by classes with no specified parents.
672This class is not stored in the `parent' slot of a class vector."
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +0000673 :abstract t)
674
cb4db863
SM
Stefan Monnier2015-01-04 23:11:37 -0500675(setq eieio-default-superclass (eieio--class-v 'eieio-default-superclass))
676
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +0000677(defalias 'standard-class 'eieio-default-superclass)
678
c4e2be45
SM
Stefan Monnier2015-02-16 02:22:46 -0500679(cl-defgeneric make-instance (class &rest initargs)
680 "Make a new instance of CLASS based on INITARGS.
681For example:
682
683 (make-instance 'foo)
684
685INITARGS is a property list with keywords based on the `:initarg'
686for each slot. For example:
687
688 (make-instance 'foo :slot1 value1 :slotN valueN)")
6dd12ef2 Chong Yidong2009-09-28 00:49:54 +0000689
c4e2be45 Stefan Monnier2015-02-16 02:22:46 -0500690(define-obsolete-function-alias 'constructor #'make-instance "25.1")
ee93d7ad Stefan Monnier2014-12-22 22:05:46 -0500691
c4e2be45
SM
Stefan Monnier2015-02-16 02:22:46 -0500692(cl-defmethod make-instance
693 ((class (subclass eieio-default-superclass)) &rest slots)
a8f316ca Juanma Barranquero2009-10-05 15:32:08 +0000694 "Default constructor for CLASS `eieio-default-superclass'.
c4e2be45 Stefan Monnier2015-02-16 02:22:46 -0500695SLOTS are the initialization slots used by `initialize-instance'.
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +0000696This static method is called when an object is constructed.
697It allocates the vector used to represent an EIEIO object, and then
c4e2be45 Stefan Monnier2015-02-16 02:22:46 -0500698calls `initialize-instance' on that object."
4610ce96 Stefan Monnier2015-01-17 09:41:51 -0500699 (let* ((new-object (copy-sequence (eieio--class-default-object-cache
c4e2be45 Stefan Monnier2015-02-16 02:22:46 -0500700 (eieio--class-object class)))))
4610ce96
SM
Stefan Monnier2015-01-17 09:41:51 -0500701 (if (and slots
702 (let ((x (car slots)))
703 (or (stringp x) (null x))))
704 (funcall (if eieio-backward-compatibility #'ignore #'message)
705 "Obsolete name %S passed to %S constructor"
706 (pop slots) class))
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +0000707 ;; Call the initialize method on the new object with the slots
708 ;; that were passed down to us.
709 (initialize-instance new-object slots)
710 ;; Return the created object.
711 new-object))
712
c4e2be45 Stefan Monnier2015-02-16 02:22:46 -0500713;; FIXME: CLOS uses "&rest INITARGS" instead.
59e7fe6d Stefan Monnier2015-01-21 14:39:06 -0500714(cl-defgeneric shared-initialize (obj slots)
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +0000715 "Set slots of OBJ with SLOTS which is a list of name/value pairs.
716Called from the constructor routine.")
717
59e7fe6d Stefan Monnier2015-01-21 14:39:06 -0500718(cl-defmethod shared-initialize ((obj eieio-default-superclass) slots)
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +0000719 "Set slots of OBJ with SLOTS which is a list of name/value pairs.
720Called from the constructor routine."
d48c98cd Stefan Monnier2015-01-16 23:48:26 -0500721 (while slots
50c117fe Stefan Monnier2015-03-18 23:02:26 -0400722 (let ((rn (eieio--initarg-to-attribute (eieio--object-class obj)
d48c98cd
SM
Stefan Monnier2015-01-16 23:48:26 -0500723 (car slots))))
724 (if (not rn)
725 (slot-missing obj (car slots) 'oset (car (cdr slots)))
726 (eieio-oset obj rn (car (cdr slots)))))
727 (setq slots (cdr (cdr slots)))))
6dd12ef2 Chong Yidong2009-09-28 00:49:54 +0000728
c4e2be45 Stefan Monnier2015-02-16 02:22:46 -0500729;; FIXME: CLOS uses "&rest INITARGS" instead.
59e7fe6d Stefan Monnier2015-01-21 14:39:06 -0500730(cl-defgeneric initialize-instance (this &optional slots)
a8f316ca Juanma Barranquero2009-10-05 15:32:08 +0000731 "Construct the new object THIS based on SLOTS.")
6dd12ef2 Chong Yidong2009-09-28 00:49:54 +0000732
59e7fe6d Stefan Monnier2015-01-21 14:39:06 -0500733(cl-defmethod initialize-instance ((this eieio-default-superclass)
6dd12ef2 Chong Yidong2009-09-28 00:49:54 +0000734 &optional slots)
a8f316ca Juanma Barranquero2009-10-05 15:32:08 +0000735 "Construct the new object THIS based on SLOTS.
6dd12ef2 Chong Yidong2009-09-28 00:49:54 +0000736SLOTS is a tagged list where odd numbered elements are tags, and
a8f316ca
JB
Juanma Barranquero2009-10-05 15:32:08 +0000737even numbered elements are the values to store in the tagged slot.
738If you overload the `initialize-instance', there you will need to
739call `shared-initialize' yourself, or you can call `call-next-method'
740to have this constructor called automatically. If these steps are
741not taken, then new objects of your class will not have their values
6dd12ef2 Chong Yidong2009-09-28 00:49:54 +0000742dynamically set from SLOTS."
890f7890
DE
David Engster2013-06-02 15:33:09 +0200743 ;; First, see if any of our defaults are `lambda', and
744 ;; re-evaluate them and apply the value to our slots.
50c117fe
SM
Stefan Monnier2015-03-18 23:02:26 -0400745 (let* ((this-class (eieio--object-class this))
746 (slots (eieio--class-slots this-class)))
747 (dotimes (i (length slots))
890f7890
DE
David Engster2013-06-02 15:33:09 +0200748 ;; For each slot, see if we need to evaluate it.
749 ;;
750 ;; Paul Landes said in an email:
751 ;; > CL evaluates it if it can, and otherwise, leaves it as
752 ;; > the quoted thing as you already have. This is by the
753 ;; > Sonya E. Keene book and other things I've look at on the
754 ;; > web.
50c117fe
SM
Stefan Monnier2015-03-18 23:02:26 -0400755 (let* ((slot (aref slots i))
756 (initform (cl--slot-descriptor-initform slot))
757 (dflt (eieio-default-eval-maybe initform)))
758 (when (not (eq dflt initform))
759 ;; FIXME: We should be able to just do (aset this (+ i <cst>) dflt)!
760 (eieio-oset this (cl--slot-descriptor-name slot) dflt)))))
890f7890
DE
David Engster2013-06-02 15:33:09 +0200761 ;; Shared initialize will parse our slots for us.
762 (shared-initialize this slots))
6dd12ef2 Chong Yidong2009-09-28 00:49:54 +0000763
59e7fe6d Stefan Monnier2015-01-21 14:39:06 -0500764(cl-defgeneric slot-missing (object slot-name operation &optional new-value)
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +0000765 "Method invoked when an attempt to access a slot in OBJECT fails.")
766
59e7fe6d Stefan Monnier2015-01-21 14:39:06 -0500767(cl-defmethod slot-missing ((object eieio-default-superclass) slot-name
94250173 Stefan Monnier2014-10-17 01:09:24 -0400768 _operation &optional _new-value)
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +0000769 "Method invoked when an attempt to access a slot in OBJECT fails.
770SLOT-NAME is the name of the failed slot, OPERATION is the type of access
771that was requested, and optional NEW-VALUE is the value that was desired
772to be set.
773
774This method is called from `oref', `oset', and other functions which
775directly reference slots in EIEIO objects."
8ca4f1e0 Stefan Monnier2013-02-18 21:57:04 -0500776 (signal 'invalid-slot-name (list (eieio-object-name object)
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +0000777 slot-name)))
778
59e7fe6d Stefan Monnier2015-01-21 14:39:06 -0500779(cl-defgeneric slot-unbound (object class slot-name fn)
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +0000780 "Slot unbound is invoked during an attempt to reference an unbound slot.")
781
59e7fe6d Stefan Monnier2015-01-21 14:39:06 -0500782(cl-defmethod slot-unbound ((object eieio-default-superclass)
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +0000783 class slot-name fn)
784 "Slot unbound is invoked during an attempt to reference an unbound slot.
785OBJECT is the instance of the object being reference. CLASS is the
786class of OBJECT, and SLOT-NAME is the offending slot. This function
787throws the signal `unbound-slot'. You can overload this function and
788return the value to use in place of the unbound value.
789Argument FN is the function signaling this error.
790Use `slot-boundp' to determine if a slot is bound or not.
791
792In CLOS, the argument list is (CLASS OBJECT SLOT-NAME), but
793EIEIO can only dispatch on the first argument, so the first two are swapped."
c4e2be45
SM
Stefan Monnier2015-02-16 02:22:46 -0500794 (signal 'unbound-slot (list (eieio-class-name class)
795 (eieio-object-name object)
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +0000796 slot-name fn)))
797
59e7fe6d Stefan Monnier2015-01-21 14:39:06 -0500798(cl-defgeneric clone (obj &rest params)
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +0000799 "Make a copy of OBJ, and then supply PARAMS.
800PARAMS is a parameter list of the same form used by `initialize-instance'.
801
802When overloading `clone', be sure to call `call-next-method'
803first and modify the returned object.")
804
59e7fe6d Stefan Monnier2015-01-21 14:39:06 -0500805(cl-defmethod clone ((obj eieio-default-superclass) &rest params)
6dd12ef2 Chong Yidong2009-09-28 00:49:54 +0000806 "Make a copy of OBJ, and then apply PARAMS."
ee93d7ad
SM
Stefan Monnier2014-12-22 22:05:46 -0500807 (let ((nobj (copy-sequence obj)))
808 (if (stringp (car params))
1599688e
SM
Stefan Monnier2015-01-07 23:11:58 -0500809 (funcall (if eieio-backward-compatibility #'ignore #'message)
810 "Obsolete name %S passed to clone" (pop params)))
ee93d7ad Stefan Monnier2014-12-22 22:05:46 -0500811 (if params (shared-initialize nobj params))
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +0000812 nobj))
813
59e7fe6d Stefan Monnier2015-01-21 14:39:06 -0500814(cl-defgeneric destructor (this &rest params)
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +0000815 "Destructor for cleaning up any dynamic links to our object.")
816
59e7fe6d Stefan Monnier2015-01-21 14:39:06 -0500817(cl-defmethod destructor ((_this eieio-default-superclass) &rest _params)
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +0000818 "Destructor for cleaning up any dynamic links to our object.
819Argument THIS is the object being destroyed. PARAMS are additional
820ignored parameters."
821 ;; No cleanup... yet.
822 )
823
59e7fe6d Stefan Monnier2015-01-21 14:39:06 -0500824(cl-defgeneric object-print (this &rest strings)
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +0000825 "Pretty printer for object THIS. Call function `object-name' with STRINGS.
826
827It is sometimes useful to put a summary of the object into the
a8f316ca Juanma Barranquero2009-10-05 15:32:08 +0000828default #<notation> string when using EIEIO browsing tools.
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +0000829Implement this method to customize the summary.")
830
59e7fe6d Stefan Monnier2015-01-21 14:39:06 -0500831(cl-defmethod object-print ((this eieio-default-superclass) &rest strings)
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +0000832 "Pretty printer for object THIS. Call function `object-name' with STRINGS.
833The default method for printing object THIS is to use the
834function `object-name'.
835
836It is sometimes useful to put a summary of the object into the
a8f316ca Juanma Barranquero2009-10-05 15:32:08 +0000837default #<notation> string when using EIEIO browsing tools.
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +0000838
839Implement this function and specify STRINGS in a call to
840`call-next-method' to provide additional summary information.
841When passing in extra strings from child classes, always remember
842to prepend a space."
94250173 Stefan Monnier2014-10-17 01:09:24 -0400843 (eieio-object-name this (apply #'concat strings)))
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +0000844
845(defvar eieio-print-depth 0
846 "When printing, keep track of the current indentation depth.")
847
59e7fe6d Stefan Monnier2015-01-21 14:39:06 -0500848(cl-defgeneric object-write (this &optional comment)
6dd12ef2 Chong Yidong2009-09-28 00:49:54 +0000849 "Write out object THIS to the current stream.
a8f316ca Juanma Barranquero2009-10-05 15:32:08 +0000850Optional COMMENT will add comments to the beginning of the output.")
6dd12ef2 Chong Yidong2009-09-28 00:49:54 +0000851
59e7fe6d Stefan Monnier2015-01-21 14:39:06 -0500852(cl-defmethod object-write ((this eieio-default-superclass) &optional comment)
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +0000853 "Write object THIS out to the current stream.
854This writes out the vector version of this object. Complex and recursive
855object are discouraged from being written.
856 If optional COMMENT is non-nil, include comments when outputting
857this object."
858 (when comment
859 (princ ";; Object ")
8ca4f1e0 Stefan Monnier2013-02-18 21:57:04 -0500860 (princ (eieio-object-name-string this))
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +0000861 (princ "\n")
862 (princ comment)
863 (princ "\n"))
8ca4f1e0 Stefan Monnier2013-02-18 21:57:04 -0500864 (let* ((cl (eieio-object-class this))
d4a12e7a Stefan Monnier2014-12-22 15:46:16 -0500865 (cv (eieio--class-v cl)))
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +0000866 ;; Now output readable lisp to recreate this object
867 ;; It should look like this:
868 ;; (<constructor> <name> <slot> <slot> ... )
869 ;; Each slot's slot is writen using its :writer.
870 (princ (make-string (* eieio-print-depth 2) ? ))
871 (princ "(")
ca80ebc7 Stefan Monnier2015-01-17 09:50:07 -0500872 (princ (symbol-name (eieio--class-constructor (eieio-object-class this))))
62a81506 Chong Yidong2012-10-02 02:10:29 +0800873 (princ " ")
8ca4f1e0 Stefan Monnier2013-02-18 21:57:04 -0500874 (prin1 (eieio-object-name-string this))
62a81506 Chong Yidong2012-10-02 02:10:29 +0800875 (princ "\n")
6dd12ef2 Chong Yidong2009-09-28 00:49:54 +0000876 ;; Loop over all the public slots
50c117fe Stefan Monnier2015-03-18 23:02:26 -0400877 (let ((slots (eieio--class-slots cv))
6dd12ef2 Chong Yidong2009-09-28 00:49:54 +0000878 (eieio-print-depth (1+ eieio-print-depth)))
50c117fe
SM
Stefan Monnier2015-03-18 23:02:26 -0400879 (dotimes (i (length slots))
880 (let ((slot (aref slots i)))
881 (when (slot-boundp this (cl--slot-descriptor-name slot))
882 (let ((i (eieio--class-slot-initarg
883 cv (cl--slot-descriptor-name slot)))
884 (v (eieio-oref this (cl--slot-descriptor-name slot))))
885 (unless (or (not i) (equal v (cl--slot-descriptor-initform slot)))
886 (unless (bolp)
887 (princ "\n"))
888 (princ (make-string (* eieio-print-depth 2) ? ))
889 (princ (symbol-name i))
890 (if (alist-get :printer (cl--slot-descriptor-props slot))
891 ;; Use our public printer
892 (progn
893 (princ " ")
894 (funcall (alist-get :printer
895 (cl--slot-descriptor-props slot))
896 v))
897 ;; Use our generic override prin1 function.
898 (princ (if (or (eieio-object-p v)
899 (eieio-object-p (car-safe v)))
900 "\n" " "))
901 (eieio-override-prin1 v))))))))
69e1c203
JB
Jonas Bernoulli2012-12-11 21:16:05 -0500902 (princ ")")
903 (when (= eieio-print-depth 0)
904 (princ "\n"))))
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +0000905
906(defun eieio-override-prin1 (thing)
a8f316ca Juanma Barranquero2009-10-05 15:32:08 +0000907 "Perform a `prin1' on THING taking advantage of object knowledge."
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +0000908 (cond ((eieio-object-p thing)
909 (object-write thing))
69e1c203 Jonas Bernoulli2012-12-11 21:16:05 -0500910 ((consp thing)
6dd12ef2 Chong Yidong2009-09-28 00:49:54 +0000911 (eieio-list-prin1 thing))
e0be229d Stefan Monnier2015-01-31 00:48:14 -0500912 ((eieio--class-p thing)
c4e2be45 Stefan Monnier2015-02-16 02:22:46 -0500913 (princ (eieio--class-print-name thing)))
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +0000914 (t (prin1 thing))))
915
916(defun eieio-list-prin1 (list)
917 "Display LIST where list may contain objects."
918 (if (not (eieio-object-p (car list)))
919 (progn
920 (princ "'")
921 (prin1 list))
6dd12ef2 Chong Yidong2009-09-28 00:49:54 +0000922 (princ (make-string (* eieio-print-depth 2) ? ))
69e1c203
JB
Jonas Bernoulli2012-12-11 21:16:05 -0500923 (princ "(list")
924 (let ((eieio-print-depth (1+ eieio-print-depth)))
925 (while list
926 (princ "\n")
927 (if (eieio-object-p (car list))
928 (object-write (car list))
929 (princ (make-string (* eieio-print-depth 2) ? ))
930 (eieio-override-prin1 (car list)))
931 (setq list (cdr list))))
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +0000932 (princ ")")))
933
934\f
935;;; Unimplemented functions from CLOS
936;;
94250173 Stefan Monnier2014-10-17 01:09:24 -0400937(defun change-class (_obj _class)
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +0000938 "Change the class of OBJ to type CLASS.
939This may create or delete slots, but does not affect the return value
940of `eq'."
a8f316ca Juanma Barranquero2009-10-05 15:32:08 +0000941 (error "EIEIO: `change-class' is unimplemented"))
6dd12ef2 Chong Yidong2009-09-28 00:49:54 +0000942
0f918d96 David Engster2014-01-08 22:30:12 +0100943;; Hook ourselves into help system for describing classes and methods.
0f918d96
DE
David Engster2014-01-08 22:30:12 +0100944(add-hook 'help-fns-describe-function-functions 'eieio-help-constructor)
945
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +0000946;;; Interfacing with edebug
947;;
fce9e553 Eric Abrahamsen2014-11-02 17:24:33 +0200948(defun eieio-edebug-prin1-to-string (print-function object &optional noescape)
a8f316ca Juanma Barranquero2009-10-05 15:32:08 +0000949 "Display EIEIO OBJECT in fancy format.
fce9e553
EA
Eric Abrahamsen2014-11-02 17:24:33 +0200950
951Used as advice around `edebug-prin1-to-string', held in the
952variable PRINT-FUNCTION. Optional argument NOESCAPE is passed to
953`prin1-to-string' when appropriate."
c4e2be45 Stefan Monnier2015-02-16 02:22:46 -0500954 (cond ((eieio--class-p object) (eieio--class-print-name object))
6dd12ef2 Chong Yidong2009-09-28 00:49:54 +0000955 ((eieio-object-p object) (object-print object))
cb4db863 Stefan Monnier2015-01-04 23:11:37 -0500956 ((and (listp object) (or (eieio--class-p (car object))
6dd12ef2 Chong Yidong2009-09-28 00:49:54 +0000957 (eieio-object-p (car object))))
cb4db863
SM
Stefan Monnier2015-01-04 23:11:37 -0500958 (concat "(" (mapconcat
959 (lambda (x) (eieio-edebug-prin1-to-string print-function x))
960 object " ")
94250173 Stefan Monnier2014-10-17 01:09:24 -0400961 ")"))
fce9e553 Eric Abrahamsen2014-11-02 17:24:33 +0200962 (t (funcall print-function object noescape))))
6dd12ef2 Chong Yidong2009-09-28 00:49:54 +0000963
d4a12e7a
SM
Stefan Monnier2014-12-22 15:46:16 -0500964(advice-add 'edebug-prin1-to-string
965 :around #'eieio-edebug-prin1-to-string)
6dd12ef2 Chong Yidong2009-09-28 00:49:54 +0000966
002b46b7
GM
Glenn Morris2011-02-18 00:00:08 -0800967\f
968;;; Start of automatically extracted autoloads.
969\f
ada5e6b7 Glenn Morris2015-05-06 06:19:35 -0400970;;;### (autoloads nil "eieio-custom" "eieio-custom.el" "916f54b818479a77a02f3ecccda84a11")
002b46b7
GM
Glenn Morris2011-02-18 00:00:08 -0800971;;; Generated autoloads from eieio-custom.el
972
973(autoload 'customize-object "eieio-custom" "\
974Customize OBJ in a custom buffer.
975Optional argument GROUP is the sub-group of slots to display.
976
977\(fn OBJ &optional GROUP)" nil nil)
978
979;;;***
980\f
5ed5a049 Glenn Morris2015-05-08 06:22:25 -0400981;;;### (autoloads nil "eieio-opt" "eieio-opt.el" "59cee62a4829ace9bb4a6526442d2b3c")
002b46b7
GM
Glenn Morris2011-02-18 00:00:08 -0800982;;; Generated autoloads from eieio-opt.el
983
984(autoload 'eieio-browse "eieio-opt" "\
985Create an object browser window to show all objects.
986If optional ROOT-CLASS, then start with that, otherwise start with
987variable `eieio-default-superclass'.
988
989\(fn &optional ROOT-CLASS)" t nil)
002b46b7 Glenn Morris2011-02-18 00:00:08 -0800990
05e0afce
DE
David Engster2014-01-09 21:31:21 +0100991(autoload 'eieio-help-class "eieio-opt" "\
992Print help description for CLASS.
002b46b7 Glenn Morris2011-02-18 00:00:08 -0800993If CLASS is actually an object, then also display current values of that object.
002b46b7 Glenn Morris2011-02-18 00:00:08 -0800994
05e0afce David Engster2014-01-09 21:31:21 +0100995\(fn CLASS)" nil nil)
002b46b7 Glenn Morris2011-02-18 00:00:08 -0800996
05e0afce
DE
David Engster2014-01-09 21:31:21 +0100997(autoload 'eieio-help-constructor "eieio-opt" "\
998Describe CTR if it is a class constructor.
002b46b7 Glenn Morris2011-02-18 00:00:08 -0800999
05e0afce David Engster2014-01-09 21:31:21 +01001000\(fn CTR)" nil nil)
002b46b7 Glenn Morris2011-02-18 00:00:08 -08001001
002b46b7
GM
Glenn Morris2011-02-18 00:00:08 -08001002;;;***
1003\f
1004;;; End of automatically extracted autoloads.
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +00001005
1006(provide 'eieio)
1007
6dd12ef2 Chong Yidong2009-09-28 00:49:54 +00001008;;; eieio ends here