eval.texi 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880
  1. @c -*-texinfo-*-
  2. @c This is part of the GNU Emacs Lisp Reference Manual.
  3. @c Copyright (C) 1990-1994, 1998, 2001-2016 Free Software Foundation,
  4. @c Inc.
  5. @c See the file elisp.texi for copying conditions.
  6. @node Evaluation
  7. @chapter Evaluation
  8. @cindex evaluation
  9. @cindex interpreter
  10. @cindex interpreter
  11. @cindex value of expression
  12. The @dfn{evaluation} of expressions in Emacs Lisp is performed by the
  13. @dfn{Lisp interpreter}---a program that receives a Lisp object as input
  14. and computes its @dfn{value as an expression}. How it does this depends
  15. on the data type of the object, according to rules described in this
  16. chapter. The interpreter runs automatically to evaluate portions of
  17. your program, but can also be called explicitly via the Lisp primitive
  18. function @code{eval}.
  19. @ifnottex
  20. @menu
  21. * Intro Eval:: Evaluation in the scheme of things.
  22. * Forms:: How various sorts of objects are evaluated.
  23. * Quoting:: Avoiding evaluation (to put constants in the program).
  24. * Backquote:: Easier construction of list structure.
  25. * Eval:: How to invoke the Lisp interpreter explicitly.
  26. @end menu
  27. @node Intro Eval
  28. @section Introduction to Evaluation
  29. The Lisp interpreter, or evaluator, is the part of Emacs that
  30. computes the value of an expression that is given to it. When a
  31. function written in Lisp is called, the evaluator computes the value
  32. of the function by evaluating the expressions in the function body.
  33. Thus, running any Lisp program really means running the Lisp
  34. interpreter.
  35. @end ifnottex
  36. @cindex form
  37. @cindex expression
  38. @cindex S-expression
  39. @cindex sexp
  40. A Lisp object that is intended for evaluation is called a @dfn{form}
  41. or @dfn{expression}@footnote{It is sometimes also referred to as an
  42. @dfn{S-expression} or @dfn{sexp}, but we generally do not use this
  43. terminology in this manual.}. The fact that forms are data objects
  44. and not merely text is one of the fundamental differences between
  45. Lisp-like languages and typical programming languages. Any object can
  46. be evaluated, but in practice only numbers, symbols, lists and strings
  47. are evaluated very often.
  48. In subsequent sections, we will describe the details of what
  49. evaluation means for each kind of form.
  50. It is very common to read a Lisp form and then evaluate the form,
  51. but reading and evaluation are separate activities, and either can be
  52. performed alone. Reading per se does not evaluate anything; it
  53. converts the printed representation of a Lisp object to the object
  54. itself. It is up to the caller of @code{read} to specify whether this
  55. object is a form to be evaluated, or serves some entirely different
  56. purpose. @xref{Input Functions}.
  57. @cindex recursive evaluation
  58. Evaluation is a recursive process, and evaluating a form often
  59. involves evaluating parts within that form. For instance, when you
  60. evaluate a @dfn{function call} form such as @code{(car x)}, Emacs
  61. first evaluates the argument (the subform @code{x}). After evaluating
  62. the argument, Emacs @dfn{executes} the function (@code{car}), and if
  63. the function is written in Lisp, execution works by evaluating the
  64. @dfn{body} of the function (in this example, however, @code{car} is
  65. not a Lisp function; it is a primitive function implemented in C).
  66. @xref{Functions}, for more information about functions and function
  67. calls.
  68. @cindex environment
  69. Evaluation takes place in a context called the @dfn{environment},
  70. which consists of the current values and bindings of all Lisp
  71. variables (@pxref{Variables}).@footnote{This definition of
  72. ``environment'' is specifically not intended to include all the data
  73. that can affect the result of a program.} Whenever a form refers to a
  74. variable without creating a new binding for it, the variable evaluates
  75. to the value given by the current environment. Evaluating a form may
  76. also temporarily alter the environment by binding variables
  77. (@pxref{Local Variables}).
  78. @cindex side effect
  79. Evaluating a form may also make changes that persist; these changes
  80. are called @dfn{side effects}. An example of a form that produces a
  81. side effect is @code{(setq foo 1)}.
  82. Do not confuse evaluation with command key interpretation. The
  83. editor command loop translates keyboard input into a command (an
  84. interactively callable function) using the active keymaps, and then
  85. uses @code{call-interactively} to execute that command. Executing the
  86. command usually involves evaluation, if the command is written in
  87. Lisp; however, this step is not considered a part of command key
  88. interpretation. @xref{Command Loop}.
  89. @node Forms
  90. @section Kinds of Forms
  91. A Lisp object that is intended to be evaluated is called a
  92. @dfn{form} (or an @dfn{expression}). How Emacs evaluates a form
  93. depends on its data type. Emacs has three different kinds of form
  94. that are evaluated differently: symbols, lists, and all other
  95. types. This section describes all three kinds, one by one, starting
  96. with the other types, which are self-evaluating forms.
  97. @menu
  98. * Self-Evaluating Forms:: Forms that evaluate to themselves.
  99. * Symbol Forms:: Symbols evaluate as variables.
  100. * Classifying Lists:: How to distinguish various sorts of list forms.
  101. * Function Indirection:: When a symbol appears as the car of a list,
  102. we find the real function via the symbol.
  103. * Function Forms:: Forms that call functions.
  104. * Macro Forms:: Forms that call macros.
  105. * Special Forms:: Special forms are idiosyncratic primitives,
  106. most of them extremely important.
  107. * Autoloading:: Functions set up to load files
  108. containing their real definitions.
  109. @end menu
  110. @node Self-Evaluating Forms
  111. @subsection Self-Evaluating Forms
  112. @cindex vector evaluation
  113. @cindex literal evaluation
  114. @cindex self-evaluating form
  115. A @dfn{self-evaluating form} is any form that is not a list or
  116. symbol. Self-evaluating forms evaluate to themselves: the result of
  117. evaluation is the same object that was evaluated. Thus, the number 25
  118. evaluates to 25, and the string @code{"foo"} evaluates to the string
  119. @code{"foo"}. Likewise, evaluating a vector does not cause evaluation
  120. of the elements of the vector---it returns the same vector with its
  121. contents unchanged.
  122. @example
  123. @group
  124. '123 ; @r{A number, shown without evaluation.}
  125. @result{} 123
  126. @end group
  127. @group
  128. 123 ; @r{Evaluated as usual---result is the same.}
  129. @result{} 123
  130. @end group
  131. @group
  132. (eval '123) ; @r{Evaluated "by hand"---result is the same.}
  133. @result{} 123
  134. @end group
  135. @group
  136. (eval (eval '123)) ; @r{Evaluating twice changes nothing.}
  137. @result{} 123
  138. @end group
  139. @end example
  140. It is common to write numbers, characters, strings, and even vectors
  141. in Lisp code, taking advantage of the fact that they self-evaluate.
  142. However, it is quite unusual to do this for types that lack a read
  143. syntax, because there's no way to write them textually. It is possible
  144. to construct Lisp expressions containing these types by means of a Lisp
  145. program. Here is an example:
  146. @example
  147. @group
  148. ;; @r{Build an expression containing a buffer object.}
  149. (setq print-exp (list 'print (current-buffer)))
  150. @result{} (print #<buffer eval.texi>)
  151. @end group
  152. @group
  153. ;; @r{Evaluate it.}
  154. (eval print-exp)
  155. @print{} #<buffer eval.texi>
  156. @result{} #<buffer eval.texi>
  157. @end group
  158. @end example
  159. @node Symbol Forms
  160. @subsection Symbol Forms
  161. @cindex symbol evaluation
  162. When a symbol is evaluated, it is treated as a variable. The result
  163. is the variable's value, if it has one. If the symbol has no value as
  164. a variable, the Lisp interpreter signals an error. For more
  165. information on the use of variables, see @ref{Variables}.
  166. In the following example, we set the value of a symbol with
  167. @code{setq}. Then we evaluate the symbol, and get back the value that
  168. @code{setq} stored.
  169. @example
  170. @group
  171. (setq a 123)
  172. @result{} 123
  173. @end group
  174. @group
  175. (eval 'a)
  176. @result{} 123
  177. @end group
  178. @group
  179. a
  180. @result{} 123
  181. @end group
  182. @end example
  183. The symbols @code{nil} and @code{t} are treated specially, so that the
  184. value of @code{nil} is always @code{nil}, and the value of @code{t} is
  185. always @code{t}; you cannot set or bind them to any other values. Thus,
  186. these two symbols act like self-evaluating forms, even though
  187. @code{eval} treats them like any other symbol. A symbol whose name
  188. starts with @samp{:} also self-evaluates in the same way; likewise,
  189. its value ordinarily cannot be changed. @xref{Constant Variables}.
  190. @node Classifying Lists
  191. @subsection Classification of List Forms
  192. @cindex list form evaluation
  193. A form that is a nonempty list is either a function call, a macro
  194. call, or a special form, according to its first element. These three
  195. kinds of forms are evaluated in different ways, described below. The
  196. remaining list elements constitute the @dfn{arguments} for the function,
  197. macro, or special form.
  198. The first step in evaluating a nonempty list is to examine its first
  199. element. This element alone determines what kind of form the list is
  200. and how the rest of the list is to be processed. The first element is
  201. @emph{not} evaluated, as it would be in some Lisp dialects such as
  202. Scheme.
  203. @node Function Indirection
  204. @subsection Symbol Function Indirection
  205. @cindex symbol function indirection
  206. @cindex indirection for functions
  207. @cindex void function
  208. If the first element of the list is a symbol then evaluation
  209. examines the symbol's function cell, and uses its contents instead of
  210. the original symbol. If the contents are another symbol, this
  211. process, called @dfn{symbol function indirection}, is repeated until
  212. it obtains a non-symbol. @xref{Function Names}, for more information
  213. about symbol function indirection.
  214. One possible consequence of this process is an infinite loop, in the
  215. event that a symbol's function cell refers to the same symbol.
  216. Otherwise, we eventually obtain a non-symbol, which ought to be a
  217. function or other suitable object.
  218. @kindex invalid-function
  219. More precisely, we should now have a Lisp function (a lambda
  220. expression), a byte-code function, a primitive function, a Lisp macro,
  221. a special form, or an autoload object. Each of these types is a case
  222. described in one of the following sections. If the object is not one
  223. of these types, Emacs signals an @code{invalid-function} error.
  224. The following example illustrates the symbol indirection process.
  225. We use @code{fset} to set the function cell of a symbol and
  226. @code{symbol-function} to get the function cell contents
  227. (@pxref{Function Cells}). Specifically, we store the symbol
  228. @code{car} into the function cell of @code{first}, and the symbol
  229. @code{first} into the function cell of @code{erste}.
  230. @example
  231. @group
  232. ;; @r{Build this function cell linkage:}
  233. ;; ------------- ----- ------- -------
  234. ;; | #<subr car> | <-- | car | <-- | first | <-- | erste |
  235. ;; ------------- ----- ------- -------
  236. @end group
  237. @group
  238. (symbol-function 'car)
  239. @result{} #<subr car>
  240. @end group
  241. @group
  242. (fset 'first 'car)
  243. @result{} car
  244. @end group
  245. @group
  246. (fset 'erste 'first)
  247. @result{} first
  248. @end group
  249. @group
  250. (erste '(1 2 3)) ; @r{Call the function referenced by @code{erste}.}
  251. @result{} 1
  252. @end group
  253. @end example
  254. By contrast, the following example calls a function without any symbol
  255. function indirection, because the first element is an anonymous Lisp
  256. function, not a symbol.
  257. @example
  258. @group
  259. ((lambda (arg) (erste arg))
  260. '(1 2 3))
  261. @result{} 1
  262. @end group
  263. @end example
  264. @noindent
  265. Executing the function itself evaluates its body; this does involve
  266. symbol function indirection when calling @code{erste}.
  267. This form is rarely used and is now deprecated. Instead, you should write it
  268. as:
  269. @example
  270. @group
  271. (funcall (lambda (arg) (erste arg))
  272. '(1 2 3))
  273. @end group
  274. @end example
  275. or just
  276. @example
  277. @group
  278. (let ((arg '(1 2 3))) (erste arg))
  279. @end group
  280. @end example
  281. The built-in function @code{indirect-function} provides an easy way to
  282. perform symbol function indirection explicitly.
  283. @c Emacs 19 feature
  284. @defun indirect-function function &optional noerror
  285. @anchor{Definition of indirect-function}
  286. This function returns the meaning of @var{function} as a function. If
  287. @var{function} is a symbol, then it finds @var{function}'s function
  288. definition and starts over with that value. If @var{function} is not a
  289. symbol, then it returns @var{function} itself.
  290. This function returns @code{nil} if the final symbol is unbound. It
  291. signals a @code{cyclic-function-indirection} error if there is a loop
  292. in the chain of symbols.
  293. The optional argument @var{noerror} is obsolete, kept for backward
  294. compatibility, and has no effect.
  295. Here is how you could define @code{indirect-function} in Lisp:
  296. @example
  297. (defun indirect-function (function)
  298. (if (symbolp function)
  299. (indirect-function (symbol-function function))
  300. function))
  301. @end example
  302. @end defun
  303. @node Function Forms
  304. @subsection Evaluation of Function Forms
  305. @cindex function form evaluation
  306. @cindex function call
  307. If the first element of a list being evaluated is a Lisp function
  308. object, byte-code object or primitive function object, then that list is
  309. a @dfn{function call}. For example, here is a call to the function
  310. @code{+}:
  311. @example
  312. (+ 1 x)
  313. @end example
  314. The first step in evaluating a function call is to evaluate the
  315. remaining elements of the list from left to right. The results are the
  316. actual argument values, one value for each list element. The next step
  317. is to call the function with this list of arguments, effectively using
  318. the function @code{apply} (@pxref{Calling Functions}). If the function
  319. is written in Lisp, the arguments are used to bind the argument
  320. variables of the function (@pxref{Lambda Expressions}); then the forms
  321. in the function body are evaluated in order, and the value of the last
  322. body form becomes the value of the function call.
  323. @node Macro Forms
  324. @subsection Lisp Macro Evaluation
  325. @cindex macro call evaluation
  326. If the first element of a list being evaluated is a macro object, then
  327. the list is a @dfn{macro call}. When a macro call is evaluated, the
  328. elements of the rest of the list are @emph{not} initially evaluated.
  329. Instead, these elements themselves are used as the arguments of the
  330. macro. The macro definition computes a replacement form, called the
  331. @dfn{expansion} of the macro, to be evaluated in place of the original
  332. form. The expansion may be any sort of form: a self-evaluating
  333. constant, a symbol, or a list. If the expansion is itself a macro call,
  334. this process of expansion repeats until some other sort of form results.
  335. Ordinary evaluation of a macro call finishes by evaluating the
  336. expansion. However, the macro expansion is not necessarily evaluated
  337. right away, or at all, because other programs also expand macro calls,
  338. and they may or may not evaluate the expansions.
  339. Normally, the argument expressions are not evaluated as part of
  340. computing the macro expansion, but instead appear as part of the
  341. expansion, so they are computed when the expansion is evaluated.
  342. For example, given a macro defined as follows:
  343. @example
  344. @group
  345. (defmacro cadr (x)
  346. (list 'car (list 'cdr x)))
  347. @end group
  348. @end example
  349. @noindent
  350. an expression such as @code{(cadr (assq 'handler list))} is a macro
  351. call, and its expansion is:
  352. @example
  353. (car (cdr (assq 'handler list)))
  354. @end example
  355. @noindent
  356. Note that the argument @code{(assq 'handler list)} appears in the
  357. expansion.
  358. @xref{Macros}, for a complete description of Emacs Lisp macros.
  359. @node Special Forms
  360. @subsection Special Forms
  361. @cindex special forms
  362. @cindex evaluation of special forms
  363. A @dfn{special form} is a primitive function specially marked so that
  364. its arguments are not all evaluated. Most special forms define control
  365. structures or perform variable bindings---things which functions cannot
  366. do.
  367. Each special form has its own rules for which arguments are evaluated
  368. and which are used without evaluation. Whether a particular argument is
  369. evaluated may depend on the results of evaluating other arguments.
  370. If an expression's first symbol is that of a special form, the
  371. expression should follow the rules of that special form; otherwise,
  372. Emacs's behavior is not well-defined (though it will not crash). For
  373. example, @code{((lambda (x) x . 3) 4)} contains a subexpression that
  374. begins with @code{lambda} but is not a well-formed @code{lambda}
  375. expression, so Emacs may signal an error, or may return 3 or 4 or
  376. @code{nil}, or may behave in other ways.
  377. @defun special-form-p object
  378. This predicate tests whether its argument is a special form, and
  379. returns @code{t} if so, @code{nil} otherwise.
  380. @end defun
  381. Here is a list, in alphabetical order, of all of the special forms in
  382. Emacs Lisp with a reference to where each is described.
  383. @table @code
  384. @item and
  385. @pxref{Combining Conditions}
  386. @item catch
  387. @pxref{Catch and Throw}
  388. @item cond
  389. @pxref{Conditionals}
  390. @item condition-case
  391. @pxref{Handling Errors}
  392. @item defconst
  393. @pxref{Defining Variables}
  394. @item defvar
  395. @pxref{Defining Variables}
  396. @item function
  397. @pxref{Anonymous Functions}
  398. @item if
  399. @pxref{Conditionals}
  400. @item interactive
  401. @pxref{Interactive Call}
  402. @item lambda
  403. @pxref{Lambda Expressions}
  404. @item let
  405. @itemx let*
  406. @pxref{Local Variables}
  407. @item or
  408. @pxref{Combining Conditions}
  409. @item prog1
  410. @itemx prog2
  411. @itemx progn
  412. @pxref{Sequencing}
  413. @item quote
  414. @pxref{Quoting}
  415. @item save-current-buffer
  416. @pxref{Current Buffer}
  417. @item save-excursion
  418. @pxref{Excursions}
  419. @item save-restriction
  420. @pxref{Narrowing}
  421. @item setq
  422. @pxref{Setting Variables}
  423. @item setq-default
  424. @pxref{Creating Buffer-Local}
  425. @item track-mouse
  426. @pxref{Mouse Tracking}
  427. @item unwind-protect
  428. @pxref{Nonlocal Exits}
  429. @item while
  430. @pxref{Iteration}
  431. @end table
  432. @cindex CL note---special forms compared
  433. @quotation
  434. @b{Common Lisp note:} Here are some comparisons of special forms in
  435. GNU Emacs Lisp and Common Lisp. @code{setq}, @code{if}, and
  436. @code{catch} are special forms in both Emacs Lisp and Common Lisp.
  437. @code{save-excursion} is a special form in Emacs Lisp, but
  438. doesn't exist in Common Lisp. @code{throw} is a special form in
  439. Common Lisp (because it must be able to throw multiple values), but it
  440. is a function in Emacs Lisp (which doesn't have multiple
  441. values).
  442. @end quotation
  443. @node Autoloading
  444. @subsection Autoloading
  445. The @dfn{autoload} feature allows you to call a function or macro
  446. whose function definition has not yet been loaded into Emacs. It
  447. specifies which file contains the definition. When an autoload object
  448. appears as a symbol's function definition, calling that symbol as a
  449. function automatically loads the specified file; then it calls the
  450. real definition loaded from that file. The way to arrange for an
  451. autoload object to appear as a symbol's function definition is
  452. described in @ref{Autoload}.
  453. @node Quoting
  454. @section Quoting
  455. The special form @code{quote} returns its single argument, as written,
  456. without evaluating it. This provides a way to include constant symbols
  457. and lists, which are not self-evaluating objects, in a program. (It is
  458. not necessary to quote self-evaluating objects such as numbers, strings,
  459. and vectors.)
  460. @defspec quote object
  461. This special form returns @var{object}, without evaluating it.
  462. @end defspec
  463. @cindex @samp{'} for quoting
  464. @cindex quoting using apostrophe
  465. @cindex apostrophe for quoting
  466. Because @code{quote} is used so often in programs, Lisp provides a
  467. convenient read syntax for it. An apostrophe character (@samp{'})
  468. followed by a Lisp object (in read syntax) expands to a list whose first
  469. element is @code{quote}, and whose second element is the object. Thus,
  470. the read syntax @code{'x} is an abbreviation for @code{(quote x)}.
  471. Here are some examples of expressions that use @code{quote}:
  472. @example
  473. @group
  474. (quote (+ 1 2))
  475. @result{} (+ 1 2)
  476. @end group
  477. @group
  478. (quote foo)
  479. @result{} foo
  480. @end group
  481. @group
  482. 'foo
  483. @result{} foo
  484. @end group
  485. @group
  486. ''foo
  487. @result{} (quote foo)
  488. @end group
  489. @group
  490. '(quote foo)
  491. @result{} (quote foo)
  492. @end group
  493. @group
  494. ['foo]
  495. @result{} [(quote foo)]
  496. @end group
  497. @end example
  498. Other quoting constructs include @code{function} (@pxref{Anonymous
  499. Functions}), which causes an anonymous lambda expression written in Lisp
  500. to be compiled, and @samp{`} (@pxref{Backquote}), which is used to quote
  501. only part of a list, while computing and substituting other parts.
  502. @node Backquote
  503. @section Backquote
  504. @cindex backquote (list substitution)
  505. @cindex ` (list substitution)
  506. @findex `
  507. @dfn{Backquote constructs} allow you to quote a list, but
  508. selectively evaluate elements of that list. In the simplest case, it
  509. is identical to the special form @code{quote}
  510. @iftex
  511. @end iftex
  512. @ifnottex
  513. (described in the previous section; @pxref{Quoting}).
  514. @end ifnottex
  515. For example, these two forms yield identical results:
  516. @example
  517. @group
  518. `(a list of (+ 2 3) elements)
  519. @result{} (a list of (+ 2 3) elements)
  520. @end group
  521. @group
  522. '(a list of (+ 2 3) elements)
  523. @result{} (a list of (+ 2 3) elements)
  524. @end group
  525. @end example
  526. @findex , @r{(with backquote)}
  527. The special marker @samp{,} inside of the argument to backquote
  528. indicates a value that isn't constant. The Emacs Lisp evaluator
  529. evaluates the argument of @samp{,}, and puts the value in the list
  530. structure:
  531. @example
  532. @group
  533. `(a list of ,(+ 2 3) elements)
  534. @result{} (a list of 5 elements)
  535. @end group
  536. @end example
  537. @noindent
  538. Substitution with @samp{,} is allowed at deeper levels of the list
  539. structure also. For example:
  540. @example
  541. @group
  542. `(1 2 (3 ,(+ 4 5)))
  543. @result{} (1 2 (3 9))
  544. @end group
  545. @end example
  546. @findex ,@@ @r{(with backquote)}
  547. @cindex splicing (with backquote)
  548. You can also @dfn{splice} an evaluated value into the resulting list,
  549. using the special marker @samp{,@@}. The elements of the spliced list
  550. become elements at the same level as the other elements of the resulting
  551. list. The equivalent code without using @samp{`} is often unreadable.
  552. Here are some examples:
  553. @example
  554. @group
  555. (setq some-list '(2 3))
  556. @result{} (2 3)
  557. @end group
  558. @group
  559. (cons 1 (append some-list '(4) some-list))
  560. @result{} (1 2 3 4 2 3)
  561. @end group
  562. @group
  563. `(1 ,@@some-list 4 ,@@some-list)
  564. @result{} (1 2 3 4 2 3)
  565. @end group
  566. @group
  567. (setq list '(hack foo bar))
  568. @result{} (hack foo bar)
  569. @end group
  570. @group
  571. (cons 'use
  572. (cons 'the
  573. (cons 'words (append (cdr list) '(as elements)))))
  574. @result{} (use the words foo bar as elements)
  575. @end group
  576. @group
  577. `(use the words ,@@(cdr list) as elements)
  578. @result{} (use the words foo bar as elements)
  579. @end group
  580. @end example
  581. @node Eval
  582. @section Eval
  583. Most often, forms are evaluated automatically, by virtue of their
  584. occurrence in a program being run. On rare occasions, you may need to
  585. write code that evaluates a form that is computed at run time, such as
  586. after reading a form from text being edited or getting one from a
  587. property list. On these occasions, use the @code{eval} function.
  588. Often @code{eval} is not needed and something else should be used instead.
  589. For example, to get the value of a variable, while @code{eval} works,
  590. @code{symbol-value} is preferable; or rather than store expressions
  591. in a property list that then need to go through @code{eval}, it is better to
  592. store functions instead that are then passed to @code{funcall}.
  593. The functions and variables described in this section evaluate forms,
  594. specify limits to the evaluation process, or record recently returned
  595. values. Loading a file also does evaluation (@pxref{Loading}).
  596. It is generally cleaner and more flexible to store a function in a
  597. data structure, and call it with @code{funcall} or @code{apply}, than
  598. to store an expression in the data structure and evaluate it. Using
  599. functions provides the ability to pass information to them as
  600. arguments.
  601. @defun eval form &optional lexical
  602. This is the basic function for evaluating an expression. It evaluates
  603. @var{form} in the current environment, and returns the result. The
  604. type of the @var{form} object determines how it is evaluated.
  605. @xref{Forms}.
  606. The argument @var{lexical} specifies the scoping rule for local
  607. variables (@pxref{Variable Scoping}). If it is omitted or @code{nil},
  608. that means to evaluate @var{form} using the default dynamic scoping
  609. rule. If it is @code{t}, that means to use the lexical scoping rule.
  610. The value of @var{lexical} can also be a non-empty alist specifying a
  611. particular @dfn{lexical environment} for lexical bindings; however,
  612. this feature is only useful for specialized purposes, such as in Emacs
  613. Lisp debuggers. @xref{Lexical Binding}.
  614. Since @code{eval} is a function, the argument expression that appears
  615. in a call to @code{eval} is evaluated twice: once as preparation before
  616. @code{eval} is called, and again by the @code{eval} function itself.
  617. Here is an example:
  618. @example
  619. @group
  620. (setq foo 'bar)
  621. @result{} bar
  622. @end group
  623. @group
  624. (setq bar 'baz)
  625. @result{} baz
  626. ;; @r{Here @code{eval} receives argument @code{foo}}
  627. (eval 'foo)
  628. @result{} bar
  629. ;; @r{Here @code{eval} receives argument @code{bar}, which is the value of @code{foo}}
  630. (eval foo)
  631. @result{} baz
  632. @end group
  633. @end example
  634. The number of currently active calls to @code{eval} is limited to
  635. @code{max-lisp-eval-depth} (see below).
  636. @end defun
  637. @deffn Command eval-region start end &optional stream read-function
  638. @anchor{Definition of eval-region}
  639. This function evaluates the forms in the current buffer in the region
  640. defined by the positions @var{start} and @var{end}. It reads forms from
  641. the region and calls @code{eval} on them until the end of the region is
  642. reached, or until an error is signaled and not handled.
  643. By default, @code{eval-region} does not produce any output. However,
  644. if @var{stream} is non-@code{nil}, any output produced by output
  645. functions (@pxref{Output Functions}), as well as the values that
  646. result from evaluating the expressions in the region are printed using
  647. @var{stream}. @xref{Output Streams}.
  648. If @var{read-function} is non-@code{nil}, it should be a function,
  649. which is used instead of @code{read} to read expressions one by one.
  650. This function is called with one argument, the stream for reading
  651. input. You can also use the variable @code{load-read-function}
  652. (@pxref{Definition of load-read-function,, How Programs Do Loading})
  653. to specify this function, but it is more robust to use the
  654. @var{read-function} argument.
  655. @code{eval-region} does not move point. It always returns @code{nil}.
  656. @end deffn
  657. @cindex evaluation of buffer contents
  658. @deffn Command eval-buffer &optional buffer-or-name stream filename unibyte print
  659. This is similar to @code{eval-region}, but the arguments provide
  660. different optional features. @code{eval-buffer} operates on the
  661. entire accessible portion of buffer @var{buffer-or-name}
  662. (@pxref{Narrowing,,, emacs, The GNU Emacs Manual}).
  663. @var{buffer-or-name} can be a buffer, a buffer name (a string), or
  664. @code{nil} (or omitted), which means to use the current buffer.
  665. @var{stream} is used as in @code{eval-region}, unless @var{stream} is
  666. @code{nil} and @var{print} non-@code{nil}. In that case, values that
  667. result from evaluating the expressions are still discarded, but the
  668. output of the output functions is printed in the echo area.
  669. @var{filename} is the file name to use for @code{load-history}
  670. (@pxref{Unloading}), and defaults to @code{buffer-file-name}
  671. (@pxref{Buffer File Name}). If @var{unibyte} is non-@code{nil},
  672. @code{read} converts strings to unibyte whenever possible.
  673. @findex eval-current-buffer
  674. @code{eval-current-buffer} is an alias for this command.
  675. @end deffn
  676. @defopt max-lisp-eval-depth
  677. @anchor{Definition of max-lisp-eval-depth}
  678. This variable defines the maximum depth allowed in calls to @code{eval},
  679. @code{apply}, and @code{funcall} before an error is signaled (with error
  680. message @code{"Lisp nesting exceeds max-lisp-eval-depth"}).
  681. This limit, with the associated error when it is exceeded, is one way
  682. Emacs Lisp avoids infinite recursion on an ill-defined function. If
  683. you increase the value of @code{max-lisp-eval-depth} too much, such
  684. code can cause stack overflow instead. On some systems, this overflow
  685. can be handled. In that case, normal Lisp evaluation is interrupted
  686. and control is transferred back to the top level command loop
  687. (@code{top-level}). Note that there is no way to enter Emacs Lisp
  688. debugger in this situation. @xref{Error Debugging}.
  689. @cindex Lisp nesting error
  690. The depth limit counts internal uses of @code{eval}, @code{apply}, and
  691. @code{funcall}, such as for calling the functions mentioned in Lisp
  692. expressions, and recursive evaluation of function call arguments and
  693. function body forms, as well as explicit calls in Lisp code.
  694. The default value of this variable is 400. If you set it to a value
  695. less than 100, Lisp will reset it to 100 if the given value is
  696. reached. Entry to the Lisp debugger increases the value, if there is
  697. little room left, to make sure the debugger itself has room to
  698. execute.
  699. @code{max-specpdl-size} provides another limit on nesting.
  700. @xref{Definition of max-specpdl-size,, Local Variables}.
  701. @end defopt
  702. @defvar values
  703. The value of this variable is a list of the values returned by all the
  704. expressions that were read, evaluated, and printed from buffers
  705. (including the minibuffer) by the standard Emacs commands which do
  706. this. (Note that this does @emph{not} include evaluation in
  707. @file{*ielm*} buffers, nor evaluation using @kbd{C-j}, @kbd{C-x C-e},
  708. and similar evaluation commands in @code{lisp-interaction-mode}.) The
  709. elements are ordered most recent first.
  710. @example
  711. @group
  712. (setq x 1)
  713. @result{} 1
  714. @end group
  715. @group
  716. (list 'A (1+ 2) auto-save-default)
  717. @result{} (A 3 t)
  718. @end group
  719. @group
  720. values
  721. @result{} ((A 3 t) 1 @dots{})
  722. @end group
  723. @end example
  724. This variable is useful for referring back to values of forms recently
  725. evaluated. It is generally a bad idea to print the value of
  726. @code{values} itself, since this may be very long. Instead, examine
  727. particular elements, like this:
  728. @example
  729. @group
  730. ;; @r{Refer to the most recent evaluation result.}
  731. (nth 0 values)
  732. @result{} (A 3 t)
  733. @end group
  734. @group
  735. ;; @r{That put a new element on,}
  736. ;; @r{so all elements move back one.}
  737. (nth 1 values)
  738. @result{} (A 3 t)
  739. @end group
  740. @group
  741. ;; @r{This gets the element that was next-to-most-recent}
  742. ;; @r{before this example.}
  743. (nth 3 values)
  744. @result{} 1
  745. @end group
  746. @end example
  747. @end defvar