diff options
| author | Noam Postavsky | 2018-02-10 14:06:05 -0500 |
|---|---|---|
| committer | Noam Postavsky | 2018-03-23 08:19:09 -0400 |
| commit | 10b1f2fdd5465407766790131b2ead3500d0798c (patch) | |
| tree | 195c2d51d30f0a734f6eea45b00b5dd785da6897 | |
| parent | 68c2f336b196b29ee0452668b44857b3450a72ac (diff) | |
| download | emacs-10b1f2fdd5465407766790131b2ead3500d0798c.tar.gz emacs-10b1f2fdd5465407766790131b2ead3500d0798c.zip | |
Explain more about (defvar foo) form (Bug#18059)
* doc/lispref/variables.texi (Defining Variables)
(Using Lexical Binding):
* doc/lispref/compile.texi (Compiler Errors): Emphasize that omitting
VALUE for `defvar' marks the variable special only locally.
* doc/lispref/variables.texi (Using Lexical Binding): Add example of
using `defvar' without VALUE.
| -rw-r--r-- | doc/lispref/compile.texi | 3 | ||||
| -rw-r--r-- | doc/lispref/variables.texi | 39 |
2 files changed, 38 insertions, 4 deletions
diff --git a/doc/lispref/compile.texi b/doc/lispref/compile.texi index 0e39866d349..e665b84f9b8 100644 --- a/doc/lispref/compile.texi +++ b/doc/lispref/compile.texi | |||
| @@ -500,7 +500,8 @@ You can tell the compiler that a function is defined using | |||
| 500 | @item | 500 | @item |
| 501 | Likewise, you can tell the compiler that a variable is defined using | 501 | Likewise, you can tell the compiler that a variable is defined using |
| 502 | @code{defvar} with no initial value. (Note that this marks the | 502 | @code{defvar} with no initial value. (Note that this marks the |
| 503 | variable as special, i.e.@: dynamically bound.) @xref{Defining | 503 | variable as special, i.e.@: dynamically bound, but only within the |
| 504 | current lexical scope, or file if at top-level.) @xref{Defining | ||
| 504 | Variables}. | 505 | Variables}. |
| 505 | @end itemize | 506 | @end itemize |
| 506 | 507 | ||
diff --git a/doc/lispref/variables.texi b/doc/lispref/variables.texi index b80bc88a585..4d04335d83a 100644 --- a/doc/lispref/variables.texi +++ b/doc/lispref/variables.texi | |||
| @@ -443,9 +443,13 @@ dynamically bound value; @pxref{Void Variables}), then @var{value} is | |||
| 443 | evaluated and @var{symbol} is set to the result. But if @var{symbol} | 443 | evaluated and @var{symbol} is set to the result. But if @var{symbol} |
| 444 | is not void, @var{value} is not evaluated, and @var{symbol}'s value is | 444 | is not void, @var{value} is not evaluated, and @var{symbol}'s value is |
| 445 | left unchanged. If @var{value} is omitted, the value of @var{symbol} | 445 | left unchanged. If @var{value} is omitted, the value of @var{symbol} |
| 446 | is not changed in any case. Using @code{defvar} with no value is one | 446 | is not changed in any case. |
| 447 | method of suppressing byte compilation warnings, see @ref{Compiler | 447 | |
| 448 | Errors}. | 448 | Note that specifying a value, even @code{nil}, marks the variable as |
| 449 | special permanently. Whereas if @var{value} is omitted then the | ||
| 450 | variable is only marked special locally (i.e.@: within the current | ||
| 451 | lexical scope, or file if at the top-level). This can be useful for | ||
| 452 | suppressing byte compilation warnings, see @ref{Compiler Errors}. | ||
| 449 | 453 | ||
| 450 | If @var{symbol} has a buffer-local binding in the current buffer, | 454 | If @var{symbol} has a buffer-local binding in the current buffer, |
| 451 | @code{defvar} acts on the default value, which is buffer-independent, | 455 | @code{defvar} acts on the default value, which is buffer-independent, |
| @@ -489,6 +493,9 @@ it a documentation string: | |||
| 489 | 493 | ||
| 490 | The @code{defvar} form returns @var{symbol}, but it is normally used | 494 | The @code{defvar} form returns @var{symbol}, but it is normally used |
| 491 | at top level in a file where its value does not matter. | 495 | at top level in a file where its value does not matter. |
| 496 | |||
| 497 | For a more elaborate example of using @code{defvar} without a value, | ||
| 498 | see @ref{Local defvar example}. | ||
| 492 | @end defspec | 499 | @end defspec |
| 493 | 500 | ||
| 494 | @cindex constant variables | 501 | @cindex constant variables |
| @@ -1165,6 +1172,32 @@ variables}. Every variable that has been defined with @code{defvar}, | |||
| 1165 | (@pxref{Defining Variables}). All other variables are subject to | 1172 | (@pxref{Defining Variables}). All other variables are subject to |
| 1166 | lexical binding. | 1173 | lexical binding. |
| 1167 | 1174 | ||
| 1175 | @anchor{Local defvar example} | ||
| 1176 | Using @code{defvar} without a value, it is possible to bind a variable | ||
| 1177 | dynamically just in one file, or in just one part of a file while | ||
| 1178 | still binding it lexically elsewhere. For example: | ||
| 1179 | |||
| 1180 | @example | ||
| 1181 | @group | ||
| 1182 | (let (_) | ||
| 1183 | (defvar x) ; @r{Let-bindings of @code{x} will be dynamic within this let.} | ||
| 1184 | (let ((x -99)) ; @r{This is a dynamic binding of @code{x}.} | ||
| 1185 | (defun get-dynamic-x () | ||
| 1186 | x))) | ||
| 1187 | |||
| 1188 | (let ((x 'lexical)) ; @r{This is a lexical binding of @code{x}.} | ||
| 1189 | (defun get-lexical-x () | ||
| 1190 | x)) | ||
| 1191 | |||
| 1192 | (let (_) | ||
| 1193 | (defvar x) | ||
| 1194 | (let ((x 'dynamic)) | ||
| 1195 | (list (get-lexical-x) | ||
| 1196 | (get-dynamic-x)))) | ||
| 1197 | @result{} (lexical dynamic) | ||
| 1198 | @end group | ||
| 1199 | @end example | ||
| 1200 | |||
| 1168 | @defun special-variable-p symbol | 1201 | @defun special-variable-p symbol |
| 1169 | This function returns non-@code{nil} if @var{symbol} is a special | 1202 | This function returns non-@code{nil} if @var{symbol} is a special |
| 1170 | variable (i.e., it has a @code{defvar}, @code{defcustom}, or | 1203 | variable (i.e., it has a @code{defvar}, @code{defcustom}, or |