diff options
-rw-r--r-- | runtime/doc/filetype.txt | 47 | ||||
-rw-r--r-- | runtime/doc/lua.txt | 4 | ||||
-rw-r--r-- | runtime/filetype.lua | 32 | ||||
-rw-r--r-- | runtime/filetype.vim | 5 | ||||
-rw-r--r-- | runtime/lua/vim/filetype.lua | 3 | ||||
-rw-r--r-- | runtime/scripts.vim | 10 | ||||
-rw-r--r-- | src/nvim/testdir/test_filetype_lua.vim | 3 | ||||
-rw-r--r-- | src/nvim/testdir/test_legacy_filetype.vim | 4 |
8 files changed, 45 insertions, 63 deletions
diff --git a/runtime/doc/filetype.txt b/runtime/doc/filetype.txt index 7f88216f43..7fff74a963 100644 --- a/runtime/doc/filetype.txt +++ b/runtime/doc/filetype.txt @@ -177,7 +177,9 @@ This means that the contents of compressed files are not inspected. If a file type that you want to use is not detected yet, there are a few ways to add it. In any way, it's better not to modify the $VIMRUNTIME/filetype.lua or $VIMRUNTIME/filetype.vim files. They will be overwritten when installing a -new version of Nvim. +new version of Nvim. The following explains the legacy Vim mechanism (enabled +if |do_legacy_filetype| is set). For Nvim's default mechanism, see +|vim.filetype.add()|. A. If you want to overrule all default file type checks. This works by writing one file for each filetype. The disadvantage is that @@ -236,39 +238,8 @@ C. If your file type can be detected by the file name or extension. Write this file as "filetype.vim" in your user runtime directory. For example, for Unix: > :w ~/.config/nvim/filetype.vim -< - Alternatively, create a file called "filetype.lua" that adds new - filetypes. - Example: > - vim.filetype.add({ - extension = { - foo = "fooscript", - }, - filename = { - [".foorc"] = "foorc", - }, - pattern = { - [".*/etc/foo/.*%.conf"] = "foorc", - }, - }) -< - See |vim.filetype.add()|. - *g:do_filetype_lua* - For now, Lua filetype detection is opt-in. You can enable it by adding - the following to your |init.vim|: > - let g:do_filetype_lua = 1 -< *g:did_load_filetypes* - In either case, the builtin filetype detection provided by Nvim can be - disabled by setting the did_load_filetypes global variable. If this - variable exists, $VIMRUNTIME/filetype.vim will not run. - Example: > - " Disable filetype.vim (but still load filetype.lua if enabled) - let g:did_load_filetypes = 0 - " Disable filetype.vim and filetype.lua - let g:did_load_filetypes = 1 - -< 3. To use the new filetype detection you must restart Vim. +< 3. To use the new filetype detection you must restart Vim. Your filetype.vim will be sourced before the default FileType autocommands have been installed. Your autocommands will match first, and the @@ -315,6 +286,16 @@ the 'runtimepath' for a directory to use. If there isn't one, set 'runtimepath' in the |system-vimrc|. Be careful to keep the default directories! + *g:do_legacy_filetype* +To disable Nvim's default filetype detection and revert to Vim's legacy +filetype detection, add the following to your |init.vim|: > + let g:do_legacy_filetype = 1 +< *g:did_load_filetypes* +The builtin filetype detection provided by Nvim can be disabled by setting +the `did_load_filetypes` global variable. If this variable exists, neither +the default `$VIMRUNTIME/filetype.lua` nor the legacy `$VIMRUNTIME/filetype.vim` +will run. + *plugin-details* The "plugin" directory can be in any of the directories in the 'runtimepath' option. All of these directories will be searched for plugins and they are diff --git a/runtime/doc/lua.txt b/runtime/doc/lua.txt index 3372bc90f7..089cf0ce9d 100644 --- a/runtime/doc/lua.txt +++ b/runtime/doc/lua.txt @@ -2022,8 +2022,8 @@ add({filetypes}) *vim.filetype.add()* See $VIMRUNTIME/lua/vim/filetype.lua for more examples. - Note that Lua filetype detection is only enabled when - |g:do_filetype_lua| is set to 1. + Note that Lua filetype detection is disabled when + |g:do_legacy_filetype| is set. Example: > diff --git a/runtime/filetype.lua b/runtime/filetype.lua index f1885a8b95..9f5b5fd0dc 100644 --- a/runtime/filetype.lua +++ b/runtime/filetype.lua @@ -1,11 +1,8 @@ -if vim.g.did_load_filetypes and vim.g.did_load_filetypes ~= 0 then - return -end - --- For now, make this opt-in with a global variable -if vim.g.do_filetype_lua ~= 1 then +-- Skip if legacy filetype is enabled or filetype detection is disabled +if vim.g.do_legacy_filetype or vim.g.did_load_filetypes then return end +vim.g.did_load_filetypes = 1 vim.api.nvim_create_augroup('filetypedetect', { clear = false }) @@ -38,21 +35,16 @@ if not vim.g.did_load_ftdetect then ]]) end --- Set a marker so that the ftdetect scripts are not sourced a second time by filetype.vim -vim.g.did_load_ftdetect = 1 - --- If filetype.vim is disabled, set up the autocmd to use scripts.vim -if vim.g.did_load_filetypes then - vim.api.nvim_create_autocmd({ 'BufRead', 'BufNewFile' }, { - group = 'filetypedetect', - command = "if !did_filetype() && expand('<amatch>') !~ g:ft_ignore_pat | runtime! scripts.vim | endif", - }) +-- 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", +}) - vim.api.nvim_create_autocmd('StdinReadPost', { - group = 'filetypedetect', - command = 'if !did_filetype() | runtime! scripts.vim | endif', - }) -end +vim.api.nvim_create_autocmd('StdinReadPost', { + group = 'filetypedetect', + command = 'if !did_filetype() | runtime! scripts.vim | endif', +}) if not vim.g.ft_ignore_pat then vim.g.ft_ignore_pat = '\\.\\(Z\\|gz\\|bz2\\|zip\\|tgz\\)$' diff --git a/runtime/filetype.vim b/runtime/filetype.vim index 44a3567983..7469f86cf5 100644 --- a/runtime/filetype.vim +++ b/runtime/filetype.vim @@ -3,6 +3,11 @@ " Maintainer: Bram Moolenaar <Bram@vim.org> " Last Change: 2022 Jul 5 +" Only run this if enabled +if !exists("do_legacy_filetype") + finish +endif + " Listen very carefully, I will say this only once if exists("did_load_filetypes") finish diff --git a/runtime/lua/vim/filetype.lua b/runtime/lua/vim/filetype.lua index fd47e12f07..8e86812347 100644 --- a/runtime/lua/vim/filetype.lua +++ b/runtime/lua/vim/filetype.lua @@ -2227,8 +2227,7 @@ end --- --- See $VIMRUNTIME/lua/vim/filetype.lua for more examples. --- ---- Note that Lua filetype detection is only enabled when |g:do_filetype_lua| is ---- set to 1. +--- Note that Lua filetype detection is disabled when |g:do_legacy_filetype| is set. --- --- Example: --- <pre> diff --git a/runtime/scripts.vim b/runtime/scripts.vim index a129c3467e..2d8bfdcb05 100644 --- a/runtime/scripts.vim +++ b/runtime/scripts.vim @@ -11,9 +11,13 @@ " 'ignorecase' option making a difference. Where case is to be ignored use " =~? instead. Do not use =~ anywhere. -" Only do the rest when not using Lua filetype detection -" and the FileType autocommand has not been triggered yet. -if exists("g:do_filetype_lua") && g:do_filetype_lua || did_filetype() +" Only run when using legacy filetype +if !exists('g:do_legacy_filetype') + finish +endif + +" Only do the rest when the FileType autocommand has not been triggered yet. +if did_filetype() finish endif diff --git a/src/nvim/testdir/test_filetype_lua.vim b/src/nvim/testdir/test_filetype_lua.vim deleted file mode 100644 index d9c0dcba9c..0000000000 --- a/src/nvim/testdir/test_filetype_lua.vim +++ /dev/null @@ -1,3 +0,0 @@ -let g:do_filetype_lua = 1 -let g:did_load_filetypes = 0 -source test_filetype.vim diff --git a/src/nvim/testdir/test_legacy_filetype.vim b/src/nvim/testdir/test_legacy_filetype.vim new file mode 100644 index 0000000000..772faaadb0 --- /dev/null +++ b/src/nvim/testdir/test_legacy_filetype.vim @@ -0,0 +1,4 @@ +let g:do_legacy_filetype = 1 +filetype on + +source test_filetype.vim |