jwiegley / use-package

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

Proposal for post hook variables #161

Closed syl20bnr closed 9 years ago

syl20bnr commented 9 years ago

In spacemacs the configuration is organized with configuration layers stacked on top of each other. The user has a dotfile with two functions:

Packages initialization try to use use-package as much as possible to leverage the emacs lazy-loading mechanism exposed by the macro with its various keywords.

A post-init hook could allow some users to safely and consistently override the variables that need to be set before requiring a package. Indeed spacemacs comes with default values and it can be tricky to override them in some scenarios in the spacemacs user space (i.e. the package is not lazy loaded). While I believe (need someone to confirm) that eval-after-load should make a post-config hook unnecessary I would find it very convenient and symmetric if the idea of post-init hook variable is accepted.

This feature could be a game changer for spacemacs but:

syl20bnr commented 9 years ago

@jwiegley Great for the simplifications but it does not address my (spacemacs users) use case.... You wrote that you don't see a need for pre and post but this is what the whole issue is all about :-) Do you want me to explain it again ?

I also want to make it clear again that I don't propose new keywords, I propose additional hooks to put some code before and after init and config bodies.

In spacemacs there is a dotfile (~/.spacemacs) with 2 functions:

The initialization pipeline is (roughly): dotspacemacs/init (1) > eval use-package blocks (2) > dotspacemacs/config (3)

I need to provide spacemacs users a way to alter the :init of (2) in (1) and the lazy loaded stuff in :config in (2) in (3). I cannot tell them to use use-package again it is already used in (2) so I need a better way that is consistent accros all the packages, and I hoped that use-package could provide it, this is the perfect place for this.

I propose to put these hooks in place:

The only thing to add in use-package is 4 run-hook (with the appropriate check), the rest is pure naming convention. We can even add a configuration variable in use-package to disable these hooks: use-package-generate-hooks.

jwiegley commented 9 years ago

@syl20bnr Sure, I'd happy be happy to support your use case.

So, are you saying that at the beginning and end of :init and :config blocks, I should inject calls to run-hooks use-package-pre-init, etc.?

jwiegley commented 9 years ago

@syl20bnr Are you on IRC or Google Talk? I'd love to chat with you directly to support your use case.

syl20bnr commented 9 years ago

@jwiegley Thank you for the fast reply. We can catch each other tomorrow on IRC, I have to sleep a bit now (3am) ;-)

So, are you saying that at the beginning and end of :init and :config blocks, I should inject calls to run-hooks use-package-pre-init, etc.?

Yes, but it has to be namespaced with the package name.

jwiegley commented 9 years ago

@syl20bnr Ok, great, I'm johnw there. My only question is how we can support what you need, without injecting those calls for those not using spacemacs. I'll give it some thought, but I think we can just add a configuration variable which, at macro expansion time, will cause hooks to be injected at the selected places, and under the requested names.

jwiegley commented 9 years ago

@syl20bnr Let me know if ef0bcf3840506637a93da0bee08cfc9931dc689f does what you need.

npostavs commented 9 years ago

The initialization pipeline is (roughly): dotspacemacs/init (1) > eval use-package blocks (2) > dotspacemacs/config (3)

I need to provide spacemacs users a way to alter the :init of (2) in (1) and the lazy loaded stuff in :config in (2) in (3).

If I understand correctly, (2) is non-user configured code that comes with spacemacs? Why don't you just put some calls to spacemacs hooks in there?

(use-package foo
  :defer t ; we must lazy load the :config block
  :init (progn (run-hooks spacemacs-foo-init-hook) <more-spacemacs-stuff>)
  :config (progn (run-hooks spacemacs-foo-config-hook) <more-spacemacs-stuff>))
syl20bnr commented 9 years ago

@jwiegley This is perfect, the control over the block depending on the returned value is a nice plus :+1: My only remark would be to add a -hook suffix to the generated hook names to be more conventional.

@npostavs indeed I could do that but spacemacs has grown a lot lately and the package coverage is becoming big (currently there are 262 use-package occurrences), so in practice it would be really tedious.

Thank you!

jwiegley commented 9 years ago

@syl20bnr Another note: in your document at http://thume.ca/howto/2015/03/07/configuring-spacemacs-a-tutorial/, your use of :defer there is unnecessary. It is implied by :mode.

syl20bnr commented 9 years ago

@jwiegley You are right, I'll make a pass in spacemacs to clean this. Thank you for pointing it.