diff options
| author | Joakim Verona | 2012-01-23 15:10:06 +0100 |
|---|---|---|
| committer | Joakim Verona | 2012-01-23 15:10:06 +0100 |
| commit | 0322b140eead7c94de7f0f6d19a90bd15690b4eb (patch) | |
| tree | 950c011783cc896d0450084cb5155e54548bfe5b /doc/lispref/symbols.texi | |
| parent | d5114bfea3ea4c37c57e2af0f3b095be9fcd8bac (diff) | |
| parent | cb5850f27c1b4d26957d58e2da2314dd12498671 (diff) | |
| download | emacs-0322b140eead7c94de7f0f6d19a90bd15690b4eb.tar.gz emacs-0322b140eead7c94de7f0f6d19a90bd15690b4eb.zip | |
upstream
Diffstat (limited to 'doc/lispref/symbols.texi')
| -rw-r--r-- | doc/lispref/symbols.texi | 176 |
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 |
| 44 | The @dfn{print name cell} holds a string that names the symbol for | 44 | The symbol's name. |
| 45 | reading 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 |
| 49 | The @dfn{value cell} holds the current value of the symbol as a | 48 | The symbol's current value as a variable. |
| 50 | variable. When a symbol is used as a form, the value of the form is the | ||
| 51 | contents 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 |
| 56 | The @dfn{function cell} holds the function definition of the symbol. | 52 | The symbol's function definition. It can also hold a symbol, a |
| 57 | When a symbol is used as a function, its function definition is used in | 53 | keymap, or a keyboard macro. |
| 58 | its place. This cell is also used to make a symbol stand for a keymap | ||
| 59 | or a keyboard macro, for editor command execution. Because each symbol | ||
| 60 | has separate value and function cells, variables names and function names do | ||
| 61 | not 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 |
| 65 | The @dfn{property list cell} holds the property list of the symbol. See | 57 | The 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 |
| 70 | other three cells can be set individually to any specified Lisp object. | 61 | The print name cell always holds a string, and cannot be changed. |
| 71 | 62 | Each 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 | |
| 73 | Since 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. |
| 74 | not to have two symbols with the same name. The Lisp reader ensures | 65 | Since symbols are represented textually by their names, it is |
| 75 | this: every time it reads a symbol, it looks for an existing symbol with | 66 | important not to have two symbols with the same name. The Lisp reader |
| 76 | the specified name before it creates a new one. (In GNU Emacs Lisp, | 67 | ensures this: every time it reads a symbol, it looks for an existing |
| 77 | this lookup uses a hashing algorithm and an obarray; see @ref{Creating | 68 | symbol with the specified name before it creates a new one. To get a |
| 78 | Symbols}.) | 69 | symbol's name, use the function @code{symbol-name} (@pxref{Creating |
| 79 | 70 | Symbols}). | |
| 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 |
| 82 | a Lisp expression (@pxref{Evaluation}). Any Lisp object is a legitimate | 73 | you get if the symbol itself is evaluated as a Lisp expression. |
| 83 | value. Certain symbols have values that cannot be changed; these | 74 | @xref{Variables}, for details about how values are set and retrieved, |
| 84 | include @code{nil} and @code{t}, and any symbol whose name starts with | 75 | including complications such as @dfn{local bindings} and @dfn{scoping |
| 85 | @samp{:} (those are called @dfn{keywords}). @xref{Constant Variables}. | 76 | rules}. Most symbols can have any Lisp object as a value, but certain |
| 86 | 77 | special 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 |
| 88 | the function stored in the function cell of the symbol @code{foo}. We | 79 | @samp{:} (those are called @dfn{keywords}). @xref{Constant |
| 89 | make the distinction explicit only when necessary. In normal | 80 | Variables}. |
| 90 | usage, 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 |
| 92 | Lisp interpreter expects to see there (@pxref{Evaluation}). Keyboard | 83 | refer to ``the function @code{foo}'' when we really mean the function |
| 93 | macros (@pxref{Keyboard Macros}), keymaps (@pxref{Keymaps}) and | 84 | stored in the function cell of @code{foo}; we make the distinction |
| 94 | autoload objects (@pxref{Autoloading}) are also sometimes stored in | 85 | explicit only when necessary. Typically, the function cell is used to |
| 95 | the function cells of symbols. | 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}). | ||
| 96 | 92 | ||
| 97 | The property list cell normally should hold a correctly formatted | 93 | The property list cell normally should hold a correctly formatted |
| 98 | property list (@pxref{Property Lists}), as a number of functions expect | 94 | property list. To get a symbol's function cell, use the function |
| 99 | to 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 |
| 102 | that the cell does not reference any object. (This is not the same | 98 | that 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 | |||
| 104 | symbol @code{nil}.) Examining a function or value cell that is void | 100 | symbol @code{nil}.) Examining a function or value cell that is void |
| 105 | results in an error, such as @samp{Symbol's value as variable is void}. | 101 | results 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 | 104 | names and function names do not conflict. For example, the symbol |
| 109 | the 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 |
| 110 | the four cells of the symbol @code{buffer-file-name}: | 106 | visited in the current buffer) as well as a function definition (a |
| 107 | primitive function that returns the name of the file): | ||
| 111 | 108 | ||
| 112 | @example | 109 | @example |
| 113 | (symbol-name 'buffer-file-name) | 110 | buffer-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 | ||
| 124 | Because this symbol is the variable which holds the name of the file | ||
| 125 | being visited in the current buffer, the value cell contents we see are | ||
| 126 | the name of the source file of this chapter of the Emacs Lisp Manual. | ||
| 127 | The property list cell contains the list @code{(variable-documentation | ||
| 128 | 29529)} which tells the documentation functions where to find the | ||
| 129 | documentation string for the variable @code{buffer-file-name} in the | ||
| 130 | @file{DOC-@var{version}} file. (29529 is the offset from the beginning | ||
| 131 | of the @file{DOC-@var{version}} file to where that documentation string | ||
| 132 | begins---see @ref{Documentation Basics}.) The function cell contains | ||
| 133 | the function for returning the name of the file. | ||
| 134 | @code{buffer-file-name} names a primitive function, which has no read | ||
| 135 | syntax and prints in hash notation (@pxref{Primitive Function Type}). A | ||
| 136 | symbol 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 |
| 144 | intention to use a certain symbol in a particular way. In Emacs Lisp, | 121 | announces your intention to use a symbol in a particular way. It |
| 145 | you can define a symbol as a variable, or define it as a function (or | 122 | typically specifies a value or meaning for the symbol for one kind of |
| 146 | macro), or both independently. | 123 | use, plus documentation for its meaning when used in this way. Thus, |
| 147 | 124 | when you define a symbol as a variable, you can supply an initial | |
| 148 | A definition construct typically specifies a value or meaning for the | 125 | value for the variable, plus documentation for the variable. |
| 149 | symbol for one kind of use, plus documentation for its meaning when used | ||
| 150 | in this way. Thus, when you define a symbol as a variable, you can | ||
| 151 | supply an initial value for the variable, plus documentation for the | ||
| 152 | variable. | ||
| 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 |
| 155 | symbol as a global variable. They are documented in detail in | 128 | symbol as a @dfn{global variable}---a variable that can be accessed at |
| 156 | @ref{Defining Variables}. For defining user option variables that can | 129 | any point in a Lisp program. @xref{Variables}, for details about |
| 157 | be customized, use @code{defcustom} (@pxref{Customization}). | 130 | variables. 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. | ||
| 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}). | ||
| 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 |
| 160 | expression and storing it in the function cell of the symbol. This | 142 | expression 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 | |||
| 171 | that cell can hold only one Lisp object at any given time. | 153 | that 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 |
| 175 | as a variable or function. Thus, you can make a symbol a global | 157 | both as a variable (e.g.@: with @code{defvar}) and as a function or |
| 176 | variable with @code{setq}, whether you define it first or not. The real | 158 | macro (e.g.@: with @code{defun}). Such definitions do not conflict. |
| 177 | purpose of definitions is to guide programmers and programming tools. | 159 | |
| 178 | They 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, | 161 | example, the @kbd{C-h f} and @kbd{C-h v} commands create help buffers |
| 180 | utilities such as @file{etags} and @file{make-docfile} recognize | 162 | containing links to the relevant variable, function, or macro |
| 181 | definitions, and add appropriate information to tag tables and the | 163 | definitions. @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 |
| 258 | several obarrays. | 239 | for 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 |
| 450 | information to various Lisp function names or variables. If your | 431 | information to various Lisp function names or variables. If your |
| 451 | program keeps all of its associations in one association list, it will | 432 | program keeps all such information in one association list, it will |
| 452 | typically need to search that entire list each time it checks for an | 433 | typically need to search that entire list each time it checks for an |
| 453 | association. This could be slow. By contrast, if you keep the same | 434 | association for a particular Lisp function name or variable, which |
| 454 | information in the property lists of the function names or variables | 435 | could be slow. By contrast, if you keep the same information in the |
| 455 | themselves, each search will scan only the length of one property list, | 436 | property lists of the function names or variables themselves, each |
| 456 | which is usually short. This is why the documentation for a variable is | 437 | search will scan only the length of one property list, which is |
| 438 | usually short. This is why the documentation for a variable is | ||
| 457 | recorded in a property named @code{variable-documentation}. The byte | 439 | recorded in a property named @code{variable-documentation}. The byte |
| 458 | compiler likewise uses properties to record those functions needing | 440 | compiler likewise uses properties to record those functions needing |
| 459 | special treatment. | 441 | special treatment. |