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

  1. This is Info file elisp, produced by Makeinfo-1.47 from the input file
  2. elisp.texi.
  3.  
  4.    This file documents GNU Emacs Lisp.
  5.  
  6.    This is edition 1.03 of the GNU Emacs Lisp Reference Manual, for
  7. Emacs Version 18.
  8.  
  9.    Published by the Free Software Foundation, 675 Massachusetts Avenue,
  10. Cambridge, MA 02139 USA
  11.  
  12.    Copyright (C) 1990 Free Software Foundation, Inc.
  13.  
  14.    Permission is granted to make and distribute verbatim copies of this
  15. manual provided the copyright notice and this permission notice are
  16. preserved on all copies.
  17.  
  18.    Permission is granted to copy and distribute modified versions of
  19. this manual under the conditions for verbatim copying, provided that
  20. the entire resulting derived work is distributed under the terms of a
  21. permission notice identical to this one.
  22.  
  23.    Permission is granted to copy and distribute translations of this
  24. manual into another language, under the above conditions for modified
  25. versions, except that this permission notice may be stated in a
  26. translation approved by the Foundation.
  27.  
  28. 
  29. File: elisp,  Node: Output Functions,  Prev: Output Streams,  Up: Streams
  30.  
  31. Output Functions
  32. ----------------
  33.  
  34.    This section describes the Lisp functions and variables that pertain
  35. to printing.
  36.  
  37.    Some of the Emacs printing functions add quoting characters to the
  38. output when necessary so that it can be read properly.  The quoting
  39. characters used are `\' and `"'; they are used to distinguish strings
  40. from symbols, and to prevent punctuation characters in strings and
  41. symbols from being taken as delimiters.  *Note Printed
  42. Representation::, for full details.  You specify quoting or no quoting
  43. by the choice of printing function.
  44.  
  45.    If the text is to be read back into Lisp, then it is best to print
  46. with quoting characters to avoid ambiguity.  Likewise, if the purpose is
  47. to describe a Lisp object clearly for a Lisp programmer.  However, if
  48. the purpose of the output is to look nice for humans, then it is better
  49. to print without quoting.
  50.  
  51.    In the functions below, STREAM stands for an output stream. (See the
  52. previous section for a description of output streams.)  If STREAM is
  53. `nil' or omitted, it defaults to the value of `standard-output'.
  54.  
  55.  -- Function: print OBJECT &optional STREAM
  56.      The `print' is a convenient way of printing.  It outputs the
  57.      printed representation of OBJECT to STREAM, printing in addition
  58.      one newline before OBJECT and another after it.  Quoting
  59.      characters are used.  `print' returns OBJECT.  For example:
  60.  
  61.           (progn (print 'The\ cat\ in)
  62.                  (print "the hat")
  63.                  (print " came back"))
  64.                -|
  65.                -| The\ cat\ in
  66.                -|
  67.                -| "the hat"
  68.                -|
  69.                -| " came back"
  70.                -|
  71.                => " came back"
  72.  
  73.  -- Function: prin1 OBJECT &optional STREAM
  74.      This function outputs the printed representation of OBJECT to
  75.      STREAM.  It does not print any spaces or newlines to separate
  76.      output as `print' does, but it does use quoting characters just
  77.      like `print'.  It returns OBJECT.
  78.  
  79.           (progn (prin1 'The\ cat\ in)
  80.                  (prin1 "the hat")
  81.                  (prin1 " came back"))
  82.                -| The\ cat\ in"the hat"" came back"
  83.                => " came back"
  84.  
  85.  -- Function: prin1-to-string OBJECT
  86.      This function returns a string containing the text that `prin1'
  87.      would have printed for the same argument.
  88.  
  89.           (prin1-to-string 'foo)
  90.                => "foo"
  91.           (prin1-to-string (mark-marker))
  92.                => "#<marker at 2773 in strings.texi>"
  93.  
  94.      See `format', in *Note String Conversion::, for other ways to
  95.      obtain the printed representation of a Lisp object as a string.
  96.  
  97.  -- Function: princ OBJECT &optional STREAM
  98.      This function outputs the printed representation of OBJECT to
  99.      STREAM.  It returns OBJECT.
  100.  
  101.      This function is intended to produce output that is readable by
  102.      people, not by `read', so quoting characters are not used and
  103.      double-quotes are not printed around the contents of strings.  It
  104.      does not add any spacing between calls.
  105.  
  106.           (progn
  107.             (princ 'The\ cat)
  108.             (princ " in the \"hat\""))
  109.                -| The cat in the "hat"
  110.                => " in the \"hat\""
  111.  
  112.  -- Function: terpri &optional STREAM
  113.      This function outputs a newline to STREAM.  The name stands for
  114.      "terminate print".
  115.  
  116.  -- Variable: standard-output
  117.      The value of this variable is the default output stream, used when
  118.      the STREAM argument is omitted or `nil'.
  119.  
  120.  -- Variable: print-escape-newlines
  121.      If this variable is non-`nil', then newline characters in strings
  122.      are printed as `\n'.  Normally they are printed as actual newlines.
  123.  
  124.      This variable affects the print functions `prin1' and `print'; it
  125.      does not affect `princ' in Emacs 18, but this may be changed. Here
  126.      is an example using `prin1':
  127.  
  128.           (prin1 "a\nb")
  129.                -| "a
  130.                -| b"
  131.                => "a
  132.                => b"
  133.           
  134.           (let ((print-escape-newlines t))
  135.             (prin1 "a\nb"))
  136.                -| "a\nb"
  137.                => "a
  138.                => b"
  139.  
  140.      In the second expression, the local binding of
  141.      `print-escape-newlines' is in effect during the call to `prin1',
  142.      but not during the printing of the result.
  143.  
  144.  -- Variable: print-length
  145.      The value of this variable is the maximum number of elements of a
  146.      list that will be printed.  If the list being printed has more
  147.      than this many elements, then it is abbreviated with an ellipsis.
  148.  
  149.      If the value is `nil' (the default), then there is no limit.
  150.  
  151.           (setq print-length 2)
  152.                => 2
  153.           (print '(1 2 3 4 5))
  154.                -| (1 2 ...)
  155.                => (1 2 ...)
  156.  
  157.  -- Function: write-char CHARACTER &optional STREAM
  158.      This function outputs CHARACTER to STREAM.  It returns CHARACTER.
  159.  
  160. 
  161. File: elisp,  Node: Minibuffers,  Next: Command Loop,  Prev: Streams,  Up: Top
  162.  
  163. Minibuffers
  164. ***********
  165.  
  166.    A "minibuffer" is a special buffer used by Emacs commands to read
  167. arguments more complicated than the single numeric prefix argument.
  168. These arguments include file names, buffer names, and command names (as
  169. in `M-x').  The minibuffer is displayed on the bottom line of the
  170. screen, in the same place as the echo area, but only while it is in use
  171. for reading an argument.
  172.  
  173. * Menu:
  174.  
  175. * Intro to Minibuffers::      Basic information about minibuffers.
  176. * Text from Minibuffer::      How to read a straight text string.
  177. * Object from Minibuffer::    How to read a Lisp object or expression.
  178. * Completion::                How to invoke and customize completion.
  179. * Yes-or-No Queries::         Asking a question with a simple answer.
  180. * Minibuffer Misc::           Various customization hooks and variables.
  181.  
  182. 
  183. File: elisp,  Node: Intro to Minibuffers,  Next: Text from Minibuffer,  Prev: Minibuffers,  Up: Minibuffers
  184.  
  185. Introduction to Minibuffers
  186. ===========================
  187.  
  188.    In most ways, a minibuffer is a normal Emacs buffer.  Most operations
  189. *within* a buffer, such as editing commands, work normally in a
  190. minibuffer.  However, many operations for managing buffers do not apply
  191. to minibuffers.  The name of a minibuffer always has the form
  192. ` *Minibuf-NUMBER', and it cannot be changed.  There is a special
  193. window used only for minibuffers, and minibuffers cannot be displayed in
  194. any other window.  This window is normally the single line at the bottom
  195. of the screen; it can be resized temporarily with the window sizing
  196. commands, but reverts to its normal size when the minibuffer is exited.
  197.  
  198.    A "recursive minibuffer" may be created when there is an active
  199. minibuffer and a command is invoked that requires input from a
  200. minibuffer.  The first minibuffer is named ` *Minibuf-0*'. Recursive
  201. minibuffers are named by incrementing the number at the end of the
  202. name.  (The names begin with a space so that they won't show up in
  203. normal buffer lists.)  Of several recursive minibuffers, the innermost
  204. (or most recently entered) is the active minibuffer, and is the only one
  205. that is displayed in a window.  We usually call this "the" minibuffer.
  206. Recursive minibuffers may be allowed or disallowed by setting the
  207. variable `enable-recursive-minibuffers'.
  208.  
  209.    Like other buffers, a minibuffer may use any of several local keymaps
  210. (*note Keymaps::.); these contain various exit commands and in some
  211. cases completion commands.  *Note Completion::.
  212.  
  213.    * `minibuffer-local-map' is for ordinary input (no completion).
  214.  
  215.    * `minibuffer-local-ns-map' is similar, except that SPC exits just
  216.      like RET.  This is used mainly for Mocklisp compatibility.
  217.  
  218.    * `minibuffer-local-completion-map' is for permissive completion.
  219.  
  220.    * `minibuffer-local-must-match-map' is for strict completion and for
  221.      cautious completion.
  222.  
  223.    * `repeat-complex-command-map' is for use in `C-x ESC'.
  224.  
  225. 
  226. File: elisp,  Node: Text from Minibuffer,  Next: Object from Minibuffer,  Prev: Intro to Minibuffers,  Up: Minibuffers
  227.  
  228. Reading Text Strings with the Minibuffer
  229. ========================================
  230.  
  231.    The minibuffer is usually used to read text which is returned as a
  232. string, but can also be used to read a Lisp object in textual form.  The
  233. most basic primitive for minibuffer input is `read-from-minibuffer'.
  234.  
  235.  -- Function: read-from-minibuffer PROMPT-STRING &optional INITIAL
  236.           KEYMAP READ
  237.      This function is the most general way to get input through the
  238.      minibuffer.  By default, it accepts arbitrary text and returns it
  239.      as a string; however, if READ is non-`nil', then it uses `read' to
  240.      convert the text into a Lisp object (*note Input Functions::.).
  241.  
  242.      The first thing this function does is to activate a minibuffer and
  243.      display it with  PROMPT-STRING as the prompt.  This value must be a
  244.      string.
  245.  
  246.      Then, if INITIAL is non-`nil', it must be a string; its contents
  247.      are inserted into the minibuffer as initial contents.  The text
  248.      thus inserted is treated as if the user had inserted it; the user
  249.      can alter it with Emacs editing commands.
  250.  
  251.      If KEYMAP is non-`nil', that keymap is the local keymap to use
  252.      while reading.  If KEYMAP is omitted or `nil', the value of
  253.      `minibuffer-local-map' is used as the keymap.  Specifying a keymap
  254.      is the most important way to customize minibuffer input for
  255.      various applications including completion.
  256.  
  257.      When the user types a command to exit the minibuffer, the current
  258.      minibuffer contents are usually made into a string which is the
  259.      value of `read-from-minibuffer'.  However, if READ is non-`nil',
  260.      Emacs converts the result to a Lisp object and
  261.      `read-from-minibuffer' returns that object, unevaluated.
  262.  
  263.      Suppose, for example, you are writing a search command and want to
  264.      record the last search string and provide it as a default for the
  265.      next search.  Suppose that the previous search string is stored in
  266.      the variable `last-search-string'.  Here is how you can read a
  267.      search string while providing the previous string as initial input
  268.      to be edited:
  269.  
  270.           (read-from-minibuffer "Find string: " last-search-string)
  271.  
  272.      Assuming the value of `last-search-string' is `No', and the user
  273.      wants to search for `Nope', the interaction looks like this:
  274.  
  275.           (setq last-search-string "No")
  276.           
  277.           (read-from-minibuffer "Find string: " last-search-string)
  278.           ---------- Buffer: Minibuffer ----------
  279.           Find string: No-!-
  280.           ---------- Buffer: Minibuffer ----------
  281.           ;; The user now types `pe RET':
  282.                => "Nope"
  283.  
  284.  -- Function: read-string PROMPT &optional INITIAL
  285.      This function reads a string from the minibuffer and returns it. 
  286.      The arguments PROMPT and INITIAL are used as in
  287.      `read-from-minibuffer'.
  288.  
  289.      This function is a simplified interface to `read-from-minibuffer':
  290.  
  291.           (read-string PROMPT INITIAL)
  292.           ==
  293.           (read-from-minibuffer PROMPT INITIAL nil nil)
  294.  
  295.  -- Variable: minibuffer-local-map
  296.      This is the default local keymap for reading from the minibuffer. 
  297.      It is the keymap used by the minibuffer for local bindings in the
  298.      function `read-string'.  By default, it makes the following
  299.      bindings:
  300.  
  301.     LFD
  302.           `exit-minibuffer'
  303.  
  304.     RET
  305.           `exit-minibuffer'
  306.  
  307.     `C-g'
  308.           `abort-recursive-edit'
  309.  
  310.  -- Function: read-no-blanks-input PROMPT INITIAL
  311.      This function reads a string from the minibuffer, but does not
  312.      allow whitespace characters as part of the input: instead, those
  313.      characters terminate the input.  The arguments PROMPT and INITIAL
  314.      are used as in `read-from-minibuffer'.
  315.  
  316.      This function is a simplified interface to `read-from-minibuffer',
  317.      and passes the value of `minibuffer-local-ns-map' as the KEYMAP
  318.      argument for that function.  Since the keymap
  319.      `minibuffer-local-ns-map' does not rebind `C-q', it *is* possible
  320.      to put a space into the string, by quoting it.
  321.  
  322.           (read-no-blanks-input PROMPT INITIAL)
  323.           ==
  324.           (read-from-minibuffer PROMPT INITIAL minibuffer-local-ns-map)
  325.  
  326.  -- Variable: minibuffer-local-ns-map
  327.      This built-in variable is the keymap used as the minibuffer local
  328.      keymap in the function `read-no-blanks-input'.  By default, it
  329.      makes the following bindings:
  330.  
  331.     `LFD'
  332.           `exit-minibuffer'
  333.  
  334.     `SPC'
  335.           `exit-minibuffer'
  336.  
  337.     `TAB'
  338.           `exit-minibuffer'
  339.  
  340.     `RET'
  341.           `exit-minibuffer'
  342.  
  343.     `C-g'
  344.           `abort-recursive-edit'
  345.  
  346.     `?'
  347.           `self-insert-and-exit'
  348.  
  349. 
  350. File: elisp,  Node: Object from Minibuffer,  Next: Completion,  Prev: Text from Minibuffer,  Up: Minibuffers
  351.  
  352. Reading Lisp Objects with the Minibuffer
  353. ========================================
  354.  
  355.    This section describes functions for reading Lisp objects with the
  356. minibuffer.
  357.  
  358.  -- Function: read-minibuffer PROMPT &optional INITIAL
  359.      This function reads a Lisp object in the minibuffer and returns it,
  360.      unevaluated.  The arguments PROMPT and INITIAL are used as in
  361.      `read-from-minibuffer'; in particular, INITIAL must be a string or
  362.      `nil'.
  363.  
  364.      This function is a simplified interface to `read-from-minibuffer':
  365.  
  366.           (read-minibuffer PROMPT INITIAL)
  367.           ==
  368.           (read-from-minibuffer PROMPT INITIAL nil t)
  369.  
  370.      Here is an example in which we supply the string `"(testing)"' as
  371.      initial input:
  372.  
  373.           (read-minibuffer "Enter an expression: " (format "%s" '(testing)))
  374.           
  375.           ;;  Here is how the minibuffer is displayed:
  376.           
  377.           ---------- Buffer: Minibuffer ----------
  378.           Enter an expression: (testing)-!-
  379.           ---------- Buffer: Minibuffer ----------
  380.  
  381.      The user can type RET immediately to use the initial input as a
  382.      default, or can edit the input.
  383.  
  384.  -- Function: eval-minibuffer PROMPT &optional INITIAL
  385.      This function reads a Lisp expression in the minibuffer, evaluates
  386.      it, then returns the result.  The arguments PROMPT and INITIAL are
  387.      used as in `read-from-minibuffer'.
  388.  
  389.      This function simply evaluates the result of a call to
  390.      `read-minibuffer':
  391.  
  392.           (eval-minibuffer PROMPT INITIAL)
  393.           ==
  394.           (eval (read-minibuffer PROMPT INITIAL))
  395.  
  396.  -- Function: edit-and-eval-command PROMPT FORM
  397.      This function reads a Lisp expression in the minibuffer, and then
  398.      evaluates it.  The difference between this command and
  399.      `eval-minibuffer' is that here the initial FORM is not optional
  400.      and it is treated as a Lisp object to be converted to printed
  401.      representation rather than as a string of text.  It is printed with
  402.      `prin1', so if it is a string, double-quote characters (`"') will
  403.      appear in the initial text.  *Note Output Functions::.
  404.  
  405.      The first thing `edit-and-eval-command' does is to activate the
  406.      minibuffer with PROMPT as the prompt.  The printed representation
  407.      of FORM is then inserted in the minibuffer, and the user is
  408.      allowed to edit.  When the user exits the minibuffer, the edited
  409.      text is read with `read' and then evaluated.  The resulting value
  410.      becomes the value of `edit-and-eval-command'.
  411.  
  412.      In the following example, we offer the user an expression with
  413.      initial text which is a valid form already:
  414.  
  415.           (edit-and-eval-command "Please edit: " '(forward-word 1))
  416.           
  417.           ;; After evaluating the preceding expression,
  418.           ;; the following appears in the minibuffer:
  419.           
  420.           ---------- Buffer: Minibuffer ----------
  421.           Please edit: (forward-word 1)-!-
  422.           ---------- Buffer: Minibuffer ----------
  423.  
  424.      Typing RET right away would exit the minibuffer and evaluate the
  425.      expression, thus moving point forward one word.
  426.      `edit-and-eval-command' returns `nil' in this example.
  427.  
  428. 
  429. File: elisp,  Node: Completion,  Next: Yes-or-No Queries,  Prev: Object from Minibuffer,  Up: Minibuffers
  430.  
  431. Completion
  432. ==========
  433.  
  434.    "Completion" is a feature that fills in the rest of a name starting
  435. from an abbreviation for it.  Completion works by comparing the user's
  436. input against a list of valid names and determining how much of the
  437. name is determined uniquely by what the user has typed.
  438.  
  439.    For example, when you type `C-x b' (`switch-to-buffer') and then
  440. type the first few letters of the name of the buffer to which you wish
  441. to switch, and then type TAB (`minibuffer-complete'), Emacs extends the
  442. name as far as it can.  Standard Emacs commands offer completion for
  443. names of symbols, files, buffers, and processes; with the functions in
  444. this section, you can implement completion for other kinds of names.
  445.  
  446.    The `try-completion' function is the basic primitive for completion:
  447. it returns the longest determined completion of a given initial string,
  448. with a given set of strings to match against.
  449.  
  450.    The function `completing-read' provides a higher-level interface for
  451. completion.  A call to `completing-read' specifies how to determine the
  452. list of valid names.  The function then activates the minibuffer with a
  453. local keymap that binds a few keys to commands useful for completion. 
  454. Other functions provide convenient simple interfaces for reading
  455. certain kinds of names with completion.
  456.  
  457. * Menu:
  458.  
  459. * Basic Completion::       Low-level functions for completing strings.
  460.                              (These are too low level to use the minibuffer.)
  461. * Programmed Completion::  Finding the completions for a given file name.
  462. * Minibuffer Completion::  Invoking the minibuffer with completion.
  463. * Completion Commands::    Minibuffer commands that do completion.
  464. * High-Level Completion::  Convenient special cases of completion
  465.                              (reading buffer name, file name, etc.)
  466. * Reading File Names::     Using completion to read file names.
  467. * Lisp Symbol Completion:: Completing the name of a symbol.
  468.  
  469. 
  470. File: elisp,  Node: Basic Completion,  Next: Programmed Completion,  Prev: Completion,  Up: Completion
  471.  
  472. Basic Completion Functions
  473. --------------------------
  474.  
  475.  -- Function: try-completion STRING ALIST-OR-OBARRAY &optional PREDICATE
  476.      This function returns the longest common substring of all possible
  477.      completions of STRING in ALIST-OR-OBARRAY.
  478.  
  479.      If ALIST-OR-OBARRAY is an association list (*note Association
  480.      Lists::.), the CAR of each cons cell in it is compared against
  481.      STRING; if the beginning of the CAR equals STRING, the cons cell
  482.      matches.  If no cons cells match, `try-completion' returns `nil'. 
  483.      If only one cons cell matches, and the match is exact, then
  484.      `try-completion' returns `t'.  Otherwise, all matching strings are
  485.      compared, and the longest initial sequence common to them is
  486.      returned as a string.
  487.  
  488.      If ALIST-OR-OBARRAY is an obarray (*note Creating Symbols::.), the
  489.      names of all symbols in the obarray form the space of possible
  490.      names.  They are tested and used just like the CARs of the elements
  491.      of an association list.  (The global variable `obarray' holds an
  492.      obarray containing the names of all interned Lisp symbols.)
  493.  
  494.      If the argument PREDICATE is non-`nil', then it must be a function
  495.      of one argument.  It is used to test each possible match, and the
  496.      match is accepted only if PREDICATE returns non-`nil'. The
  497.      argument given to PREDICATE is either a cons cell from the alist
  498.      (the CAR of which is a string) or else it is a symbol (*not* a
  499.      symbol name) from the obarray.
  500.  
  501.      It is also possible to use a function as ALIST-OR-OBARRAY.  Then
  502.      the function is solely responsible for performing completion;
  503.      `try-completion' returns whatever this function returns.  The
  504.      function is called with three arguments: STRING, PREDICATE and
  505.      `nil'.  (The reason for the third argument is so that the same
  506.      function can be used in `all-completions' and do the appropriate
  507.      thing in either case.)  *Note Programmed Completion::.
  508.  
  509.      In the first of the following examples, the string `foo' is
  510.      matched by three of the alist CARs.  All of the matches begin with
  511.      the characters `fooba', so that is the result.  In the second
  512.      example, there is only one possible match, and it is exact, so the
  513.      value is `t'.
  514.  
  515.           (try-completion "foo"
  516.                '(("foobar1" 1) ("barfoo" 2) ("foobaz" 3) ("foobar2" 4)))
  517.                => "fooba"
  518.           
  519.           (try-completion "foo" '(("barfoo" 2) ("foo" 3)))
  520.                => t
  521.  
  522.      In the following example, numerous symbols begin with the
  523.      characters `forw', and all of them begin with the word `forward'. 
  524.      In most of the symbols, this is followed with a `-', but not in
  525.      all, so no more than `forward' can be completed.
  526.  
  527.           (try-completion "forw" obarray)
  528.                => "forward"
  529.  
  530.      Finally, in the following example, only two of the three possible
  531.      matches pass the predicate `test' (the string `foobaz' is too
  532.      short).  Both of those begin with the string `foobar'.
  533.  
  534.           (defun test (s)
  535.             (> (length (car s)) 6))
  536.                => test
  537.           (try-completion "foo"
  538.                '(("foobar1" 1) ("barfoo" 2) ("foobaz" 3) ("foobar2" 4))
  539.                'test)
  540.                => "foobar"
  541.  
  542.  -- Function: all-completions STRING ALIST-OR-OBARRAY &optional
  543.           PREDICATE
  544.      This function returns a list of all possible completions, instead
  545.      of the longest substring they share.  The parameters to this
  546.      function are the same as to `try-completion'.
  547.  
  548.      If ALIST-OR-OBARRAY is a function, it is called with three
  549.      arguments: STRING, PREDICATE and `t', and `all-completions'
  550.      returns whatever the function returns. *Note Programmed
  551.      Completion::.
  552.  
  553.      Here is an example, using the same function `test' used in the
  554.      example for `try-completion':
  555.  
  556.           (defun test (s)
  557.             (> (length (car s)) 6))
  558.                => test
  559.           
  560.           (all-completions  "foo"
  561.                '(("foobar1" 1) ("barfoo" 2) ("foobaz" 3) ("foobar2" 4))
  562.                (function test))
  563.                => ("foobar1" "foobar2")
  564.  
  565.  -- Variable: completion-ignore-case
  566.      If the value of this variable is non-`nil', Emacs does not
  567.      consider case significant in completion.
  568.  
  569.    The two functions `try-completion' and `all-completions' have
  570. nothing in themselves to do with minibuffers.  However, completion is
  571. most often used there, which is why it is described in this chapter.
  572.  
  573. 
  574. File: elisp,  Node: Programmed Completion,  Next: Minibuffer Completion,  Prev: Basic Completion,  Up: Completion
  575.  
  576. Programmed Completion
  577. ---------------------
  578.  
  579.    Sometimes it is not possible to create an alist or an obarray
  580. containing all the intended possible completions.  In such a case, you
  581. can supply your own function to compute the completion of a given
  582. string. This is called "programmed completion".
  583.  
  584.    To use this feature, pass the function as the ALIST-OR-OBARRAY
  585. argument to `completing-read'.  This command will arrange to pass the
  586. function along to `try-completion' and `all-completions', which will
  587. then let your function do all the work.
  588.  
  589.    The completion function should accept three arguments:
  590.  
  591.    * The string to be completed.
  592.  
  593.    * The predicate function to filter possible matches, or `nil' if
  594.      none. Your function should call the predicale for each possible
  595.      match and ignore the possible match if the predicate returns `nil'.
  596.  
  597.    * A flag specifying the type of operation.
  598.  
  599.    There are three flag values for three operations:
  600.  
  601.    * `nil' specifies `try-completion'.  The completion function should
  602.      return the completion of the specified string, or `t' if the
  603.      string is an exact match already, or `nil' if the string matches no
  604.      possibility.
  605.  
  606.    * `t' specifies `all-completions'.  The completion function should
  607.      return a list of all possible completions of the specified string.
  608.  
  609.    * `lambda' specifies a test for an exact match.  The completion
  610.      function should return `t' if the specified string is an exact
  611.      match for some possibility; `nil' otherwise.
  612.  
  613.    Emacs uses programmed completion when completing file names. *Note
  614. File Name Completion::.
  615.  
  616. 
  617. File: elisp,  Node: Minibuffer Completion,  Next: Completion Commands,  Prev: Programmed Completion,  Up: Completion
  618.  
  619. Completion and the Minibuffer
  620. -----------------------------
  621.  
  622.    This section describes the basic interface for reading from the
  623. minibuffer with completion.
  624.  
  625.  -- Function: completing-read PROMPT ALIST-OR-OBARRAY &optional
  626.           PREDICATE REQUIRE-MATCH INITIAL
  627.      This function reads a string in the minibuffer, assisting the user
  628.      by providing completion.  It activates the minibuffer with prompt
  629.      PROMPT, which must be a string.  If INITIAL is non-`nil',
  630.      `completing-read' inserts it into the minibuffer as part of the
  631.      input.  Then it allows the user to edit the input, providing
  632.      several commands to attempt completion.
  633.  
  634.      The actual completion is done by passing ALIST-OR-OBARRAY and
  635.      PREDICATE to the function `try-completion'.  This happens in
  636.      certain commands bound in the local keymaps used for completion.
  637.  
  638.      If REQUIRE-MATCH is `t', the user will not be allowed to exit
  639.      unless the input completes to an element of ALIST-OR-OBARRAY. If
  640.      REQUIRE-MATCH is neither `nil' nor `t', then `completing-read'
  641.      does not exit unless the input typed is itself an element of
  642.      ALIST-OR-OBARRAY.  To accomplish this, `completing-read' calls
  643.      `read-minibuffer' with the keymap
  644.      `minibuffer-local-completion-map' if REQUIRE-MATCH is `nil', or
  645.      else with the keymap `minibuffer-local-must-match-map', if
  646.      REQUIRE-MATCH is non-`nil'.
  647.  
  648.      Case is ignored when comparing the input against the possible
  649.      matches if the built-in variable `completion-ignore-case' is
  650.      non-`nil'.  *Note Basic Completion::.
  651.  
  652.      For example:
  653.  
  654.           (completing-read "Complete a foo: "
  655.                '(("foobar1" 1) ("barfoo" 2) ("foobaz" 3) ("foobar2" 4))
  656.                nil t "fo")
  657.           
  658.           ;; After evaluating the preceding expression,
  659.           ;; the following appears in the minibuffer:
  660.           
  661.           ---------- Buffer: Minibuffer ----------
  662.           Complete a foo: fo-!-
  663.           ---------- Buffer: Minibuffer ----------
  664.  
  665.      If the user then types `DEL DEL b RET', `completing-read' returns
  666.      `barfoo'.
  667.  
  668.      The `completing-read' function binds three variables to pass
  669.      information to the commands which actually do completion.  Here
  670.      they are:
  671.  
  672.     `minibuffer-completion-table'
  673.           This variable is bound to ALIST-OR-OBARRAY argument.  It is
  674.           passed to the `try-completion' function.
  675.  
  676.     `minibuffer-completion-predicate'
  677.           This variable is bound to the PREDICATE argument.  It is
  678.           passed to the `try-completion' function.
  679.  
  680.     `minibuffer-completion-confirm'
  681.           This variable is bound to the REQUIRE-MATCH argument.  It is
  682.           used in the `minibuffer-complete-and-exit' function.
  683.  
  684. 
  685. File: elisp,  Node: Completion Commands,  Next: High-Level Completion,  Prev: Minibuffer Completion,  Up: Completion
  686.  
  687. Minibuffer Commands That Do Completion
  688. --------------------------------------
  689.  
  690.    This section describes the keymaps, commands and user options used in
  691. the minibuffer to do completion.
  692.  
  693.  -- Variable: minibuffer-local-completion-map
  694.      `completing-read' uses this value as the local keymap when an
  695.      exact match of one of the completions is not required.  By
  696.      default, this keymap makes the following bindings:
  697.  
  698.     `?'
  699.           `minibuffer-completion-help'
  700.  
  701.     `SPC'
  702.           `minibuffer-complete-word'
  703.  
  704.     `TAB'
  705.           `minibuffer-complete'
  706.  
  707.     `LFD'
  708.           `exit-minibuffer'
  709.  
  710.     `RET'
  711.           `exit-minibuffer'
  712.  
  713.     `C-g'
  714.           `abort-recursive-edit'
  715.  
  716.  
  717.  -- Variable: minibuffer-local-must-match-map
  718.      `completing-read' uses this value as the local keymap when an
  719.      exact match of one of the completions is required.  Therefore, no
  720.      keys are bound to `exit-minibuffer', the command which exits the
  721.      minibuffer unconditionally.  By default, this keymap makes the
  722.      following bindings:
  723.  
  724.     `?'
  725.           `minibuffer-completion-help'
  726.  
  727.     `SPC'
  728.           `minibuffer-complete-word'
  729.  
  730.     `TAB'
  731.           `minibuffer-complete'
  732.  
  733.     `LFD'
  734.           `minibuffer-complete-and-exit'
  735.  
  736.     `RET'
  737.           `minibuffer-complete-and-exit'
  738.  
  739.     `C-g'
  740.           `abort-recursive-edit'
  741.  
  742.  -- Variable: minibuffer-completion-table
  743.      The value of this variable is the alist or obarray used for
  744.      completion in the minibuffer.  This is the global variable that
  745.      contains what `completing-read' passes to `try-completion'.  It is
  746.      used by all the minibuffer completion functions, such as
  747.      `minibuffer-complete-word'.
  748.  
  749.  -- Variable: minibuffer-completion-predicate
  750.      The value of this variable is the predicate that `completing-read'
  751.      passes to `try-completion'.  The variable is also used by the
  752.      other minibuffer completion functions.
  753.  
  754.  -- Command: minibuffer-complete-word
  755.      This function completes the minibuffer contents by at most a single
  756.      word.  Even if the minibuffer contents has only one completion,
  757.      `minibuffer-complete-word' will not add any characters beyond the
  758.      first character that is not a word constituent.  *Note Syntax
  759.      Tables::.
  760.  
  761.  -- Command: minibuffer-complete
  762.      This function completes the minibuffer contents as far as possible.
  763.  
  764.  -- Command: minibuffer-complete-and-exit
  765.      This function completes the minibuffer contents, and exits if
  766.      confirmation is not required, i.e., if
  767.      `minibuffer-completion-confirm' is non-`nil'.  If confirmation
  768.      *is* required, it is given by repeating this command immediately.
  769.  
  770.  -- Variable: minibuffer-completion-confirm
  771.      When the value of this variable is non-`nil', Emacs asks for
  772.      confirmation of a completion before exiting the minibuffer.  The
  773.      function `minibuffer-complete-and-exit' checks the value of this
  774.      variable before it exits.
  775.  
  776.  -- Command: minibuffer-completion-help
  777.      This function creates a list of the possible completions of the
  778.      current minibuffer contents.  It works by calling
  779.      `all-completions'; the values of `minibuffer-completion-table' and
  780.      `minibuffer-completion-predicate' are used as arguments.  The list
  781.      of completions is displayed as text in a buffer named
  782.      `*Completions*'.
  783.  
  784.  -- Function: display-completion-list COMPLETIONS
  785.      This function displays COMPLETIONS to the stream `standard-output'
  786.      (usually a buffer).  (*Note Streams::, for more information about
  787.      streams.)  The argument COMPLETIONS is normally a list of
  788.      completions just returned by `all-completions', but it does not
  789.      have to be.  Each element may be a symbol or a string, either of
  790.      which is simply printed, or a list of two strings, which is printed
  791.      as if the strings were concatenated.
  792.  
  793.      This function is called by `minibuffer-completion-help'.
  794.  
  795.  -- User Option: completion-auto-help
  796.      If this variable is non-`nil', the completion commands
  797.      automatically display a list of possible completions whenever
  798.      nothing can be completed because the next character is not
  799.      uniquely determined.
  800.  
  801. 
  802. File: elisp,  Node: High-Level Completion,  Next: Reading File Names,  Prev: Completion Commands,  Up: Completion
  803.  
  804. High-Level Completion  Functions
  805. --------------------------------
  806.  
  807.    This section describes the higher-level convenient functions for
  808. reading certain sorts of names with completion.
  809.  
  810.  -- Function: read-buffer PROMPT &optional DEFAULT EXISTING
  811.      This function reads the name of a buffer and returns it as a
  812.      string. The argument DEFAULT is the default name to use, the value
  813.      to return if the user exits with an empty minibuffer.  If
  814.      non-`nil', it should be a string.  It is mentioned in the prompt,
  815.      but is not inserted in the minibuffer as initial input.
  816.  
  817.      If EXISTING is non-`nil', then the name specified must be that of
  818.      an  existing buffer.  The usual commands to exit the minibuffer
  819.      will not exit if the text is not valid, and RET will do completion
  820.      to attempt to find a valid name.  (However, DEFAULT is not checked
  821.      for this; it is returned, whatever it is, if the user exits with
  822.      the minibuffer empty.)
  823.  
  824.      In the following example, the user enters `minibuffer.t', and then
  825.      types RET.  The argument EXISTING is `t', and the only buffer name
  826.      starting with the given input is `minibuffer.texi', so that name
  827.      is the value.
  828.  
  829.           (read-buffer "Buffer name? " "foo" t)
  830.           
  831.           ;; After evaluating the preceding expression,
  832.           ;; the following prompt appears, with an empty minibuffer:
  833.           
  834.           ---------- Buffer: Minibuffer ----------
  835.           Buffer name? (default foo) -!-
  836.           ---------- Buffer: Minibuffer ----------
  837.           
  838.           ;; The user types `minibuffer.t RET'.
  839.           
  840.                => "minibuffer.texi"
  841.  
  842.  -- Function: read-command PROMPT
  843.      This function reads the name of a command and returns it as a Lisp
  844.      symbol.  The argument PROMPT is used as in `read-from-minibuffer'.
  845.       Recall that a command is anything for which `commandp' returns
  846.      `t', and a command name is a symbol for which `commandp' returns
  847.      `t'.  *Note Interactive Call::.
  848.  
  849.           (read-command "Command name? ")
  850.           
  851.           ;; After evaluating the preceding expression,
  852.           ;; the following appears in the minibuffer:
  853.           
  854.           ---------- Buffer: Minibuffer ----------
  855.           Command name?
  856.           ---------- Buffer: Minibuffer ----------
  857.  
  858.      If the user types `forward-c RET', then this function returns
  859.      `forward-char'.
  860.  
  861.      The `read-command' function is a simplified interface to
  862.      `completing-read'.  It uses the `commandp' predicate to allow only
  863.      commands to be entered, and it uses the variable `obarray' so as
  864.      to be able to complete all extant Lisp symbols:
  865.  
  866.           (read-command PROMPT)
  867.           ==
  868.           (intern (completing-read PROMPT obarray 'commandp t nil))
  869.  
  870.  -- Function: read-variable PROMPT
  871.      This function reads the name of a user variable and returns it as a
  872.      symbol.
  873.  
  874.           (read-variable "Variable name? ")
  875.           
  876.           ;; After evaluating the preceding expression,
  877.           ;; the following prompt appears with an empty minibuffer:
  878.           
  879.           ---------- Buffer: Minibuffer ----------
  880.           Variable name? -!-
  881.           ---------- Buffer: Minibuffer ----------
  882.  
  883.      If the user then types `fill-p RET', `read-variable' will return
  884.      `fill-prefix'.
  885.  
  886.      This function is similar to `read-command', but uses the predicate
  887.      `user-variable-p' instead of `commandp':
  888.  
  889.           (read-variable PROMPT)
  890.           ==
  891.           (intern (completing-read PROMPT obarray 'user-variable-p t nil))
  892.  
  893. 
  894. File: elisp,  Node: Reading File Names,  Next: Lisp Symbol Completion,  Prev: High-Level Completion,  Up: Completion
  895.  
  896. Reading File Names
  897. ------------------
  898.  
  899.    Here is another high-level completion function, designed for reading
  900. a file name.  It provides special features including automatic insertion
  901. of the default directory.
  902.  
  903.  -- Function: read-file-name PROMPT &optional DIRECTORY DEFAULT EXISTING
  904.      This function reads a file name in the minibuffer, prompting with
  905.      PROMPT and providing completion.  If DEFAULT is non-`nil', then
  906.      the value of DEFAULT will be returned by the function if the user
  907.      just types RET.
  908.  
  909.      If EXISTING is non-`nil', then the name must refer to an existing
  910.      file; then RET performs completion to make the name valid if
  911.      possible, and then refuses to exit if it is not valid.  If the
  912.      value of EXISTING is neither `nil' nor `t', then RET also requires
  913.      confirmation after completion.
  914.  
  915.      The argument DIRECTORY specifies the directory to use for
  916.      completion of relative file names.  Usually it is inserted in the
  917.      minibuffer as initial input as well.  It defaults to the current
  918.      buffer's default directory.
  919.  
  920.      Here is an example:
  921.  
  922.           (read-file-name "The file is ")
  923.           
  924.           ;; After evaluating the preceding expression,
  925.           ;; the following appears in the minibuffer:
  926.           
  927.           ---------- Buffer: Minibuffer ----------
  928.           The file is /gp/gnu/elisp/-!-
  929.           ---------- Buffer: Minibuffer ----------
  930.  
  931.      Typing `manual TAB' results in the following:
  932.  
  933.           ---------- Buffer: Minibuffer ----------
  934.           The file is /gp/gnu/elisp/manual.texi-!-
  935.           ---------- Buffer: Minibuffer ----------
  936.  
  937.      If the user types RET, `read-file-name' returns
  938.      `"/gp/gnu/elisp/manual.texi"'.
  939.  
  940.  -- User Option: insert-default-directory
  941.      This variable is used by `read-file-name'.  The value of this
  942.      variable controls whether `read-file-name' starts by placing the
  943.      name of the default directory in the minibuffer.  If the value of
  944.      this variable is `nil', then `read-file-name' does not place any
  945.      initial input in the minibuffer.  In that case, the default
  946.      directory is still used for completion of relative file names, but
  947.      is not displayed.
  948.  
  949.      For example:
  950.  
  951.           ;; Here the minibuffer starts out containing the default directory.
  952.           
  953.           (let ((insert-default-directory t))
  954.             (read-file-name "The file is "))
  955.           
  956.           ---------- Buffer: Minibuffer ----------
  957.           The file is ~lewis/manual/-!-
  958.           ---------- Buffer: Minibuffer ----------
  959.           
  960.           ;; Here the minibuffer is empty and only the prompt appears on its line.
  961.           
  962.           (let ((insert-default-directory nil))
  963.             (read-file-name "The file is "))
  964.           
  965.           ---------- Buffer: Minibuffer ----------
  966.           The file is -!-
  967.           ---------- Buffer: Minibuffer ----------
  968.  
  969. 
  970. File: elisp,  Node: Lisp Symbol Completion,  Prev: Reading File Names,  Up: Completion
  971.  
  972. Lisp Symbol Completion
  973. ----------------------
  974.  
  975.    If you type a part of a symbol, and then type `M-TAB'
  976. (`lisp-complete-symbol'), it will attempt to fill in as much more of
  977. the symbol name as it can.  Not only does this save typing, but it can
  978. help you with the name of a symbol that you have partially forgotten.
  979.  
  980.  -- Command: lisp-complete-symbol
  981.      This function performs completion on the symbol name preceding
  982.      point. The name is completed against the symbols in the global
  983.      variable `obarray', and characters from the completion are
  984.      inserted into the buffer, making the name longer.  If there is
  985.      more than one completion, a list of all possible completions is
  986.      placed in the `*Help*' buffer. The bell rings if there is no
  987.      possible completion in `obarray'.
  988.  
  989.      If an open parenthesis immediately precedes the name, only symbols
  990.      with function definitions are considered.  (By reducing the number
  991.      of alternatives, this may succeed in completing more characters.)
  992.      Otherwise, symbols with either a function definition, a value, or
  993.      at least one property are considered.
  994.  
  995.      `lisp-complete-symbol' returns `t' if the symbol had an exact, and
  996.      unique, match; otherwise, it returns `nil'.
  997.  
  998.      In the following example, the user has already inserted `(forwa'
  999.      into the buffer `foo.el'.  The command `lisp-complete-symbol' then
  1000.      completes the name to `(forward-'.
  1001.  
  1002.           ---------- Buffer: foo.el ----------
  1003.           (forwa-!-
  1004.           ---------- Buffer: foo.el ----------
  1005.           
  1006.           (lisp-complete-symbol)
  1007.                => nil
  1008.           
  1009.           ---------- Buffer: foo.el ----------
  1010.           (forward--!-
  1011.           ---------- Buffer: foo.el ----------
  1012.  
  1013. 
  1014. File: elisp,  Node: Yes-or-No Queries,  Next: Minibuffer Misc,  Prev: Completion,  Up: Minibuffers
  1015.  
  1016. Yes-or-No Queries
  1017. =================
  1018.  
  1019.    This section describes functions used to ask the user a yes-or-no
  1020. question.  The function `y-or-n-p' can be answered with a single
  1021. character; it is useful for questions where an inadvertent wrong answer
  1022. will not have serious consequences.  `yes-or-no-p' is suitable for more
  1023. momentous questions, since it requires three or four characters to
  1024. answer.
  1025.  
  1026.    Strictly speaking, `yes-or-no-p' uses the minibuffer and `y-or-n-p'
  1027. does not; but it seems best to describe them together.
  1028.  
  1029.  -- Function: y-or-n-p PROMPT
  1030.      This function asks the user a question, expecting input in the echo
  1031.      area.  It returns `t' if the user types `y', `nil' if the user
  1032.      types `n'.  This function also accepts SPC to mean yes and DEL to
  1033.      mean no.  The answer is a single character, with no RET needed to
  1034.      terminate it.  Upper and lower case are equivalent.
  1035.  
  1036.      "Asking the question" means printing PROMPT in the echo area,
  1037.      followed by the string `(y or n) '.  If the input is not one of the
  1038.      expected answers (`y', `n', `SPC', or `DEL'), the function
  1039.      responds `Please answer y or n.', and repeats the request.
  1040.  
  1041.      This function does not actually use the minibuffer, since it does
  1042.      not allow editing of the answer.  It actually uses the echo area
  1043.      (*note The Echo Area::.), which uses the same screen space as the
  1044.      minibuffer.  The cursor moves to the echo area while the question
  1045.      is being asked.
  1046.  
  1047.      In the following example, the user first types `q', which is
  1048.      invalid.  At the next prompt the user types `n'.
  1049.  
  1050.           (y-or-n-p "Do you need a lift? ")
  1051.           
  1052.           ;; After evaluating the preceding expression,
  1053.           ;; the following prompt appears in the echo area:
  1054.           
  1055.           ---------- Echo area ----------
  1056.           Do you need a lift? (y or n)
  1057.           ---------- Echo area ----------
  1058.           
  1059.           ;; If the user then types `q', the following appears:
  1060.           
  1061.           ---------- Echo area ----------
  1062.           Please answer y or n.  Do you need a lift? (y or n)
  1063.           ---------- Echo area ----------
  1064.           
  1065.           ;; When the user types a valid answer, it is displayed after the question:
  1066.           
  1067.           ---------- Echo area ----------
  1068.           Do you need a lift? (y or n) y
  1069.           ---------- Echo area ----------
  1070.  
  1071.      Note that we show successive lines of echo area messages here. 
  1072.      Only one will appear on the screen at a time.
  1073.  
  1074.  -- Function: yes-or-no-p PROMPT
  1075.      This function asks the user a question, expecting input in
  1076.      minibuffer. It returns `t' if the user enters `yes', `nil' if the
  1077.      user types `no'.  The user must type RET to finalize the response.
  1078.       Upper and lower case are equivalent.
  1079.  
  1080.      `yes-or-no-p' starts by displaying PROMPT in the echo area,
  1081.      followed by `(yes or no) '.  The user must type one of the
  1082.      expected responses; otherwise, the function responds `Please answer
  1083.      yes or no.', waits about two seconds and repeats the request.
  1084.  
  1085.      `yes-or-no-p' requires more work from the user than `y-or-n-p' and
  1086.      is appropriate for more crucial decisions.
  1087.  
  1088.      Here is an example:
  1089.  
  1090.           (yes-or-no-p "Do you really want to remove your entire directory? ")
  1091.           
  1092.           ;; After evaluating the preceding expression,
  1093.           ;; the following prompt appears with an empty minibuffer:
  1094.           
  1095.           ---------- Buffer: minibuffer ----------
  1096.           Do you really want to remove your entire directory? (yes or no)
  1097.           ---------- Buffer: minibuffer ----------
  1098.  
  1099.      If the user first types `y RET', which is invalid because this
  1100.      function demands the entire word `yes', it responds by displaying
  1101.      these prompts, with a brief pause between them:
  1102.  
  1103.           ---------- Buffer: minibuffer ----------
  1104.           Please answer yes or no.
  1105.           Do you really want to remove your entire directory? (yes or no)
  1106.           ---------- Buffer: minibuffer ----------
  1107.  
  1108. 
  1109. File: elisp,  Node: Minibuffer Misc,  Prev: Yes-or-No Queries,  Up: Minibuffers
  1110.  
  1111. Minibuffer Miscellany
  1112. =====================
  1113.  
  1114.    Some basic minibuffer functions and variables are described in this
  1115. section.
  1116.  
  1117.  -- Command: exit-minibuffer
  1118.      This function exits the active minibuffer.  It is normally bound to
  1119.      keys in minibuffer local keymaps.
  1120.  
  1121.  -- Command: self-insert-and-exit
  1122.      This function exits the active minibuffer after inserting the last
  1123.      character typed on the keyboard (found in `last-command-char';
  1124.      *note Command Loop Info::.).
  1125.  
  1126.  -- Variable: minibuffer-help-form
  1127.      The current value of this variable is used to rebind `help-form'
  1128.      locally inside the minibuffer (*note Help Functions::.).
  1129.  
  1130.  -- Function: minibuffer-window
  1131.      This function returns the window that is used for the minibuffer.
  1132.      There is one and only one minibuffer window in Emacs 18; this
  1133.      window always exists and cannot be deleted.
  1134.  
  1135.  -- Variable: minibuffer-scroll-window
  1136.      If the value of this variable is non-`nil', it should be a window
  1137.      object.  When the function `scroll-other-window' is called in the
  1138.      minibuffer, it will scroll the `minibuffer-scroll-window' window.
  1139.  
  1140.    Finally, some functions and variables deal with recursive minibuffers
  1141. (*note Recursive Editing::.):
  1142.  
  1143.  -- Function: minibuffer-depth
  1144.      This function returns the current depth of activations of the
  1145.      minibuffer, a nonnegative integer.  If no minibuffers are active,
  1146.      it returns zero.
  1147.  
  1148.  -- User Option: enable-recursive-minibuffers
  1149.      If this variable is non-`nil', you can invoke commands (such as
  1150.      `find-file') which use minibuffers even while in the minibuffer
  1151.      window.  Such invocation produces a recursive editing level for a
  1152.      new minibuffer.  The outer-level minibuffer is invisible while you
  1153.      are editing the inner one.
  1154.  
  1155.      This variable only affects invoking the minibuffer while the
  1156.      minibuffer window is selected.   If you switch windows while in the
  1157.      minibuffer, you can always invoke minibuffer commands while some
  1158.      other window is selected.
  1159.  
  1160. 
  1161. File: elisp,  Node: Command Loop,  Next: Keymaps,  Prev: Minibuffers,  Up: Top
  1162.  
  1163. Command Loop
  1164. ************
  1165.  
  1166.    When you run Emacs, it enters the "editor command loop" almost
  1167. immediately.  This loop reads key sequences, executes their definitions,
  1168. and displays the results.  In this chapter, we describe how these things
  1169. are done, and the subroutines that allow Lisp programs to do them.
  1170.  
  1171. * Menu:
  1172.  
  1173. * Command Overview::    How the command loop reads commands.
  1174. * Defining Commands::   Specifying how a function should read arguments.
  1175. * Interactive Call::    Calling a command, so that it will read arguments.
  1176. * Command Loop Info::   Variables set by the command loop for you to examine.
  1177. * Keyboard Input::      How your program can read characters from the keyboard.
  1178. * Quitting::            How `C-g' works.  How to catch or defer quitting.
  1179. * Prefix Command Arguments::    How the commands to set prefix args work.
  1180. * Recursive Editing::   Entering a recursive edit,
  1181.                           and why you usually shouldn't.
  1182. * Disabling Commands::  How the command loop handles disabled commands.
  1183. * Command History::     How the command history is set up, and how accessed.
  1184. * Keyboard Macros::     How keyboard macros are implemented.
  1185.  
  1186. 
  1187. File: elisp,  Node: Command Overview,  Next: Defining Commands,  Prev: Command Loop,  Up: Command Loop
  1188.  
  1189. Command Loop Overview
  1190. =====================
  1191.  
  1192.    The first thing the command loop must do is read a key sequence,
  1193. which is a sequence of characters that translates into a command.  It
  1194. does this by calling the function `read-key-sequence'.  Your Lisp code
  1195. can also call this function (*note Keyboard Input::.).  Lisp programs
  1196. can also do input at a lower level with `read-char' or discard pending
  1197. input with `discard-input'.
  1198.  
  1199.    The key sequence is translated into a command through the keymaps of
  1200. the current buffer.  *Note Keymaps::, for information on how this is
  1201. done.  The result should be a keyboard macro or an interactively
  1202. callable function.  If the key is `M-x', then it reads the name of
  1203. another command, which is used instead.  This is done by the command
  1204. `execute-extended-command' (*note Interactive Call::.).
  1205.  
  1206.    Once the command is read, it must be executed, which includes reading
  1207. arguments to be given to it.  This is done by calling `command-execute'
  1208. (*note Interactive Call::.).  For commands written in Lisp, the
  1209. `interactive' specification says how to read the arguments.  This may
  1210. use the prefix argument (*note Prefix Command Arguments::.) or may read
  1211. with prompting in the minibuffer (*note Minibuffers::.).  For example,
  1212. the command `find-file' has an `interactive' specification which says
  1213. to read a file name using the minibuffer.  The command's function body
  1214. does not use the minibuffer; if you call this command from Lisp code as
  1215. a function, you must supply the file name string as an ordinary Lisp
  1216. function argument.
  1217.  
  1218.    If the command is a string (i.e., a keyboard macro) then the function
  1219. `execute-kbd-macro' is used to execute it.  You can call this function
  1220. yourself (*note Keyboard Macros::.).
  1221.  
  1222.    If a command runs away, typing `C-g' will terminate its execution
  1223. immediately.  This is called "quitting" (*note Quitting::.).
  1224.  
  1225. 
  1226. File: elisp,  Node: Defining Commands,  Next: Interactive Call,  Prev: Command Overview,  Up: Command Loop
  1227.  
  1228. Defining Commands
  1229. =================
  1230.  
  1231.    A Lisp function becomes a command when its body contains, at top
  1232. level, a form which calls the special form `interactive'.  This form
  1233. does nothing when actually executed, but its presence serves as a flag
  1234. to indicate that interactive calling is permitted.  Its argument
  1235. controls the reading of arguments for an interactive call.
  1236.  
  1237. * Menu:
  1238.  
  1239. * Using Interactive::     General rules for `interactive'.
  1240. * Interactive Codes::     The standard letter-codes for reading arguments
  1241.                              in various ways.
  1242. * Interactive Examples::  Examples of how to read interactive arguments.
  1243.  
  1244.