Better delimiter control for environments
This commit is contained in:
parent
ef4c758b7a
commit
d536242a53
@ -17,13 +17,20 @@ function! vimtex#delim#init_script() " {{{1
|
|||||||
let s:delims.env = {
|
let s:delims.env = {
|
||||||
\ 'list' : [
|
\ 'list' : [
|
||||||
\ ['begin', 'end'],
|
\ ['begin', 'end'],
|
||||||
|
\ ],
|
||||||
|
\ 're' : [
|
||||||
|
\ ['\\begin\s*{[^}]*}', '\\end\s*{[^}]*}'],
|
||||||
|
\ ],
|
||||||
|
\}
|
||||||
|
|
||||||
|
let s:delims.env_math = {
|
||||||
|
\ 'list' : [
|
||||||
\ ['\(', '\)'],
|
\ ['\(', '\)'],
|
||||||
\ ['\[', '\]'],
|
\ ['\[', '\]'],
|
||||||
\ ['$$', '$$'],
|
\ ['$$', '$$'],
|
||||||
\ ['$', '$'],
|
\ ['$', '$'],
|
||||||
\ ],
|
\ ],
|
||||||
\ 're' : [
|
\ 're' : [
|
||||||
\ ['\\begin\s*{[^}]*}', '\\end\s*{[^}]*}'],
|
|
||||||
\ ['\\(', '\\)'],
|
\ ['\\(', '\\)'],
|
||||||
\ ['\\\[', '\\\]'],
|
\ ['\\\[', '\\\]'],
|
||||||
\ ['\$\$', '\$\$'],
|
\ ['\$\$', '\$\$'],
|
||||||
@ -109,6 +116,19 @@ function! vimtex#delim#init_script() " {{{1
|
|||||||
\ . '\)'
|
\ . '\)'
|
||||||
\}
|
\}
|
||||||
|
|
||||||
|
let s:re.env_math = {
|
||||||
|
\ 'open' : '\%('
|
||||||
|
\ . join(map(copy(s:delims.env_math.re), 'v:val[0]'), '\|')
|
||||||
|
\ . '\)',
|
||||||
|
\ 'close' : '\%('
|
||||||
|
\ . join(map(copy(s:delims.env_math.re), 'v:val[1]'), '\|')
|
||||||
|
\ . '\)',
|
||||||
|
\ 'both' : '\%('
|
||||||
|
\ . join(map(copy(s:delims.env_math.re), 'v:val[0]'), '\|') . '\|'
|
||||||
|
\ . join(map(copy(s:delims.env_math.re), 'v:val[1]'), '\|')
|
||||||
|
\ . '\)'
|
||||||
|
\}
|
||||||
|
|
||||||
let s:re.delim_tex = {
|
let s:re.delim_tex = {
|
||||||
\ 'open' : '\%('
|
\ 'open' : '\%('
|
||||||
\ . join(map(copy(s:delims.delim_tex.re), 'v:val[0]'), '\|')
|
\ . join(map(copy(s:delims.delim_tex.re), 'v:val[0]'), '\|')
|
||||||
@ -141,15 +161,19 @@ function! vimtex#delim#init_script() " {{{1
|
|||||||
\ . '\)'
|
\ . '\)'
|
||||||
\}
|
\}
|
||||||
|
|
||||||
|
let s:delims.env_all = {}
|
||||||
let s:delims.delim_all = {}
|
let s:delims.delim_all = {}
|
||||||
let s:delims.all = {}
|
let s:delims.all = {}
|
||||||
|
let s:re.env_all = {}
|
||||||
let s:re.delim_all = {}
|
let s:re.delim_all = {}
|
||||||
let s:re.all = {}
|
let s:re.all = {}
|
||||||
for k in ['list', 're']
|
for k in ['list', 're']
|
||||||
|
let s:delims.env_all[k] = s:delims.env[k] + s:delims.env[k]
|
||||||
let s:delims.delim_all[k] = s:delims.delim_math[k] + s:delims.delim_tex[k]
|
let s:delims.delim_all[k] = s:delims.delim_math[k] + s:delims.delim_tex[k]
|
||||||
let s:delims.all[k] = s:delims.env[k] + s:delims.delim_all[k]
|
let s:delims.all[k] = s:delims.env[k] + s:delims.delim_all[k]
|
||||||
endfor
|
endfor
|
||||||
for k in ['open', 'close', 'both']
|
for k in ['open', 'close', 'both']
|
||||||
|
let s:re.env_all[k] = s:re.env[k] . '\|' . s:re.env_math[k]
|
||||||
let s:re.delim_all[k] = s:re.delim_math[k] . '\|' . s:re.delim_tex[k]
|
let s:re.delim_all[k] = s:re.delim_math[k] . '\|' . s:re.delim_tex[k]
|
||||||
let s:re.all[k] = s:re.env[k] . '\|' . s:re.delim_all[k]
|
let s:re.all[k] = s:re.env[k] . '\|' . s:re.delim_all[k]
|
||||||
endfor
|
endfor
|
||||||
@ -333,6 +357,8 @@ function! s:get_delim(direction, type, side) " {{{1
|
|||||||
" prev
|
" prev
|
||||||
" current
|
" current
|
||||||
" type env
|
" type env
|
||||||
|
" env_math
|
||||||
|
" env_all
|
||||||
" delim_tex
|
" delim_tex
|
||||||
" delim_math
|
" delim_math
|
||||||
" delim_all
|
" delim_all
|
||||||
@ -343,7 +369,7 @@ function! s:get_delim(direction, type, side) " {{{1
|
|||||||
"
|
"
|
||||||
" Returns:
|
" Returns:
|
||||||
" delim = {
|
" delim = {
|
||||||
" type : env | delim | $ | $$ | \( | \[
|
" type : env | delim
|
||||||
" side : open | close
|
" side : open | close
|
||||||
" name : name of environment [only for type env]
|
" name : name of environment [only for type env]
|
||||||
" lnum : number
|
" lnum : number
|
||||||
@ -381,7 +407,8 @@ function! s:get_delim(direction, type, side) " {{{1
|
|||||||
for l:type in s:types
|
for l:type in s:types
|
||||||
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, a:side, a:type, a:direction),
|
\ l:type.parser(l:match, l:lnum, l:cnum,
|
||||||
|
\ a:side, a:type, a:direction),
|
||||||
\ l:result, 'keep')
|
\ l:result, 'keep')
|
||||||
break
|
break
|
||||||
endif
|
endif
|
||||||
@ -430,7 +457,7 @@ function! s:parser_tex(match, lnum, cnum, side, type, direction) " {{{1
|
|||||||
" the side.
|
" the side.
|
||||||
"
|
"
|
||||||
let result = {}
|
let result = {}
|
||||||
let result.type = a:match
|
let result.type = 'env'
|
||||||
let result.corr = a:match
|
let result.corr = a:match
|
||||||
let result.get_matching = function('s:get_matching_tex')
|
let result.get_matching = function('s:get_matching_tex')
|
||||||
let result.re = {
|
let result.re = {
|
||||||
@ -473,7 +500,7 @@ endfunction
|
|||||||
function! s:parser_latex(match, lnum, cnum, ...) " {{{1
|
function! s:parser_latex(match, lnum, cnum, ...) " {{{1
|
||||||
let result = {}
|
let result = {}
|
||||||
|
|
||||||
let result.type = a:match =~# '\\(\|\\)' ? '\(' : '\['
|
let result.type = 'env'
|
||||||
let result.side = a:match =~# '\\(\|\\\[' ? 'open' : 'close'
|
let result.side = a:match =~# '\\(\|\\\[' ? 'open' : 'close'
|
||||||
let result.is_open = result.side ==# 'open'
|
let result.is_open = result.side ==# 'open'
|
||||||
let result.get_matching = function('s:get_matching_latex')
|
let result.get_matching = function('s:get_matching_latex')
|
||||||
@ -483,8 +510,8 @@ function! s:parser_latex(match, lnum, cnum, ...) " {{{1
|
|||||||
\ : substitute(substitute(a:match, '\]', '[', ''), ')', '(', '')
|
\ : substitute(substitute(a:match, '\]', '[', ''), ')', '(', '')
|
||||||
|
|
||||||
let result.re = {
|
let result.re = {
|
||||||
\ 'open' : result.type ==# '\(' ? '\\(' : '\\\[',
|
\ 'open' : a:match =~# '\\(\|\\)' ? '\\(' : '\\\[',
|
||||||
\ 'close' : result.type ==# '\(' ? '\\)' : '\\\]',
|
\ 'close' : a:match =~# '\\(\|\\)' ? '\\)' : '\\\]',
|
||||||
\}
|
\}
|
||||||
|
|
||||||
let result.re.this = result.is_open ? result.re.open : result.re.close
|
let result.re.this = result.is_open ? result.re.open : result.re.close
|
||||||
|
@ -39,7 +39,7 @@ endfunction
|
|||||||
" }}}1
|
" }}}1
|
||||||
|
|
||||||
function! vimtex#env#change(new) " {{{1
|
function! vimtex#env#change(new) " {{{1
|
||||||
let [l:open, l:close] = vimtex#delim#get_surrounding('env')
|
let [l:open, l:close] = vimtex#delim#get_surrounding('env_all')
|
||||||
|
|
||||||
"
|
"
|
||||||
" Set target environment
|
" Set target environment
|
||||||
@ -91,7 +91,7 @@ function! vimtex#env#change(new) " {{{1
|
|||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
function! vimtex#env#change_prompt() " {{{1
|
function! vimtex#env#change_prompt() " {{{1
|
||||||
let [l:open, l:close] = vimtex#delim#get_surrounding('env')
|
let [l:open, l:close] = vimtex#delim#get_surrounding('env_all')
|
||||||
if empty(l:open) | return | endif
|
if empty(l:open) | return | endif
|
||||||
let l:name = l:open.type ==# 'env' ? l:open.name : l:open.type
|
let l:name = l:open.type ==# 'env' ? l:open.name : l:open.type
|
||||||
|
|
||||||
@ -110,7 +110,7 @@ endfunction
|
|||||||
|
|
||||||
function! vimtex#env#toggle_star() " {{{1
|
function! vimtex#env#toggle_star() " {{{1
|
||||||
let [l:open, l:close] = vimtex#delim#get_surrounding('env')
|
let [l:open, l:close] = vimtex#delim#get_surrounding('env')
|
||||||
if empty(l:open) || l:open.type !=# 'env' | return | endif
|
if empty(l:open) | return | endif
|
||||||
|
|
||||||
call vimtex#env#change(l:open.starred ? l:open.name : l:open.name . '*')
|
call vimtex#env#change(l:open.starred ? l:open.name : l:open.name . '*')
|
||||||
|
|
||||||
|
@ -66,7 +66,7 @@ function! vimtex#text_obj#environments(...) " {{{1
|
|||||||
let l:inner = a:0 > 0
|
let l:inner = a:0 > 0
|
||||||
|
|
||||||
let [l:open, l:close] = vimtex#delim#get_surrounding('env')
|
let [l:open, l:close] = vimtex#delim#get_surrounding('env')
|
||||||
if empty(l:open) || l:open.type !=# 'env' | return | endif
|
if empty(l:open) | return | endif
|
||||||
|
|
||||||
if l:inner
|
if l:inner
|
||||||
call cursor(l:open.lnum, l:open.cnum + strlen(l:open.match))
|
call cursor(l:open.lnum, l:open.cnum + strlen(l:open.match))
|
||||||
@ -91,8 +91,8 @@ endfunction
|
|||||||
|
|
||||||
" }}}1
|
" }}}1
|
||||||
function! vimtex#text_obj#inline_math(...) " {{{1
|
function! vimtex#text_obj#inline_math(...) " {{{1
|
||||||
let [l:open, l:close] = vimtex#delim#get_surrounding('env')
|
let [l:open, l:close] = vimtex#delim#get_surrounding('env_math')
|
||||||
if empty(l:open) || l:open.type ==# 'env' | return | endif
|
if empty(l:open) | return | endif
|
||||||
call s:text_obj_delim(l:open, l:close, a:0 > 0)
|
call s:text_obj_delim(l:open, l:close, a:0 > 0)
|
||||||
endfunction
|
endfunction
|
||||||
" }}}1
|
" }}}1
|
||||||
|
Loading…
x
Reference in New Issue
Block a user