| 1 | package Term::Fabulous::Editor; | ||
| 2 | |||
| 3 | 3 | ▶ | use v5.32; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 3 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 3 | ▶ | use feature 'signatures'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 3 | ▶ | no warnings 'experimental::signatures'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | |||
| 8 | our $VERSION = '0.05'; | ||
| 9 | |||
| 10 | 3 | ▶ | use Object::Pad 0.825; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | |||
| 12 | class Term::Fabulous::Editor :strict(params) { | ||
| 13 | 3 | ▶ | use Feature::Compat::Try; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 14 | 3 | ▶ | use List::Util qw(sum0 uniq); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 15 | 0 | ▶ | use Term::Fabulous::Check qw(describe); |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 16 | use Term::Fabulous::Unicode qw(grapheme_clusters); | ||
| 17 | |||
| 18 | use constant UNDO_LIMIT => 100; | ||
| 19 | use constant BOUNDARY_CACHE_LIMIT => 4096; | ||
| 20 | |||
| 21 | # Grapheme cluster boundaries (character offsets) of a line, shared by | ||
| 22 | # every editor. Lines are segmented as the renderer segments them. | ||
| 23 | my %boundaries_by_line; | ||
| 24 | |||
| 25 | # Shared by every editor of the process, like a desktop clipboard. | ||
| 26 | my $clipboard = ''; | ||
| 27 | |||
| 28 | field $multi_line :param :reader = 1; | ||
| 29 | field $max_length :param = undef; | ||
| 30 | |||
| 31 | # Which grapheme clusters may enter the text: the spec as given (a | ||
| 32 | # character class body, a regular expression or a code reference) and | ||
| 33 | # the predicate it means; both undef when every cluster may. | ||
| 34 | field $accept :param :reader = undef; | ||
| 35 | field $is_accepted; | ||
| 36 | |||
| 37 | field @lines = (''); | ||
| 38 | |||
| 39 | # Positions are [row, offset]: a line index and a character offset that | ||
| 40 | # is a grapheme cluster boundary of that line. The anchor is the other | ||
| 41 | # end of the selection; it is empty when nothing is selected. | ||
| 42 | field @cursor = ( 0, 0 ); | ||
| 43 | field @anchor; | ||
| 44 | |||
| 45 | # Count changes, so views can cache what they derive: of the text, and | ||
| 46 | # of the text, the cursor or the selection. | ||
| 47 | field $text_revision :reader = 0; | ||
| 48 | field $revision :reader = 0; | ||
| 49 | |||
| 50 | # Undo steps, oldest first: { changes, cursor, anchor, cursor_after }. | ||
| 51 | # A change { row, offset, old, new } says that the text $old at | ||
| 52 | # (row, offset) was replaced by $new; a step holds the changes of one | ||
| 53 | # edit, or of a run of typing, in the order they were made, with the | ||
| 54 | # cursor and anchor before the first and the cursor after the last. | ||
| 55 | field @undo_stack; | ||
| 56 | field @redo_stack; | ||
| 57 | |||
| 58 | # The changes the running edit has made so far. | ||
| 59 | field @changes; | ||
| 60 | |||
| 61 | # The kind of the last undo step when typing may extend it: 'word' or | ||
| 62 | # 'space'; undef when the next edit starts a new step. | ||
| 63 | field $typing_run; | ||
| 64 | |||
| 65 | ADJUST :params ( :$text = '' ) { | ||
| 66 | $self->set_max_length($max_length); | ||
| 67 | $self->set_accept($accept); | ||
| 68 | $self->set_text($text); | ||
| 69 | } | ||
| 70 | |||
| 71 | sub _boundaries ($line) { | ||
| 72 | my $boundaries = $boundaries_by_line{$line}; | ||
| 73 | return $boundaries if defined $boundaries; | ||
| 74 | |||
| 75 | %boundaries_by_line = () if keys(%boundaries_by_line) >= BOUNDARY_CACHE_LIMIT; | ||
| 76 | my @offsets = (0); | ||
| 77 | push @offsets, $offsets[-1] + length $_ foreach grapheme_clusters($line); | ||
| 78 | return $boundaries_by_line{$line} = \@offsets; | ||
| 79 | } | ||
| 80 | |||
| 81 | sub _compare ( $row_a, $offset_a, $row_b, $offset_b ) { | ||
| 82 | return $row_a <=> $row_b || $offset_a <=> $offset_b; | ||
| 83 | } | ||
| 84 | |||
| 85 | # --------------------------------------------------------------------- | ||
| 86 | # Text | ||
| 87 | # --------------------------------------------------------------------- | ||
| 88 | |||
| 89 | # Line breaks become "\n"; a single-line editor turns them into spaces. | ||
| 90 | method _normalize ($text) { | ||
| 91 | die "Term::Fabulous::Editor: text must be a string, got " . ( ref $text || 'undef' ) unless defined $text && !ref $text; | ||
| 92 | $text =~ s/\r\n?/\n/g; | ||
| 93 | $text =~ tr/\n/ / unless $multi_line; | ||
| 94 | return $text; | ||
| 95 | } | ||
| 96 | |||
| 97 | sub _cluster_count ($text) { | ||
| 98 | return sum0 map { scalar( @{ _boundaries($_) } ) - 1 } split /\n/, $text, -1; | ||
| 99 | } | ||
| 100 | |||
| 101 | # The predicate for a grapheme cluster that an accept spec means: a | ||
| 102 | # code reference as it is, a regular expression the cluster must match, | ||
| 103 | # a string as the body of a character class. Undef means no restriction. | ||
| 104 | sub _acceptance_of ($spec) { | ||
| 105 | return undef unless defined $spec; | ||
| 106 | return $spec if ref $spec eq 'CODE'; | ||
| 107 | return sub ($cluster) { $cluster =~ $spec } | ||
| 108 | if ref $spec eq 'Regexp'; | ||
| 109 | die "Term::Fabulous::Editor: accept must be a character class body, a regular expression, a code reference or undef, got " . describe($spec) if ref $spec; | ||
| 110 | |||
| 111 | my $class; | ||
| 112 | try { | ||
| 113 | $class = qr/\A[$spec]\z/; | ||
| 114 | } | ||
| 115 | catch ($error) { | ||
| 116 | die "Term::Fabulous::Editor: accept '$spec' is not the body of a character class: $error"; | ||
| 117 | } | ||
| 118 | return sub ($cluster) { $cluster =~ $class }; | ||
| 119 | } | ||
| 120 | |||
| 121 | # The distinct clusters of $text (line breaks aside) that the predicate | ||
| 122 | # rejects. | ||
| 123 | sub _rejected_by ( $is_accepted, $text ) { | ||
| 124 | return () unless defined $is_accepted; | ||
| 125 | return uniq grep { !$is_accepted->($_) } map { grapheme_clusters($_) } split /\n/, $text; | ||
| 126 | } | ||
| 127 | |||
| 128 | sub _die_for_rejected (@rejected) { | ||
| 129 | die 'Term::Fabulous::Editor: the text has characters that accept rejects: "' . join( '', @rejected ) . '"'; | ||
| 130 | } | ||
| 131 | |||
| 132 | method set_accept ($spec) { | ||
| 133 | my $is_accepted_now = _acceptance_of($spec); | ||
| 134 | my @rejected = _rejected_by( $is_accepted_now, $self->text ); | ||
| 135 | _die_for_rejected(@rejected) if @rejected; | ||
| 136 | ( $accept, $is_accepted ) = ( $spec, $is_accepted_now ); | ||
| 137 | return $self; | ||
| 138 | } | ||
| 139 | |||
| 140 | # $text without the clusters that accept rejects; line breaks stay. | ||
| 141 | method _accepted ($text) { | ||
| 142 | return $text unless defined $is_accepted; | ||
| 143 | return join "\n", map { $self->_accepted_line($_) } split /\n/, $text, -1; | ||
| 144 | } | ||
| 145 | |||
| 146 | method _accepted_line ($line) { | ||
| 147 | return join q{}, grep { $is_accepted->($_) } grapheme_clusters($line); | ||
| 148 | } | ||
| 149 | |||
| 150 | method text () { | ||
| 151 | return join "\n", @lines; | ||
| 152 | } | ||
| 153 | |||
| 154 | method set_text ($text) { | ||
| 155 | $text = $self->_normalize($text); | ||
| 156 | my $length = _cluster_count($text) + ( $text =~ tr/\n// ); | ||
| 157 | die "Term::Fabulous::Editor: the text has $length characters, more than max_length $max_length" | ||
| 158 | if defined $max_length && $length > $max_length; | ||
| 159 | my @rejected = _rejected_by( $is_accepted, $text ); | ||
| 160 | _die_for_rejected(@rejected) if @rejected; | ||
| 161 | |||
| 162 | @lines = split /\n/, $text, -1; | ||
| 163 | @lines = ('') unless @lines; | ||
| 164 | @cursor = ( $#lines, length $lines[-1] ); | ||
| 165 | @anchor = (); | ||
| 166 | @undo_stack = @redo_stack = (); | ||
| 167 | $typing_run = undef; | ||
| 168 | $text_revision++; | ||
| 169 | $revision++; | ||
| 170 | return $self; | ||
| 171 | } | ||
| 172 | |||
| 173 | method lines () { | ||
| 174 | return @lines; | ||
| 175 | } | ||
| 176 | |||
| 177 | method line ($row) { | ||
| 178 | return undef if $row < 0; | ||
| 179 | return $lines[$row]; | ||
| 180 | } | ||
| 181 | |||
| 182 | method line_count () { | ||
| 183 | return scalar @lines; | ||
| 184 | } | ||
| 185 | |||
| 186 | method boundaries ($row) { | ||
| 187 | return @{ _boundaries( $lines[$row] ) }; | ||
| 188 | } | ||
| 189 | |||
| 190 | # In grapheme clusters; every line break counts as one. | ||
| 191 | method character_count () { | ||
| 192 | return sum0( map { scalar( @{ _boundaries($_) } ) - 1 } @lines ) + $#lines; | ||
| 193 | } | ||
| 194 | |||
| 195 | method max_length () { | ||
| 196 | return $max_length; | ||
| 197 | } | ||
| 198 | |||
| 199 | # undef removes the limit; a limit below the current length dies. | ||
| 200 | method set_max_length ($limit) { | ||
| 201 | die "Term::Fabulous::Editor: max_length must be a non-negative integer or undef, got " . describe($limit) | ||
| 202 | if defined $limit && !( !ref $limit && $limit =~ /\A[0-9]+\z/ ); | ||
| 203 | die "Term::Fabulous::Editor: the text has " . $self->character_count . " characters, more than max_length $limit" | ||
| 204 | if defined $limit && $self->character_count > $limit; | ||
| 205 | $max_length = defined $limit ? $limit + 0 : undef; | ||
| 206 | return $self; | ||
| 207 | } | ||
| 208 | |||
| 209 | method is_empty () { | ||
| 210 | return @lines == 1 && $lines[0] eq ''; | ||
| 211 | } | ||
| 212 | |||
| 213 | # --------------------------------------------------------------------- | ||
| 214 | # Cursor and selection | ||
| 215 | # --------------------------------------------------------------------- | ||
| 216 | |||
| 217 | method cursor () { | ||
| 218 | return @cursor; | ||
| 219 | } | ||
| 220 | |||
| 221 | method has_selection () { | ||
| 222 | return @anchor && _compare( @anchor, @cursor ) != 0 ? 1 : 0; | ||
| 223 | } | ||
| 224 | |||
| 225 | method selection () { | ||
| 226 | return () unless $self->has_selection; | ||
| 227 | return _compare( @anchor, @cursor ) < 0 ? ( @anchor, @cursor ) : ( @cursor, @anchor ); | ||
| 228 | } | ||
| 229 | |||
| 230 | method selected_text () { | ||
| 231 | my ( $row_0, $offset_0, $row_1, $offset_1 ) = $self->selection; | ||
| 232 | return '' unless defined $row_0; | ||
| 233 | return $self->_text_between( $row_0, $offset_0, $row_1, $offset_1 ); | ||
| 234 | } | ||
| 235 | |||
| 236 | method _text_between ( $row_0, $offset_0, $row_1, $offset_1 ) { | ||
| 237 | return substr( $lines[$row_0], $offset_0, $offset_1 - $offset_0 ) if $row_0 == $row_1; | ||
| 238 | return join "\n", substr( $lines[$row_0], $offset_0 ), @lines[ $row_0 + 1 .. $row_1 - 1 ], substr( $lines[$row_1], 0, $offset_1 ); | ||
| 239 | } | ||
| 240 | |||
| 241 | # The boundary at or before (direction -1) or after (+1) an offset. | ||
| 242 | method _snap ( $row, $offset, $direction ) { | ||
| 243 | my $boundaries = _boundaries( $lines[$row] ); | ||
| 244 | return $boundaries->[-1] if $offset >= $boundaries->[-1]; | ||
| 245 | return 0 if $offset <= 0; | ||
| 246 | my $index = 0; | ||
| 247 | $index++ while $boundaries->[ $index + 1 ] <= $offset; | ||
| 248 | return $boundaries->[$index] if $boundaries->[$index] == $offset || $direction < 0; | ||
| 249 | return $boundaries->[ $index + 1 ]; | ||
| 250 | } | ||
| 251 | |||
| 252 | method _clamped_position ( $row, $offset ) { | ||
| 253 | die "Term::Fabulous::Editor: a position needs integer row and offset, got " . describe($row) . ', ' . describe($offset) | ||
| 254 | unless grep( { defined && !ref && /\A-?[0-9]+\z/ } $row, $offset ) == 2; | ||
| 255 | $row = 0 if $row < 0; | ||
| 256 | $row = $#lines if $row > $#lines; | ||
| 257 | return ( $row, $self->_snap( $row, $offset, -1 ) ); | ||
| 258 | } | ||
| 259 | |||
| 260 | # Moves the cursor; with $extend the selection grows from where the | ||
| 261 | # cursor was, otherwise it is cleared. | ||
| 262 | method move_to ( $row, $offset, $extend = 0 ) { | ||
| 263 | my @target = $self->_clamped_position( $row, $offset ); | ||
| 264 | if ($extend) { | ||
| 265 | @anchor = @cursor unless @anchor; | ||
| 266 | } | ||
| 267 | else { | ||
| 268 | @anchor = (); | ||
| 269 | } | ||
| 270 | @cursor = @target; | ||
| 271 | $typing_run = undef; | ||
| 272 | $revision++; | ||
| 273 | return $self; | ||
| 274 | } | ||
| 275 | |||
| 276 | method set_selection ( $anchor_row, $anchor_offset, $cursor_row, $cursor_offset ) { | ||
| 277 | my @from = $self->_clamped_position( $anchor_row, $anchor_offset ); | ||
| 278 | $self->move_to( $cursor_row, $cursor_offset ); | ||
| 279 | @anchor = @from; | ||
| 280 | $revision++; | ||
| 281 | return $self; | ||
| 282 | } | ||
| 283 | |||
| 284 | method select_all () { | ||
| 285 | return $self->set_selection( 0, 0, $#lines, length $lines[-1] ); | ||
| 286 | } | ||
| 287 | |||
| 288 | method clear_selection () { | ||
| 289 | @anchor = (); | ||
| 290 | $revision++; | ||
| 291 | return $self; | ||
| 292 | } | ||
| 293 | |||
| 294 | # The word (or, between words, the single cluster) starting at or | ||
| 295 | # covering a position. A cluster belongs to a word when its first | ||
| 296 | # character is a word character. | ||
| 297 | method select_word_at ( $row, $offset ) { | ||
| 298 | ( $row, $offset ) = $self->_clamped_position( $row, $offset ); | ||
| 299 | my $line = $lines[$row]; | ||
| 300 | my @boundaries = @{ _boundaries($line) }; | ||
| 301 | return $self->move_to( $row, $offset ) if @boundaries == 1; | ||
| 302 | |||
| 303 | my @is_word = map { substr( $line, $boundaries[$_], 1 ) =~ /\w/ ? 1 : 0 } 0 .. $#boundaries - 1; | ||
| 304 | my ($index) = grep { $boundaries[$_] <= $offset } reverse 0 .. $#is_word; # the last cluster at the end | ||
| 305 | |||
| 306 | my ( $first, $last ) = ( $index, $index ); | ||
| 307 | if ( $is_word[$index] ) { | ||
| 308 | $first-- while $first > 0 && $is_word[ $first - 1 ]; | ||
| 309 | $last++ while $last < $#is_word && $is_word[ $last + 1 ]; | ||
| 310 | } | ||
| 311 | return $self->set_selection( $row, $boundaries[$first], $row, $boundaries[ $last + 1 ] ); | ||
| 312 | } | ||
| 313 | |||
| 314 | method _position_left_of ( $row, $offset ) { | ||
| 315 | return ( $row - 1, length $lines[ $row - 1 ] ) if $offset == 0 && $row > 0; | ||
| 316 | return ( $row, $self->_snap( $row, $offset - 1, -1 ) ); | ||
| 317 | } | ||
| 318 | |||
| 319 | method _position_right_of ( $row, $offset ) { | ||
| 320 | return ( $row + 1, 0 ) if $offset == length( $lines[$row] ) && $row < $#lines; | ||
| 321 | return ( $row, $self->_snap( $row, $offset + 1, 1 ) ); | ||
| 322 | } | ||
| 323 | |||
| 324 | # Start of the word before the position, crossing to the previous line | ||
| 325 | # from the start of a line. | ||
| 326 | method _word_start_before ( $row, $offset ) { | ||
| 327 | return ( $row - 1, length $lines[ $row - 1 ] ) if $offset == 0 && $row > 0; | ||
| 328 | my $start = substr( $lines[$row], 0, $offset ) =~ /(\w+)\W*\z/ ? $-[1] : 0; | ||
| 329 | return ( $row, $self->_snap( $row, $start, -1 ) ); | ||
| 330 | } | ||
| 331 | |||
| 332 | # End of the word after the position, crossing to the next line from | ||
| 333 | # the end of a line. | ||
| 334 | method _word_end_after ( $row, $offset ) { | ||
| 335 | my $line = $lines[$row]; | ||
| 336 | return ( $row + 1, 0 ) if $offset == length($line) && $row < $#lines; | ||
| 337 | my $end = substr( $line, $offset ) =~ /\A\W*\w+/ ? $offset + $+[0] : length $line; | ||
| 338 | return ( $row, $self->_snap( $row, $end, 1 ) ); | ||
| 339 | } | ||
| 340 | |||
| 341 | # Without $extend, a selection collapses to its start (left) or end | ||
| 342 | # (right) instead of moving. | ||
| 343 | method move_left ( $extend = 0 ) { | ||
| 344 | my @selection = $self->selection; | ||
| 345 | return $self->move_to( @selection[ 0, 1 ] ) if @selection && !$extend; | ||
| 346 | return $self->move_to( $self->_position_left_of(@cursor), $extend ); | ||
| 347 | } | ||
| 348 | |||
| 349 | method move_right ( $extend = 0 ) { | ||
| 350 | my @selection = $self->selection; | ||
| 351 | return $self->move_to( @selection[ 2, 3 ] ) if @selection && !$extend; | ||
| 352 | return $self->move_to( $self->_position_right_of(@cursor), $extend ); | ||
| 353 | } | ||
| 354 | |||
| 355 | method move_word_left ( $extend = 0 ) { | ||
| 356 | return $self->move_to( $self->_word_start_before(@cursor), $extend ); | ||
| 357 | } | ||
| 358 | |||
| 359 | method move_word_right ( $extend = 0 ) { | ||
| 360 | return $self->move_to( $self->_word_end_after(@cursor), $extend ); | ||
| 361 | } | ||
| 362 | |||
| 363 | method move_line_start ( $extend = 0 ) { | ||
| 364 | return $self->move_to( $cursor[0], 0, $extend ); | ||
| 365 | } | ||
| 366 | |||
| 367 | method move_line_end ( $extend = 0 ) { | ||
| 368 | return $self->move_to( $cursor[0], length $lines[ $cursor[0] ], $extend ); | ||
| 369 | } | ||
| 370 | |||
| 371 | method move_document_start ( $extend = 0 ) { | ||
| 372 | return $self->move_to( 0, 0, $extend ); | ||
| 373 | } | ||
| 374 | |||
| 375 | method move_document_end ( $extend = 0 ) { | ||
| 376 | return $self->move_to( $#lines, length $lines[-1], $extend ); | ||
| 377 | } | ||
| 378 | |||
| 379 | # --------------------------------------------------------------------- | ||
| 380 | # Editing. Every edit returns whether the text changed; an edit that | ||
| 381 | # changes it can be undone. | ||
| 382 | # --------------------------------------------------------------------- | ||
| 383 | |||
| 384 | # Runs an edit and records an undo step when it changed the text. Typing | ||
| 385 | # extends the last step while the typed text stays words or stays | ||
| 386 | # spaces, so undo takes back a word at a time. | ||
| 387 | method _edit ( $run, $code ) { | ||
| 388 | my @before = ( [@cursor], [@anchor] ); | ||
| 389 | @changes = (); | ||
| 390 | $code->(); | ||
| 391 | my @made = grep { $_->{old} ne $_->{new} } splice @changes; | ||
| 392 | $revision++ if @made || "@{ $before[0] };@{ $before[1] }" ne "@cursor;@anchor"; | ||
| 393 | if ( !@made ) { | ||
| 394 | $typing_run = undef; | ||
| 395 | return 0; | ||
| 396 | } | ||
| 397 | |||
| 398 | my $extends_step = defined $run && defined $typing_run && $run eq $typing_run && @undo_stack; | ||
| 399 | if ($extends_step) { | ||
| 400 | push @{ $undo_stack[-1]{changes} }, @made; | ||
| 401 | $undo_stack[-1]{cursor_after} = [@cursor]; | ||
| 402 | } | ||
| 403 | else { | ||
| 404 | push @undo_stack, { changes => \@made, cursor => $before[0], anchor => $before[1], cursor_after => [@cursor] }; | ||
| 405 | } | ||
| 406 | shift @undo_stack while @undo_stack > UNDO_LIMIT; | ||
| 407 | @redo_stack = (); | ||
| 408 | $typing_run = $run; | ||
| 409 | $text_revision++; | ||
| 410 | return 1; | ||
| 411 | } | ||
| 412 | |||
| 413 | # Replaces the text between two ordered positions, recording the change | ||
| 414 | # for undo; the cursor ends up after the new text, without a selection. | ||
| 415 | method _replace ( $row_0, $offset_0, $row_1, $offset_1, $text ) { | ||
| 416 | push @changes, { row => $row_0, offset => $offset_0, old => $self->_text_between( $row_0, $offset_0, $row_1, $offset_1 ), new => $text }; | ||
| 417 | my @end = $self->_splice_text( $row_0, $offset_0, $row_1, $offset_1, $text ); | ||
| 418 | @cursor = ( $end[0], $self->_snap( @end, 1 ) ); | ||
| 419 | @anchor = (); | ||
| 420 | return; | ||
| 421 | } | ||
| 422 | |||
| 423 | # Puts $text between two ordered positions; returns the position after it. | ||
| 424 | method _splice_text ( $row_0, $offset_0, $row_1, $offset_1, $text ) { | ||
| 425 | my @new_lines = split /\n/, $text, -1; | ||
| 426 | @new_lines = ('') unless @new_lines; | ||
| 427 | my $after = substr( $lines[$row_1], $offset_1 ); | ||
| 428 | $new_lines[0] = substr( $lines[$row_0], 0, $offset_0 ) . $new_lines[0]; | ||
| 429 | my @end = ( $row_0 + $#new_lines, length $new_lines[-1] ); | ||
| 430 | $new_lines[-1] .= $after; | ||
| 431 | |||
| 432 | splice @lines, $row_0, $row_1 - $row_0 + 1, @new_lines; | ||
| 433 | return @end; | ||
| 434 | } | ||
| 435 | |||
| 436 | # The position after $text written at ($row, $offset). | ||
| 437 | sub _end_of ( $row, $offset, $text ) { | ||
| 438 | my @segments = split /\n/, $text, -1; | ||
| 439 | return ( $row, $offset + length $text ) if @segments <= 1; | ||
| 440 | return ( $row + $#segments, length $segments[-1] ); | ||
| 441 | } | ||
| 442 | |||
| 443 | method _delete_selection () { | ||
| 444 | my @selection = $self->selection; | ||
| 445 | return 0 unless @selection; | ||
| 446 | $self->_replace( @selection, '' ); | ||
| 447 | return 1; | ||
| 448 | } | ||
| 449 | |||
| 450 | # The longest prefix of $text that keeps the editor within max_length | ||
| 451 | # once the selection is replaced. | ||
| 452 | method _fitting ($text) { | ||
| 453 | return $text unless defined $max_length; | ||
| 454 | my $selected = $self->selected_text; | ||
| 455 | my $room = $max_length - $self->character_count + _cluster_count($selected) + ( $selected =~ tr/\n// ); | ||
| 456 | return '' if $room <= 0; | ||
| 457 | |||
| 458 | my ( $taken, $length ) = ( 0, 0 ); | ||
| 459 | foreach my $line ( split /(\n)/, $text ) { | ||
| 460 | my @offsets = @{ _boundaries($line) }; | ||
| 461 | my $clusters = $line eq "\n" ? 1 : $#offsets; | ||
| 462 | if ( $taken + $clusters > $room ) { | ||
| 463 | $length += $offsets[ $room - $taken ] if $line ne "\n"; | ||
| 464 | return substr( $text, 0, $length ); | ||
| 465 | } | ||
| 466 | $taken += $clusters; | ||
| 467 | $length += length $line; | ||
| 468 | } | ||
| 469 | return $text; | ||
| 470 | } | ||
| 471 | |||
| 472 | # The selection, or the empty range at the cursor. | ||
| 473 | method _replaced_range () { | ||
| 474 | my @range = $self->selection; | ||
| 475 | return @range ? @range : ( @cursor, @cursor ); | ||
| 476 | } | ||
| 477 | |||
| 478 | # What of $text may go in at the cursor: normalized, without the | ||
| 479 | # clusters accept rejects, cut to fit max_length. Undef when the text | ||
| 480 | # was not empty but nothing of it is accepted; such an edit does | ||
| 481 | # nothing, not even replace the selection. | ||
| 482 | method _insertable ($text) { | ||
| 483 | my $normalized = $self->_normalize($text); | ||
| 484 | my $accepted = $self->_accepted($normalized); | ||
| 485 | return undef if length $normalized && !length $accepted; | ||
| 486 | return $self->_fitting($accepted); | ||
| 487 | } | ||
| 488 | |||
| 489 | method insert ($text) { | ||
| 490 | $text = $self->_insertable($text) // return 0; | ||
| 491 | return 0 unless length $text || $self->has_selection; | ||
| 492 | return $self->_edit( undef, sub { $self->_replace( $self->_replaced_range, $text ) } ); | ||
| 493 | } | ||
| 494 | |||
| 495 | # Like insert, for text the user types: consecutive typing is undone | ||
| 496 | # word by word. | ||
| 497 | method type ($text) { | ||
| 498 | $text = $self->_insertable($text) // return 0; | ||
| 499 | return 0 unless length $text || $self->has_selection; | ||
| 500 | my $run = !length $text ? undef : $text =~ /\A\s+\z/ ? 'space' : 'word'; | ||
| 501 | return $self->_edit( $run, sub { $self->_replace( $self->_replaced_range, $text ) } ); | ||
| 502 | } | ||
| 503 | |||
| 504 | method _delete_to ( $row, $offset ) { | ||
| 505 | return $self->_edit( | ||
| 506 | undef, | ||
| 507 | sub { | ||
| 508 | return if $self->_delete_selection; | ||
| 509 | my @range = _compare( $row, $offset, @cursor ) < 0 ? ( $row, $offset, @cursor ) : ( @cursor, $row, $offset ); | ||
| 510 | $self->_replace( @range, '' ); | ||
| 511 | } | ||
| 512 | ); | ||
| 513 | } | ||
| 514 | |||
| 515 | method delete_backward () { | ||
| 516 | return $self->_delete_to( $self->_position_left_of(@cursor) ); | ||
| 517 | } | ||
| 518 | |||
| 519 | method delete_forward () { | ||
| 520 | return $self->_delete_to( $self->_position_right_of(@cursor) ); | ||
| 521 | } | ||
| 522 | |||
| 523 | method delete_word_backward () { | ||
| 524 | return $self->_delete_to( $self->_word_start_before(@cursor) ); | ||
| 525 | } | ||
| 526 | |||
| 527 | method delete_word_forward () { | ||
| 528 | return $self->_delete_to( $self->_word_end_after(@cursor) ); | ||
| 529 | } | ||
| 530 | |||
| 531 | # At the start (end) of a line, joins it with the previous (next) one. | ||
| 532 | method delete_to_line_start () { | ||
| 533 | return $self->delete_backward if $cursor[1] == 0 && !$self->has_selection; | ||
| 534 | return $self->_delete_to( $cursor[0], 0 ); | ||
| 535 | } | ||
| 536 | |||
| 537 | method delete_to_line_end () { | ||
| 538 | return $self->delete_forward if $cursor[1] == length( $lines[ $cursor[0] ] ) && !$self->has_selection; | ||
| 539 | return $self->_delete_to( $cursor[0], length $lines[ $cursor[0] ] ); | ||
| 540 | } | ||
| 541 | |||
| 542 | # --------------------------------------------------------------------- | ||
| 543 | # Clipboard | ||
| 544 | # --------------------------------------------------------------------- | ||
| 545 | |||
| 546 | # A plain sub, so that it can be called on the class and on an editor. | ||
| 547 | sub clipboard ( $invocant, @new ) { | ||
| 548 | return $clipboard unless @new; | ||
| 549 | die "Term::Fabulous::Editor: the clipboard holds a string, got " . ( ref $new[0] || 'undef' ) unless defined $new[0] && !ref $new[0]; | ||
| 550 | return $clipboard = $new[0]; | ||
| 551 | } | ||
| 552 | |||
| 553 | method copy () { | ||
| 554 | return 0 unless $self->has_selection; | ||
| 555 | $clipboard = $self->selected_text; | ||
| 556 | return 1; | ||
| 557 | } | ||
| 558 | |||
| 559 | method cut () { | ||
| 560 | return 0 unless $self->has_selection; | ||
| 561 | $clipboard = $self->selected_text; | ||
| 562 | return $self->_edit( undef, sub { $self->_delete_selection } ); | ||
| 563 | } | ||
| 564 | |||
| 565 | method paste () { | ||
| 566 | return $self->insert($clipboard); | ||
| 567 | } | ||
| 568 | |||
| 569 | # --------------------------------------------------------------------- | ||
| 570 | # Undo | ||
| 571 | # --------------------------------------------------------------------- | ||
| 572 | |||
| 573 | method can_undo () { | ||
| 574 | return @undo_stack ? 1 : 0; | ||
| 575 | } | ||
| 576 | |||
| 577 | method can_redo () { | ||
| 578 | return @redo_stack ? 1 : 0; | ||
| 579 | } | ||
| 580 | |||
| 581 | # Takes back the changes of the last step, newest first, and puts the | ||
| 582 | # cursor where it was before them. | ||
| 583 | method undo () { | ||
| 584 | my $step = pop @undo_stack // return 0; | ||
| 585 | foreach my $change ( reverse @{ $step->{changes} } ) { | ||
| 586 | my @at = @$change{qw(row offset)}; | ||
| 587 | $self->_splice_text( @at, _end_of( @at, $change->{new} ), $change->{old} ); | ||
| 588 | } | ||
| 589 | $self->_after_history( $step->{cursor}, $step->{anchor} ); | ||
| 590 | push @redo_stack, $step; | ||
| 591 | return 1; | ||
| 592 | } | ||
| 593 | |||
| 594 | # Makes the changes of the last undone step again, oldest first. | ||
| 595 | method redo () { | ||
| 596 | my $step = pop @redo_stack // return 0; | ||
| 597 | foreach my $change ( @{ $step->{changes} } ) { | ||
| 598 | my @at = @$change{qw(row offset)}; | ||
| 599 | $self->_splice_text( @at, _end_of( @at, $change->{old} ), $change->{new} ); | ||
| 600 | } | ||
| 601 | $self->_after_history( $step->{cursor_after}, [] ); | ||
| 602 | push @undo_stack, $step; | ||
| 603 | return 1; | ||
| 604 | } | ||
| 605 | |||
| 606 | method _after_history ( $cursor, $anchor ) { | ||
| 607 | @cursor = @$cursor; | ||
| 608 | @anchor = @$anchor; | ||
| 609 | $typing_run = undef; | ||
| 610 | $text_revision++; | ||
| 611 | $revision++; | ||
| 612 | return; | ||
| 613 | } | ||
| 614 | } | ||
| 615 | |||
| 616 | 1; | ||
| 617 | |||
| 618 | __END__ | ||