Files
scripts/vim/cf5-compiler.vim
Vahagn Khachatryan 0c99ee6dfd User libraries are added to PATH for windows system.
Also, if the buffer is not saved then user is notified about that instead of compiling the version on disk.
2013-06-22 23:00:42 +04:00

199 lines
4.0 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() "{{{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
echo ccline
let cout = system( ccline )
if v:shell_error
echo cout
return
endif
echo cout
" run it
let cmdline="\"set PATH=".g:ldlibpath.$PATH." && ".exename." ".g:argv."\""
echo exename." ".g:argv
"echo cmdline
let eout = system( cmdline )
echo eout
endfunction
function! s:CompileJava() "{{{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() "{{{2
let ext=expand("%:e")
if ext=="java"
call s:CompileJava()
endif
if ext=="cpp"
call s:CompileMSVC()
endif
endfunction
"
" Linux {{{1
"
function! s:CompileGCC() "{{{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
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)
endfunction
function! s:CompileLinux() "{{{2
call s:CompileGCC()
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:ldlibpath=""
endfunction
"
" Load compile instructions and call window or linux compiler. {{{1
"
function! CF5Compile()
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()
else
call s:CompileLinux()
endif
endfunction