all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: hugo@heagren.com
To: Stefan Monnier <monnier@iro.umontreal.ca>
Cc: 72460@debbugs.gnu.org, Eli Zaretskii <eliz@gnu.org>
Subject: bug#72460: [patch] add commands for setting keyboard translations interactively
Date: Thu, 03 Oct 2024 03:45:02 +0100	[thread overview]
Message-ID: <57d837226be0ce82a6340658b1297445@heagren.com> (raw)
In-Reply-To: <jwvjzevydf3.fsf-monnier+emacs@gnu.org>

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


  reply	other threads:[~2024-10-03  2:45 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
2024-10-03 15:56                 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=57d837226be0ce82a6340658b1297445@heagren.com \
    --to=hugo@heagren.com \
    --cc=72460@debbugs.gnu.org \
    --cc=eliz@gnu.org \
    --cc=monnier@iro.umontreal.ca \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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.