Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 49 additions & 2 deletions ftplugin/tex/textobj-latex.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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('<sfile>: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',
Expand Down Expand Up @@ -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