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