aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChong Yidong2012-02-04 22:56:32 +0800
committerChong Yidong2012-02-04 22:56:32 +0800
commit735cc5ca6fe9b19aa9f69eb696baef2b8b4bd0fb (patch)
treecf9722413f79647d3c302142d8e9cc95bdfa13af
parentd7f29f8e5c589e4676b2ab53fea7a8b4cfcd4be3 (diff)
downloademacs-735cc5ca6fe9b19aa9f69eb696baef2b8b4bd0fb.tar.gz
emacs-735cc5ca6fe9b19aa9f69eb696baef2b8b4bd0fb.zip
Update Functions chapter of Lisp manual; document closures.
* doc/emacs/functions.texi (What Is a Function): Add closures. Mention "return value" terminology. Add xref for command-execute. Remove unused "keystroke command" terminology. (Lambda Expressions): Give a different example than in the following subsection. Add xref to Anonymous Functions. (Function Documentation): Remove gratuitous markup. (Function Names): Move introductory text to `What Is a Function'. (Defining Functions): Fix defun argument spec. (Anonymous Functions): Document lambda macro explicitly. Mention effects on lexical binding. (Function Cells): Downplay direct usage of fset. (Closures): New node. (Inline Functions): Remove "open-code" terminology. (Declaring Functions): Minor tweak; .m is not C code. * doc/emacs/variables.texi (Variables): Don't refer to "global value". (Local Variables, Void Variables): Copyedits. (Lexical Binding): Minor clarification of example. (File Local Variables): Mention :safe and :risky defcustom args. (Lexical Binding): Add xref to Closures node.
-rw-r--r--admin/FOR-RELEASE6
-rw-r--r--doc/lispref/ChangeLog23
-rw-r--r--doc/lispref/elisp.texi1
-rw-r--r--doc/lispref/functions.texi671
-rw-r--r--doc/lispref/variables.texi137
-rw-r--r--doc/lispref/vol1.texi1
-rw-r--r--doc/lispref/vol2.texi1
-rw-r--r--etc/NEWS7
8 files changed, 416 insertions, 431 deletions
diff --git a/admin/FOR-RELEASE b/admin/FOR-RELEASE
index 45c57200055..34c9c4bb21c 100644
--- a/admin/FOR-RELEASE
+++ b/admin/FOR-RELEASE
@@ -111,7 +111,7 @@ TUTORIAL.he eliz
111TUTORIAL.it 111TUTORIAL.it
112TUTORIAL.ja 112TUTORIAL.ja
113TUTORIAL.ko 113TUTORIAL.ko
114TUTORIAL.nl 114TUTORIAL.nl Pieter Schoenmakers
115TUTORIAL.pl 115TUTORIAL.pl
116TUTORIAL.pt_BR 116TUTORIAL.pt_BR
117TUTORIAL.ro 117TUTORIAL.ro
@@ -197,7 +197,7 @@ errors.texi
197eval.texi cyd 197eval.texi cyd
198files.texi 198files.texi
199frames.texi 199frames.texi
200functions.texi 200functions.texi cyd
201hash.texi cyd 201hash.texi cyd
202help.texi 202help.texi
203hooks.texi 203hooks.texi
@@ -228,7 +228,7 @@ symbols.texi cyd
228syntax.texi 228syntax.texi
229text.texi 229text.texi
230tips.texi 230tips.texi
231variables.texi 231variables.texi cyd
232windows.texi 232windows.texi
233 233
234* PLANNED ADDITIONS 234* PLANNED ADDITIONS
diff --git a/doc/lispref/ChangeLog b/doc/lispref/ChangeLog
index 36780b20248..e98e18b864d 100644
--- a/doc/lispref/ChangeLog
+++ b/doc/lispref/ChangeLog
@@ -1,3 +1,26 @@
12012-02-04 Chong Yidong <cyd@gnu.org>
2
3 * functions.texi (What Is a Function): Add closures. Mention
4 "return value" terminology. Add xref for command-execute. Remove
5 unused "keystroke command" terminology.
6 (Lambda Expressions): Give a different example than in the
7 following subsection. Add xref to Anonymous Functions.
8 (Function Documentation): Remove gratuitous markup.
9 (Function Names): Move introductory text to `What Is a Function'.
10 (Defining Functions): Fix defun argument spec.
11 (Anonymous Functions): Document lambda macro explicitly. Mention
12 effects on lexical binding.
13 (Function Cells): Downplay direct usage of fset.
14 (Closures): New node.
15 (Inline Functions): Remove "open-code" terminology.
16 (Declaring Functions): Minor tweak; .m is not C code.
17
18 * variables.texi (Variables): Don't refer to "global value".
19 (Local Variables, Void Variables): Copyedits.
20 (Lexical Binding): Minor clarification of example.
21 (File Local Variables): Mention :safe and :risky defcustom args.
22 (Lexical Binding): Add xref to Closures node.
23
12012-02-04 Glenn Morris <rgm@gnu.org> 242012-02-04 Glenn Morris <rgm@gnu.org>
2 25
3 * minibuf.texi (High-Level Completion): Updates for read-color. 26 * minibuf.texi (High-Level Completion): Updates for read-color.
diff --git a/doc/lispref/elisp.texi b/doc/lispref/elisp.texi
index b20ac1a9442..b4692cd33ce 100644
--- a/doc/lispref/elisp.texi
+++ b/doc/lispref/elisp.texi
@@ -459,6 +459,7 @@ Functions
459* Anonymous Functions:: Lambda expressions are functions with no names. 459* Anonymous Functions:: Lambda expressions are functions with no names.
460* Function Cells:: Accessing or setting the function definition 460* Function Cells:: Accessing or setting the function definition
461 of a symbol. 461 of a symbol.
462* Closures:: Functions that enclose a lexical environment.
462* Obsolete Functions:: Declaring functions obsolete. 463* Obsolete Functions:: Declaring functions obsolete.
463* Inline Functions:: Defining functions that the compiler 464* Inline Functions:: Defining functions that the compiler
464 will open code. 465 will open code.
diff --git a/doc/lispref/functions.texi b/doc/lispref/functions.texi
index ada00867bd0..9ee94557066 100644
--- a/doc/lispref/functions.texi
+++ b/doc/lispref/functions.texi
@@ -21,8 +21,9 @@ define them.
21* Anonymous Functions:: Lambda expressions are functions with no names. 21* Anonymous Functions:: Lambda expressions are functions with no names.
22* Function Cells:: Accessing or setting the function definition 22* Function Cells:: Accessing or setting the function definition
23 of a symbol. 23 of a symbol.
24* Closures:: Functions that enclose a lexical environment.
24* Obsolete Functions:: Declaring functions obsolete. 25* Obsolete Functions:: Declaring functions obsolete.
25* Inline Functions:: Defining functions that the compiler will open code. 26* Inline Functions:: Functions that the compiler will expand inline.
26* Declaring Functions:: Telling the compiler that a function is defined. 27* Declaring Functions:: Telling the compiler that a function is defined.
27* Function Safety:: Determining whether a function is safe to call. 28* Function Safety:: Determining whether a function is safe to call.
28* Related Topics:: Cross-references to specific Lisp primitives 29* Related Topics:: Cross-references to specific Lisp primitives
@@ -32,104 +33,117 @@ define them.
32@node What Is a Function 33@node What Is a Function
33@section What Is a Function? 34@section What Is a Function?
34 35
35 In a general sense, a function is a rule for carrying on a computation 36@cindex return value
36given several values called @dfn{arguments}. The result of the 37@cindex value of function
37computation is called the value of the function. The computation can 38@cindex argument
38also have side effects: lasting changes in the values of variables or 39 In a general sense, a function is a rule for carrying out a
39the contents of data structures. 40computation given input values called @dfn{arguments}. The result of
40 41the computation is called the @dfn{value} or @dfn{return value} of the
41 Here are important terms for functions in Emacs Lisp and for other 42function. The computation can also have side effects, such as lasting
42function-like objects. 43changes in the values of variables or the contents of data structures.
44
45 In most computer languages, every function has a name. But in Lisp,
46a function in the strictest sense has no name: it is an object which
47can @emph{optionally} be associated with a symbol (e.g.@: @code{car})
48that serves as the function name. @xref{Function Names}. When a
49function has been given a name, we usually also refer to that symbol
50as a ``function'' (e.g.@: we refer to ``the function @code{car}'').
51In this manual, the distinction between a function name and the
52function object itself is usually unimportant, but we will take note
53wherever it is relevant.
54
55 Certain function-like objects, called @dfn{special forms} and
56@dfn{macros}, also accept arguments to carry out computations.
57However, as explained below, these are not considered functions in
58Emacs Lisp.
59
60 Here are important terms for functions and function-like objects:
43 61
44@table @dfn 62@table @dfn
45@item function 63@item lambda expression
46@cindex function 64A function (in the strict sense, i.e.@: a function object) which is
47In Emacs Lisp, a @dfn{function} is anything that can be applied to 65written in Lisp. These are described in the following section.
48arguments in a Lisp program. In some cases, we use it more 66@ifnottex
49specifically to mean a function written in Lisp. Special forms and 67@xref{Lambda Expressions}.
50macros are not functions. 68@end ifnottex
51 69
52@item primitive 70@item primitive
53@cindex primitive 71@cindex primitive
54@cindex subr 72@cindex subr
55@cindex built-in function 73@cindex built-in function
56A @dfn{primitive} is a function callable from Lisp that is written in C, 74A function which is callable from Lisp but is actually written in C.
57such as @code{car} or @code{append}. These functions are also called 75Primitives are also called @dfn{built-in functions}, or @dfn{subrs}.
58@dfn{built-in functions}, or @dfn{subrs}. (Special forms are also 76Examples include functions like @code{car} and @code{append}. In
59considered primitives.) 77addition, all special forms (see below) are also considered
60 78primitives.
61Usually the reason we implement a function as a primitive is either 79
62because it is fundamental, because it provides a low-level interface 80Usually, a function is implemented as a primitive because it is a
63to operating system services, or because it needs to run fast. 81fundamental part of Lisp (e.g.@: @code{car}), or because it provides a
64Primitives can be modified or added only by changing the C sources and 82low-level interface to operating system services, or because it needs
65recompiling the editor. See @ref{Writing Emacs Primitives}. 83to run fast. Unlike functions defined in Lisp, primitives can be
66 84modified or added only by changing the C sources and recompiling
67@item lambda expression 85Emacs. See @ref{Writing Emacs Primitives}.
68A @dfn{lambda expression} is a function written in Lisp.
69These are described in the following section.
70@ifnottex
71@xref{Lambda Expressions}.
72@end ifnottex
73 86
74@item special form 87@item special form
75A @dfn{special form} is a primitive that is like a function but does not 88A primitive that is like a function but does not evaluate all of its
76evaluate all of its arguments in the usual way. It may evaluate only 89arguments in the usual way. It may evaluate only some of the
77some of the arguments, or may evaluate them in an unusual order, or 90arguments, or may evaluate them in an unusual order, or several times.
78several times. Many special forms are described in @ref{Control 91Examples include @code{if}, @code{and}, and @code{while}.
79Structures}. 92@xref{Special Forms}.
80 93
81@item macro 94@item macro
82@cindex macro 95@cindex macro
83A @dfn{macro} is a construct defined in Lisp by the programmer. It 96A construct defined in Lisp, which differs from a function in that it
84differs from a function in that it translates a Lisp expression that you 97translates a Lisp expression into another expression which is to be
85write into an equivalent expression to be evaluated instead of the 98evaluated instead of the original expression. Macros enable Lisp
86original expression. Macros enable Lisp programmers to do the sorts of 99programmers to do the sorts of things that special forms can do.
87things that special forms can do. @xref{Macros}, for how to define and 100@xref{Macros}.
88use macros.
89 101
90@item command 102@item command
91@cindex command 103@cindex command
92A @dfn{command} is an object that @code{command-execute} can invoke; it 104An object which can be invoked via the @code{command-execute}
93is a possible definition for a key sequence. Some functions are 105primitive, usually due to the user typing in a key sequence
94commands; a function written in Lisp is a command if it contains an 106@dfn{bound} to that command. @xref{Interactive Call}. A command is
95interactive declaration (@pxref{Defining Commands}). Such a function 107usually a function; if the function is written in Lisp, it is made
96can be called from Lisp expressions like other functions; in this case, 108into a command by an @code{interactive} form in the function
97the fact that the function is a command makes no difference. 109definition (@pxref{Defining Commands}). Commands that are functions
110can also be called from Lisp expressions, just like other functions.
98 111
99Keyboard macros (strings and vectors) are commands also, even though 112Keyboard macros (strings and vectors) are commands also, even though
100they are not functions. A symbol is a command if its function 113they are not functions. @xref{Keyboard Macros}. We say that a symbol
101definition is a command; such symbols can be invoked with @kbd{M-x}. 114is a command if its function cell contains a command (@pxref{Symbol
102The symbol is a function as well if the definition is a function. 115Components}); such a @dfn{named command} can be invoked with
103@xref{Interactive Call}. 116@kbd{M-x}.
104 117
105@item keystroke command 118@item closure
106@cindex keystroke command 119A function object that is much like a lambda expression, except that
107A @dfn{keystroke command} is a command that is bound to a key sequence 120it also encloses an ``environment'' of lexical variable bindings.
108(typically one to three keystrokes). The distinction is made here 121@xref{Closures}.
109merely to avoid confusion with the meaning of ``command'' in non-Emacs
110editors; for Lisp programs, the distinction is normally unimportant.
111 122
112@item byte-code function 123@item byte-code function
113A @dfn{byte-code function} is a function that has been compiled by the 124A function that has been compiled by the byte compiler.
114byte compiler. @xref{Byte-Code Type}. 125@xref{Byte-Code Type}.
115 126
116@item autoload object 127@item autoload object
117@cindex autoload object 128@cindex autoload object
118An @dfn{autoload object} is a place-holder for a real function. If 129A place-holder for a real function. If the autoload object is called,
119the autoload object is called, it will make Emacs load the file 130Emacs loads the file containing the definition of the real function,
120containing the definition of the real function, and then call the real 131and then calls the real function. @xref{Autoload}.
121function instead.
122@end table 132@end table
123 133
134 You can use the function @code{functionp} to test if an object is a
135function:
136
124@defun functionp object 137@defun functionp object
125This function returns @code{t} if @var{object} is any kind of 138This function returns @code{t} if @var{object} is any kind of
126function, i.e.@: can be passed to @code{funcall}. Note that 139function, i.e.@: can be passed to @code{funcall}. Note that
127@code{functionp} returns @code{nil} for special forms (@pxref{Special 140@code{functionp} returns @code{t} for symbols that are function names,
128Forms}). 141and returns @code{nil} for special forms.
129@end defun 142@end defun
130 143
131Unlike @code{functionp}, the next three functions do @emph{not} 144@noindent
132treat a symbol as its function definition. 145Unlike @code{functionp}, the next three functions do @emph{not} treat
146a symbol as its function definition.
133 147
134@defun subrp object 148@defun subrp object
135This function returns @code{t} if @var{object} is a built-in function 149This function returns @code{t} if @var{object} is a built-in function
@@ -172,21 +186,26 @@ function with @code{&rest} arguments, or the symbol @code{unevalled} if
172@section Lambda Expressions 186@section Lambda Expressions
173@cindex lambda expression 187@cindex lambda expression
174 188
175 A function written in Lisp is a list that looks like this: 189 A lambda expression is a function object written in Lisp. Here is
190an example:
176 191
177@example 192@example
178(lambda (@var{arg-variables}@dots{}) 193(lambda (x)
179 @r{[}@var{documentation-string}@r{]} 194 "Return the hyperbolic cosine of X."
180 @r{[}@var{interactive-declaration}@r{]} 195 (* 0.5 (+ (exp x) (exp (- x)))))
181 @var{body-forms}@dots{})
182@end example 196@end example
183 197
184@noindent 198@noindent
185Such a list is called a @dfn{lambda expression}. In Emacs Lisp, it 199In Emacs Lisp, such a list is valid as an expression---it evaluates to
186actually is valid as an expression---it evaluates to itself. In some 200itself. But its main use is not to be evaluated as an expression, but
187other Lisp dialects, a lambda expression is not a valid expression at 201to be called as a function.
188all. In either case, its main use is not to be evaluated as an 202
189expression, but to be called as a function. 203 A lambda expression, by itself, has no name; it is an @dfn{anonymous
204function}. Although lambda expressions can be used this way
205(@pxref{Anonymous Functions}), they are more commonly associated with
206symbols to make @dfn{named functions} (@pxref{Function Names}).
207Before going into these details, the following subsections describe
208the components of a lambda expression and what they do.
190 209
191@menu 210@menu
192* Lambda Components:: The parts of a lambda expression. 211* Lambda Components:: The parts of a lambda expression.
@@ -198,10 +217,7 @@ expression, but to be called as a function.
198@node Lambda Components 217@node Lambda Components
199@subsection Components of a Lambda Expression 218@subsection Components of a Lambda Expression
200 219
201@ifnottex 220 A lambda expression is a list that looks like this:
202
203 A function written in Lisp (a ``lambda expression'') is a list that
204looks like this:
205 221
206@example 222@example
207(lambda (@var{arg-variables}@dots{}) 223(lambda (@var{arg-variables}@dots{})
@@ -209,7 +225,6 @@ looks like this:
209 [@var{interactive-declaration}] 225 [@var{interactive-declaration}]
210 @var{body-forms}@dots{}) 226 @var{body-forms}@dots{})
211@end example 227@end example
212@end ifnottex
213 228
214@cindex lambda list 229@cindex lambda list
215 The first element of a lambda expression is always the symbol 230 The first element of a lambda expression is always the symbol
@@ -243,9 +258,9 @@ code to do the work of the function (or, as a Lisp programmer would say,
243function is the value returned by the last element of the body. 258function is the value returned by the last element of the body.
244 259
245@node Simple Lambda 260@node Simple Lambda
246@subsection A Simple Lambda-Expression Example 261@subsection A Simple Lambda Expression Example
247 262
248 Consider for example the following function: 263 Consider the following example:
249 264
250@example 265@example
251(lambda (a b c) (+ a b c)) 266(lambda (a b c) (+ a b c))
@@ -283,18 +298,15 @@ This evaluates the arguments @code{1}, @code{(* 2 3)}, and @code{(- 5
2834)} from left to right. Then it applies the lambda expression to the 2984)} from left to right. Then it applies the lambda expression to the
284argument values 1, 6 and 1 to produce the value 8. 299argument values 1, 6 and 1 to produce the value 8.
285 300
286 It is not often useful to write a lambda expression as the @sc{car} of 301 As these examples show, you can use a form with a lambda expression
287a form in this way. You can get the same result, of making local 302as its @sc{car} to make local variables and give them values. In the
288variables and giving them values, using the special form @code{let} 303old days of Lisp, this technique was the only way to bind and
289(@pxref{Local Variables}). And @code{let} is clearer and easier to use. 304initialize local variables. But nowadays, it is clearer to use the
290In practice, lambda expressions are either stored as the function 305special form @code{let} for this purpose (@pxref{Local Variables}).
291definitions of symbols, to produce named functions, or passed as 306Lambda expressions are mainly used as anonymous functions for passing
292arguments to other functions (@pxref{Anonymous Functions}). 307as arguments to other functions (@pxref{Anonymous Functions}), or
293 308stored as symbol function definitions to produce named functions
294 However, calls to explicit lambda expressions were very useful in the 309(@pxref{Function Names}).
295old days of Lisp, before the special form @code{let} was invented. At
296that time, they were the only way to bind and initialize local
297variables.
298 310
299@node Argument List 311@node Argument List
300@subsection Other Features of Argument Lists 312@subsection Other Features of Argument Lists
@@ -405,12 +417,12 @@ after a @code{&rest} argument.
405@subsection Documentation Strings of Functions 417@subsection Documentation Strings of Functions
406@cindex documentation of function 418@cindex documentation of function
407 419
408 A lambda expression may optionally have a @dfn{documentation string} just 420 A lambda expression may optionally have a @dfn{documentation string}
409after the lambda list. This string does not affect execution of the 421just after the lambda list. This string does not affect execution of
410function; it is a kind of comment, but a systematized comment which 422the function; it is a kind of comment, but a systematized comment
411actually appears inside the Lisp world and can be used by the Emacs help 423which actually appears inside the Lisp world and can be used by the
412facilities. @xref{Documentation}, for how the @var{documentation-string} is 424Emacs help facilities. @xref{Documentation}, for how the
413accessed. 425documentation string is accessed.
414 426
415 It is a good idea to provide documentation strings for all the 427 It is a good idea to provide documentation strings for all the
416functions in your program, even those that are called only from within 428functions in your program, even those that are called only from within
@@ -463,55 +475,45 @@ way users think of the parts of the macro call.
463@cindex named function 475@cindex named function
464@cindex function name 476@cindex function name
465 477
466 In most computer languages, every function has a name; the idea of a 478 A symbol can serve as the name of a function. This happens when the
467function without a name is nonsensical. In Lisp, a function in the 479symbol's @dfn{function cell} (@pxref{Symbol Components}) contains a
468strictest sense has no name. It is simply a list whose first element is 480function object (e.g.@: a lambda expression). Then the symbol itself
469@code{lambda}, a byte-code function object, or a primitive subr-object. 481becomes a valid, callable function, equivalent to the function object
470 482in its function cell.
471 However, a symbol can serve as the name of a function. This happens 483
472when you put the function in the symbol's @dfn{function cell} 484 The contents of the function cell are also called the symbol's
473(@pxref{Symbol Components}). Then the symbol itself becomes a valid, 485@dfn{function definition}. The procedure of using a symbol's function
474callable function, equivalent to the list or subr-object that its 486definition in place of the symbol is called @dfn{symbol function
475function cell refers to. The contents of the function cell are also 487indirection}; see @ref{Function Indirection}. If you have not given a
476called the symbol's @dfn{function definition}. The procedure of using a 488symbol a function definition, its function cell is said to be
477symbol's function definition in place of the symbol is called 489@dfn{void}, and it cannot be used as a function.
478@dfn{symbol function indirection}; see @ref{Function Indirection}. 490
479 491 In practice, nearly all functions have names, and are referred to by
480 In practice, nearly all functions are given names in this way and 492their names. You can create a named Lisp function by defining a
481referred to through their names. For example, the symbol @code{car} works 493lambda expression and putting it in a function cell (@pxref{Function
482as a function and does what it does because the primitive subr-object 494Cells}). However, it is more common to use the @code{defun} special
483@code{#<subr car>} is stored in its function cell. 495form, described in the next section.
496@ifnottex
497@xref{Defining Functions}.
498@end ifnottex
484 499
485 We give functions names because it is convenient to refer to them by 500 We give functions names because it is convenient to refer to them by
486their names in Lisp expressions. For primitive subr-objects such as 501their names in Lisp expressions. Also, a named Lisp function can
487@code{#<subr car>}, names are the only way you can refer to them: there 502easily refer to itself---it can be recursive. Furthermore, primitives
488is no read syntax for such objects. For functions written in Lisp, the 503can only be referred to textually by their names, since primitive
489name is more convenient to use in a call than an explicit lambda 504function objects (@pxref{Primitive Function Type}) have no read
490expression. Also, a function with a name can refer to itself---it can 505syntax.
491be recursive. Writing the function's name in its own definition is much 506
492more convenient than making the function definition point to itself 507 A function need not have a unique name. A given function object
493(something that is not impossible but that has various disadvantages in 508@emph{usually} appears in the function cell of only one symbol, but
494practice). 509this is just a convention. It is easy to store it in several symbols
495 510using @code{fset}; then each of the symbols is a valid name for the
496 We often identify functions with the symbols used to name them. For 511same function.
497example, we often speak of ``the function @code{car},'' not 512
498distinguishing between the symbol @code{car} and the primitive 513 Note that a symbol used as a function name may also be used as a
499subr-object that is its function definition. For most purposes, the 514variable; these two uses of a symbol are independent and do not
500distinction is not important. 515conflict. (This is not the case in some dialects of Lisp, like
501 516Scheme.)
502 Even so, keep in mind that a function need not have a unique name. While
503a given function object @emph{usually} appears in the function cell of only
504one symbol, this is just a matter of convenience. It is easy to store
505it in several symbols using @code{fset}; then each of the symbols is
506equally well a name for the same function.
507
508 A symbol used as a function name may also be used as a variable; these
509two uses of a symbol are independent and do not conflict. (Some Lisp
510dialects, such as Scheme, do not distinguish between a symbol's value
511and its function definition; a symbol's value as a variable is also its
512function definition.) If you have not given a symbol a function
513definition, you cannot use it as a function; whether the symbol has a
514value as a variable makes no difference to this.
515 517
516@node Defining Functions 518@node Defining Functions
517@section Defining Functions 519@section Defining Functions
@@ -521,7 +523,7 @@ value as a variable makes no difference to this.
521is called @dfn{defining a function}, and it is done with the 523is called @dfn{defining a function}, and it is done with the
522@code{defun} special form. 524@code{defun} special form.
523 525
524@defspec defun name argument-list body-forms 526@defspec defun name argument-list body-forms...
525@code{defun} is the usual way to define new Lisp functions. It 527@code{defun} is the usual way to define new Lisp functions. It
526defines the symbol @var{name} as a function that looks like this: 528defines the symbol @var{name} as a function that looks like this:
527 529
@@ -534,14 +536,9 @@ defines the symbol @var{name} as a function that looks like this:
534value. 536value.
535 537
536As described previously, @var{argument-list} is a list of argument 538As described previously, @var{argument-list} is a list of argument
537names and may include the keywords @code{&optional} and @code{&rest} 539names and may include the keywords @code{&optional} and @code{&rest}.
538(@pxref{Lambda Expressions}). Also, the first two of the 540Also, the first two of the @var{body-forms} may be a documentation
539@var{body-forms} may be a documentation string and an interactive 541string and an interactive declaration. @xref{Lambda Components}.
540declaration.
541
542There is no conflict if the same symbol @var{name} is also used as a
543variable, since the symbol's value cell is independent of the function
544cell. @xref{Symbol Components}.
545 542
546Here are some examples: 543Here are some examples:
547 544
@@ -575,7 +572,7 @@ Here are some examples:
575 572
576@group 573@group
577(defun capitalize-backwards () 574(defun capitalize-backwards ()
578 "Upcase the last letter of a word." 575 "Upcase the last letter of the word at point."
579 (interactive) 576 (interactive)
580 (backward-word 1) 577 (backward-word 1)
581 (forward-word 1) 578 (forward-word 1)
@@ -587,9 +584,10 @@ Here are some examples:
587 584
588Be careful not to redefine existing functions unintentionally. 585Be careful not to redefine existing functions unintentionally.
589@code{defun} redefines even primitive functions such as @code{car} 586@code{defun} redefines even primitive functions such as @code{car}
590without any hesitation or notification. Redefining a function already 587without any hesitation or notification. Emacs does not prevent you
591defined is often done deliberately, and there is no way to distinguish 588from doing this, because redefining a function is sometimes done
592deliberate redefinition from unintentional redefinition. 589deliberately, and there is no way to distinguish deliberate
590redefinition from unintentional redefinition.
593@end defspec 591@end defspec
594 592
595@cindex function aliases 593@cindex function aliases
@@ -626,7 +624,8 @@ call the primitive's C definition directly, so changing the symbol's
626definition will have no effect on them. 624definition will have no effect on them.
627 625
628 See also @code{defsubst}, which defines a function like @code{defun} 626 See also @code{defsubst}, which defines a function like @code{defun}
629and tells the Lisp compiler to open-code it. @xref{Inline Functions}. 627and tells the Lisp compiler to perform inline expansion on it.
628@xref{Inline Functions}.
630 629
631@node Calling Functions 630@node Calling Functions
632@section Calling Functions 631@section Calling Functions
@@ -790,11 +789,10 @@ This function returns @var{arg} and has no side effects.
790This function ignores any arguments and returns @code{nil}. 789This function ignores any arguments and returns @code{nil}.
791@end defun 790@end defun
792 791
793 Emacs Lisp functions can also be user-visible @dfn{commands}. A 792 Some functions are user-visible @dfn{commands}, which can be called
794command is a function that has an @dfn{interactive} specification. 793interactively (usually by a key sequence). It is possible to invoke
795You may want to call these functions as if they were called 794such a command exactly as though it was called interactively, by using
796interactively. See @ref{Interactive Call} for details on how to do 795the @code{call-interactively} function. @xref{Interactive Call}.
797that.
798 796
799@node Mapping Functions 797@node Mapping Functions
800@section Mapping Functions 798@section Mapping Functions
@@ -802,12 +800,12 @@ that.
802 800
803 A @dfn{mapping function} applies a given function (@emph{not} a 801 A @dfn{mapping function} applies a given function (@emph{not} a
804special form or macro) to each element of a list or other collection. 802special form or macro) to each element of a list or other collection.
805Emacs Lisp has several such functions; @code{mapcar} and 803Emacs Lisp has several such functions; this section describes
806@code{mapconcat}, which scan a list, are described here. 804@code{mapcar}, @code{mapc}, and @code{mapconcat}, which map over a
807@xref{Definition of mapatoms}, for the function @code{mapatoms} which 805list. @xref{Definition of mapatoms}, for the function @code{mapatoms}
808maps over the symbols in an obarray. @xref{Definition of maphash}, 806which maps over the symbols in an obarray. @xref{Definition of
809for the function @code{maphash} which maps over key/value associations 807maphash}, for the function @code{maphash} which maps over key/value
810in a hash table. 808associations in a hash table.
811 809
812 These mapping functions do not allow char-tables because a char-table 810 These mapping functions do not allow char-tables because a char-table
813is a sparse array whose nominal range of indices is very large. To map 811is a sparse array whose nominal range of indices is very large. To map
@@ -898,48 +896,67 @@ bool-vector, or a string.
898@section Anonymous Functions 896@section Anonymous Functions
899@cindex anonymous function 897@cindex anonymous function
900 898
901 In Lisp, a function is a list that starts with @code{lambda}, a 899 Although functions are usually defined with @code{defun} and given
902byte-code function compiled from such a list, or alternatively a 900names at the same time, it is sometimes convenient to use an explicit
903primitive subr-object; names are ``extra.'' Although functions are 901lambda expression---an @dfn{anonymous function}. Anonymous functions
904usually defined with @code{defun} and given names at the same time, it 902are valid wherever function names are. They are often assigned as
905is occasionally more concise to use an explicit lambda expression---an 903variable values, or as arguments to functions; for instance, you might
906anonymous function. Such a list is valid wherever a function name is. 904pass one as the @var{function} argument to @code{mapcar}, which
905applies that function to each element of a list (@pxref{Mapping
906Functions}). @xref{describe-symbols example}, for a realistic example
907of this.
908
909 When defining a lambda expression that is to be used as an anonymous
910function, you can in principle use any method to construct the list.
911But typically you should use the @code{lambda} macro, or the
912@code{function} special form, or the @code{#'} read syntax:
913
914@defmac lambda args body...
915This macro returns an anonymous function with argument list @var{args}
916and body forms given by @var{body}. In effect, this macro makes
917@code{lambda} forms ``self-quoting'': evaluating a form whose @sc{car}
918is @code{lambda} yields the form itself:
919
920@example
921(lambda (x) (* x x))
922 @result{} (lambda (x) (* x x))
923@end example
924
925The @code{lambda} form has one other effect: it tells the Emacs
926evaluator and byte-compiler that its argument is a function, by using
927@code{function} as a subroutine (see below).
928@end defmac
907 929
908 Any method of creating such a list makes a valid function. Even this: 930@defspec function function-object
931@cindex function quoting
932This special form returns @var{function-object} without evaluating it.
933In this, it is similar to @code{quote} (@pxref{Quoting}). But unlike
934@code{quote}, it also serves as a note to the Emacs evaluator and
935byte-compiler that @var{function-object} is intended to be used as a
936function. Assuming @var{function-object} is a valid lambda
937expression, this has two effects:
909 938
910@smallexample 939@itemize
911@group 940@item
912(setq silly (append '(lambda (x)) (list (list '+ (* 3 4) 'x)))) 941When the code is byte-compiled, @var{function-object} is compiled into
913@result{} (lambda (x) (+ 12 x)) 942a byte-code function object (@pxref{Byte Compilation}).
914@end group
915@end smallexample
916 943
917@noindent 944@item
918This computes a list that looks like @code{(lambda (x) (+ 12 x))} and 945When lexical binding is enabled, @var{function-object} is converted
919makes it the value (@emph{not} the function definition!) of 946into a closure. @xref{Closures}.
920@code{silly}. 947@end itemize
948@end defspec
921 949
922 Here is how we might call this function: 950@cindex @samp{#'} syntax
951The read syntax @code{#'} is a short-hand for using @code{function}.
952The following forms are all equivalent:
923 953
924@example 954@example
925@group 955(lambda (x) (* x x))
926(funcall silly 1) 956(function (lambda (x) (* x x)))
927@result{} 13 957#'(lambda (x) (* x x))
928@end group
929@end example 958@end example
930 959
931@noindent
932It does @emph{not} work to write @code{(silly 1)}, because this
933function is not the @emph{function definition} of @code{silly}. We
934have not given @code{silly} any function definition, just a value as a
935variable.
936
937 Most of the time, anonymous functions are constants that appear in
938your program. For instance, you might want to pass one as an argument
939to the function @code{mapcar}, which applies any given function to
940each element of a list (@pxref{Mapping Functions}).
941@xref{describe-symbols example}, for a realistic example of this.
942
943 In the following example, we define a @code{change-property} 960 In the following example, we define a @code{change-property}
944function that takes a function as its third argument, followed by a 961function that takes a function as its third argument, followed by a
945@code{double-property} function that makes use of 962@code{double-property} function that makes use of
@@ -959,15 +976,11 @@ function that takes a function as its third argument, followed by a
959@end example 976@end example
960 977
961@noindent 978@noindent
962In the @code{double-property} function, we did not quote the 979Note that we do not quote the @code{lambda} form.
963@code{lambda} form. This is permissible, because a @code{lambda} form
964is @dfn{self-quoting}: evaluating the form yields the form itself.
965 980
966Whether or not you quote a @code{lambda} form makes a difference if 981 If you compile the above code, the anonymous function is also
967you compile the code (@pxref{Byte Compilation}). If the @code{lambda} 982compiled. This would not happen if, say, you had constructed the
968form is unquoted, as in the above example, the anonymous function is 983anonymous function by quoting it as a list:
969also compiled. Suppose, however, that we quoted the @code{lambda}
970form:
971 984
972@example 985@example
973@group 986@group
@@ -977,52 +990,10 @@ form:
977@end example 990@end example
978 991
979@noindent 992@noindent
980If you compile this, the argument passed to @code{change-property} is 993In that case, the anonymous function is kept as a lambda expression in
981the precise list shown: 994the compiled code. The byte-compiler cannot assume this list is a
982 995function, even though it looks like one, since it does not know that
983@example 996@code{change-property} intends to use it as a function.
984(lambda (x) (* x 2))
985@end example
986
987@noindent
988The Lisp compiler cannot assume this list is a function, even though
989it looks like one, since it does not know what @code{change-property}
990will do with the list. Perhaps it will check whether the @sc{car} of
991the third element is the symbol @code{*}!
992
993@findex function
994The @code{function} special form explicitly tells the byte-compiler
995that its argument is a function:
996
997@defspec function function-object
998@cindex function quoting
999This special form returns @var{function-object} without evaluating it.
1000In this, it is equivalent to @code{quote}. However, it serves as a
1001note to the Emacs Lisp compiler that @var{function-object} is intended
1002to be used only as a function, and therefore can safely be compiled.
1003Contrast this with @code{quote}, in @ref{Quoting}.
1004@end defspec
1005
1006@cindex @samp{#'} syntax
1007The read syntax @code{#'} is a short-hand for using @code{function}.
1008Generally, it is not necessary to use either @code{#'} or
1009@code{function}; just use an unquoted @code{lambda} form instead.
1010(Actually, @code{lambda} is a macro defined using @code{function}.)
1011The following forms are all equivalent:
1012
1013@example
1014#'(lambda (x) (* x x))
1015(function (lambda (x) (* x x)))
1016(lambda (x) (* x x))
1017@end example
1018
1019 We sometimes write @code{function} instead of @code{quote} when
1020quoting the name of a function, but this usage is just a sort of
1021comment:
1022
1023@example
1024(function @var{symbol}) @equiv{} (quote @var{symbol}) @equiv{} '@var{symbol}
1025@end example
1026 997
1027@node Function Cells 998@node Function Cells
1028@section Accessing Function Cell Contents 999@section Accessing Function Cell Contents
@@ -1118,77 +1089,60 @@ This function stores @var{definition} in the function cell of
1118this is not checked. The argument @var{symbol} is an ordinary evaluated 1089this is not checked. The argument @var{symbol} is an ordinary evaluated
1119argument. 1090argument.
1120 1091
1121There are three normal uses of this function: 1092The primary use of this function is as a subroutine by constructs that
1093define or alter functions, like @code{defadvice} (@pxref{Advising
1094Functions}). (If @code{defun} were not a primitive, it could be
1095written as a Lisp macro using @code{fset}.) You can also use it to
1096give a symbol a function definition that is not a list, e.g.@: a
1097keyboard macro (@pxref{Keyboard Macros}):
1122 1098
1123@itemize @bullet 1099@example
1124@item 1100;; @r{Define a named keyboard macro.}
1125Copying one symbol's function definition to another---in other words, 1101(fset 'kill-two-lines "\^u2\^k")
1126making an alternate name for a function. (If you think of this as the 1102 @result{} "\^u2\^k"
1127definition of the new name, you should use @code{defalias} instead of 1103@end example
1128@code{fset}; see @ref{Definition of defalias}.)
1129 1104
1130@item 1105It you wish to use @code{fset} to make an alternate name for a
1131Giving a symbol a function definition that is not a list and therefore 1106function, consider using @code{defalias} instead. @xref{Definition of
1132cannot be made with @code{defun}. For example, you can use @code{fset} 1107defalias}.
1133to give a symbol @code{s1} a function definition which is another symbol 1108@end defun
1134@code{s2}; then @code{s1} serves as an alias for whatever definition
1135@code{s2} presently has. (Once again use @code{defalias} instead of
1136@code{fset} if you think of this as the definition of @code{s1}.)
1137 1109
1138@item 1110@node Closures
1139In constructs for defining or altering functions. If @code{defun} 1111@section Closures
1140were not a primitive, it could be written in Lisp (as a macro) using
1141@code{fset}.
1142@end itemize
1143 1112
1144Here are examples of these uses: 1113 As explained in @ref{Variable Scoping}, Emacs can optionally enable
1114lexical binding of variables. When lexical binding is enabled, any
1115named function that you create (e.g.@: with @code{defun}), as well as
1116any anonymous function that you create using the @code{lambda} macro
1117or the @code{function} special form or the @code{#'} syntax
1118(@pxref{Anonymous Functions}), is automatically converted into a
1119closure.
1145 1120
1146@example 1121 A closure is a function that also carries a record of the lexical
1147@group 1122environment that existed when the function was defined. When it is
1148;; @r{Save @code{foo}'s definition in @code{old-foo}.} 1123invoked, any lexical variable references within its definition use the
1149(fset 'old-foo (symbol-function 'foo)) 1124retained lexical environment. In all other respects, closures behave
1150@end group 1125much like ordinary functions; in particular, they can be called in the
1126same way as ordinary functions.
1151 1127
1152@group 1128 @xref{Lexical Binding}, for an example of using a closure.
1153;; @r{Make the symbol @code{car} the function definition of @code{xfirst}.}
1154;; @r{(Most likely, @code{defalias} would be better than @code{fset} here.)}
1155(fset 'xfirst 'car)
1156 @result{} car
1157@end group
1158@group
1159(xfirst '(1 2 3))
1160 @result{} 1
1161@end group
1162@group
1163(symbol-function 'xfirst)
1164 @result{} car
1165@end group
1166@group
1167(symbol-function (symbol-function 'xfirst))
1168 @result{} #<subr car>
1169@end group
1170 1129
1171@group 1130 Currently, an Emacs Lisp closure object is represented by a list
1172;; @r{Define a named keyboard macro.} 1131with the symbol @code{closure} as the first element, a list
1173(fset 'kill-two-lines "\^u2\^k") 1132representing the lexical environment as the second element, and the
1174 @result{} "\^u2\^k" 1133argument list and body forms as the remaining elements:
1175@end group
1176 1134
1177@group 1135@example
1178;; @r{Here is a function that alters other functions.} 1136;; @r{lexical binding is enabled.}
1179(defun copy-function-definition (new old) 1137(lambda (x) (* x x))
1180 "Define NEW with the same function definition as OLD." 1138 @result{} (closure (t) (x) (* x x))
1181 (fset new (symbol-function old)))
1182@end group
1183@end example 1139@end example
1184@end defun
1185 1140
1186 @code{fset} is sometimes used to save the old definition of a 1141@noindent
1187function before redefining it. That permits the new definition to 1142However, the fact that the internal structure of a closure is
1188invoke the old definition. But it is unmodular and unclean for a Lisp 1143``exposed'' to the rest of the Lisp world is considered an internal
1189file to redefine a function defined elsewhere. If you want to modify 1144implementation detail. For this reason, we recommend against directly
1190a function defined by another package, it is cleaner to use 1145examining or altering the structure of closure objects.
1191@code{defadvice} (@pxref{Advising Functions}).
1192 1146
1193@node Obsolete Functions 1147@node Obsolete Functions
1194@section Declaring Functions Obsolete 1148@section Declaring Functions Obsolete
@@ -1254,41 +1208,46 @@ this:
1254@section Inline Functions 1208@section Inline Functions
1255@cindex inline functions 1209@cindex inline functions
1256 1210
1257@findex defsubst 1211@defmac defsubst name argument-list body-forms...
1258You can define an @dfn{inline function} by using @code{defsubst} instead 1212Define an inline function. The syntax is exactly the same as
1259of @code{defun}. An inline function works just like an ordinary 1213@code{defun} (@pxref{Defining Functions}).
1260function except for one thing: when you compile a call to the function, 1214@end defmac
1261the function's definition is open-coded into the caller. 1215
1216 You can define an @dfn{inline function} by using @code{defsubst}
1217instead of @code{defun}. An inline function works just like an
1218ordinary function except for one thing: when you byte-compile a call
1219to the function (@pxref{Byte Compilation}), the function's definition
1220is expanded into the caller.
1262 1221
1263Making a function inline makes explicit calls run faster. But it also 1222 Making a function inline often makes its function calls run faster.
1264has disadvantages. For one thing, it reduces flexibility; if you 1223But it also has disadvantages. For one thing, it reduces flexibility;
1265change the definition of the function, calls already inlined still use 1224if you change the definition of the function, calls already inlined
1266the old definition until you recompile them. 1225still use the old definition until you recompile them.
1267 1226
1268Another disadvantage is that making a large function inline can increase 1227 Another disadvantage is that making a large function inline can
1269the size of compiled code both in files and in memory. Since the speed 1228increase the size of compiled code both in files and in memory. Since
1270advantage of inline functions is greatest for small functions, you 1229the speed advantage of inline functions is greatest for small
1271generally should not make large functions inline. 1230functions, you generally should not make large functions inline.
1272 1231
1273Also, inline functions do not behave well with respect to debugging, 1232 Also, inline functions do not behave well with respect to debugging,
1274tracing, and advising (@pxref{Advising Functions}). Since ease of 1233tracing, and advising (@pxref{Advising Functions}). Since ease of
1275debugging and the flexibility of redefining functions are important 1234debugging and the flexibility of redefining functions are important
1276features of Emacs, you should not make a function inline, even if it's 1235features of Emacs, you should not make a function inline, even if it's
1277small, unless its speed is really crucial, and you've timed the code 1236small, unless its speed is really crucial, and you've timed the code
1278to verify that using @code{defun} actually has performance problems. 1237to verify that using @code{defun} actually has performance problems.
1279 1238
1280It's possible to define a macro to expand into the same code that an 1239 It's possible to define a macro to expand into the same code that an
1281inline function would execute. (@xref{Macros}.) But the macro would be 1240inline function would execute (@pxref{Macros}). But the macro would
1282limited to direct use in expressions---a macro cannot be called with 1241be limited to direct use in expressions---a macro cannot be called
1283@code{apply}, @code{mapcar} and so on. Also, it takes some work to 1242with @code{apply}, @code{mapcar} and so on. Also, it takes some work
1284convert an ordinary function into a macro. To convert it into an inline 1243to convert an ordinary function into a macro. To convert it into an
1285function is very easy; simply replace @code{defun} with @code{defsubst}. 1244inline function is easy; just replace @code{defun} with
1286Since each argument of an inline function is evaluated exactly once, you 1245@code{defsubst}. Since each argument of an inline function is
1287needn't worry about how many times the body uses the arguments, as you 1246evaluated exactly once, you needn't worry about how many times the
1288do for macros. (@xref{Argument Evaluation}.) 1247body uses the arguments, as you do for macros.
1289 1248
1290Inline functions can be used and open-coded later on in the same file, 1249 After an inline function is defined, its inline expansion can be
1291following the definition, just like macros. 1250performed later on in the same file, just like macros.
1292 1251
1293@node Declaring Functions 1252@node Declaring Functions
1294@section Telling the Compiler that a Function is Defined 1253@section Telling the Compiler that a Function is Defined
@@ -1352,12 +1311,10 @@ definition using @code{locate-library}; if that finds no file, they
1352expand the definition file name relative to the directory of the file 1311expand the definition file name relative to the directory of the file
1353that contains the @code{declare-function} call. 1312that contains the @code{declare-function} call.
1354 1313
1355 You can also say that a function is defined by C code by specifying a 1314 You can also say that a function is a primitive by specifying a file
1356file name ending in @samp{.c} or @samp{.m}. @code{check-declare-file} 1315name ending in @samp{.c} or @samp{.m}. This is useful only when you
1357looks for these files in the C source code directory. This is useful 1316call a primitive that is defined only on certain systems. Most
1358only when you call a function that is defined only on certain systems. 1317primitives are always defined, so they will never give you a warning.
1359Most of the primitive functions of Emacs are always defined so they will
1360never give you a warning.
1361 1318
1362 Sometimes a file will optionally use functions from an external package. 1319 Sometimes a file will optionally use functions from an external package.
1363If you prefix the filename in the @code{declare-function} statement with 1320If you prefix the filename in the @code{declare-function} statement with
diff --git a/doc/lispref/variables.texi b/doc/lispref/variables.texi
index 9e0c439e57e..bdb16cd10a8 100644
--- a/doc/lispref/variables.texi
+++ b/doc/lispref/variables.texi
@@ -9,16 +9,13 @@
9 9
10 A @dfn{variable} is a name used in a program to stand for a value. 10 A @dfn{variable} is a name used in a program to stand for a value.
11In Lisp, each variable is represented by a Lisp symbol 11In Lisp, each variable is represented by a Lisp symbol
12(@pxref{Symbols}). The symbol's name serves as the variable name, and 12(@pxref{Symbols}). The variable name is simply the symbol's name, and
13the symbol's value cell holds the variable's value@footnote{Strictly 13the variable's value is stored in the symbol's value cell@footnote{To
14speaking, the symbol's value cell always holds the variable's current 14be precise, under the default @dfn{dynamic binding} rules the value
15value under the default @dfn{dynamic binding} rules. Under 15cell always holds the variable's current value, but this is not the
16@dfn{lexical binding} rules, the value cell holds the variable's 16case under @dfn{lexical binding} rules. @xref{Variable Scoping}, for
17@dfn{global value}. @xref{Variable Scoping}, for details.}. 17details.}. @xref{Symbol Components}. In Emacs Lisp, the use of a
18@xref{Symbol Components}. 18symbol as a variable is independent of its use as a function name.
19
20 In Emacs Lisp, the use of a symbol as a variable is independent of
21its use as a function name.
22 19
23 As previously noted in this manual, a Lisp program is represented 20 As previously noted in this manual, a Lisp program is represented
24primarily by Lisp objects, and only secondarily as text. The textual 21primarily by Lisp objects, and only secondarily as text. The textual
@@ -151,8 +148,8 @@ does not raise an error if you actually change it.
151with new values. Sometimes it is useful to give a variable a 148with new values. Sometimes it is useful to give a variable a
152@dfn{local value}---a value that takes effect only within a certain 149@dfn{local value}---a value that takes effect only within a certain
153part of a Lisp program. When a variable has a local value, we say 150part of a Lisp program. When a variable has a local value, we say
154that it has a @dfn{local binding}, or that it is a @dfn{local 151that it is @dfn{locally bound} to that value, and that it is a
155variable}. 152@dfn{local variable}.
156 153
157 For example, when a function is called, its argument variables 154 For example, when a function is called, its argument variables
158receive local values, which are the actual arguments supplied to the 155receive local values, which are the actual arguments supplied to the
@@ -186,10 +183,10 @@ local binding. To be more precise, a rule called the @dfn{scoping
186rule} determines where in a program a local binding takes effect. The 183rule} determines where in a program a local binding takes effect. The
187default scoping rule in Emacs Lisp is called @dfn{dynamic scoping}, 184default scoping rule in Emacs Lisp is called @dfn{dynamic scoping},
188which simply states that the current binding at any given point in the 185which simply states that the current binding at any given point in the
189execution of a program is the most recently-created local binding for 186execution of a program is the most recently-created binding for that
190that variable that still exists. For details about dynamic scoping, 187variable that still exists. For details about dynamic scoping, and an
191and an alternative scoping rule called @dfn{lexical scoping}, 188alternative scoping rule called @dfn{lexical scoping}, @xref{Variable
192@xref{Variable Scoping}. 189Scoping}.
193 190
194 The special forms @code{let} and @code{let*} exist to create local 191 The special forms @code{let} and @code{let*} exist to create local
195bindings: 192bindings:
@@ -294,27 +291,27 @@ has room to execute.
294@cindex @code{void-variable} error 291@cindex @code{void-variable} error
295@cindex void variable 292@cindex void variable
296 293
297 If you have never given a symbol any value as a global variable, we 294 We say that a variable is void if its symbol has an unassigned value
298say that that symbol's global value is @dfn{void}. Note that this 295cell (@pxref{Symbol Components}). Under Emacs Lisp's default dynamic
299does @emph{not} mean the value is @code{nil}. The symbol @code{nil} 296binding rules (@pxref{Variable Scoping}), the value cell stores the
300is a Lisp object and can be the value of a variable, just as any other 297variable's current (local or global) value. Note that an unassigned
301object can be; but it is still a value. 298value cell is @emph{not} the same as having @code{nil} in the value
302 299cell. The symbol @code{nil} is a Lisp object and can be the value of
303 More precisely, a variable is void if its symbol has an unassigned 300a variable, just as any other object can be; but it is still a value.
304value cell (@pxref{Symbol Components}). Under Emacs Lisp's default 301If a variable is void, trying to evaluate the variable signals a
305dynamic binding rules, the value cell stores the variable's current 302@code{void-variable} error rather than a value.
306(local or global) value; if a variable is void, trying to evaluate the 303
307variable signals a @code{void-variable} error rather than a value. 304 Under lexical binding rules, the value cell only holds the
308But when a variable is lexically bound, it can have a local value 305variable's global value, i.e.@: the value outside of any lexical
309which is determined by the lexical environment, even if the value cell 306binding contruct. When a variable is lexically bound, the local value
310is empty and the variable is technically void. @xref{Variable 307is determined by the lexical environment; the variable may have a
311Scoping}. 308local value if its symbol's value cell is unassigned.
312 309
313@defun makunbound symbol 310@defun makunbound symbol
314This function empties out the value cell of @var{symbol}, making the 311This function empties out the value cell of @var{symbol}, making the
315variable void. It returns @var{symbol}. 312variable void. It returns @var{symbol}.
316 313
317If @var{symbol} has a (dynamic) local binding, @code{makunbound} voids 314If @var{symbol} has a dynamic local binding, @code{makunbound} voids
318the current binding, and this voidness lasts only as long as the local 315the current binding, and this voidness lasts only as long as the local
319binding is in effect. Afterwards, the previously shadowed local or 316binding is in effect. Afterwards, the previously shadowed local or
320global binding is reexposed; then the variable will no longer be void, 317global binding is reexposed; then the variable will no longer be void,
@@ -615,15 +612,15 @@ name in the text of the program. You can use the @code{symbol-value}
615function to extract the value. 612function to extract the value.
616 613
617@defun symbol-value symbol 614@defun symbol-value symbol
618This function returns the value of @var{symbol}. This is the value in 615This function returns the value stored in @var{symbol}'s value cell.
619the symbol's value cell, which is where the variable's current 616This is where the variable's current (dynamic) value is stored. If
620(dynamic) value is stored. If the variable has no local binding, this 617the variable has no local binding, this is simply its global value.
621is simply its global value. 618If the variable is void, a @code{void-variable} error is signaled.
622 619
623If the variable is lexically bound, the value reported by 620If the variable is lexically bound, the value reported by
624@code{symbol-value} is the dynamic value, and not the local lexical 621@code{symbol-value} is not necessarily the same as the variable's
625value (which is determined by the lexical environment rather than the 622lexical value, which is determined by the lexical environment rather
626symbol's value cell). @xref{Variable Scoping}. 623than the symbol's value cell. @xref{Variable Scoping}.
627 624
628@example 625@example
629@group 626@group
@@ -657,9 +654,6 @@ symbol's value cell). @xref{Variable Scoping}.
657 @result{} 5 654 @result{} 5
658@end group 655@end group
659@end example 656@end example
660
661A @code{void-variable} error is signaled if @var{symbol} is void as a
662variable.
663@end defun 657@end defun
664 658
665@node Setting Variables 659@node Setting Variables
@@ -945,13 +939,13 @@ construct.
945 939
946@example 940@example
947@group 941@group
948(defun getx ()
949 x) ; @r{@code{x} is used ``free'' in this function.}
950
951(let ((x 1)) ; @r{@code{x} is lexically bound.} 942(let ((x 1)) ; @r{@code{x} is lexically bound.}
952 (+ x 3)) 943 (+ x 3))
953 @result{} 4 944 @result{} 4
954 945
946(defun getx ()
947 x) ; @r{@code{x} is used ``free'' in this function.}
948
955(let ((x 1)) ; @r{@code{x} is lexically bound.} 949(let ((x 1)) ; @r{@code{x} is lexically bound.}
956 (getx)) 950 (getx))
957@error{} Symbol's value as variable is void: x 951@error{} Symbol's value as variable is void: x
@@ -972,20 +966,18 @@ itself.
972within the construct and their local values. When the Lisp evaluator 966within the construct and their local values. When the Lisp evaluator
973wants the current value of a variable, it looks first in the lexical 967wants the current value of a variable, it looks first in the lexical
974environment; if the variable is not specified in there, it looks in 968environment; if the variable is not specified in there, it looks in
975the symbol's value cell, where the dynamical value is stored. 969the symbol's value cell, where the dynamic value is stored.
976 970
977@cindex closures 971@cindex closures
978 Lexical bindings have indefinite extent. Even after a binding 972 Lexical bindings have indefinite extent. Even after a binding
979construct has finished executing, its lexical environment can be 973construct has finished executing, its lexical environment can be
980``kept around'' in Lisp objects called @dfn{closures}. A closure is 974``kept around'' in Lisp objects called @dfn{closures}. A closure is
981created whenever you evaluate a lambda expression (@pxref{Lambda 975created when you create a named or anonymous function with lexical
982Expressions}) with lexical binding enabled. It is represented by a 976binding enabled. @xref{Closures}, for details.
983list whose @sc{car} is the symbol @code{closure}. It is a function,
984in the sense that it can be passed as an argument to @code{funcall};
985when called as a function, any lexical variable references within its
986definition will use the retained lexical environment.
987 977
988 Here is an example which illustrates the use of a closure: 978 When a closure is called as a function, any lexical variable
979references within its definition use the retained lexical environment.
980Here is an example:
989 981
990@example 982@example
991(defvar my-ticker nil) ; @r{We will use this dynamically bound} 983(defvar my-ticker nil) ; @r{We will use this dynamically bound}
@@ -1199,7 +1191,8 @@ foo @result{} 'a
1199@end group 1191@end group
1200@end example 1192@end example
1201 1193
1202 Note that references to @code{foo} in @var{body} access the 1194@noindent
1195Note that references to @code{foo} in @var{body} access the
1203buffer-local binding of buffer @samp{b}. 1196buffer-local binding of buffer @samp{b}.
1204 1197
1205 When a file specifies local variable values, these become buffer-local 1198 When a file specifies local variable values, these become buffer-local
@@ -1642,6 +1635,11 @@ For boolean-valued variables that are safe, use @code{booleanp} as the
1642property value. Lambda expressions should be quoted so that 1635property value. Lambda expressions should be quoted so that
1643@code{describe-variable} can display the predicate. 1636@code{describe-variable} can display the predicate.
1644 1637
1638 When defining a user option using @code{defcustom}, you can set its
1639@code{safe-local-variable} property by adding the arguments
1640@code{:safe @var{function}} to @code{defcustom} (@pxref{Variable
1641Definitions}).
1642
1645@defopt safe-local-variable-values 1643@defopt safe-local-variable-values
1646This variable provides another way to mark some variable values as 1644This variable provides another way to mark some variable values as
1647safe. It is a list of cons cells @code{(@var{var} . @var{val})}, 1645safe. It is a list of cons cells @code{(@var{var} . @var{val})},
@@ -1661,28 +1659,31 @@ the value @var{val}, based on the above criteria.
1661@end defun 1659@end defun
1662 1660
1663@c @cindex risky local variable Duplicates risky-local-variable 1661@c @cindex risky local variable Duplicates risky-local-variable
1664 Some variables are considered @dfn{risky}. A variable whose name 1662 Some variables are considered @dfn{risky}. If a variable is risky,
1665ends in any of @samp{-command}, @samp{-frame-alist}, @samp{-function}, 1663it is never entered automatically into
1664@code{safe-local-variable-values}; Emacs always queries before setting
1665a risky variable, unless the user explicitly allows a value by
1666customizing @code{safe-local-variable-values} directly.
1667
1668 Any variable whose name has a non-@code{nil}
1669@code{risky-local-variable} property is considered risky. When you
1670define a user option using @code{defcustom}, you can set its
1671@code{risky-local-variable} property by adding the arguments
1672@code{:risky @var{value}} to @code{defcustom} (@pxref{Variable
1673Definitions}). In addition, any variable whose name ends in any of
1674@samp{-command}, @samp{-frame-alist}, @samp{-function},
1666@samp{-functions}, @samp{-hook}, @samp{-hooks}, @samp{-form}, 1675@samp{-functions}, @samp{-hook}, @samp{-hooks}, @samp{-form},
1667@samp{-forms}, @samp{-map}, @samp{-map-alist}, @samp{-mode-alist}, 1676@samp{-forms}, @samp{-map}, @samp{-map-alist}, @samp{-mode-alist},
1668@samp{-program}, or @samp{-predicate} is considered risky. The 1677@samp{-program}, or @samp{-predicate} is automatically considered
1669variables @samp{font-lock-keywords}, @samp{font-lock-keywords} 1678risky. The variables @samp{font-lock-keywords},
1670followed by a digit, and @samp{font-lock-syntactic-keywords} are also 1679@samp{font-lock-keywords} followed by a digit, and
1671considered risky. Finally, any variable whose name has a 1680@samp{font-lock-syntactic-keywords} are also considered risky.
1672non-@code{nil} @code{risky-local-variable} property is considered
1673risky.
1674 1681
1675@defun risky-local-variable-p sym 1682@defun risky-local-variable-p sym
1676This function returns non-@code{nil} if @var{sym} is a risky variable, 1683This function returns non-@code{nil} if @var{sym} is a risky variable,
1677based on the above criteria. 1684based on the above criteria.
1678@end defun 1685@end defun
1679 1686
1680 If a variable is risky, it will not be entered automatically into
1681@code{safe-local-variable-values} as described above. Therefore,
1682Emacs will always query before setting a risky variable, unless the
1683user explicitly allows the setting by customizing
1684@code{safe-local-variable-values} directly.
1685
1686@defvar ignored-local-variables 1687@defvar ignored-local-variables
1687This variable holds a list of variables that should not be given local 1688This variable holds a list of variables that should not be given local
1688values by files. Any value specified for one of these variables is 1689values by files. Any value specified for one of these variables is
diff --git a/doc/lispref/vol1.texi b/doc/lispref/vol1.texi
index 3a2f4723b05..4a48536f5f3 100644
--- a/doc/lispref/vol1.texi
+++ b/doc/lispref/vol1.texi
@@ -478,6 +478,7 @@ Functions
478* Anonymous Functions:: Lambda expressions are functions with no names. 478* Anonymous Functions:: Lambda expressions are functions with no names.
479* Function Cells:: Accessing or setting the function definition 479* Function Cells:: Accessing or setting the function definition
480 of a symbol. 480 of a symbol.
481* Closures:: Functions that enclose a lexical environment.
481* Obsolete Functions:: Declaring functions obsolete. 482* Obsolete Functions:: Declaring functions obsolete.
482* Inline Functions:: Defining functions that the compiler 483* Inline Functions:: Defining functions that the compiler
483 will open code. 484 will open code.
diff --git a/doc/lispref/vol2.texi b/doc/lispref/vol2.texi
index 41558549da5..961b3225823 100644
--- a/doc/lispref/vol2.texi
+++ b/doc/lispref/vol2.texi
@@ -477,6 +477,7 @@ Functions
477* Anonymous Functions:: Lambda expressions are functions with no names. 477* Anonymous Functions:: Lambda expressions are functions with no names.
478* Function Cells:: Accessing or setting the function definition 478* Function Cells:: Accessing or setting the function definition
479 of a symbol. 479 of a symbol.
480* Closures:: Functions that enclose a lexical environment.
480* Obsolete Functions:: Declaring functions obsolete. 481* Obsolete Functions:: Declaring functions obsolete.
481* Inline Functions:: Defining functions that the compiler 482* Inline Functions:: Defining functions that the compiler
482 will open code. 483 will open code.
diff --git a/etc/NEWS b/etc/NEWS
index 6dc0756e880..0a401eb3a3f 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -1049,19 +1049,20 @@ sc.el, x-menu.el, rnews.el, rnewspost.el
1049 1049
1050* Lisp changes in Emacs 24.1 1050* Lisp changes in Emacs 24.1
1051 1051
1052+++
1052** Code can now use lexical scoping by default instead of dynamic scoping. 1053** Code can now use lexical scoping by default instead of dynamic scoping.
1053The `lexical-binding' variable lets code use lexical scoping for local 1054The `lexical-binding' variable lets code use lexical scoping for local
1054variables. It is typically set via a file-local variable in the first 1055variables. It is typically set via a file-local variable in the first
1055line of the file, in which case it applies to all the code in that file. 1056line of the file, in which case it applies to all the code in that file.
1056 1057+++
1057*** `eval' takes a new optional argument `lexical' to choose the new lexical 1058*** `eval' takes a new optional argument `lexical' to choose the new lexical
1058binding instead of the old dynamic binding mode. 1059binding instead of the old dynamic binding mode.
1059 1060+++
1060*** Lexically scoped interpreted functions are represented with a new form 1061*** Lexically scoped interpreted functions are represented with a new form
1061of function value which looks like (closure ENV ARGS &rest BODY). 1062of function value which looks like (closure ENV ARGS &rest BODY).
1062 1063
1063*** New macro `letrec' to define recursive local functions. 1064*** New macro `letrec' to define recursive local functions.
1064 1065+++
1065*** New function `special-variable-p' to check whether a variable is 1066*** New function `special-variable-p' to check whether a variable is
1066declared as dynamically bound. 1067declared as dynamically bound.
1067 1068