RNA
RNA

Reputation: 153321

add a function with parameters to a hook

I want to run a function according to the programming language when I open a source code file using the following. I have to pass the language-specific str to the foo function. How can do it in the add-hook statement?

(defun foo (str)
   (blahblah...))
(add-hook 'prog-mode-hook 'foo)

Upvotes: 2

Views: 1770

Answers (3)

user355252
user355252

Reputation:

Use the built-in apply-partially:

(add-hook 'prog-mode-hook (apply-partially #'foo "spam with eggs"))

Upvotes: 9

immerrr
immerrr

Reputation: 1273

The bad news is that mode hooks usually don't have any arguments. The good news is that here's what (emacs) Hooks section of manual has to say about your situation:

Most major modes run one or more "mode hooks" as the last step of initialization.

So, I didn't test it myself, but I'm pretty sure you can write a generic hook that will inspect major-mode variable and do the mode-specific actions instead.

Upvotes: 0

C. K. Young
C. K. Young

Reputation: 223013

This is described in the manual (first hit on Google for "emacs add-hook"):

(add-hook 'prog-mode-hook (lambda () (foo "foobarbaz")))

Upvotes: 7

Related Questions