-
Notifications
You must be signed in to change notification settings - Fork 0
/
.vimrc
2444 lines (1836 loc) · 64.7 KB
/
.vimrc
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
" Has this already been loaded?
"
if exists("loaded_vc_like_mappings")
finish
endif
let loaded_vc_like_mappings=1
"============================================================
"Change of common settings
"============================================================
"turn of vi compatibility mode
set nocompatible
"===
"get in vundle
"===
filetype off " required
" set the runtime path to include Vundle and initialize
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
" alternatively, pass a path where Vundle should install plugins
"call vundle#begin('~/some/path/here')
" let Vundle manage Vundle, required
Plugin 'VundleVim/Vundle.vim'
Plugin 'jeetsukumaran/vim-buffergator'
Plugin 'git://github.com/ycm-core/YouCompleteMe'
" All of your Plugins must be added before the following line
call vundle#end() " required
filetype plugin indent on " required
" To ignore plugin indent changes, instead use:
"filetype plugin on
"
" Brief help
" :PluginList - lists configured plugins
" :PluginInstall - installs plugins; append `!` to update or just :PluginUpdate
" :PluginSearch foo - searches for foo; append `!` to refresh local cache
" :PluginClean - confirms removal of unused plugins; append `!` to auto-approve removal
"
" see :h vundle for more details or wiki for FAQ
" Put your non-Plugin stuff after this line
"
filetype on
"===
"eof vundle
"===
"turn off vi startup screen (?)
"set shortmess+=I
set shortmess=I " Read :help shortmess for everything else.
"next line will causes colors to look like good ol' norton editor
"blue background is making people friendlier and better. Serious.
"colorscheme wildcharm
"colorscheme blue
"colorscheme evening
"colorscheme morning
"Looks better on the mac...
colorscheme desert
"show current file name in title bar
set title
" underline the current line when in insert mode. (some like that)
":autocmd InsertEnter * set cul
":autocmd InsertLeave * set nocul
" I like to have a special underscore cusor in insert mode. (works with gnome)
:autocmd InsertEnter * :SetCursorEdit
:autocmd InsertLeave * :SetCursorNormal
"
"preserve indentation level if you press enter - start of line
"is now indented just as the previous line.
"amazing that this is _not_ the default behavior.
set autoindent
"set spaces to local coding convention
set tabstop=4 " width that a <TAB> character displays as
" religious issue of tabs vs spaces. each project seems to have an opinion...
"set expandtab " convert <TAB> key-presses to spaces
set shiftwidth=4 " number of spaces to use for each step of (auto)indent
set softtabstop=4 " backspace after pressing <TAB> will remove up to this many spaces
" don't keep .*.swp? lock files. (they never help me, and seem to mess things up more then they help)
set nobackup
set nowritebackup
set noswapfile
"turn off bells on errors (like moving the cursor out of range)
set vb t_vb=
" want to stuff paste in. (problem: when paste mode is on then can't remap in
" insert and command mode; problem to turn it on by default)
"set paste
set nopaste
function! StatusLineGitBranch()
"let s:branch_name = trim(system("git branch --show-current 2>/dev/null"))
"let s:branch_name = trim(system("git rev-parse --abbrev-ref HEAD 2>/dev/null"))
let s:branch_name = trim(system("git symbolic-ref --short HEAD"))
if s:branch_name == ""
return ""
endif
return '[' . s:branch_name . ']'
endfunction
" sometimes any attempt to get the branch name is getting too slow for vim.
" in this event vim shows garbage characters, while moving around the file!
" that's a very nasty thing to do...
" (Somehow i only get this on windows, while running under wsl)
"set statusline=%f\ %h%w%m%r\ %=%(%l,%c%V\ %=\ %P%)\ %{StatusLineGitBranch()}
set statusline=%f\ %h%w%m%r\ %=%(%l,%c%V\ %=\ %P%)
"always show status line
set laststatus=2
"show cursor pos in status line
set ruler
"======================================================
" utility functions
"======================================================
function! s:Chomp(string)
return substitute(a:string, '\n\+$', '', '')
endfunction
function! s:BeepNow()
:set novisualbell
:set errorbells
:exe "normal \<Esc>"
endfunction
function! s:GitCheckGitDir()
let s:top_dir = s:Chomp( system("git rev-parse --show-toplevel") )
if v:shell_error != 0
echo "current directory not in a git repository"
return ""
endif
return s:top_dir
endfunction
"======================================================
" navigation keys
"======================================================
":map , <PageUp>
":map . <PageDown>
:vnoremap , :call RunMPGDV()<Return>
:vnoremap . :call RunMPGUV()<Return>
:nnoremap , :call RunMPGD()<Return>
:nnoremap . :call RunMPGU()<Return>
"command! -nargs=* MyPageDown call s:RunMPGD()
"command! -nargs=* MyPageUp call s:RunMPGU()
function! RunMPGDV()
normal gv
call RunMPGD()
endfunction
function! RunMPGD()
let s:pagesize = winheight(0)
let s:filesize = line('$')
let s:topline = line('w0')
let s:move = s:pagesize
if s:topline + s:pagesize > s:filesize
let s:move = s:filesize - s:topline
endif
"execute "normal" . s:move . "j"
" let s:vm = visualmode()
" if s:vm != ""
" let s:pos = getpos("'>")
" let s:pos[1] += s:move
" call setpos("'>", s:pos)
" else
let s:curline = line('.') + s:move
let s:col = col('.')
call setpos(".", [0, s:curline, s:col ] )
" endif
endfunction
function! RunMPGUV()
normal gv
call RunMPGU()
endfunction
function! RunMPGU()
let s:curline = line('.')
let s:pagesize = winheight(0)
let s:topline = line('w0')
let s:move = s:pagesize
if s:curline < s:pagesize
let s:move = s:curline
endif
"execute "normal" . s:pagesize . "k"
"
" let s:vm = visualmode()
" if s:vm != ""
" let s:pos = getpos("'>")
" let s:pos[1] += s:pagesize
" call setpos("'>", s:pos)
" else
let s:curline = s:curline - s:move
let s:col = col('.')
call setpos(".", [0, s:curline, s:col ] )
" endif
endfunction
"======================================================
"search customization
"======================================================
"Case sensitive search
":set noignorecase
"Case insensitive search
:set ignorecase
" highlight all search matches.
:set hlsearch
"======================================================
" enable spell checker for markdown and txt files
"======================================================
"
autocmd BufRead,BufNewFile *.aes setlocal spell
autocmd BufRead,BufNewFile *.md setlocal spell
"autocmd BufRead,BufNewFile *.txt setlocal spell
"======================================================
"key assignments for grep (find in files) script
"======================================================
:inoremap <F3> <Esc>:DoGrep<Return>
:nnoremap <F3> :DoGrep<Return>
:vnoremap <F3> <Esc>:DoGrep<Return>
"======================================================
"key assignments for find file by name script
"======================================================
:nnoremap <F10> :FindFile<Return>
:vnoremap <F10> <Esc>:FindFile<Return>
:inoremap <F10> <Esc>:FindFile<Return>
"======================================================
"key assignments for save and quit
"======================================================
:nnoremap <F12> :SaveAndQuit<Return>
:vnoremap <F12> <Esc>:SaveAndQuit<Return>
:inoremap <F12> <Esc>:SaveAndQuit<Return>
"======================================================
"key assignments for Find
"======================================================
:inoremap <C-F> <Esc>:FindCurrentWord<Return>
:nnoremap <C-F> :FindCurrentWord<Return>
:vnoremap <C-F> <Esc>:FindCurrentWord<Return>
"======================================================
"open Buffergator plugin (fast buffer switching)
"======================================================
if !exists("g:buffergator_viewport_split_policy")
let g:buffergator_viewport_split_policy = "T"
endif
:inoremap <C-B> <Esc>:BuffergatorOpen<Return>
:nnoremap <C-B> :BuffergatorOpen<Return>
:vnoremap <C-B> <Esc>:BuffergatorOpen<Return>
"======================================================
"key assignments for Build/make script
"======================================================
:nnoremap <F5> :Build<Return>
:vnoremap <F5> <Esc>:Build<Return>
:inoremap <F5> <Esc>:Build<Return>
"======================================================
"key assignments for stop Builds cript
"======================================================
:nnoremap <C-F5> :StopBuild<Return>
:vnoremap <C-F5> <Esc>:StopBuild<Return>
:inoremap <C-F5> <Esc>:StopBuild<Return>
"======================================================
"key assignments for show previous build results
"======================================================
"F4 - show compiler errors (in normal mode)
:nnoremap <F4> :PrevBuildResults<Return>
"F4 - show compiler errors (in visual mode)
:vnoremap <F4> <Esc>:PrevBuildResults<Return>
"F4 - show compiler errors (in insert mode)
:inoremap <F4> <Esc>:PrevBuildResults<Return>
"======================================================
"key assignments to add timestamped entry
"======================================================
":nnoremap <F12> :Entry<Return>
":vnoremap <F12> <Esc>:Entry<Return>
":inoremap <F12> <Esc>:Entry<Return>
"======================================================
"Load C header file (if current line is #include something)
"======================================================
map <F9> :call LoadHeaderFile( getline( "." ), 0 )<CR>
map <C-F9> :call LoadHeaderFile( getline( "." ), 1 )<CR>
"======================================================
"Display man page on word under the cursor
"======================================================
"map <F2> :FindHelp<CR>
":inoremap <F2> <Esc>:FindHelp<Return>
":vnoremap <F2> <Esc>:FindHelp<Return>
"======================================================
" goto line (prompts for number)
"======================================================
"Ctlr+G - goto line (insert mode)
":inoremap <C-G> <Esc>:
:inoremap <C-G> <Esc>:GotoLine<Return>
"Ctlr+G - goto line (command mode)
":nnoremap <C-G> :
:nnoremap <C-G> :GotoLine<Return>
"Ctlr+G - goto line (visual mode)
":vnoremap <C-G> <Esc>:
:vnoremap <C-G> <Esc>:GotoLine<Return>
"======================================================
"key assignments
"======================================================
" <Ctrl+A> force redraw the screen.
" at some stage you need to force redraw the window; don't know why.
:vnoremap <C-A> <Esc>:redraw!<Return>
:inoremap <C-A> <Esc>:redraw!<Return>i
:nnoremap <C-A> :redraw!<Return>
" <Ctrl+W> save
"
":vnoremap <C-W> <Esc>:silent w!<Return>
"
":inoremap <C-W> <Esc>:silent w!<Return>i
"
"
":nnoremap <C-W>t :silent w!<Return>
"
" old remap
":vnoremap <C-C> y
":vnoremap <C-X> x
":nnoremap <C-V> P
":vnoremap <C-V> P
":inoremap <C-V> <Esc>Pi
"Ctrl+C - copy
:vnoremap <C-C> y:<Esc>:MyCPXAfterYank<Return>
:nnoremap <C-C> :MyCPXCurrentWord<Return>
:inoremap <C-C> <Esc>:MyCPXCurrentWord<Return>i
"Ctrl+X - cut
:vnoremap <C-X> x:<Esc>:MyCPXAfterYank<Return>
:nnoremap <C-X> :MyCPXCurrentWord<Return>diw
:inoremap <C-X> <Esc>:MyCPXCurrentWord<Return>diwi
"Ctrl+V - paste
:vnoremap <C-V> dy:<Esc>:MyCPXPaste<Return>l
:nnoremap <C-V> :MyCPXPaste<Return>l
:inoremap <C-V> <Esc>:MyCPXPaste<Return>li
"Ctrl+R - redo (in insert mode)
:inoremap <C-R> <Esc>:red<Return>i
"Ctrl+U - undo (in normal mode only)
:nnoremap <C-U> :u<Return>
"Ctrl+U - undo (in insert mode - return to normal mode and undo>
:inoremap <C-U> <Esc>:u<Return>i
"F8 - split windows
:nnoremap <F8> :split<Return>
"F8 - split windows (in visual mode)
:vnoremap <F8> <Esc>:split<Return>
"F8 - split windows (in insert mode)
:inoremap <F8> <Esc>:split<Return>i
"Control + cursor key will goto next/previous word
":nnoremap <C-Left> B
":vnoremap <C-Left> B
":inoremap <C-Left> <Esc>Bi
"
":nnoremap <C-Right> W
":vnoremap <C-Right> W
":inoremap <C-Right> <Esc>Wi
"
"
"Shift + cursor key will start selection of text
"results in selection of text.
"
"Problem: some terminal key mapping kill this (like RXVT terminal) XTERM is ok.
"in tmux you need to add the following line:
"set-window-option -g xterm-keys on
:nnoremap <S-Left> v<Left>
:inoremap <S-Left> <Esc>v<Left>
:vnoremap <S-Left> <Left>
:nnoremap <S-Right> v<Right>
:vnoremap <S-Right> <Right>
:inoremap <S-Right> <Esc>v<Right>
:vnoremap <S-Right> <Right>
:nnoremap <S-Up> v<Up>
:inoremap <S-Up> <Esc>v<Up>
:vnoremap <S-Up> <Up>
:nnoremap <S-Down> v<Down>
:inoremap <S-Down> <Esc>v<Down>
:vnoremap <S-Down> <Down>
"Can't override Ctrl+Q and all other combinations are already set
":inoremap <C-A> <Esc>:q<Enter>
"Ctlr+A - quit (in normal mode only)
"Can't override Ctrl+Q and all other combinations are already set
":nnoremap <C-A> :q<Enter>
"Ctlr+A - quit (in visual mode only)
"Can't override Ctrl+Q and all other combinations are already set
":vnoremap <C-A> <Esc>:q<Enter>
"Ctrl+W - save (in insert mode only>
":inoremap <C-W> <Esc>:w<Return>a
"
"Ctrl+W - save (in normal mode only)
":nnoremap <C-W> :w<Return>
"
"ALt+W - save & overwrite read only file (in insert mode only>
":inoremap <A-W> <Esc>:w+<Return>a
"
"Alt+W - save overwrite read only file (in normal mode only)
":nnoremap <A-W> :w+<Return>
"F3 - show next search hit
":nnoremap <F3> /<Return>
"F3 - show next search hit (in visual mode)
":vnoremap <F3> <Esc>/<Return>
"F3 - show next search hit (in insert mode)
":inoremap <F3> <Esc>/<Return>
"---
"
"Tab - indent a block of text (one tab deep)
:nnoremap <Tab> >>
:vnoremap <Tab> >gv
:nnoremap <S-Tab> <<
:vnoremap <S-Tab> <gv
"======================================================
"open quickfix window, and make it a third of the screen
"======================================================
function! s:OpenQuickFix()
let size = &lines
let size = size / 3
let s:ccmdo = 'copen ' . size
execute s:ccmdo
endfunction
command! -nargs=* OpenQuickFix call s:OpenQuickFix()
"F4 - show compiler errors (in normal mode)
":nnoremap <F4> OpenQuickFix<Return>
"F4 - show compiler errors (in visual mode)
":vnoremap <F4> <Esc>:OpenQuickFix<Return>
"F4 - show compiler errors (in insert mode)
":inoremap <F4> <Esc>OpenQuickFix<Return>
"F6 - goto previous compiler error (normal mode)
":nnoremap <F6> :cp<Return>
"F6 - goto previous compiler error (in visual mode)
:vnoremap <F6> <Esc>:cp<Return>
"F6 - goto previous compiler error (in insert mode)
:inoremap <F6> <Esc>:cp<Return>
"F7 - goto next compiler error (normal mode)
:nnoremap <F7> :cn<Return>
"F7 - goto next compiler error (in visual mode)
:vnoremap <F7> <Esc>:cn<Return>
"F7 - goto next< compiler error (in insert mode)
:inoremap <F7> <Esc>:cn<Return>
"Shift-Tab - unindent a block of text (one tab down)
"This one is commented out, Shift-Tab also covers Alt-Tab - then you can' switch
"windows any longer.
":inoremap <S-Tab> <C-O><LT><LT>
":nnoremap <S-Tab> <LT><LT>
":vnoremap <S-Tab> <LT>
"======================================================
" internal: plugin install and exit
"======================================================
command! -nargs=* MyPInstall call s:RunMyPInstall()
function! s:RunMyPInstall()
PluginInstall
execute "q!"
execute "q!"
endfunction
"======================================================
" toggle spelling
"======================================================
command! -nargs=* SpellOff setlocal nospell
command! -nargs=* SpellOn setlocal spell
"======================================================
" Copy and paste
"
" if xsel is installed then copy also puts to x clipboard.
"======================================================
command! -nargs=* MyCPXAfterYank call s:RunMyCPXAfterYank()
command! -nargs=* MyCPXCurrentWord call s:RunMyCPXCurrentWord()
command! -nargs=* MyCPXPaste call s:RunMyCPXPaste()
command! -nargs=* MyCPXPasteWord call s:RunMyCPXPasteWord()
function! s:RunMyCPXAfterYank()
let g:YankedText=getreg("")
if has('macunix')
call system("pbcopy", g:YankedText )
else
call system("xsel -i -b", g:YankedText )
endif
endfunction
function! s:RunMyCPXCurrentWord()
let g:YankedText=expand("<cword>")
if has('macunix')
call system("pbcopy", g:YankedText )
else
call system("xsel -i -b", g:YankedText )
endif
endfunction
function! s:RunMyCPXPaste()
if exists("g:YankedText")
set paste
execute "normal! i" . g:YankedText
set nopaste
endif
endfunction
function! s:RunMyCPXPasteWord()
if exists("g:YankedText")
" in normal mode: delete the current text and put in the yanked text
execute "normal! viwdi" . g:YankedText
endif
endfunction
"======================================================
" paste from x clipboard into vim (without paste)
" set paste doesn't allow to override keys in inserv/visul mode.
" and most installations come without xwindows support.
"
" needs xsel installed
"======================================================
command! -nargs=* Paste call s:RunYpaste()
function! s:RunYpaste()
if has('macunix')
let g:YankedText = system("pbpaste")
else
let g:YankedText = system("xsel -o -b")
endif
if g:YankedText != ""
" in normal mode: delete the current text and put in the yanked text
set paste
execute "normal! i" . g:YankedText
set nopaste
call setreg("", g:YankedText)
endif
endfunction
"======================================================
" List buffers in error window in chose one of them
"======================================================
"command! -nargs=* Cb call s:ChooseBuffer()
"
"
"
"function! s:ChooseBuffer()
"let bufnumber = 1
"let sbuffers = ""
"
"while bufexists( bufnumber )
" let bname = bufname( bufnumber )
" let sbuffers = sbuffers . bufnumber . " " . bname . "\n"
" let bufnumber = bufnumber + 1
"endwhile
"
"
"let outfile = tempname()
"
""shit - this one is disabled for security reasons
"call writefile( sbuffers, outfile )
"
"execute "silent! cgetfile " . outfile
"endfunction
"======================================================
" check if there is a makefile here;
" optionally checks if target exists.
"======================================================
function! s:MakeHasTarget(targetName)
" name of default makefiles:
let s:makefilenames = [ 'GNUmakefile', 'makefile', 'Makefile' ]
for s:item in s:makefilenames
if filereadable(s:item)
if a:targetName == ''
return 1
endif
let s:cmd = "grep -cE '^" . a:targetName . ":'" . s:item
let s:hasTarget=system(s:cmd)
if s:hasTarget != 0
return 1
endif
endif
endfor
return 0
endfunction
"======================================================
"Build script
"======================================================
command! -nargs=* B call s:RunBuild()
command! -nargs=* Build call s:RunBuild()
command! -nargs=* StopBuild call s:StopBuild()
command! -nargs=* PrevBuildResults call s:SetPrevBuildResults()
" bring back the result of the last build (restores quickfix window)
function! s:SetPrevBuildResults()
if exists("g:buildCommandOutput")
" Read the output from the command into the quickfix window
"execute "cfile! " . g:buildCommandOutput
"
let old_efm = &efm
set efm=%f:%l:%m
execute "silent! cgetfile " . g:buildCommandOutput
let &efm = old_efm
" Open the quickfix window
OpenQuickFix
endif
endfunction
function! BuildJobEofCallbackGlobal(job, exit_status)
call delete(g:buildCommandWraper)
" make quickfix window readonly again, now that the build has ended.
let s:quick_fix_buffer = getqflist({'qfbufnr' : 0}).qfbufnr
call setbufvar(s:quick_fix_buffer, "&modifiable", 0)
call setbufvar(s:quick_fix_buffer, "&modified", 0)
unlet g:build_job
" delete previous build results
if !exists("g:buildCommandOutput")
g:buildCommandOutput = tempname()
endif
call writefile( getbufline(s:quick_fix_buffer, 1, "$"), g:buildCommandOutput )
endfunction
function! s:RunBuild()
" save the current file
execute "silent! :w"
" run build command ---
if filereadable("./make_override")
let s:buildcmd = './make_override'
elseif filereadable("./build.gradle")
if filereadable("./gradlew")
let s:buildcmd = "./gradlew build cleanTest test --fail-fast"
else
let s:buildcmd = "gradle cleanTest test --fail-fast"
endif
elseif filereadable("./pom.xml")
let s:buildcmd = 'mvn test'
elseif s:MakeHasTarget('') == 1
let s:buildcmd = "make " . $MAKE_OPT
else
echo "don't know how to build this"
return
endif
let g:buildCommandWraper = tempname()
let g:buildCommandOutput = tempname()
"use files
"let s:wrapper = [ "echo 'Build command: '" . s:buildcmd, "echo ''", s:buildcmd . " 2>&1 | tee " . g:buildCommandOutput ]
" use buffers
let s:wrapper = [ "echo 'Build command: '" . s:buildcmd, "echo ''", s:buildcmd . " 2>&1" ]
call writefile( s:wrapper, g:buildCommandWraper)
OpenQuickFix
"use buffers
let s:quick_fix_buffer = getqflist({'qfbufnr' : 0}).qfbufnr
"must set quickfix window as modifiable, for the duration of the build."(otherwise it can't append anything)
call setbufvar(s:quick_fix_buffer, "&modifiable", 1)
let g:build_job = job_start( [ "/bin/sh", g:buildCommandWraper ], {'out_io' : 'buffer', 'out_buf': s:quick_fix_buffer, 'exit_cb': function('BuildJobEofCallbackGlobal') })
endfunction
function! s:StopBuild()
if exists("g:build_job")
echo "stopping build"
call job_stop(g:build_job)
unlet g:build_job
call delete( g:buildCommandOutput )
unlet g:buildCommandOutput
else
echo "no build running"
endif
endfunction
"function! s:RunOldBuildSynchronously()
"
" " save the current file
" execute "silent! :w"
"
" let tmpfile = tempname()
"
"
" "build and surpresses build status messages.
" "(those are not very informative and may be very very long)
" "Error messages are redirected to temporary file.
"
" if filereadable("./make_override")
" let buildcmd = "./make_override " . $MAKE_OPT . " > " . tmpfile . " 2>&1"
" elseif filereadable("./build.gradle")
" if filereadable("./gradlew")
" let buildcmd = "./gradlew cleanTest test --fail-fast > " . tmpfile . " 2>&1"
" else
" let buildcmd = "gradle cleanTest test --fail-fast > " . tmpfile . " 2>&1"
" endif
" elseif filereadable("./pom.xml")
" let buildcmd = 'mvn test'
" elseif MakeHasTarget('')
" let buildcmd = "make " . $MAKE_OPT . " > " . tmpfile . " 2>&1"
" else
" echo "Error: don't know how to build from current directory"
" return
" endif
"
" "let fname = expand("%")
" "let fnameidx = strridx(fname,".")
" "if fnameidx != -1
" "let ext = fname[ fnameidx : ]
" "if ext == ".pl" || ext == ".perl"
" " let buildcmd = "perl -c " . fname . ' 2>&1 | perl -ne ''$_ =~ s#.*line (\d+).*#' . fname . ':$1: $&#g; print $_;'' | tee ~/uuu >' . tmpfile
" " endif
" "endif
"
" " --- run build command ---
" echo "Running make ... "
"
" let cmd_output = system(buildcmd)
"
" if getfsize(tmpfile) == 0
"
" cclose
" execute "silent! cfile " . tmpfile
" echo "Build failed"
"
" else
" let old_efm = &efm
" set efm=%f:%l:%m
" execute "silent! cfile " . tmpfile
" let &efm = old_efm
"
" OpenQuickFix
" endif
" call delete(tmpfile)
"
" OpenQuickFix
"
"endfunction
"
"======================================================
" pretty print/format the current source file.
"======================================================
command! -nargs=* Format call s:RunFormat()
function! s:RunOnCurrentBuffer(cmd, opts)
if executable(a:cmd)
let s:file = expand('%:p')
if filewritable(s:file)
execute "silent! :w"
let s:cmd = a:cmd . " " . a:opts . " " . s:file
call system( s:cmd )
execute "silent! e ". s:file
else
echo "Error: file " . s:file . " is not writable"
endif
else
echo "Error: " . a:cmd . " program is not in the current path"
endif
endfunction
function! s:RunFormat()
let s:extension = expand('%:e')
" remove trailing spaces, in an case
if s:extension == "go"
echo "formatting go code in current buffer"
if !executable("gofmt")
echo "Error: executable gofmt is not found in current path"
return
endif
call s:RunOnCurrentBuffer("gofmt", "-w")
echo "go code formatted'
elseif s:extension == "c" || s:extension == "cpp" || s:extension == "h" || s:extension == "hpp"
echo "formatting c/c++ code in current buffer"
if !executable("clang-format")
echo "Error: executable clang-format is not found in current path'
return
endif
call s:RunOnCurrentBuffer("clang-format", "-i")
echo "c/c++ code formatted"
elseif s:extension == "py"
echo "formatting python code"
if !executable("black")
echo "Error: executable block is not found in current path"
return
endif
call s:RunOnCurrentBuffer("black","")
echo "python code formatted"
else
echo "for file with extension ". s:extension " in current buffer: tabs to spaces & removing trailing spaces only."
"tabs to spaces
:retab
"remove trailing newlines
:%s/\s\+$//e
:set ff=unix
execute "silent! :w"
echo "command completed"
endif
endfunction
"======================================================
"" check/lint the current source file.
"======================================================
command! -nargs=* Lint call s:RunLint()
function! s:RunLint()
" save the current file
execute "silent! :w"
let s:extension = expand('%:e')
let s:file = expand('%:p')
let s:tmpfile = tempname()
if s:extension == "sh"
execute "silent! :w"
if !executable("shellcheck")
echo "Error: shellcheck is not found in the current path"
return
endif
let s:cmd = "shellcheck -f gcc " . s:file . " > " . s:tmpfile . " 2>&1"
let old_efm = &efm
set efm=%f:%l:%m
elseif s:extension == "py"
if !executable("pylint")
echo "Error: pylint is not found in the current path"
return
endif
" remove trailing whitespaces (tha's one of the issues)
:%s/\s\+$//e
execute "silent! :w"
" enable warnings and errors
let s:cmd = "pylint --disable=C0301 --disable=C0116 --disable=C0115 --disable=C0114 " . s:file . " > " . s:tmpfile . " 2>&1"
"let s:cmd = "pylint --reports=n --output-format=parseable %:p --disable=R,C " . s:file . " > " . s:tmpfile . " 2>&1"
" enable errors only