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