filetype.lua 1.6 KB

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