aboutsummaryrefslogtreecommitdiffstats
path: root/lispref
diff options
context:
space:
mode:
authorRichard M. Stallman1994-04-29 10:16:53 +0000
committerRichard M. Stallman1994-04-29 10:16:53 +0000
commitf57ddf6775eb05baae28b628f85e8d1ee6023d07 (patch)
tree6789c10f84e58e0ed0598e320efe9350ff7e9420 /lispref
parentf25df2ab1d2a3bb5b11c92c2c4c7d93ae489b889 (diff)
downloademacs-f57ddf6775eb05baae28b628f85e8d1ee6023d07.tar.gz
emacs-f57ddf6775eb05baae28b628f85e8d1ee6023d07.zip
*** empty log message ***
Diffstat (limited to 'lispref')
-rw-r--r--lispref/macros.texi51
-rw-r--r--lispref/variables.texi45
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)}
228The special marker, @code{,}, inside of the argument to backquote, 228The special marker @code{,} inside of the argument to backquote
229indicates a value that isn't constant. Backquote evaluates the 229indicates a value that isn't constant. Backquote evaluates the
230argument of @code{,} and puts the value in the list structure: 230argument 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)
245You can also @dfn{splice} an evaluated value into the resulting list, 245You can also @dfn{splice} an evaluated value into the resulting list,
246using the special marker @code{,@@}. The elements of the spliced list 246using 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
281Emacs 18 had a bug which made the previous example fail. The bug 281Emacs 18 had a bug that made the previous example fail. The bug
282affected @code{,@@} followed only by constant elements. If you are 282affected @code{,@@} followed only by constant elements. If you are
283concerned with Emacs 18 compatibility, you can work around the bug like 283concerned with Emacs 18 compatibility, you can work around the bug like
284this: 284this:
@@ -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
324as reader macros, so they do not require parentheses. Emacs Lisp implements 324implemented as reader macros, so they do not require parentheses. In
325them as functions because reader macros are not supported (to save space). 325Emacs Lisp they use function call syntax because reader macros are not
326supported (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))."
389will write noise words (such as @code{from}, @code{to}, and @code{do}) 390will write noise words (such as @code{from}, @code{to}, and @code{do})
390in those positions in the macro call.) 391in those positions in the macro call.)
391 392
392This macro suffers from the defect that @var{final} is evaluated on 393Here's an equivalent definition simplified through use of backquote:
393every iteration. If @var{final} is a constant, this is not a problem. 394
394If it is a more complex form, say @code{(long-complex-calculation x)}, 395@smallexample
395this can slow down the execution significantly. If @var{final} has side 396@group
396effects, 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.
399For 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
407Both forms of this definition (with backquote and without) suffer from
408the 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
410complex form, say @code{(long-complex-calculation x)}, this can slow
411down the execution significantly. If @var{final} has side effects,
412executing it more than once is probably incorrect.
397 413
398@cindex macro argument evaluation 414@cindex macro argument evaluation
399A well-designed macro definition takes steps to avoid this problem by 415A well-designed macro definition takes steps to avoid this problem by
@@ -475,11 +491,12 @@ the binding made by @code{for}.
475 491
476The way to correct this is to use an uninterned symbol instead of 492The 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
478bound and referred to just like any other symbol, but since it is created 494bound and referred to just like any other symbol, but since it is
479by @code{for}, we know that it cannot appear in the user's program. 495created by @code{for}, we know that it cannot already appear in the
480Since it is not interned, there is no way the user can put it into the 496user's program. Since it is not interned, there is no way the user can
481program later. It will never appear anywhere except where put by 497put 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: 498where put by @code{for}. Here is a definition of @code{for} that works
499this 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
20a function name. @xref{Symbol Components}. 20a 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
23form of the program--it is simply the read syntax for those Lisp 23form of the program---it is simply the read syntax for those Lisp
24objects. This is why, for example, a variable in a textual Lisp program 24objects. This is why, for example, a variable in a textual Lisp program
25is written using the read syntax for the symbol that represents the 25is written using the read syntax for the symbol that represents the
26variable. 26variable.
@@ -166,7 +166,7 @@ binding.
166local bindings. 166local bindings.
167 167
168@defspec let (bindings@dots{}) forms@dots{} 168@defspec let (bindings@dots{}) forms@dots{}
169This function binds variables according to @var{bindings} and then 169This special form binds variables according to @var{bindings} and then
170evaluates all of the @var{forms} in textual order. The @code{let}-form 170evaluates all of the @var{forms} in textual order. The @code{let}-form
171returns the value of the last form in @var{forms}. 171returns 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
221bindings: 221bindings:
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
370with a definition, using @code{defconst} or @code{defvar}. 371with a @dfn{variable definition}: a special form, either @code{defconst}
372or @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
373people who read the code that certain symbols are @emph{intended} to be 375people who read the code that certain symbols are @emph{intended} to be
@@ -381,7 +383,7 @@ variables in a program.
381a matter of intent, serving to inform human readers of whether programs 383a matter of intent, serving to inform human readers of whether programs
382will change the variable. Emacs Lisp does not restrict the ways in 384will change the variable. Emacs Lisp does not restrict the ways in
383which a variable can be used based on @code{defconst} or @code{defvar} 385which a variable can be used based on @code{defconst} or @code{defvar}
384declarations. However, it also makes a difference for initialization: 386declarations. 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
521This function returns @code{t} if @var{variable} is a user option--- a 523This function returns @code{t} if @var{variable} is a user option---a
522variable intended to be set by the user for customization---and 524variable 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
524internal purposes of Lisp programs, and users need not know about them.) 526internal 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
649The most-local existing binding of the variable is the binding that is 651The most-local existing binding of the variable is the binding that is
650set; shadowed bindings are not affected. If @var{symbol} is not 652set; shadowed bindings are not affected.
651actually 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
685If @var{symbol} is not actually a symbol, a @code{wrong-type-argument}
686error is signaled.
687
688@example
689(set '(x y) 'z)
690@error{} Wrong type argument: symbolp, (x y)
691@end example
692
684Logically speaking, @code{set} is a more fundamental primitive than 693Logically 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
687availability of @code{set}. However, @code{set} itself is rarely used; 696availability of @code{set}. However, @code{set} itself is rarely used;
688beginners hardly need to know about it. It is needed for choosing which 697beginners hardly need to know about it. It is useful only for choosing
689variable to set is made at run time. For example, the command 698at 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
691sets the variable, needs to use @code{set}. 700sets 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
696symbol's special value, ignoring any lexical bindings. In Emacs Lisp, 705symbol's special value, ignoring any lexical bindings. In Emacs Lisp,
697all variables and all bindings are (in effect) special, so @code{set} 706all variables and all bindings are (in effect) special, so @code{set}
698always affects the most local existing binding. 707always 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
729are dynamically scoped like variables in Emacs Lisp. 738are 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:
1012buffer-local binding of buffer @samp{b}. 1021buffer-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
1015value when you visit the file. @xref{Auto Major Mode}. 1024values 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
1116create a local binding for it. 1125create a local binding for it.
1117 1126
1118@code{kill-local-variable} returns @var{variable}. 1127@code{kill-local-variable} returns @var{variable}.
1128
1129This function is a command because it is sometimes useful to kill one
1130buffer-local variable interactively, just as it is useful to create
1131buffer-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
1156buffer has a buffer-local binding. For example, you could use 1169buffer 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
1159you are in a C or Lisp mode buffer which has a buffer-local value for 1172you are in a C or Lisp mode buffer that has a buffer-local value for
1160this variable. 1173this 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
1176The function @code{default-boundp} tells you whether @var{variable}'s 1189The function @code{default-boundp} tells you whether @var{symbol}'s
1177default value is nonvoid. If @code{(default-boundp 'foo)} returns 1190default 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