-
Notifications
You must be signed in to change notification settings - Fork 162
/
Copy pathipy.vim
568 lines (523 loc) · 20.9 KB
/
ipy.vim
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
" Vim integration with IPython 0.11+
"
" A two-way integration between Vim and IPython.
"
" Using this plugin, you can send lines or whole files for IPython to execute,
" and also get back object introspection and word completions in Vim, like
" what you get with: object?<enter> object.<tab> in IPython
"
" -----------------
" Quickstart Guide:
" -----------------
" Start ipython qtconsole and copy the connection string.
" Source this file, which provides new IPython command
" :source ipy.vim
" :IPythonClipboard
" (or :IPythonXSelection if you're using X11 without having to copy)
"
" written by Paul Ivanov (http://pirsquared.org)
python << EOF
reselect = False # reselect lines after sending from Visual mode
show_execution_count = True # wait to get numbers for In[43]: feedback?
monitor_subchannel = True # update vim-ipython 'shell' on every send?
run_flags= "-i" # flags to for IPython's run magic when using <F5>
import vim
import sys
# get around unicode problems when interfacing with vim
vim_encoding=vim.eval('&encoding') or 'utf-8'
try:
sys.stdout.flush
except AttributeError:
# IPython complains if stderr and stdout don't have flush
# this is fixed in newer version of Vim
class WithFlush(object):
def __init__(self,noflush):
self.write=noflush.write
self.writelines=noflush.writelines
def flush(self):pass
sys.stdout = WithFlush(sys.stdout)
sys.stderr = WithFlush(sys.stderr)
ip = '127.0.0.1'
try:
km
except NameError:
km = None
def km_from_string(s=''):
"""create kernel manager from IPKernelApp string
such as '--shell=47378 --iopub=39859 --stdin=36778 --hb=52668' for IPython 0.11
or just 'kernel-12345.json' for IPython 0.12
"""
from os.path import join as pjoin
from IPython.zmq.blockingkernelmanager import BlockingKernelManager, Empty
from IPython.config.loader import KeyValueConfigLoader
from IPython.zmq.kernelapp import kernel_aliases
global km,send,Empty
s = s.replace('--existing', '')
if 'connection_file' in BlockingKernelManager.class_trait_names():
from IPython.lib.kernel import find_connection_file
# 0.12 uses files instead of a collection of ports
# include default IPython search path
# filefind also allows for absolute paths, in which case the search
# is ignored
try:
# XXX: the following approach will be brittle, depending on what
# connection strings will end up looking like in the future, and
# whether or not they are allowed to have spaces. I'll have to sync
# up with the IPython team to address these issues -pi
if '--profile' in s:
k,p = s.split('--profile')
k = k.lstrip().rstrip() # kernel part of the string
p = p.lstrip().rstrip() # profile part of the string
fullpath = find_connection_file(k,p)
else:
fullpath = find_connection_file(s.lstrip().rstrip())
except IOError,e:
echo(":IPython " + s + " failed", "Info")
echo("^-- failed '" + s + "' not found", "Error")
return
km = BlockingKernelManager(connection_file = fullpath)
km.load_connection_file()
else:
if s == '':
echo(":IPython 0.11 requires the full connection string")
return
loader = KeyValueConfigLoader(s.split(), aliases=kernel_aliases)
cfg = loader.load_config()['KernelApp']
try:
km = BlockingKernelManager(
shell_address=(ip, cfg['shell_port']),
sub_address=(ip, cfg['iopub_port']),
stdin_address=(ip, cfg['stdin_port']),
hb_address=(ip, cfg['hb_port']))
except KeyError,e:
echo(":IPython " +s + " failed", "Info")
echo("^-- failed --"+e.message.replace('_port','')+" not specified", "Error")
return
km.start_channels()
send = km.shell_channel.execute
return km
def echo(arg,style="Question"):
try:
vim.command("echohl %s" % style)
vim.command("echom \"%s\"" % arg.replace('\"','\\\"'))
vim.command("echohl None")
except vim.error:
print "-- %s" % arg
def disconnect():
"disconnect kernel manager"
# XXX: make a prompt here if this km owns the kernel
pass
def get_doc(word):
if km is None:
return ["Not connected to IPython, cannot query \"%s\"" %word]
msg_id = km.shell_channel.object_info(word)
doc = get_doc_msg(msg_id)
# get around unicode problems when interfacing with vim
return [d.encode(vim_encoding) for d in doc]
import re
# from http://serverfault.com/questions/71285/in-centos-4-4-how-can-i-strip-escape-sequences-from-a-text-file
strip = re.compile('\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]')
def strip_color_escapes(s):
return strip.sub('',s)
def get_doc_msg(msg_id):
n = 13 # longest field name (empirically)
b=[]
try:
content = get_child_msg(msg_id)['content']
except Empty:
# timeout occurred
return ["no reply from IPython kernel"]
if not content['found']:
return b
for field in ['type_name','base_class','string_form','namespace',
'file','length','definition','source','docstring']:
c = content.get(field,None)
if c:
if field in ['definition']:
c = strip_color_escapes(c).rstrip()
s = field.replace('_',' ').title()+':'
s = s.ljust(n)
if c.find('\n')==-1:
b.append(s+c)
else:
b.append(s)
b.extend(c.splitlines())
return b
def get_doc_buffer(level=0):
# empty string in case vim.eval return None
word = vim.eval('expand("<cfile>")') or ''
doc = get_doc(word)
if len(doc) ==0:
echo(word+" not found","Error")
return
# close any currently open preview windows
vim.command('pcl')
# documentation buffer name is same as the query made to ipython
vim.command('new '+word)
vim.command('setlocal pvw modifiable noro')
# doc window quick quit keys: 'q' and 'escape'
vim.command('map <buffer> q :q<CR>')
# Known issue: to enable the use of arrow keys inside the terminal when
# viewing the documentation, comment out the next line
vim.command('map <buffer> <Esc> :q<CR>')
# and uncomment this line (which will work if you have a timoutlen set)
#vim.command('map <buffer> <Esc><Esc> :q<CR>')
b = vim.current.buffer
b[:] = None
b[:] = doc
vim.command('setlocal nomodified bufhidden=wipe')
#vim.command('setlocal previewwindow nomodifiable nomodified ro')
#vim.command('set previewheight=%d'%len(b))# go to previous window
vim.command('resize %d'%len(b))
#vim.command('pcl')
#vim.command('pedit doc')
#vim.command('normal ') # go to previous window
# "rest" is the syntax of python documentation. Stock vim doesn't have
# a syntax file for rest, but if the user has installed one then this will
# cause the docs to be colorized.
vim.command('setlocal syntax=rest')
def update_subchannel_msgs(debug=False):
msgs = km.sub_channel.get_msgs()
if debug:
#try:
# vim.command("b debug_msgs")
#except vim.error:
# vim.command("new debug_msgs")
#finally:
db = vim.current.buffer
else:
db = []
b = vim.current.buffer
startedin_vimipython = vim.eval('@%')=='vim-ipython'
if not startedin_vimipython:
# switch to preview window
vim.command(
"try"
"|silent! wincmd P"
"|catch /^Vim\%((\a\+)\)\=:E441/"
"|silent pedit +set\ ma vim-ipython"
"|silent! wincmd P"
"|endtry")
# if the current window is called 'vim-ipython'
if vim.eval('@%')=='vim-ipython':
# set the preview window height to the current height
vim.command("set pvh=" + vim.eval('winheight(0)'))
else:
# close preview window, it was something other than 'vim-ipython'
vim.command("pcl")
vim.command("silent pedit +set\ ma vim-ipython")
vim.command("wincmd P") #switch to preview window
# subchannel window quick quit key 'q'
vim.command('map <buffer> q :q<CR>')
vim.command("set bufhidden=hide buftype=nofile ft=python")
#syntax highlighting for python prompt
# QtConsole In[] is blue, but I prefer the oldschool green
# since it makes the vim-ipython 'shell' look like the holidays!
#vim.command("hi Blue ctermfg=Blue guifg=Blue")
vim.command("hi Green ctermfg=Green guifg=Green")
vim.command("hi Red ctermfg=Red guifg=Red")
vim.command("syn keyword Green 'In\ []:'")
vim.command("syn match Green /^In \[[0-9]*\]\:/")
vim.command("syn match Red /^Out\[[0-9]*\]\:/")
b = vim.current.buffer
for m in msgs:
#db.append(str(m).splitlines())
s = ''
if 'msg_type' not in m['header']:
# debug information
#echo('skipping a message on sub_channel','WarningMsg')
#echo(str(m))
continue
elif m['header']['msg_type'] == 'status':
continue
elif m['header']['msg_type'] == 'stream':
s = strip_color_escapes(m['content']['data'])
elif m['header']['msg_type'] == 'pyout':
s = "Out[%d]: " % m['content']['execution_count']
s += m['content']['data']['text/plain']
elif m['header']['msg_type'] == 'pyin':
# TODO: the next line allows us to resend a line to ipython if
# %doctest_mode is on. In the future, IPython will send the
# execution_count on subchannel, so this will need to be updated
# once that happens
if 'execution_count' in m['content']:
s = "\nIn [%d]: "% m['content']['execution_count']
else:
s = "\nIn [00]: "
s += m['content']['code'].strip()
elif m['header']['msg_type'] == 'pyerr':
c = m['content']
s = "\n".join(map(strip_color_escapes,c['traceback']))
s += c['ename'] + ":" + c['evalue']
if s.find('\n') == -1:
# somewhat ugly unicode workaround from
# http://vim.1045645.n5.nabble.com/Limitations-of-vim-python-interface-with-respect-to-character-encodings-td1223881.html
if isinstance(s,unicode):
s=s.encode(vim_encoding)
b.append(s)
else:
try:
b.append(s.splitlines())
except:
b.append([l.encode(vim_encoding) for l in s.splitlines()])
# make a newline so we can just start typing there
if b[-1] != '':
b.append([''])
vim.command('normal G') # go to the end of the file
if not startedin_vimipython:
vim.command('normal p') # go back to where you were
def get_child_msg(msg_id):
# XXX: message handling should be split into its own process in the future
while True:
# get_msg will raise with Empty exception if no messages arrive in 1 second
m= km.shell_channel.get_msg(timeout=1)
if m['parent_header']['msg_id'] == msg_id:
break
else:
#got a message, but not the one we were looking for
echo('skipping a message on shell_channel','WarningMsg')
return m
def print_prompt(prompt,msg_id=None):
"""Print In[] or In[42] style messages"""
global show_execution_count
if show_execution_count and msg_id:
# wait to get message back from kernel
try:
child = get_child_msg(msg_id)
count = child['content']['execution_count']
echo("In[%d]: %s" %(count,prompt))
except Empty:
echo("In[]: %s (no reply from IPython kernel)" % prompt)
else:
echo("In[]: %s" % prompt)
def with_subchannel(f,*args):
"conditionally monitor subchannel"
def f_with_update(*args):
try:
f(*args)
if monitor_subchannel:
update_subchannel_msgs()
except AttributeError: #if km is None
echo("not connected to IPython", 'Error')
return f_with_update
@with_subchannel
def run_this_file():
msg_id = send('run %s %s' % (run_flags, repr(vim.current.buffer.name),))
print_prompt("In[]: run %s %s" % (run_flags, repr(vim.current.buffer.name)),msg_id)
@with_subchannel
def run_this_line():
msg_id = send(vim.current.line)
print_prompt(vim.current.line, msg_id)
@with_subchannel
def run_command(cmd):
msg_id = send(cmd)
print_prompt(cmd, msg_id)
if monitor_subchannel:
update_subchannel_msgs()
@with_subchannel
def run_these_lines():
r = vim.current.range
lines = "\n".join(vim.current.buffer[r.start:r.end+1])
msg_id = send(lines)
#alternative way of doing this in more recent versions of ipython
#but %paste only works on the local machine
#vim.command("\"*yy")
#send("'%paste')")
#reselect the previously highlighted block
vim.command("normal gv")
if not reselect:
vim.command("normal ")
#vim lines start with 1
#print "lines %d-%d sent to ipython"% (r.start+1,r.end+1)
prompt = "lines %d-%d "% (r.start+1,r.end+1)
print_prompt(prompt,msg_id)
def dedent_run_this_line():
vim.command("left")
run_this_line()
vim.command("silent undo")
def dedent_run_these_lines():
r = vim.current.range
shiftwidth = vim.eval('&shiftwidth')
count = int(vim.eval('indent(%d+1)/%s' % (r.start,shiftwidth)))
vim.command("'<,'>" + "<"*count)
run_these_lines()
vim.command("silent undo")
#def set_this_line():
# # not sure if there's a way to do this, since we have multiple clients
# send("get_ipython().shell.set_next_input(\'%s\')" % vim.current.line.replace("\'","\\\'"))
# #print "line \'%s\' set at ipython prompt"% vim.current.line
# echo("line \'%s\' set at ipython prompt"% vim.current.line,'Statement')
def toggle_reselect():
global reselect
reselect=not reselect
print "F9 will%sreselect lines after sending to ipython"% (reselect and " " or " not ")
#def set_breakpoint():
# send("__IP.InteractiveTB.pdb.set_break('%s',%d)" % (vim.current.buffer.name,
# vim.current.window.cursor[0]))
# print "set breakpoint in %s:%d"% (vim.current.buffer.name,
# vim.current.window.cursor[0])
#
#def clear_breakpoint():
# send("__IP.InteractiveTB.pdb.clear_break('%s',%d)" % (vim.current.buffer.name,
# vim.current.window.cursor[0]))
# print "clearing breakpoint in %s:%d" % (vim.current.buffer.name,
# vim.current.window.cursor[0])
#
#def clear_all_breakpoints():
# send("__IP.InteractiveTB.pdb.clear_all_breaks()");
# print "clearing all breakpoints"
#
#def run_this_file_pdb():
# send(' __IP.InteractiveTB.pdb.run(\'execfile("%s")\')' % (vim.current.buffer.name,))
# #send('run -d %s' % (vim.current.buffer.name,))
# echo("In[]: run -d %s (using pdb)" % vim.current.buffer.name)
EOF
fun! <SID>toggle_send_on_save()
if exists("s:ssos") && s:ssos == 0
let s:ssos = 1
au BufWritePost *.py :py run_this_file()
echo "Autosend On"
else
let s:ssos = 0
au! BufWritePost *.py
echo "Autosend Off"
endif
endfun
" Allow custom mappings
if !exists('g:ipy_perform_mappings')
let g:ipy_perform_mappings = 1
endif
if g:ipy_perform_mappings != 0
map <silent> <F5> <Plug>IPyRunFile
map <silent> <S-F5> <Plug>IPyRunLine
map <silent> <F9> :python run_these_lines()<CR>
map <silent> <leader>d <Plug>IPyDocWord
map <silent> <leader>s :py update_subchannel_msgs(); echo("vim-ipython shell updated",'Operator')<CR>
map <silent> <S-F9> :python toggle_reselect()<CR>
"map <silent> <C-F6> :python send('%pdb')<CR>
"map <silent> <F6> :python set_breakpoint()<CR>
"map <silent> <s-F6> :python clear_breakpoint()<CR>
"map <silent> <F7> :python run_this_file_pdb()<CR>
"map <silent> <s-F7> :python clear_all_breaks()<CR>
imap <C-F5> <C-O><F5>
imap <S-F5> <C-O><S-F5>
imap <silent> <F5> <C-O><F5>
map <C-F5> :call <SID>toggle_send_on_save()<CR>
"" Example of how to quickly clear the current plot with a keystroke
"map <silent> <F12> :python run_command("plt.clf()")<cr>
"" Example of how to quickly close all figures with a keystroke
"map <silent> <F11> :python run_command("plt.close('all')")<cr>
" <leader>p runs command from a visual selection or a motion.
" For example, <leader>piw to execute a word.
vmap <silent> <leader>p <Plug>IPyRunSel
nmap <silent> <leader>p <Plug>IPyRunSel
" <leader>pp runs one line (analogous to e.g. "dd")
nmap <silent> <leader>pp <Plug>IPyRunLine
"pi custom
map <silent> <C-Return> <Plug>IpyRunFile
map <silent> <C-s> <Plug>IPyRunLine
imap <silent> <C-s> <Plug>IPyRunLine
map <silent> <M-s> :python dedent_run_this_line()<CR>
vmap <silent> <C-S> :python run_these_lines()<CR>
vmap <silent> <M-s> :python dedent_run_these_lines()<CR>
map <silent> <M-c> I#<ESC>
vmap <silent> <M-c> I#<ESC>
map <silent> <M-C> :s/^\([ \t]*\)#/\1/<CR>
vmap <silent> <M-C> :s/^\([ \t]*\)#/\1/<CR>
endif
nnoremap <Plug>IPyRunFile :py run_this_file()<CR>
noremap <Plug>IPyRunLine :python run_this_line()<CR>
inoremap <Plug>IPyRunLine <C-\><C-O>:python run_this_line()<CR>
" See: http://learnvimscriptthehardway.stevelosh.com/chapters/34.html
" And: :help map
vnoremap <Plug>IPyRunSel :<C-U>call <SID>IPythonRunOp(visualmode(), 1)<cr>
nnoremap <Plug>IPyRunSel :set operatorfunc=<SID>IPythonRunOp<cr>g@
nnoremap <Plug>IPyDocWord :py get_doc_buffer()<CR>
command! -nargs=* IPython :py km_from_string("<args>")
command! -nargs=0 IPythonClipboard :py km_from_string(vim.eval('@+'))
command! -nargs=0 IPythonXSelection :py km_from_string(vim.eval('@*'))
function! IPythonBalloonExpr()
python << endpython
word = vim.eval('v:beval_text')
reply = get_doc(word)
vim.command("let l:doc = %s"% reply)
endpython
return l:doc
endfunction
if has('balloon_eval')
set bexpr=IPythonBalloonExpr()
set ballooneval
endif
fun! CompleteIPython(findstart, base)
if a:findstart
" locate the start of the word
let line = getline('.')
let start = col('.') - 1
while start > 0 && line[start-1] =~ '\k\|\.' "keyword
let start -= 1
endwhile
echo start
return start
else
" find months matching with "a:base"
let res = []
python << endpython
base = vim.eval("a:base")
findstart = vim.eval("a:findstart")
msg_id = km.shell_channel.complete(base, vim.current.line, vim.eval("col('.')"))
try:
m = get_child_msg(msg_id)
matches = m['content']['matches']
matches.insert(0,base) # the "no completion" version
# we need to be careful with unicode, because we can have unicode
# completions for filenames (for the %run magic, for example). So the next
# line will fail on those:
#completions= [str(u) for u in matches]
# because str() won't work for non-ascii characters
# and we also have problems with unicode in vim, hence the following:
completions = [s.encode(vim_encoding) for s in matches]
except Empty:
echo("no reply from IPython kernel")
completions=['']
## Additionally, we have no good way of communicating lists to vim, so we have
## to turn in into one long string, which can be problematic if e.g. the
## completions contain quotes. The next line will not work if some filenames
## contain quotes - but if that's the case, the user's just asking for
## it, right?
#completions = '["'+ '", "'.join(completions)+'"]'
#vim.command("let completions = %s" % completions)
## An alternative for the above, which will insert matches one at a time, so
## if there's a problem with turning a match into a string, it'll just not
## include the problematic match, instead of not including anything. There's a
## bit more indirection here, but I think it's worth it
for c in completions:
vim.command('call add(res,"'+c+'")')
endpython
"call extend(res,completions)
return res
endif
endfun
set completefunc=CompleteIPython
" Only create function if it doesn't already exist. It isn't sufficient to
" just use "function!" since the function may already be referenced by
" operatorfunc, and vim doesn't allow redefining the function in that case.
if !exists('*s:IPythonRunOp')
function s:IPythonRunOp(type, ...)
let saved_unnamed_register = @@
if a:0 " Invoked from Visual mode, use '< and '> marks.
exe "silent normal! `<" . a:type . "`>y"
elseif a:type ==# 'char'
silent normal! `[v`]y
elseif a:type == 'line'
silent normal! '[V']y
elseif a:type == 'block'
silent normal! `[\<C-V>`]y
else
echo "error: type=".a:type." a:0=".a:0
return
endif
"echo "0: ".a:0." t: ".a:type." @: ".@@
python run_command(vim.eval('@@'))
let @@ = saved_unnamed_register
endfunction
endif