# ftp://ftp.invisible-island.net/xterm/patches/xterm-341.patch.gz
# patch by Thomas E. Dickey <dickey@invisible-island.net>
# created  Tue Dec 25 01:44:55 UTC 2018
# ------------------------------------------------------------------------------
# MANIFEST                 |    2 
# button.c                 |   23 ++--
# charproc.c               |    6 -
# ctlseqs.ms               |   19 ++-
# ctlseqs.txt              |   29 +++---
# fontutils.c              |  214 ++++++++++++++++++++++++++++++---------------
# misc.c                   |    3 
# package/debian/changelog |    6 +
# package/freebsd/Makefile |    2 
# package/xterm.spec       |    4 
# ptyx.h                   |    6 +
# screen.c                 |   10 +-
# trace.c                  |   11 ++
# trace.h                  |    4 
# version.h                |    6 -
# vttests/mouse-codes      |   49 +++++++++-
# xterm.log.html           |   33 ++++++
# 17 files changed, 303 insertions(+), 124 deletions(-)
# ------------------------------------------------------------------------------
Index: MANIFEST
--- xterm-340+/MANIFEST	2018-12-14 00:10:48.000000000 +0000
+++ xterm-341/MANIFEST	2018-12-18 10:25:26.000000000 +0000
@@ -1,4 +1,4 @@
-MANIFEST for xterm-340, version xterm-340
+MANIFEST for xterm-341, version xterm-341
 --------------------------------------------------------------------------------
 MANIFEST                        this file
 256colres.h                     resource-definitions for 256-color mode
Index: button.c
--- xterm-340+/button.c	2018-12-15 15:02:52.000000000 +0000
+++ xterm-341/button.c	2018-12-24 15:07:40.000000000 +0000
@@ -1,4 +1,4 @@
-/* $XTermId: button.c,v 1.560 2018/12/15 15:02:52 tom Exp $ */
+/* $XTermId: button.c,v 1.562 2018/12/24 15:07:40 tom Exp $ */
 
 /*
  * Copyright 1999-2017,2018 by Thomas E. Dickey
@@ -3945,7 +3945,7 @@
     if (scp->data_limit <= (unsigned) need) {
 	if ((line = (Char *) malloc((size_t) need + 1)) == 0)
 	    SysError(ERROR_BMALLOC2);
-	XtFree((char *) scp->data_buffer);
+	free(scp->data_buffer);
 	scp->data_buffer = line;
 	scp->data_limit = (size_t) (need + 1);
     } else {
@@ -3986,8 +3986,9 @@
     int which = TargetToSelection(screen, selection);
     SelectedCells *scp = &(screen->selected_cells[which >= 0 ? which : 0]);
     if (scp->data_buffer) {
-	XtFree((char *) (scp->data_buffer));
+	free(scp->data_buffer);
 	scp->data_buffer = 0;
+	scp->data_limit = 0;
     }
     scp->data_length = 0;
     screen->base64_count = 0;
@@ -4487,7 +4488,7 @@
 	    if ((buf = (Char *) malloc((size_t) scp->data_length)) == 0)
 		SysError(ERROR_BMALLOC2);
 
-	    XtFree((char *) screen->clipboard_data);
+	    free(screen->clipboard_data);
 	    memcpy(buf, scp->data_buffer, scp->data_length);
 	    screen->clipboard_data = buf;
 	    screen->clipboard_size = scp->data_length;
@@ -4694,14 +4695,15 @@
     return (result);
 }
 
-/* 32 + following 7-bit word:
+/* 32 + following 8-bit word:
 
    1:0  Button no: 0, 1, 2.  3=release.
      2  shift
      3  meta
      4  ctrl
      5  set for motion notify
-     6  set for wheel
+     6  set for wheel (and button 6 and 7)
+     7  set for buttons 8 to 11
 */
 
 /* Position: 32 - 255. */
@@ -4716,9 +4718,11 @@
     if (button < 0) {
 	result += 3;
     } else {
-	if (button > 3)
-	    result += (64 - 4);
-	result += button;
+	result += button & 3;
+	if (button & 4)
+	    result += 64;
+	if (button & 8)
+	    result += 128;
     }
     TRACE(("BtnCode button %d, %s state " FMT_MODIFIER_NAMES " ->%#x\n",
 	   button,
@@ -5118,6 +5122,7 @@
 	    result[scp->data_limit] = 0;
 	}
 	free(scp->data_buffer);
+	scp->data_limit = 0;
     }
 
     TRACE(("...getDataFromScreen restoring previous selection\n"));
Index: charproc.c
--- xterm-340+/charproc.c	2018-12-09 14:05:20.000000000 +0000
+++ xterm-341/charproc.c	2018-12-20 23:39:26.000000000 +0000
@@ -1,4 +1,4 @@
-/* $XTermId: charproc.c,v 1.1618 2018/12/09 14:05:20 tom Exp $ */
+/* $XTermId: charproc.c,v 1.1619 2018/12/20 23:39:26 tom Exp $ */
 
 /*
  * Copyright 1999-2017,2018 by Thomas E. Dickey
@@ -9765,7 +9765,9 @@
 	for (k = 0; k < fMAX; ++k) {
 	    if (screen->menu_font_names[n][k] !=
 		screen->cacheVTFonts.menu_font_names[n][k]) {
-		TRACE_FREE_LEAK(screen->menu_font_names[n][k]);
+		if (screen->menu_font_names[n][k] != _Font_Selected_) {
+		    TRACE_FREE_LEAK(screen->menu_font_names[n][k]);
+		}
 		TRACE_FREE_LEAK(screen->cacheVTFonts.menu_font_names[n][k]);
 	    } else {
 		TRACE_FREE_LEAK(screen->menu_font_names[n][k]);
Index: ctlseqs.ms
--- xterm-340+/ctlseqs.ms	2018-11-22 02:19:21.000000000 +0000
+++ xterm-341/ctlseqs.ms	2018-12-24 15:29:04.000000000 +0000
@@ -1,6 +1,6 @@
 .\"#! troff -ms $1		-*- Nroff -*-
 .\" "Xterm Control Sequences" document
-.\" $XTermId: ctlseqs.ms,v 1.485 2018/11/22 02:19:21 tom Exp $
+.\" $XTermId: ctlseqs.ms,v 1.486 2018/12/24 15:29:04 tom Exp $
 .\"
 .\"
 .\" Copyright 1996-2017,2018 by Thomas E. Dickey
@@ -69,8 +69,8 @@
 .\"
 .ds XT XTerm
 .ds xt xterm
-.ds LF Patch #336
-.ds RF 2018/11/21
+.ds LF Patch #341
+.ds RF 2018/12/24
 .\"
 .if n .pl 9999v		\" no page breaks in nroff
 .ND
@@ -2991,10 +2991,15 @@
 using the \fBalternateScroll\fP resource.
 .Ss Other buttons
 .LP
-Additional buttons are encoded like the wheel mice, by adding 64.
-The encoding is unambiguous for buttons 6 and 7,
-which are seen for some tiltable wheel mice.
-However, it is not possible to use these buttons in \fI\*(xt\fP's
+Additional buttons are encoded like the wheel mice,
+.bP
+by adding 64 (for buttons 6 and 7), or
+.bP
+by adding 128 (for buttons 8 through 11).
+.LP
+Past button 11, the encoding is ambiguous because the same code
+may correspond to different button/modifier combinations.
+It is not possible to use these buttons (6-11) in \fI\*(xt\fP's
 \fBtranslation\fP resource because their names are not in the X Toolkit's
 symbol table.
 .Ss Highlight tracking
Index: ctlseqs.txt
--- xterm-340+/ctlseqs.txt	2018-11-22 02:24:30.000000000 +0000
+++ xterm-341/ctlseqs.txt	2018-12-24 15:29:54.000000000 +0000
@@ -21,7 +21,7 @@
                              Thomas Dickey
                       XFree86 Project (1996-2006)
                     invisible-island.net (2006-2018)
-               updated for XTerm Patch #336 (2018/11/21)
+               updated for XTerm Patch #341 (2018/12/24)
 
 
 
@@ -2185,11 +2185,16 @@
 
 Other buttons
 
-Additional buttons are encoded like the wheel mice, by adding 64.  The
-encoding is unambiguous for buttons 6 and 7, which are seen for some
-tiltable wheel mice.  However, it is not possible to use these buttons
-in xterm's translation resource because their names are not in the X
-Toolkit's symbol table.
+Additional buttons are encoded like the wheel mice,
+
+o   by adding 64 (for buttons 6 and 7), or
+
+o   by adding 128 (for buttons 8 through 11).
+
+Past button 11, the encoding is ambiguous because the same code may cor-
+respond to different button/modifier combinations.  It is not possible
+to use these buttons (6-11) in xterm's translation resource because
+their names are not in the X Toolkit's symbol table.
 
 
 Highlight tracking
@@ -2749,12 +2754,12 @@
         be used in termcap for vi, for example, to turn off saving of
         lines, but restore whatever the original state was on exit.
 
-    while  the  SCOSC/SCORC pair was added in 1995 by XFree86 (and docu-
+    while the SCOSC/SCORC pair was added in 1995 by XFree86  (and  docu-
     mented long afterwards).
 
 o   The aixterm manual page gives the format of the control sequence for
-    foreground  and  background  colors  8-15, but does not specify what
-    those colors are.  That is implied by the description's  mention  of
+    foreground and background colors 8-15, but  does  not  specify  what
+    those  colors  are.  That is implied by the description's mention of
     HFT:
 
         The aixterm command provides a standard terminal type for
@@ -2764,7 +2769,7 @@
         the -v flag.
 
     Unlike xterm, there are no resource names for the 16 colors, leaving
-    the reader to assume that the mapping is  hard-coded.   The  control
-    sequences  for  colors 8-15 are not specified by ECMA-48, but rather
-    (as done in other instances by xterm) chosen to  not  conflict  with
+    the  reader  to  assume that the mapping is hard-coded.  The control
+    sequences for colors 8-15 are not specified by ECMA-48,  but  rather
+    (as  done  in  other instances by xterm) chosen to not conflict with
     current or future standards.
Index: fontutils.c
--- xterm-340+/fontutils.c	2018-12-15 15:05:02.000000000 +0000
+++ xterm-341/fontutils.c	2018-12-24 20:11:50.000000000 +0000
@@ -1,4 +1,4 @@
-/* $XTermId: fontutils.c,v 1.609 2018/12/15 15:05:02 tom Exp $ */
+/* $XTermId: fontutils.c,v 1.619 2018/12/24 20:11:50 tom Exp $ */
 
 /*
  * Copyright 1998-2017,2018 by Thomas E. Dickey
@@ -2209,6 +2209,8 @@
     screen->box = VTbox;
 }
 
+#if OPT_RENDERFONT
+
 #define CACHE_XFT(dst,src) if (src.font != 0) {\
 	    int err = checkXft(xw, &(dst[fontnum]), &src);\
 	    TRACE(("Xft metrics %s[%d] = %d (%d,%d)%s advance %d, actual %d%s%s\n",\
@@ -2224,12 +2226,11 @@
 		err ? " ERROR" : ""));\
 	    if (err) {\
 		xtermCloseXft(screen, &src);\
+		memset((&dst[fontnum]), 0, sizeof(dst[fontnum]));\
 		failed += err;\
 	    }\
 	}
 
-#if OPT_RENDERFONT
-
 #if OPT_REPORT_FONTS
 static FcChar32
 xtermXftFirstChar(XftFont *xft)
@@ -2315,38 +2316,45 @@
 #define DUMP_XFT(xw, data)	/* nothing */
 #endif
 
+/*
+ * Check if this is a FC_COLOR font, which fontconfig misrepresents to "fix" a
+ * problem with web browsers.  As of 2018/12 (4 years later), Xft does not work
+ * with that.
+ */
+#ifdef FC_COLOR
+#define GetFcBool(pattern, what) \
+    (FcPatternGetBool(pattern, what, 0, &fcbogus) == FcResultMatch)
+
+static Boolean
+isBogusXft(XftFont *font)
+{
+    Boolean result = False;
+    if (font != 0) {
+	FcBool fcbogus;
+	if (GetFcBool(font->pattern, FC_COLOR) && fcbogus) {
+	    TRACE(("...matched color-bitmap font\n"));
+	    result = True;
+	} else if (GetFcBool(font->pattern, FC_OUTLINE) && !fcbogus) {
+	    TRACE(("...matched non-outline font\n"));
+	    result = True;
+	}
+    }
+    return result;
+}
+#endif
+
 static int
 checkXft(XtermWidget xw, XTermXftFonts *target, XTermXftFonts *source)
 {
     FcChar32 c;
     Dimension width = 0;
     int failed = 0;
-    FcChar32 lo_check = 32;
-    FcChar32 hi_check = 255;
-#ifdef FC_COLOR
-    FcBool fcbogus;
-#endif
 
     target->font = source->font;
     target->pattern = source->pattern;
     target->map.min_width = 0;
     target->map.max_width = (Dimension) source->font->max_advance_width;
 
-#ifdef FC_COLOR
-#define GetFcBogus(pattern, what) \
-    (FcPatternGetBool(pattern, what, 0, &fcbogus) == FcResultMatch)
-    /*
-     * Check if this is a FC_COLOR font, which fontconfig misrepresents to
-     * "fix" a problem with web browsers.  As of 2018/12 (4 years later),
-     * Xft does not work with that.
-     */
-    if (GetFcBogus(source->pattern, FC_COLOR)
-	&& fcbogus) {
-	lo_check = 0;
-	hi_check = 0;
-    }
-#endif
-
     /*
      * For each ASCII or ISO-8859-1 printable code, ask what its width is.
      * Given the maximum width for those, we have a reasonable estimate of
@@ -2354,7 +2362,7 @@
      *
      * Ignore control characters - their extent information is misleading.
      */
-    for (c = lo_check; c < hi_check; ++c) {
+    for (c = 32; c < 255; ++c) {
 	if (c >= 127 && c <= 159)
 	    continue;
 	if (FcCharSetHasChar(source->font->charset, c)) {
@@ -2502,7 +2510,17 @@
 
 	match = FcFontMatch(NULL, pat, &status);
 	if (match != 0) {
+	    Boolean maybeReopened = False;
 	    result = XftFontOpenPattern(dpy, match);
+#ifdef FC_COLOR
+	    if (result != 0) {
+		if (isBogusXft(result)) {
+		    XftFontClose(dpy, result);
+		    result = 0;
+		    maybeReopened = True;
+		}
+	    }
+#endif
 	    if (result != 0) {
 		TRACE(("...matched %s font\n", tag));
 		if (!maybeXftCache(xw, result)) {
@@ -2510,7 +2528,8 @@
 		}
 	    } else {
 		TRACE(("...could not open %s font\n", tag));
-		XftPatternDestroy(match);
+		if (!maybeReopened)
+		    XftPatternDestroy(match);
 		if (xw->misc.fontWarnings >= fwAlways) {
 		    cannotFont(xw, "open", tag, name);
 		}
@@ -2524,9 +2543,7 @@
     }
     return result;
 }
-#endif
 
-#if OPT_RENDERFONT
 #if OPT_SHIFT_FONTS
 /*
  * Don't make a dependency on the math library for a single function.
@@ -2860,9 +2877,12 @@
 checkFontInfo(int value, const char *tag, int failed)
 {
     if (value == 0 || failed) {
-	xtermWarning("Selected font has no non-zero %s for ISO-8859-1 encoding\n", tag);
-	if (value == 0)
+	if (value == 0) {
+	    xtermWarning("Selected font has no non-zero %s for ISO-8859-1 encoding\n", tag);
 	    exit(1);
+	} else {
+	    xtermWarning("Selected font has no valid %s for ISO-8859-1 encoding\n", tag);
+	}
     }
 }
 
@@ -2892,7 +2912,7 @@
 }
 
 /*
- * Get the faceName/faceDoublesize resource setting.
+ * Get the faceName/faceNameDoublesize resource setting.
  */
 String
 getFaceName(XtermWidget xw, Bool wideName GCC_UNUSED)
@@ -2969,7 +2989,7 @@
 	XTermXftFonts wital = screen->renderWideItal[fontnum];
 #endif
 
-	if (norm.font == 0 && face_name) {
+	if (norm.font == 0 && !IsEmpty(face_name)) {
 	    XftPattern *pat;
 	    double face_size;
 
@@ -2988,16 +3008,24 @@
 	     * cumulative.  Build the bold- and italic-patterns on top of the
 	     * normal pattern.
 	     */
+#ifdef FC_COLOR
+#define NormXftPattern \
+	    XFT_FAMILY,     XftTypeString, "mono", \
+	    FC_COLOR,       XftTypeBool,   FcFalse, \
+	    FC_OUTLINE,     XftTypeBool,   FcTrue, \
+	    XFT_SIZE,       XftTypeDouble, face_size
+#else
 #define NormXftPattern \
-	    XFT_FAMILY, XftTypeString, "mono", \
-	    XFT_SIZE, XftTypeDouble, face_size
+	    XFT_FAMILY,     XftTypeString, "mono", \
+	    XFT_SIZE,       XftTypeDouble, face_size
+#endif
 
 #define BoldXftPattern(norm) \
-	    XFT_WEIGHT, XftTypeInteger, XFT_WEIGHT_BOLD, \
+	    XFT_WEIGHT,     XftTypeInteger, XFT_WEIGHT_BOLD, \
 	    XFT_CHAR_WIDTH, XftTypeInteger, norm.font->max_advance_width
 
 #define ItalXftPattern(norm) \
-	    XFT_SLANT, XftTypeInteger, XFT_SLANT_ITALIC, \
+	    XFT_SLANT,      XftTypeInteger, XFT_SLANT_ITALIC, \
 	    XFT_CHAR_WIDTH, XftTypeInteger, norm.font->max_advance_width
 
 #if OPT_WIDE_ATTRS
@@ -3019,15 +3047,16 @@
 		OPEN_XFT(norm, "normal");
 
 		if (norm.font != 0) {
-		    bold.pattern = XftPatternDuplicate(norm.pattern);
+		    bold.pattern = XftPatternDuplicate(pat);
 		    XftPatternBuild(bold.pattern,
+				    NormXftPattern,
 				    BoldXftPattern(norm),
 				    (void *) 0);
 		    OPEN_XFT(bold, "bold");
 
 #if HAVE_ITALICS
 		    if (FIND_ITALICS) {
-			ital.pattern = XftPatternDuplicate(norm.pattern);
+			ital.pattern = XftPatternDuplicate(pat);
 			XftPatternBuild(ital.pattern,
 					NormXftPattern,
 					ItalXftPattern(norm),
@@ -3035,7 +3064,6 @@
 			OPEN_XFT(ital, "italic");
 		    }
 #endif
-#undef OPEN_XFT
 
 		    /*
 		     * FIXME:  just assume that the corresponding font has no
@@ -3048,25 +3076,51 @@
 		    }
 		}
 
+		CACHE_XFT(screen->renderFontNorm, norm);
+
+		CACHE_XFT(screen->renderFontBold, bold);
+		if (norm.font != 0 && !bold.font) {
+		    xtermWarning("did not find a usable bold TrueType font\n");
+		    XftPatternDestroy(bold.pattern);
+		    bold.pattern = XftPatternDuplicate(pat);
+		    XftPatternBuild(bold.pattern,
+				    NormXftPattern,
+				    (void *) 0);
+		    OPEN_XFT(bold, "bold");
+		    failed = 0;
+		    CACHE_XFT(screen->renderFontBold, bold);
+		}
+#if HAVE_ITALICS
+		CACHE_XFT(screen->renderFontItal, ital);
+		if (norm.font != 0 && !ital.font) {
+		    xtermWarning("did not find a usable italic TrueType font\n");
+		    XftPatternDestroy(ital.pattern);
+		    ital.pattern = XftPatternDuplicate(pat);
+		    XftPatternBuild(ital.pattern,
+				    NormXftPattern,
+				    (void *) 0);
+		    OPEN_XFT(ital, "italics");
+		    failed = 0;
+		    CACHE_XFT(screen->renderFontItal, ital);
+		}
+#endif
 		XftPatternDestroy(pat);
+	    } else {
+		failed = 1;
 	    }
-
-	    CACHE_XFT(screen->renderFontNorm, norm);
-	    CACHE_XFT(screen->renderFontBold, bold);
-	    CACHE_XFT(screen->renderFontItal, ital);
+#undef OPEN_XFT
 
 	    /*
-	     * See xtermXftDrawString().
+	     * See xtermXftDrawString().  A separate double-width font is nice
+	     * to have, but not essential.
 	     */
 #if OPT_RENDERWIDE
 	    if (norm.font != 0 && screen->wide_chars) {
 		int char_width = norm.font->max_advance_width * 2;
-#ifdef FC_ASPECT
 		double aspect = ((FirstItemOf(xw->work.fonts.xft.list_w)
 				  || screen->renderFontNorm[fontnum].map.mixed)
 				 ? 1.0
 				 : 2.0);
-#endif
 
 		face_name = getFaceName(xw, True);
 		TRACE(("xtermComputeFontInfo wide(face %s, char_width %d)\n",
@@ -3074,25 +3128,24 @@
 		       char_width));
 
 #define WideXftPattern \
-		XFT_FAMILY, XftTypeString, "mono", \
-		XFT_SIZE, XftTypeDouble, face_size, \
-		XFT_SPACING, XftTypeInteger, XFT_MONO
+		XFT_FAMILY,     XftTypeString,   "mono", \
+		XFT_SIZE,       XftTypeDouble,   face_size, \
+		XFT_SPACING,    XftTypeInteger,  XFT_MONO, \
+		XFT_CHAR_WIDTH, XftTypeInteger,  char_width, \
+		FC_ASPECT,      XftTypeDouble,   aspect
 
-		if (face_name && (pat = XftNameParse(face_name)) != 0) {
+		if (!IsEmpty(face_name) && (pat = XftNameParse(face_name))
+		    != 0) {
 #define OPEN_XFT(name, tag) name.font = xtermOpenXft(xw, face_name, name.pattern, tag)
 		    wnorm.pattern = XftPatternDuplicate(pat);
 		    XftPatternBuild(wnorm.pattern,
 				    WideXftPattern,
-				    XFT_CHAR_WIDTH, XftTypeInteger, char_width,
-#ifdef FC_ASPECT
-				    FC_ASPECT, XftTypeDouble, aspect,
-#endif
 				    (void *) 0);
 		    OPEN_XFT(wnorm, "wide");
 
 		    if (wnorm.font != 0) {
-			wbold.pattern = XftPatternDuplicate(wnorm.pattern);
-			XftPatternBuild(pat,
+			wbold.pattern = XftPatternDuplicate(pat);
+			XftPatternBuild(wbold.pattern,
 					WideXftPattern,
 					BoldXftPattern(wnorm),
 					(void *) 0);
@@ -3100,22 +3153,47 @@
 
 #if HAVE_ITALICS
 			if (FIND_ITALICS) {
-			    wital.pattern = XftPatternDuplicate(wnorm.pattern);
-			    XftPatternBuild(pat,
+			    wital.pattern = XftPatternDuplicate(pat);
+			    XftPatternBuild(wital.pattern,
 					    WideXftPattern,
 					    ItalXftPattern(wnorm),
 					    (void *) 0);
 			    OPEN_XFT(wital, "wide-italic");
 			}
 #endif
-#undef OPEN_XFT
 		    }
+
+		    CACHE_XFT(screen->renderWideNorm, wnorm);
+
+		    CACHE_XFT(screen->renderWideBold, wbold);
+		    if (wnorm.font != 0 && !wbold.font) {
+			xtermWarning("did not find a usable wide-bold TrueType font\n");
+			XftPatternDestroy(wbold.pattern);
+			wbold.pattern = XftPatternDuplicate(pat);
+			XftPatternBuild(bold.pattern,
+					WideXftPattern,
+					(void *) 0);
+			OPEN_XFT(wbold, "wide-bold");
+			failed = 0;
+			CACHE_XFT(screen->renderWideBold, bold);
+		    }
+
+		    CACHE_XFT(screen->renderWideItal, wital);
+		    if (wnorm.font != 0 && !wital.font) {
+			xtermWarning("did not find a usable wide-italic TrueType font\n");
+			XftPatternDestroy(wital.pattern);
+			wital.pattern = XftPatternDuplicate(pat);
+			XftPatternBuild(wital.pattern,
+					WideXftPattern,
+					(void *) 0);
+			OPEN_XFT(wital, "wide-italic");
+			failed = 0;
+			CACHE_XFT(screen->renderWideItal, wital);
+		    }
+
 		    XftPatternDestroy(pat);
 		}
-
-		CACHE_XFT(screen->renderWideNorm, wnorm);
-		CACHE_XFT(screen->renderWideBold, wbold);
-		CACHE_XFT(screen->renderWideItal, wital);
+#undef OPEN_XFT
 	    }
 #endif /* OPT_RENDERWIDE */
 	}
@@ -3726,8 +3804,9 @@
 	return 0;
     }
     /* the end of the BMP is reserved for non-characters */
-    if (wc >= 0xfff0 && wc <= 0xffff)
+    if (wc >= 0xfff0 && wc <= 0xffff) {
 	return 0;
+    }
 
     for (n = 0; n < XtNumber(table); ++n) {
 	XTermXftFonts *check = (XTermXftFonts *) ((void *) ((char *) screen
@@ -3832,15 +3911,8 @@
 		    }
 #ifdef FC_COLOR
 		    if (!giveup) {
-			FcBool fcbogus;
-			if (GetFcBogus(check->pattern, FC_COLOR)
-			    && (fcbogus != FcFalse)) {
-			    giveup = True;
-			    TRACE(("ignoring color-bitmap font match by fontconfig\n"));
-			} else if (GetFcBogus(check->pattern, FC_OUTLINE)
-				   && (fcbogus == FcFalse)) {
+			if (isBogusXft(check)) {
 			    giveup = True;
-			    TRACE(("ignoring non-scalable font match by fontconfig\n"));
 			}
 		    }
 #endif
Index: misc.c
--- xterm-340+/misc.c	2018-12-15 21:50:47.000000000 +0000
+++ xterm-341/misc.c	2018-12-18 23:17:17.000000000 +0000
@@ -1,4 +1,4 @@
-/* $XTermId: misc.c,v 1.852 2018/12/15 21:50:47 tom Exp $ */
+/* $XTermId: misc.c,v 1.853 2018/12/18 23:17:17 tom Exp $ */
 
 /*
  * Copyright 1999-2017,2018 by Thomas E. Dickey
@@ -5978,6 +5978,7 @@
 xerror(Display *d, XErrorEvent *ev)
 {
     xtermWarning("warning, error event received:\n");
+    TRACE_X_ERR(d, ev);
     (void) XmuPrintDefaultErrorMessage(d, ev, stderr);
     Exit(ERROR_XERROR);
     return 0;			/* appease the compiler */
Index: package/debian/changelog
--- xterm-340+/package/debian/changelog	2018-12-14 00:11:25.000000000 +0000
+++ xterm-341/package/debian/changelog	2018-12-18 10:25:26.000000000 +0000
@@ -1,3 +1,9 @@
+xterm-dev (341) unstable; urgency=low
+
+  * maintenance updates
+
+ -- Thomas E. Dickey <dickey@invisible-island.net>  Tue, 18 Dec 2018 05:25:26 -0500
+
 xterm-dev (340) unstable; urgency=low
 
   * maintenance updates
Index: package/freebsd/Makefile
--- xterm-340+/package/freebsd/Makefile	2018-12-14 00:11:25.000000000 +0000
+++ xterm-341/package/freebsd/Makefile	2018-12-18 10:25:26.000000000 +0000
@@ -5,7 +5,7 @@
 # and "make makesum".
 
 PORTNAME=	xterm
-PORTVERSION=	340
+PORTVERSION=	341
 CATEGORIES=	x11
 MASTER_SITES=	ftp://ftp.invisible-island.net/xterm/ \
 		CRITICAL
Index: package/xterm.spec
--- xterm-340+/package/xterm.spec	2018-12-14 00:11:25.000000000 +0000
+++ xterm-341/package/xterm.spec	2018-12-18 10:25:26.000000000 +0000
@@ -1,11 +1,11 @@
-# $XTermId: xterm.spec,v 1.110 2018/12/14 00:11:25 tom Exp $
+# $XTermId: xterm.spec,v 1.111 2018/12/18 10:25:26 tom Exp $
 Summary: X terminal emulator (development version)
 %global my_middle xterm
 %global my_suffix -dev
 %global fullname %{my_middle}%{my_suffix}
 %global my_class XTermDev
 Name: %{fullname}
-Version: 340
+Version: 341
 Release: 1
 License: X11
 Group: User Interface/X
Index: ptyx.h
--- xterm-340+/ptyx.h	2018-12-09 14:06:28.000000000 +0000
+++ xterm-341/ptyx.h	2018-12-18 23:08:57.000000000 +0000
@@ -1,4 +1,4 @@
-/* $XTermId: ptyx.h,v 1.943 2018/12/09 14:06:28 tom Exp $ */
+/* $XTermId: ptyx.h,v 1.944 2018/12/18 23:08:57 tom Exp $ */
 
 /*
  * Copyright 1999-2017,2018 by Thomas E. Dickey
@@ -3485,6 +3485,10 @@
 #define TRACE_WM_HINTS(w) /*nothing*/
 #endif
 
+#ifndef TRACE_X_ERR
+#define TRACE_X_ERR(d,e) /*nothing*/
+#endif
+
 #ifndef TRACE_XRES
 #define TRACE_XRES() /*nothing*/
 #endif
Index: screen.c
--- xterm-340+/screen.c	2018-12-15 15:19:08.000000000 +0000
+++ xterm-341/screen.c	2018-12-24 02:04:07.000000000 +0000
@@ -1,4 +1,4 @@
-/* $XTermId: screen.c,v 1.569 2018/12/15 15:19:08 tom Exp $ */
+/* $XTermId: screen.c,v 1.570 2018/12/24 02:04:07 Martin.Hostettler Exp $ */
 
 /*
  * Copyright 1999-2017,2018 by Thomas E. Dickey
@@ -2454,8 +2454,8 @@
 			Clear1Cell(ld, left);
 		    }
 		}
-		if (right < (int) ld->lineSize) {
-		    if (ld->charData[right] == HIDDEN_CHAR) {
+		if (right + 1 < (int) ld->lineSize) {
+		    if (ld->charData[right + 1] == HIDDEN_CHAR) {
 			b_right = 1;
 			Clear1Cell(ld, right);
 			Clear1Cell(ld, right + 1);
@@ -2767,8 +2767,8 @@
 			Clear1Cell(ld, left);
 		    }
 		}
-		if (right < (int) ld->lineSize && !IsProtected(ld, right)) {
-		    if (ld->charData[right] == HIDDEN_CHAR) {
+		if (right + 1 < (int) ld->lineSize && !IsProtected(ld, right)) {
+		    if (ld->charData[right + 1] == HIDDEN_CHAR) {
 			b_right = 1;
 			Clear1Cell(ld, right);
 			Clear1Cell(ld, right + 1);
Index: trace.c
--- xterm-340+/trace.c	2018-12-02 22:47:21.000000000 +0000
+++ xterm-341/trace.c	2018-12-18 23:18:34.000000000 +0000
@@ -1,4 +1,4 @@
-/* $XTermId: trace.c,v 1.196 2018/12/02 22:47:21 tom Exp $ */
+/* $XTermId: trace.c,v 1.197 2018/12/18 23:18:34 tom Exp $ */
 
 /*
  * Copyright 1997-2017,2018 by Thomas E. Dickey
@@ -54,6 +54,7 @@
 
 #include <X11/Xatom.h>
 #include <X11/Xmu/Atoms.h>
+#include <X11/Xmu/Error.h>
 
 #ifdef HAVE_X11_TRANSLATEI_H
 #include <X11/ConvertI.h>
@@ -167,6 +168,14 @@
 }
 
 void
+TraceXError(Display *d, XErrorEvent *ev)
+{
+    FILE *fp = TraceOpen();
+    (void) XmuPrintDefaultErrorMessage(d, ev, fp);
+    (void) fflush(fp);
+}
+
+void
 TraceIds(const char *fname, int lnum)
 {
     Trace("process %d ", (int) getpid());
Index: trace.h
--- xterm-340+/trace.h	2018-11-26 09:41:16.000000000 +0000
+++ xterm-341/trace.h	2018-12-18 23:14:28.000000000 +0000
@@ -1,4 +1,4 @@
-/* $XTermId: trace.h,v 1.85 2018/11/26 09:41:16 tom Exp $ */
+/* $XTermId: trace.h,v 1.86 2018/12/18 23:14:28 tom Exp $ */
 
 /*
  * Copyright 1997-2017,2018 by Thomas E. Dickey
@@ -45,12 +45,14 @@
 
 extern	void	Trace ( const char *, ... ) GCC_PRINTFLIKE(1,2);
 extern	void	TraceVA ( const char *fmt, va_list ap );
+extern	void	TraceXError (Display *d, XErrorEvent *ev);
 
 #undef  TRACE
 #define TRACE(p) Trace p
 
 #undef  TRACE_VA
 #define TRACE_VA(p) TraceVA p
+#define TRACE_X_ERR(d,e) TraceXError(d,e)
 
 extern	void	TraceClose (void);
 
Index: version.h
--- xterm-340+/version.h	2018-12-17 01:41:12.000000000 +0000
+++ xterm-341/version.h	2018-12-25 01:43:46.000000000 +0000
@@ -1,4 +1,4 @@
-/* $XTermId: version.h,v 1.458 2018/12/17 01:41:12 tom Exp $ */
+/* $XTermId: version.h,v 1.460 2018/12/25 01:43:46 tom Exp $ */
 
 /*
  * Copyright 1998-2017,2018 by Thomas E. Dickey
@@ -38,8 +38,8 @@
  * version of X to which this version of xterm has been built.  The resulting
  * number in parentheses is my patch number (Thomas E. Dickey).
  */
-#define XTERM_PATCH   340
-#define XTERM_DATE    2018-12-16
+#define XTERM_PATCH   341
+#define XTERM_DATE    2018-12-24
 
 #ifndef __vendorversion__
 #define __vendorversion__ "XTerm"
Index: vttests/mouse-codes
--- xterm-340+/vttests/mouse-codes	2018-11-16 13:54:43.000000000 +0000
+++ xterm-341/vttests/mouse-codes	2018-12-24 14:17:07.000000000 +0000
@@ -1,5 +1,5 @@
 #!/usr/bin/env perl
-# $XTermId: mouse-codes,v 1.1 2018/11/16 13:54:43 tom Exp $
+# $XTermId: mouse-codes,v 1.4 2018/12/24 14:17:07 tom Exp $
 # -----------------------------------------------------------------------------
 # this file is part of xterm
 #
@@ -37,6 +37,10 @@
 use warnings;
 use diagnostics;
 
+use Getopt::Std;
+
+our ( $opt_b, $opt_i, $opt_x );
+
 # Typically,
 # -	Buttons 1, 2, and 3 are left-to-right assignments on a 3-button mouse.
 # -	Buttons 4 and 5 are (by convention) assigned to a wheel mouse's
@@ -87,6 +91,7 @@
     my $states;
     my $motion;
     my %encoded;
+    my %reports;
     for $states ( 0 .. $maxstates ) {
         for $motion ( 0 .. 1 ) {
             for $button ( -1 .. $maxbutton ) {
@@ -97,7 +102,12 @@
                     $result += 3;
                 }
                 else {
-                    $result += ( 64 - 4 ) if ( $button > 3 );
+                    if ( $button > 7 ) {
+                        $result += ( 128 - 4 );
+                    }
+                    elsif ( $button > 3 ) {
+                        $result += ( 64 - 4 );
+                    }
                     $result += $button;
                 }
                 my $code   = sprintf "%3d",    $result;
@@ -105,16 +115,43 @@
                   &KeyState($states), &Motion($motion);
                 printf "OOPS %s ->%s versus %s\n", $code, $report,
                   $encoded{$code}
-                  if ( defined $encoded{$code} );
-                $encoded{$code} = $report;
+                  if ( defined $encoded{$code}
+                    or ( $result > 255 and not defined $opt_x ) );
+                $encoded{$code}   = $report;
+                $reports{$report} = $result;
             }
         }
     }
-    foreach my $code ( sort keys %encoded ) {
-        printf "%s = %s\n", $code, $encoded{$code};
+    if ($opt_i) {
+        foreach my $report ( sort keys %reports ) {
+            printf "%s = %s\n", $report, $reports{$report};
+        }
+    }
+    else {
+        foreach my $code ( sort keys %encoded ) {
+            printf "%s = %s\n", $code, $encoded{$code};
+        }
     }
 }
 
+sub main::HELP_MESSAGE() {
+    printf STDERR <<EOF
+Usage: $0 [options]
+
+Options:
+	-b NUM	set the number of buttons (default: 7)
+	-i	invert the report to show code for each combination
+	-x	eliminate 1-byte limit for codes
+EOF
+      ;
+    exit 1;
+}
+$Getopt::Std::STANDARD_HELP_VERSION = 1;
+&getopts('b:ix') || &main::HELP_MESSAGE;
+$maxbutton = $opt_b if ( defined $opt_b );
+&main::HELP_MESSAGE if ( $maxbutton !~ /^\d+$/ );
+$#ARGV < 0 || &main::HELP_MESSAGE;
+
 &report;
 
 1;
Index: xterm.log.html
--- xterm-340+/xterm.log.html	2018-12-17 01:41:20.000000000 +0000
+++ xterm-341/xterm.log.html	2018-12-25 01:43:53.000000000 +0000
@@ -30,7 +30,7 @@
  * sale, use or other dealings in this Software without prior written        *
  * authorization.                                                            *
  *****************************************************************************
-  $XTermId: xterm.log.html,v 1.1965 2018/12/17 01:41:20 tom Exp $
+  $XTermId: xterm.log.html,v 1.1971 2018/12/25 01:43:53 tom Exp $
   -->
 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
 
@@ -72,6 +72,8 @@
   CHANGELOG</a>).</p>
 
   <ul>
+    <li><a href="#xterm_341">Patch #341 - 2018/12/24</a></li>
+
     <li><a href="#xterm_340">Patch #340 - 2018/12/16</a></li>
 
     <li><a href="#xterm_339">Patch #339 - 2018/12/12</a></li>
@@ -965,6 +967,35 @@
     <li><a href="#xterm_01">Patch #1 - 1996/1/6</a></li>
   </ul>
 
+  <h1><a name="xterm_341" id="xterm_341">Patch #341 -
+  2018/12/24</a></h1>
+
+  <ul>
+    <li>add options to <code>mouse-codes</code> script to
+    demonstrate that the encoding used for mouse events limits
+    button numbers to 11.</li>
+
+    <li>allow mouse button numbers up to 11 using an offset of 128
+    for button numbers 8-11 rather than the offset of 64 used for
+    wheel-mouse, etc., buttons 4-7 in <a href="#xterm_338">patch
+    #338</a> (adapted from patch by Martin Hostettler).</li>
+
+    <li>correct off-by-one right-margin checks for double-width
+    character adjustments in DECERA/DECFRA (patch by by Martin
+    Hostettler).</li>
+
+    <li>allocated size of selection buffer was not reset when
+    freeing the buffer in <a href="#xterm_338">patch #338</a>
+    (report/analysis by Stefan Assmann).</li>
+
+    <li>modify the initial pattern passed to fontconfig to disallow
+    color bitmap fonts.</li>
+
+    <li>improve error recovery for a case where fontconfig ignores
+    the requested pattern due to override in the user's font
+    configuration (report/testcase by Stefan Assmann).</li>
+  </ul>
+
   <h1><a name="xterm_340" id="xterm_340">Patch #340 -
   2018/12/16</a></h1>
 
