@@ -240,16 +240,44 @@ endfunction
240240
241241" }}}1
242242function ! 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
0 commit comments