patch 9.0.0886: horizontal mouse scroll only works in the GUI
Problem: Horizontal mouse scroll only works in the GUI.
Solution: Make horizontal mouse scroll also work in a terminal.
(Christopher Plewright, closes #11448)
diff --git a/src/ui.c b/src/ui.c
index b56e395..9fc88e5 100644
--- a/src/ui.c
+++ b/src/ui.c
@@ -1126,6 +1126,75 @@
}
/*
+ * Return length of line "lnum" in screen cells for horizontal scrolling.
+ */
+ long
+scroll_line_len(linenr_T lnum)
+{
+ char_u *p = ml_get(lnum);
+ colnr_T col = 0;
+
+ if (*p != NUL)
+ for (;;)
+ {
+ int w = chartabsize(p, col);
+ MB_PTR_ADV(p);
+ if (*p == NUL) // don't count the last character
+ break;
+ col += w;
+ }
+ return col;
+}
+
+/*
+ * Find the longest visible line number. This is used for horizontal
+ * scrolling. If this is not possible (or not desired, by setting 'h' in
+ * "guioptions") then the current line number is returned.
+ */
+ linenr_T
+ui_find_longest_lnum(void)
+{
+ linenr_T ret = 0;
+
+ // Calculate maximum for horizontal scrollbar. Check for reasonable
+ // line numbers, topline and botline can be invalid when displaying is
+ // postponed.
+ if (
+# ifdef FEAT_GUI
+ (!gui.in_use || vim_strchr(p_go, GO_HORSCROLL) == NULL) &&
+# endif
+ curwin->w_topline <= curwin->w_cursor.lnum
+ && curwin->w_botline > curwin->w_cursor.lnum
+ && curwin->w_botline <= curbuf->b_ml.ml_line_count + 1)
+ {
+ linenr_T lnum;
+ long n;
+ long max = 0;
+
+ // Use maximum of all visible lines. Remember the lnum of the
+ // longest line, closest to the cursor line. Used when scrolling
+ // below.
+ for (lnum = curwin->w_topline; lnum < curwin->w_botline; ++lnum)
+ {
+ n = scroll_line_len(lnum);
+ if (n > max)
+ {
+ max = n;
+ ret = lnum;
+ }
+ else if (n == max && abs((int)(lnum - curwin->w_cursor.lnum))
+ < abs((int)(ret - curwin->w_cursor.lnum)))
+ ret = lnum;
+ }
+ }
+ else
+ // Use cursor line only.
+ ret = curwin->w_cursor.lnum;
+
+ return ret;
+}
+
+/*
* Called when focus changed. Used for the GUI or for systems where this can
* be done in the console (Win32).
*/