Permalink
Browse files

Make CoffeeCompile per-buffer instead intead of global (#75)

Now buffers can open their own separate CoffeeCompile watchers.

Horizontal and vertical splitting was also changed to make the CoffeeCompile
buffer end up closer to its source buffer.
  • Loading branch information...
1 parent 049c8cc commit 319700c3ce04897fc03f61f7b32f792c14772499 @kchmck committed Feb 13, 2012
Showing with 48 additions and 67 deletions.
  1. +48 −67 ftplugin/coffee.vim
View
@@ -19,57 +19,40 @@ if !len(&l:makeprg)
compiler coffee
endif
-" Reset the global variables used by CoffeeCompile.
+" Reset the CoffeeCompile variables for the current buffer.
function! s:CoffeeCompileResetVars()
- " Position in the source buffer
- let s:coffee_compile_src_buf = -1
- let s:coffee_compile_src_pos = []
-
- " Position in the CoffeeCompile buffer
- let s:coffee_compile_buf = -1
- let s:coffee_compile_win = -1
- let s:coffee_compile_pos = []
+ " Compiled output buffer
+ let b:coffee_compile_buf = -1
+ let b:coffee_compile_pos = []
" If CoffeeCompile is watching a buffer
- let s:coffee_compile_watch = 0
+ let b:coffee_compile_watch = 0
endfunction
-" Save the cursor position when moving to and from the CoffeeCompile buffer.
+" Save the cursor in the CoffeeCompile buffer.
function! s:CoffeeCompileSavePos()
- let buf = bufnr('%')
- let pos = getpos('.')
-
- if buf == s:coffee_compile_buf
- let s:coffee_compile_pos = pos
- else
- let s:coffee_compile_src_buf = buf
- let s:coffee_compile_src_pos = pos
- endif
+ let b:coffee_compile_pos = getpos('.')
endfunction
-" Restore the cursor to the source buffer.
+" Restore the cursor in the CoffeeCompile buffer.
function! s:CoffeeCompileRestorePos()
- let win = bufwinnr(s:coffee_compile_src_buf)
-
- if win != -1
- exec win 'wincmd w'
- call setpos('.', s:coffee_compile_src_pos)
- endif
+ call setpos('.', b:coffee_compile_pos)
endfunction
-" Close the CoffeeCompile buffer and clean things up.
+" Clean things up in the source buffer.
function! s:CoffeeCompileClose()
- silent! autocmd! CoffeeCompileAuPos
- silent! autocmd! CoffeeCompileAuWatch
-
- call s:CoffeeCompileRestorePos()
+ exec bufwinnr(b:coffee_compile_src_buf) 'wincmd w'
+ silent! autocmd! CoffeeCompileAuWatch * <buffer>
call s:CoffeeCompileResetVars()
endfunction
" Update the CoffeeCompile buffer given some input lines.
function! s:CoffeeCompileUpdate(startline, endline)
let input = join(getline(a:startline, a:endline), "\n")
+ " Move to the CoffeeCompile buffer.
+ exec bufwinnr(b:coffee_compile_buf) 'wincmd w'
+
" Coffee doesn't like empty input.
if !len(input)
return
@@ -78,8 +61,11 @@ function! s:CoffeeCompileUpdate(startline, endline)
" Compile input.
let output = system('coffee -scb 2>&1', input)
- " Move to the CoffeeCompile buffer.
- exec s:coffee_compile_win 'wincmd w'
+ " Be sure we're in the CoffeeCompile buffer before overwriting.
+ if exists('b:coffee_compile_buf')
+ echoerr 'Something is very wrong'
+ return
+ endif
" Replace buffer contents with new output and delete the last empty line.
setlocal modifiable
@@ -95,93 +81,88 @@ function! s:CoffeeCompileUpdate(startline, endline)
setlocal filetype=javascript
endif
- " Restore the cursor in the compiled output.
- call setpos('.', s:coffee_compile_pos)
+ call s:CoffeeCompileRestorePos()
endfunction
-" Update the CoffeeCompile buffer with the whole source buffer and restore the
-" cursor.
+" Update the CoffeeCompile buffer with the whole source buffer.
function! s:CoffeeCompileWatchUpdate()
- call s:CoffeeCompileSavePos()
call s:CoffeeCompileUpdate(1, '$')
- call s:CoffeeCompileRestorePos()
+ exec bufwinnr(b:coffee_compile_src_buf) 'wincmd w'
endfunction
" Peek at compiled CoffeeScript in a scratch buffer. We handle ranges like this
" to prevent the cursor from being moved (and its position saved) before the
" function is called.
function! s:CoffeeCompile(startline, endline, args)
- " Don't compile the CoffeeCompile buffer.
- if bufnr('%') == s:coffee_compile_buf
- return
+ " If in the CoffeeCompile buffer, switch back to the source buffer and
+ " continue.
+ if !exists('b:coffee_compile_buf')
+ exec bufwinnr(b:coffee_compile_src_buf) 'wincmd w'
endif
" Parse arguments.
let watch = a:args =~ '\<watch\>'
let unwatch = a:args =~ '\<unwatch\>'
let size = str2nr(matchstr(a:args, '\<\d\+\>'))
-
+
" Determine default split direction.
- if exists("g:coffee_compile_vert")
+ if exists('g:coffee_compile_vert')
let vert = 1
else
let vert = a:args =~ '\<vert\%[ical]\>'
endif
" Remove any watch listeners.
- silent! autocmd! CoffeeCompileAuWatch
+ silent! autocmd! CoffeeCompileAuWatch * <buffer>
" If just unwatching, don't compile.
if unwatch
- let s:coffee_compile_watch = 0
+ let b:coffee_compile_watch = 0
return
endif
if watch
- let s:coffee_compile_watch = 1
+ let b:coffee_compile_watch = 1
endif
- call s:CoffeeCompileSavePos()
-
" Build the CoffeeCompile buffer if it doesn't exist.
- if s:coffee_compile_buf == -1
- let src_win = bufwinnr(s:coffee_compile_src_buf)
+ if bufwinnr(b:coffee_compile_buf) == -1
+ let src_buf = bufnr('%')
+ let src_win = bufwinnr(src_buf)
" Create the new window and resize it.
if vert
let width = size ? size : winwidth(src_win) / 2
- vertical new
+ belowright vertical new
exec 'vertical resize' width
else
" Try to guess the compiled output's height.
let height = size ? size : min([winheight(src_win) / 2,
\ a:endline - a:startline + 2])
- botright new
+ belowright new
exec 'resize' height
endif
- " Set up scratch buffer.
+ " We're now in the scratch buffer, so set it up.
setlocal bufhidden=wipe buftype=nofile
setlocal nobuflisted nomodifiable noswapfile nowrap
autocmd BufWipeout <buffer> call s:CoffeeCompileClose()
+ autocmd BufLeave <buffer> call s:CoffeeCompileSavePos()
+
nnoremap <buffer> <silent> q :hide<CR>
- " Save the cursor position on each buffer switch.
- augroup CoffeeCompileAuPos
- autocmd BufEnter,BufLeave * call s:CoffeeCompileSavePos()
- augroup END
+ let b:coffee_compile_src_buf = src_buf
+ let buf = bufnr('%')
- let s:coffee_compile_buf = bufnr('%')
- let s:coffee_compile_win = bufwinnr(s:coffee_compile_buf)
+ " Go back to the source buffer and set it up.
+ exec bufwinnr(b:coffee_compile_src_buf) 'wincmd w'
+ let b:coffee_compile_buf = buf
endif
- " Go back to the source buffer and do the initial compile.
- call s:CoffeeCompileRestorePos()
-
- if s:coffee_compile_watch
+ if b:coffee_compile_watch
call s:CoffeeCompileWatchUpdate()
augroup CoffeeCompileAuWatch
@@ -209,8 +190,8 @@ function! s:CoffeeCompileComplete(arg, cmdline, cursor)
endfor
endfunction
-" Don't let new windows overwrite the CoffeeCompile variables.
-if !exists("s:coffee_compile_buf")
+" Don't overwrite the CoffeeCompile variables.
+if !exists('b:coffee_compile_buf')
call s:CoffeeCompileResetVars()
endif

0 comments on commit 319700c

Please sign in to comment.