diff src/evalvars.c @ 24490:08050e45bd06 v8.2.2785

patch 8.2.2785: Vim9: cannot redirect to local variable Commit: https://github.com/vim/vim/commit/2d1c57ed3dd25c44b41b9ddd4cf63c01ae89007e Author: Bram Moolenaar <Bram@vim.org> Date: Mon Apr 19 20:50:03 2021 +0200 patch 8.2.2785: Vim9: cannot redirect to local variable Problem: Vim9: cannot redirect to local variable. Solution: Compile :redir when redirecting to a variable.
author Bram Moolenaar <Bram@vim.org>
date Mon, 19 Apr 2021 21:00:04 +0200
parents 3d5a66e478f8
children a4fda40e0bb9
line wrap: on
line diff
--- a/src/evalvars.c
+++ b/src/evalvars.c
@@ -3778,6 +3778,27 @@ static garray_T redir_ga;	// only valid 
 static char_u	*redir_endp = NULL;
 static char_u	*redir_varname = NULL;
 
+    int
+alloc_redir_lval(void)
+{
+    redir_lval = ALLOC_CLEAR_ONE(lval_T);
+    if (redir_lval == NULL)
+	return FAIL;
+    return OK;
+}
+
+    void
+clear_redir_lval(void)
+{
+    VIM_CLEAR(redir_lval);
+}
+
+    void
+init_redir_ga(void)
+{
+    ga_init2(&redir_ga, (int)sizeof(char), 500);
+}
+
 /*
  * Start recording command output to a variable
  * When "append" is TRUE append to an existing variable.
@@ -3801,15 +3822,14 @@ var_redir_start(char_u *name, int append
     if (redir_varname == NULL)
 	return FAIL;
 
-    redir_lval = ALLOC_CLEAR_ONE(lval_T);
-    if (redir_lval == NULL)
+    if (alloc_redir_lval() == FAIL)
     {
 	var_redir_stop();
 	return FAIL;
     }
 
     // The output is stored in growarray "redir_ga" until redirection ends.
-    ga_init2(&redir_ga, (int)sizeof(char), 500);
+    init_redir_ga();
 
     // Parse the variable name (can be a dict or list entry).
     redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
@@ -3922,6 +3942,20 @@ var_redir_stop(void)
 }
 
 /*
+ * Get the collected redirected text and clear redir_ga.
+ */
+    char_u *
+get_clear_redir_ga(void)
+{
+    char_u *res;
+
+    ga_append(&redir_ga, NUL);  // Append the trailing NUL.
+    res = redir_ga.ga_data;
+    redir_ga.ga_data = NULL;
+    return res;
+}
+
+/*
  * "gettabvar()" function
  */
     void