blob: ddde7da907b0d4d291c3a9449f7e8270cce68048 [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
4if has('gui_running') || !has('unix')
5 finish
6endif
7
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +02008" Helper function to emit a terminal escape code.
9func TerminalEscapeCode(code_xterm, code_sgr, row, col, m)
10 if &ttymouse ==# 'xterm'
11 " need to use byte encoding here.
12 let str = list2str([a:code_xterm, a:col + 0x20, a:row + 0x20])
13 if has('iconv')
14 let bytes = iconv(str, 'utf-8', 'latin1')
15 else
16 " Hopefully the numbers are not too big.
17 let bytes = str
18 endif
19 call feedkeys("\<Esc>[M" .. bytes, 'Lx!')
20 elseif &ttymouse ==# 'sgr'
21 call feedkeys(printf("\<Esc>[<%d;%d;%d%s", a:code_sgr, a:col, a:row, a:m), 'Lx!')
22 endif
23endfunc
24
25func MouseLeftClick(row, col)
26 call TerminalEscapeCode(0x20, 0, a:row, a:col, 'M')
27endfunc
28
29func MouseLeftRelease(row, col)
30 call TerminalEscapeCode(0x23, 3, a:row, a:col, 'm')
31endfunc
32
33func MouseLeftDrag(row, col)
34 call TerminalEscapeCode(0x43, 0x20, a:row, a:col, 'M')
35endfunc
36
37func MouseWheelUp(row, col)
38 call TerminalEscapeCode(0x40, 0x40, a:row, a:col, 'M')
39endfunc
40
41func MouseWheelDown(row, col)
42 call TerminalEscapeCode(0x41, 0x41, a:row, a:col, 'M')
43endfunc
44
Bram Moolenaar905dd902019-04-07 14:21:47 +020045func Test_xterm_mouse_click()
46 new
47 let save_mouse = &mouse
48 let save_term = &term
49 let save_ttymouse = &ttymouse
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +020050 set mouse=a term=xterm
Bram Moolenaar905dd902019-04-07 14:21:47 +020051 call setline(1, ['line 1', 'line 2', 'line 3 is a bit longer'])
Bram Moolenaar905dd902019-04-07 14:21:47 +020052
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +020053 for ttymouse_val in ['xterm', 'sgr']
54 exe 'set ttymouse=' . ttymouse_val
55 go
56 call assert_equal([0, 1, 1, 0], getpos('.'))
57 let row = 2
58 let col = 6
59 call MouseLeftClick(row, col)
60 call MouseLeftRelease(row, col)
61 call assert_equal([0, 2, 6, 0], getpos('.'))
62 endfor
Bram Moolenaar905dd902019-04-07 14:21:47 +020063
64 let &mouse = save_mouse
65 let &term = save_term
66 let &ttymouse = save_ttymouse
67 bwipe!
68endfunc
69
Bram Moolenaar049736f2019-04-07 21:55:07 +020070func Test_xterm_mouse_wheel()
71 new
72 let save_mouse = &mouse
73 let save_term = &term
74 let save_ttymouse = &ttymouse
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +020075 set mouse=a term=xterm
Bram Moolenaar049736f2019-04-07 21:55:07 +020076 call setline(1, range(1, 100))
77
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +020078 for ttymouse_val in ['xterm', 'sgr']
79 exe 'set ttymouse=' . ttymouse_val
80 go
81 call assert_equal(1, line('w0'))
82 call assert_equal([0, 1, 1, 0], getpos('.'))
Bram Moolenaar049736f2019-04-07 21:55:07 +020083
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +020084 call MouseWheelDown(1, 1)
85 call assert_equal(4, line('w0'))
86 call assert_equal([0, 4, 1, 0], getpos('.'))
Bram Moolenaar049736f2019-04-07 21:55:07 +020087
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +020088 call MouseWheelDown(1, 1)
89 call assert_equal(7, line('w0'))
90 call assert_equal([0, 7, 1, 0], getpos('.'))
Bram Moolenaar049736f2019-04-07 21:55:07 +020091
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +020092 call MouseWheelUp(1, 1)
93 call assert_equal(4, line('w0'))
94 call assert_equal([0, 7, 1, 0], getpos('.'))
Bram Moolenaar049736f2019-04-07 21:55:07 +020095
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +020096 call MouseWheelUp(1, 1)
97 call assert_equal(1, line('w0'))
98 call assert_equal([0, 7, 1, 0], getpos('.'))
99 endfor
Bram Moolenaar049736f2019-04-07 21:55:07 +0200100
101 let &mouse = save_mouse
102 let &term = save_term
103 let &ttymouse = save_ttymouse
104 bwipe!
105endfunc
Bram Moolenaar3fb01a52019-04-09 21:52:02 +0200106
107func Test_xterm_mouse_drag_window_separator()
108 let save_mouse = &mouse
109 let save_term = &term
110 let save_ttymouse = &ttymouse
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200111 set mouse=a term=xterm
Bram Moolenaar3fb01a52019-04-09 21:52:02 +0200112
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200113 for ttymouse_val in ['xterm', 'sgr']
114 exe 'set ttymouse=' . ttymouse_val
Bram Moolenaar3fb01a52019-04-09 21:52:02 +0200115
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200116 " Split horizontally and test dragging the horizontal window separator.
117 split
118 let rowseparator = winheight(0) + 1
119 let row = rowseparator
120 let col = 1
Bram Moolenaarc8b3dda2019-04-12 21:42:52 +0200121
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200122 " When 'ttymouse' is 'xterm', row/col bigger than 223 are not supported.
123 if ttymouse_val !=# 'xterm' || row <= 223
124 call MouseLeftClick(row, col)
125 let row -= 1
126 call MouseLeftDrag(row, col)
127 call assert_equal(rowseparator - 1, winheight(0) + 1)
128 let row += 1
129 call MouseLeftDrag(row, col)
130 call assert_equal(rowseparator, winheight(0) + 1)
131 call MouseLeftRelease(row, col)
132 call assert_equal(rowseparator, winheight(0) + 1)
Bram Moolenaarc8b3dda2019-04-12 21:42:52 +0200133 endif
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200134 bwipe!
Bram Moolenaar3fb01a52019-04-09 21:52:02 +0200135
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200136 " Split vertically and test dragging the vertical window separator.
137 vsplit
138 let colseparator = winwidth(0) + 1
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200139 let row = 1
140 let col = colseparator
Bram Moolenaar3fb01a52019-04-09 21:52:02 +0200141
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200142 " When 'ttymouse' is 'xterm', row/col bigger than 223 are not supported.
143 if ttymouse_val !=# 'xterm' || col <= 223
144 call MouseLeftClick(row, col)
145 let col -= 1
146 call MouseLeftDrag(row, col)
147 call assert_equal(colseparator - 1, winwidth(0) + 1)
148 let col += 1
149 call MouseLeftDrag(row, col)
150 call assert_equal(colseparator, winwidth(0) + 1)
151 call MouseLeftRelease(row, col)
152 call assert_equal(colseparator, winwidth(0) + 1)
153 endif
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200154 bwipe!
155 endfor
Bram Moolenaar3fb01a52019-04-09 21:52:02 +0200156
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200157 let &mouse = save_mouse
158 let &term = save_term
159 let &ttymouse = save_ttymouse
160endfunc
Bram Moolenaar3fb01a52019-04-09 21:52:02 +0200161
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200162func Test_xterm_mouse_drag_statusline()
163 let save_mouse = &mouse
164 let save_term = &term
165 let save_ttymouse = &ttymouse
Bram Moolenaarca57ab52019-04-13 14:53:16 +0200166 let save_laststatus = &laststatus
167 set mouse=a term=xterm laststatus=2
Bram Moolenaar3fb01a52019-04-09 21:52:02 +0200168
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200169 for ttymouse_val in ['xterm', 'sgr']
170 exe 'set ttymouse=' . ttymouse_val
171
172 call assert_equal(1, &cmdheight)
173 let rowstatusline = winheight(0) + 1
174 let row = rowstatusline
175 let col = 1
Bram Moolenaarc8b3dda2019-04-12 21:42:52 +0200176
177 if ttymouse_val ==# 'xterm' && row > 223
178 " When 'ttymouse' is 'xterm', row/col bigger than 223 are not supported.
179 continue
180 endif
181
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200182 call MouseLeftClick(row, col)
183 let row -= 1
184 call MouseLeftDrag(row, col)
185 call assert_equal(2, &cmdheight)
186 call assert_equal(rowstatusline - 1, winheight(0) + 1)
187 let row += 1
188 call MouseLeftDrag(row, col)
189 call assert_equal(1, &cmdheight)
190 call assert_equal(rowstatusline, winheight(0) + 1)
191 call MouseLeftRelease(row, col)
192 call assert_equal(1, &cmdheight)
193 call assert_equal(rowstatusline, winheight(0) + 1)
194 endfor
195
Bram Moolenaar3fb01a52019-04-09 21:52:02 +0200196 let &mouse = save_mouse
197 let &term = save_term
198 let &ttymouse = save_ttymouse
Bram Moolenaarca57ab52019-04-13 14:53:16 +0200199 let &laststatus = save_laststatus
200endfunc
201
202func Test_xterm_mouse_click_tab()
203 let save_mouse = &mouse
204 let save_term = &term
205 let save_ttymouse = &ttymouse
206 set mouse=a term=xterm
207 let row = 1
208
209 for ttymouse_val in ['xterm', 'sgr']
210 exe 'set ttymouse=' . ttymouse_val
211 e Xfoo
212 tabnew Xbar
213
214 let a = split(execute(':tabs'), "\n")
215 call assert_equal(['Tab page 1',
216 \ ' Xfoo',
217 \ 'Tab page 2',
218 \ '> Xbar'], a)
219
220 " Test clicking on tab names in the tabline at the top.
221 let col = 2
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200222 redraw
Bram Moolenaarca57ab52019-04-13 14:53:16 +0200223 call MouseLeftClick(row, col)
224 call MouseLeftRelease(row, col)
225 let a = split(execute(':tabs'), "\n")
226 call assert_equal(['Tab page 1',
227 \ '> Xfoo',
228 \ 'Tab page 2',
229 \ ' Xbar'], a)
230
231 let col = 9
232 call MouseLeftClick(row, col)
233 call MouseLeftRelease(row, col)
234 let a = split(execute(':tabs'), "\n")
235 call assert_equal(['Tab page 1',
236 \ ' Xfoo',
237 \ 'Tab page 2',
238 \ '> Xbar'], a)
239
240 %bwipe!
241 endfor
242
243 let &mouse = save_mouse
244 let &term = save_term
245 let &ttymouse = save_ttymouse
Bram Moolenaar3fb01a52019-04-09 21:52:02 +0200246endfunc
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200247
248func Test_xterm_mouse_click_X_to_close_tab()
249 let save_mouse = &mouse
250 let save_term = &term
251 let save_ttymouse = &ttymouse
252 set mouse=a term=xterm
253 let row = 1
254 let col = &columns
255
256 for ttymouse_val in ['xterm', 'sgr']
257 if ttymouse_val ==# 'xterm' && col > 223
258 " When 'ttymouse' is 'xterm', row/col bigger than 223 are not supported.
259 continue
260 endif
261 exe 'set ttymouse=' . ttymouse_val
262 e Xtab1
263 tabnew Xtab2
264 tabnew Xtab3
265 tabn 2
266
267 let a = split(execute(':tabs'), "\n")
268 call assert_equal(['Tab page 1',
269 \ ' Xtab1',
270 \ 'Tab page 2',
271 \ '> Xtab2',
272 \ 'Tab page 3',
273 \ ' Xtab3'], a)
274
275 " Click on "X" in tabline to close current tab i.e. Xtab2.
276 redraw
277 call MouseLeftClick(row, col)
278 call MouseLeftRelease(row, col)
279 let a = split(execute(':tabs'), "\n")
280 call assert_equal(['Tab page 1',
281 \ ' Xtab1',
282 \ 'Tab page 2',
283 \ '> Xtab3'], a)
284
285 %bwipe!
286 endfor
287
288 let &mouse = save_mouse
289 let &term = save_term
290 let &ttymouse = save_ttymouse
291endfunc
Bram Moolenaare3e38282019-04-15 20:55:31 +0200292
293func Test_xterm_mouse_drag_to_move_tab()
294 let save_mouse = &mouse
295 let save_term = &term
296 let save_ttymouse = &ttymouse
297 " Set 'mousetime' to 1 to avoid recognizing a double-click in the loop
298 set mouse=a term=xterm mousetime=1
299 let row = 1
300
301 for ttymouse_val in ['xterm', 'sgr']
302 exe 'set ttymouse=' . ttymouse_val
303 e Xtab1
304 tabnew Xtab2
305
306 let a = split(execute(':tabs'), "\n")
307 call assert_equal(['Tab page 1',
308 \ ' Xtab1',
309 \ 'Tab page 2',
310 \ '> Xtab2'], a)
311 redraw
312
313 " Click in tab2 and drag it to tab1.
314 " Check getcharmod() to verify that click is not
315 " interpreted as a spurious double-click.
316 call MouseLeftClick(row, 10)
317 call assert_equal(0, getcharmod())
318 for col in [9, 8, 7, 6]
319 call MouseLeftDrag(row, col)
320 endfor
321 call MouseLeftRelease(row, col)
322 let a = split(execute(':tabs'), "\n")
323 call assert_equal(['Tab page 1',
324 \ '> Xtab2',
325 \ 'Tab page 2',
326 \ ' Xtab1'], a)
327
Bram Moolenaar7f279762019-04-15 21:48:22 +0200328 " brief sleep to avoid causing a double-click
329 sleep 20m
Bram Moolenaare3e38282019-04-15 20:55:31 +0200330 %bwipe!
331 endfor
332
333 let &mouse = save_mouse
334 let &term = save_term
335 let &ttymouse = save_ttymouse
336 set mousetime&
337endfunc
338
339func Test_xterm_mouse_double_click_to_create_tab()
340 let save_mouse = &mouse
341 let save_term = &term
342 let save_ttymouse = &ttymouse
343 " Set 'mousetime' to a small value, so that double-click works but we don't
344 " have to wait long to avoid a triple-click.
345 set mouse=a term=xterm mousetime=100
346 let row = 1
347 let col = 10
348
349 for ttymouse_val in ['xterm', 'sgr']
350 exe 'set ttymouse=' . ttymouse_val
351 e Xtab1
352 tabnew Xtab2
353
354 let a = split(execute(':tabs'), "\n")
355 call assert_equal(['Tab page 1',
356 \ ' Xtab1',
357 \ 'Tab page 2',
358 \ '> Xtab2'], a)
359
360 redraw
361 call MouseLeftClick(row, col)
362 " Check getcharmod() to verify that first click is not
363 " interpreted as a spurious double-click.
364 call assert_equal(0, getcharmod())
365 call MouseLeftRelease(row, col)
366 call MouseLeftClick(row, col)
367 call assert_equal(32, getcharmod()) " double-click
368 call MouseLeftRelease(row, col)
369 let a = split(execute(':tabs'), "\n")
370 call assert_equal(['Tab page 1',
371 \ ' Xtab1',
372 \ 'Tab page 2',
373 \ '> [No Name]',
374 \ 'Tab page 3',
375 \ ' Xtab2'], a)
376
377 if ttymouse_val !=# 'sgr'
378 " We need to sleep, or else MouseLeftClick() in next loop
379 " iteration will be interpreted as a spurious triple-click.
380 sleep 100m
381 endif
382 %bwipe!
383 endfor
384
385 let &mouse = save_mouse
386 let &term = save_term
387 let &ttymouse = save_ttymouse
388 set mousetime&
389endfunc