Skip to content

Commit 9e73b65

Browse files
committed
Parse options of documentclass
with updated __pprint and test file
1 parent 32b3084 commit 9e73b65

File tree

3 files changed

+116
-4
lines changed

3 files changed

+116
-4
lines changed

autoload/vimtex/state/class.vim

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ function! vimtex#state#class#new(opts) abort " {{{1
3434
\ ? vimtex#parser#preamble(l:new.tex, {'root' : l:new.root})
3535
\ : []
3636

37-
let l:new.documentclass = s:parse_documentclass(l:preamble)
37+
let [l:new.documentclass, l:new.documentclass_options] =
38+
\ s:parse_documentclass(l:preamble)
3839
let l:new.packages = s:parse_packages(l:preamble)
3940
let l:new.graphicspath = s:parse_graphicspath(l:preamble, l:new.root)
4041
let l:new.glossaries = s:parse_glossaries(
@@ -74,6 +75,16 @@ function! s:vimtex.__pprint() abort dict " {{{1
7475
call add(l:items, ['document class', self.documentclass])
7576
endif
7677

78+
if exists('self.documentclass_options')
79+
let l:string = join(map(sort(keys(self.documentclass_options)),
80+
\ {_, key -> key.."="..
81+
\ (self.documentclass_options[key]== v:true ? 'true' :
82+
\ self.documentclass_options[key] == v:false ? 'false' :
83+
\ self.documentclass_options[key])
84+
\ }))
85+
call add(l:items, ['document class options', l:string])
86+
endif
87+
7788
if !empty(self.packages)
7889
call add(l:items, ['packages', join(sort(keys(self.packages)))])
7990
endif
@@ -194,9 +205,37 @@ endfunction
194205

195206

196207
function! s:parse_documentclass(preamble) abort " {{{1
197-
let l:preamble_lines = filter(copy(a:preamble), {_, x -> x !~# '^\s*%'})
198-
return matchstr(join(l:preamble_lines, ''),
199-
\ '\\documentclass[^{]*{\zs[^}]\+\ze}')
208+
" Remove EOL comments and then join
209+
let l:preamble_joined = join(map(copy(a:preamble),
210+
\ {_, x -> split(x..' ', '%')[0]}), '')
211+
212+
let l:docclass = matchstr(l:preamble_joined, '\\documentclass[^{]*{\zs[^}]\+\ze}')
213+
214+
let l:docclass_options = {}
215+
let l:unparsed = matchstr(l:preamble_joined, '\\documentclass[^\[]*\[\zs[^\]]\+\ze\]')
216+
for l:el in map(split(l:unparsed, ','), {_, x -> trim(x)})
217+
if l:el == ''
218+
" Empty option
219+
continue
220+
elseif l:el =~ '='
221+
" Key-value option
222+
let [l:key, l:value] = map(split(l:el, '='), {_, x -> trim(x)} )
223+
224+
if l:value ==? 'true'
225+
let l:docclass_options[l:key] = v:true
226+
elseif l:value ==? 'false'
227+
let l:docclass_options[l:key] = v:false
228+
else
229+
let l:docclass_options[l:key] = l:value
230+
endif
231+
232+
else
233+
" Key-only option
234+
let l:docclass_options[l:el] = v:true
235+
endif
236+
endfor
237+
238+
return [l:docclass, l:docclass_options]
200239
endfunction
201240

202241
" }}}1
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
% Leading comments here
2+
3+
\documentclass[% Options of scrbook
4+
%
5+
% draft,
6+
fontsize=12pt,
7+
% smallheadings,
8+
headings=big,
9+
english,
10+
paper=a4,
11+
twoside,
12+
open=right,
13+
DIV=14,
14+
BCOR=20mm,
15+
headinclude=false,
16+
footinclude=false,
17+
mpinclude=false,
18+
% pagesize,
19+
titlepage,
20+
parskip=half,
21+
headsepline,
22+
chapterprefix=false,
23+
appendixprefix=Appendix,
24+
appendixwithprefixline=true,
25+
bibliography=totoc,
26+
toc=graduated,
27+
numbers=noenddot,
28+
]{scrbook}
29+
30+
\begin{document}
31+
32+
Hello, world!
33+
34+
\end{document}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
set nocompatible
2+
let &rtp = '../..,' . &rtp
3+
filetype plugin on
4+
5+
nnoremap q :qall!<cr>
6+
7+
call vimtex#log#set_silent()
8+
9+
silent edit test_parse_documentclass_options.tex
10+
11+
if empty($INMAKE) | finish | endif
12+
13+
call assert_equal('scrbook', b:vimtex.documentclass)
14+
15+
let s:options = {
16+
\ 'fontsize': '12pt',
17+
\ 'headings': 'big',
18+
\ 'english': v:true,
19+
\ 'paper': 'a4',
20+
\ 'twoside': v:true,
21+
\ 'open': 'right',
22+
\ 'DIV': '14',
23+
\ 'BCOR': '20mm',
24+
\ 'headinclude': v:false,
25+
\ 'footinclude': v:false,
26+
\ 'mpinclude': v:false,
27+
\ 'titlepage': v:true,
28+
\ 'parskip': 'half',
29+
\ 'headsepline': v:true,
30+
\ 'chapterprefix': v:false,
31+
\ 'appendixprefix': 'Appendix',
32+
\ 'appendixwithprefixline': v:true,
33+
\ 'bibliography': 'totoc',
34+
\ 'toc': 'graduated',
35+
\ 'numbers': 'noenddot',
36+
\ }
37+
call assert_equal(s:options, b:vimtex.documentclass_options)
38+
39+
call vimtex#test#finished()

0 commit comments

Comments
 (0)