Swapping chords in Dvorak, or Why does emacs keyboard-translate fail with (wrong-type-argument characterp 134217845)

Table of Contents

Intro

One of the great pieces of advice I received for setting up Dvorak keys was to swap C-x and C-u keys, and the following snippet does just that.

;; Dvorak settings
(keyboard-translate ?\C-u ?\C-x)
(keyboard-translate ?\C-x ?\C-u)

I had hoped that a simple replacement of the the C with M would make that change for Meta, too, since M-x is common and awkward on Dvorak. However, that gave me a type error. So, for understanding’s sake I want to understand why the error; why wasn’t it as simple as a find-and-replace? Instead I had to make a change to the key-translation map, which works great but still leaves me in the dark. Can someone explain why keyboard-translate didn’t work here?

;; (keyboard-translate ?\M-u ?\M-x) ;; Debugger entered--Lisp error: (wrong-type-argument characterp 134217845)
;; (keyboard-translate ?\M-x ?\M-u)
(define-key key-translation-map (kbd "M-x") (kbd "M-u"))
(define-key key-translation-map (kbd "M-u") (kbd "M-x"))

Anwer

Big thanks to 7890yuiop, who explained.1

keyboard-translate works with char tables, hence the characterp error. Modified keys are not necessarily valid as characters.

(elisp) Char-Tables tells us: “A char-table is much like a vector, except that it is indexed by character codes. Any valid character code, without modifiers, can be used as an index in a char-table.”

The control modifier is a bit special, as typical control characters are still plain ASCII values, and hence valid for characterp.

In other words, there is something special about certain key chords like C-x that works within the character contraints of keyboard-translate but other keys don’t have that privilege.

Footnotes

1 Reddit comment in context here: https://www.reddit.com/r/emacs/comments/pr7gij/why_does_emacs_keyboardtranslate_fail_for_meta/hdgl4fq?utm_source=share&utm_medium=web2x&context=3

Tory Anderson avatar
Tory Anderson
Full-time Web App Engineer, Digital Humanist, Researcher, Computer Psychologist