Skip to content

Commit 8eae3da

Browse files
committed
Merge remote-tracking branch 'vim/master'
2 parents 05f6723 + 9532fe7 commit 8eae3da

26 files changed

Lines changed: 900 additions & 288 deletions

runtime/defaults.vim

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
" The default vimrc file.
22
"
33
" Maintainer: Bram Moolenaar <[email protected]>
4-
" Last change: 2016 Jul 28
4+
" Last change: 2016 Jul 29
55
"
66
" This is loaded if no vimrc file was found.
77
" Except when Vim is run with "-u NONE" or "-C".
@@ -25,6 +25,9 @@ set ruler " show the cursor position all the time
2525
set showcmd " display incomplete commands
2626
set wildmenu " display completion matches in a status line
2727

28+
" Show @@@ in the last line if it is truncated.
29+
set display=truncate
30+
2831
" Do incremental searching when it's possible to timeout.
2932
if has('reltime')
3033
set incsearch

runtime/doc/eval.txt

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*eval.txt* For Vim version 7.4. Last change: 2016 Jul 24
1+
*eval.txt* For Vim version 7.4. Last change: 2016 Jul 29
22

33

44
VIM REFERENCE MANUAL by Bram Moolenaar
@@ -40,7 +40,7 @@ done, the features in this document are not available. See |+eval| and
4040
There are nine types of variables:
4141

4242
Number A 32 or 64 bit signed number. |expr-number| *Number*
43-
64-bit Number is available only when compiled with the
43+
64-bit Numbers are available only when compiled with the
4444
|+num64| feature.
4545
Examples: -123 0x10 0177
4646

@@ -1219,7 +1219,7 @@ the following ways:
12191219

12201220
1. The body of the lambda expression is an |expr1| and not a sequence of |Ex|
12211221
commands.
1222-
2. The prefix "a:" is optional for arguments. E.g.: >
1222+
2. The prefix "a:" should not be used for arguments. E.g.: >
12231223
:let F = {arg1, arg2 -> arg1 - arg2}
12241224
:echo F(5, 2)
12251225
< 3
@@ -1228,6 +1228,19 @@ The arguments are optional. Example: >
12281228
:let F = {-> 'error function'}
12291229
:echo F()
12301230
< error function
1231+
*closure*
1232+
Lambda expressions can access outer scope variables and arguments. This is
1233+
often called a closure. Example where "i" a and "a:arg" are used in a lambda
1234+
while they exists in the function scope. They remain valid even after the
1235+
function returns: >
1236+
:function Foo(arg)
1237+
: let i = 3
1238+
: return {x -> x + i - a:arg}
1239+
:endfunction
1240+
:let Bar = Foo(4)
1241+
:echo Bar(6)
1242+
< 5
1243+
See also |:func-closure|.
12311244

12321245
Examples for using a lambda expression with |sort()|, |map()| and |filter()|: >
12331246
:echo map([1, 2, 3], {idx, val -> val + 1})
@@ -1245,6 +1258,12 @@ The lambda expression is also useful for Channel, Job and timer: >
12451258

12461259
Note how execute() is used to execute an Ex command. That's ugly though.
12471260

1261+
1262+
Lambda expressions have internal names like '<lambda>42'. If you get an error
1263+
for a lambda expression, you can find what it is with the following command: >
1264+
:function {'<lambda>42'}
1265+
See also: |numbered-function|
1266+
12481267
==============================================================================
12491268
3. Internal variable *internal-variables* *E461*
12501269

@@ -7494,7 +7513,8 @@ test_null_string() *test_null_string()*
74947513

74957514
test_settime({expr}) *test_settime()*
74967515
Set the time Vim uses internally. Currently only used for
7497-
timestamps in the history, as they are used in viminfo.
7516+
timestamps in the history, as they are used in viminfo, and
7517+
for undo.
74987518
{expr} must evaluate to a number. When the value is zero the
74997519
normal behavior is restored.
75007520

@@ -8202,7 +8222,7 @@ last defined. Example: >
82028222
See |:verbose-cmd| for more information.
82038223

82048224
*E124* *E125* *E853* *E884*
8205-
:fu[nction][!] {name}([arguments]) [range] [abort] [dict]
8225+
:fu[nction][!] {name}([arguments]) [range] [abort] [dict] [closure]
82068226
Define a new function by the name {name}. The name
82078227
must be made of alphanumeric characters and '_', and
82088228
must start with a capital or "s:" (see above). Note
@@ -8245,6 +8265,28 @@ See |:verbose-cmd| for more information.
82458265
be invoked through an entry in a |Dictionary|. The
82468266
local variable "self" will then be set to the
82478267
dictionary. See |Dictionary-function|.
8268+
*:func-closure* *E932*
8269+
When the [closure] argument is added, the function
8270+
can access variables and arguments from the outer
8271+
scope. This is usually called a closure. In this
8272+
example Bar() uses "x" from the scope of Foo(). It
8273+
remains referenced even after Foo() returns: >
8274+
:function! Foo()
8275+
: let x = 0
8276+
: function! Bar() closure
8277+
: let x += 1
8278+
: return x
8279+
: endfunction
8280+
: return function('Bar')
8281+
:endfunction
8282+
8283+
:let F = Foo()
8284+
:echo F()
8285+
< 1 >
8286+
:echo F()
8287+
< 2 >
8288+
:echo F()
8289+
< 3
82488290

82498291
*function-search-undo*
82508292
The last used search pattern and the redo command "."

src/Makefile

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2061,7 +2061,7 @@ test1 \
20612061
test30 test31 test32 test33 test34 test36 test37 test38 test39 \
20622062
test40 test41 test42 test43 test44 test45 test46 test47 test48 test49 \
20632063
test50 test51 test52 test53 test54 test55 test56 test57 test58 test59 \
2064-
test60 test61 test62 test63 test64 test65 test66 test67 test68 test69 \
2064+
test60 test62 test63 test64 test65 test66 test67 test68 test69 \
20652065
test70 test71 test72 test73 test74 test75 test76 test77 test78 test79 \
20662066
test80 test81 test82 test83 test84 test85 test86 test87 test88 test89 \
20672067
test90 test91 test92 test93 test94 test95 test97 test98 test99 \
@@ -2124,6 +2124,7 @@ test_arglist \
21242124
test_regexp_utf8 \
21252125
test_reltime \
21262126
test_ruby \
2127+
test_startup \
21272128
test_searchpos \
21282129
test_set \
21292130
test_sort \
@@ -2136,9 +2137,9 @@ test_arglist \
21362137
test_textobjects \
21372138
test_timers \
21382139
test_true_false \
2139-
test_undolevels \
2140-
test_usercommands \
2140+
test_undo \
21412141
test_unlet \
2142+
test_usercommands \
21422143
test_viminfo \
21432144
test_viml \
21442145
test_visual \

src/dosinst.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1153,10 +1153,9 @@ install_vimrc(int idx)
11531153
fprintf(fd, "set compatible\n");
11541154
break;
11551155
case compat_some_enhancements:
1156-
fprintf(fd, "set nocompatible\n");
1156+
fprintf(fd, "source $VIMRUNTIME/defaults.vim\n");
11571157
break;
11581158
case compat_all_enhancements:
1159-
fprintf(fd, "set nocompatible\n");
11601159
fprintf(fd, "source $VIMRUNTIME/vimrc_example.vim\n");
11611160
break;
11621161
}

src/eval.c

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -237,8 +237,8 @@ static int get_env_tv(char_u **arg, typval_T *rettv, int evaluate);
237237

238238
static int get_env_len(char_u **arg);
239239
static char_u * make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
240+
static void check_vars(char_u *name, int len);
240241
static typval_T *alloc_string_tv(char_u *string);
241-
static hashtab_T *find_var_ht(char_u *name, char_u **varname);
242242
static void delete_var(hashtab_T *ht, hashitem_T *hi);
243243
static void list_one_var(dictitem_T *v, char_u *prefix, int *first);
244244
static void list_one_var_a(char_u *prefix, char_u *name, int type, char_u *string, int *first);
@@ -2837,7 +2837,9 @@ do_unlet(char_u *name, int forceit)
28372837
}
28382838
}
28392839
hi = hash_find(ht, varname);
2840-
if (!HASHITEM_EMPTY(hi))
2840+
if (HASHITEM_EMPTY(hi))
2841+
hi = find_hi_in_scoped_ht(name, &varname, &ht);
2842+
if (hi != NULL && !HASHITEM_EMPTY(hi))
28412843
{
28422844
di = HI2DI(hi);
28432845
if (var_check_fixed(di->di_flags, name, FALSE)
@@ -4332,6 +4334,9 @@ eval7(
43324334
{
43334335
partial_T *partial;
43344336

4337+
if (!evaluate)
4338+
check_vars(s, len);
4339+
43354340
/* If "s" is the name of a variable of type VAR_FUNC
43364341
* use its contents. */
43374342
s = deref_func_name(s, &len, &partial, !evaluate);
@@ -4363,7 +4368,10 @@ eval7(
43634368
else if (evaluate)
43644369
ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
43654370
else
4371+
{
4372+
check_vars(s, len);
43664373
ret = OK;
4374+
}
43674375
}
43684376
vim_free(alias);
43694377
}
@@ -5540,6 +5548,10 @@ set_ref_in_item(
55405548
}
55415549
}
55425550
}
5551+
else if (tv->v_type == VAR_FUNC)
5552+
{
5553+
abort = set_ref_in_func(tv->vval.v_string, copyID);
5554+
}
55435555
else if (tv->v_type == VAR_PARTIAL)
55445556
{
55455557
partial_T *pt = tv->vval.v_partial;
@@ -5549,6 +5561,8 @@ set_ref_in_item(
55495561
*/
55505562
if (pt != NULL)
55515563
{
5564+
abort = set_ref_in_func(pt->pt_name, copyID);
5565+
55525566
if (pt->pt_dict != NULL)
55535567
{
55545568
typval_T dtv;
@@ -6790,6 +6804,34 @@ get_var_tv(
67906804
return ret;
67916805
}
67926806

6807+
/*
6808+
* Check if variable "name[len]" is a local variable or an argument.
6809+
* If so, "*eval_lavars_used" is set to TRUE.
6810+
*/
6811+
static void
6812+
check_vars(char_u *name, int len)
6813+
{
6814+
int cc;
6815+
char_u *varname;
6816+
hashtab_T *ht;
6817+
6818+
if (eval_lavars_used == NULL)
6819+
return;
6820+
6821+
/* truncate the name, so that we can use strcmp() */
6822+
cc = name[len];
6823+
name[len] = NUL;
6824+
6825+
ht = find_var_ht(name, &varname);
6826+
if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
6827+
{
6828+
if (find_var(name, NULL, TRUE) != NULL)
6829+
*eval_lavars_used = TRUE;
6830+
}
6831+
6832+
name[len] = cc;
6833+
}
6834+
67936835
/*
67946836
* Handle expr[expr], expr[expr:expr] subscript and .name lookup.
67956837
* Also handle function call with Funcref variable: func(expr)
@@ -7274,13 +7316,20 @@ find_var(char_u *name, hashtab_T **htp, int no_autoload)
72747316
{
72757317
char_u *varname;
72767318
hashtab_T *ht;
7319+
dictitem_T *ret = NULL;
72777320

72787321
ht = find_var_ht(name, &varname);
72797322
if (htp != NULL)
72807323
*htp = ht;
72817324
if (ht == NULL)
72827325
return NULL;
7283-
return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
7326+
ret = find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
7327+
if (ret != NULL)
7328+
return ret;
7329+
7330+
/* Search in parent scope for lambda */
7331+
return find_var_in_scoped_ht(name, varname ? &varname : NULL,
7332+
no_autoload || htp != NULL);
72847333
}
72857334

72867335
/*
@@ -7341,7 +7390,7 @@ find_var_in_ht(
73417390
* Return NULL if the name is not valid.
73427391
* Set "varname" to the start of name without ':'.
73437392
*/
7344-
static hashtab_T *
7393+
hashtab_T *
73457394
find_var_ht(char_u *name, char_u **varname)
73467395
{
73477396
hashitem_T *hi;
@@ -7617,6 +7666,10 @@ set_var(
76177666
}
76187667
v = find_var_in_ht(ht, 0, varname, TRUE);
76197668

7669+
/* Search in parent scope which is possible to reference from lambda */
7670+
if (v == NULL)
7671+
v = find_var_in_scoped_ht(name, varname ? &varname : NULL, TRUE);
7672+
76207673
if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
76217674
&& var_check_func_name(name, v == NULL))
76227675
return;

src/evalfunc.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5218,6 +5218,7 @@ f_has(typval_T *argvars, typval_T *rettv)
52185218
#ifdef FEAT_KEYMAP
52195219
"keymap",
52205220
#endif
5221+
"lambda", /* always with FEAT_EVAL, since 7.4.2120 with closure */
52215222
#ifdef FEAT_LANGMAP
52225223
"langmap",
52235224
#endif

src/ex_cmds.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2867,7 +2867,6 @@ write_viminfo_barlines(vir_T *virp, FILE *fp_out)
28672867
}
28682868
#endif /* FEAT_VIMINFO */
28692869

2870-
#if defined(FEAT_CMDHIST) || defined(FEAT_VIMINFO) || defined(PROTO)
28712870
/*
28722871
* Return the current time in seconds. Calls time(), unless test_settime()
28732872
* was used.
@@ -2881,7 +2880,6 @@ vim_time(void)
28812880
return time(NULL);
28822881
# endif
28832882
}
2884-
#endif
28852883

28862884
/*
28872885
* Implementation of ":fixdel", also used by get_stty().

src/ex_cmds2.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1265,8 +1265,16 @@ set_ref_in_timer(int copyID)
12651265

12661266
for (timer = first_timer; timer != NULL; timer = timer->tr_next)
12671267
{
1268-
tv.v_type = VAR_PARTIAL;
1269-
tv.vval.v_partial = timer->tr_partial;
1268+
if (timer->tr_partial != NULL)
1269+
{
1270+
tv.v_type = VAR_PARTIAL;
1271+
tv.vval.v_partial = timer->tr_partial;
1272+
}
1273+
else
1274+
{
1275+
tv.v_type = VAR_FUNC;
1276+
tv.vval.v_string = timer->tr_callback;
1277+
}
12701278
abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
12711279
}
12721280
return abort;

0 commit comments

Comments
 (0)