digraphs: link to and from altgr keyboards
[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 use Shiar_Sheet::FormatChar;
10
11 our $VERSION = '1.01';
12
13 my $matchvim;  # enable to prefer best compatibility
14
15 my $symname = eval {
16         open my $keysymh, '<', 'data/keysymdef.json' or die $!;
17         local $/;
18         return decode_json(readline $keysymh);
19 } or die "Could not read keysym definitions: $@\n";
20
21 my $vidi = eval {
22         open my $jsfh, '<', 'data/digraphs.json' or die $!;
23         local $/;
24         return JSON->new->decode(readline $jsfh);
25 } or warn "Could not read comparison digraphs: $@\n";
26
27 my %table;
28 while ($_ = readline) {
29         my ($mnem, $chr, $trail) = m/\A <Multi_key> \h (.*?) \h+ : \h "([^"]+)" \h* (.*)/
30                 or next;
31         $chr =~ s/\\(.)/$1/g;
32         $mnem !~ m/<dead | <KP_ | <U[0-9A-Fa-f]{4}/ or next;  # skip non-standard keys
33         eval {
34                 $mnem =~ s{<([^>]+)> \h?}{$symname->{$1} // die "reference to unknown keysym $1\n"}eg;
35                 1;
36         } or warn($@), next;
37         $mnem =~ m/\A [\x20-\x7F]{2} \z/ or next;  # only interested in two ascii
38
39         my $alias = \(state $seen = {})->{$chr};  # assume first is preferred
40         my $cp = ord $chr;
41         my ($class, $name, undef, undef, $string) = @{
42                 Shiar_Sheet::FormatChar->glyph_info($cp)
43         };
44         my $comparison = (
45                 !$vidi->{key}->{$mnem} ? 'l3' :  # free
46                 $vidi->{key}->{$mnem}->[0] != $cp ? 'l1' :  # conflict
47                 $vidi->{key}->{$mnem}->[2] eq 'l5' ? 'l5' :  # rfc
48                 'l4'  # any
49         );
50
51         if (${$alias}) {
52                 # aliases an earlier occurrence
53                 if ($matchvim and ${$alias}->[2] lt $comparison) {
54                         # replace lower compatibility level
55                         ${$alias}->[3] = 'l0';
56                         ${$alias}->[2] .=  ' u-' . ${$alias}->[2];
57                         ${$alias} = undef;
58                 }
59                 else {
60                         $class = 'l0';
61                         my $menm = substr($mnem, 1, 1).substr($mnem, 0, 1);
62                         if ($table{$menm} && $table{$menm}[0] == $cp) {
63                                 # unannotated if identical to reversed input
64                                 $cp = 0;
65                         }
66                         else {
67                                 $class .= ' ex';
68                         }
69                 }
70         }
71
72         $table{$mnem} = [ $cp, $name, $comparison, $class, $string // () ];
73         ${$alias} //= $table{$mnem};
74 }
75
76 print JSON->new->canonical->indent->encode({
77         title => 'X.Org',
78         key   => \%table,
79         intro => join("\n",
80                 'Character mnemonics following compose key ⎄:',
81                 'in the X Window System (Shift+AltGr by default).',
82                 'Differences from <a href="/digraphs">RFC-1345</a> are indicated.',
83                 'Also see <a href="/keyboard/altgr">monograph maps</a>',
84                 'of alternative Xorg input modes with an AltGr modifier.</p>',
85         ),
86         keywords => [qw( xorg x11 x )],
87         flag  => {
88                 'l5' => "matching RFC-1345",
89                 'l4' => "matching Vim extension",
90                 'l3' => "unique to Xorg",
91                 'l1' => "conflict",
92                 ('l0' => "Xorg preference") x !!$matchvim,
93                 'l0 ex' => "alias",
94         },
95         flagclass => {
96                 l5 => 'u-l4',
97                 l4 => 'u-l5',
98         },
99 });
100
101 __END__
102
103 =head1 NAME
104
105 mkdigraphs-xorg - Output Xorg compose sequences
106
107 =head1 SYNOPSIS
108
109
110     mkdigraphs-xorg /usr/share/X11/locale/en_US.UTF-8/Compose |
111     jq -r '.key."AT"[0]' | perl -nE 'say chr' # @
112
113 =head1 DESCRIPTION
114
115 Extracts Multi_key definitions from X11/Xorg Compose.pre include file.
116 If successful, a JSON object is output containing a digraphs list in C<key>
117 with Unicode code points keyed by mnemonics.
118 Any errors and warnings are given at STDERR.
119
120 =head1 AUTHOR
121
122 Mischa POSLAWSKY <perl@shiar.org>
123
124 =head1 LICENSE
125
126 Licensed under the GNU Affero General Public License version 3.
127