Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

We’re showing branches in this repository, but you can also compare across forks.

base fork: satyr/vim-coco
base: 79bc0c056a
...
head fork: satyr/vim-coco
compare: 4b90e13e1c
  • 2 commits
  • 4 files changed
  • 0 commit comments
  • 1 contributor
Showing with 194 additions and 142 deletions.
  1. +28 −10 compiler/co.vim
  2. +61 −78 ftplugin/co.vim
  3. +95 −35 readme.md
  4. +10 −19 syntax/co.vim
38 compiler/co.vim
View
@@ -11,16 +11,38 @@ let current_compiler = 'co'
" Pattern to check if coco is the compiler
let s:pat = '^' . current_compiler
+" Path to Coco compiler
+if !exists('coco_compiler')
+ let coco_compiler = 'coco'
+endif
+
+if !exists('coco_make_options')
+ let coco_make_options = ''
+endif
+
" Get a `makeprg` for the current filename. This is needed to support filenames
" with spaces and quotes, but also not break generic `make`.
function! s:GetMakePrg()
- return escape('coco -c' . g:coco_make_options
- \ . ' $* '
- \ . fnameescape(expand('%')),
- \ ' ')
+ return g:coco_compiler . ' -c ' . g:coco_make_options . ' $* '
+ \ . fnameescape(expand('%'))
+endfunction
+
+" Set `makeprg` and return 1 if coffee is still the compiler, else return 0.
+function! s:SetMakePrg()
+ if &l:makeprg =~ s:pat
+ let &l:makeprg = s:GetMakePrg()
+ elseif &g:makeprg =~ s:pat
+ let &g:makeprg = s:GetMakePrg()
+ else
+ return 0
+ endif
+
+ return 1
endfunction
-exec 'CompilerSet makeprg=' . s:GetMakePrg()
+" Set a dummy compiler so we can check whether to set locally or globally.
+CompilerSet makeprg=coco
+call s:SetMakePrg()
CompilerSet errorformat=%EFailed\ at:\ %f,
\%CSyntaxError:\ %m\ on\ line\ %l,
@@ -37,11 +59,7 @@ augroup CocoUpdateMakePrg
" Update `makeprg` if coco is still the compiler, else stop running this
" function.
function! s:UpdateMakePrg()
- if &l:makeprg =~ s:pat
- let &l:makeprg = s:GetMakePrg()
- elseif &g:makeprg =~ s:pat
- let &g:makeprg = s:GetMakePrg()
- else
+ if !s:SetMakePrg()
autocmd! CocoUpdateMakePrg
endif
endfunction
139 ftplugin/co.vim
View
@@ -14,60 +14,30 @@ setlocal comments=:#
setlocal commentstring=#\ %s
setlocal omnifunc=javascriptcomplete#CompleteJS
-" Extra options passed to CocoMake
-if !exists("coco_make_options")
- let coco_make_options = ""
-endif
-
" Enable CocoMake if it won't overwrite any settings.
if !len(&l:makeprg)
compiler co
endif
-" Reset the global variables used by CocoCompile.
-function! s:CocoCompileResetVars()
- " Position in the source buffer
- let s:coco_compile_src_buf = -1
- let s:coco_compile_src_pos = []
+" Check here too in case the compiler above isn't loaded.
+if !exists('coco_compiler')
+ let coco_compiler = 'coco'
+endif
- " Position in the CocoCompile buffer
- let s:coco_compile_buf = -1
- let s:coco_compile_win = -1
- let s:coco_compile_pos = []
+" Reset the CocoCompile variables for the current buffer.
+function! s:CocoCompileResetVars()
+ " Compiled output buffer
+ let b:coco_compile_buf = -1
+ let b:coco_compile_pos = []
" If CocoCompile is watching a buffer
- let s:coco_compile_watch = 0
+ let b:coco_compile_watch = 0
endfunction
-" Save the cursor position when moving to and from the CocoCompile buffer.
-function! s:CocoCompileSavePos()
- let buf = bufnr('%')
- let pos = getpos('.')
-
- if buf == s:coco_compile_buf
- let s:coco_compile_pos = pos
- else
- let s:coco_compile_src_buf = buf
- let s:coco_compile_src_pos = pos
- endif
-endfunction
-
-" Restore the cursor to the source buffer.
-function! s:CocoCompileRestorePos()
- let win = bufwinnr(s:coco_compile_src_buf)
-
- if win != -1
- exec win 'wincmd w'
- call setpos('.', s:coco_compile_src_pos)
- endif
-endfunction
-
-" Close the CocoCompile buffer and clean things up.
+" Clean things up in the source buffer.
function! s:CocoCompileClose()
- silent! autocmd! CocoCompileAuPos
- silent! autocmd! CocoCompileAuWatch
-
- call s:CocoCompileRestorePos()
+ exec bufwinnr(b:coco_compile_src_buf) 'wincmd w'
+ silent! autocmd! CocoCompileAuWatch * <buffer>
call s:CocoCompileResetVars()
endfunction
@@ -75,16 +45,22 @@ endfunction
function! s:CocoCompileUpdate(startline, endline)
let input = join(getline(a:startline, a:endline), "\n")
+ " Move to the CocoCompile buffer.
+ exec bufwinnr(b:coco_compile_buf) 'wincmd w'
+
" Coco doesn't like empty input.
if !len(input)
return
endif
" Compile input.
- let output = system('coco -scb 2>&1', input)
+ let output = system(g:coco_compiler . ' -scb 2>&1', input)
- " Move to the CocoCompile buffer.
- exec s:coco_compile_win 'wincmd w'
+ " Be sure we're in the CocoCompile buffer before overwriting.
+ if exists('b:coco_compile_buf')
+ echoerr 'CocoCompile buffers are messed up'
+ return
+ endif
" Replace buffer contents with new output and delete the last empty line.
setlocal modifiable
@@ -100,87 +76,94 @@ function! s:CocoCompileUpdate(startline, endline)
setlocal filetype=javascript
endif
- " Restore the cursor in the compiled output.
- call setpos('.', s:coco_compile_pos)
+ call setpos('.', b:coco_compile_pos)
endfunction
-" Update the CocoCompile buffer with the whole source buffer and restore the
-" cursor.
+" Update the CocoCompile buffer with the whole source buffer.
function! s:CocoCompileWatchUpdate()
- call s:CocoCompileSavePos()
call s:CocoCompileUpdate(1, '$')
- call s:CocoCompileRestorePos()
+ exec bufwinnr(b:coco_compile_src_buf) 'wincmd w'
endfunction
" Peek at compiled CocoScript 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:CocoCompile(startline, endline, args)
- " Don't compile the CocoCompile buffer.
- if bufnr('%') == s:coco_compile_buf
+ if !executable(g:coco_compiler)
+ echoerr "Can't find CocoScript compiler `" . g:coco_compiler . "`"
return
endif
+ " If in the CocoCompile buffer, switch back to the source buffer and
+ " continue.
+ if !exists('b:coco_compile_buf')
+ exec bufwinnr(b:coco_compile_src_buf) 'wincmd w'
+ endif
+
" Parse arguments.
let watch = a:args =~ '\<watch\>'
let unwatch = a:args =~ '\<unwatch\>'
- let vert = a:args =~ '\<vert\%[ical]\>'
let size = str2nr(matchstr(a:args, '\<\d\+\>'))
+ " Determine default split direction.
+ if exists('g:coco_compile_vert')
+ let vert = 1
+ else
+ let vert = a:args =~ '\<vert\%[ical]\>'
+ endif
+
" Remove any watch listeners.
- silent! autocmd! CocoCompileAuWatch
+ silent! autocmd! CocoCompileAuWatch * <buffer>
" If just unwatching, don't compile.
if unwatch
- let s:coco_compile_watch = 0
+ let b:coco_compile_watch = 0
return
endif
if watch
- let s:coco_compile_watch = 1
+ let b:coco_compile_watch = 1
endif
- call s:CocoCompileSavePos()
-
" Build the CocoCompile buffer if it doesn't exist.
- if s:coco_compile_buf == -1
- let src_win = bufwinnr(s:coco_compile_src_buf)
+ if bufwinnr(b:coco_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 + 4])
+ \ 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:CocoCompileClose()
+ " Save the cursor when leaving the CocoCompile buffer.
+ autocmd BufLeave <buffer> let b:coco_compile_pos = getpos('.')
+
nnoremap <buffer> <silent> q :hide<CR>
- " Save the cursor position on each buffer switch.
- augroup CocoCompileAuPos
- autocmd BufEnter,BufLeave * call s:CocoCompileSavePos()
- augroup END
+ let b:coco_compile_src_buf = src_buf
+ let buf = bufnr('%')
- let s:coco_compile_buf = bufnr('%')
- let s:coco_compile_win = bufwinnr(s:coco_compile_buf)
+ " Go back to the source buffer and set it up.
+ exec bufwinnr(b:coco_compile_src_buf) 'wincmd w'
+ let b:coco_compile_buf = buf
endif
- " Go back to the source buffer and do the initial compile.
- call s:CocoCompileRestorePos()
-
- if s:coco_compile_watch
+ if b:coco_compile_watch
call s:CocoCompileWatchUpdate()
augroup CocoCompileAuWatch
@@ -208,7 +191,7 @@ function! s:CocoCompileComplete(arg, cmdline, cursor)
endfor
endfunction
-" Don't let new windows overwrite the CocoCompile variables.
+" Don't overwrite the CoffeeCompile variables.
if !exists("s:coco_compile_buf")
call s:CocoCompileResetVars()
endif
@@ -217,4 +200,4 @@ endif
command! -range=% -bar -nargs=* -complete=customlist,s:CocoCompileComplete
\ CocoCompile call s:CocoCompile(<line1>, <line2>, <q-args>)
" Run some Coco.
-command! -range=% -bar CocoRun <line1>,<line2>:w !coco -seb
+command! -range=% -bar CocoRun <line1>,<line2>:w !coco -sp
130 readme.md
View
@@ -44,11 +44,15 @@ extension or a `Cokefile` will load everything.
Everything will then be brought up to date.
-### Compiling the Current File and Autocompiling
+### CocoMake: Compile the Current File
The `CocoMake` command compiles the current file and parses any errors.
-By default, `CocoMake` shows all compiler output and jumps to the line
+The full signature of the command is:
+
+ :[silent] CocoMake[!] [co-OPTIONS]...
+
+By default, `CocoMake` shows all compiler output and jumps to the first line
reported as an error by `coco`:
:CocoMake
@@ -65,72 +69,128 @@ Options given to `CocoMake` are passed along to `coco`:
:CocoMake --bare
-The command can be bound to a key like:
+`CocoMake` can be manually loaded for a file with:
- nmap KEY :CocoMake<CR>
+ :compiler co
-#### Autocompiling
+#### Recompile on write
-To get autocompiling when a file is written,
-add an `autocmd` like this to your `~/.vimrc`:
+To recompile a file when it's written, add an `autocmd` like this to your
+`vimrc`:
- autocmd BufWritePost *.co silent CocoMake!
+ au BufWritePost *.co silent CocoMake!
All of the customizations above can be used, too. This one compiles silently
-with the `-b` option, but shows any errors:
+and with the `-b` option, but shows any errors:
+
+ au BufWritePost *.co silent CocoMake! -b | cwindow | redraw!
+
+The `redraw!` command is needed to fix a redrawing quirk in terminal vim, but
+can removed for gVim.
- autocmd BufWritePost *.co silent CocoMake! -b | cwindow
+#### Default compiler options
-#### Passing options on-the-fly
+The `CocoMake` command passes any options in the `co_make_options`
+variable along to the compiler. You can use this to set default options:
-The `CocoMake` command passes any options in the `coco_make_options`
-variable along to the compiler. This can be used to set options on-the-fly:
+ let co_make_options = '--bare'
- :let coco_make_options = "-n"
+#### Path to compiler
-### Compiling a Coco Snippet
+To change the compiler used by `CocoMake` and `CocoCompile`, set
+`co_compiler` to the full path of an executable or the filename of one
+in your `$PATH`:
+
+ let co_compiler = '/usr/bin/coco'
+
+This option is set to `coco` by default.
+
+### CocoCompile: Compile Snippets of Coco
The `CocoCompile` command shows how the current file or a snippet of
-Coco would be compiled to JavaScript. Calling `CocoCompile` without a
-range compiles the entire file.
+Coco is compiled to JavaScript. The full signature of the command is:
+
+ :[RANGE] CocoCompile [watch|unwatch] [vert[ical]] [WINDOW-SIZE]
+
+Calling `CocoCompile` without a range compiles the whole file.
Calling `CocoCompile` with a range, like in visual mode, compiles the selected
snippet of Coco.
-The command can also be mapped to a visual mode key for convenience:
+The scratch buffer can be quickly closed by hitting the `q` key.
- vmap KEY :CocoCompile<CR>
+Using `vert` splits the CocoCompile buffer vertically instead of horizontally:
-### Customizing
+ :CocoCompile vert
-These customizations can be enabled or disabled by adding the relevant `let`
-statement to your `~/.vimrc`.
+Set the `co_compile_vert` variable to split the buffer vertically by
+default:
-#### Fold by indentation
+ let co_compile_vert = 1
+
+The initial size of the CocoCompile buffer can be given as a number:
+
+ :CocoCompile 4
+
+#### Watch (live preview) mode
+
+Writing some code and then exiting insert mode automatically updates the
+compiled JavaScript buffer.
+
+Use `watch` to start watching a buffer (`vert` is also recommended):
-Folding is automatically setup as indent-based.
-It's disabled by default, but can be enabled with:
+ :CocoCompile watch vert
- let coco_folding = 1
+After making some changes in insert mode, hit escape and your code will
+be recompiled. Changes made outside of insert mode don't trigger this recompile,
+but calling `CocoCompile` will compile these changes without any bad effects.
-Otherwise, it can be quickly toggled per-file with the `zi` command.
+To get synchronized scrolling of a Coco and CocoCompile buffer, set
+`scrollbind` on each:
+
+ :setl scrollbind
+
+Use `unwatch` to stop watching a buffer:
+
+ :CocoCompile unwatch
+
+### Configure Syntax Highlighting
+
+Add these lines to your `vimrc` to disable the relevant syntax group.
#### Disable trailing whitespace error
Trailing whitespace is highlighted as an error by default. This can be disabled
with:
- let coco_no_trailing_space_error = 1
+ hi link coSpaceError NONE
+
+#### Disable reserved words error
-#### Disable trailing semicolon error
+Reserved words like `function` and `var` are highlighted as an error where
+they're not allowed in Coco. This can be disabled with:
-Trailing semicolons are also considered an error. This can be disabled with:
+ hi link coReservedError NONE
- let coco_no_trailing_semicolon_error = 1
+### Tune Vim for Coco
-#### Disable reserved words error
+Changing these core settings can make vim more Coco friendly.
+
+#### Fold by indentation
+
+Folding by indentation works well for Coco functions and classes.
+To fold by indentation in Coco files, add this line to your `vimrc`:
+
+ au BufNewFile,BufReadPost *.co setl foldmethod=indent nofoldenable
+
+With this, folding is disabled by default but can be quickly toggled per-file
+by hitting `zi`. To enable folding by default, remove `nofoldenable`:
+
+ au BufNewFile,BufReadPost *.co setl foldmethod=indent
+
+#### Two-space indentation
-Reserved words such as `var` and `static` are highlighted as error in contexts
-disallowed by Coco. This can be disabled with:
+To get standard two-space indentation in Coco files, add this line to
+your `vimrc`:
- let coco_no_reserved_words_error = 1
+ au BufNewFile,BufReadPost *.co setl shiftwidth=2 expandtab
29 syntax/co.vim
View
@@ -69,15 +69,14 @@ syntax match coNumber
highlight default link coNumber Number
" Displays an error for reserved words.
-if !exists("coco_no_reserved_words_error")
- syntax match coReservedError /\<\%(var\|const\|enum\|implements\|interface\|package\|private\|protected\|public\|static\|yield\)\>/
- highlight default link coReservedError Error
-endif
+syntax match coReservedError /\<\%(var\|const\|enum\|implements\|interface\|package\|private\|protected\|public\|static\|yield\)\>/
+highlight default link coReservedError Error
syntax keyword coTodo TODO FIXME XXX contained
highlight default link coTodo Todo
-syntax match coComment /#.*/ contains=@Spell,coTodo
+syntax match coComment /#.*/ contains=@Spell,coTodo
+syntax region coComment start=/\/\*/ end=/\*\// contains=@Spell,coTodo
highlight default link coComment Comment
syntax region coEmbed start=/`/ skip=/\\\\\|\\`/ end=/`/
@@ -101,10 +100,10 @@ syntax cluster coSimpleString contains=@Spell,coEscape
syntax cluster coInterpString contains=@coSimpleString,
\ coInterpolation,coVarInterpolation
-syntax region coRegex start=/\%(\%()\|\i\@<!\d\)\s*\|\i\)\@<!\/\s\@!/
-\ skip=/\[[^]]\{-}\/[^]]\{-}\]/
-\ end=/\/[gimy$]\{,4}/
-\ oneline contains=@coSimpleString
+syntax region coRegex start=/\%(\%()\|\i\@<!\d\)\s*\|\i\)\@<!\/\*\@!/
+\ skip=/\[[^]]\{-}\/[^]]\{-}\]/
+\ end=/\/[gimy$]\{,4}/
+\ oneline contains=@coSimpleString
syntax region coHeregex start=/\/\// end=/\/\/[gimy$?]\{,4}/ contains=@coInterpString,coComment,coSpaceError fold
highlight default link coHeregex coRegex
highlight default link coRegex String
@@ -129,16 +128,8 @@ syntax match coKey
\ contains=ALLBUT,coIdentifier,coContext,coGlobal,coReservedError,@coReserved
" Displays an error for trailing whitespace.
-if !exists("coco_no_trailing_space_error")
- syntax match coSpaceError /\s\+$/ display
- highlight default link coSpaceError Error
-endif
-
-" Displays an error for trailing semicolons.
-if !exists("coco_no_trailing_semicolon_error")
- syntax match coSemicolonError /;$/ display
- highlight default link coSemicolonError Error
-endif
+syntax match coSpaceError /\s\+$/ display
+highlight default link coSpaceError Error
if !exists('b:current_syntax')
let b:current_syntax = 'coco'

No commit comments for this range

Something went wrong with that request. Please try again.