14

I've wrote this, and it works:

(use-package web-mode
  :ensure t
  :mode (("\\.php$" .  web-mode)
         ("\\.html$" .  web-mode))
  :preface
  (defun dg/web-mode-hook())
  :config
  (add-hook 'web-mode-hook 'dg/web-mode-hook))

It enables web-mode when opening .php or .html files. I've noticed that use-package also provides :hook directive, but it's not very clear how I can use it in this case.

Any clue?

Thanks in advance.

Daniele
  • 637
  • 1
  • 5
  • 14
  • 2
    https://github.com/jwiegley/use-package#hooks seems pretty clear to me, but maybe it can be further improved. Is that the documentation which you found confusing? – phils Mar 06 '18 at 01:34

1 Answers1

14

As others said I think https://github.com/jwiegley/use-package#hooks is pretty clear. :hook replaces add-hook and creates autoloads for you. Your example with :hook would be:

(use-package web-mode
  :ensure t
  :mode (("\\.php$" .  web-mode)
         ("\\.html$" .  web-mode))
  :hook (web-mode . dg/web-mode-hook)
  :config
  (defun dg/web-mode-hook()))
xuchunyang
  • 14,302
  • 1
  • 18
  • 39
dakra
  • 396
  • 3
  • 6
  • 1
    Thank you, I tought that `:hook` could some way save me from having to define a `my-hook-fn()`. – Daniele Mar 06 '18 at 08:43
  • 1
    @Daniele, you should be able to use a lambda form: (use-package web-mode :ensure t :mode (("\\.php$" . web-mode) ("\\.html$" . web-mode)) :hook (web-mode . (lambda () ...))) – paradoja Feb 15 '19 at 13:48
  • It is not clear to me. For instance, couldn't you simply write `:hook dg/web-mode`, and if not, why? – kotchwane May 30 '20 at 06:19
  • @kotchwane if you click the readme link in the answer you see that if you pass only one argument (or a list of symbold), use-packages assumes those are the modes you want to activate your current "use-package package" if that makes sense. So in your case it would add `web-mode` to `dg/web-mode-hook` (the `-hook` is added by use-package) – dakra Jun 01 '20 at 11:37