Skip to content

Commit 69bf634

Browse files
committed
patch 8.1.2233: cannot get the Vim command line arguments
Problem: Cannot get the Vim command line arguments. Solution: Add v:argv. (Dmitri Vereshchagin, closes #1322)
1 parent 8b530c1 commit 69bf634

8 files changed

Lines changed: 53 additions & 4 deletions

File tree

runtime/doc/eval.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*eval.txt* For Vim version 8.1. Last change: 2019 Oct 26
1+
*eval.txt* For Vim version 8.1. Last change: 2019 Oct 29
22

33

44
VIM REFERENCE MANUAL by Bram Moolenaar
@@ -1662,6 +1662,10 @@ PREDEFINED VIM VARIABLES *vim-variable* *v:var* *v:*
16621662
*E963*
16631663
Some variables can be set by the user, but the type cannot be changed.
16641664

1665+
*v:argv* *argv-variable*
1666+
v:argv The command line arguments Vim was invoked with. This is a
1667+
list of strings. The first item is the Vim command.
1668+
16651669
*v:beval_col* *beval_col-variable*
16661670
v:beval_col The number of the column, over which the mouse pointer is.
16671671
This is the byte index in the |v:beval_lnum| line.
@@ -3034,6 +3038,7 @@ argv([{nr} [, {winid}])
30343038
the whole |arglist| is returned.
30353039

30363040
The {winid} argument specifies the window ID, see |argc()|.
3041+
For the Vim command line arguments see |v:argv|.
30373042

30383043

30393044
assert_ functions are documented here: |assert-functions-details|

src/evalvars.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ static struct vimvar
143143
{VV_NAME("event", VAR_DICT), VV_RO},
144144
{VV_NAME("versionlong", VAR_NUMBER), VV_RO},
145145
{VV_NAME("echospace", VAR_NUMBER), VV_RO},
146+
{VV_NAME("argv", VAR_LIST), VV_RO},
146147
};
147148

148149
// shorthand
@@ -2085,6 +2086,27 @@ set_vim_var_dict(int idx, dict_T *val)
20852086
}
20862087
}
20872088

2089+
/*
2090+
* Set the v:argv list.
2091+
*/
2092+
void
2093+
set_argv_var(char **argv, int argc)
2094+
{
2095+
list_T *l = list_alloc();
2096+
int i;
2097+
2098+
if (l == NULL)
2099+
getout(1);
2100+
l->lv_lock = VAR_FIXED;
2101+
for (i = 0; i < argc; ++i)
2102+
{
2103+
if (list_append_string(l, (char_u *)argv[i], -1) == FAIL)
2104+
getout(1);
2105+
l->lv_last->li_tv.v_lock = VAR_FIXED;
2106+
}
2107+
set_vim_var_list(VV_ARGV, l);
2108+
}
2109+
20882110
/*
20892111
* Set v:register if needed.
20902112
*/

src/main.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1009,7 +1009,11 @@ common_init(mparm_T *paramp)
10091009
TIME_MSG("inits 1");
10101010

10111011
#ifdef FEAT_EVAL
1012-
set_lang_var(); /* set v:lang and v:ctype */
1012+
// set v:lang and v:ctype
1013+
set_lang_var();
1014+
1015+
// set v:argv
1016+
set_argv_var(paramp->argv, paramp->argc);
10131017
#endif
10141018

10151019
#ifdef FEAT_SIGNS

src/normal.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5986,7 +5986,7 @@ nv_g_cmd(cmdarg_T *cap)
59865986
oap->motion_type = MCHAR;
59875987
oap->inclusive = FALSE;
59885988
if (has_mbyte)
5989-
i = mb_string2cells(ptr, STRLEN(ptr));
5989+
i = mb_string2cells(ptr, (int)STRLEN(ptr));
59905990
else
59915991
i = (int)STRLEN(ptr);
59925992
if (cap->count0 > 0 && cap->count0 <= 100)

src/proto/evalvars.pro

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ void restore_vimvars(vimvars_save_T *vvsave);
4141
void set_vim_var_string(int idx, char_u *val, int len);
4242
void set_vim_var_list(int idx, list_T *val);
4343
void set_vim_var_dict(int idx, dict_T *val);
44+
void set_argv_var(char **argv, int argc);
4445
void set_reg_var(int c);
4546
char_u *v_exception(char_u *oldval);
4647
char_u *v_throwpoint(char_u *oldval);

src/testdir/test_startup.vim

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -671,3 +671,15 @@ func Test_start_with_tabs()
671671
" clean up
672672
call StopVimInTerminal(buf)
673673
endfunc
674+
675+
func Test_v_argv()
676+
" Can't catch the output of gvim.
677+
CheckNotGui
678+
679+
let out = system(GetVimCommand() . ' -es -V1 -X arg1 --cmd "echo v:argv" --cmd q')
680+
let list = out->split("', '")
681+
call assert_match('vim', list[0])
682+
let idx = index(list, 'arg1')
683+
call assert_true(idx > 2)
684+
call assert_equal(['arg1', '--cmd', 'echo v:argv', '--cmd', 'q'']'], list[idx:])
685+
endfunc

src/version.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -741,6 +741,10 @@ static char *(features[]) =
741741

742742
static int included_patches[] =
743743
{ /* Add new patch number below this line */
744+
/**/
745+
2233,
746+
/**/
747+
2232,
744748
/**/
745749
2231,
746750
/**/

src/vim.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1990,7 +1990,8 @@ typedef int sock_T;
19901990
#define VV_EVENT 90
19911991
#define VV_VERSIONLONG 91
19921992
#define VV_ECHOSPACE 92
1993-
#define VV_LEN 93 // number of v: vars
1993+
#define VV_ARGV 93
1994+
#define VV_LEN 94 // number of v: vars
19941995

19951996
// used for v_number in VAR_SPECIAL
19961997
#define VVAL_FALSE 0L

0 commit comments

Comments
 (0)