From 06f24d9c18afb8e91d2a8404595f22597a2afe01 Mon Sep 17 00:00:00 2001 From: JohnReid Date: Wed, 14 Nov 2018 10:16:13 +0000 Subject: [PATCH 1/2] Add text object for paragraphs next to environments. --- ftplugin/tex/textobj-latex.vim | 67 +++++++++++++++++++++++++++++----- 1 file changed, 57 insertions(+), 10 deletions(-) diff --git a/ftplugin/tex/textobj-latex.vim b/ftplugin/tex/textobj-latex.vim index f995107..f3d9c2a 100644 --- a/ftplugin/tex/textobj-latex.vim +++ b/ftplugin/tex/textobj-latex.vim @@ -7,17 +7,39 @@ 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', -\ }, +\ 'environment': { +\ '*pattern*': [s:re_begin, s:re_end], +\ 'select-a': 'ae', +\ 'select-i': 'ie', +\ }, +\ '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', -\ }, +\ '*pattern*': ['\\\[', '\\\]'], +\ 'select-a': 'ab', +\ 'select-i': 'ib', +\ }, \ 'paren-math': { \ '*pattern*': ['\\(', '\\)'], \ 'select-a': 'a\', @@ -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 From e55a612c4efc8bed0902a9ceb36b9bdbba06a45a Mon Sep 17 00:00:00 2001 From: JohnReid Date: Wed, 14 Nov 2018 10:25:30 +0000 Subject: [PATCH 2/2] Formatting changes --- ftplugin/tex/textobj-latex.vim | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/ftplugin/tex/textobj-latex.vim b/ftplugin/tex/textobj-latex.vim index f3d9c2a..94c3004 100644 --- a/ftplugin/tex/textobj-latex.vim +++ b/ftplugin/tex/textobj-latex.vim @@ -25,21 +25,21 @@ endfunction call textobj#user#plugin('latex', { -\ 'environment': { -\ '*pattern*': [s:re_begin, s:re_end], -\ 'select-a': 'ae', -\ 'select-i': 'ie', -\ }, -\ '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', -\ }, +\ 'environment': { +\ '*pattern*': ['\\begin{[^}]\+}.*\n\s*', '\n^\s*\\end{[^}]\+}.*$'], +\ 'select-a': 'ae', +\ 'select-i': 'ie', +\ }, +\ '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', +\ }, \ 'paren-math': { \ '*pattern*': ['\\(', '\\)'], \ 'select-a': 'a\',