blob: 9d7f6b5fa788582897132606a24ee891d44fdff9 [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 Moolenaarb0f94c12019-06-13 22:19:53 +02004if has('gui_running')
5 throw 'Skipped, does not work in the GUI'
6endif
7if !has('unix')
8 throw 'Skipped, not on Unix'
Bram Moolenaar905dd902019-04-07 14:21:47 +02009endif
10
Bram Moolenaar564344a2019-04-28 13:00:12 +020011source shared.vim
12
Bram Moolenaard0621d82019-05-02 21:12:19 +020013" xterm2 and sgr always work, urxvt is optional.
Bram Moolenaar92fd5992019-05-02 23:00:22 +020014let s:ttymouse_values = ['xterm2', 'sgr']
Bram Moolenaard0621d82019-05-02 21:12:19 +020015if has('mouse_urxvt')
Bram Moolenaar92fd5992019-05-02 23:00:22 +020016 call add(s:ttymouse_values, 'urxvt')
17endif
18
19" dec doesn't support all the functionality
20if has('mouse_dec')
21 let s:ttymouse_dec = ['dec']
22else
23 let s:ttymouse_dec = []
Bram Moolenaard0621d82019-05-02 21:12:19 +020024endif
25
Bram Moolenaard7885432019-05-03 13:44:10 +020026" netterm only supports left click
27if has('mouse_netterm')
28 let s:ttymouse_netterm = ['netterm']
29else
30 let s:ttymouse_netterm = []
31endif
32
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +020033" Helper function to emit a terminal escape code.
Bram Moolenaard0621d82019-05-02 21:12:19 +020034func TerminalEscapeCode(code, row, col, m)
Bram Moolenaar2b00b9b2019-04-17 17:08:27 +020035 if &ttymouse ==# 'xterm2'
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +020036 " need to use byte encoding here.
Bram Moolenaard0621d82019-05-02 21:12:19 +020037 let str = list2str([a:code + 0x20, a:col + 0x20, a:row + 0x20])
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +020038 if has('iconv')
39 let bytes = iconv(str, 'utf-8', 'latin1')
40 else
41 " Hopefully the numbers are not too big.
42 let bytes = str
43 endif
44 call feedkeys("\<Esc>[M" .. bytes, 'Lx!')
45 elseif &ttymouse ==# 'sgr'
Bram Moolenaard0621d82019-05-02 21:12:19 +020046 call feedkeys(printf("\<Esc>[<%d;%d;%d%s", a:code, a:col, a:row, a:m), 'Lx!')
47 elseif &ttymouse ==# 'urxvt'
48 call feedkeys(printf("\<Esc>[%d;%d;%dM", a:code + 0x20, a:col, a:row), 'Lx!')
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +020049 endif
50endfunc
51
Bram Moolenaar92fd5992019-05-02 23:00:22 +020052func DecEscapeCode(code, down, row, col)
53 call feedkeys(printf("\<Esc>[%d;%d;%d;%d&w", a:code, a:down, a:row, a:col), 'Lx!')
54endfunc
55
Bram Moolenaard7885432019-05-03 13:44:10 +020056func NettermEscapeCode(row, col)
57 call feedkeys(printf("\<Esc>}%d,%d\r", a:row, a:col), 'Lx!')
58endfunc
59
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +020060func MouseLeftClick(row, col)
Bram Moolenaar92fd5992019-05-02 23:00:22 +020061 if &ttymouse ==# 'dec'
62 call DecEscapeCode(2, 4, a:row, a:col)
Bram Moolenaard7885432019-05-03 13:44:10 +020063 elseif &ttymouse ==# 'netterm'
64 call NettermEscapeCode(a:row, a:col)
Bram Moolenaar92fd5992019-05-02 23:00:22 +020065 else
66 call TerminalEscapeCode(0, a:row, a:col, 'M')
67 endif
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +020068endfunc
69
Bram Moolenaarc1b81602019-04-27 19:11:35 +020070func MouseMiddleClick(row, col)
Bram Moolenaar92fd5992019-05-02 23:00:22 +020071 if &ttymouse ==# 'dec'
72 call DecEscapeCode(4, 2, a:row, a:col)
73 else
74 call TerminalEscapeCode(1, a:row, a:col, 'M')
75 endif
Bram Moolenaarc1b81602019-04-27 19:11:35 +020076endfunc
77
Bram Moolenaar1ee36d62019-05-01 23:13:56 +020078func MouseCtrlLeftClick(row, col)
79 let ctrl = 0x10
Bram Moolenaard0621d82019-05-02 21:12:19 +020080 call TerminalEscapeCode(0 + ctrl, a:row, a:col, 'M')
Bram Moolenaar1ee36d62019-05-01 23:13:56 +020081endfunc
82
83func MouseCtrlRightClick(row, col)
84 let ctrl = 0x10
Bram Moolenaard0621d82019-05-02 21:12:19 +020085 call TerminalEscapeCode(2 + ctrl, a:row, a:col, 'M')
Bram Moolenaar1ee36d62019-05-01 23:13:56 +020086endfunc
87
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +020088func MouseLeftRelease(row, col)
Bram Moolenaar92fd5992019-05-02 23:00:22 +020089 if &ttymouse ==# 'dec'
90 call DecEscapeCode(3, 0, a:row, a:col)
Bram Moolenaard7885432019-05-03 13:44:10 +020091 elseif &ttymouse ==# 'netterm'
92 " send nothing
Bram Moolenaar92fd5992019-05-02 23:00:22 +020093 else
94 call TerminalEscapeCode(3, a:row, a:col, 'm')
95 endif
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +020096endfunc
97
Bram Moolenaarc1b81602019-04-27 19:11:35 +020098func MouseMiddleRelease(row, col)
Bram Moolenaar92fd5992019-05-02 23:00:22 +020099 if &ttymouse ==# 'dec'
100 call DecEscapeCode(5, 0, a:row, a:col)
101 else
102 call TerminalEscapeCode(3, a:row, a:col, 'm')
103 endif
Bram Moolenaarc1b81602019-04-27 19:11:35 +0200104endfunc
105
Bram Moolenaar1ee36d62019-05-01 23:13:56 +0200106func MouseRightRelease(row, col)
Bram Moolenaard0621d82019-05-02 21:12:19 +0200107 call TerminalEscapeCode(3, a:row, a:col, 'm')
Bram Moolenaar1ee36d62019-05-01 23:13:56 +0200108endfunc
109
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200110func MouseLeftDrag(row, col)
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200111 if &ttymouse ==# 'dec'
112 call DecEscapeCode(1, 4, a:row, a:col)
113 else
114 call TerminalEscapeCode(0x20, a:row, a:col, 'M')
115 endif
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200116endfunc
117
118func MouseWheelUp(row, col)
Bram Moolenaard0621d82019-05-02 21:12:19 +0200119 call TerminalEscapeCode(0x40, a:row, a:col, 'M')
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200120endfunc
121
122func MouseWheelDown(row, col)
Bram Moolenaard0621d82019-05-02 21:12:19 +0200123 call TerminalEscapeCode(0x41, a:row, a:col, 'M')
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200124endfunc
125
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200126func Test_term_mouse_left_click()
Bram Moolenaar905dd902019-04-07 14:21:47 +0200127 new
128 let save_mouse = &mouse
129 let save_term = &term
130 let save_ttymouse = &ttymouse
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200131 call test_override('no_query_mouse', 1)
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200132 set mouse=a term=xterm
Bram Moolenaar905dd902019-04-07 14:21:47 +0200133 call setline(1, ['line 1', 'line 2', 'line 3 is a bit longer'])
Bram Moolenaar905dd902019-04-07 14:21:47 +0200134
Bram Moolenaard7885432019-05-03 13:44:10 +0200135 for ttymouse_val in s:ttymouse_values + s:ttymouse_dec + s:ttymouse_netterm
Bram Moolenaar49452192019-04-17 16:27:02 +0200136 let msg = 'ttymouse=' .. ttymouse_val
Bram Moolenaar1ee36d62019-05-01 23:13:56 +0200137 exe 'set ttymouse=' .. ttymouse_val
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200138 go
Bram Moolenaar49452192019-04-17 16:27:02 +0200139 call assert_equal([0, 1, 1, 0], getpos('.'), msg)
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200140 let row = 2
141 let col = 6
142 call MouseLeftClick(row, col)
143 call MouseLeftRelease(row, col)
Bram Moolenaar49452192019-04-17 16:27:02 +0200144 call assert_equal([0, 2, 6, 0], getpos('.'), msg)
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200145 endfor
Bram Moolenaar905dd902019-04-07 14:21:47 +0200146
147 let &mouse = save_mouse
148 let &term = save_term
149 let &ttymouse = save_ttymouse
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200150 call test_override('no_query_mouse', 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +0200151 bwipe!
152endfunc
153
Bram Moolenaar1ee36d62019-05-01 23:13:56 +0200154" Test that <C-LeftMouse> jumps to help tag and <C-RightMouse> jumps back.
155func Test_xterm_mouse_ctrl_click()
156 let save_mouse = &mouse
157 let save_term = &term
158 let save_ttymouse = &ttymouse
159 set mouse=a term=xterm
160
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200161 for ttymouse_val in s:ttymouse_values
Bram Moolenaar1ee36d62019-05-01 23:13:56 +0200162 let msg = 'ttymouse=' .. ttymouse_val
163 exe 'set ttymouse=' .. ttymouse_val
164 help
165 /usr_02.txt
166 norm! zt
167 let row = 1
168 let col = 1
169 call MouseCtrlLeftClick(row, col)
170 call MouseLeftRelease(row, col)
171 call assert_match('usr_02.txt$', bufname('%'), msg)
172 call assert_equal('*usr_02.txt*', expand('<cWORD>'))
173
174 call MouseCtrlRightClick(row, col)
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200175 call MouseRightRelease(row, col)
Bram Moolenaar1ee36d62019-05-01 23:13:56 +0200176 call assert_match('help.txt$', bufname('%'), msg)
177 call assert_equal('|usr_02.txt|', expand('<cWORD>'))
178
179 helpclose
180 endfor
181
182 let &mouse = save_mouse
183 let &term = save_term
184 let &ttymouse = save_ttymouse
185endfunc
186
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200187func Test_term_mouse_middle_click()
Bram Moolenaar564344a2019-04-28 13:00:12 +0200188 if !WorkingClipboard()
189 throw 'Skipped: No working clipboard'
190 endif
191
Bram Moolenaarc1b81602019-04-27 19:11:35 +0200192 new
193 let save_mouse = &mouse
194 let save_term = &term
195 let save_ttymouse = &ttymouse
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200196 call test_override('no_query_mouse', 1)
Bram Moolenaarc1b81602019-04-27 19:11:35 +0200197 let save_quotestar = @*
198 let @* = 'abc'
199 set mouse=a term=xterm
200
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200201 for ttymouse_val in s:ttymouse_values + s:ttymouse_dec
Bram Moolenaarc1b81602019-04-27 19:11:35 +0200202 let msg = 'ttymouse=' .. ttymouse_val
Bram Moolenaar1ee36d62019-05-01 23:13:56 +0200203 exe 'set ttymouse=' .. ttymouse_val
Bram Moolenaarc1b81602019-04-27 19:11:35 +0200204 call setline(1, ['123456789', '123456789'])
205
206 " Middle-click in the middle of the line pastes text where clicked.
207 let row = 1
208 let col = 6
209 call MouseMiddleClick(row, col)
210 call MouseMiddleRelease(row, col)
211 call assert_equal(['12345abc6789', '123456789'], getline(1, '$'), msg)
212
213 " Middle-click beyond end of the line pastes text at the end of the line.
214 let col = 20
215 call MouseMiddleClick(row, col)
216 call MouseMiddleRelease(row, col)
217 call assert_equal(['12345abc6789abc', '123456789'], getline(1, '$'), msg)
218
219 " Middle-click beyond the last line pastes in the last line.
220 let row = 5
221 let col = 3
222 call MouseMiddleClick(row, col)
223 call MouseMiddleRelease(row, col)
224 call assert_equal(['12345abc6789abc', '12abc3456789'], getline(1, '$'), msg)
225 endfor
226
227 let &mouse = save_mouse
228 let &term = save_term
229 let &ttymouse = save_ttymouse
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200230 call test_override('no_query_mouse', 0)
Bram Moolenaarc1b81602019-04-27 19:11:35 +0200231 let @* = save_quotestar
232 bwipe!
233endfunc
234
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200235" TODO: for unclear reasons this test fails if it comes after
236" Test_xterm_mouse_ctrl_click()
237func Test_1xterm_mouse_wheel()
Bram Moolenaar049736f2019-04-07 21:55:07 +0200238 new
239 let save_mouse = &mouse
240 let save_term = &term
241 let save_ttymouse = &ttymouse
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200242 set mouse=a term=xterm
Bram Moolenaar049736f2019-04-07 21:55:07 +0200243 call setline(1, range(1, 100))
244
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200245 for ttymouse_val in s:ttymouse_values
Bram Moolenaar49452192019-04-17 16:27:02 +0200246 let msg = 'ttymouse=' .. ttymouse_val
Bram Moolenaar1ee36d62019-05-01 23:13:56 +0200247 exe 'set ttymouse=' .. ttymouse_val
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200248 go
Bram Moolenaar49452192019-04-17 16:27:02 +0200249 call assert_equal(1, line('w0'), msg)
250 call assert_equal([0, 1, 1, 0], getpos('.'), msg)
Bram Moolenaar049736f2019-04-07 21:55:07 +0200251
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200252 call MouseWheelDown(1, 1)
Bram Moolenaar49452192019-04-17 16:27:02 +0200253 call assert_equal(4, line('w0'), msg)
254 call assert_equal([0, 4, 1, 0], getpos('.'), msg)
Bram Moolenaar049736f2019-04-07 21:55:07 +0200255
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200256 call MouseWheelDown(1, 1)
Bram Moolenaar49452192019-04-17 16:27:02 +0200257 call assert_equal(7, line('w0'), msg)
258 call assert_equal([0, 7, 1, 0], getpos('.'), msg)
Bram Moolenaar049736f2019-04-07 21:55:07 +0200259
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200260 call MouseWheelUp(1, 1)
Bram Moolenaar49452192019-04-17 16:27:02 +0200261 call assert_equal(4, line('w0'), msg)
262 call assert_equal([0, 7, 1, 0], getpos('.'), msg)
Bram Moolenaar049736f2019-04-07 21:55:07 +0200263
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200264 call MouseWheelUp(1, 1)
Bram Moolenaar49452192019-04-17 16:27:02 +0200265 call assert_equal(1, line('w0'), msg)
266 call assert_equal([0, 7, 1, 0], getpos('.'), msg)
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200267 endfor
Bram Moolenaar049736f2019-04-07 21:55:07 +0200268
269 let &mouse = save_mouse
270 let &term = save_term
271 let &ttymouse = save_ttymouse
272 bwipe!
273endfunc
Bram Moolenaar3fb01a52019-04-09 21:52:02 +0200274
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200275func Test_term_mouse_drag_window_separator()
Bram Moolenaar3fb01a52019-04-09 21:52:02 +0200276 let save_mouse = &mouse
277 let save_term = &term
278 let save_ttymouse = &ttymouse
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200279 call test_override('no_query_mouse', 1)
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200280 set mouse=a term=xterm
Bram Moolenaar3fb01a52019-04-09 21:52:02 +0200281
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200282 for ttymouse_val in s:ttymouse_values + s:ttymouse_dec
Bram Moolenaar49452192019-04-17 16:27:02 +0200283 let msg = 'ttymouse=' .. ttymouse_val
Bram Moolenaar1ee36d62019-05-01 23:13:56 +0200284 exe 'set ttymouse=' .. ttymouse_val
Bram Moolenaar3fb01a52019-04-09 21:52:02 +0200285
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200286 " Split horizontally and test dragging the horizontal window separator.
287 split
288 let rowseparator = winheight(0) + 1
289 let row = rowseparator
290 let col = 1
Bram Moolenaarc8b3dda2019-04-12 21:42:52 +0200291
Bram Moolenaar2b00b9b2019-04-17 17:08:27 +0200292 " When 'ttymouse' is 'xterm2', row/col bigger than 223 are not supported.
293 if ttymouse_val !=# 'xterm2' || row <= 223
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200294 call MouseLeftClick(row, col)
295 let row -= 1
296 call MouseLeftDrag(row, col)
Bram Moolenaar49452192019-04-17 16:27:02 +0200297 call assert_equal(rowseparator - 1, winheight(0) + 1, msg)
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200298 let row += 1
299 call MouseLeftDrag(row, col)
Bram Moolenaar49452192019-04-17 16:27:02 +0200300 call assert_equal(rowseparator, winheight(0) + 1, msg)
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200301 call MouseLeftRelease(row, col)
Bram Moolenaar49452192019-04-17 16:27:02 +0200302 call assert_equal(rowseparator, winheight(0) + 1, msg)
Bram Moolenaarc8b3dda2019-04-12 21:42:52 +0200303 endif
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200304 bwipe!
Bram Moolenaar3fb01a52019-04-09 21:52:02 +0200305
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200306 " Split vertically and test dragging the vertical window separator.
307 vsplit
308 let colseparator = winwidth(0) + 1
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200309 let row = 1
310 let col = colseparator
Bram Moolenaar3fb01a52019-04-09 21:52:02 +0200311
Bram Moolenaar2b00b9b2019-04-17 17:08:27 +0200312 " When 'ttymouse' is 'xterm2', row/col bigger than 223 are not supported.
313 if ttymouse_val !=# 'xterm2' || col <= 223
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200314 call MouseLeftClick(row, col)
315 let col -= 1
316 call MouseLeftDrag(row, col)
Bram Moolenaar49452192019-04-17 16:27:02 +0200317 call assert_equal(colseparator - 1, winwidth(0) + 1, msg)
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200318 let col += 1
319 call MouseLeftDrag(row, col)
Bram Moolenaar49452192019-04-17 16:27:02 +0200320 call assert_equal(colseparator, winwidth(0) + 1, msg)
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200321 call MouseLeftRelease(row, col)
Bram Moolenaar49452192019-04-17 16:27:02 +0200322 call assert_equal(colseparator, winwidth(0) + 1, msg)
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200323 endif
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200324 bwipe!
325 endfor
Bram Moolenaar3fb01a52019-04-09 21:52:02 +0200326
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200327 let &mouse = save_mouse
328 let &term = save_term
329 let &ttymouse = save_ttymouse
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200330 call test_override('no_query_mouse', 0)
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200331endfunc
Bram Moolenaar3fb01a52019-04-09 21:52:02 +0200332
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200333func Test_term_mouse_drag_statusline()
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200334 let save_mouse = &mouse
335 let save_term = &term
336 let save_ttymouse = &ttymouse
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200337 call test_override('no_query_mouse', 1)
Bram Moolenaarca57ab52019-04-13 14:53:16 +0200338 let save_laststatus = &laststatus
339 set mouse=a term=xterm laststatus=2
Bram Moolenaar3fb01a52019-04-09 21:52:02 +0200340
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200341 for ttymouse_val in s:ttymouse_values + s:ttymouse_dec
Bram Moolenaar49452192019-04-17 16:27:02 +0200342 let msg = 'ttymouse=' .. ttymouse_val
Bram Moolenaar1ee36d62019-05-01 23:13:56 +0200343 exe 'set ttymouse=' .. ttymouse_val
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200344
Bram Moolenaar49452192019-04-17 16:27:02 +0200345 call assert_equal(1, &cmdheight, msg)
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200346 let rowstatusline = winheight(0) + 1
347 let row = rowstatusline
348 let col = 1
Bram Moolenaarc8b3dda2019-04-12 21:42:52 +0200349
Bram Moolenaar2b00b9b2019-04-17 17:08:27 +0200350 if ttymouse_val ==# 'xterm2' && row > 223
351 " When 'ttymouse' is 'xterm2', row/col bigger than 223 are not supported.
Bram Moolenaarc8b3dda2019-04-12 21:42:52 +0200352 continue
353 endif
354
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200355 call MouseLeftClick(row, col)
356 let row -= 1
357 call MouseLeftDrag(row, col)
Bram Moolenaar49452192019-04-17 16:27:02 +0200358 call assert_equal(2, &cmdheight, msg)
359 call assert_equal(rowstatusline - 1, winheight(0) + 1, msg)
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200360 let row += 1
361 call MouseLeftDrag(row, col)
Bram Moolenaar49452192019-04-17 16:27:02 +0200362 call assert_equal(1, &cmdheight, msg)
363 call assert_equal(rowstatusline, winheight(0) + 1, msg)
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200364 call MouseLeftRelease(row, col)
Bram Moolenaar49452192019-04-17 16:27:02 +0200365 call assert_equal(1, &cmdheight, msg)
366 call assert_equal(rowstatusline, winheight(0) + 1, msg)
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200367 endfor
368
Bram Moolenaar3fb01a52019-04-09 21:52:02 +0200369 let &mouse = save_mouse
370 let &term = save_term
371 let &ttymouse = save_ttymouse
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200372 call test_override('no_query_mouse', 0)
Bram Moolenaarca57ab52019-04-13 14:53:16 +0200373 let &laststatus = save_laststatus
374endfunc
375
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200376func Test_term_mouse_click_tab()
Bram Moolenaarca57ab52019-04-13 14:53:16 +0200377 let save_mouse = &mouse
378 let save_term = &term
379 let save_ttymouse = &ttymouse
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200380 call test_override('no_query_mouse', 1)
Bram Moolenaarca57ab52019-04-13 14:53:16 +0200381 set mouse=a term=xterm
382 let row = 1
383
Bram Moolenaard7885432019-05-03 13:44:10 +0200384 for ttymouse_val in s:ttymouse_values + s:ttymouse_dec + s:ttymouse_netterm
Bram Moolenaar49452192019-04-17 16:27:02 +0200385 let msg = 'ttymouse=' .. ttymouse_val
Bram Moolenaar1ee36d62019-05-01 23:13:56 +0200386 exe 'set ttymouse=' .. ttymouse_val
Bram Moolenaarca57ab52019-04-13 14:53:16 +0200387 e Xfoo
388 tabnew Xbar
389
390 let a = split(execute(':tabs'), "\n")
391 call assert_equal(['Tab page 1',
392 \ ' Xfoo',
393 \ 'Tab page 2',
Bram Moolenaar49452192019-04-17 16:27:02 +0200394 \ '> Xbar'], a, msg)
Bram Moolenaarca57ab52019-04-13 14:53:16 +0200395
396 " Test clicking on tab names in the tabline at the top.
397 let col = 2
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200398 redraw
Bram Moolenaarca57ab52019-04-13 14:53:16 +0200399 call MouseLeftClick(row, col)
400 call MouseLeftRelease(row, col)
401 let a = split(execute(':tabs'), "\n")
402 call assert_equal(['Tab page 1',
403 \ '> Xfoo',
404 \ 'Tab page 2',
Bram Moolenaar49452192019-04-17 16:27:02 +0200405 \ ' Xbar'], a, msg)
Bram Moolenaarca57ab52019-04-13 14:53:16 +0200406
407 let col = 9
408 call MouseLeftClick(row, col)
409 call MouseLeftRelease(row, col)
410 let a = split(execute(':tabs'), "\n")
411 call assert_equal(['Tab page 1',
412 \ ' Xfoo',
413 \ 'Tab page 2',
Bram Moolenaar49452192019-04-17 16:27:02 +0200414 \ '> Xbar'], a, msg)
Bram Moolenaarca57ab52019-04-13 14:53:16 +0200415
416 %bwipe!
417 endfor
418
419 let &mouse = save_mouse
420 let &term = save_term
421 let &ttymouse = save_ttymouse
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200422 call test_override('no_query_mouse', 0)
Bram Moolenaar3fb01a52019-04-09 21:52:02 +0200423endfunc
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200424
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200425func Test_term_mouse_click_X_to_close_tab()
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200426 let save_mouse = &mouse
427 let save_term = &term
428 let save_ttymouse = &ttymouse
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200429 call test_override('no_query_mouse', 1)
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200430 set mouse=a term=xterm
431 let row = 1
432 let col = &columns
433
Bram Moolenaard7885432019-05-03 13:44:10 +0200434 for ttymouse_val in s:ttymouse_values + s:ttymouse_dec + s:ttymouse_netterm
Bram Moolenaar2b00b9b2019-04-17 17:08:27 +0200435 if ttymouse_val ==# 'xterm2' && col > 223
436 " When 'ttymouse' is 'xterm2', row/col bigger than 223 are not supported.
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200437 continue
438 endif
Bram Moolenaar49452192019-04-17 16:27:02 +0200439 let msg = 'ttymouse=' .. ttymouse_val
Bram Moolenaar1ee36d62019-05-01 23:13:56 +0200440 exe 'set ttymouse=' .. ttymouse_val
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200441 e Xtab1
442 tabnew Xtab2
443 tabnew Xtab3
444 tabn 2
445
446 let a = split(execute(':tabs'), "\n")
447 call assert_equal(['Tab page 1',
448 \ ' Xtab1',
449 \ 'Tab page 2',
450 \ '> Xtab2',
451 \ 'Tab page 3',
Bram Moolenaar49452192019-04-17 16:27:02 +0200452 \ ' Xtab3'], a, msg)
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200453
454 " Click on "X" in tabline to close current tab i.e. Xtab2.
455 redraw
456 call MouseLeftClick(row, col)
457 call MouseLeftRelease(row, col)
458 let a = split(execute(':tabs'), "\n")
459 call assert_equal(['Tab page 1',
460 \ ' Xtab1',
461 \ 'Tab page 2',
Bram Moolenaar49452192019-04-17 16:27:02 +0200462 \ '> Xtab3'], a, msg)
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200463
464 %bwipe!
465 endfor
466
467 let &mouse = save_mouse
468 let &term = save_term
469 let &ttymouse = save_ttymouse
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200470 call test_override('no_query_mouse', 0)
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200471endfunc
Bram Moolenaare3e38282019-04-15 20:55:31 +0200472
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200473func Test_term_mouse_drag_to_move_tab()
Bram Moolenaare3e38282019-04-15 20:55:31 +0200474 let save_mouse = &mouse
475 let save_term = &term
476 let save_ttymouse = &ttymouse
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200477 call test_override('no_query_mouse', 1)
Bram Moolenaare3e38282019-04-15 20:55:31 +0200478 " Set 'mousetime' to 1 to avoid recognizing a double-click in the loop
479 set mouse=a term=xterm mousetime=1
480 let row = 1
481
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200482 for ttymouse_val in s:ttymouse_values + s:ttymouse_dec
Bram Moolenaar49452192019-04-17 16:27:02 +0200483 let msg = 'ttymouse=' .. ttymouse_val
Bram Moolenaar1ee36d62019-05-01 23:13:56 +0200484 exe 'set ttymouse=' .. ttymouse_val
Bram Moolenaare3e38282019-04-15 20:55:31 +0200485 e Xtab1
486 tabnew Xtab2
487
488 let a = split(execute(':tabs'), "\n")
489 call assert_equal(['Tab page 1',
490 \ ' Xtab1',
491 \ 'Tab page 2',
Bram Moolenaar49452192019-04-17 16:27:02 +0200492 \ '> Xtab2'], a, msg)
Bram Moolenaare3e38282019-04-15 20:55:31 +0200493 redraw
494
495 " Click in tab2 and drag it to tab1.
496 " Check getcharmod() to verify that click is not
497 " interpreted as a spurious double-click.
498 call MouseLeftClick(row, 10)
Bram Moolenaar49452192019-04-17 16:27:02 +0200499 call assert_equal(0, getcharmod(), msg)
Bram Moolenaare3e38282019-04-15 20:55:31 +0200500 for col in [9, 8, 7, 6]
501 call MouseLeftDrag(row, col)
502 endfor
503 call MouseLeftRelease(row, col)
504 let a = split(execute(':tabs'), "\n")
505 call assert_equal(['Tab page 1',
506 \ '> Xtab2',
507 \ 'Tab page 2',
Bram Moolenaar49452192019-04-17 16:27:02 +0200508 \ ' Xtab1'], a, msg)
Bram Moolenaare3e38282019-04-15 20:55:31 +0200509
Bram Moolenaar7f279762019-04-15 21:48:22 +0200510 " brief sleep to avoid causing a double-click
511 sleep 20m
Bram Moolenaare3e38282019-04-15 20:55:31 +0200512 %bwipe!
513 endfor
514
515 let &mouse = save_mouse
516 let &term = save_term
517 let &ttymouse = save_ttymouse
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200518 call test_override('no_query_mouse', 0)
Bram Moolenaare3e38282019-04-15 20:55:31 +0200519 set mousetime&
520endfunc
521
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200522func Test_term_mouse_double_click_to_create_tab()
Bram Moolenaare3e38282019-04-15 20:55:31 +0200523 let save_mouse = &mouse
524 let save_term = &term
525 let save_ttymouse = &ttymouse
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200526 call test_override('no_query_mouse', 1)
Bram Moolenaare3e38282019-04-15 20:55:31 +0200527 " Set 'mousetime' to a small value, so that double-click works but we don't
528 " have to wait long to avoid a triple-click.
529 set mouse=a term=xterm mousetime=100
530 let row = 1
531 let col = 10
532
Bram Moolenaard0621d82019-05-02 21:12:19 +0200533 let round = 0
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200534 for ttymouse_val in s:ttymouse_values + s:ttymouse_dec
Bram Moolenaar49452192019-04-17 16:27:02 +0200535 let msg = 'ttymouse=' .. ttymouse_val
Bram Moolenaar1ee36d62019-05-01 23:13:56 +0200536 exe 'set ttymouse=' .. ttymouse_val
Bram Moolenaare3e38282019-04-15 20:55:31 +0200537 e Xtab1
538 tabnew Xtab2
539
Bram Moolenaard0621d82019-05-02 21:12:19 +0200540 if round > 0
541 " We need to sleep, or else the first MouseLeftClick() will be
542 " interpreted as a spurious triple-click.
543 sleep 100m
544 endif
545 let round += 1
546
Bram Moolenaare3e38282019-04-15 20:55:31 +0200547 let a = split(execute(':tabs'), "\n")
548 call assert_equal(['Tab page 1',
549 \ ' Xtab1',
550 \ 'Tab page 2',
Bram Moolenaar49452192019-04-17 16:27:02 +0200551 \ '> Xtab2'], a, msg)
Bram Moolenaare3e38282019-04-15 20:55:31 +0200552
553 redraw
554 call MouseLeftClick(row, col)
555 " Check getcharmod() to verify that first click is not
556 " interpreted as a spurious double-click.
Bram Moolenaar49452192019-04-17 16:27:02 +0200557 call assert_equal(0, getcharmod(), msg)
Bram Moolenaare3e38282019-04-15 20:55:31 +0200558 call MouseLeftRelease(row, col)
559 call MouseLeftClick(row, col)
Bram Moolenaar49452192019-04-17 16:27:02 +0200560 call assert_equal(32, getcharmod(), msg) " double-click
Bram Moolenaare3e38282019-04-15 20:55:31 +0200561 call MouseLeftRelease(row, col)
562 let a = split(execute(':tabs'), "\n")
563 call assert_equal(['Tab page 1',
564 \ ' Xtab1',
565 \ 'Tab page 2',
566 \ '> [No Name]',
567 \ 'Tab page 3',
Bram Moolenaar49452192019-04-17 16:27:02 +0200568 \ ' Xtab2'], a, msg)
Bram Moolenaare3e38282019-04-15 20:55:31 +0200569
Bram Moolenaare3e38282019-04-15 20:55:31 +0200570 %bwipe!
571 endfor
572
573 let &mouse = save_mouse
574 let &term = save_term
575 let &ttymouse = save_ttymouse
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200576 call test_override('no_query_mouse', 0)
Bram Moolenaare3e38282019-04-15 20:55:31 +0200577 set mousetime&
578endfunc
Bram Moolenaar696d6372019-04-17 16:33:46 +0200579
580func Test_xterm_mouse_click_in_fold_columns()
581 new
582 let save_mouse = &mouse
583 let save_term = &term
584 let save_ttymouse = &ttymouse
585 let save_foldcolumn = &foldcolumn
Bram Moolenaar2b00b9b2019-04-17 17:08:27 +0200586 set mouse=a term=xterm foldcolumn=3 ttymouse=xterm2
Bram Moolenaar696d6372019-04-17 16:33:46 +0200587
588 " Create 2 nested folds.
589 call setline(1, range(1, 7))
590 2,6fold
591 norm! zR
592 4,5fold
593 call assert_equal([-1, -1, -1, 4, 4, -1, -1],
594 \ map(range(1, 7), 'foldclosed(v:val)'))
595
596 " Click in "+" of inner fold in foldcolumn should open it.
597 redraw
598 let row = 4
599 let col = 2
600 call MouseLeftClick(row, col)
601 call MouseLeftRelease(row, col)
602 call assert_equal([-1, -1, -1, -1, -1, -1, -1],
603 \ map(range(1, 7), 'foldclosed(v:val)'))
604
605 " Click in "-" of outer fold in foldcolumn should close it.
606 redraw
607 let row = 2
608 let col = 1
609 call MouseLeftClick(row, col)
610 call MouseLeftRelease(row, col)
611 call assert_equal([-1, 2, 2, 2, 2, 2, -1],
612 \ map(range(1, 7), 'foldclosed(v:val)'))
613 norm! zR
614
615 " Click in "|" of inner fold in foldcolumn should close it.
616 redraw
617 let row = 5
618 let col = 2
619 call MouseLeftClick(row, col)
620 call MouseLeftRelease(row, col)
621 call assert_equal([-1, -1, -1, 4, 4, -1, -1],
622 \ map(range(1, 7), 'foldclosed(v:val)'))
623
624 let &foldcolumn = save_foldcolumn
625 let &ttymouse = save_ttymouse
626 let &term = save_term
627 let &mouse = save_mouse
628 bwipe!
629endfunc
Bram Moolenaar66761db2019-06-05 22:07:51 +0200630
631" This only checks if the sequence is recognized.
Bram Moolenaar66761db2019-06-05 22:07:51 +0200632func Test_term_rgb_response()
633 set t_RF=x
634 set t_RB=y
635
636 " response to t_RF, 4 digits
637 let red = 0x12
638 let green = 0x34
639 let blue = 0x56
640 let seq = printf("\<Esc>]10;rgb:%02x00/%02x00/%02x00\x07", red, green, blue)
641 call feedkeys(seq, 'Lx!')
642 call assert_equal(seq, v:termrfgresp)
643
644 " response to t_RF, 2 digits
645 let red = 0x78
646 let green = 0x9a
647 let blue = 0xbc
648 let seq = printf("\<Esc>]10;rgb:%02x/%02x/%02x\x07", red, green, blue)
649 call feedkeys(seq, 'Lx!')
650 call assert_equal(seq, v:termrfgresp)
651
Bram Moolenaar32e19772019-06-05 22:57:04 +0200652 " response to t_RB, 4 digits, dark
653 set background=light
654 call test_option_not_set('background')
655 let red = 0x29
656 let green = 0x4a
657 let blue = 0x6b
658 let seq = printf("\<Esc>]11;rgb:%02x00/%02x00/%02x00\x07", red, green, blue)
659 call feedkeys(seq, 'Lx!')
660 call assert_equal(seq, v:termrbgresp)
661 call assert_equal('dark', &background)
662
663 " response to t_RB, 4 digits, light
664 set background=dark
665 call test_option_not_set('background')
666 let red = 0x81
667 let green = 0x63
Bram Moolenaar66761db2019-06-05 22:07:51 +0200668 let blue = 0x65
669 let seq = printf("\<Esc>]11;rgb:%02x00/%02x00/%02x00\x07", red, green, blue)
670 call feedkeys(seq, 'Lx!')
671 call assert_equal(seq, v:termrbgresp)
Bram Moolenaar32e19772019-06-05 22:57:04 +0200672 call assert_equal('light', &background)
Bram Moolenaar66761db2019-06-05 22:07:51 +0200673
Bram Moolenaar32e19772019-06-05 22:57:04 +0200674 " response to t_RB, 2 digits, dark
675 set background=light
676 call test_option_not_set('background')
677 let red = 0x47
678 let green = 0x59
679 let blue = 0x5b
Bram Moolenaar66761db2019-06-05 22:07:51 +0200680 let seq = printf("\<Esc>]11;rgb:%02x/%02x/%02x\x07", red, green, blue)
681 call feedkeys(seq, 'Lx!')
682 call assert_equal(seq, v:termrbgresp)
Bram Moolenaar32e19772019-06-05 22:57:04 +0200683 call assert_equal('dark', &background)
684
685 " response to t_RB, 2 digits, light
686 set background=dark
687 call test_option_not_set('background')
688 let red = 0x83
689 let green = 0xa4
690 let blue = 0xc2
691 let seq = printf("\<Esc>]11;rgb:%02x/%02x/%02x\x07", red, green, blue)
692 call feedkeys(seq, 'Lx!')
693 call assert_equal(seq, v:termrbgresp)
694 call assert_equal('light', &background)
Bram Moolenaar66761db2019-06-05 22:07:51 +0200695
696 set t_RF= t_RB=
697endfunc
698
699" This only checks if the sequence is recognized.
700" This must be last, because it has side effects to xterm properties.
701" TODO: check that the values were parsed properly
702func Test_xx_term_style_response()
703 " Termresponse is only parsed when t_RV is not empty.
704 set t_RV=x
705
706 " send the termresponse to trigger requesting the XT codes
707 let seq = "\<Esc>[>41;337;0c"
708 call feedkeys(seq, 'Lx!')
709 call assert_equal(seq, v:termresponse)
710
711 let seq = "\<Esc>P1$r2 q\<Esc>\\"
712 call feedkeys(seq, 'Lx!')
713 call assert_equal(seq, v:termstyleresp)
714
715 set t_RV=
716endfunc
Bram Moolenaarde6dbb42019-06-06 11:59:18 +0200717
718func Test_get_termcode()
719 let k1 = &t_k1
720 set t_k1=
721 set t_k1&
722 call assert_equal(k1, &t_k1)
Bram Moolenaar9aeb3362019-06-06 12:36:15 +0200723
724 " use external termcap first
725 set nottybuiltin
726 set t_k1=
727 set t_k1&
728 " when using external termcap may get something else, but it must not be
729 " empty, since we would fallback to the builtin one.
730 call assert_notequal('', &t_k1)
731
732 if &term =~ 'xterm'
733 " use internal termcap first
734 let term_save = &term
735 let &term = 'builtin_' .. &term
736 set t_k1=
737 set t_k1&
738 call assert_equal(k1, &t_k1)
739 let &term = term_save
740 endif
741
742 set ttybuiltin
Bram Moolenaarde6dbb42019-06-06 11:59:18 +0200743endfunc