diff options
| author | Richard M. Stallman | 2003-06-04 09:25:00 +0000 |
|---|---|---|
| committer | Richard M. Stallman | 2003-06-04 09:25:00 +0000 |
| commit | beab64dc855fec1abd9b0c95f3da839d7fb20163 (patch) | |
| tree | 19044d3529ed2b92c3b6451aa378fb770d09560e | |
| parent | 54f7f2a41a2429dc8b6348875d68635e71f7c02c (diff) | |
| download | emacs-beab64dc855fec1abd9b0c95f3da839d7fb20163.tar.gz emacs-beab64dc855fec1abd9b0c95f3da839d7fb20163.zip | |
Update the example and explanation about let and buffer-local bindings.
| -rw-r--r-- | lispref/variables.texi | 41 |
1 files changed, 15 insertions, 26 deletions
diff --git a/lispref/variables.texi b/lispref/variables.texi index 9b83117f67b..0b316af897c 100644 --- a/lispref/variables.texi +++ b/lispref/variables.texi | |||
| @@ -1184,16 +1184,17 @@ the default binding untouched. This means that the default value cannot | |||
| 1184 | be changed with @code{setq} in any buffer; the only way to change it is | 1184 | be changed with @code{setq} in any buffer; the only way to change it is |
| 1185 | with @code{setq-default}. | 1185 | with @code{setq-default}. |
| 1186 | 1186 | ||
| 1187 | @strong{Warning:} When a variable has buffer-local values in one or | 1187 | @strong{Warning:} When a variable has buffer-local or frame-local |
| 1188 | more buffers, binding the variable with @code{let} and changing to a | 1188 | bindings in one or more buffers, @code{let} rebinds the binding that's |
| 1189 | different current buffer in which a different binding is in | 1189 | currently in effect. For instance, if the current buffer has a |
| 1190 | effect, and then exiting the @code{let}, the variable may not be | 1190 | buffer-local value, @code{let} temporarily rebinds that. If no |
| 1191 | restored to the value it had before the @code{let}. | 1191 | buffer-local or frame-local bindings are in effect, @code{let} rebinds |
| 1192 | 1192 | the default value. If inside the @code{let} you then change to a | |
| 1193 | To preserve your sanity, avoid using a variable in that way. If you | 1193 | different current buffer in which a different binding is in effect, |
| 1194 | use @code{save-excursion} around each piece of code that changes to a | 1194 | you won't see the @code{let} binding any more. And if you exit the |
| 1195 | different current buffer, you will not have this problem | 1195 | @code{let} while still in the other buffer, you won't see the |
| 1196 | (@pxref{Excursions}). Here is an example of what to avoid: | 1196 | unbinding occur (though it will occur properly). Here is an example |
| 1197 | to illustrate: | ||
| 1197 | 1198 | ||
| 1198 | @example | 1199 | @example |
| 1199 | @group | 1200 | @group |
| @@ -1208,24 +1209,12 @@ different current buffer, you will not have this problem | |||
| 1208 | ;; foo @result{} 'g ; @r{the global value since foo is not local in @samp{b}} | 1209 | ;; foo @result{} 'g ; @r{the global value since foo is not local in @samp{b}} |
| 1209 | @var{body}@dots{}) | 1210 | @var{body}@dots{}) |
| 1210 | @group | 1211 | @group |
| 1211 | foo @result{} 'a ; @r{we are still in buffer @samp{b}, but exiting the let} | 1212 | foo @result{} 'g ; @r{exiting restored the local value in buffer @samp{a},} |
| 1212 | ; @r{restored the local value in buffer @samp{a}} | 1213 | ; @r{but we don't see that in buffer @samp{b}} |
| 1213 | @end group | 1214 | @end group |
| 1214 | @group | 1215 | @group |
| 1215 | (set-buffer "a") ; @r{This can be seen here:} | 1216 | (set-buffer "a") ; @r{verify the local value was restored} |
| 1216 | foo @result{} 'a ; @r{we are back to the local value in buffer @samp{a}} | 1217 | foo @result{} 'a |
| 1217 | @end group | ||
| 1218 | @end example | ||
| 1219 | |||
| 1220 | @noindent | ||
| 1221 | But @code{save-excursion} as shown here avoids the problem: | ||
| 1222 | |||
| 1223 | @example | ||
| 1224 | @group | ||
| 1225 | (let ((foo 'temp)) | ||
| 1226 | (save-excursion | ||
| 1227 | (set-buffer "b") | ||
| 1228 | @var{body}@dots{})) | ||
| 1229 | @end group | 1218 | @end group |
| 1230 | @end example | 1219 | @end example |
| 1231 | 1220 | ||