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