1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
|
" 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 <buffer> <sid>(v) v
nnoremap <buffer> <sid>(V) V
" Matching pairs
nnoremap <silent><buffer> <plug>(vimtex-%) :call vimtex#motion#find_matching_pair()<cr>
xnoremap <silent><buffer> <sid>(vimtex-%) :<c-u>call vimtex#motion#find_matching_pair(1)<cr>
xmap <silent><buffer> <plug>(vimtex-%) <sid>(vimtex-%)
onoremap <silent><buffer> <plug>(vimtex-%) :execute "normal \<sid>(v)\<sid>(vimtex-%)"<cr>
" Sections
nnoremap <silent><buffer> <plug>(vimtex-]]) :<c-u>call vimtex#motion#section(0,0,0)<cr>
nnoremap <silent><buffer> <plug>(vimtex-][) :<c-u>call vimtex#motion#section(1,0,0)<cr>
nnoremap <silent><buffer> <plug>(vimtex-[]) :<c-u>call vimtex#motion#section(1,1,0)<cr>
nnoremap <silent><buffer> <plug>(vimtex-[[) :<c-u>call vimtex#motion#section(0,1,0)<cr>
xnoremap <silent><buffer> <sid>(vimtex-]]) :<c-u>call vimtex#motion#section(0,0,1)<cr>
xnoremap <silent><buffer> <sid>(vimtex-][) :<c-u>call vimtex#motion#section(1,0,1)<cr>
xnoremap <silent><buffer> <sid>(vimtex-[]) :<c-u>call vimtex#motion#section(1,1,1)<cr>
xnoremap <silent><buffer> <sid>(vimtex-[[) :<c-u>call vimtex#motion#section(0,1,1)<cr>
xmap <silent><buffer> <plug>(vimtex-]]) <sid>(vimtex-]])
xmap <silent><buffer> <plug>(vimtex-][) <sid>(vimtex-][)
xmap <silent><buffer> <plug>(vimtex-[]) <sid>(vimtex-[])
xmap <silent><buffer> <plug>(vimtex-[[) <sid>(vimtex-[[)
onoremap <silent><buffer> <plug>(vimtex-]]) :execute "normal \<sid>(V)" . v:count1 . "\<sid>(vimtex-]])"<cr>
onoremap <silent><buffer> <plug>(vimtex-][) :execute "normal \<sid>(V)" . v:count1 . "\<sid>(vimtex-][)"<cr>
onoremap <silent><buffer> <plug>(vimtex-[]) :execute "normal \<sid>(V)" . v:count1 . "\<sid>(vimtex-[])"<cr>
onoremap <silent><buffer> <plug>(vimtex-[[) :execute "normal \<sid>(V)" . v:count1 . "\<sid>(vimtex-[[)"<cr>
" Math environments ($-$, $$-$$, \(-\), \[-\], \begin-\end)
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> <sid>(vimtex-]n) :<c-u>call vimtex#motion#math(1,0,1)<cr>
xnoremap <silent><buffer> <sid>(vimtex-]N) :<c-u>call vimtex#motion#math(0,0,1)<cr>
xnoremap <silent><buffer> <sid>(vimtex-[n) :<c-u>call vimtex#motion#math(1,1,1)<cr>
xnoremap <silent><buffer> <sid>(vimtex-[N) :<c-u>call vimtex#motion#math(0,1,1)<cr>
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)
xmap <silent><buffer> <plug>(vimtex-[N) <sid>(vimtex-]N)
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>
nnoremap <silent><buffer> <plug>(vimtex-[m) :<c-u>call vimtex#motion#environment(1,1,0)<cr>
nnoremap <silent><buffer> <plug>(vimtex-[M) :<c-u>call vimtex#motion#environment(0,1,0)<cr>
xnoremap <silent><buffer> <sid>(vimtex-]m) :<c-u>call vimtex#motion#environment(1,0,1)<cr>
xnoremap <silent><buffer> <sid>(vimtex-]M) :<c-u>call vimtex#motion#environment(0,0,1)<cr>
xnoremap <silent><buffer> <sid>(vimtex-[m) :<c-u>call vimtex#motion#environment(1,1,1)<cr>
xnoremap <silent><buffer> <sid>(vimtex-[M) :<c-u>call vimtex#motion#environment(0,1,1)<cr>
xmap <silent><buffer> <plug>(vimtex-]m) <sid>(vimtex-]m)
xmap <silent><buffer> <plug>(vimtex-]M) <sid>(vimtex-]M)
xmap <silent><buffer> <plug>(vimtex-[m) <sid>(vimtex-[m)
xmap <silent><buffer> <plug>(vimtex-[M) <sid>(vimtex-[M)
onoremap <silent><buffer> <plug>(vimtex-]m) :execute "normal \<sid>(V)" . v:count1 . "\<sid>(vimtex-]m)"<cr>
onoremap <silent><buffer> <plug>(vimtex-]M) :execute "normal \<sid>(V)" . v:count1 . "\<sid>(vimtex-]M)"<cr>
onoremap <silent><buffer> <plug>(vimtex-[m) :execute "normal \<sid>(V)" . v:count1 . "\<sid>(vimtex-[m)"<cr>
onoremap <silent><buffer> <plug>(vimtex-[M) :execute "normal \<sid>(V)" . v:count1 . "\<sid>(vimtex-[M)"<cr>
" Frames
nnoremap <silent><buffer> <plug>(vimtex-]r) :<c-u>call vimtex#motion#frame(1,0,0)<cr>
nnoremap <silent><buffer> <plug>(vimtex-]R) :<c-u>call vimtex#motion#frame(0,0,0)<cr>
nnoremap <silent><buffer> <plug>(vimtex-[r) :<c-u>call vimtex#motion#frame(1,1,0)<cr>
nnoremap <silent><buffer> <plug>(vimtex-[R) :<c-u>call vimtex#motion#frame(0,1,0)<cr>
xnoremap <silent><buffer> <sid>(vimtex-]r) :<c-u>call vimtex#motion#frame(1,0,1)<cr>
xnoremap <silent><buffer> <sid>(vimtex-]R) :<c-u>call vimtex#motion#frame(0,0,1)<cr>
xnoremap <silent><buffer> <sid>(vimtex-[r) :<c-u>call vimtex#motion#frame(1,1,1)<cr>
xnoremap <silent><buffer> <sid>(vimtex-[R) :<c-u>call vimtex#motion#frame(0,1,1)<cr>
xmap <silent><buffer> <plug>(vimtex-]r) <sid>(vimtex-]r)
xmap <silent><buffer> <plug>(vimtex-]R) <sid>(vimtex-]R)
xmap <silent><buffer> <plug>(vimtex-[r) <sid>(vimtex-[r)
xmap <silent><buffer> <plug>(vimtex-[R) <sid>(vimtex-[R)
onoremap <silent><buffer> <plug>(vimtex-]r) :execute "normal \<sid>(V)" . v:count1 . "\<sid>(vimtex-]r)"<cr>
onoremap <silent><buffer> <plug>(vimtex-]R) :execute "normal \<sid>(V)" . v:count1 . "\<sid>(vimtex-]R)"<cr>
onoremap <silent><buffer> <plug>(vimtex-[r) :execute "normal \<sid>(V)" . v:count1 . "\<sid>(vimtex-[r)"<cr>
onoremap <silent><buffer> <plug>(vimtex-[R) :execute "normal \<sid>(V)" . v:count1 . "\<sid>(vimtex-[R)"<cr>
" Comments
nnoremap <silent><buffer> <plug>(vimtex-]/) :<c-u>call vimtex#motion#comment(1,0,0)<cr>
nnoremap <silent><buffer> <plug>(vimtex-]*) :<c-u>call vimtex#motion#comment(0,0,0)<cr>
nnoremap <silent><buffer> <plug>(vimtex-[/) :<c-u>call vimtex#motion#comment(1,1,0)<cr>
nnoremap <silent><buffer> <plug>(vimtex-[*) :<c-u>call vimtex#motion#comment(0,1,0)<cr>
xnoremap <silent><buffer> <sid>(vimtex-]/) :<c-u>call vimtex#motion#comment(1,0,1)<cr>
xnoremap <silent><buffer> <sid>(vimtex-]*) :<c-u>call vimtex#motion#comment(0,0,1)<cr>
xnoremap <silent><buffer> <sid>(vimtex-[/) :<c-u>call vimtex#motion#comment(1,1,1)<cr>
xnoremap <silent><buffer> <sid>(vimtex-[*) :<c-u>call vimtex#motion#comment(0,1,1)<cr>
xmap <silent><buffer> <plug>(vimtex-]/) <sid>(vimtex-]/)
xmap <silent><buffer> <plug>(vimtex-]*) <sid>(vimtex-]*)
xmap <silent><buffer> <plug>(vimtex-[/) <sid>(vimtex-[/)
xmap <silent><buffer> <plug>(vimtex-[*) <sid>(vimtex-[*)
onoremap <silent><buffer> <plug>(vimtex-]/) :execute "normal \<sid>(V)" . v:count1 . "\<sid>(vimtex-]/)"<cr>
onoremap <silent><buffer> <plug>(vimtex-]*) :execute "normal \<sid>(V)" . v:count1 . "\<sid>(vimtex-]*)"<cr>
onoremap <silent><buffer> <plug>(vimtex-[/) :execute "normal \<sid>(V)" . v:count1 . "\<sid>(vimtex-[/)"<cr>
onoremap <silent><buffer> <plug>(vimtex-[*) :execute "normal \<sid>(V)" . v:count1 . "\<sid>(vimtex-[*)"<cr>
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
" Hack to update the jump list so CTRL-o jumps back to the right place
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
" Hack to update the jump list so CTRL-o jumps back to the right place
normal! m`
" 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
" Patterns to match section/chapter/...
let s:re_sec = '\v^\s*\\%(' . join([
\ '%(sub)?paragraph',
\ '%(sub)*section',
\ 'chapter',
\ 'part',
\ 'appendi%(x|ces)',
\ '%(front|back|main)matter',
\ 'add%(sec|chap|part)',
\ ], '|') . ')>'
let s:re_sec_t1 = '\v%(' . s:re_sec . '|^\s*%(\\end\{document\}|%$))'
let s:re_sec_t2 = '\v%(' . s:re_sec . '|^\s*\\end\{document\})'
" }}}1
function! vimtex#motion#environment(begin, backwards, visual) abort " {{{1
let l:count = v:count1
if a:visual
normal! gv
endif
" Hack to update the jump list so CTRL-o jumps back to the right place
normal! m`
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#frame(begin, backwards, visual) abort " {{{1
let l:count = v:count1
if a:visual
normal! gv
endif
" Hack to update the jump list so CTRL-o jumps back to the right place
normal! m`
let l:re = g:vimtex#re#not_comment . (a:begin ? '\\begin\s*\{frame\}' : '\\end\s*\{frame\}')
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
" Hack to update the jump list so CTRL-o jumps back to the right place
normal! m`
let l:re = a:begin
\ ? '\v%(^\s*\%.*\n)@<!\s*\%'
\ : '\v^\s*\%.*\n%(^\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#math(begin, backwards, visual) abort " {{{1
let l:curpos_saved = vimtex#pos#get_cursor()
let l:count = v:count1
if a:visual
normal! gv
endif
" Hack to update the jump list so CTRL-o jumps back to the right place
normal! m`
" Search for math environment group delimiters
let l:re = g:vimtex#re#not_comment . (a:begin
\ ? '%((\\\[)|(\\\()|(\\begin\s*\{)|(\$\$)|(\$))'
\ : '%((\\\])|(\\\))|(\\end\s*\{)|(\$\$)|(\$))')
" The p flag is key here and is used to specify for search to return the sub
" group that matches
let l:flags = 'Wp' . (a:backwards ? 'b' : '')
for l:_ in range(l:count)
let l:success = 0
" Iterate a maximum of 6 times to ensure we are not going into an infinite
" loop. The number 6 is arbitrary, but typically good enough to find the
" math zone in the text currently visible in the window.
let l:iter = 0
while l:iter <= 5
let l:iter += 1
let l:submatch = search(l:re, l:flags)
let l:pos = vimtex#pos#get_cursor()
if l:submatch == 0 | break | endif
" Jump directly to \[, \], \(, \)
if l:submatch < 4
let l:success = 1
break
endif
if l:submatch == 4
" The target position is inside a \begin ... \end delimited math
" environment, where the syntax group is properly recognized on both
" sides.
if vimtex#syntax#in_mathzone(l:pos[1], l:pos[2])
let l:success = 1
break
endif
else
" The target position is inside a $ ... $ or $$ ... $$ based math zone.
" In this case, the beginning delimiter is syntax matched as a math
" zone, whereas the ending delimiter is not.
if a:begin
if vimtex#syntax#in_mathzone(l:pos[1], l:pos[2])
let l:success = 1
break
endif
else
" First check that the current search position is at least 2 columns
" left from the initial position and not in mathzone already. The
" check works because only opening $ and $$ are in mathzone, not
" the closing ones.
if vimtex#syntax#in_mathzone(l:pos[1], l:pos[2])
\ || vimtex#pos#val(l:curpos_saved) - vimtex#pos#val(l:pos) == 1
continue
endif
" Now check if previous position is inside a mathzone or not
let l:pos = vimtex#pos#prev(vimtex#pos#prev(l:pos))
if vimtex#syntax#in_mathzone(l:pos[1], l:pos[2])
let l:success = 1
break
endif
endif
endif
endwhile
" If a math group delimiter is found, update the saved cursor position. If
" not, then we restore the last saved position and quit. This ensures that
" we achieve the expected behaviour with the jumps with counts.
if l:success
let l:curpos_saved = vimtex#pos#get_cursor()
else
call vimtex#pos#set_cursor(l:curpos_saved)
break
endif
endfor
endfunction
" }}}1
|