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 | |
| 106 | func MouseLeftReleaseCode(row, col) |
| 107 | if &ttymouse ==# 'dec' |
| 108 | return DecEscapeCode(3, 0, a:row, a:col) |
| 109 | elseif &ttymouse ==# 'netterm' |
| 110 | return '' |
| 111 | else |
| 112 | return TerminalEscapeCode(3, a:row, a:col, 'm') |
| 113 | endif |
| 114 | endfunc |
| 115 | |
| 116 | func MouseLeftRelease(row, col) |
| 117 | call feedkeys(MouseLeftReleaseCode(a:row, a:col), 'Lx!') |
| 118 | endfunc |
| 119 | |
| 120 | func MouseMiddleReleaseCode(row, col) |
| 121 | if &ttymouse ==# 'dec' |
| 122 | return DecEscapeCode(5, 0, a:row, a:col) |
| 123 | else |
| 124 | return TerminalEscapeCode(3, a:row, a:col, 'm') |
| 125 | endif |
| 126 | endfunc |
| 127 | |
| 128 | func MouseMiddleRelease(row, col) |
| 129 | call feedkeys(MouseMiddleReleaseCode(a:row, a:col), 'Lx!') |
| 130 | endfunc |
| 131 | |
| 132 | func MouseRightReleaseCode(row, col) |
| 133 | if &ttymouse ==# 'dec' |
| 134 | return DecEscapeCode(7, 0, a:row, a:col) |
| 135 | else |
| 136 | return TerminalEscapeCode(3, a:row, a:col, 'm') |
| 137 | endif |
| 138 | endfunc |
| 139 | |
| 140 | func MouseRightRelease(row, col) |
| 141 | call feedkeys(MouseRightReleaseCode(a:row, a:col), 'Lx!') |
| 142 | endfunc |
| 143 | |
| 144 | func MouseLeftDragCode(row, col) |
| 145 | if &ttymouse ==# 'dec' |
| 146 | return DecEscapeCode(1, 4, a:row, a:col) |
| 147 | else |
| 148 | return TerminalEscapeCode(0x20, a:row, a:col, 'M') |
| 149 | endif |
| 150 | endfunc |
| 151 | |
| 152 | func MouseLeftDrag(row, col) |
| 153 | call feedkeys(MouseLeftDragCode(a:row, a:col), 'Lx!') |
| 154 | endfunc |
| 155 | |
| 156 | func MouseWheelUpCode(row, col) |
| 157 | return TerminalEscapeCode(0x40, a:row, a:col, 'M') |
| 158 | endfunc |
| 159 | |
| 160 | func MouseWheelUp(row, col) |
| 161 | call feedkeys(MouseWheelUpCode(a:row, a:col), 'Lx!') |
| 162 | endfunc |
| 163 | |
| 164 | func MouseWheelDownCode(row, col) |
| 165 | return TerminalEscapeCode(0x41, a:row, a:col, 'M') |
| 166 | endfunc |
| 167 | |
| 168 | func MouseWheelDown(row, col) |
| 169 | call feedkeys(MouseWheelDownCode(a:row, a:col), 'Lx!') |
| 170 | endfunc |
| 171 | |
| 172 | " vim: shiftwidth=2 sts=2 expandtab |