Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Add after-save-hook to a mode

131 views
Skip to first unread message

Florian Lindner

unread,
May 27, 2012, 6:00:54 PM5/27/12
to help-gn...@gnu.org
Hello,

I want to org-mobile-push executed everytime a file in org-mode is
saved. I tried that:

(add-hook 'org-mode-hook
(add-hook 'after-save-hook
(lambda ()
(org-mobile-push)
)))

This seems to war, but the hook is executed regardless of the mode the
file is in. How can I restrict it to org-mode only?

Thanks,

Florian

Jambunathan K

unread,
May 27, 2012, 6:11:27 PM5/27/12
to Florian Lindner, help-gn...@gnu.org
(add-hook 'after-save-hook
(lambda ()
(when (string= mode-name "Org")
(org-mobile-push))))


> Thanks,
>
> Florian
>
>

--

Philipp Haselwarter

unread,
May 28, 2012, 5:08:04 AM5/28/12
to help-gn...@gnu.org
Your code modifies `after-save-hook', which is a global variable, every
time `org-mode-hook' is run.
`add-hook' also allows to modify hooks buffer-locally. From its
docstring:

> The optional fourth argument, LOCAL, if non-nil, says to modify
> the hook's buffer-local value rather than its global value.
> This makes the hook buffer-local, and it makes t a member of the
> buffer-local value. That acts as a flag to run the hook
> functions of the global value as well as in the local value.

You can use this to change the value of `after-save-hook' only in those
buffers that run the `org-mode-hook'. This should do:


(add-hook
'org-mode-hook
(lambda nil
(add-hook 'after-save-hook
(lambda nil (org-mobile-push))
nil 'local))) ;Only in the current buffer


--
Philipp Haselwarter


0 new messages