I'm trying to adapt this answer to use-package elegantly. It requires that I hook a lambda function to python-mode. I came up with this solution — which works:

(use-package python
  (python-mode . (lambda ()
                   (setq indent-tabs-mode t)
                   (setq tab-width 4)
                   (setq python-indent-offset 4))))

but I find it inelegant, as it requires me to repeat "python-mode" inside the use-package for python (shouldn't that be obvious?). Is there a better way, and if not, why? I'm not sure I understand well the hook process in use-package.

  • 1
    Hook is to add something for this package on another module load hook, like prog-mode would be. That's why you need to give its name first. In this case why not just place those variables in :config? – Muihlinn May 29 at 16:23
  • See Hooks in use-package. – NickD May 29 at 16:52
  • @Muihlinn Thank you for this clarification. So using a hook on a package is exactly the same as putting the hook function inside :config ? I reread your first sentence 5 times and still find it a little cryptic. It means that in use-package, :hook is used to load a package that the current package would need if a different third package were loaded, is that it? – kotchwane May 29 at 19:16
  • 3
    In this case, indent-tabs-mode and tab-width are buffer-local variables, and since they potentially have optimal values that differ depending on the major mode you're in, they should NOT be moved inside :config as suggested (unless you always want to have that as default, for all major modes). You might move python-indent-offset though. :config has code that is evaluated after the major mode is loaded (usually once), and the functions you add to the hook are called every time a new buffer triggers that major mode. – Arthur Colombini Gusmão May 30 at 20:58
  • 1
    Your question seems to be "shouldn't it be obvious that if I'm configuring a package called X and I want to add to a hook, that the hook name would be X-mode-hook?". The answer is no, it's not obvious. It might well be a common case, but it's absolutely not a given. – phils May 31 at 0:30

Potentially the simplification of defaulting to <modename>-mode-hook, for a given <modename>, could have been implemented in use-package, yes, since it is in the Major Mode Conventions. However, it seems the authors decided to use this notation to simplify adding the current package (the package in the current use-package declaration) to another hook; see [here]. Also, notice that hooks may be used for several other reasons than to start major modes, so their name may vary a lot.

| improve this answer | |

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.