7.2 The Command Interpreter

The command interpreter is a function which reads key-events (see section 7.2.1) from the keyboard and dispatches to different commands on the basis of what the user types. When the command interpreter executes a command, we say it invokes the command. The command interpreter also provides facilities for communication between commands contiguously running commands, such as a last command type register. It also takes care of resetting communication mechanisms, clearing the echo area, displaying partial keys typed slowly by the user, etc. [annotate]

invoke-hook   [Variable]
          

This variable contains a function the command interpreter calls when it wants to invoke a command. The function receives the command and the prefix argument as arguments. The initial value is a function which simply funcalls the command-function of the command with the supplied prefix argument. This is useful for implementing keyboard macros and similar things. [annotate]

Command Abort Hook  (initial value ) [Variable]
          

The command interpreter invokes the function in this variable whenever someone aborts a command (for example, if someone called editor-error). [annotate]

[annotate]

When Hemlock initially starts the command interpreter is in control, but commands may read from the keyboard themselves and assign whatever interpretation they will to the key-events read. Commands may call the command interpreter recursively using the function recursive-edit. [annotate]

7.2.1 Editor Input

The canonical representation of editor input is a key-event structure. Users can bind commands to keys (see section 1.3.1), which are non-zero length sequences of key-events. A key-event consists of an identifying token known as a keysym and a field of bits representing modifiers. Users define keysyms, integers between 0 and 65535 inclusively, by supplying names that reflect the legends on their keyboard's keys. Users define modifier names similarly, but the system chooses the bit and mask for recognizing the modifier. You can use keysym and modifier names to textually specify key-events and Hemlock keys in a #k syntax. The following are some examples: #k"C-u" #k"Control-u" #k"c-m-z" #k"control-x meta-d" #k"a" #k"A" #k"Linefeed" This is convenient for use within code and in init files containing bind-key calls. [annotate]

The #k syntax is delimited by double quotes, but the system parses the contents rather than reading it as a Common Lisp string. Within the double quotes, spaces separate multiple key-events. A single key-event optionally starts with modifier names terminated by hyphens. Modifier names are alphabetic sequences of characters which the system uses case-insensitively. Following modifiers is a keysym name, which is case-insensitive if it consists of multiple characters, but if the name consists of only a single character, then it is case-sensitive. [annotate]

You can escape special characters -- hyphen, double quote, open angle bracket, close angle bracket, and space -- with a backslash, and you can specify a backslash by using two contiguously. You can use angle brackets to enclose a keysym name with many special characters in it. Between angle brackets appearing in a keysym name position, there are only two special characters, the closing angle bracket and backslash. [annotate]

For more information on key-events see section 18.1. [annotate]

7.2.2 Binding Commands to Keys

The command interpreter determines which command to invoke on the basis of key bindings. A key binding is an association between a command and a sequence of key-events (see section 7.2.1. A sequence of key-events is called a key and is represented by a single key-event or a sequence (list or vector) of key-events. [annotate]

Since key bindings may be local to a mode or buffer, the current environment (page 5) determines the set of key bindings in effect at any given time. When the command interpreter tries to find the binding for a key, it first checks if there is a local binding in the current-buffer, then if there is a binding in each of the minor modes and the major mode for the current buffer (page 8), and finally checks to see if there is a global binding. If no binding is found, then the command interpreter beeps or flashes the screen to indicate this. [annotate]

bind-key  name key &optional kind where [Function]
          

This function associates command name and key in some environment. Key is either a key-event or a sequence of key-events. There are three possible values of kind: [annotate]

:global
The default, make a global key binding. [annotate]
:mode
Make a mode specific key binding in the mode whose name is where. [annotate]
:buffer
Make a binding which is local to buffer where. [annotate]

This processes key for key translations before establishing the binding. See section 7.2.3. [annotate]

If the key is some prefix of a key binding which already exists in the specified place, then the new one will override the old one, effectively deleting it. [annotate]

ext:do-alpha-key-events is useful for setting up bindings in certain new modes. [annotate]

[annotate]

command-bindings  command [Function]
          

This function returns a list of the places where command is bound. A place is specified as a list of the key (always a vector), the kind of binding, and where (either the mode or buffer to which the binding is local, or nil if it is a global). [annotate]

[annotate]

delete-key-binding  key &optional kind where [Function]
          

This function removes the binding of key in some place. Key is either a key-event or a sequence of key-events. kind is the kind of binding to delete, one of :global (the default), :mode or :buffer. If kind is :mode, where is the mode name, and if kind is :buffer, then where is the buffer. [annotate]

This function signals an error if key is unbound. [annotate]

This processes key for key translations before deleting the binding. See section 7.2.3. [annotate]

[annotate]

get-command  key &optional kind where [Function]
          

This function returns the command bound to key, returning nil if it is unbound. Key is either a key-event or a sequence of key-events. If key is an initial subsequence of some keys, then this returns the keyword :prefix. There are four cases of kind: [annotate]

:current
Return the current binding of key using the current buffer's search list. If there are any transparent key bindings for key, then they are returned in a list as a second value. [annotate]
:global
Return the global binding of key. This is the default. [annotate]
:mode
Return the binding of key in the mode named where. [annotate]
:buffer
Return the binding of key local to the buffer where. [annotate]

This processes key for key translations before looking for any binding. See section 7.2.3. [annotate]

[annotate]

map-bindings  function kind &optional where [Function]
          

This function maps over the key bindings in some place. For each binding, this passes function the key and the command bound to it. Kind and where are the same as in bind-key. The key is not guaranteed to remain valid after a given iteration. [annotate]

[annotate]

7.2.3 Key Translation

Key translation is a process that the command interpreter applies to keys before doing anything else. There are two kinds of key translations: substitution and bit-prefix. In either case, the command interpreter translates a key when a specified key-event sequence appears in a key. [annotate]

In a substitution translation, the system replaces the matched subsequence with another key-event sequence. Key translation is not recursively applied to the substituted key-events. [annotate]

In a bit-prefix translation, the system removes the matched subsequence and effectively sets the specified bits in the next key-event in the key. [annotate]

While translating a key, if the system encounters an incomplete final subsequence of key-events, it aborts the translation process. This happens when those last key-events form a prefix of some translation. It also happens when they translate to a bit-prefix, but there is no following key-event to which the system can apply the indicated modifier. If there is a binding for this partially untranslated key, then the command interpreter will invoke that command; otherwise, it will wait for the user to type more key-events. [annotate]

key-translation  key [Function]
          

This form is setf'able and allows users to register key translations that the command interpreter will use as users type key-events. [annotate]

This function returns the key translation for key, returning nil if there is none. Key is either a key-event or a sequence of key-events. If key is a prefix of a translation, then this returns :prefix. [annotate]

A key translation is either a key or modifier specification. The bits translations have a list form: (:bits {bit-name}*). [annotate]

Whenever key appears as a subsequence of a key argument to the binding manipulation functions, that portion will be replaced with the translation. [annotate]

[annotate]

7.2.4 Transparent Key Bindings

Key bindings local to a mode may be transparent. A transparent key binding does not shadow less local key bindings, but rather indicates that the bound command should be invoked before the first normal key binding. Transparent key bindings are primarily useful for implementing minor modes such as auto fill and word abbreviation. There may be several transparent key bindings for a given key, in which case all of the commands bound are invoked in the order they were found. If there no normal key binding for a key typed, then the command interpreter acts as though the key is unbound even if there are transparent key bindings. [annotate]

The :transparent-p argument to defmode determines whether the key bindings in a mode are transparent or not. [annotate]

7.2.5 Interactive

Hemlock supports keyboard macros. A user may enter a mode where the editor records his actions, and when the user exits this mode, the command Last Keyboard Macro plays back the actions. Some commands behave differently when invoked as part of the definition of a keyboard macro. For example, when used in a keyboard macro, a command that message's useless user confirmation will slow down the repeated invocations of Last Keyboard Macro because the command will pause on each execution to make sure the user sees the message. This can be eliminated with the use of interactive. As another example, some commands conditionally signal an editor-error versus simply beeping the device depending on whether it executes on behalf of the user or a keyboard macro. [annotate]

interactive   [Function]
          

This returns t when the user invoked the command directly. [annotate]

[annotate]

[annotate]