2 " Language: Mathematica
3 " Maintainer: steve layland <layland@wolfram.com>
4 " Last Change: Thu May 19 21:36:04 CDT 2005
5 " Source: http://members.wri.com/layland/vim/syntax/mma.vim
6 " http://vim.sourceforge.net/scripts/script.php?script_id=1273
7 " Id: $Id: mma.vim,v 1.4 2006/04/14 20:40:38 vimboss Exp $
10 " Empty .m files will automatically be presumed as Matlab files
11 " unless you have the following in your .vimrc:
13 " let filetype_m="mma"
15 " I also recommend setting the default 'Comment' hilighting to something
16 " other than the color used for 'Function', since both are plentiful in
17 " most mathematica files, and they are often the same color (when using
21 " o Original Mathematica syntax version written by
22 " Wolfgang Waltenberger <wwalten@ben.tuwien.ac.at>
23 " o Some ideas like the CommentStar,CommentTitle were adapted
24 " from the Java vim syntax file by Claudio Fleiner. Thanks!
25 " o Everything else written by steve <layland@wolfram.com>
28 " o Vim 6.1 didn't really have support for character classes
29 " of other named character classes. For example, [\a\d]
30 " didn't work. Therefore, a lot of this code uses explicit
31 " character classes instead: [0-9a-zA-Z]
36 " finish populating popular symbols
40 elseif exists("b:current_syntax")
45 syntax cluster mmaNotes contains=mmaTodo,mmaFixme
46 syntax cluster mmaComments contains=mmaComment,mmaFunctionComment,mmaItem,mmaFunctionTitle,mmaCommentStar
47 syntax cluster mmaCommentStrings contains=mmaLooseQuote,mmaCommentString,mmaUnicode
48 syntax cluster mmaStrings contains=@mmaCommentStrings,mmaString
49 syntax cluster mmaTop contains=mmaOperator,mmaGenericFunction,mmaPureFunction,mmaVariable
51 " Predefined Constants:
52 " to list all predefined Symbols would be too insane...
53 " it's probably smarter to define a select few, and get the rest from
54 " context if absolutely necessary.
55 " TODO - populate this with other often used Symbols
57 " standard fixed symbols:
58 syntax keyword mmaVariable True False None Automatic All Null C General
60 " mathematical constants:
61 syntax keyword mmaVariable Pi I E Infinity ComplexInfinity Indeterminate GoldenRatio EulerGamma Degree Catalan Khinchin Glaisher
63 " stream data / atomic heads:
64 syntax keyword mmaVariable Byte Character Expression Number Real String Word EndOfFile Integer Symbol
67 syntax keyword mmaVariable Integers Complexes Reals Booleans Rationals
70 syntax keyword mmaPattern DigitCharacter LetterCharacter WhitespaceCharacter WordCharacter EndOfString StartOfString EndOfLine StartOfLine WordBoundary
72 " SelectionMove directions/units:
73 syntax keyword mmaVariable Next Previous After Before Character Word Expression TextLine CellContents Cell CellGroup EvaluationCell ButtonCell GeneratedCell Notebook
74 syntax keyword mmaVariable CellTags CellStyle CellLabel
76 " TableForm positions:
77 syntax keyword mmaVariable Above Below Left Right
80 syntax keyword mmaVariable Black Blue Brown Cyan Gray Green Magenta Orange Pink Purple Red White Yellow
83 syntax keyword mmaVariable Protected Listable OneIdentity Orderless Flat Constant NumericFunction Locked ReadProtected HoldFirst HoldRest HoldAll HoldAllComplete SequenceHold NHoldFirst NHoldRest NHoldAll Temporary Stub
88 syntax match mmaItem "\%(^[( |*\t]*\)\@<=\%(:\+\|\w\)\w\+\%( \w\+\)\{0,3}:" contained contains=@mmaNotes
91 syntax keyword mmaTodo TODO NOTE HEY contained
92 syntax match mmaTodo "X\{3,}" contained
93 syntax keyword mmaFixme FIX[ME] FIXTHIS BROKEN contained
94 syntax match mmaFixme "BUG\%( *\#\=[0-9]\+\)\=" contained
96 syntax match mmaFixme "\%(Y\=A\+R\+G\+\|GRR\+\|CR\+A\+P\+\)\%(!\+\)\=" contained
99 " this unnecessary, but whatever :)
100 syntax match mmaemPHAsis "\%(^\|\s\)\([_/]\)[a-zA-Z0-9]\+\%([- \t':]\+[a-zA-Z0-9]\+\)*\1\%(\s\|$\)" contained contains=mmaemPHAsis
101 syntax match mmaemPHAsis "\%(^\|\s\)(\@<!\*[a-zA-Z0-9]\+\%([- \t':]\+[a-zA-Z0-9]\+\)*)\@!\*\%(\s\|$\)" contained contains=mmaemPHAsis
105 " allow nesting (* (* *) *) even though the frontend
106 " won't always like it.
107 syntax region mmaComment start=+(\*+ end=+\*)+ skipempty contains=@mmaNotes,mmaItem,@mmaCommentStrings,mmaemPHAsis,mmaComment
110 " just like a normal comment except the first sentance is Special ala Java
112 " TODO - fix this for nesting, or not...
113 syntax region mmaFunctionComment start="(\*\*\+" end="\*\+)" contains=@mmaNotes,mmaItem,mmaFunctionTitle,@mmaCommentStrings,mmaemPHAsis,mmaComment
114 syntax region mmaFunctionTitle contained matchgroup=mmaFunctionComment start="\%((\*\*[ *]*\)" matchgroup=mmaFunctionTitle keepend end=".[.!-]\=\s*$" end="[.!-][ \t\r<&]"me=e-1 end="\%(\*\+)\)\@=" contained contains=@mmaNotes,mmaItem,mmaCommentStar
116 " catch remaining (**********)'s
117 syntax match mmaComment "(\*\*\+)"
119 syntax match mmaCommentStar "^\s*\*\+" contained
122 " Dollar sign variables
123 syntax match mmaVariable "\$\a\+[0-9a-zA-Z$]*"
125 " Preceding and Following Contexts
126 syntax match mmaVariable "`[a-zA-Z$]\+[0-9a-zA-Z$]*" contains=mmaVariable
127 syntax match mmaVariable "[a-zA-Z$]\+[0-9a-zA-Z$]*`" contains=mmaVariable
131 " 'string' is not accepted (until literal strings are supported!)
132 syntax region mmaString start=+\\\@<!"+ skip=+\\\@<!\\\%(\\\\\)*"+ end=+"+
133 syntax region mmaCommentString oneline start=+\\\@<!"+ skip=+\\\@<!\\\%(\\\\\)*"+ end=+"+ contained
137 " Each pattern marker below can be Blank[] (_), BlankSequence[] (__)
138 " or BlankNullSequence[] (___). Most examples below can also be
139 " combined, for example Pattern tests with Default values.
141 " _Head Anonymous patterns
143 " name:(_Head|_Head2) Named patterns
146 " name:_Head:val Default values
149 " _Head?(test[#]&) Pattern tests
151 " name_Head/;test[name] Conditionals
153 " _Head:. Predefined Default
155 " .. ... Pattern Repeat
157 syntax match mmaPatternError "\%(_\{4,}\|)\s*&\s*)\@!\)" contained
160 syntax match mmaPattern "[A-Za-z0-9`]\+\s*:\+[=>]\@!" contains=mmaOperator
162 syntax match mmaPattern ": *[^ ,]\+[\], ]\@=" contains=@mmaCommentStrings,@mmaTop,mmaOperator
164 syntax match mmaPattern "[A-Za-z0-9`]*_\+\%(\a\+\)\=\%(?([^)]\+)\|?[^\]},]\+\)\=" contains=@mmaTop,@mmaCommentStrings,mmaPatternError
180 " // @ Function application
181 " <> ~~ String/Pattern join
183 " . : Pattern operators
184 syntax match mmaOperator "\%(@\{1,3}\|//[.@]\=\)"
185 syntax match mmaOperator "\%(/[;:@.]\=\|\^\=:\==\)"
186 syntax match mmaOperator "\%([-:=]\=>\|<=\=\)"
187 "syntax match mmaOperator "\%(++\=\|--\=\|[/+-*]=\|[^*]\)"
188 syntax match mmaOperator "[*+=^.:?-]"
189 syntax match mmaOperator "\%(\~\~\=\)"
190 syntax match mmaOperator "\%(=\{2,3}\|=\=!=\|||\=\|&&\|!\)" contains=ALLBUT,mmaPureFunction
194 "syntax match mmaSymbol "`\=[a-zA-Z$]\+[0-9a-zA-Z$]*\%(`\%([a-zA-Z$]\+[0-9a-zA-Z$]*\)\=\)*" contained
195 syntax match mmaMessage "`\=\([a-zA-Z$]\+[0-9a-zA-Z$]*\)\%(`\%([a-zA-Z$]\+[0-9a-zA-Z$]*\)\=\)*::\a\+" contains=mmaMessageType
196 syntax match mmaMessageType "::\a\+"hs=s+2 contained
199 syntax match mmaPureFunction "#\%(#\|\d\+\)\="
200 syntax match mmaPureFunction "&"
203 " Since everything is pretty much a function, get this straight
205 syntax match mmaGenericFunction "[A-Za-z0-9`]\+\s*\%([@[]\|/:\|/\=/@\)\@=" contains=mmaOperator
206 syntax match mmaGenericFunction "\~\s*[^~]\+\s*\~"hs=s+1,he=e-1 contains=mmaOperator,mmaBoring
207 syntax match mmaGenericFunction "//\s*[A-Za-z0-9`]\+"hs=s+2 contains=mmaOperator
210 syntax match mmaNumber "\<\%(\d\+\.\=\d*\|\d*\.\=\d\+\)\>"
211 syntax match mmaNumber "`\d\+\%(\d\@!\.\|\>\)"
213 " Special Characters:
214 " \[Name] named character
217 " \:xxxx 4 digit hex (multibyte unicode)
218 syntax match mmaUnicode "\\\[\w\+\d*\]"
219 syntax match mmaUnicode "\\\%(\x\{3}\|\.\x\{2}\|:\x\{4}\)"
222 syntax match mmaError "\*)" containedin=ALLBUT,@mmaComments,@mmaStrings
223 syntax match mmaError "\%([/]{3,}\|[&:|+*?~-]\{3,}\|[.=]\{4,}\|_\@<=\.\{2,}\|`\{2,}\)" containedin=ALLBUT,@mmaComments,@mmaStrings
226 " things that shouldn't really be highlighted, or highlighted
227 " in they're own group if you _really_ want. :)
229 " TODO - use Delimiter group?
230 syntax match mmaBoring "[(){}]" contained
232 " ------------------------------------
233 " future explorations...
234 " ------------------------------------
235 " Function Arguments:
236 " anything between brackets []
238 "syntax region mmaArgument start="\[" end="\]" containedin=ALLBUT,@mmaComments,@mmaStrings transparent fold
242 "syntax region mmaLists start="{" end="}" containedin=ALLBUT,@mmaComments,@mmaStrings transparent fold
246 "syntax region mmaRegion start="(\*\+[^<]*<!--[^>]*\*\+)" end="--> \*)" containedin=ALLBUT,@mmaStrings transparent fold keepend
249 set commentstring='(*%s*)'
250 "set foldtext=MmaFoldText()
252 "function MmaFoldText()
253 " let line = getline(v:foldstart)
255 " let lines = v:foldend-v:foldstart+1
257 " let sub = substitute(line, '(\*\+|\*\+)|[-*_]\+', '', 'g')
259 " if match(line, '(\*') != -1
260 " let lines = lines.' line comment'
262 " let lines = lines.' lines'
265 " return v:folddashes.' '.lines.' '.sub
268 "this is slow for computing folds, but it does so accurately
269 syntax sync fromstart
271 " but this seems to do alright for non fold syntax coloring.
272 " for folding, however, it doesn't get the nesting right.
273 " TODO - find sync group for multiline modules? ick...
275 " sync multi line comments
276 "syntax sync match syncComments groupthere NONE "\*)"
277 "syntax sync match syncComments groupthere mmaComment "(\*"
279 "set foldmethod=syntax
283 if version >= 508 || !exists("did_mma_syn_inits")
285 let did_mma_syn_inits = 1
286 command -nargs=+ HiLink hi link <args>
288 command -nargs=+ HiLink hi def link <args>
291 " NOTE - the following links are not guaranteed to
292 " look good under all colorschemes. You might need to
293 " :so $VIMRUNTIME/syntax/hitest.vim and tweak these to
297 HiLink mmaComment Comment
298 HiLink mmaCommentStar Comment
299 HiLink mmaFunctionComment Comment
300 HiLink mmaLooseQuote Comment
301 HiLink mmaGenericFunction Function
302 HiLink mmaVariable Identifier
303 " HiLink mmaSymbol Identifier
304 HiLink mmaOperator Operator
305 HiLink mmaPatternOp Operator
306 HiLink mmaPureFunction Operator
307 HiLink mmaString String
308 HiLink mmaCommentString String
309 HiLink mmaUnicode String
310 HiLink mmaMessage Type
311 HiLink mmaNumber Type
312 HiLink mmaPattern Type
313 HiLink mmaError Error
314 HiLink mmaFixme Error
315 HiLink mmaPatternError Error
317 HiLink mmaemPHAsis Special
318 HiLink mmaFunctionTitle Special
319 HiLink mmaMessageType Special
320 HiLink mmaItem Preproc
325 let b:current_syntax = "mma"