]> git.eshelyaron.com Git - emacs.git/commitdiff
Allow multiple :delight arguments, or omitting the mode. ()
authorJoe Wreschnig <joe.wreschnig@gmail.com>
Sun, 2 Jul 2017 14:20:01 +0000 (16:20 +0200)
committerJoe Wreschnig <joe.wreschnig@gmail.com>
Sun, 2 Jul 2017 14:32:38 +0000 (16:32 +0200)
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
test/lisp/use-package/use-package-tests.el

index 68d38d19b8c89fbc6482b532e4f87cf47ffe0d08..ce38a6e1a777275cd5d44c6f395c416882f648e4 100644 (file)
@@ -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))))
 
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 ;;
index 00682e9e0fca12be1f3e43d41808c126faabb394..ababfe0d5b204f3b12d6eb4b26f7707973234fe5 100644 (file)
   (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