digraphs: extended regexps for all xorg parsing
[sheet.git] / tools / mkdigraphs-xorg
1 #!/usr/bin/env perl
2 use 5.014;
3 use warnings;
4 use utf8;
5 use open IO => ':utf8', ':std';
6 use re '/msx';
7 use Data::Dump 'pp';
8
9 our $VERSION = '1.01';
10
11 my $keysymh;
12 open $keysymh, '<', 'data/keysymdef.h'
13         or open $keysymh, '<', '/usr/include/X11/keysymdef.h'
14         or die "Could not find keysym definitions: $!\n";
15
16 my %keysym;
17 while (readline $keysymh) {
18         m{
19                 \A  [#]define[ ]XK_ (?<name>[a-zA-Z_0-9]+)
20                 \h+ 0x(?<value>[0-9a-f]+)
21                 \h* [/][*] [\h(] U[+] (?<unicode>[0-9A-F]{4,6})
22         } or next;
23         $keysym{ $+{name} } = chr hex $+{unicode};
24 }
25
26 say "# automatically generated by $0";
27 say '+{';
28
29 while ($_ = readline) {
30         my ($mnem, $chr, $trail) = m/\A <Multi_key> \h (.*?) \h+ : \h "([^"]+)" \h* (.*)/
31                 or next;
32         $chr =~ s/\\(.)/$1/g;
33         $mnem !~ m/<dead | <KP_ | <U[0-9A-Fa-f]{4}/ or next;  # skip non-standard keys
34         $mnem =~ s{<([^>]+)> \h?}{$keysym{$1} // die "reference to unknown keysym $1\n"}eg;
35         $mnem !~ m/[^\x20-\x7F]/ or next;  # skip unicode
36 #       (state $seen = {})->{$chr}++ and next;
37         printf "%s => %s,\n", pp($mnem), pp($chr);
38 }
39
40 say '}';
41
42 __END__
43
44 =head1 NAME
45
46 mkdigraphs-xorg - Output Xorg compose sequences
47
48 =head1 SYNOPSIS
49
50
51     mkdigraphs-xorg /usr/share/X11/locale/en_US.UTF-8/Compose >digraphs-xorg.inc.pl
52     perl -e'$di = do "digraphs-xorg.inc.pl"; print chr $di->{AT}'
53
54 =head1 DESCRIPTION
55
56 Extracts Multi_key definitions from X11/Xorg Compose.pre include file.
57 If successful, Perl code is output resulting in a hash
58 with Unicode code points keyed by mnemonics.
59 Any errors and warnings are given at STDERR.
60
61 =head1 AUTHOR
62
63 Mischa POSLAWSKY <perl@shiar.org>
64
65 =head1 LICENSE
66
67 Licensed under the GNU Affero General Public License version 3.
68