-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.vim
1001 lines (880 loc) · 33.4 KB
/
main.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
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
" This file is sourced when the user tries starting vim using the shortcuts
" defined in plugin/vipy.vim; the files are split like this to reduce startup
" time for sessions where vipy isn't used.
let g:ipy_status="idle"
" let the user specify the IPython profile they want to use
if !exists('g:vipy_profile')
let g:vipy_profile='default'
endif
if !exists('g:vipy_position')
let g:vipy_position='rightbelow'
endif
if !exists('g:vipy_height')
let g:vipy_height=20
endif
function! VipySyntax()
syn region VipyIn start=/\v(^\>{3})\zs/ end=/\v\ze^.{0,2}$|\ze^\>{3}|\ze^[^.>]..|\ze^.[^.>].|\ze^..[^.>]/ contains=ALL transparent keepend
syn region VipyOut start=/\v\zs^.{0,2}$|\zs^[^.>]..|\zs^.[^.>].|\zs^..[^.>]/ end=/\v\ze^\>{3}/
hi link VipyOut Normal
endfunction
noremap <silent> <S-F12> :py vipy_shutdown()<CR><ESC>
inoremap <silent> <S-F12> <ESC>:py vipy_shutdown()<CR>
python << EOF
import subprocess, sys, re, os
from os import path, kill
from string import replace
import vim
## Helper functions
def vprint(msg):
if type(msg) == str:
print(msg)
if type(msg) == list:
for line in msg:
print(line)
def vcommand(cmds):
if type(cmds) == str:
vim.command(cmds)
if type(cmds) == list:
for cmd in cmds:
vim.command(cmd)
## Make sure you have appropriate dependencies
try:
import IPython
except ImportError:
msg = "You must have IPython 0.13 or newer installed to use ViPy."
vprint(msg)
raise Exception(msg)
version = IPython.__version__.split('.')
major = int(version[0])
minor = int(version[1])
using_windows = os.name == 'nt'
if major == 0 and minor < 13:
vprint([
"It appears you have IPython {}.{} installed.".format(major, minor),
"You must have IPython 0.13 or newer installed to use ViPy.",
])
raise Exception("You must have IPython 0.13 or newer installed.")
try:
from IPython.zmq.blockingkernelmanager import BlockingKernelManager, Empty
from IPython.lib.kernel import find_connection_file
except ImportError:
msg = ["You must have pyzmq >= 2.1.4 installed to use ViPy."]
if using_windows:
msg.extend([
"There is a known issue with pyzmq on 64 bit windows machines.",
"See the documentation for fixing this."
])
vprint(msg)
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)
class Vipy(object):
def __init__(self):
self.debugging = False
self.in_debugger = False
self.monitor_subchannel = True # update vipy 'shell' on every send?
self.run_flags= "-i" # flags to for IPython's run magic when using <F5>
self.current_line = ''
debugging = False
in_debugger = False
monitor_subchannel = True # update vipy 'shell' on every send?
run_flags= "-i" # flags to for IPython's run magic when using <F5>
current_line = ''
try:
status
except:
status = 'idle'
try:
length_of_last_input_request
except:
length_of_last_input_request = 0
try:
vib
except:
vib = False
try:
vihb
except:
vihb = False
try:
km
except NameError:
km = None
try:
km_started_by_vim
except:
km_started_by_vim = False
# get around unicode problems when interfacing with vim
vim_encoding = vim.eval('&encoding') or 'utf-8'
## STARTUP and SHUTDOWN
ipython_process = None
def vipy_startup():
global km, fullpath, km_started_by_vim, profile_dir
if not km:
vim.command("augroup vimipython")
vim.command("au CursorHold * :python update_subchannel_msgs()")
vim.command("au FocusGained *.py :python update_subchannel_msgs()")
vim.command("au filetype python setlocal completefunc=CompleteIPython")
# run shutdown sequense
vim.command("au VimLeavePre :python vipy_shutdown()")
vim.command("augroup END")
count = 0
profile = vim.eval('g:vipy_profile')
profile_dir = vim.eval('system("ipython locate profile ' + profile + '")').strip()
if not path.exists(profile_dir):
echo("It doesn't appear that the IPython profile, %s, specified using the g:vipy_profile variable exists. Creating the profile ..." % profile)
external_in_bg('ipython profile create ' + profile)
profile_dir = vim.eval('system("ipython locate profile ' + profile + '")').strip()
fullpath = None
ipy_args = []
try:
# see if there is already an IPython instance open ...
while (not fullpath):
fullpath = find_connection_file('', profile=profile)
pid=path.basename(fullpath).replace('kernel-','').replace('.json','')
try:
kill(int(pid),0)
except:
# remove old connection files
os.remove(fullpath)
fullpath=None
km_started_by_vim = False
except: # ... if not start one
ipy_args.append('--profile={}'.format(profile))
options = {}
if using_windows:
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
options['startupinfo'] = startupinfo
ipython_process = subprocess.Popen(['ipython', 'kernel'] + ipy_args, **options)
# try to find connection file (sometimes you need to wait a bit)
fullpath = None
while not fullpath:
try:
fullpath = find_connection_file('', profile=profile)
except:
pass
km_started_by_vim = True
if fullpath:
km = BlockingKernelManager(connection_file = fullpath)
km.load_connection_file()
km.start_channels()
else:
echo("Couldn't connect to vim-ipython.")
return
vib = get_vim_ipython_buffer()
if not vib:
setup_vib()
else:
echo('It appears that there is already a file named vipy.py open! This will cause errors unless it is generated by the vipy plugin (i.e. if it is a regular file named vipy.py).')
goto_vib()
# Update the vipy shell when the cursor is not moving
# the cursor hold is updated 3 times a second (maximum), but it doesn't
# update if you stop moving
vim.command("set updatetime=333")
else:
echo('Vipy has already been started! Press SHIFT-F12 to close the current seeion.')
def vipy_shutdown():
global km, in_debugger, vib, vihb, km_started_by_vim
status = 'idle'
# shutdown the kernel if we started it
if km_started_by_vim:
if km != None:
try:
km.shell_channel.shutdown()
km.cleanup_connection_file()
except:
echo('The kernel must have already shut down.')
else:
echo('The kernel must have already shut down.')
km = None
# wipe the buffer
try:
if vib:
if len(vim.windows) == 1:
vim.command('bprevious')
vim.command('bw ' + vib.name)
vib = None
except:
echo('The vipy buffer must have already been closed.')
try:
if vihb:
if len(vim.windows) == 1:
vim.command('bprevious')
vim.command('bw ' + vihb.name)
vihb = None
except:
vihb = None
try:
vim.command("au! vimipython")
except:
pass
def setup_vib():
""" Setup vib (vipy buffer), that acts like a prompt. """
global vib
vipy_pos = vim.eval('g:vipy_position')
vim.command(vipy_pos + " new vipy.py")
vipy_height = str(vim.eval('g:vipy_height'))
vim.command("resize " + vipy_height)
# set the global variable for everyone to reference easily
vib = get_vim_ipython_buffer()
if not vib:
echo('It appears that your value for g:vipy_position is invalid! See :help vipy')
new_prompt(append=False)
vim.command("setlocal nonumber")
vim.command("setlocal bufhidden=hide buftype=nofile ft=python noswf")
# turn of auto indent (there is some custom indenting that accounts
# for the prompt). See vim-tip 330
vim.command("setl noai nocin nosi inde=")
vim.command("syn match Normal /^>>>/")
# mappings to control sending stuff from vipy
vim.command('inoremap <expr> <buffer> <silent> <s-cr> pumvisible() ? "\<ESC>:py print_completions()\<CR>" : "\<ESC>:py shift_enter_at_prompt()\<CR>"')
vim.command('nnoremap <buffer> <silent> <cr> <ESC>:py enter_at_prompt()<CR>')
vim.command('inoremap <buffer> <silent> <cr> <ESC>:py enter_at_prompt()<CR>')
# setup history mappings etc.
enter_normal(first=True)
# add and auto command, so that the cursor always moves to the end
# upon entereing the vipy buffer
vim.command("au WinEnter <buffer> :python insert_at_new()")
# not working; the idea was to make
# vim.command("au InsertEnter <buffer> :py if above_prompt(): vim.command('normal G$')")
vim.command("setlocal statusline=\ VIPY:\ %-{g:ipy_status}")
# handle syntax coloring a little better
vim.command('call VipySyntax()') # avoid problems with \v being escaped in the regexps
def enter_normal(first=False):
global vib_map, in_debugger
in_debugger = False
vib_map = "on"
in_debugger = False
# mappings to control history
vim.command("inoremap <buffer> <silent> <up> <ESC>:py prompt_history('up')<CR>")
vim.command("inoremap <buffer> <silent> <down> <ESC>:py prompt_history('down')<CR>")
# make some normal vim commands convenient when in the vib
vim.command("nnoremap <buffer> <silent> dd cc>>> ")
vim.command("noremap <buffer> <silent> <home> 0llll")
vim.command("inoremap <buffer> <silent> <home> <ESC>0llla")
vim.command("noremap <buffer> <silent> 0 0llll")
vim.command("noremap <buffer> <silent> <c-l> zt")
vim.command("noremap <buffer> <silent> I 0llla")
def enter_debug():
""" Remove all the convenience mappings. """
global vib_map, in_debugger
vib_map = "off"
in_debugger = True
def if_vipy_started(func):
def wrapper(*args, **kwargs):
if km:
func(*args, **kwargs)
else:
echo("You must start VIPY first, using <CTRL-F5>")
return wrapper
## COMMAND-LINE-HISTORY
need_new_hist = True
last_hist = []
hist_pos = 0
num_lines_added_last = 1
hist_prompt = '>>> '
hist_last_appended = ''
def prompt_history(key):
""" Poll server for history if a new search is needed, otherwise rotate
through matches. """
global last_hist, hist_pos, need_new_hist, num_lines_added_last, hist_prompt, hist_last_appended
if not at_end_of_prompt():
r,c = vim.current.window.cursor
if key == "up":
if not r == 1:
vim.current.window.cursor = (r - 1, c)
elif key == "down":
if not r == len(vim.current.buffer):
vim.current.window.cursor = (r + 1, c)
return
if status == "busy":
# echo("No history available because the python server is busy.")
# the message gets overridden immedieatly when the mapping switches back to
# insert mode
return
if not vib[-1] == hist_last_appended:
need_new_hist = True
if need_new_hist:
cl = vim.current.line
if len(cl) > 4: # search for everything starting with the current line
pat = cl[4:] + '*'
msg_id = km.shell_channel.history(hist_access_type='search', pattern=pat)
else: # return the last 100 inputs
pat = ' '
msg_id = km.shell_channel.history(hist_access_type='tail', n=50)
hist_prompt = cl[:4] if len(cl) >= 4 else '>>> '
hist = get_child_msg(msg_id)['content']['history']
# sort the history by time
last_hist = sorted(hist, key=hist_sort, reverse=True)
last_hist = [hi[2].encode(vim_encoding) for hi in last_hist] + [pat[:-1]]
need_new_hist = False
hist_pos = 0
num_lines_added_last = 1
else:
if key == "up":
hist_pos = (hist_pos + 1) % len(last_hist)
else: # if key == "down"
hist_pos = (hist_pos - 1) % len(last_hist)
# remove the previously added lines
if num_lines_added_last > 1:
del vib[-(num_lines_added_last - 1):]
toadd = format_for_prompt(last_hist[hist_pos], firstline=hist_prompt)
num_lines_added_last = len(toadd)
vib[-1] = toadd[0]
for line in toadd[1:]:
vib.append(line)
hist_last_appended = toadd[-1]
vim.command('normal G$')
vim.command('startinsert!')
def hist_sort(hist_item):
""" Sort history items such that the most recent sessions has highest
priority.
hist_item is a tuple with: (session, line_number, input)
where session and line_number increase through time. """
return hist_item[0]*10000 + hist_item[1]
## COMMAND LINE
numspace = re.compile(r'^[>.]{3}(\s*)')
def shift_enter_at_prompt():
if at_end_of_prompt():
match = numspace.match(vib[-1])
if match:
space_on_lastline = match.group(1)
else:
space_on_lastline = ''
vib.append('...' + space_on_lastline)
vim.command('normal G')
vim.command('startinsert!')
else:
vim.command('call feedkeys("\<CR>", "n")')
from math import ceil
def print_completions(invipy=True):
""" Print the current completions into the vib buffer.
This helps when there are a lot of completions, or you want
to use them as a reference. """
# grab current input
if len(completions) > 2:
# TODO: make this work on multi-line input
# Save the original input
del completions[0]
input_length = 1
input = vib[-input_length:]
# format the text from the list of completions
vib_width = vim.current.window.width
max_comp_len = max([len(c) for c in completions]) + 1 # 1 is for the space
num_col = int(vib_width/max_comp_len)
if num_col == 0:
num_col = 1
comp_per_col = int(ceil(len(completions)/float(num_col)))
# a list of lists of strings on each line
formatted = [] # the formatted lines
on_line = [completions[i::comp_per_col] for i in xrange(comp_per_col)]
for line in on_line:
tmp = ''
for comp in line:
tmp += comp.ljust(max_comp_len)
formatted.append(tmp)
# append the completions
if len(vib) == 1:
vib[0] = formatted[0]
else:
vib[-1] = formatted[0]
if len(formatted) > 1:
vib.append(formatted[1:])
# then append the old input and scroll to the bottom
vib.append(input)
goto_vib()
if not invipy:
toggle_vib()
def enter_at_prompt():
""" Remove prompts and whitespace before sending to ipython. """
if status == 'input requested':
km.stdin_channel.input(vib[-1][length_of_last_input_request:])
else:
stop_str = r'>>>'
cmds = []
linen = len(vib)
while linen > 0:
# remove the last three characters
cmd = vib[linen - 1]
# only add the line if it isn't empty
if len(cmd) > 4:
cmds.append(cmd[4:])
if cmd.startswith(stop_str):
break
else:
linen -= 1
if len(cmds) == 0:
return
cmds.reverse()
cmds = '\n'.join(cmds)
if cmds == 'cls' or cmds == 'clear':
vim.command('normal zt')
new_prompt(append=False)
elif cmds.startswith('edit ') or cmds.startswith('vedit ') or cmds.startswith('sedit '):
fnames = cmds[5:].split(' ')
for fname in fnames:
try:
pwd = get_ipy_pwd()
pp = path.join(pwd, fname)
if cmds[0] == 'e':
vim.command('edit ' + pp)
elif cmds[0] == 'v':
vim.command('vsp ' + pp)
else: # ... if cmds[0] == 's':
vim.command('sp ' + pp)
except:
vib.append("Couldn't find " + pp)
elif cmds.strip() == 'cdv':
try:
pwd = get_ipy_pwd()
vim.command('cd ' + pwd)
except:
vib.append("Couldn't change vim cwd to %s" % cmds.strip())
elif cmds.endswith('??'):
obj = cmds[:-2]
msg_id = km.shell_channel.object_info(obj)
try:
content = get_child_msg(msg_id)['content']
except Empty:
# timeout occurred
return echo("no reply from IPython kernel")
deffind = False
if content['found']:
if content['file']:
vim.command("drop " + content['file'])
vim.command("set syntax=python")
# try to position the cursor in the source file
# if content['source']:
# firstNL = content['source'].find('\n')
# if firstNL != -1:
# firstLine = content['source'][:firstNL]
# else:
# firstLine = content['source']
# cursorPositioner = re.compile(firstLine)
# if content['definition']:
# cursorPositioner = re.compile(content['definition'])
if content['type_name'] == 'function':
deffind = re.compile('def ' + obj.split('.')[-1] + '[ (]')
elif content['type_name'] == 'classobj':
deffind = re.compile('class ' + obj.split('.')[-1] + '[ (]')
else:
deffind = re.compile(obj.split('.')[-1] + '[ (]')
if deffind:
for ind, line in enumerate(vim.current.buffer):
if deffind.match(line):
vib.append('match found at %d' % ind)
break
content = None
else:
content = "IPython could not find a source file associated with %s." % obj
else:
content = "IPython could not find no object information associated with %s. \
Make sure that the requested object is in the interactive namespace and \
try again." % obj
if content:
vib.append(content)
new_prompt()
# this is ugly to put the cursor movement here: TODO: find a better way
if deffind and deffind.match(line):
vim.current.window.cursor = (ind + 1, 0)
else:
vim.current.window.cursor = (1, 0)
elif cmds.endswith('?'):
content = get_doc(cmds[:-1])
if content == '':
content = 'No matches found for: %s' % cmds[:-1]
vib.append(content)
new_prompt()
return
else:
send(cmds)
# make vim poll for a while
ping_count = 0
while ping_count < 30 and not update_subchannel_msgs():
vim.command("sleep 20m")
ping_count += 1
def new_prompt(goto=True, append=True):
if append:
vib.append('>>> ')
else:
vib[-1] = '>>> '
if goto:
vim.command('normal G')
vim.command('startinsert!')
def format_for_prompt(cmds, firstline='>>> ', limit=False):
# format and input text
max_lines = 10
lines_to_show_when_over = 4
if debugging:
vib.append('this is what is being formated for the prompt:')
vib.append(cmds)
if not cmds == '':
formatted = re.sub(r'\n',r'\n... ',cmds).splitlines()
lines = len(formatted)
if limit and lines > max_lines:
formatted = formatted[:lines_to_show_when_over] + ['... (%d more lines)' % (lines - lines_to_show_when_over)]
formatted[0] = firstline + formatted[0]
return formatted
else:
return [firstline]
## IPYTHON-VIM COMMUNICATION
blankprompt = re.compile(r'^\>\>\> $')
def send(cmds, *args, **kargs):
""" Send commands to ipython kernel.
Format the input, then print the statements to the vipy buffer.
"""
formatted = None
if status == 'input requested':
echo('Can not send further commands until you respond to the input request.')
return
elif status == 'busy':
echo('Can not send commands while the python kernel is busy.')
return
if not in_vipy():
formatted = format_for_prompt(cmds, limit=True)
# remove any prompts or blank lines
while len(vib) > 1 and blankprompt.match(vib[-1]):
del vib[-1]
if blankprompt.match(vib[-1]):
vib[-1] = formatted[0]
if len(formatted) > 1:
vib.append(formatted[1:])
else:
vib.append(formatted)
val = km.shell_channel.execute(cmds, *args, **kargs)
return val
def update_subchannel_msgs(debug=False):
""" This function grabs messages from ipython and acts accordinly; note
that communications are asynchronous, and furthermore there is no good way to
repeatedly trigger a function in vim. There is an autofunction that will
trigger whenever the cursor moves, which is the next best thing.
"""
global status, length_of_last_input_request
if km is None:
return False
newprompt = False
gotoend = False # this is a hack for moving to the end of the prompt when new input is requested that should get cleaned up
msgs = km.sub_channel.get_msgs()
msgs += km.stdin_channel.get_msgs() # also handle messages from stdin
for m in msgs:
if debugging:
vib.append('message from ipython:')
vib.append(repr(m).splitlines())
if 'msg_type' not in m['header']:
continue
else:
msg_type = m['header']['msg_type']
s = None
if msg_type == 'status':
if m['content']['execution_state'] == 'idle':
status = 'idle'
newprompt = True
else:
newprompt = False
if m['content']['execution_state'] == 'busy':
status = 'busy'
vim.command('let g:ipy_status="' + status + '"')
elif msg_type == 'stream':
s = strip_color_escapes(m['content']['data'])
elif msg_type == 'pyout':
s = m['content']['data']['text/plain']
elif msg_type == 'pyin':
# don't want to print the input twice
continue
# TODO: add better error formatting
elif msg_type == 'pyerr':
c = m['content']
s = "\n".join(map(strip_color_escapes, c['traceback']))
elif msg_type == 'object_info_reply':
c = m['content']
if not c['found']:
s = c['name'] + " not found!"
else:
# TODO: finish implementing this
s = c['docstring']
elif msg_type == 'input_request':
s = m['content']['prompt']
status = 'input requested'
vim.command('let g:ipy_status="' + status + '"')
length_of_last_input_request = len(m['content']['prompt'])
gotoend = True
elif msg_type == 'crash':
s = "The IPython Kernel Crashed!"
s += "\nUnfortuneatly this means that all variables in the interactive namespace were lost."
s += "\nHere is the crash info from IPython:\n"
s += repr(m['content']['info'])
s += "Type CTRL-F12 to restart the Kernel"
if s: # then update the vipy buffer with the formatted text
if s.find('\n') == -1: # then use 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)
vib.append(s)
if debugging:
vib.append('using unicode workaround')
else:
try:
vib.append(s.splitlines())
except:
vib.append([l.encode(vim_encoding) for l in s.splitlines()])
# move to the vipy (so that the autocommand can scroll down)
if in_vipy():
if newprompt:
new_prompt()
if gotoend:
goto_vib()
# turn off some mappings when input is requested (e.g. the history search)
if status == "input requested" and vib_map == "on":
enter_debug()
if vib_map == "off" and status != "input requested":
enter_normal()
else:
if newprompt:
new_prompt(goto=False)
if is_vim_ipython_open():
goto_vib(insert_at_end=False)
# scroll to the bottom of the screen if there is new input
if msgs:
vim.command('exe "normal G\<C-w>p"')
else:
vim.command('exe "normal \<C-w>p"')
return len(msgs)
def get_child_msg(msg_id):
while True:
# get_msg will raise with Empty exception if no messages arrive in 5 second
m= km.shell_channel.get_msg(timeout=5)
if m['parent_header']['msg_id'] == msg_id:
break
else:
#got a message, but not the one we were looking for
if debugging:
echo('skipping a message on shell_channel','WarningMsg')
return m
def with_subchannel(f, *args, **kwargs):
"conditionally monitor subchannel"
def f_with_update(*args, **kwargs):
try:
f(*args, **kwargs)
if monitor_subchannel:
update_subchannel_msgs()
except AttributeError: #if km is None
echo("not connected to IPython", 'Error')
return f_with_update
@if_vipy_started
@with_subchannel
def run_this_file():
fname = repr(vim.current.buffer.name) # use repr to avoid escapes
fname = fname.rstrip('ru') # remove r or u if it is raw or unicode
fname = fname[1:-1] # remove the quotations
fname = fname.replace('\\\\','\\')
msg_id = send("run %s %s" % (run_flags, fname))
@if_vipy_started
@with_subchannel
def run_this_line():
# don't send blank lines
if vim.current.line != '':
msg_id = send(vim.current.line.strip())
ws = re.compile(r'\s*')
@if_vipy_started
@with_subchannel
def run_these_lines():
vim.command('normal y')
lines = vim.eval("getreg('0')").splitlines()
ws_length = len(ws.match(lines[0]).group())
lines = [line[ws_length:] for line in lines]
msg_id = send("\n".join(lines))
# TODO: add support for nested cells
# TODO: fix glitch where the cursor moves incorrectly as a result of cell mode
# TODO: suppress the text output when in cell mode
cell_line = re.compile(r'^\s*##[^#]?')
@if_vipy_started
@with_subchannel
def run_cell(progress=False):
""" run the code between the previous ## and next ## """
row, col = vim.current.window.cursor
cb = vim.current.buffer
nrows = len(cb)
# find previous ## or start of file
crow = row - 1
cell_start = 0
while crow > 0:
if cell_line.search(cb[crow]):
cell_start = crow
break
else:
crow = crow - 1
# find next ## or end of file
crow = row
cell_end = nrows
while crow < nrows:
if cell_line.search(cb[crow]):
cell_end = crow
break
else:
crow = crow + 1
lines = cb[cell_start:cell_end]
ws_length = len(ws.match(lines[0]).group())
lines = [line[ws_length:] for line in lines]
msg_id = send("\n".join(lines))
if progress: # move cursor to next cell
if cell_end >= nrows - 1:
cell_end = nrows - 1
vim.current.window.cursor = (cell_end + 1, 0)
## HELP BUFFER
try:
vihb
except:
vihb = None
def get_doc(word):
msg_id = km.shell_channel.object_info(word)
doc = get_doc_msg(msg_id)
if len(doc) == 0:
return ''
else:
# get around unicode problems when interfacing with vim
return [d.encode(vim_encoding) for d in doc]
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 print_help():
word = vim.eval('expand("<cfile>")') or ''
doc = get_doc(word)
if len(doc) == 0 :
vib.append(doc)
## HELPER FUNCTIONS
def external_in_bg(cmd):
""" Run an external command, either minimized if on windows, or in the
background if on a unix system. """
if vim.eval("has('win32')") == '1' or vim.eval("has('win64')") == '1':
vim.command('!start /min ' + cmd)
elif vim.eval("has('unix')") == '1' or vim.eval("has('mac')") == '1':
vim.command('!' + cmd + ' &')
ds2ss = re.compile(r'\\\\')
def get_ipy_pwd():
msg_id = km.shell_channel.execute('', user_expressions={'pwd': 'get_ipython().magic("pwd")'})
try:
pwd = get_child_msg(msg_id)
pwd = pwd['content']['user_expressions']['pwd'][2:-1] # remove the u'....'
pwd = re.sub(ds2ss, r'/', pwd)
return pwd
except Empty:
# timeout occurred
return echo("no reply from IPython kernel")
def goto_vib(insert_at_end=True):
global vib
try:
name = get_vim_ipython_buffer().name
vim.command('drop ' + name)
if insert_at_end:
vim.command('normal G')
vim.command('startinsert!')
except:
echo("It appears that the vipy.py buffer was deleted. If the ipython kernel is still open, you can create a new vipy buffer without reseting the python server by pressing CTRL-F12. If the ipython server is no longer available, reset the server by pressing SHIFT-F12 and then CTRL-F12 to start it up again along with a new vipy buffer.")
vib = None
def toggle_vib():
if in_vipy():
if len(vim.windows) == 1:
vim.command('bprevious')
else:
vim.command('exe "normal \<C-w>p"')
else:
goto_vib()
def at_end_of_prompt():
""" Is the cursor at the end of a prompt line? """
row, col = vim.current.window.cursor
lineend = len(vim.current.line) - 1
bufend = len(vim.current.buffer)
return numspace.match(vim.current.line) and row == bufend and col == lineend
def above_prompt():
""" See if the cursor is above the last >>> prompt. """
row, col = vim.current.window.cursor
i = len(vib) - 1
last_prompt = 0
while i >= 0:
if vib[i].startswith(r'>>> '):
last_prompt = i + 1 # convert from index to line-number
break
if row < last_prompt:
return True
else:
return False
def is_vim_ipython_open():
"""
Helper function to let us know if the vipy shell is currently
visible
"""
for w in vim.windows:
if w.buffer.name is not None and w.buffer.name.endswith("vipy.py"):
return True
return False
def in_vipy():
cbn = vim.current.buffer.name
if cbn:
return cbn.endswith('vipy.py')
else:
return False
def insert_at_new():
""" Insert at the bottom of the file, if it is the ipy buffer. """
if in_vipy():
# insert at end of last line
vim.command('normal G')
vim.command('startinsert!')
def get_vim_ipython_buffer():
""" Return the vipy buffer. """
for b in vim.buffers:
try:
if b.name.endswith("vipy.py"):
return b
except:
continue
return False
def get_vim_ipython_window():
""" Return the vipy window. """
for w in vim.windows:
if w.buffer.name is not None and w.buffer.name.endswith("vipy.py"):
return w
raise Exception("couldn't find vipy window")
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
# 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)
EOF