xref: /vim-8.2.3635/runtime/scripts.vim (revision c236c16d)
1" Vim support file to detect file types in scripts
2"
3" Maintainer:	Bram Moolenaar <[email protected]>
4" Last change:	2008 Jun 30
5
6" This file is called by an autocommand for every file that has just been
7" loaded into a buffer.  It checks if the type of file can be recognized by
8" the file contents.  The autocommand is in $VIMRUNTIME/filetype.vim.
9
10
11" Only do the rest when the FileType autocommand has not been triggered yet.
12if did_filetype()
13  finish
14endif
15
16" Load the user defined scripts file first
17" Only do this when the FileType autocommand has not been triggered yet
18if exists("myscriptsfile") && filereadable(expand(myscriptsfile))
19  execute "source " . myscriptsfile
20  if did_filetype()
21    finish
22  endif
23endif
24
25" Line continuation is used here, remove 'C' from 'cpoptions'
26let s:cpo_save = &cpo
27set cpo&vim
28
29let s:line1 = getline(1)
30
31if s:line1 =~ "^#!"
32  " A script that starts with "#!".
33
34  " Check for a line like "#!/usr/bin/env VAR=val bash".  Turn it into
35  " "#!/usr/bin/bash" to make matching easier.
36  if s:line1 =~ '^#!\s*\S*\<env\s'
37    let s:line1 = substitute(s:line1, '\S\+=\S\+', '', 'g')
38    let s:line1 = substitute(s:line1, '\<env\s\+', '', '')
39  endif
40
41  " Get the program name.
42  " Only accept spaces in PC style paths: "#!c:/program files/perl [args]".
43  " If the word env is used, use the first word after the space:
44  " "#!/usr/bin/env perl [path/args]"
45  " If there is no path use the first word: "#!perl [path/args]".
46  " Otherwise get the last word after a slash: "#!/usr/bin/perl [path/args]".
47  if s:line1 =~ '^#!\s*\a:[/\\]'
48    let s:name = substitute(s:line1, '^#!.*[/\\]\(\i\+\).*', '\1', '')
49  elseif s:line1 =~ '^#!.*\<env\>'
50    let s:name = substitute(s:line1, '^#!.*\<env\>\s\+\(\i\+\).*', '\1', '')
51  elseif s:line1 =~ '^#!\s*[^/\\ ]*\>\([^/\\]\|$\)'
52    let s:name = substitute(s:line1, '^#!\s*\([^/\\ ]*\>\).*', '\1', '')
53  else
54    let s:name = substitute(s:line1, '^#!\s*\S*[/\\]\(\i\+\).*', '\1', '')
55  endif
56
57  " tcl scripts may have #!/bin/sh in the first line and "exec wish" in the
58  " third line.  Suggested by Steven Atkinson.
59  if getline(3) =~ '^exec wish'
60    let s:name = 'wish'
61  endif
62
63  " Bourne-like shell scripts: bash bash2 ksh ksh93 sh
64  if s:name =~ '^\(bash\d*\|\|ksh\d*\|sh\)\>'
65    call SetFileTypeSH(s:line1)	" defined in filetype.vim
66
67    " csh scripts
68  elseif s:name =~ '^csh\>'
69    if exists("g:filetype_csh")
70      call SetFileTypeShell(g:filetype_csh)
71    else
72      call SetFileTypeShell("csh")
73    endif
74
75    " tcsh scripts
76  elseif s:name =~ '^tcsh\>'
77    call SetFileTypeShell("tcsh")
78
79    " Z shell scripts
80  elseif s:name =~ '^zsh\>'
81    set ft=zsh
82
83    " TCL scripts
84  elseif s:name =~ '^\(tclsh\|wish\|expectk\|itclsh\|itkwish\)\>'
85    set ft=tcl
86
87    " Expect scripts
88  elseif s:name =~ '^expect\>'
89    set ft=expect
90
91    " Gnuplot scripts
92  elseif s:name =~ '^gnuplot\>'
93    set ft=gnuplot
94
95    " Makefiles
96  elseif s:name =~ 'make\>'
97    set ft=make
98
99    " Lua
100  elseif s:name =~ 'lua'
101    set ft=lua
102
103    " Perl
104  elseif s:name =~ 'perl'
105    set ft=perl
106
107    " PHP
108  elseif s:name =~ 'php'
109    set ft=php
110
111    " Python
112  elseif s:name =~ 'python'
113    set ft=python
114
115    " Groovy
116  elseif s:name =~ '^groovy\>'
117    set ft=groovy
118
119    " Ruby
120  elseif s:name =~ 'ruby'
121    set ft=ruby
122
123    " BC calculator
124  elseif s:name =~ '^bc\>'
125    set ft=bc
126
127    " sed
128  elseif s:name =~ 'sed\>'
129    set ft=sed
130
131    " OCaml-scripts
132  elseif s:name =~ 'ocaml'
133    set ft=ocaml
134
135    " Awk scripts
136  elseif s:name =~ 'awk\>'
137    set ft=awk
138
139    " Website MetaLanguage
140  elseif s:name =~ 'wml'
141    set ft=wml
142
143    " Scheme scripts
144  elseif s:name =~ 'scheme'
145    set ft=scheme
146
147    " CFEngine scripts
148  elseif s:name =~ 'cfengine'
149    set ft=cfengine
150
151  endif
152  unlet s:name
153
154else
155  " File does not start with "#!".
156
157  let s:line2 = getline(2)
158  let s:line3 = getline(3)
159  let s:line4 = getline(4)
160  let s:line5 = getline(5)
161
162  " Bourne-like shell scripts: sh ksh bash bash2
163  if s:line1 =~ '^:$'
164    call SetFileTypeSH(s:line1)	" defined in filetype.vim
165
166    " Z shell scripts
167  elseif s:line1 =~ '^#compdef\>' || s:line1 =~ '^#autoload\>'
168    set ft=zsh
169
170  " ELM Mail files
171  elseif s:line1 =~ '^From \([a-zA-Z][a-zA-Z_0-9\.=-]*\(@[^ ]*\)\=\|-\) .* \(19\|20\)\d\d$'
172    set ft=mail
173
174    " Mason
175  elseif s:line1 =~ '^<[%&].*>'
176    set ft=mason
177
178    " Vim scripts (must have '" vim' as the first line to trigger this)
179  elseif s:line1 =~ '^" *[vV]im$'
180    set ft=vim
181
182    " MOO
183  elseif s:line1 =~ '^\*\* LambdaMOO Database, Format Version \%([1-3]\>\)\@!\d\+ \*\*$'
184    set ft=moo
185
186    " Diff file:
187    " - "diff" in first line (context diff)
188    " - "Only in " in first line
189    " - "--- " in first line and "+++ " in second line (unified diff).
190    " - "*** " in first line and "--- " in second line (context diff).
191    " - "# It was generated by makepatch " in the second line (makepatch diff).
192    " - "Index: <filename>" in the first line (CVS file)
193    " - "=== ", line of "=", "---", "+++ " (SVK diff)
194    " - "=== ", "--- ", "+++ " (bzr diff, common case)
195    " - "=== (removed|added|renamed|modified)" (bzr diff, alternative)
196  elseif s:line1 =~ '^\(diff\>\|Only in \|\d\+\(,\d\+\)\=[cda]\d\+\>\|# It was generated by makepatch \|Index:\s\+\f\+\r\=$\|===== \f\+ \d\+\.\d\+ vs edited\|==== //\f\+#\d\+\)'
197	\ || (s:line1 =~ '^--- ' && s:line2 =~ '^+++ ')
198	\ || (s:line1 =~ '^\* looking for ' && s:line2 =~ '^\* comparing to ')
199	\ || (s:line1 =~ '^\*\*\* ' && s:line2 =~ '^--- ')
200	\ || (s:line1 =~ '^=== ' && ((s:line2 =~ '^=\{66\}' && s:line3 =~ '^--- ' && s:line4 =~ '^+++') || (s:line2 =~ '^--- ' && s:line3 =~ '^+++ ')))
201	\ || (s:line1 =~ '^=== \(removed\|added\|renamed\|modified\)')
202    set ft=diff
203
204    " PostScript Files (must have %!PS as the first line, like a2ps output)
205  elseif s:line1 =~ '^%![ \t]*PS'
206    set ft=postscr
207
208    " M4 scripts: Guess there is a line that starts with "dnl".
209  elseif s:line1 =~ '^\s*dnl\>'
210	\ || s:line2 =~ '^\s*dnl\>'
211	\ || s:line3 =~ '^\s*dnl\>'
212	\ || s:line4 =~ '^\s*dnl\>'
213	\ || s:line5 =~ '^\s*dnl\>'
214    set ft=m4
215
216    " AmigaDos scripts
217  elseif $TERM == "amiga"
218	\ && (s:line1 =~ "^;" || s:line1 =~ '^\.[bB][rR][aA]')
219    set ft=amiga
220
221    " SiCAD scripts (must have procn or procd as the first line to trigger this)
222  elseif s:line1 =~? '^ *proc[nd] *$'
223    set ft=sicad
224
225    " Purify log files start with "****  Purify"
226  elseif s:line1 =~ '^\*\*\*\*  Purify'
227    set ft=purifylog
228
229    " XML
230  elseif s:line1 =~ '<?\s*xml.*?>'
231    set ft=xml
232
233    " XHTML (e.g.: PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN")
234  elseif s:line1 =~ '\<DTD\s\+XHTML\s'
235    set ft=xhtml
236
237    " PDF
238  elseif s:line1 =~ '^%PDF-'
239    set ft=pdf
240
241    " XXD output
242  elseif s:line1 =~ '^\x\{7}: \x\{2} \=\x\{2} \=\x\{2} \=\x\{2} '
243    set ft=xxd
244
245    " RCS/CVS log output
246  elseif s:line1 =~ '^RCS file:' || s:line2 =~ '^RCS file:'
247    set ft=rcslog
248
249    " CVS commit
250  elseif s:line2 =~ '^CVS:' || getline("$") =~ '^CVS: '
251    set ft=cvs
252
253    " Prescribe
254  elseif s:line1 =~ '^!R!'
255    set ft=prescribe
256
257    " Send-pr
258  elseif s:line1 =~ '^SEND-PR:'
259    set ft=sendpr
260
261    " SNNS files
262  elseif s:line1 =~ '^SNNS network definition file'
263    set ft=snnsnet
264  elseif s:line1 =~ '^SNNS pattern definition file'
265    set ft=snnspat
266  elseif s:line1 =~ '^SNNS result file'
267    set ft=snnsres
268
269    " Virata
270  elseif s:line1 =~ '^%.\{-}[Vv]irata'
271	\ || s:line2 =~ '^%.\{-}[Vv]irata'
272	\ || s:line3 =~ '^%.\{-}[Vv]irata'
273	\ || s:line4 =~ '^%.\{-}[Vv]irata'
274	\ || s:line5 =~ '^%.\{-}[Vv]irata'
275    set ft=virata
276
277    " Strace
278  elseif s:line1 =~ '^[0-9]* *execve('
279    set ft=strace
280
281    " VSE JCL
282  elseif s:line1 =~ '^\* $$ JOB\>' || s:line1 =~ '^// *JOB\>'
283    set ft=vsejcl
284
285    " TAK and SINDA
286  elseif s:line4 =~ 'K & K  Associates' || s:line2 =~ 'TAK 2000'
287    set ft=takout
288  elseif s:line3 =~ 'S Y S T E M S   I M P R O V E D '
289    set ft=sindaout
290  elseif getline(6) =~ 'Run Date: '
291    set ft=takcmp
292  elseif getline(9) =~ 'Node    File  1'
293    set ft=sindacmp
294
295    " DNS zone files
296  elseif s:line1.s:line2.s:line3.s:line4 =~ '^; <<>> DiG [0-9.]\+ <<>>\|BIND.*named\|$ORIGIN\|$TTL\|IN\s\+SOA'
297    set ft=bindzone
298
299    " BAAN
300  elseif s:line1 =~ '|\*\{1,80}' && s:line2 =~ 'VRC '
301	\ || s:line2 =~ '|\*\{1,80}' && s:line3 =~ 'VRC '
302    set ft=baan
303
304  " Valgrind
305  elseif s:line1 =~ '^==\d\+== valgrind' || s:line3 =~ '^==\d\+== Using valgrind'
306    set ft=valgrind
307
308  " Renderman Interface Bytestream
309  elseif s:line1 =~ '^##RenderMan'
310    set ft=rib
311
312  " Scheme scripts
313  elseif s:line1 =~ 'exec\s\+\S*scheme' || s:line2 =~ 'exec\s\+\S*scheme'
314    set ft=scheme
315
316  " Git output
317  elseif s:line1 =~ '^\(commit\|tree\|object\) \x\{40\}$\|^tag \S\+$'
318    set ft=git
319
320  " CVS diff
321  else
322    let lnum = 1
323    while getline(lnum) =~ "^? " && lnum < line("$")
324      let lnum = lnum + 1
325    endwhile
326    if getline(lnum) =~ '^Index:\s\+\f\+$'
327      set ft=diff
328
329      " locale input files: Formal Definitions of Cultural Conventions
330      " filename must be like en_US, fr_FR@euro or en_US.UTF-8
331    elseif expand("%") =~ '\a\a_\a\a\($\|[.@]\)\|i18n$\|POSIX$\|translit_'
332      let lnum = 1
333      while lnum < 100 && lnum < line("$")
334	if getline(lnum) =~ '^LC_\(IDENTIFICATION\|CTYPE\|COLLATE\|MONETARY\|NUMERIC\|TIME\|MESSAGES\|PAPER\|TELEPHONE\|MEASUREMENT\|NAME\|ADDRESS\)$'
335	  setf fdcc
336	  break
337	endif
338	let lnum = lnum + 1
339      endwhile
340    endif
341
342  endif
343
344  unlet s:line2 s:line3 s:line4 s:line5
345
346endif
347
348" Restore 'cpoptions'
349let &cpo = s:cpo_save
350
351unlet s:cpo_save s:line1
352