diff options
| author | Stefan Monnier | 2011-03-01 00:03:24 -0500 |
|---|---|---|
| committer | Stefan Monnier | 2011-03-01 00:03:24 -0500 |
| commit | d032d5e7dfabfae60f3304da02c97cd1e189b9a2 (patch) | |
| tree | 64219849ec4b697e19a1da1c2a5786b61a2c3387 /doc/lispref | |
| parent | 39605a343b566a1a72e0afb61f96d085c2ef8054 (diff) | |
| download | emacs-d032d5e7dfabfae60f3304da02c97cd1e189b9a2.tar.gz emacs-d032d5e7dfabfae60f3304da02c97cd1e189b9a2.zip | |
* doc/lispref/variables.texi (Scope): Mention the availability of lexbind.
(Lexical Binding): New node.
* doc/lispref/eval.texi (Eval): Add `eval's new `lexical' arg.
* lisp/emacs-lisp/cconv.el (cconv-liftwhen): Increase threshold.
(cconv-closure-convert-rec): Convert interactive spec in empty lexenv.
(cconv-analyse-use): Improve unused vars warnings.
(cconv-analyse-form): Analyze interactive spec in empty lexenv.
* lisp/emacs-lisp/bytecomp.el (byte-compile-lambda): Always byte-compile
the interactive spec in lexical-binding mode.
(byte-compile-refresh-preloaded): Don't reload byte-compiler files.
* lisp/custom.el (custom-initialize-default): Use defvar.
(custom-declare-variable): Set the special-variable-p flag.
* lisp/help-fns.el (help-make-usage): Drop leading underscores.
* lisp/dired.el (dired-revert, dired-make-relative): Mark unused args.
(dired-unmark-all-files): Remove unused var `query'.
(dired-overwrite-confirmed): Declare.
(dired-restore-desktop-buffer): Don't use dynamically scoped arg names.
* lisp/mpc.el: Mark unused args.
(mpc--faster-toggle): Remove unused var `songnb'.
* lisp/server.el (server-kill-buffer-running): Move before first use.
* lisp/minibuffer.el: Mark unused args.
* src/callint.c (quotify_arg): Simplify the logic.
(Fcall_interactively): Use lexical binding when evaluating the
interactive spec of a lexically bound function.
Diffstat (limited to 'doc/lispref')
| -rw-r--r-- | doc/lispref/ChangeLog | 6 | ||||
| -rw-r--r-- | doc/lispref/elisp.texi | 3 | ||||
| -rw-r--r-- | doc/lispref/eval.texi | 10 | ||||
| -rw-r--r-- | doc/lispref/variables.texi | 111 |
4 files changed, 97 insertions, 33 deletions
diff --git a/doc/lispref/ChangeLog b/doc/lispref/ChangeLog index c5e445cec38..338dbb5e7fd 100644 --- a/doc/lispref/ChangeLog +++ b/doc/lispref/ChangeLog | |||
| @@ -1,3 +1,9 @@ | |||
| 1 | 2011-03-01 Stefan Monnier <monnier@iro.umontreal.ca> | ||
| 2 | |||
| 3 | * variables.texi (Scope): Mention the availability of lexical scoping. | ||
| 4 | (Lexical Binding): New node. | ||
| 5 | * eval.texi (Eval): Add `eval's new `lexical' arg. | ||
| 6 | |||
| 1 | 2011-02-25 Stefan Monnier <monnier@iro.umontreal.ca> | 7 | 2011-02-25 Stefan Monnier <monnier@iro.umontreal.ca> |
| 2 | 8 | ||
| 3 | * vol2.texi (Top): | 9 | * vol2.texi (Top): |
diff --git a/doc/lispref/elisp.texi b/doc/lispref/elisp.texi index f7c1d55f6ae..cc3ceb8003c 100644 --- a/doc/lispref/elisp.texi +++ b/doc/lispref/elisp.texi | |||
| @@ -466,7 +466,8 @@ Functions | |||
| 466 | * Declaring Functions:: Telling the compiler that a function is defined. | 466 | * Declaring Functions:: Telling the compiler that a function is defined. |
| 467 | * Function Safety:: Determining whether a function is safe to call. | 467 | * Function Safety:: Determining whether a function is safe to call. |
| 468 | * Related Topics:: Cross-references to specific Lisp primitives | 468 | * Related Topics:: Cross-references to specific Lisp primitives |
| 469 | that have a special bearing on how functions work. | 469 | that have a special bearing on how |
| 470 | functions work. | ||
| 470 | 471 | ||
| 471 | Lambda Expressions | 472 | Lambda Expressions |
| 472 | 473 | ||
diff --git a/doc/lispref/eval.texi b/doc/lispref/eval.texi index d44fe5bb95b..74f3d9c48b9 100644 --- a/doc/lispref/eval.texi +++ b/doc/lispref/eval.texi | |||
| @@ -585,6 +585,11 @@ occurrence in a program being run. On rare occasions, you may need to | |||
| 585 | write code that evaluates a form that is computed at run time, such as | 585 | write code that evaluates a form that is computed at run time, such as |
| 586 | after reading a form from text being edited or getting one from a | 586 | after reading a form from text being edited or getting one from a |
| 587 | property list. On these occasions, use the @code{eval} function. | 587 | property list. On these occasions, use the @code{eval} function. |
| 588 | Often @code{eval} is not needed and something else should be used instead. | ||
| 589 | For example, to get the value of a variable, while @code{eval} works, | ||
| 590 | @code{symbol-value} is preferable; or rather than store expressions | ||
| 591 | in a property list that then need to go through @code{eval}, it is better to | ||
| 592 | store functions instead that are then passed to @code{funcall}. | ||
| 588 | 593 | ||
| 589 | The functions and variables described in this section evaluate forms, | 594 | The functions and variables described in this section evaluate forms, |
| 590 | specify limits to the evaluation process, or record recently returned | 595 | specify limits to the evaluation process, or record recently returned |
| @@ -596,10 +601,13 @@ to store an expression in the data structure and evaluate it. Using | |||
| 596 | functions provides the ability to pass information to them as | 601 | functions provides the ability to pass information to them as |
| 597 | arguments. | 602 | arguments. |
| 598 | 603 | ||
| 599 | @defun eval form | 604 | @defun eval form &optional lexical |
| 600 | This is the basic function evaluating an expression. It evaluates | 605 | This is the basic function evaluating an expression. It evaluates |
| 601 | @var{form} in the current environment and returns the result. How the | 606 | @var{form} in the current environment and returns the result. How the |
| 602 | evaluation proceeds depends on the type of the object (@pxref{Forms}). | 607 | evaluation proceeds depends on the type of the object (@pxref{Forms}). |
| 608 | @var{lexical} if non-nil means to evaluate @var{form} using lexical scoping | ||
| 609 | rules (@pxref{Lexical Binding}) instead of the default dynamic scoping used | ||
| 610 | historically in Emacs Lisp. | ||
| 603 | 611 | ||
| 604 | Since @code{eval} is a function, the argument expression that appears | 612 | Since @code{eval} is a function, the argument expression that appears |
| 605 | in a call to @code{eval} is evaluated twice: once as preparation before | 613 | in a call to @code{eval} is evaluated twice: once as preparation before |
diff --git a/doc/lispref/variables.texi b/doc/lispref/variables.texi index 0cdcaa84d58..edffb4742ec 100644 --- a/doc/lispref/variables.texi +++ b/doc/lispref/variables.texi | |||
| @@ -25,22 +25,22 @@ textual Lisp program is written using the read syntax for the symbol | |||
| 25 | representing the variable. | 25 | representing the variable. |
| 26 | 26 | ||
| 27 | @menu | 27 | @menu |
| 28 | * Global Variables:: Variable values that exist permanently, everywhere. | 28 | * Global Variables:: Variable values that exist permanently, everywhere. |
| 29 | * Constant Variables:: Certain "variables" have values that never change. | 29 | * Constant Variables:: Certain "variables" have values that never change. |
| 30 | * Local Variables:: Variable values that exist only temporarily. | 30 | * Local Variables:: Variable values that exist only temporarily. |
| 31 | * Void Variables:: Symbols that lack values. | 31 | * Void Variables:: Symbols that lack values. |
| 32 | * Defining Variables:: A definition says a symbol is used as a variable. | 32 | * Defining Variables:: A definition says a symbol is used as a variable. |
| 33 | * Tips for Defining:: Things you should think about when you | 33 | * Tips for Defining:: Things you should think about when you |
| 34 | define a variable. | 34 | define a variable. |
| 35 | * Accessing Variables:: Examining values of variables whose names | 35 | * Accessing Variables:: Examining values of variables whose names |
| 36 | are known only at run time. | 36 | are known only at run time. |
| 37 | * Setting Variables:: Storing new values in variables. | 37 | * Setting Variables:: Storing new values in variables. |
| 38 | * Variable Scoping:: How Lisp chooses among local and global values. | 38 | * Variable Scoping:: How Lisp chooses among local and global values. |
| 39 | * Buffer-Local Variables:: Variable values in effect only in one buffer. | 39 | * Buffer-Local Variables:: Variable values in effect only in one buffer. |
| 40 | * File Local Variables:: Handling local variable lists in files. | 40 | * File Local Variables:: Handling local variable lists in files. |
| 41 | * Directory Local Variables:: Local variables common to all files in a directory. | 41 | * Directory Local Variables:: Local variables common to all files in a directory. |
| 42 | * Frame-Local Variables:: Frame-local bindings for variables. | 42 | * Frame-Local Variables:: Frame-local bindings for variables. |
| 43 | * Variable Aliases:: Variables that are aliases for other variables. | 43 | * Variable Aliases:: Variables that are aliases for other variables. |
| 44 | * Variables with Restricted Values:: Non-constant variables whose value can | 44 | * Variables with Restricted Values:: Non-constant variables whose value can |
| 45 | @emph{not} be an arbitrary Lisp object. | 45 | @emph{not} be an arbitrary Lisp object. |
| 46 | @end menu | 46 | @end menu |
| @@ -437,14 +437,18 @@ this reason, user options must be defined with @code{defvar}. | |||
| 437 | This special form defines @var{symbol} as a variable and can also | 437 | This special form defines @var{symbol} as a variable and can also |
| 438 | initialize and document it. The definition informs a person reading | 438 | initialize and document it. The definition informs a person reading |
| 439 | your code that @var{symbol} is used as a variable that might be set or | 439 | your code that @var{symbol} is used as a variable that might be set or |
| 440 | changed. Note that @var{symbol} is not evaluated; the symbol to be | 440 | changed. It also declares this variable as @dfn{special}, meaning that it |
| 441 | defined must appear explicitly in the @code{defvar}. | 441 | should always use dynamic scoping rules. Note that @var{symbol} is not |
| 442 | evaluated; the symbol to be defined must appear explicitly in the | ||
| 443 | @code{defvar}. | ||
| 442 | 444 | ||
| 443 | If @var{symbol} is void and @var{value} is specified, @code{defvar} | 445 | If @var{symbol} is void and @var{value} is specified, @code{defvar} |
| 444 | evaluates it and sets @var{symbol} to the result. But if @var{symbol} | 446 | evaluates it and sets @var{symbol} to the result. But if @var{symbol} |
| 445 | already has a value (i.e., it is not void), @var{value} is not even | 447 | already has a value (i.e., it is not void), @var{value} is not even |
| 446 | evaluated, and @var{symbol}'s value remains unchanged. If @var{value} | 448 | evaluated, and @var{symbol}'s value remains unchanged. |
| 447 | is omitted, the value of @var{symbol} is not changed in any case. | 449 | If @var{value} is omitted, the value of @var{symbol} is not changed in any |
| 450 | case; instead, the only effect of @code{defvar} is to declare locally that this | ||
| 451 | variable exists elsewhere and should hence always use dynamic scoping rules. | ||
| 448 | 452 | ||
| 449 | If @var{symbol} has a buffer-local binding in the current buffer, | 453 | If @var{symbol} has a buffer-local binding in the current buffer, |
| 450 | @code{defvar} operates on the default value, which is buffer-independent, | 454 | @code{defvar} operates on the default value, which is buffer-independent, |
| @@ -881,7 +885,7 @@ the others. | |||
| 881 | @cindex extent | 885 | @cindex extent |
| 882 | @cindex dynamic scoping | 886 | @cindex dynamic scoping |
| 883 | @cindex lexical scoping | 887 | @cindex lexical scoping |
| 884 | Local bindings in Emacs Lisp have @dfn{indefinite scope} and | 888 | By default, local bindings in Emacs Lisp have @dfn{indefinite scope} and |
| 885 | @dfn{dynamic extent}. @dfn{Scope} refers to @emph{where} textually in | 889 | @dfn{dynamic extent}. @dfn{Scope} refers to @emph{where} textually in |
| 886 | the source code the binding can be accessed. ``Indefinite scope'' means | 890 | the source code the binding can be accessed. ``Indefinite scope'' means |
| 887 | that any part of the program can potentially access the variable | 891 | that any part of the program can potentially access the variable |
| @@ -893,6 +897,8 @@ lasts as long as the activation of the construct that established it. | |||
| 893 | @dfn{dynamic scoping}. By contrast, most programming languages use | 897 | @dfn{dynamic scoping}. By contrast, most programming languages use |
| 894 | @dfn{lexical scoping}, in which references to a local variable must be | 898 | @dfn{lexical scoping}, in which references to a local variable must be |
| 895 | located textually within the function or block that binds the variable. | 899 | located textually within the function or block that binds the variable. |
| 900 | Emacs can also support lexical scoping, upon request (@pxref{Lexical | ||
| 901 | Binding}). | ||
| 896 | 902 | ||
| 897 | @cindex CL note---special variables | 903 | @cindex CL note---special variables |
| 898 | @quotation | 904 | @quotation |
| @@ -901,11 +907,12 @@ dynamically scoped, like all variables in Emacs Lisp. | |||
| 901 | @end quotation | 907 | @end quotation |
| 902 | 908 | ||
| 903 | @menu | 909 | @menu |
| 904 | * Scope:: Scope means where in the program a value is visible. | 910 | * Scope:: Scope means where in the program a value is visible. |
| 905 | Comparison with other languages. | 911 | Comparison with other languages. |
| 906 | * Extent:: Extent means how long in time a value exists. | 912 | * Extent:: Extent means how long in time a value exists. |
| 907 | * Impl of Scope:: Two ways to implement dynamic scoping. | 913 | * Impl of Scope:: Two ways to implement dynamic scoping. |
| 908 | * Using Scoping:: How to use dynamic scoping carefully and avoid problems. | 914 | * Using Scoping:: How to use dynamic scoping carefully and avoid problems. |
| 915 | * Lexical Binding:: | ||
| 909 | @end menu | 916 | @end menu |
| 910 | 917 | ||
| 911 | @node Scope | 918 | @node Scope |
| @@ -969,12 +976,12 @@ Here, when @code{foo} is called by @code{binder}, it binds @code{x}. | |||
| 969 | by @code{foo} instead of the one bound by @code{binder}. | 976 | by @code{foo} instead of the one bound by @code{binder}. |
| 970 | @end itemize | 977 | @end itemize |
| 971 | 978 | ||
| 972 | Emacs Lisp uses dynamic scoping because simple implementations of | 979 | Emacs Lisp used dynamic scoping by default because simple implementations of |
| 973 | lexical scoping are slow. In addition, every Lisp system needs to offer | 980 | lexical scoping are slow. In addition, every Lisp system needs to offer |
| 974 | dynamic scoping at least as an option; if lexical scoping is the norm, | 981 | dynamic scoping at least as an option; if lexical scoping is the norm, there |
| 975 | there must be a way to specify dynamic scoping instead for a particular | 982 | must be a way to specify dynamic scoping instead for a particular variable. |
| 976 | variable. It might not be a bad thing for Emacs to offer both, but | 983 | Nowadays, Emacs offers both, but the default is still to use exclusively |
| 977 | implementing it with dynamic scoping only was much easier. | 984 | dynamic scoping. |
| 978 | 985 | ||
| 979 | @node Extent | 986 | @node Extent |
| 980 | @subsection Extent | 987 | @subsection Extent |
| @@ -1088,6 +1095,48 @@ for inter-function usage. It also avoids a warning from the byte | |||
| 1088 | compiler. Choose the variable's name to avoid name conflicts---don't | 1095 | compiler. Choose the variable's name to avoid name conflicts---don't |
| 1089 | use short names like @code{x}. | 1096 | use short names like @code{x}. |
| 1090 | 1097 | ||
| 1098 | |||
| 1099 | @node Lexical Binding | ||
| 1100 | @subsection Use of Lexical Scoping | ||
| 1101 | |||
| 1102 | Emacs Lisp can be evaluated in two different modes: in dynamic binding mode or | ||
| 1103 | lexical binding mode. In dynamic binding mode, all local variables use dynamic | ||
| 1104 | scoping, whereas in lexical binding mode variables that have been declared | ||
| 1105 | @dfn{special} (i.e., declared with @code{defvar} or @code{defconst}) use | ||
| 1106 | dynamic scoping and all others use lexical scoping. | ||
| 1107 | |||
| 1108 | @defvar lexical-binding | ||
| 1109 | When non-nil, evaluation of Lisp code uses lexical scoping for non-special | ||
| 1110 | local variables instead of dynamic scoping. If nil, dynamic scoping is used | ||
| 1111 | for all local variables. This variable is typically set for a whole Elisp file | ||
| 1112 | via file local variables (@pxref{File Local Variables}). | ||
| 1113 | @end defvar | ||
| 1114 | |||
| 1115 | @defun special-variable-p SYMBOL | ||
| 1116 | Return whether SYMBOL has been declared as a special variable, via | ||
| 1117 | @code{defvar} or @code{defconst}. | ||
| 1118 | @end defun | ||
| 1119 | |||
| 1120 | The use of a special variable as a formal argument in a function is generally | ||
| 1121 | discouraged and its behavior in lexical binding mode is unspecified (it may use | ||
| 1122 | lexical scoping sometimes and dynamic scoping other times). | ||
| 1123 | |||
| 1124 | Functions like @code{symbol-value}, @code{boundp}, or @code{set} only know | ||
| 1125 | about dynamically scoped variables, so you cannot get the value of a lexical | ||
| 1126 | variable via @code{symbol-value} and neither can you change it via @code{set}. | ||
| 1127 | Another particularity is that code in the body of a @code{defun} or | ||
| 1128 | @code{defmacro} cannot refer to surrounding lexical variables. | ||
| 1129 | |||
| 1130 | Evaluation of a @code{lambda} expression in lexical binding mode will not just | ||
| 1131 | return that lambda expression unchanged, as in the dynamic binding case, but | ||
| 1132 | will instead construct a new object that remembers the current lexical | ||
| 1133 | environment in which that lambda expression was defined, so that the function | ||
| 1134 | body can later be evaluated in the proper context. Those objects are called | ||
| 1135 | @dfn{closures}. They are also functions, in the sense that they are accepted | ||
| 1136 | by @code{funcall}, and they are represented by a cons cell whose @code{car} is | ||
| 1137 | the symbol @code{closure}. | ||
| 1138 | |||
| 1139 | |||
| 1091 | @node Buffer-Local Variables | 1140 | @node Buffer-Local Variables |
| 1092 | @section Buffer-Local Variables | 1141 | @section Buffer-Local Variables |
| 1093 | @cindex variable, buffer-local | 1142 | @cindex variable, buffer-local |
| @@ -1103,9 +1152,9 @@ local to each terminal, or to each frame. @xref{Multiple Terminals}, | |||
| 1103 | and @xref{Frame-Local Variables}.) | 1152 | and @xref{Frame-Local Variables}.) |
| 1104 | 1153 | ||
| 1105 | @menu | 1154 | @menu |
| 1106 | * Intro to Buffer-Local:: Introduction and concepts. | 1155 | * Intro to Buffer-Local:: Introduction and concepts. |
| 1107 | * Creating Buffer-Local:: Creating and destroying buffer-local bindings. | 1156 | * Creating Buffer-Local:: Creating and destroying buffer-local bindings. |
| 1108 | * Default Value:: The default value is seen in buffers | 1157 | * Default Value:: The default value is seen in buffers |
| 1109 | that don't have their own buffer-local values. | 1158 | that don't have their own buffer-local values. |
| 1110 | @end menu | 1159 | @end menu |
| 1111 | 1160 | ||