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