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