-
Notifications
You must be signed in to change notification settings - Fork 0
/
inflate_keymap.pl
83 lines (73 loc) · 2.51 KB
/
inflate_keymap.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/env perl
use 5.014;
use strict;
use warnings qw[ FATAL all ];
no warnings qw[ uninitialized numeric ];
use utf8::all;
use List::MoreUtils qw[uniq];
use Algorithm::FastPermute qw[permute];
use Text::Tabs;
use Getopt::Long qw[GetOptions :config bundling no_ignore_case no_auto_abbrev];
use Unicode::Casing uc => \&my_uc, ucfirst => \&my_ucfirst;
$tabstop = 16;
my(%uc,%lc, %kp); # upper/lower case, keypad
BEGIN {
%uc= qw[ ß ẞ i I ı I ];
%lc = qw[ İ i I i ẞ ß ];
%kp = qw[ + <kPlus> - <kMinus> * <kMultiply> / <kDivide> ];
}
my %opt = qw[ case 1 marks 0 ];
GetOptions( \%opt, qw[ case|c! marks|m! keypad|k! exclude|e=s ],
'tabstop|t=i' => \$tabstop,
'i!' => sub { @uc{qw/i ı/} = qw[ İ I ]; %lc = reverse %uc; } )
or die "Error getting options\n";
sub my_uc { my $str = shift; $str =~ s/([ßiı])/$uc{$1}/g; return uc $str; }
sub my_ucfirst { my $str = shift; $str =~ s/\A([ßiı])/$uc{$1}/g; return ucfirst $str; }
sub my_lc { my $str = shift; $str =~ s/([ẞİI])/$lc{$1}/g; return lc $str; }
sub my_lcfirst { my $str = shift; $str =~ s/\A([ẞİI])/$lc{$1}/g; return lcfirst $str; }
sub tc { ($_[0] // $_) =~ s/(\p{LC}+)/\u\L$1/gr }
my $exclude = $opt{exclude} ? qr/$opt{exclude}/ : undef;
my %seen;
ENTRY:
while ( <> ) {
chomp;
say $_ and next ENTRY if 1 .. /loadkeymap/;
say $_ and next if /^"/ or /^\s*$/;
# my $full_line = $_;
my($key,$char,$comment) = split /\s+/, $_, 3;
my $name = charnames::viacode(ord $char);
# $comment = q{} if $comment =~ /\Q$name/;
$_ = $key."\t".$char;
my @lines;
if ( $opt{case} and /^\S*\p{LC}/ ) {
@lines = uniq (lc, uc, tc);
}
@lines = ($_) unless @lines;
if ( $opt{marks} and /^([\pP\pS]{2,})\pL/ ) {
my $m = $1 =~ s/\\(["\\])/$1/gr;
my @marks = $m =~ /./g;
my @perms;
{
local $" = q();
permute { push @perms, "@marks" =~ s/(["\\])/\\$1/gr } @marks;
}
my @perm_lines;
for my $line ( @lines ) {
$line =~ s/^[\pP\pS]+//;
push @perm_lines, map { $_.$line } @perms;
}
@lines = @perm_lines;
}
if ( $opt{keypad} and grep { m{[-+*/]} } @lines ) {
push @lines, map { s{\A(\S+)}{$1 =~ s!([-+*/])!$kp{$1}!gr}er } @lines;
}
@lines = grep { !$seen{$_}++ } @lines;
if ( $exclude ) {
@lines = grep { !/$exclude/ } @lines;
}
next ENTRY unless @lines;
$lines[0] .= qq{\t}.$comment if $comment;
# $lines[0] .= qq{\t" }.$name;
say $_ for expand @lines;
}
__END__