home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / elisp / elisp-9 < prev    next >
Encoding:
GNU Info File  |  1993-05-31  |  49.3 KB  |  1,263 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: Calling Functions,  Next: Mapping Functions,  Prev: Defining Functions,  Up: Functions
  28.  
  29. Calling Functions
  30. =================
  31.  
  32.    Defining functions is only half the battle.  Functions don't do
  33. anything until you "call" them, i.e., tell them to run.  This process
  34. is also known as "invocation".
  35.  
  36.    The most common way of invoking a function is by evaluating a list.
  37. For example, evaluating the list `(concat "a" "b")' calls the function
  38. `concat'.  *Note Evaluation::, for a description of evaluation.
  39.  
  40.    When you write a list as an expression in your program, the function
  41. name is part of the program.  This means that the choice of which
  42. function to call is made when you write the program.  Usually that's
  43. just what you want.  Occasionally you need to decide at run time which
  44. function to call.  Then you can use the functions `funcall' and `apply'.
  45.  
  46.  - Function: funcall FUNCTION &rest ARGUMENTS
  47.      `funcall' calls FUNCTION with ARGUMENTS, and returns whatever
  48.      FUNCTION returns.
  49.  
  50.      Since `funcall' is a function, all of its arguments, including
  51.      FUNCTION, are evaluated before `funcall' is called.  This means
  52.      that you can use any expression to obtain the function to be
  53.      called.  It also means that `funcall' does not see the expressions
  54.      you write for the ARGUMENTS, only their values.  These values are
  55.      *not* evaluated a second time in the act of calling FUNCTION;
  56.      `funcall' enters the normal procedure for calling a function at the
  57.      place where the arguments have already been evaluated.
  58.  
  59.      The argument FUNCTION must be either a Lisp function or a
  60.      primitive function.  Special forms and macros are not allowed,
  61.      because they make sense only when given the "unevaluated" argument
  62.      expressions.  `funcall' cannot provide these because, as we saw
  63.      above, it never knows them in the first place.
  64.  
  65.           (setq f 'list)
  66.                => list
  67.           (funcall f 'x 'y 'z)
  68.                => (x y z)
  69.           (funcall f 'x 'y '(z))
  70.                => (x y (z))
  71.           (funcall 'and t nil)
  72.           error--> Invalid function: #<subr and>
  73.  
  74.      Compare this example with that of `apply'.
  75.  
  76.  - Function: apply FUNCTION &rest ARGUMENTS
  77.      `apply' calls FUNCTION with ARGUMENTS, just like `funcall' but
  78.      with one difference: the last of ARGUMENTS is a list of arguments
  79.      to give to FUNCTION, rather than a single argument.  We also say
  80.      that this list is "appended" to the other arguments.
  81.  
  82.      `apply' returns the result of calling FUNCTION.  As with
  83.      `funcall', FUNCTION must either be a Lisp function or a primitive
  84.      function; special forms and macros do not make sense in `apply'.
  85.  
  86.           (setq f 'list)
  87.                => list
  88.           (apply f 'x 'y 'z)
  89.           error--> Wrong type argument: listp, z
  90.           (apply '+ 1 2 '(3 4))
  91.                => 10
  92.           (apply '+ '(1 2 3 4))
  93.                => 10
  94.           
  95.           (apply 'append '((a b c) nil (x y z) nil))
  96.                => (a b c x y z)
  97.  
  98.      An interesting example of using `apply' is found in the description
  99.      of `mapcar'; see the following section.
  100.  
  101.    It is common for Lisp functions to accept functions as arguments or
  102. find them in data structures (especially in hook variables and property
  103. lists) and call them using `funcall' or `apply'.  Functions that accept
  104. function arguments are often called "functionals".
  105.  
  106.    Sometimes, when you call such a function, it is useful to supply a
  107. no-op function as the argument.  Here are two different kinds of no-op
  108. function:
  109.  
  110.  - Function: identity ARG
  111.      This function returns ARG and has no side effects.
  112.  
  113.  - Function: ignore &rest ARGS
  114.      This function ignores any arguments and returns `nil'.
  115.  
  116. 
  117. File: elisp,  Node: Mapping Functions,  Next: Anonymous Functions,  Prev: Calling Functions,  Up: Functions
  118.  
  119. Mapping Functions
  120. =================
  121.  
  122.    A "mapping function" applies a given function to each element of a
  123. list or other collection.  Emacs Lisp has three such functions;
  124. `mapcar' and `mapconcat', which scan a list, are described here.  For
  125. the third mapping function, `mapatoms', see *Note Creating Symbols::.
  126.  
  127.  - Function: mapcar FUNCTION SEQUENCE
  128.      `mapcar' applies FUNCTION to each element of SEQUENCE in turn.
  129.      The results are made into a `nil'-terminated list.
  130.  
  131.      The argument SEQUENCE may be a list, a vector or a string.  The
  132.      result is always a list.  The length of the result is the same as
  133.      the length of SEQUENCE.
  134.  
  135.      For example:
  136.  
  137.           (mapcar 'car '((a b) (c d) (e f)))
  138.                => (a c e)
  139.           (mapcar '1+ [1 2 3])
  140.                => (2 3 4)
  141.           (mapcar 'char-to-string "abc")
  142.                => ("a" "b" "c")
  143.  
  144.           ;; Call each function in `my-hooks'.
  145.           (mapcar 'funcall my-hooks)
  146.  
  147.           (defun mapcar* (f &rest args)
  148.             "Apply FUNCTION to successive cars of all ARGS, until one ends.
  149.           Return the list of results."
  150.             ;; If no list is exhausted,
  151.             (if (not (memq 'nil args))
  152.                 ;; Apply function to CARs.
  153.                 (cons (apply f (mapcar 'car args))
  154.                       (apply 'mapcar* f
  155.                              ;; Recurse for rest of elements.
  156.                              (mapcar 'cdr args)))))
  157.  
  158.           (mapcar* 'cons '(a b c) '(1 2 3 4))
  159.                => ((a . 1) (b . 2) (c . 3))
  160.  
  161.  - Function: mapconcat FUNCTION SEQUENCE SEPARATOR
  162.      `mapconcat' applies FUNCTION to each element of SEQUENCE: the
  163.      results, which must be strings, are concatenated.  Between each
  164.      pair of result strings, `mapconcat' inserts the string SEPARATOR.
  165.      Usually SEPARATOR contains a space or comma or other suitable
  166.      punctuation.
  167.  
  168.      The argument FUNCTION must be a function that can take one
  169.      argument and returns a string.
  170.  
  171.           (mapconcat 'symbol-name
  172.                      '(The cat in the hat)
  173.                      " ")
  174.                => "The cat in the hat"
  175.  
  176.           (mapconcat (function (lambda (x) (format "%c" (1+ x))))
  177.                      "HAL-8000"
  178.                      "")
  179.                => "IBM.9111"
  180.  
  181. 
  182. File: elisp,  Node: Anonymous Functions,  Next: Function Cells,  Prev: Mapping Functions,  Up: Functions
  183.  
  184. Anonymous Functions
  185. ===================
  186.  
  187.    In Lisp, a function is a list that starts with `lambda' (or
  188. alternatively a primitive subr-object); names are "extra".  Although
  189. usually functions are defined with `defun' and given names at the same
  190. time, it is occasionally more concise to use an explicit lambda
  191. expression--an anonymous function.  Such a list is valid wherever a
  192. function name is.
  193.  
  194.    Any method of creating such a list makes a valid function.  Even
  195. this:
  196.  
  197.      (setq silly (append '(lambda (x)) (list (list '+ (* 3 4) 'x))))
  198.           => (lambda (x) (+ 12 x))
  199.  
  200. This computes a list that looks like `(lambda (x) (+ 12 x))' and makes
  201. it the value (*not* the function definition!) of `silly'.
  202.  
  203.    Here is how we might call this function:
  204.  
  205.      (funcall silly 1)
  206.           => 13
  207.  
  208. (It does *not* work to write `(silly 1)', because this function is not
  209. the *function definition* of `silly'.  We have not given `silly' any
  210. function definition, just a value as a variable.)
  211.  
  212.    Most of the time, anonymous functions are constants that appear in
  213. your program.  For example, you might want to pass one as an argument
  214. to the function `mapcar', which applies any given function to each
  215. element of a list.  Here we pass an anonymous function that multiplies
  216. a number by two:
  217.  
  218.      (defun double-each (list)
  219.        (mapcar '(lambda (x) (* 2 x)) list))
  220.           => double-each
  221.      (double-each '(2 11))
  222.           => (4 22)
  223.  
  224. In such cases, we usually use the special form `function' instead of
  225. simple quotation to quote the anonymous function.
  226.  
  227.  - Special Form: function FUNCTION-OBJECT
  228.      This special form returns FUNCTION-OBJECT without evaluating it.
  229.      In this, it is equivalent to `quote'.  However, it serves as a
  230.      note to the Emacs Lisp compiler that FUNCTION-OBJECT is intended
  231.      to be used only as a function, and therefore can safely be
  232.      compiled.  *Note Quoting::, for comparison.
  233.  
  234.    Using `function' instead of `quote' makes a difference inside a
  235. function or macro that you are going to compile.  For example:
  236.  
  237.      (defun double-each (list)
  238.        (mapcar (function (lambda (x) (* 2 x))) list))
  239.           => double-each
  240.      (double-each '(2 11))
  241.           => (4 22)
  242.  
  243. If this definition of `double-each' is compiled, the anonymous function
  244. is compiled as well.  By contrast, in the previous definition where
  245. ordinary `quote' is used, the argument passed to `mapcar' is the
  246. precise list shown:
  247.  
  248.      (lambda (arg) (+ arg 5))
  249.  
  250. The Lisp compiler cannot assume this list is a function, even though it
  251. looks like one, since it does not know what `mapcar' does with the
  252. list.  Perhaps `mapcar' will check that the CAR of the third element is
  253. the symbol `+'!  The advantage of `function' is that it tells the
  254. compiler to go ahead and compile the constant function.
  255.  
  256.    We sometimes write `function' instead of `quote' when quoting the
  257. name of a function, but this usage is just a sort of comment.
  258.  
  259.      (function SYMBOL) == (quote SYMBOL) == 'SYMBOL
  260.  
  261.    See `documentation' in *Note Accessing Documentation::, for a
  262. realistic example using `function' and an anonymous function.
  263.  
  264. 
  265. File: elisp,  Node: Function Cells,  Next: Inline Functions,  Prev: Anonymous Functions,  Up: Functions
  266.  
  267. Accessing Function Cell Contents
  268. ================================
  269.  
  270.    The "function definition" of a symbol is the object stored in the
  271. function cell of the symbol.  The functions described here access, test,
  272. and set the function cell of symbols.
  273.  
  274.  - Function: symbol-function SYMBOL
  275.      This returns the object in the function cell of SYMBOL.  If the
  276.      symbol's function cell is void, a `void-function' error is
  277.      signaled.
  278.  
  279.      This function does not check that the returned object is a
  280.      legitimate function.
  281.  
  282.           (defun bar (n) (+ n 2))
  283.                => bar
  284.           (symbol-function 'bar)
  285.                => (lambda (n) (+ n 2))
  286.           (fset 'baz 'bar)
  287.                => bar
  288.           (symbol-function 'baz)
  289.                => bar
  290.  
  291.    If you have never given a symbol any function definition, we say that
  292. that symbol's function cell is "void".  In other words, the function
  293. cell does not have any Lisp object in it.  If you try to call such a
  294. symbol as a function, it signals a `void-function' error.
  295.  
  296.    Note that void is not the same as `nil' or the symbol `void'.  The
  297. symbols `nil' and `void' are Lisp objects, and can be stored into a
  298. function cell just as any other object can be (and they can be valid
  299. functions if you define them in turn with `defun'); but `nil' or `void'
  300. is *an object*.  A void function cell contains no object whatsoever.
  301.  
  302.    You can test the voidness of a symbol's function definition with
  303. `fboundp'.  After you have given a symbol a function definition, you
  304. can make it void once more using `fmakunbound'.
  305.  
  306.  - Function: fboundp SYMBOL
  307.      Returns `t' if the symbol has an object in its function cell,
  308.      `nil' otherwise.  It does not check that the object is a legitimate
  309.      function.
  310.  
  311.  - Function: fmakunbound SYMBOL
  312.      This function makes SYMBOL's function cell void, so that a
  313.      subsequent attempt to access this cell will cause a `void-function'
  314.      error.  (See also `makunbound', in *Note Local Variables::.)
  315.  
  316.           (defun foo (x) x)
  317.                => x
  318.           (fmakunbound 'foo)
  319.                => x
  320.           (foo 1)
  321.           error--> Symbol's function definition is void: foo
  322.  
  323.  - Function: fset SYMBOL OBJECT
  324.      This function stores OBJECT in the function cell of SYMBOL.  The
  325.      result is OBJECT.  Normally OBJECT should be a function or the
  326.      name of a function, but this is not checked.
  327.  
  328.      There are three normal uses of this function:
  329.  
  330.         * Copying one symbol's function definition to another.  (In
  331.           other words, making an alternate name for a function.)
  332.  
  333.         * Giving a symbol a function definition that is not a list and
  334.           therefore cannot be made with `defun'.  *Note Classifying
  335.           Lists::, for an example of this usage.
  336.  
  337.         * In constructs for defining or altering functions.  If `defun'
  338.           were not a primitive, it could be written in Lisp (as a
  339.           macro) using `fset'.
  340.  
  341.      Here are examples of the first two uses:
  342.  
  343.           ;; Give `first' the same definition `car' has.
  344.           (fset 'first (symbol-function 'car))
  345.                => #<subr car>
  346.           (first '(1 2 3))
  347.                => 1
  348.           
  349.           ;; Make the symbol `car' the function definition of `xfirst'.
  350.           (fset 'xfirst 'car)
  351.                => car
  352.           (xfirst '(1 2 3))
  353.                => 1
  354.           (symbol-function 'xfirst)
  355.                => car
  356.           (symbol-function (symbol-function 'xfirst))
  357.                => #<subr car>
  358.           
  359.           ;; Define a named keyboard macro.
  360.           (fset 'kill-two-lines "\^u2\^k")
  361.                => "\^u2\^k"
  362.  
  363.    When writing a function that extends a previously defined function,
  364. the following idiom is often used:
  365.  
  366.      (fset 'old-foo (symbol-function 'foo))
  367.      
  368.      (defun foo ()
  369.        "Just like old-foo, except more so."
  370.        (old-foo)
  371.        (more-so))
  372.  
  373. This does not work properly if `foo' has been defined to autoload.  In
  374. such a case, when `foo' calls `old-foo', Lisp attempts to define
  375. `old-foo' by loading a file.  Since this presumably defines `foo'
  376. rather than `old-foo', it does not produce the proper results.  The
  377. only way to avoid this problem is to make sure the file is loaded
  378. before moving aside the old definition of `foo'.
  379.  
  380.    See also the function `indirect-function' in *Note Function
  381. Indirection::.
  382.  
  383. 
  384. File: elisp,  Node: Inline Functions,  Next: Related Topics,  Prev: Function Cells,  Up: Functions
  385.  
  386. Inline Functions
  387. ================
  388.  
  389.    You can define an "inline function" by using `defsubst' instead of
  390. `defun'.  An inline function works just like an ordinary function
  391. except for one thing: when you compile a call to the function, the
  392. function's definition is open-coded into the caller.
  393.  
  394.    Making a function inline makes explicit calls run faster.  But it
  395. also has disadvantages.  For one thing, it reduces flexibility; if you
  396. change the definition of the function, calls already inlined still use
  397. the old definition until you recompile them.
  398.  
  399.    Another disadvantage is that making a large function inline can
  400. increase the size of compiled code both in files and in memory.  Since
  401. the advantages of inline functions are greatest for small functions, you
  402. generally should not make large functions inline.
  403.  
  404.    It's possible to define a macro to expand into the same code that an
  405. inline function would execute.  But the macro would have a limitation:
  406. you can use it only explicitly--a macro cannot be called with `apply',
  407. `mapcar' and so on.  Also, it takes some work to convert an ordinary
  408. function into a macro.  (*Note Macros::.)  To convert it into an inline
  409. function is very easy; simply replace `defun' with `defsubst'.
  410.  
  411.    Inline functions can be used and open coded later on in the same
  412. file, following the definition, just like macros.
  413.  
  414.    Emacs versions prior to 19 did not have inline functions.
  415.  
  416. 
  417. File: elisp,  Node: Related Topics,  Prev: Inline Functions,  Up: Functions
  418.  
  419. Other Topics Related to Functions
  420. =================================
  421.  
  422.    Here is a table of several functions that do things related to
  423. function calling and function definitions.  They are documented
  424. elsewhere, but we provide cross references here.
  425.  
  426. `apply'
  427.      See *Note Calling Functions::.
  428.  
  429. `autoload'
  430.      See *Note Autoload::.
  431.  
  432. `call-interactively'
  433.      See *Note Interactive Call::.
  434.  
  435. `commandp'
  436.      See *Note Interactive Call::.
  437.  
  438. `documentation'
  439.      See *Note Accessing Documentation::.
  440.  
  441. `eval'
  442.      See *Note Eval::.
  443.  
  444. `funcall'
  445.      See *Note Calling Functions::.
  446.  
  447. `ignore'
  448.      See *Note Calling Functions::.
  449.  
  450. `indirect-function'
  451.      See *Note Function Indirection::.
  452.  
  453. `interactive'
  454.      See *Note Using Interactive::.
  455.  
  456. `interactive-p'
  457.      See *Note Interactive Call::.
  458.  
  459. `mapatoms'
  460.      See *Note Creating Symbols::.
  461.  
  462. `mapcar'
  463.      See *Note Mapping Functions::.
  464.  
  465. `mapconcat'
  466.      See *Note Mapping Functions::.
  467.  
  468. `undefined'
  469.      See *Note Key Lookup::.
  470.  
  471. 
  472. File: elisp,  Node: Macros,  Next: Loading,  Prev: Functions,  Up: Top
  473.  
  474. Macros
  475. ******
  476.  
  477.    "Macros" enable you to define new control constructs and other
  478. language features.  A macro is defined much like a function, but instead
  479. of telling how to compute a value, it tells how to compute another Lisp
  480. expression which will in turn compute the value.  We call this
  481. expression the "expansion" of the macro.
  482.  
  483.    Macros can do this because they operate on the unevaluated
  484. expressions for the arguments, not on the argument values as functions
  485. do.  They can therefore construct an expansion containing these
  486. argument expressions or parts of them.
  487.  
  488.    If you are using a macro to do something an ordinary function could
  489. do, just for the sake of speed, consider using an inline function
  490. instead.  *Note Inline Functions::.
  491.  
  492. * Menu:
  493.  
  494. * Simple Macro::            A basic example.
  495. * Expansion::               How, when and why macros are expanded.
  496. * Compiling Macros::        How macros are expanded by the compiler.
  497. * Defining Macros::         How to write a macro definition.
  498. * Backquote::               Easier construction of list structure.
  499. * Problems with Macros::    Don't evaluate the macro arguments too many times.
  500.                               Don't hide the user's variables.
  501.  
  502. 
  503. File: elisp,  Node: Simple Macro,  Next: Expansion,  Prev: Macros,  Up: Macros
  504.  
  505. A Simple Example of a Macro
  506. ===========================
  507.  
  508.    Suppose we would like to define a Lisp construct to increment a
  509. variable value, much like the `++' operator in C.  We would like to
  510. write `(inc x)' and have the effect of `(setq x (1+ x))'.  Here's a
  511. macro definition that does the job:
  512.  
  513.      (defmacro inc (var)
  514.         (list 'setq var (list '1+ var)))
  515.  
  516.    When this is called with `(inc x)', the argument `var' has the value
  517. `x'--*not* the *value* of `x'.  The body of the macro uses this to
  518. construct the expansion, which is `(setq x (1+ x))'.  Once the macro
  519. definition returns this expansion, Lisp proceeds to evaluate it, thus
  520. incrementing `x'.
  521.  
  522. 
  523. File: elisp,  Node: Expansion,  Next: Compiling Macros,  Prev: Simple Macro,  Up: Macros
  524.  
  525. Expansion of a Macro Call
  526. =========================
  527.  
  528.    A macro call looks just like a function call in that it is a list
  529. which starts with the name of the macro.  The rest of the elements of
  530. the list are the arguments of the macro.
  531.  
  532.    Evaluation of the macro call begins like evaluation of a function
  533. call except for one crucial difference: the macro arguments are the
  534. actual expressions appearing in the macro call.  They are not evaluated
  535. before they are given to the macro definition.  By contrast, the
  536. arguments of a function are results of evaluating the elements of the
  537. function call list.
  538.  
  539.    Having obtained the arguments, Lisp invokes the macro definition just
  540. as a function is invoked.  The argument variables of the macro are bound
  541. to the argument values from the macro call, or to a list of them in the
  542. case of a `&rest' argument.  And the macro body executes and returns
  543. its value just as a function body does.
  544.  
  545.    The second crucial difference between macros and functions is that
  546. the value returned by the macro body is not the value of the macro call.
  547. Instead, it is an alternate expression for computing that value, also
  548. known as the "expansion" of the macro.  The Lisp interpreter proceeds
  549. to evaluate the expansion as soon as it comes back from the macro.
  550.  
  551.    Since the expansion is evaluated in the normal manner, it may contain
  552. calls to other macros.  It may even be a call to the same macro, though
  553. this is unusual.
  554.  
  555.    You can see the expansion of a given macro call by calling
  556. `macroexpand'.
  557.  
  558.  - Function: macroexpand FORM &optional ENVIRONMENT
  559.      This function expands FORM, if it is a macro call.  If the result
  560.      is another macro call, it is expanded in turn, until something
  561.      which is not a macro call results.  That is the value returned by
  562.      `macroexpand'.  If FORM is not a macro call to begin with, it is
  563.      returned as given.
  564.  
  565.      Note that `macroexpand' does not look at the subexpressions of
  566.      FORM (although some macro definitions may do so).  Even if they
  567.      are macro calls themselves, `macroexpand' does not expand them.
  568.  
  569.      The function `macroexpand' does not expand calls to inline
  570.      functions.  Normally there is no need for that, since a call to an
  571.      inline function is no harder to understand than a call to an
  572.      ordinary function.
  573.  
  574.      If ENVIRONMENT is provided, it specifies an alist of macro
  575.      definitions that shadow the currently defined macros.  This is used
  576.      by byte compilation.
  577.  
  578.           (defmacro inc (var)
  579.               (list 'setq var (list '1+ var)))
  580.                => inc
  581.  
  582.           (macroexpand '(inc r))
  583.                => (setq r (1+ r))
  584.  
  585.           (defmacro inc2 (var1 var2)
  586.               (list 'progn (list 'inc var1) (list 'inc var2)))
  587.                => inc2
  588.  
  589.           (macroexpand '(inc2 r s))
  590.                => (progn (inc r) (inc s))  ; `inc' not expanded here.
  591.  
  592. 
  593. File: elisp,  Node: Compiling Macros,  Next: Defining Macros,  Prev: Expansion,  Up: Macros
  594.  
  595. Macros and Byte Compilation
  596. ===========================
  597.  
  598.    You might ask why we take the trouble to compute an expansion for a
  599. macro and then evaluate the expansion.  Why not have the macro body
  600. produce the desired results directly?  The reason has to do with
  601. compilation.
  602.  
  603.    When a macro call appears in a Lisp program being compiled, the Lisp
  604. compiler calls the macro definition just as the interpreter would, and
  605. receives an expansion.  But instead of evaluating this expansion, it
  606. compiles the expansion as if it had appeared directly in the program.
  607. As a result, the compiled code produces the value and side effects
  608. intended for the macro, but executes at full compiled speed.  This would
  609. not work if the macro body computed the value and side effects
  610. itself--they would be computed at compile time, which is not useful.
  611.  
  612.    In order for compilation of macro calls to work, the macros must be
  613. defined in Lisp when the calls to them are compiled.  The compiler has a
  614. special feature to help you do this: if a file being compiled contains a
  615. `defmacro' form, the macro is defined temporarily for the rest of the
  616. compilation of that file.  To use this feature, you must define the
  617. macro in the same file where it is used and before its first use.
  618.  
  619.    While byte-compiling a file, any `require' calls at top-level are
  620. executed.  One way to ensure that necessary macro definitions are
  621. available during compilation is to require the file that defines them.
  622. *Note Features::.
  623.  
  624. 
  625. File: elisp,  Node: Defining Macros,  Next: Backquote,  Prev: Compiling Macros,  Up: Macros
  626.  
  627. Defining Macros
  628. ===============
  629.  
  630.    A Lisp macro is a list whose CAR is `macro'.  Its CDR should be a
  631. function; expansion of the macro works by applying the function (with
  632. `apply') to the list of unevaluated argument-expressions from the macro
  633. call.
  634.  
  635.    It is possible to use an anonymous Lisp macro just like an anonymous
  636. function, but this is never done, because it does not make sense to pass
  637. an anonymous macro to mapping functions such as `mapcar'.  In practice,
  638. all Lisp macros have names, and they are usually defined with the
  639. special form `defmacro'.
  640.  
  641.  - Special Form: defmacro NAME ARGUMENT-LIST BODY-FORMS...
  642.      `defmacro' defines the symbol NAME as a macro that looks like this:
  643.  
  644.           (macro lambda ARGUMENT-LIST . BODY-FORMS)
  645.  
  646.      This macro object is stored in the function cell of NAME.  The
  647.      value returned by evaluating the `defmacro' form is NAME, but
  648.      usually we ignore this value.
  649.  
  650.      The shape and meaning of ARGUMENT-LIST is the same as in a
  651.      function, and the keywords `&rest' and `&optional' may be used
  652.      (*note Argument List::.).  Macros may have a documentation string,
  653.      but any `interactive' declaration is ignored since macros cannot be
  654.      called interactively.
  655.  
  656. 
  657. File: elisp,  Node: Backquote,  Next: Problems with Macros,  Prev: Defining Macros,  Up: Macros
  658.  
  659. Backquote
  660. =========
  661.  
  662.    It could prove rather awkward to write macros of significant size,
  663. simply due to the number of times the function `list' needs to be
  664. called.  To make writing these forms easier, a macro ``' (often called
  665. "backquote") exists.
  666.  
  667.    Backquote allows you to quote a list, but selectively evaluate
  668. elements of that list.  In the simplest case, it is identical to the
  669. special form `quote' (*note Quoting::.).  For example, these two forms
  670. yield identical results:
  671.  
  672.      (` (a list of (+ 2 3) elements))
  673.           => (a list of (+ 2 3) elements)
  674.      (quote (a list of (+ 2 3) elements))
  675.           => (a list of (+ 2 3) elements)
  676.  
  677.    By inserting a special marker, `,', inside of the argument to
  678. backquote, it is possible to evaluate desired portions of the argument:
  679.  
  680.      (list 'a 'list 'of (+ 2 3) 'elements)
  681.           => (a list of 5 elements)
  682.      (` (a list of (, (+ 2 3)) elements))
  683.           => (a list of 5 elements)
  684.  
  685.    It is also possible to have an evaluated list "spliced" into the
  686. resulting list by using the special marker `,@'.  The elements of the
  687. spliced list become elements at the same level as the other elements of
  688. the resulting list.  The equivalent code without using ``' is often
  689. unreadable.  Here are some examples:
  690.  
  691.      (setq some-list '(2 3))
  692.           => (2 3)
  693.      (cons 1 (append some-list '(4) some-list))
  694.           => (1 2 3 4 2 3)
  695.      (` (1 (,@ some-list) 4 (,@ some-list)))
  696.           => (1 2 3 4 2 3)
  697.      
  698.      (setq list '(hack foo bar))
  699.           => (hack foo bar)
  700.      (cons 'use
  701.        (cons 'the
  702.          (cons 'words (append (cdr list) '(as elements)))))
  703.           => (use the words foo bar as elements)
  704.      (` (use the words (,@ (cdr list)) as elements (,@ nil)))
  705.           => (use the words foo bar as elements)
  706.  
  707.    The reason for `(,@ nil)' is to avoid a bug in Emacs version 18.
  708. The bug occurs when a call to `,@' is followed only by constant
  709. elements.  Thus,
  710.  
  711.      (` (use the words (,@ (cdr list)) as elements))
  712.  
  713. would not work, though it really ought to.  `(,@ nil)' avoids the
  714. problem by being a nonconstant element that does not affect the result.
  715.  
  716.  - Macro: ` LIST
  717.      This macro returns LIST as `quote' would, except that the list is
  718.      copied each time this expression is evaluated, and any sublist of
  719.      the form `(, SUBEXP)' is replaced by the value of SUBEXP.  Any
  720.      sublist of the form `(,@ LISTEXP)' is replaced by evaluating
  721.      LISTEXP and splicing its elements into the containing list in
  722.      place of this sublist.  (A single sublist can in this way be
  723.      replaced by any number of new elements in the containing list.)
  724.  
  725.      There are certain contexts in which `,' would not be recognized and
  726.      should not be used:
  727.  
  728.           ;; Use of a `,' expression as the CDR of a list.
  729.           (` (a . (, 1)))                             ; Not `(a . 1)'
  730.                => (a \, 1)
  731.  
  732.           ;; Use of `,' in a vector.
  733.           (` [a (, 1) c])                             ; Not `[a 1 c]'
  734.                error--> Wrong type argument
  735.  
  736.           ;; Use of a `,' as the entire argument of ``'.
  737.           (` (, 2))                                   ; Not 2
  738.                => (\, 2)
  739.  
  740.      Common Lisp note: in Common Lisp, `,' and `,@' are implemented as
  741.      reader macros, so they do not require parentheses.  Emacs Lisp
  742.      implements them as functions because reader macros are not
  743.      supported (to save space).
  744.  
  745. 
  746. File: elisp,  Node: Problems with Macros,  Prev: Backquote,  Up: Macros
  747.  
  748. Common Problems Using Macros
  749. ============================
  750.  
  751.    The basic facts of macro expansion have all been described above, but
  752. there consequences are often counterintuitive.  This section describes
  753. some important consequences that can lead to trouble, and rules to
  754. follow to avoid trouble.
  755.  
  756. * Menu:
  757.  
  758. * Argument Evaluation::    The expansion should evaluate each macro arg once.
  759. * Surprising Local Vars::  Local variable bindings in the expansion
  760.                               require special care.
  761. * Eval During Expansion::  Don't evaluate them; put them in the expansion.
  762. * Repeated Expansion::     Avoid depending on how many times expansion is done.
  763.  
  764. 
  765. File: elisp,  Node: Argument Evaluation,  Next: Surprising Local Vars,  Prev: Problems with Macros,  Up: Problems with Macros
  766.  
  767. Evaluating Macro Arguments Too Many Times
  768. -----------------------------------------
  769.  
  770.    When defining a macro you must pay attention to the number of times
  771. the arguments will be evaluated when the expansion is executed.  The
  772. following macro (used to facilitate iteration) illustrates the problem.
  773. This macro allows us to write a simple "for" loop such as one might
  774. find in Pascal.
  775.  
  776.      (defmacro for (var from init to final do &rest body)
  777.        "Execute a simple \"for\" loop, e.g.,
  778.          (for i from 1 to 10 do (print i))."
  779.        (list 'let (list (list var init))
  780.              (cons 'while (cons (list '<= var final)
  781.                                 (append body (list (list 'inc var)))))))
  782.      => for
  783.      (for i from 1 to 3 do
  784.         (setq square (* i i))
  785.         (princ (format "\n%d %d" i square)))
  786.      ==>
  787.  
  788.      (let ((i 1))
  789.        (while (<= i 3)
  790.          (setq square (* i i))
  791.          (princ (format "%d      %d" i square))
  792.          (inc i)))
  793.  
  794.  
  795.      -|1       1
  796.           -|2       4
  797.           -|3       9
  798.      => nil
  799.  
  800. (The arguments `from', `to', and `do' in this macro are "syntactic
  801. sugar"; they are entirely ignored.  The idea is that you will write
  802. noise words (such as `from', `to', and `do') in those positions in the
  803. macro call.)
  804.  
  805.    This macro suffers from the defect that FINAL is evaluated on every
  806. iteration.  If FINAL is a constant, this is not a problem.  If it is a
  807. more complex form, say `(long-complex-calculation x)', this can slow
  808. down the execution significantly.  If FINAL has side effects, executing
  809. it more than once is probably incorrect.
  810.  
  811.    A well-designed macro definition takes steps to avoid this problem by
  812. producing an expansion that evaluates the argument expressions exactly
  813. once unless repeated evaluation is part of the intended purpose of the
  814. macro.  Here is a correct expansion for the `for' macro:
  815.  
  816.      (let ((i 1)
  817.            (max 3))
  818.        (while (<= i max)
  819.          (setq square (* i i))
  820.          (princ (format "%d      %d" i square))
  821.          (inc i)))
  822.  
  823.    Here is a macro definition that creates this expansion:
  824.  
  825.      (defmacro for (var from init to final do &rest body)
  826.        "Execute a simple for loop: (for i from 1 to 10 do (print i))."
  827.        (` (let (((, var) (, init))
  828.                 (max (, final)))
  829.             (while (<= (, var) max)
  830.               (,@ body)
  831.               (inc (, var))))))
  832.  
  833.    Unfortunately, this introduces another problem.  Proceed to the
  834. following node.
  835.  
  836. 
  837. File: elisp,  Node: Surprising Local Vars,  Next: Eval During Expansion,  Prev: Argument Evaluation,  Up: Problems with Macros
  838.  
  839. Local Variables in Macro Expansions
  840. -----------------------------------
  841.  
  842.    In the previous section, the definition of `for' was fixed as
  843. follows to make the expansion evaluate the macro arguments the proper
  844. number of times:
  845.  
  846.      (defmacro for (var from init to final do &rest body)
  847.        "Execute a simple for loop: (for i from 1 to 10 do (print i))."
  848.  
  849.      (` (let (((, var) (, init))
  850.                 (max (, final)))
  851.             (while (<= (, var) max)
  852.               (,@ body)
  853.               (inc (, var))))))
  854.  
  855.    The new definition of `for' has a new problem: it introduces a local
  856. variable named `max' which the user does not expect.  This causes
  857. trouble in examples such as the following:
  858.  
  859.      (let ((max 0))
  860.        (for x from 0 to 10 do
  861.          (let ((this (frob x)))
  862.            (if (< max this)
  863.                (setq max this)))))
  864.  
  865. The references to `max' inside the body of the `for', which are
  866. supposed to refer to the user's binding of `max', really access the
  867. binding made by `for'.
  868.  
  869.    The way to correct this is to use an uninterned symbol instead of
  870. `max' (*note Creating Symbols::.).  The uninterned symbol can be bound
  871. and referred to just like any other symbol, but since it is created by
  872. `for', we know that it cannot appear in the user's program.  Since it
  873. is not interned, there is no way the user can put it into the program
  874. later.  It will never appear anywhere except where put by `for'.  Here
  875. is a definition of `for' which works this way:
  876.  
  877.      (defmacro for (var from init to final do &rest body)
  878.        "Execute a simple for loop: (for i from 1 to 10 do (print i))."
  879.        (let ((tempvar (make-symbol "max")))
  880.          (` (let (((, var) (, init))
  881.                   ((, tempvar) (, final)))
  882.               (while (<= (, var) (, tempvar))
  883.                      (,@ body)
  884.                      (inc (, var)))))))
  885.  
  886. This creates an uninterned symbol named `max' and puts it in the
  887. expansion instead of the usual interned symbol `max' that appears in
  888. expressions ordinarily.
  889.  
  890. 
  891. File: elisp,  Node: Eval During Expansion,  Next: Repeated Expansion,  Prev: Surprising Local Vars,  Up: Problems with Macros
  892.  
  893. Evaluating Macro Arguments in Expansion
  894. ---------------------------------------
  895.  
  896.    Another problem can happen if you evaluate any of the macro argument
  897. expressions during the computation of the expansion, such as by calling
  898. `eval' (*note Eval::.).  If the argument is supposed to refer to the
  899. user's variables, you may have trouble if the user happens to use a
  900. variable with the same name as one of the macro arguments.  Inside the
  901. macro body, the macro argument binding is the most local binding of this
  902. variable, so any references inside the form being evaluated do refer to
  903. it.  Here is an example:
  904.  
  905.      (defmacro foo (a)
  906.        (list 'setq (eval a) t))
  907.           => foo
  908.      (setq x 'b)
  909.      (foo x) ==> (setq b t)
  910.           => t                  ; and `b' has been set.
  911.      ;; but
  912.      (setq a 'b)
  913.      (foo a) ==> (setq 'b t)     ; invalid!
  914.      error--> Symbol's value is void: b
  915.  
  916.    It makes a difference whether the user types `a' or `x', because `a'
  917. conflicts with the macro argument variable `a'.
  918.  
  919.    In general it is best to avoid calling `eval' in a macro definition
  920. at all.
  921.  
  922. 
  923. File: elisp,  Node: Repeated Expansion,  Prev: Eval During Expansion,  Up: Problems with Macros
  924.  
  925. How Many Times is the Macro Expanded?
  926. -------------------------------------
  927.  
  928.    Occasionally problems result from the fact that a macro call is
  929. expanded each time it is evaluated in an interpreted function, but is
  930. expanded only once (during compilation) for a compiled function.  If the
  931. macro definition has side effects, they will work differently depending
  932. on how many times the macro is expanded.
  933.  
  934.    In particular, constructing objects is a kind of side effect.  If the
  935. macro is called once, then the objects are constructed only once.  In
  936. other words, the same structure of objects is used each time the macro
  937. call is executed.  In interpreted operation, the macro is reexpanded
  938. each time, producing a fresh collection of objects each time.  Usually
  939. this does not matter--the objects have the same contents whether they
  940. are shared or not.  But if the surrounding program does side effects on
  941. the objects, it makes a difference whether they are shared.  Here is an
  942. example:
  943.  
  944.      (defmacro new-object ()
  945.        (list 'quote (cons nil nil)))
  946.  
  947.      (defun initialize (condition)
  948.        (let ((object (new-object)))
  949.          (if condition
  950.          (setcar object condition))
  951.          object))
  952.  
  953. If `initialize' is interpreted, a new list `(nil)' is constructed each
  954. time `initialize' is called.  Thus, no side effect survives between
  955. calls.  If `initialize' is compiled, then the macro `new-object' is
  956. expanded during compilation, producing a single "constant" `(nil)' that
  957. is reused and altered each time `initialize' is called.
  958.  
  959. 
  960. File: elisp,  Node: Loading,  Next: Byte Compilation,  Prev: Macros,  Up: Top
  961.  
  962. Loading
  963. *******
  964.  
  965.    Loading a file of Lisp code means bringing its contents into the Lisp
  966. environment in the form of Lisp objects.  Emacs finds and opens the
  967. file, reads the text, evaluates each form, and then closes the file.
  968.  
  969.    The load functions evaluate all the expressions in a file just as
  970. the `eval-current-buffer' function evaluates all the expressions in a
  971. buffer.  The difference is that the load functions read and evaluate
  972. the text in the file as found on disk, not the text in an Emacs buffer.
  973.  
  974.    The loaded file must contain Lisp expressions, either as source code
  975. or, optionally, as byte-compiled code.  Each form in the file is called
  976. a "top-level form".  There is no special format for the forms in a
  977. loadable file; any form in a file may equally well be typed directly
  978. into a buffer and evaluated there.  (Indeed, most code is tested this
  979. way.)  Most often, the forms are function definitions and variable
  980. definitions.
  981.  
  982.    A file containing Lisp code is often called a "library".  Thus, the
  983. "Rmail library" is a file containing code for Rmail mode.  Similarly, a
  984. "Lisp library directory" is a directory of files containing Lisp code.
  985.  
  986. * Menu:
  987.  
  988. * How Programs Do Loading::     The `load' function and others.
  989. * Autoload::                    Setting up a function to autoload.
  990. * Repeated Loading::            Precautions about loading a file twice.
  991. * Features::                    Loading a library if it isn't already loaded.
  992. * Unloading::            How to "unload" a library that was loaded.
  993. * Hooks for Loading::        Providing code to be run when
  994.                   particular libraries are loaded.
  995.  
  996. 
  997. File: elisp,  Node: How Programs Do Loading,  Next: Autoload,  Up: Loading
  998.  
  999. How Programs Do Loading
  1000. =======================
  1001.  
  1002.    There are several interface functions for loading.  For example, the
  1003. `autoload' function creates a Lisp object that loads a file when it is
  1004. evaluated (*note Autoload::.).  `require' also causes files to be
  1005. loaded (*note Features::.).  Ultimately, all these facilities call the
  1006. `load' function to do the work.
  1007.  
  1008.  - Function: load FILENAME &optional MISSING-OK NOMESSAGE NOSUFFIX
  1009.      This function finds and opens a file of Lisp code, evaluates all
  1010.      the forms in it, and closes the file.
  1011.  
  1012.      To find the file, `load' first looks for a file named
  1013.      `FILENAME.elc', that is, for a file whose name has `.elc'
  1014.      appended.  If such a file exists, it is loaded.  But if there is
  1015.      no file by that name, then `load' looks for a file whose name has
  1016.      `.el' appended.  If that file exists, it is loaded.  Finally, if
  1017.      there is no file by either name, `load' looks for a file named
  1018.      FILENAME with nothing appended, and loads it if it exists.  (The
  1019.      `load' function is not clever about looking at FILENAME.  In the
  1020.      perverse case of a file named `foo.el.el', evaluation of `(load
  1021.      "foo.el")' will indeed find it.)
  1022.  
  1023.      If the optional argument NOSUFFIX is non-`nil', then the suffixes
  1024.      `.elc' and `.el' are not tried.  In this case, you must specify
  1025.      the precise file name you want.
  1026.  
  1027.      If FILENAME is a relative file name, such as `foo' or
  1028.      `baz/foo.bar', `load' searches for the file using the variable
  1029.      `load-path'.  It appends FILENAME to each of the directories
  1030.      listed in `load-path', and loads the first file it finds whose
  1031.      name matches.  The current default directory is tried only if it is
  1032.      specified in `load-path', where it is represented as `nil'.  All
  1033.      three possible suffixes are tried in the first directory in
  1034.      `load-path', then all three in the second directory in
  1035.      `load-path', etc.
  1036.  
  1037.      If you get a warning that `foo.elc' is older than `foo.el', it
  1038.      means you should consider recompiling `foo.el'.  *Note Byte
  1039.      Compilation::.
  1040.  
  1041.      Messages like `Loading foo...' and `Loading foo...done' appear in
  1042.      the echo area during loading unless NOMESSAGE is non-`nil'.
  1043.  
  1044.      Any errors that are encountered while loading a file cause `load'
  1045.      to abort.  If the load was done for the sake of `autoload', certain
  1046.      kinds of top-level forms, those which define functions, are undone.
  1047.  
  1048.      The error `file-error' is signaled (with `Cannot open load file
  1049.      FILENAME') if no file is found.  No error is signaled if
  1050.      MISSING-OK is non-`nil'--then `load' just returns `nil'.
  1051.  
  1052.      `load' returns `t' if the file loads successfully.
  1053.  
  1054.  - User Option: load-path
  1055.      The value of this variable is a list of directories to search when
  1056.      loading files with `load'.  Each element is a string (which must be
  1057.      a directory name) or `nil' (which stands for the current working
  1058.      directory).  The value of `load-path' is initialized from the
  1059.      environment variable `EMACSLOADPATH', if it exists; otherwise it is
  1060.      set to the default specified in `emacs/src/paths.h' when Emacs is
  1061.      built.
  1062.  
  1063.      The syntax of `EMACSLOADPATH' is the same as that of `PATH';
  1064.      fields are separated by `:', and `.' is used for the current
  1065.      default directory.  Here is an example of how to set your
  1066.      `EMACSLOADPATH' variable from a `csh' `.login' file:
  1067.  
  1068.           setenv EMACSLOADPATH .:/user/bil/emacs:/usr/local/lib/emacs/lisp
  1069.  
  1070.      Here is how to set it using `sh':
  1071.  
  1072.           export EMACSLOADPATH
  1073.           EMACSLOADPATH=.:/user/bil/emacs:/usr/local/lib/emacs/lisp
  1074.  
  1075.      Here is an example of code you can place in a `.emacs' file to add
  1076.      several directories to the front of your default `load-path':
  1077.  
  1078.           (setq load-path
  1079.                 (append
  1080.                  (list nil
  1081.                        "/user/bil/emacs"
  1082.                        "/usr/local/lisplib")
  1083.                  load-path))
  1084.  
  1085.      In this example, the path searches the current working directory
  1086.      first, followed then by the `/user/bil/emacs' directory and then by
  1087.      the `/usr/local/lisplib' directory, which are then followed by the
  1088.      standard directories for Lisp code.
  1089.  
  1090.      When Emacs version 18 processes command options `-l' or `-load'
  1091.      which specify Lisp libraries to be loaded, it temporarily adds the
  1092.      current directory to the front of `load-path' so that files in the
  1093.      current directory can be specified easily.  Newer Emacs versions
  1094.      also find such files in the current directory, but without
  1095.      altering `load-path'.
  1096.  
  1097.  - Variable: load-in-progress
  1098.      This variable is non-`nil' if Emacs is in the process of loading a
  1099.      file, and it is `nil' otherwise.  This is how `defun' and
  1100.      `provide' determine whether a load is in progress, so that their
  1101.      effect can be undone if the load fails.
  1102.  
  1103.    To learn how `load' is used to build Emacs, see *Note Building
  1104. Emacs::.
  1105.  
  1106. 
  1107. File: elisp,  Node: Autoload,  Next: Repeated Loading,  Prev: How Programs Do Loading,  Up: Loading
  1108.  
  1109. Autoload
  1110. ========
  1111.  
  1112.    The "autoload" facility allows you to make a function or macro
  1113. available but put off loading its actual definition.  An attempt to call
  1114. a symbol whose definition is an autoload object automatically reads the
  1115. file to install the real definition and its other associated code, and
  1116. then calls the real definition.
  1117.  
  1118.    To prepare a function or macro for autoloading, you must call
  1119. `autoload', specifying the function name and the name of the file to be
  1120. loaded.  A file such as `emacs/lisp/loaddefs.el' usually does this when
  1121. Emacs is first built.
  1122.  
  1123.    The following example shows how `doctor' is prepared for autoloading
  1124. in `loaddefs.el':
  1125.  
  1126.      (autoload 'doctor "doctor"
  1127.        "\
  1128.      Switch to *doctor* buffer and start giving psychotherapy."
  1129.        t)
  1130.  
  1131. The backslash and newline immediately following the double-quote are a
  1132. convention used only in the preloaded Lisp files such as `loaddefs.el';
  1133. they cause the documentation string to be put in the `etc/DOC' file.
  1134. (*Note Building Emacs::.)  In any other source file, you would write
  1135. just this:
  1136.  
  1137.      (autoload 'doctor "doctor"
  1138.        "Switch to *doctor* buffer and start giving psychotherapy."
  1139.        t)
  1140.  
  1141.    Calling `autoload' creates an autoload object containing the name of
  1142. the file and some other information, and makes this the function
  1143. definition of the specified symbol.  When you later try to call that
  1144. symbol as a function or macro, the file is loaded; the loading should
  1145. redefine that symbol with its proper definition.  After the file
  1146. completes loading, the function or macro is called as if it had been
  1147. there originally.
  1148.  
  1149.    If, at the end of loading the file, the desired Lisp function or
  1150. macro has not been defined, then the error `error' is signaled (with
  1151. data `"Autoloading failed to define function FUNCTION-NAME"').
  1152.  
  1153.    The autoloaded file may, of course, contain other definitions and may
  1154. require or provide one or more features.  If the file is not completely
  1155. loaded (due to an error in the evaluation of the contents) any function
  1156. definitions or `provide' calls that occurred during the load are
  1157. undone.  This is to ensure that the next attempt to call any function
  1158. autoloading from this file will try again to load the file.  If not for
  1159. this, then some of the functions in the file might appear defined, but
  1160. they may fail to work properly for the lack of certain subroutines
  1161. defined later in the file and not loaded successfully.
  1162.  
  1163.    Emacs as distributed comes with many autoloaded functions.  The
  1164. calls to `autoload' are in the file `loaddefs.el'.  There is a
  1165. convenient way of updating them automatically.
  1166.  
  1167.    Write `;;;###autoload' on a line by itself before a function
  1168. definition before the real definition of the function, in its
  1169. autoloadable source file; then the command `M-x update-file-autoloads'
  1170. automatically puts the `autoload' call into `loaddefs.el'.  `M-x
  1171. update-directory-autoloads' is more powerful; it updates autoloads for
  1172. all files in the current directory.
  1173.  
  1174.    You can also put other kinds of forms into `loaddefs.el', by writing
  1175. `;;;###autoload' followed on the same line by the form.  `M-x
  1176. update-file-autoloads' copies the form from that line.
  1177.  
  1178.    The commands for updating autoloads work by visiting and editing the
  1179. file `loaddefs.el'.  To make the result take effect, you must save that
  1180. file's buffer.
  1181.  
  1182.  - Function: autoload SYMBOL FILENAME &optional DOCSTRING INTERACTIVE
  1183.           TYPE
  1184.      This function defines the function (or macro) named SYMBOL so as
  1185.      to load automatically from FILENAME.  The string FILENAME is a
  1186.      file name which will be passed to `load' when the function is
  1187.      called.
  1188.  
  1189.      The argument DOCSTRING is the documentation string for the
  1190.      function.  Normally, this is the same string that is in the
  1191.      function definition itself.  This makes it possible to look at the
  1192.      documentation without loading the real definition.
  1193.  
  1194.      If INTERACTIVE is non-`nil', then the function can be called
  1195.      interactively.  This lets completion in `M-x' work without loading
  1196.      the function's real definition.  The complete interactive
  1197.      specification need not be given here.  If TYPE is `macro', then
  1198.      the function is really a macro.  If TYPE is `keymap', then the
  1199.      function is really a keymap.
  1200.  
  1201.      If SYMBOL already has a non-`nil' function definition that is not
  1202.      an autoload object, `autoload' does nothing and returns `nil'.  If
  1203.      the function cell of SYMBOL is void, or is already an autoload
  1204.      object, then it is set to an autoload object that looks like this:
  1205.  
  1206.           (autoload FILENAME DOCSTRING INTERACTIVE TYPE)
  1207.  
  1208.      For example,
  1209.  
  1210.           (symbol-function 'run-prolog)
  1211.                => (autoload "prolog" 169681 t nil)
  1212.  
  1213.      In this case, `"prolog"' is the name of the file to load, 169681
  1214.      refers to the documentation string in the `emacs/etc/DOC' file
  1215.      (*note Documentation Basics::.), `t' means the function is
  1216.      interactive, and `nil' that it is not a macro.
  1217.  
  1218. 
  1219. File: elisp,  Node: Repeated Loading,  Next: Features,  Prev: Autoload,  Up: Loading
  1220.  
  1221. Repeated Loading
  1222. ================
  1223.  
  1224.    You may load a file more than once in an Emacs session.  For
  1225. example, after you have rewritten and reinstalled a function definition
  1226. by editing it in a buffer, you may wish to return to the original
  1227. version; you can do this by reloading the file in which it is located.
  1228.  
  1229.    When you load or reload files, bear in mind that the `load' and
  1230. `load-library' functions automatically load a byte-compiled file rather
  1231. than a non-compiled file of similar name.  If you rewrite a file that
  1232. you intend to save and reinstall, remember to byte-compile it if
  1233. necessary; otherwise you may find yourself inadvertently reloading the
  1234. older, byte-compiled file instead of your newer, non-compiled file!
  1235.  
  1236.    When writing the forms in a library, keep in mind that the library
  1237. might be loaded more than once.  For example, the choice of `defvar'
  1238. vs. `defconst' for defining a variable depends on whether it is
  1239. desirable to reinitialize the variable if the library is reloaded:
  1240. `defconst' does so, and `defvar' does not.  (*Note Defining
  1241. Variables::.)
  1242.  
  1243.    The simplest way to add an element to an alist is like this:
  1244.  
  1245.      (setq minor-mode-alist
  1246.            (cons '(leif-mode " Leif") minor-mode-alist))
  1247.  
  1248. But this would add multiple elements if the library is reloaded.  To
  1249. avoid the problem, write this:
  1250.  
  1251.      (or (assq 'leif-mode minor-mode-alist)
  1252.          (setq minor-mode-alist
  1253.                (cons '(leif-mode " Leif") minor-mode-alist)))
  1254.  
  1255.    Occasionally you will want to test explicitly whether a library has
  1256. already been loaded; you can do so as follows:
  1257.  
  1258.      (if (not (boundp 'foo-was-loaded))
  1259.          EXECUTE-FIRST-TIME-ONLY)
  1260.      
  1261.      (setq foo-was-loaded t)
  1262.  
  1263.