Skip to content

Commit da6c033

Browse files
committed
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)
1 parent 0fdddee commit da6c033

13 files changed

Lines changed: 604 additions & 539 deletions

File tree

src/eval.c

Lines changed: 15 additions & 517 deletions
Large diffs are not rendered by default.

src/evalvars.c

Lines changed: 370 additions & 1 deletion
Large diffs are not rendered by default.

src/ex_getln.c

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4317,3 +4317,111 @@ script_get(exarg_T *eap, char_u *cmd)
43174317

43184318
return (char_u *)ga.ga_data;
43194319
}
4320+
4321+
#if defined(FEAT_EVAL) || defined(PROTO)
4322+
/*
4323+
* This function is used by f_input() and f_inputdialog() functions. The third
4324+
* argument to f_input() specifies the type of completion to use at the
4325+
* prompt. The third argument to f_inputdialog() specifies the value to return
4326+
* when the user cancels the prompt.
4327+
*/
4328+
void
4329+
get_user_input(
4330+
typval_T *argvars,
4331+
typval_T *rettv,
4332+
int inputdialog,
4333+
int secret)
4334+
{
4335+
char_u *prompt = tv_get_string_chk(&argvars[0]);
4336+
char_u *p = NULL;
4337+
int c;
4338+
char_u buf[NUMBUFLEN];
4339+
int cmd_silent_save = cmd_silent;
4340+
char_u *defstr = (char_u *)"";
4341+
int xp_type = EXPAND_NOTHING;
4342+
char_u *xp_arg = NULL;
4343+
4344+
rettv->v_type = VAR_STRING;
4345+
rettv->vval.v_string = NULL;
4346+
4347+
#ifdef NO_CONSOLE_INPUT
4348+
// While starting up, there is no place to enter text. When running tests
4349+
// with --not-a-term we assume feedkeys() will be used.
4350+
if (no_console_input() && !is_not_a_term())
4351+
return;
4352+
#endif
4353+
4354+
cmd_silent = FALSE; // Want to see the prompt.
4355+
if (prompt != NULL)
4356+
{
4357+
// Only the part of the message after the last NL is considered as
4358+
// prompt for the command line
4359+
p = vim_strrchr(prompt, '\n');
4360+
if (p == NULL)
4361+
p = prompt;
4362+
else
4363+
{
4364+
++p;
4365+
c = *p;
4366+
*p = NUL;
4367+
msg_start();
4368+
msg_clr_eos();
4369+
msg_puts_attr((char *)prompt, get_echo_attr());
4370+
msg_didout = FALSE;
4371+
msg_starthere();
4372+
*p = c;
4373+
}
4374+
cmdline_row = msg_row;
4375+
4376+
if (argvars[1].v_type != VAR_UNKNOWN)
4377+
{
4378+
defstr = tv_get_string_buf_chk(&argvars[1], buf);
4379+
if (defstr != NULL)
4380+
stuffReadbuffSpec(defstr);
4381+
4382+
if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
4383+
{
4384+
char_u *xp_name;
4385+
int xp_namelen;
4386+
long argt;
4387+
4388+
// input() with a third argument: completion
4389+
rettv->vval.v_string = NULL;
4390+
4391+
xp_name = tv_get_string_buf_chk(&argvars[2], buf);
4392+
if (xp_name == NULL)
4393+
return;
4394+
4395+
xp_namelen = (int)STRLEN(xp_name);
4396+
4397+
if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
4398+
&xp_arg) == FAIL)
4399+
return;
4400+
}
4401+
}
4402+
4403+
if (defstr != NULL)
4404+
{
4405+
int save_ex_normal_busy = ex_normal_busy;
4406+
4407+
ex_normal_busy = 0;
4408+
rettv->vval.v_string =
4409+
getcmdline_prompt(secret ? NUL : '@', p, get_echo_attr(),
4410+
xp_type, xp_arg);
4411+
ex_normal_busy = save_ex_normal_busy;
4412+
}
4413+
if (inputdialog && rettv->vval.v_string == NULL
4414+
&& argvars[1].v_type != VAR_UNKNOWN
4415+
&& argvars[2].v_type != VAR_UNKNOWN)
4416+
rettv->vval.v_string = vim_strsave(tv_get_string_buf(
4417+
&argvars[2], buf));
4418+
4419+
vim_free(xp_arg);
4420+
4421+
// since the user typed this, no need to wait for return
4422+
need_wait_return = FALSE;
4423+
msg_didout = FALSE;
4424+
}
4425+
cmd_silent = cmd_silent_save;
4426+
}
4427+
#endif

src/globals.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,9 +191,6 @@ EXTERN int emsg_skip INIT(= 0); // don't display errors for
191191
EXTERN int emsg_severe INIT(= FALSE); // use message of next of several
192192
// emsg() calls for throw
193193
EXTERN int did_endif INIT(= FALSE); // just had ":endif"
194-
EXTERN dict_T vimvardict; // Dictionary with v: variables
195-
EXTERN dict_T globvardict; // Dictionary with g: variables
196-
#define globvarht globvardict.dv_hashtab
197194
#endif
198195
EXTERN int did_emsg; // set by emsg() when the message
199196
// is displayed or thrown

src/if_py_both.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6847,8 +6847,8 @@ populate_module(PyObject *m)
68476847
return -1;
68486848
ADD_OBJECT(m, "error", VimError);
68496849

6850-
ADD_CHECKED_OBJECT(m, "vars", NEW_DICTIONARY(&globvardict));
6851-
ADD_CHECKED_OBJECT(m, "vvars", NEW_DICTIONARY(&vimvardict));
6850+
ADD_CHECKED_OBJECT(m, "vars", NEW_DICTIONARY(get_globvar_dict()));
6851+
ADD_CHECKED_OBJECT(m, "vvars", NEW_DICTIONARY(get_vimvar_dict()));
68526852
ADD_CHECKED_OBJECT(m, "options",
68536853
OptionsNew(SREQ_GLOBAL, NULL, dummy_check, NULL));
68546854

src/proto/eval.pro

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,6 @@ varnumber_T num_divide(varnumber_T n1, varnumber_T n2);
33
varnumber_T num_modulus(varnumber_T n1, varnumber_T n2);
44
void eval_init(void);
55
void eval_clear(void);
6-
int var_redir_start(char_u *name, int append);
7-
void var_redir_str(char_u *value, int value_len);
8-
void var_redir_stop(void);
9-
int eval_charconvert(char_u *enc_from, char_u *enc_to, char_u *fname_from, char_u *fname_to);
10-
int eval_printexpr(char_u *fname, char_u *args);
11-
void eval_diff(char_u *origfile, char_u *newfile, char_u *outfile);
12-
void eval_patch(char_u *origfile, char_u *difffile, char_u *outfile);
136
int eval_to_bool(char_u *arg, int *error, char_u **nextcmd, int skip);
147
int eval_expr_typval(typval_T *expr, typval_T *argv, int argc, typval_T *rettv);
158
int eval_expr_to_bool(typval_T *expr, int *error);
@@ -33,7 +26,6 @@ void *eval_for_line(char_u *arg, int *errp, char_u **nextcmdp, int skip);
3326
int next_for_item(void *fi_void, char_u *arg);
3427
void free_for_info(void *fi_void);
3528
void set_context_for_expression(expand_T *xp, char_u *arg, cmdidx_T cmdidx);
36-
void del_menutrans_vars(void);
3729
int pattern_match(char_u *pat, char_u *text, int ic);
3830
int eval0(char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate);
3931
int eval1(char_u **arg, typval_T *rettv, int evaluate);
@@ -76,13 +68,11 @@ char_u *tv_get_string_chk(typval_T *varp);
7668
char_u *tv_get_string_buf_chk(typval_T *varp, char_u *buf);
7769
void copy_tv(typval_T *from, typval_T *to);
7870
int item_copy(typval_T *from, typval_T *to, int deep, int copyID);
79-
void get_user_input(typval_T *argvars, typval_T *rettv, int inputdialog, int secret);
8071
void ex_echo(exarg_T *eap);
8172
void ex_echohl(exarg_T *eap);
73+
int get_echo_attr(void);
8274
void ex_execute(exarg_T *eap);
8375
char_u *find_option_end(char_u **arg, int *opt_flags);
84-
char_u *autoload_name(char_u *name);
85-
int script_autoload(char_u *name, int reload);
8676
void last_set_msg(sctx_T script_ctx);
8777
int typval_compare(typval_T *typ1, typval_T *typ2, exptype_T type, int type_is, int ic);
8878
char_u *typval_tostring(typval_T *arg);

src/proto/evalvars.pro

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
/* evalvars.c */
22
void evalvars_init(void);
33
void evalvars_clear(void);
4+
int garbage_collect_globvars(int copyID);
45
int garbage_collect_vimvars(int copyID);
56
int garbage_collect_scriptvars(int copyID);
67
void set_internal_string_var(char_u *name, char_u *value);
8+
int eval_charconvert(char_u *enc_from, char_u *enc_to, char_u *fname_from, char_u *fname_to);
9+
int eval_printexpr(char_u *fname, char_u *args);
10+
void eval_diff(char_u *origfile, char_u *newfile, char_u *outfile);
11+
void eval_patch(char_u *origfile, char_u *difffile, char_u *outfile);
712
void prepare_vimvar(int idx, typval_T *save_tv);
813
void restore_vimvar(int idx, typval_T *save_tv);
914
void ex_let(exarg_T *eap);
@@ -14,7 +19,12 @@ void list_hashtable_vars(hashtab_T *ht, char *prefix, int empty, int *first);
1419
void ex_unlet(exarg_T *eap);
1520
void ex_lockvar(exarg_T *eap);
1621
int do_unlet(char_u *name, int forceit);
22+
void del_menutrans_vars(void);
1723
char_u *get_user_var_name(expand_T *xp, int idx);
24+
char *get_var_special_name(int nr);
25+
dict_T *get_globvar_dict(void);
26+
hashtab_T *get_globvar_ht(void);
27+
dict_T *get_vimvar_dict(void);
1828
void set_vim_var_type(int idx, vartype_T type);
1929
void set_vim_var_nr(int idx, varnumber_T val);
2030
typval_T *get_vim_var_tv(int idx);
@@ -44,7 +54,6 @@ void init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope);
4454
void unref_var_dict(dict_T *dict);
4555
void vars_clear(hashtab_T *ht);
4656
void vars_clear_ext(hashtab_T *ht, int free_val);
47-
void delete_var(hashtab_T *ht, hashitem_T *hi);
4857
void set_var(char_u *name, typval_T *tv, int copy);
4958
void set_var_const(char_u *name, typval_T *tv, int copy, int is_const);
5059
int var_check_ro(int flags, char_u *name, int use_gettext);
@@ -55,6 +64,9 @@ int valid_varname(char_u *varname);
5564
void reset_v_option_vars(void);
5665
void assert_error(garray_T *gap);
5766
int var_exists(char_u *var);
67+
int var_redir_start(char_u *name, int append);
68+
void var_redir_str(char_u *value, int value_len);
69+
void var_redir_stop(void);
5870
void f_gettabvar(typval_T *argvars, typval_T *rettv);
5971
void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
6072
void f_getwinvar(typval_T *argvars, typval_T *rettv);

src/proto/ex_getln.pro

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,5 @@ int get_cmdline_type(void);
3535
int get_cmdline_firstc(void);
3636
int get_list_range(char_u **str, int *num1, int *num2);
3737
char_u *script_get(exarg_T *eap, char_u *cmd);
38+
void get_user_input(typval_T *argvars, typval_T *rettv, int inputdialog, int secret);
3839
/* vim: set ft=c : */

src/proto/scriptfile.pro

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,14 @@ void ex_scriptnames(exarg_T *eap);
2020
void scriptnames_slash_adjust(void);
2121
char_u *get_scriptname(scid_T id);
2222
void free_scriptnames(void);
23+
void free_autoload_scriptnames(void);
2324
linenr_T get_sourced_lnum(char_u *(*fgetline)(int, void *, int, int), void *cookie);
2425
char_u *getsourceline(int c, void *cookie, int indent, int do_concat);
2526
void ex_scriptencoding(exarg_T *eap);
2627
void ex_scriptversion(exarg_T *eap);
2728
void ex_finish(exarg_T *eap);
2829
void do_finish(exarg_T *eap, int reanimate);
2930
int source_finished(char_u *(*fgetline)(int, void *, int, int), void *cookie);
31+
char_u *autoload_name(char_u *name);
32+
int script_autoload(char_u *name, int reload);
3033
/* vim: set ft=c : */

src/scriptfile.c

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@
1313

1414
#include "vim.h"
1515

16+
#if defined(FEAT_EVAL) || defined(PROTO)
17+
// The names of packages that once were loaded are remembered.
18+
static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
19+
#endif
20+
1621
/*
1722
* ":runtime [what] {name}"
1823
*/
@@ -1334,6 +1339,12 @@ free_scriptnames(void)
13341339
vim_free(SCRIPT_ITEM(i).sn_name);
13351340
ga_clear(&script_items);
13361341
}
1342+
1343+
void
1344+
free_autoload_scriptnames(void)
1345+
{
1346+
ga_clear_strings(&ga_loaded);
1347+
}
13371348
# endif
13381349

13391350
#endif
@@ -1690,4 +1701,76 @@ source_finished(
16901701
&& ((struct source_cookie *)getline_cookie(
16911702
fgetline, cookie))->finished);
16921703
}
1704+
1705+
/*
1706+
* Return the autoload script name for a function or variable name.
1707+
* Returns NULL when out of memory.
1708+
* Caller must make sure that "name" contains AUTOLOAD_CHAR.
1709+
*/
1710+
char_u *
1711+
autoload_name(char_u *name)
1712+
{
1713+
char_u *p, *q = NULL;
1714+
char_u *scriptname;
1715+
1716+
// Get the script file name: replace '#' with '/', append ".vim".
1717+
scriptname = alloc(STRLEN(name) + 14);
1718+
if (scriptname == NULL)
1719+
return NULL;
1720+
STRCPY(scriptname, "autoload/");
1721+
STRCAT(scriptname, name);
1722+
for (p = scriptname + 9; (p = vim_strchr(p, AUTOLOAD_CHAR)) != NULL;
1723+
q = p, ++p)
1724+
*p = '/';
1725+
STRCPY(q, ".vim");
1726+
return scriptname;
1727+
}
1728+
1729+
/*
1730+
* If "name" has a package name try autoloading the script for it.
1731+
* Return TRUE if a package was loaded.
1732+
*/
1733+
int
1734+
script_autoload(
1735+
char_u *name,
1736+
int reload) // load script again when already loaded
1737+
{
1738+
char_u *p;
1739+
char_u *scriptname, *tofree;
1740+
int ret = FALSE;
1741+
int i;
1742+
1743+
// If there is no '#' after name[0] there is no package name.
1744+
p = vim_strchr(name, AUTOLOAD_CHAR);
1745+
if (p == NULL || p == name)
1746+
return FALSE;
1747+
1748+
tofree = scriptname = autoload_name(name);
1749+
if (scriptname == NULL)
1750+
return FALSE;
1751+
1752+
// Find the name in the list of previously loaded package names. Skip
1753+
// "autoload/", it's always the same.
1754+
for (i = 0; i < ga_loaded.ga_len; ++i)
1755+
if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
1756+
break;
1757+
if (!reload && i < ga_loaded.ga_len)
1758+
ret = FALSE; // was loaded already
1759+
else
1760+
{
1761+
// Remember the name if it wasn't loaded already.
1762+
if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
1763+
{
1764+
((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
1765+
tofree = NULL;
1766+
}
1767+
1768+
// Try loading the package from $VIMRUNTIME/autoload/<name>.vim
1769+
if (source_runtime(scriptname, 0) == OK)
1770+
ret = TRUE;
1771+
}
1772+
1773+
vim_free(tofree);
1774+
return ret;
1775+
}
16931776
#endif

0 commit comments

Comments
 (0)