aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorzeertzjq <zeertzjq@outlook.com>2023-01-01 21:45:12 +0800
committerGitHub <noreply@github.com>2023-01-01 21:45:12 +0800
commit83e8723864e24d9bf542e779d9612d3ffd014dbf (patch)
tree5d29a292201ae7b2f46ae0d449c7c1559e377117
parent6ba34e21fee2a81677e8261dfeaf24c8cd320500 (diff)
parent85b7c8b004a36a15731a461e5e95f2c9fca732f1 (diff)
downloadrneovim-83e8723864e24d9bf542e779d9612d3ffd014dbf.tar.gz
rneovim-83e8723864e24d9bf542e779d9612d3ffd014dbf.tar.bz2
rneovim-83e8723864e24d9bf542e779d9612d3ffd014dbf.zip
Merge pull request #21597 from gi1242/tex-ft-detection
fix(filetype): make .tex filetype detection match Vim
-rw-r--r--runtime/lua/vim/filetype/detect.lua39
-rw-r--r--src/nvim/testdir/test_filetype.vim38
2 files changed, 51 insertions, 26 deletions
diff --git a/runtime/lua/vim/filetype/detect.lua b/runtime/lua/vim/filetype/detect.lua
index ee538dc8c7..edffdde9c7 100644
--- a/runtime/lua/vim/filetype/detect.lua
+++ b/runtime/lua/vim/filetype/detect.lua
@@ -1256,17 +1256,15 @@ end
-- 2. Check the first 1000 non-comment lines for LaTeX or ConTeXt keywords.
-- 3. Default to "plain" or to g:tex_flavor, can be set in user's vimrc.
function M.tex(path, bufnr)
- local format = getlines(bufnr, 1):find('^%%&%s*(%a+)')
- if format then
+ local matched, _, format = getlines(bufnr, 1):find('^%%&%s*(%a+)')
+ if matched then
format = format:lower():gsub('pdf', '', 1)
- if format == 'tex' then
- return 'tex'
- elseif format == 'plaintex' then
- return 'plaintex'
- end
elseif path:lower():find('tex/context/.*/.*%.tex') then
return 'context'
else
+ -- Default value, may be changed later:
+ format = vim.g.tex_flavor or 'plaintex'
+
local lpat = [[documentclass\>\|usepackage\>\|begin{\|newcommand\>\|renewcommand\>]]
local cpat =
[[start\a\+\|setup\a\+\|usemodule\|enablemode\|enableregime\|setvariables\|useencoding\|usesymbols\|stelle\a\+\|verwende\a\+\|stel\a\+\|gebruik\a\+\|usa\a\+\|imposta\a\+\|regle\a\+\|utilisemodule\>]]
@@ -1275,26 +1273,25 @@ function M.tex(path, bufnr)
-- Find first non-comment line
if not l:find('^%s*%%%S') then
-- Check the next thousand lines for a LaTeX or ConTeXt keyword.
- for _, line in ipairs(getlines(bufnr, i + 1, i + 1000)) do
- local lpat_match, cpat_match =
- matchregex(line, [[\c^\s*\\\%(]] .. lpat .. [[\)\|^\s*\\\(]] .. cpat .. [[\)]])
- if lpat_match then
+ for _, line in ipairs(getlines(bufnr, i, i + 1000)) do
+ if matchregex(line, [[\c^\s*\\\%(]] .. lpat .. [[\)]]) then
return 'tex'
- elseif cpat_match then
+ elseif matchregex(line, [[\c^\s*\\\%(]] .. cpat .. [[\)]]) then
return 'context'
end
end
end
end
- -- TODO: add AMSTeX, RevTex, others?
- if not vim.g.tex_flavor or vim.g.tex_flavor == 'plain' then
- return 'plaintex'
- elseif vim.g.tex_flavor == 'context' then
- return 'context'
- else
- -- Probably LaTeX
- return 'tex'
- end
+ end -- if matched
+
+ -- Translation from formats to file types. TODO: add AMSTeX, RevTex, others?
+ if format == 'plain' then
+ return 'plaintex'
+ elseif format == 'plaintex' or format == 'context' then
+ return format
+ else
+ -- Probably LaTeX
+ return 'tex'
end
end
diff --git a/src/nvim/testdir/test_filetype.vim b/src/nvim/testdir/test_filetype.vim
index cf155b3cb9..912e702631 100644
--- a/src/nvim/testdir/test_filetype.vim
+++ b/src/nvim/testdir/test_filetype.vim
@@ -1676,16 +1676,44 @@ endfunc
func Test_tex_file()
filetype on
- " only tests one case, should do more
+ call writefile(['%& pdflatex'], 'Xfile.tex')
+ split Xfile.tex
+ call assert_equal('tex', &filetype)
+ bwipe
+
+ call writefile(['\newcommand{\test}{some text}'], 'Xfile.tex')
+ split Xfile.tex
+ call assert_equal('tex', &filetype)
+ bwipe
+
+ " tex_flavor is unset
+ call writefile(['%& plain'], 'Xfile.tex')
+ split Xfile.tex
+ call assert_equal('plaintex', &filetype)
+ bwipe
+
+ let g:tex_flavor = 'plain'
+ call writefile(['just some text'], 'Xfile.tex')
+ split Xfile.tex
+ call assert_equal('plaintex', &filetype)
+ bwipe
+
let lines =<< trim END
- % This is a sentence.
+ % This is a comment.
- This is a sentence.
+ \usemodule[translate]
END
- call writefile(lines, "Xfile.tex")
+ call writefile(lines, 'Xfile.tex')
split Xfile.tex
- call assert_equal('plaintex', &filetype)
+ call assert_equal('context', &filetype)
+ bwipe
+
+ let g:tex_flavor = 'context'
+ call writefile(['just some text'], 'Xfile.tex')
+ split Xfile.tex
+ call assert_equal('context', &filetype)
bwipe
+ unlet g:tex_flavor
call delete('Xfile.tex')
filetype off