diff src/misc2.c @ 31133:cc0c4141fb73 v9.0.0901

patch 9.0.0901: setting w_leftcol and handling side effects is confusing Commit: https://github.com/vim/vim/commit/0c34d562647f029faca40f7733ccfb7b5377672b Author: Bram Moolenaar <Bram@vim.org> Date: Fri Nov 18 14:07:20 2022 +0000 patch 9.0.0901: setting w_leftcol and handling side effects is confusing Problem: Setting w_leftcol and handling side effects is confusing. Solution: Use a function to set w_leftcol() and handle side effects.
author Bram Moolenaar <Bram@vim.org>
date Fri, 18 Nov 2022 15:15:04 +0100
parents 004aee2845d2
children 0ecb16d5f86f
line wrap: on
line diff
--- a/src/misc2.c
+++ b/src/misc2.c
@@ -673,25 +673,27 @@ adjust_cursor_col(void)
 }
 
 /*
- * When curwin->w_leftcol has changed, adjust the cursor position.
+ * Set "curwin->w_leftcol" to "leftcol".
+ * Adjust the cursor position if needed.
  * Return TRUE if the cursor was moved.
  */
     int
-leftcol_changed(void)
+set_leftcol(colnr_T leftcol)
 {
-    long	lastcol;
-    colnr_T	s, e;
     int		retval = FALSE;
-    long	siso = get_sidescrolloff_value();
+
+    // Return quickly when there is no change.
+    if (curwin->w_leftcol == leftcol)
+	return FALSE;
+    curwin->w_leftcol = leftcol;
 
     changed_cline_bef_curs();
-    lastcol = curwin->w_leftcol + curwin->w_width - curwin_col_off() - 1;
+    long lastcol = curwin->w_leftcol + curwin->w_width - curwin_col_off() - 1;
     validate_virtcol();
 
-    /*
-     * If the cursor is right or left of the screen, move it to last or first
-     * character.
-     */
+    // If the cursor is right or left of the screen, move it to last or first
+    // visible character.
+    long siso = get_sidescrolloff_value();
     if (curwin->w_virtcol > (colnr_T)(lastcol - siso))
     {
 	retval = TRUE;
@@ -703,11 +705,10 @@ leftcol_changed(void)
 	(void)coladvance((colnr_T)(curwin->w_leftcol + siso));
     }
 
-    /*
-     * If the start of the character under the cursor is not on the screen,
-     * advance the cursor one more char.  If this fails (last char of the
-     * line) adjust the scrolling.
-     */
+    // If the start of the character under the cursor is not on the screen,
+    // advance the cursor one more char.  If this fails (last char of the
+    // line) adjust the scrolling.
+    colnr_T	s, e;
     getvvcol(curwin, &curwin->w_cursor, &s, NULL, &e);
     if (e > (colnr_T)lastcol)
     {