Improve keyboard input of UTF-8.

This commit is contained in:
Renaud 2021-07-31 14:45:35 +08:00
parent d48120a557
commit 486d01297d
1 changed files with 26 additions and 28 deletions

54
input.c
View File

@ -324,13 +324,32 @@ int tgetc(void)
return c; return c;
} }
/* GET1KEY: Get one keystroke. The only prefixs legal here are the SPEC static int get1unicode( int *k) {
/* Accept UTF-8 sequence */
int c = *k ;
if( c > 0xC1 && c <= 0xF4) {
char utf[ 4] ;
char cc ;
utf[ 0] = c ;
utf[ 1] = cc = tgetc() ;
if( (c & 0x20) && ((cc & 0xC0) == 0x80)) { /* at least 3 bytes and a valid encoded char */
utf[ 2] = cc = tgetc() ;
if( (c & 0x10) && ((cc & 0xC0) == 0x80)) /* at least 4 bytes and a valid encoded char */
utf[ 3] = tgetc() ;
}
return utf8_to_unicode( utf, 0, sizeof utf, (unicode_t *) k) ;
} else
return 1 ;
}
/* GET1KEY: Get one keystroke. The only prefixes legal here are the SPEC
and CTRL prefixes. */ and CTRL prefixes. */
int get1key( void) { int get1key( void) {
int c ;
/* get a keystroke */ /* get a keystroke */
c = tgetc(); int c = tgetc() ;
get1unicode( &c) ;
if( (c >= 0x00 && c <= 0x1F) || c == 0x7F) /* C0 control -> C- */ if( (c >= 0x00 && c <= 0x1F) || c == 0x7F) /* C0 control -> C- */
c ^= CTRL | 0x40 ; c ^= CTRL | 0x40 ;
@ -341,26 +360,6 @@ int get1key( void) {
/* GETCMD: Get a command from the keyboard. Process all applicable /* GETCMD: Get a command from the keyboard. Process all applicable
prefix keys */ prefix keys */
static int get1unicode( int *k) {
/* Accept UTF-8 sequence */
int c = *k ;
if( c > 0xC1 && c <= 0xF4) {
char utf[ 4] ;
char cc ;
utf[ 0] = c ;
utf[ 1] = cc = get1key() ;
if( (c & 0x20) && ((cc & 0xC0) == 0x80)) { /* at least 3 bytes and a valid encoded char */
utf[ 2] = cc = get1key() ;
if( (c & 0x10) && ((cc & 0xC0) == 0x80)) /* at least 4 bytes and a valid encoded char */
utf[ 3] = get1key() ;
}
return utf8_to_unicode( utf, 0, sizeof utf, (unicode_t *) k) ;
} else
return 1 ;
}
int getcmd(void) int getcmd(void)
{ {
int c; /* fetched keystroke */ int c; /* fetched keystroke */
@ -465,7 +464,7 @@ handle_CSI:
} }
#ifdef CYGWIN #ifdef CYGWIN
get1unicode( &c) ; // get1unicode( &c) ;
#endif #endif
/* otherwise, just return it */ /* otherwise, just return it */
@ -696,9 +695,8 @@ int getstring( const char *prompt, char *buf, int nbuf, int eolchar)
quote_f = TRUE ; quote_f = TRUE ;
else { else {
/* store as it is */ /* store as it is */
int n ; // n = get1unicode( &c) ; /* fetch multiple bytes */
int n = 4 ; /* long utf-8 encoding */
n = get1unicode( &c) ; /* fetch multiple bytes */
if( cpos + n < nbuf) { if( cpos + n < nbuf) {
cpos += unicode_to_utf8( c, &buf[ cpos]) ; cpos += unicode_to_utf8( c, &buf[ cpos]) ;
echov( c) ; echov( c) ;