@@ -19,3 +19,116 @@ if get(g:, 'nix_recommended_style', 1)
19
19
\ softtabstop = 2
20
20
\ expandtab
21
21
endif
22
+
23
+ " Borrowed from vim-markdown: https://github.com/plasticboy/vim-markdown/
24
+ if exists (' g:vim_nix_fenced_languages' )
25
+ let s: filetype_dict = {}
26
+ for s: filetype in g: vim_markdown_fenced_languages
27
+ let key = matchstr (s: filetype , " [^=]*" )
28
+ let val = matchstr (s: filetype , " [^=]*$" )
29
+ let s: filetype_dict [key ] = val
30
+ endfor
31
+ else
32
+ let s: filetype_dict = {
33
+ \ ' c++' : ' cpp' ,
34
+ \ ' viml' : ' vim' ,
35
+ \ ' bash' : ' sh' ,
36
+ \ ' ini' : ' dosini'
37
+ \ }
38
+ endif
39
+
40
+ function ! s: NixHighlightSources (force)
41
+ " Syntax highlight source code embedded in notes.
42
+ " Look for code blocks in the current file
43
+ let filetypes = {}
44
+ for line in getline (1 , ' $' )
45
+ let ft = matchstr (line , " /\\ *\\ s*\\ zs[0-9A-Za-z_+-]*\\ ze\\ s*\\ */\\ s*''" )
46
+ if ! empty (ft ) && ft !~ ' ^\d*$' | let filetypes[ft ] = 1 | endif
47
+ endfor
48
+ if ! exists (' b:nix_known_filetypes' )
49
+ let b: nix_known_filetypes = {}
50
+ endif
51
+ if ! exists (' b:nix_included_filetypes' )
52
+ " set syntax file name included
53
+ let b: nix_included_filetypes = {}
54
+ endif
55
+ if ! a: force && (b: nix_known_filetypes == filetypes || empty (filetypes))
56
+ return
57
+ endif
58
+
59
+ " Now we're ready to actually highlight the code blocks.
60
+ for ft in keys (filetypes)
61
+ if a: force || ! has_key (b: nix_known_filetypes , ft )
62
+ if has_key (s: filetype_dict , ft )
63
+ let filetype = s: filetype_dict [ft ]
64
+ else
65
+ let filetype = ft
66
+ endif
67
+ let group = ' nixSnippet' . toupper (substitute (filetype , " [+-]" , " _" , " g" ))
68
+ if ! has_key (b: nix_included_filetypes , filetype )
69
+ let include = s: SyntaxInclude (filetype )
70
+ let b: nix_included_filetypes [filetype ] = 1
71
+ else
72
+ let include = ' @' . toupper (filetype )
73
+ endif
74
+ let command = " syn region %s matchgroup=nixCodeStart start=@/\\ *\\ s*%s\\ s*\\ */\\ s*''@ matchgroup=NONE skip=+''['$\\\\ ]+ matchgroup=nixCodeEnd end=+''+ keepend extend contains=nixInterpolation,nixStringSpecial,nixInvalidStringEscape,%s"
75
+ execute printf (command , group, ft , include )
76
+ execute printf (" syn cluster nixExpr add=%s" , group)
77
+ execute printf (" syn region nixInterpolation matchgroup=nixInterpolationDelimiter start=+\\ (''\\ )\\ @<!\\ ${+ end=+}+ containedin=%s contains=@nixExpr,nixInterpolationParam" , include )
78
+ execute printf (" syn match nixStringSpecial /''\\ $/me=e-1 containedin=%s" , include )
79
+ execute printf (" syn match nixStringSpecial /'''/me=e-2 containedin=%s" , include )
80
+ execute printf (" syn match nixStringSpecial /''\\\\ [nrt]/ containedin=%s" , include )
81
+ execute printf (" syn match nixInvalidStringEscape /''\\\\ [^nrt]/ containedin=%s" , include )
82
+
83
+ let b: nix_known_filetypes [ft ] = 1
84
+ endif
85
+ endfor
86
+ endfunction
87
+
88
+ function ! s: SyntaxInclude (filetype )
89
+ " Include the syntax highlighting of another {filetype}.
90
+ let grouplistname = ' @' . toupper (a: filetype )
91
+ " Unset the name of the current syntax while including the other syntax
92
+ " because some syntax scripts do nothing when "b:current_syntax" is set
93
+ if exists (' b:current_syntax' )
94
+ let syntax_save = b: current_syntax
95
+ unlet b: current_syntax
96
+ endif
97
+ try
98
+ execute ' syntax include' grouplistname ' syntax/' . a: filetype . ' .vim'
99
+ execute ' syntax include' grouplistname ' after/syntax/' . a: filetype . ' .vim'
100
+ catch /E484/
101
+ " Ignore missing scripts
102
+ endtry
103
+ " Restore the name of the current syntax
104
+ if exists (' syntax_save' )
105
+ let b: current_syntax = syntax_save
106
+ elseif exists (' b:current_syntax' )
107
+ unlet b: current_syntax
108
+ endif
109
+ return grouplistname
110
+ endfunction
111
+
112
+
113
+ function ! s: NixRefreshSyntax (force)
114
+ if &filetype = ~ ' nix' && line (' $' ) > 1
115
+ call s: NixHighlightSources (a: force )
116
+ endif
117
+ endfunction
118
+
119
+ function ! s: NixClearSyntaxVariables ()
120
+ if &filetype = ~ ' nix'
121
+ unlet ! b: nix_included_filetypes
122
+ endif
123
+ endfunction
124
+
125
+ augroup Nix
126
+ " These autocmd calling s:NixRefreshSyntax need to be kept in sync with
127
+ " the autocmds calling s:NixSetupFolding in after/ftplugin/markdown.vim.
128
+ autocmd ! * <buffer>
129
+ autocmd BufWinEnter <buffer> call s: NixRefreshSyntax (1 )
130
+ autocmd BufUnload <buffer> call s: NixClearSyntaxVariables ()
131
+ autocmd BufWritePost <buffer> call s: NixRefreshSyntax (0 )
132
+ autocmd InsertEnter ,InsertLeave <buffer> call s: NixRefreshSyntax (0 )
133
+ autocmd CursorHold ,CursorHoldI <buffer> call s: NixRefreshSyntax (0 )
134
+ augroup END
0 commit comments