| 1 |
#! perl |
| 2 |
|
| 3 |
=head1 NAME |
| 4 |
|
| 5 |
bidi - Make urxvt present Bidi text correctly |
| 6 |
|
| 7 |
=head1 DESCRIPTION |
| 8 |
|
| 9 |
This extension filters the text displayed by Urxvt, so that Bi-directional |
| 10 |
text (e.g., Hebrew or Arabic mixed with English) is displayed correctly. It |
| 11 |
does so using the L<Text::Bidi> module (which should be installed). |
| 12 |
|
| 13 |
The extension emulates a cursor via rendition. This means that when typing, |
| 14 |
there will be two cursors, the original one whose location corresponds to the |
| 15 |
current location within the logical string (so, mostly useless), and a fake |
| 16 |
one which corresponds to the current insertion point. |
| 17 |
|
| 18 |
To enable the extension, add C<bidi> to the I<Urxvt.perl-ext-common> |
| 19 |
resource. See urxvt(1) and urxvtperl(1) for other options and more details. |
| 20 |
|
| 21 |
The extension recognises the following resources: |
| 22 |
|
| 23 |
=over |
| 24 |
|
| 25 |
=cut |
| 26 |
|
| 27 |
#:META:RESOURCE:%.FieldSep:string:Separator between different fields |
| 28 |
|
| 29 |
=item I<bidi.FieldSep> |
| 30 |
|
| 31 |
This should be a string on which each line is split before applying the Bidi |
| 32 |
algorithm. This permits creating tables, where each cell is treated |
| 33 |
separately, e.g., in the index of an email client. Note that this is a |
| 34 |
string, not a regular expression. The default is C<\x{2502}>. |
| 35 |
|
| 36 |
=cut |
| 37 |
|
| 38 |
#:META:RESOURCE:%.LRE:string:Insert LRE mark where this matches |
| 39 |
|
| 40 |
=item I<bidi.LRE> |
| 41 |
|
| 42 |
A regular expression. The plugin insert an explicit left-right mark where it |
| 43 |
matches. |
| 44 |
|
| 45 |
=cut |
| 46 |
|
| 47 |
#:META:RESOURCE:%.PDF:string:Insert PDF mark where this matches |
| 48 |
|
| 49 |
=item I<bidi.PDF> |
| 50 |
|
| 51 |
A regular expression. The plugin insert a PDF mark (end of LRE) where it |
| 52 |
matches. |
| 53 |
|
| 54 |
=cut |
| 55 |
|
| 56 |
#:META:RESOURCE:%.par:boolean:Work in paragraph mode |
| 57 |
|
| 58 |
=item I<bidi.par> |
| 59 |
|
| 60 |
Boolean, if true work in paragraph mode: the paragraph direction is not reset |
| 61 |
on each line, but only on lines that start or end a paragraph |
| 62 |
|
| 63 |
=cut |
| 64 |
|
| 65 |
#:META:RESOURCE:%.ParReset:string:Start a new paragraph where this matches |
| 66 |
|
| 67 |
=item I<bidi.ParReset> |
| 68 |
|
| 69 |
A regular expression. A line matching this will start a new paragraph. |
| 70 |
|
| 71 |
=cut |
| 72 |
|
| 73 |
#:META:RESOURCE:%.ParResetAfter:string:Start a new paragraph after this matches |
| 74 |
|
| 75 |
=item I<bidi.ParResetAfter> |
| 76 |
|
| 77 |
Similar to I<bidi.ParReset>, but the paragraph starts after the matching |
| 78 |
line. |
| 79 |
|
| 80 |
=back |
| 81 |
|
| 82 |
For example, I use urxvt with this plugin to edit LaTeX documents in Hebrew. |
| 83 |
I have the following resources defined: |
| 84 |
|
| 85 |
URxvt.bidi.LRE: \\\\[([] |
| 86 |
URxvt.bidi.PDF: \\\\[)\\]] |
| 87 |
urxvt-vimh.bidi.par: true |
| 88 |
urxvt-vimh.cursorUnderline: true |
| 89 |
urxvt-vimh.bidi.ParReset: ^\\s*$|^\\\\begin|^\\\\\\[ |
| 90 |
urxvt-vimh.bidi.ParResetAfter: ^\\\\end|\\\\\\]$ |
| 91 |
|
| 92 |
This causes inline equations (delimited by C<\(> and C<\)>) be displayed |
| 93 |
correctly, and equation environments be considered a paragraph (so that it is |
| 94 |
displayed on the left). |
| 95 |
|
| 96 |
=cut |
| 97 |
|
| 98 |
use 5.10.0; |
| 99 |
use Text::Bidi qw(log2vis is_bidi); |
| 100 |
use Text::Bidi::Constants; |
| 101 |
use Encode qw(decode_utf8); |
| 102 |
|
| 103 |
sub on_start { |
| 104 |
my ($self) = @_; |
| 105 |
$self->{'split'} = decode_utf8($self->x_resource('%.FieldSep')) // |
| 106 |
"\x{2502}"; |
| 107 |
my $lre = decode_utf8($self->x_resource('%.LRE')) // ''; |
| 108 |
my $pdf = decode_utf8($self->x_resource('%.PDF')) // ''; |
| 109 |
my $preset = decode_utf8($self->x_resource('%.ParReset')) // '^\s*$'; |
| 110 |
my $preseta = decode_utf8($self->x_resource('%.ParResetAfter')) // '(?!)'; |
| 111 |
$self->{'par'} = decode_utf8($self->x_resource('%.par')) // ''; |
| 112 |
$self->{'lre'} = qr/.*?($lre)/ if $lre; |
| 113 |
$self->{'pdf'} = qr/.*?$pdf/ if $pdf; |
| 114 |
$self->{'pres'} = qr/$preset/; |
| 115 |
$self->{'presa'} = qr/$preseta/; |
| 116 |
#warn "LRE: $self->{'lre'}\n"; |
| 117 |
#warn "PDF: $self->{'pdf'}\n"; |
| 118 |
#warn "$self->{'lre'}, $self->{'llre'}, $self->{'pdf'}, |
| 119 |
#$self->{'lpdf'}\n"; |
| 120 |
$self->{'spre'} = qr/\Q$self->{'split'}\E/; |
| 121 |
$self->{'ls'} = length($self->{'split'}); |
| 122 |
} |
| 123 |
|
| 124 |
sub on_refresh_begin { |
| 125 |
my ($self) = @_; |
| 126 |
my ($crow, $ccol) = $self->screen_cur; |
| 127 |
my $pdir; |
| 128 |
my $l; |
| 129 |
for my $i ( 0..$self->nrow-1 ) { |
| 130 |
$l = $self->ROW_t($i); |
| 131 |
$pdir = undef if $l =~ $self->{'pres'}; |
| 132 |
# for speed |
| 133 |
next unless is_bidi($l) or |
| 134 |
(defined($pdir) and $pdir == $Text::Bidi::Par::RTL); |
| 135 |
chomp($l); |
| 136 |
my @r = @{$self->ROW_r($i)}; |
| 137 |
# expand combining chars (Nikud) from one illegal char to two legal |
| 138 |
$l = $self->special_decode($l); |
| 139 |
# add bidi marks |
| 140 |
if ( $self->{'lre'} ) { |
| 141 |
while ( $l =~ /\G$self->{'lre'}/gc ) { |
| 142 |
my $p = pos($l); |
| 143 |
my $j = $p - length($1); |
| 144 |
substr($l, $j, 0) = "\x{202a}"; |
| 145 |
splice(@r, $j, 0, 0); |
| 146 |
$ccol++ if $j <= $ccol and $i == $crow; |
| 147 |
pos($l) = $p + 1; |
| 148 |
} |
| 149 |
pos($l) = undef; |
| 150 |
while ( $l =~ /\G.*?$self->{'pdf'}/gc ) { |
| 151 |
my $j = pos($l); |
| 152 |
substr($l, $j, 0) = "\x{202c}"; |
| 153 |
splice(@r, $j, 0, 0); |
| 154 |
$ccol++ if $j <= $ccol and $i == $crow; |
| 155 |
pos($l) = $j + 1; |
| 156 |
} |
| 157 |
} |
| 158 |
# nikud |
| 159 |
while ( $l =~ /[\x{5b0}-\x{5bb}]/g ) { |
| 160 |
my $j = pos($l) - 1; |
| 161 |
splice(@r, $j, 0, 0); |
| 162 |
$ccol++ if $j <= $ccol and $i == $crow; |
| 163 |
} |
| 164 |
my @l = split $self->{'spre'}, $l; |
| 165 |
my (@res, @map); |
| 166 |
# current offset within the line |
| 167 |
my $off = 0; |
| 168 |
# we keep the map, so that we can apply it to the rendition |
| 169 |
for my $part ( @l ) { |
| 170 |
my ($p, $v) = log2vis($part, undef, $pdir); |
| 171 |
my $types = $p->types; |
| 172 |
#warn "$i: <$part> ==>\n <$v>\n dir:" . $p->dir . "\ntype: " . |
| 173 |
#join(',', map { my $cc = substr($part, $_, 1); '[' . $cc . ':' . |
| 174 |
#sprintf("%x", ord($cc)) . ":" . $types->[$_] . ']' } |
| 175 |
#0..$#{$types}) . "\n\n"; |
| 176 |
push @res, $v; |
| 177 |
my @mmap = @{$p->map}; |
| 178 |
push @map, map { $_ + $off } @mmap; |
| 179 |
$off += length($v); |
| 180 |
# compensate for the field separator |
| 181 |
push @map, $off..$off+$self->{'ls'}-1; |
| 182 |
$off += $self->{'ls'}; |
| 183 |
$pdir = $p->dir if ( $self->{'par'} and not defined $pdir ); |
| 184 |
} |
| 185 |
# remove the last field separator |
| 186 |
splice @map, -$self->{'ls'}; |
| 187 |
my $res = join($self->{'split'}, @res); |
| 188 |
# remove bidi marks |
| 189 |
while ( $res =~ /[\x{200b}-\x{200f}\x{202a}-\x{202f}\x{5b0}-\x{5bb}]/g ) { |
| 190 |
$map[pos($res)-1] = -1; |
| 191 |
} |
| 192 |
$res =~ s/[\x{200b}-\x{200f}\x{202a}-\x{202f}]//g; |
| 193 |
$res = $self->special_encode($res); |
| 194 |
# fake cursor |
| 195 |
if ( $crow == $i ) { |
| 196 |
$r[$ccol] |= urxvt::RS_RVid unless $map[$ccol]==$ccol; |
| 197 |
} |
| 198 |
my @newr = (@r)[grep { $_ >= 0 } @map]; |
| 199 |
# keep the logical data for restoring after display |
| 200 |
$self->{'text'}[$i] = $self->ROW_t($i, $res); |
| 201 |
$self->{'rend'}[$i] = $self->ROW_r($i, \@newr); |
| 202 |
} continue { |
| 203 |
$pdir = undef if $l =~ $self->{'presa'}; |
| 204 |
} |
| 205 |
() |
| 206 |
} |
| 207 |
|
| 208 |
sub on_refresh_end { |
| 209 |
my ($self) = @_; |
| 210 |
return unless defined $self->{'text'}; |
| 211 |
foreach (0..$self->nrow-1 ) { |
| 212 |
next unless defined $self->{'text'}[$_]; |
| 213 |
$self->ROW_t($_, $self->{'text'}[$_]); |
| 214 |
$self->ROW_r($_, $self->{'rend'}[$_]); |
| 215 |
} |
| 216 |
delete $self->{'text'}; |
| 217 |
delete $self->{'rend'}; |
| 218 |
() |
| 219 |
} |
| 220 |
|