summaryrefslogtreecommitdiffstats
path: root/src/ex_getln.c
diff options
context:
space:
mode:
authorShougo Matsushita <Shougo.Matsu@gmail.com>2022-08-27 12:22:25 +0100
committerBram Moolenaar <Bram@vim.org>2022-08-27 12:22:25 +0100
commit07ea5f1509fe8dafe3262ed2702b4d0fc99e288b (patch)
tree1b409138b5a115619cb2852ece07194edca7ed51 /src/ex_getln.c
parent5ff595d9db2d9a33aa10cc9f18f256826226862f (diff)
patch 9.0.0285: it is not easy to change the command line from a pluginv9.0.0285
Problem: It is not easy to change the command line from a plugin. Solution: Add setcmdline(). (Shougo Matsushita, closes #10869)
Diffstat (limited to 'src/ex_getln.c')
-rw-r--r--src/ex_getln.c58
1 files changed, 58 insertions, 0 deletions
diff --git a/src/ex_getln.c b/src/ex_getln.c
index 2ef66abb19..587a9ff3df 100644
--- a/src/ex_getln.c
+++ b/src/ex_getln.c
@@ -4211,6 +4211,35 @@ f_getcmdscreenpos(typval_T *argvars UNUSED, typval_T *rettv)
rettv->vval.v_number = get_cmdline_screen_pos() + 1;
}
+// Set the command line str to "str".
+// Returns 1 when failed, 0 when OK.
+ int
+set_cmdline_str(char_u *str, int pos)
+{
+ cmdline_info_T *p = get_ccline_ptr();
+ int cmdline_type;
+ int len;
+
+ if (p == NULL)
+ return 1;
+
+ len = (int)STRLEN(str);
+ realloc_cmdbuff(len + 1);
+ p->cmdlen = len;
+ STRCPY(p->cmdbuff, str);
+
+ p->cmdpos = pos < 0 || pos > p->cmdlen ? p->cmdlen : pos;
+ new_cmdpos = p->cmdpos;
+
+ redrawcmd();
+
+ // Trigger CmdlineChanged autocommands.
+ cmdline_type = ccline.cmdfirstc == NUL ? '-' : ccline.cmdfirstc;
+ trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINECHANGED);
+
+ return 0;
+}
+
/*
* Set the command line byte position to "pos". Zero is the first position.
* Only works when the command line is being edited.
@@ -4234,6 +4263,35 @@ set_cmdline_pos(
return 0;
}
+// "setcmdline()" function
+ void
+f_setcmdline(typval_T *argvars, typval_T *rettv)
+{
+ int pos = -1;
+
+ if (argvars[0].v_type != VAR_STRING || argvars[0].vval.v_string == NULL)
+ {
+ emsg(_(e_string_required));
+ return;
+ }
+
+ if (argvars[1].v_type != VAR_UNKNOWN)
+ {
+ int error = FALSE;
+
+ pos = (int)tv_get_number_chk(&argvars[1], &error) - 1;
+ if (error)
+ return;
+ if (pos < 0)
+ {
+ emsg(_(e_argument_must_be_positive));
+ return;
+ }
+ }
+
+ rettv->vval.v_number = set_cmdline_str(argvars[0].vval.v_string, pos);
+}
+
/*
* "setcmdpos()" function
*/