1" Vim support file to detect file types in scripts 2" 3" Maintainer: Bram Moolenaar <[email protected]> 4" Last change: 2005 Sep 20 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 " Bourne-like shell scripts: bash bash2 ksh ksh93 sh 58 if s:name =~ '^\(bash\d*\|\|ksh\d*\|sh\)\>' 59 call SetFileTypeSH(s:line1) " defined in filetype.vim 60 61 " csh scripts 62 elseif s:name =~ '^csh\>' 63 if exists("g:filetype_csh") 64 call SetFileTypeShell(g:filetype_csh) 65 else 66 call SetFileTypeShell("csh") 67 endif 68 69 " tcsh scripts 70 elseif s:name =~ '^tcsh\>' 71 call SetFileTypeShell("tcsh") 72 73 " Z shell scripts 74 elseif s:name =~ '^zsh\>' 75 set ft=zsh 76 77 " TCL scripts 78 elseif s:name =~ '^\(tclsh\|wish\|expectk\|itclsh\|itkwish\)\>' 79 set ft=tcl 80 81 " Expect scripts 82 elseif s:name =~ '^expect\>' 83 set ft=expect 84 85 " Gnuplot scripts 86 elseif s:name =~ '^gnuplot\>' 87 set ft=gnuplot 88 89 " Makefiles 90 elseif s:name =~ 'make\>' 91 set ft=make 92 93 " Lua 94 elseif s:name =~ 'lua' 95 set ft=lua 96 97 " Perl 98 elseif s:name =~ 'perl' 99 set ft=perl 100 101 " PHP 102 elseif s:name =~ 'php' 103 set ft=php 104 105 " Python 106 elseif s:name =~ 'python' 107 set ft=python 108 109 " Groovy 110 elseif s:name =~ '^groovy\>' 111 set ft=groovy 112 113 " Ruby 114 elseif s:name =~ 'ruby' 115 set ft=ruby 116 117 " BC calculator 118 elseif s:name =~ '^bc\>' 119 set ft=bc 120 121 " sed 122 elseif s:name =~ 'sed\>' 123 set ft=sed 124 125 " OCaml-scripts 126 elseif s:name =~ 'ocaml' 127 set ft=ocaml 128 129 " Awk scripts 130 elseif s:name =~ 'awk\>' 131 set ft=awk 132 133 " Website MetaLanguage 134 elseif s:name =~ 'wml' 135 set ft=wml 136 137 " Scheme scripts 138 elseif s:name =~ 'scheme' 139 set ft=scheme 140 141 endif 142 unlet s:name 143 144else 145 " File does not start with "#!". 146 147 let s:line2 = getline(2) 148 let s:line3 = getline(3) 149 let s:line4 = getline(4) 150 let s:line5 = getline(5) 151 152 " Bourne-like shell scripts: sh ksh bash bash2 153 if s:line1 =~ '^:$' 154 call SetFileTypeSH(s:line1) " defined in filetype.vim 155 156 " Z shell scripts 157 elseif s:line1 =~ '^#compdef\>' || s:line1 =~ '^#autoload\>' 158 set ft=zsh 159 160 " ELM Mail files 161 elseif s:line1 =~ '^From [a-zA-Z][a-zA-Z_0-9\.=-]*\(@[^ ]*\)\= .*[12][09]\d\d$' 162 set ft=mail 163 164 " Mason 165 elseif s:line1 =~ '^<[%&].*>' 166 set ft=mason 167 168 " Vim scripts (must have '" vim' as the first line to trigger this) 169 elseif s:line1 =~ '^" *[vV]im$' 170 set ft=vim 171 172 " MOO 173 elseif s:line1 =~ '^\*\* LambdaMOO Database, Format Version \%([1-3]\>\)\@!\d\+ \*\*$' 174 set ft=moo 175 176 " Diff file: 177 " - "diff" in first line (context diff) 178 " - "Only in " in first line 179 " - "--- " in first line and "+++ " in second line (unified diff). 180 " - "*** " in first line and "--- " in second line (context diff). 181 " - "# It was generated by makepatch " in the second line (makepatch diff). 182 " - "Index: <filename>" in the first line (CVS file) 183 elseif s:line1 =~ '^\(diff\>\|Only in \|\d\+\(,\d\+\)\=[cda]\d\+\>\|# It was generated by makepatch \|Index:\s\+\f\+$\|===== \f\+ \d\+\.\d\+ vs edited\|==== //\f\+#\d\+\)' 184 \ || (s:line1 =~ '^--- ' && s:line2 =~ '^+++ ') 185 \ || (s:line1 =~ '^\* looking for ' && s:line2 =~ '^\* comparing to ') 186 \ || (s:line1 =~ '^\*\*\* ' && s:line2 =~ '^--- ') 187 set ft=diff 188 189 " PostScript Files (must have %!PS as the first line, like a2ps output) 190 elseif s:line1 =~ '^%![ \t]*PS' 191 set ft=postscr 192 193 " M4 scripts: Guess there is a line that starts with "dnl". 194 elseif s:line1 =~ '^\s*dnl\>' 195 \ || s:line2 =~ '^\s*dnl\>' 196 \ || s:line3 =~ '^\s*dnl\>' 197 \ || s:line4 =~ '^\s*dnl\>' 198 \ || s:line5 =~ '^\s*dnl\>' 199 set ft=m4 200 201 " AmigaDos scripts 202 elseif $TERM == "amiga" 203 \ && (s:line1 =~ "^;" || s:line1 =~ '^\.[bB][rR][aA]') 204 set ft=amiga 205 206 " SiCAD scripts (must have procn or procd as the first line to trigger this) 207 elseif s:line1 =~? '^ *proc[nd] *$' 208 set ft=sicad 209 210 " Purify log files start with "**** Purify" 211 elseif s:line1 =~ '^\*\*\*\* Purify' 212 set ft=purifylog 213 214 " XML 215 elseif s:line1 =~ '<?\s*xml.*?>' 216 set ft=xml 217 218 " XHTML (e.g.: PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN") 219 elseif s:line1 =~ '\<DTD\s\+XHTML\s' 220 set ft=xhtml 221 222 " XXD output 223 elseif s:line1 =~ '^\x\{7}: \x\{2} \=\x\{2} \=\x\{2} \=\x\{2} ' 224 set ft=xxd 225 226 " RCS/CVS log output 227 elseif s:line1 =~ '^RCS file:' || s:line2 =~ '^RCS file:' 228 set ft=rcslog 229 230 " CVS commit 231 elseif s:line2 =~ '^CVS:' || getline("$") =~ '^CVS: ' 232 set ft=cvs 233 234 " Prescribe 235 elseif s:line1 =~ '^!R!' 236 set ft=prescribe 237 238 " Send-pr 239 elseif s:line1 =~ '^SEND-PR:' 240 set ft=sendpr 241 242 " SNNS files 243 elseif s:line1 =~ '^SNNS network definition file' 244 set ft=snnsnet 245 elseif s:line1 =~ '^SNNS pattern definition file' 246 set ft=snnspat 247 elseif s:line1 =~ '^SNNS result file' 248 set ft=snnsres 249 250 " Virata 251 elseif s:line1 =~ '^%.\{-}[Vv]irata' 252 \ || s:line2 =~ '^%.\{-}[Vv]irata' 253 \ || s:line3 =~ '^%.\{-}[Vv]irata' 254 \ || s:line4 =~ '^%.\{-}[Vv]irata' 255 \ || s:line5 =~ '^%.\{-}[Vv]irata' 256 set ft=virata 257 258 " Strace 259 elseif s:line1 =~ '^[0-9]* *execve(' 260 set ft=strace 261 262 " VSE JCL 263 elseif s:line1 =~ '^\* $$ JOB\>' || s:line1 =~ '^// *JOB\>' 264 set ft=vsejcl 265 266 " TAK and SINDA 267 elseif s:line4 =~ 'K & K Associates' || s:line2 =~ 'TAK 2000' 268 set ft=takout 269 elseif s:line3 =~ 'S Y S T E M S I M P R O V E D ' 270 set ft=sindaout 271 elseif getline(6) =~ 'Run Date: ' 272 set ft=takcmp 273 elseif getline(9) =~ 'Node File 1' 274 set ft=sindacmp 275 276 " DNS zone files 277 elseif s:line1.s:line2 =~ '$ORIGIN\|$TTL\|IN\s*SOA' 278 \ || s:line1.s:line2.s:line3.s:line4 =~ 'BIND.*named' 279 set ft=dns 280 281 " BAAN 282 elseif s:line1 =~ '|\*\{1,80}' && s:line2 =~ 'VRC ' 283 \ || s:line2 =~ '|\*\{1,80}' && s:line3 =~ 'VRC ' 284 set ft=baan 285 286 " Valgrind 287 elseif s:line1 =~ '^==\d\+== valgrind' 288 set ft=valgrind 289 290 " Renderman Interface Bytestream 291 elseif s:line1 =~ '^##RenderMan' 292 set ft=rib 293 294 " Scheme scripts 295 elseif s:line1 =~ 'exec\s\+\S*scheme' || s:line2 =~ 'exec\s\+\S*scheme' 296 set ft=scheme 297 298 " CVS diff 299 else 300 let lnum = 1 301 while getline(lnum) =~ "^? " && lnum < line("$") 302 let lnum = lnum + 1 303 endwhile 304 if getline(lnum) =~ '^Index:\s\+\f\+$' 305 set ft=diff 306 307 " locale input files: Formal Definitions of Cultural Conventions 308 " filename must be like en_US, fr_FR@euro or en_US.UTF-8 309 elseif expand("%") =~ '\a\a_\a\a\($\|[.@]\)\|i18n$\|POSIX$\|translit_' 310 let lnum = 1 311 while lnum < 100 && lnum < line("$") 312 if getline(lnum) =~ '^LC_\(IDENTIFICATION\|CTYPE\|COLLATE\|MONETARY\|NUMERIC\|TIME\|MESSAGES\|PAPER\|TELEPHONE\|MEASUREMENT\|NAME\|ADDRESS\)$' 313 setf fdcc 314 break 315 endif 316 let lnum = lnum + 1 317 endwhile 318 endif 319 320 endif 321 322 unlet s:line2 s:line3 s:line4 s:line5 323 324endif 325 326" Restore 'cpoptions' 327let &cpo = s:cpo_save 328 329unlet s:cpo_save s:line1 330