Skip to content

Commit

Permalink
Merge pull request #205 from mknos/ed-move
Browse files Browse the repository at this point in the history
ed: implement move command (m)
  • Loading branch information
briandfoy authored Jul 18, 2023
2 parents 5a11caf + 64572da commit 6fd4911
Showing 1 changed file with 45 additions and 2 deletions.
47 changes: 45 additions & 2 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
# m - move
# t - move/apend
# u - undo
# v - global command "inVerted"
Expand Down Expand Up @@ -258,6 +257,8 @@ while (1) {
}
} elsif ($command eq 'j') {
&edJoin;
} elsif ($command eq 'm') {
&edMove;
} elsif ($command eq 'n') {
edPrint(1);
}
Expand Down Expand Up @@ -322,6 +323,44 @@ sub edJoin {
$UserHasBeenWarned = 0;
}

sub edMove {
my $start = $adrs[0];
my $end = $adrs[1];
unless (defined $start) {
$start = $end = $CurrentLineNum;
}
unless (defined $end) {
$end = $start;
}
if ($start == 0 || $end == 0) { # allowed for $dst only
edWarn('invalid address');
return;
}
my $dst = $args[0];
unless (defined $dst) {
$dst = $CurrentLineNum;
}
my $count = $end - $start + 1;

if ($start > $dst && $end > $dst) {
my @sel = splice @lines, $start, $count;
splice @lines, $dst + 1, 0, @sel;
} 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;
}

$NeedToSave = 1;
$UserHasBeenWarned = 0;
}

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

Expand Down Expand Up @@ -1122,6 +1161,10 @@ Insert text
Join a range of lines into a single line
=item m
Move a range of lines to a new address
=item n
Print from buffer with line number prefix
Expand Down

0 comments on commit 6fd4911

Please sign in to comment.