Document reserved keys
[emacs.git] / lisp / emacs-lisp / eieio-base.el
blobf0fed17b7da02c30f846b8d9ae26f7bde625c672
1 ;;; eieio-base.el --- Base classes for EIEIO. -*- lexical-binding:t -*-
3 ;;; Copyright (C) 2000-2002, 2004-2005, 2007-2018 Free Software
4 ;;; Foundation, Inc.
6 ;; Author: Eric M. Ludlam <zappo@gnu.org>
7 ;; Keywords: OO, lisp
8 ;; Package: eieio
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 <https://www.gnu.org/licenses/>.
25 ;;; Commentary:
27 ;; Base classes for EIEIO. These classes perform some basic tasks
28 ;; but are generally useless on their own. To use any of these classes,
29 ;; inherit from one or more of them.
31 ;;; Code:
33 (require 'eieio)
34 (require 'seq)
35 (eval-when-compile (require 'cl-lib))
37 ;;; eieio-instance-inheritor
39 ;; Enable instance inheritance via the `clone' method.
40 ;; Works by using the `slot-unbound' method which usually throws an
41 ;; error if a slot is unbound.
42 (defclass eieio-instance-inheritor ()
43 ((parent-instance :initarg :parent-instance
44 :type eieio-instance-inheritor
45 :documentation
46 "The parent of this instance.
47 If a slot of this class is referenced, and is unbound, then the parent
48 is checked for a value.")
50 "This special class can enable instance inheritance.
51 Use `clone' to make a new object that does instance inheritance from
52 a parent instance. When a slot in the child is referenced, and has
53 not been set, use values from the parent."
54 :abstract t)
56 (cl-defmethod slot-unbound ((object eieio-instance-inheritor)
57 _class slot-name _fn)
58 "If a slot OBJECT in this CLASS is unbound, try to inherit, or throw a signal.
59 SLOT-NAME is the offending slot. FN is the function signaling the error."
60 (if (slot-boundp object 'parent-instance)
61 ;; It may not look like it, but this line recurses back into this
62 ;; method if the parent instance's slot is unbound.
63 (eieio-oref (oref object parent-instance) slot-name)
64 ;; Throw the regular signal.
65 (cl-call-next-method)))
67 (cl-defmethod clone ((obj eieio-instance-inheritor) &rest _params)
68 "Clone OBJ, initializing `:parent' to OBJ.
69 All slots are unbound, except those initialized with PARAMS."
70 (let ((nobj (cl-call-next-method)))
71 (oset nobj parent-instance obj)
72 nobj))
74 (cl-defmethod eieio-instance-inheritor-slot-boundp ((object eieio-instance-inheritor)
75 slot)
76 "Return non-nil if the instance inheritor OBJECT's SLOT is bound.
77 See `slot-boundp' for details on binding slots.
78 The instance inheritor uses unbound slots as a way of cascading cloned
79 slot values, so testing for a slot being bound requires extra steps
80 for this kind of object."
81 (if (slot-boundp object slot)
82 ;; If it is regularly bound, return t.
84 (if (slot-boundp object 'parent-instance)
85 (eieio-instance-inheritor-slot-boundp (oref object parent-instance)
86 slot)
87 nil)))
90 ;;; eieio-instance-tracker
92 ;; Track all created instances of this class.
93 ;; The class must initialize the `tracking-symbol' slot, and that
94 ;; symbol is then used to contain these objects.
95 (defclass eieio-instance-tracker ()
96 ((tracking-symbol :type symbol
97 :allocation :class
98 :documentation
99 "The symbol used to maintain a list of our instances.
100 The instance list is treated as a variable, with new instances added to it.")
102 "This special class enables instance tracking.
103 Inheritors from this class must overload `tracking-symbol' which is
104 a variable symbol used to store a list of all instances."
105 :abstract t)
107 (cl-defmethod initialize-instance :after ((this eieio-instance-tracker)
108 &rest _slots)
109 "Make sure THIS is in our master list of this class.
110 Optional argument SLOTS are the initialization arguments."
111 ;; Theoretically, this is never called twice for a given instance.
112 (let ((sym (oref this tracking-symbol)))
113 (if (not (memq this (symbol-value sym)))
114 (set sym (append (symbol-value sym) (list this))))))
116 (cl-defmethod delete-instance ((this eieio-instance-tracker))
117 "Remove THIS from the master list of this class."
118 (set (oref this tracking-symbol)
119 (delq this (symbol-value (oref this tracking-symbol)))))
121 ;; In retrospect, this is a silly function.
122 (defun eieio-instance-tracker-find (key slot list-symbol)
123 "Find KEY as an element of SLOT in the objects in LIST-SYMBOL.
124 Returns the first match."
125 (object-assoc key slot (symbol-value list-symbol)))
127 ;;; eieio-singleton
129 ;; The singleton Design Pattern specifies that there is but one object
130 ;; of a given class ever created. The EIEIO singleton base class defines
131 ;; a CLASS allocated slot which contains the instance used. All calls to
132 ;; `make-instance' will either create a new instance and store it in this
133 ;; slot, or it will just return what is there.
134 (defclass eieio-singleton ()
135 ((singleton :type eieio-singleton
136 :allocation :class
137 :documentation
138 "The only instance of this class that will be instantiated.
139 Multiple calls to `make-instance' will return this object."))
140 "This special class causes subclasses to be singletons.
141 A singleton is a class which will only ever have one instance."
142 :abstract t)
144 (cl-defmethod make-instance ((class (subclass eieio-singleton)) &rest _slots)
145 "Constructor for singleton CLASS.
146 NAME and SLOTS initialize the new object.
147 This constructor guarantees that no matter how many you request,
148 only one object ever exists."
149 ;; NOTE TO SELF: In next version, make `slot-boundp' support classes
150 ;; with class allocated slots or default values.
151 (let ((old (oref-default class singleton)))
152 (if (eq old eieio-unbound)
153 (oset-default class singleton (cl-call-next-method))
154 old)))
157 ;;; eieio-persistent
159 ;; For objects which must save themselves to disk. Provides an
160 ;; `object-write' method to save an object to disk, and a
161 ;; `eieio-persistent-read' function to call to read an object
162 ;; from disk.
164 ;; Also provide the method `eieio-persistent-path-relative' to
165 ;; calculate path names relative to a given instance. This will
166 ;; make the saved object location independent by converting all file
167 ;; references to be relative to the directory the object is saved to.
168 ;; You must call `eieio-persistent-path-relative' on each file name
169 ;; saved in your object.
170 (defclass eieio-persistent ()
171 ((file :initarg :file
172 :type string
173 :documentation
174 "The save file for this persistent object.
175 This must be a string, and must be specified when the new object is
176 instantiated.")
177 (extension :type string
178 :allocation :class
179 :initform ".eieio"
180 :documentation
181 "Extension of files saved by this object.
182 Enables auto-choosing nice file names based on name.")
183 (file-header-line :type string
184 :allocation :class
185 :initform ";; EIEIO PERSISTENT OBJECT"
186 :documentation
187 "Header line for the save file.
188 This is used with the `object-write' method.")
189 (do-backups :type boolean
190 :allocation :class
191 :initform t
192 :documentation
193 "Saving this object should make backup files.
194 Setting to nil will mean no backups are made."))
195 "This special class enables persistence through save files
196 Use the `object-save' method to write this object to disk. The save
197 format is Emacs Lisp code which calls the constructor for the saved
198 object. For this reason, only slots which do not have an `:initarg'
199 specified will not be saved."
200 :abstract t)
202 (cl-defmethod eieio-persistent-save-interactive ((this eieio-persistent) prompt
203 &optional name)
204 "Prepare to save THIS. Use in an `interactive' statement.
205 Query user for file name with PROMPT if THIS does not yet specify
206 a file. Optional argument NAME specifies a default file name."
207 (unless (slot-boundp this 'file)
208 (oset this file
209 (read-file-name prompt nil
210 (if name
211 (concat name (oref this extension))
212 ))))
213 (oref this file))
215 (defun eieio-persistent-read (filename &optional class allow-subclass)
216 "Read a persistent object from FILENAME, and return it.
217 Signal an error if the object in FILENAME is not a constructor
218 for CLASS. Optional ALLOW-SUBCLASS says that it is ok for
219 `eieio-persistent-read' to load in subclasses of class instead of
220 being pedantic."
221 (unless class
222 (message "Unsafe call to `eieio-persistent-read'."))
223 (when class (cl-check-type class class))
224 (let ((ret nil)
225 (buffstr nil))
226 (unwind-protect
227 (progn
228 (with-current-buffer (get-buffer-create " *tmp eieio read*")
229 (insert-file-contents filename nil nil nil t)
230 (goto-char (point-min))
231 (setq buffstr (buffer-string)))
232 ;; Do the read in the buffer the read was initialized from
233 ;; so that any initialize-instance calls that depend on
234 ;; the current buffer will work.
235 (setq ret (read buffstr))
236 (when (not (child-of-class-p (car ret) 'eieio-persistent))
237 (error "Corrupt object on disk: Unknown saved object"))
238 (when (and class
239 (not (or (eq (car ret) class ) ; same class
240 (and allow-subclass
241 (child-of-class-p (car ret) class)) ; subclasses
243 (error "Corrupt object on disk: Invalid saved class"))
244 (setq ret (eieio-persistent-convert-list-to-object ret))
245 (oset ret file filename))
246 (kill-buffer " *tmp eieio read*"))
247 ret))
249 (defun eieio-persistent-convert-list-to-object (inputlist)
250 "Convert the INPUTLIST, representing object creation to an object.
251 While it is possible to just `eval' the INPUTLIST, this code instead
252 validates the existing list, and explicitly creates objects instead of
253 calling eval. This avoids the possibility of accidentally running
254 malicious code.
256 Note: This function recurses when a slot of :type of some object is
257 identified, and needing more object creation."
258 (let* ((objclass (nth 0 inputlist))
259 ;; Earlier versions of `object-write' added a string name for
260 ;; the object, now obsolete.
261 (slots (nthcdr
262 (if (stringp (nth 1 inputlist)) 2 1)
263 inputlist))
264 (createslots nil)
265 (class
266 (progn
267 ;; If OBJCLASS is an eieio autoload object, then we need to
268 ;; load it.
269 (eieio-class-un-autoload objclass)
270 (eieio--class-object objclass))))
272 (while slots
273 (let ((initarg (car slots))
274 (value (car (cdr slots))))
276 ;; Make sure that the value proposed for SLOT is valid.
277 ;; In addition, strip out quotes, list functions, and update
278 ;; object constructors as needed.
279 (setq value (eieio-persistent-validate/fix-slot-value
280 class (eieio--initarg-to-attribute class initarg) value))
282 (push initarg createslots)
283 (push value createslots)
286 (setq slots (cdr (cdr slots))))
288 (apply #'make-instance objclass (nreverse createslots))
290 ;;(eval inputlist)
293 (defun eieio-persistent-validate/fix-slot-value (class slot proposed-value)
294 "Validate that in CLASS, the SLOT with PROPOSED-VALUE is good, then fix.
295 A limited number of functions, such as quote, list, and valid object
296 constructor functions are considered valid.
297 Second, any text properties will be stripped from strings."
298 (cond ((consp proposed-value)
299 ;; Lists with something in them need special treatment.
300 (let* ((slot-idx (- (eieio--slot-name-index class slot)
301 (eval-when-compile eieio--object-num-slots)))
302 (type (cl--slot-descriptor-type (aref (eieio--class-slots class)
303 slot-idx)))
304 (classtype (eieio-persistent-slot-type-is-class-p type)))
306 (cond ((eq (car proposed-value) 'quote)
307 (car (cdr proposed-value)))
309 ;; An empty list sometimes shows up as (list), which is dumb, but
310 ;; we need to support it for backward compat.
311 ((and (eq (car proposed-value) 'list)
312 (= (length proposed-value) 1))
313 nil)
315 ;; List of object constructors.
316 ((and (eq (car proposed-value) 'list)
317 ;; 2nd item is a list.
318 (consp (car (cdr proposed-value)))
319 ;; 1st elt of 2nd item is a class name.
320 (class-p (car (car (cdr proposed-value))))
323 ;; Check the value against the input class type.
324 ;; If something goes wrong, issue a smart warning
325 ;; about how a :type is needed for this to work.
326 (unless (and
327 ;; Do we have a type?
328 (consp classtype) (class-p (car classtype)))
329 (error "In save file, list of object constructors found, but no :type specified for slot %S of type %S"
330 slot classtype))
332 ;; We have a predicate, but it doesn't satisfy the predicate?
333 (dolist (PV (cdr proposed-value))
334 (unless (child-of-class-p (car PV) (car classtype))
335 (error "Corrupt object on disk")))
337 ;; We have a list of objects here. Lets load them
338 ;; in.
339 (let ((objlist nil))
340 (dolist (subobj (cdr proposed-value))
341 (push (eieio-persistent-convert-list-to-object subobj)
342 objlist))
343 ;; return the list of objects ... reversed.
344 (nreverse objlist)))
345 ;; We have a slot with a single object that can be
346 ;; saved here. Recurse and evaluate that
347 ;; sub-object.
348 ((and classtype
349 (seq-some
350 (lambda (elt)
351 (child-of-class-p (car proposed-value) elt))
352 classtype))
353 (eieio-persistent-convert-list-to-object
354 proposed-value))
356 proposed-value))))
357 ;; For hash-tables and vectors, the top-level `read' will not
358 ;; "look inside" member values, so we need to do that
359 ;; explicitly.
360 ((hash-table-p proposed-value)
361 (maphash
362 (lambda (key value)
363 (when (class-p (car-safe value))
364 (setf (gethash key proposed-value)
365 (eieio-persistent-convert-list-to-object
366 value))))
367 proposed-value)
368 proposed-value)
370 ((vectorp proposed-value)
371 (dotimes (i (length proposed-value))
372 (when (class-p (car-safe (aref proposed-value i)))
373 (aset proposed-value i
374 (eieio-persistent-convert-list-to-object
375 (aref proposed-value i)))))
376 proposed-value)
378 ((stringp proposed-value)
379 ;; Else, check for strings, remove properties.
380 (substring-no-properties proposed-value))
383 ;; Else, just return whatever the constant was.
384 proposed-value))
387 (defun eieio-persistent-slot-type-is-class-p (type)
388 "Return the class referred to in TYPE.
389 If no class is referenced there, then return nil."
390 (cond ((class-p type)
391 ;; If the type is a class, then return it.
392 type)
393 ((and (eq 'list-of (car-safe type)) (class-p (cadr type)))
394 ;; If it is the type of a list of a class, then return that class and
395 ;; the type.
396 (cons (cadr type) type))
398 ((and (symbolp type) (get type 'cl-deftype-handler))
399 ;; Macro-expand the type according to cl-deftype definitions.
400 (eieio-persistent-slot-type-is-class-p
401 (funcall (get type 'cl-deftype-handler))))
403 ;; FIXME: foo-child should not be a valid type!
404 ((and (symbolp type) (string-match "-child\\'" (symbol-name type))
405 (class-p (intern-soft (substring (symbol-name type) 0
406 (match-beginning 0)))))
407 (unless eieio-backward-compatibility
408 (error "Use of bogus %S type instead of %S"
409 type (intern-soft (substring (symbol-name type) 0
410 (match-beginning 0)))))
411 ;; If it is the predicate ending with -child, then return
412 ;; that class. Unfortunately, in EIEIO, typep of just the
413 ;; class is the same as if we used -child, so no further work needed.
414 (intern-soft (substring (symbol-name type) 0
415 (match-beginning 0))))
416 ;; FIXME: foo-list should not be a valid type!
417 ((and (symbolp type) (string-match "-list\\'" (symbol-name type))
418 (class-p (intern-soft (substring (symbol-name type) 0
419 (match-beginning 0)))))
420 (unless eieio-backward-compatibility
421 (error "Use of bogus %S type instead of (list-of %S)"
422 type (intern-soft (substring (symbol-name type) 0
423 (match-beginning 0)))))
424 ;; If it is the predicate ending with -list, then return
425 ;; that class and the predicate to use.
426 (cons (intern-soft (substring (symbol-name type) 0
427 (match-beginning 0)))
428 type))
430 ((eq (car-safe type) 'or)
431 ;; If type is a list, and is an `or', return all valid class
432 ;; types within the `or' statement.
433 (seq-filter #'eieio-persistent-slot-type-is-class-p (cdr type)))
436 ;; No match, not a class.
437 nil)))
439 (cl-defmethod object-write ((this eieio-persistent) &optional comment)
440 "Write persistent object THIS out to the current stream.
441 Optional argument COMMENT is a header line comment."
442 (cl-call-next-method this (or comment (oref this file-header-line))))
444 (cl-defmethod eieio-persistent-path-relative ((this eieio-persistent) file)
445 "For object THIS, make absolute file name FILE relative."
446 (file-relative-name (expand-file-name file)
447 (file-name-directory (oref this file))))
449 (cl-defmethod eieio-persistent-save ((this eieio-persistent) &optional file)
450 "Save persistent object THIS to disk.
451 Optional argument FILE overrides the file name specified in the object
452 instance."
453 (when file (setq file (expand-file-name file)))
454 (with-temp-buffer
455 (let* ((cfn (or file (oref this file)))
456 (default-directory (file-name-directory cfn)))
457 (cl-letf ((standard-output (current-buffer))
458 ((oref this file) ;FIXME: Why change it?
459 (if file
460 ;; FIXME: Makes a name relative to (oref this file),
461 ;; whereas I think it should be relative to cfn.
462 (eieio-persistent-path-relative this file)
463 (file-name-nondirectory cfn))))
464 (object-write this (oref this file-header-line)))
465 (let ((backup-inhibited (not (oref this do-backups)))
466 (coding-system-for-write 'utf-8-emacs))
467 ;; Old way - write file. Leaves message behind.
468 ;;(write-file cfn nil)
470 ;; New way - Avoid the vast quantities of error checking
471 ;; just so I can get at the special flags that disable
472 ;; displaying random messages.
473 (write-region (point-min) (point-max) cfn nil 1)
474 ))))
476 ;; Notes on the persistent object:
477 ;; It should also set up some hooks to help it keep itself up to date.
480 ;;; Named object
482 (defclass eieio-named ()
483 ((object-name :initarg :object-name :initform nil))
484 "Object with a name."
485 :abstract t)
487 (cl-defmethod eieio-object-name-string ((obj eieio-named))
488 "Return a string which is OBJ's name."
489 (or (slot-value obj 'object-name)
490 (symbol-name (eieio-object-class obj))))
492 (cl-defmethod eieio-object-set-name-string ((obj eieio-named) name)
493 "Set the string which is OBJ's NAME."
494 (cl-check-type name string)
495 (eieio-oset obj 'object-name name))
497 (cl-defmethod clone ((obj eieio-named) &rest params)
498 "Clone OBJ, initializing `:parent' to OBJ.
499 All slots are unbound, except those initialized with PARAMS."
500 (let* ((newname (and (stringp (car params)) (pop params)))
501 (nobj (apply #'cl-call-next-method obj params))
502 (nm (slot-value obj 'object-name)))
503 (eieio-oset obj 'object-name
504 (or newname
505 (save-match-data
506 (if (and nm (string-match "-\\([0-9]+\\)" nm))
507 (let ((num (1+ (string-to-number
508 (match-string 1 nm)))))
509 (concat (substring nm 0 (match-beginning 0))
510 "-" (int-to-string num)))
511 (concat nm "-1")))))
512 nobj))
514 (cl-defmethod make-instance ((class (subclass eieio-named)) &rest args)
515 (if (not (stringp (car args)))
516 (cl-call-next-method)
517 (funcall (if eieio-backward-compatibility #'ignore #'message)
518 "Obsolete: name passed without :object-name to %S constructor"
519 class)
520 (apply #'cl-call-next-method class :object-name args)))
523 (provide 'eieio-base)
525 ;;; eieio-base.el ends here