--- src/LYStrings.c.orig	Wed Feb 17 23:29:33 1999
+++ src/LYStrings.c	Mon Feb 22 14:52:34 1999
@@ -24,6 +24,7 @@
 extern unsigned short *LYKbLayout;
 extern BOOL HTPassHighCtrlRaw;
 extern HTCJKlang HTCJK;
+extern HTkcode kanji_code;
 
 /*Allowing the user to press tab when entering URL to get the closest
   match in the closet*/
@@ -1737,6 +1738,9 @@
 #define DspWdth  edit->dspwdth
 #define DspStart edit->xpan
 #define Margin	 edit->margin
+#ifdef SUPPORT_MULTIBYTE_EDIT
+#define KanjiBuf edit->kanji_buf
+#endif /* SUPPORT_MULTIBYTE_EDIT */
 
 PUBLIC void LYSetupEdit ARGS4(
 	EDREC *,	edit,
@@ -1751,6 +1755,9 @@
     edit->pad	= ' ';
     edit->dirty = TRUE;
     edit->panon = FALSE;
+#ifdef SUPPORT_MULTIBYTE_EDIT
+    edit->kanji_buf = '\0';
+#endif /* SUPPORT_MULTIBYTE_EDIT */
 
     StrLen  = strlen(old);
     MaxLen  = maxstr;
@@ -1791,6 +1798,31 @@
     }
 }
 
+#ifdef SUPPORT_MULTIBYTE_EDIT
+PRIVATE int prev_pos ARGS2(
+	EDREC *,	edit,
+	int,		pos)
+{
+    int i = 0;
+
+    if (pos <= 0)
+	return 0;
+    if (HTCJK == NOCJK)
+	return (pos - 1);
+    else {
+	while (i < pos - 1) {
+	    if (!isascii(Buf[i]))
+		i++;
+	    i++;
+	}
+	if (i == pos)
+	    return (i - 2);
+	else
+	    return i;
+    }
+}
+#endif /* SUPPORT_MULTIBYTE_EDIT */
+
 PUBLIC int LYEdit1 ARGS4(
 	EDREC *,	edit,
 	int,		ch,
@@ -1807,6 +1839,13 @@
 
     if (MaxLen <= 0)
 	return(0); /* Be defensive */
+#ifdef SUPPORT_MULTIBYTE_EDIT
+    if (HTCJK != NOCJK && (KanjiBuf) &&
+	action != LYE_AIX && action != LYE_CHAR) {
+	KanjiBuf = '\0';
+	return(0);
+    }
+#endif /* SUPPORT_MULTIBYTE_EDIT */
 
     length = strlen(&Buf[0]);
     StrLen = length;
@@ -1839,18 +1878,47 @@
 	/*
 	 *  ch is printable or ISO-8859-1 escape character.
 	 */
+#ifdef SUPPORT_MULTIBYTE_EDIT
+	if (HTCJK != NOCJK && !(KanjiBuf) && (ch & 0x80)) {
+	    if (kanji_code == SJIS) {
+		unsigned char hi, lo;
+
+		JISx0201TO0208_SJIS((unsigned char)ch, &hi, &lo);
+		KanjiBuf = hi;
+		ch = (char)lo;
+	    } else {
+		KanjiBuf = (unsigned char) ch;
+		return(0);
+	    }
+	}
+	if (HTCJK != NOCJK && KanjiBuf &&
+	    Pos <= (MaxLen) && StrLen < (MaxLen)) {
+	    for(i = length; i >= Pos; i--)    /* Make room */
+		Buf[i+2] = Buf[i];
+	    Buf[length+2]='\0';
+	    Buf[Pos] = KanjiBuf;
+	    Buf[Pos+1] = (unsigned char) ch;
+	    Pos += 2;
+	    KanjiBuf = '\0';
+	} else if (Pos <= (MaxLen) && StrLen < (MaxLen)) {
+#else /* !SUPPORT_MULTIBYTE_EDIT */
 	if (Pos <= (MaxLen) && StrLen < (MaxLen)) {
+#endif
 	    for(i = length; i >= Pos; i--)    /* Make room */
 		Buf[i+1] = Buf[i];
 	    Buf[length+1]='\0';
 	    Buf[Pos] = (unsigned char) ch;
 	    Pos++;
-	} else if (maxMessage) {
-	    _statusline(MAXLEN_REACHED_DEL_OR_MOV);
+	} else {
+	    if (maxMessage) {
+	        _statusline(MAXLEN_REACHED_DEL_OR_MOV);
+	    }
+	    return(ch);
 	}
 	break;
 
     case LYE_BACKW:
+#ifndef SUPPORT_MULTIBYTE_EDIT
 	/*
 	 *  Backword.
 	 *  Definition of word is very naive: 1 or more a/n characters.
@@ -1859,16 +1927,58 @@
 	    Pos--;
 	while (Pos &&  isalnum(Buf[Pos-1]))
 	    Pos--;
+#else /* SUPPORT_MULTIBYTE_EDIT */
+        /*
+	 *  Backword.
+	 *  Definition of word is very naive: 1 or more a/n characters,
+	 *  or 1 or more multibyte character.
+	 */
+	{
+	    int pos0;
+
+	    pos0 = prev_pos(edit, Pos);
+	    while (Pos &&
+		   (HTCJK == NOCJK || isascii(Buf[pos0])) &&
+		   !isalnum(Buf[pos0])) {
+		Pos = pos0;
+		pos0 = prev_pos(edit, Pos);
+	    }
+	    if (HTCJK != NOCJK && !isascii(Buf[pos0])) {
+		while (Pos && !isascii(Buf[pos0])) {
+		    Pos = pos0;
+		    pos0 = prev_pos(edit, Pos);
+		}
+	    } else {
+		while (Pos && isascii(Buf[pos0]) && isalnum(Buf[pos0])) {
+		    Pos = pos0;
+		    pos0 = prev_pos(edit, Pos);
+		}
+	    }
+	}
+#endif /* SUPPORT_MULTIBYTE_EDIT */
 	break;
 
     case LYE_FORWW:
 	/*
 	 *  Word forward.
 	 */
+#ifndef SUPPORT_MULTIBYTE_EDIT
 	while (isalnum(Buf[Pos]))
 	    Pos++;   /* '\0' is not a/n */
 	while (!isalnum(Buf[Pos]) && Buf[Pos])
 	    Pos++ ;
+#else /* SUPPORT_MULTIBYTE_EDIT */
+	if (HTCJK != NOCJK && !isascii(Buf[Pos])) {
+	    while (!isascii(Buf[Pos]))
+		Pos += 2;
+	} else {
+	    while (isascii(Buf[Pos]) && isalnum(Buf[Pos]))
+		Pos++;	/* '\0' is not a/n */
+	}
+	while ((HTCJK == NOCJK || isascii(Buf[Pos])) &&
+	       !isalnum(Buf[Pos]) && Buf[Pos])
+	    Pos++;
+#endif /* SUPPORT_MULTIBYTE_EDIT */
 	break;
 
     case LYE_ERASE:
@@ -1948,6 +2058,10 @@
 	 */
 	if (Pos >= length)
 	    break;
+#ifdef SUPPORT_MULTIBYTE_EDIT
+	if (HTCJK != NOCJK && !isascii(Buf[Pos]))
+	    Pos++;
+#endif
 	Pos++;
 	/* fall through - DO NOT RELOCATE the LYE_DELN case wrt LYE_DELP */
 
@@ -1955,12 +2069,30 @@
 	/*
 	 *  Delete preceding character.
 	 */
+#ifndef SUPPORT_MULTIBYTE_EDIT
 	if (length == 0 || Pos == 0)
 	    break;
 	Pos--;
 	for (i = Pos; i < length; i++)
 	    Buf[i] = Buf[i+1];
 	i--;
+#else /* SUPPORT_MULTIBYTE_EDIT */
+	{
+	    int offset = 1;
+	    int pos0 = Pos;
+
+	    if (length == 0 || Pos == 0)
+		break;
+	    if (HTCJK != NOCJK) {
+		Pos = prev_pos(edit, pos0);
+		offset = pos0 - Pos;
+	    } else
+		Pos--;
+	    for (i = Pos; i < length; i++)
+		Buf[i] = Buf[i + offset];
+	    i -= offset;
+	}
+#endif /* SUPPORT_MULTIBYTE_EDIT */
 	Buf[i] = 0;
 	break;
 
@@ -1968,8 +2100,16 @@
 	/*
 	 *  Move cursor to the right.
 	 */
+#ifndef SUPPORT_MULTIBYTE_EDIT
 	if (Pos < length)
 	    Pos++;
+#else /* SUPPORT_MULTIBYTE_EDIT */
+	if (Pos < length) {
+	    Pos++;
+	    if (HTCJK != NOCJK && !isascii(Buf[Pos-1]))
+		Pos++;
+	}
+#endif /* SUPPORT_MULTIBYTE_EDIT */
 	break;
 
     case LYE_BACK:
@@ -1977,7 +2117,14 @@
 	 *  Left-arrow move cursor to the left.
 	 */
 	if (Pos > 0)
+#ifndef SUPPORT_MULTIBYTE_EDIT
 	    Pos--;
+#else /* SUPPORT_MULTIBYTE_EDIT */
+	    if (HTCJK != NOCJK)
+		Pos = prev_pos(edit, Pos);
+	    else
+		Pos--;
+#endif /* SUPPORT_MULTIBYTE_EDIT */
 	break;
 
     case LYE_UPPER:
@@ -2006,6 +2153,19 @@
     int padsize;
     char *str;
     char buffer[3];
+#ifdef SUPPORT_MULTIBYTE_EDIT
+    int begin_multi = 0;
+    int end_multi = 0;
+    int prev_start;
+    BOOL force_refresh = FALSE;
+
+    if (HTCJK != NOCJK && KanjiBuf) /* don't refresh */
+	return;
+    prev_start = DspStart;
+    if (Buf[Pos] & 0x80) {
+	force_refresh = TRUE;
+    }
+#endif /* SUPPORT_MULTIBYTE_EDIT */
 
     buffer[0] = buffer[1] = buffer[2] = '\0';
     if (!edit->dirty || (DspWdth == 0))
@@ -2030,16 +2190,57 @@
  *   data entry at low baudrates.
  */
     if ((DspStart + DspWdth) <= length)
+#ifndef SUPPORT_MULTIBYTE_EDIT
 	if (Pos >= (DspStart + DspWdth) - Margin)
 	    DspStart=(Pos - DspWdth) + Margin;
+#else /* SUPPORT_MULTIBYTE_EDIT */
+	if (Pos >= (DspStart + DspWdth) - Margin) {
+	    if (HTCJK != NOCJK) {
+		int tmp = (Pos - DspWdth) + Margin;
+
+		while (DspStart < tmp) {
+		    if (!isascii(Buf[DspStart]))
+			DspStart++;
+		    DspStart++;
+		}
+	    } else
+		DspStart=(Pos - DspWdth) + Margin;
+	}
+#endif /* SUPPORT_MULTIBYTE_EDIT */
 
     if (Pos < DspStart + Margin) {
+#ifndef SUPPORT_MULTIBYTE_EDIT
 	DspStart = Pos - Margin;
 	if (DspStart < 0)
 	    DspStart = 0;
+#else /* SUPPORT_MULTIBYTE_EDIT */
+	if (HTCJK != NOCJK) {
+	    int tmp = Pos - Margin;
+
+	    DspStart = 0;
+	    while (DspStart < tmp) {
+		if (!isascii(Buf[DspStart]))
+		    DspStart++;
+		DspStart++;
+	    }
+	} else {
+	    DspStart = Pos - Margin;
+	    if (DspStart < 0)
+		DspStart = 0;
+	}
+#endif /* SUPPORT_MULTIBYTE_EDIT */
+    }
+#ifdef SUPPORT_MULTIBYTE_EDIT
+    if (HTCJK != NOCJK && DspStart != prev_start) {
+	force_refresh = TRUE;
     }
+#endif
 
     str = &Buf[DspStart];
+#ifdef SUPPORT_MULTIBYTE_EDIT
+    if (HTCJK != NOCJK && !isascii(str[0]))
+	begin_multi = 1;
+#endif /* SUPPORT_MULTIBYTE_EDIT */
 
     nrdisplayed = length-DspStart;
     if (nrdisplayed > DspWdth)
@@ -2058,15 +2259,29 @@
 		    !(LYCharSet_UC[current_char_set].like8859
 		      & UCT_R_8859SPECL))))) {
 		addch(' ');
+#ifdef SUPPORT_MULTIBYTE_EDIT
+		end_multi = 0;
+#endif /* SUPPORT_MULTIBYTE_EDIT */
 	    } else {
 		/* For CJK strings, by Masanobu Kimura */
 		if (HTCJK != NOCJK && !isascii(buffer[0])) {
+#ifndef SUPPORT_MULTIBYTE_EDIT
 		    if (i < (nrdisplayed - 1))
 			buffer[1] = str[++i];
+#else /* SUPPORT_MULTIBYTE_EDIT */
+		    if (i < (nrdisplayed - 1)) {
+		        buffer[1] = str[++i];
+			end_multi = 1;
+		    } else
+			end_multi = 0;
+#endif /* SUPPORT_MULTIBYTE_EDIT */
 		    addstr(buffer);
 		    buffer[1] = '\0';
 		} else {
 		    addstr(buffer);
+#ifdef SUPPORT_MULTIBYTE_EDIT
+		    end_multi = 0;
+#endif /* SUPPORT_MULTIBYTE_EDIT */
 		}
 	    }
     }
@@ -2083,16 +2298,39 @@
      */
     if (edit->panon) {
 	if ((DspStart + nrdisplayed) < length) {
+#ifndef SUPPORT_MULTIBYTE_EDIT
 	    move(edit->sy, edit->sx+nrdisplayed-1);
 	    addch('}');
+#else /* SUPPORT_MULTIBYTE_EDIT */
+	    if (end_multi) {
+		move(edit->sy, edit->sx+nrdisplayed-2);
+		addstr(" }");
+	    } else {
+	        move(edit->sy, edit->sx+nrdisplayed-1);
+	        addch('}');
+	    }
+#endif /* SUPPORT_MULTIBYTE_EDIT */
 	}
 	if (DspStart) {
 	    move(edit->sy, edit->sx);
+#ifndef SUPPORT_MULTIBYTE_EDIT
 	    addch('{');
+#else /* SUPPORT_MULTIBYTE_EDIT */
+	    if (begin_multi)
+		addstr("{ ");
+	    else
+		addch('{');
+#endif /* SUPPORT_MULTIBYTE_EDIT */
 	}
     }
 
     move(edit->sy, edit->sx + Pos - DspStart);
+#ifdef SUPPORT_MULTIBYTE_EDIT
+#if (!USE_SLANG && !defined(USE_MULTIBYTE_CURSES))
+    if (HTCJK != NOCJK && force_refresh)
+        lynx_force_repaint();
+#endif /* !USE_SLANG && !defined(USE_MULTIBYTE_CURSES) */
+#endif /* SUPPORT_MULTIBYTE_EDIT */
     refresh();
 }
 
@@ -2157,12 +2395,21 @@
 	     *	character in the current display character set.
 	     *	Otherwise, we treat this as LYE_ENTER.
 	     */
+#ifndef SUPPORT_MULTIBYTE_EDIT
 	    if (ch != '\t' &&
 		(HTCJK != NOCJK ||
 		 LYlowest_eightbit[current_char_set] <= 0x97)) {
 		LYLineEdit(&MyEdit,ch, FALSE);
 		break;
 	    }
+#else /* SUPPORT_MULTIBYTE_EDIT (quick hack) */
+	    if (ch != '\t' &&
+		(HTCJK != NOCJK ||
+		 LYlowest_eightbit[current_char_set] <= 0x97)) {
+		goto normal;
+	    }
+	    /* fall through */
+#endif /* SUPPORT_MULTIBYTE_EDIT */
 	case LYE_ENTER:
 	    /*
 	     *	Terminate the string and return.
@@ -2195,8 +2442,11 @@
 	    break;
 
 	default:
-	    LYLineEdit(&MyEdit, ch, FALSE);
-	}
+#ifdef SUPPORT_MULTIBYTE_EDIT
+normal:
+#endif /* SUPPORT_MULTIBYTE_EDIT */
+            LYLineEdit(&MyEdit, ch, FALSE);
+        }
     }
 }
 
