# HG changeset patch # User Bram Moolenaar # Date 1361391130 -3600 # Node ID cb185366f5c84852a7221ed6d9b274a7239ad06d # Parent ce49dd67405b69b41de735a1ee9ece1cceff53d1 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) diff --git a/runtime/doc/eval.txt b/runtime/doc/eval.txt --- a/runtime/doc/eval.txt +++ b/runtime/doc/eval.txt @@ -1,4 +1,4 @@ -*eval.txt* For Vim version 7.3. Last change: 2013 Jan 23 +*eval.txt* For Vim version 7.3. Last change: 2013 Feb 20 VIM REFERENCE MANUAL by Bram Moolenaar @@ -1777,7 +1777,8 @@ get( {list}, {idx} [, {def}]) any get it get( {dict}, {key} [, {def}]) any get item {key} from {dict} or {def} getbufline( {expr}, {lnum} [, {end}]) List lines {lnum} to {end} of buffer {expr} -getbufvar( {expr}, {varname}) any variable {varname} in buffer {expr} +getbufvar( {expr}, {varname} [, {def}]) + any variable {varname} in buffer {expr} getchar( [expr]) Number get one character from the user getcharmod( ) Number modifiers for the last typed character getcmdline() String return the current command-line @@ -1798,12 +1799,14 @@ getpos( {expr}) List position of curso getqflist() List list of quickfix items getreg( [{regname} [, 1]]) String contents of register getregtype( [{regname}]) String type of register -gettabvar( {nr}, {varname}) any variable {varname} in tab {nr} -gettabwinvar( {tabnr}, {winnr}, {name}) +gettabvar( {nr}, {varname} [, {def}]) + any variable {varname} in tab {nr} or {def} +gettabwinvar( {tabnr}, {winnr}, {name} [, {def}]) any {name} in {winnr} in tab page {tabnr} getwinposx() Number X coord in pixels of GUI Vim window getwinposy() Number Y coord in pixels of GUI Vim window -getwinvar( {nr}, {varname}) any variable {varname} in window {nr} +getwinvar( {nr}, {varname} [, {def}]) + any variable {varname} in window {nr} glob( {expr} [, {nosuf} [, {list}]]) any expand file wildcards in {expr} globpath( {path}, {expr} [, {flag}]) @@ -3143,7 +3146,7 @@ getbufline({expr}, {lnum} [, {end}]) Example: > :let lines = getbufline(bufnr("myfile"), 1, "$") -getbufvar({expr}, {varname}) *getbufvar()* +getbufvar({expr}, {varname} [, {def}]) *getbufvar()* The result is the value of option or local buffer variable {varname} in buffer {expr}. Note that the name without "b:" must be used. @@ -3153,8 +3156,8 @@ getbufvar({expr}, {varname}) *getbufv doesn't work for a global variable, window-local variable or window-local option. For the use of {expr}, see |bufname()| above. - When the buffer or variable doesn't exist an empty string is - returned, there is no error message. + When the buffer or variable doesn't exist {def} or an empty + string is returned, there is no error message. Examples: > :let bufmodified = getbufvar(1, "&mod") :echo "todo myvar = " . getbufvar("todo", "myvar") @@ -3431,26 +3434,30 @@ getregtype([{regname}]) *getregtype( is one character with value 0x16. If {regname} is not specified, |v:register| is used. -gettabvar({tabnr}, {varname}) *gettabvar()* +gettabvar({tabnr}, {varname} [, {def}]) *gettabvar()* Get the value of a tab-local variable {varname} in tab page {tabnr}. |t:var| Tabs are numbered starting with one. Note that the name without "t:" must be used. - -gettabwinvar({tabnr}, {winnr}, {varname}) *gettabwinvar()* + When the tab or variable doesn't exist {def} or an empty + string is returned, there is no error message. + +gettabwinvar({tabnr}, {winnr}, {varname} [, {def}]) *gettabwinvar()* Get the value of window-local variable {varname} in window {winnr} in tab page {tabnr}. When {varname} starts with "&" get the value of a window-local option. + When {varname} is empty a dictionary with all window-local + variables is returned. + Note that {varname} must be the name without "w:". Tabs are numbered starting with one. For the current tabpage use |getwinvar()|. When {winnr} is zero the current window is used. This also works for a global option, buffer-local option and window-local option, but it doesn't work for a global variable or buffer-local variable. - When {varname} is empty a dictionary with all window-local - variables is returned. - Note that {varname} must be the name without "w:". + When the tab, window or variable doesn't exist {def} or an + empty string is returned, there is no error message. Examples: > :let list_is_on = gettabwinvar(1, 2, '&list') :echo "myvar = " . gettabwinvar(3, 1, 'myvar') @@ -3465,7 +3472,7 @@ getwinposy() The result is a Number, whi the top of the GUI Vim window. The result will be -1 if the information is not available. -getwinvar({winnr}, {varname}) *getwinvar()* +getwinvar({winnr}, {varname} [, {def}]) *getwinvar()* Like |gettabwinvar()| for the current tabpage. Examples: > :let list_is_on = getwinvar(2, '&list') diff --git a/src/eval.c b/src/eval.c --- a/src/eval.c +++ b/src/eval.c @@ -7916,7 +7916,7 @@ static struct fst {"garbagecollect", 0, 1, f_garbagecollect}, {"get", 2, 3, f_get}, {"getbufline", 2, 3, f_getbufline}, - {"getbufvar", 2, 2, f_getbufvar}, + {"getbufvar", 2, 3, f_getbufvar}, {"getchar", 0, 1, f_getchar}, {"getcharmod", 0, 0, f_getcharmod}, {"getcmdline", 0, 0, f_getcmdline}, @@ -7936,11 +7936,11 @@ static struct fst {"getqflist", 0, 0, f_getqflist}, {"getreg", 0, 2, f_getreg}, {"getregtype", 0, 1, f_getregtype}, - {"gettabvar", 2, 2, f_gettabvar}, - {"gettabwinvar", 3, 3, f_gettabwinvar}, + {"gettabvar", 2, 3, f_gettabvar}, + {"gettabwinvar", 3, 4, f_gettabwinvar}, {"getwinposx", 0, 0, f_getwinposx}, {"getwinposy", 0, 0, f_getwinposy}, - {"getwinvar", 2, 2, f_getwinvar}, + {"getwinvar", 2, 3, f_getwinvar}, {"glob", 1, 3, f_glob}, {"globpath", 2, 3, f_globpath}, {"has", 1, 1, f_has}, @@ -11115,8 +11115,14 @@ f_getbufvar(argvars, rettv) ++emsg_off; buf = get_buf_tv(&argvars[0]); - rettv->v_type = VAR_STRING; - rettv->vval.v_string = NULL; + if (argvars[2].v_type != VAR_UNKNOWN) + /* set the default value */ + copy_tv(&argvars[2], rettv); + else + { + rettv->v_type = VAR_STRING; + rettv->vval.v_string = NULL; + } if (buf != NULL && varname != NULL) { @@ -11785,7 +11791,11 @@ f_gettabvar(argvars, rettv) v = find_var_in_ht(&tp->tp_vars.dv_hashtab, varname, FALSE); if (v != NULL) copy_tv(&v->di_tv, rettv); - } + else if (argvars[2].v_type != VAR_UNKNOWN) + copy_tv(&argvars[2], rettv); + } + else if (argvars[2].v_type != VAR_UNKNOWN) + copy_tv(&argvars[2], rettv); } /* @@ -11907,8 +11917,14 @@ getwinvar(argvars, rettv, off) varname = get_tv_string_chk(&argvars[off + 1]); ++emsg_off; - rettv->v_type = VAR_STRING; - rettv->vval.v_string = NULL; + if (argvars[off + 2].v_type != VAR_UNKNOWN) + /* set the default return value */ + copy_tv(&argvars[off + 2], rettv); + else + { + rettv->v_type = VAR_STRING; + rettv->vval.v_string = NULL; + } if (win != NULL && varname != NULL) { diff --git a/src/testdir/Make_amiga.mak b/src/testdir/Make_amiga.mak --- a/src/testdir/Make_amiga.mak +++ b/src/testdir/Make_amiga.mak @@ -32,7 +32,7 @@ SCRIPTS = test1.out test3.out test4.out test71.out test72.out test73.out test74.out test75.out \ test76.out test77.out test78.out test79.out test80.out \ test81.out test82.out test83.out test84.out test88.out \ - test89.out test90.out + test89.out test90.out test91.out .SUFFIXES: .in .out @@ -139,3 +139,4 @@ test84.out: test84.in test88.out: test88.in test89.out: test89.in test90.out: test90.in +test91.out: test91.in diff --git a/src/testdir/Make_dos.mak b/src/testdir/Make_dos.mak --- a/src/testdir/Make_dos.mak +++ b/src/testdir/Make_dos.mak @@ -31,7 +31,7 @@ SCRIPTS = test3.out test4.out test5.out test74.out test75.out test76.out test77.out test78.out \ test79.out test80.out test81.out test82.out test83.out \ test84.out test85.out test86.out test87.out test88.out \ - test89.out test90.out + test89.out test90.out test91.out SCRIPTS32 = test50.out test70.out diff --git a/src/testdir/Make_ming.mak b/src/testdir/Make_ming.mak --- a/src/testdir/Make_ming.mak +++ b/src/testdir/Make_ming.mak @@ -51,7 +51,7 @@ SCRIPTS = test3.out test4.out test5.out test74.out test75.out test76.out test77.out test78.out \ test79.out test80.out test81.out test82.out test83.out \ test84.out test85.out test86.out test87.out test88.out \ - test89.out test90.out + test89.out test90.out test91.out SCRIPTS32 = test50.out test70.out diff --git a/src/testdir/Make_os2.mak b/src/testdir/Make_os2.mak --- a/src/testdir/Make_os2.mak +++ b/src/testdir/Make_os2.mak @@ -32,7 +32,7 @@ SCRIPTS = test1.out test3.out test4.out test71.out test72.out test73.out test74.out test75.out \ test76.out test77.out test78.out test79.out test80.out \ test81.out test82.out test83.out test84.out test88.out \ - test89.out test90.out + test89.out test90.out test91.out .SUFFIXES: .in .out diff --git a/src/testdir/Make_vms.mms b/src/testdir/Make_vms.mms --- a/src/testdir/Make_vms.mms +++ b/src/testdir/Make_vms.mms @@ -4,7 +4,7 @@ # Authors: Zoltan Arpadffy, # Sandor Kopanyi, # -# Last change: 2013 Feb 13 +# Last change: 2013 Feb 20 # # This has been tested on VMS 6.2 to 8.3 on DEC Alpha, VAX and IA64. # Edit the lines in the Configuration section below to select. @@ -77,7 +77,7 @@ SCRIPT = test1.out test2.out test3.out test71.out test72.out test74.out test75.out test76.out \ test77.out test78.out test79.out test80.out test81.out \ test82.out test83.out test84.out test88.out test89.out \ - test90.out + test90.out test91.out # Known problems: # Test 30: a problem around mac format - unknown reason diff --git a/src/testdir/Makefile b/src/testdir/Makefile --- a/src/testdir/Makefile +++ b/src/testdir/Makefile @@ -28,7 +28,7 @@ SCRIPTS = test1.out test2.out test3.out test74.out test75.out test76.out test77.out test78.out \ test79.out test80.out test81.out test82.out test83.out \ test84.out test85.out test86.out test87.out test88.out \ - test89.out test90.out + test89.out test90.out test91.out SCRIPTS_GUI = test16.out diff --git a/src/testdir/test91.in b/src/testdir/test91.in new file mode 100644 --- /dev/null +++ b/src/testdir/test91.in @@ -0,0 +1,98 @@ +Tests for getbufvar(), getwinvar(), gettabvar() and gettabwinvar(). +vim: set ft=vim : + +STARTTEST +:so small.vim +:" +:" test for getbufvar() +:let b:var_num = 1234 +:let def_num = 5678 +:$put =string(getbufvar(1, 'var_num')) +:$put =string(getbufvar(1, 'var_num', def_num)) +:$put =string(getbufvar(1, '')) +:$put =string(getbufvar(1, '', def_num)) +:unlet b:var_num +:$put =string(getbufvar(1, 'var_num', def_num)) +:$put =string(getbufvar(1, '')) +:$put =string(getbufvar(1, '', def_num)) +:$put =string(getbufvar(9, '')) +:$put =string(getbufvar(9, '', def_num)) +:unlet def_num +:$put =string(getbufvar(1, '&autoindent')) +:$put =string(getbufvar(1, '&autoindent', 1)) +:" +:" test for getwinvar() +:let w:var_str = "Dance" +:let def_str = "Chance" +:$put =string(getwinvar(1, 'var_str')) +:$put =string(getwinvar(1, 'var_str', def_str)) +:$put =string(getwinvar(1, '')) +:$put =string(getwinvar(1, '', def_str)) +:unlet w:var_str +:$put =string(getwinvar(1, 'var_str', def_str)) +:$put =string(getwinvar(1, '')) +:$put =string(getwinvar(1, '', def_str)) +:$put =string(getwinvar(9, '')) +:$put =string(getwinvar(9, '', def_str)) +:$put =string(getwinvar(1, '&nu')) +:$put =string(getwinvar(1, '&nu', 1)) +:unlet def_str +:" +:" test for gettabvar() +:tabnew +:tabnew +:let t:var_list = [1, 2, 3] +:let def_list = [4, 5, 6, 7] +:tabrewind +:$put =string(gettabvar(3, 'var_list')) +:$put =string(gettabvar(3, 'var_list', def_list)) +:$put =string(gettabvar(3, '')) +:$put =string(gettabvar(3, '', def_list)) +:tablast +:unlet t:var_list +:tabrewind +:$put =string(gettabvar(3, 'var_list', def_list)) +:$put =string(gettabvar(9, '')) +:$put =string(gettabvar(9, '', def_list)) +:$put =string(gettabvar(3, '&nu')) +:$put =string(gettabvar(3, '&nu', def_list)) +:unlet def_list +:tabonly +:" +:" test for gettabwinvar() +:tabnew +:tabnew +:tabprev +:split +:split +:wincmd w +:vert split +:wincmd w +:let w:var_dict = {'dict': 'tabwin'} +:let def_dict = {'dict2': 'newval'} +:wincmd b +:tabrewind +:$put =string(gettabwinvar(2, 3, 'var_dict')) +:$put =string(gettabwinvar(2, 3, 'var_dict', def_dict)) +:$put =string(gettabwinvar(2, 3, '')) +:$put =string(gettabwinvar(2, 3, '', def_dict)) +:tabnext +:3wincmd w +:unlet w:var_dict +:tabrewind +:$put =string(gettabwinvar(2, 3, 'var_dict', def_dict)) +:$put =string(gettabwinvar(2, 3, '')) +:$put =string(gettabwinvar(2, 3, '', def_dict)) +:$put =string(gettabwinvar(2, 9, '')) +:$put =string(gettabwinvar(2, 9, '', def_dict)) +:$put =string(gettabwinvar(9, 3, '')) +:$put =string(gettabwinvar(9, 3, '', def_dict)) +:unlet def_dict +:$put =string(gettabwinvar(2, 3, '&nux')) +:$put =string(gettabwinvar(2, 3, '&nux', 1)) +:tabonly +:" +:/^start/,$wq! test.out +ENDTEST + +start: diff --git a/src/testdir/test91.ok b/src/testdir/test91.ok new file mode 100644 --- /dev/null +++ b/src/testdir/test91.ok @@ -0,0 +1,45 @@ +start: +1234 +1234 +{'var_num': 1234} +{'var_num': 1234} +5678 +{} +{} +'' +5678 +0 +0 +'Dance' +'Dance' +{'var_str': 'Dance'} +{'var_str': 'Dance'} +'Chance' +{} +{} +'' +'Chance' +0 +0 +[1, 2, 3] +[1, 2, 3] +'' +[4, 5, 6, 7] +[4, 5, 6, 7] +'' +[4, 5, 6, 7] +'' +[4, 5, 6, 7] +{'dict': 'tabwin'} +{'dict': 'tabwin'} +{'var_dict': {'dict': 'tabwin'}} +{'var_dict': {'dict': 'tabwin'}} +{'dict2': 'newval'} +{} +{} +'' +{'dict2': 'newval'} +'' +{'dict2': 'newval'} +'' +1 diff --git a/src/version.c b/src/version.c --- a/src/version.c +++ b/src/version.c @@ -729,6 +729,8 @@ static char *(features[]) = static int included_patches[] = { /* Add new patch number below this line */ /**/ + 831, +/**/ 830, /**/ 829,