diff options
| -rw-r--r-- | incdec.bash | 19 | ||||
| -rw-r--r-- | incdec.pm | 19 | 
2 files changed, 28 insertions, 10 deletions
| diff --git a/incdec.bash b/incdec.bash index 7144282..0b8eae3 100644 --- a/incdec.bash +++ b/incdec.bash @@ -31,7 +31,9 @@ sub incdec {  	$is_backward ||= 0;  	my $start_position = 0; +	# my $previous_match_start = $point_position;  	my $previous_match_start = 0; +	my $i = 0;  	while ($line =~ /(-?\d+)/g) {  		if ($is_backward) {  			# Set start position to the current match start. This gives us the @@ -39,6 +41,12 @@ sub incdec {  			# line.  			$start_position = $-[0]; +			# Keep the start position of the first number if point is before +			# the first number. +			if ($i == 0 && $point_position < $-[0]) { +				last; +			} +  			# If point is not at the end, set start position to the number  			# closest to the point position.  			if ($point_position < $-[0]) { @@ -48,6 +56,7 @@ sub incdec {  			}  			$previous_match_start = $-[0]; +			$i++;  		}  		else {  			if ($point_position < $+[0]) { @@ -59,13 +68,13 @@ sub incdec {  	}  	# Using `\G` when `pos` is 0 seems to cause occasional missed substitutions. -	if ($start_position > 0) { +	# if ($start_position > 0) {  		pos($line) = $start_position;  		$line =~ s/\G(-?\d+)/$1 + $increment_by/e; -	} -	else { -		$line =~ s/(-?\d+)/$1 + $increment_by/e; -	} +	# } +	# else { +	# 	$line =~ s/(-?\d+)/$1 + $increment_by/e; +	# }  	return ($line, $start_position);  } @@ -28,7 +28,9 @@ sub incdec {  	$is_backward ||= 0;  	my $start_position = 0; +	# my $previous_match_start = $point_position;  	my $previous_match_start = 0; +	my $i = 0;  	while ($line =~ /(-?\d+)/g) {  		if ($is_backward) {  			# Set start position to the current match start. This gives us the @@ -36,6 +38,12 @@ sub incdec {  			# line.  			$start_position = $-[0]; +			# Keep the start position of the first number if point is before +			# the first number. +			if ($i == 0 && $point_position < $-[0]) { +				last; +			} +  			# If point is not at the end, set start position to the number  			# closest to the point position.  			if ($point_position < $-[0]) { @@ -45,6 +53,7 @@ sub incdec {  			}  			$previous_match_start = $-[0]; +			$i++;  		}  		else {  			if ($point_position < $+[0]) { @@ -56,13 +65,13 @@ sub incdec {  	}  	# Using `\G` when `pos` is 0 seems to cause occasional missed substitutions. -	if ($start_position > 0) { +	# if ($start_position > 0) {  		pos($line) = $start_position;  		$line =~ s/\G(-?\d+)/$1 + $increment_by/e; -	} -	else { -		$line =~ s/(-?\d+)/$1 + $increment_by/e; -	} +	# } +	# else { +	# 	$line =~ s/(-?\d+)/$1 + $increment_by/e; +	# }  	return ($line, $start_position);  } | 
