1" Vim syntax file 2" Language: Mathematica 3" Maintainer: steve layland <[email protected]> 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 $ 8" NOTE: 9" 10" Empty .m files will automatically be presumed as Matlab files 11" unless you have the following in your .vimrc: 12" 13" let filetype_m="mma" 14" 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 18" background=dark). 19" 20" Credits: 21" o Original Mathematica syntax version written by 22" Wolfgang Waltenberger <[email protected]> 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 <[email protected]> 26" 27" Bugs: 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] 32" 33" TODO: 34" folding 35" fix nesting 36" finish populating popular symbols 37 38if version < 600 39 syntax clear 40elseif exists("b:current_syntax") 41 finish 42endif 43 44" Group Definitions: 45syntax cluster mmaNotes contains=mmaTodo,mmaFixme 46syntax cluster mmaComments contains=mmaComment,mmaFunctionComment,mmaItem,mmaFunctionTitle,mmaCommentStar 47syntax cluster mmaCommentStrings contains=mmaLooseQuote,mmaCommentString,mmaUnicode 48syntax cluster mmaStrings contains=@mmaCommentStrings,mmaString 49syntax cluster mmaTop contains=mmaOperator,mmaGenericFunction,mmaPureFunction,mmaVariable 50 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 56 57" standard fixed symbols: 58syntax keyword mmaVariable True False None Automatic All Null C General 59 60" mathematical constants: 61syntax keyword mmaVariable Pi I E Infinity ComplexInfinity Indeterminate GoldenRatio EulerGamma Degree Catalan Khinchin Glaisher 62 63" stream data / atomic heads: 64syntax keyword mmaVariable Byte Character Expression Number Real String Word EndOfFile Integer Symbol 65 66" sets: 67syntax keyword mmaVariable Integers Complexes Reals Booleans Rationals 68 69" character classes: 70syntax keyword mmaPattern DigitCharacter LetterCharacter WhitespaceCharacter WordCharacter EndOfString StartOfString EndOfLine StartOfLine WordBoundary 71 72" SelectionMove directions/units: 73syntax keyword mmaVariable Next Previous After Before Character Word Expression TextLine CellContents Cell CellGroup EvaluationCell ButtonCell GeneratedCell Notebook 74syntax keyword mmaVariable CellTags CellStyle CellLabel 75 76" TableForm positions: 77syntax keyword mmaVariable Above Below Left Right 78 79" colors: 80syntax keyword mmaVariable Black Blue Brown Cyan Gray Green Magenta Orange Pink Purple Red White Yellow 81 82" function attributes 83syntax keyword mmaVariable Protected Listable OneIdentity Orderless Flat Constant NumericFunction Locked ReadProtected HoldFirst HoldRest HoldAll HoldAllComplete SequenceHold NHoldFirst NHoldRest NHoldAll Temporary Stub 84 85" Comment Sections: 86" this: 87" :that: 88syntax match mmaItem "\%(^[( |*\t]*\)\@<=\%(:\+\|\w\)\w\+\%( \w\+\)\{0,3}:" contained contains=@mmaNotes 89 90" Comment Keywords: 91syntax keyword mmaTodo TODO NOTE HEY contained 92syntax match mmaTodo "X\{3,}" contained 93syntax keyword mmaFixme FIX[ME] FIXTHIS BROKEN contained 94syntax match mmaFixme "BUG\%( *\#\=[0-9]\+\)\=" contained 95" yay pirates... 96syntax match mmaFixme "\%(Y\=A\+R\+G\+\|GRR\+\|CR\+A\+P\+\)\%(!\+\)\=" contained 97 98" EmPHAsis: 99" this unnecessary, but whatever :) 100syntax match mmaemPHAsis "\%(^\|\s\)\([_/]\)[a-zA-Z0-9]\+\%([- \t':]\+[a-zA-Z0-9]\+\)*\1\%(\s\|$\)" contained contains=mmaemPHAsis 101syntax match mmaemPHAsis "\%(^\|\s\)(\@<!\*[a-zA-Z0-9]\+\%([- \t':]\+[a-zA-Z0-9]\+\)*)\@!\*\%(\s\|$\)" contained contains=mmaemPHAsis 102 103" Regular Comments: 104" (* *) 105" allow nesting (* (* *) *) even though the frontend 106" won't always like it. 107syntax region mmaComment start=+(\*+ end=+\*)+ skipempty contains=@mmaNotes,mmaItem,@mmaCommentStrings,mmaemPHAsis,mmaComment 108 109" Function Comments: 110" just like a normal comment except the first sentance is Special ala Java 111" (** *) 112" TODO - fix this for nesting, or not... 113syntax region mmaFunctionComment start="(\*\*\+" end="\*\+)" contains=@mmaNotes,mmaItem,mmaFunctionTitle,@mmaCommentStrings,mmaemPHAsis,mmaComment 114syntax region mmaFunctionTitle contained matchgroup=mmaFunctionComment start="\%((\*\*[ *]*\)" matchgroup=mmaFunctionTitle keepend end=".[.!-]\=\s*$" end="[.!-][ \t\r<&]"me=e-1 end="\%(\*\+)\)\@=" contained contains=@mmaNotes,mmaItem,mmaCommentStar 115 116" catch remaining (**********)'s 117syntax match mmaComment "(\*\*\+)" 118" catch preceding * 119syntax match mmaCommentStar "^\s*\*\+" contained 120 121" Variables: 122" Dollar sign variables 123syntax match mmaVariable "\$\a\+[0-9a-zA-Z$]*" 124 125" Preceding and Following Contexts 126syntax match mmaVariable "`[a-zA-Z$]\+[0-9a-zA-Z$]*" contains=mmaVariable 127syntax match mmaVariable "[a-zA-Z$]\+[0-9a-zA-Z$]*`" contains=mmaVariable 128 129" Strings: 130" "string" 131" 'string' is not accepted (until literal strings are supported!) 132syntax region mmaString start=+\\\@<!"+ skip=+\\\@<!\\\%(\\\\\)*"+ end=+"+ 133syntax region mmaCommentString oneline start=+\\\@<!"+ skip=+\\\@<!\\\%(\\\\\)*"+ end=+"+ contained 134 135 136" Patterns: 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. 140" 141" _Head Anonymous patterns 142" name_Head 143" name:(_Head|_Head2) Named patterns 144" 145" _Head : val 146" name:_Head:val Default values 147" 148" _Head?testQ, 149" _Head?(test[#]&) Pattern tests 150" 151" name_Head/;test[name] Conditionals 152" 153" _Head:. Predefined Default 154" 155" .. ... Pattern Repeat 156 157syntax match mmaPatternError "\%(_\{4,}\|)\s*&\s*)\@!\)" contained 158 159"pattern name: 160syntax match mmaPattern "[A-Za-z0-9`]\+\s*:\+[=>]\@!" contains=mmaOperator 161"pattern default: 162syntax match mmaPattern ": *[^ ,]\+[\], ]\@=" contains=@mmaCommentStrings,@mmaTop,mmaOperator 163"pattern head/test: 164syntax match mmaPattern "[A-Za-z0-9`]*_\+\%(\a\+\)\=\%(?([^)]\+)\|?[^\]},]\+\)\=" contains=@mmaTop,@mmaCommentStrings,mmaPatternError 165 166" Operators: 167" /: ^= ^:= UpValue 168" /; Conditional 169" := = DownValue 170" == === || 171" != =!= && Logic 172" >= <= < > 173" += -= *= 174" /= ++ -- Math 175" ^* 176" -> :> Rules 177" @@ @@@ Apply 178" /@ //@ Map 179" /. //. Replace 180" // @ Function application 181" <> ~~ String/Pattern join 182" ~ infix operator 183" . : Pattern operators 184syntax match mmaOperator "\%(@\{1,3}\|//[.@]\=\)" 185syntax match mmaOperator "\%(/[;:@.]\=\|\^\=:\==\)" 186syntax match mmaOperator "\%([-:=]\=>\|<=\=\)" 187"syntax match mmaOperator "\%(++\=\|--\=\|[/+-*]=\|[^*]\)" 188syntax match mmaOperator "[*+=^.:?-]" 189syntax match mmaOperator "\%(\~\~\=\)" 190syntax match mmaOperator "\%(=\{2,3}\|=\=!=\|||\=\|&&\|!\)" contains=ALLBUT,mmaPureFunction 191 192" Symbol Tags: 193" "SymbolName::item" 194"syntax match mmaSymbol "`\=[a-zA-Z$]\+[0-9a-zA-Z$]*\%(`\%([a-zA-Z$]\+[0-9a-zA-Z$]*\)\=\)*" contained 195syntax match mmaMessage "`\=\([a-zA-Z$]\+[0-9a-zA-Z$]*\)\%(`\%([a-zA-Z$]\+[0-9a-zA-Z$]*\)\=\)*::\a\+" contains=mmaMessageType 196syntax match mmaMessageType "::\a\+"hs=s+2 contained 197 198" Pure Functions: 199syntax match mmaPureFunction "#\%(#\|\d\+\)\=" 200syntax match mmaPureFunction "&" 201 202" Named Functions: 203" Since everything is pretty much a function, get this straight 204" from context 205syntax match mmaGenericFunction "[A-Za-z0-9`]\+\s*\%([@[]\|/:\|/\=/@\)\@=" contains=mmaOperator 206syntax match mmaGenericFunction "\~\s*[^~]\+\s*\~"hs=s+1,he=e-1 contains=mmaOperator,mmaBoring 207syntax match mmaGenericFunction "//\s*[A-Za-z0-9`]\+"hs=s+2 contains=mmaOperator 208 209" Numbers: 210syntax match mmaNumber "\<\%(\d\+\.\=\d*\|\d*\.\=\d\+\)\>" 211syntax match mmaNumber "`\d\+\%(\d\@!\.\|\>\)" 212 213" Special Characters: 214" \[Name] named character 215" \ooo octal 216" \.xx 2 digit hex 217" \:xxxx 4 digit hex (multibyte unicode) 218syntax match mmaUnicode "\\\[\w\+\d*\]" 219syntax match mmaUnicode "\\\%(\x\{3}\|\.\x\{2}\|:\x\{4}\)" 220 221" Syntax Errors: 222syntax match mmaError "\*)" containedin=ALLBUT,@mmaComments,@mmaStrings 223syntax match mmaError "\%([/]{3,}\|[&:|+*?~-]\{3,}\|[.=]\{4,}\|_\@<=\.\{2,}\|`\{2,}\)" containedin=ALLBUT,@mmaComments,@mmaStrings 224 225" Punctuation: 226" things that shouldn't really be highlighted, or highlighted 227" in they're own group if you _really_ want. :) 228" ( ) { } 229" TODO - use Delimiter group? 230syntax match mmaBoring "[(){}]" contained 231 232" ------------------------------------ 233" future explorations... 234" ------------------------------------ 235" Function Arguments: 236" anything between brackets [] 237" (fold) 238"syntax region mmaArgument start="\[" end="\]" containedin=ALLBUT,@mmaComments,@mmaStrings transparent fold 239 240" Lists: 241" (fold) 242"syntax region mmaLists start="{" end="}" containedin=ALLBUT,@mmaComments,@mmaStrings transparent fold 243 244" Regions: 245" (fold) 246"syntax region mmaRegion start="(\*\+[^<]*<!--[^>]*\*\+)" end="--> \*)" containedin=ALLBUT,@mmaStrings transparent fold keepend 247 248" show fold text 249set commentstring='(*%s*)' 250"set foldtext=MmaFoldText() 251 252"function MmaFoldText() 253" let line = getline(v:foldstart) 254" 255" let lines = v:foldend-v:foldstart+1 256" 257" let sub = substitute(line, '(\*\+|\*\+)|[-*_]\+', '', 'g') 258" 259" if match(line, '(\*') != -1 260" let lines = lines.' line comment' 261" else 262" let lines = lines.' lines' 263" endif 264" 265" return v:folddashes.' '.lines.' '.sub 266"endf 267 268"this is slow for computing folds, but it does so accurately 269syntax sync fromstart 270 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... 274 275" sync multi line comments 276"syntax sync match syncComments groupthere NONE "\*)" 277"syntax sync match syncComments groupthere mmaComment "(\*" 278 279"set foldmethod=syntax 280"set foldnestmax=1 281"set foldminlines=15 282 283if version >= 508 || !exists("did_mma_syn_inits") 284 if version < 508 285 let did_mma_syn_inits = 1 286 command -nargs=+ HiLink hi link <args> 287 else 288 command -nargs=+ HiLink hi def link <args> 289 endif 290 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 294 " look good in yours 295 296 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 316 HiLink mmaTodo Todo 317 HiLink mmaemPHAsis Special 318 HiLink mmaFunctionTitle Special 319 HiLink mmaMessageType Special 320 HiLink mmaItem Preproc 321 322 delcommand HiLink 323endif 324 325let b:current_syntax = "mma" 326