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