aboutsummaryrefslogtreecommitdiffstats
path: root/doc/lispref/symbols.texi
diff options
context:
space:
mode:
authorJoakim Verona2012-01-23 15:10:06 +0100
committerJoakim Verona2012-01-23 15:10:06 +0100
commit0322b140eead7c94de7f0f6d19a90bd15690b4eb (patch)
tree950c011783cc896d0450084cb5155e54548bfe5b /doc/lispref/symbols.texi
parentd5114bfea3ea4c37c57e2af0f3b095be9fcd8bac (diff)
parentcb5850f27c1b4d26957d58e2da2314dd12498671 (diff)
downloademacs-0322b140eead7c94de7f0f6d19a90bd15690b4eb.tar.gz
emacs-0322b140eead7c94de7f0f6d19a90bd15690b4eb.zip
upstream
Diffstat (limited to 'doc/lispref/symbols.texi')
-rw-r--r--doc/lispref/symbols.texi176
1 files changed, 79 insertions, 97 deletions
diff --git a/doc/lispref/symbols.texi b/doc/lispref/symbols.texi
index 866a63c4cd9..0ee22b905b6 100644
--- a/doc/lispref/symbols.texi
+++ b/doc/lispref/symbols.texi
@@ -41,62 +41,58 @@ references another object:
41@table @asis 41@table @asis
42@item Print name 42@item Print name
43@cindex print name cell 43@cindex print name cell
44The @dfn{print name cell} holds a string that names the symbol for 44The symbol's name.
45reading and printing. See @code{symbol-name} in @ref{Creating Symbols}.
46 45
47@item Value 46@item Value
48@cindex value cell 47@cindex value cell
49The @dfn{value cell} holds the current value of the symbol as a 48The symbol's current value as a variable.
50variable. When a symbol is used as a form, the value of the form is the
51contents of the symbol's value cell. See @code{symbol-value} in
52@ref{Accessing Variables}.
53 49
54@item Function 50@item Function
55@cindex function cell 51@cindex function cell
56The @dfn{function cell} holds the function definition of the symbol. 52The symbol's function definition. It can also hold a symbol, a
57When a symbol is used as a function, its function definition is used in 53keymap, or a keyboard macro.
58its place. This cell is also used to make a symbol stand for a keymap
59or a keyboard macro, for editor command execution. Because each symbol
60has separate value and function cells, variables names and function names do
61not conflict. See @code{symbol-function} in @ref{Function Cells}.
62 54
63@item Property list 55@item Property list
64@cindex property list cell 56@cindex property list cell
65The @dfn{property list cell} holds the property list of the symbol. See 57The symbol's property list.
66@code{symbol-plist} in @ref{Property Lists}.
67@end table 58@end table
68 59
69 The print name cell always holds a string, and cannot be changed. The 60@noindent
70other three cells can be set individually to any specified Lisp object. 61The print name cell always holds a string, and cannot be changed.
71 62Each of the other three cells can be set to any Lisp object.
72 The print name cell holds the string that is the name of the symbol. 63
73Since symbols are represented textually by their names, it is important 64 The print name cell holds the string that is the name of a symbol.
74not to have two symbols with the same name. The Lisp reader ensures 65Since symbols are represented textually by their names, it is
75this: every time it reads a symbol, it looks for an existing symbol with 66important not to have two symbols with the same name. The Lisp reader
76the specified name before it creates a new one. (In GNU Emacs Lisp, 67ensures this: every time it reads a symbol, it looks for an existing
77this lookup uses a hashing algorithm and an obarray; see @ref{Creating 68symbol with the specified name before it creates a new one. To get a
78Symbols}.) 69symbol's name, use the function @code{symbol-name} (@pxref{Creating
79 70Symbols}).
80 The value cell holds the symbol's value as a variable 71
81(@pxref{Variables}). That is what you get if you evaluate the symbol as 72 The value cell holds a symbol's value as a variable, which is what
82a Lisp expression (@pxref{Evaluation}). Any Lisp object is a legitimate 73you get if the symbol itself is evaluated as a Lisp expression.
83value. Certain symbols have values that cannot be changed; these 74@xref{Variables}, for details about how values are set and retrieved,
84include @code{nil} and @code{t}, and any symbol whose name starts with 75including complications such as @dfn{local bindings} and @dfn{scoping
85@samp{:} (those are called @dfn{keywords}). @xref{Constant Variables}. 76rules}. Most symbols can have any Lisp object as a value, but certain
86 77special symbols have values that cannot be changed; these include
87 We often refer to ``the function @code{foo}'' when we really mean 78@code{nil} and @code{t}, and any symbol whose name starts with
88the function stored in the function cell of the symbol @code{foo}. We 79@samp{:} (those are called @dfn{keywords}). @xref{Constant
89make the distinction explicit only when necessary. In normal 80Variables}.
90usage, the function cell usually contains a function 81
91(@pxref{Functions}) or a macro (@pxref{Macros}), as that is what the 82 The function cell holds a symbol's function definition. Often, we
92Lisp interpreter expects to see there (@pxref{Evaluation}). Keyboard 83refer to ``the function @code{foo}'' when we really mean the function
93macros (@pxref{Keyboard Macros}), keymaps (@pxref{Keymaps}) and 84stored in the function cell of @code{foo}; we make the distinction
94autoload objects (@pxref{Autoloading}) are also sometimes stored in 85explicit only when necessary. Typically, the function cell is used to
95the function cells of symbols. 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}).
96 92
97 The property list cell normally should hold a correctly formatted 93 The property list cell normally should hold a correctly formatted
98property list (@pxref{Property Lists}), as a number of functions expect 94property list. To get a symbol's function cell, use the function
99to see a property list there. 95@code{symbol-plist}. @xref{Property Lists}.
100 96
101 The function cell or the value cell may be @dfn{void}, which means 97 The function cell or the value cell may be @dfn{void}, which means
102that the cell does not reference any object. (This is not the same 98that the cell does not reference any object. (This is not the same
@@ -104,57 +100,43 @@ thing as holding the symbol @code{void}, nor the same as holding the
104symbol @code{nil}.) Examining a function or value cell that is void 100symbol @code{nil}.) Examining a function or value cell that is void
105results in an error, such as @samp{Symbol's value as variable is void}. 101results in an error, such as @samp{Symbol's value as variable is void}.
106 102
107 The four functions @code{symbol-name}, @code{symbol-value}, 103 Because each symbol has separate value and function cells, variables
108@code{symbol-plist}, and @code{symbol-function} return the contents of 104names and function names do not conflict. For example, the symbol
109the four cells of a symbol. Here as an example we show the contents of 105@code{buffer-file-name} has a value (the name of the file being
110the four cells of the symbol @code{buffer-file-name}: 106visited in the current buffer) as well as a function definition (a
107primitive function that returns the name of the file):
111 108
112@example 109@example
113(symbol-name 'buffer-file-name) 110buffer-file-name
114 @result{} "buffer-file-name"
115(symbol-value 'buffer-file-name)
116 @result{} "/gnu/elisp/symbols.texi" 111 @result{} "/gnu/elisp/symbols.texi"
117(symbol-function 'buffer-file-name) 112(symbol-function 'buffer-file-name)
118 @result{} #<subr buffer-file-name> 113 @result{} #<subr buffer-file-name>
119(symbol-plist 'buffer-file-name)
120 @result{} (variable-documentation 29529)
121@end example 114@end example
122 115
123@noindent
124Because this symbol is the variable which holds the name of the file
125being visited in the current buffer, the value cell contents we see are
126the name of the source file of this chapter of the Emacs Lisp Manual.
127The property list cell contains the list @code{(variable-documentation
12829529)} which tells the documentation functions where to find the
129documentation string for the variable @code{buffer-file-name} in the
130@file{DOC-@var{version}} file. (29529 is the offset from the beginning
131of the @file{DOC-@var{version}} file to where that documentation string
132begins---see @ref{Documentation Basics}.) The function cell contains
133the function for returning the name of the file.
134@code{buffer-file-name} names a primitive function, which has no read
135syntax and prints in hash notation (@pxref{Primitive Function Type}). A
136symbol naming a function written in Lisp would have a lambda expression
137(or a byte-code object) in this cell.
138
139@node Definitions, Creating Symbols, Symbol Components, Symbols 116@node Definitions, Creating Symbols, Symbol Components, Symbols
140@section Defining Symbols 117@section Defining Symbols
141@cindex definitions of symbols 118@cindex definitions of symbols
142 119
143 A @dfn{definition} in Lisp is a special form that announces your 120 A @dfn{definition} is a special kind of Lisp expression that
144intention to use a certain symbol in a particular way. In Emacs Lisp, 121announces your intention to use a symbol in a particular way. It
145you can define a symbol as a variable, or define it as a function (or 122typically specifies a value or meaning for the symbol for one kind of
146macro), or both independently. 123use, plus documentation for its meaning when used in this way. Thus,
147 124when you define a symbol as a variable, you can supply an initial
148 A definition construct typically specifies a value or meaning for the 125value for the variable, plus documentation for the variable.
149symbol for one kind of use, plus documentation for its meaning when used
150in this way. Thus, when you define a symbol as a variable, you can
151supply an initial value for the variable, plus documentation for the
152variable.
153 126
154 @code{defvar} and @code{defconst} are special forms that define a 127 @code{defvar} and @code{defconst} are special forms that define a
155symbol as a global variable. They are documented in detail in 128symbol as a @dfn{global variable}---a variable that can be accessed at
156@ref{Defining Variables}. For defining user option variables that can 129any point in a Lisp program. @xref{Variables}, for details about
157be customized, use @code{defcustom} (@pxref{Customization}). 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}).
158 140
159 @code{defun} defines a symbol as a function, creating a lambda 141 @code{defun} defines a symbol as a function, creating a lambda
160expression and storing it in the function cell of the symbol. This 142expression and storing it in the function cell of the symbol. This
@@ -171,15 +153,14 @@ both macro and function definitions are kept in the function cell, and
171that cell can hold only one Lisp object at any given time. 153that cell can hold only one Lisp object at any given time.
172@xref{Macros}. 154@xref{Macros}.
173 155
174 In Emacs Lisp, a definition is not required in order to use a symbol 156 As previously noted, Emacs Lisp allows the same symbol to be defined
175as a variable or function. Thus, you can make a symbol a global 157both as a variable (e.g.@: with @code{defvar}) and as a function or
176variable with @code{setq}, whether you define it first or not. The real 158macro (e.g.@: with @code{defun}). Such definitions do not conflict.
177purpose of definitions is to guide programmers and programming tools. 159
178They inform programmers who read the code that certain symbols are 160 These definition also act as guides for programming tools. For
179@emph{intended} to be used as variables, or as functions. In addition, 161example, the @kbd{C-h f} and @kbd{C-h v} commands create help buffers
180utilities such as @file{etags} and @file{make-docfile} recognize 162containing links to the relevant variable, function, or macro
181definitions, and add appropriate information to tag tables and the 163definitions. @xref{Name Help,,, emacs, The GNU Emacs Manual}.
182@file{DOC-@var{version}} file. @xref{Accessing Documentation}.
183 164
184@node Creating Symbols, Property Lists, Definitions, Symbols 165@node Creating Symbols, Property Lists, Definitions, Symbols
185@section Creating and Interning Symbols 166@section Creating and Interning Symbols
@@ -254,8 +235,8 @@ not work---only @code{intern} can enter a symbol in an obarray properly.
254 235
255@cindex CL note---symbol in obarrays 236@cindex CL note---symbol in obarrays
256@quotation 237@quotation
257@b{Common Lisp note:} In Common Lisp, a single symbol may be interned in 238@b{Common Lisp note:} Unlike Common Lisp, Emacs Lisp does not provide
258several obarrays. 239for interning a single symbol in several obarrays.
259@end quotation 240@end quotation
260 241
261 Most of the functions below take a name and sometimes an obarray as 242 Most of the functions below take a name and sometimes an obarray as
@@ -448,12 +429,13 @@ must be distinct.
448 429
449 Property lists are better than association lists for attaching 430 Property lists are better than association lists for attaching
450information to various Lisp function names or variables. If your 431information to various Lisp function names or variables. If your
451program keeps all of its associations in one association list, it will 432program keeps all such information in one association list, it will
452typically need to search that entire list each time it checks for an 433typically need to search that entire list each time it checks for an
453association. This could be slow. By contrast, if you keep the same 434association for a particular Lisp function name or variable, which
454information in the property lists of the function names or variables 435could be slow. By contrast, if you keep the same information in the
455themselves, each search will scan only the length of one property list, 436property lists of the function names or variables themselves, each
456which is usually short. This is why the documentation for a variable is 437search will scan only the length of one property list, which is
438usually short. This is why the documentation for a variable is
457recorded in a property named @code{variable-documentation}. The byte 439recorded in a property named @code{variable-documentation}. The byte
458compiler likewise uses properties to record those functions needing 440compiler likewise uses properties to record those functions needing
459special treatment. 441special treatment.