diff options
| author | Teddy Wing | 2021-08-22 20:17:54 +0200 |
|---|---|---|
| committer | Teddy Wing | 2021-08-22 20:32:27 +0200 |
| commit | ebce4b2272bcb8299d0ab4fa1524b6f4af9976c3 (patch) | |
| tree | d813be4d796a7e0b960e8d8145da4a340bc9eb68 /incdec.pm | |
| parent | 6a2d1fc08c17b417a25156bdadcaf45d1929ea4f (diff) | |
| download | readline-incdec-ebce4b2272bcb8299d0ab4fa1524b6f4af9976c3.tar.bz2 | |
incdec: Fix negative number handling when number is second of two
When the negative number is the last of two numbers, the first number
would be incremented or decremented instead of the second one. Fix this
by:
* Removing the `\b` word boundaries in the search pattern. This caused
the match start position to not include the hyphen that designates a
negative number (instead starting at one after the hyphen).
* Setting `$start_position` to the start position of the current match
(`$-[0]`) on every match loop iteration. This ensures the start
position is set to an appropriate index when the number we want to
increment is the last one in the line. Since the condition in the loop
depends on there being a previous match position, it wouldn't set the
start position correctly in all cases for the last number in the line.
* Remove the special case to handle the last number in the line when
searching backward with the cursor in the last position. That case is
now handled directly in the loop by always setting `$start_position`.
Diffstat (limited to 'incdec.pm')
| -rw-r--r-- | incdec.pm | 19 |
1 files changed, 9 insertions, 10 deletions
@@ -12,15 +12,18 @@ sub incdec { my $start_position = 0; # my @match_ranges; my $previous_match_start = 0; - # my $previous_match_end = 0; - while ($line =~ /\b(-?\d+)\b/g) { + my $previous_match_end = 0; + while ($line =~ /(-?\d+)/g) { if ($is_backward) { - # print "p[$point_position] -[$-[0]] +[$+[0]]\n"; - # print "p[$point_position] -[$previous_match_start] +[$previous_match_end]\n"; + print "p[$point_position] -[$-[0]] +[$+[0]]\n"; + print "p[$point_position] -[$previous_match_start] +[$previous_match_end]\n"; # print "last match: $^N\n"; - # print $previous_match_end - 1 . " <= $point_position < $-[0]\n"; + print $previous_match_end - 1 . " <= $point_position < $-[0]\n"; # if ($previous_match_end - 1 <= $point_position # && $point_position < $-[0]) { + + # Try always setting $start_position and resetting it if there's another number? + $start_position = $-[0]; if ($point_position < $-[0]) { # print "match at [$previous_match_start]"; $start_position = $previous_match_start; @@ -29,7 +32,7 @@ sub incdec { } $previous_match_start = $-[0]; - # $previous_match_end = $+[0]; + $previous_match_end = $+[0]; # my @range = ($-[0], $+[0]); # push @match_ranges, \@range; @@ -43,10 +46,6 @@ sub incdec { } } - if ($is_backward && $point_position == length $line) { - $start_position = $previous_match_start; - } - pos($line) = $start_position; $line =~ s/\G([^-\d]*)(-?\d+)/$1 . ($2 + $increment_by)/e; |
