Skip to content

Commit

Permalink
Merge pull request #207 from mknos/ed-copy
Browse files Browse the repository at this point in the history
ed: implement t command (copy)
  • Loading branch information
briandfoy authored Jul 19, 2023
2 parents d3f0836 + e332486 commit aca4afc
Showing 1 changed file with 31 additions and 13 deletions.
44 changes: 31 additions & 13 deletions bin/ed
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ License: gpl
# g - global command
# k - mark
# l - "list" lines, show special chars
# t - move/apend
# u - undo
# v - global command "inVerted"
# x - get encryption key
Expand Down Expand Up @@ -258,9 +257,11 @@ while (1) {
} elsif ($command eq 'j') {
&edJoin;
} elsif ($command eq 'm') {
&edMove;
edMove(1);
} elsif ($command eq 'n') {
edPrint(1);
} elsif ($command eq 't') {
&edMove;
}

} else {
Expand Down Expand Up @@ -324,6 +325,8 @@ sub edJoin {
}

sub edMove {
my $delete = shift;

my $start = $adrs[0];
my $end = $adrs[1];
unless (defined $start) {
Expand All @@ -340,21 +343,32 @@ sub edMove {
unless (defined $dst) {
$dst = $CurrentLineNum;
}
my $count = $end - $start + 1;
if ($delete) {
# move a line to itself
if ($start == $dst && $end == $dst) {
return;
}
# move a range into itself
if ($dst >= $start && $dst <= $end) {
edWarn('invalid destination');
return;
}
}

my $count = $end - $start + 1;
my @copy;
if ($count == 1) {
push @copy, $lines[$start];
} else {
@copy = @lines[$start .. $end];
}
if ($start > $dst && $end > $dst) {
my @sel = splice @lines, $start, $count;
splice @lines, $dst + 1, 0, @sel;
splice(@lines, $start, $count) if $delete;
splice @lines, $dst + 1, 0, @copy;
} else {
# avoid $dst referring to the wrong line
my @copy;
if ($count == 1) {
push @copy, $lines[$start];
} else {
@copy = @lines[$start .. $end];
}
splice @lines, $dst + 1, 0, @copy;
splice @lines, $start, $count;
splice(@lines, $start, $count) if $delete;
}

$NeedToSave = 1;
Expand Down Expand Up @@ -853,7 +867,7 @@ sub edParse {
(([+-])?(\d+))? # [+]num | -num
(([\+]+)|([-^]+))? # + | -
)?
([acdeEfhHijmnpPqQrswW=])? # command char
([acdeEfhHijmnpPqQrstwW=])? # command char
\s*(\S+)? # argument (filename, etc.)
)$/x);

Expand Down Expand Up @@ -1194,6 +1208,10 @@ Read named FILE into buffer
Substitute text with a regular expression
=item t
Copy a range of lines to a destination address
=item W [FILE]
Write buffer to file in append mode
Expand Down

0 comments on commit aca4afc

Please sign in to comment.