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