blob: 17c1534e884f82235740d213406c580f8f3af91e [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 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 Moolenaar3fbd2d72019-04-11 23:56:16 +020023" Helper function to emit a terminal escape code.
Bram Moolenaard0621d82019-05-02 21:12:19 +020024func TerminalEscapeCode(code, row, col, m)
Bram Moolenaar2b00b9b2019-04-17 17:08:27 +020025 if &ttymouse ==# 'xterm2'
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +020026 " need to use byte encoding here.
Bram Moolenaard0621d82019-05-02 21:12:19 +020027 let str = list2str([a:code + 0x20, a:col + 0x20, a:row + 0x20])
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +020028 if has('iconv')
29 let bytes = iconv(str, 'utf-8', 'latin1')
30 else
31 " Hopefully the numbers are not too big.
32 let bytes = str
33 endif
34 call feedkeys("\<Esc>[M" .. bytes, 'Lx!')
35 elseif &ttymouse ==# 'sgr'
Bram Moolenaard0621d82019-05-02 21:12:19 +020036 call feedkeys(printf("\<Esc>[<%d;%d;%d%s", a:code, a:col, a:row, a:m), 'Lx!')
37 elseif &ttymouse ==# 'urxvt'
38 call feedkeys(printf("\<Esc>[%d;%d;%dM", a:code + 0x20, a:col, a:row), 'Lx!')
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +020039 endif
40endfunc
41
Bram Moolenaar92fd5992019-05-02 23:00:22 +020042func DecEscapeCode(code, down, row, col)
43 call feedkeys(printf("\<Esc>[%d;%d;%d;%d&w", a:code, a:down, a:row, a:col), 'Lx!')
44endfunc
45
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +020046func MouseLeftClick(row, col)
Bram Moolenaar92fd5992019-05-02 23:00:22 +020047 if &ttymouse ==# 'dec'
48 call DecEscapeCode(2, 4, a:row, a:col)
49 else
50 call TerminalEscapeCode(0, a:row, a:col, 'M')
51 endif
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +020052endfunc
53
Bram Moolenaarc1b81602019-04-27 19:11:35 +020054func MouseMiddleClick(row, col)
Bram Moolenaar92fd5992019-05-02 23:00:22 +020055 if &ttymouse ==# 'dec'
56 call DecEscapeCode(4, 2, a:row, a:col)
57 else
58 call TerminalEscapeCode(1, a:row, a:col, 'M')
59 endif
Bram Moolenaarc1b81602019-04-27 19:11:35 +020060endfunc
61
Bram Moolenaar1ee36d62019-05-01 23:13:56 +020062func MouseCtrlLeftClick(row, col)
63 let ctrl = 0x10
Bram Moolenaard0621d82019-05-02 21:12:19 +020064 call TerminalEscapeCode(0 + ctrl, a:row, a:col, 'M')
Bram Moolenaar1ee36d62019-05-01 23:13:56 +020065endfunc
66
67func MouseCtrlRightClick(row, col)
68 let ctrl = 0x10
Bram Moolenaard0621d82019-05-02 21:12:19 +020069 call TerminalEscapeCode(2 + ctrl, a:row, a:col, 'M')
Bram Moolenaar1ee36d62019-05-01 23:13:56 +020070endfunc
71
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +020072func MouseLeftRelease(row, col)
Bram Moolenaar92fd5992019-05-02 23:00:22 +020073 if &ttymouse ==# 'dec'
74 call DecEscapeCode(3, 0, a:row, a:col)
75 else
76 call TerminalEscapeCode(3, a:row, a:col, 'm')
77 endif
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +020078endfunc
79
Bram Moolenaarc1b81602019-04-27 19:11:35 +020080func MouseMiddleRelease(row, col)
Bram Moolenaar92fd5992019-05-02 23:00:22 +020081 if &ttymouse ==# 'dec'
82 call DecEscapeCode(5, 0, a:row, a:col)
83 else
84 call TerminalEscapeCode(3, a:row, a:col, 'm')
85 endif
Bram Moolenaarc1b81602019-04-27 19:11:35 +020086endfunc
87
Bram Moolenaar1ee36d62019-05-01 23:13:56 +020088func MouseRightRelease(row, col)
Bram Moolenaard0621d82019-05-02 21:12:19 +020089 call TerminalEscapeCode(3, a:row, a:col, 'm')
Bram Moolenaar1ee36d62019-05-01 23:13:56 +020090endfunc
91
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +020092func MouseLeftDrag(row, col)
Bram Moolenaar92fd5992019-05-02 23:00:22 +020093 if &ttymouse ==# 'dec'
94 call DecEscapeCode(1, 4, a:row, a:col)
95 else
96 call TerminalEscapeCode(0x20, a:row, a:col, 'M')
97 endif
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +020098endfunc
99
100func MouseWheelUp(row, col)
Bram Moolenaard0621d82019-05-02 21:12:19 +0200101 call TerminalEscapeCode(0x40, a:row, a:col, 'M')
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200102endfunc
103
104func MouseWheelDown(row, col)
Bram Moolenaard0621d82019-05-02 21:12:19 +0200105 call TerminalEscapeCode(0x41, a:row, a:col, 'M')
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200106endfunc
107
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200108func Test_term_mouse_left_click()
Bram Moolenaar905dd902019-04-07 14:21:47 +0200109 new
110 let save_mouse = &mouse
111 let save_term = &term
112 let save_ttymouse = &ttymouse
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200113 call test_override('no_query_mouse', 1)
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200114 set mouse=a term=xterm
Bram Moolenaar905dd902019-04-07 14:21:47 +0200115 call setline(1, ['line 1', 'line 2', 'line 3 is a bit longer'])
Bram Moolenaar905dd902019-04-07 14:21:47 +0200116
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200117 for ttymouse_val in s:ttymouse_values + s:ttymouse_dec
Bram Moolenaar49452192019-04-17 16:27:02 +0200118 let msg = 'ttymouse=' .. ttymouse_val
Bram Moolenaar1ee36d62019-05-01 23:13:56 +0200119 exe 'set ttymouse=' .. ttymouse_val
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200120 go
Bram Moolenaar49452192019-04-17 16:27:02 +0200121 call assert_equal([0, 1, 1, 0], getpos('.'), msg)
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200122 let row = 2
123 let col = 6
124 call MouseLeftClick(row, col)
125 call MouseLeftRelease(row, col)
Bram Moolenaar49452192019-04-17 16:27:02 +0200126 call assert_equal([0, 2, 6, 0], getpos('.'), msg)
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200127 endfor
Bram Moolenaar905dd902019-04-07 14:21:47 +0200128
129 let &mouse = save_mouse
130 let &term = save_term
131 let &ttymouse = save_ttymouse
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200132 call test_override('no_query_mouse', 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +0200133 bwipe!
134endfunc
135
Bram Moolenaar1ee36d62019-05-01 23:13:56 +0200136" Test that <C-LeftMouse> jumps to help tag and <C-RightMouse> jumps back.
137func Test_xterm_mouse_ctrl_click()
138 let save_mouse = &mouse
139 let save_term = &term
140 let save_ttymouse = &ttymouse
141 set mouse=a term=xterm
142
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200143 for ttymouse_val in s:ttymouse_values
Bram Moolenaar1ee36d62019-05-01 23:13:56 +0200144 let msg = 'ttymouse=' .. ttymouse_val
145 exe 'set ttymouse=' .. ttymouse_val
146 help
147 /usr_02.txt
148 norm! zt
149 let row = 1
150 let col = 1
151 call MouseCtrlLeftClick(row, col)
152 call MouseLeftRelease(row, col)
153 call assert_match('usr_02.txt$', bufname('%'), msg)
154 call assert_equal('*usr_02.txt*', expand('<cWORD>'))
155
156 call MouseCtrlRightClick(row, col)
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200157 call MouseRightRelease(row, col)
Bram Moolenaar1ee36d62019-05-01 23:13:56 +0200158 call assert_match('help.txt$', bufname('%'), msg)
159 call assert_equal('|usr_02.txt|', expand('<cWORD>'))
160
161 helpclose
162 endfor
163
164 let &mouse = save_mouse
165 let &term = save_term
166 let &ttymouse = save_ttymouse
167endfunc
168
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200169func Test_term_mouse_middle_click()
Bram Moolenaar564344a2019-04-28 13:00:12 +0200170 if !WorkingClipboard()
171 throw 'Skipped: No working clipboard'
172 endif
173
Bram Moolenaarc1b81602019-04-27 19:11:35 +0200174 new
175 let save_mouse = &mouse
176 let save_term = &term
177 let save_ttymouse = &ttymouse
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200178 call test_override('no_query_mouse', 1)
Bram Moolenaarc1b81602019-04-27 19:11:35 +0200179 let save_quotestar = @*
180 let @* = 'abc'
181 set mouse=a term=xterm
182
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200183 for ttymouse_val in s:ttymouse_values + s:ttymouse_dec
Bram Moolenaarc1b81602019-04-27 19:11:35 +0200184 let msg = 'ttymouse=' .. ttymouse_val
Bram Moolenaar1ee36d62019-05-01 23:13:56 +0200185 exe 'set ttymouse=' .. ttymouse_val
Bram Moolenaarc1b81602019-04-27 19:11:35 +0200186 call setline(1, ['123456789', '123456789'])
187
188 " Middle-click in the middle of the line pastes text where clicked.
189 let row = 1
190 let col = 6
191 call MouseMiddleClick(row, col)
192 call MouseMiddleRelease(row, col)
193 call assert_equal(['12345abc6789', '123456789'], getline(1, '$'), msg)
194
195 " Middle-click beyond end of the line pastes text at the end of the line.
196 let col = 20
197 call MouseMiddleClick(row, col)
198 call MouseMiddleRelease(row, col)
199 call assert_equal(['12345abc6789abc', '123456789'], getline(1, '$'), msg)
200
201 " Middle-click beyond the last line pastes in the last line.
202 let row = 5
203 let col = 3
204 call MouseMiddleClick(row, col)
205 call MouseMiddleRelease(row, col)
206 call assert_equal(['12345abc6789abc', '12abc3456789'], getline(1, '$'), msg)
207 endfor
208
209 let &mouse = save_mouse
210 let &term = save_term
211 let &ttymouse = save_ttymouse
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200212 call test_override('no_query_mouse', 0)
Bram Moolenaarc1b81602019-04-27 19:11:35 +0200213 let @* = save_quotestar
214 bwipe!
215endfunc
216
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200217" TODO: for unclear reasons this test fails if it comes after
218" Test_xterm_mouse_ctrl_click()
219func Test_1xterm_mouse_wheel()
Bram Moolenaar049736f2019-04-07 21:55:07 +0200220 new
221 let save_mouse = &mouse
222 let save_term = &term
223 let save_ttymouse = &ttymouse
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200224 set mouse=a term=xterm
Bram Moolenaar049736f2019-04-07 21:55:07 +0200225 call setline(1, range(1, 100))
226
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200227 for ttymouse_val in s:ttymouse_values
Bram Moolenaar49452192019-04-17 16:27:02 +0200228 let msg = 'ttymouse=' .. ttymouse_val
Bram Moolenaar1ee36d62019-05-01 23:13:56 +0200229 exe 'set ttymouse=' .. ttymouse_val
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200230 go
Bram Moolenaar49452192019-04-17 16:27:02 +0200231 call assert_equal(1, line('w0'), msg)
232 call assert_equal([0, 1, 1, 0], getpos('.'), msg)
Bram Moolenaar049736f2019-04-07 21:55:07 +0200233
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200234 call MouseWheelDown(1, 1)
Bram Moolenaar49452192019-04-17 16:27:02 +0200235 call assert_equal(4, line('w0'), msg)
236 call assert_equal([0, 4, 1, 0], getpos('.'), msg)
Bram Moolenaar049736f2019-04-07 21:55:07 +0200237
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200238 call MouseWheelDown(1, 1)
Bram Moolenaar49452192019-04-17 16:27:02 +0200239 call assert_equal(7, line('w0'), msg)
240 call assert_equal([0, 7, 1, 0], getpos('.'), msg)
Bram Moolenaar049736f2019-04-07 21:55:07 +0200241
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200242 call MouseWheelUp(1, 1)
Bram Moolenaar49452192019-04-17 16:27:02 +0200243 call assert_equal(4, line('w0'), msg)
244 call assert_equal([0, 7, 1, 0], getpos('.'), msg)
Bram Moolenaar049736f2019-04-07 21:55:07 +0200245
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200246 call MouseWheelUp(1, 1)
Bram Moolenaar49452192019-04-17 16:27:02 +0200247 call assert_equal(1, line('w0'), msg)
248 call assert_equal([0, 7, 1, 0], getpos('.'), msg)
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200249 endfor
Bram Moolenaar049736f2019-04-07 21:55:07 +0200250
251 let &mouse = save_mouse
252 let &term = save_term
253 let &ttymouse = save_ttymouse
254 bwipe!
255endfunc
Bram Moolenaar3fb01a52019-04-09 21:52:02 +0200256
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200257func Test_term_mouse_drag_window_separator()
Bram Moolenaar3fb01a52019-04-09 21:52:02 +0200258 let save_mouse = &mouse
259 let save_term = &term
260 let save_ttymouse = &ttymouse
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200261 call test_override('no_query_mouse', 1)
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200262 set mouse=a term=xterm
Bram Moolenaar3fb01a52019-04-09 21:52:02 +0200263
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200264 for ttymouse_val in s:ttymouse_values + s:ttymouse_dec
Bram Moolenaar49452192019-04-17 16:27:02 +0200265 let msg = 'ttymouse=' .. ttymouse_val
Bram Moolenaar1ee36d62019-05-01 23:13:56 +0200266 exe 'set ttymouse=' .. ttymouse_val
Bram Moolenaar3fb01a52019-04-09 21:52:02 +0200267
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200268 " Split horizontally and test dragging the horizontal window separator.
269 split
270 let rowseparator = winheight(0) + 1
271 let row = rowseparator
272 let col = 1
Bram Moolenaarc8b3dda2019-04-12 21:42:52 +0200273
Bram Moolenaar2b00b9b2019-04-17 17:08:27 +0200274 " When 'ttymouse' is 'xterm2', row/col bigger than 223 are not supported.
275 if ttymouse_val !=# 'xterm2' || row <= 223
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200276 call MouseLeftClick(row, col)
277 let row -= 1
278 call MouseLeftDrag(row, col)
Bram Moolenaar49452192019-04-17 16:27:02 +0200279 call assert_equal(rowseparator - 1, winheight(0) + 1, msg)
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200280 let row += 1
281 call MouseLeftDrag(row, col)
Bram Moolenaar49452192019-04-17 16:27:02 +0200282 call assert_equal(rowseparator, winheight(0) + 1, msg)
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200283 call MouseLeftRelease(row, col)
Bram Moolenaar49452192019-04-17 16:27:02 +0200284 call assert_equal(rowseparator, winheight(0) + 1, msg)
Bram Moolenaarc8b3dda2019-04-12 21:42:52 +0200285 endif
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200286 bwipe!
Bram Moolenaar3fb01a52019-04-09 21:52:02 +0200287
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200288 " Split vertically and test dragging the vertical window separator.
289 vsplit
290 let colseparator = winwidth(0) + 1
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200291 let row = 1
292 let col = colseparator
Bram Moolenaar3fb01a52019-04-09 21:52:02 +0200293
Bram Moolenaar2b00b9b2019-04-17 17:08:27 +0200294 " When 'ttymouse' is 'xterm2', row/col bigger than 223 are not supported.
295 if ttymouse_val !=# 'xterm2' || col <= 223
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200296 call MouseLeftClick(row, col)
297 let col -= 1
298 call MouseLeftDrag(row, col)
Bram Moolenaar49452192019-04-17 16:27:02 +0200299 call assert_equal(colseparator - 1, winwidth(0) + 1, msg)
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200300 let col += 1
301 call MouseLeftDrag(row, col)
Bram Moolenaar49452192019-04-17 16:27:02 +0200302 call assert_equal(colseparator, winwidth(0) + 1, msg)
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200303 call MouseLeftRelease(row, col)
Bram Moolenaar49452192019-04-17 16:27:02 +0200304 call assert_equal(colseparator, winwidth(0) + 1, msg)
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200305 endif
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200306 bwipe!
307 endfor
Bram Moolenaar3fb01a52019-04-09 21:52:02 +0200308
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200309 let &mouse = save_mouse
310 let &term = save_term
311 let &ttymouse = save_ttymouse
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200312 call test_override('no_query_mouse', 0)
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200313endfunc
Bram Moolenaar3fb01a52019-04-09 21:52:02 +0200314
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200315func Test_term_mouse_drag_statusline()
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200316 let save_mouse = &mouse
317 let save_term = &term
318 let save_ttymouse = &ttymouse
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200319 call test_override('no_query_mouse', 1)
Bram Moolenaarca57ab52019-04-13 14:53:16 +0200320 let save_laststatus = &laststatus
321 set mouse=a term=xterm laststatus=2
Bram Moolenaar3fb01a52019-04-09 21:52:02 +0200322
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200323 for ttymouse_val in s:ttymouse_values + s:ttymouse_dec
Bram Moolenaar49452192019-04-17 16:27:02 +0200324 let msg = 'ttymouse=' .. ttymouse_val
Bram Moolenaar1ee36d62019-05-01 23:13:56 +0200325 exe 'set ttymouse=' .. ttymouse_val
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200326
Bram Moolenaar49452192019-04-17 16:27:02 +0200327 call assert_equal(1, &cmdheight, msg)
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200328 let rowstatusline = winheight(0) + 1
329 let row = rowstatusline
330 let col = 1
Bram Moolenaarc8b3dda2019-04-12 21:42:52 +0200331
Bram Moolenaar2b00b9b2019-04-17 17:08:27 +0200332 if ttymouse_val ==# 'xterm2' && row > 223
333 " When 'ttymouse' is 'xterm2', row/col bigger than 223 are not supported.
Bram Moolenaarc8b3dda2019-04-12 21:42:52 +0200334 continue
335 endif
336
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200337 call MouseLeftClick(row, col)
338 let row -= 1
339 call MouseLeftDrag(row, col)
Bram Moolenaar49452192019-04-17 16:27:02 +0200340 call assert_equal(2, &cmdheight, msg)
341 call assert_equal(rowstatusline - 1, winheight(0) + 1, msg)
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200342 let row += 1
343 call MouseLeftDrag(row, col)
Bram Moolenaar49452192019-04-17 16:27:02 +0200344 call assert_equal(1, &cmdheight, msg)
345 call assert_equal(rowstatusline, winheight(0) + 1, msg)
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200346 call MouseLeftRelease(row, col)
Bram Moolenaar49452192019-04-17 16:27:02 +0200347 call assert_equal(1, &cmdheight, msg)
348 call assert_equal(rowstatusline, winheight(0) + 1, msg)
Bram Moolenaar3fbd2d72019-04-11 23:56:16 +0200349 endfor
350
Bram Moolenaar3fb01a52019-04-09 21:52:02 +0200351 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 Moolenaarca57ab52019-04-13 14:53:16 +0200355 let &laststatus = save_laststatus
356endfunc
357
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200358func Test_term_mouse_click_tab()
Bram Moolenaarca57ab52019-04-13 14:53:16 +0200359 let save_mouse = &mouse
360 let save_term = &term
361 let save_ttymouse = &ttymouse
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200362 call test_override('no_query_mouse', 1)
Bram Moolenaarca57ab52019-04-13 14:53:16 +0200363 set mouse=a term=xterm
364 let row = 1
365
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200366 for ttymouse_val in s:ttymouse_values + s:ttymouse_dec
Bram Moolenaar49452192019-04-17 16:27:02 +0200367 let msg = 'ttymouse=' .. ttymouse_val
Bram Moolenaar1ee36d62019-05-01 23:13:56 +0200368 exe 'set ttymouse=' .. ttymouse_val
Bram Moolenaarca57ab52019-04-13 14:53:16 +0200369 e Xfoo
370 tabnew Xbar
371
372 let a = split(execute(':tabs'), "\n")
373 call assert_equal(['Tab page 1',
374 \ ' Xfoo',
375 \ 'Tab page 2',
Bram Moolenaar49452192019-04-17 16:27:02 +0200376 \ '> Xbar'], a, msg)
Bram Moolenaarca57ab52019-04-13 14:53:16 +0200377
378 " Test clicking on tab names in the tabline at the top.
379 let col = 2
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200380 redraw
Bram Moolenaarca57ab52019-04-13 14:53:16 +0200381 call MouseLeftClick(row, col)
382 call MouseLeftRelease(row, col)
383 let a = split(execute(':tabs'), "\n")
384 call assert_equal(['Tab page 1',
385 \ '> Xfoo',
386 \ 'Tab page 2',
Bram Moolenaar49452192019-04-17 16:27:02 +0200387 \ ' Xbar'], a, msg)
Bram Moolenaarca57ab52019-04-13 14:53:16 +0200388
389 let col = 9
390 call MouseLeftClick(row, col)
391 call MouseLeftRelease(row, col)
392 let a = split(execute(':tabs'), "\n")
393 call assert_equal(['Tab page 1',
394 \ ' Xfoo',
395 \ 'Tab page 2',
Bram Moolenaar49452192019-04-17 16:27:02 +0200396 \ '> Xbar'], a, msg)
Bram Moolenaarca57ab52019-04-13 14:53:16 +0200397
398 %bwipe!
399 endfor
400
401 let &mouse = save_mouse
402 let &term = save_term
403 let &ttymouse = save_ttymouse
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200404 call test_override('no_query_mouse', 0)
Bram Moolenaar3fb01a52019-04-09 21:52:02 +0200405endfunc
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200406
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200407func Test_term_mouse_click_X_to_close_tab()
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200408 let save_mouse = &mouse
409 let save_term = &term
410 let save_ttymouse = &ttymouse
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200411 call test_override('no_query_mouse', 1)
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200412 set mouse=a term=xterm
413 let row = 1
414 let col = &columns
415
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200416 for ttymouse_val in s:ttymouse_values + s:ttymouse_dec
Bram Moolenaar2b00b9b2019-04-17 17:08:27 +0200417 if ttymouse_val ==# 'xterm2' && col > 223
418 " When 'ttymouse' is 'xterm2', row/col bigger than 223 are not supported.
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200419 continue
420 endif
Bram Moolenaar49452192019-04-17 16:27:02 +0200421 let msg = 'ttymouse=' .. ttymouse_val
Bram Moolenaar1ee36d62019-05-01 23:13:56 +0200422 exe 'set ttymouse=' .. ttymouse_val
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200423 e Xtab1
424 tabnew Xtab2
425 tabnew Xtab3
426 tabn 2
427
428 let a = split(execute(':tabs'), "\n")
429 call assert_equal(['Tab page 1',
430 \ ' Xtab1',
431 \ 'Tab page 2',
432 \ '> Xtab2',
433 \ 'Tab page 3',
Bram Moolenaar49452192019-04-17 16:27:02 +0200434 \ ' Xtab3'], a, msg)
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200435
436 " Click on "X" in tabline to close current tab i.e. Xtab2.
437 redraw
438 call MouseLeftClick(row, col)
439 call MouseLeftRelease(row, col)
440 let a = split(execute(':tabs'), "\n")
441 call assert_equal(['Tab page 1',
442 \ ' Xtab1',
443 \ 'Tab page 2',
Bram Moolenaar49452192019-04-17 16:27:02 +0200444 \ '> Xtab3'], a, msg)
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200445
446 %bwipe!
447 endfor
448
449 let &mouse = save_mouse
450 let &term = save_term
451 let &ttymouse = save_ttymouse
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200452 call test_override('no_query_mouse', 0)
Bram Moolenaar39f76c62019-04-13 22:13:23 +0200453endfunc
Bram Moolenaare3e38282019-04-15 20:55:31 +0200454
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200455func Test_term_mouse_drag_to_move_tab()
Bram Moolenaare3e38282019-04-15 20:55:31 +0200456 let save_mouse = &mouse
457 let save_term = &term
458 let save_ttymouse = &ttymouse
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200459 call test_override('no_query_mouse', 1)
Bram Moolenaare3e38282019-04-15 20:55:31 +0200460 " Set 'mousetime' to 1 to avoid recognizing a double-click in the loop
461 set mouse=a term=xterm mousetime=1
462 let row = 1
463
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200464 for ttymouse_val in s:ttymouse_values + s:ttymouse_dec
Bram Moolenaar49452192019-04-17 16:27:02 +0200465 let msg = 'ttymouse=' .. ttymouse_val
Bram Moolenaar1ee36d62019-05-01 23:13:56 +0200466 exe 'set ttymouse=' .. ttymouse_val
Bram Moolenaare3e38282019-04-15 20:55:31 +0200467 e Xtab1
468 tabnew Xtab2
469
470 let a = split(execute(':tabs'), "\n")
471 call assert_equal(['Tab page 1',
472 \ ' Xtab1',
473 \ 'Tab page 2',
Bram Moolenaar49452192019-04-17 16:27:02 +0200474 \ '> Xtab2'], a, msg)
Bram Moolenaare3e38282019-04-15 20:55:31 +0200475 redraw
476
477 " Click in tab2 and drag it to tab1.
478 " Check getcharmod() to verify that click is not
479 " interpreted as a spurious double-click.
480 call MouseLeftClick(row, 10)
Bram Moolenaar49452192019-04-17 16:27:02 +0200481 call assert_equal(0, getcharmod(), msg)
Bram Moolenaare3e38282019-04-15 20:55:31 +0200482 for col in [9, 8, 7, 6]
483 call MouseLeftDrag(row, col)
484 endfor
485 call MouseLeftRelease(row, col)
486 let a = split(execute(':tabs'), "\n")
487 call assert_equal(['Tab page 1',
488 \ '> Xtab2',
489 \ 'Tab page 2',
Bram Moolenaar49452192019-04-17 16:27:02 +0200490 \ ' Xtab1'], a, msg)
Bram Moolenaare3e38282019-04-15 20:55:31 +0200491
Bram Moolenaar7f279762019-04-15 21:48:22 +0200492 " brief sleep to avoid causing a double-click
493 sleep 20m
Bram Moolenaare3e38282019-04-15 20:55:31 +0200494 %bwipe!
495 endfor
496
497 let &mouse = save_mouse
498 let &term = save_term
499 let &ttymouse = save_ttymouse
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200500 call test_override('no_query_mouse', 0)
Bram Moolenaare3e38282019-04-15 20:55:31 +0200501 set mousetime&
502endfunc
503
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200504func Test_term_mouse_double_click_to_create_tab()
Bram Moolenaare3e38282019-04-15 20:55:31 +0200505 let save_mouse = &mouse
506 let save_term = &term
507 let save_ttymouse = &ttymouse
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200508 call test_override('no_query_mouse', 1)
Bram Moolenaare3e38282019-04-15 20:55:31 +0200509 " Set 'mousetime' to a small value, so that double-click works but we don't
510 " have to wait long to avoid a triple-click.
511 set mouse=a term=xterm mousetime=100
512 let row = 1
513 let col = 10
514
Bram Moolenaard0621d82019-05-02 21:12:19 +0200515 let round = 0
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200516 for ttymouse_val in s:ttymouse_values + s:ttymouse_dec
Bram Moolenaar49452192019-04-17 16:27:02 +0200517 let msg = 'ttymouse=' .. ttymouse_val
Bram Moolenaar1ee36d62019-05-01 23:13:56 +0200518 exe 'set ttymouse=' .. ttymouse_val
Bram Moolenaare3e38282019-04-15 20:55:31 +0200519 e Xtab1
520 tabnew Xtab2
521
Bram Moolenaard0621d82019-05-02 21:12:19 +0200522 if round > 0
523 " We need to sleep, or else the first MouseLeftClick() will be
524 " interpreted as a spurious triple-click.
525 sleep 100m
526 endif
527 let round += 1
528
Bram Moolenaare3e38282019-04-15 20:55:31 +0200529 let a = split(execute(':tabs'), "\n")
530 call assert_equal(['Tab page 1',
531 \ ' Xtab1',
532 \ 'Tab page 2',
Bram Moolenaar49452192019-04-17 16:27:02 +0200533 \ '> Xtab2'], a, msg)
Bram Moolenaare3e38282019-04-15 20:55:31 +0200534
535 redraw
536 call MouseLeftClick(row, col)
537 " Check getcharmod() to verify that first click is not
538 " interpreted as a spurious double-click.
Bram Moolenaar49452192019-04-17 16:27:02 +0200539 call assert_equal(0, getcharmod(), msg)
Bram Moolenaare3e38282019-04-15 20:55:31 +0200540 call MouseLeftRelease(row, col)
541 call MouseLeftClick(row, col)
Bram Moolenaar49452192019-04-17 16:27:02 +0200542 call assert_equal(32, getcharmod(), msg) " double-click
Bram Moolenaare3e38282019-04-15 20:55:31 +0200543 call MouseLeftRelease(row, col)
544 let a = split(execute(':tabs'), "\n")
545 call assert_equal(['Tab page 1',
546 \ ' Xtab1',
547 \ 'Tab page 2',
548 \ '> [No Name]',
549 \ 'Tab page 3',
Bram Moolenaar49452192019-04-17 16:27:02 +0200550 \ ' Xtab2'], a, msg)
Bram Moolenaare3e38282019-04-15 20:55:31 +0200551
Bram Moolenaare3e38282019-04-15 20:55:31 +0200552 %bwipe!
553 endfor
554
555 let &mouse = save_mouse
556 let &term = save_term
557 let &ttymouse = save_ttymouse
Bram Moolenaar92fd5992019-05-02 23:00:22 +0200558 call test_override('no_query_mouse', 0)
Bram Moolenaare3e38282019-04-15 20:55:31 +0200559 set mousetime&
560endfunc
Bram Moolenaar696d6372019-04-17 16:33:46 +0200561
562func Test_xterm_mouse_click_in_fold_columns()
563 new
564 let save_mouse = &mouse
565 let save_term = &term
566 let save_ttymouse = &ttymouse
567 let save_foldcolumn = &foldcolumn
Bram Moolenaar2b00b9b2019-04-17 17:08:27 +0200568 set mouse=a term=xterm foldcolumn=3 ttymouse=xterm2
Bram Moolenaar696d6372019-04-17 16:33:46 +0200569
570 " Create 2 nested folds.
571 call setline(1, range(1, 7))
572 2,6fold
573 norm! zR
574 4,5fold
575 call assert_equal([-1, -1, -1, 4, 4, -1, -1],
576 \ map(range(1, 7), 'foldclosed(v:val)'))
577
578 " Click in "+" of inner fold in foldcolumn should open it.
579 redraw
580 let row = 4
581 let col = 2
582 call MouseLeftClick(row, col)
583 call MouseLeftRelease(row, col)
584 call assert_equal([-1, -1, -1, -1, -1, -1, -1],
585 \ map(range(1, 7), 'foldclosed(v:val)'))
586
587 " Click in "-" of outer fold in foldcolumn should close it.
588 redraw
589 let row = 2
590 let col = 1
591 call MouseLeftClick(row, col)
592 call MouseLeftRelease(row, col)
593 call assert_equal([-1, 2, 2, 2, 2, 2, -1],
594 \ map(range(1, 7), 'foldclosed(v:val)'))
595 norm! zR
596
597 " Click in "|" of inner fold in foldcolumn should close it.
598 redraw
599 let row = 5
600 let col = 2
601 call MouseLeftClick(row, col)
602 call MouseLeftRelease(row, col)
603 call assert_equal([-1, -1, -1, 4, 4, -1, -1],
604 \ map(range(1, 7), 'foldclosed(v:val)'))
605
606 let &foldcolumn = save_foldcolumn
607 let &ttymouse = save_ttymouse
608 let &term = save_term
609 let &mouse = save_mouse
610 bwipe!
611endfunc