home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional / OS2PRO194.ISO / os2 / prgramer / unix / info / elisp.i07 < prev    next >
Encoding:
GNU Info File  |  1993-06-14  |  50.3 KB  |  1,221 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: Scope,  Next: Extent,  Prev: Variable Scoping,  Up: Variable Scoping
  30.  
  31. Scope
  32. -----
  33.  
  34.    Emacs Lisp uses "indefinite scope" for local variable bindings. This
  35. means that any function anywhere in the program text might access a
  36. given binding of a variable.  Consider the following function
  37. definitions:
  38.  
  39.      (defun binder (x)  ; `x' is bound in `binder'.
  40.         (foo 5))        ; `foo' is some other function.
  41.      
  42.      (defun user ()     ; `x' is used in `user'.
  43.        (list x))
  44.  
  45.    In a lexically scoped language, the binding of `x' from `binder'
  46. would never be accessible in `user', because `user' is not textually
  47. contained within the function `binder'.  However, in dynamically scoped
  48. Emacs Lisp, `user' may or may not refer to the binding of `x'
  49. established in `binder', depending on circumstances:
  50.  
  51.    * If we call `user' directly without calling `binder' at all, then
  52.      whatever binding of `x' is found, it cannot come from `binder'.
  53.  
  54.    * If we define `foo' as follows and call `binder', then the binding
  55.      made in `binder' will be seen in `user':
  56.  
  57.           (defun foo (lose)
  58.             (user))
  59.  
  60.    * If we define `foo' as follows and call `binder', then the binding
  61.      made in `binder' *will not* be seen in `user':
  62.  
  63.           (defun foo (x)
  64.             (user))
  65.  
  66.      Here, when `foo' is called by `binder', it binds `x'. (The binding
  67.      in `foo' is said to "shadow" the one made in `binder'.) 
  68.      Therefore, `user' will access the `x' bound by `foo' instead of
  69.      the one bound by `binder'.
  70.  
  71. 
  72. File: elisp,  Node: Extent,  Next: Impl of Scope,  Prev: Scope,  Up: Variable Scoping
  73.  
  74. Extent
  75. ------
  76.  
  77.    "Extent" refers to the time during program execution that a variable
  78. name is valid.  In Emacs Lisp, a variable is valid only while the form
  79. that bound it is executing.  This is called "dynamic extent".  "Local"
  80. or "automatic" variables in most languages, including C and Pascal,
  81. have dynamic extent.
  82.  
  83.    One alternative to dynamic extent is "indefinite extent".  This
  84. means that a variable binding can live on past the exit from the form
  85. that made the binding.  Common Lisp and Scheme, for example, support
  86. this, but Emacs Lisp does not.
  87.  
  88.    To illustrate this, the function below, `make-add', returns a
  89. function that purports to add N to its own argument M. This would work
  90. in Common Lisp, but it does not work as intended in Emacs Lisp, because
  91. after the call to `make-add' exits, the variable `n' is no longer bound
  92. to the actual argument 2.
  93.  
  94.      (defun make-add (n)
  95.          (function (lambda (m) (+ n m))))  ; Return a function.
  96.           => make-add
  97.      (fset 'add2 (make-add 2))  ; Define function `add2' with `(make-add 2)'.
  98.           => (lambda (m) (+ n m))
  99.      (add2 4)                   ; Try to add 2 to 4.
  100.      error--> Symbol's value as variable is void: n
  101.  
  102. 
  103. File: elisp,  Node: Impl of Scope,  Next: Using Scoping,  Prev: Extent,  Up: Variable Scoping
  104.  
  105. Implementation of Dynamic Scoping
  106. ---------------------------------
  107.  
  108.    A simple sample implementation (which is not how Emacs Lisp actually
  109. works) may help you understand dynamic binding.  This technique is
  110. called "deep binding" and was used in early Lisp systems.
  111.  
  112.    Suppose there is a stack of bindings: variable-value pairs.  At entry
  113. to a function or to a `let' form, we can push bindings on the stack for
  114. the arguments or local variables created there.  We can pop those
  115. bindings from the stack at exit from the binding construct.
  116.  
  117.    We can find the value of a variable by searching the stack from top
  118. to bottom for a binding for that variable; the value from that binding
  119. is the value of the variable.  To set the variable, we search for the
  120. current binding, then store the new value into that binding.
  121.  
  122.    As you can see, a function's bindings remain in effect as long as it
  123. continues execution, even during its calls to other functions.  That is
  124. why we say the extent of the binding is dynamic.  And any other function
  125. can refer to the bindings, if it uses the same variables while the
  126. bindings are in effect.  That is why we say the scope is indefinite.
  127.  
  128.    The actual implementation of variable scoping in GNU Emacs Lisp uses
  129. a technique called "shallow binding".  Each variable has a standard
  130. place in which its current value is always found--the value cell of the
  131. symbol.
  132.  
  133.    In shallow binding, setting the variable works by storing a value in
  134. the value cell.  When a new local binding is created, the local value is
  135. stored in the value cell, and the old value (belonging to a previous
  136. binding) is pushed on a stack.  When a binding is eliminated, the old
  137. value is popped off the stack and stored in the value cell.
  138.  
  139.    We use shallow binding because it has the same results as deep
  140. binding, but runs faster, since there is never a need to search for a
  141. binding.
  142.  
  143. 
  144. File: elisp,  Node: Using Scoping,  Prev: Impl of Scope,  Up: Variable Scoping
  145.  
  146. Proper Use of Dynamic Scoping
  147. -----------------------------
  148.  
  149.    Binding a variable in one function and using it in another is a
  150. powerful technique, but if used without restraint, it can make programs
  151. hard to understand.  There are two clean ways to use this technique:
  152.  
  153.    * Use or bind the variable only in a few related functions, written
  154.      close together in one file.  Such a variable is used for
  155.      communication within one program.
  156.  
  157.      You should write comments to inform other programmers that they
  158.      can see all uses of the variable before them, and to advise them
  159.      not to add uses elsewhere.
  160.  
  161.    * Give the variable a well-defined, documented meaning, and make all
  162.      appropriate functions refer to it (but not bind it or set it)
  163.      wherever that meaning is relevant.  For example, the variable
  164.      `case-fold-search' is defined as "non-`nil' means ignore case when
  165.      searching"; various search and replace functions refer to it
  166.      directly or through their subroutines, but do not bind or set it.
  167.  
  168.      Then you can bind the variable in other programs, knowing reliably
  169.      what the effect will be.
  170.  
  171. 
  172. File: elisp,  Node: Buffer-Local Variables,  Prev: Variable Scoping,  Up: Variables
  173.  
  174. Buffer-Local Variables
  175. ======================
  176.  
  177.    Global and local variable bindings are found in most programming
  178. languages in one form or another.  Emacs also supports another, unusual
  179. kind of variable binding: "buffer-local" bindings, which apply only to
  180. one buffer.  Emacs Lisp is meant for programming editing commands, and
  181. having different values for a variable in different buffers is an
  182. important customization method.
  183.  
  184. * Menu:
  185.  
  186. * Intro to Buffer-Local::      Introduction and concepts.
  187. * Creating Buffer-Local::      Creating and destroying buffer-local bindings.
  188. * Default Value::              The default value is seen in buffers
  189.                                  that don't have their own local values.
  190.  
  191. 
  192. File: elisp,  Node: Intro to Buffer-Local,  Next: Creating Buffer-Local,  Prev: Buffer-Local Variables,  Up: Buffer-Local Variables
  193.  
  194. Introduction to Buffer-Local Variables
  195. --------------------------------------
  196.  
  197.    A buffer-local variable has a buffer-local binding associated with a
  198. particular buffer.  The binding is in effect when that buffer is
  199. current; otherwise, it is not in effect.  If you set the variable while
  200. a buffer-local binding is in effect, the new value goes in that binding,
  201. so the global binding is unchanged; this means that the change is
  202. visible in that buffer alone.
  203.  
  204.    A variable may have buffer-local bindings in some buffers but not in
  205. others.  The global binding is shared by all the buffers that don't have
  206. their own bindings.  Thus, if you set the variable in a buffer that does
  207. not have a buffer-local binding for it, the new value is visible in all
  208. buffers except those with buffer-local bindings.  (Here we are assuming
  209. that there are no `let'-style local bindings to complicate the issue.)
  210.  
  211.    The most common use of buffer-local bindings is for major modes to
  212. change variables that control the behavior of commands.  For example, C
  213. mode and Lisp mode both set the variable `paragraph-start' to specify
  214. that only blank lines separate paragraphs.  They do this by making the
  215. variable buffer-local in the buffer that is being put into C mode or
  216. Lisp mode, and then setting it to the new value for that mode.
  217.  
  218.    The usual way to make a buffer-local binding is with
  219. `make-local-variable', which is what major mode commands use.  This
  220. affects just the current buffer; all other buffers (including those yet
  221. to be created) continue to share the global value.
  222.  
  223.    A more powerful operation is to mark the variable as "automatically
  224. buffer-local" by calling `make-variable-buffer-local'.  You can think
  225. of this as making the variable local in all buffers, even those yet to
  226. be created.  More precisely, the effect is that setting the variable
  227. automatically makes the variable local to the current buffer if it is
  228. not already so.  All buffers start out by sharing the global value of
  229. the variable as usual, but any `setq' creates a buffer-local binding
  230. for the current buffer.  The new value is stored in the buffer-local
  231. binding, leaving the (default) global binding untouched.  The global
  232. value can no longer be changed with `setq'; you need to use
  233. `setq-default' to do that.
  234.  
  235.    When a variable has local values in one or more buffers, you can get
  236. Emacs very confused by binding the variable with `let', changing to a
  237. different current buffer in which a different binding is in effect, and
  238. then exiting the `let'.  The best way to preserve your sanity is to
  239. avoid such situations.  If you use `save-excursion' around each piece
  240. of code that changes to a different current buffer, you will not have
  241. this problem.  Here is an example of incorrect code:
  242.  
  243.      (setq foo 'b)
  244.      (set-buffer "a")
  245.      (make-local-variable 'foo)
  246.      (setq foo 'a)
  247.      (let ((foo 'temp))
  248.        (set-buffer "b")
  249.        ...)
  250.      foo => 'a      ; The old buffer-local value from buffer `a'
  251.                            ; is now the default value.
  252.      (set-buffer "a")
  253.      foo => 'temp   ; The local value that should be gone
  254.                            ; is now the buffer-local value in buffer `a'.
  255.  
  256. But `save-excursion' as shown here avoids the problem:
  257.  
  258.      (let ((foo 'temp))
  259.        (save-excursion
  260.          (set-buffer "b")
  261.          ...))
  262.  
  263.    Local variables in a file you edit are also represented by
  264. buffer-local bindings for the buffer that holds the file within Emacs.
  265. *Note Auto Major Mode::.
  266.  
  267. 
  268. File: elisp,  Node: Creating Buffer-Local,  Next: Default Value,  Prev: Intro to Buffer-Local,  Up: Buffer-Local Variables
  269.  
  270. Creating and Destroying Buffer-local Bindings
  271. ---------------------------------------------
  272.  
  273.  -- Command: make-local-variable SYMBOL
  274.      This function creates a buffer-local binding for SYMBOL in the
  275.      current buffer.  Other buffers are not affected.  The value
  276.      returned is SYMBOL.
  277.  
  278.      The buffer-local value of SYMBOL starts out as the same value
  279.      SYMBOL previously had.
  280.  
  281.           ;; In buffer `b1':
  282.           (setq foo 5)                ; Affects all buffers.
  283.                => 5
  284.           (make-local-variable 'foo)  ; Now it is local in `b1'.
  285.                => foo
  286.           foo                         ; That did not change the value.
  287.                => 5
  288.           (setq foo 6)                ; Change the value in `b1'.
  289.                => 6
  290.           foo
  291.                => 6
  292.           
  293.           ;; In buffer `b2', the value hasn't changed.
  294.           (save-excursion
  295.             (set-buffer "b2")
  296.             foo)
  297.                => 5
  298.  
  299.  -- Command: make-variable-buffer-local SYMBOL
  300.      This function marks SYMBOL automatically buffer-local, so that any
  301.      attempt to set it will make it local to the current buffer at the
  302.      time.
  303.  
  304.      The value returned is SYMBOL.
  305.  
  306.  -- Function: buffer-local-variables &optional BUFFER
  307.      This function tells you what the buffer-local variables are in
  308.      buffer BUFFER.  It returns an association list (*note Association
  309.      Lists::.) in which each association contains one buffer-local
  310.      variable and its value.  If BUFFER is omitted, the current buffer
  311.      is used.
  312.  
  313.           (setq lcl (buffer-local-variables))
  314.           => ((fill-column . 75)
  315.               (case-fold-search . t)
  316.               ...
  317.               (mark-ring #<marker at 5454 in buffers.texi>)
  318.               (require-final-newline . t))
  319.  
  320.      Note that storing new values into the CDRs of the elements in this
  321.      list will *not* change the local values of the variables.
  322.  
  323.  -- Command: kill-local-variable SYMBOL
  324.      This function deletes the buffer-local binding (if any) for SYMBOL
  325.      in the current buffer.  As a result, the global (default) binding
  326.      of SYMBOL becomes visible in this buffer.  Usually this results in
  327.      a change in the value of SYMBOL, since the global value is usually
  328.      different from the buffer-local value just eliminated.
  329.  
  330.      It is possible to kill the local binding of a variable that
  331.      automatically becomes local when set.  This causes the variable to
  332.      show its global value in the current buffer.  However, if you set
  333.      the variable again, this will once again create a local value.
  334.  
  335.      `kill-local-variable' returns SYMBOL.
  336.  
  337.  -- Function: kill-all-local-variables
  338.      This function eliminates all the buffer-local variable bindings of
  339.      the current buffer.  As a result, the buffer will see the default
  340.      values of all variables.  This function also resets certain other
  341.      information pertaining to the buffer: its local keymap is set to
  342.      `nil', its syntax table is set to the value of
  343.      `standard-syntax-table', and its abbrev table is set to the value
  344.      of `fundamental-mode-abbrev-table'.
  345.  
  346.      Every major mode command begins by calling this function, which
  347.      has the effect of switching to Fundamental mode and erasing most
  348.      of the effects of the previous major mode.
  349.  
  350.      `kill-all-local-variables' returns `nil'.
  351.  
  352. 
  353. File: elisp,  Node: Default Value,  Prev: Creating Buffer-Local,  Up: Buffer-Local Variables
  354.  
  355. The Default Value of a Buffer-Local Variable
  356. --------------------------------------------
  357.  
  358.    The global value of a variable with buffer-local bindings is also
  359. called the "default" value, because it is the value that is in effect
  360. except when specifically overridden.
  361.  
  362.    The functions `default-value' and `setq-default' allow you to access
  363. and change the default value regardless of whether the current buffer
  364. has a buffer-local binding.  For example, you could use `setq-default'
  365. to change the default setting of `paragraph-start' for most buffers;
  366. and this would work even when you are in a C or Lisp mode buffer which
  367. has a buffer-local value for this variable.
  368.  
  369.  -- Function: default-value SYMBOL
  370.      This function returns SYMBOL's default value.  This is the value
  371.      that is seen in buffers that do not have their own values for this
  372.      variable.  If SYMBOL is not buffer-local, this is equivalent to
  373.      `symbol-value' (*note Accessing Variables::.).
  374.  
  375.  -- Special Form: setq-default SYMBOL VALUE
  376.      This sets the default value of SYMBOL to VALUE. SYMBOL is not
  377.      evaluated, but VALUE is.  The value of the `setq-default' form is
  378.      VALUE.
  379.  
  380.      If a SYMBOL is not buffer-local for the current buffer, and is not
  381.      marked automatically buffer-local, this has the same effect as
  382.      `setq'.  If SYMBOL is buffer-local for the current buffer, then
  383.      this changes the value that other buffers will see (as long as they
  384.      don't have a buffer-local value), but not the value that the
  385.      current buffer sees.
  386.  
  387.           ;; In buffer `foo':
  388.           (make-local-variable 'local)
  389.                => local
  390.           (setq local 'value-in-foo)
  391.                => value-in-foo
  392.           (setq-default local 'new-default)
  393.                => new-default
  394.           local
  395.                => value-in-foo
  396.           (default-value 'local)
  397.                => new-default
  398.           
  399.           ;; In (the new) buffer `bar':
  400.           local
  401.                => new-default
  402.           (default-value 'local)
  403.                => new-default
  404.           (setq local 'another-default)
  405.                => another-default
  406.           (default-value 'local)
  407.                => another-default
  408.           
  409.           ;; Back in buffer `foo':
  410.           local
  411.                => value-in-foo
  412.           (default-value 'local)
  413.                => another-default
  414.  
  415.  -- Function: set-default SYMBOL VALUE
  416.      This function is like `setq-default', except that SYMBOL is
  417.      evaluated.
  418.  
  419.           (set-default (car '(a b c)) 23)
  420.                => 23
  421.           (default-value 'a)
  422.                => 23
  423.  
  424. 
  425. File: elisp,  Node: Functions,  Next: Macros,  Prev: Variables,  Up: Top
  426.  
  427. Functions
  428. *********
  429.  
  430.    A Lisp program is composed mainly of Lisp functions.  This chapter
  431. explains what functions are, how they accept arguments, and how to
  432. define them.
  433.  
  434. * Menu:
  435.  
  436. * What Is a Function::    Lisp functions vs primitives; terminology.
  437. * Lambda Expressions::    How functions are expressed as Lisp objects.
  438. * Function Names::        A symbol can serve as the name of a function.
  439. * Defining Functions::    Lisp expressions for defining functions.
  440. * Calling Functions::     How to use an existing function.
  441. * Mapping Functions::     Applying a function to each element of a list, etc.
  442. * Anonymous Functions::   Lambda-expressions are functions with no names.
  443. * Function Cells::        Accessing or setting the function definition
  444.                             of a symbol.
  445. * Related Topics::        Cross-references to specific Lisp primitives
  446.                             that have a special bearing on how functions work.
  447.  
  448. 
  449. File: elisp,  Node: What Is a Function,  Next: Lambda Expressions,  Prev: Functions,  Up: Functions
  450.  
  451. What Is a Function?
  452. ===================
  453.  
  454.    In a general sense, a function is a rule for carrying on a
  455. computation given several values called "arguments".  The result of the
  456. computation is called the value of the function.  The computation can
  457. also have side effects: lasting changes in the values of variables or
  458. the contents of data structures.
  459.  
  460.    Here are important terms for functions in Emacs Lisp and for other
  461. function-like objects.
  462.  
  463. "function"
  464.      In Emacs Lisp, a "function" is anything that can be applied to
  465.      arguments in a Lisp program.  In some cases, we will use it more
  466.      specifically to mean a function written in Lisp.  Special forms and
  467.      macros are not functions.
  468.  
  469. "primitive"
  470.      A "primitive" is a function callable from Lisp that is written in
  471.      C, such as `car' or `append'.  These functions are also called
  472.      "built-in" functions or "subrs".  (Special forms are also
  473.      considered primitives.)
  474.  
  475.      Primitives provide the lowest-level interfaces to editing
  476.      functions or operating system services, or in a few cases they
  477.      perform important operations more quickly than a Lisp program
  478.      could.  Primitives can be modified or added only by changing the C
  479.      sources and recompiling the editor.  See *Note Writing Emacs
  480.      Primitives::.
  481.  
  482. "lambda expression"
  483.      A "lambda expression" is a function written in Lisp. These are
  484.      described in the following section. *Note Lambda Expressions::.
  485.  
  486. "special form"
  487.      A "special form" is a primitive that is like a function but does
  488.      not evaluate all of its arguments in the usual way.  It may
  489.      evaluate only some of the arguments, or may evaluate them in an
  490.      unusual order, or several times.  Many special forms are described
  491.      in *Note Control Structures::.
  492.  
  493. "macro"
  494.      A "macro" is a construct defined in Lisp by the programmer.  It
  495.      differs from a function in that it translates a Lisp expression
  496.      that you write into an equivalent expression to be evaluated
  497.      instead of the original expression.  *Note Macros::, for how to
  498.      define and use macros.
  499.  
  500. "command"
  501.      A "command" is an object that `command-execute' can invoke; it is
  502.      a possible definition for a key sequence.  Some functions are
  503.      commands; a function written in Lisp is a command if it contains an
  504.      interactive declaration (*note Defining Commands::.).  Such a
  505.      function can be called from Lisp expressions like other functions;
  506.      in this case, the fact that the function is a command makes no
  507.      difference.
  508.  
  509.      Strings are commands also, even though they are not functions.  A
  510.      symbol is a command if its function definition is a command; such
  511.      symbols can be invoked with `M-x'.  The symbol is a function as
  512.      well if the definition is a function.  *Note Command Overview::.
  513.  
  514. "keystroke command"
  515.      A "keystroke command" is a command that is bound to a key sequence
  516.      (typically one to three keystrokes).  The distinction is made here
  517.      merely to avoid confusion with the meaning of "command" in
  518.      non-Emacs editors; for programmers, the distinction is normally
  519.      unimportant.
  520.  
  521.  -- Function: subrp OBJECT
  522.      This function returns `t' if OBJECT is a built-in function (i.e. a
  523.      Lisp primitive).
  524.  
  525.           (subrp 'message)                ; `message' is a symbol,
  526.                => nil                     ; not a subr object.
  527.           (subrp (symbol-function 'message))
  528.                => t
  529.  
  530. 
  531. File: elisp,  Node: Lambda Expressions,  Next: Function Names,  Prev: What Is a Function,  Up: Functions
  532.  
  533. Lambda Expressions
  534. ==================
  535.  
  536.    A function written in Lisp is a list that looks like this:
  537.  
  538.      (lambda (ARG-VARIABLES...)
  539.        [DOCUMENTATION-STRING]
  540.        [INTERACTIVE-DECLARATION]
  541.        BODY-FORMS...)
  542.  
  543. (Such a list is called a "lambda expression", even though it is not an
  544. expression at all, for historical reasons.)
  545.  
  546. * Menu:
  547.  
  548. * Lambda Components::       The parts of a lambda expression.
  549. * Simple Lambda::           A simple example.
  550. * Argument List::           Details and special features of argument lists.
  551. * Function Documentation::  How to put documentation in a function.
  552.  
  553. 
  554. File: elisp,  Node: Lambda Components,  Next: Simple Lambda,  Prev: Lambda Expressions,  Up: Lambda Expressions
  555.  
  556. Components of a Lambda Expression
  557. ---------------------------------
  558.  
  559.    A function written in Lisp (a "lambda expression") is a list that
  560. looks like this:
  561.  
  562.      (lambda (ARG-VARIABLES...)
  563.        [DOCUMENTATION-STRING]
  564.        [INTERACTIVE-DECLARATION]
  565.        BODY-FORMS...)
  566.  
  567.    The first element of a lambda expression is always the symbol
  568. `lambda'.  This indicates that the list represents a function.  The
  569. reason functions are defined to start with `lambda' is so that other
  570. lists, intended for other uses, will not accidentally be valid as
  571. functions.
  572.  
  573.    The second element is a list of argument variable names (symbols).
  574. This is called the "lambda list".  When a Lisp function is called, the
  575. argument values are matched up against the variables in the lambda
  576. list, which are given local bindings with the values provided. *Note
  577. Local Variables::.
  578.  
  579.    The documentation string is an actual string that serves to describe
  580. the function for the Emacs help facilities.  *Note Function
  581. Documentation::.
  582.  
  583.    The interactive declaration is a list of the form `(interactive
  584. CODE-STRING)'.  This declares how to provide arguments if the function
  585. is used interactively.  Functions with this declaration are called
  586. "commands"; they can be called using `M-x' or bound to a key. Functions
  587. not intended to be called in this way should not have interactive
  588. declarations.  *Note Defining Commands::, for how to write an
  589. interactive declaration.
  590.  
  591.    The rest of the elements are the "body" of the function: the Lisp
  592. code to do the work of the function (or, as a Lisp programmer would say,
  593. "a list of Lisp forms to evaluate").  The value returned by the
  594. function is the value returned by the last element of the body.
  595.  
  596. 
  597. File: elisp,  Node: Simple Lambda,  Next: Argument List,  Prev: Lambda Components,  Up: Lambda Expressions
  598.  
  599. A Simple Lambda-Expression Example
  600. ----------------------------------
  601.  
  602.    Consider for example the following function:
  603.  
  604.      (lambda (a b c) (+ a b c))
  605.  
  606. We can call this function by writing it as the CAR of an expression,
  607. like this:
  608.  
  609.      ((lambda (a b c) (+ a b c))
  610.       1 2 3)
  611.  
  612. The body of this lambda expression is evaluated with the variable `a'
  613. bound to 1, `b' bound to 2, and `c' bound to 3. Evaluation of the body
  614. adds these three numbers, producing the result 6; therefore, this call
  615. to the function returns the value 6.
  616.  
  617.    Note that the arguments can be the results of other function calls,
  618. as in this example:
  619.  
  620.      ((lambda (a b c) (+ a b c))
  621.       1 (* 2 3) (- 5 4))
  622.  
  623. Here all the arguments `1', `(* 2 3)', and `(- 5 4)' are evaluated,
  624. left to right.  Then the lambda expression is applied to the argument
  625. values 1, 6 and 1 to produce the value 8.
  626.  
  627.    It is not often useful to write a lambda expression as the CAR of a
  628. form in this way.  You can get the same result, of making local
  629. variables and giving them values, using the special form `let' (*note
  630. Local Variables::.).  And `let' is clearer and easier to use. In
  631. practice, lambda expressions are either stored as the function
  632. definitions of symbols, to produce named functions, or passed as
  633. arguments to other functions (*note Anonymous Functions::.).
  634.  
  635.    However, calls to explicit lambda expressions were very useful in the
  636. old days of Lisp, before the special form `let' was invented.  At that
  637. time, they were the only way to bind and initialize local variables.
  638.  
  639. 
  640. File: elisp,  Node: Argument List,  Next: Function Documentation,  Prev: Simple Lambda,  Up: Lambda Expressions
  641.  
  642. Advanced Features of Argument Lists
  643. -----------------------------------
  644.  
  645.    Our simple sample function, `(lambda (a b c) (+ a b c))', specifies
  646. three argument variables, so it must be called with three arguments: if
  647. you try to call it with only two arguments or four arguments, you will
  648. get a `wrong-number-of-arguments' error.
  649.  
  650.    It is often convenient to write a function that allows certain
  651. arguments to be omitted.  For example, the function `substring' accepts
  652. three arguments--a string, the start index and the end index--but the
  653. third argument defaults to the end of the string if you omit it.  It is
  654. also convenient for certain functions to accept an indefinite number of
  655. arguments, as the functions `and' and `+' do.
  656.  
  657.    To specify optional arguments that may be omitted when a function is
  658. called, simply include the keyword `&optional' before the optional
  659. arguments.  To specify a list of zero or more extra arguments, include
  660. the keyword `&rest' before one final argument.
  661.  
  662.    Thus, the complete syntax for an argument list is as follows:
  663.  
  664.      (REQUIRED-VARS...
  665.       [&optional OPTIONAL-VARS...]
  666.       [&rest REST-VAR])
  667.  
  668. The square brackets indicate that the `&optional' and `&rest' clauses,
  669. and the variables that follow them, are optional.
  670.  
  671.    A call to the function requires one actual argument for each of the
  672. REQUIRED-VARS.  There may be actual arguments for zero or more of the
  673. OPTIONAL-VARS, and there cannot be any more actual arguments than these
  674. unless `&rest' exists.  In that case, there may be any number of extra
  675. actual arguments.
  676.  
  677.    If actual arguments for the optional and rest variables are omitted,
  678. then they always default to `nil'.  However, the body of the function
  679. is free to consider `nil' an abbreviation for some other meaningful
  680. value.  This is what `substring' does; `nil' as the third argument
  681. means to use the length of the string supplied.  There is no way for the
  682. function to distinguish between an explicit argument of `nil' and an
  683. omitted argument.
  684.  
  685.      Common Lisp note: Common Lisp allows the function to specify what
  686.      default values will be used when an optional argument is omitted;
  687.      GNU Emacs Lisp always uses `nil'.
  688.  
  689.    For example, an argument list that looks like this:
  690.  
  691.      (a b &optional c d &rest e)
  692.  
  693. binds `a' and `b' to the first two actual arguments, which are
  694. required.  If one or two more arguments are provided, `c' and `d' are
  695. bound to them respectively; any arguments after the first four are
  696. collected into a list and `e' is bound to that list.  If there are only
  697. two arguments, `c' is `nil'; if two or three arguments, `d' is `nil';
  698. if four arguments or fewer, `e' is `nil'.
  699.  
  700.    There is no way to have required arguments following optional
  701. ones--it would not make sense.  To see why this must be so, suppose
  702. that `c' in the example were optional and `d' were required. If three
  703. actual arguments are given; then which variable would the third
  704. argument be for?  Similarly, it makes no sense to have any more
  705. arguments (either required or optional) after a `&rest' argument.
  706.  
  707.    Here are some examples of argument lists and proper calls:
  708.  
  709.      ((lambda (n) (1+ n))                ; One required:
  710.       1)                                 ; requires exactly one argument.
  711.           => 2
  712.      ((lambda (n &optional n1)           ; One required and one optional:
  713.               (if n1 (+ n n1) (1+ n)))   ; 1 or 2 arguments.
  714.       1 2)
  715.           => 3
  716.      ((lambda (n &rest ns)               ; One required and one rest:
  717.               (+ n (apply '+ ns)))       ; 1 or more arguments.
  718.       1 2 3 4 5)
  719.           => 15
  720.  
  721. 
  722. File: elisp,  Node: Function Documentation,  Prev: Argument List,  Up: Lambda Expressions
  723.  
  724. Documentation Strings of Functions
  725. ----------------------------------
  726.  
  727.    A lambda expression may optionally have a "documentation string" just
  728. after the lambda list.  This string does not affect execution of the
  729. function; it is a kind of comment, but a systematized comment which
  730. actually appears inside the Lisp world and can be used by the Emacs help
  731. facilities.  *Note Documentation::, for how the DOCUMENTATION-STRING is
  732. accessed.
  733.  
  734.    It is a good idea to provide documentation strings for all commands,
  735. and for all other functions in your program that users of your program
  736. should know about; internal functions might as well have only comments,
  737. since comments don't take up any room when your program is loaded.
  738.  
  739.    The first line of the documentation string should stand on its own,
  740. because `apropos' displays just this first line.  It should consist of
  741. one or two complete sentences that summarize the function's purpose.
  742.  
  743.    The start of the documentation string is usually indented, but since
  744. these spaces come before the starting double-quote, they are not part of
  745. the string.  Some people make a practice of indenting any additional
  746. lines of the string so that the text lines up.  *This is a mistake.* 
  747. The indentation of the following lines is inside the string; what looks
  748. nice in the source code will look ugly when displayed by the help
  749. commands.
  750.  
  751.    You may wonder how the documentation string could be optional, since
  752. there are required components of the function that follow it (the body).
  753. Since evaluation of a string returns that string, without any side
  754. effects, it has no effect if it is not the last form in the body. 
  755. Thus, in practice, there is no confusion between the first form of the
  756. body and the documentation string; if the only body form is a string
  757. then it serves both as the return value and as the documentation.
  758.  
  759. 
  760. File: elisp,  Node: Function Names,  Next: Defining Functions,  Prev: Lambda Expressions,  Up: Functions
  761.  
  762. Naming a Function
  763. =================
  764.  
  765.    In most computer languages, every function has a name; the idea of a
  766. function without a name is nonsensical.  In Lisp, a function in the
  767. strictest sense has no name.  It is simply a list whose first element is
  768. `lambda', or a primitive subr-object.
  769.  
  770.    However, a symbol can serve as the name of a function.  This happens
  771. when you put the function in the symbol's "function cell" (*note Symbol
  772. Components::.).  Then the symbol itself becomes a valid, callable
  773. function, equivalent to the list or subr-object that its function cell
  774. refers to.  The contents of the function cell are also called the
  775. symbol's "function definition".
  776.  
  777.    In practice, nearly all functions are given names in this way and
  778. referred to through their names.  For example, the symbol `car' works
  779. as a function and does what it does because the primitive subr-object
  780. `#<subr car>' is stored in its function cell.
  781.  
  782.    We give functions names because it is more convenient to refer to
  783. them by their names in other functions.  For primitive subr-objects
  784. such as `#<subr car>', names are the only way you can refer to them:
  785. there is no read syntax for such objects.  For functions written in
  786. Lisp, the name is more convenient to use in a call than an explicit
  787. lambda expression.  Also, a function with a name can refer to
  788. itself--it can be recursive.  Writing the function's name in its own
  789. definition is much more convenient than making the function definition
  790. point to itself (something that is not impossible but that has various
  791. disadvantages in practice).
  792.  
  793.    Functions are often identified with the symbols used to name them. 
  794. For example, we often speak of "the function `car'", not distinguishing
  795. between the symbol `car' and the primitive subr-object that is its
  796. function definition.  For most purposes, there is no need to
  797. distinguish.
  798.  
  799.    Even so, keep in mind that a function need not have a unique name. 
  800. While a given function object *usually* appears in the function cell of
  801. only one symbol, this is just a matter of convenience.  It is very easy
  802. to store it in several symbols using `fset'; then each of the symbols is
  803. equally well a name for the same function.
  804.  
  805.    A symbol used as a function name may also be used as a variable;
  806. these two uses of a symbol are independent and do not conflict.
  807.  
  808. 
  809. File: elisp,  Node: Defining Functions,  Next: Calling Functions,  Prev: Function Names,  Up: Functions
  810.  
  811. Defining Named Functions
  812. ========================
  813.  
  814.    We usually give a name to a function when it is first created.  This
  815. is called "defining a function", and it is done with the `defun'
  816. special form.
  817.  
  818.  -- Special Form: defun NAME ARGUMENT-LIST BODY-FORMS
  819.      `defun' is the usual way to define new Lisp functions.  It defines
  820.      the symbol NAME as a function that looks like this:
  821.  
  822.           (lambda ARGUMENT-LIST . BODY-FORMS)
  823.  
  824.      This lambda expression is stored in the function cell of NAME. The
  825.      value returned by evaluating the `defun' form is NAME, but usually
  826.      we ignore this value.
  827.  
  828.      As described previously (*note Lambda Expressions::.),
  829.      ARGUMENT-LIST is a list of argument names and may include the
  830.      keywords `&optional' and `&rest'.  Also, the first two forms in
  831.      BODY-FORMS may be a documentation string and an interactive
  832.      declaration.
  833.  
  834.      Note that the same symbol NAME may also be used as a global
  835.      variable, since the value cell is independent of the function cell.
  836.  
  837.      Here are some examples:
  838.  
  839.           (defun foo () 5)
  840.                => foo
  841.           (foo)
  842.                => 5
  843.           
  844.           (defun bar (a &optional b &rest c)
  845.               (list a b c))
  846.                => bar
  847.           (bar 1 2 3 4 5)
  848.                => (1 2 (3 4 5))
  849.           (bar 1)
  850.                => (1 nil nil)
  851.           (bar)
  852.           error--> Wrong number of arguments.
  853.           
  854.           (defun capitalize-backwards ()
  855.             "This function makes the last letter of a word upper case."
  856.             (interactive)
  857.             (backward-word 1)
  858.             (forward-word 1)
  859.             (backward-char 1)
  860.             (capitalize-word 1))
  861.                => capitalize-backwards
  862.  
  863.      Be careful not to redefine existing functions unintentionally.
  864.      `defun' will redefine even primitive functions such as `car'
  865.      without any hesitation or notification.  Redefining a function
  866.      already defined is often done deliberately, and there is no way to
  867.      distinguish deliberate redefinition from unintentional
  868.      redefinition.
  869.  
  870. 
  871. File: elisp,  Node: Calling Functions,  Next: Mapping Functions,  Prev: Defining Functions,  Up: Functions
  872.  
  873. Calling Functions
  874. =================
  875.  
  876.    Defining functions is only half the battle.  Functions don't do
  877. anything until you "call" them, i.e., tell them to run.  This process
  878. is also known as "invocation".
  879.  
  880.    The most common way of invoking a function is by evaluating a list. 
  881. For example, evaluating the list `(concat "a" "b")' calls the function
  882. `concat'.  *Note Evaluation::, for a description of evaluation.
  883.  
  884.    When you write a list as an expression in your program, the function
  885. name is part of the program.  This means that the choice of which
  886. function to call is made when you write the program.  Usually that's
  887. just what you want.  Occasionally you need to decide at run time which
  888. function to call.  Then you can use the functions `funcall' and `apply'.
  889.  
  890.  -- Function: funcall FUNCTION &rest ARGUMENTS
  891.      `funcall' calls FUNCTION with ARGUMENTS, and returns whatever
  892.      FUNCTION returns.
  893.  
  894.      Since `funcall' is a function, all of its arguments, including
  895.      FUNCTION, are evaluated before `funcall' is called.  This means
  896.      that you can use any expression to obtain the function to be
  897.      called.  It also means that `funcall' does not see the expressions
  898.      you write for the ARGUMENTS, only their values.  These values are
  899.      *not* evaluated a second time in the act of calling FUNCTION;
  900.      `funcall' enters the normal procedure for calling a function at the
  901.      place where the arguments have already been evaluated.
  902.  
  903.      The argument FUNCTION must be either a Lisp function or a
  904.      primitive function.  Special forms and macros are not allowed,
  905.      because they make sense only when given the "unevaluated" argument
  906.      expressions.  `funcall' cannot provide these because, as we saw
  907.      above, it never knows them in the first place.
  908.  
  909.           (setq f 'list)
  910.                => list
  911.           (funcall f 'x 'y 'z)
  912.                => (x y z)
  913.           (funcall f 'x 'y '(z))
  914.                => (x y (z))
  915.           (funcall 'and t nil)
  916.           error--> Invalid function: #<subr and>
  917.  
  918.      Compare this example with that of `apply'.
  919.  
  920.  -- Function: apply FUNCTION &rest ARGUMENTS
  921.      `apply' calls FUNCTION with ARGUMENTS, just like `funcall' but
  922.      with one difference: the last of ARGUMENTS is a list of arguments
  923.      to give to FUNCTION, rather than a single argument.  We also say
  924.      that this list is "appended" to the other arguments.
  925.  
  926.      `apply' returns the result of calling FUNCTION.  As with
  927.      `funcall', FUNCTION must either be a Lisp function or a primitive
  928.      function; special forms and macros do not make sense in `apply'.
  929.  
  930.           (setq f 'list)
  931.                => list
  932.           (apply f 'x 'y 'z)
  933.           error--> Wrong type argument: listp, z
  934.           (apply '+ 1 2 '(3 4))
  935.                => 10
  936.           (apply '+ '(1 2 3 4))
  937.                => 10
  938.           
  939.           (apply 'append '((a b c) nil (x y z) nil))
  940.                => (a b c x y z)
  941.  
  942.      An interesting example of using `apply' is found in the description
  943.      of `mapcar'; see the following section.
  944.  
  945.    It is common for Lisp functions to accept functions as arguments or
  946. find them in data structures (especially in hook variables and property
  947. lists) and call them using `funcall' or `apply'.  Functions that accept
  948. function arguments are often called "functionals".
  949.  
  950.    Sometimes, when you call such a function, it is useful to supply a
  951. no-op function as the argument.  Here are two different kinds of no-op
  952. function:
  953.  
  954.  -- Function: identity ARG
  955.      This function returns ARG and has no side effects.
  956.  
  957.  -- Function: ignore &rest ARGS
  958.      This function ignores any arguments and returns `nil'.
  959.  
  960. 
  961. File: elisp,  Node: Mapping Functions,  Next: Anonymous Functions,  Prev: Calling Functions,  Up: Functions
  962.  
  963. Mapping Functions
  964. =================
  965.  
  966.    A "mapping function" applies a given function to each element of a
  967. list or other collection.  Emacs Lisp has three such functions;
  968. `mapcar' and `mapconcat', which scan a list, are described here.  For
  969. the third mapping function, `mapatoms', see *Note Creating Symbols::.
  970.  
  971.  -- Function: mapcar FUNCTION SEQUENCE
  972.      `mapcar' applies FUNCTION to each element of SEQUENCE in turn. 
  973.      The results are made into a `nil'-terminated list.
  974.  
  975.      The argument SEQUENCE may be a list, a vector or a string.  The
  976.      result is always a list.  The length of the result is the same as
  977.      the length of SEQUENCE.
  978.  
  979.      For example:
  980.  
  981.           (mapcar 'car '((a b) (c d) (e f)))
  982.                => (a c e)
  983.           (mapcar '1+ [1 2 3])
  984.                => (2 3 4)
  985.           (mapcar 'char-to-string "abc")
  986.                => ("a" "b" "c")
  987.           
  988.           ;; Call each function in `my-hooks'.
  989.           (mapcar 'funcall my-hooks)
  990.           
  991.           (defun mapcar* (f &rest args)
  992.             "Apply FUNCTION to successive cars of all ARGS, until one ends.
  993.           Return the list of results."
  994.             (if (not (memq 'nil args))              ; If no list is exhausted,
  995.                 (cons (apply f (mapcar 'car args))  ; Apply function to CARs.
  996.                       (apply 'mapcar* f             ; Recurse for rest of elements.
  997.                              (mapcar 'cdr args)))))
  998.           
  999.           (mapcar* 'cons '(a b c) '(1 2 3 4))
  1000.                => ((a . 1) (b . 2) (c . 3))
  1001.  
  1002.  -- Function: mapconcat FUNCTION SEQUENCE SEPARATOR
  1003.      `mapconcat' applies FUNCTION to each element of SEQUENCE: the
  1004.      results, which must be strings, are concatenated. Between each
  1005.      pair of result strings, `mapconcat' inserts the string SEPARATOR. 
  1006.      Usually SEPARATOR contains a space or comma or other suitable
  1007.      punctuation.
  1008.  
  1009.      The argument FUNCTION must be a function that can take one
  1010.      argument and returns a string.
  1011.  
  1012.           (mapconcat 'symbol-name
  1013.                      '(The cat in the hat)
  1014.                      " ")
  1015.                => "The cat in the hat"
  1016.           
  1017.           (mapconcat (function (lambda (x) (format "%c" (1+ x))))
  1018.                      "HAL-8000"
  1019.                      "")
  1020.                => "IBM.9111"
  1021.  
  1022. 
  1023. File: elisp,  Node: Anonymous Functions,  Next: Function Cells,  Prev: Mapping Functions,  Up: Functions
  1024.  
  1025. Anonymous Functions
  1026. ===================
  1027.  
  1028.    In Lisp, a function is a list that starts with `lambda' (or
  1029. alternatively a primitive subr-object); names are "extra".  Although
  1030. usually functions are defined with `defun' and given names at the same
  1031. time, it is occasionally more concise to use an explicit lambda
  1032. expression--an anonymous function.  Such a list is valid wherever a
  1033. function name is.
  1034.  
  1035.    Any method of creating such a list makes a valid function.  Even
  1036. this:
  1037.  
  1038.      (setq silly (append '(lambda (x)) (list (list '+ (* 3 4) 'x))))
  1039.           => (lambda (x) (+ 12 x))
  1040.  
  1041. This computes a list that looks like `(lambda (x) (+ 12 x))' and makes
  1042. it the value (*not* the function definition!) of `silly'.
  1043.  
  1044.    Here is how we might call this function:
  1045.  
  1046.      (funcall silly 1)
  1047.           => 13
  1048.  
  1049. (It does *not* work to write `(silly 1)', because this function is not
  1050. the *function definition* of `silly'.  We have not given `silly' any
  1051. function definition, just a value as a variable.)
  1052.  
  1053.    Most of the time, anonymous functions are constants that appear in
  1054. your program.  For example, you might want to pass one as an argument
  1055. to the function `mapcar', which applies any given function to each
  1056. element of a list.  Here we pass an anonymous function that multiplies
  1057. a number by two:
  1058.  
  1059.      (defun double-each (list)
  1060.        (mapcar '(lambda (x) (* 2 x)) list))
  1061.           => double-each
  1062.      (double-each '(2 11))
  1063.           => (4 22)
  1064.  
  1065. In such cases, we usually use the special form `function' instead of
  1066. simple quotation to quote the anonymous function.
  1067.  
  1068.  -- Special Form: function FUNCTION-OBJECT
  1069.      This special form returns FUNCTION-OBJECT without evaluating it.
  1070.      In this, it is equivalent to `quote'.  However, it serves as a
  1071.      note to the Emacs Lisp compiler that FUNCTION-OBJECT is intended
  1072.      to be used only as a function, and therefore can safely be
  1073.      compiled. *Note Quoting::, for comparison.
  1074.  
  1075.    Using `function' instead of `quote' makes a difference inside a
  1076. function or macro that you are going to compile.  For example:
  1077.  
  1078.      (defun double-each (list)
  1079.        (mapcar (function (lambda (x) (* 2 x))) list))
  1080.           => double-each
  1081.      (double-each '(2 11))
  1082.           => (4 22)
  1083.  
  1084. If this definition of `double-each' is compiled, the anonymous function
  1085. is compiled as well.  By contrast, in the previous definition where
  1086. ordinary `quote' is used, the argument passed to `mapcar' is the
  1087. precise list shown:
  1088.  
  1089.      (lambda (arg) (+ arg 5))
  1090.  
  1091. The Lisp compiler cannot assume this list is a function, even though it
  1092. looks like one, since it does not know what `mapcar' does with the
  1093. list.  Perhaps `mapcar' will check that the CAR of the third element is
  1094. the symbol `+'!  The advantage of `function' is that it tells the
  1095. compiler to go ahead and compile the constant function.
  1096.  
  1097.    We sometimes write `function' instead of `quote' when quoting the
  1098. name of a function, but this usage is just a sort of comment.
  1099.  
  1100.      (function SYMBOL) == (quote SYMBOL) == 'SYMBOL
  1101.  
  1102.    See `documentation' in *Note Accessing Documentation::, for a
  1103. realistic example using `function' and an anonymous function.
  1104.  
  1105. 
  1106. File: elisp,  Node: Function Cells,  Next: Related Topics,  Prev: Anonymous Functions,  Up: Functions
  1107.  
  1108. Accessing Function Cell Contents
  1109. ================================
  1110.  
  1111.    The "function definition" of a symbol is the object stored in the
  1112. function cell of the symbol.  The functions described here access, test,
  1113. and set the function cell of symbols.
  1114.  
  1115.  -- Function: symbol-function SYMBOL
  1116.      This returns the object in the function cell of SYMBOL.  If the
  1117.      symbol's function cell is void, a `void-function' error is
  1118.      signaled.
  1119.  
  1120.      This function does not check that the returned object is a
  1121.      legitimate function.
  1122.  
  1123.           (defun bar (n) (+ n 2))
  1124.                => bar
  1125.           (symbol-function 'bar)
  1126.                => (lambda (n) (+ n 2))
  1127.           (fset 'baz 'bar)
  1128.                => bar
  1129.           (symbol-function 'baz)
  1130.                => bar
  1131.  
  1132.    If you have never given a symbol any function definition, we say that
  1133. that symbol's function cell is "void".  In other words, the function
  1134. cell does not have any Lisp object in it.  If you try to call such a
  1135. symbol as a function, it signals a `void-function' error.
  1136.  
  1137.    Note that void is not the same as `nil' or the symbol `void'.  The
  1138. symbols `nil' and `void' are Lisp objects, and can be stored into a
  1139. function cell just as any other object can be (and they can be valid
  1140. functions if you define them in turn with `defun'); but `nil' or `void'
  1141. is *an object*.  A void function cell contains no object whatsoever.
  1142.  
  1143.    You can test the voidness of a symbol's function definition with
  1144. `fboundp'.  After you have given a symbol a function definition, you
  1145. can make it void once more using `fmakunbound'.
  1146.  
  1147.  -- Function: fboundp SYMBOL
  1148.      Returns `t' if the symbol has an object in its function cell,
  1149.      `nil' otherwise.  It does not check that the object is a legitimate
  1150.      function.
  1151.  
  1152.  -- Function: fmakunbound SYMBOL
  1153.      This function makes SYMBOL's function cell void, so that a
  1154.      subsequent attempt to access this cell will cause a `void-function'
  1155.      error.  (See also `makunbound', in *Note Local Variables::.)
  1156.  
  1157.           (defun foo (x) x)
  1158.                => x
  1159.           (fmakunbound 'foo)
  1160.                => x
  1161.           (foo 1)
  1162.           error--> Symbol's function definition is void: foo
  1163.  
  1164.  -- Function: fset SYMBOL OBJECT
  1165.      This function stores OBJECT in the function cell of SYMBOL. The
  1166.      result is OBJECT.  Normally OBJECT should be a function or the
  1167.      name of a function, but this is not checked.
  1168.  
  1169.      There are three normal uses of this function:
  1170.  
  1171.         * Copying one symbol's function definition to another.  (In
  1172.           other words, making an alternate name for a function.)
  1173.  
  1174.         * Giving a symbol a function definition that is not a list and
  1175.           therefore cannot be made with `defun'.  *Note Classifying
  1176.           Lists::, for an example of this usage.
  1177.  
  1178.         * In constructs for defining or altering functions.  If `defun'
  1179.           were not a primitive, it could be written in Lisp (as a
  1180.           macro) using `fset'.
  1181.  
  1182.      Here are examples of the first two uses:
  1183.  
  1184.           ;; Give `first' the same definition `car' has.
  1185.           (fset 'first (symbol-function 'car))
  1186.                => #<subr car>
  1187.           (first '(1 2 3))
  1188.                => 1
  1189.           
  1190.           ;; Make the symbol `car' the function definition of `xfirst'.
  1191.           (fset 'xfirst 'car)
  1192.                => car
  1193.           (xfirst '(1 2 3))
  1194.                => 1
  1195.           (symbol-function 'xfirst)
  1196.                => car
  1197.           (symbol-function (symbol-function 'xfirst))
  1198.                => #<subr car>
  1199.           
  1200.           ;; Define a named keyboard macro.
  1201.           (fset 'kill-two-lines "\^u2\^k")
  1202.                => "\^u2\^k"
  1203.  
  1204.    When writing a function that extends a previously defined function,
  1205. the following idiom is often used:
  1206.  
  1207.      (fset 'old-foo (symbol-function 'foo))
  1208.      
  1209.      (defun foo ()
  1210.        "Just like old-foo, except more so."
  1211.        (old-foo)
  1212.        (more-so))
  1213.  
  1214. This does not work properly if `foo' has been defined to autoload. In
  1215. such a case, when `foo' calls `old-foo', Lisp will attempt to define
  1216. `old-foo' by loading a file.  Since this presumably defines `foo'
  1217. rather than `old-foo', it will not produce the proper results.  The
  1218. only way to avoid this problem is to make sure the file is loaded
  1219. before moving aside the old definition of `foo'.
  1220.  
  1221.