summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorYegappan Lakshmanan <yegappan@yahoo.com>2021-12-12 16:26:44 +0000
committerBram Moolenaar <Bram@vim.org>2021-12-12 16:26:44 +0000
commit6ae8fae8696623b527c7fb22567f6a3705b2f0dd (patch)
tree5013ad6590516571ae06f992906c4270d7f03b45 /src
parent6e371ecb27227ff8fedd8561d0f3880a17576848 (diff)
patch 8.2.3788: lambda for option that is a function may be freedv8.2.3788
Problem: Lambda for option that is a function may be garbage collected. Solution: Set a reference in the funcref. (Yegappan Lakshmanan, closes #9330)
Diffstat (limited to 'src')
-rw-r--r--src/eval.c28
-rw-r--r--src/evalbuffer.c36
-rw-r--r--src/evalvars.c1
-rw-r--r--src/gui_xim.c20
-rw-r--r--src/insexpand.c15
-rw-r--r--src/ops.c20
-rw-r--r--src/proto/eval.pro1
-rw-r--r--src/proto/gui_xim.pro1
-rw-r--r--src/proto/insexpand.pro1
-rw-r--r--src/proto/ops.pro1
-rw-r--r--src/proto/tag.pro1
-rw-r--r--src/quickfix.c13
-rw-r--r--src/tag.c24
-rw-r--r--src/testdir/test_iminsert.vim230
-rw-r--r--src/testdir/test_ins_complete.vim957
-rw-r--r--src/testdir/test_normal.vim248
-rw-r--r--src/testdir/test_quickfix.vim126
-rw-r--r--src/testdir/test_tagfunc.vim264
-rw-r--r--src/version.c2
19 files changed, 1091 insertions, 898 deletions
diff --git a/src/eval.c b/src/eval.c
index e2aa41b34f..64381f0f3d 100644
--- a/src/eval.c
+++ b/src/eval.c
@@ -4516,6 +4516,18 @@ garbage_collect(int testing)
// callbacks in buffers
abort = abort || set_ref_in_buffers(copyID);
+ // 'completefunc', 'omnifunc' and 'thesaurusfunc' callbacks
+ abort = abort || set_ref_in_insexpand_funcs(copyID);
+
+ // 'operatorfunc' callback
+ abort = abort || set_ref_in_opfunc(copyID);
+
+ // 'tagfunc' callback
+ abort = abort || set_ref_in_tagfunc(copyID);
+
+ // 'imactivatefunc' and 'imstatusfunc' callbacks
+ abort = abort || set_ref_in_im_funcs(copyID);
+
#ifdef FEAT_LUA
abort = abort || set_ref_in_lua(copyID);
#endif
@@ -4744,6 +4756,22 @@ set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
}
/*
+ * Mark the partial in callback 'cb' with "copyID".
+ */
+ int
+set_ref_in_callback(callback_T *cb, int copyID)
+{
+ typval_T tv;
+
+ if (cb->cb_name == NULL || *cb->cb_name == NUL || cb->cb_partial == NULL)
+ return FALSE;
+
+ tv.v_type = VAR_PARTIAL;
+ tv.vval.v_partial = cb->cb_partial;
+ return set_ref_in_item(&tv, copyID, NULL, NULL);
+}
+
+/*
* Mark all lists and dicts referenced through typval "tv" with "copyID".
* "list_stack" is used to add lists to be marked. Can be NULL.
* "ht_stack" is used to add hashtabs to be marked. Can be NULL.
diff --git a/src/evalbuffer.c b/src/evalbuffer.c
index 77acf8fe90..ba16436ded 100644
--- a/src/evalbuffer.c
+++ b/src/evalbuffer.c
@@ -26,31 +26,25 @@ set_ref_in_buffers(int copyID)
FOR_ALL_BUFFERS(bp)
{
listener_T *lnr;
- typval_T tv;
for (lnr = bp->b_listener; !abort && lnr != NULL; lnr = lnr->lr_next)
- {
- if (lnr->lr_callback.cb_partial != NULL)
- {
- tv.v_type = VAR_PARTIAL;
- tv.vval.v_partial = lnr->lr_callback.cb_partial;
- abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
- }
- }
+ abort = abort || set_ref_in_callback(&lnr->lr_callback, copyID);
# ifdef FEAT_JOB_CHANNEL
- if (!abort && bp->b_prompt_callback.cb_partial != NULL)
- {
- tv.v_type = VAR_PARTIAL;
- tv.vval.v_partial = bp->b_prompt_callback.cb_partial;
- abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
- }
- if (!abort && bp->b_prompt_interrupt.cb_partial != NULL)
- {
- tv.v_type = VAR_PARTIAL;
- tv.vval.v_partial = bp->b_prompt_interrupt.cb_partial;
- abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
- }
+ if (!abort)
+ abort = abort || set_ref_in_callback(&bp->b_prompt_callback, copyID);
+ if (!abort)
+ abort = abort || set_ref_in_callback(&bp->b_prompt_interrupt, copyID);
# endif
+#ifdef FEAT_COMPL_FUNC
+ if (!abort)
+ abort = abort || set_ref_in_callback(&bp->b_cfu_cb, copyID);
+ if (!abort)
+ abort = abort || set_ref_in_callback(&bp->b_ofu_cb, copyID);
+ if (!abort)
+ abort = abort || set_ref_in_callback(&bp->b_tsrfu_cb, copyID);
+#endif
+ if (!abort)
+ abort = abort || set_ref_in_callback(&bp->b_tfu_cb, copyID);
if (abort)
break;
}
diff --git a/src/evalvars.c b/src/evalvars.c
index 041025967a..a12b9bd23b 100644
--- a/src/evalvars.c
+++ b/src/evalvars.c
@@ -1395,7 +1395,6 @@ ex_let_option(
char_u *tofree = NULL;
char_u numbuf[NUMBUFLEN];
-
c1 = *p;
*p = NUL;
diff --git a/src/gui_xim.c b/src/gui_xim.c
index 6d72ef1d66..4bd1a2eedf 100644
--- a/src/gui_xim.c
+++ b/src/gui_xim.c
@@ -117,7 +117,7 @@ call_imstatusfunc(void)
void
free_xim_stuff(void)
{
-#if defined(FEAT_EVAL) && \
+# if defined(FEAT_EVAL) && \
(defined(FEAT_XIM) || defined(IME_WITHOUT_XIM) || defined(VIMDLL))
free_callback(&imaf_cb);
free_callback(&imsf_cb);
@@ -125,6 +125,24 @@ free_xim_stuff(void)
}
#endif
+/*
+ * Mark the global 'imactivatefunc' and 'imstatusfunc' callbacks with 'copyID'
+ * so that they are not garbage collected.
+ */
+ int
+set_ref_in_im_funcs(int copyID UNUSED)
+{
+ int abort = FALSE;
+
+#if defined(FEAT_EVAL) && \
+ (defined(FEAT_XIM) || defined(IME_WITHOUT_XIM) || defined(VIMDLL))
+ abort = set_ref_in_callback(&imaf_cb, copyID);
+ abort = abort || set_ref_in_callback(&imsf_cb, copyID);
+#endif
+
+ return abort;
+}
+
#if defined(FEAT_XIM) || defined(PROTO)
diff --git a/src/insexpand.c b/src/insexpand.c
index 8c09841f00..bc490b9648 100644
--- a/src/insexpand.c
+++ b/src/insexpand.c
@@ -2341,6 +2341,21 @@ set_thesaurusfunc_option(void)
return retval;
}
+/*
+ * Mark the global 'completefunc' 'omnifunc' and 'thesaurusfunc' callbacks with
+ * 'copyID' so that they are not garbage collected.
+ */
+ int
+set_ref_in_insexpand_funcs(int copyID)
+{
+ int abort = FALSE;
+
+ abort = set_ref_in_callback(&cfu_cb, copyID);
+ abort = abort || set_ref_in_callback(&ofu_cb, copyID);
+ abort = abort || set_ref_in_callback(&tsrfu_cb, copyID);
+
+ return abort;
+}
/*
* Get the user-defined completion function name for completion 'type'
diff --git a/src/ops.c b/src/ops.c
index a1afc258e1..f58d6f8d1d 100644
--- a/src/ops.c
+++ b/src/ops.c
@@ -3341,13 +3341,29 @@ set_operatorfunc_option(void)
void
free_operatorfunc_option(void)
{
-# ifdef FEAT_EVAL
+# ifdef FEAT_EVAL
free_callback(&opfunc_cb);
-# endif
+# endif
}
#endif
/*
+ * Mark the global 'operatorfunc' callback with 'copyID' so that it is not
+ * garbage collected.
+ */
+ int
+set_ref_in_opfunc(int copyID UNUSED)
+{
+ int abort = FALSE;
+
+#ifdef FEAT_EVAL
+ abort = set_ref_in_callback(&opfunc_cb, copyID);
+#endif
+
+ return abort;
+}
+
+/*
* Handle the "g@" operator: call 'operatorfunc'.
*/
static void
diff --git a/src/proto/eval.pro b/src/proto/eval.pro
index 45b559de52..dc9c53b516 100644
--- a/src/proto/eval.pro
+++ b/src/proto/eval.pro
@@ -52,6 +52,7 @@ int set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack);
int set_ref_in_dict(dict_T *d, int copyID);
int set_ref_in_list(list_T *ll, int copyID);
int set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack);
+int set_ref_in_callback(callback_T *cb, int copyID);
int set_ref_in_item(typval_T *tv, int copyID, ht_stack_T **ht_stack, list_stack_T **list_stack);
char_u *echo_string_core(typval_T *tv, char_u **tofree, char_u *numbuf, int copyID, int echo_style, int restore_copyID, int composite_val);
char_u *echo_string(typval_T *tv, char_u **tofree, char_u *numbuf, int copyID);
diff --git a/src/proto/gui_xim.pro b/src/proto/gui_xim.pro
index 60f30d7587..8c5c53a150 100644
--- a/src/proto/gui_xim.pro
+++ b/src/proto/gui_xim.pro
@@ -2,6 +2,7 @@
int set_imactivatefunc_option(void);
int set_imstatusfunc_option(void);
void free_xim_stuff(void);
+int set_ref_in_im_funcs(int copyID);
void im_set_active(int active);
void xim_set_focus(int focus);
void im_set_position(int row, int col);
diff --git a/src/proto/insexpand.pro b/src/proto/insexpand.pro
index d455a98fd7..6d975570d6 100644
--- a/src/proto/insexpand.pro
+++ b/src/proto/insexpand.pro
@@ -44,6 +44,7 @@ void set_buflocal_cfu_callback(buf_T *buf);
int set_omnifunc_option(void);
void set_buflocal_ofu_callback(buf_T *buf);
int set_thesaurusfunc_option(void);
+int set_ref_in_insexpand_funcs(int copyID);
callback_T *get_insert_callback(int type);
void f_complete(typval_T *argvars, typval_T *rettv);
void f_complete_add(typval_T *argvars, typval_T *rettv);
diff --git a/src/proto/ops.pro b/src/proto/ops.pro
index c46af05a04..1e89c4e99f 100644
--- a/src/proto/ops.pro
+++ b/src/proto/ops.pro
@@ -19,5 +19,6 @@ void clear_oparg(oparg_T *oap);
void cursor_pos_info(dict_T *dict);
int set_operatorfunc_option(void);
void free_operatorfunc_option(void);
+int set_ref_in_opfunc(int copyID);
void do_pending_operator(cmdarg_T *cap, int old_col, int gui_yank);
/* vim: set ft=c : */
diff --git a/src/proto/tag.pro b/src/proto/tag.pro
index c8ef72101e..9820571874 100644
--- a/src/proto/tag.pro
+++ b/src/proto/tag.pro
@@ -1,6 +1,7 @@
/* tag.c */
int set_tagfunc_option(void);
void free_tagfunc_option(void);
+int set_ref_in_tagfunc(int copyID);
void set_buflocal_tfu_callback(buf_T *buf);
int do_tag(char_u *tag, int type, int count, int forceit, int verbose);
void tag_freematch(void);
diff --git a/src/quickfix.c b/src/quickfix.c
index c7abc8ec4a..eeee8068d3 100644
--- a/src/quickfix.c
+++ b/src/quickfix.c
@@ -7675,7 +7675,8 @@ set_errorlist(
}
/*
- * Mark the context as in use for all the lists in a quickfix stack.
+ * Mark the quickfix context and callback function as in use for all the lists
+ * in a quickfix stack.
*/
static int
mark_quickfix_ctx(qf_info_T *qi, int copyID)
@@ -7683,13 +7684,17 @@ mark_quickfix_ctx(qf_info_T *qi, int copyID)
int i;
int abort = FALSE;
typval_T *ctx;
+ callback_T *cb;
for (i = 0; i < LISTCOUNT && !abort; ++i)
{
ctx = qi->qf_lists[i].qf_ctx;
if (ctx != NULL && ctx->v_type != VAR_NUMBER
&& ctx->v_type != VAR_STRING && ctx->v_type != VAR_FLOAT)
- abort = set_ref_in_item(ctx, copyID, NULL, NULL);
+ abort = abort || set_ref_in_item(ctx, copyID, NULL, NULL);
+
+ cb = &qi->qf_lists[i].qftf_cb;
+ abort = abort || set_ref_in_callback(cb, copyID);
}
return abort;
@@ -7710,6 +7715,10 @@ set_ref_in_quickfix(int copyID)
if (abort)
return abort;
+ abort = set_ref_in_callback(&qftf_cb, copyID);
+ if (abort)
+ return abort;
+
FOR_ALL_TAB_WINDOWS(tp, win)
{
if (win->w_llist != NULL)
diff --git a/src/tag.c b/src/tag.c
index 48a69d466c..85d1438881 100644
--- a/src/tag.c
+++ b/src/tag.c
@@ -133,15 +133,31 @@ set_tagfunc_option(void)
return OK;
}
-# if defined(EXITFREE) || defined(PROTO)
+#if defined(EXITFREE) || defined(PROTO)
void
free_tagfunc_option(void)
{
-# ifdef FEAT_EVAL
+# ifdef FEAT_EVAL
free_callback(&tfu_cb);
-# endif
-}
# endif
+}
+#endif
+
+/*
+ * Mark the global 'tagfunc' callback with 'copyID' so that it is not garbage
+ * collected.
+ */
+ int
+set_ref_in_tagfunc(int copyID UNUSED)
+{
+ int abort = FALSE;
+
+#ifdef FEAT_EVAL
+ abort = set_ref_in_callback(&tfu_cb, copyID);
+#endif
+
+ return abort;
+}
/*
* Copy the global 'tagfunc' callback function to the buffer-local 'tagfunc'
diff --git a/src/testdir/test_iminsert.vim b/src/testdir/test_iminsert.vim
index a53e6f36b4..860fb07ad3 100644
--- a/src/testdir/test_iminsert.vim
+++ b/src/testdir/test_iminsert.vim
@@ -119,83 +119,142 @@ func Test_imactivatefunc_imstatusfunc_callback()
let g:IMstatusfunc_called += 1
return 1
endfunc
- let g:IMactivatefunc_called = 0
- let g:IMstatusfunc_called = 0
set iminsert=2
- " Test for using a function()
- set imactivatefunc=function('IMactivatefunc1')
- set imstatusfunc=function('IMstatusfunc1')
- normal! i
+ let lines =<< trim END
+ LET g:IMactivatefunc_called = 0
+ LET g:IMstatusfunc_called = 0
- " Using a funcref variable to set 'completefunc'
- let Fn1 = function('IMactivatefunc1')
- let &imactivatefunc = Fn1
- let Fn2 = function('IMstatusfunc1')
- let &imstatusfunc = Fn2
- normal! i
+ #" Test for using a function()
+ set imactivatefunc=function('g:IMactivatefunc1')
+ set imstatusfunc=function('g:IMstatusfunc1')
+ normal! i
- " Using a string(funcref variable) to set 'completefunc'
- let &imactivatefunc = string(Fn1)
- let &imstatusfunc = string(Fn2)
- normal! i
+ #" Using a funcref variable to set 'completefunc'
+ VAR Fn1 = function('g:IMactivatefunc1')
+ LET &imactivatefunc = Fn1
+ VAR Fn2 = function('g:IMstatusfunc1')
+ LET &imstatusfunc = Fn2
+ normal! i
- " Test for using a funcref()
- set imactivatefunc=funcref('IMactivatefunc1')
- set imstatusfunc=funcref('IMstatusfunc1')
- normal! i
+ #" Using a string(funcref variable) to set 'completefunc'
+ LET &imactivatefunc = string(Fn1)
+ LET &imstatusfunc = string(Fn2)
+ normal! i
- " Using a funcref variable to set 'imactivatefunc'
- let Fn1 = funcref('IMactivatefunc1')
- let &imactivatefunc = Fn1
- let Fn2 = funcref('IMstatusfunc1')
- let &imstatusfunc = Fn2
- normal! i
+ #" Test for using a funcref()
+ set imactivatefunc=funcref('g:IMactivatefunc1')
+ set imstatusfunc=funcref('g:IMstatusfunc1')
+ normal! i
- " Using a string(funcref variable) to set 'imactivatefunc'
- let &imactivatefunc = string(Fn1)
- let &imstatusfunc = string(Fn2)
- normal! i
+ #" Using a funcref variable to set 'imactivatefunc'
+ LET Fn1 = funcref('g:IMactivatefunc1')
+ LET &imactivatefunc = Fn1
+ LET Fn2 = funcref('g:IMstatusfunc1')
+ LET &imstatusfunc = Fn2
+ normal! i
- " Test for using a lambda function
- set imactivatefunc={a\ ->\ IMactivatefunc1(a)}
- set imstatusfunc={\ ->\ IMstatusfunc1()}
- normal! i
+ #" Using a string(funcref variable) to set 'imactivatefunc'
+ LET &imactivatefunc = string(Fn1)
+ LET &imstatusfunc = string(Fn2)
+ normal! i
- " Set 'imactivatefunc' and 'imstatusfunc' to a lambda expression
- let &imactivatefunc = {a -> IMactivatefunc1(a)}
- let &imstatusfunc = { -> IMstatusfunc1()}
- normal! i
+ #" Test for using a lambda function
+ VAR optval = "LSTART a LMIDDLE IMactivatefunc1(a) LEND"
+ LET optval = substitute(optval, ' ', '\\ ', 'g')
+ exe "set imactivatefunc=" .. optval
+ LET optval = "LSTART LMIDDLE IMstatusfunc1() LEND"
+ LET optval = substitute(optval, ' ', '\\ ', 'g')
+ exe "set imstatusfunc=" .. optval
+ normal! i
- " Set 'imactivatefunc' and 'imstatusfunc' to a string(lambda expression)
- let &imactivatefunc = '{a -> IMactivatefunc1(a)}'
- let &imstatusfunc = '{ -> IMstatusfunc1()}'
- normal! i
+ #" Set 'imactivatefunc' and 'imstatusfunc' to a lambda expression
+ LET &imactivatefunc = LSTART a LMIDDLE IMactivatefunc1(a) LEND
+ LET &imstatusfunc = LSTART LMIDDLE IMstatusfunc1() LEND
+ normal! i
- " Set 'imactivatefunc' 'imstatusfunc' to a variable with a lambda expression
- let Lambda1 = {a -> IMactivatefunc1(a)}
- let Lambda2 = { -> IMstatusfunc1()}
- let &imactivatefunc = Lambda1
- let &imstatusfunc = Lambda2
- normal! i
+ #" Set 'imactivatefunc' and 'imstatusfunc' to a string(lambda expression)
+ LET &imactivatefunc = 'LSTART a LMIDDLE IMactivatefunc1(a) LEND'
+ LET &imstatusfunc = 'LSTART LMIDDLE IMstatusfunc1() LEND'
+ normal! i
- " Set 'imactivatefunc' 'imstatusfunc' to a string(variable with a lambda
- " expression)
- let &imactivatefunc = string(Lambda1)
- let &imstatusfunc = string(Lambda2)
- normal! i
+ #" Set 'imactivatefunc' 'imstatusfunc' to a variable with a lambda
+ #" expression
+ VAR Lambda1 = LSTART a LMIDDLE IMactivatefunc1(a) LEND
+ VAR Lambda2 = LSTART LMIDDLE IMstatusfunc1() LEND
+ LET &imactivatefunc = Lambda1
+ LET &imstatusfunc = Lambda2
+ normal! i
+
+ #" Set 'imactivatefunc' 'imstatusfunc' to a string(variable with a lambda
+ #" expression)
+ LET &imactivatefunc = string(Lambda1)
+ LET &imstatusfunc = string(Lambda2)
+ normal! i
- " Test for clearing the 'completefunc' option
- set imactivatefunc='' imstatusfunc=''
- set imactivatefunc& imstatusfunc&
+ #" Test for clearing the 'completefunc' option
+ set imactivatefunc='' imstatusfunc=''
+ set imactivatefunc& imstatusfunc&
+
+ set imactivatefunc=g:IMactivatefunc1
+ set imstatusfunc=g:IMstatusfunc1
+ call assert_fails("set imactivatefunc=function('abc')", "E700:")
+ call assert_fails("set imstatusfunc=function('abc')", "E700:")
+ call assert_fails("set imactivatefunc=funcref('abc')", "E700:")
+ call assert_fails("set imstatusfunc=funcref('abc')", "E700:")
+ call assert_fails("LET &imstatusfunc = function('abc')", "E700:")
+ call assert_fails("LET &imactivatefunc = function('abc')", "E700:")
+ normal! i
+
+ #" set 'imactivatefunc' and 'imstatusfunc' to a non-existing function
+ set imactivatefunc=IMactivatefunc1
+ set imstatusfunc=IMstatusfunc1
+ call assert_fails("set imactivatefunc=function('NonExistingFunc')", 'E700:')
+ call assert_fails("set imstatusfunc=function('NonExistingFunc')", 'E700:')
+ call assert_fails("LET &imactivatefunc = function('NonExistingFunc')", 'E700:')
+ call assert_fails("LET &imstatusfunc = function('NonExistingFunc')", 'E700:')
+ normal! i
+
+ call assert_equal(13, g:IMactivatefunc_called)
+ call assert_equal(26, g:IMstatusfunc_called)
+ END
+ call CheckLegacyAndVim9Success(lines)
- call assert_fails("set imactivatefunc=function('abc')", "E700:")
- call assert_fails("set imstatusfunc=function('abc')", "E700:")
- call assert_fails("set imactivatefunc=funcref('abc')", "E700:")
- call assert_fails("set imstatusfunc=funcref('abc')", "E700:")
+ " Using Vim9 lambda expression in legacy context should fail
+ set imactivatefunc=(a)\ =>\ IMactivatefunc1(a)
+ set imstatusfunc=IMstatusfunc1
+ call assert_fails('normal! i', 'E117:')
+ set imactivatefunc=IMactivatefunc1
+ set imstatusfunc=()\ =>\ IMstatusfunc1(a)
+ call assert_fails('normal! i', 'E117:')
- call assert_equal(11, g:IMactivatefunc_called)
- call assert_equal(22, g:IMstatusfunc_called)
+ " set 'imactivatefunc' and 'imstatusfunc' to a partial with dict. This used
+ " to cause a crash.
+ func SetIMFunc()
+ let params1 = {'activate': function('g:DictActivateFunc')}
+ let params2 = {'status': function('g:DictStatusFunc')}
+ let &imactivatefunc = params1.activate
+ let &imstatusfunc = params2.status
+ endfunc
+ func g:DictActivateFunc(_) dict
+ endfunc
+ func g:DictStatusFunc(_) dict
+ endfunc
+ call SetIMFunc()
+ new
+ call SetIMFunc()
+ bw
+ call test_garbagecollect_now()
+ new
+ set imactivatefunc=
+ set imstatusfunc=
+ wincmd w
+ set imactivatefunc=
+ set imstatusfunc=
+ :%bw!
+ delfunc g:DictActivateFunc
+ delfunc g:DictStatusFunc
+ delfunc SetIMFunc
" Vim9 tests
let lines =<< trim END
@@ -217,59 +276,12 @@ func Test_imactivatefunc_imstatusfunc_callback()
set imstatusfunc=function('IMstatusfunc1')
normal! i
- # Test for using a lambda
- &imactivatefunc = '(a) => IMactivatefunc1(a)'
- &imstatusfunc = '() => IMstatusfunc1()'
- normal! i
-
- # Test for using a variable with a lambda expression
- var Fn1: func = (active) => {
- g:IMactivatefunc_called += 1
- return 1
- }
- var Fn2: func = () => {
- g:IMstatusfunc_called += 1
- return 1
- }
- &imactivatefunc = Fn1
- &imstatusfunc = Fn2
- normal! i
-
- # Test for using a string(variable with a lambda expression)
- &imactivatefunc = string(Fn1)
- &imstatusfunc = string(Fn2)
- normal! i
-
- assert_equal(4, g:IMactivatefunc_called)
- assert_equal(8, g:IMstatusfunc_called)
-
set iminsert=0
set imactivatefunc=
set imstatusfunc=
END
call CheckScriptSuccess(lines)
- " Using Vim9 lambda expression in legacy context should fail
- set imactivatefunc=(a)\ =>\ IMactivatefunc1(a)
- set imstatusfunc=IMstatusfunc1
- call assert_fails('normal! i', 'E117:')
- set imactivatefunc=IMactivatefunc1
- set imstatusfunc=()\ =>\ IMstatusfunc1(a)
- call assert_fails('normal! i', 'E117:')
-
- " set 'imactivatefunc' and 'imstatusfunc' to a non-existing function
- set imactivatefunc=IMactivatefunc1
- set imstatusfunc=IMstatusfunc1
- call assert_fails("set imactivatefunc=function('NonExistingFunc')", 'E700:')
- call assert_fails("set imstatusfunc=function('NonExistingFunc')", 'E700:')
- call assert_fails("let &imactivatefunc = function('NonExistingFunc')", 'E700:')
- call assert_fails("let &imstatusfunc = function('NonExistingFunc')", 'E700:')
- let g:IMactivatefunc_called = 0
- let g:IMstatusfunc_called = 0
- normal! i
- call assert_equal(2, g:IMactivatefunc_called)
- call assert_equal(2, g:IMstatusfunc_called)
-
" cleanup
delfunc IMactivatefunc1
delfunc IMstatusfunc1
diff --git a/src/testdir/test_ins_complete.vim b/src/testdir/test_ins_complete.vim
index 6a99ecd3eb..1303a13545 100644
--- a/src/testdir/test_ins_complete.vim
+++ b/src/testdir/test_ins_complete.vim
@@ -875,154 +875,179 @@ func Test_completefunc_callback()
return a:findstart ? 0 : []
endfunc
- " Test for using a function()
- set completefunc=function('MycompleteFunc1',[10])
- new | only
- call setline(1, 'one')
- let g:MycompleteFunc1_args = []
- call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
- call assert_equal([[10, 1, ''], [10, 0, 'one']], g:MycompleteFunc1_args)
- bw!
+ let lines =<< trim END
+ #" Test for using a function()
+ set completefunc=function('g:MycompleteFunc1',\ [10])
+ new | only
+ call setline(1, 'one')
+ LET g:MycompleteFunc1_args = []
+ call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
+ call assert_equal([[10, 1, ''], [10, 0, 'one']], g:MycompleteFunc1_args)
+ bw!
- " Using a funcref variable to set 'completefunc'
- let Fn = function('MycompleteFunc1', [11])
- let &completefunc = Fn
- new | only
- call setline(1, 'two')
- let g:MycompleteFunc1_args = []
- call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
- call assert_equal([[11, 1, ''], [11, 0, 'two']], g:MycompleteFunc1_args)
- bw!
+ #" Using a funcref variable to set 'completefunc'
+ VAR Fn = function('g:MycompleteFunc1', [11])
+ LET &completefunc = Fn
+ new | only
+ call setline(1, 'two')
+ LET g:MycompleteFunc1_args = []
+ call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
+ call assert_equal([[11, 1, ''], [11, 0, 'two']], g:MycompleteFunc1_args)
+ bw!
- " Using string(funcref_variable) to set 'completefunc'
- let Fn = function('MycompleteFunc1', [12])
- let &completefunc = string(Fn)
- new | only
- call setline(1, 'two')
- let g:MycompleteFunc1_args = []
- call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
- call assert_equal([[12, 1, ''], [12, 0, 'two']], g:MycompleteFunc1_args)
- bw!
+ #" Using string(funcref_variable) to set 'completefunc'
+ LET Fn = function('g:MycompleteFunc1', [12])
+ LET &completefunc = string(Fn)
+ new | only
+ call setline(1, 'two')
+ LET g:MycompleteFunc1_args = []
+ call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
+ call assert_equal([[12, 1, ''], [12, 0, 'two']], g:MycompleteFunc1_args)
+ bw!
- " Test for using a funcref()
- set completefunc=funcref('MycompleteFunc1',\ [13])
- new | only
- call setline(1, 'three')
- let g:MycompleteFunc1_args = []
- call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
- call assert_equal([[13, 1, ''], [13, 0, 'three']], g:MycompleteFunc1_args)
- bw!
+ #" Test for using a funcref()
+ set completefunc=funcref('g:MycompleteFunc1',\ [13])
+ new | only
+ call setline(1, 'three')
+ LET g:MycompleteFunc1_args = []
+ call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
+ call assert_equal([[13, 1, ''], [13, 0, 'three']], g:MycompleteFunc1_args)
+ bw!
- " Using a funcref variable to set 'completefunc'
- let Fn = funcref('MycompleteFunc1', [14])
- let &completefunc = Fn
- new | only
- call setline(1, 'four')
- let g:MycompleteFunc1_args = []
- call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
- call assert_equal([[14, 1, ''], [14, 0, 'four']], g:MycompleteFunc1_args)
- bw!
+ #" Using a funcref variable to set 'completefunc'
+ LET Fn = funcref('g:MycompleteFunc1', [14])
+ LET &completefunc = Fn
+ new | only
+ call setline(1, 'four')
+ LET g:MycompleteFunc1_args = []
+ call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
+ call assert_equal([[14, 1, ''], [14, 0, 'four']], g:MycompleteFunc1_args)
+ bw!
- " Using a string(funcref_variable) to set 'completefunc'
- let Fn = funcref('MycompleteFunc1', [15])
- let &completefunc = string(Fn)
- new | only
- call setline(1, 'four')
- let g:MycompleteFunc1_args = []
- call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
- call assert_equal([[15, 1, ''], [15, 0, 'four']], g:MycompleteFunc1_args)
- bw!
+ #" Using a string(funcref_variable) to set 'completefunc'
+ LET Fn = funcref('g:MycompleteFunc1', [15])
+ LET &completefunc = string(Fn)
+ new | only
+ call setline(1, 'four')
+ LET g:MycompleteFunc1_args = []
+ call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
+ call assert_equal([[15, 1, ''], [15, 0, 'four']], g:MycompleteFunc1_args)
+ bw!
- " Test for using a lambda function
- set completefunc={a,\ b\ ->\ MycompleteFunc1(16,\ a,\ b)}
- new | only
- call setline(1, 'five')
- let g:MycompleteFunc1_args = []
- call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
- call assert_equal([[16, 1, ''], [16, 0, 'five']], g:MycompleteFunc1_args)
- bw!
+ #" Test for using a lambda function with set
+ VAR optval = "LSTART a, b LMIDDLE MycompleteFunc1(16, a, b) LEND"
+ LET optval = substitute(optval, ' ', '\\ ', 'g')
+ exe "set completefunc=" .. optval
+ new | only
+ call setline(1, 'five')
+ LET g:MycompleteFunc1_args = []
+ call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
+ call assert_equal([[16, 1, ''], [16, 0, 'five']], g:MycompleteFunc1_args)
+ bw!
- " Set 'completefunc' to a lambda expression
- let &completefunc = {a, b -> MycompleteFunc1(17, a, b)}
- new | only
- call setline(1, 'six')
- let g:MycompleteFunc1_args = []
- call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
- call assert_equal([[17, 1, ''], [17, 0, 'six']], g:MycompleteFunc1_args)
- bw!
+ #" Set 'completefunc' to a lambda expression
+ LET &completefunc = LSTART a, b LMIDDLE MycompleteFunc1(17, a, b) LEND
+ new | only
+ call setline(1, 'six')
+ LET g:MycompleteFunc1_args = []
+ call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
+ call assert_equal([[17, 1, ''], [17, 0, 'six']], g:MycompleteFunc1_args)
+ bw!
- " Set 'completefunc' to string(lambda_expression)
- let &completefunc = '{a, b -> MycompleteFunc1(18, a, b)}'
- new | only
- call setline(1, 'six')
- let g:MycompleteFunc1_args = []
- call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
- call assert_equal([[18, 1, ''], [18, 0, 'six']], g:MycompleteFunc1_args)
- bw!
+ #" Set 'completefunc' to string(lambda_expression)
+ LET &completefunc = 'LSTART a, b LMIDDLE MycompleteFunc1(18, a, b) LEND'
+ new | only
+ call setline(1, 'six')
+ LET g:MycompleteFunc1_args = []
+ call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
+ call assert_equal([[18, 1, ''], [18, 0, 'six']], g:MycompleteFunc1_args)
+ bw!
- " Set 'completefunc' to a variable with a lambda expression
- let Lambda = {a, b -> MycompleteFunc1(19, a, b)}
- let &completefunc = Lambda
- new | only
- call setline(1, 'seven')
- let g:MycompleteFunc1_args = []
- call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
- call assert_equal([[19, 1, ''], [19, 0, 'seven']], g:MycompleteFunc1_args)
- bw!
+ #" Set 'completefunc' to a variable with a lambda expression
+ VAR Lambda = LSTART a, b LMIDDLE MycompleteFunc1(19, a, b) LEND
+ LET &completefunc = Lambda
+ new | only
+ call setline(1, 'seven')
+ LET g:MycompleteFunc1_args = []
+ call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
+ call assert_equal([[19, 1, ''], [19, 0, 'seven']], g:MycompleteFunc1_args)
+ bw!
- " Set 'completefunc' to a string(variable with a lambda expression)
- let Lambda = {a, b -> MycompleteFunc1(20, a, b)}
- let &completefunc = string(Lambda)
- new | only
- call setline(1, 'seven')
- let g:MycompleteFunc1_args = []
- call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
- call assert_equal([[20, 1, ''], [20, 0, 'seven']], g:MycompleteFunc1_args)
- bw!
+ #" Set 'completefunc' to a string(variable with a lambda expression)
+ LET Lambda = LSTART a, b LMIDDLE MycompleteFunc1(20, a, b) LEND
+ LET &completefunc = string(Lambda)
+ new | only
+ call setline(1, 'seven')
+ LET g:MycompleteFunc1_args = []
+ call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
+ call assert_equal([[20, 1, ''], [20, 0, 'seven']], g:MycompleteFunc1_args)
+ bw!
- " Test for using a lambda function with incorrect return value
- let Lambda = {s -> strlen(s)}
- let &completefunc = Lambda
- new | only
- call setline(1, 'eight')
- call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
- bw!
+ #" Test for using a lambda function with incorrect return value
+ LET Lambda = LSTART a, b LMIDDLE strlen(a) LEND
+ LET &completefunc = Lambda
+ new | only
+ call setline(1, 'eight')
+ call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
+ bw!
- " Test for clearing the 'completefunc' option
- set completefunc=''
- set completefunc&
+ #" Test for clearing the 'completefunc' option
+ set completefunc=''
+ set completefunc&
+ call assert_fails("set completefunc=function('abc')", "E700:")
+ call assert_fails("set completefunc=funcref('abc')", "E700:")
+
+ #" set 'completefunc' to a non-existing function
+ func MycompleteFunc2(findstart, base)
+ call add(g:MycompleteFunc2_args, [a:findstart, a:base])
+ return a:findstart ? 0 : []
+ endfunc
+ set completefunc=MycompleteFunc2
+ call setline(1, 'five')
+ call assert_fails("set completefunc=function('NonExistingFunc')", 'E700:')
+ call assert_fails("LET &completefunc = function('NonExistingFunc')", 'E700:')
+ LET g:MycompleteFunc2_args = []
+ call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
+ call assert_equal([[1, ''], [0, 'five']], g:MycompleteFunc2_args)
+ bw!
+ END
+ call CheckLegacyAndVim9Succ