Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Retry syntax-highlighted CoffeeCompile

`redir` wasn't capturing stdout from coffee in terminal vim, so use the
`system` function instead.

Thanks again to sdiehl and chrismetcalf/YankRingWindow
  • Loading branch information...
commit ae54812713d10e14156444f2b02f8effb951ad4a 1 parent a0d5aa2
@kchmck authored
Showing with 57 additions and 10 deletions.
  1. +51 −2 ftplugin/coffee.vim
  2. +6 −8 readme.md
View
53 ftplugin/coffee.vim
@@ -9,6 +9,9 @@ endif
let b:did_ftplugin = 1
+" Previously-opened `CoffeeCompile` buffer
+let s:coffee_compile_buf = -1
+
setlocal formatoptions-=t formatoptions+=croql
setlocal comments=:#
setlocal commentstring=#\ %s
@@ -42,8 +45,54 @@ call s:SetMakePrg()
" Reset `makeprg` on rename.
autocmd BufFilePost,BufWritePost,FileWritePost <buffer> call s:SetMakePrg()
-" Compile some CoffeeScript.
-command! -range=% CoffeeCompile <line1>,<line2>:w !coffee -scb
+" Compile some CoffeeScript and show it in a scratch buffer.
+function! s:CoffeeCompile() range
+ " Build stdin lines.
+ let lines = join(getline(a:firstline, a:lastline), "\n")
+ " Get compiler output.
+ let output = system('coffee -scb', lines)
+
+ " Use at most half of the screen.
+ let max_height = winheight('%') / 2
+ " Try to get the old window.
+ let win = bufwinnr(s:coffee_compile_buf)
+
+ if win == -1
+ " Make a new window and store its ID.
+ botright new
+ let s:coffee_compile_buf = bufnr('%')
+
+ setlocal bufhidden=wipe buftype=nofile
+ setlocal nobuflisted noswapfile nowrap
+
+ nnoremap <buffer> <silent> q :close<CR>
+ else
+ " Move to the old window and clear the buffer.
+ exec win 'wincmd w'
+ setlocal modifiable
+ exec '% delete _'
+ endif
+
+ " Paste in the output and delete the last empty line.
+ put! =output
+ exec '$ delete _'
+
+ exec 'resize' min([max_height, line('$') + 1])
+ call cursor(1, 1)
+
+ if v:shell_error
+ " A compile error occured.
+ setlocal filetype=
+ else
+ " Coffee was compiled successfully.
+ setlocal filetype=javascript
+ endif
+
+ setlocal nomodifiable
+endfunction
+
+" Peek at compiled CoffeeScript.
+command! -range=% -bar CoffeeCompile <line1>,<line2>:call s:CoffeeCompile()
" Compile the current file.
command! -bang -bar -nargs=* CoffeeMake make<bang> <args>
" Run some CoffeeScript.
View
14 readme.md
@@ -107,26 +107,24 @@ variable along to the compiler. This can be used to set options on-the-fly:
The `CoffeeCompile` command shows how the current file or a snippet of
CoffeeScript would be compiled to JavaScript. Calling `CoffeeCompile` without a
-range compiles the entire file:
+range compiles the whole file:
- ![CoffeeCompile](http://i.imgur.com/AZAAd.png)
+ ![CoffeeCompile](http://i.imgur.com/7GRGR.png)
and shows an output like:
- ![Compiled](http://i.imgur.com/5Huj4.png)
+ ![Compiled](http://i.imgur.com/YfdjH.png)
Calling `CoffeeCompile` with a range, like in visual mode, compiles the selected
snippet of CoffeeScript:
- ![CoffeeCompile Snippet](http://i.imgur.com/SKqCc.png)
+ ![CoffeeCompile Snippet](http://i.imgur.com/7EoW4.png)
and shows an output like:
- ![Compiled Snippet](http://i.imgur.com/wkO4f.png)
+ ![Compiled Snippet](http://i.imgur.com/vd06X.png)
-The command can also be mapped to a visual mode key for convenience:
-
- vmap KEY :CoffeeCompile<CR>
+This scratch buffer can be quickly closed by hitting the `q` key.
### Running some CoffeeScript

0 comments on commit ae54812

Please sign in to comment.
Something went wrong with that request. Please try again.