views:

303

answers:

3

Is it possible to configure Emacs, so that it saves all files when the emacs window loses focus?

+2  A: 

Not sure if this is what you want.

(defun dld-deselect-frame-hook ()
  (save-some-buffers 1))

(add-hook 'deselect-frame-hook 'dld-deselect-frame-hook)

From: http://www.dribin.org/dave/blog/archives/2003/09/10/emacs/

EDIT: It only seems to work in XEmacs

Yeah, I looked all -hooks and -functions, but I didn't find something that says select or focus.
Rockiger
A: 

You can use `auto-save-interval' to save every n characters you type. Mine is set to 100. So about every 2-3 lines of code, maybe?

auto-save-interval is a variable defined in `C source code'. Its value is 100

Documentation: *Number of input events between auto-saves. Zero means disable autosaving due to number of characters typed.

You can customize this variable.

This doesn't answer your original question; it's just a way to achieve something similar.

Denis Bueno
I know, the feature I am talking about is from Scribes.It is very convient when editing html and the like, you don't have to press C-x C-s anymore, you just change the window and check your browser.
Rockiger
+1  A: 

[…] the feature I am talking about is from Scribes. It is very convient when editing html and the like, you don't have to press C-x C-s anymore, you just change the window and check your browser.

In that case, instead of switching to the browser application, order Emacs to load the browser application (C-c C-v or M-x browse-url-of-buffer). With this method, you can write your own function that saves the buffer and then brings the browser up, like:

(defun my-browse-url-of-buffer ()
  "Save current buffer and view its content in browser."
  (interactive)
  (save-buffer)
  (browse-url-of-buffer))

And hook it to a convenient binding.

Or you can still use the html-autoview-mode that each time you saves the buffer, automatically loads the file into your favorite browser.

Török Gábor
This sounds like an interesting solution. I will try this. But I am still a bit suprised, that Emacs can't react on somthing like changing focus.
Rockiger
@Rockiger it seems that Emacs doesn't provide a way to hook to the unfocus event.
Török Gábor