]> git.ipfire.org Git - thirdparty/vim.git/commitdiff
patch 9.0.1773: cannot distinguish Forth and Fortran *.f files v9.0.1773
authorDoug Kearns <dougkearns@gmail.com>
Sun, 20 Aug 2023 18:51:12 +0000 (20:51 +0200)
committerChristian Brabandt <cb@256bit.org>
Sun, 20 Aug 2023 18:53:47 +0000 (20:53 +0200)
Problem:  cannot distinguish Forth and Fortran *.f files
Solution: Add Filetype detection Code

Also add *.4th as a Forth filetype

closes: #12251

Signed-off-by: Christian Brabandt <cb@256bit.org>
Co-authored-by: Doug Kearns <dougkearns@gmail.com>
runtime/autoload/dist/ft.vim
runtime/doc/filetype.txt
runtime/doc/syntax.txt
runtime/filetype.vim
src/testdir/test_filetype.vim
src/version.c

index 9f53f684604c8bb0ce2c4f6c642c0b6ef04ba0f1..bc2125754a67d69cad715960c4c9148e2d01496f 100644 (file)
@@ -287,6 +287,37 @@ export def FTe()
   endif
 enddef
 
+def IsForth(): bool
+  var first_line = nextnonblank(1)
+
+  # SwiftForth block comment (line is usually filled with '-' or '=') or
+  # OPTIONAL (sometimes precedes the header comment)
+  if getline(first_line) =~? '^\%({\%(\s\|$\)\|OPTIONAL\s\)'
+    return true
+  endif
+
+  var n = first_line
+  while n < 100 && n <= line("$")
+    # Forth comments and colon definitions
+    if getline(n) =~ '^[:(\\] '
+      return true
+    endif
+    n += 1
+  endwhile
+  return false
+enddef
+
+# Distinguish between Forth and Fortran
+export def FTf()
+  if exists("g:filetype_f")
+    exe "setf " .. g:filetype_f
+  elseif IsForth()
+    setf forth
+  else
+    setf fortran
+  endif
+enddef
+
 export def FTfrm()
   if exists("g:filetype_frm")
     exe "setf " .. g:filetype_frm
@@ -302,21 +333,13 @@ export def FTfrm()
   endif
 enddef
 
-# Distinguish between Forth and F#.
-# Provided by Doug Kearns.
+# Distinguish between Forth and F#
 export def FTfs()
   if exists("g:filetype_fs")
     exe "setf " .. g:filetype_fs
+  elseif IsForth()
+    setf forth
   else
-    var n = 1
-    while n < 100 && n <= line("$")
-      # Forth comments and colon definitions
-      if getline(n) =~ "^[:(\\\\] "
-        setf forth
-        return
-      endif
-      n += 1
-    endwhile
     setf fsharp
   endif
 enddef
index 05574eba22fa3d1ab6b46924184fe931dc3e4c84..05047224f9f244fd08c4b521435d751af471f310 100644 (file)
@@ -146,6 +146,7 @@ variables can be used to overrule the filetype used for certain extensions:
        *.cls           g:filetype_cls
        *.csh           g:filetype_csh  |ft-csh-syntax|
        *.dat           g:filetype_dat
+       *.f             g:filetype_f    |ft-forth-syntax|
        *.frm           g:filetype_frm  |ft-form-syntax|
        *.fs            g:filetype_fs   |ft-forth-syntax|
        *.i             g:filetype_i    |ft-progress-syntax|
index 17f81391a9d30ea66dcc2f1bb59f894015563e7c..29a12396d4af6f320d79f846dfcd34b61589da7d 100644 (file)
@@ -1579,9 +1579,10 @@ example, FORM files, use this in your startup vimrc: >
 
 FORTH                                          *forth.vim* *ft-forth-syntax*
 
-Files matching "*.fs" could be F# or Forth.  If the automatic detection
-doesn't work for you, or you don't edit F# at all, use this in your
-startup vimrc: >
+Files matching "*.f" could be Fortran or Forth and those matching "*.fs" could
+be F# or Forth.  If the automatic detection doesn't work for you, or you don't
+edit F# or Fortran at all, use this in your startup vimrc: >
+   :let filetype_f  = "forth"
    :let filetype_fs = "forth"
 
 
index 16d4a131fd624842712976c311c53369c7e0dc37..a434503418dd28ecafa6f5cb929441ed7b91e369 100644 (file)
@@ -724,16 +724,19 @@ au BufNewFile,BufRead auto.master         setf conf
 au BufNewFile,BufRead *.mas,*.master           setf master
 
 " Forth
-au BufNewFile,BufRead *.ft,*.fth               setf forth
+au BufNewFile,BufRead *.ft,*.fth,*.4th         setf forth
 
 " Reva Forth
 au BufNewFile,BufRead *.frt                    setf reva
 
 " Fortran
 if has("fname_case")
-  au BufNewFile,BufRead *.F,*.FOR,*.FPP,*.FTN,*.F77,*.F90,*.F95,*.F03,*.F08     setf fortran
+  au BufNewFile,BufRead *.F,*.FOR,*.FPP,*.FTN,*.F77,*.F90,*.F95,*.F03,*.F08    setf fortran
 endif
-au BufNewFile,BufRead   *.f,*.for,*.fortran,*.fpp,*.ftn,*.f77,*.f90,*.f95,*.f03,*.f08  setf fortran
+au BufNewFile,BufRead *.for,*.fortran,*.fpp,*.ftn,*.f77,*.f90,*.f95,*.f03,*.f08        setf fortran
+
+" Fortran or Forth
+au BufNewFile,BufRead *.f                      call dist#ft#FTf()
 
 " Framescript
 au BufNewFile,BufRead *.fsl                    setf framescript
index 778736b36ccd1144c98f6905654c2fb5f55fefe2..a8ce88cca01e8e0226b606e71abd9ce2a1569a60 100644 (file)
@@ -246,7 +246,7 @@ def s:GetFilenameChecks(): dict<list<string>>
     fish: ['file.fish'],
     focexec: ['file.fex', 'file.focexec'],
     form: ['file.frm'],
-    forth: ['file.ft', 'file.fth'],
+    forth: ['file.ft', 'file.fth', 'file.4th'],
     fortran: ['file.f', 'file.for', 'file.fortran', 'file.fpp', 'file.ftn', 'file.f77', 'file.f90', 'file.f95', 'file.f03', 'file.f08'],
     fpcmake: ['file.fpc'],
     framescript: ['file.fsl'],
@@ -1265,6 +1265,54 @@ func Test_ex_file()
   filetype off
 endfunc
 
+func Test_f_file()
+  filetype on
+
+  call writefile(['looks like Fortran'], 'Xfile.f', 'D')
+  split Xfile.f
+  call assert_equal('fortran', &filetype)
+  bwipe!
+
+  let g:filetype_f = 'forth'
+  split Xfile.f
+  call assert_equal('forth', &filetype)
+  bwipe!
+  unlet g:filetype_f
+
+  " Test dist#ft#FTf()
+
+  " Forth
+
+  call writefile(['( Forth inline comment )'], 'Xfile.f')
+  split Xfile.f
+  call assert_equal('forth', &filetype)
+  bwipe!
+
+  call writefile(['\ Forth line comment'], 'Xfile.f')
+  split Xfile.f
+  call assert_equal('forth', &filetype)
+  bwipe!
+
+  call writefile([': squared ( n -- n^2 )', 'dup * ;'], 'Xfile.f')
+  split Xfile.f
+  call assert_equal('forth', &filetype)
+  bwipe!
+
+  " SwiftForth
+
+  call writefile(['{ ================', 'Header comment', '================ }'], 'Xfile.f')
+  split Xfile.f
+  call assert_equal('forth', &filetype)
+  bwipe!
+
+  call writefile(['OPTIONAL Maybe Descriptive text'], 'Xfile.f')
+  split Xfile.f
+  call assert_equal('forth', &filetype)
+  bwipe!
+
+  filetype off
+endfunc
+
 func Test_foam_file()
   filetype on
   call assert_true(mkdir('0', 'pR'))
@@ -1355,7 +1403,7 @@ func Test_fs_file()
 
   " Test dist#ft#FTfs()
 
-  " Forth (Gforth)
+  " Forth
 
   call writefile(['( Forth inline comment )'], 'Xfile.fs')
   split Xfile.fs
@@ -1372,6 +1420,18 @@ func Test_fs_file()
   call assert_equal('forth', &filetype)
   bwipe!
 
+  " SwiftForth
+
+  call writefile(['{ ================', 'Header comment', '================ }'], 'Xfile.fs')
+  split Xfile.fs
+  call assert_equal('forth', &filetype)
+  bwipe!
+
+  call writefile(['OPTIONAL Maybe Descriptive text'], 'Xfile.fs')
+  split Xfile.fs
+  call assert_equal('forth', &filetype)
+  bwipe!
+
   filetype off
 endfunc
 
index 13d27f4afd7ebc918d6e559bde45389a3f068c06..ffecee9e1791fc488aff721c93b84b711ccd76f5 100644 (file)
@@ -695,6 +695,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    1773,
 /**/
     1772,
 /**/