Host127001 · 2022-02-02 08:36
I want to start LSP mode whenever I enter a C/C++ file. When I use the `:hook` keyword, nothing happens:
(use-package lsp-mode
:commands (lsp lsp-deferred)
:hook (c-mode-common-hook . lsp-deferred))
Even using `add-hook` in the `:config` section does nothing
(use-package lsp-mode
:commands (lsp lsp-deferred)
:config (add-hook 'c-mode-common-hook 'lsp-deferred))
But if I add the `add-hook` outside of `use-package`, it works
(use-package lsp-mode
:commands (lsp lsp-deferred))
(add-hook 'c-mode-common-hook 'lsp-deferred)
My question is: How do you correctly add hooks inside `use-package`?
bhrgunatha · 2022-02-02 09:26
[`use-package` automatically adds `-hook`](https://github.com/jwiegley/use-package#hooks) for the the `:hook` keyword.
So try this instead.
(use-package lsp-mode
:commands (lsp lsp-deferred)
:hook (c-mode-common . lsp-deferred))
bhrgunatha · 2022-02-02 09:31
FWIW - and to let others point out my mistakes - since I've only just started using emacs for C/C++ here's my configuration:
(use-package cc-mode
:hook ((c-mode . lsp-deferred)
(c++-mode . lsp-deferred))
:config
(electric-indent-mode)
(c-set-offset 'substatement-open 0)
(setq c-default-style "linux"
indent-tabs-mode nil
c-basic-offset 4))
deaddyfreddy · 2022-02-02 10:10
`:custom` instead of `setq` works better in some cases, so I prefer it
vifon · 2022-02-02 12:07
/u/bhrgunatha already explained why the first version didn't work, but the second one contains a different mistake worth explaining.
`:commands` implicitly defers the loading of the feature and `:config` gets executed only after the feature gets loaded (internally it's an `eval-after-load` block). Using `:init` instead would work.
gepardcv · 2022-02-02 17:34
I’ve been using `use-package` for years, and the details of deferred-loading still burn me occasionally. The nature of the beast, I suppose.