-
Notifications
You must be signed in to change notification settings - Fork 3
/
compose.py
197 lines (167 loc) · 5.29 KB
/
compose.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Parser pour Compose
#
# Copyright (C) 2008 Gaëtan Lehmann <gaetan.lehmann@jouy.inra.fr>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
import unicodedata, re, sys, defaults
def ishex(s):
for c in s:
if c not in "0123456789abcdefABCDEF":
return False
return True
fKeysymdef = file(defaults.keysymdefFile)
regexp = re.compile(r'#define XK_([^ ]+).*U\+([0-9A-Fa-f]+)')
composeNames = {}
for l in fKeysymdef:
res = regexp.match(l)
if res:
name = res.group(1)
c = res.group(2)
try:
C = unichr( int(c, 16) )
composeNames[name] = C
except:
print l
pass
# some missing chars
composeNames["combining_acute"] = u"́"
composeNames["combining_belowdot"] = u"̣"
composeNames["combining_grave"] = u"̀"
composeNames["combining_tilde"] = u"̃"
composeNames["NoSymbol"] = u""
composeNames["VoidSymbol"] = u""
# dead keys
composeNames["dead_abovedot"] = u"abovedot"
composeNames["dead_abovering"] = u"ringabove"
composeNames["dead_acute"] = u"acute"
composeNames["dead_belowdot"] = u"belowdot"
composeNames["dead_breve"] = u"breve"
composeNames["dead_caron"] = u"caron"
composeNames["dead_cedilla"] = u"cedilla"
composeNames["dead_circumflex"] = u"circumflex"
composeNames["dead_dasia"] = u"dasia"
composeNames["dead_diaeresis"] = u"diaeresis"
composeNames["dead_doubleacute"] = u"doubleacute"
composeNames["dead_grave"] = u"grave"
composeNames["dead_horn"] = u"horn"
composeNames["dead_macron"] = u"macron"
composeNames["dead_ogonek"] = u"ogonek"
composeNames["dead_psili"] = u"psili"
composeNames["dead_tilde"] = u"tilde"
composeNames["dead_stroke"] = u"stroke"
composeNames["UFDD0"] = u"stroke"
composeNames["dead_currency"] = u"currency"
composeNames["UFDD1"] = u"currency"
composeNames["Multi_key"] = u"Multi_key"
composeNames["dead_greek"] = u"greek"
composeNames["UFDD2"] = u"greek"
composeNames["dead_commabelow"] = u"commabelow"
composeNames["UFDD3"] = u"commabelow"
composeNames["dead_hook"] = u"hook"
composeNames["dead_horn"] = u"horn"
composeNames["dead_belowdot"] = u"belowdot"
composeChars = {}
for name, C in composeNames.iteritems():
composeChars[C] = name
# force oslash name
composeChars[u"ø"] = "oslash"
from terminators import terminators
def char(k):
if k == '':
return u''
if not composeNames.has_key(k) and k[0] == 'U' and len(k) == 5 and ishex(k[1:]):
C = unichr(int(k[1:], 16))
k = k.upper()
if composeChars.has_key(C):
return C
composeNames[k] = C
composeChars[C] = k
return composeNames[k]
def name(c):
if composeChars.has_key(c):
return composeChars[c]
k = u"U"+repr(c)[4:-1].rjust(4, '0').upper()
composeNames[k] = c
composeChars[c] = k
return k
def isSupportedChar(k):
if k[0] == 'U' and len(k) == 5 and ishex(k[1:]):
return True
return composeNames.has_key(k)
def areSupportedChars(ks):
for k in ks:
if not isSupportedChar(k):
return False
return True
def upperUnicode(k):
if k[0] == 'U' and 5 <= len(k) <= 6 and ishex(k[1:]):
return k.upper()
return k
fCompose = file(defaults.composeFile)
states = set()
outputs = {}
for l in fCompose:
if l.startswith("<Multi_key>") and "<KP_" not in l and "<underbar>" not in l and "<rightcaret>" not in l and "<leftshoe>" not in l and "<leftcaret>" not in l and "<rightshoe>" not in l and "<U223C>" not in l:
seq = re.findall('<([^ ]+)>', l.split(":")[0])
seq = [upperUnicode(s) for s in seq]
if areSupportedChars(seq):
for i in range(1, len(seq)):
s = tuple(seq[:i])
states.add(s)
c = l.split(":")[1].split()[1]
outputs[tuple(seq)] = char(c)
# for k in seq:
# if k[0] == 'U' and len(k) == 5 and ishex(k[1:]):
# k = unichr(int(k[1:], 16))
# keys.add(k)
statesByAction = {}
for S in states:
a = S[-1]
s = S[:-1]
sset = statesByAction.get(a, set())
sset.add(s)
statesByAction[a] = sset
outputsByAction = {}
for S in outputs.keys():
a = S[-1]
s = S[:-1]
sset = outputsByAction.get(a, set())
sset.add((s, outputs[S]))
outputsByAction[a] = sset
charActions = {}
for a in set(statesByAction.keys() + outputsByAction.keys()):
if charActions.has_key(char(a)) :
print (a, char(a), charActions[char(a)])
charActions[char(a)] = a
# print charActions[u'(']
# sys.exit()
if __name__ == "__main__":
for a in sorted(set(statesByAction.keys() + outputsByAction.keys()) ):
C = char(a)
if C:
print u' <action id="%s">' % C
if statesByAction.has_key(a):
for s in sorted(statesByAction[a]):
print u' <when state="%s" next="%s"/>' % ('_'.join(s), '_'.join(s+(a,)))
if outputsByAction.has_key(a):
for s, c in sorted(outputsByAction[a]):
print u' <when state="%s" output="%s"/>' % ('_'.join(s), c)
print u' </action>'
print '''
<action id="Multikey">
<when state="none" next="Multikey"/>
</action>
'''
print " <terminators>"
for ss in sorted(states):
C = ''.join([terminators.get(char(s), char(s)) for s in ss])
s = '_'.join(list(ss))
print ' <when state="%s" output="%s"/>' % (s, C)
print " </terminators>"