Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a hook to only run in a particular mode?

Tags:

emacs

hook

I have the following defun

(defun a-test-save-hook()   "Test of save hook"   (message "banana")   ) 

that I use via the following hook

(add-hook 'after-save-hook 'a-test-save-hook) 

This works as expected. What I would like to do is to limit the hook to particular mode, in this case org-mode. Any ideas as to how I would go about this?

Thanks in advance.

like image 421
Aaron Barclay Avatar asked May 26 '11 11:05

Aaron Barclay


People also ask

Can you use hooks conditionally?

Don't call Hooks inside loops, conditions, or nested functions. Instead, always use Hooks at the top level of your React function, before any early returns. By following this rule, you ensure that Hooks are called in the same order each time a component renders.

How do you make use effect run only once?

Side Effect Runs Only Once After Initial Render You do not want to make this API call again. You can pass an empty array as the second argument to the useEffect hook to tackle this use case. useEffect(() => { // Side Effect }, []); In this case, the side effect runs only once after the initial render of the component.

What is useEffect () hook?

The useEffect Hook allows you to perform side effects in your components. Some examples of side effects are: fetching data, directly updating the DOM, and timers. useEffect accepts two arguments. The second argument is optional. useEffect(<function>, <dependency>)

Can I use a custom hook inside useEffect?

You can't use a hook inside another hook because it breaks the rule Call Hooks from React function components and the function you pass to useEffect is a regular javascript function. What you can do is call a hook inside another custom hook.


1 Answers

If you take a look at the documentation for add-hook (or C-h f add-hook RET), you'll see that one possible solution is to make the hook local to the major modes you want. This is slightly more involved than vderyagin's answer, and looks like this:

(add-hook 'org-mode-hook            (lambda ()               (add-hook 'after-save-hook 'a-test-save-hook nil 'make-it-local))) 

The 'make-it-local is the flag (can be anything that isn't nil) that tells add-hook to add the hook only in the current buffer. With the above, you'll only get the a-test-save-hook added in org-mode.

This is nice if you want to use a-test-save-hook in more than one mode.

The documentation for add-hook is:

add-hook is a compiled Lisp function in `subr.el'.  (add-hook HOOK FUNCTION &optional APPEND LOCAL)  Add to the value of HOOK the function FUNCTION. FUNCTION is not added if already present. FUNCTION is added (if necessary) at the beginning of the hook list unless the optional argument APPEND is non-nil, in which case FUNCTION is added at the end.  The optional fourth argument, LOCAL, if non-nil, says to modify the hook's buffer-local value rather than its default value. This makes the hook buffer-local if needed, and it makes t a member of the buffer-local value.  That acts as a flag to run the hook functions in the default value as well as in the local value.  HOOK should be a symbol, and FUNCTION may be any valid function.  If HOOK is void, it is first set to nil.  If HOOK's value is a single function, it is changed to a list of functions. 
like image 169
Trey Jackson Avatar answered Sep 20 '22 20:09

Trey Jackson