From ea9fad47ece69cba9b30be8ec57a51f4f9421851 Mon Sep 17 00:00:00 2001 From: Case Duckworth Date: Fri, 12 Mar 2021 17:29:26 -0600 Subject: Change `acdw/hooks' form Instead of only allowing (list-of-hooks) (list-of-functions), `acdw/hooks' now accepts a (list-of-hook-specs), each of which is of the above form, allowing the user to set multiple unrelated hooks at once. --- init.el | 8 ++++---- lisp/acdw.el | 55 +++++++++++++++++++++++++++---------------------------- 2 files changed, 31 insertions(+), 32 deletions(-) diff --git a/init.el b/init.el index 2f8760f..6940b6c 100644 --- a/init.el +++ b/init.el @@ -51,7 +51,7 @@ (empty indentation space-before-tab space-after-tab)) (indent-tabs-mode t) (tab-width 8))) -(acdw/hooks before-save-hook whitespace-cleanup) +(add-hook 'before-save-hook #'whitespace-cleanup) ;; Pairs (add-hook 'prog-mode-hook #'electric-pair-local-mode) @@ -377,8 +377,8 @@ indicator in the mode-line." (when (eq acdw/system :work) (add-to-list 'exec-path "C:/Program Files/Mozilla Firefox")) -(acdw/hooks text-mode-hook goto-address-mode) -(acdw/hooks prog-mode-hook goto-address-prog-mode) +(acdw/hooks ((text-mode-hook goto-address-mode) + (prog-mode-hook goto-address-prog-mode))) ;;; Dired @@ -403,7 +403,7 @@ indicator in the mode-line." :binds (("i" dired-subtree-toggle :map dired-mode-map)))) (acdw/pkg dired-collapse - :hooks (dired-mode-hook dired-collapse-mode)) + :hooks ((dired-mode-hook dired-collapse-mode))) ;;; Eshell diff --git a/lisp/acdw.el b/lisp/acdw.el index 201db1f..6641e9b 100644 --- a/lisp/acdw.el +++ b/lisp/acdw.el @@ -101,35 +101,34 @@ SPEC is as for `defface'." ,@face-list))) ;;; Hooks +(defmacro acdw/hooks (hook-specs &rest args) + "Add functions to hooks, according to HOOK-SPECS. -(defmacro acdw/hooks (hooks funcs &optional depth local) - "Add FUNCS to HOOKS. - -Either HOOKS or FUNCS can be a list, in which case they're mapped -over to add all FUNCS to all HOOKS. They can also be singletons, -in which case `acdw/hooks' acts pretty much like `add-hook'. - -DEPTH and LOCAL apply to all HOOKS defined here. If you need -more fine-grained control, just use `add-hook'." - (let ((hooks (if (listp hooks) hooks (list hooks))) - (funcs (if (listp funcs) funcs (list funcs))) - (depth (if depth depth 0)) - (hook-list)) - (dolist (hook hooks) - (dolist (func funcs) - (push `(add-hook ',hook #',func ,depth ,local) hook-list))) - `(progn - ,@hook-list))) - -(defmacro acdw/hooks-after (file hooks funcs &optional depth local) - "Add FUNCS, from FILE, to HOOKS." - (let ((funcs (if (listp funcs) funcs (list funcs))) - (autoload-list)) - (dolist (func funcs) - (add-to-list 'autoload-list `(autoload #',func ,file))) +Each HOOK-SPEC is of the following format: (HOOKS FUNCS [DEPTH] [LOCAL]). +Either HOOKS or FUNCS can also be a list, in which case `add-hook' is called +over the Cartesian product of HOOKS and FUNCS. In each HOOK-SPEC, DEPTH and +LOCAL apply to all hooks defined; if finer control is needed, either pass the +same hooks and functions in different HOOK-SPECs, or just use `add-hook'. + +ARGS accept the following keywords: + +:after FEATURE .. `autoload' all functions after FEATURE." + (let ((after (plist-get args :after)) + (command-list)) + (dolist (spec hook-specs) + (let* ((hooks (car spec)) + (funcs (cadr spec)) + (depth (or (caddr spec) 0)) + (local (cadddr spec))) + (when (not (listp hooks)) (setq hooks (list hooks))) + (when (not (listp funcs)) (setq funcs (list funcs))) + (dolist (hook hooks) + (dolist (func funcs) + (push `(add-hook ',hook #',func ,depth ,local) command-list) + (when after + (push `(autoload #',func ,after) command-list)))))) `(progn - ,@autoload-list - (acdw/hooks ,hooks ,funcs ,depth ,local)))) + ,@command-list))) ;;; Keybindings @@ -209,7 +208,7 @@ ARGS can include the following keywords: (when then-forms (push `(with-eval-after-load ',requirement ,@then-forms) final-form)) (when hooks - (push `(acdw/hooks-after ,(symbol-name requirement) ,@hooks) final-form)) + (push `(acdw/hooks ,hooks :after ,(symbol-name requirement)) final-form)) (when binds (push `(acdw/bind-after-map ,(symbol-name requirement) nil ,binds) final-form)) -- cgit 1.4.1-21-gabe81