diff --git a/ftplugin/tex/textobj-latex.vim b/ftplugin/tex/textobj-latex.vim index f995107..94c3004 100644 --- a/ftplugin/tex/textobj-latex.vim +++ b/ftplugin/tex/textobj-latex.vim @@ -7,13 +7,35 @@ if exists('g:loaded_textobj_latex') finish endif +let s:re_begin = '\\begin{[^}]\+}' +let s:re_end = '\\end{[^}]\+}' + + +" +" Join alternatives for a regular expression +function! s:ReJoin(...) + return join(a:000, '\|') +endfunction + +" +" Make sure RE matches whole line +function! s:ReWholeLine(re) + return '^\('.a:re.'\)\n' +endfunction + + call textobj#user#plugin('latex', { \ 'environment': { \ '*pattern*': ['\\begin{[^}]\+}.*\n\s*', '\n^\s*\\end{[^}]\+}.*$'], \ 'select-a': 'ae', \ 'select-i': 'ie', \ }, -\ 'bracket-math': { +\ 'outside-environ': { +\ '*sfile*': expand(':p'), +\ 'select-a': 'ar', '*select-a-function*': 's:select_a', +\ 'select-i': 'ir', '*select-i-function*': 's:select_i' +\ }, +\ 'bracket-math': { \ '*pattern*': ['\\\[', '\\\]'], \ 'select-a': 'ab', \ 'select-i': 'ib', @@ -43,5 +65,30 @@ call textobj#user#plugin('latex', { \ }, \ }) -let g:loaded_textobj_latex = 1 +function! s:select_a() + " echom s:ReWholeLine(s:ReJoin(s:re_end, '')) + let start_pos = search(s:ReWholeLine(s:ReJoin(s:re_end, '')), 'bnW') + if 0 == start_pos + return 0 " We did not find the start of the text object + endif + let end_pos = search(s:ReWholeLine(s:ReJoin(s:re_begin, '')), 'nW') + if 0 == end_pos + return 0 " We did not find the end of the text object + endif + return ['V', [0, start_pos, 0, 0], [0, end_pos, 0, 0]] +endfunction + +function! s:select_i() + let start_pos = search(s:ReWholeLine(s:ReJoin(s:re_end, '')), 'bnW') + if 0 == start_pos + return 0 " We did not find the start of the text object + endif + let end_pos = search(s:ReWholeLine(s:ReJoin(s:re_begin, '')), 'nW') + if 0 == end_pos + return 0 " We did not find the end of the text object + endif + return ['V', [0, start_pos + 1, 0, 0], [0, end_pos - 1, 0, 0]] +endfunction + +let g:loaded_textobj_latex = 1