From 65caa3b423e9a535568d3a60551f412916ce4c1f Mon Sep 17 00:00:00 2001 From: John Wiegley Date: Tue, 5 Dec 2017 13:11:30 -0800 Subject: [PATCH] Rewrite normalization of :bind and :bind* Fixes https://github.com/jwiegley/use-package/issues/550 --- lisp/use-package/use-package-bind-key.el | 47 +++++-- test/lisp/use-package/use-package-tests.el | 153 +++++++++++++++++++-- 2 files changed, 175 insertions(+), 25 deletions(-) diff --git a/lisp/use-package/use-package-bind-key.el b/lisp/use-package/use-package-bind-key.el index 09229153f0c..7c5d2725301 100644 --- a/lisp/use-package/use-package-bind-key.el +++ b/lisp/use-package/use-package-bind-key.el @@ -67,17 +67,44 @@ deferred until the prefix key sequence is pressed." package keymap-symbol))))) (defun use-package-normalize-binder (name keyword args) - (use-package-as-one (symbol-name keyword) args - #'(lambda (label arg) - (unless (consp arg) + (let ((arg args) + args*) + (while arg + (let ((x (car arg))) + (cond + ;; (KEY . COMMAND) + ((and (consp x) + (or (stringp (car x)) + (vectorp (car x))) + (or (use-package-recognize-function (cdr x) t #'stringp))) + (setq args* (nconc args* (list x))) + (setq arg (cdr arg))) + ;; KEYWORD + ;; :map KEYMAP + ;; :prefix-docstring STRING + ;; :prefix-map SYMBOL + ;; :prefix STRING + ;; :filter SEXP + ;; :menu-name STRING + ((or (and (eq x :map) (symbolp (cadr arg))) + (and (eq x :prefix) (stringp (cadr arg))) + (and (eq x :prefix-map) (symbolp (cadr arg))) + (and (eq x :prefix-docstring) (stringp (cadr arg))) + (eq x :filter) + (and (eq x :menu-name) (stringp (cadr arg)))) + (setq args* (nconc args* (list x (cadr arg)))) + (setq arg (cddr arg))) + ((listp x) + (setq args* + (nconc args* (use-package-normalize-binder name keyword x))) + (setq arg (cdr arg))) + (t + ;; Error! (use-package-error - (concat label " a ( . )" - " or list of these"))) - (use-package-normalize-pairs - #'(lambda (k) (cond ((stringp k) t) - ((vectorp k) t))) - #'(lambda (v) (use-package-recognize-function v t #'stringp)) - name label arg)))) + (concat (symbol-name name) + " wants arguments acceptable to the `bind-keys' macro," + " or a list of such values")))))) + args*)) ;;;; :bind, :bind* diff --git a/test/lisp/use-package/use-package-tests.el b/test/lisp/use-package/use-package-tests.el index 4e65de082c1..966c1221ba4 100644 --- a/test/lisp/use-package/use-package-tests.el +++ b/test/lisp/use-package/use-package-tests.el @@ -546,26 +546,115 @@ (eval-when-compile (with-demoted-errors "Cannot load foo: %S" nil nil)))))) -(ert-deftest use-package-test-normalize/:bind () - (flet ((norm (&rest args) - (apply #'use-package-normalize-binder - 'foopkg :bind args))) - (let ((good-values '(:map map-sym - ("str" . sym) ("str" . "str") - ([vec] . sym) ([vec] . "str")))) - (should (equal (norm good-values) good-values))) - (should-error (norm '("foo"))) - (should-error (norm '("foo" . 99))) - (should-error (norm '(99 . sym))))) +(defun use-package-test-normalize-bind (&rest args) + (apply #'use-package-normalize-binder 'foo :bind args)) + +(ert-deftest use-package-test-normalize/:bind-1 () + (should (equal (use-package-test-normalize-bind + '(("C-a" . alpha))) + '(("C-a" . alpha))))) + +(ert-deftest use-package-test-normalize/:bind-2 () + (should (equal (use-package-test-normalize-bind + '(("C-a" . alpha) + :map foo-map + ("C-b" . beta))) + '(("C-a" . alpha) + :map foo-map + ("C-b" . beta))))) + +(ert-deftest use-package-test-normalize/:bind-3 () + (should (equal (use-package-test-normalize-bind + '(:map foo-map + ("C-a" . alpha) + ("C-b" . beta))) + '(:map foo-map + ("C-a" . alpha) + ("C-b" . beta))))) (ert-deftest use-package-test/:bind-1 () (match-expansion - (use-package foo :bind ("C-k" . key)) + (use-package foo :bind ("C-k" . key1) ("C-u" . key2)) `(progn - (unless (fboundp 'key) - (autoload #'key "foo" nil t)) + (unless + (fboundp 'key1) + (autoload #'key1 "foo" nil t)) + (unless + (fboundp 'key2) + (autoload #'key2 "foo" nil t)) + (ignore + (bind-keys :package foo + ("C-k" . key1) + ("C-u" . key2)))))) + +(ert-deftest use-package-test/:bind-2 () + (match-expansion + (use-package foo :bind (("C-k" . key1) ("C-u" . key2))) + `(progn + (unless (fboundp 'key1) + (autoload #'key1 "foo" nil t)) + (unless (fboundp 'key2) + (autoload #'key2 "foo" nil t)) + (ignore + (bind-keys :package foo + ("C-k" . key1) + ("C-u" . key2)))))) + +(ert-deftest use-package-test/:bind-3 () + (match-expansion + (use-package foo :bind (:map my-map ("C-k" . key1) ("C-u" . key2))) + `(progn + (unless + (fboundp 'key1) + (autoload #'key1 "foo" nil t)) + (unless + (fboundp 'key2) + (autoload #'key2 "foo" nil t)) + (ignore + (bind-keys :package foo :map my-map + ("C-k" . key1) + ("C-u" . key2)))))) + +(ert-deftest use-package-test/:bind-4 () + (should-error + (match-expansion + (use-package foo :bind :map my-map ("C-k" . key1) ("C-u" . key2)) + `(ignore + (bind-keys :package foo))))) + +(ert-deftest use-package-test/:bind-5 () + (match-expansion + (use-package foo :bind ("C-k" . key1) (:map my-map ("C-u" . key2))) + `(progn + (unless (fboundp 'key1) + (autoload #'key1 "foo" nil t)) + (unless (fboundp 'key2) + (autoload #'key2 "foo" nil t)) (ignore - (bind-keys :package foo ("C-k" . key)))))) + (bind-keys :package foo + ("C-k" . key1) + :map my-map + ("C-u" . key2)))))) + +(ert-deftest use-package-test/:bind-6 () + (match-expansion + (use-package foo + :bind + ("C-k" . key1) + (:map my-map ("C-u" . key2)) + (:map my-map2 ("C-u" . key3))) + `(progn + (unless (fboundp 'key1) + (autoload #'key1 "foo" nil t)) + (unless (fboundp 'key2) + (autoload #'key2 "foo" nil t)) + (unless (fboundp 'key3) + (autoload #'key3 "foo" nil t)) + (ignore + (bind-keys :package foo + ("C-k" . key1) + :map my-map ("C-u" . key2) + :map my-map2 ("C-u" . key3)))))) (ert-deftest use-package-test/:bind*-1 () (match-expansion @@ -1524,6 +1613,40 @@ (use-package-ensure-elpa 'hydra '(t) 'nil) (require 'hydra nil nil)))) +(ert-deftest use-package-test/545 () + (match-expansion + (use-package spacemacs-theme + :ensure t + :init ; or :config + (load-theme 'spacemacs-dark t) + ) + `(progn + (use-package-ensure-elpa 'spacemacs-theme '(t) 'nil) + (load-theme 'spacemacs-dark t) + (require 'spacemacs-theme nil nil)) + )) + +(ert-deftest use-package-test/550 () + (match-expansion + (use-package company-try-hard + :ensure t + :bind + ("C-c M-/" . company-try-hard) + (:map company-active-map + ("C-c M-/" . company-try-hard))) + `(progn + (use-package-ensure-elpa 'company-try-hard + '(t) + 'nil) + (unless + (fboundp 'company-try-hard) + (autoload #'company-try-hard "company-try-hard" nil t)) + (ignore + (bind-keys :package company-try-hard + ("C-c M-/" . company-try-hard) + :map company-active-map + ("C-c M-/" . company-try-hard)))))) + (ert-deftest use-package-test/558 () (match-expansion (bind-keys* :package org-ref -- 2.39.2