summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Wiegley <johnw@newartisans.com>2014-02-16 23:04:18 -0600
committerJohn Wiegley <johnw@newartisans.com>2014-02-16 23:04:18 -0600
commitaec1268960ff1161d7339139ba6b88175bc6fd70 (patch)
tree284917e4875ed9d80721a8c034c923cf58fb1e3e
parentdd937c4e3637dbd80451b17267bdb4954e7470dc (diff)
parent7d0779114ed82fa893260910edd67c26624fa91a (diff)
downloademacs-aec1268960.tar.gz
Merge pull request from Fuco1/bind-keys
Add `bind-keys` macro GitHub-reference: https://github.com/jwiegley/use-package/issues/87
-rw-r--r--lisp/use-package/bind-key.el61
1 files changed, 61 insertions, 0 deletions
diff --git a/lisp/use-package/bind-key.el b/lisp/use-package/bind-key.el
index 36de06023dc..c15a4321ef9 100644
--- a/lisp/use-package/bind-key.el
+++ b/lisp/use-package/bind-key.el
@@ -51,6 +51,28 @@
;;
;; (unbind-key "C-c x" some-other-mode-map)
;;
+;; To bind multiple keys at once, or set up a prefix map, a
+;; `bind-keys' macro is provided. It accepts keyword arguments, see
+;; its documentation for detailed description.
+;;
+;; To add keys into a specific map, use :map argument
+;;
+;; (bind-keys :map dired-mode-map
+;; ("o" . dired-omit-mode)
+;; ("a" . some-custom-dired-function))
+;;
+;; To set up a prefix map, use :prefix-map and :prefix
+;; arguments (both are required)
+;;
+;; (bind-keys :prefix-map my-customize-prefix-map
+;; :prefix "C-c c"
+;; ("f" . customize-face)
+;; ("v" . customize-variable))
+;;
+;; You can combine all the keywords together.
+;; Additionally, :prefix-docstring can be specified to set
+;; documentation of created :prefix-map variable.
+;;
;; After Emacs loads, you can see a summary of all your personal keybindings
;; currently in effect with this command:
;;
@@ -118,6 +140,45 @@
(bind-key ,key-name ,command)
(define-key override-global-map ,(read-kbd-macro key-name) ,command)))
+(defmacro bind-keys (&rest args)
+ "Bind multiple keys at once.
+
+Accepts keyword arguments:
+:map - a keymap into which the keybindings should be added
+:prefix-map - name of the prefix map that should be created for
+ these bindings
+:prefix - prefix key for these bindings
+:prefix-docstring - docstring for the prefix-map variable
+
+The rest of the arguments are conses of keybinding string and a
+function symbol (unquoted)."
+ (let ((map (plist-get args :map))
+ (doc (plist-get args :prefix-docstring))
+ (prefix-map (plist-get args :prefix-map))
+ (prefix (plist-get args :prefix))
+ (key-bindings (progn
+ (while (keywordp (car args))
+ (pop args)
+ (pop args))
+ args)))
+ (when (or (and prefix-map
+ (not prefix))
+ (and prefix
+ (not prefix-map)))
+ (error "Both :prefix-map and :prefix must be supplied"))
+ `(progn
+ ,@(when prefix-map
+ `((defvar ,prefix-map)
+ ,@(when doc `((put ',prefix-map'variable-documentation ,doc)))
+ (define-prefix-command ',prefix-map)
+ (bind-key ,prefix ',prefix-map ,@(when map (list map)))))
+ ,@(mapcar (lambda (form) `(bind-key ,(if prefix
+ (concat prefix " " (car form))
+ (car form))
+ ',(cdr form)
+ ,@(when map (list map))))
+ key-bindings))))
+
(defun get-binding-description (elem)
(cond
((listp elem)