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