-
Notifications
You must be signed in to change notification settings - Fork 0
/
print-deadkeys.py
231 lines (196 loc) · 7.54 KB
/
print-deadkeys.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
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
#! python3
import sys
# hint: use set PYTHONIOENCODING=UTF-8 to control encoding of stdout
version = [1, 8, 4]
import re
import sys
import unicodedata
latin = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
# mapping of latin letters in the Symbol font
greek = "ΑΒΧΔΕΦΓΗΙϑΚΛΜΝΟΠΘΡΣΤΥςΩΞΨΖαβχδεφγηιϕκλμνοπθρστυϖωξψζ"
class DeadKey:
def __init__(self, name, keypress, basekey, deadkey, combining_accent, extras):
self.name = name
self.keypress = keypress
self.basekey = basekey
self.deadkey = deadkey
self.combining_accent = combining_accent
self.extras = extras
if self.combining_accent and not unicodedata.combining(self.combining_accent):
raise ValueError("That is not a combining accent: {}".format(self.combining_accent))
def isaccent(self):
return self.combining_accent is not None
def accent_letters(self, letters=latin):
""" all letters with combined accent
returns empty iterator if isaccent() is False """
if self.isaccent():
for l in letters:
a = l + self.combining_accent
b = unicodedata.normalize("NFC", a)
if len(b) == 1:
yield (l, b)
def all(self):
""" all combinations"""
yield from self.extras
yield from self.accent_letters()
yield (" ", self.deadkey)
# Note that not all characters work as dead keys. En-dash doesn't work.
dk = [
DeadKey("Backtick", "`", "`", "`", "\u0300", []),
DeadKey("Quote", "'", "'", "´", "\u0301", [
(",", "‚") # ← that's a low quotation mark
]),
DeadKey("Circumflex", "6", "6", "^", "\u0302", []),
DeadKey("Tilde", "Shift + `", "~", "~", "\u0303", []),
DeadKey("Dash", "-", "-", "¯", "\u0304", [
(">", "→"),
("<", "←"),
("v", "↓"),
("^", "↑"),
("-", "–"),
("_", "—"),
(":", "÷"),
("+", "±"),
]),
DeadKey("Dot", ".", ".", "˙", "\u0307", [
(".", "…"),
("-", "·"),
("=", "•"),
]),
DeadKey("Double quote", "Shift + '", '"', "¨", "\u0308", [
(",", "„")
]),
DeadKey("Ring above", "O", "o", "˚", "\u030a", []),
DeadKey("Caron", "Shift + 6", "^", "ˇ", "\u030c", []),
DeadKey("Cedilla", ",", ",", "¸", "\u0327", []),
DeadKey("Mathematical characters", "+", "=", "×", None, [
("-", "−"), # minus sign (subtly different from en dash)
(".", "⋅"), # dot operator (subtly different from middle dot)
("<", "≤"),
(">", "≥"),
("/", "≠"),
("~", "≈"),
("v", "√"),
("o", "°"),
("'", "′"),
('"', "″"),
("8", "∞"),
("D", "∆"),
("d", "∂"),
("s", "∫"),
("S", "∑"),
("P", "∏"),
]),
DeadKey("Greek letters", "G", "g", "γ", None, list(zip(latin, greek))),
]
dklut = dict((x.basekey, x) for x in dk)
dklut_no_dead = dict((x.basekey, x) for x in dk if x.basekey not in latin)
# patch our KLC template
def klc_tochar(s):
if s == '-1':
return None
elif len(s) == 1:
return s
else:
return chr(int(s, 16))
def klc_fromchar(ch, deadkey):
if ch is None:
return '-1'
if deadkey:
return "{0:04x}@".format(ord(ch))
else:
return ch if re.match("[a-zA-Z0-9]", ch) else "{0:04x}".format(ord(ch))
def klc_tocomment(ch):
if ch is None:
return '<none>'
else:
return unicodedata.name(ch, "?")
def substitute_deadkeys(m, with_dead_letters):
"""Fill in dead keys.
We configure a dead key for a character by adding AltGr to the matching keystroke.
Meaning that we cannot have dead keys on keys without AltGr
"""
lut = dklut if with_dead_letters else dklut_no_dead
keys = list(map(klc_tochar, m.groups()[1:]))
assert(len(keys) == 5) # that is [normal, shift, ctrl, altgr, shift+altgr]
dk = [None, None, None, lut.get(keys[0]), lut.get(keys[1])]
for i in range(2):
if dk[i+3]:
if keys[i+3] is not None:
raise ValueError('dead key configured for {} but AltGr combination already assigned to {}'.format(keys[i], keys[i+3]))
keys[i+3] = dk[i+3].deadkey
s = m.group(1)
for i in range(5):
s += klc_fromchar(keys[i], dk[i] is not None) + '\t'
s += '// '
s += ", ".join(map(klc_tocomment, keys))
s += "\n"
return s
def get_deadkeytables(with_dead_letters):
t = []
for d in dk:
if not with_dead_letters and d.keypress in latin:
continue
t.append("DEADKEY\t{:04x} \n".format(ord(d.deadkey)))
t.append("\n")
for a, b in d.all():
t.append("{:04x}\t{:04x}\t// {} -> {}\n".format(ord(a), ord(b), a, b))
t.append("\n")
return t
def get_deadkeynames(with_dead_letters):
t = []
for d in dk:
if not with_dead_letters and d.keypress in latin:
continue
ch = d.deadkey
t.append("{:04x} \"{}\"\n".format(ord(ch), unicodedata.name(ch)))
return t
""" open KLC template and fill it in """
def write_klc(with_dead_letters, output_file):
# loop over lines, patch up the dead keys in the LAYOUT section
lines = []
with (open('qucmp.klc.in', encoding='utf-16')) as f:
layout = False
pat = re.compile(r'([0-9a-f]{2}\t+\w+\t+\w+\t+)(\S+)\t+(\S+)\t+(\S+)\t+(\S+)\t+(\S+)\t+//')
for line in f:
if line == "{layout}\n":
layout = True
elif line == "{endlayout}\n":
layout = False
elif line == "{deadkeys}\n":
lines += get_deadkeytables(with_dead_letters)
elif line == "{deadkeynames}\n":
lines += get_deadkeynames(with_dead_letters)
elif layout and line[0] != '5': # the '5' avoids modifying the "decimal separator" key
m = pat.match(line)
if m:
lines.append(substitute_deadkeys(m, with_dead_letters))
else:
raise ValueError("don’t know how to parse input line\n"+line)
else:
n = "QUERTY AltGr Compose{full} {v[0]}.{v[1]}.{v[2]}".format(
full = " Full" if with_dead_letters else "",
v = version)
lines.append(line.format(filename=output_file, fullname=n))
with (open(output_file+'.klc', 'w', encoding='utf-16')) as f:
for l in lines:
f.write(l)
write_klc(True, 'qucmf{}{}{}'.format(*version))
write_klc(False, 'qucms{}{}{}'.format(*version))
# write markdown
with (open('keys.md', 'w', encoding='utf-8')) as f:
print("""# All key combinations
## Combinations with dead keys
""", file=f)
for d in dk:
print("### {}: <kbd>{}</kbd>".format(d.name, d.keypress), file=f)
print("| base | char | name |", file=f)
print("| ---- | ---- | -------- |", file=f)
for a, b in d.extras:
if a == " ": a = "Space"
print("| <kbd>{}</kbd> | {} | {} |".format(a, b, unicodedata.name(b).lower()), file=f)
if d.isaccent():
a, b = next(d.accent_letters('aoc'))
name = unicodedata.name(d.combining_accent).lower().replace('combining', 'letter with')
print("| _letter_ | {} | {} |".format(b, name), file=f)
print(file=f)