home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional / OS2PRO194.ISO / os2 / prgramer / unix / info / elisp.i05 < prev    next >
Encoding:
GNU Info File  |  1993-06-14  |  50.7 KB  |  1,283 lines

  1. This is Info file elisp, produced by Makeinfo-1.47 from the input file
  2. elisp.texi.
  3.  
  4.    This file documents GNU Emacs Lisp.
  5.  
  6.    This is edition 1.03 of the GNU Emacs Lisp Reference Manual, for
  7. Emacs Version 18.
  8.  
  9.    Published by the Free Software Foundation, 675 Massachusetts Avenue,
  10. Cambridge, MA 02139 USA
  11.  
  12.    Copyright (C) 1990 Free Software Foundation, Inc.
  13.  
  14.    Permission is granted to make and distribute verbatim copies of this
  15. manual provided the copyright notice and this permission notice are
  16. preserved on all copies.
  17.  
  18.    Permission is granted to copy and distribute modified versions of
  19. this manual under the conditions for verbatim copying, provided that
  20. the entire resulting derived work is distributed under the terms of a
  21. permission notice identical to this one.
  22.  
  23.    Permission is granted to copy and distribute translations of this
  24. manual into another language, under the above conditions for modified
  25. versions, except that this permission notice may be stated in a
  26. translation approved by the Foundation.
  27.  
  28. 
  29. File: elisp,  Node: Symbol Components,  Next: Definitions,  Prev: Symbols,  Up: Symbols
  30.  
  31. Symbol Components
  32. =================
  33.  
  34.    Each symbol has four components (or "cells"), each of which
  35. references another object:
  36.  
  37. Print name
  38.      The "print name cell" holds a string which names the symbol for
  39.      reading and printing.  See `symbol-name' in *Note Creating
  40.      Symbols::.
  41.  
  42. Value
  43.      The "value cell" holds the current value of the symbol as a
  44.      variable.  When a symbol is used as a form, the value of the form
  45.      is the contents of the symbol's value cell.  See `symbol-value' in
  46.      *Note Accessing Variables::.
  47.  
  48. Function
  49.      The "function cell" holds the function definition of the symbol.
  50.      When a symbol is used as a function, its function definition is
  51.      used in its place.  This cell is also used by the editor command
  52.      loop to record keymaps and keyboard macros.  Because each symbol
  53.      has separate value and function cells, variables and function
  54.      names do not conflict.  See `symbol-function' in *Note Function
  55.      Cells::.
  56.  
  57. Property list
  58.      The "property list cell" holds the property list of the symbol. 
  59.      See `symbol-plist' in *Note Property Lists::.
  60.  
  61.    The print name cell always holds a string, and cannot be changed. 
  62. The other three cells can be set individually to any specified Lisp
  63. object.
  64.  
  65.    The print name cell holds the string that is the name of the symbol.
  66. Since symbols are represented textually by their names, it is important
  67. not to have two symbols with the same name.  The Lisp reader ensures
  68. this: every time it reads a symbol, it looks for an existing symbol with
  69. the specified name before it creates a new one.  (In GNU Emacs Lisp,
  70. this is done with a hashing algorithm that uses an obarray; see *Note
  71. Creating Symbols::.)
  72.  
  73.    In normal usage, the function cell usually contains a function or
  74. macro, as that is what the Lisp interpreter expects to see there (*note
  75. Evaluation::.).  Keyboard macros (*note Keyboard Macros::.), keymaps
  76. (*note Keymaps::.) and autoload objects (*note Autoloading::.) are also
  77. sometimes stored in the function cell of symbols.  We often refer to
  78. "the function `foo'" when we really mean the function stored in the
  79. function cell of the symbol `foo'.  The distinction will be made only
  80. when necessary.
  81.  
  82.    Similarly, the property list cell normally holds a correctly
  83. formatted property list (*note Property Lists::.), as a number of
  84. functions will expect to see a property list there.
  85.  
  86.    The function cell or the value cell may be "void", which means that
  87. the cell does not reference any object.  (This is not the same thing as
  88. holding the symbol `void', nor the same as holding the symbol `nil'.) 
  89. Examining the value of a cell which is void results in an error, such
  90. as `Symbol's value as variable is void'.
  91.  
  92.    The four functions `symbol-name', `symbol-value', `symbol-plist',
  93. and `symbol-function' return the contents of the four cells.  Here as
  94. an example we show the contents of the four cells of the symbol
  95. `buffer-file-name':
  96.  
  97.      (symbol-name 'buffer-file-name)
  98.           => "buffer-file-name"
  99.      (symbol-value 'buffer-file-name)
  100.           => "/gnu/elisp/symbols.texi"
  101.      (symbol-plist 'buffer-file-name)
  102.           => (variable-documentation 29529)
  103.      (symbol-function 'buffer-file-name)
  104.           => #<subr buffer-file-name>
  105.  
  106. Because this symbol is the variable which holds the name of the file
  107. being visited in the current buffer, the value cell contents we see are
  108. the name of the source file of this chapter of the Emacs Lisp Manual.
  109. The property list cell contains the list `(variable-documentation
  110. 29529)' which tells the documentation functions where to find
  111. documentation about `buffer-file-name' in the `DOC' file. (29529 is the
  112. offset from the beginning of the `DOC' file where the documentation for
  113. the function begins.)  The function cell contains the function for
  114. returning the name of the file.  Since `buffer-file-name' is a
  115. primitive function, its function definition has no read syntax and
  116. prints in hash notation (*note Primitive Function Type::.).  A function
  117. definition written in Lisp will have a lambda expression (or byte-code)
  118. in this cell.
  119.  
  120. 
  121. File: elisp,  Node: Definitions,  Next: Creating Symbols,  Prev: Symbol Components,  Up: Symbols
  122.  
  123. Defining Symbols
  124. ================
  125.  
  126.    A "definition" in Lisp is a special form that announces your
  127. intention to use a certain symbol in a particular way.  In Emacs Lisp,
  128. you can define a symbol as a variable, or define it as a function (or
  129. macro), or both independently.
  130.  
  131.    A definition construct typically specifies a value or meaning for the
  132. symbol for one kind of use, plus documentation for its meaning when used
  133. in this way.  Thus, when you define a symbol as a variable, you can
  134. supply an initial value for the variable, plus documentation for the
  135. variable.
  136.  
  137.    `defvar' and `defconst' are definitions that establish a symbol as a
  138. global variable.  They are documented in detail in *Note Defining
  139. Variables::.
  140.  
  141.    `defun' defines a symbol as a function, creating a lambda expression
  142. and storing it in the function cell of the symbol.  This lambda
  143. expression thus becomes the function definition of the symbol. (The
  144. term "function definition", meaning the contents of the function cell,
  145. is derived from the idea that `defun' gives the symbol its definition
  146. as a function.)  *Note Functions::.
  147.  
  148.    `defmacro' defines a symbol as a macro.  It creates a macro object
  149. and stores it in the function cell of the symbol.  Note that a given
  150. symbol can be a macro or a function, but not both at once, because both
  151. macro and function definitions are kept in the function cell, and that
  152. cell can hold only one Lisp object at any given time. *Note Macros::.
  153.  
  154.    In GNU Emacs Lisp, a definition is not required in order to use a
  155. symbol as a variable or function.  Thus, you can make a symbol a global
  156. variable with `setq', whether you define it first or not.  The real
  157. purpose of definitions is to guide programmers and programming tools.
  158. They inform programmers who read the code that certain symbols are
  159. *intended* to be used as variables, or as functions.  In addition,
  160. utilities such as `etags' and `make-docfile' can recognize definitions,
  161. and add the appropriate information to tag tables and the
  162. `emacs/etc/DOC-VERSION' file. *Note Accessing Documentation::.
  163.  
  164. 
  165. File: elisp,  Node: Creating Symbols,  Next: Property Lists,  Prev: Definitions,  Up: Symbols
  166.  
  167. Creating and Interning Symbols
  168. ==============================
  169.  
  170.    To understand how symbols are created in GNU Emacs Lisp, it is
  171. necessary to know how Lisp reads them.  It is essential to ensure that
  172. every time Lisp reads the same set of characters, it finds the same
  173. symbol. Failure to do so would be disastrous.
  174.  
  175.    When the Lisp reader encounters a symbol, it reads all the characters
  176. of the name.  Then it "hashes" those characters to find an index in a
  177. table called an "obarray".  Hashing is an efficient method of looking
  178. something up.  For example, instead of searching a telephone book cover
  179. to cover when looking up Jan Jones, you start with the J's and go from
  180. there.  That is a simple version of hashing.  Each element of the
  181. obarray is a "bucket" which holds all the symbols with a given hash
  182. code; to look for a given name, it is sufficient to look through all
  183. the symbols in the bucket for that name's hash code.
  184.  
  185.    If a symbol with the desired name is found, then it is used.  If no
  186. such symbol is found, then a new symbol is created and added to the
  187. obarray bucket.  Adding a symbol to an obarray is called "interning"
  188. it, and the symbol is then called an "interned symbol".  In Emacs Lisp,
  189. a symbol may be interned in only one obarray.
  190.  
  191.      Common Lisp note: in Common Lisp, a symbol may be interned in
  192.      several obarrays at once.
  193.  
  194.    If a symbol is not in the obarray, then there is no way for Lisp to
  195. find it when its name is read.  Such a symbol is called an "uninterned
  196. symbol" relative to the obarray.  An uninterned symbol has all the
  197. other characteristics of symbols.  It is possible, though uncommon, for
  198. two different symbols to have the same name in different obarrays; they
  199. are not `eq' or `equal'.
  200.  
  201.    In Emacs Lisp, an obarray is represented as a vector.  Each element
  202. of the vector is a bucket; its value is either an interned symbol whose
  203. name hashes to that bucket, or 0 if the bucket is empty.  Each interned
  204. symbol has an internal link (invisible to the user) to the next symbol
  205. in the bucket.  Because these links are invisible, there is no way to
  206. scan the symbols in an obarray except using `mapatoms' (below). The
  207. order of symbols in a bucket is not significant.
  208.  
  209.    In an empty obarray, every element is 0, and you can create an
  210. obarray with `(make-vector LENGTH 0)'.  Prime numbers as lengths tend
  211. to result in good hashing; lengths one less than a power of two are also
  212. good.
  213.  
  214.    Most of the functions below take a name and sometimes an obarray as
  215. arguments.  A `wrong-type-argument' error is signaled if the name is
  216. not a string, or if the obarray is not a vector.
  217.  
  218.  -- Function: symbol-name SYMBOL
  219.      This function returns the string that is SYMBOL's name.  For
  220.      example:
  221.  
  222.           (symbol-name 'foo)
  223.                => "foo"
  224.  
  225.      Changing the string by substituting characters, etc, will change
  226.      the name of the symbol, but will fail to update the obarray, so
  227.      don't do it!
  228.  
  229.  -- Function: make-symbol NAME
  230.      This function returns a newly-allocated uninterned symbol whose
  231.      name is NAME (which must be a string).  Its value and function
  232.      definition are void, and its property list is `nil'.  In the
  233.      example below, the value of `sym' is not `eq' to `foo' because it
  234.      is a distinct uninterned symbol whose name is also `foo'.
  235.  
  236.           (setq sym (make-symbol "foo"))
  237.                => foo
  238.           (eq sym 'foo)
  239.                => nil
  240.  
  241.  -- Function: intern NAME &optional OBARRAY
  242.      This function returns the interned symbol whose name is NAME.  If
  243.      there is no such symbol in the obarray, a new one is created,
  244.      added to the obarray, and returned.  If OBARRAY is supplied, it
  245.      specifies the obarray to use; otherwise, the value of the global
  246.      variable `obarray' is used.
  247.  
  248.           (setq sym (intern "foo"))
  249.                => foo
  250.           (eq sym 'foo)
  251.                => t
  252.  
  253.  -- Function: intern-soft NAME &optional OBARRAY
  254.      This function returns the symbol whose name is NAME, or `nil' if a
  255.      symbol with that name is not found in the obarray.  Therefore, you
  256.      can use `intern-soft' to test whether a symbol with a given name is
  257.      interned.  If OBARRAY is supplied, it specifies the obarray to
  258.      use; otherwise the value of the global variable `obarray' is used.
  259.  
  260.           (intern-soft "frazzle")                ; No such symbol exists.
  261.                => nil
  262.           (make-symbol "frazzle")                ; Create an uninterned one.
  263.                => frazzle
  264.           (intern-soft "frazzle")                ; That one cannot be found.
  265.                => nil
  266.           (setq sym (intern "frazzle"))          ; Create an interned one.
  267.                => frazzle
  268.           (intern-soft "frazzle")                ; That one can be found!
  269.                => frazzle
  270.           (eq sym 'frazzle)                      ; And it is the same one.
  271.                => t
  272.  
  273.  -- Variable: obarray
  274.      This variable is the standard obarray for use by `intern' and
  275.      `read'.
  276.  
  277.  -- Function: mapatoms FUNCTION &optional OBARRAY
  278.      This function applies FUNCTION to every symbol in OBARRAY. It
  279.      returns `nil'.  If OBARRAY is not supplied, it defaults to the
  280.      value of `obarray', the standard obarray for ordinary symbols.
  281.  
  282.           (setq count 0)
  283.                => 0
  284.           (defun count-syms (s)
  285.             (setq count (1+ count)))
  286.                => count-syms
  287.           (mapatoms 'count-syms)
  288.                => nil
  289.           count
  290.                => 1871
  291.  
  292.      See `documentation' in *Note Accessing Documentation::, for another
  293.      example using `mapatoms'.
  294.  
  295. 
  296. File: elisp,  Node: Property Lists,  Prev: Creating Symbols,  Up: Symbols
  297.  
  298. Property Lists
  299. ==============
  300.  
  301.    A "property list" ("plist" for short) is a list of paired elements
  302. stored in the property list cell of a symbol.  Each of the pairs
  303. associates a property name (usually a symbol) with a property or value.
  304.  Property lists are generally used to record information about a
  305. symbol, such as how to compile it, the name of the file where it was
  306. defined, or perhaps even the grammatical class of the symbol
  307. (representing a word) in a language understanding system.
  308.  
  309.    The property names and property values may be any Lisp objects, but
  310. the names are usually symbols.  They are compared using `eq'.  Here is
  311. an example of a property list, found on the symbol `progn' when the
  312. compiler is loaded:
  313.  
  314.      (lisp-indent-hook 0 byte-compile byte-compile-progn)
  315.  
  316. Here `lisp-indent-hook' and `byte-compile' are property names, and the
  317. other two elements are the corresponding values.
  318.  
  319.    Association lists (*note Association Lists::.) are very similar to
  320. property lists.  In contrast to association lists, the order of the
  321. pairs in the property list is not significant since the property names
  322. must be distinct.
  323.  
  324.    Property lists are better than association lists when it is necessary
  325. to attach information to various Lisp function names or variables.  If
  326. all the pairs are recorded in one association list, it will be necessary
  327. to search that entire list each time a function or variable is to be
  328. operated on.  By contrast, if the information is recorded in the
  329. property lists of the function names or variables themselves, each
  330. search will scan only the length of one property list, which is usually
  331. short.  For this reason, the documentation for a variable is recorded in
  332. a property named `variable-documentation'.  The byte compiler likewise
  333. uses properties to record those functions needing special treatment.
  334.  
  335.    However, association lists have their own advantages.  Depending on
  336. your application, it may be faster to add an association to the front of
  337. an association list than to update a property.  All properties for a
  338. symbol are stored in the same property list, so there is a possibility
  339. of a conflict between different uses of a property name.  (For this
  340. reason, it is a good idea to use property names that are probably
  341. unique, such as by including the name of the library in the property
  342. name.)  An association list may be used like a stack where associations
  343. are pushed on the front of the list and later discarded; this is not
  344. possible with a property list.
  345.  
  346.  -- Function: symbol-plist SYMBOL
  347.      This function returns the property list of SYMBOL.
  348.  
  349.  -- Function: setplist SYMBOL PLIST
  350.      This function sets SYMBOL's property list to PLIST. Normally,
  351.      PLIST should be a well-formed property list, but this is not
  352.      enforced.
  353.  
  354.           (setplist 'foo '(a 1 b (2 3) c nil))
  355.                => (a 1 b (2 3) c nil)
  356.           (symbol-plist 'foo)
  357.                => (a 1 b (2 3) c nil)
  358.  
  359.      For symbols in special obarrays, which are not used for ordinary
  360.      purposes, it may make sense to use the property list cell in a
  361.      nonstandard fashion; in fact, the abbrev mechanism does so (*note
  362.      Abbrevs::.).
  363.  
  364.  -- Function: get SYMBOL PROPERTY
  365.      This function finds the value of the property named PROPERTY in
  366.      SYMBOL's property list.  If there is no such property, `nil' is
  367.      returned.  Thus, there is no distinction between a value of `nil'
  368.      and the absence of the property.
  369.  
  370.      The name PROPERTY is compared with the existing property names
  371.      using `eq', so any object is a legitimate property.
  372.  
  373.      See `put' for an example.
  374.  
  375.  -- Function: put SYMBOL PROPERTY VALUE
  376.      This function puts VALUE onto SYMBOL's property list under the
  377.      property name PROPERTY, replacing any previous value.
  378.  
  379.           (put 'fly 'verb 'transitive)
  380.                =>'transitive
  381.           (put 'fly 'noun '(a buzzing little bug))
  382.                => (a buzzing little bug)
  383.           (get 'fly 'verb)
  384.                => transitive
  385.           (symbol-plist 'fly)
  386.                => (verb transitive noun (a buzzing little bug))
  387.  
  388. 
  389. File: elisp,  Node: Evaluation,  Next: Control Structures,  Prev: Symbols,  Up: Top
  390.  
  391. Evaluation
  392. **********
  393.  
  394.    The "evaluation" of expressions in Emacs Lisp is performed by the
  395. "Lisp interpreter"--a program that receives a Lisp object as input and
  396. computes its "value as an expression".  The value is computed in a
  397. fashion that depends on the data type of the object, following rules
  398. described in this chapter.  The interpreter runs automatically to
  399. evaluate portions of your program, but can also be called explicitly
  400. via the Lisp primitive function `eval'.
  401.  
  402. * Menu:
  403.  
  404. * Intro Eval::  Evaluation in the scheme of things.
  405. * Eval::        How to invoke the Lisp interpreter explicitly.
  406. * Forms::       How various sorts of objects are evaluated.
  407. * Quoting::     Avoiding evaluation (to put constants in the program).
  408.  
  409. 
  410. File: elisp,  Node: Intro Eval,  Next: Eval,  Prev: Evaluation,  Up: Evaluation
  411.  
  412. Introduction to Evaluation
  413. ==========================
  414.  
  415.    The Lisp interpreter, or evaluator, is the program which computes
  416. the value of an expression which is given to it.  When a function
  417. written in Lisp is called, the evaluator computes the value of the
  418. function by evaluating the expressions in the function body.  Thus,
  419. running any Lisp program really means running the Lisp interpreter.
  420.  
  421.    How the evaluator handles an object depends primarily on the data
  422. type of the object.
  423.  
  424.    A Lisp object which is intended for evaluation is called an
  425. "expression" or a "form".  The fact that expressions are data objects
  426. and not merely text is one of the fundamental differences between
  427. Lisp-like languages and typical programming languages.  Any object can
  428. be evaluated, but in practice only numbers, symbols, lists and strings
  429. are evaluated very often.
  430.  
  431.    It is very common to read a Lisp expression and then evaluate the
  432. expression, but reading and evaluation are separate activities, and
  433. either can be performed alone.  Reading per se does not evaluate
  434. anything; it converts the printed representation of a Lisp object to the
  435. object itself.  It is up to the caller of `read' whether this object is
  436. a form to be evaluated, or serves some entirely different purpose. 
  437. *Note Input Functions::.
  438.  
  439.    Do not confuse evaluation with command key interpretation.  The
  440. editor command loop translates keyboard input into a command (an
  441. interactively callable function) using the current keymaps, and then
  442. uses `call-interactively' to invoke the command.  The execution of the
  443. command itself involves evaluation if the command is written in Lisp,
  444. but that is not a part of command key interpretation itself. *Note
  445. Command Loop::.
  446.  
  447.    Evaluation is a recursive process.  That is, evaluation of a form may
  448. cause `eval' to be called again in order to evaluate parts of the form.
  449.  For example, evaluation of a function call first evaluates each
  450. argument of the function call, and then evaluates each form in the
  451. function body.  Consider evaluation of the form `(car x)': the subform
  452. `x' must first be evaluated recursively, so that its value can be
  453. passed as an argument to the function `car'.
  454.  
  455.    The evaluation of forms takes place in a context called the
  456. "environment", which consists of the current values and bindings of all
  457. Lisp variables.  Whenever the form refers to a variable without
  458. creating a new binding for it, the value of the current binding is used.
  459. *Note Variables::.
  460.  
  461.    Evaluation of a form may create new environments for recursive
  462. evaluation by binding variables (*note Local Variables::.).  These
  463. environments are temporary and will be gone by the time evaluation of
  464. the form is complete.  The form may also make changes that persist;
  465. these changes are called "side-effects".  An example of a form that
  466. produces side-effects is `(setq foo 1)'.
  467.  
  468.    Finally, evaluation of one particular function call, `byte-code',
  469. invokes the "byte-code interpreter" on its arguments.  Although the
  470. byte-code interpreter is not the same as the Lisp interpreter, it uses
  471. the same environment as the Lisp interpreter, and may on occasion invoke
  472. the Lisp interpreter.  (*Note Byte Compilation::.)
  473.  
  474.    The details of what evaluation means for each kind of form are
  475. described below (*note Forms::.).
  476.  
  477. 
  478. File: elisp,  Node: Eval,  Next: Forms,  Prev: Intro Eval,  Up: Evaluation
  479.  
  480. Eval
  481. ====
  482.  
  483.    Most often, forms are evaluated automatically, by virtue of their
  484. occurrence in a program being run.  On rare occasions, you may need to
  485. write code that evaluates a form that is computed at run time, such as
  486. when the form is read from text being edited or found on a property
  487. list.  On these occasions, use the `eval' function.
  488.  
  489.    The functions and variables described in this section evaluate
  490. forms, specify limits to the evaluation process, or record recently
  491. returned values.  Evaluation is also performed by `load' (*note
  492. Loading::.).
  493.  
  494.  -- Function: eval FORM
  495.      This is the basic function for performing evaluation.  It evaluates
  496.      FORM in the current environment and returns the result.  How the
  497.      evaluation proceeds depends on the type of the object (*note
  498.      Forms::.).
  499.  
  500.      Since `eval' is a function, the argument expression that appears
  501.      in a call to `eval' is evaluated twice: once as preparation before
  502.      `eval' is called, and again by the `eval' function itself. Here is
  503.      an example:
  504.  
  505.           (setq foo 'bar)
  506.                => bar
  507.           (setq bar 'baz)
  508.                => baz
  509.           ;; `eval' is called on the form `bar', which is the value of `foo'
  510.           (eval foo)
  511.                => baz
  512.  
  513.      The number of currently active calls to `eval' is limited to
  514.      `max-lisp-eval-depth'.
  515.  
  516.  -- Command: eval-current-buffer &optional STREAM
  517.      This function evaluates the forms in the current buffer.  It reads
  518.      forms from the buffer and calls `eval' on them until the end of the
  519.      buffer is reached, or until an error is signaled and not handled.
  520.  
  521.      If STREAM is supplied, the variable `standard-output' is bound to
  522.      STREAM during the evaluation (*note Output Functions::.).
  523.  
  524.      `eval-current-buffer' always returns `nil'.
  525.  
  526.  -- Command: eval-region START END &optional STREAM
  527.      This function evaluates the forms in the current buffer in the
  528.      region defined by the positions START and END.  It reads forms from
  529.      the region and calls `eval' on them until the end of the region is
  530.      reached, or until an error is signaled and not handled.
  531.  
  532.      If STREAM is supplied, `standard-output' is bound to it for the
  533.      duration of the command.
  534.  
  535.      `eval-region' always returns `nil'.
  536.  
  537.  -- Variable: max-lisp-eval-depth
  538.      This variable defines the maximum depth allowed in calls to
  539.      `eval', `apply', and `funcall' before an error is signaled (with
  540.      error message `"Lisp nesting exceeds max-lisp-eval-depth"'). 
  541.      `eval' is called recursively to evaluate the arguments of Lisp
  542.      function calls and to evaluate bodies of functions.
  543.  
  544.      This limit, with the associated error when it is exceeded, is one
  545.      way that Lisp avoids infinite recursion on an ill-defined function.
  546.  
  547.      The default value of this variable is 200.  If you set it to a
  548.      value less than 100, Lisp will reset it to 100 if the given value
  549.      is reached.
  550.  
  551.  -- Variable: values
  552.      The value of this variable is a list of values returned by all
  553.      expressions which were read from buffers (including the
  554.      minibuffer), evaluated, and printed.  The elements are in order,
  555.      most recent first.
  556.  
  557.           (setq x 1)
  558.                => 1
  559.           (list 'A (1+ 2) auto-save-default)
  560.                => (A 3 t)
  561.           values
  562.                => ((A 3 t) 1 ...)
  563.  
  564.      This variable is useful for referring back to values of forms
  565.      recently evaluated.  It is generally a bad idea to print the value
  566.      of `values' itself, since this may be very long.  Instead, examine
  567.      particular elements, like this:
  568.  
  569.           ;; Refer to the most recent evaluation result.
  570.           (nth 0 values)
  571.                => (A 3 t)
  572.           ;; That put a new element on, so all elements move back one.
  573.           (nth 1 values)
  574.                => (A 3 t)
  575.           ;; This gets the element that was next-to-last before this example.
  576.           (nth 3 values)
  577.                => 1
  578.  
  579. 
  580. File: elisp,  Node: Forms,  Next: Quoting,  Prev: Eval,  Up: Evaluation
  581.  
  582. Kinds of Forms
  583. ==============
  584.  
  585.    A Lisp object that is intended to be evaluated is called a "form".
  586. How Emacs evaluates a form depends on its data type.  Emacs has three
  587. different kinds of form that are evaluated differently: symbols, lists,
  588. and "all other types".  All three kinds are described in this section,
  589. starting with "all other types" which are self-evaluating forms.
  590.  
  591. * Menu:
  592.  
  593. * Self-Evaluating Forms::   Forms that evaluate to themselves.
  594. * Symbol Forms::            Symbols evaluate as variables.
  595. * Classifying Lists::       How to distinguish various sorts of list forms.
  596. * Function Forms::          Forms that call functions.
  597. * Macro Forms::             Forms that call macros.
  598. * Special Forms::           "Special forms" are idiosyncratic primitives,
  599.                               most of them extremely important.
  600. * Autoloading::             Functions set up to load files
  601.                               containing their real definitions.
  602.  
  603. 
  604. File: elisp,  Node: Self-Evaluating Forms,  Next: Symbol Forms,  Prev: Forms,  Up: Forms
  605.  
  606. Self-Evaluating Forms
  607. ---------------------
  608.  
  609.    A "self-evaluating form" is any form that is not a list or symbol.
  610. Self-evaluating forms evaluate to themselves: the result of evaluation
  611. is the same object that was evaluated.  Thus, the number 25 evaluates to
  612. 25, and the string `"foo"' evaluates to the string `"foo"'. Likewise,
  613. evaluation of a vector does not cause evaluation of the elements of the
  614. vector--it returns the same vector with its contents unchanged.
  615.  
  616.      '123               ; An object, shown without evaluation.
  617.           => 123
  618.      123                ; Evaluated as usual---result is the same.
  619.           => 123
  620.      (eval '123)        ; Evaluated ``by hand''---result is the same.
  621.           => 123
  622.      (eval (eval '123)) ; Evaluating twice changes nothing.
  623.           => 123
  624.  
  625.    It is common to write numbers, characters, strings, and even vectors
  626. in Lisp code, taking advantage of the fact that they self-evaluate.
  627. However, it is quite unusual to do this for types that lack a read
  628. syntax, because it is inconvenient and not very useful; however, it is
  629. possible to put them inside Lisp programs when they are constructed
  630. from subexpressions rather than read.  Here is an example:
  631.  
  632.      ;; Build such an expression.
  633.      (setq buffer (list 'print (current-buffer)))
  634.           => (print #<buffer eval.texi>)
  635.      ;; Evaluate it.
  636.      (eval buffer)
  637.           -| #<buffer eval.texi>
  638.           => #<buffer eval.texi>
  639.  
  640. 
  641. File: elisp,  Node: Symbol Forms,  Next: Classifying Lists,  Prev: Self-Evaluating Forms,  Up: Forms
  642.  
  643. Symbol Forms
  644. ------------
  645.  
  646.    When a symbol is evaluated, it is treated as a variable.  The result
  647. is the variable's value, if it has one.  If it has none (if its value
  648. cell is void), an error is signaled.  For more information on the use of
  649. variables, see *Note Variables::.
  650.  
  651.    In the following example, the value of a symbol is set with `setq'. 
  652. When the symbol is later evaluated, that value is returned.
  653.  
  654.      (setq a 123)
  655.           => 123
  656.      (eval 'a)
  657.           => 123
  658.      a
  659.           => 123
  660.  
  661.    The symbols `nil' and `t' are treated specially, so that the value
  662. of `nil' is always `nil', and the value of `t' is always `t'.  Thus,
  663. these two symbols act like self-evaluating forms, even though `eval'
  664. treats them like any other symbol.
  665.  
  666. 
  667. File: elisp,  Node: Classifying Lists,  Next: Function Forms,  Prev: Symbol Forms,  Up: Forms
  668.  
  669. Classification of List Forms
  670. ----------------------------
  671.  
  672.    A form that is a nonempty list is either a function call, a macro
  673. call, or a special form, according to its first element.  These three
  674. kinds of forms are evaluated in different ways, described below.  The
  675. rest of the list consists of "arguments" for the function, macro or
  676. special form.
  677.  
  678.    The first step in evaluating a nonempty list is to examine its first
  679. element.  This element alone determines what kind of form the list is
  680. and how the rest of the list is to be processed.  The first element is
  681. *not* evaluated, as it would be in some Lisp dialects including Scheme.
  682.  
  683.    If the first element of the list is a symbol, as it most commonly is,
  684. then the symbol's function cell is examined, and its contents are used
  685. instead of the original symbol.  If the contents are another symbol,
  686. this process, called "symbol function indirection", is repeated until a
  687. non-symbol is obtained.
  688.  
  689.    One possible consequence of this process is an infinite loop, in the
  690. event that a symbol's function cell refers to the same symbol.  Or a
  691. symbol may have a void function cell, causing a `void-function' error. 
  692. But if neither of these things happens, we eventually obtain a
  693. non-symbol, which ought to be a function or other suitable object.
  694.  
  695.    More precisely, we should now have a Lisp function (a lambda
  696. expression), a primitive function, a Lisp macro, a special form, or an
  697. autoload object.  Each of these types is a case described in one of the
  698. following sections.  If the object is not one of these types, the error
  699. `invalid-function' is signaled.
  700.  
  701.    The following example illustrates the symbol indirection process.  We
  702. use `fset' to set the function cell of a symbol and `symbol-function'
  703. to get the function cell contents (*note Function Cells::.). 
  704. Specifically, we store the symbol `car' into the function cell of
  705. `first', and the symbol `first' into the function cell of `erste'.
  706.  
  707.      ;; Build this function cell linkage:
  708.      ;;    -------------       -----        -------        -------
  709.      ;;   | #<subr car> | <-- | car |  <-- | first |  <-- | erste |
  710.      ;;    -------------       -----        -------        -------
  711.      
  712.      (symbol-function 'car)
  713.           => #<subr car>
  714.      (fset 'first 'car)
  715.           => car
  716.      (fset 'erste 'first)
  717.           => first
  718.      (erste '(1 2 3))           ; Call the function referenced by `erste'.
  719.           => 1
  720.  
  721.    By contrast, the following example calls a function without any
  722. symbol function indirection, because the first element is an anonymous
  723. Lisp function, not a symbol.
  724.  
  725.      ((lambda (arg) (erste arg))
  726.       '(1 2 3))
  727.           => 1
  728.  
  729. After that function is called, its body is evaluated; this does involve
  730. symbol function indirection when calling `erste'.
  731.  
  732. 
  733. File: elisp,  Node: Function Forms,  Next: Macro Forms,  Prev: Classifying Lists,  Up: Forms
  734.  
  735. Evaluation of Function Forms
  736. ----------------------------
  737.  
  738.    If the first element of a list being evaluated is a Lisp function
  739. object or primitive function object, then that list is a "function
  740. call".  For example, here is a call to the function `+':
  741.  
  742.      (+ 1 x)
  743.  
  744.    When a function call is evaluated, the first step is to evaluate the
  745. remaining elements of the list in the order they appear.  The results
  746. are the actual argument values, one argument from each element.  Then
  747. the function is called with this list of arguments, effectively using
  748. the function `apply' (*note Calling Functions::.).  If the function is
  749. written in Lisp, the arguments are used to bind the argument variables
  750. of the function (*note Lambda Expressions::.); then the forms in the
  751. function body are evaluated in order, and the result of the last one is
  752. used as the value of the function call.
  753.  
  754. 
  755. File: elisp,  Node: Macro Forms,  Next: Special Forms,  Prev: Function Forms,  Up: Forms
  756.  
  757. Lisp Macro Evaluation
  758. ---------------------
  759.  
  760.    If the first element of a list being evaluated is a macro object,
  761. then the list is a "macro call".  When a macro call is evaluated, the
  762. elements of the rest of the list are *not* initially evaluated.
  763. Instead, these elements themselves are used as the arguments of the
  764. macro.  The macro definition computes a replacement form, called the
  765. "expansion" of the macro, which is evaluated in place of the original
  766. form.  The expansion may be any sort of form: a self-evaluating
  767. constant, a symbol or a list.  If the expansion is itself a macro call,
  768. this process of expansion repeats until some other sort of form results.
  769.  
  770.    Normally, the argument expressions are not evaluated as part of
  771. computing the macro expansion, but instead appear as part of the
  772. expansion, so they are evaluated when the expansion is evaluated.
  773.  
  774.    For example, given a macro defined as follows:
  775.  
  776.      (defmacro cadr (x)
  777.        (list 'car (list 'cdr x)))
  778.  
  779. an expression such as `(cadr (assq 'handler list))' is a macro call,
  780. and its expansion is:
  781.  
  782.      (car (cdr (assq 'handler list)))
  783.  
  784. Note that the argument `(assq 'handler list)' appears in the expansion.
  785.  
  786.    *Note Macros::, for a complete description of Emacs Lisp macros.
  787.  
  788. 
  789. File: elisp,  Node: Special Forms,  Next: Autoloading,  Prev: Macro Forms,  Up: Forms
  790.  
  791. Special Forms
  792. -------------
  793.  
  794.    A "special form" is a primitive function specially marked so that
  795. its arguments are not all evaluated.  Special forms define control
  796. structures or perform variable bindings--things which functions cannot
  797. do.
  798.  
  799.    Each special form has its own rules for which arguments are evaluated
  800. and which are used without evaluation.  Whether a particular argument is
  801. evaluated may depend on the results of evaluating other arguments.
  802.  
  803.    Here is a list, in alphabetical order, of all of the special forms in
  804. Emacs Lisp with a reference to where each is described.
  805.  
  806. `and'
  807.      *note Combining Conditions::.
  808.  
  809. `catch'
  810.      *note Catch and Throw::.
  811.  
  812. `cond'
  813.      *note Conditionals::.
  814.  
  815. `condition-case'
  816.      *note Errors::.
  817.  
  818. `defconst'
  819.      *note Defining Variables::.
  820.  
  821. `defmacro'
  822.      *note Defining Macros::.
  823.  
  824. `defun'
  825.      *note Defining Functions::.
  826.  
  827. `defvar'
  828.      *note Defining Variables::.
  829.  
  830. `function'
  831.      *note Anonymous Functions::.
  832.  
  833. `if'
  834.      *note Conditionals::.
  835.  
  836. `interactive'
  837.      *note Interactive Call::.
  838.  
  839. `let'
  840.      *note Local Variables::.
  841.  
  842. `let*'
  843.      *note Local Variables::.
  844.  
  845. `or'
  846.      *note Combining Conditions::.
  847.  
  848. `prog1'
  849.      *note Sequencing::.
  850.  
  851. `prog2'
  852.      *note Sequencing::.
  853.  
  854. `progn'
  855.      *note Sequencing::.
  856.  
  857. `quote'
  858.      *note Quoting::.
  859.  
  860. `save-excursion'
  861.      *note Excursions::.
  862.  
  863. `save-restriction'
  864.      *note Narrowing::.
  865.  
  866. `save-window-excursion'
  867.      *note Window Configurations::.
  868.  
  869. `setq'
  870.      *note Setting Variables::.
  871.  
  872. `setq-default'
  873.      *note Creating Buffer-Local::.
  874.  
  875. `unwind-protect'
  876.      *note Nonlocal Exits::.
  877.  
  878. `while'
  879.      *note Iteration::.
  880.  
  881. `with-output-to-temp-buffer'
  882.      *note Temporary Displays::.
  883.  
  884.      Common Lisp note: here are some comparisons of special forms in
  885.      GNU Emacs Lisp and Common Lisp.  `setq', `if', and `catch' are
  886.      special forms in both Emacs Lisp and Common Lisp. `defun' is a
  887.      special form in Emacs Lisp, but a macro in Common Lisp. 
  888.      `save-excursion' is a special form in Emacs Lisp, but doesn't
  889.      exist in Common Lisp.  `throw' is a special form in Common Lisp
  890.      (because it must be able to throw multiple values), but it is a
  891.      function in Emacs Lisp (which doesn't have multiple values).
  892.  
  893. 
  894. File: elisp,  Node: Autoloading,  Prev: Special Forms,  Up: Forms
  895.  
  896. Autoloading
  897. -----------
  898.  
  899.    The "autoload" feature allows you to call a function or macro whose
  900. function definition has not yet been loaded into Emacs.  When an
  901. autoload object appears as a symbol's function definition and that
  902. symbol is used as a function, Emacs will automatically install the real
  903. definition (plus other associated code) and then call that definition.
  904. (*Note Autoload::.)
  905.  
  906. 
  907. File: elisp,  Node: Quoting,  Prev: Forms,  Up: Evaluation
  908.  
  909. Quoting
  910. =======
  911.  
  912.    The special form `quote' returns its single argument "unchanged".
  913.  
  914.  -- Special Form: quote OBJECT
  915.      This special form returns OBJECT, without evaluating it.  This
  916.      allows symbols and lists, which would normally be evaluated, to be
  917.      included literally in a program.  (It is not necessary to quote
  918.      numbers, strings, and vectors since they are self-evaluating.)  Use
  919.      `function' instead of `quote' when quoting lambda expressions
  920.      (*note Anonymous Functions::.).
  921.  
  922.      Because `quote' is used so often in programs, a convenient read
  923.      syntax is defined for it.  An apostrophe character (`'') followed
  924.      by a Lisp object (in read syntax) expands to a list whose first
  925.      element is `quote', and whose second element is the object.  Thus,
  926.      the read syntax `'x' is an abbreviation for `(quote x)'.
  927.  
  928.      Here are some examples of expressions that use `quote':
  929.  
  930.           (quote (+ 1 2))
  931.                => (+ 1 2)
  932.           (quote foo)
  933.                => foo
  934.           'foo
  935.                => foo
  936.           ''foo
  937.                => (quote foo)
  938.           '(quote foo)
  939.                => (quote foo)
  940.           ['foo]
  941.                => [(quote foo)]
  942.  
  943. 
  944. File: elisp,  Node: Control Structures,  Next: Variables,  Prev: Evaluation,  Up: Top
  945.  
  946. Control Structures
  947. ******************
  948.  
  949.    A Lisp program consists of expressions or "forms" (*note Forms::.).
  950. We control the order of execution of the forms by enclosing them in
  951. "control structures".  Control structures are special forms which
  952. control when, whether, or how many times to execute the forms they
  953. contain.
  954.  
  955.    The simplest control structure is sequential execution: first form
  956. A, then form B, and so on.  This is what happens when you write several
  957. forms in succession in the body of a function, or at top level in a
  958. file of Lisp code--the forms are executed in the order they are
  959. written.  We call this "textual order".  For example, if a function
  960. body consists of two forms A and B, evaluation of the function
  961. evaluates first A and then B, and the function's value is the value of
  962. B.
  963.  
  964.    Naturally, Emacs Lisp has many kinds of control structures, including
  965. other varieties of sequencing, function calls, conditionals, iteration,
  966. and (controlled) jumps.  The built-in control structures are special
  967. forms since their subforms are not necessarily evaluated.  You can use
  968. macros to define your own control structure constructs (*note
  969. Macros::.).
  970.  
  971. * Menu:
  972.  
  973. * Sequencing::             Evaluation in textual order.
  974. * Conditionals::           `if', `cond'.
  975. * Combining Conditions::   `and', `or', `not'.
  976. * Iteration::              `while' loops.
  977. * Nonlocal Exits::         Jumping out of a sequence.
  978.  
  979. 
  980. File: elisp,  Node: Sequencing,  Next: Conditionals,  Prev: Control Structures,  Up: Control Structures
  981.  
  982. Sequencing
  983. ==========
  984.  
  985.    Evaluating forms in the order they are written is the most common
  986. control structure.  Sometimes this happens automatically, such as in a
  987. function body.  Elsewhere you must use a control structure construct to
  988. do this: `progn', the simplest control construct of Lisp.
  989.  
  990.    A `progn' special form looks like this:
  991.  
  992.      (progn A B C ...)
  993.  
  994. and it says to execute the forms A, B, C and so on, in that order. 
  995. These forms are called the body of the `progn' form. The value of the
  996. last form in the body becomes the value of the entire `progn'.
  997.  
  998.    When Lisp was young, `progn' was the only way to execute two or more
  999. forms in succession and use the value of the last of them.  But
  1000. programmers found they often needed to use a `progn' in the body of a
  1001. function, where (at that time) only one form was allowed.  So the body
  1002. of a function was made into an "implicit `progn'": several forms are
  1003. allowed just as in the body of an actual `progn'.  Many other control
  1004. structures likewise contain an implicit `progn'.  As a result, `progn'
  1005. is not used as often as it used to be.  It is needed now most often
  1006. inside of an `unwind-protect', `and', or `or'.
  1007.  
  1008.  -- Special Form: progn FORMS...
  1009.      This special form evaluates all of the FORMS, in textual order,
  1010.      returning the result of the final form.
  1011.  
  1012.           (progn (print "The first form")
  1013.                  (print "The second form")
  1014.                  (print "The third form"))
  1015.                -| "The first form"
  1016.                -| "The second form"
  1017.                -| "The third form"
  1018.           => "The third form"
  1019.  
  1020.    Two other control constructs likewise evaluate a series of forms but
  1021. return a different value:
  1022.  
  1023.  -- Special Form: prog1 FORM1 FORMS...
  1024.      This special form evaluates FORM1 and all of the FORMS, in textual
  1025.      order, returning the result of FORM1.
  1026.  
  1027.           (prog1 (print "The first form")
  1028.                  (print "The second form")
  1029.                  (print "The third form"))
  1030.                -| "The first form"
  1031.                -| "The second form"
  1032.                -| "The third form"
  1033.           => "The first form"
  1034.  
  1035.      Here is a way to remove the first element from a list in the
  1036.      variable `x', then return the value of that former element:
  1037.  
  1038.           (prog1 (car x) (setq x (cdr x)))
  1039.  
  1040.  -- Special Form: prog2 FORM1 FORM2 FORMS...
  1041.      This special form evaluates FORM1, FORM2, and all of the following
  1042.      FORMS, in textual order, returning the result of FORM2.
  1043.  
  1044.           (prog2 (print "The first form")
  1045.                  (print "The second form")
  1046.                  (print "The third form"))
  1047.                -| "The first form"
  1048.                -| "The second form"
  1049.                -| "The third form"
  1050.           => "The second form"
  1051.  
  1052. 
  1053. File: elisp,  Node: Conditionals,  Next: Combining Conditions,  Prev: Sequencing,  Up: Control Structures
  1054.  
  1055. Conditionals
  1056. ============
  1057.  
  1058.    Conditional control structures choose among alternatives.  Emacs Lisp
  1059. has two conditional forms: `if', which is much the same as in other
  1060. languages, and `cond', which is a generalized case statement.
  1061.  
  1062.  -- Special Form: if CONDITION THEN-FORM ELSE-FORMS...
  1063.      `if' chooses between the THEN-FORM and the ELSE-FORMS based on the
  1064.      value of CONDITION.  If the evaluated CONDITION is non-`nil',
  1065.      THEN-FORM is evaluated and the result returned. Otherwise, the
  1066.      ELSE-FORMS are evaluated in textual order, and the value of the
  1067.      last one is returned.  (The ELSE part of `if' is an example of an
  1068.      implicit `progn'.  *Note Sequencing::.)
  1069.  
  1070.      If CONDITION has the value `nil', and no ELSE-FORMS are given,
  1071.      `if' returns `nil'.
  1072.  
  1073.      `if' is a special form because the branch which is not selected is
  1074.      never evaluated--it is ignored.  Thus, in the example below,
  1075.      `true' is not printed because `print' is never called.
  1076.  
  1077.           (if nil
  1078.               (print 'true)
  1079.             'very-false)
  1080.           => very-false
  1081.  
  1082.  -- Special Form: cond CLAUSE...
  1083.      `cond' chooses among an arbitrary number of alternatives.  Each
  1084.      CLAUSE in the `cond' must be a list.  The CAR of this list is the
  1085.      CONDITION; the remaining elements, if any, the BODY-FORMS.  Thus,
  1086.      a clause looks like this:
  1087.  
  1088.           (CONDITION BODY-FORMS...)
  1089.  
  1090.      `cond' tries the clauses in textual order, by evaluating the
  1091.      CONDITION of each clause.  If the value of CONDITION is non-`nil',
  1092.      the BODY-FORMS are evaluated, and the value of the last of
  1093.      BODY-FORMS becomes the value of the `cond'.  The remaining clauses
  1094.      are ignored.
  1095.  
  1096.      If the value of CONDITION is `nil', the clause "fails", so the
  1097.      `cond' moves on to the following clause, trying its CONDITION.
  1098.  
  1099.      If every CONDITION evaluates to `nil', so that every clause fails,
  1100.      `cond' returns `nil'.
  1101.  
  1102.      A clause may also look like this:
  1103.  
  1104.           (CONDITION)
  1105.  
  1106.      Then, if CONDITION is non-`nil' when tested, the value of
  1107.      CONDITION becomes the value of the `cond' form.
  1108.  
  1109.      The following example has four clauses, which test for the cases
  1110.      where the value of `x' is a number, string, buffer and symbol,
  1111.      respectively:
  1112.  
  1113.           (cond ((numberp x) x)
  1114.                  ((stringp x) x)
  1115.                  ((bufferp x)
  1116.                   (setq temporary-hack x) ; multiple body-forms
  1117.                   (buffer-name x))        ; in one clause
  1118.                  ((symbolp x) (symbol-value x)))
  1119.  
  1120.      Often we want the last clause to be executed whenever none of the
  1121.      previous clauses was successful.  To do this, we use `t' as the
  1122.      CONDITION of the last clause, like this: `(t BODY-FORMS)'.  The
  1123.      form `t' evaluates to `t', which is never `nil', so this clause
  1124.      never fails, provided the `cond' gets to it at all.
  1125.  
  1126.      For example,
  1127.  
  1128.           (cond ((eq a 1) 'foo)
  1129.                 (t "default"))
  1130.           => "default"
  1131.  
  1132.      This expression is a `cond' which returns `foo' if the value of
  1133.      `a' is 1, and returns the string `"default"' otherwise.
  1134.  
  1135.    Both `cond' and `if' can usually be written in terms of the other. 
  1136. Therefore, the choice between them is a matter of taste and style.  For
  1137. example:
  1138.  
  1139.      (if A B C)
  1140.      ==
  1141.      (cond (A B) (t C))
  1142.  
  1143. 
  1144. File: elisp,  Node: Combining Conditions,  Next: Iteration,  Prev: Conditionals,  Up: Control Structures
  1145.  
  1146. Constructs for Combining Conditions
  1147. ===================================
  1148.  
  1149.    This section describes three constructs that are often used together
  1150. with `if' and `cond' to express complicated conditions.  The constructs
  1151. `and' and `or' can also be used individually as kinds of multiple
  1152. conditional constructs.
  1153.  
  1154.  -- Function: not CONDITION
  1155.      This function tests for the falsehood of CONDITION.  It returns
  1156.      `t' if CONDITION is `nil', and `nil' otherwise. The function `not'
  1157.      is identical to `null', and we recommend using `null' if you are
  1158.      testing for an empty list.
  1159.  
  1160.  -- Special Form: and CONDITIONS...
  1161.      The `and' special form tests whether all the CONDITIONS are true. 
  1162.      It works by evaluating the CONDITIONS one by one in the order
  1163.      written.
  1164.  
  1165.      If any of the CONDITIONS evaluates to `nil', then the result of
  1166.      the `and' must be `nil' regardless of the remaining CONDITIONS; so
  1167.      the remaining CONDITIONS are ignored and the `and' returns right
  1168.      away.
  1169.  
  1170.      If all the CONDITIONS turn out non-`nil', then the value of the
  1171.      last of them becomes the value of the `and' form.
  1172.  
  1173.      Here is an example.  The first condition returns the integer 1,
  1174.      which is not `nil'.  Similarly, the second condition returns the
  1175.      integer 2, which is not `nil'.  The third condition is `nil', so
  1176.      the remaining condition is never evaluated.
  1177.  
  1178.           (and (print 1) (print 2) nil (print 3))
  1179.                -| 1
  1180.                -| 2
  1181.           => nil
  1182.  
  1183.      Here is a more realistic example of using `and':
  1184.  
  1185.           (if (and (consp foo) (eq (car foo) 'x))
  1186.               (message "foo is a list starting with x"))
  1187.  
  1188.      Note that `(car foo)' is not executed if `(consp foo)' returns
  1189.      `nil', thus avoiding an error.
  1190.  
  1191.      `and' can be expressed in terms of either `if' or `cond'. For
  1192.      example:
  1193.  
  1194.           (and ARG1 ARG2 ARG3)
  1195.           ==
  1196.           (if ARG1 (if ARG2 ARG3))
  1197.           ==
  1198.           (cond (ARG1 (cond (ARG2 ARG3))))
  1199.  
  1200.  -- Special Form: or CONDITIONS...
  1201.      The `or' special form tests whether at least one of the CONDITIONS
  1202.      is true.  It works by evaluating all the CONDITIONS one by one in
  1203.      the order written.
  1204.  
  1205.      If any of the CONDITIONS evaluates to a non-`nil' value, then the
  1206.      result of the `or' must be non-`nil'; so the remaining CONDITIONS
  1207.      are ignored and the `or' returns right away.  The value it returns
  1208.      is the non-`nil' value of the condition just evaluated.
  1209.  
  1210.      If all the CONDITIONS turn out `nil', then the `or' expression
  1211.      returns `nil'.
  1212.  
  1213.      For example, this expression tests whether `x' is either 0 or
  1214.      `nil':
  1215.  
  1216.           (or (eq x nil) (= x 0))
  1217.  
  1218.      Like the `and' construct, `or' can be written in terms of `cond'. 
  1219.      For example:
  1220.  
  1221.           (or ARG1 ARG2 ARG3)
  1222.           ==
  1223.           (cond (ARG1)
  1224.                 (ARG2)
  1225.                 (ARG3))
  1226.  
  1227.      You could almost write `or' in terms of `if', but not quite:
  1228.  
  1229.           (if ARG1 ARG1
  1230.             (if ARG2 ARG2
  1231.               ARG3))
  1232.  
  1233.      This is not completely equivalent because it can evaluate ARG1 or
  1234.      ARG2 twice.  By contrast, `(or ARG1 ARG2 ARG3)' never evaluates
  1235.      any argument more than once.
  1236.  
  1237. 
  1238. File: elisp,  Node: Iteration,  Next: Nonlocal Exits,  Prev: Combining Conditions,  Up: Control Structures
  1239.  
  1240. Iteration
  1241. =========
  1242.  
  1243.    Iteration means executing part of a program repetitively.  For
  1244. example, you might want to repeat some expressions once for each
  1245. element of a list, or once for each integer from 0 to N.  You can do
  1246. this in Emacs Lisp with the special form `while':
  1247.  
  1248.  -- Special Form: while CONDITION FORMS...
  1249.      `while' first evaluates CONDITION.  If the result is non-`nil', it
  1250.      evaluates FORMS in textual order.  Then it reevaluates CONDITION,
  1251.      and if the result is non-`nil', it evaluates FORMS again.  This
  1252.      process repeats until CONDITION evaluates to `nil'.
  1253.  
  1254.      There is no limit on the number of iterations that may occur.  The
  1255.      loop will continue until either CONDITION evaluates to `nil' or
  1256.      until an error or `throw' jumps out of it (*note Nonlocal
  1257.      Exits::.).
  1258.  
  1259.      The value of a `while' form is always `nil'.
  1260.  
  1261.           (setq num 0)
  1262.                => 0
  1263.           (while (< num 4)
  1264.             (princ (format "Iteration %d." num))
  1265.             (setq num (1+ num)))
  1266.                -| Iteration 0.
  1267.                -| Iteration 1.
  1268.                -| Iteration 2.
  1269.                -| Iteration 3.
  1270.                => nil
  1271.  
  1272.      If you would like to execute something on each iteration before the
  1273.      end-test, put it together with the end-test in a `progn' as the
  1274.      first argument of `while', as shown here:
  1275.  
  1276.           (while (progn
  1277.                    (forward-line 1)
  1278.                    (not (looking-at "^$"))))
  1279.  
  1280.      This moves forward one line and continues moving by lines until an
  1281.      empty line is reached.
  1282.  
  1283.