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)

?

5
  • 1
    emacs.stackexchange.com/tags/elisp/info
    – Drew
    Commented Nov 2, 2022 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
    Commented Nov 2, 2022 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
    Commented Nov 2, 2022 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
    Commented Nov 2, 2022 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
    Commented Nov 3, 2022 at 7:43

1 Answer 1

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 :)

1
  • 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
    Commented Nov 2, 2022 at 14:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.