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