Patch 9.0.0901
Problem:    Setting w_leftcol and handling side effects is confusing.
Solution:   Use a function to set w_leftcol() and handle side effects.
Files:      src/misc2.c, src/proto/misc2.pro, src/normal.c, src/mouse.c


*** ../vim-9.0.0900/src/misc2.c 2022-11-14 19:49:09.662298086 +0000
--- src/misc2.c 2022-11-18 13:34:50.327639658 +0000
***************
*** 673,697 ****
  }
  
  /*
!  * When curwin->w_leftcol has changed, adjust the cursor position.
   * Return TRUE if the cursor was moved.
   */
      int
! leftcol_changed(void)
  {
-     long      lastcol;
-     colnr_T   s, e;
      int               retval = FALSE;
!     long      siso = get_sidescrolloff_value();
  
      changed_cline_bef_curs();
!     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 (curwin->w_virtcol > (colnr_T)(lastcol - siso))
      {
        retval = TRUE;
--- 673,699 ----
  }
  
  /*
!  * Set "curwin->w_leftcol" to "leftcol".
!  * Adjust the cursor position if needed.
   * Return TRUE if the cursor was moved.
   */
      int
! set_leftcol(colnr_T leftcol)
  {
      int               retval = FALSE;
! 
!     // Return quickly when there is no change.
!     if (curwin->w_leftcol == leftcol)
!       return FALSE;
!     curwin->w_leftcol = leftcol;
  
      changed_cline_bef_curs();
!     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
!     // visible character.
!     long siso = get_sidescrolloff_value();
      if (curwin->w_virtcol > (colnr_T)(lastcol - siso))
      {
        retval = TRUE;
***************
*** 703,713 ****
        (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.
!      */
      getvvcol(curwin, &curwin->w_cursor, &s, NULL, &e);
      if (e > (colnr_T)lastcol)
      {
--- 705,714 ----
        (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.
!     colnr_T   s, e;
      getvvcol(curwin, &curwin->w_cursor, &s, NULL, &e);
      if (e > (colnr_T)lastcol)
      {
*** ../vim-9.0.0900/src/proto/misc2.pro 2022-11-14 19:49:09.662298086 +0000
--- src/proto/misc2.pro 2022-11-18 13:31:32.507718467 +0000
***************
*** 19,25 ****
  void check_cursor(void);
  void check_visual_pos(void);
  void adjust_cursor_col(void);
! int leftcol_changed(void);
  int copy_option_part(char_u **option, char_u *buf, int maxlen, char 
*sep_chars);
  int vim_isspace(int x);
  int simplify_key(int key, int *modifiers);
--- 19,25 ----
  void check_cursor(void);
  void check_visual_pos(void);
  void adjust_cursor_col(void);
! int set_leftcol(colnr_T leftcol);
  int copy_option_part(char_u **option, char_u *buf, int maxlen, char 
*sep_chars);
  int vim_isspace(int x);
  int simplify_key(int key, int *modifiers);
*** ../vim-9.0.0900/src/normal.c        2022-11-15 17:43:28.442135533 +0000
--- src/normal.c        2022-11-18 13:31:25.503723015 +0000
***************
*** 1930,1940 ****
        }
  
        // do the horizontal scroll
!       if (want_hor && curwin->w_leftcol != tgt_leftcol)
!       {
!           curwin->w_leftcol = tgt_leftcol;
!           leftcol_changed();
!       }
      }
  
      // reset current-window
--- 1930,1937 ----
        }
  
        // do the horizontal scroll
!       if (want_hor)
!           (void)set_leftcol(tgt_leftcol);
      }
  
      // reset current-window
***************
*** 2458,2464 ****
        scrollup(count, TRUE);
      else
        scrolldown(count, TRUE);
!     if (get_scrolloff_value())
      {
        // Adjust the cursor position for 'scrolloff'.  Mark w_topline as
        // valid, otherwise the screen jumps back at the end of the file.
--- 2455,2461 ----
        scrollup(count, TRUE);
      else
        scrolldown(count, TRUE);
!     if (get_scrolloff_value() > 0)
      {
        // Adjust the cursor position for 'scrolloff'.  Mark w_topline as
        // valid, otherwise the screen jumps back at the end of the file.
***************
*** 2734,2761 ****
      case 'h':
      case K_LEFT:
                if (!curwin->w_p_wrap)
!               {
!                   if ((colnr_T)cap->count1 > curwin->w_leftcol)
!                       curwin->w_leftcol = 0;
!                   else
!                       curwin->w_leftcol -= (colnr_T)cap->count1;
!                   leftcol_changed();
!               }
                break;
  
!               // "zL" - scroll screen left half-page
      case 'L': cap->count1 *= curwin->w_width / 2;
                // FALLTHROUGH
  
!               // "zl" - scroll screen to the left
      case 'l':
      case K_RIGHT:
                if (!curwin->w_p_wrap)
!               {
!                   // scroll the window left
!                   curwin->w_leftcol += (colnr_T)cap->count1;
!                   leftcol_changed();
!               }
                break;
  
                // "zs" - scroll screen, cursor at the start
--- 2731,2749 ----
      case 'h':
      case K_LEFT:
                if (!curwin->w_p_wrap)
!                   (void)set_leftcol((colnr_T)cap->count1 > curwin->w_leftcol
!                              ? 0 : curwin->w_leftcol - (colnr_T)cap->count1);
                break;
  
!               // "zL" - scroll window left half-page
      case 'L': cap->count1 *= curwin->w_width / 2;
                // FALLTHROUGH
  
!               // "zl" - scroll window to the left if not wrapping
      case 'l':
      case K_RIGHT:
                if (!curwin->w_p_wrap)
!                   (void)set_leftcol(curwin->w_leftcol + (colnr_T)cap->count1);
                break;
  
                // "zs" - scroll screen, cursor at the start
*** ../vim-9.0.0900/src/mouse.c 2022-11-15 17:43:28.442135533 +0000
--- src/mouse.c 2022-11-18 13:27:22.095887457 +0000
***************
*** 2035,2042 ****
      if (curwin->w_leftcol == (colnr_T)leftcol)
        return FALSE;  // already there
  
-     curwin->w_leftcol = (colnr_T)leftcol;
- 
      // When the line of the cursor is too short, move the cursor to the
      // longest visible line.
      if (
--- 2035,2040 ----
***************
*** 2050,2056 ****
        curwin->w_cursor.col = 0;
      }
  
!     return leftcol_changed();
  }
  
  /*
--- 2048,2054 ----
        curwin->w_cursor.col = 0;
      }
  
!     return set_leftcol((colnr_T)leftcol);
  }
  
  /*
***************
*** 2098,2104 ****
        send_keys_to_term(curbuf->b_term, cap->cmdchar, mod_mask, FALSE);
      else
  # endif
!     // For insert mode, don't scroll the window in which completion is being
      // done.
      if (mode == MODE_NORMAL || !pum_visible() || curwin != old_curwin)
      {
--- 2096,2102 ----
        send_keys_to_term(curbuf->b_term, cap->cmdchar, mod_mask, FALSE);
      else
  # endif
!     // For Insert mode, don't scroll the window in which completion is being
      // done.
      if (mode == MODE_NORMAL || !pum_visible() || curwin != old_curwin)
      {
*** ../vim-9.0.0900/src/version.c       2022-11-18 12:52:23.418570894 +0000
--- src/version.c       2022-11-18 14:05:13.972695669 +0000
***************
*** 697,698 ****
--- 697,700 ----
  {   /* Add new patch number below this line */
+ /**/
+     901,
  /**/

-- 
>From "know your smileys":
 8<}}   Glasses, big nose, beard

 /// Bram Moolenaar -- [email protected] -- http://www.Moolenaar.net   \\\
///                                                                      \\\
\\\        sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ ///
 \\\            help me help AIDS victims -- http://ICCF-Holland.org    ///

-- 
-- 
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

--- 
You received this message because you are subscribed to the Google Groups 
"vim_dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/vim_dev/20221118140746.33F6E1C0396%40moolenaar.net.

Raspunde prin e-mail lui