Show More
@@ -1,67 +1,143 b'' | |||||
1 | if !exists("$IPY_SESSION") |
|
1 | if !exists("$IPY_SESSION") | |
2 | finish |
|
2 | finish | |
3 | endif |
|
3 | endif | |
4 |
|
4 | |||
5 | " set up the python interpreter within vim, to have all the right modules |
|
5 | " set up the python interpreter within vim, to have all the right modules | |
6 | " imported, as well as certain useful globals set |
|
6 | " imported, as well as certain useful globals set | |
7 | python import socket |
|
7 | python import socket | |
8 | python import os |
|
8 | python import os | |
9 | python import vim |
|
9 | python import vim | |
|
10 | python from IPython.core.debugger import Pdb | |||
10 | python IPYSERVER = None |
|
11 | python IPYSERVER = None | |
|
12 | python reselect = True | |||
11 |
|
13 | |||
12 | python << EOF |
|
14 | python << EOF | |
13 | # do we have a connection to the ipython instance? |
|
15 | # do we have a connection to the ipython instance? | |
14 | def check_server(): |
|
16 | def check_server(): | |
15 | global IPYSERVER |
|
17 | global IPYSERVER | |
16 | if IPYSERVER: |
|
18 | if IPYSERVER: | |
17 | return True |
|
19 | return True | |
18 | else: |
|
20 | else: | |
19 | return False |
|
21 | return False | |
20 |
|
22 | |||
21 | # connect to the ipython server, if we need to |
|
23 | # connect to the ipython server, if we need to | |
22 | def connect(): |
|
24 | def connect(): | |
23 | global IPYSERVER |
|
25 | global IPYSERVER | |
24 | if check_server(): |
|
26 | if check_server(): | |
25 | return |
|
27 | return | |
26 | try: |
|
28 | try: | |
27 | IPYSERVER = socket.socket(socket.AF_UNIX) |
|
29 | IPYSERVER = socket.socket(socket.AF_UNIX) | |
28 | IPYSERVER.connect(os.environ.get('IPY_SERVER')) |
|
30 | IPYSERVER.connect(os.environ.get('IPY_SERVER')) | |
29 | except: |
|
31 | except: | |
30 | IPYSERVER = None |
|
32 | IPYSERVER = None | |
31 |
|
33 | |||
32 | def disconnect(): |
|
34 | def disconnect(): | |
33 | if IPYSERVER: |
|
35 | if IPYSERVER: | |
34 | IPYSERVER.close() |
|
36 | IPYSERVER.close() | |
35 |
|
37 | |||
36 | def send(cmd): |
|
38 | def send(cmd): | |
37 | x = 0 |
|
39 | x = 0 | |
38 | while True: |
|
40 | while True: | |
39 | x += IPYSERVER.send(cmd) |
|
41 | x += IPYSERVER.send(cmd) | |
40 | if x < len(cmd): |
|
42 | if x < len(cmd): | |
41 | cmd = cmd[x:] |
|
43 | cmd = cmd[x:] | |
42 | else: |
|
44 | else: | |
43 | break |
|
45 | break | |
44 |
|
46 | |||
45 | def run_this_file(): |
|
47 | def run_this_file(): | |
46 | if check_server(): |
|
48 | if check_server(): | |
47 | send('run %s' % (vim.current.buffer.name,)) |
|
49 | send('run %s' % (vim.current.buffer.name,)) | |
48 | else: |
|
50 | else: | |
49 | raise Exception, "Not connected to an IPython server" |
|
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 | EOF |
|
116 | EOF | |
51 |
|
117 | |||
52 | fun! <SID>toggle_send_on_save() |
|
118 | fun! <SID>toggle_send_on_save() | |
53 | if exists("s:ssos") && s:ssos == 1 |
|
119 | if exists("s:ssos") && s:ssos == 1 | |
54 | let s:ssos = 0 |
|
120 | let s:ssos = 0 | |
55 | au! BufWritePost *.py :py run_this_file() |
|
121 | au! BufWritePost *.py :py run_this_file() | |
56 | echo "Autosend Off" |
|
122 | echo "Autosend Off" | |
57 | else |
|
123 | else | |
58 | let s:ssos = 1 |
|
124 | let s:ssos = 1 | |
59 | au BufWritePost *.py :py run_this_file() |
|
125 | au BufWritePost *.py :py run_this_file() | |
60 | echo "Autowsend On" |
|
126 | echo "Autowsend On" | |
61 | endif |
|
127 | endif | |
62 | endfun |
|
128 | endfun | |
63 |
|
129 | |||
64 | map <silent> <F5> :python run_this_file()<CR> |
|
130 | map <silent> <F5> :python run_this_file()<CR> | |
65 | imap <silent> <C-F5> <ESC><F5>a |
|
131 | map <silent> <S-F5> :python run_this_line()<CR> | |
66 | map <F7> :call <SID>toggle_send_on_save()<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 | py connect() |
|
143 | py connect() |
General Comments 0
You need to be logged in to leave comments.
Login now