Subject: Re: bug#51475: read-char-choice should hide the cursor

From: Gregory Heytings <[email-masked]>
Date: Fri, 29 Oct 2021 18:41:37 +0000

>
> We could hide it if the prompt is multi-line, I guess?  Anybody got an 
> opinion?
>

Or we could make it depend on a dynamic variable.  See attached patch.

From d541c01d44fed39b3e144fff5499c7a306c255f8 Mon Sep 17 00:00:00 2001
From: Gregory Heytings <[email-masked]>
Date: Fri, 29 Oct 2021 18:37:32 +0000
Subject: [PATCH] Make it possible to hide the cursor during
 read-char-choice/from-minibuffer.

* lisp/subr.el (read-char-choice-hide-cursor,
read-char-from-minibuffer-hide-cursor): New variables.
(read-char-choice, read-char-choice-with-read-key,
read-char-from-minibuffer): Use them.
---
 lisp/subr.el | 19 +++++++++++++++----
 1 file changed, 15 insertions(+), 4 deletions(-)

diff --git a/lisp/subr.el b/lisp/subr.el
index 86460d9da6..3ff160cc64 100644
--- a/lisp/subr.el
+++ b/lisp/subr.el
@@ -3041,6 +3041,9 @@ read-char-choice-use-read-key
 character.  This is not possible when using `read-key', but using
 `read-key' may be less confusing to some users.")
 
+(defvar read-char-choice-hide-cursor nil
+  "Whether to hide cursor during `read-char-choice'.")
+
 (defun read-char-choice (prompt chars &optional inhibit-keyboard-quit)
   "Read and return one of CHARS, prompting for PROMPT.
 Any input that is not one of CHARS is ignored.
@@ -3050,7 +3053,8 @@ read-char-choice
 `read-char-choice-use-read-key' is non-nil, the modal `read-key'
 function is used instead (see `read-char-choice-with-read-key')."
   (if (not read-char-choice-use-read-key)
-      (read-char-from-minibuffer prompt chars)
+      (let ((read-char-from-minibuffer-hide-cursor read-char-choice-hide-cursor))
+        (read-char-from-minibuffer prompt chars))
     (read-char-choice-with-read-key prompt chars inhibit-keyboard-quit)))
 
 (defun read-char-choice-with-read-key (prompt chars &optional inhibit-keyboard-quit)
@@ -3066,7 +3070,7 @@ read-char-choice-with-read-key
   (unless (consp chars)
     (error "Called `read-char-choice' without valid char choices"))
   (let (char done show-help (helpbuf " *Char Help*"))
-    (let ((cursor-in-echo-area t)
+    (let ((cursor-in-echo-area (not read-char-choice-hide-cursor))
           (executing-kbd-macro executing-kbd-macro)
 	  (esc-flag nil))
       (save-window-excursion	      ; in case we call help-form-show
@@ -3224,6 +3228,9 @@ read-char-from-minibuffer-insert-other
     (minibuffer-message "Wrong answer")
     (sit-for 2)))
 
+(defvar read-char-from-minibuffer-hide-cursor nil
+  "Whether to hide cursor during `read-char-from-minibuffer'.")
+
 (defun read-char-from-minibuffer (prompt &optional chars history)
   "Read a character from the minibuffer, prompting for it with PROMPT.
 Like `read-char', but uses the minibuffer to read and return a character.
@@ -3268,8 +3275,12 @@ read-char-from-minibuffer
          ;; Protect this-command when called from pre-command-hook (bug#45029)
          (this-command this-command)
          (result
-          (read-from-minibuffer prompt nil map nil
-                                (or history 'empty-history)))
+          (minibuffer-with-setup-hook
+              (lambda ()
+                (setq-local cursor-type
+                            (not read-char-from-minibuffer-hide-cursor)))
+            (read-from-minibuffer prompt nil map nil
+                                  (or history 'empty-history))))
          (char
           (if (> (length result) 0)
               ;; We have a string (with one character), so return the first one.
-- 
2.33.0