Skip to content

Commit 522c898

Browse files
authored
ed: simplify edMove()
* The code was more complicated than it needed to be * Copy & delete operations can always be done in the same order; first copy, then delete * Delete happens for "m" command but not for "t" command * If lines were copied before the start address, the address of the lines to be delete was increased by the number of lines copied * test1: 17,18m5 --> after copy, original lines to delete are now at 19,20 * test2: 17,18m29 --> no address adjustment needed before deleting original lines
1 parent 5c0679f commit 522c898

File tree

1 file changed

+5
-7
lines changed

1 file changed

+5
-7
lines changed

bin/ed

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -485,13 +485,11 @@ sub edMove {
485485

486486
my $count = $end - $start + 1;
487487
my @copy = @lines[$start .. $end];
488-
if ($start > $dst && $end > $dst) {
489-
splice(@lines, $start, $count) if $delete;
490-
splice @lines, $dst + 1, 0, @copy;
491-
} else {
492-
# avoid $dst referring to the wrong line
493-
splice @lines, $dst + 1, 0, @copy;
494-
splice(@lines, $start, $count) if $delete;
488+
splice @lines, $dst + 1, 0, @copy;
489+
if ($delete) {
490+
my $i = $start;
491+
$i += $count if $dst < $start; # lines moved up
492+
splice @lines, $i, $count;
495493
}
496494

497495
$NeedToSave = 1;

0 commit comments

Comments
 (0)