Bram Moolenaar | 851ee6c | 2017-11-09 20:46:17 +0100 | [diff] [blame] | 1 | " Vim functions for file type detection |
| 2 | " |
| 3 | " Maintainer: Bram Moolenaar <Bram@vim.org> |
Bram Moolenaar | 207f009 | 2020-08-30 17:20:20 +0200 | [diff] [blame] | 4 | " Last Change: 2020 Aug 17 |
Bram Moolenaar | 851ee6c | 2017-11-09 20:46:17 +0100 | [diff] [blame] | 5 | |
| 6 | " These functions are moved here from runtime/filetype.vim to make startup |
| 7 | " faster. |
| 8 | |
| 9 | " Line continuation is used here, remove 'C' from 'cpoptions' |
| 10 | let s:cpo_save = &cpo |
| 11 | set cpo&vim |
| 12 | |
Bram Moolenaar | d09a206 | 2017-11-11 15:37:45 +0100 | [diff] [blame] | 13 | func dist#ft#Check_inp() |
Bram Moolenaar | 851ee6c | 2017-11-09 20:46:17 +0100 | [diff] [blame] | 14 | if getline(1) =~ '^\*' |
| 15 | setf abaqus |
| 16 | else |
| 17 | let n = 1 |
| 18 | if line("$") > 500 |
| 19 | let nmax = 500 |
| 20 | else |
| 21 | let nmax = line("$") |
| 22 | endif |
| 23 | while n <= nmax |
| 24 | if getline(n) =~? "^header surface data" |
| 25 | setf trasys |
| 26 | break |
| 27 | endif |
| 28 | let n = n + 1 |
| 29 | endwhile |
| 30 | endif |
| 31 | endfunc |
| 32 | |
| 33 | " This function checks for the kind of assembly that is wanted by the user, or |
| 34 | " can be detected from the first five lines of the file. |
Bram Moolenaar | d09a206 | 2017-11-11 15:37:45 +0100 | [diff] [blame] | 35 | func dist#ft#FTasm() |
Bram Moolenaar | 851ee6c | 2017-11-09 20:46:17 +0100 | [diff] [blame] | 36 | " make sure b:asmsyntax exists |
| 37 | if !exists("b:asmsyntax") |
| 38 | let b:asmsyntax = "" |
| 39 | endif |
| 40 | |
| 41 | if b:asmsyntax == "" |
Bram Moolenaar | d09a206 | 2017-11-11 15:37:45 +0100 | [diff] [blame] | 42 | call dist#ft#FTasmsyntax() |
Bram Moolenaar | 851ee6c | 2017-11-09 20:46:17 +0100 | [diff] [blame] | 43 | endif |
| 44 | |
| 45 | " if b:asmsyntax still isn't set, default to asmsyntax or GNU |
| 46 | if b:asmsyntax == "" |
| 47 | if exists("g:asmsyntax") |
| 48 | let b:asmsyntax = g:asmsyntax |
| 49 | else |
| 50 | let b:asmsyntax = "asm" |
| 51 | endif |
| 52 | endif |
| 53 | |
| 54 | exe "setf " . fnameescape(b:asmsyntax) |
| 55 | endfunc |
| 56 | |
Bram Moolenaar | d09a206 | 2017-11-11 15:37:45 +0100 | [diff] [blame] | 57 | func dist#ft#FTasmsyntax() |
Bram Moolenaar | 851ee6c | 2017-11-09 20:46:17 +0100 | [diff] [blame] | 58 | " see if file contains any asmsyntax=foo overrides. If so, change |
| 59 | " b:asmsyntax appropriately |
| 60 | let head = " ".getline(1)." ".getline(2)." ".getline(3)." ".getline(4). |
| 61 | \" ".getline(5)." " |
| 62 | let match = matchstr(head, '\sasmsyntax=\zs[a-zA-Z0-9]\+\ze\s') |
| 63 | if match != '' |
| 64 | let b:asmsyntax = match |
| 65 | elseif ((head =~? '\.title') || (head =~? '\.ident') || (head =~? '\.macro') || (head =~? '\.subtitle') || (head =~? '\.library')) |
| 66 | let b:asmsyntax = "vmasm" |
| 67 | endif |
| 68 | endfunc |
| 69 | |
| 70 | " Check if one of the first five lines contains "VB_Name". In that case it is |
| 71 | " probably a Visual Basic file. Otherwise it's assumed to be "alt" filetype. |
Bram Moolenaar | d09a206 | 2017-11-11 15:37:45 +0100 | [diff] [blame] | 72 | func dist#ft#FTVB(alt) |
Bram Moolenaar | 851ee6c | 2017-11-09 20:46:17 +0100 | [diff] [blame] | 73 | if getline(1).getline(2).getline(3).getline(4).getline(5) =~? 'VB_Name\|Begin VB\.\(Form\|MDIForm\|UserControl\)' |
| 74 | setf vb |
| 75 | else |
| 76 | exe "setf " . a:alt |
| 77 | endif |
| 78 | endfunc |
| 79 | |
Bram Moolenaar | d09a206 | 2017-11-11 15:37:45 +0100 | [diff] [blame] | 80 | func dist#ft#FTbtm() |
Bram Moolenaar | 851ee6c | 2017-11-09 20:46:17 +0100 | [diff] [blame] | 81 | if exists("g:dosbatch_syntax_for_btm") && g:dosbatch_syntax_for_btm |
| 82 | setf dosbatch |
| 83 | else |
| 84 | setf btm |
| 85 | endif |
| 86 | endfunc |
| 87 | |
Bram Moolenaar | d09a206 | 2017-11-11 15:37:45 +0100 | [diff] [blame] | 88 | func dist#ft#BindzoneCheck(default) |
Bram Moolenaar | 851ee6c | 2017-11-09 20:46:17 +0100 | [diff] [blame] | 89 | if getline(1).getline(2).getline(3).getline(4) =~ '^; <<>> DiG [0-9.]\+.* <<>>\|$ORIGIN\|$TTL\|IN\s\+SOA' |
| 90 | setf bindzone |
| 91 | elseif a:default != '' |
| 92 | exe 'setf ' . a:default |
| 93 | endif |
| 94 | endfunc |
| 95 | |
Bram Moolenaar | d09a206 | 2017-11-11 15:37:45 +0100 | [diff] [blame] | 96 | func dist#ft#FTlpc() |
Bram Moolenaar | 851ee6c | 2017-11-09 20:46:17 +0100 | [diff] [blame] | 97 | if exists("g:lpc_syntax_for_c") |
| 98 | let lnum = 1 |
| 99 | while lnum <= 12 |
| 100 | if getline(lnum) =~# '^\(//\|inherit\|private\|protected\|nosave\|string\|object\|mapping\|mixed\)' |
| 101 | setf lpc |
| 102 | return |
| 103 | endif |
| 104 | let lnum = lnum + 1 |
| 105 | endwhile |
| 106 | endif |
| 107 | setf c |
| 108 | endfunc |
| 109 | |
Bram Moolenaar | d09a206 | 2017-11-11 15:37:45 +0100 | [diff] [blame] | 110 | func dist#ft#FTheader() |
Bram Moolenaar | 851ee6c | 2017-11-09 20:46:17 +0100 | [diff] [blame] | 111 | if match(getline(1, min([line("$"), 200])), '^@\(interface\|end\|class\)') > -1 |
| 112 | if exists("g:c_syntax_for_h") |
| 113 | setf objc |
| 114 | else |
| 115 | setf objcpp |
| 116 | endif |
| 117 | elseif exists("g:c_syntax_for_h") |
| 118 | setf c |
| 119 | elseif exists("g:ch_syntax_for_h") |
| 120 | setf ch |
| 121 | else |
| 122 | setf cpp |
| 123 | endif |
| 124 | endfunc |
| 125 | |
| 126 | " This function checks if one of the first ten lines start with a '@'. In |
| 127 | " that case it is probably a change file. |
| 128 | " If the first line starts with # or ! it's probably a ch file. |
Bram Moolenaar | ba3ff53 | 2018-11-04 14:45:49 +0100 | [diff] [blame] | 129 | " If a line has "main", "include", "//" or "/*" it's probably ch. |
Bram Moolenaar | 851ee6c | 2017-11-09 20:46:17 +0100 | [diff] [blame] | 130 | " Otherwise CHILL is assumed. |
Bram Moolenaar | d09a206 | 2017-11-11 15:37:45 +0100 | [diff] [blame] | 131 | func dist#ft#FTchange() |
Bram Moolenaar | 851ee6c | 2017-11-09 20:46:17 +0100 | [diff] [blame] | 132 | let lnum = 1 |
| 133 | while lnum <= 10 |
| 134 | if getline(lnum)[0] == '@' |
| 135 | setf change |
| 136 | return |
| 137 | endif |
| 138 | if lnum == 1 && (getline(1)[0] == '#' || getline(1)[0] == '!') |
| 139 | setf ch |
| 140 | return |
| 141 | endif |
| 142 | if getline(lnum) =~ "MODULE" |
| 143 | setf chill |
| 144 | return |
| 145 | endif |
| 146 | if getline(lnum) =~ 'main\s*(\|#\s*include\|//' |
| 147 | setf ch |
| 148 | return |
| 149 | endif |
| 150 | let lnum = lnum + 1 |
| 151 | endwhile |
| 152 | setf chill |
| 153 | endfunc |
| 154 | |
Bram Moolenaar | d09a206 | 2017-11-11 15:37:45 +0100 | [diff] [blame] | 155 | func dist#ft#FTent() |
Bram Moolenaar | 851ee6c | 2017-11-09 20:46:17 +0100 | [diff] [blame] | 156 | " This function checks for valid cl syntax in the first five lines. |
| 157 | " Look for either an opening comment, '#', or a block start, '{". |
| 158 | " If not found, assume SGML. |
| 159 | let lnum = 1 |
| 160 | while lnum < 6 |
| 161 | let line = getline(lnum) |
| 162 | if line =~ '^\s*[#{]' |
| 163 | setf cl |
| 164 | return |
| 165 | elseif line !~ '^\s*$' |
| 166 | " Not a blank line, not a comment, and not a block start, |
| 167 | " so doesn't look like valid cl code. |
| 168 | break |
| 169 | endif |
| 170 | let lnum = lnum + 1 |
| 171 | endw |
| 172 | setf dtd |
| 173 | endfunc |
| 174 | |
Austin Gatlin | f3caeb6 | 2021-06-26 12:02:55 +0200 | [diff] [blame] | 175 | func dist#ft#ExCheck() |
| 176 | let lines = getline(1, min([line("$"), 100])) |
| 177 | if exists('g:filetype_euphoria') |
| 178 | exe 'setf ' . g:filetype_euphoria |
| 179 | elseif match(lines, '^--\|^ifdef\>\|^include\>') > -1 |
| 180 | setf euphoria3 |
| 181 | else |
| 182 | setf elixir |
| 183 | endif |
| 184 | endfunc |
| 185 | |
Bram Moolenaar | d09a206 | 2017-11-11 15:37:45 +0100 | [diff] [blame] | 186 | func dist#ft#EuphoriaCheck() |
Bram Moolenaar | 851ee6c | 2017-11-09 20:46:17 +0100 | [diff] [blame] | 187 | if exists('g:filetype_euphoria') |
| 188 | exe 'setf ' . g:filetype_euphoria |
| 189 | else |
| 190 | setf euphoria3 |
| 191 | endif |
| 192 | endfunc |
| 193 | |
Bram Moolenaar | d09a206 | 2017-11-11 15:37:45 +0100 | [diff] [blame] | 194 | func dist#ft#DtraceCheck() |
Bram Moolenaar | 851ee6c | 2017-11-09 20:46:17 +0100 | [diff] [blame] | 195 | let lines = getline(1, min([line("$"), 100])) |
| 196 | if match(lines, '^module\>\|^import\>') > -1 |
| 197 | " D files often start with a module and/or import statement. |
| 198 | setf d |
| 199 | elseif match(lines, '^#!\S\+dtrace\|#pragma\s\+D\s\+option\|:\S\{-}:\S\{-}:') > -1 |
| 200 | setf dtrace |
| 201 | else |
| 202 | setf d |
| 203 | endif |
| 204 | endfunc |
| 205 | |
Bram Moolenaar | d09a206 | 2017-11-11 15:37:45 +0100 | [diff] [blame] | 206 | func dist#ft#FTe() |
Bram Moolenaar | 851ee6c | 2017-11-09 20:46:17 +0100 | [diff] [blame] | 207 | if exists('g:filetype_euphoria') |
| 208 | exe 'setf ' . g:filetype_euphoria |
| 209 | else |
| 210 | let n = 1 |
Bram Moolenaar | 493fbe4 | 2019-03-17 17:16:12 +0100 | [diff] [blame] | 211 | while n < 100 && n <= line("$") |
Bram Moolenaar | 851ee6c | 2017-11-09 20:46:17 +0100 | [diff] [blame] | 212 | if getline(n) =~ "^\\s*\\(<'\\|'>\\)\\s*$" |
| 213 | setf specman |
| 214 | return |
| 215 | endif |
| 216 | let n = n + 1 |
| 217 | endwhile |
| 218 | setf eiffel |
| 219 | endif |
| 220 | endfunc |
| 221 | |
| 222 | " Distinguish between HTML, XHTML and Django |
Bram Moolenaar | d09a206 | 2017-11-11 15:37:45 +0100 | [diff] [blame] | 223 | func dist#ft#FThtml() |
Bram Moolenaar | 851ee6c | 2017-11-09 20:46:17 +0100 | [diff] [blame] | 224 | let n = 1 |
Bram Moolenaar | 493fbe4 | 2019-03-17 17:16:12 +0100 | [diff] [blame] | 225 | while n < 10 && n <= line("$") |
Bram Moolenaar | 851ee6c | 2017-11-09 20:46:17 +0100 | [diff] [blame] | 226 | if getline(n) =~ '\<DTD\s\+XHTML\s' |
| 227 | setf xhtml |
| 228 | return |
| 229 | endif |
| 230 | if getline(n) =~ '{%\s*\(extends\|block\|load\)\>\|{#\s\+' |
| 231 | setf htmldjango |
| 232 | return |
| 233 | endif |
| 234 | let n = n + 1 |
| 235 | endwhile |
Bram Moolenaar | 493fbe4 | 2019-03-17 17:16:12 +0100 | [diff] [blame] | 236 | setf FALLBACK html |
Bram Moolenaar | 851ee6c | 2017-11-09 20:46:17 +0100 | [diff] [blame] | 237 | endfunc |
| 238 | |
| 239 | " Distinguish between standard IDL and MS-IDL |
Bram Moolenaar | d09a206 | 2017-11-11 15:37:45 +0100 | [diff] [blame] | 240 | func dist#ft#FTidl() |
Bram Moolenaar | 851ee6c | 2017-11-09 20:46:17 +0100 | [diff] [blame] | 241 | let n = 1 |
Bram Moolenaar | 493fbe4 | 2019-03-17 17:16:12 +0100 | [diff] [blame] | 242 | while n < 50 && n <= line("$") |
Bram Moolenaar | 851ee6c | 2017-11-09 20:46:17 +0100 | [diff] [blame] | 243 | if getline(n) =~ '^\s*import\s\+"\(unknwn\|objidl\)\.idl"' |
| 244 | setf msidl |
| 245 | return |
| 246 | endif |
| 247 | let n = n + 1 |
| 248 | endwhile |
| 249 | setf idl |
| 250 | endfunc |
| 251 | |
| 252 | " Distinguish between "default" and Cproto prototype file. */ |
Bram Moolenaar | d09a206 | 2017-11-11 15:37:45 +0100 | [diff] [blame] | 253 | func dist#ft#ProtoCheck(default) |
Bram Moolenaar | 851ee6c | 2017-11-09 20:46:17 +0100 | [diff] [blame] | 254 | " Cproto files have a comment in the first line and a function prototype in |
| 255 | " the second line, it always ends in ";". Indent files may also have |
| 256 | " comments, thus we can't match comments to see the difference. |
| 257 | " IDL files can have a single ';' in the second line, require at least one |
| 258 | " chacter before the ';'. |
| 259 | if getline(2) =~ '.;$' |
| 260 | setf cpp |
| 261 | else |
| 262 | exe 'setf ' . a:default |
| 263 | endif |
| 264 | endfunc |
| 265 | |
Bram Moolenaar | d09a206 | 2017-11-11 15:37:45 +0100 | [diff] [blame] | 266 | func dist#ft#FTm() |
Bram Moolenaar | deba5eb | 2021-09-03 19:21:36 +0200 | [diff] [blame] | 267 | if exists("g:filetype_m") |
| 268 | exe "setf " . g:filetype_m |
| 269 | return |
| 270 | endif |
| 271 | |
Bram Moolenaar | ca0627d | 2021-09-12 17:03:08 +0200 | [diff] [blame] | 272 | " excluding end(for|function|if|switch|while) common to Murphi |
| 273 | let octave_block_terminators = '\<end\%(_try_catch\|classdef\|enumeration\|events\|methods\|parfor\|properties\)\>' |
Bram Moolenaar | deba5eb | 2021-09-03 19:21:36 +0200 | [diff] [blame] | 274 | |
Doug Kearns | 7329cfa | 2021-11-26 13:01:41 +0000 | [diff] [blame] | 275 | let objc_preprocessor = '^\s*#\s*\%(import\|include\|define\|if\|ifn\=def\|undef\|line\|error\|pragma\)\>' |
| 276 | |
Bram Moolenaar | 851ee6c | 2017-11-09 20:46:17 +0100 | [diff] [blame] | 277 | let n = 1 |
| 278 | let saw_comment = 0 " Whether we've seen a multiline comment leader. |
| 279 | while n < 100 |
| 280 | let line = getline(n) |
| 281 | if line =~ '^\s*/\*' |
| 282 | " /* ... */ is a comment in Objective C and Murphi, so we can't conclude |
| 283 | " it's either of them yet, but track this as a hint in case we don't see |
| 284 | " anything more definitive. |
| 285 | let saw_comment = 1 |
| 286 | endif |
Doug Kearns | 7329cfa | 2021-11-26 13:01:41 +0000 | [diff] [blame] | 287 | if line =~ '^\s*//' || line =~ '^\s*@import\>' || line =~ objc_preprocessor |
Bram Moolenaar | 851ee6c | 2017-11-09 20:46:17 +0100 | [diff] [blame] | 288 | setf objc |
| 289 | return |
| 290 | endif |
Bram Moolenaar | ca0627d | 2021-09-12 17:03:08 +0200 | [diff] [blame] | 291 | if line =~ '^\s*\%(#\|%!\)' || line =~ '^\s*unwind_protect\>' || |
Bram Moolenaar | deba5eb | 2021-09-03 19:21:36 +0200 | [diff] [blame] | 292 | \ line =~ '\%(^\|;\)\s*' .. octave_block_terminators |
| 293 | setf octave |
| 294 | return |
| 295 | endif |
| 296 | " TODO: could be Matlab or Octave |
Bram Moolenaar | 851ee6c | 2017-11-09 20:46:17 +0100 | [diff] [blame] | 297 | if line =~ '^\s*%' |
| 298 | setf matlab |
| 299 | return |
| 300 | endif |
| 301 | if line =~ '^\s*(\*' |
| 302 | setf mma |
| 303 | return |
| 304 | endif |
| 305 | if line =~ '^\c\s*\(\(type\|var\)\>\|--\)' |
| 306 | setf murphi |
| 307 | return |
| 308 | endif |
| 309 | let n = n + 1 |
| 310 | endwhile |
| 311 | |
| 312 | if saw_comment |
| 313 | " We didn't see anything definitive, but this looks like either Objective C |
| 314 | " or Murphi based on the comment leader. Assume the former as it is more |
| 315 | " common. |
| 316 | setf objc |
Bram Moolenaar | 851ee6c | 2017-11-09 20:46:17 +0100 | [diff] [blame] | 317 | else |
Bram Moolenaar | deba5eb | 2021-09-03 19:21:36 +0200 | [diff] [blame] | 318 | " Default is Matlab |
Bram Moolenaar | 851ee6c | 2017-11-09 20:46:17 +0100 | [diff] [blame] | 319 | setf matlab |
| 320 | endif |
| 321 | endfunc |
| 322 | |
Bram Moolenaar | d09a206 | 2017-11-11 15:37:45 +0100 | [diff] [blame] | 323 | func dist#ft#FTmms() |
Bram Moolenaar | 851ee6c | 2017-11-09 20:46:17 +0100 | [diff] [blame] | 324 | let n = 1 |
Bram Moolenaar | d7df279 | 2020-01-02 21:34:42 +0100 | [diff] [blame] | 325 | while n < 20 |
Bram Moolenaar | 851ee6c | 2017-11-09 20:46:17 +0100 | [diff] [blame] | 326 | let line = getline(n) |
| 327 | if line =~ '^\s*\(%\|//\)' || line =~ '^\*' |
| 328 | setf mmix |
| 329 | return |
| 330 | endif |
| 331 | if line =~ '^\s*#' |
| 332 | setf make |
| 333 | return |
| 334 | endif |
| 335 | let n = n + 1 |
| 336 | endwhile |
| 337 | setf mmix |
| 338 | endfunc |
| 339 | |
| 340 | " This function checks if one of the first five lines start with a dot. In |
| 341 | " that case it is probably an nroff file: 'filetype' is set and 1 is returned. |
Bram Moolenaar | d09a206 | 2017-11-11 15:37:45 +0100 | [diff] [blame] | 342 | func dist#ft#FTnroff() |
Bram Moolenaar | 851ee6c | 2017-11-09 20:46:17 +0100 | [diff] [blame] | 343 | if getline(1)[0] . getline(2)[0] . getline(3)[0] . getline(4)[0] . getline(5)[0] =~ '\.' |
| 344 | setf nroff |
| 345 | return 1 |
| 346 | endif |
| 347 | return 0 |
| 348 | endfunc |
| 349 | |
Bram Moolenaar | d09a206 | 2017-11-11 15:37:45 +0100 | [diff] [blame] | 350 | func dist#ft#FTmm() |
Bram Moolenaar | 851ee6c | 2017-11-09 20:46:17 +0100 | [diff] [blame] | 351 | let n = 1 |
Bram Moolenaar | d1caa94 | 2020-04-10 22:10:56 +0200 | [diff] [blame] | 352 | while n < 20 |
Bram Moolenaar | 851ee6c | 2017-11-09 20:46:17 +0100 | [diff] [blame] | 353 | let line = getline(n) |
| 354 | if line =~ '^\s*\(#\s*\(include\|import\)\>\|@import\>\|/\*\)' |
| 355 | setf objcpp |
| 356 | return |
| 357 | endif |
| 358 | let n = n + 1 |
| 359 | endwhile |
| 360 | setf nroff |
| 361 | endfunc |
| 362 | |
Bram Moolenaar | d09a206 | 2017-11-11 15:37:45 +0100 | [diff] [blame] | 363 | func dist#ft#FTpl() |
Bram Moolenaar | 851ee6c | 2017-11-09 20:46:17 +0100 | [diff] [blame] | 364 | if exists("g:filetype_pl") |
| 365 | exe "setf " . g:filetype_pl |
| 366 | else |
| 367 | " recognize Prolog by specific text in the first non-empty line |
| 368 | " require a blank after the '%' because Perl uses "%list" and "%translate" |
| 369 | let l = getline(nextnonblank(1)) |
| 370 | if l =~ '\<prolog\>' || l =~ '^\s*\(%\+\(\s\|$\)\|/\*\)' || l =~ ':-' |
| 371 | setf prolog |
| 372 | else |
| 373 | setf perl |
| 374 | endif |
| 375 | endif |
| 376 | endfunc |
| 377 | |
Bram Moolenaar | d09a206 | 2017-11-11 15:37:45 +0100 | [diff] [blame] | 378 | func dist#ft#FTinc() |
Bram Moolenaar | 851ee6c | 2017-11-09 20:46:17 +0100 | [diff] [blame] | 379 | if exists("g:filetype_inc") |
| 380 | exe "setf " . g:filetype_inc |
| 381 | else |
| 382 | let lines = getline(1).getline(2).getline(3) |
| 383 | if lines =~? "perlscript" |
| 384 | setf aspperl |
| 385 | elseif lines =~ "<%" |
| 386 | setf aspvbs |
| 387 | elseif lines =~ "<?" |
| 388 | setf php |
Bram Moolenaar | a0122dc | 2021-01-12 17:42:24 +0100 | [diff] [blame] | 389 | " Pascal supports // comments but they're vary rarely used for file |
| 390 | " headers so assume POV-Ray |
| 391 | elseif lines =~ '^\s*\%({\|(\*\)' || lines =~? s:ft_pascal_keywords |
| 392 | setf pascal |
Bram Moolenaar | 851ee6c | 2017-11-09 20:46:17 +0100 | [diff] [blame] | 393 | else |
Bram Moolenaar | d09a206 | 2017-11-11 15:37:45 +0100 | [diff] [blame] | 394 | call dist#ft#FTasmsyntax() |
Bram Moolenaar | 851ee6c | 2017-11-09 20:46:17 +0100 | [diff] [blame] | 395 | if exists("b:asmsyntax") |
| 396 | exe "setf " . fnameescape(b:asmsyntax) |
| 397 | else |
| 398 | setf pov |
| 399 | endif |
| 400 | endif |
| 401 | endif |
| 402 | endfunc |
| 403 | |
Bram Moolenaar | d09a206 | 2017-11-11 15:37:45 +0100 | [diff] [blame] | 404 | func dist#ft#FTprogress_cweb() |
Bram Moolenaar | 851ee6c | 2017-11-09 20:46:17 +0100 | [diff] [blame] | 405 | if exists("g:filetype_w") |
| 406 | exe "setf " . g:filetype_w |
| 407 | return |
| 408 | endif |
| 409 | if getline(1) =~ '&ANALYZE' || getline(3) =~ '&GLOBAL-DEFINE' |
| 410 | setf progress |
| 411 | else |
| 412 | setf cweb |
| 413 | endif |
| 414 | endfunc |
| 415 | |
Bram Moolenaar | d09a206 | 2017-11-11 15:37:45 +0100 | [diff] [blame] | 416 | func dist#ft#FTprogress_asm() |
Bram Moolenaar | 851ee6c | 2017-11-09 20:46:17 +0100 | [diff] [blame] | 417 | if exists("g:filetype_i") |
| 418 | exe "setf " . g:filetype_i |
| 419 | return |
| 420 | endif |
| 421 | " This function checks for an assembly comment the first ten lines. |
| 422 | " If not found, assume Progress. |
| 423 | let lnum = 1 |
| 424 | while lnum <= 10 && lnum < line('$') |
| 425 | let line = getline(lnum) |
| 426 | if line =~ '^\s*;' || line =~ '^\*' |
Bram Moolenaar | d09a206 | 2017-11-11 15:37:45 +0100 | [diff] [blame] | 427 | call dist#ft#FTasm() |
Bram Moolenaar | 851ee6c | 2017-11-09 20:46:17 +0100 | [diff] [blame] | 428 | return |
| 429 | elseif line !~ '^\s*$' || line =~ '^/\*' |
| 430 | " Not an empty line: Doesn't look like valid assembly code. |
| 431 | " Or it looks like a Progress /* comment |
| 432 | break |
| 433 | endif |
| 434 | let lnum = lnum + 1 |
| 435 | endw |
| 436 | setf progress |
| 437 | endfunc |
| 438 | |
Bram Moolenaar | a0122dc | 2021-01-12 17:42:24 +0100 | [diff] [blame] | 439 | let s:ft_pascal_comments = '^\s*\%({\|(\*\|//\)' |
| 440 | let s:ft_pascal_keywords = '^\s*\%(program\|unit\|library\|uses\|begin\|procedure\|function\|const\|type\|var\)\>' |
| 441 | |
Bram Moolenaar | d09a206 | 2017-11-11 15:37:45 +0100 | [diff] [blame] | 442 | func dist#ft#FTprogress_pascal() |
Bram Moolenaar | 851ee6c | 2017-11-09 20:46:17 +0100 | [diff] [blame] | 443 | if exists("g:filetype_p") |
| 444 | exe "setf " . g:filetype_p |
| 445 | return |
| 446 | endif |
| 447 | " This function checks for valid Pascal syntax in the first ten lines. |
| 448 | " Look for either an opening comment or a program start. |
| 449 | " If not found, assume Progress. |
| 450 | let lnum = 1 |
| 451 | while lnum <= 10 && lnum < line('$') |
| 452 | let line = getline(lnum) |
Bram Moolenaar | a0122dc | 2021-01-12 17:42:24 +0100 | [diff] [blame] | 453 | if line =~ s:ft_pascal_comments || line =~? s:ft_pascal_keywords |
Bram Moolenaar | 851ee6c | 2017-11-09 20:46:17 +0100 | [diff] [blame] | 454 | setf pascal |
| 455 | return |
| 456 | elseif line !~ '^\s*$' || line =~ '^/\*' |
| 457 | " Not an empty line: Doesn't look like valid Pascal code. |
| 458 | " Or it looks like a Progress /* comment |
| 459 | break |
| 460 | endif |
| 461 | let lnum = lnum + 1 |
| 462 | endw |
| 463 | setf progress |
| 464 | endfunc |
| 465 | |
Bram Moolenaar | a0122dc | 2021-01-12 17:42:24 +0100 | [diff] [blame] | 466 | func dist#ft#FTpp() |
| 467 | if exists("g:filetype_pp") |
| 468 | exe "setf " . g:filetype_pp |
| 469 | else |
| 470 | let line = getline(nextnonblank(1)) |
| 471 | if line =~ s:ft_pascal_comments || line =~? s:ft_pascal_keywords |
| 472 | setf pascal |
| 473 | else |
| 474 | setf puppet |
| 475 | endif |
| 476 | endif |
| 477 | endfunc |
| 478 | |
Bram Moolenaar | d09a206 | 2017-11-11 15:37:45 +0100 | [diff] [blame] | 479 | func dist#ft#FTr() |
Bram Moolenaar | 851ee6c | 2017-11-09 20:46:17 +0100 | [diff] [blame] | 480 | let max = line("$") > 50 ? 50 : line("$") |
| 481 | |
| 482 | for n in range(1, max) |
| 483 | " Rebol is easy to recognize, check for that first |
| 484 | if getline(n) =~? '\<REBOL\>' |
| 485 | setf rebol |
| 486 | return |
| 487 | endif |
| 488 | endfor |
| 489 | |
| 490 | for n in range(1, max) |
| 491 | " R has # comments |
| 492 | if getline(n) =~ '^\s*#' |
| 493 | setf r |
| 494 | return |
| 495 | endif |
| 496 | " Rexx has /* comments */ |
| 497 | if getline(n) =~ '^\s*/\*' |
| 498 | setf rexx |
| 499 | return |
| 500 | endif |
| 501 | endfor |
| 502 | |
| 503 | " Nothing recognized, use user default or assume Rexx |
| 504 | if exists("g:filetype_r") |
| 505 | exe "setf " . g:filetype_r |
| 506 | else |
| 507 | " Rexx used to be the default, but R appears to be much more popular. |
| 508 | setf r |
| 509 | endif |
| 510 | endfunc |
| 511 | |
Bram Moolenaar | d09a206 | 2017-11-11 15:37:45 +0100 | [diff] [blame] | 512 | func dist#ft#McSetf() |
Bram Moolenaar | 851ee6c | 2017-11-09 20:46:17 +0100 | [diff] [blame] | 513 | " Rely on the file to start with a comment. |
| 514 | " MS message text files use ';', Sendmail files use '#' or 'dnl' |
| 515 | for lnum in range(1, min([line("$"), 20])) |
| 516 | let line = getline(lnum) |
| 517 | if line =~ '^\s*\(#\|dnl\)' |
| 518 | setf m4 " Sendmail .mc file |
| 519 | return |
| 520 | elseif line =~ '^\s*;' |
| 521 | setf msmessages " MS Message text file |
| 522 | return |
| 523 | endif |
| 524 | endfor |
| 525 | setf m4 " Default: Sendmail .mc file |
| 526 | endfunc |
| 527 | |
| 528 | " Called from filetype.vim and scripts.vim. |
Bram Moolenaar | d09a206 | 2017-11-11 15:37:45 +0100 | [diff] [blame] | 529 | func dist#ft#SetFileTypeSH(name) |
Bram Moolenaar | 147e7d0 | 2019-01-18 21:46:47 +0100 | [diff] [blame] | 530 | if did_filetype() |
| 531 | " Filetype was already detected |
| 532 | return |
| 533 | endif |
Bram Moolenaar | 851ee6c | 2017-11-09 20:46:17 +0100 | [diff] [blame] | 534 | if expand("<amatch>") =~ g:ft_ignore_pat |
| 535 | return |
| 536 | endif |
| 537 | if a:name =~ '\<csh\>' |
| 538 | " Some .sh scripts contain #!/bin/csh. |
Bram Moolenaar | d09a206 | 2017-11-11 15:37:45 +0100 | [diff] [blame] | 539 | call dist#ft#SetFileTypeShell("csh") |
Bram Moolenaar | 851ee6c | 2017-11-09 20:46:17 +0100 | [diff] [blame] | 540 | return |
| 541 | elseif a:name =~ '\<tcsh\>' |
| 542 | " Some .sh scripts contain #!/bin/tcsh. |
Bram Moolenaar | d09a206 | 2017-11-11 15:37:45 +0100 | [diff] [blame] | 543 | call dist#ft#SetFileTypeShell("tcsh") |
Bram Moolenaar | 851ee6c | 2017-11-09 20:46:17 +0100 | [diff] [blame] | 544 | return |
| 545 | elseif a:name =~ '\<zsh\>' |
| 546 | " Some .sh scripts contain #!/bin/zsh. |
Bram Moolenaar | d09a206 | 2017-11-11 15:37:45 +0100 | [diff] [blame] | 547 | call dist#ft#SetFileTypeShell("zsh") |
Bram Moolenaar | 851ee6c | 2017-11-09 20:46:17 +0100 | [diff] [blame] | 548 | return |
| 549 | elseif a:name =~ '\<ksh\>' |
| 550 | let b:is_kornshell = 1 |
| 551 | if exists("b:is_bash") |
| 552 | unlet b:is_bash |
| 553 | endif |
| 554 | if exists("b:is_sh") |
| 555 | unlet b:is_sh |
| 556 | endif |
| 557 | elseif exists("g:bash_is_sh") || a:name =~ '\<bash\>' || a:name =~ '\<bash2\>' |
| 558 | let b:is_bash = 1 |
| 559 | if exists("b:is_kornshell") |
| 560 | unlet b:is_kornshell |
| 561 | endif |
| 562 | if exists("b:is_sh") |
| 563 | unlet b:is_sh |
| 564 | endif |
| 565 | elseif a:name =~ '\<sh\>' |
| 566 | let b:is_sh = 1 |
| 567 | if exists("b:is_kornshell") |
| 568 | unlet b:is_kornshell |
| 569 | endif |
| 570 | if exists("b:is_bash") |
| 571 | unlet b:is_bash |
| 572 | endif |
| 573 | endif |
Bram Moolenaar | d09a206 | 2017-11-11 15:37:45 +0100 | [diff] [blame] | 574 | call dist#ft#SetFileTypeShell("sh") |
Bram Moolenaar | 851ee6c | 2017-11-09 20:46:17 +0100 | [diff] [blame] | 575 | endfunc |
| 576 | |
| 577 | " For shell-like file types, check for an "exec" command hidden in a comment, |
| 578 | " as used for Tcl. |
| 579 | " Also called from scripts.vim, thus can't be local to this script. |
Bram Moolenaar | d09a206 | 2017-11-11 15:37:45 +0100 | [diff] [blame] | 580 | func dist#ft#SetFileTypeShell(name) |
Bram Moolenaar | 147e7d0 | 2019-01-18 21:46:47 +0100 | [diff] [blame] | 581 | if did_filetype() |
| 582 | " Filetype was already detected |
| 583 | return |
| 584 | endif |
Bram Moolenaar | 851ee6c | 2017-11-09 20:46:17 +0100 | [diff] [blame] | 585 | if expand("<amatch>") =~ g:ft_ignore_pat |
| 586 | return |
| 587 | endif |
| 588 | let l = 2 |
| 589 | while l < 20 && l < line("$") && getline(l) =~ '^\s*\(#\|$\)' |
| 590 | " Skip empty and comment lines. |
| 591 | let l = l + 1 |
| 592 | endwhile |
| 593 | if l < line("$") && getline(l) =~ '\s*exec\s' && getline(l - 1) =~ '^\s*#.*\\$' |
| 594 | " Found an "exec" line after a comment with continuation |
| 595 | let n = substitute(getline(l),'\s*exec\s\+\([^ ]*/\)\=', '', '') |
| 596 | if n =~ '\<tclsh\|\<wish' |
| 597 | setf tcl |
| 598 | return |
| 599 | endif |
| 600 | endif |
| 601 | exe "setf " . a:name |
| 602 | endfunc |
| 603 | |
Bram Moolenaar | d09a206 | 2017-11-11 15:37:45 +0100 | [diff] [blame] | 604 | func dist#ft#CSH() |
Bram Moolenaar | 147e7d0 | 2019-01-18 21:46:47 +0100 | [diff] [blame] | 605 | if did_filetype() |
| 606 | " Filetype was already detected |
| 607 | return |
| 608 | endif |
Bram Moolenaar | 851ee6c | 2017-11-09 20:46:17 +0100 | [diff] [blame] | 609 | if exists("g:filetype_csh") |
Bram Moolenaar | d09a206 | 2017-11-11 15:37:45 +0100 | [diff] [blame] | 610 | call dist#ft#SetFileTypeShell(g:filetype_csh) |
Bram Moolenaar | 851ee6c | 2017-11-09 20:46:17 +0100 | [diff] [blame] | 611 | elseif &shell =~ "tcsh" |
Bram Moolenaar | d09a206 | 2017-11-11 15:37:45 +0100 | [diff] [blame] | 612 | call dist#ft#SetFileTypeShell("tcsh") |
Bram Moolenaar | 851ee6c | 2017-11-09 20:46:17 +0100 | [diff] [blame] | 613 | else |
Bram Moolenaar | d09a206 | 2017-11-11 15:37:45 +0100 | [diff] [blame] | 614 | call dist#ft#SetFileTypeShell("csh") |
Bram Moolenaar | 851ee6c | 2017-11-09 20:46:17 +0100 | [diff] [blame] | 615 | endif |
| 616 | endfunc |
| 617 | |
Bram Moolenaar | cef7322 | 2017-11-09 21:05:31 +0100 | [diff] [blame] | 618 | let s:ft_rules_udev_rules_pattern = '^\s*\cudev_rules\s*=\s*"\([^"]\{-1,}\)/*".*' |
Bram Moolenaar | d09a206 | 2017-11-11 15:37:45 +0100 | [diff] [blame] | 619 | func dist#ft#FTRules() |
Bram Moolenaar | 851ee6c | 2017-11-09 20:46:17 +0100 | [diff] [blame] | 620 | let path = expand('<amatch>:p') |
Bram Moolenaar | aa9675a | 2020-08-17 21:57:09 +0200 | [diff] [blame] | 621 | if path =~ '/\(etc/udev/\%(rules\.d/\)\=.*\.rules\|\%(usr/\)\=lib/udev/\%(rules\.d/\)\=.*\.rules\)$' |
Bram Moolenaar | 851ee6c | 2017-11-09 20:46:17 +0100 | [diff] [blame] | 622 | setf udevrules |
| 623 | return |
| 624 | endif |
| 625 | if path =~ '^/etc/ufw/' |
| 626 | setf conf " Better than hog |
| 627 | return |
| 628 | endif |
| 629 | if path =~ '^/\(etc\|usr/share\)/polkit-1/rules\.d' |
| 630 | setf javascript |
| 631 | return |
| 632 | endif |
| 633 | try |
| 634 | let config_lines = readfile('/etc/udev/udev.conf') |
| 635 | catch /^Vim\%((\a\+)\)\=:E484/ |
| 636 | setf hog |
| 637 | return |
| 638 | endtry |
| 639 | let dir = expand('<amatch>:p:h') |
| 640 | for line in config_lines |
| 641 | if line =~ s:ft_rules_udev_rules_pattern |
| 642 | let udev_rules = substitute(line, s:ft_rules_udev_rules_pattern, '\1', "") |
| 643 | if dir == udev_rules |
| 644 | setf udevrules |
| 645 | endif |
| 646 | break |
| 647 | endif |
| 648 | endfor |
| 649 | setf hog |
| 650 | endfunc |
| 651 | |
Bram Moolenaar | d09a206 | 2017-11-11 15:37:45 +0100 | [diff] [blame] | 652 | func dist#ft#SQL() |
Bram Moolenaar | 851ee6c | 2017-11-09 20:46:17 +0100 | [diff] [blame] | 653 | if exists("g:filetype_sql") |
| 654 | exe "setf " . g:filetype_sql |
| 655 | else |
| 656 | setf sql |
| 657 | endif |
| 658 | endfunc |
| 659 | |
| 660 | " If the file has an extension of 't' and is in a directory 't' or 'xt' then |
| 661 | " it is almost certainly a Perl test file. |
| 662 | " If the first line starts with '#' and contains 'perl' it's probably a Perl |
| 663 | " file. |
| 664 | " (Slow test) If a file contains a 'use' statement then it is almost certainly |
| 665 | " a Perl file. |
Bram Moolenaar | d09a206 | 2017-11-11 15:37:45 +0100 | [diff] [blame] | 666 | func dist#ft#FTperl() |
Bram Moolenaar | 851ee6c | 2017-11-09 20:46:17 +0100 | [diff] [blame] | 667 | let dirname = expand("%:p:h:t") |
| 668 | if expand("%:e") == 't' && (dirname == 't' || dirname == 'xt') |
| 669 | setf perl |
| 670 | return 1 |
| 671 | endif |
| 672 | if getline(1)[0] == '#' && getline(1) =~ 'perl' |
| 673 | setf perl |
| 674 | return 1 |
| 675 | endif |
Bram Moolenaar | f0b03c4 | 2017-12-17 17:17:07 +0100 | [diff] [blame] | 676 | let save_cursor = getpos('.') |
| 677 | call cursor(1,1) |
| 678 | let has_use = search('^use\s\s*\k', 'c', 30) |
| 679 | call setpos('.', save_cursor) |
| 680 | if has_use |
Bram Moolenaar | 851ee6c | 2017-11-09 20:46:17 +0100 | [diff] [blame] | 681 | setf perl |
| 682 | return 1 |
| 683 | endif |
| 684 | return 0 |
| 685 | endfunc |
| 686 | |
| 687 | " Choose context, plaintex, or tex (LaTeX) based on these rules: |
| 688 | " 1. Check the first line of the file for "%&<format>". |
| 689 | " 2. Check the first 1000 non-comment lines for LaTeX or ConTeXt keywords. |
Bram Moolenaar | 20aac6c | 2018-09-02 21:07:30 +0200 | [diff] [blame] | 690 | " 3. Default to "plain" or to g:tex_flavor, can be set in user's vimrc. |
Bram Moolenaar | d09a206 | 2017-11-11 15:37:45 +0100 | [diff] [blame] | 691 | func dist#ft#FTtex() |
Bram Moolenaar | 851ee6c | 2017-11-09 20:46:17 +0100 | [diff] [blame] | 692 | let firstline = getline(1) |
| 693 | if firstline =~ '^%&\s*\a\+' |
| 694 | let format = tolower(matchstr(firstline, '\a\+')) |
| 695 | let format = substitute(format, 'pdf', '', '') |
| 696 | if format == 'tex' |
| 697 | let format = 'latex' |
| 698 | elseif format == 'plaintex' |
| 699 | let format = 'plain' |
| 700 | endif |
| 701 | elseif expand('%') =~ 'tex/context/.*/.*.tex' |
| 702 | let format = 'context' |
| 703 | else |
| 704 | " Default value, may be changed later: |
| 705 | let format = exists("g:tex_flavor") ? g:tex_flavor : 'plain' |
| 706 | " Save position, go to the top of the file, find first non-comment line. |
| 707 | let save_cursor = getpos('.') |
| 708 | call cursor(1,1) |
| 709 | let firstNC = search('^\s*[^[:space:]%]', 'c', 1000) |
| 710 | if firstNC " Check the next thousand lines for a LaTeX or ConTeXt keyword. |
| 711 | let lpat = 'documentclass\>\|usepackage\>\|begin{\|newcommand\>\|renewcommand\>' |
| 712 | let 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\>' |
| 713 | let kwline = search('^\s*\\\%(' . lpat . '\)\|^\s*\\\(' . cpat . '\)', |
| 714 | \ 'cnp', firstNC + 1000) |
| 715 | if kwline == 1 " lpat matched |
| 716 | let format = 'latex' |
| 717 | elseif kwline == 2 " cpat matched |
| 718 | let format = 'context' |
| 719 | endif " If neither matched, keep default set above. |
| 720 | " let lline = search('^\s*\\\%(' . lpat . '\)', 'cn', firstNC + 1000) |
| 721 | " let cline = search('^\s*\\\%(' . cpat . '\)', 'cn', firstNC + 1000) |
| 722 | " if cline > 0 |
| 723 | " let format = 'context' |
| 724 | " endif |
| 725 | " if lline > 0 && (cline == 0 || cline > lline) |
| 726 | " let format = 'tex' |
| 727 | " endif |
| 728 | endif " firstNC |
| 729 | call setpos('.', save_cursor) |
| 730 | endif " firstline =~ '^%&\s*\a\+' |
| 731 | |
| 732 | " Translation from formats to file types. TODO: add AMSTeX, RevTex, others? |
| 733 | if format == 'plain' |
| 734 | setf plaintex |
| 735 | elseif format == 'context' |
| 736 | setf context |
| 737 | else " probably LaTeX |
| 738 | setf tex |
| 739 | endif |
| 740 | return |
| 741 | endfunc |
| 742 | |
Bram Moolenaar | d09a206 | 2017-11-11 15:37:45 +0100 | [diff] [blame] | 743 | func dist#ft#FTxml() |
Bram Moolenaar | 851ee6c | 2017-11-09 20:46:17 +0100 | [diff] [blame] | 744 | let n = 1 |
Bram Moolenaar | 493fbe4 | 2019-03-17 17:16:12 +0100 | [diff] [blame] | 745 | while n < 100 && n <= line("$") |
Bram Moolenaar | 851ee6c | 2017-11-09 20:46:17 +0100 | [diff] [blame] | 746 | let line = getline(n) |
| 747 | " DocBook 4 or DocBook 5. |
| 748 | let is_docbook4 = line =~ '<!DOCTYPE.*DocBook' |
| 749 | let is_docbook5 = line =~ ' xmlns="http://docbook.org/ns/docbook"' |
| 750 | if is_docbook4 || is_docbook5 |
| 751 | let b:docbk_type = "xml" |
| 752 | if is_docbook5 |
| 753 | let b:docbk_ver = 5 |
| 754 | else |
| 755 | let b:docbk_ver = 4 |
| 756 | endif |
| 757 | setf docbk |
| 758 | return |
| 759 | endif |
| 760 | if line =~ 'xmlns:xbl="http://www.mozilla.org/xbl"' |
| 761 | setf xbl |
| 762 | return |
| 763 | endif |
| 764 | let n += 1 |
| 765 | endwhile |
| 766 | setf xml |
| 767 | endfunc |
| 768 | |
Bram Moolenaar | d09a206 | 2017-11-11 15:37:45 +0100 | [diff] [blame] | 769 | func dist#ft#FTy() |
Bram Moolenaar | 851ee6c | 2017-11-09 20:46:17 +0100 | [diff] [blame] | 770 | let n = 1 |
Bram Moolenaar | 493fbe4 | 2019-03-17 17:16:12 +0100 | [diff] [blame] | 771 | while n < 100 && n <= line("$") |
Bram Moolenaar | 851ee6c | 2017-11-09 20:46:17 +0100 | [diff] [blame] | 772 | let line = getline(n) |
| 773 | if line =~ '^\s*%' |
| 774 | setf yacc |
| 775 | return |
| 776 | endif |
| 777 | if getline(n) =~ '^\s*\(#\|class\>\)' && getline(n) !~ '^\s*#\s*include' |
| 778 | setf racc |
| 779 | return |
| 780 | endif |
| 781 | let n = n + 1 |
| 782 | endwhile |
| 783 | setf yacc |
| 784 | endfunc |
| 785 | |
Bram Moolenaar | d09a206 | 2017-11-11 15:37:45 +0100 | [diff] [blame] | 786 | func dist#ft#Redif() |
Bram Moolenaar | 851ee6c | 2017-11-09 20:46:17 +0100 | [diff] [blame] | 787 | let lnum = 1 |
| 788 | while lnum <= 5 && lnum < line('$') |
| 789 | if getline(lnum) =~ "^\ctemplate-type:" |
| 790 | setf redif |
| 791 | return |
| 792 | endif |
| 793 | let lnum = lnum + 1 |
| 794 | endwhile |
| 795 | endfunc |
| 796 | |
| 797 | |
| 798 | " Restore 'cpoptions' |
| 799 | let &cpo = s:cpo_save |
| 800 | unlet s:cpo_save |