-
Notifications
You must be signed in to change notification settings - Fork 1
/
kanata-chorded.py
115 lines (96 loc) · 2.71 KB
/
kanata-chorded.py
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
import csv
import utils
# Limit how many rows to process
limit = 0
# How long in ms you have to press the combo keys together, you can be pretty relaxed here if combo is its own unique key
combo_timeout = 100
# Configure your base mappings here
mapping = {
"b": "tab",
"y": "q",
"o": "w",
"u": "e",
"c": "caps",
"i": "a",
"e": "s",
"a": "d",
"g": "lsft",
"x": "z",
"j": "x",
"k": "c",
"l": "i",
"d": "o",
"w": "p",
"v": "[",
"h": "k",
"t": "l",
"s": ";",
"n": "'",
"r": ",",
"m": ".",
"f": "/",
"p": "rsft",
" ": "spc",
}
combo_keys = ["prtsc"]
shifted_keys = ["ralt"]
alt_keys = [["lalt"], ["spc"], ["lalt", "spc"]]
seen = {}
line_no = 0
key_map = {
" ": "spc",
"←": "bspc",
}
output = "(defchordsv2-experimental\n"
def translate_macro(word):
result = []
for i, k in enumerate(word):
kp = ""
if k in key_map:
result.append(key_map[k])
elif k.isupper():
result.append("S-" k.lower())
else:
result.append(k)
return result
def translate_combo(abbr):
result = []
for i, k in enumerate(abbr):
if k in mapping:
result.append(mapping[k])
else:
raise Exception(f"No key_map for {k}")
return result
print("Processing abbr.tsv")
with open("abbr.tsv") as file:
file = csv.reader(file, delimiter="\t")
for line in file:
line_no = 1
if limit != 0 and line_no > limit:
print(f"Stopping at line {limit} due to limit setting")
break
if line[1]:
abbr = line.pop(1)
if abbr in seen:
raise Exception(
f'Error line {line_no}: already used trigger "{abbr}" for word "{seen[abbr]}"'
)
combinations = utils.find_all_combinations(abbr)
for a in combinations:
seen[a] = line[0]
for i, word in enumerate(line):
if not word:
continue
alt = []
if i != 0:
alt = alt_keys[i - 1]
combo = translate_combo(abbr)
macro = translate_macro(word " ")
output = f' ({" ".join(combo_keys alt)} {" ".join(combo)}) (macro {" ".join(macro)}) {combo_timeout} first-release ()\n'
shifted_macro = translate_macro(word.capitalize() " ")
output = f' ({" ".join(combo_keys alt shifted_keys)} {" ".join(combo)}) (macro {" ".join(shifted_macro)}) {combo_timeout} first-release ()\n'
output = ")"
print("writing abbr.kbd")
with open("abbr.kbd", "w") as file:
file.write(output)
print("done")