| 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 | Mouse selections follow the displayed text and copy the original characters | ||
| 19 | in logical reading order. A selection across mixed-direction runs may select | ||
| 20 | several separate ranges in the logical text; intervening, unselected | ||
| 21 | characters are not copied. Arabic text is copied without presentation-form | ||
| 22 | shaping, and combining marks remain attached to their original characters. | ||
| 23 | |||
| 24 | Click and drag to select characters, double-click for a word, or triple-click | ||
| 25 | for a line. Right-click and Shift-click extend the selection; Meta-drag makes | ||
| 26 | a rectangular selection. Dragging outside the window scrolls through the | ||
| 27 | history. In applications that request mouse reporting, use Shift-drag to | ||
| 28 | select text instead of sending the mouse event to the application. | ||
| 29 | |||
| 30 | When bidi text is displayed, word selection uses the C<cutchars> resource. | ||
| 31 | Custom patterns from the separate C<selection> extension are not applied. | ||
| 32 | |||
| 33 | To enable the extension, add C<bidi> to the I<Urxvt.perl-ext-common> | ||
| 34 | resource. See urxvt(1) and urxvtperl(1) for other options and more details. | ||
| 35 | |||
| 36 | The extension recognises the following resources: | ||
| 37 | |||
| 38 | =over | ||
| 39 | |||
| 40 | =cut | ||
| 41 | |||
| 42 | #:META:RESOURCE:%.FieldSep:string:Separator between different fields | ||
| 43 | |||
| 44 | =item I<bidi.FieldSep> | ||
| 45 | |||
| 46 | This should be a string on which each line is split before applying the Bidi | ||
| 47 | algorithm. This permits creating tables, where each cell is treated | ||
| 48 | separately, e.g., in the index of an email client. Note that this is a | ||
| 49 | string, not a regular expression. The default is C<\x{2502}>. | ||
| 50 | |||
| 51 | =cut | ||
| 52 | |||
| 53 | #:META:RESOURCE:%.LRE:string:Insert LRE mark where this matches | ||
| 54 | |||
| 55 | =item I<bidi.LRE> | ||
| 56 | |||
| 57 | A regular expression. The plugin insert an explicit left-right mark where it | ||
| 58 | matches. | ||
| 59 | |||
| 60 | =cut | ||
| 61 | |||
| 62 | #:META:RESOURCE:%.PDF:string:Insert PDF mark where this matches | ||
| 63 | |||
| 64 | =item I<bidi.PDF> | ||
| 65 | |||
| 66 | A regular expression. The plugin insert a PDF mark (end of LRE) where it | ||
| 67 | matches. | ||
| 68 | |||
| 69 | =cut | ||
| 70 | |||
| 71 | #:META:RESOURCE:%.par:boolean:Work in paragraph mode | ||
| 72 | |||
| 73 | =item I<bidi.par> | ||
| 74 | |||
| 75 | Boolean, if true work in paragraph mode: the paragraph direction is not reset | ||
| 76 | on each line, but only on lines that start or end a paragraph | ||
| 77 | |||
| 78 | =cut | ||
| 79 | |||
| 80 | #:META:RESOURCE:%.ParReset:string:Start a new paragraph where this matches | ||
| 81 | |||
| 82 | =item I<bidi.ParReset> | ||
| 83 | |||
| 84 | A regular expression. A line matching this will start a new paragraph. | ||
| 85 | |||
| 86 | =cut | ||
| 87 | |||
| 88 | #:META:RESOURCE:%.ParResetAfter:string:Start a new paragraph after this matches | ||
| 89 | |||
| 90 | =item I<bidi.ParResetAfter> | ||
| 91 | |||
| 92 | Similar to I<bidi.ParReset>, but the paragraph starts after the matching | ||
| 93 | line. | ||
| 94 | |||
| 95 | =back | ||
| 96 | |||
| 97 | For example, I use urxvt with this plugin to edit LaTeX documents in Hebrew. | ||
| 98 | I have the following resources defined: | ||
| 99 | |||
| 100 | URxvt.bidi.LRE: \\\\[([] | ||
| 101 | URxvt.bidi.PDF: \\\\[)\\]] | ||
| 102 | urxvt-vimh.bidi.par: true | ||
| 103 | urxvt-vimh.cursorUnderline: true | ||
| 104 | urxvt-vimh.bidi.ParReset: ^\\s*$|^\\\\begin|^\\\\\\[ | ||
| 105 | urxvt-vimh.bidi.ParResetAfter: ^\\\\end|\\\\\\]$ | ||
| 106 | |||
| 107 | This causes inline equations (delimited by C<\(> and C<\)>) be displayed | ||
| 108 | correctly, and equation environments be considered a paragraph (so that it is | ||
| 109 | displayed on the left). | ||
| 110 | |||
| 111 | =cut | ||
| 112 | |||
| 113 | 1 | ▶ | use 5.10.0; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 114 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 115 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 116 | 0 | ▶ | use Text::Bidi qw(log2vis is_bidi); |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 117 | use Text::Bidi::Constants; | ||
| 118 | use Text::Bidi::Selection; | ||
| 119 | use Encode qw(decode_utf8); | ||
| 120 | |||
| 121 | sub on_start { | ||
| 122 | my ($self) = @_; | ||
| 123 | $self->{'split'} = decode_utf8($self->x_resource('%.FieldSep')) // | ||
| 124 | "\x{2502}"; | ||
| 125 | my $lre = decode_utf8($self->x_resource('%.LRE')) // ''; | ||
| 126 | my $pdf = decode_utf8($self->x_resource('%.PDF')) // ''; | ||
| 127 | my $preset = decode_utf8($self->x_resource('%.ParReset')) // '^\s*$'; | ||
| 128 | my $preseta = decode_utf8($self->x_resource('%.ParResetAfter')) // '(?!)'; | ||
| 129 | $self->{'par'} = decode_utf8($self->x_resource('%.par')) // ''; | ||
| 130 | $self->{'lre'} = qr/.*?($lre)/ if $lre; | ||
| 131 | $self->{'pdf'} = qr/.*?$pdf/ if $pdf; | ||
| 132 | $self->{'pres'} = qr/$preset/; | ||
| 133 | $self->{'presa'} = qr/$preseta/; | ||
| 134 | #warn "LRE: $self->{'lre'}\n"; | ||
| 135 | #warn "PDF: $self->{'pdf'}\n"; | ||
| 136 | #warn "$self->{'lre'}, $self->{'llre'}, $self->{'pdf'}, | ||
| 137 | #$self->{'lpdf'}\n"; | ||
| 138 | $self->{'spre'} = qr/\Q$self->{'split'}\E/; | ||
| 139 | $self->{'ls'} = length($self->{'split'}); | ||
| 140 | } | ||
| 141 | |||
| 142 | # A layout maps each displayed terminal cell to its original logical cells. | ||
| 143 | # Keep this separate from the temporary screen edits made by the refresh hooks. | ||
| 144 | sub _layout { | ||
| 145 | my ($self, $row, $pdir) = @_; | ||
| 146 | my $source = $self->ROW_t($row); | ||
| 147 | return unless defined $source; | ||
| 148 | my @source = split //, $source; | ||
| 149 | my @rend = @{$self->ROW_r($row)}; | ||
| 150 | my $layout = { | ||
| 151 | source => $source, | ||
| 152 | length => $self->ROW_l($row), | ||
| 153 | screen => $self->current_screen, | ||
| 154 | ncol => $self->ncol, | ||
| 155 | }; | ||
| 156 | my $text = $self->special_decode($source); | ||
| 157 | $pdir = undef if $text =~ $self->{'pres'}; | ||
| 158 | unless (is_bidi($text) || (defined($pdir) && $pdir == $Text::Bidi::Par::RTL)) { | ||
| 159 | $layout->{visual} = $source; | ||
| 160 | $layout->{rend} = \@rend; | ||
| 161 | $layout->{map} = [map { [$_] } 0..$#source]; | ||
| 162 | $layout->{dir} = $text =~ $self->{'presa'} ? undef : $pdir; | ||
| 163 | return $layout; | ||
| 164 | } | ||
| 165 | $layout->{bidi} = 1; | ||
| 166 | |||
| 167 | # Decoding a composite can produce several Unicode characters; decoding a | ||
| 168 | # wide-character continuation produces none. Track the original cell for | ||
| 169 | # every decoded character instead of treating character offsets as columns. | ||
| 170 | my (@owner, @r); | ||
| 171 | for my $cell (0..$#source) { | ||
| 172 | my $decoded = $self->special_decode($source[$cell]); | ||
| 173 | push @owner, ($cell) x length($decoded); | ||
| 174 | push @r, ($rend[$cell]) x length($decoded); | ||
| 175 | } | ||
| 176 | if ($self->{'lre'}) { | ||
| 177 | while ($text =~ /\G$self->{'lre'}/gc) { | ||
| 178 | my $pos = pos($text); | ||
| 179 | my $at = $pos - length($1); | ||
| 180 | substr($text, $at, 0) = "\x{202a}"; | ||
| 181 | splice @owner, $at, 0, undef; | ||
| 182 | splice @r, $at, 0, 0; | ||
| 183 | pos($text) = $pos + 1; | ||
| 184 | } | ||
| 185 | pos($text) = undef; | ||
| 186 | while ($text =~ /\G.*?$self->{'pdf'}/gc) { | ||
| 187 | my $at = pos($text); | ||
| 188 | substr($text, $at, 0) = "\x{202c}"; | ||
| 189 | splice @owner, $at, 0, undef; | ||
| 190 | splice @r, $at, 0, 0; | ||
| 191 | pos($text) = $at + 1; | ||
| 192 | } | ||
| 193 | } | ||
| 194 | |||
| 195 | my @parts = length($self->{'split'}) ? split($self->{'spre'}, $text, -1) : ($text); | ||
| 196 | my (@visual, @map); | ||
| 197 | my $off = 0; | ||
| 198 | for my $part (@parts) { | ||
| 199 | if (@visual) { | ||
| 200 | push @visual, $self->{'split'}; | ||
| 201 | push @map, $off..$off + $self->{'ls'} - 1; | ||
| 202 | $off += $self->{'ls'}; | ||
| 203 | } | ||
| 204 | my ($par, $visual) = log2vis($part, undef, $pdir); | ||
| 205 | push @visual, $visual; | ||
| 206 | push @map, map { $_ + $off } @{$par->map}; | ||
| 207 | $off += length($part); | ||
| 208 | $pdir = $par->dir if $self->{'par'} && !defined($pdir); | ||
| 209 | } | ||
| 210 | my $visual = join '', @visual; | ||
| 211 | my (@cells, @newr, @pending); | ||
| 212 | my $encoded = ''; | ||
| 213 | # Encode grapheme clusters together so combining marks stay with their base. | ||
| 214 | # Prefixing a space also makes isolated zero-width format characters safe | ||
| 215 | # to encode. Such characters share the preceding displayed cell. | ||
| 216 | while ($visual =~ /(\X)/g) { | ||
| 217 | my $cluster = $1; | ||
| 218 | my $at = $-[1]; | ||
| 219 | my %owners; | ||
| 220 | for my $index (@map[$at..$at + length($cluster) - 1]) { | ||
| 221 | $owners{$owner[$index]} = 1 if defined $owner[$index]; | ||
| 222 | } | ||
| 223 | $cluster =~ s/[\x{200b}-\x{200f}\x{202a}-\x{202f}\x{2066}-\x{2069}\x{feff}]//g; | ||
| 224 | my $part = substr($self->special_encode(' ' . $cluster), 1); | ||
| 225 | my @owners = sort { $a <=> $b } keys %owners; | ||
| 226 | unless (length($part)) { | ||
| 227 | if (@cells) { | ||
| 228 | push @{$cells[-1]}, @owners; | ||
| 229 | } else { | ||
| 230 | push @pending, @owners; | ||
| 231 | } | ||
| 232 | next; | ||
| 233 | } | ||
| 234 | unshift @owners, splice @pending; | ||
| 235 | my $rend = $r[$map[$at]] // 0; | ||
| 236 | push @cells, map { [@owners] } 1..length($part); | ||
| 237 | push @newr, ($rend) x length($part); | ||
| 238 | $encoded .= $part; | ||
| 239 | } | ||
| 240 | $layout->{visual} = $encoded; | ||
| 241 | my $padding = $self->ncol - length($encoded); | ||
| 242 | if ($padding > 0) { | ||
| 243 | $layout->{visual} .= ' ' x $padding; | ||
| 244 | push @cells, map { [] } 1..$padding; | ||
| 245 | push @newr, ($rend[-1] // 0) x $padding; | ||
| 246 | } | ||
| 247 | $layout->{rend} = \@newr; | ||
| 248 | $layout->{map} = \@cells; | ||
| 249 | $layout->{dir} = $text =~ $self->{'presa'} ? undef : $pdir; | ||
| 250 | return $layout; | ||
| 251 | } | ||
| 252 | |||
| 253 | sub _native_selection { | ||
| 254 | my ($self) = @_; | ||
| 255 | return if $self->selection_screen != $self->current_screen; | ||
| 256 | my $top = $self->view_start; | ||
| 257 | for my $row ($top..$top + $self->nrow - 1) { | ||
| 258 | my $rend = $self->ROW_r($row) // next; | ||
| 259 | next unless grep { $_ & urxvt::RS_Sel() } @$rend; | ||
| 260 | return { | ||
| 261 | beg => [$self->selection_beg], end => [$self->selection_end], | ||
| 262 | rectangular => $self->{'rectangular'}, | ||
| 263 | }; | ||
| 264 | } | ||
| 265 | return; | ||
| 266 | } | ||
| 267 | |||
| 268 | sub _xor_selection { | ||
| 269 | my ($self, $selection) = @_; | ||
| 270 | return unless $selection; | ||
| 271 | my @span = (@{$selection->{beg}}, @{$selection->{end}}); | ||
| 272 | my $style = urxvt::RS_Sel() | urxvt::RS_RVid(); | ||
| 273 | if ($selection->{rectangular}) { | ||
| 274 | $self->scr_xor_rect(@span, $style, $style | urxvt::RS_Uline()); | ||
| 275 | } else { | ||
| 276 | $self->scr_xor_span(@span, $style); | ||
| 277 | } | ||
| 278 | } | ||
| 279 | |||
| 280 | sub on_refresh_begin { | ||
| 281 | my ($self) = @_; | ||
| 282 | $self->_restore_selection; | ||
| 283 | # urxvt applies its selection rendition before REFRESH_BEGIN. Remove it | ||
| 284 | # while reordering characters and their styles, then apply it at the | ||
| 285 | # original visual columns so the highlight does not follow the text map. | ||
| 286 | my $selection = $self->{'refresh_selection'} = $self->_native_selection; | ||
| 287 | $self->_xor_selection($selection); | ||
| 288 | my ($crow, $ccol) = $self->screen_cur; | ||
| 289 | my $pdir; | ||
| 290 | my $top = $self->view_start; | ||
| 291 | $self->{'saved_rows'} = {}; | ||
| 292 | $self->{'layouts'} = {}; | ||
| 293 | for my $row ($top..$top + $self->nrow - 1) { | ||
| 294 | my $layout = $self->_layout($row, $pdir); | ||
| 295 | next unless $layout; | ||
| 296 | $pdir = $layout->{dir}; | ||
| 297 | $self->{'layouts'}{$row} = $layout; | ||
| 298 | next unless $layout->{bidi}; | ||
| 299 | my @rend = @{$layout->{rend}}; | ||
| 300 | if ($row == $crow) { | ||
| 301 | for my $col (0..$#{$layout->{map}}) { | ||
| 302 | $rend[$col] |= urxvt::RS_RVid() | ||
| 303 | if $col != $ccol && grep { $_ == $ccol } @{$layout->{map}[$col]}; | ||
| 304 | } | ||
| 305 | } | ||
| 306 | $self->{'saved_rows'}{$row} = [ | ||
| 307 | $self->ROW_t($row, $layout->{visual}), | ||
| 308 | $self->ROW_r($row, \@rend), | ||
| 309 | ]; | ||
| 310 | } | ||
| 311 | $self->_xor_selection($selection); | ||
| 312 | () | ||
| 313 | } | ||
| 314 | |||
| 315 | sub on_refresh_end { | ||
| 316 | my ($self) = @_; | ||
| 317 | my $selection = delete $self->{'refresh_selection'}; | ||
| 318 | $self->_xor_selection($selection); | ||
| 319 | my $saved = delete $self->{'saved_rows'} // {}; | ||
| 320 | for my $row (keys %$saved) { | ||
| 321 | $self->ROW_t($row, $saved->{$row}[0]); | ||
| 322 | $self->ROW_r($row, $saved->{$row}[1]); | ||
| 323 | } | ||
| 324 | # Leave the logical buffer with urxvt's original selection rendition; | ||
| 325 | # the terminal removes that rendition after REFRESH_END returns. | ||
| 326 | $self->_xor_selection($selection); | ||
| 327 | () | ||
| 328 | } | ||
| 329 | |||
| 330 | sub _selection_layout { | ||
| 331 | my ($self, $row, $pdir) = @_; | ||
| 332 | my $layout = $self->{'layouts'}{$row}; | ||
| 333 | my $source = $self->ROW_t($row); | ||
| 334 | return unless defined $source; | ||
| 335 | return $layout if $layout && $layout->{source} eq $source | ||
| 336 | && $layout->{length} == $self->ROW_l($row) | ||
| 337 | && $layout->{ncol} == $self->ncol | ||
| 338 | && $layout->{screen} == $self->current_screen; | ||
| 339 | return $self->{'layouts'}{$row} = $self->_layout($row, $pdir); | ||
| 340 | } | ||
| 341 | |||
| 342 | sub _selection_for_layout { | ||
| 343 | my ($self, $layout) = @_; | ||
| 344 | return $layout->{selection} if $layout->{selection}; | ||
| 345 | # urxvt maps cells, including private composite characters and padding. | ||
| 346 | # Translate that encoding into the public module's Unicode offsets. | ||
| 347 | my (@cell_to_logical, $logical); | ||
| 348 | $logical = ''; | ||
| 349 | for my $cell (split //, substr($layout->{source}, 0, $layout->{length})) { | ||
| 350 | my $decoded = $self->special_decode($cell); | ||
| 351 | my $offset = length($logical); | ||
| 352 | push @cell_to_logical, [$offset..$offset + length($decoded) - 1]; | ||
| 353 | $logical .= $decoded; | ||
| 354 | } | ||
| 355 | my @map = map { | ||
| 356 | [map { @{$cell_to_logical[$_] // []} } @$_] | ||
| 357 | } @{$layout->{map}}; | ||
| 358 | return $layout->{selection} = Text::Bidi::Selection->new( | ||
| 359 | text => $logical, visual_to_logical => \@map, | ||
| 360 | ); | ||
| 361 | } | ||
| 362 | |||
| 363 | sub _mouse_reporting { | ||
| 364 | my ($self, $event) = @_; | ||
| 365 | return ($self->priv_modes & urxvt::PrivMode_mouse_report()) | ||
| 366 | && !($event->{state} & ($self->ModMetaMask | urxvt::ShiftMask())); | ||
| 367 | } | ||
| 368 | |||
| 369 | sub _mouse_position { | ||
| 370 | my ($self, $event) = @_; | ||
| 371 | my $row = $event->{row}; | ||
| 372 | $row = $self->view_start if $row < $self->view_start; | ||
| 373 | my $bottom = $self->view_start + $self->nrow - 1; | ||
| 374 | $row = $bottom if $row > $bottom; | ||
| 375 | my $col = $event->{col}; | ||
| 376 | $col = 0 if $col < 0; | ||
| 377 | $col = $self->ncol if $col > $self->ncol; | ||
| 378 | my $layout = $self->_selection_layout($row); | ||
| 379 | --$col while $col > 0 && $layout | ||
| 380 | && substr($layout->{visual}, $col, 1) eq "\x{ffff}"; | ||
| 381 | return [$row, $col]; | ||
| 382 | } | ||
| 383 | |||
| 384 | sub _set_selection { | ||
| 385 | my ($self, $beg, $end) = @_; | ||
| 386 | if ($beg->[0] == $end->[0] && $beg->[1] == 0 && $end->[1] == 0) { | ||
| 387 | # selection_end(row, 0) means the end of the previous row in urxvt. | ||
| 388 | # An empty range at column one has the same (empty) highlight. | ||
| 389 | ($beg, $end) = ([$beg->[0], 1], [$end->[0], 1]); | ||
| 390 | } | ||
| 391 | my %saved; | ||
| 392 | for my $row ($beg->[0], $end->[0]) { | ||
| 393 | next if exists $saved{$row}; | ||
| 394 | my $layout = $self->_selection_layout($row) or return; | ||
| 395 | $saved{$row} = [$self->ROW_t($row), $self->ROW_l($row)]; | ||
| 396 | $self->ROW_t($row, $layout->{visual}); | ||
| 397 | $self->ROW_l($row, $self->ncol); | ||
| 398 | } | ||
| 399 | $self->selection_beg(@$beg); | ||
| 400 | $self->selection_end(@$end); | ||
| 401 | $self->selection_mark(@{$self->{'mouse_anchor'}}) if $self->{'mouse_anchor'}; | ||
| 402 | for my $row (keys %saved) { | ||
| 403 | $self->ROW_t($row, $saved{$row}[0]); | ||
| 404 | $self->ROW_l($row, $saved{$row}[1]); | ||
| 405 | } | ||
| 406 | $self->want_refresh; | ||
| 407 | } | ||
| 408 | |||
| 409 | sub _word_span { | ||
| 410 | my ($self, $position) = @_; | ||
| 411 | my ($row, $col) = @$position; | ||
| 412 | $col = $self->ncol - 1 if $col == $self->ncol; | ||
| 413 | my $layout = $self->_selection_layout($row); | ||
| 414 | my @cells = split //, $layout->{visual}; | ||
| 415 | my $cut = $self->resource('cutchars') // q{"&()*,;<=>?@[\]^`{|}}; | ||
| 416 | my $class = sub { | ||
| 417 | my $text = $self->special_decode($_[0]); | ||
| 418 | return 'space' if $text =~ /^\s/; | ||
| 419 | return 'cut' if length($text) && index($cut, substr($text, 0, 1)) >= 0; | ||
| 420 | return 'word'; | ||
| 421 | }; | ||
| 422 | --$col while $col > 0 && $cells[$col] eq "\x{ffff}"; | ||
| 423 | my $kind = $class->($cells[$col]); | ||
| 424 | my ($from, $to) = ($col, $col + 1); | ||
| 425 | --$from while $from > 0 && ($cells[$from - 1] eq "\x{ffff}" | ||
| 426 | || $class->($cells[$from - 1]) eq $kind); | ||
| 427 | ++$to while $to < @cells && ($cells[$to] eq "\x{ffff}" | ||
| 428 | || $class->($cells[$to]) eq $kind); | ||
| 429 | my ($beg, $end) = ([$row, $from], [$row, $to]); | ||
| 430 | while ($beg->[1] == 0 && $beg->[0] > $self->top_row | ||
| 431 | && $self->ROW_is_longer($beg->[0] - 1)) { | ||
| 432 | my $previous = $self->_selection_layout($beg->[0] - 1) or last; | ||
| 433 | my @previous = split //, $previous->{visual}; | ||
| 434 | my $col = @previous; | ||
| 435 | --$col while $col > 0 && ($previous[$col - 1] eq "\x{ffff}" | ||
| 436 | || $class->($previous[$col - 1]) eq $kind); | ||
| 437 | last if $col == @previous; | ||
| 438 | $beg = [$beg->[0] - 1, $col]; | ||
| 439 | } | ||
| 440 | while ($end->[1] == $self->ncol && $end->[0] < $self->nrow - 1 | ||
| 441 | && $self->ROW_is_longer($end->[0])) { | ||
| 442 | my $next = $self->_selection_layout($end->[0] + 1) or last; | ||
| 443 | my @next = split //, $next->{visual}; | ||
| 444 | my $col = 0; | ||
| 445 | ++$col while $col < @next && ($next[$col] eq "\x{ffff}" | ||
| 446 | || $class->($next[$col]) eq $kind); | ||
| 447 | last unless $col; | ||
| 448 | $end = [$end->[0] + 1, $col]; | ||
| 449 | } | ||
| 450 | return ($beg, $end); | ||
| 451 | } | ||
| 452 | |||
| 453 | sub _extend_selection { | ||
| 454 | my ($self, $position) = @_; | ||
| 455 | my $anchor = $self->{'mouse_anchor'}; | ||
| 456 | my ($beg, $end) = ($anchor, $position); | ||
| 457 | ($beg, $end) = ($end, $beg) if $beg->[0] > $end->[0] | ||
| 458 | || ($beg->[0] == $end->[0] && $beg->[1] > $end->[1]); | ||
| 459 | if ($self->{'rectangular'}) { | ||
| 460 | ($beg, $end) = ([@$beg], [@$end]); | ||
| 461 | ($beg->[1], $end->[1]) = ($end->[1], $beg->[1]) if $beg->[1] > $end->[1]; | ||
| 462 | } elsif ($self->{'mouse_clicks'} == 2) { | ||
| 463 | ($beg) = $self->_word_span($beg); | ||
| 464 | (undef, $end) = $self->_word_span($end); | ||
| 465 | } elsif ($self->{'mouse_clicks'} == 3) { | ||
| 466 | ($beg, $end) = ([$beg->[0], 0], [$end->[0], $self->ncol]); | ||
| 467 | --$beg->[0] while $beg->[0] > $self->top_row && $self->ROW_is_longer($beg->[0] - 1); | ||
| 468 | ++$end->[0] while $end->[0] < $self->nrow - 1 && $self->ROW_is_longer($end->[0]); | ||
| 469 | } | ||
| 470 | $self->_set_selection($beg, $end); | ||
| 471 | } | ||
| 472 | |||
| 473 | sub on_button_press { | ||
| 474 | my ($self, $event) = @_; | ||
| 475 | return if $self->_mouse_reporting($event); | ||
| 476 | return unless $event->{button} == 1 || $event->{button} == 3; | ||
| 477 | # Keep plain-text terminals on urxvt's native selection path. When any | ||
| 478 | # displayed row is bidi, a drag starting in an LTR row can still cross it. | ||
| 479 | my $bidi; | ||
| 480 | my $pdir; | ||
| 481 | for my $row ($self->view_start..$self->view_start + $self->nrow - 1) { | ||
| 482 | my $layout = $self->_selection_layout($row, $pdir) or next; | ||
| 483 | $pdir = $layout->{dir}; | ||
| 484 | $bidi ||= $layout->{bidi}; | ||
| 485 | } | ||
| 486 | unless ($bidi) { | ||
| 487 | delete $self->{'mouse_last'}; | ||
| 488 | return; | ||
| 489 | } | ||
| 490 | my $position = $self->_mouse_position($event); | ||
| 491 | if ($event->{button} == 3 || (($event->{state} & urxvt::ShiftMask()) | ||
| 492 | && !($self->priv_modes & urxvt::PrivMode_mouse_report()))) { | ||
| 493 | my @beg = $self->selection_beg; | ||
| 494 | my @end = $self->selection_end; | ||
| 495 | my $point = $position->[0] * $self->ncol + $position->[1]; | ||
| 496 | my $left = $beg[0] * $self->ncol + $beg[1]; | ||
| 497 | my $right = $end[0] * $self->ncol + $end[1]; | ||
| 498 | $self->{'mouse_anchor'} = abs($point - $left) < abs($point - $right) ? \@end : \@beg; | ||
| 499 | $self->{'mouse_clicks'} = 1; | ||
| 500 | } else { | ||
| 501 | my $last = $self->{'mouse_last'}; | ||
| 502 | my $interval = $self->resource('multiClickTime') // 500; | ||
| 503 | my $repeat = $last && $last->{screen} == $self->current_screen | ||
| 504 | && $last->{row} == $position->[0] && $last->{col} == $position->[1] | ||
| 505 | && $event->{time} - $last->{time} >= 0 | ||
| 506 | && $event->{time} - $last->{time} < $interval; | ||
| 507 | $self->{'mouse_clicks'} = $repeat ? $self->{'mouse_clicks'} % 3 + 1 : 1; | ||
| 508 | $self->{'mouse_last'} = { row => $position->[0], col => $position->[1], | ||
| 509 | time => $event->{time}, screen => $self->current_screen }; | ||
| 510 | $self->{'mouse_anchor'} = $position; | ||
| 511 | $self->{'rectangular'} = !!($event->{state} & $self->ModMetaMask); | ||
| 512 | } | ||
| 513 | $self->{'mouse_button'} = $event->{button}; | ||
| 514 | { | ||
| 515 | # The public API also activates urxvt's selection highlight. Abort its | ||
| 516 | # copying hook here; merely setting coordinates does not activate it. | ||
| 517 | local $self->{'starting_selection'} = 1; | ||
| 518 | $self->selection_make($event->{time}, !!$self->{'rectangular'}); | ||
| 519 | } | ||
| 520 | $self->_extend_selection($position); | ||
| 521 | return 1; | ||
| 522 | } | ||
| 523 | |||
| 524 | sub on_motion_notify { | ||
| 525 | my ($self, $event) = @_; | ||
| 526 | return unless $self->{'mouse_button'}; | ||
| 527 | $self->_extend_selection($self->_mouse_position($event)); | ||
| 528 | my $top = $self->view_start; | ||
| 529 | my $direction = $event->{row} < $top ? -1 | ||
| 530 | : $event->{row} >= $top + $self->nrow ? 1 : 0; | ||
| 531 | if ($direction) { | ||
| 532 | $self->{'mouse_scroll'} = [$direction, $event->{col}]; | ||
| 533 | $self->{'mouse_scroll_timer'} //= urxvt::timer->new | ||
| 534 | ->cb(sub { $self->_scroll_selection }) | ||
| 535 | ->after(0.1, 0.05); | ||
| 536 | } else { | ||
| 537 | $self->_stop_selection_scroll; | ||
| 538 | } | ||
| 539 | return 1; | ||
| 540 | } | ||
| 541 | |||
| 542 | sub _stop_selection_scroll { | ||
| 543 | my ($self) = @_; | ||
| 544 | my $timer = delete $self->{'mouse_scroll_timer'}; | ||
| 545 | $timer->stop if $timer; | ||
| 546 | delete $self->{'mouse_scroll'}; | ||
| 547 | } | ||
| 548 | |||
| 549 | sub _scroll_selection { | ||
| 550 | my ($self) = @_; | ||
| 551 | my ($direction, $col) = @{$self->{'mouse_scroll'}}; | ||
| 552 | my $top = $self->view_start + $direction; | ||
| 553 | $top = $self->top_row if $top < $self->top_row; | ||
| 554 | $top = 0 if $top > 0; | ||
| 555 | return $self->_stop_selection_scroll if $top == $self->view_start; | ||
| 556 | $self->view_start($top); | ||
| 557 | $self->{'layouts'} = {}; | ||
| 558 | $self->_extend_selection($self->_mouse_position({ | ||
| 559 | row => $direction < 0 ? $top : $top + $self->nrow - 1, | ||
| 560 | col => $col, | ||
| 561 | })); | ||
| 562 | } | ||
| 563 | |||
| 564 | sub on_button_release { | ||
| 565 | my ($self, $event) = @_; | ||
| 566 | return unless $self->{'mouse_button'} && $event->{button} == $self->{'mouse_button'}; | ||
| 567 | delete $self->{'mouse_button'}; | ||
| 568 | $self->_stop_selection_scroll; | ||
| 569 | $self->_extend_selection($self->_mouse_position($event)); | ||
| 570 | local $self->{'making_selection'} = 1; | ||
| 571 | $self->selection_make($event->{time}, !!$self->{'rectangular'}); | ||
| 572 | # Another extension can abort selection making, and urxvt does not call | ||
| 573 | # sel_grab for an empty result. Restore on either path before returning. | ||
| 574 | $self->_restore_selection; | ||
| 575 | return 1; | ||
| 576 | } | ||
| 577 | |||
| 578 | sub on_reset { | ||
| 579 | my ($self) = @_; | ||
| 580 | $self->_stop_selection_scroll; | ||
| 581 | delete $self->{'mouse_button'}; | ||
| 582 | delete $self->{'mouse_last'}; | ||
| 583 | delete $self->{'layouts'}; | ||
| 584 | () | ||
| 585 | } | ||
| 586 | |||
| 587 | sub on_destroy { | ||
| 588 | my ($self) = @_; | ||
| 589 | $self->_stop_selection_scroll; | ||
| 590 | () | ||
| 591 | } | ||
| 592 | |||
| 593 | sub _restore_selection { | ||
| 594 | my ($self) = @_; | ||
| 595 | my $saved = delete $self->{'selection_rows'} or return; | ||
| 596 | # The end-coordinate setter clamps columns beyond ROW_l to ncol. Restore | ||
| 597 | # visual coordinates while the temporary row is still padded to full width. | ||
| 598 | $self->ROW_l($saved->{end}[0], $self->ncol); | ||
| 599 | $self->selection_beg(@{$saved->{beg}}); | ||
| 600 | $self->selection_end(@{$saved->{end}}); | ||
| 601 | for my $row (keys %{$saved->{rows}}) { | ||
| 602 | my ($text, $rend, $length) = @{$saved->{rows}{$row}}; | ||
| 603 | $self->ROW_t($row, $text); | ||
| 604 | $self->ROW_r($row, $rend); | ||
| 605 | $self->ROW_l($row, $length); | ||
| 606 | } | ||
| 607 | } | ||
| 608 | |||
| 609 | sub on_sel_make { | ||
| 610 | my ($self) = @_; | ||
| 611 | return 1 if $self->{'starting_selection'}; | ||
| 612 | return unless $self->{'making_selection'}; | ||
| 613 | my @beg = $self->selection_beg; | ||
| 614 | my @end = $self->selection_end; | ||
| 615 | return if $self->selection_screen != $self->current_screen; | ||
| 616 | my $rect = $self->{'rectangular'}; | ||
| 617 | my (@rows, $has_bidi, $has_text, $pdir, $last_from); | ||
| 618 | for my $row ($beg[0]..$end[0]) { | ||
| 619 | # A selection can be made before the next repaint or after scrolling. | ||
| 620 | my $layout = $self->_selection_layout($row, $pdir) or return; | ||
| 621 | $pdir = $layout->{dir}; | ||
| 622 | $has_bidi ||= $layout->{bidi}; | ||
| 623 | my $from = $rect || $row == $beg[0] ? $beg[1] : 0; | ||
| 624 | my $to = $rect || $row == $end[0] ? $end[1] : $self->ncol; | ||
| 625 | $from = 0 if $from < 0; | ||
| 626 | $to = $self->ncol if $to > $self->ncol; | ||
| 627 | $last_from = $from; | ||
| 628 | my $selection = $self->_selection_for_layout($layout); | ||
| 629 | my $text = $self->special_encode($selection->selected_text($from, $to)); | ||
| 630 | $has_text ||= length($text); | ||
| 631 | push @rows, [$row, $text]; | ||
| 632 | } | ||
| 633 | return unless $has_bidi; | ||
| 634 | shift @rows if !$rect && $beg[1] == $self->ncol && @rows > 1; | ||
| 635 | # As in urxvt, an empty selection leaves the previous selection alone. | ||
| 636 | return 1 unless $has_text || @rows > 1 || $rect | ||
| 637 | || ($end[1] == $self->ncol && $beg[1] < $self->ncol); | ||
| 638 | |||
| 639 | # Let urxvt itself copy these logical fragments, join wrapped lines, and | ||
| 640 | # run every sel_grab hook (including selection-to-clipboard). Coordinates | ||
| 641 | # and buffers are restored synchronously by on_sel_grab, before repaint. | ||
| 642 | my $saved = $self->{'selection_rows'} = { rows => {}, beg => \@beg, end => \@end }; | ||
| 643 | my $last_length = 0; | ||
| 644 | for my $entry (@rows) { | ||
| 645 | my ($row, $text) = @$entry; | ||
| 646 | $saved->{rows}{$row} = [ | ||
| 647 | $self->ROW_t($row), [@{$self->ROW_r($row)}], $self->ROW_l($row), | ||
| 648 | ]; | ||
| 649 | $last_length = length($text); | ||
| 650 | $self->ROW_t($row, $text . ' ' x ($self->ncol - $last_length)); | ||
| 651 | $self->ROW_l($row, $last_length); | ||
| 652 | } | ||
| 653 | $self->selection_beg($rows[0][0], 0); | ||
| 654 | # Preserve the line break when selecting an entire short terminal row. | ||
| 655 | my $last = $rows[-1][0]; | ||
| 656 | my $end_col = $rect || ($last_from == 0 && $end[1] == $self->ncol | ||
| 657 | && $saved->{rows}{$last}[2] < $self->ncol) ? $self->ncol : $last_length; | ||
| 658 | $self->selection_end($last, $end_col); | ||
| 659 | () | ||
| 660 | } | ||
| 661 | |||
| 662 | sub on_sel_grab { | ||
| 663 | my ($self) = @_; | ||
| 664 | $self->_restore_selection; | ||
| 665 | () | ||
| 666 | } | ||
| 667 | |||
| 668 | 1; | ||