Files
scripts/vim/plugin/cf5-compiler.vim
2014-11-12 18:33:57 +04:00

220 lines
4.4 KiB
VimL
Executable File

"
"
" Functions to compile and link a single c/cpp/java files.
"
" let g:cf5output
" =0 - no output window opened.
" =1 - ouput window opened.
"
if exists('g:loaded_cf5_compiler')
finish
endif
let g:loaded_cf5_compiler = 1
"
" Make sure let-modeline.vim is loaded.
"
if !exists('*FirstModeLine')
runtime plugin/let-modeline.vim
endif
"
" Init global variables with default values.
"
function! s:initDefaults()
let g:cf5output=0
let g:argv=""
let g:pyflags=""
let g:cppflags=""
let g:wcppflags="/O2 /EHsc /DWIN32"
let g:lcppflags="-O2"
let g:ldflags=""
let g:wldflags=""
let g:ldlibpath=""
endfunction
"
" Microsoft Visual C++
"
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 en = "set PATH=\"".g:ldlibpath."%PATH%\""
let cmdline=exename." ".g:argv
let cont = [ en, cmdline ]
let tf = tempname()
let tf = fnamemodify( tf, ":p:r")
let tf = tf.".bat"
call writefile( cont, tf )
let eout = system( tf )
echo eout
call delete( tf )
endif
endfunction
"
" GCC
"
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 $LD_LIBRARY_PATH="LD_LIBRARY_PATH=".g:ldlibpath.":".$LD_LIBRARY_PATH
let cmdline=exename." ".g:argv
call s:appendOutput(cmdline)
let eout = system( cmdline )
call s:appendOutput(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:InterpretPython(run)
" Interpret it
let cmd = "python " . g:pyflags . " " . expand("%") . ' ' . g:argv
echo cmd
let cout = system( cmd )
echo cout
if v:shell_error
return
endif
endfunction
function! s:Compile(run)
if &filetype=="c" || &filetype=="cpp"
if has("win32") || has("win64")
call s:CompileMSVC(a:run)
else
call s:CompileGCC(a:run)
endif
endif
if &filetype=="python"
call s:InterpretPython(a:run)
endif
if &filetype=="java"
call s:CompileJava(a:run)
endif
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
"
" Load compile instructions and call window or linux compiler. {{{1
"
function! CF5Compile(run)
"
" Interpreters and compilers don't work with buffers, but ratter they run
" on files. So, make sure that the file is updated.
"
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()
"
" Set source specific compiler options.
"
call FirstModeLine()
"
" Clear output window
"
call s:clearOutputWindow()
"
" Compile.
"
call s:Compile(a:run)
endfunction