-
Notifications
You must be signed in to change notification settings - Fork 0
/
compile
executable file
·168 lines (151 loc) · 4.56 KB
/
compile
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
#!/usr/bin/env python
import re
import time
keymap = {
0x0020: 'space',
0x0021: 'exclam',
0x0022: 'quotedbl',
0x0023: 'numbersign',
0x0024: 'dollar',
0x0025: 'percent',
0x0026: 'ampersand',
0x0027: 'apostrophe',
0x0028: 'parenleft',
0x0029: 'parenright',
0x002A: 'asterisk',
0x002B: 'plus',
0x002C: 'comma',
0x002D: 'minus',
0x002E: 'period',
0x002F: 'slash',
0x003A: 'colon',
0x003B: 'semicolon',
0x003C: 'less',
0x003D: 'equal',
0x003E: 'greater',
0x003F: 'question',
0x0040: 'at',
0x005B: 'bracketleft',
0x005C: 'backslash',
0x005D: 'bracketright',
0x005E: 'asciicircum',
0x005F: 'underscore',
0x0060: 'grave',
0x007B: 'braceleft',
0x007C: 'bar',
0x007D: 'braceright',
0x007E: 'asciitilde',
}
def key(c):
if c == r'\\' or c == r'\'':
c = c[1]
return keymap.get(ord(c), c)
unimap = {}
with open('UnicodeData.txt', 'r') as f:
for line in f:
if not line:
continue
line = line.strip()
v = tuple(line.split(';'))
k = int(v[0], 16)
unimap[k] = v
timestamp = time.strftime('%Y-%m-%d %H:%M:%S %z')
header = f'''\
# vim: ft=xcompose
# XCompose digraphs as defined by Vim
#
# Generated by:
#
# https://github.com/mkoskar/xcompose-vim
# > {timestamp}
#
# See also:
#
# (vim/nvim) :help digraphs
# Compose(5)
# RFC1345 (Character Mnemonics and Character Sets)
# The second character has a standard meaning:
#
# Char name Char Meaning
# --------- ---- -------
# Exclamation mark ! Grave
# Apostrophe ' Acute accent
# Greater-Than sign > Circumflex accent
# Question mark ? Tilde
# Hyphen-Minus - Macron
# Left parenthesis ( Breve
# Full stop . Dot above
# Colon : Diaeresis
# Comma , Cedilla
# Underline _ Underline
# Solidus / Stroke
# Quotation mark " Double acute accent
# Semicolon ; Ogonek
# Less-Than sign < Caron
# Zero 0 Ring above
# Two 2 Hook
# Nine 9 Horn
#
# Equals = Cyrillic (= used as second char)
# Asterisk * Greek
# Percent sign % Greek/Cyrillic special
# Plus + smalls: Arabic, capitals: Hebrew
# Three 3 some Latin/Greek/Cyrillic letters
# Four 4 Bopomofo
# Five 5 Hiragana
# Six 6 Katakana
'''
digraphs = set()
with open('digraph.c', 'r') as f, \
open('.XCompose-vim', 'w') as of:
of.write(header)
started = False
for line in f:
if not line:
continue
line = line.strip()
if not started:
if re.search(r'digraphs for Unicode from RFC1345', line):
started = True
else:
if re.fullmatch(r'\};', line):
break
if m := re.search(r'define DG_START_(\w*)', line):
of.write(f'\n# {m.group(1)}\n')
continue
if m := re.search(r"\{\s*'(.*)',\s*'(.*)',\s*(.+)\s*\}", line):
a = key(m.group(1))
b = key(m.group(2))
digraphs.add((a, b))
dec = int(m.group(3), 16)
uni = unimap[dec]
val = f'"{chr(dec)}"'
if dec == 10:
val = r'"\x0A"'
elif dec == 92:
val = r'"\\"'
desc = f'# {uni[0]} : {uni[1]}'
if uni[2] == 'Cc' and uni[10]:
desc += f' {uni[10]}'
a_ = f'<{a}>'
b_ = f'<{b}>'
of.write(f'<Multi_key> {a_:>14} {b_:14} : {val:6} {desc}\n')
compose_file = '/usr/share/X11/locale/en_US.UTF-8/Compose'
header = f'''\
# vim: ft=xcompose
# {compose_file}
# - tri+graphs hiding Vim digraphs are commented out starting with ###
#
# Generated by:
#
# https://github.com/mkoskar/xcompose-vim
# > {timestamp}
'''
with open(compose_file, 'r') as f, \
open('.XCompose-en', 'w') as of:
of.write(header)
for line in f:
if m := re.match(r'^<Multi_key>\s*<(\w+)>\s*<(\w+)>\s*<\w+>', line):
if (m.group(1), m.group(2)) in digraphs:
of.write('### ')
of.write(line)