Skip to content

Commit 3a50a5f

Browse files
authored
ed: support ?re? address mode (#774)
* ?re? is a valid prefix which searches backward * Tested against GNU and OpenBSD versions * test1: ?perl?n ---> search for previous instance of "perl" then run command n * test2: ??l ---> search for previous instance of saved pattern "perl" then run command l * test3: ?\??p ---> search backwards for literal "?" then run command p * test4: ?\/\/? ---> search backward for literal "//" then show result
1 parent 0b064c1 commit 3a50a5f

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

bin/ed

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -958,13 +958,14 @@ sub getAddr {
958958
$n = $CurrentLineNum;
959959
} elsif (s/\A\$//) { # '$' == max line
960960
$n = maxline();
961-
} elsif (s/\A\///) { # '/re/' == search
961+
} elsif (s/\A([\/\?])//) { # search: '/re/' or '?re?'
962+
my $delim = $1;
962963
my $i;
963964
my @chars = split //;
964965
for ($i = 0; $i < scalar(@chars); $i++) {
965966
my $j = $i - 1;
966967
$j = 0 if $j < 0;
967-
last if $chars[$i] eq '/' && $chars[$j] ne '\\';
968+
last if $chars[$i] eq $delim && $chars[$j] ne '\\';
968969
}
969970
my $re = substr $_, 0, $i;
970971
if (length($re) == 0) {
@@ -976,7 +977,11 @@ sub getAddr {
976977
}
977978
}
978979
$_ = substr $_, $i + 1;
979-
$n = edSearchForward($re);
980+
if ($delim eq '/') {
981+
$n = edSearchForward($re);
982+
} else {
983+
$n = edSearchBackward($re);
984+
}
980985
}
981986
return $n;
982987
}

0 commit comments

Comments
 (0)