diff options
| author | Chong Yidong | 2012-02-04 22:56:32 +0800 |
|---|---|---|
| committer | Chong Yidong | 2012-02-04 22:56:32 +0800 |
| commit | 735cc5ca6fe9b19aa9f69eb696baef2b8b4bd0fb (patch) | |
| tree | cf9722413f79647d3c302142d8e9cc95bdfa13af | |
| parent | d7f29f8e5c589e4676b2ab53fea7a8b4cfcd4be3 (diff) | |
| download | emacs-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-RELEASE | 6 | ||||
| -rw-r--r-- | doc/lispref/ChangeLog | 23 | ||||
| -rw-r--r-- | doc/lispref/elisp.texi | 1 | ||||
| -rw-r--r-- | doc/lispref/functions.texi | 671 | ||||
| -rw-r--r-- | doc/lispref/variables.texi | 137 | ||||
| -rw-r--r-- | doc/lispref/vol1.texi | 1 | ||||
| -rw-r--r-- | doc/lispref/vol2.texi | 1 | ||||
| -rw-r--r-- | etc/NEWS | 7 |
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 | |||
| 111 | TUTORIAL.it | 111 | TUTORIAL.it |
| 112 | TUTORIAL.ja | 112 | TUTORIAL.ja |
| 113 | TUTORIAL.ko | 113 | TUTORIAL.ko |
| 114 | TUTORIAL.nl | 114 | TUTORIAL.nl Pieter Schoenmakers |
| 115 | TUTORIAL.pl | 115 | TUTORIAL.pl |
| 116 | TUTORIAL.pt_BR | 116 | TUTORIAL.pt_BR |
| 117 | TUTORIAL.ro | 117 | TUTORIAL.ro |
| @@ -197,7 +197,7 @@ errors.texi | |||
| 197 | eval.texi cyd | 197 | eval.texi cyd |
| 198 | files.texi | 198 | files.texi |
| 199 | frames.texi | 199 | frames.texi |
| 200 | functions.texi | 200 | functions.texi cyd |
| 201 | hash.texi cyd | 201 | hash.texi cyd |
| 202 | help.texi | 202 | help.texi |
| 203 | hooks.texi | 203 | hooks.texi |
| @@ -228,7 +228,7 @@ symbols.texi cyd | |||
| 228 | syntax.texi | 228 | syntax.texi |
| 229 | text.texi | 229 | text.texi |
| 230 | tips.texi | 230 | tips.texi |
| 231 | variables.texi | 231 | variables.texi cyd |
| 232 | windows.texi | 232 | windows.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 @@ | |||
| 1 | 2012-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 | |||
| 1 | 2012-02-04 Glenn Morris <rgm@gnu.org> | 24 | 2012-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 |
| 36 | given several values called @dfn{arguments}. The result of the | 37 | @cindex value of function |
| 37 | computation is called the value of the function. The computation can | 38 | @cindex argument |
| 38 | also 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 |
| 39 | the contents of data structures. | 40 | computation given input values called @dfn{arguments}. The result of |
| 40 | 41 | the 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 | 42 | function. The computation can also have side effects, such as lasting |
| 42 | function-like objects. | 43 | changes 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, | ||
| 46 | a function in the strictest sense has no name: it is an object which | ||
| 47 | can @emph{optionally} be associated with a symbol (e.g.@: @code{car}) | ||
| 48 | that serves as the function name. @xref{Function Names}. When a | ||
| 49 | function has been given a name, we usually also refer to that symbol | ||
| 50 | as a ``function'' (e.g.@: we refer to ``the function @code{car}''). | ||
| 51 | In this manual, the distinction between a function name and the | ||
| 52 | function object itself is usually unimportant, but we will take note | ||
| 53 | wherever it is relevant. | ||
| 54 | |||
| 55 | Certain function-like objects, called @dfn{special forms} and | ||
| 56 | @dfn{macros}, also accept arguments to carry out computations. | ||
| 57 | However, as explained below, these are not considered functions in | ||
| 58 | Emacs 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 | 64 | A function (in the strict sense, i.e.@: a function object) which is |
| 47 | In Emacs Lisp, a @dfn{function} is anything that can be applied to | 65 | written in Lisp. These are described in the following section. |
| 48 | arguments in a Lisp program. In some cases, we use it more | 66 | @ifnottex |
| 49 | specifically to mean a function written in Lisp. Special forms and | 67 | @xref{Lambda Expressions}. |
| 50 | macros 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 |
| 56 | A @dfn{primitive} is a function callable from Lisp that is written in C, | 74 | A function which is callable from Lisp but is actually written in C. |
| 57 | such as @code{car} or @code{append}. These functions are also called | 75 | Primitives are also called @dfn{built-in functions}, or @dfn{subrs}. |
| 58 | @dfn{built-in functions}, or @dfn{subrs}. (Special forms are also | 76 | Examples include functions like @code{car} and @code{append}. In |
| 59 | considered primitives.) | 77 | addition, all special forms (see below) are also considered |
| 60 | 78 | primitives. | |
| 61 | Usually the reason we implement a function as a primitive is either | 79 | |
| 62 | because it is fundamental, because it provides a low-level interface | 80 | Usually, a function is implemented as a primitive because it is a |
| 63 | to operating system services, or because it needs to run fast. | 81 | fundamental part of Lisp (e.g.@: @code{car}), or because it provides a |
| 64 | Primitives can be modified or added only by changing the C sources and | 82 | low-level interface to operating system services, or because it needs |
| 65 | recompiling the editor. See @ref{Writing Emacs Primitives}. | 83 | to run fast. Unlike functions defined in Lisp, primitives can be |
| 66 | 84 | modified or added only by changing the C sources and recompiling | |
| 67 | @item lambda expression | 85 | Emacs. See @ref{Writing Emacs Primitives}. |
| 68 | A @dfn{lambda expression} is a function written in Lisp. | ||
| 69 | These 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 |
| 75 | A @dfn{special form} is a primitive that is like a function but does not | 88 | A primitive that is like a function but does not evaluate all of its |
| 76 | evaluate all of its arguments in the usual way. It may evaluate only | 89 | arguments in the usual way. It may evaluate only some of the |
| 77 | some of the arguments, or may evaluate them in an unusual order, or | 90 | arguments, or may evaluate them in an unusual order, or several times. |
| 78 | several times. Many special forms are described in @ref{Control | 91 | Examples include @code{if}, @code{and}, and @code{while}. |
| 79 | Structures}. | 92 | @xref{Special Forms}. |
| 80 | 93 | ||
| 81 | @item macro | 94 | @item macro |
| 82 | @cindex macro | 95 | @cindex macro |
| 83 | A @dfn{macro} is a construct defined in Lisp by the programmer. It | 96 | A construct defined in Lisp, which differs from a function in that it |
| 84 | differs from a function in that it translates a Lisp expression that you | 97 | translates a Lisp expression into another expression which is to be |
| 85 | write into an equivalent expression to be evaluated instead of the | 98 | evaluated instead of the original expression. Macros enable Lisp |
| 86 | original expression. Macros enable Lisp programmers to do the sorts of | 99 | programmers to do the sorts of things that special forms can do. |
| 87 | things that special forms can do. @xref{Macros}, for how to define and | 100 | @xref{Macros}. |
| 88 | use macros. | ||
| 89 | 101 | ||
| 90 | @item command | 102 | @item command |
| 91 | @cindex command | 103 | @cindex command |
| 92 | A @dfn{command} is an object that @code{command-execute} can invoke; it | 104 | An object which can be invoked via the @code{command-execute} |
| 93 | is a possible definition for a key sequence. Some functions are | 105 | primitive, usually due to the user typing in a key sequence |
| 94 | commands; a function written in Lisp is a command if it contains an | 106 | @dfn{bound} to that command. @xref{Interactive Call}. A command is |
| 95 | interactive declaration (@pxref{Defining Commands}). Such a function | 107 | usually a function; if the function is written in Lisp, it is made |
| 96 | can be called from Lisp expressions like other functions; in this case, | 108 | into a command by an @code{interactive} form in the function |
| 97 | the fact that the function is a command makes no difference. | 109 | definition (@pxref{Defining Commands}). Commands that are functions |
| 110 | can also be called from Lisp expressions, just like other functions. | ||
| 98 | 111 | ||
| 99 | Keyboard macros (strings and vectors) are commands also, even though | 112 | Keyboard macros (strings and vectors) are commands also, even though |
| 100 | they are not functions. A symbol is a command if its function | 113 | they are not functions. @xref{Keyboard Macros}. We say that a symbol |
| 101 | definition is a command; such symbols can be invoked with @kbd{M-x}. | 114 | is a command if its function cell contains a command (@pxref{Symbol |
| 102 | The symbol is a function as well if the definition is a function. | 115 | Components}); 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 | 119 | A function object that is much like a lambda expression, except that |
| 107 | A @dfn{keystroke command} is a command that is bound to a key sequence | 120 | it also encloses an ``environment'' of lexical variable bindings. |
| 108 | (typically one to three keystrokes). The distinction is made here | 121 | @xref{Closures}. |
| 109 | merely to avoid confusion with the meaning of ``command'' in non-Emacs | ||
| 110 | editors; for Lisp programs, the distinction is normally unimportant. | ||
| 111 | 122 | ||
| 112 | @item byte-code function | 123 | @item byte-code function |
| 113 | A @dfn{byte-code function} is a function that has been compiled by the | 124 | A function that has been compiled by the byte compiler. |
| 114 | byte 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 |
| 118 | An @dfn{autoload object} is a place-holder for a real function. If | 129 | A place-holder for a real function. If the autoload object is called, |
| 119 | the autoload object is called, it will make Emacs load the file | 130 | Emacs loads the file containing the definition of the real function, |
| 120 | containing the definition of the real function, and then call the real | 131 | and then calls the real function. @xref{Autoload}. |
| 121 | function instead. | ||
| 122 | @end table | 132 | @end table |
| 123 | 133 | ||
| 134 | You can use the function @code{functionp} to test if an object is a | ||
| 135 | function: | ||
| 136 | |||
| 124 | @defun functionp object | 137 | @defun functionp object |
| 125 | This function returns @code{t} if @var{object} is any kind of | 138 | This function returns @code{t} if @var{object} is any kind of |
| 126 | function, i.e.@: can be passed to @code{funcall}. Note that | 139 | function, 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, |
| 128 | Forms}). | 141 | and returns @code{nil} for special forms. |
| 129 | @end defun | 142 | @end defun |
| 130 | 143 | ||
| 131 | Unlike @code{functionp}, the next three functions do @emph{not} | 144 | @noindent |
| 132 | treat a symbol as its function definition. | 145 | Unlike @code{functionp}, the next three functions do @emph{not} treat |
| 146 | a symbol as its function definition. | ||
| 133 | 147 | ||
| 134 | @defun subrp object | 148 | @defun subrp object |
| 135 | This function returns @code{t} if @var{object} is a built-in function | 149 | This 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 |
| 190 | an 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 |
| 185 | Such a list is called a @dfn{lambda expression}. In Emacs Lisp, it | 199 | In Emacs Lisp, such a list is valid as an expression---it evaluates to |
| 186 | actually is valid as an expression---it evaluates to itself. In some | 200 | itself. But its main use is not to be evaluated as an expression, but |
| 187 | other Lisp dialects, a lambda expression is not a valid expression at | 201 | to be called as a function. |
| 188 | all. In either case, its main use is not to be evaluated as an | 202 | |
| 189 | expression, but to be called as a function. | 203 | A lambda expression, by itself, has no name; it is an @dfn{anonymous |
| 204 | function}. Although lambda expressions can be used this way | ||
| 205 | (@pxref{Anonymous Functions}), they are more commonly associated with | ||
| 206 | symbols to make @dfn{named functions} (@pxref{Function Names}). | ||
| 207 | Before going into these details, the following subsections describe | ||
| 208 | the 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 | ||
| 204 | looks 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, | |||
| 243 | function is the value returned by the last element of the body. | 258 | function 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 | |||
| 283 | 4)} from left to right. Then it applies the lambda expression to the | 298 | 4)} from left to right. Then it applies the lambda expression to the |
| 284 | argument values 1, 6 and 1 to produce the value 8. | 299 | argument 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 |
| 287 | a form in this way. You can get the same result, of making local | 302 | as its @sc{car} to make local variables and give them values. In the |
| 288 | variables and giving them values, using the special form @code{let} | 303 | old 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. | 304 | initialize local variables. But nowadays, it is clearer to use the |
| 290 | In practice, lambda expressions are either stored as the function | 305 | special form @code{let} for this purpose (@pxref{Local Variables}). |
| 291 | definitions of symbols, to produce named functions, or passed as | 306 | Lambda expressions are mainly used as anonymous functions for passing |
| 292 | arguments to other functions (@pxref{Anonymous Functions}). | 307 | as arguments to other functions (@pxref{Anonymous Functions}), or |
| 293 | 308 | stored as symbol function definitions to produce named functions | |
| 294 | However, calls to explicit lambda expressions were very useful in the | 309 | (@pxref{Function Names}). |
| 295 | old days of Lisp, before the special form @code{let} was invented. At | ||
| 296 | that time, they were the only way to bind and initialize local | ||
| 297 | variables. | ||
| 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} |
| 409 | after the lambda list. This string does not affect execution of the | 421 | just after the lambda list. This string does not affect execution of |
| 410 | function; it is a kind of comment, but a systematized comment which | 422 | the function; it is a kind of comment, but a systematized comment |
| 411 | actually appears inside the Lisp world and can be used by the Emacs help | 423 | which actually appears inside the Lisp world and can be used by the |
| 412 | facilities. @xref{Documentation}, for how the @var{documentation-string} is | 424 | Emacs help facilities. @xref{Documentation}, for how the |
| 413 | accessed. | 425 | documentation 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 |
| 416 | functions in your program, even those that are called only from within | 428 | functions 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 |
| 467 | function without a name is nonsensical. In Lisp, a function in the | 479 | symbol's @dfn{function cell} (@pxref{Symbol Components}) contains a |
| 468 | strictest sense has no name. It is simply a list whose first element is | 480 | function object (e.g.@: a lambda expression). Then the symbol itself |
| 469 | @code{lambda}, a byte-code function object, or a primitive subr-object. | 481 | becomes a valid, callable function, equivalent to the function object |
| 470 | 482 | in its function cell. | |
| 471 | However, a symbol can serve as the name of a function. This happens | 483 | |
| 472 | when 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 |
| 474 | callable function, equivalent to the list or subr-object that its | 486 | definition in place of the symbol is called @dfn{symbol function |
| 475 | function cell refers to. The contents of the function cell are also | 487 | indirection}; see @ref{Function Indirection}. If you have not given a |
| 476 | called the symbol's @dfn{function definition}. The procedure of using a | 488 | symbol a function definition, its function cell is said to be |
| 477 | symbol'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 | 492 | their names. You can create a named Lisp function by defining a |
| 481 | referred to through their names. For example, the symbol @code{car} works | 493 | lambda expression and putting it in a function cell (@pxref{Function |
| 482 | as a function and does what it does because the primitive subr-object | 494 | Cells}). However, it is more common to use the @code{defun} special |
| 483 | @code{#<subr car>} is stored in its function cell. | 495 | form, 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 |
| 486 | their names in Lisp expressions. For primitive subr-objects such as | 501 | their 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 | 502 | easily refer to itself---it can be recursive. Furthermore, primitives |
| 488 | is no read syntax for such objects. For functions written in Lisp, the | 503 | can only be referred to textually by their names, since primitive |
| 489 | name is more convenient to use in a call than an explicit lambda | 504 | function objects (@pxref{Primitive Function Type}) have no read |
| 490 | expression. Also, a function with a name can refer to itself---it can | 505 | syntax. |
| 491 | be recursive. Writing the function's name in its own definition is much | 506 | |
| 492 | more 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 |
| 494 | practice). | 509 | this is just a convention. It is easy to store it in several symbols |
| 495 | 510 | using @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 | 511 | same function. |
| 497 | example, we often speak of ``the function @code{car},'' not | 512 | |
| 498 | distinguishing between the symbol @code{car} and the primitive | 513 | Note that a symbol used as a function name may also be used as a |
| 499 | subr-object that is its function definition. For most purposes, the | 514 | variable; these two uses of a symbol are independent and do not |
| 500 | distinction is not important. | 515 | conflict. (This is not the case in some dialects of Lisp, like |
| 501 | 516 | Scheme.) | |
| 502 | Even so, keep in mind that a function need not have a unique name. While | ||
| 503 | a given function object @emph{usually} appears in the function cell of only | ||
| 504 | one symbol, this is just a matter of convenience. It is easy to store | ||
| 505 | it in several symbols using @code{fset}; then each of the symbols is | ||
| 506 | equally well a name for the same function. | ||
| 507 | |||
| 508 | A symbol used as a function name may also be used as a variable; these | ||
| 509 | two uses of a symbol are independent and do not conflict. (Some Lisp | ||
| 510 | dialects, such as Scheme, do not distinguish between a symbol's value | ||
| 511 | and its function definition; a symbol's value as a variable is also its | ||
| 512 | function definition.) If you have not given a symbol a function | ||
| 513 | definition, you cannot use it as a function; whether the symbol has a | ||
| 514 | value 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. | |||
| 521 | is called @dfn{defining a function}, and it is done with the | 523 | is 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 |
| 526 | defines the symbol @var{name} as a function that looks like this: | 528 | defines 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: | |||
| 534 | value. | 536 | value. |
| 535 | 537 | ||
| 536 | As described previously, @var{argument-list} is a list of argument | 538 | As described previously, @var{argument-list} is a list of argument |
| 537 | names and may include the keywords @code{&optional} and @code{&rest} | 539 | names and may include the keywords @code{&optional} and @code{&rest}. |
| 538 | (@pxref{Lambda Expressions}). Also, the first two of the | 540 | Also, the first two of the @var{body-forms} may be a documentation |
| 539 | @var{body-forms} may be a documentation string and an interactive | 541 | string and an interactive declaration. @xref{Lambda Components}. |
| 540 | declaration. | ||
| 541 | |||
| 542 | There is no conflict if the same symbol @var{name} is also used as a | ||
| 543 | variable, since the symbol's value cell is independent of the function | ||
| 544 | cell. @xref{Symbol Components}. | ||
| 545 | 542 | ||
| 546 | Here are some examples: | 543 | Here 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 | ||
| 588 | Be careful not to redefine existing functions unintentionally. | 585 | Be 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} |
| 590 | without any hesitation or notification. Redefining a function already | 587 | without any hesitation or notification. Emacs does not prevent you |
| 591 | defined is often done deliberately, and there is no way to distinguish | 588 | from doing this, because redefining a function is sometimes done |
| 592 | deliberate redefinition from unintentional redefinition. | 589 | deliberately, and there is no way to distinguish deliberate |
| 590 | redefinition 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 | |||
| 626 | definition will have no effect on them. | 624 | definition 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} |
| 629 | and tells the Lisp compiler to open-code it. @xref{Inline Functions}. | 627 | and 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. | |||
| 790 | This function ignores any arguments and returns @code{nil}. | 789 | This 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 |
| 794 | command is a function that has an @dfn{interactive} specification. | 793 | interactively (usually by a key sequence). It is possible to invoke |
| 795 | You may want to call these functions as if they were called | 794 | such a command exactly as though it was called interactively, by using |
| 796 | interactively. See @ref{Interactive Call} for details on how to do | 795 | the @code{call-interactively} function. @xref{Interactive Call}. |
| 797 | that. | ||
| 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 |
| 804 | special form or macro) to each element of a list or other collection. | 802 | special form or macro) to each element of a list or other collection. |
| 805 | Emacs Lisp has several such functions; @code{mapcar} and | 803 | Emacs 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 | 805 | list. @xref{Definition of mapatoms}, for the function @code{mapatoms} |
| 808 | maps over the symbols in an obarray. @xref{Definition of maphash}, | 806 | which maps over the symbols in an obarray. @xref{Definition of |
| 809 | for the function @code{maphash} which maps over key/value associations | 807 | maphash}, for the function @code{maphash} which maps over key/value |
| 810 | in a hash table. | 808 | associations 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 |
| 813 | is a sparse array whose nominal range of indices is very large. To map | 811 | is 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 |
| 902 | byte-code function compiled from such a list, or alternatively a | 900 | names at the same time, it is sometimes convenient to use an explicit |
| 903 | primitive subr-object; names are ``extra.'' Although functions are | 901 | lambda expression---an @dfn{anonymous function}. Anonymous functions |
| 904 | usually defined with @code{defun} and given names at the same time, it | 902 | are valid wherever function names are. They are often assigned as |
| 905 | is occasionally more concise to use an explicit lambda expression---an | 903 | variable values, or as arguments to functions; for instance, you might |
| 906 | anonymous function. Such a list is valid wherever a function name is. | 904 | pass one as the @var{function} argument to @code{mapcar}, which |
| 905 | applies that function to each element of a list (@pxref{Mapping | ||
| 906 | Functions}). @xref{describe-symbols example}, for a realistic example | ||
| 907 | of this. | ||
| 908 | |||
| 909 | When defining a lambda expression that is to be used as an anonymous | ||
| 910 | function, you can in principle use any method to construct the list. | ||
| 911 | But 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... | ||
| 915 | This macro returns an anonymous function with argument list @var{args} | ||
| 916 | and body forms given by @var{body}. In effect, this macro makes | ||
| 917 | @code{lambda} forms ``self-quoting'': evaluating a form whose @sc{car} | ||
| 918 | is @code{lambda} yields the form itself: | ||
| 919 | |||
| 920 | @example | ||
| 921 | (lambda (x) (* x x)) | ||
| 922 | @result{} (lambda (x) (* x x)) | ||
| 923 | @end example | ||
| 924 | |||
| 925 | The @code{lambda} form has one other effect: it tells the Emacs | ||
| 926 | evaluator 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 | ||
| 932 | This special form returns @var{function-object} without evaluating it. | ||
| 933 | In 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 | ||
| 935 | byte-compiler that @var{function-object} is intended to be used as a | ||
| 936 | function. Assuming @var{function-object} is a valid lambda | ||
| 937 | expression, 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)))) | 941 | When the code is byte-compiled, @var{function-object} is compiled into |
| 913 | @result{} (lambda (x) (+ 12 x)) | 942 | a byte-code function object (@pxref{Byte Compilation}). |
| 914 | @end group | ||
| 915 | @end smallexample | ||
| 916 | 943 | ||
| 917 | @noindent | 944 | @item |
| 918 | This computes a list that looks like @code{(lambda (x) (+ 12 x))} and | 945 | When lexical binding is enabled, @var{function-object} is converted |
| 919 | makes it the value (@emph{not} the function definition!) of | 946 | into 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 |
| 951 | The read syntax @code{#'} is a short-hand for using @code{function}. | ||
| 952 | The 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 | ||
| 932 | It does @emph{not} work to write @code{(silly 1)}, because this | ||
| 933 | function is not the @emph{function definition} of @code{silly}. We | ||
| 934 | have not given @code{silly} any function definition, just a value as a | ||
| 935 | variable. | ||
| 936 | |||
| 937 | Most of the time, anonymous functions are constants that appear in | ||
| 938 | your program. For instance, you might want to pass one as an argument | ||
| 939 | to the function @code{mapcar}, which applies any given function to | ||
| 940 | each 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} |
| 944 | function that takes a function as its third argument, followed by a | 961 | function 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 |
| 962 | In the @code{double-property} function, we did not quote the | 979 | Note that we do not quote the @code{lambda} form. |
| 963 | @code{lambda} form. This is permissible, because a @code{lambda} form | ||
| 964 | is @dfn{self-quoting}: evaluating the form yields the form itself. | ||
| 965 | 980 | ||
| 966 | Whether or not you quote a @code{lambda} form makes a difference if | 981 | If you compile the above code, the anonymous function is also |
| 967 | you compile the code (@pxref{Byte Compilation}). If the @code{lambda} | 982 | compiled. This would not happen if, say, you had constructed the |
| 968 | form is unquoted, as in the above example, the anonymous function is | 983 | anonymous function by quoting it as a list: |
| 969 | also compiled. Suppose, however, that we quoted the @code{lambda} | ||
| 970 | form: | ||
| 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 |
| 980 | If you compile this, the argument passed to @code{change-property} is | 993 | In that case, the anonymous function is kept as a lambda expression in |
| 981 | the precise list shown: | 994 | the compiled code. The byte-compiler cannot assume this list is a |
| 982 | 995 | function, 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 | ||
| 988 | The Lisp compiler cannot assume this list is a function, even though | ||
| 989 | it looks like one, since it does not know what @code{change-property} | ||
| 990 | will do with the list. Perhaps it will check whether the @sc{car} of | ||
| 991 | the third element is the symbol @code{*}! | ||
| 992 | |||
| 993 | @findex function | ||
| 994 | The @code{function} special form explicitly tells the byte-compiler | ||
| 995 | that its argument is a function: | ||
| 996 | |||
| 997 | @defspec function function-object | ||
| 998 | @cindex function quoting | ||
| 999 | This special form returns @var{function-object} without evaluating it. | ||
| 1000 | In this, it is equivalent to @code{quote}. However, it serves as a | ||
| 1001 | note to the Emacs Lisp compiler that @var{function-object} is intended | ||
| 1002 | to be used only as a function, and therefore can safely be compiled. | ||
| 1003 | Contrast this with @code{quote}, in @ref{Quoting}. | ||
| 1004 | @end defspec | ||
| 1005 | |||
| 1006 | @cindex @samp{#'} syntax | ||
| 1007 | The read syntax @code{#'} is a short-hand for using @code{function}. | ||
| 1008 | Generally, 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}.) | ||
| 1011 | The 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 | ||
| 1020 | quoting the name of a function, but this usage is just a sort of | ||
| 1021 | comment: | ||
| 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 | |||
| 1118 | this is not checked. The argument @var{symbol} is an ordinary evaluated | 1089 | this is not checked. The argument @var{symbol} is an ordinary evaluated |
| 1119 | argument. | 1090 | argument. |
| 1120 | 1091 | ||
| 1121 | There are three normal uses of this function: | 1092 | The primary use of this function is as a subroutine by constructs that |
| 1093 | define or alter functions, like @code{defadvice} (@pxref{Advising | ||
| 1094 | Functions}). (If @code{defun} were not a primitive, it could be | ||
| 1095 | written as a Lisp macro using @code{fset}.) You can also use it to | ||
| 1096 | give a symbol a function definition that is not a list, e.g.@: a | ||
| 1097 | keyboard macro (@pxref{Keyboard Macros}): | ||
| 1122 | 1098 | ||
| 1123 | @itemize @bullet | 1099 | @example |
| 1124 | @item | 1100 | ;; @r{Define a named keyboard macro.} |
| 1125 | Copying one symbol's function definition to another---in other words, | 1101 | (fset 'kill-two-lines "\^u2\^k") |
| 1126 | making an alternate name for a function. (If you think of this as the | 1102 | @result{} "\^u2\^k" |
| 1127 | definition 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 | 1105 | It you wish to use @code{fset} to make an alternate name for a |
| 1131 | Giving a symbol a function definition that is not a list and therefore | 1106 | function, consider using @code{defalias} instead. @xref{Definition of |
| 1132 | cannot be made with @code{defun}. For example, you can use @code{fset} | 1107 | defalias}. |
| 1133 | to 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 |
| 1139 | In constructs for defining or altering functions. If @code{defun} | 1111 | @section Closures |
| 1140 | were not a primitive, it could be written in Lisp (as a macro) using | ||
| 1141 | @code{fset}. | ||
| 1142 | @end itemize | ||
| 1143 | 1112 | ||
| 1144 | Here are examples of these uses: | 1113 | As explained in @ref{Variable Scoping}, Emacs can optionally enable |
| 1114 | lexical binding of variables. When lexical binding is enabled, any | ||
| 1115 | named function that you create (e.g.@: with @code{defun}), as well as | ||
| 1116 | any anonymous function that you create using the @code{lambda} macro | ||
| 1117 | or the @code{function} special form or the @code{#'} syntax | ||
| 1118 | (@pxref{Anonymous Functions}), is automatically converted into a | ||
| 1119 | closure. | ||
| 1145 | 1120 | ||
| 1146 | @example | 1121 | A closure is a function that also carries a record of the lexical |
| 1147 | @group | 1122 | environment that existed when the function was defined. When it is |
| 1148 | ;; @r{Save @code{foo}'s definition in @code{old-foo}.} | 1123 | invoked, any lexical variable references within its definition use the |
| 1149 | (fset 'old-foo (symbol-function 'foo)) | 1124 | retained lexical environment. In all other respects, closures behave |
| 1150 | @end group | 1125 | much like ordinary functions; in particular, they can be called in the |
| 1126 | same 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.} | 1131 | with the symbol @code{closure} as the first element, a list |
| 1173 | (fset 'kill-two-lines "\^u2\^k") | 1132 | representing the lexical environment as the second element, and the |
| 1174 | @result{} "\^u2\^k" | 1133 | argument 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 |
| 1187 | function before redefining it. That permits the new definition to | 1142 | However, the fact that the internal structure of a closure is |
| 1188 | invoke 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 |
| 1189 | file to redefine a function defined elsewhere. If you want to modify | 1144 | implementation detail. For this reason, we recommend against directly |
| 1190 | a function defined by another package, it is cleaner to use | 1145 | examining 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... |
| 1258 | You can define an @dfn{inline function} by using @code{defsubst} instead | 1212 | Define an inline function. The syntax is exactly the same as |
| 1259 | of @code{defun}. An inline function works just like an ordinary | 1213 | @code{defun} (@pxref{Defining Functions}). |
| 1260 | function except for one thing: when you compile a call to the function, | 1214 | @end defmac |
| 1261 | the function's definition is open-coded into the caller. | 1215 | |
| 1216 | You can define an @dfn{inline function} by using @code{defsubst} | ||
| 1217 | instead of @code{defun}. An inline function works just like an | ||
| 1218 | ordinary function except for one thing: when you byte-compile a call | ||
| 1219 | to the function (@pxref{Byte Compilation}), the function's definition | ||
| 1220 | is expanded into the caller. | ||
| 1262 | 1221 | ||
| 1263 | Making a function inline makes explicit calls run faster. But it also | 1222 | Making a function inline often makes its function calls run faster. |
| 1264 | has disadvantages. For one thing, it reduces flexibility; if you | 1223 | But it also has disadvantages. For one thing, it reduces flexibility; |
| 1265 | change the definition of the function, calls already inlined still use | 1224 | if you change the definition of the function, calls already inlined |
| 1266 | the old definition until you recompile them. | 1225 | still use the old definition until you recompile them. |
| 1267 | 1226 | ||
| 1268 | Another disadvantage is that making a large function inline can increase | 1227 | Another disadvantage is that making a large function inline can |
| 1269 | the size of compiled code both in files and in memory. Since the speed | 1228 | increase the size of compiled code both in files and in memory. Since |
| 1270 | advantage of inline functions is greatest for small functions, you | 1229 | the speed advantage of inline functions is greatest for small |
| 1271 | generally should not make large functions inline. | 1230 | functions, you generally should not make large functions inline. |
| 1272 | 1231 | ||
| 1273 | Also, inline functions do not behave well with respect to debugging, | 1232 | Also, inline functions do not behave well with respect to debugging, |
| 1274 | tracing, and advising (@pxref{Advising Functions}). Since ease of | 1233 | tracing, and advising (@pxref{Advising Functions}). Since ease of |
| 1275 | debugging and the flexibility of redefining functions are important | 1234 | debugging and the flexibility of redefining functions are important |
| 1276 | features of Emacs, you should not make a function inline, even if it's | 1235 | features of Emacs, you should not make a function inline, even if it's |
| 1277 | small, unless its speed is really crucial, and you've timed the code | 1236 | small, unless its speed is really crucial, and you've timed the code |
| 1278 | to verify that using @code{defun} actually has performance problems. | 1237 | to verify that using @code{defun} actually has performance problems. |
| 1279 | 1238 | ||
| 1280 | It'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 |
| 1281 | inline function would execute. (@xref{Macros}.) But the macro would be | 1240 | inline function would execute (@pxref{Macros}). But the macro would |
| 1282 | limited to direct use in expressions---a macro cannot be called with | 1241 | be 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 | 1242 | with @code{apply}, @code{mapcar} and so on. Also, it takes some work |
| 1284 | convert an ordinary function into a macro. To convert it into an inline | 1243 | to convert an ordinary function into a macro. To convert it into an |
| 1285 | function is very easy; simply replace @code{defun} with @code{defsubst}. | 1244 | inline function is easy; just replace @code{defun} with |
| 1286 | Since each argument of an inline function is evaluated exactly once, you | 1245 | @code{defsubst}. Since each argument of an inline function is |
| 1287 | needn't worry about how many times the body uses the arguments, as you | 1246 | evaluated exactly once, you needn't worry about how many times the |
| 1288 | do for macros. (@xref{Argument Evaluation}.) | 1247 | body uses the arguments, as you do for macros. |
| 1289 | 1248 | ||
| 1290 | Inline 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 |
| 1291 | following the definition, just like macros. | 1250 | performed 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 | |||
| 1352 | expand the definition file name relative to the directory of the file | 1311 | expand the definition file name relative to the directory of the file |
| 1353 | that contains the @code{declare-function} call. | 1312 | that 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 |
| 1356 | file name ending in @samp{.c} or @samp{.m}. @code{check-declare-file} | 1315 | name ending in @samp{.c} or @samp{.m}. This is useful only when you |
| 1357 | looks for these files in the C source code directory. This is useful | 1316 | call a primitive that is defined only on certain systems. Most |
| 1358 | only when you call a function that is defined only on certain systems. | 1317 | primitives are always defined, so they will never give you a warning. |
| 1359 | Most of the primitive functions of Emacs are always defined so they will | ||
| 1360 | never 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. |
| 1363 | If you prefix the filename in the @code{declare-function} statement with | 1320 | If 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. |
| 11 | In Lisp, each variable is represented by a Lisp symbol | 11 | In 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 |
| 13 | the symbol's value cell holds the variable's value@footnote{Strictly | 13 | the variable's value is stored in the symbol's value cell@footnote{To |
| 14 | speaking, the symbol's value cell always holds the variable's current | 14 | be precise, under the default @dfn{dynamic binding} rules the value |
| 15 | value under the default @dfn{dynamic binding} rules. Under | 15 | cell always holds the variable's current value, but this is not the |
| 16 | @dfn{lexical binding} rules, the value cell holds the variable's | 16 | case under @dfn{lexical binding} rules. @xref{Variable Scoping}, for |
| 17 | @dfn{global value}. @xref{Variable Scoping}, for details.}. | 17 | details.}. @xref{Symbol Components}. In Emacs Lisp, the use of a |
| 18 | @xref{Symbol Components}. | 18 | symbol 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 | ||
| 21 | its 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 |
| 24 | primarily by Lisp objects, and only secondarily as text. The textual | 21 | primarily by Lisp objects, and only secondarily as text. The textual |
| @@ -151,8 +148,8 @@ does not raise an error if you actually change it. | |||
| 151 | with new values. Sometimes it is useful to give a variable a | 148 | with 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 |
| 153 | part of a Lisp program. When a variable has a local value, we say | 150 | part of a Lisp program. When a variable has a local value, we say |
| 154 | that it has a @dfn{local binding}, or that it is a @dfn{local | 151 | that it is @dfn{locally bound} to that value, and that it is a |
| 155 | variable}. | 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 |
| 158 | receive local values, which are the actual arguments supplied to the | 155 | receive 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 | |||
| 186 | rule} determines where in a program a local binding takes effect. The | 183 | rule} determines where in a program a local binding takes effect. The |
| 187 | default scoping rule in Emacs Lisp is called @dfn{dynamic scoping}, | 184 | default scoping rule in Emacs Lisp is called @dfn{dynamic scoping}, |
| 188 | which simply states that the current binding at any given point in the | 185 | which simply states that the current binding at any given point in the |
| 189 | execution of a program is the most recently-created local binding for | 186 | execution of a program is the most recently-created binding for that |
| 190 | that variable that still exists. For details about dynamic scoping, | 187 | variable that still exists. For details about dynamic scoping, and an |
| 191 | and an alternative scoping rule called @dfn{lexical scoping}, | 188 | alternative scoping rule called @dfn{lexical scoping}, @xref{Variable |
| 192 | @xref{Variable Scoping}. | 189 | Scoping}. |
| 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 |
| 195 | bindings: | 192 | bindings: |
| @@ -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 |
| 298 | say that that symbol's global value is @dfn{void}. Note that this | 295 | cell (@pxref{Symbol Components}). Under Emacs Lisp's default dynamic |
| 299 | does @emph{not} mean the value is @code{nil}. The symbol @code{nil} | 296 | binding rules (@pxref{Variable Scoping}), the value cell stores the |
| 300 | is a Lisp object and can be the value of a variable, just as any other | 297 | variable's current (local or global) value. Note that an unassigned |
| 301 | object can be; but it is still a value. | 298 | value cell is @emph{not} the same as having @code{nil} in the value |
| 302 | 299 | cell. 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 | 300 | a variable, just as any other object can be; but it is still a value. |
| 304 | value cell (@pxref{Symbol Components}). Under Emacs Lisp's default | 301 | If a variable is void, trying to evaluate the variable signals a |
| 305 | dynamic 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 | |
| 307 | variable signals a @code{void-variable} error rather than a value. | 304 | Under lexical binding rules, the value cell only holds the |
| 308 | But when a variable is lexically bound, it can have a local value | 305 | variable's global value, i.e.@: the value outside of any lexical |
| 309 | which is determined by the lexical environment, even if the value cell | 306 | binding contruct. When a variable is lexically bound, the local value |
| 310 | is empty and the variable is technically void. @xref{Variable | 307 | is determined by the lexical environment; the variable may have a |
| 311 | Scoping}. | 308 | local value if its symbol's value cell is unassigned. |
| 312 | 309 | ||
| 313 | @defun makunbound symbol | 310 | @defun makunbound symbol |
| 314 | This function empties out the value cell of @var{symbol}, making the | 311 | This function empties out the value cell of @var{symbol}, making the |
| 315 | variable void. It returns @var{symbol}. | 312 | variable void. It returns @var{symbol}. |
| 316 | 313 | ||
| 317 | If @var{symbol} has a (dynamic) local binding, @code{makunbound} voids | 314 | If @var{symbol} has a dynamic local binding, @code{makunbound} voids |
| 318 | the current binding, and this voidness lasts only as long as the local | 315 | the current binding, and this voidness lasts only as long as the local |
| 319 | binding is in effect. Afterwards, the previously shadowed local or | 316 | binding is in effect. Afterwards, the previously shadowed local or |
| 320 | global binding is reexposed; then the variable will no longer be void, | 317 | global 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} | |||
| 615 | function to extract the value. | 612 | function to extract the value. |
| 616 | 613 | ||
| 617 | @defun symbol-value symbol | 614 | @defun symbol-value symbol |
| 618 | This function returns the value of @var{symbol}. This is the value in | 615 | This function returns the value stored in @var{symbol}'s value cell. |
| 619 | the symbol's value cell, which is where the variable's current | 616 | This is where the variable's current (dynamic) value is stored. If |
| 620 | (dynamic) value is stored. If the variable has no local binding, this | 617 | the variable has no local binding, this is simply its global value. |
| 621 | is simply its global value. | 618 | If the variable is void, a @code{void-variable} error is signaled. |
| 622 | 619 | ||
| 623 | If the variable is lexically bound, the value reported by | 620 | If 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 |
| 625 | value (which is determined by the lexical environment rather than the | 622 | lexical value, which is determined by the lexical environment rather |
| 626 | symbol's value cell). @xref{Variable Scoping}. | 623 | than 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 | |||
| 661 | A @code{void-variable} error is signaled if @var{symbol} is void as a | ||
| 662 | variable. | ||
| 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. | |||
| 972 | within the construct and their local values. When the Lisp evaluator | 966 | within the construct and their local values. When the Lisp evaluator |
| 973 | wants the current value of a variable, it looks first in the lexical | 967 | wants the current value of a variable, it looks first in the lexical |
| 974 | environment; if the variable is not specified in there, it looks in | 968 | environment; if the variable is not specified in there, it looks in |
| 975 | the symbol's value cell, where the dynamical value is stored. | 969 | the 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 |
| 979 | construct has finished executing, its lexical environment can be | 973 | construct 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 |
| 981 | created whenever you evaluate a lambda expression (@pxref{Lambda | 975 | created when you create a named or anonymous function with lexical |
| 982 | Expressions}) with lexical binding enabled. It is represented by a | 976 | binding enabled. @xref{Closures}, for details. |
| 983 | list whose @sc{car} is the symbol @code{closure}. It is a function, | ||
| 984 | in the sense that it can be passed as an argument to @code{funcall}; | ||
| 985 | when called as a function, any lexical variable references within its | ||
| 986 | definition 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 |
| 979 | references within its definition use the retained lexical environment. | ||
| 980 | Here 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 |
| 1195 | Note that references to @code{foo} in @var{body} access the | ||
| 1203 | buffer-local binding of buffer @samp{b}. | 1196 | buffer-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 | |||
| 1642 | property value. Lambda expressions should be quoted so that | 1635 | property 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 | ||
| 1641 | Definitions}). | ||
| 1642 | |||
| 1645 | @defopt safe-local-variable-values | 1643 | @defopt safe-local-variable-values |
| 1646 | This variable provides another way to mark some variable values as | 1644 | This variable provides another way to mark some variable values as |
| 1647 | safe. It is a list of cons cells @code{(@var{var} . @var{val})}, | 1645 | safe. 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, |
| 1665 | ends in any of @samp{-command}, @samp{-frame-alist}, @samp{-function}, | 1663 | it is never entered automatically into |
| 1664 | @code{safe-local-variable-values}; Emacs always queries before setting | ||
| 1665 | a risky variable, unless the user explicitly allows a value by | ||
| 1666 | customizing @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 | ||
| 1670 | define 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 | ||
| 1673 | Definitions}). 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 |
| 1669 | variables @samp{font-lock-keywords}, @samp{font-lock-keywords} | 1678 | risky. The variables @samp{font-lock-keywords}, |
| 1670 | followed by a digit, and @samp{font-lock-syntactic-keywords} are also | 1679 | @samp{font-lock-keywords} followed by a digit, and |
| 1671 | considered risky. Finally, any variable whose name has a | 1680 | @samp{font-lock-syntactic-keywords} are also considered risky. |
| 1672 | non-@code{nil} @code{risky-local-variable} property is considered | ||
| 1673 | risky. | ||
| 1674 | 1681 | ||
| 1675 | @defun risky-local-variable-p sym | 1682 | @defun risky-local-variable-p sym |
| 1676 | This function returns non-@code{nil} if @var{sym} is a risky variable, | 1683 | This function returns non-@code{nil} if @var{sym} is a risky variable, |
| 1677 | based on the above criteria. | 1684 | based 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, | ||
| 1682 | Emacs will always query before setting a risky variable, unless the | ||
| 1683 | user explicitly allows the setting by customizing | ||
| 1684 | @code{safe-local-variable-values} directly. | ||
| 1685 | |||
| 1686 | @defvar ignored-local-variables | 1687 | @defvar ignored-local-variables |
| 1687 | This variable holds a list of variables that should not be given local | 1688 | This variable holds a list of variables that should not be given local |
| 1688 | values by files. Any value specified for one of these variables is | 1689 | values 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. |
| @@ -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. |
| 1053 | The `lexical-binding' variable lets code use lexical scoping for local | 1054 | The `lexical-binding' variable lets code use lexical scoping for local |
| 1054 | variables. It is typically set via a file-local variable in the first | 1055 | variables. It is typically set via a file-local variable in the first |
| 1055 | line of the file, in which case it applies to all the code in that file. | 1056 | line 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 |
| 1058 | binding instead of the old dynamic binding mode. | 1059 | binding 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 |
| 1061 | of function value which looks like (closure ENV ARGS &rest BODY). | 1062 | of 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 |
| 1066 | declared as dynamically bound. | 1067 | declared as dynamically bound. |
| 1067 | 1068 | ||