Bram Moolenaar | 515545e | 2020-03-22 14:08:59 +0100 | [diff] [blame] | 1 | " Helper functions for generating mouse events |
| 2 | |
| 3 | " xterm2 and sgr always work, urxvt is optional. |
| 4 | let g:Ttymouse_values = ['xterm2', 'sgr'] |
| 5 | if has('mouse_urxvt') |
| 6 | call add(g:Ttymouse_values, 'urxvt') |
| 7 | endif |
| 8 | |
| 9 | " dec doesn't support all the functionality |
| 10 | if has('mouse_dec') |
| 11 | let g:Ttymouse_dec = ['dec'] |
| 12 | else |
| 13 | let g:Ttymouse_dec = [] |
| 14 | endif |
| 15 | |
| 16 | " netterm only supports left click |
| 17 | if has('mouse_netterm') |
| 18 | let g:Ttymouse_netterm = ['netterm'] |
| 19 | else |
| 20 | let g:Ttymouse_netterm = [] |
| 21 | endif |
| 22 | |
Christopher Plewright | 20b795e | 2022-12-20 20:01:58 +0000 | [diff] [blame] | 23 | " Vim Mouse Codes. |
| 24 | " Used by the GUI and by MS-Windows Consoles. |
| 25 | " Keep these in sync with vim.h |
| 26 | let 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 Moolenaar | 515545e | 2020-03-22 14:08:59 +0100 | [diff] [blame] | 44 | " Helper function to emit a terminal escape code. |
| 45 | func 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 |
| 61 | endfunc |
| 62 | |
| 63 | func DecEscapeCode(code, down, row, col) |
| 64 | return printf("\<Esc>[%d;%d;%d;%d&w", a:code, a:down, a:row, a:col) |
| 65 | endfunc |
| 66 | |
| 67 | func NettermEscapeCode(row, col) |
| 68 | return printf("\<Esc>}%d,%d\r", a:row, a:col) |
| 69 | endfunc |
| 70 | |
Christopher Plewright | 20b795e | 2022-12-20 20:01:58 +0000 | [diff] [blame] | 71 | " Send low level mouse event to MS-Windows consoles or GUI |
| 72 | func 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 |
| 94 | endfunc |
| 95 | |
Bram Moolenaar | 515545e | 2020-03-22 14:08:59 +0100 | [diff] [blame] | 96 | func 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 |
| 104 | endfunc |
| 105 | |
| 106 | func MouseLeftClick(row, col) |
Christopher Plewright | 20b795e | 2022-12-20 20:01:58 +0000 | [diff] [blame] | 107 | 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 Moolenaar | 515545e | 2020-03-22 14:08:59 +0100 | [diff] [blame] | 112 | endfunc |
| 113 | |
| 114 | func 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 |
| 120 | endfunc |
| 121 | |
| 122 | func MouseMiddleClick(row, col) |
Christopher Plewright | 20b795e | 2022-12-20 20:01:58 +0000 | [diff] [blame] | 123 | 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 Moolenaar | 515545e | 2020-03-22 14:08:59 +0100 | [diff] [blame] | 128 | endfunc |
| 129 | |
| 130 | func 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 |
| 136 | endfunc |
| 137 | |
| 138 | func MouseRightClick(row, col) |
Christopher Plewright | 20b795e | 2022-12-20 20:01:58 +0000 | [diff] [blame] | 139 | 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 Moolenaar | 515545e | 2020-03-22 14:08:59 +0100 | [diff] [blame] | 144 | endfunc |
| 145 | |
| 146 | func MouseCtrlLeftClickCode(row, col) |
| 147 | let ctrl = 0x10 |
| 148 | return TerminalEscapeCode(0 + ctrl, a:row, a:col, 'M') |
| 149 | endfunc |
| 150 | |
| 151 | func MouseCtrlLeftClick(row, col) |
Christopher Plewright | 20b795e | 2022-12-20 20:01:58 +0000 | [diff] [blame] | 152 | 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 Moolenaar | 515545e | 2020-03-22 14:08:59 +0100 | [diff] [blame] | 158 | endfunc |
| 159 | |
| 160 | func MouseCtrlRightClickCode(row, col) |
| 161 | let ctrl = 0x10 |
| 162 | return TerminalEscapeCode(2 + ctrl, a:row, a:col, 'M') |
| 163 | endfunc |
| 164 | |
| 165 | func MouseCtrlRightClick(row, col) |
Christopher Plewright | 20b795e | 2022-12-20 20:01:58 +0000 | [diff] [blame] | 166 | 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 Moolenaar | 515545e | 2020-03-22 14:08:59 +0100 | [diff] [blame] | 172 | endfunc |
| 173 | |
Bram Moolenaar | 2764d06 | 2020-07-18 12:59:19 +0200 | [diff] [blame] | 174 | func MouseAltLeftClickCode(row, col) |
| 175 | let alt = 0x8 |
| 176 | return TerminalEscapeCode(0 + alt, a:row, a:col, 'M') |
| 177 | endfunc |
| 178 | |
| 179 | func MouseAltLeftClick(row, col) |
Christopher Plewright | 20b795e | 2022-12-20 20:01:58 +0000 | [diff] [blame] | 180 | 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 Moolenaar | 2764d06 | 2020-07-18 12:59:19 +0200 | [diff] [blame] | 186 | endfunc |
| 187 | |
| 188 | func MouseAltRightClickCode(row, col) |
| 189 | let alt = 0x8 |
| 190 | return TerminalEscapeCode(2 + alt, a:row, a:col, 'M') |
| 191 | endfunc |
| 192 | |
| 193 | func MouseAltRightClick(row, col) |
Christopher Plewright | 20b795e | 2022-12-20 20:01:58 +0000 | [diff] [blame] | 194 | 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 Moolenaar | 2764d06 | 2020-07-18 12:59:19 +0200 | [diff] [blame] | 200 | endfunc |
| 201 | |
Bram Moolenaar | 515545e | 2020-03-22 14:08:59 +0100 | [diff] [blame] | 202 | func 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 |
| 210 | endfunc |
| 211 | |
| 212 | func MouseLeftRelease(row, col) |
Christopher Plewright | 20b795e | 2022-12-20 20:01:58 +0000 | [diff] [blame] | 213 | 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 Moolenaar | 515545e | 2020-03-22 14:08:59 +0100 | [diff] [blame] | 218 | endfunc |
| 219 | |
| 220 | func 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 |
| 226 | endfunc |
| 227 | |
| 228 | func MouseMiddleRelease(row, col) |
Christopher Plewright | 20b795e | 2022-12-20 20:01:58 +0000 | [diff] [blame] | 229 | 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 Moolenaar | 515545e | 2020-03-22 14:08:59 +0100 | [diff] [blame] | 234 | endfunc |
| 235 | |
| 236 | func 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 |
| 242 | endfunc |
| 243 | |
| 244 | func MouseRightRelease(row, col) |
Christopher Plewright | 20b795e | 2022-12-20 20:01:58 +0000 | [diff] [blame] | 245 | 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 Moolenaar | 515545e | 2020-03-22 14:08:59 +0100 | [diff] [blame] | 250 | endfunc |
| 251 | |
| 252 | func 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 |
| 258 | endfunc |
| 259 | |
| 260 | func MouseLeftDrag(row, col) |
Christopher Plewright | 20b795e | 2022-12-20 20:01:58 +0000 | [diff] [blame] | 261 | 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 Moolenaar | 515545e | 2020-03-22 14:08:59 +0100 | [diff] [blame] | 266 | endfunc |
| 267 | |
| 268 | func MouseWheelUpCode(row, col) |
| 269 | return TerminalEscapeCode(0x40, a:row, a:col, 'M') |
| 270 | endfunc |
| 271 | |
| 272 | func MouseWheelUp(row, col) |
Christopher Plewright | 20b795e | 2022-12-20 20:01:58 +0000 | [diff] [blame] | 273 | 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 Moolenaar | 515545e | 2020-03-22 14:08:59 +0100 | [diff] [blame] | 278 | endfunc |
| 279 | |
| 280 | func MouseWheelDownCode(row, col) |
| 281 | return TerminalEscapeCode(0x41, a:row, a:col, 'M') |
| 282 | endfunc |
| 283 | |
| 284 | func MouseWheelDown(row, col) |
Christopher Plewright | 20b795e | 2022-12-20 20:01:58 +0000 | [diff] [blame] | 285 | 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 Moolenaar | 515545e | 2020-03-22 14:08:59 +0100 | [diff] [blame] | 290 | endfunc |
| 291 | |
Bram Moolenaar | d58d4f9 | 2020-07-01 15:49:29 +0200 | [diff] [blame] | 292 | func MouseWheelLeftCode(row, col) |
| 293 | return TerminalEscapeCode(0x42, a:row, a:col, 'M') |
| 294 | endfunc |
| 295 | |
| 296 | func MouseWheelLeft(row, col) |
Christopher Plewright | 20b795e | 2022-12-20 20:01:58 +0000 | [diff] [blame] | 297 | 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 Moolenaar | d58d4f9 | 2020-07-01 15:49:29 +0200 | [diff] [blame] | 302 | endfunc |
| 303 | |
| 304 | func MouseWheelRightCode(row, col) |
| 305 | return TerminalEscapeCode(0x43, a:row, a:col, 'M') |
| 306 | endfunc |
| 307 | |
| 308 | func MouseWheelRight(row, col) |
Christopher Plewright | 20b795e | 2022-12-20 20:01:58 +0000 | [diff] [blame] | 309 | 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 |
| 314 | endfunc |
| 315 | |
| 316 | func MouseShiftWheelUpCode(row, col) |
| 317 | " todo feed shift mod. |
| 318 | return TerminalEscapeCode(0x40, a:row, a:col, 'M') |
| 319 | endfunc |
| 320 | |
| 321 | func 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 |
| 328 | endfunc |
| 329 | |
| 330 | func MouseShiftWheelDownCode(row, col) |
| 331 | " todo feed shift mod. |
| 332 | return TerminalEscapeCode(0x41, a:row, a:col, 'M') |
| 333 | endfunc |
| 334 | |
| 335 | func 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 |
| 342 | endfunc |
| 343 | |
| 344 | func MouseShiftWheelLeftCode(row, col) |
| 345 | " todo feed shift mod. |
| 346 | return TerminalEscapeCode(0x42, a:row, a:col, 'M') |
| 347 | endfunc |
| 348 | |
| 349 | func 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 |
| 356 | endfunc |
| 357 | |
| 358 | func MouseShiftWheelRightCode(row, col) |
| 359 | " todo feed shift mod. |
| 360 | return TerminalEscapeCode(0x43, a:row, a:col, 'M') |
| 361 | endfunc |
| 362 | |
| 363 | func 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 Moolenaar | d58d4f9 | 2020-07-01 15:49:29 +0200 | [diff] [blame] | 370 | endfunc |
| 371 | |
Bram Moolenaar | 515545e | 2020-03-22 14:08:59 +0100 | [diff] [blame] | 372 | " vim: shiftwidth=2 sts=2 expandtab |