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
|
2024-12-06 05:57:21 -07:00
|
|
|
-- However, bufname() can't be used, as it doesn't work with :doautocmd. #31306
|
|
|
|
filename = args.file,
|
2024-03-22 20:46:23 -07:00
|
|
|
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
|
2024-06-08 12:40:18 -07:00
|
|
|
vim._with({ buf = args.buf }, function()
|
2022-09-25 07:29:25 -07:00
|
|
|
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
|
|
|
|
2024-06-08 12:40:18 -07:00
|
|
|
vim._with({ buf = args.buf }, function()
|
2023-08-24 10:48:21 -07:00
|
|
|
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-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
|
2024-06-22 06:17:36 -07:00
|
|
|
|
|
|
|
-- These *must* be sourced after the autocommands above are created
|
|
|
|
vim.cmd([[
|
|
|
|
augroup filetypedetect
|
|
|
|
runtime! ftdetect/*.{vim,lua}
|
|
|
|
augroup END
|
|
|
|
]])
|