Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jump to previous/next math zone #1818

Closed
wants to merge 20 commits into from
Closed
Show file tree
Hide file tree
Changes from 6 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
5 changes: 5 additions & 0 deletions autoload/vimtex.vim
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,11 @@ function! s:init_default_mappings() abort " {{{1
call s:map(1, 'o', ']*', '<plug>(vimtex-]*)')
call s:map(1, 'o', '[/', '<plug>(vimtex-[/)')
call s:map(1, 'o', '[*', '<plug>(vimtex-[*)')

call s:map(1, 'n', ']N', '<plug>(vimtex-]N)')
call s:map(1, 'n', ']n', '<plug>(vimtex-]n)')
call s:map(1, 'n', '[N', '<plug>(vimtex-[N)')
call s:map(1, 'n', '[n', '<plug>(vimtex-[n)')
This conversation was marked as resolved.
Show resolved Hide resolved
endif

if g:vimtex_text_obj_enabled
Expand Down
45 changes: 45 additions & 0 deletions autoload/vimtex/motion.vim
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ function! vimtex#motion#init_buffer() abort " {{{1
\ :execute "normal \<sid>(V)" . v:count1 . "\<sid>(vimtex-[/)"<cr>
onoremap <silent><buffer> <plug>(vimtex-[*)
\ :execute "normal \<sid>(V)" . v:count1 . "\<sid>(vimtex-[*)"<cr>

" Math
nnoremap <silent><buffer> <plug>(vimtex-]n) :<c-u>call vimtex#motion#math(1,0,0)<cr>
nnoremap <silent><buffer> <plug>(vimtex-]N) :<c-u>call vimtex#motion#math(0,0,0)<cr>
nnoremap <silent><buffer> <plug>(vimtex-[n) :<c-u>call vimtex#motion#math(1,1,0)<cr>
nnoremap <silent><buffer> <plug>(vimtex-[N) :<c-u>call vimtex#motion#math(0,1,0)<cr>
lervag marked this conversation as resolved.
Show resolved Hide resolved
endfunction

" }}}1
Expand Down Expand Up @@ -194,6 +200,45 @@ function! vimtex#motion#comment(begin, backwards, visual) abort " {{{1
endfunction

" }}}1
function! vimtex#motion#math(begin, backwards, visual) abort " {{{1
" Save cursor position first (and restore it on errors)
let l:curpos_saved = vimtex#pos#get_cursor()

let l:count = v:count1
if a:visual
normal! gv
endif

lervag marked this conversation as resolved.
Show resolved Hide resolved
" Search for $, $$, \[, \(, \begin
" Use syntax to determine if we are inside math region
let l:re = g:vimtex#re#not_comment . (a:begin ? '%(\${1,2}|\\\[|\\\(|\\begin\s*\{)' : '%(\${1,2}|\\\]|\\\)|\\end\s*\{)')
This conversation was marked as resolved.
Show resolved Hide resolved
let l:flags = 'W' . (a:backwards ? 'b' : '')

for l:_ in range(l:count)
" Ensure we are not going into infinite loop
let l:iter = 0

" We need to restore cursor back to it's original position if jumping
" to count number of math environments fail.
let l:restore_cursor = 1
This conversation was marked as resolved.
Show resolved Hide resolved
while l:iter <= 5
let l:iter += 1
call search(l:re, l:flags)
let l:pos = vimtex#pos#get_cursor()
if a:begin == 0
let l:pos = vimtex#pos#prev(l:pos[1],l:pos[2]-1)
This conversation was marked as resolved.
Show resolved Hide resolved
endif
if vimtex#syntax#in_mathzone(l:pos[1],l:pos[2],l:pos[3])
This conversation was marked as resolved.
Show resolved Hide resolved
let l:restore_cursor = 0
break
endif
endwhile
endfor
" Restore cursor position if fail
This conversation was marked as resolved.
Show resolved Hide resolved
if l:restore_cursor
call vimtex#pos#set_cursor(l:curpos_saved)
endif
endfunction


" Patterns to match section/chapter/...
Expand Down