blob: 7b71d60ae9aea70aec1ffbbbd4a74ef340de4ab6 [file] [log] [blame]
Bram Moolenaar905dd902019-04-07 14:21:47 +02001" Tests for decoding escape sequences sent by the terminal.
2
3" This only works for Unix in a terminal
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02004source check.vim
5CheckNotGui
6CheckUnix
Bram Moolenaar905dd902019-04-07 14:21:47 +02007
Bram Moolenaar564344a2019-04-28 13:00:12 +02008source shared.vim
9
Bram Moolenaard0621d82019-05-02 21:12:19 +020010" xterm2 and sgr always work, urxvt is optional.
Bram Moolenaar92fd5992019-05-02 23:00:22 +020011let s:ttymouse_values = ['xterm2', 'sgr']
Bram Moolenaard0621d82019-05-02 21:12:19 +020012if has('mouse_urxvt')
Bram Moolenaar92fd5992019-05-02 23:00:22 +020013 call add(s:ttymouse_values, 'urxvt')
14endif
15
16" dec doesn't support all the functionality
17if has('mouse_dec')
18 let s:ttymouse_dec = ['dec']
19else
20 let s:ttymouse_dec = []
Bram Moolenaard0621d82019-05-02 21:12:19 +020021endif
22
Bram Moolenaard7885432019-05-03 13:44:10 +020023" netterm only supports left click
24if has('mouse_netterm')
25 let s:ttymouse_netterm = ['netterm']
26else
27 let s:ttymouse_netterm = []
28endif
29
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +020030" Helper function to emit a terminal escape code.
Bram Moolenaard0621d82019-05-02 21:12:19 +020031func TerminalEscapeCode(code, row, col, m)
Bram Moolenaar2b00b9b2019-04-17 17:08:27 +020032 if &ttymouse ==# 'xterm2'
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +020033 " need to use byte encoding here.
Bram Moolenaard0621d82019-05-02 21:12:19 +020034 let str = list2str([a:code + 0x20, a:col + 0x20, a:row + 0x20])
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +020035 if has('iconv')
Bram Moolenaarf9f24ce2019-08-31 21:17:39 +020036 let bytes = str->iconv('utf-8', 'latin1')
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +020037 else
38 " Hopefully the numbers are not too big.
39 let bytes = str
40 endif
41 call feedkeys("\<Esc>[M" .. bytes, 'Lx!')
42 elseif &ttymouse ==# 'sgr'
Bram Moolenaard0621d82019-05-02 21:12:19 +020043 call feedkeys(printf("\<Esc>[<%d;%d;%d%s", a:code, a:col, a:row, a:m), 'Lx!')
44 elseif &ttymouse ==# 'urxvt'
45 call feedkeys(printf("\<Esc>[%d;%d;%dM", a:code + 0x20, a:col, a:row), 'Lx!')
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +020046 endif
47endfunc
48
Bram Moolenaar92fd5992019-05-02 23:00:22 +020049func DecEscapeCode(code, down, row, col)
50 call feedkeys(printf("\<Esc>[%d;%d;%d;%d&w", a:code, a:down, a:row, a:col), 'Lx!')
51endfunc
52
Bram Moolenaard7885432019-05-03 13:44:10 +020053func NettermEscapeCode(row, col)
54 call feedkeys(printf("\<Esc>}%d,%d\r", a:row, a:col), 'Lx!')
55endfunc
56
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +020057func MouseLeftClick(row, col)
Bram Moolenaar92fd5992019-05-02 23:00:22 +020058 if &ttymouse ==# 'dec'
59 call DecEscapeCode(2, 4, a:row, a:col)
Bram Moolenaard7885432019-05-03 13:44:10 +020060 elseif &ttymouse ==# 'netterm'
61 call NettermEscapeCode(a:row, a:col)
Bram Moolenaar92fd5992019-05-02 23:00:22 +020062 else
63 call TerminalEscapeCode(0, a:row, a:col, 'M')
64 endif
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +020065endfunc
66
Bram Moolenaarc1b81602019-04-27 19:11:35 +020067func MouseMiddleClick(row, col)
Bram Moolenaar92fd5992019-05-02 23:00:22 +020068 if &ttymouse ==# 'dec'
69 call DecEscapeCode(4, 2, a:row, a:col)
70 else
71 call TerminalEscapeCode(1, a:row, a:col, 'M')
72 endif
Bram Moolenaarc1b81602019-04-27 19:11:35 +020073endfunc
74
Bram Moolenaar1ee36d62019-05-01 23:13:56 +020075func MouseCtrlLeftClick(row, col)
76 let ctrl = 0x10
Bram Moolenaard0621d82019-05-02 21:12:19 +020077 call TerminalEscapeCode(0 + ctrl, a:row, a:col, 'M')
Bram Moolenaar1ee36d62019-05-01 23:13:56 +020078endfunc
79
80func MouseCtrlRightClick(row, col)
81 let ctrl = 0x10
Bram Moolenaard0621d82019-05-02 21:12:19 +020082 call TerminalEscapeCode(2 + ctrl, a:row, a:col, 'M')
Bram Moolenaar1ee36d62019-05-01 23:13:56 +020083endfunc
84
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +020085func MouseLeftRelease(row, col)
Bram Moolenaar92fd5992019-05-02 23:00:22 +020086 if &ttymouse ==# 'dec'
87 call DecEscapeCode(3, 0, a:row, a:col)
Bram Moolenaard7885432019-05-03 13:44:10 +020088 elseif &ttymouse ==# 'netterm'
89 " send nothing
Bram Moolenaar92fd5992019-05-02 23:00:22 +020090 else
91 call TerminalEscapeCode(3, a:row, a:col, 'm')
92 endif
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +020093endfunc
94
Bram Moolenaarc1b81602019-04-27 19:11:35 +020095func MouseMiddleRelease(row, col)
Bram Moolenaar92fd5992019-05-02 23:00:22 +020096 if &ttymouse ==# 'dec'
97 call DecEscapeCode(5, 0, a:row, a:col)
98 else
99 call TerminalEscapeCode(3, a:row, a:col, 'm')
100 endif
Bram Moolenaarc1b81602019-04-27 19:11:35 +0200101endfunc
102
Bram Moolenaar1ee36d62019-05-01 23:13:56 +0200103func MouseRightRelease(row, col)
Bram Moolenaard0621d82019-05-02 21:12:19 +0200104 call TerminalEscapeCode(3, a:row, a:col, 'm')
Bram Moolenaar1ee36d62019-05-01 23:13:56 +0200105endfunc
106
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200107func MouseLeftDrag(row, col)
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200108 if &ttymouse ==# 'dec'
109 call DecEscapeCode(1, 4, a:row, a:col)
110 else
111 call TerminalEscapeCode(0x20, a:row, a:col, 'M')
112 endif
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200113endfunc
114
115func MouseWheelUp(row, col)
Bram Moolenaard0621d82019-05-02 21:12:19 +0200116 call TerminalEscapeCode(0x40, a:row, a:col, 'M')
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200117endfunc
118
119func MouseWheelDown(row, col)
Bram Moolenaard0621d82019-05-02 21:12:19 +0200120 call TerminalEscapeCode(0x41, a:row, a:col, 'M')
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200121endfunc
122
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200123func Test_term_mouse_left_click()
Bram Moolenaar905dd902019-04-07 14:21:47 +0200124 new
125 let save_mouse = &mouse
126 let save_term = &term
127 let save_ttymouse = &ttymouse
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200128 call test_override('no_query_mouse', 1)
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200129 set mouse=a term=xterm
Bram Moolenaar905dd902019-04-07 14:21:47 +0200130 call setline(1, ['line 1', 'line 2', 'line 3 is a bit longer'])
Bram Moolenaar905dd902019-04-07 14:21:47 +0200131
Bram Moolenaard7885432019-05-03 13:44:10 +0200132 for ttymouse_val in s:ttymouse_values + s:ttymouse_dec + s:ttymouse_netterm
Bram Moolenaar49452192019-04-17 16:27:02 +0200133 let msg = 'ttymouse=' .. ttymouse_val
Bram Moolenaar1ee36d62019-05-01 23:13:56 +0200134 exe 'set ttymouse=' .. ttymouse_val
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200135 go
Bram Moolenaar49452192019-04-17 16:27:02 +0200136 call assert_equal([0, 1, 1, 0], getpos('.'), msg)
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200137 let row = 2
138 let col = 6
139 call MouseLeftClick(row, col)
140 call MouseLeftRelease(row, col)
Bram Moolenaar49452192019-04-17 16:27:02 +0200141 call assert_equal([0, 2, 6, 0], getpos('.'), msg)
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200142 endfor
Bram Moolenaar905dd902019-04-07 14:21:47 +0200143
144 let &mouse = save_mouse
145 let &term = save_term
146 let &ttymouse = save_ttymouse
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200147 call test_override('no_query_mouse', 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +0200148 bwipe!
149endfunc
150
Bram Moolenaar1ee36d62019-05-01 23:13:56 +0200151" Test that <C-LeftMouse> jumps to help tag and <C-RightMouse> jumps back.
152func Test_xterm_mouse_ctrl_click()
153 let save_mouse = &mouse
154 let save_term = &term
155 let save_ttymouse = &ttymouse
156 set mouse=a term=xterm
157
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200158 for ttymouse_val in s:ttymouse_values
Bram Moolenaar1ee36d62019-05-01 23:13:56 +0200159 let msg = 'ttymouse=' .. ttymouse_val
160 exe 'set ttymouse=' .. ttymouse_val
161 help
162 /usr_02.txt
163 norm! zt
164 let row = 1
165 let col = 1
166 call MouseCtrlLeftClick(row, col)
167 call MouseLeftRelease(row, col)
168 call assert_match('usr_02.txt$', bufname('%'), msg)
169 call assert_equal('*usr_02.txt*', expand('<cWORD>'))
170
171 call MouseCtrlRightClick(row, col)
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200172 call MouseRightRelease(row, col)
Bram Moolenaar1ee36d62019-05-01 23:13:56 +0200173 call assert_match('help.txt$', bufname('%'), msg)
174 call assert_equal('|usr_02.txt|', expand('<cWORD>'))
175
176 helpclose
177 endfor
178
179 let &mouse = save_mouse
180 let &term = save_term
181 let &ttymouse = save_ttymouse
182endfunc
183
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200184func Test_term_mouse_middle_click()
Bram Moolenaar52992fe2019-08-12 14:20:33 +0200185 CheckFeature clipboard_working
Bram Moolenaar564344a2019-04-28 13:00:12 +0200186
Bram Moolenaarc1b81602019-04-27 19:11:35 +0200187 new
188 let save_mouse = &mouse
189 let save_term = &term
190 let save_ttymouse = &ttymouse
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200191 call test_override('no_query_mouse', 1)
Bram Moolenaarc1b81602019-04-27 19:11:35 +0200192 let save_quotestar = @*
193 let @* = 'abc'
194 set mouse=a term=xterm
195
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200196 for ttymouse_val in s:ttymouse_values + s:ttymouse_dec
Bram Moolenaarc1b81602019-04-27 19:11:35 +0200197 let msg = 'ttymouse=' .. ttymouse_val
Bram Moolenaar1ee36d62019-05-01 23:13:56 +0200198 exe 'set ttymouse=' .. ttymouse_val
Bram Moolenaarc1b81602019-04-27 19:11:35 +0200199 call setline(1, ['123456789', '123456789'])
200
201 " Middle-click in the middle of the line pastes text where clicked.
202 let row = 1
203 let col = 6
204 call MouseMiddleClick(row, col)
205 call MouseMiddleRelease(row, col)
206 call assert_equal(['12345abc6789', '123456789'], getline(1, '$'), msg)
207
208 " Middle-click beyond end of the line pastes text at the end of the line.
209 let col = 20
210 call MouseMiddleClick(row, col)
211 call MouseMiddleRelease(row, col)
212 call assert_equal(['12345abc6789abc', '123456789'], getline(1, '$'), msg)
213
214 " Middle-click beyond the last line pastes in the last line.
215 let row = 5
216 let col = 3
217 call MouseMiddleClick(row, col)
218 call MouseMiddleRelease(row, col)
219 call assert_equal(['12345abc6789abc', '12abc3456789'], getline(1, '$'), msg)
220 endfor
221
222 let &mouse = save_mouse
223 let &term = save_term
224 let &ttymouse = save_ttymouse
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200225 call test_override('no_query_mouse', 0)
Bram Moolenaarc1b81602019-04-27 19:11:35 +0200226 let @* = save_quotestar
227 bwipe!
228endfunc
229
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200230" TODO: for unclear reasons this test fails if it comes after
231" Test_xterm_mouse_ctrl_click()
232func Test_1xterm_mouse_wheel()
Bram Moolenaar049736f2019-04-07 21:55:07 +0200233 new
234 let save_mouse = &mouse
235 let save_term = &term
236 let save_ttymouse = &ttymouse
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200237 set mouse=a term=xterm
Bram Moolenaar049736f2019-04-07 21:55:07 +0200238 call setline(1, range(1, 100))
239
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200240 for ttymouse_val in s:ttymouse_values
Bram Moolenaar49452192019-04-17 16:27:02 +0200241 let msg = 'ttymouse=' .. ttymouse_val
Bram Moolenaar1ee36d62019-05-01 23:13:56 +0200242 exe 'set ttymouse=' .. ttymouse_val
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200243 go
Bram Moolenaar49452192019-04-17 16:27:02 +0200244 call assert_equal(1, line('w0'), msg)
245 call assert_equal([0, 1, 1, 0], getpos('.'), msg)
Bram Moolenaar049736f2019-04-07 21:55:07 +0200246
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200247 call MouseWheelDown(1, 1)
Bram Moolenaar49452192019-04-17 16:27:02 +0200248 call assert_equal(4, line('w0'), msg)
249 call assert_equal([0, 4, 1, 0], getpos('.'), msg)
Bram Moolenaar049736f2019-04-07 21:55:07 +0200250
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200251 call MouseWheelDown(1, 1)
Bram Moolenaar49452192019-04-17 16:27:02 +0200252 call assert_equal(7, line('w0'), msg)
253 call assert_equal([0, 7, 1, 0], getpos('.'), msg)
Bram Moolenaar049736f2019-04-07 21:55:07 +0200254
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200255 call MouseWheelUp(1, 1)
Bram Moolenaar49452192019-04-17 16:27:02 +0200256 call assert_equal(4, line('w0'), msg)
257 call assert_equal([0, 7, 1, 0], getpos('.'), msg)
Bram Moolenaar049736f2019-04-07 21:55:07 +0200258
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200259 call MouseWheelUp(1, 1)
Bram Moolenaar49452192019-04-17 16:27:02 +0200260 call assert_equal(1, line('w0'), msg)
261 call assert_equal([0, 7, 1, 0], getpos('.'), msg)
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200262 endfor
Bram Moolenaar049736f2019-04-07 21:55:07 +0200263
264 let &mouse = save_mouse
265 let &term = save_term
266 let &ttymouse = save_ttymouse
267 bwipe!
268endfunc
Bram Moolenaar3fb01a52019-04-09 21:52:02 +0200269
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200270func Test_term_mouse_drag_window_separator()
Bram Moolenaar3fb01a52019-04-09 21:52:02 +0200271 let save_mouse = &mouse
272 let save_term = &term
273 let save_ttymouse = &ttymouse
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200274 call test_override('no_query_mouse', 1)
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200275 set mouse=a term=xterm
Bram Moolenaar3fb01a52019-04-09 21:52:02 +0200276
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200277 for ttymouse_val in s:ttymouse_values + s:ttymouse_dec
Bram Moolenaar49452192019-04-17 16:27:02 +0200278 let msg = 'ttymouse=' .. ttymouse_val
Bram Moolenaar1ee36d62019-05-01 23:13:56 +0200279 exe 'set ttymouse=' .. ttymouse_val
Bram Moolenaar3fb01a52019-04-09 21:52:02 +0200280
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200281 " Split horizontally and test dragging the horizontal window separator.
282 split
283 let rowseparator = winheight(0) + 1
284 let row = rowseparator
285 let col = 1
Bram Moolenaarc8b3dda2019-04-12 21:42:52 +0200286
Bram Moolenaar2b00b9b2019-04-17 17:08:27 +0200287 " When 'ttymouse' is 'xterm2', row/col bigger than 223 are not supported.
288 if ttymouse_val !=# 'xterm2' || row <= 223
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200289 call MouseLeftClick(row, col)
290 let row -= 1
291 call MouseLeftDrag(row, col)
Bram Moolenaar49452192019-04-17 16:27:02 +0200292 call assert_equal(rowseparator - 1, winheight(0) + 1, msg)
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200293 let row += 1
294 call MouseLeftDrag(row, col)
Bram Moolenaar49452192019-04-17 16:27:02 +0200295 call assert_equal(rowseparator, winheight(0) + 1, msg)
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200296 call MouseLeftRelease(row, col)
Bram Moolenaar49452192019-04-17 16:27:02 +0200297 call assert_equal(rowseparator, winheight(0) + 1, msg)
Bram Moolenaarc8b3dda2019-04-12 21:42:52 +0200298 endif
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200299 bwipe!
Bram Moolenaar3fb01a52019-04-09 21:52:02 +0200300
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200301 " Split vertically and test dragging the vertical window separator.
302 vsplit
303 let colseparator = winwidth(0) + 1
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200304 let row = 1
305 let col = colseparator
Bram Moolenaar3fb01a52019-04-09 21:52:02 +0200306
Bram Moolenaar2b00b9b2019-04-17 17:08:27 +0200307 " When 'ttymouse' is 'xterm2', row/col bigger than 223 are not supported.
308 if ttymouse_val !=# 'xterm2' || col <= 223
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200309 call MouseLeftClick(row, col)
310 let col -= 1
311 call MouseLeftDrag(row, col)
Bram Moolenaar49452192019-04-17 16:27:02 +0200312 call assert_equal(colseparator - 1, winwidth(0) + 1, msg)
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200313 let col += 1
314 call MouseLeftDrag(row, col)
Bram Moolenaar49452192019-04-17 16:27:02 +0200315 call assert_equal(colseparator, winwidth(0) + 1, msg)
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200316 call MouseLeftRelease(row, col)
Bram Moolenaar49452192019-04-17 16:27:02 +0200317 call assert_equal(colseparator, winwidth(0) + 1, msg)
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200318 endif
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200319 bwipe!
320 endfor
Bram Moolenaar3fb01a52019-04-09 21:52:02 +0200321
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200322 let &mouse = save_mouse
323 let &term = save_term
324 let &ttymouse = save_ttymouse
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200325 call test_override('no_query_mouse', 0)
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200326endfunc
Bram Moolenaar3fb01a52019-04-09 21:52:02 +0200327
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200328func Test_term_mouse_drag_statusline()
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200329 let save_mouse = &mouse
330 let save_term = &term
331 let save_ttymouse = &ttymouse
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200332 call test_override('no_query_mouse', 1)
Bram Moolenaarca57ab52019-04-13 14:53:16 +0200333 let save_laststatus = &laststatus
334 set mouse=a term=xterm laststatus=2
Bram Moolenaar3fb01a52019-04-09 21:52:02 +0200335
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200336 for ttymouse_val in s:ttymouse_values + s:ttymouse_dec
Bram Moolenaar49452192019-04-17 16:27:02 +0200337 let msg = 'ttymouse=' .. ttymouse_val
Bram Moolenaar1ee36d62019-05-01 23:13:56 +0200338 exe 'set ttymouse=' .. ttymouse_val
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200339
Bram Moolenaar49452192019-04-17 16:27:02 +0200340 call assert_equal(1, &cmdheight, msg)
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200341 let rowstatusline = winheight(0) + 1
342 let row = rowstatusline
343 let col = 1
Bram Moolenaarc8b3dda2019-04-12 21:42:52 +0200344
Bram Moolenaar2b00b9b2019-04-17 17:08:27 +0200345 if ttymouse_val ==# 'xterm2' && row > 223
346 " When 'ttymouse' is 'xterm2', row/col bigger than 223 are not supported.
Bram Moolenaarc8b3dda2019-04-12 21:42:52 +0200347 continue
348 endif
349
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200350 call MouseLeftClick(row, col)
351 let row -= 1
352 call MouseLeftDrag(row, col)
Bram Moolenaar49452192019-04-17 16:27:02 +0200353 call assert_equal(2, &cmdheight, msg)
354 call assert_equal(rowstatusline - 1, winheight(0) + 1, msg)
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200355 let row += 1
356 call MouseLeftDrag(row, col)
Bram Moolenaar49452192019-04-17 16:27:02 +0200357 call assert_equal(1, &cmdheight, msg)
358 call assert_equal(rowstatusline, winheight(0) + 1, msg)
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200359 call MouseLeftRelease(row, col)
Bram Moolenaar49452192019-04-17 16:27:02 +0200360 call assert_equal(1, &cmdheight, msg)
361 call assert_equal(rowstatusline, winheight(0) + 1, msg)
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200362 endfor
363
Bram Moolenaar3fb01a52019-04-09 21:52:02 +0200364 let &mouse = save_mouse
365 let &term = save_term
366 let &ttymouse = save_ttymouse
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200367 call test_override('no_query_mouse', 0)
Bram Moolenaarca57ab52019-04-13 14:53:16 +0200368 let &laststatus = save_laststatus
369endfunc
370
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200371func Test_term_mouse_click_tab()
Bram Moolenaarca57ab52019-04-13 14:53:16 +0200372 let save_mouse = &mouse
373 let save_term = &term
374 let save_ttymouse = &ttymouse
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200375 call test_override('no_query_mouse', 1)
Bram Moolenaarca57ab52019-04-13 14:53:16 +0200376 set mouse=a term=xterm
377 let row = 1
378
Bram Moolenaard7885432019-05-03 13:44:10 +0200379 for ttymouse_val in s:ttymouse_values + s:ttymouse_dec + s:ttymouse_netterm
Bram Moolenaar49452192019-04-17 16:27:02 +0200380 let msg = 'ttymouse=' .. ttymouse_val
Bram Moolenaar1ee36d62019-05-01 23:13:56 +0200381 exe 'set ttymouse=' .. ttymouse_val
Bram Moolenaarca57ab52019-04-13 14:53:16 +0200382 e Xfoo
383 tabnew Xbar
384
385 let a = split(execute(':tabs'), "\n")
386 call assert_equal(['Tab page 1',
387 \ ' Xfoo',
388 \ 'Tab page 2',
Bram Moolenaar49452192019-04-17 16:27:02 +0200389 \ '> Xbar'], a, msg)
Bram Moolenaarca57ab52019-04-13 14:53:16 +0200390
391 " Test clicking on tab names in the tabline at the top.
392 let col = 2
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200393 redraw
Bram Moolenaarca57ab52019-04-13 14:53:16 +0200394 call MouseLeftClick(row, col)
395 call MouseLeftRelease(row, col)
396 let a = split(execute(':tabs'), "\n")
397 call assert_equal(['Tab page 1',
398 \ '> Xfoo',
399 \ 'Tab page 2',
Bram Moolenaar49452192019-04-17 16:27:02 +0200400 \ ' Xbar'], a, msg)
Bram Moolenaarca57ab52019-04-13 14:53:16 +0200401
402 let col = 9
403 call MouseLeftClick(row, col)
404 call MouseLeftRelease(row, col)
405 let a = split(execute(':tabs'), "\n")
406 call assert_equal(['Tab page 1',
407 \ ' Xfoo',
408 \ 'Tab page 2',
Bram Moolenaar49452192019-04-17 16:27:02 +0200409 \ '> Xbar'], a, msg)
Bram Moolenaarca57ab52019-04-13 14:53:16 +0200410
411 %bwipe!
412 endfor
413
414 let &mouse = save_mouse
415 let &term = save_term
416 let &ttymouse = save_ttymouse
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200417 call test_override('no_query_mouse', 0)
Bram Moolenaar3fb01a52019-04-09 21:52:02 +0200418endfunc
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200419
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200420func Test_term_mouse_click_X_to_close_tab()
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200421 let save_mouse = &mouse
422 let save_term = &term
423 let save_ttymouse = &ttymouse
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200424 call test_override('no_query_mouse', 1)
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200425 set mouse=a term=xterm
426 let row = 1
427 let col = &columns
428
Bram Moolenaard7885432019-05-03 13:44:10 +0200429 for ttymouse_val in s:ttymouse_values + s:ttymouse_dec + s:ttymouse_netterm
Bram Moolenaar2b00b9b2019-04-17 17:08:27 +0200430 if ttymouse_val ==# 'xterm2' && col > 223
431 " When 'ttymouse' is 'xterm2', row/col bigger than 223 are not supported.
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200432 continue
433 endif
Bram Moolenaar49452192019-04-17 16:27:02 +0200434 let msg = 'ttymouse=' .. ttymouse_val
Bram Moolenaar1ee36d62019-05-01 23:13:56 +0200435 exe 'set ttymouse=' .. ttymouse_val
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200436 e Xtab1
437 tabnew Xtab2
438 tabnew Xtab3
439 tabn 2
440
441 let a = split(execute(':tabs'), "\n")
442 call assert_equal(['Tab page 1',
443 \ ' Xtab1',
444 \ 'Tab page 2',
445 \ '> Xtab2',
446 \ 'Tab page 3',
Bram Moolenaar49452192019-04-17 16:27:02 +0200447 \ ' Xtab3'], a, msg)
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200448
449 " Click on "X" in tabline to close current tab i.e. Xtab2.
450 redraw
451 call MouseLeftClick(row, col)
452 call MouseLeftRelease(row, col)
453 let a = split(execute(':tabs'), "\n")
454 call assert_equal(['Tab page 1',
455 \ ' Xtab1',
456 \ 'Tab page 2',
Bram Moolenaar49452192019-04-17 16:27:02 +0200457 \ '> Xtab3'], a, msg)
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200458
459 %bwipe!
460 endfor
461
462 let &mouse = save_mouse
463 let &term = save_term
464 let &ttymouse = save_ttymouse
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200465 call test_override('no_query_mouse', 0)
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200466endfunc
Bram Moolenaare3e38282019-04-15 20:55:31 +0200467
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200468func Test_term_mouse_drag_to_move_tab()
Bram Moolenaare3e38282019-04-15 20:55:31 +0200469 let save_mouse = &mouse
470 let save_term = &term
471 let save_ttymouse = &ttymouse
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200472 call test_override('no_query_mouse', 1)
Bram Moolenaare3e38282019-04-15 20:55:31 +0200473 " Set 'mousetime' to 1 to avoid recognizing a double-click in the loop
474 set mouse=a term=xterm mousetime=1
475 let row = 1
476
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200477 for ttymouse_val in s:ttymouse_values + s:ttymouse_dec
Bram Moolenaar49452192019-04-17 16:27:02 +0200478 let msg = 'ttymouse=' .. ttymouse_val
Bram Moolenaar1ee36d62019-05-01 23:13:56 +0200479 exe 'set ttymouse=' .. ttymouse_val
Bram Moolenaare3e38282019-04-15 20:55:31 +0200480 e Xtab1
481 tabnew Xtab2
482
483 let a = split(execute(':tabs'), "\n")
484 call assert_equal(['Tab page 1',
485 \ ' Xtab1',
486 \ 'Tab page 2',
Bram Moolenaar49452192019-04-17 16:27:02 +0200487 \ '> Xtab2'], a, msg)
Bram Moolenaare3e38282019-04-15 20:55:31 +0200488 redraw
489
490 " Click in tab2 and drag it to tab1.
491 " Check getcharmod() to verify that click is not
492 " interpreted as a spurious double-click.
493 call MouseLeftClick(row, 10)
Bram Moolenaar49452192019-04-17 16:27:02 +0200494 call assert_equal(0, getcharmod(), msg)
Bram Moolenaare3e38282019-04-15 20:55:31 +0200495 for col in [9, 8, 7, 6]
496 call MouseLeftDrag(row, col)
497 endfor
498 call MouseLeftRelease(row, col)
499 let a = split(execute(':tabs'), "\n")
500 call assert_equal(['Tab page 1',
501 \ '> Xtab2',
502 \ 'Tab page 2',
Bram Moolenaar49452192019-04-17 16:27:02 +0200503 \ ' Xtab1'], a, msg)
Bram Moolenaare3e38282019-04-15 20:55:31 +0200504
Bram Moolenaar7f279762019-04-15 21:48:22 +0200505 " brief sleep to avoid causing a double-click
506 sleep 20m
Bram Moolenaare3e38282019-04-15 20:55:31 +0200507 %bwipe!
508 endfor
509
510 let &mouse = save_mouse
511 let &term = save_term
512 let &ttymouse = save_ttymouse
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200513 call test_override('no_query_mouse', 0)
Bram Moolenaare3e38282019-04-15 20:55:31 +0200514 set mousetime&
515endfunc
516
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200517func Test_term_mouse_double_click_to_create_tab()
Bram Moolenaare3e38282019-04-15 20:55:31 +0200518 let save_mouse = &mouse
519 let save_term = &term
520 let save_ttymouse = &ttymouse
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200521 call test_override('no_query_mouse', 1)
Bram Moolenaare3e38282019-04-15 20:55:31 +0200522 " Set 'mousetime' to a small value, so that double-click works but we don't
523 " have to wait long to avoid a triple-click.
524 set mouse=a term=xterm mousetime=100
525 let row = 1
526 let col = 10
527
Bram Moolenaard0621d82019-05-02 21:12:19 +0200528 let round = 0
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200529 for ttymouse_val in s:ttymouse_values + s:ttymouse_dec
Bram Moolenaar49452192019-04-17 16:27:02 +0200530 let msg = 'ttymouse=' .. ttymouse_val
Bram Moolenaar1ee36d62019-05-01 23:13:56 +0200531 exe 'set ttymouse=' .. ttymouse_val
Bram Moolenaare3e38282019-04-15 20:55:31 +0200532 e Xtab1
533 tabnew Xtab2
534
Bram Moolenaard0621d82019-05-02 21:12:19 +0200535 if round > 0
536 " We need to sleep, or else the first MouseLeftClick() will be
537 " interpreted as a spurious triple-click.
538 sleep 100m
539 endif
540 let round += 1
541
Bram Moolenaare3e38282019-04-15 20:55:31 +0200542 let a = split(execute(':tabs'), "\n")
543 call assert_equal(['Tab page 1',
544 \ ' Xtab1',
545 \ 'Tab page 2',
Bram Moolenaar49452192019-04-17 16:27:02 +0200546 \ '> Xtab2'], a, msg)
Bram Moolenaare3e38282019-04-15 20:55:31 +0200547
548 redraw
549 call MouseLeftClick(row, col)
550 " Check getcharmod() to verify that first click is not
551 " interpreted as a spurious double-click.
Bram Moolenaar49452192019-04-17 16:27:02 +0200552 call assert_equal(0, getcharmod(), msg)
Bram Moolenaare3e38282019-04-15 20:55:31 +0200553 call MouseLeftRelease(row, col)
554 call MouseLeftClick(row, col)
Bram Moolenaar49452192019-04-17 16:27:02 +0200555 call assert_equal(32, getcharmod(), msg) " double-click
Bram Moolenaare3e38282019-04-15 20:55:31 +0200556 call MouseLeftRelease(row, col)
557 let a = split(execute(':tabs'), "\n")
558 call assert_equal(['Tab page 1',
559 \ ' Xtab1',
560 \ 'Tab page 2',
561 \ '> [No Name]',
562 \ 'Tab page 3',
Bram Moolenaar49452192019-04-17 16:27:02 +0200563 \ ' Xtab2'], a, msg)
Bram Moolenaare3e38282019-04-15 20:55:31 +0200564
Bram Moolenaare3e38282019-04-15 20:55:31 +0200565 %bwipe!
566 endfor
567
568 let &mouse = save_mouse
569 let &term = save_term
570 let &ttymouse = save_ttymouse
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200571 call test_override('no_query_mouse', 0)
Bram Moolenaare3e38282019-04-15 20:55:31 +0200572 set mousetime&
573endfunc
Bram Moolenaar696d6372019-04-17 16:33:46 +0200574
575func Test_xterm_mouse_click_in_fold_columns()
576 new
577 let save_mouse = &mouse
578 let save_term = &term
579 let save_ttymouse = &ttymouse
580 let save_foldcolumn = &foldcolumn
Bram Moolenaar2b00b9b2019-04-17 17:08:27 +0200581 set mouse=a term=xterm foldcolumn=3 ttymouse=xterm2
Bram Moolenaar696d6372019-04-17 16:33:46 +0200582
583 " Create 2 nested folds.
584 call setline(1, range(1, 7))
585 2,6fold
586 norm! zR
587 4,5fold
588 call assert_equal([-1, -1, -1, 4, 4, -1, -1],
589 \ map(range(1, 7), 'foldclosed(v:val)'))
590
591 " Click in "+" of inner fold in foldcolumn should open it.
592 redraw
593 let row = 4
594 let col = 2
595 call MouseLeftClick(row, col)
596 call MouseLeftRelease(row, col)
597 call assert_equal([-1, -1, -1, -1, -1, -1, -1],
598 \ map(range(1, 7), 'foldclosed(v:val)'))
599
600 " Click in "-" of outer fold in foldcolumn should close it.
601 redraw
602 let row = 2
603 let col = 1
604 call MouseLeftClick(row, col)
605 call MouseLeftRelease(row, col)
606 call assert_equal([-1, 2, 2, 2, 2, 2, -1],
607 \ map(range(1, 7), 'foldclosed(v:val)'))
608 norm! zR
609
610 " Click in "|" of inner fold in foldcolumn should close it.
611 redraw
612 let row = 5
613 let col = 2
614 call MouseLeftClick(row, col)
615 call MouseLeftRelease(row, col)
616 call assert_equal([-1, -1, -1, 4, 4, -1, -1],
617 \ map(range(1, 7), 'foldclosed(v:val)'))
618
619 let &foldcolumn = save_foldcolumn
620 let &ttymouse = save_ttymouse
621 let &term = save_term
622 let &mouse = save_mouse
623 bwipe!
624endfunc
Bram Moolenaar66761db2019-06-05 22:07:51 +0200625
626" This only checks if the sequence is recognized.
Bram Moolenaar66761db2019-06-05 22:07:51 +0200627func Test_term_rgb_response()
628 set t_RF=x
629 set t_RB=y
630
631 " response to t_RF, 4 digits
632 let red = 0x12
633 let green = 0x34
634 let blue = 0x56
635 let seq = printf("\<Esc>]10;rgb:%02x00/%02x00/%02x00\x07", red, green, blue)
636 call feedkeys(seq, 'Lx!')
637 call assert_equal(seq, v:termrfgresp)
638
639 " response to t_RF, 2 digits
640 let red = 0x78
641 let green = 0x9a
642 let blue = 0xbc
643 let seq = printf("\<Esc>]10;rgb:%02x/%02x/%02x\x07", red, green, blue)
644 call feedkeys(seq, 'Lx!')
645 call assert_equal(seq, v:termrfgresp)
646
Bram Moolenaar32e19772019-06-05 22:57:04 +0200647 " response to t_RB, 4 digits, dark
648 set background=light
649 call test_option_not_set('background')
650 let red = 0x29
651 let green = 0x4a
652 let blue = 0x6b
653 let seq = printf("\<Esc>]11;rgb:%02x00/%02x00/%02x00\x07", red, green, blue)
654 call feedkeys(seq, 'Lx!')
655 call assert_equal(seq, v:termrbgresp)
656 call assert_equal('dark', &background)
657
658 " response to t_RB, 4 digits, light
659 set background=dark
660 call test_option_not_set('background')
661 let red = 0x81
662 let green = 0x63
Bram Moolenaar66761db2019-06-05 22:07:51 +0200663 let blue = 0x65
664 let seq = printf("\<Esc>]11;rgb:%02x00/%02x00/%02x00\x07", red, green, blue)
665 call feedkeys(seq, 'Lx!')
666 call assert_equal(seq, v:termrbgresp)
Bram Moolenaar32e19772019-06-05 22:57:04 +0200667 call assert_equal('light', &background)
Bram Moolenaar66761db2019-06-05 22:07:51 +0200668
Bram Moolenaar32e19772019-06-05 22:57:04 +0200669 " response to t_RB, 2 digits, dark
670 set background=light
671 call test_option_not_set('background')
672 let red = 0x47
673 let green = 0x59
674 let blue = 0x5b
Bram Moolenaar66761db2019-06-05 22:07:51 +0200675 let seq = printf("\<Esc>]11;rgb:%02x/%02x/%02x\x07", red, green, blue)
676 call feedkeys(seq, 'Lx!')
677 call assert_equal(seq, v:termrbgresp)
Bram Moolenaar32e19772019-06-05 22:57:04 +0200678 call assert_equal('dark', &background)
679
680 " response to t_RB, 2 digits, light
681 set background=dark
682 call test_option_not_set('background')
683 let red = 0x83
684 let green = 0xa4
685 let blue = 0xc2
686 let seq = printf("\<Esc>]11;rgb:%02x/%02x/%02x\x07", red, green, blue)
687 call feedkeys(seq, 'Lx!')
688 call assert_equal(seq, v:termrbgresp)
689 call assert_equal('light', &background)
Bram Moolenaar66761db2019-06-05 22:07:51 +0200690
691 set t_RF= t_RB=
692endfunc
693
694" This only checks if the sequence is recognized.
695" This must be last, because it has side effects to xterm properties.
696" TODO: check that the values were parsed properly
697func Test_xx_term_style_response()
698 " Termresponse is only parsed when t_RV is not empty.
699 set t_RV=x
700
701 " send the termresponse to trigger requesting the XT codes
702 let seq = "\<Esc>[>41;337;0c"
703 call feedkeys(seq, 'Lx!')
704 call assert_equal(seq, v:termresponse)
705
706 let seq = "\<Esc>P1$r2 q\<Esc>\\"
707 call feedkeys(seq, 'Lx!')
708 call assert_equal(seq, v:termstyleresp)
709
710 set t_RV=
711endfunc
Bram Moolenaarde6dbb42019-06-06 11:59:18 +0200712
713func Test_get_termcode()
714 let k1 = &t_k1
715 set t_k1=
716 set t_k1&
717 call assert_equal(k1, &t_k1)
Bram Moolenaar9aeb3362019-06-06 12:36:15 +0200718
719 " use external termcap first
720 set nottybuiltin
721 set t_k1=
722 set t_k1&
723 " when using external termcap may get something else, but it must not be
724 " empty, since we would fallback to the builtin one.
725 call assert_notequal('', &t_k1)
726
727 if &term =~ 'xterm'
728 " use internal termcap first
729 let term_save = &term
730 let &term = 'builtin_' .. &term
731 set t_k1=
732 set t_k1&
733 call assert_equal(k1, &t_k1)
734 let &term = term_save
735 endif
736
737 set ttybuiltin
Bram Moolenaarde6dbb42019-06-06 11:59:18 +0200738endfunc