aboutsummaryrefslogtreecommitdiff
path: root/runtime/lua/vim
diff options
context:
space:
mode:
authorJonas Strittmatter <40792180+smjonas@users.noreply.github.com>2022-06-06 17:25:33 +0200
committerGitHub <noreply@github.com>2022-06-06 09:25:33 -0600
commit41a49dd9c8e4362c24a3699ec2be746af4623449 (patch)
tree00c604012160bfed14e4bc3ebbd95e71cc359e9c /runtime/lua/vim
parentcf2738109af69f572034147a1aad35a200608180 (diff)
downloadrneovim-41a49dd9c8e4362c24a3699ec2be746af4623449.tar.gz
rneovim-41a49dd9c8e4362c24a3699ec2be746af4623449.tar.bz2
rneovim-41a49dd9c8e4362c24a3699ec2be746af4623449.zip
refactor(runtime): port remaining patterns from filetype.vim to filetype.lua (#18814)
Diffstat (limited to 'runtime/lua/vim')
-rw-r--r--runtime/lua/vim/filetype.lua319
-rw-r--r--runtime/lua/vim/filetype/detect.lua113
2 files changed, 360 insertions, 72 deletions
diff --git a/runtime/lua/vim/filetype.lua b/runtime/lua/vim/filetype.lua
index f5c29639c6..c091e2f35c 100644
--- a/runtime/lua/vim/filetype.lua
+++ b/runtime/lua/vim/filetype.lua
@@ -3,21 +3,22 @@ local api = vim.api
local M = {}
---@private
-local function starsetf(ft)
+local function starsetf(ft, opts)
return {
- function(path)
+ function(path, bufnr)
+ local f = type(ft) == 'function' and ft(path, bufnr) or ft
if not vim.g.ft_ignore_pat then
- return ft
+ return f
end
local re = vim.regex(vim.g.ft_ignore_pat)
- if re:match_str(path) then
- return ft
+ if not re:match_str(path) then
+ return f
end
end,
{
-- Starset matches should always have lowest priority
- priority = -math.huge,
+ priority = (opts and opts.priority) or -math.huge,
},
}
end
@@ -29,8 +30,73 @@ local function getline(bufnr, start_lnum, end_lnum)
return table.concat(lines) or ''
end
--- Filetypes based on file extension
+---@private
+--- Get a single line or line-range from the buffer.
+---
+---@param bufnr number|nil The buffer to get the lines from
+---@param start_lnum number The line number of the first line (inclusive, 1-based)
+---@param end_lnum number|nil The line number of the last line (inclusive, 1-based)
+---@return table<string>|string Array of lines, or string when end_lnum is omitted
+function M.getlines(bufnr, start_lnum, end_lnum)
+ if not end_lnum then
+ -- Return a single line as a string
+ return vim.api.nvim_buf_get_lines(bufnr, start_lnum - 1, start_lnum, false)[1]
+ end
+ return vim.api.nvim_buf_get_lines(bufnr, start_lnum - 1, end_lnum, false)
+end
+
+---@private
+--- Check whether a string matches any of the given Lua patterns.
+---
+---@param s string The string to check
+---@param patterns table<string> A list of Lua patterns
+---@return boolean `true` if s matched a pattern, else `false`
+function M.findany(s, patterns)
+ if s == nil then
+ return false
+ end
+ for _, v in ipairs(patterns) do
+ if s:find(v) then
+ return true
+ end
+ end
+ return false
+end
+
+---@private
+--- Get the next non-whitespace line in the buffer.
+---
+---@param bufnr number The buffer to get the line from
+---@param start_lnum number The line number of the first line to start from (inclusive, 1-based)
+---@return string|nil The first non-blank line if found or `nil` otherwise
+function M.nextnonblank(bufnr, start_lnum)
+ for _, line in ipairs(M.getlines(bufnr, start_lnum, -1)) do
+ if not line:find('^%s*$') then
+ return line
+ end
+ end
+ return nil
+end
+
+---@private
+--- Check whether the given string matches the Vim regex pattern.
+M.matchregex = (function()
+ local cache = {}
+ return function(s, pattern)
+ if s == nil then
+ return nil
+ end
+ if not cache[pattern] then
+ cache[pattern] = vim.regex(pattern)
+ end
+ return cache[pattern]:match_str(s)
+ end
+end)()
+
-- luacheck: push no unused args
+-- luacheck: push ignore 122
+
+-- Filetypes based on file extension
---@diagnostic disable: unused-local
local extension = {
-- BEGIN EXTENSION
@@ -178,6 +244,7 @@ local extension = {
diff = 'diff',
rej = 'diff',
Dockerfile = 'dockerfile',
+ dockerfile = 'dockerfile',
bat = 'dosbatch',
wrap = 'dosini',
ini = 'dosini',
@@ -1032,7 +1099,7 @@ local extension = {
end,
txt = function(path, bufnr)
--helpfiles match *.txt, but should have a modeline as last line
- if not getline(bufnr, -1):match('vim:.*ft=help') then
+ if not getline(bufnr, -1):find('vim:.*ft=help') then
return 'text'
end
end,
@@ -1175,6 +1242,67 @@ local extension = {
return 'rc'
end
end,
+ control = function(path, bufnr)
+ return require('vim.filetype.detect').control(bufnr)
+ end,
+ copyright = function(path, bufnr)
+ return require('vim.filetype.detect').copyright(bufnr)
+ end,
+ -- Ignored extensions
+ bak = function(path, bufnr)
+ local root = vim.fn.fnamemodify(path, ':r')
+ return M.match(root, bufnr)
+ end,
+ ['dpkg-dist'] = function(path, bufnr)
+ local root = vim.fn.fnamemodify(path, ':r')
+ return M.match(root, bufnr)
+ end,
+ ['dpkg-old'] = function(path, bufnr)
+ local root = vim.fn.fnamemodify(path, ':r')
+ return M.match(root, bufnr)
+ end,
+ ['dpkg-new'] = function(path, bufnr)
+ local root = vim.fn.fnamemodify(path, ':r')
+ return M.match(root, bufnr)
+ end,
+ ['dpkg-bak'] = function(path, bufnr)
+ local root = vim.fn.fnamemodify(path, ':r')
+ return M.match(root, bufnr)
+ end,
+ new = function(path, bufnr)
+ local root = vim.fn.fnamemodify(path, ':r')
+ return M.match(root, bufnr)
+ end,
+ old = function(path, bufnr)
+ local root = vim.fn.fnamemodify(path, ':r')
+ return M.match(root, bufnr)
+ end,
+ orig = function(path, bufnr)
+ local root = vim.fn.fnamemodify(path, ':r')
+ return M.match(root, bufnr)
+ end,
+ pacsave = function(path, bufnr)
+ local root = vim.fn.fnamemodify(path, ':r')
+ return M.match(root, bufnr)
+ end,
+ pacnew = function(path, bufnr)
+ local root = vim.fn.fnamemodify(path, ':r')
+ return M.match(root, bufnr)
+ end,
+ rpmsave = function(path, bufnr)
+ local root = vim.fn.fnamemodify(path, ':r')
+ return M.match(root, bufnr)
+ end,
+ rmpnew = function(path, bufnr)
+ local root = vim.fn.fnamemodify(path, ':r')
+ return M.match(root, bufnr)
+ end,
+ ['in'] = function(path, bufnr)
+ if vim.fs.basename(path) ~= 'configure.in' then
+ local root = vim.fn.fnamemodify(path, ':r')
+ return M.match(root, bufnr)
+ end
+ end,
-- END EXTENSION
}
@@ -1196,8 +1324,9 @@ local filename = {
['named.root'] = 'bindzone',
WORKSPACE = 'bzl',
BUILD = 'bzl',
- ['cabal.config'] = 'cabalconfig',
['cabal.project'] = 'cabalproject',
+ [vim.env.HOME .. '/cabal.config'] = 'cabalconfig',
+ ['cabal.config'] = 'cabalconfig',
calendar = 'calendar',
catalog = 'catalog',
['/etc/cdrdao.conf'] = 'cdrdaoconf',
@@ -1355,6 +1484,9 @@ local filename = {
['/etc/nanorc'] = 'nanorc',
Neomuttrc = 'neomuttrc',
['.netrc'] = 'netrc',
+ NEWS = function(path, bufnr)
+ return require('vim.filetype.detect').news(bufnr)
+ end,
['.ocamlinit'] = 'ocaml',
['.octaverc'] = 'octave',
octaverc = 'octave',
@@ -1380,15 +1512,28 @@ local filename = {
['/etc/pinforc'] = 'pinfo',
['/.pinforc'] = 'pinfo',
['.povrayrc'] = 'povini',
+ ['printcap'] = function(path, bufnr)
+ vim.b[bufnr].ptcap_type = 'print'
+ return 'ptcap'
+ end,
+ ['termcap'] = function(path, bufnr)
+ vim.b[bufnr].ptcap_type = 'term'
+ return 'ptcap'
+ end,
['.procmailrc'] = 'procmail',
['.procmail'] = 'procmail',
['/etc/protocols'] = 'protocols',
+ ['INDEX'] = function(path, bufnr)
+ return require('vim.filetype.detect').psf(bufnr)
+ end,
+ ['INFO'] = function(path, bufnr)
+ return require('vim.filetype.detect').psf(bufnr)
+ end,
['.pythonstartup'] = 'python',
['.pythonrc'] = 'python',
SConstruct = 'python',
ratpoisonrc = 'ratpoison',
['.ratpoisonrc'] = 'ratpoison',
- v = 'rcs',
inputrc = 'readline',
['.inputrc'] = 'readline',
['.reminders'] = 'remind',
@@ -1458,6 +1603,14 @@ local filename = {
['.Xpdefaults'] = 'xdefaults',
['xdm-config'] = 'xdefaults',
['.Xdefaults'] = 'xdefaults',
+ ['xorg.conf'] = function(path, bufnr)
+ vim.b[bufnr].xf86conf_xfree86_version = 4
+ return 'xf86conf'
+ end,
+ ['xorg.conf-4'] = function(path, bufnr)
+ vim.b[bufnr].xf86conf_xfree86_version = 4
+ return 'xf86conf'
+ end,
['/etc/xinetd.conf'] = 'xinetd',
fglrxrc = 'xml',
['/etc/blkid.tab'] = 'xml',
@@ -1528,6 +1681,9 @@ local filename = {
['tcsh.tcshrc'] = function(path, bufnr)
return require('vim.filetype.detect').shell(path, bufnr, 'tcsh')
end,
+ ['XF86Config'] = function(path, bufnr)
+ return require('vim.filetype.detect').xf86conf(bufnr)
+ end,
-- END FILENAME
}
@@ -1544,12 +1700,22 @@ local pattern = {
['.*bsd'] = 'bsdl',
['bzr_log%..*'] = 'bzr',
['.*enlightenment/.*%.cfg'] = 'c',
+ ['.*/%.calendar/.*'] = starsetf('calendar'),
+ ['.*/share/calendar/.*/calendar%..*'] = starsetf('calendar'),
+ ['.*/share/calendar/calendar%..*'] = starsetf('calendar'),
['.*/etc/defaults/cdrdao'] = 'cdrdaoconf',
['.*/etc/cdrdao%.conf'] = 'cdrdaoconf',
['.*/etc/default/cdrdao'] = 'cdrdaoconf',
['.*hgrc'] = 'cfg',
['.*%.%.ch'] = 'chill',
['.*%.cmake%.in'] = 'cmake',
+ -- */cmus/rc and */.cmus/rc
+ ['.*/%.?cmus/rc'] = 'cmusrc',
+ -- */cmus/*.theme and */.cmus/*.theme
+ ['.*/%.?cmus/.*%.theme'] = 'cmusrc',
+ ['.*/%.cmus/autosave'] = 'cmusrc',
+ ['.*/%.cmus/command%-history'] = 'cmusrc',
+ ['cvs%d+'] = 'cvs',
['.*/debian/changelog'] = 'debchangelog',
['.*/debian/control'] = 'debcontrol',
['.*/debian/copyright'] = 'debcopyright',
@@ -1572,11 +1738,26 @@ local pattern = {
['.*/%.config/git/config'] = 'gitconfig',
['.*%.git/config%.worktree'] = 'gitconfig',
['.*%.git/worktrees/.*/config%.worktree'] = 'gitconfig',
+ ['.*/git/config'] = function(path, bufnr)
+ if vim.env.XDG_CONFIG_HOME and path:find(vim.env.XDG_CONFIG_HOME .. '/git/config') then
+ return 'gitconfig'
+ end
+ end,
['%.gitsendemail%.msg%.......'] = 'gitsendemail',
['gkrellmrc_.'] = 'gkrellmrc',
['.*/usr/.*/gnupg/options%.skel'] = 'gpg',
['.*/%.gnupg/options'] = 'gpg',
['.*/%.gnupg/gpg%.conf'] = 'gpg',
+ ['.*/options'] = function(path, bufnr)
+ if vim.env.GNUPGHOME and path:find(vim.env.GNUPGHOME .. '/options') then
+ return 'gpg'
+ end
+ end,
+ ['.*/gpg%.conf'] = function(path, bufnr)
+ if vim.env.GNUPGHOME and path:find(vim.env.GNUPGHOME .. '/gpg%.conf') then
+ return 'gpg'
+ end
+ end,
['.*/etc/group'] = 'group',
['.*/etc/gshadow'] = 'group',
['.*/etc/group%.edit'] = 'group',
@@ -1647,6 +1828,12 @@ local pattern = {
['.*/%.pinforc'] = 'pinfo',
['.*/etc/pinforc'] = 'pinfo',
['.*/etc/protocols'] = 'protocols',
+ ['.*printcap.*'] = starsetf(function(path, bufnr)
+ if vim.fn.did_filetype() == 0 then
+ vim.b[bufnr].ptcap_type = 'print'
+ return 'ptcap'
+ end
+ end),
['.*baseq[2-3]/.*%.cfg'] = 'quake',
['.*quake[1-3]/.*%.cfg'] = 'quake',
['.*id1/.*%.cfg'] = 'quake',
@@ -1671,9 +1858,33 @@ local pattern = {
['.*%.swift%.gyb'] = 'swiftgyb',
['.*/etc/sysctl%.conf'] = 'sysctl',
['.*/etc/sysctl%.d/.*%.conf'] = 'sysctl',
+ ['.*/systemd/.*%.automount'] = 'systemd',
+ ['.*/systemd/.*%.dnssd'] = 'systemd',
+ ['.*/systemd/.*%.link'] = 'systemd',
+ ['.*/systemd/.*%.mount'] = 'systemd',
+ ['.*/systemd/.*%.netdev'] = 'systemd',
+ ['.*/systemd/.*%.network'] = 'systemd',
+ ['.*/systemd/.*%.nspawn'] = 'systemd',
+ ['.*/systemd/.*%.path'] = 'systemd',
+ ['.*/systemd/.*%.service'] = 'systemd',
+ ['.*/systemd/.*%.slice'] = 'systemd',
+ ['.*/systemd/.*%.socket'] = 'systemd',
+ ['.*/systemd/.*%.swap'] = 'systemd',
+ ['.*/systemd/.*%.target'] = 'systemd',
+ ['.*/systemd/.*%.timer'] = 'systemd',
['.*/etc/systemd/.*%.conf%.d/.*%.conf'] = 'systemd',
['.*/%.config/systemd/user/.*%.d/.*%.conf'] = 'systemd',
['.*/etc/systemd/system/.*%.d/.*%.conf'] = 'systemd',
+ ['.*/etc/systemd/system/.*%.d/%.#.*'] = 'systemd',
+ ['.*/etc/systemd/system/%.#.*'] = 'systemd',
+ ['.*/%.config/systemd/user/.*%.d/%.#.*'] = 'systemd',
+ ['.*/%.config/systemd/user/%.#.*'] = 'systemd',
+ ['.*termcap.*'] = starsetf(function(path, bufnr)
+ if vim.fn.did_filetype() == 0 then
+ vim.b[bufnr].ptcap_type = 'term'
+ return 'ptcap'
+ end
+ end),
['.*%.t%.html'] = 'tilde',
['%.?tmux.*%.conf'] = 'tmux',
['%.?tmux.*%.conf.*'] = { 'tmux', { priority = -1 } },
@@ -1719,7 +1930,19 @@ local pattern = {
['%.login.*'] = function(path, bufnr)
return require('vim.filetype.detect').csh(path, bufnr)
end,
- ['%.neomuttrc.*'] = starsetf('neomuttrc'),
+ ['Muttrc.*'] = starsetf('muttrc'),
+ ['Muttngrc.*'] = starsetf('muttrc'),
+ -- muttrc* and .muttrc*
+ ['%.?muttrc.*'] = starsetf('muttrc'),
+ -- muttngrc* and .muttngrc*
+ ['%.?muttngrc.*'] = starsetf('muttrc'),
+ ['.*/%.mutt/muttrc.*'] = starsetf('muttrc'),
+ ['.*/%.muttng/muttrc.*'] = starsetf('muttrc'),
+ ['.*/%.muttng/muttngrc.*'] = starsetf('muttrc'),
+ ['Neomuttrc.*'] = starsetf('neomuttrc'),
+ -- neomuttrc* and .neomuttrc*
+ ['%.?neomuttrc.*'] = starsetf('neomuttrc'),
+ ['.*/%.neomutt/neomuttrc.*'] = starsetf('neomuttrc'),
['%.profile.*'] = function(path, bufnr)
return require('vim.filetype.detect').sh(path, bufnr, getline(bufnr, 1))
end,
@@ -1742,8 +1965,15 @@ local pattern = {
['.*%.properties_.._.._.*'] = starsetf('jproperties'),
['.*%.vhdl_[0-9].*'] = starsetf('vhdl'),
['.*/%.fvwm/.*'] = starsetf('fvwm'),
+ ['.*fvwmrc.*'] = starsetf(function(path, bufnr)
+ vim.b[bufnr].fvwm_version = 1
+ return 'fvwm'
+ end),
+ ['.*fvwm95.*%.hook'] = starsetf(function(path, bufnr)
+ vim.b[bufnr].fvwm_version = 1
+ return 'fvwm'
+ end),
['.*/%.gitconfig%.d/.*'] = starsetf('gitconfig'),
- ['.*/%.neomutt/neomuttrc.*'] = starsetf('neomuttrc'),
['.*/Xresources/.*'] = starsetf('xdefaults'),
['.*/app%-defaults/.*'] = starsetf('xdefaults'),
['.*/bind/db%..*'] = starsetf('bindzone'),
@@ -1763,6 +1993,11 @@ local pattern = {
['.*/etc/httpd/sites%-.*/.*'] = starsetf('apache'),
['.*/etc/logcheck/.*%.d.*/.*'] = starsetf('logcheck'),
['.*/etc/modprobe%..*'] = starsetf('modconf'),
+ ['.*/etc/modutils/.*'] = starsetf(function(path, bufnr)
+ if vim.fn.executable(vim.fn.expand(path)) ~= 1 then
+ return 'modconf'
+ end
+ end),
['.*/etc/pam%.d/.*'] = starsetf('pamconf'),
['.*/etc/profile'] = function(path, bufnr)
return require('vim.filetype.detect').sh(path, bufnr, getline(bufnr, 1))
@@ -1785,7 +2020,6 @@ local pattern = {
['Dockerfile%..*'] = starsetf('dockerfile'),
['JAM.*%..*'] = starsetf('jam'),
['Kconfig%..*'] = starsetf('kconfig'),
- ['Neomuttrc.*'] = starsetf('neomuttrc'),
['Prl.*%..*'] = starsetf('jam'),
['Xresources.*'] = starsetf('xdefaults'),
['[mM]akefile.*'] = starsetf('make'),
@@ -1802,7 +2036,8 @@ local pattern = {
['gtkrc.*'] = starsetf('gtkrc'),
['httpd%.conf.*'] = starsetf('apache'),
['lilo%.conf.*'] = starsetf('lilo'),
- ['neomuttrc.*'] = starsetf('neomuttrc'),
+ ['Muttrc'] = 'muttrc',
+ ['Muttngrc'] = 'muttrc',
['proftpd%.conf.*'] = starsetf('apachestyle'),
['reportbug%-.*'] = starsetf('mail'),
['sgml%.catalog.*'] = starsetf('catalog'),
@@ -1816,8 +2051,8 @@ local pattern = {
['%.article%.%d+'] = 'mail',
['pico%.%d+'] = 'mail',
['mutt%-.*%-%w+'] = 'mail',
- ['neomutt%-.*%-%w+'] = 'mail',
['muttng%-.*%-%w+'] = 'mail',
+ ['neomutt%-.*%-%w+'] = 'mail',
['mutt' .. string.rep('[%w_-]', 6)] = 'mail',
['neomutt' .. string.rep('[%w_-]', 6)] = 'mail',
['/tmp/SLRN[0-9A-Z.]+'] = 'mail',
@@ -1861,7 +2096,7 @@ local pattern = {
{ priority = -1 },
},
['.*%.[Dd][Aa][Tt]'] = function(path, bufnr)
- return require('vim.filetype.detect').dat(bufnr)
+ return require('vim.filetype.detect').dat(path, bufnr)
end,
['.*%.[Mm][Oo][Dd]'] = function(path, bufnr)
return require('vim.filetype.detect').mod(path, bufnr)
@@ -1880,9 +2115,61 @@ local pattern = {
['example%.gitolite%.rc'] = 'perl',
-- Neovim only
['.*/queries/.*%.scm'] = 'query', -- tree-sitter queries
+ ['.*,v'] = 'rcs',
+ ['.*/xorg%.conf%.d/.*%.conf'] = function(path, bufnr)
+ vim.b[bufnr].xf86conf_xfree86_version = 4
+ return 'xf86config'
+ end,
+ -- Increase priority to run before the pattern below
+ ['XF86Config%-4'] = starsetf(function(path, bufnr)
+ vim.b[bufnr].xf86conf_xfree86_version = 4
+ return 'xf86config'
+ end, { priority = -math.huge + 1 }),
+ ['XF86Config.*'] = starsetf(function(path, bufnr)
+ vim.b[bufnr].xf86conf_xfree86_version = 4
+ return require('vim.filetype.detect').xf86conf(bufnr)
+ end),
+ ['[cC]hange[lL]og.*'] = starsetf(function(path, bufnr)
+ local line = getline(bufnr, 1):lower()
+ if line:find('; urgency=') then
+ return 'debchangelog'
+ else
+ return 'changelog'
+ end
+ end),
+ ['.*fvwm2rc.*'] = starsetf(function(path, bufnr)
+ if vim.fn.fnamemodify(path, ':e') == 'm4' then
+ return 'fvwm2m4'
+ else
+ vim.b[bufnr].fvwm_version = 2
+ return 'fvwm'
+ end
+ end),
+ ['.*%.[Ll][Oo][Gg]'] = function(path, bufnr)
+ -- Innovation Data Processing
+ -- (refactor of filetype.vim since the patterns are case-insensitive)
+ path = path:lower()
+ if M.findany(path, { 'upstream%.log', 'upstream%..*%.log', '.*%.upstream%.log', 'upstream%-.*%.log' }) then
+ return 'upstreamlog'
+ elseif M.findany(path, { 'upstreaminstall%.log', 'upstreaminstall%..*%.log', '.*%.upstreaminstall%.log' }) then
+ return 'upstreaminstalllog'
+ elseif M.findany(path, { 'usserver%.log', 'usserver%..*%.log', '.*%.usserver%.log' }) then
+ return 'usserverlog'
+ elseif M.findany(path, { 'usw2kagt%.log', 'usws2kagt%..*%.log', '.*%.usws2kagt%.log' }) then
+ return 'usw2kagtlog'
+ end
+ end,
+ -- Ignored extension
+ ['.*~'] = function(path, bufnr)
+ local short = path:gsub('~$', '', 1)
+ if path ~= short and short ~= '' then
+ return M.match(vim.fn.fnameescape(short), bufnr)
+ end
+ end,
-- END PATTERN
}
-- luacheck: pop
+-- luacheck: pop
---@private
local function sort_by_priority(t)
diff --git a/runtime/lua/vim/filetype/detect.lua b/runtime/lua/vim/filetype/detect.lua
index b35303eefc..2cc75b026e 100644
--- a/runtime/lua/vim/filetype/detect.lua
+++ b/runtime/lua/vim/filetype/detect.lua
@@ -16,56 +16,10 @@
local M = {}
----@private
-local function getlines(bufnr, start_lnum, end_lnum)
- if not end_lnum then
- -- Return a single line as a string
- return vim.api.nvim_buf_get_lines(bufnr, start_lnum - 1, start_lnum, false)[1]
- end
- return vim.api.nvim_buf_get_lines(bufnr, start_lnum - 1, end_lnum, false)
-end
-
----@private
-local function findany(s, patterns)
- if s == nil then
- return false
- end
- for _, v in ipairs(patterns) do
- if s:find(v) then
- return true
- end
- end
- return false
-end
-
----@private
-local function nextnonblank(bufnr, start_lnum)
- for _, line in ipairs(getlines(bufnr, start_lnum, -1)) do
- if not line:find('^%s*$') then
- return line
- end
- end
- return nil
-end
-
----@private
-local matchregex = (function()
- local cache = {}
- return function(line, pattern)
- if line == nil then
- return nil
- end
- if not cache[pattern] then
- cache[pattern] = vim.regex(pattern)
- end
- return cache[pattern]:match_str(line)
- end
-end)()
-
----@private
-local did_filetype = function()
- return vim.fn.did_filetype() ~= 0
-end
+local getlines = vim.filetype.getlines
+local findany = vim.filetype.findany
+local nextnonblank = vim.filetype.nextnonblank
+local matchregex = vim.filetype.matchregex
-- luacheck: push no unused args
-- luacheck: push ignore 122
@@ -200,7 +154,7 @@ function M.change(bufnr)
end
function M.csh(path, bufnr)
- if did_filetype() then
+ if vim.fn.did_filetype() ~= 0 then
-- Filetype was already detected
return
end
@@ -213,11 +167,15 @@ function M.csh(path, bufnr)
end
end
--- Determine if a *.dat file is Kuka Robot Language
-function M.dat(bufnr)
+function M.dat(path, bufnr)
+ -- Innovation data processing
+ if findany(path:lower(), { '^upstream%.dat$', '^upstream%..*%.dat$', '^.*%.upstream%.dat$' }) then
+ return 'upstreamdat'
+ end
if vim.g.filetype_dat then
return vim.g.filetype_dat
end
+ -- Determine if a *.dat file is Kuka Robot Language
local line = nextnonblank(bufnr, 1)
if matchregex(line, [[\c\v^\s*%(\&\w+|defdat>)]]) then
return 'krl'
@@ -257,7 +215,7 @@ function M.dep3patch(path, bufnr)
end
function M.dtrace(bufnr)
- if did_filetype() then
+ if vim.fn.did_filetype() ~= 0 then
-- Filetype was already detected
return
end
@@ -815,7 +773,7 @@ end
-- Also called from filetype.lua
function M.sh(path, bufnr, name)
- if did_filetype() or path:find(vim.g.ft_ignore_pat) then
+ if vim.fn.did_filetype() ~= 0 or path:find(vim.g.ft_ignore_pat) then
-- Filetype was already detected or detection should be skipped
return
end
@@ -848,7 +806,7 @@ end
-- For shell-like file types, check for an "exec" command hidden in a comment, as used for Tcl.
-- Also called from scripts.vim, thus can't be local to this script. [TODO]
function M.shell(path, bufnr, name)
- if did_filetype() or matchregex(path, vim.g.ft_ignore_pat) then
+ if vim.fn.did_filetype() ~= 0 or matchregex(path, vim.g.ft_ignore_pat) then
-- Filetype was already detected or detection should be skipped
return
end
@@ -1054,6 +1012,49 @@ function M.hw(bufnr)
end
return 'virata'
end
+
+function M.news(bufnr)
+ if getlines(bufnr, 1):lower():find('; urgency=') then
+ return 'debchangelog'
+ end
+end
+
+-- Debian Control
+function M.control(bufnr)
+ if getlines(bufnr, 1):find('^Source:') then
+ return 'debcontrol'
+ end
+end
+
+-- Debian Copyright
+function M.copyright(bufnr)
+ if getlines(bufnr, 1):find('^Format:') then
+ return 'debcopyright'
+ end
+end
+
+-- Software Distributor Product Specification File (POSIX 1387.2-1995)
+function M.psf(bufnr)
+ local line = getlines(bufnr, 1):lower()
+ if
+ findany(
+ line,
+ { '^%s*distribution%s*$', '^%s*installed_software%s*$', '^%s*root%s*$', '^%s*bundle%s*$', '^%s*product%s*$' }
+ )
+ then
+ return 'psf'
+ end
+end
+
+-- XFree86 config
+function M.xfree86(bufnr)
+ local line = getlines(bufnr, 1)
+ if matchregex(line, [[\<XConfigurator\>]]) then
+ vim.b[bufnr].xf86conf_xfree86_version = 3
+ return 'xf86conf'
+ end
+end
+
-- luacheck: pop
-- luacheck: pop