vim9script
# Vim functions for file type detection
#
# Maintainer: The Vim Project <https://github.com/vim/vim>
# Last Change: 2023 Aug 10
# Former Maintainer: Bram Moolenaar <Bram@vim.org>
# These functions are moved here from runtime/filetype.vim to make startup
# faster.
export def Check_inp()
if getline(1) =~ '^\*'
setf abaqus
else
var n = 1
var nmax = line("$") > 500 ? 500 : line("$")
while n <= nmax
if getline(n) =~? "^header surface data"
setf trasys
break
endif
n += 1
endwhile
endif
enddef
# This function checks for the kind of assembly that is wanted by the user, or
# can be detected from the first five lines of the file.
export def FTasm()
# make sure b:asmsyntax exists
if !exists("b:asmsyntax")
b:asmsyntax = ""
endif
if b:asmsyntax == ""
FTasmsyntax()
endif
# if b:asmsyntax still isn't set, default to asmsyntax or GNU
if b:asmsyntax == ""
if exists("g:asmsyntax")
b:asmsyntax = g:asmsyntax
else
b:asmsyntax = "asm"
endif
endif
exe "setf " .. fnameescape(b:asmsyntax)
enddef
export def FTasmsyntax()
# see if the file contains any asmsyntax=foo overrides. If so, change
# b:asmsyntax appropriately
var head = " " .. getline(1) .. " " .. getline(2) .. " "
.. getline(3) .. " " .. getline(4) .. " " .. getline(5) .. " "
var match = matchstr(head, '\sasmsyntax=\zs[a-zA-Z0-9]\+\ze\s')
if match != ''
b:asmsyntax = match
elseif ((head =~? '\.title') || (head =~? '\.ident') || (head =~? '\.macro') || (head =~? '\.subtitle') || (head =~? '\.library'))
b:asmsyntax = "vmasm"
endif
enddef
var ft_visual_basic_content = '\c^\s*\%(Attribute\s\+VB_Name\|Begin\s\+\%(VB\.\|{\%(\x\+-\)\+\x\+}\)\)'
# See FTfrm() for Visual Basic form file detection
export def FTbas()
if exists("g:filetype_bas")
exe "setf " .. g:filetype_bas
return
endif
# most frequent FreeBASIC-specific keywords in distro files
var fb_keywords = '\c^\s*\%(extern\|var\|enum\|private\|scope\|union\|byref\|operator\|constructor\|delete\|namespace\|public\|property\|with\|destructor\|using\)\>\%(\s*[:=(]\)\@!'
var fb_preproc = '\c^\s*\%(' ..
# preprocessor
'#\s*\a\+\|' ..
# compiler option
'option\s\+\%(byval\|dynamic\|escape\|\%(no\)\=gosub\|nokeyword\|private\|static\)\>\|' ..
# metacommand
'\%(''\|rem\)\s*\$lang\>\|' ..
# default datatype
'def\%(byte\|longint\|short\|ubyte\|uint\|ulongint\|ushort\)\>' ..
'\)'
var fb_comment = "^\\s*/'"
# OPTION EXPLICIT, without the leading underscore, is common to many dialects
var qb64_preproc = '\c^\s*\%($\a\+\|option\s\+\%(_explicit\|_\=explicitarray\)\>\)'
for lnum in range(1, min([line("$"), 100]))
var line = getline(lnum)
if line =~ ft_visual_basic_content
setf vb
return
elseif line =~ fb_preproc || line =~ fb_comment || line =~ fb_keywords
setf freebasic
return
elseif line =~ qb64_preproc
setf qb64
return
endif
endfor
setf basic
enddef
export def FTbtm()
if exists("g:dosbatch_syntax_for_btm") && g:dosbatch_syntax_for_btm
setf dosbatch
else
setf btm
endif
enddef
export def BindzoneCheck(default = '')
if getline(1) .. getline(2) .. getline(3) .. getline(4)
=~ '^; <<>> DiG [0-9.]\+.* <<>>\|$ORIGIN\|$TTL\|IN\s\+SOA'
setf bindzone
elseif default != ''
exe 'setf ' .. default
endif
enddef
# Returns true if file content looks like RAPID
def IsRapid(sChkExt: string = ""): bool
if sChkExt == "cfg"
return getline(1) =~? '\v^%(EIO|MMC|MOC|PROC|SIO|SYS):CFG'
endif
# called from FTmod, FTprg or FTsys
return getline(nextnonblank(1)) =~? '\v^\s*%(\%{3}|module\s+\k+\s*%(\(|$))'
enddef
export def FTcfg()
if exists("g:filetype_cfg")
exe "setf " .. g:filetype_cfg
elseif IsRapid("cfg")
setf rapid
else
setf cfg
endif
enddef
export def FTcls()
if exists("g:filetype_cls")
exe "setf " .. g:filetype_cls
return
endif
var line1 = getline(1)
if line1 =~ '^\v%(\%|\\)'
setf tex
elseif line1[0] == '#' && line1 =~ 'rexx'
setf rexx
elseif line1 == 'VERSION 1.0 CLASS'
setf vb
else
setf st
endif
enddef
export def FTlpc()
if exists("g:lpc_syntax_for_c")
var lnum = 1
while lnum <= 12
if getline(lnum) =~# '^\(//\|inherit\|private\|protected\|nosave\|string\|object\|mapping\|mixed\)'
setf lpc
return
endif
lnum += 1
endwhile
endif
setf c
enddef
export def FTheader()
if match(getline(1, min([line("$"), 200])), '^@\(interface\|end\|class\)'