blob: d59ad0eab6bcc795a6ad5ff7ac704b0decd79968 [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
23" Helper function to emit a terminal escape code.
24func 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
40endfunc
41
42func DecEscapeCode(code, down, row, col)
43 return printf("\<Esc>[%d;%d;%d;%d&w", a:code, a:down, a:row, a:col)
44endfunc
45
46func NettermEscapeCode(row, col)
47 return printf("\<Esc>}%d,%d\r", a:row, a:col)
48endfunc
49
50func 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
58endfunc
59
60func MouseLeftClick(row, col)
61 call feedkeys(MouseLeftClickCode(a:row, a:col), 'Lx!')
62endfunc
63
64func 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
70endfunc
71
72func MouseMiddleClick(row, col)
73 call feedkeys(MouseMiddleClickCode(a:row, a:col), 'Lx!')
74endfunc
75
76func 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
82endfunc
83
84func MouseRightClick(row, col)
85 call feedkeys(MouseRightClickCode(a:row, a:col), 'Lx!')
86endfunc
87
88func MouseCtrlLeftClickCode(row, col)
89 let ctrl = 0x10
90 return TerminalEscapeCode(0 + ctrl, a:row, a:col, 'M')
91endfunc
92
93func MouseCtrlLeftClick(row, col)
94 call feedkeys(MouseCtrlLeftClickCode(a:row, a:col), 'Lx!')
95endfunc
96
97func MouseCtrlRightClickCode(row, col)
98 let ctrl = 0x10
99 return TerminalEscapeCode(2 + ctrl, a:row, a:col, 'M')
100endfunc
101
102func MouseCtrlRightClick(row, col)
103 call feedkeys(MouseCtrlRightClickCode(a:row, a:col), 'Lx!')
104endfunc
105
Bram Moolenaar2764d062020-07-18 12:59:19 +0200106func MouseAltLeftClickCode(row, col)
107 let alt = 0x8
108 return TerminalEscapeCode(0 + alt, a:row, a:col, 'M')
109endfunc
110
111func MouseAltLeftClick(row, col)
112 call feedkeys(MouseAltLeftClickCode(a:row, a:col), 'Lx!')
113endfunc
114
115func MouseAltRightClickCode(row, col)
116 let alt = 0x8
117 return TerminalEscapeCode(2 + alt, a:row, a:col, 'M')
118endfunc
119
120func MouseAltRightClick(row, col)
121 call feedkeys(MouseAltRightClickCode(a:row, a:col), 'Lx!')
122endfunc
123
Bram Moolenaar515545e2020-03-22 14:08:59 +0100124func 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
132endfunc
133
134func MouseLeftRelease(row, col)
135 call feedkeys(MouseLeftReleaseCode(a:row, a:col), 'Lx!')
136endfunc
137
138func 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
144endfunc
145
146func MouseMiddleRelease(row, col)
147 call feedkeys(MouseMiddleReleaseCode(a:row, a:col), 'Lx!')
148endfunc
149
150func 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
156endfunc
157
158func MouseRightRelease(row, col)
159 call feedkeys(MouseRightReleaseCode(a:row, a:col), 'Lx!')
160endfunc
161
162func 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
168endfunc
169
170func MouseLeftDrag(row, col)
171 call feedkeys(MouseLeftDragCode(a:row, a:col), 'Lx!')
172endfunc
173
174func MouseWheelUpCode(row, col)
175 return TerminalEscapeCode(0x40, a:row, a:col, 'M')
176endfunc
177
178func MouseWheelUp(row, col)
179 call feedkeys(MouseWheelUpCode(a:row, a:col), 'Lx!')
180endfunc
181
182func MouseWheelDownCode(row, col)
183 return TerminalEscapeCode(0x41, a:row, a:col, 'M')
184endfunc
185
186func MouseWheelDown(row, col)
187 call feedkeys(MouseWheelDownCode(a:row, a:col), 'Lx!')
188endfunc
189
Bram Moolenaard58d4f92020-07-01 15:49:29 +0200190func MouseWheelLeftCode(row, col)
191 return TerminalEscapeCode(0x42, a:row, a:col, 'M')
192endfunc
193
194func MouseWheelLeft(row, col)
195 call feedkeys(MouseWheelLeftCode(a:row, a:col), 'Lx!')
196endfunc
197
198func MouseWheelRightCode(row, col)
199 return TerminalEscapeCode(0x43, a:row, a:col, 'M')
200endfunc
201
202func MouseWheelRight(row, col)
203 call feedkeys(MouseWheelRightCode(a:row, a:col), 'Lx!')
204endfunc
205
Bram Moolenaar515545e2020-03-22 14:08:59 +0100206" vim: shiftwidth=2 sts=2 expandtab