2 @c This is part of the GNU Emacs Lisp Reference Manual.
3 @c Copyright (C) 1990-1995, 1998-1999, 2001-2017 Free Software
5 @c See the file elisp.texi for copying conditions.
10 A @dfn{symbol} is an object with a unique name. This chapter
11 describes symbols, their components, their property lists, and how they
12 are created and interned. Separate chapters describe the use of symbols
13 as variables and as function names; see @ref{Variables}, and
14 @ref{Functions}. For the precise read syntax for symbols, see
17 You can test whether an arbitrary Lisp object is a symbol with
21 This function returns @code{t} if @var{object} is a symbol, @code{nil}
26 * Symbol Components:: Symbols have names, values, function definitions
28 * Definitions:: A definition says how a symbol will be used.
29 * Creating Symbols:: How symbols are kept unique.
30 * Symbol Properties:: Each symbol has a property list
31 for recording miscellaneous information.
34 @node Symbol Components
35 @section Symbol Components
36 @cindex symbol components
38 Each symbol has four components (or ``cells''), each of which
39 references another object:
43 @cindex print name cell
48 The symbol's current value as a variable.
52 The symbol's function definition. It can also hold a symbol, a
53 keymap, or a keyboard macro.
56 @cindex property list cell
57 The symbol's property list.
61 The print name cell always holds a string, and cannot be changed.
62 Each of the other three cells can be set to any Lisp object.
64 The print name cell holds the string that is the name of a symbol.
65 Since symbols are represented textually by their names, it is
66 important not to have two symbols with the same name. The Lisp reader
67 ensures this: every time it reads a symbol, it looks for an existing
68 symbol with the specified name before it creates a new one. To get a
69 symbol's name, use the function @code{symbol-name} (@pxref{Creating
72 The value cell holds a symbol's value as a variable, which is what
73 you get if the symbol itself is evaluated as a Lisp expression.
74 @xref{Variables}, for details about how values are set and retrieved,
75 including complications such as @dfn{local bindings} and @dfn{scoping
76 rules}. Most symbols can have any Lisp object as a value, but certain
77 special 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
82 The function cell holds a symbol's function definition. Often, we
83 refer to ``the function @code{foo}'' when we really mean the function
84 stored in the function cell of @code{foo}; we make the distinction
85 explicit only when necessary. Typically, the function cell is used to
86 hold a function (@pxref{Functions}) or a macro (@pxref{Macros}).
87 However, it can also be used to hold a symbol (@pxref{Function
88 Indirection}), keyboard macro (@pxref{Keyboard Macros}), keymap
89 (@pxref{Keymaps}), or autoload object (@pxref{Autoloading}). To get
90 the contents of a symbol's function cell, use the function
91 @code{symbol-function} (@pxref{Function Cells}).
93 The property list cell normally should hold a correctly formatted
94 property list. To get a symbol's property list, use the function
95 @code{symbol-plist}. @xref{Symbol Properties}.
97 The function cell or the value cell may be @dfn{void}, which means
98 that the cell does not reference any object. (This is not the same
99 thing as holding the symbol @code{void}, nor the same as holding the
100 symbol @code{nil}.) Examining a function or value cell that is void
101 results in an error, such as @samp{Symbol's value as variable is void}.
103 Because each symbol has separate value and function cells, variables
104 names and function names do not conflict. For example, the symbol
105 @code{buffer-file-name} has a value (the name of the file being
106 visited in the current buffer) as well as a function definition (a
107 primitive function that returns the name of the file):
111 @result{} "/gnu/elisp/symbols.texi"
112 (symbol-function 'buffer-file-name)
113 @result{} #<subr buffer-file-name>
117 @section Defining Symbols
118 @cindex definitions of symbols
120 A @dfn{definition} is a special kind of Lisp expression that
121 announces your intention to use a symbol in a particular way. It
122 typically specifies a value or meaning for the symbol for one kind of
123 use, plus documentation for its meaning when used in this way. Thus,
124 when you define a symbol as a variable, you can supply an initial
125 value for the variable, plus documentation for the variable.
127 @code{defvar} and @code{defconst} are special forms that define a
128 symbol as a @dfn{global variable}---a variable that can be accessed at
129 any point in a Lisp program. @xref{Variables}, for details about
130 variables. To define a customizable variable, use the
131 @code{defcustom} macro, which also calls @code{defvar} as a subroutine
132 (@pxref{Customization}).
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.
136 However, you ought to write a variable definition for each global
137 variable that you want to use; otherwise, your Lisp program may not
138 act correctly if it is evaluated with lexical scoping enabled
139 (@pxref{Variable Scoping}).
141 @code{defun} defines a symbol as a function, creating a lambda
142 expression and storing it in the function cell of the symbol. This
143 lambda expression thus becomes the function definition of the symbol.
144 (The term ``function definition'', meaning the contents of the function
145 cell, is derived from the idea that @code{defun} gives the symbol its
146 definition as a function.) @code{defsubst} and @code{defalias} are two
147 other ways of defining a function. @xref{Functions}.
149 @code{defmacro} defines a symbol as a macro. It creates a macro
150 object and stores it in the function cell of the symbol. Note that a
151 given symbol can be a macro or a function, but not both at once, because
152 both macro and function definitions are kept in the function cell, and
153 that cell can hold only one Lisp object at any given time.
156 As previously noted, Emacs Lisp allows the same symbol to be defined
157 both as a variable (e.g., with @code{defvar}) and as a function or
158 macro (e.g., with @code{defun}). Such definitions do not conflict.
160 These definitions also act as guides for programming tools. For
161 example, the @kbd{C-h f} and @kbd{C-h v} commands create help buffers
162 containing links to the relevant variable, function, or macro
163 definitions. @xref{Name Help,,, emacs, The GNU Emacs Manual}.
165 @node Creating Symbols
166 @section Creating and Interning Symbols
167 @cindex reading symbols
169 To understand how symbols are created in GNU Emacs Lisp, you must know
170 how Lisp reads them. Lisp must ensure that it finds the same symbol
171 every time it reads the same set of characters. Failure to do so would
172 cause complete confusion.
174 @cindex symbol name hashing
177 @cindex bucket (in obarray)
178 When the Lisp reader encounters a symbol, it reads all the characters
179 of the name. Then it hashes those characters to find an index in a
180 table called an @dfn{obarray}. Hashing is an efficient method of
181 looking something up. For example, instead of searching a telephone
182 book cover to cover when looking up Jan Jones, you start with the J's
183 and go from there. That is a simple version of hashing. Each element
184 of the obarray is a @dfn{bucket} which holds all the symbols with a
185 given hash code; to look for a given name, it is sufficient to look
186 through all the symbols in the bucket for that name's hash code. (The
187 same idea is used for general Emacs hash tables, but they are a
188 different data type; see @ref{Hash Tables}.)
191 If a symbol with the desired name is found, the reader uses that
192 symbol. If the obarray does not contain a symbol with that name, the
193 reader makes a new symbol and adds it to the obarray. Finding or adding
194 a symbol with a certain name is called @dfn{interning} it, and the
195 symbol is then called an @dfn{interned symbol}.
197 Interning ensures that each obarray has just one symbol with any
198 particular name. Other like-named symbols may exist, but not in the
199 same obarray. Thus, the reader gets the same symbols for the same
200 names, as long as you keep reading with the same obarray.
202 Interning usually happens automatically in the reader, but sometimes
203 other programs need to do it. For example, after the @kbd{M-x} command
204 obtains the command name as a string using the minibuffer, it then
205 interns the string, to get the interned symbol with that name.
207 @cindex symbol equality
208 @cindex uninterned symbol
209 No obarray contains all symbols; in fact, some symbols are not in any
210 obarray. They are called @dfn{uninterned symbols}. An uninterned
211 symbol has the same four cells as other symbols; however, the only way
212 to gain access to it is by finding it in some other object or as the
215 Creating an uninterned symbol is useful in generating Lisp code,
216 because an uninterned symbol used as a variable in the code you generate
217 cannot clash with any variables used in other Lisp programs.
219 In Emacs Lisp, an obarray is actually a vector. Each element of the
220 vector is a bucket; its value is either an interned symbol whose name
221 hashes to that bucket, or 0 if the bucket is empty. Each interned
222 symbol has an internal link (invisible to the user) to the next symbol
223 in the bucket. Because these links are invisible, there is no way to
224 find all the symbols in an obarray except using @code{mapatoms} (below).
225 The order of symbols in a bucket is not significant.
227 In an empty obarray, every element is 0, so you can create an obarray
228 with @code{(make-vector @var{length} 0)}. @strong{This is the only
229 valid way to create an obarray.} Prime numbers as lengths tend
230 to result in good hashing; lengths one less than a power of two are also
233 @strong{Do not try to put symbols in an obarray yourself.} This does
234 not work---only @code{intern} can enter a symbol in an obarray properly.
236 @cindex CL note---symbol in obarrays
238 @b{Common Lisp note:} Unlike Common Lisp, Emacs Lisp does not provide
239 for interning a single symbol in several obarrays.
242 Most of the functions below take a name and sometimes an obarray as
243 arguments. A @code{wrong-type-argument} error is signaled if the name
244 is not a string, or if the obarray is not a vector.
246 @defun symbol-name symbol
247 This function returns the string that is @var{symbol}'s name. For example:
256 @strong{Warning:} Changing the string by substituting characters does
257 change the name of the symbol, but fails to update the obarray, so don't
261 @defun make-symbol name
262 This function returns a newly-allocated, uninterned symbol whose name is
263 @var{name} (which must be a string). Its value and function definition
264 are void, and its property list is @code{nil}. In the example below,
265 the value of @code{sym} is not @code{eq} to @code{foo} because it is a
266 distinct uninterned symbol whose name is also @samp{foo}.
269 (setq sym (make-symbol "foo"))
276 @defun gensym &optional prefix
277 This function returns a symbol using @code{make-symbol}, whose name is
278 made by appending @code{gensym-counter} to @var{prefix}. The prefix
279 defaults to @code{"g"}.
282 @defun intern name &optional obarray
283 This function returns the interned symbol whose name is @var{name}. If
284 there is no such symbol in the obarray @var{obarray}, @code{intern}
285 creates a new one, adds it to the obarray, and returns it. If
286 @var{obarray} is omitted, the value of the global variable
287 @code{obarray} is used.
290 (setq sym (intern "foo"))
295 (setq sym1 (intern "foo" other-obarray))
302 @cindex CL note---interning existing symbol
304 @b{Common Lisp note:} In Common Lisp, you can intern an existing symbol
305 in an obarray. In Emacs Lisp, you cannot do this, because the argument
306 to @code{intern} must be a string, not a symbol.
309 @defun intern-soft name &optional obarray
310 This function returns the symbol in @var{obarray} whose name is
311 @var{name}, or @code{nil} if @var{obarray} has no symbol with that name.
312 Therefore, you can use @code{intern-soft} to test whether a symbol with
313 a given name is already interned. If @var{obarray} is omitted, the
314 value of the global variable @code{obarray} is used.
316 The argument @var{name} may also be a symbol; in that case,
317 the function returns @var{name} if @var{name} is interned
318 in the specified obarray, and otherwise @code{nil}.
321 (intern-soft "frazzle") ; @r{No such symbol exists.}
323 (make-symbol "frazzle") ; @r{Create an uninterned one.}
326 (intern-soft "frazzle") ; @r{That one cannot be found.}
330 (setq sym (intern "frazzle")) ; @r{Create an interned one.}
334 (intern-soft "frazzle") ; @r{That one can be found!}
338 (eq sym 'frazzle) ; @r{And it is the same one.}
345 This variable is the standard obarray for use by @code{intern} and
349 @defun mapatoms function &optional obarray
350 @anchor{Definition of mapatoms}
351 This function calls @var{function} once with each symbol in the obarray
352 @var{obarray}. Then it returns @code{nil}. If @var{obarray} is
353 omitted, it defaults to the value of @code{obarray}, the standard
354 obarray for ordinary symbols.
359 (defun count-syms (s)
360 (setq count (1+ count)))
362 (mapatoms 'count-syms)
368 See @code{documentation} in @ref{Accessing Documentation}, for another
369 example using @code{mapatoms}.
372 @defun unintern symbol obarray
373 This function deletes @var{symbol} from the obarray @var{obarray}. If
374 @code{symbol} is not actually in the obarray, @code{unintern} does
375 nothing. If @var{obarray} is @code{nil}, the current obarray is used.
377 If you provide a string instead of a symbol as @var{symbol}, it stands
378 for a symbol name. Then @code{unintern} deletes the symbol (if any) in
379 the obarray which has that name. If there is no such symbol,
380 @code{unintern} does nothing.
382 If @code{unintern} does delete a symbol, it returns @code{t}. Otherwise
383 it returns @code{nil}.
386 @node Symbol Properties
387 @section Symbol Properties
388 @cindex symbol property
390 A symbol may possess any number of @dfn{symbol properties}, which
391 can be used to record miscellaneous information about the symbol. For
392 example, when a symbol has a @code{risky-local-variable} property with
393 a non-@code{nil} value, that means the variable which the symbol names
394 is a risky file-local variable (@pxref{File Local Variables}).
396 Each symbol's properties and property values are stored in the
397 symbol's property list cell (@pxref{Symbol Components}), in the form
398 of a property list (@pxref{Property Lists}).
401 * Symbol Plists:: Accessing symbol properties.
402 * Standard Properties:: Standard meanings of symbol properties.
406 @subsection Accessing Symbol Properties
408 The following functions can be used to access symbol properties.
410 @defun get symbol property
411 This function returns the value of the property named @var{property}
412 in @var{symbol}'s property list. If there is no such property, it
413 returns @code{nil}. Thus, there is no distinction between a value of
414 @code{nil} and the absence of the property.
416 The name @var{property} is compared with the existing property names
417 using @code{eq}, so any object is a legitimate property.
419 See @code{put} for an example.
422 @defun put symbol property value
423 This function puts @var{value} onto @var{symbol}'s property list under
424 the property name @var{property}, replacing any previous property value.
425 The @code{put} function returns @var{value}.
428 (put 'fly 'verb 'transitive)
430 (put 'fly 'noun '(a buzzing little bug))
431 @result{} (a buzzing little bug)
435 @result{} (verb transitive noun (a buzzing little bug))
439 @defun symbol-plist symbol
440 This function returns the property list of @var{symbol}.
443 @defun setplist symbol plist
444 This function sets @var{symbol}'s property list to @var{plist}.
445 Normally, @var{plist} should be a well-formed property list, but this is
446 not enforced. The return value is @var{plist}.
449 (setplist 'foo '(a 1 b (2 3) c nil))
450 @result{} (a 1 b (2 3) c nil)
452 @result{} (a 1 b (2 3) c nil)
455 For symbols in special obarrays, which are not used for ordinary
456 purposes, it may make sense to use the property list cell in a
457 nonstandard fashion; in fact, the abbrev mechanism does so
460 You could define @code{put} in terms of @code{setplist} and
461 @code{plist-put}, as follows:
464 (defun put (symbol prop value)
466 (plist-put (symbol-plist symbol) prop value)))
470 @defun function-get symbol property &optional autoload
471 This function is identical to @code{get}, except that if @var{symbol}
472 is the name of a function alias, it looks in the property list of the
473 symbol naming the actual function. @xref{Defining Functions}. If the
474 optional argument @var{autoload} is non-@code{nil}, and @var{symbol}
475 is auto-loaded, this function will try to autoload it, since
476 autoloading might set @var{property} of @var{symbol}. If
477 @var{autoload} is the symbol @code{macro}, only try autoloading if
478 @var{symbol} is an auto-loaded macro.
481 @defun function-put function property value
482 This function sets @var{property} of @var{function} to @var{value}.
483 @var{function} should be a symbol. This function is preferred to
484 calling @code{put} for setting properties of a function, because it
485 will allow us some day to implement remapping of old properties to new
489 @node Standard Properties
490 @subsection Standard Symbol Properties
492 Here, we list the symbol properties which are used for special
493 purposes in Emacs. In the following table, whenever we say ``the
494 named function'', that means the function whose name is the relevant
495 symbol; similarly for ``the named variable'' etc.
498 @item :advertised-binding
499 This property value specifies the preferred key binding, when showing
500 documentation, for the named function. @xref{Keys in Documentation}.
502 @item char-table-extra-slots
503 The value, if non-@code{nil}, specifies the number of extra slots in
504 the named char-table type. @xref{Char-Tables}.
506 @item customized-face
507 @itemx face-defface-spec
510 These properties are used to record a face's standard, saved,
511 customized, and themed face specs. Do not set them directly; they are
512 managed by @code{defface} and related functions. @xref{Defining
515 @item customized-value
517 @itemx standard-value
519 These properties are used to record a customizable variable's standard
520 value, saved value, customized-but-unsaved value, and themed values.
521 Do not set them directly; they are managed by @code{defcustom} and
522 related functions. @xref{Variable Definitions}.
525 If the value is non-@code{nil}, the named function is disabled as a
526 command. @xref{Disabling Commands}.
528 @item face-documentation
529 The value stores the documentation string of the named face. This is
530 set automatically by @code{defface}. @xref{Defining Faces}.
533 The value, if non-@code{nil}, specifies the maximum minibuffer history
534 length for the named history list variable. @xref{Minibuffer
537 @item interactive-form
538 The value is an interactive form for the named function. Normally,
539 you should not set this directly; use the @code{interactive} special
540 form instead. @xref{Interactive Call}.
543 The value is an expression for determining whether the named menu item
544 should be enabled in menus. @xref{Simple Menu Items}.
547 If the value is @code{special}, the named major mode is special.
548 @xref{Major Mode Conventions}.
550 @item permanent-local
551 If the value is non-@code{nil}, the named variable is a buffer-local
552 variable whose value should not be reset when changing major modes.
553 @xref{Creating Buffer-Local}.
555 @item permanent-local-hook
556 If the value is non-@code{nil}, the named function should not be
557 deleted from the local value of a hook variable when changing major
558 modes. @xref{Setting Hooks}.
561 If the value is non-@code{nil}, the named function is considered to be
562 side-effect free. Calls with constant arguments can be evaluated at
563 compile time. This may shift run time errors to compile time.
565 @item risky-local-variable
566 If the value is non-@code{nil}, the named variable is considered risky
567 as a file-local variable. @xref{File Local Variables}.
570 If the value is non-@code{nil}, the named function is considered
571 generally safe for evaluation. @xref{Function Safety}.
573 @item safe-local-eval-function
574 If the value is non-@code{nil}, the named function is safe to call in
575 file-local evaluation forms. @xref{File Local Variables}.
577 @item safe-local-variable
578 The value specifies a function for determining safe file-local values
579 for the named variable. @xref{File Local Variables}.
581 @item side-effect-free
582 A non-@code{nil} value indicates that the named function is free of
583 side-effects, for determining function safety (@pxref{Function
584 Safety}) as well as for byte compiler optimizations. Do not set it.
586 @item variable-documentation
587 If non-@code{nil}, this specifies the named variable's documentation
588 string. This is set automatically by @code{defvar} and related
589 functions. @xref{Defining Faces}.