diff options
| author | Chong Yidong | 2012-01-23 12:23:50 +0800 |
|---|---|---|
| committer | Chong Yidong | 2012-01-23 12:23:50 +0800 |
| commit | 31cbea1d3d3c548025f70551514bd1a370301ccf (patch) | |
| tree | 5643d70c29d6489feec93b45b135ee25bd7ae8a1 | |
| parent | 2724d9c71e29aa0aa298c3534b0b7b18d8fc6202 (diff) | |
| download | emacs-31cbea1d3d3c548025f70551514bd1a370301ccf.tar.gz emacs-31cbea1d3d3c548025f70551514bd1a370301ccf.zip | |
Update several Lisp manual chapters.
* doc/lispref/eval.texi (Intro Eval, Symbol Forms): Minor tweaks for
correctness with lexical scoping.
(Eval): Copyedits.
* doc/lispref/sequences.texi (Sequence Functions): Don't repeat the
introduction already given in the parent.
(Vectors): Copyedits.
(Rings): Move from lists.texi. Note that this is specific to the
ring package.
* doc/lispref/lists.texi (Cons Cells): Copyedits.
(List Elements): Mention push.
(List Variables): Mention pop.
(Rings): Move to sequences.texi.
* doc/lispref/strings.texi (Text Comparison): Minor qualification.
* doc/lispref/symbols.texi (Definitions, Symbol Components): Mention variable
scoping issues.
(Plists and Alists): Copyedits.
| -rw-r--r-- | admin/FOR-RELEASE | 19 | ||||
| -rw-r--r-- | doc/lispref/ChangeLog | 23 | ||||
| -rw-r--r-- | doc/lispref/control.texi | 10 | ||||
| -rw-r--r-- | doc/lispref/elisp.texi | 2 | ||||
| -rw-r--r-- | doc/lispref/eval.texi | 24 | ||||
| -rw-r--r-- | doc/lispref/lists.texi | 163 | ||||
| -rw-r--r-- | doc/lispref/sequences.texi | 120 | ||||
| -rw-r--r-- | doc/lispref/strings.texi | 13 | ||||
| -rw-r--r-- | doc/lispref/symbols.texi | 176 |
9 files changed, 281 insertions, 269 deletions
diff --git a/admin/FOR-RELEASE b/admin/FOR-RELEASE index 9335e5b1fbe..f704a3c9397 100644 --- a/admin/FOR-RELEASE +++ b/admin/FOR-RELEASE | |||
| @@ -187,25 +187,25 @@ backups.texi | |||
| 187 | buffers.texi | 187 | buffers.texi |
| 188 | commands.texi | 188 | commands.texi |
| 189 | compile.texi | 189 | compile.texi |
| 190 | control.texi | 190 | control.texi cyd |
| 191 | customize.texi | 191 | customize.texi |
| 192 | debugging.texi | 192 | debugging.texi |
| 193 | display.texi | 193 | display.texi |
| 194 | edebug.texi | 194 | edebug.texi |
| 195 | elisp.texi | 195 | elisp.texi |
| 196 | errors.texi | 196 | errors.texi |
| 197 | eval.texi | 197 | eval.texi cyd |
| 198 | files.texi | 198 | files.texi |
| 199 | frames.texi | 199 | frames.texi |
| 200 | functions.texi | 200 | functions.texi |
| 201 | hash.texi | 201 | hash.texi cyd |
| 202 | help.texi | 202 | help.texi |
| 203 | hooks.texi | 203 | hooks.texi |
| 204 | index.texi | 204 | index.texi |
| 205 | internals.texi | 205 | internals.texi |
| 206 | intro.texi cyd | 206 | intro.texi cyd |
| 207 | keymaps.texi | 207 | keymaps.texi |
| 208 | lists.texi | 208 | lists.texi cyd |
| 209 | loading.texi | 209 | loading.texi |
| 210 | locals.texi | 210 | locals.texi |
| 211 | macros.texi | 211 | macros.texi |
| @@ -214,17 +214,17 @@ markers.texi | |||
| 214 | minibuf.texi | 214 | minibuf.texi |
| 215 | modes.texi | 215 | modes.texi |
| 216 | nonascii.texi | 216 | nonascii.texi |
| 217 | numbers.texi | 217 | numbers.texi cyd |
| 218 | objects.texi cyd | 218 | objects.texi cyd |
| 219 | os.texi | 219 | os.texi |
| 220 | package.texi | 220 | package.texi |
| 221 | positions.texi | 221 | positions.texi |
| 222 | processes.texi | 222 | processes.texi |
| 223 | searching.texi | 223 | searching.texi |
| 224 | sequences.texi | 224 | sequences.texi cyd |
| 225 | streams.texi | 225 | streams.texi |
| 226 | strings.texi | 226 | strings.texi cyd |
| 227 | symbols.texi | 227 | symbols.texi cyd |
| 228 | syntax.texi | 228 | syntax.texi |
| 229 | text.texi | 229 | text.texi |
| 230 | tips.texi | 230 | tips.texi |
| @@ -232,8 +232,7 @@ variables.texi | |||
| 232 | windows.texi | 232 | windows.texi |
| 233 | 233 | ||
| 234 | * PLANNED ADDITIONS | 234 | * PLANNED ADDITIONS |
| 235 | 235 | * pov-mode (probably not for Emacs-23: waiting for a Free POV-Ray). | |
| 236 | ** pov-mode (probably not for Emacs-23: waiting for a Free POV-Ray). | ||
| 237 | ** gas-mode ? | 236 | ** gas-mode ? |
| 238 | 237 | ||
| 239 | 238 | ||
diff --git a/doc/lispref/ChangeLog b/doc/lispref/ChangeLog index 3c18de96d72..b66f82c5738 100644 --- a/doc/lispref/ChangeLog +++ b/doc/lispref/ChangeLog | |||
| @@ -1,3 +1,26 @@ | |||
| 1 | 2012-01-23 Chong Yidong <cyd@gnu.org> | ||
| 2 | |||
| 3 | * strings.texi (Text Comparison): Minor qualification. | ||
| 4 | |||
| 5 | * lists.texi (Cons Cells): Copyedits. | ||
| 6 | (List Elements): Mention push. | ||
| 7 | (List Variables): Mention pop. | ||
| 8 | (Rings): Move to sequences.texi. | ||
| 9 | |||
| 10 | * sequences.texi (Sequence Functions): Don't repeat the | ||
| 11 | introduction already given in the parent. | ||
| 12 | (Vectors): Copyedits. | ||
| 13 | (Rings): Move from lists.texi. Note that this is specific to the | ||
| 14 | ring package. | ||
| 15 | |||
| 16 | * symbols.texi (Definitions, Symbol Components): Mention variable | ||
| 17 | scoping issues. | ||
| 18 | (Plists and Alists): Copyedits. | ||
| 19 | |||
| 20 | * eval.texi (Intro Eval, Symbol Forms): Minor tweaks for | ||
| 21 | correctness with lexical scoping. | ||
| 22 | (Eval): Copyedits. | ||
| 23 | |||
| 1 | 2012-01-21 Chong Yidong <cyd@gnu.org> | 24 | 2012-01-21 Chong Yidong <cyd@gnu.org> |
| 2 | 25 | ||
| 3 | * intro.texi (A Sample Function Description): Special notation | 26 | * intro.texi (A Sample Function Description): Special notation |
diff --git a/doc/lispref/control.texi b/doc/lispref/control.texi index e74f3e198bf..0511f21007d 100644 --- a/doc/lispref/control.texi +++ b/doc/lispref/control.texi | |||
| @@ -8,11 +8,11 @@ | |||
| 8 | @cindex special forms for control structures | 8 | @cindex special forms for control structures |
| 9 | @cindex control structures | 9 | @cindex control structures |
| 10 | 10 | ||
| 11 | A Lisp program consists of expressions or @dfn{forms} (@pxref{Forms}). | 11 | A Lisp program consists of a set of @dfn{expressions}, or |
| 12 | We control the order of execution of these forms by enclosing them in | 12 | @dfn{forms} (@pxref{Forms}). We control the order of execution of |
| 13 | @dfn{control structures}. Control structures are special forms which | 13 | these forms by enclosing them in @dfn{control structures}. Control |
| 14 | control when, whether, or how many times to execute the forms they | 14 | structures are special forms which control when, whether, or how many |
| 15 | contain. | 15 | times to execute the forms they contain. |
| 16 | 16 | ||
| 17 | @cindex textual order | 17 | @cindex textual order |
| 18 | The simplest order of execution is sequential execution: first form | 18 | The simplest order of execution is sequential execution: first form |
diff --git a/doc/lispref/elisp.texi b/doc/lispref/elisp.texi index 0b8d972c1d5..1555b98e7fb 100644 --- a/doc/lispref/elisp.texi +++ b/doc/lispref/elisp.texi | |||
| @@ -326,7 +326,6 @@ Lists | |||
| 326 | * Modifying Lists:: Storing new pieces into an existing list. | 326 | * Modifying Lists:: Storing new pieces into an existing list. |
| 327 | * Sets And Lists:: A list can represent a finite mathematical set. | 327 | * Sets And Lists:: A list can represent a finite mathematical set. |
| 328 | * Association Lists:: A list can represent a finite relation or mapping. | 328 | * Association Lists:: A list can represent a finite relation or mapping. |
| 329 | * Rings:: Managing a fixed-size ring of objects. | ||
| 330 | 329 | ||
| 331 | Modifying Existing List Structure | 330 | Modifying Existing List Structure |
| 332 | 331 | ||
| @@ -344,6 +343,7 @@ Sequences, Arrays, and Vectors | |||
| 344 | * Vector Functions:: Functions specifically for vectors. | 343 | * Vector Functions:: Functions specifically for vectors. |
| 345 | * Char-Tables:: How to work with char-tables. | 344 | * Char-Tables:: How to work with char-tables. |
| 346 | * Bool-Vectors:: How to work with bool-vectors. | 345 | * Bool-Vectors:: How to work with bool-vectors. |
| 346 | * Rings:: Managing a fixed-size ring of objects. | ||
| 347 | 347 | ||
| 348 | Hash Tables | 348 | Hash Tables |
| 349 | 349 | ||
diff --git a/doc/lispref/eval.texi b/doc/lispref/eval.texi index adb4841a82d..fc18e503543 100644 --- a/doc/lispref/eval.texi +++ b/doc/lispref/eval.texi | |||
| @@ -64,8 +64,8 @@ evaluate a @dfn{function call} form such as @code{(car x)}, Emacs | |||
| 64 | first evaluates the argument (the subform @code{x}). After evaluating | 64 | first evaluates the argument (the subform @code{x}). After evaluating |
| 65 | the argument, Emacs @dfn{executes} the function (@code{car}), and if | 65 | the argument, Emacs @dfn{executes} the function (@code{car}), and if |
| 66 | the function is written in Lisp, execution works by evaluating the | 66 | the function is written in Lisp, execution works by evaluating the |
| 67 | @dfn{body} of the function. (In this example, however, @code{car} is | 67 | @dfn{body} of the function (in this example, however, @code{car} is |
| 68 | not a Lisp function; it is a primitive function implemented in C.) | 68 | not a Lisp function; it is a primitive function implemented in C). |
| 69 | @xref{Functions}, for more information about functions and function | 69 | @xref{Functions}, for more information about functions and function |
| 70 | calls. | 70 | calls. |
| 71 | 71 | ||
| @@ -77,9 +77,8 @@ variables (@pxref{Variables}).@footnote{This definition of | |||
| 77 | that can affect the result of a program.} Whenever a form refers to a | 77 | that can affect the result of a program.} Whenever a form refers to a |
| 78 | variable without creating a new binding for it, the variable evaluates | 78 | variable without creating a new binding for it, the variable evaluates |
| 79 | to the value given by the current environment. Evaluating a form may | 79 | to the value given by the current environment. Evaluating a form may |
| 80 | create a new environment for recursive evaluation, by binding | 80 | also temporarily alter the environment by binding variables |
| 81 | variables (@pxref{Local Variables}). Such environments are temporary, | 81 | (@pxref{Local Variables}). |
| 82 | and vanish when the evaluation of the form is complete. | ||
| 83 | 82 | ||
| 84 | @cindex side effect | 83 | @cindex side effect |
| 85 | Evaluating a form may also make changes that persist; these changes | 84 | Evaluating a form may also make changes that persist; these changes |
| @@ -177,9 +176,9 @@ program. Here is an example: | |||
| 177 | @cindex symbol evaluation | 176 | @cindex symbol evaluation |
| 178 | 177 | ||
| 179 | When a symbol is evaluated, it is treated as a variable. The result | 178 | When a symbol is evaluated, it is treated as a variable. The result |
| 180 | is the variable's value, if it has one. If it has none (if its value | 179 | is the variable's value, if it has one. If the symbol has no value as |
| 181 | cell is void), an error is signaled. For more information on the use of | 180 | a variable, the Lisp interpreter signals an error. For more |
| 182 | variables, see @ref{Variables}. | 181 | information on the use of variables, see @ref{Variables}. |
| 183 | 182 | ||
| 184 | In the following example, we set the value of a symbol with | 183 | In the following example, we set the value of a symbol with |
| 185 | @code{setq}. Then we evaluate the symbol, and get back the value that | 184 | @code{setq}. Then we evaluate the symbol, and get back the value that |
| @@ -602,12 +601,13 @@ functions provides the ability to pass information to them as | |||
| 602 | arguments. | 601 | arguments. |
| 603 | 602 | ||
| 604 | @defun eval form &optional lexical | 603 | @defun eval form &optional lexical |
| 605 | This is the basic function evaluating an expression. It evaluates | 604 | This is the basic function for evaluating an expression. It evaluates |
| 606 | @var{form} in the current environment and returns the result. How the | 605 | @var{form} in the current environment and returns the result. How the |
| 607 | evaluation proceeds depends on the type of the object (@pxref{Forms}). | 606 | evaluation proceeds depends on the type of the object (@pxref{Forms}). |
| 608 | @var{lexical} if non-nil means to evaluate @var{form} using lexical scoping | 607 | |
| 609 | rules (@pxref{Lexical Binding}) instead of the default dynamic scoping used | 608 | The argument @var{lexical}, if non-@code{nil}, means to evaluate |
| 610 | historically in Emacs Lisp. | 609 | @var{form} using lexical scoping rules for variables, instead of the |
| 610 | default dynamic scoping rules. @xref{Lexical Binding}. | ||
| 611 | 611 | ||
| 612 | Since @code{eval} is a function, the argument expression that appears | 612 | Since @code{eval} is a function, the argument expression that appears |
| 613 | in a call to @code{eval} is evaluated twice: once as preparation before | 613 | in a call to @code{eval} is evaluated twice: once as preparation before |
diff --git a/doc/lispref/lists.texi b/doc/lispref/lists.texi index eb9ddf58603..c8433c79b54 100644 --- a/doc/lispref/lists.texi +++ b/doc/lispref/lists.texi | |||
| @@ -23,7 +23,6 @@ the whole list. | |||
| 23 | * Modifying Lists:: Storing new pieces into an existing list. | 23 | * Modifying Lists:: Storing new pieces into an existing list. |
| 24 | * Sets And Lists:: A list can represent a finite mathematical set. | 24 | * Sets And Lists:: A list can represent a finite mathematical set. |
| 25 | * Association Lists:: A list can represent a finite relation or mapping. | 25 | * Association Lists:: A list can represent a finite relation or mapping. |
| 26 | * Rings:: Managing a fixed-size ring of objects. | ||
| 27 | @end menu | 26 | @end menu |
| 28 | 27 | ||
| 29 | @node Cons Cells | 28 | @node Cons Cells |
| @@ -31,61 +30,56 @@ the whole list. | |||
| 31 | @cindex lists and cons cells | 30 | @cindex lists and cons cells |
| 32 | 31 | ||
| 33 | Lists in Lisp are not a primitive data type; they are built up from | 32 | Lists in Lisp are not a primitive data type; they are built up from |
| 34 | @dfn{cons cells}. A cons cell is a data object that represents an | 33 | @dfn{cons cells} (@pxref{Cons Cell Type}). A cons cell is a data |
| 35 | ordered pair. That is, it has two slots, and each slot @dfn{holds}, or | 34 | object that represents an ordered pair. That is, it has two slots, |
| 36 | @dfn{refers to}, some Lisp object. One slot is known as the @sc{car}, | 35 | and each slot @dfn{holds}, or @dfn{refers to}, some Lisp object. One |
| 37 | and the other is known as the @sc{cdr}. (These names are traditional; | 36 | slot is known as the @sc{car}, and the other is known as the @sc{cdr}. |
| 38 | see @ref{Cons Cell Type}.) @sc{cdr} is pronounced ``could-er.'' | 37 | (These names are traditional; see @ref{Cons Cell Type}.) @sc{cdr} is |
| 38 | pronounced ``could-er.'' | ||
| 39 | 39 | ||
| 40 | We say that ``the @sc{car} of this cons cell is'' whatever object | 40 | We say that ``the @sc{car} of this cons cell is'' whatever object |
| 41 | its @sc{car} slot currently holds, and likewise for the @sc{cdr}. | 41 | its @sc{car} slot currently holds, and likewise for the @sc{cdr}. |
| 42 | 42 | ||
| 43 | A list is a series of cons cells ``chained together,'' so that each | 43 | A list is a series of cons cells ``chained together,'' so that each |
| 44 | cell refers to the next one. There is one cons cell for each element of | 44 | cell refers to the next one. There is one cons cell for each element |
| 45 | the list. By convention, the @sc{car}s of the cons cells hold the | 45 | of the list. By convention, the @sc{car}s of the cons cells hold the |
| 46 | elements of the list, and the @sc{cdr}s are used to chain the list: the | 46 | elements of the list, and the @sc{cdr}s are used to chain the list |
| 47 | @sc{cdr} slot of each cons cell refers to the following cons cell. The | 47 | (this asymmetry between @sc{car} and @sc{cdr} is entirely a matter of |
| 48 | @sc{cdr} of the last cons cell is @code{nil}. This asymmetry between | 48 | convention; at the level of cons cells, the @sc{car} and @sc{cdr} |
| 49 | the @sc{car} and the @sc{cdr} is entirely a matter of convention; at the | 49 | slots have similar properties). Hence, the @sc{cdr} slot of each cons |
| 50 | level of cons cells, the @sc{car} and @sc{cdr} slots have the same | 50 | cell in a list refers to the following cons cell. |
| 51 | characteristics. | ||
| 52 | 51 | ||
| 53 | @cindex true list | 52 | @cindex true list |
| 54 | Since @code{nil} is the conventional value to put in the @sc{cdr} of | 53 | Also by convention, the @sc{cdr} of the last cons cell in a list is |
| 55 | the last cons cell in the list, we call that case a @dfn{true list}. | 54 | @code{nil}. We call such a @code{nil}-terminated structure a |
| 56 | 55 | @dfn{true list}. In Emacs Lisp, the symbol @code{nil} is both a | |
| 57 | In Lisp, we consider the symbol @code{nil} a list as well as a | 56 | symbol and a list with no elements. For convenience, the symbol |
| 58 | symbol; it is the list with no elements. For convenience, the symbol | ||
| 59 | @code{nil} is considered to have @code{nil} as its @sc{cdr} (and also | 57 | @code{nil} is considered to have @code{nil} as its @sc{cdr} (and also |
| 60 | as its @sc{car}). Therefore, the @sc{cdr} of a true list is always a | 58 | as its @sc{car}). |
| 61 | true list. | 59 | |
| 60 | Hence, the @sc{cdr} of a true list is always a true list. The | ||
| 61 | @sc{cdr} of a nonempty true list is a true list containing all the | ||
| 62 | elements except the first. | ||
| 62 | 63 | ||
| 63 | @cindex dotted list | 64 | @cindex dotted list |
| 64 | @cindex circular list | 65 | @cindex circular list |
| 65 | If the @sc{cdr} of a list's last cons cell is some other value, | 66 | If the @sc{cdr} of a list's last cons cell is some value other than |
| 66 | neither @code{nil} nor another cons cell, we call the structure a | 67 | @code{nil}, we call the structure a @dfn{dotted list}, since its |
| 67 | @dfn{dotted list}, since its printed representation would use | 68 | printed representation would use dotted pair notation (@pxref{Dotted |
| 68 | @samp{.}. There is one other possibility: some cons cell's @sc{cdr} | 69 | Pair Notation}). There is one other possibility: some cons cell's |
| 69 | could point to one of the previous cons cells in the list. We call | 70 | @sc{cdr} could point to one of the previous cons cells in the list. |
| 70 | that structure a @dfn{circular list}. | 71 | We call that structure a @dfn{circular list}. |
| 71 | 72 | ||
| 72 | For some purposes, it does not matter whether a list is true, | 73 | For some purposes, it does not matter whether a list is true, |
| 73 | circular or dotted. If the program doesn't look far enough down the | 74 | circular or dotted. If a program doesn't look far enough down the |
| 74 | list to see the @sc{cdr} of the final cons cell, it won't care. | 75 | list to see the @sc{cdr} of the final cons cell, it won't care. |
| 75 | However, some functions that operate on lists demand true lists and | 76 | However, some functions that operate on lists demand true lists and |
| 76 | signal errors if given a dotted list. Most functions that try to find | 77 | signal errors if given a dotted list. Most functions that try to find |
| 77 | the end of a list enter infinite loops if given a circular list. | 78 | the end of a list enter infinite loops if given a circular list. |
| 78 | 79 | ||
| 79 | @cindex list structure | 80 | @cindex list structure |
| 80 | Because most cons cells are used as part of lists, the phrase | 81 | Because most cons cells are used as part of lists, we refer to any |
| 81 | @dfn{list structure} has come to mean any structure made out of cons | 82 | structure made out of cons cells as a @dfn{list structure}. |
| 82 | cells. | ||
| 83 | |||
| 84 | The @sc{cdr} of any nonempty true list @var{l} is a list containing all the | ||
| 85 | elements of @var{l} except the first. | ||
| 86 | |||
| 87 | @xref{Cons Cell Type}, for the read and print syntax of cons cells and | ||
| 88 | lists, and for ``box and arrow'' illustrations of lists. | ||
| 89 | 83 | ||
| 90 | @node List-related Predicates | 84 | @node List-related Predicates |
| 91 | @section Predicates on Lists | 85 | @section Predicates on Lists |
| @@ -257,6 +251,10 @@ x | |||
| 257 | x | 251 | x |
| 258 | @result{} (b c) | 252 | @result{} (b c) |
| 259 | @end example | 253 | @end example |
| 254 | |||
| 255 | @noindent | ||
| 256 | For the @code{pop} macro, which removes an element from a list, | ||
| 257 | @xref{List Variables}. | ||
| 260 | @end defmac | 258 | @end defmac |
| 261 | 259 | ||
| 262 | @defun nth n list | 260 | @defun nth n list |
| @@ -695,6 +693,10 @@ This macro provides an alternative way to write | |||
| 695 | l | 693 | l |
| 696 | @result{} (c a b) | 694 | @result{} (c a b) |
| 697 | @end example | 695 | @end example |
| 696 | |||
| 697 | @noindent | ||
| 698 | For the @code{pop} macro, which removes the first element from a list, | ||
| 699 | @xref{List Elements}. | ||
| 698 | @end defmac | 700 | @end defmac |
| 699 | 701 | ||
| 700 | Two functions modify lists that are the values of variables. | 702 | Two functions modify lists that are the values of variables. |
| @@ -1800,90 +1802,3 @@ often modifies the original list structure of @var{alist}. | |||
| 1800 | compares the @sc{cdr} of each @var{alist} association instead of the | 1802 | compares the @sc{cdr} of each @var{alist} association instead of the |
| 1801 | @sc{car}. | 1803 | @sc{car}. |
| 1802 | @end defun | 1804 | @end defun |
| 1803 | |||
| 1804 | @node Rings | ||
| 1805 | @section Managing a Fixed-Size Ring of Objects | ||
| 1806 | |||
| 1807 | @cindex ring data structure | ||
| 1808 | This section describes functions for operating on rings. A | ||
| 1809 | @dfn{ring} is a fixed-size data structure that supports insertion, | ||
| 1810 | deletion, rotation, and modulo-indexed reference and traversal. | ||
| 1811 | |||
| 1812 | @defun make-ring size | ||
| 1813 | This returns a new ring capable of holding @var{size} objects. | ||
| 1814 | @var{size} should be an integer. | ||
| 1815 | @end defun | ||
| 1816 | |||
| 1817 | @defun ring-p object | ||
| 1818 | This returns @code{t} if @var{object} is a ring, @code{nil} otherwise. | ||
| 1819 | @end defun | ||
| 1820 | |||
| 1821 | @defun ring-size ring | ||
| 1822 | This returns the maximum capacity of the @var{ring}. | ||
| 1823 | @end defun | ||
| 1824 | |||
| 1825 | @defun ring-length ring | ||
| 1826 | This returns the number of objects that @var{ring} currently contains. | ||
| 1827 | The value will never exceed that returned by @code{ring-size}. | ||
| 1828 | @end defun | ||
| 1829 | |||
| 1830 | @defun ring-elements ring | ||
| 1831 | This returns a list of the objects in @var{ring}, in order, newest first. | ||
| 1832 | @end defun | ||
| 1833 | |||
| 1834 | @defun ring-copy ring | ||
| 1835 | This returns a new ring which is a copy of @var{ring}. | ||
| 1836 | The new ring contains the same (@code{eq}) objects as @var{ring}. | ||
| 1837 | @end defun | ||
| 1838 | |||
| 1839 | @defun ring-empty-p ring | ||
| 1840 | This returns @code{t} if @var{ring} is empty, @code{nil} otherwise. | ||
| 1841 | @end defun | ||
| 1842 | |||
| 1843 | The newest element in the ring always has index 0. Higher indices | ||
| 1844 | correspond to older elements. Indices are computed modulo the ring | ||
| 1845 | length. Index @minus{}1 corresponds to the oldest element, @minus{}2 | ||
| 1846 | to the next-oldest, and so forth. | ||
| 1847 | |||
| 1848 | @defun ring-ref ring index | ||
| 1849 | This returns the object in @var{ring} found at index @var{index}. | ||
| 1850 | @var{index} may be negative or greater than the ring length. If | ||
| 1851 | @var{ring} is empty, @code{ring-ref} signals an error. | ||
| 1852 | @end defun | ||
| 1853 | |||
| 1854 | @defun ring-insert ring object | ||
| 1855 | This inserts @var{object} into @var{ring}, making it the newest | ||
| 1856 | element, and returns @var{object}. | ||
| 1857 | |||
| 1858 | If the ring is full, insertion removes the oldest element to | ||
| 1859 | make room for the new element. | ||
| 1860 | @end defun | ||
| 1861 | |||
| 1862 | @defun ring-remove ring &optional index | ||
| 1863 | Remove an object from @var{ring}, and return that object. The | ||
| 1864 | argument @var{index} specifies which item to remove; if it is | ||
| 1865 | @code{nil}, that means to remove the oldest item. If @var{ring} is | ||
| 1866 | empty, @code{ring-remove} signals an error. | ||
| 1867 | @end defun | ||
| 1868 | |||
| 1869 | @defun ring-insert-at-beginning ring object | ||
| 1870 | This inserts @var{object} into @var{ring}, treating it as the oldest | ||
| 1871 | element. The return value is not significant. | ||
| 1872 | |||
| 1873 | If the ring is full, this function removes the newest element to make | ||
| 1874 | room for the inserted element. | ||
| 1875 | @end defun | ||
| 1876 | |||
| 1877 | @cindex fifo data structure | ||
| 1878 | If you are careful not to exceed the ring size, you can | ||
| 1879 | use the ring as a first-in-first-out queue. For example: | ||
| 1880 | |||
| 1881 | @lisp | ||
| 1882 | (let ((fifo (make-ring 5))) | ||
| 1883 | (mapc (lambda (obj) (ring-insert fifo obj)) | ||
| 1884 | '(0 one "two")) | ||
| 1885 | (list (ring-remove fifo) t | ||
| 1886 | (ring-remove fifo) t | ||
| 1887 | (ring-remove fifo))) | ||
| 1888 | @result{} (0 t one t "two") | ||
| 1889 | @end lisp | ||
diff --git a/doc/lispref/sequences.texi b/doc/lispref/sequences.texi index 0ea32f99e12..94f1bf666d2 100644 --- a/doc/lispref/sequences.texi +++ b/doc/lispref/sequences.texi | |||
| @@ -8,10 +8,10 @@ | |||
| 8 | @chapter Sequences, Arrays, and Vectors | 8 | @chapter Sequences, Arrays, and Vectors |
| 9 | @cindex sequence | 9 | @cindex sequence |
| 10 | 10 | ||
| 11 | Recall that the @dfn{sequence} type is the union of two other Lisp | 11 | The @dfn{sequence} type is the union of two other Lisp types: lists |
| 12 | types: lists and arrays. In other words, any list is a sequence, and | 12 | and arrays. In other words, any list is a sequence, and any array is |
| 13 | any array is a sequence. The common property that all sequences have is | 13 | a sequence. The common property that all sequences have is that each |
| 14 | that each is an ordered collection of elements. | 14 | is an ordered collection of elements. |
| 15 | 15 | ||
| 16 | An @dfn{array} is a fixed-length object with a slot for each of its | 16 | An @dfn{array} is a fixed-length object with a slot for each of its |
| 17 | elements. All the elements are accessible in constant time. The four | 17 | elements. All the elements are accessible in constant time. The four |
| @@ -54,19 +54,17 @@ But it is possible to add elements to the list, or remove elements. | |||
| 54 | * Vector Functions:: Functions specifically for vectors. | 54 | * Vector Functions:: Functions specifically for vectors. |
| 55 | * Char-Tables:: How to work with char-tables. | 55 | * Char-Tables:: How to work with char-tables. |
| 56 | * Bool-Vectors:: How to work with bool-vectors. | 56 | * Bool-Vectors:: How to work with bool-vectors. |
| 57 | * Rings:: Managing a fixed-size ring of objects. | ||
| 57 | @end menu | 58 | @end menu |
| 58 | 59 | ||
| 59 | @node Sequence Functions | 60 | @node Sequence Functions |
| 60 | @section Sequences | 61 | @section Sequences |
| 61 | 62 | ||
| 62 | In Emacs Lisp, a @dfn{sequence} is either a list or an array. The | 63 | This section describes functions that accept any kind of sequence. |
| 63 | common property of all sequences is that they are ordered collections of | ||
| 64 | elements. This section describes functions that accept any kind of | ||
| 65 | sequence. | ||
| 66 | 64 | ||
| 67 | @defun sequencep object | 65 | @defun sequencep object |
| 68 | Returns @code{t} if @var{object} is a list, vector, string, | 66 | This function returns @code{t} if @var{object} is a list, vector, |
| 69 | bool-vector, or char-table, @code{nil} otherwise. | 67 | string, bool-vector, or char-table, @code{nil} otherwise. |
| 70 | @end defun | 68 | @end defun |
| 71 | 69 | ||
| 72 | @defun length sequence | 70 | @defun length sequence |
| @@ -149,8 +147,9 @@ This function generalizes @code{aref} (@pxref{Array Functions}) and | |||
| 149 | 147 | ||
| 150 | @defun copy-sequence sequence | 148 | @defun copy-sequence sequence |
| 151 | @cindex copying sequences | 149 | @cindex copying sequences |
| 152 | Returns a copy of @var{sequence}. The copy is the same type of object | 150 | This function returns a copy of @var{sequence}. The copy is the same |
| 153 | as the original sequence, and it has the same elements in the same order. | 151 | type of object as the original sequence, and it has the same elements |
| 152 | in the same order. | ||
| 154 | 153 | ||
| 155 | Storing a new element into the copy does not affect the original | 154 | Storing a new element into the copy does not affect the original |
| 156 | @var{sequence}, and vice versa. However, the elements of the new | 155 | @var{sequence}, and vice versa. However, the elements of the new |
| @@ -394,8 +393,8 @@ symbol-lookup tables (@pxref{Creating Symbols}), as part of the | |||
| 394 | representation of a byte-compiled function (@pxref{Byte Compilation}), | 393 | representation of a byte-compiled function (@pxref{Byte Compilation}), |
| 395 | and more. | 394 | and more. |
| 396 | 395 | ||
| 397 | In Emacs Lisp, the indices of the elements of a vector start from zero | 396 | Like other arrays, vectors use zero-origin indexing: the first |
| 398 | and count up from there. | 397 | element has index 0. |
| 399 | 398 | ||
| 400 | Vectors are printed with square brackets surrounding the elements. | 399 | Vectors are printed with square brackets surrounding the elements. |
| 401 | Thus, a vector whose elements are the symbols @code{a}, @code{b} and | 400 | Thus, a vector whose elements are the symbols @code{a}, @code{b} and |
| @@ -728,3 +727,96 @@ bv | |||
| 728 | @noindent | 727 | @noindent |
| 729 | These results make sense because the binary codes for control-_ and | 728 | These results make sense because the binary codes for control-_ and |
| 730 | control-W are 11111 and 10111, respectively. | 729 | control-W are 11111 and 10111, respectively. |
| 730 | |||
| 731 | @node Rings | ||
| 732 | @section Managing a Fixed-Size Ring of Objects | ||
| 733 | |||
| 734 | @cindex ring data structure | ||
| 735 | A @dfn{ring} is a fixed-size data structure that supports insertion, | ||
| 736 | deletion, rotation, and modulo-indexed reference and traversal. An | ||
| 737 | efficient ring data structure is implemented by the @code{ring} | ||
| 738 | package. It provides the functions listed in this section. | ||
| 739 | |||
| 740 | Note that several ``rings'' in Emacs, like the kill ring and the | ||
| 741 | mark ring, are actually implemented as simple lists, @emph{not} using | ||
| 742 | the @code{ring} package; thus the following functions won't work on | ||
| 743 | them. | ||
| 744 | |||
| 745 | @defun make-ring size | ||
| 746 | This returns a new ring capable of holding @var{size} objects. | ||
| 747 | @var{size} should be an integer. | ||
| 748 | @end defun | ||
| 749 | |||
| 750 | @defun ring-p object | ||
| 751 | This returns @code{t} if @var{object} is a ring, @code{nil} otherwise. | ||
| 752 | @end defun | ||
| 753 | |||
| 754 | @defun ring-size ring | ||
| 755 | This returns the maximum capacity of the @var{ring}. | ||
| 756 | @end defun | ||
| 757 | |||
| 758 | @defun ring-length ring | ||
| 759 | This returns the number of objects that @var{ring} currently contains. | ||
| 760 | The value will never exceed that returned by @code{ring-size}. | ||
| 761 | @end defun | ||
| 762 | |||
| 763 | @defun ring-elements ring | ||
| 764 | This returns a list of the objects in @var{ring}, in order, newest first. | ||
| 765 | @end defun | ||
| 766 | |||
| 767 | @defun ring-copy ring | ||
| 768 | This returns a new ring which is a copy of @var{ring}. | ||
| 769 | The new ring contains the same (@code{eq}) objects as @var{ring}. | ||
| 770 | @end defun | ||
| 771 | |||
| 772 | @defun ring-empty-p ring | ||
| 773 | This returns @code{t} if @var{ring} is empty, @code{nil} otherwise. | ||
| 774 | @end defun | ||
| 775 | |||
| 776 | The newest element in the ring always has index 0. Higher indices | ||
| 777 | correspond to older elements. Indices are computed modulo the ring | ||
| 778 | length. Index @minus{}1 corresponds to the oldest element, @minus{}2 | ||
| 779 | to the next-oldest, and so forth. | ||
| 780 | |||
| 781 | @defun ring-ref ring index | ||
| 782 | This returns the object in @var{ring} found at index @var{index}. | ||
| 783 | @var{index} may be negative or greater than the ring length. If | ||
| 784 | @var{ring} is empty, @code{ring-ref} signals an error. | ||
| 785 | @end defun | ||
| 786 | |||
| 787 | @defun ring-insert ring object | ||
| 788 | This inserts @var{object} into @var{ring}, making it the newest | ||
| 789 | element, and returns @var{object}. | ||
| 790 | |||
| 791 | If the ring is full, insertion removes the oldest element to | ||
| 792 | make room for the new element. | ||
| 793 | @end defun | ||
| 794 | |||
| 795 | @defun ring-remove ring &optional index | ||
| 796 | Remove an object from @var{ring}, and return that object. The | ||
| 797 | argument @var{index} specifies which item to remove; if it is | ||
| 798 | @code{nil}, that means to remove the oldest item. If @var{ring} is | ||
| 799 | empty, @code{ring-remove} signals an error. | ||
| 800 | @end defun | ||
| 801 | |||
| 802 | @defun ring-insert-at-beginning ring object | ||
| 803 | This inserts @var{object} into @var{ring}, treating it as the oldest | ||
| 804 | element. The return value is not significant. | ||
| 805 | |||
| 806 | If the ring is full, this function removes the newest element to make | ||
| 807 | room for the inserted element. | ||
| 808 | @end defun | ||
| 809 | |||
| 810 | @cindex fifo data structure | ||
| 811 | If you are careful not to exceed the ring size, you can | ||
| 812 | use the ring as a first-in-first-out queue. For example: | ||
| 813 | |||
| 814 | @lisp | ||
| 815 | (let ((fifo (make-ring 5))) | ||
| 816 | (mapc (lambda (obj) (ring-insert fifo obj)) | ||
| 817 | '(0 one "two")) | ||
| 818 | (list (ring-remove fifo) t | ||
| 819 | (ring-remove fifo) t | ||
| 820 | (ring-remove fifo))) | ||
| 821 | @result{} (0 t one t "two") | ||
| 822 | @end lisp | ||
diff --git a/doc/lispref/strings.texi b/doc/lispref/strings.texi index 49199d3e32f..bbb75f1474d 100644 --- a/doc/lispref/strings.texi +++ b/doc/lispref/strings.texi | |||
| @@ -410,8 +410,13 @@ in case if @code{case-fold-search} is non-@code{nil}. | |||
| 410 | @defun string= string1 string2 | 410 | @defun string= string1 string2 |
| 411 | This function returns @code{t} if the characters of the two strings | 411 | This function returns @code{t} if the characters of the two strings |
| 412 | match exactly. Symbols are also allowed as arguments, in which case | 412 | match exactly. Symbols are also allowed as arguments, in which case |
| 413 | their print names are used. | 413 | the symbol names are used. Case is always significant, regardless of |
| 414 | Case is always significant, regardless of @code{case-fold-search}. | 414 | @code{case-fold-search}. |
| 415 | |||
| 416 | This function is equivalent to @code{equal} for comparing two strings | ||
| 417 | (@pxref{Equality Predicates}). In particular, the text properties of | ||
| 418 | the two strings are ignored. But if either argument is not a string | ||
| 419 | or symbol, an error is signaled. | ||
| 415 | 420 | ||
| 416 | @example | 421 | @example |
| 417 | (string= "abc" "abc") | 422 | (string= "abc" "abc") |
| @@ -422,10 +427,6 @@ Case is always significant, regardless of @code{case-fold-search}. | |||
| 422 | @result{} nil | 427 | @result{} nil |
| 423 | @end example | 428 | @end example |
| 424 | 429 | ||
| 425 | The function @code{string=} ignores the text properties of the two | ||
| 426 | strings. When @code{equal} (@pxref{Equality Predicates}) compares two | ||
| 427 | strings, it uses @code{string=}. | ||
| 428 | |||
| 429 | For technical reasons, a unibyte and a multibyte string are | 430 | For technical reasons, a unibyte and a multibyte string are |
| 430 | @code{equal} if and only if they contain the same sequence of | 431 | @code{equal} if and only if they contain the same sequence of |
| 431 | character codes and all these codes are either in the range 0 through | 432 | character codes and all these codes are either in the range 0 through |
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. |