# HG changeset patch # User Christian Brabandt # Date 1692562146 -7200 # Node ID 53bd850dd268608ea6419481e405d9d819d50560 # Parent 8c5377e802de759290b4893d8a29955f9561aa72 commit 19a3bc3addf9b4aa8150a01b11b4249c67d15d3b Author: Doug Kearns Date: Sun Aug 20 20:51:12 2023 +0200 patch 9.0.1773: cannot distinguish Forth and Fortran *.f files 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 Co-authored-by: Doug Kearns diff --git a/runtime/autoload/dist/ft.vim b/runtime/autoload/dist/ft.vim --- a/runtime/autoload/dist/ft.vim +++ b/runtime/autoload/dist/ft.vim @@ -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 diff --git a/runtime/doc/filetype.txt b/runtime/doc/filetype.txt --- a/runtime/doc/filetype.txt +++ b/runtime/doc/filetype.txt @@ -146,6 +146,7 @@ variables can be used to overrule the fi *.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| diff --git a/runtime/doc/syntax.txt b/runtime/doc/syntax.txt --- a/runtime/doc/syntax.txt +++ b/runtime/doc/syntax.txt @@ -1579,9 +1579,10 @@ example, FORM files, use this in your st 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" diff --git a/runtime/filetype.vim b/runtime/filetype.vim --- a/runtime/filetype.vim +++ b/runtime/filetype.vim @@ -724,16 +724,19 @@ au BufNewFile,BufRead auto.master setf 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 diff --git a/src/testdir/test_filetype.vim b/src/testdir/test_filetype.vim --- a/src/testdir/test_filetype.vim +++ b/src/testdir/test_filetype.vim @@ -246,7 +246,7 @@ def s:GetFilenameChecks(): dict