From ae34825f58d2f7d7a6e3bcf97868277a5c12e6c8 Mon Sep 17 00:00:00 2001
From: Benno Schulenberg <bensberg@telfort.nl>
Date: Mon, 18 Sep 2017 20:34:57 +0200
Subject: [PATCH] display: rework the switching on and off of the cursor

The basic idea is that the cursor is always off, except when it needs
to be on: when waiting for text input, and in a few other cases: when
something was searched and found in the help viewer, and in the file
browser when option -g is in effect.

This fixes https://savannah.gnu.org/bugs/?51923.
Reported-by: Mike Frysinger <vapier@gentoo.org>
---
 src/browser.c |  6 ++----
 src/global.c  |  2 ++
 src/help.c    |  4 +++-
 src/nano.c    |  7 ++++---
 src/prompt.c  |  7 ++++---
 src/proto.h   |  1 +
 src/search.c  |  3 ---
 src/text.c    |  5 ++---
 src/winio.c   | 14 +++++++-------
 9 files changed, 25 insertions(+), 24 deletions(-)

diff --git a/src/browser.c b/src/browser.c
index de264cdd..ff1b4033 100644
--- a/src/browser.c
+++ b/src/browser.c
@@ -55,8 +55,8 @@ char *do_browser(char *path)
     DIR *dir;
 	/* The directory whose contents we are showing. */
 
-    /* Don't show a cursor in the file list. */
-    curs_set(0);
+    /* Show a cursor in the file list only when requested. */
+    reveal_cursor = ISSET(SHOW_CURSOR);
 
   read_directory_contents:
 	/* We come here when we refresh or select a new directory. */
@@ -106,8 +106,6 @@ char *do_browser(char *path)
     titlebar(path);
 
     while (TRUE) {
-	/* Make sure that the cursor is off. */
-	curs_set(0);
 	lastmessage = HUSH;
 
 	bottombars(MBROWSER);
diff --git a/src/global.c b/src/global.c
index 46633e38..f1cc8530 100644
--- a/src/global.c
+++ b/src/global.c
@@ -56,6 +56,8 @@ bool have_palette = FALSE;
 	/* Whether the colors for the current syntax have been initialized. */
 #endif
 
+bool reveal_cursor = FALSE;
+	/* Whether the cursor should be shown when waiting for input. */
 bool suppress_cursorpos = FALSE;
 	/* Should we skip constant position display for current keystroke? */
 
diff --git a/src/help.c b/src/help.c
index 84e957db..69aa2e98 100644
--- a/src/help.c
+++ b/src/help.c
@@ -161,6 +161,7 @@ void do_help(void)
 
     bottombars(MHELP);
     wnoutrefresh(bottomwin);
+    reveal_cursor = FALSE;
 
     /* Extract the title from the head of the help text. */
     length = break_line(help_text, MAX_BUF_SIZE, TRUE);
@@ -224,12 +225,13 @@ void do_help(void)
 	} else if (func == do_exit) {
 	    /* Exit from the help viewer. */
 	    close_buffer();
+	    curs_set(0);
 	    break;
 	} else
 	    unbound_key(kbinput);
 
 	/* If we searched and found something, let the cursor show it. */
-	curs_set(didfind == 1 ? 1 : 0);
+	reveal_cursor = (didfind == 1);
 
 	currmenu = MHELP;
 	edit_refresh();
diff --git a/src/nano.c b/src/nano.c
index a5ba3e62..25e22b32 100644
--- a/src/nano.c
+++ b/src/nano.c
@@ -1579,9 +1579,13 @@ int do_input(bool allow_funcs)
     const sc *s;
     bool have_shortcut;
 
+    reveal_cursor = TRUE;
+
     /* Read in a keystroke. */
     input = get_kbinput(edit);
 
+    reveal_cursor = FALSE;
+
 #ifndef NANO_TINY
     if (input == KEY_WINCH)
 	return KEY_WINCH;
@@ -2650,9 +2654,6 @@ int main(int argc, char **argv)
 	} else
 	    edit_refresh();
 
-	/* Make sure the cursor is visible. */
-	curs_set(1);
-
 	focusing = TRUE;
 
 	/* Forget any earlier statusbar x position. */
diff --git a/src/prompt.c b/src/prompt.c
index 5785cb4c..f61d9a94 100644
--- a/src/prompt.c
+++ b/src/prompt.c
@@ -478,7 +478,7 @@ functionptrtype acquire_an_answer(int *actual, bool allow_tabs,
 
     while (TRUE) {
 	/* Ensure the cursor is shown when waiting for input. */
-	curs_set(1);
+	reveal_cursor = TRUE;
 
 	kbinput = do_statusbar_input(&ran_func, &finished);
 
@@ -597,6 +597,8 @@ functionptrtype acquire_an_answer(int *actual, bool allow_tabs,
     }
 #endif
 
+    reveal_cursor = FALSE;
+
     *actual = kbinput;
 
     return func;
@@ -746,8 +748,7 @@ int do_yesno_prompt(bool all, const char *msg)
 	wnoutrefresh(bottomwin);
 
 	/* When not replacing, show the cursor. */
-	if (!all)
-	    curs_set(1);
+	reveal_cursor = !all;
 
 	currmenu = MYESNO;
 	kbinput = get_kbinput(bottomwin);
diff --git a/src/proto.h b/src/proto.h
index bd3a3cb0..c6303f34 100644
--- a/src/proto.h
+++ b/src/proto.h
@@ -47,6 +47,7 @@ extern int editwincols;
 extern bool have_palette;
 #endif
 
+extern bool reveal_cursor;
 extern bool suppress_cursorpos;
 
 extern message_type lastmessage;
diff --git a/src/search.c b/src/search.c
index f8049193..ea7b6b65 100644
--- a/src/search.c
+++ b/src/search.c
@@ -608,9 +608,6 @@ ssize_t do_replace_loop(const char *needle, bool whole_word_only,
 	    /* Refresh the edit window, scrolling it if necessary. */
 	    edit_refresh();
 
-	    /* Don't show cursor, to not distract from highlighted match. */
-	    curs_set(0);
-
 	    spotlight(TRUE, from_col, to_col);
 
 	    /* TRANSLATORS: This is a prompt. */
diff --git a/src/text.c b/src/text.c
index b302163c..522197e5 100644
--- a/src/text.c
+++ b/src/text.c
@@ -2478,7 +2478,6 @@ void do_justify(bool full_justify)
 #endif
 	statusbar(_("Can now UnJustify!"));
 	place_the_cursor();
-	curs_set(1);
 	kbinput = do_input(FALSE);
 #ifndef NANO_TINY
     } while (kbinput == KEY_WINCH);
@@ -3337,8 +3336,8 @@ void do_linter(void)
 
 	/* Place and show the cursor to indicate the affected line. */
 	place_the_cursor();
+	reveal_cursor = TRUE;
 	wnoutrefresh(edit);
-	curs_set(1);
 
 	kbinput = get_kbinput(bottomwin);
 
@@ -3585,7 +3584,7 @@ void do_verbatim_input(void)
      * inserted verbatim. */
     statusbar(_("Verbatim Input"));
     place_the_cursor();
-    curs_set(1);
+    reveal_cursor = TRUE;
 
     /* Read in all the verbatim characters. */
     kbinput = get_verbatim_kbinput(edit, &kbinput_len);
diff --git a/src/winio.c b/src/winio.c
index b2e5bab9..7225fd3d 100644
--- a/src/winio.c
+++ b/src/winio.c
@@ -120,9 +120,14 @@ void get_key_buffer(WINDOW *win)
      * screen updates. */
     doupdate();
 
+    if (reveal_cursor)
+	curs_set(1);
+
     /* Read in the first character using whatever mode we're in. */
     input = wgetch(win);
 
+    curs_set(0);
+
 #ifndef NANO_TINY
     if (the_window_resized) {
 	ungetch(input);
@@ -1553,6 +1558,8 @@ int *parse_verbatim_kbinput(WINDOW *win, size_t *count)
 	    char *multibyte;
 	    int onebyte, i;
 
+	    reveal_cursor = FALSE;
+
 	    while (unicode == ERR) {
 		free(kbinput);
 		while ((kbinput = get_input(win, 1)) == NULL)
@@ -2186,9 +2193,6 @@ void statusline(message_type importance, const char *msg, ...)
 
     lastmessage = importance;
 
-    /* Turn the cursor off while fiddling in the statusbar. */
-    curs_set(0);
-
     blank_statusbar();
 
     /* Construct the message out of all the arguments. */
@@ -3369,9 +3373,6 @@ void do_cursorpos(bool force)
 	return;
     }
 
-    /* Hide the cursor while we are calculating. */
-    curs_set(0);
-
     /* Determine the size of the file up to the cursor. */
     saved_byte = openfile->current->data[openfile->current_x];
     openfile->current->data[openfile->current_x] = '\0';
@@ -3607,7 +3608,6 @@ void do_credits(void)
 	window_init();
     }
 
-    curs_set(0);
     nodelay(edit, TRUE);
 
     blank_titlebar();
-- 
GitLab