filetype.lua (1682B)
1 if vim.g.did_load_filetypes then 2 return 3 end 4 vim.g.did_load_filetypes = 1 5 6 vim.api.nvim_create_augroup('filetypedetect', { clear = false }) 7 8 vim.api.nvim_create_autocmd({ 'BufRead', 'BufNewFile', 'StdinReadPost' }, { 9 group = 'filetypedetect', 10 callback = function(args) 11 if not vim.api.nvim_buf_is_valid(args.buf) then 12 return 13 end 14 local ft, on_detect, is_fallback = vim.filetype.match({ 15 -- The unexpanded file name is needed here. #27914 16 -- However, bufname() can't be used, as it doesn't work with :doautocmd. #31306 17 filename = args.file, 18 buf = args.buf, 19 }) 20 if ft then 21 -- on_detect is called before setting the filetype so that it can set any buffer local 22 -- variables that may be used the filetype's ftplugin 23 if on_detect then 24 on_detect(args.buf) 25 end 26 27 vim._with({ buf = args.buf }, function() 28 vim.api.nvim_cmd({ 29 cmd = 'setf', 30 args = (is_fallback and { 'FALLBACK', ft } or { ft }), 31 }, {}) 32 end) 33 end 34 end, 35 }) 36 37 -- Set up the autocmd for user scripts.vim 38 vim.api.nvim_create_autocmd({ 'BufRead', 'BufNewFile' }, { 39 group = 'filetypedetect', 40 command = "if !did_filetype() && expand('<amatch>') !~ g:ft_ignore_pat | runtime! scripts.vim | endif", 41 }) 42 43 vim.api.nvim_create_autocmd('StdinReadPost', { 44 group = 'filetypedetect', 45 command = 'if !did_filetype() | runtime! scripts.vim | endif', 46 }) 47 48 if not vim.g.ft_ignore_pat then 49 vim.g.ft_ignore_pat = '\\.\\(Z\\|gz\\|bz2\\|zip\\|tgz\\)$' 50 end 51 52 -- These *must* be sourced after the autocommands above are created 53 vim.cmd([[ 54 augroup filetypedetect 55 runtime! ftdetect/*.{vim,lua} 56 augroup END 57 ]])