File: ipy.vim

package info (click to toggle)
ipython 0.8.4-1
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 3,564 kB
  • ctags: 3,288
  • sloc: python: 22,341; lisp: 262; sh: 60; makefile: 29
file content (67 lines) | stat: -rw-r--r-- 1,530 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
if !exists("$IPY_SESSION")
    finish
endif

" set up the python interpreter within vim, to have all the right modules
" imported, as well as certain useful globals set
python import socket
python import os
python import vim
python IPYSERVER = None

python << EOF
# do we have a connection to the ipython instance?
def check_server():
    global IPYSERVER
    if IPYSERVER:
        return True
    else:
        return False

# connect to the ipython server, if we need to
def connect():
    global IPYSERVER
    if check_server():
        return
    try:
        IPYSERVER = socket.socket(socket.AF_UNIX)
        IPYSERVER.connect(os.environ.get('IPY_SERVER'))
    except:
        IPYSERVER = None

def disconnect():
    if IPYSERVER:
        IPYSERVER.close()

def send(cmd):
    x = 0
    while True:
        x += IPYSERVER.send(cmd)
        if x < len(cmd):
            cmd = cmd[x:]
        else:
            break

def run_this_file():
    if check_server():
        send('run %s' % (vim.current.buffer.name,))
    else:
        raise Exception, "Not connected to an IPython server"
EOF

fun! <SID>toggle_send_on_save()
    if exists("s:ssos") && s:ssos == 1
        let s:ssos = 0
        au! BufWritePost *.py :py run_this_file()
        echo "Autosend Off"
    else
        let s:ssos = 1
        au BufWritePost *.py :py run_this_file()
        echo "Autowsend On"
    endif
endfun

map <silent> <F5> :python run_this_file()<CR>
imap <silent> <C-F5> <ESC><F5>a
map <F7> :call <SID>toggle_send_on_save()<CR>
py connect()