variables.texi 82 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181
  1. @c -*-texinfo-*-
  2. @c This is part of the GNU Emacs Lisp Reference Manual.
  3. @c Copyright (C) 1990-1995, 1998-2016 Free Software Foundation, Inc.
  4. @c See the file elisp.texi for copying conditions.
  5. @node Variables
  6. @chapter Variables
  7. @cindex variable
  8. A @dfn{variable} is a name used in a program to stand for a value.
  9. In Lisp, each variable is represented by a Lisp symbol
  10. (@pxref{Symbols}). The variable name is simply the symbol's name, and
  11. the variable's value is stored in the symbol's value cell@footnote{To
  12. be precise, under the default @dfn{dynamic scoping} rule, the value
  13. cell always holds the variable's current value, but this is not the
  14. case under the @dfn{lexical scoping} rule. @xref{Variable Scoping},
  15. for details.}. @xref{Symbol Components}. In Emacs Lisp, the use of a
  16. symbol as a variable is independent of its use as a function name.
  17. As previously noted in this manual, a Lisp program is represented
  18. primarily by Lisp objects, and only secondarily as text. The textual
  19. form of a Lisp program is given by the read syntax of the Lisp objects
  20. that constitute the program. Hence, the textual form of a variable in
  21. a Lisp program is written using the read syntax for the symbol
  22. representing the variable.
  23. @menu
  24. * Global Variables:: Variable values that exist permanently, everywhere.
  25. * Constant Variables:: Variables that never change.
  26. * Local Variables:: Variable values that exist only temporarily.
  27. * Void Variables:: Symbols that lack values.
  28. * Defining Variables:: A definition says a symbol is used as a variable.
  29. * Tips for Defining:: Things you should think about when you
  30. define a variable.
  31. * Accessing Variables:: Examining values of variables whose names
  32. are known only at run time.
  33. * Setting Variables:: Storing new values in variables.
  34. * Variable Scoping:: How Lisp chooses among local and global values.
  35. * Buffer-Local Variables:: Variable values in effect only in one buffer.
  36. * File Local Variables:: Handling local variable lists in files.
  37. * Directory Local Variables:: Local variables common to all files in a directory.
  38. * Variable Aliases:: Variables that are aliases for other variables.
  39. * Variables with Restricted Values:: Non-constant variables whose value can
  40. @emph{not} be an arbitrary Lisp object.
  41. * Generalized Variables:: Extending the concept of variables.
  42. @end menu
  43. @node Global Variables
  44. @section Global Variables
  45. @cindex global variable
  46. The simplest way to use a variable is @dfn{globally}. This means that
  47. the variable has just one value at a time, and this value is in effect
  48. (at least for the moment) throughout the Lisp system. The value remains
  49. in effect until you specify a new one. When a new value replaces the
  50. old one, no trace of the old value remains in the variable.
  51. You specify a value for a symbol with @code{setq}. For example,
  52. @example
  53. (setq x '(a b))
  54. @end example
  55. @noindent
  56. gives the variable @code{x} the value @code{(a b)}. Note that
  57. @code{setq} is a special form (@pxref{Special Forms}); it does not
  58. evaluate its first argument, the name of the variable, but it does
  59. evaluate the second argument, the new value.
  60. Once the variable has a value, you can refer to it by using the
  61. symbol itself as an expression. Thus,
  62. @example
  63. @group
  64. x @result{} (a b)
  65. @end group
  66. @end example
  67. @noindent
  68. assuming the @code{setq} form shown above has already been executed.
  69. If you do set the same variable again, the new value replaces the old
  70. one:
  71. @example
  72. @group
  73. x
  74. @result{} (a b)
  75. @end group
  76. @group
  77. (setq x 4)
  78. @result{} 4
  79. @end group
  80. @group
  81. x
  82. @result{} 4
  83. @end group
  84. @end example
  85. @node Constant Variables
  86. @section Variables that Never Change
  87. @cindex @code{setting-constant} error
  88. @cindex keyword symbol
  89. @cindex variable with constant value
  90. @cindex constant variables
  91. @cindex symbol that evaluates to itself
  92. @cindex symbol with constant value
  93. In Emacs Lisp, certain symbols normally evaluate to themselves. These
  94. include @code{nil} and @code{t}, as well as any symbol whose name starts
  95. with @samp{:} (these are called @dfn{keywords}). These symbols cannot
  96. be rebound, nor can their values be changed. Any attempt to set or bind
  97. @code{nil} or @code{t} signals a @code{setting-constant} error. The
  98. same is true for a keyword (a symbol whose name starts with @samp{:}),
  99. if it is interned in the standard obarray, except that setting such a
  100. symbol to itself is not an error.
  101. @example
  102. @group
  103. nil @equiv{} 'nil
  104. @result{} nil
  105. @end group
  106. @group
  107. (setq nil 500)
  108. @error{} Attempt to set constant symbol: nil
  109. @end group
  110. @end example
  111. @defun keywordp object
  112. function returns @code{t} if @var{object} is a symbol whose name
  113. starts with @samp{:}, interned in the standard obarray, and returns
  114. @code{nil} otherwise.
  115. @end defun
  116. These constants are fundamentally different from the constants
  117. defined using the @code{defconst} special form (@pxref{Defining
  118. Variables}). A @code{defconst} form serves to inform human readers
  119. that you do not intend to change the value of a variable, but Emacs
  120. does not raise an error if you actually change it.
  121. @node Local Variables
  122. @section Local Variables
  123. @cindex binding local variables
  124. @cindex local variables
  125. @cindex local binding
  126. @cindex global binding
  127. Global variables have values that last until explicitly superseded
  128. with new values. Sometimes it is useful to give a variable a
  129. @dfn{local value}---a value that takes effect only within a certain
  130. part of a Lisp program. When a variable has a local value, we say
  131. that it is @dfn{locally bound} to that value, and that it is a
  132. @dfn{local variable}.
  133. For example, when a function is called, its argument variables
  134. receive local values, which are the actual arguments supplied to the
  135. function call; these local bindings take effect within the body of the
  136. function. To take another example, the @code{let} special form
  137. explicitly establishes local bindings for specific variables, which
  138. take effect within the body of the @code{let} form.
  139. We also speak of the @dfn{global binding}, which is where
  140. (conceptually) the global value is kept.
  141. @cindex shadowing of variables
  142. Establishing a local binding saves away the variable's previous
  143. value (or lack of one). We say that the previous value is
  144. @dfn{shadowed}. Both global and local values may be shadowed. If a
  145. local binding is in effect, using @code{setq} on the local variable
  146. stores the specified value in the local binding. When that local
  147. binding is no longer in effect, the previously shadowed value (or lack
  148. of one) comes back.
  149. @cindex current binding
  150. A variable can have more than one local binding at a time (e.g., if
  151. there are nested @code{let} forms that bind the variable). The
  152. @dfn{current binding} is the local binding that is actually in effect.
  153. It determines the value returned by evaluating the variable symbol,
  154. and it is the binding acted on by @code{setq}.
  155. For most purposes, you can think of the current binding as the
  156. innermost local binding, or the global binding if there is no
  157. local binding. To be more precise, a rule called the @dfn{scoping
  158. rule} determines where in a program a local binding takes effect. The
  159. default scoping rule in Emacs Lisp is called @dfn{dynamic scoping},
  160. which simply states that the current binding at any given point in the
  161. execution of a program is the most recently-created binding for that
  162. variable that still exists. For details about dynamic scoping, and an
  163. alternative scoping rule called @dfn{lexical scoping}, @xref{Variable
  164. Scoping}.
  165. The special forms @code{let} and @code{let*} exist to create local
  166. bindings:
  167. @defspec let (bindings@dots{}) forms@dots{}
  168. This special form sets up local bindings for a certain set of
  169. variables, as specified by @var{bindings}, and then evaluates all of
  170. the @var{forms} in textual order. Its return value is the value of
  171. the last form in @var{forms}.
  172. Each of the @var{bindings} is either @w{(i) a} symbol, in which case
  173. that symbol is locally bound to @code{nil}; or @w{(ii) a} list of the
  174. form @code{(@var{symbol} @var{value-form})}, in which case
  175. @var{symbol} is locally bound to the result of evaluating
  176. @var{value-form}. If @var{value-form} is omitted, @code{nil} is used.
  177. All of the @var{value-form}s in @var{bindings} are evaluated in the
  178. order they appear and @emph{before} binding any of the symbols to them.
  179. Here is an example of this: @code{z} is bound to the old value of
  180. @code{y}, which is 2, not the new value of @code{y}, which is 1.
  181. @example
  182. @group
  183. (setq y 2)
  184. @result{} 2
  185. @end group
  186. @group
  187. (let ((y 1)
  188. (z y))
  189. (list y z))
  190. @result{} (1 2)
  191. @end group
  192. @end example
  193. @end defspec
  194. @defspec let* (bindings@dots{}) forms@dots{}
  195. This special form is like @code{let}, but it binds each variable right
  196. after computing its local value, before computing the local value for
  197. the next variable. Therefore, an expression in @var{bindings} can
  198. refer to the preceding symbols bound in this @code{let*} form.
  199. Compare the following example with the example above for @code{let}.
  200. @example
  201. @group
  202. (setq y 2)
  203. @result{} 2
  204. @end group
  205. @group
  206. (let* ((y 1)
  207. (z y)) ; @r{Use the just-established value of @code{y}.}
  208. (list y z))
  209. @result{} (1 1)
  210. @end group
  211. @end example
  212. @end defspec
  213. Here is a complete list of the other facilities that create local
  214. bindings:
  215. @itemize @bullet
  216. @item
  217. Function calls (@pxref{Functions}).
  218. @item
  219. Macro calls (@pxref{Macros}).
  220. @item
  221. @code{condition-case} (@pxref{Errors}).
  222. @end itemize
  223. Variables can also have buffer-local bindings (@pxref{Buffer-Local
  224. Variables}); a few variables have terminal-local bindings
  225. (@pxref{Multiple Terminals}). These kinds of bindings work somewhat
  226. like ordinary local bindings, but they are localized depending on
  227. where you are in Emacs.
  228. @defopt max-specpdl-size
  229. @anchor{Definition of max-specpdl-size}
  230. @cindex variable limit error
  231. @cindex evaluation error
  232. @cindex infinite recursion
  233. This variable defines the limit on the total number of local variable
  234. bindings and @code{unwind-protect} cleanups (see @ref{Cleanups,,
  235. Cleaning Up from Nonlocal Exits}) that are allowed before Emacs
  236. signals an error (with data @code{"Variable binding depth exceeds
  237. max-specpdl-size"}).
  238. This limit, with the associated error when it is exceeded, is one way
  239. that Lisp avoids infinite recursion on an ill-defined function.
  240. @code{max-lisp-eval-depth} provides another limit on depth of nesting.
  241. @xref{Definition of max-lisp-eval-depth,, Eval}.
  242. The default value is 1300. Entry to the Lisp debugger increases the
  243. value, if there is little room left, to make sure the debugger itself
  244. has room to execute.
  245. @end defopt
  246. @node Void Variables
  247. @section When a Variable is Void
  248. @cindex @code{void-variable} error
  249. @cindex void variable
  250. We say that a variable is void if its symbol has an unassigned value
  251. cell (@pxref{Symbol Components}).
  252. Under Emacs Lisp's default dynamic scoping rule (@pxref{Variable
  253. Scoping}), the value cell stores the variable's current (local or
  254. global) value. Note that an unassigned value cell is @emph{not} the
  255. same as having @code{nil} in the value cell. The symbol @code{nil} is
  256. a Lisp object and can be the value of a variable, just as any other
  257. object can be; but it is still a value. If a variable is void, trying
  258. to evaluate the variable signals a @code{void-variable} error, instead
  259. of returning a value.
  260. Under the optional lexical scoping rule, the value cell only holds
  261. the variable's global value---the value outside of any lexical binding
  262. construct. When a variable is lexically bound, the local value is
  263. determined by the lexical environment; hence, variables can have local
  264. values even if their symbols' value cells are unassigned.
  265. @defun makunbound symbol
  266. This function empties out the value cell of @var{symbol}, making the
  267. variable void. It returns @var{symbol}.
  268. If @var{symbol} has a dynamic local binding, @code{makunbound} voids
  269. the current binding, and this voidness lasts only as long as the local
  270. binding is in effect. Afterwards, the previously shadowed local or
  271. global binding is reexposed; then the variable will no longer be void,
  272. unless the reexposed binding is void too.
  273. Here are some examples (assuming dynamic binding is in effect):
  274. @smallexample
  275. @group
  276. (setq x 1) ; @r{Put a value in the global binding.}
  277. @result{} 1
  278. (let ((x 2)) ; @r{Locally bind it.}
  279. (makunbound 'x) ; @r{Void the local binding.}
  280. x)
  281. @error{} Symbol's value as variable is void: x
  282. @end group
  283. @group
  284. x ; @r{The global binding is unchanged.}
  285. @result{} 1
  286. (let ((x 2)) ; @r{Locally bind it.}
  287. (let ((x 3)) ; @r{And again.}
  288. (makunbound 'x) ; @r{Void the innermost-local binding.}
  289. x)) ; @r{And refer: it's void.}
  290. @error{} Symbol's value as variable is void: x
  291. @end group
  292. @group
  293. (let ((x 2))
  294. (let ((x 3))
  295. (makunbound 'x)) ; @r{Void inner binding, then remove it.}
  296. x) ; @r{Now outer @code{let} binding is visible.}
  297. @result{} 2
  298. @end group
  299. @end smallexample
  300. @end defun
  301. @defun boundp variable
  302. This function returns @code{t} if @var{variable} (a symbol) is not
  303. void, and @code{nil} if it is void.
  304. Here are some examples (assuming dynamic binding is in effect):
  305. @smallexample
  306. @group
  307. (boundp 'abracadabra) ; @r{Starts out void.}
  308. @result{} nil
  309. @end group
  310. @group
  311. (let ((abracadabra 5)) ; @r{Locally bind it.}
  312. (boundp 'abracadabra))
  313. @result{} t
  314. @end group
  315. @group
  316. (boundp 'abracadabra) ; @r{Still globally void.}
  317. @result{} nil
  318. @end group
  319. @group
  320. (setq abracadabra 5) ; @r{Make it globally nonvoid.}
  321. @result{} 5
  322. @end group
  323. @group
  324. (boundp 'abracadabra)
  325. @result{} t
  326. @end group
  327. @end smallexample
  328. @end defun
  329. @node Defining Variables
  330. @section Defining Global Variables
  331. @cindex variable definition
  332. A @dfn{variable definition} is a construct that announces your
  333. intention to use a symbol as a global variable. It uses the special
  334. forms @code{defvar} or @code{defconst}, which are documented below.
  335. A variable definition serves three purposes. First, it informs
  336. people who read the code that the symbol is @emph{intended} to be used
  337. a certain way (as a variable). Second, it informs the Lisp system of
  338. this, optionally supplying an initial value and a documentation
  339. string. Third, it provides information to programming tools such as
  340. @command{etags}, allowing them to find where the variable was defined.
  341. The difference between @code{defconst} and @code{defvar} is mainly a
  342. matter of intent, serving to inform human readers of whether the value
  343. should ever change. Emacs Lisp does not actually prevent you from
  344. changing the value of a variable defined with @code{defconst}. One
  345. notable difference between the two forms is that @code{defconst}
  346. unconditionally initializes the variable, whereas @code{defvar}
  347. initializes it only if it is originally void.
  348. To define a customizable variable, you should use @code{defcustom}
  349. (which calls @code{defvar} as a subroutine). @xref{Variable
  350. Definitions}.
  351. @defspec defvar symbol [value [doc-string]]
  352. This special form defines @var{symbol} as a variable. Note that
  353. @var{symbol} is not evaluated; the symbol to be defined should appear
  354. explicitly in the @code{defvar} form. The variable is marked as
  355. @dfn{special}, meaning that it should always be dynamically bound
  356. (@pxref{Variable Scoping}).
  357. If @var{value} is specified, and @var{symbol} is void (i.e., it has no
  358. dynamically bound value; @pxref{Void Variables}), then @var{value} is
  359. evaluated and @var{symbol} is set to the result. But if @var{symbol}
  360. is not void, @var{value} is not evaluated, and @var{symbol}'s value is
  361. left unchanged. If @var{value} is omitted, the value of @var{symbol}
  362. is not changed in any case.
  363. If @var{symbol} has a buffer-local binding in the current buffer,
  364. @code{defvar} acts on the default value, which is buffer-independent,
  365. rather than the buffer-local binding. It sets the default value if
  366. the default value is void. @xref{Buffer-Local Variables}.
  367. If @var{symbol} is already lexically bound (e.g., if the @code{defvar}
  368. form occurs in a @code{let} form with lexical binding enabled), then
  369. @code{defvar} sets the dynamic value. The lexical binding remains in
  370. effect until its binding construct exits. @xref{Variable Scoping}.
  371. When you evaluate a top-level @code{defvar} form with @kbd{C-M-x} in
  372. Emacs Lisp mode (@code{eval-defun}), a special feature of
  373. @code{eval-defun} arranges to set the variable unconditionally, without
  374. testing whether its value is void.
  375. If the @var{doc-string} argument is supplied, it specifies the
  376. documentation string for the variable (stored in the symbol's
  377. @code{variable-documentation} property). @xref{Documentation}.
  378. Here are some examples. This form defines @code{foo} but does not
  379. initialize it:
  380. @example
  381. @group
  382. (defvar foo)
  383. @result{} foo
  384. @end group
  385. @end example
  386. This example initializes the value of @code{bar} to @code{23}, and gives
  387. it a documentation string:
  388. @example
  389. @group
  390. (defvar bar 23
  391. "The normal weight of a bar.")
  392. @result{} bar
  393. @end group
  394. @end example
  395. The @code{defvar} form returns @var{symbol}, but it is normally used
  396. at top level in a file where its value does not matter.
  397. @end defspec
  398. @cindex constant variables
  399. @defspec defconst symbol value [doc-string]
  400. This special form defines @var{symbol} as a value and initializes it.
  401. It informs a person reading your code that @var{symbol} has a standard
  402. global value, established here, that should not be changed by the user
  403. or by other programs. Note that @var{symbol} is not evaluated; the
  404. symbol to be defined must appear explicitly in the @code{defconst}.
  405. The @code{defconst} form, like @code{defvar}, marks the variable as
  406. @dfn{special}, meaning that it should always be dynamically bound
  407. (@pxref{Variable Scoping}). In addition, it marks the variable as
  408. risky (@pxref{File Local Variables}).
  409. @code{defconst} always evaluates @var{value}, and sets the value of
  410. @var{symbol} to the result. If @var{symbol} does have a buffer-local
  411. binding in the current buffer, @code{defconst} sets the default value,
  412. not the buffer-local value. (But you should not be making
  413. buffer-local bindings for a symbol that is defined with
  414. @code{defconst}.)
  415. An example of the use of @code{defconst} is Emacs's definition of
  416. @code{float-pi}---the mathematical constant @math{pi}, which ought not
  417. to be changed by anyone (attempts by the Indiana State Legislature
  418. notwithstanding). As the second form illustrates, however,
  419. @code{defconst} is only advisory.
  420. @example
  421. @group
  422. (defconst float-pi 3.141592653589793 "The value of Pi.")
  423. @result{} float-pi
  424. @end group
  425. @group
  426. (setq float-pi 3)
  427. @result{} float-pi
  428. @end group
  429. @group
  430. float-pi
  431. @result{} 3
  432. @end group
  433. @end example
  434. @end defspec
  435. @strong{Warning:} If you use a @code{defconst} or @code{defvar}
  436. special form while the variable has a local binding (made with
  437. @code{let}, or a function argument), it sets the local binding rather
  438. than the global binding. This is not what you usually want. To
  439. prevent this, use these special forms at top level in a file, where
  440. normally no local binding is in effect, and make sure to load the file
  441. before making a local binding for the variable.
  442. @node Tips for Defining
  443. @section Tips for Defining Variables Robustly
  444. When you define a variable whose value is a function, or a list of
  445. functions, use a name that ends in @samp{-function} or
  446. @samp{-functions}, respectively.
  447. There are several other variable name conventions;
  448. here is a complete list:
  449. @table @samp
  450. @item @dots{}-hook
  451. The variable is a normal hook (@pxref{Hooks}).
  452. @item @dots{}-function
  453. The value is a function.
  454. @item @dots{}-functions
  455. The value is a list of functions.
  456. @item @dots{}-form
  457. The value is a form (an expression).
  458. @item @dots{}-forms
  459. The value is a list of forms (expressions).
  460. @item @dots{}-predicate
  461. The value is a predicate---a function of one argument that returns
  462. non-@code{nil} for success and @code{nil} for failure.
  463. @item @dots{}-flag
  464. The value is significant only as to whether it is @code{nil} or not.
  465. Since such variables often end up acquiring more values over time,
  466. this convention is not strongly recommended.
  467. @item @dots{}-program
  468. The value is a program name.
  469. @item @dots{}-command
  470. The value is a whole shell command.
  471. @item @dots{}-switches
  472. The value specifies options for a command.
  473. @end table
  474. When you define a variable, always consider whether you should mark
  475. it as safe or risky; see @ref{File Local Variables}.
  476. When defining and initializing a variable that holds a complicated
  477. value (such as a keymap with bindings in it), it's best to put the
  478. entire computation of the value into the @code{defvar}, like this:
  479. @example
  480. (defvar my-mode-map
  481. (let ((map (make-sparse-keymap)))
  482. (define-key map "\C-c\C-a" 'my-command)
  483. @dots{}
  484. map)
  485. @var{docstring})
  486. @end example
  487. @noindent
  488. This method has several benefits. First, if the user quits while
  489. loading the file, the variable is either still uninitialized or
  490. initialized properly, never in-between. If it is still uninitialized,
  491. reloading the file will initialize it properly. Second, reloading the
  492. file once the variable is initialized will not alter it; that is
  493. important if the user has run hooks to alter part of the contents
  494. (such as, to rebind keys). Third, evaluating the @code{defvar} form
  495. with @kbd{C-M-x} will reinitialize the map completely.
  496. Putting so much code in the @code{defvar} form has one disadvantage:
  497. it puts the documentation string far away from the line which names the
  498. variable. Here's a safe way to avoid that:
  499. @example
  500. (defvar my-mode-map nil
  501. @var{docstring})
  502. (unless my-mode-map
  503. (let ((map (make-sparse-keymap)))
  504. (define-key map "\C-c\C-a" 'my-command)
  505. @dots{}
  506. (setq my-mode-map map)))
  507. @end example
  508. @noindent
  509. This has all the same advantages as putting the initialization inside
  510. the @code{defvar}, except that you must type @kbd{C-M-x} twice, once on
  511. each form, if you do want to reinitialize the variable.
  512. @node Accessing Variables
  513. @section Accessing Variable Values
  514. The usual way to reference a variable is to write the symbol which
  515. names it. @xref{Symbol Forms}.
  516. Occasionally, you may want to reference a variable which is only
  517. determined at run time. In that case, you cannot specify the variable
  518. name in the text of the program. You can use the @code{symbol-value}
  519. function to extract the value.
  520. @defun symbol-value symbol
  521. This function returns the value stored in @var{symbol}'s value cell.
  522. This is where the variable's current (dynamic) value is stored. If
  523. the variable has no local binding, this is simply its global value.
  524. If the variable is void, a @code{void-variable} error is signaled.
  525. If the variable is lexically bound, the value reported by
  526. @code{symbol-value} is not necessarily the same as the variable's
  527. lexical value, which is determined by the lexical environment rather
  528. than the symbol's value cell. @xref{Variable Scoping}.
  529. @example
  530. @group
  531. (setq abracadabra 5)
  532. @result{} 5
  533. @end group
  534. @group
  535. (setq foo 9)
  536. @result{} 9
  537. @end group
  538. @group
  539. ;; @r{Here the symbol @code{abracadabra}}
  540. ;; @r{is the symbol whose value is examined.}
  541. (let ((abracadabra 'foo))
  542. (symbol-value 'abracadabra))
  543. @result{} foo
  544. @end group
  545. @group
  546. ;; @r{Here, the value of @code{abracadabra},}
  547. ;; @r{which is @code{foo},}
  548. ;; @r{is the symbol whose value is examined.}
  549. (let ((abracadabra 'foo))
  550. (symbol-value abracadabra))
  551. @result{} 9
  552. @end group
  553. @group
  554. (symbol-value 'abracadabra)
  555. @result{} 5
  556. @end group
  557. @end example
  558. @end defun
  559. @node Setting Variables
  560. @section Setting Variable Values
  561. The usual way to change the value of a variable is with the special
  562. form @code{setq}. When you need to compute the choice of variable at
  563. run time, use the function @code{set}.
  564. @defspec setq [symbol form]@dots{}
  565. This special form is the most common method of changing a variable's
  566. value. Each @var{symbol} is given a new value, which is the result of
  567. evaluating the corresponding @var{form}. The current binding of the
  568. symbol is changed.
  569. @code{setq} does not evaluate @var{symbol}; it sets the symbol that you
  570. write. We say that this argument is @dfn{automatically quoted}. The
  571. @samp{q} in @code{setq} stands for ``quoted''.
  572. The value of the @code{setq} form is the value of the last @var{form}.
  573. @example
  574. @group
  575. (setq x (1+ 2))
  576. @result{} 3
  577. @end group
  578. x ; @r{@code{x} now has a global value.}
  579. @result{} 3
  580. @group
  581. (let ((x 5))
  582. (setq x 6) ; @r{The local binding of @code{x} is set.}
  583. x)
  584. @result{} 6
  585. @end group
  586. x ; @r{The global value is unchanged.}
  587. @result{} 3
  588. @end example
  589. Note that the first @var{form} is evaluated, then the first
  590. @var{symbol} is set, then the second @var{form} is evaluated, then the
  591. second @var{symbol} is set, and so on:
  592. @example
  593. @group
  594. (setq x 10 ; @r{Notice that @code{x} is set before}
  595. y (1+ x)) ; @r{the value of @code{y} is computed.}
  596. @result{} 11
  597. @end group
  598. @end example
  599. @end defspec
  600. @defun set symbol value
  601. This function puts @var{value} in the value cell of @var{symbol}.
  602. Since it is a function rather than a special form, the expression
  603. written for @var{symbol} is evaluated to obtain the symbol to set.
  604. The return value is @var{value}.
  605. When dynamic variable binding is in effect (the default), @code{set}
  606. has the same effect as @code{setq}, apart from the fact that
  607. @code{set} evaluates its @var{symbol} argument whereas @code{setq}
  608. does not. But when a variable is lexically bound, @code{set} affects
  609. its @emph{dynamic} value, whereas @code{setq} affects its current
  610. (lexical) value. @xref{Variable Scoping}.
  611. @example
  612. @group
  613. (set one 1)
  614. @error{} Symbol's value as variable is void: one
  615. @end group
  616. @group
  617. (set 'one 1)
  618. @result{} 1
  619. @end group
  620. @group
  621. (set 'two 'one)
  622. @result{} one
  623. @end group
  624. @group
  625. (set two 2) ; @r{@code{two} evaluates to symbol @code{one}.}
  626. @result{} 2
  627. @end group
  628. @group
  629. one ; @r{So it is @code{one} that was set.}
  630. @result{} 2
  631. (let ((one 1)) ; @r{This binding of @code{one} is set,}
  632. (set 'one 3) ; @r{not the global value.}
  633. one)
  634. @result{} 3
  635. @end group
  636. @group
  637. one
  638. @result{} 2
  639. @end group
  640. @end example
  641. If @var{symbol} is not actually a symbol, a @code{wrong-type-argument}
  642. error is signaled.
  643. @example
  644. (set '(x y) 'z)
  645. @error{} Wrong type argument: symbolp, (x y)
  646. @end example
  647. @end defun
  648. @node Variable Scoping
  649. @section Scoping Rules for Variable Bindings
  650. @cindex scoping rule
  651. When you create a local binding for a variable, that binding takes
  652. effect only within a limited portion of the program (@pxref{Local
  653. Variables}). This section describes exactly what this means.
  654. @cindex scope
  655. @cindex extent
  656. Each local binding has a certain @dfn{scope} and @dfn{extent}.
  657. @dfn{Scope} refers to @emph{where} in the textual source code the
  658. binding can be accessed. @dfn{Extent} refers to @emph{when}, as the
  659. program is executing, the binding exists.
  660. @cindex dynamic binding
  661. @cindex dynamic scope
  662. @cindex dynamic extent
  663. By default, the local bindings that Emacs creates are @dfn{dynamic
  664. bindings}. Such a binding has @dfn{dynamic scope}, meaning that any
  665. part of the program can potentially access the variable binding. It
  666. also has @dfn{dynamic extent}, meaning that the binding lasts only
  667. while the binding construct (such as the body of a @code{let} form) is
  668. being executed.
  669. @cindex lexical binding
  670. @cindex lexical scope
  671. @cindex indefinite extent
  672. Emacs can optionally create @dfn{lexical bindings}. A lexical
  673. binding has @dfn{lexical scope}, meaning that any reference to the
  674. variable must be located textually within the binding
  675. construct@footnote{With some exceptions; for instance, a lexical
  676. binding can also be accessed from the Lisp debugger.}. It also has
  677. @dfn{indefinite extent}, meaning that under some circumstances the
  678. binding can live on even after the binding construct has finished
  679. executing, by means of special objects called @dfn{closures}.
  680. The following subsections describe dynamic binding and lexical
  681. binding in greater detail, and how to enable lexical binding in Emacs
  682. Lisp programs.
  683. @menu
  684. * Dynamic Binding:: The default for binding local variables in Emacs.
  685. * Dynamic Binding Tips:: Avoiding problems with dynamic binding.
  686. * Lexical Binding:: A different type of local variable binding.
  687. * Using Lexical Binding:: How to enable lexical binding.
  688. @end menu
  689. @node Dynamic Binding
  690. @subsection Dynamic Binding
  691. By default, the local variable bindings made by Emacs are dynamic
  692. bindings. When a variable is dynamically bound, its current binding
  693. at any point in the execution of the Lisp program is simply the most
  694. recently-created dynamic local binding for that symbol, or the global
  695. binding if there is no such local binding.
  696. Dynamic bindings have dynamic scope and extent, as shown by the
  697. following example:
  698. @example
  699. @group
  700. (defvar x -99) ; @r{@code{x} receives an initial value of @minus{}99.}
  701. (defun getx ()
  702. x) ; @r{@code{x} is used free in this function.}
  703. (let ((x 1)) ; @r{@code{x} is dynamically bound.}
  704. (getx))
  705. @result{} 1
  706. ;; @r{After the @code{let} form finishes, @code{x} reverts to its}
  707. ;; @r{previous value, which is @minus{}99.}
  708. (getx)
  709. @result{} -99
  710. @end group
  711. @end example
  712. @noindent
  713. The function @code{getx} refers to @code{x}. This is a @dfn{free}
  714. reference, in the sense that there is no binding for @code{x} within
  715. that @code{defun} construct itself. When we call @code{getx} from
  716. within a @code{let} form in which @code{x} is (dynamically) bound, it
  717. retrieves the local value (i.e., 1). But when we call @code{getx}
  718. outside the @code{let} form, it retrieves the global value (i.e.,
  719. @minus{}99).
  720. Here is another example, which illustrates setting a dynamically
  721. bound variable using @code{setq}:
  722. @example
  723. @group
  724. (defvar x -99) ; @r{@code{x} receives an initial value of @minus{}99.}
  725. (defun addx ()
  726. (setq x (1+ x))) ; @r{Add 1 to @code{x} and return its new value.}
  727. (let ((x 1))
  728. (addx)
  729. (addx))
  730. @result{} 3 ; @r{The two @code{addx} calls add to @code{x} twice.}
  731. ;; @r{After the @code{let} form finishes, @code{x} reverts to its}
  732. ;; @r{previous value, which is @minus{}99.}
  733. (addx)
  734. @result{} -98
  735. @end group
  736. @end example
  737. Dynamic binding is implemented in Emacs Lisp in a simple way. Each
  738. symbol has a value cell, which specifies its current dynamic value (or
  739. absence of value). @xref{Symbol Components}. When a symbol is given
  740. a dynamic local binding, Emacs records the contents of the value cell
  741. (or absence thereof) in a stack, and stores the new local value in the
  742. value cell. When the binding construct finishes executing, Emacs pops
  743. the old value off the stack, and puts it in the value cell.
  744. @node Dynamic Binding Tips
  745. @subsection Proper Use of Dynamic Binding
  746. Dynamic binding is a powerful feature, as it allows programs to
  747. refer to variables that are not defined within their local textual
  748. scope. However, if used without restraint, this can also make
  749. programs hard to understand. There are two clean ways to use this
  750. technique:
  751. @itemize @bullet
  752. @item
  753. If a variable has no global definition, use it as a local variable
  754. only within a binding construct, such as the body of the @code{let}
  755. form where the variable was bound. If this convention is followed
  756. consistently throughout a program, the value of the variable will not
  757. affect, nor be affected by, any uses of the same variable symbol
  758. elsewhere in the program.
  759. @item
  760. Otherwise, define the variable with @code{defvar}, @code{defconst}, or
  761. @code{defcustom}. @xref{Defining Variables}. Usually, the definition
  762. should be at top-level in an Emacs Lisp file. As far as possible, it
  763. should include a documentation string which explains the meaning and
  764. purpose of the variable. You should also choose the variable's name
  765. to avoid name conflicts (@pxref{Coding Conventions}).
  766. Then you can bind the variable anywhere in a program, knowing reliably
  767. what the effect will be. Wherever you encounter the variable, it will
  768. be easy to refer back to the definition, e.g., via the @kbd{C-h v}
  769. command (provided the variable definition has been loaded into Emacs).
  770. @xref{Name Help,,, emacs, The GNU Emacs Manual}.
  771. For example, it is common to use local bindings for customizable
  772. variables like @code{case-fold-search}:
  773. @example
  774. @group
  775. (defun search-for-abc ()
  776. "Search for the string \"abc\", ignoring case differences."
  777. (let ((case-fold-search nil))
  778. (re-search-forward "abc")))
  779. @end group
  780. @end example
  781. @end itemize
  782. @node Lexical Binding
  783. @subsection Lexical Binding
  784. Lexical binding was introduced to Emacs, as an optional feature, in
  785. version 24.1. We expect its importance to increase in the future.
  786. Lexical binding opens up many more opportunities for optimization, so
  787. programs using it are likely to run faster in future Emacs versions.
  788. Lexical binding is also more compatible with concurrency, which we
  789. want to add to Emacs in the future.
  790. A lexically-bound variable has @dfn{lexical scope}, meaning that any
  791. reference to the variable must be located textually within the binding
  792. construct. Here is an example
  793. @iftex
  794. (see the next subsection, for how to actually enable lexical binding):
  795. @end iftex
  796. @ifnottex
  797. (@pxref{Using Lexical Binding}, for how to actually enable lexical binding):
  798. @end ifnottex
  799. @example
  800. @group
  801. (let ((x 1)) ; @r{@code{x} is lexically bound.}
  802. (+ x 3))
  803. @result{} 4
  804. (defun getx ()
  805. x) ; @r{@code{x} is used free in this function.}
  806. (let ((x 1)) ; @r{@code{x} is lexically bound.}
  807. (getx))
  808. @error{} Symbol's value as variable is void: x
  809. @end group
  810. @end example
  811. @noindent
  812. Here, the variable @code{x} has no global value. When it is lexically
  813. bound within a @code{let} form, it can be used in the textual confines
  814. of that @code{let} form. But it can @emph{not} be used from within a
  815. @code{getx} function called from the @code{let} form, since the
  816. function definition of @code{getx} occurs outside the @code{let} form
  817. itself.
  818. @cindex lexical environment
  819. Here is how lexical binding works. Each binding construct defines a
  820. @dfn{lexical environment}, specifying the symbols that are bound
  821. within the construct and their local values. When the Lisp evaluator
  822. wants the current value of a variable, it looks first in the lexical
  823. environment; if the variable is not specified in there, it looks in
  824. the symbol's value cell, where the dynamic value is stored.
  825. (Internally, the lexical environment is an alist of symbol-value
  826. pairs, with the final element in the alist being the symbol @code{t}
  827. rather than a cons cell. Such an alist can be passed as the second
  828. argument to the @code{eval} function, in order to specify a lexical
  829. environment in which to evaluate a form. @xref{Eval}. Most Emacs
  830. Lisp programs, however, should not interact directly with lexical
  831. environments in this way; only specialized programs like debuggers.)
  832. @cindex closures, example of using
  833. Lexical bindings have indefinite extent. Even after a binding
  834. construct has finished executing, its lexical environment can be
  835. ``kept around'' in Lisp objects called @dfn{closures}. A closure is
  836. created when you define a named or anonymous function with lexical
  837. binding enabled. @xref{Closures}, for details.
  838. When a closure is called as a function, any lexical variable
  839. references within its definition use the retained lexical environment.
  840. Here is an example:
  841. @example
  842. (defvar my-ticker nil) ; @r{We will use this dynamically bound}
  843. ; @r{variable to store a closure.}
  844. (let ((x 0)) ; @r{@code{x} is lexically bound.}
  845. (setq my-ticker (lambda ()
  846. (setq x (1+ x)))))
  847. @result{} (closure ((x . 0) t) ()
  848. (setq x (1+ x)))
  849. (funcall my-ticker)
  850. @result{} 1
  851. (funcall my-ticker)
  852. @result{} 2
  853. (funcall my-ticker)
  854. @result{} 3
  855. x ; @r{Note that @code{x} has no global value.}
  856. @error{} Symbol's value as variable is void: x
  857. @end example
  858. @noindent
  859. The @code{let} binding defines a lexical environment in which the
  860. variable @code{x} is locally bound to 0. Within this binding
  861. construct, we define a lambda expression which increments @code{x} by
  862. one and returns the incremented value. This lambda expression is
  863. automatically turned into a closure, in which the lexical environment
  864. lives on even after the @code{let} binding construct has exited. Each
  865. time we evaluate the closure, it increments @code{x}, using the
  866. binding of @code{x} in that lexical environment.
  867. Note that functions like @code{symbol-value}, @code{boundp}, and
  868. @code{set} only retrieve or modify a variable's dynamic binding
  869. (i.e., the contents of its symbol's value cell). Also, the code in
  870. the body of a @code{defun} or @code{defmacro} cannot refer to
  871. surrounding lexical variables.
  872. @node Using Lexical Binding
  873. @subsection Using Lexical Binding
  874. When loading an Emacs Lisp file or evaluating a Lisp buffer, lexical
  875. binding is enabled if the buffer-local variable @code{lexical-binding}
  876. is non-@code{nil}:
  877. @defvar lexical-binding
  878. If this buffer-local variable is non-@code{nil}, Emacs Lisp files and
  879. buffers are evaluated using lexical binding instead of dynamic
  880. binding. (However, special variables are still dynamically bound; see
  881. below.) If @code{nil}, dynamic binding is used for all local
  882. variables. This variable is typically set for a whole Emacs Lisp
  883. file, as a file local variable (@pxref{File Local Variables}).
  884. Note that unlike other such variables, this one must be set in the
  885. first line of a file.
  886. @end defvar
  887. @noindent
  888. When evaluating Emacs Lisp code directly using an @code{eval} call,
  889. lexical binding is enabled if the @var{lexical} argument to
  890. @code{eval} is non-@code{nil}. @xref{Eval}.
  891. @cindex special variables
  892. Even when lexical binding is enabled, certain variables will
  893. continue to be dynamically bound. These are called @dfn{special
  894. variables}. Every variable that has been defined with @code{defvar},
  895. @code{defcustom} or @code{defconst} is a special variable
  896. (@pxref{Defining Variables}). All other variables are subject to
  897. lexical binding.
  898. @defun special-variable-p symbol
  899. This function returns non-@code{nil} if @var{symbol} is a special
  900. variable (i.e., it has a @code{defvar}, @code{defcustom}, or
  901. @code{defconst} variable definition). Otherwise, the return value is
  902. @code{nil}.
  903. @end defun
  904. The use of a special variable as a formal argument in a function is
  905. discouraged. Doing so gives rise to unspecified behavior when lexical
  906. binding mode is enabled (it may use lexical binding sometimes, and
  907. dynamic binding other times).
  908. Converting an Emacs Lisp program to lexical binding is easy. First,
  909. add a file-local variable setting of @code{lexical-binding} to
  910. @code{t} in the header line of the Emacs Lisp source file (@pxref{File
  911. Local Variables}). Second, check that every variable in the program
  912. which needs to be dynamically bound has a variable definition, so that
  913. it is not inadvertently bound lexically.
  914. @cindex free variable
  915. @cindex unused lexical variable
  916. A simple way to find out which variables need a variable definition
  917. is to byte-compile the source file. @xref{Byte Compilation}. If a
  918. non-special variable is used outside of a @code{let} form, the
  919. byte-compiler will warn about reference or assignment to a free
  920. variable. If a non-special variable is bound but not used within a
  921. @code{let} form, the byte-compiler will warn about an unused lexical
  922. variable. The byte-compiler will also issue a warning if you use a
  923. special variable as a function argument.
  924. (To silence byte-compiler warnings about unused variables, just use
  925. a variable name that start with an underscore. The byte-compiler
  926. interprets this as an indication that this is a variable known not to
  927. be used.)
  928. @node Buffer-Local Variables
  929. @section Buffer-Local Variables
  930. @cindex variable, buffer-local
  931. @cindex buffer-local variables
  932. Global and local variable bindings are found in most programming
  933. languages in one form or another. Emacs, however, also supports
  934. additional, unusual kinds of variable binding, such as
  935. @dfn{buffer-local} bindings, which apply only in one buffer. Having
  936. different values for a variable in different buffers is an important
  937. customization method. (Variables can also have bindings that are
  938. local to each terminal. @xref{Multiple Terminals}.)
  939. @menu
  940. * Intro to Buffer-Local:: Introduction and concepts.
  941. * Creating Buffer-Local:: Creating and destroying buffer-local bindings.
  942. * Default Value:: The default value is seen in buffers
  943. that don't have their own buffer-local values.
  944. @end menu
  945. @node Intro to Buffer-Local
  946. @subsection Introduction to Buffer-Local Variables
  947. A buffer-local variable has a buffer-local binding associated with a
  948. particular buffer. The binding is in effect when that buffer is
  949. current; otherwise, it is not in effect. If you set the variable while
  950. a buffer-local binding is in effect, the new value goes in that binding,
  951. so its other bindings are unchanged. This means that the change is
  952. visible only in the buffer where you made it.
  953. The variable's ordinary binding, which is not associated with any
  954. specific buffer, is called the @dfn{default binding}. In most cases,
  955. this is the global binding.
  956. A variable can have buffer-local bindings in some buffers but not in
  957. other buffers. The default binding is shared by all the buffers that
  958. don't have their own bindings for the variable. (This includes all
  959. newly-created buffers.) If you set the variable in a buffer that does
  960. not have a buffer-local binding for it, this sets the default binding,
  961. so the new value is visible in all the buffers that see the default
  962. binding.
  963. The most common use of buffer-local bindings is for major modes to change
  964. variables that control the behavior of commands. For example, C mode and
  965. Lisp mode both set the variable @code{paragraph-start} to specify that only
  966. blank lines separate paragraphs. They do this by making the variable
  967. buffer-local in the buffer that is being put into C mode or Lisp mode, and
  968. then setting it to the new value for that mode. @xref{Major Modes}.
  969. The usual way to make a buffer-local binding is with
  970. @code{make-local-variable}, which is what major mode commands typically
  971. use. This affects just the current buffer; all other buffers (including
  972. those yet to be created) will continue to share the default value unless
  973. they are explicitly given their own buffer-local bindings.
  974. @cindex automatically buffer-local
  975. A more powerful operation is to mark the variable as
  976. @dfn{automatically buffer-local} by calling
  977. @code{make-variable-buffer-local}. You can think of this as making the
  978. variable local in all buffers, even those yet to be created. More
  979. precisely, the effect is that setting the variable automatically makes
  980. the variable local to the current buffer if it is not already so. All
  981. buffers start out by sharing the default value of the variable as usual,
  982. but setting the variable creates a buffer-local binding for the current
  983. buffer. The new value is stored in the buffer-local binding, leaving
  984. the default binding untouched. This means that the default value cannot
  985. be changed with @code{setq} in any buffer; the only way to change it is
  986. with @code{setq-default}.
  987. @strong{Warning:} When a variable has buffer-local
  988. bindings in one or more buffers, @code{let} rebinds the binding that's
  989. currently in effect. For instance, if the current buffer has a
  990. buffer-local value, @code{let} temporarily rebinds that. If no
  991. buffer-local bindings are in effect, @code{let} rebinds
  992. the default value. If inside the @code{let} you then change to a
  993. different current buffer in which a different binding is in effect,
  994. you won't see the @code{let} binding any more. And if you exit the
  995. @code{let} while still in the other buffer, you won't see the
  996. unbinding occur (though it will occur properly). Here is an example
  997. to illustrate:
  998. @example
  999. @group
  1000. (setq foo 'g)
  1001. (set-buffer "a")
  1002. (make-local-variable 'foo)
  1003. @end group
  1004. (setq foo 'a)
  1005. (let ((foo 'temp))
  1006. ;; foo @result{} 'temp ; @r{let binding in buffer @samp{a}}
  1007. (set-buffer "b")
  1008. ;; foo @result{} 'g ; @r{the global value since foo is not local in @samp{b}}
  1009. @var{body}@dots{})
  1010. @group
  1011. foo @result{} 'g ; @r{exiting restored the local value in buffer @samp{a},}
  1012. ; @r{but we don't see that in buffer @samp{b}}
  1013. @end group
  1014. @group
  1015. (set-buffer "a") ; @r{verify the local value was restored}
  1016. foo @result{} 'a
  1017. @end group
  1018. @end example
  1019. @noindent
  1020. Note that references to @code{foo} in @var{body} access the
  1021. buffer-local binding of buffer @samp{b}.
  1022. When a file specifies local variable values, these become buffer-local
  1023. values when you visit the file. @xref{File Variables,,, emacs, The
  1024. GNU Emacs Manual}.
  1025. A buffer-local variable cannot be made terminal-local
  1026. (@pxref{Multiple Terminals}).
  1027. @node Creating Buffer-Local
  1028. @subsection Creating and Deleting Buffer-Local Bindings
  1029. @deffn Command make-local-variable variable
  1030. This function creates a buffer-local binding in the current buffer for
  1031. @var{variable} (a symbol). Other buffers are not affected. The value
  1032. returned is @var{variable}.
  1033. The buffer-local value of @var{variable} starts out as the same value
  1034. @var{variable} previously had. If @var{variable} was void, it remains
  1035. void.
  1036. @example
  1037. @group
  1038. ;; @r{In buffer @samp{b1}:}
  1039. (setq foo 5) ; @r{Affects all buffers.}
  1040. @result{} 5
  1041. @end group
  1042. @group
  1043. (make-local-variable 'foo) ; @r{Now it is local in @samp{b1}.}
  1044. @result{} foo
  1045. @end group
  1046. @group
  1047. foo ; @r{That did not change}
  1048. @result{} 5 ; @r{the value.}
  1049. @end group
  1050. @group
  1051. (setq foo 6) ; @r{Change the value}
  1052. @result{} 6 ; @r{in @samp{b1}.}
  1053. @end group
  1054. @group
  1055. foo
  1056. @result{} 6
  1057. @end group
  1058. @group
  1059. ;; @r{In buffer @samp{b2}, the value hasn't changed.}
  1060. (with-current-buffer "b2"
  1061. foo)
  1062. @result{} 5
  1063. @end group
  1064. @end example
  1065. Making a variable buffer-local within a @code{let}-binding for that
  1066. variable does not work reliably, unless the buffer in which you do this
  1067. is not current either on entry to or exit from the @code{let}. This is
  1068. because @code{let} does not distinguish between different kinds of
  1069. bindings; it knows only which variable the binding was made for.
  1070. If the variable is terminal-local (@pxref{Multiple Terminals}), this
  1071. function signals an error. Such variables cannot have buffer-local
  1072. bindings as well.
  1073. @strong{Warning:} do not use @code{make-local-variable} for a hook
  1074. variable. The hook variables are automatically made buffer-local as
  1075. needed if you use the @var{local} argument to @code{add-hook} or
  1076. @code{remove-hook}.
  1077. @end deffn
  1078. @defmac setq-local variable value
  1079. This macro creates a buffer-local binding in the current buffer for
  1080. @var{variable}, and gives it the buffer-local value @var{value}. It
  1081. is equivalent to calling @code{make-local-variable} followed by
  1082. @code{setq}. @var{variable} should be an unquoted symbol.
  1083. @end defmac
  1084. @deffn Command make-variable-buffer-local variable
  1085. This function marks @var{variable} (a symbol) automatically
  1086. buffer-local, so that any subsequent attempt to set it will make it
  1087. local to the current buffer at the time. Unlike
  1088. @code{make-local-variable}, with which it is often confused, this
  1089. cannot be undone, and affects the behavior of the variable in all
  1090. buffers.
  1091. A peculiar wrinkle of this feature is that binding the variable (with
  1092. @code{let} or other binding constructs) does not create a buffer-local
  1093. binding for it. Only setting the variable (with @code{set} or
  1094. @code{setq}), while the variable does not have a @code{let}-style
  1095. binding that was made in the current buffer, does so.
  1096. If @var{variable} does not have a default value, then calling this
  1097. command will give it a default value of @code{nil}. If @var{variable}
  1098. already has a default value, that value remains unchanged.
  1099. Subsequently calling @code{makunbound} on @var{variable} will result
  1100. in a void buffer-local value and leave the default value unaffected.
  1101. The value returned is @var{variable}.
  1102. @strong{Warning:} Don't assume that you should use
  1103. @code{make-variable-buffer-local} for user-option variables, simply
  1104. because users @emph{might} want to customize them differently in
  1105. different buffers. Users can make any variable local, when they wish
  1106. to. It is better to leave the choice to them.
  1107. The time to use @code{make-variable-buffer-local} is when it is crucial
  1108. that no two buffers ever share the same binding. For example, when a
  1109. variable is used for internal purposes in a Lisp program which depends
  1110. on having separate values in separate buffers, then using
  1111. @code{make-variable-buffer-local} can be the best solution.
  1112. @end deffn
  1113. @defmac defvar-local variable value &optional docstring
  1114. This macro defines @var{variable} as a variable with initial value
  1115. @var{value} and @var{docstring}, and marks it as automatically
  1116. buffer-local. It is equivalent to calling @code{defvar} followed by
  1117. @code{make-variable-buffer-local}. @var{variable} should be an
  1118. unquoted symbol.
  1119. @end defmac
  1120. @defun local-variable-p variable &optional buffer
  1121. This returns @code{t} if @var{variable} is buffer-local in buffer
  1122. @var{buffer} (which defaults to the current buffer); otherwise,
  1123. @code{nil}.
  1124. @end defun
  1125. @defun local-variable-if-set-p variable &optional buffer
  1126. This returns @code{t} if @var{variable} either has a buffer-local
  1127. value in buffer @var{buffer}, or is automatically buffer-local.
  1128. Otherwise, it returns @code{nil}. If omitted or @code{nil},
  1129. @var{buffer} defaults to the current buffer.
  1130. @end defun
  1131. @defun buffer-local-value variable buffer
  1132. This function returns the buffer-local binding of @var{variable} (a
  1133. symbol) in buffer @var{buffer}. If @var{variable} does not have a
  1134. buffer-local binding in buffer @var{buffer}, it returns the default
  1135. value (@pxref{Default Value}) of @var{variable} instead.
  1136. @end defun
  1137. @defun buffer-local-variables &optional buffer
  1138. This function returns a list describing the buffer-local variables in
  1139. buffer @var{buffer}. (If @var{buffer} is omitted, the current buffer
  1140. is used.) Normally, each list element has the form
  1141. @w{@code{(@var{sym} . @var{val})}}, where @var{sym} is a buffer-local
  1142. variable (a symbol) and @var{val} is its buffer-local value. But when
  1143. a variable's buffer-local binding in @var{buffer} is void, its list
  1144. element is just @var{sym}.
  1145. @example
  1146. @group
  1147. (make-local-variable 'foobar)
  1148. (makunbound 'foobar)
  1149. (make-local-variable 'bind-me)
  1150. (setq bind-me 69)
  1151. @end group
  1152. (setq lcl (buffer-local-variables))
  1153. ;; @r{First, built-in variables local in all buffers:}
  1154. @result{} ((mark-active . nil)
  1155. (buffer-undo-list . nil)
  1156. (mode-name . "Fundamental")
  1157. @dots{}
  1158. @group
  1159. ;; @r{Next, non-built-in buffer-local variables.}
  1160. ;; @r{This one is buffer-local and void:}
  1161. foobar
  1162. ;; @r{This one is buffer-local and nonvoid:}
  1163. (bind-me . 69))
  1164. @end group
  1165. @end example
  1166. Note that storing new values into the @sc{cdr}s of cons cells in this
  1167. list does @emph{not} change the buffer-local values of the variables.
  1168. @end defun
  1169. @deffn Command kill-local-variable variable
  1170. This function deletes the buffer-local binding (if any) for
  1171. @var{variable} (a symbol) in the current buffer. As a result, the
  1172. default binding of @var{variable} becomes visible in this buffer. This
  1173. typically results in a change in the value of @var{variable}, since the
  1174. default value is usually different from the buffer-local value just
  1175. eliminated.
  1176. If you kill the buffer-local binding of a variable that automatically
  1177. becomes buffer-local when set, this makes the default value visible in
  1178. the current buffer. However, if you set the variable again, that will
  1179. once again create a buffer-local binding for it.
  1180. @code{kill-local-variable} returns @var{variable}.
  1181. This function is a command because it is sometimes useful to kill one
  1182. buffer-local variable interactively, just as it is useful to create
  1183. buffer-local variables interactively.
  1184. @end deffn
  1185. @cindex local variables, killed by major mode
  1186. @defun kill-all-local-variables
  1187. This function eliminates all the buffer-local variable bindings of the
  1188. current buffer except for variables marked as permanent and local
  1189. hook functions that have a non-@code{nil} @code{permanent-local-hook}
  1190. property (@pxref{Setting Hooks}). As a result, the buffer will see
  1191. the default values of most variables.
  1192. This function also resets certain other information pertaining to the
  1193. buffer: it sets the local keymap to @code{nil}, the syntax table to the
  1194. value of @code{(standard-syntax-table)}, the case table to
  1195. @code{(standard-case-table)}, and the abbrev table to the value of
  1196. @code{fundamental-mode-abbrev-table}.
  1197. The very first thing this function does is run the normal hook
  1198. @code{change-major-mode-hook} (see below).
  1199. Every major mode command begins by calling this function, which has the
  1200. effect of switching to Fundamental mode and erasing most of the effects
  1201. of the previous major mode. To ensure that this does its job, the
  1202. variables that major modes set should not be marked permanent.
  1203. @code{kill-all-local-variables} returns @code{nil}.
  1204. @end defun
  1205. @defvar change-major-mode-hook
  1206. The function @code{kill-all-local-variables} runs this normal hook
  1207. before it does anything else. This gives major modes a way to arrange
  1208. for something special to be done if the user switches to a different
  1209. major mode. It is also useful for buffer-specific minor modes
  1210. that should be forgotten if the user changes the major mode.
  1211. For best results, make this variable buffer-local, so that it will
  1212. disappear after doing its job and will not interfere with the
  1213. subsequent major mode. @xref{Hooks}.
  1214. @end defvar
  1215. @cindex permanent local variable
  1216. A buffer-local variable is @dfn{permanent} if the variable name (a
  1217. symbol) has a @code{permanent-local} property that is non-@code{nil}.
  1218. Such variables are unaffected by @code{kill-all-local-variables}, and
  1219. their local bindings are therefore not cleared by changing major modes.
  1220. Permanent locals are appropriate for data pertaining to where the file
  1221. came from or how to save it, rather than with how to edit the contents.
  1222. @node Default Value
  1223. @subsection The Default Value of a Buffer-Local Variable
  1224. @cindex default value
  1225. The global value of a variable with buffer-local bindings is also
  1226. called the @dfn{default} value, because it is the value that is in
  1227. effect whenever neither the current buffer nor the selected frame has
  1228. its own binding for the variable.
  1229. The functions @code{default-value} and @code{setq-default} access and
  1230. change a variable's default value regardless of whether the current
  1231. buffer has a buffer-local binding. For example, you could use
  1232. @code{setq-default} to change the default setting of
  1233. @code{paragraph-start} for most buffers; and this would work even when
  1234. you are in a C or Lisp mode buffer that has a buffer-local value for
  1235. this variable.
  1236. @c Emacs 19 feature
  1237. The special forms @code{defvar} and @code{defconst} also set the
  1238. default value (if they set the variable at all), rather than any
  1239. buffer-local value.
  1240. @defun default-value symbol
  1241. This function returns @var{symbol}'s default value. This is the value
  1242. that is seen in buffers and frames that do not have their own values for
  1243. this variable. If @var{symbol} is not buffer-local, this is equivalent
  1244. to @code{symbol-value} (@pxref{Accessing Variables}).
  1245. @end defun
  1246. @c Emacs 19 feature
  1247. @defun default-boundp symbol
  1248. The function @code{default-boundp} tells you whether @var{symbol}'s
  1249. default value is nonvoid. If @code{(default-boundp 'foo)} returns
  1250. @code{nil}, then @code{(default-value 'foo)} would get an error.
  1251. @code{default-boundp} is to @code{default-value} as @code{boundp} is to
  1252. @code{symbol-value}.
  1253. @end defun
  1254. @defspec setq-default [symbol form]@dots{}
  1255. This special form gives each @var{symbol} a new default value, which is
  1256. the result of evaluating the corresponding @var{form}. It does not
  1257. evaluate @var{symbol}, but does evaluate @var{form}. The value of the
  1258. @code{setq-default} form is the value of the last @var{form}.
  1259. If a @var{symbol} is not buffer-local for the current buffer, and is not
  1260. marked automatically buffer-local, @code{setq-default} has the same
  1261. effect as @code{setq}. If @var{symbol} is buffer-local for the current
  1262. buffer, then this changes the value that other buffers will see (as long
  1263. as they don't have a buffer-local value), but not the value that the
  1264. current buffer sees.
  1265. @example
  1266. @group
  1267. ;; @r{In buffer @samp{foo}:}
  1268. (make-local-variable 'buffer-local)
  1269. @result{} buffer-local
  1270. @end group
  1271. @group
  1272. (setq buffer-local 'value-in-foo)
  1273. @result{} value-in-foo
  1274. @end group
  1275. @group
  1276. (setq-default buffer-local 'new-default)
  1277. @result{} new-default
  1278. @end group
  1279. @group
  1280. buffer-local
  1281. @result{} value-in-foo
  1282. @end group
  1283. @group
  1284. (default-value 'buffer-local)
  1285. @result{} new-default
  1286. @end group
  1287. @group
  1288. ;; @r{In (the new) buffer @samp{bar}:}
  1289. buffer-local
  1290. @result{} new-default
  1291. @end group
  1292. @group
  1293. (default-value 'buffer-local)
  1294. @result{} new-default
  1295. @end group
  1296. @group
  1297. (setq buffer-local 'another-default)
  1298. @result{} another-default
  1299. @end group
  1300. @group
  1301. (default-value 'buffer-local)
  1302. @result{} another-default
  1303. @end group
  1304. @group
  1305. ;; @r{Back in buffer @samp{foo}:}
  1306. buffer-local
  1307. @result{} value-in-foo
  1308. (default-value 'buffer-local)
  1309. @result{} another-default
  1310. @end group
  1311. @end example
  1312. @end defspec
  1313. @defun set-default symbol value
  1314. This function is like @code{setq-default}, except that @var{symbol} is
  1315. an ordinary evaluated argument.
  1316. @example
  1317. @group
  1318. (set-default (car '(a b c)) 23)
  1319. @result{} 23
  1320. @end group
  1321. @group
  1322. (default-value 'a)
  1323. @result{} 23
  1324. @end group
  1325. @end example
  1326. @end defun
  1327. @node File Local Variables
  1328. @section File Local Variables
  1329. @cindex file local variables
  1330. A file can specify local variable values; Emacs uses these to create
  1331. buffer-local bindings for those variables in the buffer visiting that
  1332. file. @xref{File Variables, , Local Variables in Files, emacs, The
  1333. GNU Emacs Manual}, for basic information about file-local variables.
  1334. This section describes the functions and variables that affect how
  1335. file-local variables are processed.
  1336. If a file-local variable could specify an arbitrary function or Lisp
  1337. expression that would be called later, visiting a file could take over
  1338. your Emacs. Emacs protects against this by automatically setting only
  1339. those file-local variables whose specified values are known to be
  1340. safe. Other file-local variables are set only if the user agrees.
  1341. For additional safety, @code{read-circle} is temporarily bound to
  1342. @code{nil} when Emacs reads file-local variables (@pxref{Input
  1343. Functions}). This prevents the Lisp reader from recognizing circular
  1344. and shared Lisp structures (@pxref{Circular Objects}).
  1345. @defopt enable-local-variables
  1346. This variable controls whether to process file-local variables.
  1347. The possible values are:
  1348. @table @asis
  1349. @item @code{t} (the default)
  1350. Set the safe variables, and query (once) about any unsafe variables.
  1351. @item @code{:safe}
  1352. Set only the safe variables and do not query.
  1353. @item @code{:all}
  1354. Set all the variables and do not query.
  1355. @item @code{nil}
  1356. Don't set any variables.
  1357. @item anything else
  1358. Query (once) about all the variables.
  1359. @end table
  1360. @end defopt
  1361. @defvar inhibit-local-variables-regexps
  1362. This is a list of regular expressions. If a file has a name
  1363. matching an element of this list, then it is not scanned for
  1364. any form of file-local variable. For examples of why you might want
  1365. to use this, @pxref{Auto Major Mode}.
  1366. @end defvar
  1367. @defun hack-local-variables &optional mode-only
  1368. This function parses, and binds or evaluates as appropriate, any local
  1369. variables specified by the contents of the current buffer. The variable
  1370. @code{enable-local-variables} has its effect here. However, this
  1371. function does not look for the @samp{mode:} local variable in the
  1372. @w{@samp{-*-}} line. @code{set-auto-mode} does that, also taking
  1373. @code{enable-local-variables} into account (@pxref{Auto Major Mode}).
  1374. This function works by walking the alist stored in
  1375. @code{file-local-variables-alist} and applying each local variable in
  1376. turn. It calls @code{before-hack-local-variables-hook} and
  1377. @code{hack-local-variables-hook} before and after applying the
  1378. variables, respectively. It only calls the before-hook if the alist
  1379. is non-@code{nil}; it always calls the other hook. This
  1380. function ignores a @samp{mode} element if it specifies the same major
  1381. mode as the buffer already has.
  1382. If the optional argument @var{mode-only} is non-@code{nil}, then all
  1383. this function does is return a symbol specifying the major mode,
  1384. if the @w{@samp{-*-}} line or the local variables list specifies one,
  1385. and @code{nil} otherwise. It does not set the mode nor any other
  1386. file-local variable.
  1387. @end defun
  1388. @defvar file-local-variables-alist
  1389. This buffer-local variable holds the alist of file-local variable
  1390. settings. Each element of the alist is of the form
  1391. @w{@code{(@var{var} . @var{value})}}, where @var{var} is a symbol of
  1392. the local variable and @var{value} is its value. When Emacs visits a
  1393. file, it first collects all the file-local variables into this alist,
  1394. and then the @code{hack-local-variables} function applies them one by
  1395. one.
  1396. @end defvar
  1397. @defvar before-hack-local-variables-hook
  1398. Emacs calls this hook immediately before applying file-local variables
  1399. stored in @code{file-local-variables-alist}.
  1400. @end defvar
  1401. @defvar hack-local-variables-hook
  1402. Emacs calls this hook immediately after it finishes applying
  1403. file-local variables stored in @code{file-local-variables-alist}.
  1404. @end defvar
  1405. @cindex safe local variable
  1406. You can specify safe values for a variable with a
  1407. @code{safe-local-variable} property. The property has to be a
  1408. function of one argument; any value is safe if the function returns
  1409. non-@code{nil} given that value. Many commonly-encountered file
  1410. variables have @code{safe-local-variable} properties; these include
  1411. @code{fill-column}, @code{fill-prefix}, and @code{indent-tabs-mode}.
  1412. For boolean-valued variables that are safe, use @code{booleanp} as the
  1413. property value.
  1414. When defining a user option using @code{defcustom}, you can set its
  1415. @code{safe-local-variable} property by adding the arguments
  1416. @code{:safe @var{function}} to @code{defcustom} (@pxref{Variable
  1417. Definitions}).
  1418. @defopt safe-local-variable-values
  1419. This variable provides another way to mark some variable values as
  1420. safe. It is a list of cons cells @code{(@var{var} . @var{val})},
  1421. where @var{var} is a variable name and @var{val} is a value which is
  1422. safe for that variable.
  1423. When Emacs asks the user whether or not to obey a set of file-local
  1424. variable specifications, the user can choose to mark them as safe.
  1425. Doing so adds those variable/value pairs to
  1426. @code{safe-local-variable-values}, and saves it to the user's custom
  1427. file.
  1428. @end defopt
  1429. @defun safe-local-variable-p sym val
  1430. This function returns non-@code{nil} if it is safe to give @var{sym}
  1431. the value @var{val}, based on the above criteria.
  1432. @end defun
  1433. @c @cindex risky local variable Duplicates risky-local-variable
  1434. Some variables are considered @dfn{risky}. If a variable is risky,
  1435. it is never entered automatically into
  1436. @code{safe-local-variable-values}; Emacs always queries before setting
  1437. a risky variable, unless the user explicitly allows a value by
  1438. customizing @code{safe-local-variable-values} directly.
  1439. Any variable whose name has a non-@code{nil}
  1440. @code{risky-local-variable} property is considered risky. When you
  1441. define a user option using @code{defcustom}, you can set its
  1442. @code{risky-local-variable} property by adding the arguments
  1443. @code{:risky @var{value}} to @code{defcustom} (@pxref{Variable
  1444. Definitions}). In addition, any variable whose name ends in any of
  1445. @samp{-command}, @samp{-frame-alist}, @samp{-function},
  1446. @samp{-functions}, @samp{-hook}, @samp{-hooks}, @samp{-form},
  1447. @samp{-forms}, @samp{-map}, @samp{-map-alist}, @samp{-mode-alist},
  1448. @samp{-program}, or @samp{-predicate} is automatically considered
  1449. risky. The variables @samp{font-lock-keywords},
  1450. @samp{font-lock-keywords} followed by a digit, and
  1451. @samp{font-lock-syntactic-keywords} are also considered risky.
  1452. @defun risky-local-variable-p sym
  1453. This function returns non-@code{nil} if @var{sym} is a risky variable,
  1454. based on the above criteria.
  1455. @end defun
  1456. @defvar ignored-local-variables
  1457. This variable holds a list of variables that should not be given local
  1458. values by files. Any value specified for one of these variables is
  1459. completely ignored.
  1460. @end defvar
  1461. The @samp{Eval:} ``variable'' is also a potential loophole, so Emacs
  1462. normally asks for confirmation before handling it.
  1463. @defopt enable-local-eval
  1464. This variable controls processing of @samp{Eval:} in @samp{-*-} lines
  1465. or local variables
  1466. lists in files being visited. A value of @code{t} means process them
  1467. unconditionally; @code{nil} means ignore them; anything else means ask
  1468. the user what to do for each file. The default value is @code{maybe}.
  1469. @end defopt
  1470. @defopt safe-local-eval-forms
  1471. This variable holds a list of expressions that are safe to
  1472. evaluate when found in the @samp{Eval:} ``variable'' in a file
  1473. local variables list.
  1474. @end defopt
  1475. If the expression is a function call and the function has a
  1476. @code{safe-local-eval-function} property, the property value
  1477. determines whether the expression is safe to evaluate. The property
  1478. value can be a predicate to call to test the expression, a list of
  1479. such predicates (it's safe if any predicate succeeds), or @code{t}
  1480. (always safe provided the arguments are constant).
  1481. Text properties are also potential loopholes, since their values
  1482. could include functions to call. So Emacs discards all text
  1483. properties from string values specified for file-local variables.
  1484. @node Directory Local Variables
  1485. @section Directory Local Variables
  1486. @cindex directory local variables
  1487. A directory can specify local variable values common to all files in
  1488. that directory; Emacs uses these to create buffer-local bindings for
  1489. those variables in buffers visiting any file in that directory. This
  1490. is useful when the files in the directory belong to some @dfn{project}
  1491. and therefore share the same local variables.
  1492. There are two different methods for specifying directory local
  1493. variables: by putting them in a special file, or by defining a
  1494. @dfn{project class} for that directory.
  1495. @defvr Constant dir-locals-file
  1496. This constant is the name of the file where Emacs expects to find the
  1497. directory-local variables. The name of the file is
  1498. @file{.dir-locals.el}@footnote{
  1499. The MS-DOS version of Emacs uses @file{_dir-locals.el} instead, due to
  1500. limitations of the DOS filesystems.
  1501. }. A file by that name in a directory causes Emacs to apply its
  1502. settings to any file in that directory or any of its subdirectories
  1503. (optionally, you can exclude subdirectories; see below).
  1504. If some of the subdirectories have their own @file{.dir-locals.el}
  1505. files, Emacs uses the settings from the deepest file it finds starting
  1506. from the file's directory and moving up the directory tree. The file
  1507. specifies local variables as a specially formatted list; see
  1508. @ref{Directory Variables, , Per-directory Local Variables, emacs, The
  1509. GNU Emacs Manual}, for more details.
  1510. @end defvr
  1511. @defun hack-dir-local-variables
  1512. This function reads the @code{.dir-locals.el} file and stores the
  1513. directory-local variables in @code{file-local-variables-alist} that is
  1514. local to the buffer visiting any file in the directory, without
  1515. applying them. It also stores the directory-local settings in
  1516. @code{dir-locals-class-alist}, where it defines a special class for
  1517. the directory in which @file{.dir-locals.el} file was found. This
  1518. function works by calling @code{dir-locals-set-class-variables} and
  1519. @code{dir-locals-set-directory-class}, described below.
  1520. @end defun
  1521. @defun hack-dir-local-variables-non-file-buffer
  1522. This function looks for directory-local variables, and immediately
  1523. applies them in the current buffer. It is intended to be called in
  1524. the mode commands for non-file buffers, such as Dired buffers, to let
  1525. them obey directory-local variable settings. For non-file buffers,
  1526. Emacs looks for directory-local variables in @code{default-directory}
  1527. and its parent directories.
  1528. @end defun
  1529. @defun dir-locals-set-class-variables class variables
  1530. This function defines a set of variable settings for the named
  1531. @var{class}, which is a symbol. You can later assign the class to one
  1532. or more directories, and Emacs will apply those variable settings to
  1533. all files in those directories. The list in @var{variables} can be of
  1534. one of the two forms: @code{(@var{major-mode} . @var{alist})} or
  1535. @code{(@var{directory} . @var{list})}. With the first form, if the
  1536. file's buffer turns on a mode that is derived from @var{major-mode},
  1537. then the all the variables in the associated @var{alist} are applied;
  1538. @var{alist} should be of the form @code{(@var{name} . @var{value})}.
  1539. A special value @code{nil} for @var{major-mode} means the settings are
  1540. applicable to any mode. In @var{alist}, you can use a special
  1541. @var{name}: @code{subdirs}. If the associated value is
  1542. @code{nil}, the alist is only applied to files in the relevant
  1543. directory, not to those in any subdirectories.
  1544. With the second form of @var{variables}, if @var{directory} is the
  1545. initial substring of the file's directory, then @var{list} is applied
  1546. recursively by following the above rules; @var{list} should be of one
  1547. of the two forms accepted by this function in @var{variables}.
  1548. @end defun
  1549. @defun dir-locals-set-directory-class directory class &optional mtime
  1550. This function assigns @var{class} to all the files in @code{directory}
  1551. and its subdirectories. Thereafter, all the variable settings
  1552. specified for @var{class} will be applied to any visited file in
  1553. @var{directory} and its children. @var{class} must have been already
  1554. defined by @code{dir-locals-set-class-variables}.
  1555. Emacs uses this function internally when it loads directory variables
  1556. from a @code{.dir-locals.el} file. In that case, the optional
  1557. argument @var{mtime} holds the file modification time (as returned by
  1558. @code{file-attributes}). Emacs uses this time to check stored
  1559. local variables are still valid. If you are assigning a class
  1560. directly, not via a file, this argument should be @code{nil}.
  1561. @end defun
  1562. @defvar dir-locals-class-alist
  1563. This alist holds the class symbols and the associated variable
  1564. settings. It is updated by @code{dir-locals-set-class-variables}.
  1565. @end defvar
  1566. @defvar dir-locals-directory-cache
  1567. This alist holds directory names, their assigned class names, and
  1568. modification times of the associated directory local variables file
  1569. (if there is one). The function @code{dir-locals-set-directory-class}
  1570. updates this list.
  1571. @end defvar
  1572. @defvar enable-dir-local-variables
  1573. If @code{nil}, directory-local variables are ignored. This variable
  1574. may be useful for modes that want to ignore directory-locals while
  1575. still respecting file-local variables (@pxref{File Local Variables}).
  1576. @end defvar
  1577. @node Variable Aliases
  1578. @section Variable Aliases
  1579. @cindex variable aliases
  1580. @cindex alias, for variables
  1581. It is sometimes useful to make two variables synonyms, so that both
  1582. variables always have the same value, and changing either one also
  1583. changes the other. Whenever you change the name of a
  1584. variable---either because you realize its old name was not well
  1585. chosen, or because its meaning has partly changed---it can be useful
  1586. to keep the old name as an @emph{alias} of the new one for
  1587. compatibility. You can do this with @code{defvaralias}.
  1588. @defun defvaralias new-alias base-variable &optional docstring
  1589. This function defines the symbol @var{new-alias} as a variable alias
  1590. for symbol @var{base-variable}. This means that retrieving the value
  1591. of @var{new-alias} returns the value of @var{base-variable}, and
  1592. changing the value of @var{new-alias} changes the value of
  1593. @var{base-variable}. The two aliased variable names always share the
  1594. same value and the same bindings.
  1595. If the @var{docstring} argument is non-@code{nil}, it specifies the
  1596. documentation for @var{new-alias}; otherwise, the alias gets the same
  1597. documentation as @var{base-variable} has, if any, unless
  1598. @var{base-variable} is itself an alias, in which case @var{new-alias} gets
  1599. the documentation of the variable at the end of the chain of aliases.
  1600. This function returns @var{base-variable}.
  1601. @end defun
  1602. Variable aliases are convenient for replacing an old name for a
  1603. variable with a new name. @code{make-obsolete-variable} declares that
  1604. the old name is obsolete and therefore that it may be removed at some
  1605. stage in the future.
  1606. @defun make-obsolete-variable obsolete-name current-name when &optional access-type
  1607. This function makes the byte compiler warn that the variable
  1608. @var{obsolete-name} is obsolete. If @var{current-name} is a symbol,
  1609. it is the variable's new name; then the warning message says to use
  1610. @var{current-name} instead of @var{obsolete-name}. If
  1611. @var{current-name} is a string, this is the message and there is no
  1612. replacement variable. @var{when} should be a string indicating when
  1613. the variable was first made obsolete (usually a version number
  1614. string).
  1615. The optional argument @var{access-type}, if non-@code{nil}, should
  1616. specify the kind of access that will trigger obsolescence warnings; it
  1617. can be either @code{get} or @code{set}.
  1618. @end defun
  1619. You can make two variables synonyms and declare one obsolete at the
  1620. same time using the macro @code{define-obsolete-variable-alias}.
  1621. @defmac define-obsolete-variable-alias obsolete-name current-name &optional when docstring
  1622. This macro marks the variable @var{obsolete-name} as obsolete and also
  1623. makes it an alias for the variable @var{current-name}. It is
  1624. equivalent to the following:
  1625. @example
  1626. (defvaralias @var{obsolete-name} @var{current-name} @var{docstring})
  1627. (make-obsolete-variable @var{obsolete-name} @var{current-name} @var{when})
  1628. @end example
  1629. @end defmac
  1630. @defun indirect-variable variable
  1631. This function returns the variable at the end of the chain of aliases
  1632. of @var{variable}. If @var{variable} is not a symbol, or if @var{variable} is
  1633. not defined as an alias, the function returns @var{variable}.
  1634. This function signals a @code{cyclic-variable-indirection} error if
  1635. there is a loop in the chain of symbols.
  1636. @end defun
  1637. @example
  1638. (defvaralias 'foo 'bar)
  1639. (indirect-variable 'foo)
  1640. @result{} bar
  1641. (indirect-variable 'bar)
  1642. @result{} bar
  1643. (setq bar 2)
  1644. bar
  1645. @result{} 2
  1646. @group
  1647. foo
  1648. @result{} 2
  1649. @end group
  1650. (setq foo 0)
  1651. bar
  1652. @result{} 0
  1653. foo
  1654. @result{} 0
  1655. @end example
  1656. @node Variables with Restricted Values
  1657. @section Variables with Restricted Values
  1658. @cindex lisp variables defined in C, restrictions
  1659. Ordinary Lisp variables can be assigned any value that is a valid
  1660. Lisp object. However, certain Lisp variables are not defined in Lisp,
  1661. but in C@. Most of these variables are defined in the C code using
  1662. @code{DEFVAR_LISP}. Like variables defined in Lisp, these can take on
  1663. any value. However, some variables are defined using
  1664. @code{DEFVAR_INT} or @code{DEFVAR_BOOL}. @xref{Defining Lisp
  1665. variables in C,, Writing Emacs Primitives}, in particular the
  1666. description of functions of the type @code{syms_of_@var{filename}},
  1667. for a brief discussion of the C implementation.
  1668. Variables of type @code{DEFVAR_BOOL} can only take on the values
  1669. @code{nil} or @code{t}. Attempting to assign them any other value
  1670. will set them to @code{t}:
  1671. @example
  1672. (let ((display-hourglass 5))
  1673. display-hourglass)
  1674. @result{} t
  1675. @end example
  1676. @defvar byte-boolean-vars
  1677. This variable holds a list of all variables of type @code{DEFVAR_BOOL}.
  1678. @end defvar
  1679. Variables of type @code{DEFVAR_INT} can take on only integer values.
  1680. Attempting to assign them any other value will result in an error:
  1681. @example
  1682. (setq undo-limit 1000.0)
  1683. @error{} Wrong type argument: integerp, 1000.0
  1684. @end example
  1685. @node Generalized Variables
  1686. @section Generalized Variables
  1687. @cindex generalized variable
  1688. @cindex place form
  1689. A @dfn{generalized variable} or @dfn{place form} is one of the many places
  1690. in Lisp memory where values can be stored. The simplest place form is
  1691. a regular Lisp variable. But the @sc{car}s and @sc{cdr}s of lists, elements
  1692. of arrays, properties of symbols, and many other locations are also
  1693. places where Lisp values are stored.
  1694. Generalized variables are analogous to lvalues in the C
  1695. language, where @samp{x = a[i]} gets an element from an array
  1696. and @samp{a[i] = x} stores an element using the same notation.
  1697. Just as certain forms like @code{a[i]} can be lvalues in C, there
  1698. is a set of forms that can be generalized variables in Lisp.
  1699. @menu
  1700. * Setting Generalized Variables:: The @code{setf} macro.
  1701. * Adding Generalized Variables:: Defining new @code{setf} forms.
  1702. @end menu
  1703. @node Setting Generalized Variables
  1704. @subsection The @code{setf} Macro
  1705. The @code{setf} macro is the most basic way to operate on generalized
  1706. variables. The @code{setf} form is like @code{setq}, except that it
  1707. accepts arbitrary place forms on the left side rather than just
  1708. symbols. For example, @code{(setf (car a) b)} sets the car of
  1709. @code{a} to @code{b}, doing the same operation as @code{(setcar a b)},
  1710. but without having to remember two separate functions for setting and
  1711. accessing every type of place.
  1712. @defmac setf [place form]@dots{}
  1713. This macro evaluates @var{form} and stores it in @var{place}, which
  1714. must be a valid generalized variable form. If there are several
  1715. @var{place} and @var{form} pairs, the assignments are done sequentially
  1716. just as with @code{setq}. @code{setf} returns the value of the last
  1717. @var{form}.
  1718. @end defmac
  1719. The following Lisp forms will work as generalized variables, and
  1720. so may appear in the @var{place} argument of @code{setf}:
  1721. @itemize
  1722. @item
  1723. A symbol naming a variable. In other words, @code{(setf x y)} is
  1724. exactly equivalent to @code{(setq x y)}, and @code{setq} itself is
  1725. strictly speaking redundant given that @code{setf} exists. Many
  1726. programmers continue to prefer @code{setq} for setting simple
  1727. variables, though, purely for stylistic or historical reasons.
  1728. The macro @code{(setf x y)} actually expands to @code{(setq x y)},
  1729. so there is no performance penalty for using it in compiled code.
  1730. @item
  1731. A call to any of the following standard Lisp functions:
  1732. @smallexample
  1733. aref cddr symbol-function
  1734. car elt symbol-plist
  1735. caar get symbol-value
  1736. cadr gethash
  1737. cdr nth
  1738. cdar nthcdr
  1739. @end smallexample
  1740. @item
  1741. A call to any of the following Emacs-specific functions:
  1742. @smallexample
  1743. alist-get process-get
  1744. frame-parameter process-sentinel
  1745. terminal-parameter window-buffer
  1746. keymap-parent window-display-table
  1747. match-data window-dedicated-p
  1748. overlay-get window-hscroll
  1749. overlay-start window-parameter
  1750. overlay-end window-point
  1751. process-buffer window-start
  1752. process-filter default-value
  1753. @end smallexample
  1754. @end itemize
  1755. @noindent
  1756. @code{setf} signals an error if you pass a @var{place} form that it
  1757. does not know how to handle.
  1758. @c And for cl-lib's cl-getf.
  1759. Note that for @code{nthcdr}, the list argument of the function must
  1760. itself be a valid @var{place} form. For example, @code{(setf (nthcdr
  1761. 0 foo) 7)} will set @code{foo} itself to 7.
  1762. @c The use of @code{nthcdr} as a @var{place} form is an extension
  1763. @c to standard Common Lisp.
  1764. @c FIXME I don't think is a particularly good way to do it,
  1765. @c but these macros are introduced before generalized variables are.
  1766. The macros @code{push} (@pxref{List Variables}) and @code{pop}
  1767. (@pxref{List Elements}) can manipulate generalized variables,
  1768. not just lists. @code{(pop @var{place})} removes and returns the first
  1769. element of the list stored in @var{place}. It is analogous to
  1770. @code{(prog1 (car @var{place}) (setf @var{place} (cdr @var{place})))},
  1771. except that it takes care to evaluate all subforms only once.
  1772. @code{(push @var{x} @var{place})} inserts @var{x} at the front of
  1773. the list stored in @var{place}. It is analogous to @code{(setf
  1774. @var{place} (cons @var{x} @var{place}))}, except for evaluation of the
  1775. subforms. Note that @code{push} and @code{pop} on an @code{nthcdr}
  1776. place can be used to insert or delete at any position in a list.
  1777. The @file{cl-lib} library defines various extensions for generalized
  1778. variables, including additional @code{setf} places.
  1779. @xref{Generalized Variables,,, cl, Common Lisp Extensions}.
  1780. @node Adding Generalized Variables
  1781. @subsection Defining new @code{setf} forms
  1782. This section describes how to define new forms that @code{setf} can
  1783. operate on.
  1784. @defmac gv-define-simple-setter name setter &optional fix-return
  1785. This macro enables you to easily define @code{setf} methods for simple
  1786. cases. @var{name} is the name of a function, macro, or special form.
  1787. You can use this macro whenever @var{name} has a directly
  1788. corresponding @var{setter} function that updates it, e.g.,
  1789. @code{(gv-define-simple-setter car setcar)}.
  1790. This macro translates a call of the form
  1791. @example
  1792. (setf (@var{name} @var{args}@dots{}) @var{value})
  1793. @end example
  1794. into
  1795. @example
  1796. (@var{setter} @var{args}@dots{} @var{value})
  1797. @end example
  1798. @noindent
  1799. Such a @code{setf} call is documented to return @var{value}. This is
  1800. no problem with, e.g., @code{car} and @code{setcar}, because
  1801. @code{setcar} returns the value that it set. If your @var{setter}
  1802. function does not return @var{value}, use a non-@code{nil} value for
  1803. the @var{fix-return} argument of @code{gv-define-simple-setter}. This
  1804. expands into something equivalent to
  1805. @example
  1806. (let ((temp @var{value}))
  1807. (@var{setter} @var{args}@dots{} temp)
  1808. temp)
  1809. @end example
  1810. so ensuring that it returns the correct result.
  1811. @end defmac
  1812. @defmac gv-define-setter name arglist &rest body
  1813. This macro allows for more complex @code{setf} expansions than the
  1814. previous form. You may need to use this form, for example, if there
  1815. is no simple setter function to call, or if there is one but it
  1816. requires different arguments to the place form.
  1817. This macro expands the form
  1818. @code{(setf (@var{name} @var{args}@dots{}) @var{value})} by
  1819. first binding the @code{setf} argument forms
  1820. @code{(@var{value} @var{args}@dots{})} according to @var{arglist},
  1821. and then executing @var{body}. @var{body} should return a Lisp
  1822. form that does the assignment, and finally returns the value that was
  1823. set. An example of using this macro is:
  1824. @example
  1825. (gv-define-setter caar (val x) `(setcar (car ,x) ,val))
  1826. @end example
  1827. @end defmac
  1828. @findex gv-define-expander
  1829. @findex gv-letplace
  1830. @c FIXME? Not sure what or how much to say about these.
  1831. @c See cl.texi for an example of using gv-letplace.
  1832. For more control over the expansion, see the macro @code{gv-define-expander}.
  1833. The macro @code{gv-letplace} can be useful in defining macros that
  1834. perform similarly to @code{setf}; for example, the @code{incf} macro
  1835. of Common Lisp. Consult the source file @file{gv.el} for more details.
  1836. @cindex CL note---no @code{setf} functions
  1837. @quotation
  1838. @b{Common Lisp note:} Common Lisp defines another way to specify the
  1839. @code{setf} behavior of a function, namely @code{setf} functions,
  1840. whose names are lists @code{(setf @var{name})} rather than symbols.
  1841. For example, @code{(defun (setf foo) @dots{})} defines the function
  1842. that is used when @code{setf} is applied to @code{foo}. Emacs does
  1843. not support this. It is a compile-time error to use @code{setf} on a
  1844. form that has not already had an appropriate expansion defined. In
  1845. Common Lisp, this is not an error since the function @code{(setf
  1846. @var{func})} might be defined later.
  1847. @end quotation