Skip to content

Commit 51d92c0

Browse files
committed
updated for version 7.3.831
Problem: Clumsy to handle the situation that a variable does not exist. Solution: Add default value to getbufvar() et al. (Shougo Matsushita, Hirohito Higashi)
1 parent 218a85f commit 51d92c0

File tree

11 files changed

+198
-29
lines changed

11 files changed

+198
-29
lines changed

runtime/doc/eval.txt

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*eval.txt* For Vim version 7.3. Last change: 2013 Jan 23
1+
*eval.txt* For Vim version 7.3. Last change: 2013 Feb 20
22

33

44
VIM REFERENCE MANUAL by Bram Moolenaar
@@ -1777,7 +1777,8 @@ get( {list}, {idx} [, {def}]) any get item {idx} from {list} or {def}
17771777
get( {dict}, {key} [, {def}]) any get item {key} from {dict} or {def}
17781778
getbufline( {expr}, {lnum} [, {end}])
17791779
List lines {lnum} to {end} of buffer {expr}
1780-
getbufvar( {expr}, {varname}) any variable {varname} in buffer {expr}
1780+
getbufvar( {expr}, {varname} [, {def}])
1781+
any variable {varname} in buffer {expr}
17811782
getchar( [expr]) Number get one character from the user
17821783
getcharmod( ) Number modifiers for the last typed character
17831784
getcmdline() String return the current command-line
@@ -1798,12 +1799,14 @@ getpos( {expr}) List position of cursor, mark, etc.
17981799
getqflist() List list of quickfix items
17991800
getreg( [{regname} [, 1]]) String contents of register
18001801
getregtype( [{regname}]) String type of register
1801-
gettabvar( {nr}, {varname}) any variable {varname} in tab {nr}
1802-
gettabwinvar( {tabnr}, {winnr}, {name})
1802+
gettabvar( {nr}, {varname} [, {def}])
1803+
any variable {varname} in tab {nr} or {def}
1804+
gettabwinvar( {tabnr}, {winnr}, {name} [, {def}])
18031805
any {name} in {winnr} in tab page {tabnr}
18041806
getwinposx() Number X coord in pixels of GUI Vim window
18051807
getwinposy() Number Y coord in pixels of GUI Vim window
1806-
getwinvar( {nr}, {varname}) any variable {varname} in window {nr}
1808+
getwinvar( {nr}, {varname} [, {def}])
1809+
any variable {varname} in window {nr}
18071810
glob( {expr} [, {nosuf} [, {list}]])
18081811
any expand file wildcards in {expr}
18091812
globpath( {path}, {expr} [, {flag}])
@@ -3143,7 +3146,7 @@ getbufline({expr}, {lnum} [, {end}])
31433146
Example: >
31443147
:let lines = getbufline(bufnr("myfile"), 1, "$")
31453148
3146-
getbufvar({expr}, {varname}) *getbufvar()*
3149+
getbufvar({expr}, {varname} [, {def}]) *getbufvar()*
31473150
The result is the value of option or local buffer variable
31483151
{varname} in buffer {expr}. Note that the name without "b:"
31493152
must be used.
@@ -3153,8 +3156,8 @@ getbufvar({expr}, {varname}) *getbufvar()*
31533156
doesn't work for a global variable, window-local variable or
31543157
window-local option.
31553158
For the use of {expr}, see |bufname()| above.
3156-
When the buffer or variable doesn't exist an empty string is
3157-
returned, there is no error message.
3159+
When the buffer or variable doesn't exist {def} or an empty
3160+
string is returned, there is no error message.
31583161
Examples: >
31593162
:let bufmodified = getbufvar(1, "&mod")
31603163
:echo "todo myvar = " . getbufvar("todo", "myvar")
@@ -3431,26 +3434,30 @@ getregtype([{regname}]) *getregtype()*
34313434
<CTRL-V> is one character with value 0x16.
34323435
If {regname} is not specified, |v:register| is used.
34333436

3434-
gettabvar({tabnr}, {varname}) *gettabvar()*
3437+
gettabvar({tabnr}, {varname} [, {def}]) *gettabvar()*
34353438
Get the value of a tab-local variable {varname} in tab page
34363439
{tabnr}. |t:var|
34373440
Tabs are numbered starting with one.
34383441
Note that the name without "t:" must be used.
3442+
When the tab or variable doesn't exist {def} or an empty
3443+
string is returned, there is no error message.
34393444

3440-
gettabwinvar({tabnr}, {winnr}, {varname}) *gettabwinvar()*
3445+
gettabwinvar({tabnr}, {winnr}, {varname} [, {def}]) *gettabwinvar()*
34413446
Get the value of window-local variable {varname} in window
34423447
{winnr} in tab page {tabnr}.
34433448
When {varname} starts with "&" get the value of a window-local
34443449
option.
3450+
When {varname} is empty a dictionary with all window-local
3451+
variables is returned.
3452+
Note that {varname} must be the name without "w:".
34453453
Tabs are numbered starting with one. For the current tabpage
34463454
use |getwinvar()|.
34473455
When {winnr} is zero the current window is used.
34483456
This also works for a global option, buffer-local option and
34493457
window-local option, but it doesn't work for a global variable
34503458
or buffer-local variable.
3451-
When {varname} is empty a dictionary with all window-local
3452-
variables is returned.
3453-
Note that {varname} must be the name without "w:".
3459+
When the tab, window or variable doesn't exist {def} or an
3460+
empty string is returned, there is no error message.
34543461
Examples: >
34553462
:let list_is_on = gettabwinvar(1, 2, '&list')
34563463
:echo "myvar = " . gettabwinvar(3, 1, 'myvar')
@@ -3465,7 +3472,7 @@ getwinposy() The result is a Number, which is the Y coordinate in pixels of
34653472
the top of the GUI Vim window. The result will be -1 if the
34663473
information is not available.
34673474

3468-
getwinvar({winnr}, {varname}) *getwinvar()*
3475+
getwinvar({winnr}, {varname} [, {def}]) *getwinvar()*
34693476
Like |gettabwinvar()| for the current tabpage.
34703477
Examples: >
34713478
:let list_is_on = getwinvar(2, '&list')

src/eval.c

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7916,7 +7916,7 @@ static struct fst
79167916
{"garbagecollect", 0, 1, f_garbagecollect},
79177917
{"get", 2, 3, f_get},
79187918
{"getbufline", 2, 3, f_getbufline},
7919-
{"getbufvar", 2, 2, f_getbufvar},
7919+
{"getbufvar", 2, 3, f_getbufvar},
79207920
{"getchar", 0, 1, f_getchar},
79217921
{"getcharmod", 0, 0, f_getcharmod},
79227922
{"getcmdline", 0, 0, f_getcmdline},
@@ -7936,11 +7936,11 @@ static struct fst
79367936
{"getqflist", 0, 0, f_getqflist},
79377937
{"getreg", 0, 2, f_getreg},
79387938
{"getregtype", 0, 1, f_getregtype},
7939-
{"gettabvar", 2, 2, f_gettabvar},
7940-
{"gettabwinvar", 3, 3, f_gettabwinvar},
7939+
{"gettabvar", 2, 3, f_gettabvar},
7940+
{"gettabwinvar", 3, 4, f_gettabwinvar},
79417941
{"getwinposx", 0, 0, f_getwinposx},
79427942
{"getwinposy", 0, 0, f_getwinposy},
7943-
{"getwinvar", 2, 2, f_getwinvar},
7943+
{"getwinvar", 2, 3, f_getwinvar},
79447944
{"glob", 1, 3, f_glob},
79457945
{"globpath", 2, 3, f_globpath},
79467946
{"has", 1, 1, f_has},
@@ -11115,8 +11115,14 @@ f_getbufvar(argvars, rettv)
1111511115
++emsg_off;
1111611116
buf = get_buf_tv(&argvars[0]);
1111711117

11118-
rettv->v_type = VAR_STRING;
11119-
rettv->vval.v_string = NULL;
11118+
if (argvars[2].v_type != VAR_UNKNOWN)
11119+
/* set the default value */
11120+
copy_tv(&argvars[2], rettv);
11121+
else
11122+
{
11123+
rettv->v_type = VAR_STRING;
11124+
rettv->vval.v_string = NULL;
11125+
}
1112011126

1112111127
if (buf != NULL && varname != NULL)
1112211128
{
@@ -11785,7 +11791,11 @@ f_gettabvar(argvars, rettv)
1178511791
v = find_var_in_ht(&tp->tp_vars.dv_hashtab, varname, FALSE);
1178611792
if (v != NULL)
1178711793
copy_tv(&v->di_tv, rettv);
11794+
else if (argvars[2].v_type != VAR_UNKNOWN)
11795+
copy_tv(&argvars[2], rettv);
1178811796
}
11797+
else if (argvars[2].v_type != VAR_UNKNOWN)
11798+
copy_tv(&argvars[2], rettv);
1178911799
}
1179011800

1179111801
/*
@@ -11907,8 +11917,14 @@ getwinvar(argvars, rettv, off)
1190711917
varname = get_tv_string_chk(&argvars[off + 1]);
1190811918
++emsg_off;
1190911919

11910-
rettv->v_type = VAR_STRING;
11911-
rettv->vval.v_string = NULL;
11920+
if (argvars[off + 2].v_type != VAR_UNKNOWN)
11921+
/* set the default return value */
11922+
copy_tv(&argvars[off + 2], rettv);
11923+
else
11924+
{
11925+
rettv->v_type = VAR_STRING;
11926+
rettv->vval.v_string = NULL;
11927+
}
1191211928

1191311929
if (win != NULL && varname != NULL)
1191411930
{

src/testdir/Make_amiga.mak

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ SCRIPTS = test1.out test3.out test4.out test5.out test6.out \
3232
test71.out test72.out test73.out test74.out test75.out \
3333
test76.out test77.out test78.out test79.out test80.out \
3434
test81.out test82.out test83.out test84.out test88.out \
35-
test89.out test90.out
35+
test89.out test90.out test91.out
3636

3737
.SUFFIXES: .in .out
3838

@@ -139,3 +139,4 @@ test84.out: test84.in
139139
test88.out: test88.in
140140
test89.out: test89.in
141141
test90.out: test90.in
142+
test91.out: test91.in

src/testdir/Make_dos.mak

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ SCRIPTS = test3.out test4.out test5.out test6.out test7.out \
3131
test74.out test75.out test76.out test77.out test78.out \
3232
test79.out test80.out test81.out test82.out test83.out \
3333
test84.out test85.out test86.out test87.out test88.out \
34-
test89.out test90.out
34+
test89.out test90.out test91.out
3535

3636
SCRIPTS32 = test50.out test70.out
3737

src/testdir/Make_ming.mak

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ SCRIPTS = test3.out test4.out test5.out test6.out test7.out \
5151
test74.out test75.out test76.out test77.out test78.out \
5252
test79.out test80.out test81.out test82.out test83.out \
5353
test84.out test85.out test86.out test87.out test88.out \
54-
test89.out test90.out
54+
test89.out test90.out test91.out
5555

5656
SCRIPTS32 = test50.out test70.out
5757

src/testdir/Make_os2.mak

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ SCRIPTS = test1.out test3.out test4.out test5.out test6.out \
3232
test71.out test72.out test73.out test74.out test75.out \
3333
test76.out test77.out test78.out test79.out test80.out \
3434
test81.out test82.out test83.out test84.out test88.out \
35-
test89.out test90.out
35+
test89.out test90.out test91.out
3636

3737
.SUFFIXES: .in .out
3838

src/testdir/Make_vms.mms

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# Authors: Zoltan Arpadffy, <[email protected]>
55
# Sandor Kopanyi, <[email protected]>
66
#
7-
# Last change: 2013 Feb 13
7+
# Last change: 2013 Feb 20
88
#
99
# This has been tested on VMS 6.2 to 8.3 on DEC Alpha, VAX and IA64.
1010
# Edit the lines in the Configuration section below to select.
@@ -77,7 +77,7 @@ SCRIPT = test1.out test2.out test3.out test4.out test5.out \
7777
test71.out test72.out test74.out test75.out test76.out \
7878
test77.out test78.out test79.out test80.out test81.out \
7979
test82.out test83.out test84.out test88.out test89.out \
80-
test90.out
80+
test90.out test91.out
8181

8282
# Known problems:
8383
# Test 30: a problem around mac format - unknown reason

src/testdir/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ SCRIPTS = test1.out test2.out test3.out test4.out test5.out test6.out \
2828
test74.out test75.out test76.out test77.out test78.out \
2929
test79.out test80.out test81.out test82.out test83.out \
3030
test84.out test85.out test86.out test87.out test88.out \
31-
test89.out test90.out
31+
test89.out test90.out test91.out
3232

3333
SCRIPTS_GUI = test16.out
3434

src/testdir/test91.in

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
Tests for getbufvar(), getwinvar(), gettabvar() and gettabwinvar().
2+
vim: set ft=vim :
3+
4+
STARTTEST
5+
:so small.vim
6+
:"
7+
:" test for getbufvar()
8+
:let b:var_num = 1234
9+
:let def_num = 5678
10+
:$put =string(getbufvar(1, 'var_num'))
11+
:$put =string(getbufvar(1, 'var_num', def_num))
12+
:$put =string(getbufvar(1, ''))
13+
:$put =string(getbufvar(1, '', def_num))
14+
:unlet b:var_num
15+
:$put =string(getbufvar(1, 'var_num', def_num))
16+
:$put =string(getbufvar(1, ''))
17+
:$put =string(getbufvar(1, '', def_num))
18+
:$put =string(getbufvar(9, ''))
19+
:$put =string(getbufvar(9, '', def_num))
20+
:unlet def_num
21+
:$put =string(getbufvar(1, '&autoindent'))
22+
:$put =string(getbufvar(1, '&autoindent', 1))
23+
:"
24+
:" test for getwinvar()
25+
:let w:var_str = "Dance"
26+
:let def_str = "Chance"
27+
:$put =string(getwinvar(1, 'var_str'))
28+
:$put =string(getwinvar(1, 'var_str', def_str))
29+
:$put =string(getwinvar(1, ''))
30+
:$put =string(getwinvar(1, '', def_str))
31+
:unlet w:var_str
32+
:$put =string(getwinvar(1, 'var_str', def_str))
33+
:$put =string(getwinvar(1, ''))
34+
:$put =string(getwinvar(1, '', def_str))
35+
:$put =string(getwinvar(9, ''))
36+
:$put =string(getwinvar(9, '', def_str))
37+
:$put =string(getwinvar(1, '&nu'))
38+
:$put =string(getwinvar(1, '&nu', 1))
39+
:unlet def_str
40+
:"
41+
:" test for gettabvar()
42+
:tabnew
43+
:tabnew
44+
:let t:var_list = [1, 2, 3]
45+
:let def_list = [4, 5, 6, 7]
46+
:tabrewind
47+
:$put =string(gettabvar(3, 'var_list'))
48+
:$put =string(gettabvar(3, 'var_list', def_list))
49+
:$put =string(gettabvar(3, ''))
50+
:$put =string(gettabvar(3, '', def_list))
51+
:tablast
52+
:unlet t:var_list
53+
:tabrewind
54+
:$put =string(gettabvar(3, 'var_list', def_list))
55+
:$put =string(gettabvar(9, ''))
56+
:$put =string(gettabvar(9, '', def_list))
57+
:$put =string(gettabvar(3, '&nu'))
58+
:$put =string(gettabvar(3, '&nu', def_list))
59+
:unlet def_list
60+
:tabonly
61+
:"
62+
:" test for gettabwinvar()
63+
:tabnew
64+
:tabnew
65+
:tabprev
66+
:split
67+
:split
68+
:wincmd w
69+
:vert split
70+
:wincmd w
71+
:let w:var_dict = {'dict': 'tabwin'}
72+
:let def_dict = {'dict2': 'newval'}
73+
:wincmd b
74+
:tabrewind
75+
:$put =string(gettabwinvar(2, 3, 'var_dict'))
76+
:$put =string(gettabwinvar(2, 3, 'var_dict', def_dict))
77+
:$put =string(gettabwinvar(2, 3, ''))
78+
:$put =string(gettabwinvar(2, 3, '', def_dict))
79+
:tabnext
80+
:3wincmd w
81+
:unlet w:var_dict
82+
:tabrewind
83+
:$put =string(gettabwinvar(2, 3, 'var_dict', def_dict))
84+
:$put =string(gettabwinvar(2, 3, ''))
85+
:$put =string(gettabwinvar(2, 3, '', def_dict))
86+
:$put =string(gettabwinvar(2, 9, ''))
87+
:$put =string(gettabwinvar(2, 9, '', def_dict))
88+
:$put =string(gettabwinvar(9, 3, ''))
89+
:$put =string(gettabwinvar(9, 3, '', def_dict))
90+
:unlet def_dict
91+
:$put =string(gettabwinvar(2, 3, '&nux'))
92+
:$put =string(gettabwinvar(2, 3, '&nux', 1))
93+
:tabonly
94+
:"
95+
:/^start/,$wq! test.out
96+
ENDTEST
97+
98+
start:

src/testdir/test91.ok

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
start:
2+
1234
3+
1234
4+
{'var_num': 1234}
5+
{'var_num': 1234}
6+
5678
7+
{}
8+
{}
9+
''
10+
5678
11+
0
12+
0
13+
'Dance'
14+
'Dance'
15+
{'var_str': 'Dance'}
16+
{'var_str': 'Dance'}
17+
'Chance'
18+
{}
19+
{}
20+
''
21+
'Chance'
22+
0
23+
0
24+
[1, 2, 3]
25+
[1, 2, 3]
26+
''
27+
[4, 5, 6, 7]
28+
[4, 5, 6, 7]
29+
''
30+
[4, 5, 6, 7]
31+
''
32+
[4, 5, 6, 7]
33+
{'dict': 'tabwin'}
34+
{'dict': 'tabwin'}
35+
{'var_dict': {'dict': 'tabwin'}}
36+
{'var_dict': {'dict': 'tabwin'}}
37+
{'dict2': 'newval'}
38+
{}
39+
{}
40+
''
41+
{'dict2': 'newval'}
42+
''
43+
{'dict2': 'newval'}
44+
''
45+
1

0 commit comments

Comments
 (0)