blob: e2979b771c5f4429e7be743664873bf2310195f0 [file] [log] [blame]
Bram Moolenaar515545e2020-03-22 14:08:59 +01001" Helper functions for generating mouse events
2
3" xterm2 and sgr always work, urxvt is optional.
4let g:Ttymouse_values = ['xterm2', 'sgr']
5if has('mouse_urxvt')
6 call add(g:Ttymouse_values, 'urxvt')
7endif
8
9" dec doesn't support all the functionality
10if has('mouse_dec')
11 let g:Ttymouse_dec = ['dec']
12else
13 let g:Ttymouse_dec = []
14endif
15
16" netterm only supports left click
17if has('mouse_netterm')
18 let g:Ttymouse_netterm = ['netterm']
19else
20 let g:Ttymouse_netterm = []
21endif
22
Christopher Plewright20b795e2022-12-20 20:01:58 +000023" Vim Mouse Codes.
24" Used by the GUI and by MS-Windows Consoles.
25" Keep these in sync with vim.h
26let s:MOUSE_CODE = {
27 \ 'BTN_LEFT' : 0x00,
28 \ 'BTN_MIDDLE' : 0x01,
29 \ 'BTN_RIGHT' : 0x02,
30 \ 'BTN_RELEASE' : 0x03,
31 \ 'BTN_X1' : 0x300,
32 \ 'BTN_X2' : 0x400,
33 \ 'SCRL_DOWN' : 0x100,
34 \ 'SCRL_UP' : 0x200,
35 \ 'SCRL_LEFT' : 0x500,
36 \ 'SCRL_RIGHT' : 0x600,
37 \ 'MOVE' : 0x700,
38 \ 'MOD_SHIFT' : 0x04,
39 \ 'MOD_ALT' : 0x08,
40 \ 'MOD_CTRL' : 0x10,
41 \ }
42
43
Bram Moolenaar515545e2020-03-22 14:08:59 +010044" Helper function to emit a terminal escape code.
45func TerminalEscapeCode(code, row, col, m)
46 if &ttymouse ==# 'xterm2'
47 " need to use byte encoding here.
48 let str = list2str([a:code + 0x20, a:col + 0x20, a:row + 0x20])
49 if has('iconv')
50 let bytes = str->iconv('utf-8', 'latin1')
51 else
52 " Hopefully the numbers are not too big.
53 let bytes = str
54 endif
55 return "\<Esc>[M" .. bytes
56 elseif &ttymouse ==# 'sgr'
57 return printf("\<Esc>[<%d;%d;%d%s", a:code, a:col, a:row, a:m)
58 elseif &ttymouse ==# 'urxvt'
59 return printf("\<Esc>[%d;%d;%dM", a:code + 0x20, a:col, a:row)
60 endif
61endfunc
62
63func DecEscapeCode(code, down, row, col)
64 return printf("\<Esc>[%d;%d;%d;%d&w", a:code, a:down, a:row, a:col)
65endfunc
66
67func NettermEscapeCode(row, col)
68 return printf("\<Esc>}%d,%d\r", a:row, a:col)
69endfunc
70
Christopher Plewright20b795e2022-12-20 20:01:58 +000071" Send low level mouse event to MS-Windows consoles or GUI
72func MSWinMouseEvent(button, row, col, move, multiclick, modifiers)
73 let args = { }
74 let args.button = a:button
75 " Scroll directions are inverted in the GUI, no idea why.
76 if has('gui_running')
77 if a:button == s:MOUSE_CODE.SCRL_UP
78 let args.button = s:MOUSE_CODE.SCRL_DOWN
79 elseif a:button == s:MOUSE_CODE.SCRL_DOWN
80 let args.button = s:MOUSE_CODE.SCRL_UP
81 elseif a:button == s:MOUSE_CODE.SCRL_LEFT
82 let args.button = s:MOUSE_CODE.SCRL_RIGHT
83 elseif a:button == s:MOUSE_CODE.SCRL_RIGHT
84 let args.button = s:MOUSE_CODE.SCRL_LEFT
85 endif
86 endif
87 let args.row = a:row
88 let args.col = a:col
89 let args.move = a:move
90 let args.multiclick = a:multiclick
91 let args.modifiers = a:modifiers
92 call test_mswin_event("mouse", args)
93 unlet args
94endfunc
95
Bram Moolenaar515545e2020-03-22 14:08:59 +010096func MouseLeftClickCode(row, col)
97 if &ttymouse ==# 'dec'
98 return DecEscapeCode(2, 4, a:row, a:col)
99 elseif &ttymouse ==# 'netterm'
100 return NettermEscapeCode(a:row, a:col)
101 else
102 return TerminalEscapeCode(0, a:row, a:col, 'M')
103 endif
104endfunc
105
106func MouseLeftClick(row, col)
Christopher Plewright20b795e2022-12-20 20:01:58 +0000107 if has('win32')
108 call MSWinMouseEvent(s:MOUSE_CODE.BTN_LEFT, a:row, a:col, 0, 0, 0)
109 else
110 call feedkeys(MouseLeftClickCode(a:row, a:col), 'Lx!')
111 endif
Bram Moolenaar515545e2020-03-22 14:08:59 +0100112endfunc
113
114func MouseMiddleClickCode(row, col)
115 if &ttymouse ==# 'dec'
116 return DecEscapeCode(4, 2, a:row, a:col)
117 else
118 return TerminalEscapeCode(1, a:row, a:col, 'M')
119 endif
120endfunc
121
122func MouseMiddleClick(row, col)
Christopher Plewright20b795e2022-12-20 20:01:58 +0000123 if has('win32')
124 call MSWinMouseEvent(s:MOUSE_CODE.BTN_MIDDLE, a:row, a:col, 0, 0, 0)
125 else
126 call feedkeys(MouseMiddleClickCode(a:row, a:col), 'Lx!')
127 endif
Bram Moolenaar515545e2020-03-22 14:08:59 +0100128endfunc
129
130func MouseRightClickCode(row, col)
131 if &ttymouse ==# 'dec'
132 return DecEscapeCode(6, 1, a:row, a:col)
133 else
134 return TerminalEscapeCode(2, a:row, a:col, 'M')
135 endif
136endfunc
137
138func MouseRightClick(row, col)
Christopher Plewright20b795e2022-12-20 20:01:58 +0000139 if has('win32')
140 call MSWinMouseEvent(s:MOUSE_CODE.BTN_RIGHT, a:row, a:col, 0, 0, 0)
141 else
142 call feedkeys(MouseRightClickCode(a:row, a:col), 'Lx!')
143 endif
Bram Moolenaar515545e2020-03-22 14:08:59 +0100144endfunc
145
146func MouseCtrlLeftClickCode(row, col)
147 let ctrl = 0x10
148 return TerminalEscapeCode(0 + ctrl, a:row, a:col, 'M')
149endfunc
150
151func MouseCtrlLeftClick(row, col)
Christopher Plewright20b795e2022-12-20 20:01:58 +0000152 if has('win32')
153 call MSWinMouseEvent(s:MOUSE_CODE.BTN_LEFT, a:row, a:col, 0, 0,
154 \ s:MOUSE_CODE.MOD_CTRL)
155 else
156 call feedkeys(MouseCtrlLeftClickCode(a:row, a:col), 'Lx!')
157 endif
Bram Moolenaar515545e2020-03-22 14:08:59 +0100158endfunc
159
160func MouseCtrlRightClickCode(row, col)
161 let ctrl = 0x10
162 return TerminalEscapeCode(2 + ctrl, a:row, a:col, 'M')
163endfunc
164
165func MouseCtrlRightClick(row, col)
Christopher Plewright20b795e2022-12-20 20:01:58 +0000166 if has('win32')
167 call MSWinMouseEvent(s:MOUSE_CODE.BTN_RIGHT, a:row, a:col, 0, 0,
168 \ s:MOUSE_CODE.MOD_CTRL)
169 else
170 call feedkeys(MouseCtrlRightClickCode(a:row, a:col), 'Lx!')
171 endif
Bram Moolenaar515545e2020-03-22 14:08:59 +0100172endfunc
173
Bram Moolenaar2764d062020-07-18 12:59:19 +0200174func MouseAltLeftClickCode(row, col)
175 let alt = 0x8
176 return TerminalEscapeCode(0 + alt, a:row, a:col, 'M')
177endfunc
178
179func MouseAltLeftClick(row, col)
Christopher Plewright20b795e2022-12-20 20:01:58 +0000180 if has('win32')
181 call MSWinMouseEvent(s:MOUSE_CODE.BTN_LEFT, a:row, a:col, 0, 0,
182 \ s:MOUSE_CODE.MOD_ALT)
183 else
184 call feedkeys(MouseAltLeftClickCode(a:row, a:col), 'Lx!')
185 endif
Bram Moolenaar2764d062020-07-18 12:59:19 +0200186endfunc
187
188func MouseAltRightClickCode(row, col)
189 let alt = 0x8
190 return TerminalEscapeCode(2 + alt, a:row, a:col, 'M')
191endfunc
192
193func MouseAltRightClick(row, col)
Christopher Plewright20b795e2022-12-20 20:01:58 +0000194 if has('win32')
195 call MSWinMouseEvent(s:MOUSE_CODE.BTN_RIGHT, a:row, a:col, 0, 0,
196 \ s:MOUSE_CODE.MOD_ALT)
197 else
198 call feedkeys(MouseAltRightClickCode(a:row, a:col), 'Lx!')
199 endif
Bram Moolenaar2764d062020-07-18 12:59:19 +0200200endfunc
201
Bram Moolenaar515545e2020-03-22 14:08:59 +0100202func MouseLeftReleaseCode(row, col)
203 if &ttymouse ==# 'dec'
204 return DecEscapeCode(3, 0, a:row, a:col)
205 elseif &ttymouse ==# 'netterm'
206 return ''
207 else
208 return TerminalEscapeCode(3, a:row, a:col, 'm')
209 endif
210endfunc
211
212func MouseLeftRelease(row, col)
Christopher Plewright20b795e2022-12-20 20:01:58 +0000213 if has('win32')
214 call MSWinMouseEvent(s:MOUSE_CODE.BTN_RELEASE, a:row, a:col, 0, 0, 0)
215 else
216 call feedkeys(MouseLeftReleaseCode(a:row, a:col), 'Lx!')
217 endif
Bram Moolenaar515545e2020-03-22 14:08:59 +0100218endfunc
219
220func MouseMiddleReleaseCode(row, col)
221 if &ttymouse ==# 'dec'
222 return DecEscapeCode(5, 0, a:row, a:col)
223 else
224 return TerminalEscapeCode(3, a:row, a:col, 'm')
225 endif
226endfunc
227
228func MouseMiddleRelease(row, col)
Christopher Plewright20b795e2022-12-20 20:01:58 +0000229 if has('win32')
230 call MSWinMouseEvent(s:MOUSE_CODE.BTN_RELEASE, a:row, a:col, 0, 0, 0)
231 else
232 call feedkeys(MouseMiddleReleaseCode(a:row, a:col), 'Lx!')
233 endif
Bram Moolenaar515545e2020-03-22 14:08:59 +0100234endfunc
235
236func MouseRightReleaseCode(row, col)
237 if &ttymouse ==# 'dec'
238 return DecEscapeCode(7, 0, a:row, a:col)
239 else
240 return TerminalEscapeCode(3, a:row, a:col, 'm')
241 endif
242endfunc
243
244func MouseRightRelease(row, col)
Christopher Plewright20b795e2022-12-20 20:01:58 +0000245 if has('win32')
246 call MSWinMouseEvent(s:MOUSE_CODE.BTN_RELEASE, a:row, a:col, 0, 0, 0)
247 else
248 call feedkeys(MouseRightReleaseCode(a:row, a:col), 'Lx!')
249 endif
Bram Moolenaar515545e2020-03-22 14:08:59 +0100250endfunc
251
252func MouseLeftDragCode(row, col)
253 if &ttymouse ==# 'dec'
254 return DecEscapeCode(1, 4, a:row, a:col)
255 else
256 return TerminalEscapeCode(0x20, a:row, a:col, 'M')
257 endif
258endfunc
259
260func MouseLeftDrag(row, col)
Christopher Plewright20b795e2022-12-20 20:01:58 +0000261 if has('win32')
262 call MSWinMouseEvent(s:MOUSE_CODE.BTN_LEFT, a:row, a:col, 1, 0, 0)
263 else
264 call feedkeys(MouseLeftDragCode(a:row, a:col), 'Lx!')
265 endif
Bram Moolenaar515545e2020-03-22 14:08:59 +0100266endfunc
267
268func MouseWheelUpCode(row, col)
269 return TerminalEscapeCode(0x40, a:row, a:col, 'M')
270endfunc
271
272func MouseWheelUp(row, col)
Christopher Plewright20b795e2022-12-20 20:01:58 +0000273 if has('win32')
274 call MSWinMouseEvent(s:MOUSE_CODE.SCRL_UP, a:row, a:col, 0, 0, 0)
275 else
276 call feedkeys(MouseWheelUpCode(a:row, a:col), 'Lx!')
277 endif
Bram Moolenaar515545e2020-03-22 14:08:59 +0100278endfunc
279
280func MouseWheelDownCode(row, col)
281 return TerminalEscapeCode(0x41, a:row, a:col, 'M')
282endfunc
283
284func MouseWheelDown(row, col)
Christopher Plewright20b795e2022-12-20 20:01:58 +0000285 if has('win32')
286 call MSWinMouseEvent(s:MOUSE_CODE.SCRL_DOWN, a:row, a:col, 0, 0, 0)
287 else
288 call feedkeys(MouseWheelDownCode(a:row, a:col), 'Lx!')
289 endif
Bram Moolenaar515545e2020-03-22 14:08:59 +0100290endfunc
291
Bram Moolenaard58d4f92020-07-01 15:49:29 +0200292func MouseWheelLeftCode(row, col)
293 return TerminalEscapeCode(0x42, a:row, a:col, 'M')
294endfunc
295
296func MouseWheelLeft(row, col)
Christopher Plewright20b795e2022-12-20 20:01:58 +0000297 if has('win32')
298 call MSWinMouseEvent(s:MOUSE_CODE.SCRL_LEFT, a:row, a:col, 0, 0, 0)
299 else
300 call feedkeys(MouseWheelLeftCode(a:row, a:col), 'Lx!')
301 endif
Bram Moolenaard58d4f92020-07-01 15:49:29 +0200302endfunc
303
304func MouseWheelRightCode(row, col)
305 return TerminalEscapeCode(0x43, a:row, a:col, 'M')
306endfunc
307
308func MouseWheelRight(row, col)
Christopher Plewright20b795e2022-12-20 20:01:58 +0000309 if has('win32')
310 call MSWinMouseEvent(s:MOUSE_CODE.SCRL_RIGHT, a:row, a:col, 0, 0, 0)
311 else
312 call feedkeys(MouseWheelRightCode(a:row, a:col), 'Lx!')
313 endif
314endfunc
315
316func MouseShiftWheelUpCode(row, col)
317 " todo feed shift mod.
318 return TerminalEscapeCode(0x40, a:row, a:col, 'M')
319endfunc
320
321func MouseShiftWheelUp(row, col)
322 if has('win32')
323 call MSWinMouseEvent(s:MOUSE_CODE.SCRL_UP, a:row, a:col, 0, 0,
324 \ s:MOUSE_CODE.MOD_SHIFT)
325 else
326 call feedkeys(MouseShiftWheelUpCode(a:row, a:col), 'Lx!')
327 endif
328endfunc
329
330func MouseShiftWheelDownCode(row, col)
331 " todo feed shift mod.
332 return TerminalEscapeCode(0x41, a:row, a:col, 'M')
333endfunc
334
335func MouseShiftWheelDown(row, col)
336 if has('win32')
337 call MSWinMouseEvent(s:MOUSE_CODE.SCRL_DOWN, a:row, a:col, 0, 0,
338 \ s:MOUSE_CODE.MOD_SHIFT)
339 else
340 call feedkeys(MouseShiftWheelDownCode(a:row, a:col), 'Lx!')
341 endif
342endfunc
343
344func MouseShiftWheelLeftCode(row, col)
345 " todo feed shift mod.
346 return TerminalEscapeCode(0x42, a:row, a:col, 'M')
347endfunc
348
349func MouseShiftWheelLeft(row, col)
350 if has('win32')
351 call MSWinMouseEvent(s:MOUSE_CODE.SCRL_LEFT, a:row, a:col, 0, 0,
352 \ s:MOUSE_CODE.MOD_SHIFT)
353 else
354 call feedkeys(MouseShiftWheelLeftCode(a:row, a:col), 'Lx!')
355 endif
356endfunc
357
358func MouseShiftWheelRightCode(row, col)
359 " todo feed shift mod.
360 return TerminalEscapeCode(0x43, a:row, a:col, 'M')
361endfunc
362
363func MouseShiftWheelRight(row, col)
364 if has('win32')
365 call MSWinMouseEvent(s:MOUSE_CODE.SCRL_RIGHT, a:row, a:col, 0, 0,
366 \ s:MOUSE_CODE.MOD_SHIFT)
367 else
368 call feedkeys(MouseShiftWheelRightCode(a:row, a:col), 'Lx!')
369 endif
Bram Moolenaard58d4f92020-07-01 15:49:29 +0200370endfunc
371
Bram Moolenaar515545e2020-03-22 14:08:59 +0100372" vim: shiftwidth=2 sts=2 expandtab