diff src/usercmd.c @ 25382:b80e4e9c4988 v8.2.3228

patch 8.2.3228: cannot use a simple block for the :command argument Commit: https://github.com/vim/vim/commit/5d7c2df536c17db4a9c61e0760bdcf78d0db7330 Author: Bram Moolenaar <Bram@vim.org> Date: Tue Jul 27 21:17:32 2021 +0200 patch 8.2.3228: cannot use a simple block for the :command argument Problem: Cannot use a simple block for the :command argument. (Maarten Tournoij) Solution: Recognize a simple {} block. (issue #8623)
author Bram Moolenaar <Bram@vim.org>
date Tue, 27 Jul 2021 21:30:08 +0200
parents a9ea83a3659a
children 05f9e8f2016c
line wrap: on
line diff
--- a/src/usercmd.c
+++ b/src/usercmd.c
@@ -115,6 +115,7 @@ static struct
 };
 
 #define UC_BUFFER	1	// -buffer: local to current buffer
+#define UC_VIM9		2	// {} argument: Vim9 syntax.
 
 /*
  * Search for a user command that matches "eap->cmd".
@@ -872,10 +873,10 @@ uc_add_command(
     replace_termcodes(rep, &rep_buf, 0, NULL);
     if (rep_buf == NULL)
     {
-	// Can't replace termcodes - try using the string as is
+	// can't replace termcodes - try using the string as is
 	rep_buf = vim_strsave(rep);
 
-	// Give up if out of memory
+	// give up if out of memory
 	if (rep_buf == NULL)
 	    return FAIL;
     }
@@ -955,6 +956,8 @@ uc_add_command(
     cmd->uc_def = def;
     cmd->uc_compl = compl;
     cmd->uc_script_ctx = current_sctx;
+    if (flags & UC_VIM9)
+	cmd->uc_script_ctx.sc_version = SCRIPT_VERSION_VIM9;
 #ifdef FEAT_EVAL
     cmd->uc_script_ctx.sc_lnum += SOURCING_LNUM;
     cmd->uc_compl_arg = compl_arg;
@@ -1037,8 +1040,46 @@ ex_command(exarg_T *eap)
 		       (char_u *)_(e_complete_used_without_nargs), TRUE, TRUE);
     }
     else
+    {
+	char_u *tofree = NULL;
+
+	if (*p == '{' && ends_excmd2(eap->arg, skipwhite(p + 1))
+						       && eap->getline != NULL)
+	{
+	    garray_T	ga;
+	    char_u	*line = NULL;
+
+	    ga_init2(&ga, sizeof(char_u *), 10);
+	    if (ga_add_string(&ga, p) == FAIL)
+		return;
+
+	    // Read lines between '{' and '}'.  Does not support nesting or
+	    // here-doc constructs.
+	    //
+	    for (;;)
+	    {
+		vim_free(line);
+		if ((line = eap->getline(':', eap->cookie,
+					   0, GETLINE_CONCAT_CONTBAR)) == NULL)
+		{
+		    emsg(_(e_missing_rcurly));
+		    break;
+		}
+		if (ga_add_string(&ga, line) == FAIL)
+		    break;
+		if (*skipwhite(line) == '}')
+		    break;
+	    }
+	    vim_free(line);
+	    p = tofree = ga_concat_strings(&ga, "\n");
+	    ga_clear_strings(&ga);
+	    flags |= UC_VIM9;
+	}
+
 	uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
 						  addr_type_arg, eap->forceit);
+	vim_free(tofree);
+    }
 }
 
 /*