mirror of
https://github.com/neovim/neovim.git
synced 2025-02-25 18:55:25 -06:00
Merge pull request #21597 from gi1242/tex-ft-detection
fix(filetype): make .tex filetype detection match Vim
This commit is contained in:
commit
83e8723864
@ -1256,17 +1256,15 @@ end
|
|||||||
-- 2. Check the first 1000 non-comment lines for LaTeX or ConTeXt keywords.
|
-- 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.
|
-- 3. Default to "plain" or to g:tex_flavor, can be set in user's vimrc.
|
||||||
function M.tex(path, bufnr)
|
function M.tex(path, bufnr)
|
||||||
local format = getlines(bufnr, 1):find('^%%&%s*(%a+)')
|
local matched, _, format = getlines(bufnr, 1):find('^%%&%s*(%a+)')
|
||||||
if format then
|
if matched then
|
||||||
format = format:lower():gsub('pdf', '', 1)
|
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
|
elseif path:lower():find('tex/context/.*/.*%.tex') then
|
||||||
return 'context'
|
return 'context'
|
||||||
else
|
else
|
||||||
|
-- Default value, may be changed later:
|
||||||
|
format = vim.g.tex_flavor or 'plaintex'
|
||||||
|
|
||||||
local lpat = [[documentclass\>\|usepackage\>\|begin{\|newcommand\>\|renewcommand\>]]
|
local lpat = [[documentclass\>\|usepackage\>\|begin{\|newcommand\>\|renewcommand\>]]
|
||||||
local cpat =
|
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\>]]
|
[[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
|
-- Find first non-comment line
|
||||||
if not l:find('^%s*%%%S') then
|
if not l:find('^%s*%%%S') then
|
||||||
-- Check the next thousand lines for a LaTeX or ConTeXt keyword.
|
-- Check the next thousand lines for a LaTeX or ConTeXt keyword.
|
||||||
for _, line in ipairs(getlines(bufnr, i + 1, i + 1000)) do
|
for _, line in ipairs(getlines(bufnr, i, i + 1000)) do
|
||||||
local lpat_match, cpat_match =
|
if matchregex(line, [[\c^\s*\\\%(]] .. lpat .. [[\)]]) then
|
||||||
matchregex(line, [[\c^\s*\\\%(]] .. lpat .. [[\)\|^\s*\\\(]] .. cpat .. [[\)]])
|
|
||||||
if lpat_match then
|
|
||||||
return 'tex'
|
return 'tex'
|
||||||
elseif cpat_match then
|
elseif matchregex(line, [[\c^\s*\\\%(]] .. cpat .. [[\)]]) then
|
||||||
return 'context'
|
return 'context'
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
-- TODO: add AMSTeX, RevTex, others?
|
end -- if matched
|
||||||
if not vim.g.tex_flavor or vim.g.tex_flavor == 'plain' then
|
|
||||||
return 'plaintex'
|
-- Translation from formats to file types. TODO: add AMSTeX, RevTex, others?
|
||||||
elseif vim.g.tex_flavor == 'context' then
|
if format == 'plain' then
|
||||||
return 'context'
|
return 'plaintex'
|
||||||
else
|
elseif format == 'plaintex' or format == 'context' then
|
||||||
-- Probably LaTeX
|
return format
|
||||||
return 'tex'
|
else
|
||||||
end
|
-- Probably LaTeX
|
||||||
|
return 'tex'
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -1676,17 +1676,45 @@ endfunc
|
|||||||
func Test_tex_file()
|
func Test_tex_file()
|
||||||
filetype on
|
filetype on
|
||||||
|
|
||||||
" only tests one case, should do more
|
call writefile(['%& pdflatex'], 'Xfile.tex')
|
||||||
let lines =<< trim END
|
split Xfile.tex
|
||||||
% This is a sentence.
|
call assert_equal('tex', &filetype)
|
||||||
|
bwipe
|
||||||
|
|
||||||
This is a sentence.
|
call writefile(['\newcommand{\test}{some text}'], 'Xfile.tex')
|
||||||
END
|
split Xfile.tex
|
||||||
call writefile(lines, "Xfile.tex")
|
call assert_equal('tex', &filetype)
|
||||||
|
bwipe
|
||||||
|
|
||||||
|
" tex_flavor is unset
|
||||||
|
call writefile(['%& plain'], 'Xfile.tex')
|
||||||
split Xfile.tex
|
split Xfile.tex
|
||||||
call assert_equal('plaintex', &filetype)
|
call assert_equal('plaintex', &filetype)
|
||||||
bwipe
|
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 comment.
|
||||||
|
|
||||||
|
\usemodule[translate]
|
||||||
|
END
|
||||||
|
call writefile(lines, 'Xfile.tex')
|
||||||
|
split Xfile.tex
|
||||||
|
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')
|
call delete('Xfile.tex')
|
||||||
filetype off
|
filetype off
|
||||||
endfunc
|
endfunc
|
||||||
|
Loading…
Reference in New Issue
Block a user