]> git.donarmstrong.com Git - dactyl.git/blob - common/javascript.vim
Import 1.0b7.1 supporting Firefox up to 8.*
[dactyl.git] / common / javascript.vim
1 " Vim syntax file
2 " Language:     JavaScript
3 " Maintainer:   Yi Zhao (ZHAOYI) <zzlinux AT hotmail DOT com>
4 " Last Change:  May 17, 2007
5 " Version:      0.7.5
6 " Changes:      1, Get the vimdiff problem fixed finally.
7 "                Matthew Gallant reported the problem and test the fix. ;)
8 "               2, Follow the suggestioin from Ingo Karkat.
9 "                The 'foldtext' and 'foldlevel' settings should only be
10 "                changed if the file being edited is pure JavaScript,
11 "                not if JavaScript syntax is embedded inside other syntaxes.
12 "               3, Remove function FT_JavaScriptDoc().
13 "                Since VIM do the better than me.
14 "
15 " TODO:
16 "  - Add the HTML syntax inside the JSDoc
17
18 if !exists("main_syntax")
19   if version < 600
20     syntax clear
21   elseif exists("b:current_syntax")
22     finish
23   endif
24   let main_syntax = 'javascript'
25 endif
26
27 "" Drop fold if it set but VIM doesn't support it.
28 let b:javascript_fold='true'
29 if version < 600    " Don't support the old version
30   unlet! b:javascript_fold
31 endif
32
33 syn include @xmlTop syntax/xml.vim
34 unlet b:current_syntax
35
36 syn include @cssTop syntax/css.vim
37 unlet b:current_syntax
38
39 "" dollar sigh is permittd anywhere in an identifier
40 setlocal iskeyword+=$
41
42 syntax sync fromstart
43 syntax sync maxlines=200
44
45 "" JavaScript comments
46 syntax keyword javaScriptCommentTodo    TODO FIXME XXX TBD contained
47 syntax region  javaScriptLineComment    start=+\/\/+ end="\v$|(\</?(css|e4x)\>)@=" keepend contains=javaScriptCommentTodo,@Spell
48 syntax region  javaScriptLineComment    start=+^\s*\/\/+ skip=+\n\s*\/\/+ end="\v$|(\</?(css|e4x)\>)@=" keepend contains=javaScriptCommentTodo,@Spell fold
49 syntax region  javaScriptCvsTag         start="\$\cid:" end="\$" oneline contained
50 syntax region  javaScriptComment        start="/\*"  end="\v\*/|(\</?(css|e4x)\>)@=" contains=javaScriptCommentTodo,javaScriptCvsTag,@Spell fold
51
52 "" JSDoc support start
53 if !exists("javascript_ignore_javaScriptdoc")
54   syntax case ignore
55
56   "" syntax coloring for javadoc comments (HTML)
57   "syntax include @javaHtml <sfile>:p:h/html.vim
58   "unlet b:current_syntax
59
60   syntax region javaScriptDocComment    matchgroup=javaScriptComment start="/\*\*\s*$"  end="\*/" contains=javaScriptDocTags,javaScriptCommentTodo,javaScriptCvsTag,@javaScriptHtml,@Spell fold
61   syntax match  javaScriptDocTags       contained "@\(param\|argument\|requires\|exception\|throws\|type\|class\|extends\|see\|link\|member\|module\|method\|title\|namespace\|optional\|default\|base\|file\)\>" nextgroup=javaScriptDocParam,javaScriptDocSeeTag skipwhite
62   syntax match  javaScriptDocTags       contained "@\(beta\|deprecated\|description\|fileoverview\|author\|license\|version\|returns\=\|constructor\|private\|protected\|final\|ignore\|addon\|exec\)\>"
63   syntax match  javaScriptDocParam      contained "\%(#\|\w\|\.\|:\|\/\)\+"
64   syntax region javaScriptDocSeeTag     contained matchgroup=javaScriptDocSeeTag start="{" end="}" contains=javaScriptDocTags
65
66   syntax case match
67 endif   "" JSDoc end
68
69 syntax case match
70
71 "" Syntax in the JavaScript code
72 syntax match   javaScriptSpecial        "\\\d\d\d\|\\x\x\{2\}\|\\u\x\{4\}\|\\."
73 syntax region  javaScriptStringD        start=+"+  skip=+\\\\\|\\$"+  end=+"+  contains=javaScriptSpecial,@htmlPreproc
74 syntax region  javaScriptStringS        start=+'+  skip=+\\\\\|\\$'+  end=+'+  contains=javaScriptSpecial,@htmlPreproc
75 syntax region  javaScriptRegexpString   start=+/\(\*\|/\)\@!+ skip=+\\\\\|\\/+ end=+/[gim]\{-,3}+ contains=javaScriptSpecial,@htmlPreproc oneline
76 syntax match   javaScriptNumber         /\<-\=\d\+L\=\>\|\<0[xX]\x\+\>/
77 syntax match   javaScriptFloat          /\<-\=\%(\d\+\.\d\+\|\d\+\.\|\.\d\+\)\%([eE][+-]\=\d\+\)\=\>/
78 syntax match   javaScriptLabel          /\(?\s*\)\@<!\<\w\+\(\s*:\)\@=/
79
80 "" JavaScript Prototype
81 syntax keyword javaScriptPrototype      prototype
82
83 "" Programm Keywords
84 syntax keyword javaScriptSource         import export
85 syntax keyword javaScriptType           const this var void yield arguments
86 syntax keyword javaScriptOperator       delete new in instanceof let typeof
87 syntax keyword javaScriptBoolean        true false
88 syntax keyword javaScriptNull           null
89
90 "" Statement Keywords
91 syntax keyword javaScriptConditional    if else
92 syntax keyword javaScriptRepeat         do while for
93 syntax keyword javaScriptBranch         break continue switch case default return
94 syntax keyword javaScriptStatement      try catch throw with finally
95
96 syntax keyword javaScriptGlobalObjects  Array Boolean Date Function Infinity JavaArray JavaClass JavaObject JavaPackage Math Number NaN Object Packages RegExp String Undefined java netscape sun
97
98 syntax keyword javaScriptExceptions     Error EvalError RangeError ReferenceError SyntaxError TypeError URIError
99
100 syntax keyword javaScriptFutureKeys     abstract enum int short boolean export interface static byte extends long super char final native synchronized class float package throws goto private transient debugger implements protected volatile double import public
101
102 "" DOM/HTML/CSS specified things
103
104   " DOM2 Objects
105   syntax keyword javaScriptGlobalObjects  DOMImplementation DocumentFragment Document Node NodeList NamedNodeMap CharacterData Attr Element Text Comment CDATASection DocumentType Notation Entity EntityReference ProcessingInstruction
106   syntax keyword javaScriptExceptions     DOMException
107
108   " DOM2 CONSTANT
109   syntax keyword javaScriptDomErrNo       INDEX_SIZE_ERR DOMSTRING_SIZE_ERR HIERARCHY_REQUEST_ERR WRONG_DOCUMENT_ERR INVALID_CHARACTER_ERR NO_DATA_ALLOWED_ERR NO_MODIFICATION_ALLOWED_ERR NOT_FOUND_ERR NOT_SUPPORTED_ERR INUSE_ATTRIBUTE_ERR INVALID_STATE_ERR SYNTAX_ERR INVALID_MODIFICATION_ERR NAMESPACE_ERR INVALID_ACCESS_ERR
110   syntax keyword javaScriptDomNodeConsts  ELEMENT_NODE ATTRIBUTE_NODE TEXT_NODE CDATA_SECTION_NODE ENTITY_REFERENCE_NODE ENTITY_NODE PROCESSING_INSTRUCTION_NODE COMMENT_NODE DOCUMENT_NODE DOCUMENT_TYPE_NODE DOCUMENT_FRAGMENT_NODE NOTATION_NODE
111
112   " HTML events and internal variables
113   syntax case ignore
114   syntax keyword javaScriptHtmlEvents     onblur onclick oncontextmenu ondblclick onfocus onkeydown onkeypress onkeyup onmousedown onmousemove onmouseout onmouseover onmouseup onresize
115   syntax case match
116
117 " Follow stuff should be highligh within a special context
118 " While it can't be handled with context depended with Regex based highlight
119 " So, turn it off by default
120 if exists("javascript_enable_domhtmlcss")
121
122     " DOM2 things
123     syntax match javaScriptDomElemAttrs     contained /\%(nodeName\|nodeValue\|nodeType\|parentNode\|childNodes\|firstChild\|lastChild\|previousSibling\|nextSibling\|attributes\|ownerDocument\|namespaceURI\|prefix\|localName\|tagName\)\>/
124     syntax match javaScriptDomElemFuncs     contained /\%(insertBefore\|replaceChild\|removeChild\|appendChild\|hasChildNodes\|cloneNode\|normalize\|isSupported\|hasAttributes\|getAttribute\|setAttribute\|removeAttribute\|getAttributeNode\|setAttributeNode\|removeAttributeNode\|getElementsByTagName\|getAttributeNS\|setAttributeNS\|removeAttributeNS\|getAttributeNodeNS\|setAttributeNodeNS\|getElementsByTagNameNS\|hasAttribute\|hasAttributeNS\)\>/ nextgroup=javaScriptParen skipwhite
125     " HTML things
126     syntax match javaScriptHtmlElemAttrs    contained /\%(className\|clientHeight\|clientLeft\|clientTop\|clientWidth\|dir\|id\|innerHTML\|lang\|length\|offsetHeight\|offsetLeft\|offsetParent\|offsetTop\|offsetWidth\|scrollHeight\|scrollLeft\|scrollTop\|scrollWidth\|style\|tabIndex\|title\)\>/
127     syntax match javaScriptHtmlElemFuncs    contained /\%(blur\|click\|focus\|scrollIntoView\|addEventListener\|dispatchEvent\|removeEventListener\|item\)\>/ nextgroup=javaScriptParen skipwhite
128
129     " CSS Styles in JavaScript
130     syntax keyword javaScriptCssStyles      contained color font fontFamily fontSize fontSizeAdjust fontStretch fontStyle fontVariant fontWeight letterSpacing lineBreak lineHeight quotes rubyAlign rubyOverhang rubyPosition
131     syntax keyword javaScriptCssStyles      contained textAlign textAlignLast textAutospace textDecoration textIndent textJustify textJustifyTrim textKashidaSpace textOverflowW6 textShadow textTransform textUnderlinePosition
132     syntax keyword javaScriptCssStyles      contained unicodeBidi whiteSpace wordBreak wordSpacing wordWrap writingMode
133     syntax keyword javaScriptCssStyles      contained bottom height left position right top width zIndex
134     syntax keyword javaScriptCssStyles      contained border borderBottom borderLeft borderRight borderTop borderBottomColor borderLeftColor borderTopColor borderBottomStyle borderLeftStyle borderRightStyle borderTopStyle borderBottomWidth borderLeftWidth borderRightWidth borderTopWidth borderColor borderStyle borderWidth borderCollapse borderSpacing captionSide emptyCells tableLayout
135     syntax keyword javaScriptCssStyles      contained margin marginBottom marginLeft marginRight marginTop outline outlineColor outlineStyle outlineWidth padding paddingBottom paddingLeft paddingRight paddingTop
136     syntax keyword javaScriptCssStyles      contained listStyle listStyleImage listStylePosition listStyleType
137     syntax keyword javaScriptCssStyles      contained background backgroundAttachment backgroundColor backgroundImage gackgroundPosition backgroundPositionX backgroundPositionY backgroundRepeat
138     syntax keyword javaScriptCssStyles      contained clear clip clipBottom clipLeft clipRight clipTop content counterIncrement counterReset cssFloat cursor direction display filter layoutGrid layoutGridChar layoutGridLine layoutGridMode layoutGridType
139     syntax keyword javaScriptCssStyles      contained marks maxHeight maxWidth minHeight minWidth opacity MozOpacity overflow overflowX overflowY verticalAlign visibility zoom cssText
140     syntax keyword javaScriptCssStyles      contained scrollbar3dLightColor scrollbarArrowColor scrollbarBaseColor scrollbarDarkShadowColor scrollbarFaceColor scrollbarHighlightColor scrollbarShadowColor scrollbarTrackColor
141
142     " Highlight ways
143     syntax match javaScriptDotNotation      "\." nextgroup=javaScriptPrototype,javaScriptDomElemAttrs,javaScriptDomElemFuncs,javaScriptHtmlElemAttrs,javaScriptHtmlElemFuncs
144     syntax match javaScriptDotNotation      "\.style\." nextgroup=javaScriptCssStyles
145
146 endif "DOM/HTML/CSS
147
148 "" end DOM/HTML/CSS specified things
149
150
151 "" Code blocks
152 syntax cluster javaScriptAll       contains=javaScriptComment,javaScriptLineComment,javaScriptDocComment,javaScriptStringD,javaScriptStringS,javaScriptRegexpString,javaScriptNumber,javaScriptFloat,javaScriptLabel,javaScriptSource,javaScriptType,javaScriptOperator,javaScriptBoolean,javaScriptNull,javaScriptFunction,javaScriptConditional,javaScriptRepeat,javaScriptBranch,javaScriptStatement,javaScriptGlobalObjects,javaScriptExceptions,javaScriptFutureKeys,javaScriptDomErrNo,javaScriptDomNodeConsts,javaScriptHtmlEvents,javaScriptDotNotation,javascriptE4X,javascriptCSS,javascriptCDATA
153 syntax region  javaScriptBracket   matchgroup=javaScriptBracket transparent start="\[" end="\]" contains=@javaScriptAll,javaScriptParensErrB,javaScriptParensErrC,javaScriptBracket,javaScriptParen,javaScriptBlock,@htmlPreproc
154 syntax region  javaScriptParen     matchgroup=javaScriptParen   transparent start="("  end=")"  contains=@javaScriptAll,javaScriptParensErrA,javaScriptParensErrC,javaScriptParen,javaScriptBracket,javaScriptBlock,@htmlPreproc
155 syntax region  javaScriptBlock     matchgroup=javaScriptBlock   transparent start="{"  end="}"  contains=@javaScriptAll,javaScriptParensErrA,javaScriptParensErrB,javaScriptParen,javaScriptBracket,javaScriptBlock,@htmlPreproc
156
157 syntax region  javascriptCDATA  matchgroup=javascriptCDATA start="<\!\[CDATA\[" end="\]\]>" keepend contains=javascriptCSS
158 syntax region  javascriptCSS    matchgroup=javascriptCSSDelimiter start="<css>" end="</css>" contains=@cssTop
159 syntax region  javascriptE4X    matchgroup=javascriptE4XDelimiter start="<e4x>" end="</e4x>" contains=@xmlTop
160 syntax region  javascriptE4X    matchgroup=javascriptE4XDelimiter start="<>" end="</>" contains=@xmlTop oneline
161
162 "" catch errors caused by wrong parenthesis
163 syntax match   javaScriptParensError    ")\|}\|\]"
164 syntax match   javaScriptParensErrA     contained "\]"
165 syntax match   javaScriptParensErrB     contained ")"
166 syntax match   javaScriptParensErrC     contained "}"
167
168 if main_syntax == "javascript"
169   syntax sync ccomment javaScriptComment
170 endif
171
172 "" Fold control
173 if exists("b:javascript_fold")
174     syntax match   javaScriptFunction       /\<function\>/ nextgroup=javaScriptFuncName skipwhite
175     syntax match   javaScriptOpAssign       /=\@<!=/ nextgroup=javaScriptFuncBlock skipwhite skipempty
176     syntax region  javaScriptFuncName       contained matchgroup=javaScriptFuncName start=/\%(\$\|\w\)*\s*(/ end=/)/ contains=javaScriptLineComment,javaScriptComment nextgroup=javaScriptFuncBlock skipwhite skipempty
177     syntax region  javaScriptFuncBlock      contained matchgroup=javaScriptFuncBlock start="{" end="}" contains=@javaScriptAll,javaScriptParensErrA,javaScriptParensErrB,javaScriptParen,javaScriptBracket,javaScriptBlock fold
178
179     if &l:filetype=='javascript' && !&diff
180       " Fold setting
181       " Redefine the foldtext (to show a JS function outline) and foldlevel
182       " only if the entire buffer is JavaScript, but not if JavaScript syntax
183       " is embedded in another syntax (e.g. HTML).
184       setlocal foldmethod=syntax
185       setlocal foldlevel=4
186     endif
187 else
188     syntax keyword javaScriptFunction       function
189     setlocal foldmethod<
190     setlocal foldlevel<
191 endif
192
193 " Define the default highlighting.
194 " For version 5.7 and earlier: only when not done already
195 " For version 5.8 and later: only when an item doesn't have highlighting yet
196 if version >= 508 || !exists("did_javascript_syn_inits")
197   if version < 508
198     let did_javascript_syn_inits = 1
199     command -nargs=+ HiLink hi link <args>
200   else
201     command -nargs=+ HiLink hi def link <args>
202   endif
203   HiLink javascriptCDATA                String
204   HiLink javaScriptComment              Comment
205   HiLink javaScriptLineComment          Comment
206   HiLink javaScriptDocComment           Comment
207   HiLink javaScriptCommentTodo          Todo
208   HiLink javaScriptCvsTag               Function
209   HiLink javaScriptDocTags              Special
210   HiLink javaScriptDocSeeTag            Function
211   HiLink javaScriptDocParam             Function
212   HiLink javaScriptStringS              String
213   HiLink javaScriptStringD              String
214   HiLink javaScriptRegexpString         String
215   HiLink javaScriptCharacter            Character
216   HiLink javaScriptPrototype            Type
217   HiLink javaScriptConditional          Conditional
218   HiLink javaScriptBranch               Conditional
219   HiLink javaScriptRepeat               Repeat
220   HiLink javaScriptStatement            Statement
221   HiLink javaScriptFunction             Function
222   HiLink javaScriptError                Error
223   HiLink javaScriptParensError          Error
224   HiLink javaScriptParensErrA           Error
225   HiLink javaScriptParensErrB           Error
226   HiLink javaScriptParensErrC           Error
227   HiLink javaScriptOperator             Operator
228   HiLink javaScriptType                 Type
229   HiLink javaScriptNull                 Type
230   HiLink javaScriptNumber               Number
231   HiLink javaScriptFloat                Number
232   HiLink javaScriptBoolean              Boolean
233   HiLink javaScriptLabel                Label
234   HiLink javaScriptSpecial              Special
235   HiLink javaScriptSource               Special
236   HiLink javaScriptGlobalObjects        Special
237   HiLink javaScriptExceptions           Special
238
239   HiLink javaScriptDomErrNo             Constant
240   HiLink javaScriptDomNodeConsts        Constant
241   HiLink javaScriptDomElemAttrs         Label
242   HiLink javaScriptDomElemFuncs         PreProc
243
244   HiLink javaScriptHtmlEvents           Special
245   HiLink javaScriptHtmlElemAttrs        Label
246   HiLink javaScriptHtmlElemFuncs        PreProc
247
248   HiLink javaScriptCssStyles            Label
249
250   delcommand HiLink
251 endif
252
253 " Define the htmlJavaScript for HTML syntax html.vim
254 "syntax clear htmlJavaScript
255 "syntax clear javaScriptExpression
256 syntax cluster  htmlJavaScript contains=@javaScriptAll,javaScriptBracket,javaScriptParen,javaScriptBlock,javaScriptParenError
257 syntax cluster  javaScriptExpression contains=@javaScriptAll,javaScriptBracket,javaScriptParen,javaScriptBlock,javaScriptParenError,@htmlPreproc
258
259 let b:current_syntax = "javascript"
260 if main_syntax == 'javascript'
261   unlet main_syntax
262 endif
263
264
265 if exists('b:did_indent')
266   finish
267 endif
268 let b:did_indent = 1
269
270 setlocal indentexpr=GetJsIndent()
271 setlocal indentkeys=0{,0},0),:,!^F,O,e,=*/
272 " Clean CR when the file is in Unix format
273 if &fileformat == "unix"
274     silent! %s/\r$//g
275 endif
276 " Only define the functions once per Vim session.
277 "if exists("*GetJsIndent")
278 "    finish
279 "endif
280 "function! GetJsIndent()
281 "    let pnum = prevnonblank(v:lnum - 1)
282 "    if pnum == 0
283 "       return 0
284 "    endif
285 "    let line = getline(v:lnum)
286 "    let pline = getline(pnum)
287 "    let ind = indent(pnum)
288 "
289 "    if pline =~ '{\s*$\|[\s*$\|(\s*$'
290 "       let ind = ind + &sw
291 "    endif
292 "
293 "    if pline =~ ';\s*$' && line =~ '^\s*}'
294 "        let ind = ind - &sw
295 "    endif
296 "
297 "    if pline =~ '\s*]\s*$' && line =~ '^\s*),\s*$'
298 "      let ind = ind - &sw
299 "    endif
300 "
301 "    if pline =~ '\s*]\s*$' && line =~ '^\s*}\s*$'
302 "      let ind = ind - &sw
303 "    endif
304 "
305 "    if line =~ '^\s*});\s*$\|^\s*);\s*$' && pline !~ ';\s*$'
306 "      let ind = ind - &sw
307 "    endif
308 "
309 "    if line =~ '^\s*})' && pline =~ '\s*,\s*$'
310 "      let ind = ind - &sw
311 "    endif
312 "
313 "    if line =~ '^\s*}();\s*$' && pline =~ '^\s*}\s*$'
314 "      let ind = ind - &sw
315 "    endif
316 "
317 "    if line =~ '^\s*}),\s*$'
318 "      let ind = ind - &sw
319 "    endif
320 "
321 "    if pline =~ '^\s*}\s*$' && line =~ '),\s*$'
322 "       let ind = ind - &sw
323 "    endif
324 "
325 "    if pline =~ '^\s*for\s*' && line =~ ')\s*$'
326 "       let ind = ind + &sw
327 "    endif
328 "
329 "    if line =~ '^\s*}\s*$\|^\s*]\s*$\|\s*},\|\s*]);\s*\|\s*}]\s*$\|\s*};\s*$\|\s*})$\|\s*}).el$' && pline !~ '\s*;\s*$\|\s*]\s*$' && line !~ '^\s*{' && line !~ '\s*{\s*}\s*'
330 "          let ind = ind - &sw
331 "    endif
332 "
333 "    if pline =~ '^\s*/\*'
334 "      let ind = ind + 1
335 "    endif
336 "
337 "    if pline =~ '\*/$'
338 "      let ind = ind - 1
339 "    endif
340 "    return ind
341 "endfunction
342
343 " vim: ts=4