digraphs/xorg: support keysymdef aliases in compose definitions
[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         eval {
27                 $mnem =~ s{<([^>]+)> \h?}{$symname->{$1} // die "reference to unknown keysym $1\n"}eg;
28                 1;
29         } or warn($@), next;
30         $mnem !~ m/[^\x20-\x7F]/ or next;  # skip unicode
31 #       (state $seen = {})->{$chr}++ and next;
32         printf "%s => %s,\n", pp($mnem), pp($chr);
33 }
34
35 say '}';
36
37 __END__
38
39 =head1 NAME
40
41 mkdigraphs-xorg - Output Xorg compose sequences
42
43 =head1 SYNOPSIS
44
45
46     mkdigraphs-xorg /usr/share/X11/locale/en_US.UTF-8/Compose >digraphs-xorg.inc.pl
47     perl -e'$di = do "digraphs-xorg.inc.pl"; print chr $di->{AT}'
48
49 =head1 DESCRIPTION
50
51 Extracts Multi_key definitions from X11/Xorg Compose.pre include file.
52 If successful, Perl code is output resulting in a hash
53 with Unicode code points keyed by mnemonics.
54 Any errors and warnings are given at STDERR.
55
56 =head1 AUTHOR
57
58 Mischa POSLAWSKY <perl@shiar.org>
59
60 =head1 LICENSE
61
62 Licensed under the GNU Affero General Public License version 3.
63