-
Notifications
You must be signed in to change notification settings - Fork 100
/
quickrun.vim
1156 lines (1072 loc) · 30.3 KB
/
quickrun.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
" Run commands quickly.
" Version: 0.6.0
" Author : thinca <thinca+vim@gmail.com>
" License: zlib License
let s:save_cpo = &cpo
set cpo&vim
let s:V = vital#of('quickrun').load('Data.List', 'System.File', 'System.Filepath')
unlet! g:quickrun#V
let g:quickrun#V = s:V
lockvar! g:quickrun#V
let s:is_win = s:V.is_windows()
" Default config. " {{{1
unlet! g:quickrun#default_config
let g:quickrun#default_config = {
\ '_': {
\ 'outputter': 'buffer',
\ 'runner': 'system',
\ 'cmdopt': '',
\ 'args': '',
\ 'tempfile' : '%{tempname()}',
\ 'exec': '%c %o %s %a',
\ },
\ 'awk': {
\ 'exec': '%c %o -f %s %a',
\ },
\ 'bash': {},
\ 'c': {
\ 'type':
\ s:is_win && executable('cl') ? 'c/vc' :
\ executable('gcc') ? 'c/gcc' :
\ executable('clang') ? 'c/clang' : '',
\ },
\ 'c/C': {
\ 'command': 'C',
\ 'exec': '%c %o -m %s',
\ },
\ 'c/clang': {
\ 'command': 'clang',
\ 'exec': ['%c %o %s -o %s:p:r', '%s:p:r %a'],
\ 'tempfile': '%{tempname()}.c',
\ 'hook/sweep/files': '%S:p:r',
\ },
\ 'c/gcc': {
\ 'command': 'gcc',
\ 'exec': ['%c %o %s -o %s:p:r', '%s:p:r %a'],
\ 'tempfile': '%{tempname()}.c',
\ 'hook/sweep/files': '%S:p:r',
\ },
\ 'c/vc': {
\ 'command': 'cl',
\ 'exec': ['%c %o %s /nologo /Fo%s:p:r.obj /Fe%s:p:r.exe > nul',
\ '%s:p:r.exe %a'],
\ 'tempfile': '%{tempname()}.c',
\ 'hook/sweep/files': ['%S:p:r.exe', '%S:p:r.obj'],
\ },
\ 'cpp': {
\ 'type':
\ s:is_win && executable('cl') ? 'cpp/vc' :
\ executable('clang++') ? 'cpp/clang++' :
\ executable('g++') ? 'cpp/g++' : '',
\ },
\ 'cpp/C': {
\ 'command': 'C',
\ 'exec': '%c %o -p %s',
\ },
\ 'cpp/clang++': {
\ 'command': 'clang++',
\ 'exec': ['%c %o %s -o %s:p:r', '%s:p:r %a'],
\ 'tempfile': '%{tempname()}.cpp',
\ 'hook/sweep/files': ['%S:p:r'],
\ },
\ 'cpp/g++': {
\ 'command': 'g++',
\ 'exec': ['%c %o %s -o %s:p:r', '%s:p:r %a'],
\ 'tempfile': '%{tempname()}.cpp',
\ 'hook/sweep/files': '%S:p:r',
\ },
\ 'cpp/vc': {
\ 'command': 'cl',
\ 'exec': ['%c %o %s /nologo /Fo%s:p:r.obj /Fe%s:p:r.exe > nul',
\ '%s:p:r.exe %a'],
\ 'tempfile': '%{tempname()}.cpp',
\ 'hook/sweep/files': ['%S:p:r.exe', '%S:p:r.obj'],
\ },
\ 'clojure': {
\ 'type': executable('jark') ? 'clojure/jark':
\ executable('clj') ? 'clojure/clj':
\ '',
\ },
\ 'clojure/jark': {
\ 'command': 'jark',
\ 'exec': '%c ns load %s',
\ },
\ 'clojure/clj': {
\ 'command': 'clj',
\ 'exec': '%c %s',
\ },
\ 'coffee': {},
\ 'cs': {
\ 'type': executable('csc') ? 'cs/csc' :
\ executable('dmcs') ? 'cs/dmcs' :
\ executable('smcs') ? 'cs/smcs' :
\ executable('gmcs') ? 'cs/gmcs' :
\ executable('mcs') ? 'cs/mcs' : ''
\ },
\ 'cs/csc': {
\ 'command': 'csc',
\ 'exec': ['%c /nologo /out:%s:p:r:gs?/?\\?.exe %s:gs?/?\\?', '%s:p:r.exe %a'],
\ 'tempfile': '%{tempname()}.cs',
\ 'hook/output_encode/encoding': '&termencoding',
\ 'hook/sweep/files': ['%S:p:r.exe'],
\ },
\ 'cs/mcs': {
\ 'command': 'mcs',
\ 'exec': ['%c %o -out:%s:p:r.exe %s', 'mono %s:p:r.exe %a'],
\ 'tempfile': '%{tempname()}.cs',
\ 'hook/sweep/files': ['%S:p:r.exe'],
\ },
\ 'cs/gmcs': {
\ 'command': 'gmcs',
\ 'exec': ['%c %o -out:%s:p:r.exe %s', 'mono %s:p:r.exe %a'],
\ 'tempfile': '%{tempname()}.cs',
\ 'hook/sweep/files': ['%S:p:r.exe'],
\ },
\ 'cs/smcs': {
\ 'command': 'smcs',
\ 'exec': ['%c %o -out:%s:p:r.exe %s', 'mono %s:p:r.exe %a'],
\ 'tempfile': '%{tempname()}.cs',
\ 'hook/sweep/files': ['%S:p:r.exe'],
\ },
\ 'cs/dmcs': {
\ 'command': 'dmcs',
\ 'exec': ['%c %o -out:%s:p:r.exe %s', 'mono %s:p:r.exe %a'],
\ 'tempfile': '%{tempname()}.cs',
\ 'hook/sweep/files': ['%S:p:r.exe'],
\ },
\ 'd': {
\ 'type':
\ executable('rdmd') ? 'd/rdmd' :
\ executable('ldc') ? 'd/ldc' :
\ executable('gdc') ? 'd/gdc' : '',
\ },
\ 'd/rdmd': {
\ 'command': 'rdmd',
\ 'tempfile': '%{tempname()}.d',
\ },
\ 'd/ldc': {
\ 'command': 'ldc',
\ 'exec': ['%c %o %s -o %s:p:r', '%s:p:r %a'],
\ 'tempfile': '%{tempname()}.d',
\ 'hook/sweep/files': ['%S:p:r'],
\ },
\ 'd/gdc': {
\ 'command': 'gdc',
\ 'exec': ['%c %o %s -o %s:p:r', '%s:p:r %a'],
\ 'tempfile': '%{tempname()}.d',
\ 'hook/sweep/files': ['%S:p:r'],
\ },
\ 'dosbatch': {
\ 'command': '',
\ 'exec': 'call %s %a',
\ 'tempfile': '%{tempname()}.bat',
\ },
\ 'dart': {
\ 'type':
\ executable('dart') ? 'dart/dart/checked':
\ '',
\ },
\ 'dart/dart/checked': {
\ 'command': 'dart',
\ 'cmdopt': '--enable-type-checks',
\ 'tempfile': '%{tempname()}.dart',
\ },
\ 'dart/dart/production': {
\ 'command': 'dart',
\ 'tempfile': '%{tempname()}.dart',
\ },
\ 'erlang': {
\ 'command': 'escript',
\ },
\ 'eruby': {
\ 'command': 'erb',
\ 'exec': '%c %o -T - %s %a',
\ },
\ 'fortran': {
\ 'type': 'fortran/gfortran',
\ },
\ 'fortran/gfortran': {
\ 'command': 'gfortran',
\ 'exec': ['%c %o -o %s:p:r %s', '%s:p:r %a'],
\ 'tempfile': '%{tempname()}.f95',
\ 'hook/sweep/files': ['%S:p:r'],
\ },
\ 'go': {
\ 'type':
\ executable('8g') || executable('6g') || executable('5g') ?
\ $GOARCH ==# '386' ? (s:is_win ? 'go/386/win' : 'go/386'):
\ $GOARCH ==# 'amd64' ? 'go/amd64':
\ $GOARCH ==# 'arm' ? 'go/arm': '' :
\ executable('go') ? (s:is_win ? 'go/go/win' : 'go/go'): '',
\ },
\ 'go/386': {
\ 'exec': ['8g %o -o %s:p:r.8 %s', '8l -o %s:p:r %s:p:r.8', '%s:p:r %a'],
\ 'tempfile': '%{tempname()}.go',
\ 'hook/output_encode/encoding': 'utf-8',
\ 'hook/sweep/files': '%S:p:r',
\ },
\ 'go/386/win': {
\ 'exec': ['8g %o -o %s:p:r.8 %s', '8l -o %s:p:r.exe %s:p:r.8',
\ '%s:p:r.exe %a'],
\ 'tempfile': '%{tempname()}.go',
\ 'hook/output_encode/encoding': 'utf-8',
\ 'hook/sweep/files': '%S:p:r.exe',
\ },
\ 'go/amd64': {
\ 'exec': ['6g %o -o %s:p:r.6 %s', '6l -o %s:p:r %s:p:r.6', '%s:p:r %a'],
\ 'tempfile': '%{tempname()}.go',
\ 'hook/output_encode/encoding': 'utf-8',
\ 'hook/sweep/files': '%S:p:r',
\ },
\ 'go/arm': {
\ 'exec': ['5g %o -o %s:p:r.5 %s', '5l -o %s:p:r %s:p:r.5', '%s:p:r %a'],
\ 'tempfile': '%{tempname()}.go',
\ 'hook/output_encode/encoding': 'utf-8',
\ 'hook/sweep/files': '%S:p:r',
\ },
\ 'go/go': {
\ 'exec': ['cd %s:p:h \&\& go run %s:p:t'],
\ 'tempfile': '%{tempname()}.go',
\ 'output_encode': 'utf-8',
\ },
\ 'go/go/win': {
\ 'exec': ['cmd /c (cd %s:p:h ^\& go run %s:p:t)'],
\ 'tempfile': '%{tempname()}.go',
\ 'output_encode': 'utf-8',
\ },
\ 'groovy': {
\ 'cmdopt': '-c %{&fenc==#""?&enc:&fenc}'
\ },
\ 'haskell': {'type': 'haskell/runghc'},
\ 'haskell/runghc': {
\ 'command': 'runghc',
\ 'tempfile': '%{tempname()}.hs',
\ 'hook/eval/template': 'main = print \$ %s',
\ },
\ 'haskell/ghc': {
\ 'command': 'ghc',
\ 'exec': ['%c %o %s -o %s:p:r', '%s:p:r %a'],
\ 'cmdopt': '-v0 --make',
\ 'tempfile': '%{tempname()}.hs',
\ 'hook/sweep/files': ['%S:p:r', '%S:p:r.o', '%S:p:r.hi'],
\ },
\ 'haskell/ghc/core': {
\ 'command': 'ghc',
\ 'exec': '%c %o -ddump-simpl -dsuppress-coercions %s',
\ 'cmdopt': '-v0 --make',
\ 'tempfile': '%{tempname()}.hs',
\ 'hook/sweep/files': ['%S:p:r', '%S:p:r.o', '%S:p:r.hi'],
\ },
\ 'io': {},
\ 'java': {
\ 'exec': ['javac %o %s', '%c %s:t:r %a'],
\ 'hook/output_encode/encoding': '&termencoding',
\ 'hook/sweep/files': '%S:p:r.class',
\ },
\ 'javascript': {
\ 'type': executable('js') ? 'javascript/spidermonkey':
\ executable('d8') ? 'javascript/v8':
\ executable('node') ? 'javascript/nodejs':
\ executable('phantomjs') ? 'javascript/phantomjs':
\ executable('jrunscript') ? 'javascript/rhino':
\ executable('cscript') ? 'javascript/cscript': '',
\ },
\ 'javascript/cscript': {
\ 'command': 'cscript',
\ 'exec': '%c //e:jscript %o %s %a',
\ 'cmdopt': '//Nologo',
\ 'tempfile': '%{tempname()}.js',
\ },
\ 'javascript/rhino': {
\ 'command': 'jrunscript',
\ 'tempfile': '%{tempname()}.js',
\ },
\ 'javascript/spidermonkey': {
\ 'command': 'js',
\ 'tempfile': '%{tempname()}.js',
\ },
\ 'javascript/v8': {
\ 'command': 'd8',
\ 'tempfile': '%{tempname()}.js',
\ },
\ 'javascript/nodejs': {
\ 'command': 'node',
\ 'tempfile': '%{tempname()}.js',
\ },
\ 'javascript/phantomjs': {
\ 'command': 'phantomjs',
\ 'tempfile': '%{tempname()}.js',
\ },
\ 'jsx': {
\ 'exec': '%c --run %o %s %a',
\ 'hook/eval/template':
\ 'class _Main { static function main(args : string[]) :void { %s }}',
\ },
\ 'lisp': {
\ 'command': 'clisp',
\ },
\ 'llvm': {
\ 'command': 'llvm-as %s -o=- | lli - %a',
\ },
\ 'lua': {},
\ 'markdown': {
\ 'type': executable('Markdown.pl') ? 'markdown/Markdown.pl':
\ executable('kramdown') ? 'markdown/kramdown':
\ executable('bluecloth') ? 'markdown/bluecloth':
\ executable('redcarpet') ? 'markdown/redcarpet':
\ executable('pandoc') ? 'markdown/pandoc': '',
\ },
\ 'markdown/Markdown.pl': {
\ 'command': 'Markdown.pl',
\ },
\ 'markdown/bluecloth': {
\ 'command': 'bluecloth',
\ 'cmdopt': '-f',
\ },
\ 'markdown/kramdown': {
\ 'command': 'kramdown',
\ },
\ 'markdown/pandoc': {
\ 'command': 'pandoc',
\ 'exec': '%c --from=markdown --to=html %o %s %a',
\ },
\ 'markdown/redcarpet': {
\ 'command': 'redcarpet',
\ },
\ 'ocaml': {},
\ 'perl': {
\ 'hook/eval/template': join([
\ 'use Data::Dumper',
\ '\$Data::Dumper::Terse = 1',
\ '\$Data::Dumper::Indent = 0',
\ 'print Dumper eval{%s}'], ';')
\ },
\ 'perl6': {'hook/eval/template': '{%s}().perl.print'},
\ 'python': {'hook/eval/template': 'print(%s)'},
\ 'php': {},
\ 'r': {
\ 'command': 'R',
\ 'exec': '%c %o --no-save --slave %a < %s',
\ },
\ 'ruby': {'hook/eval/template': " p proc {\n%s\n}.call"},
\ 'rust': {
\ 'command': 'rustc',
\ 'exec': ['%c %o %s -o %s:p:r', '%s:p:r %a'],
\ 'tempfile': '%{tempname()}.rs',
\ 'hook/sweep/files': '%S:p:r',
\ },
\ 'scala': {
\ 'cmdopt': '-Dfile.encoding=' . &termencoding,
\ 'hook/output_encode/encoding': '&termencoding',
\ },
\ 'scheme': {
\ 'type': executable('gosh') ? 'scheme/gauche':
\ executable('mzscheme') ? 'scheme/mzscheme': '',
\ },
\ 'scheme/gauche': {
\ 'command': 'gosh',
\ 'exec': '%c %o %s:p %a',
\ 'hook/eval/template': '(display (begin %s))',
\ },
\ 'scheme/mzscheme': {
\ 'command': 'mzscheme',
\ 'exec': '%c %o -f %s %a',
\ },
\ 'sed': {},
\ 'sh': {},
\ 'typescript': {
\ 'command': 'tsc',
\ 'cmdopt': '--exec',
\ 'tempfile': '%{tempname()}.ts',
\ },
\ 'vim': {
\ 'command': ':source',
\ 'exec': '%C %s',
\ 'hook/eval/template': "echo %s",
\ 'runner': 'vimscript',
\ },
\ 'wsh': {
\ 'command': 'cscript',
\ 'cmdopt': '//Nologo',
\ 'hook/output_encode/encoding': '&termencoding',
\ },
\ 'zsh': {},
\}
lockvar! g:quickrun#default_config
let s:Session = {} " {{{1
" Initialize of instance.
function! s:Session.initialize(config)
let self.base_config = s:build_config(a:config)
endfunction
" The option is appropriately set referring to default options.
function! s:Session.normalize(config)
let config = a:config
if has_key(config, 'input')
let input = quickrun#expand(config.input)
try
let config.input = input[0] ==# '=' ? input[1:]
\ : join(readfile(input, 'b'), "\n")
catch
throw 'quickrun: Can not treat input: ' . v:exception
endtry
else
let config.input = ''
endif
let config.command = get(config, 'command', config.type)
if has_key(config, 'srcfile')
let config.srcfile = quickrun#expand(expand(config.srcfile))
elseif !has_key(config, 'src')
if filereadable(expand('%:p')) &&
\ !has_key(config, 'region') && !&modified
" Use file in direct.
let config.srcfile = expand('%:p')
else
let config.region = get(config, 'region', {
\ 'first': [1, 0, 0],
\ 'last': [line('$'), 0, 0],
\ 'wise': 'V',
\ })
" Executes on the temporary file.
let body = s:get_region(config.region)
let body = s:V.iconv(body, &encoding, &fileencoding)
if &l:fileformat ==# 'mac'
let body = substitute(body, "\n", "\r", 'g')
elseif &l:fileformat ==# 'dos'
if !&l:binary
let body .= "\n"
endif
let body = substitute(body, "\n", "\r\n", 'g')
endif
let config.src = body
endif
endif
if !has_key(config, 'srcfile')
let fname = quickrun#expand(config.tempfile)
call self.tempname(fname)
call writefile(split(config.src, "\n", 1), fname, 'b')
let config.srcfile = fname
endif
for opt in ['cmdopt', 'args']
let config[opt] = quickrun#expand(config[opt])
endfor
return config
endfunction
function! s:Session.setup()
try
if has_key(self, 'exit_code')
call remove(self, 'exit_code')
endif
let self.config = deepcopy(self.base_config)
let self.hooks = map(quickrun#module#get('hook'),
\ 'self.make_module("hook", v:val.name)')
call self.invoke_hook('hook_loaded')
call filter(self.hooks, 'v:val.config.enable')
let self.config = self.normalize(self.config)
call self.invoke_hook('normalized')
let self.runner = self.make_module('runner', self.config.runner)
let self.outputter = self.make_module('outputter', self.config.outputter)
call self.invoke_hook('module_loaded')
let exec = get(self.config, 'exec', '')
let commands = type(exec) == type([]) ? copy(exec) : [exec]
call filter(map(commands, 'self.build_command(v:val)'),
\ 'v:val =~# "\\S"')
let self.commands = commands
catch /^quickrun:/
call self.sweep()
throw v:exception
catch
call self.sweep()
throw join(['quickrun: Error occurred in setup():',
\ v:exception, v:throwpoint], "\n")
endtry
endfunction
function! s:Session.make_module(kind, line)
let name = ''
if type(a:line) == type([]) && !empty([])
let [name; args] = a:line
elseif a:line =~# '^\w'
let [name, arg] = split(a:line, '^\w\+\zs', 1)
let args = [arg]
endif
let module = deepcopy(quickrun#module#get(a:kind, name))
try
call module.validate()
catch
let exception = matchstr(v:exception, '^\%(quickrun:\s*\)\?\zs.*')
throw printf('quickrun: Specified %s is not available: %s: %s',
\ a:kind, name, exception)
endtry
try
call s:build_module(module, [self.config] + args)
call map(module.config, 'quickrun#expand(v:val)')
call module.init(self)
catch
let exception = matchstr(v:exception, '^\%(quickrun:\s*\)\?\zs.*')
throw printf('quickrun: %s/%s: %s',
\ a:kind, name, exception)
endtry
return module
endfunction
function! s:Session.run()
if has_key(self, '_running')
throw 'quickrun: session.run() was called in running.'
endif
let self._running = 1
call self.setup()
call self.invoke_hook('ready')
let exit_code = 1
try
call self.outputter.start(self)
let exit_code = self.runner.run(self.commands, self.config.input, self)
finally
if !has_key(self, '_continue_key')
call self.finish(exit_code)
endif
endtry
endfunction
function! s:Session.continue()
let self._continue_key = s:save_session(self)
return self._continue_key
endfunction
function! s:Session.output(data)
let context = {'data': a:data}
call self.invoke_hook('output', context)
if context.data !=# ''
call self.outputter.output(context.data, self)
endif
endfunction
function! s:Session.finish(...)
if !has_key(self, 'exit_code')
let self.exit_code = a:0 ? a:1 : 0
if self.exit_code == 0
call self.invoke_hook('success')
else
call self.invoke_hook('failure', {'exit_code': self.exit_code})
endif
call self.invoke_hook('finish')
call self.outputter.finish(self)
call self.sweep()
call self.invoke_hook('exit')
endif
endfunction
" Build a command to execute it from options.
" XXX: Undocumented yet. This is used by core modules only.
function! s:Session.build_command(tmpl)
let config = self.config
let command = config.command
let rule = {
\ 'c': command,
\ 's': config.srcfile,
\ 'o': config.cmdopt,
\ 'a': config.args,
\ '%': '%',
\}
let rest = a:tmpl
let result = ''
while 1
let pos = match(rest, '%')
if pos < 0
let result .= rest
break
elseif pos != 0
let result .= rest[: pos - 1]
let rest = rest[pos :]
endif
let symbol = rest[1]
let value = get(rule, tolower(symbol), '')
if symbol ==? 'c' && value ==# ''
throw 'quickrun: "command" option is empty.'
endif
let rest = rest[2 :]
if symbol =~? '^[cs]$'
if symbol ==# 'c'
let value_ = s:V.System.Filepath.which(value)
if value_ !=# ''
let value = value_
endif
endif
let mod = matchstr(rest, '^\v\zs%(\:[p8~.htre]|\:g?s(.).{-}\1.{-}\1)*')
let value = fnamemodify(value, mod)
if symbol =~# '\U'
let value = command =~# '^\s*:' ? fnameescape(value)
\ : self.runner.shellescape(value)
endif
let value = escape(value, '\@&$%')
let rest = rest[len(mod) :]
endif
let result .= value
endwhile
return substitute(quickrun#expand(result), '[\r\n]\+', ' ', 'g')
endfunction
function! s:Session.tempname(...)
let name = a:0 ? a:1 : tempname()
if !has_key(self, '_temp_names')
let self._temp_names = []
endif
call add(self._temp_names, name)
return name
endfunction
" Sweep the session.
function! s:Session.sweep()
" Remove temporary files.
if has_key(self, '_temp_names')
for name in self._temp_names
if filewritable(name)
call delete(name)
elseif isdirectory(name)
call s:V.System.File.rmdir(name)
endif
endfor
endif
" Sweep the execution of vimproc.
if has_key(self, '_vimproc')
try
call self._vimproc.kill(15)
call self._vimproc.waitpid()
catch
endtry
call remove(self, '_vimproc')
endif
if has_key(self, '_continue_key')
if has_key(s:sessions, self._continue_key)
call remove(s:sessions, self._continue_key)
endif
call remove(self, '_continue_key')
endif
if has_key(self, 'runner')
call self.runner.sweep()
endif
if has_key(self, 'outputter')
call self.outputter.sweep()
endif
if has_key(self, 'hooks')
for hook in self.hooks
call hook.sweep()
endfor
endif
if has_key(self, '_running')
call remove(self, '_running')
endif
endfunction
function! s:Session.invoke_hook(point, ...)
let context = a:0 ? a:1 : {}
let func = 'on_' . a:point
let pri = printf('v:val.priority(%s) - 0', string(a:point))
for hook in s:V.Data.List.sort_by(self.hooks, pri)
if has_key(hook, func) && s:V.is_funcref(hook[func])
call call(hook[func], [self, context], hook)
endif
endfor
endfunction
let s:sessions = {} " Store for sessions.
function! s:save_session(session)
let key = has('reltime') ? reltimestr(reltime()) : string(localtime())
let s:sessions[key] = a:session
return key
endfunction
" Call a function of a session by key.
function! quickrun#session(key, ...)
let session = get(s:sessions, a:key, {})
if a:0 && !empty(session)
return call(session[a:1], a:000[1 :], session)
endif
return session
endfunction
function! s:dispose_session(key)
if has_key(s:sessions, a:key)
let session = remove(s:sessions, a:key)
call session.sweep()
endif
endfunction
function! quickrun#sweep_sessions()
call map(keys(s:sessions), 's:dispose_session(v:val)')
endfunction
function! quickrun#is_running()
return !empty(s:sessions)
endfunction
" Interfaces. {{{1
function! quickrun#new(...)
let session = copy(s:Session)
call session.initialize(a:0 ? a:1 : {})
return session
endfunction
function! quickrun#run(...)
call quickrun#sweep_sessions()
let session = quickrun#new(a:0 ? a:1 : {})
" for debug
if has_key(session.base_config, 'debug')
let g:{matchstr(session.base_config.debug, '\h\w*')} = session
endif
call session.run()
endfunction
" function for |g@|.
function! quickrun#operator(wise)
let wise = {
\ 'line': 'V',
\ 'char': 'v',
\ 'block': "\<C-v>" }[a:wise]
call quickrun#run({'region': {
\ 'first': getpos("'[")[1 :],
\ 'last': getpos("']")[1 :],
\ 'wise': wise,
\ 'selection': 'inclusive',
\ }})
endfunction
" function for main command.
function! quickrun#command(config, use_range, line1, line2)
try
let config = {}
if a:use_range
let config.region = {
\ 'first': [a:line1, 0, 0],
\ 'last': [a:line2, 0, 0],
\ 'wise': 'V',
\ }
endif
call quickrun#run([config, a:config])
catch /^quickrun:/
call s:V.print_error(v:exception)
endtry
endfunction
" completion function for main command.
function! quickrun#complete(lead, cmd, pos)
let line = split(a:cmd[:a:pos - 1], '', 1)
let head = line[-1]
let kinds = quickrun#module#get_kinds()
if 2 <= len(line) && line[-2] =~# '^-'
" a value of option.
let opt = line[-2][1:]
if opt !=# 'type'
let list = []
if opt ==# 'mode'
let list = ['n', 'v']
elseif 0 <= index(kinds, opt)
let list = map(filter(quickrun#module#get(opt),
\ 'v:val.available()'), 'v:val.name')
endif
return filter(list, 'v:val =~# "^".a:lead')
endif
elseif head =~# '^-'
" a name of option.
let list = ['type', 'src', 'srcfile', 'input', 'runner', 'outputter',
\ 'command', 'exec', 'cmdopt', 'args', 'tempfile', 'mode']
let mod_options = {}
for kind in kinds
for module in filter(quickrun#module#get(kind), 'v:val.available()')
for opt in keys(module.config)
let mod_options[opt] = 1
let mod_options[kind . '/' . opt] = 1
let mod_options[module.name . '/' . opt] = 1
let mod_options[kind . '/' . module.name . '/' . opt] = 1
endfor
endfor
endfor
let list += keys(mod_options)
call map(list, '"-" . v:val')
endif
if !exists('list')
" no context: types
let list = keys(extend(exists('g:quickrun_config') ?
\ copy(g:quickrun_config) : {}, g:quickrun#default_config))
call filter(list, 'v:val !~# "^[_*]$"')
endif
let re = '^\V' . escape(head, '\') . '\v[^/]*/?'
return s:V.Data.List.uniq(sort(map(list, 'matchstr(v:val, re)')))
endfunction
" Expand the keyword.
" - @register @{register}
" - &option &{option}
" - $ENV_NAME ${ENV_NAME}
" - %{expr}
" Escape by \ if you does not want to expand.
function! quickrun#expand(input)
if type(a:input) == type([]) || type(a:input) == type({})
return map(copy(a:input), 'quickrun#expand(v:val)')
elseif type(a:input) != type('')
return a:input
endif
let i = 0
let rest = a:input
let result = ''
while 1
let f = match(rest, '\\\?[@&$%\\]')
if f < 0
let result .= rest
break
endif
if f != 0
let result .= rest[: f - 1]
let rest = rest[f :]
endif
if rest[0] ==# '\'
let result .= rest[1] =~# '[@&$%\\]' ? rest[1] : rest[0 : 1]
let rest = rest[2 :]
else
if rest =~# '^[@&$]{'
let rest = '%{' . rest[0] . rest[2 :]
endif
if rest[0] ==# '@'
let e = 2
let expr = rest[0 : 1]
elseif rest =~# '^[&$]'
let e = matchend(rest, '.\w\+')
let expr = rest[: e - 1]
elseif rest =~# '^%{'
let e = matchend(rest, '\\\@<!}')
let expr = substitute(rest[2 : e - 2], '\\}', '}', 'g')
else
let e = 1
let expr = string(rest[0])
endif
if e < 0
break
endif
try
let result .= eval(expr)
catch
endtry
let rest = rest[e :]
endif
endwhile
return result
endfunction
" Execute commands by expr. This is used by remote_expr()
function! quickrun#execute(cmd)
let result = ''
let temp = tempname()
try
let save_vfile = &verbosefile
let &verbosefile = temp
for cmd in type(a:cmd) == type([]) ? a:cmd : [a:cmd]
silent execute cmd
endfor
finally
if &verbosefile ==# temp
let &verbosefile = save_vfile
let result = join(readfile(temp, 'b'), "\n")
endif
call delete(temp)
endtry
return result
endfunction
" Converts a string as argline or a list of config to config object.
function! quickrun#config(config)
if type(a:config) == type('')
return s:build_config_from_arglist(s:parse_argline(a:config))
elseif type(a:config) == type([])
let config = {}
for c in a:config
call extend(config, quickrun#config(c))
unlet c
endfor
return config
endif
return a:config
endfunction
" Misc functions. {{{1
function! s:parse_argline(argline)
" foo 'bar buz' "hoge \"huga"
" => ['foo', 'bar buz', 'hoge "huga']
" TODO: More improve.
" ex:
" foo ba'r b'uz "hoge \nhuga"
" => ['foo, 'bar buz', "hoge \nhuga"]
let argline = a:argline
let arglist = []
while argline !~# '^\s*$'
let argline = matchstr(argline, '^\s*\zs.*$')
if argline[0] =~# '[''"]'
let arg = matchstr(argline, '\v([''"])\zs.{-}\ze\\@<!\1')
let argline = argline[strlen(arg) + 2 :]
else
let arg = matchstr(argline, '\S\+')
let argline = argline[strlen(arg) :]
endif
let arg = substitute(arg, '\\\(.\)', '\1', 'g')
call add(arglist, arg)
endwhile
return arglist
endfunction
function! s:build_config_from_arglist(arglist)
let config = {}
let option = ''
for arg in a:arglist
if option !=# ''
if has_key(config, option)
if type(config[option]) == type([])
call add(config[option], arg)
else
let newarg = [config[option], arg]
unlet config[option]
let config[option] = newarg
endif
else
let config[option] = arg
endif
let option = ''
elseif arg[0] ==# '-'
let option = arg[1:]
elseif arg[0] ==# '>'
if arg[1] ==# '>'
let config.append = 1
let arg = arg[1:]
endif
let config.outputter = arg[1:]
elseif arg[0] ==# '<'
let config.input = arg[1:]
else
let config.type = arg
endif
endfor
return config
endfunction
function! s:build_config(config)
let config = quickrun#config(a:config)
if !has_key(config, 'mode')
let config.mode = histget(':') =~# "^'<,'>\\s*Q\\%[uickRun]" ? 'v' : 'n'
endif
if config.mode ==# 'v'
let config.region = {
\ 'first': getpos("'<")[1 :],
\ 'last': getpos("'>")[1 :],