22.5 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. With the exception of this-command and last-command it’s generally a bad idea to change any of these variables in a Lisp program.

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 has specified a prefix argument for the following command.

This variable is always local to the current terminal and cannot be buffer-local. See Multiple Terminals.

Variable: real-last-command

This variable is set up by Emacs just like last-command, but never altered by Lisp programs.

Variable: last-repeatable-command

This variable stores the most recently executed command that was not part of an input event. This is the command repeat will try to repeat, See Repeating in The GNU Emacs Manual.

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 specified 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)))

We do not bind this-command with let because that would restore the old value in case of error—a feature of let which in this case does precisely what we want to avoid.

Variable: this-original-command

This has the same value as this-command except when command remapping occurs (see Remapping Commands). In that case, this-command gives the command actually run (the result of remapping), and this-original-command gives the command that was specified to run but remapped into another command.

Variable: current-minibuffer-command

This has the same value as this-command, but is bound recursively when entering a minibuffer. This variable can be used from minibuffer hooks and the like to determine what command opened the current minibuffer session.

Function: this-command-keys

This function returns a string or vector containing the key sequence that invoked the present command. Any events read by the command using read-event without a timeout get tacked on to the end.

However, if the command has called read-key-sequence, it returns the last read key sequence. See Key Sequence Input. The value is a string if all events in the sequence were characters that fit in a string. See Input Events.

(this-command-keys)
;; Now use C-u C-x C-e to evaluate that.
     ⇒ "^X^E"
Function: this-command-keys-vector

Like this-command-keys, except that it always returns the events in a vector, so you don’t need to deal with the complexities of storing input events in a string (see Putting Keyboard Events in Strings).

Function: clear-this-command-keys &optional keep-record

This function empties out the table of events for this-command-keys to return. Unless keep-record is non-nil, it also empties the records that the function recent-keys (see Recording Input) will subsequently return. This is useful after reading a password, to prevent the password from echoing inadvertently as part of the next command in certain cases.

Variable: last-nonmenu-event

This variable holds the last input event read as part of a key sequence, not counting events resulting from mouse menus.

One use of this variable is for telling x-popup-menu where to pop up a menu. It is also used internally by y-or-n-p (see Yes-or-No Queries).

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, and in post-self-insert-hook (see User-Level Insertion Commands), which uses it to access the character that was just inserted.

last-command-event
;; Now use C-u C-x C-e to evaluate that.
     ⇒ 5

The value is 5 because that is the ASCII code for C-e.

Variable: last-event-frame

This variable records which frame the last input event was directed to. Usually this is the frame that was selected when the event was generated, but if that frame has redirected input focus to another frame, the value is the frame to which the event was redirected. See Input Focus.

If the last event came from a keyboard macro, the value is macro.

Input events must come from somewhere; sometimes, that is a keyboard macro, a signal, or ‘unread-command-events’, but it is usually a physical input device connected to a computer that is controlled by the user. Those devices are referred to as input devices, and Emacs associates each input event with the input device from which it originated. They are identified by a name that is unique to each input device.

The ability to determine the precise input device used depends on the details of each system. When that information is unavailable, Emacs reports keyboard events as originating from the ‘"Virtual core keyboard"’, and other events as originating from the ‘"Virtual core pointer"’. (These values are used on every platform because the X server reports them when detailed device information is not known.)

Variable: last-event-device

This variable records the name of the input device from which the last input event read was generated. It is nil if no such device exists, i.e., the last input event was read from unread-command-events, or it came from a keyboard macro.

When the X Input Extension is being used on X Windows, the device name is a string that is unique to each physical keyboard, pointing device and touchscreen attached to the X server. Otherwise, it is either the string ‘"Virtual core pointer"’ or ‘"Virtual core keyboard"’, depending on whether the event was generated by a pointing device (such as a mouse) or a keyboard.

Function: device-class frame name

There are various different types of devices, which can be determined from their names. This function can be used to determined the correct type of the device name for an event originating from frame.

The return value is one of the following symbols (“device classes”):

core-keyboard

The core keyboard; this is means the device is a keyboard-like device, but no other characteristics are unknown.

core-pointer

The core pointer; this means the device is a pointing device, but no other characteristics are known.

mouse

A computer mouse.

trackpoint

A trackpoint or joystick (or other similar control.)

eraser

The other end of a stylus on a graphics tablet, or a standalone eraser.

pen

The pointed end of a pen on a graphics tablet, a stylus, or some other similar device.

puck

A device that looks like a computer mouse, but reports absolute coordinates relative to some other surface.

power-button

A power button or volume button (or other similar control.)

keyboard

A computer keyboard.

touchscreen

A computer touchpad.

pad

A collection of sensitive buttons, rings, and strips commonly found around a drawing tablet.

touchpad

An indirect touch device such as a touchpad.

piano

A musical instrument such as an electronic keyboard.

test

A device used by the XTEST extension to report input.