1

I'm new to elisp- what is the difference between:

(add-hook 'js2-mode-hook
          (lambda ()
            (flyspell-prog-mode)
          ))

and

(add-hook 'js2-mode-hook 'flyspell-prog-mode)

?

Drew
  • 75,699
  • 9
  • 109
  • 225
Fergie
  • 193
  • 8
  • 1
    https://emacs.stackexchange.com/tags/elisp/info – Drew Nov 02 '22 at 13:52
  • @Drew Thanks for bringing that to my attention. I feel the need to push back on it a bit: Firstly, it seems nonsensical that questions about elisp syntax should not be tagged as `elisp`, secondly the linked page contains no sensible guidance for what beginners _should_ tag their elisp questions as (since its reasonable to assume that beginners do not fully understand the tags the page suggests such as elisp-macros, variables, lexical-scoping). The whole guideline feels a bit gatekeepery. – Fergie Nov 02 '22 at 14:19
  • 2
    The second version is to be preferred. First, because of clarity. Second, you can use `(remove-hook 'js2-mode-hook 'flyspell-prog-mode)` when needed. If you need more stuff in the hook function, define a named function and add that function via `add-hook`. If the additional stuff is very closely related to `flyspell-prog-mode` then put everything in one named function that you add via one `add-hook` only. If the additional stuff is actually unrelated to `flyspell-prog-mode` use an additional `add-hook`. That helps later on if you want to remove the behavioral modifications separately. – Tobias Nov 02 '22 at 14:20
  • 3
    The point is that if every question that includes a bit of code was tagged `elisp`, then a very large percentage of all the questions would end up with that tag, making it useless as an aid for searching. – NickD Nov 02 '22 at 16:52
  • @NickD Fair point in and of itself. That said, there should be an easily discoverable tag such as `elisp-beginners`. As things stand, novices who ask basic/general elisp questions tagged `elisp` are reprimanded, and consequently discouraged from using Emacs. (Luckily I am a thick skinned greybeard ) – Fergie Nov 03 '22 at 07:43

1 Answers1

4

The functions added to hooks will be called using funcall.

In the first version, you are adding an anonymous function which calls flyspell-prog-mode in its body. So when executing the hook function, funcall will call the anonymous function which then will call flyspell-prog-mode.

In the second version, you are adding a reference to the function flyspell-prog-mode directly. So when executing the hook function, funcall will call that function directly, i.e. this is the (almost) 'correct' version.

Although it is fine to reference a function via a symbol, it is better to let the compiler know that you are referring to a function by using a sharp quote (see for example this answer), although in this case, as far as I understand, it does not really matter.

Others are welcome to provide more nuanced answers :)

dalanicolai
  • 6,108
  • 7
  • 23
  • 2
    This and the link you provided fulfill all the nuancing necessary :-) - IMO. There is one other advantage of `#'` : it tells the *reader* that it's a function - so I always try to use it in such cases, even if not strictly necessary. – NickD Nov 02 '22 at 14:25