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