Merge changes from emacs-24 branch
[emacs.git] / doc / lispref / symbols.texi
blob1e749b8c3bbaa4aa0425e81f02e51be5a4991184
1 @c -*-texinfo-*-
2 @c This is part of the GNU Emacs Lisp Reference Manual.
3 @c Copyright (C) 1990-1995, 1998-1999, 2001-2012 Free Software Foundation, Inc.
4 @c See the file elisp.texi for copying conditions.
5 @node Symbols, Evaluation, Hash Tables, Top
6 @chapter Symbols
7 @cindex symbol
9   A @dfn{symbol} is an object with a unique name.  This chapter
10 describes symbols, their components, their property lists, and how they
11 are created and interned.  Separate chapters describe the use of symbols
12 as variables and as function names; see @ref{Variables}, and
13 @ref{Functions}.  For the precise read syntax for symbols, see
14 @ref{Symbol Type}.
16   You can test whether an arbitrary Lisp object is a symbol
17 with @code{symbolp}:
19 @defun symbolp object
20 This function returns @code{t} if @var{object} is a symbol, @code{nil}
21 otherwise.
22 @end defun
24 @menu
25 * Symbol Components::        Symbols have names, values, function definitions
26                                and property lists.
27 * Definitions::              A definition says how a symbol will be used.
28 * Creating Symbols::         How symbols are kept unique.
29 * Property Lists::           Each symbol has a property list
30                                for recording miscellaneous information.
31 @end menu
33 @node Symbol Components, Definitions, Symbols, Symbols
34 @section Symbol Components
35 @cindex symbol components
37   Each symbol has four components (or ``cells''), each of which
38 references another object:
40 @table @asis
41 @item Print name
42 @cindex print name cell
43 The symbol's name.
45 @item Value
46 @cindex value cell
47 The symbol's current value as a variable.
49 @item Function
50 @cindex function cell
51 The symbol's function definition.  It can also hold a symbol, a
52 keymap, or a keyboard macro.
54 @item Property list
55 @cindex property list cell
56 The symbol's property list.
57 @end table
59 @noindent
60 The print name cell always holds a string, and cannot be changed.
61 Each of the other three cells can be set to any Lisp object.
63   The print name cell holds the string that is the name of a symbol.
64 Since symbols are represented textually by their names, it is
65 important not to have two symbols with the same name.  The Lisp reader
66 ensures this: every time it reads a symbol, it looks for an existing
67 symbol with the specified name before it creates a new one.  To get a
68 symbol's name, use the function @code{symbol-name} (@pxref{Creating
69 Symbols}).
71   The value cell holds a symbol's value as a variable, which is what
72 you get if the symbol itself is evaluated as a Lisp expression.
73 @xref{Variables}, for details about how values are set and retrieved,
74 including complications such as @dfn{local bindings} and @dfn{scoping
75 rules}.  Most symbols can have any Lisp object as a value, but certain
76 special symbols have values that cannot be changed; these include
77 @code{nil} and @code{t}, and any symbol whose name starts with
78 @samp{:} (those are called @dfn{keywords}).  @xref{Constant
79 Variables}.
81   The function cell holds a symbol's function definition.  Often, we
82 refer to ``the function @code{foo}'' when we really mean the function
83 stored in the function cell of @code{foo}; we make the distinction
84 explicit only when necessary.  Typically, the function cell is used to
85 hold a function (@pxref{Functions}) or a macro (@pxref{Macros}).
86 However, it can also be used to hold a symbol (@pxref{Function
87 Indirection}), keyboard macro (@pxref{Keyboard Macros}), keymap
88 (@pxref{Keymaps}), or autoload object (@pxref{Autoloading}).  To get
89 the contents of a symbol's function cell, use the function
90 @code{symbol-function} (@pxref{Function Cells}).
92   The property list cell normally should hold a correctly formatted
93 property list.  To get a symbol's function cell, use the function
94 @code{symbol-plist}.  @xref{Property Lists}.
96   The function cell or the value cell may be @dfn{void}, which means
97 that the cell does not reference any object.  (This is not the same
98 thing as holding the symbol @code{void}, nor the same as holding the
99 symbol @code{nil}.)  Examining a function or value cell that is void
100 results in an error, such as @samp{Symbol's value as variable is void}.
102   Because each symbol has separate value and function cells, variables
103 names and function names do not conflict.  For example, the symbol
104 @code{buffer-file-name} has a value (the name of the file being
105 visited in the current buffer) as well as a function definition (a
106 primitive function that returns the name of the file):
108 @example
109 buffer-file-name
110      @result{} "/gnu/elisp/symbols.texi"
111 (symbol-function 'buffer-file-name)
112      @result{} #<subr buffer-file-name>
113 @end example
115 @node Definitions, Creating Symbols, Symbol Components, Symbols
116 @section Defining Symbols
117 @cindex definitions of symbols
119   A @dfn{definition} is a special kind of Lisp expression that
120 announces your intention to use a symbol in a particular way.  It
121 typically specifies a value or meaning for the symbol for one kind of
122 use, plus documentation for its meaning when used in this way.  Thus,
123 when you define a symbol as a variable, you can supply an initial
124 value for the variable, plus documentation for the variable.
126   @code{defvar} and @code{defconst} are special forms that define a
127 symbol as a @dfn{global variable}---a variable that can be accessed at
128 any point in a Lisp program.  @xref{Variables}, for details about
129 variables.  To define a customizable variable, use the
130 @code{defcustom} macro, which also calls @code{defvar} as a subroutine
131 (@pxref{Customization}).
133   In principle, you can assign a variable value to any symbol with
134 @code{setq}, whether not it has first been defined as a variable.
135 However, you ought to write a variable definition for each global
136 variable that you want to use; otherwise, your Lisp program may not
137 act correctly if it is evaluated with lexical scoping enabled
138 (@pxref{Variable Scoping}).
140   @code{defun} defines a symbol as a function, creating a lambda
141 expression and storing it in the function cell of the symbol.  This
142 lambda expression thus becomes the function definition of the symbol.
143 (The term ``function definition'', meaning the contents of the function
144 cell, is derived from the idea that @code{defun} gives the symbol its
145 definition as a function.)  @code{defsubst} and @code{defalias} are two
146 other ways of defining a function.  @xref{Functions}.
148   @code{defmacro} defines a symbol as a macro.  It creates a macro
149 object and stores it in the function cell of the symbol.  Note that a
150 given symbol can be a macro or a function, but not both at once, because
151 both macro and function definitions are kept in the function cell, and
152 that cell can hold only one Lisp object at any given time.
153 @xref{Macros}.
155   As previously noted, Emacs Lisp allows the same symbol to be defined
156 both as a variable (e.g.@: with @code{defvar}) and as a function or
157 macro (e.g.@: with @code{defun}).  Such definitions do not conflict.
159   These definition also act as guides for programming tools.  For
160 example, the @kbd{C-h f} and @kbd{C-h v} commands create help buffers
161 containing links to the relevant variable, function, or macro
162 definitions.  @xref{Name Help,,, emacs, The GNU Emacs Manual}.
164 @node Creating Symbols, Property Lists, Definitions, Symbols
165 @section Creating and Interning Symbols
166 @cindex reading symbols
168   To understand how symbols are created in GNU Emacs Lisp, you must know
169 how Lisp reads them.  Lisp must ensure that it finds the same symbol
170 every time it reads the same set of characters.  Failure to do so would
171 cause complete confusion.
173 @cindex symbol name hashing
174 @cindex hashing
175 @cindex obarray
176 @cindex bucket (in obarray)
177   When the Lisp reader encounters a symbol, it reads all the characters
178 of the name.  Then it ``hashes'' those characters to find an index in a
179 table called an @dfn{obarray}.  Hashing is an efficient method of
180 looking something up.  For example, instead of searching a telephone
181 book cover to cover when looking up Jan Jones, you start with the J's
182 and go from there.  That is a simple version of hashing.  Each element
183 of the obarray is a @dfn{bucket} which holds all the symbols with a
184 given hash code; to look for a given name, it is sufficient to look
185 through all the symbols in the bucket for that name's hash code.  (The
186 same idea is used for general Emacs hash tables, but they are a
187 different data type; see @ref{Hash Tables}.)
189 @cindex interning
190   If a symbol with the desired name is found, the reader uses that
191 symbol.  If the obarray does not contain a symbol with that name, the
192 reader makes a new symbol and adds it to the obarray.  Finding or adding
193 a symbol with a certain name is called @dfn{interning} it, and the
194 symbol is then called an @dfn{interned symbol}.
196   Interning ensures that each obarray has just one symbol with any
197 particular name.  Other like-named symbols may exist, but not in the
198 same obarray.  Thus, the reader gets the same symbols for the same
199 names, as long as you keep reading with the same obarray.
201   Interning usually happens automatically in the reader, but sometimes
202 other programs need to do it.  For example, after the @kbd{M-x} command
203 obtains the command name as a string using the minibuffer, it then
204 interns the string, to get the interned symbol with that name.
206 @cindex symbol equality
207 @cindex uninterned symbol
208   No obarray contains all symbols; in fact, some symbols are not in any
209 obarray.  They are called @dfn{uninterned symbols}.  An uninterned
210 symbol has the same four cells as other symbols; however, the only way
211 to gain access to it is by finding it in some other object or as the
212 value of a variable.
214   Creating an uninterned symbol is useful in generating Lisp code,
215 because an uninterned symbol used as a variable in the code you generate
216 cannot clash with any variables used in other Lisp programs.
218   In Emacs Lisp, an obarray is actually a vector.  Each element of the
219 vector is a bucket; its value is either an interned symbol whose name
220 hashes to that bucket, or 0 if the bucket is empty.  Each interned
221 symbol has an internal link (invisible to the user) to the next symbol
222 in the bucket.  Because these links are invisible, there is no way to
223 find all the symbols in an obarray except using @code{mapatoms} (below).
224 The order of symbols in a bucket is not significant.
226   In an empty obarray, every element is 0, so you can create an obarray
227 with @code{(make-vector @var{length} 0)}.  @strong{This is the only
228 valid way to create an obarray.}  Prime numbers as lengths tend
229 to result in good hashing; lengths one less than a power of two are also
230 good.
232   @strong{Do not try to put symbols in an obarray yourself.}  This does
233 not work---only @code{intern} can enter a symbol in an obarray properly.
235 @cindex CL note---symbol in obarrays
236 @quotation
237 @b{Common Lisp note:} Unlike Common Lisp, Emacs Lisp does not provide
238 for interning a single symbol in several obarrays.
239 @end quotation
241   Most of the functions below take a name and sometimes an obarray as
242 arguments.  A @code{wrong-type-argument} error is signaled if the name
243 is not a string, or if the obarray is not a vector.
245 @defun symbol-name symbol
246 This function returns the string that is @var{symbol}'s name.  For example:
248 @example
249 @group
250 (symbol-name 'foo)
251      @result{} "foo"
252 @end group
253 @end example
255 @strong{Warning:} Changing the string by substituting characters does
256 change the name of the symbol, but fails to update the obarray, so don't
257 do it!
258 @end defun
260 @defun make-symbol name
261 This function returns a newly-allocated, uninterned symbol whose name is
262 @var{name} (which must be a string).  Its value and function definition
263 are void, and its property list is @code{nil}.  In the example below,
264 the value of @code{sym} is not @code{eq} to @code{foo} because it is a
265 distinct uninterned symbol whose name is also @samp{foo}.
267 @example
268 (setq sym (make-symbol "foo"))
269      @result{} foo
270 (eq sym 'foo)
271      @result{} nil
272 @end example
273 @end defun
275 @defun intern name &optional obarray
276 This function returns the interned symbol whose name is @var{name}.  If
277 there is no such symbol in the obarray @var{obarray}, @code{intern}
278 creates a new one, adds it to the obarray, and returns it.  If
279 @var{obarray} is omitted, the value of the global variable
280 @code{obarray} is used.
282 @example
283 (setq sym (intern "foo"))
284      @result{} foo
285 (eq sym 'foo)
286      @result{} t
288 (setq sym1 (intern "foo" other-obarray))
289      @result{} foo
290 (eq sym1 'foo)
291      @result{} nil
292 @end example
293 @end defun
295 @cindex CL note---interning existing symbol
296 @quotation
297 @b{Common Lisp note:} In Common Lisp, you can intern an existing symbol
298 in an obarray.  In Emacs Lisp, you cannot do this, because the argument
299 to @code{intern} must be a string, not a symbol.
300 @end quotation
302 @defun intern-soft name &optional obarray
303 This function returns the symbol in @var{obarray} whose name is
304 @var{name}, or @code{nil} if @var{obarray} has no symbol with that name.
305 Therefore, you can use @code{intern-soft} to test whether a symbol with
306 a given name is already interned.  If @var{obarray} is omitted, the
307 value of the global variable @code{obarray} is used.
309 The argument @var{name} may also be a symbol; in that case,
310 the function returns @var{name} if @var{name} is interned
311 in the specified obarray, and otherwise @code{nil}.
313 @smallexample
314 (intern-soft "frazzle")        ; @r{No such symbol exists.}
315      @result{} nil
316 (make-symbol "frazzle")        ; @r{Create an uninterned one.}
317      @result{} frazzle
318 @group
319 (intern-soft "frazzle")        ; @r{That one cannot be found.}
320      @result{} nil
321 @end group
322 @group
323 (setq sym (intern "frazzle"))  ; @r{Create an interned one.}
324      @result{} frazzle
325 @end group
326 @group
327 (intern-soft "frazzle")        ; @r{That one can be found!}
328      @result{} frazzle
329 @end group
330 @group
331 (eq sym 'frazzle)              ; @r{And it is the same one.}
332      @result{} t
333 @end group
334 @end smallexample
335 @end defun
337 @defvar obarray
338 This variable is the standard obarray for use by @code{intern} and
339 @code{read}.
340 @end defvar
342 @defun mapatoms function &optional obarray
343 @anchor{Definition of mapatoms}
344 This function calls @var{function} once with each symbol in the obarray
345 @var{obarray}.  Then it returns @code{nil}.  If @var{obarray} is
346 omitted, it defaults to the value of @code{obarray}, the standard
347 obarray for ordinary symbols.
349 @smallexample
350 (setq count 0)
351      @result{} 0
352 (defun count-syms (s)
353   (setq count (1+ count)))
354      @result{} count-syms
355 (mapatoms 'count-syms)
356      @result{} nil
357 count
358      @result{} 1871
359 @end smallexample
361 See @code{documentation} in @ref{Accessing Documentation}, for another
362 example using @code{mapatoms}.
363 @end defun
365 @defun unintern symbol obarray
366 This function deletes @var{symbol} from the obarray @var{obarray}.  If
367 @code{symbol} is not actually in the obarray, @code{unintern} does
368 nothing.  If @var{obarray} is @code{nil}, the current obarray is used.
370 If you provide a string instead of a symbol as @var{symbol}, it stands
371 for a symbol name.  Then @code{unintern} deletes the symbol (if any) in
372 the obarray which has that name.  If there is no such symbol,
373 @code{unintern} does nothing.
375 If @code{unintern} does delete a symbol, it returns @code{t}.  Otherwise
376 it returns @code{nil}.
377 @end defun
379 @node Property Lists,, Creating Symbols, Symbols
380 @section Property Lists
381 @cindex property list
382 @cindex plist
384   A @dfn{property list} (@dfn{plist} for short) is a list of paired
385 elements.  Each of the pairs associates a property name (usually a
386 symbol) with a property or value.
388   Every symbol has a cell that stores a property list (@pxref{Symbol
389 Components}).  This property list is used to record information about
390 the symbol, such as its variable documentation and the name of the
391 file where it was defined.
393   Property lists can also be used in other contexts.  For instance,
394 you can assign property lists to character positions in a string or
395 buffer.  @xref{Text Properties}.
397   The property names and values in a property list can be any Lisp
398 objects, but the names are usually symbols.  Property list functions
399 compare the property names using @code{eq}.  Here is an example of a
400 property list, found on the symbol @code{progn} when the compiler is
401 loaded:
403 @example
404 (lisp-indent-function 0 byte-compile byte-compile-progn)
405 @end example
407 @noindent
408 Here @code{lisp-indent-function} and @code{byte-compile} are property
409 names, and the other two elements are the corresponding values.
411 @menu
412 * Plists and Alists::           Comparison of the advantages of property
413                                   lists and association lists.
414 * Symbol Plists::               Functions to access symbols' property lists.
415 * Other Plists::                Accessing property lists stored elsewhere.
416 @end menu
418 @node Plists and Alists
419 @subsection Property Lists and Association Lists
420 @cindex plist vs. alist
421 @cindex alist vs. plist
423 @cindex property lists vs association lists
424   Association lists (@pxref{Association Lists}) are very similar to
425 property lists.  In contrast to association lists, the order of the
426 pairs in the property list is not significant since the property names
427 must be distinct.
429   Property lists are better than association lists for attaching
430 information to various Lisp function names or variables.  If your
431 program keeps all such information in one association list, it will
432 typically need to search that entire list each time it checks for an
433 association for a particular Lisp function name or variable, which
434 could be slow.  By contrast, if you keep the same information in the
435 property lists of the function names or variables themselves, each
436 search will scan only the length of one property list, which is
437 usually short.  This is why the documentation for a variable is
438 recorded in a property named @code{variable-documentation}.  The byte
439 compiler likewise uses properties to record those functions needing
440 special treatment.
442   However, association lists have their own advantages.  Depending on
443 your application, it may be faster to add an association to the front of
444 an association list than to update a property.  All properties for a
445 symbol are stored in the same property list, so there is a possibility
446 of a conflict between different uses of a property name.  (For this
447 reason, it is a good idea to choose property names that are probably
448 unique, such as by beginning the property name with the program's usual
449 name-prefix for variables and functions.)  An association list may be
450 used like a stack where associations are pushed on the front of the list
451 and later discarded; this is not possible with a property list.
453 @node Symbol Plists
454 @subsection Property List Functions for Symbols
456 @defun symbol-plist symbol
457 This function returns the property list of @var{symbol}.
458 @end defun
460 @defun setplist symbol plist
461 This function sets @var{symbol}'s property list to @var{plist}.
462 Normally, @var{plist} should be a well-formed property list, but this is
463 not enforced.  The return value is @var{plist}.
465 @smallexample
466 (setplist 'foo '(a 1 b (2 3) c nil))
467      @result{} (a 1 b (2 3) c nil)
468 (symbol-plist 'foo)
469      @result{} (a 1 b (2 3) c nil)
470 @end smallexample
472 For symbols in special obarrays, which are not used for ordinary
473 purposes, it may make sense to use the property list cell in a
474 nonstandard fashion; in fact, the abbrev mechanism does so
475 (@pxref{Abbrevs}).
476 @end defun
478 @defun get symbol property
479 This function finds the value of the property named @var{property} in
480 @var{symbol}'s property list.  If there is no such property, @code{nil}
481 is returned.  Thus, there is no distinction between a value of
482 @code{nil} and the absence of the property.
484 The name @var{property} is compared with the existing property names
485 using @code{eq}, so any object is a legitimate property.
487 See @code{put} for an example.
488 @end defun
490 @defun put symbol property value
491 This function puts @var{value} onto @var{symbol}'s property list under
492 the property name @var{property}, replacing any previous property value.
493 The @code{put} function returns @var{value}.
495 @smallexample
496 (put 'fly 'verb 'transitive)
497      @result{}'transitive
498 (put 'fly 'noun '(a buzzing little bug))
499      @result{} (a buzzing little bug)
500 (get 'fly 'verb)
501      @result{} transitive
502 (symbol-plist 'fly)
503      @result{} (verb transitive noun (a buzzing little bug))
504 @end smallexample
505 @end defun
507 @node Other Plists
508 @subsection Property Lists Outside Symbols
510   These functions are useful for manipulating property lists
511 that are stored in places other than symbols:
513 @defun plist-get plist property
514 This returns the value of the @var{property} property stored in the
515 property list @var{plist}.  It accepts a malformed @var{plist}
516 argument.  If @var{property} is not found in the @var{plist}, it
517 returns @code{nil}.  For example,
519 @example
520 (plist-get '(foo 4) 'foo)
521      @result{} 4
522 (plist-get '(foo 4 bad) 'foo)
523      @result{} 4
524 (plist-get '(foo 4 bad) 'bad)
525      @result{} nil
526 (plist-get '(foo 4 bad) 'bar)
527      @result{} nil
528 @end example
529 @end defun
531 @defun plist-put plist property value
532 This stores @var{value} as the value of the @var{property} property in
533 the property list @var{plist}.  It may modify @var{plist} destructively,
534 or it may construct a new list structure without altering the old.  The
535 function returns the modified property list, so you can store that back
536 in the place where you got @var{plist}.  For example,
538 @example
539 (setq my-plist '(bar t foo 4))
540      @result{} (bar t foo 4)
541 (setq my-plist (plist-put my-plist 'foo 69))
542      @result{} (bar t foo 69)
543 (setq my-plist (plist-put my-plist 'quux '(a)))
544      @result{} (bar t foo 69 quux (a))
545 @end example
546 @end defun
548   You could define @code{put} in terms of @code{plist-put} as follows:
550 @example
551 (defun put (symbol prop value)
552   (setplist symbol
553             (plist-put (symbol-plist symbol) prop value)))
554 @end example
556 @defun lax-plist-get plist property
557 Like @code{plist-get} except that it compares properties
558 using @code{equal} instead of @code{eq}.
559 @end defun
561 @defun lax-plist-put plist property value
562 Like @code{plist-put} except that it compares properties
563 using @code{equal} instead of @code{eq}.
564 @end defun
566 @defun plist-member plist property
567 This returns non-@code{nil} if @var{plist} contains the given
568 @var{property}.  Unlike @code{plist-get}, this allows you to distinguish
569 between a missing property and a property with the value @code{nil}.
570 The value is actually the tail of @var{plist} whose @code{car} is
571 @var{property}.
572 @end defun