Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: getquery() acts funny with multibyte characters



On Thu, 26 Jun 2008 11:45:02 +0200
"Mikael Magnusson" <mikachu@xxxxxxxxx> wrote:
> If you misspell a command or run rm * (there are probably other cases
> too) you get a prompt that asks you for y/n or whatever is applicable.
> If you write a multibyte character at this prompt, zsh will erase a
> character of the prompt for every extra byte in the character, ie
> entering å (U+E5) erases one character and entering は (U+306F) erases
> two. It seems to happen with unsetopt multibyte too.
> 
> The function seems to emit "\b \b" to erase what you wrote if it isn't
> a valid response, which seems a bit strange. Wouldn't it be more sane
> to turn off echoing and simply print the valid response instead?

This might work, if the mailing list ever shows up.

Index: Src/utils.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/utils.c,v
retrieving revision 1.194
diff -u -r1.194 utils.c
--- Src/utils.c	10 Jun 2008 08:50:52 -0000	1.194
+++ Src/utils.c	30 Jun 2008 21:43:42 -0000
@@ -2069,9 +2069,8 @@
     return (getquery("ny", 1) == 'y');
 }
 
-/**/
-int
-read1char(void)
+static int
+read1char(int echo)
 {
     char c;
 
@@ -2079,6 +2078,8 @@
 	if (errno != EINTR || errflag || retflag || breaks || contflag)
 	    return -1;
     }
+    if (echo)
+	write(SHTTY, &c, 1);
     return STOUC(c);
 }
 
@@ -2105,12 +2106,26 @@
 int
 getquery(char *valid_chars, int purge)
 {
-    int c, d;
+    int c, d, nl = 0;
     int isem = !strcmp(term, "emacs");
+    struct ttyinfo ti;
 
     attachtty(mypgrp);
+
+    gettyinfo(&ti);
+#ifdef HAS_TIO
+    ti.tio.c_lflag &= ~ECHO;
+    if (!isem) {
+	ti.tio.c_lflag &= ~ICANON;
+	ti.tio.c_cc[VMIN] = 1;
+	ti.tio.c_cc[VTIME] = 0;
+    }
+#else
+    ti.sgttyb.sg_flags &= ~ECHO;
     if (!isem)
-	setcbreak();
+	ti.sgttyb.sg_flags |= CBREAK;
+#endif
+    settyinfo(&ti);
 
     if (noquery(purge)) {
 	if (!isem)
@@ -2119,7 +2134,7 @@
 	return 'n';
     }
 
-    while ((c = read1char()) >= 0) {
+    while ((c = read1char(0)) >= 0) {
 	if (c == 'Y')
 	    c = 'y';
 	else if (c == 'N')
@@ -2131,17 +2146,18 @@
 	    break;
 	}
 	if (strchr(valid_chars, c)) {
-	    write(SHTTY, "\n", 1);
+	    nl = 1;
 	    break;
 	}
 	zbeep();
-	if (icntrl(c))
-	    write(SHTTY, "\b \b", 3);
-	write(SHTTY, "\b \b", 3);
     }
+    write(SHTTY, &c, 1);
+    if (nl)
+	write(SHTTY, "\n", 1);
+
     if (isem) {
 	if (c != '\n')
-	    while ((d = read1char()) >= 0 && d != '\n');
+	    while ((d = read1char(1)) >= 0 && d != '\n');
     } else {
 	if (c != '\n' && !valid_chars) {
 #ifdef MULTIBYTE_SUPPORT
@@ -2159,19 +2175,17 @@
 
 		    if (ret != MB_INCOMPLETE)
 			break;
-		    c = read1char();
+		    c = read1char(1);
 		    if (c < 0)
 			break;
 		    cc = (char)c;
 		}
 	    }
 #endif
-	    settyinfo(&shttyinfo);
 	    write(SHTTY, "\n", 1);
 	}
-	else
-	    settyinfo(&shttyinfo);
     }
+    settyinfo(&shttyinfo);
     return c;
 }
 

-- 
Peter Stephenson <p.w.stephenson@xxxxxxxxxxxx>
Web page now at http://homepage.ntlworld.com/p.w.stephenson/



Messages sorted by: Reverse Date, Date, Thread, Author