@wangjun403 分享的一键编译运行的函数写得真心不错
vim吧
全部回复
仅看楼主
level 11
danielhugo 楼主
引用 @wangjun403 (11楼)
分享个一键编译运行的函数
函数很长主要是检测quickfix的输出
没有error和warning直接运行
有warning可以选择继续还是去解决
有error就直接跳出quickfix窗口了
function! CompileFile()
if &filetype == 'c' || &filetype == 'cpp'
if &filetype == 'c' | set makeprg=gcc\ -std=c99\ -Wall\ -Wconversion\ -o\ %<.exe\ %
else | set makeprg=g++\ -o\ %<.exe\ %
endif
silent exe "make"
if getqflist() == [] "compile correct and no warning
let l:flag = 0 | silent exe "ccl" | exe "!%<.exe"
else
for l:inx in getqflist()
for l:val in values(l:inx)
if l:val =~ 'error' | let l:flag = 1 | break
elseif l:val =~ 'warning' | let l:flag = 2
else | let l:flag = 0
endif
endfor
if l:val =~ 'error' | break | endif
endfor
endif
if l:flag == 1| exe "cw"
elseif l:flag == 2
let l:select = input('There are warnings! [r]un or [s]olve? ')
if l:select == 'r' | exe "!%<.exe" | exe "cw"
elseif l:select == 's' | exe "cw"
else | echohl ErrorMsg | echo "input error!"
endif
else | exe "cw"
endif
else
echohl ErrorMsg | echo "This filetype can't be compiled !"
endif
echohl None
endfunction
———————————————————————————
@wangjun403 分享的一键编译运行的函数写得真心不错,值得单发一贴分享,虽然我基本不用一键编译,但也忍不住修改了一下……
2014年12月05日 17点12分 1
level 11
danielhugo 楼主
求指正,修改成这样有没有问题? @VimMSF @wangjun403
if has("win32") || has("win95") || has("win64") || has("win16")
let s:isWindows=1
else
let s:isWindows=0
endif
function! CompileFile()
if expand("%:p:h")!=getcwd()
echohl WarningMsg
\| echo "Fail to make! This file is not in the current directory!"
\| echohl None
return
endif
let sourcefileename=expand("%:t")
if (sourcefileename=="" || (&filetype!="cpp" && &filetype!="c"))
echohl WarningMsg
\| echo "Fail to make! Please select the right file!"
\| echohl None
return
endif
let deletedspacefilename=substitute(sourcefileename,' ','','g')
if strlen(deletedspacefilename)!=strlen(sourcefileename)
echohl WarningMsg
\| echo "Fail to make! Please delete the spaces in the filename!"
\| echohl None
return
endif
if &filetype=="c"
if s:isWindows==1
setlocal makeprg=gcc\ -Wall\ -Wconversion\ -o\ %<.exe\ %
else
setlocal makeprg=gcc\ -Wall\ -Wconversion\ -o\ %<\ %
endif
elseif &filetype=="cpp"
if s:isWindows==1
setlocal makeprg=g++\ -o\ %<.exe\ %
else
setlocal makeprg=g++\ -o\ %<\ %
endif
" elseif &filetype=="cs"
" setlocal makeprg=csc\ \/nologo\ \/out:%<.exe\ %
endif
if(s:isWindows==1)
let outfilename=substitute(sourcefileename,'\(\.[^.]*\)' ,'.exe','g')
let toexename=outfilename
else
let outfilename=substitute(sourcefileename,'\(\.[^.]*\)' ,'','g')
let toexename=outfilename
endif
if filereadable(outfilename)
if(s:isWindows==1)
let outdeletedsuccess=delete(getcwd()."\\".outfilename)
else
let outdeletedsuccess=delete("./".outfilename)
endif
if(outdeletedsuccess!=0)
setlocal makeprg=make
echohl WarningMsg
\| echo "Fail to make! I cannot delete the ".outfilename
\| echohl None
return
endif
endif
execute "make"
setlocal makeprg=make
if getqflist() == [] "compile successfully and no warning
let l:flag = 0
silent execute "cclose"
execute "normal :"
if filereadable(outfilename)
if(s:isWindows==1)
execute "!".toexename
else
execute "!./".toexename
endif
endif
else
for l:inx in getqflist()
for l:val in values(l:inx)
if l:val =~ 'error'
let l:flag = 1
break
elseif l:val =~ 'warning'
let l:flag = 2
else
let l:flag = 0
endif
endfor
if l:val =~ 'error'
break
endif
endfor
endif
if l:flag == 1
execute "cwindow"
elseif l:flag == 2
let l:select = input('There are warnings! [r]un or [s]olve? ')
if l:select == 'r'
execute "normal :"
if filereadable(outfilename)
if(s:isWindows==1)
execute "!".toexename
else
execute "!./".toexename
endif
endif
execute "cwindow"
elseif l:select == 's'
execute "cwindow"
else
echohl WarningMsg
\| echo "Input error!"
\| echohl None
endif
else
execute "cwindow"
endif
endfunction
2014年12月05日 17点12分 2
level 12
行不行,试下不就知道了
2014年12月06日 01点12分 3
我在Linux下试了点简单的代码,效果可以。但测试没有覆盖全部代码全部分支,不知道是不是完全正确……
2014年12月06日 11点12分
1