Re: SendKeys and Python 2.7

2010-09-17 Thread Jakson A. Aquino
> On Mon, Sep 13, 2010 at 4:02 PM, Michel Claveau - MVP
>  wrote:
>> Sorry for time, but I am very busy...
>>
>> With Python + Pywin32, you can force the activation of a window (before
>> send some keys...)
>> See:
>>  win32gui.SetForegroundWindow(w_handle)
>>
>> or
>>  win32gui.SetActiveWindow(w_handle)
>>
>>
>>
>> For to find a windows (and his handle), see:
>>  win32gui.EnumWindows()
>>  win32gui.GetWindowTex()
>> or
>>  win32gui.FindWindowEx()

I ended up using Dispatch("WScript.Shell") and AppActivate("R
Console") of the win32com.client module because they are higher level
functions. I guess that your code runs faster, but I don't have an old
machine to compare the performance of the two codes. I still may
switch to your code in the future, when I have easy access to a real
machine running Windows to make some tests. The released version of
the plugin is here:
http://www.vim.org/scripts/script.php?script_id=2628

Thanks for your help!

Jakson Aquino
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: SendKeys and Python 2.7

2010-09-13 Thread Jakson A. Aquino
On Mon, Sep 13, 2010 at 4:02 PM, Michel Claveau - MVP
 wrote:
> Re!
>
> Sorry for time, but I am very busy...
>
>
> With Python + Pywin32, you can force the activation of a window (before
> send some keys...)
> See:
>  win32gui.SetForegroundWindow(w_handle)
>
> or
>  win32gui.SetActiveWindow(w_handle)
>
>
>
> For to find a windows (and his handle), see:
>  win32gui.EnumWindows()
>  win32gui.GetWindowTex()
> or
>  win32gui.FindWindowEx()

Thanks! I have found a simple workaround: sending Alt+Tab to R
Console. Tomorrow I will test your solution. I can't do it right now
because I don't have a real machine running Windows here.

Best regards,

Jakson
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: SendKeys and Python 2.7

2010-09-13 Thread Michel Claveau - MVP
Re!

Sorry for time, but I am very busy...


With Python + Pywin32, you can force the activation of a window (before
send some keys...)
See: 
  win32gui.SetForegroundWindow(w_handle)

or
  win32gui.SetActiveWindow(w_handle)



For to find a windows (and his handle), see:
  win32gui.EnumWindows()
  win32gui.GetWindowTex()
or 
  win32gui.FindWindowEx()


@-salutations
-- 
Michel Claveau 


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: SendKeys and Python 2.7

2010-09-11 Thread Lawrence D'Oliveiro
In message , Jakson A. 
Aquino wrote:

> On Sat, Sep 11, 2010 at 4:25 AM, Lawrence D'Oliveiro
>  wrote:
>
>> In message , Jakson
>> A. Aquino wrote:
>>
>>> Your code do send the ^v as expected, but I have problem with the
>>> selection of the Windows which will receive the ^v. The code above was
>>> OK in a Windows XP running inside VirtualBox, but when tested in a real
>>> machine, it proved to be highly inconsistent. Sometimes the ^v gets
>>> pasted into R, but more frequently it is pasted into Vim itself or
>>> nowhere.
>>
>> No big surprise your roundabout GUI-fiddling approach is flaky.
> 
> Thanks for your comment, but could you please suggest a feasible and
> non flaky approach?

I already did.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: SendKeys and Python 2.7

2010-09-11 Thread Jakson A. Aquino
On Fri, Sep 10, 2010 at 10:38 PM, MRAB  wrote:
> I'd add some more small sleeps to give Windows/R time to act, IYSWIM. I
> learned that from experience. :-)

I've tried adding sleeps, small and large, but they didn't make much
of a difference. Anyway, different machines could require different
amounts of sleep and, thus, this workaround may not be generalizable
enough since the plugin would be used by many people on different
systems.

Thanks,

Jakson
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: SendKeys and Python 2.7

2010-09-11 Thread Jakson A. Aquino
On Sat, Sep 11, 2010 at 4:25 AM, Lawrence D'Oliveiro
 wrote:
> In message , Jakson A.
> Aquino wrote:
>
>> Your code do send the ^v as expected, but I have problem with the
>> selection of the Windows which will receive the ^v. The code above was OK
>> in a Windows XP running inside VirtualBox, but when tested in a real
>> machine, it proved to be highly inconsistent. Sometimes the ^v gets pasted
>> into R, but more frequently it is pasted into Vim itself or nowhere.
>
> No big surprise your roundabout GUI-fiddling approach is flaky.

Thanks for your comment, but could you please suggest a feasible and
non flaky approach?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: SendKeys and Python 2.7

2010-09-11 Thread Lawrence D'Oliveiro
In message , Jakson A. 
Aquino wrote:

> Your code do send the ^v as expected, but I have problem with the
> selection of the Windows which will receive the ^v. The code above was OK
> in a Windows XP running inside VirtualBox, but when tested in a real
> machine, it proved to be highly inconsistent. Sometimes the ^v gets pasted
> into R, but more frequently it is pasted into Vim itself or nowhere.

No big surprise your roundabout GUI-fiddling approach is flaky.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: SendKeys and Python 2.7

2010-09-11 Thread Lawrence D'Oliveiro
In message , Jakson A. 
Aquino wrote:

> On Fri, Sep 10, 2010 at 7:19 PM, Lawrence D'Oliveiro
>  wrote:
>
>> In message , Jakson
>> A. Aquino wrote:
>>
>>> On Fri, Sep 10, 2010 at 6:26 AM, Lawrence D'Oliveiro
>>>  wrote:
>>
 In message ,
 Jakson A. Aquino wrote:

> I would like to send code from Vim [1] to R [2] on Microsoft Windows.

 Why such a roundabout way? Why not just run R in a subprocess and feed
 it a script to run?
>>>
>>> Emacs with ESS runs R in a subprocess (at least I think it does). Vim
>>> can't do that.
>>
>> Why not?
> 
> I don't know how to embed R into Vim ...

I wasn’t saying “embed”, I was saying “run R in a subprocess”.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: SendKeys and Python 2.7

2010-09-10 Thread MRAB

On 11/09/2010 01:45, Jakson A. Aquino wrote:

On Thu, Sep 9, 2010 at 8:24 PM, Jakson A. Aquino  wrote:

On Thu, Sep 9, 2010 at 5:40 PM, Michel Claveau - MVP
  wrote:

Hi!

Example for send ^V  (with PyWin32):

  import time,win32api,win32con
  win32api.keybd_event(win32con.VK_CONTROL, 0, 0, 0)
  win32api.keybd_event(ord('V'), 0, win32con.KEYEVENTF_EXTENDEDKEY | 0, 0)
  time.sleep(0.05)
  win32api.keybd_event(ord('V'), 0, win32con.KEYEVENTF_EXTENDEDKEY | 
win32con.KEYEVENTF_KEYUP, 0)
  win32api.keybd_event(win32con.VK_CONTROL, 0, win32con.KEYEVENTF_KEYUP, 0)


Thank you very much! Your code solved my problem. I added some lines
to set the focus into R before the ^V and then back to Vim:


Unfortunately, I was wrong. Your code do send the ^v as expected, but
I have problem with the selection of the Windows which will receive
the ^v. The code above was OK in a Windows XP running inside
VirtualBox, but when tested in a real machine, it proved to be highly
inconsistent. Sometimes the ^v gets pasted into R, but more frequently
it is pasted into Vim itself or nowhere. Below is the complete code
that I'm using. It's a vim script. The python code is delimited by
"python<<  EOL" and "EOL":

function! SendToRPy(aString)
python<<  EOL
import time
import win32api
import win32con
import win32com.client
import win32clipboard
import vim

aString = vim.eval("a:aString")
win32clipboard.OpenClipboard()
win32clipboard.EmptyClipboard()
win32clipboard.SetClipboardText(aString)
win32clipboard.CloseClipboard()
shell = win32com.client.Dispatch("WScript.Shell")
ok = shell.AppActivate("R Console")
if ok:
 win32api.keybd_event(win32con.VK_CONTROL, 0, 0, 0)
 win32api.keybd_event(ord('V'), 0, win32con.KEYEVENTF_EXTENDEDKEY | 0, 0)
 time.sleep(0.05)
 win32api.keybd_event(ord('V'), 0, win32con.KEYEVENTF_EXTENDEDKEY |
win32con.KEYEVENTF_KEYUP, 0)
 win32api.keybd_event(win32con.VK_CONTROL, 0, win32con.KEYEVENTF_KEYUP, 0)
 shell.AppActivate("Vim")
else:
 vim.command("call RWarningMsg('Is R running?')")
 time.sleep(1)
EOL
endfunction

When R isn't running, the script correctly shows the warning message
"Is R running?". Does anyone know what should I do to correctly use
the AppActivate function or is there a better approach to this
problem?


I'd add some more small sleeps to give Windows/R time to act, IYSWIM. I
learned that from experience. :-)
--
http://mail.python.org/mailman/listinfo/python-list


Re: SendKeys and Python 2.7

2010-09-10 Thread Jakson A. Aquino
On Thu, Sep 9, 2010 at 8:24 PM, Jakson A. Aquino  wrote:
> On Thu, Sep 9, 2010 at 5:40 PM, Michel Claveau - MVP
>  wrote:
>> Hi!
>>
>> Example for send ^V  (with PyWin32):
>>
>>  import time,win32api,win32con
>>  win32api.keybd_event(win32con.VK_CONTROL, 0, 0, 0)
>>  win32api.keybd_event(ord('V'), 0, win32con.KEYEVENTF_EXTENDEDKEY | 0, 0)
>>  time.sleep(0.05)
>>  win32api.keybd_event(ord('V'), 0, win32con.KEYEVENTF_EXTENDEDKEY | 
>> win32con.KEYEVENTF_KEYUP, 0)
>>  win32api.keybd_event(win32con.VK_CONTROL, 0, win32con.KEYEVENTF_KEYUP, 0)
>
> Thank you very much! Your code solved my problem. I added some lines
> to set the focus into R before the ^V and then back to Vim:

Unfortunately, I was wrong. Your code do send the ^v as expected, but
I have problem with the selection of the Windows which will receive
the ^v. The code above was OK in a Windows XP running inside
VirtualBox, but when tested in a real machine, it proved to be highly
inconsistent. Sometimes the ^v gets pasted into R, but more frequently
it is pasted into Vim itself or nowhere. Below is the complete code
that I'm using. It's a vim script. The python code is delimited by
"python << EOL" and "EOL":

function! SendToRPy(aString)
python << EOL
import time
import win32api
import win32con
import win32com.client
import win32clipboard
import vim

aString = vim.eval("a:aString")
win32clipboard.OpenClipboard()
win32clipboard.EmptyClipboard()
win32clipboard.SetClipboardText(aString)
win32clipboard.CloseClipboard()
shell = win32com.client.Dispatch("WScript.Shell")
ok = shell.AppActivate("R Console")
if ok:
win32api.keybd_event(win32con.VK_CONTROL, 0, 0, 0)
win32api.keybd_event(ord('V'), 0, win32con.KEYEVENTF_EXTENDEDKEY | 0, 0)
time.sleep(0.05)
win32api.keybd_event(ord('V'), 0, win32con.KEYEVENTF_EXTENDEDKEY |
win32con.KEYEVENTF_KEYUP, 0)
win32api.keybd_event(win32con.VK_CONTROL, 0, win32con.KEYEVENTF_KEYUP, 0)
shell.AppActivate("Vim")
else:
vim.command("call RWarningMsg('Is R running?')")
time.sleep(1)
EOL
endfunction

When R isn't running, the script correctly shows the warning message
"Is R running?". Does anyone know what should I do to correctly use
the AppActivate function or is there a better approach to this
problem?

Thanks!

Jakson Aquino
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: SendKeys and Python 2.7

2010-09-10 Thread Jakson A. Aquino
On Fri, Sep 10, 2010 at 7:19 PM, Lawrence D'Oliveiro
 wrote:
> In message , Jakson A.
> Aquino wrote:
>> On Fri, Sep 10, 2010 at 6:26 AM, Lawrence D'Oliveiro
>>  wrote:
>>> In message , Jakson
>>> A. Aquino wrote:
 I would like to send code from Vim [1] to R [2] on Microsoft Windows.
>>>
>>> Why such a roundabout way? Why not just run R in a subprocess and feed it
>>> a script to run?
>>
>> Emacs with ESS runs R in a subprocess (at least I think it does). Vim
>> can't do that.
>
> Why not?

I don't know how to embed R into Vim, but I would be grateful if you
could explain how to do it since this could be an opportunity to
improve my plugin.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: SendKeys and Python 2.7

2010-09-10 Thread Lawrence D'Oliveiro
In message , Jakson A. 
Aquino wrote:

> On Fri, Sep 10, 2010 at 6:26 AM, Lawrence D'Oliveiro
>  wrote:
>
>> In message , Jakson
>> A. Aquino wrote:
>>
>>> I would like to send code from Vim [1] to R [2] on Microsoft Windows.
>>
>> Why such a roundabout way? Why not just run R in a subprocess and feed it
>> a script to run?
> 
> Emacs with ESS runs R in a subprocess (at least I think it does). Vim
> can't do that.

Why not?

> The plugin doesn't simply send code to R. It has many other features
> that make it easier to edit R scripts.

But those are editing functions, nothing to do with R.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: SendKeys and Python 2.7

2010-09-10 Thread Jakson A. Aquino
On Fri, Sep 10, 2010 at 6:26 AM, Lawrence D'Oliveiro
 wrote:
> In message , Jakson A.
> Aquino wrote:
>
>> I would like to send code from Vim [1] to R [2] on Microsoft Windows.
>
> Why such a roundabout way? Why not just run R in a subprocess and feed it a
> script to run?

Emacs with ESS runs R in a subprocess (at least I think it does). Vim
can't do that.

The plugin doesn't simply send code to R. It has many other features
that make it easier to edit R scripts.

Best,

Jakson
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: SendKeys and Python 2.7

2010-09-10 Thread Lawrence D'Oliveiro
In message , Jakson A. 
Aquino wrote:

> I would like to send code from Vim [1] to R [2] on Microsoft Windows.

Why such a roundabout way? Why not just run R in a subprocess and feed it a 
script to run?

> [1] http://www.vim.org/
> [2] http://www.r-project.org/

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: SendKeys and Python 2.7

2010-09-09 Thread Jakson A. Aquino
On Thu, Sep 9, 2010 at 5:40 PM, Michel Claveau - MVP
 wrote:
> Hi!
>
> Example for send ^V  (with PyWin32):
>
>  import time,win32api,win32con
>  win32api.keybd_event(win32con.VK_CONTROL, 0, 0, 0)
>  win32api.keybd_event(ord('V'), 0, win32con.KEYEVENTF_EXTENDEDKEY | 0, 0)
>  time.sleep(0.05)
>  win32api.keybd_event(ord('V'), 0, win32con.KEYEVENTF_EXTENDEDKEY | 
> win32con.KEYEVENTF_KEYUP, 0)
>  win32api.keybd_event(win32con.VK_CONTROL, 0, win32con.KEYEVENTF_KEYUP, 0)

Thank you very much! Your code solved my problem. I added some lines
to set the focus into R before the ^V and then back to Vim:

import time,win32api,win32con,win32com.client

shell = win32com.client.Dispatch("WScript.Shell")
shell.AppActivate("R Console")
win32api.keybd_event(win32con.VK_CONTROL, 0, 0, 0)
win32api.keybd_event(ord('V'), 0, win32con.KEYEVENTF_EXTENDEDKEY | 0, 0)
time.sleep(0.05)
win32api.keybd_event(ord('V'), 0, win32con.KEYEVENTF_EXTENDEDKEY |
win32con.KEYEVENTF_KEYUP, 0)
win32api.keybd_event(win32con.VK_CONTROL, 0, win32con.KEYEVENTF_KEYUP, 0)
shell.AppActivate("Vim")

Best regards,

Jakson Aquino
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: SendKeys and Python 2.7

2010-09-09 Thread Michel Claveau - MVP
Hi!

Example for send ^V  (with PyWin32):

  import time,win32api,win32con
  win32api.keybd_event(win32con.VK_CONTROL, 0, 0, 0)
  win32api.keybd_event(ord('V'), 0, win32con.KEYEVENTF_EXTENDEDKEY | 0, 0) 
  time.sleep(0.05)
  win32api.keybd_event(ord('V'), 0, win32con.KEYEVENTF_EXTENDEDKEY | 
win32con.KEYEVENTF_KEYUP, 0)
  win32api.keybd_event(win32con.VK_CONTROL, 0, win32con.KEYEVENTF_KEYUP, 0)



@-salutations
-- 
Michel Claveau 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: SendKeys and Python 2.7

2010-09-09 Thread Albert Hopkins
On Thu, 2010-09-09 at 07:07 -0300, Jakson A. Aquino wrote:
> Vim needs python 2.7

>From where do you base this assertion?  I have been using vim 7.3 (with
embedded python) with python 2.6 pretty much since it has been released.

:version
VIM - Vi IMproved 7.3 (2010 Aug 15, compiled Aug 29 2010
07:21:44)
[...]

:python import sys; print sys.version
2.6.5 (release26-maint, Aug 28 2010, 21:57:54)
[GCC 4.4.4]



-- 
http://mail.python.org/mailman/listinfo/python-list


SendKeys and Python 2.7

2010-09-09 Thread Jakson A. Aquino
Hi,

I would like to send code from Vim [1] to R [2] on Microsoft Windows.
Vim needs python 2.7 but the pre-compiled SendKeys module [3] is only
avaiable for python 2.6. How can I make SendKeys work with python 2.7?
I don't know how to compile python code.

Is there an alternative to SendKeys? I'm sending to R only "^v" to
paste the content of the clipboard.

Thanks,

Jakson Aquino

[1] http://www.vim.org/
[2] http://www.r-project.org/
[3] http://www.rutherfurd.net/python/sendkeys/#binaries
-- 
http://mail.python.org/mailman/listinfo/python-list