jwiegley / use-package

A use-package declaration for simplifying your .emacs
https://jwiegley.github.io/use-package
GNU General Public License v3.0
4.37k stars 260 forks source link

Feature request: add `use-package-after-configure-all-hook` #1066

Open hab25 opened 6 months ago

hab25 commented 6 months ago

I.e. a hook that runs immediately after the last-to-be-configured package of all current use-package definitions is configured. The name can be something else.

One use case is adding an element to a list with meaningful order, such as emulation-mode-map-alists, such that one wants to have it as the first element. Modifying such a list only after all packages have been loaded and configured decreases the chance that such an undesirable override happens.


Given that I use (setopt use-package-compute-statistics t), these are the problematic alternatives I've considered: 1) In init.el, putting the following after all other usages of use-package:

    (eval
     `(use-package
       my-virtual-package
       :after
       ,(copy-tree (hash-table-keys use-package-statistics))
       :config (message "I'm running my code after all use-package declarations have run their configuration!")
       :no-require t))
This does not work if one is has a lot `use-package` declarations; one encounters this error
https://github.com/emacs-mirror/emacs/blob/c4541a35770fe7925f733fcdaa9e4e3348a3c85c/src/fns.c#L2754

In my case, `(length (hash-table-keys use-package-statistics))` is 152.

2) "Map" (hash-table-keys use-package-statistics) into multiple nested eval-after-loads: It doesn't meet my needs: this will run after the last package is loaded. What I want is to run after the last package is configured.

3) Assume that at least one package managed by use-package has not yet been loaded by the end of init.el (this assumption breaks, for example, if one uses :demand t in all of their use-package declarations). One can advice use-package-statistics-gather at the end of their init.el like so:

    (advice-add
     'use-package-statistics-gather
     :after
     (lambda (&rest _)
       (when (cl-every
              (lambda (use-package-statistic)
                (equal
                 (use-package-statistics-status
                  use-package-statistic)
                 "Configured"))
              (hash-table-values use-package-statistics))
         (message "I'm running my code after all use-package declarations have run their configuration!"))))))
This is roughly what I'm doing and works fine, but, as is typical with pieces of `advice`, relies on fragile assumptions (i.e. that are easy to be broken by code updates to this repository) about the internal workings of `use-package`, e.g. the fact that this is the only function that calls `(puthash KEY VALUE use-package-statistics)`.