TransWikia.com

Hook with quoted lambda as an argument

Emacs Asked by siery on December 19, 2021

I been reading a blog post from Chris Wanstrath from 2010 where he write about coffee-mode. He proposed there this functions hook:

(defun coffee-custom ()
  "coffee-mode-hook"

  ;; Emacs key binding
  (define-key coffee-mode-map [(meta r)] 'coffee-compile-buffer))

(add-hook 'coffee-mode-hook (lambda () (coffee-custom)))

In coffee-mode.el you can see similar hook call

"... Add `(lambda () (coffee-cos-mode t))' to `coffee-mode-hook' to turn
it on by default."

I don’t understand the use of lambda calling another function in order to execute it. Why not simply:

(add-hook 'coffee-mode-hook #'my:coffee-custom)

Resources: http://ozmm.org/posts/coffee_mode.html

One Answer

(add-hook 'coffee-mode-hook '(lambda () (coffee-custom)))

Definitely not recommended.

  1. Quoting lambdas like that is not good practice.
  2. As you say, (add-hook 'coffee-mode-hook #'coffee-custom) would be better (for many reasons).

In coffee-mode.el you can see similar hook call

"... Add `'(lambda () (coffee-cos-mode t))' to `coffee-mode-hook'

This one at least had a purpose, as it's passing an argument; however:

  1. Again with the quoted lambda.
  2. I'd still recommend using a named function instead of a lambda, even if you have to define it yourself.
  3. If coffee-cos-mode is a minor mode defined with the standard macro then the argument will be redundant in this scenario (since Emacs 24.1), in which case you could just add #'coffee-cos-mode to the hook, in the same fashion as the previous scenario.

Answered by phils on December 19, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP