help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Save on losing focus in Emacs


From: Tim X
Subject: Re: Save on losing focus in Emacs
Date: Mon, 02 Jul 2007 19:02:50 +1000
User-agent: Gnus/5.11 (Gnus v5.11) Emacs/22.1.50 (gnu/linux)

Tassilo Horn <address@hidden> writes:

> David Kastrup <address@hidden> writes:
>
> Hi David,
>
>> Try advising the following function, 
>
> [...]
>
>> handle-switch-frame is an interactive built-in function in `C source code'.
>> It is bound to <switch-frame>.
>> (handle-switch-frame EVENT)
>
> I tried it, but it only works if I switch from one emacs frame to
> another emacs frame.
>
> ,----[ ~/.emacs ]
> | (defadvice handle-switch-frame (before save-some-buffers-advice activate)
> |   "Save all file buffers if emacs looses focus."
> |   (save-some-buffers t))
> `----
>
> The OP wants emacs to save all files when switching to other
> applications like a browser (that's not an emacs browser in another
> frame).
>
> Any ideas?
>

Just some ideas off the top of my head. To what extent they are useful.....

The problem is likely that focus is handled by the window manager. Emacs knows
which of its frames is in focus, but doesn't really know about anything
happening outside and there doesn't seem to be any simple way to determine when
a frame loses focus (things seem more oriented to notification when a frame
gains focus. 

However, two functions which may be useful are visible-frame-list and
filtered-frame-list. To make use of these, I think you would dneed to use
run-with -idle-timer. Yo could possibly define a function that looks to see if
visible-frame-list returns anything. If it doesn't, then save any buffers with
unsaved changes. 

The problem with this is that if you have a big enough display, you may have
both your emacs frame and some other application both 'visible', but focus is
in the other application. In which case, this wouldn't work. 

Alternatively, you might be able to use mouse-position to determine when the
mouse is not in an emacs frame - the docs don't seem very clear on this and you
may need to experiment to see what this function actually returns if it is 
called
when the mouse focus is on another app. Its likely emacs will report the last
frame the mouse was focused on. 

Personally, I'd just rely on Emac's auto saving feature. As it currently
stands, files are auto-saved after a certain number of 'events' or after a
certain amount of idle time. You should be able to tweak the settings to save
things often enough that it is unlikely you will lose data and at the same
time, the saving doesn't interfere with normal work. 

HTH

Tim

P.S. I do find the OPs example a bit odd. I've used Emacs to develop with for
years and often need to switch to a browser or some other app and I've never
ever lost any work. I guess they must be using an OS like Windows, in which it
is easy/common for one app to crash and drag everything else down with it!




-- 
tcross (at) rapttech dot com dot au


reply via email to

[Prev in Thread] Current Thread [Next in Thread]