]> code.delx.au - gnu-emacs/blob - lisp/emacs-lisp/eieio.el
bf51986b133788a0f720069d2a0021327ff75296
[gnu-emacs] / lisp / emacs-lisp / eieio.el
1 ;;; eieio.el --- Enhanced Implementation of Emacs Interpreted Objects -*- lexical-binding:t -*-
2 ;;; or maybe Eric's Implementation of Emacs Interpreted Objects
3
4 ;; Copyright (C) 1995-1996, 1998-2015 Free Software Foundation, Inc.
5
6 ;; Author: Eric M. Ludlam <zappo@gnu.org>
7 ;; Version: 1.4
8 ;; 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.
33 ;;
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
38
39 ;; There is funny stuff going on with typep and deftype. This
40 ;; is the only way I seem to be able to make this stuff load properly.
41
42 ;; @TODO - fix :initform to be a form, not a quoted value
43 ;; @TODO - Prefix non-clos functions with `eieio-'.
44
45 ;;; Code:
46
47 (defvar eieio-version "1.4"
48 "Current version of EIEIO.")
49
50 (defun eieio-version ()
51 "Display the current version of EIEIO."
52 (interactive)
53 (message eieio-version))
54
55 (require 'eieio-core)
56 (require 'eieio-generic)
57
58 \f
59 ;;; Defining a new class
60 ;;
61 (defmacro defclass (name superclass slots &rest options-and-doc)
62 "Define NAME as a new class derived from SUPERCLASS with SLOTS.
63 OPTIONS-AND-DOC is used as the class' options and base documentation.
64 SUPERCLASS is a list of superclasses to inherit from, with SLOTS
65 being the slots residing in that class definition. NOTE: Currently
66 only one slot may exist in SUPERCLASS as multiple inheritance is not
67 yet supported. Supported tags are:
68
69 :initform - Initializing form.
70 :initarg - Tag used during initialization.
71 :accessor - Tag used to create a function to access this slot.
72 :allocation - Specify where the value is stored.
73 Defaults to `:instance', but could also be `:class'.
74 :writer - A function symbol which will `write' an object's slot.
75 :reader - A function symbol which will `read' an object.
76 :type - The type of data allowed in this slot (see `typep').
77 :documentation
78 - A string documenting use of this slot.
79
80 The following are extensions on CLOS:
81 :protection - Specify protection for this slot.
82 Defaults to `:public'. Also use `:protected', or `:private'.
83 :custom - When customizing an object, the custom :type. Public only.
84 :label - A text string label used for a slot when customizing.
85 :group - Name of a customization group this slot belongs in.
86 :printer - A function to call to print the value of a slot.
87 See `eieio-override-prin1' as an example.
88
89 A class can also have optional options. These options happen in place
90 of documentation (including a :documentation tag), in addition to
91 documentation, or not at all. Supported options are:
92
93 :documentation - The doc-string used for this class.
94
95 Options added to EIEIO:
96
97 :allow-nil-initform - Non-nil to skip typechecking of null initforms.
98 :custom-groups - List of custom group names. Organizes slots into
99 reasonable groups for customizations.
100 :abstract - Non-nil to prevent instances of this class.
101 If a string, use as an error string if someone does
102 try to make an instance.
103 :method-invocation-order
104 - Control the method invocation order if there is
105 multiple inheritance. Valid values are:
106 :breadth-first - The default.
107 :depth-first
108
109 Options in CLOS not supported in EIEIO:
110
111 :metaclass - Class to use in place of `standard-class'
112 :default-initargs - Initargs to use when initializing new objects of
113 this class.
114
115 Due to the way class options are set up, you can add any tags you wish,
116 and reference them using the function `class-option'."
117 (declare (doc-string 4))
118 ;; This is eval-and-compile only to silence spurious compiler warnings
119 ;; about functions and variables not known to be defined.
120 ;; When eieio-defclass code is merged here and this becomes
121 ;; transparent to the compiler, the eval-and-compile can be removed.
122 `(eval-and-compile
123 (eieio-defclass ',name ',superclass ',slots ',options-and-doc)))
124
125
126 ;;; CLOS style implementation of object creators.
127 ;;
128 (defun make-instance (class &rest initargs)
129 "Make a new instance of CLASS based on INITARGS.
130 CLASS is a class symbol. For example:
131
132 (make-instance 'foo)
133
134 INITARGS is a property list with keywords based on the :initarg
135 for each slot. For example:
136
137 (make-instance 'foo :slot1 value1 :slotN valueN)
138
139 Compatibility note:
140
141 If the first element of INITARGS is a string, it is used as the
142 name of the class.
143
144 In EIEIO, the class' constructor requires a name for use when printing.
145 `make-instance' in CLOS doesn't use names the way Emacs does, so the
146 class is used as the name slot instead when INITARGS doesn't start with
147 a string."
148 (apply (class-constructor class) initargs))
149
150 \f
151 ;;; Get/Set slots in an object.
152 ;;
153 (defmacro oref (obj slot)
154 "Retrieve the value stored in OBJ in the slot named by SLOT.
155 Slot is the name of the slot when created by `defclass' or the label
156 created by the :initarg tag."
157 (declare (debug (form symbolp)))
158 `(eieio-oref ,obj (quote ,slot)))
159
160 (defalias 'slot-value 'eieio-oref)
161 (defalias 'set-slot-value 'eieio-oset)
162
163 (defmacro oref-default (obj slot)
164 "Get the default value of OBJ (maybe a class) for SLOT.
165 The default value is the value installed in a class with the :initform
166 tag. SLOT can be the slot name, or the tag specified by the :initarg
167 tag in the `defclass' call."
168 (declare (debug (form symbolp)))
169 `(eieio-oref-default ,obj (quote ,slot)))
170
171 ;;; Handy CLOS macros
172 ;;
173 (defmacro with-slots (spec-list object &rest body)
174 "Bind SPEC-LIST lexically to slot values in OBJECT, and execute BODY.
175 This establishes a lexical environment for referring to the slots in
176 the instance named by the given slot-names as though they were
177 variables. Within such a context the value of the slot can be
178 specified by using its slot name, as if it were a lexically bound
179 variable. Both setf and setq can be used to set the value of the
180 slot.
181
182 SPEC-LIST is of a form similar to `let'. For example:
183
184 ((VAR1 SLOT1)
185 SLOT2
186 SLOTN
187 (VARN+1 SLOTN+1))
188
189 Where each VAR is the local variable given to the associated
190 SLOT. A slot specified without a variable name is given a
191 variable name of the same name as the slot."
192 (declare (indent 2) (debug (sexp sexp def-body)))
193 (require 'cl-lib)
194 ;; Transform the spec-list into a cl-symbol-macrolet spec-list.
195 (let ((mappings (mapcar (lambda (entry)
196 (let ((var (if (listp entry) (car entry) entry))
197 (slot (if (listp entry) (cadr entry) entry)))
198 (list var `(slot-value ,object ',slot))))
199 spec-list)))
200 (append (list 'cl-symbol-macrolet mappings)
201 body)))
202 \f
203 ;;; Simple generators, and query functions. None of these would do
204 ;; well embedded into an object.
205 ;;
206 (define-obsolete-function-alias
207 'object-class-fast #'eieio--object-class-name "24.4")
208
209 (defun eieio-object-name (obj &optional extra)
210 "Return a Lisp like symbol string for object OBJ.
211 If EXTRA, include that in the string returned to represent the symbol."
212 (eieio--check-type eieio-object-p obj)
213 (format "#<%s %s%s>" (eieio--object-class-name obj)
214 (eieio-object-name-string obj) (or extra "")))
215 (define-obsolete-function-alias 'object-name #'eieio-object-name "24.4")
216
217 (defconst eieio--object-names (make-hash-table :test #'eq :weakness 'key))
218
219 ;; In the past, every EIEIO object had a `name' field, so we had the two method
220 ;; below "for free". Since this field is very rarely used, we got rid of it
221 ;; and instead we keep it in a weak hash-tables, for those very rare objects
222 ;; that use it.
223 (defmethod eieio-object-name-string (obj)
224 "Return a string which is OBJ's name."
225 (declare (obsolete eieio-named "25.1"))
226 (or (gethash obj eieio--object-names)
227 (symbol-name (eieio-object-class obj))))
228 (define-obsolete-function-alias
229 'object-name-string #'eieio-object-name-string "24.4")
230
231 (defmethod eieio-object-set-name-string (obj name)
232 "Set the string which is OBJ's NAME."
233 (declare (obsolete eieio-named "25.1"))
234 (eieio--check-type stringp name)
235 (setf (gethash obj eieio--object-names) name))
236 (define-obsolete-function-alias
237 'object-set-name-string 'eieio-object-set-name-string "24.4")
238
239 (defun eieio-object-class (obj)
240 "Return the class struct defining OBJ."
241 ;; FIXME: We say we return a "struct" but we return a symbol instead!
242 (eieio--check-type eieio-object-p obj)
243 (eieio--object-class-name obj))
244 (define-obsolete-function-alias 'object-class #'eieio-object-class "24.4")
245 ;; CLOS name, maybe?
246 (define-obsolete-function-alias 'class-of #'eieio-object-class "24.4")
247
248 (defun eieio-object-class-name (obj)
249 "Return a Lisp like symbol name for OBJ's class."
250 (eieio--check-type eieio-object-p obj)
251 (eieio-class-name (eieio--object-class-name obj)))
252 (define-obsolete-function-alias
253 'object-class-name 'eieio-object-class-name "24.4")
254
255 (defun eieio-class-parents (class)
256 "Return parent classes to CLASS. (overload of variable).
257
258 The CLOS function `class-direct-superclasses' is aliased to this function."
259 (let ((c (eieio-class-object class)))
260 (eieio--class-parent c)))
261
262 (define-obsolete-function-alias 'class-parents #'eieio-class-parents "24.4")
263
264 (defun eieio-class-children (class)
265 "Return child classes to CLASS.
266 The CLOS function `class-direct-subclasses' is aliased to this function."
267 (eieio--check-type class-p class)
268 (eieio--class-children (eieio--class-v class)))
269 (define-obsolete-function-alias
270 'class-children #'eieio-class-children "24.4")
271
272 ;; Official CLOS functions.
273 (define-obsolete-function-alias
274 'class-direct-superclasses #'eieio-class-parents "24.4")
275 (define-obsolete-function-alias
276 'class-direct-subclasses #'eieio-class-children "24.4")
277
278 (defmacro eieio-class-parent (class)
279 "Return first parent class to CLASS. (overload of variable)."
280 `(car (eieio-class-parents ,class)))
281 (define-obsolete-function-alias 'class-parent 'eieio-class-parent "24.4")
282
283 (defun same-class-p (obj class)
284 "Return t if OBJ is of class-type CLASS."
285 (setq class (eieio--class-object class))
286 (eieio--check-type eieio--class-p class)
287 (eieio--check-type eieio-object-p obj)
288 (eq (eieio--object-class-object obj) class))
289
290 (defun object-of-class-p (obj class)
291 "Return non-nil if OBJ is an instance of CLASS or CLASS' subclasses."
292 (eieio--check-type eieio-object-p obj)
293 ;; class will be checked one layer down
294 (child-of-class-p (eieio--object-class-object obj) class))
295 ;; Backwards compatibility
296 (defalias 'obj-of-class-p 'object-of-class-p)
297
298 (defun child-of-class-p (child class)
299 "Return non-nil if CHILD class is a subclass of CLASS."
300 (setq child (eieio--class-object child))
301 (eieio--check-type eieio--class-p child)
302 ;; `eieio-default-superclass' is never mentioned in eieio--class-parent,
303 ;; so we have to special case it here.
304 (or (eq class 'eieio-default-superclass)
305 (let ((p nil))
306 (setq class (eieio--class-object class))
307 (eieio--check-type eieio--class-p class)
308 (while (and child (not (eq child class)))
309 (setq p (append p (eieio--class-parent child))
310 child (pop p)))
311 (if child t))))
312
313 (defun object-slots (obj)
314 "Return list of slots available in OBJ."
315 (eieio--check-type eieio-object-p obj)
316 (eieio--class-public-a (eieio--object-class-object obj)))
317
318 (defun eieio--class-slot-initarg (class slot) "Fetch from CLASS, SLOT's :initarg."
319 (eieio--check-type eieio--class-p class)
320 (let ((ia (eieio--class-initarg-tuples class))
321 (f nil))
322 (while (and ia (not f))
323 (if (eq (cdr (car ia)) slot)
324 (setq f (car (car ia))))
325 (setq ia (cdr ia)))
326 f))
327
328 ;;; Object Set macros
329 ;;
330 (defmacro oset (obj slot value)
331 "Set the value in OBJ for slot SLOT to VALUE.
332 SLOT is the slot name as specified in `defclass' or the tag created
333 with in the :initarg slot. VALUE can be any Lisp object."
334 (declare (debug (form symbolp form)))
335 `(eieio-oset ,obj (quote ,slot) ,value))
336
337 (defmacro oset-default (class slot value)
338 "Set the default slot in CLASS for SLOT to VALUE.
339 The default value is usually set with the :initform tag during class
340 creation. This allows users to change the default behavior of classes
341 after they are created."
342 (declare (debug (form symbolp form)))
343 `(eieio-oset-default ,class (quote ,slot) ,value))
344
345 ;;; CLOS queries into classes and slots
346 ;;
347 (defun slot-boundp (object slot)
348 "Return non-nil if OBJECT's SLOT is bound.
349 Setting a slot's value makes it bound. Calling `slot-makeunbound' will
350 make a slot unbound.
351 OBJECT can be an instance or a class."
352 ;; Skip typechecking while retrieving this value.
353 (let ((eieio-skip-typecheck t))
354 ;; Return nil if the magic symbol is in there.
355 (not (eq (cond
356 ((eieio-object-p object) (eieio-oref object slot))
357 ((class-p object) (eieio-oref-default object slot))
358 (t (signal 'wrong-type-argument (list 'eieio-object-p object))))
359 eieio-unbound))))
360
361 (defun slot-makeunbound (object slot)
362 "In OBJECT, make SLOT unbound."
363 (eieio-oset object slot eieio-unbound))
364
365 (defun slot-exists-p (object-or-class slot)
366 "Return non-nil if OBJECT-OR-CLASS has SLOT."
367 (let ((cv (cond ((eieio-object-p object-or-class)
368 (eieio--object-class-object object-or-class))
369 (t (eieio-class-object object-or-class)))))
370 (or (memq slot (eieio--class-public-a cv))
371 (memq slot (eieio--class-class-allocation-a cv)))
372 ))
373
374 (defun find-class (symbol &optional errorp)
375 "Return the class that SYMBOL represents.
376 If there is no class, nil is returned if ERRORP is nil.
377 If ERRORP is non-nil, `wrong-argument-type' is signaled."
378 (if (not (class-p symbol))
379 (if errorp (signal 'wrong-type-argument (list 'class-p symbol))
380 nil)
381 (eieio--class-v symbol)))
382
383 ;;; Slightly more complex utility functions for objects
384 ;;
385 (defun object-assoc (key slot list)
386 "Return an object if KEY is `equal' to SLOT's value of an object in LIST.
387 LIST is a list of objects whose slots are searched.
388 Objects in LIST do not need to have a slot named SLOT, nor does
389 SLOT need to be bound. If these errors occur, those objects will
390 be ignored."
391 (eieio--check-type listp list)
392 (while (and list (not (condition-case nil
393 ;; This prevents errors for missing slots.
394 (equal key (eieio-oref (car list) slot))
395 (error nil))))
396 (setq list (cdr list)))
397 (car list))
398
399 (defun object-assoc-list (slot list)
400 "Return an association list with the contents of SLOT as the key element.
401 LIST must be a list of objects with SLOT in it.
402 This is useful when you need to do completing read on an object group."
403 (eieio--check-type listp list)
404 (let ((assoclist nil))
405 (while list
406 (setq assoclist (cons (cons (eieio-oref (car list) slot)
407 (car list))
408 assoclist))
409 (setq list (cdr list)))
410 (nreverse assoclist)))
411
412 (defun object-assoc-list-safe (slot list)
413 "Return an association list with the contents of SLOT as the key element.
414 LIST must be a list of objects, but those objects do not need to have
415 SLOT in it. If it does not, then that element is left out of the association
416 list."
417 (eieio--check-type listp list)
418 (let ((assoclist nil))
419 (while list
420 (if (slot-exists-p (car list) slot)
421 (setq assoclist (cons (cons (eieio-oref (car list) slot)
422 (car list))
423 assoclist)))
424 (setq list (cdr list)))
425 (nreverse assoclist)))
426
427 (defun object-add-to-list (object slot item &optional append)
428 "In OBJECT's SLOT, add ITEM to the list of elements.
429 Optional argument APPEND indicates we need to append to the list.
430 If ITEM already exists in the list in SLOT, then it is not added.
431 Comparison is done with `equal' through the `member' function call.
432 If SLOT is unbound, bind it to the list containing ITEM."
433 (let (ov)
434 ;; Find the originating list.
435 (if (not (slot-boundp object slot))
436 (setq ov (list item))
437 (setq ov (eieio-oref object slot))
438 ;; turn it into a list.
439 (unless (listp ov)
440 (setq ov (list ov)))
441 ;; Do the combination
442 (if (not (member item ov))
443 (setq ov
444 (if append
445 (append ov (list item))
446 (cons item ov)))))
447 ;; Set back into the slot.
448 (eieio-oset object slot ov)))
449
450 (defun object-remove-from-list (object slot item)
451 "In OBJECT's SLOT, remove occurrences of ITEM.
452 Deletion is done with `delete', which deletes by side effect,
453 and comparisons are done with `equal'.
454 If SLOT is unbound, do nothing."
455 (if (not (slot-boundp object slot))
456 nil
457 (eieio-oset object slot (delete item (eieio-oref object slot)))))
458
459 ;;; Here are some CLOS items that need the CL package
460 ;;
461
462 (gv-define-simple-setter eieio-oref eieio-oset)
463
464 \f
465 ;;;
466 ;; We want all objects created by EIEIO to have some default set of
467 ;; behaviors so we can create object utilities, and allow various
468 ;; types of error checking. To do this, create the default EIEIO
469 ;; class, and when no parent class is specified, use this as the
470 ;; default. (But don't store it in the other classes as the default,
471 ;; allowing for transparent support.)
472 ;;
473
474 (defclass eieio-default-superclass nil
475 nil
476 "Default parent class for classes with no specified parent class.
477 Its slots are automatically adopted by classes with no specified parents.
478 This class is not stored in the `parent' slot of a class vector."
479 :abstract t)
480
481 (setq eieio-default-superclass (eieio--class-v 'eieio-default-superclass))
482
483 (defalias 'standard-class 'eieio-default-superclass)
484
485 (defgeneric eieio-constructor (class &rest slots)
486 "Default constructor for CLASS `eieio-default-superclass'.")
487
488 (define-obsolete-function-alias 'constructor #'eieio-constructor "25.1")
489
490 (defmethod eieio-constructor :static
491 ((class eieio-default-superclass) &rest slots)
492 "Default constructor for CLASS `eieio-default-superclass'.
493 SLOTS are the initialization slots used by `shared-initialize'.
494 This static method is called when an object is constructed.
495 It allocates the vector used to represent an EIEIO object, and then
496 calls `shared-initialize' on that object."
497 (let* ((new-object (copy-sequence (eieio--class-default-object-cache (eieio--class-v class)))))
498 ;; Call the initialize method on the new object with the slots
499 ;; that were passed down to us.
500 (initialize-instance new-object slots)
501 ;; Return the created object.
502 new-object))
503
504 (defgeneric shared-initialize (obj slots)
505 "Set slots of OBJ with SLOTS which is a list of name/value pairs.
506 Called from the constructor routine.")
507
508 (defmethod shared-initialize ((obj eieio-default-superclass) slots)
509 "Set slots of OBJ with SLOTS which is a list of name/value pairs.
510 Called from the constructor routine."
511 (eieio--with-scoped-class (eieio--object-class-object obj)
512 (while slots
513 (let ((rn (eieio--initarg-to-attribute (eieio--object-class-object obj)
514 (car slots))))
515 (if (not rn)
516 (slot-missing obj (car slots) 'oset (car (cdr slots)))
517 (eieio-oset obj rn (car (cdr slots)))))
518 (setq slots (cdr (cdr slots))))))
519
520 (defgeneric initialize-instance (this &optional slots)
521 "Construct the new object THIS based on SLOTS.")
522
523 (defmethod initialize-instance ((this eieio-default-superclass)
524 &optional slots)
525 "Construct the new object THIS based on SLOTS.
526 SLOTS is a tagged list where odd numbered elements are tags, and
527 even numbered elements are the values to store in the tagged slot.
528 If you overload the `initialize-instance', there you will need to
529 call `shared-initialize' yourself, or you can call `call-next-method'
530 to have this constructor called automatically. If these steps are
531 not taken, then new objects of your class will not have their values
532 dynamically set from SLOTS."
533 ;; First, see if any of our defaults are `lambda', and
534 ;; re-evaluate them and apply the value to our slots.
535 (let* ((this-class (eieio--object-class-object this))
536 (slot (eieio--class-public-a this-class))
537 (defaults (eieio--class-public-d this-class)))
538 (while slot
539 ;; For each slot, see if we need to evaluate it.
540 ;;
541 ;; Paul Landes said in an email:
542 ;; > CL evaluates it if it can, and otherwise, leaves it as
543 ;; > the quoted thing as you already have. This is by the
544 ;; > Sonya E. Keene book and other things I've look at on the
545 ;; > web.
546 (let ((dflt (eieio-default-eval-maybe (car defaults))))
547 (when (not (eq dflt (car defaults)))
548 (eieio-oset this (car slot) dflt) ))
549 ;; Next.
550 (setq slot (cdr slot)
551 defaults (cdr defaults))))
552 ;; Shared initialize will parse our slots for us.
553 (shared-initialize this slots))
554
555 (defgeneric slot-missing (object slot-name operation &optional new-value)
556 "Method invoked when an attempt to access a slot in OBJECT fails.")
557
558 (defmethod slot-missing ((object eieio-default-superclass) slot-name
559 _operation &optional _new-value)
560 "Method invoked when an attempt to access a slot in OBJECT fails.
561 SLOT-NAME is the name of the failed slot, OPERATION is the type of access
562 that was requested, and optional NEW-VALUE is the value that was desired
563 to be set.
564
565 This method is called from `oref', `oset', and other functions which
566 directly reference slots in EIEIO objects."
567 (signal 'invalid-slot-name (list (eieio-object-name object)
568 slot-name)))
569
570 (defgeneric slot-unbound (object class slot-name fn)
571 "Slot unbound is invoked during an attempt to reference an unbound slot.")
572
573 (defmethod slot-unbound ((object eieio-default-superclass)
574 class slot-name fn)
575 "Slot unbound is invoked during an attempt to reference an unbound slot.
576 OBJECT is the instance of the object being reference. CLASS is the
577 class of OBJECT, and SLOT-NAME is the offending slot. This function
578 throws the signal `unbound-slot'. You can overload this function and
579 return the value to use in place of the unbound value.
580 Argument FN is the function signaling this error.
581 Use `slot-boundp' to determine if a slot is bound or not.
582
583 In CLOS, the argument list is (CLASS OBJECT SLOT-NAME), but
584 EIEIO can only dispatch on the first argument, so the first two are swapped."
585 (signal 'unbound-slot (list (eieio-class-name class) (eieio-object-name object)
586 slot-name fn)))
587
588 (defgeneric clone (obj &rest params)
589 "Make a copy of OBJ, and then supply PARAMS.
590 PARAMS is a parameter list of the same form used by `initialize-instance'.
591
592 When overloading `clone', be sure to call `call-next-method'
593 first and modify the returned object.")
594
595 (defmethod clone ((obj eieio-default-superclass) &rest params)
596 "Make a copy of OBJ, and then apply PARAMS."
597 (let ((nobj (copy-sequence obj)))
598 (if (stringp (car params))
599 (funcall (if eieio-backward-compatibility #'ignore #'message)
600 "Obsolete name %S passed to clone" (pop params)))
601 (if params (shared-initialize nobj params))
602 nobj))
603
604 (defgeneric destructor (this &rest params)
605 "Destructor for cleaning up any dynamic links to our object.")
606
607 (defmethod destructor ((_this eieio-default-superclass) &rest _params)
608 "Destructor for cleaning up any dynamic links to our object.
609 Argument THIS is the object being destroyed. PARAMS are additional
610 ignored parameters."
611 ;; No cleanup... yet.
612 )
613
614 (defgeneric object-print (this &rest strings)
615 "Pretty printer for object THIS. Call function `object-name' with STRINGS.
616
617 It is sometimes useful to put a summary of the object into the
618 default #<notation> string when using EIEIO browsing tools.
619 Implement this method to customize the summary.")
620
621 (defmethod object-print ((this eieio-default-superclass) &rest strings)
622 "Pretty printer for object THIS. Call function `object-name' with STRINGS.
623 The default method for printing object THIS is to use the
624 function `object-name'.
625
626 It is sometimes useful to put a summary of the object into the
627 default #<notation> string when using EIEIO browsing tools.
628
629 Implement this function and specify STRINGS in a call to
630 `call-next-method' to provide additional summary information.
631 When passing in extra strings from child classes, always remember
632 to prepend a space."
633 (eieio-object-name this (apply #'concat strings)))
634
635 (defvar eieio-print-depth 0
636 "When printing, keep track of the current indentation depth.")
637
638 (defgeneric object-write (this &optional comment)
639 "Write out object THIS to the current stream.
640 Optional COMMENT will add comments to the beginning of the output.")
641
642 (defmethod object-write ((this eieio-default-superclass) &optional comment)
643 "Write object THIS out to the current stream.
644 This writes out the vector version of this object. Complex and recursive
645 object are discouraged from being written.
646 If optional COMMENT is non-nil, include comments when outputting
647 this object."
648 (when comment
649 (princ ";; Object ")
650 (princ (eieio-object-name-string this))
651 (princ "\n")
652 (princ comment)
653 (princ "\n"))
654 (let* ((cl (eieio-object-class this))
655 (cv (eieio--class-v cl)))
656 ;; Now output readable lisp to recreate this object
657 ;; It should look like this:
658 ;; (<constructor> <name> <slot> <slot> ... )
659 ;; Each slot's slot is writen using its :writer.
660 (princ (make-string (* eieio-print-depth 2) ? ))
661 (princ "(")
662 (princ (symbol-name (class-constructor (eieio-object-class this))))
663 (princ " ")
664 (prin1 (eieio-object-name-string this))
665 (princ "\n")
666 ;; Loop over all the public slots
667 (let ((publa (eieio--class-public-a cv))
668 (publd (eieio--class-public-d cv))
669 (publp (eieio--class-public-printer cv))
670 (eieio-print-depth (1+ eieio-print-depth)))
671 (while publa
672 (when (slot-boundp this (car publa))
673 (let ((i (eieio--class-slot-initarg cv (car publa)))
674 (v (eieio-oref this (car publa)))
675 )
676 (unless (or (not i) (equal v (car publd)))
677 (unless (bolp)
678 (princ "\n"))
679 (princ (make-string (* eieio-print-depth 2) ? ))
680 (princ (symbol-name i))
681 (if (car publp)
682 ;; Use our public printer
683 (progn
684 (princ " ")
685 (funcall (car publp) v))
686 ;; Use our generic override prin1 function.
687 (princ (if (or (eieio-object-p v)
688 (eieio-object-p (car-safe v)))
689 "\n" " "))
690 (eieio-override-prin1 v)))))
691 (setq publa (cdr publa) publd (cdr publd)
692 publp (cdr publp))))
693 (princ ")")
694 (when (= eieio-print-depth 0)
695 (princ "\n"))))
696
697 (defun eieio-override-prin1 (thing)
698 "Perform a `prin1' on THING taking advantage of object knowledge."
699 (cond ((eieio-object-p thing)
700 (object-write thing))
701 ((consp thing)
702 (eieio-list-prin1 thing))
703 ((class-p thing)
704 (princ (eieio-class-name thing)))
705 ((or (keywordp thing) (booleanp thing))
706 (prin1 thing))
707 ((symbolp thing)
708 (princ (concat "'" (symbol-name thing))))
709 (t (prin1 thing))))
710
711 (defun eieio-list-prin1 (list)
712 "Display LIST where list may contain objects."
713 (if (not (eieio-object-p (car list)))
714 (progn
715 (princ "'")
716 (prin1 list))
717 (princ (make-string (* eieio-print-depth 2) ? ))
718 (princ "(list")
719 (let ((eieio-print-depth (1+ eieio-print-depth)))
720 (while list
721 (princ "\n")
722 (if (eieio-object-p (car list))
723 (object-write (car list))
724 (princ (make-string (* eieio-print-depth 2) ? ))
725 (eieio-override-prin1 (car list)))
726 (setq list (cdr list))))
727 (princ ")")))
728
729 \f
730 ;;; Unimplemented functions from CLOS
731 ;;
732 (defun change-class (_obj _class)
733 "Change the class of OBJ to type CLASS.
734 This may create or delete slots, but does not affect the return value
735 of `eq'."
736 (error "EIEIO: `change-class' is unimplemented"))
737
738 ;; Hook ourselves into help system for describing classes and methods.
739 (add-hook 'help-fns-describe-function-functions 'eieio-help-constructor)
740
741 ;;; Interfacing with edebug
742 ;;
743 (defun eieio-edebug-prin1-to-string (print-function object &optional noescape)
744 "Display EIEIO OBJECT in fancy format.
745
746 Used as advice around `edebug-prin1-to-string', held in the
747 variable PRINT-FUNCTION. Optional argument NOESCAPE is passed to
748 `prin1-to-string' when appropriate."
749 (cond ((eieio--class-p object) (eieio-class-name object))
750 ((eieio-object-p object) (object-print object))
751 ((and (listp object) (or (eieio--class-p (car object))
752 (eieio-object-p (car object))))
753 (concat "(" (mapconcat
754 (lambda (x) (eieio-edebug-prin1-to-string print-function x))
755 object " ")
756 ")"))
757 (t (funcall print-function object noescape))))
758
759 (advice-add 'edebug-prin1-to-string
760 :around #'eieio-edebug-prin1-to-string)
761
762 \f
763 ;;; Start of automatically extracted autoloads.
764 \f
765 ;;;### (autoloads nil "eieio-custom" "eieio-custom.el" "7d3c0bca065713ae74af0c07778dd1f4")
766 ;;; Generated autoloads from eieio-custom.el
767
768 (autoload 'customize-object "eieio-custom" "\
769 Customize OBJ in a custom buffer.
770 Optional argument GROUP is the sub-group of slots to display.
771
772 \(fn OBJ &optional GROUP)" nil nil)
773
774 ;;;***
775 \f
776 ;;;### (autoloads nil "eieio-opt" "eieio-opt.el" "7267115a161243e1e6ea75f2d25c8ebc")
777 ;;; Generated autoloads from eieio-opt.el
778
779 (autoload 'eieio-browse "eieio-opt" "\
780 Create an object browser window to show all objects.
781 If optional ROOT-CLASS, then start with that, otherwise start with
782 variable `eieio-default-superclass'.
783
784 \(fn &optional ROOT-CLASS)" t nil)
785
786 (autoload 'eieio-help-class "eieio-opt" "\
787 Print help description for CLASS.
788 If CLASS is actually an object, then also display current values of that object.
789
790 \(fn CLASS)" nil nil)
791
792 (autoload 'eieio-help-constructor "eieio-opt" "\
793 Describe CTR if it is a class constructor.
794
795 \(fn CTR)" nil nil)
796
797 ;;;***
798 \f
799 ;;; End of automatically extracted autoloads.
800
801 (provide 'eieio)
802
803 ;;; eieio ends here