2022-10-16 23:52:40 -07:00
|
|
|
if vim.g.did_load_filetypes then
|
2022-01-04 07:28:29 -07:00
|
|
|
return
|
|
|
|
end
|
2022-07-07 09:53:47 -07:00
|
|
|
vim.g.did_load_filetypes = 1
|
2022-01-04 07:28:29 -07:00
|
|
|
|
2022-03-13 14:52:41 -07:00
|
|
|
vim.api.nvim_create_augroup('filetypedetect', { clear = false })
|
|
|
|
|
2022-09-03 10:27:57 -07:00
|
|
|
vim.api.nvim_create_autocmd({ 'BufRead', 'BufNewFile', 'StdinReadPost' }, {
|
2022-03-13 14:52:41 -07:00
|
|
|
group = 'filetypedetect',
|
2022-06-09 12:12:36 -07:00
|
|
|
callback = function(args)
|
2023-08-30 08:14:58 -07:00
|
|
|
if not vim.api.nvim_buf_is_valid(args.buf) then
|
|
|
|
return
|
|
|
|
end
|
2024-03-22 20:46:23 -07:00
|
|
|
local ft, on_detect = vim.filetype.match({
|
|
|
|
-- The unexpanded file name is needed here. #27914
|
|
|
|
-- Neither args.file nor args.match are guaranteed to be unexpanded.
|
|
|
|
filename = vim.fn.bufname(args.buf),
|
|
|
|
buf = args.buf,
|
|
|
|
})
|
2022-07-06 07:25:23 -07:00
|
|
|
if not ft then
|
|
|
|
-- Generic configuration file used as fallback
|
|
|
|
ft = require('vim.filetype.detect').conf(args.file, args.buf)
|
|
|
|
if ft then
|
2022-09-25 07:29:25 -07:00
|
|
|
vim.api.nvim_buf_call(args.buf, function()
|
|
|
|
vim.api.nvim_cmd({ cmd = 'setf', args = { 'FALLBACK', ft } }, {})
|
|
|
|
end)
|
2022-07-06 07:25:23 -07:00
|
|
|
end
|
|
|
|
else
|
2023-08-24 10:48:21 -07:00
|
|
|
-- on_detect is called before setting the filetype so that it can set any buffer local
|
|
|
|
-- variables that may be used the filetype's ftplugin
|
2022-06-09 12:12:36 -07:00
|
|
|
if on_detect then
|
|
|
|
on_detect(args.buf)
|
|
|
|
end
|
2023-08-24 10:48:21 -07:00
|
|
|
|
|
|
|
vim.api.nvim_buf_call(args.buf, function()
|
|
|
|
vim.api.nvim_cmd({ cmd = 'setf', args = { ft } }, {})
|
|
|
|
end)
|
2022-06-09 12:12:36 -07:00
|
|
|
end
|
2022-03-13 14:52:41 -07:00
|
|
|
end,
|
|
|
|
})
|
2022-01-04 07:28:29 -07:00
|
|
|
|
2022-03-13 14:52:41 -07:00
|
|
|
-- These *must* be sourced after the autocommand above is created
|
2022-04-23 08:57:38 -07:00
|
|
|
if not vim.g.did_load_ftdetect then
|
|
|
|
vim.cmd([[
|
|
|
|
augroup filetypedetect
|
2023-07-13 15:46:16 -07:00
|
|
|
runtime! ftdetect/*.{vim,lua}
|
2022-04-23 08:57:38 -07:00
|
|
|
augroup END
|
|
|
|
]])
|
|
|
|
end
|
2022-01-04 07:28:29 -07:00
|
|
|
|
2022-07-07 09:53:47 -07:00
|
|
|
-- Set up the autocmd for user scripts.vim
|
|
|
|
vim.api.nvim_create_autocmd({ 'BufRead', 'BufNewFile' }, {
|
|
|
|
group = 'filetypedetect',
|
|
|
|
command = "if !did_filetype() && expand('<amatch>') !~ g:ft_ignore_pat | runtime! scripts.vim | endif",
|
|
|
|
})
|
2022-02-24 10:02:17 -07:00
|
|
|
|
2022-07-07 09:53:47 -07:00
|
|
|
vim.api.nvim_create_autocmd('StdinReadPost', {
|
|
|
|
group = 'filetypedetect',
|
|
|
|
command = 'if !did_filetype() | runtime! scripts.vim | endif',
|
|
|
|
})
|
2022-01-04 15:02:01 -07:00
|
|
|
|
|
|
|
if not vim.g.ft_ignore_pat then
|
|
|
|
vim.g.ft_ignore_pat = '\\.\\(Z\\|gz\\|bz2\\|zip\\|tgz\\)$'
|
|
|
|
end
|