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:

  1. The use-package library provides a use-package-inject-hooks variable that can be enabled in order to activate hooks for the :init and :config sections:

    If non-nil, add hooks to the `:init' and `:config' sections.
    In particular, for a given package `foo', the following hooks
    become available:
    This way, you can add to these hooks before evaluation of a
    `use-package` declaration, and exercise some control over what
    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
    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
  2. 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
      (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.)

  • 1
    If you cannot directly modify the 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 your use-package code would be the simplest solution. – Tyler Apr 12 at 19:43
  • Context: I am a Doom Emacs user and currently do not have the time to maintain my own fork, which I believe would be necessary in order for me to directly modify the use-package form in question. – Sam Apr 12 at 22:50
  • Doom's configuration appears to be sufficiently distinct from regular Emacs that you might be better off looking for help from the maintainers directly. discord.gg/qvGgnVx – Tyler Apr 13 at 0:20

I think something has gone wrong if you can't modify the use-package form -- AFAIK that macro is intended for use by end-users only.

That said, the documentation you've quoted:

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
  (add-hook 'prog-mode-hook #'ace-jump-mode))

suggests to me that you can probably just do the following (subsequent to loading the above code):

(with-eval-after-load 'ace-jump-mode
  (remove-hook 'prog-mode-hook #'ace-jump-mode))
  • As a Doom Emacs user, I think the trick for me is figuring out exactly when the use-package form is evaluated (the lazy loading is probably what's tripping me up). Your code makes sense but for whatever reason it's not working in my case. – Sam Apr 12 at 23:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.