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
|
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 <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>
" 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>
" 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
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)@<!\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
" Patterns to match section/chapter/...
let s:re_sec = '\v^\s*\\%(%(sub)?paragraph|%(sub)*section|chapter|part|'
\ . 'appendi%(x|ces)|%(front|back|main)matter)>'
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
|