robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

Justin M. KeyesMerge #38259 docs9a5abf0277

master
1.6 KiB57 linesraw
1if vim.g.did_load_filetypes then
2  return
3end
4vim.g.did_load_filetypes = 1
5
6vim.api.nvim_create_augroup('filetypedetect', { clear = false })
7
8vim.api.nvim_create_autocmd({ 'BufRead', 'BufNewFile', 'StdinReadPost' }, {
9  group = 'filetypedetect',
10  callback = function(ev)
11    if not vim.api.nvim_buf_is_valid(ev.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 = ev.file,
18      buf = ev.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(ev.buf)
25      end
26
27      vim._with({ buf = ev.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
38vim.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
43vim.api.nvim_create_autocmd('StdinReadPost', {
44  group = 'filetypedetect',
45  command = 'if !did_filetype() | runtime! scripts.vim | endif',
46})
47
48if not vim.g.ft_ignore_pat then
49  vim.g.ft_ignore_pat = '\\.\\(Z\\|gz\\|bz2\\|zip\\|tgz\\)$'
50end
51
52-- These *must* be sourced after the autocommands above are created
53vim.cmd([[
54  augroup filetypedetect
55  runtime! ftdetect/*.{vim,lua}
56  augroup END
57]])