blob: 3925bd7c16eb8ac6109e76776bf8de0085175c55 [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
Bram Moolenaarf19f8d12019-12-18 19:36:23 +010041 return "\<Esc>[M" .. bytes
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +020042 elseif &ttymouse ==# 'sgr'
Bram Moolenaarf19f8d12019-12-18 19:36:23 +010043 return printf("\<Esc>[<%d;%d;%d%s", a:code, a:col, a:row, a:m)
Bram Moolenaard0621d82019-05-02 21:12:19 +020044 elseif &ttymouse ==# 'urxvt'
Bram Moolenaarf19f8d12019-12-18 19:36:23 +010045 return printf("\<Esc>[%d;%d;%dM", a:code + 0x20, a:col, a:row)
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +020046 endif
47endfunc
48
Bram Moolenaar92fd5992019-05-02 23:00:22 +020049func DecEscapeCode(code, down, row, col)
Bram Moolenaarf19f8d12019-12-18 19:36:23 +010050 return printf("\<Esc>[%d;%d;%d;%d&w", a:code, a:down, a:row, a:col)
Bram Moolenaar92fd5992019-05-02 23:00:22 +020051endfunc
52
Bram Moolenaard7885432019-05-03 13:44:10 +020053func NettermEscapeCode(row, col)
Bram Moolenaarf19f8d12019-12-18 19:36:23 +010054 return printf("\<Esc>}%d,%d\r", a:row, a:col)
55endfunc
56
57func MouseLeftClickCode(row, col)
58 if &ttymouse ==# 'dec'
59 return DecEscapeCode(2, 4, a:row, a:col)
60 elseif &ttymouse ==# 'netterm'
61 return NettermEscapeCode(a:row, a:col)
62 else
63 return TerminalEscapeCode(0, a:row, a:col, 'M')
64 endif
Bram Moolenaard7885432019-05-03 13:44:10 +020065endfunc
66
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +020067func MouseLeftClick(row, col)
Bram Moolenaarf19f8d12019-12-18 19:36:23 +010068 call feedkeys(MouseLeftClickCode(a:row, a:col), 'Lx!')
69endfunc
70
71func MouseMiddleClickCode(row, col)
Bram Moolenaar92fd5992019-05-02 23:00:22 +020072 if &ttymouse ==# 'dec'
Bram Moolenaarf19f8d12019-12-18 19:36:23 +010073 return DecEscapeCode(4, 2, a:row, a:col)
Bram Moolenaar92fd5992019-05-02 23:00:22 +020074 else
Bram Moolenaarf19f8d12019-12-18 19:36:23 +010075 return TerminalEscapeCode(1, a:row, a:col, 'M')
Bram Moolenaar92fd5992019-05-02 23:00:22 +020076 endif
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +020077endfunc
78
Bram Moolenaarc1b81602019-04-27 19:11:35 +020079func MouseMiddleClick(row, col)
Bram Moolenaarf19f8d12019-12-18 19:36:23 +010080 call feedkeys(MouseMiddleClickCode(a:row, a:col), 'Lx!')
81endfunc
82
83func MouseRightClickCode(row, col)
Bram Moolenaar92fd5992019-05-02 23:00:22 +020084 if &ttymouse ==# 'dec'
Bram Moolenaarf19f8d12019-12-18 19:36:23 +010085 return DecEscapeCode(6, 1, a:row, a:col)
Bram Moolenaar92fd5992019-05-02 23:00:22 +020086 else
Bram Moolenaarf19f8d12019-12-18 19:36:23 +010087 return TerminalEscapeCode(2, a:row, a:col, 'M')
Bram Moolenaar92fd5992019-05-02 23:00:22 +020088 endif
Bram Moolenaarc1b81602019-04-27 19:11:35 +020089endfunc
90
Bram Moolenaar6aa75232019-10-13 21:01:34 +020091func MouseRightClick(row, col)
Bram Moolenaarf19f8d12019-12-18 19:36:23 +010092 call feedkeys(MouseRightClickCode(a:row, a:col), 'Lx!')
93endfunc
94
95func MouseCtrlLeftClickCode(row, col)
96 let ctrl = 0x10
97 return TerminalEscapeCode(0 + ctrl, a:row, a:col, 'M')
Bram Moolenaar6aa75232019-10-13 21:01:34 +020098endfunc
99
Bram Moolenaar1ee36d62019-05-01 23:13:56 +0200100func MouseCtrlLeftClick(row, col)
Bram Moolenaarf19f8d12019-12-18 19:36:23 +0100101 call feedkeys(MouseCtrlLeftClickCode(a:row, a:col), 'Lx!')
102endfunc
103
104func MouseCtrlRightClickCode(row, col)
Bram Moolenaar1ee36d62019-05-01 23:13:56 +0200105 let ctrl = 0x10
Bram Moolenaarf19f8d12019-12-18 19:36:23 +0100106 return TerminalEscapeCode(2 + ctrl, a:row, a:col, 'M')
Bram Moolenaar1ee36d62019-05-01 23:13:56 +0200107endfunc
108
109func MouseCtrlRightClick(row, col)
Bram Moolenaarf19f8d12019-12-18 19:36:23 +0100110 call feedkeys(MouseCtrlRightClickCode(a:row, a:col), 'Lx!')
111endfunc
112
113func MouseLeftReleaseCode(row, col)
114 if &ttymouse ==# 'dec'
115 return DecEscapeCode(3, 0, a:row, a:col)
116 elseif &ttymouse ==# 'netterm'
117 return ''
118 else
119 return TerminalEscapeCode(3, a:row, a:col, 'm')
120 endif
Bram Moolenaar1ee36d62019-05-01 23:13:56 +0200121endfunc
122
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200123func MouseLeftRelease(row, col)
Bram Moolenaarf19f8d12019-12-18 19:36:23 +0100124 call feedkeys(MouseLeftReleaseCode(a:row, a:col), 'Lx!')
125endfunc
126
127func MouseMiddleReleaseCode(row, col)
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200128 if &ttymouse ==# 'dec'
Bram Moolenaarf19f8d12019-12-18 19:36:23 +0100129 return DecEscapeCode(5, 0, a:row, a:col)
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200130 else
Bram Moolenaarf19f8d12019-12-18 19:36:23 +0100131 return TerminalEscapeCode(3, a:row, a:col, 'm')
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200132 endif
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200133endfunc
134
Bram Moolenaarc1b81602019-04-27 19:11:35 +0200135func MouseMiddleRelease(row, col)
Bram Moolenaarf19f8d12019-12-18 19:36:23 +0100136 call feedkeys(MouseMiddleReleaseCode(a:row, a:col), 'Lx!')
137endfunc
138
139func MouseRightReleaseCode(row, col)
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200140 if &ttymouse ==# 'dec'
Bram Moolenaarf19f8d12019-12-18 19:36:23 +0100141 return DecEscapeCode(7, 0, a:row, a:col)
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200142 else
Bram Moolenaarf19f8d12019-12-18 19:36:23 +0100143 return TerminalEscapeCode(3, a:row, a:col, 'm')
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200144 endif
Bram Moolenaarc1b81602019-04-27 19:11:35 +0200145endfunc
146
Bram Moolenaar1ee36d62019-05-01 23:13:56 +0200147func MouseRightRelease(row, col)
Bram Moolenaarf19f8d12019-12-18 19:36:23 +0100148 call feedkeys(MouseRightReleaseCode(a:row, a:col), 'Lx!')
149endfunc
150
151func MouseLeftDragCode(row, col)
Bram Moolenaar6aa75232019-10-13 21:01:34 +0200152 if &ttymouse ==# 'dec'
Bram Moolenaarf19f8d12019-12-18 19:36:23 +0100153 return DecEscapeCode(1, 4, a:row, a:col)
Bram Moolenaar6aa75232019-10-13 21:01:34 +0200154 else
Bram Moolenaarf19f8d12019-12-18 19:36:23 +0100155 return TerminalEscapeCode(0x20, a:row, a:col, 'M')
Bram Moolenaar6aa75232019-10-13 21:01:34 +0200156 endif
Bram Moolenaar1ee36d62019-05-01 23:13:56 +0200157endfunc
158
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200159func MouseLeftDrag(row, col)
Bram Moolenaarf19f8d12019-12-18 19:36:23 +0100160 call feedkeys(MouseLeftDragCode(a:row, a:col), 'Lx!')
161endfunc
162
163func MouseWheelUpCode(row, col)
164 return TerminalEscapeCode(0x40, a:row, a:col, 'M')
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200165endfunc
166
167func MouseWheelUp(row, col)
Bram Moolenaarf19f8d12019-12-18 19:36:23 +0100168 call feedkeys(MouseWheelUpCode(a:row, a:col), 'Lx!')
169endfunc
170
171func MouseWheelDownCode(row, col)
172 return TerminalEscapeCode(0x41, a:row, a:col, 'M')
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200173endfunc
174
175func MouseWheelDown(row, col)
Bram Moolenaarf19f8d12019-12-18 19:36:23 +0100176 call feedkeys(MouseWheelDownCode(a:row, a:col), 'Lx!')
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200177endfunc
178
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200179func Test_term_mouse_left_click()
Bram Moolenaar905dd902019-04-07 14:21:47 +0200180 new
181 let save_mouse = &mouse
182 let save_term = &term
183 let save_ttymouse = &ttymouse
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200184 call test_override('no_query_mouse', 1)
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200185 set mouse=a term=xterm
Bram Moolenaar905dd902019-04-07 14:21:47 +0200186 call setline(1, ['line 1', 'line 2', 'line 3 is a bit longer'])
Bram Moolenaar905dd902019-04-07 14:21:47 +0200187
Bram Moolenaard7885432019-05-03 13:44:10 +0200188 for ttymouse_val in s:ttymouse_values + s:ttymouse_dec + s:ttymouse_netterm
Bram Moolenaar49452192019-04-17 16:27:02 +0200189 let msg = 'ttymouse=' .. ttymouse_val
Bram Moolenaar1ee36d62019-05-01 23:13:56 +0200190 exe 'set ttymouse=' .. ttymouse_val
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200191 go
Bram Moolenaar49452192019-04-17 16:27:02 +0200192 call assert_equal([0, 1, 1, 0], getpos('.'), msg)
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200193 let row = 2
194 let col = 6
195 call MouseLeftClick(row, col)
196 call MouseLeftRelease(row, col)
Bram Moolenaar49452192019-04-17 16:27:02 +0200197 call assert_equal([0, 2, 6, 0], getpos('.'), msg)
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200198 endfor
Bram Moolenaar905dd902019-04-07 14:21:47 +0200199
200 let &mouse = save_mouse
201 let &term = save_term
202 let &ttymouse = save_ttymouse
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200203 call test_override('no_query_mouse', 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +0200204 bwipe!
205endfunc
206
Bram Moolenaar6aa75232019-10-13 21:01:34 +0200207func Test_xterm_mouse_right_click_extends_visual()
208 if has('mac')
209 throw "Skipped: test right click in visual mode does not work on macOs (why?)"
210 endif
211 let save_mouse = &mouse
212 let save_term = &term
213 let save_ttymouse = &ttymouse
214 call test_override('no_query_mouse', 1)
215 set mouse=a term=xterm
216
217 for visual_mode in ["v", "V", "\<C-V>"]
218 for ttymouse_val in s:ttymouse_values + s:ttymouse_dec
219 let msg = 'visual=' .. visual_mode .. ' ttymouse=' .. ttymouse_val
220 exe 'set ttymouse=' .. ttymouse_val
221
222 call setline(1, repeat([repeat('-', 7)], 7))
223 call MouseLeftClick(4, 4)
224 call MouseLeftRelease(4, 4)
225 exe "norm! " .. visual_mode
226
227 " Right click extends top left of visual area.
228 call MouseRightClick(2, 2)
229 call MouseRightRelease(2, 2)
230
Bram Moolenaarf19f8d12019-12-18 19:36:23 +0100231 " Right click extends bottom right of visual area.
Bram Moolenaar6aa75232019-10-13 21:01:34 +0200232 call MouseRightClick(6, 6)
233 call MouseRightRelease(6, 6)
234 norm! r1gv
235
236 " Right click shrinks top left of visual area.
237 call MouseRightClick(3, 3)
238 call MouseRightRelease(3, 3)
239
240 " Right click shrinks bottom right of visual area.
241 call MouseRightClick(5, 5)
242 call MouseRightRelease(5, 5)
243 norm! r2
244
245 if visual_mode ==# 'v'
246 call assert_equal(['-------',
247 \ '-111111',
248 \ '1122222',
249 \ '2222222',
250 \ '2222211',
251 \ '111111-',
252 \ '-------'], getline(1, '$'), msg)
253 elseif visual_mode ==# 'V'
254 call assert_equal(['-------',
255 \ '1111111',
256 \ '2222222',
257 \ '2222222',
258 \ '2222222',
259 \ '1111111',
260 \ '-------'], getline(1, '$'), msg)
261 else
262 call assert_equal(['-------',
263 \ '-11111-',
264 \ '-12221-',
265 \ '-12221-',
266 \ '-12221-',
267 \ '-11111-',
268 \ '-------'], getline(1, '$'), msg)
269 endif
270 endfor
271 endfor
272
273 let &mouse = save_mouse
274 let &term = save_term
275 let &ttymouse = save_ttymouse
276 call test_override('no_query_mouse', 0)
277 bwipe!
278endfunc
279
Bram Moolenaar1ee36d62019-05-01 23:13:56 +0200280" Test that <C-LeftMouse> jumps to help tag and <C-RightMouse> jumps back.
281func Test_xterm_mouse_ctrl_click()
282 let save_mouse = &mouse
283 let save_term = &term
284 let save_ttymouse = &ttymouse
285 set mouse=a term=xterm
286
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200287 for ttymouse_val in s:ttymouse_values
Bram Moolenaar1ee36d62019-05-01 23:13:56 +0200288 let msg = 'ttymouse=' .. ttymouse_val
289 exe 'set ttymouse=' .. ttymouse_val
290 help
291 /usr_02.txt
292 norm! zt
293 let row = 1
294 let col = 1
295 call MouseCtrlLeftClick(row, col)
296 call MouseLeftRelease(row, col)
297 call assert_match('usr_02.txt$', bufname('%'), msg)
Bram Moolenaarb4367b72019-10-01 14:19:07 +0200298 call assert_equal('*usr_02.txt*', expand('<cWORD>'), msg)
Bram Moolenaar1ee36d62019-05-01 23:13:56 +0200299
300 call MouseCtrlRightClick(row, col)
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200301 call MouseRightRelease(row, col)
Bram Moolenaar1ee36d62019-05-01 23:13:56 +0200302 call assert_match('help.txt$', bufname('%'), msg)
Bram Moolenaarb4367b72019-10-01 14:19:07 +0200303 call assert_equal('|usr_02.txt|', expand('<cWORD>'), msg)
Bram Moolenaar1ee36d62019-05-01 23:13:56 +0200304
305 helpclose
306 endfor
307
308 let &mouse = save_mouse
309 let &term = save_term
310 let &ttymouse = save_ttymouse
311endfunc
312
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200313func Test_term_mouse_middle_click()
Bram Moolenaar52992fe2019-08-12 14:20:33 +0200314 CheckFeature clipboard_working
Bram Moolenaar564344a2019-04-28 13:00:12 +0200315
Bram Moolenaarc1b81602019-04-27 19:11:35 +0200316 new
317 let save_mouse = &mouse
318 let save_term = &term
319 let save_ttymouse = &ttymouse
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200320 call test_override('no_query_mouse', 1)
Bram Moolenaarc1b81602019-04-27 19:11:35 +0200321 let save_quotestar = @*
322 let @* = 'abc'
323 set mouse=a term=xterm
324
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200325 for ttymouse_val in s:ttymouse_values + s:ttymouse_dec
Bram Moolenaarc1b81602019-04-27 19:11:35 +0200326 let msg = 'ttymouse=' .. ttymouse_val
Bram Moolenaar1ee36d62019-05-01 23:13:56 +0200327 exe 'set ttymouse=' .. ttymouse_val
Bram Moolenaarc1b81602019-04-27 19:11:35 +0200328 call setline(1, ['123456789', '123456789'])
329
330 " Middle-click in the middle of the line pastes text where clicked.
331 let row = 1
332 let col = 6
333 call MouseMiddleClick(row, col)
334 call MouseMiddleRelease(row, col)
335 call assert_equal(['12345abc6789', '123456789'], getline(1, '$'), msg)
336
337 " Middle-click beyond end of the line pastes text at the end of the line.
338 let col = 20
339 call MouseMiddleClick(row, col)
340 call MouseMiddleRelease(row, col)
341 call assert_equal(['12345abc6789abc', '123456789'], getline(1, '$'), msg)
342
343 " Middle-click beyond the last line pastes in the last line.
344 let row = 5
345 let col = 3
346 call MouseMiddleClick(row, col)
347 call MouseMiddleRelease(row, col)
348 call assert_equal(['12345abc6789abc', '12abc3456789'], getline(1, '$'), msg)
349 endfor
350
351 let &mouse = save_mouse
352 let &term = save_term
353 let &ttymouse = save_ttymouse
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200354 call test_override('no_query_mouse', 0)
Bram Moolenaarc1b81602019-04-27 19:11:35 +0200355 let @* = save_quotestar
356 bwipe!
357endfunc
358
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200359" TODO: for unclear reasons this test fails if it comes after
360" Test_xterm_mouse_ctrl_click()
361func Test_1xterm_mouse_wheel()
Bram Moolenaar049736f2019-04-07 21:55:07 +0200362 new
363 let save_mouse = &mouse
364 let save_term = &term
365 let save_ttymouse = &ttymouse
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200366 set mouse=a term=xterm
Bram Moolenaar049736f2019-04-07 21:55:07 +0200367 call setline(1, range(1, 100))
368
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200369 for ttymouse_val in s:ttymouse_values
Bram Moolenaar49452192019-04-17 16:27:02 +0200370 let msg = 'ttymouse=' .. ttymouse_val
Bram Moolenaar1ee36d62019-05-01 23:13:56 +0200371 exe 'set ttymouse=' .. ttymouse_val
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200372 go
Bram Moolenaar49452192019-04-17 16:27:02 +0200373 call assert_equal(1, line('w0'), msg)
374 call assert_equal([0, 1, 1, 0], getpos('.'), msg)
Bram Moolenaar049736f2019-04-07 21:55:07 +0200375
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200376 call MouseWheelDown(1, 1)
Bram Moolenaar49452192019-04-17 16:27:02 +0200377 call assert_equal(4, line('w0'), msg)
378 call assert_equal([0, 4, 1, 0], getpos('.'), msg)
Bram Moolenaar049736f2019-04-07 21:55:07 +0200379
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200380 call MouseWheelDown(1, 1)
Bram Moolenaar49452192019-04-17 16:27:02 +0200381 call assert_equal(7, line('w0'), msg)
382 call assert_equal([0, 7, 1, 0], getpos('.'), msg)
Bram Moolenaar049736f2019-04-07 21:55:07 +0200383
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200384 call MouseWheelUp(1, 1)
Bram Moolenaar49452192019-04-17 16:27:02 +0200385 call assert_equal(4, line('w0'), msg)
386 call assert_equal([0, 7, 1, 0], getpos('.'), msg)
Bram Moolenaar049736f2019-04-07 21:55:07 +0200387
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200388 call MouseWheelUp(1, 1)
Bram Moolenaar49452192019-04-17 16:27:02 +0200389 call assert_equal(1, line('w0'), msg)
390 call assert_equal([0, 7, 1, 0], getpos('.'), msg)
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200391 endfor
Bram Moolenaar049736f2019-04-07 21:55:07 +0200392
393 let &mouse = save_mouse
394 let &term = save_term
395 let &ttymouse = save_ttymouse
396 bwipe!
397endfunc
Bram Moolenaar3fb01a52019-04-09 21:52:02 +0200398
Bram Moolenaarb4367b72019-10-01 14:19:07 +0200399" Test that dragging beyond the window (at the bottom and at the top)
Bram Moolenaarf19f8d12019-12-18 19:36:23 +0100400" scrolls window content by the number of lines beyond the window.
Bram Moolenaarb4367b72019-10-01 14:19:07 +0200401func Test_term_mouse_drag_beyond_window()
402 let save_mouse = &mouse
403 let save_term = &term
404 let save_ttymouse = &ttymouse
405 call test_override('no_query_mouse', 1)
406 set mouse=a term=xterm
407 let col = 1
408 call setline(1, range(1, 100))
409
410 " Split into 3 windows, and go into the middle window
411 " so we test dragging mouse below and above the window.
412 2split
413 wincmd j
414 2split
415
416 for ttymouse_val in s:ttymouse_values + s:ttymouse_dec
417 let msg = 'ttymouse=' .. ttymouse_val
418 exe 'set ttymouse=' .. ttymouse_val
419
420 " Line #10 at the top.
421 norm! 10zt
422 redraw
423 call assert_equal(10, winsaveview().topline, msg)
424 call assert_equal(2, winheight(0), msg)
425
426 let row = 4
427 call MouseLeftClick(row, col)
428 call assert_equal(10, winsaveview().topline, msg)
429
430 " Drag downwards. We're still in the window so topline should
431 " not change yet.
432 let row += 1
433 call MouseLeftDrag(row, col)
434 call assert_equal(10, winsaveview().topline, msg)
435
436 " We now leave the window at the bottom, so the window content should
437 " scroll by 1 line, then 2 lines (etc) as we drag further away.
438 let row += 1
439 call MouseLeftDrag(row, col)
440 call assert_equal(11, winsaveview().topline, msg)
441
442 let row += 1
443 call MouseLeftDrag(row, col)
444 call assert_equal(13, winsaveview().topline, msg)
445
446 " Now drag upwards.
447 let row -= 1
448 call MouseLeftDrag(row, col)
449 call assert_equal(14, winsaveview().topline, msg)
450
451 " We're now back in the window so the topline should not change.
452 let row -= 1
453 call MouseLeftDrag(row, col)
454 call assert_equal(14, winsaveview().topline, msg)
455
456 let row -= 1
457 call MouseLeftDrag(row, col)
458 call assert_equal(14, winsaveview().topline, msg)
459
460 " We now leave the window at the top so the window content should
461 " scroll by 1 line, then 2, then 3 (etc) in the opposite direction.
462 let row -= 1
463 call MouseLeftDrag(row, col)
464 call assert_equal(13, winsaveview().topline, msg)
465
466 let row -= 1
467 call MouseLeftDrag(row, col)
468 call assert_equal(11, winsaveview().topline, msg)
469
470 let row -= 1
471 call MouseLeftDrag(row, col)
472 call assert_equal(8, winsaveview().topline, msg)
473
474 call MouseLeftRelease(row, col)
475 call assert_equal(8, winsaveview().topline, msg)
476 call assert_equal(2, winheight(0), msg)
477 endfor
478
479 let &mouse = save_mouse
480 let &term = save_term
481 let &ttymouse = save_ttymouse
482 call test_override('no_query_mouse', 0)
483 bwipe!
484endfunc
485
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200486func Test_term_mouse_drag_window_separator()
Bram Moolenaar3fb01a52019-04-09 21:52:02 +0200487 let save_mouse = &mouse
488 let save_term = &term
489 let save_ttymouse = &ttymouse
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200490 call test_override('no_query_mouse', 1)
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200491 set mouse=a term=xterm
Bram Moolenaar3fb01a52019-04-09 21:52:02 +0200492
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200493 for ttymouse_val in s:ttymouse_values + s:ttymouse_dec
Bram Moolenaar49452192019-04-17 16:27:02 +0200494 let msg = 'ttymouse=' .. ttymouse_val
Bram Moolenaar1ee36d62019-05-01 23:13:56 +0200495 exe 'set ttymouse=' .. ttymouse_val
Bram Moolenaar3fb01a52019-04-09 21:52:02 +0200496
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200497 " Split horizontally and test dragging the horizontal window separator.
498 split
499 let rowseparator = winheight(0) + 1
500 let row = rowseparator
501 let col = 1
Bram Moolenaarc8b3dda2019-04-12 21:42:52 +0200502
Bram Moolenaar2b00b9b2019-04-17 17:08:27 +0200503 " When 'ttymouse' is 'xterm2', row/col bigger than 223 are not supported.
504 if ttymouse_val !=# 'xterm2' || row <= 223
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200505 call MouseLeftClick(row, col)
506 let row -= 1
507 call MouseLeftDrag(row, col)
Bram Moolenaar49452192019-04-17 16:27:02 +0200508 call assert_equal(rowseparator - 1, winheight(0) + 1, msg)
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200509 let row += 1
510 call MouseLeftDrag(row, col)
Bram Moolenaar49452192019-04-17 16:27:02 +0200511 call assert_equal(rowseparator, winheight(0) + 1, msg)
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200512 call MouseLeftRelease(row, col)
Bram Moolenaar49452192019-04-17 16:27:02 +0200513 call assert_equal(rowseparator, winheight(0) + 1, msg)
Bram Moolenaarc8b3dda2019-04-12 21:42:52 +0200514 endif
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200515 bwipe!
Bram Moolenaar3fb01a52019-04-09 21:52:02 +0200516
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200517 " Split vertically and test dragging the vertical window separator.
518 vsplit
519 let colseparator = winwidth(0) + 1
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200520 let row = 1
521 let col = colseparator
Bram Moolenaar3fb01a52019-04-09 21:52:02 +0200522
Bram Moolenaar2b00b9b2019-04-17 17:08:27 +0200523 " When 'ttymouse' is 'xterm2', row/col bigger than 223 are not supported.
524 if ttymouse_val !=# 'xterm2' || col <= 223
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200525 call MouseLeftClick(row, col)
526 let col -= 1
527 call MouseLeftDrag(row, col)
Bram Moolenaar49452192019-04-17 16:27:02 +0200528 call assert_equal(colseparator - 1, winwidth(0) + 1, msg)
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200529 let col += 1
530 call MouseLeftDrag(row, col)
Bram Moolenaar49452192019-04-17 16:27:02 +0200531 call assert_equal(colseparator, winwidth(0) + 1, msg)
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200532 call MouseLeftRelease(row, col)
Bram Moolenaar49452192019-04-17 16:27:02 +0200533 call assert_equal(colseparator, winwidth(0) + 1, msg)
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200534 endif
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200535 bwipe!
536 endfor
Bram Moolenaar3fb01a52019-04-09 21:52:02 +0200537
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200538 let &mouse = save_mouse
539 let &term = save_term
540 let &ttymouse = save_ttymouse
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200541 call test_override('no_query_mouse', 0)
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200542endfunc
Bram Moolenaar3fb01a52019-04-09 21:52:02 +0200543
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200544func Test_term_mouse_drag_statusline()
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200545 let save_mouse = &mouse
546 let save_term = &term
547 let save_ttymouse = &ttymouse
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200548 call test_override('no_query_mouse', 1)
Bram Moolenaarca57ab52019-04-13 14:53:16 +0200549 let save_laststatus = &laststatus
550 set mouse=a term=xterm laststatus=2
Bram Moolenaar3fb01a52019-04-09 21:52:02 +0200551
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200552 for ttymouse_val in s:ttymouse_values + s:ttymouse_dec
Bram Moolenaar49452192019-04-17 16:27:02 +0200553 let msg = 'ttymouse=' .. ttymouse_val
Bram Moolenaar1ee36d62019-05-01 23:13:56 +0200554 exe 'set ttymouse=' .. ttymouse_val
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200555
Bram Moolenaar49452192019-04-17 16:27:02 +0200556 call assert_equal(1, &cmdheight, msg)
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200557 let rowstatusline = winheight(0) + 1
558 let row = rowstatusline
559 let col = 1
Bram Moolenaarc8b3dda2019-04-12 21:42:52 +0200560
Bram Moolenaar2b00b9b2019-04-17 17:08:27 +0200561 if ttymouse_val ==# 'xterm2' && row > 223
562 " When 'ttymouse' is 'xterm2', row/col bigger than 223 are not supported.
Bram Moolenaarc8b3dda2019-04-12 21:42:52 +0200563 continue
564 endif
565
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200566 call MouseLeftClick(row, col)
567 let row -= 1
568 call MouseLeftDrag(row, col)
Bram Moolenaar49452192019-04-17 16:27:02 +0200569 call assert_equal(2, &cmdheight, msg)
570 call assert_equal(rowstatusline - 1, winheight(0) + 1, msg)
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200571 let row += 1
572 call MouseLeftDrag(row, col)
Bram Moolenaar49452192019-04-17 16:27:02 +0200573 call assert_equal(1, &cmdheight, msg)
574 call assert_equal(rowstatusline, winheight(0) + 1, msg)
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200575 call MouseLeftRelease(row, col)
Bram Moolenaar49452192019-04-17 16:27:02 +0200576 call assert_equal(1, &cmdheight, msg)
577 call assert_equal(rowstatusline, winheight(0) + 1, msg)
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200578 endfor
579
Bram Moolenaar3fb01a52019-04-09 21:52:02 +0200580 let &mouse = save_mouse
581 let &term = save_term
582 let &ttymouse = save_ttymouse
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200583 call test_override('no_query_mouse', 0)
Bram Moolenaarca57ab52019-04-13 14:53:16 +0200584 let &laststatus = save_laststatus
585endfunc
586
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200587func Test_term_mouse_click_tab()
Bram Moolenaarca57ab52019-04-13 14:53:16 +0200588 let save_mouse = &mouse
589 let save_term = &term
590 let save_ttymouse = &ttymouse
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200591 call test_override('no_query_mouse', 1)
Bram Moolenaarca57ab52019-04-13 14:53:16 +0200592 set mouse=a term=xterm
593 let row = 1
594
Bram Moolenaard7885432019-05-03 13:44:10 +0200595 for ttymouse_val in s:ttymouse_values + s:ttymouse_dec + s:ttymouse_netterm
Bram Moolenaar49452192019-04-17 16:27:02 +0200596 let msg = 'ttymouse=' .. ttymouse_val
Bram Moolenaar1ee36d62019-05-01 23:13:56 +0200597 exe 'set ttymouse=' .. ttymouse_val
Bram Moolenaarca57ab52019-04-13 14:53:16 +0200598 e Xfoo
599 tabnew Xbar
600
601 let a = split(execute(':tabs'), "\n")
602 call assert_equal(['Tab page 1',
603 \ ' Xfoo',
604 \ 'Tab page 2',
Bram Moolenaar49452192019-04-17 16:27:02 +0200605 \ '> Xbar'], a, msg)
Bram Moolenaarca57ab52019-04-13 14:53:16 +0200606
607 " Test clicking on tab names in the tabline at the top.
608 let col = 2
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200609 redraw
Bram Moolenaarca57ab52019-04-13 14:53:16 +0200610 call MouseLeftClick(row, col)
611 call MouseLeftRelease(row, col)
612 let a = split(execute(':tabs'), "\n")
613 call assert_equal(['Tab page 1',
614 \ '> Xfoo',
615 \ 'Tab page 2',
Bram Moolenaar49452192019-04-17 16:27:02 +0200616 \ ' Xbar'], a, msg)
Bram Moolenaarca57ab52019-04-13 14:53:16 +0200617
618 let col = 9
619 call MouseLeftClick(row, col)
620 call MouseLeftRelease(row, col)
621 let a = split(execute(':tabs'), "\n")
622 call assert_equal(['Tab page 1',
623 \ ' Xfoo',
624 \ 'Tab page 2',
Bram Moolenaar49452192019-04-17 16:27:02 +0200625 \ '> Xbar'], a, msg)
Bram Moolenaarca57ab52019-04-13 14:53:16 +0200626
627 %bwipe!
628 endfor
629
630 let &mouse = save_mouse
631 let &term = save_term
632 let &ttymouse = save_ttymouse
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200633 call test_override('no_query_mouse', 0)
Bram Moolenaar3fb01a52019-04-09 21:52:02 +0200634endfunc
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200635
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200636func Test_term_mouse_click_X_to_close_tab()
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200637 let save_mouse = &mouse
638 let save_term = &term
639 let save_ttymouse = &ttymouse
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200640 call test_override('no_query_mouse', 1)
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200641 set mouse=a term=xterm
642 let row = 1
643 let col = &columns
644
Bram Moolenaard7885432019-05-03 13:44:10 +0200645 for ttymouse_val in s:ttymouse_values + s:ttymouse_dec + s:ttymouse_netterm
Bram Moolenaar2b00b9b2019-04-17 17:08:27 +0200646 if ttymouse_val ==# 'xterm2' && col > 223
647 " When 'ttymouse' is 'xterm2', row/col bigger than 223 are not supported.
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200648 continue
649 endif
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 Moolenaar39f76c62019-04-13 22:13:23 +0200652 e Xtab1
653 tabnew Xtab2
654 tabnew Xtab3
655 tabn 2
656
657 let a = split(execute(':tabs'), "\n")
658 call assert_equal(['Tab page 1',
659 \ ' Xtab1',
660 \ 'Tab page 2',
661 \ '> Xtab2',
662 \ 'Tab page 3',
Bram Moolenaar49452192019-04-17 16:27:02 +0200663 \ ' Xtab3'], a, msg)
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200664
665 " Click on "X" in tabline to close current tab i.e. Xtab2.
666 redraw
667 call MouseLeftClick(row, col)
668 call MouseLeftRelease(row, col)
669 let a = split(execute(':tabs'), "\n")
670 call assert_equal(['Tab page 1',
671 \ ' Xtab1',
672 \ 'Tab page 2',
Bram Moolenaar49452192019-04-17 16:27:02 +0200673 \ '> Xtab3'], a, msg)
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200674
675 %bwipe!
676 endfor
677
678 let &mouse = save_mouse
679 let &term = save_term
680 let &ttymouse = save_ttymouse
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200681 call test_override('no_query_mouse', 0)
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200682endfunc
Bram Moolenaare3e38282019-04-15 20:55:31 +0200683
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200684func Test_term_mouse_drag_to_move_tab()
Bram Moolenaare3e38282019-04-15 20:55:31 +0200685 let save_mouse = &mouse
686 let save_term = &term
687 let save_ttymouse = &ttymouse
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200688 call test_override('no_query_mouse', 1)
Bram Moolenaare3e38282019-04-15 20:55:31 +0200689 " Set 'mousetime' to 1 to avoid recognizing a double-click in the loop
690 set mouse=a term=xterm mousetime=1
691 let row = 1
692
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200693 for ttymouse_val in s:ttymouse_values + s:ttymouse_dec
Bram Moolenaar49452192019-04-17 16:27:02 +0200694 let msg = 'ttymouse=' .. ttymouse_val
Bram Moolenaar1ee36d62019-05-01 23:13:56 +0200695 exe 'set ttymouse=' .. ttymouse_val
Bram Moolenaare3e38282019-04-15 20:55:31 +0200696 e Xtab1
697 tabnew Xtab2
698
699 let a = split(execute(':tabs'), "\n")
700 call assert_equal(['Tab page 1',
701 \ ' Xtab1',
702 \ 'Tab page 2',
Bram Moolenaar49452192019-04-17 16:27:02 +0200703 \ '> Xtab2'], a, msg)
Bram Moolenaare3e38282019-04-15 20:55:31 +0200704 redraw
705
706 " Click in tab2 and drag it to tab1.
707 " Check getcharmod() to verify that click is not
708 " interpreted as a spurious double-click.
709 call MouseLeftClick(row, 10)
Bram Moolenaar49452192019-04-17 16:27:02 +0200710 call assert_equal(0, getcharmod(), msg)
Bram Moolenaare3e38282019-04-15 20:55:31 +0200711 for col in [9, 8, 7, 6]
712 call MouseLeftDrag(row, col)
713 endfor
714 call MouseLeftRelease(row, col)
715 let a = split(execute(':tabs'), "\n")
716 call assert_equal(['Tab page 1',
717 \ '> Xtab2',
718 \ 'Tab page 2',
Bram Moolenaar49452192019-04-17 16:27:02 +0200719 \ ' Xtab1'], a, msg)
Bram Moolenaare3e38282019-04-15 20:55:31 +0200720
Bram Moolenaarf36a2c72019-11-16 18:57:16 +0100721 " Click elsewhere so that click in next iteration is not
722 " interpreted as unwanted double-click.
723 call MouseLeftClick(row, 11)
724 call MouseLeftRelease(row, 11)
725
Bram Moolenaare3e38282019-04-15 20:55:31 +0200726 %bwipe!
727 endfor
728
729 let &mouse = save_mouse
730 let &term = save_term
731 let &ttymouse = save_ttymouse
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200732 call test_override('no_query_mouse', 0)
Bram Moolenaare3e38282019-04-15 20:55:31 +0200733 set mousetime&
734endfunc
735
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200736func Test_term_mouse_double_click_to_create_tab()
Bram Moolenaare3e38282019-04-15 20:55:31 +0200737 let save_mouse = &mouse
738 let save_term = &term
739 let save_ttymouse = &ttymouse
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200740 call test_override('no_query_mouse', 1)
Bram Moolenaare3e38282019-04-15 20:55:31 +0200741 " Set 'mousetime' to a small value, so that double-click works but we don't
742 " have to wait long to avoid a triple-click.
Bram Moolenaarf36a2c72019-11-16 18:57:16 +0100743 set mouse=a term=xterm mousetime=200
Bram Moolenaare3e38282019-04-15 20:55:31 +0200744 let row = 1
745 let col = 10
746
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200747 for ttymouse_val in s:ttymouse_values + s:ttymouse_dec
Bram Moolenaar49452192019-04-17 16:27:02 +0200748 let msg = 'ttymouse=' .. ttymouse_val
Bram Moolenaar1ee36d62019-05-01 23:13:56 +0200749 exe 'set ttymouse=' .. ttymouse_val
Bram Moolenaare3e38282019-04-15 20:55:31 +0200750 e Xtab1
751 tabnew Xtab2
752
753 let a = split(execute(':tabs'), "\n")
754 call assert_equal(['Tab page 1',
755 \ ' Xtab1',
756 \ 'Tab page 2',
Bram Moolenaar49452192019-04-17 16:27:02 +0200757 \ '> Xtab2'], a, msg)
Bram Moolenaare3e38282019-04-15 20:55:31 +0200758
759 redraw
760 call MouseLeftClick(row, col)
761 " Check getcharmod() to verify that first click is not
762 " interpreted as a spurious double-click.
Bram Moolenaar49452192019-04-17 16:27:02 +0200763 call assert_equal(0, getcharmod(), msg)
Bram Moolenaare3e38282019-04-15 20:55:31 +0200764 call MouseLeftRelease(row, col)
765 call MouseLeftClick(row, col)
Bram Moolenaar49452192019-04-17 16:27:02 +0200766 call assert_equal(32, getcharmod(), msg) " double-click
Bram Moolenaare3e38282019-04-15 20:55:31 +0200767 call MouseLeftRelease(row, col)
768 let a = split(execute(':tabs'), "\n")
769 call assert_equal(['Tab page 1',
770 \ ' Xtab1',
771 \ 'Tab page 2',
772 \ '> [No Name]',
773 \ 'Tab page 3',
Bram Moolenaar49452192019-04-17 16:27:02 +0200774 \ ' Xtab2'], a, msg)
Bram Moolenaare3e38282019-04-15 20:55:31 +0200775
Bram Moolenaarf36a2c72019-11-16 18:57:16 +0100776 " Click elsewhere so that click in next iteration is not
777 " interpreted as unwanted double click.
778 call MouseLeftClick(row, col + 1)
779 call MouseLeftRelease(row, col + 1)
780
Bram Moolenaare3e38282019-04-15 20:55:31 +0200781 %bwipe!
782 endfor
783
784 let &mouse = save_mouse
785 let &term = save_term
786 let &ttymouse = save_ttymouse
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200787 call test_override('no_query_mouse', 0)
Bram Moolenaare3e38282019-04-15 20:55:31 +0200788 set mousetime&
789endfunc
Bram Moolenaar696d6372019-04-17 16:33:46 +0200790
Bram Moolenaarf36a2c72019-11-16 18:57:16 +0100791" Test double/triple/quadruple click in normal mode to visually select.
792func Test_term_mouse_multiple_clicks_to_visually_select()
793 let save_mouse = &mouse
794 let save_term = &term
795 let save_ttymouse = &ttymouse
796 call test_override('no_query_mouse', 1)
797 set mouse=a term=xterm mousetime=200
798 new
799
800 for ttymouse_val in s:ttymouse_values + s:ttymouse_dec
801 let msg = 'ttymouse=' .. ttymouse_val
802 exe 'set ttymouse=' .. ttymouse_val
803 call setline(1, ['foo [foo bar] foo', 'foo'])
804
805 " Double-click on word should visually select the word.
806 call MouseLeftClick(1, 2)
807 call assert_equal(0, getcharmod(), msg)
808 call MouseLeftRelease(1, 2)
809 call MouseLeftClick(1, 2)
810 call assert_equal(32, getcharmod(), msg) " double-click
811 call MouseLeftRelease(1, 2)
812 call assert_equal('v', mode(), msg)
813 norm! r1
814 call assert_equal(['111 [foo bar] foo', 'foo'], getline(1, '$'), msg)
815
816 " Double-click on opening square bracket should visually
817 " select the whole [foo bar].
818 call MouseLeftClick(1, 5)
819 call assert_equal(0, getcharmod(), msg)
820 call MouseLeftRelease(1, 5)
821 call MouseLeftClick(1, 5)
822 call assert_equal(32, getcharmod(), msg) " double-click
823 call MouseLeftRelease(1, 5)
824 call assert_equal('v', mode(), msg)
825 norm! r2
826 call assert_equal(['111 222222222 foo', 'foo'], getline(1, '$'), msg)
827
828 " Triple-click should visually select the whole line.
829 call MouseLeftClick(1, 3)
830 call assert_equal(0, getcharmod(), msg)
831 call MouseLeftRelease(1, 3)
832 call MouseLeftClick(1, 3)
833 call assert_equal(32, getcharmod(), msg) " double-click
834 call MouseLeftRelease(1, 3)
835 call MouseLeftClick(1, 3)
836 call assert_equal(64, getcharmod(), msg) " triple-click
837 call MouseLeftRelease(1, 3)
838 call assert_equal('V', mode(), msg)
839 norm! r3
840 call assert_equal(['33333333333333333', 'foo'], getline(1, '$'), msg)
841
842 " Quadruple-click should start visual block select.
843 call MouseLeftClick(1, 2)
844 call assert_equal(0, getcharmod(), msg)
845 call MouseLeftRelease(1, 2)
846 call MouseLeftClick(1, 2)
847 call assert_equal(32, getcharmod(), msg) " double-click
848 call MouseLeftRelease(1, 2)
849 call MouseLeftClick(1, 2)
850 call assert_equal(64, getcharmod(), msg) " triple-click
851 call MouseLeftRelease(1, 2)
852 call MouseLeftClick(1, 2)
853 call assert_equal(96, getcharmod(), msg) " quadruple-click
854 call MouseLeftRelease(1, 2)
855 call assert_equal("\<c-v>", mode(), msg)
856 norm! r4
857 call assert_equal(['34333333333333333', 'foo'], getline(1, '$'), msg)
858 endfor
859
860 let &mouse = save_mouse
861 let &term = save_term
862 let &ttymouse = save_ttymouse
863 set mousetime&
864 call test_override('no_query_mouse', 0)
865 bwipe!
866endfunc
867
Bram Moolenaar696d6372019-04-17 16:33:46 +0200868func Test_xterm_mouse_click_in_fold_columns()
869 new
870 let save_mouse = &mouse
871 let save_term = &term
872 let save_ttymouse = &ttymouse
873 let save_foldcolumn = &foldcolumn
Bram Moolenaar2b00b9b2019-04-17 17:08:27 +0200874 set mouse=a term=xterm foldcolumn=3 ttymouse=xterm2
Bram Moolenaar696d6372019-04-17 16:33:46 +0200875
876 " Create 2 nested folds.
877 call setline(1, range(1, 7))
878 2,6fold
879 norm! zR
880 4,5fold
881 call assert_equal([-1, -1, -1, 4, 4, -1, -1],
882 \ map(range(1, 7), 'foldclosed(v:val)'))
883
884 " Click in "+" of inner fold in foldcolumn should open it.
885 redraw
886 let row = 4
887 let col = 2
888 call MouseLeftClick(row, col)
889 call MouseLeftRelease(row, col)
890 call assert_equal([-1, -1, -1, -1, -1, -1, -1],
891 \ map(range(1, 7), 'foldclosed(v:val)'))
892
893 " Click in "-" of outer fold in foldcolumn should close it.
894 redraw
895 let row = 2
896 let col = 1
897 call MouseLeftClick(row, col)
898 call MouseLeftRelease(row, col)
899 call assert_equal([-1, 2, 2, 2, 2, 2, -1],
900 \ map(range(1, 7), 'foldclosed(v:val)'))
901 norm! zR
902
903 " Click in "|" of inner fold in foldcolumn should close it.
904 redraw
905 let row = 5
906 let col = 2
907 call MouseLeftClick(row, col)
908 call MouseLeftRelease(row, col)
909 call assert_equal([-1, -1, -1, 4, 4, -1, -1],
910 \ map(range(1, 7), 'foldclosed(v:val)'))
911
912 let &foldcolumn = save_foldcolumn
913 let &ttymouse = save_ttymouse
914 let &term = save_term
915 let &mouse = save_mouse
916 bwipe!
917endfunc
Bram Moolenaar66761db2019-06-05 22:07:51 +0200918
Bram Moolenaarf19f8d12019-12-18 19:36:23 +0100919" Left or right click in Ex command line sets position of the cursor.
920func Test_term_mouse_click_in_cmdline_to_set_pos()
921 let save_mouse = &mouse
922 let save_term = &term
923 let save_ttymouse = &ttymouse
924 call test_override('no_query_mouse', 1)
925 set mouse=a term=xterm
926 let row = &lines
927
928 for ttymouse_val in s:ttymouse_values + s:ttymouse_dec
929 let msg = 'ttymouse=' .. ttymouse_val
930 exe 'set ttymouse=' .. ttymouse_val
931
932 call feedkeys(':"3456789'
933 \ .. MouseLeftClickCode(row, 7)
934 \ .. MouseLeftReleaseCode(row, 7) .. 'L'
935 \ .. MouseRightClickCode(row, 4)
936 \ .. MouseRightReleaseCode(row, 4) .. 'R'
937 \ .. "\<CR>", 'Lx!')
938 call assert_equal('"3R456L789', @:, msg)
939 endfor
940
941 let &mouse = save_mouse
942 let &term = save_term
943 let &ttymouse = save_ttymouse
944 set mousetime&
945 call test_override('no_query_mouse', 0)
946endfunc
947
948" Middle click in command line pastes at position of cursor.
949func Test_term_mouse_middle_click_in_cmdline_to_paste()
950 CheckFeature clipboard_working
951 let save_mouse = &mouse
952 let save_term = &term
953 let save_ttymouse = &ttymouse
954 call test_override('no_query_mouse', 1)
955 set mouse=a term=xterm
956 let row = &lines
957 " Column values does not matter, paste is done at position of cursor.
958 let col = 1
959 let @* = 'paste'
960
961 for ttymouse_val in s:ttymouse_values + s:ttymouse_dec
962 let msg = 'ttymouse=' .. ttymouse_val
963 exe 'set ttymouse=' .. ttymouse_val
964
965 call feedkeys(":\"->"
966 \ .. MouseMiddleReleaseCode(row, col)
967 \ .. MouseMiddleClickCode(row, col)
968 \ .. "<-"
969 \ .. MouseMiddleReleaseCode(row, col)
970 \ .. MouseMiddleClickCode(row, col)
971 \ .. "\<CR>", 'Lx!')
972 call assert_equal('"->paste<-paste', @:, msg)
973 endfor
974
975 let &mouse = save_mouse
976 let &term = save_term
977 let &ttymouse = save_ttymouse
978 let @* = ''
979 call test_override('no_query_mouse', 0)
980endfunc
981
Bram Moolenaar0eabd4d2020-03-15 16:13:53 +0100982" Test for displaying the popup menu using the right mouse click
983func Test_mouse_popup_menu()
984 CheckFeature menu
985 new
986 call setline(1, 'popup menu test')
987 let save_mouse = &mouse
988 let save_term = &term
989 let save_ttymouse = &ttymouse
990 let save_mousemodel = &mousemodel
991 call test_override('no_query_mouse', 1)
992 set mouse=a term=xterm mousemodel=popup
993
994 menu PopUp.foo :let g:menustr = 'foo'<CR>
995 menu PopUp.bar :let g:menustr = 'bar'<CR>
996 menu PopUp.baz :let g:menustr = 'baz'<CR>
997
998 for ttymouse_val in s:ttymouse_values
999 exe 'set ttymouse=' .. ttymouse_val
1000 let g:menustr = ''
1001 call feedkeys(MouseRightClickCode(1, 4)
1002 \ .. MouseRightReleaseCode(1, 4) .. "\<Down>\<Down>\<CR>", "x")
1003 call assert_equal('bar', g:menustr)
1004 endfor
1005
1006 unmenu PopUp
1007 let &mouse = save_mouse
1008 let &term = save_term
1009 let &ttymouse = save_ttymouse
1010 let &mousemodel = save_mousemodel
1011 call test_override('no_query_mouse', 0)
1012 close!
1013endfunc
1014
Bram Moolenaar66761db2019-06-05 22:07:51 +02001015" This only checks if the sequence is recognized.
Bram Moolenaar66761db2019-06-05 22:07:51 +02001016func Test_term_rgb_response()
1017 set t_RF=x
1018 set t_RB=y
1019
1020 " response to t_RF, 4 digits
1021 let red = 0x12
1022 let green = 0x34
1023 let blue = 0x56
1024 let seq = printf("\<Esc>]10;rgb:%02x00/%02x00/%02x00\x07", red, green, blue)
1025 call feedkeys(seq, 'Lx!')
1026 call assert_equal(seq, v:termrfgresp)
1027
1028 " response to t_RF, 2 digits
1029 let red = 0x78
1030 let green = 0x9a
1031 let blue = 0xbc
1032 let seq = printf("\<Esc>]10;rgb:%02x/%02x/%02x\x07", red, green, blue)
1033 call feedkeys(seq, 'Lx!')
1034 call assert_equal(seq, v:termrfgresp)
1035
Bram Moolenaar32e19772019-06-05 22:57:04 +02001036 " response to t_RB, 4 digits, dark
1037 set background=light
Bram Moolenaarce90e362019-09-08 18:58:44 +02001038 eval 'background'->test_option_not_set()
Bram Moolenaar32e19772019-06-05 22:57:04 +02001039 let red = 0x29
1040 let green = 0x4a
1041 let blue = 0x6b
1042 let seq = printf("\<Esc>]11;rgb:%02x00/%02x00/%02x00\x07", red, green, blue)
1043 call feedkeys(seq, 'Lx!')
1044 call assert_equal(seq, v:termrbgresp)
1045 call assert_equal('dark', &background)
1046
1047 " response to t_RB, 4 digits, light
1048 set background=dark
1049 call test_option_not_set('background')
1050 let red = 0x81
1051 let green = 0x63
Bram Moolenaar66761db2019-06-05 22:07:51 +02001052 let blue = 0x65
1053 let seq = printf("\<Esc>]11;rgb:%02x00/%02x00/%02x00\x07", red, green, blue)
1054 call feedkeys(seq, 'Lx!')
1055 call assert_equal(seq, v:termrbgresp)
Bram Moolenaar32e19772019-06-05 22:57:04 +02001056 call assert_equal('light', &background)
Bram Moolenaar66761db2019-06-05 22:07:51 +02001057
Bram Moolenaar32e19772019-06-05 22:57:04 +02001058 " response to t_RB, 2 digits, dark
1059 set background=light
1060 call test_option_not_set('background')
1061 let red = 0x47
1062 let green = 0x59
1063 let blue = 0x5b
Bram Moolenaar66761db2019-06-05 22:07:51 +02001064 let seq = printf("\<Esc>]11;rgb:%02x/%02x/%02x\x07", red, green, blue)
1065 call feedkeys(seq, 'Lx!')
1066 call assert_equal(seq, v:termrbgresp)
Bram Moolenaar32e19772019-06-05 22:57:04 +02001067 call assert_equal('dark', &background)
Bram Moolenaarf19f8d12019-12-18 19:36:23 +01001068
Bram Moolenaar32e19772019-06-05 22:57:04 +02001069 " response to t_RB, 2 digits, light
1070 set background=dark
1071 call test_option_not_set('background')
1072 let red = 0x83
1073 let green = 0xa4
1074 let blue = 0xc2
1075 let seq = printf("\<Esc>]11;rgb:%02x/%02x/%02x\x07", red, green, blue)
1076 call feedkeys(seq, 'Lx!')
1077 call assert_equal(seq, v:termrbgresp)
1078 call assert_equal('light', &background)
Bram Moolenaarf19f8d12019-12-18 19:36:23 +01001079
Bram Moolenaar66761db2019-06-05 22:07:51 +02001080 set t_RF= t_RB=
1081endfunc
1082
1083" This only checks if the sequence is recognized.
Bram Moolenaar1a4cbb12019-10-12 13:25:44 +02001084" This must be after other tests, because it has side effects to xterm
1085" properties.
1086func Test_xx01_term_style_response()
Bram Moolenaar66761db2019-06-05 22:07:51 +02001087 " Termresponse is only parsed when t_RV is not empty.
1088 set t_RV=x
1089
1090 " send the termresponse to trigger requesting the XT codes
1091 let seq = "\<Esc>[>41;337;0c"
1092 call feedkeys(seq, 'Lx!')
1093 call assert_equal(seq, v:termresponse)
1094
1095 let seq = "\<Esc>P1$r2 q\<Esc>\\"
1096 call feedkeys(seq, 'Lx!')
1097 call assert_equal(seq, v:termstyleresp)
1098
1099 set t_RV=
1100endfunc
Bram Moolenaarde6dbb42019-06-06 11:59:18 +02001101
Bram Moolenaar89577b32019-10-18 21:26:05 +02001102" This checks the iTerm2 version response.
1103" This must be after other tests, because it has side effects to xterm
1104" properties.
1105func Test_xx02_iTerm2_response()
1106 " Termresponse is only parsed when t_RV is not empty.
1107 set t_RV=x
1108
1109 " Old versions of iTerm2 used a different style term response.
1110 set ttymouse=xterm
1111 call test_option_not_set('ttymouse')
1112 let seq = "\<Esc>[>0;95;c"
1113 call feedkeys(seq, 'Lx!')
1114 call assert_equal(seq, v:termresponse)
1115 call assert_equal('xterm', &ttymouse)
1116
1117 set ttymouse=xterm
1118 call test_option_not_set('ttymouse')
1119 let seq = "\<Esc>[>0;95;0c"
1120 call feedkeys(seq, 'Lx!')
1121 call assert_equal(seq, v:termresponse)
1122 call assert_equal('sgr', &ttymouse)
1123
1124 set t_RV=
1125endfunc
1126
Bram Moolenaar1a4cbb12019-10-12 13:25:44 +02001127" This checks the libvterm version response.
1128" This must be after other tests, because it has side effects to xterm
1129" properties.
Bram Moolenaar89577b32019-10-18 21:26:05 +02001130func Test_xx03_libvterm_response()
Bram Moolenaar1a4cbb12019-10-12 13:25:44 +02001131 " Termresponse is only parsed when t_RV is not empty.
1132 set t_RV=x
Bram Moolenaar03b00472019-10-14 22:22:03 +02001133
Bram Moolenaar1a4cbb12019-10-12 13:25:44 +02001134 set ttymouse=xterm
1135 call test_option_not_set('ttymouse')
Bram Moolenaar1a4cbb12019-10-12 13:25:44 +02001136 let seq = "\<Esc>[>0;100;0c"
1137 call feedkeys(seq, 'Lx!')
1138 call assert_equal(seq, v:termresponse)
1139 call assert_equal('sgr', &ttymouse)
1140
1141 set t_RV=
1142endfunc
1143
Bram Moolenaar89577b32019-10-18 21:26:05 +02001144" This checks the Mac Terminal.app version response.
1145" This must be after other tests, because it has side effects to xterm
1146" properties.
1147func Test_xx04_Mac_Terminal_response()
1148 " Termresponse is only parsed when t_RV is not empty.
1149 set t_RV=x
1150
1151 set ttymouse=xterm
1152 call test_option_not_set('ttymouse')
1153 let seq = "\<Esc>[>1;95;0c"
1154 call feedkeys(seq, 'Lx!')
1155 call assert_equal(seq, v:termresponse)
1156 call assert_equal('sgr', &ttymouse)
1157
1158 " Reset is_not_xterm and is_mac_terminal.
1159 set t_RV=
1160 set term=xterm
1161 set t_RV=x
1162endfunc
1163
1164" This checks the mintty version response.
1165" This must be after other tests, because it has side effects to xterm
1166" properties.
1167func Test_xx05_mintty_response()
1168 " Termresponse is only parsed when t_RV is not empty.
1169 set t_RV=x
1170
1171 set ttymouse=xterm
1172 call test_option_not_set('ttymouse')
1173 let seq = "\<Esc>[>77;20905;0c"
1174 call feedkeys(seq, 'Lx!')
1175 call assert_equal(seq, v:termresponse)
1176 call assert_equal('sgr', &ttymouse)
1177
1178 set t_RV=
1179endfunc
1180
1181" This checks the screen version response.
1182" This must be after other tests, because it has side effects to xterm
1183" properties.
1184func Test_xx06_screen_response()
1185 " Termresponse is only parsed when t_RV is not empty.
1186 set t_RV=x
1187
1188 " Old versions of screen don't support SGR mouse mode.
1189 set ttymouse=xterm
1190 call test_option_not_set('ttymouse')
1191 let seq = "\<Esc>[>83;40500;0c"
1192 call feedkeys(seq, 'Lx!')
1193 call assert_equal(seq, v:termresponse)
1194 call assert_equal('xterm', &ttymouse)
1195
1196 " screen supports SGR mouse mode starting in version 4.7.
1197 set ttymouse=xterm
1198 call test_option_not_set('ttymouse')
1199 let seq = "\<Esc>[>83;40700;0c"
1200 call feedkeys(seq, 'Lx!')
1201 call assert_equal(seq, v:termresponse)
1202 call assert_equal('sgr', &ttymouse)
1203
1204 set t_RV=
1205endfunc
1206
Bram Moolenaar03b00472019-10-14 22:22:03 +02001207" This checks the xterm version response.
1208" This must be after other tests, because it has side effects to xterm
1209" properties.
Bram Moolenaar89577b32019-10-18 21:26:05 +02001210func Test_xx07_xterm_response()
Bram Moolenaar03b00472019-10-14 22:22:03 +02001211 " Termresponse is only parsed when t_RV is not empty.
1212 set t_RV=x
1213
Bram Moolenaar3cea8a92019-10-17 21:55:24 +02001214 " Do Terminal.app first to check that is_mac_terminal is reset.
1215 set ttymouse=xterm
1216 call test_option_not_set('ttymouse')
1217 let seq = "\<Esc>[>1;95;0c"
1218 call feedkeys(seq, 'Lx!')
1219 call assert_equal(seq, v:termresponse)
1220 call assert_equal('sgr', &ttymouse)
1221
Bram Moolenaar03b00472019-10-14 22:22:03 +02001222 " xterm < 95: "xterm" (actually unmodified)
Bram Moolenaar3cea8a92019-10-17 21:55:24 +02001223 set t_RV=
1224 set term=xterm
1225 set t_RV=x
Bram Moolenaar03b00472019-10-14 22:22:03 +02001226 set ttymouse=xterm
1227 call test_option_not_set('ttymouse')
1228 let seq = "\<Esc>[>0;94;0c"
1229 call feedkeys(seq, 'Lx!')
1230 call assert_equal(seq, v:termresponse)
1231 call assert_equal('xterm', &ttymouse)
1232
1233 " xterm >= 95 < 277 "xterm2"
1234 set ttymouse=xterm
1235 call test_option_not_set('ttymouse')
1236 let seq = "\<Esc>[>0;267;0c"
1237 call feedkeys(seq, 'Lx!')
1238 call assert_equal(seq, v:termresponse)
1239 call assert_equal('xterm2', &ttymouse)
1240
1241 " xterm >= 277: "sgr"
1242 set ttymouse=xterm
1243 call test_option_not_set('ttymouse')
1244 let seq = "\<Esc>[>0;277;0c"
1245 call feedkeys(seq, 'Lx!')
1246 call assert_equal(seq, v:termresponse)
1247 call assert_equal('sgr', &ttymouse)
1248
1249 set t_RV=
1250endfunc
1251
Bram Moolenaarde6dbb42019-06-06 11:59:18 +02001252func Test_get_termcode()
Bram Moolenaareb663282019-10-06 12:02:15 +02001253 try
1254 let k1 = &t_k1
1255 catch /E113/
1256 throw 'Skipped: Unable to query termcodes'
1257 endtry
Bram Moolenaarde6dbb42019-06-06 11:59:18 +02001258 set t_k1=
1259 set t_k1&
1260 call assert_equal(k1, &t_k1)
Bram Moolenaar9aeb3362019-06-06 12:36:15 +02001261
1262 " use external termcap first
1263 set nottybuiltin
1264 set t_k1=
1265 set t_k1&
1266 " when using external termcap may get something else, but it must not be
1267 " empty, since we would fallback to the builtin one.
1268 call assert_notequal('', &t_k1)
1269
1270 if &term =~ 'xterm'
1271 " use internal termcap first
1272 let term_save = &term
1273 let &term = 'builtin_' .. &term
1274 set t_k1=
1275 set t_k1&
1276 call assert_equal(k1, &t_k1)
1277 let &term = term_save
1278 endif
1279
1280 set ttybuiltin
Bram Moolenaarde6dbb42019-06-06 11:59:18 +02001281endfunc
Bram Moolenaar18a79a62019-10-12 15:36:11 +02001282
1283func GetEscCodeCSI27(key, modifier)
1284 let key = printf("%d", char2nr(a:key))
1285 let mod = printf("%d", a:modifier)
1286 return "\<Esc>[27;" .. mod .. ';' .. key .. '~'
1287endfunc
1288
1289func GetEscCodeCSIu(key, modifier)
1290 let key = printf("%d", char2nr(a:key))
1291 let mod = printf("%d", a:modifier)
1292 return "\<Esc>[" .. key .. ';' .. mod .. 'u'
1293endfunc
1294
1295" This checks the CSI sequences when in modifyOtherKeys mode.
1296" The mode doesn't need to be enabled, the codes are always detected.
1297func RunTest_modifyOtherKeys(func)
1298 new
Bram Moolenaar459fd782019-10-13 16:43:39 +02001299 set timeoutlen=10
Bram Moolenaar18a79a62019-10-12 15:36:11 +02001300
Bram Moolenaarf19f8d12019-12-18 19:36:23 +01001301 " Shift-X is sent as 'X' with the shift modifier
Bram Moolenaar18a79a62019-10-12 15:36:11 +02001302 call feedkeys('a' .. a:func('X', 2) .. "\<Esc>", 'Lx!')
1303 call assert_equal('X', getline(1))
1304
1305 " Ctrl-i is Tab
1306 call setline(1, '')
1307 call feedkeys('a' .. a:func('i', 5) .. "\<Esc>", 'Lx!')
1308 call assert_equal("\t", getline(1))
1309
1310 " Ctrl-I is also Tab
1311 call setline(1, '')
1312 call feedkeys('a' .. a:func('I', 5) .. "\<Esc>", 'Lx!')
1313 call assert_equal("\t", getline(1))
1314
1315 " Alt-x is ø
1316 call setline(1, '')
1317 call feedkeys('a' .. a:func('x', 3) .. "\<Esc>", 'Lx!')
1318 call assert_equal("ø", getline(1))
1319
1320 " Meta-x is also ø
1321 call setline(1, '')
1322 call feedkeys('a' .. a:func('x', 9) .. "\<Esc>", 'Lx!')
1323 call assert_equal("ø", getline(1))
1324
1325 " Alt-X is Ø
1326 call setline(1, '')
1327 call feedkeys('a' .. a:func('X', 3) .. "\<Esc>", 'Lx!')
1328 call assert_equal("Ø", getline(1))
1329
1330 " Meta-X is ø
1331 call setline(1, '')
1332 call feedkeys('a' .. a:func('X', 9) .. "\<Esc>", 'Lx!')
1333 call assert_equal("Ø", getline(1))
1334
Bram Moolenaar828ffd52019-11-21 23:24:00 +01001335 " Ctrl-6 is Ctrl-^
1336 split aaa
1337 edit bbb
1338 call feedkeys(a:func('6', 5), 'Lx!')
1339 call assert_equal("aaa", bufname())
1340 bwipe aaa
1341 bwipe bbb
1342
Bram Moolenaar18a79a62019-10-12 15:36:11 +02001343 bwipe!
1344 set timeoutlen&
1345endfunc
1346
Bram Moolenaar459fd782019-10-13 16:43:39 +02001347func Test_modifyOtherKeys_basic()
Bram Moolenaar18a79a62019-10-12 15:36:11 +02001348 call RunTest_modifyOtherKeys(function('GetEscCodeCSI27'))
Bram Moolenaar18a79a62019-10-12 15:36:11 +02001349 call RunTest_modifyOtherKeys(function('GetEscCodeCSIu'))
1350endfunc
Bram Moolenaard1e2f392019-10-12 18:22:50 +02001351
Bram Moolenaar38571a02019-11-26 14:28:15 +01001352func Test_modifyOtherKeys_no_mapping()
1353 set timeoutlen=10
1354
1355 let @a = 'aaa'
1356 call feedkeys(":let x = '" .. GetEscCodeCSI27('R', 5) .. GetEscCodeCSI27('R', 5) .. "a'\<CR>", 'Lx!')
1357 call assert_equal("let x = 'aaa'", @:)
1358
1359 new
1360 call feedkeys("a" .. GetEscCodeCSI27('R', 5) .. GetEscCodeCSI27('R', 5) .. "a\<Esc>", 'Lx!')
1361 call assert_equal("aaa", getline(1))
1362 bwipe!
1363
1364 new
1365 call feedkeys("axx\<CR>yy" .. GetEscCodeCSI27('G', 5) .. GetEscCodeCSI27('K', 5) .. "a\<Esc>", 'Lx!')
1366 call assert_equal("axx", getline(1))
1367 call assert_equal("yy", getline(2))
1368 bwipe!
1369
1370 set timeoutlen&
1371endfunc
1372
Bram Moolenaard1e2f392019-10-12 18:22:50 +02001373func RunTest_mapping_shift(key, func)
1374 call setline(1, '')
1375 if a:key == '|'
1376 exe 'inoremap \| xyz'
1377 else
1378 exe 'inoremap ' .. a:key .. ' xyz'
1379 endif
1380 call feedkeys('a' .. a:func(a:key, 2) .. "\<Esc>", 'Lx!')
1381 call assert_equal("xyz", getline(1))
1382 if a:key == '|'
1383 exe 'iunmap \|'
1384 else
1385 exe 'iunmap ' .. a:key
1386 endif
1387endfunc
1388
1389func RunTest_mapping_works_with_shift(func)
1390 new
Bram Moolenaar459fd782019-10-13 16:43:39 +02001391 set timeoutlen=10
Bram Moolenaard1e2f392019-10-12 18:22:50 +02001392
1393 call RunTest_mapping_shift('@', a:func)
1394 call RunTest_mapping_shift('A', a:func)
1395 call RunTest_mapping_shift('Z', a:func)
1396 call RunTest_mapping_shift('^', a:func)
1397 call RunTest_mapping_shift('_', a:func)
1398 call RunTest_mapping_shift('{', a:func)
1399 call RunTest_mapping_shift('|', a:func)
1400 call RunTest_mapping_shift('}', a:func)
1401 call RunTest_mapping_shift('~', a:func)
1402
1403 bwipe!
1404 set timeoutlen&
1405endfunc
1406
Bram Moolenaar459fd782019-10-13 16:43:39 +02001407func Test_mapping_works_with_shift_plain()
Bram Moolenaard1e2f392019-10-12 18:22:50 +02001408 call RunTest_mapping_works_with_shift(function('GetEscCodeCSI27'))
1409 call RunTest_mapping_works_with_shift(function('GetEscCodeCSIu'))
1410endfunc
Bram Moolenaar459fd782019-10-13 16:43:39 +02001411
1412func RunTest_mapping_mods(map, key, func, code)
1413 call setline(1, '')
1414 exe 'inoremap ' .. a:map .. ' xyz'
1415 call feedkeys('a' .. a:func(a:key, a:code) .. "\<Esc>", 'Lx!')
1416 call assert_equal("xyz", getline(1))
1417 exe 'iunmap ' .. a:map
1418endfunc
1419
1420func RunTest_mapping_works_with_mods(func, mods, code)
1421 new
1422 set timeoutlen=10
1423
1424 if a:mods !~ 'S'
1425 " Shift by itself has no effect
1426 call RunTest_mapping_mods('<' .. a:mods .. '-@>', '@', a:func, a:code)
1427 endif
1428 call RunTest_mapping_mods('<' .. a:mods .. '-A>', 'A', a:func, a:code)
1429 call RunTest_mapping_mods('<' .. a:mods .. '-Z>', 'Z', a:func, a:code)
1430 if a:mods !~ 'S'
1431 " with Shift code is always upper case
1432 call RunTest_mapping_mods('<' .. a:mods .. '-a>', 'a', a:func, a:code)
1433 call RunTest_mapping_mods('<' .. a:mods .. '-z>', 'z', a:func, a:code)
1434 endif
1435 if a:mods != 'A'
1436 " with Alt code is not in upper case
1437 call RunTest_mapping_mods('<' .. a:mods .. '-a>', 'A', a:func, a:code)
1438 call RunTest_mapping_mods('<' .. a:mods .. '-z>', 'Z', a:func, a:code)
1439 endif
1440 call RunTest_mapping_mods('<' .. a:mods .. '-á>', 'á', a:func, a:code)
1441 if a:mods !~ 'S'
1442 " Shift by itself has no effect
1443 call RunTest_mapping_mods('<' .. a:mods .. '-^>', '^', a:func, a:code)
1444 call RunTest_mapping_mods('<' .. a:mods .. '-_>', '_', a:func, a:code)
1445 call RunTest_mapping_mods('<' .. a:mods .. '-{>', '{', a:func, a:code)
1446 call RunTest_mapping_mods('<' .. a:mods .. '-\|>', '|', a:func, a:code)
1447 call RunTest_mapping_mods('<' .. a:mods .. '-}>', '}', a:func, a:code)
1448 call RunTest_mapping_mods('<' .. a:mods .. '-~>', '~', a:func, a:code)
1449 endif
1450
1451 bwipe!
1452 set timeoutlen&
1453endfunc
1454
1455func Test_mapping_works_with_shift()
1456 call RunTest_mapping_works_with_mods(function('GetEscCodeCSI27'), 'S', 2)
1457 call RunTest_mapping_works_with_mods(function('GetEscCodeCSIu'), 'S', 2)
1458endfunc
Bram Moolenaarf19f8d12019-12-18 19:36:23 +01001459
Bram Moolenaar459fd782019-10-13 16:43:39 +02001460func Test_mapping_works_with_ctrl()
1461 call RunTest_mapping_works_with_mods(function('GetEscCodeCSI27'), 'C', 5)
1462 call RunTest_mapping_works_with_mods(function('GetEscCodeCSIu'), 'C', 5)
1463endfunc
1464
1465func Test_mapping_works_with_shift_ctrl()
1466 call RunTest_mapping_works_with_mods(function('GetEscCodeCSI27'), 'C-S', 6)
1467 call RunTest_mapping_works_with_mods(function('GetEscCodeCSIu'), 'C-S', 6)
1468endfunc
1469
1470" Below we also test the "u" code with Alt, This works, but libvterm would not
1471" send the Alt key like this but by prefixing an Esc.
Bram Moolenaarf19f8d12019-12-18 19:36:23 +01001472
Bram Moolenaar459fd782019-10-13 16:43:39 +02001473func Test_mapping_works_with_alt()
1474 call RunTest_mapping_works_with_mods(function('GetEscCodeCSI27'), 'A', 3)
1475 call RunTest_mapping_works_with_mods(function('GetEscCodeCSIu'), 'A', 3)
1476endfunc
1477
1478func Test_mapping_works_with_shift_alt()
1479 call RunTest_mapping_works_with_mods(function('GetEscCodeCSI27'), 'S-A', 4)
1480 call RunTest_mapping_works_with_mods(function('GetEscCodeCSIu'), 'S-A', 4)
1481endfunc
1482
1483func Test_mapping_works_with_ctrl_alt()
1484 call RunTest_mapping_works_with_mods(function('GetEscCodeCSI27'), 'C-A', 7)
1485 call RunTest_mapping_works_with_mods(function('GetEscCodeCSIu'), 'C-A', 7)
1486endfunc
1487
1488func Test_mapping_works_with_shift_ctrl_alt()
1489 call RunTest_mapping_works_with_mods(function('GetEscCodeCSI27'), 'C-S-A', 8)
1490 call RunTest_mapping_works_with_mods(function('GetEscCodeCSIu'), 'C-S-A', 8)
1491endfunc
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001492
1493func Test_insert_literal()
1494 set timeoutlen=10
1495 new
1496 " CTRL-V CTRL-X inserts a ^X
1497 call feedkeys('a' .. GetEscCodeCSIu('V', '5') .. GetEscCodeCSIu('X', '5') .. "\<Esc>", 'Lx!')
1498 call assert_equal("\<C-X>", getline(1))
1499
1500 call setline(1, '')
1501 call feedkeys('a' .. GetEscCodeCSI27('V', '5') .. GetEscCodeCSI27('X', '5') .. "\<Esc>", 'Lx!')
1502 call assert_equal("\<C-X>", getline(1))
1503
Bram Moolenaarf19f8d12019-12-18 19:36:23 +01001504 " CTRL-SHIFT-V CTRL-X inserts escape sequence
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001505 call setline(1, '')
1506 call feedkeys('a' .. GetEscCodeCSIu('V', '6') .. GetEscCodeCSIu('X', '5') .. "\<Esc>", 'Lx!')
1507 call assert_equal("\<Esc>[88;5u", getline(1))
1508
1509 call setline(1, '')
1510 call feedkeys('a' .. GetEscCodeCSI27('V', '6') .. GetEscCodeCSI27('X', '5') .. "\<Esc>", 'Lx!')
1511 call assert_equal("\<Esc>[27;5;88~", getline(1))
1512
1513 bwipe!
1514 set timeoutlen&
1515endfunc
1516
1517func Test_cmdline_literal()
1518 set timeoutlen=10
1519
1520 " CTRL-V CTRL-Y inserts a ^Y
1521 call feedkeys(':' .. GetEscCodeCSIu('V', '5') .. GetEscCodeCSIu('Y', '5') .. "\<C-B>\"\<CR>", 'Lx!')
1522 call assert_equal("\"\<C-Y>", @:)
1523
1524 call feedkeys(':' .. GetEscCodeCSI27('V', '5') .. GetEscCodeCSI27('Y', '5') .. "\<C-B>\"\<CR>", 'Lx!')
1525 call assert_equal("\"\<C-Y>", @:)
1526
Bram Moolenaarf19f8d12019-12-18 19:36:23 +01001527 " CTRL-SHIFT-V CTRL-Y inserts escape sequence
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001528 call feedkeys(':' .. GetEscCodeCSIu('V', '6') .. GetEscCodeCSIu('Y', '5') .. "\<C-B>\"\<CR>", 'Lx!')
1529 call assert_equal("\"\<Esc>[89;5u", @:)
1530
1531 call setline(1, '')
1532 call feedkeys(':' .. GetEscCodeCSI27('V', '6') .. GetEscCodeCSI27('Y', '5') .. "\<C-B>\"\<CR>", 'Lx!')
1533 call assert_equal("\"\<Esc>[27;5;89~", @:)
1534
1535 set timeoutlen&
1536endfunc
Bram Moolenaar0eabd4d2020-03-15 16:13:53 +01001537
1538" vim: shiftwidth=2 sts=2 expandtab