commit 9fafdcb99cc90919a4b56dcf11cbd197c0aad436
parent 7da1639a14f99c75a7946f1161238c6e4abf4319
Author: zeertzjq <zeertzjq@outlook.com>
Date: Mon, 21 Apr 2025 16:59:42 +0800
vim-patch:9.1.1327: filetype: nroff detection can be improved
Problem: filetype: nroff detection can be improved
Solution: improve nroff detection (Eisuke Kawashima)
- explicitly check roff comments and macros typically found in manpages
- do not try to detect alphabetically-sectioned files, except for n, as
nroff
- l: > 'l' happens to be a section for historical reasons
<https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=391977>
- n: e.g. /usr/share/man/mann/Tcl.n.gz
- o: unsure (perhaps fedora-specific)
- p: unsure (perhaps fedora-specific)
closes: vim/vim#17160
https://github.com/vim/vim/commit/2cb42efc18e46402ac84fd51afe54bb48be8aea7
Co-authored-by: Eisuke Kawashima <e-kwsm@users.noreply.github.com>
Diffstat:
3 files changed, 21 insertions(+), 7 deletions(-)
diff --git a/runtime/lua/vim/filetype.lua b/runtime/lua/vim/filetype.lua
@@ -696,7 +696,7 @@ local extension = {
lex = 'lex',
lxx = 'lex',
['l++'] = 'lex',
- l = detect_seq('lex', detect.nroff),
+ l = 'lex',
lhs = 'lhaskell',
lidr = 'lidris2',
ly = 'lilypond',
@@ -862,7 +862,6 @@ local extension = {
['3posix'] = detect.nroff,
['3type'] = detect.nroff,
n = detect.nroff,
- o = detect.nroff,
roff = 'nroff',
tmac = 'nroff',
man = 'nroff',
@@ -1409,7 +1408,7 @@ local extension = {
pp = detect.pp,
i = detect.i,
w = detect.progress_cweb,
- p = detect_seq(detect.nroff, detect.progress_pascal),
+ p = detect.progress_pascal,
pro = detect_seq(detect.proto, 'idlang'),
patch = detect.patch,
r = detect.r,
diff --git a/runtime/lua/vim/filetype/detect.lua b/runtime/lua/vim/filetype/detect.lua
@@ -1170,12 +1170,17 @@ function M.news(_, bufnr)
end
end
---- This function checks if one of the first five lines start with a dot. In
---- that case it is probably an nroff file.
+--- This function checks if one of the first five lines start with a typical
+--- nroff pattern in man files. In that case it is probably an nroff file.
--- @type vim.filetype.mapfn
function M.nroff(_, bufnr)
for _, line in ipairs(getlines(bufnr, 1, 5)) do
- if line:find('^%.%S%S?') then
+ if
+ matchregex(
+ line,
+ [[^\%([.']\s*\%(TH\|D[dt]\|S[Hh]\|d[es]1\?\|so\)\s\+\S\|[.'']\s*ig\>\|\%([.'']\s*\)\?\\"\)]]
+ )
+ then
return 'nroff'
end
end
diff --git a/test/old/testdir/test_filetype.vim b/test/old/testdir/test_filetype.vim
@@ -2921,11 +2921,21 @@ endfunc
func Test_nroff_file()
filetype on
- call writefile(['.TH vim 1 "YYYY Mth DD"'], 'Xfile.1', 'D')
+ call writefile(['.TH VIM 1 "YYYY Mth DD"'], 'Xfile.1', 'D')
split Xfile.1
call assert_equal('nroff', &filetype)
bwipe!
+ call writefile(['.Dd $Mdocdate$', '.Dt "DETECTION TEST" "7"', '.Os'], 'Xfile.7', 'D')
+ split Xfile.7
+ call assert_equal('nroff', &filetype)
+ bwipe!
+
+ call writefile(['''\" t'], 'Xfile.3p', 'D')
+ split Xfile.3p
+ call assert_equal('nroff', &filetype)
+ bwipe!
+
call writefile(['. /etc/profile'], 'Xfile.1', 'D')
split Xfile.1
call assert_notequal('nroff', &filetype)