unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#70139: 29.3.50; key-translate does not support all keyboard-translate usages
@ 2024-04-02  9:50 Robert Pluim
  2024-04-02 14:21 ` bug#70139: [PATCH] Improve key-translate to support removing translations Charalampos Mitrodimas
                   ` (3 more replies)
  0 siblings, 4 replies; 20+ messages in thread
From: Robert Pluim @ 2024-04-02  9:50 UTC (permalink / raw)
  To: 70139


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
-- 





^ permalink raw reply	[flat|nested] 20+ messages in thread

* bug#70139: [PATCH] Improve key-translate to support removing translations
  2024-04-02  9:50 bug#70139: 29.3.50; key-translate does not support all keyboard-translate usages Robert Pluim
@ 2024-04-02 14:21 ` Charalampos Mitrodimas
  2024-04-02 14:28 ` bug#70139: 29.3.50; key-translate does not support all keyboard-translate usages Charalampos Mitrodimas
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 20+ messages in thread
From: Charalampos Mitrodimas @ 2024-04-02 14:21 UTC (permalink / raw)
  To: 70139; +Cc: Charalampos Mitrodimas

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






^ permalink raw reply related	[flat|nested] 20+ messages in thread

* bug#70139: 29.3.50; key-translate does not support all keyboard-translate usages
  2024-04-02  9:50 bug#70139: 29.3.50; key-translate does not support all keyboard-translate usages Robert Pluim
  2024-04-02 14:21 ` bug#70139: [PATCH] Improve key-translate to support removing translations Charalampos Mitrodimas
@ 2024-04-02 14:28 ` Charalampos Mitrodimas
  2024-04-02 15:41   ` Robert Pluim
  2024-04-02 19:02 ` bug#70139: [PATCH] Improve key-translate to support removing translations Charalampos Mitrodimas
  2024-04-04 12:27 ` bug#70139: 29.3.50; key-translate does not support all keyboard-translate usages Eli Zaretskii
  3 siblings, 1 reply; 20+ messages in thread
From: Charalampos Mitrodimas @ 2024-04-02 14:28 UTC (permalink / raw)
  To: Robert Pluim; +Cc: 70139

[-- Attachment #1: Type: text/plain, Size: 1738 bytes --]


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

[-- Attachment #2: 0001-Improve-key-translate-to-support-removing-translatio.patch --]
[-- Type: text/x-patch, Size: 2018 bytes --]

From 4c35e45c128d8ad0c93ac787fe51eecbcd570286 Mon Sep 17 00:00:00 2001
From: Charalampos Mitrodimas <charmitro@posteo.net>
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


^ permalink raw reply related	[flat|nested] 20+ messages in thread

* bug#70139: 29.3.50; key-translate does not support all keyboard-translate usages
  2024-04-02 14:28 ` bug#70139: 29.3.50; key-translate does not support all keyboard-translate usages Charalampos Mitrodimas
@ 2024-04-02 15:41   ` Robert Pluim
  0 siblings, 0 replies; 20+ messages in thread
From: Robert Pluim @ 2024-04-02 15:41 UTC (permalink / raw)
  To: Charalampos Mitrodimas; +Cc: 70139

>>>>> On Tue,  2 Apr 2024 14:28:47 +0000, Charalampos Mitrodimas <charmitro@posteo.net> said:

The patch looks ok, it needs a ChangeLog entry style commit message
though (see the "** Commit messages" and "** Generating ChangeLog
entries" sections of CONTRIBUTE).

For extra credit, update the emacs lisp manual entry for
`key-translate' :-)

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

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

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

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

    Charalampos> The documentation string is updated to reflect the new behavior.

Robert
-- 





^ permalink raw reply	[flat|nested] 20+ messages in thread

* bug#70139: [PATCH] Improve key-translate to support removing translations
  2024-04-02  9:50 bug#70139: 29.3.50; key-translate does not support all keyboard-translate usages Robert Pluim
  2024-04-02 14:21 ` bug#70139: [PATCH] Improve key-translate to support removing translations Charalampos Mitrodimas
  2024-04-02 14:28 ` bug#70139: 29.3.50; key-translate does not support all keyboard-translate usages Charalampos Mitrodimas
@ 2024-04-02 19:02 ` Charalampos Mitrodimas
  2024-04-04 13:00   ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
  2024-04-04 12:27 ` bug#70139: 29.3.50; key-translate does not support all keyboard-translate usages Eli Zaretskii
  3 siblings, 1 reply; 20+ messages in thread
From: Charalampos Mitrodimas @ 2024-04-02 19:02 UTC (permalink / raw)
  To: 70139; +Cc: Charalampos Mitrodimas

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). (Bug#70139)
---
 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






^ permalink raw reply related	[flat|nested] 20+ messages in thread

* bug#70139: 29.3.50; key-translate does not support all keyboard-translate usages
  2024-04-02  9:50 bug#70139: 29.3.50; key-translate does not support all keyboard-translate usages Robert Pluim
                   ` (2 preceding siblings ...)
  2024-04-02 19:02 ` bug#70139: [PATCH] Improve key-translate to support removing translations Charalampos Mitrodimas
@ 2024-04-04 12:27 ` Eli Zaretskii
  3 siblings, 0 replies; 20+ messages in thread
From: Eli Zaretskii @ 2024-04-04 12:27 UTC (permalink / raw)
  To: Robert Pluim, Stefan Monnier; +Cc: 70139

> From: Robert Pluim <rpluim@gmail.com>
> Date: Tue, 02 Apr 2024 11:50:40 +0200
> 
> 
> 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?

Stefan, any comments or suggestions?





^ permalink raw reply	[flat|nested] 20+ messages in thread

* bug#70139: [PATCH] Improve key-translate to support removing translations
  2024-04-02 19:02 ` bug#70139: [PATCH] Improve key-translate to support removing translations Charalampos Mitrodimas
@ 2024-04-04 13:00   ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
  2024-04-04 19:48     ` Charalampos Mitrodimas
  0 siblings, 1 reply; 20+ messages in thread
From: Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors @ 2024-04-04 13:00 UTC (permalink / raw)
  To: Charalampos Mitrodimas; +Cc: 70139

I don't have much to say about this bug report or the fix for it, I'm
not very familiar with the `keyboard-translate-table` part of our
input processing.

>    (aset keyboard-translate-table
>          (aref (key-parse from) 0)
> -        (aref (key-parse to) 0)))
> +        (and to (aref (key-parse to) 0))))

But here, I think that, in keeping with the tradition of the `key-*`
functions, we should signal an error if `key-parse` returns an array
longer than 1 element, instead of just using the first element and
silently dropping the rest on the floor.


        Stefan






^ permalink raw reply	[flat|nested] 20+ messages in thread

* bug#70139: [PATCH] Improve key-translate to support removing translations
  2024-04-04 13:00   ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
@ 2024-04-04 19:48     ` Charalampos Mitrodimas
  2024-04-04 21:53       ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
  0 siblings, 1 reply; 20+ messages in thread
From: Charalampos Mitrodimas @ 2024-04-04 19:48 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: 70139

Hi,

Thanks for your review, much appreciated.

On 4/4/24 4:00 PM, Stefan Monnier via Bug reports for GNU Emacs, the 
Swiss army knife of text editors wrote:
> I don't have much to say about this bug report or the fix for it, I'm
> not very familiar with the `keyboard-translate-table` part of our
> input processing.
>
>>     (aset keyboard-translate-table
>>           (aref (key-parse from) 0)
>> -        (aref (key-parse to) 0)))
>> +        (and to (aref (key-parse to) 0))))
> But here, I think that, in keeping with the tradition of the `key-*`
> functions, we should signal an error if `key-parse` returns an array
> longer than 1 element, instead of just using the first element and
> silently dropping the rest on the floor.
>
>
>          Stefan

Just to clarify, are you referring to something like this?

     (key-translate "C-x" "C-z" "C-a")


--
Charalampos






^ permalink raw reply	[flat|nested] 20+ messages in thread

* bug#70139: [PATCH] Improve key-translate to support removing translations
  2024-04-04 19:48     ` Charalampos Mitrodimas
@ 2024-04-04 21:53       ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
  2024-04-06 17:09         ` Charalampos Mitrodimas
  0 siblings, 1 reply; 20+ messages in thread
From: Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors @ 2024-04-04 21:53 UTC (permalink / raw)
  To: Charalampos Mitrodimas; +Cc: 70139

> 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






^ permalink raw reply	[flat|nested] 20+ messages in thread

* bug#70139: [PATCH] Improve key-translate to support removing translations
  2024-04-04 21:53       ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
@ 2024-04-06 17:09         ` Charalampos Mitrodimas
  2024-04-06 17:18           ` Charalampos Mitrodimas
  0 siblings, 1 reply; 20+ messages in thread
From: Charalampos Mitrodimas @ 2024-04-06 17:09 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: 70139

[-- Attachment #1: Type: text/plain, Size: 297 bytes --]

Stefan Monnier <monnier@iro.umontreal.ca> 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.


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: patch --]
[-- Type: text/x-patch, Size: 2202 bytes --]

From 34a045b66ecc4e7428952c6a1f8ce4b7c7dfd7ce Mon Sep 17 00:00:00 2001
From: Charalampos Mitrodimas <charmitro@posteo.net>
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 | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/lisp/keymap.el b/lisp/keymap.el
index 4bdf65d39fa..94dcb592305 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.39.3 (Apple Git-146)


[-- Attachment #3: Type: text/plain, Size: 16 bytes --]


--
Charalampos

^ permalink raw reply related	[flat|nested] 20+ messages in thread

* bug#70139: [PATCH] Improve key-translate to support removing translations
  2024-04-06 17:09         ` Charalampos Mitrodimas
@ 2024-04-06 17:18           ` Charalampos Mitrodimas
  2024-04-07 14:45             ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
  0 siblings, 1 reply; 20+ messages in thread
From: Charalampos Mitrodimas @ 2024-04-06 17:18 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: 70139

[-- Attachment #1: Type: text/plain, Size: 546 bytes --]

Charalampos Mitrodimas <charmitro@posteo.net> writes:

> Stefan Monnier <monnier@iro.umontreal.ca> 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


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: patch --]
[-- Type: text/x-patch, Size: 2696 bytes --]

From 45fd8a776bd51117e9c14d5ce096b2121383795b Mon Sep 17 00:00:00 2001
From: Charalampos Mitrodimas <charmitro@posteo.net>
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)


^ permalink raw reply related	[flat|nested] 20+ messages in thread

* bug#70139: [PATCH] Improve key-translate to support removing translations
  2024-04-06 17:18           ` Charalampos Mitrodimas
@ 2024-04-07 14:45             ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
  2024-04-09 11:14               ` Charalampos Mitrodimas
  0 siblings, 1 reply; 20+ messages in thread
From: Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors @ 2024-04-07 14:45 UTC (permalink / raw)
  To: Charalampos Mitrodimas; +Cc: 70139

> +            (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))

I'd check `=` while at it (if length is 0 the error is caught by
`aref`, but it's a less helpful error).


        Stefan






^ permalink raw reply	[flat|nested] 20+ messages in thread

* bug#70139: [PATCH] Improve key-translate to support removing translations
  2024-04-07 14:45             ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
@ 2024-04-09 11:14               ` Charalampos Mitrodimas
  2024-04-09 12:57                 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
  0 siblings, 1 reply; 20+ messages in thread
From: Charalampos Mitrodimas @ 2024-04-09 11:14 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: 70139

[-- Attachment #1: Type: text/plain, Size: 1074 bytes --]


On 4/7/24 17:45, Stefan Monnier wrote:
>> +            (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))
> I'd check `=` while at it (if length is 0 the error is caught by
> `aref`, but it's a less helpful error).
>
>
>          Stefan

Updated & attached.

--
Charalampos

[-- Attachment #2: 0001-Improve-key-translate-to-support-removing-translatio.patch --]
[-- Type: text/x-patch, Size: 2939 bytes --]

From 30cbe7722a677a53ae9c9f66b13153275324adc6 Mon Sep 17 00:00:00 2001
From: Charalampos Mitrodimas <charmitro@posteo.net>
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 | 33 ++++++++++++++++++++++++---------
 1 file changed, 24 insertions(+), 9 deletions(-)

diff --git a/lisp/keymap.el b/lisp/keymap.el
index b2b475c7d71..51dfbf45d24 100644
--- a/lisp/keymap.el
+++ b/lisp/keymap.el
@@ -379,20 +379,35 @@ key-valid-p
 
 (defun key-translate (from to)
   "Translate character FROM to TO on the current terminal.
+
 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 (if to to nil)) 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))))
+    (cond
+     ((= (length from-key) 0)
+      (error "FROM key is empty"))
+     ((> (length from-key) 1)
+      (error "FROM key %s is not a single key" from)))
+    (cond
+     ((and to (= (length to-key) 0))
+      (error "TO key is empty"))
+     ((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.2


^ permalink raw reply related	[flat|nested] 20+ messages in thread

* bug#70139: [PATCH] Improve key-translate to support removing translations
  2024-04-09 11:14               ` Charalampos Mitrodimas
@ 2024-04-09 12:57                 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
  2024-04-09 13:07                   ` Charalampos Mitrodimas
  0 siblings, 1 reply; 20+ messages in thread
From: Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors @ 2024-04-09 12:57 UTC (permalink / raw)
  To: Charalampos Mitrodimas; +Cc: 70139

> -            (lambda (form) (keymap--compile-check from to) form)))
> +            (lambda (form) (keymap--compile-check from (if to to nil)) form)))
                                                          ^^^^^^^^^^^^^^

??

Just like (and to to), this is just a complicated way to say `to`.
What is the underlying idea?


        Stefan






^ permalink raw reply	[flat|nested] 20+ messages in thread

* bug#70139: [PATCH] Improve key-translate to support removing translations
  2024-04-09 12:57                 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
@ 2024-04-09 13:07                   ` Charalampos Mitrodimas
  2024-04-13  9:19                     ` Eli Zaretskii
  0 siblings, 1 reply; 20+ messages in thread
From: Charalampos Mitrodimas @ 2024-04-09 13:07 UTC (permalink / raw)
  To: 70139

[-- Attachment #1: Type: text/plain, Size: 578 bytes --]


On 4/9/24 15:57, Stefan Monnier via Bug reports for GNU Emacs, the Swiss 
army knife of text editors wrote:
>> -            (lambda (form) (keymap--compile-check from to) form)))
>> +            (lambda (form) (keymap--compile-check from (if to to nil)) form)))
>                                                            ^^^^^^^^^^^^^^
>
> ??
>
> Just like (and to to), this is just a complicated way to say `to`.
> What is the underlying idea?

I was trying to pass nil to keymap--compile-check but I was complicating 
this for no reason. Reverted it completely, apologies.

[-- Attachment #2: 0001-Improve-key-translate-to-support-removing-translatio.patch --]
[-- Type: text/x-patch, Size: 2857 bytes --]

From 2c39b3ac7ddffa600ec2982db76162e1456df19f Mon Sep 17 00:00:00 2001
From: Charalampos Mitrodimas <charmitro@posteo.net>
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 | 31 +++++++++++++++++++++++--------
 1 file changed, 23 insertions(+), 8 deletions(-)

diff --git a/lisp/keymap.el b/lisp/keymap.el
index b2b475c7d71..2fae7754bb0 100644
--- a/lisp/keymap.el
+++ b/lisp/keymap.el
@@ -379,20 +379,35 @@ key-valid-p
 
 (defun key-translate (from to)
   "Translate character FROM to TO on the current terminal.
+
 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)))
   (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))))
+    (cond
+     ((= (length from-key) 0)
+      (error "FROM key is empty"))
+     ((> (length from-key) 1)
+      (error "FROM key %s is not a single key" from)))
+    (cond
+     ((and to (= (length to-key) 0))
+      (error "TO key is empty"))
+     ((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.2


^ permalink raw reply related	[flat|nested] 20+ messages in thread

* bug#70139: [PATCH] Improve key-translate to support removing translations
  2024-04-09 13:07                   ` Charalampos Mitrodimas
@ 2024-04-13  9:19                     ` Eli Zaretskii
  2024-04-13 12:46                       ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
  2024-04-14 21:31                       ` Charalampos Mitrodimas
  0 siblings, 2 replies; 20+ messages in thread
From: Eli Zaretskii @ 2024-04-13  9:19 UTC (permalink / raw)
  To: Charalampos Mitrodimas, Stefan Monnier; +Cc: 70139

> Date: Tue,  9 Apr 2024 13:07:50 +0000
> From: Charalampos Mitrodimas <charmitro@posteo.net>
> 
> On 4/9/24 15:57, Stefan Monnier via Bug reports for GNU Emacs, the Swiss 
> army knife of text editors wrote:
> >> -            (lambda (form) (keymap--compile-check from to) form)))
> >> +            (lambda (form) (keymap--compile-check from (if to to nil)) form)))
> >                                                            ^^^^^^^^^^^^^^
> >
> > ??
> >
> > Just like (and to to), this is just a complicated way to say `to`.
> > What is the underlying idea?
> 
> I was trying to pass nil to keymap--compile-check but I was complicating 
> this for no reason. Reverted it completely, apologies.

Stefan, any further comments, or is this ready to be installed?

Charalampos, would you like to start the legal paperwork of assigning
to the FSF the copyright for your contributions?  This contribution is
already slightly above the limit that we are allowed to accept without
a copyright assignment.  If you agree, I will send you the form to
fill and the instructions to go with it.

Thanks.





^ permalink raw reply	[flat|nested] 20+ messages in thread

* bug#70139: [PATCH] Improve key-translate to support removing translations
  2024-04-13  9:19                     ` Eli Zaretskii
@ 2024-04-13 12:46                       ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
  2024-04-13 16:02                         ` Eli Zaretskii
  2024-04-14 21:31                       ` Charalampos Mitrodimas
  1 sibling, 1 reply; 20+ messages in thread
From: Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors @ 2024-04-13 12:46 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: Charalampos Mitrodimas, 70139

>> I was trying to pass nil to keymap--compile-check but I was complicating 
>> this for no reason. Reverted it completely, apologies.
>
> Stefan, any further comments, or is this ready to be installed?

Ready for me, thanks.


        Stefan






^ permalink raw reply	[flat|nested] 20+ messages in thread

* bug#70139: [PATCH] Improve key-translate to support removing translations
  2024-04-13 12:46                       ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
@ 2024-04-13 16:02                         ` Eli Zaretskii
  0 siblings, 0 replies; 20+ messages in thread
From: Eli Zaretskii @ 2024-04-13 16:02 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: charmitro, 70139

> From: Stefan Monnier <monnier@iro.umontreal.ca>
> Cc: Charalampos Mitrodimas <charmitro@posteo.net>,  70139@debbugs.gnu.org
> Date: Sat, 13 Apr 2024 08:46:16 -0400
> 
> >> I was trying to pass nil to keymap--compile-check but I was complicating 
> >> this for no reason. Reverted it completely, apologies.
> >
> > Stefan, any further comments, or is this ready to be installed?
> 
> Ready for me, thanks.

OK, so we are waiting for the legal paperwork, and then we can
install.

Thanks.





^ permalink raw reply	[flat|nested] 20+ messages in thread

* bug#70139: [PATCH] Improve key-translate to support removing translations
  2024-04-13  9:19                     ` Eli Zaretskii
  2024-04-13 12:46                       ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
@ 2024-04-14 21:31                       ` Charalampos Mitrodimas
  2024-04-15  2:30                         ` Eli Zaretskii
  1 sibling, 1 reply; 20+ messages in thread
From: Charalampos Mitrodimas @ 2024-04-14 21:31 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: 70139



> On 13 Apr 2024, at 12:21 PM, Eli Zaretskii <eliz@gnu.org> wrote:
> 
> 
>> 
>> Date: Tue,  9 Apr 2024 13:07:50 +0000
>> From: Charalampos Mitrodimas <charmitro@posteo.net>
>> 
>> On 4/9/24 15:57, Stefan Monnier via Bug reports for GNU Emacs, the Swiss
>> army knife of text editors wrote:
>>>> -            (lambda (form) (keymap--compile-check from to) form)))
>>>> +            (lambda (form) (keymap--compile-check from (if to to nil)) form)))
>>>                                                           ^^^^^^^^^^^^^^
>>> 
>>> ??
>>> 
>>> Just like (and to to), this is just a complicated way to say `to`.
>>> What is the underlying idea?
>> 
>> I was trying to pass nil to keymap--compile-check but I was complicating
>> this for no reason. Reverted it completely, apologies.
> 
> Stefan, any further comments, or is this ready to be installed?
> 
> Charalampos, would you like to start the legal paperwork of assigning
> to the FSF the copyright for your contributions?  This contribution is
> already slightly above the limit that we are allowed to accept without
> a copyright assignment.  If you agree, I will send you the form to
> fill and the instructions to go with it.

Yes I would like, as I’m planning to become more active as a contributor.

Please send it over. 

Thanks!





^ permalink raw reply	[flat|nested] 20+ messages in thread

* bug#70139: [PATCH] Improve key-translate to support removing translations
  2024-04-14 21:31                       ` Charalampos Mitrodimas
@ 2024-04-15  2:30                         ` Eli Zaretskii
  0 siblings, 0 replies; 20+ messages in thread
From: Eli Zaretskii @ 2024-04-15  2:30 UTC (permalink / raw)
  To: Charalampos Mitrodimas; +Cc: 70139

> From: Charalampos Mitrodimas <charmitro@posteo.net>
> Date: Sun, 14 Apr 2024 21:31:37 +0000
> Cc: 70139@debbugs.gnu.org
> 
> > 
> > Charalampos, would you like to start the legal paperwork of assigning
> > to the FSF the copyright for your contributions?  This contribution is
> > already slightly above the limit that we are allowed to accept without
> > a copyright assignment.  If you agree, I will send you the form to
> > fill and the instructions to go with it.
> 
> Yes I would like, as I’m planning to become more active as a contributor.
> 
> Please send it over. 

Form sent off-list.





^ permalink raw reply	[flat|nested] 20+ messages in thread

end of thread, other threads:[~2024-04-15  2:30 UTC | newest]

Thread overview: 20+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-04-02  9:50 bug#70139: 29.3.50; key-translate does not support all keyboard-translate usages Robert Pluim
2024-04-02 14:21 ` bug#70139: [PATCH] Improve key-translate to support removing translations Charalampos Mitrodimas
2024-04-02 14:28 ` bug#70139: 29.3.50; key-translate does not support all keyboard-translate usages Charalampos Mitrodimas
2024-04-02 15:41   ` Robert Pluim
2024-04-02 19:02 ` bug#70139: [PATCH] Improve key-translate to support removing translations Charalampos Mitrodimas
2024-04-04 13:00   ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-04-04 19:48     ` Charalampos Mitrodimas
2024-04-04 21:53       ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-04-06 17:09         ` Charalampos Mitrodimas
2024-04-06 17:18           ` Charalampos Mitrodimas
2024-04-07 14:45             ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-04-09 11:14               ` Charalampos Mitrodimas
2024-04-09 12:57                 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-04-09 13:07                   ` Charalampos Mitrodimas
2024-04-13  9:19                     ` Eli Zaretskii
2024-04-13 12:46                       ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-04-13 16:02                         ` Eli Zaretskii
2024-04-14 21:31                       ` Charalampos Mitrodimas
2024-04-15  2:30                         ` Eli Zaretskii
2024-04-04 12:27 ` bug#70139: 29.3.50; key-translate does not support all keyboard-translate usages Eli Zaretskii

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).