/
slimv.vim
3809 lines (3500 loc) · 144 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.14
" Last Change: 19 Feb 2023
" 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
if ( !exists( 'g:slimv_python_version' ) && has( 'python3' ) ) ||
\ ( exists( 'g:slimv_python_version' ) && g:slimv_python_version == 3 )
let s:py_cmd = 'python3 ' "note space
let s:pyfile_cmd = 'py3file '
else
let s:py_cmd = 'python ' "note space
let s:pyfile_cmd = 'pyfile '
endif
" =====================================================================
" Functions used by global variable definitions
" =====================================================================
" 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 = SlimvSwankLoader()
if cmd != ''
if g:slimv_windows || g:slimv_cygwin
return '!start /MIN ' . cmd
elseif $STY != ''
" GNU screen under Linux or macOS
return "! screen -X eval 'title swank' 'screen " . cmd . "' 'select swank'"
elseif $TMUX != ''
" tmux under Linux or macOS
return "! tmux new-window -d -n swank '" . cmd . "'"
elseif g:slimv_osx
let result = system('osascript -e "exists application \"iterm\""')
if result[:-2] == 'true'
let path2as = globpath( &runtimepath, 'ftplugin/**/iterm.applescript')
return '!' . path2as . ' ' . cmd
else
" doubles quotes within 'cmd' need to become '\\\"'
return '!osascript -e "tell application \"Terminal\" to do script \"' . escape(escape(cmd, '"'), '\"') . '\""'
endif
elseif $DISPLAY == ''
" No X, no terminal multiplexer. Cannot run swank server.
call SlimvErrorWait( 'No X server. Run Vim from screen/tmux or start SWANK server manually.' )
return ''
else
" Must be Linux
return '! SWANK_PORT=' . g:swank_port . ' 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 Lisp (if not given in vimrc)
if !exists( 'g:slimv_lisp' )
let lisp = ['', '']
if exists( 'g:slimv_preferred' )
let lisp = SlimvAutodetect( tolower(g:slimv_preferred) )
endif
if lisp[0] == ''
let lisp = SlimvAutodetect( '' )
endif
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 = 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
" THREADS buffer name
if !exists( 'g:slimv_threads_name' )
let g:slimv_threads_name = 'THREADS'
endif
" Shall we open REPL buffer in split window?
if !exists( 'g:slimv_repl_split' )
let g:slimv_repl_split = 1
endif
" Size of the split window
if !exists( 'g:slimv_repl_split_size' )
let g:slimv_repl_split_size = ''
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 = 1
endif
" Specifies the behaviour of insert mode <CR>, <Up>, <Down> in the REPL buffer:
" 1: <CR> evaluates, <Up>/<Down> brings up command history
" 0: <C-CR> evaluates, <C-Up>/<C-Down> brings up command history,
" <CR> opens new line, <Up>/<Down> moves cursor up/down
if !exists( 'g:slimv_repl_simple_eval' )
let g:slimv_repl_simple_eval = 1
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
" Name of tags file used by slimv for find-definitions
" If this is the empty string then no tags file is used
if !exists( 'g:slimv_tags_file' )
let g:slimv_tags_file = tempname()
endif
" Prepend tags file to the tags list
if g:slimv_tags_file != ''
if &tags == ''
let &tags=g:slimv_tags_file
else
let &tags=g:slimv_tags_file . ',' . &tags
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' ) && 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 = 50
endif
" Special indentation for keyword lists
if !exists( 'g:slimv_indent_keylists' )
let g:slimv_indent_keylists = 1
endif
" Maximum length of the REPL buffer
if !exists( 'g:slimv_repl_max_len' )
let g:slimv_repl_max_len = 0
endif
" Shall we strip ANSI escape sequences from the REPL output?
if !exists( 'g:slimv_strip_ansi' )
let g:slimv_strip_ansi = 0
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: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_version = '' " SWANK server version string
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_package_form = '' " The entire form that was used to set current package
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:sldb_level = -1 " Are we in the SWANK debugger? -1 == no, else SLDB level
let s:break_on_exception = 0 " Enable debugger break on exceptions (for ritz-swank)
let s:compiled_file = '' " Name of the compiled file
let s:win_id = 0 " Counter for generating unique window id
let s:repl_buf = -1 " Buffer number for the REPL buffer
let s:current_buf = -1 " Swank action was requested from this buffer
let s:current_win = 0 " Swank action was requested from this window
let s:read_string_mode = 0 " Read string mode indicator
let s:arglist_line = 0 " Arglist was requested in this line ...
let s:arglist_col = 0 " ... and column
let s:inspect_path = [] " Inspection path of the current object
let s:skip_sc = 'synIDattr(synID(line("."), col("."), 0), "name") =~ "[Ss]tring\\|[Cc]omment"'
" Skip matches inside string or comment
let s:skip_q = 'getline(".")[col(".")-2] == "\\"' " Skip escaped double quote characters in matches
let s:frame_def = '^\s\{0,2}\d\{1,}:' " Regular expression to match SLDB restart or frame identifier
let s:spec_indent = 'flet\|labels\|macrolet\|symbol-macrolet'
" List of symbols need special indenting
let s:spec_param = 'defmacro' " List of symbols with special parameter list
let s:binding_form = 'let\|let\*' " List of symbols with binding list
" =====================================================================
" General utility functions
" =====================================================================
" Check that current SWANK version is same or newer than the given parameter
function! s:SinceVersion( ver )
" Before ver 2.18 SWANK version string was a date of form YYYY-MM-DD
if len( a:ver ) >= 8
" Checking for old style version string YYYY-MM-DD
if len( s:swank_version ) < 8
" Current version is new style -> must be newer than the one we are checking for
return 1
endif
else
" Checking for new style version string X.XX
if len( s:swank_version ) >= 8
" Current version is old style -> must be older than the one we are checking for
return 0
endif
endif
if s:swank_version >= a:ver
return 1
else
return 0
endif
endfunction
" 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
" Go to the end of buffer, make sure the cursor is positioned
" after the last character of the buffer when in insert mode
function s:EndOfBuffer()
normal! G$
if &virtualedit != 'all'
call cursor( line('$'), 99999 )
endif
endfunction
" Position the cursor at the end of the REPL buffer
" Optionally mark this position in Vim mark 's'
function! SlimvEndOfReplBuffer( force )
if line( '.' ) >= b:repl_prompt_line - 1 || a:force
" Go to the end of file only if the user did not move up from here
call s:EndOfBuffer()
endif
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( force )
if exists( 'b:slimv_repl_buffer' )
setlocal nomodified
call SlimvEndOfReplBuffer( a:force )
let b:repl_prompt_line = line( '$' )
let b:repl_prompt_col = len( getline('$') ) + 1
let b:repl_prompt = getline( b:repl_prompt_line )
endif
endfunction
" Get REPL prompt line. Fix stored prompt position when corrupted
" (e.g. some lines were deleted from the REPL buffer)
function! s:GetPromptLine()
if b:repl_prompt_line > line( '$' )
" Stored prompt line is corrupt
let b:repl_prompt_line = line( '$' )
let b:repl_prompt_col = len( getline('$') ) + 1
let b:repl_prompt = getline( b:repl_prompt_line )
endif
return b:repl_prompt_line
endfunction
" Generate unique window id for the current window
function s:MakeWindowId()
if g:slimv_repl_split && !exists('w:id')
let s:win_id = s:win_id + 1
let w:id = s:win_id
endif
endfunction
" Find and switch to window with the specified window id
function s:SwitchToWindow( id )
for winnr in range( 1, winnr('$') )
if getwinvar( winnr, 'id' ) is a:id
execute winnr . "wincmd w"
endif
endfor
endfunction
" Save caller buffer identification
function! SlimvBeginUpdate()
call s:MakeWindowId()
let s:current_buf = bufnr( "%" )
let s:current_win = getwinvar( winnr(), 'id' )
endfunction
" Switch to the buffer/window that was active before a swank action
function! SlimvRestoreFocus( hide_current_buf )
if exists("b:previous_buf")
let new_buf = b:previous_buf
let new_win = b:previous_win
else
let new_buf = s:current_buf
let new_win = s:current_win
endif
let buf = bufnr( "%" )
let win = getwinvar( winnr(), 'id' )
if a:hide_current_buf
set nobuflisted
b #
endif
if winnr('$') > 1 && new_win != '' && new_win != win
" Switch to the caller window
call s:SwitchToWindow( new_win )
endif
if new_buf >= 0 && buf != new_buf
" Switch to the caller buffer
execute "buf " . new_buf
endif
endfunction
" Handle response coming from the SWANK listener
function! SlimvSwankResponse()
let s:swank_ok_result = ''
let s:refresh_disabled = 1
silent execute s:py_cmd . 'swank_output(1)'
let s:refresh_disabled = 0
let s:swank_action = ''
let s:swank_result = ''
silent execute s:py_cmd . 'swank_response("")'
if s:swank_action == ':describe-symbol' && s:swank_result != ''
echo substitute(s:swank_result,'^\n*','','')
elseif s:swank_ok_result != ''
" Display the :ok result also in status bar in case the REPL buffer is not shown
let s:swank_ok_result = substitute(s:swank_ok_result,"\<LF>",'','g')
if s:swank_ok_result == ''
call SlimvShortEcho( '=> OK' )
else
call SlimvShortEcho( '=> ' . s:swank_ok_result )
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
if &updatetime == g:slimv_updatetime
let &updatetime = s:save_updatetime
endif
else
" SWANK output still pending, keep higher update frequency
if &updatetime != g:slimv_updatetime
let s:save_updatetime = &updatetime
endif
let &updatetime = g:slimv_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 &updatetime != g:slimv_updatetime
let s:save_updatetime = &updatetime
endif
" Update more frequently until all swank responses processed
let &updatetime = g:slimv_updatetime
let s:last_update = -1
endfunction
" Execute the given SWANK command, wait for and return the response
function! SlimvCommandGetResponse( name, cmd, timeout )
let s:refresh_disabled = 1
call SlimvCommand( a:cmd )
let s:swank_action = ''
let s:swank_result = ''
let starttime = localtime()
let cmd_timeout = a:timeout
if cmd_timeout == 0
let cmd_timeout = 3
endif
while s:swank_action == '' && localtime()-starttime < cmd_timeout
execute s:py_cmd . "swank_output( 0 )"
silent execute s:py_cmd . 'swank_response("' . a:name . '")'
endwhile
let s:refresh_disabled = 0
return s:swank_result
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
if s:repl_buf == -1
" REPL buffer not loaded
return
endif
if s:swank_connected
call SlimvSwankResponse()
endif
if exists("s:input_prompt") && s:input_prompt != ''
let answer = input( s:input_prompt )
unlet s:input_prompt
echo ""
call SlimvCommand( s:py_cmd . 'swank_return("' . answer . '")' )
endif
endfunction
" This function re-triggers the CursorHold event
" after refreshing the REPL buffer
function! SlimvTimer()
if v:count > 0
" Skip refreshing if the user started a command prefixed with a count
return
endif
" We don't want autocommands trigger during the quick switch to/from the REPL buffer
noautocmd call SlimvRefreshReplBuffer()
if mode() == 'i' || mode() == 'I' || mode() == 'r' || mode() == 'R'
if bufname('%') != g:slimv_sldb_name && bufname('%') != g:slimv_inspect_name && bufname('%') != g:slimv_threads_name
" 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
call feedkeys("\<insert>\<insert>")
endif
else
" Put an incomplete 'f' command and an Esc into the typeahead buffer
call feedkeys("f\e", 'n')
endif
endfunction
" Switch refresh mode on:
" refresh REPL buffer on frequent Vim events
function! SlimvRefreshModeOn()
augroup SlimvCursorHold
au!
execute "au CursorHold * :call SlimvTimer()"
execute "au CursorHoldI * :call SlimvTimer()"
augroup END
endfunction
" Switch refresh mode off
function! SlimvRefreshModeOff()
augroup SlimvCursorHold
au!
augroup END
endfunction
" Called when entering REPL buffer
function! SlimvReplEnter()
call SlimvAddReplMenu()
augroup SlimvReplChanged
au!
execute "au FileChangedRO " . g:slimv_repl_name . " :call SlimvRefreshModeOff()"
augroup END
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
" Refresh cursor position in the REPL buffer after new lines appended
function! SlimvReplSetCursorPos( force )
" We do not want these autocommands to fire, the buffer switch will be temporary
let savemark = getpos("'`'")
let save_ei = &eventignore
set eventignore=BufEnter,BufLeave,BufWinEnter
let win = winnr()
windo call SlimvMarkBufferEnd( a:force )
execute win . "wincmd w"
let &eventignore = save_ei
call setpos("'`", savemark)
endfunction
" View the given file in a top/bottom/left/right split window
function! s:SplitView( filename )
" Check if we have at least two windows used by slimv (have a window id assigned)
let winnr1 = 0
let winnr2 = 0
for winnr in range( 1, winnr('$') )
if getwinvar( winnr, 'id' ) != ''
let winnr2 = winnr1
let winnr1 = winnr
endif
endfor
" create a unique buffer name that does not collide with any file or directory name
let bname = a:filename
let i = 0
while filereadable(bname) || isdirectory(bname)
let i = i+1
let bname = a:filename . i
endwhile
if winnr1 > 0 && winnr2 > 0
" We have already at least two windows used by slimv
let winid = getwinvar( winnr(), 'id' )
if bufnr("%") == s:current_buf && winid == s:current_win
" Keep the current window on screen, use the other window for the new buffer
if winnr1 != winnr()
execute winnr1 . "wincmd w"
else
execute winnr2 . "wincmd w"
endif
endif
execute "silent view! " . bname
else
" Generate unique window id for the old window if not yet done
call s:MakeWindowId()
" No windows yet, need to split
if g:slimv_repl_split == 1
execute "silent topleft " . g:slimv_repl_split_size . "sview! " . bname
elseif g:slimv_repl_split == 2
execute "silent botright " . g:slimv_repl_split_size . "sview! " . bname
elseif g:slimv_repl_split == 3
execute "silent topleft vertical " . g:slimv_repl_split_size . "sview! " . bname
elseif g:slimv_repl_split == 4
execute "silent botright vertical " . g:slimv_repl_split_size . "sview! " . bname
else
execute "silent view! " . bname
endif
" Generate unique window id for the new window as well
call s:MakeWindowId()
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
if s:current_buf != bufnr( "%" )
" Keep track of the previous buffer and window
let b:previous_buf = s:current_buf
let b:previous_win = s:current_win
endif
setlocal buftype=nofile
setlocal noswapfile
setlocal modifiable
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
" Set special syntax rules for the REPL buffer
function! SlimvSetSyntaxRepl()
if SlimvGetFiletype() == 'scheme'
syn cluster replListCluster contains=@schemeListCluster,lispList
else
syn cluster replListCluster contains=@lispListCluster
endif
if exists("g:lisp_rainbow") && g:lisp_rainbow != 0
if &bg == "dark"
hi def hlLevel0 ctermfg=red guifg=red1
hi def hlLevel1 ctermfg=yellow guifg=orange1
hi def hlLevel2 ctermfg=green guifg=yellow1
hi def hlLevel3 ctermfg=cyan guifg=greenyellow
hi def hlLevel4 ctermfg=magenta guifg=green1
hi def hlLevel5 ctermfg=red guifg=springgreen1
hi def hlLevel6 ctermfg=yellow guifg=cyan1
hi def hlLevel7 ctermfg=green guifg=slateblue1
hi def hlLevel8 ctermfg=cyan guifg=magenta1
hi def hlLevel9 ctermfg=magenta guifg=purple1
else
hi def hlLevel0 ctermfg=red guifg=red3
hi def hlLevel1 ctermfg=darkyellow guifg=orangered3
hi def hlLevel2 ctermfg=darkgreen guifg=orange2
hi def hlLevel3 ctermfg=blue guifg=yellow3
hi def hlLevel4 ctermfg=darkmagenta guifg=olivedrab4
hi def hlLevel5 ctermfg=red guifg=green4
hi def hlLevel6 ctermfg=darkyellow guifg=paleturquoise3
hi def hlLevel7 ctermfg=darkgreen guifg=deepskyblue4
hi def hlLevel8 ctermfg=blue guifg=darkslateblue
hi def hlLevel9 ctermfg=darkmagenta guifg=darkviolet
endif
if SlimvGetFiletype() =~ '.*\(clojure\|scheme\|racket\).*'
syn region lispParen9 matchgroup=hlLevel9 start="`\=(" matchgroup=hlLevel9 end=")" matchgroup=replPrompt end="^\S\+>" contains=TOP,@Spell
syn region lispParen0 matchgroup=hlLevel8 start="`\=(" end=")" matchgroup=replPrompt end="^\S\+>"me=s-1,re=s-1 contains=TOP,lispParen0,lispParen1,lispParen2,lispParen3,lispParen4,lispParen5,lispParen6,lispParen7,lispParen8,NoInParens
syn region lispParen1 matchgroup=hlLevel7 start="`\=(" end=")" matchgroup=replPrompt end="^\S\+>"me=s-1,re=s-1 contains=TOP,lispParen1,lispParen2,lispParen3,lispParen4,lispParen5,lispParen6,lispParen7,lispParen8,NoInParens
syn region lispParen2 matchgroup=hlLevel6 start="`\=(" end=")" matchgroup=replPrompt end="^\S\+>"me=s-1,re=s-1 contains=TOP,lispParen2,lispParen3,lispParen4,lispParen5,lispParen6,lispParen7,lispParen8,NoInParens
syn region lispParen3 matchgroup=hlLevel5 start="`\=(" end=")" matchgroup=replPrompt end="^\S\+>"me=s-1,re=s-1 contains=TOP,lispParen3,lispParen4,lispParen5,lispParen6,lispParen7,lispParen8,NoInParens
syn region lispParen4 matchgroup=hlLevel4 start="`\=(" end=")" matchgroup=replPrompt end="^\S\+>"me=s-1,re=s-1 contains=TOP,lispParen4,lispParen5,lispParen6,lispParen7,lispParen8,NoInParens
syn region lispParen5 matchgroup=hlLevel3 start="`\=(" end=")" matchgroup=replPrompt end="^\S\+>"me=s-1,re=s-1 contains=TOP,lispParen5,lispParen6,lispParen7,lispParen8,NoInParens
syn region lispParen6 matchgroup=hlLevel2 start="`\=(" end=")" matchgroup=replPrompt end="^\S\+>"me=s-1,re=s-1 contains=TOP,lispParen6,lispParen7,lispParen8,NoInParens
syn region lispParen7 matchgroup=hlLevel1 start="`\=(" end=")" matchgroup=replPrompt end="^\S\+>"me=s-1,re=s-1 contains=TOP,lispParen7,lispParen8,NoInParens
syn region lispParen8 matchgroup=hlLevel0 start="`\=(" end=")" matchgroup=replPrompt end="^\S\+>"me=s-1,re=s-1 contains=TOP,lispParen8,NoInParens
syn region lispParen9 matchgroup=hlLevel9 start="`\=\[" matchgroup=hlLevel9 end="\]" matchgroup=replPrompt end="^\S\+>" contains=TOP,@Spell
syn region lispParen0 matchgroup=hlLevel8 start="`\=\[" end="\]" matchgroup=replPrompt end="^\S\+>"me=s-1,re=s-1 contains=TOP,lispParen0,lispParen1,lispParen2,lispParen3,lispParen4,lispParen5,lispParen6,lispParen7,lispParen8,NoInParens
syn region lispParen1 matchgroup=hlLevel7 start="`\=\[" end="\]" matchgroup=replPrompt end="^\S\+>"me=s-1,re=s-1 contains=TOP,lispParen1,lispParen2,lispParen3,lispParen4,lispParen5,lispParen6,lispParen7,lispParen8,NoInParens
syn region lispParen2 matchgroup=hlLevel6 start="`\=\[" end="\]" matchgroup=replPrompt end="^\S\+>"me=s-1,re=s-1 contains=TOP,lispParen2,lispParen3,lispParen4,lispParen5,lispParen6,lispParen7,lispParen8,NoInParens
syn region lispParen3 matchgroup=hlLevel5 start="`\=\[" end="\]" matchgroup=replPrompt end="^\S\+>"me=s-1,re=s-1 contains=TOP,lispParen3,lispParen4,lispParen5,lispParen6,lispParen7,lispParen8,NoInParens
syn region lispParen4 matchgroup=hlLevel4 start="`\=\[" end="\]" matchgroup=replPrompt end="^\S\+>"me=s-1,re=s-1 contains=TOP,lispParen4,lispParen5,lispParen6,lispParen7,lispParen8,NoInParens
syn region lispParen5 matchgroup=hlLevel3 start="`\=\[" end="\]" matchgroup=replPrompt end="^\S\+>"me=s-1,re=s-1 contains=TOP,lispParen5,lispParen6,lispParen7,lispParen8,NoInParens
syn region lispParen6 matchgroup=hlLevel2 start="`\=\[" end="\]" matchgroup=replPrompt end="^\S\+>"me=s-1,re=s-1 contains=TOP,lispParen6,lispParen7,lispParen8,NoInParens
syn region lispParen7 matchgroup=hlLevel1 start="`\=\[" end="\]" matchgroup=replPrompt end="^\S\+>"me=s-1,re=s-1 contains=TOP,lispParen7,lispParen8,NoInParens
syn region lispParen8 matchgroup=hlLevel0 start="`\=\[" end="\]" matchgroup=replPrompt end="^\S\+>"me=s-1,re=s-1 contains=TOP,lispParen8,NoInParens
syn region lispParen9 matchgroup=hlLevel9 start="`\={" matchgroup=hlLevel9 end="}" matchgroup=replPrompt end="^\S\+>" contains=TOP,@Spell
syn region lispParen0 matchgroup=hlLevel8 start="`\={" end="}" matchgroup=replPrompt end="^\S\+>"me=s-1,re=s-1 contains=TOP,lispParen0,lispParen1,lispParen2,lispParen3,lispParen4,lispParen5,lispParen6,lispParen7,lispParen8,NoInParens
syn region lispParen1 matchgroup=hlLevel7 start="`\={" end="}" matchgroup=replPrompt end="^\S\+>"me=s-1,re=s-1 contains=TOP,lispParen1,lispParen2,lispParen3,lispParen4,lispParen5,lispParen6,lispParen7,lispParen8,NoInParens
syn region lispParen2 matchgroup=hlLevel6 start="`\={" end="}" matchgroup=replPrompt end="^\S\+>"me=s-1,re=s-1 contains=TOP,lispParen2,lispParen3,lispParen4,lispParen5,lispParen6,lispParen7,lispParen8,NoInParens
syn region lispParen3 matchgroup=hlLevel5 start="`\={" end="}" matchgroup=replPrompt end="^\S\+>"me=s-1,re=s-1 contains=TOP,lispParen3,lispParen4,lispParen5,lispParen6,lispParen7,lispParen8,NoInParens
syn region lispParen4 matchgroup=hlLevel4 start="`\={" end="}" matchgroup=replPrompt end="^\S\+>"me=s-1,re=s-1 contains=TOP,lispParen4,lispParen5,lispParen6,lispParen7,lispParen8,NoInParens
syn region lispParen5 matchgroup=hlLevel3 start="`\={" end="}" matchgroup=replPrompt end="^\S\+>"me=s-1,re=s-1 contains=TOP,lispParen5,lispParen6,lispParen7,lispParen8,NoInParens
syn region lispParen6 matchgroup=hlLevel2 start="`\={" end="}" matchgroup=replPrompt end="^\S\+>"me=s-1,re=s-1 contains=TOP,lispParen6,lispParen7,lispParen8,NoInParens
syn region lispParen7 matchgroup=hlLevel1 start="`\={" end="}" matchgroup=replPrompt end="^\S\+>"me=s-1,re=s-1 contains=TOP,lispParen7,lispParen8,NoInParens
syn region lispParen8 matchgroup=hlLevel0 start="`\={" end="}" matchgroup=replPrompt end="^\S\+>"me=s-1,re=s-1 contains=TOP,lispParen8,NoInParens
else
syn region lispParen0 matchgroup=hlLevel0 start="`\=(" skip="|.\{-}|" end=")" matchgroup=replPrompt end="^\S\+>" contains=@replListCluster,lispParen1,replPrompt
syn region lispParen1 contained matchgroup=hlLevel1 start="`\=(" skip="|.\{-}|" end=")" matchgroup=replPrompt end="^\S\+>"me=s-1,re=s-1 contains=@replListCluster,lispParen2
syn region lispParen2 contained matchgroup=hlLevel2 start="`\=(" skip="|.\{-}|" end=")" matchgroup=replPrompt end="^\S\+>"me=s-1,re=s-1 contains=@replListCluster,lispParen3
syn region lispParen3 contained matchgroup=hlLevel3 start="`\=(" skip="|.\{-}|" end=")" matchgroup=replPrompt end="^\S\+>"me=s-1,re=s-1 contains=@replListCluster,lispParen4
syn region lispParen4 contained matchgroup=hlLevel4 start="`\=(" skip="|.\{-}|" end=")" matchgroup=replPrompt end="^\S\+>"me=s-1,re=s-1 contains=@replListCluster,lispParen5
syn region lispParen5 contained matchgroup=hlLevel5 start="`\=(" skip="|.\{-}|" end=")" matchgroup=replPrompt end="^\S\+>"me=s-1,re=s-1 contains=@replListCluster,lispParen6
syn region lispParen6 contained matchgroup=hlLevel6 start="`\=(" skip="|.\{-}|" end=")" matchgroup=replPrompt end="^\S\+>"me=s-1,re=s-1 contains=@replListCluster,lispParen7
syn region lispParen7 contained matchgroup=hlLevel7 start="`\=(" skip="|.\{-}|" end=")" matchgroup=replPrompt end="^\S\+>"me=s-1,re=s-1 contains=@replListCluster,lispParen8
syn region lispParen8 contained matchgroup=hlLevel8 start="`\=(" skip="|.\{-}|" end=")" matchgroup=replPrompt end="^\S\+>"me=s-1,re=s-1 contains=@replListCluster,lispParen9
syn region lispParen9 contained matchgroup=hlLevel9 start="`\=(" skip="|.\{-}|" end=")" matchgroup=replPrompt end="^\S\+>"me=s-1,re=s-1 contains=@replListCluster,lispParen0
endif
else
if SlimvGetFiletype() !~ '.*clojure.*'
syn region lispList matchgroup=Delimiter start="(" skip="|.\{-}|" end=")" matchgroup=replPrompt end="^\S\+>" contains=@replListCluster
syn region lispBQList matchgroup=PreProc start="`(" skip="|.\{-}|" end=")" matchgroup=replPrompt end="^\S\+>" contains=@replListCluster
endif
endif
syn match replPrompt /^[^(]\S\+>/
syn match replPrompt /^(\S\+)>/
hi def link replPrompt Type
endfunction
" Open a new REPL buffer
function! SlimvOpenReplBuffer()
call SlimvOpenBuffer( g:slimv_repl_name )
setlocal noreadonly
let s:repl_buf = bufnr( "%" )
let b:slimv_repl_buffer = 1
call SlimvInitRepl()
if g:slimv_repl_syntax
call SlimvSetSyntaxRepl()
else
set syntax=
endif
" Prompt and its line and column number in the REPL buffer
if !exists( 'b:repl_prompt' )
let b:repl_prompt = ''
let b:repl_prompt_line = 1
let b:repl_prompt_col = 1
endif
" Add keybindings valid only for the REPL buffer
inoremap <buffer> <silent> <C-CR> <End><C-O>:call SlimvSendCommand(1)<CR><End>
inoremap <buffer> <silent> <C-C> <C-O>:call SlimvInterrupt()<CR>
inoremap <buffer> <silent> <expr> <C-W> SlimvHandleCW()
if g:slimv_repl_simple_eval
inoremap <buffer> <silent> <CR> <C-R>=pumvisible() ? "\<lt>C-Y>" : "\<lt>End>\<lt>C-O>:call SlimvSendCommand(0)\<lt>CR>\<lt>End>"<CR>
inoremap <buffer> <silent> <Up> <C-R>=pumvisible() ? "\<lt>Up>" : SlimvHandleUp()<CR>
inoremap <buffer> <silent> <Down> <C-R>=pumvisible() ? "\<lt>Down>" : SlimvHandleDown()<CR>
else
inoremap <buffer> <silent> <CR> <C-R>=pumvisible() ? "\<lt>C-Y>" : SlimvHandleEnterRepl()<CR><C-R>=SlimvArglistOnEnter()<CR>
inoremap <buffer> <silent> <C-Up> <C-R>=pumvisible() ? "\<lt>Up>" : SlimvHandleUp()<CR>
inoremap <buffer> <silent> <C-Down> <C-R>=pumvisible() ? "\<lt>Down>" : SlimvHandleDown()<CR>
endif
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
augroup SlimvReplAutoCmd
au!
" 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()"
execute "au BufWinEnter " . g:slimv_repl_name . " :call SlimvMarkBufferEnd(1)"
execute "au TabEnter *" . " :call SlimvReplSetCursorPos(1)"
augroup END
call SlimvRefreshReplBuffer()
endfunction
" Clear the contents of the REPL buffer, keeping the last prompt only
function! SlimvClearReplBuffer()
let this_buf = bufnr( "%" )
if s:repl_buf == -1
call SlimvError( "There is no REPL buffer." )
return
endif
if this_buf != s:repl_buf
let oldpos = winsaveview()
execute "buf " . s:repl_buf
endif
if b:repl_prompt_line > 1
execute "normal! gg0d" . (b:repl_prompt_line-1) . "GG$"
let b:repl_prompt_line = 1
endif
if this_buf != s:repl_buf
execute "buf " . this_buf
call winrestview( oldpos )
endif
endfunction
" Open a new Inspect buffer
function SlimvOpenInspectBuffer()
call SlimvOpenBuffer( g:slimv_inspect_name )
let b:range_start = 0
let b:range_end = 0
let b:help = SlimvHelpInspect()
" Add keybindings valid only for the Inspect buffer
noremap <buffer> <silent> <F1> :call SlimvToggleHelp()<CR>
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(1)<CR>'
if version < 703
" conceal mechanism is defined since Vim 7.3
syn region inspectItem matchgroup=Ignore start="{\[\d\+\]\s*" end="\[]}"
syn region inspectAction matchgroup=Ignore start="{<\d\+>\s*" end="<>}"
else
syn region inspectItem matchgroup=Ignore start="{\[\d\+\]\s*" end="\[]}" concealends
syn region inspectAction matchgroup=Ignore start="{<\d\+>\s*" end="<>}" concealends
setlocal conceallevel=3 concealcursor=nc
endif
hi def link inspectItem Special
hi def link inspectAction String