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