Skip to content

Commit 07ad816

Browse files
committed
patch 8.0.1514: getting the list of changes is not easy
Problem: Getting the list of changes is not easy. Solution: Add the getchangelist() function. (Yegappan Lakshmanan, closes #2634)
1 parent 4867974 commit 07ad816

7 files changed

Lines changed: 124 additions & 0 deletions

File tree

runtime/doc/eval.txt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2152,6 +2152,7 @@ getbufline({expr}, {lnum} [, {end}])
21522152
List lines {lnum} to {end} of buffer {expr}
21532153
getbufvar({expr}, {varname} [, {def}])
21542154
any variable {varname} in buffer {expr}
2155+
getchangelist({expr}) List list of change list items
21552156
getchar([expr]) Number get one character from the user
21562157
getcharmod() Number modifiers for the last typed character
21572158
getcharsearch() Dict last character search
@@ -4278,6 +4279,22 @@ getbufvar({expr}, {varname} [, {def}]) *getbufvar()*
42784279
:let bufmodified = getbufvar(1, "&mod")
42794280
:echo "todo myvar = " . getbufvar("todo", "myvar")
42804281
<
4282+
getchangelist({expr}) *getchangelist()*
4283+
Returns the |changelist| for the buffer {expr}. For the use
4284+
of {expr}, see |bufname()| above. If buffer {expr} doesn't
4285+
exist, an empty list is returned.
4286+
4287+
The returned list contains two entries: a list with the change
4288+
locations and the current position in the list. Each
4289+
entry in the change list is a dictionary with the following
4290+
entries:
4291+
col column number
4292+
coladd column offset for 'virtualedit'
4293+
lnum line number
4294+
If buffer {expr} is the current buffer, then the current
4295+
position refers to the position in the list. For other
4296+
buffers, it is set to the length of the list.
4297+
42814298
getchar([expr]) *getchar()*
42824299
Get a single character from the user or input stream.
42834300
If [expr] is omitted, wait until a character is available.

runtime/doc/usr_41.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -807,6 +807,7 @@ Buffers, windows and the argument list:
807807
getbufinfo() get a list with buffer information
808808
gettabinfo() get a list with tab page information
809809
getwininfo() get a list with window information
810+
getchangelist() get a list of change list entries
810811
getjumplist() get a list of jump list entries
811812

812813
Command line: *command-line-functions*

src/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2127,6 +2127,7 @@ test_arglist \
21272127
test_cd \
21282128
test_cdo \
21292129
test_changedtick \
2130+
test_changelist \
21302131
test_channel \
21312132
test_charsearch \
21322133
test_charsearch_utf8 \

src/evalfunc.c

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ static void f_get(typval_T *argvars, typval_T *rettv);
165165
static void f_getbufinfo(typval_T *argvars, typval_T *rettv);
166166
static void f_getbufline(typval_T *argvars, typval_T *rettv);
167167
static void f_getbufvar(typval_T *argvars, typval_T *rettv);
168+
static void f_getchangelist(typval_T *argvars, typval_T *rettv);
168169
static void f_getchar(typval_T *argvars, typval_T *rettv);
169170
static void f_getcharmod(typval_T *argvars, typval_T *rettv);
170171
static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
@@ -607,6 +608,7 @@ static struct fst
607608
{"getbufinfo", 0, 1, f_getbufinfo},
608609
{"getbufline", 2, 3, f_getbufline},
609610
{"getbufvar", 2, 3, f_getbufvar},
611+
{"getchangelist", 1, 1, f_getchangelist},
610612
{"getchar", 0, 1, f_getchar},
611613
{"getcharmod", 0, 0, f_getcharmod},
612614
{"getcharsearch", 0, 0, f_getcharsearch},
@@ -4346,6 +4348,58 @@ f_getbufvar(typval_T *argvars, typval_T *rettv)
43464348
--emsg_off;
43474349
}
43484350

4351+
/*
4352+
* "getchangelist()" function
4353+
*/
4354+
static void
4355+
f_getchangelist(typval_T *argvars, typval_T *rettv)
4356+
{
4357+
#ifdef FEAT_JUMPLIST
4358+
buf_T *buf;
4359+
int i;
4360+
list_T *l;
4361+
dict_T *d;
4362+
#endif
4363+
4364+
if (rettv_list_alloc(rettv) != OK)
4365+
return;
4366+
4367+
#ifdef FEAT_JUMPLIST
4368+
buf = find_buffer(&argvars[0]);
4369+
if (buf == NULL)
4370+
return;
4371+
4372+
l = list_alloc();
4373+
if (l == NULL)
4374+
return;
4375+
4376+
if (list_append_list(rettv->vval.v_list, l) == FAIL)
4377+
return;
4378+
/*
4379+
* The current window change list index tracks only the position in the
4380+
* current buffer change list. For other buffers, use the change list
4381+
* length as the current index.
4382+
*/
4383+
list_append_number(rettv->vval.v_list,
4384+
(varnumber_T)((buf == curwin->w_buffer)
4385+
? curwin->w_changelistidx : buf->b_changelistlen));
4386+
4387+
for (i = 0; i < buf->b_changelistlen; ++i)
4388+
{
4389+
if (buf->b_changelist[i].lnum == 0)
4390+
continue;
4391+
if ((d = dict_alloc()) == NULL)
4392+
return;
4393+
if (list_append_dict(l, d) == FAIL)
4394+
return;
4395+
dict_add_nr_str(d, "lnum", (long)buf->b_changelist[i].lnum, NULL);
4396+
dict_add_nr_str(d, "col", (long)buf->b_changelist[i].col, NULL);
4397+
# ifdef FEAT_VIRTUALEDIT
4398+
dict_add_nr_str(d, "coladd", (long)buf->b_changelist[i].coladd, NULL);
4399+
# endif
4400+
}
4401+
#endif
4402+
}
43494403
/*
43504404
* "getchar()" function
43514405
*/

src/testdir/Make_all.mak

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ NEW_TESTS = test_arabic.res \
7575
test_breakindent.res \
7676
test_bufwintabinfo.res \
7777
test_cdo.res \
78+
test_changelist.res \
7879
test_channel.res \
7980
test_charsearch.res \
8081
test_cindent.res \

src/testdir/test_changelist.vim

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
" Tests for the changelist functionality
2+
3+
" Tests for the getchangelist() function
4+
func Test_getchangelist()
5+
if !has("jumplist")
6+
return
7+
endif
8+
9+
bwipe!
10+
enew
11+
call assert_equal([], getchangelist(10))
12+
call assert_equal([[], 0], getchangelist(bufnr('%')))
13+
14+
call writefile(['line1', 'line2', 'line3'], 'Xfile1.txt')
15+
call writefile(['line1', 'line2', 'line3'], 'Xfile2.txt')
16+
17+
edit Xfile1.txt
18+
exe "normal 1Goline\<C-G>u1.1"
19+
exe "normal 3Goline\<C-G>u2.1"
20+
exe "normal 5Goline\<C-G>u3.1"
21+
normal g;
22+
call assert_equal([[
23+
\ {'lnum' : 2, 'col' : 4, 'coladd' : 0},
24+
\ {'lnum' : 4, 'col' : 4, 'coladd' : 0},
25+
\ {'lnum' : 6, 'col' : 4, 'coladd' : 0}], 2],
26+
\ getchangelist(bufnr('%')))
27+
28+
hide edit Xfile2.txt
29+
exe "normal 1GOline\<C-G>u1.0"
30+
exe "normal 2Goline\<C-G>u2.0"
31+
call assert_equal([[
32+
\ {'lnum' : 1, 'col' : 6, 'coladd' : 0},
33+
\ {'lnum' : 3, 'col' : 6, 'coladd' : 0}], 2],
34+
\ getchangelist(bufnr('%')))
35+
hide enew
36+
37+
call assert_equal([[
38+
\ {'lnum' : 2, 'col' : 4, 'coladd' : 0},
39+
\ {'lnum' : 4, 'col' : 4, 'coladd' : 0},
40+
\ {'lnum' : 6, 'col' : 4, 'coladd' : 0}], 3], getchangelist(2))
41+
call assert_equal([[
42+
\ {'lnum' : 1, 'col' : 6, 'coladd' : 0},
43+
\ {'lnum' : 3, 'col' : 6, 'coladd' : 0}], 2], getchangelist(3))
44+
45+
bwipe!
46+
call delete('Xfile1.txt')
47+
call delete('Xfile2.txt')
48+
endfunc

src/version.c

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

772772
static int included_patches[] =
773773
{ /* Add new patch number below this line */
774+
/**/
775+
1514,
774776
/**/
775777
1513,
776778
/**/

0 commit comments

Comments
 (0)