From 5510361a8ca8b47e9c3a46fae9ec0f0b00b507d3 Mon Sep 17 00:00:00 2001 From: Marco Hinz Date: Wed, 3 Apr 2019 22:13:00 +0200 Subject: [PATCH] vim-patch:8.1.0493: argv() and argc() only work on the current argument list Problem: argv() and argc() only work on the current argument list. Solution: Add a window ID argument. (Yegappan Lakshmanan, closes vim/vim#832) https://github.com/vim/vim/commit/e6e3989c1b3f18907a0c305712b867e9a3821369 --- runtime/doc/eval.txt | 31 ++++++++---- src/nvim/eval.c | 84 ++++++++++++++++++++++++------- src/nvim/testdir/test_arglist.vim | 42 +++++++++++++++- 3 files changed, 127 insertions(+), 30 deletions(-) diff --git a/runtime/doc/eval.txt b/runtime/doc/eval.txt index db278b2fb0..f5817279df 100644 --- a/runtime/doc/eval.txt +++ b/runtime/doc/eval.txt @@ -1970,11 +1970,11 @@ and({expr}, {expr}) Number bitwise AND api_info() Dict api metadata append({lnum}, {string}) Number append {string} below line {lnum} append({lnum}, {list}) Number append lines {list} below line {lnum} -argc() Number number of files in the argument list +argc([{winid}]) Number number of files in the argument list argidx() Number current index in the argument list arglistid([{winnr} [, {tabnr}]]) Number argument list id -argv({nr}) String {nr} entry of the argument list -argv() List the argument list +argv({nr} [, {winid}]) String {nr} entry of the argument list +argv([-1, {winid}]) List the argument list assert_equal({exp}, {act} [, {msg}]) none assert {exp} is equal to {act} assert_exception({error} [, {msg}]) @@ -2446,8 +2446,15 @@ append({lnum}, {text}) *append()* :let failed = append(0, ["Chapter 1", "the beginning"]) < *argc()* -argc() The result is the number of files in the argument list of the - current window. See |arglist|. +argc([{winid}]) + The result is the number of files in the argument list. See + |arglist|. + If {winid} is not supplied, the argument list of the current + window is used. + If {winid} is -1, the global argument list is used. + Otherwise {winid} specifies the window of which the argument + list is used: either the window number or the window ID. + Returns -1 if the {winid} argument is invalid. *argidx()* argidx() The result is the current index in the argument list. 0 is @@ -2458,7 +2465,7 @@ arglistid([{winnr} [, {tabnr}]]) Return the argument list ID. This is a number which identifies the argument list being used. Zero is used for the global argument list. See |arglist|. - Return -1 if the arguments are invalid. + Returns -1 if the arguments are invalid. Without arguments use the current window. With {winnr} only use this window in the current tab page. @@ -2467,17 +2474,19 @@ arglistid([{winnr} [, {tabnr}]]) {winnr} can be the window number or the |window-ID|. *argv()* -argv([{nr}]) The result is the {nr}th file in the argument list of the - current window. See |arglist|. "argv(0)" is the first one. - Example: > +argv([{nr} [, {winid}]) + The result is the {nr}th file in the argument list. See + |arglist|. "argv(0)" is the first one. Example: > :let i = 0 :while i < argc() : let f = escape(fnameescape(argv(i)), '.') : exe 'amenu Arg.' . f . ' :e ' . f . '' : let i = i + 1 :endwhile -< Without the {nr} argument a |List| with the whole |arglist| is - returned. +< Without the {nr} argument, or when {nr} is -1, a |List| with + the whole |arglist| is returned. + + The {winid} argument specifies the window ID, see |argc()|. *assert_equal()* assert_equal({expected}, {actual}, [, {msg}]) diff --git a/src/nvim/eval.c b/src/nvim/eval.c index 68a0c86583..833a5073c4 100644 --- a/src/nvim/eval.c +++ b/src/nvim/eval.c @@ -6662,12 +6662,24 @@ static void f_append(typval_T *argvars, typval_T *rettv, FunPtr fptr) rettv->vval.v_number = 1; /* Failed */ } -/* - * "argc()" function - */ static void f_argc(typval_T *argvars, typval_T *rettv, FunPtr fptr) { - rettv->vval.v_number = ARGCOUNT; + if (argvars[0].v_type == VAR_UNKNOWN) { + // use the current window + rettv->vval.v_number = ARGCOUNT; + } else if (argvars[0].v_type == VAR_NUMBER + && tv_get_number(&argvars[0]) == -1) { + // use the global argument list + rettv->vval.v_number = GARGCOUNT; + } else { + // use the argument list of the specified window + win_T *wp = find_win_by_nr_or_id(&argvars[0]); + if (wp != NULL) { + rettv->vval.v_number = WARGCOUNT(wp); + } else { + rettv->vval.v_number = -1; + } + } } /* @@ -6688,28 +6700,54 @@ static void f_arglistid(typval_T *argvars, typval_T *rettv, FunPtr fptr) } } +/// Get the argument list for a given window +static void get_arglist_as_rettv(aentry_T *arglist, int argcount, + typval_T *rettv) +{ + tv_list_alloc_ret(rettv, argcount); + if (arglist != NULL) { + for (int idx = 0; idx < argcount; idx++) { + tv_list_append_string(rettv->vval.v_list, + (const char *)alist_name(&arglist[idx]), -1); + } + } +} + /* * "argv(nr)" function */ static void f_argv(typval_T *argvars, typval_T *rettv, FunPtr fptr) { - int idx; + aentry_T *arglist = NULL; + int argcount = -1; if (argvars[0].v_type != VAR_UNKNOWN) { - idx = (int)tv_get_number_chk(&argvars[0], NULL); - if (idx >= 0 && idx < ARGCOUNT) { - rettv->vval.v_string = (char_u *)xstrdup( - (const char *)alist_name(&ARGLIST[idx])); + if (argvars[1].v_type == VAR_UNKNOWN) { + arglist = ARGLIST; + argcount = ARGCOUNT; + } else if (argvars[1].v_type == VAR_NUMBER + && tv_get_number(&argvars[1]) == -1) { + arglist = GARGLIST; + argcount = GARGCOUNT; } else { - rettv->vval.v_string = NULL; + win_T *wp = find_win_by_nr_or_id(&argvars[1]); + if (wp != NULL) { + // Use the argument list of the specified window + arglist = WARGLIST(wp); + argcount = WARGCOUNT(wp); + } } rettv->v_type = VAR_STRING; - } else { - tv_list_alloc_ret(rettv, ARGCOUNT); - for (idx = 0; idx < ARGCOUNT; idx++) { - tv_list_append_string(rettv->vval.v_list, - (const char *)alist_name(&ARGLIST[idx]), -1); + rettv->vval.v_string = NULL; + int idx = tv_get_number_chk(&argvars[0], NULL); + if (arglist != NULL && idx >= 0 && idx < argcount) { + rettv->vval.v_string = (char_u *)xstrdup( + (const char *)alist_name(&arglist[idx])); + } else if (idx == -1) { + get_arglist_as_rettv(arglist, argcount, rettv); } + } else { + get_arglist_as_rettv(ARGLIST, ARGCOUNT, rettv); } } @@ -8219,6 +8257,19 @@ static void f_exepath(typval_T *argvars, typval_T *rettv, FunPtr fptr) rettv->vval.v_string = path; } +/// Find a window: When using a Window ID in any tab page, when using a number +/// in the current tab page. +win_T * find_win_by_nr_or_id(typval_T *vp) +{ + int nr = (int)tv_get_number_chk(vp, NULL); + + if (nr >= LOWEST_WIN_ID) { + return win_id2wp(vp); + } + + return find_win_by_nr(vp, NULL); +} + /* * "exists()" function */ @@ -11205,7 +11256,6 @@ static void f_index(typval_T *argvars, typval_T *rettv, FunPtr fptr) static int inputsecret_flag = 0; - /* * This function is used by f_input() and f_inputdialog() functions. The third * argument to f_input() specifies the type of completion to use at the @@ -16667,7 +16717,6 @@ static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv, FunPtr fptr) } } - /* * "tabpagenr()" function */ @@ -16748,7 +16797,6 @@ static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv, FunPtr fptr) rettv->vval.v_number = nr; } - /* * "tagfiles()" function */ diff --git a/src/nvim/testdir/test_arglist.vim b/src/nvim/testdir/test_arglist.vim index 368fc9810d..ae975fe137 100644 --- a/src/nvim/testdir/test_arglist.vim +++ b/src/nvim/testdir/test_arglist.vim @@ -240,13 +240,53 @@ func Test_arglistid() call assert_equal(0, arglistid()) endfunc -" Test for argv() +" Tests for argv() and argc() func Test_argv() call Reset_arglist() call assert_equal([], argv()) call assert_equal("", argv(2)) + call assert_equal(0, argc()) argadd a b c d + call assert_equal(4, argc()) call assert_equal('c', argv(2)) + + let w1_id = win_getid() + split + let w2_id = win_getid() + arglocal + args e f g + tabnew + let w3_id = win_getid() + split + let w4_id = win_getid() + argglobal + tabfirst + call assert_equal(4, argc(w1_id)) + call assert_equal('b', argv(1, w1_id)) + call assert_equal(['a', 'b', 'c', 'd'], argv(-1, w1_id)) + + call assert_equal(3, argc(w2_id)) + call assert_equal('f', argv(1, w2_id)) + call assert_equal(['e', 'f', 'g'], argv(-1, w2_id)) + + call assert_equal(3, argc(w3_id)) + call assert_equal('e', argv(0, w3_id)) + call assert_equal(['e', 'f', 'g'], argv(-1, w3_id)) + + call assert_equal(4, argc(w4_id)) + call assert_equal('c', argv(2, w4_id)) + call assert_equal(['a', 'b', 'c', 'd'], argv(-1, w4_id)) + + call assert_equal(4, argc(-1)) + call assert_equal(3, argc()) + call assert_equal('d', argv(3, -1)) + call assert_equal(['a', 'b', 'c', 'd'], argv(-1, -1)) + tabonly | only | enew! + " Negative test cases + call assert_equal(-1, argc(100)) + call assert_equal('', argv(1, 100)) + call assert_equal([], argv(-1, 100)) + call assert_equal('', argv(10, -1)) endfunc " Test for the :argedit command