Next: , Previous: , Up: Command Loop   [Contents][Index]


25.4 Information from the Command Loop

The editor command loop sets several Lisp variables to keep status records for itself and for commands that are run.

Variable: last-command

This variable records the name of the previous command executed by the command loop (the one before the current command). Normally the value is a symbol with a function definition, but this is not guaranteed.

The value is copied from this-command when a command returns to the command loop, except when the command specifies a prefix argument for the following command.

Variable: this-command

This variable records the name of the command now being executed by the editor command loop. Like last-command, it is normally a symbol with a function definition.

The command loop sets this variable just before running a command, and copies its value into last-command when the command finishes (unless the command specifies a prefix argument for the following command).

Some commands set this variable during their execution, as a flag for whatever command runs next. In particular, the functions for killing text set this-command to kill-region so that any kill commands immediately following will know to append the killed text to the previous kill.

If you do not want a particular command to be recognized as the previous command in the case where it got an error, you must code that command to prevent this. One way is to set this-command to t at the beginning of the command, and set this-command back to its proper value at the end, like this:

(defun foo (args…)
  (interactive …)
  (let ((old-this-command this-command))
    (setq this-command t)
    …do the work…
    (setq this-command old-this-command)))
Function: this-command-keys

This function returns a vector containing the key and mouse events that invoked the present command, plus any previous commands that generated the prefix argument for this command.

Note: this is not the same as in FSF Emacs, which can return a string. See Events.

This function copies the vector and the events; it is safe to keep and modify them.

(this-command-keys)
;; Now use C-u C-x C-e to evaluate that.
     ⇒ [#<keypress-event control-U> #<keypress-event control-X> #<keypress-event control-E>]
Variable: last-command-event

This variable is set to the last input event that was read by the command loop as part of a command. The principal use of this variable is in self-insert-command, which uses it to decide which character to insert.

This variable is off limits: you may not set its value or modify the event that is its value, as it is destructively modified by read-key-sequence. If you want to keep a pointer to this value, you must use copy-event.

Note: This variable is an alias for last-command-char in FSF Emacs.

last-command-event
;; Now type C-u C-x C-e.
     ⇒ #<keypress-event control-E>
Variable: last-command-char

If the value of last-command-event is a keyboard event, then this is the nearest character equivalent to it (or nil if there is no character equivalent). last-command-char is the character that self-insert-command will insert in the buffer.

Remember that there is not a one-to-one mapping between keyboard events and SXEmacs characters: many keyboard events have no corresponding character, and when the Mule feature is available, most characters can not be input on standard keyboards, except possibly with help from an input method.

So writing code that examines this variable to determine what key has been typed is bad practice, unless you are certain that it will be one of a small set of characters.

This variable exists for compatibility with Emacs version 18.

last-command-char
;; Now use C-u C-x C-e to evaluate that.
     ⇒ ?\^E
Variable: current-mouse-event

This variable holds the mouse-button event which invoked this command, or nil. This is what (interactive "e") returns.

Variable: echo-keystrokes

This variable determines how much time should elapse before command characters echo. Its value must be an integer, which specifies the number of seconds to wait before echoing. If the user types a prefix key (say C-x) and then delays this many seconds before continuing, the key C-x is echoed in the echo area. Any subsequent characters in the same command will be echoed as well.

If the value is zero, then command input is not echoed.


Next: , Previous: , Up: Command Loop   [Contents][Index]