Show More
@@ -1,67 +1,143 b'' | |||
|
1 | 1 | if !exists("$IPY_SESSION") |
|
2 | 2 | finish |
|
3 | 3 | endif |
|
4 | 4 | |
|
5 | 5 | " set up the python interpreter within vim, to have all the right modules |
|
6 | 6 | " imported, as well as certain useful globals set |
|
7 | 7 | python import socket |
|
8 | 8 | python import os |
|
9 | 9 | python import vim |
|
10 | python from IPython.Debugger import Pdb | |
|
10 | 11 | python IPYSERVER = None |
|
12 | python reselect = True | |
|
11 | 13 | |
|
12 | 14 | python << EOF |
|
13 | 15 | # do we have a connection to the ipython instance? |
|
14 | 16 | def check_server(): |
|
15 | 17 | global IPYSERVER |
|
16 | 18 | if IPYSERVER: |
|
17 | 19 | return True |
|
18 | 20 | else: |
|
19 | 21 | return False |
|
20 | 22 | |
|
21 | 23 | # connect to the ipython server, if we need to |
|
22 | 24 | def connect(): |
|
23 | 25 | global IPYSERVER |
|
24 | 26 | if check_server(): |
|
25 | 27 | return |
|
26 | 28 | try: |
|
27 | 29 | IPYSERVER = socket.socket(socket.AF_UNIX) |
|
28 | 30 | IPYSERVER.connect(os.environ.get('IPY_SERVER')) |
|
29 | 31 | except: |
|
30 | 32 | IPYSERVER = None |
|
31 | 33 | |
|
32 | 34 | def disconnect(): |
|
33 | 35 | if IPYSERVER: |
|
34 | 36 | IPYSERVER.close() |
|
35 | 37 | |
|
36 | 38 | def send(cmd): |
|
37 | 39 | x = 0 |
|
38 | 40 | while True: |
|
39 | 41 | x += IPYSERVER.send(cmd) |
|
40 | 42 | if x < len(cmd): |
|
41 | 43 | cmd = cmd[x:] |
|
42 | 44 | else: |
|
43 | 45 | break |
|
44 | 46 | |
|
45 | 47 | def run_this_file(): |
|
46 | 48 | if check_server(): |
|
47 | 49 | send('run %s' % (vim.current.buffer.name,)) |
|
48 | 50 | else: |
|
49 | 51 | raise Exception, "Not connected to an IPython server" |
|
52 | print "\'run %s\' sent to ipython" % vim.current.buffer.name | |
|
53 | ||
|
54 | def run_this_line(): | |
|
55 | if check_server(): | |
|
56 | send(vim.current.line) | |
|
57 | print "line \'%s\' sent to ipython"% vim.current.line | |
|
58 | else: | |
|
59 | raise Exception, "Not connected to an IPython server" | |
|
60 | ||
|
61 | def run_these_lines(): | |
|
62 | r = vim.current.range | |
|
63 | if check_server(): | |
|
64 | #send(str((vim.current.range.start,vim.current.range.end))) | |
|
65 | for l in vim.current.buffer[r.start:r.end+1]: | |
|
66 | send(str(l)+'\n') | |
|
67 | #send(str(vim.current.buffer[vim.current.range.start:vim.current.range.end]).join("\n")) | |
|
68 | #print "lines %d-%d sent to ipython"% (r.start,r.end) | |
|
69 | else: | |
|
70 | raise Exception, "Not connected to an IPython server" | |
|
71 | ||
|
72 | #reselect the previously highlighted block | |
|
73 | if reselect: | |
|
74 | vim.command("normal gv") | |
|
75 | #vim lines start with 1 | |
|
76 | print "lines %d-%d sent to ipython"% (r.start+1,r.end+1) | |
|
77 | ||
|
78 | def toggle_reselect(): | |
|
79 | global reselect | |
|
80 | reselect=not reselect | |
|
81 | print "F9 will%sreselect lines after sending to ipython"% (reselect and " " or " not ") | |
|
82 | ||
|
83 | def set_breakpoint(): | |
|
84 | if check_server(): | |
|
85 | send("__IP.InteractiveTB.pdb.set_break('%s',%d)" % (vim.current.buffer.name, | |
|
86 | vim.current.window.cursor[0])) | |
|
87 | print "set breakpoint in %s:%d"% (vim.current.buffer.name, | |
|
88 | vim.current.window.cursor[0]) | |
|
89 | else: | |
|
90 | raise Exception, "Not connected to an IPython server" | |
|
91 | ||
|
92 | def clear_breakpoint(): | |
|
93 | if check_server(): | |
|
94 | send("__IP.InteractiveTB.pdb.clear_break('%s',%d)" % (vim.current.buffer.name, | |
|
95 | vim.current.window.cursor[0])) | |
|
96 | print "clearing breakpoint in %s:%d" % (vim.current.buffer.name, | |
|
97 | vim.current.window.cursor[0]) | |
|
98 | else: | |
|
99 | raise Exception, "Not connected to an IPython server" | |
|
100 | ||
|
101 | def clear_all_breakpoints(): | |
|
102 | if check_server(): | |
|
103 | send("__IP.InteractiveTB.pdb.clear_all_breaks()"); | |
|
104 | print "clearing all breakpoints" | |
|
105 | else: | |
|
106 | raise Exception, "Not connected to an IPython server" | |
|
107 | ||
|
108 | def run_this_file_pdb(): | |
|
109 | if check_server(): | |
|
110 | send(' __IP.InteractiveTB.pdb.run(\'execfile("%s")\')' % (vim.current.buffer.name,)) | |
|
111 | else: | |
|
112 | raise Exception, "Not connected to an IPython server" | |
|
113 | print "\'run %s\' using pdb sent to ipython" % vim.current.buffer.name | |
|
114 | ||
|
115 | #XXX: have IPYSERVER print the prompt (look at Leo example) | |
|
50 | 116 | EOF |
|
51 | 117 | |
|
52 | 118 | fun! <SID>toggle_send_on_save() |
|
53 | 119 | if exists("s:ssos") && s:ssos == 1 |
|
54 | 120 | let s:ssos = 0 |
|
55 | 121 | au! BufWritePost *.py :py run_this_file() |
|
56 | 122 | echo "Autosend Off" |
|
57 | 123 | else |
|
58 | 124 | let s:ssos = 1 |
|
59 | 125 | au BufWritePost *.py :py run_this_file() |
|
60 | 126 | echo "Autowsend On" |
|
61 | 127 | endif |
|
62 | 128 | endfun |
|
63 | 129 | |
|
64 | 130 | map <silent> <F5> :python run_this_file()<CR> |
|
65 | imap <silent> <C-F5> <ESC><F5>a | |
|
66 | map <F7> :call <SID>toggle_send_on_save()<CR> | |
|
131 | map <silent> <S-F5> :python run_this_line()<CR> | |
|
132 | map <silent> <F9> :python run_these_lines()<CR> | |
|
133 | map <silent> <S-F9> :python toggle_reselect()<CR> | |
|
134 | map <silent> <C-F6> :python send('%pdb')<CR> | |
|
135 | map <silent> <F6> :python set_breakpoint()<CR> | |
|
136 | map <silent> <s-F6> :python clear_breakpoint()<CR> | |
|
137 | map <silent> <F7> :python run_this_file_pdb()<CR> | |
|
138 | map <silent> <s-F7> :python clear_all_breaks()<CR> | |
|
139 | imap <C-F5> <ESC><F5>a | |
|
140 | imap <S-F5> <ESC><S-F5>a | |
|
141 | imap <silent> <F5> <ESC><F5>a | |
|
142 | map <C-F5> :call <SID>toggle_send_on_save()<CR> | |
|
67 | 143 | py connect() |
General Comments 0
You need to be logged in to leave comments.
Login now