Skip to content

Commit

Permalink
ed: simplify code for r command
Browse files Browse the repository at this point in the history
* For "0r filename", file content is added to the beginning of lines list (preserving initial undef element)
* For "3r filename", file content is added after the 3rd line in buffer (i.e. after element 4 considering initial undef element)
* Each case can be written as a single call to splice
  • Loading branch information
mknos authored Nov 30, 2023
1 parent 9dc8209 commit e189f2d
Showing 1 changed file with 3 additions and 10 deletions.
13 changes: 3 additions & 10 deletions bin/ed
Original file line number Diff line number Diff line change
Expand Up @@ -726,16 +726,10 @@ sub edEdit {
push(@lines,@tmp_lines);
$CurrentLineNum = maxline();
} elsif ($adrs[0] == 0) {
shift(@lines); # get rid of undefined line
unshift(@lines,@tmp_lines);
unshift(@lines,undef); # put 0 line back
splice @lines, 0, 1, undef, @tmp_lines;
$CurrentLineNum = scalar(@tmp_lines);
} else {
shift(@lines); # get rid 0 line
@tmp_lines2 = splice(@lines,0,$adrs[0]);
unshift(@lines,@tmp_lines);
unshift(@lines,@tmp_lines2);
unshift(@lines,undef); # put 0 line back
splice @lines, $adrs[0] + 1, 0, @tmp_lines;
$CurrentLineNum = $adrs[0] + scalar(@tmp_lines);
}

Expand All @@ -747,8 +741,7 @@ sub edEdit {
return;
}

@lines = @tmp_lines;
unshift(@lines,undef); # line 0 is not used
@lines = (undef, @tmp_lines);
$NeedToSave = 0;
$CurrentLineNum = maxline();
}
Expand Down

0 comments on commit e189f2d

Please sign in to comment.