2 # Generate a unicode sampler from an Xmodmap file
5 XKEYSIMDEF_H = "/usr/include/X11/keysymdef.h"
8 def sample(lines, keysyms):
9 """Conver lines from an Xmodmap file to sampler lines."""
13 if not line.startswith('keycode '):
16 # keycode 10 = 1 exclam U2081 onehalf
17 fields = fields + [None] * (7-len(fields))
18 k,keycode,eq,a,b,c,d = fields
19 assert k == 'keycode', k
21 keycode = int(keycode)
23 continue # control code, not very interesting
25 # convert keys to unicode
27 for i,key in enumerate(keys):
31 keys[i] = keysyms[key]
33 assert key.startswith('U'), key
34 key = r'\u'+key[len('U'):]
35 keys[i] = unicode(key, 'unicode escape')
37 line = (' '.join([key or ' ' for key in keys])).rstrip()
41 def read_keysyms(file=None):
42 """Read keysymdef.h and extract keysym -> unicode mappings."""
46 for line in open(file, 'r').readlines():
47 if not line.startswith('#define XK_'):
49 # #define XK_space 0x0020 /* U+0020 SPACE */
51 assert len(fields) >= 3, fields
54 assert keysym.startswith('XK_'), keysym
55 assert code.startswith('0x'), code
56 keysym = keysym[len('XK_'):]
59 if not codepoint.startswith('U+'):
62 codepoint = r'\u'+codepoint[len('U+'):]
63 codepoint = unicode(codepoint, 'unicode escape')
65 codepoint = None # keysym to unicode mapping not well defined
66 keysyms[keysym] = codepoint
70 if __name__ == '__main__':
74 xmodmap_filename = sys.argv[1]
76 keysyms = read_keysyms()
77 lines = open(xmodmap_filename, 'r').readlines()
78 #lines = sample(lines, keysyms)
79 lines = sorted(sample(lines, keysyms))
80 print 'These characters are bound in my current .Xmodmap'
81 print '(%s)' % time.asctime()
84 print line.encode(ENCODING)