updated for version 7.3.632
Problem:    Cannot select beyond 222 columns with the mouse in xterm.
Solution:   Add support for SGR mouse tracking. (Hayaki Saito)
diff --git a/src/feature.h b/src/feature.h
index d13e621..f335104 100644
--- a/src/feature.h
+++ b/src/feature.h
@@ -1056,6 +1056,9 @@
 # ifdef FEAT_BIG
 #  define FEAT_MOUSE_URXVT
 # endif
+# ifdef FEAT_BIG
+#  define FEAT_MOUSE_SGR
+# endif
 # if defined(FEAT_NORMAL) && (defined(MSDOS) || defined(WIN3264))
 #  define DOS_MOUSE
 # endif
@@ -1077,6 +1080,11 @@
 # define FEAT_MOUSE_XTERM
 #endif
 
+/* sgr is a small variation of mouse_xterm, and shares its code */
+#if defined(FEAT_MOUSE_SGR) && !defined(FEAT_MOUSE_XTERM)
+# define FEAT_MOUSE_XTERM
+#endif
+
 /* Define FEAT_MOUSE when any of the above is defined or FEAT_GUI. */
 #if !defined(FEAT_MOUSE_TTY) \
 	&& (defined(FEAT_MOUSE_XTERM) \
@@ -1087,7 +1095,8 @@
 	    || defined(FEAT_MOUSE_JSB) \
 	    || defined(FEAT_MOUSE_PTERM) \
 	    || defined(FEAT_SYSMOUSE) \
-	    || defined(FEAT_MOUSE_URXVT))
+	    || defined(FEAT_MOUSE_URXVT) \
+	    || defined(FEAT_MOUSE_SGR))
 # define FEAT_MOUSE_TTY		/* include non-GUI mouse support */
 #endif
 #if !defined(FEAT_MOUSE) && (defined(FEAT_MOUSE_TTY) || defined(FEAT_GUI))
diff --git a/src/keymap.h b/src/keymap.h
index 4706dfe..986006d 100644
--- a/src/keymap.h
+++ b/src/keymap.h
@@ -110,6 +110,9 @@
 /* Used for the urxvt mouse. */
 #define KS_URXVT_MOUSE		238
 
+/* Used for the sgr mouse. */
+#define KS_SGR_MOUSE		237
+
 /*
  * Filler used after KS_SPECIAL and others
  */
@@ -412,6 +415,7 @@
 #define K_JSBTERM_MOUSE	TERMCAP2KEY(KS_JSBTERM_MOUSE, KE_FILLER)
 #define K_PTERM_MOUSE	TERMCAP2KEY(KS_PTERM_MOUSE, KE_FILLER)
 #define K_URXVT_MOUSE	TERMCAP2KEY(KS_URXVT_MOUSE, KE_FILLER)
+#define K_SGR_MOUSE	TERMCAP2KEY(KS_SGR_MOUSE, KE_FILLER)
 
 #define K_SELECT	TERMCAP2KEY(KS_SELECT, KE_FILLER)
 #define K_TEAROFF	TERMCAP2KEY(KS_TEAROFF, KE_FILLER)
diff --git a/src/misc2.c b/src/misc2.c
index 43fec19..084d451 100644
--- a/src/misc2.c
+++ b/src/misc2.c
@@ -2433,6 +2433,9 @@
 #ifdef FEAT_MOUSE_URXVT
     {K_URXVT_MOUSE,	(char_u *)"UrxvtMouse"},
 #endif
+#ifdef FEAT_MOUSE_SGR
+    {K_SGR_MOUSE,	(char_u *)"SgrMouse"},
+#endif
     {K_LEFTMOUSE,	(char_u *)"LeftMouse"},
     {K_LEFTMOUSE_NM,	(char_u *)"LeftMouseNM"},
     {K_LEFTDRAG,	(char_u *)"LeftDrag"},
diff --git a/src/option.h b/src/option.h
index b0cbe12..3474d8e 100644
--- a/src/option.h
+++ b/src/option.h
@@ -822,7 +822,7 @@
 EXTERN char_u	*p_ttym;	/* 'ttymouse' */
 EXTERN unsigned ttym_flags;
 # ifdef IN_OPTION_C
-static char *(p_ttym_values[]) = {"xterm", "xterm2", "dec", "netterm", "jsbterm", "pterm", "urxvt", NULL};
+static char *(p_ttym_values[]) = {"xterm", "xterm2", "dec", "netterm", "jsbterm", "pterm", "urxvt", "sgr", NULL};
 # endif
 # define TTYM_XTERM		0x01
 # define TTYM_XTERM2		0x02
@@ -831,6 +831,7 @@
 # define TTYM_JSBTERM		0x10
 # define TTYM_PTERM		0x20
 # define TTYM_URXVT		0x40
+# define TTYM_SGR		0x80
 #endif
 EXTERN char_u	*p_udir;	/* 'undodir' */
 EXTERN long	p_ul;		/* 'undolevels' */
diff --git a/src/os_unix.c b/src/os_unix.c
index 860fbcc..98ff1ea 100644
--- a/src/os_unix.c
+++ b/src/os_unix.c
@@ -2159,10 +2159,13 @@
  * Return 1 for "xterm".
  * Return 2 for "xterm2".
  * Return 3 for "urxvt".
+ * Return 4 for "sgr".
  */
     int
 use_xterm_mouse()
 {
+    if (ttym_flags == TTYM_SGR)
+	return 4;
     if (ttym_flags == TTYM_URXVT)
 	return 3;
     if (ttym_flags == TTYM_XTERM2)
@@ -3339,7 +3342,8 @@
     xterm_mouse_vers = use_xterm_mouse();
 
 # ifdef FEAT_MOUSE_URXVT
-    if (ttym_flags == TTYM_URXVT) {
+    if (ttym_flags == TTYM_URXVT)
+    {
 	out_str_nf((char_u *)
 		   (on
 		   ? IF_EB("\033[?1015h", ESC_STR "[?1015h")
@@ -3348,6 +3352,17 @@
     }
 # endif
 
+# ifdef FEAT_MOUSE_SGR
+    if (ttym_flags == TTYM_SGR)
+    {
+	out_str_nf((char_u *)
+		   (on
+		   ? IF_EB("\033[?1006h", ESC_STR "[?1006h")
+		   : IF_EB("\033[?1006l", ESC_STR "[?1006l")));
+	ison = on;
+    }
+# endif
+
     if (xterm_mouse_vers > 0)
     {
 	if (on)	/* enable mouse events, use mouse tracking if available */
@@ -3577,6 +3592,27 @@
     else
 	del_mouse_termcode(KS_URXVT_MOUSE);
 # endif
+# ifdef FEAT_MOUSE_SGR
+    /* same as the dec mouse */
+    if (use_xterm_mouse() == 4
+#  ifdef FEAT_GUI
+	    && !gui.in_use
+#  endif
+	    )
+    {
+	set_mouse_termcode(KS_SGR_MOUSE, (char_u *)(term_is_8bit(T_NAME)
+		    ? IF_EB("\233<", CSI_STR "<")
+		    : IF_EB("\033[<", ESC_STR "[<")));
+
+	if (*p_mouse != NUL)
+	{
+	    mch_setmouse(FALSE);
+	    setmouse();
+	}
+    }
+    else
+	del_mouse_termcode(KS_SGR_MOUSE);
+# endif
 }
 #endif
 
diff --git a/src/term.c b/src/term.c
index 74c5a33..0a296fc 100644
--- a/src/term.c
+++ b/src/term.c
@@ -1997,6 +1997,7 @@
 #  define HMT_JSBTERM	8
 #  define HMT_PTERM	16
 #  define HMT_URXVT	32
+#  define HMT_SGR	64
 static int has_mouse_termcode = 0;
 # endif
 
@@ -2037,6 +2038,11 @@
 	has_mouse_termcode |= HMT_URXVT;
     else
 #   endif
+#   ifdef FEAT_MOUSE_SGR
+    if (n == KS_SGR_MOUSE)
+	has_mouse_termcode |= HMT_SGR;
+    else
+#   endif
 	has_mouse_termcode |= HMT_NORMAL;
 #  endif
 }
@@ -2079,6 +2085,11 @@
 	has_mouse_termcode &= ~HMT_URXVT;
     else
 #   endif
+#   ifdef FEAT_MOUSE_SGR
+    if (n == KS_SGR_MOUSE)
+	has_mouse_termcode &= ~HMT_SGR;
+    else
+#   endif
 	has_mouse_termcode &= ~HMT_NORMAL;
 #  endif
 }
@@ -4023,7 +4034,8 @@
 #ifdef FEAT_TERMRESPONSE
 	if (key_name[0] == NUL
 	    /* URXVT mouse uses <ESC>[#;#;#M, but we are matching <ESC>[ */
-	    || key_name[0] == KS_URXVT_MOUSE)
+	    || key_name[0] == KS_URXVT_MOUSE
+	    || key_name[0] == KS_SGR_MOUSE)
 	{
 	    /* Check for xterm version string: "<Esc>[>{x};{vers};{y}c".  Also
 	     * eat other possible responses to t_RV, rxvt returns
@@ -4061,6 +4073,16 @@
 
 		    if (tp[1 + (tp[0] != CSI)] == '>' && j == 2)
 		    {
+# ifdef TTYM_SGR
+			if (extra >= 277
+# ifdef TTYM_URXVT
+				&& ttym_flags != TTYM_URXVT
+# endif
+				)
+			    set_option_value((char_u *)"ttym", 0L,
+							  (char_u *)"sgr", 0);
+                        else
+# endif
 			/* if xterm version >= 95 use mouse dragging */
 			if (extra >= 95
 # ifdef TTYM_URXVT
@@ -4147,21 +4169,24 @@
 	/*
 	 * If it is a mouse click, get the coordinates.
 	 */
-	if (key_name[0] == (int)KS_MOUSE
+	if (key_name[0] == KS_MOUSE
 # ifdef FEAT_MOUSE_JSB
-		|| key_name[0] == (int)KS_JSBTERM_MOUSE
+		|| key_name[0] == KS_JSBTERM_MOUSE
 # endif
 # ifdef FEAT_MOUSE_NET
-		|| key_name[0] == (int)KS_NETTERM_MOUSE
+		|| key_name[0] == KS_NETTERM_MOUSE
 # endif
 # ifdef FEAT_MOUSE_DEC
-		|| key_name[0] == (int)KS_DEC_MOUSE
+		|| key_name[0] == KS_DEC_MOUSE
 # endif
 # ifdef FEAT_MOUSE_PTERM
-		|| key_name[0] == (int)KS_PTERM_MOUSE
+		|| key_name[0] == KS_PTERM_MOUSE
 # endif
 # ifdef FEAT_MOUSE_URXVT
-		|| key_name[0] == (int)KS_URXVT_MOUSE
+		|| key_name[0] == KS_URXVT_MOUSE
+# endif
+# ifdef FEAT_MOUSE_SGR
+		|| key_name[0] == KS_SGR_MOUSE
 # endif
 		)
 	{
@@ -4243,8 +4268,9 @@
 		}
 	    }
 
-# ifdef FEAT_MOUSE_URXVT
-	    if (key_name[0] == (int)KS_URXVT_MOUSE)
+# if defined(FEAT_MOUSE_URXVT) || defined(FEAT_MOUSE_SGR)
+	    if (key_name[0] == KS_URXVT_MOUSE
+		|| key_name[0] == KS_SGR_MOUSE)
 	    {
 		for (;;)
 		{
@@ -4256,6 +4282,20 @@
 		     *		  ^-- row
 		     *	       ^----- column
 		     *	    ^-------- code
+		     *
+		     * SGR 1006 mouse reporting mode:
+		     * Almost identical to xterm mouse mode, except the values
+		     * are decimal instead of bytes.
+		     *
+		     * \033[<%d;%d;%dM
+		     *		   ^-- row
+		     *	        ^----- column
+		     *	     ^-------- code
+		     *
+		     * \033[<%d;%d;%dm        : mouse release event
+		     *		   ^-- row
+		     *	        ^----- column
+		     *	     ^-------- code
 		     */
 		    p = tp + slen;
 
@@ -4263,32 +4303,46 @@
 		    if (*p++ != ';')
 			return -1;
 
+		    /* when mouse reporting is SGR, add 32 to mouse code */
+                    if (key_name[0] == KS_SGR_MOUSE)
+                        mouse_code += 32;
+
 		    mouse_col = getdigits(&p) - 1;
 		    if (*p++ != ';')
 			return -1;
 
 		    mouse_row = getdigits(&p) - 1;
-		    if (*p++ != 'M')
+                    if (key_name[0] == KS_SGR_MOUSE && *p == 'm')
+			mouse_code |= MOUSE_RELEASE;
+                    else if (*p != 'M')
 			return -1;
+                    p++;
 
 		    slen += (int)(p - (tp + slen));
 
 		    /* skip this one if next one has same code (like xterm
 		     * case) */
 		    j = termcodes[idx].len;
-		    if (STRNCMP(tp, tp + slen, (size_t)j) == 0) {
-			/* check if the command is complete by looking for the
-			 * M */
+		    if (STRNCMP(tp, tp + slen, (size_t)j) == 0)
+		    {
 			int slen2;
 			int cmd_complete = 0;
-			for (slen2 = slen; slen2 < len; slen2++) {
-			    if (tp[slen2] == 'M') {
+
+			/* check if the command is complete by looking for the
+			 * 'M' */
+			for (slen2 = slen; slen2 < len; slen2++)
+			{
+			    if (tp[slen2] == 'M'
+                                || (key_name[0] == KS_SGR_MOUSE
+							 && tp[slen2] == 'm'))
+			    {
 				cmd_complete = 1;
 				break;
 			    }
 			}
 			p += j;
-			if (cmd_complete && getdigits(&p) == mouse_code) {
+			if (cmd_complete && getdigits(&p) == mouse_code)
+			{
 			    slen += j; /* skip the \033[ */
 			    continue;
 			}
@@ -4302,6 +4356,9 @@
 #ifdef FEAT_MOUSE_URXVT
 	    || key_name[0] == (int)KS_URXVT_MOUSE
 #endif
+#ifdef FEAT_MOUSE_SGR
+	    || key_name[0] == KS_SGR_MOUSE
+#endif
 	    )
 	{
 #  if !defined(MSWIN) && !defined(MSDOS)
diff --git a/src/version.c b/src/version.c
index 5e6dedc..a9f80b1 100644
--- a/src/version.c
+++ b/src/version.c
@@ -384,6 +384,11 @@
 # else
 	"-mouse_urxvt",
 # endif
+# ifdef FEAT_MOUSE_SGR
+	"+mouse_sgr",
+# else
+	"-mouse_sgr",
+# endif
 #endif
 #ifdef __QNX__
 # ifdef FEAT_MOUSE_PTERM
@@ -715,6 +720,8 @@
 static int included_patches[] =
 {   /* Add new patch number below this line */
 /**/
+    632,
+/**/
     631,
 /**/
     630,