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