More flexible options for s:get_delim()

This commit is contained in:
Karl Yngve Lervåg 2016-11-08 23:06:44 +01:00
parent fa1adca64a
commit 66c0446cb5

View File

@ -365,17 +365,29 @@ endfunction
" }}}1 " }}}1
function! vimtex#delim#get_next(type, side) " {{{1 function! vimtex#delim#get_next(type, side) " {{{1
return s:get_delim('next', a:type, a:side) return s:get_delim({
\ 'direction' : 'next',
\ 'type' : a:type,
\ 'side' : a:side,
\})
endfunction endfunction
" }}}1 " }}}1
function! vimtex#delim#get_prev(type, side) " {{{1 function! vimtex#delim#get_prev(type, side) " {{{1
return s:get_delim('prev', a:type, a:side) return s:get_delim({
\ 'direction' : 'prev',
\ 'type' : a:type,
\ 'side' : a:side,
\})
endfunction endfunction
" }}}1 " }}}1
function! vimtex#delim#get_current(type, side) " {{{1 function! vimtex#delim#get_current(type, side) " {{{1
return s:get_delim('current', a:type, a:side) return s:get_delim({
\ 'direction' : 'current',
\ 'type' : a:type,
\ 'side' : a:side,
\})
endfunction endfunction
" }}}1 " }}}1
@ -446,22 +458,24 @@ endfunction
" }}}1 " }}}1
function! s:get_delim(direction, type, side) " {{{1 function! s:get_delim(opts) " {{{1
" "
" Arguments: " Arguments:
" direction next " opts = {
" prev " 'direction' : next
" current " prev
" type env " current
" env_math " 'type' : env
" env_all " env_math
" delim_tex " env_all
" delim_math " delim_tex
" delim_all " delim_math
" all " delim_all
" side open " all
" close " 'side' : open
" both " close
" both
" }
" "
" Returns: " Returns:
" delim = { " delim = {
@ -478,15 +492,15 @@ function! s:get_delim(direction, type, side) " {{{1
" } " }
" } " }
" "
let l:re = s:re[a:type][a:side] let l:re = s:re[a:opts.type][a:opts.side]
let [l:lnum, l:cnum] = a:direction ==# 'next' let [l:lnum, l:cnum] = a:opts.direction ==# 'next'
\ ? searchpos(l:re, 'cnW', line('.') + s:stopline) \ ? searchpos(l:re, 'cnW', line('.') + s:stopline)
\ : a:direction ==# 'prev' \ : a:opts.direction ==# 'prev'
\ ? searchpos(l:re, 'bcnW', max([line('.') - s:stopline, 1])) \ ? searchpos(l:re, 'bcnW', max([line('.') - s:stopline, 1]))
\ : searchpos(l:re, 'bcnW', line('.')) \ : searchpos(l:re, 'bcnW', line('.'))
let l:match = matchstr(getline(l:lnum), '^' . l:re, l:cnum-1) let l:match = matchstr(getline(l:lnum), '^' . l:re, l:cnum-1)
if a:direction ==# 'current' if a:opts.direction ==# 'current'
\ && l:cnum + strlen(l:match) + (mode() ==# 'i' ? 1 : 0) <= col('.') \ && l:cnum + strlen(l:match) + (mode() ==# 'i' ? 1 : 0) <= col('.')
let l:match = '' let l:match = ''
let l:lnum = 0 let l:lnum = 0
@ -504,7 +518,7 @@ function! s:get_delim(direction, type, side) " {{{1
if l:match =~# '^' . l:type.re if l:match =~# '^' . l:type.re
let l:result = extend( let l:result = extend(
\ l:type.parser(l:match, l:lnum, l:cnum, \ l:type.parser(l:match, l:lnum, l:cnum,
\ a:side, a:type, a:direction), \ a:opts.side, a:opts.type, a:opts.direction),
\ l:result, 'keep') \ l:result, 'keep')
break break
endif endif
@ -587,7 +601,11 @@ function! s:parser_tex(match, lnum, cnum, side, type, direction) " {{{1
\ : s:pos_prev(a:lnum, a:cnum)) \ : s:pos_prev(a:lnum, a:cnum))
" Get new result " Get new result
let result = s:get_delim(a:direction, a:type, a:side) let result = s:get_delim({
\ 'direction' : a:direction,
\ 'type' : a:type,
\ 'side' : a:side,
\})
" Restore the cursor " Restore the cursor
call setpos('.', l:save_pos) call setpos('.', l:save_pos)