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