]> granicus.if.org Git - vim/commitdiff
patch 8.0.0613: the conf filetype is used before ftdetect from packages v8.0.0613
authorBram Moolenaar <Bram@vim.org>
Sun, 4 Jun 2017 17:00:32 +0000 (19:00 +0200)
committerBram Moolenaar <Bram@vim.org>
Sun, 4 Jun 2017 17:00:32 +0000 (19:00 +0200)
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)

runtime/filetype.vim
src/Makefile
src/ex_docmd.c
src/testdir/test_alot.vim
src/testdir/test_filetype.vim [new file with mode: 0644]
src/version.c

index 8758dd2f75f4c19928020f1320ba7c55f0d399ed..9878236b62da6a7ea9bd7afd79648e81c41c77ec 100644 (file)
@@ -1,7 +1,7 @@
 " Vim support file to detect file types
 "
 " Maintainer:  Bram Moolenaar <Bram@vim.org>
-" Last Change: 2017 May 27
+" Last Change: 2017 Jun 04
 
 " Listen very carefully, I will say this only once
 if exists("did_load_filetypes")
@@ -1181,14 +1181,21 @@ au BufNewFile,BufRead *.markdown,*.mdown,*.mkd,*.mkdn,*.mdwn,*.md  setf markdown
 " 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()
 
 func! s:FTm()
   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)
-    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
       return
     endif
@@ -1200,11 +1207,23 @@ func! s:FTm()
       setf mma
       return
     endif
+    if line =~ '^\c\s*\(\(type\|var\)\>\|--\)'
+      setf murphi
+      return
+    endif
     let n = n + 1
   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
   else
+    " Default is matlab
     setf matlab
   endif
 endfunc
@@ -2777,12 +2796,12 @@ runtime! ftdetect/*.vim
 " state.
 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 *
        \ if !did_filetype() && expand("<amatch>") !~ g:ft_ignore_pat
        \    && (getline(1) =~ '^#' || getline(2) =~ '^#' || getline(3) =~ '^#'
        \       || getline(4) =~ '^#' || getline(5) =~ '^#') |
-       \   setf conf |
+       \   setf FALLBACK conf |
        \ endif
 
 
index ddb61dc08a9f604ebc6e503565d0923eea2a1ff5..8fa004e8f74692f73ff5e592f03e4f5545b0e979 100644 (file)
@@ -2133,6 +2133,7 @@ test_arglist \
        test_feedkeys \
        test_file_perm \
        test_fileformat \
+       test_filetype \
        test_filter_cmd \
        test_filter_map \
        test_findfile \
index 13d5fe495ee0722828e26bed94e6d57fed8b581f..2c9c8782e748740525e3675f1ac70046929e38fc 100644 (file)
@@ -12172,13 +12172,22 @@ ex_filetype(exarg_T *eap)
 }
 
 /*
- * ":setfiletype {name}"
+ * ":setfiletype [FALLBACK] {name}"
  */
     static void
 ex_setfiletype(exarg_T *eap)
 {
     if (!did_filetype)
-       set_option_value((char_u *)"filetype", 0L, eap->arg, OPT_LOCAL);
+    {
+       char_u *arg = eap->arg;
+
+       if (STRNCMP(arg, "FALLBACK ", 9) == 0)
+           arg += 9;
+
+       set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
+       if (arg != eap->arg)
+           did_filetype = FALSE;
+    }
 }
 #endif
 
index e961e9945111cf14bcea9689c96564b12b8460c5..fec22714e704dd7315c71a5345b1247af76c10f6 100644 (file)
@@ -16,6 +16,7 @@ source test_expr.vim
 source test_feedkeys.vim
 source test_file_perm.vim
 source test_fileformat.vim
+source test_filetype.vim
 source test_filter_cmd.vim
 source test_filter_map.vim
 source test_findfile.vim
diff --git a/src/testdir/test_filetype.vim b/src/testdir/test_filetype.vim
new file mode 100644 (file)
index 0000000..818603b
--- /dev/null
@@ -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
index 91cd500526e6d5fd5bf75618ebe7db7e8a4b4d7a..f5b5732ad7e24a5c1731e09ac76932e923672199 100644 (file)
@@ -764,6 +764,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    613,
 /**/
     612,
 /**/