summaryrefslogtreecommitdiff
path: root/rc/filetype/typst.kak
blob: 5353ea6a6b444f9370e44d0bd8dad60176cf1c20 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# Detection
# ‾‾‾‾‾‾‾‾‾

hook global BufCreate .*[.](typ) %{
    set-option buffer filetype typst
}

# Initialization
# ‾‾‾‾‾‾‾‾‾‾‾‾‾‾

hook -group typst-highlight global WinSetOption filetype=typst %{
    require-module typst

    add-highlighter window/typst ref typst
    hook -once -always window WinSetOption filetype=.* %{ remove-highlighter window/typst }
    hook window InsertChar \n -group typst typst-on-new-line
}

provide-module typst %§

# Highlighters
# ‾‾‾‾‾‾‾‾‾‾‾‾

add-highlighter shared/typst group

# Comments
add-highlighter shared/typst/ regex ^//(?:[^\n/][^\n]*|)$ 0:comment

# Strings
add-highlighter shared/typst/ regex '"[^"]*"' 0:string

# Headings
add-highlighter shared/typst/ regex ^=+\h+[^\n]+$ 0:header

# Code blocks
# Raw with optional syntax highlighting
add-highlighter shared/typst/ regex '^```[^(```)]*```' 0:mono
# Multiline monospace
add-highlighter shared/typst/ regex '^`[^(`)]*`' 0:mono

# Monospace text
add-highlighter shared/typst/ regex \B(`[^\n]+?`)\B 0:mono
add-highlighter shared/typst/ regex \B(```[^\n]+?```)\B 0:mono

# Bold text
add-highlighter shared/typst/ regex \s\*[^\*]+\*\B 0:+b

# Italic text
add-highlighter shared/typst/ regex \b_.*?_\b 0:+i

# Code expressions: functions, variables
add-highlighter shared/typst/ regex (^|\h)#(\w|\.|-)+ 0:meta

# Bold terms in term lists
add-highlighter shared/typst/ regex ^/\h[^:]*: 0:+b

§

# Commands
# ‾‾‾‾‾‾‾‾
 
define-command -hidden typst-on-new-line %<
    evaluate-commands -draft -itersel %<
        # Preserve previous line indent
        try %{ execute-keys -draft <semicolon> K <a-&> }
        # Cleanup trailing whitespaces from previous line
        try %{ execute-keys -draft k x s \h+$ <ret> d }
    >
>