Skip to content

Commit 9dbcb2b

Browse files
committed
Parse options given to packages
with test file
1 parent a1f8864 commit 9dbcb2b

File tree

3 files changed

+103
-8
lines changed

3 files changed

+103
-8
lines changed

autoload/vimtex/state/class.vim

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -240,16 +240,44 @@ endfunction
240240

241241
" }}}1
242242
function! s:parse_packages(preamble) abort " {{{1
243-
let l:usepackages = filter(copy(a:preamble),
244-
\ 'v:val =~# ''\v%(usep|RequireP)ackage''')
245-
let l:pat = g:vimtex#re#not_comment . g:vimtex#re#not_bslash
246-
\ . '\v\\%(usep|RequireP)ackage\s*%(\[[^[\]]*\])?\s*\{\s*\zs%([^{}]+)\ze\s*\}'
247-
call map(l:usepackages, {_, x -> split(matchstr(x, l:pat), '\s*,\s*')})
243+
" Remove EOL comments and then join
244+
let l:preamble_joined = join(map(copy(a:preamble),
245+
\ {_, x -> split(x..' ', '%')[0]}), '')
248246

247+
let l:pat = g:vimtex#re#not_comment . g:vimtex#re#not_bslash
248+
\ . '\v\\%(usep|RequireP)ackage\s*%(\[([^[\]]*)\])?\s*\{\s*\zs%([^{}]+\S)\ze\s*\}'
249+
" Regex:
250+
" - Match contains package name(s)
251+
" - First submatch contains package options
249252
let l:parsed = {}
250-
for l:packages in l:usepackages
251-
for l:package in l:packages
252-
let l:parsed[l:package] = {}
253+
for l:el in matchstrlist([l:preamble_joined], pat, #{submatches:v:true})
254+
let l:packages = map(split(l:el['text'], ','), {_, x -> trim(x)})
255+
let l:options = {}
256+
if l:el['submatches'][0] != ''
257+
for l:el in map(split(l:el['submatches'][0], ','), {_, x -> trim(x)})
258+
if l:el == ''
259+
" Empty option
260+
continue
261+
elseif l:el =~ '='
262+
" Key-value option
263+
let [l:key, l:value] = map(split(l:el, '='), {_, x -> trim(x)} )
264+
265+
if l:value ==? 'true'
266+
let l:options[l:key] = v:true
267+
elseif l:value ==? 'false'
268+
let l:options[l:key] = v:false
269+
else
270+
let l:options[l:key] = l:value
271+
endif
272+
273+
else
274+
" Key-only option
275+
let l:options[l:el] = v:true
276+
endif
277+
endfor
278+
endif
279+
for l:pkg in l:packages
280+
let l:parsed[l:pkg] = l:options
253281
endfor
254282
endfor
255283

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
\RequirePackage[debrief]{silence}
2+
\documentclass{article}
3+
4+
\usepackage[main=german,english]{babel}
5+
6+
\usepackage[acronyms]{glossaries}
7+
\usepackage[record,style=long]{glossaries-extra}
8+
9+
\usepackage[% comment
10+
backend=biber,
11+
style=numeric-comp,
12+
maxcitenames=99,
13+
% doi=false, % commented option
14+
url=false,
15+
giveninits=true,
16+
]{biblatex}
17+
18+
\usepackage[notes, useibid]{biblatex-chicago} % with space
19+
20+
% Issue in VimTeX 2.16 from January 2025:
21+
% Following multi-line syntax of \usepackage is not detected.
22+
\usepackage{
23+
amsmath,
24+
tikz
25+
}
26+
27+
% Invalid syntax but parsed anyway
28+
\usepackage[draft]{package1, package2} % packages with a shared option
29+
30+
\begin{document}
31+
32+
Hello, world!
33+
34+
\end{document}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
set nocompatible
2+
set runtimepath^=../..
3+
filetype plugin on
4+
5+
nnoremap q :qall!<cr>
6+
7+
call vimtex#log#set_silent()
8+
9+
silent edit test_parse_package_options.tex
10+
11+
if empty($INMAKE) | finish | endif
12+
13+
let s:packages = {
14+
\ 'glossaries-extra': {'style': 'long', 'record': v:true},
15+
\ 'biblatex': {
16+
\ 'backend': 'biber',
17+
\ 'url': v:false,
18+
\ 'giveninits': v:true,
19+
\ 'style': 'numeric-comp',
20+
\ 'maxcitenames': '99'
21+
\ },
22+
\ 'glossaries': {'acronyms': v:true},
23+
\ 'tikz': {},
24+
\ 'babel' : {'main': 'german', 'english': v:true},
25+
\ 'silence': {'debrief': v:true},
26+
\ 'package1': {'draft': v:true},
27+
\ 'biblatex-chicago': {'notes': v:true, 'useibid': v:true},
28+
\ 'amsmath': {},
29+
\ 'package2': {'draft': v:true}
30+
\ }
31+
call assert_equal(s:packages, b:vimtex.packages)
32+
33+
call vimtex#test#finished()

0 commit comments

Comments
 (0)