* bug#72460: [patch] add commands for setting keyboard translations interactively
@ 2024-08-04 12:16 hugo
2024-08-17 8:46 ` Eli Zaretskii
2024-08-19 14:42 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
0 siblings, 2 replies; 11+ messages in thread
From: hugo @ 2024-08-04 12:16 UTC (permalink / raw)
To: 72460
[-- Attachment #1: Type: text/plain, Size: 827 bytes --]
Dear all,
The attached patch adds commands for adding and removing entries in
`keyboard-translate-table' interactively:
- add an interactive spec to `key-translate'
- add a new command `key-translate-remove', which does the opposite of
`key-translate'.
- add an option for users to customise how the latter function prompts
for which from/to pair is removed. The default just lists them with
`completing-read'.
I find this sort of thing useful for cases where I want to use
something other than space as a word separator (e.g. when typing lots
of function names), or when for a long period I want to use the symbol
which is usually on the shift of a key (i.e. I know I want to type "+"
a lot, but not "=").
The patch doesn't contain a NEWS entry yet -- I wanted to get the
greenlight on the code first.
Best,
Hugo
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Add-commands-to-interactively-set-unset-keyboard-tra.patch --]
[-- Type: text/x-diff; name=0001-Add-commands-to-interactively-set-unset-keyboard-tra.patch, Size: 3772 bytes --]
From b5b0d6e4c53fcedea92df03e86c57af856c9fbb7 Mon Sep 17 00:00:00 2001
From: Hugo Heagren <hugo@heagren.com>
Date: Sun, 4 Aug 2024 12:54:27 +0100
Subject: [PATCH] Add commands to interactively set/unset keyboard translations
* lisp/keymap.el (key-translate): Add an interactive form, prompting for
keys to translate, and update docstring to reflect this.
(key-translate-selection-function): New custom option.
(key-select-translation): New function, default value of above option.
(key-translate-remove): New command, for removing entries from
`keyboard-translate-table'.
---
lisp/keymap.el | 57 +++++++++++++++++++++++++++++++++++++++++++++++++-
1 file changed, 56 insertions(+), 1 deletion(-)
diff --git a/lisp/keymap.el b/lisp/keymap.el
index 861d6724c9e..d5bf51fd849 100644
--- a/lisp/keymap.el
+++ b/lisp/keymap.el
@@ -392,9 +392,13 @@ key-translate
and then modifies one entry in it.
Both FROM and TO should be specified by strings that satisfy `key-valid-p'.
-If TO is nil, remove any existing translation for FROM."
+If TO is nil, remove any existing translation for FROM.
+
+Interactively, prompt for FROM and TO with `read-char'."
(declare (compiler-macro
(lambda (form) (keymap--compile-check from to) form)))
+ (interactive `(,(key-description `[,(read-char "From: ")])
+ ,(key-description `[,(read-char "To: ")])))
(keymap--check from)
(when to
(keymap--check to))
@@ -417,6 +421,57 @@ key-translate
(aref from-key 0)
(and to (aref to-key 0)))))
+(defcustom key-translate-selection-function #'key-select-translation
+ "Function to select one current key translation pair.
+
+`key-translate-remove' uses this function to prompt for the translation
+to remove. It must take no arguments, prompt the user for a translation
+pair in `keyboard-translate-table', and return a vector containing only
+the FROM key of the selected pair (e.g. if the selected pair translates
+\"=\" to \"+\", the function should return the vector [61])"
+ :type 'function
+ :group 'keyboard)
+
+(defun key-select-translation ()
+ "Prompt for a current keyboard translation pair with `'completing-read'.
+
+Each pair is formatted as \"FROM -> TO\"."
+ (let* ((minibuffer-allow-text-properties t)
+ (collection)
+ ;; Alignment helpers
+ (pad 0)
+ (_ (map-char-table
+ (lambda (k _)
+ (when (> (length (key-description `[,k])) pad)
+ (setq pad (length (key-description `[,k])))))
+ keyboard-translate-table
+ ))
+ (format-func
+ (lambda (k v)
+ (setq collection
+ (cons
+ (propertize
+ (format
+ "%s -> %s"
+ (string-pad (key-description `[,k]) pad)
+ (key-description `[,v]))
+ 'key-translate-from `[,k])
+ collection)))))
+ ;; Populate collection
+ (map-char-table format-func keyboard-translate-table)
+ (get-text-property 0 'key-translate-from
+ (completing-read "Key Translation: " collection))))
+
+(defun key-translate-remove (from)
+ "Remove translation of FROM from `keyboard-translate-table'.
+
+FROM must satisfy `key-valid-p'. If FROM has no entry in
+`keyboard-translate-table', this has no effect."
+ (interactive (list (key-description
+ (funcall key-translate-selection-function))))
+ (set-char-table-range
+ keyboard-translate-table (aref (key-parse from) 0) nil))
+
(defun keymap-lookup (keymap key &optional accept-default no-remap position)
"Return the binding for command KEY in KEYMAP.
KEY is a string that satisfies `key-valid-p'.
--
2.20.1
^ permalink raw reply related [flat|nested] 11+ messages in thread
* bug#72460: [patch] add commands for setting keyboard translations interactively
2024-08-04 12:16 bug#72460: [patch] add commands for setting keyboard translations interactively hugo
@ 2024-08-17 8:46 ` Eli Zaretskii
2024-08-19 14:42 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
1 sibling, 0 replies; 11+ messages in thread
From: Eli Zaretskii @ 2024-08-17 8:46 UTC (permalink / raw)
To: hugo, Stefan Monnier; +Cc: 72460
> Date: Sun, 04 Aug 2024 13:16:29 +0100
> From: hugo@heagren.com
>
> The attached patch adds commands for adding and removing entries in
> `keyboard-translate-table' interactively:
> - add an interactive spec to `key-translate'
> - add a new command `key-translate-remove', which does the opposite of
> `key-translate'.
> - add an option for users to customise how the latter function prompts
> for which from/to pair is removed. The default just lists them with
> `completing-read'.
>
> I find this sort of thing useful for cases where I want to use
> something other than space as a word separator (e.g. when typing lots
> of function names), or when for a long period I want to use the symbol
> which is usually on the shift of a key (i.e. I know I want to type "+"
> a lot, but not "=").
>
> The patch doesn't contain a NEWS entry yet -- I wanted to get the
> greenlight on the code first.
Thanks.
Stefan, any comments?
^ permalink raw reply [flat|nested] 11+ messages in thread
* bug#72460: [patch] add commands for setting keyboard translations interactively
2024-08-04 12:16 bug#72460: [patch] add commands for setting keyboard translations interactively hugo
2024-08-17 8:46 ` Eli Zaretskii
@ 2024-08-19 14:42 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-08-31 8:08 ` Eli Zaretskii
[not found] ` <ed0eede1b3ac4e6da54c2449e3c8ea4f@heagren.com>
1 sibling, 2 replies; 11+ messages in thread
From: Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors @ 2024-08-19 14:42 UTC (permalink / raw)
To: hugo; +Cc: 72460
> I find this sort of thing useful for cases where I want to use
> something other than space as a word separator (e.g. when typing lots
> of function names), or when for a long period I want to use the symbol
> which is usually on the shift of a key (i.e. I know I want to type "+"
> a lot, but not "=").
Interesting. I can see that `keyboard-translate-table` is indeed
a cheap way to implement it.
[ This said, it applies at a very low level (directly inside
`read-event`) and only to "characters" (e.g. not to the `tab` or
`return` keys), which can make it both maybe too powerful and yet not
flexible enough. ]
> + (interactive `(,(key-description `[,(read-char "From: ")])
> + ,(key-description `[,(read-char "To: ")])))
[ Personally, I'd add a comment lamenting the fact that `keymap-*`
functions force us to use `key-description` here only for that to be
undone by `key-parse`. ]
> +(defcustom key-translate-selection-function #'key-select-translation
Do we need this?
> +pair in `keyboard-translate-table', and return a vector containing only
> +the FROM key of the selected pair (e.g. if the selected pair translates
> +\"=\" to \"+\", the function should return the vector [61])"
Why not return just the char rather than a vector containing the char?
> +(defun key-translate-remove (from)
> + "Remove translation of FROM from `keyboard-translate-table'.
> +
> +FROM must satisfy `key-valid-p'. If FROM has no entry in
> +`keyboard-translate-table', this has no effect."
> + (interactive (list (key-description
> + (funcall key-translate-selection-function))))
> + (set-char-table-range
> + keyboard-translate-table (aref (key-parse from) 0) nil))
If FROM maps to a sequence of length != 1, we'll either signal an ugly
error or silently ignore the rest.
Any reason why you didn't make FROM be a simple character?
Stefan
^ permalink raw reply [flat|nested] 11+ messages in thread
* bug#72460: [patch] add commands for setting keyboard translations interactively
2024-08-19 14:42 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
@ 2024-08-31 8:08 ` Eli Zaretskii
[not found] ` <ed0eede1b3ac4e6da54c2449e3c8ea4f@heagren.com>
1 sibling, 0 replies; 11+ messages in thread
From: Eli Zaretskii @ 2024-08-31 8:08 UTC (permalink / raw)
To: hugo, Stefan Monnier; +Cc: 72460
Ping! Hugo, can you please respond to Stefan's review comments, so we
could make progress here?
> Cc: 72460@debbugs.gnu.org
> Date: Mon, 19 Aug 2024 10:42:43 -0400
> From: Stefan Monnier via "Bug reports for GNU Emacs,
> the Swiss army knife of text editors" <bug-gnu-emacs@gnu.org>
>
> > I find this sort of thing useful for cases where I want to use
> > something other than space as a word separator (e.g. when typing lots
> > of function names), or when for a long period I want to use the symbol
> > which is usually on the shift of a key (i.e. I know I want to type "+"
> > a lot, but not "=").
>
> Interesting. I can see that `keyboard-translate-table` is indeed
> a cheap way to implement it.
>
> [ This said, it applies at a very low level (directly inside
> `read-event`) and only to "characters" (e.g. not to the `tab` or
> `return` keys), which can make it both maybe too powerful and yet not
> flexible enough. ]
>
> > + (interactive `(,(key-description `[,(read-char "From: ")])
> > + ,(key-description `[,(read-char "To: ")])))
>
> [ Personally, I'd add a comment lamenting the fact that `keymap-*`
> functions force us to use `key-description` here only for that to be
> undone by `key-parse`. ]
>
> > +(defcustom key-translate-selection-function #'key-select-translation
>
> Do we need this?
>
> > +pair in `keyboard-translate-table', and return a vector containing only
> > +the FROM key of the selected pair (e.g. if the selected pair translates
> > +\"=\" to \"+\", the function should return the vector [61])"
>
> Why not return just the char rather than a vector containing the char?
>
> > +(defun key-translate-remove (from)
> > + "Remove translation of FROM from `keyboard-translate-table'.
> > +
> > +FROM must satisfy `key-valid-p'. If FROM has no entry in
> > +`keyboard-translate-table', this has no effect."
> > + (interactive (list (key-description
> > + (funcall key-translate-selection-function))))
> > + (set-char-table-range
> > + keyboard-translate-table (aref (key-parse from) 0) nil))
>
> If FROM maps to a sequence of length != 1, we'll either signal an ugly
> error or silently ignore the rest.
> Any reason why you didn't make FROM be a simple character?
>
>
> Stefan
>
>
>
>
>
^ permalink raw reply [flat|nested] 11+ messages in thread
[parent not found: <ed0eede1b3ac4e6da54c2449e3c8ea4f@heagren.com>]
* bug#72460: [patch] add commands for setting keyboard translations interactively
[not found] ` <ed0eede1b3ac4e6da54c2449e3c8ea4f@heagren.com>
@ 2024-08-31 13:56 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-09-14 7:38 ` Eli Zaretskii
0 siblings, 1 reply; 11+ messages in thread
From: Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors @ 2024-08-31 13:56 UTC (permalink / raw)
To: hugo; +Cc: 72460
>> > +(defcustom key-translate-selection-function #'key-select-translation
>> Do we need this?
> Short answer: yes.
>
> I wanted a way for users to customise how the current translation
> pairs are presented when they choose one to remove. The initial
> motivation was that some users might want to customise the formatting
> of the pairs; "X -> Y" vs "X → Y" vs "X Y" etc. (I'm the sort of user
> who cares about this sort of thing a lot).
Rewriting a whole new `key-translate-selection-function` function for
that is not significantly easier than rewriting a whole new
`key-translate-remove`, so I think this is firmly in the overengineering
side of the camp.
> I also envisage that many users will want to use/expect
> completing-read (as in the default) and will want the from/to elements
> vertically aligned (again, I am this user).
AFAICT, you're the first user to propose this functionality in Emacs's
40 years life, so I wouldn't bet on "many users".
BTW, I just noticed:
> + ;; Populate collection
> + (map-char-table format-func keyboard-translate-table)
> + (get-text-property 0 'key-translate-from
> + (completing-read "Key Translation: " collection))))
You can't trust `get-text-property` here (especially since you don't
even force `completing-read` to `require-match`).
Instead you should do something like `key-parse`.
> I think here I was just copying the current calling convention for the
> existing (non-interactive) `key-translate'. Its docstring reads:
>
>> Both FROM and TO should be specified by strings that satisfy
>> `key-valid-p'.
>
> I sort of assumed someone cleverer than me had a good reason.
I suspect this is because we expect this function to be called from
a `~/.emacs` with a literal constant as argument, and we've tried to
unify on using the "KBD" syntax for those cases, since it's a friendlier
syntax than the "vector of events" where you need to distinguish between
symbols and chars with/without modifiers.
Since the arg of `key-translate` can be only a valid char (rather than
any arbitrary key potentially with modifiers), the argument is much less
strong, but my guess is that it was uniformity.
In the case of `key-translate-remove` I don't expect the arg to be
a literal constant in the code, so "nice/uniform syntax to write the
argument value" shouldn't be a concern. 🙂
Stefan
^ permalink raw reply [flat|nested] 11+ messages in thread
* bug#72460: [patch] add commands for setting keyboard translations interactively
2024-08-31 13:56 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
@ 2024-09-14 7:38 ` Eli Zaretskii
2024-09-28 8:49 ` Eli Zaretskii
0 siblings, 1 reply; 11+ messages in thread
From: Eli Zaretskii @ 2024-09-14 7:38 UTC (permalink / raw)
To: hugo, Stefan Monnier; +Cc: 72460
Hugo,
Any followups to Stefan's comments?
> Cc: 72460@debbugs.gnu.org
> Date: Sat, 31 Aug 2024 09:56:58 -0400
> From: Stefan Monnier via "Bug reports for GNU Emacs,
> the Swiss army knife of text editors" <bug-gnu-emacs@gnu.org>
>
> >> > +(defcustom key-translate-selection-function #'key-select-translation
> >> Do we need this?
> > Short answer: yes.
> >
> > I wanted a way for users to customise how the current translation
> > pairs are presented when they choose one to remove. The initial
> > motivation was that some users might want to customise the formatting
> > of the pairs; "X -> Y" vs "X → Y" vs "X Y" etc. (I'm the sort of user
> > who cares about this sort of thing a lot).
>
> Rewriting a whole new `key-translate-selection-function` function for
> that is not significantly easier than rewriting a whole new
> `key-translate-remove`, so I think this is firmly in the overengineering
> side of the camp.
>
> > I also envisage that many users will want to use/expect
> > completing-read (as in the default) and will want the from/to elements
> > vertically aligned (again, I am this user).
>
> AFAICT, you're the first user to propose this functionality in Emacs's
> 40 years life, so I wouldn't bet on "many users".
>
> BTW, I just noticed:
>
> > + ;; Populate collection
> > + (map-char-table format-func keyboard-translate-table)
> > + (get-text-property 0 'key-translate-from
> > + (completing-read "Key Translation: " collection))))
>
> You can't trust `get-text-property` here (especially since you don't
> even force `completing-read` to `require-match`).
> Instead you should do something like `key-parse`.
>
> > I think here I was just copying the current calling convention for the
> > existing (non-interactive) `key-translate'. Its docstring reads:
> >
> >> Both FROM and TO should be specified by strings that satisfy
> >> `key-valid-p'.
> >
> > I sort of assumed someone cleverer than me had a good reason.
>
> I suspect this is because we expect this function to be called from
> a `~/.emacs` with a literal constant as argument, and we've tried to
> unify on using the "KBD" syntax for those cases, since it's a friendlier
> syntax than the "vector of events" where you need to distinguish between
> symbols and chars with/without modifiers.
>
> Since the arg of `key-translate` can be only a valid char (rather than
> any arbitrary key potentially with modifiers), the argument is much less
> strong, but my guess is that it was uniformity.
>
> In the case of `key-translate-remove` I don't expect the arg to be
> a literal constant in the code, so "nice/uniform syntax to write the
> argument value" shouldn't be a concern. 🙂
>
>
> Stefan
>
>
>
>
>
^ permalink raw reply [flat|nested] 11+ messages in thread
* bug#72460: [patch] add commands for setting keyboard translations interactively
2024-09-14 7:38 ` Eli Zaretskii
@ 2024-09-28 8:49 ` Eli Zaretskii
2024-09-28 15:33 ` hugo
0 siblings, 1 reply; 11+ messages in thread
From: Eli Zaretskii @ 2024-09-28 8:49 UTC (permalink / raw)
To: hugo; +Cc: 72460, monnier
Ping! Hugo, please respond.
> Cc: 72460@debbugs.gnu.org
> Date: Sat, 14 Sep 2024 10:38:04 +0300
> From: Eli Zaretskii <eliz@gnu.org>
>
> Hugo,
>
> Any followups to Stefan's comments?
>
> > Cc: 72460@debbugs.gnu.org
> > Date: Sat, 31 Aug 2024 09:56:58 -0400
> > From: Stefan Monnier via "Bug reports for GNU Emacs,
> > the Swiss army knife of text editors" <bug-gnu-emacs@gnu.org>
> >
> > >> > +(defcustom key-translate-selection-function #'key-select-translation
> > >> Do we need this?
> > > Short answer: yes.
> > >
> > > I wanted a way for users to customise how the current translation
> > > pairs are presented when they choose one to remove. The initial
> > > motivation was that some users might want to customise the formatting
> > > of the pairs; "X -> Y" vs "X → Y" vs "X Y" etc. (I'm the sort of user
> > > who cares about this sort of thing a lot).
> >
> > Rewriting a whole new `key-translate-selection-function` function for
> > that is not significantly easier than rewriting a whole new
> > `key-translate-remove`, so I think this is firmly in the overengineering
> > side of the camp.
> >
> > > I also envisage that many users will want to use/expect
> > > completing-read (as in the default) and will want the from/to elements
> > > vertically aligned (again, I am this user).
> >
> > AFAICT, you're the first user to propose this functionality in Emacs's
> > 40 years life, so I wouldn't bet on "many users".
> >
> > BTW, I just noticed:
> >
> > > + ;; Populate collection
> > > + (map-char-table format-func keyboard-translate-table)
> > > + (get-text-property 0 'key-translate-from
> > > + (completing-read "Key Translation: " collection))))
> >
> > You can't trust `get-text-property` here (especially since you don't
> > even force `completing-read` to `require-match`).
> > Instead you should do something like `key-parse`.
> >
> > > I think here I was just copying the current calling convention for the
> > > existing (non-interactive) `key-translate'. Its docstring reads:
> > >
> > >> Both FROM and TO should be specified by strings that satisfy
> > >> `key-valid-p'.
> > >
> > > I sort of assumed someone cleverer than me had a good reason.
> >
> > I suspect this is because we expect this function to be called from
> > a `~/.emacs` with a literal constant as argument, and we've tried to
> > unify on using the "KBD" syntax for those cases, since it's a friendlier
> > syntax than the "vector of events" where you need to distinguish between
> > symbols and chars with/without modifiers.
> >
> > Since the arg of `key-translate` can be only a valid char (rather than
> > any arbitrary key potentially with modifiers), the argument is much less
> > strong, but my guess is that it was uniformity.
> >
> > In the case of `key-translate-remove` I don't expect the arg to be
> > a literal constant in the code, so "nice/uniform syntax to write the
> > argument value" shouldn't be a concern. 🙂
> >
> >
> > Stefan
> >
> >
> >
> >
> >
>
>
>
>
^ permalink raw reply [flat|nested] 11+ messages in thread
* bug#72460: [patch] add commands for setting keyboard translations interactively
2024-09-28 8:49 ` Eli Zaretskii
@ 2024-09-28 15:33 ` hugo
2024-09-28 16:02 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
0 siblings, 1 reply; 11+ messages in thread
From: hugo @ 2024-09-28 15:33 UTC (permalink / raw)
To: Eli Zaretskii; +Cc: 72460, monnier
[-- Attachment #1: Type: text/plain, Size: 1400 bytes --]
Hi all,
sorry, I was working on this last night , had the patch sorted, and
then somehow my browser crashed and I couldn't get to my email.
Apologies for all the delays.
> Rewriting a whole new `key-translate-selection-function` function for
> that is not significantly easier than rewriting a whole new
> `key-translate-remove`, so I think this is firmly in the
> overengineering
> side of the camp.
Fair enough, I've got rid of the defcustom.
> You can't trust `get-text-property` here (especially since you don't
> even force `completing-read` to `require-match`).
> Instead you should do something like `key-parse`.
Good spot.
> In the case of `key-translate-remove` I don't expect the arg to be
> a literal constant in the code, so "nice/uniform syntax to write the
> argument value" shouldn't be a concern. 🙂
Nice, removing the defcustom makes the whole thing simpler anyway.
The way it works now is that `key-select-translation' returns a string
(which passes `key-valid-p'). I went with this because then it's very
straightforward to pass the result to `key-translate' in
`key-translate-remove'. The alternative would be to parse a key vector
out of the string at the end of `key-select-translation', but then
immediately convert this back into a string (a string like the one I
went with in the end) in the interactive form of `key-translate-remove'.
New patch attached.
Hugo
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Add-commands-to-interactively-set-unset-keyboard-tra.patch --]
[-- Type: text/x-diff; name=0001-Add-commands-to-interactively-set-unset-keyboard-tra.patch, Size: 3338 bytes --]
From 56e9270dc202b940d18f5c34807a6f719faa9909 Mon Sep 17 00:00:00 2001
From: Hugo Heagren <hugo@heagren.com>
Date: Sun, 4 Aug 2024 12:54:27 +0100
Subject: [PATCH] Add commands to interactively set/unset keyboard translations
* lisp/keymap.el (key-translate): Add an interactive form, prompting for
keys to translate, and update docstring to reflect this.
(key-translate-selection-function): New custom option.
(key-select-translation): New function, default value of above option.
(key-translate-remove): New command, for removing entries from
`keyboard-translate-table'.
---
lisp/keymap.el | 51 +++++++++++++++++++++++++++++++++++++++++++++++++-
1 file changed, 50 insertions(+), 1 deletion(-)
diff --git a/lisp/keymap.el b/lisp/keymap.el
index 861d6724c9e..4e1deecff51 100644
--- a/lisp/keymap.el
+++ b/lisp/keymap.el
@@ -392,9 +392,16 @@ key-translate
and then modifies one entry in it.
Both FROM and TO should be specified by strings that satisfy `key-valid-p'.
-If TO is nil, remove any existing translation for FROM."
+If TO is nil, remove any existing translation for FROM.
+
+Interactively, prompt for FROM and TO with `read-char'."
(declare (compiler-macro
(lambda (form) (keymap--compile-check from to) form)))
+ ;; Using `key-description' is a necessary evil here, so that the
+ ;; values can be passed to keymap-* functions, even though those
+ ;; functions immediately undo it with `key-parse'.
+ (interactive `(,(key-description `[,(read-char "From: ")])
+ ,(key-description `[,(read-char "To: ")])))
(keymap--check from)
(when to
(keymap--check to))
@@ -417,6 +424,48 @@ key-translate
(aref from-key 0)
(and to (aref to-key 0)))))
+(defun key-select-translation ()
+ "Prompt for a current keyboard translation pair with `'completing-read'.
+
+Each pair is formatted as \"FROM -> TO\".
+
+Return the \"FROM\" as a key string."
+ (let* ((minibuffer-allow-text-properties t)
+ (collection)
+ ;; Alignment helpers
+ (pad 0)
+ (_ (map-char-table
+ (lambda (k _)
+ (when (> (length (key-description `[,k])) pad)
+ (setq pad (length (key-description `[,k])))))
+ keyboard-translate-table
+ ))
+ (format-func
+ (lambda (k v)
+ (setq collection
+ (cons
+ (propertize
+ (format
+ "%s -> %s"
+ (string-pad (key-description `[,k]) pad)
+ (key-description `[,v]))
+ 'key-translate-from k)
+ collection)))))
+ ;; Populate collection
+ (map-char-table format-func keyboard-translate-table)
+ (car
+ (split-string
+ (completing-read
+ "Key Translation: " collection nil t)))))
+
+(defun key-translate-remove (from)
+ "Remove translation of FROM from `keyboard-translate-table'.
+
+FROM must satisfy `key-valid-p'. If FROM has no entry in
+`keyboard-translate-table', this has no effect."
+ (interactive (list (key-select-translation)))
+ (key-translate from nil))
+
(defun keymap-lookup (keymap key &optional accept-default no-remap position)
"Return the binding for command KEY in KEYMAP.
KEY is a string that satisfies `key-valid-p'.
--
2.20.1
^ permalink raw reply related [flat|nested] 11+ messages in thread
* bug#72460: [patch] add commands for setting keyboard translations interactively
2024-09-28 15:33 ` hugo
@ 2024-09-28 16:02 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-10-03 2:45 ` hugo
0 siblings, 1 reply; 11+ messages in thread
From: Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors @ 2024-09-28 16:02 UTC (permalink / raw)
To: hugo; +Cc: 72460, Eli Zaretskii
> New patch attached.
Thanks Hugo, LGTM.
Stefan
^ permalink raw reply [flat|nested] 11+ messages in thread
* bug#72460: [patch] add commands for setting keyboard translations interactively
2024-09-28 16:02 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
@ 2024-10-03 2:45 ` hugo
2024-10-03 15:56 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
0 siblings, 1 reply; 11+ messages in thread
From: hugo @ 2024-10-03 2:45 UTC (permalink / raw)
To: Stefan Monnier; +Cc: 72460, Eli Zaretskii
[-- Attachment #1: Type: text/plain, Size: 673 bytes --]
Hi all,
following some short comments from Stefan, a better patch is now
attached. The new version accounts for the case where multiple
adjacent characters have the same translation and are thus presented
as a range in `map-char-table'. I've dealt with this by displaying
each one individually in the prompt, since:
- the user probably *initially set* them separately if they are using
the interactive functions
- if they aren't using the interactive functions, the prompt doesn't
matter
Since this is the only substantive change since I was last told this
looked good, I've also included a NEWS entry.
I have already assigned my copyright to the FSF.
Best,
Hugo
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Add-commands-to-interactively-set-unset-keyboard-tra.patch --]
[-- Type: text/x-diff; name=0001-Add-commands-to-interactively-set-unset-keyboard-tra.patch, Size: 4151 bytes --]
From 7f4867ecbd971e1d13868e99f3aea5d9fa4b07c3 Mon Sep 17 00:00:00 2001
From: Hugo Heagren <hugo@heagren.com>
Date: Sun, 4 Aug 2024 12:54:27 +0100
Subject: [PATCH] Add commands to interactively set/unset keyboard translations
* lisp/keymap.el (key-translate): Add an interactive form, prompting for
keys to translate, and update docstring to reflect this.
(key-translate-selection-function): New custom option.
(key-select-translation): New function, default value of above option.
(key-translate-remove): New command, for removing entries from
`keyboard-translate-table'.
---
etc/NEWS | 5 +++++
lisp/keymap.el | 58 +++++++++++++++++++++++++++++++++++++++++++++++++-
2 files changed, 62 insertions(+), 1 deletion(-)
diff --git a/etc/NEWS b/etc/NEWS
index f10f9ae4d65..3149b99db66 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -32,6 +32,11 @@ applies, and please also update docstrings as needed.
\f
* Editing Changes in Emacs 31.1
+** Commands for keyboard translation
+`key-translate' is now interactive. It prompts for a key to translate
+from, and another to translate to, and sets `keyboard-translate-table'.
+The new command `key-translate-remove' prompts for a key/translation
+pair with completing-read, and removes it from the translation table.
\f
* Changes in Specialized Modes and Packages in Emacs 31.1
diff --git a/lisp/keymap.el b/lisp/keymap.el
index 861d6724c9e..b65f34f96bf 100644
--- a/lisp/keymap.el
+++ b/lisp/keymap.el
@@ -392,9 +392,16 @@ key-translate
and then modifies one entry in it.
Both FROM and TO should be specified by strings that satisfy `key-valid-p'.
-If TO is nil, remove any existing translation for FROM."
+If TO is nil, remove any existing translation for FROM.
+
+Interactively, prompt for FROM and TO with `read-char'."
(declare (compiler-macro
(lambda (form) (keymap--compile-check from to) form)))
+ ;; Using `key-description' is a necessary evil here, so that the
+ ;; values can be passed to keymap-* functions, even though those
+ ;; functions immediately undo it with `key-parse'.
+ (interactive `(,(key-description `[,(read-char "From: ")])
+ ,(key-description `[,(read-char "To: ")])))
(keymap--check from)
(when to
(keymap--check to))
@@ -417,6 +424,55 @@ key-translate
(aref from-key 0)
(and to (aref to-key 0)))))
+(defun key-select-translation ()
+ "Prompt for a current keyboard translation pair with `completing-read'.
+
+Each pair is formatted as \"FROM -> TO\".
+
+Return the \"FROM\" as a key string."
+ (let* ((formatted-trans-alist nil)
+ ;; Alignment helpers
+ (pad 0)
+ (key-code-func
+ (lambda (kc trans)
+ (let* ((desc (key-description `[,kc]))
+ (len (length desc)))
+ (when (> len pad)
+ (setq pad len))
+ (push
+ `(,desc . ,(key-description `[,trans]))
+ formatted-trans-alist))))
+ (format-func
+ (lambda (pair) ;; (key . value)
+ (format
+ "%s -> %s"
+ (string-pad (key-description `[,(car pair)]) pad)
+ (key-description `[,(cdr pair)])))))
+ ;; Set `pad' and `formatted-trans-alist'
+ (map-char-table
+ (lambda (chr trans)
+ (if (characterp chr)
+ (funcall key-code-func chr trans)
+ (require 'range)
+ (range-map
+ (lambda (kc) (funcall key-code-func kc trans))
+ chr)))
+ keyboard-translate-table)
+ (car
+ (split-string
+ (completing-read
+ "Key Translation: "
+ (mapcar format-func formatted-trans-alist)
+ nil t)))))
+
+(defun key-translate-remove (from)
+ "Remove translation of FROM from `keyboard-translate-table'.
+
+FROM must satisfy `key-valid-p'. If FROM has no entry in
+`keyboard-translate-table', this has no effect."
+ (interactive (list (key-select-translation)))
+ (key-translate from nil))
+
(defun keymap-lookup (keymap key &optional accept-default no-remap position)
"Return the binding for command KEY in KEYMAP.
KEY is a string that satisfies `key-valid-p'.
--
2.20.1
^ permalink raw reply related [flat|nested] 11+ messages in thread
* bug#72460: [patch] add commands for setting keyboard translations interactively
2024-10-03 2:45 ` hugo
@ 2024-10-03 15:56 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
0 siblings, 0 replies; 11+ messages in thread
From: Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors @ 2024-10-03 15:56 UTC (permalink / raw)
To: hugo; +Cc: 72460, Eli Zaretskii
> Since this is the only substantive change since I was last told this
> looked good, I've also included a NEWS entry.
Thank you. Pushed to `master`!
Stefan
^ permalink raw reply [flat|nested] 11+ messages in thread
end of thread, other threads:[~2024-10-03 15:56 UTC | newest]
Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-08-04 12:16 bug#72460: [patch] add commands for setting keyboard translations interactively hugo
2024-08-17 8:46 ` Eli Zaretskii
2024-08-19 14:42 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-08-31 8:08 ` Eli Zaretskii
[not found] ` <ed0eede1b3ac4e6da54c2449e3c8ea4f@heagren.com>
2024-08-31 13:56 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-09-14 7:38 ` Eli Zaretskii
2024-09-28 8:49 ` Eli Zaretskii
2024-09-28 15:33 ` hugo
2024-09-28 16:02 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-10-03 2:45 ` hugo
2024-10-03 15:56 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.