From 4249328a06849f3c7fc932119b45f9e0d0d3408d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karl=20Yngve=20Lerv=C3=A5g?= Date: Tue, 11 Oct 2016 22:56:50 +0200 Subject: [PATCH] Fixed #592: Better handling of delimiter indentation --- autoload/vimtex/delim.vim | 16 ++----- indent/tex.vim | 93 ++++++++++++++++++++++++++++++--------- 2 files changed, 76 insertions(+), 33 deletions(-) diff --git a/autoload/vimtex/delim.vim b/autoload/vimtex/delim.vim index a4104fe..6d680db 100644 --- a/autoload/vimtex/delim.vim +++ b/autoload/vimtex/delim.vim @@ -213,19 +213,9 @@ endfunction " }}}1 -function! vimtex#delim#get_valid_regexps(...) " {{{1 - " - " Arguments: (Optional) - " line number - " column number - " - " Returns: - " [regexp_open_delims, regexp_close_delims] - " - - return call('vimtex#util#in_mathzone', a:000) - \ ? [s:re.delim_math.open, s:re.delim_math.close] - \ : [s:re.delim_tex.open, s:re.delim_tex.close] +function! vimtex#delim#get_delim_regexes(...) " {{{1 + return [s:re.delim_math.open, s:re.delim_math.close, + \ s:re.delim_tex.open, s:re.delim_tex.close] endfunction " }}}1 diff --git a/indent/tex.vim b/indent/tex.vim index ae0af7a..116373f 100644 --- a/indent/tex.vim +++ b/indent/tex.vim @@ -46,11 +46,11 @@ function! VimtexIndent(lnum) " {{{1 let l:nprev = s:get_prev_line(l:nprev, 'ignore-ampersands') if l:nprev == 0 | return 0 | endif - let l:prev = getline(l:nprev) + let l:prev = substitute(getline(l:nprev), '\\\@= 0 + let sum += 1 + let match = matchstr(a:line, a:pattern, indx) + let indx += len(match) + let indx = match(a:line, a:pattern, indx) + endwhile + return sum +endfunction + +" }}}2 +function! s:split(line, lnum) " {{{2 + let l:map = map(range(1,col([a:lnum, strlen(a:line)])), + \ '[v:val, vimtex#util#in_mathzone(a:lnum, v:val)]') + + " Adjust math mode limits (currently handle only $'s) + let l:prev = 1 + for l:i in range(len(l:map)) + if l:map[l:i][1] == 1 && l:prev == 0 + let l:prev = l:map[l:i][1] + let l:map[l:i][1] = 0 + else + let l:prev = l:map[l:i][1] + endif + endfor + if l:map[0][1] == 1 && a:line[0] ==# '$' + let l:map[0][1] = 0 + endif + + " Extract normal text + let l:normal = '' + for [l:i, l:val] in l:map + if l:val == 0 + let l:normal .= a:line[l:i - 1] + endif + endfor + let l:normal = substitute(l:normal, '\\verb\(.\).\{}\1', '', 'g') + + " Extract math text from beginning of line + let l:math_pre = '' + let l:indx = 0 + while l:map[l:indx][1] == 1 + let l:math_pre .= a:line[l:map[l:indx][0] - 1] + let l:indx += 1 + endwhile + + " Extract math text from end of line + let l:math_end = '' + let l:indx = -1 + while l:map[l:indx][1] == 1 + let l:math_end = a:line[l:map[l:indx][0] - 1] . l:math_end + let l:indx -= 1 + endwhile + + return [l:normal, l:math_pre, l:math_end] +endfunction + +" }}}2 + " }}}1 function! s:indent_tikz(lnum, prev) " {{{1 if vimtex#env#is_inside('tikzpicture') @@ -142,20 +209,6 @@ let s:tikz_commands = '\v\\%(' . join([ " }}}1 -function! s:count(line, pattern) " {{{1 - let sum = 0 - let indx = match(a:line, a:pattern) - while indx >= 0 - let sum += 1 - let match = matchstr(a:line, a:pattern, indx) - let indx += len(match) - let indx = match(a:line, a:pattern, indx) - endwhile - return sum -endfunction - -" }}}1 - let &cpo = s:cpo_save unlet s:cpo_save