From 856e8ee2459de8843b1c0efc0d05b576fccd3d2d Mon Sep 17 00:00:00 2001 From: John Wiegley Date: Thu, 25 Feb 2016 15:57:50 -0800 Subject: [PATCH] Support multiples uses of :map with :bind GitHub-reference: fixes https://github.com/jwiegley/use-package/issues/121 --- lisp/use-package/bind-key.el | 78 +++++++++++++++++++++++---------- lisp/use-package/use-package.el | 4 +- 2 files changed, 57 insertions(+), 25 deletions(-) diff --git a/lisp/use-package/bind-key.el b/lisp/use-package/bind-key.el index 65f40f7901a..c74e7b6a753 100644 --- a/lisp/use-package/bind-key.el +++ b/lisp/use-package/bind-key.el @@ -190,8 +190,7 @@ See `bind-key' for more details." "Similar to `bind-key', but overrides any mode-specific bindings." `(bind-key ,key-name ,command override-global-map ,predicate)) -;;;###autoload -(defmacro bind-keys (&rest args) +(defun bind-keys-form (args) "Bind multiple keys at once. Accepts keyword arguments: @@ -223,33 +222,64 @@ function symbol (unquoted)." (error "Both :prefix-map and :prefix must be supplied")) (when (and menu-name (not prefix)) (error "If :menu-name is supplied, :prefix must be too")) - (macroexp-progn - (append - (when prefix-map - `((defvar ,prefix-map) - ,@(when doc `((put ',prefix-map 'variable-documentation ,doc))) - ,@(if menu-name - `((define-prefix-command ',prefix-map nil ,menu-name)) - `((define-prefix-command ',prefix-map))) - ,@(if maps + (let ((args key-bindings) + first next) + (while args + (if (keywordp (car args)) + (progn + (setq next args) + (setq args nil)) + (if first + (nconc first (list (car args))) + (setq first (list (car args)))) + (setq args (cdr args)))) + (append + (when prefix-map + `((defvar ,prefix-map) + ,@(when doc `((put ',prefix-map 'variable-documentation ,doc))) + ,@(if menu-name + `((define-prefix-command ',prefix-map nil ,menu-name)) + `((define-prefix-command ',prefix-map))) + ,@(if maps + (mapcar + #'(lambda (m) + `(bind-key ,prefix ',prefix-map ,m ,filter)) maps) + `((bind-key ,prefix ',prefix-map nil ,filter))))) + (cl-mapcan + (lambda (form) + (if prefix-map + `((bind-key ,(car form) ',(cdr form) ,prefix-map ,filter)) + (if maps (mapcar #'(lambda (m) - `(bind-key ,prefix ',prefix-map ,m ,filter)) maps) - `((bind-key ,prefix ',prefix-map nil ,filter))))) - (cl-mapcan - (lambda (form) - (if prefix-map - `((bind-key ,(car form) ',(cdr form) ,prefix-map ,filter)) - (if maps - (mapcar - #'(lambda (m) - `(bind-key ,(car form) ',(cdr form) ,m ,filter)) maps) - `((bind-key ,(car form) ',(cdr form) nil ,filter))))) - key-bindings))))) + `(bind-key ,(car form) ',(cdr form) ,m ,filter)) maps) + `((bind-key ,(car form) ',(cdr form) nil ,filter))))) + first) + (when next + (bind-keys-form next)))))) + +;;;###autoload +(defmacro bind-keys (&rest args) + "Bind multiple keys at once. + +Accepts keyword arguments: +:map MAP - a keymap into which the keybindings should be + added +:prefix KEY - prefix key for these bindings +:prefix-map MAP - name of the prefix map that should be created + for these bindings +:prefix-docstring STR - docstring for the prefix-map variable +:menu-name NAME - optional menu string for prefix map +:filter FORM - optional form to determine when bindings apply + +The rest of the arguments are conses of keybinding string and a +function symbol (unquoted)." + (macroexp-progn (bind-keys-form args))) ;;;###autoload (defmacro bind-keys* (&rest args) - `(bind-keys :map override-global-map ,@args)) + (macroexp-progn + (bind-keys-form (cons :map (cons override-global-map args))))) (defun get-binding-description (elem) (cond diff --git a/lisp/use-package/use-package.el b/lisp/use-package/use-package.el index baa50b0270f..15da2452f1a 100644 --- a/lisp/use-package/use-package.el +++ b/lisp/use-package/use-package.el @@ -698,7 +698,9 @@ may also be a string, as accepted by `define-key'." (use-package-sort-keywords (use-package-plist-maybe-put rest :defer t)) (use-package-plist-append state :commands commands)) - `((ignore (,(if bind-macro bind-macro 'bind-keys) ,@arg)))))) + `((ignore + ,(macroexpand + `(,(if bind-macro bind-macro 'bind-keys) ,@arg))))))) (defun use-package-handler/:bind* (name keyword arg rest state) (use-package-handler/:bind name keyword arg rest state 'bind-keys*)) -- 2.39.2