1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
<h3 class="section">Keymaps for Translating Sequences of Events</h3> <p>When the <code>read-key-sequence</code> function reads a key sequence (see <a href="key-sequence-input">Key Sequence Input</a>), it uses <em>translation keymaps</em> to translate certain event sequences into others. The translation keymaps are <code>input-decode-map</code>, <code>local-function-key-map</code>, and <code>key-translation-map</code> (in order of priority). </p> <p>Translation keymaps have the same structure as other keymaps, but are used differently: they specify translations to make while reading key sequences, rather than bindings for complete key sequences. As each key sequence is read, it is checked against each translation keymap. If one of the translation keymaps binds <var>k</var> to a vector <var>v</var>, then whenever <var>k</var> appears as a sub-sequence <em>anywhere</em> in a key sequence, that sub-sequence is replaced with the events in <var>v</var>. </p> <p>For example, VT100 terminals send <kbd><span class="key">ESC</span> O P</kbd> when the keypad key <tt class="key">PF1</tt> is pressed. On such terminals, Emacs must translate that sequence of events into a single event <code>pf1</code>. This is done by binding <kbd><span class="key">ESC</span> O P</kbd> to <code>[pf1]</code> in <code>input-decode-map</code>. Thus, when you type <kbd>C-c <span class="key">PF1</span></kbd> on the terminal, the terminal emits the character sequence <kbd>C-c <span class="key">ESC</span> O P</kbd>, and <code>read-key-sequence</code> translates this back into <kbd>C-c <span class="key">PF1</span></kbd> and returns it as the vector <code>[?\C-c pf1]</code>. </p> <p>Translation keymaps take effect only after Emacs has decoded the keyboard input (via the input coding system specified by <code>keyboard-coding-system</code>). See <a href="terminal-i_002fo-encoding">Terminal I/O Encoding</a>. </p> <dl> <dt id="input-decode-map">Variable: <strong>input-decode-map</strong>
</dt> <dd>
<p>This variable holds a keymap that describes the character sequences sent by function keys on an ordinary character terminal. </p> <p>The value of <code>input-decode-map</code> is usually set up automatically according to the terminal’s Terminfo or Termcap entry, but sometimes those need help from terminal-specific Lisp files. Emacs comes with terminal-specific files for many common terminals; their main purpose is to make entries in <code>input-decode-map</code> beyond those that can be deduced from Termcap and Terminfo. See <a href="terminal_002dspecific">Terminal-Specific</a>. </p>
</dd>
</dl> <dl> <dt id="local-function-key-map">Variable: <strong>local-function-key-map</strong>
</dt> <dd>
<p>This variable holds a keymap similar to <code>input-decode-map</code> except that it describes key sequences which should be translated to alternative interpretations that are usually preferred. It applies after <code>input-decode-map</code> and before <code>key-translation-map</code>. </p> <p>Entries in <code>local-function-key-map</code> are ignored if they conflict with bindings made in the minor mode, local, or global keymaps. I.e., the remapping only applies if the original key sequence would otherwise not have any binding. </p> <p><code>local-function-key-map</code> inherits from <code>function-key-map</code>. The latter should only be altered if you want the binding to apply in all terminals, so using the former is almost always preferred. </p>
</dd>
</dl> <dl> <dt id="key-translation-map">Variable: <strong>key-translation-map</strong>
</dt> <dd>
<p>This variable is another keymap used just like <code>input-decode-map</code> to translate input events into other events. It differs from <code>input-decode-map</code> in that it goes to work after <code>local-function-key-map</code> is finished rather than before; it receives the results of translation by <code>local-function-key-map</code>. </p> <p>Just like <code>input-decode-map</code>, but unlike <code>local-function-key-map</code>, this keymap is applied regardless of whether the input key-sequence has a normal binding. Note however that actual key bindings can have an effect on <code>key-translation-map</code>, even though they are overridden by it. Indeed, actual key bindings override <code>local-function-key-map</code> and thus may alter the key sequence that <code>key-translation-map</code> receives. Clearly, it is better to avoid this type of situation. </p> <p>The intent of <code>key-translation-map</code> is for users to map one character set to another, including ordinary characters normally bound to <code>self-insert-command</code>. </p>
</dd>
</dl> <p>You can use <code>input-decode-map</code>, <code>local-function-key-map</code>, and <code>key-translation-map</code> for more than simple aliases, by using a function, instead of a key sequence, as the translation of a key. Then this function is called to compute the translation of that key. </p> <p>The key translation function receives one argument, which is the prompt that was specified in <code>read-key-sequence</code>—or <code>nil</code> if the key sequence is being read by the editor command loop. In most cases you can ignore the prompt value. </p> <p>If the function reads input itself, it can have the effect of altering the event that follows. For example, here’s how to define <kbd>C-c h</kbd> to turn the character that follows into a Hyper character: </p> <div class="example"> <pre class="example">(defun hyperify (prompt)
(let ((e (read-event)))
(vector (if (numberp e)
(logior (ash 1 24) e)
(if (memq 'hyper (event-modifiers e))
e
(add-event-modifier "H-" e))))))
(defun add-event-modifier (string e)
(let ((symbol (if (symbolp e) e (car e))))
(setq symbol (intern (concat string
(symbol-name symbol))))
(if (symbolp e)
symbol
(cons symbol (cdr e)))))
(define-key local-function-key-map "\C-ch" 'hyperify)
</pre>
</div> <h4 class="subsection">Interaction with normal keymaps</h4> <p>The end of a key sequence is detected when that key sequence either is bound to a command, or when Emacs determines that no additional event can lead to a sequence that is bound to a command. </p> <p>This means that, while <code>input-decode-map</code> and <code>key-translation-map</code> apply regardless of whether the original key sequence would have a binding, the presence of such a binding can still prevent translation from taking place. For example, let us return to our VT100 example above and add a binding for <kbd>C-c <span class="key">ESC</span></kbd> to the global map; now when the user hits <kbd>C-c <span class="key">PF1</span></kbd> Emacs will fail to decode <kbd>C-c <span class="key">ESC</span> O P</kbd> into <kbd>C-c <span class="key">PF1</span></kbd> because it will stop reading keys right after <kbd>C-c <span class="key">ESC</span></kbd>, leaving <kbd>O P</kbd> for later. This is in case the user really hit <kbd>C-c <span class="key">ESC</span></kbd>, in which case Emacs should not sit there waiting for the next key to decide whether the user really pressed <kbd><span class="key">ESC</span></kbd> or <kbd><span class="key">PF1</span></kbd>. </p> <p>For that reason, it is better to avoid binding commands to key sequences where the end of the key sequence is a prefix of a key translation. The main such problematic suffixes/prefixes are <kbd><span class="key">ESC</span></kbd>, <kbd>M-O</kbd> (which is really <kbd><span class="key">ESC</span> O</kbd>) and <kbd>M-[</kbd> (which is really <kbd><span class="key">ESC</span> [</kbd>). </p><div class="_attribution">
<p class="_attribution-p">
Copyright © 1990-1996, 1998-2022 Free Software Foundation, Inc. <br>Licensed under the GNU GPL license.<br>
<a href="https://www.gnu.org/software/emacs/manual/html_node/elisp/Translation-Keymaps.html" class="_attribution-link">https://www.gnu.org/software/emacs/manual/html_node/elisp/Translation-Keymaps.html</a>
</p>
</div>
|