diff -cr lynx2-7-1/WWW/Library/Implementation/SGML.c lynx2-7-1.new/WWW/Library/Implementation/SGML.c
*** lynx2-7-1/WWW/Library/Implementation/SGML.c	Wed Jul 23 07:35:30 1997
--- lynx2-7-1.new/WWW/Library/Implementation/SGML.c	Mon Aug 18 13:55:05 1997
***************
*** 86,91 ****
--- 86,94 ----
  		S_esc_dq, S_dollar_dq, S_paren_dq, S_nonascii_text_dq,
  		S_dollar_paren_dq,
  		S_in_kanji, S_junk_tag} state;
+     HTkcode			kcode;
+     unsigned char		kanji_buf;
+     int				in_sjis;
  #ifdef CALLERDATA
      void *			callerData;
  #endif /* CALLERDATA */
***************
*** 889,895 ****
--- 892,912 ----
      switch(context->state) {
  
      case S_in_kanji:
+ 	if (HTCJK == JAPANESE && context->kcode == NOKANJI) {
+ 	    unsigned char hi = context->kanji_buf;
+ 	    unsigned char lo = unsign_c;
+ 
+ 	    if (IS_SJIS(hi, lo, context->in_sjis) &&
+ 		IS_EUC(hi, lo)) {
+ 		context->kcode = NOKANJI;
+ 	    } else if (IS_SJIS(hi, lo, context->in_sjis)) {
+ 		context->kcode = SJIS;
+ 	    } else if (IS_EUC(hi, lo)) {
+ 		context->kcode = EUC;
+ 	    }
+ 	}
  	context->state = S_text;
+ 	context->kanji_buf = '\0';
  	PUTC(c);
  	break;
  
***************
*** 899,905 ****
  	    **  Setting up for Kanji multibyte handling (based on
  	    **  Takuya ASADA's (asada@three-a.co.jp) CJK Lynx). - FM
  	    */
! 	    context->state = S_in_kanji;
  	    PUTC(c);
  	    break;
  	} else if (HTCJK != NOCJK && c == '\033') {
--- 916,927 ----
  	    **  Setting up for Kanji multibyte handling (based on
  	    **  Takuya ASADA's (asada@three-a.co.jp) CJK Lynx). - FM
  	    */
! 	    if ((HTCJK != JAPANESE) ||
! 		(context->kcode != SJIS) ||
! 		(unsign_c < 161) || (unsign_c > 223)) {
! 	        context->state = S_in_kanji;
! 	        context->kanji_buf = unsign_c;
! 	    }
  	    PUTC(c);
  	    break;
  	} else if (HTCJK != NOCJK && c == '\033') {
***************
*** 2232,2237 ****
--- 2254,2262 ----
      context->actions = (HTStructuredClass*)(((HTStream*)target)->isa);
      					/* Ugh: no OO */
      context->state = S_text;
+     context->kcode = NOKANJI;
+     context->kanji_buf = '\0';
+     context->in_sjis = 0;
      context->element_stack = 0;			/* empty */
  #ifdef CALLERDATA		  
      context->callerData = (void*) callerData;
diff -cr lynx2-7-1/src/GridText.c lynx2-7-1.new/src/GridText.c
*** lynx2-7-1/src/GridText.c	Sun Aug 17 16:27:16 1997
--- lynx2-7-1.new/src/GridText.c	Mon Aug 18 13:54:29 1997
***************
*** 1791,1797 ****
  		/*
  		 *  JIS X0201 Kana in SJIS support. - by ASATAKU
  		 */
! 	        if ((text->kcode == SJIS) &&
  		    ((unsigned char)ch >= 0xA1) &&
  		    ((unsigned char)ch <= 0xDF)) {
  		    unsigned char c = (unsigned char)ch;
--- 1791,1798 ----
  		/*
  		 *  JIS X0201 Kana in SJIS support. - by ASATAKU
  		 */
! 	        if (((text->kcode == SJIS) ||
! 		     (text->kcode == NOKANJI) && (text->in_sjis)) &&
  		    ((unsigned char)ch >= 0xA1) &&
  		    ((unsigned char)ch <= 0xDF)) {
  		    unsigned char c = (unsigned char)ch;
***************
*** 7022,7025 ****
--- 7023,7039 ----
       *  or other directives not to use a cached rendition. - FM
       */
      return(FALSE);
+ }
+ 
+ PUBLIC HTkcode HText_getKcode ARGS1(
+ 	HText *, 	text)
+ {
+       return text->kcode;
+ }
+ 
+ PUBLIC void HText_updateKcode ARGS2(
+ 	HText *,	text,
+ 	HTkcode,	kcode)
+ {
+       text->kcode = kcode;
  }
diff -cr lynx2-7-1/src/GridText.h lynx2-7-1.new/src/GridText.h
*** lynx2-7-1/src/GridText.h	Wed Aug  6 18:18:48 1997
--- lynx2-7-1.new/src/GridText.h	Mon Aug 18 13:51:37 1997
***************
*** 10,15 ****
--- 10,19 ----
  #include "HTForms.h"
  #endif /* HTFORMS_H */
  
+ #ifndef HTCJK_H
+ #include "HTCJK.h"
+ #endif /* HTCJK_H */
+ 
  #define LY_UNDERLINE_START_CHAR	'\003'
  #define LY_UNDERLINE_END_CHAR	'\004'
  #define LY_BOLD_START_CHAR	'\005'
***************
*** 184,188 ****
--- 188,195 ----
  extern BOOL HText_AreDifferent PARAMS((
  	HTParentAnchor *	anchor,
  	CONST char *		full_address));
+ 
+ extern HTkcode HText_getKcode PARAMS((HText *text));
+ extern void HText_updateKcode PARAMS((HText *text, HTkcode kcode));
  
  #endif /* LYGRIDTEXT_H */
diff -cr lynx2-7-1/src/HTML.c lynx2-7-1.new/src/HTML.c
*** lynx2-7-1/src/HTML.c	Wed Aug 13 22:03:12 1997
--- lynx2-7-1.new/src/HTML.c	Mon Aug 18 13:51:38 1997
***************
*** 55,60 ****
--- 55,61 ----
  
  extern BOOL HTPassEightBitRaw;
  extern HTCJKlang HTCJK;
+ extern HTkcode kanji_code;
  
  extern BOOLEAN HT_Is_Gopher_URL;
  
***************
*** 3985,3990 ****
--- 3986,3992 ----
  	    BOOL UseALTasVALUE = FALSE;
  	    BOOL HaveSRClink = FALSE;
  	    BOOL IsSubmitOrReset = FALSE;
+ 	    HTkcode kcode;
  
  	    /* init */
  	    I.align=NULL; I.accept=NULL; I.checked=NO; I.class=NULL;
***************
*** 4355,4360 ****
--- 4357,4366 ----
  		for (; chars > 0; chars--)
  		    HTML_put_character(me, '_');
  	    } else {
+ 		if (HTCJK != NOCJK) {
+ 		    kcode = HText_getKcode(me->text);
+ 		    HText_updateKcode(me->text, kanji_code);
+ 		}
  		if (me->sp[0].tag_number == HTML_PRE) {
  		    /*
  		     *  We have a submit or reset button in a PRE block,
***************
*** 4393,4398 ****
--- 4399,4406 ----
  		    while (i < chars)
  			HTML_put_character(me, HT_NON_BREAK_SPACE);
  		}
+ 		if (HTCJK != NOCJK)
+ 		    HText_updateKcode(me->text, kcode);
  	    }
  	    HText_setIgnoreExcess(me->text, FALSE);
  	}
diff -cr lynx2-7-1/src/LYBookmark.c lynx2-7-1.new/src/LYBookmark.c
*** lynx2-7-1/src/LYBookmark.c	Sun Jun 29 10:52:50 1997
--- lynx2-7-1.new/src/LYBookmark.c	Mon Aug 18 13:51:39 1997
***************
*** 11,16 ****
--- 11,17 ----
  #include "LYCharUtils.h"
  #include "LYCurses.h"
  #include "GridText.h"
+ #include "HTCJK.h"
  
  #ifdef VMS
  #include "HTVMSUtils.h"
***************
*** 25,30 ****
--- 26,34 ----
  PRIVATE BOOLEAN is_mosaic_hotlist=FALSE;
  PRIVATE char * convert_mosaic_bookmark_file PARAMS((char *filename_buffer));
  
+ extern HTkcode kanji_code;
+ extern HTCJKlang HTCJK;
+ 
  /*
   *  Tries to open a bookmark file for reading, which may be
   *  the default, or based on offering the user a choice from
***************
*** 215,220 ****
--- 219,225 ----
      char *bookmark_URL = NULL;
      char filename_buffer[256];
      char string_buffer[256];
+     char tmp_buffer[512];
      char *Address = NULL;
      char *Title = NULL;
      int i, c;
***************
*** 284,290 ****
       *  Allow user to change the title. - FM
       */
      string_buffer[255] = '\0';
!     LYstrncpy(string_buffer, title, 255);
      convert_to_spaces(string_buffer, FALSE);
      LYMBM_statusline(TITLE_PROMPT);
      LYgetstr(string_buffer, VISIBLE, sizeof(string_buffer), NORECALL);
--- 289,309 ----
       *  Allow user to change the title. - FM
       */
      string_buffer[255] = '\0';
!     if (HTCJK == JAPANESE) {
! 	switch(kanji_code) {
! 	case EUC:
! 	    TO_EUC(title, tmp_buffer);
! 	    break;
! 	case SJIS:
! 	    TO_SJIS(title, tmp_buffer);
! 	    break;
! 	default:
! 	    break;
! 	}
! 	LYstrncpy(string_buffer, tmp_buffer, 255);
!     } else {
! 	LYstrncpy(string_buffer, title, 255);
!     }
      convert_to_spaces(string_buffer, FALSE);
      LYMBM_statusline(TITLE_PROMPT);
      LYgetstr(string_buffer, VISIBLE, sizeof(string_buffer), NORECALL);
***************
*** 300,306 ****
       *  converted to &lt; entities and any ampersands
       *  converted to &amp; entities.  - FM
       */
!     StrAllocCopy(Title, string_buffer);
      LYEntify(&Title, TRUE);
  
      /*
--- 319,330 ----
       *  converted to &lt; entities and any ampersands
       *  converted to &amp; entities.  - FM
       */
!     if (HTCJK == JAPANESE) {
! 	TO_JIS(string_buffer, tmp_buffer);
! 	StrAllocCopy(Title, tmp_buffer);
!     } else {
!         StrAllocCopy(Title, string_buffer);
!     }
      LYEntify(&Title, TRUE);
  
      /*
***************
*** 336,342 ****
       */
      if (first_time) {
  	fprintf(fp,"<head>\n");
! 	LYAddMETAcharsetToFD(fp, -1);
  	fprintf(fp,"<title>%s</title>\n</head>\n",BOOKMARK_TITLE);
  	fprintf(fp,"\
       You can delete links using the remove bookmark command.  It is usually\n\
--- 360,371 ----
       */
      if (first_time) {
  	fprintf(fp,"<head>\n");
! 	if (HTCJK != JAPANESE)
! 	    LYAddMETAcharsetToFD(fp, -1);
! 	else
! 	    fprintf(fp, "<META %s %s>\n",
! 			"http-equiv=\"content-type\"",
! 			"conetnt=\"text/html;charset=iso-2022-jp\""); 
  	fprintf(fp,"<title>%s</title>\n</head>\n",BOOKMARK_TITLE);
  	fprintf(fp,"\
       You can delete links using the remove bookmark command.  It is usually\n\
diff -cr lynx2-7-1/src/LYCharUtils.c lynx2-7-1.new/src/LYCharUtils.c
*** lynx2-7-1/src/LYCharUtils.c	Wed Aug 13 22:03:42 1997
--- lynx2-7-1.new/src/LYCharUtils.c	Mon Aug 18 13:51:40 1997
***************
*** 151,156 ****
--- 151,160 ----
      char *p = *str;
      char *q = NULL, *cp = NULL;
      int amps = 0, lts = 0, gts = 0;
+     enum _state
+         { S_text, S_esc, S_dollar, S_paren,
+           S_nonascii_text, S_dollar_paren } state = S_text;
+     int in_sjis = 0;
      
      if (p == NULL || *p == '\0')
          return;
***************
*** 199,204 ****
--- 203,288 ----
      if ((cp = q) == NULL)
          outofmem(__FILE__, "LYEntify");
      for (p = *str; *p; p++) {
+ 	if (HTCJK != NOCJK) {
+ 	    switch(state) {
+ 		case S_text:
+ 		    if (*p == '\033') {
+ 			state = S_esc;
+ 			*q++ = *p;
+ 			continue;
+                     }
+                     break;
+ 
+ 		case S_esc:
+ 		    if (*p == '$') {
+ 			state = S_dollar;
+ 			*q++ = *p;
+ 			continue;
+ 		    } else if (*p == '(') {
+ 			state = S_paren;
+ 			*q++ = *p;
+ 			continue;
+ 		    } else {
+ 			state = S_text;
+ 			*q++ = *p;
+ 			continue;
+ 		    }
+ 
+ 		case S_dollar:
+ 		    if (*p == '@' || *p == 'B' || *p == 'A') {
+ 			state = S_nonascii_text;
+ 			*q++ = *p;
+ 			continue;
+ 		    } else if (*p == '(') {
+ 			state = S_dollar_paren;
+ 			*q++ = *p;
+ 			continue;
+ 		    } else {
+ 			state = S_text;
+ 			*q++ = *p;
+ 			continue;
+ 		    }
+ 
+ 		case S_dollar_paren:
+ 		    if (*p == 'C') {
+ 			state = S_nonascii_text;
+ 			*q++ = *p;
+ 			continue;
+ 		    } else {
+ 			state = S_text;
+ 			*q++ = *p;
+ 			continue;
+ 		    }
+ 
+ 		case S_paren:
+ 		    if (*p == 'B' || *p == 'J' || *p =='T') {
+ 			state = S_text;
+ 			*q++ = *p;
+ 			continue;
+ 		    } else if (*p == 'I') {
+ 			state = S_nonascii_text;
+ 			*q++ = *p;
+ 			continue;
+ 		    }
+ 
+ 		case S_nonascii_text:
+ 		    if (*p == '\033') 
+ 			state = S_esc;
+ 		    *q++ = *p;
+ 		    continue;
+ 
+ 		default:
+ 		    break;
+ 	    }
+ 	    if (*(p+1) != '\0' &&
+ 		(IS_EUC((unsigned char)*p, (unsigned char)*(p+1)) ||
+ 		 IS_SJIS((unsigned char)*p, (unsigned char)*(p+1), in_sjis) ||
+ 		 IS_BIG5((unsigned char)*p, (unsigned char)*(p+1)))) {
+ 		*q++ = *p++;
+ 		*q++ = *p;
+ 		continue;
+ 	    }
+ 	}
      	if (*p == '&') {
  	    *q++ = '&';
  	    *q++ = 'a';
***************
*** 219,224 ****
--- 303,309 ----
  	    *q++ = *p;
  	}
      }
+     *q = '\0';
      StrAllocCopy(*str, cp);
      FREE(cp);
  }
***************
*** 3221,3226 ****
--- 3306,3312 ----
  	 *  Handle end tag.
  	 */
  	char *ptr;
+ 	HTkcode kcode;
  	if (!me->text)
  	    UPDATE_STYLE;
  
***************
*** 3275,3280 ****
--- 3361,3370 ----
  	     *  Output popup box with the default option to screen,
  	     *  but use non-breaking spaces for output.
  	     */
+ 	    if (HTCJK != NOCJK) {
+ 	    	kcode = HText_getKcode(me->text);
+ 		HText_updateKcode(me->text, kanji_code);
+ 	    }
  	    if (ptr &&
  		me->sp[0].tag_number == HTML_PRE && strlen(ptr) > 6) {
  	        /*
***************
*** 3285,3296 ****
  		for (i = 0; i < 6; i++) {
  		    if (*ptr == ' ')
  	                HText_appendCharacter(me->text, HT_NON_BREAK_SPACE); 
! 		    else
  	    	        HText_appendCharacter(me->text, *ptr);
  		    ptr++;
  		}
  		HText_setIgnoreExcess(me->text, TRUE);
  	    }
  	    for (; ptr && *ptr != '\0'; ptr++) {
  		if (*ptr == ' ')
  	            HText_appendCharacter(me->text, HT_NON_BREAK_SPACE); 
--- 3375,3387 ----
  		for (i = 0; i < 6; i++) {
  		    if (*ptr == ' ')
  	                HText_appendCharacter(me->text, HT_NON_BREAK_SPACE); 
! 		    else 
  	    	        HText_appendCharacter(me->text, *ptr);
  		    ptr++;
  		}
  		HText_setIgnoreExcess(me->text, TRUE);
  	    }
+ 	    
  	    for (; ptr && *ptr != '\0'; ptr++) {
  		if (*ptr == ' ')
  	            HText_appendCharacter(me->text, HT_NON_BREAK_SPACE); 
***************
*** 3304,3309 ****
--- 3395,3402 ----
  	    HText_setLastChar(me->text, ']');
  	    me->in_word = YES;
  	    HText_setIgnoreExcess(me->text, FALSE); 
+ 	    if (HTCJK != NOCJK)
+ 	    	HText_updateKcode(me->text, kcode);
  	}
      	HTChunkClear(&me->option);
  
