How to find if gvim has focus?

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

How to find if gvim has focus?

Groleo Marius
Hi!

Is it possible to find if the gvim window is active, using a vim command?

--
Regards, Groleo!

# touch universe
# chmod +x universe
# ./universe
Reply | Threaded
Open this post in threaded view
|

Re: How to find if gvim has focus?

James Vega-3
On Fri, Sep 09, 2005 at 05:16:41PM +0300, Groleo Marius wrote:
> Is it possible to find if the gvim window is active, using a vim command?

You could add a couple of autocommands to your vimrc that act on
FocusLost/FocusGained.  For example:

au FocusGained * let g:focused = 1
au FocusLost * let g:focused = 0

Then you could just check the value of g:focused when you want to know
if gvim has focus.

There are some caveats, so I suggest reading the help for FocusGained
and FocusLost.

James
--
GPG Key: 1024D/61326D40 2003-09-02 James Vega <[hidden email]>

attachment0 (204 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: How to find if gvim has focus?

A.J.Mechelynck
In reply to this post by Groleo Marius
----- Original Message -----
From: "Groleo Marius" <[hidden email]>
To: <[hidden email]>
Sent: Friday, September 09, 2005 4:16 PM
Subject: How to find if gvim has focus?


> Hi!
>
> Is it possible to find if the gvim window is active, using a vim command?
>
> --
> Regards, Groleo!
>
> # touch universe
> # chmod +x universe
> # ./universe

There are autocommand events for that:
see
    :help FocusGained
    :help FocusLost

however, if gvim _doesn't_ have focus it will probably simply not be
executing anything.

but maybe I misunderstood? To find if you are running in the GUI or in
console mode, you can test

    if has("gui_running")
        echo "GUI is running or about to start"
    else
        echo "this is console Vim"
    endif

Best regards,
Tony.


Reply | Threaded
Open this post in threaded view
|

Re: How to find if gvim has focus?

Tim Chase-2
In reply to this post by Groleo Marius
> Is it possible to find if the gvim window is active, using a
> vim command?

function! VimHasFocus()
        return 1
endfunc

bosh...not having vim active all the time...what sort of heathen
are you?!  :)

Seriously though, I don't think so.  However, in what sort of
scenario would you be using it?  Since vim doesn't have any sort
of timer/background method, about every action I can think of
would be initiated from within the vim window itself, so
obviously, the vim window has focus in order to initiate that action.

Edge cases to my knowledge on that...possibly dragging and
dropping a file from elsewhere

        :help drag-n-drop-win32

or using vim's built-in client/server calls as detailed in

        :help client-server

Either of these might trigger events that would run code, while
the vim window proper doesn't have focus.

-tim







Reply | Threaded
Open this post in threaded view
|

Re: How to find if gvim has focus?

Groleo Marius
On 9/9/05, Tim Chase <[hidden email]> wrote:

> > Is it possible to find if the gvim window is active, using a
> > vim command?
>
> function! VimHasFocus()
>         return 1
> endfunc
>
> bosh...not having vim active all the time...what sort of heathen
> are you?!  :)
>
> Seriously though, I don't think so.  However, in what sort of
> scenario would you be using it?  Since vim doesn't have any sort
> of timer/background method, about every action I can think of
> would be initiated from within the vim window itself, so
> obviously, the vim window has focus in order to initiate that action.
>
> Edge cases to my knowledge on that...possibly dragging and
> dropping a file from elsewhere
>
>         :help drag-n-drop-win32
>
> or using vim's built-in client/server calls as detailed in
>
>         :help client-server
>
> Either of these might trigger events that would run code, while
> the vim window proper doesn't have focus.
>
> -tim
>
>
>
>
>
>
>
>

Well, I made a vim script, with embeded ruby in it.
Ruby creates a treeview containing the possible completions for a word.
The problem apears when I switch away from gvim's window, since the
TreeView is a POPUP window, I have to know when to destry it( the window
manager wont handle popups).


--
Regards, Groleo!

# touch universe
# chmod +x universe
# ./universe