-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathkeyboard.lua
270 lines (239 loc) · 6.07 KB
/
keyboard.lua
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
--[[
Module for all things keyboard
( am I aware that I am working on a text editor?
I mean, __keyboard-typing__ sort of text editor?
yeah, I am
Still, I firmly believe some things are (a little?)
closer related to keyboard than others
)
]]--
local keymap = require "core.keymap"
local keyboard = {}
-- I should probably take lite-xl one..
keyboard.modkey_map = {
["left command"] = "cmd",
["right command"] = "cmd",
["left windows"] = "cmd",
["right windows"] = "cmd",
["left ctrl"] = "ctrl",
["right ctrl"] = "ctrl",
["left shift"] = "shift",
["right shift"] = "shift",
["left alt"] = "alt",
["right option"]= "alt",
["left option"] = "alt",
["right alt"] = "alt",
}
local modkeys = { "ctrl", "alt", "shift", "cmd", "vibe_magic"}
local modkeys_sh = {
[ "ctrl" ] = "C",
[ "alt" ] = "A",
[ "shift" ] = "S",
[ "cmd" ] = "M",
[ "vibe_magic" ] = "X-", -- imaginary keystroke, only for nmaps
}
local modkeys_sh__inv = {}
for a,b in pairs(modkeys_sh) do
modkeys_sh__inv[b] = a
end
-- shift'ed keys to emulate shift (I know that's stupid but hey)
local shift_keys = {
[";"] = ":",
["§"] = "±",
["`"] = "~",
["1"] = "!",
["2"] = "@",
["3"] = "#",
["4"] = "$",
["5"] = "%",
["6"] = "^",
["7"] = "&",
["8"] = "*",
["9"] = "(",
["0"] = ")",
["-"] = "_",
["="] = "+",
["["] = "{",
["]"] = "}",
[";"] = ":",
["'"] = "\"",
["\\"] = "|",
[","] = "<",
["."] = ">",
["/"] = "?",
}
-- also add letters
keyboard.letters = {}
keyboard.LETTERS = {}
keyboard.digits = {'0','1','2','3','4','5','6','7','8','9'}
local letterstr = "qwertyuiopasdfghjklzxcvbnm"
for i=1, #letterstr do
shift_keys[letterstr:sub(i,i)] = letterstr:sub(i,i):upper()
table.insert(keyboard.letters, letterstr:sub(i,i))
table.insert(keyboard.LETTERS, letterstr:sub(i,i):upper())
end
keyboard.shift_keys = shift_keys
-- inverse, F -> f
local shift_keys_inv = {}
for a,b in pairs(shift_keys) do
shift_keys_inv[b]=a
end
keyboard.all_typed_symbols = {' '} -- I know.
for a,b in pairs(shift_keys) do
table.insert(keyboard.all_typed_symbols, a)
table.insert(keyboard.all_typed_symbols, b)
end
local escape_char_sub = {
["<"] = "\\<", -- for <ESC> and <CR>
["\\"] = "\\\\", -- for the escaping "\" itself -- it's a good thing I don't need triple escaping..
["-"] = "\\-", -- for "-" in "C/A/M-.."
["escape"] = "<ESC>",
["return"] = "<CR>",
["keypad enter"] = "<return>",
[" "] = "<space>", -- I know.
}
-- map back to symbol entered
-- should be handy? I mean.. it is already, for f/F
local un_escape_sub = {
["\\<"] = "<",
["\\\\"] = "\\",
["\\-"] = "-",
["<space>"] = " ",
["<CR>"] = "\n",
}
local escape_simple_keys = {
'home','space','up','down','left','right','end','pageup','pagedown','delete','insert','tab','backspace'
}
-- add Fs
for i = 1, 64 do
table.insert(escape_simple_keys, 'f' .. i)
end
keyboard.escape_char_sub__inv = {}
for a,b in pairs(escape_char_sub) do
keyboard.escape_char_sub__inv[b] = a
end
for _,str in ipairs(escape_simple_keys) do
escape_char_sub[str] = "<" .. str .. ">"
-- so that these take precedence
keyboard.escape_char_sub__inv["<"..str..">"] = str
end
-- I thought this would help with commandview submit in sequence
keyboard.escape_char_sub__inv["<CR>"] = "return"
function keyboard.escape_stroke(k)
local r = escape_char_sub[k]
if r then
return r
end
return k
end
function keyboard.key_to_stroke(k)
local stroke = ""
-- prep modifiers
for _, mk in ipairs({'ctrl','alt','altgr','cmd'}) do
if keymap.modkeys[mk] then
stroke = stroke .. modkeys_sh[mk] .. '-'
end
end
-- Shift is special - it may be unnecessary
if keymap.modkeys["shift"] then
if shift_keys[k] then
stroke = stroke .. keyboard.escape_stroke(shift_keys[k])
else
-- when necessary, add Shift as S-
stroke = stroke .. 'S-' .. keyboard.escape_stroke(k)
end
else
stroke = stroke .. keyboard.escape_stroke(k)
end
return stroke
end
function keyboard.key_to_stroke__orig(k)
local stroke = ""
for _, mk in ipairs(modkeys) do
if keymap.modkeys[mk] then
stroke = stroke .. mk .. "+"
end
end
return stroke .. k
end
local function stroke_strip_mod(stroke)
-- strips C/A/M/S- from stroke
-- returns:
-- - old-style stroke mod-string (ctrl+/..)
-- - stripped stroke
--
local lstroke = stroke
local R = ''
local s = ''
while #lstroke>2 and lstroke:sub(2,2)=='-' do
for sh,mod in pairs(modkeys_sh__inv) do
s = string.match(lstroke, '^' .. sh .. '%-')
if s then
R = R .. mod .. '+'
lstroke = lstroke:sub(#s+1,#lstroke)
end
end
end
return R, lstroke
end
function keyboard.stroke_to_orig_stroke(stroke)
local R, lstroke = stroke_strip_mod(stroke)
-- if it's one of the escaped characters
local s = keyboard.escape_char_sub__inv[lstroke]
if s then
return R .. s
end
-- check if shift'ed
s = shift_keys_inv[lstroke]
if s then
R = R .. "shift+" .. s
else
R = R .. lstroke
end
return R
end
function keyboard.stroke_to_symbol(stroke)
local R, lstroke = stroke_strip_mod(stroke)
local s = un_escape_sub[lstroke]
if s then
return s
else
-- if it is one symbol, return it
-- otherwise - nil
return #lstroke==1 and lstroke or nil
end
end
keyboard.stroke_patterns = {
'<[^>]+>',
'\\.',
}
for _,sh in pairs(modkeys_sh) do
table.insert(keyboard.stroke_patterns, sh .. '%-<[^>]+>')
table.insert(keyboard.stroke_patterns, sh .. '%-\\.')
table.insert(keyboard.stroke_patterns, sh .. '%-.')
end
table.insert(keyboard.stroke_patterns, '.')
function keyboard.split_stroke_seq(seq)
local R = {}
local ts = seq
while #ts>0 do
local match = ''
for _,p in ipairs(keyboard.stroke_patterns) do
match = string.match(ts,'^' .. p)
if match then
break
end
end
table.insert(R,match)
ts = ts:sub(#match+1,#ts)
end
return R
end
-- the other side of `the finer control`
function keymap.on_key_released(k)
local mk = keyboard.modkey_map[k]
if mk then
keymap.modkeys[mk] = false
end
end
return keyboard