home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional / OS2PRO194.ISO / os2 / prgramer / unix / info / elisp.i11 (.txt) < prev    next >
GNU Info File  |  1993-06-14  |  52KB  |  932 lines

  1. This is Info file elisp, produced by Makeinfo-1.47 from the input file
  2. elisp.texi.
  3.    This file documents GNU Emacs Lisp.
  4.    This is edition 1.03 of the GNU Emacs Lisp Reference Manual, for
  5. Emacs Version 18.
  6.    Published by the Free Software Foundation, 675 Massachusetts Avenue,
  7. Cambridge, MA 02139 USA
  8.    Copyright (C) 1990 Free Software Foundation, Inc.
  9.    Permission is granted to make and distribute verbatim copies of this
  10. manual provided the copyright notice and this permission notice are
  11. preserved on all copies.
  12.    Permission is granted to copy and distribute modified versions of
  13. this manual under the conditions for verbatim copying, provided that
  14. the entire resulting derived work is distributed under the terms of a
  15. permission notice identical to this one.
  16.    Permission is granted to copy and distribute translations of this
  17. manual into another language, under the above conditions for modified
  18. versions, except that this permission notice may be stated in a
  19. translation approved by the Foundation.
  20. File: elisp,  Node: Using Interactive,  Next: Interactive Codes,  Prev: Defining Commands,  Up: Defining Commands
  21. Using `interactive'
  22. -------------------
  23.    This section describes how to write the `interactive' form that
  24. makes a Lisp function an interactively-callable command.
  25.  -- Special Form: interactive ARG-DESCRIPTOR
  26.      This special form declares that the function in which it appears
  27.      is a command, and that it may therefore be called interactively
  28.      (via `M-x' or by entering a key sequence bound to it).  The
  29.      argument ARG-DESCRIPTOR declares the way the arguments to the
  30.      command are to be computed when the command is called
  31.      interactively.
  32.      A command may be called from Lisp programs like any other
  33.      function, but then the arguments are supplied by the caller and
  34.      ARG-DESCRIPTOR has no effect.
  35.      The `interactive' form has its effect because the command loop
  36.      (actually, its subroutine `call-interactively') scans through the
  37.      function definition looking for it, before calling the function. 
  38.      Once the function is called, all its body forms including the
  39.      `interactive' form are executed, but at this time `interactive'
  40.      simply returns `nil' without even evaluating its argument.
  41.    There are three possibilities for the argument ARG-DESCRIPTOR:
  42.    * It may be omitted or `nil'; then the command is called with no
  43.      arguments.  This leads quickly to an error if the command requires
  44.      one or more arguments.
  45.    * It may be a Lisp expression that is not a string; then it should
  46.      be a form that is evaluated to get a list of arguments to pass to
  47.      the command.
  48.    * It may be a string; then its contents should consist of a code
  49.      character followed by a prompt (if required for that code
  50.      character).  The prompt ends either with the end of the string or
  51.      with a newline.  Here is a simple example:
  52.           (interactive "bFrobnicate buffer: ")
  53.      The code letter `b' says to read the name of an existing buffer,
  54.      with completion.  The buffer name will be the sole argument passed
  55.      to the command.  The rest of the string is a prompt.
  56.      If there is a newline character in the string, it terminates the
  57.      prompt. If the string does not end there, then the rest of the
  58.      string should contain another code character and prompt,
  59.      specifying another argument. Any number of arguments may be
  60.      specified in this way.
  61.      If the first character in the string is `*', then an error is
  62.      signaled if the buffer is read-only.  Otherwise, the following
  63.      character is the first code character.
  64. File: elisp,  Node: Interactive Codes,  Next: Interactive Examples,  Prev: Using Interactive,  Up: Defining Commands
  65. Code Characters for `interactive'
  66. ---------------------------------
  67.    The code character descriptions below contain a number of key words,
  68. defined here as follows:
  69. Completion
  70.      Provide completion.  TAB, SPC, and RET perform name completion
  71.      because the argument is read using `completing-read' (*note
  72.      Completion::.).  `?' displays a list of possible completions.
  73. Existing
  74.      Require the name of an existing object.  An invalid name is not
  75.      accepted; the commands to exit the minibuffer do not exit if the
  76.      current input is not valid.
  77. Default
  78.      A default value of some sort is used if the user enters no text in
  79.      the minibuffer.  The default depends on the code character.
  80. Prompt
  81.      A prompt immediately follows the code character.  The prompt ends
  82.      either with the end of the string or with a newline.
  83. No I/O
  84.      This code letter computes an argument without reading any input.
  85.      Therefore, it does not use a prompt string, and any prompt string
  86.      you supply is ignored.
  87.    Here are the code character descriptions for use with `interactive':
  88.      A function name (i.e., a symbol which is `fboundp').  Existing,
  89.      Completion, Prompt.
  90.      The name of an existing buffer.  By default, uses the name of the
  91.      current buffer (*note Buffers::.).  Existing, Completion, Default,
  92.      Prompt.
  93.      A buffer name.  The buffer need not exist.  By default, uses the
  94.      name of a recently used buffer other than the current buffer. 
  95.      Completion, Prompt.
  96.      A character.  The cursor does not move into the echo area.  Prompt.
  97.      A command name (i.e., a symbol satisfying `commandp').  Existing,
  98.      Completion, Prompt.
  99.      The position of point as a number (*note Point::.).  No I/O.
  100.      A directory name.  The default is the current default directory of
  101.      the current buffer, `default-directory' (*note System
  102.      Environment::.). Existing, Completion, Default, Prompt.
  103.      A file name of an existing file (*note File Names::.).  The default
  104.      directory is `default-directory'.  Existing, Completion, Default,
  105.      Prompt.
  106.      A file name.  The file need not exist.  Completion, Default,
  107.      Prompt.
  108.      A key sequence (*note Keymap Terms::.).  This keeps reading
  109.      characters until a command (or undefined command) is found in the
  110.      current key maps. The key sequence argument is represented as a
  111.      string.  The cursor does not move into the echo area.  Prompt.
  112.      This kind of input is used by commands such as `describe-key' and
  113.      `global-set-key'.
  114.      The position of the mark as a number.  No I/O.
  115.      A number read with the minibuffer.  If the input is not a number,
  116.      the user is asked to try again.  The prefix argument, if any, is
  117.      not used. Prompt.
  118.      The raw prefix argument.  If the prefix argument is `nil', then a
  119.      number is read as with `n'.  Requires a number.  Prompt.
  120.      The numeric prefix argument.  (Note that this `p' is lower case.)
  121.      No I/O.
  122.      The raw prefix argument.  (Note that this `P' is upper case.)
  123.      *Note Prefix Command Arguments::.  No I/O.
  124.      Point and the mark, as two numeric arguments, smallest first. 
  125.      This is the only code letter that specifies two successive
  126.      arguments rather than one.  No I/O.
  127.      Arbitrary text, read in the minibuffer and returned as a string
  128.      (*note Text from Minibuffer::.).  Terminate the input with either
  129.      LFD or RET.  (`C-q' may be used to include either of these
  130.      characters in the input.)  Prompt.
  131.      An interned symbol whose name is read in the minibuffer.  Any
  132.      whitespace character terminates the input.  (Use `C-q' to include
  133.      whitespace in the string.)  Other characters that normally
  134.      terminate a symbol (e.g., parentheses and brackets) do not do so
  135.      here.  Prompt.
  136.      A variable declared to be a user option (i.e., satisfying
  137.      `user-variable-p').  *Note High-Level Completion::.  Existing,
  138.      Completion, Prompt.
  139.      A Lisp object specified in printed representation, terminated with
  140.      a LFD or RET.  The object is not evaluated.  *Note Object from
  141.      Minibuffer::.  Prompt.
  142.      A Lisp form is read as with `x', but then evaluated so that its
  143.      value becomes the argument for the command.  Prompt.
  144. File: elisp,  Node: Interactive Examples,  Prev: Interactive Codes,  Up: Defining Commands
  145. Examples of Using `interactive'
  146. -------------------------------
  147.    Here are some examples of `interactive':
  148.      (defun foo1 ()                ; `foo1' takes no arguments,
  149.          (interactive)             ; just moves forward two words.
  150.          (forward-word 2))
  151.           => foo1
  152.      
  153.      (defun foo2 (n)               ; `foo2' takes one argument,
  154.          (interactive "p")         ; which is the numeric prefix.
  155.          (forward-word (* 2 n)))
  156.           => foo2
  157.      
  158.      (defun foo3 (n)               ; `foo3' takes one argument,
  159.          (interactive "nCount:")   ; which is read with the Minibuffer.
  160.          (forward-word (* 2 n)))
  161.           => foo3
  162.      
  163.      (defun three-b (b1 b2 b3)
  164.        "Select three existing buffers (prompting for them in
  165.      the Minibuffer).  Put them into three windows, selecting the
  166.      last one."
  167.          (interactive "bBuffer1:\nbBuffer2:\nbBuffer3:")
  168.          (delete-other-windows)
  169.          (split-window (selected-window) 8)
  170.          (switch-to-buffer b1)
  171.          (other-window 1)
  172.          (split-window (selected-window) 8)
  173.          (switch-to-buffer b2)
  174.          (other-window 1)
  175.          (switch-to-buffer b3))
  176.           => three-b
  177.      (three-b "*scratch*" "declarations.texi" "*mail*")
  178.           => nil
  179. File: elisp,  Node: Interactive Call,  Next: Command Loop Info,  Prev: Defining Commands,  Up: Command Loop
  180. Interactive Call
  181. ================
  182.    After the command loop has translated a key sequence into a
  183. definition, it invokes that definition using the function
  184. `command-execute'.  If the definition is a function that is a command,
  185. `command-execute' calls `call-interactively', which reads the arguments
  186. and calls the command.  You can also call these functions yourself.
  187.  -- Function: commandp OBJECT
  188.      Returns `t' if OBJECT is suitable for calling interactively; that
  189.      is, if OBJECT is a command.  Otherwise, returns `nil'.
  190.      The interactively callable objects include strings (treated as
  191.      keyboard macros), lambda expressions that contain a top-level call
  192.      to `interactive', autoload objects that are declared as interactive
  193.      (non-`nil' fourth argument to `autoload'), and some of the
  194.      primitive functions.
  195.      A symbol is `commandp' if its function definition is `commandp'.
  196.      Keys and keymaps are not commands.  Rather, they are used to look
  197.      up commands (*note Keymaps::.).
  198.      See `documentation' in *Note Accessing Documentation::, for a
  199.      realistic example of using `commandp'.
  200.  -- Function: call-interactively COMMAND &optional RECORD-FLAG
  201.      This function calls the interactively callable function COMMAND,
  202.      reading arguments according to its interactive calling
  203.      specifications. An error is signaled if COMMAND cannot be called
  204.      interactively (i.e., it is not a command).  Note that strings are
  205.      not accepted, even though they are considered commands.
  206.      If RECORD-FLAG is non-`nil', then this command and its arguments
  207.      are unconditionally added to the list `command-history'.
  208.      Otherwise, the command is added only if it uses the minibuffer to
  209.      read an argument.  *Note Command History::.
  210.  -- Function: command-execute COMMAND &optional RECORD-FLAG
  211.      This function executes COMMAND as an editing command.  The
  212.      argument COMMAND must satisfy the `commandp' predicate; i.e., it
  213.      must be an interactively callable function or a string.
  214.      A string as COMMAND is executed with `execute-kbd-macro'.  A
  215.      function is passed to `call-interactively', along with the optional
  216.      RECORD-FLAG.
  217.      A symbol is handled by using its function definition in its place.
  218.       A symbol with an `autoload' definition counts as a command if it
  219.      was declared to stand for an interactively callable function. 
  220.      Such a definition is handled by loading the specified library and
  221.      then rechecking the definition of the symbol.
  222.  -- Command: execute-extended-command PREFIX-ARGUMENT
  223.      This primitive function reads a command name from the minibuffer
  224.      using `completing-read' (*note Completion::.).  Then it uses
  225.      `command-execute' to call the specified command.  Whatever that
  226.      command returns becomes the value of `execute-extended-command'.
  227.      If the command asks for a prefix argument, the value
  228.      PREFIX-ARGUMENT is supplied.  If `execute-extended-command' is
  229.      called interactively, the current raw prefix argument is used for
  230.      PREFIX-ARGUMENT, and thus passed on to whatever command is run.
  231.      `execute-extended-command' is the normal definition of `M-x', so
  232.      it uses the string `M-x ' as a prompt.  (It would be better to
  233.      take the prompt from the characters used to invoke
  234.      `execute-extended-command', but that is painful to implement.)  A
  235.      description of the value of the prefix argument, if any, also
  236.      becomes part of the prompt.
  237.           (execute-extended-command 1)
  238.           ---------- Buffer: Minibuffer ----------
  239.           M-x forward-word RET
  240.           ---------- Buffer: Minibuffer ----------
  241.                => t
  242.  -- Function: interactive-p
  243.      This function returns `t' if the containing function (the one that
  244.      called `interactive-p') was called interactively, with
  245.      `call-interactively'.  (It makes no difference whether
  246.      `call-interactively' was called from Lisp or directly from the
  247.      editor command loop.)  Note that if the containing function was
  248.      called by Lisp evaluation (or with `apply' or `funcall'), then it
  249.      was not called interactively.
  250.      The usual application of `interactive-p' is for deciding whether to
  251.      print an informative message.  As a special exception,
  252.      `interactive-p' returns `nil' whenever a keyboard macro is being
  253.      run.  This is to suppress the informative messages and speed
  254.      execution of the macro.
  255.      For example:
  256.           (defun foo ()
  257.             (interactive)
  258.             (and (interactive-p)
  259.                  (message "foo")))
  260.                => foo
  261.           
  262.           (defun bar ()
  263.             (interactive)
  264.             (setq foobar (list (foo) (interactive-p))))
  265.                => bar
  266.           
  267.           ;; Type `M-x foo'.
  268.                -| foo
  269.           
  270.           ;; Type `M-x bar'.
  271.           ;; This does not print anything.
  272.           
  273.           foobar
  274.                => (nil t)
  275. File: elisp,  Node: Command Loop Info,  Next: Keyboard Input,  Prev: Interactive Call,  Up: Command Loop
  276. Information from the Command Loop
  277. =================================
  278.    The editor command loop sets several Lisp variables to keep status
  279. records for itself and for commands that are run.
  280.  -- Variable: last-command
  281.      This variable records the name of the previous command executed by
  282.      the command loop (the one before the current command).  Normally
  283.      the value is a symbol with a function definition, but this is not
  284.      guaranteed.
  285.      The value is set by copying the value of `this-command' when a
  286.      command returns to the command loop, except when the command
  287.      specifies a prefix argument for the following command.
  288.  -- Variable: this-command
  289.      This variable records the name of the command now being executed by
  290.      editor command loop.  Like `last-command', it is normally a symbol
  291.      with a function definition.
  292.      This variable is set by the command loop just before the command
  293.      is run, and its value is copied into `last-command' when the
  294.      command finishes (unless the command specifies a prefix argument
  295.      for the following command).
  296.      Some commands change the value of this variable during their
  297.      execution, simply as a flag for whatever command runs next.  In
  298.      particular, the functions that kill text set `this-command' to
  299.      `kill-region' so that any kill commands immediately following will
  300.      know to append the killed text to the previous kill.
  301.  -- Function: this-command-keys
  302.      This function returns a string containing the key sequence that
  303.      invoked the present command, plus any previous commands that
  304.      generated the prefix argument for this command.
  305.           (this-command-keys) ;; Now type `C-u C-x C-e'.
  306.                => "^U^X^E"
  307.  -- Variable: last-command-char
  308.      This variable is set to the last character that was typed on the
  309.      terminal and was part of a command.  The principal use of this
  310.      variable is in `self-insert-command', which uses it to decide which
  311.      character to insert.
  312.           last-command-char ;; Now type `C-u C-x C-e'.
  313.                => 5
  314.      The value is 5 because that is the ASCII code for `C-e'.
  315.  -- Variable: echo-keystrokes
  316.      This variable determines how much time should elapse before command
  317.      characters are echoed.  Its value must be an integer, which
  318.      specifies the number of seconds to wait before echoing.  If the
  319.      user types a prefix key (say `C-x') and then delays this many
  320.      seconds before continuing, the key `C-x' is echoed in the echo
  321.      area.  Any subsequent characters in the same command will be
  322.      echoed as well.
  323.      If the value is zero, then command input is not echoed.
  324. File: elisp,  Node: Keyboard Input,  Next: Quitting,  Prev: Command Loop Info,  Up: Command Loop
  325. Keyboard Input
  326. ==============
  327.    The editor command loop reads keyboard input using
  328. `read-key-sequence', which uses `read-char'.  These and other functions
  329. for keyboard input are also available for use in Lisp programs.  See
  330. also `momentary-string-display' in *Note Temporary Displays::, and
  331. `sit-for' in *Note Waiting::.  *Note Terminal Input::, for functions
  332. and variables for controlling terminal input modes and debugging
  333. terminal input.
  334.  -- Function: read-char
  335.      This function reads a character from the command input (either from
  336.      direct keyboard input or from an executing keyboard macro), and
  337.      returns it.
  338.      No message is displayed to indicate that keyboard input is
  339.      expected.  If you want to display a message, call `message' first.
  340.       If `cursor-in-echo-area' is non-`nil', then the cursor moves to
  341.      the echo area, to the end of any message displayed there. 
  342.      Otherwise the cursor does not move.  *Note The Echo Area::.
  343.      In the first example, the user types `1' (which is ASCII code 49).
  344.       The second example shows a keyboard macro definition that calls
  345.      `read-char' from the minibuffer.  `read-char' reads the keyboard
  346.      macro's very next character, which is `1'.  The value of this
  347.      function is displayed in the echo area by the command
  348.      `eval-expression'.
  349.           (read-char)
  350.                => 49
  351.           
  352.           (symbol-function 'foo)
  353.                => "^[^[(read-char)^M1"
  354.           (execute-kbd-macro foo)
  355.                -| 49
  356.                => nil
  357.  -- Function: read-quoted-char &optional PROMPT
  358.      This function is like `read-char', except that if the first
  359.      character read is an octal digit (0-7), it reads up to two more
  360.      octal digits (but stopping if a non-octal digit is found) and
  361.      returns the character represented by those digits as an octal
  362.      number.
  363.      Quitting is suppressed when the first character is read, so that
  364.      the user can enter a `C-g'.  *Note Quitting::.
  365.      If PROMPT is supplied, it specifies a string for prompting the
  366.      user.  The prompt string is always printed in the echo area and
  367.      followed by a single `-'.
  368.      In the following example, the user types in the octal number 177
  369.      (which is 127 in decimal).
  370.           (read-quoted-char "What character")
  371.           
  372.           ---------- Echo Area ----------
  373.           What character-`177'
  374.           ---------- Echo Area ----------
  375.           
  376.                => 127
  377.  -- Function: read-key-sequence PROMPT
  378.      This function reads a key sequence and returns it as a string.  It
  379.      keeps reading characters until it has accumulated a full key
  380.      sequence; that is, enough characters to specify a non-prefix
  381.      command using the current local and global keymaps. 
  382.      `read-key-sequence' is used by the command loop to read command
  383.      input.
  384.      If an input character is an upper case letter and has no
  385.      definition, but the lower case equivalent is defined, then the
  386.      character is converted to lower case.  Note that `lookup-key' does
  387.      not perform case conversion in this way.
  388.      Quitting is suppressed inside `read-key-sequence'.  In other words,
  389.      a `C-g' typed while reading with this function is treated like any
  390.      other character, and `quit-flag' is not set.  *Note Quitting::.
  391.      The argument PROMPT is either a string to be displayed in the echo
  392.      area as a prompt, or `nil', meaning that no prompt is displayed.
  393.      In the example below, the prompt `?' is displayed in the echo area,
  394.      and the user types `C-x C-f'.
  395.           (read-key-sequence "?")
  396.           
  397.           ---------- Echo Area ----------
  398.           ?`C-x C-f'
  399.           ---------- Echo Area ----------
  400.           
  401.                => "^X^F"
  402.  -- Variable: unread-command-char
  403.      This variable holds a character waiting to be read as the next
  404.      input from the command input stream, or to the integer -1 if no
  405.      character is waiting.  The variable is used because in some cases
  406.      an input function reads a character and then decides not to use it.
  407.      Storing the character in this variable causes it to be processed
  408.      normally by the command loop or when `read-char' is next called.
  409.      For example, the function that governs prefix arguments reads any
  410.      number of digits.  When it finds a non-digit character, it must
  411.      unread the character so that it becomes input for the next
  412.      command.  Likewise, incremental search uses this feature to unread
  413.      a control character used to terminate the search.
  414.  -- Function: input-pending-p
  415.      This function determines whether command input is currently
  416.      available. It returns immediately, with value `t' if there is
  417.      input, `nil' otherwise.  On rare occasions it may return `t' when
  418.      no input is available.
  419.  -- Variable: last-input-char
  420.      This variable records the last terminal input character read,
  421.      whether as part of a command or explicitly by a Lisp program.
  422.      In the example below, a character is read (the character `1',
  423.      ASCII code 49).  It becomes the value of `last-input-char', while
  424.      `C-e' (from the `C-x C-e' command used to evaluate this
  425.      expression) remains the value of `last-command-char'.
  426.           (progn (print (read-char))
  427.                  (print last-command-char)
  428.                  last-input-char)
  429.                -| 49
  430.                -| 5
  431.                => 49
  432.  -- Function: discard-input
  433.      This function discards the contents of the terminal input buffer
  434.      and cancels any keyboard macro that might be in the process of
  435.      definition. It returns `nil'.
  436.      In the following example, the user may type a number of characters
  437.      right after starting the evaluation of the form.  After the
  438.      `sleep-for' finishes sleeping, any characters that have been typed
  439.      are discarded.
  440.           (progn (sleep-for 2)
  441.             (discard-input))
  442.                => nil
  443. File: elisp,  Node: Quitting,  Next: Prefix Command Arguments,  Prev: Keyboard Input,  Up: Command Loop
  444. Quitting
  445. ========
  446.    Typing `C-g' while the command loop has run a Lisp function causes
  447. Emacs to "quit" whatever it is doing.  This means that control returns
  448. to the innermost active command loop.
  449.    Typing `C-g' while the command loop is waiting for keyboard input
  450. does not cause a quit; it acts as an ordinary input character.  In the
  451. simplest case, you cannot tell the difference, because `C-g' normally
  452. runs the command `keyboard-quit', whose effect is to quit. However,
  453. when `C-g' follows a prefix key, the result is an undefined key.  The
  454. effect is to cancel the prefix key as well as any prefix argument.
  455.    In the minibuffer, `C-g' has a different definition: it aborts out
  456. of the minibuffer.  This means, in effect, that it exits the minibuffer
  457. and then quits.  (Simply quitting would return to the command loop
  458. *within* the minibuffer.)  The reason why `C-g' does not quit directly
  459. when the command reader is reading input is so that its meaning can be
  460. redefined in the minibuffer in this way.  `C-g' following a prefix key
  461. is not redefined in the minibuffer, and it has its normal effect of
  462. canceling the prefix key and prefix argument.  This too would not be
  463. possible if `C-g' quit directly.
  464.    `C-g' causes a quit by setting the variable `quit-flag' to a
  465. non-`nil' value.  Emacs checks this variable at appropriate times and
  466. quits if it is not `nil'.  Setting `quit-flag' non-`nil' in any way
  467. thus causes a quit.
  468.    At the level of C code, quits cannot happen just anywhere; only at
  469. the special places which check `quit-flag'.  The reason for this is
  470. that quitting at other places might leave an inconsistency in Emacs's
  471. internal state.  Because quitting is delayed until a safe place,
  472. quitting cannot make Emacs crash.
  473.    Certain functions such as `read-key-sequence' or `read-quoted-char'
  474. prevent quitting entirely even though they wait for input.  Instead of
  475. quitting, `C-g' serves as the requested input.  In the case of
  476. `read-key-sequence', this serves to bring about the special behavior of
  477. `C-g' in the command loop.  In the case of `read-quoted-char', this is
  478. so that `C-q' can be used to quote a `C-g'.
  479.    You can prevent quitting for a portion of a Lisp function by binding
  480. the variable `inhibit-quit' to a non-`nil' value.  Then, although `C-g'
  481. still sets `quit-flag' to `t' as usual, the usual result of this--a
  482. quit--is prevented.  Eventually, `inhibit-quit' will become `nil'
  483. again, such as when its binding is unwound at the end of a `let' form. 
  484. At that time, if `quit-flag' is still non-`nil', the requested quit
  485. happens immediately.  This behavior is ideal for a "critical section",
  486. where you wish to make sure that quitting does not happen within that
  487. part of the program.
  488.    In some functions (such as `read-quoted-char'), `C-g' is handled in
  489. a special way which does not involve quitting.  This is done by reading
  490. the input with `inhibit-quit' bound to `t' and setting `quit-flag' to
  491. `nil' before `inhibit-quit' becomes `nil' again.  This excerpt from the
  492. definition of `read-quoted-char' shows how this is done; it also shows
  493. that normal quitting is permitted after the first character of input.
  494.      (defun read-quoted-char (&optional prompt)
  495.        "...DOCUMENTATION..."
  496.        (let ((count 0) (code 0) char)
  497.          (while (< count 3)
  498.            (let ((inhibit-quit (zerop count))
  499.                  (help-form nil))
  500.              (and prompt (message "%s-" prompt))
  501.              (setq char (read-char))
  502.              (if inhibit-quit (setq quit-flag nil)))
  503.            ...)
  504.          (logand 255 code)))
  505.  -- Variable: quit-flag
  506.      If this variable is non-`nil', then Emacs quits immediately,
  507.      unless `inhibit-quit' is non-`nil'.  Typing `C-g' sets `quit-flag'
  508.      non-`nil', regardless of `inhibit-quit'.
  509.  -- Variable: inhibit-quit
  510.      This variable determines whether Emacs should quit when `quit-flag'
  511.      is set to a value other than `nil'.  If `inhibit-quit' is
  512.      non-`nil', then `quit-flag' has no special effect.
  513.  -- Command: keyboard-quit
  514.      This function signals the `quit' condition with `(signal 'quit
  515.      nil)'.  This is the same thing that quitting does.  (See `signal'
  516.      in *Note Errors::.)
  517.    You can specify a character other than `C-g' to use for quitting.
  518. See the function `set-input-mode' in *Note Terminal Input::.
  519. File: elisp,  Node: Prefix Command Arguments,  Next: Recursive Editing,  Prev: Quitting,  Up: Command Loop
  520. Prefix Command Arguments
  521. ========================
  522.    Most Emacs commands can use a "prefix argument", a number specified
  523. before the command itself.  (Don't confuse prefix arguments with prefix
  524. keys.)  The prefix argument is represented by a value that is always
  525. available (though it may be `nil', meaning there is no prefix
  526. argument).  Each command may use the prefix argument or ignore it.
  527.    There are two representations of the prefix argument: "raw" and
  528. "numeric".  The editor command loop uses the raw representation
  529. internally, and so do the Lisp variables that store the information, but
  530. commands can request either representation.
  531.    Here are the possible values of a raw prefix argument:
  532.    * `nil', meaning there is no prefix argument.  Its numeric value is
  533.      1, but numerous commands make a distinction between `nil' and the
  534.      integer 1.
  535.    * An integer, which stands for itself.
  536.    * A list of one element, which is an integer.  This form of prefix
  537.      argument results from one or a succession of `C-u''s with no
  538.      digits.  The numeric value is the integer in the list, but some
  539.      commands make a distinction between such a list and an integer
  540.      alone.
  541.    * The symbol `-'.  This indicates that `M--' or `C-u -' was typed,
  542.      without following digits.  The equivalent numeric value is -1, but
  543.      some commands make a distinction between the integer -1 and the
  544.      symbol `-'.
  545.    The various possibilities may be illustrated by calling the following
  546. function with various prefixes:
  547.      (defun print-prefix (arg)
  548.        "Print the value of the raw prefix arg at point."
  549.        (interactive "P")
  550.        (message "%s" arg))
  551. Here are the results of calling `print-prefix' with various raw prefix
  552. arguments:
  553.              M-x print-prefix  -| nil
  554.      
  555.      C-u     M-x print-prefix  -| (4)
  556.      
  557.      C-u C-u M-x print-prefix  -| (16)
  558.      
  559.      C-u 3   M-x print-prefix  -| 3
  560.      
  561.      M-3     M-x print-prefix  -| 3      ; (Same as `C-u 3'.)
  562.      
  563.      C-u -   M-x print-prefix  -| -
  564.      
  565.      M- -    M-x print-prefix  -| -      ; (Same as `C-u -'.)
  566.      
  567.      C-u -7  M-x print-prefix  -| -7
  568.      
  569.      M- -7   M-x print-prefix  -| -7     ; (Same as `C-u -7'.)
  570.    There are two variables used to store the prefix argument:
  571. `prefix-arg' and `current-prefix-arg'.  Commands such as
  572. `universal-argument' that set up prefix arguments for other commands
  573. store them in `prefix-arg'.  In contrast, `current-prefix-arg' conveys
  574. the prefix argument to the current command, so setting it has no effect
  575. on the prefix arguments for future commands.
  576.    Normally, commands specify which representation to use for the prefix
  577. argument, either numeric or raw, in the `interactive' declaration.
  578. (*Note Interactive Call::.)  Alternatively, functions may look at the
  579. value of the prefix argument directly in the variable
  580. `current-prefix-arg', but this is less clean.
  581.    Don't call `universal-argument', `digit-argument', or
  582. `negative-argument' unless you intend to let the user enter the prefix
  583. argument for the *next* command.
  584.  -- Command: universal-argument
  585.      This command reads input and specifies a prefix argument for the
  586.      following command.  Don't call this command yourself unless you
  587.      know what you are doing.
  588.  -- Command: digit-argument ARG
  589.      This command adds to the prefix argument for the following
  590.      command.  The argument ARG is the raw prefix argument as it was
  591.      before this command; it is used to compute the updated prefix
  592.      argument.  Don't call this command yourself unless you know what
  593.      you are doing.
  594.  -- Command: negative-argument ARG
  595.      This command adds to the numeric argument for the next command. 
  596.      The argument ARG is the raw prefix argument as it was before this
  597.      command; its value is negated to form the new prefix argument. 
  598.      Don't call this command yourself unless you know what you are
  599.      doing.
  600.  -- Function: prefix-numeric-value ARG
  601.      This function returns the numeric meaning of a valid raw prefix
  602.      argument value, ARG.  The argument may be a symbol, a number, or a
  603.      list. If it is `nil', the value 1 is returned; if it is any other
  604.      symbol, the value -1 is returned.  If it is a number, that number
  605.      is returned; if it is a list, the CAR of that list (which should
  606.      be a number) is returned.
  607.  -- Variable: current-prefix-arg
  608.      This variable is the value of the raw prefix argument for the
  609.      *current* command.  Commands may examine it directly, but the usual
  610.      way to access it is with `(interactive "P")'.
  611.  -- Variable: prefix-arg
  612.      The value of this variable is the raw prefix argument for the
  613.      *next* editing command.  Commands that specify prefix arguments for
  614.      the following command work by setting this variable.
  615. File: elisp,  Node: Recursive Editing,  Next: Disabling Commands,  Prev: Prefix Command Arguments,  Up: Command Loop
  616. Recursive Editing
  617. =================
  618.    The Emacs command loop is entered automatically when Emacs starts up.
  619. This top-level invocation of the command loop is never exited until the
  620. Emacs is killed.  Lisp programs can also invoke the command loop.  Since
  621. this makes more than one activation of the command loop, we call it
  622. "recursive editing".  A recursive editing level has the effect of
  623. suspending whatever command invoked it and permitting the user to do
  624. arbitrary editing before resuming that command.
  625.    The commands available during recursive editing are the same ones
  626. available in the top-level editing loop and defined in the keymaps.
  627. Only a few special commands exit the recursive editing level; the others
  628. return to the recursive editing level when finished.  (The special
  629. commands for exiting are always available, but do nothing when recursive
  630. editing is not in progress.)
  631.    All command loops, including recursive ones, set up all-purpose error
  632. handlers so that an error in a command run from the command loop will
  633. not exit the loop.
  634.    Minibuffer input is a special kind of recursive editing.  It has a
  635. few special wrinkles, such as enabling display of the minibuffer and the
  636. minibuffer window, but fewer than you might suppose.  Certain keys
  637. behave differently in the minibuffer, but that is only because of the
  638. minibuffer's local map; if you switch windows, you get the usual Emacs
  639. commands.
  640.    To invoke a recursive editing level, call the function
  641. `recursive-edit'.  This function contains the command loop; it also
  642. contains a call to `catch' with tag `exit', which makes it possible to
  643. exit the recursive editing level by throwing to `exit' (*note Catch and
  644. Throw::.).  If you throw a value other than `t', then `recursive-edit'
  645. returns normally to the function that called it.  The command `C-M-c'
  646. (`exit-recursive-edit') does this. Throwing a `t' value causes
  647. `recursive-edit' to quit, so that control returns to the command loop
  648. one level up.  This is called "aborting", and is done by `C-]'
  649. (`abort-recursive-edit').
  650.    Most applications should not use recursive editing, except as part of
  651. using the minibuffer.  Usually it is more convenient for the user if you
  652. change the major mode of the current buffer temporarily to a special
  653. major mode, which has a command to go back to the previous mode.  (This
  654. technique is used by the `w' command in Rmail.)  Or, if you wish to
  655. give the user different text to edit "recursively", create and select a
  656. new buffer in a special mode.  In this mode, define a command to
  657. complete the processing and go back to the previous buffer.  (The `m'
  658. command in Rmail does this.)
  659.    Recursive edits are useful in debugging.  You can insert a call to
  660. `debug' into a function definition as a sort of breakpoint, so that you
  661. can look around when the function gets there.  `debug' invokes a
  662. recursive edit but also provides the other features of the debugger.
  663.    Recursive editing levels are also used when you type `C-r' in
  664. `query-replace' or use `C-x q' (`kbd-macro-query').
  665.  -- Function: recursive-edit
  666.      This function invokes the editor command loop.  It is called
  667.      automatically by the initialization of Emacs, to let the user begin
  668.      editing.  When called from a Lisp program, it enters a recursive
  669.      editing level.
  670.      In the following example, the function `simple-rec' first advances
  671.      point one word, then enters a recursive edit, printing out a
  672.      message in the echo area.  The user can then do any editing
  673.      desired, and then type `C-M-c' to exit and continue executing
  674.      `simple-rec'.
  675.           (defun simple-rec ()
  676.             (forward-word 1)
  677.             (message "Recursive edit in progress.")
  678.             (recursive-edit)
  679.             (forward-word 1))
  680.                => simple-rec
  681.           (simple-rec)
  682.                => nil
  683.  -- Command: exit-recursive-edit
  684.      This function exits from the innermost recursive edit (including
  685.      minibuffer input).  Its definition is effectively `(throw 'exit
  686.      nil)'.
  687.  -- Command: abort-recursive-edit
  688.      This function aborts the command that requested the innermost
  689.      recursive edit (including minibuffer input), by signaling `quit'
  690.      after exiting the recursive edit.  Its definition is effectively
  691.      `(throw 'exit t)'.  *Note Quitting::.
  692.  -- Command: top-level
  693.      This function exits all recursive editing levels; it does not
  694.      return a value, as it jumps completely out of any computation
  695.      directly back to the main command loop.
  696.  -- Function: recursion-depth
  697.      This function returns the current depth of recursive edits.  When
  698.      no recursive edit is active, it returns 0.
  699. File: elisp,  Node: Disabling Commands,  Next: Command History,  Prev: Recursive Editing,  Up: Command Loop
  700. Disabling Commands
  701. ==================
  702.    "Disabling a command" marks the command as requiring user
  703. confirmation before it can be executed.  Disabling is used for commands
  704. which might be confusing to beginning users, to prevent them from using
  705. the commands by accident.
  706.    The low-level mechanism for disabling a command is to put a
  707. non-`nil' `disabled' property on the Lisp symbol for the command. 
  708. These properties are normally set up by the user's `.emacs' file with
  709. Lisp expressions such as this:
  710.      (put 'upcase-region 'disabled t)
  711. For a few commands, these properties are present by default and may be
  712. removed by the `.emacs' file.
  713.    If the value of the `disabled' property is a string, that string is
  714. included in the message printed when the command is used:
  715.      (put 'delete-region 'disabled
  716.           "Text deleted this way cannot be yanked back!\n")
  717.    *Note Disabling: (emacs)Disabling, for the details on what happens
  718. when a disabled command is invoked interactively. Disabling a command
  719. has no effect on calling it as a function from Lisp programs.
  720.  -- Command: enable-command COMMAND
  721.      Allow COMMAND to be executed without special confirmation from now
  722.      on.  The user's `.emacs' file is optionally altered so that this
  723.      will apply to future sessions.
  724.  -- Command: disable-command COMMAND
  725.      Require special confirmation to execute COMMAND from now on.  The
  726.      user's `.emacs' file is optionally altered so that this will apply
  727.      to future sessions.
  728.  -- Variable: disabled-command-hook
  729.      The value of this variable is a function to be called instead of
  730.      any command that is disabled (i.e., that has a non-`nil' disabled
  731.      property).  By default, the value of `disabled-command-hook' is a
  732.      function defined to ask the user whether to proceed.
  733. File: elisp,  Node: Command History,  Next: Keyboard Macros,  Prev: Disabling Commands,  Up: Command Loop
  734. Command History
  735. ===============
  736.    The command loop keeps a history of the complex commands that have
  737. been executed, to make it convenient to repeat these commands.  A
  738. "complex command" is one for which the interactive argument reading
  739. uses the minibuffer.  This includes any `M-x' command, any `M-ESC'
  740. command, and any command whose `interactive' specification reads an
  741. argument from the minibuffer.  Explicit use of the minibuffer during
  742. the execution of the command itself does not cause the command to be
  743. considered complex.
  744.  -- Variable: command-history
  745.      This variable's value is a list of recent complex commands, each
  746.      represented as a form to evaluate.  It continues to accumulate all
  747.      complex commands for the duration of the editing session, but all
  748.      but the first (most recent) thirty elements are deleted when a
  749.      garbage collection takes place (*note Garbage Collection::.).
  750.           command-history
  751.           => ((switch-to-buffer "chistory.texi")
  752.               (describe-key "^X^[")
  753.               (visit-tags-table "~/emacs/src/")
  754.               (find-tag "repeat-complex-command"))
  755.    There are a number of commands and even two entire modes devoted to
  756. facilitating the editing and recall of previous commands.  The commands
  757. `repeat-complex-command', and `list-command-history' are described in
  758. the user manual (*note Repetition: (emacs)Repetition.).
  759.  -- Variable: repeat-complex-command-map
  760.      The value of this variable is a sparse keymap used by the
  761.      minibuffer inside of `read-complex-command'.
  762. File: elisp,  Node: Keyboard Macros,  Prev: Command History,  Up: Command Loop
  763. Keyboard Macros
  764. ===============
  765.    A "keyboard macro" is a canned sequence of keystrokes that can be
  766. considered a command and made the definition of a key.  Don't confuse
  767. keyboard macros with Lisp macros (*note Macros::.).
  768.  -- Function: execute-kbd-macro MACRO &optional COUNT
  769.      This function executes MACRO as a string of editor commands.  If
  770.      MACRO is a string, then the characters in that string are executed
  771.      exactly as if they had been typed as command input.
  772.      If MACRO is a symbol, then its function definition is used in
  773.      place of MACRO.  If that is another symbol, this process repeats.
  774.      Eventually the result should be a string.  If the result is
  775.      neither a symbol nor a string, an error is signaled.
  776.      The argument COUNT is a repeat count; MACRO is executed that many
  777.      times.  If COUNT is omitted or `nil', MACRO is executed once.  If
  778.      it is 0, MACRO is executed over and over until it encounters an
  779.      error or a failing search.
  780.  -- Variable: last-kbd-macro
  781.      This variable is the definition of the most recently defined
  782.      keyboard macro.  Its value is a string or `nil'.
  783.  -- Variable: executing-macro
  784.      This variable contains the string that defines the keyboard macro
  785.      that is currently executing.  It is `nil' if no macro is currently
  786.      executing.
  787.  -- Variable: defining-kbd-macro
  788.      This variable indicates whether a keyboard macro is being defined.
  789.       It is set to `t' by `start-kbd-macro', and `nil' by
  790.      `end-kbd-macro'.  It is not hard to use this variable to make a
  791.      command behave differently when run from a keyboard macro (perhaps
  792.      indirectly by calling `interactive-p').  However, do not set this
  793.      variable yourself.
  794.    The user-level commands for defining, running and editing keyboard
  795. macros include `call-last-kbd-macro', `insert-kbd-macro',
  796. `start-kbd-macro', `end-kbd-macro', `kbd-macro-query', and
  797. `name-last-kbd-macro'.  They are described in the user's manual (*note
  798. Keyboard Macros: (emacs)Keyboard Macros.).
  799. File: elisp,  Node: Keymaps,  Next: Modes,  Prev: Command Loop,  Up: Top
  800. Keymaps
  801. *******
  802.    The bindings between keyboard input and commands are recorded in data
  803. structures called "keymaps".  Each binding in a keymap associates (or
  804. "binds") an individual character either with another keymap or with a
  805. command.  When a character is bound to a keymap, that keymap is used to
  806. look up the next character typed; this continues until a command is
  807. found.  This process is called "key lookup".
  808. * Menu:
  809. * Keymap Terms::        Definitions of terms pertaining to keymaps.
  810. * Creating Keymaps::    Functions to create and copy keymaps.
  811. * Key Lookup::                  How extracting elements from keymaps works.
  812. * Functions for Key Lookup::    How to request key lookup.
  813. * Prefix Keys::                 Defining a key with a keymap as its definition.
  814. * Global and Local Keymaps::    Each buffer has a local keymap
  815.                                    to override the standard (global) bindings.
  816. * Changing Key Bindings::       Redefining a key in a keymap.
  817. * Key Binding Commands::        Interactive interfaces for redefining keys.
  818. * Scanning Keymaps::            Looking through all keymaps, for printing help.
  819. File: elisp,  Node: Keymap Terms,  Next: Creating Keymaps,  Prev: Keymaps,  Up: Keymaps
  820. Keymaps: Terminology
  821. ====================
  822.    A "keymap" is a table mapping characters to definitions (which can
  823. be any Lisp objects, though only certain types are meaningful for
  824. execution by the command loop).  Given a character and a keymap, Emacs
  825. can get the character's definition.
  826.    A sequence of keyboard input characters that form a unit is called a
  827. "key sequence", or "key" for short.  A sequence of one character is
  828. always a key sequence, and so are some multicharacter sequences.
  829.    A keymap determines a binding or definition for any key sequence.  If
  830. the key sequence is a single character, its binding is the definition of
  831. the character in the keymap.  The binding of a multicharacter key
  832. sequence is found by an iterative process: the binding of the first
  833. character is found, and must be a keymap; then the second character's
  834. binding is found in that keymap, and so on until all the characters in
  835. the key sequence are used up.
  836.    If the binding of a key sequence is a keymap, we call the key
  837. sequence a "prefix key".  Otherwise, we call it a "complete key"
  838. (because no more characters can be added to it).  If the binding is
  839. `nil', we call the key "undefined".  Examples of prefix keys are `C-c',
  840. `C-x', and `C-x 4'.  Examples of defined complete keys are `X', RET,
  841. and `C-x 4 C-f'.  Examples of undefined complete keys are `C-x C-g',
  842. and `C-c 3'.  *Note Prefix Keys::, for more details.
  843.    The rule for finding the binding of a key sequence assumes that the
  844. intermediate bindings (found for the characters before the last) are all
  845. keymaps; if this is not so, the sequence of characters does not form a
  846. unit--it is not really a key sequence.  In other words, removing one or
  847. more characters from the end of any key must always yield a prefix key.
  848. For example, `C-f C-f' is not a key; `C-f' is not a prefix key, so a
  849. longer sequence starting with `C-f' cannot be a key.  Note that the set
  850. of possible multicharacter key sequences depends on the bindings for
  851. prefix keys; therefore, it can be different for different keymaps, and
  852. can change when bindings are changed.  However, a one-character
  853. sequence is always a key sequence, because it does not depend on any
  854. prefix keys for its validity.
  855.    At any time, two primary keymaps are in use for finding key bindings:
  856. the "global map", which is shared by all buffers, and the "local
  857. keymap", which is usually associated with a specific major mode.  The
  858. local keymap bindings shadow (i.e., take precedence over) the
  859. corresponding global bindings.  *Note Global and Local Keymaps::, for
  860. details.
  861. File: elisp,  Node: Creating Keymaps,  Next: Key Lookup,  Prev: Keymap Terms,  Up: Keymaps
  862. Creating Keymaps
  863. ================
  864.    A keymap can be represented as one of two kinds of Lisp object: a
  865. vector or a list.  A "full keymap" is a vector of length 128.  The
  866. binding for a character in such a keymap is found by indexing into the
  867. vector with the character as the index.
  868.    A "sparse keymap" is a list whose CAR is the symbol `keymap', and
  869. whose remaining elements are cons cells of the form `(CHAR . BINDING)'.
  870.  It is called a sparse keymap because it stores only the entries which
  871. are significant.  Use a sparse keymap when you expect only a few
  872. entries.  (`define-key' automatically creates sparse keymaps for
  873. intermediate keymaps.)
  874.    Keymaps record directly only character codes less than 128; they are
  875. unable to handle directly the META characters, whose codes are from 128
  876. to 255.  Instead, META characters are regarded for purposes of key
  877. lookup as sequences of two characters, the first of which is ESC (the
  878. usual value of `meta-prefix-char').  Thus, the key `M-a' is really
  879. represented as `ESC a', and its global binding is found at the slot for
  880. `a' in `esc-map'.
  881.    Here as an example is the local keymap for Lisp mode, a sparse
  882. keymap. It defines `C-c C-l' as the `run-lisp' command, `M-C-q' as
  883. `indent-sexp', and `M-C-x' as `lisp-send-defun'.
  884.      lisp-mode-map
  885.      =>
  886.      (keymap
  887.       (9 . lisp-indent-line)                 ; TAB
  888.       (127 . backward-delete-char-untabify)  ; DEL
  889.       (3 keymap
  890.          (12 . run-lisp))                    ; `C-c C-l'
  891.       (27 keymap
  892.           (17 . indent-sexp)                 ; `M-C-q', treated as `ESC C-q'
  893.           (24 . lisp-send-defun)))           ; `M-C-x', treated as `ESC C-x'
  894.  -- Function: keymapp OBJECT
  895.      This function returns `t' if OBJECT is a keymap, `nil' otherwise. 
  896.      A keymap is either a vector of length 128, or a list with the form
  897.      `(keymap PAIRS...)', where PAIRS stands for a series of
  898.      associations, cons cells of the form `(CHAR . BINDING)'.
  899.           (keymapp '(keymap))
  900.               => t
  901.           (keymapp (current-global-map))
  902.               => t
  903.  -- Function: make-keymap
  904.      This function creates and returns a new full keymap (i.e., a vector
  905.      of length 128).  All entries in the keymap are `nil', which means
  906.      that no characters are defined.
  907.           (make-keymap)
  908.               => [nil nil nil ... nil nil]
  909.  -- Function: make-sparse-keymap
  910.      This function creates and returns a new sparse keymap with no
  911.      entries. In this keymap, no characters are defined.
  912.           (make-sparse-keymap)
  913.               => (keymap)
  914.  -- Function: copy-keymap KEYMAP
  915.      This function returns a copy of KEYMAP.  Any keymaps which appear
  916.      directly as bindings in KEYMAP are also copied recursively, and so
  917.      on to any number of levels.  However, recursive copying does not
  918.      take place when the definition of a character is a symbol whose
  919.      function definition is a keymap; the same symbol appears in the
  920.      new copy.
  921.           (setq map (copy-keymap (current-local-map)))
  922.           => (keymap
  923.                (27 keymap         ; (This implements META characters.)
  924.                  (83 . center-paragraph)
  925.                  (115 . center-line))
  926.                (9 . tab-to-tab-stop))
  927.           
  928.           (eq map (current-local-map))
  929.               => nil
  930.           (equal map (current-local-map))
  931.               => t
  932.