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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
|
" Please do not edit this file directly, instead modify polyglot.vim or scripts/build
" Line continuation is used here, remove 'C' from 'cpoptions'
let s:cpo_save = &cpo
set cpo&vim
func! polyglot#Shebang()
if getline(1) =~# "^#!"
let ft = polyglot#ShebangFiletype()
if ft != ""
let &ft = ft
endif
endif
if &ft == ""
runtime! scripts.vim
endif
return &ft != ""
endfunc
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#ShebangFiletype()
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
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',
\ 'swipl': 'prolog',
\ 'yap': 'prolog',
\ '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',
\ }
func! polyglot#DetectInpFiletype()
let line = getline(nextnonblank(1))
if line =~# '^\*'
set ft=abaqus | return
endif
for lnum in range(1, min([line("$"), 500]))
let line = getline(lnum)
if line =~? '^header surface data'
set ft=trasys | return
endif
endfor
endfunc
func! polyglot#DetectAsaFiletype()
if exists("g:filetype_asa")
let &ft = g:filetype_asa | return
endif
set ft=aspvbs | return
endfunc
func! polyglot#DetectAspFiletype()
if exists("g:filetype_asp")
let &ft = g:filetype_asp | return
endif
for lnum in range(1, min([line("$"), 3]))
let line = getline(lnum)
if line =~? 'perlscript'
set ft=aspperl | return
endif
endfor
set ft=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")
set ft=objc | return
endif
set ft=objcpp | return
endif
endfor
if exists("g:c_syntax_for_h")
set ft=c | return
endif
if exists("g:ch_syntax_for_h")
set ft=ch | return
endif
set ft=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[">]\)'
set ft=objc | return
endif
if line =~# '^\s*%'
set ft=octave | return
endif
if line =~# '^\s*(\*'
set ft=mma | return
endif
if line =~? '^\s*\(\(type\|var\)\(\<\|\>\)\|--\)'
set ft=murphi | return
endif
endfor
if saw_comment
set ft=objc | return
endif
if exists("g:filetype_m")
let &ft = g:filetype_m | return
endif
set ft=octave | return
endfunc
func! polyglot#DetectFsFiletype()
for lnum in range(1, min([line("$"), 50]))
let line = getline(lnum)
if line =~# '^\(: \|new-device\)'
set ft=forth | return
endif
if line =~# '^\s*\(#light\|import\|let\|module\|namespace\|open\|type\)'
set ft=fsharp | return
endif
if line =~# '\s*\(#version\|precision\|uniform\|varying\|vec[234]\)'
set ft=glsl | return
endif
endfor
if exists("g:filetype_fs")
let &ft = g:filetype_fs | return
endif
set ft=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*<\)'
set ft=cpp | return
endif
set ft=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'
set ft=idris | return
endif
if line =~# '^\s*--.*[Ii]dris \=2'
set ft=idris2 | return
endif
endfor
for lnum in range(1, min([line("$"), 30]))
let line = getline(lnum)
if line =~# '^pkgs =.*'
set ft=idris | return
endif
if line =~# '^depends =.*'
set ft=idris2 | return
endif
if line =~# '^%language \(TypeProviders\|ElabReflection\)'
set ft=idris | return
endif
if line =~# '^%language PostfixProjections'
set ft=idris2 | return
endif
if line =~# '^%access .*'
set ft=idris | return
endif
endfor
if exists("g:filetype_idr")
let &ft = g:filetype_idr | return
endif
set ft=idris2 | return
endfunc
func! polyglot#DetectLidrFiletype()
for lnum in range(1, min([line("$"), 200]))
let line = getline(lnum)
if line =~# '^>\s*--.*[Ii]dris \=1'
set ft=lidris | return
endif
endfor
set ft=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\)'
set ft=vb | return
endif
endfor
set ft=basic | return
endfunc
func! polyglot#DetectPmFiletype()
let line = getline(nextnonblank(1))
if line =~# 'XPM2'
set ft=xpm2 | return
endif
if line =~# 'XPM'
set ft=xpm | return
endif
for lnum in range(1, min([line("$"), 50]))
let line = getline(lnum)
if line =~# '^\s*\%(use\s\+v6\(\<\|\>\)\|\(\<\|\>\)module\(\<\|\>\)\|\(\<\|\>\)\%(my\s\+\)\=class\(\<\|\>\)\)'
set ft=raku | return
endif
if line =~# '\(\<\|\>\)use\s\+\%(strict\(\<\|\>\)\|v\=5\.\)'
set ft=perl | return
endif
endfor
if exists("g:filetype_pm")
let &ft = g:filetype_pm | return
endif
set ft=perl | return
endfunc
func! polyglot#DetectPlFiletype()
let line = getline(nextnonblank(1))
if line =~# '^[^#]*:-' || line =~# '^\s*\%(%\|/\*\)' || line =~# '\.\s*$'
set ft=prolog | return
endif
for lnum in range(1, min([line("$"), 50]))
let line = getline(lnum)
if line =~# '^\s*\%(use\s\+v6\(\<\|\>\)\|\(\<\|\>\)module\(\<\|\>\)\|\(\<\|\>\)\%(my\s\+\)\=class\(\<\|\>\)\)'
set ft=raku | return
endif
if line =~# '\(\<\|\>\)use\s\+\%(strict\(\<\|\>\)\|v\=5\.\)'
set ft=perl | return
endif
endfor
if exists("g:filetype_pl")
let &ft = g:filetype_pl | return
endif
set ft=perl | return
endfunc
func! polyglot#DetectTFiletype()
for lnum in range(1, min([line("$"), 5]))
let line = getline(lnum)
if line =~# '^\.'
set ft=nroff | return
endif
endfor
for lnum in range(1, min([line("$"), 50]))
let line = getline(lnum)
if line =~# '^\s*\%(use\s\+v6\(\<\|\>\)\|\(\<\|\>\)module\(\<\|\>\)\|\(\<\|\>\)\%(my\s\+\)\=class\(\<\|\>\)\)'
set ft=raku | return
endif
if line =~# '\(\<\|\>\)use\s\+\%(strict\(\<\|\>\)\|v\=5\.\)'
set ft=perl | return
endif
endfor
if exists("g:filetype_t")
let &ft = g:filetype_t | return
endif
set ft=perl | return
endfunc
func! polyglot#DetectTt2Filetype()
for lnum in range(1, min([line("$"), 3]))
let line = getline(lnum)
if line =~? '<\%(!DOCTYPE HTML\|[%?]\|html\)'
set ft=tt2html | return
endif
endfor
set ft=tt2 | return
endfunc
func! polyglot#DetectHtmlFiletype()
let line = getline(nextnonblank(1))
if line =~# '^\(%\|<[%&].*>\)'
set ft=mason | return
endif
for lnum in range(1, min([line("$"), 50]))
let line = getline(lnum)
if line =~# '{{.*}}\|{%-\=\s*\(end.*\|extends\|block\|macro\|set\|if\|for\|include\|trans\)\(\<\|\>\)\|{#\s\+'
set ft=htmldjango | return
endif
if line =~# '\(\<\|\>\)DTD\s\+XHTML\s'
set ft=xhtml | return
endif
endfor
set ft=html | au! BufWritePost <buffer> ++once call polyglot#DetectHtmlFiletype()
return
endfunc
" Restore 'cpoptions'
let &cpo = s:cpo_save
unlet s:cpo_save
""" ftdetect/polyglot.vim
" don't spam the user when Vim is started in Vi compatibility mode
let s:cpo_save = &cpo
set cpo&vim
" Disable all native vim ftdetect
if exists('g:polyglot_test')
autocmd!
endif
func! polyglot#Observe(fn)
let b:polyglot_observe = a:fn
augroup polyglot-observer
au! CursorHold,CursorHoldI,BufWritePost <buffer>
\ execute('if polyglot#' . b:polyglot_observe . '() | au! polyglot-observer | endif')
augroup END
endfunc
let s:disabled_packages = {}
let s:new_polyglot_disabled = []
if exists('g:polyglot_disabled')
for pkg in g:polyglot_disabled
let base = split(pkg, '\.')
if len(base) > 0
let s:disabled_packages[pkg] = 1
call add(s:new_polyglot_disabled, base[0])
endif
endfor
else
let g:polyglot_disabled_not_set = 1
endif
function! s:SetDefault(name, value)
if !exists(a:name)
let {a:name} = a:value
endif
endfunction
call s:SetDefault('g:markdown_enable_spell_checking', 0)
call s:SetDefault('g:markdown_enable_input_abbreviations', 0)
call s:SetDefault('g:markdown_enable_mappings', 0)
" Enable jsx syntax by default
call s:SetDefault('g:jsx_ext_required', 0)
" Needed for sql highlighting
call s:SetDefault('g:javascript_sql_dialect', 'sql')
" Make csv loading faster
call s:SetDefault('g:csv_start', 1)
call s:SetDefault('g:csv_end', 2)
" Disable json concealing by default
call s:SetDefault('g:vim_json_syntax_conceal', 0)
call s:SetDefault('g:filetype_euphoria', 'elixir')
if !exists('g:python_highlight_all')
call s:SetDefault('g:python_highlight_builtins', 1)
call s:SetDefault('g:python_highlight_builtin_objs', 1)
call s:SetDefault('g:python_highlight_builtin_types', 1)
call s:SetDefault('g:python_highlight_builtin_funcs', 1)
call s:SetDefault('g:python_highlight_builtin_funcs_kwarg', 1)
call s:SetDefault('g:python_highlight_exceptions', 1)
call s:SetDefault('g:python_highlight_string_formatting', 1)
call s:SetDefault('g:python_highlight_string_format', 1)
call s:SetDefault('g:python_highlight_string_templates', 1)
call s:SetDefault('g:python_highlight_indent_errors', 1)
call s:SetDefault('g:python_highlight_space_errors', 1)
call s:SetDefault('g:python_highlight_doctests', 1)
call s:SetDefault('g:python_highlight_func_calls', 1)
call s:SetDefault('g:python_highlight_class_vars', 1)
call s:SetDefault('g:python_highlight_operators', 1)
call s:SetDefault('g:python_highlight_file_headers_as_comments', 1)
call s:SetDefault('g:python_slow_sync', 1)
endif
" We need it because scripts.vim in vim uses "set ft=" which cannot be
" overridden with setf (and we can't use set ft= so our scripts.vim work)
func! s:Setf(ft)
if &filetype !~# '\<'.a:ft.'\>'
let &filetype = a:ft
endif
endfunc
" Function used for patterns that end in a star: don't set the filetype if the
" file name matches ft_ignore_pat.
" When using this, the entry should probably be further down below with the
" other StarSetf() calls.
func! s:StarSetf(ft)
if expand("<amatch>") !~ g:ft_ignore_pat && &filetype !~# '\<'.a:ft.'\>'
let &filetype = a:ft
endif
endfunc
augroup filetypedetect
" scripts/build inserts here filetype detection autocommands
au! BufNewFile,BufRead,StdinReadPost * if expand("<afile>") !~ g:ft_ignore_pat |
\ call polyglot#Shebang() | endif
au BufEnter * if &ft == "" && expand("<afile>") !~ g:ft_ignore_pat |
\ call polyglot#Observe('Shebang') | endif
augroup END
if !has_key(s:disabled_packages, 'autoindent')
" Code below re-implements sleuth for vim-polyglot
let g:loaded_sleuth = 1
let g:loaded_foobar = 1
" Makes shiftwidth to be synchronized with tabstop by default
if &shiftwidth == &tabstop
let &shiftwidth = 0
endif
function! s:guess(lines) abort
let options = {}
let ccomment = 0
let podcomment = 0
let triplequote = 0
let backtick = 0
let xmlcomment = 0
let heredoc = ''
let minindent = 10
let spaces_minus_tabs = 0
let i = 0
for line in a:lines
let i += 1
if !len(line) || line =~# '^\W*$'
continue
endif
if line =~# '^\s*/\*'
let ccomment = 1
endif
if ccomment
if line =~# '\*/'
let ccomment = 0
endif
continue
endif
if line =~# '^=\w'
let podcomment = 1
endif
if podcomment
if line =~# '^=\%(end\|cut\)\>'
let podcomment = 0
endif
continue
endif
if triplequote
if line =~# '^[^"]*"""[^"]*$'
let triplequote = 0
endif
continue
elseif line =~# '^[^"]*"""[^"]*$'
let triplequote = 1
endif
if backtick
if line =~# '^[^`]*`[^`]*$'
let backtick = 0
endif
continue
elseif &filetype ==# 'go' && line =~# '^[^`]*`[^`]*$'
let backtick = 1
endif
if line =~# '^\s*<\!--'
let xmlcomment = 1
endif
if xmlcomment
if line =~# '-->'
let xmlcomment = 0
endif
continue
endif
" This is correct order because both "<<EOF" and "EOF" matches end
if heredoc != ''
if line =~# heredoc
let heredoc = ''
endif
continue
endif
let herematch = matchlist(line, '\C<<\W*\([A-Z]\+\)\s*$')
if len(herematch) > 0
let heredoc = herematch[1] . '$'
endif
let spaces_minus_tabs += line[0] == "\t" ? 1 : -1
if line[0] == "\t"
setlocal noexpandtab
let &shiftwidth=&tabstop
let b:sleuth_culprit .= ':' . i
return 1
elseif line[0] == " "
let indent = len(matchstr(line, '^ *'))
if (indent % 2 == 0 || indent % 3 == 0) && indent < minindent
let minindent = indent
endif
endif
endfor
if minindent < 10
setlocal expandtab
let &shiftwidth=minindent
let b:sleuth_culprit .= ':' . i
return 1
endif
return 0
endfunction
function! s:detect_indent() abort
if &buftype ==# 'help'
return
endif
let b:sleuth_culprit = expand("<afile>:p")
if s:guess(getline(1, 32))
return
endif
let pattern = sleuth#GlobForFiletype(&filetype)
if len(pattern) == 0
return
endif
let pattern = '{' . pattern . ',.git,.svn,.hg}'
let dir = expand('%:p:h')
let level = 3
while isdirectory(dir) && dir !=# fnamemodify(dir, ':h') && level > 0
" Ignore files from homedir and root
if dir == expand('~') || dir == '/'
unlet b:sleuth_culprit
return
endif
for neighbor in glob(dir . '/' . pattern, 0, 1)[0:level]
let b:sleuth_culprit = neighbor
" Do not consider directories above .git, .svn or .hg
if fnamemodify(neighbor, ":h:t")[0] == "."
let level = 0
continue
endif
if neighbor !=# expand('%:p') && filereadable(neighbor)
if s:guess(readfile(neighbor, '', 32))
return
endif
endif
endfor
let dir = fnamemodify(dir, ':h')
let level -= 1
endwhile
unlet b:sleuth_culprit
endfunction
setglobal smarttab
function! SleuthIndicator() abort
let sw = &shiftwidth ? &shiftwidth : &tabstop
if &expandtab
return 'sw='.sw
elseif &tabstop == sw
return 'ts='.&tabstop
else
return 'sw='.sw.',ts='.&tabstop
endif
endfunction
augroup polyglot-sleuth
au!
au FileType * call s:detect_indent()
au User Flags call Hoist('buffer', 5, 'SleuthIndicator')
augroup END
command! -bar -bang Sleuth call s:detect_indent()
endif
func! s:verify()
if exists("g:polyglot_disabled_not_set")
if exists("g:polyglot_disabled")
echohl WarningMsg
echo "vim-polyglot: g:polyglot_disabled should be defined before loading vim-polyglot"
echohl None
endif
unlet g:polyglot_disabled_not_set
endif
endfunc
au VimEnter * call s:verify()
" Save polyglot_disabled without postfixes
if exists('g:polyglot_disabled')
let g:polyglot_disabled = s:new_polyglot_disabled
endif
" restore Vi compatibility settings
let &cpo = s:cpo_save
|