variables.texi 82 KB

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