vim-patch:9.0.1773: cannot distinguish Forth and Fortran *.f files (#24841)

Problem:  cannot distinguish Forth and Fortran *.f files
Solution: Add Filetype detection Code

Also add *.4th as a Forth filetype

closes: vim/vim#12251

19a3bc3add

Don't remove filetype files from Vim patches:
- filetype.vim, script.vim, ft.vim usually contain useful changes
- script.vim and ft.vim don't even have their paths spelled correctly

Co-authored-by: Doug Kearns <dougkearns@gmail.com>
This commit is contained in:
zeertzjq
2023-08-23 19:32:11 +08:00
committed by GitHub
parent 2234b84a1b
commit 08fa71fd27
6 changed files with 108 additions and 15 deletions

View File

@ -144,6 +144,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|

View File

@ -1542,9 +1542,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"

View File

@ -398,6 +398,7 @@ local extension = {
EXW = detect.euphoria,
ex = detect.ex,
exp = 'expect',
f = detect.f,
factor = 'factor',
fal = 'falcon',
fan = 'fan',
@ -410,8 +411,9 @@ local extension = {
fish = 'fish',
focexec = 'focexec',
fex = 'focexec',
fth = 'forth',
ft = 'forth',
fth = 'forth',
['4th'] = 'forth',
FOR = 'fortran',
f77 = 'fortran',
f03 = 'fortran',
@ -427,7 +429,6 @@ local extension = {
F77 = 'fortran',
f95 = 'fortran',
FPP = 'fortran',
f = 'fortran',
F = 'fortran',
F08 = 'fortran',
f08 = 'fortran',

View File

@ -473,6 +473,38 @@ function M.ex(_, bufnr)
end
end
--- @param bufnr integer
--- @return boolean
local function is_forth(bufnr)
local first_line = nextnonblank(bufnr, 1)
-- SwiftForth block comment (line is usually filled with '-' or '=') or
-- OPTIONAL (sometimes precedes the header comment)
if first_line and findany(first_line:lower(), { '^%{%s', '^%{$', '^optional%s' }) then
return true
end
for _, line in ipairs(getlines(bufnr, 1, 100)) do
-- Forth comments and colon definitions
if line:find('^[:(\\] ') then
return true
end
end
return false
end
-- Distinguish between Forth and Fortran
--- @type vim.filetype.mapfn
function M.f(_, bufnr)
if vim.g.filetype_f then
return vim.g.filetype_f
end
if is_forth(bufnr) then
return 'forth'
end
return 'fortran'
end
-- This function checks the first 15 lines for appearance of 'FoamFile'
-- and then 'object' in a following line.
-- In that case, it's probably an OpenFOAM file
@ -518,16 +550,14 @@ function M.fvwm_v2(path, _)
end
end
-- Distinguish between Forth and F#.
-- Distinguish between Forth and F#
--- @type vim.filetype.mapfn
function M.fs(_, bufnr)
if vim.g.filetype_fs then
return vim.g.filetype_fs
end
for _, line in ipairs(getlines(bufnr, 1, 100)) do
if line:find('^[:(\\] ') then
return 'forth'
end
if is_forth(bufnr) then
return 'forth'
end
return 'fsharp'
end

View File

@ -202,9 +202,9 @@ preprocess_patch() {
# Remove *.proto, Make*, INSTALL*, gui_*, beval.*, some if_*, gvim, libvterm, tee, VisVim, xpm, xxd
local na_src='auto\|configure.*\|GvimExt\|hardcopy.*\|libvterm\|proto\|tee\|VisVim\|xpm\|xxd\|Make.*\|INSTALL.*\|beval.*\|gui.*\|if_cscop\|if_lua\|if_mzsch\|if_olepp\|if_ole\|if_perl\|if_py\|if_ruby\|if_tcl\|if_xcmdsrv'
2>/dev/null $nvim --cmd 'set dir=/tmp' +'g@^diff --git a/src/\S*\<\%(testdir/\)\@<!\%('"${na_src}"'\)\>@norm! d/\v(^diff)|%$
' +w +q "$file"
# Remove runtime files ported to Lua.
' +w +q "$file"
# Remove runtime/print/
local na_rt='print\/.*'
2>/dev/null $nvim --cmd 'set dir=/tmp' +'g@^diff --git a/runtime/\<\%('"${na_rt}"'\)\>@norm! d/\v(^diff)|%$' +w +q "$file"

View File

@ -236,7 +236,7 @@ func s:GetFilenameChecks() abort
\ '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'],
@ -1173,6 +1173,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'))
@ -1263,7 +1311,7 @@ func Test_fs_file()
" Test dist#ft#FTfs()
" Forth (Gforth)
" Forth
call writefile(['( Forth inline comment )'], 'Xfile.fs')
split Xfile.fs
@ -1280,6 +1328,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