From 4b8b850cf0d814bda1e39d8ea276d8e93938d573 Mon Sep 17 00:00:00 2001 From: Joe Wreschnig Date: Sun, 2 Jul 2017 16:20:01 +0200 Subject: [PATCH] Allow multiple :delight arguments, or omitting the mode. () This allows using forms such as (use-package foo :delight) ;; => (delight 'foo-mode) (use-package foo :delight " f") ;; => (delight 'foo-mode " f") (use-package foo :delight (a-mode) (b-mode " b") ;; => (delight 'a-mode) (delight 'b-mode " b") This brings support for `:delight` in line with `:diminish`. GitHub-reference: https://github.com/jwiegley/use-package/issues/477 --- lisp/use-package/use-package.el | 51 ++++++++++++++++------ test/lisp/use-package/use-package-tests.el | 19 ++++++++ 2 files changed, 57 insertions(+), 13 deletions(-) diff --git a/lisp/use-package/use-package.el b/lisp/use-package/use-package.el index 68d38d19b8c..ce38a6e1a77 100644 --- a/lisp/use-package/use-package.el +++ b/lisp/use-package/use-package.el @@ -330,6 +330,14 @@ convert it to a string and return that." (if (stringp string-or-symbol) string-or-symbol (symbol-name string-or-symbol))) +(defun use-package-as-mode (string-or-symbol) + "If STRING-OR-SYMBOL ends in `-mode' (or its name does), return +it as a symbol. Otherwise, return it as a symbol with `-mode' +appended." + (let ((string (use-package-as-string string-or-symbol))) + (intern (if (string-match "-mode\\'" string) string + (concat string "-mode"))))) + (defun use-package-load-name (name &optional noerror) "Return a form which will load or require NAME depending on whether it's a string or symbol." @@ -1435,26 +1443,43 @@ deferred until the prefix key sequence is pressed." ;;; :delight ;; +(defun use-package--normalize-delight-1 (name args) + "Normalize ARGS for a single call to `delight'." + (when (eq :eval (car args)) + ;; Handle likely common mistake. + (use-package-error ":delight mode line constructs must be quoted")) + (cond ((and (= (length args) 1) (symbolp (car args))) + `(,(nth 0 args) nil ,name)) + ((= (length args) 2) + `(,(nth 0 args) ,(nth 1 args) ,name)) + ((= (length args) 3) + args) + (t + (use-package-error + ":delight expects `delight' arguments or a list of them")))) + (defun use-package-normalize/:delight (name keyword args) "Normalize arguments to delight." - (cond - ((and (= (length args) 1) - (symbolp (car args))) - (list (car args) nil name)) - ((and (= (length args) 2) - (symbolp (car args))) - (list (car args) (cadr args) (use-package-as-symbol name))) - ((and (= (length args) 3) - (symbolp (car args))) - args) - (t - (use-package-error ":delight expects same args as delight function")))) + (cond ((null args) + `((,(use-package-as-mode name) nil ,name))) + ((and (= (length args) 1) + (symbolp (car args))) + `((,(car args) nil ,name))) + ((and (= (length args) 1) + (or (not (listp (car args))) + (eq 'quote (caar args)))) + `((,(use-package-as-mode name) ,(car args) ,name))) + (t (mapcar + (apply-partially #'use-package--normalize-delight-1 name) + (if (symbolp (car args)) (list args) args))))) (defun use-package-handler/:delight (name keyword args rest state) (let ((body (use-package-process-keywords name rest state))) (use-package-concat body - `((delight (quote ,(nth 0 args)) ,(nth 1 args) (quote ,(nth 2 args))) t)))) + (mapcar (lambda (arg) + `(delight ',(nth 0 arg) ,(nth 1 arg) ',(nth 2 arg))) + args)))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; diff --git a/test/lisp/use-package/use-package-tests.el b/test/lisp/use-package/use-package-tests.el index 00682e9e0fc..ababfe0d5b2 100644 --- a/test/lisp/use-package/use-package-tests.el +++ b/test/lisp/use-package/use-package-tests.el @@ -51,6 +51,25 @@ (should (equal (use-package-normalize-mode 'foopkg :mode '((".foo" . foo) (".bar" . bar))) '((".foo" . foo) (".bar" . bar))))) +(ert-deftest use-package-normalize-delight () + (should (equal `((foo-mode nil foo)) + (use-package-normalize/:delight 'foo :delight nil))) + (should (equal `((foo-mode nil foo-mode)) + (use-package-normalize/:delight 'foo-mode :delight nil))) + (should (equal `((bar-mode nil foo)) + (use-package-normalize/:delight 'foo :delight '(bar-mode)))) + (should (equal `((foo-mode "abc" foo)) + (use-package-normalize/:delight 'foo :delight '("abc")))) + (should (equal `((foo-mode '(:eval 1) foo)) + (use-package-normalize/:delight 'foo :delight '('(:eval 1))))) + (should (equal `((a-mode nil foo) + (b-mode " b" foo)) + (use-package-normalize/:delight 'foo :delight '((a-mode) + (b-mode " b"))))) + (should-error (use-package-normalize/:delight 'foo :delight '((:eval 1)))) + + ) + ;; Local Variables: ;; indent-tabs-mode: nil ;; no-byte-compile: t -- 2.39.2