From d757bfd643cc73c2d495355c153ed0257f5d5b47 Mon Sep 17 00:00:00 2001 From: Adam Stankiewicz Date: Sat, 25 Apr 2020 21:30:46 +0200 Subject: Change latex provider to luatex, closes #476 --- autoload/vimtex/motion.vim | 207 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 207 insertions(+) create mode 100644 autoload/vimtex/motion.vim (limited to 'autoload/vimtex/motion.vim') diff --git a/autoload/vimtex/motion.vim b/autoload/vimtex/motion.vim new file mode 100644 index 00000000..86b42055 --- /dev/null +++ b/autoload/vimtex/motion.vim @@ -0,0 +1,207 @@ +if !exists('g:polyglot_disabled') || index(g:polyglot_disabled, 'latex') == -1 + +" vimtex - LaTeX plugin for Vim +" +" Maintainer: Karl Yngve LervÄg +" Email: karl.yngve@gmail.com +" + +function! vimtex#motion#init_buffer() abort " {{{1 + if !g:vimtex_motion_enabled | return | endif + + " Utility map to avoid conflict with "normal" command + nnoremap (v) v + nnoremap (V) V + + " Matching pairs + nnoremap (vimtex-%) :call vimtex#motion#find_matching_pair() + xnoremap (vimtex-%) :call vimtex#motion#find_matching_pair(1) + xmap (vimtex-%) (vimtex-%) + onoremap (vimtex-%) :execute "normal \(v)\(vimtex-%)" + + " Sections + nnoremap (vimtex-]]) :call vimtex#motion#section(0,0,0) + nnoremap (vimtex-][) :call vimtex#motion#section(1,0,0) + nnoremap (vimtex-[]) :call vimtex#motion#section(1,1,0) + nnoremap (vimtex-[[) :call vimtex#motion#section(0,1,0) + xnoremap (vimtex-]]) :call vimtex#motion#section(0,0,1) + xnoremap (vimtex-][) :call vimtex#motion#section(1,0,1) + xnoremap (vimtex-[]) :call vimtex#motion#section(1,1,1) + xnoremap (vimtex-[[) :call vimtex#motion#section(0,1,1) + xmap (vimtex-]]) (vimtex-]]) + xmap (vimtex-][) (vimtex-][) + xmap (vimtex-[]) (vimtex-[]) + xmap (vimtex-[[) (vimtex-[[) + onoremap (vimtex-]]) + \ :execute "normal \(V)" . v:count1 . "\(vimtex-]])" + onoremap (vimtex-][) + \ :execute "normal \(V)" . v:count1 . "\(vimtex-][)" + onoremap (vimtex-[]) + \ :execute "normal \(V)" . v:count1 . "\(vimtex-[])" + onoremap (vimtex-[[) + \ :execute "normal \(V)" . v:count1 . "\(vimtex-[[)" + + " Environments + nnoremap (vimtex-]m) :call vimtex#motion#environment(1,0,0) + nnoremap (vimtex-]M) :call vimtex#motion#environment(0,0,0) + nnoremap (vimtex-[m) :call vimtex#motion#environment(1,1,0) + nnoremap (vimtex-[M) :call vimtex#motion#environment(0,1,0) + xnoremap (vimtex-]m) :call vimtex#motion#environment(1,0,1) + xnoremap (vimtex-]M) :call vimtex#motion#environment(0,0,1) + xnoremap (vimtex-[m) :call vimtex#motion#environment(1,1,1) + xnoremap (vimtex-[M) :call vimtex#motion#environment(0,1,1) + xmap (vimtex-]m) (vimtex-]m) + xmap (vimtex-]M) (vimtex-]M) + xmap (vimtex-[m) (vimtex-[m) + xmap (vimtex-[M) (vimtex-[M) + onoremap (vimtex-]m) + \ :execute "normal \(V)" . v:count1 . "\(vimtex-]m)" + onoremap (vimtex-]M) + \ :execute "normal \(V)" . v:count1 . "\(vimtex-]M)" + onoremap (vimtex-[m) + \ :execute "normal \(V)" . v:count1 . "\(vimtex-[m)" + onoremap (vimtex-[M) + \ :execute "normal \(V)" . v:count1 . "\(vimtex-[M)" + + " Comments + nnoremap (vimtex-]/) :call vimtex#motion#comment(1,0,0) + nnoremap (vimtex-]*) :call vimtex#motion#comment(0,0,0) + nnoremap (vimtex-[/) :call vimtex#motion#comment(1,1,0) + nnoremap (vimtex-[*) :call vimtex#motion#comment(0,1,0) + xnoremap (vimtex-]/) :call vimtex#motion#comment(1,0,1) + xnoremap (vimtex-]*) :call vimtex#motion#comment(0,0,1) + xnoremap (vimtex-[/) :call vimtex#motion#comment(1,1,1) + xnoremap (vimtex-[*) :call vimtex#motion#comment(0,1,1) + xmap (vimtex-]/) (vimtex-]/) + xmap (vimtex-]*) (vimtex-]*) + xmap (vimtex-[/) (vimtex-[/) + xmap (vimtex-[*) (vimtex-[*) + onoremap (vimtex-]/) + \ :execute "normal \(V)" . v:count1 . "\(vimtex-]/)" + onoremap (vimtex-]*) + \ :execute "normal \(V)" . v:count1 . "\(vimtex-]*)" + onoremap (vimtex-[/) + \ :execute "normal \(V)" . v:count1 . "\(vimtex-[/)" + onoremap (vimtex-[*) + \ :execute "normal \(V)" . v:count1 . "\(vimtex-[*)" +endfunction + +" }}}1 + +function! vimtex#motion#find_matching_pair(...) abort " {{{1 + if a:0 > 0 + normal! gv + endif + + let delim = vimtex#delim#get_current('all', 'both') + if empty(delim) + let delim = vimtex#delim#get_next('all', 'both') + if empty(delim) | return | endif + endif + + let delim = vimtex#delim#get_matching(delim) + if empty(delim) | return | endif + if empty(delim.match) | return | endif + + normal! m` + call vimtex#pos#set_cursor(delim.lnum, + \ (delim.is_open + \ ? delim.cnum + \ : delim.cnum + strlen(delim.match) - 1)) +endfunction + +" }}}1 +function! vimtex#motion#section(type, backwards, visual) abort " {{{1 + let l:count = v:count1 + if a:visual + normal! gv + endif + + " Check trivial cases + let l:top = search(s:re_sec, 'nbW') == 0 + let l:bottom = search(a:type == 1 ? s:re_sec_t2 : s:re_sec, 'nW') == 0 + if a:backwards && l:top + return vimtex#pos#set_cursor([1, 1]) + elseif !a:backwards && l:bottom + return vimtex#pos#set_cursor([line('$'), 1]) + endif + + " Define search pattern and search flag + let l:re = a:type == 0 ? s:re_sec : s:re_sec_t1 + let l:flags = 'W' + if a:backwards + let l:flags .= 'b' + endif + + for l:_ in range(l:count) + let l:save_pos = vimtex#pos#get_cursor() + + if a:type == 1 + call search('\S', 'W') + endif + + let l:bottom = search(s:re_sec_t2, 'nW') == 0 + if a:type == 1 && !a:backwards && l:bottom + return vimtex#pos#set_cursor([line('$'), 1]) + endif + + let l:top = search(s:re_sec, 'ncbW') == 0 + let l:lnum = search(l:re, l:flags) + + if l:top && l:lnum > 0 && a:type == 1 && !a:backwards + let l:lnum = search(l:re, l:flags) + endif + + if a:type == 1 + call search('\S\s*\n\zs', 'Wb') + + " Move to start of file if cursor was moved to top part of document + if search(s:re_sec, 'ncbW') == 0 + call vimtex#pos#set_cursor([1, 1]) + endif + endif + endfor +endfunction + +" }}}1 +function! vimtex#motion#environment(begin, backwards, visual) abort " {{{1 + let l:count = v:count1 + if a:visual + normal! gv + endif + + let l:re = g:vimtex#re#not_comment . (a:begin ? '\\begin\s*\{' : '\\end\s*\{') + let l:flags = 'W' . (a:backwards ? 'b' : '') + + for l:_ in range(l:count) + call search(l:re, l:flags) + endfor +endfunction + +" }}}1 +function! vimtex#motion#comment(begin, backwards, visual) abort " {{{1 + let l:count = v:count1 + if a:visual + normal! gv + endif + + let l:re = a:begin + \ ? '\v%(^\s*\%.*\n)@' +let s:re_sec_t1 = '\v%(' . s:re_sec . '|^\s*%(\\end\{document\}|%$))' +let s:re_sec_t2 = '\v%(' . s:re_sec . '|^\s*\\end\{document\})' + +endif -- cgit v1.2.3