symbols.texi 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. @c -*-texinfo-*-
  2. @c This is part of the GNU Emacs Lisp Reference Manual.
  3. @c Copyright (C) 1990-1995, 1998-1999, 2001-2016 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
  9. A @dfn{symbol} is an object with a unique name. This chapter
  10. describes symbols, their components, their property lists, and how they
  11. are created and interned. Separate chapters describe the use of symbols
  12. as 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. You can test whether an arbitrary Lisp object is a symbol with
  16. @code{symbolp}:
  17. @defun symbolp object
  18. This function returns @code{t} if @var{object} is a symbol, @code{nil}
  19. otherwise.
  20. @end defun
  21. @menu
  22. * Symbol Components:: Symbols have names, values, function definitions
  23. and property lists.
  24. * Definitions:: A definition says how a symbol will be used.
  25. * Creating Symbols:: How symbols are kept unique.
  26. * Symbol Properties:: Each symbol has a property list
  27. for recording miscellaneous information.
  28. @end menu
  29. @node Symbol Components
  30. @section Symbol Components
  31. @cindex symbol components
  32. Each symbol has four components (or ``cells''), each of which
  33. references another object:
  34. @table @asis
  35. @item Print name
  36. @cindex print name cell
  37. The symbol's name.
  38. @item Value
  39. @cindex value cell
  40. The symbol's current value as a variable.
  41. @item Function
  42. @cindex function cell
  43. The symbol's function definition. It can also hold a symbol, a
  44. keymap, or a keyboard macro.
  45. @item Property list
  46. @cindex property list cell
  47. The symbol's property list.
  48. @end table
  49. @noindent
  50. The print name cell always holds a string, and cannot be changed.
  51. Each of the other three cells can be set to any Lisp object.
  52. The print name cell holds the string that is the name of a symbol.
  53. Since symbols are represented textually by their names, it is
  54. important not to have two symbols with the same name. The Lisp reader
  55. ensures this: every time it reads a symbol, it looks for an existing
  56. symbol with the specified name before it creates a new one. To get a
  57. symbol's name, use the function @code{symbol-name} (@pxref{Creating
  58. Symbols}).
  59. The value cell holds a symbol's value as a variable, which is what
  60. you get if the symbol itself is evaluated as a Lisp expression.
  61. @xref{Variables}, for details about how values are set and retrieved,
  62. including complications such as @dfn{local bindings} and @dfn{scoping
  63. rules}. Most symbols can have any Lisp object as a value, but certain
  64. special symbols have values that cannot be changed; these include
  65. @code{nil} and @code{t}, and any symbol whose name starts with
  66. @samp{:} (those are called @dfn{keywords}). @xref{Constant
  67. Variables}.
  68. The function cell holds a symbol's function definition. Often, we
  69. refer to ``the function @code{foo}'' when we really mean the function
  70. stored in the function cell of @code{foo}; we make the distinction
  71. explicit only when necessary. Typically, the function cell is used to
  72. hold a function (@pxref{Functions}) or a macro (@pxref{Macros}).
  73. However, it can also be used to hold a symbol (@pxref{Function
  74. Indirection}), keyboard macro (@pxref{Keyboard Macros}), keymap
  75. (@pxref{Keymaps}), or autoload object (@pxref{Autoloading}). To get
  76. the contents of a symbol's function cell, use the function
  77. @code{symbol-function} (@pxref{Function Cells}).
  78. The property list cell normally should hold a correctly formatted
  79. property list. To get a symbol's property list, use the function
  80. @code{symbol-plist}. @xref{Symbol Properties}.
  81. The function cell or the value cell may be @dfn{void}, which means
  82. that the cell does not reference any object. (This is not the same
  83. thing as holding the symbol @code{void}, nor the same as holding the
  84. symbol @code{nil}.) Examining a function or value cell that is void
  85. results in an error, such as @samp{Symbol's value as variable is void}.
  86. Because each symbol has separate value and function cells, variables
  87. names and function names do not conflict. For example, the symbol
  88. @code{buffer-file-name} has a value (the name of the file being
  89. visited in the current buffer) as well as a function definition (a
  90. primitive function that returns the name of the file):
  91. @example
  92. buffer-file-name
  93. @result{} "/gnu/elisp/symbols.texi"
  94. (symbol-function 'buffer-file-name)
  95. @result{} #<subr buffer-file-name>
  96. @end example
  97. @node Definitions
  98. @section Defining Symbols
  99. @cindex definitions of symbols
  100. A @dfn{definition} is a special kind of Lisp expression that
  101. announces your intention to use a symbol in a particular way. It
  102. typically specifies a value or meaning for the symbol for one kind of
  103. use, plus documentation for its meaning when used in this way. Thus,
  104. when you define a symbol as a variable, you can supply an initial
  105. value for the variable, plus documentation for the variable.
  106. @code{defvar} and @code{defconst} are special forms that define a
  107. symbol as a @dfn{global variable}---a variable that can be accessed at
  108. any point in a Lisp program. @xref{Variables}, for details about
  109. variables. To define a customizable variable, use the
  110. @code{defcustom} macro, which also calls @code{defvar} as a subroutine
  111. (@pxref{Customization}).
  112. In principle, you can assign a variable value to any symbol with
  113. @code{setq}, whether not it has first been defined as a variable.
  114. However, you ought to write a variable definition for each global
  115. variable that you want to use; otherwise, your Lisp program may not
  116. act correctly if it is evaluated with lexical scoping enabled
  117. (@pxref{Variable Scoping}).
  118. @code{defun} defines a symbol as a function, creating a lambda
  119. expression and storing it in the function cell of the symbol. This
  120. lambda expression thus becomes the function definition of the symbol.
  121. (The term ``function definition'', meaning the contents of the function
  122. cell, is derived from the idea that @code{defun} gives the symbol its
  123. definition as a function.) @code{defsubst} and @code{defalias} are two
  124. other ways of defining a function. @xref{Functions}.
  125. @code{defmacro} defines a symbol as a macro. It creates a macro
  126. object and stores it in the function cell of the symbol. Note that a
  127. given symbol can be a macro or a function, but not both at once, because
  128. both macro and function definitions are kept in the function cell, and
  129. that cell can hold only one Lisp object at any given time.
  130. @xref{Macros}.
  131. As previously noted, Emacs Lisp allows the same symbol to be defined
  132. both as a variable (e.g., with @code{defvar}) and as a function or
  133. macro (e.g., with @code{defun}). Such definitions do not conflict.
  134. These definition also act as guides for programming tools. For
  135. example, the @kbd{C-h f} and @kbd{C-h v} commands create help buffers
  136. containing links to the relevant variable, function, or macro
  137. definitions. @xref{Name Help,,, emacs, The GNU Emacs Manual}.
  138. @node Creating Symbols
  139. @section Creating and Interning Symbols
  140. @cindex reading symbols
  141. To understand how symbols are created in GNU Emacs Lisp, you must know
  142. how Lisp reads them. Lisp must ensure that it finds the same symbol
  143. every time it reads the same set of characters. Failure to do so would
  144. cause complete confusion.
  145. @cindex symbol name hashing
  146. @cindex hashing
  147. @cindex obarray
  148. @cindex bucket (in obarray)
  149. When the Lisp reader encounters a symbol, it reads all the characters
  150. of the name. Then it hashes those characters to find an index in a
  151. table called an @dfn{obarray}. Hashing is an efficient method of
  152. looking something up. For example, instead of searching a telephone
  153. book cover to cover when looking up Jan Jones, you start with the J's
  154. and go from there. That is a simple version of hashing. Each element
  155. of the obarray is a @dfn{bucket} which holds all the symbols with a
  156. given hash code; to look for a given name, it is sufficient to look
  157. through all the symbols in the bucket for that name's hash code. (The
  158. same idea is used for general Emacs hash tables, but they are a
  159. different data type; see @ref{Hash Tables}.)
  160. @cindex interning
  161. If a symbol with the desired name is found, the reader uses that
  162. symbol. If the obarray does not contain a symbol with that name, the
  163. reader makes a new symbol and adds it to the obarray. Finding or adding
  164. a symbol with a certain name is called @dfn{interning} it, and the
  165. symbol is then called an @dfn{interned symbol}.
  166. Interning ensures that each obarray has just one symbol with any
  167. particular name. Other like-named symbols may exist, but not in the
  168. same obarray. Thus, the reader gets the same symbols for the same
  169. names, as long as you keep reading with the same obarray.
  170. Interning usually happens automatically in the reader, but sometimes
  171. other programs need to do it. For example, after the @kbd{M-x} command
  172. obtains the command name as a string using the minibuffer, it then
  173. interns the string, to get the interned symbol with that name.
  174. @cindex symbol equality
  175. @cindex uninterned symbol
  176. No obarray contains all symbols; in fact, some symbols are not in any
  177. obarray. They are called @dfn{uninterned symbols}. An uninterned
  178. symbol has the same four cells as other symbols; however, the only way
  179. to gain access to it is by finding it in some other object or as the
  180. value of a variable.
  181. Creating an uninterned symbol is useful in generating Lisp code,
  182. because an uninterned symbol used as a variable in the code you generate
  183. cannot clash with any variables used in other Lisp programs.
  184. In Emacs Lisp, an obarray is actually a vector. Each element of the
  185. vector is a bucket; its value is either an interned symbol whose name
  186. hashes to that bucket, or 0 if the bucket is empty. Each interned
  187. symbol has an internal link (invisible to the user) to the next symbol
  188. in the bucket. Because these links are invisible, there is no way to
  189. find all the symbols in an obarray except using @code{mapatoms} (below).
  190. The order of symbols in a bucket is not significant.
  191. In an empty obarray, every element is 0, so you can create an obarray
  192. with @code{(make-vector @var{length} 0)}. @strong{This is the only
  193. valid way to create an obarray.} Prime numbers as lengths tend
  194. to result in good hashing; lengths one less than a power of two are also
  195. good.
  196. @strong{Do not try to put symbols in an obarray yourself.} This does
  197. not work---only @code{intern} can enter a symbol in an obarray properly.
  198. @cindex CL note---symbol in obarrays
  199. @quotation
  200. @b{Common Lisp note:} Unlike Common Lisp, Emacs Lisp does not provide
  201. for interning a single symbol in several obarrays.
  202. @end quotation
  203. Most of the functions below take a name and sometimes an obarray as
  204. arguments. A @code{wrong-type-argument} error is signaled if the name
  205. is not a string, or if the obarray is not a vector.
  206. @defun symbol-name symbol
  207. This function returns the string that is @var{symbol}'s name. For example:
  208. @example
  209. @group
  210. (symbol-name 'foo)
  211. @result{} "foo"
  212. @end group
  213. @end example
  214. @strong{Warning:} Changing the string by substituting characters does
  215. change the name of the symbol, but fails to update the obarray, so don't
  216. do it!
  217. @end defun
  218. @defun make-symbol name
  219. This function returns a newly-allocated, uninterned symbol whose name is
  220. @var{name} (which must be a string). Its value and function definition
  221. are void, and its property list is @code{nil}. In the example below,
  222. the value of @code{sym} is not @code{eq} to @code{foo} because it is a
  223. distinct uninterned symbol whose name is also @samp{foo}.
  224. @example
  225. (setq sym (make-symbol "foo"))
  226. @result{} foo
  227. (eq sym 'foo)
  228. @result{} nil
  229. @end example
  230. @end defun
  231. @defun intern name &optional obarray
  232. This function returns the interned symbol whose name is @var{name}. If
  233. there is no such symbol in the obarray @var{obarray}, @code{intern}
  234. creates a new one, adds it to the obarray, and returns it. If
  235. @var{obarray} is omitted, the value of the global variable
  236. @code{obarray} is used.
  237. @example
  238. (setq sym (intern "foo"))
  239. @result{} foo
  240. (eq sym 'foo)
  241. @result{} t
  242. (setq sym1 (intern "foo" other-obarray))
  243. @result{} foo
  244. (eq sym1 'foo)
  245. @result{} nil
  246. @end example
  247. @end defun
  248. @cindex CL note---interning existing symbol
  249. @quotation
  250. @b{Common Lisp note:} In Common Lisp, you can intern an existing symbol
  251. in an obarray. In Emacs Lisp, you cannot do this, because the argument
  252. to @code{intern} must be a string, not a symbol.
  253. @end quotation
  254. @defun intern-soft name &optional obarray
  255. This function returns the symbol in @var{obarray} whose name is
  256. @var{name}, or @code{nil} if @var{obarray} has no symbol with that name.
  257. Therefore, you can use @code{intern-soft} to test whether a symbol with
  258. a given name is already interned. If @var{obarray} is omitted, the
  259. value of the global variable @code{obarray} is used.
  260. The argument @var{name} may also be a symbol; in that case,
  261. the function returns @var{name} if @var{name} is interned
  262. in the specified obarray, and otherwise @code{nil}.
  263. @example
  264. (intern-soft "frazzle") ; @r{No such symbol exists.}
  265. @result{} nil
  266. (make-symbol "frazzle") ; @r{Create an uninterned one.}
  267. @result{} frazzle
  268. @group
  269. (intern-soft "frazzle") ; @r{That one cannot be found.}
  270. @result{} nil
  271. @end group
  272. @group
  273. (setq sym (intern "frazzle")) ; @r{Create an interned one.}
  274. @result{} frazzle
  275. @end group
  276. @group
  277. (intern-soft "frazzle") ; @r{That one can be found!}
  278. @result{} frazzle
  279. @end group
  280. @group
  281. (eq sym 'frazzle) ; @r{And it is the same one.}
  282. @result{} t
  283. @end group
  284. @end example
  285. @end defun
  286. @defvar obarray
  287. This variable is the standard obarray for use by @code{intern} and
  288. @code{read}.
  289. @end defvar
  290. @defun mapatoms function &optional obarray
  291. @anchor{Definition of mapatoms}
  292. This function calls @var{function} once with each symbol in the obarray
  293. @var{obarray}. Then it returns @code{nil}. If @var{obarray} is
  294. omitted, it defaults to the value of @code{obarray}, the standard
  295. obarray for ordinary symbols.
  296. @example
  297. (setq count 0)
  298. @result{} 0
  299. (defun count-syms (s)
  300. (setq count (1+ count)))
  301. @result{} count-syms
  302. (mapatoms 'count-syms)
  303. @result{} nil
  304. count
  305. @result{} 1871
  306. @end example
  307. See @code{documentation} in @ref{Accessing Documentation}, for another
  308. example using @code{mapatoms}.
  309. @end defun
  310. @defun unintern symbol obarray
  311. This function deletes @var{symbol} from the obarray @var{obarray}. If
  312. @code{symbol} is not actually in the obarray, @code{unintern} does
  313. nothing. If @var{obarray} is @code{nil}, the current obarray is used.
  314. If you provide a string instead of a symbol as @var{symbol}, it stands
  315. for a symbol name. Then @code{unintern} deletes the symbol (if any) in
  316. the obarray which has that name. If there is no such symbol,
  317. @code{unintern} does nothing.
  318. If @code{unintern} does delete a symbol, it returns @code{t}. Otherwise
  319. it returns @code{nil}.
  320. @end defun
  321. @node Symbol Properties
  322. @section Symbol Properties
  323. @cindex symbol property
  324. A symbol may possess any number of @dfn{symbol properties}, which
  325. can be used to record miscellaneous information about the symbol. For
  326. example, when a symbol has a @code{risky-local-variable} property with
  327. a non-@code{nil} value, that means the variable which the symbol names
  328. is a risky file-local variable (@pxref{File Local Variables}).
  329. Each symbol's properties and property values are stored in the
  330. symbol's property list cell (@pxref{Symbol Components}), in the form
  331. of a property list (@pxref{Property Lists}).
  332. @menu
  333. * Symbol Plists:: Accessing symbol properties.
  334. * Standard Properties:: Standard meanings of symbol properties.
  335. @end menu
  336. @node Symbol Plists
  337. @subsection Accessing Symbol Properties
  338. The following functions can be used to access symbol properties.
  339. @defun get symbol property
  340. This function returns the value of the property named @var{property}
  341. in @var{symbol}'s property list. If there is no such property, it
  342. returns @code{nil}. Thus, there is no distinction between a value of
  343. @code{nil} and the absence of the property.
  344. The name @var{property} is compared with the existing property names
  345. using @code{eq}, so any object is a legitimate property.
  346. See @code{put} for an example.
  347. @end defun
  348. @defun put symbol property value
  349. This function puts @var{value} onto @var{symbol}'s property list under
  350. the property name @var{property}, replacing any previous property value.
  351. The @code{put} function returns @var{value}.
  352. @example
  353. (put 'fly 'verb 'transitive)
  354. @result{}'transitive
  355. (put 'fly 'noun '(a buzzing little bug))
  356. @result{} (a buzzing little bug)
  357. (get 'fly 'verb)
  358. @result{} transitive
  359. (symbol-plist 'fly)
  360. @result{} (verb transitive noun (a buzzing little bug))
  361. @end example
  362. @end defun
  363. @defun symbol-plist symbol
  364. This function returns the property list of @var{symbol}.
  365. @end defun
  366. @defun setplist symbol plist
  367. This function sets @var{symbol}'s property list to @var{plist}.
  368. Normally, @var{plist} should be a well-formed property list, but this is
  369. not enforced. The return value is @var{plist}.
  370. @example
  371. (setplist 'foo '(a 1 b (2 3) c nil))
  372. @result{} (a 1 b (2 3) c nil)
  373. (symbol-plist 'foo)
  374. @result{} (a 1 b (2 3) c nil)
  375. @end example
  376. For symbols in special obarrays, which are not used for ordinary
  377. purposes, it may make sense to use the property list cell in a
  378. nonstandard fashion; in fact, the abbrev mechanism does so
  379. (@pxref{Abbrevs}).
  380. You could define @code{put} in terms of @code{setplist} and
  381. @code{plist-put}, as follows:
  382. @example
  383. (defun put (symbol prop value)
  384. (setplist symbol
  385. (plist-put (symbol-plist symbol) prop value)))
  386. @end example
  387. @end defun
  388. @defun function-get symbol property &optional autoload
  389. This function is identical to @code{get}, except that if @var{symbol}
  390. is the name of a function alias, it looks in the property list of the
  391. symbol naming the actual function. @xref{Defining Functions}. If the
  392. optional argument @var{autoload} is non-@code{nil}, and @var{symbol}
  393. is auto-loaded, this function will try to autoload it, since
  394. autoloading might set @var{property} of @var{symbol}. If
  395. @var{autoload} is the symbol @code{macro}, only try autoloading if
  396. @var{symbol} is an auto-loaded macro.
  397. @end defun
  398. @defun function-put function property value
  399. This function sets @var{property} of @var{function} to @var{value}.
  400. @var{function} should be a symbol. This function is preferred to
  401. calling @code{put} for setting properties of a function, because it
  402. will allow us some day to implement remapping of old properties to new
  403. ones.
  404. @end defun
  405. @node Standard Properties
  406. @subsection Standard Symbol Properties
  407. Here, we list the symbol properties which are used for special
  408. purposes in Emacs. In the following table, whenever we say ``the
  409. named function'', that means the function whose name is the relevant
  410. symbol; similarly for ``the named variable'' etc.
  411. @table @code
  412. @item :advertised-binding
  413. This property value specifies the preferred key binding, when showing
  414. documentation, for the named function. @xref{Keys in Documentation}.
  415. @item char-table-extra-slots
  416. The value, if non-@code{nil}, specifies the number of extra slots in
  417. the named char-table type. @xref{Char-Tables}.
  418. @item customized-face
  419. @itemx face-defface-spec
  420. @itemx saved-face
  421. @itemx theme-face
  422. These properties are used to record a face's standard, saved,
  423. customized, and themed face specs. Do not set them directly; they are
  424. managed by @code{defface} and related functions. @xref{Defining
  425. Faces}.
  426. @item customized-value
  427. @itemx saved-value
  428. @itemx standard-value
  429. @itemx theme-value
  430. These properties are used to record a customizable variable's standard
  431. value, saved value, customized-but-unsaved value, and themed values.
  432. Do not set them directly; they are managed by @code{defcustom} and
  433. related functions. @xref{Variable Definitions}.
  434. @item disabled
  435. If the value is non-@code{nil}, the named function is disabled as a
  436. command. @xref{Disabling Commands}.
  437. @item face-documentation
  438. The value stores the documentation string of the named face. This is
  439. set automatically by @code{defface}. @xref{Defining Faces}.
  440. @item history-length
  441. The value, if non-@code{nil}, specifies the maximum minibuffer history
  442. length for the named history list variable. @xref{Minibuffer
  443. History}.
  444. @item interactive-form
  445. The value is an interactive form for the named function. Normally,
  446. you should not set this directly; use the @code{interactive} special
  447. form instead. @xref{Interactive Call}.
  448. @item menu-enable
  449. The value is an expression for determining whether the named menu item
  450. should be enabled in menus. @xref{Simple Menu Items}.
  451. @item mode-class
  452. If the value is @code{special}, the named major mode is special.
  453. @xref{Major Mode Conventions}.
  454. @item permanent-local
  455. If the value is non-@code{nil}, the named variable is a buffer-local
  456. variable whose value should not be reset when changing major modes.
  457. @xref{Creating Buffer-Local}.
  458. @item permanent-local-hook
  459. If the value is non-@code{nil}, the named function should not be
  460. deleted from the local value of a hook variable when changing major
  461. modes. @xref{Setting Hooks}.
  462. @item pure
  463. If the value is non-@code{nil}, the named function is considered to be
  464. side-effect free. Calls with constant arguments can be evaluated at
  465. compile time. This may shift run time errors to compile time.
  466. @item risky-local-variable
  467. If the value is non-@code{nil}, the named variable is considered risky
  468. as a file-local variable. @xref{File Local Variables}.
  469. @item safe-function
  470. If the value is non-@code{nil}, the named function is considered
  471. generally safe for evaluation. @xref{Function Safety}.
  472. @item safe-local-eval-function
  473. If the value is non-@code{nil}, the named function is safe to call in
  474. file-local evaluation forms. @xref{File Local Variables}.
  475. @item safe-local-variable
  476. The value specifies a function for determining safe file-local values
  477. for the named variable. @xref{File Local Variables}.
  478. @item side-effect-free
  479. A non-@code{nil} value indicates that the named function is free of
  480. side-effects, for determining function safety (@pxref{Function
  481. Safety}) as well as for byte compiler optimizations. Do not set it.
  482. @item variable-documentation
  483. If non-@code{nil}, this specifies the named variable's documentation
  484. string. This is set automatically by @code{defvar} and related
  485. functions. @xref{Defining Faces}.
  486. @end table