Merge from emacs-24; up to 2012-12-01T13:25:13Z!cyd@gnu.org
[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.
ecc6530d 5@node Symbols
b8d4c8d0
GM
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
f02f19bd
CY
16 You can test whether an arbitrary Lisp object is a symbol with
17@code{symbolp}:
b8d4c8d0
GM
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.
f02f19bd 29* Symbol Properties:: Each symbol has a property list
b8d4c8d0
GM
30 for recording miscellaneous information.
31@end menu
32
ecc6530d 33@node Symbol Components
b8d4c8d0
GM
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
f02f19bd 94@code{symbol-plist}. @xref{Symbol Properties}.
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
ecc6530d 115@node Definitions
b8d4c8d0
GM
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 155 As previously noted, Emacs Lisp allows the same symbol to be defined
1df7defd
PE
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.
31cbea1d
CY
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 163
ecc6530d 164@node Creating Symbols
b8d4c8d0
GM
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
f02f19bd
CY
379@node Symbol Properties
380@section Symbol Properties
381@cindex symbol property
b8d4c8d0 382
f02f19bd
CY
383 A symbol may possess any number of @dfn{symbol properties}, which
384can be used to record miscellaneous information about the symbol. For
385example, when a symbol has a @code{risky-local-variable} property with
386a non-@code{nil} value, that means the variable which the symbol names
387is a risky file-local variable (@pxref{File Local Variables}).
b8d4c8d0 388
f02f19bd
CY
389 Each symbol's properties and property values are stored in the
390symbol's property list cell (@pxref{Symbol Components}), in the form
391of a property list (@pxref{Property Lists}).
b8d4c8d0
GM
392
393@menu
f02f19bd
CY
394* Symbol Plists:: Accessing symbol properties.
395* Standard Properties:: Standard meanings of symbol properties.
b8d4c8d0
GM
396@end menu
397
b8d4c8d0 398@node Symbol Plists
f02f19bd 399@subsection Accessing Symbol Properties
b8d4c8d0 400
f02f19bd 401 The following functions can be used to access symbol properties.
b8d4c8d0
GM
402
403@defun get symbol property
f02f19bd
CY
404This function returns the value of the property named @var{property}
405in @var{symbol}'s property list. If there is no such property, it
406returns @code{nil}. Thus, there is no distinction between a value of
b8d4c8d0
GM
407@code{nil} and the absence of the property.
408
409The name @var{property} is compared with the existing property names
410using @code{eq}, so any object is a legitimate property.
411
412See @code{put} for an example.
413@end defun
414
415@defun put symbol property value
416This function puts @var{value} onto @var{symbol}'s property list under
417the property name @var{property}, replacing any previous property value.
418The @code{put} function returns @var{value}.
419
ddff3351 420@example
b8d4c8d0
GM
421(put 'fly 'verb 'transitive)
422 @result{}'transitive
423(put 'fly 'noun '(a buzzing little bug))
424 @result{} (a buzzing little bug)
425(get 'fly 'verb)
426 @result{} transitive
427(symbol-plist 'fly)
428 @result{} (verb transitive noun (a buzzing little bug))
ddff3351 429@end example
b8d4c8d0
GM
430@end defun
431
f02f19bd
CY
432@defun symbol-plist symbol
433This function returns the property list of @var{symbol}.
b8d4c8d0
GM
434@end defun
435
f02f19bd
CY
436@defun setplist symbol plist
437This function sets @var{symbol}'s property list to @var{plist}.
438Normally, @var{plist} should be a well-formed property list, but this is
439not enforced. The return value is @var{plist}.
b8d4c8d0
GM
440
441@example
f02f19bd
CY
442(setplist 'foo '(a 1 b (2 3) c nil))
443 @result{} (a 1 b (2 3) c nil)
444(symbol-plist 'foo)
445 @result{} (a 1 b (2 3) c nil)
b8d4c8d0
GM
446@end example
447
f02f19bd
CY
448For symbols in special obarrays, which are not used for ordinary
449purposes, it may make sense to use the property list cell in a
450nonstandard fashion; in fact, the abbrev mechanism does so
451(@pxref{Abbrevs}).
a20ae0b9
CY
452
453You could define @code{put} in terms of @code{setplist} and
454@code{plist-put}, as follows:
455
456@example
457(defun put (symbol prop value)
458 (setplist symbol
459 (plist-put (symbol-plist symbol) prop value)))
460@end example
b8d4c8d0
GM
461@end defun
462
f02f19bd
CY
463@defun function-get symbol property
464This function is identical to @code{get}, except that if @var{symbol}
465is the name of a function alias, it looks in the property list of the
466symbol naming the actual function. @xref{Defining Functions}.
b8d4c8d0
GM
467@end defun
468
f02f19bd
CY
469@node Standard Properties
470@subsection Standard Symbol Properties
471
472 Here, we list the symbol properties which are used for special
473purposes in Emacs. In the following table, whenever we say ``the
474named function'', that means the function whose name is the relevant
475symbol; similarly for ``the named variable'' etc.
476
477@table @code
478@item :advertised-binding
479This property value specifies the preferred key binding, when showing
480documentation, for the named function. @xref{Keys in Documentation}.
481
482@item char-table-extra-slots
483The value, if non-@code{nil}, specifies the number of extra slots in
484the named char-table type. @xref{Char-Tables}.
485
a20ae0b9
CY
486@item customized-face
487@itemx face-defface-spec
f02f19bd
CY
488@itemx saved-face
489@itemx theme-face
490These properties are used to record a face's standard, saved,
491customized, and themed face specs. Do not set them directly; they are
492managed by @code{defface} and related functions. @xref{Defining
493Faces}.
494
a20ae0b9 495@item customized-value
f02f19bd 496@itemx saved-value
a20ae0b9 497@itemx standard-value
f02f19bd
CY
498@itemx theme-value
499These properties are used to record a customizable variable's standard
500value, saved value, customized-but-unsaved value, and themed values.
501Do not set them directly; they are managed by @code{defcustom} and
502related functions. @xref{Variable Definitions}.
503
504@item disabled
505If the value is non-@code{nil}, the named function is disabled as a
506command. @xref{Disabling Commands}.
507
508@item face-documentation
509The value stores the documentation string of the named face. This is
a20ae0b9 510set automatically by @code{defface}. @xref{Defining Faces}.
f02f19bd
CY
511
512@item history-length
513The value, if non-@code{nil}, specifies the maximum minibuffer history
514length for the named history list variable. @xref{Minibuffer
515History}.
516
517@item interactive-form
518The value is an interactive form for the named function. Normally,
519you should not set this directly; use the @code{interactive} special
520form instead. @xref{Interactive Call}.
521
522@item menu-enable
523The value is an expression for determining whether the named menu item
524should be enabled in menus. @xref{Simple Menu Items}.
525
526@item mode-class
527If the value is @code{special}, the named major mode is ``special''.
528@xref{Major Mode Conventions}.
529
530@item permanent-local
531If the value is non-@code{nil}, the named variable is a buffer-local
532variable whose value should not be reset when changing major modes.
533@xref{Creating Buffer-Local}.
534
535@item permanent-local-hook
536If the value is non-@code{nil}, the named function should not be
537deleted from the local value of a hook variable when changing major
538modes. @xref{Setting Hooks}.
539
540@item pure
541This property is used internally to mark certain named functions for
542byte compiler optimization. Do not set it.
543
544@item risky-local-variable
545If the value is non-@code{nil}, the named variable is considered risky
546as a file-local variable. @xref{File Local Variables}.
547
548@item safe-function
549If the value is non-@code{nil}, the named function is considered
550generally safe for evaluation. @xref{Function Safety}.
551
552@item safe-local-eval-function
553If the value is non-@code{nil}, the named function is safe to call in
554file-local evaluation forms. @xref{File Local Variables}.
555
556@item safe-local-variable
557The value specifies a function for determining safe file-local values
558for the named variable. @xref{File Local Variables}.
559
560@item side-effect-free
561A non-@code{nil} value indicates that the named function is free of
562side-effects, for determining function safety (@pxref{Function
563Safety}) as well as for byte compiler optimizations. Do not set it.
564
565@item variable-documentation
f24f2e22 566If non-@code{nil}, this specifies the named variable's documentation
a20ae0b9
CY
567string. This is set automatically by @code{defvar} and related
568functions. @xref{Defining Faces}.
f02f19bd 569@end table