blob: eb30230140cff0386d7e221edfa5d114ecf40971 [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 Moolenaarc255b782022-11-26 19:16:48 +000020 call WaitForResponses()
21
Bram Moolenaar905dd902019-04-07 14:21:47 +020022 call setline(1, ['line 1', 'line 2', 'line 3 is a bit longer'])
Bram Moolenaar905dd902019-04-07 14:21:47 +020023
Bram Moolenaar515545e2020-03-22 14:08:59 +010024 for ttymouse_val in g:Ttymouse_values + g:Ttymouse_dec + g:Ttymouse_netterm
Bram Moolenaar49452192019-04-17 16:27:02 +020025 let msg = 'ttymouse=' .. ttymouse_val
Bram Moolenaar1ee36d62019-05-01 23:13:56 +020026 exe 'set ttymouse=' .. ttymouse_val
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +020027 go
Bram Moolenaar49452192019-04-17 16:27:02 +020028 call assert_equal([0, 1, 1, 0], getpos('.'), msg)
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +020029 let row = 2
30 let col = 6
31 call MouseLeftClick(row, col)
32 call MouseLeftRelease(row, col)
Bram Moolenaar49452192019-04-17 16:27:02 +020033 call assert_equal([0, 2, 6, 0], getpos('.'), msg)
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +020034 endfor
Bram Moolenaar905dd902019-04-07 14:21:47 +020035
36 let &mouse = save_mouse
37 let &term = save_term
38 let &ttymouse = save_ttymouse
Bram Moolenaar92fd5992019-05-02 23:00:22 +020039 call test_override('no_query_mouse', 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +020040 bwipe!
41endfunc
42
Bram Moolenaar6aa75232019-10-13 21:01:34 +020043func Test_xterm_mouse_right_click_extends_visual()
44 if has('mac')
45 throw "Skipped: test right click in visual mode does not work on macOs (why?)"
46 endif
47 let save_mouse = &mouse
48 let save_term = &term
49 let save_ttymouse = &ttymouse
50 call test_override('no_query_mouse', 1)
51 set mouse=a term=xterm
Bram Moolenaarc255b782022-11-26 19:16:48 +000052 call WaitForResponses()
Bram Moolenaar6aa75232019-10-13 21:01:34 +020053
54 for visual_mode in ["v", "V", "\<C-V>"]
Bram Moolenaar515545e2020-03-22 14:08:59 +010055 for ttymouse_val in g:Ttymouse_values + g:Ttymouse_dec
Bram Moolenaar6aa75232019-10-13 21:01:34 +020056 let msg = 'visual=' .. visual_mode .. ' ttymouse=' .. ttymouse_val
57 exe 'set ttymouse=' .. ttymouse_val
58
59 call setline(1, repeat([repeat('-', 7)], 7))
60 call MouseLeftClick(4, 4)
61 call MouseLeftRelease(4, 4)
62 exe "norm! " .. visual_mode
63
64 " Right click extends top left of visual area.
65 call MouseRightClick(2, 2)
66 call MouseRightRelease(2, 2)
67
Bram Moolenaarf19f8d12019-12-18 19:36:23 +010068 " Right click extends bottom right of visual area.
Bram Moolenaar6aa75232019-10-13 21:01:34 +020069 call MouseRightClick(6, 6)
70 call MouseRightRelease(6, 6)
71 norm! r1gv
72
73 " Right click shrinks top left of visual area.
74 call MouseRightClick(3, 3)
75 call MouseRightRelease(3, 3)
76
77 " Right click shrinks bottom right of visual area.
78 call MouseRightClick(5, 5)
79 call MouseRightRelease(5, 5)
80 norm! r2
81
82 if visual_mode ==# 'v'
83 call assert_equal(['-------',
84 \ '-111111',
85 \ '1122222',
86 \ '2222222',
87 \ '2222211',
88 \ '111111-',
89 \ '-------'], getline(1, '$'), msg)
90 elseif visual_mode ==# 'V'
91 call assert_equal(['-------',
92 \ '1111111',
93 \ '2222222',
94 \ '2222222',
95 \ '2222222',
96 \ '1111111',
97 \ '-------'], getline(1, '$'), msg)
98 else
99 call assert_equal(['-------',
100 \ '-11111-',
101 \ '-12221-',
102 \ '-12221-',
103 \ '-12221-',
104 \ '-11111-',
105 \ '-------'], getline(1, '$'), msg)
106 endif
107 endfor
108 endfor
109
110 let &mouse = save_mouse
111 let &term = save_term
112 let &ttymouse = save_ttymouse
113 call test_override('no_query_mouse', 0)
114 bwipe!
115endfunc
116
Bram Moolenaar1ee36d62019-05-01 23:13:56 +0200117" Test that <C-LeftMouse> jumps to help tag and <C-RightMouse> jumps back.
Bram Moolenaar297bec02020-07-14 22:11:04 +0200118" Also test for g<LeftMouse> and g<RightMouse>.
119func Test_xterm_mouse_tagjump()
Bram Moolenaar1ee36d62019-05-01 23:13:56 +0200120 let save_mouse = &mouse
121 let save_term = &term
122 let save_ttymouse = &ttymouse
123 set mouse=a term=xterm
Bram Moolenaarc255b782022-11-26 19:16:48 +0000124 call WaitForResponses()
Bram Moolenaar1ee36d62019-05-01 23:13:56 +0200125
Bram Moolenaar515545e2020-03-22 14:08:59 +0100126 for ttymouse_val in g:Ttymouse_values
Bram Moolenaar1ee36d62019-05-01 23:13:56 +0200127 let msg = 'ttymouse=' .. ttymouse_val
128 exe 'set ttymouse=' .. ttymouse_val
129 help
130 /usr_02.txt
131 norm! zt
Bram Moolenaar297bec02020-07-14 22:11:04 +0200132
133 " CTRL-left click to jump to a tag
Bram Moolenaar1ee36d62019-05-01 23:13:56 +0200134 let row = 1
135 let col = 1
136 call MouseCtrlLeftClick(row, col)
137 call MouseLeftRelease(row, col)
138 call assert_match('usr_02.txt$', bufname('%'), msg)
Bram Moolenaarb4367b72019-10-01 14:19:07 +0200139 call assert_equal('*usr_02.txt*', expand('<cWORD>'), msg)
Bram Moolenaar1ee36d62019-05-01 23:13:56 +0200140
Bram Moolenaar297bec02020-07-14 22:11:04 +0200141 " CTRL-right click to pop a tag
Bram Moolenaar1ee36d62019-05-01 23:13:56 +0200142 call MouseCtrlRightClick(row, col)
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200143 call MouseRightRelease(row, col)
Bram Moolenaar1ee36d62019-05-01 23:13:56 +0200144 call assert_match('help.txt$', bufname('%'), msg)
Bram Moolenaarb4367b72019-10-01 14:19:07 +0200145 call assert_equal('|usr_02.txt|', expand('<cWORD>'), msg)
Bram Moolenaar1ee36d62019-05-01 23:13:56 +0200146
Bram Moolenaar297bec02020-07-14 22:11:04 +0200147 " Jump to a tag
148 exe "normal \<C-]>"
149 call assert_match('usr_02.txt$', bufname('%'), msg)
150 call assert_equal('*usr_02.txt*', expand('<cWORD>'), msg)
151
152 " Use CTRL-right click in insert mode to pop the tag
153 new
154 let str = 'iHello' .. MouseCtrlRightClickCode(row, col)
155 \ .. MouseRightReleaseCode(row, col) .. "\<C-C>"
Bram Moolenaar2764d062020-07-18 12:59:19 +0200156 call assert_fails('call feedkeys(str, "Lx!")', 'E37:', msg)
Bram Moolenaar297bec02020-07-14 22:11:04 +0200157 close!
158
159 " CTRL-right click with a count
160 let str = "4" .. MouseCtrlRightClickCode(row, col)
161 \ .. MouseRightReleaseCode(row, col)
Bram Moolenaar2764d062020-07-18 12:59:19 +0200162 call assert_fails('call feedkeys(str, "Lx!")', 'E555:', msg)
Bram Moolenaar297bec02020-07-14 22:11:04 +0200163 call assert_match('help.txt$', bufname('%'), msg)
164 call assert_equal(1, line('.'), msg)
165
166 " g<LeftMouse> to jump to a tag
167 /usr_02.txt
168 norm! zt
169 call test_setmouse(row, col)
170 exe "normal g\<LeftMouse>"
171 call assert_match('usr_02.txt$', bufname('%'), msg)
172 call assert_equal('*usr_02.txt*', expand('<cWORD>'), msg)
173
174 " g<RightMouse> to pop to a tag
175 call test_setmouse(row, col)
176 exe "normal g\<RightMouse>"
177 call assert_match('help.txt$', bufname('%'), msg)
178 call assert_equal('|usr_02.txt|', expand('<cWORD>'), msg)
179
180 %bw!
Bram Moolenaar1ee36d62019-05-01 23:13:56 +0200181 endfor
182
183 let &mouse = save_mouse
184 let &term = save_term
185 let &ttymouse = save_ttymouse
186endfunc
187
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200188func Test_term_mouse_middle_click()
Bram Moolenaar52992fe2019-08-12 14:20:33 +0200189 CheckFeature clipboard_working
Bram Moolenaar564344a2019-04-28 13:00:12 +0200190
Bram Moolenaarc1b81602019-04-27 19:11:35 +0200191 new
192 let save_mouse = &mouse
193 let save_term = &term
194 let save_ttymouse = &ttymouse
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200195 call test_override('no_query_mouse', 1)
Bram Moolenaarc1b81602019-04-27 19:11:35 +0200196 let save_quotestar = @*
Bram Moolenaar297bec02020-07-14 22:11:04 +0200197 let save_quoteplus = @+
Bram Moolenaarc1b81602019-04-27 19:11:35 +0200198 set mouse=a term=xterm
Bram Moolenaarc255b782022-11-26 19:16:48 +0000199 call WaitForResponses()
Bram Moolenaarc1b81602019-04-27 19:11:35 +0200200
Bram Moolenaar515545e2020-03-22 14:08:59 +0100201 for ttymouse_val in g:Ttymouse_values + g:Ttymouse_dec
Bram Moolenaarc1b81602019-04-27 19:11:35 +0200202 let msg = 'ttymouse=' .. ttymouse_val
Bram Moolenaar1ee36d62019-05-01 23:13:56 +0200203 exe 'set ttymouse=' .. ttymouse_val
Bram Moolenaarc1b81602019-04-27 19:11:35 +0200204 call setline(1, ['123456789', '123456789'])
Bram Moolenaar297bec02020-07-14 22:11:04 +0200205 let @* = 'abc'
Bram Moolenaarc1b81602019-04-27 19:11:35 +0200206
207 " Middle-click in the middle of the line pastes text where clicked.
208 let row = 1
209 let col = 6
210 call MouseMiddleClick(row, col)
211 call MouseMiddleRelease(row, col)
212 call assert_equal(['12345abc6789', '123456789'], getline(1, '$'), msg)
213
214 " Middle-click beyond end of the line pastes text at the end of the line.
215 let col = 20
216 call MouseMiddleClick(row, col)
217 call MouseMiddleRelease(row, col)
218 call assert_equal(['12345abc6789abc', '123456789'], getline(1, '$'), msg)
219
220 " Middle-click beyond the last line pastes in the last line.
221 let row = 5
222 let col = 3
223 call MouseMiddleClick(row, col)
224 call MouseMiddleRelease(row, col)
225 call assert_equal(['12345abc6789abc', '12abc3456789'], getline(1, '$'), msg)
Bram Moolenaar297bec02020-07-14 22:11:04 +0200226
227 " Middle mouse click in operator pending mode beeps
228 call assert_beeps('exe "normal c\<MiddleMouse>"')
229
230 " Clicking middle mouse in visual mode, yanks the selection and pastes the
231 " clipboard contents
232 let save_clipboard = &clipboard
233 set clipboard=
234 let @" = ''
235 call cursor(1, 1)
236 call feedkeys("v3l" ..
237 \ MouseMiddleClickCode(2, 7) .. MouseMiddleReleaseCode(2, 7), 'Lx!')
238 call assert_equal(['12345abc6789abc', '12abc3abc456789'],
239 \ getline(1, '$'), msg)
Bram Moolenaar2764d062020-07-18 12:59:19 +0200240 call assert_equal('1234', @", msg)
Bram Moolenaar297bec02020-07-14 22:11:04 +0200241 let &clipboard = save_clipboard
242
243 " Clicking middle mouse in select mode, replaces the selected text with
244 " the clipboard contents
245 let @+ = 'xyz'
246 call cursor(1, 3)
247 exe "normal gh\<Right>\<Right>\<MiddleMouse>"
248 call assert_equal(['12xyzabc6789abc', '12abc3abc456789'],
249 \ getline(1, '$'), msg)
Bram Moolenaar2764d062020-07-18 12:59:19 +0200250
251 " Prefixing middle click with [ or ] fixes the indent after pasting.
252 %d
253 call setline(1, " one two")
254 call setreg('r', 'red blue', 'l')
255 call test_setmouse(1, 5)
256 exe "normal \"r[\<MiddleMouse>"
257 call assert_equal(' red blue', getline(1), msg)
258 call test_setmouse(2, 5)
259 exe "normal \"r]\<MiddleMouse>"
260 call assert_equal(' red blue', getline(3), msg)
261 %d
Bram Moolenaarc1b81602019-04-27 19:11:35 +0200262 endfor
263
264 let &mouse = save_mouse
265 let &term = save_term
266 let &ttymouse = save_ttymouse
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200267 call test_override('no_query_mouse', 0)
Bram Moolenaarc1b81602019-04-27 19:11:35 +0200268 let @* = save_quotestar
Bram Moolenaar297bec02020-07-14 22:11:04 +0200269 let @+ = save_quotestar
Bram Moolenaarc1b81602019-04-27 19:11:35 +0200270 bwipe!
271endfunc
272
Bram Moolenaar297bec02020-07-14 22:11:04 +0200273" If clipboard is not working, then clicking the middle mouse button in visual
274" mode, will copy and paste the selected text.
275func Test_term_mouse_middle_click_no_clipboard()
276 if has('clipboard_working')
277 throw 'Skipped: clipboard support works'
Bram Moolenaar297bec02020-07-14 22:11:04 +0200278 endif
279 new
280 let save_mouse = &mouse
281 let save_term = &term
282 let save_ttymouse = &ttymouse
283 call test_override('no_query_mouse', 1)
284 set mouse=a term=xterm
Bram Moolenaarc255b782022-11-26 19:16:48 +0000285 call WaitForResponses()
Bram Moolenaar297bec02020-07-14 22:11:04 +0200286
287 for ttymouse_val in g:Ttymouse_values + g:Ttymouse_dec
288 let msg = 'ttymouse=' .. ttymouse_val
289 exe 'set ttymouse=' .. ttymouse_val
290 call setline(1, ['123456789', '123456789'])
291
292 " Clicking middle mouse in visual mode, yanks the selection and pastes it
293 call cursor(1, 1)
294 call feedkeys("v3l" ..
295 \ MouseMiddleClickCode(2, 7) .. MouseMiddleReleaseCode(2, 7), 'Lx!')
296 call assert_equal(['123456789', '1234561234789'],
297 \ getline(1, '$'), msg)
298 endfor
299
300 call test_override('no_query_mouse', 0)
301 let &ttymouse = save_ttymouse
302 let &term = save_term
303 let &mouse = save_mouse
304 close!
305endfunc
306
307func Test_term_mouse_middle_click_insert_mode()
308 CheckFeature clipboard_working
309
310 new
311 let save_mouse = &mouse
312 let save_term = &term
313 let save_ttymouse = &ttymouse
314 call test_override('no_query_mouse', 1)
315 set mouse=a term=xterm
Bram Moolenaarc255b782022-11-26 19:16:48 +0000316 call WaitForResponses()
Bram Moolenaar297bec02020-07-14 22:11:04 +0200317
318 for ttymouse_val in g:Ttymouse_values + g:Ttymouse_dec
319 let msg = 'ttymouse=' .. ttymouse_val
320 exe 'set ttymouse=' .. ttymouse_val
321 call setline(1, ['123456789', '123456789'])
322 let @* = 'abc'
323
Dominique Pelle923dce22021-11-21 11:36:04 +0000324 " Middle-click in insert mode doesn't move the cursor but inserts the
325 " contents of a register
Bram Moolenaar297bec02020-07-14 22:11:04 +0200326 call cursor(1, 4)
327 call feedkeys('i' ..
328 \ MouseMiddleClickCode(2, 7) .. MouseMiddleReleaseCode(2, 7) ..
329 \ "\<C-C>", 'Lx!')
330 call assert_equal(['123abc456789', '123456789'],
331 \ getline(1, '$'), msg)
Bram Moolenaar2764d062020-07-18 12:59:19 +0200332 call assert_equal([1, 6], [line('.'), col('.')], msg)
Bram Moolenaar297bec02020-07-14 22:11:04 +0200333
334 " Middle-click in replace mode
335 call cursor(1, 1)
336 call feedkeys('$R' ..
337 \ MouseMiddleClickCode(2, 7) .. MouseMiddleReleaseCode(2, 7) ..
338 \ "\<C-C>", 'Lx!')
339 call assert_equal(['123abc45678abc', '123456789'],
340 \ getline(1, '$'), msg)
Bram Moolenaar2764d062020-07-18 12:59:19 +0200341 call assert_equal([1, 14], [line('.'), col('.')], msg)
Bram Moolenaar297bec02020-07-14 22:11:04 +0200342 endfor
343
344 let &mouse = save_mouse
345 let &term = save_term
346 let &ttymouse = save_ttymouse
347 call test_override('no_query_mouse', 0)
348 close!
349endfunc
350
351" Test for switching window using mouse in insert mode
352func Test_term_mouse_switch_win_insert_mode()
353 5new
354 let save_mouse = &mouse
355 let save_term = &term
356 let save_ttymouse = &ttymouse
357 call test_override('no_query_mouse', 1)
358 set mouse=a term=xterm ttymouse=xterm2
Bram Moolenaarc255b782022-11-26 19:16:48 +0000359 call WaitForResponses()
Bram Moolenaar297bec02020-07-14 22:11:04 +0200360
361 call feedkeys('ivim' ..
362 \ MouseLeftClickCode(8, 6) .. MouseLeftReleaseCode(8, 6) ..
363 \ "\<C-C>", 'Lx!')
364 call assert_equal(2, winnr())
365 wincmd w
366 call assert_equal('n', mode())
367 call assert_equal(['vim'], getline(1, '$'))
368
369 let &mouse = save_mouse
370 let &term = save_term
371 let &ttymouse = save_ttymouse
372 call test_override('no_query_mouse', 0)
373 close!
374endfunc
375
Dominique Pelle923dce22021-11-21 11:36:04 +0000376" Test for using the mouse to increase the height of the cmdline window
Bram Moolenaar297bec02020-07-14 22:11:04 +0200377func Test_mouse_cmdwin_resize()
378 let save_mouse = &mouse
379 let save_term = &term
380 let save_ttymouse = &ttymouse
381 call test_override('no_query_mouse', 1)
382 set mouse=a term=xterm ttymouse=xterm2
Bram Moolenaarc255b782022-11-26 19:16:48 +0000383 call WaitForResponses()
384
Bram Moolenaar297bec02020-07-14 22:11:04 +0200385 5new
386 redraw!
387
388 let h = 0
389 let row = &lines - &cmdwinheight - 2
390 call feedkeys("q:" ..
391 \ MouseLeftClickCode(row, 1) ..
392 \ MouseLeftDragCode(row - 1, 1) ..
393 \ MouseLeftReleaseCode(row - 2, 1) ..
394 \ "alet h = \<C-R>=winheight(0)\<CR>\<CR>", 'Lx!')
395 call assert_equal(&cmdwinheight + 2, h)
396
397 let &mouse = save_mouse
398 let &term = save_term
399 let &ttymouse = save_ttymouse
400 call test_override('no_query_mouse', 0)
401 close!
402endfunc
403
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200404" TODO: for unclear reasons this test fails if it comes after
405" Test_xterm_mouse_ctrl_click()
406func Test_1xterm_mouse_wheel()
Bram Moolenaar049736f2019-04-07 21:55:07 +0200407 new
408 let save_mouse = &mouse
409 let save_term = &term
Bram Moolenaard58d4f92020-07-01 15:49:29 +0200410 let save_wrap = &wrap
Bram Moolenaar049736f2019-04-07 21:55:07 +0200411 let save_ttymouse = &ttymouse
Bram Moolenaard58d4f92020-07-01 15:49:29 +0200412 set mouse=a term=xterm nowrap
Bram Moolenaarc255b782022-11-26 19:16:48 +0000413 call WaitForResponses()
414
Bram Moolenaard58d4f92020-07-01 15:49:29 +0200415 call setline(1, range(100000000000000, 100000000000100))
Bram Moolenaar049736f2019-04-07 21:55:07 +0200416
Bram Moolenaar515545e2020-03-22 14:08:59 +0100417 for ttymouse_val in g:Ttymouse_values
Bram Moolenaar49452192019-04-17 16:27:02 +0200418 let msg = 'ttymouse=' .. ttymouse_val
Bram Moolenaar1ee36d62019-05-01 23:13:56 +0200419 exe 'set ttymouse=' .. ttymouse_val
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200420 go
Bram Moolenaar49452192019-04-17 16:27:02 +0200421 call assert_equal(1, line('w0'), msg)
422 call assert_equal([0, 1, 1, 0], getpos('.'), msg)
Bram Moolenaar049736f2019-04-07 21:55:07 +0200423
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200424 call MouseWheelDown(1, 1)
Bram Moolenaar49452192019-04-17 16:27:02 +0200425 call assert_equal(4, line('w0'), msg)
426 call assert_equal([0, 4, 1, 0], getpos('.'), msg)
Bram Moolenaar049736f2019-04-07 21:55:07 +0200427
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200428 call MouseWheelDown(1, 1)
Bram Moolenaar49452192019-04-17 16:27:02 +0200429 call assert_equal(7, line('w0'), msg)
430 call assert_equal([0, 7, 1, 0], getpos('.'), msg)
Bram Moolenaar049736f2019-04-07 21:55:07 +0200431
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200432 call MouseWheelUp(1, 1)
Bram Moolenaar49452192019-04-17 16:27:02 +0200433 call assert_equal(4, line('w0'), msg)
434 call assert_equal([0, 7, 1, 0], getpos('.'), msg)
Bram Moolenaar049736f2019-04-07 21:55:07 +0200435
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200436 call MouseWheelUp(1, 1)
Bram Moolenaar49452192019-04-17 16:27:02 +0200437 call assert_equal(1, line('w0'), msg)
438 call assert_equal([0, 7, 1, 0], getpos('.'), msg)
Bram Moolenaard58d4f92020-07-01 15:49:29 +0200439
440 if has('gui')
441 " Horizontal wheel scrolling currently only works when vim is
442 " compiled with gui enabled.
443 call MouseWheelRight(1, 1)
444 call assert_equal(7, 1 + virtcol(".") - wincol(), msg)
445 call assert_equal([0, 7, 7, 0], getpos('.'), msg)
446
447 call MouseWheelRight(1, 1)
448 call assert_equal(13, 1 + virtcol(".") - wincol(), msg)
449 call assert_equal([0, 7, 13, 0], getpos('.'), msg)
450
451 call MouseWheelLeft(1, 1)
452 call assert_equal(7, 1 + virtcol(".") - wincol(), msg)
453 call assert_equal([0, 7, 13, 0], getpos('.'), msg)
454
455 call MouseWheelLeft(1, 1)
456 call assert_equal(1, 1 + virtcol(".") - wincol(), msg)
457 call assert_equal([0, 7, 13, 0], getpos('.'), msg)
458 endif
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200459 endfor
Bram Moolenaar049736f2019-04-07 21:55:07 +0200460
461 let &mouse = save_mouse
462 let &term = save_term
Bram Moolenaard58d4f92020-07-01 15:49:29 +0200463 let &wrap = save_wrap
Bram Moolenaar049736f2019-04-07 21:55:07 +0200464 let &ttymouse = save_ttymouse
465 bwipe!
466endfunc
Bram Moolenaar3fb01a52019-04-09 21:52:02 +0200467
Bram Moolenaarb4367b72019-10-01 14:19:07 +0200468" Test that dragging beyond the window (at the bottom and at the top)
Bram Moolenaarf19f8d12019-12-18 19:36:23 +0100469" scrolls window content by the number of lines beyond the window.
Bram Moolenaarb4367b72019-10-01 14:19:07 +0200470func Test_term_mouse_drag_beyond_window()
471 let save_mouse = &mouse
472 let save_term = &term
473 let save_ttymouse = &ttymouse
474 call test_override('no_query_mouse', 1)
475 set mouse=a term=xterm
Bram Moolenaarc255b782022-11-26 19:16:48 +0000476 call WaitForResponses()
477
Bram Moolenaarb4367b72019-10-01 14:19:07 +0200478 let col = 1
479 call setline(1, range(1, 100))
480
481 " Split into 3 windows, and go into the middle window
482 " so we test dragging mouse below and above the window.
483 2split
484 wincmd j
485 2split
486
Bram Moolenaar515545e2020-03-22 14:08:59 +0100487 for ttymouse_val in g:Ttymouse_values + g:Ttymouse_dec
Bram Moolenaarb4367b72019-10-01 14:19:07 +0200488 let msg = 'ttymouse=' .. ttymouse_val
489 exe 'set ttymouse=' .. ttymouse_val
490
491 " Line #10 at the top.
492 norm! 10zt
493 redraw
494 call assert_equal(10, winsaveview().topline, msg)
495 call assert_equal(2, winheight(0), msg)
496
497 let row = 4
498 call MouseLeftClick(row, col)
499 call assert_equal(10, winsaveview().topline, msg)
500
501 " Drag downwards. We're still in the window so topline should
502 " not change yet.
503 let row += 1
504 call MouseLeftDrag(row, col)
505 call assert_equal(10, winsaveview().topline, msg)
506
507 " We now leave the window at the bottom, so the window content should
508 " scroll by 1 line, then 2 lines (etc) as we drag further away.
509 let row += 1
510 call MouseLeftDrag(row, col)
511 call assert_equal(11, winsaveview().topline, msg)
512
513 let row += 1
514 call MouseLeftDrag(row, col)
515 call assert_equal(13, winsaveview().topline, msg)
516
517 " Now drag upwards.
518 let row -= 1
519 call MouseLeftDrag(row, col)
520 call assert_equal(14, winsaveview().topline, msg)
521
522 " We're now back in the window so the topline should not change.
523 let row -= 1
524 call MouseLeftDrag(row, col)
525 call assert_equal(14, winsaveview().topline, msg)
526
527 let row -= 1
528 call MouseLeftDrag(row, col)
529 call assert_equal(14, winsaveview().topline, msg)
530
531 " We now leave the window at the top so the window content should
532 " scroll by 1 line, then 2, then 3 (etc) in the opposite direction.
533 let row -= 1
534 call MouseLeftDrag(row, col)
535 call assert_equal(13, winsaveview().topline, msg)
536
537 let row -= 1
538 call MouseLeftDrag(row, col)
539 call assert_equal(11, winsaveview().topline, msg)
540
541 let row -= 1
542 call MouseLeftDrag(row, col)
543 call assert_equal(8, winsaveview().topline, msg)
544
545 call MouseLeftRelease(row, col)
546 call assert_equal(8, winsaveview().topline, msg)
547 call assert_equal(2, winheight(0), msg)
548 endfor
549
550 let &mouse = save_mouse
551 let &term = save_term
552 let &ttymouse = save_ttymouse
553 call test_override('no_query_mouse', 0)
554 bwipe!
555endfunc
556
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200557func Test_term_mouse_drag_window_separator()
Bram Moolenaar3fb01a52019-04-09 21:52:02 +0200558 let save_mouse = &mouse
559 let save_term = &term
560 let save_ttymouse = &ttymouse
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200561 call test_override('no_query_mouse', 1)
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200562 set mouse=a term=xterm
Bram Moolenaarc255b782022-11-26 19:16:48 +0000563 call WaitForResponses()
Bram Moolenaar3fb01a52019-04-09 21:52:02 +0200564
Bram Moolenaar515545e2020-03-22 14:08:59 +0100565 for ttymouse_val in g:Ttymouse_values + g:Ttymouse_dec
Bram Moolenaar49452192019-04-17 16:27:02 +0200566 let msg = 'ttymouse=' .. ttymouse_val
Bram Moolenaar1ee36d62019-05-01 23:13:56 +0200567 exe 'set ttymouse=' .. ttymouse_val
Bram Moolenaar3fb01a52019-04-09 21:52:02 +0200568
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200569 " Split horizontally and test dragging the horizontal window separator.
570 split
571 let rowseparator = winheight(0) + 1
572 let row = rowseparator
573 let col = 1
Bram Moolenaarc8b3dda2019-04-12 21:42:52 +0200574
Bram Moolenaar2b00b9b2019-04-17 17:08:27 +0200575 " When 'ttymouse' is 'xterm2', row/col bigger than 223 are not supported.
576 if ttymouse_val !=# 'xterm2' || row <= 223
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200577 call MouseLeftClick(row, col)
578 let row -= 1
579 call MouseLeftDrag(row, col)
zeertzjq8e0ccb62022-11-01 18:35:27 +0000580 call assert_equal(rowseparator - 1, winheight(0) + 1, msg)
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200581 let row += 1
582 call MouseLeftDrag(row, col)
Bram Moolenaar49452192019-04-17 16:27:02 +0200583 call assert_equal(rowseparator, winheight(0) + 1, msg)
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200584 call MouseLeftRelease(row, col)
Bram Moolenaar49452192019-04-17 16:27:02 +0200585 call assert_equal(rowseparator, winheight(0) + 1, msg)
Bram Moolenaarc8b3dda2019-04-12 21:42:52 +0200586 endif
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200587 bwipe!
Bram Moolenaar3fb01a52019-04-09 21:52:02 +0200588
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200589 " Split vertically and test dragging the vertical window separator.
590 vsplit
591 let colseparator = winwidth(0) + 1
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200592 let row = 1
593 let col = colseparator
Bram Moolenaar3fb01a52019-04-09 21:52:02 +0200594
Bram Moolenaar2b00b9b2019-04-17 17:08:27 +0200595 " When 'ttymouse' is 'xterm2', row/col bigger than 223 are not supported.
596 if ttymouse_val !=# 'xterm2' || col <= 223
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200597 call MouseLeftClick(row, col)
598 let col -= 1
599 call MouseLeftDrag(row, col)
Bram Moolenaar49452192019-04-17 16:27:02 +0200600 call assert_equal(colseparator - 1, winwidth(0) + 1, msg)
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200601 let col += 1
602 call MouseLeftDrag(row, col)
Bram Moolenaar49452192019-04-17 16:27:02 +0200603 call assert_equal(colseparator, winwidth(0) + 1, msg)
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200604 call MouseLeftRelease(row, col)
Bram Moolenaar49452192019-04-17 16:27:02 +0200605 call assert_equal(colseparator, winwidth(0) + 1, msg)
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200606 endif
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200607 bwipe!
608 endfor
Bram Moolenaar3fb01a52019-04-09 21:52:02 +0200609
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200610 let &mouse = save_mouse
611 let &term = save_term
612 let &ttymouse = save_ttymouse
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200613 call test_override('no_query_mouse', 0)
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200614endfunc
Bram Moolenaar3fb01a52019-04-09 21:52:02 +0200615
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200616func Test_term_mouse_drag_statusline()
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200617 let save_mouse = &mouse
618 let save_term = &term
619 let save_ttymouse = &ttymouse
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200620 call test_override('no_query_mouse', 1)
Bram Moolenaarca57ab52019-04-13 14:53:16 +0200621 let save_laststatus = &laststatus
622 set mouse=a term=xterm laststatus=2
Bram Moolenaarc255b782022-11-26 19:16:48 +0000623 call WaitForResponses()
Bram Moolenaar3fb01a52019-04-09 21:52:02 +0200624
Bram Moolenaar515545e2020-03-22 14:08:59 +0100625 for ttymouse_val in g:Ttymouse_values + g:Ttymouse_dec
Bram Moolenaar49452192019-04-17 16:27:02 +0200626 let msg = 'ttymouse=' .. ttymouse_val
Bram Moolenaar1ee36d62019-05-01 23:13:56 +0200627 exe 'set ttymouse=' .. ttymouse_val
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200628
Bram Moolenaar49452192019-04-17 16:27:02 +0200629 call assert_equal(1, &cmdheight, msg)
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200630 let rowstatusline = winheight(0) + 1
631 let row = rowstatusline
632 let col = 1
Bram Moolenaarc8b3dda2019-04-12 21:42:52 +0200633
Bram Moolenaar2b00b9b2019-04-17 17:08:27 +0200634 if ttymouse_val ==# 'xterm2' && row > 223
635 " When 'ttymouse' is 'xterm2', row/col bigger than 223 are not supported.
Bram Moolenaarc8b3dda2019-04-12 21:42:52 +0200636 continue
637 endif
638
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200639 call MouseLeftClick(row, col)
640 let row -= 1
641 call MouseLeftDrag(row, col)
Bram Moolenaar49452192019-04-17 16:27:02 +0200642 call assert_equal(2, &cmdheight, msg)
643 call assert_equal(rowstatusline - 1, winheight(0) + 1, msg)
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200644 let row += 1
645 call MouseLeftDrag(row, col)
Bram Moolenaar49452192019-04-17 16:27:02 +0200646 call assert_equal(1, &cmdheight, msg)
647 call assert_equal(rowstatusline, winheight(0) + 1, msg)
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200648 call MouseLeftRelease(row, col)
Bram Moolenaar49452192019-04-17 16:27:02 +0200649 call assert_equal(1, &cmdheight, msg)
650 call assert_equal(rowstatusline, winheight(0) + 1, msg)
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200651 endfor
652
Bram Moolenaar3fb01a52019-04-09 21:52:02 +0200653 let &mouse = save_mouse
654 let &term = save_term
655 let &ttymouse = save_ttymouse
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200656 call test_override('no_query_mouse', 0)
Bram Moolenaarca57ab52019-04-13 14:53:16 +0200657 let &laststatus = save_laststatus
658endfunc
659
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200660func Test_term_mouse_click_tab()
Bram Moolenaarca57ab52019-04-13 14:53:16 +0200661 let save_mouse = &mouse
662 let save_term = &term
663 let save_ttymouse = &ttymouse
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200664 call test_override('no_query_mouse', 1)
Bram Moolenaarca57ab52019-04-13 14:53:16 +0200665 set mouse=a term=xterm
Bram Moolenaarc255b782022-11-26 19:16:48 +0000666 call WaitForResponses()
667
Bram Moolenaarca57ab52019-04-13 14:53:16 +0200668 let row = 1
669
Bram Moolenaar515545e2020-03-22 14:08:59 +0100670 for ttymouse_val in g:Ttymouse_values + g:Ttymouse_dec + g:Ttymouse_netterm
Bram Moolenaar49452192019-04-17 16:27:02 +0200671 let msg = 'ttymouse=' .. ttymouse_val
Bram Moolenaar1ee36d62019-05-01 23:13:56 +0200672 exe 'set ttymouse=' .. ttymouse_val
Bram Moolenaarca57ab52019-04-13 14:53:16 +0200673 e Xfoo
674 tabnew Xbar
675
676 let a = split(execute(':tabs'), "\n")
677 call assert_equal(['Tab page 1',
678 \ ' Xfoo',
679 \ 'Tab page 2',
Bram Moolenaar49452192019-04-17 16:27:02 +0200680 \ '> Xbar'], a, msg)
Bram Moolenaarca57ab52019-04-13 14:53:16 +0200681
682 " Test clicking on tab names in the tabline at the top.
683 let col = 2
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200684 redraw
Bram Moolenaarca57ab52019-04-13 14:53:16 +0200685 call MouseLeftClick(row, col)
686 call MouseLeftRelease(row, col)
687 let a = split(execute(':tabs'), "\n")
688 call assert_equal(['Tab page 1',
689 \ '> Xfoo',
690 \ 'Tab page 2',
Bram Moolenaar49452192019-04-17 16:27:02 +0200691 \ ' Xbar'], a, msg)
Bram Moolenaarca57ab52019-04-13 14:53:16 +0200692
693 let col = 9
694 call MouseLeftClick(row, col)
695 call MouseLeftRelease(row, col)
696 let a = split(execute(':tabs'), "\n")
697 call assert_equal(['Tab page 1',
698 \ ' Xfoo',
699 \ 'Tab page 2',
Bram Moolenaar49452192019-04-17 16:27:02 +0200700 \ '> Xbar'], a, msg)
Bram Moolenaarca57ab52019-04-13 14:53:16 +0200701
702 %bwipe!
703 endfor
704
705 let &mouse = save_mouse
706 let &term = save_term
707 let &ttymouse = save_ttymouse
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200708 call test_override('no_query_mouse', 0)
Bram Moolenaar3fb01a52019-04-09 21:52:02 +0200709endfunc
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200710
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200711func Test_term_mouse_click_X_to_close_tab()
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200712 let save_mouse = &mouse
713 let save_term = &term
714 let save_ttymouse = &ttymouse
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200715 call test_override('no_query_mouse', 1)
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200716 set mouse=a term=xterm
Bram Moolenaarc255b782022-11-26 19:16:48 +0000717 call WaitForResponses()
718
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200719 let row = 1
720 let col = &columns
721
Bram Moolenaar515545e2020-03-22 14:08:59 +0100722 for ttymouse_val in g:Ttymouse_values + g:Ttymouse_dec + g:Ttymouse_netterm
Bram Moolenaar2b00b9b2019-04-17 17:08:27 +0200723 if ttymouse_val ==# 'xterm2' && col > 223
724 " When 'ttymouse' is 'xterm2', row/col bigger than 223 are not supported.
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200725 continue
726 endif
Bram Moolenaar49452192019-04-17 16:27:02 +0200727 let msg = 'ttymouse=' .. ttymouse_val
Bram Moolenaar1ee36d62019-05-01 23:13:56 +0200728 exe 'set ttymouse=' .. ttymouse_val
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200729 e Xtab1
730 tabnew Xtab2
731 tabnew Xtab3
732 tabn 2
733
734 let a = split(execute(':tabs'), "\n")
735 call assert_equal(['Tab page 1',
736 \ ' Xtab1',
737 \ 'Tab page 2',
738 \ '> Xtab2',
739 \ 'Tab page 3',
Bram Moolenaar49452192019-04-17 16:27:02 +0200740 \ ' Xtab3'], a, msg)
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200741
742 " Click on "X" in tabline to close current tab i.e. Xtab2.
743 redraw
744 call MouseLeftClick(row, col)
745 call MouseLeftRelease(row, col)
746 let a = split(execute(':tabs'), "\n")
747 call assert_equal(['Tab page 1',
748 \ ' Xtab1',
749 \ 'Tab page 2',
Bram Moolenaar49452192019-04-17 16:27:02 +0200750 \ '> Xtab3'], a, msg)
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200751
752 %bwipe!
753 endfor
754
755 let &mouse = save_mouse
756 let &term = save_term
757 let &ttymouse = save_ttymouse
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200758 call test_override('no_query_mouse', 0)
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200759endfunc
Bram Moolenaare3e38282019-04-15 20:55:31 +0200760
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200761func Test_term_mouse_drag_to_move_tab()
Bram Moolenaare3e38282019-04-15 20:55:31 +0200762 let save_mouse = &mouse
763 let save_term = &term
764 let save_ttymouse = &ttymouse
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200765 call test_override('no_query_mouse', 1)
Bram Moolenaare3e38282019-04-15 20:55:31 +0200766 " Set 'mousetime' to 1 to avoid recognizing a double-click in the loop
767 set mouse=a term=xterm mousetime=1
Bram Moolenaarc255b782022-11-26 19:16:48 +0000768 call WaitForResponses()
769
Bram Moolenaare3e38282019-04-15 20:55:31 +0200770 let row = 1
771
Bram Moolenaar515545e2020-03-22 14:08:59 +0100772 for ttymouse_val in g:Ttymouse_values + g:Ttymouse_dec
Bram Moolenaar49452192019-04-17 16:27:02 +0200773 let msg = 'ttymouse=' .. ttymouse_val
Bram Moolenaar1ee36d62019-05-01 23:13:56 +0200774 exe 'set ttymouse=' .. ttymouse_val
Bram Moolenaare3e38282019-04-15 20:55:31 +0200775 e Xtab1
776 tabnew Xtab2
777
778 let a = split(execute(':tabs'), "\n")
779 call assert_equal(['Tab page 1',
780 \ ' Xtab1',
781 \ 'Tab page 2',
Bram Moolenaar49452192019-04-17 16:27:02 +0200782 \ '> Xtab2'], a, msg)
Bram Moolenaare3e38282019-04-15 20:55:31 +0200783 redraw
784
785 " Click in tab2 and drag it to tab1.
786 " Check getcharmod() to verify that click is not
787 " interpreted as a spurious double-click.
788 call MouseLeftClick(row, 10)
Bram Moolenaar49452192019-04-17 16:27:02 +0200789 call assert_equal(0, getcharmod(), msg)
Bram Moolenaare3e38282019-04-15 20:55:31 +0200790 for col in [9, 8, 7, 6]
791 call MouseLeftDrag(row, col)
792 endfor
793 call MouseLeftRelease(row, col)
794 let a = split(execute(':tabs'), "\n")
795 call assert_equal(['Tab page 1',
796 \ '> Xtab2',
797 \ 'Tab page 2',
Bram Moolenaar49452192019-04-17 16:27:02 +0200798 \ ' Xtab1'], a, msg)
Bram Moolenaare3e38282019-04-15 20:55:31 +0200799
zeertzjq8e0ccb62022-11-01 18:35:27 +0000800 " Switch to tab1
801 tabnext
802 let a = split(execute(':tabs'), "\n")
803 call assert_equal(['Tab page 1',
804 \ ' Xtab2',
805 \ 'Tab page 2',
806 \ '> Xtab1'], a, msg)
807
808 " Click in tab2 and drag it to tab1.
809 " This time it is non-current tab.
810 call MouseLeftClick(row, 6)
811 call assert_equal(0, getcharmod(), msg)
812 for col in [7, 8, 9, 10]
813 call MouseLeftDrag(row, col)
814 endfor
815 call MouseLeftRelease(row, col)
816 let a = split(execute(':tabs'), "\n")
817 call assert_equal(['Tab page 1',
818 \ ' Xtab1',
819 \ 'Tab page 2',
820 \ '> Xtab2'], a, msg)
821
Bram Moolenaarf36a2c72019-11-16 18:57:16 +0100822 " Click elsewhere so that click in next iteration is not
823 " interpreted as unwanted double-click.
824 call MouseLeftClick(row, 11)
825 call MouseLeftRelease(row, 11)
826
Bram Moolenaare3e38282019-04-15 20:55:31 +0200827 %bwipe!
828 endfor
829
830 let &mouse = save_mouse
831 let &term = save_term
832 let &ttymouse = save_ttymouse
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200833 call test_override('no_query_mouse', 0)
Bram Moolenaare3e38282019-04-15 20:55:31 +0200834 set mousetime&
835endfunc
836
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200837func Test_term_mouse_double_click_to_create_tab()
Bram Moolenaare3e38282019-04-15 20:55:31 +0200838 let save_mouse = &mouse
839 let save_term = &term
840 let save_ttymouse = &ttymouse
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200841 call test_override('no_query_mouse', 1)
Bram Moolenaare3e38282019-04-15 20:55:31 +0200842 " Set 'mousetime' to a small value, so that double-click works but we don't
843 " have to wait long to avoid a triple-click.
Bram Moolenaarf36a2c72019-11-16 18:57:16 +0100844 set mouse=a term=xterm mousetime=200
Bram Moolenaarc255b782022-11-26 19:16:48 +0000845 call WaitForResponses()
846
Bram Moolenaare3e38282019-04-15 20:55:31 +0200847 let row = 1
848 let col = 10
849
Bram Moolenaar515545e2020-03-22 14:08:59 +0100850 for ttymouse_val in g:Ttymouse_values + g:Ttymouse_dec
Bram Moolenaar49452192019-04-17 16:27:02 +0200851 let msg = 'ttymouse=' .. ttymouse_val
Bram Moolenaar1ee36d62019-05-01 23:13:56 +0200852 exe 'set ttymouse=' .. ttymouse_val
Bram Moolenaare3e38282019-04-15 20:55:31 +0200853 e Xtab1
854 tabnew Xtab2
855
856 let a = split(execute(':tabs'), "\n")
857 call assert_equal(['Tab page 1',
858 \ ' Xtab1',
859 \ 'Tab page 2',
Bram Moolenaar49452192019-04-17 16:27:02 +0200860 \ '> Xtab2'], a, msg)
Bram Moolenaare3e38282019-04-15 20:55:31 +0200861
862 redraw
863 call MouseLeftClick(row, col)
864 " Check getcharmod() to verify that first click is not
865 " interpreted as a spurious double-click.
Bram Moolenaar49452192019-04-17 16:27:02 +0200866 call assert_equal(0, getcharmod(), msg)
Bram Moolenaare3e38282019-04-15 20:55:31 +0200867 call MouseLeftRelease(row, col)
868 call MouseLeftClick(row, col)
Bram Moolenaar49452192019-04-17 16:27:02 +0200869 call assert_equal(32, getcharmod(), msg) " double-click
Bram Moolenaare3e38282019-04-15 20:55:31 +0200870 call MouseLeftRelease(row, col)
871 let a = split(execute(':tabs'), "\n")
872 call assert_equal(['Tab page 1',
873 \ ' Xtab1',
874 \ 'Tab page 2',
875 \ '> [No Name]',
876 \ 'Tab page 3',
Bram Moolenaar49452192019-04-17 16:27:02 +0200877 \ ' Xtab2'], a, msg)
Bram Moolenaare3e38282019-04-15 20:55:31 +0200878
Bram Moolenaarf36a2c72019-11-16 18:57:16 +0100879 " Click elsewhere so that click in next iteration is not
880 " interpreted as unwanted double click.
881 call MouseLeftClick(row, col + 1)
882 call MouseLeftRelease(row, col + 1)
883
Bram Moolenaare3e38282019-04-15 20:55:31 +0200884 %bwipe!
885 endfor
886
887 let &mouse = save_mouse
888 let &term = save_term
889 let &ttymouse = save_ttymouse
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200890 call test_override('no_query_mouse', 0)
Bram Moolenaare3e38282019-04-15 20:55:31 +0200891 set mousetime&
892endfunc
Bram Moolenaar696d6372019-04-17 16:33:46 +0200893
Bram Moolenaarf36a2c72019-11-16 18:57:16 +0100894" Test double/triple/quadruple click in normal mode to visually select.
895func Test_term_mouse_multiple_clicks_to_visually_select()
896 let save_mouse = &mouse
897 let save_term = &term
898 let save_ttymouse = &ttymouse
899 call test_override('no_query_mouse', 1)
Bram Moolenaar2a5c61a2020-12-30 14:59:23 +0100900
901 " 'mousetime' must be sufficiently large, or else the test is flaky when
902 " using a ssh connection with X forwarding; i.e. ssh -X (issue #7563).
903 set mouse=a term=xterm mousetime=600
Bram Moolenaarc255b782022-11-26 19:16:48 +0000904 call WaitForResponses()
905
Bram Moolenaarf36a2c72019-11-16 18:57:16 +0100906 new
907
Bram Moolenaar515545e2020-03-22 14:08:59 +0100908 for ttymouse_val in g:Ttymouse_values + g:Ttymouse_dec
Bram Moolenaarf36a2c72019-11-16 18:57:16 +0100909 let msg = 'ttymouse=' .. ttymouse_val
910 exe 'set ttymouse=' .. ttymouse_val
911 call setline(1, ['foo [foo bar] foo', 'foo'])
912
913 " Double-click on word should visually select the word.
914 call MouseLeftClick(1, 2)
915 call assert_equal(0, getcharmod(), msg)
916 call MouseLeftRelease(1, 2)
917 call MouseLeftClick(1, 2)
918 call assert_equal(32, getcharmod(), msg) " double-click
919 call MouseLeftRelease(1, 2)
920 call assert_equal('v', mode(), msg)
921 norm! r1
922 call assert_equal(['111 [foo bar] foo', 'foo'], getline(1, '$'), msg)
923
924 " Double-click on opening square bracket should visually
925 " select the whole [foo bar].
926 call MouseLeftClick(1, 5)
927 call assert_equal(0, getcharmod(), msg)
928 call MouseLeftRelease(1, 5)
929 call MouseLeftClick(1, 5)
930 call assert_equal(32, getcharmod(), msg) " double-click
931 call MouseLeftRelease(1, 5)
932 call assert_equal('v', mode(), msg)
933 norm! r2
934 call assert_equal(['111 222222222 foo', 'foo'], getline(1, '$'), msg)
935
936 " Triple-click should visually select the whole line.
937 call MouseLeftClick(1, 3)
938 call assert_equal(0, getcharmod(), msg)
939 call MouseLeftRelease(1, 3)
940 call MouseLeftClick(1, 3)
941 call assert_equal(32, getcharmod(), msg) " double-click
942 call MouseLeftRelease(1, 3)
943 call MouseLeftClick(1, 3)
944 call assert_equal(64, getcharmod(), msg) " triple-click
945 call MouseLeftRelease(1, 3)
946 call assert_equal('V', mode(), msg)
947 norm! r3
948 call assert_equal(['33333333333333333', 'foo'], getline(1, '$'), msg)
949
950 " Quadruple-click should start visual block select.
951 call MouseLeftClick(1, 2)
952 call assert_equal(0, getcharmod(), msg)
953 call MouseLeftRelease(1, 2)
954 call MouseLeftClick(1, 2)
955 call assert_equal(32, getcharmod(), msg) " double-click
956 call MouseLeftRelease(1, 2)
957 call MouseLeftClick(1, 2)
958 call assert_equal(64, getcharmod(), msg) " triple-click
959 call MouseLeftRelease(1, 2)
960 call MouseLeftClick(1, 2)
961 call assert_equal(96, getcharmod(), msg) " quadruple-click
962 call MouseLeftRelease(1, 2)
963 call assert_equal("\<c-v>", mode(), msg)
964 norm! r4
965 call assert_equal(['34333333333333333', 'foo'], getline(1, '$'), msg)
Bram Moolenaar2764d062020-07-18 12:59:19 +0200966
967 " Double-click on a space character should visually select all the
968 " consecutive space characters.
969 %d
970 call setline(1, ' one two')
971 call MouseLeftClick(1, 2)
972 call MouseLeftRelease(1, 2)
973 call MouseLeftClick(1, 2)
974 call MouseLeftRelease(1, 2)
975 call assert_equal('v', mode(), msg)
976 norm! r1
977 call assert_equal(['1111one two'], getline(1, '$'), msg)
978
979 " Double-click on a word with exclusive selection
980 set selection=exclusive
981 let @" = ''
982 call MouseLeftClick(1, 10)
983 call MouseLeftRelease(1, 10)
984 call MouseLeftClick(1, 10)
985 call MouseLeftRelease(1, 10)
986 norm! y
987 call assert_equal('two', @", msg)
988
989 " Double click to select a block of text with exclusive selection
990 %d
991 call setline(1, 'one (two) three')
992 call MouseLeftClick(1, 5)
993 call MouseLeftRelease(1, 5)
994 call MouseLeftClick(1, 5)
995 call MouseLeftRelease(1, 5)
996 norm! y
997 call assert_equal(5, col("'<"), msg)
998 call assert_equal(10, col("'>"), msg)
999
1000 call MouseLeftClick(1, 9)
1001 call MouseLeftRelease(1, 9)
1002 call MouseLeftClick(1, 9)
1003 call MouseLeftRelease(1, 9)
1004 norm! y
1005 call assert_equal(5, col("'<"), msg)
1006 call assert_equal(10, col("'>"), msg)
1007 set selection&
1008
1009 " Click somewhere else so that the clicks above is not combined with the
1010 " clicks in the next iteration.
1011 call MouseRightClick(3, 10)
1012 call MouseRightRelease(3, 10)
Bram Moolenaarf36a2c72019-11-16 18:57:16 +01001013 endfor
1014
1015 let &mouse = save_mouse
1016 let &term = save_term
1017 let &ttymouse = save_ttymouse
1018 set mousetime&
1019 call test_override('no_query_mouse', 0)
1020 bwipe!
1021endfunc
1022
Bram Moolenaar2764d062020-07-18 12:59:19 +02001023" Test for selecting text in visual blockwise mode using Alt-LeftClick
1024func Test_mouse_alt_leftclick()
1025 let save_mouse = &mouse
1026 let save_term = &term
1027 let save_ttymouse = &ttymouse
1028 call test_override('no_query_mouse', 1)
1029 set mouse=a term=xterm mousetime=200
1030 set mousemodel=popup
Bram Moolenaarc255b782022-11-26 19:16:48 +00001031 call WaitForResponses()
1032
Bram Moolenaar2764d062020-07-18 12:59:19 +02001033 new
1034 call setline(1, 'one (two) three')
1035
1036 for ttymouse_val in g:Ttymouse_values
1037 let msg = 'ttymouse=' .. ttymouse_val
1038 exe 'set ttymouse=' .. ttymouse_val
1039
1040 " Left click with the Alt modifier key should extend the selection in
1041 " blockwise visual mode.
1042 let @" = ''
1043 call MouseLeftClick(1, 3)
1044 call MouseLeftRelease(1, 3)
1045 call MouseAltLeftClick(1, 11)
1046 call MouseLeftRelease(1, 11)
1047 call assert_equal("\<C-V>", mode(), msg)
1048 normal! y
1049 call assert_equal('e (two) t', @")
1050 endfor
1051
1052 let &mouse = save_mouse
1053 let &term = save_term
1054 let &ttymouse = save_ttymouse
1055 set mousetime& mousemodel&
1056 call test_override('no_query_mouse', 0)
1057 close!
1058endfunc
1059
Bram Moolenaar696d6372019-04-17 16:33:46 +02001060func Test_xterm_mouse_click_in_fold_columns()
1061 new
1062 let save_mouse = &mouse
1063 let save_term = &term
1064 let save_ttymouse = &ttymouse
1065 let save_foldcolumn = &foldcolumn
Bram Moolenaar2b00b9b2019-04-17 17:08:27 +02001066 set mouse=a term=xterm foldcolumn=3 ttymouse=xterm2
Bram Moolenaarc255b782022-11-26 19:16:48 +00001067 call WaitForResponses()
Bram Moolenaar696d6372019-04-17 16:33:46 +02001068
1069 " Create 2 nested folds.
1070 call setline(1, range(1, 7))
1071 2,6fold
1072 norm! zR
1073 4,5fold
1074 call assert_equal([-1, -1, -1, 4, 4, -1, -1],
1075 \ map(range(1, 7), 'foldclosed(v:val)'))
1076
1077 " Click in "+" of inner fold in foldcolumn should open it.
1078 redraw
1079 let row = 4
1080 let col = 2
1081 call MouseLeftClick(row, col)
1082 call MouseLeftRelease(row, col)
1083 call assert_equal([-1, -1, -1, -1, -1, -1, -1],
1084 \ map(range(1, 7), 'foldclosed(v:val)'))
1085
1086 " Click in "-" of outer fold in foldcolumn should close it.
1087 redraw
1088 let row = 2
1089 let col = 1
1090 call MouseLeftClick(row, col)
1091 call MouseLeftRelease(row, col)
1092 call assert_equal([-1, 2, 2, 2, 2, 2, -1],
1093 \ map(range(1, 7), 'foldclosed(v:val)'))
1094 norm! zR
1095
1096 " Click in "|" of inner fold in foldcolumn should close it.
1097 redraw
1098 let row = 5
1099 let col = 2
1100 call MouseLeftClick(row, col)
1101 call MouseLeftRelease(row, col)
1102 call assert_equal([-1, -1, -1, 4, 4, -1, -1],
1103 \ map(range(1, 7), 'foldclosed(v:val)'))
1104
1105 let &foldcolumn = save_foldcolumn
1106 let &ttymouse = save_ttymouse
1107 let &term = save_term
1108 let &mouse = save_mouse
1109 bwipe!
1110endfunc
Bram Moolenaar66761db2019-06-05 22:07:51 +02001111
Bram Moolenaarf19f8d12019-12-18 19:36:23 +01001112" Left or right click in Ex command line sets position of the cursor.
1113func Test_term_mouse_click_in_cmdline_to_set_pos()
1114 let save_mouse = &mouse
1115 let save_term = &term
1116 let save_ttymouse = &ttymouse
1117 call test_override('no_query_mouse', 1)
1118 set mouse=a term=xterm
Bram Moolenaarc255b782022-11-26 19:16:48 +00001119 call WaitForResponses()
1120
Bram Moolenaarf19f8d12019-12-18 19:36:23 +01001121 let row = &lines
1122
Bram Moolenaar515545e2020-03-22 14:08:59 +01001123 for ttymouse_val in g:Ttymouse_values + g:Ttymouse_dec
Bram Moolenaarab505b12020-03-23 19:28:44 +01001124 " When 'ttymouse' is 'xterm2', row/col bigger than 223 are not supported.
1125 if ttymouse_val !=# 'xterm2' || row <= 223
1126 let msg = 'ttymouse=' .. ttymouse_val
1127 exe 'set ttymouse=' .. ttymouse_val
Bram Moolenaarf19f8d12019-12-18 19:36:23 +01001128
Bram Moolenaarab505b12020-03-23 19:28:44 +01001129
1130 call feedkeys(':"3456789'
1131 \ .. MouseLeftClickCode(row, 7)
1132 \ .. MouseLeftReleaseCode(row, 7) .. 'L'
1133 \ .. MouseRightClickCode(row, 4)
1134 \ .. MouseRightReleaseCode(row, 4) .. 'R'
1135 \ .. "\<CR>", 'Lx!')
1136 call assert_equal('"3R456L789', @:, msg)
1137 endif
Bram Moolenaarf19f8d12019-12-18 19:36:23 +01001138 endfor
1139
1140 let &mouse = save_mouse
1141 let &term = save_term
1142 let &ttymouse = save_ttymouse
1143 set mousetime&
1144 call test_override('no_query_mouse', 0)
1145endfunc
1146
1147" Middle click in command line pastes at position of cursor.
1148func Test_term_mouse_middle_click_in_cmdline_to_paste()
1149 CheckFeature clipboard_working
1150 let save_mouse = &mouse
1151 let save_term = &term
1152 let save_ttymouse = &ttymouse
1153 call test_override('no_query_mouse', 1)
1154 set mouse=a term=xterm
Bram Moolenaarc255b782022-11-26 19:16:48 +00001155 call WaitForResponses()
1156
Bram Moolenaarf19f8d12019-12-18 19:36:23 +01001157 let row = &lines
1158 " Column values does not matter, paste is done at position of cursor.
1159 let col = 1
1160 let @* = 'paste'
1161
Bram Moolenaar515545e2020-03-22 14:08:59 +01001162 for ttymouse_val in g:Ttymouse_values + g:Ttymouse_dec
Bram Moolenaarf19f8d12019-12-18 19:36:23 +01001163 let msg = 'ttymouse=' .. ttymouse_val
1164 exe 'set ttymouse=' .. ttymouse_val
1165
1166 call feedkeys(":\"->"
1167 \ .. MouseMiddleReleaseCode(row, col)
1168 \ .. MouseMiddleClickCode(row, col)
1169 \ .. "<-"
1170 \ .. MouseMiddleReleaseCode(row, col)
1171 \ .. MouseMiddleClickCode(row, col)
1172 \ .. "\<CR>", 'Lx!')
1173 call assert_equal('"->paste<-paste', @:, msg)
1174 endfor
1175
1176 let &mouse = save_mouse
1177 let &term = save_term
1178 let &ttymouse = save_ttymouse
1179 let @* = ''
1180 call test_override('no_query_mouse', 0)
1181endfunc
1182
Bram Moolenaar297bec02020-07-14 22:11:04 +02001183" Test for making sure S-Middlemouse doesn't do anything
1184func Test_term_mouse_shift_middle_click()
1185 new
1186 let save_mouse = &mouse
1187 let save_term = &term
1188 let save_ttymouse = &ttymouse
1189 call test_override('no_query_mouse', 1)
1190 set mouse=a term=xterm ttymouse=xterm2 mousemodel=
Bram Moolenaarc255b782022-11-26 19:16:48 +00001191 call WaitForResponses()
Bram Moolenaar297bec02020-07-14 22:11:04 +02001192
1193 call test_setmouse(1, 1)
1194 exe "normal \<S-MiddleMouse>"
1195 call assert_equal([''], getline(1, '$'))
1196 call assert_equal(1, winnr())
1197
1198 let &mouse = save_mouse
1199 let &term = save_term
1200 let &ttymouse = save_ttymouse
1201 set mousemodel&
1202 call test_override('no_query_mouse', 0)
1203 close!
1204endfunc
1205
1206" Test for using mouse in visual mode
1207func Test_term_mouse_visual_mode()
1208 new
1209 let save_mouse = &mouse
1210 let save_term = &term
1211 let save_ttymouse = &ttymouse
1212 call test_override('no_query_mouse', 1)
1213 set term=xterm ttymouse=xterm2
Bram Moolenaarc255b782022-11-26 19:16:48 +00001214 call WaitForResponses()
Bram Moolenaar297bec02020-07-14 22:11:04 +02001215
1216 " If visual mode is not present in 'mouse', then left click should not
1217 " do anything in visal mode.
1218 call setline(1, ['one two three four'])
1219 set mouse=nci
1220 call cursor(1, 5)
1221 let @" = ''
1222 call feedkeys("ve"
1223 \ .. MouseLeftClickCode(1, 15) .. MouseLeftReleaseCode(1, 15)
1224 \ .. 'y', 'Lx!')
1225 call assert_equal(5, col('.'))
1226 call assert_equal('two', @")
1227
1228 " Pressing right click in visual mode should change the visual selection
1229 " if 'mousemodel' doesn't contain popup.
1230 " Right click after the visual selection
1231 set mousemodel=
1232 set mouse=a
1233 call test_setmouse(1, 13)
1234 exe "normal 5|ve\<RightMouse>y"
1235 call assert_equal('two three', @")
1236 call assert_equal(5, col('.'))
1237
1238 " Right click before the visual selection
1239 call test_setmouse(1, 9)
1240 exe "normal 15|ve\<RightMouse>y"
1241 call assert_equal('three four', @")
1242 call assert_equal(9, col('.'))
1243
Bram Moolenaar2764d062020-07-18 12:59:19 +02001244 " Right click inside the selection closer to the start of the selection
1245 call test_setmouse(1, 7)
1246 exe "normal 5|vee\<RightMouse>lly"
1247 call assert_equal('three', @")
1248 call assert_equal(9, col('.'))
1249 call assert_equal(9, col("'<"))
1250 call assert_equal(13, col("'>"))
1251
1252 " Right click inside the selection closer to the end of the selection
1253 call test_setmouse(1, 11)
1254 exe "normal 5|vee\<RightMouse>ly"
1255 call assert_equal('two thre', @")
1256 call assert_equal(5, col('.'))
1257 call assert_equal(5, col("'<"))
1258 call assert_equal(12, col("'>"))
1259
dundargocc57b5bc2022-11-02 13:30:51 +00001260 " Multi-line selection. Right click inside the selection.
Bram Moolenaar2764d062020-07-18 12:59:19 +02001261 call setline(1, repeat(['aaaaaa'], 7))
Bram Moolenaar297bec02020-07-14 22:11:04 +02001262 call test_setmouse(3, 1)
1263 exe "normal ggVG\<RightMouse>y"
1264 call assert_equal(3, line("'<"))
Bram Moolenaar2764d062020-07-18 12:59:19 +02001265 call test_setmouse(5, 1)
Bram Moolenaar297bec02020-07-14 22:11:04 +02001266 exe "normal ggVG\<RightMouse>y"
Bram Moolenaar2764d062020-07-18 12:59:19 +02001267 call assert_equal(5, line("'>"))
1268
1269 " Click right in the middle line of the selection
1270 call test_setmouse(4, 3)
1271 exe "normal ggVG$\<RightMouse>y"
1272 call assert_equal(4, line("'<"))
1273 call test_setmouse(4, 4)
1274 exe "normal ggVG$\<RightMouse>y"
Bram Moolenaar297bec02020-07-14 22:11:04 +02001275 call assert_equal(4, line("'>"))
1276
1277 set mousemodel&
1278 let &mouse = save_mouse
1279 let &term = save_term
1280 let &ttymouse = save_ttymouse
1281 call test_override('no_query_mouse', 0)
1282 close!
1283endfunc
1284
Bram Moolenaar0eabd4d2020-03-15 16:13:53 +01001285" Test for displaying the popup menu using the right mouse click
Bram Moolenaar297bec02020-07-14 22:11:04 +02001286func Test_term_mouse_popup_menu()
Bram Moolenaar0eabd4d2020-03-15 16:13:53 +01001287 CheckFeature menu
1288 new
1289 call setline(1, 'popup menu test')
1290 let save_mouse = &mouse
1291 let save_term = &term
1292 let save_ttymouse = &ttymouse
1293 let save_mousemodel = &mousemodel
1294 call test_override('no_query_mouse', 1)
1295 set mouse=a term=xterm mousemodel=popup
Bram Moolenaarc255b782022-11-26 19:16:48 +00001296 call WaitForResponses()
Bram Moolenaar0eabd4d2020-03-15 16:13:53 +01001297
1298 menu PopUp.foo :let g:menustr = 'foo'<CR>
1299 menu PopUp.bar :let g:menustr = 'bar'<CR>
1300 menu PopUp.baz :let g:menustr = 'baz'<CR>
1301
Bram Moolenaar515545e2020-03-22 14:08:59 +01001302 for ttymouse_val in g:Ttymouse_values
Bram Moolenaar2764d062020-07-18 12:59:19 +02001303 let msg = 'ttymouse=' .. ttymouse_val
Bram Moolenaar0eabd4d2020-03-15 16:13:53 +01001304 exe 'set ttymouse=' .. ttymouse_val
1305 let g:menustr = ''
1306 call feedkeys(MouseRightClickCode(1, 4)
1307 \ .. MouseRightReleaseCode(1, 4) .. "\<Down>\<Down>\<CR>", "x")
Bram Moolenaar2764d062020-07-18 12:59:19 +02001308 call assert_equal('bar', g:menustr, msg)
Bram Moolenaar0eabd4d2020-03-15 16:13:53 +01001309 endfor
1310
1311 unmenu PopUp
1312 let &mouse = save_mouse
1313 let &term = save_term
1314 let &ttymouse = save_ttymouse
1315 let &mousemodel = save_mousemodel
1316 call test_override('no_query_mouse', 0)
1317 close!
1318endfunc
1319
Bram Moolenaar297bec02020-07-14 22:11:04 +02001320" Test for 'mousemodel' set to popup_setpos to move the cursor where the popup
1321" menu is displayed.
1322func Test_term_mouse_popup_menu_setpos()
1323 CheckFeature menu
1324 5new
1325 call setline(1, ['the dish ran away with the spoon',
1326 \ 'the cow jumped over the moon' ])
1327 let save_mouse = &mouse
1328 let save_term = &term
1329 let save_ttymouse = &ttymouse
1330 let save_mousemodel = &mousemodel
1331 call test_override('no_query_mouse', 1)
1332 set mouse=a term=xterm mousemodel=popup_setpos
Bram Moolenaarc255b782022-11-26 19:16:48 +00001333 call WaitForResponses()
Bram Moolenaar297bec02020-07-14 22:11:04 +02001334
1335 nmenu PopUp.foo :let g:menustr = 'foo'<CR>
1336 nmenu PopUp.bar :let g:menustr = 'bar'<CR>
1337 nmenu PopUp.baz :let g:menustr = 'baz'<CR>
1338 vmenu PopUp.foo y:<C-U>let g:menustr = 'foo'<CR>
1339 vmenu PopUp.bar y:<C-U>let g:menustr = 'bar'<CR>
1340 vmenu PopUp.baz y:<C-U>let g:menustr = 'baz'<CR>
1341
1342 for ttymouse_val in g:Ttymouse_values
Bram Moolenaar2764d062020-07-18 12:59:19 +02001343 let msg = 'ttymouse=' .. ttymouse_val
Bram Moolenaar297bec02020-07-14 22:11:04 +02001344 exe 'set ttymouse=' .. ttymouse_val
1345 let g:menustr = ''
1346 call cursor(1, 1)
1347 call feedkeys(MouseRightClickCode(1, 5)
1348 \ .. MouseRightReleaseCode(1, 5) .. "\<Down>\<Down>\<CR>", "x")
Bram Moolenaar2764d062020-07-18 12:59:19 +02001349 call assert_equal('bar', g:menustr, msg)
1350 call assert_equal([1, 5], [line('.'), col('.')], msg)
Bram Moolenaar297bec02020-07-14 22:11:04 +02001351
1352 " Test for right click in visual mode inside the selection
1353 let @" = ''
1354 call cursor(1, 10)
1355 call feedkeys('vee' .. MouseRightClickCode(1, 12)
1356 \ .. MouseRightReleaseCode(1, 12) .. "\<Down>\<CR>", "x")
Bram Moolenaar2764d062020-07-18 12:59:19 +02001357 call assert_equal([1, 10], [line('.'), col('.')], msg)
1358 call assert_equal('ran away', @", msg)
Bram Moolenaar297bec02020-07-14 22:11:04 +02001359
Yee Cheng Chin17822c52022-10-13 13:17:40 +01001360 " Test for right click in visual mode right before the selection
Bram Moolenaar297bec02020-07-14 22:11:04 +02001361 let @" = ''
1362 call cursor(1, 10)
Yee Cheng Chin17822c52022-10-13 13:17:40 +01001363 call feedkeys('vee' .. MouseRightClickCode(1, 9)
1364 \ .. MouseRightReleaseCode(1, 9) .. "\<Down>\<CR>", "x")
1365 call assert_equal([1, 9], [line('.'), col('.')], msg)
Bram Moolenaar2764d062020-07-18 12:59:19 +02001366 call assert_equal('', @", msg)
Bram Moolenaar297bec02020-07-14 22:11:04 +02001367
Yee Cheng Chin17822c52022-10-13 13:17:40 +01001368 " Test for right click in visual mode right after the selection
Bram Moolenaar297bec02020-07-14 22:11:04 +02001369 let @" = ''
1370 call cursor(1, 10)
Yee Cheng Chin17822c52022-10-13 13:17:40 +01001371 call feedkeys('vee' .. MouseRightClickCode(1, 18)
1372 \ .. MouseRightReleaseCode(1, 18) .. "\<Down>\<CR>", "x")
1373 call assert_equal([1, 18], [line('.'), col('.')], msg)
Bram Moolenaar2764d062020-07-18 12:59:19 +02001374 call assert_equal('', @", msg)
Bram Moolenaar297bec02020-07-14 22:11:04 +02001375
1376 " Test for right click in block-wise visual mode inside the selection
1377 let @" = ''
1378 call cursor(1, 16)
1379 call feedkeys("\<C-V>j3l" .. MouseRightClickCode(2, 17)
1380 \ .. MouseRightReleaseCode(2, 17) .. "\<Down>\<CR>", "x")
Bram Moolenaar2764d062020-07-18 12:59:19 +02001381 call assert_equal([1, 16], [line('.'), col('.')], msg)
1382 call assert_equal("\<C-V>4", getregtype('"'), msg)
Bram Moolenaar297bec02020-07-14 22:11:04 +02001383
1384 " Test for right click in block-wise visual mode outside the selection
1385 let @" = ''
1386 call cursor(1, 16)
1387 call feedkeys("\<C-V>j3l" .. MouseRightClickCode(2, 2)
1388 \ .. MouseRightReleaseCode(2, 2) .. "\<Down>\<CR>", "x")
Bram Moolenaar2764d062020-07-18 12:59:19 +02001389 call assert_equal([2, 2], [line('.'), col('.')], msg)
1390 call assert_equal('v', getregtype('"'), msg)
1391 call assert_equal('', @", msg)
Bram Moolenaar297bec02020-07-14 22:11:04 +02001392
Yee Cheng Chin17822c52022-10-13 13:17:40 +01001393 " Test for right click in line-wise visual mode inside the selection
1394 let @" = ''
1395 call cursor(1, 16)
1396 call feedkeys("V" .. MouseRightClickCode(1, 10)
1397 \ .. MouseRightReleaseCode(1, 10) .. "\<Down>\<CR>", "x")
1398 call assert_equal([1, 1], [line('.'), col('.')], msg) " After yanking, the cursor goes to 1,1
1399 call assert_equal("V", getregtype('"'), msg)
zeertzjqdf63f052022-10-19 15:12:54 +01001400 call assert_equal(1, len(getreg('"', 1, v:true)), msg)
Yee Cheng Chin17822c52022-10-13 13:17:40 +01001401
1402 " Test for right click in multi-line line-wise visual mode inside the selection
1403 let @" = ''
1404 call cursor(1, 16)
1405 call feedkeys("Vj" .. MouseRightClickCode(2, 20)
1406 \ .. MouseRightReleaseCode(2, 20) .. "\<Down>\<CR>", "x")
1407 call assert_equal([1, 1], [line('.'), col('.')], msg) " After yanking, the cursor goes to 1,1
1408 call assert_equal("V", getregtype('"'), msg)
zeertzjqdf63f052022-10-19 15:12:54 +01001409 call assert_equal(2, len(getreg('"', 1, v:true)), msg)
Yee Cheng Chin17822c52022-10-13 13:17:40 +01001410
1411 " Test for right click in line-wise visual mode outside the selection
1412 let @" = ''
1413 call cursor(1, 16)
1414 call feedkeys("V" .. MouseRightClickCode(2, 10)
1415 \ .. MouseRightReleaseCode(2, 10) .. "\<Down>\<CR>", "x")
1416 call assert_equal([2, 10], [line('.'), col('.')], msg)
1417 call assert_equal("", @", msg)
1418
Bram Moolenaar297bec02020-07-14 22:11:04 +02001419 " Try clicking on the status line
1420 let @" = ''
1421 call cursor(1, 10)
1422 call feedkeys('vee' .. MouseRightClickCode(6, 2)
1423 \ .. MouseRightReleaseCode(6, 2) .. "\<Down>\<CR>", "x")
Bram Moolenaar2764d062020-07-18 12:59:19 +02001424 call assert_equal([1, 10], [line('.'), col('.')], msg)
1425 call assert_equal('ran away', @", msg)
Bram Moolenaar297bec02020-07-14 22:11:04 +02001426
1427 " Try clicking outside the window
1428 let @" = ''
zeertzjqdf63f052022-10-19 15:12:54 +01001429 call cursor(2, 2)
Bram Moolenaar297bec02020-07-14 22:11:04 +02001430 call feedkeys('vee' .. MouseRightClickCode(7, 2)
1431 \ .. MouseRightReleaseCode(7, 2) .. "\<Down>\<CR>", "x")
Bram Moolenaar2764d062020-07-18 12:59:19 +02001432 call assert_equal(2, winnr(), msg)
1433 call assert_equal('', @", msg)
Bram Moolenaar297bec02020-07-14 22:11:04 +02001434 wincmd w
1435 endfor
1436
1437 unmenu PopUp
1438 let &mouse = save_mouse
1439 let &term = save_term
1440 let &ttymouse = save_ttymouse
1441 let &mousemodel = save_mousemodel
1442 call test_override('no_query_mouse', 0)
1443 close!
1444endfunc
1445
1446" Test for searching for the word under the cursor using Shift-Right or
1447" Shift-Left click.
1448func Test_term_mouse_search()
1449 new
1450 let save_mouse = &mouse
1451 let save_term = &term
1452 let save_ttymouse = &ttymouse
1453 call test_override('no_query_mouse', 1)
1454 set mouse=a term=xterm ttymouse=xterm2
1455 set mousemodel=
Bram Moolenaarc255b782022-11-26 19:16:48 +00001456 call WaitForResponses()
Bram Moolenaar297bec02020-07-14 22:11:04 +02001457
1458 " In normal mode, Shift-Left or Shift-Right click should search for the word
1459 " under the cursor.
1460 call setline(1, ['one two three four', 'four three two one'])
1461 call test_setmouse(1, 4)
1462 exe "normal \<S-LeftMouse>"
1463 call assert_equal([2, 12], [line('.'), col('.')])
1464 call test_setmouse(2, 16)
1465 exe "normal \<S-RightMouse>"
1466 call assert_equal([1, 1], [line('.'), col('.')])
1467
1468 " In visual mode, Shift-Left or Shift-Right click should search for the word
1469 " under the cursor and extend the selection.
1470 call test_setmouse(1, 4)
1471 exe "normal 4|ve\<S-LeftMouse>y"
1472 call assert_equal([2, 12], [line("'>"), col("'>")])
1473 call test_setmouse(2, 16)
1474 exe "normal 2G16|ve\<S-RightMouse>y"
1475 call assert_equal([1, 1], [line("'<"), col("'<")])
1476
1477 set mousemodel&
1478 let &mouse = save_mouse
1479 let &term = save_term
1480 let &ttymouse = save_ttymouse
1481 call test_override('no_query_mouse', 0)
1482 close!
1483endfunc
1484
1485" Test for selecting an entry in the quickfix/location list window using the
1486" mouse.
1487func Test_term_mouse_quickfix_window()
1488 let save_mouse = &mouse
1489 let save_term = &term
1490 let save_ttymouse = &ttymouse
1491 call test_override('no_query_mouse', 1)
1492 set mouse=a term=xterm ttymouse=xterm2
1493 set mousemodel=
Bram Moolenaarc255b782022-11-26 19:16:48 +00001494 call WaitForResponses()
Bram Moolenaar297bec02020-07-14 22:11:04 +02001495
1496 cgetexpr "Xfile1:1:L1"
1497 copen 5
1498 call test_setmouse(&lines - 7, 1)
1499 exe "normal \<2-LeftMouse>"
1500 call assert_equal('Xfile1', @%)
1501 %bw!
1502
1503 lgetexpr "Xfile2:1:L1"
1504 lopen 5
1505 call test_setmouse(&lines - 7, 1)
1506 exe "normal \<2-LeftMouse>"
1507 call assert_equal('Xfile2', @%)
1508 %bw!
1509
1510 set mousemodel&
1511 let &mouse = save_mouse
1512 let &term = save_term
1513 let &ttymouse = save_ttymouse
1514 call test_override('no_query_mouse', 0)
1515endfunc
1516
Bram Moolenaar2764d062020-07-18 12:59:19 +02001517" Test for the 'h' flag in the 'mouse' option. Using mouse in the help window.
1518func Test_term_mouse_help_window()
1519 let save_mouse = &mouse
1520 let save_term = &term
1521 let save_ttymouse = &ttymouse
1522 call test_override('no_query_mouse', 1)
1523 set mouse=h term=xterm mousetime=200
Bram Moolenaarc255b782022-11-26 19:16:48 +00001524 call WaitForResponses()
Bram Moolenaar2764d062020-07-18 12:59:19 +02001525
1526 for ttymouse_val in g:Ttymouse_values + g:Ttymouse_dec
1527 let msg = 'ttymouse=' .. ttymouse_val
1528 exe 'set ttymouse=' .. ttymouse_val
1529 help
1530 let @" = ''
1531 call MouseLeftClick(2, 5)
1532 call MouseLeftRelease(2, 5)
1533 call MouseLeftClick(1, 1)
1534 call MouseLeftDrag(1, 10)
1535 call MouseLeftRelease(1, 10)
1536 norm! y
1537 call assert_equal('*help.txt*', @", msg)
1538 helpclose
1539
1540 " Click somewhere else to make sure the left click above is not combined
1541 " with the next left click and treated as a double click
1542 call MouseRightClick(5, 10)
1543 call MouseRightRelease(5, 10)
1544 endfor
1545
1546 let &mouse = save_mouse
1547 let &term = save_term
1548 let &ttymouse = save_ttymouse
1549 set mousetime&
1550 call test_override('no_query_mouse', 0)
1551 %bw!
1552endfunc
1553
1554" Test for the translation of various mouse terminal codes
1555func Test_mouse_termcodes()
1556 let save_mouse = &mouse
1557 let save_term = &term
1558 let save_ttymouse = &ttymouse
1559 call test_override('no_query_mouse', 1)
1560 set mouse=a term=xterm mousetime=200
Bram Moolenaarc255b782022-11-26 19:16:48 +00001561 call WaitForResponses()
Bram Moolenaar2764d062020-07-18 12:59:19 +02001562
1563 new
1564 for ttymouse_val in g:Ttymouse_values + g:Ttymouse_dec + g:Ttymouse_netterm
1565 let msg = 'ttymouse=' .. ttymouse_val
1566 exe 'set ttymouse=' .. ttymouse_val
1567
1568 let mouse_codes = [
1569 \ ["\<LeftMouse>", "<LeftMouse>"],
1570 \ ["\<MiddleMouse>", "<MiddleMouse>"],
1571 \ ["\<RightMouse>", "<RightMouse>"],
1572 \ ["\<S-LeftMouse>", "<S-LeftMouse>"],
1573 \ ["\<S-MiddleMouse>", "<S-MiddleMouse>"],
1574 \ ["\<S-RightMouse>", "<S-RightMouse>"],
1575 \ ["\<C-LeftMouse>", "<C-LeftMouse>"],
1576 \ ["\<C-MiddleMouse>", "<C-MiddleMouse>"],
1577 \ ["\<C-RightMouse>", "<C-RightMouse>"],
1578 \ ["\<M-LeftMouse>", "<M-LeftMouse>"],
1579 \ ["\<M-MiddleMouse>", "<M-MiddleMouse>"],
1580 \ ["\<M-RightMouse>", "<M-RightMouse>"],
1581 \ ["\<2-LeftMouse>", "<2-LeftMouse>"],
1582 \ ["\<2-MiddleMouse>", "<2-MiddleMouse>"],
1583 \ ["\<2-RightMouse>", "<2-RightMouse>"],
1584 \ ["\<3-LeftMouse>", "<3-LeftMouse>"],
1585 \ ["\<3-MiddleMouse>", "<3-MiddleMouse>"],
1586 \ ["\<3-RightMouse>", "<3-RightMouse>"],
1587 \ ["\<4-LeftMouse>", "<4-LeftMouse>"],
1588 \ ["\<4-MiddleMouse>", "<4-MiddleMouse>"],
1589 \ ["\<4-RightMouse>", "<4-RightMouse>"],
1590 \ ["\<LeftDrag>", "<LeftDrag>"],
1591 \ ["\<MiddleDrag>", "<MiddleDrag>"],
1592 \ ["\<RightDrag>", "<RightDrag>"],
1593 \ ["\<LeftRelease>", "<LeftRelease>"],
1594 \ ["\<MiddleRelease>", "<MiddleRelease>"],
1595 \ ["\<RightRelease>", "<RightRelease>"],
1596 \ ["\<ScrollWheelUp>", "<ScrollWheelUp>"],
1597 \ ["\<S-ScrollWheelUp>", "<S-ScrollWheelUp>"],
1598 \ ["\<C-ScrollWheelUp>", "<C-ScrollWheelUp>"],
1599 \ ["\<ScrollWheelDown>", "<ScrollWheelDown>"],
1600 \ ["\<S-ScrollWheelDown>", "<S-ScrollWheelDown>"],
1601 \ ["\<C-ScrollWheelDown>", "<C-ScrollWheelDown>"],
1602 \ ["\<ScrollWheelLeft>", "<ScrollWheelLeft>"],
1603 \ ["\<S-ScrollWheelLeft>", "<S-ScrollWheelLeft>"],
1604 \ ["\<C-ScrollWheelLeft>", "<C-ScrollWheelLeft>"],
1605 \ ["\<ScrollWheelRight>", "<ScrollWheelRight>"],
1606 \ ["\<S-ScrollWheelRight>", "<S-ScrollWheelRight>"],
1607 \ ["\<C-ScrollWheelRight>", "<C-ScrollWheelRight>"]
1608 \ ]
1609
1610 for [code, outstr] in mouse_codes
1611 exe "normal ggC\<C-K>" . code
1612 call assert_equal(outstr, getline(1), msg)
1613 endfor
1614 endfor
1615
1616 let &mouse = save_mouse
1617 let &term = save_term
1618 let &ttymouse = save_ttymouse
1619 set mousetime&
1620 call test_override('no_query_mouse', 0)
1621 %bw!
1622endfunc
1623
Bram Moolenaar66761db2019-06-05 22:07:51 +02001624" This only checks if the sequence is recognized.
Bram Moolenaar66761db2019-06-05 22:07:51 +02001625func Test_term_rgb_response()
1626 set t_RF=x
1627 set t_RB=y
1628
1629 " response to t_RF, 4 digits
1630 let red = 0x12
1631 let green = 0x34
1632 let blue = 0x56
1633 let seq = printf("\<Esc>]10;rgb:%02x00/%02x00/%02x00\x07", red, green, blue)
1634 call feedkeys(seq, 'Lx!')
1635 call assert_equal(seq, v:termrfgresp)
1636
1637 " response to t_RF, 2 digits
1638 let red = 0x78
1639 let green = 0x9a
1640 let blue = 0xbc
1641 let seq = printf("\<Esc>]10;rgb:%02x/%02x/%02x\x07", red, green, blue)
1642 call feedkeys(seq, 'Lx!')
1643 call assert_equal(seq, v:termrfgresp)
1644
Bram Moolenaar32e19772019-06-05 22:57:04 +02001645 " response to t_RB, 4 digits, dark
1646 set background=light
Bram Moolenaarce90e362019-09-08 18:58:44 +02001647 eval 'background'->test_option_not_set()
Bram Moolenaar32e19772019-06-05 22:57:04 +02001648 let red = 0x29
1649 let green = 0x4a
1650 let blue = 0x6b
1651 let seq = printf("\<Esc>]11;rgb:%02x00/%02x00/%02x00\x07", red, green, blue)
1652 call feedkeys(seq, 'Lx!')
1653 call assert_equal(seq, v:termrbgresp)
1654 call assert_equal('dark', &background)
1655
1656 " response to t_RB, 4 digits, light
1657 set background=dark
1658 call test_option_not_set('background')
1659 let red = 0x81
1660 let green = 0x63
Bram Moolenaar66761db2019-06-05 22:07:51 +02001661 let blue = 0x65
1662 let seq = printf("\<Esc>]11;rgb:%02x00/%02x00/%02x00\x07", red, green, blue)
1663 call feedkeys(seq, 'Lx!')
1664 call assert_equal(seq, v:termrbgresp)
Bram Moolenaar32e19772019-06-05 22:57:04 +02001665 call assert_equal('light', &background)
Bram Moolenaar66761db2019-06-05 22:07:51 +02001666
Bram Moolenaar32e19772019-06-05 22:57:04 +02001667 " response to t_RB, 2 digits, dark
1668 set background=light
1669 call test_option_not_set('background')
1670 let red = 0x47
1671 let green = 0x59
1672 let blue = 0x5b
Bram Moolenaar66761db2019-06-05 22:07:51 +02001673 let seq = printf("\<Esc>]11;rgb:%02x/%02x/%02x\x07", red, green, blue)
1674 call feedkeys(seq, 'Lx!')
1675 call assert_equal(seq, v:termrbgresp)
Bram Moolenaar32e19772019-06-05 22:57:04 +02001676 call assert_equal('dark', &background)
Bram Moolenaarf19f8d12019-12-18 19:36:23 +01001677
Bram Moolenaar32e19772019-06-05 22:57:04 +02001678 " response to t_RB, 2 digits, light
1679 set background=dark
1680 call test_option_not_set('background')
1681 let red = 0x83
1682 let green = 0xa4
1683 let blue = 0xc2
1684 let seq = printf("\<Esc>]11;rgb:%02x/%02x/%02x\x07", red, green, blue)
1685 call feedkeys(seq, 'Lx!')
1686 call assert_equal(seq, v:termrbgresp)
1687 call assert_equal('light', &background)
Bram Moolenaarf19f8d12019-12-18 19:36:23 +01001688
Bram Moolenaar66761db2019-06-05 22:07:51 +02001689 set t_RF= t_RB=
1690endfunc
1691
1692" This only checks if the sequence is recognized.
Bram Moolenaar1a4cbb12019-10-12 13:25:44 +02001693" This must be after other tests, because it has side effects to xterm
1694" properties.
1695func Test_xx01_term_style_response()
Bram Moolenaar66761db2019-06-05 22:07:51 +02001696 " Termresponse is only parsed when t_RV is not empty.
1697 set t_RV=x
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001698 call test_override('term_props', 1)
Bram Moolenaar66761db2019-06-05 22:07:51 +02001699
1700 " send the termresponse to trigger requesting the XT codes
1701 let seq = "\<Esc>[>41;337;0c"
1702 call feedkeys(seq, 'Lx!')
1703 call assert_equal(seq, v:termresponse)
1704
1705 let seq = "\<Esc>P1$r2 q\<Esc>\\"
1706 call feedkeys(seq, 'Lx!')
1707 call assert_equal(seq, v:termstyleresp)
1708
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001709 call assert_equal(#{
1710 \ cursor_style: 'u',
1711 \ cursor_blink_mode: 'u',
1712 \ underline_rgb: 'u',
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01001713 \ mouse: 's',
1714 \ kitty: 'u',
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001715 \ }, terminalprops())
1716
Bram Moolenaar66761db2019-06-05 22:07:51 +02001717 set t_RV=
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001718 call test_override('term_props', 0)
Bram Moolenaar66761db2019-06-05 22:07:51 +02001719endfunc
Bram Moolenaarde6dbb42019-06-06 11:59:18 +02001720
Bram Moolenaar89577b32019-10-18 21:26:05 +02001721" This checks the iTerm2 version response.
1722" This must be after other tests, because it has side effects to xterm
1723" properties.
1724func Test_xx02_iTerm2_response()
1725 " Termresponse is only parsed when t_RV is not empty.
1726 set t_RV=x
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001727 call test_override('term_props', 1)
Bram Moolenaar89577b32019-10-18 21:26:05 +02001728
1729 " Old versions of iTerm2 used a different style term response.
1730 set ttymouse=xterm
1731 call test_option_not_set('ttymouse')
1732 let seq = "\<Esc>[>0;95;c"
1733 call feedkeys(seq, 'Lx!')
1734 call assert_equal(seq, v:termresponse)
1735 call assert_equal('xterm', &ttymouse)
1736
1737 set ttymouse=xterm
1738 call test_option_not_set('ttymouse')
1739 let seq = "\<Esc>[>0;95;0c"
1740 call feedkeys(seq, 'Lx!')
1741 call assert_equal(seq, v:termresponse)
1742 call assert_equal('sgr', &ttymouse)
1743
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001744 call assert_equal(#{
1745 \ cursor_style: 'n',
1746 \ cursor_blink_mode: 'u',
1747 \ underline_rgb: 'u',
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01001748 \ mouse: 's',
1749 \ kitty: 'u',
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001750 \ }, terminalprops())
1751
Bram Moolenaar89577b32019-10-18 21:26:05 +02001752 set t_RV=
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001753 call test_override('term_props', 0)
Bram Moolenaar89577b32019-10-18 21:26:05 +02001754endfunc
1755
Bram Moolenaard55f9ef2022-08-26 12:26:07 +01001756func Run_libvterm_konsole_response(code)
Bram Moolenaar1a4cbb12019-10-12 13:25:44 +02001757 set ttymouse=xterm
1758 call test_option_not_set('ttymouse')
Bram Moolenaard55f9ef2022-08-26 12:26:07 +01001759 let seq = "\<Esc>[>0;" .. a:code .. ";0c"
Bram Moolenaar1a4cbb12019-10-12 13:25:44 +02001760 call feedkeys(seq, 'Lx!')
1761 call assert_equal(seq, v:termresponse)
1762 call assert_equal('sgr', &ttymouse)
1763
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001764 call assert_equal(#{
1765 \ cursor_style: 'n',
1766 \ cursor_blink_mode: 'u',
1767 \ underline_rgb: 'u',
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01001768 \ mouse: 's',
1769 \ kitty: 'u',
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001770 \ }, terminalprops())
Bram Moolenaard55f9ef2022-08-26 12:26:07 +01001771endfunc
1772
1773" This checks the libvterm version response.
1774" This must be after other tests, because it has side effects to xterm
1775" properties.
1776func Test_xx03_libvterm_konsole_response()
1777 " Termresponse is only parsed when t_RV is not empty.
1778 set t_RV=x
1779 call test_override('term_props', 1)
1780
1781 " libvterm
1782 call Run_libvterm_konsole_response(100)
1783 " Konsole
1784 call Run_libvterm_konsole_response(115)
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001785
Bram Moolenaar1a4cbb12019-10-12 13:25:44 +02001786 set t_RV=
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001787 call test_override('term_props', 0)
Bram Moolenaar1a4cbb12019-10-12 13:25:44 +02001788endfunc
1789
Bram Moolenaar89577b32019-10-18 21:26:05 +02001790" This checks the Mac Terminal.app version response.
1791" This must be after other tests, because it has side effects to xterm
1792" properties.
1793func Test_xx04_Mac_Terminal_response()
1794 " Termresponse is only parsed when t_RV is not empty.
1795 set t_RV=x
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001796 call test_override('term_props', 1)
Bram Moolenaar89577b32019-10-18 21:26:05 +02001797
1798 set ttymouse=xterm
Bram Moolenaar8dff4cb2020-06-14 14:34:16 +02001799 " t_8u is not reset
1800 let &t_8u = "\<Esc>[58;2;%lu;%lu;%lum"
Bram Moolenaar89577b32019-10-18 21:26:05 +02001801 call test_option_not_set('ttymouse')
1802 let seq = "\<Esc>[>1;95;0c"
1803 call feedkeys(seq, 'Lx!')
1804 call assert_equal(seq, v:termresponse)
1805 call assert_equal('sgr', &ttymouse)
1806
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001807 call assert_equal(#{
1808 \ cursor_style: 'n',
1809 \ cursor_blink_mode: 'u',
1810 \ underline_rgb: 'y',
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01001811 \ mouse: 's',
1812 \ kitty: 'u',
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001813 \ }, terminalprops())
Bram Moolenaar8dff4cb2020-06-14 14:34:16 +02001814 call assert_equal("\<Esc>[58;2;%lu;%lu;%lum", &t_8u)
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001815
Bram Moolenaar89577b32019-10-18 21:26:05 +02001816 " Reset is_not_xterm and is_mac_terminal.
1817 set t_RV=
1818 set term=xterm
1819 set t_RV=x
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001820 call test_override('term_props', 0)
Bram Moolenaar89577b32019-10-18 21:26:05 +02001821endfunc
1822
1823" This checks the mintty version response.
1824" This must be after other tests, because it has side effects to xterm
1825" properties.
1826func Test_xx05_mintty_response()
1827 " Termresponse is only parsed when t_RV is not empty.
1828 set t_RV=x
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001829 call test_override('term_props', 1)
Bram Moolenaar89577b32019-10-18 21:26:05 +02001830
1831 set ttymouse=xterm
1832 call test_option_not_set('ttymouse')
1833 let seq = "\<Esc>[>77;20905;0c"
1834 call feedkeys(seq, 'Lx!')
1835 call assert_equal(seq, v:termresponse)
1836 call assert_equal('sgr', &ttymouse)
1837
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001838 call assert_equal(#{
1839 \ cursor_style: 'n',
1840 \ cursor_blink_mode: 'u',
1841 \ underline_rgb: 'y',
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01001842 \ mouse: 's',
1843 \ kitty: 'u',
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001844 \ }, terminalprops())
1845
Bram Moolenaar89577b32019-10-18 21:26:05 +02001846 set t_RV=
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001847 call test_override('term_props', 0)
Bram Moolenaar89577b32019-10-18 21:26:05 +02001848endfunc
1849
1850" This checks the screen version response.
1851" This must be after other tests, because it has side effects to xterm
1852" properties.
1853func Test_xx06_screen_response()
1854 " 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 Moolenaar89577b32019-10-18 21:26:05 +02001857
1858 " Old versions of screen don't support SGR mouse mode.
1859 set ttymouse=xterm
1860 call test_option_not_set('ttymouse')
1861 let seq = "\<Esc>[>83;40500;0c"
1862 call feedkeys(seq, 'Lx!')
1863 call assert_equal(seq, v:termresponse)
1864 call assert_equal('xterm', &ttymouse)
1865
1866 " screen supports SGR mouse mode starting in version 4.7.
1867 set ttymouse=xterm
1868 call test_option_not_set('ttymouse')
1869 let seq = "\<Esc>[>83;40700;0c"
1870 call feedkeys(seq, 'Lx!')
1871 call assert_equal(seq, v:termresponse)
1872 call assert_equal('sgr', &ttymouse)
1873
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001874 call assert_equal(#{
1875 \ cursor_style: 'n',
1876 \ cursor_blink_mode: 'n',
1877 \ underline_rgb: 'y',
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01001878 \ mouse: 's',
1879 \ kitty: 'u',
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001880 \ }, terminalprops())
1881
Bram Moolenaar89577b32019-10-18 21:26:05 +02001882 set t_RV=
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001883 call test_override('term_props', 0)
Bram Moolenaar89577b32019-10-18 21:26:05 +02001884endfunc
1885
Bram Moolenaard0eaf672022-04-20 19:55:37 +01001886func Do_check_t_8u_set_reset(set_by_user)
1887 set ttymouse=xterm
1888 call test_option_not_set('ttymouse')
1889 let default_value = "\<Esc>[58;2;%lu;%lu;%lum"
1890 let &t_8u = default_value
1891 if !a:set_by_user
1892 call test_option_not_set('t_8u')
1893 endif
1894 let seq = "\<Esc>[>0;279;0c"
1895 call feedkeys(seq, 'Lx!')
1896 call assert_equal(seq, v:termresponse)
1897 call assert_equal('sgr', &ttymouse)
1898
1899 call assert_equal(#{
1900 \ cursor_style: 'u',
1901 \ cursor_blink_mode: 'u',
1902 \ underline_rgb: 'u',
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01001903 \ mouse: 's',
1904 \ kitty: 'u',
Bram Moolenaard0eaf672022-04-20 19:55:37 +01001905 \ }, terminalprops())
1906 call assert_equal(a:set_by_user ? default_value : '', &t_8u)
1907endfunc
1908
Bram Moolenaar03b00472019-10-14 22:22:03 +02001909" This checks the xterm version response.
1910" This must be after other tests, because it has side effects to xterm
1911" properties.
Bram Moolenaar89577b32019-10-18 21:26:05 +02001912func Test_xx07_xterm_response()
Bram Moolenaar03b00472019-10-14 22:22:03 +02001913 " Termresponse is only parsed when t_RV is not empty.
1914 set t_RV=x
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001915 call test_override('term_props', 1)
Bram Moolenaar03b00472019-10-14 22:22:03 +02001916
Bram Moolenaar3cea8a92019-10-17 21:55:24 +02001917 " Do Terminal.app first to check that is_mac_terminal is reset.
1918 set ttymouse=xterm
1919 call test_option_not_set('ttymouse')
1920 let seq = "\<Esc>[>1;95;0c"
1921 call feedkeys(seq, 'Lx!')
1922 call assert_equal(seq, v:termresponse)
1923 call assert_equal('sgr', &ttymouse)
1924
Bram Moolenaar03b00472019-10-14 22:22:03 +02001925 " xterm < 95: "xterm" (actually unmodified)
Bram Moolenaar3cea8a92019-10-17 21:55:24 +02001926 set t_RV=
1927 set term=xterm
Bram Moolenaarc255b782022-11-26 19:16:48 +00001928 call WaitForResponses()
1929
Bram Moolenaar3cea8a92019-10-17 21:55:24 +02001930 set t_RV=x
Bram Moolenaar03b00472019-10-14 22:22:03 +02001931 set ttymouse=xterm
1932 call test_option_not_set('ttymouse')
1933 let seq = "\<Esc>[>0;94;0c"
1934 call feedkeys(seq, 'Lx!')
1935 call assert_equal(seq, v:termresponse)
1936 call assert_equal('xterm', &ttymouse)
1937
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001938 call assert_equal(#{
1939 \ cursor_style: 'n',
1940 \ cursor_blink_mode: 'u',
1941 \ underline_rgb: 'y',
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01001942 \ mouse: 'u',
1943 \ kitty: 'u',
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001944 \ }, terminalprops())
1945
Bram Moolenaar03b00472019-10-14 22:22:03 +02001946 " xterm >= 95 < 277 "xterm2"
1947 set ttymouse=xterm
1948 call test_option_not_set('ttymouse')
1949 let seq = "\<Esc>[>0;267;0c"
1950 call feedkeys(seq, 'Lx!')
1951 call assert_equal(seq, v:termresponse)
1952 call assert_equal('xterm2', &ttymouse)
1953
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001954 call assert_equal(#{
1955 \ cursor_style: 'n',
1956 \ cursor_blink_mode: 'u',
1957 \ underline_rgb: 'u',
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01001958 \ mouse: '2',
1959 \ kitty: 'u',
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001960 \ }, terminalprops())
1961
Bram Moolenaar03b00472019-10-14 22:22:03 +02001962 " xterm >= 277: "sgr"
1963 set ttymouse=xterm
1964 call test_option_not_set('ttymouse')
1965 let seq = "\<Esc>[>0;277;0c"
1966 call feedkeys(seq, 'Lx!')
1967 call assert_equal(seq, v:termresponse)
1968 call assert_equal('sgr', &ttymouse)
1969
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001970 call assert_equal(#{
1971 \ cursor_style: 'n',
1972 \ cursor_blink_mode: 'u',
1973 \ underline_rgb: 'u',
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01001974 \ mouse: 's',
1975 \ kitty: 'u',
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001976 \ }, terminalprops())
1977
Bram Moolenaard0eaf672022-04-20 19:55:37 +01001978 " xterm >= 279: "sgr" and cursor_style not reset; also check t_8u reset,
1979 " except when it was set by the user
1980 call Do_check_t_8u_set_reset(0)
1981 call Do_check_t_8u_set_reset(1)
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001982
Bram Moolenaar03b00472019-10-14 22:22:03 +02001983 set t_RV=
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001984 call test_override('term_props', 0)
Bram Moolenaar03b00472019-10-14 22:22:03 +02001985endfunc
1986
Bram Moolenaar4bc85f22022-10-21 14:17:24 +01001987func Test_xx08_kitty_response()
1988 " Termresponse is only parsed when t_RV is not empty.
1989 set t_RV=x
1990 call test_override('term_props', 1)
1991
1992 set ttymouse=xterm
1993 call test_option_not_set('ttymouse')
1994 let seq = "\<Esc>[>1;4001;12c"
1995 call feedkeys(seq, 'Lx!')
1996 call assert_equal(seq, v:termresponse)
1997 call assert_equal('sgr', &ttymouse)
1998
1999 call assert_equal(#{
2000 \ cursor_style: 'u',
2001 \ cursor_blink_mode: 'u',
2002 \ underline_rgb: 'y',
2003 \ mouse: 's',
2004 \ kitty: 'y',
2005 \ }, terminalprops())
2006
2007 set t_RV=
2008 call test_override('term_props', 0)
2009endfunc
2010
Bram Moolenaar92e5df82021-01-30 15:39:47 +01002011func Test_focus_events()
2012 let save_term = &term
2013 let save_ttymouse = &ttymouse
2014 set term=xterm ttymouse=xterm2
Bram Moolenaarc255b782022-11-26 19:16:48 +00002015 call WaitForResponses()
Bram Moolenaar92e5df82021-01-30 15:39:47 +01002016
2017 au FocusGained * let g:focus_gained += 1
2018 au FocusLost * let g:focus_lost += 1
2019 let g:focus_gained = 0
2020 let g:focus_lost = 0
2021
2022 call feedkeys("\<Esc>[O", "Lx!")
2023 call assert_equal(1, g:focus_lost)
2024 call feedkeys("\<Esc>[I", "Lx!")
2025 call assert_equal(1, g:focus_gained)
2026
2027 " still works when 'ttymouse' is empty
2028 set ttymouse=
2029 call feedkeys("\<Esc>[O", "Lx!")
2030 call assert_equal(2, g:focus_lost)
2031 call feedkeys("\<Esc>[I", "Lx!")
2032 call assert_equal(2, g:focus_gained)
2033
2034 au! FocusGained
2035 au! FocusLost
2036 let &term = save_term
2037 let &ttymouse = save_ttymouse
2038endfunc
2039
Bram Moolenaarde6dbb42019-06-06 11:59:18 +02002040func Test_get_termcode()
Bram Moolenaareb663282019-10-06 12:02:15 +02002041 try
2042 let k1 = &t_k1
2043 catch /E113/
2044 throw 'Skipped: Unable to query termcodes'
2045 endtry
Bram Moolenaarde6dbb42019-06-06 11:59:18 +02002046 set t_k1=
2047 set t_k1&
2048 call assert_equal(k1, &t_k1)
Bram Moolenaar9aeb3362019-06-06 12:36:15 +02002049
2050 " use external termcap first
2051 set nottybuiltin
2052 set t_k1=
2053 set t_k1&
2054 " when using external termcap may get something else, but it must not be
2055 " empty, since we would fallback to the builtin one.
2056 call assert_notequal('', &t_k1)
2057
2058 if &term =~ 'xterm'
2059 " use internal termcap first
2060 let term_save = &term
2061 let &term = 'builtin_' .. &term
2062 set t_k1=
2063 set t_k1&
2064 call assert_equal(k1, &t_k1)
2065 let &term = term_save
2066 endif
2067
2068 set ttybuiltin
Bram Moolenaarde6dbb42019-06-06 11:59:18 +02002069endfunc
Bram Moolenaar18a79a62019-10-12 15:36:11 +02002070
Bram Moolenaarecd34bf2020-08-04 20:17:31 +02002071func Test_list_builtin_terminals()
Bram Moolenaare46a2ed2020-08-04 21:04:57 +02002072 CheckRunVimInTerminal
Bram Moolenaar4654d632022-11-17 22:05:12 +00002073
Bram Moolenaar4d05af02020-11-27 20:55:00 +01002074 call RunVimInTerminal('', #{rows: 14})
2075 call term_sendkeys('', ":set cmdheight=3\<CR>")
2076 call TermWait('', 100)
2077 call term_sendkeys('', ":set term=xxx\<CR>")
2078 call TermWait('', 100)
Bram Moolenaar4654d632022-11-17 22:05:12 +00002079
2080 " Check that the list ends in "builtin_dumb" and "builtin_debug".
2081 let dumb_idx = 0
2082 for n in range(8, 12)
2083 if term_getline('', n) =~ 'builtin_dumb'
2084 let dumb_idx = n
2085 break
2086 endif
2087 endfor
2088 call assert_notequal(0, dumb_idx, 'builtin_dumb not found')
2089
2090 call assert_match('builtin_dumb', term_getline('', dumb_idx))
2091 call assert_match('builtin_debug', term_getline('', dumb_idx + 1))
2092 call assert_match('Not found in termcap', term_getline('', dumb_idx + 2))
2093
Bram Moolenaar4d05af02020-11-27 20:55:00 +01002094 call StopVimInTerminal('')
Bram Moolenaarecd34bf2020-08-04 20:17:31 +02002095endfunc
2096
Bram Moolenaar18a79a62019-10-12 15:36:11 +02002097" This checks the CSI sequences when in modifyOtherKeys mode.
2098" The mode doesn't need to be enabled, the codes are always detected.
2099func RunTest_modifyOtherKeys(func)
2100 new
Bram Moolenaar459fd782019-10-13 16:43:39 +02002101 set timeoutlen=10
Bram Moolenaar18a79a62019-10-12 15:36:11 +02002102
Bram Moolenaarf19f8d12019-12-18 19:36:23 +01002103 " Shift-X is sent as 'X' with the shift modifier
Bram Moolenaar18a79a62019-10-12 15:36:11 +02002104 call feedkeys('a' .. a:func('X', 2) .. "\<Esc>", 'Lx!')
2105 call assert_equal('X', getline(1))
2106
2107 " Ctrl-i is Tab
2108 call setline(1, '')
2109 call feedkeys('a' .. a:func('i', 5) .. "\<Esc>", 'Lx!')
2110 call assert_equal("\t", getline(1))
2111
2112 " Ctrl-I is also Tab
2113 call setline(1, '')
2114 call feedkeys('a' .. a:func('I', 5) .. "\<Esc>", 'Lx!')
2115 call assert_equal("\t", getline(1))
2116
2117 " Alt-x is ø
2118 call setline(1, '')
2119 call feedkeys('a' .. a:func('x', 3) .. "\<Esc>", 'Lx!')
2120 call assert_equal("ø", getline(1))
2121
2122 " Meta-x is also ø
2123 call setline(1, '')
2124 call feedkeys('a' .. a:func('x', 9) .. "\<Esc>", 'Lx!')
2125 call assert_equal("ø", getline(1))
2126
2127 " Alt-X is Ø
2128 call setline(1, '')
2129 call feedkeys('a' .. a:func('X', 3) .. "\<Esc>", 'Lx!')
2130 call assert_equal("Ø", getline(1))
2131
2132 " Meta-X is ø
2133 call setline(1, '')
2134 call feedkeys('a' .. a:func('X', 9) .. "\<Esc>", 'Lx!')
2135 call assert_equal("Ø", getline(1))
2136
Bram Moolenaar828ffd52019-11-21 23:24:00 +01002137 " Ctrl-6 is Ctrl-^
2138 split aaa
2139 edit bbb
2140 call feedkeys(a:func('6', 5), 'Lx!')
2141 call assert_equal("aaa", bufname())
2142 bwipe aaa
2143 bwipe bbb
2144
Bram Moolenaar0684e362020-12-03 19:54:42 +01002145 " Ctrl-V X 33 is 3
2146 call setline(1, '')
2147 call feedkeys("a\<C-V>" .. a:func('X', 2) .. "33\<Esc>", 'Lx!')
2148 call assert_equal("3", getline(1))
2149
2150 " Ctrl-V U 12345 is Unicode 12345
2151 call setline(1, '')
2152 call feedkeys("a\<C-V>" .. a:func('U', 2) .. "12345\<Esc>", 'Lx!')
2153 call assert_equal("\U12345", getline(1))
2154
Bram Moolenaar18a79a62019-10-12 15:36:11 +02002155 bwipe!
2156 set timeoutlen&
2157endfunc
2158
Bram Moolenaar459fd782019-10-13 16:43:39 +02002159func Test_modifyOtherKeys_basic()
Bram Moolenaar18a79a62019-10-12 15:36:11 +02002160 call RunTest_modifyOtherKeys(function('GetEscCodeCSI27'))
Bram Moolenaar18a79a62019-10-12 15:36:11 +02002161 call RunTest_modifyOtherKeys(function('GetEscCodeCSIu'))
2162endfunc
Bram Moolenaard1e2f392019-10-12 18:22:50 +02002163
Bram Moolenaar38571a02019-11-26 14:28:15 +01002164func Test_modifyOtherKeys_no_mapping()
2165 set timeoutlen=10
2166
2167 let @a = 'aaa'
2168 call feedkeys(":let x = '" .. GetEscCodeCSI27('R', 5) .. GetEscCodeCSI27('R', 5) .. "a'\<CR>", 'Lx!')
2169 call assert_equal("let x = 'aaa'", @:)
2170
2171 new
2172 call feedkeys("a" .. GetEscCodeCSI27('R', 5) .. GetEscCodeCSI27('R', 5) .. "a\<Esc>", 'Lx!')
2173 call assert_equal("aaa", getline(1))
2174 bwipe!
2175
2176 new
2177 call feedkeys("axx\<CR>yy" .. GetEscCodeCSI27('G', 5) .. GetEscCodeCSI27('K', 5) .. "a\<Esc>", 'Lx!')
2178 call assert_equal("axx", getline(1))
2179 call assert_equal("yy", getline(2))
2180 bwipe!
2181
2182 set timeoutlen&
2183endfunc
2184
Trygve Aabergeb9c09c12022-10-14 12:08:24 +01002185func Test_CSIu_keys_without_modifiers()
Bram Moolenaarebed1b02022-11-24 14:05:19 +00002186 " make this execute faster
2187 set timeoutlen=10
2188
Bram Moolenaarc255b782022-11-26 19:16:48 +00002189 call WaitForResponses()
2190
Trygve Aabergeb9c09c12022-10-14 12:08:24 +01002191 " Escape sent as `CSI 27 u` should act as normal escape and not undo
2192 call setline(1, 'a')
2193 call feedkeys('a' .. GetEscCodeCSIuWithoutModifier("\e"), 'Lx!')
2194 call assert_equal('n', mode())
2195 call assert_equal('a', getline(1))
2196
2197 " Tab sent as `CSI 9 u` should work
2198 call setline(1, '')
2199 call feedkeys('a' .. GetEscCodeCSIuWithoutModifier("\t") .. "\<Esc>", 'Lx!')
2200 call assert_equal("\t", getline(1))
Bram Moolenaarebed1b02022-11-24 14:05:19 +00002201
2202 set timeoutlen&
Trygve Aabergeb9c09c12022-10-14 12:08:24 +01002203endfunc
2204
Bram Moolenaarc14b57c2021-12-03 13:20:29 +00002205" Check that when DEC mouse codes are recognized a special key is handled.
2206func Test_ignore_dec_mouse()
Dominique Pellef589fd32021-12-05 12:39:21 +00002207 silent !infocmp gnome >/dev/null 2>&1
2208 if v:shell_error != 0
2209 throw 'Skipped: gnome entry missing in the terminfo db'
2210 endif
Bram Moolenaarc14b57c2021-12-03 13:20:29 +00002211
2212 new
2213 let save_mouse = &mouse
2214 let save_term = &term
2215 let save_ttymouse = &ttymouse
2216 call test_override('no_query_mouse', 1)
2217 set mouse=a term=gnome ttymouse=
Bram Moolenaarc255b782022-11-26 19:16:48 +00002218 call WaitForResponses()
Bram Moolenaarc14b57c2021-12-03 13:20:29 +00002219
2220 execute "set <xF1>=\<Esc>[1;*P"
2221 nnoremap <S-F1> agot it<Esc>
2222 call feedkeys("\<Esc>[1;2P", 'Lx!')
2223 call assert_equal('got it', getline(1))
2224
2225 let &mouse = save_mouse
2226 let &term = save_term
2227 let &ttymouse = save_ttymouse
2228 call test_override('no_query_mouse', 0)
2229 bwipe!
2230endfunc
2231
Bram Moolenaard1e2f392019-10-12 18:22:50 +02002232func RunTest_mapping_shift(key, func)
2233 call setline(1, '')
2234 if a:key == '|'
2235 exe 'inoremap \| xyz'
2236 else
2237 exe 'inoremap ' .. a:key .. ' xyz'
2238 endif
2239 call feedkeys('a' .. a:func(a:key, 2) .. "\<Esc>", 'Lx!')
2240 call assert_equal("xyz", getline(1))
2241 if a:key == '|'
2242 exe 'iunmap \|'
2243 else
2244 exe 'iunmap ' .. a:key
2245 endif
2246endfunc
2247
Bram Moolenaar975a8802020-06-06 22:36:24 +02002248func Test_modifyOtherKeys_mapped()
2249 set timeoutlen=10
2250 imap ' <C-W>
2251 imap <C-W><C-A> c-a
2252 call setline(1, '')
2253
2254 " single quote is turned into single byte CTRL-W
2255 " CTRL-A is added with a separate modifier, and needs to be simplified before
2256 " the mapping can match.
2257 call feedkeys("a'" .. GetEscCodeCSI27('A', 5) .. "\<Esc>", 'Lx!')
2258 call assert_equal('c-a', getline(1))
2259
2260 iunmap '
2261 iunmap <C-W><C-A>
2262 set timeoutlen&
2263endfunc
2264
Bram Moolenaar196c3852022-03-04 19:22:36 +00002265func Test_modifyOtherKeys_ambiguous_mapping()
2266 new
2267 set timeoutlen=10
2268 map <C-J> a
2269 map <C-J>x <Nop>
2270 call setline(1, 'x')
2271
2272 " CTRL-J b should have trigger the <C-J> mapping and then insert "b"
2273 call feedkeys(GetEscCodeCSI27('J', 5) .. "b\<Esc>", 'Lx!')
2274 call assert_equal('xb', getline(1))
2275
2276 unmap <C-J>
2277 unmap <C-J>x
Bram Moolenaarf35fd8e2022-03-18 15:41:17 +00002278
2279 " if a special character is following there should be a check for a termcode
2280 nnoremap s aX<Esc>
2281 nnoremap s<BS> aY<Esc>
2282 set t_kb=
2283 call setline(1, 'x')
2284 call feedkeys("s\x08", 'Lx!')
2285 call assert_equal('xY', getline(1))
2286
Bram Moolenaar196c3852022-03-04 19:22:36 +00002287 set timeoutlen&
2288 bwipe!
2289endfunc
2290
Bram Moolenaar749bc952020-10-31 16:33:47 +01002291" Whether Shift-Tab sends "ESC [ Z" or "ESC [ 27 ; 2 ; 9 ~" is unpredictable,
2292" both should work.
2293func Test_modifyOtherKeys_shift_tab()
2294 set timeoutlen=10
2295
2296 call setline(1, '')
2297 call feedkeys("a\<C-K>" .. GetEscCodeCSI27("\t", '2') .. "\<Esc>", 'Lx!')
2298 eval getline(1)->assert_equal('<S-Tab>')
2299
2300 call setline(1, '')
2301 call feedkeys("a\<C-K>\<Esc>[Z\<Esc>", 'Lx!')
2302 eval getline(1)->assert_equal('<S-Tab>')
2303
2304 set timeoutlen&
2305 bwipe!
2306endfunc
2307
Bram Moolenaard1e2f392019-10-12 18:22:50 +02002308func RunTest_mapping_works_with_shift(func)
2309 new
Bram Moolenaar459fd782019-10-13 16:43:39 +02002310 set timeoutlen=10
Bram Moolenaard1e2f392019-10-12 18:22:50 +02002311
2312 call RunTest_mapping_shift('@', a:func)
2313 call RunTest_mapping_shift('A', a:func)
2314 call RunTest_mapping_shift('Z', a:func)
2315 call RunTest_mapping_shift('^', a:func)
2316 call RunTest_mapping_shift('_', a:func)
2317 call RunTest_mapping_shift('{', a:func)
2318 call RunTest_mapping_shift('|', a:func)
2319 call RunTest_mapping_shift('}', a:func)
2320 call RunTest_mapping_shift('~', a:func)
2321
2322 bwipe!
2323 set timeoutlen&
2324endfunc
2325
Bram Moolenaar459fd782019-10-13 16:43:39 +02002326func Test_mapping_works_with_shift_plain()
Bram Moolenaard1e2f392019-10-12 18:22:50 +02002327 call RunTest_mapping_works_with_shift(function('GetEscCodeCSI27'))
2328 call RunTest_mapping_works_with_shift(function('GetEscCodeCSIu'))
2329endfunc
Bram Moolenaar459fd782019-10-13 16:43:39 +02002330
2331func RunTest_mapping_mods(map, key, func, code)
2332 call setline(1, '')
2333 exe 'inoremap ' .. a:map .. ' xyz'
2334 call feedkeys('a' .. a:func(a:key, a:code) .. "\<Esc>", 'Lx!')
2335 call assert_equal("xyz", getline(1))
2336 exe 'iunmap ' .. a:map
2337endfunc
2338
2339func RunTest_mapping_works_with_mods(func, mods, code)
2340 new
2341 set timeoutlen=10
2342
2343 if a:mods !~ 'S'
2344 " Shift by itself has no effect
2345 call RunTest_mapping_mods('<' .. a:mods .. '-@>', '@', a:func, a:code)
2346 endif
2347 call RunTest_mapping_mods('<' .. a:mods .. '-A>', 'A', a:func, a:code)
2348 call RunTest_mapping_mods('<' .. a:mods .. '-Z>', 'Z', a:func, a:code)
2349 if a:mods !~ 'S'
2350 " with Shift code is always upper case
2351 call RunTest_mapping_mods('<' .. a:mods .. '-a>', 'a', a:func, a:code)
2352 call RunTest_mapping_mods('<' .. a:mods .. '-z>', 'z', a:func, a:code)
2353 endif
2354 if a:mods != 'A'
2355 " with Alt code is not in upper case
2356 call RunTest_mapping_mods('<' .. a:mods .. '-a>', 'A', a:func, a:code)
2357 call RunTest_mapping_mods('<' .. a:mods .. '-z>', 'Z', a:func, a:code)
2358 endif
2359 call RunTest_mapping_mods('<' .. a:mods .. '-á>', 'á', a:func, a:code)
2360 if a:mods !~ 'S'
2361 " Shift by itself has no effect
2362 call RunTest_mapping_mods('<' .. a:mods .. '-^>', '^', a:func, a:code)
2363 call RunTest_mapping_mods('<' .. a:mods .. '-_>', '_', a:func, a:code)
2364 call RunTest_mapping_mods('<' .. a:mods .. '-{>', '{', a:func, a:code)
2365 call RunTest_mapping_mods('<' .. a:mods .. '-\|>', '|', a:func, a:code)
2366 call RunTest_mapping_mods('<' .. a:mods .. '-}>', '}', a:func, a:code)
2367 call RunTest_mapping_mods('<' .. a:mods .. '-~>', '~', a:func, a:code)
2368 endif
2369
2370 bwipe!
2371 set timeoutlen&
2372endfunc
2373
2374func Test_mapping_works_with_shift()
2375 call RunTest_mapping_works_with_mods(function('GetEscCodeCSI27'), 'S', 2)
2376 call RunTest_mapping_works_with_mods(function('GetEscCodeCSIu'), 'S', 2)
2377endfunc
Bram Moolenaarf19f8d12019-12-18 19:36:23 +01002378
Bram Moolenaar459fd782019-10-13 16:43:39 +02002379func Test_mapping_works_with_ctrl()
2380 call RunTest_mapping_works_with_mods(function('GetEscCodeCSI27'), 'C', 5)
2381 call RunTest_mapping_works_with_mods(function('GetEscCodeCSIu'), 'C', 5)
Bram Moolenaar4e2114e2020-10-07 16:12:37 +02002382
2383 new
2384 set timeoutlen=10
2385
2386 " CTRL-@ actually produces the code for CTRL-2, which is converted
2387 call RunTest_mapping_mods('<C-@>', '2', function('GetEscCodeCSI27'), 5)
2388 call RunTest_mapping_mods('<C-@>', '2', function('GetEscCodeCSIu'), 5)
2389
2390 " CTRL-^ actually produces the code for CTRL-6, which is converted
2391 call RunTest_mapping_mods('<C-^>', '6', function('GetEscCodeCSI27'), 5)
2392 call RunTest_mapping_mods('<C-^>', '6', function('GetEscCodeCSIu'), 5)
2393
2394 " CTRL-_ actually produces the code for CTRL--, which is converted
2395 call RunTest_mapping_mods('<C-_>', '-', function('GetEscCodeCSI27'), 5)
2396 call RunTest_mapping_mods('<C-_>', '-', function('GetEscCodeCSIu'), 5)
2397
2398 bwipe!
2399 set timeoutlen&
Bram Moolenaar459fd782019-10-13 16:43:39 +02002400endfunc
2401
2402func Test_mapping_works_with_shift_ctrl()
2403 call RunTest_mapping_works_with_mods(function('GetEscCodeCSI27'), 'C-S', 6)
2404 call RunTest_mapping_works_with_mods(function('GetEscCodeCSIu'), 'C-S', 6)
Bram Moolenaar9a033d72020-10-07 17:29:48 +02002405
2406 new
2407 set timeoutlen=10
2408
2409 " Ctrl-Shift-[ actually produces CTRL-Shift-{ which is mapped as <C-{>
2410 call RunTest_mapping_mods('<C-{>', '{', function('GetEscCodeCSI27'), 6)
2411 call RunTest_mapping_mods('<C-{>', '{', function('GetEscCodeCSIu'), 6)
2412
2413 " Ctrl-Shift-] actually produces CTRL-Shift-} which is mapped as <C-}>
2414 call RunTest_mapping_mods('<C-{>', '{', function('GetEscCodeCSI27'), 6)
2415 call RunTest_mapping_mods('<C-{>', '{', function('GetEscCodeCSIu'), 6)
2416
2417 " Ctrl-Shift-\ actually produces CTRL-Shift-| which is mapped as <C-|>
2418 call RunTest_mapping_mods('<C-\|>', '|', function('GetEscCodeCSI27'), 6)
2419 call RunTest_mapping_mods('<C-\|>', '|', function('GetEscCodeCSIu'), 6)
2420
2421 bwipe!
2422 set timeoutlen&
Bram Moolenaar459fd782019-10-13 16:43:39 +02002423endfunc
2424
2425" Below we also test the "u" code with Alt, This works, but libvterm would not
2426" send the Alt key like this but by prefixing an Esc.
Bram Moolenaarf19f8d12019-12-18 19:36:23 +01002427
Bram Moolenaar459fd782019-10-13 16:43:39 +02002428func Test_mapping_works_with_alt()
2429 call RunTest_mapping_works_with_mods(function('GetEscCodeCSI27'), 'A', 3)
2430 call RunTest_mapping_works_with_mods(function('GetEscCodeCSIu'), 'A', 3)
2431endfunc
2432
2433func Test_mapping_works_with_shift_alt()
2434 call RunTest_mapping_works_with_mods(function('GetEscCodeCSI27'), 'S-A', 4)
2435 call RunTest_mapping_works_with_mods(function('GetEscCodeCSIu'), 'S-A', 4)
2436endfunc
2437
Bram Moolenaardaff0fb2020-09-27 13:16:46 +02002438func Test_mapping_works_with_alt_and_shift()
2439 new
2440 set timeoutlen=10
2441
2442 " mapping <A-?> works even though the code is A-S-?
2443 for c in ['!', '$', '+', ':', '?', '^', '~']
2444 call RunTest_mapping_mods('<A-' .. c .. '>', c, function('GetEscCodeCSI27'), 4)
2445 call RunTest_mapping_mods('<A-' .. c .. '>', c, function('GetEscCodeCSIu'), 4)
2446 endfor
2447
2448 bwipe!
2449 set timeoutlen&
2450endfunc
2451
Bram Moolenaar459fd782019-10-13 16:43:39 +02002452func Test_mapping_works_with_ctrl_alt()
2453 call RunTest_mapping_works_with_mods(function('GetEscCodeCSI27'), 'C-A', 7)
2454 call RunTest_mapping_works_with_mods(function('GetEscCodeCSIu'), 'C-A', 7)
2455endfunc
2456
2457func Test_mapping_works_with_shift_ctrl_alt()
2458 call RunTest_mapping_works_with_mods(function('GetEscCodeCSI27'), 'C-S-A', 8)
2459 call RunTest_mapping_works_with_mods(function('GetEscCodeCSIu'), 'C-S-A', 8)
2460endfunc
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01002461
Bram Moolenaar064fd672022-11-29 18:32:32 +00002462func Test_mapping_works_with_unknown_modifiers()
2463 new
2464 set timeoutlen=10
2465
2466 for Func in [function('GetEscCodeCSI27'), function('GetEscCodeCSIu')]
2467 call RunTest_mapping_mods('<C-z>', 'z', Func, 5)
2468 " Add 16, 32, 64 or 128 for modifiers we currently don't support.
2469 call RunTest_mapping_mods('<C-z>', 'z', Func, 5 + 16)
2470 call RunTest_mapping_mods('<C-z>', 'z', Func, 5 + 32)
2471 call RunTest_mapping_mods('<C-z>', 'z', Func, 5 + 64)
2472 call RunTest_mapping_mods('<C-z>', 'z', Func, 5 + 128)
2473
2474 call RunTest_mapping_mods('<S-X>', 'X', Func, 2)
2475 " Add 16, 32, 64 or 128 for modifiers we currently don't support.
2476 call RunTest_mapping_mods('<S-X>', 'X', Func, 2 + 16)
2477 call RunTest_mapping_mods('<S-X>', 'X', Func, 2 + 32)
2478 call RunTest_mapping_mods('<S-X>', 'X', Func, 2 + 64)
2479 call RunTest_mapping_mods('<S-X>', 'X', Func, 2 + 128)
2480 endfor
2481
2482 bwipe!
2483 set timeoutlen&
2484endfunc
2485
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01002486func Test_insert_literal()
2487 set timeoutlen=10
Bram Moolenaarc255b782022-11-26 19:16:48 +00002488
2489 call WaitForResponses()
2490
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01002491 new
2492 " CTRL-V CTRL-X inserts a ^X
2493 call feedkeys('a' .. GetEscCodeCSIu('V', '5') .. GetEscCodeCSIu('X', '5') .. "\<Esc>", 'Lx!')
2494 call assert_equal("\<C-X>", getline(1))
2495
2496 call setline(1, '')
2497 call feedkeys('a' .. GetEscCodeCSI27('V', '5') .. GetEscCodeCSI27('X', '5') .. "\<Esc>", 'Lx!')
2498 call assert_equal("\<C-X>", getline(1))
2499
Bram Moolenaarf19f8d12019-12-18 19:36:23 +01002500 " CTRL-SHIFT-V CTRL-X inserts escape sequence
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01002501 call setline(1, '')
2502 call feedkeys('a' .. GetEscCodeCSIu('V', '6') .. GetEscCodeCSIu('X', '5') .. "\<Esc>", 'Lx!')
2503 call assert_equal("\<Esc>[88;5u", getline(1))
2504
2505 call setline(1, '')
2506 call feedkeys('a' .. GetEscCodeCSI27('V', '6') .. GetEscCodeCSI27('X', '5') .. "\<Esc>", 'Lx!')
2507 call assert_equal("\<Esc>[27;5;88~", getline(1))
2508
2509 bwipe!
2510 set timeoutlen&
2511endfunc
2512
2513func Test_cmdline_literal()
2514 set timeoutlen=10
2515
2516 " CTRL-V CTRL-Y inserts a ^Y
2517 call feedkeys(':' .. GetEscCodeCSIu('V', '5') .. GetEscCodeCSIu('Y', '5') .. "\<C-B>\"\<CR>", 'Lx!')
2518 call assert_equal("\"\<C-Y>", @:)
2519
2520 call feedkeys(':' .. GetEscCodeCSI27('V', '5') .. GetEscCodeCSI27('Y', '5') .. "\<C-B>\"\<CR>", 'Lx!')
2521 call assert_equal("\"\<C-Y>", @:)
2522
Bram Moolenaarf19f8d12019-12-18 19:36:23 +01002523 " CTRL-SHIFT-V CTRL-Y inserts escape sequence
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01002524 call feedkeys(':' .. GetEscCodeCSIu('V', '6') .. GetEscCodeCSIu('Y', '5') .. "\<C-B>\"\<CR>", 'Lx!')
2525 call assert_equal("\"\<Esc>[89;5u", @:)
2526
2527 call setline(1, '')
2528 call feedkeys(':' .. GetEscCodeCSI27('V', '6') .. GetEscCodeCSI27('Y', '5') .. "\<C-B>\"\<CR>", 'Lx!')
2529 call assert_equal("\"\<Esc>[27;5;89~", @:)
2530
2531 set timeoutlen&
2532endfunc
Bram Moolenaar0eabd4d2020-03-15 16:13:53 +01002533
Bram Moolenaarbbf84e22022-03-12 13:48:39 +00002534func Test_mapping_esc()
2535 set timeoutlen=10
2536
2537 new
2538 nnoremap <Up> iHello<Esc>
2539 nnoremap <Esc> <Nop>
2540
2541 call feedkeys(substitute(&t_ku, '\*', '', 'g'), 'Lx!')
2542 call assert_equal("Hello", getline(1))
2543
2544 bwipe!
2545 nunmap <Up>
2546 nunmap <Esc>
2547 set timeoutlen&
2548endfunc
2549
Bram Moolenaar0ff5ded2020-05-07 18:43:44 +02002550" Test for translation of special key codes (<xF1>, <xF2>, etc.)
Bram Moolenaar92e5df82021-01-30 15:39:47 +01002551func Test_Keycode_Translation()
Bram Moolenaar0ff5ded2020-05-07 18:43:44 +02002552 let keycodes = [
2553 \ ["<xUp>", "<Up>"],
2554 \ ["<xDown>", "<Down>"],
2555 \ ["<xLeft>", "<Left>"],
2556 \ ["<xRight>", "<Right>"],
2557 \ ["<xHome>", "<Home>"],
2558 \ ["<xEnd>", "<End>"],
2559 \ ["<zHome>", "<Home>"],
2560 \ ["<zEnd>", "<End>"],
2561 \ ["<xF1>", "<F1>"],
2562 \ ["<xF2>", "<F2>"],
2563 \ ["<xF3>", "<F3>"],
2564 \ ["<xF4>", "<F4>"],
2565 \ ["<S-xF1>", "<S-F1>"],
2566 \ ["<S-xF2>", "<S-F2>"],
2567 \ ["<S-xF3>", "<S-F3>"],
2568 \ ["<S-xF4>", "<S-F4>"]]
2569 for [k1, k2] in keycodes
2570 exe "nnoremap " .. k1 .. " 2wx"
2571 call assert_true(maparg(k1, 'n', 0, 1).lhs == k2)
2572 exe "nunmap " .. k1
2573 endfor
2574endfunc
2575
Bram Moolenaar1f448d92021-03-22 19:37:06 +01002576" Test for terminal keycodes that doesn't have termcap entries
2577func Test_special_term_keycodes()
2578 new
2579 " Test for <xHome>, <S-xHome> and <C-xHome>
2580 " send <K_SPECIAL> <KS_EXTRA> keycode
2581 call feedkeys("i\<C-K>\x80\xfd\x3f\n", 'xt')
2582 " send <K_SPECIAL> <KS_MODIFIER> bitmap <K_SPECIAL> <KS_EXTRA> keycode
2583 call feedkeys("i\<C-K>\x80\xfc\x2\x80\xfd\x3f\n", 'xt')
2584 call feedkeys("i\<C-K>\x80\xfc\x4\x80\xfd\x3f\n", 'xt')
2585 " Test for <xEnd>, <S-xEnd> and <C-xEnd>
2586 call feedkeys("i\<C-K>\x80\xfd\x3d\n", 'xt')
2587 call feedkeys("i\<C-K>\x80\xfc\x2\x80\xfd\x3d\n", 'xt')
2588 call feedkeys("i\<C-K>\x80\xfc\x4\x80\xfd\x3d\n", 'xt')
2589 " Test for <zHome>, <S-zHome> and <C-zHome>
2590 call feedkeys("i\<C-K>\x80\xfd\x40\n", 'xt')
2591 call feedkeys("i\<C-K>\x80\xfc\x2\x80\xfd\x40\n", 'xt')
2592 call feedkeys("i\<C-K>\x80\xfc\x4\x80\xfd\x40\n", 'xt')
2593 " Test for <zEnd>, <S-zEnd> and <C-zEnd>
2594 call feedkeys("i\<C-K>\x80\xfd\x3e\n", 'xt')
2595 call feedkeys("i\<C-K>\x80\xfc\x2\x80\xfd\x3e\n", 'xt')
2596 call feedkeys("i\<C-K>\x80\xfc\x4\x80\xfd\x3e\n", 'xt')
2597 " Test for <xUp>, <xDown>, <xLeft> and <xRight>
2598 call feedkeys("i\<C-K>\x80\xfd\x41\n", 'xt')
2599 call feedkeys("i\<C-K>\x80\xfd\x42\n", 'xt')
2600 call feedkeys("i\<C-K>\x80\xfd\x43\n", 'xt')
2601 call feedkeys("i\<C-K>\x80\xfd\x44\n", 'xt')
2602 call assert_equal(['<Home>', '<S-Home>', '<C-Home>',
2603 \ '<End>', '<S-End>', '<C-End>',
2604 \ '<Home>', '<S-Home>', '<C-Home>',
2605 \ '<End>', '<S-End>', '<C-End>',
2606 \ '<Up>', '<Down>', '<Left>', '<Right>', ''], getline(1, '$'))
2607 bw!
2608endfunc
2609
Bram Moolenaar1573e732022-11-16 20:33:21 +00002610func Test_home_key_works()
2611 " The '@' character in K_HOME must only match "1" when followed by ";",
2612 " otherwise this code for Home is not recognized: "<Esc>[1~"
Bram Moolenaar1d8765d2022-11-17 16:43:35 +00002613 " Set termcap values like "xterm" uses them. Except using F2 for xHome,
2614 " because that termcap entry can't be set here.
2615 let save_K1 = exists('&t_K1') ? &t_K1 : ''
2616 let save_kh = exists('&t_kh') ? &t_kh : ''
2617 let save_k2 = exists('&t_k2') ? &t_k2 : ''
2618 let save_k3 = exists('&t_k3') ? &t_k3 : ''
2619 let save_end = exists('&t_@7') ? &t_@7 : ''
2620
Bram Moolenaar1573e732022-11-16 20:33:21 +00002621 let &t_K1 = "\<Esc>[1;*~" " <kHome>
2622 let &t_kh = "\<Esc>[@;*H" " <Home>
Bram Moolenaar1d8765d2022-11-17 16:43:35 +00002623 let &t_k2 = "\<Esc>O*H" " use <F2> for <xHome>
Bram Moolenaar1573e732022-11-16 20:33:21 +00002624 let &t_k3 = "\<Esc>[7;*~" " use <F3> for <zHome>
2625 let &t_@7 = "\<Esc>[@;*F" " <End>
2626
2627 new
2628 call feedkeys("i\<C-K>\<Esc>OH\n\<Esc>", 'tx')
2629 call feedkeys("i\<C-K>\<Esc>[1~\n\<Esc>", 'tx')
2630 call assert_equal([
Bram Moolenaar1d8765d2022-11-17 16:43:35 +00002631 \ '<F2>',
Bram Moolenaar1573e732022-11-16 20:33:21 +00002632 \ '<kHome>',
2633 \ ''], getline(1, '$'))
2634
2635 bwipe!
Bram Moolenaar1573e732022-11-16 20:33:21 +00002636 let &t_K1 = save_K1
Bram Moolenaar1d8765d2022-11-17 16:43:35 +00002637 let &t_kh = save_kh
Bram Moolenaar1573e732022-11-16 20:33:21 +00002638 let &t_k2 = save_k2
2639 let &t_k3 = save_k3
2640 let &t_@7 = save_end
2641endfunc
2642
Bram Moolenaar0f5575d2021-07-30 21:18:03 +02002643func Test_terminal_builtin_without_gui()
2644 CheckNotMSWindows
2645
2646 " builtin_gui should not be output by :set term=xxx
2647 let output = systemlist("TERM=dumb " .. v:progpath .. " --clean -c ':set t_ti= t_te=' -c 'set term=xxx' -c ':q!'")
2648 redraw!
2649 call map(output, {_, val -> trim(val)})
2650 call assert_equal(-1, index(output, 'builtin_gui'))
2651 call assert_notequal(-1, index(output, 'builtin_dumb'))
2652endfunc
2653
2654
Bram Moolenaar0eabd4d2020-03-15 16:13:53 +01002655" vim: shiftwidth=2 sts=2 expandtab