Files
scripts/vim/cf5-compiler.vim
Vahagn Khachatryan 4b93ac1861 Minor changes.
2013-09-19 19:08:31 +04:00

204 lines
4.1 KiB
VimL
Executable File

" {{{1
" Functions to compile and link a single c/cpp file.
"
" let g:cf5output
" =0 - no output window opened.
" =1 - ouput window opened.
"
"if exists("g:loaded_vahagn_compiler") | finish | endif
let g:loaded_vahagn_compiler = 1
"
" Make sure let-modeline.vim is loaded.
"
if !exists('*FirstModeLine')
runtime plugin/let-modeline.vim
endif
"
" Windows {{{1
"
function! s:CompileMSVC(run) "{{{2
let exename=expand("%:p:r:s,$,.exe,")
let srcname=expand("%")
" compile it
let ccline="cl ".g:cppflags." ".g:wcppflags." ".srcname." /Fe".exename." /link ".g:ldflags. " ".g:wldflags
echo ccline
let cout = system( ccline )
if v:shell_error
echo cout
return
endif
echo cout
" run it
if a:run==1
let cmdline="\"set PATH=".g:ldlibpath.$PATH." && ".exename." ".g:argv."\""
echo exename." ".g:argv
"echo cmdline
let eout = system( cmdline )
echo eout
endif
endfunction
function! s:CompileJava(run) "{{{2
" compile it
let cmd = "javac " . g:javaflags . " " . expand("%")
echo cmd
let cout = system( cmd )
echo cout
if v:shell_error
return
endif
" run it
"let classpath=expand("%:p:r")
let exename=expand("%:r")
let cmd = "java " . exename . " " . g:argv
echo cmd
let eout = system( cmd )
echo eout
endfunction
function! s:CompileWindows(run) "{{{2
let ext=expand("%:e")
if ext=="java"
call s:CompileJava(a:run)
endif
if ext=="cpp"
call s:CompileMSVC(a:run)
endif
endfunction
"
" Linux {{{1
"
function! s:CompileGCC(run) "{{{2
let exename=expand("%:p:r:s,$,.exe,")
let srcname=expand("%")
" compile it
let ccline="g++ ".g:cppflags." ".g:lcppflags." ".g:ldflags." ".srcname." -o".exename
call s:appendOutput(ccline)
let cout = system( ccline )
if v:shell_error
call s:appendOutput(cout)
return
endif
call s:appendOutput(cout)
" run it
if a:run == 1
let cmdline="LD_LIBRARY_PATH=".g:ldlibpath.":".$LD_LIBRARY_PATH." ".exename." ".g:argv
call s:appendOutput(cmdline)
let eout = system( cmdline )
call s:appendOutput(eout)
endif
endfunction
function! s:CompileLinux(run) "{{{2
call s:CompileGCC(a:run)
endfunction
"
" Output Window {{{1
"
" Create output window. {{{2
"
function! s:getOutputWindow()
if !exists("w:outputwin")
let w:outputwin=tempname().'_output'
endif
let obuff = w:outputwin
let winnr = bufwinnr('^'.obuff.'$')
if (winnr < 0)
execute "below 10new ".obuff
setlocal buftype=nofile bufhidden=wipe nobuflisted noswapfile nowrap
" setlocal nomodifiable
let winnr = bufwinnr('^'.obuff.'$')
endif
return winnr
endfunction
"
" Append text to output window. {{{2
"
function! s:appendOutput( text )
if exists("g:cf5output") && (g:cf5output==1)
let cwinnr = winnr()
let owinnr = s:getOutputWindow()
" setlocal modifiable
execute owinnr . 'wincmd w'
execute 'normal! Go'.a:text
" setlocal nomodifiable
execute cwinnr.'wincmd w'
else
echo a:text
endif
endfunction
"
" Clear text from output window. {{{2
"
function! s:clearOutputWindow()
if exists("g:cf5output") && (g:cf5output==1)
let cwinnr = winnr()
let owinnr = s:getOutputWindow()
" setlocal modifiable
execute owinnr . 'wincmd w'
execute 'normal ggdG'
" setlocal nomodifiable
execute cwinnr . 'wincmd w'
endif
endfunction
"
" Init global variables with default values. {{{1
"
function! s:initDefaults()
let g:cf5output=0
let g:argv=""
let g:cppflags=""
let g:wcppflags="/O2 /EHsc /DWIN32"
let g:lcppflags="-O2"
let g:ldflags=""
let g:wldflags=""
let g:ldlibpath=""
endfunction
"
" Load compile instructions and call window or linux compiler. {{{1
"
function! CF5Compile(run)
if &modified == 1
echo "The buffer is not saved. First save it."
return
endif
"
" First init global variables to let cf5-opt override it.
"
call s:initDefaults()
"
" Source compile-opt.vim if exists.
"
verbose let l:copt=expand("%:p:h")."/cf5-opt.vim"
if filereadable(l:copt)
exec ":source ".l:copt
endif
"
" Set source specific compiler options.
"
call FirstModeLine()
"if exists("g:cf5script")
" execute g:cf5script
"endif
"
" Clear output window
"
call s:clearOutputWindow()
"
" Compile.
"
if has("win32") || has("win64")
call s:CompileWindows(a:run)
else
call s:CompileLinux(a:run)
endif
endfunction