Update copyright year to 2014 by running admin/update-copyright.
[emacs.git] / doc / lispref / symbols.texi
blobe4455692d4508968c435c968365e571a30d6e2b9
1 @c -*-texinfo-*-
2 @c This is part of the GNU Emacs Lisp Reference Manual.
3 @c Copyright (C) 1990-1995, 1998-1999, 2001-2014 Free Software
4 @c Foundation, Inc.
5 @c See the file elisp.texi for copying conditions.
6 @node Symbols
7 @chapter Symbols
8 @cindex symbol
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
15 @ref{Symbol Type}.
17   You can test whether an arbitrary Lisp object is a symbol with
18 @code{symbolp}:
20 @defun symbolp object
21 This function returns @code{t} if @var{object} is a symbol, @code{nil}
22 otherwise.
23 @end defun
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 * Symbol Properties::        Each symbol has a property list
31                                for recording miscellaneous information.
32 @end menu
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:
41 @table @asis
42 @item Print name
43 @cindex print name cell
44 The symbol's name.
46 @item Value
47 @cindex value cell
48 The symbol's current value as a variable.
50 @item Function
51 @cindex function cell
52 The symbol's function definition.  It can also hold a symbol, a
53 keymap, or a keyboard macro.
55 @item Property list
56 @cindex property list cell
57 The symbol's property list.
58 @end table
60 @noindent
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
70 Symbols}).
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
80 Variables}.
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):
109 @example
110 buffer-file-name
111      @result{} "/gnu/elisp/symbols.texi"
112 (symbol-function 'buffer-file-name)
113      @result{} #<subr buffer-file-name>
114 @end example
116 @node Definitions
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.
154 @xref{Macros}.
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 definition 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
175 @cindex hashing
176 @cindex obarray
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}.)
190 @cindex interning
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
213 value of a variable.
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
231 good.
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
237 @quotation
238 @b{Common Lisp note:} Unlike Common Lisp, Emacs Lisp does not provide
239 for interning a single symbol in several obarrays.
240 @end quotation
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:
249 @example
250 @group
251 (symbol-name 'foo)
252      @result{} "foo"
253 @end group
254 @end 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
258 do it!
259 @end defun
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}.
268 @example
269 (setq sym (make-symbol "foo"))
270      @result{} foo
271 (eq sym 'foo)
272      @result{} nil
273 @end example
274 @end defun
276 @defun intern name &optional obarray
277 This function returns the interned symbol whose name is @var{name}.  If
278 there is no such symbol in the obarray @var{obarray}, @code{intern}
279 creates 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.
283 @example
284 (setq sym (intern "foo"))
285      @result{} foo
286 (eq sym 'foo)
287      @result{} t
289 (setq sym1 (intern "foo" other-obarray))
290      @result{} foo
291 (eq sym1 'foo)
292      @result{} nil
293 @end example
294 @end defun
296 @cindex CL note---interning existing symbol
297 @quotation
298 @b{Common Lisp note:} In Common Lisp, you can intern an existing symbol
299 in an obarray.  In Emacs Lisp, you cannot do this, because the argument
300 to @code{intern} must be a string, not a symbol.
301 @end quotation
303 @defun intern-soft name &optional obarray
304 This 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.
306 Therefore, you can use @code{intern-soft} to test whether a symbol with
307 a given name is already interned.  If @var{obarray} is omitted, the
308 value of the global variable @code{obarray} is used.
310 The argument @var{name} may also be a symbol; in that case,
311 the function returns @var{name} if @var{name} is interned
312 in the specified obarray, and otherwise @code{nil}.
314 @example
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 example
336 @end defun
338 @defvar obarray
339 This variable is the standard obarray for use by @code{intern} and
340 @code{read}.
341 @end defvar
343 @defun mapatoms function &optional obarray
344 @anchor{Definition of mapatoms}
345 This function calls @var{function} once with each symbol in the obarray
346 @var{obarray}.  Then it returns @code{nil}.  If @var{obarray} is
347 omitted, it defaults to the value of @code{obarray}, the standard
348 obarray for ordinary symbols.
350 @example
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
358 count
359      @result{} 1871
360 @end example
362 See @code{documentation} in @ref{Accessing Documentation}, for another
363 example using @code{mapatoms}.
364 @end defun
366 @defun unintern symbol obarray
367 This function deletes @var{symbol} from the obarray @var{obarray}.  If
368 @code{symbol} is not actually in the obarray, @code{unintern} does
369 nothing.  If @var{obarray} is @code{nil}, the current obarray is used.
371 If you provide a string instead of a symbol as @var{symbol}, it stands
372 for a symbol name.  Then @code{unintern} deletes the symbol (if any) in
373 the obarray which has that name.  If there is no such symbol,
374 @code{unintern} does nothing.
376 If @code{unintern} does delete a symbol, it returns @code{t}.  Otherwise
377 it returns @code{nil}.
378 @end defun
380 @node Symbol Properties
381 @section Symbol Properties
382 @cindex symbol property
384   A symbol may possess any number of @dfn{symbol properties}, which
385 can be used to record miscellaneous information about the symbol.  For
386 example, when a symbol has a @code{risky-local-variable} property with
387 a non-@code{nil} value, that means the variable which the symbol names
388 is a risky file-local variable (@pxref{File Local Variables}).
390   Each symbol's properties and property values are stored in the
391 symbol's property list cell (@pxref{Symbol Components}), in the form
392 of a property list (@pxref{Property Lists}).
394 @menu
395 * Symbol Plists::        Accessing symbol properties.
396 * Standard Properties::  Standard meanings of symbol properties.
397 @end menu
399 @node Symbol Plists
400 @subsection Accessing Symbol Properties
402   The following functions can be used to access symbol properties.
404 @defun get symbol property
405 This function returns the value of the property named @var{property}
406 in @var{symbol}'s property list.  If there is no such property, it
407 returns @code{nil}.  Thus, there is no distinction between a value of
408 @code{nil} and the absence of the property.
410 The name @var{property} is compared with the existing property names
411 using @code{eq}, so any object is a legitimate property.
413 See @code{put} for an example.
414 @end defun
416 @defun put symbol property value
417 This function puts @var{value} onto @var{symbol}'s property list under
418 the property name @var{property}, replacing any previous property value.
419 The @code{put} function returns @var{value}.
421 @example
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))
430 @end example
431 @end defun
433 @defun symbol-plist symbol
434 This function returns the property list of @var{symbol}.
435 @end defun
437 @defun setplist symbol plist
438 This function sets @var{symbol}'s property list to @var{plist}.
439 Normally, @var{plist} should be a well-formed property list, but this is
440 not enforced.  The return value is @var{plist}.
442 @example
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)
447 @end example
449 For symbols in special obarrays, which are not used for ordinary
450 purposes, it may make sense to use the property list cell in a
451 nonstandard fashion; in fact, the abbrev mechanism does so
452 (@pxref{Abbrevs}).
454 You could define @code{put} in terms of @code{setplist} and
455 @code{plist-put}, as follows:
457 @example
458 (defun put (symbol prop value)
459   (setplist symbol
460             (plist-put (symbol-plist symbol) prop value)))
461 @end example
462 @end defun
464 @defun function-get symbol property
465 This function is identical to @code{get}, except that if @var{symbol}
466 is the name of a function alias, it looks in the property list of the
467 symbol naming the actual function.  @xref{Defining Functions}.
468 @end defun
470 @node Standard Properties
471 @subsection Standard Symbol Properties
473   Here, we list the symbol properties which are used for special
474 purposes in Emacs.  In the following table, whenever we say ``the
475 named function'', that means the function whose name is the relevant
476 symbol; similarly for ``the named variable'' etc.
478 @table @code
479 @item :advertised-binding
480 This property value specifies the preferred key binding, when showing
481 documentation, for the named function.  @xref{Keys in Documentation}.
483 @item char-table-extra-slots
484 The value, if non-@code{nil}, specifies the number of extra slots in
485 the named char-table type.  @xref{Char-Tables}.
487 @item customized-face
488 @itemx face-defface-spec
489 @itemx saved-face
490 @itemx theme-face
491 These properties are used to record a face's standard, saved,
492 customized, and themed face specs.  Do not set them directly; they are
493 managed by @code{defface} and related functions.  @xref{Defining
494 Faces}.
496 @item customized-value
497 @itemx saved-value
498 @itemx standard-value
499 @itemx theme-value
500 These properties are used to record a customizable variable's standard
501 value, saved value, customized-but-unsaved value, and themed values.
502 Do not set them directly; they are managed by @code{defcustom} and
503 related functions.  @xref{Variable Definitions}.
505 @item disabled
506 If the value is non-@code{nil}, the named function is disabled as a
507 command.  @xref{Disabling Commands}.
509 @item face-documentation
510 The value stores the documentation string of the named face.  This is
511 set automatically by @code{defface}.  @xref{Defining Faces}.
513 @item history-length
514 The value, if non-@code{nil}, specifies the maximum minibuffer history
515 length for the named history list variable.  @xref{Minibuffer
516 History}.
518 @item interactive-form
519 The value is an interactive form for the named function.  Normally,
520 you should not set this directly; use the @code{interactive} special
521 form instead.  @xref{Interactive Call}.
523 @item menu-enable
524 The value is an expression for determining whether the named menu item
525 should be enabled in menus.  @xref{Simple Menu Items}.
527 @item mode-class
528 If the value is @code{special}, the named major mode is ``special''.
529 @xref{Major Mode Conventions}.
531 @item permanent-local
532 If the value is non-@code{nil}, the named variable is a buffer-local
533 variable whose value should not be reset when changing major modes.
534 @xref{Creating Buffer-Local}.
536 @item permanent-local-hook
537 If the value is non-@code{nil}, the named function should not be
538 deleted from the local value of a hook variable when changing major
539 modes.  @xref{Setting Hooks}.
541 @item pure
542 If the value is non-@code{nil}, the named function is considered to be
543 side-effect free.  Calls with constant arguments can be evaluated at
544 compile time.  This may shift run time errors to compile time.
546 @item risky-local-variable
547 If the value is non-@code{nil}, the named variable is considered risky
548 as a file-local variable.  @xref{File Local Variables}.
550 @item safe-function
551 If the value is non-@code{nil}, the named function is considered
552 generally safe for evaluation.  @xref{Function Safety}.
554 @item safe-local-eval-function
555 If the value is non-@code{nil}, the named function is safe to call in
556 file-local evaluation forms.  @xref{File Local Variables}.
558 @item safe-local-variable
559 The value specifies a function for determining safe file-local values
560 for the named variable.  @xref{File Local Variables}.
562 @item side-effect-free
563 A non-@code{nil} value indicates that the named function is free of
564 side-effects, for determining function safety (@pxref{Function
565 Safety}) as well as for byte compiler optimizations.  Do not set it.
567 @item variable-documentation
568 If non-@code{nil}, this specifies the named variable's documentation
569 string.  This is set automatically by @code{defvar} and related
570 functions.  @xref{Defining Faces}.
571 @end table