vim-patch:8.2.4731: the changelist index is not remembered per buffer

Problem:    The changelist index is not remembered per buffer.
Solution:   Keep the changelist index per window and buffer. (closes vim/vim#10135,
            closes vim/vim#2173)
db0ea7f2b0

Cherry-pick FOR_ALL_BUF_WININFO from patch 8.2.0500.
Cherry-pick test_changelist.vim change from patch 8.2.3795.
This commit is contained in:
zeertzjq
2022-07-23 09:15:31 +08:00
parent ed6bbc03af
commit 91c99eed54
5 changed files with 55 additions and 13 deletions

View File

@@ -2483,6 +2483,9 @@ void buflist_setfpos(buf_T *const buf, win_T *const win, linenr_T lnum, colnr_T
wip->wi_mark.view = mark_view_make(win->w_topline, wip->wi_mark.mark); wip->wi_mark.view = mark_view_make(win->w_topline, wip->wi_mark.mark);
} }
} }
if (win != NULL) {
wip->wi_changelistidx = win->w_changelistidx;
}
if (copy_options && win != NULL) { if (copy_options && win != NULL) {
// Save the window-specific option values. // Save the window-specific option values.
copy_winopt(&win->w_onebuf_opt, &wip->wi_opt); copy_winopt(&win->w_onebuf_opt, &wip->wi_opt);
@@ -2586,6 +2589,9 @@ void get_winopts(buf_T *buf)
} else { } else {
copy_winopt(&curwin->w_allbuf_opt, &curwin->w_onebuf_opt); copy_winopt(&curwin->w_allbuf_opt, &curwin->w_onebuf_opt);
} }
if (wip != NULL) {
curwin->w_changelistidx = wip->wi_changelistidx;
}
if (curwin->w_float_config.style == kWinStyleMinimal) { if (curwin->w_float_config.style == kWinStyleMinimal) {
didset_window_options(curwin); didset_window_options(curwin);

View File

@@ -289,6 +289,7 @@ struct wininfo_S {
winopt_T wi_opt; // local window options winopt_T wi_opt; // local window options
bool wi_fold_manual; // copy of w_fold_manual bool wi_fold_manual; // copy of w_fold_manual
garray_T wi_folds; // clone of w_folds garray_T wi_folds; // clone of w_folds
int wi_changelistidx; // copy of w_changelistidx
}; };
/* /*

View File

@@ -2824,13 +2824,23 @@ static void f_getchangelist(typval_T *argvars, typval_T *rettv, FunPtr fptr)
list_T *const l = tv_list_alloc(buf->b_changelistlen); list_T *const l = tv_list_alloc(buf->b_changelistlen);
tv_list_append_list(rettv->vval.v_list, l); tv_list_append_list(rettv->vval.v_list, l);
// The current window change list index tracks only the position in the // The current window change list index tracks only the position for the
// current buffer change list. For other buffers, use the change list // current buffer. For other buffers use the stored index for the current
// length as the current index. // window, or, if that's not available, the change list length.
tv_list_append_number(rettv->vval.v_list, int changelistindex;
(buf == curwin->w_buffer) if (buf == curwin->w_buffer) {
? curwin->w_changelistidx changelistindex = curwin->w_changelistidx;
: buf->b_changelistlen); } else {
wininfo_T *wip;
FOR_ALL_BUF_WININFO(buf, wip) {
if (wip->wi_win == curwin) {
break;
}
}
changelistindex = wip != NULL ? wip->wi_changelistidx : buf->b_changelistlen;
}
tv_list_append_number(rettv->vval.v_list, (varnumber_T)changelistindex);
for (int i = 0; i < buf->b_changelistlen; i++) { for (int i = 0; i < buf->b_changelistlen; i++) {
if (buf->b_changelist[i].mark.lnum == 0) { if (buf->b_changelist[i].mark.lnum == 0) {

View File

@@ -467,6 +467,9 @@ EXTERN buf_T *curbuf INIT(= NULL); // currently active buffer
#define FOR_ALL_BUFFERS_BACKWARDS(buf) \ #define FOR_ALL_BUFFERS_BACKWARDS(buf) \
for (buf_T *buf = lastbuf; buf != NULL; buf = buf->b_prev) for (buf_T *buf = lastbuf; buf != NULL; buf = buf->b_prev)
#define FOR_ALL_BUF_WININFO(buf, wip) \
for ((wip) = (buf)->b_wininfo; (wip) != NULL; (wip) = (wip)->wi_next) // NOLINT
// Iterate through all the signs placed in a buffer // Iterate through all the signs placed in a buffer
#define FOR_ALL_SIGNS_IN_BUF(buf, sign) \ #define FOR_ALL_SIGNS_IN_BUF(buf, sign) \
for ((sign) = (buf)->b_signlist; (sign) != NULL; (sign) = (sign)->se_next) // NOLINT for ((sign) = (buf)->b_signlist; (sign) != NULL; (sign) = (sign)->se_next) // NOLINT

View File

@@ -1,11 +1,29 @@
" Tests for the changelist functionality " Tests for the changelist functionality
" Tests for the getchangelist() function " Tests for the getchangelist() function
func Test_getchangelist() func Test_changelist_index()
if !has("jumplist") edit Xfile1.txt
return exe "normal iabc\<C-G>u\ndef\<C-G>u\nghi"
endif call assert_equal(3, getchangelist('%')[1])
" Move one step back in the changelist.
normal 2g;
hide edit Xfile2.txt
exe "normal iabcd\<C-G>u\ndefg\<C-G>u\nghij"
call assert_equal(3, getchangelist('%')[1])
" Move to the beginning of the changelist.
normal 99g;
" Check the changelist indices.
call assert_equal(0, getchangelist('%')[1])
call assert_equal(1, getchangelist('#')[1])
bwipe!
call delete('Xfile1.txt')
call delete('Xfile2.txt')
endfunc
func Test_getchangelist()
bwipe! bwipe!
enew enew
call assert_equal([], 10->getchangelist()) call assert_equal([], 10->getchangelist())
@@ -15,6 +33,7 @@ func Test_getchangelist()
call writefile(['line1', 'line2', 'line3'], 'Xfile2.txt') call writefile(['line1', 'line2', 'line3'], 'Xfile2.txt')
edit Xfile1.txt edit Xfile1.txt
let buf_1 = bufnr()
exe "normal 1Goline\<C-G>u1.1" exe "normal 1Goline\<C-G>u1.1"
exe "normal 3Goline\<C-G>u2.1" exe "normal 3Goline\<C-G>u2.1"
exe "normal 5Goline\<C-G>u3.1" exe "normal 5Goline\<C-G>u3.1"
@@ -26,6 +45,7 @@ func Test_getchangelist()
\ getchangelist('%')) \ getchangelist('%'))
hide edit Xfile2.txt hide edit Xfile2.txt
let buf_2 = bufnr()
exe "normal 1GOline\<C-G>u1.0" exe "normal 1GOline\<C-G>u1.0"
exe "normal 2Goline\<C-G>u2.0" exe "normal 2Goline\<C-G>u2.0"
call assert_equal([[ call assert_equal([[
@@ -37,10 +57,12 @@ func Test_getchangelist()
call assert_equal([[ call assert_equal([[
\ {'lnum' : 2, 'col' : 4, 'coladd' : 0}, \ {'lnum' : 2, 'col' : 4, 'coladd' : 0},
\ {'lnum' : 4, 'col' : 4, 'coladd' : 0}, \ {'lnum' : 4, 'col' : 4, 'coladd' : 0},
\ {'lnum' : 6, 'col' : 4, 'coladd' : 0}], 3], getchangelist(2)) \ {'lnum' : 6, 'col' : 4, 'coladd' : 0}], 2],
\ getchangelist(buf_1))
call assert_equal([[ call assert_equal([[
\ {'lnum' : 1, 'col' : 6, 'coladd' : 0}, \ {'lnum' : 1, 'col' : 6, 'coladd' : 0},
\ {'lnum' : 3, 'col' : 6, 'coladd' : 0}], 2], getchangelist(3)) \ {'lnum' : 3, 'col' : 6, 'coladd' : 0}], 2],
\ getchangelist(buf_2))
bwipe! bwipe!
call delete('Xfile1.txt') call delete('Xfile1.txt')