Subject: Re: bug#70139: [PATCH] Improve key-translate to support removing translations

From: Charalampos Mitrodimas <[email-masked]>
Date: Sat, 06 Apr 2024 17:18:29 +0000

Charalampos Mitrodimas <[email-masked]> writes:

> Stefan Monnier <[email-masked]> writes:
>
>>> Just to clarify, are you referring to something like this?
>>>
>>>     (key-translate "C-x" "C-z" "C-a")
>>
>> No, rather (key-translate "C-a C-z" "C-x b")
>>
>>
>>         Stefan
>
> Thanks for clarifying this! Updated patch attached.
>
> [2. patch --- text/x-patch; 0001-Improve-key-translate-to-support-removing-translatio.patch]...

Apologies, wrong patch. Attaching the correct one :-)

--
Charalampos

>From 45fd8a776bd51117e9c14d5ce096b2121383795b Mon Sep 17 00:00:00 2001
From: Charalampos Mitrodimas <[email-masked]>
Date: Tue, 2 Apr 2024 19:02:46 +0000
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 (key-translate): support removing translations by
  passing nil as the second argument (TO). Also throw error if multiple
  items passed. (Bug#70139)
---
 lisp/keymap.el | 26 +++++++++++++++++---------
 1 file changed, 17 insertions(+), 9 deletions(-)

diff --git a/lisp/keymap.el b/lisp/keymap.el
index 4bdf65d39fa..acff41a3786 100644
--- a/lisp/keymap.el
+++ b/lisp/keymap.el
@@ -382,17 +382,25 @@ 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)
-  (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)))
+  (when to
+    (keymap--check to))
+  (let ((from-key (key-parse from))
+        (to-key (and to (key-parse to))))
+    (when (> (length from-key) 1)
+      (error "FROM key %s is not a single key" from))
+    (when (and to (> (length to-key) 1))
+      (error "TO key %s is not a single key" to))
+    (or (char-table-p keyboard-translate-table)
+        (setq keyboard-translate-table
+              (make-char-table 'keyboard-translate-table nil)))
+    (aset keyboard-translate-table
+          (aref from-key 0)
+          (and to (aref to-key 0)))))
 
 (defun keymap-lookup (keymap key &optional accept-default no-remap position)
   "Return the binding for command KEY in KEYMAP.
-- 
2.39.3 (Apple Git-146)