From 925b14ce2b8fa732aa757089799bdd90a4133566 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Sat, 16 May 2026 16:23:22 +0800 Subject: [PATCH] vim-patch:9.2.0489: filetype: some Objective-C files are not recognized Problem: filetype: some Objective-C files are not recognized Solution: Add g:filetype_mm override variable, improve the objective c pattern detection (Keith Smiley). closes: vim/vim#20221 https://github.com/vim/vim/commit/bc7f736a393123c1349fe3fbe4fb48bfc56754b1 Co-authored-by: Keith Smiley --- runtime/doc/filetype.txt | 1 + runtime/lua/vim/filetype/detect.lua | 6 +++++- test/old/testdir/test_filetype.vim | 31 +++++++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/runtime/doc/filetype.txt b/runtime/doc/filetype.txt index 7135486a52..ca40c76ce4 100644 --- a/runtime/doc/filetype.txt +++ b/runtime/doc/filetype.txt @@ -158,6 +158,7 @@ variables can be used to overrule the filetype used for certain extensions: `*.int` g:filetype_int `*.lsl` g:filetype_lsl `*.m` g:filetype_m |ft-mathematica-syntax| + `*.mm` g:filetype_mm `*.mac` g:filetype_mac `*[mM]makefile,*.mk,*.mak,[mM]akefile*` g:make_flavor |ft-make-syntax| diff --git a/runtime/lua/vim/filetype/detect.lua b/runtime/lua/vim/filetype/detect.lua index e80bd66b57..24a5ca799e 100644 --- a/runtime/lua/vim/filetype/detect.lua +++ b/runtime/lua/vim/filetype/detect.lua @@ -1258,8 +1258,12 @@ end --- @type vim.filetype.mapfn function M.mm(_, bufnr) + if vim.g.filetype_mm then + return vim.g.filetype_mm + end + for _, line in ipairs(getlines(bufnr, 1, 20)) do - if matchregex(line, [[\c^\s*\(#\s*\(include\|import\)\>\|@import\>\|/\*\)]]) then + if matchregex(line, [[\c^\s*\(//\|#\s*\(include\|import\)\>\|@import\>\|/\*\)]]) then return 'objcpp' end end diff --git a/test/old/testdir/test_filetype.vim b/test/old/testdir/test_filetype.vim index f42a5e9510..c4a301c6f0 100644 --- a/test/old/testdir/test_filetype.vim +++ b/test/old/testdir/test_filetype.vim @@ -2054,6 +2054,37 @@ func Test_m4_file() endtry endfunc +func Test_mm_file() + filetype on + + call writefile(['#import "test.h"'], 'Xfile.mm', 'D') + split Xfile.mm + call assert_equal('objcpp', &filetype) + bwipe! + + call writefile(['// Objective-C++ line comment'], 'Xfile.mm', 'D') + split Xfile.mm + call assert_equal('objcpp', &filetype) + bwipe! + + call writefile(['.TH VIM 1 "YYYY Mth DD"'], 'Xfile.mm', 'D') + split Xfile.mm + call assert_equal('nroff', &filetype) + bwipe! + + try + let g:filetype_mm = 'objcpp' + call writefile(['.TH VIM 1 "YYYY Mth DD"'], 'Xfile_override.mm', 'D') + split Xfile_override.mm + call assert_equal('objcpp', &filetype) + bwipe! + finally + unlet! g:filetype_mm + endtry + + filetype off +endfunc + func Test_mod_file() filetype on