| | 1476 | @node Translation Keymaps |
|---|
| | 1477 | @section Keymaps for Translating Sequences of Events |
|---|
| | 1478 | |
|---|
| | 1479 | This section describes keymaps that are used during reading a key |
|---|
| | 1480 | sequence, to translate certain event sequences into others. |
|---|
| | 1481 | @code{read-key-sequence} checks every subsequence of the key sequence |
|---|
| | 1482 | being read, as it is read, against @code{function-key-map} and then |
|---|
| | 1483 | against @code{key-translation-map}. |
|---|
| | 1484 | |
|---|
| | 1485 | @defvar function-key-map |
|---|
| | 1486 | This variable holds a keymap that describes the character sequences sent |
|---|
| | 1487 | by function keys on an ordinary character terminal. This keymap has the |
|---|
| | 1488 | same structure as other keymaps, but is used differently: it specifies |
|---|
| | 1489 | translations to make while reading key sequences, rather than bindings |
|---|
| | 1490 | for key sequences. |
|---|
| | 1491 | |
|---|
| | 1492 | If @code{function-key-map} ``binds'' a key sequence @var{k} to a vector |
|---|
| | 1493 | @var{v}, then when @var{k} appears as a subsequence @emph{anywhere} in a |
|---|
| | 1494 | key sequence, it is replaced with the events in @var{v}. |
|---|
| | 1495 | |
|---|
| | 1496 | For example, VT100 terminals send @kbd{@key{ESC} O P} when the |
|---|
| | 1497 | keypad @key{PF1} key is pressed. Therefore, we want Emacs to translate |
|---|
| | 1498 | that sequence of events into the single event @code{pf1}. We accomplish |
|---|
| | 1499 | this by ``binding'' @kbd{@key{ESC} O P} to @code{[pf1]} in |
|---|
| | 1500 | @code{function-key-map}, when using a VT100. |
|---|
| | 1501 | |
|---|
| | 1502 | Thus, typing @kbd{C-c @key{PF1}} sends the character sequence @kbd{C-c |
|---|
| | 1503 | @key{ESC} O P}; later the function @code{read-key-sequence} translates |
|---|
| | 1504 | this back into @kbd{C-c @key{PF1}}, which it returns as the vector |
|---|
| | 1505 | @code{[?\C-c pf1]}. |
|---|
| | 1506 | |
|---|
| | 1507 | Entries in @code{function-key-map} are ignored if they conflict with |
|---|
| | 1508 | bindings made in the minor mode, local, or global keymaps. The intent |
|---|
| | 1509 | is that the character sequences that function keys send should not have |
|---|
| | 1510 | command bindings in their own right---but if they do, the ordinary |
|---|
| | 1511 | bindings take priority. |
|---|
| | 1512 | |
|---|
| | 1513 | The value of @code{function-key-map} is usually set up automatically |
|---|
| | 1514 | according to the terminal's Terminfo or Termcap entry, but sometimes |
|---|
| | 1515 | those need help from terminal-specific Lisp files. Emacs comes with |
|---|
| | 1516 | terminal-specific files for many common terminals; their main purpose is |
|---|
| | 1517 | to make entries in @code{function-key-map} beyond those that can be |
|---|
| | 1518 | deduced from Termcap and Terminfo. @xref{Terminal-Specific}. |
|---|
| | 1519 | @end defvar |
|---|
| | 1520 | |
|---|
| | 1521 | @defvar key-translation-map |
|---|
| | 1522 | This variable is another keymap used just like @code{function-key-map} |
|---|
| | 1523 | to translate input events into other events. It differs from |
|---|
| | 1524 | @code{function-key-map} in two ways: |
|---|
| | 1525 | |
|---|
| | 1526 | @itemize @bullet |
|---|
| | 1527 | @item |
|---|
| | 1528 | @code{key-translation-map} goes to work after @code{function-key-map} is |
|---|
| | 1529 | finished; it receives the results of translation by |
|---|
| | 1530 | @code{function-key-map}. |
|---|
| | 1531 | |
|---|
| | 1532 | @item |
|---|
| | 1533 | Non-prefix bindings in @code{key-translation-map} override actual key |
|---|
| | 1534 | bindings. For example, if @kbd{C-x f} has a non-prefix binding in |
|---|
| | 1535 | @code{key-translation-map}, that translation takes effect even though |
|---|
| | 1536 | @kbd{C-x f} also has a key binding in the global map. |
|---|
| | 1537 | @end itemize |
|---|
| | 1538 | |
|---|
| | 1539 | Note however that actual key bindings can have an effect on |
|---|
| | 1540 | @code{key-translation-map}, even though they are overridden by it. |
|---|
| | 1541 | Indeed, actual key bindings override @code{function-key-map} and thus |
|---|
| | 1542 | may alter the key sequence that @code{key-translation-map} receives. |
|---|
| | 1543 | Clearly, it is better to avoid this type of situation. |
|---|
| | 1544 | |
|---|
| | 1545 | The intent of @code{key-translation-map} is for users to map one |
|---|
| | 1546 | character set to another, including ordinary characters normally bound |
|---|
| | 1547 | to @code{self-insert-command}. |
|---|
| | 1548 | @end defvar |
|---|
| | 1549 | |
|---|
| | 1550 | @cindex key translation function |
|---|
| | 1551 | You can use @code{function-key-map} or @code{key-translation-map} for |
|---|
| | 1552 | more than simple aliases, by using a function, instead of a key |
|---|
| | 1553 | sequence, as the ``translation'' of a key. Then this function is called |
|---|
| | 1554 | to compute the translation of that key. |
|---|
| | 1555 | |
|---|
| | 1556 | The key translation function receives one argument, which is the prompt |
|---|
| | 1557 | that was specified in @code{read-key-sequence}---or @code{nil} if the |
|---|
| | 1558 | key sequence is being read by the editor command loop. In most cases |
|---|
| | 1559 | you can ignore the prompt value. |
|---|
| | 1560 | |
|---|
| | 1561 | If the function reads input itself, it can have the effect of altering |
|---|
| | 1562 | the event that follows. For example, here's how to define @kbd{C-c h} |
|---|
| | 1563 | to turn the character that follows into a Hyper character: |
|---|
| | 1564 | |
|---|
| | 1565 | @example |
|---|
| | 1566 | @group |
|---|
| | 1567 | (defun hyperify (prompt) |
|---|
| | 1568 | (let ((e (read-event))) |
|---|
| | 1569 | (vector (if (numberp e) |
|---|
| | 1570 | (logior (lsh 1 24) e) |
|---|
| | 1571 | (if (memq 'hyper (event-modifiers e)) |
|---|
| | 1572 | e |
|---|
| | 1573 | (add-event-modifier "H-" e)))))) |
|---|
| | 1574 | |
|---|
| | 1575 | (defun add-event-modifier (string e) |
|---|
| | 1576 | (let ((symbol (if (symbolp e) e (car e)))) |
|---|
| | 1577 | (setq symbol (intern (concat string |
|---|
| | 1578 | (symbol-name symbol)))) |
|---|
| | 1579 | @end group |
|---|
| | 1580 | @group |
|---|
| | 1581 | (if (symbolp e) |
|---|
| | 1582 | symbol |
|---|
| | 1583 | (cons symbol (cdr e))))) |
|---|
| | 1584 | |
|---|
| | 1585 | (define-key function-key-map "\C-ch" 'hyperify) |
|---|
| | 1586 | @end group |
|---|
| | 1587 | @end example |
|---|
| | 1588 | |
|---|
| | 1589 | If you have enabled keyboard character set decoding using |
|---|
| | 1590 | @code{set-keyboard-coding-system}, decoding is done after the |
|---|
| | 1591 | translations listed above. @xref{Terminal I/O Encoding}. However, in |
|---|
| | 1592 | future Emacs versions, character set decoding may be done at an |
|---|
| | 1593 | earlier stage. |
|---|
| | 1594 | |
|---|