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