Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

coffescript

  • Loading branch information...
commit a6f801be02de738f8b4e03f169222c4698dddeea 1 parent 718589d
@creaktive authored
View
9 after/syntax/haml.vim
@@ -0,0 +1,9 @@
+" Language: CoffeeScript
+" Maintainer: Sven Felix Oberquelle <Svelix.Github@gmail.com>
+" URL: http://github.com/kchmck/vim-coffee-script
+" License: WTFPL
+
+" Inherit coffee from html so coffeeComment isn't redefined and given higher
+" priority than hamlInterpolation.
+syn cluster hamlCoffeescript contains=@htmlCoffeeScript
+syn region hamlCoffeescriptFilter matchgroup=hamlFilter start="^\z(\s*\):coffee\z(script\)*\s*$" end="^\%(\z1 \| *$\)\@!" contains=@hamlCoffeeScript,hamlInterpolation keepend
View
11 after/syntax/html.vim
@@ -0,0 +1,11 @@
+" Language: CoffeeScript
+" Maintainer: Mick Koch <kchmck@gmail.com>
+" URL: http://github.com/kchmck/vim-coffee-script
+" License: WTFPL
+
+" Syntax highlighting for text/coffeescript script tags
+syn include @htmlCoffeeScript syntax/coffee.vim
+syn region coffeeScript start=+<script [^>]*type *=[^>]*text/coffeescript[^>]*>+
+\ end=+</script>+me=s-1 keepend
+\ contains=@htmlCoffeeScript,htmlScriptTag,@htmlPreproc
+\ containedin=htmlHead
View
82 compiler/coffee.vim
@@ -0,0 +1,82 @@
+" Language: CoffeeScript
+" Maintainer: Mick Koch <kchmck@gmail.com>
+" URL: http://github.com/kchmck/vim-coffee-script
+" License: WTFPL
+
+" All this is needed to support compiling filenames with spaces, quotes, and
+" such. The filename is escaped and embedded into the `makeprg` setting.
+"
+" Because of this, `makeprg` must be updated on every file rename. And because
+" of that, `CompilerSet` can't be used because it doesn't exist when the
+" rename autocmd is ran. So, we have to do some checks to see whether `compiler`
+" was called locally or globally, and respect that in the rest of the script.
+
+if exists('current_compiler')
+ finish
+else
+ let current_compiler = 'coffee'
+endif
+
+" Pattern to check if coffee is the compiler
+let s:pat = '^' . current_compiler
+
+" Path to CoffeeScript compiler
+if !exists('coffee_compiler')
+ let coffee_compiler = 'coffee'
+endif
+
+" Extra options passed to CoffeeMake
+if !exists('coffee_make_options')
+ let coffee_make_options = ''
+endif
+
+" Get a `makeprg` for the current filename.
+function! s:GetMakePrg()
+ return g:coffee_compiler . ' -c ' . g:coffee_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
+
+" Set a dummy compiler so we can check whether to set locally or globally.
+CompilerSet makeprg=coffee
+call s:SetMakePrg()
+
+CompilerSet errorformat=Error:\ In\ %f\\,\ %m\ on\ line\ %l,
+ \Error:\ In\ %f\\,\ Parse\ error\ on\ line\ %l:\ %m,
+ \SyntaxError:\ In\ %f\\,\ %m,
+ \%-G%.%#
+
+" Compile the current file.
+command! -bang -bar -nargs=* CoffeeMake make<bang> <args>
+
+" Set `makeprg` on rename since we embed the filename in the setting.
+augroup CoffeeUpdateMakePrg
+ autocmd!
+
+ " Update `makeprg` if coffee is still the compiler, else stop running this
+ " function.
+ function! s:UpdateMakePrg()
+ if !s:SetMakePrg()
+ autocmd! CoffeeUpdateMakePrg
+ endif
+ endfunction
+
+ " Set autocmd locally if compiler was set locally.
+ if &l:makeprg =~ s:pat
+ autocmd BufFilePost,BufWritePost <buffer> call s:UpdateMakePrg()
+ else
+ autocmd BufFilePost,BufWritePost call s:UpdateMakePrg()
+ endif
+augroup END
View
143 doc/coffee-script.txt
@@ -0,0 +1,143 @@
+*coffee-script.txt* For Vim version 7.3
+
+=============================================================================
+Author: Mick Koch <kchmck@gmail.com> *coffee-script-author*
+License: WTFPL (see |coffee-script-license|)
+=============================================================================
+
+CONTENTS *coffee-script-contents*
+
+|coffee-script-introduction| Introduction and Feature Summary
+|coffee-script-commands| Commands
+|coffee-script-settings| Settings
+|compiler-coffee-script| Compiler
+
+{Vi does not have any of this}
+
+=============================================================================
+
+INTRODUCTION *coffee-script*
+ *coffee-script-introduction*
+
+This plugin adds support for CoffeeScript syntax, indenting, and compiling.
+Also included is an eco syntax and support for CoffeeScript in Haml and HTML.
+
+COMMANDS *coffee-script-commands*
+
+ *:CoffeeMake*
+:CoffeeMake[!] {opts} Wrapper around |:make| that also passes options in
+ |g:coffee_make_options| to the compiler. Use |:silent|
+ to hide compiler output. See |:make| for more
+ information about [!] and other helpful commands.
+
+ *:CoffeeCompile*
+:[range]CoffeeCompile [vertical] [{win-size}]
+ Shows how the current file or [range] is compiled
+ to JavaScript. [vertical] (or vert) splits the
+ compile buffer vertically instead of horizontally, and
+ {win-size} sets the initial size of the buffer. It can
+ be closed quickly with the "q" key.
+
+:CoffeeCompile {watch} [vertical] [{win-size}]
+ The watch mode of :CoffeeCompile emulates the "Try
+ CoffeeScript" live preview on the CoffeeScript web
+ site. After making changes to the source file,
+ exiting insert mode will cause the preview buffer to
+ update automatically. {watch} should be given as
+ "watch" or "unwatch," where the latter will stop the
+ automatic updating. [vertical] is recommended, and
+ 'scrollbind' is useful.
+
+ *:CoffeeLint*
+:[range]CoffeeLint[!] {opts}
+ Run {coffeelint} on the current file and add any
+ errors to the quickfix list. The first error is jumped
+ to if [!] isn't given. Options in
+ |g:coffee_lint_options| and {opts} are passed along to
+ {coffeelint}. Although the whole file is linted, if a
+ [range] is given, only errors within those lines will
+ be considered.
+
+ *:CoffeeRun*
+:[range]CoffeeRun Compiles the file or [range] and runs the resulting
+ JavaScript, displaying the output.
+
+SETTINGS *coffee-script-settings*
+
+You can configure plugin behavior using global variables and syntax commands
+in your |vimrc|.
+
+Global Settings~
+ *g:coffee_compiler*
+Set the path to the compiler used by |CoffeeMake| and |CoffeeCompile| (it's
+{coffee} by default.)
+>
+ let coffee_compiler = '/usr/bin/coffee'
+<
+ *g:coffee_make_options*
+Set default options |CoffeeMake| should pass to the compiler.
+>
+ let coffee_make_options = '--bare'
+<
+ *g:coffee_compile_vert*
+Split the CoffeeCompile buffer vertically by default.
+>
+ let coffee_compile_vert = 1
+<
+ *g:coffee_linter*
+Set the path to the {coffeelint} executable (it's {coffeelint} by default.)
+>
+ let coffee_linter = '/usr/bin/coffeelint'
+<
+ *g:coffee_lint_options*
+Set default options |CoffeeLint| should pass to {coffeelint}.
+>
+ let coffee_lint_options = '-f lint.json'
+<
+
+Syntax Highlighting~
+ *ft-coffee-script-syntax*
+Trailing whitespace is highlighted as an error by default. This can be
+disabled with:
+>
+ hi link coffeeSpaceError NONE
+
+Trailing semicolons are also considered an error (for help transitioning from
+JavaScript.) This can be disabled with:
+>
+ hi link coffeeSemicolonError NONE
+
+Reserved words like {function} and {var} are highlighted where they're not
+allowed in CoffeeScript. This can be disabled with:
+>
+ hi link coffeeReservedError NONE
+
+COMPILER *compiler-coffee-script*
+
+A CoffeeScript compiler is provided as a wrapper around {coffee} and can be
+loaded with;
+>
+ compiler coffee
+
+This is done automatically when a CoffeeScript file is opened if no other
+compiler is loaded.
+
+=============================================================================
+
+LICENSE *coffee-script-license*
+
+ DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
+ Version 2, December 2004
+
+ Copyright (C) 2010 to 2012 Mick Koch <kchmck@gmail.com>
+
+ Everyone is permitted to copy and distribute verbatim or modified
+ copies of this license document, and changing it is allowed as long
+ as the name is changed.
+
+ DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
+ TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
+
+ 0. You just DO WHAT THE FUCK YOU WANT TO.
+
+ vim:tw=78:ts=8:ft=help:norl:
View
19 doc/tags
@@ -86,6 +86,10 @@
:AckAdd ack.txt /*:AckAdd*
:AckFile ack.txt /*:AckFile*
:AckFromSearch ack.txt /*:AckFromSearch*
+:CoffeeCompile coffee-script.txt /*:CoffeeCompile*
+:CoffeeLint coffee-script.txt /*:CoffeeLint*
+:CoffeeMake coffee-script.txt /*:CoffeeMake*
+:CoffeeRun coffee-script.txt /*:CoffeeRun*
:CtrlP ctrlp.txt /*:CtrlP*
:CtrlPBookmarkDir ctrlp.txt /*:CtrlPBookmarkDir*
:CtrlPBookmarkDirAdd ctrlp.txt /*:CtrlPBookmarkDirAdd*
@@ -290,6 +294,15 @@ ack-author ack.txt /*ack-author*
ack-mappings ack.txt /*ack-mappings*
ack.txt ack.txt /*ack.txt*
before solarized.txt /*before*
+coffee-script coffee-script.txt /*coffee-script*
+coffee-script-author coffee-script.txt /*coffee-script-author*
+coffee-script-commands coffee-script.txt /*coffee-script-commands*
+coffee-script-contents coffee-script.txt /*coffee-script-contents*
+coffee-script-introduction coffee-script.txt /*coffee-script-introduction*
+coffee-script-license coffee-script.txt /*coffee-script-license*
+coffee-script-settings coffee-script.txt /*coffee-script-settings*
+coffee-script.txt coffee-script.txt /*coffee-script.txt*
+compiler-coffee-script coffee-script.txt /*compiler-coffee-script*
ctrlp-changelog ctrlp.txt /*ctrlp-changelog*
ctrlp-commands ctrlp.txt /*ctrlp-commands*
ctrlp-contents ctrlp.txt /*ctrlp-contents*
@@ -332,6 +345,7 @@ eregex-options eregex.txt /*eregex-options*
eregex-principle eregex.txt /*eregex-principle*
eregex-table eregex.txt /*eregex-table*
eregex.txt eregex.txt /*eregex.txt*
+ft-coffee-script-syntax coffee-script.txt /*ft-coffee-script-syntax*
fugitive fugitive.txt /*fugitive*
fugitive#statusline() fugitive.txt /*fugitive#statusline()*
fugitive-:Gblame fugitive.txt /*fugitive-:Gblame*
@@ -384,6 +398,11 @@ g:SuperTabMappingTabLiteral supertab.txt /*g:SuperTabMappingTabLiteral*
g:SuperTabNoCompleteAfter supertab.txt /*g:SuperTabNoCompleteAfter*
g:SuperTabNoCompleteBefore supertab.txt /*g:SuperTabNoCompleteBefore*
g:SuperTabRetainCompletionDuration supertab.txt /*g:SuperTabRetainCompletionDuration*
+g:coffee_compile_vert coffee-script.txt /*g:coffee_compile_vert*
+g:coffee_compiler coffee-script.txt /*g:coffee_compiler*
+g:coffee_lint_options coffee-script.txt /*g:coffee_lint_options*
+g:coffee_linter coffee-script.txt /*g:coffee_linter*
+g:coffee_make_options coffee-script.txt /*g:coffee_make_options*
g:ctrlp_dont_split ctrlp.txt /*g:ctrlp_dont_split*
g:ctrlp_dotfiles ctrlp.txt /*g:ctrlp_dotfiles*
g:ctrlp_highlight_match ctrlp.txt /*g:ctrlp_highlight_match*
View
17 ftdetect/coffee.vim
@@ -0,0 +1,17 @@
+" Language: CoffeeScript
+" Maintainer: Mick Koch <kchmck@gmail.com>
+" URL: http://github.com/kchmck/vim-coffee-script
+" License: WTFPL
+
+autocmd BufNewFile,BufRead *.coffee set filetype=coffee
+autocmd BufNewFile,BufRead *Cakefile set filetype=coffee
+autocmd BufNewFile,BufRead *.coffeekup,*.ck set filetype=coffee
+autocmd BufNewFile,BufRead *._coffee set filetype=coffee
+
+function! s:DetectCoffee()
+ if getline(1) =~ '^#!.*\<coffee\>'
+ set filetype=coffee
+ endif
+endfunction
+
+autocmd BufNewFile,BufRead * call s:DetectCoffee()
View
1  ftdetect/eco.vim
@@ -0,0 +1 @@
+autocmd BufNewFile,BufRead *.eco set filetype=eco
View
263 ftplugin/coffee.vim
@@ -0,0 +1,263 @@
+" Language: CoffeeScript
+" Maintainer: Mick Koch <kchmck@gmail.com>
+" URL: http://github.com/kchmck/vim-coffee-script
+" License: WTFPL
+
+if exists("b:did_ftplugin")
+ finish
+endif
+
+let b:did_ftplugin = 1
+
+setlocal formatoptions-=t formatoptions+=croql
+setlocal comments=:#
+setlocal commentstring=#\ %s
+setlocal omnifunc=javascriptcomplete#CompleteJS
+
+" Enable CoffeeMake if it won't overwrite any settings.
+if !len(&l:makeprg)
+ compiler coffee
+endif
+
+" Check here too in case the compiler above isn't loaded.
+if !exists('coffee_compiler')
+ let coffee_compiler = 'coffee'
+endif
+
+" Path to coffeelint executable
+if !exists('coffee_linter')
+ let coffee_linter = 'coffeelint'
+endif
+
+" Options passed to CoffeeLint
+if !exists('coffee_lint_options')
+ let coffee_lint_options = ''
+endif
+
+" Reset the CoffeeCompile variables for the current buffer.
+function! s:CoffeeCompileResetVars()
+ " Compiled output buffer
+ let b:coffee_compile_buf = -1
+ let b:coffee_compile_pos = []
+
+ " If CoffeeCompile is watching a buffer
+ let b:coffee_compile_watch = 0
+endfunction
+
+" Clean things up in the source buffer.
+function! s:CoffeeCompileClose()
+ 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
+ endif
+
+ " Compile input.
+ let output = system(g:coffee_compiler . ' -scb 2>&1', input)
+
+ " Be sure we're in the CoffeeCompile buffer before overwriting.
+ if exists('b:coffee_compile_buf')
+ echoerr 'CoffeeCompile buffers are messed up'
+ return
+ endif
+
+ " Replace buffer contents with new output and delete the last empty line.
+ setlocal modifiable
+ exec '% delete _'
+ put! =output
+ exec '$ delete _'
+ setlocal nomodifiable
+
+ " Highlight as JavaScript if there is no compile error.
+ if v:shell_error
+ setlocal filetype=
+ else
+ setlocal filetype=javascript
+ endif
+
+ call setpos('.', b:coffee_compile_pos)
+endfunction
+
+" Update the CoffeeCompile buffer with the whole source buffer.
+function! s:CoffeeCompileWatchUpdate()
+ call s:CoffeeCompileUpdate(1, '$')
+ 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)
+ if !executable(g:coffee_compiler)
+ echoerr "Can't find CoffeeScript compiler `" . g:coffee_compiler . "`"
+ return
+ endif
+
+ " 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')
+ let vert = 1
+ else
+ let vert = a:args =~ '\<vert\%[ical]\>'
+ endif
+
+ " Remove any watch listeners.
+ silent! autocmd! CoffeeCompileAuWatch * <buffer>
+
+ " If just unwatching, don't compile.
+ if unwatch
+ let b:coffee_compile_watch = 0
+ return
+ endif
+
+ if watch
+ let b:coffee_compile_watch = 1
+ endif
+
+ " Build the CoffeeCompile buffer if it doesn't exist.
+ 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
+
+ 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])
+
+ belowright new
+ exec 'resize' height
+ endif
+
+ " 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()
+ " Save the cursor when leaving the CoffeeCompile buffer.
+ autocmd BufLeave <buffer> let b:coffee_compile_pos = getpos('.')
+
+ nnoremap <buffer> <silent> q :hide<CR>
+
+ let b:coffee_compile_src_buf = src_buf
+ let buf = bufnr('%')
+
+ " 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
+
+ if b:coffee_compile_watch
+ call s:CoffeeCompileWatchUpdate()
+
+ augroup CoffeeCompileAuWatch
+ autocmd InsertLeave <buffer> call s:CoffeeCompileWatchUpdate()
+ augroup END
+ else
+ call s:CoffeeCompileUpdate(a:startline, a:endline)
+ endif
+endfunction
+
+" Complete arguments for the CoffeeCompile command.
+function! s:CoffeeCompileComplete(arg, cmdline, cursor)
+ let args = ['unwatch', 'vertical', 'watch']
+
+ if !len(a:arg)
+ return args
+ endif
+
+ let match = '^' . a:arg
+
+ for arg in args
+ if arg =~ match
+ return [arg]
+ endif
+ endfor
+endfunction
+
+" Run coffeelint on a file, and add any errors between @startline and @endline
+" to the quickfix list.
+function! s:CoffeeLint(startline, endline, bang, args)
+ if !executable(g:coffee_linter)
+ echoerr "Can't find CoffeeScript linter `" . g:coffee_linter . "`"
+ return
+ endif
+
+ let filename = expand('%')
+
+ if !len(filename)
+ echoerr 'CoffeeLint must be ran on a saved file'
+ return
+ endif
+
+ let lines = split(system(g:coffee_linter . ' --csv ' . g:coffee_lint_options .
+ \ ' ' . a:args . ' ' . filename . ' 2>&1'), '\n')
+ let qflist = []
+
+ for line in lines
+ let match = matchlist(line, '\f\+,\(\d\+\),error,\(.\+\)')
+
+ " Ignore invalid lines.
+ if !len(match)
+ continue
+ endif
+
+ let lnum = str2nr(match[1])
+
+ " Don't add the error if it's not in the range.
+ if lnum < a:startline || lnum > a:endline
+ continue
+ endif
+
+ let text = match[2]
+
+ call add(qflist, {'bufnr': bufnr('%'), 'lnum': lnum, 'text': text})
+ endfor
+
+ call setqflist(qflist, 'r')
+
+ " Don't jump if there's a bang.
+ if !len(a:bang)
+ silent! cc 1
+ endif
+endfunction
+
+" Don't overwrite the CoffeeCompile variables.
+if !exists('b:coffee_compile_buf')
+ call s:CoffeeCompileResetVars()
+endif
+
+" Peek at compiled CoffeeScript.
+command! -range=% -bar -nargs=* -complete=customlist,s:CoffeeCompileComplete
+\ CoffeeCompile call s:CoffeeCompile(<line1>, <line2>, <q-args>)
+" Run some CoffeeScript.
+command! -range=% -bar CoffeeRun <line1>,<line2>:w !coffee -s
+" Run coffeelint on the file.
+command! -range=% -bang -bar -nargs=* CoffeeLint
+\ call s:CoffeeLint(<line1>, <line2>, '<bang>', <q-args>)
View
351 indent/coffee.vim
@@ -0,0 +1,351 @@
+" Language: CoffeeScript
+" Maintainer: Mick Koch <kchmck@gmail.com>
+" URL: http://github.com/kchmck/vim-coffee-script
+" License: WTFPL
+
+if exists("b:did_indent")
+ finish
+endif
+
+let b:did_indent = 1
+
+setlocal autoindent
+setlocal indentexpr=GetCoffeeIndent(v:lnum)
+" Make sure GetCoffeeIndent is run when these are typed so they can be
+" indented or outdented.
+setlocal indentkeys+=0],0),0.,=else,=when,=catch,=finally
+
+" Only define the function once.
+if exists("*GetCoffeeIndent")
+ finish
+endif
+
+" Keywords to indent after
+let s:INDENT_AFTER_KEYWORD = '^\%(if\|unless\|else\|for\|while\|until\|'
+\ . 'loop\|switch\|when\|try\|catch\|finally\|'
+\ . 'class\)\>'
+
+" Operators to indent after
+let s:INDENT_AFTER_OPERATOR = '\%([([{:=]\|[-=]>\)$'
+
+" Keywords and operators that continue a line
+let s:CONTINUATION = '\<\%(is\|isnt\|and\|or\)\>$'
+\ . '\|'
+\ . '\%(-\@<!-\|+\@<!+\|<\|[-=]\@<!>\|\*\|/\@<!/\|%\||\|'
+\ . '&\|,\|\.\@<!\.\)$'
+
+" Operators that block continuation indenting
+let s:CONTINUATION_BLOCK = '[([{:=]$'
+
+" A continuation dot access
+let s:DOT_ACCESS = '^\.'
+
+" Keywords to outdent after
+let s:OUTDENT_AFTER = '^\%(return\|break\|continue\|throw\)\>'
+
+" A compound assignment like `... = if ...`
+let s:COMPOUND_ASSIGNMENT = '[:=]\s*\%(if\|unless\|for\|while\|until\|'
+\ . 'switch\|try\|class\)\>'
+
+" A postfix condition like `return ... if ...`.
+let s:POSTFIX_CONDITION = '\S\s\+\zs\<\%(if\|unless\)\>'
+
+" A single line else statement like `else ...` but not `else if ...`
+let s:SINGLE_LINE_ELSE = '^else\s\+\%(\<\%(if\|unless\)\>\)\@!'
+
+" Max lines to look back for a match
+let s:MAX_LOOKBACK = 50
+
+" Syntax names for strings
+let s:SYNTAX_STRING = 'coffee\%(String\|AssignString\|Embed\|Regex\|Heregex\|'
+\ . 'Heredoc\)'
+
+" Syntax names for comments
+let s:SYNTAX_COMMENT = 'coffee\%(Comment\|BlockComment\|HeregexComment\)'
+
+" Syntax names for strings and comments
+let s:SYNTAX_STRING_COMMENT = s:SYNTAX_STRING . '\|' . s:SYNTAX_COMMENT
+
+" Get the linked syntax name of a character.
+function! s:SyntaxName(linenum, col)
+ return synIDattr(synID(a:linenum, a:col, 1), 'name')
+endfunction
+
+" Check if a character is in a comment.
+function! s:IsComment(linenum, col)
+ return s:SyntaxName(a:linenum, a:col) =~ s:SYNTAX_COMMENT
+endfunction
+
+" Check if a character is in a string.
+function! s:IsString(linenum, col)
+ return s:SyntaxName(a:linenum, a:col) =~ s:SYNTAX_STRING
+endfunction
+
+" Check if a character is in a comment or string.
+function! s:IsCommentOrString(linenum, col)
+ return s:SyntaxName(a:linenum, a:col) =~ s:SYNTAX_STRING_COMMENT
+endfunction
+
+" Check if a whole line is a comment.
+function! s:IsCommentLine(linenum)
+ " Check the first non-whitespace character.
+ return s:IsComment(a:linenum, indent(a:linenum) + 1)
+endfunction
+
+" Search a line for a regex until one is found outside a string or comment.
+function! s:SmartSearch(linenum, regex)
+ " Start at the first column.
+ let col = 0
+
+ " Search until there are no more matches, unless a good match is found.
+ while 1
+ call cursor(a:linenum, col + 1)
+ let [_, col] = searchpos(a:regex, 'cn', a:linenum)
+
+ " No more matches.
+ if !col
+ break
+ endif
+
+ if !s:IsCommentOrString(a:linenum, col)
+ return 1
+ endif
+ endwhile
+
+ " No good match found.
+ return 0
+endfunction
+
+" Check if a match should be skipped.
+function! s:ShouldSkip(startlinenum, linenum, col)
+ " Skip if in a comment or string.
+ if s:IsCommentOrString(a:linenum, a:col)
+ return 1
+ endif
+
+ " Skip if a single line statement that isn't adjacent.
+ if s:SmartSearch(a:linenum, '\<then\>') && a:startlinenum - a:linenum > 1
+ return 1
+ endif
+
+ " Skip if a postfix condition.
+ if s:SmartSearch(a:linenum, s:POSTFIX_CONDITION) &&
+ \ !s:SmartSearch(a:linenum, s:COMPOUND_ASSIGNMENT)
+ return 1
+ endif
+
+ return 0
+endfunction
+
+" Find the farthest line to look back to, capped to line 1 (zero and negative
+" numbers cause bad things).
+function! s:MaxLookback(startlinenum)
+ return max([1, a:startlinenum - s:MAX_LOOKBACK])
+endfunction
+
+" Get the skip expression for searchpair().
+function! s:SkipExpr(startlinenum)
+ return "s:ShouldSkip(" . a:startlinenum . ", line('.'), col('.'))"
+endfunction
+
+" Search for pairs of text.
+function! s:SearchPair(start, end)
+ " The cursor must be in the first column for regexes to match.
+ call cursor(0, 1)
+
+ let startlinenum = line('.')
+
+ " Don't need the W flag since MaxLookback caps the search to line 1.
+ return searchpair(a:start, '', a:end, 'bcn',
+ \ s:SkipExpr(startlinenum),
+ \ s:MaxLookback(startlinenum))
+endfunction
+
+" Try to find a previous matching line.
+function! s:GetMatch(curline)
+ let firstchar = a:curline[0]
+
+ if firstchar == '}'
+ return s:SearchPair('{', '}')
+ elseif firstchar == ')'
+ return s:SearchPair('(', ')')
+ elseif firstchar == ']'
+ return s:SearchPair('\[', '\]')
+ elseif a:curline =~ '^else\>'
+ return s:SearchPair('\<\%(if\|unless\|when\)\>', '\<else\>')
+ elseif a:curline =~ '^catch\>'
+ return s:SearchPair('\<try\>', '\<catch\>')
+ elseif a:curline =~ '^finally\>'
+ return s:SearchPair('\<try\>', '\<finally\>')
+ endif
+
+ return 0
+endfunction
+
+" Get the nearest previous line that isn't a comment.
+function! s:GetPrevNormalLine(startlinenum)
+ let curlinenum = a:startlinenum
+
+ while curlinenum
+ let curlinenum = prevnonblank(curlinenum - 1)
+
+ if !s:IsCommentLine(curlinenum)
+ return curlinenum
+ endif
+ endwhile
+
+ return 0
+endfunction
+
+" Try to find a comment in a line.
+function! s:FindComment(linenum)
+ call cursor(a:linenum, 0)
+
+ " Current column
+ let cur = 0
+ " Last column in the line
+ let end = col('$') - 1
+
+ while cur != end
+ call cursor(0, cur + 1)
+ let [_, cur] = searchpos('#', 'cn', a:linenum)
+
+ if !cur
+ break
+ endif
+
+ if s:IsComment(a:linenum, cur)
+ return cur
+ endif
+ endwhile
+
+ return 0
+endfunction
+
+" Get a line without comments or surrounding whitespace.
+function! s:GetTrimmedLine(linenum)
+ let comment = s:FindComment(a:linenum)
+ let line = getline(a:linenum)
+
+ if comment
+ " Subtract 1 to get to the column before the comment and another 1 for
+ " zero-based indexing.
+ let line = line[:comment - 2]
+ endif
+
+ return substitute(substitute(line, '^\s\+', '', ''),
+ \ '\s\+$', '', '')
+endfunction
+
+function! GetCoffeeIndent(curlinenum)
+ " Don't do anything if on the first line.
+ if a:curlinenum == 1
+ return -1
+ endif
+
+ let prevlinenum = a:curlinenum - 1
+
+ " If continuing a comment, keep the indent level.
+ if s:IsCommentLine(prevlinenum)
+ return indent(prevlinenum)
+ endif
+
+ let prevlinenum = s:GetPrevNormalLine(a:curlinenum)
+
+ " Don't do anything if there's no code before.
+ if !prevlinenum
+ return -1
+ endif
+
+ " Indent based on the current line.
+ let curline = s:GetTrimmedLine(a:curlinenum)
+
+ " Try to find a matching statement. This handles outdenting.
+ let matchlinenum = s:GetMatch(curline)
+
+ if matchlinenum
+ return indent(matchlinenum)
+ endif
+
+ " Try to find a matching when.
+ if curline =~ '^when\>' && !s:SmartSearch(prevlinenum, '\<switch\>')
+ let linenum = a:curlinenum
+
+ while linenum
+ let linenum = s:GetPrevNormalLine(linenum)
+
+ if getline(linenum) =~ '^\s*when\>'
+ return indent(linenum)
+ endif
+ endwhile
+
+ return -1
+ endif
+
+ " Indent based on the previous line.
+ let prevline = s:GetTrimmedLine(prevlinenum)
+ let previndent = indent(prevlinenum)
+
+ " Always indent after these operators.
+ if prevline =~ s:INDENT_AFTER_OPERATOR
+ return previndent + &shiftwidth
+ endif
+
+ " Indent after a continuation if it's the first.
+ if prevline =~ s:CONTINUATION
+ " If the line ends in a slash, make sure it isn't a regex.
+ if prevline =~ '/$'
+ " Move to the line so we can get the last column.
+ call cursor(prevlinenum)
+
+ if s:IsString(prevlinenum, col('$') - 1)
+ return -1
+ endif
+ endif
+
+ let prevprevlinenum = s:GetPrevNormalLine(prevlinenum)
+
+ " If the continuation is the first in the file, there can't be others before
+ " it.
+ if !prevprevlinenum
+ return previndent + &shiftwidth
+ endif
+
+ let prevprevline = s:GetTrimmedLine(prevprevlinenum)
+
+ " Only indent after the first continuation.
+ if prevprevline !~ s:CONTINUATION && prevprevline !~ s:CONTINUATION_BLOCK
+ return previndent + &shiftwidth
+ endif
+
+ return -1
+ endif
+
+ " Indent after these keywords and compound assignments if they aren't a
+ " single line statement.
+ if prevline =~ s:INDENT_AFTER_KEYWORD || prevline =~ s:COMPOUND_ASSIGNMENT
+ if !s:SmartSearch(prevlinenum, '\<then\>') && prevline !~ s:SINGLE_LINE_ELSE
+ return previndent + &shiftwidth
+ endif
+
+ return -1
+ endif
+
+ " Indent a dot access if it's the first.
+ if curline =~ s:DOT_ACCESS && prevline !~ s:DOT_ACCESS
+ return previndent + &shiftwidth
+ endif
+
+ " Outdent after these keywords if they don't have a postfix condition or are
+ " a single-line statement.
+ if prevline =~ s:OUTDENT_AFTER
+ if !s:SmartSearch(prevlinenum, s:POSTFIX_CONDITION) ||
+ \ s:SmartSearch(prevlinenum, '\<then\>')
+ return previndent - &shiftwidth
+ endif
+ endif
+
+ " If no indent or outdent is needed, keep the indent level of the previous
+ " line.
+ return previndent
+endfunction
View
221 syntax/coffee.vim
@@ -0,0 +1,221 @@
+" Language: CoffeeScript
+" Maintainer: Mick Koch <kchmck@gmail.com>
+" URL: http://github.com/kchmck/vim-coffee-script
+" License: WTFPL
+
+" Bail if our syntax is already loaded.
+if exists('b:current_syntax') && b:current_syntax == 'coffee'
+ finish
+endif
+
+" Include JavaScript for coffeeEmbed.
+syn include @coffeeJS syntax/javascript.vim
+
+" Highlight long strings.
+syn sync minlines=100
+
+" CoffeeScript identifiers can have dollar signs.
+setlocal isident+=$
+
+" These are `matches` instead of `keywords` because vim's highlighting
+" priority for keywords is higher than matches. This causes keywords to be
+" highlighted inside matches, even if a match says it shouldn't contain them --
+" like with coffeeAssign and coffeeDot.
+syn match coffeeStatement /\<\%(return\|break\|continue\|throw\)\>/ display
+hi def link coffeeStatement Statement
+
+syn match coffeeRepeat /\<\%(for\|while\|until\|loop\)\>/ display
+hi def link coffeeRepeat Repeat
+
+syn match coffeeConditional /\<\%(if\|else\|unless\|switch\|when\|then\)\>/
+\ display
+hi def link coffeeConditional Conditional
+
+syn match coffeeException /\<\%(try\|catch\|finally\)\>/ display
+hi def link coffeeException Exception
+
+syn match coffeeKeyword /\<\%(new\|in\|of\|by\|and\|or\|not\|is\|isnt\|class\|extends\|super\|do\)\>/
+\ display
+" The `own` keyword is only a keyword after `for`.
+syn match coffeeKeyword /\<for\s\+own\>/ contained containedin=coffeeRepeat
+\ display
+hi def link coffeeKeyword Keyword
+
+syn match coffeeOperator /\<\%(instanceof\|typeof\|delete\)\>/ display
+hi def link coffeeOperator Operator
+
+" The first case matches symbol operators only if they have an operand before.
+syn match coffeeExtendedOp /\%(\S\s*\)\@<=[+\-*/%&|\^=!<>?.]\{-1,}\|[-=]>\|--\|++\|:/
+\ display
+syn match coffeeExtendedOp /\<\%(and\|or\)=/ display
+hi def link coffeeExtendedOp coffeeOperator
+
+" This is separate from `coffeeExtendedOp` to help differentiate commas from
+" dots.
+syn match coffeeSpecialOp /[,;]/ display
+hi def link coffeeSpecialOp SpecialChar
+
+syn match coffeeBoolean /\<\%(true\|on\|yes\|false\|off\|no\)\>/ display
+hi def link coffeeBoolean Boolean
+
+syn match coffeeGlobal /\<\%(null\|undefined\)\>/ display
+hi def link coffeeGlobal Type
+
+" A special variable
+syn match coffeeSpecialVar /\<\%(this\|prototype\|arguments\)\>/ display
+hi def link coffeeSpecialVar Special
+
+" An @-variable
+syn match coffeeSpecialIdent /@\%(\I\i*\)\?/ display
+hi def link coffeeSpecialIdent Identifier
+
+" A class-like name that starts with a capital letter
+syn match coffeeObject /\<\u\w*\>/ display
+hi def link coffeeObject Structure
+
+" A constant-like name in SCREAMING_CAPS
+syn match coffeeConstant /\<\u[A-Z0-9_]\+\>/ display
+hi def link coffeeConstant Constant
+
+" A variable name
+syn cluster coffeeIdentifier contains=coffeeSpecialVar,coffeeSpecialIdent,
+\ coffeeObject,coffeeConstant
+
+" A non-interpolated string
+syn cluster coffeeBasicString contains=@Spell,coffeeEscape
+" An interpolated string
+syn cluster coffeeInterpString contains=@coffeeBasicString,coffeeInterp
+
+" Regular strings
+syn region coffeeString start=/"/ skip=/\\\\\|\\"/ end=/"/
+\ contains=@coffeeInterpString
+syn region coffeeString start=/'/ skip=/\\\\\|\\'/ end=/'/
+\ contains=@coffeeBasicString
+hi def link coffeeString String
+
+" A integer, including a leading plus or minus
+syn match coffeeNumber /\i\@<![-+]\?\d\+\%([eE][+-]\?\d\+\)\?/ display
+" A hex, binary, or octal number
+syn match coffeeNumber /\<0[xX]\x\+\>/ display
+syn match coffeeNumber /\<0[bB][01]\+\>/ display
+syn match coffeeNumber /\<0[oO][0-7]\+\>/ display
+hi def link coffeeNumber Number
+
+" A floating-point number, including a leading plus or minus
+syn match coffeeFloat /\i\@<![-+]\?\d*\.\@<!\.\d\+\%([eE][+-]\?\d\+\)\?/
+\ display
+hi def link coffeeFloat Float
+
+" An error for reserved keywords
+if !exists("coffee_no_reserved_words_error")
+ syn match coffeeReservedError /\<\%(case\|default\|function\|var\|void\|with\|const\|let\|enum\|export\|import\|native\|__hasProp\|__extends\|__slice\|__bind\|__indexOf\|implements\|interface\|let\|package\|private\|protected\|public\|static\|yield\)\>/
+ \ display
+ hi def link coffeeReservedError Error
+endif
+
+" A normal object assignment
+syn match coffeeObjAssign /@\?\I\i*\s*\ze::\@!/ contains=@coffeeIdentifier display
+hi def link coffeeObjAssign Identifier
+
+syn keyword coffeeTodo TODO FIXME XXX contained
+hi def link coffeeTodo Todo
+
+syn match coffeeComment /#.*/ contains=@Spell,coffeeTodo
+hi def link coffeeComment Comment
+
+syn region coffeeBlockComment start=/####\@!/ end=/###/
+\ contains=@Spell,coffeeTodo
+hi def link coffeeBlockComment coffeeComment
+
+" A comment in a heregex
+syn region coffeeHeregexComment start=/#/ end=/\ze\/\/\/\|$/ contained
+\ contains=@Spell,coffeeTodo
+hi def link coffeeHeregexComment coffeeComment
+
+" Embedded JavaScript
+syn region coffeeEmbed matchgroup=coffeeEmbedDelim
+\ start=/`/ skip=/\\\\\|\\`/ end=/`/
+\ contains=@coffeeJS
+hi def link coffeeEmbedDelim Delimiter
+
+syn region coffeeInterp matchgroup=coffeeInterpDelim start=/#{/ end=/}/ contained
+\ contains=@coffeeAll
+hi def link coffeeInterpDelim PreProc
+
+" A string escape sequence
+syn match coffeeEscape /\\\d\d\d\|\\x\x\{2\}\|\\u\x\{4\}\|\\./ contained display
+hi def link coffeeEscape SpecialChar
+
+" A regex -- must not follow a parenthesis, number, or identifier, and must not
+" be followed by a number
+syn region coffeeRegex start=/\%(\%()\|\i\@<!\d\)\s*\|\i\)\@<!\/=\@!\s\@!/
+\ skip=/\[[^\]]\{-}\/[^\]]\{-}\]/
+\ end=/\/[gimy]\{,4}\d\@!/
+\ oneline contains=@coffeeBasicString
+hi def link coffeeRegex String
+
+" A heregex
+syn region coffeeHeregex start=/\/\/\// end=/\/\/\/[gimy]\{,4}/
+\ contains=@coffeeInterpString,coffeeHeregexComment
+\ fold
+hi def link coffeeHeregex coffeeRegex
+
+" Heredoc strings
+syn region coffeeHeredoc start=/"""/ end=/"""/ contains=@coffeeInterpString
+\ fold
+syn region coffeeHeredoc start=/'''/ end=/'''/ contains=@coffeeBasicString
+\ fold
+hi def link coffeeHeredoc String
+
+" An error for trailing whitespace, as long as the line isn't just whitespace
+if !exists("coffee_no_trailing_space_error")
+ syn match coffeeSpaceError /\S\@<=\s\+$/ display
+ hi def link coffeeSpaceError Error
+endif
+
+" An error for trailing semicolons, for help transitioning from JavaScript
+if !exists("coffee_no_trailing_semicolon_error")
+ syn match coffeeSemicolonError /;$/ display
+ hi def link coffeeSemicolonError Error
+endif
+
+" Ignore reserved words in dot accesses.
+syn match coffeeDotAccess /\.\@<!\.\s*\I\i*/he=s+1 contains=@coffeeIdentifier
+hi def link coffeeDotAccess coffeeExtendedOp
+
+" Ignore reserved words in prototype accesses.
+syn match coffeeProtoAccess /::\s*\I\i*/he=s+2 contains=@coffeeIdentifier
+hi def link coffeeProtoAccess coffeeExtendedOp
+
+" This is required for interpolations to work.
+syn region coffeeCurlies matchgroup=coffeeCurly start=/{/ end=/}/
+\ contains=@coffeeAll
+syn region coffeeBrackets matchgroup=coffeeBracket start=/\[/ end=/\]/
+\ contains=@coffeeAll
+syn region coffeeParens matchgroup=coffeeParen start=/(/ end=/)/
+\ contains=@coffeeAll
+
+" These are highlighted the same as commas since they tend to go together.
+hi def link coffeeBlock coffeeSpecialOp
+hi def link coffeeBracket coffeeBlock
+hi def link coffeeCurly coffeeBlock
+hi def link coffeeParen coffeeBlock
+
+" This is used instead of TOP to keep things coffee-specific for good
+" embedding. `contained` groups aren't included.
+syn cluster coffeeAll contains=coffeeStatement,coffeeRepeat,coffeeConditional,
+\ coffeeException,coffeeKeyword,coffeeOperator,
+\ coffeeExtendedOp,coffeeSpecialOp,coffeeBoolean,
+\ coffeeGlobal,coffeeSpecialVar,coffeeSpecialIdent,
+\ coffeeObject,coffeeConstant,coffeeString,
+\ coffeeNumber,coffeeFloat,coffeeReservedError,
+\ coffeeObjAssign,coffeeComment,coffeeBlockComment,
+\ coffeeEmbed,coffeeRegex,coffeeHeregex,
+\ coffeeHeredoc,coffeeSpaceError,
+\ coffeeSemicolonError,coffeeDotAccess,
+\ coffeeProtoAccess,coffeeCurlies,coffeeBrackets,
+\ coffeeParens
+
+if !exists('b:current_syntax')
+ let b:current_syntax = 'coffee'
+endif
View
62 syntax/eco.vim
@@ -0,0 +1,62 @@
+" Vim syntax file
+" Language: eco
+" Maintainer: Jay Adkisson
+" Mostly stolen from eruby.vim
+
+if !exists("g:eco_default_subtype")
+ let g:eco_default_subtype = "html"
+endif
+
+if !exists("b:eco_subtype")
+ let s:lines = getline(1)."\n".getline(2)."\n".getline(3)."\n".getline(4)."\n".getline(5)."\n".getline("$")
+ let b:eco_subtype = matchstr(s:lines,'eco_subtype=\zs\w\+')
+ if b:eco_subtype == ''
+ let b:eco_subtype = matchstr(substitute(expand("%:t"),'\c\%(\.eco\)\+$','',''),'\.\zs\w\+$')
+ endif
+ if b:eco_subtype == 'rhtml'
+ let b:eco_subtype = 'html'
+ elseif b:eco_subtype == 'jst'
+ let b:eco_subtype = 'html'
+ elseif b:eco_subtype == 'rb'
+ let b:eco_subtype = 'ruby'
+ elseif b:eco_subtype == 'yml'
+ let b:eco_subtype = 'yaml'
+ elseif b:eco_subtype == 'js' || b:eco_subtype == 'json'
+ let b:eco_subtype = 'javascript'
+ elseif b:eco_subtype == 'txt'
+ " Conventional; not a real file type
+ let b:eco_subtype = 'text'
+ elseif b:eco_subtype == ''
+ if exists('b:current_syntax') && b:current_syntax != ''
+ let b:eco_subtype = b:current_syntax
+ else
+ let b:eco_subtype = g:eco_default_subtype
+ endif
+ endif
+endif
+
+if exists("b:eco_subtype") && b:eco_subtype != '' && b:eco_subtype != 'eco'
+ exec "runtime! syntax/".b:eco_subtype.".vim"
+ syn include @coffeeTop syntax/coffee.vim
+endif
+
+syn cluster ecoRegions contains=ecoBlock,ecoExpression,ecoComment
+
+syn region ecoBlock matchgroup=ecoDelimiter start=/<%/ end=/%>/ contains=@coffeeTop containedin=ALLBUT,@ecoRegions keepend
+syn region ecoExpression matchgroup=ecoDelimiter start=/<%[=\-]/ end=/%>/ contains=@coffeeTop containedin=ALLBUT,@ecoRegions keepend
+syn region ecoComment matchgroup=ecoComment start=/<%#/ end=/%>/ contains=@coffeeTodo,@Spell containedin=ALLBUT,@ecoRegions keepend
+
+" eco features not in coffeescript proper
+syn keyword ecoEnd end containedin=@ecoRegions
+syn match ecoIndentColon /\s+\w+:/ containedin=@ecoRegions
+
+" Define the default highlighting.
+
+hi def link ecoDelimiter Delimiter
+hi def link ecoComment Comment
+hi def link ecoEnd coffeeConditional
+hi def link ecoIndentColon None
+
+let b:current_syntax = 'eco'
+
+" vim: nowrap sw=2 sts=2 ts=8:
Please sign in to comment.
Something went wrong with that request. Please try again.