about summary refs log tree commit diff
path: root/exwm-input.el
diff options
context:
space:
mode:
authorChris Feng <chris.w.feng@gmail.com>2018-02-25T16·19+0800
committerChris Feng <chris.w.feng@gmail.com>2018-02-25T16·19+0800
commit7cef4320cca8aeba5c4c6df37842bd73f16996d9 (patch)
treec3767c9e3cd09a2cf667ccd5343a0fb7e70a1c50 /exwm-input.el
parenta50058be786bfa58d24c839dd1c9b9267f10003f (diff)
Improve the Customize interface for simulation keys
* exwm-input.el (exwm-input--set-simulation-keys): New function for
actullay setting simulation keys.
(exwm-input-simulation-keys, exwm-input-set-local-simulation-keys)
(exwm-input--init): Use this instead of
`exwm-input-set-simulation-keys'.
(exwm-input-set-simulation-keys): Make obsolete.
(exwm-input--read-keys): New function for reading an arbitrary key
sequence.
(exwm-input-set-simulation-key): New command for setting a new
simulation key (which can be saved in the Customize interface).
Diffstat (limited to 'exwm-input.el')
-rw-r--r--exwm-input.el84
1 files changed, 56 insertions, 28 deletions
diff --git a/exwm-input.el b/exwm-input.el
index edb7eb390cfa..fa9c1eb1d53d 100644
--- a/exwm-input.el
+++ b/exwm-input.el
@@ -754,30 +754,20 @@ multiple keys."
       (setq keys (vconcat keys (vector key)))
       (exwm-input--fake-key key))))
 
-;; (defun exwm-input-send-last-key ()
-;;   (interactive)
-;;   (unless (listp last-input-event)      ;not a key event
-;;     (exwm-input--fake-key last-input-event)))
-
-(defun exwm-input-set-simulation-keys (simulation-keys)
-  "Set simulation keys.
-
-SIMULATION-KEYS is an alist of the form (original-key . simulated-key),
-where both original-key and simulated-key are key sequences.
-
-Simulation keys set this way take effect in real time.  For configuration
-it's recommended to customize or set `exwm-input-simulation-keys' instead."
-  ;; Clear keymaps and the hash table.
-  (when (hash-table-p exwm-input--simulation-keys)
-    (maphash (lambda (key _value)
-               (when (sequencep key)
-                 (if exwm-input--local-simulation-keys
-                     (local-unset-key key)
-                   (define-key exwm-mode-map key nil))))
-             exwm-input--simulation-keys)
-    (clrhash exwm-input--simulation-keys))
-  ;; Update the hash table.
-  (setq exwm-input--simulation-keys (make-hash-table :test #'equal))
+(defun exwm-input--set-simulation-keys (simulation-keys &optional no-refresh)
+  "Set simulation keys."
+  (unless no-refresh
+    ;; Clear keymaps and the hash table.
+    (when (hash-table-p exwm-input--simulation-keys)
+      (maphash (lambda (key _value)
+                 (when (sequencep key)
+                   (if exwm-input--local-simulation-keys
+                       (local-unset-key key)
+                     (define-key exwm-mode-map key nil))))
+               exwm-input--simulation-keys)
+      (clrhash exwm-input--simulation-keys))
+    ;; Update the hash table.
+    (setq exwm-input--simulation-keys (make-hash-table :test #'equal)))
   (dolist (i simulation-keys)
     (let ((original (vconcat (car i)))
           (simulated (cdr i)))
@@ -798,6 +788,11 @@ it's recommended to customize or set `exwm-input-simulation-keys' instead."
                    #'exwm-input-send-simulation-key))))
            exwm-input--simulation-keys))
 
+(defun exwm-input-set-simulation-keys (simulation-keys)
+  "Please customize or set `exwm-input-simulation-keys' instead."
+  (declare (obsolete nil "26"))
+  (exwm-input--set-simulation-keys simulation-keys))
+
 (defcustom exwm-input-simulation-keys nil
   "Simulation keys.
 
@@ -833,7 +828,39 @@ Notes:
                                     (key-sequence :tag "User-defined")))
   :set (lambda (symbol value)
          (set symbol value)
-         (exwm-input-set-simulation-keys value)))
+         (exwm-input--set-simulation-keys value)))
+
+(cl-defun exwm-input--read-keys (prompt stop-key)
+  (let ((cursor-in-echo-area t)
+        keys key)
+    (while (not (eq key stop-key))
+      (setq key (read-key (format "%s (terminate with %s): %s"
+                                  prompt
+                                  (key-description (vector stop-key))
+                                  (key-description keys)))
+            keys (vconcat keys (vector key))))
+    (substring keys 0 -1)))
+
+;;;###autoload
+(defun exwm-input-set-simulation-key (original-key simulated-key)
+  "Set a simulation key.
+
+The simulation key takes effect in real time, but is lost when this session
+ends unless it's specifically saved in the Customize interface for
+`exwm-input-simulation-keys'."
+  (interactive
+   (let (original simulated)
+     (setq original (exwm-input--read-keys "Original keys" ?\C-g))
+     (when original
+       (setq simulated (exwm-input--read-keys
+                        (format "Simulate %s as" (key-description original))
+                        ?\C-g)))
+     (list original simulated)))
+  (when (and original-key simulated-key)
+    (let ((entry `((,original-key . ,simulated-key))))
+      (setq exwm-input-simulation-keys (append exwm-input-simulation-keys
+                                               entry))
+      (exwm-input--set-simulation-keys entry t))))
 
 (defun exwm-input--unset-simulation-keys ()
   "Clear simulation keys and key bindings defined."
@@ -847,11 +874,12 @@ Notes:
 (defun exwm-input-set-local-simulation-keys (simulation-keys)
   "Set buffer-local simulation keys.
 
-Its usage is the same with `exwm-input-set-simulation-keys'."
+SIMULATION-KEYS is an alist of the form (original-key . simulated-key),
+where both ORIGINAL-KEY and SIMULATED-KEY are key sequences."
   (make-local-variable 'exwm-input--simulation-keys)
   (use-local-map (copy-keymap exwm-mode-map))
   (let ((exwm-input--local-simulation-keys t))
-    (exwm-input-set-simulation-keys simulation-keys)))
+    (exwm-input--set-simulation-keys simulation-keys)))
 
 ;;;###autoload
 (cl-defun exwm-input-send-simulation-key (times)
@@ -906,7 +934,7 @@ Its usage is the same with `exwm-input-set-simulation-keys'."
     (exwm-input--set-key (car i) (cdr i)))
   ;; Initialize simulation keys.
   (when exwm-input-simulation-keys
-    (exwm-input-set-simulation-keys exwm-input-simulation-keys))
+    (exwm-input--set-simulation-keys exwm-input-simulation-keys))
   ;; Attach event listeners
   (xcb:+event exwm--connection 'xcb:PropertyNotify
               #'exwm-input--on-PropertyNotify)