1
Fork 0
mirror of git://git.sv.gnu.org/emacs.git synced 2025-12-13 09:30:24 -08:00

All the second argument in use-package-defaults lists to be a function

Address the question raised in https://github.com/jwiegley/use-package/issues/591
This commit is contained in:
John Wiegley 2017-12-15 22:48:42 -08:00
parent 51eceb4238
commit 2d226310f1
2 changed files with 37 additions and 12 deletions

View file

@ -159,21 +159,30 @@ See also `use-package-defaults', which uses this value."
(not (plist-member args :defer)) (not (plist-member args :defer))
(not (plist-member args :demand)))))) (not (plist-member args :demand))))))
"Default values for specified `use-package' keywords. "Default values for specified `use-package' keywords.
Each entry in the alist is a list of three elements. The first Each entry in the alist is a list of three elements:
element is the `use-package' keyword and the second is a form The first element is the `use-package' keyword.
that can be evaluated to get the default value. The third element
is a form that can be evaluated to determine whether or not to The second is a form that can be evaluated to get the default
assign a default value; if it evaluates to nil, then the default value. It can also be a function that will receive the name of
value is not assigned even if the keyword is not present in the the use-package declaration and the keyword plist given to
`use-package' form. This third element may also be a function, in `use-package', in normalized form. The value it returns should
which case it receives the name of the package (as a symbol) and also be in normalized form (which is sometimes *not* what one
a list of keywords (in normalized form). It should return nil or would normally write in a `use-package' declaration, so use
t according to whether defaulting should be attempted." caution).
The third element is a form that can be evaluated to determine
whether or not to assign a default value; if it evaluates to nil,
then the default value is not assigned even if the keyword is not
present in the `use-package' form. This third element may also be
a function, in which case it receives the name of the package (as
a symbol) and a list of keywords (in normalized form). It should
return nil or non-nil depending on whether defaulting should be
attempted."
:type `(repeat :type `(repeat
(list (choice :tag "Keyword" (list (choice :tag "Keyword"
,@(mapcar #'(lambda (k) (list 'const k)) ,@(mapcar #'(lambda (k) (list 'const k))
use-package-keywords)) use-package-keywords))
(choice :tag "Default value" sexp) (choice :tag "Default value" sexp function)
(choice :tag "Enable if non-nil" sexp function))) (choice :tag "Enable if non-nil" sexp function)))
:group 'use-package) :group 'use-package)
@ -564,7 +573,11 @@ extending any keys already present."
(funcall func name args) (funcall func name args)
(eval func))) (eval func)))
(setq args (use-package-plist-maybe-put (setq args (use-package-plist-maybe-put
args (nth 0 spec) (eval (nth 1 spec)))))) args (nth 0 spec)
(let ((func (nth 1 spec)))
(if (and func (functionp func))
(funcall func name args)
(eval func)))))))
;; Determine any autoloads implied by the keywords used. ;; Determine any autoloads implied by the keywords used.
(let ((iargs args) (let ((iargs args)

View file

@ -1862,6 +1862,18 @@
(string-match ":defer wants exactly one argument" (string-match ":defer wants exactly one argument"
(car warnings))) 44)))))) (car warnings))) 44))))))
(ert-deftest use-package-test/591 ()
(let ((use-package-defaults
(cons '(:if (lambda (name _) `(locate-library ,name)) t)
use-package-defaults)))
(match-expansion
(use-package nonexistent
:hook lisp-mode)
`(when (locate-library nonexistent)
(unless (fboundp 'nonexistent)
(autoload #'nonexistent "nonexistent" nil t))
(add-hook 'lisp-mode-hook #'nonexistent)))))
(ert-deftest bind-key/:prefix-map () (ert-deftest bind-key/:prefix-map ()
(match-expansion (match-expansion
(bind-keys :prefix "<f1>" (bind-keys :prefix "<f1>"