Subject: Re: bug#70139: 29.3.50; key-translate does not support all keyboard-translate usages

From: Charalampos Mitrodimas <[email-masked]>
Date: Tue, 2 Apr 2024 14:28:47 +0000

On 4/2/24 12:50, Robert Pluim wrote:
> When `key-translate' was added, it didnʼt cover all the cases that
> `keyboard-translate' does.
>
> Add a translation:
>
>      (keyboard-translate ?\C-a ?\C-z)
>
> Two ways to remove, of which I submit the first is 'obvious':
>
>      (keyboard-translate ?\C-a nil)
>      (keyboard-translate ?\C-a ?\C-a)
>
> Add:
>
>      (key-translate "C-a" "C-z")
>
> This works for removing a translation but is non-obvious:
>
>      (key-translate "C-a" "C-a")
>
> But this doesnʼt:
>
>      (key-translate "C-a" nil)
>
> =>
> Debugger entered--Lisp error: (error "nil is not a valid key definition; see ‘key-valid-...")
>    signal(error ("nil is not a valid key definition; see ‘key-valid-..."))
>    error("%S is not a valid key definition; see `key-valid-p..." nil)
>
> Iʼm not sure this is worth fixing, but perhaps documenting that
> re-adding the same translation is (almost) the same as removing it?
>
> Thanks
>
> Robert
>
> In GNU Emacs 29.3.50 (build 2, x86_64-pc-linux-gnu, GTK+ Version
>   3.24.38, cairo version 1.16.0) of 2024-04-02 built on rltb
> Repository revision: 6b8b0a12333afeadb32744ba481679b05b758ed2
> Repository branch: emacs-29
> Windowing system distributor 'The X.Org Foundation', version 11.0.12009000
> System Description: Debian GNU/Linux 12 (bookworm)
>
> Configured features:
> ACL CAIRO DBUS FREETYPE GIF GLIB GMP GNUTLS GPM GSETTINGS HARFBUZZ JPEG
> JSON LCMS2 LIBOTF LIBSELINUX LIBSYSTEMD LIBXML2 M17N_FLT MODULES NOTIFY
> INOTIFY PDUMPER PNG RSVG SECCOMP SOUND SQLITE3 THREADS TIFF
> TOOLKIT_SCROLL_BARS TREE_SITTER WEBP X11 XDBE XIM XINPUT2 XPM GTK3 ZLIB

I accidentally sent the patch the wrong way (newcomer here), attaching here.

--
Charalampos Mitrodimas
From 4c35e45c128d8ad0c93ac787fe51eecbcd570286 Mon Sep 17 00:00:00 2001
From: Charalampos Mitrodimas <[email-masked]>
Date: Tue, 2 Apr 2024 17:20:44 +0300
Subject: [PATCH] Improve key-translate to support removing translations

This patch enhances the key-translate function to allow removing
keyboard translations by passing nil as the second argument (TO).

If TO is nil, any existing translation for the FROM key will be removed.
The compiler macro is updated to only check TO when it is non-nil.

This change makes key-translate more consistent with the behavior of
keyboard-translate, providing a way to remove translations without
having to specify the same key for both FROM and TO.

The documentation string is updated to reflect the new behavior.
---
 lisp/keymap.el | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/lisp/keymap.el b/lisp/keymap.el
index b2b475c7d71..1481f1fe72b 100644
--- a/lisp/keymap.el
+++ b/lisp/keymap.el
@@ -382,17 +382,19 @@ key-translate
 This function creates a `keyboard-translate-table' if necessary
 and then modifies one entry in it.
 
-Both FROM and TO should be specified by strings that satisfy `key-valid-p'."
+Both FROM and TO should be specified by strings that satisfy `key-valid-p'.
+If TO is nil, remove any existing translation for FROM."
   (declare (compiler-macro
-            (lambda (form) (keymap--compile-check from to) form)))
+            (lambda (form) (keymap--compile-check from (and to to)) form)))
   (keymap--check from)
-  (keymap--check to)
+  (when to
+    (keymap--check to))
   (or (char-table-p keyboard-translate-table)
       (setq keyboard-translate-table
             (make-char-table 'keyboard-translate-table nil)))
   (aset keyboard-translate-table
         (aref (key-parse from) 0)
-        (aref (key-parse to) 0)))
+        (and to (aref (key-parse to) 0))))
 
 (defun keymap-lookup (keymap key &optional accept-default no-remap position)
   "Return the binding for command KEY in KEYMAP.
-- 
2.34.1