home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / GNU / emacs.inst / emacs19.idb / usr / gnu / info / elisp-8.z / elisp-8
Encoding:
GNU Info File  |  1994-08-02  |  50.5 KB  |  1,221 lines

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