forked from vim-scripts/slimv.vim
/
slimv.vim
2475 lines (2248 loc) · 86.7 KB
/
slimv.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
" slimv.vim: The Superior Lisp Interaction Mode for VIM
" Version: 0.9.2
" Last Change: 25 Oct 2011
" Maintainer: Tamas Kovacs <kovisoft at gmail dot com>
" License: This file is placed in the public domain.
" No warranty, express or implied.
" *** *** Use At-Your-Own-Risk! *** ***
"
" =====================================================================
"
" Load Once:
if &cp || exists( 'g:slimv_loaded' )
finish
endif
let g:slimv_loaded = 1
let g:slimv_windows = 0
let g:slimv_cygwin = 0
let g:slimv_osx = 0
if has( 'win32' ) || has( 'win95' ) || has( 'win64' ) || has( 'win16' )
let g:slimv_windows = 1
elseif has( 'win32unix' )
let g:slimv_cygwin = 1
elseif has( 'macunix' )
let g:slimv_osx = 1
endif
" =====================================================================
" Functions used by global variable definitions
" =====================================================================
" Try to autodetect Python executable
function! SlimvAutodetectPython()
if !g:slimv_cygwin && executable( 'python' )
return 'python'
endif
if g:slimv_windows || g:slimv_cygwin
" Try to find Python on the standard installation places
" For Cygwin we need to use the Windows Python instead of the Cygwin Python
let pythons = split( globpath( 'c:/python*,c:/Program Files/python*', 'python.exe' ), '\n' )
if len( pythons ) == 0
" Go deeper in subdirectories
let pythons = split( globpath( 'c:/python*/**,c:/Program Files/python*/**', 'python.exe' ), '\n' )
if len( pythons ) == 0
return ''
endif
endif
let pycmd = pythons[0]
if match( pycmd, ' ' ) >= 0
" Convert Python command to short 8.3 format if path contains spaces
let pycmd = fnamemodify( pycmd, ':8' )
endif
return pycmd
else
return ''
endif
endfunction
" Convert Cygwin path to Windows path, if needed
function! s:Cygpath( path )
let path = a:path
if g:slimv_cygwin
let path = system( 'cygpath -w ' . path )
let path = substitute( path, "\n", "", "g" )
let path = substitute( path, "\\", "/", "g" )
endif
return path
endfunction
" Find swank.py in the Vim ftplugin directory (if not given in vimrc)
if !exists( 'g:swank_path' )
let plugins = split( globpath( &runtimepath, 'ftplugin/**/swank.py'), '\n' )
if len( plugins ) > 0
let g:swank_path = s:Cygpath( plugins[0] )
else
let g:swank_path = 'swank.py'
endif
endif
" Get the filetype (Lisp dialect) used by Slimv
function! SlimvGetFiletype()
if &ft != ''
" Return Vim filetype if defined
return &ft
endif
if match( tolower( g:slimv_lisp ), 'clojure' ) >= 0 || match( tolower( g:slimv_lisp ), 'clj' ) >= 0
" Must be Clojure
return 'clojure'
endif
" We have no clue, guess its lisp
return 'lisp'
endfunction
" Try to autodetect SWANK and build the command to start the SWANK server
function! SlimvSwankCommand()
if exists( 'g:slimv_swank_clojure' ) && SlimvGetFiletype() == 'clojure'
return g:slimv_swank_clojure
endif
if exists( 'g:slimv_swank_scheme' ) && SlimvGetFiletype() == 'scheme'
return g:slimv_swank_scheme
endif
if exists( 'g:slimv_swank_cmd' )
return g:slimv_swank_cmd
endif
if g:slimv_lisp == ''
let g:slimv_lisp = input( 'Enter Lisp path (or fill g:slimv_lisp in your vimrc): ', '', 'file' )
endif
let cmd = ''
if SlimvGetFiletype() == 'clojure'
" First autodetect Leiningen and Cake
if executable( 'lein' )
let cmd = '"lein swank"'
elseif executable( 'cake' )
let cmd = '"cake swank"'
else
" Check if swank-clojure is bundled with Slimv
let swanks = split( globpath( &runtimepath, 'swank-clojure/swank/swank.clj'), '\n' )
if len( swanks ) == 0
return ''
endif
let sclj = substitute( swanks[0], '\', '/', "g" )
let cmd = g:slimv_lisp . ' -i "' . sclj . '" -e "(swank.swank/start-repl)" -r'
endif
elseif SlimvGetFiletype() == 'scheme'
let swanks = split( globpath( &runtimepath, 'slime/contrib/swank-mit-scheme.scm'), '\n' )
if len( swanks ) == 0
return ''
endif
if b:SlimvImplementation() == 'mit'
let cmd = '"' . g:slimv_lisp . '" --load "' . swanks[0] . '"'
endif
else
" First check if SWANK is bundled with Slimv
let swanks = split( globpath( &runtimepath, 'slime/start-swank.lisp'), '\n' )
if len( swanks ) == 0
" Try to find SWANK in the standard SLIME installation locations
if g:slimv_windows || g:slimv_cygwin
let swanks = split( globpath( 'c:/slime/,c:/*lisp*/slime/,c:/*lisp*/site/lisp/slime/,c:/Program Files/*lisp*/site/lisp/slime/', 'start-swank.lisp' ), '\n' )
else
let swanks = split( globpath( '/usr/share/common-lisp/source/slime/', 'start-swank.lisp' ), '\n' )
endif
endif
if len( swanks ) == 0
return ''
endif
" Build proper SWANK start command for the Lisp implementation used
if b:SlimvImplementation() == 'sbcl'
let cmd = '"' . g:slimv_lisp . '" --load "' . swanks[0] . '"'
elseif b:SlimvImplementation() == 'clisp'
let cmd = '"' . g:slimv_lisp . '" -i "' . swanks[0] . '"'
elseif b:SlimvImplementation() == 'allegro'
let cmd = '"' . g:slimv_lisp . '" -L "' . swanks[0] . '"'
elseif b:SlimvImplementation() == 'cmu'
let cmd = '"' . g:slimv_lisp . '" -load "' . swanks[0] . '"'
else
let cmd = '"' . g:slimv_lisp . '" -l "' . swanks[0] . '"'
endif
endif
if cmd != ''
if g:slimv_windows || g:slimv_cygwin
return '!start /MIN ' . cmd
elseif g:slimv_osx
return '!osascript -e "tell application \"Terminal\" to do script \"' . cmd . '\""'
else
return '! xterm -iconic -e ' . cmd . ' &'
endif
endif
return ''
endfunction
" =====================================================================
" Global variable definitions
" =====================================================================
" Host name or IP address of the SWANK server
if !exists( 'g:swank_host' )
let g:swank_host = 'localhost'
endif
" TCP port number to use for the SWANK server
if !exists( 'g:swank_port' )
let g:swank_port = 4005
endif
" Find Python (if not given in vimrc)
if !exists( 'g:slimv_python' )
let g:slimv_python = SlimvAutodetectPython()
endif
" Find Lisp (if not given in vimrc)
if !exists( 'g:slimv_lisp' )
let lisp = b:SlimvAutodetect()
let g:slimv_lisp = lisp[0]
if !exists( 'g:slimv_impl' )
let g:slimv_impl = lisp[1]
endif
endif
" Try to find out the Lisp implementation
" if not autodetected and not given in vimrc
if !exists( 'g:slimv_impl' )
let g:slimv_impl = b:SlimvImplementation()
endif
" REPL buffer name
if !exists( 'g:slimv_repl_name' )
let g:slimv_repl_name = 'REPL'
endif
" SLDB buffer name
if !exists( 'g:slimv_sldb_name' )
let g:slimv_sldb_name = 'SLDB'
endif
" INSPECT buffer name
if !exists( 'g:slimv_inspect_name' )
let g:slimv_inspect_name = 'INSPECT'
endif
" Shall we open REPL buffer in split window?
if !exists( 'g:slimv_repl_split' )
let g:slimv_repl_split = 1
endif
" Wrap long lines in REPL buffer
if !exists( 'g:slimv_repl_wrap' )
let g:slimv_repl_wrap = 1
endif
" Wrap long lines in SLDB buffer
if !exists( 'g:slimv_sldb_wrap' )
let g:slimv_sldb_wrap = 0
endif
" Maximum number of lines echoed from the evaluated form
if !exists( 'g:slimv_echolines' )
let g:slimv_echolines = 4
endif
" Syntax highlighting for the REPL buffer
if !exists( 'g:slimv_repl_syntax' )
let g:slimv_repl_syntax = 0
endif
" Alternative value (in msec) for 'updatetime' while the REPL buffer is changing
if !exists( 'g:slimv_updatetime' )
let g:slimv_updatetime = 500
endif
" Slimv keybinding set (0 = no keybindings)
if !exists( 'g:slimv_keybindings' )
let g:slimv_keybindings = 1
endif
" Append Slimv menu to the global menu (0 = no menu)
if !exists( 'g:slimv_menu' )
let g:slimv_menu = 1
endif
" Build the ctags command capable of generating lisp tags file
" The command can be run with execute 'silent !' . g:slimv_ctags
if !exists( 'g:slimv_ctags' )
let ctags = split( globpath( '$vim,$vimruntime', 'ctags.exe' ), '\n' )
if len( ctags ) > 0
" Remove -a option to regenerate every time
let g:slimv_ctags = '"' . ctags[0] . '" -a --language-force=lisp *.lisp *.clj'
endif
endif
" Package/namespace handling
if !exists( 'g:slimv_package' )
let g:slimv_package = 1
endif
" General timeout for various startup and connection events (seconds)
if !exists( 'g:slimv_timeout' )
let g:slimv_timeout = 20
endif
" Use balloonexpr to display symbol description
if !exists( 'g:slimv_balloon' )
let g:slimv_balloon = 1
endif
" Shall we use simple or fuzzy completion?
if !exists( 'g:slimv_simple_compl' )
let g:slimv_simple_compl = 0
endif
" Custom <Leader> for the Slimv plugin
if !exists( 'g:slimv_leader' )
if exists( 'mapleader' )
let g:slimv_leader = mapleader
else
let g:slimv_leader = ','
endif
endif
" Maximum number of lines searched backwards for indenting special forms
if !exists( 'g:slimv_indent_maxlines' )
let g:slimv_indent_maxlines = 20
endif
" =====================================================================
" Template definitions
" =====================================================================
if !exists( 'g:slimv_template_apropos' )
if SlimvGetFiletype() == 'clojure'
let g:slimv_template_apropos = '(find-doc "%1")'
else
let g:slimv_template_apropos = '(apropos "%1")'
endif
endif
" =====================================================================
" Other non-global script variables
" =====================================================================
let s:prompt = '' " Lisp prompt in the last line
let s:indent = '' " Most recent indentation info
let s:last_update = 0 " The last update time for the REPL buffer
let s:save_updatetime = &updatetime " The original value for 'updatetime'
let s:save_showmode = &showmode " The original value for 'showmode'
let s:python_initialized = 0 " Is the embedded Python initialized?
let s:swank_connected = 0 " Is the SWANK server connected?
let s:swank_package = '' " Package to use at the next SWANK eval
let s:swank_form = '' " Form to send to SWANK
let s:refresh_disabled = 0 " Set this variable temporarily to avoid recursive REPL rehresh calls
let s:debug_activated = 0 " Are we in the SWANK debugger?
let s:compiled_file = '' " Name of the compiled file
let s:au_curhold_set = 0 " Whether the autocommand has been set
let s:current_buf = -1 " Swank action was requested from this buffer
let s:current_win = -1 " Swank action was requested from this window
let s:skip_sc = 'synIDattr(synID(line("."), col("."), 0), "name") =~ "[Ss]tring\\|[Cc]omment"'
" Skip matches inside string or comment
let s:frame_def = '^\s\{0,2}\d\{1,3}:' " Regular expression to match SLDB restart or frame identifier
let s:spec_indent = 'flet\|labels\|macrolet' " List of symbols need special indenting
" =====================================================================
" General utility functions
" =====================================================================
" Display an error message
function! SlimvError( msg )
echohl ErrorMsg
echo a:msg
echohl None
endfunction
" Display an error message and a question, return user response
function! SlimvErrorAsk( msg, question )
echohl ErrorMsg
let answer = input( a:msg . a:question )
echo ""
echohl None
return answer
endfunction
" Display an error message and wait for ENTER
function! SlimvErrorWait( msg )
call SlimvErrorAsk( a:msg, " Press ENTER to continue." )
endfunction
" Shorten long messages to fit status line
function! SlimvShortEcho( msg )
let saved=&shortmess
set shortmess+=T
exe "normal :echomsg a:msg\n"
let &shortmess=saved
endfunction
" Position the cursor at the end of the REPL buffer
" Optionally mark this position in Vim mark 's'
function! SlimvEndOfReplBuffer()
normal! G$
endfunction
" Remember the end of the REPL buffer: user may enter commands here
" Also remember the prompt, because the user may overwrite it
function! SlimvMarkBufferEnd()
setlocal nomodified
call SlimvEndOfReplBuffer()
call setpos( "'s", [0, line('$'), col('$'), 0] )
let s:prompt = getline( "'s" )
endfunction
" Save caller buffer identification
function! SlimvBeginUpdate()
let s:current_buf = bufnr( "%" )
if winnr('$') < 2
" No windows yet
let s:current_win = -1
else
let s:current_win = winnr()
endif
endfunction
" Stop updating the REPL buffer and switch back to caller
function! SlimvEndUpdateRepl()
call SlimvMarkBufferEnd()
let repl_buf = bufnr( g:slimv_repl_name )
let repl_win = bufwinnr( repl_buf )
if repl_buf != s:current_buf && repl_win != -1 && !s:debug_activated
" Switch back to the caller buffer/window
if g:slimv_repl_split
if s:current_win == -1
let s:current_win = winnr('#')
endif
if s:current_win > 0 && s:current_win != repl_win
execute s:current_win . "wincmd w"
endif
else
execute "buf " . s:current_buf
endif
endif
endfunction
" Handle response coming from the SWANK listener
function! SlimvSwankResponse()
let s:refresh_disabled = 1
silent execute 'python swank_output(1)'
let s:refresh_disabled = 0
let msg = ''
redir => msg
silent execute 'python swank_response("")'
redir END
if msg != ''
if s:swank_action == ':describe-symbol'
echo substitute(msg,'^\n*','','')
endif
endif
if s:swank_actions_pending
let s:last_update = -1
elseif s:last_update < 0
" Remember the time when all actions are processed
let s:last_update = localtime()
endif
if s:swank_actions_pending == 0 && s:last_update >= 0 && s:last_update < localtime() - 2
" All SWANK output handled long ago, restore original update frequency
let &updatetime = s:save_updatetime
endif
endfunction
" Execute the given command and write its output at the end of the REPL buffer
function! SlimvCommand( cmd )
silent execute a:cmd
if g:slimv_updatetime < &updatetime
" Update more frequently until all swank responses processed
let &updatetime = g:slimv_updatetime
let s:last_update = -1
endif
endfunction
" Execute the given SWANK command, wait for and return the response
function! SlimvCommandGetResponse( name, cmd )
let s:refresh_disabled = 1
call SlimvCommand( a:cmd )
let msg = ''
let s:swank_action = ''
let starttime = localtime()
let cmd_timeout = 3
while s:swank_action == '' && localtime()-starttime < cmd_timeout
python swank_output( 0 )
redir => msg
silent execute 'python swank_response("' . a:name . '")'
redir END
endwhile
let s:refresh_disabled = 0
return msg
endfunction
" Reload the contents of the REPL buffer from the output file if changed
function! SlimvRefreshReplBuffer()
if s:refresh_disabled
" Refresh is unwanted at the moment, probably another refresh is going on
return
endif
let repl_buf = bufnr( g:slimv_repl_name )
if repl_buf == -1
" REPL buffer not loaded
return
endif
let repl_win = bufwinnr( repl_buf )
let this_win = winnr()
if s:swank_connected
call SlimvSwankResponse()
endif
endfunction
" This function re-triggers the CursorHold event
" after refreshing the REPL buffer
function! SlimvTimer()
call SlimvRefreshReplBuffer()
if mode() == 'i' || mode() == 'I' || mode() == 'r' || mode() == 'R'
" Put '<Insert>' twice into the typeahead buffer, which should not do anything
" just switch to replace/insert mode then back to insert/replace mode
" But don't do this for readonly buffers
if bufname('%') != g:slimv_sldb_name && bufname('%') != g:slimv_inspect_name
call feedkeys("\<insert>\<insert>")
endif
else
" Put an incomplete 'f' command and an Esc into the typeahead buffer
call feedkeys("f\e")
endif
endfunction
" Switch refresh mode on:
" refresh REPL buffer on frequent Vim events
function! SlimvRefreshModeOn()
if ! s:au_curhold_set
let s:au_curhold_set = 1
execute "au CursorHold * :call SlimvTimer()"
execute "au CursorHoldI * :call SlimvTimer()"
endif
endfunction
" Switch refresh mode off
function! SlimvRefreshModeOff()
execute "au! CursorHold"
execute "au! CursorHoldI"
let s:au_curhold_set = 0
endfunction
" Called when entering REPL buffer
function! SlimvReplEnter()
call SlimvAddReplMenu()
execute "au FileChangedRO " . g:slimv_repl_name . " :call SlimvRefreshModeOff()"
call SlimvRefreshModeOn()
endfunction
" Called when leaving REPL buffer
function! SlimvReplLeave()
try
" Check if REPL menu exists, then remove it
aunmenu REPL
execute ':unmap ' . g:slimv_leader . '\'
catch /.*/
" REPL menu not found, we cannot remove it
endtry
if g:slimv_repl_split
call SlimvRefreshModeOn()
else
call SlimvRefreshModeOff()
endif
endfunction
" View the given file in a top/bottom/left/right split window
function! s:SplitView( filename )
if winnr('$') == 2
" We have exactly two windows
if bufnr("%") == s:current_buf && winnr() == s:current_win
" Keep the current window on screen, use the other window for the new buffer
execute "wincmd w"
endif
execute "silent view! " . a:filename
else
if g:slimv_repl_split == 1
execute "silent topleft sview! " . a:filename
elseif g:slimv_repl_split == 2
execute "silent botright sview! " . a:filename
elseif g:slimv_repl_split == 3
execute "silent topleft vertical sview! " . a:filename
elseif g:slimv_repl_split == 4
execute "silent botright vertical sview! " . a:filename
else
execute "silent view! " . a:filename
endif
endif
stopinsert
endfunction
" Open a buffer with the given name if not yet open, and switch to it
function! SlimvOpenBuffer( name )
let buf = bufnr( a:name )
if buf == -1
" Create a new buffer
call s:SplitView( a:name )
else
if g:slimv_repl_split
" Buffer is already created. Check if it is open in a window
let win = bufwinnr( buf )
if win == -1
" Create windows
call s:SplitView( a:name )
else
" Switch to the buffer's window
if winnr() != win
execute win . "wincmd w"
endif
endif
else
execute "buffer " . buf
stopinsert
endif
endif
setlocal buftype=nofile
setlocal noswapfile
setlocal noreadonly
endfunction
" Go to the end of the screen line
function s:EndOfScreenLine()
if len(getline('.')) < &columns
" g$ moves the cursor to the rightmost column if virtualedit=all
normal! $
else
normal! g$
endif
endfunction
" Open a new REPL buffer
function! SlimvOpenReplBuffer()
call SlimvOpenBuffer( g:slimv_repl_name )
call b:SlimvInitRepl()
call PareditInitBuffer()
if !g:slimv_repl_syntax
set syntax=
endif
" Add keybindings valid only for the REPL buffer
inoremap <buffer> <silent> <CR> <C-R>=pumvisible() ? "\<lt>CR>" : "\<lt>End>\<lt>C-O>:call SlimvSendCommand(0)\<lt>CR>"<CR>
inoremap <buffer> <silent> <C-CR> <End><C-O>:call SlimvSendCommand(1)<CR>
inoremap <buffer> <silent> <Up> <C-R>=pumvisible() ? "\<lt>Up>" : "\<lt>C-O>:call SlimvHandleUp()\<lt>CR>"<CR>
inoremap <buffer> <silent> <Down> <C-R>=pumvisible() ? "\<lt>Down>" : "\<lt>C-O>:call SlimvHandleDown()\<lt>CR>"<CR>
inoremap <buffer> <silent> <C-C> <C-O>:call SlimvInterrupt()<CR>
if exists( 'g:paredit_loaded' )
inoremap <buffer> <silent> <expr> <BS> PareditBackspace(1)
else
inoremap <buffer> <silent> <expr> <BS> SlimvHandleBS()
endif
if g:slimv_keybindings == 1
execute 'noremap <buffer> <silent> ' . g:slimv_leader.'. :call SlimvSendCommand(0)<CR>'
execute 'noremap <buffer> <silent> ' . g:slimv_leader.'/ :call SlimvSendCommand(1)<CR>'
execute 'noremap <buffer> <silent> ' . g:slimv_leader.'<Up> :call SlimvPreviousCommand()<CR>'
execute 'noremap <buffer> <silent> ' . g:slimv_leader.'<Down> :call SlimvNextCommand()<CR>'
elseif g:slimv_keybindings == 2
execute 'noremap <buffer> <silent> ' . g:slimv_leader.'rs :call SlimvSendCommand(0)<CR>'
execute 'noremap <buffer> <silent> ' . g:slimv_leader.'ro :call SlimvSendCommand(1)<CR>'
execute 'noremap <buffer> <silent> ' . g:slimv_leader.'rp :call SlimvPreviousCommand()<CR>'
execute 'noremap <buffer> <silent> ' . g:slimv_leader.'rn :call SlimvNextCommand()<CR>'
endif
if g:slimv_repl_wrap
inoremap <buffer> <silent> <Home> <C-O>g<Home>
inoremap <buffer> <silent> <End> <C-O>:call <SID>EndOfScreenLine()<CR>
noremap <buffer> <silent> <Up> gk
noremap <buffer> <silent> <Down> gj
noremap <buffer> <silent> <Home> g<Home>
noremap <buffer> <silent> <End> :call <SID>EndOfScreenLine()<CR>
noremap <buffer> <silent> k gk
noremap <buffer> <silent> j gj
noremap <buffer> <silent> 0 g0
noremap <buffer> <silent> $ :call <SID>EndOfScreenLine()<CR>
setlocal wrap
endif
hi SlimvNormal term=none cterm=none gui=none
hi SlimvCursor term=reverse cterm=reverse gui=reverse
" Add autocommands specific to the REPL buffer
execute "au FileChangedShell " . g:slimv_repl_name . " :call SlimvRefreshReplBuffer()"
execute "au FocusGained " . g:slimv_repl_name . " :call SlimvRefreshReplBuffer()"
execute "au BufEnter " . g:slimv_repl_name . " :call SlimvReplEnter()"
execute "au BufLeave " . g:slimv_repl_name . " :call SlimvReplLeave()"
call SlimvRefreshReplBuffer()
endfunction
" Open a new Inspect buffer
function SlimvOpenInspectBuffer()
call SlimvOpenBuffer( g:slimv_inspect_name )
" Add keybindings valid only for the Inspect buffer
noremap <buffer> <silent> <CR> :call SlimvHandleEnterInspect()<CR>
noremap <buffer> <silent> <Backspace> :call SlimvSendSilent(['[-1]'])<CR>
execute 'noremap <buffer> <silent> ' . g:slimv_leader.'q :call SlimvQuitInspect()<CR>'
endfunction
" Open a new SLDB buffer
function SlimvOpenSldbBuffer()
call SlimvOpenBuffer( g:slimv_sldb_name )
" Add keybindings valid only for the SLDB buffer
noremap <buffer> <silent> <CR> :call SlimvHandleEnterSldb()<CR>
if g:slimv_keybindings == 1
execute 'noremap <buffer> <silent> ' . g:slimv_leader.'a :call SlimvDebugCommand("swank_invoke_abort")<CR>'
execute 'noremap <buffer> <silent> ' . g:slimv_leader.'q :call SlimvDebugCommand("swank_throw_toplevel")<CR>'
execute 'noremap <buffer> <silent> ' . g:slimv_leader.'n :call SlimvDebugCommand("swank_invoke_continue")<CR>'
elseif g:slimv_keybindings == 2
execute 'noremap <buffer> <silent> ' . g:slimv_leader.'da :call SlimvDebugCommand("swank_invoke_abort")<CR>'
execute 'noremap <buffer> <silent> ' . g:slimv_leader.'dq :call SlimvDebugCommand("swank_throw_toplevel")<CR>'
execute 'noremap <buffer> <silent> ' . g:slimv_leader.'dn :call SlimvDebugCommand("swank_invoke_continue")<CR>'
endif
" Set folding parameters
setlocal foldmethod=marker
setlocal foldmarker={{{,}}}
setlocal foldtext=substitute(getline(v:foldstart),'{{{','','')
setlocal iskeyword+=+,-,*,/,%,<,=,>,:,$,?,!,@-@,94,~,#,\|,&,{,},[,]
if g:slimv_sldb_wrap
setlocal wrap
endif
if version < 703
" conceal mechanism is defined since Vim 7.3
syn match Ignore /{{{/
syn match Ignore /}}}/
else
setlocal conceallevel=3 concealcursor=nc
syn match Comment /{{{/ conceal
syn match Comment /}}}/ conceal
endif
syn match Type /^\s\{0,2}\d\{1,3}:/
syn match Type /^\s\+in "\(.*\)" \(line\|byte\) \(\d\+\)$/
endfunction
" End updating an otherwise readonly buffer
function SlimvEndUpdate()
setlocal readonly
setlocal nomodified
endfunction
" Quit Inspector
function SlimvQuitInspect()
" Clear the contents of the Inspect buffer
setlocal noreadonly
silent! %d
call SlimvEndUpdate()
call SlimvCommand( 'python swank_quit_inspector()' )
b #
endfunction
" Quit Sldb
function SlimvQuitSldb()
" Clear the contents of the Sldb buffer
setlocal noreadonly
silent! %d
call SlimvEndUpdate()
b #
endfunction
" Open SLDB buffer and place cursor on the given frame
function SlimvGotoFrame( frame )
call SlimvOpenSldbBuffer()
let bcktrpos = search( '^Backtrace:', 'bcnw' )
let line = getline( '.' )
let item = matchstr( line, '^\s*' . a:frame . ':' )
if item != '' && line('.') > bcktrpos
" Already standing on the frame
return
endif
" Must locate the frame starting from the 'Backtrace:' string
call search( '^Backtrace:', 'bcw' )
call search( '^\s*' . a:frame . ':', 'w' )
endfunction
" Set 'iskeyword' option depending on file type
function! s:SetKeyword()
if SlimvGetFiletype() == 'clojure'
setlocal iskeyword+=+,-,*,/,%,<,=,>,:,$,?,!,@-@,94,~,#,\|,&
else
setlocal iskeyword+=+,-,*,/,%,<,=,>,:,$,?,!,@-@,94,~,#,\|,&,{,},[,]
endif
endfunction
" Select symbol under cursor and return it
function! SlimvSelectSymbol()
call s:SetKeyword()
let symbol = expand('<cword>')
return symbol
endfunction
" Select symbol with possible prefixes under cursor and return it
function! SlimvSelectSymbolExt()
let save_iskeyword = &iskeyword
call s:SetKeyword()
setlocal iskeyword+='
let symbol = expand('<cword>')
let &iskeyword = save_iskeyword
return symbol
endfunction
" Select bottom level form the cursor is inside and copy it to register 's'
function! SlimvSelectForm()
" Search the opening '(' if we are standing on a special form prefix character
let c = col( '.' ) - 1
while c < len( getline( '.' ) ) && match( "'`#", getline( '.' )[c] ) >= 0
normal! l
let c = c + 1
endwhile
normal! va(o
" Handle '() or #'() etc. type special syntax forms
let c = col( '.' ) - 2
while c >= 0 && match( ' \t()', getline( '.' )[c] ) < 0
normal! h
let c = c - 1
endwhile
silent normal! "sy
let sel = SlimvGetSelection()
if sel == ''
call SlimvError( "Form is empty." )
return 0
elseif sel == '(' || sel == '[' || sel == '{'
call SlimvError( "Form is unbalanced." )
return 0
else
return 1
endif
endfunction
" Find starting '(' of a top level form
function! SlimvFindDefunStart()
let l = line( '.' )
let matchb = max( [l-100, 1] )
while searchpair( '(', '', ')', 'bW', s:skip_sc, matchb )
endwhile
endfunction
" Select top level form the cursor is inside and copy it to register 's'
function! SlimvSelectDefun()
call SlimvFindDefunStart()
return SlimvSelectForm()
endfunction
" Return the contents of register 's'
function! SlimvGetSelection()
return getreg( '"s' )
endfunction
" Find the given string backwards and put it in front of the current selection
" if it is a valid Lisp form (i.e. not inside comment or string)
function! SlimvFindAddSel( string )
let found = 0
let searching = search( '(\s*' . a:string . '\s', 'bcW' )
while searching
" Search for the previos occurrence
if synIDattr( synID( line('.'), col('.'), 0), 'name' ) !~ '[Ss]tring\|[Cc]omment'
" It is not inside a comment or string
let found = 1
break
endif
let searching = search( '(\s*' . a:string . '\s', 'bW' )
endwhile
if found
silent normal! ww
let l:packagename_tokens = split(expand('<cWORD>'),')\|\s')
if l:packagename_tokens != []
let s:swank_package = l:packagename_tokens[0]
else
let s:swank_package = ''
endif
endif
endfunction
" Find and add language specific package/namespace definition before the
" cursor position and if exists then add it in front of the current selection
function! SlimvFindPackage()
if !g:slimv_package || SlimvGetFiletype() == 'scheme'
return
endif
let oldpos = getpos( '.' )
if SlimvGetFiletype() == 'clojure'
call SlimvFindAddSel( 'in-ns' )
else
call SlimvFindAddSel( '\(cl:\|common-lisp:\|\)in-package' )
endif
call setpos( '.', oldpos )
endfunction
" Execute the given SWANK command with current package defined
function! SlimvCommandUsePackage( cmd )
let oldpos = getpos( '.' )
call SlimvFindPackage()
let s:refresh_disabled = 1
call setpos( '.', oldpos )
call SlimvCommand( a:cmd )
let s:swank_package = ''
let s:refresh_disabled = 0
call SlimvRefreshReplBuffer()
endfunction
" Initialize embedded Python and connect to SWANK server
function! SlimvConnectSwank()
if !s:python_initialized
if ! has('python')
call SlimvErrorWait( 'Vim is compiled without the Python feature. Unable to run SWANK client.' )
return 0
endif
if g:slimv_windows || g:slimv_cygwin
" Verify that Vim is compiled with Python and Python is properly installed
let v = ''
redir => v
silent ver
redir END
let pydll = matchstr( v, '\cpython..\.dll' )
if ! executable( pydll )
call SlimvErrorWait( pydll . ' not found. Unable to run SWANK client.' )
return 0
endif
endif
python import vim
execute 'pyfile ' . g:swank_path
let s:python_initialized = 1
endif
if !s:swank_connected
let s:swank_version = ''
let s:lisp_version = ''
if g:swank_host == ''
let g:swank_host = input( 'Swank server host name: ', 'localhost' )
endif
execute 'python swank_connect("' . g:swank_host . '", ' . g:swank_port . ', "result" )'
if result != '' && ( g:swank_host == 'localhost' || g:swank_host == '127.0.0.1' )
" SWANK server is not running, start server if possible
let swank = SlimvSwankCommand()
if swank != ''
redraw
echon "\rStarting SWANK server..."
silent execute swank
let starttime = localtime()
while result != '' && localtime()-starttime < g:slimv_timeout
sleep 500m
execute 'python swank_connect("' . g:swank_host . '", ' . g:swank_port . ', "result" )'
endwhile
redraw!
endif
endif
if result != ''
" Display connection error message
call SlimvErrorWait( result )
return 0
endif
" Connected to SWANK server
redraw
echon "\rGetting SWANK connection info..."
let starttime = localtime()
while s:swank_version == '' && localtime()-starttime < g:slimv_timeout
call SlimvSwankResponse()
endwhile
if s:swank_version >= '2008-12-23'
call SlimvCommandGetResponse( ':create-repl', 'python swank_create_repl()' )
endif
let s:swank_connected = 1
if g:slimv_simple_compl == 0
python swank_require('swank-fuzzy')
call SlimvSwankResponse()
endif
redraw
echon "\rConnected to SWANK server on port " . g:swank_port . "."
if exists( "g:swank_block_size" ) && SlimvGetFiletype() == 'lisp'
" Override SWANK connection output buffer size
let cmd = "(progn (setf (slot-value (swank::connection.user-output swank::*emacs-connection*) 'swank-backend::buffer)"
let cmd = cmd . " (make-string " . g:swank_block_size . ")) nil)"
call SlimvSend( [cmd], 0, 1 )
endif
if exists( "*b:SlimvReplInit" )
" Perform implementation specific REPL initialization if supplied
call b:SlimvReplInit( s:lisp_version )
endif
endif
return s:swank_connected
endfunction
" Send argument to Lisp server for evaluation
function! SlimvSend( args, echoing, output )
call SlimvBeginUpdate()
if ! SlimvConnectSwank()
return
endif
" Send the lines to the client for evaluation
let text = join( a:args, "\n" ) . "\n"
let s:refresh_disabled = 1
let s:swank_form = text
if a:output
call SlimvOpenReplBuffer()
endif
if a:echoing && g:slimv_echolines != 0
if g:slimv_echolines > 0