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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
|
" Line continuation is used here, remove 'C' from 'cpoptions'
let s:cpo_save = &cpo
set cpo&vim
func! polyglot#Heuristics()
" Try to detect filetype from shebang
let filetype = polyglot#Shebang()
if filetype != ""
exec "setf " . filetype
return 1
endif
return 0
endfunc
let s:interpreters = {
\ 'osascript': 'applescript',
\ 'tcc': 'c',
\ 'coffee': 'coffee',
\ 'crystal': 'crystal',
\ 'dart': 'dart',
\ 'elixir': 'elixir',
\ 'escript': 'erlang',
\ 'fish': 'fish',
\ 'gnuplot': 'gnuplot',
\ 'groovy': 'groovy',
\ 'runhaskell': 'haskell',
\ 'chakra': 'javascript',
\ 'd8': 'javascript',
\ 'gjs': 'javascript',
\ 'js': 'javascript',
\ 'node': 'javascript',
\ 'nodejs': 'javascript',
\ 'qjs': 'javascript',
\ 'rhino': 'javascript',
\ 'v8': 'javascript',
\ 'v8-shell': 'javascript',
\ 'julia': 'julia',
\ 'lua': 'lua',
\ 'moon': 'moon',
\ 'ocaml': 'ocaml',
\ 'ocamlrun': 'ocaml',
\ 'ocamlscript': 'ocaml',
\ 'cperl': 'perl',
\ 'perl': 'perl',
\ 'php': 'php',
\ 'pwsh': 'ps1',
\ 'python': 'python',
\ 'python2': 'python',
\ 'python3': 'python',
\ 'qmake': 'qmake',
\ 'Rscript': 'r',
\ 'racket': 'racket',
\ 'perl6': 'raku',
\ 'raku': 'raku',
\ 'rakudo': 'raku',
\ 'ruby': 'ruby',
\ 'macruby': 'ruby',
\ 'rake': 'ruby',
\ 'jruby': 'ruby',
\ 'rbx': 'ruby',
\ 'scala': 'scala',
\ 'ash': 'sh',
\ 'bash': 'sh',
\ 'dash': 'sh',
\ 'ksh': 'sh',
\ 'mksh': 'sh',
\ 'pdksh': 'sh',
\ 'rc': 'sh',
\ 'sh': 'sh',
\ 'zsh': 'sh',
\ 'boolector': 'smt2',
\ 'cvc4': 'smt2',
\ 'mathsat5': 'smt2',
\ 'opensmt': 'smt2',
\ 'smtinterpol': 'smt2',
\ 'smt-rat': 'smt2',
\ 'stp': 'smt2',
\ 'verit': 'smt2',
\ 'yices2': 'smt2',
\ 'z3': 'smt2',
\ 'deno': 'typescript',
\ 'ts-node': 'typescript',
\ }
let s:r_hashbang = '^#!\s*\(\S\+\)\s*\(.*\)\s*'
let s:r_envflag = '%(\S\+=\S\+\|-[iS]\|--ignore-environment\|--split-string\)'
let s:r_env = '^\%(\' . s:r_envflag . '\s\+\)*\(\S\+\)'
func! polyglot#Shebang()
let line1 = getline(1)
if line1 !~# "^#!"
return
endif
let pathrest = matchlist(line1, s:r_hashbang)
if len(pathrest) == 0
return
endif
let [_, path, rest; __] = pathrest
let script = split(path, "/")[-1]
if len(script) == 0
return
endif
if script == "env"
let argspath = matchlist(rest, s:r_env)
if len(argspath) == 0
return
endif
let script = argspath[1]
endif
if has_key(s:interpreters, script)
return s:interpreters[script]
endif
for interpreter in keys(s:interpreters)
if script =~# '^' . interpreter
return s:interpreters[interpreter]
endif
endfor
endfunc
func! polyglot#DetectInpFiletype()
let line = getline(1)
if line =~# '^\*'
setf abaqus | return
endif
for lnum in range(1, min([line("$"), 500]))
let line = getline(lnum)
if line =~? '^header surface data'
setf trasys | return
endif
endfor
endfunc
func! polyglot#DetectAsaFiletype()
if exists("g:filetype_asa")
exe "setf " . g:filetype_asa | return
endif
setf aspvbs | return
endfunc
func! polyglot#DetectAspFiletype()
if exists("g:filetype_asp")
exe "setf " . g:filetype_asp | return
endif
for lnum in range(1, min([line("$"), 3]))
let line = getline(lnum)
if line =~? 'perlscript'
setf aspperl | return
endif
endfor
setf aspvbs | return
endfunc
func! polyglot#DetectHFiletype()
for lnum in range(1, min([line("$"), 200]))
let line = getline(lnum)
if line =~# '^\s*\(@\(interface\|class\|protocol\|property\|end\|synchronised\|selector\|implementation\)\(\<\|\>\)\|#import\s\+.\+\.h[">]\)'
if exists("g:c_syntax_for_h")
setf objc | return
endif
setf objcpp | return
endif
endfor
if exists("g:c_syntax_for_h")
setf c | return
endif
if exists("g:ch_syntax_for_h")
setf ch | return
endif
setf cpp | return
endfunc
func! polyglot#DetectMFiletype()
let saw_comment = 0
for lnum in range(1, min([line("$"), 100]))
let line = getline(lnum)
if line =~# '^\s*/\*'
let saw_comment = 1
endif
if line =~# '^\s*\(@\(interface\|class\|protocol\|property\|end\|synchronised\|selector\|implementation\)\(\<\|\>\)\|#import\s\+.\+\.h[">]\)'
setf objc | return
endif
if line =~# '^\s*%'
setf octave | return
endif
if line =~# '^\s*(\*'
setf mma | return
endif
if line =~? '^\s*\(\(type\|var\)\(\<\|\>\)\|--\)'
setf murphi | return
endif
endfor
if saw_comment
setf objc | return
endif
if exists("g:filetype_m")
exe "setf " . g:filetype_m | return
endif
setf octave | return
endfunc
func! polyglot#DetectFsFiletype()
for lnum in range(1, min([line("$"), 50]))
let line = getline(lnum)
if line =~# '^\(: \|new-device\)'
setf forth | return
endif
if line =~# '^\s*\(#light\|import\|let\|module\|namespace\|open\|type\)'
setf fsharp | return
endif
if line =~# '\s*\(#version\|precision\|uniform\|varying\|vec[234]\)'
setf glsl | return
endif
endfor
if exists("g:filetype_fs")
exe "setf " . g:filetype_fs | return
endif
setf forth | return
endfunc
func! polyglot#DetectReFiletype()
for lnum in range(1, min([line("$"), 50]))
let line = getline(lnum)
if line =~# '^\s*#\%(\%(if\|ifdef\|define\|pragma\)\s\+\w\|\s*include\s\+[<"]\|template\s*<\)'
setf cpp | return
endif
setf reason | return
endfor
endfunc
func! polyglot#DetectIdrFiletype()
for lnum in range(1, min([line("$"), 5]))
let line = getline(lnum)
if line =~# '^\s*--.*[Ii]dris \=1'
setf idris | return
endif
if line =~# '^\s*--.*[Ii]dris \=2'
setf idris2 | return
endif
endfor
for lnum in range(1, min([line("$"), 30]))
let line = getline(lnum)
if line =~# '^pkgs =.*'
setf idris | return
endif
if line =~# '^depends =.*'
setf idris2 | return
endif
if line =~# '^%language \(TypeProviders\|ElabReflection\)'
setf idris | return
endif
if line =~# '^%language PostfixProjections'
setf idris2 | return
endif
if line =~# '^%access .*'
setf idris | return
endif
if exists("g:filetype_idr")
exe "setf " . g:filetype_idr | return
endif
endfor
setf idris2 | return
endfunc
func! polyglot#DetectLidrFiletype()
for lnum in range(1, min([line("$"), 200]))
let line = getline(lnum)
if line =~# '^>\s*--.*[Ii]dris \=1'
setf lidris | return
endif
endfor
setf lidris2 | return
endfunc
func! polyglot#DetectBasFiletype()
for lnum in range(1, min([line("$"), 5]))
let line = getline(lnum)
if line =~? 'VB_Name\|Begin VB\.\(Form\|MDIForm\|UserControl\)'
setf vb | return
endif
endfor
setf basic | return
endfunc
" Restore 'cpoptions'
let &cpo = s:cpo_save
unlet s:cpo_save
|