Mercurial > emacs
comparison lispref/variables.texi @ 21682:90da2489c498
*** empty log message ***
author | Richard M. Stallman <rms@gnu.org> |
---|---|
date | Mon, 20 Apr 1998 17:43:57 +0000 |
parents | 66d807bdc5b4 |
children | d4ac295a98b3 |
comparison
equal
deleted
inserted
replaced
21681:11eafe90b842 | 21682:90da2489c498 |
---|---|
36 * Accessing Variables:: Examining values of variables whose names | 36 * Accessing Variables:: Examining values of variables whose names |
37 are known only at run time. | 37 are known only at run time. |
38 * Setting Variables:: Storing new values in variables. | 38 * Setting Variables:: Storing new values in variables. |
39 * Variable Scoping:: How Lisp chooses among local and global values. | 39 * Variable Scoping:: How Lisp chooses among local and global values. |
40 * Buffer-Local Variables:: Variable values in effect only in one buffer. | 40 * Buffer-Local Variables:: Variable values in effect only in one buffer. |
41 * Frame-Local Variables:: Variable values in effect only in one frame. | |
42 * Future Local Variables:: New kinds of local values we might add some day. | |
41 @end menu | 43 @end menu |
42 | 44 |
43 @node Global Variables | 45 @node Global Variables |
44 @section Global Variables | 46 @section Global Variables |
45 @cindex global variable | 47 @cindex global variable |
71 @end example | 73 @end example |
72 | 74 |
73 @noindent | 75 @noindent |
74 assuming the @code{setq} form shown above has already been executed. | 76 assuming the @code{setq} form shown above has already been executed. |
75 | 77 |
76 If you do another @code{setq}, the new value replaces the old one: | 78 If you do set the same variable again, the new value replaces the old |
79 one: | |
77 | 80 |
78 @example | 81 @example |
79 @group | 82 @group |
80 x | 83 x |
81 @result{} (a b) | 84 @result{} (a b) |
94 @section Variables That Never Change | 97 @section Variables That Never Change |
95 @vindex nil | 98 @vindex nil |
96 @vindex t | 99 @vindex t |
97 @kindex setting-constant | 100 @kindex setting-constant |
98 | 101 |
99 In Emacs Lisp, certain symbols normally evaluate to themselves. | 102 In Emacs Lisp, certain symbols normally evaluate to themselves. These |
100 These include @code{nil} and @code{t}, as well as any symbol whose | 103 include @code{nil} and @code{t}, as well as any symbol whose name starts |
101 name starts with @samp{:}. | 104 with @samp{:}. These symbols cannot be rebound, nor can their values be |
102 | 105 changed. Any attempt to set or bind @code{nil} or @code{t} signals a |
103 The symbols @code{nil} and @code{t} @emph{always} evaluate to | 106 @code{setting-constant} error. The same is true for a symbol whose name |
104 themselves. These symbols cannot be rebound, nor can their values be | 107 starts with @samp{:}, except that you are allowed to set such symbol to |
105 changed. Any attempt to change the value of @code{nil} or @code{t} | 108 itself. |
106 signals a @code{setting-constant} error. | |
107 | 109 |
108 @example | 110 @example |
109 @group | 111 @group |
110 nil @equiv{} 'nil | 112 nil @equiv{} 'nil |
111 @result{} nil | 113 @result{} nil |
113 @group | 115 @group |
114 (setq nil 500) | 116 (setq nil 500) |
115 @error{} Attempt to set constant symbol: nil | 117 @error{} Attempt to set constant symbol: nil |
116 @end group | 118 @end group |
117 @end example | 119 @end example |
120 | |
121 @tindex keyword-symbols-constant-flag | |
122 @defvar keyword-symbols-constant-flag | |
123 If this variable is @code{nil}, you are allowed to set and bind symbols | |
124 whose names start with @samp{:} as you wish. This is to make it | |
125 possible to run old Lisp programs which do that. | |
126 @end defvar | |
118 | 127 |
119 @node Local Variables | 128 @node Local Variables |
120 @section Local Variables | 129 @section Local Variables |
121 @cindex binding local variables | 130 @cindex binding local variables |
122 @cindex local variables | 131 @cindex local variables |
123 @cindex local binding | 132 @cindex local binding |
124 @cindex global binding | 133 @cindex global binding |
125 | 134 |
126 Global variables have values that last until explicitly superseded | 135 Global variables have values that last until explicitly superseded |
127 with new values. Sometimes it is useful to create variable values that | 136 with new values. Sometimes it is useful to create variable values that |
128 exist temporarily---only while within a certain part of the program. | 137 exist temporarily---only until a certain part of the program finishes. |
129 These values are called @dfn{local}, and the variables so used are | 138 These values are called @dfn{local}, and the variables so used are |
130 called @dfn{local variables}. | 139 called @dfn{local variables}. |
131 | 140 |
132 For example, when a function is called, its argument variables receive | 141 For example, when a function is called, its argument variables receive |
133 new local values that last until the function exits. The @code{let} | 142 new local values that last until the function exits. The @code{let} |
141 previous value is @dfn{shadowed} and @dfn{not visible}. Both global and | 150 previous value is @dfn{shadowed} and @dfn{not visible}. Both global and |
142 local values may be shadowed (@pxref{Scope}). | 151 local values may be shadowed (@pxref{Scope}). |
143 | 152 |
144 If you set a variable (such as with @code{setq}) while it is local, | 153 If you set a variable (such as with @code{setq}) while it is local, |
145 this replaces the local value; it does not alter the global value, or | 154 this replaces the local value; it does not alter the global value, or |
146 previous local values that are shadowed. To model this behavior, we | 155 previous local values, that are shadowed. To model this behavior, we |
147 speak of a @dfn{local binding} of the variable as well as a local value. | 156 speak of a @dfn{local binding} of the variable as well as a local value. |
148 | 157 |
149 The local binding is a conceptual place that holds a local value. | 158 The local binding is a conceptual place that holds a local value. |
150 Entry to a function, or a special form such as @code{let}, creates the | 159 Entry to a function, or a special form such as @code{let}, creates the |
151 local binding; exit from the function or from the @code{let} removes the | 160 local binding; exit from the function or from the @code{let} removes the |
159 | 168 |
160 @cindex current binding | 169 @cindex current binding |
161 A variable can have more than one local binding at a time (for | 170 A variable can have more than one local binding at a time (for |
162 example, if there are nested @code{let} forms that bind it). In such a | 171 example, if there are nested @code{let} forms that bind it). In such a |
163 case, the most recently created local binding that still exists is the | 172 case, the most recently created local binding that still exists is the |
164 @dfn{current binding} of the variable. (This is called @dfn{dynamic | 173 @dfn{current binding} of the variable. (This rule is called |
165 scoping}; see @ref{Variable Scoping}.) If there are no local bindings, | 174 @dfn{dynamic scoping}; see @ref{Variable Scoping}.) If there are no |
166 the variable's global binding is its current binding. We also call the | 175 local bindings, the variable's global binding is its current binding. |
167 current binding the @dfn{most-local existing binding}, for emphasis. | 176 We sometimes call the current binding the @dfn{most-local existing |
168 Ordinary evaluation of a symbol always returns the value of its current | 177 binding}, for emphasis. Ordinary evaluation of a symbol always returns |
169 binding. | 178 the value of its current binding. |
170 | 179 |
171 The special forms @code{let} and @code{let*} exist to create | 180 The special forms @code{let} and @code{let*} exist to create |
172 local bindings. | 181 local bindings. |
173 | 182 |
174 @defspec let (bindings@dots{}) forms@dots{} | 183 @defspec let (bindings@dots{}) forms@dots{} |
287 | 296 |
288 @code{makunbound} returns @var{symbol}. | 297 @code{makunbound} returns @var{symbol}. |
289 | 298 |
290 @example | 299 @example |
291 @group | 300 @group |
292 (makunbound 'x) ; @r{Make the global value} | 301 (makunbound 'x) ; @r{Make the global value of @code{x} void.} |
293 ; @r{of @code{x} void.} | |
294 @result{} x | 302 @result{} x |
295 @end group | 303 @end group |
296 @group | 304 @group |
297 x | 305 x |
298 @error{} Symbol's value as variable is void: x | 306 @error{} Symbol's value as variable is void: x |
302 If @var{symbol} is locally bound, @code{makunbound} affects the most | 310 If @var{symbol} is locally bound, @code{makunbound} affects the most |
303 local existing binding. This is the only way a symbol can have a void | 311 local existing binding. This is the only way a symbol can have a void |
304 local binding, since all the constructs that create local bindings | 312 local binding, since all the constructs that create local bindings |
305 create them with values. In this case, the voidness lasts at most as | 313 create them with values. In this case, the voidness lasts at most as |
306 long as the binding does; when the binding is removed due to exit from | 314 long as the binding does; when the binding is removed due to exit from |
307 the construct that made it, the previous or global binding is reexposed | 315 the construct that made it, the previous local or global binding is |
308 as usual, and the variable is no longer void unless the newly reexposed | 316 reexposed as usual, and the variable is no longer void unless the newly |
309 binding was void all along. | 317 reexposed binding was void all along. |
310 | 318 |
311 @smallexample | 319 @smallexample |
312 @group | 320 @group |
313 (setq x 1) ; @r{Put a value in the global binding.} | 321 (setq x 1) ; @r{Put a value in the global binding.} |
314 @result{} 1 | 322 @result{} 1 |
437 stored in the symbol's @code{variable-documentation} property. The | 445 stored in the symbol's @code{variable-documentation} property. The |
438 Emacs help functions (@pxref{Documentation}) look for this property. | 446 Emacs help functions (@pxref{Documentation}) look for this property. |
439 | 447 |
440 If the first character of @var{doc-string} is @samp{*}, it means that | 448 If the first character of @var{doc-string} is @samp{*}, it means that |
441 this variable is considered a user option. This lets users set the | 449 this variable is considered a user option. This lets users set the |
442 variable conventiently using the commands @code{set-variable} and | 450 variable conveniently using the commands @code{set-variable} and |
443 @code{edit-options}. However, it is better to use @code{defcustom} | 451 @code{edit-options}. However, it is better to use @code{defcustom} |
444 instead of @code{defvar}, for user option variables, to specify | 452 instead of @code{defvar} for user option variables, so you can specify |
445 customization information. @xref{Customization}. | 453 customization information. @xref{Customization}. |
446 | 454 |
447 Here are some examples. This form defines @code{foo} but does not | 455 Here are some examples. This form defines @code{foo} but does not |
448 initialize it: | 456 initialize it: |
449 | 457 |
578 @end example | 586 @end example |
579 | 587 |
580 @noindent | 588 @noindent |
581 This method has several benefits. First, if the user quits while | 589 This method has several benefits. First, if the user quits while |
582 loading the file, the variable is either still uninitialized or | 590 loading the file, the variable is either still uninitialized or |
583 initialized properly, never in-between. If it is uninitialized, | 591 initialized properly, never in-between. If it is still uninitialized, |
584 reloading the file will initialize it properly. Second, reloading the | 592 reloading the file will initialize it properly. Second, reloading the |
585 file once the variable is initialized will not alter it; that is | 593 file once the variable is initialized will not alter it; that is |
586 important if the user has run hooks to alter part of the contents (such | 594 important if the user has run hooks to alter part of the contents (such |
587 as, to rebind keys). Third, evaluating the @code{defvar} form with | 595 as, to rebind keys). Third, evaluating the @code{defvar} form with |
588 @kbd{C-M-x} @emph{will} reinitialize the map completely. | 596 @kbd{C-M-x} @emph{will} reinitialize the map completely. |
618 (define-key my-mode-map "\C-c\C-a" 'my-command) | 626 (define-key my-mode-map "\C-c\C-a" 'my-command) |
619 @dots{}) | 627 @dots{}) |
620 @end example | 628 @end example |
621 | 629 |
622 @noindent | 630 @noindent |
623 This code sets the variable, then alters it, but only if the variable | 631 This code sets the variable, then alters it, but it does so in more than |
624 had been @code{ni}. If the user quits just after the @code{setq}, that | 632 one step. If the user quits just after the @code{setq}, that leaves the |
625 leaves the variable neither correctly initialized nor void nor | 633 variable neither correctly initialized nor void nor @code{nil}. Once |
626 @code{nil}. Once that happens, reloading the file will not initialize | 634 that happens, reloading the file will not initialize the variable; it |
627 the variable; it will remain incomplete. | 635 will remain incomplete. |
628 | 636 |
629 @node Accessing Variables | 637 @node Accessing Variables |
630 @section Accessing Variable Values | 638 @section Accessing Variable Values |
631 | 639 |
632 The usual way to reference a variable is to write the symbol which | 640 The usual way to reference a variable is to write the symbol which |
671 (symbol-value 'abracadabra) | 679 (symbol-value 'abracadabra) |
672 @result{} 5 | 680 @result{} 5 |
673 @end group | 681 @end group |
674 @end example | 682 @end example |
675 | 683 |
676 A @code{void-variable} error is signaled if @var{symbol} has neither a | 684 A @code{void-variable} error is signaled if the current binding of |
677 local binding nor a global one. | 685 @var{symbol} is void. |
678 @end defun | 686 @end defun |
679 | 687 |
680 @node Setting Variables | 688 @node Setting Variables |
681 @section How to Alter a Variable Value | 689 @section How to Alter a Variable Value |
682 | 690 |
801 value of @var{symbol} had better be a list already before the call. | 809 value of @var{symbol} had better be a list already before the call. |
802 | 810 |
803 The argument @var{symbol} is not implicitly quoted; @code{add-to-list} | 811 The argument @var{symbol} is not implicitly quoted; @code{add-to-list} |
804 is an ordinary function, like @code{set} and unlike @code{setq}. Quote | 812 is an ordinary function, like @code{set} and unlike @code{setq}. Quote |
805 the argument yourself if that is what you want. | 813 the argument yourself if that is what you want. |
814 @end defun | |
806 | 815 |
807 Here's a scenario showing how to use @code{add-to-list}: | 816 Here's a scenario showing how to use @code{add-to-list}: |
808 | 817 |
809 @example | 818 @example |
810 (setq foo '(a b)) | 819 (setq foo '(a b)) |
817 @result{} (c a b) | 826 @result{} (c a b) |
818 | 827 |
819 foo ;; @r{@code{foo} was changed.} | 828 foo ;; @r{@code{foo} was changed.} |
820 @result{} (c a b) | 829 @result{} (c a b) |
821 @end example | 830 @end example |
822 @end defun | |
823 | 831 |
824 An equivalent expression for @code{(add-to-list '@var{var} | 832 An equivalent expression for @code{(add-to-list '@var{var} |
825 @var{value})} is this: | 833 @var{value})} is this: |
826 | 834 |
827 @example | 835 @example |
977 | 985 |
978 A simple sample implementation (which is not how Emacs Lisp actually | 986 A simple sample implementation (which is not how Emacs Lisp actually |
979 works) may help you understand dynamic binding. This technique is | 987 works) may help you understand dynamic binding. This technique is |
980 called @dfn{deep binding} and was used in early Lisp systems. | 988 called @dfn{deep binding} and was used in early Lisp systems. |
981 | 989 |
982 Suppose there is a stack of bindings: variable-value pairs. At entry | 990 Suppose there is a stack of bindings, which are variable-value pairs. |
983 to a function or to a @code{let} form, we can push bindings onto the stack | 991 At entry to a function or to a @code{let} form, we can push bindings |
984 for the arguments or local variables created there. We can pop those | 992 onto the stack for the arguments or local variables created there. We |
985 bindings from the stack at exit from the binding construct. | 993 can pop those bindings from the stack at exit from the binding |
994 construct. | |
986 | 995 |
987 We can find the value of a variable by searching the stack from top to | 996 We can find the value of a variable by searching the stack from top to |
988 bottom for a binding for that variable; the value from that binding is | 997 bottom for a binding for that variable; the value from that binding is |
989 the value of the variable. To set the variable, we search for the | 998 the value of the variable. To set the variable, we search for the |
990 current binding, then store the new value into that binding. | 999 current binding, then store the new value into that binding. |
1050 @section Buffer-Local Variables | 1059 @section Buffer-Local Variables |
1051 @cindex variables, buffer-local | 1060 @cindex variables, buffer-local |
1052 @cindex buffer-local variables | 1061 @cindex buffer-local variables |
1053 | 1062 |
1054 Global and local variable bindings are found in most programming | 1063 Global and local variable bindings are found in most programming |
1055 languages in one form or another. Emacs also supports another, unusual | 1064 languages in one form or another. Emacs also supports additional, |
1056 kind of variable binding: @dfn{buffer-local} bindings, which apply only | 1065 unusual kinds of variable binding: @dfn{buffer-local} bindings, which |
1057 to one buffer. Emacs Lisp is meant for programming editing commands, | 1066 apply only in one buffer, and frame-local bindings, which apply only in |
1058 and having different values for a variable in different buffers is an | 1067 one frame. Having different values for a variable in different buffers |
1059 important customization method. (A few variables have bindings that | 1068 and/or frames is an important customization method. |
1060 are local to a given X terminal; see @ref{Multiple Displays}.) | 1069 |
1070 This section describes buffer-local bindings; for frame-local | |
1071 bindings, see the following section, @ref{Frame-Local Variables}. (A few | |
1072 variables have bindings that are local to a X terminal; see | |
1073 @ref{Multiple Displays}.) | |
1061 | 1074 |
1062 @menu | 1075 @menu |
1063 * Intro to Buffer-Local:: Introduction and concepts. | 1076 * Intro to Buffer-Local:: Introduction and concepts. |
1064 * Creating Buffer-Local:: Creating and destroying buffer-local bindings. | 1077 * Creating Buffer-Local:: Creating and destroying buffer-local bindings. |
1065 * Default Value:: The default value is seen in buffers | 1078 * Default Value:: The default value is seen in buffers |
1066 that don't have their own local values. | 1079 that don't have their own buffer-local values. |
1067 @end menu | 1080 @end menu |
1068 | 1081 |
1069 @node Intro to Buffer-Local | 1082 @node Intro to Buffer-Local |
1070 @subsection Introduction to Buffer-Local Variables | 1083 @subsection Introduction to Buffer-Local Variables |
1071 | 1084 |
1080 specific buffer, is called the @dfn{default binding}. In most cases, | 1093 specific buffer, is called the @dfn{default binding}. In most cases, |
1081 this is the global binding. | 1094 this is the global binding. |
1082 | 1095 |
1083 A variable can have buffer-local bindings in some buffers but not in | 1096 A variable can have buffer-local bindings in some buffers but not in |
1084 other buffers. The default binding is shared by all the buffers that | 1097 other buffers. The default binding is shared by all the buffers that |
1085 don't have their own bindings for the variable. If you set the variable | 1098 don't have their own bindings for the variable. (This includes all |
1086 in a buffer that does not have a buffer-local binding for it, this sets | 1099 newly created buffers.) If you set the variable in a buffer that does |
1087 the default binding, so the new value is visible in all the buffers that | 1100 not have a buffer-local binding for it, this sets the default binding |
1088 see the default binding. | 1101 (assuming there are no frame-local bindings to complicate the matter), |
1102 so the new value is visible in all the buffers that see the default | |
1103 binding. | |
1089 | 1104 |
1090 The most common use of buffer-local bindings is for major modes to change | 1105 The most common use of buffer-local bindings is for major modes to change |
1091 variables that control the behavior of commands. For example, C mode and | 1106 variables that control the behavior of commands. For example, C mode and |
1092 Lisp mode both set the variable @code{paragraph-start} to specify that only | 1107 Lisp mode both set the variable @code{paragraph-start} to specify that only |
1093 blank lines separate paragraphs. They do this by making the variable | 1108 blank lines separate paragraphs. They do this by making the variable |
1094 buffer-local in the buffer that is being put into C mode or Lisp mode, and | 1109 buffer-local in the buffer that is being put into C mode or Lisp mode, and |
1095 then setting it to the new value for that mode. | 1110 then setting it to the new value for that mode. @xref{Major Modes}. |
1096 | 1111 |
1097 The usual way to make a buffer-local binding is with | 1112 The usual way to make a buffer-local binding is with |
1098 @code{make-local-variable}, which is what major mode commands use. This | 1113 @code{make-local-variable}, which is what major mode commands typically |
1099 affects just the current buffer; all other buffers (including those yet to | 1114 use. This affects just the current buffer; all other buffers (including |
1100 be created) continue to share the default value. | 1115 those yet to be created) will continue to share the default value unless |
1116 they are explicitly given their own buffer-local bindings. | |
1101 | 1117 |
1102 @cindex automatically buffer-local | 1118 @cindex automatically buffer-local |
1103 A more powerful operation is to mark the variable as | 1119 A more powerful operation is to mark the variable as |
1104 @dfn{automatically buffer-local} by calling | 1120 @dfn{automatically buffer-local} by calling |
1105 @code{make-variable-buffer-local}. You can think of this as making the | 1121 @code{make-variable-buffer-local}. You can think of this as making the |
1106 variable local in all buffers, even those yet to be created. More | 1122 variable local in all buffers, even those yet to be created. More |
1107 precisely, the effect is that setting the variable automatically makes | 1123 precisely, the effect is that setting the variable automatically makes |
1108 the variable local to the current buffer if it is not already so. All | 1124 the variable local to the current buffer if it is not already so. All |
1109 buffers start out by sharing the default value of the variable as usual, | 1125 buffers start out by sharing the default value of the variable as usual, |
1110 but any @code{setq} creates a buffer-local binding for the current | 1126 but setting the variable creates a buffer-local binding for the current |
1111 buffer. The new value is stored in the buffer-local binding, leaving | 1127 buffer. The new value is stored in the buffer-local binding, leaving |
1112 the default binding untouched. The default value can no longer be | 1128 the default binding untouched. This means that the default value cannot |
1113 changed with @code{setq} in this buffer; you need to use | 1129 be changed with @code{setq} in any buffer; the only way to change it is |
1114 @code{setq-default} to do that. | 1130 with @code{setq-default}. |
1115 | 1131 |
1116 @strong{Warning:} When a variable has buffer-local values in one or | 1132 @strong{Warning:} When a variable has buffer-local values in one or |
1117 more buffers, you can get Emacs very confused by binding the variable | 1133 more buffers, you can get Emacs very confused by binding the variable |
1118 with @code{let}, changing to a different current buffer in which a | 1134 with @code{let}, changing to a different current buffer in which a |
1119 different binding is in effect, and then exiting the @code{let}. This | 1135 different binding is in effect, and then exiting the @code{let}. This |
1159 | 1175 |
1160 Note that references to @code{foo} in @var{body} access the | 1176 Note that references to @code{foo} in @var{body} access the |
1161 buffer-local binding of buffer @samp{b}. | 1177 buffer-local binding of buffer @samp{b}. |
1162 | 1178 |
1163 When a file specifies local variable values, these become buffer-local | 1179 When a file specifies local variable values, these become buffer-local |
1164 values when you visit the file. @xref{Auto Major Mode}. | 1180 values when you visit the file. @xref{File Variables,,, emacs, The |
1181 GNU Emacs Manual}. | |
1165 | 1182 |
1166 @node Creating Buffer-Local | 1183 @node Creating Buffer-Local |
1167 @subsection Creating and Deleting Buffer-Local Bindings | 1184 @subsection Creating and Deleting Buffer-Local Bindings |
1168 | 1185 |
1169 @deffn Command make-local-variable variable | 1186 @deffn Command make-local-variable variable |
1207 @result{} 5 | 1224 @result{} 5 |
1208 @end group | 1225 @end group |
1209 @end example | 1226 @end example |
1210 | 1227 |
1211 Making a variable buffer-local within a @code{let}-binding for that | 1228 Making a variable buffer-local within a @code{let}-binding for that |
1212 variable does not work. This is because @code{let} does not distinguish | 1229 variable does not work reliably, unless the buffer in which you do this |
1213 between different kinds of bindings; it knows only which variable the | 1230 is not current either on entry to or exit from the @code{let}. This is |
1214 binding was made for. | 1231 because @code{let} does not distinguish between different kinds of |
1232 bindings; it knows only which variable the binding was made for. | |
1215 | 1233 |
1216 If the variable is terminal-local, this function signals an error. Such | 1234 If the variable is terminal-local, this function signals an error. Such |
1217 variables cannot have buffer-local bindings as well. @xref{Multiple | 1235 variables cannot have buffer-local bindings as well. @xref{Multiple |
1218 Displays}. | 1236 Displays}. |
1219 | 1237 |
1224 @deffn Command make-variable-buffer-local variable | 1242 @deffn Command make-variable-buffer-local variable |
1225 This function marks @var{variable} (a symbol) automatically | 1243 This function marks @var{variable} (a symbol) automatically |
1226 buffer-local, so that any subsequent attempt to set it will make it | 1244 buffer-local, so that any subsequent attempt to set it will make it |
1227 local to the current buffer at the time. | 1245 local to the current buffer at the time. |
1228 | 1246 |
1247 A peculiar wrinkle of this feature is that binding the variable (with | |
1248 @code{let} or other binding constructs) does not create a buffer-local | |
1249 binding for it. Only setting the variable (with @code{set} or | |
1250 @code{setq}) does so. | |
1251 | |
1229 The value returned is @var{variable}. | 1252 The value returned is @var{variable}. |
1230 | 1253 |
1231 @strong{Note:} It is a mistake to use @code{make-variable-buffer-local} | 1254 @strong{Warning:} Don't assume that you should use |
1232 for user-option variables, simply because users @emph{might} want to | 1255 @code{make-variable-buffer-local} for user-option variables, simply |
1233 customize them differently in different buffers. Users can make any | 1256 because users @emph{might} want to customize them differently in |
1234 variable local, when they wish to. | 1257 different buffers. Users can make any variable local, when they wish |
1235 | 1258 to. It is better to leave the choice to them. |
1236 The main use of @code{make-variable-buffer-local} is when a variable is | 1259 |
1237 used for internal purposes, and the Lisp program depends on having | 1260 The time to use @code{make-variable-buffer-local} is when it is crucial |
1238 separate values in separate buffers. | 1261 that no two buffers ever share the same binding. For example, when a |
1262 variable is used for internal purposes in a Lisp program which depends | |
1263 on having separate values in separate buffers, then using | |
1264 @code{make-variable-buffer-local} can be the best solution. | |
1239 @end deffn | 1265 @end deffn |
1240 | 1266 |
1241 @defun local-variable-p variable &optional buffer | 1267 @defun local-variable-p variable &optional buffer |
1242 This returns @code{t} if @var{variable} is buffer-local in buffer | 1268 This returns @code{t} if @var{variable} is buffer-local in buffer |
1243 @var{buffer} (which defaults to the current buffer); otherwise, | 1269 @var{buffer} (which defaults to the current buffer); otherwise, |
1244 @code{nil}. | 1270 @code{nil}. |
1245 @end defun | 1271 @end defun |
1246 | 1272 |
1247 @defun buffer-local-variables &optional buffer | 1273 @defun buffer-local-variables &optional buffer |
1248 This function returns a list describing the buffer-local variables in | 1274 This function returns a list describing the buffer-local variables in |
1249 buffer @var{buffer}. It returns an association list (@pxref{Association | 1275 buffer @var{buffer}. (If @var{buffer} is omitted, the current buffer is |
1250 Lists}) in which each association contains one buffer-local variable and | 1276 used.) It returns an association list (@pxref{Association Lists}) in |
1251 its value. When a buffer-local variable is void in @var{buffer}, then | 1277 which each element contains one buffer-local variable and its value. |
1252 it appears directly in the resulting list. If @var{buffer} is omitted, | 1278 However, when a variable's buffer-local binding in @var{buffer} is void, |
1253 the current buffer is used. | 1279 then the variable appears directly in the resulting list. |
1254 | 1280 |
1255 @example | 1281 @example |
1256 @group | 1282 @group |
1257 (make-local-variable 'foobar) | 1283 (make-local-variable 'foobar) |
1258 (makunbound 'foobar) | 1284 (makunbound 'foobar) |
1307 buffer: it sets the local keymap to @code{nil}, the syntax table to the | 1333 buffer: it sets the local keymap to @code{nil}, the syntax table to the |
1308 value of @code{standard-syntax-table}, and the abbrev table to the value | 1334 value of @code{standard-syntax-table}, and the abbrev table to the value |
1309 of @code{fundamental-mode-abbrev-table}. | 1335 of @code{fundamental-mode-abbrev-table}. |
1310 | 1336 |
1311 The very first thing this function does is run the normal hook | 1337 The very first thing this function does is run the normal hook |
1312 @code{change-major-mode-hook} (@pxref{Major Mode Conventions}). | 1338 @code{change-major-mode-hook} (see below). |
1313 | 1339 |
1314 Every major mode command begins by calling this function, which has the | 1340 Every major mode command begins by calling this function, which has the |
1315 effect of switching to Fundamental mode and erasing most of the effects | 1341 effect of switching to Fundamental mode and erasing most of the effects |
1316 of the previous major mode. To ensure that this does its job, the | 1342 of the previous major mode. To ensure that this does its job, the |
1317 variables that major modes set should not be marked permanent. | 1343 variables that major modes set should not be marked permanent. |
1318 | 1344 |
1319 @code{kill-all-local-variables} returns @code{nil}. | 1345 @code{kill-all-local-variables} returns @code{nil}. |
1320 @end defun | 1346 @end defun |
1347 | |
1348 @defvar change-major-mode-hook | |
1349 The function @code{kill-all-local-variables} runs this normal hook | |
1350 before it does anything else. This gives major modes a way to arrange | |
1351 for something special to be done if the user switches to a different | |
1352 major mode. For best results, make this variable buffer-local, so that | |
1353 it will disappear after doing its job and will not interfere with the | |
1354 subsequent major mode. @xref{Hooks}. | |
1355 @end defvar | |
1321 | 1356 |
1322 @c Emacs 19 feature | 1357 @c Emacs 19 feature |
1323 @cindex permanent local variable | 1358 @cindex permanent local variable |
1324 A buffer-local variable is @dfn{permanent} if the variable name (a | 1359 A buffer-local variable is @dfn{permanent} if the variable name (a |
1325 symbol) has a @code{permanent-local} property that is non-@code{nil}. | 1360 symbol) has a @code{permanent-local} property that is non-@code{nil}. |
1330 @subsection The Default Value of a Buffer-Local Variable | 1365 @subsection The Default Value of a Buffer-Local Variable |
1331 @cindex default value | 1366 @cindex default value |
1332 | 1367 |
1333 The global value of a variable with buffer-local bindings is also | 1368 The global value of a variable with buffer-local bindings is also |
1334 called the @dfn{default} value, because it is the value that is in | 1369 called the @dfn{default} value, because it is the value that is in |
1335 effect except when specifically overridden. | 1370 effect whenever neither the current buffer nor the selected frame has |
1371 its own binding for the variable. | |
1336 | 1372 |
1337 The functions @code{default-value} and @code{setq-default} access and | 1373 The functions @code{default-value} and @code{setq-default} access and |
1338 change a variable's default value regardless of whether the current | 1374 change a variable's default value regardless of whether the current |
1339 buffer has a buffer-local binding. For example, you could use | 1375 buffer has a buffer-local binding. For example, you could use |
1340 @code{setq-default} to change the default setting of | 1376 @code{setq-default} to change the default setting of |
1343 this variable. | 1379 this variable. |
1344 | 1380 |
1345 @c Emacs 19 feature | 1381 @c Emacs 19 feature |
1346 The special forms @code{defvar} and @code{defconst} also set the | 1382 The special forms @code{defvar} and @code{defconst} also set the |
1347 default value (if they set the variable at all), rather than any | 1383 default value (if they set the variable at all), rather than any |
1348 buffer-local value. | 1384 buffer-local or frame-local value. |
1349 | 1385 |
1350 @defun default-value symbol | 1386 @defun default-value symbol |
1351 This function returns @var{symbol}'s default value. This is the value | 1387 This function returns @var{symbol}'s default value. This is the value |
1352 that is seen in buffers that do not have their own values for this | 1388 that is seen in buffers and frames that do not have their own values for |
1353 variable. If @var{symbol} is not buffer-local, this is equivalent to | 1389 this variable. If @var{symbol} is not buffer-local, this is equivalent |
1354 @code{symbol-value} (@pxref{Accessing Variables}). | 1390 to @code{symbol-value} (@pxref{Accessing Variables}). |
1355 @end defun | 1391 @end defun |
1356 | 1392 |
1357 @c Emacs 19 feature | 1393 @c Emacs 19 feature |
1358 @defun default-boundp symbol | 1394 @defun default-boundp symbol |
1359 The function @code{default-boundp} tells you whether @var{symbol}'s | 1395 The function @code{default-boundp} tells you whether @var{symbol}'s |
1377 current buffer sees. | 1413 current buffer sees. |
1378 | 1414 |
1379 @example | 1415 @example |
1380 @group | 1416 @group |
1381 ;; @r{In buffer @samp{foo}:} | 1417 ;; @r{In buffer @samp{foo}:} |
1382 (make-local-variable 'local) | 1418 (make-local-variable 'buffer-local) |
1383 @result{} local | 1419 @result{} buffer-local |
1384 @end group | 1420 @end group |
1385 @group | 1421 @group |
1386 (setq local 'value-in-foo) | 1422 (setq buffer-local 'value-in-foo) |
1387 @result{} value-in-foo | 1423 @result{} value-in-foo |
1388 @end group | 1424 @end group |
1389 @group | 1425 @group |
1390 (setq-default local 'new-default) | 1426 (setq-default buffer-local 'new-default) |
1391 @result{} new-default | 1427 @result{} new-default |
1392 @end group | 1428 @end group |
1393 @group | 1429 @group |
1394 local | 1430 buffer-local |
1395 @result{} value-in-foo | 1431 @result{} value-in-foo |
1396 @end group | 1432 @end group |
1397 @group | 1433 @group |
1398 (default-value 'local) | 1434 (default-value 'buffer-local) |
1399 @result{} new-default | 1435 @result{} new-default |
1400 @end group | 1436 @end group |
1401 | 1437 |
1402 @group | 1438 @group |
1403 ;; @r{In (the new) buffer @samp{bar}:} | 1439 ;; @r{In (the new) buffer @samp{bar}:} |
1404 local | 1440 buffer-local |
1405 @result{} new-default | 1441 @result{} new-default |
1406 @end group | 1442 @end group |
1407 @group | 1443 @group |
1408 (default-value 'local) | 1444 (default-value 'buffer-local) |
1409 @result{} new-default | 1445 @result{} new-default |
1410 @end group | 1446 @end group |
1411 @group | 1447 @group |
1412 (setq local 'another-default) | 1448 (setq buffer-local 'another-default) |
1413 @result{} another-default | 1449 @result{} another-default |
1414 @end group | 1450 @end group |
1415 @group | 1451 @group |
1416 (default-value 'local) | 1452 (default-value 'buffer-local) |
1417 @result{} another-default | 1453 @result{} another-default |
1418 @end group | 1454 @end group |
1419 | 1455 |
1420 @group | 1456 @group |
1421 ;; @r{Back in buffer @samp{foo}:} | 1457 ;; @r{Back in buffer @samp{foo}:} |
1422 local | 1458 buffer-local |
1423 @result{} value-in-foo | 1459 @result{} value-in-foo |
1424 (default-value 'local) | 1460 (default-value 'buffer-local) |
1425 @result{} another-default | 1461 @result{} another-default |
1426 @end group | 1462 @end group |
1427 @end example | 1463 @end example |
1428 @end defspec | 1464 @end defspec |
1429 | 1465 |
1440 (default-value 'a) | 1476 (default-value 'a) |
1441 @result{} 23 | 1477 @result{} 23 |
1442 @end group | 1478 @end group |
1443 @end example | 1479 @end example |
1444 @end defun | 1480 @end defun |
1481 | |
1482 @node Frame-Local Variables | |
1483 @section Frame-Local Variables | |
1484 | |
1485 Just as variables can have buffer-local bindings, they can also have | |
1486 frame-local bindings. These bindings belong to one frame, and are in | |
1487 effect when that frame is selected. Frame-local bindings are actually | |
1488 frame parameters: you create a frame-local binding in a specific frame | |
1489 by calling @code{modify-frame-parameters} and specifying the variable | |
1490 name as the parameter name. | |
1491 | |
1492 To enable frame-local bindings for a certain variable, call the function | |
1493 @code{make-variable-frame-local}. | |
1494 | |
1495 @defun make-variable-frame-local variable | |
1496 Enable the use of frame-local bindings for @var{variable}. This does | |
1497 not in itself create any frame-local bindings for the variable; however, | |
1498 if some frame already has a value for @var{variable} as a frame | |
1499 parameter, that value automatically becomes a frame-local binding. | |
1500 | |
1501 If the variable is terminal-local, this function signals an error. Such | |
1502 variables cannot have buffer-local bindings as well. @xref{Multiple | |
1503 Displays}. A few variables that are implemented specially in Emacs | |
1504 can be (and usually are) buffer-local, but can never be frame-local. | |
1505 @end defun | |
1506 | |
1507 Buffer-local bindings take precedence over frame-local bindings. Thus, | |
1508 consider a variable @code{foo}: if the current buffer has a buffer-local | |
1509 binding for @code{foo}, that binding is active; otherwise, if the | |
1510 selected frame has a frame-local binding for @code{foo}, that binding is | |
1511 active; otherwise, the default binding of @code{foo} is active. | |
1512 | |
1513 Here is an example. First we prepare a few bindings for @code{foo}: | |
1514 | |
1515 @example | |
1516 (setq f1 (selected-frame)) | |
1517 (make-variable-frame-local 'foo) | |
1518 | |
1519 ;; @r{Make a buffer-local binding for @code{foo} in @samp{b1}.} | |
1520 (set-buffer (get-buffer-create "b1")) | |
1521 (make-local-variable 'foo) | |
1522 (setq foo '(b 1)) | |
1523 | |
1524 ;; @r{Make a frame-local binding for @code{foo} in a new frame.} | |
1525 ;; @r{Store that frame in @code{f2}.} | |
1526 (setq f2 (make-frame)) | |
1527 (modify-frame-parameters f2 '((foo . (f 2)))) | |
1528 @end example | |
1529 | |
1530 Now we examine @code{foo} in various contexts. Whenever the | |
1531 buffer @samp{b1} is current, its buffer-local binding is in effect, | |
1532 regardless of the selected frame: | |
1533 | |
1534 @example | |
1535 (select-frame f1) | |
1536 (set-buffer (get-buffer-create "b1")) | |
1537 foo | |
1538 @result{} (b 1) | |
1539 | |
1540 (select-frame f2) | |
1541 (set-buffer (get-buffer-create "b1")) | |
1542 foo | |
1543 @result{} (b 1) | |
1544 @end example | |
1545 | |
1546 @noindent | |
1547 Otherwise, the frame gets a chance to provide the binding; when frame | |
1548 @code{f2} is selected, its frame-local binding is in effect: | |
1549 | |
1550 @example | |
1551 (select-frame f2) | |
1552 (set-buffer (get-buffer "*scratch*")) | |
1553 foo | |
1554 @result{} (f 2) | |
1555 @end example | |
1556 | |
1557 @noindent | |
1558 When neither the current buffer nor the selected frame provides | |
1559 a binding, the default binding is used: | |
1560 | |
1561 @example | |
1562 (select-frame f1) | |
1563 (set-buffer (get-buffer "*scratch*")) | |
1564 foo | |
1565 @result{} nil | |
1566 @end example | |
1567 | |
1568 @noindent | |
1569 When the active binding of a variable is a frame-local binding, setting | |
1570 the variable changes that binding. You can observe the result with | |
1571 @code{frame-parameters}: | |
1572 | |
1573 @example | |
1574 (select-frame f2) | |
1575 (set-buffer (get-buffer "*scratch*")) | |
1576 (setq foo 'nobody) | |
1577 (assq 'foo (frame-parameters f2)) | |
1578 @result{} (foo . nobody) | |
1579 @end example | |
1580 | |
1581 @node Future Local Variables | |
1582 @section Possible Future Local Variables | |
1583 | |
1584 We have considered the idea of bindings that are local to a category | |
1585 of frames---for example, all color frames, or all frames with dark | |
1586 backgrounds. We have not implemented them because it is not clear that | |
1587 this feature is really useful. You can get more or less the same | |
1588 results by adding a function to @code{after-make-frame-hook}, set up to | |
1589 define a particular frame parameter according to the appropriate | |
1590 conditions for each frame. | |
1591 | |
1592 It would also be possible to implement window-local bindings. We | |
1593 don't know of many situations where they would be useful, and it seems | |
1594 that indirect buffers (@pxref{Indirect Buffers}) with buffer-local | |
1595 bindings offer a way to handle these situations more robustly. | |
1596 | |
1597 If sufficient application is found for either of these two kinds of | |
1598 local bindings, we will provide it in a subsequent Emacs version. | |
1599 | |
1600 |