summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--usr.bin/mg/re_search.c22
1 files changed, 16 insertions, 6 deletions
diff --git a/usr.bin/mg/re_search.c b/usr.bin/mg/re_search.c
index 6c5a9b9f115..59641ca9a33 100644
--- a/usr.bin/mg/re_search.c
+++ b/usr.bin/mg/re_search.c
@@ -390,7 +390,7 @@ re_backsrch() {
register LINE *clp;
register int tbo;
- int error, startpos;
+ regmatch_t lastmatch;
char m[1];
clp = curwp->w_dotp;
@@ -410,15 +410,25 @@ re_backsrch() {
while (clp != (curbp->b_linep)) {
- re_match[0].rm_so = tbo;
- re_match[0].rm_eo = llength(clp);
- /* XXX - does not search backwards! */
- error = regexec(&re_buff, ltext(clp), RE_NMATCH, re_match, REG_STARTEND);
+ re_match[0].rm_so = 0;
+ re_match[0].rm_eo = tbo;
+ lastmatch.rm_so = -1;
+ /* Keep searching until we don't match any longer. Assumes a non-match
+ does not modify the re_match array.
+ */
+ while (!regexec(&re_buff, ltext(clp), RE_NMATCH, re_match, REG_STARTEND)) {
+ memcpy(&lastmatch, &re_match[0], sizeof(regmatch_t));
+ if (re_match[0].rm_eo >= tbo)
+ break;
+ re_match[0].rm_so = re_match[0].rm_eo;
+ re_match[0].rm_eo = tbo;
+ }
- if (error) {
+ if (lastmatch.rm_so == -1) {
clp = lback(clp);
tbo = llength(clp);
} else {
+ memcpy(&re_match[0], &lastmatch, sizeof(regmatch_t));
curwp->w_doto = re_match[0].rm_so;
curwp->w_dotp = clp;
curwp->w_flag |= WFMOVE;