blob: 5014e7544212782e8d8587b064226baba4a6152d [file] [log] [blame]
Bram Moolenaara2baa732022-02-04 16:09:54 +00001vim9script
Bram Moolenaar851ee6c2017-11-09 20:46:17 +01002
Bram Moolenaara2baa732022-02-04 16:09:54 +00003# Vim functions for file type detection
4#
Doug Kearns68a89472024-01-05 17:59:04 +01005# Maintainer: The Vim Project <https://github.com/vim/vim>
Eisuke Kawashima48295112025-06-17 20:30:52 +02006# Last Change: 2025 Jun 17
Christian Brabandte978b452023-08-13 10:33:05 +02007# Former Maintainer: Bram Moolenaar <Bram@vim.org>
Bram Moolenaar851ee6c2017-11-09 20:46:17 +01008
Bram Moolenaara2baa732022-02-04 16:09:54 +00009# These functions are moved here from runtime/filetype.vim to make startup
10# faster.
Bram Moolenaar851ee6c2017-11-09 20:46:17 +010011
igna_martinoli37853b72024-07-18 21:34:36 +020012var prolog_pattern = '^\s*\(:-\|%\+\(\s\|$\)\|\/\*\)\|\.\s*$'
13
Bram Moolenaara2baa732022-02-04 16:09:54 +000014export def Check_inp()
Wu, Zhenyu61ee8332024-04-09 22:09:30 +020015 if getline(1) =~ '%%'
16 setf tex
17 elseif getline(1) =~ '^\*'
Bram Moolenaar851ee6c2017-11-09 20:46:17 +010018 setf abaqus
19 else
Bram Moolenaara2baa732022-02-04 16:09:54 +000020 var n = 1
21 var nmax = line("$") > 500 ? 500 : line("$")
Bram Moolenaar851ee6c2017-11-09 20:46:17 +010022 while n <= nmax
23 if getline(n) =~? "^header surface data"
24 setf trasys
25 break
26 endif
Bram Moolenaara2baa732022-02-04 16:09:54 +000027 n += 1
Bram Moolenaar851ee6c2017-11-09 20:46:17 +010028 endwhile
29 endif
Bram Moolenaara2baa732022-02-04 16:09:54 +000030enddef
Bram Moolenaar851ee6c2017-11-09 20:46:17 +010031
Bram Moolenaara2baa732022-02-04 16:09:54 +000032# This function checks for the kind of assembly that is wanted by the user, or
33# can be detected from the first five lines of the file.
34export def FTasm()
Wu, Zhenyu4f73c072025-01-08 20:20:06 +010035 # tiasm uses `* commment`
36 if join(getline(1, 10), "\n") =~ '\%(\%(^\|\n\)\*\|Texas Instruments Incorporated\)'
37 setf tiasm
38 endif
Bram Moolenaara2baa732022-02-04 16:09:54 +000039 # make sure b:asmsyntax exists
Bram Moolenaar851ee6c2017-11-09 20:46:17 +010040 if !exists("b:asmsyntax")
Bram Moolenaara2baa732022-02-04 16:09:54 +000041 b:asmsyntax = ""
Bram Moolenaar851ee6c2017-11-09 20:46:17 +010042 endif
43
44 if b:asmsyntax == ""
Bram Moolenaara2baa732022-02-04 16:09:54 +000045 FTasmsyntax()
Bram Moolenaar851ee6c2017-11-09 20:46:17 +010046 endif
47
Bram Moolenaara2baa732022-02-04 16:09:54 +000048 # if b:asmsyntax still isn't set, default to asmsyntax or GNU
Bram Moolenaar851ee6c2017-11-09 20:46:17 +010049 if b:asmsyntax == ""
50 if exists("g:asmsyntax")
Bram Moolenaara2baa732022-02-04 16:09:54 +000051 b:asmsyntax = g:asmsyntax
Bram Moolenaar851ee6c2017-11-09 20:46:17 +010052 else
Bram Moolenaara2baa732022-02-04 16:09:54 +000053 b:asmsyntax = "asm"
Bram Moolenaar851ee6c2017-11-09 20:46:17 +010054 endif
55 endif
56
Bram Moolenaara2baa732022-02-04 16:09:54 +000057 exe "setf " .. fnameescape(b:asmsyntax)
58enddef
Bram Moolenaar851ee6c2017-11-09 20:46:17 +010059
Bram Moolenaara2baa732022-02-04 16:09:54 +000060export def FTasmsyntax()
61 # see if the file contains any asmsyntax=foo overrides. If so, change
62 # b:asmsyntax appropriately
63 var head = " " .. getline(1) .. " " .. getline(2) .. " "
64 .. getline(3) .. " " .. getline(4) .. " " .. getline(5) .. " "
65 var match = matchstr(head, '\sasmsyntax=\zs[a-zA-Z0-9]\+\ze\s')
Bram Moolenaar851ee6c2017-11-09 20:46:17 +010066 if match != ''
Bram Moolenaara2baa732022-02-04 16:09:54 +000067 b:asmsyntax = match
Bram Moolenaar851ee6c2017-11-09 20:46:17 +010068 elseif ((head =~? '\.title') || (head =~? '\.ident') || (head =~? '\.macro') || (head =~? '\.subtitle') || (head =~? '\.library'))
Bram Moolenaara2baa732022-02-04 16:09:54 +000069 b:asmsyntax = "vmasm"
Bram Moolenaar851ee6c2017-11-09 20:46:17 +010070 endif
Bram Moolenaara2baa732022-02-04 16:09:54 +000071enddef
Bram Moolenaar851ee6c2017-11-09 20:46:17 +010072
Doug Kearnsf97f6bb2023-08-27 18:44:09 +020073var ft_visual_basic_content = '\c^\s*\%(Attribute\s\+VB_Name\|Begin\s\+\%(VB\.\|{\%(\x\+-\)\+\x\+}\)\)'
Doug Kearnsc570e9c2022-01-31 17:09:14 +000074
Bram Moolenaara2baa732022-02-04 16:09:54 +000075# See FTfrm() for Visual Basic form file detection
76export def FTbas()
Bram Moolenaar6517f142022-01-21 14:55:13 +000077 if exists("g:filetype_bas")
Bram Moolenaara2baa732022-02-04 16:09:54 +000078 exe "setf " .. g:filetype_bas
Bram Moolenaar6517f142022-01-21 14:55:13 +000079 return
80 endif
81
Bram Moolenaara2baa732022-02-04 16:09:54 +000082 # most frequent FreeBASIC-specific keywords in distro files
83 var fb_keywords = '\c^\s*\%(extern\|var\|enum\|private\|scope\|union\|byref\|operator\|constructor\|delete\|namespace\|public\|property\|with\|destructor\|using\)\>\%(\s*[:=(]\)\@!'
Bram Moolenaar8b5901e2022-06-29 14:39:12 +010084 var fb_preproc = '\c^\s*\%(' ..
85 # preprocessor
86 '#\s*\a\+\|' ..
87 # compiler option
88 'option\s\+\%(byval\|dynamic\|escape\|\%(no\)\=gosub\|nokeyword\|private\|static\)\>\|' ..
89 # metacommand
90 '\%(''\|rem\)\s*\$lang\>\|' ..
91 # default datatype
92 'def\%(byte\|longint\|short\|ubyte\|uint\|ulongint\|ushort\)\>' ..
93 '\)'
Bram Moolenaara2baa732022-02-04 16:09:54 +000094 var fb_comment = "^\\s*/'"
Bram Moolenaar8b5901e2022-06-29 14:39:12 +010095
Bram Moolenaara2baa732022-02-04 16:09:54 +000096 # OPTION EXPLICIT, without the leading underscore, is common to many dialects
97 var qb64_preproc = '\c^\s*\%($\a\+\|option\s\+\%(_explicit\|_\=explicitarray\)\>\)'
Bram Moolenaar6517f142022-01-21 14:55:13 +000098
Bram Moolenaar8b5901e2022-06-29 14:39:12 +010099 for lnum in range(1, min([line("$"), 100]))
100 var line = getline(lnum)
101 if line =~ ft_visual_basic_content
102 setf vb
103 return
104 elseif line =~ fb_preproc || line =~ fb_comment || line =~ fb_keywords
105 setf freebasic
106 return
107 elseif line =~ qb64_preproc
108 setf qb64
109 return
110 endif
111 endfor
112 setf basic
Bram Moolenaara2baa732022-02-04 16:09:54 +0000113enddef
Bram Moolenaar851ee6c2017-11-09 20:46:17 +0100114
Bram Moolenaara2baa732022-02-04 16:09:54 +0000115export def FTbtm()
Bram Moolenaar851ee6c2017-11-09 20:46:17 +0100116 if exists("g:dosbatch_syntax_for_btm") && g:dosbatch_syntax_for_btm
117 setf dosbatch
118 else
119 setf btm
120 endif
Bram Moolenaara2baa732022-02-04 16:09:54 +0000121enddef
Bram Moolenaar851ee6c2017-11-09 20:46:17 +0100122
Bram Moolenaara2baa732022-02-04 16:09:54 +0000123export def BindzoneCheck(default = '')
124 if getline(1) .. getline(2) .. getline(3) .. getline(4)
125 =~ '^; <<>> DiG [0-9.]\+.* <<>>\|$ORIGIN\|$TTL\|IN\s\+SOA'
Bram Moolenaar851ee6c2017-11-09 20:46:17 +0100126 setf bindzone
Bram Moolenaara2baa732022-02-04 16:09:54 +0000127 elseif default != ''
128 exe 'setf ' .. default
Bram Moolenaar851ee6c2017-11-09 20:46:17 +0100129 endif
Bram Moolenaara2baa732022-02-04 16:09:54 +0000130enddef
Bram Moolenaar851ee6c2017-11-09 20:46:17 +0100131
Bram Moolenaar0bbf09c2022-04-09 15:16:53 +0100132# Returns true if file content looks like RAPID
133def IsRapid(sChkExt: string = ""): bool
134 if sChkExt == "cfg"
135 return getline(1) =~? '\v^%(EIO|MMC|MOC|PROC|SIO|SYS):CFG'
136 endif
137 # called from FTmod, FTprg or FTsys
138 return getline(nextnonblank(1)) =~? '\v^\s*%(\%{3}|module\s+\k+\s*%(\(|$))'
139enddef
140
141export def FTcfg()
142 if exists("g:filetype_cfg")
143 exe "setf " .. g:filetype_cfg
144 elseif IsRapid("cfg")
145 setf rapid
146 else
147 setf cfg
148 endif
149enddef
150
Wu, Zhenyue2c27ca2024-11-19 20:55:25 +0100151export def FTcl()
152 if join(getline(1, 4), '') =~ '/\*'
153 setf opencl
154 else
155 setf lisp
156 endif
157enddef
158
Bram Moolenaar8b5901e2022-06-29 14:39:12 +0100159export def FTcls()
160 if exists("g:filetype_cls")
161 exe "setf " .. g:filetype_cls
162 return
163 endif
164
Doug Kearnsf97f6bb2023-08-27 18:44:09 +0200165 var line1 = getline(1)
Doug Kearnse06afb72023-08-29 22:21:35 +0200166 if line1 =~ '^#!.*\<\%(rexx\|regina\)\>'
Bram Moolenaar8b5901e2022-06-29 14:39:12 +0100167 setf rexx
Doug Kearnse06afb72023-08-29 22:21:35 +0200168 return
Doug Kearnsf97f6bb2023-08-27 18:44:09 +0200169 elseif line1 == 'VERSION 1.0 CLASS'
Bram Moolenaar8b5901e2022-06-29 14:39:12 +0100170 setf vb
Doug Kearnse06afb72023-08-29 22:21:35 +0200171 return
172 endif
173
174 var nonblank1 = getline(nextnonblank(1))
175 if nonblank1 =~ '^\v%(\%|\\)'
176 setf tex
177 elseif nonblank1 =~ '^\s*\%(/\*\|::\w\)'
178 setf rexx
Bram Moolenaar8b5901e2022-06-29 14:39:12 +0100179 else
180 setf st
181 endif
182enddef
183
Wu, Zhenyubc32bbd2024-11-14 22:55:36 +0100184export def FTll()
185 if getline(1) =~ ';\|\<source_filename\>\|\<target\>'
186 setf llvm
Eisuke Kawashima48295112025-06-17 20:30:52 +0200187 return
Wu, Zhenyubc32bbd2024-11-14 22:55:36 +0100188 endif
Eisuke Kawashima48295112025-06-17 20:30:52 +0200189 var n = 1
190 while n < 100 && n <= line("$")
191 var line = getline(n)
192 if line =~ '^\s*%'
193 setf lex
194 return
195 endif
196 n += 1
197 endwhile
198 setf lifelines
Wu, Zhenyubc32bbd2024-11-14 22:55:36 +0100199enddef
200
Bram Moolenaara2baa732022-02-04 16:09:54 +0000201export def FTlpc()
Bram Moolenaar851ee6c2017-11-09 20:46:17 +0100202 if exists("g:lpc_syntax_for_c")
Bram Moolenaara2baa732022-02-04 16:09:54 +0000203 var lnum = 1
Bram Moolenaar851ee6c2017-11-09 20:46:17 +0100204 while lnum <= 12
205 if getline(lnum) =~# '^\(//\|inherit\|private\|protected\|nosave\|string\|object\|mapping\|mixed\)'
206 setf lpc
207 return
208 endif
Bram Moolenaara2baa732022-02-04 16:09:54 +0000209 lnum += 1
Bram Moolenaar851ee6c2017-11-09 20:46:17 +0100210 endwhile
211 endif
212 setf c
Bram Moolenaara2baa732022-02-04 16:09:54 +0000213enddef
Bram Moolenaar851ee6c2017-11-09 20:46:17 +0100214
Amelia Clarke3041cf62025-04-19 11:48:10 +0200215# Searches within the first `maxlines` lines of the file for distinctive
216# Objective-C or C++ syntax and returns the appropriate filetype. Returns a
217# null_string if the search was inconclusive.
218def CheckObjCOrCpp(maxlines = 100): string
219 var n = 1
220 while n < maxlines && n <= line('$')
221 const line = getline(n)
222 if line =~ '\v^\s*\@%(class|interface|end)>'
223 return 'objcpp'
224 elseif line =~ '\v^\s*%(class|namespace|template|using)>'
225 return 'cpp'
Bram Moolenaar851ee6c2017-11-09 20:46:17 +0100226 endif
Amelia Clarke3041cf62025-04-19 11:48:10 +0200227 ++n
228 endwhile
229 return null_string
230enddef
231
232# Determines whether a *.h file is C, C++, Ch, or Objective-C/Objective-C++.
233export def FTheader()
234 if exists('g:filetype_h')
235 execute $'setf {g:filetype_h}'
236 elseif exists('g:c_syntax_for_h')
Bram Moolenaar851ee6c2017-11-09 20:46:17 +0100237 setf c
Amelia Clarke3041cf62025-04-19 11:48:10 +0200238 elseif exists('g:ch_syntax_for_h')
Bram Moolenaar851ee6c2017-11-09 20:46:17 +0100239 setf ch
240 else
Amelia Clarke3041cf62025-04-19 11:48:10 +0200241 # Search the first 100 lines of the file for distinctive Objective-C or C++
242 # syntax and set the filetype accordingly. Otherwise, use C as the default
243 # filetype.
244 execute $'setf {CheckObjCOrCpp() ?? 'c'}'
Bram Moolenaar851ee6c2017-11-09 20:46:17 +0100245 endif
Bram Moolenaara2baa732022-02-04 16:09:54 +0000246enddef
Bram Moolenaar851ee6c2017-11-09 20:46:17 +0100247
Bram Moolenaara2baa732022-02-04 16:09:54 +0000248# This function checks if one of the first ten lines start with a '@'. In
249# that case it is probably a change file.
250# If the first line starts with # or ! it's probably a ch file.
251# If a line has "main", "include", "//" or "/*" it's probably ch.
252# Otherwise CHILL is assumed.
253export def FTchange()
254 var lnum = 1
Bram Moolenaar851ee6c2017-11-09 20:46:17 +0100255 while lnum <= 10
256 if getline(lnum)[0] == '@'
257 setf change
258 return
259 endif
260 if lnum == 1 && (getline(1)[0] == '#' || getline(1)[0] == '!')
261 setf ch
262 return
263 endif
264 if getline(lnum) =~ "MODULE"
265 setf chill
266 return
267 endif
268 if getline(lnum) =~ 'main\s*(\|#\s*include\|//'
269 setf ch
270 return
271 endif
Bram Moolenaara2baa732022-02-04 16:09:54 +0000272 lnum += 1
Bram Moolenaar851ee6c2017-11-09 20:46:17 +0100273 endwhile
274 setf chill
Bram Moolenaara2baa732022-02-04 16:09:54 +0000275enddef
Bram Moolenaar851ee6c2017-11-09 20:46:17 +0100276
Bram Moolenaara2baa732022-02-04 16:09:54 +0000277export def FTent()
278 # This function checks for valid cl syntax in the first five lines.
Bram Moolenaar0bbf09c2022-04-09 15:16:53 +0100279 # Look for either an opening comment, '#', or a block start, '{'.
Bram Moolenaara2baa732022-02-04 16:09:54 +0000280 # If not found, assume SGML.
281 var lnum = 1
Bram Moolenaar851ee6c2017-11-09 20:46:17 +0100282 while lnum < 6
Bram Moolenaara2baa732022-02-04 16:09:54 +0000283 var line = getline(lnum)
Bram Moolenaar851ee6c2017-11-09 20:46:17 +0100284 if line =~ '^\s*[#{]'
285 setf cl
286 return
287 elseif line !~ '^\s*$'
Bram Moolenaara2baa732022-02-04 16:09:54 +0000288 # Not a blank line, not a comment, and not a block start,
289 # so doesn't look like valid cl code.
Bram Moolenaar851ee6c2017-11-09 20:46:17 +0100290 break
291 endif
Bram Moolenaara2baa732022-02-04 16:09:54 +0000292 lnum += 1
Bram Moolenaarc12dc472022-03-05 13:45:56 +0000293 endwhile
Bram Moolenaar851ee6c2017-11-09 20:46:17 +0100294 setf dtd
Bram Moolenaara2baa732022-02-04 16:09:54 +0000295enddef
Bram Moolenaar851ee6c2017-11-09 20:46:17 +0100296
Bram Moolenaara2baa732022-02-04 16:09:54 +0000297export def ExCheck()
298 var lines = getline(1, min([line("$"), 100]))
Austin Gatlinf3caeb62021-06-26 12:02:55 +0200299 if exists('g:filetype_euphoria')
Bram Moolenaara2baa732022-02-04 16:09:54 +0000300 exe 'setf ' .. g:filetype_euphoria
Austin Gatlinf3caeb62021-06-26 12:02:55 +0200301 elseif match(lines, '^--\|^ifdef\>\|^include\>') > -1
302 setf euphoria3
303 else
304 setf elixir
305 endif
Bram Moolenaara2baa732022-02-04 16:09:54 +0000306enddef
Austin Gatlinf3caeb62021-06-26 12:02:55 +0200307
Bram Moolenaara2baa732022-02-04 16:09:54 +0000308export def EuphoriaCheck()
Bram Moolenaar851ee6c2017-11-09 20:46:17 +0100309 if exists('g:filetype_euphoria')
Bram Moolenaara2baa732022-02-04 16:09:54 +0000310 exe 'setf ' .. g:filetype_euphoria
Bram Moolenaar851ee6c2017-11-09 20:46:17 +0100311 else
312 setf euphoria3
313 endif
Bram Moolenaara2baa732022-02-04 16:09:54 +0000314enddef
Bram Moolenaar851ee6c2017-11-09 20:46:17 +0100315
Bram Moolenaara2baa732022-02-04 16:09:54 +0000316export def DtraceCheck()
=?UTF-8?q?Teubel=20Gy=C3=B6rgy?=4d56b972022-02-24 17:59:09 +0000317 if did_filetype()
318 # Filetype was already detected
319 return
320 endif
Bram Moolenaara2baa732022-02-04 16:09:54 +0000321 var lines = getline(1, min([line("$"), 100]))
Bram Moolenaar851ee6c2017-11-09 20:46:17 +0100322 if match(lines, '^module\>\|^import\>') > -1
Bram Moolenaara2baa732022-02-04 16:09:54 +0000323 # D files often start with a module and/or import statement.
Bram Moolenaar851ee6c2017-11-09 20:46:17 +0100324 setf d
325 elseif match(lines, '^#!\S\+dtrace\|#pragma\s\+D\s\+option\|:\S\{-}:\S\{-}:') > -1
326 setf dtrace
327 else
328 setf d
329 endif
Bram Moolenaara2baa732022-02-04 16:09:54 +0000330enddef
Bram Moolenaar851ee6c2017-11-09 20:46:17 +0100331
Doug Kearns68a89472024-01-05 17:59:04 +0100332export def FTdef()
Wu, Zhenyu61ee8332024-04-09 22:09:30 +0200333 # LaTeX def files are usually generated by docstrip, which will output '%%' in first line
334 if getline(1) =~ '%%'
335 setf tex
336 endif
Doug Kearns68a89472024-01-05 17:59:04 +0100337 if get(g:, "filetype_def", "") == "modula2" || IsModula2()
338 SetFiletypeModula2()
339 return
340 endif
341
342 if exists("g:filetype_def")
343 exe "setf " .. g:filetype_def
344 else
345 setf def
346 endif
347enddef
348
Bram Moolenaara2baa732022-02-04 16:09:54 +0000349export def FTe()
Bram Moolenaar851ee6c2017-11-09 20:46:17 +0100350 if exists('g:filetype_euphoria')
Bram Moolenaara2baa732022-02-04 16:09:54 +0000351 exe 'setf ' .. g:filetype_euphoria
Bram Moolenaar851ee6c2017-11-09 20:46:17 +0100352 else
Bram Moolenaara2baa732022-02-04 16:09:54 +0000353 var n = 1
Bram Moolenaar493fbe42019-03-17 17:16:12 +0100354 while n < 100 && n <= line("$")
Bram Moolenaar851ee6c2017-11-09 20:46:17 +0100355 if getline(n) =~ "^\\s*\\(<'\\|'>\\)\\s*$"
356 setf specman
357 return
358 endif
Bram Moolenaara2baa732022-02-04 16:09:54 +0000359 n += 1
Bram Moolenaar851ee6c2017-11-09 20:46:17 +0100360 endwhile
361 setf eiffel
362 endif
Bram Moolenaara2baa732022-02-04 16:09:54 +0000363enddef
Bram Moolenaar851ee6c2017-11-09 20:46:17 +0100364
Doug Kearns19a3bc32023-08-20 20:51:12 +0200365def IsForth(): bool
366 var first_line = nextnonblank(1)
367
368 # SwiftForth block comment (line is usually filled with '-' or '=') or
369 # OPTIONAL (sometimes precedes the header comment)
370 if getline(first_line) =~? '^\%({\%(\s\|$\)\|OPTIONAL\s\)'
371 return true
372 endif
373
374 var n = first_line
375 while n < 100 && n <= line("$")
376 # Forth comments and colon definitions
377 if getline(n) =~ '^[:(\\] '
378 return true
379 endif
380 n += 1
381 endwhile
382 return false
383enddef
384
385# Distinguish between Forth and Fortran
386export def FTf()
387 if exists("g:filetype_f")
388 exe "setf " .. g:filetype_f
389 elseif IsForth()
390 setf forth
391 else
392 setf fortran
393 endif
394enddef
395
Bram Moolenaara2baa732022-02-04 16:09:54 +0000396export def FTfrm()
Doug Kearnsc570e9c2022-01-31 17:09:14 +0000397 if exists("g:filetype_frm")
Bram Moolenaara2baa732022-02-04 16:09:54 +0000398 exe "setf " .. g:filetype_frm
Doug Kearnsc570e9c2022-01-31 17:09:14 +0000399 return
400 endif
401
Doug Kearnsf97f6bb2023-08-27 18:44:09 +0200402 if getline(1) == "VERSION 5.00"
403 setf vb
404 return
405 endif
406
Bram Moolenaara2baa732022-02-04 16:09:54 +0000407 var lines = getline(1, min([line("$"), 5]))
Doug Kearnsc570e9c2022-01-31 17:09:14 +0000408
Bram Moolenaarb2c72352022-02-22 21:17:40 +0000409 if match(lines, ft_visual_basic_content) > -1
Doug Kearnsc570e9c2022-01-31 17:09:14 +0000410 setf vb
411 else
412 setf form
413 endif
Bram Moolenaara2baa732022-02-04 16:09:54 +0000414enddef
Doug Kearnsc570e9c2022-01-31 17:09:14 +0000415
Doug Kearns19a3bc32023-08-20 20:51:12 +0200416# Distinguish between Forth and F#
Bram Moolenaara2baa732022-02-04 16:09:54 +0000417export def FTfs()
Bram Moolenaar3d14c0f2021-11-27 17:22:07 +0000418 if exists("g:filetype_fs")
Bram Moolenaara2baa732022-02-04 16:09:54 +0000419 exe "setf " .. g:filetype_fs
Doug Kearns19a3bc32023-08-20 20:51:12 +0200420 elseif IsForth()
421 setf forth
Bram Moolenaar3d14c0f2021-11-27 17:22:07 +0000422 else
Johan Kotlinski065088d2023-04-02 20:29:38 +0100423 setf fsharp
Bram Moolenaar3d14c0f2021-11-27 17:22:07 +0000424 endif
Bram Moolenaara2baa732022-02-04 16:09:54 +0000425enddef
Bram Moolenaar3d14c0f2021-11-27 17:22:07 +0000426
Amelia Clarke35dfe582024-05-24 08:05:00 +0200427# Recursively search for Hare source files in a directory and any
428# subdirectories, up to a given depth.
429def IsHareModule(dir: string, depth: number): bool
430 if depth <= 0
431 return !empty(glob(dir .. '/*.ha'))
432 endif
433
434 return reduce(sort(glob(dir .. '/*', true, true),
435 (a, b) => isdirectory(a) - isdirectory(b)),
436 (acc, n) => acc
437 || n =~ '\.ha$'
438 || isdirectory(n)
439 && IsHareModule(n, depth - 1),
440 false)
441enddef
442
443# Determine if a README file exists within a Hare module and should be given the
444# Haredoc filetype.
445export def FTharedoc()
446 if exists('g:filetype_haredoc')
447 if IsHareModule('<afile>:h', get(g:, 'haredoc_search_depth', 1))
448 setf haredoc
449 endif
450 endif
451enddef
452
Dennis van den Berg1ad194c2024-07-09 19:25:33 +0200453# Distinguish between HTML, XHTML, Django and Angular
Bram Moolenaara2baa732022-02-04 16:09:54 +0000454export def FThtml()
455 var n = 1
Dennis van den Berg1ad194c2024-07-09 19:25:33 +0200456
457 # Test if the filename follows the Angular component template convention
Christian Brabandtc03f6312024-07-10 19:23:39 +0200458 # Disabled for the reasons mentioned here: #13594
459 # if expand('%:t') =~ '^.*\.component\.html$'
460 # setf htmlangular
461 # return
462 # endif
Dennis van den Berg1ad194c2024-07-09 19:25:33 +0200463
Afiq Nazrie9718ed72024-06-18 19:59:53 +0200464 while n < 40 && n <= line("$")
Dennis van den Berg1ad194c2024-07-09 19:25:33 +0200465 # Check for Angular
Christian Brabandt668e9f22025-01-11 09:19:12 +0100466 if getline(n) =~ '@\(if\|for\|defer\|switch\)\|\*\(ngIf\|ngFor\|ngSwitch\|ngTemplateOutlet\)\|ng-template\|ng-content'
Dennis van den Berg1ad194c2024-07-09 19:25:33 +0200467 setf htmlangular
468 return
469 endif
Dennis van den Berg1ad194c2024-07-09 19:25:33 +0200470 # Check for XHTML
Bram Moolenaar851ee6c2017-11-09 20:46:17 +0100471 if getline(n) =~ '\<DTD\s\+XHTML\s'
472 setf xhtml
473 return
474 endif
Dennis van den Berg1ad194c2024-07-09 19:25:33 +0200475 # Check for Django
Afiq Nazrie9718ed72024-06-18 19:59:53 +0200476 if getline(n) =~ '{%\s*\(autoescape\|block\|comment\|csrf_token\|cycle\|debug\|extends\|filter\|firstof\|for\|if\|ifchanged\|include\|load\|lorem\|now\|query_string\|regroup\|resetcycle\|spaceless\|templatetag\|url\|verbatim\|widthratio\|with\)\>\|{#\s\+'
Bram Moolenaar851ee6c2017-11-09 20:46:17 +0100477 setf htmldjango
478 return
479 endif
EliSaudere57c9a12024-07-28 21:28:11 +0200480 # Check for SuperHTML
481 if getline(n) =~ '<extend\|<super>'
482 setf superhtml
483 return
484 endif
Bram Moolenaara2baa732022-02-04 16:09:54 +0000485 n += 1
Bram Moolenaar851ee6c2017-11-09 20:46:17 +0100486 endwhile
Bram Moolenaar493fbe42019-03-17 17:16:12 +0100487 setf FALLBACK html
Bram Moolenaara2baa732022-02-04 16:09:54 +0000488enddef
Bram Moolenaar851ee6c2017-11-09 20:46:17 +0100489
Bram Moolenaara2baa732022-02-04 16:09:54 +0000490# Distinguish between standard IDL and MS-IDL
491export def FTidl()
492 var n = 1
Bram Moolenaar493fbe42019-03-17 17:16:12 +0100493 while n < 50 && n <= line("$")
Bram Moolenaar851ee6c2017-11-09 20:46:17 +0100494 if getline(n) =~ '^\s*import\s\+"\(unknwn\|objidl\)\.idl"'
495 setf msidl
496 return
497 endif
Bram Moolenaara2baa732022-02-04 16:09:54 +0000498 n += 1
Bram Moolenaar851ee6c2017-11-09 20:46:17 +0100499 endwhile
500 setf idl
Bram Moolenaara2baa732022-02-04 16:09:54 +0000501enddef
Bram Moolenaar851ee6c2017-11-09 20:46:17 +0100502
Wu, Zhenyu887a38c2024-05-09 20:35:13 +0200503# Distinguish between "default", Prolog, zsh module's C and Cproto prototype file.
Bram Moolenaara2baa732022-02-04 16:09:54 +0000504export def ProtoCheck(default: string)
Wu, Zhenyu887a38c2024-05-09 20:35:13 +0200505 # zsh modules use '#include "*.pro"'
506 # https://github.com/zsh-users/zsh/blob/63f086d167960a27ecdbcb762179e2c2bf8a29f5/Src/Modules/example.c#L31
507 if getline(1) =~ '/* Generated automatically */'
508 setf c
Bram Moolenaara2baa732022-02-04 16:09:54 +0000509 # Cproto files have a comment in the first line and a function prototype in
510 # the second line, it always ends in ";". Indent files may also have
511 # comments, thus we can't match comments to see the difference.
512 # IDL files can have a single ';' in the second line, require at least one
513 # chacter before the ';'.
Wu, Zhenyu887a38c2024-05-09 20:35:13 +0200514 elseif getline(2) =~ '.;$'
Bram Moolenaar851ee6c2017-11-09 20:46:17 +0100515 setf cpp
516 else
Bram Moolenaar48c3f4e2022-08-08 15:42:38 +0100517 # recognize Prolog by specific text in the first non-empty line
518 # require a blank after the '%' because Perl uses "%list" and "%translate"
Bram Moolenaarf07d1a72023-06-09 21:01:47 +0100519 var lnum = getline(nextnonblank(1))
igna_martinoli37853b72024-07-18 21:34:36 +0200520 if lnum =~ '\<prolog\>' || lnum =~ prolog_pattern
Bram Moolenaar48c3f4e2022-08-08 15:42:38 +0100521 setf prolog
522 else
523 exe 'setf ' .. default
524 endif
Bram Moolenaar851ee6c2017-11-09 20:46:17 +0100525 endif
Bram Moolenaara2baa732022-02-04 16:09:54 +0000526enddef
Bram Moolenaar851ee6c2017-11-09 20:46:17 +0100527
Bram Moolenaara2baa732022-02-04 16:09:54 +0000528export def FTm()
Bram Moolenaardeba5eb2021-09-03 19:21:36 +0200529 if exists("g:filetype_m")
Bram Moolenaara2baa732022-02-04 16:09:54 +0000530 exe "setf " .. g:filetype_m
Bram Moolenaardeba5eb2021-09-03 19:21:36 +0200531 return
532 endif
533
Bram Moolenaara2baa732022-02-04 16:09:54 +0000534 # excluding end(for|function|if|switch|while) common to Murphi
535 var octave_block_terminators = '\<end\%(_try_catch\|classdef\|enumeration\|events\|methods\|parfor\|properties\)\>'
Bram Moolenaardeba5eb2021-09-03 19:21:36 +0200536
Bram Moolenaara2baa732022-02-04 16:09:54 +0000537 var objc_preprocessor = '^\s*#\s*\%(import\|include\|define\|if\|ifn\=def\|undef\|line\|error\|pragma\)\>'
Doug Kearns7329cfa2021-11-26 13:01:41 +0000538
Bram Moolenaara2baa732022-02-04 16:09:54 +0000539 var n = 1
540 var saw_comment = 0 # Whether we've seen a multiline comment leader.
Bram Moolenaar851ee6c2017-11-09 20:46:17 +0100541 while n < 100
Bram Moolenaara2baa732022-02-04 16:09:54 +0000542 var line = getline(n)
Bram Moolenaar851ee6c2017-11-09 20:46:17 +0100543 if line =~ '^\s*/\*'
Bram Moolenaara2baa732022-02-04 16:09:54 +0000544 # /* ... */ is a comment in Objective C and Murphi, so we can't conclude
545 # it's either of them yet, but track this as a hint in case we don't see
546 # anything more definitive.
547 saw_comment = 1
Bram Moolenaar851ee6c2017-11-09 20:46:17 +0100548 endif
Doug Kearns7329cfa2021-11-26 13:01:41 +0000549 if line =~ '^\s*//' || line =~ '^\s*@import\>' || line =~ objc_preprocessor
Bram Moolenaar851ee6c2017-11-09 20:46:17 +0100550 setf objc
551 return
552 endif
Bram Moolenaarca0627d2021-09-12 17:03:08 +0200553 if line =~ '^\s*\%(#\|%!\)' || line =~ '^\s*unwind_protect\>' ||
Bram Moolenaardeba5eb2021-09-03 19:21:36 +0200554 \ line =~ '\%(^\|;\)\s*' .. octave_block_terminators
555 setf octave
556 return
557 endif
Bram Moolenaara2baa732022-02-04 16:09:54 +0000558 # TODO: could be Matlab or Octave
Bram Moolenaar851ee6c2017-11-09 20:46:17 +0100559 if line =~ '^\s*%'
560 setf matlab
561 return
562 endif
563 if line =~ '^\s*(\*'
564 setf mma
565 return
566 endif
567 if line =~ '^\c\s*\(\(type\|var\)\>\|--\)'
568 setf murphi
569 return
570 endif
Bram Moolenaara2baa732022-02-04 16:09:54 +0000571 n += 1
Bram Moolenaar851ee6c2017-11-09 20:46:17 +0100572 endwhile
573
574 if saw_comment
Bram Moolenaara2baa732022-02-04 16:09:54 +0000575 # We didn't see anything definitive, but this looks like either Objective C
576 # or Murphi based on the comment leader. Assume the former as it is more
577 # common.
Bram Moolenaar851ee6c2017-11-09 20:46:17 +0100578 setf objc
Bram Moolenaar851ee6c2017-11-09 20:46:17 +0100579 else
Bram Moolenaara2baa732022-02-04 16:09:54 +0000580 # Default is Matlab
Bram Moolenaar851ee6c2017-11-09 20:46:17 +0100581 setf matlab
582 endif
Bram Moolenaara2baa732022-02-04 16:09:54 +0000583enddef
Bram Moolenaar851ee6c2017-11-09 20:46:17 +0100584
Ken Takataeb4b9032024-07-25 21:07:13 +0200585export def FTmake()
Eisuke Kawashimaf35bd762025-04-15 19:20:06 +0200586 # Check if it is a BSD, GNU, or Microsoft Makefile
587 unlet! b:make_flavor
588
589 # 1. filename
590 if expand('%:t') == 'BSDmakefile'
591 b:make_flavor = 'bsd'
592 setf make
593 return
594 elseif expand('%:t') == 'GNUmakefile'
595 b:make_flavor = 'gnu'
596 setf make
597 return
598 endif
599
600 # 2. user's setting
601 if exists('g:make_flavor')
602 b:make_flavor = g:make_flavor
603 setf make
604 return
605 elseif get(g:, 'make_microsoft')
606 echom "make_microsoft is deprecated; try g:make_flavor = 'microsoft' instead"
607 b:make_flavor = 'microsoft'
608 setf make
609 return
610 endif
611
612 # 3. try to detect a flavor from file content
Ken Takataeb4b9032024-07-25 21:07:13 +0200613 var n = 1
614 while n < 1000 && n <= line('$')
615 var line = getline(n)
616 if line =~? '^\s*!\s*\(ifn\=\(def\)\=\|include\|message\|error\)\>'
Eisuke Kawashimaf35bd762025-04-15 19:20:06 +0200617 b:make_flavor = 'microsoft'
Ken Takataeb4b9032024-07-25 21:07:13 +0200618 break
Eisuke Kawashimaf35bd762025-04-15 19:20:06 +0200619 elseif line =~ '^\.\%(export\|error\|for\|if\%(n\=\%(def\|make\)\)\=\|info\|warning\)\>'
620 b:make_flavor = 'bsd'
Ken Takataeb4b9032024-07-25 21:07:13 +0200621 break
Eisuke Kawashimaf35bd762025-04-15 19:20:06 +0200622 elseif line =~ '^ *\%(ifn\=\%(eq\|def\)\|define\|override\)\>'
623 b:make_flavor = 'gnu'
624 break
625 elseif line =~ '\$[({][a-z-]\+\s\+\S\+' # a function call, e.g. $(shell pwd)
626 b:make_flavor = 'gnu'
Ken Takataeb4b9032024-07-25 21:07:13 +0200627 break
628 endif
629 n += 1
630 endwhile
631 setf make
632enddef
633
Bram Moolenaara2baa732022-02-04 16:09:54 +0000634export def FTmms()
635 var n = 1
Bram Moolenaard7df2792020-01-02 21:34:42 +0100636 while n < 20
Bram Moolenaara2baa732022-02-04 16:09:54 +0000637 var line = getline(n)
Bram Moolenaar851ee6c2017-11-09 20:46:17 +0100638 if line =~ '^\s*\(%\|//\)' || line =~ '^\*'
639 setf mmix
640 return
641 endif
642 if line =~ '^\s*#'
643 setf make
644 return
645 endif
Bram Moolenaara2baa732022-02-04 16:09:54 +0000646 n += 1
Bram Moolenaar851ee6c2017-11-09 20:46:17 +0100647 endwhile
648 setf mmix
Bram Moolenaara2baa732022-02-04 16:09:54 +0000649enddef
Bram Moolenaar851ee6c2017-11-09 20:46:17 +0100650
Eisuke Kawashima2cb42ef2025-04-21 10:51:05 +0200651# This function checks if one of the first five lines start with a typical
652# nroff pattern in man files. In that case it is probably an nroff file:
653# 'filetype' is set and 1 is returned.
Bram Moolenaara2baa732022-02-04 16:09:54 +0000654export def FTnroff(): number
Eisuke Kawashimababdb052025-04-15 18:30:05 +0200655 var n = 1
Eisuke Kawashima2cb42ef2025-04-21 10:51:05 +0200656 while n <= 5
Eisuke Kawashimababdb052025-04-15 18:30:05 +0200657 var line = getline(n)
Eisuke Kawashima2cb42ef2025-04-21 10:51:05 +0200658 if line =~ '^\%([.'']\s*\%(TH\|D[dt]\|S[Hh]\|d[es]1\?\|so\)\s\+\S\|[.'']\s*ig\>\|\%([.'']\s*\)\?\\"\)'
Eisuke Kawashimababdb052025-04-15 18:30:05 +0200659 setf nroff
660 return 1
661 endif
662 n += 1
663 endwhile
Bram Moolenaar851ee6c2017-11-09 20:46:17 +0100664 return 0
Bram Moolenaara2baa732022-02-04 16:09:54 +0000665enddef
Bram Moolenaar851ee6c2017-11-09 20:46:17 +0100666
Bram Moolenaara2baa732022-02-04 16:09:54 +0000667export def FTmm()
668 var n = 1
Bram Moolenaard1caa942020-04-10 22:10:56 +0200669 while n < 20
Bram Moolenaara2baa732022-02-04 16:09:54 +0000670 if getline(n) =~ '^\s*\(#\s*\(include\|import\)\>\|@import\>\|/\*\)'
Bram Moolenaar851ee6c2017-11-09 20:46:17 +0100671 setf objcpp
672 return
673 endif
Bram Moolenaara2baa732022-02-04 16:09:54 +0000674 n += 1
Bram Moolenaar851ee6c2017-11-09 20:46:17 +0100675 endwhile
676 setf nroff
Bram Moolenaara2baa732022-02-04 16:09:54 +0000677enddef
Bram Moolenaar851ee6c2017-11-09 20:46:17 +0100678
Bram Moolenaarcdbfc6d2022-06-30 16:25:21 +0100679# Returns true if file content looks like LambdaProlog module
Bram Moolenaar0bbf09c2022-04-09 15:16:53 +0100680def IsLProlog(): bool
Gaetan Lepage4ce1bda2023-05-10 22:01:55 +0100681 # skip apparent comments and blank lines, what looks like
Bram Moolenaar0bbf09c2022-04-09 15:16:53 +0100682 # LambdaProlog comment may be RAPID header
Bram Moolenaarf07d1a72023-06-09 21:01:47 +0100683 var lnum: number = nextnonblank(1)
684 while lnum > 0 && lnum < line('$') && getline(lnum) =~ '^\s*%' # LambdaProlog comment
685 lnum = nextnonblank(lnum + 1)
Bram Moolenaar0bbf09c2022-04-09 15:16:53 +0100686 endwhile
687 # this pattern must not catch a go.mod file
Bram Moolenaarf07d1a72023-06-09 21:01:47 +0100688 return getline(lnum) =~ '\<module\s\+\w\+\s*\.\s*\(%\|$\)'
Bram Moolenaar0bbf09c2022-04-09 15:16:53 +0100689enddef
690
Doug Kearns68a89472024-01-05 17:59:04 +0100691def IsModula2(): bool
dkearnsef387c02024-02-20 06:58:30 +1100692 return getline(nextnonblank(1)) =~ '\<MODULE\s\+\w\+\s*\%(\[.*]\s*\)\=;\|^\s*(\*'
Doug Kearns68a89472024-01-05 17:59:04 +0100693enddef
694
695def SetFiletypeModula2()
696 const KNOWN_DIALECTS = ["iso", "pim", "r10"]
697 const KNOWN_EXTENSIONS = ["gm2"]
698 const LINE_COUNT = 200
699 const TAG = '(\*!m2\(\w\+\)\%(+\(\w\+\)\)\=\*)'
700
701 var dialect = get(g:, "modula2_default_dialect", "pim")
702 var extension = get(g:, "modula2_default_extension", "")
703
704 var matches = []
705
706 # ignore unknown dialects or badly formatted tags
707 for lnum in range(1, min([line("$"), LINE_COUNT]))
708 matches = matchlist(getline(lnum), TAG)
709 if !empty(matches)
710 if index(KNOWN_DIALECTS, matches[1]) >= 0
711 dialect = matches[1]
712 endif
713 if index(KNOWN_EXTENSIONS, matches[2]) >= 0
714 extension = matches[2]
715 endif
716 break
717 endif
718 endfor
719
720 modula2#SetDialect(dialect, extension)
721
722 setf modula2
723enddef
724
Bram Moolenaar0bbf09c2022-04-09 15:16:53 +0100725# Determine if *.mod is ABB RAPID, LambdaProlog, Modula-2, Modsim III or go.mod
726export def FTmod()
Doug Kearns68a89472024-01-05 17:59:04 +0100727 if get(g:, "filetype_mod", "") == "modula2" || IsModula2()
728 SetFiletypeModula2()
729 return
730 endif
731
Bram Moolenaar0bbf09c2022-04-09 15:16:53 +0100732 if exists("g:filetype_mod")
733 exe "setf " .. g:filetype_mod
Omar El Halabic9fbd252023-05-29 19:59:45 +0100734 elseif expand("<afile>") =~ '\<go.mod$'
735 setf gomod
Bram Moolenaar0bbf09c2022-04-09 15:16:53 +0100736 elseif IsLProlog()
737 setf lprolog
Bram Moolenaar0bbf09c2022-04-09 15:16:53 +0100738 elseif IsRapid()
739 setf rapid
Bram Moolenaar0bbf09c2022-04-09 15:16:53 +0100740 else
741 # Nothing recognized, assume modsim3
742 setf modsim3
743 endif
744enddef
745
Bram Moolenaara2baa732022-02-04 16:09:54 +0000746export def FTpl()
Bram Moolenaar851ee6c2017-11-09 20:46:17 +0100747 if exists("g:filetype_pl")
Bram Moolenaara2baa732022-02-04 16:09:54 +0000748 exe "setf " .. g:filetype_pl
Bram Moolenaar851ee6c2017-11-09 20:46:17 +0100749 else
Bram Moolenaara2baa732022-02-04 16:09:54 +0000750 # recognize Prolog by specific text in the first non-empty line
751 # require a blank after the '%' because Perl uses "%list" and "%translate"
Bram Moolenaarf07d1a72023-06-09 21:01:47 +0100752 var line = getline(nextnonblank(1))
igna_martinoli37853b72024-07-18 21:34:36 +0200753 if line =~ '\<prolog\>' || line =~ prolog_pattern
Bram Moolenaar851ee6c2017-11-09 20:46:17 +0100754 setf prolog
755 else
756 setf perl
757 endif
758 endif
Bram Moolenaara2baa732022-02-04 16:09:54 +0000759enddef
Bram Moolenaar851ee6c2017-11-09 20:46:17 +0100760
Bram Moolenaara2baa732022-02-04 16:09:54 +0000761export def FTinc()
Bram Moolenaar851ee6c2017-11-09 20:46:17 +0100762 if exists("g:filetype_inc")
Bram Moolenaara2baa732022-02-04 16:09:54 +0000763 exe "setf " .. g:filetype_inc
Bram Moolenaar851ee6c2017-11-09 20:46:17 +0100764 else
Bram Moolenaara2baa732022-02-04 16:09:54 +0000765 var lines = getline(1) .. getline(2) .. getline(3)
Bram Moolenaar851ee6c2017-11-09 20:46:17 +0100766 if lines =~? "perlscript"
767 setf aspperl
768 elseif lines =~ "<%"
769 setf aspvbs
770 elseif lines =~ "<?"
771 setf php
Bram Moolenaara2baa732022-02-04 16:09:54 +0000772 # Pascal supports // comments but they're vary rarely used for file
773 # headers so assume POV-Ray
Bram Moolenaarb2c72352022-02-22 21:17:40 +0000774 elseif lines =~ '^\s*\%({\|(\*\)' || lines =~? ft_pascal_keywords
Bram Moolenaara0122dc2021-01-12 17:42:24 +0100775 setf pascal
Gregory Anders30e212d2022-07-26 21:42:03 +0100776 elseif lines =~# '\<\%(require\|inherit\)\>' || lines =~# '[A-Z][A-Za-z0-9_:${}]*\s\+\%(??\|[?:+]\)\?= '
Gregory Andersfa49eb42022-07-16 17:46:47 +0100777 setf bitbake
Bram Moolenaar851ee6c2017-11-09 20:46:17 +0100778 else
Bram Moolenaara2baa732022-02-04 16:09:54 +0000779 FTasmsyntax()
Bram Moolenaar851ee6c2017-11-09 20:46:17 +0100780 if exists("b:asmsyntax")
Gregory Andersfa49eb42022-07-16 17:46:47 +0100781 exe "setf " .. fnameescape(b:asmsyntax)
Bram Moolenaar851ee6c2017-11-09 20:46:17 +0100782 else
Gregory Andersfa49eb42022-07-16 17:46:47 +0100783 setf pov
Bram Moolenaar851ee6c2017-11-09 20:46:17 +0100784 endif
785 endif
786 endif
Bram Moolenaara2baa732022-02-04 16:09:54 +0000787enddef
Bram Moolenaar851ee6c2017-11-09 20:46:17 +0100788
Bram Moolenaara2baa732022-02-04 16:09:54 +0000789export def FTprogress_cweb()
Bram Moolenaar851ee6c2017-11-09 20:46:17 +0100790 if exists("g:filetype_w")
Bram Moolenaara2baa732022-02-04 16:09:54 +0000791 exe "setf " .. g:filetype_w
Bram Moolenaar851ee6c2017-11-09 20:46:17 +0100792 return
793 endif
794 if getline(1) =~ '&ANALYZE' || getline(3) =~ '&GLOBAL-DEFINE'
795 setf progress
796 else
797 setf cweb
798 endif
Bram Moolenaara2baa732022-02-04 16:09:54 +0000799enddef
Bram Moolenaar851ee6c2017-11-09 20:46:17 +0100800
Julien Marrec2e310652023-11-25 15:30:46 +0100801# These include the leading '%' sign
802var ft_swig_keywords = '^\s*%\%(addmethods\|apply\|beginfile\|clear\|constant\|define\|echo\|enddef\|endoffile\|extend\|feature\|fragment\|ignore\|import\|importfile\|include\|includefile\|inline\|insert\|keyword\|module\|name\|namewarn\|native\|newobject\|parms\|pragma\|rename\|template\|typedef\|typemap\|types\|varargs\|warn\)'
803# This is the start/end of a block that is copied literally to the processor file (C/C++)
804var ft_swig_verbatim_block_start = '^\s*%{'
805
806export def FTi()
Bram Moolenaar851ee6c2017-11-09 20:46:17 +0100807 if exists("g:filetype_i")
Bram Moolenaara2baa732022-02-04 16:09:54 +0000808 exe "setf " .. g:filetype_i
Bram Moolenaar851ee6c2017-11-09 20:46:17 +0100809 return
810 endif
Julien Marrec2e310652023-11-25 15:30:46 +0100811 # This function checks for an assembly comment or a SWIG keyword or verbatim block in the first 50 lines.
Bram Moolenaara2baa732022-02-04 16:09:54 +0000812 # If not found, assume Progress.
813 var lnum = 1
Julien Marrec2e310652023-11-25 15:30:46 +0100814 while lnum <= 50 && lnum < line('$')
Bram Moolenaara2baa732022-02-04 16:09:54 +0000815 var line = getline(lnum)
Bram Moolenaar851ee6c2017-11-09 20:46:17 +0100816 if line =~ '^\s*;' || line =~ '^\*'
Bram Moolenaara2baa732022-02-04 16:09:54 +0000817 FTasm()
Bram Moolenaar851ee6c2017-11-09 20:46:17 +0100818 return
Julien Marrec2e310652023-11-25 15:30:46 +0100819 elseif line =~ ft_swig_keywords || line =~ ft_swig_verbatim_block_start
820 setf swig
821 return
Bram Moolenaar851ee6c2017-11-09 20:46:17 +0100822 endif
Bram Moolenaara2baa732022-02-04 16:09:54 +0000823 lnum += 1
Bram Moolenaarc12dc472022-03-05 13:45:56 +0000824 endwhile
Bram Moolenaar851ee6c2017-11-09 20:46:17 +0100825 setf progress
Bram Moolenaara2baa732022-02-04 16:09:54 +0000826enddef
Bram Moolenaar851ee6c2017-11-09 20:46:17 +0100827
Bram Moolenaara2baa732022-02-04 16:09:54 +0000828var ft_pascal_comments = '^\s*\%({\|(\*\|//\)'
829var ft_pascal_keywords = '^\s*\%(program\|unit\|library\|uses\|begin\|procedure\|function\|const\|type\|var\)\>'
Bram Moolenaara0122dc2021-01-12 17:42:24 +0100830
Bram Moolenaara2baa732022-02-04 16:09:54 +0000831export def FTprogress_pascal()
Bram Moolenaar851ee6c2017-11-09 20:46:17 +0100832 if exists("g:filetype_p")
Bram Moolenaara2baa732022-02-04 16:09:54 +0000833 exe "setf " .. g:filetype_p
Bram Moolenaar851ee6c2017-11-09 20:46:17 +0100834 return
835 endif
Bram Moolenaara2baa732022-02-04 16:09:54 +0000836 # This function checks for valid Pascal syntax in the first ten lines.
837 # Look for either an opening comment or a program start.
838 # If not found, assume Progress.
839 var lnum = 1
Bram Moolenaar851ee6c2017-11-09 20:46:17 +0100840 while lnum <= 10 && lnum < line('$')
Bram Moolenaara2baa732022-02-04 16:09:54 +0000841 var line = getline(lnum)
Bram Moolenaarb2c72352022-02-22 21:17:40 +0000842 if line =~ ft_pascal_comments || line =~? ft_pascal_keywords
Bram Moolenaar851ee6c2017-11-09 20:46:17 +0100843 setf pascal
844 return
845 elseif line !~ '^\s*$' || line =~ '^/\*'
Bram Moolenaara2baa732022-02-04 16:09:54 +0000846 # Not an empty line: Doesn't look like valid Pascal code.
847 # Or it looks like a Progress /* comment
Bram Moolenaar851ee6c2017-11-09 20:46:17 +0100848 break
849 endif
Bram Moolenaara2baa732022-02-04 16:09:54 +0000850 lnum += 1
Bram Moolenaarc12dc472022-03-05 13:45:56 +0000851 endwhile
Bram Moolenaar851ee6c2017-11-09 20:46:17 +0100852 setf progress
Bram Moolenaara2baa732022-02-04 16:09:54 +0000853enddef
Bram Moolenaar851ee6c2017-11-09 20:46:17 +0100854
Bram Moolenaara2baa732022-02-04 16:09:54 +0000855export def FTpp()
Bram Moolenaara0122dc2021-01-12 17:42:24 +0100856 if exists("g:filetype_pp")
Bram Moolenaara2baa732022-02-04 16:09:54 +0000857 exe "setf " .. g:filetype_pp
Bram Moolenaara0122dc2021-01-12 17:42:24 +0100858 else
Bram Moolenaara2baa732022-02-04 16:09:54 +0000859 var line = getline(nextnonblank(1))
Bram Moolenaarb2c72352022-02-22 21:17:40 +0000860 if line =~ ft_pascal_comments || line =~? ft_pascal_keywords
Bram Moolenaara0122dc2021-01-12 17:42:24 +0100861 setf pascal
862 else
863 setf puppet
864 endif
865 endif
Bram Moolenaara2baa732022-02-04 16:09:54 +0000866enddef
Bram Moolenaara0122dc2021-01-12 17:42:24 +0100867
Bram Moolenaar0bbf09c2022-04-09 15:16:53 +0100868# Determine if *.prg is ABB RAPID. Can also be Clipper, FoxPro or eviews
869export def FTprg()
870 if exists("g:filetype_prg")
871 exe "setf " .. g:filetype_prg
872 elseif IsRapid()
873 setf rapid
874 else
875 # Nothing recognized, assume Clipper
876 setf clipper
877 endif
878enddef
879
Bram Moolenaara2baa732022-02-04 16:09:54 +0000880export def FTr()
881 var max = line("$") > 50 ? 50 : line("$")
Bram Moolenaar851ee6c2017-11-09 20:46:17 +0100882
883 for n in range(1, max)
Bram Moolenaara2baa732022-02-04 16:09:54 +0000884 # Rebol is easy to recognize, check for that first
Bram Moolenaar851ee6c2017-11-09 20:46:17 +0100885 if getline(n) =~? '\<REBOL\>'
886 setf rebol
887 return
888 endif
889 endfor
890
891 for n in range(1, max)
Bram Moolenaara2baa732022-02-04 16:09:54 +0000892 # R has # comments
Bram Moolenaar851ee6c2017-11-09 20:46:17 +0100893 if getline(n) =~ '^\s*#'
894 setf r
895 return
896 endif
Bram Moolenaara2baa732022-02-04 16:09:54 +0000897 # Rexx has /* comments */
Bram Moolenaar851ee6c2017-11-09 20:46:17 +0100898 if getline(n) =~ '^\s*/\*'
899 setf rexx
900 return
901 endif
902 endfor
903
Bram Moolenaara2baa732022-02-04 16:09:54 +0000904 # Nothing recognized, use user default or assume Rexx
Bram Moolenaar851ee6c2017-11-09 20:46:17 +0100905 if exists("g:filetype_r")
Bram Moolenaara2baa732022-02-04 16:09:54 +0000906 exe "setf " .. g:filetype_r
Bram Moolenaar851ee6c2017-11-09 20:46:17 +0100907 else
Bram Moolenaara2baa732022-02-04 16:09:54 +0000908 # Rexx used to be the default, but R appears to be much more popular.
Bram Moolenaar851ee6c2017-11-09 20:46:17 +0100909 setf r
910 endif
Bram Moolenaara2baa732022-02-04 16:09:54 +0000911enddef
Bram Moolenaar851ee6c2017-11-09 20:46:17 +0100912
Bram Moolenaara2baa732022-02-04 16:09:54 +0000913export def McSetf()
914 # Rely on the file to start with a comment.
915 # MS message text files use ';', Sendmail files use '#' or 'dnl'
Bram Moolenaar851ee6c2017-11-09 20:46:17 +0100916 for lnum in range(1, min([line("$"), 20]))
Bram Moolenaara2baa732022-02-04 16:09:54 +0000917 var line = getline(lnum)
Bram Moolenaar851ee6c2017-11-09 20:46:17 +0100918 if line =~ '^\s*\(#\|dnl\)'
Bram Moolenaara2baa732022-02-04 16:09:54 +0000919 setf m4 # Sendmail .mc file
Bram Moolenaar851ee6c2017-11-09 20:46:17 +0100920 return
921 elseif line =~ '^\s*;'
Bram Moolenaara2baa732022-02-04 16:09:54 +0000922 setf msmessages # MS Message text file
Bram Moolenaar851ee6c2017-11-09 20:46:17 +0100923 return
924 endif
925 endfor
Bram Moolenaar0bbf09c2022-04-09 15:16:53 +0100926 setf m4 # Default: Sendmail .mc file
Bram Moolenaara2baa732022-02-04 16:09:54 +0000927enddef
Bram Moolenaar851ee6c2017-11-09 20:46:17 +0100928
Bram Moolenaara2baa732022-02-04 16:09:54 +0000929# Called from filetype.vim and scripts.vim.
Bram Moolenaarf07d1a72023-06-09 21:01:47 +0100930# When "setft" is passed and false then the 'filetype' option is not set.
931export def SetFileTypeSH(name: string, setft = true): string
932 if setft && did_filetype()
Bram Moolenaara2baa732022-02-04 16:09:54 +0000933 # Filetype was already detected
Bram Moolenaarf07d1a72023-06-09 21:01:47 +0100934 return ''
Bram Moolenaar147e7d02019-01-18 21:46:47 +0100935 endif
Bram Moolenaarf07d1a72023-06-09 21:01:47 +0100936 if setft && expand("<amatch>") =~ g:ft_ignore_pat
937 return ''
Bram Moolenaar851ee6c2017-11-09 20:46:17 +0100938 endif
Aliaksei Budavei5c84d122025-04-10 21:52:14 +0200939 if name =~ '^csh$' || name =~ '^#!.\{-2,}\<csh\>'
Bram Moolenaara2baa732022-02-04 16:09:54 +0000940 # Some .sh scripts contain #!/bin/csh.
Bram Moolenaarf07d1a72023-06-09 21:01:47 +0100941 return SetFileTypeShell("csh", setft)
Aliaksei Budavei5c84d122025-04-10 21:52:14 +0200942 elseif name =~ '^tcsh$' || name =~ '^#!.\{-2,}\<tcsh\>'
Bram Moolenaara2baa732022-02-04 16:09:54 +0000943 # Some .sh scripts contain #!/bin/tcsh.
Bram Moolenaarf07d1a72023-06-09 21:01:47 +0100944 return SetFileTypeShell("tcsh", setft)
Aliaksei Budavei5c84d122025-04-10 21:52:14 +0200945 elseif name =~ '^zsh$' || name =~ '^#!.\{-2,}\<zsh\>'
Bram Moolenaara2baa732022-02-04 16:09:54 +0000946 # Some .sh scripts contain #!/bin/zsh.
Bram Moolenaarf07d1a72023-06-09 21:01:47 +0100947 return SetFileTypeShell("zsh", setft)
Aliaksei Budavei5c84d122025-04-10 21:52:14 +0200948 elseif name =~ '^ksh$' || name =~ '^#!.\{-2,}\<ksh\>'
Bram Moolenaara2baa732022-02-04 16:09:54 +0000949 b:is_kornshell = 1
Bram Moolenaar851ee6c2017-11-09 20:46:17 +0100950 if exists("b:is_bash")
951 unlet b:is_bash
952 endif
953 if exists("b:is_sh")
954 unlet b:is_sh
955 endif
Aliaksei Budavei5c84d122025-04-10 21:52:14 +0200956 elseif exists("g:bash_is_sh") || name =~ '^bash2\=$' ||
957 \ name =~ '^#!.\{-2,}\<bash2\=\>'
Bram Moolenaara2baa732022-02-04 16:09:54 +0000958 b:is_bash = 1
Bram Moolenaar851ee6c2017-11-09 20:46:17 +0100959 if exists("b:is_kornshell")
960 unlet b:is_kornshell
961 endif
962 if exists("b:is_sh")
963 unlet b:is_sh
964 endif
Aliaksei Budavei5c84d122025-04-10 21:52:14 +0200965 elseif name =~ '^\%(da\)\=sh$' || name =~ '^#!.\{-2,}\<\%(da\)\=sh\>'
Eisuke Kawashima24482fb2022-11-24 10:58:10 +0000966 # Ubuntu links "sh" to "dash", thus it is expected to work the same way
Bram Moolenaara2baa732022-02-04 16:09:54 +0000967 b:is_sh = 1
Bram Moolenaar851ee6c2017-11-09 20:46:17 +0100968 if exists("b:is_kornshell")
969 unlet b:is_kornshell
970 endif
971 if exists("b:is_bash")
972 unlet b:is_bash
973 endif
974 endif
Bram Moolenaarf07d1a72023-06-09 21:01:47 +0100975
976 return SetFileTypeShell("sh", setft)
Bram Moolenaara2baa732022-02-04 16:09:54 +0000977enddef
Bram Moolenaar851ee6c2017-11-09 20:46:17 +0100978
Bram Moolenaara2baa732022-02-04 16:09:54 +0000979# For shell-like file types, check for an "exec" command hidden in a comment,
980# as used for Tcl.
Bram Moolenaarf07d1a72023-06-09 21:01:47 +0100981# When "setft" is passed and false then the 'filetype' option is not set.
Bram Moolenaara2baa732022-02-04 16:09:54 +0000982# Also called from scripts.vim, thus can't be local to this script.
Bram Moolenaarf07d1a72023-06-09 21:01:47 +0100983export def SetFileTypeShell(name: string, setft = true): string
984 if setft && did_filetype()
Bram Moolenaara2baa732022-02-04 16:09:54 +0000985 # Filetype was already detected
Bram Moolenaarf07d1a72023-06-09 21:01:47 +0100986 return ''
Bram Moolenaar147e7d02019-01-18 21:46:47 +0100987 endif
Bram Moolenaarf07d1a72023-06-09 21:01:47 +0100988 if setft && expand("<amatch>") =~ g:ft_ignore_pat
989 return ''
Bram Moolenaar851ee6c2017-11-09 20:46:17 +0100990 endif
Bram Moolenaarf07d1a72023-06-09 21:01:47 +0100991
992 var lnum = 2
993 while lnum < 20 && lnum < line("$") && getline(lnum) =~ '^\s*\(#\|$\)'
Bram Moolenaara2baa732022-02-04 16:09:54 +0000994 # Skip empty and comment lines.
Bram Moolenaarf07d1a72023-06-09 21:01:47 +0100995 lnum += 1
Bram Moolenaar851ee6c2017-11-09 20:46:17 +0100996 endwhile
Bram Moolenaarf07d1a72023-06-09 21:01:47 +0100997 if lnum < line("$") && getline(lnum) =~ '\s*exec\s' && getline(lnum - 1) =~ '^\s*#.*\\$'
Bram Moolenaara2baa732022-02-04 16:09:54 +0000998 # Found an "exec" line after a comment with continuation
Bram Moolenaarf07d1a72023-06-09 21:01:47 +0100999 var n = substitute(getline(lnum), '\s*exec\s\+\([^ ]*/\)\=', '', '')
Bram Moolenaar851ee6c2017-11-09 20:46:17 +01001000 if n =~ '\<tclsh\|\<wish'
Bram Moolenaarf07d1a72023-06-09 21:01:47 +01001001 if setft
1002 setf tcl
1003 endif
1004 return 'tcl'
Bram Moolenaar851ee6c2017-11-09 20:46:17 +01001005 endif
1006 endif
Bram Moolenaarf07d1a72023-06-09 21:01:47 +01001007
1008 if setft
1009 exe "setf " .. name
1010 endif
1011 return name
Bram Moolenaara2baa732022-02-04 16:09:54 +00001012enddef
Bram Moolenaar851ee6c2017-11-09 20:46:17 +01001013
Bram Moolenaara2baa732022-02-04 16:09:54 +00001014export def CSH()
Bram Moolenaar147e7d02019-01-18 21:46:47 +01001015 if did_filetype()
Bram Moolenaara2baa732022-02-04 16:09:54 +00001016 # Filetype was already detected
Bram Moolenaar147e7d02019-01-18 21:46:47 +01001017 return
1018 endif
Bram Moolenaar851ee6c2017-11-09 20:46:17 +01001019 if exists("g:filetype_csh")
Bram Moolenaara2baa732022-02-04 16:09:54 +00001020 SetFileTypeShell(g:filetype_csh)
Bram Moolenaar851ee6c2017-11-09 20:46:17 +01001021 elseif &shell =~ "tcsh"
Bram Moolenaara2baa732022-02-04 16:09:54 +00001022 SetFileTypeShell("tcsh")
Bram Moolenaar851ee6c2017-11-09 20:46:17 +01001023 else
Bram Moolenaara2baa732022-02-04 16:09:54 +00001024 SetFileTypeShell("csh")
Bram Moolenaar851ee6c2017-11-09 20:46:17 +01001025 endif
Bram Moolenaara2baa732022-02-04 16:09:54 +00001026enddef
Bram Moolenaar851ee6c2017-11-09 20:46:17 +01001027
Bram Moolenaara2baa732022-02-04 16:09:54 +00001028var ft_rules_udev_rules_pattern = '^\s*\cudev_rules\s*=\s*"\([^"]\{-1,}\)/*".*'
1029export def FTRules()
1030 var path = expand('<amatch>:p')
Bram Moolenaaraa9675a2020-08-17 21:57:09 +02001031 if path =~ '/\(etc/udev/\%(rules\.d/\)\=.*\.rules\|\%(usr/\)\=lib/udev/\%(rules\.d/\)\=.*\.rules\)$'
Bram Moolenaar851ee6c2017-11-09 20:46:17 +01001032 setf udevrules
1033 return
1034 endif
1035 if path =~ '^/etc/ufw/'
Bram Moolenaara2baa732022-02-04 16:09:54 +00001036 setf conf # Better than hog
Bram Moolenaar851ee6c2017-11-09 20:46:17 +01001037 return
1038 endif
1039 if path =~ '^/\(etc\|usr/share\)/polkit-1/rules\.d'
1040 setf javascript
1041 return
1042 endif
Bram Moolenaara2baa732022-02-04 16:09:54 +00001043 var config_lines: list<string>
Bram Moolenaar851ee6c2017-11-09 20:46:17 +01001044 try
Bram Moolenaara2baa732022-02-04 16:09:54 +00001045 config_lines = readfile('/etc/udev/udev.conf')
Bram Moolenaar851ee6c2017-11-09 20:46:17 +01001046 catch /^Vim\%((\a\+)\)\=:E484/
1047 setf hog
1048 return
1049 endtry
Bram Moolenaara2baa732022-02-04 16:09:54 +00001050 var dir = expand('<amatch>:p:h')
Bram Moolenaar851ee6c2017-11-09 20:46:17 +01001051 for line in config_lines
Bram Moolenaarb2c72352022-02-22 21:17:40 +00001052 if line =~ ft_rules_udev_rules_pattern
1053 var udev_rules = substitute(line, ft_rules_udev_rules_pattern, '\1', "")
Bram Moolenaar851ee6c2017-11-09 20:46:17 +01001054 if dir == udev_rules
1055 setf udevrules
1056 endif
1057 break
1058 endif
1059 endfor
1060 setf hog
Bram Moolenaara2baa732022-02-04 16:09:54 +00001061enddef
Bram Moolenaar851ee6c2017-11-09 20:46:17 +01001062
Bram Moolenaara2baa732022-02-04 16:09:54 +00001063export def SQL()
Bram Moolenaar851ee6c2017-11-09 20:46:17 +01001064 if exists("g:filetype_sql")
Bram Moolenaara2baa732022-02-04 16:09:54 +00001065 exe "setf " .. g:filetype_sql
Bram Moolenaar851ee6c2017-11-09 20:46:17 +01001066 else
1067 setf sql
1068 endif
Bram Moolenaara2baa732022-02-04 16:09:54 +00001069enddef
Bram Moolenaar851ee6c2017-11-09 20:46:17 +01001070
Wu, Zhenyu4f73c072025-01-08 20:20:06 +01001071export def FTsa()
1072 if join(getline(1, 4), "\n") =~# '\%(^\|\n\);'
1073 setf tiasm
1074 return
1075 endif
1076 setf sather
1077enddef
1078
ranjithshegde8cac20e2022-04-13 15:29:21 +01001079# This function checks the first 25 lines of file extension "sc" to resolve
Chris Kipp70ef3f52022-12-14 16:42:15 +00001080# detection between scala and SuperCollider.
1081# NOTE: We don't check for 'Class : Method', as this can easily be confused
1082# with valid Scala like `val x : Int = 3`. So we instead only rely on
1083# checks that can't be confused.
ranjithshegde8cac20e2022-04-13 15:29:21 +01001084export def FTsc()
1085 for lnum in range(1, min([line("$"), 25]))
Chris Kipp70ef3f52022-12-14 16:42:15 +00001086 if getline(lnum) =~# 'var\s<\|classvar\s<\|\^this.*\||\w\+|\|+\s\w*\s{\|\*ar\s'
ranjithshegde8cac20e2022-04-13 15:29:21 +01001087 setf supercollider
1088 return
1089 endif
1090 endfor
1091 setf scala
1092enddef
1093
1094# This function checks the first line of file extension "scd" to resolve
1095# detection between scdoc and SuperCollider
1096export def FTscd()
1097 if getline(1) =~# '\%^\S\+(\d[0-9A-Za-z]*)\%(\s\+\"[^"]*\"\%(\s\+\"[^"]*\"\)\=\)\=$'
1098 setf scdoc
1099 else
1100 setf supercollider
1101 endif
1102enddef
1103
Bram Moolenaara2baa732022-02-04 16:09:54 +00001104# If the file has an extension of 't' and is in a directory 't' or 'xt' then
1105# it is almost certainly a Perl test file.
1106# If the first line starts with '#' and contains 'perl' it's probably a Perl
1107# file.
1108# (Slow test) If a file contains a 'use' statement then it is almost certainly
1109# a Perl file.
1110export def FTperl(): number
1111 var dirname = expand("%:p:h:t")
Bram Moolenaar851ee6c2017-11-09 20:46:17 +01001112 if expand("%:e") == 't' && (dirname == 't' || dirname == 'xt')
1113 setf perl
1114 return 1
1115 endif
1116 if getline(1)[0] == '#' && getline(1) =~ 'perl'
1117 setf perl
1118 return 1
1119 endif
Bram Moolenaara2baa732022-02-04 16:09:54 +00001120 var save_cursor = getpos('.')
1121 call cursor(1, 1)
Bram Moolenaare5b78972022-02-05 19:50:34 +00001122 var has_use = search('^use\s\s*\k', 'c', 30) > 0
Bram Moolenaarf0b03c42017-12-17 17:17:07 +01001123 call setpos('.', save_cursor)
1124 if has_use
Bram Moolenaar851ee6c2017-11-09 20:46:17 +01001125 setf perl
1126 return 1
1127 endif
1128 return 0
Bram Moolenaara2baa732022-02-04 16:09:54 +00001129enddef
Bram Moolenaar851ee6c2017-11-09 20:46:17 +01001130
Bram Moolenaarcdbfc6d2022-06-30 16:25:21 +01001131# LambdaProlog and Standard ML signature files
1132export def FTsig()
1133 if exists("g:filetype_sig")
1134 exe "setf " .. g:filetype_sig
1135 return
1136 endif
1137
1138 var lprolog_comment = '^\s*\%(/\*\|%\)'
1139 var lprolog_keyword = '^\s*sig\s\+\a'
1140 var sml_comment = '^\s*(\*'
1141 var sml_keyword = '^\s*\%(signature\|structure\)\s\+\a'
1142
1143 var line = getline(nextnonblank(1))
1144
1145 if line =~ lprolog_comment || line =~# lprolog_keyword
1146 setf lprolog
1147 elseif line =~ sml_comment || line =~# sml_keyword
1148 setf sml
1149 endif
1150enddef
1151
Bram Moolenaarbe807d52022-09-01 15:01:25 +01001152# This function checks the first 100 lines of files matching "*.sil" to
1153# resolve detection between Swift Intermediate Language and SILE.
1154export def FTsil()
1155 for lnum in range(1, [line('$'), 100]->min())
1156 var line: string = getline(lnum)
1157 if line =~ '^\s*[\\%]'
1158 setf sile
1159 return
1160 elseif line =~ '^\s*\S'
1161 setf sil
1162 return
1163 endif
1164 endfor
1165 # no clue, default to "sil"
1166 setf sil
1167enddef
1168
Bram Moolenaar0bbf09c2022-04-09 15:16:53 +01001169export def FTsys()
KnoP-01f420ff22022-04-13 20:46:21 +01001170 if exists("g:filetype_sys")
1171 exe "setf " .. g:filetype_sys
1172 elseif IsRapid()
Bram Moolenaar0bbf09c2022-04-09 15:16:53 +01001173 setf rapid
1174 else
1175 setf bat
1176 endif
1177enddef
1178
Bram Moolenaara2baa732022-02-04 16:09:54 +00001179# Choose context, plaintex, or tex (LaTeX) based on these rules:
1180# 1. Check the first line of the file for "%&<format>".
1181# 2. Check the first 1000 non-comment lines for LaTeX or ConTeXt keywords.
1182# 3. Default to "plain" or to g:tex_flavor, can be set in user's vimrc.
1183export def FTtex()
1184 var firstline = getline(1)
1185 var format: string
Bram Moolenaar851ee6c2017-11-09 20:46:17 +01001186 if firstline =~ '^%&\s*\a\+'
Bram Moolenaara2baa732022-02-04 16:09:54 +00001187 format = tolower(matchstr(firstline, '\a\+'))
1188 format = substitute(format, 'pdf', '', '')
Bram Moolenaar851ee6c2017-11-09 20:46:17 +01001189 if format == 'tex'
Bram Moolenaara2baa732022-02-04 16:09:54 +00001190 format = 'latex'
Bram Moolenaar851ee6c2017-11-09 20:46:17 +01001191 elseif format == 'plaintex'
Bram Moolenaara2baa732022-02-04 16:09:54 +00001192 format = 'plain'
Bram Moolenaar851ee6c2017-11-09 20:46:17 +01001193 endif
1194 elseif expand('%') =~ 'tex/context/.*/.*.tex'
Bram Moolenaara2baa732022-02-04 16:09:54 +00001195 format = 'context'
Bram Moolenaar851ee6c2017-11-09 20:46:17 +01001196 else
Bram Moolenaara2baa732022-02-04 16:09:54 +00001197 # Default value, may be changed later:
1198 format = exists("g:tex_flavor") ? g:tex_flavor : 'plain'
1199 # Save position, go to the top of the file, find first non-comment line.
1200 var save_cursor = getpos('.')
1201 call cursor(1, 1)
1202 var firstNC = search('^\s*[^[:space:]%]', 'c', 1000)
Bram Moolenaare5b78972022-02-05 19:50:34 +00001203 if firstNC > 0
1204 # Check the next thousand lines for a LaTeX or ConTeXt keyword.
Bram Moolenaara2baa732022-02-04 16:09:54 +00001205 var lpat = 'documentclass\>\|usepackage\>\|begin{\|newcommand\>\|renewcommand\>'
1206 var cpat = 'start\a\+\|setup\a\+\|usemodule\|enablemode\|enableregime\|setvariables\|useencoding\|usesymbols\|stelle\a\+\|verwende\a\+\|stel\a\+\|gebruik\a\+\|usa\a\+\|imposta\a\+\|regle\a\+\|utilisemodule\>'
1207 var kwline = search('^\s*\\\%(' .. lpat .. '\)\|^\s*\\\(' .. cpat .. '\)',
1208 'cnp', firstNC + 1000)
1209 if kwline == 1 # lpat matched
1210 format = 'latex'
1211 elseif kwline == 2 # cpat matched
1212 format = 'context'
1213 endif # If neither matched, keep default set above.
1214 # let lline = search('^\s*\\\%(' . lpat . '\)', 'cn', firstNC + 1000)
1215 # let cline = search('^\s*\\\%(' . cpat . '\)', 'cn', firstNC + 1000)
1216 # if cline > 0
1217 # let format = 'context'
1218 # endif
1219 # if lline > 0 && (cline == 0 || cline > lline)
1220 # let format = 'tex'
1221 # endif
1222 endif # firstNC
Bram Moolenaar851ee6c2017-11-09 20:46:17 +01001223 call setpos('.', save_cursor)
Bram Moolenaara2baa732022-02-04 16:09:54 +00001224 endif # firstline =~ '^%&\s*\a\+'
Bram Moolenaar851ee6c2017-11-09 20:46:17 +01001225
Bram Moolenaara2baa732022-02-04 16:09:54 +00001226 # Translation from formats to file types. TODO: add AMSTeX, RevTex, others?
Bram Moolenaar851ee6c2017-11-09 20:46:17 +01001227 if format == 'plain'
1228 setf plaintex
1229 elseif format == 'context'
1230 setf context
Bram Moolenaara2baa732022-02-04 16:09:54 +00001231 else # probably LaTeX
Bram Moolenaar851ee6c2017-11-09 20:46:17 +01001232 setf tex
1233 endif
1234 return
Bram Moolenaara2baa732022-02-04 16:09:54 +00001235enddef
Bram Moolenaar851ee6c2017-11-09 20:46:17 +01001236
Bram Moolenaara2baa732022-02-04 16:09:54 +00001237export def FTxml()
1238 var n = 1
Bram Moolenaar493fbe42019-03-17 17:16:12 +01001239 while n < 100 && n <= line("$")
Bram Moolenaara2baa732022-02-04 16:09:54 +00001240 var line = getline(n)
1241 # DocBook 4 or DocBook 5.
1242 var is_docbook4 = line =~ '<!DOCTYPE.*DocBook'
1243 var is_docbook5 = line =~ ' xmlns="http://docbook.org/ns/docbook"'
Bram Moolenaar851ee6c2017-11-09 20:46:17 +01001244 if is_docbook4 || is_docbook5
Bram Moolenaara2baa732022-02-04 16:09:54 +00001245 b:docbk_type = "xml"
Bram Moolenaar851ee6c2017-11-09 20:46:17 +01001246 if is_docbook5
Bram Moolenaara2baa732022-02-04 16:09:54 +00001247 b:docbk_ver = 5
Bram Moolenaar851ee6c2017-11-09 20:46:17 +01001248 else
Bram Moolenaara2baa732022-02-04 16:09:54 +00001249 b:docbk_ver = 4
Bram Moolenaar851ee6c2017-11-09 20:46:17 +01001250 endif
1251 setf docbk
1252 return
1253 endif
1254 if line =~ 'xmlns:xbl="http://www.mozilla.org/xbl"'
1255 setf xbl
1256 return
1257 endif
Bram Moolenaara2baa732022-02-04 16:09:54 +00001258 n += 1
Bram Moolenaar851ee6c2017-11-09 20:46:17 +01001259 endwhile
1260 setf xml
Bram Moolenaara2baa732022-02-04 16:09:54 +00001261enddef
Bram Moolenaar851ee6c2017-11-09 20:46:17 +01001262
Bram Moolenaara2baa732022-02-04 16:09:54 +00001263export def FTy()
1264 var n = 1
Bram Moolenaar493fbe42019-03-17 17:16:12 +01001265 while n < 100 && n <= line("$")
Bram Moolenaara2baa732022-02-04 16:09:54 +00001266 var line = getline(n)
Bram Moolenaar851ee6c2017-11-09 20:46:17 +01001267 if line =~ '^\s*%'
1268 setf yacc
1269 return
1270 endif
1271 if getline(n) =~ '^\s*\(#\|class\>\)' && getline(n) !~ '^\s*#\s*include'
1272 setf racc
1273 return
1274 endif
Bram Moolenaara2baa732022-02-04 16:09:54 +00001275 n += 1
Bram Moolenaar851ee6c2017-11-09 20:46:17 +01001276 endwhile
1277 setf yacc
Bram Moolenaara2baa732022-02-04 16:09:54 +00001278enddef
Bram Moolenaar851ee6c2017-11-09 20:46:17 +01001279
Bram Moolenaara2baa732022-02-04 16:09:54 +00001280export def Redif()
1281 var lnum = 1
Bram Moolenaar851ee6c2017-11-09 20:46:17 +01001282 while lnum <= 5 && lnum < line('$')
1283 if getline(lnum) =~ "^\ctemplate-type:"
1284 setf redif
1285 return
1286 endif
Bram Moolenaara2baa732022-02-04 16:09:54 +00001287 lnum += 1
Bram Moolenaar851ee6c2017-11-09 20:46:17 +01001288 endwhile
Bram Moolenaara2baa732022-02-04 16:09:54 +00001289enddef
Bram Moolenaar851ee6c2017-11-09 20:46:17 +01001290
Bram Moolenaara2baa732022-02-04 16:09:54 +00001291# This function is called for all files under */debian/patches/*, make sure not
1292# to non-dep3patch files, such as README and other text files.
1293export def Dep3patch()
James McCoy647ab4c2021-12-17 20:52:57 +00001294 if expand('%:t') ==# 'series'
1295 return
1296 endif
1297
1298 for ln in getline(1, 100)
1299 if ln =~# '^\%(Description\|Subject\|Origin\|Bug\|Forwarded\|Author\|From\|Reviewed-by\|Acked-by\|Last-Updated\|Applied-Upstream\):'
1300 setf dep3patch
1301 return
1302 elseif ln =~# '^---'
Bram Moolenaara2baa732022-02-04 16:09:54 +00001303 # end of headers found. stop processing
James McCoy647ab4c2021-12-17 20:52:57 +00001304 return
1305 endif
1306 endfor
Bram Moolenaara2baa732022-02-04 16:09:54 +00001307enddef
Bram Moolenaar851ee6c2017-11-09 20:46:17 +01001308
Bram Moolenaara2baa732022-02-04 16:09:54 +00001309# This function checks the first 15 lines for appearance of 'FoamFile'
1310# and then 'object' in a following line.
1311# In that case, it's probably an OpenFOAM file
1312export def FTfoam()
1313 var ffile = 0
1314 var lnum = 1
Elwardi2284f6c2022-01-11 18:14:23 +00001315 while lnum <= 15
1316 if getline(lnum) =~# '^FoamFile'
Bram Moolenaara2baa732022-02-04 16:09:54 +00001317 ffile = 1
Elwardi2284f6c2022-01-11 18:14:23 +00001318 elseif ffile == 1 && getline(lnum) =~# '^\s*object'
1319 setf foam
1320 return
1321 endif
Bram Moolenaara2baa732022-02-04 16:09:54 +00001322 lnum += 1
Elwardi2284f6c2022-01-11 18:14:23 +00001323 endwhile
Bram Moolenaara2baa732022-02-04 16:09:54 +00001324enddef
Elwardi2284f6c2022-01-11 18:14:23 +00001325
Bram Moolenaara2baa732022-02-04 16:09:54 +00001326# Determine if a *.tf file is TF mud client or terraform
1327export def FTtf()
1328 var numberOfLines = line('$')
=?UTF-8?q?Dundar=20G=C3=B6c?=bd8168c2022-01-28 14:15:09 +00001329 for i in range(1, numberOfLines)
Bram Moolenaara2baa732022-02-04 16:09:54 +00001330 var currentLine = trim(getline(i))
1331 var firstCharacter = currentLine[0]
=?UTF-8?q?Dundar=20G=C3=B6c?=bd8168c2022-01-28 14:15:09 +00001332 if firstCharacter !=? ";" && firstCharacter !=? "/" && firstCharacter !=? ""
1333 setf terraform
1334 return
1335 endif
1336 endfor
1337 setf tf
Bram Moolenaara2baa732022-02-04 16:09:54 +00001338enddef
=?UTF-8?q?Dundar=20G=C3=B6c?=bd8168c2022-01-28 14:15:09 +00001339
KnoP-0193c7a452022-04-16 21:14:04 +01001340var ft_krl_header = '\&\w+'
Bram Moolenaar3ad20902022-04-06 18:57:39 +01001341# Determine if a *.src file is Kuka Robot Language
1342export def FTsrc()
KnoP-0193c7a452022-04-16 21:14:04 +01001343 var ft_krl_def_or_deffct = '%(global\s+)?def%(fct)?>'
Bram Moolenaar3ad20902022-04-06 18:57:39 +01001344 if exists("g:filetype_src")
1345 exe "setf " .. g:filetype_src
KnoP-0193c7a452022-04-16 21:14:04 +01001346 elseif getline(nextnonblank(1)) =~? '\v^\s*%(' .. ft_krl_header .. '|' .. ft_krl_def_or_deffct .. ')'
Bram Moolenaar3ad20902022-04-06 18:57:39 +01001347 setf krl
1348 endif
1349enddef
1350
1351# Determine if a *.dat file is Kuka Robot Language
1352export def FTdat()
KnoP-0193c7a452022-04-16 21:14:04 +01001353 var ft_krl_defdat = 'defdat>'
Bram Moolenaar3ad20902022-04-06 18:57:39 +01001354 if exists("g:filetype_dat")
1355 exe "setf " .. g:filetype_dat
KnoP-0193c7a452022-04-16 21:14:04 +01001356 elseif getline(nextnonblank(1)) =~? '\v^\s*%(' .. ft_krl_header .. '|' .. ft_krl_defdat .. ')'
Bram Moolenaar3ad20902022-04-06 18:57:39 +01001357 setf krl
1358 endif
1359enddef
=?UTF-8?q?Dundar=20G=C3=B6c?=bd8168c2022-01-28 14:15:09 +00001360
Doug Kearns4ac8e792022-10-17 13:32:17 +01001361export def FTlsl()
1362 if exists("g:filetype_lsl")
1363 exe "setf " .. g:filetype_lsl
1364 endif
1365
1366 var line = getline(nextnonblank(1))
1367 if line =~ '^\s*%' || line =~# ':\s*trait\s*$'
1368 setf larch
1369 else
1370 setf lsl
1371 endif
1372enddef
1373
Gaetan Lepage4ce1bda2023-05-10 22:01:55 +01001374export def FTtyp()
1375 if exists("g:filetype_typ")
1376 exe "setf " .. g:filetype_typ
1377 return
1378 endif
1379
1380 # Look for SQL type definition syntax
1381 for line in getline(1, 200)
1382 # SQL type files may define the casing
1383 if line =~ '^CASE\s\==\s\=\(SAME\|LOWER\|UPPER\|OPPOSITE\)$'
1384 setf sql
1385 return
1386 endif
1387
1388 # SQL type files may define some types as follows
1389 if line =~ '^TYPE\s.*$'
1390 setf sql
1391 return
1392 endif
1393 endfor
1394
1395 # Otherwise, affect the typst filetype
1396 setf typst
1397enddef
1398
PowerUser64aa61b8a2024-06-19 20:32:11 +02001399# Detect Microsoft Developer Studio Project files (Makefile) or Faust DSP
1400# files.
1401export def FTdsp()
1402 if exists("g:filetype_dsp")
1403 exe "setf " .. g:filetype_dsp
1404 return
1405 endif
1406
1407 # Test the filename
1408 if expand('%:t') =~ '^[mM]akefile.*$'
1409 setf make
1410 return
1411 endif
1412
1413 # Test the file contents
1414 for line in getline(1, 200)
1415 # Chech for comment style
1416 if line =~ '^#.*'
1417 setf make
1418 return
1419 endif
1420
1421 # Check for common lines
1422 if line =~ '^.*Microsoft Developer Studio Project File.*$'
1423 setf make
1424 return
1425 endif
1426
1427 if line =~ '^!MESSAGE This is not a valid makefile\..+$'
1428 setf make
1429 return
1430 endif
1431
1432 # Check for keywords
1433 if line =~ '^!(IF,ELSEIF,ENDIF).*$'
1434 setf make
1435 return
1436 endif
1437
1438 # Check for common assignments
1439 if line =~ '^SOURCE=.*$'
1440 setf make
1441 return
1442 endif
1443 endfor
1444
1445 # Otherwise, assume we have a Faust file
1446 setf faust
1447enddef
1448
Christian Brabandt99181202025-01-25 14:54:28 +01001449# Set the filetype of a *.v file to Verilog, V or Cog based on the first 500
Turiiya80406c22023-04-22 21:38:47 +01001450# lines.
1451export def FTv()
1452 if did_filetype()
1453 # ":setf" will do nothing, bail out early
1454 return
1455 endif
Christian Brabandt10b4f752024-01-01 19:19:20 +01001456 if exists("g:filetype_v")
1457 exe "setf " .. g:filetype_v
1458 return
1459 endif
Turiiya80406c22023-04-22 21:38:47 +01001460
Christian Brabandt10b4f752024-01-01 19:19:20 +01001461 var in_comment = 0
Christian Brabandt99181202025-01-25 14:54:28 +01001462 for lnum in range(1, min([line("$"), 500]))
Christian Brabandt10b4f752024-01-01 19:19:20 +01001463 var line = getline(lnum)
1464 # Skip Verilog and V comments (lines and blocks).
1465 if line =~ '^\s*/\*'
1466 # start comment block
1467 in_comment = 1
1468 endif
1469 if in_comment == 1
1470 if line =~ '\*/'
1471 # end comment block
1472 in_comment = 0
1473 endif
1474 # skip comment-block line
1475 continue
1476 endif
1477 if line =~ '^\s*//'
Turiiya80406c22023-04-22 21:38:47 +01001478 # skip comment line
1479 continue
1480 endif
1481
Christian Brabandt10b4f752024-01-01 19:19:20 +01001482 # Coq: line ends with a '.' followed by an optional variable number of
1483 # spaces or contains the start of a comment, but not inside a Verilog or V
1484 # comment.
1485 # Example: "Definition x := 10. (*".
1486 if (line =~ '\.\s*$' && line !~ '/[/*]') || (line =~ '(\*' && line !~ '/[/*].*(\*')
1487 setf coq
1488 return
1489 endif
1490
Turiiya80406c22023-04-22 21:38:47 +01001491 # Verilog: line ends with ';' followed by an optional variable number of
1492 # spaces and an optional start of a comment.
1493 # Example: " b <= a + 1; // Add 1".
Christian Brabandtfb49e3c2025-01-25 16:18:51 +01001494 # Alternatively: a module is defined: " module MyModule ( input )"
1495 if line =~ ';\s*\(/[/*].*\)\?$' || line =~ '\C^\s*module\s\+\w\+\s*('
Turiiya80406c22023-04-22 21:38:47 +01001496 setf verilog
1497 return
1498 endif
Turiiya80406c22023-04-22 21:38:47 +01001499 endfor
1500
1501 # No line matched, fall back to "v".
1502 setf v
1503enddef
1504
Doug Kearnsf97f6bb2023-08-27 18:44:09 +02001505export def FTvba()
1506 if getline(1) =~ '^["#] Vimball Archiver'
1507 setf vim
1508 else
1509 setf vb
1510 endif
1511enddef
1512
Colin Caine4b3fab12024-04-18 23:53:02 +02001513export def Detect_UCI_statements(): bool
1514 # Match a config or package statement at the start of the line.
1515 const config_or_package_statement = '^\s*\(\(c\|config\)\|\(p\|package\)\)\s\+\S'
1516 # Match a line that is either all blank or blank followed by a comment
1517 const comment_or_blank = '^\s*\(#.*\)\?$'
1518
1519 # Return true iff the file has a config or package statement near the
1520 # top of the file and all preceding lines were comments or blank.
1521 return getline(1) =~# config_or_package_statement
1522 \ || getline(1) =~# comment_or_blank
1523 \ && ( getline(2) =~# config_or_package_statement
1524 \ || getline(2) =~# comment_or_blank
1525 \ && getline(3) =~# config_or_package_statement
1526 \ )
1527enddef
1528
Bram Moolenaara2baa732022-02-04 16:09:54 +00001529# Uncomment this line to check for compilation errors early
dkearnsef387c02024-02-20 06:58:30 +11001530# defcompile