blob: 1a10d62577c75ffd54ad03ca8202c5096f07d41f [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
9
Bram Moolenaard0621d82019-05-02 21:12:19 +020010" xterm2 and sgr always work, urxvt is optional.
Bram Moolenaar92fd5992019-05-02 23:00:22 +020011let s:ttymouse_values = ['xterm2', 'sgr']
Bram Moolenaard0621d82019-05-02 21:12:19 +020012if has('mouse_urxvt')
Bram Moolenaar92fd5992019-05-02 23:00:22 +020013 call add(s:ttymouse_values, 'urxvt')
14endif
15
16" dec doesn't support all the functionality
17if has('mouse_dec')
18 let s:ttymouse_dec = ['dec']
19else
20 let s:ttymouse_dec = []
Bram Moolenaard0621d82019-05-02 21:12:19 +020021endif
22
Bram Moolenaard7885432019-05-03 13:44:10 +020023" netterm only supports left click
24if has('mouse_netterm')
25 let s:ttymouse_netterm = ['netterm']
26else
27 let s:ttymouse_netterm = []
28endif
29
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +020030" Helper function to emit a terminal escape code.
Bram Moolenaard0621d82019-05-02 21:12:19 +020031func TerminalEscapeCode(code, row, col, m)
Bram Moolenaar2b00b9b2019-04-17 17:08:27 +020032 if &ttymouse ==# 'xterm2'
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +020033 " need to use byte encoding here.
Bram Moolenaard0621d82019-05-02 21:12:19 +020034 let str = list2str([a:code + 0x20, a:col + 0x20, a:row + 0x20])
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +020035 if has('iconv')
Bram Moolenaarf9f24ce2019-08-31 21:17:39 +020036 let bytes = str->iconv('utf-8', 'latin1')
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +020037 else
38 " Hopefully the numbers are not too big.
39 let bytes = str
40 endif
41 call feedkeys("\<Esc>[M" .. bytes, 'Lx!')
42 elseif &ttymouse ==# 'sgr'
Bram Moolenaard0621d82019-05-02 21:12:19 +020043 call feedkeys(printf("\<Esc>[<%d;%d;%d%s", a:code, a:col, a:row, a:m), 'Lx!')
44 elseif &ttymouse ==# 'urxvt'
45 call feedkeys(printf("\<Esc>[%d;%d;%dM", a:code + 0x20, a:col, a:row), 'Lx!')
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +020046 endif
47endfunc
48
Bram Moolenaar92fd5992019-05-02 23:00:22 +020049func DecEscapeCode(code, down, row, col)
50 call feedkeys(printf("\<Esc>[%d;%d;%d;%d&w", a:code, a:down, a:row, a:col), 'Lx!')
51endfunc
52
Bram Moolenaard7885432019-05-03 13:44:10 +020053func NettermEscapeCode(row, col)
54 call feedkeys(printf("\<Esc>}%d,%d\r", a:row, a:col), 'Lx!')
55endfunc
56
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +020057func MouseLeftClick(row, col)
Bram Moolenaar92fd5992019-05-02 23:00:22 +020058 if &ttymouse ==# 'dec'
59 call DecEscapeCode(2, 4, a:row, a:col)
Bram Moolenaard7885432019-05-03 13:44:10 +020060 elseif &ttymouse ==# 'netterm'
61 call NettermEscapeCode(a:row, a:col)
Bram Moolenaar92fd5992019-05-02 23:00:22 +020062 else
63 call TerminalEscapeCode(0, a:row, a:col, 'M')
64 endif
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +020065endfunc
66
Bram Moolenaarc1b81602019-04-27 19:11:35 +020067func MouseMiddleClick(row, col)
Bram Moolenaar92fd5992019-05-02 23:00:22 +020068 if &ttymouse ==# 'dec'
69 call DecEscapeCode(4, 2, a:row, a:col)
70 else
71 call TerminalEscapeCode(1, a:row, a:col, 'M')
72 endif
Bram Moolenaarc1b81602019-04-27 19:11:35 +020073endfunc
74
Bram Moolenaar6aa75232019-10-13 21:01:34 +020075func MouseRightClick(row, col)
76 if &ttymouse ==# 'dec'
77 call DecEscapeCode(6, 1, a:row, a:col)
78 else
79 call TerminalEscapeCode(2, a:row, a:col, 'M')
80 endif
81endfunc
82
Bram Moolenaar1ee36d62019-05-01 23:13:56 +020083func MouseCtrlLeftClick(row, col)
84 let ctrl = 0x10
Bram Moolenaard0621d82019-05-02 21:12:19 +020085 call TerminalEscapeCode(0 + ctrl, a:row, a:col, 'M')
Bram Moolenaar1ee36d62019-05-01 23:13:56 +020086endfunc
87
88func MouseCtrlRightClick(row, col)
89 let ctrl = 0x10
Bram Moolenaard0621d82019-05-02 21:12:19 +020090 call TerminalEscapeCode(2 + ctrl, a:row, a:col, 'M')
Bram Moolenaar1ee36d62019-05-01 23:13:56 +020091endfunc
92
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +020093func MouseLeftRelease(row, col)
Bram Moolenaar92fd5992019-05-02 23:00:22 +020094 if &ttymouse ==# 'dec'
95 call DecEscapeCode(3, 0, a:row, a:col)
Bram Moolenaard7885432019-05-03 13:44:10 +020096 elseif &ttymouse ==# 'netterm'
97 " send nothing
Bram Moolenaar92fd5992019-05-02 23:00:22 +020098 else
99 call TerminalEscapeCode(3, a:row, a:col, 'm')
100 endif
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200101endfunc
102
Bram Moolenaarc1b81602019-04-27 19:11:35 +0200103func MouseMiddleRelease(row, col)
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200104 if &ttymouse ==# 'dec'
105 call DecEscapeCode(5, 0, a:row, a:col)
106 else
107 call TerminalEscapeCode(3, a:row, a:col, 'm')
108 endif
Bram Moolenaarc1b81602019-04-27 19:11:35 +0200109endfunc
110
Bram Moolenaar1ee36d62019-05-01 23:13:56 +0200111func MouseRightRelease(row, col)
Bram Moolenaar6aa75232019-10-13 21:01:34 +0200112 if &ttymouse ==# 'dec'
113 call DecEscapeCode(7, 0, a:row, a:col)
114 else
115 call TerminalEscapeCode(3, a:row, a:col, 'm')
116 endif
Bram Moolenaar1ee36d62019-05-01 23:13:56 +0200117endfunc
118
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200119func MouseLeftDrag(row, col)
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200120 if &ttymouse ==# 'dec'
121 call DecEscapeCode(1, 4, a:row, a:col)
122 else
123 call TerminalEscapeCode(0x20, a:row, a:col, 'M')
124 endif
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200125endfunc
126
127func MouseWheelUp(row, col)
Bram Moolenaard0621d82019-05-02 21:12:19 +0200128 call TerminalEscapeCode(0x40, a:row, a:col, 'M')
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200129endfunc
130
131func MouseWheelDown(row, col)
Bram Moolenaard0621d82019-05-02 21:12:19 +0200132 call TerminalEscapeCode(0x41, a:row, a:col, 'M')
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200133endfunc
134
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200135func Test_term_mouse_left_click()
Bram Moolenaar905dd902019-04-07 14:21:47 +0200136 new
137 let save_mouse = &mouse
138 let save_term = &term
139 let save_ttymouse = &ttymouse
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200140 call test_override('no_query_mouse', 1)
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200141 set mouse=a term=xterm
Bram Moolenaar905dd902019-04-07 14:21:47 +0200142 call setline(1, ['line 1', 'line 2', 'line 3 is a bit longer'])
Bram Moolenaar905dd902019-04-07 14:21:47 +0200143
Bram Moolenaard7885432019-05-03 13:44:10 +0200144 for ttymouse_val in s:ttymouse_values + s:ttymouse_dec + s:ttymouse_netterm
Bram Moolenaar49452192019-04-17 16:27:02 +0200145 let msg = 'ttymouse=' .. ttymouse_val
Bram Moolenaar1ee36d62019-05-01 23:13:56 +0200146 exe 'set ttymouse=' .. ttymouse_val
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200147 go
Bram Moolenaar49452192019-04-17 16:27:02 +0200148 call assert_equal([0, 1, 1, 0], getpos('.'), msg)
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200149 let row = 2
150 let col = 6
151 call MouseLeftClick(row, col)
152 call MouseLeftRelease(row, col)
Bram Moolenaar49452192019-04-17 16:27:02 +0200153 call assert_equal([0, 2, 6, 0], getpos('.'), msg)
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200154 endfor
Bram Moolenaar905dd902019-04-07 14:21:47 +0200155
156 let &mouse = save_mouse
157 let &term = save_term
158 let &ttymouse = save_ttymouse
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200159 call test_override('no_query_mouse', 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +0200160 bwipe!
161endfunc
162
Bram Moolenaar6aa75232019-10-13 21:01:34 +0200163func Test_xterm_mouse_right_click_extends_visual()
164 if has('mac')
165 throw "Skipped: test right click in visual mode does not work on macOs (why?)"
166 endif
167 let save_mouse = &mouse
168 let save_term = &term
169 let save_ttymouse = &ttymouse
170 call test_override('no_query_mouse', 1)
171 set mouse=a term=xterm
172
173 for visual_mode in ["v", "V", "\<C-V>"]
174 for ttymouse_val in s:ttymouse_values + s:ttymouse_dec
175 let msg = 'visual=' .. visual_mode .. ' ttymouse=' .. ttymouse_val
176 exe 'set ttymouse=' .. ttymouse_val
177
178 call setline(1, repeat([repeat('-', 7)], 7))
179 call MouseLeftClick(4, 4)
180 call MouseLeftRelease(4, 4)
181 exe "norm! " .. visual_mode
182
183 " Right click extends top left of visual area.
184 call MouseRightClick(2, 2)
185 call MouseRightRelease(2, 2)
186
187 " Right click extends bottom bottom right of visual area.
188 call MouseRightClick(6, 6)
189 call MouseRightRelease(6, 6)
190 norm! r1gv
191
192 " Right click shrinks top left of visual area.
193 call MouseRightClick(3, 3)
194 call MouseRightRelease(3, 3)
195
196 " Right click shrinks bottom right of visual area.
197 call MouseRightClick(5, 5)
198 call MouseRightRelease(5, 5)
199 norm! r2
200
201 if visual_mode ==# 'v'
202 call assert_equal(['-------',
203 \ '-111111',
204 \ '1122222',
205 \ '2222222',
206 \ '2222211',
207 \ '111111-',
208 \ '-------'], getline(1, '$'), msg)
209 elseif visual_mode ==# 'V'
210 call assert_equal(['-------',
211 \ '1111111',
212 \ '2222222',
213 \ '2222222',
214 \ '2222222',
215 \ '1111111',
216 \ '-------'], getline(1, '$'), msg)
217 else
218 call assert_equal(['-------',
219 \ '-11111-',
220 \ '-12221-',
221 \ '-12221-',
222 \ '-12221-',
223 \ '-11111-',
224 \ '-------'], getline(1, '$'), msg)
225 endif
226 endfor
227 endfor
228
229 let &mouse = save_mouse
230 let &term = save_term
231 let &ttymouse = save_ttymouse
232 call test_override('no_query_mouse', 0)
233 bwipe!
234endfunc
235
Bram Moolenaar1ee36d62019-05-01 23:13:56 +0200236" Test that <C-LeftMouse> jumps to help tag and <C-RightMouse> jumps back.
237func Test_xterm_mouse_ctrl_click()
238 let save_mouse = &mouse
239 let save_term = &term
240 let save_ttymouse = &ttymouse
241 set mouse=a term=xterm
242
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200243 for ttymouse_val in s:ttymouse_values
Bram Moolenaar1ee36d62019-05-01 23:13:56 +0200244 let msg = 'ttymouse=' .. ttymouse_val
245 exe 'set ttymouse=' .. ttymouse_val
246 help
247 /usr_02.txt
248 norm! zt
249 let row = 1
250 let col = 1
251 call MouseCtrlLeftClick(row, col)
252 call MouseLeftRelease(row, col)
253 call assert_match('usr_02.txt$', bufname('%'), msg)
Bram Moolenaarb4367b72019-10-01 14:19:07 +0200254 call assert_equal('*usr_02.txt*', expand('<cWORD>'), msg)
Bram Moolenaar1ee36d62019-05-01 23:13:56 +0200255
256 call MouseCtrlRightClick(row, col)
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200257 call MouseRightRelease(row, col)
Bram Moolenaar1ee36d62019-05-01 23:13:56 +0200258 call assert_match('help.txt$', bufname('%'), msg)
Bram Moolenaarb4367b72019-10-01 14:19:07 +0200259 call assert_equal('|usr_02.txt|', expand('<cWORD>'), msg)
Bram Moolenaar1ee36d62019-05-01 23:13:56 +0200260
261 helpclose
262 endfor
263
264 let &mouse = save_mouse
265 let &term = save_term
266 let &ttymouse = save_ttymouse
267endfunc
268
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200269func Test_term_mouse_middle_click()
Bram Moolenaar52992fe2019-08-12 14:20:33 +0200270 CheckFeature clipboard_working
Bram Moolenaar564344a2019-04-28 13:00:12 +0200271
Bram Moolenaarc1b81602019-04-27 19:11:35 +0200272 new
273 let save_mouse = &mouse
274 let save_term = &term
275 let save_ttymouse = &ttymouse
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200276 call test_override('no_query_mouse', 1)
Bram Moolenaarc1b81602019-04-27 19:11:35 +0200277 let save_quotestar = @*
278 let @* = 'abc'
279 set mouse=a term=xterm
280
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200281 for ttymouse_val in s:ttymouse_values + s:ttymouse_dec
Bram Moolenaarc1b81602019-04-27 19:11:35 +0200282 let msg = 'ttymouse=' .. ttymouse_val
Bram Moolenaar1ee36d62019-05-01 23:13:56 +0200283 exe 'set ttymouse=' .. ttymouse_val
Bram Moolenaarc1b81602019-04-27 19:11:35 +0200284 call setline(1, ['123456789', '123456789'])
285
286 " Middle-click in the middle of the line pastes text where clicked.
287 let row = 1
288 let col = 6
289 call MouseMiddleClick(row, col)
290 call MouseMiddleRelease(row, col)
291 call assert_equal(['12345abc6789', '123456789'], getline(1, '$'), msg)
292
293 " Middle-click beyond end of the line pastes text at the end of the line.
294 let col = 20
295 call MouseMiddleClick(row, col)
296 call MouseMiddleRelease(row, col)
297 call assert_equal(['12345abc6789abc', '123456789'], getline(1, '$'), msg)
298
299 " Middle-click beyond the last line pastes in the last line.
300 let row = 5
301 let col = 3
302 call MouseMiddleClick(row, col)
303 call MouseMiddleRelease(row, col)
304 call assert_equal(['12345abc6789abc', '12abc3456789'], getline(1, '$'), msg)
305 endfor
306
307 let &mouse = save_mouse
308 let &term = save_term
309 let &ttymouse = save_ttymouse
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200310 call test_override('no_query_mouse', 0)
Bram Moolenaarc1b81602019-04-27 19:11:35 +0200311 let @* = save_quotestar
312 bwipe!
313endfunc
314
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200315" TODO: for unclear reasons this test fails if it comes after
316" Test_xterm_mouse_ctrl_click()
317func Test_1xterm_mouse_wheel()
Bram Moolenaar049736f2019-04-07 21:55:07 +0200318 new
319 let save_mouse = &mouse
320 let save_term = &term
321 let save_ttymouse = &ttymouse
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200322 set mouse=a term=xterm
Bram Moolenaar049736f2019-04-07 21:55:07 +0200323 call setline(1, range(1, 100))
324
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200325 for ttymouse_val in s:ttymouse_values
Bram Moolenaar49452192019-04-17 16:27:02 +0200326 let msg = 'ttymouse=' .. ttymouse_val
Bram Moolenaar1ee36d62019-05-01 23:13:56 +0200327 exe 'set ttymouse=' .. ttymouse_val
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200328 go
Bram Moolenaar49452192019-04-17 16:27:02 +0200329 call assert_equal(1, line('w0'), msg)
330 call assert_equal([0, 1, 1, 0], getpos('.'), msg)
Bram Moolenaar049736f2019-04-07 21:55:07 +0200331
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200332 call MouseWheelDown(1, 1)
Bram Moolenaar49452192019-04-17 16:27:02 +0200333 call assert_equal(4, line('w0'), msg)
334 call assert_equal([0, 4, 1, 0], getpos('.'), msg)
Bram Moolenaar049736f2019-04-07 21:55:07 +0200335
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200336 call MouseWheelDown(1, 1)
Bram Moolenaar49452192019-04-17 16:27:02 +0200337 call assert_equal(7, line('w0'), msg)
338 call assert_equal([0, 7, 1, 0], getpos('.'), msg)
Bram Moolenaar049736f2019-04-07 21:55:07 +0200339
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200340 call MouseWheelUp(1, 1)
Bram Moolenaar49452192019-04-17 16:27:02 +0200341 call assert_equal(4, line('w0'), msg)
342 call assert_equal([0, 7, 1, 0], getpos('.'), msg)
Bram Moolenaar049736f2019-04-07 21:55:07 +0200343
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200344 call MouseWheelUp(1, 1)
Bram Moolenaar49452192019-04-17 16:27:02 +0200345 call assert_equal(1, line('w0'), msg)
346 call assert_equal([0, 7, 1, 0], getpos('.'), msg)
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200347 endfor
Bram Moolenaar049736f2019-04-07 21:55:07 +0200348
349 let &mouse = save_mouse
350 let &term = save_term
351 let &ttymouse = save_ttymouse
352 bwipe!
353endfunc
Bram Moolenaar3fb01a52019-04-09 21:52:02 +0200354
Bram Moolenaarb4367b72019-10-01 14:19:07 +0200355" Test that dragging beyond the window (at the bottom and at the top)
356" scrolls window content by the number of of lines beyond the window.
357func Test_term_mouse_drag_beyond_window()
358 let save_mouse = &mouse
359 let save_term = &term
360 let save_ttymouse = &ttymouse
361 call test_override('no_query_mouse', 1)
362 set mouse=a term=xterm
363 let col = 1
364 call setline(1, range(1, 100))
365
366 " Split into 3 windows, and go into the middle window
367 " so we test dragging mouse below and above the window.
368 2split
369 wincmd j
370 2split
371
372 for ttymouse_val in s:ttymouse_values + s:ttymouse_dec
373 let msg = 'ttymouse=' .. ttymouse_val
374 exe 'set ttymouse=' .. ttymouse_val
375
376 " Line #10 at the top.
377 norm! 10zt
378 redraw
379 call assert_equal(10, winsaveview().topline, msg)
380 call assert_equal(2, winheight(0), msg)
381
382 let row = 4
383 call MouseLeftClick(row, col)
384 call assert_equal(10, winsaveview().topline, msg)
385
386 " Drag downwards. We're still in the window so topline should
387 " not change yet.
388 let row += 1
389 call MouseLeftDrag(row, col)
390 call assert_equal(10, winsaveview().topline, msg)
391
392 " We now leave the window at the bottom, so the window content should
393 " scroll by 1 line, then 2 lines (etc) as we drag further away.
394 let row += 1
395 call MouseLeftDrag(row, col)
396 call assert_equal(11, winsaveview().topline, msg)
397
398 let row += 1
399 call MouseLeftDrag(row, col)
400 call assert_equal(13, winsaveview().topline, msg)
401
402 " Now drag upwards.
403 let row -= 1
404 call MouseLeftDrag(row, col)
405 call assert_equal(14, winsaveview().topline, msg)
406
407 " We're now back in the window so the topline should not change.
408 let row -= 1
409 call MouseLeftDrag(row, col)
410 call assert_equal(14, winsaveview().topline, msg)
411
412 let row -= 1
413 call MouseLeftDrag(row, col)
414 call assert_equal(14, winsaveview().topline, msg)
415
416 " We now leave the window at the top so the window content should
417 " scroll by 1 line, then 2, then 3 (etc) in the opposite direction.
418 let row -= 1
419 call MouseLeftDrag(row, col)
420 call assert_equal(13, winsaveview().topline, msg)
421
422 let row -= 1
423 call MouseLeftDrag(row, col)
424 call assert_equal(11, winsaveview().topline, msg)
425
426 let row -= 1
427 call MouseLeftDrag(row, col)
428 call assert_equal(8, winsaveview().topline, msg)
429
430 call MouseLeftRelease(row, col)
431 call assert_equal(8, winsaveview().topline, msg)
432 call assert_equal(2, winheight(0), msg)
433 endfor
434
435 let &mouse = save_mouse
436 let &term = save_term
437 let &ttymouse = save_ttymouse
438 call test_override('no_query_mouse', 0)
439 bwipe!
440endfunc
441
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200442func Test_term_mouse_drag_window_separator()
Bram Moolenaar3fb01a52019-04-09 21:52:02 +0200443 let save_mouse = &mouse
444 let save_term = &term
445 let save_ttymouse = &ttymouse
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200446 call test_override('no_query_mouse', 1)
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200447 set mouse=a term=xterm
Bram Moolenaar3fb01a52019-04-09 21:52:02 +0200448
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200449 for ttymouse_val in s:ttymouse_values + s:ttymouse_dec
Bram Moolenaar49452192019-04-17 16:27:02 +0200450 let msg = 'ttymouse=' .. ttymouse_val
Bram Moolenaar1ee36d62019-05-01 23:13:56 +0200451 exe 'set ttymouse=' .. ttymouse_val
Bram Moolenaar3fb01a52019-04-09 21:52:02 +0200452
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200453 " Split horizontally and test dragging the horizontal window separator.
454 split
455 let rowseparator = winheight(0) + 1
456 let row = rowseparator
457 let col = 1
Bram Moolenaarc8b3dda2019-04-12 21:42:52 +0200458
Bram Moolenaar2b00b9b2019-04-17 17:08:27 +0200459 " When 'ttymouse' is 'xterm2', row/col bigger than 223 are not supported.
460 if ttymouse_val !=# 'xterm2' || row <= 223
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200461 call MouseLeftClick(row, col)
462 let row -= 1
463 call MouseLeftDrag(row, col)
Bram Moolenaar49452192019-04-17 16:27:02 +0200464 call assert_equal(rowseparator - 1, winheight(0) + 1, msg)
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200465 let row += 1
466 call MouseLeftDrag(row, col)
Bram Moolenaar49452192019-04-17 16:27:02 +0200467 call assert_equal(rowseparator, winheight(0) + 1, msg)
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200468 call MouseLeftRelease(row, col)
Bram Moolenaar49452192019-04-17 16:27:02 +0200469 call assert_equal(rowseparator, winheight(0) + 1, msg)
Bram Moolenaarc8b3dda2019-04-12 21:42:52 +0200470 endif
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200471 bwipe!
Bram Moolenaar3fb01a52019-04-09 21:52:02 +0200472
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200473 " Split vertically and test dragging the vertical window separator.
474 vsplit
475 let colseparator = winwidth(0) + 1
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200476 let row = 1
477 let col = colseparator
Bram Moolenaar3fb01a52019-04-09 21:52:02 +0200478
Bram Moolenaar2b00b9b2019-04-17 17:08:27 +0200479 " When 'ttymouse' is 'xterm2', row/col bigger than 223 are not supported.
480 if ttymouse_val !=# 'xterm2' || col <= 223
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200481 call MouseLeftClick(row, col)
482 let col -= 1
483 call MouseLeftDrag(row, col)
Bram Moolenaar49452192019-04-17 16:27:02 +0200484 call assert_equal(colseparator - 1, winwidth(0) + 1, msg)
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200485 let col += 1
486 call MouseLeftDrag(row, col)
Bram Moolenaar49452192019-04-17 16:27:02 +0200487 call assert_equal(colseparator, winwidth(0) + 1, msg)
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200488 call MouseLeftRelease(row, col)
Bram Moolenaar49452192019-04-17 16:27:02 +0200489 call assert_equal(colseparator, winwidth(0) + 1, msg)
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200490 endif
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200491 bwipe!
492 endfor
Bram Moolenaar3fb01a52019-04-09 21:52:02 +0200493
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200494 let &mouse = save_mouse
495 let &term = save_term
496 let &ttymouse = save_ttymouse
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200497 call test_override('no_query_mouse', 0)
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200498endfunc
Bram Moolenaar3fb01a52019-04-09 21:52:02 +0200499
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200500func Test_term_mouse_drag_statusline()
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200501 let save_mouse = &mouse
502 let save_term = &term
503 let save_ttymouse = &ttymouse
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200504 call test_override('no_query_mouse', 1)
Bram Moolenaarca57ab52019-04-13 14:53:16 +0200505 let save_laststatus = &laststatus
506 set mouse=a term=xterm laststatus=2
Bram Moolenaar3fb01a52019-04-09 21:52:02 +0200507
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200508 for ttymouse_val in s:ttymouse_values + s:ttymouse_dec
Bram Moolenaar49452192019-04-17 16:27:02 +0200509 let msg = 'ttymouse=' .. ttymouse_val
Bram Moolenaar1ee36d62019-05-01 23:13:56 +0200510 exe 'set ttymouse=' .. ttymouse_val
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200511
Bram Moolenaar49452192019-04-17 16:27:02 +0200512 call assert_equal(1, &cmdheight, msg)
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200513 let rowstatusline = winheight(0) + 1
514 let row = rowstatusline
515 let col = 1
Bram Moolenaarc8b3dda2019-04-12 21:42:52 +0200516
Bram Moolenaar2b00b9b2019-04-17 17:08:27 +0200517 if ttymouse_val ==# 'xterm2' && row > 223
518 " When 'ttymouse' is 'xterm2', row/col bigger than 223 are not supported.
Bram Moolenaarc8b3dda2019-04-12 21:42:52 +0200519 continue
520 endif
521
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200522 call MouseLeftClick(row, col)
523 let row -= 1
524 call MouseLeftDrag(row, col)
Bram Moolenaar49452192019-04-17 16:27:02 +0200525 call assert_equal(2, &cmdheight, msg)
526 call assert_equal(rowstatusline - 1, winheight(0) + 1, msg)
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200527 let row += 1
528 call MouseLeftDrag(row, col)
Bram Moolenaar49452192019-04-17 16:27:02 +0200529 call assert_equal(1, &cmdheight, msg)
530 call assert_equal(rowstatusline, winheight(0) + 1, msg)
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200531 call MouseLeftRelease(row, col)
Bram Moolenaar49452192019-04-17 16:27:02 +0200532 call assert_equal(1, &cmdheight, msg)
533 call assert_equal(rowstatusline, winheight(0) + 1, msg)
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200534 endfor
535
Bram Moolenaar3fb01a52019-04-09 21:52:02 +0200536 let &mouse = save_mouse
537 let &term = save_term
538 let &ttymouse = save_ttymouse
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200539 call test_override('no_query_mouse', 0)
Bram Moolenaarca57ab52019-04-13 14:53:16 +0200540 let &laststatus = save_laststatus
541endfunc
542
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200543func Test_term_mouse_click_tab()
Bram Moolenaarca57ab52019-04-13 14:53:16 +0200544 let save_mouse = &mouse
545 let save_term = &term
546 let save_ttymouse = &ttymouse
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200547 call test_override('no_query_mouse', 1)
Bram Moolenaarca57ab52019-04-13 14:53:16 +0200548 set mouse=a term=xterm
549 let row = 1
550
Bram Moolenaard7885432019-05-03 13:44:10 +0200551 for ttymouse_val in s:ttymouse_values + s:ttymouse_dec + s:ttymouse_netterm
Bram Moolenaar49452192019-04-17 16:27:02 +0200552 let msg = 'ttymouse=' .. ttymouse_val
Bram Moolenaar1ee36d62019-05-01 23:13:56 +0200553 exe 'set ttymouse=' .. ttymouse_val
Bram Moolenaarca57ab52019-04-13 14:53:16 +0200554 e Xfoo
555 tabnew Xbar
556
557 let a = split(execute(':tabs'), "\n")
558 call assert_equal(['Tab page 1',
559 \ ' Xfoo',
560 \ 'Tab page 2',
Bram Moolenaar49452192019-04-17 16:27:02 +0200561 \ '> Xbar'], a, msg)
Bram Moolenaarca57ab52019-04-13 14:53:16 +0200562
563 " Test clicking on tab names in the tabline at the top.
564 let col = 2
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200565 redraw
Bram Moolenaarca57ab52019-04-13 14:53:16 +0200566 call MouseLeftClick(row, col)
567 call MouseLeftRelease(row, col)
568 let a = split(execute(':tabs'), "\n")
569 call assert_equal(['Tab page 1',
570 \ '> Xfoo',
571 \ 'Tab page 2',
Bram Moolenaar49452192019-04-17 16:27:02 +0200572 \ ' Xbar'], a, msg)
Bram Moolenaarca57ab52019-04-13 14:53:16 +0200573
574 let col = 9
575 call MouseLeftClick(row, col)
576 call MouseLeftRelease(row, col)
577 let a = split(execute(':tabs'), "\n")
578 call assert_equal(['Tab page 1',
579 \ ' Xfoo',
580 \ 'Tab page 2',
Bram Moolenaar49452192019-04-17 16:27:02 +0200581 \ '> Xbar'], a, msg)
Bram Moolenaarca57ab52019-04-13 14:53:16 +0200582
583 %bwipe!
584 endfor
585
586 let &mouse = save_mouse
587 let &term = save_term
588 let &ttymouse = save_ttymouse
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200589 call test_override('no_query_mouse', 0)
Bram Moolenaar3fb01a52019-04-09 21:52:02 +0200590endfunc
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200591
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200592func Test_term_mouse_click_X_to_close_tab()
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200593 let save_mouse = &mouse
594 let save_term = &term
595 let save_ttymouse = &ttymouse
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200596 call test_override('no_query_mouse', 1)
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200597 set mouse=a term=xterm
598 let row = 1
599 let col = &columns
600
Bram Moolenaard7885432019-05-03 13:44:10 +0200601 for ttymouse_val in s:ttymouse_values + s:ttymouse_dec + s:ttymouse_netterm
Bram Moolenaar2b00b9b2019-04-17 17:08:27 +0200602 if ttymouse_val ==# 'xterm2' && col > 223
603 " When 'ttymouse' is 'xterm2', row/col bigger than 223 are not supported.
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200604 continue
605 endif
Bram Moolenaar49452192019-04-17 16:27:02 +0200606 let msg = 'ttymouse=' .. ttymouse_val
Bram Moolenaar1ee36d62019-05-01 23:13:56 +0200607 exe 'set ttymouse=' .. ttymouse_val
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200608 e Xtab1
609 tabnew Xtab2
610 tabnew Xtab3
611 tabn 2
612
613 let a = split(execute(':tabs'), "\n")
614 call assert_equal(['Tab page 1',
615 \ ' Xtab1',
616 \ 'Tab page 2',
617 \ '> Xtab2',
618 \ 'Tab page 3',
Bram Moolenaar49452192019-04-17 16:27:02 +0200619 \ ' Xtab3'], a, msg)
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200620
621 " Click on "X" in tabline to close current tab i.e. Xtab2.
622 redraw
623 call MouseLeftClick(row, col)
624 call MouseLeftRelease(row, col)
625 let a = split(execute(':tabs'), "\n")
626 call assert_equal(['Tab page 1',
627 \ ' Xtab1',
628 \ 'Tab page 2',
Bram Moolenaar49452192019-04-17 16:27:02 +0200629 \ '> Xtab3'], a, msg)
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200630
631 %bwipe!
632 endfor
633
634 let &mouse = save_mouse
635 let &term = save_term
636 let &ttymouse = save_ttymouse
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200637 call test_override('no_query_mouse', 0)
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200638endfunc
Bram Moolenaare3e38282019-04-15 20:55:31 +0200639
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200640func Test_term_mouse_drag_to_move_tab()
Bram Moolenaare3e38282019-04-15 20:55:31 +0200641 let save_mouse = &mouse
642 let save_term = &term
643 let save_ttymouse = &ttymouse
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200644 call test_override('no_query_mouse', 1)
Bram Moolenaare3e38282019-04-15 20:55:31 +0200645 " Set 'mousetime' to 1 to avoid recognizing a double-click in the loop
646 set mouse=a term=xterm mousetime=1
647 let row = 1
648
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200649 for ttymouse_val in s:ttymouse_values + s:ttymouse_dec
Bram Moolenaar49452192019-04-17 16:27:02 +0200650 let msg = 'ttymouse=' .. ttymouse_val
Bram Moolenaar1ee36d62019-05-01 23:13:56 +0200651 exe 'set ttymouse=' .. ttymouse_val
Bram Moolenaare3e38282019-04-15 20:55:31 +0200652 e Xtab1
653 tabnew Xtab2
654
655 let a = split(execute(':tabs'), "\n")
656 call assert_equal(['Tab page 1',
657 \ ' Xtab1',
658 \ 'Tab page 2',
Bram Moolenaar49452192019-04-17 16:27:02 +0200659 \ '> Xtab2'], a, msg)
Bram Moolenaare3e38282019-04-15 20:55:31 +0200660 redraw
661
662 " Click in tab2 and drag it to tab1.
663 " Check getcharmod() to verify that click is not
664 " interpreted as a spurious double-click.
665 call MouseLeftClick(row, 10)
Bram Moolenaar49452192019-04-17 16:27:02 +0200666 call assert_equal(0, getcharmod(), msg)
Bram Moolenaare3e38282019-04-15 20:55:31 +0200667 for col in [9, 8, 7, 6]
668 call MouseLeftDrag(row, col)
669 endfor
670 call MouseLeftRelease(row, col)
671 let a = split(execute(':tabs'), "\n")
672 call assert_equal(['Tab page 1',
673 \ '> Xtab2',
674 \ 'Tab page 2',
Bram Moolenaar49452192019-04-17 16:27:02 +0200675 \ ' Xtab1'], a, msg)
Bram Moolenaare3e38282019-04-15 20:55:31 +0200676
Bram Moolenaar7f279762019-04-15 21:48:22 +0200677 " brief sleep to avoid causing a double-click
678 sleep 20m
Bram Moolenaare3e38282019-04-15 20:55:31 +0200679 %bwipe!
680 endfor
681
682 let &mouse = save_mouse
683 let &term = save_term
684 let &ttymouse = save_ttymouse
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200685 call test_override('no_query_mouse', 0)
Bram Moolenaare3e38282019-04-15 20:55:31 +0200686 set mousetime&
687endfunc
688
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200689func Test_term_mouse_double_click_to_create_tab()
Bram Moolenaare3e38282019-04-15 20:55:31 +0200690 let save_mouse = &mouse
691 let save_term = &term
692 let save_ttymouse = &ttymouse
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200693 call test_override('no_query_mouse', 1)
Bram Moolenaare3e38282019-04-15 20:55:31 +0200694 " Set 'mousetime' to a small value, so that double-click works but we don't
695 " have to wait long to avoid a triple-click.
696 set mouse=a term=xterm mousetime=100
697 let row = 1
698 let col = 10
699
Bram Moolenaard0621d82019-05-02 21:12:19 +0200700 let round = 0
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200701 for ttymouse_val in s:ttymouse_values + s:ttymouse_dec
Bram Moolenaar49452192019-04-17 16:27:02 +0200702 let msg = 'ttymouse=' .. ttymouse_val
Bram Moolenaar1ee36d62019-05-01 23:13:56 +0200703 exe 'set ttymouse=' .. ttymouse_val
Bram Moolenaare3e38282019-04-15 20:55:31 +0200704 e Xtab1
705 tabnew Xtab2
706
Bram Moolenaard0621d82019-05-02 21:12:19 +0200707 if round > 0
708 " We need to sleep, or else the first MouseLeftClick() will be
709 " interpreted as a spurious triple-click.
710 sleep 100m
711 endif
712 let round += 1
713
Bram Moolenaare3e38282019-04-15 20:55:31 +0200714 let a = split(execute(':tabs'), "\n")
715 call assert_equal(['Tab page 1',
716 \ ' Xtab1',
717 \ 'Tab page 2',
Bram Moolenaar49452192019-04-17 16:27:02 +0200718 \ '> Xtab2'], a, msg)
Bram Moolenaare3e38282019-04-15 20:55:31 +0200719
720 redraw
721 call MouseLeftClick(row, col)
722 " Check getcharmod() to verify that first click is not
723 " interpreted as a spurious double-click.
Bram Moolenaar49452192019-04-17 16:27:02 +0200724 call assert_equal(0, getcharmod(), msg)
Bram Moolenaare3e38282019-04-15 20:55:31 +0200725 call MouseLeftRelease(row, col)
726 call MouseLeftClick(row, col)
Bram Moolenaar49452192019-04-17 16:27:02 +0200727 call assert_equal(32, getcharmod(), msg) " double-click
Bram Moolenaare3e38282019-04-15 20:55:31 +0200728 call MouseLeftRelease(row, col)
729 let a = split(execute(':tabs'), "\n")
730 call assert_equal(['Tab page 1',
731 \ ' Xtab1',
732 \ 'Tab page 2',
733 \ '> [No Name]',
734 \ 'Tab page 3',
Bram Moolenaar49452192019-04-17 16:27:02 +0200735 \ ' Xtab2'], a, msg)
Bram Moolenaare3e38282019-04-15 20:55:31 +0200736
Bram Moolenaare3e38282019-04-15 20:55:31 +0200737 %bwipe!
738 endfor
739
740 let &mouse = save_mouse
741 let &term = save_term
742 let &ttymouse = save_ttymouse
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200743 call test_override('no_query_mouse', 0)
Bram Moolenaare3e38282019-04-15 20:55:31 +0200744 set mousetime&
745endfunc
Bram Moolenaar696d6372019-04-17 16:33:46 +0200746
747func Test_xterm_mouse_click_in_fold_columns()
748 new
749 let save_mouse = &mouse
750 let save_term = &term
751 let save_ttymouse = &ttymouse
752 let save_foldcolumn = &foldcolumn
Bram Moolenaar2b00b9b2019-04-17 17:08:27 +0200753 set mouse=a term=xterm foldcolumn=3 ttymouse=xterm2
Bram Moolenaar696d6372019-04-17 16:33:46 +0200754
755 " Create 2 nested folds.
756 call setline(1, range(1, 7))
757 2,6fold
758 norm! zR
759 4,5fold
760 call assert_equal([-1, -1, -1, 4, 4, -1, -1],
761 \ map(range(1, 7), 'foldclosed(v:val)'))
762
763 " Click in "+" of inner fold in foldcolumn should open it.
764 redraw
765 let row = 4
766 let col = 2
767 call MouseLeftClick(row, col)
768 call MouseLeftRelease(row, col)
769 call assert_equal([-1, -1, -1, -1, -1, -1, -1],
770 \ map(range(1, 7), 'foldclosed(v:val)'))
771
772 " Click in "-" of outer fold in foldcolumn should close it.
773 redraw
774 let row = 2
775 let col = 1
776 call MouseLeftClick(row, col)
777 call MouseLeftRelease(row, col)
778 call assert_equal([-1, 2, 2, 2, 2, 2, -1],
779 \ map(range(1, 7), 'foldclosed(v:val)'))
780 norm! zR
781
782 " Click in "|" of inner fold in foldcolumn should close it.
783 redraw
784 let row = 5
785 let col = 2
786 call MouseLeftClick(row, col)
787 call MouseLeftRelease(row, col)
788 call assert_equal([-1, -1, -1, 4, 4, -1, -1],
789 \ map(range(1, 7), 'foldclosed(v:val)'))
790
791 let &foldcolumn = save_foldcolumn
792 let &ttymouse = save_ttymouse
793 let &term = save_term
794 let &mouse = save_mouse
795 bwipe!
796endfunc
Bram Moolenaar66761db2019-06-05 22:07:51 +0200797
798" This only checks if the sequence is recognized.
Bram Moolenaar66761db2019-06-05 22:07:51 +0200799func Test_term_rgb_response()
800 set t_RF=x
801 set t_RB=y
802
803 " response to t_RF, 4 digits
804 let red = 0x12
805 let green = 0x34
806 let blue = 0x56
807 let seq = printf("\<Esc>]10;rgb:%02x00/%02x00/%02x00\x07", red, green, blue)
808 call feedkeys(seq, 'Lx!')
809 call assert_equal(seq, v:termrfgresp)
810
811 " response to t_RF, 2 digits
812 let red = 0x78
813 let green = 0x9a
814 let blue = 0xbc
815 let seq = printf("\<Esc>]10;rgb:%02x/%02x/%02x\x07", red, green, blue)
816 call feedkeys(seq, 'Lx!')
817 call assert_equal(seq, v:termrfgresp)
818
Bram Moolenaar32e19772019-06-05 22:57:04 +0200819 " response to t_RB, 4 digits, dark
820 set background=light
Bram Moolenaarce90e362019-09-08 18:58:44 +0200821 eval 'background'->test_option_not_set()
Bram Moolenaar32e19772019-06-05 22:57:04 +0200822 let red = 0x29
823 let green = 0x4a
824 let blue = 0x6b
825 let seq = printf("\<Esc>]11;rgb:%02x00/%02x00/%02x00\x07", red, green, blue)
826 call feedkeys(seq, 'Lx!')
827 call assert_equal(seq, v:termrbgresp)
828 call assert_equal('dark', &background)
829
830 " response to t_RB, 4 digits, light
831 set background=dark
832 call test_option_not_set('background')
833 let red = 0x81
834 let green = 0x63
Bram Moolenaar66761db2019-06-05 22:07:51 +0200835 let blue = 0x65
836 let seq = printf("\<Esc>]11;rgb:%02x00/%02x00/%02x00\x07", red, green, blue)
837 call feedkeys(seq, 'Lx!')
838 call assert_equal(seq, v:termrbgresp)
Bram Moolenaar32e19772019-06-05 22:57:04 +0200839 call assert_equal('light', &background)
Bram Moolenaar66761db2019-06-05 22:07:51 +0200840
Bram Moolenaar32e19772019-06-05 22:57:04 +0200841 " response to t_RB, 2 digits, dark
842 set background=light
843 call test_option_not_set('background')
844 let red = 0x47
845 let green = 0x59
846 let blue = 0x5b
Bram Moolenaar66761db2019-06-05 22:07:51 +0200847 let seq = printf("\<Esc>]11;rgb:%02x/%02x/%02x\x07", red, green, blue)
848 call feedkeys(seq, 'Lx!')
849 call assert_equal(seq, v:termrbgresp)
Bram Moolenaar32e19772019-06-05 22:57:04 +0200850 call assert_equal('dark', &background)
851
852 " response to t_RB, 2 digits, light
853 set background=dark
854 call test_option_not_set('background')
855 let red = 0x83
856 let green = 0xa4
857 let blue = 0xc2
858 let seq = printf("\<Esc>]11;rgb:%02x/%02x/%02x\x07", red, green, blue)
859 call feedkeys(seq, 'Lx!')
860 call assert_equal(seq, v:termrbgresp)
861 call assert_equal('light', &background)
Bram Moolenaar66761db2019-06-05 22:07:51 +0200862
863 set t_RF= t_RB=
864endfunc
865
866" This only checks if the sequence is recognized.
Bram Moolenaar1a4cbb12019-10-12 13:25:44 +0200867" This must be after other tests, because it has side effects to xterm
868" properties.
869func Test_xx01_term_style_response()
Bram Moolenaar66761db2019-06-05 22:07:51 +0200870 " Termresponse is only parsed when t_RV is not empty.
871 set t_RV=x
872
873 " send the termresponse to trigger requesting the XT codes
874 let seq = "\<Esc>[>41;337;0c"
875 call feedkeys(seq, 'Lx!')
876 call assert_equal(seq, v:termresponse)
877
878 let seq = "\<Esc>P1$r2 q\<Esc>\\"
879 call feedkeys(seq, 'Lx!')
880 call assert_equal(seq, v:termstyleresp)
881
882 set t_RV=
883endfunc
Bram Moolenaarde6dbb42019-06-06 11:59:18 +0200884
Bram Moolenaar1a4cbb12019-10-12 13:25:44 +0200885" This checks the libvterm version response.
886" This must be after other tests, because it has side effects to xterm
887" properties.
888" TODO: check other terminals response
889func Test_xx02_libvterm_response()
890 " Termresponse is only parsed when t_RV is not empty.
891 set t_RV=x
892 set ttymouse=xterm
893 call test_option_not_set('ttymouse')
894
895 let seq = "\<Esc>[>0;100;0c"
896 call feedkeys(seq, 'Lx!')
897 call assert_equal(seq, v:termresponse)
898 call assert_equal('sgr', &ttymouse)
899
900 set t_RV=
901endfunc
902
Bram Moolenaarde6dbb42019-06-06 11:59:18 +0200903func Test_get_termcode()
Bram Moolenaareb663282019-10-06 12:02:15 +0200904 try
905 let k1 = &t_k1
906 catch /E113/
907 throw 'Skipped: Unable to query termcodes'
908 endtry
Bram Moolenaarde6dbb42019-06-06 11:59:18 +0200909 set t_k1=
910 set t_k1&
911 call assert_equal(k1, &t_k1)
Bram Moolenaar9aeb3362019-06-06 12:36:15 +0200912
913 " use external termcap first
914 set nottybuiltin
915 set t_k1=
916 set t_k1&
917 " when using external termcap may get something else, but it must not be
918 " empty, since we would fallback to the builtin one.
919 call assert_notequal('', &t_k1)
920
921 if &term =~ 'xterm'
922 " use internal termcap first
923 let term_save = &term
924 let &term = 'builtin_' .. &term
925 set t_k1=
926 set t_k1&
927 call assert_equal(k1, &t_k1)
928 let &term = term_save
929 endif
930
931 set ttybuiltin
Bram Moolenaarde6dbb42019-06-06 11:59:18 +0200932endfunc
Bram Moolenaar18a79a62019-10-12 15:36:11 +0200933
934func GetEscCodeCSI27(key, modifier)
935 let key = printf("%d", char2nr(a:key))
936 let mod = printf("%d", a:modifier)
937 return "\<Esc>[27;" .. mod .. ';' .. key .. '~'
938endfunc
939
940func GetEscCodeCSIu(key, modifier)
941 let key = printf("%d", char2nr(a:key))
942 let mod = printf("%d", a:modifier)
943 return "\<Esc>[" .. key .. ';' .. mod .. 'u'
944endfunc
945
946" This checks the CSI sequences when in modifyOtherKeys mode.
947" The mode doesn't need to be enabled, the codes are always detected.
948func RunTest_modifyOtherKeys(func)
949 new
Bram Moolenaar459fd782019-10-13 16:43:39 +0200950 set timeoutlen=10
Bram Moolenaar18a79a62019-10-12 15:36:11 +0200951
952 " Shift-X is send as 'X' with the shift modifier
953 call feedkeys('a' .. a:func('X', 2) .. "\<Esc>", 'Lx!')
954 call assert_equal('X', getline(1))
955
956 " Ctrl-i is Tab
957 call setline(1, '')
958 call feedkeys('a' .. a:func('i', 5) .. "\<Esc>", 'Lx!')
959 call assert_equal("\t", getline(1))
960
961 " Ctrl-I is also Tab
962 call setline(1, '')
963 call feedkeys('a' .. a:func('I', 5) .. "\<Esc>", 'Lx!')
964 call assert_equal("\t", getline(1))
965
966 " Alt-x is ø
967 call setline(1, '')
968 call feedkeys('a' .. a:func('x', 3) .. "\<Esc>", 'Lx!')
969 call assert_equal("ø", getline(1))
970
971 " Meta-x is also ø
972 call setline(1, '')
973 call feedkeys('a' .. a:func('x', 9) .. "\<Esc>", 'Lx!')
974 call assert_equal("ø", getline(1))
975
976 " Alt-X is Ø
977 call setline(1, '')
978 call feedkeys('a' .. a:func('X', 3) .. "\<Esc>", 'Lx!')
979 call assert_equal("Ø", getline(1))
980
981 " Meta-X is ø
982 call setline(1, '')
983 call feedkeys('a' .. a:func('X', 9) .. "\<Esc>", 'Lx!')
984 call assert_equal("Ø", getline(1))
985
986 bwipe!
987 set timeoutlen&
988endfunc
989
Bram Moolenaar459fd782019-10-13 16:43:39 +0200990func Test_modifyOtherKeys_basic()
Bram Moolenaar18a79a62019-10-12 15:36:11 +0200991 call RunTest_modifyOtherKeys(function('GetEscCodeCSI27'))
Bram Moolenaar18a79a62019-10-12 15:36:11 +0200992 call RunTest_modifyOtherKeys(function('GetEscCodeCSIu'))
993endfunc
Bram Moolenaard1e2f392019-10-12 18:22:50 +0200994
995func RunTest_mapping_shift(key, func)
996 call setline(1, '')
997 if a:key == '|'
998 exe 'inoremap \| xyz'
999 else
1000 exe 'inoremap ' .. a:key .. ' xyz'
1001 endif
1002 call feedkeys('a' .. a:func(a:key, 2) .. "\<Esc>", 'Lx!')
1003 call assert_equal("xyz", getline(1))
1004 if a:key == '|'
1005 exe 'iunmap \|'
1006 else
1007 exe 'iunmap ' .. a:key
1008 endif
1009endfunc
1010
1011func RunTest_mapping_works_with_shift(func)
1012 new
Bram Moolenaar459fd782019-10-13 16:43:39 +02001013 set timeoutlen=10
Bram Moolenaard1e2f392019-10-12 18:22:50 +02001014
1015 call RunTest_mapping_shift('@', a:func)
1016 call RunTest_mapping_shift('A', a:func)
1017 call RunTest_mapping_shift('Z', a:func)
1018 call RunTest_mapping_shift('^', a:func)
1019 call RunTest_mapping_shift('_', a:func)
1020 call RunTest_mapping_shift('{', a:func)
1021 call RunTest_mapping_shift('|', a:func)
1022 call RunTest_mapping_shift('}', a:func)
1023 call RunTest_mapping_shift('~', a:func)
1024
1025 bwipe!
1026 set timeoutlen&
1027endfunc
1028
Bram Moolenaar459fd782019-10-13 16:43:39 +02001029func Test_mapping_works_with_shift_plain()
Bram Moolenaard1e2f392019-10-12 18:22:50 +02001030 call RunTest_mapping_works_with_shift(function('GetEscCodeCSI27'))
1031 call RunTest_mapping_works_with_shift(function('GetEscCodeCSIu'))
1032endfunc
Bram Moolenaar459fd782019-10-13 16:43:39 +02001033
1034func RunTest_mapping_mods(map, key, func, code)
1035 call setline(1, '')
1036 exe 'inoremap ' .. a:map .. ' xyz'
1037 call feedkeys('a' .. a:func(a:key, a:code) .. "\<Esc>", 'Lx!')
1038 call assert_equal("xyz", getline(1))
1039 exe 'iunmap ' .. a:map
1040endfunc
1041
1042func RunTest_mapping_works_with_mods(func, mods, code)
1043 new
1044 set timeoutlen=10
1045
1046 if a:mods !~ 'S'
1047 " Shift by itself has no effect
1048 call RunTest_mapping_mods('<' .. a:mods .. '-@>', '@', a:func, a:code)
1049 endif
1050 call RunTest_mapping_mods('<' .. a:mods .. '-A>', 'A', a:func, a:code)
1051 call RunTest_mapping_mods('<' .. a:mods .. '-Z>', 'Z', a:func, a:code)
1052 if a:mods !~ 'S'
1053 " with Shift code is always upper case
1054 call RunTest_mapping_mods('<' .. a:mods .. '-a>', 'a', a:func, a:code)
1055 call RunTest_mapping_mods('<' .. a:mods .. '-z>', 'z', a:func, a:code)
1056 endif
1057 if a:mods != 'A'
1058 " with Alt code is not in upper case
1059 call RunTest_mapping_mods('<' .. a:mods .. '-a>', 'A', a:func, a:code)
1060 call RunTest_mapping_mods('<' .. a:mods .. '-z>', 'Z', a:func, a:code)
1061 endif
1062 call RunTest_mapping_mods('<' .. a:mods .. '-á>', 'á', a:func, a:code)
1063 if a:mods !~ 'S'
1064 " Shift by itself has no effect
1065 call RunTest_mapping_mods('<' .. a:mods .. '-^>', '^', a:func, a:code)
1066 call RunTest_mapping_mods('<' .. a:mods .. '-_>', '_', a:func, a:code)
1067 call RunTest_mapping_mods('<' .. a:mods .. '-{>', '{', a:func, a:code)
1068 call RunTest_mapping_mods('<' .. a:mods .. '-\|>', '|', a:func, a:code)
1069 call RunTest_mapping_mods('<' .. a:mods .. '-}>', '}', a:func, a:code)
1070 call RunTest_mapping_mods('<' .. a:mods .. '-~>', '~', a:func, a:code)
1071 endif
1072
1073 bwipe!
1074 set timeoutlen&
1075endfunc
1076
1077func Test_mapping_works_with_shift()
1078 call RunTest_mapping_works_with_mods(function('GetEscCodeCSI27'), 'S', 2)
1079 call RunTest_mapping_works_with_mods(function('GetEscCodeCSIu'), 'S', 2)
1080endfunc
1081
1082func Test_mapping_works_with_ctrl()
1083 call RunTest_mapping_works_with_mods(function('GetEscCodeCSI27'), 'C', 5)
1084 call RunTest_mapping_works_with_mods(function('GetEscCodeCSIu'), 'C', 5)
1085endfunc
1086
1087func Test_mapping_works_with_shift_ctrl()
1088 call RunTest_mapping_works_with_mods(function('GetEscCodeCSI27'), 'C-S', 6)
1089 call RunTest_mapping_works_with_mods(function('GetEscCodeCSIu'), 'C-S', 6)
1090endfunc
1091
1092" Below we also test the "u" code with Alt, This works, but libvterm would not
1093" send the Alt key like this but by prefixing an Esc.
1094
1095func Test_mapping_works_with_alt()
1096 call RunTest_mapping_works_with_mods(function('GetEscCodeCSI27'), 'A', 3)
1097 call RunTest_mapping_works_with_mods(function('GetEscCodeCSIu'), 'A', 3)
1098endfunc
1099
1100func Test_mapping_works_with_shift_alt()
1101 call RunTest_mapping_works_with_mods(function('GetEscCodeCSI27'), 'S-A', 4)
1102 call RunTest_mapping_works_with_mods(function('GetEscCodeCSIu'), 'S-A', 4)
1103endfunc
1104
1105func Test_mapping_works_with_ctrl_alt()
1106 call RunTest_mapping_works_with_mods(function('GetEscCodeCSI27'), 'C-A', 7)
1107 call RunTest_mapping_works_with_mods(function('GetEscCodeCSIu'), 'C-A', 7)
1108endfunc
1109
1110func Test_mapping_works_with_shift_ctrl_alt()
1111 call RunTest_mapping_works_with_mods(function('GetEscCodeCSI27'), 'C-S-A', 8)
1112 call RunTest_mapping_works_with_mods(function('GetEscCodeCSIu'), 'C-S-A', 8)
1113endfunc