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 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions autoload/vimtex.vim
Expand Up @@ -180,6 +180,19 @@ 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, '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)')
call s:map(1, 'x', ']N', '<plug>(vimtex-]N)')
call s:map(1, 'x', ']n', '<plug>(vimtex-]n)')
call s:map(1, 'x', '[N', '<plug>(vimtex-[N)')
call s:map(1, 'x', '[n', '<plug>(vimtex-[n)')
call s:map(1, 'o', ']N', '<plug>(vimtex-]N)')
call s:map(1, 'o', ']n', '<plug>(vimtex-]n)')
call s:map(1, 'o', '[N', '<plug>(vimtex-[N)')
call s:map(1, 'o', '[n', '<plug>(vimtex-[n)')

call s:map(1, 'n', ']M', '<plug>(vimtex-]M)')
call s:map(1, 'n', ']m', '<plug>(vimtex-]m)')
call s:map(1, 'n', '[M', '<plug>(vimtex-[M)')
Expand Down
62 changes: 62 additions & 0 deletions autoload/vimtex/motion.vim
Expand Up @@ -39,6 +39,28 @@ function! vimtex#motion#init_buffer() abort " {{{1
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>
xnoremap <silent><buffer> <plug>(vimtex-]n) :<c-u>call vimtex#motion#math(1,0,1)<cr>
xnoremap <silent><buffer> <plug>(vimtex-]N) :<c-u>call vimtex#motion#math(0,0,1)<cr>
xnoremap <silent><buffer> <plug>(vimtex-[n) :<c-u>call vimtex#motion#math(1,1,1)<cr>
xnoremap <silent><buffer> <plug>(vimtex-[N) :<c-u>call vimtex#motion#math(0,1,1)<cr>
xmap <silent><buffer> <plug>(vimtex-]n) <sid>(vimtex-]m)
xmap <silent><buffer> <plug>(vimtex-]N) <sid>(vimtex-]N)
xmap <silent><buffer> <plug>(vimtex-[n) <sid>(vimtex-]n)
xmap <silent><buffer> <plug>(vimtex-[N) <sid>(vimtex-]N)
lervag marked this conversation as resolved.
Show resolved Hide resolved
onoremap <silent><buffer> <plug>(vimtex-]n)
\ :execute "normal \<sid>(V)" . v:count1 . "\<sid>(vimtex-]n)"<cr>
onoremap <silent><buffer> <plug>(vimtex-]N)
\ :execute "normal \<sid>(V)" . v:count1 . "\<sid>(vimtex-]N)"<cr>
onoremap <silent><buffer> <plug>(vimtex-[n)
\ :execute "normal \<sid>(V)" . v:count1 . "\<sid>(vimtex-[n)"<cr>
onoremap <silent><buffer> <plug>(vimtex-[N)
\ :execute "normal \<sid>(V)" . v:count1 . "\<sid>(vimtex-[N)"<cr>

" Environments
nnoremap <silent><buffer> <plug>(vimtex-]m) :<c-u>call vimtex#motion#environment(1,0,0)<cr>
nnoremap <silent><buffer> <plug>(vimtex-]M) :<c-u>call vimtex#motion#environment(0,0,0)<cr>
Expand Down Expand Up @@ -194,6 +216,46 @@ function! vimtex#motion#comment(begin, backwards, visual) abort " {{{1
endfunction

" }}}1
function! vimtex#motion#math(begin, backwards, visual) abort " {{{1

This conversation was marked as resolved.
Show resolved Hide resolved
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*\{)')

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
let l:success = 0
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(vimtex#pos#prev(l:pos))
endif
if vimtex#syntax#in_mathzone(l:pos[1],l:pos[2])
This conversation was marked as resolved.
Show resolved Hide resolved
let l:success = 1
break
endif
endwhile
endfor

" Restore cursor position if fail
This conversation was marked as resolved.
Show resolved Hide resolved
if l:success == 0
call vimtex#pos#set_cursor(l:curpos_saved)
endif
endfunction


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