diff options
| author | Stefan Monnier | 2011-04-01 13:19:52 -0400 |
|---|---|---|
| committer | Stefan Monnier | 2011-04-01 13:19:52 -0400 |
| commit | 034086489cff2a23cb4d9f8c536e18456be617ef (patch) | |
| tree | 93fa6987e56af7b5fd452f7f909ea0653c5b47de /doc | |
| parent | 1c412c000a5d61d1be7f6fa7e632a517b89de95b (diff) | |
| parent | 7200d79c65c65686495dd95e9f6dd436cf6db55e (diff) | |
| download | emacs-034086489cff2a23cb4d9f8c536e18456be617ef.tar.gz emacs-034086489cff2a23cb4d9f8c536e18456be617ef.zip | |
Merge from lexical-binding branch.
* doc/lispref/eval.texi (Eval): Discourage the use of `eval'.
Document its new `lexical' argument.
* doc/lispref/variables.texi (Defining Variables): Mention the new meaning of `defvar'.
(Lexical Binding): New sub-section.
* lisp/Makefile.in (BIG_STACK_DEPTH, BIG_STACK_OPTS, BYTE_COMPILE_FLAGS):
New variables.
(compile-onefile, .el.elc, compile-calc, recompile): Use them.
(COMPILE_FIRST): Add macroexp and cconv.
* lisp/makefile.w32-in: Mirror changes in Makefile.in.
* lisp/vc/cvs-status.el:
* lisp/vc/diff-mode.el:
* lisp/vc/log-edit.el:
* lisp/vc/log-view.el:
* lisp/vc/smerge-mode.el:
* lisp/textmodes/bibtex-style.el:
* textmodes/css.el:
* lisp/startup.el:
* lisp/uniquify.el:
* lisp/minibuffer.el:
* lisp/newcomment.el:
* lisp/reveal.el:
* lisp/server.el:
* lisp/mpc.el:
* lisp/emacs-lisp/smie.el:
* lisp/doc-view.el:
* lisp/dired.el:
* lisp/abbrev.el: Use lexical binding.
* lisp/custom.el (custom-initialize-default, custom-declare-variable):
Use `defvar'.
* lisp/files.el (lexical-binding): Declare safe.
* lisp/help-fns.el (help-split-fundoc): Return nil if there's nothing else
than the arglist.
(help-add-fundoc-usage): Don't add `Not documented'.
(help-function-arglist): Handle closures, subroutines, and new
byte-code-functions.
(help-make-usage): Remove leading underscores.
(describe-function-1): Handle closures.
(describe-variable): Use special-variable-p for completion.
* lisp/simple.el (with-wrapper-hook, apply-partially): Move to subr.el.
* lisp/subr.el (apply-partially): Use new closures rather than CL.
(--dolist-tail--, --dotimes-limit--): Don't declare dynamic.
(dolist, dotimes): Use slightly different expansion for lexical code.
(functionp): Move to C.
(letrec): New macro.
(with-wrapper-hook): Use it and apply-partially instead of CL.
(eval-after-load): Preserve lexical-binding.
(save-window-excursion, with-output-to-temp-buffer): Turn them
into macros.
* lisp/emacs-lisp/advice.el (ad-arglist): Use help-function-arglist.
* lisp/emacs-lisp/autoload.el (make-autoload): Don't burp on trivial macros.
* lisp/emacs-lisp/byte-opt.el: Use lexical binding.
(byte-inline-lapcode): Remove (to bytecomp).
(byte-compile-inline-expand): Pay attention to inlining to/from
lexically bound code.
(byte-compile-unfold-lambda): Don't handle byte-code-functions
any more.
(byte-optimize-form-code-walker): Don't handle save-window-excursion
any more and don't call compiler-macros.
(byte-compile-splice-in-already-compiled-code): Remove.
(byte-code): Don't inline any more.
(disassemble-offset): Receive `bytes' as argument rather than via
dynamic scoping.
(byte-compile-tag-number): Declare before first use.
(byte-decompile-bytecode-1): Handle new byte-codes, don't change
`return' even if make-spliceable.
(byte-compile-side-effect-and-error-free-ops): Add stack-ref, remove
obsolete interactive-p.
(byte-optimize-lapcode): Optimize new lap-codes.
Don't trip up on new form of `byte-constant' lap code.
* lisp/emacs-lisp/byte-run.el (make-obsolete): Don't set the `byte-compile'
handler any more.
* lisp/emacs-lisp/bytecomp.el: Use lexical binding instead of
a "bytecomp-" prefix. Macroexpand everything as a separate phase.
(byte-compile-initial-macro-environment):
Handle declare-function here.
(byte-compile--lexical-environment): New var.
(byte-stack-ref, byte-stack-set, byte-discardN)
(byte-discardN-preserve-tos): New lap codes.
(byte-interactive-p): Don't use any more.
(byte-compile-push-bytecodes, byte-compile-push-bytecode-const2):
New macros.
(byte-compile-lapcode): Use them and handle new lap codes.
(byte-compile-obsolete): Remove.
(byte-compile-arglist-signature): Handle new byte-code arg"lists".
(byte-compile-arglist-warn): Check late def of inlinable funs.
(byte-compile-cl-warn): Don't silence warnings for compiler-macros
since they should have been expanded by now.
(byte-compile--outbuffer): Rename from bytecomp-outbuffer.
(byte-compile-from-buffer): Remove unused second arg.
(byte-compile-preprocess): New function.
(byte-compile-toplevel-file-form): New function to distinguish
file-form calls from outside from file-form calls from hunk-handlers.
(byte-compile-file-form): Simplify.
(byte-compile-file-form-defsubst): Remove.
(byte-compile-file-form-defmumble): Simplify now that
byte-compile-lambda always returns a byte-code-function.
(byte-compile): Preprocess.
(byte-compile-byte-code-maker, byte-compile-byte-code-unmake):
Remove, not used any more.
(byte-compile-arglist-vars, byte-compile-make-lambda-lexenv)
(byte-compile-make-args-desc): New funs.
(byte-compile-lambda): Handle lexical functions. Always return
a byte-code-function.
(byte-compile-reserved-constants): New var, to make up room for
closed-over variables.
(byte-compile-constants-vector): Obey it.
(byte-compile-top-level): New args `lexenv' and `reserved-csts'.
(byte-compile-macroexpand-declare-function): New function.
(byte-compile-form): Call byte-compile-unfold-bcf to inline immediate
byte-code-functions.
(byte-compile-form): Check obsolescence here.
(byte-compile-inline-lapcode, byte-compile-unfold-bcf): New functions.
(byte-compile-variable-ref): Remove.
(byte-compile-dynamic-variable-op): New fun.
(byte-compile-dynamic-variable-bind, byte-compile-variable-ref)
(byte-compile-variable-set): New funs.
(byte-compile-discard): Add 2 args.
(byte-compile-stack-ref, byte-compile-stack-set)
(byte-compile-make-closure, byte-compile-get-closed-var): New funs.
(byte-compile-funarg, byte-compile-funarg-2): Remove, handled in
macroexpand-all instead.
(byte-compile-quote-form): Remove.
(byte-compile-push-binding-init, byte-compile-not-lexical-var-p)
(byte-compile-bind, byte-compile-unbind): New funs.
(byte-compile-let): Handle let* and lexical binding.
(byte-compile-let*): Remove.
(byte-compile-catch, byte-compile-unwind-protect)
(byte-compile-track-mouse, byte-compile-condition-case):
Handle a new :fun-body form, used for lexical scoping.
(byte-compile-save-window-excursion)
(byte-compile-with-output-to-temp-buffer): Remove.
(byte-compile-defun): Simplify.
(byte-compile-stack-adjustment): New fun.
(byte-compile-out): Use it.
(byte-compile-refresh-preloaded): Don't reload byte-compiler files.
* lisp/emacs-lisp/cconv.el: New file.
* lisp/emacs-lisp/cl-extra.el (cl-macroexpand-all): Properly quote CL
closures.
* lisp/emacs-lisp/cl-macs.el (cl-byte-compile-block)
(cl-byte-compile-throw): Remove.
(cl-block-wrapper, cl-block-throw): Use compiler-macros instead.
* lisp/emacs-lisp/cl.el (pushnew): Silence warning.
* lisp/emacs-lisp/disass.el (disassemble-internal): Handle new
`closure' objects.
(disassemble-1): Handle new byte codes.
* lisp/emacs-lisp/edebug.el (edebug-eval-defun)
(edebug-eval-top-level-form): Use eval-sexp-add-defvars.
(edebug-toggle): Avoid `eval'.
* lisp/emacs-lisp/eieio-comp.el: Remove.
* lisp/emacs-lisp/eieio.el (byte-compile-file-form-defmethod):
Don't autoload.
(eieio-defgeneric-form-primary-only-one): Use `byte-compile' rather
than the internal `byte-compile-lambda'.
(defmethod): Don't hide code under quotes.
(eieio-defmethod): New `code' argument.
* lisp/emacs-lisp/float-sup.el (pi): Don't declare as dynamically bound.
* lisp/emacs-lisp/lisp-mode.el (eval-last-sexp-1):
Use eval-sexp-add-defvars.
(eval-sexp-add-defvars): New fun.
* lisp/emacs-lisp/macroexp.el: Use lexical binding.
(macroexpand-all-1): Check obsolete macros. Expand compiler-macros.
Don't convert ' to #' without checking that it's indeed quoting
a lambda.
* lisp/emacs-lisp/pcase.el: Don't use destructuring-bind.
(pcase--memoize): Rename from pcase-memoize. Change weakness.
(pcase): Add `let' pattern.
Change memoization so it actually works.
(pcase-mutually-exclusive-predicates): Add byte-code-function-p.
(pcase--u1) <guard, pred>: Fix possible shadowing problem.
<let>: New case.
* src/alloc.c (Fmake_symbol): Init new `declared_special' field.
* src/buffer.c (defvar_per_buffer): Set new `declared_special' field.
* src/bytecode.c (Bstack_ref, Bstack_set, Bstack_set2, BdiscardN):
New byte-codes.
(exec_byte_code): New function extracted from Fbyte_code to handle new
calling convention for byte-code-functions. Add new byte-codes.
* src/callint.c (Fcall_interactively): Preserve lexical-binding mode for
interactive spec.
* src/doc.c (Fdocumentation, store_function_docstring):
* src/data.c (Finteractive_form): Handle closures.
* src/eval.c (Fsetq): Handle lexical vars.
(Fdefun, Fdefmacro, Ffunction): Make closures when needed.
(Fdefconst, Fdefvaralias, Fdefvar): Mark as dynamic.
(FletX, Flet): Obey lexical binding.
(Fcommandp): Handle closures.
(Feval): New `lexical' arg.
(eval_sub): New function extracted from Feval. Use it almost
everywhere where Feval was used. Look up vars in lexical env.
Handle closures.
(Ffunctionp): Move from subr.el.
(Ffuncall): Handle closures.
(apply_lambda): Remove `eval_flags'.
(funcall_lambda): Handle closures and new byte-code-functions.
(Fspecial_variable_p): New function.
(syms_of_eval): Initialize the Vinternal_interpreter_environment var,
but without exporting it to Lisp.
* src/fns.c (concat, mapcar1): Accept byte-code-functions.
* src/image.c (parse_image_spec): Use Ffunctionp.
* src/keyboard.c (eval_dyn): New fun.
(menu_item_eval_property): Use it.
* src/lisp.h (struct Lisp_Symbol): New field `declared_special'.
* src/lread.c (lisp_file_lexically_bound_p): New function.
(Fload): Bind Qlexical_binding.
(readevalloop): Remove `evalfun' arg.
Bind Qinternal_interpreter_environment.
(Feval_buffer): Bind Qlexical_binding.
(defvar_int, defvar_bool, defvar_lisp_nopro, defvar_kboard):
Mark as dynamic.
(syms_of_lread): Declare `lexical-binding'.
* src/window.c (Ftemp_output_buffer_show): New fun.
(Fsave_window_excursion):
* src/print.c (Fwith_output_to_temp_buffer): Move to subr.el.
Diffstat (limited to 'doc')
| -rw-r--r-- | doc/lispref/ChangeLog | 10 | ||||
| -rw-r--r-- | doc/lispref/eval.texi | 10 | ||||
| -rw-r--r-- | doc/lispref/variables.texi | 149 |
3 files changed, 136 insertions, 33 deletions
diff --git a/doc/lispref/ChangeLog b/doc/lispref/ChangeLog index 1eb3cfa2556..faa5fa44e46 100644 --- a/doc/lispref/ChangeLog +++ b/doc/lispref/ChangeLog | |||
| @@ -1,6 +1,14 @@ | |||
| 1 | 2011-04-01 Stefan Monnier <monnier@iro.umontreal.ca> | ||
| 2 | |||
| 3 | * variables.texi (Defining Variables): Mention the new meaning of `defvar'. | ||
| 4 | (Lexical Binding): New sub-section. | ||
| 5 | |||
| 6 | * eval.texi (Eval): Discourage the use of `eval'. | ||
| 7 | Document its new `lexical' argument. | ||
| 8 | |||
| 1 | 2011-03-28 Stefan Monnier <monnier@iro.umontreal.ca> | 9 | 2011-03-28 Stefan Monnier <monnier@iro.umontreal.ca> |
| 2 | 10 | ||
| 3 | * commands.texi (Command Overview): post-command-hook is not reset to | 11 | * commands.texi (Command Overview): `post-command-hook' is not reset to |
| 4 | nil any more. | 12 | nil any more. |
| 5 | 13 | ||
| 6 | 2011-03-19 Stefan Monnier <monnier@iro.umontreal.ca> | 14 | 2011-03-19 Stefan Monnier <monnier@iro.umontreal.ca> |
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 a68b2b6dd4e..7e2c32334a4 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:: Use of lexical scoping. | ||
| 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,86 @@ 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 | @menu | ||
| 1140 | * Converting to Lexical Binding:: How to start using lexical scoping | ||
| 1141 | @end menu | ||
| 1142 | |||
| 1143 | @node Converting to Lexical Binding | ||
| 1144 | @subsubsection Converting a package to use lexical scoping | ||
| 1145 | |||
| 1146 | Lexical scoping, as currently implemented, does not bring many significant | ||
| 1147 | benefits, unless you are a seasoned functional programmer addicted to | ||
| 1148 | higher-order functions. But its importance will increase in the future: | ||
| 1149 | lexical scoping opens up a lot more opportunities for optimization, so | ||
| 1150 | lexically scoped code is likely to run faster in future Emacs versions, and it | ||
| 1151 | is much more friendly to concurrency, which we want to add in the near future. | ||
| 1152 | |||
| 1153 | Converting a package to lexical binding is usually pretty easy and should not | ||
| 1154 | break backward compatibility: just add a file-local variable setting | ||
| 1155 | @code{lexical-binding} to @code{t} and add declarations of the form | ||
| 1156 | @code{(defvar @var{VAR})} for every variable which still needs to use | ||
| 1157 | dynamic scoping. | ||
| 1158 | |||
| 1159 | To find which variables need this declaration, the simplest solution is to | ||
| 1160 | check the byte-compiler's warnings. The byte-compiler will usually find those | ||
| 1161 | variables either because they are used outside of a let-binding (leading to | ||
| 1162 | warnings about reference or assignment to ``free variable @var{VAR}'') or | ||
| 1163 | because they are let-bound but not used within the let-binding (leading to | ||
| 1164 | warnings about ``unused lexical variable @var{VAR}''). | ||
| 1165 | |||
| 1166 | In cases where a dynamically scoped variable was bound as a function argument, | ||
| 1167 | you will also need to move this binding to a @code{let}. These cases are also | ||
| 1168 | flagged by the byte-compiler. | ||
| 1169 | |||
| 1170 | To silence byte-compiler warnings about unused variables, just use a variable | ||
| 1171 | name that start with an underscore, which the byte-compiler interpret as an | ||
| 1172 | indication that this is a variable known not to be used. | ||
| 1173 | |||
| 1174 | In most cases, the resulting code will then work with either setting of | ||
| 1175 | @code{lexical-binding}, so it can still be used with older Emacsen (which will | ||
| 1176 | simply ignore the @code{lexical-binding} variable setting). | ||
| 1177 | |||
| 1091 | @node Buffer-Local Variables | 1178 | @node Buffer-Local Variables |
| 1092 | @section Buffer-Local Variables | 1179 | @section Buffer-Local Variables |
| 1093 | @cindex variable, buffer-local | 1180 | @cindex variable, buffer-local |
| @@ -1103,9 +1190,9 @@ local to each terminal, or to each frame. @xref{Multiple Terminals}, | |||
| 1103 | and @xref{Frame-Local Variables}.) | 1190 | and @xref{Frame-Local Variables}.) |
| 1104 | 1191 | ||
| 1105 | @menu | 1192 | @menu |
| 1106 | * Intro to Buffer-Local:: Introduction and concepts. | 1193 | * Intro to Buffer-Local:: Introduction and concepts. |
| 1107 | * Creating Buffer-Local:: Creating and destroying buffer-local bindings. | 1194 | * Creating Buffer-Local:: Creating and destroying buffer-local bindings. |
| 1108 | * Default Value:: The default value is seen in buffers | 1195 | * Default Value:: The default value is seen in buffers |
| 1109 | that don't have their own buffer-local values. | 1196 | that don't have their own buffer-local values. |
| 1110 | @end menu | 1197 | @end menu |
| 1111 | 1198 | ||