Skip to content

Instantly share code, notes, and snippets.

@yoppi
Created October 2, 2008 08:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save yoppi/14313 to your computer and use it in GitHub Desktop.
Save yoppi/14313 to your computer and use it in GitHub Desktop.
" quickrun - run a command and show its result quickly
" Author: ujihisa <http://ujihisa.nowa.jp/>
" ModifiedBy: kana <http://whileimautomaton.net/>
if exists('g:loaded_quickrun')
finish
endif
function! s:quickrun()
if !exists('b:quickrun_command')
echoerr 'quickrun is not available for filetype:' string(&l:filetype)
return
endif
let quickrun_command = b:quickrun_command
call s:set_headpath()
let existent_file_p = filereadable(expand('%'))
if existent_file_p
silent update
let file = expand('%')
else
let file = tempname() . expand('%:e')
let original_bufname = bufname('')
let original_modified = &l:modified
silent keepalt write `=file`
if original_bufname == ''
" Reset the side effect of ":write {file}" - it sets {file} as the
" name of the current buffer if it is unnamed buffer.
silent 0 file
endif
let &l:modified = original_modified
endif
call s:open_result_buffer(quickrun_command)
setlocal modifiable
silent % delete _
call append(0, ':-)')
redraw
silent % delete _
call append(0, '')
if exists('b:need_compiling')
execute 'silent! read !' quickrun_command file
let _ = b:head_path . '/a.out'
let exefile = string(_)
let existent_exefile_p = filereadable(_)
if existent_exefile_p
execute 'silent! read !' exefile
endif
else
execute 'silent! read !' quickrun_command file
endif
silent 1 delete _
setlocal nomodifiable
if existent_file_p
" nop.
else
call delete(file)
endif
endfunc
function! s:open_result_buffer(quickrun_command)
let bufname = printf('*quickrun* %s', a:quickrun_command)
if !bufexists(bufname)
execute g:quickrun_direction 'vnew'
setlocal bufhidden=unload
setlocal nobuflisted
setlocal buftype=nofile
setlocal nomodifiable
setlocal noswapfile
setfiletype quickrun
silent file `=bufname`
nnoremap <buffer> <silent> q <C-w>c
else
let bufnr = bufnr(bufname) " FIXME: escape.
let winnr = bufwinnr(bufnr)
if winnr == -1
execute g:quickrun_direction 'vsplit'
execute bufnr 'buffer'
else
execute winnr 'wincmd w'
endif
endif
endfunction
function! s:set_quickrun_command(command)
" Use user's settings if they exist.
if !exists('b:quickrun_command')
let b:quickrun_command = a:command
endif
endfunction
function! s:set_need_compiling(compiler)
if !exists('b:need_compiling')
let b:need_compiling = 1
call s:set_quickrun_command(a:compiler)
endif
endfunction
function! s:set_headpath()
if !exists('b:head_path')
let b:head_path = expand('%:h')
endif
endfunction
if !exists('g:quickrun_direction')
"let g:quickrun_direction = 'rightbelow'
let g:quickrun_direction = 'belowright'
endif
nnoremap <silent> <Plug>(quickrun) :<C-u>call <SID>quickrun()<Return>
silent! nmap <unique> <Leader>r <Plug>(quickrun)
augroup plugin-quickrun
autocmd!
autocmd Filetype awk call s:set_quickrun_command('awk')
"autocmd Filetype c call s:set_quickrun_command('function __rungcc__() { gcc $1 && ./a.out } && __rungcc__')
autocmd Filetype c call s:set_need_compiling('gcc')
"autocmd Filetype cpp call s:set_quickrun_command('function __rungpp__() { g++ $1 && ./a.out } && __rungpp__')
autocmd Filetype cpp call s:set_need_compiling('g++')
"autocmd Filetype objc call s:set_quickrun_command('function __rungcc__() { gcc $1 && ./a.out } && __rungcc__')
autocmd Filetype haskell call s:set_quickrun_command('runghc')
autocmd Filetype io call s:set_quickrun_command('io')
autocmd Filetype javascript call s:set_quickrun_command('js')
autocmd Filetype perl call s:set_quickrun_command('perl')
autocmd Filetype php call s:set_quickrun_command('php')
autocmd Filetype python call s:set_quickrun_command('python')
autocmd Filetype ruby call s:set_quickrun_command('ruby')
autocmd Filetype scala call s:set_quickrun_command('scala')
autocmd Filetype scheme call s:set_quickrun_command('gosh')
autocmd Filetype sed call s:set_quickrun_command('sed')
autocmd Filetype sh call s:set_quickrun_command('sh')
augroup END
let g:loaded_quickrun = 1
" __END__
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment