blob: cb9897e49326b8c15b839e7e35b0ff11323ec1d6 [file] [log] [blame]
Bram Moolenaar905dd902019-04-07 14:21:47 +02001" Tests for decoding escape sequences sent by the terminal.
2
3" This only works for Unix in a terminal
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02004source check.vim
5CheckNotGui
6CheckUnix
Bram Moolenaar905dd902019-04-07 14:21:47 +02007
Bram Moolenaar564344a2019-04-28 13:00:12 +02008source shared.vim
Bram Moolenaar515545e2020-03-22 14:08:59 +01009source mouse.vim
Bram Moolenaarecd34bf2020-08-04 20:17:31 +020010source view_util.vim
Bram Moolenaare46a2ed2020-08-04 21:04:57 +020011source term_util.vim
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +020012
Bram Moolenaar92fd5992019-05-02 23:00:22 +020013func Test_term_mouse_left_click()
Bram Moolenaar905dd902019-04-07 14:21:47 +020014 new
15 let save_mouse = &mouse
16 let save_term = &term
17 let save_ttymouse = &ttymouse
Bram Moolenaar92fd5992019-05-02 23:00:22 +020018 call test_override('no_query_mouse', 1)
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +020019 set mouse=a term=xterm
Bram Moolenaar905dd902019-04-07 14:21:47 +020020 call setline(1, ['line 1', 'line 2', 'line 3 is a bit longer'])
Bram Moolenaar905dd902019-04-07 14:21:47 +020021
Bram Moolenaar515545e2020-03-22 14:08:59 +010022 for ttymouse_val in g:Ttymouse_values + g:Ttymouse_dec + g:Ttymouse_netterm
Bram Moolenaar49452192019-04-17 16:27:02 +020023 let msg = 'ttymouse=' .. ttymouse_val
Bram Moolenaar1ee36d62019-05-01 23:13:56 +020024 exe 'set ttymouse=' .. ttymouse_val
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +020025 go
Bram Moolenaar49452192019-04-17 16:27:02 +020026 call assert_equal([0, 1, 1, 0], getpos('.'), msg)
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +020027 let row = 2
28 let col = 6
29 call MouseLeftClick(row, col)
30 call MouseLeftRelease(row, col)
Bram Moolenaar49452192019-04-17 16:27:02 +020031 call assert_equal([0, 2, 6, 0], getpos('.'), msg)
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +020032 endfor
Bram Moolenaar905dd902019-04-07 14:21:47 +020033
34 let &mouse = save_mouse
35 let &term = save_term
36 let &ttymouse = save_ttymouse
Bram Moolenaar92fd5992019-05-02 23:00:22 +020037 call test_override('no_query_mouse', 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +020038 bwipe!
39endfunc
40
Bram Moolenaar6aa75232019-10-13 21:01:34 +020041func Test_xterm_mouse_right_click_extends_visual()
42 if has('mac')
43 throw "Skipped: test right click in visual mode does not work on macOs (why?)"
44 endif
45 let save_mouse = &mouse
46 let save_term = &term
47 let save_ttymouse = &ttymouse
48 call test_override('no_query_mouse', 1)
49 set mouse=a term=xterm
50
51 for visual_mode in ["v", "V", "\<C-V>"]
Bram Moolenaar515545e2020-03-22 14:08:59 +010052 for ttymouse_val in g:Ttymouse_values + g:Ttymouse_dec
Bram Moolenaar6aa75232019-10-13 21:01:34 +020053 let msg = 'visual=' .. visual_mode .. ' ttymouse=' .. ttymouse_val
54 exe 'set ttymouse=' .. ttymouse_val
55
56 call setline(1, repeat([repeat('-', 7)], 7))
57 call MouseLeftClick(4, 4)
58 call MouseLeftRelease(4, 4)
59 exe "norm! " .. visual_mode
60
61 " Right click extends top left of visual area.
62 call MouseRightClick(2, 2)
63 call MouseRightRelease(2, 2)
64
Bram Moolenaarf19f8d12019-12-18 19:36:23 +010065 " Right click extends bottom right of visual area.
Bram Moolenaar6aa75232019-10-13 21:01:34 +020066 call MouseRightClick(6, 6)
67 call MouseRightRelease(6, 6)
68 norm! r1gv
69
70 " Right click shrinks top left of visual area.
71 call MouseRightClick(3, 3)
72 call MouseRightRelease(3, 3)
73
74 " Right click shrinks bottom right of visual area.
75 call MouseRightClick(5, 5)
76 call MouseRightRelease(5, 5)
77 norm! r2
78
79 if visual_mode ==# 'v'
80 call assert_equal(['-------',
81 \ '-111111',
82 \ '1122222',
83 \ '2222222',
84 \ '2222211',
85 \ '111111-',
86 \ '-------'], getline(1, '$'), msg)
87 elseif visual_mode ==# 'V'
88 call assert_equal(['-------',
89 \ '1111111',
90 \ '2222222',
91 \ '2222222',
92 \ '2222222',
93 \ '1111111',
94 \ '-------'], getline(1, '$'), msg)
95 else
96 call assert_equal(['-------',
97 \ '-11111-',
98 \ '-12221-',
99 \ '-12221-',
100 \ '-12221-',
101 \ '-11111-',
102 \ '-------'], getline(1, '$'), msg)
103 endif
104 endfor
105 endfor
106
107 let &mouse = save_mouse
108 let &term = save_term
109 let &ttymouse = save_ttymouse
110 call test_override('no_query_mouse', 0)
111 bwipe!
112endfunc
113
Bram Moolenaar1ee36d62019-05-01 23:13:56 +0200114" Test that <C-LeftMouse> jumps to help tag and <C-RightMouse> jumps back.
Bram Moolenaar297bec02020-07-14 22:11:04 +0200115" Also test for g<LeftMouse> and g<RightMouse>.
116func Test_xterm_mouse_tagjump()
Bram Moolenaar1ee36d62019-05-01 23:13:56 +0200117 let save_mouse = &mouse
118 let save_term = &term
119 let save_ttymouse = &ttymouse
120 set mouse=a term=xterm
121
Bram Moolenaar515545e2020-03-22 14:08:59 +0100122 for ttymouse_val in g:Ttymouse_values
Bram Moolenaar1ee36d62019-05-01 23:13:56 +0200123 let msg = 'ttymouse=' .. ttymouse_val
124 exe 'set ttymouse=' .. ttymouse_val
125 help
126 /usr_02.txt
127 norm! zt
Bram Moolenaar297bec02020-07-14 22:11:04 +0200128
129 " CTRL-left click to jump to a tag
Bram Moolenaar1ee36d62019-05-01 23:13:56 +0200130 let row = 1
131 let col = 1
132 call MouseCtrlLeftClick(row, col)
133 call MouseLeftRelease(row, col)
134 call assert_match('usr_02.txt$', bufname('%'), msg)
Bram Moolenaarb4367b72019-10-01 14:19:07 +0200135 call assert_equal('*usr_02.txt*', expand('<cWORD>'), msg)
Bram Moolenaar1ee36d62019-05-01 23:13:56 +0200136
Bram Moolenaar297bec02020-07-14 22:11:04 +0200137 " CTRL-right click to pop a tag
Bram Moolenaar1ee36d62019-05-01 23:13:56 +0200138 call MouseCtrlRightClick(row, col)
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200139 call MouseRightRelease(row, col)
Bram Moolenaar1ee36d62019-05-01 23:13:56 +0200140 call assert_match('help.txt$', bufname('%'), msg)
Bram Moolenaarb4367b72019-10-01 14:19:07 +0200141 call assert_equal('|usr_02.txt|', expand('<cWORD>'), msg)
Bram Moolenaar1ee36d62019-05-01 23:13:56 +0200142
Bram Moolenaar297bec02020-07-14 22:11:04 +0200143 " Jump to a tag
144 exe "normal \<C-]>"
145 call assert_match('usr_02.txt$', bufname('%'), msg)
146 call assert_equal('*usr_02.txt*', expand('<cWORD>'), msg)
147
148 " Use CTRL-right click in insert mode to pop the tag
149 new
150 let str = 'iHello' .. MouseCtrlRightClickCode(row, col)
151 \ .. MouseRightReleaseCode(row, col) .. "\<C-C>"
Bram Moolenaar2764d062020-07-18 12:59:19 +0200152 call assert_fails('call feedkeys(str, "Lx!")', 'E37:', msg)
Bram Moolenaar297bec02020-07-14 22:11:04 +0200153 close!
154
155 " CTRL-right click with a count
156 let str = "4" .. MouseCtrlRightClickCode(row, col)
157 \ .. MouseRightReleaseCode(row, col)
Bram Moolenaar2764d062020-07-18 12:59:19 +0200158 call assert_fails('call feedkeys(str, "Lx!")', 'E555:', msg)
Bram Moolenaar297bec02020-07-14 22:11:04 +0200159 call assert_match('help.txt$', bufname('%'), msg)
160 call assert_equal(1, line('.'), msg)
161
162 " g<LeftMouse> to jump to a tag
163 /usr_02.txt
164 norm! zt
165 call test_setmouse(row, col)
166 exe "normal g\<LeftMouse>"
167 call assert_match('usr_02.txt$', bufname('%'), msg)
168 call assert_equal('*usr_02.txt*', expand('<cWORD>'), msg)
169
170 " g<RightMouse> to pop to a tag
171 call test_setmouse(row, col)
172 exe "normal g\<RightMouse>"
173 call assert_match('help.txt$', bufname('%'), msg)
174 call assert_equal('|usr_02.txt|', expand('<cWORD>'), msg)
175
176 %bw!
Bram Moolenaar1ee36d62019-05-01 23:13:56 +0200177 endfor
178
179 let &mouse = save_mouse
180 let &term = save_term
181 let &ttymouse = save_ttymouse
182endfunc
183
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200184func Test_term_mouse_middle_click()
Bram Moolenaar52992fe2019-08-12 14:20:33 +0200185 CheckFeature clipboard_working
Bram Moolenaar564344a2019-04-28 13:00:12 +0200186
Bram Moolenaarc1b81602019-04-27 19:11:35 +0200187 new
188 let save_mouse = &mouse
189 let save_term = &term
190 let save_ttymouse = &ttymouse
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200191 call test_override('no_query_mouse', 1)
Bram Moolenaarc1b81602019-04-27 19:11:35 +0200192 let save_quotestar = @*
Bram Moolenaar297bec02020-07-14 22:11:04 +0200193 let save_quoteplus = @+
Bram Moolenaarc1b81602019-04-27 19:11:35 +0200194 set mouse=a term=xterm
195
Bram Moolenaar515545e2020-03-22 14:08:59 +0100196 for ttymouse_val in g:Ttymouse_values + g:Ttymouse_dec
Bram Moolenaarc1b81602019-04-27 19:11:35 +0200197 let msg = 'ttymouse=' .. ttymouse_val
Bram Moolenaar1ee36d62019-05-01 23:13:56 +0200198 exe 'set ttymouse=' .. ttymouse_val
Bram Moolenaarc1b81602019-04-27 19:11:35 +0200199 call setline(1, ['123456789', '123456789'])
Bram Moolenaar297bec02020-07-14 22:11:04 +0200200 let @* = 'abc'
Bram Moolenaarc1b81602019-04-27 19:11:35 +0200201
202 " Middle-click in the middle of the line pastes text where clicked.
203 let row = 1
204 let col = 6
205 call MouseMiddleClick(row, col)
206 call MouseMiddleRelease(row, col)
207 call assert_equal(['12345abc6789', '123456789'], getline(1, '$'), msg)
208
209 " Middle-click beyond end of the line pastes text at the end of the line.
210 let col = 20
211 call MouseMiddleClick(row, col)
212 call MouseMiddleRelease(row, col)
213 call assert_equal(['12345abc6789abc', '123456789'], getline(1, '$'), msg)
214
215 " Middle-click beyond the last line pastes in the last line.
216 let row = 5
217 let col = 3
218 call MouseMiddleClick(row, col)
219 call MouseMiddleRelease(row, col)
220 call assert_equal(['12345abc6789abc', '12abc3456789'], getline(1, '$'), msg)
Bram Moolenaar297bec02020-07-14 22:11:04 +0200221
222 " Middle mouse click in operator pending mode beeps
223 call assert_beeps('exe "normal c\<MiddleMouse>"')
224
225 " Clicking middle mouse in visual mode, yanks the selection and pastes the
226 " clipboard contents
227 let save_clipboard = &clipboard
228 set clipboard=
229 let @" = ''
230 call cursor(1, 1)
231 call feedkeys("v3l" ..
232 \ MouseMiddleClickCode(2, 7) .. MouseMiddleReleaseCode(2, 7), 'Lx!')
233 call assert_equal(['12345abc6789abc', '12abc3abc456789'],
234 \ getline(1, '$'), msg)
Bram Moolenaar2764d062020-07-18 12:59:19 +0200235 call assert_equal('1234', @", msg)
Bram Moolenaar297bec02020-07-14 22:11:04 +0200236 let &clipboard = save_clipboard
237
238 " Clicking middle mouse in select mode, replaces the selected text with
239 " the clipboard contents
240 let @+ = 'xyz'
241 call cursor(1, 3)
242 exe "normal gh\<Right>\<Right>\<MiddleMouse>"
243 call assert_equal(['12xyzabc6789abc', '12abc3abc456789'],
244 \ getline(1, '$'), msg)
Bram Moolenaar2764d062020-07-18 12:59:19 +0200245
246 " Prefixing middle click with [ or ] fixes the indent after pasting.
247 %d
248 call setline(1, " one two")
249 call setreg('r', 'red blue', 'l')
250 call test_setmouse(1, 5)
251 exe "normal \"r[\<MiddleMouse>"
252 call assert_equal(' red blue', getline(1), msg)
253 call test_setmouse(2, 5)
254 exe "normal \"r]\<MiddleMouse>"
255 call assert_equal(' red blue', getline(3), msg)
256 %d
Bram Moolenaarc1b81602019-04-27 19:11:35 +0200257 endfor
258
259 let &mouse = save_mouse
260 let &term = save_term
261 let &ttymouse = save_ttymouse
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200262 call test_override('no_query_mouse', 0)
Bram Moolenaarc1b81602019-04-27 19:11:35 +0200263 let @* = save_quotestar
Bram Moolenaar297bec02020-07-14 22:11:04 +0200264 let @+ = save_quotestar
Bram Moolenaarc1b81602019-04-27 19:11:35 +0200265 bwipe!
266endfunc
267
Bram Moolenaar297bec02020-07-14 22:11:04 +0200268" If clipboard is not working, then clicking the middle mouse button in visual
269" mode, will copy and paste the selected text.
270func Test_term_mouse_middle_click_no_clipboard()
271 if has('clipboard_working')
272 throw 'Skipped: clipboard support works'
Bram Moolenaar297bec02020-07-14 22:11:04 +0200273 endif
274 new
275 let save_mouse = &mouse
276 let save_term = &term
277 let save_ttymouse = &ttymouse
278 call test_override('no_query_mouse', 1)
279 set mouse=a term=xterm
280
281 for ttymouse_val in g:Ttymouse_values + g:Ttymouse_dec
282 let msg = 'ttymouse=' .. ttymouse_val
283 exe 'set ttymouse=' .. ttymouse_val
284 call setline(1, ['123456789', '123456789'])
285
286 " Clicking middle mouse in visual mode, yanks the selection and pastes it
287 call cursor(1, 1)
288 call feedkeys("v3l" ..
289 \ MouseMiddleClickCode(2, 7) .. MouseMiddleReleaseCode(2, 7), 'Lx!')
290 call assert_equal(['123456789', '1234561234789'],
291 \ getline(1, '$'), msg)
292 endfor
293
294 call test_override('no_query_mouse', 0)
295 let &ttymouse = save_ttymouse
296 let &term = save_term
297 let &mouse = save_mouse
298 close!
299endfunc
300
301func Test_term_mouse_middle_click_insert_mode()
302 CheckFeature clipboard_working
303
304 new
305 let save_mouse = &mouse
306 let save_term = &term
307 let save_ttymouse = &ttymouse
308 call test_override('no_query_mouse', 1)
309 set mouse=a term=xterm
310
311 for ttymouse_val in g:Ttymouse_values + g:Ttymouse_dec
312 let msg = 'ttymouse=' .. ttymouse_val
313 exe 'set ttymouse=' .. ttymouse_val
314 call setline(1, ['123456789', '123456789'])
315 let @* = 'abc'
316
Dominique Pelle923dce22021-11-21 11:36:04 +0000317 " Middle-click in insert mode doesn't move the cursor but inserts the
318 " contents of a register
Bram Moolenaar297bec02020-07-14 22:11:04 +0200319 call cursor(1, 4)
320 call feedkeys('i' ..
321 \ MouseMiddleClickCode(2, 7) .. MouseMiddleReleaseCode(2, 7) ..
322 \ "\<C-C>", 'Lx!')
323 call assert_equal(['123abc456789', '123456789'],
324 \ getline(1, '$'), msg)
Bram Moolenaar2764d062020-07-18 12:59:19 +0200325 call assert_equal([1, 6], [line('.'), col('.')], msg)
Bram Moolenaar297bec02020-07-14 22:11:04 +0200326
327 " Middle-click in replace mode
328 call cursor(1, 1)
329 call feedkeys('$R' ..
330 \ MouseMiddleClickCode(2, 7) .. MouseMiddleReleaseCode(2, 7) ..
331 \ "\<C-C>", 'Lx!')
332 call assert_equal(['123abc45678abc', '123456789'],
333 \ getline(1, '$'), msg)
Bram Moolenaar2764d062020-07-18 12:59:19 +0200334 call assert_equal([1, 14], [line('.'), col('.')], msg)
Bram Moolenaar297bec02020-07-14 22:11:04 +0200335 endfor
336
337 let &mouse = save_mouse
338 let &term = save_term
339 let &ttymouse = save_ttymouse
340 call test_override('no_query_mouse', 0)
341 close!
342endfunc
343
344" Test for switching window using mouse in insert mode
345func Test_term_mouse_switch_win_insert_mode()
346 5new
347 let save_mouse = &mouse
348 let save_term = &term
349 let save_ttymouse = &ttymouse
350 call test_override('no_query_mouse', 1)
351 set mouse=a term=xterm ttymouse=xterm2
352
353 call feedkeys('ivim' ..
354 \ MouseLeftClickCode(8, 6) .. MouseLeftReleaseCode(8, 6) ..
355 \ "\<C-C>", 'Lx!')
356 call assert_equal(2, winnr())
357 wincmd w
358 call assert_equal('n', mode())
359 call assert_equal(['vim'], getline(1, '$'))
360
361 let &mouse = save_mouse
362 let &term = save_term
363 let &ttymouse = save_ttymouse
364 call test_override('no_query_mouse', 0)
365 close!
366endfunc
367
Dominique Pelle923dce22021-11-21 11:36:04 +0000368" Test for using the mouse to increase the height of the cmdline window
Bram Moolenaar297bec02020-07-14 22:11:04 +0200369func Test_mouse_cmdwin_resize()
370 let save_mouse = &mouse
371 let save_term = &term
372 let save_ttymouse = &ttymouse
373 call test_override('no_query_mouse', 1)
374 set mouse=a term=xterm ttymouse=xterm2
375 5new
376 redraw!
377
378 let h = 0
379 let row = &lines - &cmdwinheight - 2
380 call feedkeys("q:" ..
381 \ MouseLeftClickCode(row, 1) ..
382 \ MouseLeftDragCode(row - 1, 1) ..
383 \ MouseLeftReleaseCode(row - 2, 1) ..
384 \ "alet h = \<C-R>=winheight(0)\<CR>\<CR>", 'Lx!')
385 call assert_equal(&cmdwinheight + 2, h)
386
387 let &mouse = save_mouse
388 let &term = save_term
389 let &ttymouse = save_ttymouse
390 call test_override('no_query_mouse', 0)
391 close!
392endfunc
393
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200394" TODO: for unclear reasons this test fails if it comes after
395" Test_xterm_mouse_ctrl_click()
396func Test_1xterm_mouse_wheel()
Bram Moolenaar049736f2019-04-07 21:55:07 +0200397 new
398 let save_mouse = &mouse
399 let save_term = &term
Bram Moolenaard58d4f92020-07-01 15:49:29 +0200400 let save_wrap = &wrap
Bram Moolenaar049736f2019-04-07 21:55:07 +0200401 let save_ttymouse = &ttymouse
Bram Moolenaard58d4f92020-07-01 15:49:29 +0200402 set mouse=a term=xterm nowrap
403 call setline(1, range(100000000000000, 100000000000100))
Bram Moolenaar049736f2019-04-07 21:55:07 +0200404
Bram Moolenaar515545e2020-03-22 14:08:59 +0100405 for ttymouse_val in g:Ttymouse_values
Bram Moolenaar49452192019-04-17 16:27:02 +0200406 let msg = 'ttymouse=' .. ttymouse_val
Bram Moolenaar1ee36d62019-05-01 23:13:56 +0200407 exe 'set ttymouse=' .. ttymouse_val
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200408 go
Bram Moolenaar49452192019-04-17 16:27:02 +0200409 call assert_equal(1, line('w0'), msg)
410 call assert_equal([0, 1, 1, 0], getpos('.'), msg)
Bram Moolenaar049736f2019-04-07 21:55:07 +0200411
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200412 call MouseWheelDown(1, 1)
Bram Moolenaar49452192019-04-17 16:27:02 +0200413 call assert_equal(4, line('w0'), msg)
414 call assert_equal([0, 4, 1, 0], getpos('.'), msg)
Bram Moolenaar049736f2019-04-07 21:55:07 +0200415
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200416 call MouseWheelDown(1, 1)
Bram Moolenaar49452192019-04-17 16:27:02 +0200417 call assert_equal(7, line('w0'), msg)
418 call assert_equal([0, 7, 1, 0], getpos('.'), msg)
Bram Moolenaar049736f2019-04-07 21:55:07 +0200419
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200420 call MouseWheelUp(1, 1)
Bram Moolenaar49452192019-04-17 16:27:02 +0200421 call assert_equal(4, line('w0'), msg)
422 call assert_equal([0, 7, 1, 0], getpos('.'), msg)
Bram Moolenaar049736f2019-04-07 21:55:07 +0200423
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200424 call MouseWheelUp(1, 1)
Bram Moolenaar49452192019-04-17 16:27:02 +0200425 call assert_equal(1, line('w0'), msg)
426 call assert_equal([0, 7, 1, 0], getpos('.'), msg)
Bram Moolenaard58d4f92020-07-01 15:49:29 +0200427
428 if has('gui')
429 " Horizontal wheel scrolling currently only works when vim is
430 " compiled with gui enabled.
431 call MouseWheelRight(1, 1)
432 call assert_equal(7, 1 + virtcol(".") - wincol(), msg)
433 call assert_equal([0, 7, 7, 0], getpos('.'), msg)
434
435 call MouseWheelRight(1, 1)
436 call assert_equal(13, 1 + virtcol(".") - wincol(), msg)
437 call assert_equal([0, 7, 13, 0], getpos('.'), msg)
438
439 call MouseWheelLeft(1, 1)
440 call assert_equal(7, 1 + virtcol(".") - wincol(), msg)
441 call assert_equal([0, 7, 13, 0], getpos('.'), msg)
442
443 call MouseWheelLeft(1, 1)
444 call assert_equal(1, 1 + virtcol(".") - wincol(), msg)
445 call assert_equal([0, 7, 13, 0], getpos('.'), msg)
446 endif
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200447 endfor
Bram Moolenaar049736f2019-04-07 21:55:07 +0200448
449 let &mouse = save_mouse
450 let &term = save_term
Bram Moolenaard58d4f92020-07-01 15:49:29 +0200451 let &wrap = save_wrap
Bram Moolenaar049736f2019-04-07 21:55:07 +0200452 let &ttymouse = save_ttymouse
453 bwipe!
454endfunc
Bram Moolenaar3fb01a52019-04-09 21:52:02 +0200455
Bram Moolenaarb4367b72019-10-01 14:19:07 +0200456" Test that dragging beyond the window (at the bottom and at the top)
Bram Moolenaarf19f8d12019-12-18 19:36:23 +0100457" scrolls window content by the number of lines beyond the window.
Bram Moolenaarb4367b72019-10-01 14:19:07 +0200458func Test_term_mouse_drag_beyond_window()
459 let save_mouse = &mouse
460 let save_term = &term
461 let save_ttymouse = &ttymouse
462 call test_override('no_query_mouse', 1)
463 set mouse=a term=xterm
464 let col = 1
465 call setline(1, range(1, 100))
466
467 " Split into 3 windows, and go into the middle window
468 " so we test dragging mouse below and above the window.
469 2split
470 wincmd j
471 2split
472
Bram Moolenaar515545e2020-03-22 14:08:59 +0100473 for ttymouse_val in g:Ttymouse_values + g:Ttymouse_dec
Bram Moolenaarb4367b72019-10-01 14:19:07 +0200474 let msg = 'ttymouse=' .. ttymouse_val
475 exe 'set ttymouse=' .. ttymouse_val
476
477 " Line #10 at the top.
478 norm! 10zt
479 redraw
480 call assert_equal(10, winsaveview().topline, msg)
481 call assert_equal(2, winheight(0), msg)
482
483 let row = 4
484 call MouseLeftClick(row, col)
485 call assert_equal(10, winsaveview().topline, msg)
486
487 " Drag downwards. We're still in the window so topline should
488 " not change yet.
489 let row += 1
490 call MouseLeftDrag(row, col)
491 call assert_equal(10, winsaveview().topline, msg)
492
493 " We now leave the window at the bottom, so the window content should
494 " scroll by 1 line, then 2 lines (etc) as we drag further away.
495 let row += 1
496 call MouseLeftDrag(row, col)
497 call assert_equal(11, winsaveview().topline, msg)
498
499 let row += 1
500 call MouseLeftDrag(row, col)
501 call assert_equal(13, winsaveview().topline, msg)
502
503 " Now drag upwards.
504 let row -= 1
505 call MouseLeftDrag(row, col)
506 call assert_equal(14, winsaveview().topline, msg)
507
508 " We're now back in the window so the topline should not change.
509 let row -= 1
510 call MouseLeftDrag(row, col)
511 call assert_equal(14, winsaveview().topline, msg)
512
513 let row -= 1
514 call MouseLeftDrag(row, col)
515 call assert_equal(14, winsaveview().topline, msg)
516
517 " We now leave the window at the top so the window content should
518 " scroll by 1 line, then 2, then 3 (etc) in the opposite direction.
519 let row -= 1
520 call MouseLeftDrag(row, col)
521 call assert_equal(13, winsaveview().topline, msg)
522
523 let row -= 1
524 call MouseLeftDrag(row, col)
525 call assert_equal(11, winsaveview().topline, msg)
526
527 let row -= 1
528 call MouseLeftDrag(row, col)
529 call assert_equal(8, winsaveview().topline, msg)
530
531 call MouseLeftRelease(row, col)
532 call assert_equal(8, winsaveview().topline, msg)
533 call assert_equal(2, winheight(0), msg)
534 endfor
535
536 let &mouse = save_mouse
537 let &term = save_term
538 let &ttymouse = save_ttymouse
539 call test_override('no_query_mouse', 0)
540 bwipe!
541endfunc
542
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200543func Test_term_mouse_drag_window_separator()
Bram Moolenaar3fb01a52019-04-09 21:52:02 +0200544 let save_mouse = &mouse
545 let save_term = &term
546 let save_ttymouse = &ttymouse
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200547 call test_override('no_query_mouse', 1)
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200548 set mouse=a term=xterm
Bram Moolenaar3fb01a52019-04-09 21:52:02 +0200549
Bram Moolenaar515545e2020-03-22 14:08:59 +0100550 for ttymouse_val in g:Ttymouse_values + g:Ttymouse_dec
Bram Moolenaar49452192019-04-17 16:27:02 +0200551 let msg = 'ttymouse=' .. ttymouse_val
Bram Moolenaar1ee36d62019-05-01 23:13:56 +0200552 exe 'set ttymouse=' .. ttymouse_val
Bram Moolenaar3fb01a52019-04-09 21:52:02 +0200553
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200554 " Split horizontally and test dragging the horizontal window separator.
555 split
556 let rowseparator = winheight(0) + 1
557 let row = rowseparator
558 let col = 1
Bram Moolenaarc8b3dda2019-04-12 21:42:52 +0200559
Bram Moolenaar2b00b9b2019-04-17 17:08:27 +0200560 " When 'ttymouse' is 'xterm2', row/col bigger than 223 are not supported.
561 if ttymouse_val !=# 'xterm2' || row <= 223
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200562 call MouseLeftClick(row, col)
563 let row -= 1
564 call MouseLeftDrag(row, col)
Bram Moolenaar7a7db042022-10-31 23:07:11 +0000565 " FIXME: for unknown reason this test fails, related to calling
566 " reset_mouse_got_click() earlier.
567 if ttymouse_val !=# 'xterm2'
568 call assert_equal(rowseparator - 1, winheight(0) + 1, msg)
569 endif
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200570 let row += 1
571 call MouseLeftDrag(row, col)
Bram Moolenaar49452192019-04-17 16:27:02 +0200572 call assert_equal(rowseparator, winheight(0) + 1, msg)
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200573 call MouseLeftRelease(row, col)
Bram Moolenaar49452192019-04-17 16:27:02 +0200574 call assert_equal(rowseparator, winheight(0) + 1, msg)
Bram Moolenaarc8b3dda2019-04-12 21:42:52 +0200575 endif
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200576 bwipe!
Bram Moolenaar3fb01a52019-04-09 21:52:02 +0200577
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200578 " Split vertically and test dragging the vertical window separator.
579 vsplit
580 let colseparator = winwidth(0) + 1
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200581 let row = 1
582 let col = colseparator
Bram Moolenaar3fb01a52019-04-09 21:52:02 +0200583
Bram Moolenaar2b00b9b2019-04-17 17:08:27 +0200584 " When 'ttymouse' is 'xterm2', row/col bigger than 223 are not supported.
585 if ttymouse_val !=# 'xterm2' || col <= 223
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200586 call MouseLeftClick(row, col)
587 let col -= 1
588 call MouseLeftDrag(row, col)
Bram Moolenaar49452192019-04-17 16:27:02 +0200589 call assert_equal(colseparator - 1, winwidth(0) + 1, msg)
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200590 let col += 1
591 call MouseLeftDrag(row, col)
Bram Moolenaar49452192019-04-17 16:27:02 +0200592 call assert_equal(colseparator, winwidth(0) + 1, msg)
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200593 call MouseLeftRelease(row, col)
Bram Moolenaar49452192019-04-17 16:27:02 +0200594 call assert_equal(colseparator, winwidth(0) + 1, msg)
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200595 endif
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200596 bwipe!
597 endfor
Bram Moolenaar3fb01a52019-04-09 21:52:02 +0200598
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200599 let &mouse = save_mouse
600 let &term = save_term
601 let &ttymouse = save_ttymouse
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200602 call test_override('no_query_mouse', 0)
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200603endfunc
Bram Moolenaar3fb01a52019-04-09 21:52:02 +0200604
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200605func Test_term_mouse_drag_statusline()
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200606 let save_mouse = &mouse
607 let save_term = &term
608 let save_ttymouse = &ttymouse
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200609 call test_override('no_query_mouse', 1)
Bram Moolenaarca57ab52019-04-13 14:53:16 +0200610 let save_laststatus = &laststatus
611 set mouse=a term=xterm laststatus=2
Bram Moolenaar3fb01a52019-04-09 21:52:02 +0200612
Bram Moolenaar515545e2020-03-22 14:08:59 +0100613 for ttymouse_val in g:Ttymouse_values + g:Ttymouse_dec
Bram Moolenaar49452192019-04-17 16:27:02 +0200614 let msg = 'ttymouse=' .. ttymouse_val
Bram Moolenaar1ee36d62019-05-01 23:13:56 +0200615 exe 'set ttymouse=' .. ttymouse_val
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200616
Bram Moolenaar49452192019-04-17 16:27:02 +0200617 call assert_equal(1, &cmdheight, msg)
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200618 let rowstatusline = winheight(0) + 1
619 let row = rowstatusline
620 let col = 1
Bram Moolenaarc8b3dda2019-04-12 21:42:52 +0200621
Bram Moolenaar2b00b9b2019-04-17 17:08:27 +0200622 if ttymouse_val ==# 'xterm2' && row > 223
623 " When 'ttymouse' is 'xterm2', row/col bigger than 223 are not supported.
Bram Moolenaarc8b3dda2019-04-12 21:42:52 +0200624 continue
625 endif
626
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200627 call MouseLeftClick(row, col)
628 let row -= 1
629 call MouseLeftDrag(row, col)
Bram Moolenaar49452192019-04-17 16:27:02 +0200630 call assert_equal(2, &cmdheight, msg)
631 call assert_equal(rowstatusline - 1, winheight(0) + 1, msg)
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200632 let row += 1
633 call MouseLeftDrag(row, col)
Bram Moolenaar49452192019-04-17 16:27:02 +0200634 call assert_equal(1, &cmdheight, msg)
635 call assert_equal(rowstatusline, winheight(0) + 1, msg)
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200636 call MouseLeftRelease(row, col)
Bram Moolenaar49452192019-04-17 16:27:02 +0200637 call assert_equal(1, &cmdheight, msg)
638 call assert_equal(rowstatusline, winheight(0) + 1, msg)
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200639 endfor
640
Bram Moolenaar3fb01a52019-04-09 21:52:02 +0200641 let &mouse = save_mouse
642 let &term = save_term
643 let &ttymouse = save_ttymouse
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200644 call test_override('no_query_mouse', 0)
Bram Moolenaarca57ab52019-04-13 14:53:16 +0200645 let &laststatus = save_laststatus
646endfunc
647
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200648func Test_term_mouse_click_tab()
Bram Moolenaarca57ab52019-04-13 14:53:16 +0200649 let save_mouse = &mouse
650 let save_term = &term
651 let save_ttymouse = &ttymouse
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200652 call test_override('no_query_mouse', 1)
Bram Moolenaarca57ab52019-04-13 14:53:16 +0200653 set mouse=a term=xterm
654 let row = 1
655
Bram Moolenaar515545e2020-03-22 14:08:59 +0100656 for ttymouse_val in g:Ttymouse_values + g:Ttymouse_dec + g:Ttymouse_netterm
Bram Moolenaar49452192019-04-17 16:27:02 +0200657 let msg = 'ttymouse=' .. ttymouse_val
Bram Moolenaar1ee36d62019-05-01 23:13:56 +0200658 exe 'set ttymouse=' .. ttymouse_val
Bram Moolenaarca57ab52019-04-13 14:53:16 +0200659 e Xfoo
660 tabnew Xbar
661
662 let a = split(execute(':tabs'), "\n")
663 call assert_equal(['Tab page 1',
664 \ ' Xfoo',
665 \ 'Tab page 2',
Bram Moolenaar49452192019-04-17 16:27:02 +0200666 \ '> Xbar'], a, msg)
Bram Moolenaarca57ab52019-04-13 14:53:16 +0200667
668 " Test clicking on tab names in the tabline at the top.
669 let col = 2
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200670 redraw
Bram Moolenaarca57ab52019-04-13 14:53:16 +0200671 call MouseLeftClick(row, col)
672 call MouseLeftRelease(row, col)
673 let a = split(execute(':tabs'), "\n")
674 call assert_equal(['Tab page 1',
675 \ '> Xfoo',
676 \ 'Tab page 2',
Bram Moolenaar49452192019-04-17 16:27:02 +0200677 \ ' Xbar'], a, msg)
Bram Moolenaarca57ab52019-04-13 14:53:16 +0200678
679 let col = 9
680 call MouseLeftClick(row, col)
681 call MouseLeftRelease(row, col)
682 let a = split(execute(':tabs'), "\n")
683 call assert_equal(['Tab page 1',
684 \ ' Xfoo',
685 \ 'Tab page 2',
Bram Moolenaar49452192019-04-17 16:27:02 +0200686 \ '> Xbar'], a, msg)
Bram Moolenaarca57ab52019-04-13 14:53:16 +0200687
688 %bwipe!
689 endfor
690
691 let &mouse = save_mouse
692 let &term = save_term
693 let &ttymouse = save_ttymouse
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200694 call test_override('no_query_mouse', 0)
Bram Moolenaar3fb01a52019-04-09 21:52:02 +0200695endfunc
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200696
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200697func Test_term_mouse_click_X_to_close_tab()
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200698 let save_mouse = &mouse
699 let save_term = &term
700 let save_ttymouse = &ttymouse
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200701 call test_override('no_query_mouse', 1)
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200702 set mouse=a term=xterm
703 let row = 1
704 let col = &columns
705
Bram Moolenaar515545e2020-03-22 14:08:59 +0100706 for ttymouse_val in g:Ttymouse_values + g:Ttymouse_dec + g:Ttymouse_netterm
Bram Moolenaar2b00b9b2019-04-17 17:08:27 +0200707 if ttymouse_val ==# 'xterm2' && col > 223
708 " When 'ttymouse' is 'xterm2', row/col bigger than 223 are not supported.
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200709 continue
710 endif
Bram Moolenaar49452192019-04-17 16:27:02 +0200711 let msg = 'ttymouse=' .. ttymouse_val
Bram Moolenaar1ee36d62019-05-01 23:13:56 +0200712 exe 'set ttymouse=' .. ttymouse_val
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200713 e Xtab1
714 tabnew Xtab2
715 tabnew Xtab3
716 tabn 2
717
718 let a = split(execute(':tabs'), "\n")
719 call assert_equal(['Tab page 1',
720 \ ' Xtab1',
721 \ 'Tab page 2',
722 \ '> Xtab2',
723 \ 'Tab page 3',
Bram Moolenaar49452192019-04-17 16:27:02 +0200724 \ ' Xtab3'], a, msg)
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200725
726 " Click on "X" in tabline to close current tab i.e. Xtab2.
727 redraw
728 call MouseLeftClick(row, col)
729 call MouseLeftRelease(row, col)
730 let a = split(execute(':tabs'), "\n")
731 call assert_equal(['Tab page 1',
732 \ ' Xtab1',
733 \ 'Tab page 2',
Bram Moolenaar49452192019-04-17 16:27:02 +0200734 \ '> Xtab3'], a, msg)
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200735
736 %bwipe!
737 endfor
738
739 let &mouse = save_mouse
740 let &term = save_term
741 let &ttymouse = save_ttymouse
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200742 call test_override('no_query_mouse', 0)
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200743endfunc
Bram Moolenaare3e38282019-04-15 20:55:31 +0200744
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200745func Test_term_mouse_drag_to_move_tab()
Bram Moolenaare3e38282019-04-15 20:55:31 +0200746 let save_mouse = &mouse
747 let save_term = &term
748 let save_ttymouse = &ttymouse
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200749 call test_override('no_query_mouse', 1)
Bram Moolenaare3e38282019-04-15 20:55:31 +0200750 " Set 'mousetime' to 1 to avoid recognizing a double-click in the loop
751 set mouse=a term=xterm mousetime=1
752 let row = 1
753
Bram Moolenaar515545e2020-03-22 14:08:59 +0100754 for ttymouse_val in g:Ttymouse_values + g:Ttymouse_dec
Bram Moolenaar49452192019-04-17 16:27:02 +0200755 let msg = 'ttymouse=' .. ttymouse_val
Bram Moolenaar1ee36d62019-05-01 23:13:56 +0200756 exe 'set ttymouse=' .. ttymouse_val
Bram Moolenaare3e38282019-04-15 20:55:31 +0200757 e Xtab1
758 tabnew Xtab2
759
760 let a = split(execute(':tabs'), "\n")
761 call assert_equal(['Tab page 1',
762 \ ' Xtab1',
763 \ 'Tab page 2',
Bram Moolenaar49452192019-04-17 16:27:02 +0200764 \ '> Xtab2'], a, msg)
Bram Moolenaare3e38282019-04-15 20:55:31 +0200765 redraw
766
767 " Click in tab2 and drag it to tab1.
768 " Check getcharmod() to verify that click is not
769 " interpreted as a spurious double-click.
770 call MouseLeftClick(row, 10)
Bram Moolenaar49452192019-04-17 16:27:02 +0200771 call assert_equal(0, getcharmod(), msg)
Bram Moolenaare3e38282019-04-15 20:55:31 +0200772 for col in [9, 8, 7, 6]
773 call MouseLeftDrag(row, col)
774 endfor
775 call MouseLeftRelease(row, col)
776 let a = split(execute(':tabs'), "\n")
777 call assert_equal(['Tab page 1',
778 \ '> Xtab2',
779 \ 'Tab page 2',
Bram Moolenaar49452192019-04-17 16:27:02 +0200780 \ ' Xtab1'], a, msg)
Bram Moolenaare3e38282019-04-15 20:55:31 +0200781
Bram Moolenaarf36a2c72019-11-16 18:57:16 +0100782 " Click elsewhere so that click in next iteration is not
783 " interpreted as unwanted double-click.
784 call MouseLeftClick(row, 11)
785 call MouseLeftRelease(row, 11)
786
Bram Moolenaare3e38282019-04-15 20:55:31 +0200787 %bwipe!
788 endfor
789
790 let &mouse = save_mouse
791 let &term = save_term
792 let &ttymouse = save_ttymouse
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200793 call test_override('no_query_mouse', 0)
Bram Moolenaare3e38282019-04-15 20:55:31 +0200794 set mousetime&
795endfunc
796
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200797func Test_term_mouse_double_click_to_create_tab()
Bram Moolenaare3e38282019-04-15 20:55:31 +0200798 let save_mouse = &mouse
799 let save_term = &term
800 let save_ttymouse = &ttymouse
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200801 call test_override('no_query_mouse', 1)
Bram Moolenaare3e38282019-04-15 20:55:31 +0200802 " Set 'mousetime' to a small value, so that double-click works but we don't
803 " have to wait long to avoid a triple-click.
Bram Moolenaarf36a2c72019-11-16 18:57:16 +0100804 set mouse=a term=xterm mousetime=200
Bram Moolenaare3e38282019-04-15 20:55:31 +0200805 let row = 1
806 let col = 10
807
Bram Moolenaar515545e2020-03-22 14:08:59 +0100808 for ttymouse_val in g:Ttymouse_values + g:Ttymouse_dec
Bram Moolenaar49452192019-04-17 16:27:02 +0200809 let msg = 'ttymouse=' .. ttymouse_val
Bram Moolenaar1ee36d62019-05-01 23:13:56 +0200810 exe 'set ttymouse=' .. ttymouse_val
Bram Moolenaare3e38282019-04-15 20:55:31 +0200811 e Xtab1
812 tabnew Xtab2
813
814 let a = split(execute(':tabs'), "\n")
815 call assert_equal(['Tab page 1',
816 \ ' Xtab1',
817 \ 'Tab page 2',
Bram Moolenaar49452192019-04-17 16:27:02 +0200818 \ '> Xtab2'], a, msg)
Bram Moolenaare3e38282019-04-15 20:55:31 +0200819
820 redraw
821 call MouseLeftClick(row, col)
822 " Check getcharmod() to verify that first click is not
823 " interpreted as a spurious double-click.
Bram Moolenaar49452192019-04-17 16:27:02 +0200824 call assert_equal(0, getcharmod(), msg)
Bram Moolenaare3e38282019-04-15 20:55:31 +0200825 call MouseLeftRelease(row, col)
826 call MouseLeftClick(row, col)
Bram Moolenaar49452192019-04-17 16:27:02 +0200827 call assert_equal(32, getcharmod(), msg) " double-click
Bram Moolenaare3e38282019-04-15 20:55:31 +0200828 call MouseLeftRelease(row, col)
829 let a = split(execute(':tabs'), "\n")
830 call assert_equal(['Tab page 1',
831 \ ' Xtab1',
832 \ 'Tab page 2',
833 \ '> [No Name]',
834 \ 'Tab page 3',
Bram Moolenaar49452192019-04-17 16:27:02 +0200835 \ ' Xtab2'], a, msg)
Bram Moolenaare3e38282019-04-15 20:55:31 +0200836
Bram Moolenaarf36a2c72019-11-16 18:57:16 +0100837 " Click elsewhere so that click in next iteration is not
838 " interpreted as unwanted double click.
839 call MouseLeftClick(row, col + 1)
840 call MouseLeftRelease(row, col + 1)
841
Bram Moolenaare3e38282019-04-15 20:55:31 +0200842 %bwipe!
843 endfor
844
845 let &mouse = save_mouse
846 let &term = save_term
847 let &ttymouse = save_ttymouse
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200848 call test_override('no_query_mouse', 0)
Bram Moolenaare3e38282019-04-15 20:55:31 +0200849 set mousetime&
850endfunc
Bram Moolenaar696d6372019-04-17 16:33:46 +0200851
Bram Moolenaarf36a2c72019-11-16 18:57:16 +0100852" Test double/triple/quadruple click in normal mode to visually select.
853func Test_term_mouse_multiple_clicks_to_visually_select()
854 let save_mouse = &mouse
855 let save_term = &term
856 let save_ttymouse = &ttymouse
857 call test_override('no_query_mouse', 1)
Bram Moolenaar2a5c61a2020-12-30 14:59:23 +0100858
859 " 'mousetime' must be sufficiently large, or else the test is flaky when
860 " using a ssh connection with X forwarding; i.e. ssh -X (issue #7563).
861 set mouse=a term=xterm mousetime=600
Bram Moolenaarf36a2c72019-11-16 18:57:16 +0100862 new
863
Bram Moolenaar515545e2020-03-22 14:08:59 +0100864 for ttymouse_val in g:Ttymouse_values + g:Ttymouse_dec
Bram Moolenaarf36a2c72019-11-16 18:57:16 +0100865 let msg = 'ttymouse=' .. ttymouse_val
866 exe 'set ttymouse=' .. ttymouse_val
867 call setline(1, ['foo [foo bar] foo', 'foo'])
868
869 " Double-click on word should visually select the word.
870 call MouseLeftClick(1, 2)
871 call assert_equal(0, getcharmod(), msg)
872 call MouseLeftRelease(1, 2)
873 call MouseLeftClick(1, 2)
874 call assert_equal(32, getcharmod(), msg) " double-click
875 call MouseLeftRelease(1, 2)
876 call assert_equal('v', mode(), msg)
877 norm! r1
878 call assert_equal(['111 [foo bar] foo', 'foo'], getline(1, '$'), msg)
879
880 " Double-click on opening square bracket should visually
881 " select the whole [foo bar].
882 call MouseLeftClick(1, 5)
883 call assert_equal(0, getcharmod(), msg)
884 call MouseLeftRelease(1, 5)
885 call MouseLeftClick(1, 5)
886 call assert_equal(32, getcharmod(), msg) " double-click
887 call MouseLeftRelease(1, 5)
888 call assert_equal('v', mode(), msg)
889 norm! r2
890 call assert_equal(['111 222222222 foo', 'foo'], getline(1, '$'), msg)
891
892 " Triple-click should visually select the whole line.
893 call MouseLeftClick(1, 3)
894 call assert_equal(0, getcharmod(), msg)
895 call MouseLeftRelease(1, 3)
896 call MouseLeftClick(1, 3)
897 call assert_equal(32, getcharmod(), msg) " double-click
898 call MouseLeftRelease(1, 3)
899 call MouseLeftClick(1, 3)
900 call assert_equal(64, getcharmod(), msg) " triple-click
901 call MouseLeftRelease(1, 3)
902 call assert_equal('V', mode(), msg)
903 norm! r3
904 call assert_equal(['33333333333333333', 'foo'], getline(1, '$'), msg)
905
906 " Quadruple-click should start visual block select.
907 call MouseLeftClick(1, 2)
908 call assert_equal(0, getcharmod(), msg)
909 call MouseLeftRelease(1, 2)
910 call MouseLeftClick(1, 2)
911 call assert_equal(32, getcharmod(), msg) " double-click
912 call MouseLeftRelease(1, 2)
913 call MouseLeftClick(1, 2)
914 call assert_equal(64, getcharmod(), msg) " triple-click
915 call MouseLeftRelease(1, 2)
916 call MouseLeftClick(1, 2)
917 call assert_equal(96, getcharmod(), msg) " quadruple-click
918 call MouseLeftRelease(1, 2)
919 call assert_equal("\<c-v>", mode(), msg)
920 norm! r4
921 call assert_equal(['34333333333333333', 'foo'], getline(1, '$'), msg)
Bram Moolenaar2764d062020-07-18 12:59:19 +0200922
923 " Double-click on a space character should visually select all the
924 " consecutive space characters.
925 %d
926 call setline(1, ' one two')
927 call MouseLeftClick(1, 2)
928 call MouseLeftRelease(1, 2)
929 call MouseLeftClick(1, 2)
930 call MouseLeftRelease(1, 2)
931 call assert_equal('v', mode(), msg)
932 norm! r1
933 call assert_equal(['1111one two'], getline(1, '$'), msg)
934
935 " Double-click on a word with exclusive selection
936 set selection=exclusive
937 let @" = ''
938 call MouseLeftClick(1, 10)
939 call MouseLeftRelease(1, 10)
940 call MouseLeftClick(1, 10)
941 call MouseLeftRelease(1, 10)
942 norm! y
943 call assert_equal('two', @", msg)
944
945 " Double click to select a block of text with exclusive selection
946 %d
947 call setline(1, 'one (two) three')
948 call MouseLeftClick(1, 5)
949 call MouseLeftRelease(1, 5)
950 call MouseLeftClick(1, 5)
951 call MouseLeftRelease(1, 5)
952 norm! y
953 call assert_equal(5, col("'<"), msg)
954 call assert_equal(10, col("'>"), msg)
955
956 call MouseLeftClick(1, 9)
957 call MouseLeftRelease(1, 9)
958 call MouseLeftClick(1, 9)
959 call MouseLeftRelease(1, 9)
960 norm! y
961 call assert_equal(5, col("'<"), msg)
962 call assert_equal(10, col("'>"), msg)
963 set selection&
964
965 " Click somewhere else so that the clicks above is not combined with the
966 " clicks in the next iteration.
967 call MouseRightClick(3, 10)
968 call MouseRightRelease(3, 10)
Bram Moolenaarf36a2c72019-11-16 18:57:16 +0100969 endfor
970
971 let &mouse = save_mouse
972 let &term = save_term
973 let &ttymouse = save_ttymouse
974 set mousetime&
975 call test_override('no_query_mouse', 0)
976 bwipe!
977endfunc
978
Bram Moolenaar2764d062020-07-18 12:59:19 +0200979" Test for selecting text in visual blockwise mode using Alt-LeftClick
980func Test_mouse_alt_leftclick()
981 let save_mouse = &mouse
982 let save_term = &term
983 let save_ttymouse = &ttymouse
984 call test_override('no_query_mouse', 1)
985 set mouse=a term=xterm mousetime=200
986 set mousemodel=popup
987 new
988 call setline(1, 'one (two) three')
989
990 for ttymouse_val in g:Ttymouse_values
991 let msg = 'ttymouse=' .. ttymouse_val
992 exe 'set ttymouse=' .. ttymouse_val
993
994 " Left click with the Alt modifier key should extend the selection in
995 " blockwise visual mode.
996 let @" = ''
997 call MouseLeftClick(1, 3)
998 call MouseLeftRelease(1, 3)
999 call MouseAltLeftClick(1, 11)
1000 call MouseLeftRelease(1, 11)
1001 call assert_equal("\<C-V>", mode(), msg)
1002 normal! y
1003 call assert_equal('e (two) t', @")
1004 endfor
1005
1006 let &mouse = save_mouse
1007 let &term = save_term
1008 let &ttymouse = save_ttymouse
1009 set mousetime& mousemodel&
1010 call test_override('no_query_mouse', 0)
1011 close!
1012endfunc
1013
Bram Moolenaar696d6372019-04-17 16:33:46 +02001014func Test_xterm_mouse_click_in_fold_columns()
1015 new
1016 let save_mouse = &mouse
1017 let save_term = &term
1018 let save_ttymouse = &ttymouse
1019 let save_foldcolumn = &foldcolumn
Bram Moolenaar2b00b9b2019-04-17 17:08:27 +02001020 set mouse=a term=xterm foldcolumn=3 ttymouse=xterm2
Bram Moolenaar696d6372019-04-17 16:33:46 +02001021
1022 " Create 2 nested folds.
1023 call setline(1, range(1, 7))
1024 2,6fold
1025 norm! zR
1026 4,5fold
1027 call assert_equal([-1, -1, -1, 4, 4, -1, -1],
1028 \ map(range(1, 7), 'foldclosed(v:val)'))
1029
1030 " Click in "+" of inner fold in foldcolumn should open it.
1031 redraw
1032 let row = 4
1033 let col = 2
1034 call MouseLeftClick(row, col)
1035 call MouseLeftRelease(row, col)
1036 call assert_equal([-1, -1, -1, -1, -1, -1, -1],
1037 \ map(range(1, 7), 'foldclosed(v:val)'))
1038
1039 " Click in "-" of outer fold in foldcolumn should close it.
1040 redraw
1041 let row = 2
1042 let col = 1
1043 call MouseLeftClick(row, col)
1044 call MouseLeftRelease(row, col)
1045 call assert_equal([-1, 2, 2, 2, 2, 2, -1],
1046 \ map(range(1, 7), 'foldclosed(v:val)'))
1047 norm! zR
1048
1049 " Click in "|" of inner fold in foldcolumn should close it.
1050 redraw
1051 let row = 5
1052 let col = 2
1053 call MouseLeftClick(row, col)
1054 call MouseLeftRelease(row, col)
1055 call assert_equal([-1, -1, -1, 4, 4, -1, -1],
1056 \ map(range(1, 7), 'foldclosed(v:val)'))
1057
1058 let &foldcolumn = save_foldcolumn
1059 let &ttymouse = save_ttymouse
1060 let &term = save_term
1061 let &mouse = save_mouse
1062 bwipe!
1063endfunc
Bram Moolenaar66761db2019-06-05 22:07:51 +02001064
Bram Moolenaarf19f8d12019-12-18 19:36:23 +01001065" Left or right click in Ex command line sets position of the cursor.
1066func Test_term_mouse_click_in_cmdline_to_set_pos()
1067 let save_mouse = &mouse
1068 let save_term = &term
1069 let save_ttymouse = &ttymouse
1070 call test_override('no_query_mouse', 1)
1071 set mouse=a term=xterm
1072 let row = &lines
1073
Bram Moolenaar515545e2020-03-22 14:08:59 +01001074 for ttymouse_val in g:Ttymouse_values + g:Ttymouse_dec
Bram Moolenaarab505b12020-03-23 19:28:44 +01001075 " When 'ttymouse' is 'xterm2', row/col bigger than 223 are not supported.
1076 if ttymouse_val !=# 'xterm2' || row <= 223
1077 let msg = 'ttymouse=' .. ttymouse_val
1078 exe 'set ttymouse=' .. ttymouse_val
Bram Moolenaarf19f8d12019-12-18 19:36:23 +01001079
Bram Moolenaarab505b12020-03-23 19:28:44 +01001080
1081 call feedkeys(':"3456789'
1082 \ .. MouseLeftClickCode(row, 7)
1083 \ .. MouseLeftReleaseCode(row, 7) .. 'L'
1084 \ .. MouseRightClickCode(row, 4)
1085 \ .. MouseRightReleaseCode(row, 4) .. 'R'
1086 \ .. "\<CR>", 'Lx!')
1087 call assert_equal('"3R456L789', @:, msg)
1088 endif
Bram Moolenaarf19f8d12019-12-18 19:36:23 +01001089 endfor
1090
1091 let &mouse = save_mouse
1092 let &term = save_term
1093 let &ttymouse = save_ttymouse
1094 set mousetime&
1095 call test_override('no_query_mouse', 0)
1096endfunc
1097
1098" Middle click in command line pastes at position of cursor.
1099func Test_term_mouse_middle_click_in_cmdline_to_paste()
1100 CheckFeature clipboard_working
1101 let save_mouse = &mouse
1102 let save_term = &term
1103 let save_ttymouse = &ttymouse
1104 call test_override('no_query_mouse', 1)
1105 set mouse=a term=xterm
1106 let row = &lines
1107 " Column values does not matter, paste is done at position of cursor.
1108 let col = 1
1109 let @* = 'paste'
1110
Bram Moolenaar515545e2020-03-22 14:08:59 +01001111 for ttymouse_val in g:Ttymouse_values + g:Ttymouse_dec
Bram Moolenaarf19f8d12019-12-18 19:36:23 +01001112 let msg = 'ttymouse=' .. ttymouse_val
1113 exe 'set ttymouse=' .. ttymouse_val
1114
1115 call feedkeys(":\"->"
1116 \ .. MouseMiddleReleaseCode(row, col)
1117 \ .. MouseMiddleClickCode(row, col)
1118 \ .. "<-"
1119 \ .. MouseMiddleReleaseCode(row, col)
1120 \ .. MouseMiddleClickCode(row, col)
1121 \ .. "\<CR>", 'Lx!')
1122 call assert_equal('"->paste<-paste', @:, msg)
1123 endfor
1124
1125 let &mouse = save_mouse
1126 let &term = save_term
1127 let &ttymouse = save_ttymouse
1128 let @* = ''
1129 call test_override('no_query_mouse', 0)
1130endfunc
1131
Bram Moolenaar297bec02020-07-14 22:11:04 +02001132" Test for making sure S-Middlemouse doesn't do anything
1133func Test_term_mouse_shift_middle_click()
1134 new
1135 let save_mouse = &mouse
1136 let save_term = &term
1137 let save_ttymouse = &ttymouse
1138 call test_override('no_query_mouse', 1)
1139 set mouse=a term=xterm ttymouse=xterm2 mousemodel=
1140
1141 call test_setmouse(1, 1)
1142 exe "normal \<S-MiddleMouse>"
1143 call assert_equal([''], getline(1, '$'))
1144 call assert_equal(1, winnr())
1145
1146 let &mouse = save_mouse
1147 let &term = save_term
1148 let &ttymouse = save_ttymouse
1149 set mousemodel&
1150 call test_override('no_query_mouse', 0)
1151 close!
1152endfunc
1153
1154" Test for using mouse in visual mode
1155func Test_term_mouse_visual_mode()
1156 new
1157 let save_mouse = &mouse
1158 let save_term = &term
1159 let save_ttymouse = &ttymouse
1160 call test_override('no_query_mouse', 1)
1161 set term=xterm ttymouse=xterm2
1162
1163 " If visual mode is not present in 'mouse', then left click should not
1164 " do anything in visal mode.
1165 call setline(1, ['one two three four'])
1166 set mouse=nci
1167 call cursor(1, 5)
1168 let @" = ''
1169 call feedkeys("ve"
1170 \ .. MouseLeftClickCode(1, 15) .. MouseLeftReleaseCode(1, 15)
1171 \ .. 'y', 'Lx!')
1172 call assert_equal(5, col('.'))
1173 call assert_equal('two', @")
1174
1175 " Pressing right click in visual mode should change the visual selection
1176 " if 'mousemodel' doesn't contain popup.
1177 " Right click after the visual selection
1178 set mousemodel=
1179 set mouse=a
1180 call test_setmouse(1, 13)
1181 exe "normal 5|ve\<RightMouse>y"
1182 call assert_equal('two three', @")
1183 call assert_equal(5, col('.'))
1184
1185 " Right click before the visual selection
1186 call test_setmouse(1, 9)
1187 exe "normal 15|ve\<RightMouse>y"
1188 call assert_equal('three four', @")
1189 call assert_equal(9, col('.'))
1190
Bram Moolenaar2764d062020-07-18 12:59:19 +02001191 " Right click inside the selection closer to the start of the selection
1192 call test_setmouse(1, 7)
1193 exe "normal 5|vee\<RightMouse>lly"
1194 call assert_equal('three', @")
1195 call assert_equal(9, col('.'))
1196 call assert_equal(9, col("'<"))
1197 call assert_equal(13, col("'>"))
1198
1199 " Right click inside the selection closer to the end of the selection
1200 call test_setmouse(1, 11)
1201 exe "normal 5|vee\<RightMouse>ly"
1202 call assert_equal('two thre', @")
1203 call assert_equal(5, col('.'))
1204 call assert_equal(5, col("'<"))
1205 call assert_equal(12, col("'>"))
1206
1207 " Multi-line selection. Right click inside thse selection.
1208 call setline(1, repeat(['aaaaaa'], 7))
Bram Moolenaar297bec02020-07-14 22:11:04 +02001209 call test_setmouse(3, 1)
1210 exe "normal ggVG\<RightMouse>y"
1211 call assert_equal(3, line("'<"))
Bram Moolenaar2764d062020-07-18 12:59:19 +02001212 call test_setmouse(5, 1)
Bram Moolenaar297bec02020-07-14 22:11:04 +02001213 exe "normal ggVG\<RightMouse>y"
Bram Moolenaar2764d062020-07-18 12:59:19 +02001214 call assert_equal(5, line("'>"))
1215
1216 " Click right in the middle line of the selection
1217 call test_setmouse(4, 3)
1218 exe "normal ggVG$\<RightMouse>y"
1219 call assert_equal(4, line("'<"))
1220 call test_setmouse(4, 4)
1221 exe "normal ggVG$\<RightMouse>y"
Bram Moolenaar297bec02020-07-14 22:11:04 +02001222 call assert_equal(4, line("'>"))
1223
1224 set mousemodel&
1225 let &mouse = save_mouse
1226 let &term = save_term
1227 let &ttymouse = save_ttymouse
1228 call test_override('no_query_mouse', 0)
1229 close!
1230endfunc
1231
Bram Moolenaar0eabd4d2020-03-15 16:13:53 +01001232" Test for displaying the popup menu using the right mouse click
Bram Moolenaar297bec02020-07-14 22:11:04 +02001233func Test_term_mouse_popup_menu()
Bram Moolenaar0eabd4d2020-03-15 16:13:53 +01001234 CheckFeature menu
1235 new
1236 call setline(1, 'popup menu test')
1237 let save_mouse = &mouse
1238 let save_term = &term
1239 let save_ttymouse = &ttymouse
1240 let save_mousemodel = &mousemodel
1241 call test_override('no_query_mouse', 1)
1242 set mouse=a term=xterm mousemodel=popup
1243
1244 menu PopUp.foo :let g:menustr = 'foo'<CR>
1245 menu PopUp.bar :let g:menustr = 'bar'<CR>
1246 menu PopUp.baz :let g:menustr = 'baz'<CR>
1247
Bram Moolenaar515545e2020-03-22 14:08:59 +01001248 for ttymouse_val in g:Ttymouse_values
Bram Moolenaar2764d062020-07-18 12:59:19 +02001249 let msg = 'ttymouse=' .. ttymouse_val
Bram Moolenaar0eabd4d2020-03-15 16:13:53 +01001250 exe 'set ttymouse=' .. ttymouse_val
1251 let g:menustr = ''
1252 call feedkeys(MouseRightClickCode(1, 4)
1253 \ .. MouseRightReleaseCode(1, 4) .. "\<Down>\<Down>\<CR>", "x")
Bram Moolenaar2764d062020-07-18 12:59:19 +02001254 call assert_equal('bar', g:menustr, msg)
Bram Moolenaar0eabd4d2020-03-15 16:13:53 +01001255 endfor
1256
1257 unmenu PopUp
1258 let &mouse = save_mouse
1259 let &term = save_term
1260 let &ttymouse = save_ttymouse
1261 let &mousemodel = save_mousemodel
1262 call test_override('no_query_mouse', 0)
1263 close!
1264endfunc
1265
Bram Moolenaar297bec02020-07-14 22:11:04 +02001266" Test for 'mousemodel' set to popup_setpos to move the cursor where the popup
1267" menu is displayed.
1268func Test_term_mouse_popup_menu_setpos()
1269 CheckFeature menu
1270 5new
1271 call setline(1, ['the dish ran away with the spoon',
1272 \ 'the cow jumped over the moon' ])
1273 let save_mouse = &mouse
1274 let save_term = &term
1275 let save_ttymouse = &ttymouse
1276 let save_mousemodel = &mousemodel
1277 call test_override('no_query_mouse', 1)
1278 set mouse=a term=xterm mousemodel=popup_setpos
1279
1280 nmenu PopUp.foo :let g:menustr = 'foo'<CR>
1281 nmenu PopUp.bar :let g:menustr = 'bar'<CR>
1282 nmenu PopUp.baz :let g:menustr = 'baz'<CR>
1283 vmenu PopUp.foo y:<C-U>let g:menustr = 'foo'<CR>
1284 vmenu PopUp.bar y:<C-U>let g:menustr = 'bar'<CR>
1285 vmenu PopUp.baz y:<C-U>let g:menustr = 'baz'<CR>
1286
1287 for ttymouse_val in g:Ttymouse_values
Bram Moolenaar2764d062020-07-18 12:59:19 +02001288 let msg = 'ttymouse=' .. ttymouse_val
Bram Moolenaar297bec02020-07-14 22:11:04 +02001289 exe 'set ttymouse=' .. ttymouse_val
1290 let g:menustr = ''
1291 call cursor(1, 1)
1292 call feedkeys(MouseRightClickCode(1, 5)
1293 \ .. MouseRightReleaseCode(1, 5) .. "\<Down>\<Down>\<CR>", "x")
Bram Moolenaar2764d062020-07-18 12:59:19 +02001294 call assert_equal('bar', g:menustr, msg)
1295 call assert_equal([1, 5], [line('.'), col('.')], msg)
Bram Moolenaar297bec02020-07-14 22:11:04 +02001296
1297 " Test for right click in visual mode inside the selection
1298 let @" = ''
1299 call cursor(1, 10)
1300 call feedkeys('vee' .. MouseRightClickCode(1, 12)
1301 \ .. MouseRightReleaseCode(1, 12) .. "\<Down>\<CR>", "x")
Bram Moolenaar2764d062020-07-18 12:59:19 +02001302 call assert_equal([1, 10], [line('.'), col('.')], msg)
1303 call assert_equal('ran away', @", msg)
Bram Moolenaar297bec02020-07-14 22:11:04 +02001304
Yee Cheng Chin17822c52022-10-13 13:17:40 +01001305 " Test for right click in visual mode right before the selection
Bram Moolenaar297bec02020-07-14 22:11:04 +02001306 let @" = ''
1307 call cursor(1, 10)
Yee Cheng Chin17822c52022-10-13 13:17:40 +01001308 call feedkeys('vee' .. MouseRightClickCode(1, 9)
1309 \ .. MouseRightReleaseCode(1, 9) .. "\<Down>\<CR>", "x")
1310 call assert_equal([1, 9], [line('.'), col('.')], msg)
Bram Moolenaar2764d062020-07-18 12:59:19 +02001311 call assert_equal('', @", msg)
Bram Moolenaar297bec02020-07-14 22:11:04 +02001312
Yee Cheng Chin17822c52022-10-13 13:17:40 +01001313 " Test for right click in visual mode right after the selection
Bram Moolenaar297bec02020-07-14 22:11:04 +02001314 let @" = ''
1315 call cursor(1, 10)
Yee Cheng Chin17822c52022-10-13 13:17:40 +01001316 call feedkeys('vee' .. MouseRightClickCode(1, 18)
1317 \ .. MouseRightReleaseCode(1, 18) .. "\<Down>\<CR>", "x")
1318 call assert_equal([1, 18], [line('.'), col('.')], msg)
Bram Moolenaar2764d062020-07-18 12:59:19 +02001319 call assert_equal('', @", msg)
Bram Moolenaar297bec02020-07-14 22:11:04 +02001320
1321 " Test for right click in block-wise visual mode inside the selection
1322 let @" = ''
1323 call cursor(1, 16)
1324 call feedkeys("\<C-V>j3l" .. MouseRightClickCode(2, 17)
1325 \ .. MouseRightReleaseCode(2, 17) .. "\<Down>\<CR>", "x")
Bram Moolenaar2764d062020-07-18 12:59:19 +02001326 call assert_equal([1, 16], [line('.'), col('.')], msg)
1327 call assert_equal("\<C-V>4", getregtype('"'), msg)
Bram Moolenaar297bec02020-07-14 22:11:04 +02001328
1329 " Test for right click in block-wise visual mode outside the selection
1330 let @" = ''
1331 call cursor(1, 16)
1332 call feedkeys("\<C-V>j3l" .. MouseRightClickCode(2, 2)
1333 \ .. MouseRightReleaseCode(2, 2) .. "\<Down>\<CR>", "x")
Bram Moolenaar2764d062020-07-18 12:59:19 +02001334 call assert_equal([2, 2], [line('.'), col('.')], msg)
1335 call assert_equal('v', getregtype('"'), msg)
1336 call assert_equal('', @", msg)
Bram Moolenaar297bec02020-07-14 22:11:04 +02001337
Yee Cheng Chin17822c52022-10-13 13:17:40 +01001338 " Test for right click in line-wise visual mode inside the selection
1339 let @" = ''
1340 call cursor(1, 16)
1341 call feedkeys("V" .. MouseRightClickCode(1, 10)
1342 \ .. MouseRightReleaseCode(1, 10) .. "\<Down>\<CR>", "x")
1343 call assert_equal([1, 1], [line('.'), col('.')], msg) " After yanking, the cursor goes to 1,1
1344 call assert_equal("V", getregtype('"'), msg)
zeertzjqdf63f052022-10-19 15:12:54 +01001345 call assert_equal(1, len(getreg('"', 1, v:true)), msg)
Yee Cheng Chin17822c52022-10-13 13:17:40 +01001346
1347 " Test for right click in multi-line line-wise visual mode inside the selection
1348 let @" = ''
1349 call cursor(1, 16)
1350 call feedkeys("Vj" .. MouseRightClickCode(2, 20)
1351 \ .. MouseRightReleaseCode(2, 20) .. "\<Down>\<CR>", "x")
1352 call assert_equal([1, 1], [line('.'), col('.')], msg) " After yanking, the cursor goes to 1,1
1353 call assert_equal("V", getregtype('"'), msg)
zeertzjqdf63f052022-10-19 15:12:54 +01001354 call assert_equal(2, len(getreg('"', 1, v:true)), msg)
Yee Cheng Chin17822c52022-10-13 13:17:40 +01001355
1356 " Test for right click in line-wise visual mode outside the selection
1357 let @" = ''
1358 call cursor(1, 16)
1359 call feedkeys("V" .. MouseRightClickCode(2, 10)
1360 \ .. MouseRightReleaseCode(2, 10) .. "\<Down>\<CR>", "x")
1361 call assert_equal([2, 10], [line('.'), col('.')], msg)
1362 call assert_equal("", @", msg)
1363
Bram Moolenaar297bec02020-07-14 22:11:04 +02001364 " Try clicking on the status line
1365 let @" = ''
1366 call cursor(1, 10)
1367 call feedkeys('vee' .. MouseRightClickCode(6, 2)
1368 \ .. MouseRightReleaseCode(6, 2) .. "\<Down>\<CR>", "x")
Bram Moolenaar2764d062020-07-18 12:59:19 +02001369 call assert_equal([1, 10], [line('.'), col('.')], msg)
1370 call assert_equal('ran away', @", msg)
Bram Moolenaar297bec02020-07-14 22:11:04 +02001371
1372 " Try clicking outside the window
1373 let @" = ''
zeertzjqdf63f052022-10-19 15:12:54 +01001374 call cursor(2, 2)
Bram Moolenaar297bec02020-07-14 22:11:04 +02001375 call feedkeys('vee' .. MouseRightClickCode(7, 2)
1376 \ .. MouseRightReleaseCode(7, 2) .. "\<Down>\<CR>", "x")
Bram Moolenaar2764d062020-07-18 12:59:19 +02001377 call assert_equal(2, winnr(), msg)
1378 call assert_equal('', @", msg)
Bram Moolenaar297bec02020-07-14 22:11:04 +02001379 wincmd w
1380 endfor
1381
1382 unmenu PopUp
1383 let &mouse = save_mouse
1384 let &term = save_term
1385 let &ttymouse = save_ttymouse
1386 let &mousemodel = save_mousemodel
1387 call test_override('no_query_mouse', 0)
1388 close!
1389endfunc
1390
1391" Test for searching for the word under the cursor using Shift-Right or
1392" Shift-Left click.
1393func Test_term_mouse_search()
1394 new
1395 let save_mouse = &mouse
1396 let save_term = &term
1397 let save_ttymouse = &ttymouse
1398 call test_override('no_query_mouse', 1)
1399 set mouse=a term=xterm ttymouse=xterm2
1400 set mousemodel=
1401
1402 " In normal mode, Shift-Left or Shift-Right click should search for the word
1403 " under the cursor.
1404 call setline(1, ['one two three four', 'four three two one'])
1405 call test_setmouse(1, 4)
1406 exe "normal \<S-LeftMouse>"
1407 call assert_equal([2, 12], [line('.'), col('.')])
1408 call test_setmouse(2, 16)
1409 exe "normal \<S-RightMouse>"
1410 call assert_equal([1, 1], [line('.'), col('.')])
1411
1412 " In visual mode, Shift-Left or Shift-Right click should search for the word
1413 " under the cursor and extend the selection.
1414 call test_setmouse(1, 4)
1415 exe "normal 4|ve\<S-LeftMouse>y"
1416 call assert_equal([2, 12], [line("'>"), col("'>")])
1417 call test_setmouse(2, 16)
1418 exe "normal 2G16|ve\<S-RightMouse>y"
1419 call assert_equal([1, 1], [line("'<"), col("'<")])
1420
1421 set mousemodel&
1422 let &mouse = save_mouse
1423 let &term = save_term
1424 let &ttymouse = save_ttymouse
1425 call test_override('no_query_mouse', 0)
1426 close!
1427endfunc
1428
1429" Test for selecting an entry in the quickfix/location list window using the
1430" mouse.
1431func Test_term_mouse_quickfix_window()
1432 let save_mouse = &mouse
1433 let save_term = &term
1434 let save_ttymouse = &ttymouse
1435 call test_override('no_query_mouse', 1)
1436 set mouse=a term=xterm ttymouse=xterm2
1437 set mousemodel=
1438
1439 cgetexpr "Xfile1:1:L1"
1440 copen 5
1441 call test_setmouse(&lines - 7, 1)
1442 exe "normal \<2-LeftMouse>"
1443 call assert_equal('Xfile1', @%)
1444 %bw!
1445
1446 lgetexpr "Xfile2:1:L1"
1447 lopen 5
1448 call test_setmouse(&lines - 7, 1)
1449 exe "normal \<2-LeftMouse>"
1450 call assert_equal('Xfile2', @%)
1451 %bw!
1452
1453 set mousemodel&
1454 let &mouse = save_mouse
1455 let &term = save_term
1456 let &ttymouse = save_ttymouse
1457 call test_override('no_query_mouse', 0)
1458endfunc
1459
Bram Moolenaar2764d062020-07-18 12:59:19 +02001460" Test for the 'h' flag in the 'mouse' option. Using mouse in the help window.
1461func Test_term_mouse_help_window()
1462 let save_mouse = &mouse
1463 let save_term = &term
1464 let save_ttymouse = &ttymouse
1465 call test_override('no_query_mouse', 1)
1466 set mouse=h term=xterm mousetime=200
1467
1468 for ttymouse_val in g:Ttymouse_values + g:Ttymouse_dec
1469 let msg = 'ttymouse=' .. ttymouse_val
1470 exe 'set ttymouse=' .. ttymouse_val
1471 help
1472 let @" = ''
1473 call MouseLeftClick(2, 5)
1474 call MouseLeftRelease(2, 5)
1475 call MouseLeftClick(1, 1)
1476 call MouseLeftDrag(1, 10)
1477 call MouseLeftRelease(1, 10)
1478 norm! y
1479 call assert_equal('*help.txt*', @", msg)
1480 helpclose
1481
1482 " Click somewhere else to make sure the left click above is not combined
1483 " with the next left click and treated as a double click
1484 call MouseRightClick(5, 10)
1485 call MouseRightRelease(5, 10)
1486 endfor
1487
1488 let &mouse = save_mouse
1489 let &term = save_term
1490 let &ttymouse = save_ttymouse
1491 set mousetime&
1492 call test_override('no_query_mouse', 0)
1493 %bw!
1494endfunc
1495
1496" Test for the translation of various mouse terminal codes
1497func Test_mouse_termcodes()
1498 let save_mouse = &mouse
1499 let save_term = &term
1500 let save_ttymouse = &ttymouse
1501 call test_override('no_query_mouse', 1)
1502 set mouse=a term=xterm mousetime=200
1503
1504 new
1505 for ttymouse_val in g:Ttymouse_values + g:Ttymouse_dec + g:Ttymouse_netterm
1506 let msg = 'ttymouse=' .. ttymouse_val
1507 exe 'set ttymouse=' .. ttymouse_val
1508
1509 let mouse_codes = [
1510 \ ["\<LeftMouse>", "<LeftMouse>"],
1511 \ ["\<MiddleMouse>", "<MiddleMouse>"],
1512 \ ["\<RightMouse>", "<RightMouse>"],
1513 \ ["\<S-LeftMouse>", "<S-LeftMouse>"],
1514 \ ["\<S-MiddleMouse>", "<S-MiddleMouse>"],
1515 \ ["\<S-RightMouse>", "<S-RightMouse>"],
1516 \ ["\<C-LeftMouse>", "<C-LeftMouse>"],
1517 \ ["\<C-MiddleMouse>", "<C-MiddleMouse>"],
1518 \ ["\<C-RightMouse>", "<C-RightMouse>"],
1519 \ ["\<M-LeftMouse>", "<M-LeftMouse>"],
1520 \ ["\<M-MiddleMouse>", "<M-MiddleMouse>"],
1521 \ ["\<M-RightMouse>", "<M-RightMouse>"],
1522 \ ["\<2-LeftMouse>", "<2-LeftMouse>"],
1523 \ ["\<2-MiddleMouse>", "<2-MiddleMouse>"],
1524 \ ["\<2-RightMouse>", "<2-RightMouse>"],
1525 \ ["\<3-LeftMouse>", "<3-LeftMouse>"],
1526 \ ["\<3-MiddleMouse>", "<3-MiddleMouse>"],
1527 \ ["\<3-RightMouse>", "<3-RightMouse>"],
1528 \ ["\<4-LeftMouse>", "<4-LeftMouse>"],
1529 \ ["\<4-MiddleMouse>", "<4-MiddleMouse>"],
1530 \ ["\<4-RightMouse>", "<4-RightMouse>"],
1531 \ ["\<LeftDrag>", "<LeftDrag>"],
1532 \ ["\<MiddleDrag>", "<MiddleDrag>"],
1533 \ ["\<RightDrag>", "<RightDrag>"],
1534 \ ["\<LeftRelease>", "<LeftRelease>"],
1535 \ ["\<MiddleRelease>", "<MiddleRelease>"],
1536 \ ["\<RightRelease>", "<RightRelease>"],
1537 \ ["\<ScrollWheelUp>", "<ScrollWheelUp>"],
1538 \ ["\<S-ScrollWheelUp>", "<S-ScrollWheelUp>"],
1539 \ ["\<C-ScrollWheelUp>", "<C-ScrollWheelUp>"],
1540 \ ["\<ScrollWheelDown>", "<ScrollWheelDown>"],
1541 \ ["\<S-ScrollWheelDown>", "<S-ScrollWheelDown>"],
1542 \ ["\<C-ScrollWheelDown>", "<C-ScrollWheelDown>"],
1543 \ ["\<ScrollWheelLeft>", "<ScrollWheelLeft>"],
1544 \ ["\<S-ScrollWheelLeft>", "<S-ScrollWheelLeft>"],
1545 \ ["\<C-ScrollWheelLeft>", "<C-ScrollWheelLeft>"],
1546 \ ["\<ScrollWheelRight>", "<ScrollWheelRight>"],
1547 \ ["\<S-ScrollWheelRight>", "<S-ScrollWheelRight>"],
1548 \ ["\<C-ScrollWheelRight>", "<C-ScrollWheelRight>"]
1549 \ ]
1550
1551 for [code, outstr] in mouse_codes
1552 exe "normal ggC\<C-K>" . code
1553 call assert_equal(outstr, getline(1), msg)
1554 endfor
1555 endfor
1556
1557 let &mouse = save_mouse
1558 let &term = save_term
1559 let &ttymouse = save_ttymouse
1560 set mousetime&
1561 call test_override('no_query_mouse', 0)
1562 %bw!
1563endfunc
1564
Bram Moolenaar66761db2019-06-05 22:07:51 +02001565" This only checks if the sequence is recognized.
Bram Moolenaar66761db2019-06-05 22:07:51 +02001566func Test_term_rgb_response()
1567 set t_RF=x
1568 set t_RB=y
1569
1570 " response to t_RF, 4 digits
1571 let red = 0x12
1572 let green = 0x34
1573 let blue = 0x56
1574 let seq = printf("\<Esc>]10;rgb:%02x00/%02x00/%02x00\x07", red, green, blue)
1575 call feedkeys(seq, 'Lx!')
1576 call assert_equal(seq, v:termrfgresp)
1577
1578 " response to t_RF, 2 digits
1579 let red = 0x78
1580 let green = 0x9a
1581 let blue = 0xbc
1582 let seq = printf("\<Esc>]10;rgb:%02x/%02x/%02x\x07", red, green, blue)
1583 call feedkeys(seq, 'Lx!')
1584 call assert_equal(seq, v:termrfgresp)
1585
Bram Moolenaar32e19772019-06-05 22:57:04 +02001586 " response to t_RB, 4 digits, dark
1587 set background=light
Bram Moolenaarce90e362019-09-08 18:58:44 +02001588 eval 'background'->test_option_not_set()
Bram Moolenaar32e19772019-06-05 22:57:04 +02001589 let red = 0x29
1590 let green = 0x4a
1591 let blue = 0x6b
1592 let seq = printf("\<Esc>]11;rgb:%02x00/%02x00/%02x00\x07", red, green, blue)
1593 call feedkeys(seq, 'Lx!')
1594 call assert_equal(seq, v:termrbgresp)
1595 call assert_equal('dark', &background)
1596
1597 " response to t_RB, 4 digits, light
1598 set background=dark
1599 call test_option_not_set('background')
1600 let red = 0x81
1601 let green = 0x63
Bram Moolenaar66761db2019-06-05 22:07:51 +02001602 let blue = 0x65
1603 let seq = printf("\<Esc>]11;rgb:%02x00/%02x00/%02x00\x07", red, green, blue)
1604 call feedkeys(seq, 'Lx!')
1605 call assert_equal(seq, v:termrbgresp)
Bram Moolenaar32e19772019-06-05 22:57:04 +02001606 call assert_equal('light', &background)
Bram Moolenaar66761db2019-06-05 22:07:51 +02001607
Bram Moolenaar32e19772019-06-05 22:57:04 +02001608 " response to t_RB, 2 digits, dark
1609 set background=light
1610 call test_option_not_set('background')
1611 let red = 0x47
1612 let green = 0x59
1613 let blue = 0x5b
Bram Moolenaar66761db2019-06-05 22:07:51 +02001614 let seq = printf("\<Esc>]11;rgb:%02x/%02x/%02x\x07", red, green, blue)
1615 call feedkeys(seq, 'Lx!')
1616 call assert_equal(seq, v:termrbgresp)
Bram Moolenaar32e19772019-06-05 22:57:04 +02001617 call assert_equal('dark', &background)
Bram Moolenaarf19f8d12019-12-18 19:36:23 +01001618
Bram Moolenaar32e19772019-06-05 22:57:04 +02001619 " response to t_RB, 2 digits, light
1620 set background=dark
1621 call test_option_not_set('background')
1622 let red = 0x83
1623 let green = 0xa4
1624 let blue = 0xc2
1625 let seq = printf("\<Esc>]11;rgb:%02x/%02x/%02x\x07", red, green, blue)
1626 call feedkeys(seq, 'Lx!')
1627 call assert_equal(seq, v:termrbgresp)
1628 call assert_equal('light', &background)
Bram Moolenaarf19f8d12019-12-18 19:36:23 +01001629
Bram Moolenaar66761db2019-06-05 22:07:51 +02001630 set t_RF= t_RB=
1631endfunc
1632
1633" This only checks if the sequence is recognized.
Bram Moolenaar1a4cbb12019-10-12 13:25:44 +02001634" This must be after other tests, because it has side effects to xterm
1635" properties.
1636func Test_xx01_term_style_response()
Bram Moolenaar66761db2019-06-05 22:07:51 +02001637 " Termresponse is only parsed when t_RV is not empty.
1638 set t_RV=x
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001639 call test_override('term_props', 1)
Bram Moolenaar66761db2019-06-05 22:07:51 +02001640
1641 " send the termresponse to trigger requesting the XT codes
1642 let seq = "\<Esc>[>41;337;0c"
1643 call feedkeys(seq, 'Lx!')
1644 call assert_equal(seq, v:termresponse)
1645
1646 let seq = "\<Esc>P1$r2 q\<Esc>\\"
1647 call feedkeys(seq, 'Lx!')
1648 call assert_equal(seq, v:termstyleresp)
1649
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001650 call assert_equal(#{
1651 \ cursor_style: 'u',
1652 \ cursor_blink_mode: 'u',
1653 \ underline_rgb: 'u',
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01001654 \ mouse: 's',
1655 \ kitty: 'u',
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001656 \ }, terminalprops())
1657
Bram Moolenaar66761db2019-06-05 22:07:51 +02001658 set t_RV=
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001659 call test_override('term_props', 0)
Bram Moolenaar66761db2019-06-05 22:07:51 +02001660endfunc
Bram Moolenaarde6dbb42019-06-06 11:59:18 +02001661
Bram Moolenaar89577b32019-10-18 21:26:05 +02001662" This checks the iTerm2 version response.
1663" This must be after other tests, because it has side effects to xterm
1664" properties.
1665func Test_xx02_iTerm2_response()
1666 " Termresponse is only parsed when t_RV is not empty.
1667 set t_RV=x
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001668 call test_override('term_props', 1)
Bram Moolenaar89577b32019-10-18 21:26:05 +02001669
1670 " Old versions of iTerm2 used a different style term response.
1671 set ttymouse=xterm
1672 call test_option_not_set('ttymouse')
1673 let seq = "\<Esc>[>0;95;c"
1674 call feedkeys(seq, 'Lx!')
1675 call assert_equal(seq, v:termresponse)
1676 call assert_equal('xterm', &ttymouse)
1677
1678 set ttymouse=xterm
1679 call test_option_not_set('ttymouse')
1680 let seq = "\<Esc>[>0;95;0c"
1681 call feedkeys(seq, 'Lx!')
1682 call assert_equal(seq, v:termresponse)
1683 call assert_equal('sgr', &ttymouse)
1684
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001685 call assert_equal(#{
1686 \ cursor_style: 'n',
1687 \ cursor_blink_mode: 'u',
1688 \ underline_rgb: 'u',
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01001689 \ mouse: 's',
1690 \ kitty: 'u',
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001691 \ }, terminalprops())
1692
Bram Moolenaar89577b32019-10-18 21:26:05 +02001693 set t_RV=
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001694 call test_override('term_props', 0)
Bram Moolenaar89577b32019-10-18 21:26:05 +02001695endfunc
1696
Bram Moolenaard55f9ef2022-08-26 12:26:07 +01001697func Run_libvterm_konsole_response(code)
Bram Moolenaar1a4cbb12019-10-12 13:25:44 +02001698 set ttymouse=xterm
1699 call test_option_not_set('ttymouse')
Bram Moolenaard55f9ef2022-08-26 12:26:07 +01001700 let seq = "\<Esc>[>0;" .. a:code .. ";0c"
Bram Moolenaar1a4cbb12019-10-12 13:25:44 +02001701 call feedkeys(seq, 'Lx!')
1702 call assert_equal(seq, v:termresponse)
1703 call assert_equal('sgr', &ttymouse)
1704
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001705 call assert_equal(#{
1706 \ cursor_style: 'n',
1707 \ cursor_blink_mode: 'u',
1708 \ underline_rgb: 'u',
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01001709 \ mouse: 's',
1710 \ kitty: 'u',
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001711 \ }, terminalprops())
Bram Moolenaard55f9ef2022-08-26 12:26:07 +01001712endfunc
1713
1714" This checks the libvterm version response.
1715" This must be after other tests, because it has side effects to xterm
1716" properties.
1717func Test_xx03_libvterm_konsole_response()
1718 " Termresponse is only parsed when t_RV is not empty.
1719 set t_RV=x
1720 call test_override('term_props', 1)
1721
1722 " libvterm
1723 call Run_libvterm_konsole_response(100)
1724 " Konsole
1725 call Run_libvterm_konsole_response(115)
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001726
Bram Moolenaar1a4cbb12019-10-12 13:25:44 +02001727 set t_RV=
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001728 call test_override('term_props', 0)
Bram Moolenaar1a4cbb12019-10-12 13:25:44 +02001729endfunc
1730
Bram Moolenaar89577b32019-10-18 21:26:05 +02001731" This checks the Mac Terminal.app version response.
1732" This must be after other tests, because it has side effects to xterm
1733" properties.
1734func Test_xx04_Mac_Terminal_response()
1735 " Termresponse is only parsed when t_RV is not empty.
1736 set t_RV=x
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001737 call test_override('term_props', 1)
Bram Moolenaar89577b32019-10-18 21:26:05 +02001738
1739 set ttymouse=xterm
Bram Moolenaar8dff4cb2020-06-14 14:34:16 +02001740 " t_8u is not reset
1741 let &t_8u = "\<Esc>[58;2;%lu;%lu;%lum"
Bram Moolenaar89577b32019-10-18 21:26:05 +02001742 call test_option_not_set('ttymouse')
1743 let seq = "\<Esc>[>1;95;0c"
1744 call feedkeys(seq, 'Lx!')
1745 call assert_equal(seq, v:termresponse)
1746 call assert_equal('sgr', &ttymouse)
1747
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001748 call assert_equal(#{
1749 \ cursor_style: 'n',
1750 \ cursor_blink_mode: 'u',
1751 \ underline_rgb: 'y',
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01001752 \ mouse: 's',
1753 \ kitty: 'u',
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001754 \ }, terminalprops())
Bram Moolenaar8dff4cb2020-06-14 14:34:16 +02001755 call assert_equal("\<Esc>[58;2;%lu;%lu;%lum", &t_8u)
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001756
Bram Moolenaar89577b32019-10-18 21:26:05 +02001757 " Reset is_not_xterm and is_mac_terminal.
1758 set t_RV=
1759 set term=xterm
1760 set t_RV=x
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001761 call test_override('term_props', 0)
Bram Moolenaar89577b32019-10-18 21:26:05 +02001762endfunc
1763
1764" This checks the mintty version response.
1765" This must be after other tests, because it has side effects to xterm
1766" properties.
1767func Test_xx05_mintty_response()
1768 " Termresponse is only parsed when t_RV is not empty.
1769 set t_RV=x
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001770 call test_override('term_props', 1)
Bram Moolenaar89577b32019-10-18 21:26:05 +02001771
1772 set ttymouse=xterm
1773 call test_option_not_set('ttymouse')
1774 let seq = "\<Esc>[>77;20905;0c"
1775 call feedkeys(seq, 'Lx!')
1776 call assert_equal(seq, v:termresponse)
1777 call assert_equal('sgr', &ttymouse)
1778
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001779 call assert_equal(#{
1780 \ cursor_style: 'n',
1781 \ cursor_blink_mode: 'u',
1782 \ underline_rgb: 'y',
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01001783 \ mouse: 's',
1784 \ kitty: 'u',
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001785 \ }, terminalprops())
1786
Bram Moolenaar89577b32019-10-18 21:26:05 +02001787 set t_RV=
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001788 call test_override('term_props', 0)
Bram Moolenaar89577b32019-10-18 21:26:05 +02001789endfunc
1790
1791" This checks the screen version response.
1792" This must be after other tests, because it has side effects to xterm
1793" properties.
1794func Test_xx06_screen_response()
1795 " Termresponse is only parsed when t_RV is not empty.
1796 set t_RV=x
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001797 call test_override('term_props', 1)
Bram Moolenaar89577b32019-10-18 21:26:05 +02001798
1799 " Old versions of screen don't support SGR mouse mode.
1800 set ttymouse=xterm
1801 call test_option_not_set('ttymouse')
1802 let seq = "\<Esc>[>83;40500;0c"
1803 call feedkeys(seq, 'Lx!')
1804 call assert_equal(seq, v:termresponse)
1805 call assert_equal('xterm', &ttymouse)
1806
1807 " screen supports SGR mouse mode starting in version 4.7.
1808 set ttymouse=xterm
1809 call test_option_not_set('ttymouse')
1810 let seq = "\<Esc>[>83;40700;0c"
1811 call feedkeys(seq, 'Lx!')
1812 call assert_equal(seq, v:termresponse)
1813 call assert_equal('sgr', &ttymouse)
1814
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001815 call assert_equal(#{
1816 \ cursor_style: 'n',
1817 \ cursor_blink_mode: 'n',
1818 \ underline_rgb: 'y',
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01001819 \ mouse: 's',
1820 \ kitty: 'u',
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001821 \ }, terminalprops())
1822
Bram Moolenaar89577b32019-10-18 21:26:05 +02001823 set t_RV=
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001824 call test_override('term_props', 0)
Bram Moolenaar89577b32019-10-18 21:26:05 +02001825endfunc
1826
Bram Moolenaard0eaf672022-04-20 19:55:37 +01001827func Do_check_t_8u_set_reset(set_by_user)
1828 set ttymouse=xterm
1829 call test_option_not_set('ttymouse')
1830 let default_value = "\<Esc>[58;2;%lu;%lu;%lum"
1831 let &t_8u = default_value
1832 if !a:set_by_user
1833 call test_option_not_set('t_8u')
1834 endif
1835 let seq = "\<Esc>[>0;279;0c"
1836 call feedkeys(seq, 'Lx!')
1837 call assert_equal(seq, v:termresponse)
1838 call assert_equal('sgr', &ttymouse)
1839
1840 call assert_equal(#{
1841 \ cursor_style: 'u',
1842 \ cursor_blink_mode: 'u',
1843 \ underline_rgb: 'u',
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01001844 \ mouse: 's',
1845 \ kitty: 'u',
Bram Moolenaard0eaf672022-04-20 19:55:37 +01001846 \ }, terminalprops())
1847 call assert_equal(a:set_by_user ? default_value : '', &t_8u)
1848endfunc
1849
Bram Moolenaar03b00472019-10-14 22:22:03 +02001850" This checks the xterm version response.
1851" This must be after other tests, because it has side effects to xterm
1852" properties.
Bram Moolenaar89577b32019-10-18 21:26:05 +02001853func Test_xx07_xterm_response()
Bram Moolenaar03b00472019-10-14 22:22:03 +02001854 " Termresponse is only parsed when t_RV is not empty.
1855 set t_RV=x
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001856 call test_override('term_props', 1)
Bram Moolenaar03b00472019-10-14 22:22:03 +02001857
Bram Moolenaar3cea8a92019-10-17 21:55:24 +02001858 " Do Terminal.app first to check that is_mac_terminal is reset.
1859 set ttymouse=xterm
1860 call test_option_not_set('ttymouse')
1861 let seq = "\<Esc>[>1;95;0c"
1862 call feedkeys(seq, 'Lx!')
1863 call assert_equal(seq, v:termresponse)
1864 call assert_equal('sgr', &ttymouse)
1865
Bram Moolenaar03b00472019-10-14 22:22:03 +02001866 " xterm < 95: "xterm" (actually unmodified)
Bram Moolenaar3cea8a92019-10-17 21:55:24 +02001867 set t_RV=
1868 set term=xterm
1869 set t_RV=x
Bram Moolenaar03b00472019-10-14 22:22:03 +02001870 set ttymouse=xterm
1871 call test_option_not_set('ttymouse')
1872 let seq = "\<Esc>[>0;94;0c"
1873 call feedkeys(seq, 'Lx!')
1874 call assert_equal(seq, v:termresponse)
1875 call assert_equal('xterm', &ttymouse)
1876
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001877 call assert_equal(#{
1878 \ cursor_style: 'n',
1879 \ cursor_blink_mode: 'u',
1880 \ underline_rgb: 'y',
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01001881 \ mouse: 'u',
1882 \ kitty: 'u',
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001883 \ }, terminalprops())
1884
Bram Moolenaar03b00472019-10-14 22:22:03 +02001885 " xterm >= 95 < 277 "xterm2"
1886 set ttymouse=xterm
1887 call test_option_not_set('ttymouse')
1888 let seq = "\<Esc>[>0;267;0c"
1889 call feedkeys(seq, 'Lx!')
1890 call assert_equal(seq, v:termresponse)
1891 call assert_equal('xterm2', &ttymouse)
1892
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001893 call assert_equal(#{
1894 \ cursor_style: 'n',
1895 \ cursor_blink_mode: 'u',
1896 \ underline_rgb: 'u',
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01001897 \ mouse: '2',
1898 \ kitty: 'u',
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001899 \ }, terminalprops())
1900
Bram Moolenaar03b00472019-10-14 22:22:03 +02001901 " xterm >= 277: "sgr"
1902 set ttymouse=xterm
1903 call test_option_not_set('ttymouse')
1904 let seq = "\<Esc>[>0;277;0c"
1905 call feedkeys(seq, 'Lx!')
1906 call assert_equal(seq, v:termresponse)
1907 call assert_equal('sgr', &ttymouse)
1908
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001909 call assert_equal(#{
1910 \ cursor_style: 'n',
1911 \ cursor_blink_mode: 'u',
1912 \ underline_rgb: 'u',
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01001913 \ mouse: 's',
1914 \ kitty: 'u',
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001915 \ }, terminalprops())
1916
Bram Moolenaard0eaf672022-04-20 19:55:37 +01001917 " xterm >= 279: "sgr" and cursor_style not reset; also check t_8u reset,
1918 " except when it was set by the user
1919 call Do_check_t_8u_set_reset(0)
1920 call Do_check_t_8u_set_reset(1)
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001921
Bram Moolenaar03b00472019-10-14 22:22:03 +02001922 set t_RV=
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001923 call test_override('term_props', 0)
Bram Moolenaar03b00472019-10-14 22:22:03 +02001924endfunc
1925
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01001926func Test_xx08_kitty_response()
1927 " Termresponse is only parsed when t_RV is not empty.
1928 set t_RV=x
1929 call test_override('term_props', 1)
1930
1931 set ttymouse=xterm
1932 call test_option_not_set('ttymouse')
1933 let seq = "\<Esc>[>1;4001;12c"
1934 call feedkeys(seq, 'Lx!')
1935 call assert_equal(seq, v:termresponse)
1936 call assert_equal('sgr', &ttymouse)
1937
1938 call assert_equal(#{
1939 \ cursor_style: 'u',
1940 \ cursor_blink_mode: 'u',
1941 \ underline_rgb: 'y',
1942 \ mouse: 's',
1943 \ kitty: 'y',
1944 \ }, terminalprops())
1945
1946 set t_RV=
1947 call test_override('term_props', 0)
1948endfunc
1949
Bram Moolenaar92e5df82021-01-30 15:39:47 +01001950func Test_focus_events()
1951 let save_term = &term
1952 let save_ttymouse = &ttymouse
1953 set term=xterm ttymouse=xterm2
1954
1955 au FocusGained * let g:focus_gained += 1
1956 au FocusLost * let g:focus_lost += 1
1957 let g:focus_gained = 0
1958 let g:focus_lost = 0
1959
1960 call feedkeys("\<Esc>[O", "Lx!")
1961 call assert_equal(1, g:focus_lost)
1962 call feedkeys("\<Esc>[I", "Lx!")
1963 call assert_equal(1, g:focus_gained)
1964
1965 " still works when 'ttymouse' is empty
1966 set ttymouse=
1967 call feedkeys("\<Esc>[O", "Lx!")
1968 call assert_equal(2, g:focus_lost)
1969 call feedkeys("\<Esc>[I", "Lx!")
1970 call assert_equal(2, g:focus_gained)
1971
1972 au! FocusGained
1973 au! FocusLost
1974 let &term = save_term
1975 let &ttymouse = save_ttymouse
1976endfunc
1977
Bram Moolenaarde6dbb42019-06-06 11:59:18 +02001978func Test_get_termcode()
Bram Moolenaareb663282019-10-06 12:02:15 +02001979 try
1980 let k1 = &t_k1
1981 catch /E113/
1982 throw 'Skipped: Unable to query termcodes'
1983 endtry
Bram Moolenaarde6dbb42019-06-06 11:59:18 +02001984 set t_k1=
1985 set t_k1&
1986 call assert_equal(k1, &t_k1)
Bram Moolenaar9aeb3362019-06-06 12:36:15 +02001987
1988 " use external termcap first
1989 set nottybuiltin
1990 set t_k1=
1991 set t_k1&
1992 " when using external termcap may get something else, but it must not be
1993 " empty, since we would fallback to the builtin one.
1994 call assert_notequal('', &t_k1)
1995
1996 if &term =~ 'xterm'
1997 " use internal termcap first
1998 let term_save = &term
1999 let &term = 'builtin_' .. &term
2000 set t_k1=
2001 set t_k1&
2002 call assert_equal(k1, &t_k1)
2003 let &term = term_save
2004 endif
2005
2006 set ttybuiltin
Bram Moolenaarde6dbb42019-06-06 11:59:18 +02002007endfunc
Bram Moolenaar18a79a62019-10-12 15:36:11 +02002008
Bram Moolenaarecd34bf2020-08-04 20:17:31 +02002009func Test_list_builtin_terminals()
Bram Moolenaare46a2ed2020-08-04 21:04:57 +02002010 CheckRunVimInTerminal
Bram Moolenaar4d05af02020-11-27 20:55:00 +01002011 call RunVimInTerminal('', #{rows: 14})
2012 call term_sendkeys('', ":set cmdheight=3\<CR>")
2013 call TermWait('', 100)
2014 call term_sendkeys('', ":set term=xxx\<CR>")
2015 call TermWait('', 100)
2016 call assert_match('builtin_dumb', term_getline('', 11))
2017 call assert_match('Not found in termcap', term_getline('', 12))
2018 call StopVimInTerminal('')
Bram Moolenaarecd34bf2020-08-04 20:17:31 +02002019endfunc
2020
Bram Moolenaar18a79a62019-10-12 15:36:11 +02002021func GetEscCodeCSI27(key, modifier)
2022 let key = printf("%d", char2nr(a:key))
2023 let mod = printf("%d", a:modifier)
2024 return "\<Esc>[27;" .. mod .. ';' .. key .. '~'
2025endfunc
2026
2027func GetEscCodeCSIu(key, modifier)
2028 let key = printf("%d", char2nr(a:key))
2029 let mod = printf("%d", a:modifier)
2030 return "\<Esc>[" .. key .. ';' .. mod .. 'u'
2031endfunc
2032
Trygve Aabergeb9c09c12022-10-14 12:08:24 +01002033func GetEscCodeCSIuWithoutModifier(key)
2034 let key = printf("%d", char2nr(a:key))
2035 return "\<Esc>[" .. key .. 'u'
2036endfunc
2037
Bram Moolenaar18a79a62019-10-12 15:36:11 +02002038" This checks the CSI sequences when in modifyOtherKeys mode.
2039" The mode doesn't need to be enabled, the codes are always detected.
2040func RunTest_modifyOtherKeys(func)
2041 new
Bram Moolenaar459fd782019-10-13 16:43:39 +02002042 set timeoutlen=10
Bram Moolenaar18a79a62019-10-12 15:36:11 +02002043
Bram Moolenaarf19f8d12019-12-18 19:36:23 +01002044 " Shift-X is sent as 'X' with the shift modifier
Bram Moolenaar18a79a62019-10-12 15:36:11 +02002045 call feedkeys('a' .. a:func('X', 2) .. "\<Esc>", 'Lx!')
2046 call assert_equal('X', getline(1))
2047
2048 " Ctrl-i is Tab
2049 call setline(1, '')
2050 call feedkeys('a' .. a:func('i', 5) .. "\<Esc>", 'Lx!')
2051 call assert_equal("\t", getline(1))
2052
2053 " Ctrl-I is also Tab
2054 call setline(1, '')
2055 call feedkeys('a' .. a:func('I', 5) .. "\<Esc>", 'Lx!')
2056 call assert_equal("\t", getline(1))
2057
2058 " Alt-x is ø
2059 call setline(1, '')
2060 call feedkeys('a' .. a:func('x', 3) .. "\<Esc>", 'Lx!')
2061 call assert_equal("ø", getline(1))
2062
2063 " Meta-x is also ø
2064 call setline(1, '')
2065 call feedkeys('a' .. a:func('x', 9) .. "\<Esc>", 'Lx!')
2066 call assert_equal("ø", getline(1))
2067
2068 " Alt-X is Ø
2069 call setline(1, '')
2070 call feedkeys('a' .. a:func('X', 3) .. "\<Esc>", 'Lx!')
2071 call assert_equal("Ø", getline(1))
2072
2073 " Meta-X is ø
2074 call setline(1, '')
2075 call feedkeys('a' .. a:func('X', 9) .. "\<Esc>", 'Lx!')
2076 call assert_equal("Ø", getline(1))
2077
Bram Moolenaar828ffd52019-11-21 23:24:00 +01002078 " Ctrl-6 is Ctrl-^
2079 split aaa
2080 edit bbb
2081 call feedkeys(a:func('6', 5), 'Lx!')
2082 call assert_equal("aaa", bufname())
2083 bwipe aaa
2084 bwipe bbb
2085
Bram Moolenaar0684e362020-12-03 19:54:42 +01002086 " Ctrl-V X 33 is 3
2087 call setline(1, '')
2088 call feedkeys("a\<C-V>" .. a:func('X', 2) .. "33\<Esc>", 'Lx!')
2089 call assert_equal("3", getline(1))
2090
2091 " Ctrl-V U 12345 is Unicode 12345
2092 call setline(1, '')
2093 call feedkeys("a\<C-V>" .. a:func('U', 2) .. "12345\<Esc>", 'Lx!')
2094 call assert_equal("\U12345", getline(1))
2095
Bram Moolenaar18a79a62019-10-12 15:36:11 +02002096 bwipe!
2097 set timeoutlen&
2098endfunc
2099
Bram Moolenaar459fd782019-10-13 16:43:39 +02002100func Test_modifyOtherKeys_basic()
Bram Moolenaar18a79a62019-10-12 15:36:11 +02002101 call RunTest_modifyOtherKeys(function('GetEscCodeCSI27'))
Bram Moolenaar18a79a62019-10-12 15:36:11 +02002102 call RunTest_modifyOtherKeys(function('GetEscCodeCSIu'))
2103endfunc
Bram Moolenaard1e2f392019-10-12 18:22:50 +02002104
Bram Moolenaar38571a02019-11-26 14:28:15 +01002105func Test_modifyOtherKeys_no_mapping()
2106 set timeoutlen=10
2107
2108 let @a = 'aaa'
2109 call feedkeys(":let x = '" .. GetEscCodeCSI27('R', 5) .. GetEscCodeCSI27('R', 5) .. "a'\<CR>", 'Lx!')
2110 call assert_equal("let x = 'aaa'", @:)
2111
2112 new
2113 call feedkeys("a" .. GetEscCodeCSI27('R', 5) .. GetEscCodeCSI27('R', 5) .. "a\<Esc>", 'Lx!')
2114 call assert_equal("aaa", getline(1))
2115 bwipe!
2116
2117 new
2118 call feedkeys("axx\<CR>yy" .. GetEscCodeCSI27('G', 5) .. GetEscCodeCSI27('K', 5) .. "a\<Esc>", 'Lx!')
2119 call assert_equal("axx", getline(1))
2120 call assert_equal("yy", getline(2))
2121 bwipe!
2122
2123 set timeoutlen&
2124endfunc
2125
Trygve Aabergeb9c09c12022-10-14 12:08:24 +01002126func Test_CSIu_keys_without_modifiers()
2127 " Escape sent as `CSI 27 u` should act as normal escape and not undo
2128 call setline(1, 'a')
2129 call feedkeys('a' .. GetEscCodeCSIuWithoutModifier("\e"), 'Lx!')
2130 call assert_equal('n', mode())
2131 call assert_equal('a', getline(1))
2132
2133 " Tab sent as `CSI 9 u` should work
2134 call setline(1, '')
2135 call feedkeys('a' .. GetEscCodeCSIuWithoutModifier("\t") .. "\<Esc>", 'Lx!')
2136 call assert_equal("\t", getline(1))
2137endfunc
2138
Bram Moolenaarc14b57c2021-12-03 13:20:29 +00002139" Check that when DEC mouse codes are recognized a special key is handled.
2140func Test_ignore_dec_mouse()
Dominique Pellef589fd32021-12-05 12:39:21 +00002141 silent !infocmp gnome >/dev/null 2>&1
2142 if v:shell_error != 0
2143 throw 'Skipped: gnome entry missing in the terminfo db'
2144 endif
Bram Moolenaarc14b57c2021-12-03 13:20:29 +00002145
2146 new
2147 let save_mouse = &mouse
2148 let save_term = &term
2149 let save_ttymouse = &ttymouse
2150 call test_override('no_query_mouse', 1)
2151 set mouse=a term=gnome ttymouse=
2152
2153 execute "set <xF1>=\<Esc>[1;*P"
2154 nnoremap <S-F1> agot it<Esc>
2155 call feedkeys("\<Esc>[1;2P", 'Lx!')
2156 call assert_equal('got it', getline(1))
2157
2158 let &mouse = save_mouse
2159 let &term = save_term
2160 let &ttymouse = save_ttymouse
2161 call test_override('no_query_mouse', 0)
2162 bwipe!
2163endfunc
2164
Bram Moolenaard1e2f392019-10-12 18:22:50 +02002165func RunTest_mapping_shift(key, func)
2166 call setline(1, '')
2167 if a:key == '|'
2168 exe 'inoremap \| xyz'
2169 else
2170 exe 'inoremap ' .. a:key .. ' xyz'
2171 endif
2172 call feedkeys('a' .. a:func(a:key, 2) .. "\<Esc>", 'Lx!')
2173 call assert_equal("xyz", getline(1))
2174 if a:key == '|'
2175 exe 'iunmap \|'
2176 else
2177 exe 'iunmap ' .. a:key
2178 endif
2179endfunc
2180
Bram Moolenaar975a8802020-06-06 22:36:24 +02002181func Test_modifyOtherKeys_mapped()
2182 set timeoutlen=10
2183 imap ' <C-W>
2184 imap <C-W><C-A> c-a
2185 call setline(1, '')
2186
2187 " single quote is turned into single byte CTRL-W
2188 " CTRL-A is added with a separate modifier, and needs to be simplified before
2189 " the mapping can match.
2190 call feedkeys("a'" .. GetEscCodeCSI27('A', 5) .. "\<Esc>", 'Lx!')
2191 call assert_equal('c-a', getline(1))
2192
2193 iunmap '
2194 iunmap <C-W><C-A>
2195 set timeoutlen&
2196endfunc
2197
Bram Moolenaar196c3852022-03-04 19:22:36 +00002198func Test_modifyOtherKeys_ambiguous_mapping()
2199 new
2200 set timeoutlen=10
2201 map <C-J> a
2202 map <C-J>x <Nop>
2203 call setline(1, 'x')
2204
2205 " CTRL-J b should have trigger the <C-J> mapping and then insert "b"
2206 call feedkeys(GetEscCodeCSI27('J', 5) .. "b\<Esc>", 'Lx!')
2207 call assert_equal('xb', getline(1))
2208
2209 unmap <C-J>
2210 unmap <C-J>x
Bram Moolenaarf35fd8e2022-03-18 15:41:17 +00002211
2212 " if a special character is following there should be a check for a termcode
2213 nnoremap s aX<Esc>
2214 nnoremap s<BS> aY<Esc>
2215 set t_kb=
2216 call setline(1, 'x')
2217 call feedkeys("s\x08", 'Lx!')
2218 call assert_equal('xY', getline(1))
2219
Bram Moolenaar196c3852022-03-04 19:22:36 +00002220 set timeoutlen&
2221 bwipe!
2222endfunc
2223
Bram Moolenaar749bc952020-10-31 16:33:47 +01002224" Whether Shift-Tab sends "ESC [ Z" or "ESC [ 27 ; 2 ; 9 ~" is unpredictable,
2225" both should work.
2226func Test_modifyOtherKeys_shift_tab()
2227 set timeoutlen=10
2228
2229 call setline(1, '')
2230 call feedkeys("a\<C-K>" .. GetEscCodeCSI27("\t", '2') .. "\<Esc>", 'Lx!')
2231 eval getline(1)->assert_equal('<S-Tab>')
2232
2233 call setline(1, '')
2234 call feedkeys("a\<C-K>\<Esc>[Z\<Esc>", 'Lx!')
2235 eval getline(1)->assert_equal('<S-Tab>')
2236
2237 set timeoutlen&
2238 bwipe!
2239endfunc
2240
Bram Moolenaard1e2f392019-10-12 18:22:50 +02002241func RunTest_mapping_works_with_shift(func)
2242 new
Bram Moolenaar459fd782019-10-13 16:43:39 +02002243 set timeoutlen=10
Bram Moolenaard1e2f392019-10-12 18:22:50 +02002244
2245 call RunTest_mapping_shift('@', a:func)
2246 call RunTest_mapping_shift('A', a:func)
2247 call RunTest_mapping_shift('Z', a:func)
2248 call RunTest_mapping_shift('^', a:func)
2249 call RunTest_mapping_shift('_', a:func)
2250 call RunTest_mapping_shift('{', a:func)
2251 call RunTest_mapping_shift('|', a:func)
2252 call RunTest_mapping_shift('}', a:func)
2253 call RunTest_mapping_shift('~', a:func)
2254
2255 bwipe!
2256 set timeoutlen&
2257endfunc
2258
Bram Moolenaar459fd782019-10-13 16:43:39 +02002259func Test_mapping_works_with_shift_plain()
Bram Moolenaard1e2f392019-10-12 18:22:50 +02002260 call RunTest_mapping_works_with_shift(function('GetEscCodeCSI27'))
2261 call RunTest_mapping_works_with_shift(function('GetEscCodeCSIu'))
2262endfunc
Bram Moolenaar459fd782019-10-13 16:43:39 +02002263
2264func RunTest_mapping_mods(map, key, func, code)
2265 call setline(1, '')
2266 exe 'inoremap ' .. a:map .. ' xyz'
2267 call feedkeys('a' .. a:func(a:key, a:code) .. "\<Esc>", 'Lx!')
2268 call assert_equal("xyz", getline(1))
2269 exe 'iunmap ' .. a:map
2270endfunc
2271
2272func RunTest_mapping_works_with_mods(func, mods, code)
2273 new
2274 set timeoutlen=10
2275
2276 if a:mods !~ 'S'
2277 " Shift by itself has no effect
2278 call RunTest_mapping_mods('<' .. a:mods .. '-@>', '@', a:func, a:code)
2279 endif
2280 call RunTest_mapping_mods('<' .. a:mods .. '-A>', 'A', a:func, a:code)
2281 call RunTest_mapping_mods('<' .. a:mods .. '-Z>', 'Z', a:func, a:code)
2282 if a:mods !~ 'S'
2283 " with Shift code is always upper case
2284 call RunTest_mapping_mods('<' .. a:mods .. '-a>', 'a', a:func, a:code)
2285 call RunTest_mapping_mods('<' .. a:mods .. '-z>', 'z', a:func, a:code)
2286 endif
2287 if a:mods != 'A'
2288 " with Alt code is not in upper case
2289 call RunTest_mapping_mods('<' .. a:mods .. '-a>', 'A', a:func, a:code)
2290 call RunTest_mapping_mods('<' .. a:mods .. '-z>', 'Z', a:func, a:code)
2291 endif
2292 call RunTest_mapping_mods('<' .. a:mods .. '-á>', 'á', a:func, a:code)
2293 if a:mods !~ 'S'
2294 " Shift by itself has no effect
2295 call RunTest_mapping_mods('<' .. a:mods .. '-^>', '^', a:func, a:code)
2296 call RunTest_mapping_mods('<' .. a:mods .. '-_>', '_', a:func, a:code)
2297 call RunTest_mapping_mods('<' .. a:mods .. '-{>', '{', a:func, a:code)
2298 call RunTest_mapping_mods('<' .. a:mods .. '-\|>', '|', a:func, a:code)
2299 call RunTest_mapping_mods('<' .. a:mods .. '-}>', '}', a:func, a:code)
2300 call RunTest_mapping_mods('<' .. a:mods .. '-~>', '~', a:func, a:code)
2301 endif
2302
2303 bwipe!
2304 set timeoutlen&
2305endfunc
2306
2307func Test_mapping_works_with_shift()
2308 call RunTest_mapping_works_with_mods(function('GetEscCodeCSI27'), 'S', 2)
2309 call RunTest_mapping_works_with_mods(function('GetEscCodeCSIu'), 'S', 2)
2310endfunc
Bram Moolenaarf19f8d12019-12-18 19:36:23 +01002311
Bram Moolenaar459fd782019-10-13 16:43:39 +02002312func Test_mapping_works_with_ctrl()
2313 call RunTest_mapping_works_with_mods(function('GetEscCodeCSI27'), 'C', 5)
2314 call RunTest_mapping_works_with_mods(function('GetEscCodeCSIu'), 'C', 5)
Bram Moolenaar4e2114e2020-10-07 16:12:37 +02002315
2316 new
2317 set timeoutlen=10
2318
2319 " CTRL-@ actually produces the code for CTRL-2, which is converted
2320 call RunTest_mapping_mods('<C-@>', '2', function('GetEscCodeCSI27'), 5)
2321 call RunTest_mapping_mods('<C-@>', '2', function('GetEscCodeCSIu'), 5)
2322
2323 " CTRL-^ actually produces the code for CTRL-6, which is converted
2324 call RunTest_mapping_mods('<C-^>', '6', function('GetEscCodeCSI27'), 5)
2325 call RunTest_mapping_mods('<C-^>', '6', function('GetEscCodeCSIu'), 5)
2326
2327 " CTRL-_ actually produces the code for CTRL--, which is converted
2328 call RunTest_mapping_mods('<C-_>', '-', function('GetEscCodeCSI27'), 5)
2329 call RunTest_mapping_mods('<C-_>', '-', function('GetEscCodeCSIu'), 5)
2330
2331 bwipe!
2332 set timeoutlen&
Bram Moolenaar459fd782019-10-13 16:43:39 +02002333endfunc
2334
2335func Test_mapping_works_with_shift_ctrl()
2336 call RunTest_mapping_works_with_mods(function('GetEscCodeCSI27'), 'C-S', 6)
2337 call RunTest_mapping_works_with_mods(function('GetEscCodeCSIu'), 'C-S', 6)
Bram Moolenaar9a033d72020-10-07 17:29:48 +02002338
2339 new
2340 set timeoutlen=10
2341
2342 " Ctrl-Shift-[ actually produces CTRL-Shift-{ which is mapped as <C-{>
2343 call RunTest_mapping_mods('<C-{>', '{', function('GetEscCodeCSI27'), 6)
2344 call RunTest_mapping_mods('<C-{>', '{', function('GetEscCodeCSIu'), 6)
2345
2346 " Ctrl-Shift-] actually produces CTRL-Shift-} which is mapped as <C-}>
2347 call RunTest_mapping_mods('<C-{>', '{', function('GetEscCodeCSI27'), 6)
2348 call RunTest_mapping_mods('<C-{>', '{', function('GetEscCodeCSIu'), 6)
2349
2350 " Ctrl-Shift-\ actually produces CTRL-Shift-| which is mapped as <C-|>
2351 call RunTest_mapping_mods('<C-\|>', '|', function('GetEscCodeCSI27'), 6)
2352 call RunTest_mapping_mods('<C-\|>', '|', function('GetEscCodeCSIu'), 6)
2353
2354 bwipe!
2355 set timeoutlen&
Bram Moolenaar459fd782019-10-13 16:43:39 +02002356endfunc
2357
2358" Below we also test the "u" code with Alt, This works, but libvterm would not
2359" send the Alt key like this but by prefixing an Esc.
Bram Moolenaarf19f8d12019-12-18 19:36:23 +01002360
Bram Moolenaar459fd782019-10-13 16:43:39 +02002361func Test_mapping_works_with_alt()
2362 call RunTest_mapping_works_with_mods(function('GetEscCodeCSI27'), 'A', 3)
2363 call RunTest_mapping_works_with_mods(function('GetEscCodeCSIu'), 'A', 3)
2364endfunc
2365
2366func Test_mapping_works_with_shift_alt()
2367 call RunTest_mapping_works_with_mods(function('GetEscCodeCSI27'), 'S-A', 4)
2368 call RunTest_mapping_works_with_mods(function('GetEscCodeCSIu'), 'S-A', 4)
2369endfunc
2370
Bram Moolenaardaff0fb2020-09-27 13:16:46 +02002371func Test_mapping_works_with_alt_and_shift()
2372 new
2373 set timeoutlen=10
2374
2375 " mapping <A-?> works even though the code is A-S-?
2376 for c in ['!', '$', '+', ':', '?', '^', '~']
2377 call RunTest_mapping_mods('<A-' .. c .. '>', c, function('GetEscCodeCSI27'), 4)
2378 call RunTest_mapping_mods('<A-' .. c .. '>', c, function('GetEscCodeCSIu'), 4)
2379 endfor
2380
2381 bwipe!
2382 set timeoutlen&
2383endfunc
2384
Bram Moolenaar459fd782019-10-13 16:43:39 +02002385func Test_mapping_works_with_ctrl_alt()
2386 call RunTest_mapping_works_with_mods(function('GetEscCodeCSI27'), 'C-A', 7)
2387 call RunTest_mapping_works_with_mods(function('GetEscCodeCSIu'), 'C-A', 7)
2388endfunc
2389
2390func Test_mapping_works_with_shift_ctrl_alt()
2391 call RunTest_mapping_works_with_mods(function('GetEscCodeCSI27'), 'C-S-A', 8)
2392 call RunTest_mapping_works_with_mods(function('GetEscCodeCSIu'), 'C-S-A', 8)
2393endfunc
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01002394
2395func Test_insert_literal()
2396 set timeoutlen=10
2397 new
2398 " CTRL-V CTRL-X inserts a ^X
2399 call feedkeys('a' .. GetEscCodeCSIu('V', '5') .. GetEscCodeCSIu('X', '5') .. "\<Esc>", 'Lx!')
2400 call assert_equal("\<C-X>", getline(1))
2401
2402 call setline(1, '')
2403 call feedkeys('a' .. GetEscCodeCSI27('V', '5') .. GetEscCodeCSI27('X', '5') .. "\<Esc>", 'Lx!')
2404 call assert_equal("\<C-X>", getline(1))
2405
Bram Moolenaarf19f8d12019-12-18 19:36:23 +01002406 " CTRL-SHIFT-V CTRL-X inserts escape sequence
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01002407 call setline(1, '')
2408 call feedkeys('a' .. GetEscCodeCSIu('V', '6') .. GetEscCodeCSIu('X', '5') .. "\<Esc>", 'Lx!')
2409 call assert_equal("\<Esc>[88;5u", getline(1))
2410
2411 call setline(1, '')
2412 call feedkeys('a' .. GetEscCodeCSI27('V', '6') .. GetEscCodeCSI27('X', '5') .. "\<Esc>", 'Lx!')
2413 call assert_equal("\<Esc>[27;5;88~", getline(1))
2414
2415 bwipe!
2416 set timeoutlen&
2417endfunc
2418
2419func Test_cmdline_literal()
2420 set timeoutlen=10
2421
2422 " CTRL-V CTRL-Y inserts a ^Y
2423 call feedkeys(':' .. GetEscCodeCSIu('V', '5') .. GetEscCodeCSIu('Y', '5') .. "\<C-B>\"\<CR>", 'Lx!')
2424 call assert_equal("\"\<C-Y>", @:)
2425
2426 call feedkeys(':' .. GetEscCodeCSI27('V', '5') .. GetEscCodeCSI27('Y', '5') .. "\<C-B>\"\<CR>", 'Lx!')
2427 call assert_equal("\"\<C-Y>", @:)
2428
Bram Moolenaarf19f8d12019-12-18 19:36:23 +01002429 " CTRL-SHIFT-V CTRL-Y inserts escape sequence
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01002430 call feedkeys(':' .. GetEscCodeCSIu('V', '6') .. GetEscCodeCSIu('Y', '5') .. "\<C-B>\"\<CR>", 'Lx!')
2431 call assert_equal("\"\<Esc>[89;5u", @:)
2432
2433 call setline(1, '')
2434 call feedkeys(':' .. GetEscCodeCSI27('V', '6') .. GetEscCodeCSI27('Y', '5') .. "\<C-B>\"\<CR>", 'Lx!')
2435 call assert_equal("\"\<Esc>[27;5;89~", @:)
2436
2437 set timeoutlen&
2438endfunc
Bram Moolenaar0eabd4d2020-03-15 16:13:53 +01002439
Bram Moolenaarbbf84e22022-03-12 13:48:39 +00002440func Test_mapping_esc()
2441 set timeoutlen=10
2442
2443 new
2444 nnoremap <Up> iHello<Esc>
2445 nnoremap <Esc> <Nop>
2446
2447 call feedkeys(substitute(&t_ku, '\*', '', 'g'), 'Lx!')
2448 call assert_equal("Hello", getline(1))
2449
2450 bwipe!
2451 nunmap <Up>
2452 nunmap <Esc>
2453 set timeoutlen&
2454endfunc
2455
Bram Moolenaar0ff5ded2020-05-07 18:43:44 +02002456" Test for translation of special key codes (<xF1>, <xF2>, etc.)
Bram Moolenaar92e5df82021-01-30 15:39:47 +01002457func Test_Keycode_Translation()
Bram Moolenaar0ff5ded2020-05-07 18:43:44 +02002458 let keycodes = [
2459 \ ["<xUp>", "<Up>"],
2460 \ ["<xDown>", "<Down>"],
2461 \ ["<xLeft>", "<Left>"],
2462 \ ["<xRight>", "<Right>"],
2463 \ ["<xHome>", "<Home>"],
2464 \ ["<xEnd>", "<End>"],
2465 \ ["<zHome>", "<Home>"],
2466 \ ["<zEnd>", "<End>"],
2467 \ ["<xF1>", "<F1>"],
2468 \ ["<xF2>", "<F2>"],
2469 \ ["<xF3>", "<F3>"],
2470 \ ["<xF4>", "<F4>"],
2471 \ ["<S-xF1>", "<S-F1>"],
2472 \ ["<S-xF2>", "<S-F2>"],
2473 \ ["<S-xF3>", "<S-F3>"],
2474 \ ["<S-xF4>", "<S-F4>"]]
2475 for [k1, k2] in keycodes
2476 exe "nnoremap " .. k1 .. " 2wx"
2477 call assert_true(maparg(k1, 'n', 0, 1).lhs == k2)
2478 exe "nunmap " .. k1
2479 endfor
2480endfunc
2481
Bram Moolenaar1f448d92021-03-22 19:37:06 +01002482" Test for terminal keycodes that doesn't have termcap entries
2483func Test_special_term_keycodes()
2484 new
2485 " Test for <xHome>, <S-xHome> and <C-xHome>
2486 " send <K_SPECIAL> <KS_EXTRA> keycode
2487 call feedkeys("i\<C-K>\x80\xfd\x3f\n", 'xt')
2488 " send <K_SPECIAL> <KS_MODIFIER> bitmap <K_SPECIAL> <KS_EXTRA> keycode
2489 call feedkeys("i\<C-K>\x80\xfc\x2\x80\xfd\x3f\n", 'xt')
2490 call feedkeys("i\<C-K>\x80\xfc\x4\x80\xfd\x3f\n", 'xt')
2491 " Test for <xEnd>, <S-xEnd> and <C-xEnd>
2492 call feedkeys("i\<C-K>\x80\xfd\x3d\n", 'xt')
2493 call feedkeys("i\<C-K>\x80\xfc\x2\x80\xfd\x3d\n", 'xt')
2494 call feedkeys("i\<C-K>\x80\xfc\x4\x80\xfd\x3d\n", 'xt')
2495 " Test for <zHome>, <S-zHome> and <C-zHome>
2496 call feedkeys("i\<C-K>\x80\xfd\x40\n", 'xt')
2497 call feedkeys("i\<C-K>\x80\xfc\x2\x80\xfd\x40\n", 'xt')
2498 call feedkeys("i\<C-K>\x80\xfc\x4\x80\xfd\x40\n", 'xt')
2499 " Test for <zEnd>, <S-zEnd> and <C-zEnd>
2500 call feedkeys("i\<C-K>\x80\xfd\x3e\n", 'xt')
2501 call feedkeys("i\<C-K>\x80\xfc\x2\x80\xfd\x3e\n", 'xt')
2502 call feedkeys("i\<C-K>\x80\xfc\x4\x80\xfd\x3e\n", 'xt')
2503 " Test for <xUp>, <xDown>, <xLeft> and <xRight>
2504 call feedkeys("i\<C-K>\x80\xfd\x41\n", 'xt')
2505 call feedkeys("i\<C-K>\x80\xfd\x42\n", 'xt')
2506 call feedkeys("i\<C-K>\x80\xfd\x43\n", 'xt')
2507 call feedkeys("i\<C-K>\x80\xfd\x44\n", 'xt')
2508 call assert_equal(['<Home>', '<S-Home>', '<C-Home>',
2509 \ '<End>', '<S-End>', '<C-End>',
2510 \ '<Home>', '<S-Home>', '<C-Home>',
2511 \ '<End>', '<S-End>', '<C-End>',
2512 \ '<Up>', '<Down>', '<Left>', '<Right>', ''], getline(1, '$'))
2513 bw!
2514endfunc
2515
Bram Moolenaar0f5575d2021-07-30 21:18:03 +02002516func Test_terminal_builtin_without_gui()
2517 CheckNotMSWindows
2518
2519 " builtin_gui should not be output by :set term=xxx
2520 let output = systemlist("TERM=dumb " .. v:progpath .. " --clean -c ':set t_ti= t_te=' -c 'set term=xxx' -c ':q!'")
2521 redraw!
2522 call map(output, {_, val -> trim(val)})
2523 call assert_equal(-1, index(output, 'builtin_gui'))
2524 call assert_notequal(-1, index(output, 'builtin_dumb'))
2525endfunc
2526
2527
Bram Moolenaar0eabd4d2020-03-15 16:13:53 +01002528" vim: shiftwidth=2 sts=2 expandtab