Patch 9.0.0841
Problem:    deletebufline() does not always return 1 on failure.
Solution:   Refactor the code to make it work more predictable. (closes #11511)
Files:      src/evalbuffer.c, src/testdir/test_bufline.vim


*** ../vim-9.0.0840/src/evalbuffer.c    2022-11-05 23:46:30.720146269 +0000
--- src/evalbuffer.c    2022-11-06 22:25:28.819970489 +0000
***************
*** 535,540 ****
--- 535,541 ----
                           || first > buf->b_ml.ml_line_count || last < first)
        return;
  
+     // After this don't use "return", goto "cleanup"!
      if (!is_curbuf)
      {
        VIsual_active = FALSE;
***************
*** 556,593 ****
      }
  
      if (u_save(first - 1, last + 1) == FAIL)
!     {
!       rettv->vval.v_number = 1;       // FAIL
!     }
!     else
!     {
!       for (lnum = first; lnum <= last; ++lnum)
!           ml_delete_flags(first, ML_DEL_MESSAGE);
  
!       FOR_ALL_TAB_WINDOWS(tp, wp)
!           if (wp->w_buffer == buf)
!           {
!               if (wp->w_cursor.lnum > last)
!                   wp->w_cursor.lnum -= count;
!               else if (wp->w_cursor.lnum > first)
!                   wp->w_cursor.lnum = first;
!               if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
!                   wp->w_cursor.lnum = wp->w_buffer->b_ml.ml_line_count;
!               wp->w_valid = 0;
!               if (wp->w_cursor.lnum <= wp->w_topline)
!                   wp->w_topline = 1;
!           }
!       check_cursor_col();
!       deleted_lines_mark(first, count);
!     }
  
      if (!is_curbuf)
      {
        curbuf = curbuf_save;
        curwin = curwin_save;
        VIsual_active = save_VIsual_active;
      }
-     rettv->vval.v_number = 0; // OK
  }
  
  /*
--- 557,591 ----
      }
  
      if (u_save(first - 1, last + 1) == FAIL)
!       goto cleanup;
  
!     for (lnum = first; lnum <= last; ++lnum)
!       ml_delete_flags(first, ML_DEL_MESSAGE);
  
+     FOR_ALL_TAB_WINDOWS(tp, wp)
+       if (wp->w_buffer == buf)
+       {
+           if (wp->w_cursor.lnum > last)
+               wp->w_cursor.lnum -= count;
+           else if (wp->w_cursor.lnum > first)
+               wp->w_cursor.lnum = first;
+           if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
+               wp->w_cursor.lnum = wp->w_buffer->b_ml.ml_line_count;
+           wp->w_valid = 0;
+           if (wp->w_cursor.lnum <= wp->w_topline)
+               wp->w_topline = 1;
+       }
+     check_cursor_col();
+     deleted_lines_mark(first, count);
+     rettv->vval.v_number = 0; // OK
+ 
+ cleanup:
      if (!is_curbuf)
      {
        curbuf = curbuf_save;
        curwin = curwin_save;
        VIsual_active = save_VIsual_active;
      }
  }
  
  /*
*** ../vim-9.0.0840/src/testdir/test_bufline.vim        2022-11-05 
23:46:30.720146269 +0000
--- src/testdir/test_bufline.vim        2022-11-06 22:17:25.107880312 +0000
***************
*** 279,282 ****
--- 279,298 ----
    call delete('Xresult')
  endfunc
  
+ " Test that setbufline(), appendbufline() and deletebufline() should fail and
+ " return 1 when "textlock" is active.
+ func Test_change_bufline_with_textlock()
+   new
+   inoremap <buffer> <expr> <F2> setbufline('', 1, '')
+   call assert_fails("normal a\<F2>", 'E565:')
+   call assert_equal('1', getline(1))
+   inoremap <buffer> <expr> <F2> appendbufline('', 1, '')
+   call assert_fails("normal a\<F2>", 'E565:')
+   call assert_equal('11', getline(1))
+   inoremap <buffer> <expr> <F2> deletebufline('', 1)
+   call assert_fails("normal a\<F2>", 'E565:')
+   call assert_equal('111', getline(1))
+   bwipe!
+ endfunc
+ 
  " vim: shiftwidth=2 sts=2 expandtab
*** ../vim-9.0.0840/src/version.c       2022-11-06 18:27:09.363922860 +0000
--- src/version.c       2022-11-06 22:20:24.971907239 +0000
***************
*** 697,698 ****
--- 697,700 ----
  {   /* Add new patch number below this line */
+ /**/
+     841,
  /**/

-- 
How To Keep A Healthy Level Of Insanity:
16. Have your coworkers address you by your wrestling name, Rock Hard Kim.

 /// 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/20221106222744.9EF531C0739%40moolenaar.net.

Raspunde prin e-mail lui