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)
?
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 :)
#'
: it tells the reader that it's a function - so I always try to use it in such cases, even if not strictly necessary.
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.(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 viaadd-hook
. If the additional stuff is very closely related toflyspell-prog-mode
then put everything in one named function that you add via oneadd-hook
only. If the additional stuff is actually unrelated toflyspell-prog-mode
use an additionaladd-hook
. That helps later on if you want to remove the behavioral modifications separately.elisp
, then a very large percentage of all the questions would end up with that tag, making it useless as an aid for searching.elisp-beginners
. As things stand, novices who ask basic/general elisp questions taggedelisp
are reprimanded, and consequently discouraged from using Emacs. (Luckily I am a thick skinned greybeard 😉)