about summary refs log tree commit diff
diff options
context:
space:
mode:
authorChris Feng <chris.w.feng@gmail.com>2018-02-25T16·15+0800
committerChris Feng <chris.w.feng@gmail.com>2018-02-25T16·15+0800
commita50058be786bfa58d24c839dd1c9b9267f10003f (patch)
tree2c357bc4a4b0843c525a85387b5624a3a79094d0
parentc719c3f54b242b06df1a7a9db05db648fa26bc2b (diff)
Add Customize interface for global keys
* exwm-input.el (exwm-input--set-key): New function for actually
setting the binding.
(exwm-input-global-keys): New user option for customizing global keys.
(exwm-input-set-key): Update `exwm-input-global-keys' and call
`exwm-input--set-key'
(exwm-input--init): Initialize global keys.
-rw-r--r--exwm-input.el39
1 files changed, 36 insertions, 3 deletions
diff --git a/exwm-input.el b/exwm-input.el
index ea3d65934026..edb7eb390cfa 100644
--- a/exwm-input.el
+++ b/exwm-input.el
@@ -464,15 +464,45 @@ ARGS are additional arguments to CALLBACK."
           (xcb:+request exwm--connection req))))
     (xcb:flush exwm--connection)))
 
+(defun exwm-input--set-key (key command)
+  (global-set-key key command)
+  (cl-pushnew key exwm-input--global-keys))
+
+(defcustom exwm-input-global-keys nil
+  "Global keys.
+
+It is an alist of the form (key . command), meaning giving KEY (a key
+sequence) a global binding as COMMAND.
+
+Notes:
+* Setting the value directly (rather than customizing it) after EXWM
+  finishes initialization has no effect."
+  :type '(alist :key-type key-sequence :value-type function)
+  :set (lambda (symbol value)
+         (when (boundp symbol)
+           (dolist (i (symbol-value symbol))
+             (global-unset-key (car i))))
+         (set symbol value)
+         (setq exwm-input--global-keys nil)
+         (dolist (i value)
+           (exwm-input--set-key (car i) (cdr i)))
+         (when exwm--connection
+           (exwm-input--update-global-prefix-keys))))
+
 ;;;###autoload
 (defun exwm-input-set-key (key command)
   "Set a global key binding.
 
 The new key binding only takes effect in real time when this command is
-called interactively.  Only invoke it non-interactively in configuration."
+called interactively, and is lost when this session ends unless it's
+specifically saved in the Customize interface for `exwm-input-global-keys'.
+
+In configuration you should customize or set `exwm-input-global-keys'
+instead."
   (interactive "KSet key globally: \nCSet key %s to command: ")
-  (global-set-key key command)
-  (cl-pushnew key exwm-input--global-keys)
+  (setq exwm-input-global-keys (append exwm-input-global-keys
+                                       (list (cons key command))))
+  (exwm-input--set-key key command)
   (when (called-interactively-p 'any)
     (exwm-input--update-global-prefix-keys)))
 
@@ -871,6 +901,9 @@ Its usage is the same with `exwm-input-set-simulation-keys'."
                                          :name-len (length atom)
                                          :name atom))
                       'atom)))
+  ;; Initialize global keys.
+  (dolist (i exwm-input-global-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))