]> granicus.if.org Git - vim/commitdiff
patch 8.1.0493: argv() and argc() only work on the current argument list v8.1.0493
authorBram Moolenaar <Bram@vim.org>
Thu, 25 Oct 2018 10:32:11 +0000 (12:32 +0200)
committerBram Moolenaar <Bram@vim.org>
Thu, 25 Oct 2018 10:32:11 +0000 (12:32 +0200)
Problem:    argv() and argc() only work on the current argument list.
Solution:   Add a window ID argument. (Yegappan Lakshmanan, closes #832)

runtime/doc/eval.txt
src/eval.c
src/evalfunc.c
src/proto/eval.pro
src/testdir/test_arglist.vim
src/version.c

index d51da60f84ffe25494c259e6685f66d6adf05a11..2e58c854e762d3fe0ca3bf96e23318fc49a38f9c 100644 (file)
@@ -2026,11 +2026,11 @@ append({lnum}, {text})          Number  append {text} below line {lnum}
 appendbufline({expr}, {lnum}, {text})
                                Number  append {text} below line {lnum}
                                        in buffer {expr}
-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_beeps({cmd})            Number  assert {cmd} causes a beep
 assert_equal({exp}, {act} [, {msg}])
                                Number  assert {exp} is equal to {act}
@@ -2596,8 +2596,15 @@ appendbufline({expr}, {lnum}, {text})                    *appendbufline()*
                        :let failed = appendbufline(13, 0, "# THE START")
 <
                                                        *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
@@ -2608,7 +2615,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.
@@ -2617,17 +2624,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 . '<CR>'
        :  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_beeps({cmd})                                    *assert_beeps()*
                Run {cmd} and add an error message to |v:errors| if it does
index 7c462fb6a27cd0205c7574ec5d5b6ee65a2d7431..e70bfd8ab31511321cf34bbeafc8e70dccb63507 100644 (file)
@@ -8193,9 +8193,7 @@ find_win_by_nr(
     tabpage_T  *tp)    /* NULL for current tab page */
 {
     win_T      *wp;
-    int                nr;
-
-    nr = (int)get_tv_number_chk(vp, NULL);
+    int                nr = (int)get_tv_number_chk(vp, NULL);
 
     if (nr < 0)
        return NULL;
@@ -8217,6 +8215,20 @@ find_win_by_nr(
     return wp;
 }
 
+/*
+ * 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)get_tv_number_chk(vp, NULL);
+
+    if (nr >= LOWEST_WIN_ID)
+       return win_id2wp(vp);
+    return find_win_by_nr(vp, NULL);
+}
+
 /*
  * Find window specified by "wvp" in tabpage "tvp".
  */
index 0d799964ff0d28ebe0ef7575b931a273a0e1a5b8..b69752416f33863f4ddaa62edcf8795b4552985f 100644 (file)
@@ -501,10 +501,10 @@ static struct fst
     {"and",            2, 2, f_and},
     {"append",         2, 2, f_append},
     {"appendbufline",  3, 3, f_appendbufline},
-    {"argc",           0, 0, f_argc},
+    {"argc",           0, 1, f_argc},
     {"argidx",         0, 0, f_argidx},
     {"arglistid",      0, 2, f_arglistid},
-    {"argv",           0, 1, f_argv},
+    {"argv",           0, 2, f_argv},
 #ifdef FEAT_FLOAT
     {"asin",           1, 1, f_asin},  /* WJMc */
 #endif
@@ -1407,12 +1407,29 @@ f_appendbufline(typval_T *argvars, typval_T *rettv)
 }
 
 /*
- * "argc()" function
+ * "argc([window id])" function
  */
     static void
-f_argc(typval_T *argvars UNUSED, typval_T *rettv)
+f_argc(typval_T *argvars, typval_T *rettv)
 {
-    rettv->vval.v_number = ARGCOUNT;
+    win_T      *wp;
+
+    if (argvars[0].v_type == VAR_UNKNOWN)
+       // use the current window
+       rettv->vval.v_number = ARGCOUNT;
+    else if (argvars[0].v_type == VAR_NUMBER
+                                          && get_tv_number(&argvars[0]) == -1)
+       // use the global argument list
+       rettv->vval.v_number = GARGCOUNT;
+    else
+    {
+       // use the argument list of the specified window
+       wp = find_win_by_nr_or_id(&argvars[0]);
+       if (wp != NULL)
+           rettv->vval.v_number = WARGCOUNT(wp);
+       else
+           rettv->vval.v_number = -1;
+    }
 }
 
 /*
@@ -1438,6 +1455,20 @@ f_arglistid(typval_T *argvars, typval_T *rettv)
        rettv->vval.v_number = wp->w_alist->id;
 }
 
+/*
+ * Get the argument list for a given window
+ */
+    static void
+get_arglist_as_rettv(aentry_T *arglist, int argcount, typval_T *rettv)
+{
+    int                idx;
+
+    if (rettv_list_alloc(rettv) == OK && arglist != NULL)
+       for (idx = 0; idx < argcount; ++idx)
+           list_append_string(rettv->vval.v_list,
+                                               alist_name(&arglist[idx]), -1);
+}
+
 /*
  * "argv(nr)" function
  */
@@ -1445,20 +1476,44 @@ f_arglistid(typval_T *argvars, typval_T *rettv)
 f_argv(typval_T *argvars, typval_T *rettv)
 {
     int                idx;
+    aentry_T   *arglist = NULL;
+    int                argcount = -1;
 
     if (argvars[0].v_type != VAR_UNKNOWN)
     {
-       idx = (int)get_tv_number_chk(&argvars[0], NULL);
-       if (idx >= 0 && idx < ARGCOUNT)
-           rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
+       if (argvars[1].v_type == VAR_UNKNOWN)
+       {
+           arglist = ARGLIST;
+           argcount = ARGCOUNT;
+       }
+       else if (argvars[1].v_type == VAR_NUMBER
+                                          && get_tv_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;
+       rettv->vval.v_string = NULL;
+       idx = get_tv_number_chk(&argvars[0], NULL);
+       if (arglist != NULL && idx >= 0 && idx < argcount)
+           rettv->vval.v_string = vim_strsave(alist_name(&arglist[idx]));
+       else if (idx == -1)
+           get_arglist_as_rettv(arglist, argcount, rettv);
     }
-    else if (rettv_list_alloc(rettv) == OK)
-       for (idx = 0; idx < ARGCOUNT; ++idx)
-           list_append_string(rettv->vval.v_list,
-                                              alist_name(&ARGLIST[idx]), -1);
+    else
+       get_arglist_as_rettv(ARGLIST, ARGCOUNT, rettv);
 }
 
 /*
@@ -5358,7 +5413,6 @@ getpos_both(
        rettv->vval.v_number = FALSE;
 }
 
-
 /*
  * "getcurpos()" function
  */
@@ -6982,7 +7036,6 @@ f_inputlist(typval_T *argvars, typval_T *rettv)
     rettv->vval.v_number = selected;
 }
 
-
 static garray_T            ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
 
 /*
@@ -12397,7 +12450,6 @@ f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
            modec = 't';
     }
 
-
     switch (TOLOWER_ASC(what[0]))
     {
        case 'b':
@@ -12808,7 +12860,6 @@ f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
     }
 }
 
-
 /*
  * "tabpagenr()" function
  */
@@ -12900,7 +12951,6 @@ f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
     rettv->vval.v_number = nr;
 }
 
-
 /*
  * "tagfiles()" function
  */
@@ -14092,5 +14142,4 @@ f_xor(typval_T *argvars, typval_T *rettv)
                                        ^ get_tv_number_chk(&argvars[1], NULL);
 }
 
-
 #endif /* FEAT_EVAL */
index 37170a2f074db2db450ffbd8f751dc36117c3fe3..03c650641627be50c241d57d5c3047c6f264876d 100644 (file)
@@ -111,6 +111,7 @@ void ex_echo(exarg_T *eap);
 void ex_echohl(exarg_T *eap);
 void ex_execute(exarg_T *eap);
 win_T *find_win_by_nr(typval_T *vp, tabpage_T *tp);
+win_T *find_win_by_nr_or_id(typval_T *vp);
 win_T *find_tabwin(typval_T *wvp, typval_T *tvp);
 void getwinvar(typval_T *argvars, typval_T *rettv, int off);
 void setwinvar(typval_T *argvars, typval_T *rettv, int off);
index 057cb0d5d60a8b86f6dac19cbf1c19d86df1b24b..b896c3dec677867e9e26b94fd00412836cce5c75 100644 (file)
@@ -278,13 +278,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
index 9283cc2fd7a2787444c151956139245f9d50696b..7900ee74184920386dcc1b1bfd3e9f6e028d444b 100644 (file)
@@ -792,6 +792,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    493,
 /**/
     492,
 /**/