defaults: do :filetype stuff unless explicitly "off"

Until now, the default `:filetype ...` setup was skipped if the user
config touched `:filetype` in any way (including implicitly via `:syntax
on`).  No one needs that, and it's very confusing.

Instead, proceed with `:filetype ... on` unless the user explicitly
called `:filetype ... off`.

closes #7765
This commit is contained in:
Justin M. Keyes 2018-01-03 19:57:22 +01:00
parent dc4a9cbe85
commit 7c4bb23ff3
4 changed files with 63 additions and 55 deletions

View File

@ -24,10 +24,8 @@ Each time a new or existing file is edited, Vim will try to recognize the type
of the file and set the 'filetype' option. This will trigger the FileType of the file and set the 'filetype' option. This will trigger the FileType
event, which can be used to set the syntax highlighting, set options, etc. event, which can be used to set the syntax highlighting, set options, etc.
Detail: The ":filetype on" command will load one of these files: Detail: The ":filetype on" command will load this file:
Mac $VIMRUNTIME/filetype.vim $VIMRUNTIME/filetype.vim
MS-DOS $VIMRUNTIME\filetype.vim
Unix $VIMRUNTIME/filetype.vim
This file is a Vim script that defines autocommands for the This file is a Vim script that defines autocommands for the
BufNewFile and BufRead events. If the file type is not found by the BufNewFile and BufRead events. If the file type is not found by the
name, the file $VIMRUNTIME/scripts.vim is used to detect it from the name, the file $VIMRUNTIME/scripts.vim is used to detect it from the

View File

@ -439,15 +439,14 @@ accordingly. Vim proceeds in this order:
:runtime! filetype.vim :runtime! filetype.vim
:runtime! ftplugin.vim :runtime! ftplugin.vim
:runtime! indent.vim :runtime! indent.vim
< This step is skipped if ":filetype ..." was called before now or if < Skipped if ":filetype … off" was called or if the "-u NONE" command
the "-u NONE" command line argument was given. line argument was given.
5. Enable syntax highlighting. 5. Enable syntax highlighting.
This does the same as the command: > This does the same as the command: >
:runtime! syntax/syntax.vim :runtime! syntax/syntax.vim
< Note: This enables filetype detection even if ":filetype off" was < Skipped if ":syntax off" was called or if the "-u NONE" command
called before now. line argument was given.
This step is skipped if the "-u NONE" command line argument was given.
6. Load the plugin scripts. *load-plugins* 6. Load the plugin scripts. *load-plugins*
This does the same as the command: > This does the same as the command: >

View File

@ -9718,17 +9718,18 @@ static void ex_filetype(exarg_T *eap)
EMSG2(_(e_invarg2), arg); EMSG2(_(e_invarg2), arg);
} }
/// Do ":filetype plugin indent on" if user did not already do some /// Set all :filetype options ON if user did not explicitly set any to OFF.
/// permutation thereof.
void filetype_maybe_enable(void) void filetype_maybe_enable(void)
{ {
if (filetype_detect == kNone if (filetype_detect == kNone) {
&& filetype_plugin == kNone
&& filetype_indent == kNone) {
source_runtime((char_u *)FILETYPE_FILE, true); source_runtime((char_u *)FILETYPE_FILE, true);
filetype_detect = kTrue; filetype_detect = kTrue;
}
if (filetype_plugin == kNone) {
source_runtime((char_u *)FTPLUGIN_FILE, true); source_runtime((char_u *)FTPLUGIN_FILE, true);
filetype_plugin = kTrue; filetype_plugin = kTrue;
}
if (filetype_indent == kNone) {
source_runtime((char_u *)INDENT_FILE, true); source_runtime((char_u *)INDENT_FILE, true);
filetype_indent = kTrue; filetype_indent = kTrue;
} }

View File

@ -11,15 +11,6 @@ local neq = helpers.neq
local mkdir = helpers.mkdir local mkdir = helpers.mkdir
local rmdir = helpers.rmdir local rmdir = helpers.rmdir
local function init_session(...)
local args = { helpers.nvim_prog, '-i', 'NONE', '--embed',
'--cmd', helpers.nvim_set }
for _, v in ipairs({...}) do
table.insert(args, v)
end
helpers.set_session(helpers.spawn(args))
end
describe('startup defaults', function() describe('startup defaults', function()
describe(':filetype', function() describe(':filetype', function()
if helpers.pending_win32(pending) then return end if helpers.pending_win32(pending) then return end
@ -36,50 +27,70 @@ describe('startup defaults', function()
) )
end end
it('enabled by `-u NORC`', function() it('all ON after `-u NORC`', function()
init_session('-u', 'NORC') clear('-u', 'NORC')
expect_filetype( expect_filetype(
'filetype detection:ON plugin:ON indent:ON |') 'filetype detection:ON plugin:ON indent:ON |')
end) end)
it('disabled by `-u NONE`', function() it('all ON after `:syntax …` #7765', function()
init_session('-u', 'NONE') clear('-u', 'NORC', '--cmd', 'syntax on')
expect_filetype(
'filetype detection:ON plugin:ON indent:ON |')
clear('-u', 'NORC', '--cmd', 'syntax off')
expect_filetype(
'filetype detection:ON plugin:ON indent:ON |')
end)
it('all OFF after `-u NONE`', function()
clear('-u', 'NONE')
expect_filetype( expect_filetype(
'filetype detection:OFF plugin:OFF indent:OFF |') 'filetype detection:OFF plugin:OFF indent:OFF |')
end) end)
it('overridden by early `filetype on`', function() it('explicit OFF stays OFF', function()
init_session('-u', 'NORC', '--cmd', 'filetype on') clear('-u', 'NORC', '--cmd',
'syntax off | filetype off | filetype plugin indent off')
expect_filetype(
'filetype detection:OFF plugin:OFF indent:OFF |')
clear('-u', 'NORC', '--cmd', 'syntax off | filetype plugin indent off')
expect_filetype( expect_filetype(
'filetype detection:ON plugin:OFF indent:OFF |') 'filetype detection:ON plugin:OFF indent:OFF |')
end) clear('-u', 'NORC', '--cmd', 'filetype indent off')
it('overridden by early `filetype plugin on`', function()
init_session('-u', 'NORC', '--cmd', 'filetype plugin on')
expect_filetype( expect_filetype(
'filetype detection:ON plugin:ON indent:OFF |') 'filetype detection:ON plugin:ON indent:OFF |')
end) clear('-u', 'NORC', '--cmd', 'syntax off | filetype off')
it('overridden by early `filetype indent on`', function()
init_session('-u', 'NORC', '--cmd', 'filetype indent on')
expect_filetype( expect_filetype(
'filetype detection:ON plugin:OFF indent:ON |') 'filetype detection:OFF plugin:(on) indent:(on) |')
end) -- Swap the order.
clear('-u', 'NORC', '--cmd', 'filetype off | syntax off')
it('adjusted by late `filetype off`', function()
init_session('-u', 'NORC', '-c', 'filetype off')
expect_filetype( expect_filetype(
'filetype detection:OFF plugin:(on) indent:(on) |') 'filetype detection:OFF plugin:(on) indent:(on) |')
end) end)
it('adjusted by late `filetype plugin off`', function() it('all ON after early `:filetype … on`', function()
init_session('-u', 'NORC', '-c', 'filetype plugin off') -- `:filetype … on` should not change the defaults. #7765
-- Only an explicit `:filetype … off` sets OFF.
clear('-u', 'NORC', '--cmd', 'filetype on')
expect_filetype( expect_filetype(
'filetype detection:ON plugin:OFF indent:ON |') 'filetype detection:ON plugin:ON indent:ON |')
clear('-u', 'NORC', '--cmd', 'filetype plugin on')
expect_filetype(
'filetype detection:ON plugin:ON indent:ON |')
clear('-u', 'NORC', '--cmd', 'filetype indent on')
expect_filetype(
'filetype detection:ON plugin:ON indent:ON |')
end) end)
it('adjusted by late `filetype indent off`', function() it('late `:filetype … off` stays OFF', function()
init_session('-u', 'NORC', '-c', 'filetype indent off') clear('-u', 'NORC', '-c', 'filetype off')
expect_filetype(
'filetype detection:OFF plugin:(on) indent:(on) |')
clear('-u', 'NORC', '-c', 'filetype plugin off')
expect_filetype(
'filetype detection:ON plugin:OFF indent:ON |')
clear('-u', 'NORC', '-c', 'filetype indent off')
expect_filetype( expect_filetype(
'filetype detection:ON plugin:ON indent:OFF |') 'filetype detection:ON plugin:ON indent:OFF |')
end) end)
@ -87,22 +98,21 @@ describe('startup defaults', function()
describe('syntax', function() describe('syntax', function()
it('enabled by `-u NORC`', function() it('enabled by `-u NORC`', function()
init_session('-u', 'NORC') clear('-u', 'NORC')
eq(1, eval('g:syntax_on')) eq(1, eval('g:syntax_on'))
end) end)
it('disabled by `-u NONE`', function() it('disabled by `-u NONE`', function()
init_session('-u', 'NONE') clear('-u', 'NONE')
eq(0, eval('exists("g:syntax_on")')) eq(0, eval('exists("g:syntax_on")'))
end) end)
it('overridden by early `syntax off`', function() it('`:syntax off` stays off', function()
init_session('-u', 'NORC', '--cmd', 'syntax off') -- early
clear('-u', 'NORC', '--cmd', 'syntax off')
eq(0, eval('exists("g:syntax_on")')) eq(0, eval('exists("g:syntax_on")'))
end) -- late
clear('-u', 'NORC', '-c', 'syntax off')
it('adjusted by late `syntax off`', function()
init_session('-u', 'NORC', '-c', 'syntax off')
eq(0, eval('exists("g:syntax_on")')) eq(0, eval('exists("g:syntax_on")'))
end) end)
end) end)