]> code.delx.au - gnu-emacs/blob - doc/misc/eieio.texi
fb4e1470016f44a219c2fdfd17449e164df018b8
[gnu-emacs] / doc / misc / eieio.texi
1 \input texinfo
2 @setfilename ../../info/eieio.info
3 @set TITLE Enhanced Implementation of Emacs Interpreted Objects
4 @set AUTHOR Eric M. Ludlam
5 @settitle @value{TITLE}
6 @include docstyle.texi
7
8 @c *************************************************************************
9 @c @ Header
10 @c *************************************************************************
11
12 @copying
13 This manual documents EIEIO, an object framework for Emacs Lisp.
14
15 Copyright @copyright{} 2007--2015 Free Software Foundation, Inc.
16
17 @quotation
18 Permission is granted to copy, distribute and/or modify this document
19 under the terms of the GNU Free Documentation License, Version 1.3 or
20 any later version published by the Free Software Foundation; with no
21 Invariant Sections, with the Front-Cover Texts being ``A GNU Manual,''
22 and with the Back-Cover Texts as in (a) below. A copy of the license
23 is included in the section entitled ``GNU Free Documentation License.''
24
25 (a) The FSF's Back-Cover Text is: ``You have the freedom to copy and
26 modify this GNU manual.''
27 @end quotation
28 @end copying
29
30 @dircategory Emacs misc features
31 @direntry
32 * EIEIO: (eieio). An objects system for Emacs Lisp.
33 @end direntry
34
35 @titlepage
36 @center @titlefont{@value{TITLE}}
37 @sp 4
38 @center by @value{AUTHOR}
39 @page
40 @vskip 0pt plus 1filll
41 @insertcopying
42 @end titlepage
43
44 @macro eieio{}
45 @i{EIEIO}
46 @end macro
47
48 @node Top
49 @top EIEIO
50
51 @eieio{} (``Enhanced Implementation of Emacs Interpreted Objects'')
52 provides an Object Oriented layer for Emacs Lisp, following the basic
53 concepts of the Common Lisp Object System (CLOS). It provides a
54 framework for writing object-oriented applications in Emacs.
55
56 @ifnottex
57 @insertcopying
58 @end ifnottex
59
60 @menu
61 * Quick Start:: Quick start for EIEIO.
62 * Introduction:: Why use @eieio{}? Basic overview, samples list.
63 * Building Classes:: How to write new class structures.
64 * Making New Objects:: How to construct new objects.
65 * Accessing Slots:: How to access a slot.
66 * Writing Methods:: How to write a method.
67 * Method Invocation:: How methods are invoked.
68 * Predicates:: Class-p, Object-p, etc-p.
69 * Association Lists:: List of objects as association lists.
70 * Customizing:: Customizing objects.
71 * Introspection:: Looking inside a class.
72 * Base Classes:: Additional classes you can inherit from.
73 * Browsing:: Browsing your class lists.
74 * Class Values:: Displaying information about a class or object.
75 * Default Superclass:: The root superclasses.
76 * Signals:: When you make errors.
77 * Naming Conventions:: Name your objects in an Emacs friendly way.
78 * CLOS compatibility:: What are the differences?
79 * Wish List:: Things about EIEIO that could be improved.
80 * GNU Free Documentation License:: The license for this documentation.
81 * Function Index::
82 @end menu
83
84 @node Quick Start
85 @chapter Quick Start
86
87 @eieio{} provides an Object Oriented layer for Emacs Lisp. You can
88 use @eieio{} to create classes, methods for those classes, and
89 instances of classes.
90
91 Here is a simple example of a class named @code{record}, containing
92 three slots named @code{name}, @code{birthday}, and @code{phone}:
93
94 @example
95 (defclass record () ; No superclasses
96 ((name :initarg :name
97 :initform ""
98 :type string
99 :custom string
100 :documentation "The name of a person.")
101 (birthday :initarg :birthday
102 :initform "Jan 1, 1970"
103 :custom string
104 :type string
105 :documentation "The person's birthday.")
106 (phone :initarg :phone
107 :initform ""
108 :documentation "Phone number."))
109 "A single record for tracking people I know.")
110 @end example
111
112 Each class can have methods, which are defined like this:
113
114 @example
115 (defmethod call-record ((rec record) &optional scriptname)
116 "Dial the phone for the record REC.
117 Execute the program SCRIPTNAME to dial the phone."
118 (message "Dialing the phone for %s" (oref rec name))
119 (shell-command (concat (or scriptname "dialphone.sh")
120 " "
121 (oref rec phone))))
122 @end example
123
124 @noindent
125 In this example, the first argument to @code{call-record} is a list,
126 of the form (@var{varname} @var{classname}). @var{varname} is the
127 name of the variable used for the first argument; @var{classname} is
128 the name of the class that is expected as the first argument for this
129 method.
130
131 @eieio{} dispatches methods based on the type of the first argument.
132 You can have multiple methods with the same name for different classes
133 of object. When the @code{call-record} method is called, the first
134 argument is examined to determine the class of that argument, and the
135 method matching the input type is then executed.
136
137 Once the behavior of a class is defined, you can create a new
138 object of type @code{record}. Objects are created by calling the
139 constructor. The constructor is a function with the same name as your
140 class which returns a new instance of that class. Here is an example:
141
142 @example
143 (setq rec (record :name "Eric" :birthday "June" :phone "555-5555"))
144 @end example
145
146 @noindent
147 For backward compatibility reasons, the first argument can be a string (a name
148 given to this instance). Each instance used to be given a name, so different
149 instances could be easily distinguished when debugging.
150
151 It can be a bit repetitive to also have a :name slot. To avoid doing
152 this, it is sometimes handy to use the base class @code{eieio-named}.
153 @xref{eieio-named}.
154
155 Calling methods on an object is a lot like calling any function. The
156 first argument should be an object of a class which has had this
157 method defined for it. In this example it would look like this:
158
159 @example
160 (call-record rec)
161 @end example
162
163 @noindent
164 or
165
166 @example
167 (call-record rec "my-call-script")
168 @end example
169
170 In these examples, @eieio{} automatically examines the class of
171 @code{rec}, and ensures that the method defined above is called. If
172 @code{rec} is some other class lacking a @code{call-record} method, or
173 some other data type, Emacs signals a @code{no-method-definition}
174 error. @ref{Signals}.
175
176 @node Introduction
177 @chapter Introduction
178
179 First off, please note that this manual cannot serve as a complete
180 introduction to object oriented programming and generic functions in
181 LISP@. Although EIEIO is not a complete implementation of the Common
182 Lisp Object System (CLOS) and also differs from it in several aspects,
183 it follows the same basic concepts. Therefore, it is highly
184 recommended to learn those from a textbook or tutorial first,
185 especially if you only know OOP from languages like C++ or Java. If
186 on the other hand you are already familiar with CLOS, you should be
187 aware that @eieio{} does not implement the full CLOS specification and
188 also differs in some other aspects which are mentioned below (also
189 @pxref{CLOS compatibility}).
190
191 @eieio{} supports the following features:
192
193 @enumerate
194 @item
195 A structured framework for the creation of basic classes with attributes
196 and methods using singular inheritance similar to CLOS.
197 @item
198 Type checking, and slot unbinding.
199 @item
200 Method definitions similar to CLOS.
201 @item
202 Simple and complex class browsers.
203 @item
204 Edebug support for methods.
205 @item
206 Imenu updates.
207 @item
208 Byte compilation support of methods.
209 @item
210 Help system extensions for classes and methods.
211 @item
212 Several base classes for interesting tasks.
213 @item
214 Simple test suite.
215 @item
216 Public and private classifications for slots (extensions to CLOS)
217 @item
218 Customization support in a class (extension to CLOS)
219 @end enumerate
220
221 Due to restrictions in the Emacs Lisp language, CLOS cannot be
222 completely supported, and a few functions have been added in place of
223 setf. Here are some important CLOS features that @eieio{} presently
224 lacks:
225
226 @table @asis
227
228 @item Method dispatch
229 EIEO does not support method dispatch for built-in types and multiple
230 arguments types. In other words, method dispatch only looks at the
231 first argument, and this one must be an @eieio{} type.
232
233 @item Support for metaclasses
234 There is just one default metaclass, @code{eieio-default-superclass},
235 and you cannot define your own. The @code{:metaclass} tag in
236 @code{defclass} is ignored. Also, functions like @code{find-class}, which
237 should return instances of the metaclass, behave differently in
238 @eieio{} in that they return symbols or plain structures instead.
239
240 @item EQL specialization
241 EIEIO does not support it.
242
243 @item @code{:around} method tag
244 This CLOS method tag is non-functional.
245
246 @item :default-initargs in @code{defclass}
247 Each slot can have an @code{:initform} tag, so this is not really necessary.
248
249 @item Mock object initializers
250 Each class contains a mock object used for fast initialization of
251 instantiated objects. Using functions with side effects on object slot
252 values can potentially cause modifications in the mock object. @eieio{}
253 should use a deep copy but currently does not.
254
255 @end table
256
257 @node Building Classes
258 @chapter Building Classes
259
260 A @dfn{class} is a definition for organizing data and methods
261 together. An @eieio{} class has structures similar to the classes
262 found in other object-oriented (OO) languages.
263
264 To create a new class, use the @code{defclass} macro:
265
266 @defmac defclass class-name superclass-list slot-list &rest options-and-doc
267
268 Create a new class named @var{class-name}. The class is represented
269 by a symbol with the name @var{class-name}. @eieio{} stores the structure of
270 the class as a symbol property of @var{class-name} (@pxref{Symbol
271 Components,,,elisp,GNU Emacs Lisp Reference Manual}).
272
273 The @var{class-name} symbol's variable documentation string is a
274 modified version of the doc string found in @var{options-and-doc}.
275 Each time a method is defined, the symbol's documentation string is
276 updated to include the methods documentation as well.
277
278 The parent classes for @var{class-name} is @var{superclass-list}.
279 Each element of @var{superclass-list} must be a class. These classes
280 are the parents of the class being created. Every slot that appears
281 in each parent class is replicated in the new class.
282
283 If two parents share the same slot name, the parent which appears in
284 the @var{superclass-list} first sets the tags for that slot. If the
285 new class has a slot with the same name as the parent, the new slot
286 overrides the parent's slot.
287
288 When overriding a slot, some slot attributes cannot be overridden
289 because they break basic OO rules. You cannot override @code{:type}
290 or @code{:protection}.
291 @end defmac
292
293 @noindent
294 Whenever defclass is used to create a new class, a predicate is
295 created for it, named @code{@var{CLASS-NAME}-p}:
296
297 @defun CLASS-NAME-p object
298 Return non-@code{nil} if and only if @var{OBJECT} is of the class
299 @var{CLASS-NAME}.
300 @end defun
301
302 @defvar eieio-error-unsupported-class-tags
303 If non-@code{nil}, @code{defclass} signals an error if a tag in a slot
304 specifier is unsupported.
305
306 This option is here to support programs written with older versions of
307 @eieio{}, which did not produce such errors.
308 @end defvar
309
310 @menu
311 * Inheritance:: How to specify parents classes.
312 * Slot Options:: How to specify features of a slot.
313 * Class Options:: How to specify features for this class.
314 @end menu
315
316 @node Inheritance
317 @section Inheritance
318
319 @dfn{Inheritance} is a basic feature of an object-oriented language.
320 In @eieio{}, a defined class specifies the super classes from which it
321 inherits by using the second argument to @code{defclass}. Here is an
322 example:
323
324 @example
325 (defclass my-baseclass ()
326 ((slot-A :initarg :slot-A)
327 (slot-B :initarg :slot-B))
328 "My Baseclass.")
329 @end example
330
331 @noindent
332 To subclass from @code{my-baseclass}, we specify it in the superclass
333 list:
334
335 @example
336 (defclass my-subclass (my-baseclass)
337 ((specific-slot-A :initarg specific-slot-A)
338 )
339 "My subclass of my-baseclass")
340 @end example
341
342 @indent
343 Instances of @code{my-subclass} will inherit @code{slot-A} and
344 @code{slot-B}, in addition to having @code{specific-slot-A} from the
345 declaration of @code{my-subclass}.
346
347 @eieio{} also supports multiple inheritance. Suppose we define a
348 second baseclass, perhaps an ``interface'' class, like this:
349
350 @example
351 (defclass my-interface ()
352 ((interface-slot :initarg :interface-slot))
353 "An interface to special behavior."
354 :abstract t)
355 @end example
356
357 @noindent
358 The interface class defines a special @code{interface-slot}, and also
359 specifies itself as abstract. Abstract classes cannot be
360 instantiated. It is not required to make interfaces abstract, but it
361 is a good programming practice.
362
363 We can now modify our definition of @code{my-subclass} to use this
364 interface class, together with our original base class:
365
366 @example
367 (defclass my-subclass (my-baseclass my-interface)
368 ((specific-slot-A :initarg specific-slot-A)
369 )
370 "My subclass of my-baseclass")
371 @end example
372
373 @noindent
374 With this, @code{my-subclass} also has @code{interface-slot}.
375
376 If @code{my-baseclass} and @code{my-interface} had slots with the same
377 name, then the superclass showing up in the list first defines the
378 slot attributes.
379
380 Inheritance in @eieio{} is more than just combining different slots.
381 It is also important in method invocation. @ref{Methods}.
382
383 If a method is called on an instance of @code{my-subclass}, and that
384 method only has an implementation on @code{my-baseclass}, or perhaps
385 @code{my-interface}, then the implementation for the baseclass is
386 called.
387
388 If there is a method implementation for @code{my-subclass}, and
389 another in @code{my-baseclass}, the implementation for
390 @code{my-subclass} can call up to the superclass as well.
391
392 @node Slot Options
393 @section Slot Options
394
395 The @var{slot-list} argument to @code{defclass} is a list of elements
396 where each element defines one slot. Each slot is a list of the form
397
398 @example
399 (SLOT-NAME :TAG1 ATTRIB-VALUE1
400 :TAG2 ATTRIB-VALUE2
401 :TAGN ATTRIB-VALUEN)
402 @end example
403
404 @noindent
405 where @var{SLOT-NAME} is a symbol that will be used to refer to the
406 slot. @var{:TAG} is a symbol that describes a feature to be set
407 on the slot. @var{ATTRIB-VALUE} is a lisp expression that will be
408 used for @var{:TAG}.
409
410 Valid tags are:
411
412 @table @code
413 @item :initarg
414 A symbol that can be used in the argument list of the constructor to
415 specify a value for this slot of the new instance being created.
416
417 A good symbol to use for initarg is one that starts with a colon @code{:}.
418
419 The slot specified like this:
420 @example
421 (myslot :initarg :myslot)
422 @end example
423 could then be initialized to the number 1 like this:
424 @example
425 (myobject :myslot 1)
426 @end example
427
428 @xref{Making New Objects}.
429
430 @item :initform
431 An expression used as the default value for this slot.
432
433 If @code{:initform} is left out, that slot defaults to being unbound.
434 It is an error to reference an unbound slot, so if you need
435 slots to always be in a bound state, you should always use an
436 @code{:initform} specifier.
437
438 Use @code{slot-boundp} to test if a slot is unbound
439 (@pxref{Predicates}). Use @code{slot-makeunbound} to set a slot to
440 being unbound after giving it a value (@pxref{Accessing Slots}).
441
442 The value passed to initform used to be automatically quoted. Thus,
443 @example
444 :initform (1 2 3)
445 @end example
446 will use the list as a value. This is incompatible with CLOS (which would
447 signal an error since 1 is not a valid function) and will likely change in the
448 future, so better quote your initforms if they're just values.
449
450 @item :type
451 An unquoted type specifier used to validate data set into this slot.
452 @xref{Type Predicates,,,cl,Common Lisp Extensions}.
453 Here are some examples:
454 @table @code
455 @item symbol
456 A symbol.
457 @item number
458 A number type
459 @item my-class-name
460 An object of your class type.
461 @item (or null symbol)
462 A symbol, or @code{nil}.
463 @end table
464
465 @item :allocation
466 Either :class or :instance (defaults to :instance) used to
467 specify how data is stored. Slots stored per instance have unique
468 values for each object. Slots stored per class have shared values for
469 each object. If one object changes a :class allocated slot, then all
470 objects for that class gain the new value.
471
472 @item :documentation
473 Documentation detailing the use of this slot. This documentation is
474 exposed when the user describes a class, and during customization of an
475 object.
476
477 @item :accessor
478 Name of a generic function which can be used to fetch the value of this slot.
479 You can call this function later on your object and retrieve the value
480 of the slot.
481
482 This options is in the CLOS spec, but is not fully compliant in @eieio{}.
483
484 @item :writer
485 Name of a generic function which will write this slot.
486
487 This options is in the CLOS spec, but is not fully compliant in @eieio{}.
488
489 @item :reader
490 Name of a generic function which will read this slot.
491
492 This options is in the CLOS spec, but is not fully compliant in @eieio{}.
493
494 @item :custom
495 A custom :type specifier used when editing an object of this type.
496 See documentation for @code{defcustom} for details. This specifier is
497 equivalent to the :type spec of a @code{defcustom} call.
498
499 This options is specific to Emacs, and is not in the CLOS spec.
500
501 @item :label
502 When customizing an object, the value of :label will be used instead
503 of the slot name. This enables better descriptions of the data than
504 would usually be afforded.
505
506 This options is specific to Emacs, and is not in the CLOS spec.
507
508 @item :group
509 Similar to @code{defcustom}'s :group command, this organizes different
510 slots in an object into groups. When customizing an object, only the
511 slots belonging to a specific group need be worked with, simplifying the
512 size of the display.
513
514 This options is specific to Emacs, and is not in the CLOS spec.
515
516 @item :printer
517 This routine takes a symbol which is a function name. The function
518 should accept one argument. The argument is the value from the slot
519 to be printed. The function in @code{object-write} will write the
520 slot value out to a printable form on @code{standard-output}.
521
522 The output format MUST be something that could in turn be interpreted
523 with @code{read} such that the object can be brought back in from the
524 output stream. Thus, if you wanted to output a symbol, you would need
525 to quote the symbol. If you wanted to run a function on load, you
526 can output the code to do the construction of the value.
527
528 @item :protection
529 This is an old option that is not supported any more.
530
531 When using a slot referencing function such as @code{slot-value}, and
532 the value behind @var{slot} is private or protected, then the current
533 scope of operation must be within a method of the calling object.
534
535 This protection is not enforced by the code any more, so it's only useful
536 as documentation.
537
538 Valid values are:
539
540 @table @code
541 @item :public
542 Access this slot from any scope.
543 @item :protected
544 Access this slot only from methods of the same class or a child class.
545 @item :private
546 Access this slot only from methods of the same class.
547 @end table
548
549 This options is specific to Emacs, and is not in the CLOS spec.
550
551 @end table
552
553 @node Class Options
554 @section Class Options
555
556 In the @var{options-and-doc} arguments to @code{defclass}, the
557 following class options may be specified:
558
559 @table @code
560 @item :documentation
561 A documentation string for this class.
562
563 If an Emacs-style documentation string is also provided, then this
564 option is ignored. An Emacs-style documentation string is not
565 prefixed by the @code{:documentation} tag, and appears after the list
566 of slots, and before the options.
567
568 @item :allow-nil-initform
569 If this option is non-@code{nil}, and the @code{:initform} is @code{nil}, but
570 the @code{:type} is specifies something such as @code{string} then allow
571 this to pass. The default is to have this option be off. This is
572 implemented as an alternative to unbound slots.
573
574 This options is specific to Emacs, and is not in the CLOS spec.
575
576 @item :abstract
577 A class which is @code{:abstract} cannot be instantiated, and instead
578 is used to define an interface which subclasses should implement.
579
580 This option is specific to Emacs, and is not in the CLOS spec.
581
582 @item :custom-groups
583 This is a list of groups that can be customized within this class. This
584 slot is auto-generated when a class is created and need not be
585 specified. It can be retrieved with the @code{class-option} command,
586 however, to see what groups are available.
587
588 This option is specific to Emacs, and is not in the CLOS spec.
589
590 @item :method-invocation-order
591 This controls the order in which method resolution occurs for
592 @code{:primary} methods in cases of multiple inheritance. The order
593 affects which method is called first in a tree, and if
594 @code{call-next-method} is used, it controls the order in which the
595 stack of methods are run.
596
597 Valid values are:
598
599 @table @code
600 @item :breadth-first
601 Search for methods in the class hierarchy in breadth first order.
602 This is the default.
603 @item :depth-first
604 Search for methods in the class hierarchy in a depth first order.
605 @item :c3
606 Searches for methods in a linearized way that most closely matches
607 what CLOS does when a monotonic class structure is defined.
608 @end table
609
610 @xref{Method Invocation}, for more on method invocation order.
611
612 @item :metaclass
613 Unsupported CLOS option. Enables the use of a different base class other
614 than @code{standard-class}.
615
616 @item :default-initargs
617 Unsupported CLOS option. Specifies a list of initargs to be used when
618 creating new objects. As far as I can tell, this duplicates the
619 function of @code{:initform}.
620 @end table
621
622 @xref{CLOS compatibility}, for more details on CLOS tags versus
623 @eieio{}-specific tags.
624
625 @node Making New Objects
626 @chapter Making New Objects
627
628 Suppose we have a simple class is defined, such as:
629
630 @example
631 (defclass record ()
632 ( ) "Doc String")
633 @end example
634
635 @noindent
636 It is now possible to create objects of that class type.
637
638 Calling @code{defclass} has defined two new functions. One is the
639 constructor @var{record}, and the other is the predicate,
640 @var{record}-p.
641
642 @defun record object-name &rest slots
643
644 This creates and returns a new object. This object is not assigned to
645 anything, and will be garbage collected if not saved. This object
646 will be given the string name @var{object-name}. There can be
647 multiple objects of the same name, but the name slot provides a handy
648 way to keep track of your objects. @var{slots} is just all the slots
649 you wish to preset. Any slot set as such @emph{will not} get its
650 default value, and any side effects from a slot's @code{:initform}
651 that may be a function will not occur.
652
653 An example pair would appear simply as @code{:value 1}. Of course you
654 can do any valid Lispy thing you want with it, such as
655 @code{:value (if (boundp 'special-symbol) special-symbol nil)}
656
657 Example of creating an object from a class:
658
659 @example
660 (record :value 3 :reference nil)
661 @end example
662
663 @end defun
664
665 To create an object from a class symbol, use @code{make-instance}.
666
667 @defun make-instance class &rest initargs
668 @anchor{make-instance}
669 Make a new instance of @var{class} based on @var{initargs}.
670 @var{class} is a class symbol. For example:
671
672 @example
673 (make-instance 'foo)
674 @end example
675
676 @var{initargs} is a property list with keywords based on the @code{:initarg}
677 for each slot. For example:
678
679 @example
680 (make-instance @code{'foo} @code{:slot1} value1 @code{:slotN} valueN)
681 @end example
682
683 @end defun
684
685 @node Accessing Slots
686 @chapter Accessing Slots
687
688 There are several ways to access slot values in an object. The naming
689 and argument-order conventions are similar to those used for
690 referencing vectors (@pxref{Vectors,,,elisp,GNU Emacs Lisp Reference
691 Manual}).
692
693 @defmac oset object slot value
694 This macro sets the value behind @var{slot} to @var{value} in
695 @var{object}. It returns @var{value}.
696 @end defmac
697
698 @defmac oset-default class slot value
699 This macro sets the value for the class-allocated @var{slot} in @var{class} to
700 @var{value}.
701
702 For example, if a user wanted all @code{data-objects} (@pxref{Building
703 Classes}) to inform a special object of his own devising when they
704 changed, this can be arranged by simply executing this bit of code:
705
706 @example
707 (oset-default data-object reference (list my-special-object))
708 @end example
709 @end defmac
710
711 @defmac oref obj slot
712 @anchor{oref}
713 Retrieve the value stored in @var{obj} in the slot named by @var{slot}.
714 Slot is the name of the slot when created by @dfn{defclass}.
715 @end defmac
716
717 @defmac oref-default class slot
718 @anchor{oref-default}
719 Get the value of the class-allocated @var{slot} from @var{class}.
720 @end defmac
721
722 The following accessors are defined by CLOS to reference or modify
723 slot values, and use the previously mentioned set/ref routines.
724
725 @defun slot-value object slot
726 @anchor{slot-value}
727 This function retrieves the value of @var{slot} from @var{object}.
728 Unlike @code{oref}, the symbol for @var{slot} must be quoted.
729 @end defun
730
731 @defun set-slot-value object slot value
732 @anchor{set-slot-value}
733 This is not a CLOS function, but is the setter for @code{slot-value}
734 used by the @code{setf} macro. This
735 function sets the value of @var{slot} from @var{object}. Unlike
736 @code{oset}, the symbol for @var{slot} must be quoted.
737 @end defun
738
739 @defun slot-makeunbound object slot
740 This function unbinds @var{slot} in @var{object}. Referencing an
741 unbound slot can signal an error.
742 @end defun
743
744 @defun object-add-to-list object slot item &optional append
745 @anchor{object-add-to-list}
746 In OBJECT's @var{slot}, add @var{item} to the list of elements.
747 Optional argument @var{append} indicates we need to append to the list.
748 If @var{item} already exists in the list in @var{slot}, then it is not added.
749 Comparison is done with @dfn{equal} through the @dfn{member} function call.
750 If @var{slot} is unbound, bind it to the list containing @var{item}.
751 @end defun
752
753 @defun object-remove-from-list object slot item
754 @anchor{object-remove-from-list}
755 In OBJECT's @var{slot}, remove occurrences of @var{item}.
756 Deletion is done with @dfn{delete}, which deletes by side effect
757 and comparisons are done with @dfn{equal}.
758 If @var{slot} is unbound, do nothing.
759 @end defun
760
761 @defun with-slots spec-list object &rest body
762 @anchor{with-slots}
763 Bind @var{spec-list} lexically to slot values in @var{object}, and execute @var{body}.
764 This establishes a lexical environment for referring to the slots in
765 the instance named by the given slot-names as though they were
766 variables. Within such a context the value of the slot can be
767 specified by using its slot name, as if it were a lexically bound
768 variable. Both @code{setf} and @code{setq} can be used to set the value of the
769 slot.
770
771 @var{spec-list} is of a form similar to @dfn{let}. For example:
772
773 @example
774 ((VAR1 SLOT1)
775 SLOT2
776 SLOTN
777 (VARN+1 SLOTN+1))
778 @end example
779
780 Where each @var{var} is the local variable given to the associated
781 @var{slot}. A slot specified without a variable name is given a
782 variable name of the same name as the slot.
783
784 @example
785 (defclass myclass () (x :initform 1))
786 (setq mc (make-instance 'myclass))
787 (with-slots (x) mc x) => 1
788 (with-slots ((something x)) mc something) => 1
789 @end example
790 @end defun
791
792 @node Writing Methods
793 @chapter Writing Methods
794
795 Writing a method in @eieio{} is similar to writing a function. The
796 differences are that there are some extra options and there can be
797 multiple definitions under the same function symbol.
798
799 Where a method defines an implementation for a particular data type, a
800 @dfn{generic method} accepts any argument, but contains no code. It
801 is used to provide the dispatching to the defined methods. A generic
802 method has no body, and is merely a symbol upon which methods are
803 attached. It also provides the base documentation for what methods
804 with that name do.
805
806 @menu
807 * Generics::
808 * Methods::
809 * Static Methods::
810 @end menu
811
812 @node Generics
813 @section Generics
814
815 Each @eieio{} method has one corresponding generic. This generic
816 provides a function binding and the base documentation for the method
817 symbol (@pxref{Symbol Components,,,elisp,GNU Emacs Lisp Reference
818 Manual}).
819
820 @defmac defgeneric method arglist [doc-string]
821 This macro turns the (unquoted) symbol @var{method} into a function.
822 @var{arglist} is the default list of arguments to use (not implemented
823 yet). @var{doc-string} is the documentation used for this symbol.
824
825 A generic function acts as a placeholder for methods. There is no
826 need to call @code{defgeneric} yourself, as @code{defmethod} will call
827 it if necessary. Currently the argument list is unused.
828
829 @code{defgeneric} signals an error if you attempt to turn an existing
830 Emacs Lisp function into a generic function.
831
832 You can also create a generic method with @code{defmethod}
833 (@pxref{Methods}). When a method is created and there is no generic
834 method in place with that name, then a new generic will be created,
835 and the new method will use it.
836 @end defmac
837
838 In CLOS, a generic call also be used to provide an argument list and
839 dispatch precedence for all the arguments. In @eieio{}, dispatching
840 only occurs for the first argument, so the @var{arglist} is not used.
841
842 @node Methods
843 @section Methods
844
845 A method is a function that is executed if the first argument passed
846 to it matches the method's class. Different @eieio{} classes may
847 share the same method names.
848
849 Methods are created with the @code{defmethod} macro, which is similar
850 to @code{defun}.
851
852 @defmac defmethod method [:before | :primary | :after | :static ] arglist [doc-string] forms
853
854 @var{method} is the name of the function to create.
855
856 @code{:before} and @code{:after} specify execution order (i.e., when
857 this form is called). If neither of these symbols are present, the
858 default priority is used (before @code{:after} and after
859 @code{:before}); this default priority is represented in CLOS as
860 @code{:primary}.
861
862 @b{Note:} The @code{:BEFORE}, @code{:PRIMARY}, @code{:AFTER}, and
863 @code{:STATIC} method tags were in all capital letters in previous
864 versions of @eieio{}.
865
866 @var{arglist} is the list of arguments to this method. The first
867 argument in this list---and @emph{only} the first argument---may have
868 a type specifier (see the example below). If no type specifier is
869 supplied, the method applies to any object.
870
871 @var{doc-string} is the documentation attached to the implementation.
872 All method doc-strings are incorporated into the generic method's
873 function documentation.
874
875 @var{forms} is the body of the function.
876
877 @end defmac
878
879 @noindent
880 In the following example, we create a method @code{mymethod} for the
881 @code{classname} class:
882
883 @example
884 (defmethod mymethod ((obj classname) secondarg)
885 "Doc string" )
886 @end example
887
888 @noindent
889 This method only executes if the @var{obj} argument passed to it is an
890 @eieio{} object of class @code{classname}.
891
892 A method with no type specifier is a @dfn{default method}. If a given
893 class has no implementation, then the default method is called when
894 that method is used on a given object of that class.
895
896 Only one default method per execution specifier (@code{:before},
897 @code{:primary}, or @code{:after}) is allowed. If two
898 @code{defmethod}s appear with @var{arglist}s lacking a type specifier,
899 and having the same execution specifier, then the first implementation
900 is replaced.
901
902 When a method is called on an object, but there is no method specified
903 for that object, but there is a method specified for object's parent
904 class, the parent class' method is called. If there is a method
905 defined for both, only the child's method is called. A child method
906 may call a parent's method using @code{call-next-method}, described
907 below.
908
909 If multiple methods and default methods are defined for the same
910 method and class, they are executed in this order:
911
912 @enumerate
913 @item method :before
914 @item default :before
915 @item method :primary
916 @item default :primary
917 @item method :after
918 @item default :after
919 @end enumerate
920
921 If no methods exist, Emacs signals a @code{no-method-definition}
922 error. @xref{Signals}.
923
924 @defun call-next-method &rest replacement-args
925 @anchor{call-next-method}
926
927 This function calls the superclass method from a subclass method.
928 This is the ``next method'' specified in the current method list.
929
930 If @var{replacement-args} is non-@code{nil}, then use them instead of
931 @code{eieio-generic-call-arglst}. At the top level, the generic
932 argument list is passed in.
933
934 Use @code{next-method-p} to find out if there is a next method to
935 call.
936 @end defun
937
938 @defun next-method-p
939 @anchor{next-method-p}
940 Non-@code{nil} if there is a next method.
941 Returns a list of lambda expressions which is the @code{next-method}
942 order.
943 @end defun
944
945 At present, @eieio{} does not implement all the features of CLOS:
946
947 @enumerate
948 @item
949 There is currently no @code{:around} tag.
950 @item
951 CLOS allows multiple sets of type-cast arguments, but @eieio{} only
952 allows the first argument to be cast.
953 @end enumerate
954
955 @node Static Methods
956 @section Static Methods
957
958 Static methods do not depend on an object instance, but instead
959 operate on a class. You can create a static method by using
960 the @code{:static} key with @code{defmethod}.
961
962 The first argument of a @code{:static} method will be a class rather than an
963 object. Use the functions @code{oref-default} or @code{oset-default} which
964 will work on a class.
965
966 A class's @code{make-instance} method is defined as a @code{:static}
967 method.
968
969 @b{Note:} The @code{:static} keyword is unique to @eieio{}.
970
971 @c TODO - Write some more about static methods here
972
973 @node Method Invocation
974 @chapter Method Invocation
975
976 When classes are defined, you can specify the
977 @code{:method-invocation-order}. This is a feature specific to EIEIO.
978
979 This controls the order in which method resolution occurs for
980 @code{:primary} methods in cases of multiple inheritance. The order
981 affects which method is called first in a tree, and if
982 @code{call-next-method} is used, it controls the order in which the
983 stack of methods are run.
984
985 The original EIEIO order turned out to be broken for multiple
986 inheritance, but some programs depended on it. As such this option
987 was added when the default invocation order was fixed to something
988 that made more sense in that case.
989
990 Valid values are:
991
992 @table @code
993 @item :breadth-first
994 Search for methods in the class hierarchy in breadth first order.
995 This is the default.
996 @item :depth-first
997 Search for methods in the class hierarchy in a depth first order.
998 @item :c3
999 Searches for methods in a linearized way that most closely matches
1000 what CLOS does when a monotonic class structure is defined.
1001
1002 This is derived from the Dylan language documents by
1003 Kim Barrett et al.: A Monotonic Superclass Linearization for Dylan
1004 Retrieved from: http://192.220.96.201/dylan/linearization-oopsla96.html
1005 @end table
1006
1007 @node Predicates
1008 @chapter Predicates and Utilities
1009
1010 Now that we know how to create classes, access slots, and define
1011 methods, it might be useful to verify that everything is doing ok. To
1012 help with this a plethora of predicates have been created.
1013
1014 @defun find-class symbol &optional errorp
1015 @anchor{find-class}
1016 Return the class that @var{symbol} represents.
1017 If there is no class, @code{nil} is returned if @var{errorp} is @code{nil}.
1018 If @var{errorp} is non-@code{nil}, @code{wrong-argument-type} is signaled.
1019 @end defun
1020
1021 @defun class-p class
1022 @anchor{class-p}
1023 Return @code{t} if @var{class} is a valid class vector.
1024 @var{class} is a symbol.
1025 @end defun
1026
1027 @defun slot-exists-p object-or-class slot
1028 @anchor{slot-exists-p}
1029 Non-@code{nil} if @var{object-or-class} has @var{slot}.
1030 @end defun
1031
1032 @defun slot-boundp object slot
1033 @anchor{slot-boundp}
1034 Non-@code{nil} if OBJECT's @var{slot} is bound.
1035 Setting a slot's value makes it bound. Calling @dfn{slot-makeunbound} will
1036 make a slot unbound.
1037 @var{object} can be an instance or a class.
1038 @end defun
1039
1040 @defun eieio-class-name class
1041 Return a string of the form @samp{#<class myclassname>} which should look
1042 similar to other Lisp objects like buffers and processes. Printing a
1043 class results only in a symbol.
1044 @end defun
1045
1046 @defun class-option class option
1047 Return the value in @var{CLASS} of a given @var{OPTION}.
1048 For example:
1049
1050 @example
1051 (class-option eieio-default-superclass :documentation)
1052 @end example
1053
1054 Will fetch the documentation string for @code{eieio-default-superclass}.
1055 @end defun
1056
1057 @defun eieio-object-name obj
1058 Return a string of the form @samp{#<object-class myobjname>} for @var{obj}.
1059 This should look like Lisp symbols from other parts of Emacs such as
1060 buffers and processes, and is shorter and cleaner than printing the
1061 object's vector. It is more useful to use @code{object-print} to get
1062 and object's print form, as this allows the object to add extra display
1063 information into the symbol.
1064 @end defun
1065
1066 @defun eieio-object-class obj
1067 Returns the class symbol from @var{obj}.
1068 @end defun
1069
1070 @defun eieio-object-class-name obj
1071 Returns the symbol of @var{obj}'s class.
1072 @end defun
1073
1074 @defun eieio-class-parents class
1075 Returns the direct parents class of @var{class}. Returns @code{nil} if
1076 it is a superclass.
1077 @end defun
1078
1079 @defun eieio-class-parents-fast class
1080 Just like @code{eieio-class-parents} except it is a macro and no type checking
1081 is performed.
1082 @end defun
1083
1084 @defun eieio-class-parent class
1085 Deprecated function which returns the first parent of @var{class}.
1086 @end defun
1087
1088 @defun eieio-class-children class
1089 Return the list of classes inheriting from @var{class}.
1090 @end defun
1091
1092 @defun eieio-class-children-fast class
1093 Just like @code{eieio-class-children}, but with no checks.
1094 @end defun
1095
1096 @defun same-class-p obj class
1097 Returns @code{t} if @var{obj}'s class is the same as @var{class}.
1098 @end defun
1099
1100 @defun same-class-fast-p obj class
1101 Same as @code{same-class-p} except this is a macro and no type checking
1102 is performed.
1103 @end defun
1104
1105 @defun object-of-class-p obj class
1106 Returns @code{t} if @var{obj} inherits anything from @var{class}. This
1107 is different from @code{same-class-p} because it checks for inheritance.
1108 @end defun
1109
1110 @defun child-of-class-p child class
1111 Returns @code{t} if @var{child} is a subclass of @var{class}.
1112 @end defun
1113
1114 @defun generic-p method-symbol
1115 Returns @code{t} if @code{method-symbol} is a generic function, as
1116 opposed to a regular Emacs Lisp function.
1117 @end defun
1118
1119 @node Association Lists
1120 @chapter Association Lists
1121
1122 Lisp offers the concept of association lists, with primitives such as
1123 @code{assoc} used to access them. The following functions can be used
1124 to manage association lists of @eieio{} objects:
1125
1126 @defun object-assoc key slot list
1127 @anchor{object-assoc}
1128 Return an object if @var{key} is @dfn{equal} to SLOT's value of an object in @var{list}.
1129 @var{list} is a list of objects whose slots are searched.
1130 Objects in @var{list} do not need to have a slot named @var{slot}, nor does
1131 @var{slot} need to be bound. If these errors occur, those objects will
1132 be ignored.
1133 @end defun
1134
1135
1136 @defun object-assoc-list slot list
1137 Return an association list generated by extracting @var{slot} from all
1138 objects in @var{list}. For each element of @var{list} the @code{car} is
1139 the value of @var{slot}, and the @code{cdr} is the object it was
1140 extracted from. This is useful for generating completion tables.
1141 @end defun
1142
1143 @defun eieio-build-class-alist &optional base-class
1144 Returns an alist of all currently defined classes. This alist is
1145 suitable for completion lists used by interactive functions to select a
1146 class. The optional argument @var{base-class} allows the programmer to
1147 select only a subset of classes which includes @var{base-class} and
1148 all its subclasses.
1149 @end defun
1150
1151 @node Customizing
1152 @chapter Customizing Objects
1153
1154 @eieio{} supports the Custom facility through two new widget types.
1155 If a variable is declared as type @code{object}, then full editing of
1156 slots via the widgets is made possible. This should be used
1157 carefully, however, because modified objects are cloned, so if there
1158 are other references to these objects, they will no longer be linked
1159 together.
1160
1161 If you want in place editing of objects, use the following methods:
1162
1163 @defun eieio-customize-object object
1164 Create a custom buffer and insert a widget for editing @var{object}. At
1165 the end, an @code{Apply} and @code{Reset} button are available. This
1166 will edit the object "in place" so references to it are also changed.
1167 There is no effort to prevent multiple edits of a singular object, so
1168 care must be taken by the user of this function.
1169 @end defun
1170
1171 @defun eieio-custom-widget-insert object flags
1172 This method inserts an edit object into the current buffer in place.
1173 It is implemented as @code{(widget-create 'object-edit :value object)}.
1174 This method is provided as a locale for adding tracking, or
1175 specializing the widget insert procedure for any object.
1176 @end defun
1177
1178 To define a slot with an object in it, use the @code{object} tag. This
1179 widget type will be automatically converted to @code{object-edit} if you
1180 do in place editing of you object.
1181
1182 If you want to have additional actions taken when a user clicks on the
1183 @code{Apply} button, then overload the method @code{eieio-done-customizing}.
1184 This method does nothing by default, but that may change in the future.
1185 This would be the best way to make your objects persistent when using
1186 in-place editing.
1187
1188 @section Widget extension
1189
1190 When widgets are being created, one new widget extension has been added,
1191 called the @code{:slotofchoices}. When this occurs in a widget
1192 definition, all elements after it are removed, and the slot is specifies
1193 is queried and converted into a series of constants.
1194
1195 @example
1196 (choice (const :tag "None" nil)
1197 :slotofchoices morestuff)
1198 @end example
1199
1200 and if the slot @code{morestuff} contains @code{(sym1 sym2 sym3)}, the
1201 above example is converted into:
1202
1203 @example
1204 (choice (const :tag "None" nil)
1205 (const sym1)
1206 (const sym2)
1207 (const sym3))
1208 @end example
1209
1210 This is useful when a given item needs to be selected from a list of
1211 items defined in this second slot.
1212
1213 @node Introspection
1214 @chapter Introspection
1215
1216 Introspection permits a programmer to peek at the contents of a class
1217 without any previous knowledge of that class. While @eieio{} implements
1218 objects on top of vectors, and thus everything is technically visible,
1219 some functions have been provided. None of these functions are a part
1220 of CLOS.
1221
1222 @defun object-slots obj
1223 Return the list of public slots for @var{obj}.
1224 @end defun
1225
1226 @defun class-slot-initarg class slot
1227 For the given @var{class} return an :initarg associated with
1228 @var{slot}. Not all slots have initargs, so the return value can be
1229 @code{nil}.
1230 @end defun
1231
1232 @node Base Classes
1233 @chapter Base Classes
1234
1235 All defined classes, if created with no specified parent class,
1236 inherit from a special class called @code{eieio-default-superclass}.
1237 @xref{Default Superclass}.
1238
1239 Often, it is more convenient to inherit from one of the other base
1240 classes provided by @eieio{}, which have useful pre-defined
1241 properties. (Since @eieio{} supports multiple inheritance, you can
1242 even inherit from more than one of these classes at once.)
1243
1244 @menu
1245 * eieio-instance-inheritor:: Enable value inheritance between instances.
1246 * eieio-instance-tracker:: Enable self tracking instances.
1247 * eieio-singleton:: Only one instance of a given class.
1248 * eieio-persistent:: Enable persistence for a class.
1249 * eieio-named:: Use the object name as a :name slot.
1250 * eieio-speedbar:: Enable speedbar support in your objects.
1251 @end menu
1252
1253 @node eieio-instance-inheritor
1254 @section @code{eieio-instance-inheritor}
1255
1256 This class is defined in the package @file{eieio-base}.
1257
1258 Instance inheritance is a mechanism whereby the value of a slot in
1259 object instance can reference the parent instance. If the parent's slot
1260 value is changed, then the child instance is also changed. If the
1261 child's slot is set, then the parent's slot is not modified.
1262
1263 @deftp {Class} eieio-instance-inheritor parent-instance
1264 A class whose instances are enabled with instance inheritance.
1265 The @var{parent-instance} slot indicates the instance which is
1266 considered the parent of the current instance. Default is @code{nil}.
1267 @end deftp
1268
1269 @cindex clone
1270 To use this class, inherit from it with your own class.
1271 To make a new instance that inherits from and existing instance of your
1272 class, use the @code{clone} method with additional parameters
1273 to specify local values.
1274
1275 @cindex slot-unbound
1276 The @code{eieio-instance-inheritor} class works by causing cloned
1277 objects to have all slots unbound. This class' @code{slot-unbound}
1278 method will cause references to unbound slots to be redirected to the
1279 parent instance. If the parent slot is also unbound, then
1280 @code{slot-unbound} will signal an error named @code{slot-unbound}.
1281
1282 @node eieio-instance-tracker
1283 @section @code{eieio-instance-tracker}
1284
1285 This class is defined in the package @file{eieio-base}.
1286
1287 Sometimes it is useful to keep a master list of all instances of a given
1288 class. The class @code{eieio-instance-tracker} performs this task.
1289
1290 @deftp {Class} eieio-instance-tracker tracker-symbol
1291 Enable instance tracking for this class.
1292 The slot @var{tracker-symbol} should be initialized in inheritors of
1293 this class to a symbol created with @code{defvar}. This symbol will
1294 serve as the variable used as a master list of all objects of the given
1295 class.
1296 @end deftp
1297
1298 @defmethod eieio-instance-tracker initialize-instance obj slot
1299 This method is defined as an @code{:after} method.
1300 It adds new instances to the master list. Do not overload this method
1301 unless you use @code{call-next-method.}
1302 @end defmethod
1303
1304 @defmethod eieio-instance-tracker delete-instance obj
1305 Remove @var{obj} from the master list of instances of this class.
1306 This may let the garbage collector nab this instance.
1307 @end defmethod
1308
1309 @deffn eieio-instance-tracker-find key slot list-symbol
1310 This convenience function lets you find instances. @var{key} is the
1311 value to search for. @var{slot} is the slot to compare @var{KEY}
1312 against. The function @code{equal} is used for comparison.
1313 The parameter @var{list-symbol} is the variable symbol which contains the
1314 list of objects to be searched.
1315 @end deffn
1316
1317 @node eieio-singleton
1318 @section @code{eieio-singleton}
1319
1320 This class is defined in the package @file{eieio-base}.
1321
1322 @deftp {Class} eieio-singleton
1323 Inheriting from the singleton class will guarantee that there will
1324 only ever be one instance of this class. Multiple calls to
1325 @code{make-instance} will always return the same object.
1326 @end deftp
1327
1328 @node eieio-persistent
1329 @section @code{eieio-persistent}
1330
1331 This class is defined in the package @file{eieio-base}.
1332
1333 If you want an object, or set of objects to be persistent, meaning the
1334 slot values are important to keep saved between sessions, then you will
1335 want your top level object to inherit from @code{eieio-persistent}.
1336
1337 To make sure your persistent object can be moved, make sure all file
1338 names stored to disk are made relative with
1339 @code{eieio-persistent-path-relative}.
1340
1341 @deftp {Class} eieio-persistent file file-header-line
1342 Enables persistence for instances of this class.
1343 Slot @var{file} with initarg @code{:file} is the file name in which this
1344 object will be saved.
1345 Class allocated slot @var{file-header-line} is used with method
1346 @code{object-write} as a header comment.
1347 @end deftp
1348
1349 All objects can write themselves to a file, but persistent objects have
1350 several additional methods that aid in maintaining them.
1351
1352 @defmethod eieio-persistent eieio-persistent-save obj &optional file
1353 Write the object @var{obj} to its file.
1354 If optional argument @var{file} is specified, use that file name
1355 instead.
1356 @end defmethod
1357
1358 @defmethod eieio-persistent eieio-persistent-path-relative obj file
1359 Return a file name derived from @var{file} which is relative to the
1360 stored location of @var{OBJ}. This method should be used to convert
1361 file names so that they are relative to the save file, making any system
1362 of files movable from one location to another.
1363 @end defmethod
1364
1365 @defmethod eieio-persistent object-write obj &optional comment
1366 Like @code{object-write} for @code{standard-object}, but will derive
1367 a header line comment from the class allocated slot if one is not
1368 provided.
1369 @end defmethod
1370
1371 @defun eieio-persistent-read filename &optional class allow-subclass
1372 Read a persistent object from @var{filename}, and return it.
1373 Signal an error if the object in @var{FILENAME} is not a constructor
1374 for @var{CLASS}. Optional @var{allow-subclass} says that it is ok for
1375 @code{eieio-persistent-read} to load in subclasses of class instead of
1376 being pedantic.
1377 @end defun
1378
1379 @node eieio-named
1380 @section @code{eieio-named}
1381
1382 This class is defined in the package @file{eieio-base}.
1383
1384 @deftp {Class} eieio-named
1385 Object with a name.
1386 Name storage already occurs in an object. This object provides get/set
1387 access to it.
1388 @end deftp
1389
1390 @node eieio-speedbar
1391 @section @code{eieio-speedbar}
1392
1393 This class is in package @file{eieio-speedbar}.
1394
1395 If a series of class instances map to a tree structure, it is possible
1396 to cause your classes to be displayable in Speedbar. @xref{Top,,,speedbar}.
1397 Inheriting from these classes will enable a speedbar major display mode
1398 with a minimum of effort.
1399
1400 @deftp {Class} eieio-speedbar buttontype buttonface
1401 Enables base speedbar display for a class.
1402 @cindex speedbar-make-tag-line
1403 The slot @var{buttontype} is any of the symbols allowed by the
1404 function @code{speedbar-make-tag-line} for the @var{exp-button-type}
1405 argument @xref{Extending,,,speedbar}.
1406 The slot @var{buttonface} is the face to use for the text of the string
1407 displayed in speedbar.
1408 The slots @var{buttontype} and @var{buttonface} are class allocated
1409 slots, and do not take up space in your instances.
1410 @end deftp
1411
1412 @deftp {Class} eieio-speedbar-directory-button buttontype buttonface
1413 This class inherits from @code{eieio-speedbar} and initializes
1414 @var{buttontype} and @var{buttonface} to appear as directory level lines.
1415 @end deftp
1416
1417 @deftp {Class} eieio-speedbar-file-button buttontype buttonface
1418 This class inherits from @code{eieio-speedbar} and initializes
1419 @var{buttontype} and @var{buttonface} to appear as file level lines.
1420 @end deftp
1421
1422 To use these classes, inherit from one of them in you class. You can
1423 use multiple inheritance with them safely. To customize your class for
1424 speedbar display, override the default values for @var{buttontype} and
1425 @var{buttonface} to get the desired effects.
1426
1427 Useful methods to define for your new class include:
1428
1429 @defmethod eieio-speedbar eieio-speedbar-derive-line-path obj depth
1430 Return a string representing a directory associated with an instance
1431 of @var{obj}. @var{depth} can be used to index how many levels of
1432 indentation have been opened by the user where @var{obj} is shown.
1433 @end defmethod
1434
1435
1436 @defmethod eieio-speedbar eieio-speedbar-description obj
1437 Return a string description of @var{OBJ}.
1438 This is shown in the minibuffer or tooltip when the mouse hovers over
1439 this instance in speedbar.
1440 @end defmethod
1441
1442 @defmethod eieio-speedbar eieio-speedbar-child-description obj
1443 Return a string representing a description of a child node of @var{obj}
1444 when that child is not an object. It is often useful to just use
1445 item info helper functions such as @code{speedbar-item-info-file-helper}.
1446 @end defmethod
1447
1448 @defmethod eieio-speedbar eieio-speedbar-object-buttonname obj
1449 Return a string which is the text displayed in speedbar for @var{obj}.
1450 @end defmethod
1451
1452 @defmethod eieio-speedbar eieio-speedbar-object-children obj
1453 Return a list of children of @var{obj}.
1454 @end defmethod
1455
1456 @defmethod eieio-speedbar eieio-speedbar-child-make-tag-lines obj depth
1457 This method inserts a list of speedbar tag lines for @var{obj} to
1458 represent its children. Implement this method for your class
1459 if your children are not objects themselves. You still need to
1460 implement @code{eieio-speedbar-object-children}.
1461
1462 In this method, use techniques specified in the Speedbar manual.
1463 @xref{Extending,,,speedbar}.
1464 @end defmethod
1465
1466 Some other functions you will need to learn to use are:
1467
1468 @deffn eieio-speedbar-create make-map key-map menu name toplevelfn
1469 Register your object display mode with speedbar.
1470 @var{make-map} is a function which initialized you keymap.
1471 @var{key-map} is a symbol you keymap is installed into.
1472 @var{menu} is an easy menu vector representing menu items specific to your
1473 object display.
1474 @var{name} is a short string to use as a name identifying you mode.
1475 @var{toplevelfn} is a function called which must return a list of
1476 objects representing those in the instance system you wish to browse in
1477 speedbar.
1478
1479 Read the Extending chapter in the speedbar manual for more information
1480 on how speedbar modes work
1481 @xref{Extending,,,speedbar}.
1482 @end deffn
1483
1484 @node Browsing
1485 @chapter Browsing class trees
1486
1487 The command @kbd{M-x eieio-browse} displays a buffer listing all the
1488 currently loaded classes in Emacs. The classes are listed in an
1489 indented tree structure, starting from @code{eieio-default-superclass}
1490 (@pxref{Default Superclass}).
1491
1492 With a prefix argument, this command prompts for a class name; it then
1493 lists only that class and its subclasses.
1494
1495 Here is a sample tree from our current example:
1496
1497 @example
1498 eieio-default-superclass
1499 +--data-object
1500 +--data-object-symbol
1501 @end example
1502
1503 Note: new classes are consed into the inheritance lists, so the tree
1504 comes out upside-down.
1505
1506 @node Class Values
1507 @chapter Class Values
1508
1509 You can use the normal @code{describe-function} command to retrieve
1510 information about a class. Running it on constructors will show a
1511 full description of the generated class. If you call it on a generic
1512 function, all implementations of that generic function will be listed,
1513 together with links through which you can directly jump to the source.
1514
1515 @node Default Superclass
1516 @chapter Default Superclass
1517
1518 All defined classes, if created with no specified parent class, will
1519 inherit from a special class stored in
1520 @code{eieio-default-superclass}. This superclass is quite simple, but
1521 with it, certain default methods or attributes can be added to all
1522 objects. In CLOS, this would be named @code{STANDARD-CLASS}, and that
1523 symbol is an alias to @code{eieio-default-superclass}.
1524
1525 Currently, the default superclass is defined as follows:
1526
1527 @example
1528 (defclass eieio-default-superclass nil
1529 nil
1530 "Default parent class for classes with no specified parent class.
1531 Its slots are automatically adopted by classes with no specified
1532 parents. This class is not stored in the `parent' slot of a class vector."
1533 :abstract t)
1534 @end example
1535
1536 The default superclass implements several methods providing a default
1537 behavior for all objects created by @eieio{}.
1538
1539 @menu
1540 * Initialization:: How objects are initialized
1541 * Basic Methods:: Clone, print, and write
1542 * Signal Handling:: Methods for managing signals.
1543 @end menu
1544
1545 @node Initialization
1546 @section Initialization
1547
1548 When creating an object of any type, you can use its constructor, or
1549 @code{make-instance}. This, in turns calls the method
1550 @code{initialize-instance}, which then calls the method
1551 @code{shared-initialize}.
1552
1553 These methods are all implemented on the default superclass so you do
1554 not need to write them yourself, unless you need to override one of
1555 their behaviors.
1556
1557 Users should not need to call @code{initialize-instance} or
1558 @code{shared-initialize}, as these are used by @code{make-instance} to
1559 initialize the object. They are instead provided so that users can
1560 augment these behaviors.
1561
1562 @defun initialize-instance obj &rest slots
1563 Initialize @var{obj}. Sets slots of @var{obj} with @var{slots} which
1564 is a list of name/value pairs. These are actually just passed to
1565 @code{shared-initialize}.
1566 @end defun
1567
1568 @defun shared-initialize obj &rest slots
1569 Sets slots of @var{obj} with @var{slots} which is a list of name/value
1570 pairs.
1571
1572 This is called from the default constructor.
1573 @end defun
1574
1575 @node Basic Methods
1576 @section Basic Methods
1577
1578 Additional useful methods defined on the base subclass are:
1579
1580 @defun clone obj &rest params
1581 @anchor{clone}
1582 Make a copy of @var{obj}, and then apply @var{params}.
1583 @var{params} is a parameter list of the same form as @var{initialize-instance}
1584 which are applied to change the object. When overloading @dfn{clone}, be
1585 sure to call @dfn{call-next-method} first and modify the returned object.
1586 @end defun
1587
1588 @defun object-print this &rest strings
1589 @anchor{object-print}
1590 Pretty printer for object @var{this}. Call function @dfn{eieio-object-name} with @var{strings}.
1591 The default method for printing object @var{this} is to use the
1592 function @dfn{eieio-object-name}.
1593
1594 It is sometimes useful to put a summary of the object into the
1595 default #<notation> string when using eieio browsing tools.
1596
1597 Implement this function and specify @var{strings} in a call to
1598 @dfn{call-next-method} to provide additional summary information.
1599 When passing in extra strings from child classes, always remember
1600 to prepend a space.
1601
1602 @example
1603 (defclass data-object ()
1604 (value)
1605 "Object containing one data slot.")
1606
1607 (defmethod object-print ((this data-object) &optional strings)
1608 "Return a string with a summary of the data object as part of the name."
1609 (apply 'call-next-method this
1610 (cons (format " value: %s" (render this)) strings)))
1611 @end example
1612
1613 Here is what some output could look like:
1614 @example
1615 (object-print test-object)
1616 => #<data-object test-object value: 3>
1617 @end example
1618 @end defun
1619
1620 @defun object-write obj &optional comment
1621 Write @var{obj} onto a stream in a readable fashion. The resulting
1622 output will be Lisp code which can be used with @code{read} and
1623 @code{eval} to recover the object. Only slots with @code{:initarg}s
1624 are written to the stream.
1625 @end defun
1626
1627 @node Signal Handling
1628 @section Signal Handling
1629
1630 The default superclass defines methods for managing error conditions.
1631 These methods all throw a signal for a particular error condition.
1632
1633 By implementing one of these methods for a class, you can change the
1634 behavior that occurs during one of these error cases, or even ignore
1635 the error by providing some behavior.
1636
1637 @defun slot-missing object slot-name operation &optional new-value
1638 @anchor{slot-missing}
1639 Method invoked when an attempt to access a slot in @var{object} fails.
1640 @var{slot-name} is the name of the failed slot, @var{operation} is the type of access
1641 that was requested, and optional @var{new-value} is the value that was desired
1642 to be set.
1643
1644 This method is called from @code{oref}, @code{oset}, and other functions which
1645 directly reference slots in EIEIO objects.
1646
1647 The default method signals an error of type @code{invalid-slot-name}.
1648 @xref{Signals}.
1649
1650 You may override this behavior, but it is not expected to return in the
1651 current implementation.
1652
1653 This function takes arguments in a different order than in CLOS.
1654 @end defun
1655
1656 @defun slot-unbound object class slot-name fn
1657 @anchor{slot-unbound}
1658 Slot unbound is invoked during an attempt to reference an unbound slot.
1659 @var{object} is the instance of the object being reference. @var{class} is the
1660 class of @var{object}, and @var{slot-name} is the offending slot. This function
1661 throws the signal @code{unbound-slot}. You can overload this function and
1662 return the value to use in place of the unbound value.
1663 Argument @var{fn} is the function signaling this error.
1664 Use @dfn{slot-boundp} to determine if a slot is bound or not.
1665
1666 In @var{clos}, the argument list is (@var{class} @var{object} @var{slot-name}), but
1667 @var{eieio} can only dispatch on the first argument, so the first two are swapped.
1668 @end defun
1669
1670 @defun no-applicable-method object method &rest args
1671 @anchor{no-applicable-method}
1672 Called if there are no implementations for @var{object} in @var{method}.
1673 @var{object} is the object which has no method implementation.
1674 @var{args} are the arguments that were passed to @var{method}.
1675
1676 Implement this for a class to block this signal. The return
1677 value becomes the return value of the original method call.
1678 @end defun
1679
1680 @defun no-next-method object &rest args
1681 @anchor{no-next-method}
1682 Called from @dfn{call-next-method} when no additional methods are available.
1683 @var{object} is othe object being called on @dfn{call-next-method}.
1684 @var{args} are the arguments it is called by.
1685 This method signals @dfn{no-next-method} by default. Override this
1686 method to not throw an error, and its return value becomes the
1687 return value of @dfn{call-next-method}.
1688 @end defun
1689
1690 @node Signals
1691 @chapter Signals
1692
1693 There are new condition names (signals) that can be caught when using
1694 @eieio{}.
1695
1696 @deffn Signal invalid-slot-name obj-or-class slot
1697 This signal is called when an attempt to reference a slot in an
1698 @var{obj-or-class} is made, and the @var{slot} is not defined for
1699 it.
1700 @end deffn
1701
1702 @deffn Signal no-method-definition method arguments
1703 This signal is called when @var{method} is called, with @var{arguments}
1704 and nothing is resolved. This occurs when @var{method} has been
1705 defined, but the arguments make it impossible for @eieio{} to determine
1706 which method body to run.
1707
1708 To prevent this signal from occurring in your class, implement the
1709 method @code{no-applicable-method} for your class. This method is
1710 called when to throw this signal, so implementing this for your class
1711 allows you block the signal, and perform some work.
1712 @end deffn
1713
1714 @deffn Signal no-next-method class arguments
1715 This signal is called if the function @code{call-next-method} is called
1716 and there is no next method to be called.
1717
1718 Overload the method @code{no-next-method} to protect against this signal.
1719 @end deffn
1720
1721 @deffn Signal invalid-slot-type slot spec value
1722 This signal is called when an attempt to set @var{slot} is made, and
1723 @var{value} doesn't match the specified type @var{spec}.
1724
1725 In @eieio{}, this is also used if a slot specifier has an invalid value
1726 during a @code{defclass}.
1727 @end deffn
1728
1729 @deffn Signal unbound-slot object class slot
1730 This signal is called when an attempt to reference @var{slot} in
1731 @var{object} is made, and that instance is currently unbound.
1732 @end deffn
1733
1734 @node Naming Conventions
1735 @chapter Naming Conventions
1736
1737 @xref{Tips,,Tips and Conventions,elisp,GNU Emacs Lisp Reference
1738 Manual}, for a description of Emacs Lisp programming conventions.
1739 These conventions help ensure that Emacs packages work nicely one
1740 another, so an @eieio{}-based program should follow them. Here are
1741 some conventions that apply specifically to @eieio{}-based programs:
1742
1743 @itemize
1744
1745 @item Come up with a package prefix that is relatively short. Prefix
1746 all classes, and methods with your prefix. This is a standard
1747 convention for functions and variables in Emacs.
1748
1749 @item Do not prefix method names with the class name. All methods in
1750 @eieio{} are ``virtual'', and are dynamically dispatched. Anyone can
1751 override your methods at any time. Your methods should be prefixed
1752 with your package name.
1753
1754 @item Do not prefix slots in your class. The slots are always locally
1755 scoped to your class, and need no prefixing.
1756
1757 @item If your library inherits from other libraries of classes, you
1758 must ``require'' that library with the @code{require} command.
1759
1760 @end itemize
1761
1762 @node CLOS compatibility
1763 @chapter CLOS compatibility
1764
1765 Currently, the following functions should behave almost as expected from
1766 CLOS.
1767
1768 @table @code
1769
1770 @item defclass
1771 All slot keywords are available but not all work correctly.
1772 Slot keyword differences are:
1773
1774 @table @asis
1775
1776 @item :reader, and :writer tags
1777 Create methods that signal errors instead of creating an unqualified
1778 method. You can still create new ones to do its business.
1779
1780 @item :accessor
1781 This should create an unqualified method to access a slot, but
1782 instead pre-builds a method that gets the slot's value.
1783
1784 @item :type
1785 Specifier uses the @code{typep} function from the @file{cl}
1786 package. @xref{Type Predicates,,,cl,Common Lisp Extensions}.
1787 It therefore has the same issues as that package. Extensions include
1788 the ability to provide object names.
1789 @end table
1790
1791 defclass also supports class options, but does not currently use values
1792 of @code{:metaclass}, and @code{:default-initargs}.
1793
1794 @item make-instance
1795 Make instance works as expected, however it just uses the @eieio{} instance
1796 creator automatically generated when a new class is created.
1797 @xref{Making New Objects}.
1798
1799 @item defgeneric
1800 Creates the desired symbol, and accepts all of the expected arguments
1801 except @code{:around}.
1802
1803 @item defmethod
1804 Calls defgeneric, and accepts most of the expected arguments. Only
1805 the first argument to the created method may have a type specifier.
1806 To type cast against a class, the class must exist before defmethod is
1807 called. In addition, the @code{:around} tag is not supported.
1808
1809 @item call-next-method
1810 Inside a method, calls the next available method up the inheritance tree
1811 for the given object. This is different than that found in CLOS because
1812 in @eieio{} this function accepts replacement arguments. This permits
1813 subclasses to modify arguments as they are passed up the tree. If no
1814 arguments are given, the expected CLOS behavior is used.
1815 @end table
1816
1817 CLOS supports the @code{describe} command, but @eieio{} provides
1818 support for using the standard @code{describe-function} command on a
1819 constructor or generic function.
1820
1821 When creating a new class (@pxref{Building Classes}) there are several
1822 new keywords supported by @eieio{}.
1823
1824 In @eieio{} tags are in lower case, not mixed case.
1825
1826 @node Wish List
1827 @chapter Wish List
1828
1829 @eieio{} is an incomplete implementation of CLOS@. Finding ways to
1830 improve the compatibility would help make CLOS style programs run
1831 better in Emacs.
1832
1833 Some important compatibility features that would be good to add are:
1834
1835 @enumerate
1836 @item
1837 Support for metaclasses and EQL specialization.
1838 @item
1839 @code{:around} method key.
1840 @item
1841 Method dispatch for built-in types.
1842 @item
1843 Method dispatch for multiple argument typing.
1844 @item
1845 Improve integration with the @file{cl} package.
1846 @end enumerate
1847
1848 There are also improvements to be made to allow @eieio{} to operate
1849 better in the Emacs environment.
1850
1851 @enumerate
1852 @item
1853 Allow subclassing of Emacs built-in types, such as faces, markers, and
1854 buffers.
1855 @item
1856 Allow method overloading of method-like functions in Emacs.
1857 @end enumerate
1858
1859 @node GNU Free Documentation License
1860 @appendix GNU Free Documentation License
1861 @include doclicense.texi
1862
1863 @node Function Index
1864 @unnumbered Function Index
1865
1866 @printindex fn
1867
1868 @contents
1869 @bye