;ς
ΞΡAHc           @   s  d  Z  d k Z d k Z d k Z d k Z d k Z d k Z d k Z e i	 e i
 BZ e a e i i   Z d e i f d     YZ d   Z e i d e d  d e i i d  d	  Z d
   Z d e _ d e _ e i d e  e i d  Z d   Z e i d e  d S(   s+   Integration with gvim, by Erich Heine

Provides a %vim magic command, and reuses the same vim session. Uses 
unix domain sockets for communication between vim and IPython. ipy.vim is 
available in doc/examples of the IPython distribution.

Slightly touched up email announcement (and description how to use it) by 
Erich Heine is here:

Ive recently been playing with ipython, and like it quite a bit. I did
however discover a bit of frustration, namely with editor interaction.
I am a gvim user, and using the command edit on a new file causes
ipython to try and run that file as soon as the text editor opens
up. The -x command of course fixes this, but its still a bit annoying,
switching windows to do a run file, then back to the text
editor. Being a heavy tab user in gvim, another annoyance is not being
able to specify weather a new tab is how I choose to open the file.

Not being one to shirk my open source duties (and seeing this as a
good excuse to poke around ipython internals), Ive created a script
for having gvim and ipython work very nicely together. Ive attached
both to this email (hoping of course that the mailing list allows such
things).

There are 2 files: 

ipy_vimserver.py -- this file contains the ipython stuff
ipy.vim -- this file contains the gvim stuff

In combination they allow for a few functionalities:

#1. the vim magic command. This is a fancy wrapper around the edit
magic, that allows for a new option, -t, which opens the text in a new
gvim tab.  Otherwise it works the same as edit -x. (it internally
calls edit -x). This magic command also juggles vim server management,
so when it is called when there is not a gvim running, it creates a
new gvim instance, named after the ipython session name.  Once such a
gvim instance is running, it will be used for subsequent uses of the
vim command.

#2. ipython - gvim interaction. Once a file has been opened with the
vim magic (and a session set up, see below), pressing the F5 key in
vim will cause the calling ipython instance to execute run
filename.py. (if you typo like I do, this is very useful)

#3. ipython server - this is a thread wich listens on a unix domain
socket, and runs commands sent to that socket.

Note, this only works on POSIX systems, that allow for AF_UNIX type
sockets. It has only been tested on linux (a fairly recent debian
testing distro).

To install it put, the ipserver.py in your favorite locaion for
sourcing ipython scripts. I put the ipy.vim in
~/.vim/after/ftplugin/python/.

To use (this can be scripted im sure, but i usually have 2 or 3
ipythons and corresponding gvims open):

import ipy_vimserver
ipy_vimserver.setup('sessionname')

(Editors note - you can probably add these to your ipy_user_conf.py)

Then use ipython as you normally would, until you need to edit
something. Instead of edit, use the vim magic.  Thats it!

Ns	   IpyServerc           B   sM   t  Z d   Z d   Z d   Z d   Z e Z d   Z d   Z d   Z	 RS(   Nc         C   sf   t  t |   i   t |  _ | |  _ t i t i	  |  _ t
 i   |  _ t   |  _ |  i t  d  S(   N(   s   supers	   IpyServers   selfs   __init__s   Trues   keep_runnings   snames   _IpyServer__snames   sockets   AF_UNIXs   selects   polls   pollers   dicts   current_connss	   setDaemon(   s   selfs   sname(    (    sQ   /u/d/b/dbachman/=/lib/python2.3/site-packages/IPython/Extensions/ipy_vimserver.pys   __init__S   s    		c         C   s'   |  i i |  i  |  i i d  d  S(   Ni   (   s   selfs   sockets   binds   _IpyServer__snames   listen(   s   self(    (    sQ   /u/d/b/dbachman/=/lib/python2.3/site-packages/IPython/Extensions/ipy_vimserver.pys   listen\   s    c         C   s   | |  i  i   j o5 t |  _ x! |  i i   D] } | i   q/ Wt Sn |  i | } |  i | =| i   |  i
 i |  d  S(   N(   s   sockets   selfs   filenos   Falses   keep_runnings   current_connss   valuess   as   closes   ys   pollers
   unregister(   s   selfs   sockets   as   y(    (    sQ   /u/d/b/dbachman/=/lib/python2.3/site-packages/IPython/Extensions/ipy_vimserver.pys   __handle_error`   s    	 

c         C   s6  |  i   |  i i |  i t i t B xψ |  i oν y |  i i	 d  } Wn
 q* n X| o q* n x΄ | D]¬ \ } } | t @o |  i |  o qm q  Pn | |  i i   j oD |  i i   d } |  i i | t i t B | |  i | i   <n |  i i |  } |  i |  qm Wq* Wt i |  i  d  S(   Ni   i    (   s   selfs   listens   pollers   registers   sockets   selects   POLLINs   ERRCONDSs   keep_runnings   polls   avails   socks   condss   _IpyServer__handle_errors   filenos   accepts   ys   current_connss   gets   handle_requests   oss   removes   _IpyServer__sname(   s   selfs   condss   socks   avails   y(    (    sQ   /u/d/b/dbachman/=/lib/python2.3/site-packages/IPython/Extensions/ipy_vimserver.pys   serve_mel   s.    
 
   c         C   s   t  |  _ d  S(   N(   s   Falses   selfs   keep_running(   s   self(    (    sQ   /u/d/b/dbachman/=/lib/python2.3/site-packages/IPython/Extensions/ipy_vimserver.pys   stop   s    c         C   s]   | i d  x< |  i o1 y | i d  } Wn t i j
 o q XPq W|  i |  d  S(   Ni   i   (	   s   socks
   settimeouts   selfs   keep_runnings   recvs   xs   sockets   timeouts   do_it(   s   selfs   socks   x(    (    sQ   /u/d/b/dbachman/=/lib/python2.3/site-packages/IPython/Extensions/ipy_vimserver.pys   handle_request   s     
c         C   sG   | i d  } t   } x | D] } | i |  q Wt i |  d  S(   Ns   
(   s   datas   splits   lists   cmdss   lines   appends   ips   runlines(   s   selfs   datas   cmdss   line(    (    sQ   /u/d/b/dbachman/=/lib/python2.3/site-packages/IPython/Extensions/ipy_vimserver.pys   do_it   s    	 (
   s   __name__s
   __module__s   __init__s   listens   _IpyServer__handle_errors   serve_mes   runs   stops   handle_requests   do_it(    (    (    sQ   /u/d/b/dbachman/=/lib/python2.3/site-packages/IPython/Extensions/ipy_vimserver.pys	   IpyServerR   s   							c         C   s2   t  o t  i   t  i d  n t i i  d  S(   Ni   (   s   SERVERs   stops   joins   IPythons   ipapis   TryNext(   s   self(    (    sQ   /u/d/b/dbachman/=/lib/python2.3/site-packages/IPython/Extensions/ipy_vimserver.pys   shutdown_server    s    
s   shutdown_hooki
   s    s   ~/.ipython/c         C   s   |  o
 |  } n0 t i i d  o t i i d  } n d } | t _ t i i | |  t _
 t o t t i
  a t i   n d  S(   Ns   IPY_SESSIONs   IPYS(   s   sessionnames   sessions   oss   environs   gets   vimhooks	   vimservers   paths   joins	   socketdirs	   ipyservers   SERVERs	   IpyServers   start(   s   sessionnames	   socketdirs   session(    (    sQ   /u/d/b/dbachman/=/lib/python2.3/site-packages/IPython/Extensions/ipy_vimserver.pys   setupͺ   s     
	c         C   s"  t  i i   } t i i   } t i	 d d t i
 d t }	 |	 i   g  i } |	 i i   D]! } | | j o | |  q] q] ~ }
 |
 o d t i f } n d } d t _ t i | d <t i | d <| t j o
 d } n d | } d	 t i | | | f } t i | d
 | d t d  S(   Ns   gvim --serverlists   stdouts   shells
   --remote%ss    s   IPY_SESSIONs
   IPY_SERVERs   +s   gvim --servername %s %s %s %ss   env(   s   oss   environs   copys   envs   vimhooks	   vimservers   uppers   vservers
   subprocesss   Popens   PIPEs   Trues   checks   waits   appends   _[1]s   stdouts	   readliness   ls   cvals   extrass   vimargss	   ipyservers   lines   Nones   fnames   vim_cmds   call(   s   selfs   fnames   lines   _[1]s   vimargss   envs   ls   vservers   vim_cmds   checks   cval(    (    sQ   /u/d/b/dbachman/=/lib/python2.3/site-packages/IPython/Extensions/ipy_vimserver.pys   vimhookΏ   s"    
A	 

s   editors
   -t(?:\s|$)c            sE     d   } | t d  \ }   | t _ d     t i    d  S(   Nc            sJ   t  |  i     } | o  |  i d    } | | f Sn d   f Sd  S(   Ns    (   s   lens   pats   findalls   argstrs   xs   subs   as   rarg(   s   pats   rargs   as   x(   s   argstr(    sQ   /u/d/b/dbachman/=/lib/python2.3/site-packages/IPython/Extensions/ipy_vimserver.pys   do_argβ   s
    s   -tabs   edit -x (   s   do_args   newtabs   ts   argstrs   vimhooks   extrass   ips   magic(   s   selfs   argstrs   do_args   t(    (   s   argstrsQ   /u/d/b/dbachman/=/lib/python2.3/site-packages/IPython/Extensions/ipy_vimserver.pys   vimα   s
    	
s   vim(   s   __doc__s   IPython.ipapis   IPythons   sockets   selects   oss	   threadings
   subprocesss   res   POLLHUPs   POLLERRs   ERRCONDSs   Nones   SERVERs   ipapis   gets   ips   Threads	   IpyServers   shutdown_servers   set_hooks   paths
   expandusers   setups   vimhooks	   vimservers	   ipyservers   compiles   newtabs   vims   expose_magic(   s   ERRCONDSs   sockets   vimhooks   res	   IpyServers   ips   setups
   subprocesss   vims	   threadings   shutdown_servers   newtabs   IPythons   oss   select(    (    sQ   /u/d/b/dbachman/=/lib/python2.3/site-packages/IPython/Extensions/ipy_vimserver.pys   ?D   s$   		N					