blob: d2f22e9be516bade48599fa65a38206da31875ec [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001" Vim syntax file
2" Language: Mathematica
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003" Maintainer: steve layland <layland@wolfram.com>
Bram Moolenaarb8ff1fb2012-02-04 21:59:01 +01004" Last Change: 2012 Feb 03 by Thilo Six
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005" Source: http://members.wri.com/layland/vim/syntax/mma.vim
6" http://vim.sourceforge.net/scripts/script.php?script_id=1273
Bram Moolenaar5c736222010-01-06 20:54:52 +01007" Id: $Id: mma.vim,v 1.4 2006/04/14 20:40:38 vimboss Exp $
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00008" NOTE:
Bram Moolenaarb8ff1fb2012-02-04 21:59:01 +01009"
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000010" 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"
Bram Moolenaar6c391a72021-09-09 21:55:11 +020015" I also recommend setting the default 'Comment' highlighting to something
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016" other than the color used for 'Function', since both are plentiful in
Bram Moolenaarb8ff1fb2012-02-04 21:59:01 +010017" most mathematica files, and they are often the same color (when using
Bram Moolenaar555b2802005-05-19 21:08:39 +000018" background=dark).
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019"
20" Credits:
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>
26"
Bram Moolenaarb8ff1fb2012-02-04 21:59:01 +010027" Bugs:
28" o Vim 6.1 didn't really have support for character classes
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +000029" of other named character classes. For example, [\a\d]
30" didn't work. Therefore, a lot of this code uses explicit
Bram Moolenaarb8ff1fb2012-02-04 21:59:01 +010031" character classes instead: [0-9a-zA-Z]
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +000032"
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000033" TODO:
34" folding
35" fix nesting
36" finish populating popular symbols
Bram Moolenaar071d4272004-06-13 20:20:40 +000037
Bram Moolenaar89bcfda2016-08-30 23:26:57 +020038" quit when a syntax file was already loaded
39if exists("b:current_syntax")
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000040 finish
Bram Moolenaar071d4272004-06-13 20:20:40 +000041endif
42
Bram Moolenaarb8ff1fb2012-02-04 21:59:01 +010043let s:cpo_save = &cpo
44set cpo&vim
45
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000046" Group Definitions:
47syntax cluster mmaNotes contains=mmaTodo,mmaFixme
48syntax cluster mmaComments contains=mmaComment,mmaFunctionComment,mmaItem,mmaFunctionTitle,mmaCommentStar
49syntax cluster mmaCommentStrings contains=mmaLooseQuote,mmaCommentString,mmaUnicode
50syntax cluster mmaStrings contains=@mmaCommentStrings,mmaString
51syntax cluster mmaTop contains=mmaOperator,mmaGenericFunction,mmaPureFunction,mmaVariable
Bram Moolenaar071d4272004-06-13 20:20:40 +000052
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000053" Predefined Constants:
54" to list all predefined Symbols would be too insane...
55" it's probably smarter to define a select few, and get the rest from
56" context if absolutely necessary.
57" TODO - populate this with other often used Symbols
Bram Moolenaar071d4272004-06-13 20:20:40 +000058
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000059" standard fixed symbols:
60syntax keyword mmaVariable True False None Automatic All Null C General
Bram Moolenaar071d4272004-06-13 20:20:40 +000061
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000062" mathematical constants:
Bram Moolenaarb8ff1fb2012-02-04 21:59:01 +010063syntax keyword mmaVariable Pi I E Infinity ComplexInfinity Indeterminate GoldenRatio EulerGamma Degree Catalan Khinchin Glaisher
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000064
65" stream data / atomic heads:
66syntax keyword mmaVariable Byte Character Expression Number Real String Word EndOfFile Integer Symbol
67
68" sets:
69syntax keyword mmaVariable Integers Complexes Reals Booleans Rationals
70
71" character classes:
72syntax keyword mmaPattern DigitCharacter LetterCharacter WhitespaceCharacter WordCharacter EndOfString StartOfString EndOfLine StartOfLine WordBoundary
73
74" SelectionMove directions/units:
75syntax keyword mmaVariable Next Previous After Before Character Word Expression TextLine CellContents Cell CellGroup EvaluationCell ButtonCell GeneratedCell Notebook
76syntax keyword mmaVariable CellTags CellStyle CellLabel
77
78" TableForm positions:
79syntax keyword mmaVariable Above Below Left Right
80
81" colors:
82syntax keyword mmaVariable Black Blue Brown Cyan Gray Green Magenta Orange Pink Purple Red White Yellow
83
84" function attributes
Bram Moolenaarb8ff1fb2012-02-04 21:59:01 +010085syntax keyword mmaVariable Protected Listable OneIdentity Orderless Flat Constant NumericFunction Locked ReadProtected HoldFirst HoldRest HoldAll HoldAllComplete SequenceHold NHoldFirst NHoldRest NHoldAll Temporary Stub
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000086
Bram Moolenaar555b2802005-05-19 21:08:39 +000087" Comment Sections:
88" this:
89" :that:
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +000090syntax match mmaItem "\%(^[( |*\t]*\)\@<=\%(:\+\|\w\)\w\+\%( \w\+\)\{0,3}:" contained contains=@mmaNotes
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000091
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000092" Comment Keywords:
93syntax keyword mmaTodo TODO NOTE HEY contained
94syntax match mmaTodo "X\{3,}" contained
95syntax keyword mmaFixme FIX[ME] FIXTHIS BROKEN contained
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +000096syntax match mmaFixme "BUG\%( *\#\=[0-9]\+\)\=" contained
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000097" yay pirates...
98syntax match mmaFixme "\%(Y\=A\+R\+G\+\|GRR\+\|CR\+A\+P\+\)\%(!\+\)\=" contained
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000099
Bram Moolenaar555b2802005-05-19 21:08:39 +0000100" EmPHAsis:
101" this unnecessary, but whatever :)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +0000102syntax match mmaemPHAsis "\%(^\|\s\)\([_/]\)[a-zA-Z0-9]\+\%([- \t':]\+[a-zA-Z0-9]\+\)*\1\%(\s\|$\)" contained contains=mmaemPHAsis
103syntax match mmaemPHAsis "\%(^\|\s\)(\@<!\*[a-zA-Z0-9]\+\%([- \t':]\+[a-zA-Z0-9]\+\)*)\@!\*\%(\s\|$\)" contained contains=mmaemPHAsis
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000104
Bram Moolenaar555b2802005-05-19 21:08:39 +0000105" Regular Comments:
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000106" (* *)
107" allow nesting (* (* *) *) even though the frontend
108" won't always like it.
109syntax region mmaComment start=+(\*+ end=+\*)+ skipempty contains=@mmaNotes,mmaItem,@mmaCommentStrings,mmaemPHAsis,mmaComment
110
111" Function Comments:
Bram Moolenaar6c391a72021-09-09 21:55:11 +0200112" just like a normal comment except the first sentence is Special ala Java
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000113" (** *)
114" TODO - fix this for nesting, or not...
115syntax region mmaFunctionComment start="(\*\*\+" end="\*\+)" contains=@mmaNotes,mmaItem,mmaFunctionTitle,@mmaCommentStrings,mmaemPHAsis,mmaComment
116syntax region mmaFunctionTitle contained matchgroup=mmaFunctionComment start="\%((\*\*[ *]*\)" matchgroup=mmaFunctionTitle keepend end=".[.!-]\=\s*$" end="[.!-][ \t\r<&]"me=e-1 end="\%(\*\+)\)\@=" contained contains=@mmaNotes,mmaItem,mmaCommentStar
117
118" catch remaining (**********)'s
119syntax match mmaComment "(\*\*\+)"
120" catch preceding *
121syntax match mmaCommentStar "^\s*\*\+" contained
122
Bram Moolenaar555b2802005-05-19 21:08:39 +0000123" Variables:
124" Dollar sign variables
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +0000125syntax match mmaVariable "\$\a\+[0-9a-zA-Z$]*"
126
127" Preceding and Following Contexts
128syntax match mmaVariable "`[a-zA-Z$]\+[0-9a-zA-Z$]*" contains=mmaVariable
129syntax match mmaVariable "[a-zA-Z$]\+[0-9a-zA-Z$]*`" contains=mmaVariable
Bram Moolenaar555b2802005-05-19 21:08:39 +0000130
131" Strings:
132" "string"
133" 'string' is not accepted (until literal strings are supported!)
134syntax region mmaString start=+\\\@<!"+ skip=+\\\@<!\\\%(\\\\\)*"+ end=+"+
135syntax region mmaCommentString oneline start=+\\\@<!"+ skip=+\\\@<!\\\%(\\\\\)*"+ end=+"+ contained
136
137
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000138" Patterns:
139" Each pattern marker below can be Blank[] (_), BlankSequence[] (__)
Bram Moolenaarb8ff1fb2012-02-04 21:59:01 +0100140" or BlankNullSequence[] (___). Most examples below can also be
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000141" combined, for example Pattern tests with Default values.
Bram Moolenaarb8ff1fb2012-02-04 21:59:01 +0100142"
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000143" _Head Anonymous patterns
Bram Moolenaarb8ff1fb2012-02-04 21:59:01 +0100144" name_Head
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000145" name:(_Head|_Head2) Named patterns
Bram Moolenaarb8ff1fb2012-02-04 21:59:01 +0100146"
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000147" _Head : val
148" name:_Head:val Default values
149"
Bram Moolenaarb8ff1fb2012-02-04 21:59:01 +0100150" _Head?testQ,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000151" _Head?(test[#]&) Pattern tests
152"
153" name_Head/;test[name] Conditionals
Bram Moolenaarb8ff1fb2012-02-04 21:59:01 +0100154"
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000155" _Head:. Predefined Default
156"
157" .. ... Pattern Repeat
Bram Moolenaarb8ff1fb2012-02-04 21:59:01 +0100158
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000159syntax match mmaPatternError "\%(_\{4,}\|)\s*&\s*)\@!\)" contained
160
161"pattern name:
162syntax match mmaPattern "[A-Za-z0-9`]\+\s*:\+[=>]\@!" contains=mmaOperator
163"pattern default:
164syntax match mmaPattern ": *[^ ,]\+[\], ]\@=" contains=@mmaCommentStrings,@mmaTop,mmaOperator
165"pattern head/test:
166syntax match mmaPattern "[A-Za-z0-9`]*_\+\%(\a\+\)\=\%(?([^)]\+)\|?[^\]},]\+\)\=" contains=@mmaTop,@mmaCommentStrings,mmaPatternError
167
168" Operators:
169" /: ^= ^:= UpValue
170" /; Conditional
171" := = DownValue
172" == === ||
173" != =!= && Logic
174" >= <= < >
175" += -= *=
176" /= ++ -- Math
Bram Moolenaarb8ff1fb2012-02-04 21:59:01 +0100177" ^*
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000178" -> :> Rules
179" @@ @@@ Apply
180" /@ //@ Map
181" /. //. Replace
182" // @ Function application
183" <> ~~ String/Pattern join
184" ~ infix operator
185" . : Pattern operators
186syntax match mmaOperator "\%(@\{1,3}\|//[.@]\=\)"
187syntax match mmaOperator "\%(/[;:@.]\=\|\^\=:\==\)"
188syntax match mmaOperator "\%([-:=]\=>\|<=\=\)"
189"syntax match mmaOperator "\%(++\=\|--\=\|[/+-*]=\|[^*]\)"
190syntax match mmaOperator "[*+=^.:?-]"
191syntax match mmaOperator "\%(\~\~\=\)"
192syntax match mmaOperator "\%(=\{2,3}\|=\=!=\|||\=\|&&\|!\)" contains=ALLBUT,mmaPureFunction
193
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +0000194" Symbol Tags:
Bram Moolenaar555b2802005-05-19 21:08:39 +0000195" "SymbolName::item"
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +0000196"syntax match mmaSymbol "`\=[a-zA-Z$]\+[0-9a-zA-Z$]*\%(`\%([a-zA-Z$]\+[0-9a-zA-Z$]*\)\=\)*" contained
197syntax match mmaMessage "`\=\([a-zA-Z$]\+[0-9a-zA-Z$]*\)\%(`\%([a-zA-Z$]\+[0-9a-zA-Z$]*\)\=\)*::\a\+" contains=mmaMessageType
198syntax match mmaMessageType "::\a\+"hs=s+2 contained
Bram Moolenaar555b2802005-05-19 21:08:39 +0000199
200" Pure Functions:
201syntax match mmaPureFunction "#\%(#\|\d\+\)\="
202syntax match mmaPureFunction "&"
203
204" Named Functions:
Bram Moolenaarb8ff1fb2012-02-04 21:59:01 +0100205" Since everything is pretty much a function, get this straight
Bram Moolenaar555b2802005-05-19 21:08:39 +0000206" from context
207syntax match mmaGenericFunction "[A-Za-z0-9`]\+\s*\%([@[]\|/:\|/\=/@\)\@=" contains=mmaOperator
208syntax match mmaGenericFunction "\~\s*[^~]\+\s*\~"hs=s+1,he=e-1 contains=mmaOperator,mmaBoring
209syntax match mmaGenericFunction "//\s*[A-Za-z0-9`]\+"hs=s+2 contains=mmaOperator
210
211" Numbers:
212syntax match mmaNumber "\<\%(\d\+\.\=\d*\|\d*\.\=\d\+\)\>"
213syntax match mmaNumber "`\d\+\%(\d\@!\.\|\>\)"
214
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000215" Special Characters:
216" \[Name] named character
217" \ooo octal
218" \.xx 2 digit hex
219" \:xxxx 4 digit hex (multibyte unicode)
220syntax match mmaUnicode "\\\[\w\+\d*\]"
221syntax match mmaUnicode "\\\%(\x\{3}\|\.\x\{2}\|:\x\{4}\)"
222
223" Syntax Errors:
224syntax match mmaError "\*)" containedin=ALLBUT,@mmaComments,@mmaStrings
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +0000225syntax match mmaError "\%([/]{3,}\|[&:|+*?~-]\{3,}\|[.=]\{4,}\|_\@<=\.\{2,}\|`\{2,}\)" containedin=ALLBUT,@mmaComments,@mmaStrings
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000226
227" Punctuation:
Bram Moolenaarb8ff1fb2012-02-04 21:59:01 +0100228" things that shouldn't really be highlighted, or highlighted
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000229" in they're own group if you _really_ want. :)
230" ( ) { }
231" TODO - use Delimiter group?
232syntax match mmaBoring "[(){}]" contained
233
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +0000234" ------------------------------------
235" future explorations...
236" ------------------------------------
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000237" Function Arguments:
238" anything between brackets []
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +0000239" (fold)
240"syntax region mmaArgument start="\[" end="\]" containedin=ALLBUT,@mmaComments,@mmaStrings transparent fold
241
242" Lists:
243" (fold)
244"syntax region mmaLists start="{" end="}" containedin=ALLBUT,@mmaComments,@mmaStrings transparent fold
245
246" Regions:
247" (fold)
248"syntax region mmaRegion start="(\*\+[^<]*<!--[^>]*\*\+)" end="--> \*)" containedin=ALLBUT,@mmaStrings transparent fold keepend
249
250" show fold text
251set commentstring='(*%s*)'
252"set foldtext=MmaFoldText()
253
254"function MmaFoldText()
255" let line = getline(v:foldstart)
Bram Moolenaarb8ff1fb2012-02-04 21:59:01 +0100256"
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +0000257" let lines = v:foldend-v:foldstart+1
Bram Moolenaarb8ff1fb2012-02-04 21:59:01 +0100258"
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +0000259" let sub = substitute(line, '(\*\+|\*\+)|[-*_]\+', '', 'g')
260"
261" if match(line, '(\*') != -1
262" let lines = lines.' line comment'
263" else
264" let lines = lines.' lines'
265" endif
266"
267" return v:folddashes.' '.lines.' '.sub
268"endf
Bram Moolenaarb8ff1fb2012-02-04 21:59:01 +0100269
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +0000270"this is slow for computing folds, but it does so accurately
271syntax sync fromstart
272
273" but this seems to do alright for non fold syntax coloring.
274" for folding, however, it doesn't get the nesting right.
275" TODO - find sync group for multiline modules? ick...
276
277" sync multi line comments
278"syntax sync match syncComments groupthere NONE "\*)"
279"syntax sync match syncComments groupthere mmaComment "(\*"
280
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000281"set foldmethod=syntax
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +0000282"set foldnestmax=1
283"set foldminlines=15
Bram Moolenaar071d4272004-06-13 20:20:40 +0000284
Bram Moolenaar071d4272004-06-13 20:20:40 +0000285
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200286" NOTE - the following links are not guaranteed to
287" look good under all colorschemes. You might need to
288" :so $VIMRUNTIME/syntax/hitest.vim and tweak these to
289" look good in yours
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000290
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +0000291
Bram Moolenaarf37506f2016-08-31 22:22:10 +0200292hi def link mmaComment Comment
293hi def link mmaCommentStar Comment
294hi def link mmaFunctionComment Comment
295hi def link mmaLooseQuote Comment
296hi def link mmaGenericFunction Function
297hi def link mmaVariable Identifier
298" hi def link mmaSymbol Identifier
299hi def link mmaOperator Operator
300hi def link mmaPatternOp Operator
301hi def link mmaPureFunction Operator
302hi def link mmaString String
303hi def link mmaCommentString String
304hi def link mmaUnicode String
305hi def link mmaMessage Type
306hi def link mmaNumber Type
307hi def link mmaPattern Type
308hi def link mmaError Error
309hi def link mmaFixme Error
310hi def link mmaPatternError Error
311hi def link mmaTodo Todo
312hi def link mmaemPHAsis Special
313hi def link mmaFunctionTitle Special
314hi def link mmaMessageType Special
315hi def link mmaItem Preproc
Bram Moolenaar071d4272004-06-13 20:20:40 +0000316
Bram Moolenaar071d4272004-06-13 20:20:40 +0000317
318let b:current_syntax = "mma"
Bram Moolenaarb8ff1fb2012-02-04 21:59:01 +0100319
320let &cpo = s:cpo_save
321unlet s:cpo_save