I am attempting to change the :hook
section of a use-package
declaration that I cannot directly modify. In my case, I want to remove hooks that are getting set, not just add more hooks. Is there a way to do this similar to how the :init
and :config
sections can be modified via use-package-inject-hooks
?
Reference points:
The
use-package
library provides ause-package-inject-hooks
variable that can be enabled in order to activate hooks for the:init
and:config
sections:[use-package-inject-hooks] If non-nil, add hooks to the `:init' and `:config' sections. In particular, for a given package `foo', the following hooks become available: `use-package--foo--pre-init-hook' `use-package--foo--post-init-hook' `use-package--foo--pre-config-hook' `use-package--foo--post-config-hook' This way, you can add to these hooks before evaluation of a `use-package` declaration, and exercise some control over what happens. NOTE: These hooks are run even if the user does not specify an `:init' or `:config' block, and they will happen at the regular time when initialization and configuration would have been performed. NOTE: If the `pre-init' hook return a nil value, that block's user-supplied configuration is not evaluated, so be certain to return t if you only wish to add behavior to what the user had specified.
The
:hook
section provides an alternate interface for special-case usage of the:init
section (see documentation):The
:hook
keyword allows adding functions onto package hooks. Thus, all of the following are equivalent:(use-package ace-jump-mode :hook prog-mode) (use-package ace-jump-mode :hook (prog-mode . ace-jump-mode)) (use-package ace-jump-mode :commands ace-jump-mode :init (add-hook 'prog-mode-hook #'ace-jump-mode))
Given the above two points, I thought that maybe hooking into the :init
section using use-package-inject-hooks
would allow me to override the :hook
section, but that was not the case. Looking at the source code for use-package
, I can see that :hook
and :init
are implemented differently (:hook
is not a special wrapper for :init
) so I can see why that would not work.
Any advice would be much appreciated. (On that note... I suppose a clever use of the advice mechanism in Emacs could provide a workaround, which I hadn't considered until I wrote that last sentence. That said, I would like to avoid such a kludge if possible.)
use-package
declaration, I'm not sure what you mean when you say "hooking into the :init: section"... I don't understand your context, but it sounds like(advice-remove ...)
called after youruse-package
code would be the simplest solution.use-package
form in question.