-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathinput.fs
More file actions
331 lines (315 loc) · 21.6 KB
/
input.fs
File metadata and controls
331 lines (315 loc) · 21.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
module FVim.input
open ui
open log
open neovim
open Avalonia.Input
open System
open FSharp.Control.Reactive
let inline trace fmt = trace "input" fmt
#nowarn "0058"
// notation meaning equivalent decimal value(s) ~
// -----------------------------------------------------------------------------------------------------------------------------------------------------------------------
// <Nul> zero CTRL-@ 0 (stored as 10) *<Nul>*
// <BS> backspace CTRL-H 8 *backspace*
// <Tab> tab CTRL-I 9 *tab* *Tab*
// <NL> linefeed CTRL-J 10 (used for <Nul>) *linefeed*
// <FF> formfeed CTRL-L 12 *formfeed*
// <CR> carriage return CTRL-M 13 *carriage-return*
// <Return> same as <CR> *<Return>*
// <Enter> same as <CR> *<Enter>*
// <Esc> escape CTRL-[ 27 *escape* *<Esc>*
// <Space> space 32 *space*
// <lt> less-than < 60 *<lt>*
// <Bslash> backslash \ 92 *backslash* *<Bslash>*
// <Bar> vertical bar | 124 *<Bar>*
// <Del> delete 127
// <CSI> command sequence intro ALT-Esc 155 *<CSI>*
// <xCSI> CSI when typed in the GUI *<xCSI>*
// <EOL> end-of-line (can be <CR>, <LF> or <CR><LF>, depends on system and 'fileformat') *<EOL>*
// <Up> cursor-up *cursor-up* *cursor_up*
// <Down> cursor-down *cursor-down* *cursor_down*
// <Left> cursor-left *cursor-left* *cursor_left*
// <Right> cursor-right *cursor-right* *cursor_right*
// <S-Up> shift-cursor-up
// <S-Down> shift-cursor-down
// <S-Left> shift-cursor-left
// <S-Right> shift-cursor-right
// <C-Left> control-cursor-left
// <C-Right> control-cursor-right
// <F1> - <F12> function keys 1 to 12 *function_key* *function-key*
// <S-F1> - <S-F12> shift-function keys 1 to 12 *<S-F1>*
// <Help> help key
// <Undo> undo key
// <Insert> insert key
// <Home> home *home*
// <End> end *end*
// <PageUp> page-up *page_up* *page-up*
// <PageDown> page-down *page_down* *page-down*
// <kHome> keypad home (upper left) *keypad-home*
// <kEnd> keypad end (lower left) *keypad-end*
// <kPageUp> keypad page-up (upper right) *keypad-page-up*
// <kPageDown> keypad page-down (lower right) *keypad-page-down*
// <kPlus> keypad + *keypad-plus*
// <kMinus> keypad - *keypad-minus*
// <kMultiply> keypad * *keypad-multiply*
// <kDivide> keypad / *keypad-divide*
// <kEnter> keypad Enter *keypad-enter*
// <kPoint> keypad Decimal point *keypad-point*
// <k0> - <k9> keypad 0 to 9 *keypad-0* *keypad-9*
// <S-...> shift-key *shift* *<S-*
// <C-...> control-key *control* *ctrl* *<C-*
// <M-...> alt-key or meta-key *META* *ALT* *<M-*
// <A-...> same as <M-...> *<A-*
// <D-...> command-key or "super" key *<D-*
let (|HasFlag|_|) (flag: KeyModifiers) (x: KeyModifiers) =
if x.HasFlag flag then Some() else None
let (|NoFlag|_|) (flag: KeyModifiers) (x: KeyModifiers) =
if x.HasFlag flag then None else Some()
let (|NvimSupportedMouseButton|_|) (mb: MouseButton) =
match mb with
| MouseButton.Left | MouseButton.Right | MouseButton.Middle -> Some mb
| _ -> None
let MB (x: MouseButton) =
match x with
| MouseButton.Left -> "left"
| MouseButton.Right -> "right"
| MouseButton.Middle -> "middle"
| _ -> "none"
let DIR (dx: float, dy: float, horizontal: bool) =
match sign dx, sign dy, horizontal with
| -1, _, true -> "right"
| _, _, true -> "left"
| _, -1, false -> "down"
| _, _, false -> "up"
let suffix (suf: string, r: int, c: int) =
sprintf "%s><%d,%d" suf r c
let mutable accumulatedX = 0.0
let mutable accumulatedY = 0.0
let (|Special|Normal|ImeEvent|TextInput|Unrecognized|) (x: InputEvent) =
match x with
// | Key(HasFlag(KeyModifiers.Control), Key.H)
// | Key(HasFlag(KeyModifiers.Control), Key.I)
// | Key(HasFlag(KeyModifiers.Control), Key.J)
// | Key(HasFlag(KeyModifiers.Control), Key.M)
// | Key(HasFlag(KeyModifiers.Control), Key.Oem4) // Oem4 is '['
// | Key(HasFlag(KeyModifiers.Control), Key.L) // if ^L is sent as <FF> then neovim discards the key.
// Avoid sending key sequence, e.g. "capslock"
| Key(_, Key.None) | Key(_, Key.Cancel)
| Key(_, Key.Clear) | Key(_, Key.Pause)
| Key(_, Key.CapsLock) | Key(_, Key.Capital) | Key(_, Key.NumLock)
| Key(_, Key.HangulMode) // | Key(_, Key.KanaMode)
| Key(_, Key.JunjaMode) | Key(_, Key.FinalMode)
| Key(_, Key.KanjiMode) // | Key(_, Key.HanjaMode)
| Key(_, Key.Select) | Key(_, Key.Print)
| Key(_, Key.Execute) | Key(_, Key.PrintScreen)
| Key(_, Key.Apps) | Key(_, Key.Sleep)
| Key(_, Key.BrowserBack) | Key(_, Key.BrowserForward)
| Key(_, Key.BrowserRefresh) | Key(_, Key.BrowserStop)
| Key(_, Key.BrowserSearch) | Key(_, Key.BrowserFavorites)
| Key(_, Key.BrowserHome)
| Key(_, Key.VolumeUp) | Key(_, Key.VolumeDown)
| Key(_, Key.VolumeMute)
| Key(_, Key.MediaNextTrack) | Key(_, Key.MediaPreviousTrack)
| Key(_, Key.MediaStop) | Key(_, Key.MediaPlayPause)
| Key(_, Key.LaunchMail) | Key(_, Key.SelectMedia)
| Key(_, Key.LaunchApplication1)| Key(_, Key.LaunchApplication2)
| Key(_, Key.Oem8)
| Key(_, Key.AbntC1) | Key(_, Key.AbntC2)
| Key(_, Key.System)
| Key(_, Key.OemAttn) //| Key(_, Key.DbeAlphanumeric)
| Key(_, Key.OemFinish) // | Key(_, Key.DbeKatakana)
| Key(_, Key.DbeHiragana) // | Key(_, Key.OemCopy)
| Key(_, Key.DbeSbcsChar) // | Key(_, Key.OemAuto)
| Key(_, Key.DbeDbcsChar) // | Key(_, Key.OemEnlw)
| Key(_, Key.OemBackTab) // | Key(_, Key.DbeRoman)
| Key(_, Key.DbeNoRoman) // | Key(_, Key.Attn)
| Key(_, Key.CrSel) // | Key(_, Key.DbeEnterWordRegisterMode)
| Key(_, Key.ExSel) // | Key(_, Key.DbeEnterImeConfigureMode)
| Key(_, Key.EraseEof) // | Key(_, Key.DbeFlushString)
| Key(_, Key.Play) // | Key(_, Key.DbeCodeInput)
| Key(_, Key.DbeNoCodeInput) | Key(_, Key.Zoom)
| Key(_, Key.NoName) //| Key(_, Key.DbeDetermineString)
| Key(_, Key.DbeEnterDialogConversionMode) // | Key(_, Key.Pa1)
| Key(_, Key.OemClear)
| Key(_, Key.DeadCharProcessed)
| Key(_, Key.FnLeftArrow) | Key(_, Key.FnRightArrow)
| Key(_, Key.FnUpArrow) | Key(_, Key.FnDownArrow)
-> Unrecognized
| Key(_, Key.Back) -> Special "BS"
| Key(_, Key.Tab) -> Special "Tab"
| Key(_, Key.LineFeed) -> Special "NL"
| Key(_, Key.Return) -> Special "CR"
| Key(_, Key.Escape) -> Special "Esc"
| Key(_, Key.Space) -> Special "Space"
| Key(HasFlag(KeyModifiers.Shift), Key.OemComma) -> Special "LT"
// note, on Windows '\' is recognized as OemPipe but on macOS it's OemBackslash
| Key(NoFlag(KeyModifiers.Shift),
(Key.OemPipe | Key.OemBackslash)) -> Special "Bslash"
| Key(HasFlag(KeyModifiers.Shift),
(Key.OemPipe | Key.OemBackslash)) -> Special "Bar"
| Key(_, Key.Delete) -> Special "Del"
| Key(HasFlag(KeyModifiers.Alt), Key.Escape) -> Special "xCSI"
| Key(_, Key.Up) -> Special "Up"
| Key(_, Key.Down) -> Special "Down"
| Key(_, Key.Left) -> Special "Left"
| Key(_, Key.Right) -> Special "Right"
| Key(_, Key.Help) -> Special "Help"
| Key(_, Key.Insert) -> Special "Insert"
| Key(_, Key.Home) -> Special "Home"
| Key(_, Key.End) -> Special "End"
| Key(_, Key.PageUp) -> Special "PageUp"
| Key(_, Key.PageDown) -> Special "PageDown"
| Key(_, x &
(Key.F1 | Key.F2 | Key.F3 | Key.F4
| Key.F5 | Key.F6 | Key.F7 | Key.F8
| Key.F9 | Key.F10 | Key.F11 | Key.F12
| Key.F13 | Key.F14 | Key.F15 | Key.F16
| Key.F17 | Key.F18 | Key.F19 | Key.F20
| Key.F21 | Key.F22 | Key.F23 | Key.F24)) -> Special(x.ToString())
| Key(NoFlag(KeyModifiers.Shift), x &
(Key.D0 | Key.D1 | Key.D2 | Key.D3
| Key.D4 | Key.D5 | Key.D6 | Key.D7
| Key.D8 | Key.D9)) -> Normal(x.ToString().TrimStart('D'))
| Key(_, x &
(Key.NumPad0 | Key.NumPad1 | Key.NumPad2 | Key.NumPad3
| Key.NumPad4 | Key.NumPad5 | Key.NumPad6 | Key.NumPad7
| Key.NumPad8 | Key.NumPad9)) -> Special("k" + string(x.ToString() |> Seq.last))
| Key(NoFlag(KeyModifiers.Shift), Key.OemComma) -> Normal ","
| Key(NoFlag(KeyModifiers.Shift), Key.OemPeriod) -> Normal "."
| Key(HasFlag(KeyModifiers.Shift), Key.OemPeriod) -> Normal ">"
| Key(NoFlag(KeyModifiers.Shift), Key.Oem2) -> Normal "/"
| Key(HasFlag(KeyModifiers.Shift), Key.Oem2) -> Normal "?"
| Key(NoFlag(KeyModifiers.Shift), Key.OemSemicolon) -> Normal ";"
| Key(HasFlag(KeyModifiers.Shift), Key.OemSemicolon) -> Normal ":"
| Key(NoFlag(KeyModifiers.Shift), Key.OemQuotes) -> Normal "'"
| Key(HasFlag(KeyModifiers.Shift), Key.OemQuotes) -> Normal "\""
| Key(NoFlag(KeyModifiers.Shift), Key.Oem4) -> Normal "["
| Key(HasFlag(KeyModifiers.Shift), Key.Oem4) -> Normal "{"
| Key(NoFlag(KeyModifiers.Shift), Key.OemCloseBrackets) -> Normal "]"
| Key(HasFlag(KeyModifiers.Shift), Key.OemCloseBrackets) -> Normal "}"
| Key(NoFlag(KeyModifiers.Shift), Key.OemMinus) -> Normal "-"
| Key(HasFlag(KeyModifiers.Shift), Key.OemMinus) -> Normal "_"
| Key(NoFlag(KeyModifiers.Shift), Key.OemPlus) -> Normal "="
| Key(HasFlag(KeyModifiers.Shift), Key.OemPlus) -> Normal "+"
| Key(NoFlag(KeyModifiers.Shift), Key.OemTilde) -> Normal "`"
| Key(HasFlag(KeyModifiers.Shift), Key.OemTilde) -> Normal "~"
| Key(HasFlag(KeyModifiers.Shift), Key.D1) -> Normal "!"
| Key(HasFlag(KeyModifiers.Shift), Key.D2) -> Normal "@"
| Key(HasFlag(KeyModifiers.Shift), Key.D3) -> Normal "#"
| Key(HasFlag(KeyModifiers.Shift), Key.D4) -> Normal "$"
| Key(HasFlag(KeyModifiers.Shift), Key.D5) -> Normal "%"
| Key(HasFlag(KeyModifiers.Shift), Key.D6) -> Normal "^"
| Key(HasFlag(KeyModifiers.Shift), Key.D7) -> Normal "&"
| Key(HasFlag(KeyModifiers.Shift), Key.D8) -> Normal "*"
| Key(HasFlag(KeyModifiers.Shift), Key.D9) -> Normal "("
| Key(HasFlag(KeyModifiers.Shift), Key.D0) -> Normal ")"
| Key(_, Key.Multiply) -> Special("kMultiply")
| Key(_, Key.Add) -> Special("kPlus")
| Key(_, Key.Subtract) -> Special("kMinus")
| Key(_, Key.Divide) -> Special("kDivide")
| Key(_, Key.Separator) -> Special("kEnter")
| Key(_, Key.Decimal) -> Special("kPoint")
| Key(_, (
Key.ImeProcessed | Key.ImeAccept | Key.ImeConvert
| Key.ImeNonConvert | Key.ImeModeChange)) -> ImeEvent
| Key(NoFlag(KeyModifiers.Shift), x) -> Normal (x.ToString().ToLowerInvariant())
| Key(_, x) -> Normal (x.ToString())
| TextInput txt -> TextInput txt
| _ -> Unrecognized
let rec ModifiersPrefix (x: InputEvent) =
match x with
// -------------- keys with special form do not carry shift modifiers
| Key(m & HasFlag(KeyModifiers.Shift), x &
(Key.OemComma | Key.OemPipe | Key.OemBackslash | Key.OemPeriod | Key.Oem2 | Key.OemSemicolon | Key.OemQuotes
| Key.Oem4 | Key.OemCloseBrackets | Key.OemMinus | Key.OemPlus | Key.OemTilde
| Key.D0 | Key.D1 | Key.D2 | Key.D3
| Key.D4 | Key.D5 | Key.D6 | Key.D7
| Key.D8 | Key.D9))
-> ModifiersPrefix <| InputEvent.Key(m &&& (~~~KeyModifiers.Shift), x)
| Key(m & HasFlag(KeyModifiers.Shift), Key.Space) when states.key_disableShiftSpace
-> ModifiersPrefix <| InputEvent.Key(m &&& (~~~KeyModifiers.Shift), Key.Space)
| Key(m, _)
| MousePress(m, _, _, _)
| MouseRelease(m, _, _, _)
| MouseDrag(m, _, _, _)
| MouseWheel(m, _, _, _, _)
->
let c = if m.HasFlag(KeyModifiers.Control) then "C-" else ""
let a = if m.HasFlag(KeyModifiers.Alt) then "A-" else ""
let d = if m.HasFlag(KeyModifiers.Meta) then "D-" else ""
let s = if m.HasFlag(KeyModifiers.Shift) then "S-" else ""
(sprintf "%s%s%s%s" c a d s).TrimEnd('-')
| TextInput _ -> ""
| _ -> ""
let mutable private _imeArmed = false
let onInput (nvim: Nvim) (input: IObservable<int*InputEvent>) =
let key,mouse = input |> Observable.partition(function | _, InputEvent.Key _ | _, InputEvent.TextInput _ -> true | _ -> false)
// translate to nvim input sequence
let key =
key
|> Observable.choose(fun (_, x) ->
// filter out pure modifiers
match x with
| InputEvent.Key(_, (Key.LeftCtrl | Key.LeftShift | Key.LeftAlt | Key.RightCtrl | Key.RightShift | Key.RightAlt | Key.LWin | Key.RWin)) -> None
| _ ->
match x with
| ImeEvent -> _imeArmed <- true
| TextInput _ -> ()
| _ -> _imeArmed <- false
// TODO anything that cancels ime input state?
let pref = ModifiersPrefix x
match x,pref with
| (Special sp), "" -> Some(sprintf "<%s>" sp)
| (Special sp), pref -> Some(sprintf "<%s-%s>" pref sp)
| (Normal n), "" -> Some n
| (Normal n), pref -> Some(sprintf "<%s-%s>" pref n)
| ImeEvent, _ -> None
| TextInput txt, _ when _imeArmed -> Some txt
| x ->
#if DEBUG
trace "rejected: %A" x
#endif
None
)
let mouse =
mouse
|> Observable.choose(fun (gridid, x) ->
let pref = ModifiersPrefix x
match x with
| MousePress(_, r, c, NvimSupportedMouseButton but) -> Some(gridid, MB but, "press", r, c, 1, pref)
| MouseRelease(_, r, c, NvimSupportedMouseButton but) -> Some(gridid, MB but, "release", r, c, 1, pref)
| MouseDrag(_, r, c, NvimSupportedMouseButton but ) -> Some(gridid, MB but, "drag", r, c, 1, pref)
| MouseWheel(_, r, c, dx, dy) ->
// filter bogus wheel events...
if abs dx >= 10.0 || abs dy >= 10.0 then None
else
// duh! don't like this
accumulatedX <- accumulatedX + dx
accumulatedY <- accumulatedY + dy
let ax, ay = abs accumulatedX, abs accumulatedY
let rpt = max ax ay
let dir = DIR(dx, dy, ax >= ay)
if rpt >= 1.0 then
if ax >= ay then
accumulatedX <- accumulatedX - truncate accumulatedX
else
accumulatedY <- accumulatedY - truncate accumulatedY
Some(gridid, "wheel", dir, r, c, int rpt, pref)
else
None
| _ -> None)
Disposables.compose [
key |> Observable.subscribe(fun x ->
#if DEBUG
trace "OnInput: key: %A" x
#endif
nvim.input x |> ignore
)
mouse |> Observable.subscribe(fun ((grid, but, act, r, c, rep, mods) as ev) ->
#if DEBUG
trace "grid #%d: OnInput: mouse: %A" grid ev
#endif
for _ in 1..rep do
nvim.input_mouse but act mods grid r c |> ignore
)
]