Skip to content

Commit

Permalink
feat: further improve toc beamer parser
Browse files Browse the repository at this point in the history
  • Loading branch information
lervag committed Jun 24, 2023
1 parent efd8248 commit 8d9d3da
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 31 deletions.
72 changes: 46 additions & 26 deletions autoload/vimtex/parser/toc/beamer_frame.vim
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,39 @@ endfunction
" }}}1

let s:matcher = {
\ 'prefilter_cmds' : ['begin'],
\ 'priority' : 0,
\ 're' : '^\s*\\begin{frame}',
\ 'prefilter_cmds': ['begin'],
\ 'priority': 0,
\ 're': '^\s*\\begin{frame}',
\ 're_end': '^\s*\\end{frame}',
\ 're_match': '^\s*\\begin{frame}\%(\[[^]]\+\]\)\?{\zs.*\ze}\s*$',
\}
function! s:matcher.init() abort dict " {{{1
let self.number = 0
let self.title = ''
let self.subtitle = ''
endfunction

" }}}1
function! s:matcher.get_entry(context) abort dict " {{{1
let l:title = vimtex#util#trim(
\ matchstr(a:context.line, self.re . '\%(\[[^]]\+\]\)\?{\zs.*\ze}\s*$'))
let self.number += 1
let self.title = ''
let self.subtitle = ''

" Handle subtitles, e.g. \begin{frame}{title}{subtitle}
let l:title = substitute(l:title, '}\s*{', ' - ', '')
let l:parts = split(matchstr(a:context.line, self.re_match), '}\s*{')
if len(l:parts) > 1
let self.title = vimtex#util#trim(l:parts[0])
let self.subtitle = vimtex#util#trim(l:parts[1])
elseif len(l:parts) > 0
let self.title = vimtex#util#trim(l:parts[0])
endif

if empty(l:title)
let l:title = 'Frame'
let a:context.__title = ''
let a:context.__subtitle = ''
if empty(self.title)
let a:context.continue = 'beamer_frame'
else
let l:title = 'Frame: ' . l:title
endif

return {
\ 'title' : l:title,
\ 'title' : self.get_title(),
\ 'number' : '',
\ 'file' : a:context.file,
\ 'line' : a:context.lnum,
Expand All @@ -42,27 +53,36 @@ function! s:matcher.get_entry(context) abort dict " {{{1
\ }
endfunction

" }}}1
function! s:matcher.get_title() abort dict " {{{1
if !empty(self.title) && !empty(self.subtitle)
let l:title = ': ' . self.title . ' - ' . self.subtitle
elseif !empty(self.title)
let l:title = ': ' . self.title
elseif !empty(self.subtitle)
let l:title = ': ' . self.subtitle
else
let l:title = ''
endif

return printf("Frame %d%s", self.number, l:title)
endfunction

" }}}1
function! s:matcher.continue(context) abort dict " {{{1
if empty(a:context.__title)
let a:context.__title = vimtex#util#trim(
if empty(self.title)
let self.title = vimtex#util#trim(
\ matchstr(a:context.line, '^\s*\\frametitle\s*{\zs[^}]*'))
endif
if empty(a:context.__subtitle)
let a:context.__subtitle = vimtex#util#trim(
if empty(self.subtitle)
let self.subtitle = vimtex#util#trim(
\ matchstr(a:context.line, '^\s*\\framesubtitle\s*{\zs[^}]*'))
endif

if !empty(a:context.__title) && !empty(a:context.__subtitle)
unlet! a:context.continue
let a:context.entry.title .= ': ' . a:context.__title . ' - ' . a:context.__subtitle
endif

if a:context.line =~# '\\end\s*{\s*frame\s*}'
if (!empty(self.title) && !empty(self.subtitle))
\ || a:context.line =~# self.re_end
let a:context.entry.title = self.get_title()
unlet! a:context.continue
if !empty(a:context.__title) || !empty(a:context.__subtitle)
let a:context.entry.title .= ': ' . a:context.__title . ' - ' . a:context.__subtitle
endif
endif
endfunction

Expand Down
8 changes: 8 additions & 0 deletions test/test-toc/test-beamer.tex
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ \section{Titles and subtitles}
\frametitle{title}
\end{frame}

\begin{frame}
\frametitle{title}
\end{frame}

\begin{frame}
\framesubtitle{subtitle}
\end{frame}

\section{Conclusion}
\begin{frame}{A title here}
One more frame.
Expand Down
11 changes: 6 additions & 5 deletions test/test-toc/test-beamer.vim
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ silent edit test-beamer.tex
if empty($INMAKE) | finish | endif

let s:toc = vimtex#toc#get_entries()
call assert_equal(12, len(s:toc))
call assert_equal(14, len(s:toc))

call assert_equal('Frame: A title here - Subtitle', s:toc[6].title)
call assert_equal('Frame 3: A title here - Subtitle', s:toc[6].title)
call assert_equal(21, s:toc[6].line)

call assert_equal('Frame: title - subtitle', s:toc[7].title)
call assert_equal('Frame 4: title - subtitle', s:toc[7].title)
call assert_equal('Frame 5: title', s:toc[8].title)
call assert_equal('Frame 6: subtitle', s:toc[9].title)

call assert_equal('Frame: Finito again', s:toc[11].title)
call assert_equal(37, s:toc[11].line)
call assert_equal('Frame 9: Finito again', s:toc[13].title)

call vimtex#test#finished()

0 comments on commit 8d9d3da

Please sign in to comment.