; Auto-commit of loaddefs files.
[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)))
c87523bd Paul Eggert2015-08-21 20:19:46 -0700117 (error "Too many arguments to ‘defclass’"))
6a67b20d
SM
Stefan Monnier2015-01-08 15:47:32 -0500118 ((and (symbolp (car options-and-doc))
119 (/= 0 (% (length options-and-doc) 2)))
c87523bd Paul Eggert2015-08-21 20:19:46 -0700120 (error "Too many arguments to ‘defclass’")))
6a67b20d
SM
Stefan Monnier2015-01-08 15:47:32 -0500121
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
59b5723c
SM
Stefan Monnier2015-07-07 02:14:16 -0400145 ;; Update eieio--known-slot-names already in case we compile code which
146 ;; uses this before the class is loaded.
147 (cl-pushnew sname eieio--known-slot-names)
148
6a67b20d
SM
Stefan Monnier2015-01-08 15:47:32 -0500149 (if eieio-error-unsupported-class-tags
150 (let ((tmp soptions))
151 (while tmp
152 (if (not (member (car tmp) '(:accessor
153 :initform
154 :initarg
155 :documentation
156 :protection
157 :reader
158 :writer
159 :allocation
160 :type
161 :custom
162 :label
163 :group
164 :printer
165 :allow-nil-initform
166 :custom-groups)))
167 (signal 'invalid-slot-type (list (car tmp))))
168 (setq tmp (cdr (cdr tmp))))))
169
170 ;; Make sure the :allocation parameter has a valid value.
171 (if (not (memq alloc '(nil :class :instance)))
172 (signal 'invalid-slot-type (list :allocation alloc)))
173
174 ;; Label is nil, or a string
175 (if (not (or (null label) (stringp label)))
176 (signal 'invalid-slot-type (list :label label)))
177
178 ;; Is there an initarg, but allocation of class?
179 (if (and initarg (eq alloc :class))
180 (message "Class allocated slots do not need :initarg"))
181
182 ;; Anyone can have an accessor function. This creates a function
183 ;; of the specified name, and also performs a `defsetf' if applicable
184 ;; so that users can `setf' the space returned by this function.
185 (when acces
59e7fe6d
SM
Stefan Monnier2015-01-21 14:39:06 -0500186 (push `(cl-defmethod (setf ,acces) (value (this ,name))
187 (eieio-oset this ',sname value))
6a67b20d Stefan Monnier2015-01-08 15:47:32 -0500188 accessors)
59e7fe6d Stefan Monnier2015-01-21 14:39:06 -0500189 (push `(cl-defmethod ,acces ((this ,name))
6a67b20d
SM
Stefan Monnier2015-01-08 15:47:32 -0500190 ,(format
191 "Retrieve the slot `%S' from an object of class `%S'."
192 sname name)
59e7fe6d
SM
Stefan Monnier2015-01-21 14:39:06 -0500193 ;; FIXME: Why is this different from the :reader case?
194 (if (slot-boundp this ',sname) (eieio-oref this ',sname)))
195 accessors)
196 (when (and eieio-backward-compatibility (eq alloc :class))
197 ;; FIXME: How could I declare this *method* as obsolete.
198 (push `(cl-defmethod ,acces ((this (subclass ,name)))
199 ,(format
200 "Retrieve the class slot `%S' from a class `%S'.
201This method is obsolete."
202 sname name)
203 (if (slot-boundp this ',sname)
204 (eieio-oref-default this ',sname)))
205 accessors)))
6a67b20d
SM
Stefan Monnier2015-01-08 15:47:32 -0500206
207 ;; If a writer is defined, then create a generic method of that
208 ;; name whose purpose is to set the value of the slot.
209 (if writer
59e7fe6d Stefan Monnier2015-01-21 14:39:06 -0500210 (push `(cl-defmethod ,writer ((this ,name) value)
6a67b20d
SM
Stefan Monnier2015-01-08 15:47:32 -0500211 ,(format "Set the slot `%S' of an object of class `%S'."
212 sname name)
213 (setf (slot-value this ',sname) value))
214 accessors))
215 ;; If a reader is defined, then create a generic method
216 ;; of that name whose purpose is to access this slot value.
217 (if reader
59e7fe6d Stefan Monnier2015-01-21 14:39:06 -0500218 (push `(cl-defmethod ,reader ((this ,name))
6a67b20d
SM
Stefan Monnier2015-01-08 15:47:32 -0500219 ,(format "Access the slot `%S' from object of class `%S'."
220 sname name)
221 (slot-value this ',sname))
222 accessors))
223 ))
224
225 `(progn
226 ;; This test must be created right away so we can have self-
227 ;; referencing classes. ei, a class whose slot can contain only
228 ;; pointers to itself.
229
e0be229d
SM
Stefan Monnier2015-01-31 00:48:14 -0500230 ;; Create the test functions.
231 (defalias ',testsym1 (eieio-make-class-predicate ',name))
232 (defalias ',testsym2 (eieio-make-child-predicate ',name))
6a67b20d
SM
Stefan Monnier2015-01-08 15:47:32 -0500233
234 ,@(when eieio-backward-compatibility
235 (let ((f (intern (format "%s-child-p" name))))
236 `((defalias ',f ',testsym2)
237 (make-obsolete
26bd978d
PE
Paul Eggert2015-09-03 15:31:12 -0700238 ',f ,(format "use (cl-typep ... \\='%s) instead" name)
239 "25.1"))))
6a67b20d
SM
Stefan Monnier2015-01-08 15:47:32 -0500240
241 ;; When using typep, (typep OBJ 'myclass) returns t for objects which
242 ;; are subclasses of myclass. For our predicates, however, it is
243 ;; important for EIEIO to be backwards compatible, where
244 ;; myobject-p, and myobject-child-p are different.
245 ;; "cl" uses this technique to specify symbols with specific typep
246 ;; test, so we can let typep have the CLOS documented behavior
247 ;; while keeping our above predicate clean.
248
249 (put ',name 'cl-deftype-satisfies #',testsym2)
250
251 (eieio-defclass-internal ',name ',superclasses ',slots ',options-and-doc)
252
253 ,@accessors
254
255 ;; Create the constructor function
256 ,(if (eieio--class-option-assoc options-and-doc :abstract)
257 ;; Abstract classes cannot be instantiated. Say so.
258 (let ((abs (eieio--class-option-assoc options-and-doc :abstract)))
259 (if (not (stringp abs))
260 (setq abs (format "Class %s is abstract" name)))
261 `(defun ,name (&rest _)
59b5723c Stefan Monnier2015-07-07 02:14:16 -0400262 ,(format "You cannot create a new object of type `%S'." name)
6a67b20d
SM
Stefan Monnier2015-01-08 15:47:32 -0500263 (error ,abs)))
264
265 ;; Non-abstract classes need a constructor.
266 `(defun ,name (&rest slots)
59b5723c Stefan Monnier2015-07-07 02:14:16 -0400267 ,(format "Create a new object of class type `%S'." name)
2a61bd00
SM
Stefan Monnier2015-01-18 12:24:43 -0500268 (declare (compiler-macro
269 (lambda (whole)
270 (if (not (stringp (car slots)))
271 whole
272 (macroexp--warn-and-return
273 (format "Obsolete name arg %S to constructor %S"
274 (car slots) (car whole))
275 ;; Keep the name arg, for backward compatibility,
276 ;; but hide it so we don't trigger indefinitely.
277 `(,(car whole) (identity ,(car slots))
278 ,@(cdr slots)))))))
c4e2be45 Stefan Monnier2015-02-16 02:22:46 -0500279 (apply #'make-instance ',name slots))))))
6dd12ef2 Chong Yidong2009-09-28 00:49:54 +0000280
6dd12ef2 Chong Yidong2009-09-28 00:49:54 +0000281
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +0000282;;; Get/Set slots in an object.
283;;
284(defmacro oref (obj slot)
285 "Retrieve the value stored in OBJ in the slot named by SLOT.
286Slot is the name of the slot when created by `defclass' or the label
287created by the :initarg tag."
d4a12e7a Stefan Monnier2014-12-22 15:46:16 -0500288 (declare (debug (form symbolp)))
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +0000289 `(eieio-oref ,obj (quote ,slot)))
290
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +0000291(defalias 'slot-value 'eieio-oref)
292(defalias 'set-slot-value 'eieio-oset)
c4e2be45 Stefan Monnier2015-02-16 02:22:46 -0500293(make-obsolete 'set-slot-value "use (setf (slot-value ..) ..) instead" "25.1")
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +0000294
295(defmacro oref-default (obj slot)
a8f316ca Juanma Barranquero2009-10-05 15:32:08 +0000296 "Get the default value of OBJ (maybe a class) for SLOT.
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +0000297The default value is the value installed in a class with the :initform
298tag. SLOT can be the slot name, or the tag specified by the :initarg
299tag in the `defclass' call."
d4a12e7a Stefan Monnier2014-12-22 15:46:16 -0500300 (declare (debug (form symbolp)))
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +0000301 `(eieio-oref-default ,obj (quote ,slot)))
302
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +0000303;;; Handy CLOS macros
304;;
305(defmacro with-slots (spec-list object &rest body)
306 "Bind SPEC-LIST lexically to slot values in OBJECT, and execute BODY.
307This establishes a lexical environment for referring to the slots in
308the instance named by the given slot-names as though they were
309variables. Within such a context the value of the slot can be
310specified by using its slot name, as if it were a lexically bound
311variable. Both setf and setq can be used to set the value of the
312slot.
313
314SPEC-LIST is of a form similar to `let'. For example:
315
316 ((VAR1 SLOT1)
317 SLOT2
318 SLOTN
319 (VARN+1 SLOTN+1))
320
321Where each VAR is the local variable given to the associated
a8f316ca Juanma Barranquero2009-10-05 15:32:08 +0000322SLOT. A slot specified without a variable name is given a
6dd12ef2 Chong Yidong2009-09-28 00:49:54 +0000323variable name of the same name as the slot."
d4a12e7a Stefan Monnier2014-12-22 15:46:16 -0500324 (declare (indent 2) (debug (sexp sexp def-body)))
94250173 Stefan Monnier2014-10-17 01:09:24 -0400325 (require 'cl-lib)
08dfa0b7 Jonas Bernoulli2014-03-12 16:02:45 +0100326 ;; Transform the spec-list into a cl-symbol-macrolet spec-list.
50c117fe
SM
Stefan Monnier2015-03-18 23:02:26 -0400327 (macroexp-let2 nil object object
328 `(cl-symbol-macrolet
329 ,(mapcar (lambda (entry)
330 (let ((var (if (listp entry) (car entry) entry))
331 (slot (if (listp entry) (cadr entry) entry)))
332 (list var `(slot-value ,object ',slot))))
333 spec-list)
334 ,@body)))
ae277259
SM
Stefan Monnier2015-03-23 18:24:30 -0400335
336;; Keep it as a non-inlined function, so the internals of object don't get
337;; hard-coded in random .elc files.
338(defun eieio-pcase-slot-index-table (obj)
339 "Return some data structure from which can be extracted the slot offset."
340 (eieio--class-index-table
341 (symbol-value (eieio--object-class-tag obj))))
342
343(defun eieio-pcase-slot-index-from-index-table (index-table slot)
344 "Find the index to pass to `aref' to access SLOT."
345 (let ((index (gethash slot index-table)))
346 (if index (+ (eval-when-compile
347 (length (cl-struct-slot-info 'eieio--object)))
348 index))))
349
350(pcase-defmacro eieio (&rest fields)
351 "Pcase patterns to match EIEIO objects.
352Elements of FIELDS can be of the form (NAME UPAT) in which case the contents of
353field NAME is matched against UPAT, or they can be of the form NAME which
354is a shorthand for (NAME NAME)."
2e47de36 Johan Bockgård2015-04-12 16:26:52 +0200355 (declare (debug (&rest [&or (sexp pcase-UPAT) sexp])))
ae277259
SM
Stefan Monnier2015-03-23 18:24:30 -0400356 (let ((is (make-symbol "table")))
357 ;; FIXME: This generates a horrendous mess of redundant let bindings.
358 ;; `pcase' needs to be improved somehow to introduce let-bindings more
359 ;; sparingly, or the byte-compiler needs to be taught to optimize
360 ;; them away.
361 ;; FIXME: `pcase' does not do a good job here of sharing tests&code among
362 ;; various branches.
363 `(and (pred eieio-object-p)
364 (app eieio-pcase-slot-index-table ,is)
365 ,@(mapcar (lambda (field)
366 (let* ((name (if (consp field) (car field) field))
367 (pat (if (consp field) (cadr field) field))
368 (i (make-symbol "index")))
369 `(and (let (and ,i (pred natnump))
370 (eieio-pcase-slot-index-from-index-table
371 ,is ',name))
372 (app (pcase--flip aref ,i) ,pat))))
373 fields))))
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +0000374\f
375;;; Simple generators, and query functions. None of these would do
376;; well embedded into an object.
377;;
50c117fe Stefan Monnier2015-03-18 23:02:26 -0400378
8ca4f1e0 Stefan Monnier2013-02-18 21:57:04 -0500379(define-obsolete-function-alias
50c117fe Stefan Monnier2015-03-18 23:02:26 -0400380 'object-class-fast #'eieio-object-class "24.4")
6dd12ef2 Chong Yidong2009-09-28 00:49:54 +0000381
59e7fe6d
SM
Stefan Monnier2015-01-21 14:39:06 -0500382(cl-defgeneric eieio-object-name-string (obj)
383 "Return a string which is OBJ's name."
384 (declare (obsolete eieio-named "25.1")))
385
8ca4f1e0 Stefan Monnier2013-02-18 21:57:04 -0500386(defun eieio-object-name (obj &optional extra)
c4e2be45 Stefan Monnier2015-02-16 02:22:46 -0500387 "Return a printed representation for object OBJ.
6dd12ef2 Chong Yidong2009-09-28 00:49:54 +0000388If EXTRA, include that in the string returned to represent the symbol."
e0be229d Stefan Monnier2015-01-31 00:48:14 -0500389 (cl-check-type obj eieio-object)
50c117fe Stefan Monnier2015-03-18 23:02:26 -0400390 (format "#<%s %s%s>" (eieio-object-class obj)
ee93d7ad Stefan Monnier2014-12-22 22:05:46 -0500391 (eieio-object-name-string obj) (or extra "")))
8ca4f1e0
SM
Stefan Monnier2013-02-18 21:57:04 -0500392(define-obsolete-function-alias 'object-name #'eieio-object-name "24.4")
393
ee93d7ad
SM
Stefan Monnier2014-12-22 22:05:46 -0500394(defconst eieio--object-names (make-hash-table :test #'eq :weakness 'key))
395
396;; In the past, every EIEIO object had a `name' field, so we had the two method
397;; below "for free". Since this field is very rarely used, we got rid of it
398;; and instead we keep it in a weak hash-tables, for those very rare objects
399;; that use it.
59e7fe6d Stefan Monnier2015-01-21 14:39:06 -0500400(cl-defmethod eieio-object-name-string (obj)
ee93d7ad
SM
Stefan Monnier2014-12-22 22:05:46 -0500401 (or (gethash obj eieio--object-names)
402 (symbol-name (eieio-object-class obj))))
8ca4f1e0
SM
Stefan Monnier2013-02-18 21:57:04 -0500403(define-obsolete-function-alias
404 'object-name-string #'eieio-object-name-string "24.4")
405
59e7fe6d Stefan Monnier2015-01-21 14:39:06 -0500406(cl-defmethod eieio-object-set-name-string (obj name)
8ca4f1e0 Stefan Monnier2013-02-18 21:57:04 -0500407 "Set the string which is OBJ's NAME."
ee93d7ad Stefan Monnier2014-12-22 22:05:46 -0500408 (declare (obsolete eieio-named "25.1"))
e0be229d Stefan Monnier2015-01-31 00:48:14 -0500409 (cl-check-type name string)
ee93d7ad Stefan Monnier2014-12-22 22:05:46 -0500410 (setf (gethash obj eieio--object-names) name))
8ca4f1e0
SM
Stefan Monnier2013-02-18 21:57:04 -0500411(define-obsolete-function-alias
412 'object-set-name-string 'eieio-object-set-name-string "24.4")
413
232823a1
SM
Stefan Monnier2014-12-29 12:11:09 -0500414(defun eieio-object-class (obj)
415 "Return the class struct defining OBJ."
416 ;; FIXME: We say we return a "struct" but we return a symbol instead!
e0be229d Stefan Monnier2015-01-31 00:48:14 -0500417 (cl-check-type obj eieio-object)
50c117fe Stefan Monnier2015-03-18 23:02:26 -0400418 (eieio--class-name (eieio--object-class obj)))
8ca4f1e0
SM
Stefan Monnier2013-02-18 21:57:04 -0500419(define-obsolete-function-alias 'object-class #'eieio-object-class "24.4")
420;; CLOS name, maybe?
421(define-obsolete-function-alias 'class-of #'eieio-object-class "24.4")
422
423(defun eieio-object-class-name (obj)
424 "Return a Lisp like symbol name for OBJ's class."
e0be229d Stefan Monnier2015-01-31 00:48:14 -0500425 (cl-check-type obj eieio-object)
50c117fe Stefan Monnier2015-03-18 23:02:26 -0400426 (eieio-class-name (eieio--object-class obj)))
8ca4f1e0
SM
Stefan Monnier2013-02-18 21:57:04 -0500427(define-obsolete-function-alias
428 'object-class-name 'eieio-object-class-name "24.4")
429
8ca4f1e0 Stefan Monnier2013-02-18 21:57:04 -0500430(defun eieio-class-parents (class)
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +0000431 "Return parent classes to CLASS. (overload of variable).
432
433The CLOS function `class-direct-superclasses' is aliased to this function."
50c117fe Stefan Monnier2015-03-18 23:02:26 -0400434 (eieio--class-parents (eieio--class-object class)))
cb4db863 Stefan Monnier2015-01-04 23:11:37 -0500435
8ca4f1e0 Stefan Monnier2013-02-18 21:57:04 -0500436(define-obsolete-function-alias 'class-parents #'eieio-class-parents "24.4")
6dd12ef2 Chong Yidong2009-09-28 00:49:54 +0000437
8ca4f1e0
SM
Stefan Monnier2013-02-18 21:57:04 -0500438(defun eieio-class-children (class)
439 "Return child classes to CLASS.
6dd12ef2 Chong Yidong2009-09-28 00:49:54 +0000440The CLOS function `class-direct-subclasses' is aliased to this function."
e0be229d Stefan Monnier2015-01-31 00:48:14 -0500441 (cl-check-type class class)
a0010db4 Stefan Monnier2015-07-02 10:59:32 -0400442 (eieio--class-children (cl--find-class class)))
8ca4f1e0
SM
Stefan Monnier2013-02-18 21:57:04 -0500443(define-obsolete-function-alias
444 'class-children #'eieio-class-children "24.4")
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +0000445
446;; Official CLOS functions.
8ca4f1e0
SM
Stefan Monnier2013-02-18 21:57:04 -0500447(define-obsolete-function-alias
448 'class-direct-superclasses #'eieio-class-parents "24.4")
449(define-obsolete-function-alias
450 'class-direct-subclasses #'eieio-class-children "24.4")
6dd12ef2 Chong Yidong2009-09-28 00:49:54 +0000451
8ca4f1e0
SM
Stefan Monnier2013-02-18 21:57:04 -0500452(defmacro eieio-class-parent (class)
453 "Return first parent class to CLASS. (overload of variable)."
454 `(car (eieio-class-parents ,class)))
9a0289a2 Glenn Morris2013-09-19 16:11:48 -0400455(define-obsolete-function-alias 'class-parent 'eieio-class-parent "24.4")
6dd12ef2 Chong Yidong2009-09-28 00:49:54 +0000456
1599688e
SM
Stefan Monnier2015-01-07 23:11:58 -0500457(defun same-class-p (obj class)
458 "Return t if OBJ is of class-type CLASS."
459 (setq class (eieio--class-object class))
e0be229d
SM
Stefan Monnier2015-01-31 00:48:14 -0500460 (cl-check-type class eieio--class)
461 (cl-check-type obj eieio-object)
50c117fe Stefan Monnier2015-03-18 23:02:26 -0400462 (eq (eieio--object-class obj) class))
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +0000463
464(defun object-of-class-p (obj class)
465 "Return non-nil if OBJ is an instance of CLASS or CLASS' subclasses."
e0be229d Stefan Monnier2015-01-31 00:48:14 -0500466 (cl-check-type obj eieio-object)
6dd12ef2 Chong Yidong2009-09-28 00:49:54 +0000467 ;; class will be checked one layer down
50c117fe Stefan Monnier2015-03-18 23:02:26 -0400468 (child-of-class-p (eieio--object-class obj) class))
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +0000469;; Backwards compatibility
470(defalias 'obj-of-class-p 'object-of-class-p)
471
472(defun child-of-class-p (child class)
a8f316ca Juanma Barranquero2009-10-05 15:32:08 +0000473 "Return non-nil if CHILD class is a subclass of CLASS."
232823a1 Stefan Monnier2014-12-29 12:11:09 -0500474 (setq child (eieio--class-object child))
e0be229d Stefan Monnier2015-01-31 00:48:14 -0500475 (cl-check-type child eieio--class)
50c117fe Stefan Monnier2015-03-18 23:02:26 -0400476 ;; `eieio-default-superclass' is never mentioned in eieio--class-parents,
bcebc831
SM
Stefan Monnier2014-12-22 15:13:02 -0500477 ;; so we have to special case it here.
478 (or (eq class 'eieio-default-superclass)
479 (let ((p nil))
232823a1 Stefan Monnier2014-12-29 12:11:09 -0500480 (setq class (eieio--class-object class))
e0be229d Stefan Monnier2015-01-31 00:48:14 -0500481 (cl-check-type class eieio--class)
bcebc831 Stefan Monnier2014-12-22 15:13:02 -0500482 (while (and child (not (eq child class)))
50c117fe Stefan Monnier2015-03-18 23:02:26 -0400483 (setq p (append p (eieio--class-parents child))
cb4db863 Stefan Monnier2015-01-04 23:11:37 -0500484 child (pop p)))
bcebc831 Stefan Monnier2014-12-22 15:13:02 -0500485 (if child t))))
6dd12ef2 Chong Yidong2009-09-28 00:49:54 +0000486
50c117fe
SM
Stefan Monnier2015-03-18 23:02:26 -0400487(defun eieio-slot-descriptor-name (slot)
488 (cl--slot-descriptor-name slot))
c4e2be45
SM
Stefan Monnier2015-02-16 02:22:46 -0500489
490(defun eieio-class-slots (class)
491 "Return list of slots available in instances of CLASS."
492 ;; FIXME: This only gives the instance slots and ignores the
493 ;; class-allocated slots.
c4e2be45
SM
Stefan Monnier2015-02-16 02:22:46 -0500494 (setq class (eieio--class-object class))
495 (cl-check-type class eieio--class)
50c117fe Stefan Monnier2015-03-18 23:02:26 -0400496 (mapcar #'identity (eieio--class-slots class)))
c4e2be45 Stefan Monnier2015-02-16 02:22:46 -0500497
a2930e43 Eric M. Ludlam2010-09-19 00:23:57 -0400498(defun object-slots (obj)
29f7f98b Stefan Monnier2015-03-19 10:35:52 -0400499 "Return list of slot names available in OBJ."
c4e2be45 Stefan Monnier2015-02-16 02:22:46 -0500500 (declare (obsolete eieio-class-slots "25.1"))
e0be229d Stefan Monnier2015-01-31 00:48:14 -0500501 (cl-check-type obj eieio-object)
29f7f98b
SM
Stefan Monnier2015-03-19 10:35:52 -0400502 (mapcar #'cl--slot-descriptor-name
503 (eieio-class-slots (eieio--object-class obj))))
6dd12ef2 Chong Yidong2009-09-28 00:49:54 +0000504
50c117fe
SM
Stefan Monnier2015-03-18 23:02:26 -0400505(defun eieio--class-slot-initarg (class slot)
506 "Fetch from CLASS, SLOT's :initarg."
e0be229d Stefan Monnier2015-01-31 00:48:14 -0500507 (cl-check-type class eieio--class)
cb4db863 Stefan Monnier2015-01-04 23:11:37 -0500508 (let ((ia (eieio--class-initarg-tuples class))
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +0000509 (f nil))
510 (while (and ia (not f))
511 (if (eq (cdr (car ia)) slot)
512 (setq f (car (car ia))))
513 (setq ia (cdr ia)))
514 f))
515
9760c73c
SM
Stefan Monnier2012-10-13 21:42:48 -0400516;;; Object Set macros
517;;
518(defmacro oset (obj slot value)
519 "Set the value in OBJ for slot SLOT to VALUE.
520SLOT is the slot name as specified in `defclass' or the tag created
521with in the :initarg slot. VALUE can be any Lisp object."
d4a12e7a Stefan Monnier2014-12-22 15:46:16 -0500522 (declare (debug (form symbolp form)))
9760c73c
SM
Stefan Monnier2012-10-13 21:42:48 -0400523 `(eieio-oset ,obj (quote ,slot) ,value))
524
9760c73c
SM
Stefan Monnier2012-10-13 21:42:48 -0400525(defmacro oset-default (class slot value)
526 "Set the default slot in CLASS for SLOT to VALUE.
527The default value is usually set with the :initform tag during class
528creation. This allows users to change the default behavior of classes
529after they are created."
d4a12e7a Stefan Monnier2014-12-22 15:46:16 -0500530 (declare (debug (form symbolp form)))
9760c73c
SM
Stefan Monnier2012-10-13 21:42:48 -0400531 `(eieio-oset-default ,class (quote ,slot) ,value))
532
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +0000533;;; CLOS queries into classes and slots
534;;
535(defun slot-boundp (object slot)
a8f316ca Juanma Barranquero2009-10-05 15:32:08 +0000536 "Return non-nil if OBJECT's SLOT is bound.
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +0000537Setting a slot's value makes it bound. Calling `slot-makeunbound' will
538make a slot unbound.
539OBJECT can be an instance or a class."
540 ;; Skip typechecking while retrieving this value.
541 (let ((eieio-skip-typecheck t))
542 ;; Return nil if the magic symbol is in there.
9869b3ae
SM
Stefan Monnier2011-05-05 00:42:09 -0300543 (not (eq (cond
544 ((eieio-object-p object) (eieio-oref object slot))
e0be229d Stefan Monnier2015-01-31 00:48:14 -0500545 ((symbolp object) (eieio-oref-default object slot))
9869b3ae
SM
Stefan Monnier2011-05-05 00:42:09 -0300546 (t (signal 'wrong-type-argument (list 'eieio-object-p object))))
547 eieio-unbound))))
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +0000548
549(defun slot-makeunbound (object slot)
550 "In OBJECT, make SLOT unbound."
551 (eieio-oset object slot eieio-unbound))
552
553(defun slot-exists-p (object-or-class slot)
a8f316ca Juanma Barranquero2009-10-05 15:32:08 +0000554 "Return non-nil if OBJECT-OR-CLASS has SLOT."
cb4db863 Stefan Monnier2015-01-04 23:11:37 -0500555 (let ((cv (cond ((eieio-object-p object-or-class)
50c117fe Stefan Monnier2015-03-18 23:02:26 -0400556 (eieio--object-class object-or-class))
e0be229d
SM
Stefan Monnier2015-01-31 00:48:14 -0500557 ((eieio--class-p object-or-class) object-or-class)
558 (t (find-class object-or-class 'error)))))
50c117fe
SM
Stefan Monnier2015-03-18 23:02:26 -0400559 (or (gethash slot (eieio--class-index-table cv))
560 ;; FIXME: We could speed this up by adding class slots into the
561 ;; index-table (e.g. with a negative index?).
562 (let ((cs (eieio--class-class-slots cv))
563 found)
564 (dotimes (i (length cs))
565 (if (eq slot (cl--slot-descriptor-name (aref cs i)))
566 (setq found t)))
567 found))))
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +0000568
569(defun find-class (symbol &optional errorp)
570 "Return the class that SYMBOL represents.
571If there is no class, nil is returned if ERRORP is nil.
572If ERRORP is non-nil, `wrong-argument-type' is signaled."
a0010db4 Stefan Monnier2015-07-02 10:59:32 -0400573 (let ((class (cl--find-class symbol)))
e0be229d
SM
Stefan Monnier2015-01-31 00:48:14 -0500574 (cond
575 ((eieio--class-p class) class)
576 (errorp (signal 'wrong-type-argument (list 'class-p symbol))))))
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +0000577
578;;; Slightly more complex utility functions for objects
579;;
580(defun object-assoc (key slot list)
581 "Return an object if KEY is `equal' to SLOT's value of an object in LIST.
a8f316ca Juanma Barranquero2009-10-05 15:32:08 +0000582LIST is a list of objects whose slots are searched.
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +0000583Objects in LIST do not need to have a slot named SLOT, nor does
584SLOT need to be bound. If these errors occur, those objects will
585be ignored."
e0be229d Stefan Monnier2015-01-31 00:48:14 -0500586 (cl-check-type list list)
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +0000587 (while (and list (not (condition-case nil
588 ;; This prevents errors for missing slots.
589 (equal key (eieio-oref (car list) slot))
590 (error nil))))
591 (setq list (cdr list)))
592 (car list))
593
594(defun object-assoc-list (slot list)
595 "Return an association list with the contents of SLOT as the key element.
596LIST must be a list of objects with SLOT in it.
597This is useful when you need to do completing read on an object group."
e0be229d Stefan Monnier2015-01-31 00:48:14 -0500598 (cl-check-type list list)
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +0000599 (let ((assoclist nil))
600 (while list
601 (setq assoclist (cons (cons (eieio-oref (car list) slot)
602 (car list))
603 assoclist))
604 (setq list (cdr list)))
605 (nreverse assoclist)))
606
607(defun object-assoc-list-safe (slot list)
608 "Return an association list with the contents of SLOT as the key element.
609LIST must be a list of objects, but those objects do not need to have
610SLOT in it. If it does not, then that element is left out of the association
611list."
e0be229d Stefan Monnier2015-01-31 00:48:14 -0500612 (cl-check-type list list)
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +0000613 (let ((assoclist nil))
614 (while list
615 (if (slot-exists-p (car list) slot)
616 (setq assoclist (cons (cons (eieio-oref (car list) slot)
617 (car list))
618 assoclist)))
619 (setq list (cdr list)))
620 (nreverse assoclist)))
621
622(defun object-add-to-list (object slot item &optional append)
623 "In OBJECT's SLOT, add ITEM to the list of elements.
624Optional argument APPEND indicates we need to append to the list.
625If ITEM already exists in the list in SLOT, then it is not added.
626Comparison is done with `equal' through the `member' function call.
627If SLOT is unbound, bind it to the list containing ITEM."
628 (let (ov)
629 ;; Find the originating list.
630 (if (not (slot-boundp object slot))
631 (setq ov (list item))
632 (setq ov (eieio-oref object slot))
633 ;; turn it into a list.
634 (unless (listp ov)
635 (setq ov (list ov)))
636 ;; Do the combination
637 (if (not (member item ov))
638 (setq ov
639 (if append
640 (append ov (list item))
641 (cons item ov)))))
642 ;; Set back into the slot.
643 (eieio-oset object slot ov)))
644
645(defun object-remove-from-list (object slot item)
646 "In OBJECT's SLOT, remove occurrences of ITEM.
a8f316ca Juanma Barranquero2009-10-05 15:32:08 +0000647Deletion is done with `delete', which deletes by side effect,
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +0000648and comparisons are done with `equal'.
649If SLOT is unbound, do nothing."
650 (if (not (slot-boundp object slot))
651 nil
652 (eieio-oset object slot (delete item (eieio-oref object slot)))))
6dd12ef2 Chong Yidong2009-09-28 00:49:54 +0000653
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +0000654;;; Here are some CLOS items that need the CL package
655;;
656
c4e2be45
SM
Stefan Monnier2015-02-16 02:22:46 -0500657;; FIXME: Shouldn't this be a more complex gv-expander which extracts the
658;; common code between oref and oset, so as to reduce the redundant work done
659;; in (push foo (oref bar baz)), like we do for the `nth' expander?
94250173 Stefan Monnier2014-10-17 01:09:24 -0400660(gv-define-simple-setter eieio-oref eieio-oset)
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +0000661
662\f
663;;;
664;; We want all objects created by EIEIO to have some default set of
91af3942 Paul Eggert2011-11-14 15:59:56 -0800665;; behaviors so we can create object utilities, and allow various
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +0000666;; types of error checking. To do this, create the default EIEIO
667;; class, and when no parent class is specified, use this as the
668;; default. (But don't store it in the other classes as the default,
669;; allowing for transparent support.)
670;;
671
672(defclass eieio-default-superclass nil
673 nil
674 "Default parent class for classes with no specified parent class.
a8f316ca
JB
Juanma Barranquero2009-10-05 15:32:08 +0000675Its slots are automatically adopted by classes with no specified parents.
676This class is not stored in the `parent' slot of a class vector."
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +0000677 :abstract t)
678
a0010db4 Stefan Monnier2015-07-02 10:59:32 -0400679(setq eieio-default-superclass (cl--find-class 'eieio-default-superclass))
cb4db863 Stefan Monnier2015-01-04 23:11:37 -0500680
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +0000681(defalias 'standard-class 'eieio-default-superclass)
682
c4e2be45
SM
Stefan Monnier2015-02-16 02:22:46 -0500683(cl-defgeneric make-instance (class &rest initargs)
684 "Make a new instance of CLASS based on INITARGS.
685For example:
686
19532d14 Paul Eggert2015-09-01 18:21:42 -0700687 (make-instance \\='foo)
c4e2be45
SM
Stefan Monnier2015-02-16 02:22:46 -0500688
689INITARGS is a property list with keywords based on the `:initarg'
690for each slot. For example:
691
19532d14 Paul Eggert2015-09-01 18:21:42 -0700692 (make-instance \\='foo :slot1 value1 :slotN valueN)")
6dd12ef2 Chong Yidong2009-09-28 00:49:54 +0000693
c4e2be45 Stefan Monnier2015-02-16 02:22:46 -0500694(define-obsolete-function-alias 'constructor #'make-instance "25.1")
ee93d7ad Stefan Monnier2014-12-22 22:05:46 -0500695
c4e2be45
SM
Stefan Monnier2015-02-16 02:22:46 -0500696(cl-defmethod make-instance
697 ((class (subclass eieio-default-superclass)) &rest slots)
a8f316ca Juanma Barranquero2009-10-05 15:32:08 +0000698 "Default constructor for CLASS `eieio-default-superclass'.
c4e2be45 Stefan Monnier2015-02-16 02:22:46 -0500699SLOTS are the initialization slots used by `initialize-instance'.
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +0000700This static method is called when an object is constructed.
701It allocates the vector used to represent an EIEIO object, and then
c4e2be45 Stefan Monnier2015-02-16 02:22:46 -0500702calls `initialize-instance' on that object."
4610ce96 Stefan Monnier2015-01-17 09:41:51 -0500703 (let* ((new-object (copy-sequence (eieio--class-default-object-cache
c4e2be45 Stefan Monnier2015-02-16 02:22:46 -0500704 (eieio--class-object class)))))
4610ce96
SM
Stefan Monnier2015-01-17 09:41:51 -0500705 (if (and slots
706 (let ((x (car slots)))
707 (or (stringp x) (null x))))
708 (funcall (if eieio-backward-compatibility #'ignore #'message)
709 "Obsolete name %S passed to %S constructor"
710 (pop slots) class))
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +0000711 ;; Call the initialize method on the new object with the slots
712 ;; that were passed down to us.
713 (initialize-instance new-object slots)
714 ;; Return the created object.
715 new-object))
716
c4e2be45 Stefan Monnier2015-02-16 02:22:46 -0500717;; FIXME: CLOS uses "&rest INITARGS" instead.
59e7fe6d Stefan Monnier2015-01-21 14:39:06 -0500718(cl-defgeneric shared-initialize (obj 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.")
721
59e7fe6d Stefan Monnier2015-01-21 14:39:06 -0500722(cl-defmethod shared-initialize ((obj eieio-default-superclass) slots)
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +0000723 "Set slots of OBJ with SLOTS which is a list of name/value pairs.
724Called from the constructor routine."
d48c98cd Stefan Monnier2015-01-16 23:48:26 -0500725 (while slots
50c117fe Stefan Monnier2015-03-18 23:02:26 -0400726 (let ((rn (eieio--initarg-to-attribute (eieio--object-class obj)
d48c98cd
SM
Stefan Monnier2015-01-16 23:48:26 -0500727 (car slots))))
728 (if (not rn)
729 (slot-missing obj (car slots) 'oset (car (cdr slots)))
730 (eieio-oset obj rn (car (cdr slots)))))
731 (setq slots (cdr (cdr slots)))))
6dd12ef2 Chong Yidong2009-09-28 00:49:54 +0000732
c4e2be45 Stefan Monnier2015-02-16 02:22:46 -0500733;; FIXME: CLOS uses "&rest INITARGS" instead.
59e7fe6d Stefan Monnier2015-01-21 14:39:06 -0500734(cl-defgeneric initialize-instance (this &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 +0000736
59e7fe6d Stefan Monnier2015-01-21 14:39:06 -0500737(cl-defmethod initialize-instance ((this eieio-default-superclass)
6dd12ef2 Chong Yidong2009-09-28 00:49:54 +0000738 &optional slots)
a8f316ca Juanma Barranquero2009-10-05 15:32:08 +0000739 "Construct the new object THIS based on SLOTS.
6dd12ef2 Chong Yidong2009-09-28 00:49:54 +0000740SLOTS is a tagged list where odd numbered elements are tags, and
a8f316ca
JB
Juanma Barranquero2009-10-05 15:32:08 +0000741even numbered elements are the values to store in the tagged slot.
742If you overload the `initialize-instance', there you will need to
743call `shared-initialize' yourself, or you can call `call-next-method'
744to have this constructor called automatically. If these steps are
745not taken, then new objects of your class will not have their values
6dd12ef2 Chong Yidong2009-09-28 00:49:54 +0000746dynamically set from SLOTS."
890f7890
DE
David Engster2013-06-02 15:33:09 +0200747 ;; First, see if any of our defaults are `lambda', and
748 ;; re-evaluate them and apply the value to our slots.
50c117fe
SM
Stefan Monnier2015-03-18 23:02:26 -0400749 (let* ((this-class (eieio--object-class this))
750 (slots (eieio--class-slots this-class)))
751 (dotimes (i (length slots))
890f7890
DE
David Engster2013-06-02 15:33:09 +0200752 ;; For each slot, see if we need to evaluate it.
753 ;;
754 ;; Paul Landes said in an email:
755 ;; > CL evaluates it if it can, and otherwise, leaves it as
756 ;; > the quoted thing as you already have. This is by the
757 ;; > Sonya E. Keene book and other things I've look at on the
758 ;; > web.
50c117fe
SM
Stefan Monnier2015-03-18 23:02:26 -0400759 (let* ((slot (aref slots i))
760 (initform (cl--slot-descriptor-initform slot))
761 (dflt (eieio-default-eval-maybe initform)))
762 (when (not (eq dflt initform))
763 ;; FIXME: We should be able to just do (aset this (+ i <cst>) dflt)!
764 (eieio-oset this (cl--slot-descriptor-name slot) dflt)))))
890f7890
DE
David Engster2013-06-02 15:33:09 +0200765 ;; Shared initialize will parse our slots for us.
766 (shared-initialize this slots))
6dd12ef2 Chong Yidong2009-09-28 00:49:54 +0000767
59e7fe6d Stefan Monnier2015-01-21 14:39:06 -0500768(cl-defgeneric slot-missing (object slot-name 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.")
770
59e7fe6d Stefan Monnier2015-01-21 14:39:06 -0500771(cl-defmethod slot-missing ((object eieio-default-superclass) slot-name
94250173 Stefan Monnier2014-10-17 01:09:24 -0400772 _operation &optional _new-value)
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +0000773 "Method invoked when an attempt to access a slot in OBJECT fails.
774SLOT-NAME is the name of the failed slot, OPERATION is the type of access
775that was requested, and optional NEW-VALUE is the value that was desired
776to be set.
777
778This method is called from `oref', `oset', and other functions which
779directly reference slots in EIEIO objects."
8ca4f1e0 Stefan Monnier2013-02-18 21:57:04 -0500780 (signal 'invalid-slot-name (list (eieio-object-name object)
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +0000781 slot-name)))
782
59e7fe6d Stefan Monnier2015-01-21 14:39:06 -0500783(cl-defgeneric slot-unbound (object class slot-name fn)
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +0000784 "Slot unbound is invoked during an attempt to reference an unbound slot.")
785
59e7fe6d Stefan Monnier2015-01-21 14:39:06 -0500786(cl-defmethod slot-unbound ((object eieio-default-superclass)
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +0000787 class slot-name fn)
788 "Slot unbound is invoked during an attempt to reference an unbound slot.
789OBJECT is the instance of the object being reference. CLASS is the
790class of OBJECT, and SLOT-NAME is the offending slot. This function
791throws the signal `unbound-slot'. You can overload this function and
792return the value to use in place of the unbound value.
793Argument FN is the function signaling this error.
794Use `slot-boundp' to determine if a slot is bound or not.
795
796In CLOS, the argument list is (CLASS OBJECT SLOT-NAME), but
797EIEIO can only dispatch on the first argument, so the first two are swapped."
c4e2be45
SM
Stefan Monnier2015-02-16 02:22:46 -0500798 (signal 'unbound-slot (list (eieio-class-name class)
799 (eieio-object-name object)
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +0000800 slot-name fn)))
801
59e7fe6d Stefan Monnier2015-01-21 14:39:06 -0500802(cl-defgeneric clone (obj &rest params)
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +0000803 "Make a copy of OBJ, and then supply PARAMS.
804PARAMS is a parameter list of the same form used by `initialize-instance'.
805
806When overloading `clone', be sure to call `call-next-method'
807first and modify the returned object.")
808
59e7fe6d Stefan Monnier2015-01-21 14:39:06 -0500809(cl-defmethod clone ((obj eieio-default-superclass) &rest params)
6dd12ef2 Chong Yidong2009-09-28 00:49:54 +0000810 "Make a copy of OBJ, and then apply PARAMS."
ee93d7ad
SM
Stefan Monnier2014-12-22 22:05:46 -0500811 (let ((nobj (copy-sequence obj)))
812 (if (stringp (car params))
1599688e
SM
Stefan Monnier2015-01-07 23:11:58 -0500813 (funcall (if eieio-backward-compatibility #'ignore #'message)
814 "Obsolete name %S passed to clone" (pop params)))
ee93d7ad Stefan Monnier2014-12-22 22:05:46 -0500815 (if params (shared-initialize nobj params))
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +0000816 nobj))
817
59e7fe6d Stefan Monnier2015-01-21 14:39:06 -0500818(cl-defgeneric destructor (this &rest params)
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +0000819 "Destructor for cleaning up any dynamic links to our object.")
820
59e7fe6d Stefan Monnier2015-01-21 14:39:06 -0500821(cl-defmethod destructor ((_this eieio-default-superclass) &rest _params)
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +0000822 "Destructor for cleaning up any dynamic links to our object.
823Argument THIS is the object being destroyed. PARAMS are additional
824ignored parameters."
825 ;; No cleanup... yet.
826 )
827
59e7fe6d Stefan Monnier2015-01-21 14:39:06 -0500828(cl-defgeneric object-print (this &rest strings)
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +0000829 "Pretty printer for object THIS. Call function `object-name' with STRINGS.
830
831It is sometimes useful to put a summary of the object into the
a8f316ca Juanma Barranquero2009-10-05 15:32:08 +0000832default #<notation> string when using EIEIO browsing tools.
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +0000833Implement this method to customize the summary.")
834
59e7fe6d Stefan Monnier2015-01-21 14:39:06 -0500835(cl-defmethod object-print ((this eieio-default-superclass) &rest strings)
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +0000836 "Pretty printer for object THIS. Call function `object-name' with STRINGS.
837The default method for printing object THIS is to use the
838function `object-name'.
839
840It is sometimes useful to put a summary of the object into the
a8f316ca Juanma Barranquero2009-10-05 15:32:08 +0000841default #<notation> string when using EIEIO browsing tools.
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +0000842
843Implement this function and specify STRINGS in a call to
844`call-next-method' to provide additional summary information.
845When passing in extra strings from child classes, always remember
846to prepend a space."
94250173 Stefan Monnier2014-10-17 01:09:24 -0400847 (eieio-object-name this (apply #'concat strings)))
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +0000848
849(defvar eieio-print-depth 0
850 "When printing, keep track of the current indentation depth.")
851
59e7fe6d Stefan Monnier2015-01-21 14:39:06 -0500852(cl-defgeneric object-write (this &optional comment)
6dd12ef2 Chong Yidong2009-09-28 00:49:54 +0000853 "Write out object THIS to the current stream.
a8f316ca Juanma Barranquero2009-10-05 15:32:08 +0000854Optional COMMENT will add comments to the beginning of the output.")
6dd12ef2 Chong Yidong2009-09-28 00:49:54 +0000855
59e7fe6d Stefan Monnier2015-01-21 14:39:06 -0500856(cl-defmethod object-write ((this eieio-default-superclass) &optional comment)
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +0000857 "Write object THIS out to the current stream.
858This writes out the vector version of this object. Complex and recursive
859object are discouraged from being written.
860 If optional COMMENT is non-nil, include comments when outputting
861this object."
862 (when comment
863 (princ ";; Object ")
8ca4f1e0 Stefan Monnier2013-02-18 21:57:04 -0500864 (princ (eieio-object-name-string this))
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +0000865 (princ "\n")
866 (princ comment)
867 (princ "\n"))
8ca4f1e0 Stefan Monnier2013-02-18 21:57:04 -0500868 (let* ((cl (eieio-object-class this))
a0010db4 Stefan Monnier2015-07-02 10:59:32 -0400869 (cv (cl--find-class cl)))
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +0000870 ;; Now output readable lisp to recreate this object
871 ;; It should look like this:
872 ;; (<constructor> <name> <slot> <slot> ... )
873 ;; Each slot's slot is writen using its :writer.
874 (princ (make-string (* eieio-print-depth 2) ? ))
875 (princ "(")
ca80ebc7 Stefan Monnier2015-01-17 09:50:07 -0500876 (princ (symbol-name (eieio--class-constructor (eieio-object-class this))))
62a81506 Chong Yidong2012-10-02 02:10:29 +0800877 (princ " ")
8ca4f1e0 Stefan Monnier2013-02-18 21:57:04 -0500878 (prin1 (eieio-object-name-string this))
62a81506 Chong Yidong2012-10-02 02:10:29 +0800879 (princ "\n")
6dd12ef2 Chong Yidong2009-09-28 00:49:54 +0000880 ;; Loop over all the public slots
50c117fe Stefan Monnier2015-03-18 23:02:26 -0400881 (let ((slots (eieio--class-slots cv))
6dd12ef2 Chong Yidong2009-09-28 00:49:54 +0000882 (eieio-print-depth (1+ eieio-print-depth)))
50c117fe
SM
Stefan Monnier2015-03-18 23:02:26 -0400883 (dotimes (i (length slots))
884 (let ((slot (aref slots i)))
885 (when (slot-boundp this (cl--slot-descriptor-name slot))
886 (let ((i (eieio--class-slot-initarg
887 cv (cl--slot-descriptor-name slot)))
888 (v (eieio-oref this (cl--slot-descriptor-name slot))))
889 (unless (or (not i) (equal v (cl--slot-descriptor-initform slot)))
890 (unless (bolp)
891 (princ "\n"))
892 (princ (make-string (* eieio-print-depth 2) ? ))
893 (princ (symbol-name i))
894 (if (alist-get :printer (cl--slot-descriptor-props slot))
895 ;; Use our public printer
896 (progn
897 (princ " ")
898 (funcall (alist-get :printer
899 (cl--slot-descriptor-props slot))
900 v))
901 ;; Use our generic override prin1 function.
902 (princ (if (or (eieio-object-p v)
903 (eieio-object-p (car-safe v)))
904 "\n" " "))
905 (eieio-override-prin1 v))))))))
69e1c203
JB
Jonas Bernoulli2012-12-11 21:16:05 -0500906 (princ ")")
907 (when (= eieio-print-depth 0)
908 (princ "\n"))))
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +0000909
910(defun eieio-override-prin1 (thing)
a8f316ca Juanma Barranquero2009-10-05 15:32:08 +0000911 "Perform a `prin1' on THING taking advantage of object knowledge."
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +0000912 (cond ((eieio-object-p thing)
913 (object-write thing))
69e1c203 Jonas Bernoulli2012-12-11 21:16:05 -0500914 ((consp thing)
6dd12ef2 Chong Yidong2009-09-28 00:49:54 +0000915 (eieio-list-prin1 thing))
e0be229d Stefan Monnier2015-01-31 00:48:14 -0500916 ((eieio--class-p thing)
c4e2be45 Stefan Monnier2015-02-16 02:22:46 -0500917 (princ (eieio--class-print-name thing)))
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +0000918 (t (prin1 thing))))
919
920(defun eieio-list-prin1 (list)
921 "Display LIST where list may contain objects."
922 (if (not (eieio-object-p (car list)))
923 (progn
924 (princ "'")
925 (prin1 list))
6dd12ef2 Chong Yidong2009-09-28 00:49:54 +0000926 (princ (make-string (* eieio-print-depth 2) ? ))
69e1c203
JB
Jonas Bernoulli2012-12-11 21:16:05 -0500927 (princ "(list")
928 (let ((eieio-print-depth (1+ eieio-print-depth)))
929 (while list
930 (princ "\n")
931 (if (eieio-object-p (car list))
932 (object-write (car list))
933 (princ (make-string (* eieio-print-depth 2) ? ))
934 (eieio-override-prin1 (car list)))
935 (setq list (cdr list))))
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +0000936 (princ ")")))
937
938\f
939;;; Unimplemented functions from CLOS
940;;
94250173 Stefan Monnier2014-10-17 01:09:24 -0400941(defun change-class (_obj _class)
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +0000942 "Change the class of OBJ to type CLASS.
943This may create or delete slots, but does not affect the return value
944of `eq'."
c87523bd Paul Eggert2015-08-21 20:19:46 -0700945 (error "EIEIO: ‘change-class’ is unimplemented"))
6dd12ef2 Chong Yidong2009-09-28 00:49:54 +0000946
0f918d96 David Engster2014-01-08 22:30:12 +0100947;; Hook ourselves into help system for describing classes and methods.
59b5723c
SM
Stefan Monnier2015-07-07 02:14:16 -0400948;; FIXME: This is not actually needed any more since we can click on the
949;; hyperlink from the constructor's docstring to see the type definition.
0f918d96
DE
David Engster2014-01-08 22:30:12 +0100950(add-hook 'help-fns-describe-function-functions 'eieio-help-constructor)
951
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +0000952;;; Interfacing with edebug
953;;
fce9e553 Eric Abrahamsen2014-11-02 17:24:33 +0200954(defun eieio-edebug-prin1-to-string (print-function object &optional noescape)
a8f316ca Juanma Barranquero2009-10-05 15:32:08 +0000955 "Display EIEIO OBJECT in fancy format.
fce9e553
EA
Eric Abrahamsen2014-11-02 17:24:33 +0200956
957Used as advice around `edebug-prin1-to-string', held in the
958variable PRINT-FUNCTION. Optional argument NOESCAPE is passed to
959`prin1-to-string' when appropriate."
c4e2be45 Stefan Monnier2015-02-16 02:22:46 -0500960 (cond ((eieio--class-p object) (eieio--class-print-name object))
6dd12ef2 Chong Yidong2009-09-28 00:49:54 +0000961 ((eieio-object-p object) (object-print object))
cb4db863 Stefan Monnier2015-01-04 23:11:37 -0500962 ((and (listp object) (or (eieio--class-p (car object))
6dd12ef2 Chong Yidong2009-09-28 00:49:54 +0000963 (eieio-object-p (car object))))
cb4db863
SM
Stefan Monnier2015-01-04 23:11:37 -0500964 (concat "(" (mapconcat
965 (lambda (x) (eieio-edebug-prin1-to-string print-function x))
966 object " ")
94250173 Stefan Monnier2014-10-17 01:09:24 -0400967 ")"))
fce9e553 Eric Abrahamsen2014-11-02 17:24:33 +0200968 (t (funcall print-function object noescape))))
6dd12ef2 Chong Yidong2009-09-28 00:49:54 +0000969
d4a12e7a
SM
Stefan Monnier2014-12-22 15:46:16 -0500970(advice-add 'edebug-prin1-to-string
971 :around #'eieio-edebug-prin1-to-string)
6dd12ef2 Chong Yidong2009-09-28 00:49:54 +0000972
002b46b7
GM
Glenn Morris2011-02-18 00:00:08 -0800973\f
974;;; Start of automatically extracted autoloads.
975\f
ada5e6b7 Glenn Morris2015-05-06 06:19:35 -0400976;;;### (autoloads nil "eieio-custom" "eieio-custom.el" "916f54b818479a77a02f3ecccda84a11")
002b46b7
GM
Glenn Morris2011-02-18 00:00:08 -0800977;;; Generated autoloads from eieio-custom.el
978
979(autoload 'customize-object "eieio-custom" "\
980Customize OBJ in a custom buffer.
981Optional argument GROUP is the sub-group of slots to display.
982
983\(fn OBJ &optional GROUP)" nil nil)
984
985;;;***
986\f
13b257b0 Glenn Morris2015-08-24 06:23:11 -0400987;;;### (autoloads nil "eieio-opt" "eieio-opt.el" "694d44fcd869546592d35f3321f62667")
002b46b7
GM
Glenn Morris2011-02-18 00:00:08 -0800988;;; Generated autoloads from eieio-opt.el
989
990(autoload 'eieio-browse "eieio-opt" "\
991Create an object browser window to show all objects.
992If optional ROOT-CLASS, then start with that, otherwise start with
993variable `eieio-default-superclass'.
994
995\(fn &optional ROOT-CLASS)" t nil)
002b46b7 Glenn Morris2011-02-18 00:00:08 -0800996
59b5723c Stefan Monnier2015-07-07 02:14:16 -0400997(define-obsolete-function-alias 'eieio-help-class 'cl--describe-class "25.1")
002b46b7 Glenn Morris2011-02-18 00:00:08 -0800998
05e0afce
DE
David Engster2014-01-09 21:31:21 +0100999(autoload 'eieio-help-constructor "eieio-opt" "\
1000Describe CTR if it is a class constructor.
002b46b7 Glenn Morris2011-02-18 00:00:08 -08001001
05e0afce David Engster2014-01-09 21:31:21 +01001002\(fn CTR)" nil nil)
002b46b7 Glenn Morris2011-02-18 00:00:08 -08001003
002b46b7
GM
Glenn Morris2011-02-18 00:00:08 -08001004;;;***
1005\f
1006;;; End of automatically extracted autoloads.
6dd12ef2
CY
Chong Yidong2009-09-28 00:49:54 +00001007
1008(provide 'eieio)
1009
6dd12ef2 Chong Yidong2009-09-28 00:49:54 +00001010;;; eieio ends here