"GOT", but the "O" is a cute, smiling pufferfish. Index | Thread | Search

From:
Stefan Sperling <stsp@stsp.name>
Subject:
Re: tog: 'n' before '/'
To:
Christian Weisgerber <naddy@mips.inka.de>
Cc:
gameoftrees@openbsd.org
Date:
Sun, 24 Jan 2021 17:43:42 +0100

Download raw body.

Thread
On Sun, Jan 24, 2021 at 04:53:14PM +0100, Christian Weisgerber wrote:
> Stefan Sperling:
> 
> > --- tog/tog.c
> > +++ tog/tog.c
> > @@ -939,7 +939,7 @@ view_input(struct tog_view **new, int *done, struct to
> >  		break;
> >  	case 'N':
> >  	case 'n':
> > -		if (view->search_next) {
> > +		if (view->searching) {
> >  			view->searching = (ch == 'n' ?
> >  			    TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
> >  			view->search_next_done = 0;
> 
> The "if (view->search_next)" checks whether a view implements a
> search_next function.  You don't want to drop that.
> 
> -               if (view->search_next) {
> +               if (view->search_next && view->searching) {
> 
> That seems to do it.

Actually, it's not that simple. Some search_next() callbacks already
contain code to handle view->searching == 0.

Let's add a new state flag for this. And clear any highlighted search terms
in the diff and blame views if the user types / followed by Enter.

diff 6e210706a4224007cf266b48c28e5a54e8279589 /home/stsp/src/got
blob - 915bc83532c216505a96c19f3ac75ac031f66b87
file + tog/tog.c
--- 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
@@ -785,6 +786,13 @@ view_search_start(struct tog_view *view)
 	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;
 
@@ -799,17 +807,13 @@ view_search_start(struct tog_view *view)
 	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);
 		if (err) {
 			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;