Skip to content

Commit 7af3ee2

Browse files
zeertzjqbrammool
authored andcommitted
patch 9.0.0841: deletebufline() does not always return 1 on failure
Problem: deletebufline() does not always return 1 on failure. Solution: Refactor the code to make it work more predictable. (closes #11511)
1 parent adbc08f commit 7af3ee2

File tree

3 files changed

+40
-24
lines changed

3 files changed

+40
-24
lines changed

src/evalbuffer.c

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,7 @@ f_deletebufline(typval_T *argvars, typval_T *rettv)
535535
|| first > buf->b_ml.ml_line_count || last < first)
536536
return;
537537

538+
// After this don't use "return", goto "cleanup"!
538539
if (!is_curbuf)
539540
{
540541
VIsual_active = FALSE;
@@ -556,38 +557,35 @@ f_deletebufline(typval_T *argvars, typval_T *rettv)
556557
}
557558

558559
if (u_save(first - 1, last + 1) == FAIL)
559-
{
560-
rettv->vval.v_number = 1; // FAIL
561-
}
562-
else
563-
{
564-
for (lnum = first; lnum <= last; ++lnum)
565-
ml_delete_flags(first, ML_DEL_MESSAGE);
560+
goto cleanup;
566561

567-
FOR_ALL_TAB_WINDOWS(tp, wp)
568-
if (wp->w_buffer == buf)
569-
{
570-
if (wp->w_cursor.lnum > last)
571-
wp->w_cursor.lnum -= count;
572-
else if (wp->w_cursor.lnum > first)
573-
wp->w_cursor.lnum = first;
574-
if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
575-
wp->w_cursor.lnum = wp->w_buffer->b_ml.ml_line_count;
576-
wp->w_valid = 0;
577-
if (wp->w_cursor.lnum <= wp->w_topline)
578-
wp->w_topline = 1;
579-
}
580-
check_cursor_col();
581-
deleted_lines_mark(first, count);
582-
}
562+
for (lnum = first; lnum <= last; ++lnum)
563+
ml_delete_flags(first, ML_DEL_MESSAGE);
564+
565+
FOR_ALL_TAB_WINDOWS(tp, wp)
566+
if (wp->w_buffer == buf)
567+
{
568+
if (wp->w_cursor.lnum > last)
569+
wp->w_cursor.lnum -= count;
570+
else if (wp->w_cursor.lnum > first)
571+
wp->w_cursor.lnum = first;
572+
if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
573+
wp->w_cursor.lnum = wp->w_buffer->b_ml.ml_line_count;
574+
wp->w_valid = 0;
575+
if (wp->w_cursor.lnum <= wp->w_topline)
576+
wp->w_topline = 1;
577+
}
578+
check_cursor_col();
579+
deleted_lines_mark(first, count);
580+
rettv->vval.v_number = 0; // OK
583581

582+
cleanup:
584583
if (!is_curbuf)
585584
{
586585
curbuf = curbuf_save;
587586
curwin = curwin_save;
588587
VIsual_active = save_VIsual_active;
589588
}
590-
rettv->vval.v_number = 0; // OK
591589
}
592590

593591
/*

src/testdir/test_bufline.vim

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,4 +279,20 @@ func Test_setbufline_startup_nofile()
279279
call delete('Xresult')
280280
endfunc
281281

282+
" Test that setbufline(), appendbufline() and deletebufline() should fail and
283+
" return 1 when "textlock" is active.
284+
func Test_change_bufline_with_textlock()
285+
new
286+
inoremap <buffer> <expr> <F2> setbufline('', 1, '')
287+
call assert_fails("normal a\<F2>", 'E565:')
288+
call assert_equal('1', getline(1))
289+
inoremap <buffer> <expr> <F2> appendbufline('', 1, '')
290+
call assert_fails("normal a\<F2>", 'E565:')
291+
call assert_equal('11', getline(1))
292+
inoremap <buffer> <expr> <F2> deletebufline('', 1)
293+
call assert_fails("normal a\<F2>", 'E565:')
294+
call assert_equal('111', getline(1))
295+
bwipe!
296+
endfunc
297+
282298
" vim: shiftwidth=2 sts=2 expandtab

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,8 @@ static char *(features[]) =
695695

696696
static int included_patches[] =
697697
{ /* Add new patch number below this line */
698+
/**/
699+
841,
698700
/**/
699701
840,
700702
/**/

0 commit comments

Comments
 (0)