diff src/ui.c @ 31103:33ca088dbd3e v9.0.0886

patch 9.0.0886: horizontal mouse scroll only works in the GUI Commit: https://github.com/vim/vim/commit/44c2209352d56d70b1fc0215e81f1822d55aa563 Author: Christopher Plewright <chris@createng.com> Date: Tue Nov 15 17:43:36 2022 +0000 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)
author Bram Moolenaar <Bram@vim.org>
date Tue, 15 Nov 2022 18:45:03 +0100
parents 71137f73c94d
children fa309d9af73c
line wrap: on
line diff
--- a/src/ui.c
+++ b/src/ui.c
@@ -1126,6 +1126,75 @@ check_row(int row)
 }
 
 /*
+ * 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).
  */