about summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/GridText.c699
-rw-r--r--src/HTAlert.c8
-rw-r--r--src/HTForms.h2
-rw-r--r--src/HTML.c40
-rw-r--r--src/LYBookmark.c2
-rw-r--r--src/LYCharUtils.c1
-rw-r--r--src/LYCookie.c34
-rw-r--r--src/LYCurses.c136
-rw-r--r--src/LYCurses.h31
-rw-r--r--src/LYLeaks.c8
-rw-r--r--src/LYMain.c2
-rw-r--r--src/LYMainLoop.c12
-rw-r--r--src/LYOptions.c2
-rw-r--r--src/LYReadCFG.c4
-rw-r--r--src/LYReadCFG.h2
-rw-r--r--src/LYSearch.c6
-rw-r--r--src/LYSignal.h2
-rw-r--r--src/LYStrings.c99
-rw-r--r--src/LYStrings.h4
-rw-r--r--src/LYUtils.c338
-rw-r--r--src/LYUtils.h3
-rw-r--r--src/LYrcFile.c10
22 files changed, 672 insertions, 773 deletions
diff --git a/src/GridText.c b/src/GridText.c
index 88630322..26cb0656 100644
--- a/src/GridText.c
+++ b/src/GridText.c
@@ -45,7 +45,7 @@
 #define NOTUSED_BAD_FOR_SCREEN
 #endif
 
-#undef DEBUG_APPCH
+/*#define DEBUG_APPCH 1*/
 
 #ifdef SOURCE_CACHE
 #include <HTFile.h>
@@ -494,6 +494,11 @@ PUBLIC char star_string[MAX_LINE + 1];
 PRIVATE int ctrl_chars_on_this_line = 0; /* num of ctrl chars in current line */
 PRIVATE int utfxtra_on_this_line = 0; /* num of UTF-8 extra bytes in line,
 				       they *also* count as ctrl chars. */
+#ifdef WIDEC_CURSES
+#define UTFXTRA_ON_THIS_LINE 0
+#else
+#define UTFXTRA_ON_THIS_LINE utfxtra_on_this_line
+#endif
 
 PRIVATE HTStyle default_style =
 	{ 0,  "(Unstyled)", "",
@@ -1094,7 +1099,7 @@ PRIVATE int display_line ARGS4(
 #define intarget NO
 #endif /* SHOW_WHEREIS_TARGETS && !USE_COLOR_STYLE */
 
-#ifndef NCURSES_VERSION
+#if !(defined(NCURSES_VERSION) || defined(WIDEC_CURSES))
     text->has_utf8 = NO; /* use as per-line flag, except with ncurses */
 #endif
 
@@ -1297,30 +1302,9 @@ PRIVATE int display_line ARGS4(
 #endif /* SHOW_WHEREIS_TARGETS */
 #endif /* USE_COLOR_STYLE */
 		i++;
-		if (text->T.output_utf8 && !isascii(UCH(buffer[0]))) {
+		if (text->T.output_utf8 && is8bits(buffer[0])) {
 		    text->has_utf8 = YES;
-		    if ((*buffer & 0xe0) == 0xc0) {
-			utf_extra = 1;
-		    } else if ((*buffer & 0xf0) == 0xe0) {
-			utf_extra = 2;
-		    } else if ((*buffer & 0xf8) == 0xf0) {
-			utf_extra = 3;
-		    } else if ((*buffer & 0xfc) == 0xf8) {
-			utf_extra = 4;
-		    } else if ((*buffer & 0xfe) == 0xfc) {
-			utf_extra = 5;
-		    } else {
-			 /*
-			  *  Garbage.
-			  */
-			utf_extra = 0;
-		    }
-		    if (strlen(data) < utf_extra) {
-			/*
-			 *  Shouldn't happen.
-			 */
-			utf_extra = 0;
-		    }
+		    utf_extra = utf8_length(text->T.output_utf8, data-1);
 		    LastDisplayChar = 'M';
 		}
 		if (utf_extra) {
@@ -1330,7 +1314,7 @@ PRIVATE int display_line ARGS4(
 		    buffer[1] = '\0';
 		    data += utf_extra;
 		    utf_extra = 0;
-		} else if (HTCJK != NOCJK && !isascii(UCH(buffer[0]))
+		} else if (HTCJK != NOCJK && is8bits(buffer[0])
 #ifndef CONV_JISX0201KANA_JISX0208KANA
 		    && kanji_code != SJIS
 #endif
@@ -1366,7 +1350,7 @@ PRIVATE int display_line ARGS4(
     } /* end of while */
 
 after_while:
-#if !defined(NCURSES_VERSION)
+#if !(defined(NCURSES_VERSION) || defined(WIDEC_CURSES))
     if (text->has_utf8) {
 	LYtouchline(scrline);
 	text->has_utf8 = NO;	/* we had some, but have dealt with it. */
@@ -1651,10 +1635,11 @@ PRIVATE void display_scrollbar ARGS1(
 	    LynxChangeStyle(s_sb_bar, STACK_OFF);
 #endif /* USE_COLOR_STYLE */
 	LYmove(i + off, LYcols + LYshiftWin - 1);
-	if (i > top_skip && i <= h - bot_skip)
+	if (i > top_skip && i <= h - bot_skip) {
 	    LYaddch(ACS_BLOCK);
-	else
+	} else {
 	    LYaddch(ACS_CKBOARD);
+	}
     }
 #ifdef USE_COLOR_STYLE
     LynxChangeStyle(s_sb_bg, STACK_OFF);
@@ -1942,30 +1927,7 @@ PRIVATE void display_page ARGS3(
 			/*
 			 *  Output all the printable target chars.
 			 */
-			if (text->T.output_utf8 && !isascii(UCH(tmp[0]))) {
-			    if ((*tmp & 0xe0) == 0xc0) {
-				utf_extra = 1;
-			    } else if ((*tmp & 0xf0) == 0xe0) {
-				utf_extra = 2;
-			    } else if ((*tmp & 0xf8) == 0xf0) {
-				utf_extra = 3;
-			    } else if ((*tmp & 0xfc) == 0xf8) {
-				utf_extra = 4;
-			    } else if ((*tmp & 0xfe) == 0xfc) {
-				utf_extra = 5;
-			    } else {
-				/*
-				 *  Garbage.
-				 */
-				utf_extra = 0;
-			    }
-			    if (strlen(&line->data[itmp+1]) < utf_extra) {
-				/*
-				 *  Shouldn't happen.
-				 */
-				utf_extra = 0;
-			    }
-			}
+			utf_extra = utf8_length(text->T.output_utf8, data + itmp);
 			if (utf_extra) {
 			    strncpy(&tmp[1], &line->data[itmp+1], utf_extra);
 			    tmp[utf_extra+1] = '\0';
@@ -1974,7 +1936,7 @@ PRIVATE void display_page ARGS3(
 			    tmp[1] = '\0';
 			    written += (utf_extra + 1);
 			    utf_extra = 0;
-			} else if (HTCJK != NOCJK && !isascii(UCH(tmp[0]))) {
+			} else if (HTCJK != NOCJK && is8bits(tmp[0])) {
 			    /*
 			     *  For CJK strings, by Masanobu Kimura.
 			     */
@@ -2164,7 +2126,7 @@ PRIVATE void display_page ARGS3(
 		/*
 		 *  Bold the link after incrementing nlinks.
 		 */
-		highlight(OFF, (nlinks - 1), target);
+		LYhighlight(OFF, (nlinks - 1), target);
 
 		display_flag = TRUE;
 
@@ -2238,6 +2200,7 @@ PRIVATE void display_page ARGS3(
     }
 #endif /* DISP_PARTIAL */
 
+#if !defined(WIDEC_CURSES)
     if (text->has_utf8 || text->had_utf8) {
 	/*
 	 *  For other than ncurses, repainting is taken care of
@@ -2255,6 +2218,8 @@ PRIVATE void display_page ARGS3(
 	 */
 	clearok(curscr, TRUE);
     }
+#endif /* WIDEC_CURSES */
+
     LYrefresh();
 }
 
@@ -2284,9 +2249,13 @@ PUBLIC void HText_beginAppend ARGS1(
    as any other large value.  (But don't use INT_MAX or something close
    to it to, avoid over/underflow.) - kw */
 #ifdef USE_SLANG
-#define LYcols_cu (dump_output_immediately ? MAX_COLS : SLtt_Screen_Cols)
+#define LYcols_cu(text) (dump_output_immediately ? MAX_COLS : SLtt_Screen_Cols)
 #else
-#define LYcols_cu (dump_output_immediately ? MAX_COLS : DISPLAY_COLS)
+#ifdef WIDEC_CURSES
+#define LYcols_cu(text) WRAP_COLS(text)
+#else
+#define LYcols_cu(text) (dump_output_immediately ? MAX_COLS : DISPLAY_COLS)
+#endif
 #endif
 
 /*	Add a new line of text
@@ -2459,7 +2428,9 @@ PRIVATE HTLine * insert_blanks_in_line ARGS7(
 	int curlim = (ip < ninserts
 		      ? oldpos[ip]
 		      /* Include'em all! */
-		      : (line->size <= MAX_LINE ? MAX_LINE+1 : line->size+1));
+		      : ((int)line->size <= MAX_LINE
+			  ? MAX_LINE+1
+			  : (int)line->size+1));
 	pre = s;
 
 	/* Fast forward to char==curlim or EOL.  Stop *before* the
@@ -2564,7 +2535,7 @@ PRIVATE void split_line ARGS2(
     char *p;
     HTLine * previous = text->last_line;
     int ctrl_chars_on_previous_line = 0;
-    int utfxtra_on_previous_line = utfxtra_on_this_line;
+    int utfxtra_on_previous_line = UTFXTRA_ON_THIS_LINE;
     char * cp;
     /* can't wrap in middle of multibyte sequences, so allocate 2 extra */
     HTLine * line = (HTLine *)LY_CALLOC(1, LINE_SIZE(MAX_LINE)+2);
@@ -2698,10 +2669,11 @@ PRIVATE void split_line ARGS2(
 		    p[i] == LY_UNDERLINE_END_CHAR ||
 		    p[i] == LY_BOLD_START_CHAR ||
 		    p[i] == LY_BOLD_END_CHAR ||
-		    p[i] == LY_SOFT_HYPHEN)
+		    p[i] == LY_SOFT_HYPHEN) {
 		    ctrl_chars_on_this_line++;
-		else if (IS_UTF_EXTRA(p[i]))
+		} else if (IS_UTF_EXTRA(p[i])) {
 		    utfxtra_on_this_line++;
+		}
 		if (p[i] == LY_SOFT_HYPHEN && (int)text->permissible_split < i)
 		    text->permissible_split = i + 1;
 	    }
@@ -2896,8 +2868,9 @@ PRIVATE void split_line ARGS2(
 		*cp == LY_BOLD_START_CHAR ||
 		*cp == LY_BOLD_END_CHAR ||
 		IS_UTF_EXTRA(*cp) ||
-		*cp == LY_SOFT_HYPHEN)
+		*cp == LY_SOFT_HYPHEN) {
 		ctrl_chars_on_previous_line++;
+	    }
 	}
 	if ((previous->size > 0) &&
 		(int)(previous->data[previous->size-1] == LY_SOFT_HYPHEN))
@@ -2912,9 +2885,9 @@ PRIVATE void split_line ARGS2(
 
 	if (spare > 0 && !dump_output_immediately &&
 	    text->T.output_utf8 && ctrl_chars_on_previous_line) {
-	    utfxtra_on_previous_line -= utfxtra_on_this_line;
+	    utfxtra_on_previous_line -= UTFXTRA_ON_THIS_LINE;
 	    if (utfxtra_on_previous_line) {
-		int spare_cu = (LYcols_cu-1) -
+		int spare_cu = (LYcols_cu(text)-1) -
 		    utfxtra_on_previous_line - indent +
 		    ctrl_chars_on_previous_line - previous->size;
 		    /*
@@ -2933,7 +2906,7 @@ PRIVATE void split_line ARGS2(
 			    (int)(previous->offset + indent + spare/2 +
 				  previous->size)
 			    - ctrl_chars_on_previous_line
-			    + utfxtra_on_previous_line <= (LYcols_cu - 1))
+			    + utfxtra_on_previous_line <= (LYcols_cu(text) - 1))
 			    /* do nothing - it still fits - kw */;
 			else {
 			    spare = spare_cu;
@@ -3107,22 +3080,8 @@ PRIVATE void split_line ARGS2(
 		*jp = ' ';	/* substitute it */
 		continue;
 	    }
-	    if (text->T.output_utf8 && !isascii(UCH(c))) {
-		int utf_extra = 0;
-		if ((c & 0xe0) == 0xc0) {
-		    utf_extra = 1;
-		} else if ((c & 0xf0) == 0xe0) {
-		    utf_extra = 2;
-		} else if ((c & 0xf8) == 0xf0) {
-		    utf_extra = 3;
-		} else if ((c & 0xfc) == 0xf8) {
-		    utf_extra = 4;
-		} else if ((c & 0xfe) == 0xfc) {
-		    utf_extra = 5;
-		} else
-		    utf_extra = 0;
-		if ( (int) strlen(jp+1) < utf_extra)
-		    utf_extra = 0;
+	    if (text->T.output_utf8 && is8bits(c)) {
+		int utf_extra = utf8_length(text->T.output_utf8, jp);
 		r->byte_len += utf_extra;
 		jp += utf_extra;
 	    }
@@ -3309,7 +3268,9 @@ PUBLIC void HText_appendCharacter ARGS2(
 {
     HTLine * line;
     HTStyle * style;
-    int indent, utfx;
+    int indent;
+    int limit = 0;
+    int actual;
 
 #ifdef DEBUG_APPCH
 #ifdef CJK_EX
@@ -3366,12 +3327,12 @@ PUBLIC void HText_appendCharacter ARGS2(
 		save_ch = 0;
 	    }
 #else
-	    if (ch < 0x80) {
-		CTRACE((tfp, "add(%c) %d/%d\n", ch,
-		    HTisDocumentSource(), HTOutputFormat != WWW_SOURCE));
+	    if (UCH(ch) < 0x80) {
+		CTRACE((tfp, "add(%c) %d/%d\n", UCH(ch),
+			HTisDocumentSource(), HTOutputFormat != WWW_SOURCE));
 	    } else {
-		CTRACE((tfp, "add(%02x) %d/%d\n", ch,
-		    HTisDocumentSource(), HTOutputFormat != WWW_SOURCE));
+		CTRACE((tfp, "add(%02x) %d/%d\n", UCH(ch),
+			HTisDocumentSource(), HTOutputFormat != WWW_SOURCE));
 	    }
 #endif	/* CJK_EX */
 	}
@@ -3516,10 +3477,11 @@ PUBLIC void HText_appendCharacter ARGS2(
      *  processing stage anyway. - kw
      */
 #ifndef   EBCDIC  /* S/390 -- gil -- 1514 */
-    if (UCH(ch) >= 128 && HTCJK == NOCJK &&
+    if (is8bits(ch) && HTCJK == NOCJK &&
 	!text->T.transp && !text->T.output_utf8 &&
-	UCH(ch) < LYlowest_eightbit[current_char_set])
+	UCH(ch) < LYlowest_eightbit[current_char_set]) {
 	return;
+    }
 #endif /* EBCDIC */
 #endif /* !USE_SLANG */
     if (UCH(ch) == 155 && HTCJK == NOCJK) {	/* octal 233 */
@@ -3534,7 +3496,6 @@ PUBLIC void HText_appendCharacter ARGS2(
     style = text->style;
 
     indent = text->in_line_1 ? (int)style->indent1st : (int)style->leftIndent;
-    utfx = utfxtra_on_this_line;
 
     if (HTCJK != NOCJK) {
 	switch(text->state) {
@@ -3718,8 +3679,9 @@ PUBLIC void HText_appendCharacter ARGS2(
 
     if (IsSpecialAttrChar(ch) && ch != LY_SOFT_NEWLINE) {
 #if !defined(USE_COLOR_STYLE) || !defined(NO_DUMP_WITH_BACKSPACES)
-	if (line->size >= (MAX_LINE-1))
+	if (line->size >= (MAX_LINE-1)) {
 	    return;
+	}
 #if defined(USE_COLOR_STYLE) && !defined(NO_DUMP_WITH_BACKSPACES)
 	if (with_backspaces && HTCJK==NOCJK && !text->T.output_utf8) {
 #endif
@@ -3757,8 +3719,9 @@ PUBLIC void HText_appendCharacter ARGS2(
 	     *  on the line, or if it is preceded by a space or
 	     *  hyphen. - FM
 	     */
-	    if (line->size < 1 || text->permissible_split >= line->size)
+	    if (line->size < 1 || text->permissible_split >= line->size) {
 		return;
+	    }
 
 	    for (i = (text->permissible_split + 1); line->data[i]; i++) {
 		if (!IsSpecialAttrChar(UCH(line->data[i])) &&
@@ -3775,8 +3738,9 @@ PUBLIC void HText_appendCharacter ARGS2(
 	}
 #if defined(USE_COLOR_STYLE) && !defined(NO_DUMP_WITH_BACKSPACES)
 	} /* if (with_backspaces && HTCJK==HTNOCJK && !text->T.output_utf8) */
-	 else
-	     return;
+	 else {
+	    return;
+	 }
 #endif
 
 #else
@@ -3796,12 +3760,13 @@ PUBLIC void HText_appendCharacter ARGS2(
 	 */
 	if (IS_UTF_EXTRA(ch)) {
 	    if ((line->size > (MAX_LINE-1))
-		|| (indent + (int)(line->offset + line->size) +
-		    utfxtra_on_this_line - ctrl_chars_on_this_line +
-		    ((line->size > 0) &&
-		     (int)(line->data[line->size-1] ==
+		|| (indent + (int)(line->offset + line->size)
+		    + UTFXTRA_ON_THIS_LINE
+		    - ctrl_chars_on_this_line
+		    + ((line->size > 0) &&
+		       (int)(line->data[line->size-1] ==
 				LY_SOFT_HYPHEN ?
-					     1 : 0)) >= (LYcols_cu-1))
+					     1 : 0)) >= (LYcols_cu(text)-1))
 		) {
 		if (!text->permissible_split || text->source) {
 		    text->permissible_split = line->size;
@@ -3817,7 +3782,7 @@ PUBLIC void HText_appendCharacter ARGS2(
 		split_line(text, text->permissible_split);
 		line = text->last_line;
 		if (text->source && line->size - ctrl_chars_on_this_line
-		    + utfxtra_on_this_line == 0)
+		    + UTFXTRA_ON_THIS_LINE == 0)
 		    HText_appendCharacter (text, LY_SOFT_NEWLINE);
 	    }
 	    line->data[line->size++] = (char) ch;
@@ -3840,7 +3805,7 @@ PUBLIC void HText_appendCharacter ARGS2(
 		split_line(text, text->permissible_split);
 		line = text->last_line;
 		if (text->source && line->size - ctrl_chars_on_this_line
-		    + utfxtra_on_this_line == 0)
+		    + UTFXTRA_ON_THIS_LINE == 0)
 		    HText_appendCharacter (text, LY_SOFT_NEWLINE);
 	    }
 	}
@@ -3850,15 +3815,15 @@ PUBLIC void HText_appendCharacter ARGS2(
      *  New Line.
      */
     if (ch == '\n') {
-	    new_line(text);
-	    text->in_line_1 = YES;	/* First line of new paragraph */
-	    /*
-	     *  There are some pages written in
-	     *  different kanji codes. - TA & kw
-	     */
-	    if (HTCJK == JAPANESE)
-		text->kcode = NOKANJI;
-	    return;
+	new_line(text);
+	text->in_line_1 = YES;	/* First line of new paragraph */
+	/*
+	 *  There are some pages written in
+	 *  different kanji codes. - TA & kw
+	 */
+	if (HTCJK == JAPANESE)
+	    text->kcode = NOKANJI;
+	return;
     }
 
     /*
@@ -3894,7 +3859,6 @@ PUBLIC void HText_appendCharacter ARGS2(
 	return;
     }
 
-
     /*
      *  Tabs.
      */
@@ -3913,7 +3877,7 @@ PUBLIC void HText_appendCharacter ARGS2(
 	}
 	here = ((int)(line->size + line->offset) + indent)
 		- ctrl_chars_on_this_line; /* Consider special chars GAB */
-	here_cu = here + utfxtra_on_this_line;
+	here_cu = here + UTFXTRA_ON_THIS_LINE;
 	if (style->tabs) {	/* Use tab table */
 	    for (Tab = style->tabs;
 		Tab->position <= here;
@@ -3976,12 +3940,11 @@ check_WrapSource:
 	 * the source visible.
 	 */
 	int target = (int)(line->offset + line->size) - ctrl_chars_on_this_line;
-	int target_cu = target + utfxtra_on_this_line;
+	int target_cu = target + UTFXTRA_ON_THIS_LINE;
 	if (target >= (WRAP_COLS(text)-1) - style->rightIndent -
 	    (((HTCJK != NOCJK) && text->kanji_buf) ? 1 : 0) ||
 	    (text->T.output_utf8 &&
-	     target_cu + UTF_XLEN(ch) >= (LYcols_cu-1))
-	    ) {
+	     target_cu + UTF_XLEN(ch) >= (LYcols_cu(text)-1))) {
 	    int saved_kanji_buf;
 	    int saved_state;
 
@@ -4016,9 +3979,9 @@ check_WrapSource:
      */
     if (text->IgnoreExcess) {
 	int nominal = (indent + (int)(line->offset + line->size) - ctrl_chars_on_this_line);
-	int limit = (WRAP_COLS(text) - 1);
 	int number;
 
+	limit = (WRAP_COLS(text) - 1);
 	if (fields_are_numbered()
 	 && !number_fields_on_left
 	 && text->last_anchor != 0
@@ -4036,7 +3999,7 @@ check_WrapSource:
 					: 1))))) + 2;
 	}
 	if ((nominal + (int)style->rightIndent) >= limit
-	 || (nominal + utfxtra_on_this_line) >= (LYcols_cu - 1)) {
+	 || (nominal + UTFXTRA_ON_THIS_LINE) >= (LYcols_cu(text) - 1)) {
 	    return;
 	}
     }
@@ -4044,21 +4007,21 @@ check_WrapSource:
     /*
      *  Check for end of line.
      */
-    if (((indent + (int)line->offset + (int)line->size) +
+    actual = ((indent + (int)line->offset + (int)line->size) +
+       ((line->size > 0) &&
+	(int)(line->data[line->size-1] == LY_SOFT_HYPHEN ? 1 : 0)));
+
+    if (text->T.output_utf8) {
+	actual += (UTFXTRA_ON_THIS_LINE - ctrl_chars_on_this_line + UTF_XLEN(ch));
+	limit = (LYcols_cu(text) - 1);
+    } else {
+	actual +=
 	 (int)style->rightIndent - ctrl_chars_on_this_line +
-	 (((HTCJK != NOCJK) && text->kanji_buf) ? 1 : 0) +
-	 ((line->size > 0) &&
-	  (int)(line->data[line->size-1] ==
-				LY_SOFT_HYPHEN ?
-					     1 : 0))) >= (WRAP_COLS(text) - 1) ||
-	(text->T.output_utf8 &&
-	 (((indent + (int)line->offset + (int)line->size) +
-	   utfxtra_on_this_line - ctrl_chars_on_this_line +
-	   UTF_XLEN(ch) +
-	   ((line->size > 0) &&
-	    (int)(line->data[line->size-1] ==
-				LY_SOFT_HYPHEN ?
-					     1 : 0))) >= (LYcols_cu - 1)))) {
+	 (((HTCJK != NOCJK) && text->kanji_buf) ? 1 : 0);
+	limit = (WRAP_COLS(text) - 1);
+    }
+
+    if (actual >= limit) {
 
 	if (style->wordWrap && HTOutputFormat != WWW_SOURCE) {
 #ifdef EXP_JUSTIFY_ELTS
@@ -4066,33 +4029,34 @@ check_WrapSource:
 		this_line_was_split = TRUE;
 #endif
 	    split_line(text, text->permissible_split);
-	    if (ch == ' ') return;	/* Ignore space causing split */
-
-	}  else if (HTOutputFormat == WWW_SOURCE) {
-		 /*
-		  *  For source output we don't want to wrap this stuff
-		  *  unless absolutely necessary. - LJM
-		  *  !
-		  *  If we don't wrap here we might get a segmentation fault.
-		  *  but let's see what happens
-		  */
-		if ((int)line->size >= (int)(MAX_LINE-1)) {
-		   new_line(text);  /* try not to linewrap */
-		}
-	} else {
-		/*
-		 *  For normal stuff like pre let's go ahead and
-		 *  wrap so the user can see all of the text.
-		 */
+	    if (ch == ' ') {
+		return;	/* Ignore space causing split */
+	    }
 
-		if ( (dump_output_immediately|| (crawl && traversal) )
-		     && dont_wrap_pre) {
-		    if ((int)line->size >= (int)(MAX_LINE-1))
-			new_line(text);
-		} else {
+	} else if (HTOutputFormat == WWW_SOURCE) {
+	    /*
+	     *  For source output we don't want to wrap this stuff
+	     *  unless absolutely necessary. - LJM
+	     *  !
+	     *  If we don't wrap here we might get a segmentation fault.
+	     *  but let's see what happens
+	     */
+	    if ((int)line->size >= (int)(MAX_LINE-1)) {
+		new_line(text);  /* try not to linewrap */
+	    }
+	} else {
+	    /*
+	     *  For normal stuff like pre let's go ahead and
+	     *  wrap so the user can see all of the text.
+	     */
+	    if ( (dump_output_immediately|| (crawl && traversal) )
+		 && dont_wrap_pre) {
+		if ((int)line->size >= (int)(MAX_LINE-1)) {
 		    new_line(text);
 		}
-
+	    } else {
+		new_line(text);
+	    }
 	}
     } else if ((int)line->size >= (int)(MAX_LINE-1)) {
 	/*
@@ -8357,7 +8321,7 @@ PRIVATE int HText_TrueLineSize ARGS3(
 	for (i = 0; i < line->size; i++) {
 	    if (!IsSpecialAttrChar(UCH(line->data[i])) &&
 		(!(text && text->T.output_utf8) ||
-		 UCH(line->data[i]) < 128 ||
+		 !is8bits(line->data[i]) ||
 		 (UCH((line->data[i] & 0xc0)) == 0xc0)) &&
 		!isspace(UCH(line->data[i])) &&
 		UCH(line->data[i]) != HT_NON_BREAK_SPACE &&
@@ -8369,7 +8333,7 @@ PRIVATE int HText_TrueLineSize ARGS3(
 	for (i = 0; i < line->size; i++) {
 	    if (!IsSpecialAttrChar(line->data[i]) &&
 		(!(text && text->T.output_utf8) ||
-		 UCH(line->data[i]) < 128 ||
+		 !is8bits(line->data[i]) ||
 		 (UCH(line->data[i] & 0xc0) == 0xc0))) {
 		true_size++;
 	    }
@@ -9764,6 +9728,123 @@ PRIVATE int find_best_target_cs ARGS3(
     return (-1);
 }
 
+PRIVATE BOOLEAN begin_submission_part ARGS5(
+    char **,	query,
+    BOOLEAN,	first_one,
+    BOOLEAN,	SemiColon,
+    BOOLEAN,	PlainText,
+    char *,	Boundary)
+{
+    if (first_one) {
+	if (Boundary) {
+	    HTSprintf(query, "--%s\r\n", Boundary);
+	}
+	first_one = FALSE;
+    } else {
+	if (PlainText) {
+	    StrAllocCat(*query, "\n");
+	} else if (SemiColon) {
+	    StrAllocCat(*query, ";");
+	} else if (Boundary) {
+	    HTSprintf(query, "\r\n--%s\r\n", Boundary);
+	} else {
+	    StrAllocCat(*query, "&");
+	}
+    }
+    return FALSE;
+}
+
+#ifdef EXP_FILE_UPLOAD
+PRIVATE BOOLEAN send_a_file ARGS6(
+    char **,	query,
+    BOOLEAN,	PlainText,
+    char *,	MultipartContentType,
+    char *,	Boundary,
+    char *,	name_used,
+    char *,	val_used)
+{
+    char *escaped1 = NULL;
+    char *escaped2 = NULL;
+    FILE *fd;
+    size_t n, bytes;
+    BOOLEAN code = FALSE;
+    BOOLEAN use_mime = FALSE;
+    char buffer[257];
+    char base64buf[128];
+
+    CTRACE((tfp, "Ok, about to convert %s to mime/thingy\n", val_used));
+
+    if ((fd = fopen(val_used, BIN_R)) == 0) {
+	/* We can't open the file, what do we do? */
+	HTAlert(gettext("Can't open file for uploading"));
+	goto exit_disgracefully;
+    }
+    StrAllocCopy(escaped2, "");
+    while ((bytes = fread(buffer, sizeof(char), 256, fd)) != 0) {
+	buffer[bytes] = 0;
+	for (n = 0; n < bytes; ++n) {
+	    int ch = UCH(buffer[n]);
+	    if ((iscntrl(ch) && !isspace(ch))
+	     || (!iscntrl(ch) && !isprint(ch))) {
+		use_mime = TRUE;
+		break;
+	    }
+	}
+	if (use_mime)
+	    break;
+	StrAllocCat(escaped2, buffer);
+    }
+    if (use_mime) {
+	rewind(fd);
+	StrAllocCopy(escaped2, "");
+	while ((bytes = fread(buffer, sizeof(char), 45, fd)) != 0) {
+	    base64_encode(base64buf, buffer, bytes);
+	    StrAllocCat(escaped2, base64buf);
+	}
+    }
+    if (ferror(fd)) {
+	/* We got an error reading the file, what do we do? */
+	HTAlert(gettext("Short read from file, problem?"));
+	LYCloseInput(fd);
+	goto exit_disgracefully;
+    }
+    LYCloseInput(fd);
+    /* we need to modify the mime-type here - rp */
+    /* Note: could use LYGetFileInfo for that and for
+       other headers that should be transmitted - kw */
+
+    if (PlainText) {
+	StrAllocCopy(escaped1, name_used);
+    } else if (Boundary) {
+	StrAllocCopy(escaped1, "Content-Disposition: form-data");
+	HTSprintf(&escaped1, "; name=%s", name_used);
+	HTSprintf(&escaped1, "; filename=\"%s\"", val_used);
+	if (MultipartContentType) {
+	    StrAllocCat(escaped1, MultipartContentType);
+	    if (use_mime)
+		StrAllocCat(escaped1, "\r\nContent-Transfer-Encoding: base64");
+	}
+	StrAllocCat(escaped1, "\r\n\r\n");
+    } else {
+	escaped1 = HTEscapeSP(name_used, URL_XALPHAS);
+    }
+
+    HTSprintf(query,
+	      "%s%s%s%s%s",
+	      escaped1,
+	      (Boundary ? "" : "="),
+	      (PlainText ? "\n" : ""),
+	      escaped2,
+	      ((PlainText && *escaped2) ? "\n" : ""));
+    code = TRUE;
+
+exit_disgracefully:
+    FREE(escaped1);
+    FREE(escaped2);
+    return code;
+}
+#endif /* EXP_FILE_UPLOAD */
+
 /*
  *  HText_SubmitForm - generate submit data from form fields.
  *  For mailto forms, send the data.
@@ -9784,7 +9865,7 @@ PUBLIC int HText_SubmitForm ARGS4(
     PerFormInfo *thisform;
     char *query = NULL;
     char *escaped1 = NULL, *escaped2 = NULL;
-    int first_one = 1;
+    BOOLEAN first_one = TRUE;
     char *last_textarea_name = NULL;
     int textarea_lineno = 0;
     char *previous_blanks = NULL;
@@ -10078,8 +10159,7 @@ PUBLIC int HText_SubmitForm ARGS4(
 			  strcmp(target_csname, "iso-8859-1"))) ||
 			(!HTMainText->node_anchor->charset &&
 			 target_cs != UCLYhndl_for_unspec)) {
-			StrAllocCat(content_type_out, "; charset=");
-			StrAllocCat(content_type_out, target_csname);
+			HTSprintf(&content_type_out, "; charset=%s", target_csname);
 		    }
 		}
 	    } else {
@@ -10115,6 +10195,15 @@ PUBLIC int HText_SubmitForm ARGS4(
 		switch(form_ptr->type) {
 		case F_RESET_TYPE:
 		    break;
+#ifdef EXP_FILE_UPLOAD
+		case F_FILE_TYPE:
+		    name_used = (form_ptr->name ? form_ptr->name : "");
+		    val_used = (form_ptr->value ? form_ptr->value : "");
+		    CTRACE((tfp,
+			    "I'd submit %s (from %s), but you've not finished it\n",
+			    val_used, name_used));
+		    break;
+#endif
 		case F_SUBMIT_TYPE:
 		case F_TEXT_SUBMIT_TYPE:
 		case F_IMAGE_SUBMIT_TYPE:
@@ -10142,16 +10231,6 @@ PUBLIC int HText_SubmitForm ARGS4(
 			break;
 		    }
 		    /* FALLTHRU */
-
-#ifdef EXP_FILE_UPLOAD
-		case F_FILE_TYPE:
-		    CTRACE((tfp, "I'd submit %s (from %s), but you've not finished it\n", form_ptr->value, form_ptr->name));
-		    name_used = (form_ptr->name ? form_ptr->name : "");
-		    val_used = (form_ptr->value ? form_ptr->value : "");
-		    break;
-#endif
-
-		    /*  fall through  */
 		case F_RADIO_TYPE:
 		case F_CHECKBOX_TYPE:
 		case F_TEXTAREA_TYPE:
@@ -10227,24 +10306,20 @@ PUBLIC int HText_SubmitForm ARGS4(
 		    if (out_cs >= 0)
 			out_csname = LYCharSet_UC[out_cs].MIMEname;
 		    if (Boundary) {
+			StrAllocCopy(MultipartContentType,
+				     "\r\nContent-Type: text/plain");
 			if (!success && form_ptr->value_cs < 0) {
 			    /*  This is weird. */
-			    StrAllocCopy(MultipartContentType,
-					 "\r\nContent-Type: text/plain; charset=");
-			    StrAllocCat(MultipartContentType, "UNKNOWN-8BIT");
+			    out_csname = "UNKNOWN-8BIT";
 			} else if (!success) {
-			    StrAllocCopy(MultipartContentType,
-					 "\r\nContent-Type: text/plain; charset=");
-			    StrAllocCat(MultipartContentType, out_csname);
 			    target_csname = NULL;
 			} else {
 			    if (!target_csname) {
 				target_csname = LYCharSet_UC[target_cs].MIMEname;
 			    }
-			    StrAllocCopy(MultipartContentType,
-					 "\r\nContent-Type: text/plain; charset=");
-			    StrAllocCat(MultipartContentType, out_csname);
 			}
+			if (strcmp(out_csname, "iso-8859-1"))
+			    HTSprintf(&MultipartContentType, "; charset=%s", out_csname);
 		    }
 
 		    /*
@@ -10354,82 +10429,18 @@ PUBLIC int HText_SubmitForm ARGS4(
 
 #ifdef EXP_FILE_UPLOAD
 		case F_FILE_TYPE:
-		{
-		    int cdisp_name_startpos = 0;
-		    FILE *fd;
-		    int bytes;
-		    char buffer[257];
-
-		    CTRACE((tfp, "Ok, about to convert %s to mime/thingy\n", form_ptr->value));
-		    if (first_one) {
-			if (Boundary) {
-			    HTSprintf(&query, "--%s\r\n", Boundary);
-			}
-			first_one = FALSE;
-		    } else {
-			if (PlainText) {
-			    HTSprintf(&query, "\n");
-			} else if (SemiColon) {
-			    HTSprintf(&query, ";");
-			} else if (Boundary) {
-			    HTSprintf(&query, "\r\n--%s\r\n", Boundary);
-			} else {
-			    HTSprintf(&query, "&");
-			}
-		    }
-
-		    if (PlainText) {
-			StrAllocCopy(escaped1, name_used);
-		    } else if (Boundary) {
-			StrAllocCopy(escaped1,
-				"Content-Disposition: form-data; name=");
-			cdisp_name_startpos = strlen(escaped1);
-			StrAllocCat(escaped1, name_used);
-			StrAllocCat(escaped1, "; filename=\"");
-			StrAllocCat(escaped1, val_used);
-			StrAllocCat(escaped1, "\"");
-			if (MultipartContentType) {
-			    StrAllocCat(escaped1, MultipartContentType);
-			    StrAllocCat(escaped1, "\r\nContent-Transfer-Encoding: base64");
-			}
-			StrAllocCat(escaped1, "\r\n\r\n");
-		    } else {
-			escaped1 = HTEscapeSP(name_used, URL_XALPHAS);
-		    }
-
-		    if ((fd = fopen(val_used, BIN_R)) == 0) {
-			/* We can't open the file, what do we do? */
-			HTAlert(gettext("Can't open file for uploading"));
-			goto exit_disgracefully;
-		    }
-		    StrAllocCopy(escaped2, "");
-		    while ((bytes = fread(buffer, sizeof(char), 45, fd)) != 0) {
-			char base64buf[128];
-			base64_encode(base64buf, buffer, bytes);
-			StrAllocCat(escaped2, base64buf);
-		    }
-		    if (ferror(fd)) {
-			/* We got an error reading the file, what do we do? */
-			HTAlert(gettext("Short read from file, problem?"));
-			LYCloseInput(fd);
+		    first_one = begin_submission_part(&query,
+						    first_one,
+						    SemiColon,
+						    PlainText,
+						    Boundary);
+		    if (!send_a_file(&query,
+				     PlainText,
+				     MultipartContentType,
+				     Boundary,
+				     name_used,
+				     val_used))
 			goto exit_disgracefully;
-		    }
-		    LYCloseInput(fd);
-		    /* we need to modify the mime-type here - rp */
-		    /* Note: could use LYGetFileInfo for that and for
-		       other headers that should be transmitted - kw */
-
-		    HTSprintf(&query,
-				   "%s%s%s%s%s",
-				   escaped1,
-				   (Boundary ? "" : "="),
-				   (PlainText ? "\n" : ""),
-					escaped2,
-				   ((PlainText && *escaped2) ?
-							 "\n" : ""));
-		    FREE(escaped1);
-		    FREE(escaped2);
-		}
 		break;
 #endif /* EXP_FILE_UPLOAD */
 
@@ -10451,22 +10462,12 @@ PUBLIC int HText_SubmitForm ARGS4(
 			(form_ptr->value && *form_ptr->value != '\0' &&
 			 !strcmp(form_ptr->value, link_value)))) {
 			int cdisp_name_startpos = 0;
-			if (first_one) {
-			    if (Boundary) {
-				HTSprintf(&query, "--%s\r\n", Boundary);
-			    }
-			    first_one=FALSE;
-			} else {
-			    if (PlainText) {
-				StrAllocCat(query, "\n");
-			    } else if (SemiColon) {
-				StrAllocCat(query, ";");
-			    } else if (Boundary) {
-				HTSprintf(&query, "\r\n--%s\r\n", Boundary);
-			    } else {
-				StrAllocCat(query, "&");
-			    }
-			}
+
+			first_one = begin_submission_part(&query,
+							  first_one,
+							  SemiColon,
+							  PlainText,
+							  Boundary);
 
 			if (PlainText) {
 			    StrAllocCopy(escaped1, name_used);
@@ -10522,13 +10523,10 @@ PUBLIC int HText_SubmitForm ARGS4(
 			    HTSprintf(&query,
 				    "%s%s%s%s%s",
 				    escaped1,
-				    (Boundary ?
-					   "" : "="),
-				    (PlainText ?
-					  "\n" : ""),
+				    (Boundary ? "" : "="),
+				    (PlainText ? "\n" : ""),
 				    escaped2,
-				    ((PlainText && *escaped2) ?
-							 "\n" : ""));
+				    ((PlainText && *escaped2) ? "\n" : ""));
 			}
 			FREE(escaped1);
 			FREE(escaped2);
@@ -10543,37 +10541,25 @@ PUBLIC int HText_SubmitForm ARGS4(
 		     *  Only add if selected.
 		     */
 		    if (form_ptr->num_value) {
-			if (first_one) {
-			    if (Boundary) {
-				HTSprintf(&query,
-					"--%s\r\n", Boundary);
-			    }
-			    first_one=FALSE;
-			} else {
-			    if (PlainText) {
-				StrAllocCat(query, "\n");
-			    } else if (SemiColon) {
-				StrAllocCat(query, ";");
-			    } else if (Boundary) {
-				HTSprintf(&query, "\r\n--%s\r\n", Boundary);
-			    } else {
-				StrAllocCat(query, "&");
-			    }
-			}
+			first_one = begin_submission_part(&query,
+							  first_one,
+							  SemiColon,
+							  PlainText,
+							  Boundary);
 
 			if (PlainText) {
 			    StrAllocCopy(escaped1, name_used);
 			} else if (Boundary) {
 			    StrAllocCopy(escaped1,
 				     "Content-Disposition: form-data; name=");
-			    StrAllocCat(escaped1,
-					name_used);
+			    StrAllocCat(escaped1, name_used);
 			    if (MultipartContentType)
 				StrAllocCat(escaped1, MultipartContentType);
 			    StrAllocCat(escaped1, "\r\n\r\n");
 			} else {
 			    escaped1 = HTEscapeSP(name_used, URL_XALPHAS);
 			}
+
 			if (PlainText || Boundary) {
 			    StrAllocCopy(escaped2,
 					 (val_used ?
@@ -10621,27 +10607,17 @@ PUBLIC int HText_SubmitForm ARGS4(
 			} else {
 			    FREE(previous_blanks);
 			}
-			if (first_one) {
-			    if (Boundary) {
-				HTSprintf(&query, "--%s\r\n", Boundary);
-			    }
-			    first_one = FALSE;
-			} else {
-			    if (PlainText) {
-				StrAllocCat(query, "\n");
-			    } else if (SemiColon) {
-				StrAllocCat(query, ";");
-			    } else if (Boundary) {
-				HTSprintf(&query, "\r\n--%s\r\n", Boundary);
-			    } else {
-				StrAllocCat(query, "&");
-			    }
-			}
+			first_one = begin_submission_part(&query,
+							  first_one,
+							  SemiColon,
+							  PlainText,
+							  Boundary);
+
 			if (PlainText) {
 			    StrAllocCopy(escaped1, name_used);
 			} else if (Boundary) {
 			    StrAllocCopy(escaped1,
-				    "Content-Disposition: form-data; name=");
+				     "Content-Disposition: form-data; name=");
 			    StrAllocCat(escaped1, name_used);
 			    if (MultipartContentType)
 				StrAllocCat(escaped1, MultipartContentType);
@@ -10649,16 +10625,14 @@ PUBLIC int HText_SubmitForm ARGS4(
 			} else {
 			    escaped1 = HTEscapeSP(name_used, URL_XALPHAS);
 			}
+
 			HTSprintf(&query,
 				"%s%s%s%s%s",
 				escaped1,
-				(Boundary ?
-				       "" : "="),
-				(PlainText ?
-				      "\n" : ""),
+				(Boundary ? "" : "="),
+				(PlainText ? "\n" : ""),
 				escaped2,
-				((PlainText && *escaped2) ?
-						     "\n" : ""));
+				((PlainText && *escaped2) ? "\n" : ""));
 			FREE(escaped1);
 			last_textarea_name = form_ptr->name;
 		    } else {
@@ -10696,23 +10670,11 @@ PUBLIC int HText_SubmitForm ARGS4(
 		case F_TEXT_TYPE:
 		case F_OPTION_LIST_TYPE:
 		case F_HIDDEN_TYPE:
-		    if (first_one) {
-			if (Boundary) {
-			    HTSprintf(&query, "--%s\r\n", Boundary);
-			}
-			first_one=FALSE;
-		    } else {
-			if (PlainText) {
-			    StrAllocCat(query, "\n");
-			} else if (SemiColon) {
-			    StrAllocCat(query, ";");
-			} else if (Boundary) {
-			    HTSprintf(&query, "\r\n--%s\r\n", Boundary);
-			} else {
-			    StrAllocCat(query, "&");
-			}
-		    }
-
+		    first_one = begin_submission_part(&query,
+						      first_one,
+						      SemiColon,
+						      PlainText,
+						      Boundary);
 		    if (PlainText) {
 		       StrAllocCopy(escaped1, name_used);
 		    } else if (Boundary) {
@@ -10737,13 +10699,10 @@ PUBLIC int HText_SubmitForm ARGS4(
 		    HTSprintf(&query,
 			    "%s%s%s%s%s",
 			    escaped1,
-			    (Boundary ?
-				   "" : "="),
-			    (PlainText ?
-				  "\n" : ""),
+			    (Boundary ? "" : "="),
+			    (PlainText ? "\n" : ""),
 			    escaped2,
-			    ((PlainText && *escaped2) ?
-						 "\n" : ""));
+			    ((PlainText && *escaped2) ? "\n" : ""));
 		    FREE(escaped1);
 		    FREE(escaped2);
 		    FREE(copied_name_used);
@@ -12827,29 +12786,8 @@ PRIVATE void redraw_part_of_line ARGS4(
 
 	    default:
 		i++;
-		if (text->T.output_utf8 && !isascii(UCH(buffer[0]))) {
-		    if ((*buffer & 0xe0) == 0xc0) {
-			utf_extra = 1;
-		    } else if ((*buffer & 0xf0) == 0xe0) {
-			utf_extra = 2;
-		    } else if ((*buffer & 0xf8) == 0xf0) {
-			utf_extra = 3;
-		    } else if ((*buffer & 0xfc) == 0xf8) {
-			utf_extra = 4;
-		    } else if ((*buffer & 0xfe) == 0xfc) {
-			utf_extra = 5;
-		    } else {
-			 /*
-			  *  Garbage.
-			  */
-			utf_extra = 0;
-		    }
-		    if (strlen(data) < utf_extra) {
-			/*
-			 *  Shouldn't happen.
-			 */
-			utf_extra = 0;
-		    }
+		if (text->T.output_utf8 && is8bits(buffer[0])) {
+		    utf_extra = utf8_length(text->T.output_utf8, data-1);
 		    LastDisplayChar = 'M';
 		}
 		if (utf_extra) {
@@ -12859,7 +12797,7 @@ PRIVATE void redraw_part_of_line ARGS4(
 		    buffer[1] = '\0';
 		    data += utf_extra;
 		    utf_extra = 0;
-		} else if (HTCJK != NOCJK && !isascii(UCH(buffer[0]))) {
+		} else if (HTCJK != NOCJK && is8bits(buffer[0])) {
 		    /*
 		     *  For CJK strings, by Masanobu Kimura.
 		     */
@@ -13050,7 +12988,7 @@ PRIVATE void move_to_glyph ARGS10(
 	     *  and when to react to it should be cleaned up (here and
 	     *  further below).  For now this seems to work but isn't
 	     *  very clear.  The complications arise from reproducing
-	     *  the behavior (previously done in highlight()) for target
+	     *  the behavior (previously done in LYhighlight()) for target
 	     *  strings that fall into or overlap a link: use target
 	     *  emphasis for the target string, except for the first
 	     *  and last character of the anchor text if the anchor is
@@ -13237,7 +13175,7 @@ PRIVATE void move_to_glyph ARGS10(
 		    if (i == XP - 1) {
 			i_after_tgt = i;
 		    } else if (i == XP - 2 && HTCJK != NOCJK &&
-			       !isascii(UCH(buffer[0]))) {
+			       is8bits(buffer[0])) {
 			i_after_tgt = i;
 			cp_tgt = NULL;
 			if (drawing) {
@@ -13295,30 +13233,9 @@ PRIVATE void move_to_glyph ARGS10(
 		    }
 
 		i++;
-		if (utf_flag && !isascii(UCH(buffer[0]))) {
+		if (utf_flag && is8bits(buffer[0])) {
 		    hadutf8 = YES;
-		    if ((*buffer & 0xe0) == 0xc0) {
-			utf_extra = 1;
-		    } else if ((*buffer & 0xf0) == 0xe0) {
-			utf_extra = 2;
-		    } else if ((*buffer & 0xf8) == 0xf0) {
-			utf_extra = 3;
-		    } else if ((*buffer & 0xfc) == 0xf8) {
-			utf_extra = 4;
-		    } else if ((*buffer & 0xfe) == 0xfc) {
-			utf_extra = 5;
-		    } else {
-			 /*
-			  *  Garbage.
-			  */
-			utf_extra = 0;
-		    }
-		    if (strlen(data) < utf_extra) {
-			/*
-			 *  Shouldn't happen.
-			 */
-			utf_extra = 0;
-		    }
+		    utf_extra = utf8_length(utf_flag, data-1);
 		    LastDisplayChar = 'M';
 		}
 		if (utf_extra) {
@@ -13344,7 +13261,7 @@ PRIVATE void move_to_glyph ARGS10(
 		    buffer[1] = '\0';
 		    sdata += utf_extra; data += utf_extra;
 		    utf_extra = 0;
-		} else if (HTCJK != NOCJK && !isascii(UCH(buffer[0]))) {
+		} else if (HTCJK != NOCJK && is8bits(buffer[0])) {
 		    /*
 		     *  For CJK strings, by Masanobu Kimura.
 		     */
diff --git a/src/HTAlert.c b/src/HTAlert.c
index 32452b62..2fb89f9f 100644
--- a/src/HTAlert.c
+++ b/src/HTAlert.c
@@ -92,7 +92,7 @@ PUBLIC void HTInfoMsg ARGS1(
     if (Msg && *Msg) {
 	CTRACE((tfp, "Info message: %s\n", Msg));
 	LYstore_message(Msg);
-	LYSleep(InfoSecs);
+	LYSleepInfo();
     }
 }
 
@@ -106,6 +106,12 @@ PUBLIC void HTUserMsg ARGS1(
     if (Msg && *Msg) {
 	CTRACE((tfp, "User message: %s\n", Msg));
 	LYstore_message(Msg);
+#if !(defined(USE_SLANG) || defined(WIDEC_CURSES))
+	if (HTCJK != NOCJK) {
+	    clearok(curscr, TRUE);
+	    LYrefresh();
+	}
+#endif
 	LYSleepMsg();
     }
 }
diff --git a/src/HTForms.h b/src/HTForms.h
index cdfa3688..5962f594 100644
--- a/src/HTForms.h
+++ b/src/HTForms.h
@@ -103,7 +103,7 @@ typedef struct _FormInfo {
 typedef struct _PerFormInfo
 {
 	int			number;	   /* form number, see GridText.c */
-    /* except for the last two, the followign fields aren't actually used.. */
+    /* except for the last two, the following fields aren't actually used.. */
 	int			disabled;  /* If YES, can't change values */
 	struct _PerFormInfo *	next;	   /* pointer to next form in doc */
 	int			nfields;   /* number of fields */
diff --git a/src/HTML.c b/src/HTML.c
index 64235829..366c18d9 100644
--- a/src/HTML.c
+++ b/src/HTML.c
@@ -4926,6 +4926,9 @@ PRIVATE int HTML_start_element ARGS6(
 	     */
 	    if (present && present[HTML_INPUT_TYPE] &&
 		value[HTML_INPUT_TYPE] && *value[HTML_INPUT_TYPE]) {
+		char *not_impl = NULL;
+		char *usingval = NULL;
+
 		I.type = value[HTML_INPUT_TYPE];
 
 		if (!strcasecomp(I.type, "range")) {
@@ -4936,7 +4939,7 @@ PRIVATE int HTML_start_element ARGS6(
 		    /*
 		     *	Not yet implemented.
 		     */
-		    HTML_put_string(me,"[RANGE Input] (Not yet implemented.)");
+		    not_impl = "[RANGE Input]";
 #ifdef NOTDEFINED
 		    if (me->inFORM)
 			HText_DisableCurrentForm();
@@ -4947,27 +4950,15 @@ PRIVATE int HTML_start_element ARGS6(
 		} else if (!strcasecomp(I.type, "file")) {
 		    if (present[HTML_INPUT_ACCEPT])
 			I.accept = value[HTML_INPUT_ACCEPT];
-#ifdef EXP_FILE_UPLOAD
-		    /*
-		     *	Not yet implemented.
-		     */
-		    if (me->inUnderline == FALSE) {
-			HText_appendCharacter(me->text,
-					      LY_UNDERLINE_START_CHAR);
-		    }
-		    HTML_put_string(me,"[FILE Input] (Not yet implemented.)");
-		    if (me->inUnderline == FALSE) {
-			HText_appendCharacter(me->text,
-					      LY_UNDERLINE_END_CHAR);
-		    }
-#else
+#ifndef EXP_FILE_UPLOAD
+		    not_impl = "[FILE Input]";
 		    CTRACE((tfp, "Attempting to fake as: %s\n", I.type));
-#endif /* EXP_FILE_UPLOAD */
 #ifdef NOTDEFINED
 		    if (me->inFORM)
 			HText_DisableCurrentForm();
 #endif /* NOTDEFINED */
 		    CTRACE((tfp, "HTML: Ignoring TYPE=\"file\"\n"));
+#endif /* EXP_FILE_UPLOAD */
 
 		} else if (!strcasecomp(I.type, "button")) {
 		    /*
@@ -4976,6 +4967,23 @@ PRIVATE int HTML_start_element ARGS6(
 		    HTML_put_string(me,"[BUTTON] ");
 		    break;
 		}
+		if (not_impl != NULL) {
+		    if (me->inUnderline == FALSE) {
+			HText_appendCharacter(me->text,
+					      LY_UNDERLINE_START_CHAR);
+		    }
+		    HTML_put_string(me, not_impl);
+		    if (usingval != NULL) {
+			HTML_put_string(me, usingval);
+			FREE(usingval);
+		    } else {
+			HTML_put_string(me, " (not implemented)");
+		    }
+		    if (me->inUnderline == FALSE) {
+			HText_appendCharacter(me->text,
+					      LY_UNDERLINE_END_CHAR);
+		    }
+		}
 	    }
 
 	    /*
diff --git a/src/LYBookmark.c b/src/LYBookmark.c
index 8a732eb8..d0bd7086 100644
--- a/src/LYBookmark.c
+++ b/src/LYBookmark.c
@@ -712,7 +712,7 @@ get_advanced_choice:
 	    c = LYCharINTERRUPT2;
 	}
 #endif /* VMS */
-	if (LYisNonAlnumKeyname(c, LYK_PREV_DOC) || LYCharIsINTERRUPT(c)) {
+	if (LYisNonAlnumKeyname(c, LYK_PREV_DOC) || LYCharIsINTERRUPT_HARD(c)) {
 	    /*
 	     *	Treat left-arrow, ^G, or ^C as cancel.
 	     */
diff --git a/src/LYCharUtils.c b/src/LYCharUtils.c
index 8c5ae7df..7c146c6e 100644
--- a/src/LYCharUtils.c
+++ b/src/LYCharUtils.c
@@ -2990,6 +2990,7 @@ PUBLIC int LYLegitimizeHREF ARGS4(
 	 * No blanks really belong in the HREF, but if it refers to an actual
 	 * file, it may actually have blanks in the name.  Try to accommodate.
 	 */
+	LYRemoveNewlines(*href);
 	convert_to_spaces(*href, FALSE);
 	LYTrimLeading(*href);
 	LYTrimTrailing(*href);
diff --git a/src/LYCookie.c b/src/LYCookie.c
index 27cc5d8d..8efc4e6c 100644
--- a/src/LYCookie.c
+++ b/src/LYCookie.c
@@ -359,7 +359,7 @@ PRIVATE void store_cookie ARGS3(
      */
     if (!is_prefix(co->path, path)) {
 	invcheck_behaviour_t invcheck_bv = (de ? de->invcheck_bv
-	    				       : DEFAULT_INVCHECK_BV);
+					       : DEFAULT_INVCHECK_BV);
 	switch (invcheck_bv) {
 	case INVCHECK_LOOSE:
 	    break;		/* continue as if nothing were wrong */
@@ -651,7 +651,7 @@ PRIVATE char * scan_cookie_sublist ARGS6(
 	char *,		path,
 	int,		port,
 	HTList *,	sublist,
-	char *, 	header,
+	char *,		header,
 	BOOL,		secure)
 {
     HTList *hl = sublist, *next = NULL;
@@ -830,8 +830,8 @@ PRIVATE void LYProcessSetCookies ARGS6(
 	CONST char *,	SetCookie,
 	CONST char *,	SetCookie2,
 	CONST char *,	address,
-	char *, 	hostname,
-	char *, 	path,
+	char *,		hostname,
+	char *,		path,
 	int,		port)
 {
     CONST char *p, *attr_start, *attr_end, *value_start, *value_end;
@@ -1932,7 +1932,15 @@ PUBLIC void LYSetCookie ARGS3(
     } else if (!strncasecomp(address, "https:", 6)) {
 	port = 443;
     }
-    path = HTParse(address, "", PARSE_PATH|PARSE_PUNCTUATION);
+    if (((path = HTParse(address, "",
+			 PARSE_PATH|PARSE_PUNCTUATION)) != NULL) &&
+	(ptr = strrchr(path, '/')) != NULL) {
+	if (ptr == path) {
+	    *(ptr+1) = '\0';	/* Leave a single '/' alone */
+	} else {
+	    *ptr = '\0';
+	}
+    }
     if (!(SetCookie && *SetCookie) &&
 	!(SetCookie2 && *SetCookie2)) {
 	/*
@@ -2119,8 +2127,8 @@ PUBLIC void LYLoadCookies ARGS1 (
 
 	expires = atol(expires_a);
 	CTRACE((tfp, "expires:\t%s\n", ctime(&expires)));
-/* 	CTRACE((tfp, "%s\t%s\t%s\t%s\t%ld\t%s\t%s\tREADCOOKIE\n", */
-/* 	    domain, what, path, secure, (long) expires, name, value)); */
+/*	CTRACE((tfp, "%s\t%s\t%s\t%s\t%ld\t%s\t%s\tREADCOOKIE\n", */
+/*	    domain, what, path, secure, (long) expires, name, value)); */
 	moo = newCookie();
 	StrAllocCopy(moo->domain, domain);
 	StrAllocCopy(moo->path, path);
@@ -2168,7 +2176,7 @@ PUBLIC void LYLoadCookies ARGS1 (
 	 *			     otherwise set it.
 	 */
 	moo->flags |= COOKIE_FLAG_FROM_FILE | COOKIE_FLAG_EXPIRES_SET |
-	    		COOKIE_FLAG_PATH_SET;
+			COOKIE_FLAG_PATH_SET;
 	if (domain[0] == '.')
 	    moo->flags |= COOKIE_FLAG_DOMAIN_SET;
 	if (secure[0] != 'F')
@@ -2646,7 +2654,7 @@ Delete_all_cookies_in_domain:
 
 	    if (co->flags & COOKIE_FLAG_FROM_FILE) {
 		HTSprintf0(&buf, "%s\n", gettext("(from a previous session)"));
-	        PUTS(buf);
+		PUTS(buf);
 	    }
 
 	    /*
@@ -2705,7 +2713,7 @@ Delete_all_cookies_in_domain:
 	     *	Show the Maximum Gobble Date. - FM
 	     */
 	    HTSprintf0(&buf, "<dd><em>%s</em> %s%s",
-	    		 gettext("Maximum Gobble Date:"),
+			 gettext("Maximum Gobble Date:"),
 			 ((co->flags & COOKIE_FLAG_EXPIRES_SET)
 					    ?
 			ctime(&co->expires) : END_OF_SESSION),
@@ -2738,8 +2746,8 @@ Delete_all_cookies_in_domain:
 */
 
 PUBLIC void cookie_domain_flag_set ARGS2(
-	char *, 	domainstr,
-	int, 		flag)
+	char *,		domainstr,
+	int,		flag)
 {
     domain_entry *de = NULL;
     domain_entry *de2 = NULL;
@@ -2850,7 +2858,7 @@ PUBLIC void cookie_domain_flag_set ARGS2(
 }
 
 /*
- * If any COOKIE_{ACCEPT,REJECT}_DOMAINS have been defined, process them. 
+ * If any COOKIE_{ACCEPT,REJECT}_DOMAINS have been defined, process them.
  * These are comma delimited lists of domains.  - BJP
  *
  * And for query/strict/loose invalid cookie checking.  - BJP
diff --git a/src/LYCurses.c b/src/LYCurses.c
index 2f43537b..24d249d1 100644
--- a/src/LYCurses.c
+++ b/src/LYCurses.c
@@ -36,6 +36,10 @@ extern int _NOSHARE(COLS);
 #include <LYHash.h>
 #endif
 
+#ifdef NEED_WCHAR_H
+#include <wchar.h>
+#endif
+
 #if defined(COLOR_CURSES)
 int lynx_has_color = FALSE;
 #endif
@@ -555,7 +559,7 @@ PUBLIC void curses_style ARGS2(
 PRIVATE BOOL lynx_called_initscr = FALSE;
 #endif
 
-#if HAVE_USE_DEFAULT_COLORS && USE_DEFAULT_COLORS
+#if defined(HAVE_USE_DEFAULT_COLORS) && defined(USE_DEFAULT_COLORS)
 /*
  * If we find a "default" color while reading the config-file, set default
  * colors on the screen.
@@ -1000,7 +1004,7 @@ PUBLIC void start_curses NOARGS
 	if (has_colors()) {
 	    lynx_has_color = TRUE;
 	    start_color();
-#if USE_DEFAULT_COLORS
+#ifdef USE_DEFAULT_COLORS
 #ifdef EXP_ASSUMED_COLOR
 	    /*
 	     * Adjust the color mapping table to match the ASSUMED_COLOR
@@ -1652,29 +1656,147 @@ PUBLIC void LYtouchline ARGS1(
 }
 
 /*
- * There's no guarantee that a library won't temporarily write on its input.
- * Be safe and copy it when we have const-data.
+ * Wrapper for waddnstr().
  */
 PUBLIC void LYwaddnstr ARGS3(
 	WINDOW *,	w,
-	CONST char *,	s,
+	CONST char *,	src,
 	size_t,		len)
 {
+    /*
+     * We only want to trace this function for the color-style code.  It would
+     * be too much logging if not needed.
+     */
 #ifdef USE_COLOR_STYLE
     if (TRACE) {
 	int y, x;
 	LYGetYX(y, x);
-	CTRACE2(TRACE_STYLE, (tfp, "[%2d,%2d] LYwaddnstr(%.*s)\n", y, x, (int) len, s));
+	CTRACE2(TRACE_STYLE, (tfp, "[%2d,%2d] LYwaddnstr(%.*s)\n", y, x, (int) len, src));
     }
 #endif
+    /*
+     * Wide (multibyte) characters are always written as part of a string.  So
+     * we can handle the conversion in one place.
+     *
+     * X/Open curses documents addstr() as able to handle multibyte sequences
+     * directly, but that is not (2001/11/5) yet implemented in ncurses.  Two
+     * alternatives are possible:  translating the string to an array of
+     * wchar_t's or to an array of cchar_t's.  The former is more direct.  Both
+     * have problems with combining-characters in this version of ncurses
+     * (successive calls are not merged), so I'm using them for testing -TD
+     */
+#ifdef WIDEC_CURSES
+#if 1	/* array of wchar_t's */
+    {
+	static wchar_t *temp = 0;
+	static size_t used = 0;
+
+	wchar_t wch;
+	int l = 0;
+	mbstate_t state;
+	size_t rc;
+	int width;
+	unsigned j;
+	size_t need;
+
+	memset(&state, 0, sizeof(state));
+	need = 1 + len;
+	if (need > used) {
+	    used = 2 * need;
+	    CTRACE((tfp, "allocated %d (%d)\n", used, len));
+	    FREE(temp);
+	    temp = typecallocn(wchar_t, used);
+	}
+	for (j = 0; j < len; j++) {
+	    rc = mbrtowc(&wch, src + j, len - j, &state);
+	    if (rc == 0 || rc == (size_t)(-1) || rc == (size_t)(-2))
+		break;
+	    j += rc - 1;
+	    if ((width = wcwidth(wch)) < 0)
+		break;
+	    temp[l++] = wch;
+	}
+	temp[l] = L'\0';
+	waddnwstr(w, temp, l);
+#ifdef LY_FIND_LEAKS
+	FREE(temp);
+	used = 0;
+#endif
+    }
+#else	/* array of cchar_t's */
+    {
+	static cchar_t *temp = 0;
+	static size_t used = 0;
+
+	wchar_t wch;
+	wchar_t wstr[CCHARW_MAX + 1];
+	int l = 0;
+	mbstate_t state;
+	size_t rc;
+	int width;
+	int y, x;
+	unsigned j, k;
+	size_t need;
+	attr_t attr;
+	short pair;
+
+	wattr_get(w, &attr, &pair, (void *)0);
+
+	memset(&state, 0, sizeof(state));
+	need = 1 + len;
+	if (need > used) {
+	    used = 2 * need;
+	    CTRACE((tfp, "allocated %d (%d)\n", used, len));
+	    FREE(temp);
+	    temp = typecallocn(cchar_t, used);
+	}
+	for (j = k = 0; j < len; j++) {
+	    rc = mbrtowc(&wch, src + j, len - j, &state);
+	    if (rc == 0 || rc == (size_t)(-1) || rc == (size_t)(-2))
+		break;
+	    j += rc - 1;
+	    if ((width = wcwidth(wch)) < 0)
+		break;
+	    if ((width > 0 && l > 0) || l == CCHARW_MAX) {
+		wstr[l] = L'\0';
+		l = 0;
+		if (setcchar(temp + k, wstr, attr, 0, NULL) != OK)
+		    break;
+		++k;
+	    }
+	    if (width == 0 && l == 0)
+		wstr[l++] = L' ';
+	    wstr[l++] = wch;
+	}
+	if (l > 0) {
+	    wstr[l] = L'\0';
+	    if (setcchar(temp + k, wstr, attr, 0, NULL) == OK)
+		++k;
+	}
+	setcchar(temp + k, L"", 0, 0, NULL);
+	wadd_wchnstr (w, temp, k);
+	getyx(w, y, x);		/* we didn't move - do it now */
+	wmove(w, y, x + k);
+#ifdef LY_FIND_LEAKS
+	FREE(temp);
+	used = 0;
+#endif
+    }
+#endif
+#else
+    /*
+     * There's no guarantee that a library won't temporarily write on its input.
+     * Be safe and copy it when we have const-data.
+     */
     while (len > 0) {
 	char temp[MAX_LINE];
 	size_t use = (len >= MAX_LINE) ? MAX_LINE - 1 : len;
-	memcpy(temp, s, use);
+	memcpy(temp, src, use);
 	temp[use] = 0;
 	waddstr(w, temp);
 	len -= use;
     }
+#endif
 }
 
 #ifdef VMS
diff --git a/src/LYCurses.h b/src/LYCurses.h
index 5de3fc6b..9ac77ed7 100644
--- a/src/LYCurses.h
+++ b/src/LYCurses.h
@@ -171,22 +171,27 @@ typedef struct {
 #endif
 
 #ifdef HAVE_CONFIG_H
-# ifdef HAVE_NCURSES_NCURSES_H
-#   include <ncurses/ncurses.h>
+# ifdef HAVE_NCURSESW_NCURSES_H
+#  undef GCC_PRINTFLIKE		/* <libutf8.h> may define 'printf' */
+#  include <ncursesw/ncurses.h>
 # else
-#  ifdef HAVE_NCURSES_H
-#   include <ncurses.h>
+#  ifdef HAVE_NCURSES_NCURSES_H
+#   include <ncurses/ncurses.h>
 #  else
-#   ifdef HAVE_CURSESX_H
-#    include <cursesX.h>	/* Ultrix */
+#   ifdef HAVE_NCURSES_H
+#    include <ncurses.h>
 #   else
-#    ifdef HAVE_JCURSES_H
-#     include <jcurses.h>	/* sony_news */
+#    ifdef HAVE_CURSESX_H
+#     include <cursesX.h>	/* Ultrix */
 #    else
-#     ifdef HAVE_XCURSES
-#      include <xcurses.h>	/* PDCurses' UNIX port */
+#     ifdef HAVE_JCURSES_H
+#      include <jcurses.h>	/* sony_news */
 #     else
-#      include <curses.h>	/* default */
+#      ifdef HAVE_XCURSES
+#       include <xcurses.h>	/* PDCurses' UNIX port */
+#      else
+#       include <curses.h>	/* default */
+#      endif
 #     endif
 #    endif
 #   endif
@@ -613,8 +618,8 @@ FANCY_CURSES.  Check your config.log to see why the FANCY_CURSES test failed.
  * If the screen library allows us to specify "default" color, allow user to 
  * control it.
  */
-#if USE_DEFAULT_COLORS
-#if USE_SLANG || (HAVE_ASSUME_DEFAULT_COLORS && !defined(USE_COLOR_STYLE))
+#ifdef USE_DEFAULT_COLORS
+#if USE_SLANG || (defined(HAVE_ASSUME_DEFAULT_COLORS) && !defined(USE_COLOR_STYLE))
 #define EXP_ASSUMED_COLOR 1
 #endif
 #endif
diff --git a/src/LYLeaks.c b/src/LYLeaks.c
index 6f188ebe..3f014d33 100644
--- a/src/LYLeaks.c
+++ b/src/LYLeaks.c
@@ -785,7 +785,7 @@ PRIVATE char * LYLeakSAVsprintf ARGS6(
 
 /* Note: the following may need updating if HTSprintf in HTString.c
  * is changed. - kw */
-#if ANSI_VARARGS
+#ifdef ANSI_VARARGS
 PRIVATE char * LYLeakHTSprintf (char **pstr, CONST char *fmt, ...)
 #else
 PRIVATE char * LYLeakHTSprintf (va_alist)
@@ -797,7 +797,7 @@ PRIVATE char * LYLeakHTSprintf (va_alist)
     va_list ap;
     LYva_start(ap,fmt);
     {
-#if !ANSI_VARARGS
+#ifndef ANSI_VARARGS
 	char **		pstr = va_arg(ap, char **);
 	CONST char *	fmt  = va_arg(ap, CONST char *);
 #endif
@@ -812,7 +812,7 @@ PRIVATE char * LYLeakHTSprintf (va_alist)
 
 /* Note: the following may need updating if HTSprintf0 in HTString.c
  * is changed. - kw */
-#if ANSI_VARARGS
+#ifdef ANSI_VARARGS
 PRIVATE char * LYLeakHTSprintf0 (char **pstr, CONST char *fmt, ...)
 #else
 PRIVATE char * LYLeakHTSprintf0 (va_alist)
@@ -823,7 +823,7 @@ PRIVATE char * LYLeakHTSprintf0 (va_alist)
     va_list ap;
     LYva_start(ap,fmt);
     {
-#if !ANSI_VARARGS
+#ifndef ANSI_VARARGS
 	char **		pstr = va_arg(ap, char **);
 	CONST char *	fmt  = va_arg(ap, CONST char *);
 #endif
diff --git a/src/LYMain.c b/src/LYMain.c
index 36303f4c..71ae0780 100644
--- a/src/LYMain.c
+++ b/src/LYMain.c
@@ -3477,7 +3477,7 @@ keys (may be incompatible with some curses packages)"
       "nolog",		4|UNSET_ARG,		error_logging,
       "disable mailing of error messages to document owners"
    ),
-#if HAVE_SIGACTION && defined(SIGWINCH)
+#if defined(HAVE_SIGACTION) && defined(SIGWINCH)
    PARSE_SET(
       "nonrestarting_sigwinch", 4|SET_ARG,	LYNonRestartingSIGWINCH,
       "\nmake window size change handler non-restarting"
diff --git a/src/LYMainLoop.c b/src/LYMainLoop.c
index 886213c3..e19a908c 100644
--- a/src/LYMainLoop.c
+++ b/src/LYMainLoop.c
@@ -217,7 +217,7 @@ PRIVATE BOOL textinput_activated = FALSE;
 #endif
 #ifdef INACTIVE_INPUT_STYLE_VH
 PUBLIC BOOL textinput_redrawn = FALSE;
-    /*must be public since used in highlight(..)*/
+    /*must be public since used in LYhighlight(..)*/
 #endif
 
 #ifdef LY_FIND_LEAKS
@@ -494,7 +494,7 @@ PRIVATE void set_curdoc_link ARGS1(
      && nextlink >= 0
      && nextlink < nlinks) {
 	if (curdoc.link >= 0 && curdoc.link < nlinks)
-	    highlight(OFF, curdoc.link, prev_target);
+	    LYhighlight(OFF, curdoc.link, prev_target);
 	curdoc.link = nextlink;
     }
 }
@@ -3615,7 +3615,7 @@ PRIVATE void handle_LYK_MAIN_MENU ARGS2(
 	    newdoc.isHEAD = FALSE;
 	    newdoc.safe = FALSE;
 	    newdoc.internal_link = FALSE;
-	    highlight(OFF, curdoc.link, prev_target);
+	    LYhighlight(OFF, curdoc.link, prev_target);
 #ifdef DIRED_SUPPORT
 	    if (lynx_edit_mode) {
 		DIRED_UNCACHE_2;
@@ -3903,7 +3903,7 @@ PRIVATE void handle_LYK_NEXT_LINK ARGS3(
     int,	real_c)
 {
     if (curdoc.link < nlinks-1) {	/* next link */
-	highlight(OFF, curdoc.link, prev_target);
+	LYhighlight(OFF, curdoc.link, prev_target);
 #ifdef FASTTAB
 	/*
 	 *  Move to different textarea if TAB in textarea.
@@ -6549,10 +6549,10 @@ try_again:
 	      *  text input field.
 	      */
 	    if (!curlink_is_editable) {
-		highlight(ON, curdoc.link, prev_target);
+		LYhighlight(ON, curdoc.link, prev_target);
 #ifndef INACTIVE_INPUT_STYLE_VH
 	    } else if (!textinput_activated) {
-		highlight(ON, curdoc.link, prev_target);
+		LYhighlight(ON, curdoc.link, prev_target);
 #endif
 	    }
 	}
diff --git a/src/LYOptions.c b/src/LYOptions.c
index eebb3f93..69c48fac 100644
--- a/src/LYOptions.c
+++ b/src/LYOptions.c
@@ -2172,7 +2172,7 @@ static OptValues show_color_values[] = {
 
 static char * show_cursor_string	= "show_cursor";
 
-#if USE_SCROLLBAR
+#ifdef USE_SCROLLBAR
 static char * show_scrollbar_string	= "show_scrollbar";
 #endif
 
diff --git a/src/LYReadCFG.c b/src/LYReadCFG.c
index 93a6457c..6c956bdb 100644
--- a/src/LYReadCFG.c
+++ b/src/LYReadCFG.c
@@ -236,7 +236,7 @@ PUBLIC int match_item_by_name ARGS3(
 #define COLOR_BLACK 0
 #endif
 
-#if USE_DEFAULT_COLORS
+#ifdef USE_DEFAULT_COLORS
 int default_fg = DEFAULT_COLOR;
 int default_bg = DEFAULT_COLOR;
 #else
@@ -294,7 +294,7 @@ PUBLIC int check_color ARGS2(
 
     CTRACE2(TRACE_STYLE, (tfp, "check_color(%s,%d)\n", color, the_default));
     if (!strcasecomp(color, "default")) {
-#if USE_DEFAULT_COLORS
+#ifdef USE_DEFAULT_COLORS
 	if (!default_color_reset)
 	    the_default = DEFAULT_COLOR;
 #endif	/* USE_DEFAULT_COLORS */
diff --git a/src/LYReadCFG.h b/src/LYReadCFG.h
index b6349cc0..e58a1074 100644
--- a/src/LYReadCFG.h
+++ b/src/LYReadCFG.h
@@ -41,7 +41,7 @@ extern int default_fg;
 extern int default_bg;
 extern BOOL default_color_reset;
 
-#if HAVE_USE_DEFAULT_COLORS && USE_DEFAULT_COLORS
+#if defined(HAVE_USE_DEFAULT_COLORS) && defined(USE_DEFAULT_COLORS)
 extern int lynx_default_colors NOPARAMS;
 #endif
 
diff --git a/src/LYSearch.c b/src/LYSearch.c
index 03961129..1afa5eb2 100644
--- a/src/LYSearch.c
+++ b/src/LYSearch.c
@@ -332,7 +332,7 @@ check_recall:
 	    /*
 	     *  Found in link, changed cur, we're done.
 	     */
-	    highlight(OFF, oldcur, prev_target);
+	    LYhighlight(OFF, oldcur, prev_target);
 	    return(TRUE);
 	}
     } else {
@@ -345,7 +345,7 @@ check_recall:
 	    /*
 	     *  Found in link, changed cur, we're done.
 	     */
-	    highlight(OFF, oldcur, prev_target);
+	    LYhighlight(OFF, oldcur, prev_target);
 	    return(TRUE);
 	}
 
@@ -366,7 +366,7 @@ check_recall:
      */
     www_user_search((cur_doc->line + offset), cur_doc, prev_target, direction);
     if (cur_doc->link != oldcur) {
-	highlight(OFF, oldcur, prev_target);
+	LYhighlight(OFF, oldcur, prev_target);
 	return(TRUE);
     }
     return (BOOL) (www_search_result > 0);
diff --git a/src/LYSignal.h b/src/LYSignal.h
index e0d5ec0a..9faf619b 100644
--- a/src/LYSignal.h
+++ b/src/LYSignal.h
@@ -12,7 +12,7 @@ extern void VMSsignal PARAMS((int sig, void (*func)()));
 #define signal(a,b) VMSsignal(a,b) /* use LYCurses.c routines for interrupts */
 #endif /* VMS */
 
-#if HAVE_SIGACTION
+#ifdef HAVE_SIGACTION
 typedef void LYSigHandlerFunc_t PARAMS((int));
 /* implementation in LYUtils.c */
 extern void LYExtSignal PARAMS((int sig, LYSigHandlerFunc_t * handler));
diff --git a/src/LYStrings.c b/src/LYStrings.c
index 5a3df082..483f7b02 100644
--- a/src/LYStrings.c
+++ b/src/LYStrings.c
@@ -668,7 +668,7 @@ PUBLIC int LYmbcsstrlen ARGS3(
 		j++;
 	    }
 	} else if (!utf_flag && HTCJK != NOCJK && !count_gcells &&
-		   !isascii(str[i]) && str[(i + 1)] != '\0' &&
+		   is8bits(str[i]) && str[(i + 1)] != '\0' &&
 		    !IsSpecialAttrChar(str[(i + 1)])) {
 	    i++;
 	}
@@ -1613,7 +1613,7 @@ re_read:
 #else
     if (c == EOF && errno == EINTR) {
 
-#if HAVE_SIZECHANGE || defined(USE_SLANG)
+#if defined(HAVE_SIZECHANGE) || defined(USE_SLANG)
 	   CTRACE((tfp, "Got EOF with EINTR, recent_sizechange so far is %d\n",
 		  recent_sizechange));
 	   if (!recent_sizechange) { /* not yet detected by ourselves */
@@ -1971,7 +1971,7 @@ re_read:
 #endif /* KEY_BTAB */
 #ifdef KEY_RESIZE
 	case KEY_RESIZE:	   /* size change detected by ncurses */
-#if HAVE_SIZECHANGE || defined(USE_SLANG)
+#if defined(HAVE_SIZECHANGE) || defined(USE_SLANG)
 	    /* Make call to detect new size, if that may be implemented.
 	     * The call may set recent_sizechange (except for USE_SLANG),
 	     * which will tell mainloop() to refresh. - kw
@@ -2400,7 +2400,8 @@ PUBLIC void LYLowerCase ARGS1(
     for (i = 0; buffer[i]; i++)
 #ifdef SUPPORT_MULTIBYTE_EDIT	/* 1998/11/23 (Mon) 17:04:55 */
     {
-	if (buffer[i] & 0x80) {
+	if (buffer[i] & 0x80
+	 && buffer[i+1] != 0) {
 	    if ((kanji_code == SJIS) && IS_SJIS_X0201KANA(UCH((buffer[i])))) {
 		continue;
 	    }
@@ -2425,7 +2426,8 @@ PUBLIC void LYUpperCase ARGS1(
     for (i = 0; buffer[i]; i++)
 #ifdef SUPPORT_MULTIBYTE_EDIT	/* 1998/11/23 (Mon) 17:05:10 */
     {
-	if (buffer[i] & 0x80) {
+	if (buffer[i] & 0x80
+	 && buffer[i+1] != 0) {
 	    if ((kanji_code == SJIS) && IS_SJIS_X0201KANA(UCH((buffer[i])))) {
 		continue;
 	    }
@@ -2440,6 +2442,21 @@ PUBLIC void LYUpperCase ARGS1(
 }
 
 /*
+ * Remove newlines from a string.
+ */
+PUBLIC void LYRemoveNewlines ARGS1(
+	char *,		buffer)
+{
+    if (buffer != 0) {
+	size_t i, j;
+	for (i = j = 0; buffer[i]; i++)
+	    if (buffer[i] != '\n' && buffer[i] != '\r')
+		buffer[j++] = buffer[i];
+	buffer[j] = 0;
+    }
+}
+
+/*
  * Remove ALL whitespace from a string (including embedded blanks).
  */
 PUBLIC void LYRemoveBlanks ARGS1(
@@ -2668,8 +2685,8 @@ PRIVATE int prev_pos ARGS2(
 	while (i < pos - 1) {
 	    int c;
 	    c = Buf[i];
-	    if (!(isascii(c) ||
-		  ((kanji_code == SJIS) && IS_SJIS_X0201KANA(UCH(c))))) {
+	    if (is8bits(c) &&
+		  !((kanji_code == SJIS) && IS_SJIS_X0201KANA(UCH(c)))) {
 		i++;
 	    }
 	    i++;
@@ -2902,19 +2919,19 @@ PUBLIC int LYEdit1 ARGS4(
 
 	    pos0 = prev_pos(edit, Pos);
 	    while (Pos &&
-		   (HTCJK == NOCJK || isascii(Buf[pos0])) &&
+		   (HTCJK == NOCJK || !is8bits(Buf[pos0])) &&
 		   !isalnum(UCH(Buf[pos0]))) {
 		Pos = pos0;
 		pos0 = prev_pos(edit, Pos);
 	    }
-	    if (HTCJK != NOCJK && !isascii(Buf[pos0])) {
-		while (Pos && !isascii(Buf[pos0])) {
+	    if (HTCJK != NOCJK && is8bits(Buf[pos0])) {
+		while (Pos && is8bits(Buf[pos0])) {
 		    Pos = pos0;
 		    pos0 = prev_pos(edit, Pos);
 		}
 	    } else {
 		while (Pos
-		 && isascii(UCH(Buf[pos0]))
+		 && !is8bits(Buf[pos0])
 		 && isalnum(UCH(Buf[pos0]))) {
 		    Pos = pos0;
 		    pos0 = prev_pos(edit, Pos);
@@ -2934,14 +2951,14 @@ PUBLIC int LYEdit1 ARGS4(
 	while (!isalnum(Buf[Pos]) && Buf[Pos])
 	    Pos++ ;
 #else /* SUPPORT_MULTIBYTE_EDIT */
-	if (HTCJK != NOCJK && !isascii(Buf[Pos])) {
-	    while (!isascii(Buf[Pos]))
+	if (HTCJK != NOCJK && is8bits(Buf[Pos])) {
+	    while (is8bits(Buf[Pos]))
 		Pos += 2;
 	} else {
-	    while (isascii(UCH(Buf[Pos])) && isalnum(UCH(Buf[Pos])))
+	    while (!is8bits(Buf[Pos]) && isalnum(Buf[Pos]))
 		Pos++;	/* '\0' is not a/n */
 	}
-	while ((HTCJK == NOCJK || isascii(UCH(Buf[Pos]))) &&
+	while ((HTCJK == NOCJK || !is8bits(Buf[Pos])) &&
 	       !isalnum(UCH(Buf[Pos])) && Buf[Pos])
 	    Pos++;
 #endif /* SUPPORT_MULTIBYTE_EDIT */
@@ -3028,7 +3045,7 @@ PUBLIC int LYEdit1 ARGS4(
 	if (Pos >= length)
 	    break;
 #ifdef SUPPORT_MULTIBYTE_EDIT
-	if (HTCJK != NOCJK && !isascii(Buf[Pos]))
+	if (HTCJK != NOCJK && is8bits(Buf[Pos]))
 	    Pos++;
 #endif
 	Pos++;
@@ -3088,7 +3105,7 @@ PUBLIC int LYEdit1 ARGS4(
 #else /* SUPPORT_MULTIBYTE_EDIT */
 	if (Pos < length) {
 	    Pos++;
-	    if (HTCJK != NOCJK && !isascii(Buf[Pos-1]))
+	    if (HTCJK != NOCJK && is8bits(Buf[Pos-1]))
 		Pos++;
 	}
 #endif /* SUPPORT_MULTIBYTE_EDIT */
@@ -3345,7 +3362,7 @@ PUBLIC void LYRefreshEdit ARGS1(
 		int tmp = (Pos - DspWdth) + Margin;
 
 		while (DspStart < tmp) {
-		    if (!isascii(Buf[DspStart]))
+		    if (is8bits(Buf[DspStart]))
 			DspStart++;
 		    DspStart++;
 		}
@@ -3367,7 +3384,7 @@ PUBLIC void LYRefreshEdit ARGS1(
 
 	    DspStart = 0;
 	    while (DspStart < tmp) {
-		if (!isascii(Buf[DspStart]))
+		if (is8bits(Buf[DspStart]))
 		    DspStart++;
 		DspStart++;
 	    }
@@ -3381,7 +3398,7 @@ PUBLIC void LYRefreshEdit ARGS1(
 
     str = &Buf[DspStart];
 #ifdef SUPPORT_MULTIBYTE_EDIT
-    if (HTCJK != NOCJK && !isascii(str[0]))
+    if (HTCJK != NOCJK && is8bits(str[0]))
 	begin_multi = 1;
 #endif /* SUPPORT_MULTIBYTE_EDIT */
 
@@ -3439,16 +3456,22 @@ PUBLIC void LYRefreshEdit ARGS1(
 #endif /* SUPPORT_MULTIBYTE_EDIT */
 	    } else {
 		/* For CJK strings, by Masanobu Kimura */
-		if (HTCJK != NOCJK && !isascii(buffer[0])) {
-#ifndef SUPPORT_MULTIBYTE_EDIT
+		if (HTCJK != NOCJK && is8bits(buffer[0])) {
 		    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;
+#ifdef SUPPORT_MULTIBYTE_EDIT
+		    end_multi = (i < nrdisplayed);
+#if !(defined(USE_SLANG) || defined(WIDEC_CURSES))
+		    {
+			int ii, yy, xx;
+
+			LYGetYX(yy, xx);
+			for (ii = 0; buffer[ii] != '\0'; ++ii)
+			    LYaddch(' ');
+			LYrefresh();
+			LYmove(yy, xx);
+		    }
+#endif /* USE_SLANG */
 #endif /* SUPPORT_MULTIBYTE_EDIT */
 		    LYaddstr(buffer);
 		    buffer[1] = '\0';
@@ -3511,12 +3534,6 @@ PUBLIC void LYRefreshEdit ARGS1(
     }
 
     LYmove(edit->sy, edit->sx + Pos - DspStart);
-#ifdef SUPPORT_MULTIBYTE_EDIT
-#if (!USE_SLANG && !defined(USE_MULTIBYTE_CURSES))
-    if (HTCJK != NOCJK)
-	lynx_force_repaint();
-#endif /* !USE_SLANG && !defined(USE_MULTIBYTE_CURSES) */
-#endif /* SUPPORT_MULTIBYTE_EDIT */
 
 #ifdef USE_COLOR_STYLE
     if (estyle != NOSTYLE)
@@ -5120,7 +5137,7 @@ PUBLIC char * LYno_attr_mbcs_case_strstr ARGS6(
      *	Seek a first target match. - FM
      */
     for (; *chptr != '\0'; chptr++) {
-	if ((!utf_flag && HTCJK != NOCJK && !isascii(*chptr) &&
+	if ((!utf_flag && HTCJK != NOCJK && is8bits(*chptr) &&
 	     *chptr == *tarptr &&
 	     *(chptr + 1) != '\0' &&
 	     !IsSpecialAttrChar(*(chptr + 1))) ||
@@ -5143,7 +5160,7 @@ PUBLIC char * LYno_attr_mbcs_case_strstr ARGS6(
 		if (nendp)	*nendp = len;
 		return(chptr);
 	    }
-	    if (!utf_flag && HTCJK != NOCJK && !isascii(*chptr) &&
+	    if (!utf_flag && HTCJK != NOCJK && is8bits(*chptr) &&
 		 *chptr == *tarptr &&
 		 *tmpchptr != '\0' &&
 		 !IsSpecialAttrChar(*tmpchptr)) {
@@ -5180,7 +5197,7 @@ PUBLIC char * LYno_attr_mbcs_case_strstr ARGS6(
 	     */
 	    while (1) {
 		if (!IsSpecialAttrChar(*tmpchptr)) {
-		    if (!utf_flag && HTCJK != NOCJK && !isascii(*tmpchptr)) {
+		    if (!utf_flag && HTCJK != NOCJK && is8bits(*tmpchptr)) {
 			if (*tmpchptr == *tmptarptr &&
 			    *(tmpchptr + 1) == *(tmptarptr + 1) &&
 			    !IsSpecialAttrChar(*(tmpchptr + 1))) {
@@ -5214,7 +5231,7 @@ PUBLIC char * LYno_attr_mbcs_case_strstr ARGS6(
 	    }
 	} else if (!(IS_UTF_EXTRA(*chptr) ||
 		      IsSpecialAttrChar(*chptr))) {
-	    if (!utf_flag && HTCJK != NOCJK && !isascii(*chptr) &&
+	    if (!utf_flag && HTCJK != NOCJK && is8bits(*chptr) &&
 		*(chptr + 1) != '\0' &&
 		!IsSpecialAttrChar(*(chptr + 1))) {
 		chptr++;
@@ -5292,7 +5309,7 @@ PUBLIC char * LYno_attr_mbcs_strstr ARGS6(
 		if (nendp)	*nendp = len;
 		return(chptr);
 	    }
-	    if (!utf_flag && HTCJK != NOCJK && !isascii(*chptr) &&
+	    if (!utf_flag && HTCJK != NOCJK && is8bits(*chptr) &&
 		 *tmpchptr != '\0' &&
 		 !IsSpecialAttrChar(*tmpchptr)) {
 		/*
@@ -5328,7 +5345,7 @@ PUBLIC char * LYno_attr_mbcs_strstr ARGS6(
 	     */
 	    while (1) {
 		 if (!IsSpecialAttrChar(*tmpchptr)) {
-		    if (!utf_flag && HTCJK != NOCJK && !isascii(*tmpchptr)) {
+		    if (!utf_flag && HTCJK != NOCJK && is8bits(*tmpchptr)) {
 			if (*tmpchptr == *tmptarptr &&
 			    *(tmpchptr + 1) == *(tmptarptr + 1) &&
 			    !IsSpecialAttrChar(*(tmpchptr + 1))) {
@@ -5361,7 +5378,7 @@ PUBLIC char * LYno_attr_mbcs_strstr ARGS6(
 	    }
 	} else if (!(IS_UTF_EXTRA(*chptr) ||
 		      IsSpecialAttrChar(*chptr))) {
-	    if (!utf_flag && HTCJK != NOCJK && !isascii(*chptr) &&
+	    if (!utf_flag && HTCJK != NOCJK && is8bits(*chptr) &&
 		*(chptr + 1) != '\0' &&
 		!IsSpecialAttrChar(*(chptr + 1))) {
 		chptr++;
diff --git a/src/LYStrings.h b/src/LYStrings.h
index 081e00e3..0fcebc98 100644
--- a/src/LYStrings.h
+++ b/src/LYStrings.h
@@ -10,6 +10,8 @@ typedef enum {
     , RECALL_MAIL
 } RecallType;
 
+#define is8bits(ch) (UCH(ch) >= 128)	/* isascii(ch) is not POSIX */
+
 /*  UPPER8(ch1,ch2) is an extension of (TOUPPER(ch1) - TOUPPER(ch2))  */
 extern int UPPER8  PARAMS((
 	int		ch1,
@@ -298,6 +300,8 @@ extern void LYLowerCase PARAMS((
 	char *		buffer));
 extern void LYUpperCase PARAMS((
 	char *		buffer));
+extern void LYRemoveNewlines PARAMS((
+	char *		buffer));
 extern void LYRemoveBlanks PARAMS((
 	char *		buffer));
 extern char * LYSkipBlanks PARAMS((
diff --git a/src/LYUtils.c b/src/LYUtils.c
index a743f4ad..d5aa910f 100644
--- a/src/LYUtils.c
+++ b/src/LYUtils.c
@@ -56,7 +56,7 @@ extern int exec_command(char * cmd, int wait_flag); /* xsystem.c */
 #include <lib$routines.h>
 #endif /* VMS */
 
-#if HAVE_UTMP
+#ifdef HAVE_UTMP
 #include <pwd.h>
 #ifdef UTMPX_FOR_UTMP
 #include <utmpx.h>
@@ -192,9 +192,46 @@ PRIVATE char *getenv_text ARGS1(char *, name)
 }
 
 /*
+ * Check for UTF-8 data, returning the length past the first character.
+ * Return zero if we found an ordinary character rather than UTF-8.
+ */
+PUBLIC size_t utf8_length ARGS2(
+	BOOL,		utf_flag,
+	CONST char *,	data)
+{
+    size_t utf_extra = 0;
+
+    if (utf_flag && is8bits(*data)) {
+	if ((*data & 0xe0) == 0xc0) {
+	    utf_extra = 1;
+	} else if ((*data & 0xf0) == 0xe0) {
+	    utf_extra = 2;
+	} else if ((*data & 0xf8) == 0xf0) {
+	    utf_extra = 3;
+	} else if ((*data & 0xfc) == 0xf8) {
+	    utf_extra = 4;
+	} else if ((*data & 0xfe) == 0xfc) {
+	    utf_extra = 5;
+	} else {
+	    /*
+	     *  Garbage.
+	     */
+	    utf_extra = 0;
+	}
+	if (strlen(data+1) < utf_extra) {
+	    /*
+	     *  Shouldn't happen.
+	     */
+	    utf_extra = 0;
+	}
+    }
+    return utf_extra;
+}
+
+/*
  *  Highlight (or unhighlight) a given link.
  */
-PUBLIC void highlight ARGS3(
+PUBLIC void LYhighlight ARGS3(
 	int,		flag,
 	int,		cur,
 	char *,		target)
@@ -214,7 +251,7 @@ PUBLIC void highlight ARGS3(
     BOOL utf_flag = (BOOL)(LYCharSet_UC[current_char_set].enc == UCT_ENC_UTF8);
     BOOL hl1_drawn = NO;
 #if defined(USE_COLOR_STYLE) && !defined(NO_HILIT_FIX)
-    BOOL hl2_drawn=FALSE;	/* whether links[cur].hightext2 is already drawn
+    BOOL hl2_drawn = FALSE;	/* whether links[cur].hightext2 is already drawn
 				   properly */
 #endif
     tmp[0] = tmp[1] = tmp[2] = '\0';
@@ -375,7 +412,7 @@ PUBLIC void highlight ARGS3(
 		    /*
 		     *	For CJK strings, by Masanobu Kimura.
 		     */
-		    if (HTCJK != NOCJK && !isascii(tmp[0])) {
+		    if (HTCJK != NOCJK && is8bits(tmp[0])) {
 			tmp[1] = links[cur].hightext2[++i];
 			LYaddstr(tmp);
 			tmp[1] = '\0';
@@ -486,30 +523,7 @@ PUBLIC void highlight ARGS3(
 		 */
 		LYmove(hLine, offset);
 		tmp[0] = data[itmp];
-		if (utf_flag && !isascii(tmp[0])) {
-		    if ((*tmp & 0xe0) == 0xc0) {
-			utf_extra = 1;
-		    } else if ((*tmp & 0xf0) == 0xe0) {
-			utf_extra = 2;
-		    } else if ((*tmp & 0xf8) == 0xf0) {
-			utf_extra = 3;
-		    } else if ((*tmp & 0xfc) == 0xf8) {
-			utf_extra = 4;
-		    } else if ((*tmp & 0xfe) == 0xfc) {
-			utf_extra = 5;
-		    } else {
-			/*
-			 *  Garbage.
-			 */
-			utf_extra = 0;
-		    }
-		    if (strlen(&data[itmp+1]) < utf_extra) {
-			/*
-			 *  Shouldn't happen.
-			 */
-			utf_extra = 0;
-		    }
-		}
+		utf_extra = utf8_length(utf_flag, data + itmp);
 		if (utf_extra) {
 		    LYstrncpy(&tmp[1], &data[itmp+1], utf_extra);
 		    itmp += utf_extra;
@@ -527,7 +541,7 @@ PUBLIC void highlight ARGS3(
 		    tmp[1] = '\0';
 		    written += (utf_extra + 1);
 		    utf_extra = 0;
-		} else if (HTCJK != NOCJK && !isascii(tmp[0])) {
+		} else if (HTCJK != NOCJK && is8bits(tmp[0])) {
 		    /*
 		     *	For CJK strings, by Masanobu Kimura.
 		     */
@@ -583,30 +597,7 @@ PUBLIC void highlight ARGS3(
 		     *	character of hightext and we are making
 		     *	the link current. - FM
 		     */
-		    if (utf_flag && !isascii(tmp[0])) {
-			if ((*tmp & 0xe0) == 0xc0) {
-			    utf_extra = 1;
-			} else if ((*tmp & 0xf0) == 0xe0) {
-			    utf_extra = 2;
-			} else if ((*tmp & 0xf8) == 0xf0) {
-			    utf_extra = 3;
-			} else if ((*tmp & 0xfc) == 0xf8) {
-			    utf_extra = 4;
-			} else if ((*tmp & 0xfe) == 0xfc) {
-			    utf_extra = 5;
-			} else {
-			    /*
-			     *	Garbage.
-			     */
-			    utf_extra = 0;
-			}
-			if (strlen(&data[itmp+1]) < utf_extra) {
-			    /*
-			     *	Shouldn't happen.
-			     */
-			    utf_extra = 0;
-			}
-		    }
+		    utf_extra = utf8_length(utf_flag, data + itmp);
 		    if (utf_extra) {
 			LYstrncpy(&tmp[1], &data[itmp+1], utf_extra);
 			itmp += utf_extra;
@@ -626,7 +617,7 @@ PUBLIC void highlight ARGS3(
 			tmp[1] = '\0';
 			written += (utf_extra + 1);
 			utf_extra = 0;
-		    } else if (HTCJK != NOCJK && !isascii(tmp[0])) {
+		    } else if (HTCJK != NOCJK && is8bits(tmp[0])) {
 			/*
 			 *  For CJK strings, by Masanobu Kimura.
 			 */
@@ -756,30 +747,7 @@ highlight_hit_within_hightext:
 	     */
 	    LYmove(hLine, offset);
 	    tmp[0] = data[itmp];
-	    if (utf_flag && !isascii(tmp[0])) {
-		if ((*tmp & 0xe0) == 0xc0) {
-		    utf_extra = 1;
-		} else if ((*tmp & 0xf0) == 0xe0) {
-		    utf_extra = 2;
-		} else if ((*tmp & 0xf8) == 0xf0) {
-		    utf_extra = 3;
-		} else if ((*tmp & 0xfc) == 0xf8) {
-		    utf_extra = 4;
-		} else if ((*tmp & 0xfe) == 0xfc) {
-		    utf_extra = 5;
-		} else {
-		    /*
-		     *	Garbage.
-		     */
-		    utf_extra = 0;
-		}
-		if (strlen(&data[itmp+1]) < utf_extra) {
-		    /*
-		     *	Shouldn't happen.
-		     */
-		    utf_extra = 0;
-		}
-	    }
+	    utf_extra = utf8_length(utf_flag, data + itmp);
 	    if (utf_extra) {
 		LYstrncpy(&tmp[1], &data[itmp+1], utf_extra);
 		itmp += utf_extra;
@@ -800,7 +768,7 @@ highlight_hit_within_hightext:
 		tmp[1] = '\0';
 		written += (utf_extra + 1);
 		utf_extra = 0;
-	    } else if (HTCJK != NOCJK && !isascii(tmp[0])) {
+	    } else if (HTCJK != NOCJK && is8bits(tmp[0])) {
 		/*
 		 *  For CJK strings, by Masanobu Kimura.
 		 */
@@ -859,30 +827,7 @@ highlight_hit_within_hightext:
 		 *  character of hightext and we are making
 		 *  the link current. - FM
 		 */
-		if (utf_flag && !isascii(tmp[0])) {
-		    if ((*tmp & 0xe0) == 0xc0) {
-			utf_extra = 1;
-		    } else if ((*tmp & 0xf0) == 0xe0) {
-			utf_extra = 2;
-		    } else if ((*tmp & 0xf8) == 0xf0) {
-			utf_extra = 3;
-		    } else if ((*tmp & 0xfc) == 0xf8) {
-			utf_extra = 4;
-		    } else if ((*tmp & 0xfe) == 0xfc) {
-			utf_extra = 5;
-		    } else {
-			/*
-			 *  Garbage.
-			 */
-			utf_extra = 0;
-		    }
-		    if (strlen(&data[itmp+1]) < utf_extra) {
-			/*
-			 *  Shouldn't happen.
-			 */
-			utf_extra = 0;
-		    }
-		}
+		utf_extra = utf8_length(utf_flag, data + itmp);
 		if (utf_extra) {
 		    LYstrncpy(&tmp[1], &data[itmp+1], utf_extra);
 		    itmp += utf_extra;
@@ -902,7 +847,7 @@ highlight_hit_within_hightext:
 		    tmp[1] = '\0';
 		    written += (utf_extra + 1);
 		    utf_extra = 0;
-		} else if (HTCJK != NOCJK && !isascii(tmp[0])) {
+		} else if (HTCJK != NOCJK && is8bits(tmp[0])) {
 		    /*
 		     *	For CJK strings, by Masanobu Kimura.
 		     */
@@ -1021,30 +966,7 @@ highlight_hit_within_hightext:
 			 *  character of hightext and we are making
 			 *  the link current. - FM
 			 */
-			if (utf_flag && !isascii(tmp[0])) {
-			    if ((*tmp & 0xe0) == 0xc0) {
-				utf_extra = 1;
-			    } else if ((*tmp & 0xf0) == 0xe0) {
-				utf_extra = 2;
-			    } else if ((*tmp & 0xf8) == 0xf0) {
-				utf_extra = 3;
-			    } else if ((*tmp & 0xfc) == 0xf8) {
-				utf_extra = 4;
-			    } else if ((*tmp & 0xfe) == 0xfc) {
-				utf_extra = 5;
-			    } else {
-				/*
-				 *  Garbage.
-				 */
-				utf_extra = 0;
-			    }
-			    if (strlen(&data[itmp+1]) < utf_extra) {
-				/*
-				 *  Shouldn't happen.
-				 */
-				utf_extra = 0;
-			    }
-			}
+			utf_extra = utf8_length(utf_flag, data);
 			if (utf_extra) {
 			    LYstrncpy(&tmp[1], &data[itmp+1], utf_extra);
 			    itmp += utf_extra;
@@ -1064,7 +986,7 @@ highlight_hit_within_hightext:
 			    tmp[1] = '\0';
 			    written += (utf_extra + 1);
 			    utf_extra = 0;
-			} else if (HTCJK != NOCJK && !isascii(tmp[0])) {
+			} else if (HTCJK != NOCJK && is8bits(tmp[0])) {
 			    /*
 			     *	For CJK strings, by Masanobu Kimura.
 			     */
@@ -1198,30 +1120,7 @@ highlight_search_hightext2:
 		 */
 		LYmove(hLine, offset);
 		tmp[0] = data[itmp];
-		if (utf_flag && !isascii(tmp[0])) {
-		    if ((*tmp & 0xe0) == 0xc0) {
-			utf_extra = 1;
-		    } else if ((*tmp & 0xf0) == 0xe0) {
-			utf_extra = 2;
-		    } else if ((*tmp & 0xf8) == 0xf0) {
-			utf_extra = 3;
-		    } else if ((*tmp & 0xfc) == 0xf8) {
-			utf_extra = 4;
-		    } else if ((*tmp & 0xfe) == 0xfc) {
-			utf_extra = 5;
-		    } else {
-			/*
-			 *  Garbage.
-			 */
-			utf_extra = 0;
-		    }
-		    if (strlen(&data[itmp+1]) < utf_extra) {
-			/*
-			 *  Shouldn't happen.
-			 */
-			utf_extra = 0;
-		    }
-		}
+		utf_extra = utf8_length(utf_flag, data + itmp);
 		if (utf_extra) {
 		    LYstrncpy(&tmp[1], &data[itmp+1], utf_extra);
 		    itmp += utf_extra;
@@ -1239,7 +1138,7 @@ highlight_search_hightext2:
 		    tmp[1] = '\0';
 		    written += (utf_extra + 1);
 		    utf_extra = 0;
-		} else if (HTCJK != NOCJK && !isascii(tmp[0])) {
+		} else if (HTCJK != NOCJK && is8bits(tmp[0])) {
 		    /*
 		     *	For CJK strings, by Masanobu Kimura.
 		     */
@@ -1295,30 +1194,7 @@ highlight_search_hightext2:
 		     *	character of hightext2 and we are making
 		     *	the link current. - FM
 		     */
-		    if (utf_flag && !isascii(tmp[0])) {
-			if ((*tmp & 0xe0) == 0xc0) {
-			    utf_extra = 1;
-			} else if ((*tmp & 0xf0) == 0xe0) {
-			    utf_extra = 2;
-			} else if ((*tmp & 0xf8) == 0xf0) {
-			    utf_extra = 3;
-			} else if ((*tmp & 0xfc) == 0xf8) {
-			    utf_extra = 4;
-			} else if ((*tmp & 0xfe) == 0xfc) {
-			    utf_extra = 5;
-			} else {
-			    /*
-			     *	Garbage.
-			     */
-			    utf_extra = 0;
-			}
-			if (strlen(&data[itmp+1]) < utf_extra) {
-			    /*
-			     *	Shouldn't happen.
-			     */
-			    utf_extra = 0;
-			}
-		    }
+		    utf_extra = utf8_length(utf_flag, data + itmp);
 		    if (utf_extra) {
 			LYstrncpy(&tmp[1], &data[itmp+1], utf_extra);
 			itmp += utf_extra;
@@ -1338,7 +1214,7 @@ highlight_search_hightext2:
 			tmp[1] = '\0';
 			written += (utf_extra + 1);
 			utf_extra = 0;
-		    } else if (HTCJK != NOCJK && !isascii(tmp[0])) {
+		    } else if (HTCJK != NOCJK && is8bits(tmp[0])) {
 			/*
 			 *  For CJK strings, by Masanobu Kimura.
 			 */
@@ -1467,30 +1343,7 @@ highlight_hit_within_hightext2:
 	     */
 	    LYmove(hLine, offset);
 	    tmp[0] = data[itmp];
-	    if (utf_flag && !isascii(tmp[0])) {
-		if ((*tmp & 0xe0) == 0xc0) {
-		    utf_extra = 1;
-		} else if ((*tmp & 0xf0) == 0xe0) {
-		    utf_extra = 2;
-		} else if ((*tmp & 0xf8) == 0xf0) {
-		    utf_extra = 3;
-		} else if ((*tmp & 0xfc) == 0xf8) {
-		    utf_extra = 4;
-		} else if ((*tmp & 0xfe) == 0xfc) {
-		    utf_extra = 5;
-		} else {
-		    /*
-		     *	Garbage.
-		     */
-		    utf_extra = 0;
-		}
-		if (strlen(&data[itmp+1]) < utf_extra) {
-		    /*
-		     *	Shouldn't happen.
-		     */
-		    utf_extra = 0;
-		}
-	    }
+	    utf_extra = utf8_length(utf_flag, data + itmp);
 	    if (utf_extra) {
 		LYstrncpy(&tmp[1], &data[itmp+1], utf_extra);
 		itmp += utf_extra;
@@ -1511,7 +1364,7 @@ highlight_hit_within_hightext2:
 		tmp[1] = '\0';
 		written += (utf_extra + 1);
 		utf_extra = 0;
-	    } else if (HTCJK != NOCJK && !isascii(tmp[0])) {
+	    } else if (HTCJK != NOCJK && is8bits(tmp[0])) {
 		/*
 		 *  For CJK strings, by Masanobu Kimura.
 		 */
@@ -1570,30 +1423,7 @@ highlight_hit_within_hightext2:
 		 *  character of hightext2 and we are making
 		 *  the link current. - FM
 		 */
-		if (utf_flag && !isascii(tmp[0])) {
-		    if ((*tmp & 0xe0) == 0xc0) {
-			utf_extra = 1;
-		    } else if ((*tmp & 0xf0) == 0xe0) {
-			utf_extra = 2;
-		    } else if ((*tmp & 0xf8) == 0xf0) {
-			utf_extra = 3;
-		    } else if ((*tmp & 0xfc) == 0xf8) {
-			utf_extra = 4;
-		    } else if ((*tmp & 0xfe) == 0xfc) {
-			utf_extra = 5;
-		    } else {
-			/*
-			 *  Garbage.
-			 */
-			utf_extra = 0;
-		    }
-		    if (strlen(&data[itmp+1]) < utf_extra) {
-			/*
-			 *  Shouldn't happen.
-			 */
-			utf_extra = 0;
-		    }
-		}
+		utf_extra = utf8_length(utf_flag, data + itmp);
 		if (utf_extra) {
 		    LYstrncpy(&tmp[1], &data[itmp+1], utf_extra);
 		    itmp += utf_extra;
@@ -1613,7 +1443,7 @@ highlight_hit_within_hightext2:
 		    tmp[1] = '\0';
 		    written += (utf_extra + 1);
 		    utf_extra = 0;
-		} else if (HTCJK != NOCJK && !isascii(tmp[0])) {
+		} else if (HTCJK != NOCJK && is8bits(tmp[0])) {
 		    /*
 		     *	For CJK strings, by Masanobu Kimura.
 		     */
@@ -1732,30 +1562,7 @@ highlight_hit_within_hightext2:
 			 *  character of hightext2 and we are making
 			 *  the link current. - FM
 			 */
-			if (utf_flag && !isascii(tmp[0])) {
-			    if ((*tmp & 0xe0) == 0xc0) {
-				utf_extra = 1;
-			    } else if ((*tmp & 0xf0) == 0xe0) {
-				utf_extra = 2;
-			    } else if ((*tmp & 0xf8) == 0xf0) {
-				utf_extra = 3;
-			    } else if ((*tmp & 0xfc) == 0xf8) {
-				utf_extra = 4;
-			    } else if ((*tmp & 0xfe) == 0xfc) {
-				utf_extra = 5;
-			    } else {
-				/*
-				 *  Garbage.
-				 */
-				utf_extra = 0;
-			    }
-			    if (strlen(&data[itmp+1]) < utf_extra) {
-				/*
-				 *  Shouldn't happen.
-				 */
-				utf_extra = 0;
-			    }
-			}
+			utf_extra = utf8_length(utf_flag, data + itmp);
 			if (utf_extra) {
 			    LYstrncpy(&tmp[1], &data[itmp+1], utf_extra);
 			    itmp += utf_extra;
@@ -1775,7 +1582,7 @@ highlight_hit_within_hightext2:
 			    tmp[1] = '\0';
 			    written += (utf_extra + 1);
 			    utf_extra = 0;
-			} else if (HTCJK != NOCJK && !isascii(tmp[0])) {
+			} else if (HTCJK != NOCJK && is8bits(tmp[0])) {
 			    /*
 			     *	For CJK strings, by Masanobu Kimura.
 			     */
@@ -3268,7 +3075,7 @@ PUBLIC void remove_backslashes ARGS1(
  */
 PUBLIC BOOLEAN inlocaldomain NOARGS
 {
-#if HAVE_UTMP
+#ifdef HAVE_UTMP
     int n;
     FILE *fp;
     struct utmp me;
@@ -3307,7 +3114,7 @@ PUBLIC BOOLEAN inlocaldomain NOARGS
 #endif /* HAVE_UTMP */
 }
 
-#if HAVE_SIGACTION
+#ifdef HAVE_SIGACTION
 /*
  *  An extended alternative for calling signal(), sets some flags for
  *  signal handler as we want them if that functionality is available.
@@ -3338,7 +3145,7 @@ PUBLIC void LYExtSignal ARGS2(
 #endif /* HAVE_SIGACTION */
 
 #if defined(SIGTSTP) && !defined(USE_SLANG)
-#if HAVE_SIGACTION
+#ifdef HAVE_SIGACTION
 /*
  *  For switching a signal's handling between SIG_DFL and something
  *  (possibly) different that may have been set up by lynx code or
@@ -3436,7 +3243,7 @@ PUBLIC void size_change ARGS1(
 	 */
 	return;
 #else /* Curses: */
-#if HAVE_SIZECHANGE
+#ifdef HAVE_SIZECHANGE
 #ifdef TIOCGSIZE
     struct ttysize win;
 #else
@@ -5600,7 +5407,7 @@ PUBLIC CONST char * Home_Dir NOARGS
 	    }
 	    StrAllocCopy(HomeDir, cp);
 #else
-#if HAVE_UTMP
+#ifdef HAVE_UTMP
 	    /*
 	     *	One could use getlogin() and getpwnam() here instead.
 	     */
@@ -6523,6 +6330,10 @@ PUBLIC BOOLEAN LYCachedTemp ARGS2(
     return FALSE;
 }
 
+#ifndef HAVE_MKDTEMP
+#define mkdtemp(path) ((mktemp(path) != 0) && (mkdir(path, 0700) == 0))
+#endif
+
 /*
  * Open a temp-file, ensuring that it is unique, and not readable by other
  * users.
@@ -6579,8 +6390,7 @@ PUBLIC FILE *LYOpenTemp ARGS3(
 	if (make_it) {
 	    int old_mask = umask(HIDE_UMASK);
 	    StrAllocCat(lynx_temp_space, "XXXXXX");
-	    if (mktemp(lynx_temp_space) == 0
-	     || mkdir(lynx_temp_space, 0700) < 0) {
+	    if (mkdtemp(lynx_temp_space) == 0) {
 		printf("%s: %s\n", lynx_temp_space, LYStrerror(errno));
 		exit(EXIT_FAILURE);
 	    }
@@ -7548,7 +7358,7 @@ PUBLIC int LYSystem ARGS1(
 {
     int code;
     int do_free = 0;
-#if HAVE_SIGACTION && defined(SIGTSTP) && !defined(USE_SLANG)
+#if defined(HAVE_SIGACTION) && defined(SIGTSTP) && !defined(USE_SLANG)
     struct sigaction saved_sigtstp_act;
     BOOLEAN sigtstp_saved = FALSE;
 #endif
@@ -7651,13 +7461,13 @@ PUBLIC int LYSystem ARGS1(
 #else
     if (restore_sigpipe_for_children)
 	signal(SIGPIPE, SIG_DFL); /* Some commands expect the default */
-#if HAVE_SIGACTION && defined(SIGTSTP) && !defined(USE_SLANG)
+#if defined(HAVE_SIGACTION) && defined(SIGTSTP) && !defined(USE_SLANG)
     if (!dump_output_immediately && !LYCursesON && !no_suspend)
 	sigtstp_saved = LYToggleSigDfl(SIGTSTP, &saved_sigtstp_act, 1);
 #endif
     code = system(command);
     saved_errno = errno;
-#if HAVE_SIGACTION && defined(SIGTSTP) && !defined(USE_SLANG)
+#if defined(HAVE_SIGACTION) && defined(SIGTSTP) && !defined(USE_SLANG)
     if (sigtstp_saved)
 	LYToggleSigDfl(SIGTSTP, &saved_sigtstp_act, 0);
 #endif
diff --git a/src/LYUtils.h b/src/LYUtils.h
index 95c89eb2..575f872c 100644
--- a/src/LYUtils.h
+++ b/src/LYUtils.h
@@ -107,6 +107,7 @@ extern int LYValidateOutput PARAMS((char * filename));
 extern int find_restriction PARAMS((CONST char * name, int len));
 extern int is_url PARAMS((char *filename));
 extern int number2arrows PARAMS((int number));
+extern size_t utf8_length PARAMS((BOOL utf_flag, CONST char * data));
 extern time_t LYmktime PARAMS((char *string, BOOL absolute));
 extern void BeginInternalPage PARAMS((FILE *fp0, char *Title, char *HelpURL));
 extern void EndInternalPage PARAMS((FILE *fp0));
@@ -134,11 +135,11 @@ extern void LYRenamedTemp PARAMS((char * oldname, char * newname));
 extern void LYTrimHtmlSep PARAMS((char *path));
 extern void LYTrimPathSep PARAMS((char *path));
 extern void LYTrimRelFromAbsPath PARAMS((char *path));
+extern void LYhighlight PARAMS((int flag, int cur, char *target));
 extern void LYsetXDisplay PARAMS((char *new_display));
 extern void change_sug_filename PARAMS((char *fname));
 extern void convert_to_spaces PARAMS((char *string, BOOL condense));
 extern void free_and_clear PARAMS((char **obj));
-extern void highlight PARAMS((int flag, int cur, char *target));
 extern void noviceline PARAMS((int more_flag));
 extern void parse_restrictions PARAMS((CONST char *s));
 extern void print_restrictions_to_fd PARAMS((FILE *fp));
diff --git a/src/LYrcFile.c b/src/LYrcFile.c
index f79d8d11..fba32428 100644
--- a/src/LYrcFile.c
+++ b/src/LYrcFile.c
@@ -240,8 +240,6 @@ PRIVATE void put_editor ARGS2(FILE *, fp, struct config_type *, tbl)
 /* This table is searched ignoring case */
 static Config_Type Config_Table [] =
 {
-    MAYBE_ENU("DTD_recovery",          Old_DTD,            tbl_DTD_recovery,
-              MSG_ENABLE_LYNXRC),
     PARSE_SET("accept_all_cookies",    LYAcceptAllCookies, N_("\
 accept_all_cookies allows the user to tell Lynx to automatically\n\
 accept all cookies if desired.  The default is \"FALSE\" which will\n\
@@ -403,7 +401,7 @@ WARNING - This is potentially dangerous.  Since you may view\n\
           you are viewing trusted source information.\n\
 ")),
 #endif
-#if USE_SCROLLBAR
+#ifdef USE_SCROLLBAR
     MAYBE_SET("scrollbar",             LYShowScrollbar, MSG_ENABLE_LYNXRC),
 #endif
     PARSE_SET("select_popups",         LYSelectPopups, N_("\
@@ -452,7 +450,7 @@ restricted via a command line switch.  If display of hidden files\n\
 is disabled, creation of such files via Lynx also is disabled.\n\
 ")),
 #ifdef EXP_READPROGRESS
-    MAYBE_ENU("show_rate",             LYTransferRate,    tbl_transfer_rate,
+    MAYBE_ENU("show_kb_rate",          LYTransferRate,    tbl_transfer_rate,
 	      MSG_ENABLE_LYNXRC),
 #endif
     PARSE_ENU("sub_bookmarks",         LYMultiBookmarks,  tbl_multi_bookmarks, N_("\
@@ -466,7 +464,8 @@ statusline prompt instead of the menu seen in novice and intermediate\n\
 user modes.  When this option is set to \"standard\", the menu will be\n\
 presented regardless of user mode.\n\
 ")),
-    MAYBE_STR("user_agent",            LYUserAgent,        MSG_ENABLE_LYNXRC),
+    MAYBE_ENU("tagsoup",               Old_DTD,            tbl_DTD_recovery,
+              MSG_ENABLE_LYNXRC),
     PARSE_ENU("user_mode",             user_mode,          tbl_user_mode, N_("\
 user_mode specifies the users level of knowledge with Lynx.  The\n\
 default is \"NOVICE\" which displays two extra lines of help at the\n\
@@ -475,6 +474,7 @@ commands.  Set user_mode to \"INTERMEDIATE\" to turn off the extra info.\n\
 Use \"ADVANCED\" to see the URL of the currently selected link at the\n\
 bottom of the screen.\n\
 ")),
+    MAYBE_STR("useragent",             LYUserAgent,        MSG_ENABLE_LYNXRC),
     PARSE_SET("verbose_images",        verbose_img, N_("\
 If verbose_images is \"on\", lynx will print the name of the image\n\
 source file in place of [INLINE], [LINK] or [IMAGE]\n\