home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / elisp / elisp-8 < prev    next >
Encoding:
GNU Info File  |  1993-05-31  |  49.4 KB  |  1,198 lines

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