From 4ad4a59685a947a31e2a2970b428920cd6b56ced Mon Sep 17 00:00:00 2001 From: John Wiegley Date: Tue, 28 Nov 2017 13:58:38 -0800 Subject: [PATCH] Add support for `:hook` Fixes https://github.com/jwiegley/use-package/issues/444 --- lisp/use-package/use-package.el | 49 +++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/lisp/use-package/use-package.el b/lisp/use-package/use-package.el index cc8a76bd1c2..648e2c7c8ae 100644 --- a/lisp/use-package/use-package.el +++ b/lisp/use-package/use-package.el @@ -158,6 +158,7 @@ the user specified." :defines :functions :defer + :hook :custom :custom-face :init @@ -1409,6 +1410,54 @@ deferred until the prefix key sequence is pressed." (message (format "Cannot load %s" ',name))) ,@config-body))))))) +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; +;;; :hook +;; + +(defun use-package-normalize/:hook (name keyword args) + (use-package-as-one (symbol-name keyword) args + (lambda (label arg) + (unless (or (symbolp arg) (consp arg)) + (use-package-error + (concat label " a or a ( . )" + " or list of these"))) + (use-package-normalize-pairs + #'(lambda (k) + (or (symbolp k) + (and (listp k) + (listp (cdr k)) + (seq-every-p #'symbolp k)))) + #'(lambda (v) + (or (symbolp v) (functionp v))) + name label arg)))) + +(defun use-package-handler/:hook (name keyword args rest state) + "Generate use-package custom keyword code." + (let ((commands (let (funs) + (dolist (def args) + (if (symbolp (cdr def)) + (setq funs (cons (cdr def) funs)))) + (nreverse funs)))) + (use-package-concat + (use-package-process-keywords name + (if commands + (use-package-sort-keywords + (use-package-plist-maybe-put rest :defer t)) + rest) + (if commands + (use-package-plist-append state :commands commands) + state)) + (cl-mapcan + (lambda (def) + (let ((syms (car def)) + (fun (cdr def))) + (mapcar + #'(lambda (sym) + `(add-hook (quote ,(intern (format "%s-hook" sym))) + (function ,fun))) + (if (symbolp syms) (list syms) syms)))) args)))) + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; ;;; :custom -- 2.39.2