Commit Diff


commit - 6e210706a4224007cf266b48c28e5a54e8279589
commit + c0c4acc88418ca34083144bd4feeb1c4001fc092
blob - 915bc83532c216505a96c19f3ac75ac031f66b87
blob + 66c10afc613515bfc9122f54ffacf6671ce45f67
--- tog/tog.c
+++ tog/tog.c
@@ -502,6 +502,7 @@ struct tog_view {
 
 	const struct got_error *(*search_start)(struct tog_view *);
 	const struct got_error *(*search_next)(struct tog_view *);
+	int search_started;
 	int searching;
 #define TOG_SEARCH_FORWARD	1
 #define TOG_SEARCH_BACKWARD	2
@@ -784,6 +785,13 @@ view_search_start(struct tog_view *view)
 	const struct got_error *err = NULL;
 	char pattern[1024];
 	int ret;
+
+	if (view->search_started) {
+		regfree(&view->regex);
+		view->searching = 0;
+		memset(&view->regmatch, 0, sizeof(view->regmatch));
+	}
+	view->search_started = 0;
 
 	if (view->nlines < 1)
 		return NULL;
@@ -798,11 +806,6 @@ view_search_start(struct tog_view *view)
 	noecho();
 	if (ret == ERR)
 		return NULL;
-
-	if (view->searching) {
-		regfree(&view->regex);
-		view->searching = 0;
-	}
 
 	if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
 		err = view->search_start(view);
@@ -810,6 +813,7 @@ view_search_start(struct tog_view *view)
 			regfree(&view->regex);
 			return err;
 		}
+		view->search_started = 1;
 		view->searching = TOG_SEARCH_FORWARD;
 		view->search_next_done = 0;
 		view->search_next(view);
@@ -939,7 +943,7 @@ view_input(struct tog_view **new, int *done, struct to
 		break;
 	case 'N':
 	case 'n':
-		if (view->search_next) {
+		if (view->search_started && view->search_next) {
 			view->searching = (ch == 'n' ?
 			    TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
 			view->search_next_done = 0;