]> granicus.if.org Git - vim/commitdiff
patch 8.2.3399: Octave files are not recognized v8.2.3399
authorBram Moolenaar <Bram@vim.org>
Fri, 3 Sep 2021 17:21:36 +0000 (19:21 +0200)
committerBram Moolenaar <Bram@vim.org>
Fri, 3 Sep 2021 17:21:36 +0000 (19:21 +0200)
Problem:    Octave files are not recognized.
Solution:   Detect Octave files. (Doug Kearns)

runtime/autoload/dist/ft.vim
runtime/doc/filetype.txt
runtime/filetype.vim
src/testdir/test_filetype.vim
src/version.c

index ac8065911370dfe614648736a013c1efc17760af..48a9657cf5f7f75dc7a788192144847dd8f9d184 100644 (file)
@@ -264,6 +264,13 @@ func dist#ft#ProtoCheck(default)
 endfunc
 
 func dist#ft#FTm()
+  if exists("g:filetype_m")
+    exe "setf " . g:filetype_m
+    return
+  endif
+
+  let octave_block_terminators = '\<end\%(_try_catch\|classdef\|enumeration\|events\|for\|function\|if\|methods\|parfor\|properties\|switch\|while\)\>'
+
   let n = 1
   let saw_comment = 0 " Whether we've seen a multiline comment leader.
   while n < 100
@@ -278,6 +285,13 @@ func dist#ft#FTm()
       setf objc
       return
     endif
+    if line =~ '^\s*\%(#\|%!\|[#%]{\=\s*$\)' ||
+         \ line =~ '^\s*unwind_protect\>' ||
+         \ line =~ '\%(^\|;\)\s*' .. octave_block_terminators
+      setf octave
+      return
+    endif
+    " TODO: could be Matlab or Octave
     if line =~ '^\s*%'
       setf matlab
       return
@@ -298,11 +312,8 @@ func dist#ft#FTm()
     " 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
+    " Default is Matlab
     setf matlab
   endif
 endfunc
index 8295d67780e3ce5b4f8ed3695e6e90a49e449fe2..75e967920fce105ec90ff362d40a958c086fa5be 100644 (file)
@@ -146,6 +146,7 @@ can be used to overrule the filetype used for certain extensions:
        *.inc           g:filetype_inc
        *.w             g:filetype_w    |ft-cweb-syntax|
        *.i             g:filetype_i    |ft-progress-syntax|
+       *.m             g:filetype_m    |ft-mathematica-syntax|
        *.p             g:filetype_p    |ft-pascal-syntax|
        *.pp            g:filetype_pp   |ft-pascal-syntax|
        *.sh            g:bash_is_sh    |ft-sh-syntax|
index 06038e94d1f6bff46b98ef64caa3907fa0c4d582..0975ccd2d4273e1d14830dd62b1ccf43b45a74eb 100644 (file)
@@ -1029,7 +1029,7 @@ au BufNewFile,BufRead *.markdown,*.mdown,*.mkd,*.mkdn,*.mdwn,*.md  setf markdown
 " Mason
 au BufNewFile,BufRead *.mason,*.mhtml,*.comp   setf mason
 
-" Mathematica, Matlab, Murphi or Objective C
+" Mathematica, Matlab, Murphi, Objective C or Octave
 au BufNewFile,BufRead *.m                      call dist#ft#FTm()
 
 " Mathematica notebook
@@ -1175,6 +1175,9 @@ au BufNewFile,BufRead *.ml,*.mli,*.mll,*.mly,.ocamlinit,*.mlt,*.mlp,*.mlip,*.mli
 " Occam
 au BufNewFile,BufRead *.occ                    setf occam
 
+" Octave
+au BufNewFile,BufRead octave.conf,.octaverc,octaverc   setf octave
+
 " Omnimark
 au BufNewFile,BufRead *.xom,*.xin              setf omnimark
 
index a30ac2cfa4d7682067d23eeab28900e067c8722d..94db223f9fcbb467bbe574b50a7c9220d2ba4b70 100644 (file)
@@ -295,6 +295,7 @@ let s:filename_checks = {
     \ 'lss': ['file.lss'],
     \ 'lua': ['file.lua', 'file.rockspec', 'file.nse'],
     \ 'lynx': ['lynx.cfg'],
+    \ 'matlab': ['file.m'],
     \ 'm3build': ['m3makefile', 'm3overrides'],
     \ 'm3quake': ['file.quake', 'cm3.cfg'],
     \ 'm4': ['file.at'],
@@ -351,6 +352,7 @@ let s:filename_checks = {
     \ 'obj': ['file.obj'],
     \ 'ocaml': ['file.ml', 'file.mli', 'file.mll', 'file.mly', '.ocamlinit', 'file.mlt', 'file.mlp', 'file.mlip', 'file.mli.cppo', 'file.ml.cppo'],
     \ 'occam': ['file.occ'],
+    \ 'octave': ['octaverc', '.octaverc', 'octave.conf'],
     \ 'omnimark': ['file.xom', 'file.xin'],
     \ 'opam': ['opam', 'file.opam', 'file.opam.template'],
     \ 'openroad': ['file.or'],
@@ -851,4 +853,96 @@ func Test_dsl_file()
   filetype off
 endfunc
 
+func Test_m_file()
+  filetype on
+
+  call writefile(['looks like Matlab'], 'Xfile.m')
+  split Xfile.m
+  call assert_equal('matlab', &filetype)
+  bwipe!
+
+  let g:filetype_m = 'octave'
+  split Xfile.m
+  call assert_equal('octave', &filetype)
+  bwipe!
+  unlet g:filetype_m
+
+  " Test dist#ft#FTm()
+
+  " Objective-C
+
+  call writefile(['// Objective-C line comment'], 'Xfile.m')
+  split Xfile.m
+  call assert_equal('objc', &filetype)
+  bwipe!
+
+  call writefile(['/* Objective-C block comment */'], 'Xfile.m')
+  split Xfile.m
+  call assert_equal('objc', &filetype)
+  bwipe!
+
+  call writefile(['#import "test.m"'], 'Xfile.m')
+  split Xfile.m
+  call assert_equal('objc', &filetype)
+  bwipe!
+
+  " Octave
+
+  call writefile(['# Octave line comment'], 'Xfile.m')
+  split Xfile.m
+  call assert_equal('octave', &filetype)
+  bwipe!
+
+  call writefile(['#{', 'Octave block comment',  '#}'], 'Xfile.m')
+  split Xfile.m
+  call assert_equal('octave', &filetype)
+  bwipe!
+
+  call writefile(['%{', 'Octave block comment', '%}'], 'Xfile.m')
+  split Xfile.m
+  call assert_equal('octave', &filetype)
+  bwipe!
+
+  call writefile(['%!test "Octave test"'], 'Xfile.m')
+  split Xfile.m
+  call assert_equal('octave', &filetype)
+  bwipe!
+
+  call writefile(['unwind_protect'], 'Xfile.m')
+  split Xfile.m
+  call assert_equal('octave', &filetype)
+  bwipe!
+
+  call writefile(['function test(); 42; endfunction'], 'Xfile.m')
+  split Xfile.m
+  call assert_equal('octave', &filetype)
+  bwipe!
+
+  " Mathematica
+
+  call writefile(['(* Mathematica comment'], 'Xfile.m')
+  split Xfile.m
+  call assert_equal('mma', &filetype)
+  bwipe!
+
+  " Murphi
+
+  call writefile(['-- Murphi comment'], 'Xfile.m')
+  split Xfile.m
+  call assert_equal('murphi', &filetype)
+  bwipe!
+
+  call writefile(['/* Murphi block comment */', 'Type'], 'Xfile.m')
+  split Xfile.m
+  call assert_equal('murphi', &filetype)
+  bwipe!
+
+  call writefile(['Type'], 'Xfile.m')
+  split Xfile.m
+  call assert_equal('murphi', &filetype)
+  bwipe!
+
+  call delete('Xfile.m')
+  filetype off
+endfunc
 " vim: shiftwidth=2 sts=2 expandtab
index 732ddee41f85a3c3f4c4b69a5620fd52d8cc7212..a9219db153e75fe01c7d0fdb06ff88f88d47a68b 100644 (file)
@@ -755,6 +755,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    3399,
 /**/
     3398,
 /**/