diff src/ex_getln.c @ 17922:4d63d47d87ef v8.1.1957

patch 8.1.1957: more code can be moved to evalvars.c Commit: https://github.com/vim/vim/commit/da6c03342117fb7f4a8110bd9e8627b612a05a64 Author: Bram Moolenaar <Bram@vim.org> Date: Sun Sep 1 16:01:30 2019 +0200 patch 8.1.1957: more code can be moved to evalvars.c Problem: More code can be moved to evalvars.c. Solution: Move code to where it fits better. (Yegappan Lakshmanan, closes #4883)
author Bram Moolenaar <Bram@vim.org>
date Sun, 01 Sep 2019 16:15:03 +0200
parents 59f8948b7590
children 684a15da9929
line wrap: on
line diff
--- a/src/ex_getln.c
+++ b/src/ex_getln.c
@@ -4317,3 +4317,111 @@ script_get(exarg_T *eap, char_u *cmd)
 
     return (char_u *)ga.ga_data;
 }
+
+#if defined(FEAT_EVAL) || defined(PROTO)
+/*
+ * This function is used by f_input() and f_inputdialog() functions. The third
+ * argument to f_input() specifies the type of completion to use at the
+ * prompt. The third argument to f_inputdialog() specifies the value to return
+ * when the user cancels the prompt.
+ */
+    void
+get_user_input(
+    typval_T	*argvars,
+    typval_T	*rettv,
+    int		inputdialog,
+    int		secret)
+{
+    char_u	*prompt = tv_get_string_chk(&argvars[0]);
+    char_u	*p = NULL;
+    int		c;
+    char_u	buf[NUMBUFLEN];
+    int		cmd_silent_save = cmd_silent;
+    char_u	*defstr = (char_u *)"";
+    int		xp_type = EXPAND_NOTHING;
+    char_u	*xp_arg = NULL;
+
+    rettv->v_type = VAR_STRING;
+    rettv->vval.v_string = NULL;
+
+#ifdef NO_CONSOLE_INPUT
+    // While starting up, there is no place to enter text. When running tests
+    // with --not-a-term we assume feedkeys() will be used.
+    if (no_console_input() && !is_not_a_term())
+	return;
+#endif
+
+    cmd_silent = FALSE;		// Want to see the prompt.
+    if (prompt != NULL)
+    {
+	// Only the part of the message after the last NL is considered as
+	// prompt for the command line
+	p = vim_strrchr(prompt, '\n');
+	if (p == NULL)
+	    p = prompt;
+	else
+	{
+	    ++p;
+	    c = *p;
+	    *p = NUL;
+	    msg_start();
+	    msg_clr_eos();
+	    msg_puts_attr((char *)prompt, get_echo_attr());
+	    msg_didout = FALSE;
+	    msg_starthere();
+	    *p = c;
+	}
+	cmdline_row = msg_row;
+
+	if (argvars[1].v_type != VAR_UNKNOWN)
+	{
+	    defstr = tv_get_string_buf_chk(&argvars[1], buf);
+	    if (defstr != NULL)
+		stuffReadbuffSpec(defstr);
+
+	    if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
+	    {
+		char_u	*xp_name;
+		int	xp_namelen;
+		long	argt;
+
+		// input() with a third argument: completion
+		rettv->vval.v_string = NULL;
+
+		xp_name = tv_get_string_buf_chk(&argvars[2], buf);
+		if (xp_name == NULL)
+		    return;
+
+		xp_namelen = (int)STRLEN(xp_name);
+
+		if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
+							     &xp_arg) == FAIL)
+		    return;
+	    }
+	}
+
+	if (defstr != NULL)
+	{
+	    int save_ex_normal_busy = ex_normal_busy;
+
+	    ex_normal_busy = 0;
+	    rettv->vval.v_string =
+		getcmdline_prompt(secret ? NUL : '@', p, get_echo_attr(),
+							      xp_type, xp_arg);
+	    ex_normal_busy = save_ex_normal_busy;
+	}
+	if (inputdialog && rettv->vval.v_string == NULL
+		&& argvars[1].v_type != VAR_UNKNOWN
+		&& argvars[2].v_type != VAR_UNKNOWN)
+	    rettv->vval.v_string = vim_strsave(tv_get_string_buf(
+							   &argvars[2], buf));
+
+	vim_free(xp_arg);
+
+	// since the user typed this, no need to wait for return
+	need_wait_return = FALSE;
+	msg_didout = FALSE;
+    }
+    cmd_silent = cmd_silent_save;
+}
+#endif