all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* 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

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.