blob: 77064bab265ff0dbaef89a056763765b05a50eab [file] [log] [blame]
Bram Moolenaarf8eb9c52017-01-02 17:31:24 +01001" Tests for various Visual mode.
Bram Moolenaar019b9c62016-03-05 17:26:00 +01002if !has('visual')
3 finish
4endif
5
Bram Moolenaarc3c766e2017-03-08 22:55:19 +01006
Bram Moolenaar019b9c62016-03-05 17:26:00 +01007func Test_block_shift_multibyte()
Bram Moolenaarf8eb9c52017-01-02 17:31:24 +01008 " Uses double-wide character.
Bram Moolenaar019b9c62016-03-05 17:26:00 +01009 split
10 call setline(1, ['xヹxxx', 'ヹxxx'])
11 exe "normal 1G0l\<C-V>jl>"
12 call assert_equal('x ヹxxx', getline(1))
13 call assert_equal(' ヹxxx', getline(2))
14 q!
15endfunc
Bram Moolenaarf8eb9c52017-01-02 17:31:24 +010016
Bram Moolenaarbae5a172017-08-06 15:42:06 +020017func Test_block_shift_overflow()
18 " This used to cause a multiplication overflow followed by a crash.
19 new
20 normal ii
21 exe "normal \<C-V>876543210>"
22 q!
23endfunc
24
Bram Moolenaarf8eb9c52017-01-02 17:31:24 +010025func Test_dotregister_paste()
26 new
27 exe "norm! ihello world\<esc>"
28 norm! 0ve".p
29 call assert_equal('hello world world', getline(1))
30 q!
31endfunc
Bram Moolenaar23fa81d2017-02-01 21:50:21 +010032
33func Test_Visual_ctrl_o()
34 new
35 call setline(1, ['one', 'two', 'three'])
36 call cursor(1,2)
37 set noshowmode
38 set tw=0
39 call feedkeys("\<c-v>jjlIa\<c-\>\<c-o>:set tw=88\<cr>\<esc>", 'tx')
40 call assert_equal(['oane', 'tawo', 'tahree'], getline(1, 3))
41 call assert_equal(88, &tw)
42 set tw&
43 bw!
44endfu
Bram Moolenaar84b2a382017-02-17 11:40:00 +010045
46func Test_Visual_vapo()
47 new
48 normal oxx
49 normal vapo
50 bwipe!
51endfunc
Bram Moolenaar46522af2017-02-18 23:12:01 +010052
53func Test_Visual_inner_quote()
54 new
55 normal oxX
56 normal vki'
57 bwipe!
58endfunc
Bram Moolenaar75373f32017-08-07 22:02:30 +020059
60" Test for Visual mode not being reset causing E315 error.
61func TriggerTheProblem()
62 " At this point there is no visual selection because :call reset it.
63 " Let's restore the selection:
64 normal gv
65 '<,'>del _
66 try
67 exe "normal \<Esc>"
68 catch /^Vim\%((\a\+)\)\=:E315/
69 echom 'Snap! E315 error!'
Bram Moolenaar63e82db2018-03-06 12:10:48 +010070 let g:msg = 'Snap! E315 error!'
Bram Moolenaar75373f32017-08-07 22:02:30 +020071 endtry
72endfunc
73
74func Test_visual_mode_reset()
Bram Moolenaar75373f32017-08-07 22:02:30 +020075 enew
Bram Moolenaar63e82db2018-03-06 12:10:48 +010076 let g:msg = "Everything's fine."
Bram Moolenaar75373f32017-08-07 22:02:30 +020077 enew
78 setl buftype=nofile
79 call append(line('$'), 'Delete this line.')
80
81 " NOTE: this has to be done by a call to a function because executing :del
82 " the ex-way will require the colon operator which resets the visual mode
83 " thus preventing the problem:
84 exe "normal! GV:call TriggerTheProblem()\<CR>"
85 call assert_equal("Everything's fine.", g:msg)
86
Bram Moolenaar75373f32017-08-07 22:02:30 +020087endfunc
Bram Moolenaar67418d92017-10-15 22:07:39 +020088
Bram Moolenaar15993ce2017-10-26 20:21:44 +020089" Test for visual block shift and tab characters.
90func Test_block_shift_tab()
91 enew!
92 call append(0, repeat(['one two three'], 5))
93 call cursor(1,1)
94 exe "normal i\<C-G>u"
95 exe "normal fe\<C-V>4jR\<Esc>ugvr1"
96 call assert_equal('on1 two three', getline(1))
97 call assert_equal('on1 two three', getline(2))
98 call assert_equal('on1 two three', getline(5))
99
100 enew!
101 call append(0, repeat(['abcdefghijklmnopqrstuvwxyz'], 5))
102 call cursor(1,1)
103 exe "normal \<C-V>4jI \<Esc>j<<11|D"
104 exe "normal j7|a\<Tab>\<Tab>"
105 exe "normal j7|a\<Tab>\<Tab> "
106 exe "normal j7|a\<Tab> \<Tab>\<Esc>4k13|\<C-V>4j<"
107 call assert_equal(' abcdefghijklmnopqrstuvwxyz', getline(1))
108 call assert_equal('abcdefghij', getline(2))
109 call assert_equal(" abc\<Tab> defghijklmnopqrstuvwxyz", getline(3))
110 call assert_equal(" abc\<Tab> defghijklmnopqrstuvwxyz", getline(4))
111 call assert_equal(" abc\<Tab> defghijklmnopqrstuvwxyz", getline(5))
112
113 %s/\s\+//g
114 call cursor(1,1)
115 exe "normal \<C-V>4jI \<Esc>j<<"
116 exe "normal j7|a\<Tab>\<Tab>"
117 exe "normal j7|a\<Tab>\<Tab>\<Tab>\<Tab>\<Tab>"
118 exe "normal j7|a\<Tab> \<Tab>\<Tab>\<Esc>4k13|\<C-V>4j3<"
119 call assert_equal(' abcdefghijklmnopqrstuvwxyz', getline(1))
120 call assert_equal('abcdefghij', getline(2))
121 call assert_equal(" abc\<Tab> defghijklmnopqrstuvwxyz", getline(3))
122 call assert_equal(" abc\<Tab>\<Tab>defghijklmnopqrstuvwxyz", getline(4))
123 call assert_equal(" abc\<Tab> defghijklmnopqrstuvwxyz", getline(5))
124
125 enew!
126endfunc
127
128" Tests Blockwise Visual when there are TABs before the text.
129func Test_blockwise_visual()
130 enew!
131 call append(0, ['123456',
132 \ '234567',
133 \ '345678',
134 \ '',
135 \ 'test text test tex start here',
136 \ "\t\tsome text",
137 \ "\t\ttest text",
138 \ 'test text'])
139 call cursor(1,1)
140 exe "normal /start here$\<CR>"
141 exe 'normal "by$' . "\<C-V>jjlld"
142 exe "normal /456$\<CR>"
143 exe "normal \<C-V>jj" . '"bP'
144 call assert_equal(['123start here56',
145 \ '234start here67',
146 \ '345start here78',
147 \ '',
148 \ 'test text test tex rt here',
149 \ "\t\tsomext",
150 \ "\t\ttesext"], getline(1, 7))
151
152 enew!
153endfunc
154
Bram Moolenaar2e949762018-05-20 14:06:38 +0200155" Test swapping corners in blockwise visual mode with o and O
156func Test_blockwise_visual_o_O()
157 enew!
158
159 exe "norm! 10i.\<Esc>Y4P3lj\<C-V>4l2jr "
160 exe "norm! gvO\<Esc>ra"
161 exe "norm! gvO\<Esc>rb"
162 exe "norm! gvo\<C-c>rc"
163 exe "norm! gvO\<C-c>rd"
164
165 call assert_equal(['..........',
166 \ '...c d..',
167 \ '... ..',
168 \ '...a b..',
169 \ '..........'], getline(1, '$'))
170
171 enew!
172endfun
173
Bram Moolenaar15993ce2017-10-26 20:21:44 +0200174" Test Virtual replace mode.
175func Test_virtual_replace()
Bram Moolenaardf0d24b2018-03-06 14:22:58 +0100176 if exists('&t_kD')
177 let save_t_kD = &t_kD
178 endif
179 if exists('&t_kb')
180 let save_t_kb = &t_kb
181 endif
Bram Moolenaar15993ce2017-10-26 20:21:44 +0200182 exe "set t_kD=\<C-V>x7f t_kb=\<C-V>x08"
183 enew!
184 exe "normal a\nabcdefghi\njk\tlmn\n opq rst\n\<C-D>uvwxyz"
185 call cursor(1,1)
186 set ai bs=2
187 exe "normal gR0\<C-D> 1\nA\nBCDEFGHIJ\n\tKL\nMNO\nPQR"
188 call assert_equal([' 1',
189 \ ' A',
190 \ ' BCDEFGHIJ',
191 \ ' KL',
192 \ ' MNO',
193 \ ' PQR',
194 \ ], getline(1, 6))
195 normal G
196 mark a
197 exe "normal o0\<C-D>\nabcdefghi\njk\tlmn\n opq\trst\n\<C-D>uvwxyz\n"
198 exe "normal 'ajgR0\<C-D> 1\nA\nBCDEFGHIJ\n\tKL\nMNO\nPQR" . repeat("\<BS>", 29)
199 call assert_equal([' 1',
200 \ 'abcdefghi',
201 \ 'jk lmn',
202 \ ' opq rst',
203 \ 'uvwxyz'], getline(7, 11))
204 normal G
205 exe "normal iab\tcdefghi\tjkl"
206 exe "normal 0gRAB......CDEFGHI.J\<Esc>o"
207 exe "normal iabcdefghijklmnopqrst\<Esc>0gRAB\tIJKLMNO\tQR"
208 call assert_equal(['AB......CDEFGHI.Jkl',
209 \ 'AB IJKLMNO QRst'], getline(12, 13))
210 enew!
Bram Moolenaare7808482018-03-06 13:17:23 +0100211 set noai bs&vim
Bram Moolenaardf0d24b2018-03-06 14:22:58 +0100212 if exists('save_t_kD')
213 let &t_kD = save_t_kD
214 endif
215 if exists('save_t_kb')
216 let &t_kb = save_t_kb
217 endif
Bram Moolenaar63e82db2018-03-06 12:10:48 +0100218endfunc
219
220" Test Virtual replace mode.
221func Test_virtual_replace2()
222 enew!
223 set bs=2
224 exe "normal a\nabcdefghi\njk\tlmn\n opq rst\n\<C-D>uvwxyz"
225 call cursor(1,1)
226 " Test 1: Test that del deletes the newline
227 exe "normal gR0\<del> 1\nA\nBCDEFGHIJ\n\tKL\nMNO\nPQR"
228 call assert_equal(['0 1',
229 \ 'A',
230 \ 'BCDEFGHIJ',
231 \ ' KL',
232 \ 'MNO',
233 \ 'PQR',
234 \ ], getline(1, 6))
235 " Test 2:
236 " a newline is not deleted, if no newline has been added in virtual replace mode
237 %d_
238 call setline(1, ['abcd', 'efgh', 'ijkl'])
239 call cursor(2,1)
240 exe "norm! gR1234\<cr>5\<bs>\<bs>\<bs>"
241 call assert_equal(['abcd',
242 \ '123h',
243 \ 'ijkl'], getline(1, '$'))
244 " Test 3:
245 " a newline is deleted, if a newline has been inserted before in virtual replace mode
246 %d_
247 call setline(1, ['abcd', 'efgh', 'ijkl'])
248 call cursor(2,1)
249 exe "norm! gR1234\<cr>\<cr>56\<bs>\<bs>\<bs>"
250 call assert_equal(['abcd',
251 \ '1234',
252 \ 'ijkl'], getline(1, '$'))
253 " Test 4:
254 " delete add a newline, delete it, add it again and check undo
255 %d_
256 call setline(1, ['abcd', 'efgh', 'ijkl'])
257 call cursor(2,1)
258 " break undo sequence explicitly
259 let &ul = &ul
260 exe "norm! gR1234\<cr>\<bs>\<del>56\<cr>"
261 let &ul = &ul
262 call assert_equal(['abcd',
263 \ '123456',
264 \ ''], getline(1, '$'))
265 norm! u
266 call assert_equal(['abcd',
267 \ 'efgh',
268 \ 'ijkl'], getline(1, '$'))
269 " clean up
270 %d_
271 set bs&vim
Bram Moolenaar15993ce2017-10-26 20:21:44 +0200272endfunc
Bram Moolenaar6d3a1942019-01-03 23:10:32 +0100273
Bram Moolenaar81b1ba42019-01-13 16:12:40 +0100274func Test_Visual_word_textobject()
275 new
276 call setline(1, ['First sentence. Second sentence.'])
277
278 " When start and end of visual area are identical, 'aw' or 'iw' select
279 " the whole word.
280 norm! 1go2fcvawy
281 call assert_equal('Second ', @")
282 norm! 1go2fcviwy
283 call assert_equal('Second', @")
284
285 " When start and end of visual area are not identical, 'aw' or 'iw'
286 " extend the word in direction of the end of the visual area.
287 norm! 1go2fcvlawy
288 call assert_equal('cond ', @")
289 norm! gv2awy
290 call assert_equal('cond sentence.', @")
291
292 norm! 1go2fcvliwy
293 call assert_equal('cond', @")
294 norm! gv2iwy
295 call assert_equal('cond sentence', @")
296
297 " Extend visual area in opposite direction.
298 norm! 1go2fcvhawy
299 call assert_equal(' Sec', @")
300 norm! gv2awy
301 call assert_equal(' sentence. Sec', @")
302
303 norm! 1go2fcvhiwy
304 call assert_equal('Sec', @")
305 norm! gv2iwy
306 call assert_equal('. Sec', @")
307
308 bwipe!
309endfunc
310
Bram Moolenaar6d3a1942019-01-03 23:10:32 +0100311func Test_Visual_sentence_textobject()
312 new
Bram Moolenaar81b1ba42019-01-13 16:12:40 +0100313 call setline(1, ['First sentence. Second sentence. Third', 'sentence. Fourth sentence'])
Bram Moolenaar6d3a1942019-01-03 23:10:32 +0100314
315 " When start and end of visual area are identical, 'as' or 'is' select
316 " the whole sentence.
317 norm! 1gofdvasy
318 call assert_equal('Second sentence. ', @")
319 norm! 1gofdvisy
320 call assert_equal('Second sentence.', @")
321
322 " When start and end of visual area are not identical, 'as' or 'is'
323 " extend the sentence in direction of the end of the visual area.
324 norm! 1gofdvlasy
325 call assert_equal('d sentence. ', @")
326 norm! gvasy
327 call assert_equal("d sentence. Third\nsentence. ", @")
328
329 norm! 1gofdvlisy
330 call assert_equal('d sentence.', @")
331 norm! gvisy
332 call assert_equal('d sentence. ', @")
333 norm! gvisy
334 call assert_equal("d sentence. Third\nsentence.", @")
335
336 " Extend visual area in opposite direction.
337 norm! 1gofdvhasy
338 call assert_equal(' Second', @")
339 norm! gvasy
340 call assert_equal("First sentence. Second", @")
341
342 norm! 1gofdvhisy
343 call assert_equal('Second', @")
344 norm! gvisy
345 call assert_equal(' Second', @")
346 norm! gvisy
347 call assert_equal('First sentence. Second', @")
348
349 bwipe!
350endfunc
Bram Moolenaar81b1ba42019-01-13 16:12:40 +0100351
352func Test_Visual_paragraph_textobject()
353 new
354 call setline(1, ['First line.',
355 \ '',
356 \ 'Second line.',
357 \ 'Third line.',
358 \ 'Fourth line.',
359 \ 'Fifth line.',
360 \ '',
361 \ 'Sixth line.'])
362
363 " When start and end of visual area are identical, 'ap' or 'ip' select
364 " the whole paragraph.
365 norm! 4ggvapy
366 call assert_equal("Second line.\nThird line.\nFourth line.\nFifth line.\n\n", @")
367 norm! 4ggvipy
368 call assert_equal("Second line.\nThird line.\nFourth line.\nFifth line.\n", @")
369
370 " When start and end of visual area are not identical, 'ap' or 'ip'
371 " extend the sentence in direction of the end of the visual area.
372 " FIXME: actually, it is not sufficient to have different start and
373 " end of visual selection, the start line and end line have to differ,
374 " which is not consistent with the documentation.
375 norm! 4ggVjapy
376 call assert_equal("Third line.\nFourth line.\nFifth line.\n\n", @")
377 norm! gvapy
378 call assert_equal("Third line.\nFourth line.\nFifth line.\n\nSixth line.\n", @")
379 norm! 4ggVjipy
380 call assert_equal("Third line.\nFourth line.\nFifth line.\n", @")
381 norm! gvipy
382 call assert_equal("Third line.\nFourth line.\nFifth line.\n\n", @")
383 norm! gvipy
384 call assert_equal("Third line.\nFourth line.\nFifth line.\n\nSixth line.\n", @")
385
386 " Extend visual area in opposite direction.
387 norm! 5ggVkapy
388 call assert_equal("\nSecond line.\nThird line.\nFourth line.\n", @")
389 norm! gvapy
390 call assert_equal("First line.\n\nSecond line.\nThird line.\nFourth line.\n", @")
391 norm! 5ggVkipy
392 call assert_equal("Second line.\nThird line.\nFourth line.\n", @")
393 norma gvipy
394 call assert_equal("\nSecond line.\nThird line.\nFourth line.\n", @")
395 norm! gvipy
396 call assert_equal("First line.\n\nSecond line.\nThird line.\nFourth line.\n", @")
397
398 bwipe!
399endfunc