Vim timer, I need help.
I need a timer in a vim script I want to improve. Vim does not support the idea of a timer in Vim script. So doing something like:
!fun callBack()
do stuff
endfun
timer( 300, callBack )
Is not possible.
I've tried using Python's threading.Timer and signals, SIGALRM, in Python with little luck. I actually got the Timer object to work, but when the Timer thread would run it would call vim.command( "stuff" ) and it would take seconds for "stuff" to execute. I'm guessing it's a threading issue.
I've looked into using the Vim clientserver feature with Python's Timer. So if the Timer goes off the Timer thread would spawn a call to vim and use the new vim process to send commands to the already running vim process. I think this would work. But, on Fedora 11 at least, Vim clientserver is not enabled for console Vim, it only works on GVim. I use console Vim.
So, suggestions please. Know any good timer like hacks? I'm looking for millisecond precision.
Edit: Explain what I'm trying to accomplish.
There is a plugin I like, popup_it.vim but it's as annoying as it is useful. The problem with it is if your in a context it feels you need completion on it will start that completion the moment you type the first letter that puts you in that context. This is really annoying as you can be typing along at a good pace, and you don't need/want any completion, but the completion popup starts flying around and messing you up. So I want a delay. Don't start any completions until I've been idle for a given time. That's all. Very simple. But not easy to accomplish in vim.
Posted Oct 13, 2009, 7:29 PM UTC · edited
Why not rebuild vim and see if it works?
On the list of things to try. The only problem is it's not a portable solution. I don't want to build my own Vim for every machine I want this feature.
I would like to know why Fedora builds the console version without clientserver. As far as I can tell, it seems the clientserver functionality requires the use of some X libraries. If that's the case it makes sense to be able to have console Vim without the X requirements. But I don't know if that's actually the case yet.
I believe GVim will run on a console, without X if the name of the program does not start with g. On RHEL systems where I work, you can run vimx to get the full featured version in a terminal.
I just looked through the Fedora 11 vim spec file and found some interesting things. gvim is, of course, configured with --with-x while vim is configured without --with-x. So if you run just 'vim', there is no clientserver enabled. You must build vim with X to get client server.
But, you can run
to run the gvim binary in console mode and get the clientserver functionality.
On to the experiment!
Using the clientserver functions with python's threading.Timer works!
If the Timer expires it runs a vim sub process that sends a remote command to the original vim's server. It's still not what I would consider a great solution, but it will do.
I had to alter my environment a little bit. I added an alias to my .bashrc
This makes sure the 'vim' command will start a console vim up with a server instance running and listening. My initial test runs this bit of python if a Timer expires:
Which gives a basic word completion after a delay. Now I just need to adapt it to use the popup_it plugin's logic for choosing the completion type and I'm good to go. For now.
Thanks for listening and responding to my questions, it has really helped me work this out.