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 | |
| 23 | " Helper function to emit a terminal escape code. |
| 24 | func TerminalEscapeCode(code, row, col, m) |
| 25 | if &ttymouse ==# 'xterm2' |
| 26 | " need to use byte encoding here. |
| 27 | let str = list2str([a:code + 0x20, a:col + 0x20, a:row + 0x20]) |
| 28 | if has('iconv') |
| 29 | let bytes = str->iconv('utf-8', 'latin1') |
| 30 | else |
| 31 | " Hopefully the numbers are not too big. |
| 32 | let bytes = str |
| 33 | endif |
| 34 | return "\<Esc>[M" .. bytes |
| 35 | elseif &ttymouse ==# 'sgr' |
| 36 | return printf("\<Esc>[<%d;%d;%d%s", a:code, a:col, a:row, a:m) |
| 37 | elseif &ttymouse ==# 'urxvt' |
| 38 | return printf("\<Esc>[%d;%d;%dM", a:code + 0x20, a:col, a:row) |
| 39 | endif |
| 40 | endfunc |
| 41 | |
| 42 | func DecEscapeCode(code, down, row, col) |
| 43 | return printf("\<Esc>[%d;%d;%d;%d&w", a:code, a:down, a:row, a:col) |
| 44 | endfunc |
| 45 | |
| 46 | func NettermEscapeCode(row, col) |
| 47 | return printf("\<Esc>}%d,%d\r", a:row, a:col) |
| 48 | endfunc |
| 49 | |
| 50 | func MouseLeftClickCode(row, col) |
| 51 | if &ttymouse ==# 'dec' |
| 52 | return DecEscapeCode(2, 4, a:row, a:col) |
| 53 | elseif &ttymouse ==# 'netterm' |
| 54 | return NettermEscapeCode(a:row, a:col) |
| 55 | else |
| 56 | return TerminalEscapeCode(0, a:row, a:col, 'M') |
| 57 | endif |
| 58 | endfunc |
| 59 | |
| 60 | func MouseLeftClick(row, col) |
| 61 | call feedkeys(MouseLeftClickCode(a:row, a:col), 'Lx!') |
| 62 | endfunc |
| 63 | |
| 64 | func MouseMiddleClickCode(row, col) |
| 65 | if &ttymouse ==# 'dec' |
| 66 | return DecEscapeCode(4, 2, a:row, a:col) |
| 67 | else |
| 68 | return TerminalEscapeCode(1, a:row, a:col, 'M') |
| 69 | endif |
| 70 | endfunc |
| 71 | |
| 72 | func MouseMiddleClick(row, col) |
| 73 | call feedkeys(MouseMiddleClickCode(a:row, a:col), 'Lx!') |
| 74 | endfunc |
| 75 | |
| 76 | func MouseRightClickCode(row, col) |
| 77 | if &ttymouse ==# 'dec' |
| 78 | return DecEscapeCode(6, 1, a:row, a:col) |
| 79 | else |
| 80 | return TerminalEscapeCode(2, a:row, a:col, 'M') |
| 81 | endif |
| 82 | endfunc |
| 83 | |
| 84 | func MouseRightClick(row, col) |
| 85 | call feedkeys(MouseRightClickCode(a:row, a:col), 'Lx!') |
| 86 | endfunc |
| 87 | |
| 88 | func MouseCtrlLeftClickCode(row, col) |
| 89 | let ctrl = 0x10 |
| 90 | return TerminalEscapeCode(0 + ctrl, a:row, a:col, 'M') |
| 91 | endfunc |
| 92 | |
| 93 | func MouseCtrlLeftClick(row, col) |
| 94 | call feedkeys(MouseCtrlLeftClickCode(a:row, a:col), 'Lx!') |
| 95 | endfunc |
| 96 | |
| 97 | func MouseCtrlRightClickCode(row, col) |
| 98 | let ctrl = 0x10 |
| 99 | return TerminalEscapeCode(2 + ctrl, a:row, a:col, 'M') |
| 100 | endfunc |
| 101 | |
| 102 | func MouseCtrlRightClick(row, col) |
| 103 | call feedkeys(MouseCtrlRightClickCode(a:row, a:col), 'Lx!') |
| 104 | endfunc |
| 105 | |
Bram Moolenaar | 2764d06 | 2020-07-18 12:59:19 +0200 | [diff] [blame] | 106 | func MouseAltLeftClickCode(row, col) |
| 107 | let alt = 0x8 |
| 108 | return TerminalEscapeCode(0 + alt, a:row, a:col, 'M') |
| 109 | endfunc |
| 110 | |
| 111 | func MouseAltLeftClick(row, col) |
| 112 | call feedkeys(MouseAltLeftClickCode(a:row, a:col), 'Lx!') |
| 113 | endfunc |
| 114 | |
| 115 | func MouseAltRightClickCode(row, col) |
| 116 | let alt = 0x8 |
| 117 | return TerminalEscapeCode(2 + alt, a:row, a:col, 'M') |
| 118 | endfunc |
| 119 | |
| 120 | func MouseAltRightClick(row, col) |
| 121 | call feedkeys(MouseAltRightClickCode(a:row, a:col), 'Lx!') |
| 122 | endfunc |
| 123 | |
Bram Moolenaar | 515545e | 2020-03-22 14:08:59 +0100 | [diff] [blame] | 124 | func MouseLeftReleaseCode(row, col) |
| 125 | if &ttymouse ==# 'dec' |
| 126 | return DecEscapeCode(3, 0, a:row, a:col) |
| 127 | elseif &ttymouse ==# 'netterm' |
| 128 | return '' |
| 129 | else |
| 130 | return TerminalEscapeCode(3, a:row, a:col, 'm') |
| 131 | endif |
| 132 | endfunc |
| 133 | |
| 134 | func MouseLeftRelease(row, col) |
| 135 | call feedkeys(MouseLeftReleaseCode(a:row, a:col), 'Lx!') |
| 136 | endfunc |
| 137 | |
| 138 | func MouseMiddleReleaseCode(row, col) |
| 139 | if &ttymouse ==# 'dec' |
| 140 | return DecEscapeCode(5, 0, a:row, a:col) |
| 141 | else |
| 142 | return TerminalEscapeCode(3, a:row, a:col, 'm') |
| 143 | endif |
| 144 | endfunc |
| 145 | |
| 146 | func MouseMiddleRelease(row, col) |
| 147 | call feedkeys(MouseMiddleReleaseCode(a:row, a:col), 'Lx!') |
| 148 | endfunc |
| 149 | |
| 150 | func MouseRightReleaseCode(row, col) |
| 151 | if &ttymouse ==# 'dec' |
| 152 | return DecEscapeCode(7, 0, a:row, a:col) |
| 153 | else |
| 154 | return TerminalEscapeCode(3, a:row, a:col, 'm') |
| 155 | endif |
| 156 | endfunc |
| 157 | |
| 158 | func MouseRightRelease(row, col) |
| 159 | call feedkeys(MouseRightReleaseCode(a:row, a:col), 'Lx!') |
| 160 | endfunc |
| 161 | |
| 162 | func MouseLeftDragCode(row, col) |
| 163 | if &ttymouse ==# 'dec' |
| 164 | return DecEscapeCode(1, 4, a:row, a:col) |
| 165 | else |
| 166 | return TerminalEscapeCode(0x20, a:row, a:col, 'M') |
| 167 | endif |
| 168 | endfunc |
| 169 | |
| 170 | func MouseLeftDrag(row, col) |
| 171 | call feedkeys(MouseLeftDragCode(a:row, a:col), 'Lx!') |
| 172 | endfunc |
| 173 | |
| 174 | func MouseWheelUpCode(row, col) |
| 175 | return TerminalEscapeCode(0x40, a:row, a:col, 'M') |
| 176 | endfunc |
| 177 | |
| 178 | func MouseWheelUp(row, col) |
| 179 | call feedkeys(MouseWheelUpCode(a:row, a:col), 'Lx!') |
| 180 | endfunc |
| 181 | |
| 182 | func MouseWheelDownCode(row, col) |
| 183 | return TerminalEscapeCode(0x41, a:row, a:col, 'M') |
| 184 | endfunc |
| 185 | |
| 186 | func MouseWheelDown(row, col) |
| 187 | call feedkeys(MouseWheelDownCode(a:row, a:col), 'Lx!') |
| 188 | endfunc |
| 189 | |
Bram Moolenaar | d58d4f9 | 2020-07-01 15:49:29 +0200 | [diff] [blame] | 190 | func MouseWheelLeftCode(row, col) |
| 191 | return TerminalEscapeCode(0x42, a:row, a:col, 'M') |
| 192 | endfunc |
| 193 | |
| 194 | func MouseWheelLeft(row, col) |
| 195 | call feedkeys(MouseWheelLeftCode(a:row, a:col), 'Lx!') |
| 196 | endfunc |
| 197 | |
| 198 | func MouseWheelRightCode(row, col) |
| 199 | return TerminalEscapeCode(0x43, a:row, a:col, 'M') |
| 200 | endfunc |
| 201 | |
| 202 | func MouseWheelRight(row, col) |
| 203 | call feedkeys(MouseWheelRightCode(a:row, a:col), 'Lx!') |
| 204 | endfunc |
| 205 | |
Bram Moolenaar | 515545e | 2020-03-22 14:08:59 +0100 | [diff] [blame] | 206 | " vim: shiftwidth=2 sts=2 expandtab |