script.vim (13004B)
1 vim9script 2 3 # Vim function for detecting a filetype from the file contents. 4 # Invoked from "scripts.vim" in 'runtimepath' 5 # 6 # Maintainer: The Vim Project <https://github.com/vim/vim> 7 # Last Change: 2026 Apr 09 8 # Former Maintainer: Bram Moolenaar <Bram@vim.org> 9 10 export def DetectFiletype() 11 var line1 = getline(1) 12 if line1[0] == '#' && line1[1] == '!' 13 # File that starts with "#!". 14 DetectFromHashBang(line1) 15 else 16 # File does not start with "#!". 17 DetectFromText(line1) 18 endif 19 enddef 20 21 # Called for a script that has "#!" in the first line. 22 def DetectFromHashBang(firstline: string) 23 var line1 = firstline 24 25 # Check for a line like "#!/usr/bin/env {options} bash". Turn it into 26 # "#!/usr/bin/bash" to make matching easier. 27 # Recognize only a few {options} that are commonly used. 28 if line1 =~ '^#!\s*\S*\<env\s' 29 line1 = substitute(line1, '\s\zs--split-string[ \t=]', '', '') 30 line1 = substitute(line1, '\s\zs[A-Za-z0-9_]\+=\S*\ze\s', '', 'g') 31 line1 = substitute(line1, '\s\zs\%(-[iS]\+\|--ignore-environment\)\ze\s', '', 'g') 32 line1 = substitute(line1, '\<env\s\+', '', '') 33 endif 34 35 # Get the program name. 36 # Only accept spaces in PC style paths: "#!c:/program files/perl [args]". 37 # If the word env is used, use the first word after the space: 38 # "#!/usr/bin/env perl [path/args]" 39 # If there is no path use the first word: "#!perl [path/args]". 40 # Otherwise get the last word after a slash: "#!/usr/bin/perl [path/args]". 41 var name: string 42 if line1 =~ '^#!\s*\a:[/\\]' 43 name = substitute(line1, '^#!.*[/\\]\(\i\+\).*', '\1', '') 44 elseif line1 =~ '^#!.*\<env\>' 45 name = substitute(line1, '^#!.*\<env\>\s\+\(\i\+\).*', '\1', '') 46 elseif line1 =~ '^#!\s*[^/\\ ]*\>\([^/\\]\|$\)' 47 name = substitute(line1, '^#!\s*\([^/\\ ]*\>\).*', '\1', '') 48 elseif line1 =~ '^#!.*\<busybox\>' 49 name = substitute(line1, '^#!.*\<busybox\>\s\+\(\i\+\).*', '\1', '') 50 else 51 name = substitute(line1, '^#!\s*\S*[/\\]\(\f\+\).*', '\1', '') 52 endif 53 54 # tcl scripts may have #!/bin/sh in the first line and "exec wish" in the 55 # third line. Suggested by Steven Atkinson. 56 if getline(3) =~ '^exec wish' 57 name = 'wish' 58 endif 59 60 var ft = Exe2filetype(name, line1) 61 if ft != '' 62 exe 'setl ft=' .. ft 63 endif 64 enddef 65 66 # Returns the filetype name associated with program "name". 67 # "line1" is the #! line at the top of the file. Use the same as "name" if 68 # not available. 69 # Returns an empty string when not recognized. 70 export def Exe2filetype(name: string, line1: string): string 71 # Bourne-like shell scripts: bash bash2 dash ksh ksh93 sh 72 if name =~ '^\(bash\d*\|d\?ash\|ksh\d*\|sh\)\>' 73 return dist#ft#SetFileTypeSH(line1, false) 74 75 # csh scripts 76 elseif name =~ '^csh\>' 77 return dist#ft#SetFileTypeShell(exists("g:filetype_csh") ? g:filetype_csh : 'csh', false) 78 79 # tcsh scripts 80 elseif name =~ '^tcsh\>' 81 return dist#ft#SetFileTypeShell("tcsh", false) 82 83 # Z shell scripts 84 elseif name =~ '^zsh\>' 85 return 'zsh' 86 87 # TCL scripts 88 elseif name =~ '^\(tclsh\|wish\|expectk\|itclsh\|itkwish\)\>' 89 return 'tcl' 90 91 # Expect scripts 92 elseif name =~ '^expect\>' 93 return 'expect' 94 95 # Gnuplot scripts 96 elseif name =~ '^gnuplot\>' 97 return 'gnuplot' 98 99 # Makefiles 100 elseif name =~ 'make\>' 101 return 'make' 102 103 # Pike 104 elseif name =~ '^pike\%(\>\|[0-9]\)' 105 return 'pike' 106 107 # Lua 108 elseif name =~ 'lua' 109 return 'lua' 110 111 # Perl 112 elseif name =~ 'perl' 113 return 'perl' 114 115 # PHP 116 elseif name =~ 'php' 117 return 'php' 118 119 # Python 120 elseif name =~ 'python' || (name == 'uv' && line1 =~ '\<uv run\>') 121 return 'python' 122 123 # Groovy 124 elseif name =~ '^groovy\>' 125 return 'groovy' 126 127 # Raku 128 elseif name =~ 'raku' 129 return 'raku' 130 131 # Ruby 132 elseif name =~ 'ruby' 133 return 'ruby' 134 135 # JavaScript 136 elseif name =~ 'node\(js\)\=\>\|js\>' || name =~ 'rhino\>' 137 return 'javascript' 138 139 elseif name =~# 'just' 140 return 'just' 141 142 # BC calculator 143 elseif name =~ '^bc\>' 144 return 'bc' 145 146 # sed 147 elseif name =~ 'sed\>' 148 return 'sed' 149 150 # OCaml-scripts 151 elseif name =~ 'ocaml' 152 return 'ocaml' 153 154 # Awk scripts; also finds "gawk" 155 elseif name =~ 'awk\>' 156 return 'awk' 157 158 # Website MetaLanguage 159 elseif name =~ 'wml' 160 return 'wml' 161 162 # Scheme scripts 163 elseif name =~ 'scheme' 164 return 'scheme' 165 166 # CFEngine scripts 167 elseif name =~ 'cfengine' 168 return 'cfengine' 169 170 # Erlang scripts 171 elseif name =~ 'escript' 172 return 'erlang' 173 174 # Haskell 175 elseif name =~ 'haskell' 176 return 'haskell' 177 178 # Scala 179 elseif name =~ 'scala\>' 180 return 'scala' 181 182 # Clojure 183 elseif name =~ 'clojure' 184 return 'clojure' 185 186 # Free Pascal 187 elseif name =~ 'instantfpc\>' 188 return 'pascal' 189 190 # Fennel 191 elseif name =~ 'fennel\>' 192 return 'fennel' 193 194 # MikroTik RouterOS script 195 elseif name =~ 'rsc\>' 196 return 'routeros' 197 198 # Fish shell 199 elseif name =~ 'fish\>' 200 return 'fish' 201 202 # Gforth 203 elseif name =~ 'gforth\>' 204 return 'forth' 205 206 # Icon 207 elseif name =~ 'icon\>' 208 return 'icon' 209 210 # Guile 211 elseif name =~ 'guile' 212 return 'scheme' 213 214 # Nix 215 elseif name =~ 'nix-shell' 216 return 'nix' 217 218 # Crystal 219 elseif name =~ '^crystal\>' 220 return 'crystal' 221 222 # Rexx 223 elseif name =~ '^\%(rexx\|regina\)\>' 224 return 'rexx' 225 226 # Janet 227 elseif name =~ '^janet\>' 228 return 'janet' 229 230 # Dart 231 elseif name =~ '^dart\>' 232 return 'dart' 233 234 # Execline (s6) 235 elseif name =~ '^execlineb\>' 236 return 'execline' 237 238 # Bpftrace 239 elseif name =~ '^bpftrace\>' 240 return 'bpftrace' 241 242 # Vim 243 elseif name =~ '^vim\>' 244 return 'vim' 245 246 # ed(1) 247 elseif name =~ '^ed\>' 248 return 'ed' 249 250 endif 251 252 return '' 253 enddef 254 255 256 # Called for a script that does not have "#!" in the first line. 257 def DetectFromText(line1: string) 258 var line2 = getline(2) 259 var line3 = getline(3) 260 var line4 = getline(4) 261 var line5 = getline(5) 262 263 # Bourne-like shell scripts: sh ksh bash bash2 264 if line1 =~ '^:$' 265 call dist#ft#SetFileTypeSH(line1) 266 267 # Z shell scripts 268 elseif line1 =~ '^#compdef\>' 269 || line1 =~ '^#autoload\>' 270 || "\n" .. line1 .. "\n" .. line2 .. "\n" .. line3 .. 271 "\n" .. line4 .. "\n" .. line5 272 =~ '\n\s*emulate\s\+\%(-[LR]\s\+\)\=[ckz]\=sh\>' 273 setl ft=zsh 274 275 # ELM Mail files 276 elseif line1 =~ '^From \([a-zA-Z][a-zA-Z_0-9\.=-]*\(@[^ ]*\)\=\|-\) .* \(19\|20\)\d\d$' 277 || line1 =~ '^\creturn-path:\s<.*@.*>$' 278 setl ft=mail 279 280 # Mason 281 elseif line1 =~ '^<[%&].*>' 282 setl ft=mason 283 284 # Vim scripts (must have '" vim' as the first line to trigger this) 285 elseif line1 =~ '^" *[vV]im$' 286 setl ft=vim 287 288 # libcxx and libstdc++ standard library headers like "iostream" do not have 289 # an extension, recognize the Emacs file mode. 290 elseif line1 =~? '-\*-.*C++.*-\*-' 291 setl ft=cpp 292 293 # MOO 294 elseif line1 =~ '^\*\* LambdaMOO Database, Format Version \%([1-3]\>\)\@!\d\+ \*\*$' 295 setl ft=moo 296 297 # Diff file: 298 # - "diff" in first line (context diff) 299 # - "Only in " in first line 300 # - "34,35c34,35" normal diff format output 301 # - "--- " in first line and "+++ " in second line (unified diff). 302 # - "*** " in first line and "--- " in second line (context diff). 303 # - "# It was generated by makepatch " in the second line (makepatch diff). 304 # - "Index: <filename>" in the first line (CVS file) 305 # - "=== ", line of "=", "---", "+++ " (SVK diff) 306 # - "=== ", "--- ", "+++ " (bzr diff, common case) 307 # - "=== (removed|added|renamed|modified)" (bzr diff, alternative) 308 # - "# HG changeset patch" in first line (Mercurial export format) 309 elseif line1 =~ '^\(diff\>\|Only in \|\d\+\(,\d\+\)\=[cda]\d\+\(,\d\+\)\=\>$\|# It was generated by makepatch \|Index:\s\+\f\+\r\=$\|===== \f\+ \d\+\.\d\+ vs edited\|==== //\f\+#\d\+\|# HG changeset patch\)' 310 || (line1 =~ '^--- ' && line2 =~ '^+++ ') 311 || (line1 =~ '^\* looking for ' && line2 =~ '^\* comparing to ') 312 || (line1 =~ '^\*\*\* ' && line2 =~ '^--- ') 313 || (line1 =~ '^=== ' && ((line2 =~ '^=\{66\}' && line3 =~ '^--- ' && line4 =~ '^+++') || (line2 =~ '^--- ' && line3 =~ '^+++ '))) 314 || (line1 =~ '^=== \(removed\|added\|renamed\|modified\)') 315 setl ft=diff 316 317 # PostScript Files (must have %!PS as the first line, like a2ps output) 318 elseif line1 =~ '^%![ \t]*PS' 319 setl ft=postscr 320 321 # M4 scripts: Guess there is a line that starts with "dnl". 322 elseif line1 =~ '^\s*dnl\>' 323 || line2 =~ '^\s*dnl\>' 324 || line3 =~ '^\s*dnl\>' 325 || line4 =~ '^\s*dnl\>' 326 || line5 =~ '^\s*dnl\>' 327 setl ft=m4 328 329 # AmigaDos scripts 330 elseif $TERM == "amiga" && (line1 =~ "^;" || line1 =~? '^\.bra') 331 setl ft=amiga 332 333 # SiCAD scripts (must have procn or procd as the first line to trigger this) 334 elseif line1 =~? '^ *proc[nd] *$' 335 setl ft=sicad 336 337 # Purify log files start with "**** Purify" 338 elseif line1 =~ '^\*\*\*\* Purify' 339 setl ft=purifylog 340 341 # XML 342 elseif line1 =~ '<?\s*xml.*?>' 343 setl ft=xml 344 345 # XHTML (e.g.: PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN") 346 elseif line1 =~ '\<DTD\s\+XHTML\s' 347 setl ft=xhtml 348 349 # HTML (e.g.: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN") 350 # Avoid "doctype html", used by slim. 351 elseif line1 =~? '<!DOCTYPE\s\+html\>' 352 setl ft=html 353 354 # PDF 355 elseif line1 =~ '^%PDF-' 356 setl ft=pdf 357 358 # XXD output 359 elseif line1 =~ '^\x\{7}: \x\{2} \=\x\{2} \=\x\{2} \=\x\{2} ' 360 setl ft=xxd 361 362 # RCS/CVS log output 363 elseif line1 =~ '^RCS file:' || line2 =~ '^RCS file:' 364 setl ft=rcslog 365 366 # CVS commit 367 elseif line2 =~ '^CVS:' || getline("$") =~ '^CVS: ' 368 setl ft=cvs 369 370 # Prescribe 371 elseif line1 =~ '^!R!' 372 setl ft=prescribe 373 374 # Send-pr 375 elseif line1 =~ '^SEND-PR:' 376 setl ft=sendpr 377 378 # SNNS files 379 elseif line1 =~ '^SNNS network definition file' 380 setl ft=snnsnet 381 elseif line1 =~ '^SNNS pattern definition file' 382 setl ft=snnspat 383 elseif line1 =~ '^SNNS result file' 384 setl ft=snnsres 385 386 # Virata 387 elseif line1 =~ '^%.\{-}[Vv]irata' 388 || line2 =~ '^%.\{-}[Vv]irata' 389 || line3 =~ '^%.\{-}[Vv]irata' 390 || line4 =~ '^%.\{-}[Vv]irata' 391 || line5 =~ '^%.\{-}[Vv]irata' 392 setl ft=virata 393 394 # Strace 395 # inaccurate fast match first, then use accurate slow match 396 elseif (line1 =~ 'execve(' && line1 =~ '^[0-9:. ]*execve(') 397 || line1 =~ '^__libc_start_main' 398 setl ft=strace 399 400 # VSE JCL 401 elseif line1 =~ '^\* $$ JOB\>' || line1 =~ '^// *JOB\>' 402 setl ft=vsejcl 403 404 # TAK and SINDA 405 elseif line4 =~ 'K & K Associates' || line2 =~ 'TAK 2000' 406 setl ft=takout 407 elseif line3 =~ 'S Y S T E M S I M P R O V E D ' 408 setl ft=sindaout 409 elseif getline(6) =~ 'Run Date: ' 410 setl ft=takcmp 411 elseif getline(9) =~ 'Node File 1' 412 setl ft=sindacmp 413 414 # DNS zone files 415 elseif line1 .. line2 .. line3 .. line4 =~ '^; <<>> DiG [0-9.]\+.* <<>>\|$ORIGIN\|$TTL\|IN\s\+SOA' 416 setl ft=bindzone 417 418 # BAAN 419 elseif line1 =~ '|\*\{1,80}' && line2 =~ 'VRC ' 420 || line2 =~ '|\*\{1,80}' && line3 =~ 'VRC ' 421 setl ft=baan 422 423 # Valgrind 424 elseif line1 =~ '^==\d\+== valgrind' || line3 =~ '^==\d\+== Using valgrind' 425 setl ft=valgrind 426 427 # Go docs 428 elseif line1 =~ '^PACKAGE DOCUMENTATION$' 429 setl ft=godoc 430 431 # Renderman Interface Bytestream 432 elseif line1 =~ '^##RenderMan' 433 setl ft=rib 434 435 # Scheme scripts 436 elseif line1 =~ 'exec\s\+\S*scheme' || line2 =~ 'exec\s\+\S*scheme' 437 setl ft=scheme 438 439 # Git output 440 elseif line1 =~ '^\(commit\|tree\|object\) \x\{40,\}\>\|^tag \S\+$' 441 setl ft=git 442 443 # Gprof (gnu profiler) 444 elseif line1 == 'Flat profile:' 445 && line2 == '' 446 && line3 =~ '^Each sample counts as .* seconds.$' 447 setl ft=gprof 448 449 # Erlang terms 450 # (See also: http://www.gnu.org/software/emacs/manual/html_node/emacs/Choosing-Modes.html#Choosing-Modes) 451 elseif line1 =~? '-\*-.*erlang.*-\*-' 452 setl ft=erlang 453 454 # YAML 455 elseif line1 =~ '^%YAML' 456 setl ft=yaml 457 458 # MikroTik RouterOS script 459 elseif line1 =~ '^#.*by RouterOS.*$' 460 setl ft=routeros 461 462 # Sed scripts 463 # #ncomment is allowed but most likely a false positive so require a space 464 # before any trailing comment text 465 elseif line1 =~ '^#n\%($\|\s\)' 466 setl ft=sed 467 468 elseif line1 =~ '^#\s\+Reconstructed via infocmp from file:' 469 setl ft=terminfo 470 471 elseif line1 =~ '^File: .*\.info, Node: .*, \%(Next\|Prev\): .*, Up: \|This is the top of the INFO tree.' 472 setl ft=info 473 474 else 475 var lnum = 1 476 while getline(lnum) =~ "^? " && lnum < line("$") 477 lnum += 1 478 endwhile 479 if getline(lnum) =~ '^Index:\s\+\f\+$' 480 # CVS diff 481 setl ft=diff 482 483 # locale input files: Formal Definitions of Cultural Conventions 484 # filename must be like en_US, fr_FR@euro or en_US.UTF-8 485 elseif expand("%") =~ '\a\a_\a\a\($\|[.@]\)\|i18n$\|POSIX$\|translit_' 486 lnum = 1 487 while lnum < 100 && lnum < line("$") 488 if getline(lnum) =~ '^LC_\(IDENTIFICATION\|CTYPE\|COLLATE\|MONETARY\|NUMERIC\|TIME\|MESSAGES\|PAPER\|TELEPHONE\|MEASUREMENT\|NAME\|ADDRESS\)$' 489 setf fdcc 490 break 491 endif 492 lnum += 1 493 endwhile 494 endif 495 endif 496 enddef