From 2203246454fddd41e0a62e78b17befce561998b9 Mon Sep 17 00:00:00 2001 From: Hugo Heagren Date: Sun, 16 Jan 2022 00:21:36 +0000 Subject: [PATCH] bind-keys-form: new keyword :repeat-map, for defining repeat maps use-package-normalize/:bind: allow keyword :repeat-map. bind-keys-form: Add keyword :repeat-map. Specifying a symbol as the repeat-map defines a keymap with that name (and with the docstring `repeat-doc', if specified). Symbols for functions bound to keys under the scope of :repeat-map have their 'repeat-map property set to this map. Update docstring (and that of `bind-keys') to reflect changes. Rename `doc' to `prefix-doc' for clarity and consistency with 'repeat-doc'. --- lisp/use-package/bind-key.el | 36 +++++++++++++++++++++--- lisp/use-package/use-package-bind-key.el | 4 +++ 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/lisp/use-package/bind-key.el b/lisp/use-package/bind-key.el index 1d611c2933c..bb09a6935a8 100644 --- a/lisp/use-package/bind-key.el +++ b/lisp/use-package/bind-key.el @@ -257,14 +257,22 @@ Accepts keyword arguments: for these bindings :prefix-docstring STR - docstring for the prefix-map variable :menu-name NAME - optional menu string for prefix map +:repeat-docstring STR - docstring for the repeat-map variable +:repeat-map MAP - name of the repeat map that should be created + for these bindings. If specified, the + 'repeat-map property of each command bound + (within the scope of the :repeat-map keyword) + is set to this 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)." (let (map - doc + prefix-doc prefix-map prefix + repeat-map + repeat-doc filter menu-name pkg) @@ -276,11 +284,18 @@ function symbol (unquoted)." (not prefix-map)) (setq map (cadr args))) ((eq :prefix-docstring (car args)) - (setq doc (cadr args))) + (setq prefix-doc (cadr args))) ((and (eq :prefix-map (car args)) (not (memq map '(global-map override-global-map)))) (setq prefix-map (cadr args))) + ((eq :repeat-docstring (car args)) + (setq repeat-doc (cadr args))) + ((and (eq :repeat-map (car args)) + (not (memq map '(global-map + override-global-map)))) + (setq repeat-map (cadr args)) + (setq map repeat-map)) ((eq :prefix (car args)) (setq prefix (cadr args))) ((eq :filter (car args)) @@ -327,13 +342,16 @@ function symbol (unquoted)." (append (when prefix-map `((defvar ,prefix-map) - ,@(when doc `((put ',prefix-map 'variable-documentation ,doc))) + ,@(when prefix-doc `((put ',prefix-map 'variable-documentation ,prefix-doc))) ,@(if menu-name `((define-prefix-command ',prefix-map nil ,menu-name)) `((define-prefix-command ',prefix-map))) ,@(if (and map (not (eq map 'global-map))) (wrap map `((bind-key ,prefix ',prefix-map ,map ,filter))) `((bind-key ,prefix ',prefix-map nil ,filter))))) + (when repeat-map + `((defvar ,repeat-map (make-sparse-keymap) + ,@(when repeat-doc `(,repeat-doc))))) (wrap map (cl-mapcan (lambda (form) @@ -341,7 +359,11 @@ function symbol (unquoted)." (if prefix-map `((bind-key ,(car form) ,fun ,prefix-map ,filter)) (if (and map (not (eq map 'global-map))) - `((bind-key ,(car form) ,fun ,map ,filter)) + ;; Only needed in this branch, since when + ;; repeat-map is non-nil, map is always + ;; non-nil + `(,@(when repeat-map `((put ,fun 'repeat-map ',repeat-map))) + (bind-key ,(car form) ,fun ,map ,filter)) `((bind-key ,(car form) ,fun nil ,filter)))))) first)) (when next @@ -361,6 +383,12 @@ Accepts keyword arguments: for these bindings :prefix-docstring STR - docstring for the prefix-map variable :menu-name NAME - optional menu string for prefix map +:repeat-docstring STR - docstring for the repeat-map variable +:repeat-map MAP - name of the repeat map that should be created + for these bindings. If specified, the + 'repeat-map property of each command bound + (within the scope of the :repeat-map keyword) + is set to this map. :filter FORM - optional form to determine when bindings apply The rest of the arguments are conses of keybinding string and a diff --git a/lisp/use-package/use-package-bind-key.el b/lisp/use-package/use-package-bind-key.el index e476b060ad6..d056d4266cc 100644 --- a/lisp/use-package/use-package-bind-key.el +++ b/lisp/use-package/use-package-bind-key.el @@ -86,6 +86,8 @@ deferred until the prefix key sequence is pressed." ;; :prefix-docstring STRING ;; :prefix-map SYMBOL ;; :prefix STRING + ;; :repeat-docstring STRING + ;; :repeat-map SYMBOL ;; :filter SEXP ;; :menu-name STRING ;; :package SYMBOL @@ -93,6 +95,8 @@ deferred until the prefix key sequence is pressed." (and (eq x :prefix) (stringp (cadr arg))) (and (eq x :prefix-map) (symbolp (cadr arg))) (and (eq x :prefix-docstring) (stringp (cadr arg))) + (and (eq x :repeat-map) (symbolp (cadr arg))) + (and (eq x :repeat-docstring) (stringp (cadr arg))) (eq x :filter) (and (eq x :menu-name) (stringp (cadr arg))) (and (eq x :package) (symbolp (cadr arg)))) -- 2.39.2