vim-patch:8.0.0613: the conf filetype is used before ftdetect from packages

Problem:    The conf filetype detection is done before ftdetect scripts from
            packages that are added later.
Solution:   Add the FALLBACK argument to :setfiletype. (closes vim/vim#1679,
            closes vim/vim#1693)

3e54569b17
This commit is contained in:
Justin M. Keyes 2018-02-03 11:58:44 +01:00
parent a1ee06a099
commit 16a4168364
4 changed files with 80 additions and 10 deletions

View File

@ -1183,14 +1183,21 @@ au BufNewFile,BufRead *.markdown,*.mdown,*.mkd,*.mkdn,*.mdwn,*.md setf markdown
" Mason " Mason
au BufNewFile,BufRead *.mason,*.mhtml,*.comp setf mason au BufNewFile,BufRead *.mason,*.mhtml,*.comp setf mason
" Matlab or Objective C " Mathematica, Matlab, Murphi or Objective C
au BufNewFile,BufRead *.m call s:FTm() au BufNewFile,BufRead *.m call s:FTm()
func! s:FTm() func! s:FTm()
let n = 1 let n = 1
while n < 10 let saw_comment = 0 " Whether we've seen a multiline comment leader.
while n < 100
let line = getline(n) let line = getline(n)
if line =~ '^\s*\(#\s*\(include\|import\)\>\|@import\>\|/\*\|//\)' if line =~ '^\s*/\*'
" /* ... */ is a comment in Objective C and Murphi, so we can't conclude
" it's either of them yet, but track this as a hint in case we don't see
" anything more definitive.
let saw_comment = 1
endif
if line =~ '^\s*\(#\s*\(include\|import\)\>\|@import\>\|//\)'
setf objc setf objc
return return
endif endif
@ -1202,11 +1209,23 @@ func! s:FTm()
setf mma setf mma
return return
endif endif
if line =~ '^\c\s*\(\(type\|var\)\>\|--\)'
setf murphi
return
endif
let n = n + 1 let n = n + 1
endwhile endwhile
if exists("g:filetype_m")
if saw_comment
" We didn't see anything definitive, but this looks like either Objective C
" or Murphi based on the comment leader. Assume the former as it is more
" common.
setf objc
elseif exists("g:filetype_m")
" Use user specified default filetype for .m
exe "setf " . g:filetype_m exe "setf " . g:filetype_m
else else
" Default is matlab
setf matlab setf matlab
endif endif
endfunc endfunc
@ -2792,12 +2811,12 @@ runtime! ftdetect/*.vim
" state. " state.
augroup END augroup END
" Generic configuration file (check this last, it's just guessing!) " Generic configuration file. Use FALLBACK, it's just guessing!
au filetypedetect BufNewFile,BufRead,StdinReadPost * au filetypedetect BufNewFile,BufRead,StdinReadPost *
\ if !did_filetype() && expand("<amatch>") !~ g:ft_ignore_pat \ if !did_filetype() && expand("<amatch>") !~ g:ft_ignore_pat
\ && (getline(1) =~ '^#' || getline(2) =~ '^#' || getline(3) =~ '^#' \ && (getline(1) =~ '^#' || getline(2) =~ '^#' || getline(3) =~ '^#'
\ || getline(4) =~ '^#' || getline(5) =~ '^#') | \ || getline(4) =~ '^#' || getline(5) =~ '^#') |
\ setf conf | \ setf FALLBACK conf |
\ endif \ endif

View File

@ -9743,13 +9743,20 @@ void filetype_maybe_enable(void)
} }
} }
/* /// ":setfiletype [FALLBACK] {name}"
* ":setfiletype {name}"
*/
static void ex_setfiletype(exarg_T *eap) static void ex_setfiletype(exarg_T *eap)
{ {
if (!did_filetype) { if (!did_filetype) {
set_option_value("filetype", 0L, (char *)eap->arg, OPT_LOCAL); char_u *arg = eap->arg;
if (STRNCMP(arg, "FALLBACK ", 9) == 0) {
arg += 9;
}
set_option_value("filetype", 0L, (char *)arg, OPT_LOCAL);
if (arg != eap->arg) {
did_filetype = false;
}
} }
} }

View File

@ -19,6 +19,7 @@ source test_ga.vim
source test_goto.vim source test_goto.vim
source test_jumps.vim source test_jumps.vim
source test_fileformat.vim source test_fileformat.vim
source test_filetype.vim
source test_lambda.vim source test_lambda.vim
source test_menu.vim source test_menu.vim
source test_mapping.vim source test_mapping.vim

View File

@ -0,0 +1,43 @@
" Test :setfiletype
func Test_detection()
filetype on
augroup filetypedetect
au BufNewFile,BufRead * call assert_equal(1, did_filetype())
augroup END
new something.vim
call assert_equal('vim', &filetype)
bwipe!
filetype off
endfunc
func Test_conf_type()
filetype on
call writefile(['# some comment', 'must be conf'], 'Xfile')
augroup filetypedetect
au BufNewFile,BufRead * call assert_equal(0, did_filetype())
augroup END
split Xfile
call assert_equal('conf', &filetype)
bwipe!
call delete('Xfile')
filetype off
endfunc
func Test_other_type()
filetype on
augroup filetypedetect
au BufNewFile,BufRead * call assert_equal(0, did_filetype())
au BufNewFile,BufRead Xfile setf testfile
au BufNewFile,BufRead * call assert_equal(1, did_filetype())
augroup END
call writefile(['# some comment', 'must be conf'], 'Xfile')
split Xfile
call assert_equal('testfile', &filetype)
bwipe!
call delete('Xfile')
filetype off
endfunc