Commit 9970f7fca9084ec99cb608732155668240b318fe

Stefan Sperling 2020-12-03T23:23:30

replace dead_view pointer in view_loop() with 'dying' flag in struct tog_view ok naddy

diff --git a/tog/tog.c b/tog/tog.c
index 93cab5b..50fefd4 100644
--- a/tog/tog.c
+++ b/tog/tog.c
@@ -447,6 +447,7 @@ struct tog_view {
 	int nlines, ncols, begin_y, begin_x;
 	int lines, cols; /* copies of LINES and COLS */
 	int focussed;
+	int dying;
 	struct tog_view *parent;
 	struct tog_view *child;
 
@@ -462,7 +463,7 @@ struct tog_view {
 
 	const struct got_error *(*show)(struct tog_view *);
 	const struct got_error *(*input)(struct tog_view **,
-	    struct tog_view **, struct tog_view**, struct tog_view *, int);
+	    struct tog_view **, struct tog_view *, int);
 	const struct got_error *(*close)(struct tog_view *);
 
 	const struct got_error *(*search_start)(struct tog_view *);
@@ -484,7 +485,7 @@ static const struct got_error *open_diff_view(struct tog_view *,
     struct got_repository *);
 static const struct got_error *show_diff_view(struct tog_view *);
 static const struct got_error *input_diff_view(struct tog_view **,
-    struct tog_view **, struct tog_view **, struct tog_view *, int);
+    struct tog_view **, struct tog_view *, int);
 static const struct got_error* close_diff_view(struct tog_view *);
 static const struct got_error *search_start_diff_view(struct tog_view *);
 static const struct got_error *search_next_diff_view(struct tog_view *);
@@ -494,7 +495,7 @@ static const struct got_error *open_log_view(struct tog_view *,
     const char *, const char *, int);
 static const struct got_error * show_log_view(struct tog_view *);
 static const struct got_error *input_log_view(struct tog_view **,
-    struct tog_view **, struct tog_view **, struct tog_view *, int);
+    struct tog_view **, struct tog_view *, int);
 static const struct got_error *close_log_view(struct tog_view *);
 static const struct got_error *search_start_log_view(struct tog_view *);
 static const struct got_error *search_next_log_view(struct tog_view *);
@@ -503,7 +504,7 @@ static const struct got_error *open_blame_view(struct tog_view *, char *,
     struct got_object_id *, struct got_repository *);
 static const struct got_error *show_blame_view(struct tog_view *);
 static const struct got_error *input_blame_view(struct tog_view **,
-    struct tog_view **, struct tog_view **, struct tog_view *, int);
+    struct tog_view **, struct tog_view *, int);
 static const struct got_error *close_blame_view(struct tog_view *);
 static const struct got_error *search_start_blame_view(struct tog_view *);
 static const struct got_error *search_next_blame_view(struct tog_view *);
@@ -512,7 +513,7 @@ static const struct got_error *open_tree_view(struct tog_view *,
     struct got_tree_object *, struct got_object_id *, struct got_repository *);
 static const struct got_error *show_tree_view(struct tog_view *);
 static const struct got_error *input_tree_view(struct tog_view **,
-    struct tog_view **, struct tog_view **, struct tog_view *, int);
+    struct tog_view **, struct tog_view *, int);
 static const struct got_error *close_tree_view(struct tog_view *);
 static const struct got_error *search_start_tree_view(struct tog_view *);
 static const struct got_error *search_next_tree_view(struct tog_view *);
@@ -521,7 +522,7 @@ static const struct got_error *open_ref_view(struct tog_view *,
     struct got_repository *);
 static const struct got_error *show_ref_view(struct tog_view *);
 static const struct got_error *input_ref_view(struct tog_view **,
-    struct tog_view **, struct tog_view **, struct tog_view *, int);
+    struct tog_view **, struct tog_view *, int);
 static const struct got_error *close_ref_view(struct tog_view *);
 static const struct got_error *search_start_ref_view(struct tog_view *);
 static const struct got_error *search_next_ref_view(struct tog_view *);
@@ -783,16 +784,14 @@ view_search_start(struct tog_view *view)
 }
 
 static const struct got_error *
-view_input(struct tog_view **new, struct tog_view **dead,
-    struct tog_view **focus, int *done, struct tog_view *view,
-    struct tog_view_list_head *views)
+view_input(struct tog_view **new, struct tog_view **focus, int *done,
+    struct tog_view *view, struct tog_view_list_head *views)
 {
 	const struct got_error *err = NULL;
 	struct tog_view *v;
 	int ch, errcode;
 
 	*new = NULL;
-	*dead = NULL;
 	*focus = NULL;
 
 	/* Clear "no matches" indicator. */
@@ -833,7 +832,7 @@ view_input(struct tog_view **new, struct tog_view **dead,
 			err = view_resize(v);
 			if (err)
 				return err;
-			err = v->input(new, dead, focus, v, KEY_RESIZE);
+			err = v->input(new, focus, v, KEY_RESIZE);
 			if (err)
 				return err;
 		}
@@ -850,8 +849,8 @@ view_input(struct tog_view **new, struct tog_view **dead,
 		}
 		break;
 	case 'q':
-		err = view->input(new, dead, focus, view, ch);
-		*dead = view;
+		err = view->input(new, focus, view, ch);
+		view->dying = 1;
 		break;
 	case 'Q':
 		*done = 1;
@@ -867,8 +866,8 @@ view_input(struct tog_view **new, struct tog_view **dead,
 				err = view_splitscreen(view->child);
 			if (err)
 				break;
-			err = view->child->input(new, dead, focus,
-			    view->child, KEY_RESIZE);
+			err = view->child->input(new, focus, view->child,
+			    KEY_RESIZE);
 		} else {
 			if (view_is_splitscreen(view)) {
 				*focus = view;
@@ -878,8 +877,7 @@ view_input(struct tog_view **new, struct tog_view **dead,
 			}
 			if (err)
 				break;
-			err = view->input(new, dead, focus, view,
-			    KEY_RESIZE);
+			err = view->input(new, focus, view, KEY_RESIZE);
 		}
 		break;
 	case KEY_RESIZE:
@@ -888,7 +886,7 @@ view_input(struct tog_view **new, struct tog_view **dead,
 		if (view->search_start)
 			view_search_start(view);
 		else
-			err = view->input(new, dead, focus, view, ch);
+			err = view->input(new, focus, view, ch);
 		break;
 	case 'N':
 	case 'n':
@@ -898,10 +896,10 @@ view_input(struct tog_view **new, struct tog_view **dead,
 			view->search_next_done = 0;
 			view->search_next(view);
 		} else
-			err = view->input(new, dead, focus, view, ch);
+			err = view->input(new, focus, view, ch);
 		break;
 	default:
-		err = view->input(new, dead, focus, view, ch);
+		err = view->input(new, focus, view, ch);
 		break;
 	}
 
@@ -945,7 +943,7 @@ view_loop(struct tog_view *view)
 {
 	const struct got_error *err = NULL;
 	struct tog_view_list_head views;
-	struct tog_view *new_view, *dead_view, *focus_view, *main_view;
+	struct tog_view *new_view, *focus_view, *main_view;
 	int fast_refresh = 10;
 	int done = 0, errcode;
 
@@ -968,45 +966,42 @@ view_loop(struct tog_view *view)
 		if (fast_refresh && fast_refresh-- == 0)
 			halfdelay(10); /* switch to once per second */
 
-		err = view_input(&new_view, &dead_view, &focus_view, &done,
-		    view, &views);
+		err = view_input(&new_view, &focus_view, &done, view, &views);
 		if (err)
 			break;
-		if (dead_view) {
+		if (view->dying) {
 			struct tog_view *prev = NULL;
 
-			if (view_is_parent_view(dead_view))
-				prev = TAILQ_PREV(dead_view,
-				    tog_view_list_head, entry);
-			else if (view->parent != dead_view)
+			if (view_is_parent_view(view))
+				prev = TAILQ_PREV(view, tog_view_list_head,
+				    entry);
+			else if (view->parent != view)
 				prev = view->parent;
 
-			if (dead_view->parent)
-				dead_view->parent->child = NULL;
+			if (view->parent)
+				view->parent->child = NULL;
 			else
-				TAILQ_REMOVE(&views, dead_view, entry);
+				TAILQ_REMOVE(&views, view, entry);
 
-			err = view_close(dead_view);
-			if (err || (dead_view == main_view && new_view == NULL))
+			err = view_close(view);
+			if (err || (view == main_view && new_view == NULL))
 				goto done;
 
-			if (view == dead_view) {
-				if (focus_view)
-					view = focus_view;
-				else if (prev)
-					view = prev;
-				else if (!TAILQ_EMPTY(&views))
-					view = TAILQ_LAST(&views,
-					    tog_view_list_head);
+			if (focus_view)
+				view = focus_view;
+			else if (prev)
+				view = prev;
+			else if (!TAILQ_EMPTY(&views))
+				view = TAILQ_LAST(&views,
+				    tog_view_list_head);
+			else
+				view = NULL;
+			if (view) {
+				if (view->child &&
+				    view->child->focussed)
+					focus_view = view->child;
 				else
-					view = NULL;
-				if (view) {
-					if (view->child &&
-					    view->child->focussed)
-						focus_view = view->child;
-					else
-						focus_view = view;
-				}
+					focus_view = view;
 			}
 		}
 		if (new_view) {
@@ -2185,13 +2180,13 @@ search_next_log_view(struct tog_view *view)
 	if (s->matched_entry) {
 		int cur = s->selected_entry->idx;
 		while (cur < s->matched_entry->idx) {
-			err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
+			err = input_log_view(NULL, NULL, view, KEY_DOWN);
 			if (err)
 				return err;
 			cur++;
 		}
 		while (cur > s->matched_entry->idx) {
-			err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
+			err = input_log_view(NULL, NULL, view, KEY_UP);
 			if (err)
 				return err;
 			cur--;
@@ -2330,8 +2325,8 @@ show_log_view(struct tog_view *view)
 }
 
 static const struct got_error *
-input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
-    struct tog_view **focus_view, struct tog_view *view, int ch)
+input_log_view(struct tog_view **new_view, struct tog_view **focus_view,
+    struct tog_view *view, int ch)
 {
 	const struct got_error *err = NULL;
 	struct tog_log_view_state *s = &view->state.log;
@@ -2508,7 +2503,7 @@ input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
 			view_close(lv);
 			return err;;
 		}
-		*dead_view = view;
+		view->dying = 1;
 		*new_view = lv;
 		break;
 	case 'B':
@@ -2526,7 +2521,7 @@ input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
 			view_close(lv);
 			return err;;
 		}
-		*dead_view = view;
+		view->dying = 1;
 		*new_view = lv;
 		break;
 	case 'r':
@@ -3587,8 +3582,8 @@ set_selected_commit(struct tog_diff_view_state *s,
 }
 
 static const struct got_error *
-input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
-    struct tog_view **focus_view, struct tog_view *view, int ch)
+input_diff_view(struct tog_view **new_view, struct tog_view **focus_view,
+    struct tog_view *view, int ch)
 {
 	const struct got_error *err = NULL;
 	struct tog_diff_view_state *s = &view->state.diff;
@@ -3671,8 +3666,7 @@ input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
 		if (entry == NULL)
 			break;
 
-		err = input_log_view(NULL, NULL, NULL, s->log_view,
-		    KEY_UP);
+		err = input_log_view(NULL, NULL, s->log_view, KEY_UP);
 		if (err)
 			break;
 
@@ -3698,8 +3692,7 @@ input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
 			if (err)
 				break;
 		}
-		err = input_log_view(NULL, NULL, NULL, s->log_view,
-		    KEY_DOWN);
+		err = input_log_view(NULL, NULL, s->log_view, KEY_DOWN);
 		if (err)
 			break;
 
@@ -4428,8 +4421,8 @@ show_blame_view(struct tog_view *view)
 }
 
 static const struct got_error *
-input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
-    struct tog_view **focus_view, struct tog_view *view, int ch)
+input_blame_view(struct tog_view **new_view, struct tog_view **focus_view,
+    struct tog_view *view, int ch)
 {
 	const struct got_error *err = NULL, *thread_err = NULL;
 	struct tog_view *diff_view;
@@ -5264,8 +5257,8 @@ show_tree_view(struct tog_view *view)
 }
 
 static const struct got_error *
-input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
-    struct tog_view **focus_view, struct tog_view *view, int ch)
+input_tree_view(struct tog_view **new_view, struct tog_view **focus_view,
+    struct tog_view *view, int ch)
 {
 	const struct got_error *err = NULL;
 	struct tog_tree_view_state *s = &view->state.tree;
@@ -6035,8 +6028,8 @@ done:
 	return err;
 }
 static const struct got_error *
-input_ref_view(struct tog_view **new_view, struct tog_view **dead_view,
-    struct tog_view **focus_view, struct tog_view *view, int ch)
+input_ref_view(struct tog_view **new_view, struct tog_view **focus_view,
+    struct tog_view *view, int ch)
 {
 	const struct got_error *err = NULL;
 	struct tog_ref_view_state *s = &view->state.ref;