Skip to content

Instantly share code, notes, and snippets.

@micheee
Forked from arolle/vimrc
Created December 10, 2013 17:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save micheee/7894283 to your computer and use it in GitHub Desktop.
Save micheee/7894283 to your computer and use it in GitHub Desktop.
" First step to make VIM an XQuery IDE using BaseX
"
" Adds possibility to run the currently edited XQuery file
" using key combination <leader>r
" An error output jumps to the file, line and column
" where the error occured.
"
" associate *.bxs with xml filetype
au BufRead,BufNewFile *.bxs setfiletype xml
" Regexp that matches a BaseX error, i.e.
" Stopped at file, line/column:
let g:BaseXErrorRegexp = 'Stopped at \(.*\)\, \(\d\+\)\/\(\d\+\)'
" Path to BaseX
let g:BaseXPath = 'basex'
" write file and execute it
autocmd BufRead,BufNewFile *.bxs nmap <buffer> <Leader>r :w<CR>:call RunCmdwError(g:BaseXPath, g:BaseXErrorRegexp)<CR>
autocmd FileType xquery nmap <buffer> <leader>r :w<CR>:call RunCmdwError(g:BaseXPath, g:BaseXErrorRegexp)<CR>
""" Helping Functions
" Runs a Command, opens a buffer containing output
" and if an error occured tries to match file name, line number and column number
" from resulting warning.
function! RunCmdwError(cmd, linecolregexp)
let s:bnm = bufname('%') " current file
let s:nm = '__CMD_'.s:bnm.'__'
call RunCmd(a:cmd)
if v:shell_error > 0 " parse for regexp if error occured
let s:bufferline = getbufline(s:nm, 1, 1) " use first line only
let s:m = matchlist(s:bufferline, a:linecolregexp) " match given expr
" switch to error file, line, col if any matches
" debug: echo len(s:m) . ' no of matches'
if len(s:m) > 0
if filereadable(s:m[1]) " go to file
call SwitchToOpenBuf(s:m[1])
endif
if strlen(s:m[2]) > 0 " go to line
exec ':' . s:m[2]
elseif strlen(s:m[3]) > 0 " go to column
call feedkeys(s:m[3] . '|')
endif
echo 'Error in file ' . s:m[1] . ' line ' . s:m[2] . ' col ' . s:m[3]
endif " endif matches
endif
endfunction
"" RunCmd runs a given command with the filename as
" argument and outputs the result of the command into
" a unique buffer. If run again, the unique buffer updates.
function! RunCmd(cmd)
" Get the output
let s:bnm = bufname("%")
let s:bytecode = system(a:cmd. ' ' . shellescape(s:bnm))
let s:nm = '__CMD_'.s:bnm.'__'
exe 'bd! '.s:nm
"exe "vsplit ".s:nm
exe 'split '.s:nm
normal! ggdG " empty buffer
" Insert the terminal output
call append(0, split(s:bytecode, '\v\n'))
" Prevent use of the buffer as a file (to ensure if the file exists, it's
" not saved and to prevent it from being modified -- we don't support
" cancelling).
setlocal noswapfile
setlocal nowrap
setlocal nonumber
setlocal buftype=nofile
" setlocal nobuflisted
setlocal readonly
setlocal filetype=xml
if !has("gui_running")
redraw!
endif
call SwitchToOpenBuf(s:bnm) " go back to file
endfunction
" Switch to tab/window where 'filename' is opened in
" http://stackoverflow.com/questions/3920287/how-to-move-to-non-hidden-buffer-that-exists-in-some-window-on-some-tab
"
function! SwitchToOpenBuf(filename)
" remember current value of switchbuf
let l:old_switchbuf = &switchbuf
try
" change switchbuf so other windows and tabs are used
set switchbuf=useopen,usetab
execute 'sbuf '. a:filename
" debug: echo 'switched to ' . a:filename
finally
" restore old value of switchbuf
let &switchbuf = l:old_switchbuf
endtry
endfunction
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment