Skip to content

Commit

Permalink
ed: re-add support for /re/ address-mode (#773)
Browse files Browse the repository at this point in the history
* Standard ed allows a regex to be placed in front of a command
* Previously CalculateLine() supported this but the function was replaced by getAddr()
* Add a new condition in getAddr() to allow resolving /re/ command prefix to a line number
* Be careful to support repeated search for empty pattern
* test1: /include/n  ---> search for /include/, resolve line number and run n command
* test2: //l ---> repeat search using saved /include/ pattern, then run l command on next-match
* test3: /return/  ---> no command given with pattern still works
* test4: /a\/b/p --> search pattern includes escaped slash; run p command on result
  • Loading branch information
mknos authored Oct 29, 2024
1 parent c93d494 commit 0b064c1
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions bin/ed
Original file line number Diff line number Diff line change
Expand Up @@ -958,6 +958,25 @@ sub getAddr {
$n = $CurrentLineNum;
} elsif (s/\A\$//) { # '$' == max line
$n = maxline();
} elsif (s/\A\///) { # '/re/' == search
my $i;
my @chars = split //;
for ($i = 0; $i < scalar(@chars); $i++) {
my $j = $i - 1;
$j = 0 if $j < 0;
last if $chars[$i] eq '/' && $chars[$j] ne '\\';
}
my $re = substr $_, 0, $i;
if (length($re) == 0) {
if (defined $SearchPat) {
$re = $SearchPat;
} else {
edWarn(E_NOPAT);
return 0;
}
}
$_ = substr $_, $i + 1;
$n = edSearchForward($re);
}
return $n;
}
Expand Down

0 comments on commit 0b064c1

Please sign in to comment.