merge master, fix conflicts
[emacs.git] / lisp / emacs-lisp / eieio.el
blob419a78be4696f3e9960e91c1c278322976a8e701
1 ;;; eieio.el --- Enhanced Implementation of Emacs Interpreted Objects -*- lexical-binding:t -*-
2 ;;; or maybe Eric's Implementation of Emacs Interpreted Objects
4 ;; Copyright (C) 1995-1996, 1998-2015 Free Software Foundation, Inc.
6 ;; Author: Eric M. Ludlam <zappo@gnu.org>
7 ;; Version: 1.4
8 ;; Keywords: OO, lisp
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;; Commentary:
27 ;; EIEIO is a series of Lisp routines which implements a subset of
28 ;; CLOS, the Common Lisp Object System. In addition, EIEIO also adds
29 ;; a few new features which help it integrate more strongly with the
30 ;; Emacs running environment.
32 ;; See eieio.texi for complete documentation on using this package.
34 ;; Note: the implementation of the c3 algorithm is based on:
35 ;; Kim Barrett et al.: A Monotonic Superclass Linearization for Dylan
36 ;; Retrieved from:
37 ;; http://192.220.96.201/dylan/linearization-oopsla96.html
39 ;; There is funny stuff going on with typep and deftype. This
40 ;; is the only way I seem to be able to make this stuff load properly.
42 ;; @TODO - fix :initform to be a form, not a quoted value
43 ;; @TODO - Prefix non-clos functions with `eieio-'.
45 ;;; Code:
47 (defvar eieio-version "1.4"
48 "Current version of EIEIO.")
50 (defun eieio-version ()
51 "Display the current version of EIEIO."
52 (interactive)
53 (message eieio-version))
55 (require 'eieio-core)
56 (require 'eieio-generic)
59 ;;; Defining a new class
61 (defmacro defclass (name superclasses 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 SUPERCLASSES is a list of superclasses to inherit from, with SLOTS
65 being the slots residing in that class definition. Supported tags are:
67 :initform - Initializing form.
68 :initarg - Tag used during initialization.
69 :accessor - Tag used to create a function to access this slot.
70 :allocation - Specify where the value is stored.
71 Defaults to `:instance', but could also be `:class'.
72 :writer - A function symbol which will `write' an object's slot.
73 :reader - A function symbol which will `read' an object.
74 :type - The type of data allowed in this slot (see `typep').
75 :documentation
76 - A string documenting use of this slot.
78 The following are extensions on CLOS:
79 :protection - Specify protection for this slot.
80 Defaults to `:public'. Also use `:protected', or `:private'.
81 :custom - When customizing an object, the custom :type. Public only.
82 :label - A text string label used for a slot when customizing.
83 :group - Name of a customization group this slot belongs in.
84 :printer - A function to call to print the value of a slot.
85 See `eieio-override-prin1' as an example.
87 A class can also have optional options. These options happen in place
88 of documentation (including a :documentation tag), in addition to
89 documentation, or not at all. Supported options are:
91 :documentation - The doc-string used for this class.
93 Options added to EIEIO:
95 :allow-nil-initform - Non-nil to skip typechecking of null initforms.
96 :custom-groups - List of custom group names. Organizes slots into
97 reasonable groups for customizations.
98 :abstract - Non-nil to prevent instances of this class.
99 If a string, use as an error string if someone does
100 try to make an instance.
101 :method-invocation-order
102 - Control the method invocation order if there is
103 multiple inheritance. Valid values are:
104 :breadth-first - The default.
105 :depth-first
107 Options in CLOS not supported in EIEIO:
109 :metaclass - Class to use in place of `standard-class'
110 :default-initargs - Initargs to use when initializing new objects of
111 this class.
113 Due to the way class options are set up, you can add any tags you wish,
114 and reference them using the function `class-option'."
115 (declare (doc-string 4))
116 (eieio--check-type listp superclasses)
118 (cond ((and (stringp (car options-and-doc))
119 (/= 1 (% (length options-and-doc) 2)))
120 (error "Too many arguments to `defclass'"))
121 ((and (symbolp (car options-and-doc))
122 (/= 0 (% (length options-and-doc) 2)))
123 (error "Too many arguments to `defclass'")))
125 (if (stringp (car options-and-doc))
126 (setq options-and-doc
127 (cons :documentation options-and-doc)))
129 ;; Make sure the method invocation order is a valid value.
130 (let ((io (eieio--class-option-assoc options-and-doc
131 :method-invocation-order)))
132 (when (and io (not (member io '(:depth-first :breadth-first :c3))))
133 (error "Method invocation order %s is not allowed" io)))
135 (let ((testsym1 (intern (concat (symbol-name name) "-p")))
136 (testsym2 (intern (format "eieio--childp--%s" name)))
137 (accessors ()))
139 ;; Collect the accessors we need to define.
140 (pcase-dolist (`(,sname . ,soptions) slots)
141 (let* ((acces (plist-get soptions :accessor))
142 (initarg (plist-get soptions :initarg))
143 (reader (plist-get soptions :reader))
144 (writer (plist-get soptions :writer))
145 (alloc (plist-get soptions :allocation))
146 (label (plist-get soptions :label)))
148 (if eieio-error-unsupported-class-tags
149 (let ((tmp soptions))
150 (while tmp
151 (if (not (member (car tmp) '(:accessor
152 :initform
153 :initarg
154 :documentation
155 :protection
156 :reader
157 :writer
158 :allocation
159 :type
160 :custom
161 :label
162 :group
163 :printer
164 :allow-nil-initform
165 :custom-groups)))
166 (signal 'invalid-slot-type (list (car tmp))))
167 (setq tmp (cdr (cdr tmp))))))
169 ;; Make sure the :allocation parameter has a valid value.
170 (if (not (memq alloc '(nil :class :instance)))
171 (signal 'invalid-slot-type (list :allocation alloc)))
173 ;; Label is nil, or a string
174 (if (not (or (null label) (stringp label)))
175 (signal 'invalid-slot-type (list :label label)))
177 ;; Is there an initarg, but allocation of class?
178 (if (and initarg (eq alloc :class))
179 (message "Class allocated slots do not need :initarg"))
181 ;; Anyone can have an accessor function. This creates a function
182 ;; of the specified name, and also performs a `defsetf' if applicable
183 ;; so that users can `setf' the space returned by this function.
184 (when acces
185 ;; FIXME: The defmethod below only defines a part of the generic
186 ;; function (good), but the define-setter below affects the whole
187 ;; generic function (bad)!
188 (push `(gv-define-setter ,acces (store object)
189 ;; Apparently, eieio-oset-default doesn't work like
190 ;; oref-default and only accept class arguments!
191 (list ',(if nil ;; (eq alloc :class)
192 'eieio-oset-default
193 'eieio-oset)
194 object '',sname store))
195 accessors)
196 (push `(defmethod ,acces ,(if (eq alloc :class) :static :primary)
197 ((this ,name))
198 ,(format
199 "Retrieve the slot `%S' from an object of class `%S'."
200 sname name)
201 (if (slot-boundp this ',sname)
202 ;; Use oref-default for :class allocated slots, since
203 ;; these also accept the use of a class argument instead
204 ;; of an object argument.
205 (,(if (eq alloc :class) 'eieio-oref-default 'eieio-oref)
206 this ',sname)
207 ;; Else - Some error? nil?
208 nil))
209 accessors))
211 ;; If a writer is defined, then create a generic method of that
212 ;; name whose purpose is to set the value of the slot.
213 (if writer
214 (push `(defmethod ,writer ((this ,name) value)
215 ,(format "Set the slot `%S' of an object of class `%S'."
216 sname name)
217 (setf (slot-value this ',sname) value))
218 accessors))
219 ;; If a reader is defined, then create a generic method
220 ;; of that name whose purpose is to access this slot value.
221 (if reader
222 (push `(defmethod ,reader ((this ,name))
223 ,(format "Access the slot `%S' from object of class `%S'."
224 sname name)
225 (slot-value this ',sname))
226 accessors))
229 `(progn
230 ;; This test must be created right away so we can have self-
231 ;; referencing classes. ei, a class whose slot can contain only
232 ;; pointers to itself.
234 ;; Create the test function.
235 (defun ,testsym1 (obj)
236 ,(format "Test OBJ to see if it an object of type %S." name)
237 (and (eieio-object-p obj)
238 (same-class-p obj ',name)))
240 (defun ,testsym2 (obj)
241 ,(format
242 "Test OBJ to see if it an object is a child of type %S."
243 name)
244 (and (eieio-object-p obj)
245 (object-of-class-p obj ',name)))
247 ,@(when eieio-backward-compatibility
248 (let ((f (intern (format "%s-child-p" name))))
249 `((defalias ',f ',testsym2)
250 (make-obsolete
251 ',f ,(format "use (cl-typep ... '%s) instead" name) "25.1"))))
253 ;; When using typep, (typep OBJ 'myclass) returns t for objects which
254 ;; are subclasses of myclass. For our predicates, however, it is
255 ;; important for EIEIO to be backwards compatible, where
256 ;; myobject-p, and myobject-child-p are different.
257 ;; "cl" uses this technique to specify symbols with specific typep
258 ;; test, so we can let typep have the CLOS documented behavior
259 ;; while keeping our above predicate clean.
261 (put ',name 'cl-deftype-satisfies #',testsym2)
263 (eieio-defclass-internal ',name ',superclasses ',slots ',options-and-doc)
265 ,@accessors
267 ;; Create the constructor function
268 ,(if (eieio--class-option-assoc options-and-doc :abstract)
269 ;; Abstract classes cannot be instantiated. Say so.
270 (let ((abs (eieio--class-option-assoc options-and-doc :abstract)))
271 (if (not (stringp abs))
272 (setq abs (format "Class %s is abstract" name)))
273 `(defun ,name (&rest _)
274 ,(format "You cannot create a new object of type %S." name)
275 (error ,abs)))
277 ;; Non-abstract classes need a constructor.
278 `(defun ,name (&rest slots)
279 ,(format "Create a new object with name NAME of class type %S."
280 name)
281 (if (and slots
282 (let ((x (car slots)))
283 (or (stringp x) (null x))))
284 (funcall (if eieio-backward-compatibility #'ignore #'message)
285 "Obsolete name %S passed to %S constructor"
286 (pop slots) ',name))
287 (apply #'eieio-constructor ',name slots))))))
290 ;;; CLOS style implementation of object creators.
292 (defun make-instance (class &rest initargs)
293 "Make a new instance of CLASS based on INITARGS.
294 CLASS is a class symbol. For example:
296 (make-instance 'foo)
298 INITARGS is a property list with keywords based on the :initarg
299 for each slot. For example:
301 (make-instance 'foo :slot1 value1 :slotN valueN)
303 Compatibility note:
305 If the first element of INITARGS is a string, it is used as the
306 name of the class.
308 In EIEIO, the class' constructor requires a name for use when printing.
309 `make-instance' in CLOS doesn't use names the way Emacs does, so the
310 class is used as the name slot instead when INITARGS doesn't start with
311 a string."
312 (apply (class-constructor class) initargs))
315 ;;; Get/Set slots in an object.
317 (defmacro oref (obj slot)
318 "Retrieve the value stored in OBJ in the slot named by SLOT.
319 Slot is the name of the slot when created by `defclass' or the label
320 created by the :initarg tag."
321 (declare (debug (form symbolp)))
322 `(eieio-oref ,obj (quote ,slot)))
324 (defalias 'slot-value 'eieio-oref)
325 (defalias 'set-slot-value 'eieio-oset)
327 (defmacro oref-default (obj slot)
328 "Get the default value of OBJ (maybe a class) for SLOT.
329 The default value is the value installed in a class with the :initform
330 tag. SLOT can be the slot name, or the tag specified by the :initarg
331 tag in the `defclass' call."
332 (declare (debug (form symbolp)))
333 `(eieio-oref-default ,obj (quote ,slot)))
335 ;;; Handy CLOS macros
337 (defmacro with-slots (spec-list object &rest body)
338 "Bind SPEC-LIST lexically to slot values in OBJECT, and execute BODY.
339 This establishes a lexical environment for referring to the slots in
340 the instance named by the given slot-names as though they were
341 variables. Within such a context the value of the slot can be
342 specified by using its slot name, as if it were a lexically bound
343 variable. Both setf and setq can be used to set the value of the
344 slot.
346 SPEC-LIST is of a form similar to `let'. For example:
348 ((VAR1 SLOT1)
349 SLOT2
350 SLOTN
351 (VARN+1 SLOTN+1))
353 Where each VAR is the local variable given to the associated
354 SLOT. A slot specified without a variable name is given a
355 variable name of the same name as the slot."
356 (declare (indent 2) (debug (sexp sexp def-body)))
357 (require 'cl-lib)
358 ;; Transform the spec-list into a cl-symbol-macrolet spec-list.
359 (let ((mappings (mapcar (lambda (entry)
360 (let ((var (if (listp entry) (car entry) entry))
361 (slot (if (listp entry) (cadr entry) entry)))
362 (list var `(slot-value ,object ',slot))))
363 spec-list)))
364 (append (list 'cl-symbol-macrolet mappings)
365 body)))
367 ;;; Simple generators, and query functions. None of these would do
368 ;; well embedded into an object.
370 (define-obsolete-function-alias
371 'object-class-fast #'eieio--object-class-name "24.4")
373 (defun eieio-object-name (obj &optional extra)
374 "Return a Lisp like symbol string for object OBJ.
375 If EXTRA, include that in the string returned to represent the symbol."
376 (eieio--check-type eieio-object-p obj)
377 (format "#<%s %s%s>" (eieio--object-class-name obj)
378 (eieio-object-name-string obj) (or extra "")))
379 (define-obsolete-function-alias 'object-name #'eieio-object-name "24.4")
381 (defconst eieio--object-names (make-hash-table :test #'eq :weakness 'key))
383 ;; In the past, every EIEIO object had a `name' field, so we had the two method
384 ;; below "for free". Since this field is very rarely used, we got rid of it
385 ;; and instead we keep it in a weak hash-tables, for those very rare objects
386 ;; that use it.
387 (defmethod eieio-object-name-string (obj)
388 "Return a string which is OBJ's name."
389 (declare (obsolete eieio-named "25.1"))
390 (or (gethash obj eieio--object-names)
391 (symbol-name (eieio-object-class obj))))
392 (define-obsolete-function-alias
393 'object-name-string #'eieio-object-name-string "24.4")
395 (defmethod eieio-object-set-name-string (obj name)
396 "Set the string which is OBJ's NAME."
397 (declare (obsolete eieio-named "25.1"))
398 (eieio--check-type stringp name)
399 (setf (gethash obj eieio--object-names) name))
400 (define-obsolete-function-alias
401 'object-set-name-string 'eieio-object-set-name-string "24.4")
403 (defun eieio-object-class (obj)
404 "Return the class struct defining OBJ."
405 ;; FIXME: We say we return a "struct" but we return a symbol instead!
406 (eieio--check-type eieio-object-p obj)
407 (eieio--object-class-name obj))
408 (define-obsolete-function-alias 'object-class #'eieio-object-class "24.4")
409 ;; CLOS name, maybe?
410 (define-obsolete-function-alias 'class-of #'eieio-object-class "24.4")
412 (defun eieio-object-class-name (obj)
413 "Return a Lisp like symbol name for OBJ's class."
414 (eieio--check-type eieio-object-p obj)
415 (eieio-class-name (eieio--object-class-name obj)))
416 (define-obsolete-function-alias
417 'object-class-name 'eieio-object-class-name "24.4")
419 (defun eieio-class-parents (class)
420 "Return parent classes to CLASS. (overload of variable).
422 The CLOS function `class-direct-superclasses' is aliased to this function."
423 (let ((c (eieio-class-object class)))
424 (eieio--class-parent c)))
426 (define-obsolete-function-alias 'class-parents #'eieio-class-parents "24.4")
428 (defun eieio-class-children (class)
429 "Return child classes to CLASS.
430 The CLOS function `class-direct-subclasses' is aliased to this function."
431 (eieio--check-type class-p class)
432 (eieio--class-children (eieio--class-v class)))
433 (define-obsolete-function-alias
434 'class-children #'eieio-class-children "24.4")
436 ;; Official CLOS functions.
437 (define-obsolete-function-alias
438 'class-direct-superclasses #'eieio-class-parents "24.4")
439 (define-obsolete-function-alias
440 'class-direct-subclasses #'eieio-class-children "24.4")
442 (defmacro eieio-class-parent (class)
443 "Return first parent class to CLASS. (overload of variable)."
444 `(car (eieio-class-parents ,class)))
445 (define-obsolete-function-alias 'class-parent 'eieio-class-parent "24.4")
447 (defun same-class-p (obj class)
448 "Return t if OBJ is of class-type CLASS."
449 (setq class (eieio--class-object class))
450 (eieio--check-type eieio--class-p class)
451 (eieio--check-type eieio-object-p obj)
452 (eq (eieio--object-class-object obj) class))
454 (defun object-of-class-p (obj class)
455 "Return non-nil if OBJ is an instance of CLASS or CLASS' subclasses."
456 (eieio--check-type eieio-object-p obj)
457 ;; class will be checked one layer down
458 (child-of-class-p (eieio--object-class-object obj) class))
459 ;; Backwards compatibility
460 (defalias 'obj-of-class-p 'object-of-class-p)
462 (defun child-of-class-p (child class)
463 "Return non-nil if CHILD class is a subclass of CLASS."
464 (setq child (eieio--class-object child))
465 (eieio--check-type eieio--class-p child)
466 ;; `eieio-default-superclass' is never mentioned in eieio--class-parent,
467 ;; so we have to special case it here.
468 (or (eq class 'eieio-default-superclass)
469 (let ((p nil))
470 (setq class (eieio--class-object class))
471 (eieio--check-type eieio--class-p class)
472 (while (and child (not (eq child class)))
473 (setq p (append p (eieio--class-parent child))
474 child (pop p)))
475 (if child t))))
477 (defun object-slots (obj)
478 "Return list of slots available in OBJ."
479 (eieio--check-type eieio-object-p obj)
480 (eieio--class-public-a (eieio--object-class-object obj)))
482 (defun eieio--class-slot-initarg (class slot) "Fetch from CLASS, SLOT's :initarg."
483 (eieio--check-type eieio--class-p class)
484 (let ((ia (eieio--class-initarg-tuples class))
485 (f nil))
486 (while (and ia (not f))
487 (if (eq (cdr (car ia)) slot)
488 (setq f (car (car ia))))
489 (setq ia (cdr ia)))
492 ;;; Object Set macros
494 (defmacro oset (obj slot value)
495 "Set the value in OBJ for slot SLOT to VALUE.
496 SLOT is the slot name as specified in `defclass' or the tag created
497 with in the :initarg slot. VALUE can be any Lisp object."
498 (declare (debug (form symbolp form)))
499 `(eieio-oset ,obj (quote ,slot) ,value))
501 (defmacro oset-default (class slot value)
502 "Set the default slot in CLASS for SLOT to VALUE.
503 The default value is usually set with the :initform tag during class
504 creation. This allows users to change the default behavior of classes
505 after they are created."
506 (declare (debug (form symbolp form)))
507 `(eieio-oset-default ,class (quote ,slot) ,value))
509 ;;; CLOS queries into classes and slots
511 (defun slot-boundp (object slot)
512 "Return non-nil if OBJECT's SLOT is bound.
513 Setting a slot's value makes it bound. Calling `slot-makeunbound' will
514 make a slot unbound.
515 OBJECT can be an instance or a class."
516 ;; Skip typechecking while retrieving this value.
517 (let ((eieio-skip-typecheck t))
518 ;; Return nil if the magic symbol is in there.
519 (not (eq (cond
520 ((eieio-object-p object) (eieio-oref object slot))
521 ((class-p object) (eieio-oref-default object slot))
522 (t (signal 'wrong-type-argument (list 'eieio-object-p object))))
523 eieio-unbound))))
525 (defun slot-makeunbound (object slot)
526 "In OBJECT, make SLOT unbound."
527 (eieio-oset object slot eieio-unbound))
529 (defun slot-exists-p (object-or-class slot)
530 "Return non-nil if OBJECT-OR-CLASS has SLOT."
531 (let ((cv (cond ((eieio-object-p object-or-class)
532 (eieio--object-class-object object-or-class))
533 (t (eieio-class-object object-or-class)))))
534 (or (memq slot (eieio--class-public-a cv))
535 (memq slot (eieio--class-class-allocation-a cv)))
538 (defun find-class (symbol &optional errorp)
539 "Return the class that SYMBOL represents.
540 If there is no class, nil is returned if ERRORP is nil.
541 If ERRORP is non-nil, `wrong-argument-type' is signaled."
542 (if (not (class-p symbol))
543 (if errorp (signal 'wrong-type-argument (list 'class-p symbol))
544 nil)
545 (eieio--class-v symbol)))
547 ;;; Slightly more complex utility functions for objects
549 (defun object-assoc (key slot list)
550 "Return an object if KEY is `equal' to SLOT's value of an object in LIST.
551 LIST is a list of objects whose slots are searched.
552 Objects in LIST do not need to have a slot named SLOT, nor does
553 SLOT need to be bound. If these errors occur, those objects will
554 be ignored."
555 (eieio--check-type listp list)
556 (while (and list (not (condition-case nil
557 ;; This prevents errors for missing slots.
558 (equal key (eieio-oref (car list) slot))
559 (error nil))))
560 (setq list (cdr list)))
561 (car list))
563 (defun object-assoc-list (slot list)
564 "Return an association list with the contents of SLOT as the key element.
565 LIST must be a list of objects with SLOT in it.
566 This is useful when you need to do completing read on an object group."
567 (eieio--check-type listp list)
568 (let ((assoclist nil))
569 (while list
570 (setq assoclist (cons (cons (eieio-oref (car list) slot)
571 (car list))
572 assoclist))
573 (setq list (cdr list)))
574 (nreverse assoclist)))
576 (defun object-assoc-list-safe (slot list)
577 "Return an association list with the contents of SLOT as the key element.
578 LIST must be a list of objects, but those objects do not need to have
579 SLOT in it. If it does not, then that element is left out of the association
580 list."
581 (eieio--check-type listp list)
582 (let ((assoclist nil))
583 (while list
584 (if (slot-exists-p (car list) slot)
585 (setq assoclist (cons (cons (eieio-oref (car list) slot)
586 (car list))
587 assoclist)))
588 (setq list (cdr list)))
589 (nreverse assoclist)))
591 (defun object-add-to-list (object slot item &optional append)
592 "In OBJECT's SLOT, add ITEM to the list of elements.
593 Optional argument APPEND indicates we need to append to the list.
594 If ITEM already exists in the list in SLOT, then it is not added.
595 Comparison is done with `equal' through the `member' function call.
596 If SLOT is unbound, bind it to the list containing ITEM."
597 (let (ov)
598 ;; Find the originating list.
599 (if (not (slot-boundp object slot))
600 (setq ov (list item))
601 (setq ov (eieio-oref object slot))
602 ;; turn it into a list.
603 (unless (listp ov)
604 (setq ov (list ov)))
605 ;; Do the combination
606 (if (not (member item ov))
607 (setq ov
608 (if append
609 (append ov (list item))
610 (cons item ov)))))
611 ;; Set back into the slot.
612 (eieio-oset object slot ov)))
614 (defun object-remove-from-list (object slot item)
615 "In OBJECT's SLOT, remove occurrences of ITEM.
616 Deletion is done with `delete', which deletes by side effect,
617 and comparisons are done with `equal'.
618 If SLOT is unbound, do nothing."
619 (if (not (slot-boundp object slot))
621 (eieio-oset object slot (delete item (eieio-oref object slot)))))
623 ;;; Here are some CLOS items that need the CL package
626 (gv-define-simple-setter eieio-oref eieio-oset)
630 ;; We want all objects created by EIEIO to have some default set of
631 ;; behaviors so we can create object utilities, and allow various
632 ;; types of error checking. To do this, create the default EIEIO
633 ;; class, and when no parent class is specified, use this as the
634 ;; default. (But don't store it in the other classes as the default,
635 ;; allowing for transparent support.)
638 (defclass eieio-default-superclass nil
640 "Default parent class for classes with no specified parent class.
641 Its slots are automatically adopted by classes with no specified parents.
642 This class is not stored in the `parent' slot of a class vector."
643 :abstract t)
645 (setq eieio-default-superclass (eieio--class-v 'eieio-default-superclass))
647 (defalias 'standard-class 'eieio-default-superclass)
649 (defgeneric eieio-constructor (class &rest slots)
650 "Default constructor for CLASS `eieio-default-superclass'.")
652 (define-obsolete-function-alias 'constructor #'eieio-constructor "25.1")
654 (defmethod eieio-constructor :static
655 ((class eieio-default-superclass) &rest slots)
656 "Default constructor for CLASS `eieio-default-superclass'.
657 SLOTS are the initialization slots used by `shared-initialize'.
658 This static method is called when an object is constructed.
659 It allocates the vector used to represent an EIEIO object, and then
660 calls `shared-initialize' on that object."
661 (let* ((new-object (copy-sequence (eieio--class-default-object-cache (eieio--class-v class)))))
662 ;; Call the initialize method on the new object with the slots
663 ;; that were passed down to us.
664 (initialize-instance new-object slots)
665 ;; Return the created object.
666 new-object))
668 (defgeneric shared-initialize (obj slots)
669 "Set slots of OBJ with SLOTS which is a list of name/value pairs.
670 Called from the constructor routine.")
672 (defmethod shared-initialize ((obj eieio-default-superclass) slots)
673 "Set slots of OBJ with SLOTS which is a list of name/value pairs.
674 Called from the constructor routine."
675 (eieio--with-scoped-class (eieio--object-class-object obj)
676 (while slots
677 (let ((rn (eieio--initarg-to-attribute (eieio--object-class-object obj)
678 (car slots))))
679 (if (not rn)
680 (slot-missing obj (car slots) 'oset (car (cdr slots)))
681 (eieio-oset obj rn (car (cdr slots)))))
682 (setq slots (cdr (cdr slots))))))
684 (defgeneric initialize-instance (this &optional slots)
685 "Construct the new object THIS based on SLOTS.")
687 (defmethod initialize-instance ((this eieio-default-superclass)
688 &optional slots)
689 "Construct the new object THIS based on SLOTS.
690 SLOTS is a tagged list where odd numbered elements are tags, and
691 even numbered elements are the values to store in the tagged slot.
692 If you overload the `initialize-instance', there you will need to
693 call `shared-initialize' yourself, or you can call `call-next-method'
694 to have this constructor called automatically. If these steps are
695 not taken, then new objects of your class will not have their values
696 dynamically set from SLOTS."
697 ;; First, see if any of our defaults are `lambda', and
698 ;; re-evaluate them and apply the value to our slots.
699 (let* ((this-class (eieio--object-class-object this))
700 (slot (eieio--class-public-a this-class))
701 (defaults (eieio--class-public-d this-class)))
702 (while slot
703 ;; For each slot, see if we need to evaluate it.
705 ;; Paul Landes said in an email:
706 ;; > CL evaluates it if it can, and otherwise, leaves it as
707 ;; > the quoted thing as you already have. This is by the
708 ;; > Sonya E. Keene book and other things I've look at on the
709 ;; > web.
710 (let ((dflt (eieio-default-eval-maybe (car defaults))))
711 (when (not (eq dflt (car defaults)))
712 (eieio-oset this (car slot) dflt) ))
713 ;; Next.
714 (setq slot (cdr slot)
715 defaults (cdr defaults))))
716 ;; Shared initialize will parse our slots for us.
717 (shared-initialize this slots))
719 (defgeneric slot-missing (object slot-name operation &optional new-value)
720 "Method invoked when an attempt to access a slot in OBJECT fails.")
722 (defmethod slot-missing ((object eieio-default-superclass) slot-name
723 _operation &optional _new-value)
724 "Method invoked when an attempt to access a slot in OBJECT fails.
725 SLOT-NAME is the name of the failed slot, OPERATION is the type of access
726 that was requested, and optional NEW-VALUE is the value that was desired
727 to be set.
729 This method is called from `oref', `oset', and other functions which
730 directly reference slots in EIEIO objects."
731 (signal 'invalid-slot-name (list (eieio-object-name object)
732 slot-name)))
734 (defgeneric slot-unbound (object class slot-name fn)
735 "Slot unbound is invoked during an attempt to reference an unbound slot.")
737 (defmethod slot-unbound ((object eieio-default-superclass)
738 class slot-name fn)
739 "Slot unbound is invoked during an attempt to reference an unbound slot.
740 OBJECT is the instance of the object being reference. CLASS is the
741 class of OBJECT, and SLOT-NAME is the offending slot. This function
742 throws the signal `unbound-slot'. You can overload this function and
743 return the value to use in place of the unbound value.
744 Argument FN is the function signaling this error.
745 Use `slot-boundp' to determine if a slot is bound or not.
747 In CLOS, the argument list is (CLASS OBJECT SLOT-NAME), but
748 EIEIO can only dispatch on the first argument, so the first two are swapped."
749 (signal 'unbound-slot (list (eieio-class-name class) (eieio-object-name object)
750 slot-name fn)))
752 (defgeneric clone (obj &rest params)
753 "Make a copy of OBJ, and then supply PARAMS.
754 PARAMS is a parameter list of the same form used by `initialize-instance'.
756 When overloading `clone', be sure to call `call-next-method'
757 first and modify the returned object.")
759 (defmethod clone ((obj eieio-default-superclass) &rest params)
760 "Make a copy of OBJ, and then apply PARAMS."
761 (let ((nobj (copy-sequence obj)))
762 (if (stringp (car params))
763 (funcall (if eieio-backward-compatibility #'ignore #'message)
764 "Obsolete name %S passed to clone" (pop params)))
765 (if params (shared-initialize nobj params))
766 nobj))
768 (defgeneric destructor (this &rest params)
769 "Destructor for cleaning up any dynamic links to our object.")
771 (defmethod destructor ((_this eieio-default-superclass) &rest _params)
772 "Destructor for cleaning up any dynamic links to our object.
773 Argument THIS is the object being destroyed. PARAMS are additional
774 ignored parameters."
775 ;; No cleanup... yet.
778 (defgeneric object-print (this &rest strings)
779 "Pretty printer for object THIS. Call function `object-name' with STRINGS.
781 It is sometimes useful to put a summary of the object into the
782 default #<notation> string when using EIEIO browsing tools.
783 Implement this method to customize the summary.")
785 (defmethod object-print ((this eieio-default-superclass) &rest strings)
786 "Pretty printer for object THIS. Call function `object-name' with STRINGS.
787 The default method for printing object THIS is to use the
788 function `object-name'.
790 It is sometimes useful to put a summary of the object into the
791 default #<notation> string when using EIEIO browsing tools.
793 Implement this function and specify STRINGS in a call to
794 `call-next-method' to provide additional summary information.
795 When passing in extra strings from child classes, always remember
796 to prepend a space."
797 (eieio-object-name this (apply #'concat strings)))
799 (defvar eieio-print-depth 0
800 "When printing, keep track of the current indentation depth.")
802 (defgeneric object-write (this &optional comment)
803 "Write out object THIS to the current stream.
804 Optional COMMENT will add comments to the beginning of the output.")
806 (defmethod object-write ((this eieio-default-superclass) &optional comment)
807 "Write object THIS out to the current stream.
808 This writes out the vector version of this object. Complex and recursive
809 object are discouraged from being written.
810 If optional COMMENT is non-nil, include comments when outputting
811 this object."
812 (when comment
813 (princ ";; Object ")
814 (princ (eieio-object-name-string this))
815 (princ "\n")
816 (princ comment)
817 (princ "\n"))
818 (let* ((cl (eieio-object-class this))
819 (cv (eieio--class-v cl)))
820 ;; Now output readable lisp to recreate this object
821 ;; It should look like this:
822 ;; (<constructor> <name> <slot> <slot> ... )
823 ;; Each slot's slot is writen using its :writer.
824 (princ (make-string (* eieio-print-depth 2) ? ))
825 (princ "(")
826 (princ (symbol-name (class-constructor (eieio-object-class this))))
827 (princ " ")
828 (prin1 (eieio-object-name-string this))
829 (princ "\n")
830 ;; Loop over all the public slots
831 (let ((publa (eieio--class-public-a cv))
832 (publd (eieio--class-public-d cv))
833 (publp (eieio--class-public-printer cv))
834 (eieio-print-depth (1+ eieio-print-depth)))
835 (while publa
836 (when (slot-boundp this (car publa))
837 (let ((i (eieio--class-slot-initarg cv (car publa)))
838 (v (eieio-oref this (car publa)))
840 (unless (or (not i) (equal v (car publd)))
841 (unless (bolp)
842 (princ "\n"))
843 (princ (make-string (* eieio-print-depth 2) ? ))
844 (princ (symbol-name i))
845 (if (car publp)
846 ;; Use our public printer
847 (progn
848 (princ " ")
849 (funcall (car publp) v))
850 ;; Use our generic override prin1 function.
851 (princ (if (or (eieio-object-p v)
852 (eieio-object-p (car-safe v)))
853 "\n" " "))
854 (eieio-override-prin1 v)))))
855 (setq publa (cdr publa) publd (cdr publd)
856 publp (cdr publp))))
857 (princ ")")
858 (when (= eieio-print-depth 0)
859 (princ "\n"))))
861 (defun eieio-override-prin1 (thing)
862 "Perform a `prin1' on THING taking advantage of object knowledge."
863 (cond ((eieio-object-p thing)
864 (object-write thing))
865 ((consp thing)
866 (eieio-list-prin1 thing))
867 ((class-p thing)
868 (princ (eieio-class-name thing)))
869 ((or (keywordp thing) (booleanp thing))
870 (prin1 thing))
871 ((symbolp thing)
872 (princ (concat "'" (symbol-name thing))))
873 (t (prin1 thing))))
875 (defun eieio-list-prin1 (list)
876 "Display LIST where list may contain objects."
877 (if (not (eieio-object-p (car list)))
878 (progn
879 (princ "'")
880 (prin1 list))
881 (princ (make-string (* eieio-print-depth 2) ? ))
882 (princ "(list")
883 (let ((eieio-print-depth (1+ eieio-print-depth)))
884 (while list
885 (princ "\n")
886 (if (eieio-object-p (car list))
887 (object-write (car list))
888 (princ (make-string (* eieio-print-depth 2) ? ))
889 (eieio-override-prin1 (car list)))
890 (setq list (cdr list))))
891 (princ ")")))
894 ;;; Unimplemented functions from CLOS
896 (defun change-class (_obj _class)
897 "Change the class of OBJ to type CLASS.
898 This may create or delete slots, but does not affect the return value
899 of `eq'."
900 (error "EIEIO: `change-class' is unimplemented"))
902 ;; Hook ourselves into help system for describing classes and methods.
903 (add-hook 'help-fns-describe-function-functions 'eieio-help-constructor)
905 ;;; Interfacing with edebug
907 (defun eieio-edebug-prin1-to-string (print-function object &optional noescape)
908 "Display EIEIO OBJECT in fancy format.
910 Used as advice around `edebug-prin1-to-string', held in the
911 variable PRINT-FUNCTION. Optional argument NOESCAPE is passed to
912 `prin1-to-string' when appropriate."
913 (cond ((eieio--class-p object) (eieio-class-name object))
914 ((eieio-object-p object) (object-print object))
915 ((and (listp object) (or (eieio--class-p (car object))
916 (eieio-object-p (car object))))
917 (concat "(" (mapconcat
918 (lambda (x) (eieio-edebug-prin1-to-string print-function x))
919 object " ")
920 ")"))
921 (t (funcall print-function object noescape))))
923 (advice-add 'edebug-prin1-to-string
924 :around #'eieio-edebug-prin1-to-string)
927 ;;; Start of automatically extracted autoloads.
929 ;;;### (autoloads nil "eieio-custom" "eieio-custom.el" "9a908efef1720439feb6323c1dd01770")
930 ;;; Generated autoloads from eieio-custom.el
932 (autoload 'customize-object "eieio-custom" "\
933 Customize OBJ in a custom buffer.
934 Optional argument GROUP is the sub-group of slots to display.
936 \(fn OBJ &optional GROUP)" nil nil)
938 ;;;***
940 ;;;### (autoloads nil "eieio-opt" "eieio-opt.el" "e922bf7ebc7dcb272480c4ba148da1ac")
941 ;;; Generated autoloads from eieio-opt.el
943 (autoload 'eieio-browse "eieio-opt" "\
944 Create an object browser window to show all objects.
945 If optional ROOT-CLASS, then start with that, otherwise start with
946 variable `eieio-default-superclass'.
948 \(fn &optional ROOT-CLASS)" t nil)
950 (autoload 'eieio-help-class "eieio-opt" "\
951 Print help description for CLASS.
952 If CLASS is actually an object, then also display current values of that object.
954 \(fn CLASS)" nil nil)
956 (autoload 'eieio-help-constructor "eieio-opt" "\
957 Describe CTR if it is a class constructor.
959 \(fn CTR)" nil nil)
961 ;;;***
963 ;;; End of automatically extracted autoloads.
965 (provide 'eieio)
967 ;;; eieio ends here