5

I am studying the following code snippet taken from prelude-c.el:

;; taken from prelude-c.el

(defun prelude-makefile-mode-defaults ()
  (whitespace-toggle-options '(tabs))
  (setq indent-tabs-mode t))

(setq prelude-makefile-mode-hook 'prelude-makefile-mode-defaults)

(add-hook 'makefile-mode-hook (lambda ()
                                (run-hooks 'prelude-makefile-mode-hook)))

The author defined a function named prelude-makefile-mode-defaults, then made prelude-makefile-mode-hook an alias to it, and finally run it with lambda and run-hooks. This looks tedious to me. In my humble opinion, it can be simplified to

(defun prelude-makefile-mode-defaults ()
  (whitespace-toggle-options '(tabs))
  (setq indent-tabs-mode t))

(add-hook 'makefile-mode-hook #'prelude-makefile-mode-defaults)

Now it is much clearer, and they appears to have the same effect, but of course, the authors of prelude.el must be much more sophisticated than me, who is just a beginner. Is there any hidden advantage of writing down all these seemingly redundant code?

  • Good question. I much prefer your version, and I don't see why the Prelude would use the other form. – Stefan Feb 16 at 14:12
  • @Stefan I'm curious is there a difference between (foo) and (run-hooks foo)? If they are equivalent, then the function run-hooks would be pointless... – nalzok Feb 16 at 16:22
  • 1
    I think the intent of the author is to provide a hook that can be used for customization. Having a way to guarantee that your own hooks run close after ones from prelude might be useful. – DoMiNeLa10 Feb 20 at 18:46
2

I believe the reason for this indirection is that it allows users of the package to override the package provided defaults by changing the value of prelude-makefile-mode-hook. The final component of the name prelude-makefile-mode-defaults is a strong hint in this direction, I think. But of course, only the original package author can give a definitive 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.