Skip to content

Commit 3e54569

Browse files
committed
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 #1679, closes #1693)
1 parent ce876aa commit 3e54569

6 files changed

Lines changed: 84 additions & 9 deletions

File tree

runtime/filetype.vim

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
" Vim support file to detect file types
22
"
33
" Maintainer: Bram Moolenaar <[email protected]>
4-
" Last Change: 2017 May 27
4+
" Last Change: 2017 Jun 04
55

66
" Listen very carefully, I will say this only once
77
if exists("did_load_filetypes")
@@ -1181,14 +1181,21 @@ au BufNewFile,BufRead *.markdown,*.mdown,*.mkd,*.mkdn,*.mdwn,*.md setf markdown
11811181
" Mason
11821182
au BufNewFile,BufRead *.mason,*.mhtml,*.comp setf mason
11831183

1184-
" Matlab or Objective C
1184+
" Mathematica, Matlab, Murphi or Objective C
11851185
au BufNewFile,BufRead *.m call s:FTm()
11861186

11871187
func! s:FTm()
11881188
let n = 1
1189-
while n < 10
1189+
let saw_comment = 0 " Whether we've seen a multiline comment leader.
1190+
while n < 100
11901191
let line = getline(n)
1191-
if line =~ '^\s*\(#\s*\(include\|import\)\>\|@import\>\|/\*\|//\)'
1192+
if line =~ '^\s*/\*'
1193+
" /* ... */ is a comment in Objective C and Murphi, so we can't conclude
1194+
" it's either of them yet, but track this as a hint in case we don't see
1195+
" anything more definitive.
1196+
let saw_comment = 1
1197+
endif
1198+
if line =~ '^\s*\(#\s*\(include\|import\)\>\|@import\>\|//\)'
11921199
setf objc
11931200
return
11941201
endif
@@ -1200,11 +1207,23 @@ func! s:FTm()
12001207
setf mma
12011208
return
12021209
endif
1210+
if line =~ '^\c\s*\(\(type\|var\)\>\|--\)'
1211+
setf murphi
1212+
return
1213+
endif
12031214
let n = n + 1
12041215
endwhile
1205-
if exists("g:filetype_m")
1216+
1217+
if saw_comment
1218+
" We didn't see anything definitive, but this looks like either Objective C
1219+
" or Murphi based on the comment leader. Assume the former as it is more
1220+
" common.
1221+
setf objc
1222+
elseif exists("g:filetype_m")
1223+
" Use user specified default filetype for .m
12061224
exe "setf " . g:filetype_m
12071225
else
1226+
" Default is matlab
12081227
setf matlab
12091228
endif
12101229
endfunc
@@ -2777,12 +2796,12 @@ runtime! ftdetect/*.vim
27772796
" state.
27782797
augroup END
27792798

2780-
" Generic configuration file (check this last, it's just guessing!)
2799+
" Generic configuration file. Use FALLBACK, it's just guessing!
27812800
au filetypedetect BufNewFile,BufRead,StdinReadPost *
27822801
\ if !did_filetype() && expand("<amatch>") !~ g:ft_ignore_pat
27832802
\ && (getline(1) =~ '^#' || getline(2) =~ '^#' || getline(3) =~ '^#'
27842803
\ || getline(4) =~ '^#' || getline(5) =~ '^#') |
2785-
\ setf conf |
2804+
\ setf FALLBACK conf |
27862805
\ endif
27872806

27882807

src/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2133,6 +2133,7 @@ test_arglist \
21332133
test_feedkeys \
21342134
test_file_perm \
21352135
test_fileformat \
2136+
test_filetype \
21362137
test_filter_cmd \
21372138
test_filter_map \
21382139
test_findfile \

src/ex_docmd.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12172,13 +12172,22 @@ ex_filetype(exarg_T *eap)
1217212172
}
1217312173

1217412174
/*
12175-
* ":setfiletype {name}"
12175+
* ":setfiletype [FALLBACK] {name}"
1217612176
*/
1217712177
static void
1217812178
ex_setfiletype(exarg_T *eap)
1217912179
{
1218012180
if (!did_filetype)
12181-
set_option_value((char_u *)"filetype", 0L, eap->arg, OPT_LOCAL);
12181+
{
12182+
char_u *arg = eap->arg;
12183+
12184+
if (STRNCMP(arg, "FALLBACK ", 9) == 0)
12185+
arg += 9;
12186+
12187+
set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
12188+
if (arg != eap->arg)
12189+
did_filetype = FALSE;
12190+
}
1218212191
}
1218312192
#endif
1218412193

src/testdir/test_alot.vim

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ source test_expr.vim
1616
source test_feedkeys.vim
1717
source test_file_perm.vim
1818
source test_fileformat.vim
19+
source test_filetype.vim
1920
source test_filter_cmd.vim
2021
source test_filter_map.vim
2122
source test_findfile.vim

src/testdir/test_filetype.vim

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
" Test :setfiletype
2+
3+
func Test_detection()
4+
filetype on
5+
augroup filetypedetect
6+
au BufNewFile,BufRead * call assert_equal(1, did_filetype())
7+
augroup END
8+
new something.vim
9+
call assert_equal('vim', &filetype)
10+
11+
bwipe!
12+
filetype off
13+
endfunc
14+
15+
func Test_conf_type()
16+
filetype on
17+
call writefile(['# some comment', 'must be conf'], 'Xfile')
18+
augroup filetypedetect
19+
au BufNewFile,BufRead * call assert_equal(0, did_filetype())
20+
augroup END
21+
split Xfile
22+
call assert_equal('conf', &filetype)
23+
24+
bwipe!
25+
call delete('Xfile')
26+
filetype off
27+
endfunc
28+
29+
func Test_other_type()
30+
filetype on
31+
augroup filetypedetect
32+
au BufNewFile,BufRead * call assert_equal(0, did_filetype())
33+
au BufNewFile,BufRead Xfile setf testfile
34+
au BufNewFile,BufRead * call assert_equal(1, did_filetype())
35+
augroup END
36+
call writefile(['# some comment', 'must be conf'], 'Xfile')
37+
split Xfile
38+
call assert_equal('testfile', &filetype)
39+
40+
bwipe!
41+
call delete('Xfile')
42+
filetype off
43+
endfunc

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -764,6 +764,8 @@ static char *(features[]) =
764764

765765
static int included_patches[] =
766766
{ /* Add new patch number below this line */
767+
/**/
768+
613,
767769
/**/
768770
612,
769771
/**/

0 commit comments

Comments
 (0)