diff options
| author | Richard M. Stallman | 1994-04-29 10:16:53 +0000 |
|---|---|---|
| committer | Richard M. Stallman | 1994-04-29 10:16:53 +0000 |
| commit | f57ddf6775eb05baae28b628f85e8d1ee6023d07 (patch) | |
| tree | 6789c10f84e58e0ed0598e320efe9350ff7e9420 | |
| parent | f25df2ab1d2a3bb5b11c92c2c4c7d93ae489b889 (diff) | |
| download | emacs-f57ddf6775eb05baae28b628f85e8d1ee6023d07.tar.gz emacs-f57ddf6775eb05baae28b628f85e8d1ee6023d07.zip | |
*** empty log message ***
| -rw-r--r-- | lispref/macros.texi | 51 | ||||
| -rw-r--r-- | lispref/variables.texi | 45 |
2 files changed, 63 insertions, 33 deletions
diff --git a/lispref/macros.texi b/lispref/macros.texi index 49d2d8e639d..13e85568eaf 100644 --- a/lispref/macros.texi +++ b/lispref/macros.texi | |||
| @@ -224,8 +224,8 @@ two forms yield identical results: | |||
| 224 | @end group | 224 | @end group |
| 225 | @end example | 225 | @end example |
| 226 | 226 | ||
| 227 | @findex , @{(with Backquote)} | 227 | @findex , @r{(with Backquote)} |
| 228 | The special marker, @code{,}, inside of the argument to backquote, | 228 | The special marker @code{,} inside of the argument to backquote |
| 229 | indicates a value that isn't constant. Backquote evaluates the | 229 | indicates a value that isn't constant. Backquote evaluates the |
| 230 | argument of @code{,} and puts the value in the list structure: | 230 | argument of @code{,} and puts the value in the list structure: |
| 231 | 231 | ||
| @@ -240,7 +240,7 @@ argument of @code{,} and puts the value in the list structure: | |||
| 240 | @end group | 240 | @end group |
| 241 | @end example | 241 | @end example |
| 242 | 242 | ||
| 243 | @findex ,@@ @{(with Backquote)} | 243 | @findex ,@@ @r{(with Backquote)} |
| 244 | @cindex splicing (with backquote) | 244 | @cindex splicing (with backquote) |
| 245 | You can also @dfn{splice} an evaluated value into the resulting list, | 245 | You can also @dfn{splice} an evaluated value into the resulting list, |
| 246 | using the special marker @code{,@@}. The elements of the spliced list | 246 | using the special marker @code{,@@}. The elements of the spliced list |
| @@ -278,7 +278,7 @@ Here are some examples: | |||
| 278 | @end group | 278 | @end group |
| 279 | @end example | 279 | @end example |
| 280 | 280 | ||
| 281 | Emacs 18 had a bug which made the previous example fail. The bug | 281 | Emacs 18 had a bug that made the previous example fail. The bug |
| 282 | affected @code{,@@} followed only by constant elements. If you are | 282 | affected @code{,@@} followed only by constant elements. If you are |
| 283 | concerned with Emacs 18 compatibility, you can work around the bug like | 283 | concerned with Emacs 18 compatibility, you can work around the bug like |
| 284 | this: | 284 | this: |
| @@ -320,9 +320,10 @@ should not be used: | |||
| 320 | 320 | ||
| 321 | @cindex CL note---@samp{,}, @samp{,@@} as functions | 321 | @cindex CL note---@samp{,}, @samp{,@@} as functions |
| 322 | @quotation | 322 | @quotation |
| 323 | @b{Common Lisp note:} in Common Lisp, @samp{,} and @samp{,@@} are implemented | 323 | @b{Common Lisp note:} in Common Lisp, @samp{,} and @samp{,@@} are |
| 324 | as reader macros, so they do not require parentheses. Emacs Lisp implements | 324 | implemented as reader macros, so they do not require parentheses. In |
| 325 | them as functions because reader macros are not supported (to save space). | 325 | Emacs Lisp they use function call syntax because reader macros are not |
| 326 | supported (for simplicity's sake). | ||
| 326 | @end quotation | 327 | @end quotation |
| 327 | 328 | ||
| 328 | @node Problems with Macros | 329 | @node Problems with Macros |
| @@ -389,11 +390,26 @@ For example, (for i from 1 to 10 do (print i))." | |||
| 389 | will write noise words (such as @code{from}, @code{to}, and @code{do}) | 390 | will write noise words (such as @code{from}, @code{to}, and @code{do}) |
| 390 | in those positions in the macro call.) | 391 | in those positions in the macro call.) |
| 391 | 392 | ||
| 392 | This macro suffers from the defect that @var{final} is evaluated on | 393 | Here's an equivalent definition simplified through use of backquote: |
| 393 | every iteration. If @var{final} is a constant, this is not a problem. | 394 | |
| 394 | If it is a more complex form, say @code{(long-complex-calculation x)}, | 395 | @smallexample |
| 395 | this can slow down the execution significantly. If @var{final} has side | 396 | @group |
| 396 | effects, executing it more than once is probably incorrect. | 397 | (defmacro for (var from init to final do &rest body) |
| 398 | "Execute a simple \"for\" loop. | ||
| 399 | For example, (for i from 1 to 10 do (print i))." | ||
| 400 | (` (let (((, var) (, init))) | ||
| 401 | (while (<= (, var) (, final)) | ||
| 402 | (,@@ body) | ||
| 403 | (inc (, var)))))) | ||
| 404 | @end group | ||
| 405 | @end smallexample | ||
| 406 | |||
| 407 | Both forms of this definition (with backquote and without) suffer from | ||
| 408 | the defect that @var{final} is evaluated on every iteration. If | ||
| 409 | @var{final} is a constant, this is not a problem. If it is a more | ||
| 410 | complex form, say @code{(long-complex-calculation x)}, this can slow | ||
| 411 | down the execution significantly. If @var{final} has side effects, | ||
| 412 | executing it more than once is probably incorrect. | ||
| 397 | 413 | ||
| 398 | @cindex macro argument evaluation | 414 | @cindex macro argument evaluation |
| 399 | A well-designed macro definition takes steps to avoid this problem by | 415 | A well-designed macro definition takes steps to avoid this problem by |
| @@ -475,11 +491,12 @@ the binding made by @code{for}. | |||
| 475 | 491 | ||
| 476 | The way to correct this is to use an uninterned symbol instead of | 492 | The way to correct this is to use an uninterned symbol instead of |
| 477 | @code{max} (@pxref{Creating Symbols}). The uninterned symbol can be | 493 | @code{max} (@pxref{Creating Symbols}). The uninterned symbol can be |
| 478 | bound and referred to just like any other symbol, but since it is created | 494 | bound and referred to just like any other symbol, but since it is |
| 479 | by @code{for}, we know that it cannot appear in the user's program. | 495 | created by @code{for}, we know that it cannot already appear in the |
| 480 | Since it is not interned, there is no way the user can put it into the | 496 | user's program. Since it is not interned, there is no way the user can |
| 481 | program later. It will never appear anywhere except where put by | 497 | put it into the program later. It will never appear anywhere except |
| 482 | @code{for}. Here is a definition of @code{for} which works this way: | 498 | where put by @code{for}. Here is a definition of @code{for} that works |
| 499 | this way: | ||
| 483 | 500 | ||
| 484 | @smallexample | 501 | @smallexample |
| 485 | @group | 502 | @group |
diff --git a/lispref/variables.texi b/lispref/variables.texi index 2b12607d488..ffee1a39c45 100644 --- a/lispref/variables.texi +++ b/lispref/variables.texi | |||
| @@ -20,7 +20,7 @@ symbol. The use of a symbol as a variable is independent of its use as | |||
| 20 | a function name. @xref{Symbol Components}. | 20 | a function name. @xref{Symbol Components}. |
| 21 | 21 | ||
| 22 | The Lisp objects that constitute a Lisp program determine the textual | 22 | The Lisp objects that constitute a Lisp program determine the textual |
| 23 | form of the program--it is simply the read syntax for those Lisp | 23 | form of the program---it is simply the read syntax for those Lisp |
| 24 | objects. This is why, for example, a variable in a textual Lisp program | 24 | objects. This is why, for example, a variable in a textual Lisp program |
| 25 | is written using the read syntax for the symbol that represents the | 25 | is written using the read syntax for the symbol that represents the |
| 26 | variable. | 26 | variable. |
| @@ -166,7 +166,7 @@ binding. | |||
| 166 | local bindings. | 166 | local bindings. |
| 167 | 167 | ||
| 168 | @defspec let (bindings@dots{}) forms@dots{} | 168 | @defspec let (bindings@dots{}) forms@dots{} |
| 169 | This function binds variables according to @var{bindings} and then | 169 | This special form binds variables according to @var{bindings} and then |
| 170 | evaluates all of the @var{forms} in textual order. The @code{let}-form | 170 | evaluates all of the @var{forms} in textual order. The @code{let}-form |
| 171 | returns the value of the last form in @var{forms}. | 171 | returns the value of the last form in @var{forms}. |
| 172 | 172 | ||
| @@ -217,7 +217,7 @@ form. Compare the following example with the example above for | |||
| 217 | @end example | 217 | @end example |
| 218 | @end defspec | 218 | @end defspec |
| 219 | 219 | ||
| 220 | Here is a complete list of the other facilities which create local | 220 | Here is a complete list of the other facilities that create local |
| 221 | bindings: | 221 | bindings: |
| 222 | 222 | ||
| 223 | @itemize @bullet | 223 | @itemize @bullet |
| @@ -365,9 +365,11 @@ more precisely, if its current binding is not void. It returns | |||
| 365 | 365 | ||
| 366 | @node Defining Variables | 366 | @node Defining Variables |
| 367 | @section Defining Global Variables | 367 | @section Defining Global Variables |
| 368 | @cindex variable definition | ||
| 368 | 369 | ||
| 369 | You may announce your intention to use a symbol as a global variable | 370 | You may announce your intention to use a symbol as a global variable |
| 370 | with a definition, using @code{defconst} or @code{defvar}. | 371 | with a @dfn{variable definition}: a special form, either @code{defconst} |
| 372 | or @code{defvar}. | ||
| 371 | 373 | ||
| 372 | In Emacs Lisp, definitions serve three purposes. First, they inform | 374 | In Emacs Lisp, definitions serve three purposes. First, they inform |
| 373 | people who read the code that certain symbols are @emph{intended} to be | 375 | people who read the code that certain symbols are @emph{intended} to be |
| @@ -381,7 +383,7 @@ variables in a program. | |||
| 381 | a matter of intent, serving to inform human readers of whether programs | 383 | a matter of intent, serving to inform human readers of whether programs |
| 382 | will change the variable. Emacs Lisp does not restrict the ways in | 384 | will change the variable. Emacs Lisp does not restrict the ways in |
| 383 | which a variable can be used based on @code{defconst} or @code{defvar} | 385 | which a variable can be used based on @code{defconst} or @code{defvar} |
| 384 | declarations. However, it also makes a difference for initialization: | 386 | declarations. However, it does make a difference for initialization: |
| 385 | @code{defconst} unconditionally initializes the variable, while | 387 | @code{defconst} unconditionally initializes the variable, while |
| 386 | @code{defvar} initializes it only if it is void. | 388 | @code{defvar} initializes it only if it is void. |
| 387 | 389 | ||
| @@ -518,7 +520,7 @@ pi | |||
| 518 | 520 | ||
| 519 | @defun user-variable-p variable | 521 | @defun user-variable-p variable |
| 520 | @cindex user option | 522 | @cindex user option |
| 521 | This function returns @code{t} if @var{variable} is a user option--- a | 523 | This function returns @code{t} if @var{variable} is a user option---a |
| 522 | variable intended to be set by the user for customization---and | 524 | variable intended to be set by the user for customization---and |
| 523 | @code{nil} otherwise. (Variables other than user options exist for the | 525 | @code{nil} otherwise. (Variables other than user options exist for the |
| 524 | internal purposes of Lisp programs, and users need not know about them.) | 526 | internal purposes of Lisp programs, and users need not know about them.) |
| @@ -647,8 +649,7 @@ This function sets @var{symbol}'s value to @var{value}, then returns | |||
| 647 | @var{symbol} is evaluated to obtain the symbol to set. | 649 | @var{symbol} is evaluated to obtain the symbol to set. |
| 648 | 650 | ||
| 649 | The most-local existing binding of the variable is the binding that is | 651 | The most-local existing binding of the variable is the binding that is |
| 650 | set; shadowed bindings are not affected. If @var{symbol} is not | 652 | set; shadowed bindings are not affected. |
| 651 | actually a symbol, a @code{wrong-type-argument} error is signaled. | ||
| 652 | 653 | ||
| 653 | @example | 654 | @example |
| 654 | @group | 655 | @group |
| @@ -681,18 +682,26 @@ one | |||
| 681 | @end group | 682 | @end group |
| 682 | @end example | 683 | @end example |
| 683 | 684 | ||
| 685 | If @var{symbol} is not actually a symbol, a @code{wrong-type-argument} | ||
| 686 | error is signaled. | ||
| 687 | |||
| 688 | @example | ||
| 689 | (set '(x y) 'z) | ||
| 690 | @error{} Wrong type argument: symbolp, (x y) | ||
| 691 | @end example | ||
| 692 | |||
| 684 | Logically speaking, @code{set} is a more fundamental primitive than | 693 | Logically speaking, @code{set} is a more fundamental primitive than |
| 685 | @code{setq}. Any use of @code{setq} can be trivially rewritten to use | 694 | @code{setq}. Any use of @code{setq} can be trivially rewritten to use |
| 686 | @code{set}; @code{setq} could even be defined as a macro, given the | 695 | @code{set}; @code{setq} could even be defined as a macro, given the |
| 687 | availability of @code{set}. However, @code{set} itself is rarely used; | 696 | availability of @code{set}. However, @code{set} itself is rarely used; |
| 688 | beginners hardly need to know about it. It is needed for choosing which | 697 | beginners hardly need to know about it. It is useful only for choosing |
| 689 | variable to set is made at run time. For example, the command | 698 | at run time which variable to set. For example, the command |
| 690 | @code{set-variable}, which reads a variable name from the user and then | 699 | @code{set-variable}, which reads a variable name from the user and then |
| 691 | sets the variable, needs to use @code{set}. | 700 | sets the variable, needs to use @code{set}. |
| 692 | 701 | ||
| 693 | @cindex CL note---@code{set} local | 702 | @cindex CL note---@code{set} local |
| 694 | @quotation | 703 | @quotation |
| 695 | @b{Common Lisp note:} in Common Lisp, @code{set} always changes the | 704 | @b{Common Lisp note:} In Common Lisp, @code{set} always changes the |
| 696 | symbol's special value, ignoring any lexical bindings. In Emacs Lisp, | 705 | symbol's special value, ignoring any lexical bindings. In Emacs Lisp, |
| 697 | all variables and all bindings are (in effect) special, so @code{set} | 706 | all variables and all bindings are (in effect) special, so @code{set} |
| 698 | always affects the most local existing binding. | 707 | always affects the most local existing binding. |
| @@ -726,7 +735,7 @@ located textually within the function or block that binds the variable. | |||
| 726 | @cindex CL note---special variables | 735 | @cindex CL note---special variables |
| 727 | @quotation | 736 | @quotation |
| 728 | @b{Common Lisp note:} variables declared ``special'' in Common Lisp | 737 | @b{Common Lisp note:} variables declared ``special'' in Common Lisp |
| 729 | are dynamically scoped like variables in Emacs Lisp. | 738 | are dynamically scoped, like variables in Emacs Lisp. |
| 730 | @end quotation | 739 | @end quotation |
| 731 | 740 | ||
| 732 | @menu | 741 | @menu |
| @@ -1012,7 +1021,7 @@ But @code{save-excursion} as shown here avoids the problem: | |||
| 1012 | buffer-local binding of buffer @samp{b}. | 1021 | buffer-local binding of buffer @samp{b}. |
| 1013 | 1022 | ||
| 1014 | When a file specifies local variable values, these become buffer-local | 1023 | When a file specifies local variable values, these become buffer-local |
| 1015 | value when you visit the file. @xref{Auto Major Mode}. | 1024 | values when you visit the file. @xref{Auto Major Mode}. |
| 1016 | 1025 | ||
| 1017 | @node Creating Buffer-Local | 1026 | @node Creating Buffer-Local |
| 1018 | @subsection Creating and Deleting Buffer-Local Bindings | 1027 | @subsection Creating and Deleting Buffer-Local Bindings |
| @@ -1116,6 +1125,10 @@ buffer. However, if you set the variable again, that will once again | |||
| 1116 | create a local binding for it. | 1125 | create a local binding for it. |
| 1117 | 1126 | ||
| 1118 | @code{kill-local-variable} returns @var{variable}. | 1127 | @code{kill-local-variable} returns @var{variable}. |
| 1128 | |||
| 1129 | This function is a command because it is sometimes useful to kill one | ||
| 1130 | buffer-local variable interactively, just as it is useful to create | ||
| 1131 | buffer-local variables interactively. | ||
| 1119 | @end deffn | 1132 | @end deffn |
| 1120 | 1133 | ||
| 1121 | @defun kill-all-local-variables | 1134 | @defun kill-all-local-variables |
| @@ -1156,7 +1169,7 @@ change a variable's default value regardless of whether the current | |||
| 1156 | buffer has a buffer-local binding. For example, you could use | 1169 | buffer has a buffer-local binding. For example, you could use |
| 1157 | @code{setq-default} to change the default setting of | 1170 | @code{setq-default} to change the default setting of |
| 1158 | @code{paragraph-start} for most buffers; and this would work even when | 1171 | @code{paragraph-start} for most buffers; and this would work even when |
| 1159 | you are in a C or Lisp mode buffer which has a buffer-local value for | 1172 | you are in a C or Lisp mode buffer that has a buffer-local value for |
| 1160 | this variable. | 1173 | this variable. |
| 1161 | 1174 | ||
| 1162 | @c Emacs 19 feature | 1175 | @c Emacs 19 feature |
| @@ -1172,8 +1185,8 @@ variable. If @var{symbol} is not buffer-local, this is equivalent to | |||
| 1172 | @end defun | 1185 | @end defun |
| 1173 | 1186 | ||
| 1174 | @c Emacs 19 feature | 1187 | @c Emacs 19 feature |
| 1175 | @defun default-boundp variable | 1188 | @defun default-boundp symbol |
| 1176 | The function @code{default-boundp} tells you whether @var{variable}'s | 1189 | The function @code{default-boundp} tells you whether @var{symbol}'s |
| 1177 | default value is nonvoid. If @code{(default-boundp 'foo)} returns | 1190 | default value is nonvoid. If @code{(default-boundp 'foo)} returns |
| 1178 | @code{nil}, then @code{(default-value 'foo)} would get an error. | 1191 | @code{nil}, then @code{(default-value 'foo)} would get an error. |
| 1179 | 1192 | ||