blob: b1f05ec85391a5aeb2cb79791060e0694bd9dfa7 [file] [log] [blame]
Gregory Anders1cc4cae2024-07-15 20:00:48 +02001" Vim syntax file
2" Language: Typst
Luca Saccarolab66cac12024-12-09 20:29:14 +01003" Previous Maintainer: Gregory Anders
4" Maintainer: Luca Saccarola <github.e41mv@aleeas.com>
5" Last Change: 2024 Dec 09
Gregory Anders1cc4cae2024-07-15 20:00:48 +02006" Based on: https://github.com/kaarmu/typst.vim
7
8if exists('b:current_syntax')
9 finish
10endif
11
12syntax sync fromstart
13syntax spell toplevel
14
15" Common {{{1
16syntax cluster typstCommon
17 \ contains=@typstComment
18
19" Common > Comment {{{2
20syntax cluster typstComment
21 \ contains=typstCommentBlock,typstCommentLine
Yinzuo Jiangd181baf2024-11-02 16:34:40 +010022syntax region typstCommentBlock
23 \ start="/\*" end="\*/" keepend
Gregory Anders1cc4cae2024-07-15 20:00:48 +020024 \ contains=typstCommentTodo,@Spell
25syntax match typstCommentLine
26 \ #//.*#
27 \ contains=typstCommentTodo,@Spell
28syntax keyword typstCommentTodo
29 \ contained
30 \ TODO FIXME XXX TBD
31
32
33" Code {{{1
34syntax cluster typstCode
35 \ contains=@typstCommon
36 \ ,@typstCodeKeywords
37 \ ,@typstCodeConstants
38 \ ,@typstCodeIdentifiers
39 \ ,@typstCodeFunctions
40 \ ,@typstCodeParens
41
42" Code > Keywords {{{2
43syntax cluster typstCodeKeywords
44 \ contains=typstCodeConditional
45 \ ,typstCodeRepeat
46 \ ,typstCodeKeyword
47 \ ,typstCodeStatement
48syntax keyword typstCodeConditional
49 \ contained
50 \ if else
51syntax keyword typstCodeRepeat
52 \ contained
53 \ while for
54syntax keyword typstCodeKeyword
55 \ contained
56 \ not in and or return
57syntax region typstCodeStatement
58 \ contained
59 \ matchgroup=typstCodeStatementWord start=/\v(let|set|import|include)>/
60 \ matchgroup=Noise end=/\v%(;|$)/
61 \ contains=@typstCode
62syntax region typstCodeStatement
63 \ contained
64 \ matchgroup=typstCodeStatementWord start=/show/
65 \ matchgroup=Noise end=/\v%(:|$)/ keepend
66 \ contains=@typstCode
67 \ skipwhite nextgroup=@typstCode,typstCodeShowRocket
68syntax match typstCodeShowRocket
69 \ contained
70 \ /.*=>/
71 \ contains=@typstCode
72 \ skipwhite nextgroup=@typstCode
73
74" Code > Identifiers {{{2
75syntax cluster typstCodeIdentifiers
76 \ contains=typstCodeIdentifier
77 \ ,typstCodeFieldAccess
78syntax match typstCodeIdentifier
79 \ contained
80 \ /\v\w\k*>(<%(let|set|show|import|include))@<![\.\[\(]@!/
81syntax match typstCodeFieldAccess
82 \ contained
83 \ /\v\w\k*>(<%(let|set|show|import|include))@<!\.[\[\(]@!/
84 \ nextgroup=typstCodeFieldAccess,typstCodeFunction
85
86" Code > Functions {{{2
87syntax cluster typstCodeFunctions
88 \ contains=typstCodeFunction
89syntax match typstCodeFunction
90 \ contained
91 \ /\v\w\k*>(<%(let|set|show|import|include))@<![\(\[]@=/
92 \ nextgroup=typstCodeFunctionArgument
93syntax match typstCodeFunctionArgument
94 \ contained
95 \ /\v%(%(\(.{-}\)|\[.{-}\]|\{.{-}\}))*/ transparent
96 \ contains=@typstCode
97
98" Code > Constants {{{2
99syntax cluster typstCodeConstants
100 \ contains=typstCodeConstant
101 \ ,typstCodeNumberInteger
102 \ ,typstCodeNumberFloat
103 \ ,typstCodeNumberLength
104 \ ,typstCodeNumberAngle
105 \ ,typstCodeNumberRatio
106 \ ,typstCodeNumberFraction
107 \ ,typstCodeString
108 \ ,typstCodeLabel
109syntax match typstCodeConstant
110 \ contained
111 \ /\v<%(none|auto|true|false)-@!>/
112syntax match typstCodeNumberInteger
113 \ contained
114 \ /\v<\d+>/
115
116syntax match typstCodeNumberFloat
117 \ contained
118 \ /\v<\d+\.\d*>/
119syntax match typstCodeNumberLength
120 \ contained
121 \ /\v<\d+(\.\d*)?(pt|mm|cm|in|em)>/
122syntax match typstCodeNumberAngle
123 \ contained
124 \ /\v<\d+(\.\d*)?(deg|rad)>/
125syntax match typstCodeNumberRatio
126 \ contained
127 \ /\v<\d+(\.\d*)?\%/
128syntax match typstCodeNumberFraction
129 \ contained
130 \ /\v<\d+(\.\d*)?fr>/
131syntax region typstCodeString
132 \ contained
133 \ start=/"/ skip=/\v\\\\|\\"/ end=/"/
134 \ contains=@Spell
135syntax match typstCodeLabel
136 \ contained
137 \ /\v\<\K%(\k*-*)*\>/
138
139" Code > Parens {{{2
140syntax cluster typstCodeParens
141 \ contains=typstCodeParen
142 \ ,typstCodeBrace
143 \ ,typstCodeBracket
144 \ ,typstCodeDollar
145 \ ,typstMarkupRawInline
146 \ ,typstMarkupRawBlock
147syntax region typstCodeParen
148 \ contained
149 \ matchgroup=Noise start=/(/ end=/)/
150 \ contains=@typstCode
151syntax region typstCodeBrace
152 \ contained
153 \ matchgroup=Noise start=/{/ end=/}/
154 \ contains=@typstCode
155syntax region typstCodeBracket
156 \ contained
157 \ matchgroup=Noise start=/\[/ end=/\]/
158 \ contains=@typstMarkup
159syntax region typstCodeDollar
160 \ contained
161 \ matchgroup=Number start=/\\\@<!\$/ end=/\\\@<!\$/
162 \ contains=@typstMath
163
164
165" Hashtag {{{1
166syntax cluster typstHashtag
167 \ contains=@typstHashtagKeywords
168 \ ,@typstHashtagConstants
169 \ ,@typstHashtagIdentifiers
170 \ ,@typstHashtagFunctions
171 \ ,@typstHashtagParens
172
173" Hashtag > Keywords {{{2
174syntax cluster typstHashtagKeywords
175 \ contains=typstHashtagConditional
176 \ ,typstHashtagRepeat
177 \ ,typstHashtagKeywords
178 \ ,typstHashtagStatement
179
180" syntax match typstHashtagControlFlowError
181" \ /\v#%(if|while|for)>-@!.{-}$\_.{-}%(\{|\[|\()/
182syntax match typstHashtagControlFlow
183 \ /\v#%(if|while|for)>.{-}\ze%(\{|\[|\()/
184 \ contains=typstHashtagConditional,typstHashtagRepeat
185 \ nextgroup=@typstCode
186syntax region typstHashtagConditional
187 \ contained
188 \ start=/\v#if>/ end=/\v\ze(\{|\[)/
189 \ contains=@typstCode
190syntax region typstHashtagRepeat
191 \ contained
192 \ start=/\v#(while|for)>/ end=/\v\ze(\{|\[)/
193 \ contains=@typstCode
194syntax match typstHashtagKeyword
195 \ /\v#(return)>/
196 \ skipwhite nextgroup=@typstCode
197syntax region typstHashtagStatement
198 \ matchgroup=typstHashtagStatementWord start=/\v#(let|set|import|include)>/
199 \ matchgroup=Noise end=/\v%(;|$)/
200 \ contains=@typstCode
201syntax region typstHashtagStatement
202 \ matchgroup=typstHashtagStatementWord start=/#show/
203 \ matchgroup=Noise end=/\v%(:|$)/ keepend
204 \ contains=@typstCode
205 \ skipwhite nextgroup=@typstCode,typstCodeShowRocket
206
207" Hashtag > Constants {{{2
208syntax cluster typstHashtagConstants
209 \ contains=typstHashtagConstant
210syntax match typstHashtagConstant
211 \ /\v#(none|auto|true|false)>/
212
213" Hashtag > Identifiers {{{2
214syntax cluster typstHashtagIdentifiers
215 \ contains=typstHashtagIdentifier
216 \ ,typstHashtagFieldAccess
217syntax match typstHashtagIdentifier
218 \ /\v#\w\k*>(<%(let|set|show|import|include))@<![\.\[\(]@!/
219syntax match typstHashtagFieldAccess
220 \ /\v#\w\k*>(<%(let|set|show|import|include))@<!\.[\[\(]@!/
221 \ nextgroup=typstCodeFieldAccess,typstCodeFunction
222
223" Hashtag > Functions {{{2
224syntax cluster typstHashtagFunctions
225 \ contains=typstHashtagFunction
226syntax match typstHashtagFunction
227 \ /\v#\w\k*>(<%(let|set|show|import|include))@<![\(\[]@=/
228 \ nextgroup=typstCodeFunctionArgument
229
230" Hashtag > Parens {{{2
231syntax cluster typstHashtagParens
232 \ contains=typstHashtagParen
233 \ ,typstHashtagBrace
234 \ ,typstHashtagBracket
235 \ ,typstHashtagDollar
236syntax region typstHashtagParen
237 \ matchgroup=Noise start=/#(/ end=/)/
238 \ contains=@typstCode
239syntax region typstHashtagBrace
240 \ matchgroup=Noise start=/#{/ end=/}/
241 \ contains=@typstCode
242syntax region typstHashtagBracket
243 \ matchgroup=Noise start=/#\[/ end=/\]/
244 \ contains=@typstMarkup
245syntax region typstHashtagDollar
246 \ matchgroup=Noise start=/#\$/ end=/\\\@<!\$/
247 \ contains=@typstMath
248
249
250" Markup {{{1
251syntax cluster typstMarkup
252 \ contains=@typstCommon
253 \ ,@Spell
254 \ ,@typstHashtag
255 \ ,@typstMarkupText
256 \ ,@typstMarkupParens
257
258" Markup > Text {{{2
259syntax cluster typstMarkupText
260 \ contains=typstMarkupRawInline
261 \ ,typstMarkupRawBlock
262 \ ,typstMarkupLabel
263 \ ,typstMarkupReference
264 \ ,typstMarkupUrl
265 \ ,typstMarkupHeading
266 \ ,typstMarkupBulletList
267 \ ,typstMarkupEnumList
268 \ ,typstMarkupTermList
269 \ ,typstMarkupBold
270 \ ,typstMarkupItalic
271 \ ,typstMarkupLinebreak
272 \ ,typstMarkupNonbreakingSpace
273 \ ,typstMarkupShy
274 \ ,typstMarkupDash
275 \ ,typstMarkupEllipsis
276
277" Raw Text
278syntax match typstMarkupRawInline
279 \ /`.\{-}`/
280syntax region typstMarkupRawBlock
281 \ matchgroup=Macro start=/```\w*/
282 \ matchgroup=Macro end=/```/ keepend
283syntax region typstMarkupCodeBlockTypst
284 \ matchgroup=Macro start=/```typst/
285 \ matchgroup=Macro end=/```/ contains=@typstCode keepend
286 \ concealends
287
288for s:name in get(g:, 'typst_embedded_languages', [])
289 let s:include = ['syntax include'
290 \ ,'@typstEmbedded_'..s:name
291 \ ,'syntax/'..s:name..'.vim']
292 let s:rule = ['syn region'
293 \,s:name
294 \,'matchgroup=Macro'
295 \,'start=/```'..s:name..'\>/ end=/```/'
296 \,'contains=@typstEmbedded_'..s:name
297 \,'keepend'
298 \,'concealends']
299 execute 'silent! ' .. join(s:include, ' ')
300 unlet! b:current_syntax
301 execute join(s:rule, ' ')
302endfor
303
304" Label & Reference
305syntax match typstMarkupLabel
306 \ /\v\<\K%(\k*-*)*\>/
307syntax match typstMarkupReference
308 \ /\v\@\K%(\k*-*)*/
309
310" URL
311syntax match typstMarkupUrl
312 \ #\v\w+://\S*#
313
314" Heading
315syntax match typstMarkupHeading
316 \ /^\s*\zs=\{1,6}\s.*$/
317 \ contains=typstMarkupLabel,@Spell
318
319" Lists
320syntax match typstMarkupBulletList
321 \ /\v^\s*-\s+/
322syntax match typstMarkupEnumList
323 \ /\v^\s*(\+|\d+\.)\s+/
324syntax region typstMarkupTermList
325 \ oneline start=/\v^\s*\/\s/ end=/:/
326 \ contains=@typstMarkup
327
328" Bold & Italic
329syntax match typstMarkupBold
330 \ /\v(\w|\\)@1<!\*\S@=.{-}(\n.{-1,})*\S@1<=\\@1<!\*/
331 \ contains=typstMarkupBoldRegion
332syntax match typstMarkupItalic
333 \ /\v(\w|\\)@1<!_\S@=.{-}(\n.{-1,})*\S@1<=\\@1<!_/
334 \ contains=typstMarkupItalicRegion
335syntax match typstMarkupBoldItalic
336 \ contained
337 \ /\v(\w|\\)@1<![_\*]\S@=.{-}(\n.{-1,})*\S@1<=\\@1<!\2/
338 \ contains=typstMarkupBoldRegion,typstMarkupItalicRegion
339syntax region typstMarkupBoldRegion
340 \ contained
341 \ transparent matchgroup=typstMarkupBold
342 \ start=/\(^\|[^0-9a-zA-Z]\)\@<=\*/ end=/\*\($\|[^0-9a-zA-Z]\)\@=/
343 \ concealends contains=typstMarkupBoldItalic,typstMarkupLabel,@Spell
344syntax region typstMarkupItalicRegion
345 \ contained
346 \ transparent matchgroup=typstMarkupItalic
347 \ start=/\(^\|[^0-9a-zA-Z]\)\@<=_/ end=/_\($\|[^0-9a-zA-Z]\)\@=/
348 \ concealends contains=typstMarkupBoldItalic,typstMarkupLabel,@Spell
349
350" Linebreak & Special Whitespace
351syntax match typstMarkupLinebreak
352 \ /\\\\/
353syntax match typstMarkupNonbreakingSpace
354 \ /\~/
355syntax match typstMarkupShy
356 \ /-?/
357
358" Special Symbols
359syntax match typstMarkupDash
360 \ /-\{2,3}/
361syntax match typstMarkupEllipsis
362 \ /\.\.\./
363
364" Markup > Parens {{{2
365syntax cluster typstMarkupParens
366 \ contains=typstMarkupBracket
367 \ ,typstMarkupDollar
368syntax region typstMarkupBracket
369 \ matchgroup=Noise start=/\[/ end=/\]/
370 \ contains=@typstMarkup
371syntax region typstMarkupDollar
372 \ matchgroup=Special start=/\\\@<!\$/ end=/\\\@<!\$/
373 \ contains=@typstMath
374
375
376" Math {{{1
377syntax cluster typstMath
378 \ contains=@typstCommon
379 \ ,@typstHashtag
380 \ ,typstMathIdentifier
381 \ ,typstMathFunction
382 \ ,typstMathNumber
383 \ ,typstMathSymbol
384 \ ,typstMathBold
385 \ ,typstMathScripts
386 \ ,typstMathQuote
387
388syntax match typstMathIdentifier
389 \ /\a\a\+/
390 \ contained
391syntax match typstMathFunction
392 \ /\a\a\+\ze(/
393 \ contained
394syntax match typstMathNumber
395 \ /\<\d\+\>/
396 \ contained
397syntax region typstMathQuote
398 \ matchgroup=String start=/"/ skip=/\\"/ end=/"/
399 \ contained
400
401" Math > Linked groups {{{2
402highlight default link typstMathIdentifier Identifier
403highlight default link typstMathFunction Statement
404highlight default link typstMathNumber Number
405highlight default link typstMathSymbol Statement
406
407" Highlighting {{{1
408
409" Highlighting > Linked groups {{{2
410highlight default link typstCommentBlock Comment
411highlight default link typstCommentLine Comment
412highlight default link typstCommentTodo Todo
413highlight default link typstCodeConditional Conditional
414highlight default link typstCodeRepeat Repeat
415highlight default link typstCodeKeyword Keyword
416highlight default link typstCodeConstant Constant
417highlight default link typstCodeNumberInteger Number
418highlight default link typstCodeNumberFloat Number
419highlight default link typstCodeNumberLength Number
420highlight default link typstCodeNumberAngle Number
421highlight default link typstCodeNumberRatio Number
422highlight default link typstCodeNumberFraction Number
423highlight default link typstCodeString String
424highlight default link typstCodeLabel Structure
425highlight default link typstCodeStatementWord Statement
426highlight default link typstCodeIdentifier Identifier
427highlight default link typstCodeFieldAccess Identifier
428highlight default link typstCodeFunction Function
429highlight default link typstCodeParen Noise
430highlight default link typstCodeBrace Noise
431highlight default link typstCodeBracket Noise
432highlight default link typstCodeDollar Noise
433" highlight default link typstHashtagControlFlowError Error
434highlight default link typstHashtagConditional Conditional
435highlight default link typstHashtagRepeat Repeat
436highlight default link typstHashtagKeyword Keyword
437highlight default link typstHashtagConstant Constant
438highlight default link typstHashtagStatementWord Statement
439highlight default link typstHashtagIdentifier Identifier
440highlight default link typstHashtagFieldAccess Identifier
441highlight default link typstHashtagFunction Function
442highlight default link typstHashtagParen Noise
443highlight default link typstHashtagBrace Noise
444highlight default link typstHashtagBracket Noise
445highlight default link typstHashtagDollar Noise
446highlight default link typstMarkupRawInline Macro
447highlight default link typstMarkupRawBlock Macro
448highlight default link typstMarkupLabel Structure
449highlight default link typstMarkupReference Structure
450highlight default link typstMarkupBulletList Structure
451" highlight default link typstMarkupItalicError Error
452" highlight default link typstMarkupBoldError Error
453highlight default link typstMarkupEnumList Structure
454highlight default link typstMarkupLinebreak Structure
455highlight default link typstMarkupNonbreakingSpace Structure
456highlight default link typstMarkupShy Structure
457highlight default link typstMarkupDash Structure
458highlight default link typstMarkupEllipsis Structure
459highlight default link typstMarkupTermList Structure
460highlight default link typstMarkupDollar Noise
461
462" Highlighting > Custom Styling {{{2
463highlight! Conceal ctermfg=NONE ctermbg=NONE guifg=NONE guibg=NONE
464
465highlight default typstMarkupHeading term=underline,bold cterm=underline,bold gui=underline,bold
466highlight default typstMarkupUrl term=underline cterm=underline gui=underline
467highlight default typstMarkupBold term=bold cterm=bold gui=bold
468highlight default typstMarkupItalic term=italic cterm=italic gui=italic
469highlight default typstMarkupBoldItalic term=bold,italic cterm=bold,italic gui=bold,italic
470
471let b:current_syntax = 'typst'
472
473" }}}1