filetype.lua 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. -- Skip if legacy filetype is enabled or filetype detection is disabled
  2. if vim.g.do_legacy_filetype or vim.g.did_load_filetypes then
  3. return
  4. end
  5. vim.g.did_load_filetypes = 1
  6. vim.api.nvim_create_augroup('filetypedetect', { clear = false })
  7. vim.api.nvim_create_autocmd({ 'BufRead', 'BufNewFile', 'StdinReadPost' }, {
  8. group = 'filetypedetect',
  9. callback = function(args)
  10. local ft, on_detect = vim.filetype.match({ filename = args.match, buf = args.buf })
  11. if not ft then
  12. -- Generic configuration file used as fallback
  13. ft = require('vim.filetype.detect').conf(args.file, args.buf)
  14. if ft then
  15. vim.api.nvim_buf_call(args.buf, function()
  16. vim.api.nvim_cmd({ cmd = 'setf', args = { 'FALLBACK', ft } }, {})
  17. end)
  18. end
  19. else
  20. vim.api.nvim_buf_call(args.buf, function()
  21. vim.api.nvim_cmd({ cmd = 'setf', args = { ft } }, {})
  22. end)
  23. if on_detect then
  24. on_detect(args.buf)
  25. end
  26. end
  27. end,
  28. })
  29. -- These *must* be sourced after the autocommand above is created
  30. if not vim.g.did_load_ftdetect then
  31. vim.cmd([[
  32. augroup filetypedetect
  33. runtime! ftdetect/*.vim
  34. runtime! ftdetect/*.lua
  35. augroup END
  36. ]])
  37. end
  38. -- Set up the autocmd for user scripts.vim
  39. vim.api.nvim_create_autocmd({ 'BufRead', 'BufNewFile' }, {
  40. group = 'filetypedetect',
  41. command = "if !did_filetype() && expand('<amatch>') !~ g:ft_ignore_pat | runtime! scripts.vim | endif",
  42. })
  43. vim.api.nvim_create_autocmd('StdinReadPost', {
  44. group = 'filetypedetect',
  45. command = 'if !did_filetype() | runtime! scripts.vim | endif',
  46. })
  47. if not vim.g.ft_ignore_pat then
  48. vim.g.ft_ignore_pat = '\\.\\(Z\\|gz\\|bz2\\|zip\\|tgz\\)$'
  49. end