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
|
" 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 l:filetype = polyglot#Shebang()
if l:filetype != ""
exec "setf " . l:filetype
return
endif
endfunc
let s:interpreters = {
\ 'osascript': 'applescript',
\ 'tcc': 'c',
\ 'coffee': 'coffee',
\ 'crystal': 'crystal',
\ 'dart': 'dart',
\ 'elixir': 'elixir',
\ 'escript': 'erlang',
\ 'fish': 'fish',
\ 'gnuplot': 'gnuplot',
\ '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 l:line1 = getline(1)
if l:line1 !~# "^#!"
return
endif
let l:pathrest = matchlist(l:line1, s:r_hashbang)
if len(l:pathrest) == 0
return
endif
let [_, l:path, l:rest; __] = l:pathrest
let l:script = split(l:path, "/")[-1]
if l:script == "env"
let l:argspath = matchlist(l:rest, s:r_env)
if len(l:argspath) == 0
return
endif
let l:script = l:argspath[1]
endif
if has_key(s:interpreters, l:script)
return s:interpreters[l:script]
endif
for interpreter in keys(s:interpreters)
if l: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#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
" Restore 'cpoptions'
let &cpo = s:cpo_save
unlet s:cpo_save
|