Subject: bug#78714: 30.1; failure binding keys to certain unicode characters

From: Stefan Monnier <[email-masked]>
Date: Tue, 24 Jun 2025 17:15:11 -0400

> However the same syntax fails for some characters (so far I've noticed this
> for \u00B0 to \u00B6 but there will be more) e.g
> after (define-key my-test-mode-map (kbd "a") "\u00B2") C-h k shows

Just never bind keys to strings.  It doesn't do what you think it does.
Such strings are interpreted as "sequences of keyboard events" for
reasons of compatibility with the pre-GUI world where ASCII reigned
supreme.  So it comes with weird baggage inherited from *many*
years ago.

Just say no.

If you want to bind a key to a keyboard macro, then use `kmacro`:

    (define-key my-test-mode-map (kbd "a") (kmacro "\u00B2"))

`kmacro` also treats the string as a "sequence of keyboard events" but
expects this string to be written in a human-readable manner, i.e. the
same syntax output by `key-binding` or accepted as input by `key-parse`,
and it will often signal convenient errors when the format is unclear,
instead of silently behaving in odd ways.

        Stefan

PS: IMO, we should deprecate the use of strings and arrays as "commands".
It's been made obsolete back in 2002 when `kmacro.el` was added
to Emacs.  We keep it for backward compatibility, but it's just a source
of confusion.  Before Emacs-29's `kmacro` macro it could still be
justified on the grounds that it was inconvenient to write

    (define-key my-test-mode-map (kbd "a") (kmacro-lambda-form "\u00B2"))

(and it still didn't use the nicer `key-parse` syntax, and it printed to
something somewhat unpalatable), but nowadays `kmacro` is just
a better alternative.