zsh-workers
 help / color / mirror / code / Atom feed
* PATCH: curses input
@ 2007-10-26 20:13 Peter Stephenson
  2007-10-26 21:56 ` Peter Stephenson
  0 siblings, 1 reply; 4+ messages in thread
From: Peter Stephenson @ 2007-10-26 20:13 UTC (permalink / raw)
  To: Zsh hackers list

"zcurses input" reads a single character with cbreak and noecho set.
Handling nocbreak would be clumsy and the manual says you should use
noecho with cbreak.  There are all sorts of possible extensions but this
is already good enough to do most of what you want.

What would be quite nice is to handle keypad mode (see curs_getch(3X)),
which would probably mean parsing KEY_* values and converting them to
strings to return.  We could just return the corresponding integer
values, but it seems a cop out.

In principle it would be good to have proper zsh input in curses
mode, but that's pie in the sky at the moment.

Index: configure.ac
===================================================================
RCS file: /cvsroot/zsh/zsh/configure.ac,v
retrieving revision 1.73
diff -u -r1.73 configure.ac
--- configure.ac	24 Oct 2007 08:47:43 -0000	1.73
+++ configure.ac	26 Oct 2007 20:11:00 -0000
@@ -1134,7 +1134,7 @@
 	       brk sbrk \
 	       pathconf sysconf \
 	       tgetent tigetflag tigetnum tigetstr setupterm initscr \
-	       setcchar waddwstr \
+	       setcchar waddwstr wget_wch \
 	       pcre_compile pcre_study pcre_exec \
 	       nl_langinfo \
 	       erand48 open_memstream \
Index: Doc/Zsh/mod_curses.yo
===================================================================
RCS file: /cvsroot/zsh/zsh/Doc/Zsh/mod_curses.yo,v
retrieving revision 1.10
diff -u -r1.10 mod_curses.yo
--- Doc/Zsh/mod_curses.yo	24 Oct 2007 22:23:10 -0000	1.10
+++ Doc/Zsh/mod_curses.yo	26 Oct 2007 20:11:01 -0000
@@ -19,7 +19,8 @@
 xitem(tt(zcurses) tt(string) var(targetwin) var(string) )
 xitem(tt(zcurses) tt(border) var(targetwin) var(border) )(
 xitem(tt(zcurses) tt(attr) var(targetwin) [ var({+/-}attribute) | var(fg_col)tt(/)var(bg_col) ] [...])
-item(tt(zcurses) tt(scroll) [ tt(on) | tt(off) | {+/-}var(lines) ])(
+xitem(tt(zcurses) tt(scroll) [ tt(on) | tt(off) | {+/-}var(lines) ])
+item(tt(input) var(targetwin) [ var(param) ])(
 Manipulate curses windows.  All uses of this command should be
 bracketed by `tt(zcurses init)' to initialise use of curses, and
 `tt(zcurses end)' to end it; omitting `tt(zcurses end)' can cause
@@ -62,7 +63,11 @@
 of lines without changing the current cursor position (which therefore
 appears to move in the opposite direction relative to the window).
 In the second case, if scrolling is tt(off) it is temporarily turned tt(on)
-to allow the window to be scrolled,
+to allow the window to be scrolled.
+
+tt(input) reads a single character from the window without echoing
+it back.  If var(param) is supplied the character is assigned to the
+parameter var(param), else it is assigned to the parameter var(REPLY).
 )
 enditem()
 
Index: Src/Modules/curses.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/Modules/curses.c,v
retrieving revision 1.22
diff -u -r1.22 curses.c
--- Src/Modules/curses.c	24 Oct 2007 22:23:10 -0000	1.22
+++ Src/Modules/curses.c	26 Oct 2007 20:11:01 -0000
@@ -43,6 +43,7 @@
 #ifndef MULTIBYTE_SUPPORT
 # undef HAVE_SETCCHAR
 # undef HAVE_WADDWSTR
+# undef HAVE_WGET_WCH
 #endif
 
 #ifdef HAVE_SETCCHAR
@@ -335,6 +336,16 @@
 	    zcurses_colorpairs->printnode   = NULL;
 
 	}
+	/*
+	 * We use cbreak mode because we don't want line buffering
+	 * on input since we'd just need to loop over characters.
+	 * We use noecho since the manual says that's the right
+	 * thing to do with cbreak.
+	 *
+	 * Turn these on immediately to catch typeahead.
+	 */
+	cbreak();
+	noecho();
 	gettyinfo(&curses_tty_state);
     } else {
 	settyinfo(&curses_tty_state);
@@ -669,6 +680,59 @@
 }
 
 
+static int
+zccmd_input(const char *nam, char **args)
+{
+    LinkNode node;
+    ZCWin w;
+    char *var;
+#ifdef HAVE_WGET_WCH
+    wint_t wi;
+    VARARR(char, instr, 2*MB_CUR_MAX+1);
+#else
+    int ci;
+    instr[3];
+#endif
+
+    node = zcurses_validate_window(args[0], ZCURSES_USED);
+    if (node == NULL) {
+	zwarnnam(nam, "%s: %s", zcurses_strerror(zc_errno), args[0]);
+	return 1;
+    }
+
+    w = (ZCWin)getdata(node);
+
+#ifdef HAVE_WGET_WCH
+    if (wget_wch(w->win, &wi) == ERR)
+	return 1;
+    wctomb(instr, (wchar_t)wi);
+    if (!*instr) {
+	instr[0] = Meta;
+	instr[1] = '\0' ^ 32;
+	instr[2] = '\0';
+    } else {
+	(void)metafy(instr, strlen(instr), META_NOALLOC);
+    }
+#else
+    ci = wgetch(w->win);
+    if (imeta(ci)) {
+	instr[0] = Meta;
+	instr[1] = (char)ci ^ 32;
+	instr[2] = '\0';
+    } else {
+	instr[0] = (char)ci;
+	instr[1] = '\0';
+    }
+#endif
+    if (args[1])
+	var = args[1];
+    else
+	var = "REPLY";
+    if (!setsparam(var, ztrdup(instr)))
+	return 1;
+    return 0;
+}
+
 /*********************
   Main builtin handler
  *********************/
@@ -693,6 +757,7 @@
 	{"end", zccmd_endwin, 0, 0},
 	{"attr", zccmd_attr, 2, -1},
 	{"scroll", zccmd_scroll, 2, 2},
+	{"input", zccmd_input, 1, 2},
 	{NULL, (zccmd_t)0, 0, 0}
     };
 
 

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


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2007-10-29 14:04 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-10-26 20:13 PATCH: curses input Peter Stephenson
2007-10-26 21:56 ` Peter Stephenson
2007-10-29 13:29   ` Clint Adams
2007-10-29 14:04     ` Peter Stephenson

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).