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

2cb42efc18

Co-authored-by: Eisuke Kawashima <e-kwsm@users.noreply.github.com>
This commit is contained in:
zeertzjq
2025-04-21 16:59:42 +08:00
parent 7da1639a14
commit 9fafdcb99c
3 changed files with 21 additions and 7 deletions

View File

@@ -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,

View File

@@ -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

View File

@@ -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)