From 07ea5f1509fe8dafe3262ed2702b4d0fc99e288b Mon Sep 17 00:00:00 2001 From: Shougo Matsushita Date: Sat, 27 Aug 2022 12:22:25 +0100 Subject: patch 9.0.0285: it is not easy to change the command line from a plugin Problem: It is not easy to change the command line from a plugin. Solution: Add setcmdline(). (Shougo Matsushita, closes #10869) --- src/ex_getln.c | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) (limited to 'src/ex_getln.c') 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 */ -- cgit v1.2.3