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