/runtime/scripts.vim

https://bitbucket.org/ultra_iter/vim-qt · Vim Script · 361 lines · 197 code · 74 blank · 90 comment · 49 complexity · b9abd17f6486bfad1e0daa3f3df2d050 MD5 · raw file

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