annotate src/vim9script.c @ 26877:06a137af96f8 v8.2.3967

patch 8.2.3967: error messages are spread out Commit: https://github.com/vim/vim/commit/460ae5dfca31fa627531c263184849976755cf6b Author: Bram Moolenaar <Bram@vim.org> Date: Sat Jan 1 14:19:49 2022 +0000 patch 8.2.3967: error messages are spread out Problem: Error messages are spread out. Solution: Move more errors to errors.h.
author Bram Moolenaar <Bram@vim.org>
date Sat, 01 Jan 2022 15:30:05 +0100
parents bce848ec8b1b
children 4e77f9961650
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1 /* vi:set ts=8 sts=4 sw=4 noet:
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2 *
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
3 * VIM - Vi IMproved by Bram Moolenaar
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
4 *
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
5 * Do ":help uganda" in Vim to read copying and usage conditions.
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
6 * Do ":help credits" in Vim to see a list of people who contributed.
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
7 * See README.txt for an overview of the Vim source code.
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
8 */
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
9
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
10 /*
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
11 * vim9script.c: :vim9script, :import, :export and friends
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
12 */
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
13
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
14 #include "vim.h"
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
15
26662
4b23672d1f0e patch 8.2.3860: Vim9: codecov struggles with the file size
Bram Moolenaar <Bram@vim.org>
parents: 26482
diff changeset
16 // When not generating protos this is included in proto.h
4b23672d1f0e patch 8.2.3860: Vim9: codecov struggles with the file size
Bram Moolenaar <Bram@vim.org>
parents: 26482
diff changeset
17 #ifdef PROTO
23390
9a5f12b36273 patch 8.2.2238: Vim9: cannot load a Vim9 script without the +eval feature
Bram Moolenaar <Bram@vim.org>
parents: 23364
diff changeset
18 # include "vim9.h"
9a5f12b36273 patch 8.2.2238: Vim9: cannot load a Vim9 script without the +eval feature
Bram Moolenaar <Bram@vim.org>
parents: 23364
diff changeset
19 #endif
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
20
24279
e3dbf2e58c6a patch 8.2.2680: Vim9: problem defining a script variable from legacy function
Bram Moolenaar <Bram@vim.org>
parents: 24176
diff changeset
21 /*
e3dbf2e58c6a patch 8.2.2680: Vim9: problem defining a script variable from legacy function
Bram Moolenaar <Bram@vim.org>
parents: 24176
diff changeset
22 * Return TRUE when currently using Vim9 script syntax.
e3dbf2e58c6a patch 8.2.2680: Vim9: problem defining a script variable from legacy function
Bram Moolenaar <Bram@vim.org>
parents: 24176
diff changeset
23 * Does not go up the stack, a ":function" inside vim9script uses legacy
e3dbf2e58c6a patch 8.2.2680: Vim9: problem defining a script variable from legacy function
Bram Moolenaar <Bram@vim.org>
parents: 24176
diff changeset
24 * syntax.
e3dbf2e58c6a patch 8.2.2680: Vim9: problem defining a script variable from legacy function
Bram Moolenaar <Bram@vim.org>
parents: 24176
diff changeset
25 */
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
26 int
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
27 in_vim9script(void)
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
28 {
24279
e3dbf2e58c6a patch 8.2.2680: Vim9: problem defining a script variable from legacy function
Bram Moolenaar <Bram@vim.org>
parents: 24176
diff changeset
29 // "sc_version" is also set when compiling a ":def" function in legacy
e3dbf2e58c6a patch 8.2.2680: Vim9: problem defining a script variable from legacy function
Bram Moolenaar <Bram@vim.org>
parents: 24176
diff changeset
30 // script.
24531
3bfec39ce31c patch 8.2.2805: Vim9: cannot use legacy syntax in Vim9 script
Bram Moolenaar <Bram@vim.org>
parents: 24469
diff changeset
31 return (current_sctx.sc_version == SCRIPT_VERSION_VIM9
3bfec39ce31c patch 8.2.2805: Vim9: cannot use legacy syntax in Vim9 script
Bram Moolenaar <Bram@vim.org>
parents: 24469
diff changeset
32 || (cmdmod.cmod_flags & CMOD_VIM9CMD))
3bfec39ce31c patch 8.2.2805: Vim9: cannot use legacy syntax in Vim9 script
Bram Moolenaar <Bram@vim.org>
parents: 24469
diff changeset
33 && !(cmdmod.cmod_flags & CMOD_LEGACY);
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
34 }
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
35
24285
28b8ede0d2b9 patch 8.2.2683: build failure without the +eval feature
Bram Moolenaar <Bram@vim.org>
parents: 24283
diff changeset
36 #if defined(FEAT_EVAL) || defined(PROTO)
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
37 /*
25622
15b54e0a576b patch 8.2.3347: check for legacy script is incomplete
Bram Moolenaar <Bram@vim.org>
parents: 25567
diff changeset
38 * Return TRUE when currently in a script with script version smaller than
15b54e0a576b patch 8.2.3347: check for legacy script is incomplete
Bram Moolenaar <Bram@vim.org>
parents: 25567
diff changeset
39 * "max_version" or command modifiers forced it.
15b54e0a576b patch 8.2.3347: check for legacy script is incomplete
Bram Moolenaar <Bram@vim.org>
parents: 25567
diff changeset
40 */
15b54e0a576b patch 8.2.3347: check for legacy script is incomplete
Bram Moolenaar <Bram@vim.org>
parents: 25567
diff changeset
41 int
15b54e0a576b patch 8.2.3347: check for legacy script is incomplete
Bram Moolenaar <Bram@vim.org>
parents: 25567
diff changeset
42 in_old_script(int max_version)
15b54e0a576b patch 8.2.3347: check for legacy script is incomplete
Bram Moolenaar <Bram@vim.org>
parents: 25567
diff changeset
43 {
25626
4e13cde003a8 patch 8.2.3349: eval test for scriptversion fails
Bram Moolenaar <Bram@vim.org>
parents: 25622
diff changeset
44 return (current_sctx.sc_version < max_version
4e13cde003a8 patch 8.2.3349: eval test for scriptversion fails
Bram Moolenaar <Bram@vim.org>
parents: 25622
diff changeset
45 && !(cmdmod.cmod_flags & CMOD_VIM9CMD))
25622
15b54e0a576b patch 8.2.3347: check for legacy script is incomplete
Bram Moolenaar <Bram@vim.org>
parents: 25567
diff changeset
46 || (cmdmod.cmod_flags & CMOD_LEGACY);
15b54e0a576b patch 8.2.3347: check for legacy script is incomplete
Bram Moolenaar <Bram@vim.org>
parents: 25567
diff changeset
47 }
15b54e0a576b patch 8.2.3347: check for legacy script is incomplete
Bram Moolenaar <Bram@vim.org>
parents: 25567
diff changeset
48
15b54e0a576b patch 8.2.3347: check for legacy script is incomplete
Bram Moolenaar <Bram@vim.org>
parents: 25567
diff changeset
49 /*
24279
e3dbf2e58c6a patch 8.2.2680: Vim9: problem defining a script variable from legacy function
Bram Moolenaar <Bram@vim.org>
parents: 24176
diff changeset
50 * Return TRUE if the current script is Vim9 script.
e3dbf2e58c6a patch 8.2.2680: Vim9: problem defining a script variable from legacy function
Bram Moolenaar <Bram@vim.org>
parents: 24176
diff changeset
51 * This also returns TRUE in a legacy function in a Vim9 script.
e3dbf2e58c6a patch 8.2.2680: Vim9: problem defining a script variable from legacy function
Bram Moolenaar <Bram@vim.org>
parents: 24176
diff changeset
52 */
e3dbf2e58c6a patch 8.2.2680: Vim9: problem defining a script variable from legacy function
Bram Moolenaar <Bram@vim.org>
parents: 24176
diff changeset
53 int
e3dbf2e58c6a patch 8.2.2680: Vim9: problem defining a script variable from legacy function
Bram Moolenaar <Bram@vim.org>
parents: 24176
diff changeset
54 current_script_is_vim9(void)
e3dbf2e58c6a patch 8.2.2680: Vim9: problem defining a script variable from legacy function
Bram Moolenaar <Bram@vim.org>
parents: 24176
diff changeset
55 {
e3dbf2e58c6a patch 8.2.2680: Vim9: problem defining a script variable from legacy function
Bram Moolenaar <Bram@vim.org>
parents: 24176
diff changeset
56 return SCRIPT_ID_VALID(current_sctx.sc_sid)
e3dbf2e58c6a patch 8.2.2680: Vim9: problem defining a script variable from legacy function
Bram Moolenaar <Bram@vim.org>
parents: 24176
diff changeset
57 && SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
e3dbf2e58c6a patch 8.2.2680: Vim9: problem defining a script variable from legacy function
Bram Moolenaar <Bram@vim.org>
parents: 24176
diff changeset
58 == SCRIPT_VERSION_VIM9;
e3dbf2e58c6a patch 8.2.2680: Vim9: problem defining a script variable from legacy function
Bram Moolenaar <Bram@vim.org>
parents: 24176
diff changeset
59 }
24285
28b8ede0d2b9 patch 8.2.2683: build failure without the +eval feature
Bram Moolenaar <Bram@vim.org>
parents: 24283
diff changeset
60 #endif
24279
e3dbf2e58c6a patch 8.2.2680: Vim9: problem defining a script variable from legacy function
Bram Moolenaar <Bram@vim.org>
parents: 24176
diff changeset
61
e3dbf2e58c6a patch 8.2.2680: Vim9: problem defining a script variable from legacy function
Bram Moolenaar <Bram@vim.org>
parents: 24176
diff changeset
62 /*
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
63 * ":vim9script".
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
64 */
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
65 void
23390
9a5f12b36273 patch 8.2.2238: Vim9: cannot load a Vim9 script without the +eval feature
Bram Moolenaar <Bram@vim.org>
parents: 23364
diff changeset
66 ex_vim9script(exarg_T *eap UNUSED)
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
67 {
23390
9a5f12b36273 patch 8.2.2238: Vim9: cannot load a Vim9 script without the +eval feature
Bram Moolenaar <Bram@vim.org>
parents: 23364
diff changeset
68 #ifdef FEAT_EVAL
23358
b3142fc0a414 patch 8.2.2222: Vim9: cannot keep script variables when reloading
Bram Moolenaar <Bram@vim.org>
parents: 23332
diff changeset
69 int sid = current_sctx.sc_sid;
20881
58137dbee8da patch 8.2.0992: Vim9: crash when using :import in the Vim command
Bram Moolenaar <Bram@vim.org>
parents: 20846
diff changeset
70 scriptitem_T *si;
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
71
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
72 if (!getline_equal(eap->getline, eap->cookie, getsourceline))
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
73 {
21821
0deb6f96a5a3 patch 8.2.1460: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 21789
diff changeset
74 emsg(_(e_vim9script_can_only_be_used_in_script));
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
75 return;
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
76 }
23358
b3142fc0a414 patch 8.2.2222: Vim9: cannot keep script variables when reloading
Bram Moolenaar <Bram@vim.org>
parents: 23332
diff changeset
77
b3142fc0a414 patch 8.2.2222: Vim9: cannot keep script variables when reloading
Bram Moolenaar <Bram@vim.org>
parents: 23332
diff changeset
78 si = SCRIPT_ITEM(sid);
b3142fc0a414 patch 8.2.2222: Vim9: cannot keep script variables when reloading
Bram Moolenaar <Bram@vim.org>
parents: 23332
diff changeset
79 if (si->sn_state == SN_STATE_HAD_COMMAND)
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
80 {
21821
0deb6f96a5a3 patch 8.2.1460: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 21789
diff changeset
81 emsg(_(e_vim9script_must_be_first_command_in_script));
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
82 return;
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
83 }
23358
b3142fc0a414 patch 8.2.2222: Vim9: cannot keep script variables when reloading
Bram Moolenaar <Bram@vim.org>
parents: 23332
diff changeset
84 if (!IS_WHITE_OR_NUL(*eap->arg) && STRCMP(eap->arg, "noclear") != 0)
b3142fc0a414 patch 8.2.2222: Vim9: cannot keep script variables when reloading
Bram Moolenaar <Bram@vim.org>
parents: 23332
diff changeset
85 {
26865
bce848ec8b1b patch 8.2.3961: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 26662
diff changeset
86 semsg(_(e_invalid_argument_str), eap->arg);
23358
b3142fc0a414 patch 8.2.2222: Vim9: cannot keep script variables when reloading
Bram Moolenaar <Bram@vim.org>
parents: 23332
diff changeset
87 return;
b3142fc0a414 patch 8.2.2222: Vim9: cannot keep script variables when reloading
Bram Moolenaar <Bram@vim.org>
parents: 23332
diff changeset
88 }
b3142fc0a414 patch 8.2.2222: Vim9: cannot keep script variables when reloading
Bram Moolenaar <Bram@vim.org>
parents: 23332
diff changeset
89 if (si->sn_state == SN_STATE_RELOAD && IS_WHITE_OR_NUL(*eap->arg))
b3142fc0a414 patch 8.2.2222: Vim9: cannot keep script variables when reloading
Bram Moolenaar <Bram@vim.org>
parents: 23332
diff changeset
90 {
b3142fc0a414 patch 8.2.2222: Vim9: cannot keep script variables when reloading
Bram Moolenaar <Bram@vim.org>
parents: 23332
diff changeset
91 hashtab_T *ht = &SCRIPT_VARS(sid);
b3142fc0a414 patch 8.2.2222: Vim9: cannot keep script variables when reloading
Bram Moolenaar <Bram@vim.org>
parents: 23332
diff changeset
92
b3142fc0a414 patch 8.2.2222: Vim9: cannot keep script variables when reloading
Bram Moolenaar <Bram@vim.org>
parents: 23332
diff changeset
93 // Reloading a script without the "noclear" argument: clear
b3142fc0a414 patch 8.2.2222: Vim9: cannot keep script variables when reloading
Bram Moolenaar <Bram@vim.org>
parents: 23332
diff changeset
94 // script-local variables and functions.
b3142fc0a414 patch 8.2.2222: Vim9: cannot keep script variables when reloading
Bram Moolenaar <Bram@vim.org>
parents: 23332
diff changeset
95 hashtab_free_contents(ht);
b3142fc0a414 patch 8.2.2222: Vim9: cannot keep script variables when reloading
Bram Moolenaar <Bram@vim.org>
parents: 23332
diff changeset
96 hash_init(ht);
b3142fc0a414 patch 8.2.2222: Vim9: cannot keep script variables when reloading
Bram Moolenaar <Bram@vim.org>
parents: 23332
diff changeset
97 delete_script_functions(sid);
b3142fc0a414 patch 8.2.2222: Vim9: cannot keep script variables when reloading
Bram Moolenaar <Bram@vim.org>
parents: 23332
diff changeset
98
b3142fc0a414 patch 8.2.2222: Vim9: cannot keep script variables when reloading
Bram Moolenaar <Bram@vim.org>
parents: 23332
diff changeset
99 // old imports and script variables are no longer valid
b3142fc0a414 patch 8.2.2222: Vim9: cannot keep script variables when reloading
Bram Moolenaar <Bram@vim.org>
parents: 23332
diff changeset
100 free_imports_and_script_vars(sid);
b3142fc0a414 patch 8.2.2222: Vim9: cannot keep script variables when reloading
Bram Moolenaar <Bram@vim.org>
parents: 23332
diff changeset
101 }
b3142fc0a414 patch 8.2.2222: Vim9: cannot keep script variables when reloading
Bram Moolenaar <Bram@vim.org>
parents: 23332
diff changeset
102 si->sn_state = SN_STATE_HAD_COMMAND;
b3142fc0a414 patch 8.2.2222: Vim9: cannot keep script variables when reloading
Bram Moolenaar <Bram@vim.org>
parents: 23332
diff changeset
103
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
104 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
105 si->sn_version = SCRIPT_VERSION_VIM9;
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
106
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
107 if (STRCMP(p_cpo, CPO_VIM) != 0)
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
108 {
22643
71b57779177d patch 8.2.1870: Vim9: no need to keep all script variables
Bram Moolenaar <Bram@vim.org>
parents: 22596
diff changeset
109 si->sn_save_cpo = vim_strsave(p_cpo);
24079
a9ff8368d35f patch 8.2.2581: Vim9: sourcing Vim9 script triggers a redraw
Bram Moolenaar <Bram@vim.org>
parents: 24049
diff changeset
110 set_option_value((char_u *)"cpo", 0L, (char_u *)CPO_VIM, OPT_NO_REDRAW);
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
111 }
23390
9a5f12b36273 patch 8.2.2238: Vim9: cannot load a Vim9 script without the +eval feature
Bram Moolenaar <Bram@vim.org>
parents: 23364
diff changeset
112 #else
9a5f12b36273 patch 8.2.2238: Vim9: cannot load a Vim9 script without the +eval feature
Bram Moolenaar <Bram@vim.org>
parents: 23364
diff changeset
113 // No check for this being the first command, it doesn't matter.
9a5f12b36273 patch 8.2.2238: Vim9: cannot load a Vim9 script without the +eval feature
Bram Moolenaar <Bram@vim.org>
parents: 23364
diff changeset
114 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
9a5f12b36273 patch 8.2.2238: Vim9: cannot load a Vim9 script without the +eval feature
Bram Moolenaar <Bram@vim.org>
parents: 23364
diff changeset
115 #endif
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
116 }
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
117
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
118 /*
21516
c7b2ce90c2de patch 8.2.1308: Vim9: accidentally using "x" causes Vim to exit
Bram Moolenaar <Bram@vim.org>
parents: 21473
diff changeset
119 * When in Vim9 script give an error and return FAIL.
c7b2ce90c2de patch 8.2.1308: Vim9: accidentally using "x" causes Vim to exit
Bram Moolenaar <Bram@vim.org>
parents: 21473
diff changeset
120 */
c7b2ce90c2de patch 8.2.1308: Vim9: accidentally using "x" causes Vim to exit
Bram Moolenaar <Bram@vim.org>
parents: 21473
diff changeset
121 int
c7b2ce90c2de patch 8.2.1308: Vim9: accidentally using "x" causes Vim to exit
Bram Moolenaar <Bram@vim.org>
parents: 21473
diff changeset
122 not_in_vim9(exarg_T *eap)
c7b2ce90c2de patch 8.2.1308: Vim9: accidentally using "x" causes Vim to exit
Bram Moolenaar <Bram@vim.org>
parents: 21473
diff changeset
123 {
21522
e17d0b882194 patch 8.2.1311: test failures with legacy Vim script
Bram Moolenaar <Bram@vim.org>
parents: 21516
diff changeset
124 if (in_vim9script())
e17d0b882194 patch 8.2.1311: test failures with legacy Vim script
Bram Moolenaar <Bram@vim.org>
parents: 21516
diff changeset
125 switch (eap->cmdidx)
e17d0b882194 patch 8.2.1311: test failures with legacy Vim script
Bram Moolenaar <Bram@vim.org>
parents: 21516
diff changeset
126 {
23980
bee8c78c0c6a patch 8.2.2532: Vim9: confusing error if :k is used with a range
Bram Moolenaar <Bram@vim.org>
parents: 23978
diff changeset
127 case CMD_k:
bee8c78c0c6a patch 8.2.2532: Vim9: confusing error if :k is used with a range
Bram Moolenaar <Bram@vim.org>
parents: 23978
diff changeset
128 if (eap->addr_count > 0)
bee8c78c0c6a patch 8.2.2532: Vim9: confusing error if :k is used with a range
Bram Moolenaar <Bram@vim.org>
parents: 23978
diff changeset
129 {
26877
06a137af96f8 patch 8.2.3967: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 26865
diff changeset
130 emsg(_(e_no_range_allowed));
23980
bee8c78c0c6a patch 8.2.2532: Vim9: confusing error if :k is used with a range
Bram Moolenaar <Bram@vim.org>
parents: 23978
diff changeset
131 return FAIL;
bee8c78c0c6a patch 8.2.2532: Vim9: confusing error if :k is used with a range
Bram Moolenaar <Bram@vim.org>
parents: 23978
diff changeset
132 }
bee8c78c0c6a patch 8.2.2532: Vim9: confusing error if :k is used with a range
Bram Moolenaar <Bram@vim.org>
parents: 23978
diff changeset
133 // FALLTHROUGH
21522
e17d0b882194 patch 8.2.1311: test failures with legacy Vim script
Bram Moolenaar <Bram@vim.org>
parents: 21516
diff changeset
134 case CMD_append:
e17d0b882194 patch 8.2.1311: test failures with legacy Vim script
Bram Moolenaar <Bram@vim.org>
parents: 21516
diff changeset
135 case CMD_change:
21584
d0c76ce48326 patch 8.2.1342: Vim9: accidentally using "t" gives a confusing error
Bram Moolenaar <Bram@vim.org>
parents: 21522
diff changeset
136 case CMD_insert:
24114
291c57cf4731 patch 8.2.2598: Vim9: :open does not need to be supported
Bram Moolenaar <Bram@vim.org>
parents: 24112
diff changeset
137 case CMD_open:
21584
d0c76ce48326 patch 8.2.1342: Vim9: accidentally using "t" gives a confusing error
Bram Moolenaar <Bram@vim.org>
parents: 21522
diff changeset
138 case CMD_t:
21522
e17d0b882194 patch 8.2.1311: test failures with legacy Vim script
Bram Moolenaar <Bram@vim.org>
parents: 21516
diff changeset
139 case CMD_xit:
23390
9a5f12b36273 patch 8.2.2238: Vim9: cannot load a Vim9 script without the +eval feature
Bram Moolenaar <Bram@vim.org>
parents: 23364
diff changeset
140 semsg(_(e_command_not_supported_in_vim9_script_missing_var_str), eap->cmd);
21522
e17d0b882194 patch 8.2.1311: test failures with legacy Vim script
Bram Moolenaar <Bram@vim.org>
parents: 21516
diff changeset
141 return FAIL;
e17d0b882194 patch 8.2.1311: test failures with legacy Vim script
Bram Moolenaar <Bram@vim.org>
parents: 21516
diff changeset
142 default: break;
e17d0b882194 patch 8.2.1311: test failures with legacy Vim script
Bram Moolenaar <Bram@vim.org>
parents: 21516
diff changeset
143 }
21516
c7b2ce90c2de patch 8.2.1308: Vim9: accidentally using "x" causes Vim to exit
Bram Moolenaar <Bram@vim.org>
parents: 21473
diff changeset
144 return OK;
c7b2ce90c2de patch 8.2.1308: Vim9: accidentally using "x" causes Vim to exit
Bram Moolenaar <Bram@vim.org>
parents: 21473
diff changeset
145 }
c7b2ce90c2de patch 8.2.1308: Vim9: accidentally using "x" causes Vim to exit
Bram Moolenaar <Bram@vim.org>
parents: 21473
diff changeset
146
23392
517fca70e084 patch 8.2.2239: Vim9: concatenating lines with backslash is inconvenient
Bram Moolenaar <Bram@vim.org>
parents: 23390
diff changeset
147 /*
24158
93e69703a290 patch 8.2.2620: Vim9: Using #{ for a dictionary gives strange errors
Bram Moolenaar <Bram@vim.org>
parents: 24114
diff changeset
148 * Give an error message if "p" points at "#{" and return TRUE.
93e69703a290 patch 8.2.2620: Vim9: Using #{ for a dictionary gives strange errors
Bram Moolenaar <Bram@vim.org>
parents: 24114
diff changeset
149 * This avoids that using a legacy style #{} dictionary leads to difficult to
93e69703a290 patch 8.2.2620: Vim9: Using #{ for a dictionary gives strange errors
Bram Moolenaar <Bram@vim.org>
parents: 24114
diff changeset
150 * understand errors.
93e69703a290 patch 8.2.2620: Vim9: Using #{ for a dictionary gives strange errors
Bram Moolenaar <Bram@vim.org>
parents: 24114
diff changeset
151 */
93e69703a290 patch 8.2.2620: Vim9: Using #{ for a dictionary gives strange errors
Bram Moolenaar <Bram@vim.org>
parents: 24114
diff changeset
152 int
93e69703a290 patch 8.2.2620: Vim9: Using #{ for a dictionary gives strange errors
Bram Moolenaar <Bram@vim.org>
parents: 24114
diff changeset
153 vim9_bad_comment(char_u *p)
93e69703a290 patch 8.2.2620: Vim9: Using #{ for a dictionary gives strange errors
Bram Moolenaar <Bram@vim.org>
parents: 24114
diff changeset
154 {
24176
12378fbc99bc patch 8.2.2629: Vim9: error for #{{ is not desired
Bram Moolenaar <Bram@vim.org>
parents: 24160
diff changeset
155 if (p[0] == '#' && p[1] == '{' && p[2] != '{')
24158
93e69703a290 patch 8.2.2620: Vim9: Using #{ for a dictionary gives strange errors
Bram Moolenaar <Bram@vim.org>
parents: 24114
diff changeset
156 {
93e69703a290 patch 8.2.2620: Vim9: Using #{ for a dictionary gives strange errors
Bram Moolenaar <Bram@vim.org>
parents: 24114
diff changeset
157 emsg(_(e_cannot_use_hash_curly_to_start_comment));
93e69703a290 patch 8.2.2620: Vim9: Using #{ for a dictionary gives strange errors
Bram Moolenaar <Bram@vim.org>
parents: 24114
diff changeset
158 return TRUE;
93e69703a290 patch 8.2.2620: Vim9: Using #{ for a dictionary gives strange errors
Bram Moolenaar <Bram@vim.org>
parents: 24114
diff changeset
159 }
93e69703a290 patch 8.2.2620: Vim9: Using #{ for a dictionary gives strange errors
Bram Moolenaar <Bram@vim.org>
parents: 24114
diff changeset
160 return FALSE;
93e69703a290 patch 8.2.2620: Vim9: Using #{ for a dictionary gives strange errors
Bram Moolenaar <Bram@vim.org>
parents: 24114
diff changeset
161 }
93e69703a290 patch 8.2.2620: Vim9: Using #{ for a dictionary gives strange errors
Bram Moolenaar <Bram@vim.org>
parents: 24114
diff changeset
162
93e69703a290 patch 8.2.2620: Vim9: Using #{ for a dictionary gives strange errors
Bram Moolenaar <Bram@vim.org>
parents: 24114
diff changeset
163 /*
24176
12378fbc99bc patch 8.2.2629: Vim9: error for #{{ is not desired
Bram Moolenaar <Bram@vim.org>
parents: 24160
diff changeset
164 * Return TRUE if "p" points at a "#" not followed by one '{'.
24158
93e69703a290 patch 8.2.2620: Vim9: Using #{ for a dictionary gives strange errors
Bram Moolenaar <Bram@vim.org>
parents: 24114
diff changeset
165 * Does not check for white space.
23392
517fca70e084 patch 8.2.2239: Vim9: concatenating lines with backslash is inconvenient
Bram Moolenaar <Bram@vim.org>
parents: 23390
diff changeset
166 */
517fca70e084 patch 8.2.2239: Vim9: concatenating lines with backslash is inconvenient
Bram Moolenaar <Bram@vim.org>
parents: 23390
diff changeset
167 int
517fca70e084 patch 8.2.2239: Vim9: concatenating lines with backslash is inconvenient
Bram Moolenaar <Bram@vim.org>
parents: 23390
diff changeset
168 vim9_comment_start(char_u *p)
517fca70e084 patch 8.2.2239: Vim9: concatenating lines with backslash is inconvenient
Bram Moolenaar <Bram@vim.org>
parents: 23390
diff changeset
169 {
24176
12378fbc99bc patch 8.2.2629: Vim9: error for #{{ is not desired
Bram Moolenaar <Bram@vim.org>
parents: 24160
diff changeset
170 return p[0] == '#' && (p[1] != '{' || p[2] == '{');
23392
517fca70e084 patch 8.2.2239: Vim9: concatenating lines with backslash is inconvenient
Bram Moolenaar <Bram@vim.org>
parents: 23390
diff changeset
171 }
517fca70e084 patch 8.2.2239: Vim9: concatenating lines with backslash is inconvenient
Bram Moolenaar <Bram@vim.org>
parents: 23390
diff changeset
172
23390
9a5f12b36273 patch 8.2.2238: Vim9: cannot load a Vim9 script without the +eval feature
Bram Moolenaar <Bram@vim.org>
parents: 23364
diff changeset
173 #if defined(FEAT_EVAL) || defined(PROTO)
9a5f12b36273 patch 8.2.2238: Vim9: cannot load a Vim9 script without the +eval feature
Bram Moolenaar <Bram@vim.org>
parents: 23364
diff changeset
174
21516
c7b2ce90c2de patch 8.2.1308: Vim9: accidentally using "x" causes Vim to exit
Bram Moolenaar <Bram@vim.org>
parents: 21473
diff changeset
175 /*
24533
9c404d78d767 patch 8.2.2806: Vim9: using "++nr" as a command might not work
Bram Moolenaar <Bram@vim.org>
parents: 24531
diff changeset
176 * "++nr" and "--nr" commands.
9c404d78d767 patch 8.2.2806: Vim9: using "++nr" as a command might not work
Bram Moolenaar <Bram@vim.org>
parents: 24531
diff changeset
177 */
9c404d78d767 patch 8.2.2806: Vim9: using "++nr" as a command might not work
Bram Moolenaar <Bram@vim.org>
parents: 24531
diff changeset
178 void
9c404d78d767 patch 8.2.2806: Vim9: using "++nr" as a command might not work
Bram Moolenaar <Bram@vim.org>
parents: 24531
diff changeset
179 ex_incdec(exarg_T *eap)
9c404d78d767 patch 8.2.2806: Vim9: using "++nr" as a command might not work
Bram Moolenaar <Bram@vim.org>
parents: 24531
diff changeset
180 {
9c404d78d767 patch 8.2.2806: Vim9: using "++nr" as a command might not work
Bram Moolenaar <Bram@vim.org>
parents: 24531
diff changeset
181 char_u *cmd = eap->cmd;
25020
91f396f149d5 patch 8.2.3047: increment and decrement don't allow for next command
Bram Moolenaar <Bram@vim.org>
parents: 24888
diff changeset
182 char_u *nextcmd = eap->nextcmd;
91f396f149d5 patch 8.2.3047: increment and decrement don't allow for next command
Bram Moolenaar <Bram@vim.org>
parents: 24888
diff changeset
183 size_t len = STRLEN(eap->cmd) + 8;
24533
9c404d78d767 patch 8.2.2806: Vim9: using "++nr" as a command might not work
Bram Moolenaar <Bram@vim.org>
parents: 24531
diff changeset
184
25022
39551b6e0112 patch 8.2.3048: strange error for white space after ++ command
Bram Moolenaar <Bram@vim.org>
parents: 25020
diff changeset
185 if (VIM_ISWHITE(cmd[2]))
39551b6e0112 patch 8.2.3048: strange error for white space after ++ command
Bram Moolenaar <Bram@vim.org>
parents: 25020
diff changeset
186 {
39551b6e0112 patch 8.2.3048: strange error for white space after ++ command
Bram Moolenaar <Bram@vim.org>
parents: 25020
diff changeset
187 semsg(_(e_no_white_space_allowed_after_str_str),
39551b6e0112 patch 8.2.3048: strange error for white space after ++ command
Bram Moolenaar <Bram@vim.org>
parents: 25020
diff changeset
188 eap->cmdidx == CMD_increment ? "++" : "--", eap->cmd);
39551b6e0112 patch 8.2.3048: strange error for white space after ++ command
Bram Moolenaar <Bram@vim.org>
parents: 25020
diff changeset
189 return;
39551b6e0112 patch 8.2.3048: strange error for white space after ++ command
Bram Moolenaar <Bram@vim.org>
parents: 25020
diff changeset
190 }
39551b6e0112 patch 8.2.3048: strange error for white space after ++ command
Bram Moolenaar <Bram@vim.org>
parents: 25020
diff changeset
191
24533
9c404d78d767 patch 8.2.2806: Vim9: using "++nr" as a command might not work
Bram Moolenaar <Bram@vim.org>
parents: 24531
diff changeset
192 // This works like "nr += 1" or "nr -= 1".
25020
91f396f149d5 patch 8.2.3047: increment and decrement don't allow for next command
Bram Moolenaar <Bram@vim.org>
parents: 24888
diff changeset
193 // Add a '|' to avoid looking in the next line.
24533
9c404d78d767 patch 8.2.2806: Vim9: using "++nr" as a command might not work
Bram Moolenaar <Bram@vim.org>
parents: 24531
diff changeset
194 eap->cmd = alloc(len);
9c404d78d767 patch 8.2.2806: Vim9: using "++nr" as a command might not work
Bram Moolenaar <Bram@vim.org>
parents: 24531
diff changeset
195 if (eap->cmd == NULL)
9c404d78d767 patch 8.2.2806: Vim9: using "++nr" as a command might not work
Bram Moolenaar <Bram@vim.org>
parents: 24531
diff changeset
196 return;
25020
91f396f149d5 patch 8.2.3047: increment and decrement don't allow for next command
Bram Moolenaar <Bram@vim.org>
parents: 24888
diff changeset
197 vim_snprintf((char *)eap->cmd, len, "%s %c= 1 |", cmd + 2,
24533
9c404d78d767 patch 8.2.2806: Vim9: using "++nr" as a command might not work
Bram Moolenaar <Bram@vim.org>
parents: 24531
diff changeset
198 eap->cmdidx == CMD_increment ? '+' : '-');
9c404d78d767 patch 8.2.2806: Vim9: using "++nr" as a command might not work
Bram Moolenaar <Bram@vim.org>
parents: 24531
diff changeset
199 eap->arg = eap->cmd;
9c404d78d767 patch 8.2.2806: Vim9: using "++nr" as a command might not work
Bram Moolenaar <Bram@vim.org>
parents: 24531
diff changeset
200 eap->cmdidx = CMD_var;
25020
91f396f149d5 patch 8.2.3047: increment and decrement don't allow for next command
Bram Moolenaar <Bram@vim.org>
parents: 24888
diff changeset
201 eap->nextcmd = NULL;
24533
9c404d78d767 patch 8.2.2806: Vim9: using "++nr" as a command might not work
Bram Moolenaar <Bram@vim.org>
parents: 24531
diff changeset
202 ex_let(eap);
9c404d78d767 patch 8.2.2806: Vim9: using "++nr" as a command might not work
Bram Moolenaar <Bram@vim.org>
parents: 24531
diff changeset
203 vim_free(eap->cmd);
9c404d78d767 patch 8.2.2806: Vim9: using "++nr" as a command might not work
Bram Moolenaar <Bram@vim.org>
parents: 24531
diff changeset
204 eap->cmd = cmd;
25020
91f396f149d5 patch 8.2.3047: increment and decrement don't allow for next command
Bram Moolenaar <Bram@vim.org>
parents: 24888
diff changeset
205 eap->nextcmd = nextcmd;
24533
9c404d78d767 patch 8.2.2806: Vim9: using "++nr" as a command might not work
Bram Moolenaar <Bram@vim.org>
parents: 24531
diff changeset
206 }
9c404d78d767 patch 8.2.2806: Vim9: using "++nr" as a command might not work
Bram Moolenaar <Bram@vim.org>
parents: 24531
diff changeset
207
9c404d78d767 patch 8.2.2806: Vim9: using "++nr" as a command might not work
Bram Moolenaar <Bram@vim.org>
parents: 24531
diff changeset
208 /*
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
209 * ":export let Name: type"
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
210 * ":export const Name: type"
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
211 * ":export def Name(..."
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
212 * ":export class Name ..."
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
213 */
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
214 void
20339
7587d892c00c patch 8.2.0725: Vim9: cannot call a function declared later in Vim9 script
Bram Moolenaar <Bram@vim.org>
parents: 20189
diff changeset
215 ex_export(exarg_T *eap)
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
216 {
21279
8d1d11afd8c8 patch 8.2.1190: Vim9: checking for Vim9 syntax is spread out
Bram Moolenaar <Bram@vim.org>
parents: 21218
diff changeset
217 if (!in_vim9script())
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
218 {
21821
0deb6f96a5a3 patch 8.2.1460: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 21789
diff changeset
219 emsg(_(e_export_can_only_be_used_in_vim9script));
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
220 return;
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
221 }
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
222
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
223 eap->cmd = eap->arg;
24049
fc4c2beea99a patch 8.2.2566: Vim9: Function name is not recognized
Bram Moolenaar <Bram@vim.org>
parents: 24033
diff changeset
224 (void)find_ex_command(eap, NULL, lookup_scriptitem, NULL);
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
225 switch (eap->cmdidx)
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
226 {
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
227 case CMD_let:
22391
a9fb7efa31d6 patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents: 22250
diff changeset
228 case CMD_var:
a9fb7efa31d6 patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents: 22250
diff changeset
229 case CMD_final:
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
230 case CMD_const:
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
231 case CMD_def:
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
232 // case CMD_class:
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
233 is_export = TRUE;
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
234 do_cmdline(eap->cmd, eap->getline, eap->cookie,
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
235 DOCMD_VERBOSE + DOCMD_NOWAIT);
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
236
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
237 // The command will reset "is_export" when exporting an item.
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
238 if (is_export)
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
239 {
21821
0deb6f96a5a3 patch 8.2.1460: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 21789
diff changeset
240 emsg(_(e_export_with_invalid_argument));
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
241 is_export = FALSE;
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
242 }
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
243 break;
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
244 default:
21821
0deb6f96a5a3 patch 8.2.1460: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 21789
diff changeset
245 emsg(_(e_invalid_command_after_export));
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
246 break;
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
247 }
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
248 }
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
249
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
250 /*
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
251 * Add a new imported item entry to the current script.
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
252 */
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
253 static imported_T *
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
254 new_imported(garray_T *gap)
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
255 {
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
256 if (ga_grow(gap, 1) == OK)
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
257 return ((imported_T *)gap->ga_data + gap->ga_len++);
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
258 return NULL;
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
259 }
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
260
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
261 /*
25567
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
262 * Free the script variables from "sn_all_vars".
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
263 */
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
264 static void
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
265 free_all_script_vars(scriptitem_T *si)
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
266 {
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
267 int todo;
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
268 hashtab_T *ht = &si->sn_all_vars.dv_hashtab;
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
269 hashitem_T *hi;
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
270 sallvar_T *sav;
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
271 sallvar_T *sav_next;
26482
b115b552071f patch 8.2.3771: Vim9: accessing freed memory when checking type
Bram Moolenaar <Bram@vim.org>
parents: 26302
diff changeset
272 int idx;
25567
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
273
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
274 hash_lock(ht);
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
275 todo = (int)ht->ht_used;
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
276 for (hi = ht->ht_array; todo > 0; ++hi)
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
277 {
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
278 if (!HASHITEM_EMPTY(hi))
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
279 {
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
280 --todo;
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
281
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
282 // Free the variable. Don't remove it from the hashtab, ht_array
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
283 // might change then. hash_clear() takes care of it later.
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
284 sav = HI2SAV(hi);
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
285 while (sav != NULL)
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
286 {
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
287 sav_next = sav->sav_next;
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
288 if (sav->sav_di == NULL)
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
289 clear_tv(&sav->sav_tv);
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
290 vim_free(sav);
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
291 sav = sav_next;
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
292 }
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
293 }
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
294 }
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
295 hash_clear(ht);
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
296 hash_init(ht);
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
297
26482
b115b552071f patch 8.2.3771: Vim9: accessing freed memory when checking type
Bram Moolenaar <Bram@vim.org>
parents: 26302
diff changeset
298 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
b115b552071f patch 8.2.3771: Vim9: accessing freed memory when checking type
Bram Moolenaar <Bram@vim.org>
parents: 26302
diff changeset
299 {
b115b552071f patch 8.2.3771: Vim9: accessing freed memory when checking type
Bram Moolenaar <Bram@vim.org>
parents: 26302
diff changeset
300 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
b115b552071f patch 8.2.3771: Vim9: accessing freed memory when checking type
Bram Moolenaar <Bram@vim.org>
parents: 26302
diff changeset
301
b115b552071f patch 8.2.3771: Vim9: accessing freed memory when checking type
Bram Moolenaar <Bram@vim.org>
parents: 26302
diff changeset
302 if (sv->sv_type_allocated)
b115b552071f patch 8.2.3771: Vim9: accessing freed memory when checking type
Bram Moolenaar <Bram@vim.org>
parents: 26302
diff changeset
303 free_type(sv->sv_type);
b115b552071f patch 8.2.3771: Vim9: accessing freed memory when checking type
Bram Moolenaar <Bram@vim.org>
parents: 26302
diff changeset
304 }
25567
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
305 ga_clear(&si->sn_var_vals);
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
306
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
307 // existing commands using script variable indexes are no longer valid
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
308 si->sn_script_seq = current_sctx.sc_seq;
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
309 }
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
310
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
311 /*
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
312 * Free all imported items in script "sid".
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
313 */
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
314 void
22594
209c7aa56325 patch 8.2.1845: Vim9: function defined in a block can't use block variables
Bram Moolenaar <Bram@vim.org>
parents: 22529
diff changeset
315 free_imports_and_script_vars(int sid)
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
316 {
19191
133ef7ba4e4e patch 8.2.0154: reallocating the list of scripts is inefficient
Bram Moolenaar <Bram@vim.org>
parents: 19181
diff changeset
317 scriptitem_T *si = SCRIPT_ITEM(sid);
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
318 int idx;
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
319
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
320 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
321 {
19726
ad37a198a708 patch 8.2.0419: various memory leaks in Vim9 script code
Bram Moolenaar <Bram@vim.org>
parents: 19623
diff changeset
322 imported_T *imp = ((imported_T *)si->sn_imports.ga_data) + idx;
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
323
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
324 vim_free(imp->imp_name);
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
325 }
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
326 ga_clear(&si->sn_imports);
22594
209c7aa56325 patch 8.2.1845: Vim9: function defined in a block can't use block variables
Bram Moolenaar <Bram@vim.org>
parents: 22529
diff changeset
327
209c7aa56325 patch 8.2.1845: Vim9: function defined in a block can't use block variables
Bram Moolenaar <Bram@vim.org>
parents: 22529
diff changeset
328 free_all_script_vars(si);
209c7aa56325 patch 8.2.1845: Vim9: function defined in a block can't use block variables
Bram Moolenaar <Bram@vim.org>
parents: 22529
diff changeset
329
21218
1f4d0375f947 patch 8.2.1160: Vim9: memory leak in allocated types
Bram Moolenaar <Bram@vim.org>
parents: 21194
diff changeset
330 clear_type_list(&si->sn_type_list);
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
331 }
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
332
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
333 /*
23364
17a0e32eefd4 patch 8.2.2225: Vim9: error when using :import in legacy script twice
Bram Moolenaar <Bram@vim.org>
parents: 23362
diff changeset
334 * Mark all imports as possible to redefine. Used when a script is loaded
17a0e32eefd4 patch 8.2.2225: Vim9: error when using :import in legacy script twice
Bram Moolenaar <Bram@vim.org>
parents: 23362
diff changeset
335 * again but not cleared.
17a0e32eefd4 patch 8.2.2225: Vim9: error when using :import in legacy script twice
Bram Moolenaar <Bram@vim.org>
parents: 23362
diff changeset
336 */
17a0e32eefd4 patch 8.2.2225: Vim9: error when using :import in legacy script twice
Bram Moolenaar <Bram@vim.org>
parents: 23362
diff changeset
337 void
17a0e32eefd4 patch 8.2.2225: Vim9: error when using :import in legacy script twice
Bram Moolenaar <Bram@vim.org>
parents: 23362
diff changeset
338 mark_imports_for_reload(int sid)
17a0e32eefd4 patch 8.2.2225: Vim9: error when using :import in legacy script twice
Bram Moolenaar <Bram@vim.org>
parents: 23362
diff changeset
339 {
17a0e32eefd4 patch 8.2.2225: Vim9: error when using :import in legacy script twice
Bram Moolenaar <Bram@vim.org>
parents: 23362
diff changeset
340 scriptitem_T *si = SCRIPT_ITEM(sid);
17a0e32eefd4 patch 8.2.2225: Vim9: error when using :import in legacy script twice
Bram Moolenaar <Bram@vim.org>
parents: 23362
diff changeset
341 int idx;
17a0e32eefd4 patch 8.2.2225: Vim9: error when using :import in legacy script twice
Bram Moolenaar <Bram@vim.org>
parents: 23362
diff changeset
342
17a0e32eefd4 patch 8.2.2225: Vim9: error when using :import in legacy script twice
Bram Moolenaar <Bram@vim.org>
parents: 23362
diff changeset
343 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
17a0e32eefd4 patch 8.2.2225: Vim9: error when using :import in legacy script twice
Bram Moolenaar <Bram@vim.org>
parents: 23362
diff changeset
344 {
17a0e32eefd4 patch 8.2.2225: Vim9: error when using :import in legacy script twice
Bram Moolenaar <Bram@vim.org>
parents: 23362
diff changeset
345 imported_T *imp = ((imported_T *)si->sn_imports.ga_data) + idx;
17a0e32eefd4 patch 8.2.2225: Vim9: error when using :import in legacy script twice
Bram Moolenaar <Bram@vim.org>
parents: 23362
diff changeset
346
17a0e32eefd4 patch 8.2.2225: Vim9: error when using :import in legacy script twice
Bram Moolenaar <Bram@vim.org>
parents: 23362
diff changeset
347 imp->imp_flags |= IMP_FLAGS_RELOAD;
17a0e32eefd4 patch 8.2.2225: Vim9: error when using :import in legacy script twice
Bram Moolenaar <Bram@vim.org>
parents: 23362
diff changeset
348 }
17a0e32eefd4 patch 8.2.2225: Vim9: error when using :import in legacy script twice
Bram Moolenaar <Bram@vim.org>
parents: 23362
diff changeset
349 }
17a0e32eefd4 patch 8.2.2225: Vim9: error when using :import in legacy script twice
Bram Moolenaar <Bram@vim.org>
parents: 23362
diff changeset
350
17a0e32eefd4 patch 8.2.2225: Vim9: error when using :import in legacy script twice
Bram Moolenaar <Bram@vim.org>
parents: 23362
diff changeset
351 /*
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
352 * Handle an ":import" command and add the resulting imported_T to "gap", when
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
353 * not NULL, or script "import_sid" sn_imports.
22596
107eae953b87 patch 8.2.1846: Vim9: block variables are not found in compiled function
Bram Moolenaar <Bram@vim.org>
parents: 22594
diff changeset
354 * "cctx" is NULL at the script level.
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
355 * Returns a pointer to after the command or NULL in case of failure
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
356 */
25567
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
357 static char_u *
21146
465d6e40e79c patch 8.2.1124: Vim9: no line break allowed in :import command
Bram Moolenaar <Bram@vim.org>
parents: 21120
diff changeset
358 handle_import(
465d6e40e79c patch 8.2.1124: Vim9: no line break allowed in :import command
Bram Moolenaar <Bram@vim.org>
parents: 21120
diff changeset
359 char_u *arg_start,
465d6e40e79c patch 8.2.1124: Vim9: no line break allowed in :import command
Bram Moolenaar <Bram@vim.org>
parents: 21120
diff changeset
360 garray_T *gap,
465d6e40e79c patch 8.2.1124: Vim9: no line break allowed in :import command
Bram Moolenaar <Bram@vim.org>
parents: 21120
diff changeset
361 int import_sid,
465d6e40e79c patch 8.2.1124: Vim9: no line break allowed in :import command
Bram Moolenaar <Bram@vim.org>
parents: 21120
diff changeset
362 evalarg_T *evalarg,
465d6e40e79c patch 8.2.1124: Vim9: no line break allowed in :import command
Bram Moolenaar <Bram@vim.org>
parents: 21120
diff changeset
363 void *cctx)
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
364 {
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
365 char_u *arg = arg_start;
21146
465d6e40e79c patch 8.2.1124: Vim9: no line break allowed in :import command
Bram Moolenaar <Bram@vim.org>
parents: 21120
diff changeset
366 char_u *cmd_end = NULL;
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
367 int ret = FAIL;
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
368 typval_T tv;
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
369 int sid = -1;
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
370 int res;
24029
429b4f8d2fac patch 8.2.2556: Vim9: :import with "as" not fully supported
Bram Moolenaar <Bram@vim.org>
parents: 23980
diff changeset
371 int mult = FALSE;
21146
465d6e40e79c patch 8.2.1124: Vim9: no line break allowed in :import command
Bram Moolenaar <Bram@vim.org>
parents: 21120
diff changeset
372 garray_T names;
24029
429b4f8d2fac patch 8.2.2556: Vim9: :import with "as" not fully supported
Bram Moolenaar <Bram@vim.org>
parents: 23980
diff changeset
373 garray_T as_names;
25423
3e56078569ca patch 8.2.3248: Vim9: error message for wrong input uses wrong line number
Bram Moolenaar <Bram@vim.org>
parents: 25358
diff changeset
374 long start_lnum = SOURCING_LNUM;
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
375
25282
9bce044c7643 patch 8.2.3178: Vim9: the file name of an :import cannot be an expression
Bram Moolenaar <Bram@vim.org>
parents: 25186
diff changeset
376 tv.v_type = VAR_UNKNOWN;
21146
465d6e40e79c patch 8.2.1124: Vim9: no line break allowed in :import command
Bram Moolenaar <Bram@vim.org>
parents: 21120
diff changeset
377 ga_init2(&names, sizeof(char_u *), 10);
24029
429b4f8d2fac patch 8.2.2556: Vim9: :import with "as" not fully supported
Bram Moolenaar <Bram@vim.org>
parents: 23980
diff changeset
378 ga_init2(&as_names, sizeof(char_u *), 10);
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
379 if (*arg == '{')
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
380 {
21146
465d6e40e79c patch 8.2.1124: Vim9: no line break allowed in :import command
Bram Moolenaar <Bram@vim.org>
parents: 21120
diff changeset
381 // "import {item, item} from ..."
24029
429b4f8d2fac patch 8.2.2556: Vim9: :import with "as" not fully supported
Bram Moolenaar <Bram@vim.org>
parents: 23980
diff changeset
382 mult = TRUE;
21146
465d6e40e79c patch 8.2.1124: Vim9: no line break allowed in :import command
Bram Moolenaar <Bram@vim.org>
parents: 21120
diff changeset
383 arg = skipwhite_and_linebreak(arg + 1, evalarg);
24029
429b4f8d2fac patch 8.2.2556: Vim9: :import with "as" not fully supported
Bram Moolenaar <Bram@vim.org>
parents: 23980
diff changeset
384 }
21146
465d6e40e79c patch 8.2.1124: Vim9: no line break allowed in :import command
Bram Moolenaar <Bram@vim.org>
parents: 21120
diff changeset
385
24029
429b4f8d2fac patch 8.2.2556: Vim9: :import with "as" not fully supported
Bram Moolenaar <Bram@vim.org>
parents: 23980
diff changeset
386 for (;;)
429b4f8d2fac patch 8.2.2556: Vim9: :import with "as" not fully supported
Bram Moolenaar <Bram@vim.org>
parents: 23980
diff changeset
387 {
429b4f8d2fac patch 8.2.2556: Vim9: :import with "as" not fully supported
Bram Moolenaar <Bram@vim.org>
parents: 23980
diff changeset
388 char_u *p = arg;
429b4f8d2fac patch 8.2.2556: Vim9: :import with "as" not fully supported
Bram Moolenaar <Bram@vim.org>
parents: 23980
diff changeset
389 int had_comma = FALSE;
429b4f8d2fac patch 8.2.2556: Vim9: :import with "as" not fully supported
Bram Moolenaar <Bram@vim.org>
parents: 23980
diff changeset
390 char_u *as_name = NULL;
429b4f8d2fac patch 8.2.2556: Vim9: :import with "as" not fully supported
Bram Moolenaar <Bram@vim.org>
parents: 23980
diff changeset
391
429b4f8d2fac patch 8.2.2556: Vim9: :import with "as" not fully supported
Bram Moolenaar <Bram@vim.org>
parents: 23980
diff changeset
392 // accept "*" or "Name"
429b4f8d2fac patch 8.2.2556: Vim9: :import with "as" not fully supported
Bram Moolenaar <Bram@vim.org>
parents: 23980
diff changeset
393 if (!mult && arg[0] == '*' && IS_WHITE_OR_NUL(arg[1]))
429b4f8d2fac patch 8.2.2556: Vim9: :import with "as" not fully supported
Bram Moolenaar <Bram@vim.org>
parents: 23980
diff changeset
394 ++arg;
429b4f8d2fac patch 8.2.2556: Vim9: :import with "as" not fully supported
Bram Moolenaar <Bram@vim.org>
parents: 23980
diff changeset
395 else
21146
465d6e40e79c patch 8.2.1124: Vim9: no line break allowed in :import command
Bram Moolenaar <Bram@vim.org>
parents: 21120
diff changeset
396 while (eval_isnamec(*arg))
465d6e40e79c patch 8.2.1124: Vim9: no line break allowed in :import command
Bram Moolenaar <Bram@vim.org>
parents: 21120
diff changeset
397 ++arg;
24029
429b4f8d2fac patch 8.2.2556: Vim9: :import with "as" not fully supported
Bram Moolenaar <Bram@vim.org>
parents: 23980
diff changeset
398 if (p == arg)
429b4f8d2fac patch 8.2.2556: Vim9: :import with "as" not fully supported
Bram Moolenaar <Bram@vim.org>
parents: 23980
diff changeset
399 break;
429b4f8d2fac patch 8.2.2556: Vim9: :import with "as" not fully supported
Bram Moolenaar <Bram@vim.org>
parents: 23980
diff changeset
400 if (ga_grow(&names, 1) == FAIL || ga_grow(&as_names, 1) == FAIL)
21146
465d6e40e79c patch 8.2.1124: Vim9: no line break allowed in :import command
Bram Moolenaar <Bram@vim.org>
parents: 21120
diff changeset
401 goto erret;
24029
429b4f8d2fac patch 8.2.2556: Vim9: :import with "as" not fully supported
Bram Moolenaar <Bram@vim.org>
parents: 23980
diff changeset
402 ((char_u **)names.ga_data)[names.ga_len] = vim_strnsave(p, arg - p);
429b4f8d2fac patch 8.2.2556: Vim9: :import with "as" not fully supported
Bram Moolenaar <Bram@vim.org>
parents: 23980
diff changeset
403 ++names.ga_len;
21146
465d6e40e79c patch 8.2.1124: Vim9: no line break allowed in :import command
Bram Moolenaar <Bram@vim.org>
parents: 21120
diff changeset
404
24029
429b4f8d2fac patch 8.2.2556: Vim9: :import with "as" not fully supported
Bram Moolenaar <Bram@vim.org>
parents: 23980
diff changeset
405 arg = skipwhite_and_linebreak(arg, evalarg);
21146
465d6e40e79c patch 8.2.1124: Vim9: no line break allowed in :import command
Bram Moolenaar <Bram@vim.org>
parents: 21120
diff changeset
406 if (STRNCMP("as", arg, 2) == 0 && IS_WHITE_OR_NUL(arg[2]))
465d6e40e79c patch 8.2.1124: Vim9: no line break allowed in :import command
Bram Moolenaar <Bram@vim.org>
parents: 21120
diff changeset
407 {
25755
33ebec729787 patch 8.2.3413: Vim9: too many characters are allowed in import name
Bram Moolenaar <Bram@vim.org>
parents: 25626
diff changeset
408 // Skip over "as Name "; no line break allowed after "as".
33ebec729787 patch 8.2.3413: Vim9: too many characters are allowed in import name
Bram Moolenaar <Bram@vim.org>
parents: 25626
diff changeset
409 // Do not allow for ':' and '#'.
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
410 arg = skipwhite(arg + 2);
21146
465d6e40e79c patch 8.2.1124: Vim9: no line break allowed in :import command
Bram Moolenaar <Bram@vim.org>
parents: 21120
diff changeset
411 p = arg;
19511
7e76d5fba19f patch 8.2.0313: Vim9: insufficient script tests
Bram Moolenaar <Bram@vim.org>
parents: 19509
diff changeset
412 if (eval_isnamec1(*arg))
25755
33ebec729787 patch 8.2.3413: Vim9: too many characters are allowed in import name
Bram Moolenaar <Bram@vim.org>
parents: 25626
diff changeset
413 while (ASCII_ISALNUM(*arg) || *arg == '_')
19511
7e76d5fba19f patch 8.2.0313: Vim9: insufficient script tests
Bram Moolenaar <Bram@vim.org>
parents: 19509
diff changeset
414 ++arg;
25755
33ebec729787 patch 8.2.3413: Vim9: too many characters are allowed in import name
Bram Moolenaar <Bram@vim.org>
parents: 25626
diff changeset
415 if (p == arg || !(IS_WHITE_OR_NUL(*arg)
33ebec729787 patch 8.2.3413: Vim9: too many characters are allowed in import name
Bram Moolenaar <Bram@vim.org>
parents: 25626
diff changeset
416 || (mult && (*arg == ',' || *arg == '}'))))
33ebec729787 patch 8.2.3413: Vim9: too many characters are allowed in import name
Bram Moolenaar <Bram@vim.org>
parents: 25626
diff changeset
417 {
33ebec729787 patch 8.2.3413: Vim9: too many characters are allowed in import name
Bram Moolenaar <Bram@vim.org>
parents: 25626
diff changeset
418 semsg(_(e_syntax_error_in_import_str), p);
33ebec729787 patch 8.2.3413: Vim9: too many characters are allowed in import name
Bram Moolenaar <Bram@vim.org>
parents: 25626
diff changeset
419 goto erret;
33ebec729787 patch 8.2.3413: Vim9: too many characters are allowed in import name
Bram Moolenaar <Bram@vim.org>
parents: 25626
diff changeset
420 }
24033
308d29307910 patch 8.2.2558: no error if a lambda argument shadows a variable
Bram Moolenaar <Bram@vim.org>
parents: 24031
diff changeset
421 if (check_defined(p, arg - p, cctx, FALSE) == FAIL)
21146
465d6e40e79c patch 8.2.1124: Vim9: no line break allowed in :import command
Bram Moolenaar <Bram@vim.org>
parents: 21120
diff changeset
422 goto erret;
465d6e40e79c patch 8.2.1124: Vim9: no line break allowed in :import command
Bram Moolenaar <Bram@vim.org>
parents: 21120
diff changeset
423 as_name = vim_strnsave(p, arg - p);
465d6e40e79c patch 8.2.1124: Vim9: no line break allowed in :import command
Bram Moolenaar <Bram@vim.org>
parents: 21120
diff changeset
424 arg = skipwhite_and_linebreak(arg, evalarg);
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
425 }
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
426 else if (*arg_start == '*')
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
427 {
21821
0deb6f96a5a3 patch 8.2.1460: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 21789
diff changeset
428 emsg(_(e_missing_as_after_star));
21146
465d6e40e79c patch 8.2.1124: Vim9: no line break allowed in :import command
Bram Moolenaar <Bram@vim.org>
parents: 21120
diff changeset
429 goto erret;
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
430 }
24029
429b4f8d2fac patch 8.2.2556: Vim9: :import with "as" not fully supported
Bram Moolenaar <Bram@vim.org>
parents: 23980
diff changeset
431 // without "as Name" the as_names entry is NULL
429b4f8d2fac patch 8.2.2556: Vim9: :import with "as" not fully supported
Bram Moolenaar <Bram@vim.org>
parents: 23980
diff changeset
432 ((char_u **)as_names.ga_data)[as_names.ga_len] = as_name;
429b4f8d2fac patch 8.2.2556: Vim9: :import with "as" not fully supported
Bram Moolenaar <Bram@vim.org>
parents: 23980
diff changeset
433 ++as_names.ga_len;
429b4f8d2fac patch 8.2.2556: Vim9: :import with "as" not fully supported
Bram Moolenaar <Bram@vim.org>
parents: 23980
diff changeset
434
429b4f8d2fac patch 8.2.2556: Vim9: :import with "as" not fully supported
Bram Moolenaar <Bram@vim.org>
parents: 23980
diff changeset
435 if (!mult)
429b4f8d2fac patch 8.2.2556: Vim9: :import with "as" not fully supported
Bram Moolenaar <Bram@vim.org>
parents: 23980
diff changeset
436 break;
429b4f8d2fac patch 8.2.2556: Vim9: :import with "as" not fully supported
Bram Moolenaar <Bram@vim.org>
parents: 23980
diff changeset
437 if (*arg == ',')
429b4f8d2fac patch 8.2.2556: Vim9: :import with "as" not fully supported
Bram Moolenaar <Bram@vim.org>
parents: 23980
diff changeset
438 {
429b4f8d2fac patch 8.2.2556: Vim9: :import with "as" not fully supported
Bram Moolenaar <Bram@vim.org>
parents: 23980
diff changeset
439 had_comma = TRUE;
429b4f8d2fac patch 8.2.2556: Vim9: :import with "as" not fully supported
Bram Moolenaar <Bram@vim.org>
parents: 23980
diff changeset
440 ++arg;
429b4f8d2fac patch 8.2.2556: Vim9: :import with "as" not fully supported
Bram Moolenaar <Bram@vim.org>
parents: 23980
diff changeset
441 }
429b4f8d2fac patch 8.2.2556: Vim9: :import with "as" not fully supported
Bram Moolenaar <Bram@vim.org>
parents: 23980
diff changeset
442 arg = skipwhite_and_linebreak(arg, evalarg);
429b4f8d2fac patch 8.2.2556: Vim9: :import with "as" not fully supported
Bram Moolenaar <Bram@vim.org>
parents: 23980
diff changeset
443 if (*arg == '}')
429b4f8d2fac patch 8.2.2556: Vim9: :import with "as" not fully supported
Bram Moolenaar <Bram@vim.org>
parents: 23980
diff changeset
444 {
429b4f8d2fac patch 8.2.2556: Vim9: :import with "as" not fully supported
Bram Moolenaar <Bram@vim.org>
parents: 23980
diff changeset
445 ++arg;
429b4f8d2fac patch 8.2.2556: Vim9: :import with "as" not fully supported
Bram Moolenaar <Bram@vim.org>
parents: 23980
diff changeset
446 break;
429b4f8d2fac patch 8.2.2556: Vim9: :import with "as" not fully supported
Bram Moolenaar <Bram@vim.org>
parents: 23980
diff changeset
447 }
429b4f8d2fac patch 8.2.2556: Vim9: :import with "as" not fully supported
Bram Moolenaar <Bram@vim.org>
parents: 23980
diff changeset
448 if (!had_comma)
429b4f8d2fac patch 8.2.2556: Vim9: :import with "as" not fully supported
Bram Moolenaar <Bram@vim.org>
parents: 23980
diff changeset
449 {
429b4f8d2fac patch 8.2.2556: Vim9: :import with "as" not fully supported
Bram Moolenaar <Bram@vim.org>
parents: 23980
diff changeset
450 emsg(_(e_missing_comma_in_import));
429b4f8d2fac patch 8.2.2556: Vim9: :import with "as" not fully supported
Bram Moolenaar <Bram@vim.org>
parents: 23980
diff changeset
451 goto erret;
429b4f8d2fac patch 8.2.2556: Vim9: :import with "as" not fully supported
Bram Moolenaar <Bram@vim.org>
parents: 23980
diff changeset
452 }
429b4f8d2fac patch 8.2.2556: Vim9: :import with "as" not fully supported
Bram Moolenaar <Bram@vim.org>
parents: 23980
diff changeset
453 }
429b4f8d2fac patch 8.2.2556: Vim9: :import with "as" not fully supported
Bram Moolenaar <Bram@vim.org>
parents: 23980
diff changeset
454 arg = skipwhite_and_linebreak(arg, evalarg);
429b4f8d2fac patch 8.2.2556: Vim9: :import with "as" not fully supported
Bram Moolenaar <Bram@vim.org>
parents: 23980
diff changeset
455
429b4f8d2fac patch 8.2.2556: Vim9: :import with "as" not fully supported
Bram Moolenaar <Bram@vim.org>
parents: 23980
diff changeset
456 if (names.ga_len == 0)
429b4f8d2fac patch 8.2.2556: Vim9: :import with "as" not fully supported
Bram Moolenaar <Bram@vim.org>
parents: 23980
diff changeset
457 {
25755
33ebec729787 patch 8.2.3413: Vim9: too many characters are allowed in import name
Bram Moolenaar <Bram@vim.org>
parents: 25626
diff changeset
458 semsg(_(e_syntax_error_in_import_str), arg_start);
24029
429b4f8d2fac patch 8.2.2556: Vim9: :import with "as" not fully supported
Bram Moolenaar <Bram@vim.org>
parents: 23980
diff changeset
459 goto erret;
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
460 }
21146
465d6e40e79c patch 8.2.1124: Vim9: no line break allowed in :import command
Bram Moolenaar <Bram@vim.org>
parents: 21120
diff changeset
461
465d6e40e79c patch 8.2.1124: Vim9: no line break allowed in :import command
Bram Moolenaar <Bram@vim.org>
parents: 21120
diff changeset
462 if (STRNCMP("from", arg, 4) != 0 || !IS_WHITE_OR_NUL(arg[4]))
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
463 {
21821
0deb6f96a5a3 patch 8.2.1460: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 21789
diff changeset
464 emsg(_(e_missing_from));
21146
465d6e40e79c patch 8.2.1124: Vim9: no line break allowed in :import command
Bram Moolenaar <Bram@vim.org>
parents: 21120
diff changeset
465 goto erret;
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
466 }
21146
465d6e40e79c patch 8.2.1124: Vim9: no line break allowed in :import command
Bram Moolenaar <Bram@vim.org>
parents: 21120
diff changeset
467
25282
9bce044c7643 patch 8.2.3178: Vim9: the file name of an :import cannot be an expression
Bram Moolenaar <Bram@vim.org>
parents: 25186
diff changeset
468 // The name of the file can be an expression, which must evaluate to a
9bce044c7643 patch 8.2.3178: Vim9: the file name of an :import cannot be an expression
Bram Moolenaar <Bram@vim.org>
parents: 25186
diff changeset
469 // string.
21148
667192c5938b patch 8.2.1125: Vim9: double quote can be a string or a comment
Bram Moolenaar <Bram@vim.org>
parents: 21146
diff changeset
470 arg = skipwhite_and_linebreak(arg + 4, evalarg);
25282
9bce044c7643 patch 8.2.3178: Vim9: the file name of an :import cannot be an expression
Bram Moolenaar <Bram@vim.org>
parents: 25186
diff changeset
471 ret = eval0(arg, &tv, NULL, evalarg);
9bce044c7643 patch 8.2.3178: Vim9: the file name of an :import cannot be an expression
Bram Moolenaar <Bram@vim.org>
parents: 25186
diff changeset
472 if (ret == FAIL)
9bce044c7643 patch 8.2.3178: Vim9: the file name of an :import cannot be an expression
Bram Moolenaar <Bram@vim.org>
parents: 25186
diff changeset
473 goto erret;
9bce044c7643 patch 8.2.3178: Vim9: the file name of an :import cannot be an expression
Bram Moolenaar <Bram@vim.org>
parents: 25186
diff changeset
474 if (tv.v_type != VAR_STRING
9bce044c7643 patch 8.2.3178: Vim9: the file name of an :import cannot be an expression
Bram Moolenaar <Bram@vim.org>
parents: 25186
diff changeset
475 || tv.vval.v_string == NULL || *tv.vval.v_string == NUL)
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
476 {
21821
0deb6f96a5a3 patch 8.2.1460: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 21789
diff changeset
477 emsg(_(e_invalid_string_after_from));
21146
465d6e40e79c patch 8.2.1124: Vim9: no line break allowed in :import command
Bram Moolenaar <Bram@vim.org>
parents: 21120
diff changeset
478 goto erret;
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
479 }
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
480 cmd_end = arg;
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
481
25423
3e56078569ca patch 8.2.3248: Vim9: error message for wrong input uses wrong line number
Bram Moolenaar <Bram@vim.org>
parents: 25358
diff changeset
482 // Give error messages for the start of the line.
3e56078569ca patch 8.2.3248: Vim9: error message for wrong input uses wrong line number
Bram Moolenaar <Bram@vim.org>
parents: 25358
diff changeset
483 SOURCING_LNUM = start_lnum;
3e56078569ca patch 8.2.3248: Vim9: error message for wrong input uses wrong line number
Bram Moolenaar <Bram@vim.org>
parents: 25358
diff changeset
484
21146
465d6e40e79c patch 8.2.1124: Vim9: no line break allowed in :import command
Bram Moolenaar <Bram@vim.org>
parents: 21120
diff changeset
485 /*
465d6e40e79c patch 8.2.1124: Vim9: no line break allowed in :import command
Bram Moolenaar <Bram@vim.org>
parents: 21120
diff changeset
486 * find script file
465d6e40e79c patch 8.2.1124: Vim9: no line break allowed in :import command
Bram Moolenaar <Bram@vim.org>
parents: 21120
diff changeset
487 */
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
488 if (*tv.vval.v_string == '.')
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
489 {
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
490 size_t len;
19191
133ef7ba4e4e patch 8.2.0154: reallocating the list of scripts is inefficient
Bram Moolenaar <Bram@vim.org>
parents: 19181
diff changeset
491 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
492 char_u *tail = gettail(si->sn_name);
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
493 char_u *from_name;
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
494
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
495 // Relative to current script: "./name.vim", "../../name.vim".
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
496 len = STRLEN(si->sn_name) - STRLEN(tail) + STRLEN(tv.vval.v_string) + 2;
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
497 from_name = alloc((int)len);
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
498 if (from_name == NULL)
21146
465d6e40e79c patch 8.2.1124: Vim9: no line break allowed in :import command
Bram Moolenaar <Bram@vim.org>
parents: 21120
diff changeset
499 goto erret;
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
500 vim_strncpy(from_name, si->sn_name, tail - si->sn_name);
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
501 add_pathsep(from_name);
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
502 STRCAT(from_name, tv.vval.v_string);
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
503 simplify_filename(from_name);
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
504
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
505 res = do_source(from_name, FALSE, DOSO_NONE, &sid);
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
506 vim_free(from_name);
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
507 }
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
508 else if (mch_isFullName(tv.vval.v_string))
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
509 {
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
510 // Absolute path: "/tmp/name.vim"
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
511 res = do_source(tv.vval.v_string, FALSE, DOSO_NONE, &sid);
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
512 }
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
513 else
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
514 {
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
515 size_t len = 7 + STRLEN(tv.vval.v_string) + 1;
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
516 char_u *from_name;
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
517
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
518 // Find file in "import" subdirs in 'runtimepath'.
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
519 from_name = alloc((int)len);
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
520 if (from_name == NULL)
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
521 {
21146
465d6e40e79c patch 8.2.1124: Vim9: no line break allowed in :import command
Bram Moolenaar <Bram@vim.org>
parents: 21120
diff changeset
522 goto erret;
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
523 }
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
524 vim_snprintf((char *)from_name, len, "import/%s", tv.vval.v_string);
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
525 res = source_in_path(p_rtp, from_name, DIP_NOAFTER, &sid);
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
526 vim_free(from_name);
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
527 }
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
528
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
529 if (res == FAIL || sid <= 0)
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
530 {
21821
0deb6f96a5a3 patch 8.2.1460: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 21789
diff changeset
531 semsg(_(e_could_not_import_str), tv.vval.v_string);
21146
465d6e40e79c patch 8.2.1124: Vim9: no line break allowed in :import command
Bram Moolenaar <Bram@vim.org>
parents: 21120
diff changeset
532 goto erret;
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
533 }
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
534
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
535 if (*arg_start == '*')
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
536 {
24029
429b4f8d2fac patch 8.2.2556: Vim9: :import with "as" not fully supported
Bram Moolenaar <Bram@vim.org>
parents: 23980
diff changeset
537 imported_T *imported;
429b4f8d2fac patch 8.2.2556: Vim9: :import with "as" not fully supported
Bram Moolenaar <Bram@vim.org>
parents: 23980
diff changeset
538 char_u *as_name = ((char_u **)as_names.ga_data)[0];
23364
17a0e32eefd4 patch 8.2.2225: Vim9: error when using :import in legacy script twice
Bram Moolenaar <Bram@vim.org>
parents: 23362
diff changeset
539
24029
429b4f8d2fac patch 8.2.2556: Vim9: :import with "as" not fully supported
Bram Moolenaar <Bram@vim.org>
parents: 23980
diff changeset
540 // "import * as That"
23364
17a0e32eefd4 patch 8.2.2225: Vim9: error when using :import in legacy script twice
Bram Moolenaar <Bram@vim.org>
parents: 23362
diff changeset
541 imported = find_imported(as_name, STRLEN(as_name), cctx);
17a0e32eefd4 patch 8.2.2225: Vim9: error when using :import in legacy script twice
Bram Moolenaar <Bram@vim.org>
parents: 23362
diff changeset
542 if (imported != NULL && imported->imp_sid == sid)
17a0e32eefd4 patch 8.2.2225: Vim9: error when using :import in legacy script twice
Bram Moolenaar <Bram@vim.org>
parents: 23362
diff changeset
543 {
17a0e32eefd4 patch 8.2.2225: Vim9: error when using :import in legacy script twice
Bram Moolenaar <Bram@vim.org>
parents: 23362
diff changeset
544 if (imported->imp_flags & IMP_FLAGS_RELOAD)
17a0e32eefd4 patch 8.2.2225: Vim9: error when using :import in legacy script twice
Bram Moolenaar <Bram@vim.org>
parents: 23362
diff changeset
545 // import already defined on a previous script load
17a0e32eefd4 patch 8.2.2225: Vim9: error when using :import in legacy script twice
Bram Moolenaar <Bram@vim.org>
parents: 23362
diff changeset
546 imported->imp_flags &= ~IMP_FLAGS_RELOAD;
17a0e32eefd4 patch 8.2.2225: Vim9: error when using :import in legacy script twice
Bram Moolenaar <Bram@vim.org>
parents: 23362
diff changeset
547 else
17a0e32eefd4 patch 8.2.2225: Vim9: error when using :import in legacy script twice
Bram Moolenaar <Bram@vim.org>
parents: 23362
diff changeset
548 {
17a0e32eefd4 patch 8.2.2225: Vim9: error when using :import in legacy script twice
Bram Moolenaar <Bram@vim.org>
parents: 23362
diff changeset
549 semsg(_(e_name_already_defined_str), as_name);
17a0e32eefd4 patch 8.2.2225: Vim9: error when using :import in legacy script twice
Bram Moolenaar <Bram@vim.org>
parents: 23362
diff changeset
550 goto erret;
17a0e32eefd4 patch 8.2.2225: Vim9: error when using :import in legacy script twice
Bram Moolenaar <Bram@vim.org>
parents: 23362
diff changeset
551 }
17a0e32eefd4 patch 8.2.2225: Vim9: error when using :import in legacy script twice
Bram Moolenaar <Bram@vim.org>
parents: 23362
diff changeset
552 }
17a0e32eefd4 patch 8.2.2225: Vim9: error when using :import in legacy script twice
Bram Moolenaar <Bram@vim.org>
parents: 23362
diff changeset
553
17a0e32eefd4 patch 8.2.2225: Vim9: error when using :import in legacy script twice
Bram Moolenaar <Bram@vim.org>
parents: 23362
diff changeset
554 imported = new_imported(gap != NULL ? gap
19191
133ef7ba4e4e patch 8.2.0154: reallocating the list of scripts is inefficient
Bram Moolenaar <Bram@vim.org>
parents: 19181
diff changeset
555 : &SCRIPT_ITEM(import_sid)->sn_imports);
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
556 if (imported == NULL)
21146
465d6e40e79c patch 8.2.1124: Vim9: no line break allowed in :import command
Bram Moolenaar <Bram@vim.org>
parents: 21120
diff changeset
557 goto erret;
465d6e40e79c patch 8.2.1124: Vim9: no line break allowed in :import command
Bram Moolenaar <Bram@vim.org>
parents: 21120
diff changeset
558 imported->imp_name = as_name;
24029
429b4f8d2fac patch 8.2.2556: Vim9: :import with "as" not fully supported
Bram Moolenaar <Bram@vim.org>
parents: 23980
diff changeset
559 ((char_u **)as_names.ga_data)[0] = NULL;
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
560 imported->imp_sid = sid;
23364
17a0e32eefd4 patch 8.2.2225: Vim9: error when using :import in legacy script twice
Bram Moolenaar <Bram@vim.org>
parents: 23362
diff changeset
561 imported->imp_flags = IMP_FLAGS_STAR;
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
562 }
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
563 else
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
564 {
21146
465d6e40e79c patch 8.2.1124: Vim9: no line break allowed in :import command
Bram Moolenaar <Bram@vim.org>
parents: 21120
diff changeset
565 int i;
465d6e40e79c patch 8.2.1124: Vim9: no line break allowed in :import command
Bram Moolenaar <Bram@vim.org>
parents: 21120
diff changeset
566
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
567 arg = arg_start;
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
568 if (*arg == '{')
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
569 arg = skipwhite(arg + 1);
21146
465d6e40e79c patch 8.2.1124: Vim9: no line break allowed in :import command
Bram Moolenaar <Bram@vim.org>
parents: 21120
diff changeset
570 for (i = 0; i < names.ga_len; ++i)
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
571 {
21146
465d6e40e79c patch 8.2.1124: Vim9: no line break allowed in :import command
Bram Moolenaar <Bram@vim.org>
parents: 21120
diff changeset
572 char_u *name = ((char_u **)names.ga_data)[i];
24029
429b4f8d2fac patch 8.2.2556: Vim9: :import with "as" not fully supported
Bram Moolenaar <Bram@vim.org>
parents: 23980
diff changeset
573 char_u *as_name = ((char_u **)as_names.ga_data)[i];
23364
17a0e32eefd4 patch 8.2.2225: Vim9: error when using :import in legacy script twice
Bram Moolenaar <Bram@vim.org>
parents: 23362
diff changeset
574 size_t len = STRLEN(name);
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
575 int idx;
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
576 imported_T *imported;
19509
17f0d6dc6a73 patch 8.2.0312: Vim9: insufficient script tests
Bram Moolenaar <Bram@vim.org>
parents: 19227
diff changeset
577 ufunc_T *ufunc = NULL;
17f0d6dc6a73 patch 8.2.0312: Vim9: insufficient script tests
Bram Moolenaar <Bram@vim.org>
parents: 19227
diff changeset
578 type_T *type;
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
579
24112
0346a59ed5bf patch 8.2.2597: Vim9: "import * as" does not work at script level
Bram Moolenaar <Bram@vim.org>
parents: 24079
diff changeset
580 idx = find_exported(sid, name, &ufunc, &type, cctx, TRUE);
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
581
19509
17f0d6dc6a73 patch 8.2.0312: Vim9: insufficient script tests
Bram Moolenaar <Bram@vim.org>
parents: 19227
diff changeset
582 if (idx < 0 && ufunc == NULL)
21146
465d6e40e79c patch 8.2.1124: Vim9: no line break allowed in :import command
Bram Moolenaar <Bram@vim.org>
parents: 21120
diff changeset
583 goto erret;
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
584
25186
0a3b1c66d3f2 patch 8.2.3129: Vim9: imported uninitialized list does not get type checked
Bram Moolenaar <Bram@vim.org>
parents: 25184
diff changeset
585 // If already imported with the same properties and the
23364
17a0e32eefd4 patch 8.2.2225: Vim9: error when using :import in legacy script twice
Bram Moolenaar <Bram@vim.org>
parents: 23362
diff changeset
586 // IMP_FLAGS_RELOAD set then we keep that entry. Otherwise create
17a0e32eefd4 patch 8.2.2225: Vim9: error when using :import in legacy script twice
Bram Moolenaar <Bram@vim.org>
parents: 23362
diff changeset
587 // a new one (and give an error for an existing import).
17a0e32eefd4 patch 8.2.2225: Vim9: error when using :import in legacy script twice
Bram Moolenaar <Bram@vim.org>
parents: 23362
diff changeset
588 imported = find_imported(name, len, cctx);
17a0e32eefd4 patch 8.2.2225: Vim9: error when using :import in legacy script twice
Bram Moolenaar <Bram@vim.org>
parents: 23362
diff changeset
589 if (imported != NULL
17a0e32eefd4 patch 8.2.2225: Vim9: error when using :import in legacy script twice
Bram Moolenaar <Bram@vim.org>
parents: 23362
diff changeset
590 && (imported->imp_flags & IMP_FLAGS_RELOAD)
17a0e32eefd4 patch 8.2.2225: Vim9: error when using :import in legacy script twice
Bram Moolenaar <Bram@vim.org>
parents: 23362
diff changeset
591 && imported->imp_sid == sid
17a0e32eefd4 patch 8.2.2225: Vim9: error when using :import in legacy script twice
Bram Moolenaar <Bram@vim.org>
parents: 23362
diff changeset
592 && (idx >= 0
25425
effe5f2b4d01 patch 8.2.3249: Vim9: error for re-imported function with default argument
Bram Moolenaar <Bram@vim.org>
parents: 25423
diff changeset
593 ? (equal_type(imported->imp_type, type, 0)
23364
17a0e32eefd4 patch 8.2.2225: Vim9: error when using :import in legacy script twice
Bram Moolenaar <Bram@vim.org>
parents: 23362
diff changeset
594 && imported->imp_var_vals_idx == idx)
25425
effe5f2b4d01 patch 8.2.3249: Vim9: error for re-imported function with default argument
Bram Moolenaar <Bram@vim.org>
parents: 25423
diff changeset
595 : (equal_type(imported->imp_type, ufunc->uf_func_type,
effe5f2b4d01 patch 8.2.3249: Vim9: error for re-imported function with default argument
Bram Moolenaar <Bram@vim.org>
parents: 25423
diff changeset
596 ETYPE_ARG_UNKNOWN)
23364
17a0e32eefd4 patch 8.2.2225: Vim9: error when using :import in legacy script twice
Bram Moolenaar <Bram@vim.org>
parents: 23362
diff changeset
597 && STRCMP(imported->imp_funcname,
17a0e32eefd4 patch 8.2.2225: Vim9: error when using :import in legacy script twice
Bram Moolenaar <Bram@vim.org>
parents: 23362
diff changeset
598 ufunc->uf_name) == 0)))
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
599 {
23364
17a0e32eefd4 patch 8.2.2225: Vim9: error when using :import in legacy script twice
Bram Moolenaar <Bram@vim.org>
parents: 23362
diff changeset
600 imported->imp_flags &= ~IMP_FLAGS_RELOAD;
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
601 }
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
602 else
21473
8bcd1ee2630b patch 8.2.1287: Vim9: crash when using an imported function
Bram Moolenaar <Bram@vim.org>
parents: 21449
diff changeset
603 {
24469
e5db23a8ad98 patch 8.2.2774: Vim9: cannot import an existing name even when using "as"
Bram Moolenaar <Bram@vim.org>
parents: 24438
diff changeset
604 if (as_name == NULL
e5db23a8ad98 patch 8.2.2774: Vim9: cannot import an existing name even when using "as"
Bram Moolenaar <Bram@vim.org>
parents: 24438
diff changeset
605 && check_defined(name, len, cctx, FALSE) == FAIL)
23364
17a0e32eefd4 patch 8.2.2225: Vim9: error when using :import in legacy script twice
Bram Moolenaar <Bram@vim.org>
parents: 23362
diff changeset
606 goto erret;
17a0e32eefd4 patch 8.2.2225: Vim9: error when using :import in legacy script twice
Bram Moolenaar <Bram@vim.org>
parents: 23362
diff changeset
607
17a0e32eefd4 patch 8.2.2225: Vim9: error when using :import in legacy script twice
Bram Moolenaar <Bram@vim.org>
parents: 23362
diff changeset
608 imported = new_imported(gap != NULL ? gap
17a0e32eefd4 patch 8.2.2225: Vim9: error when using :import in legacy script twice
Bram Moolenaar <Bram@vim.org>
parents: 23362
diff changeset
609 : &SCRIPT_ITEM(import_sid)->sn_imports);
17a0e32eefd4 patch 8.2.2225: Vim9: error when using :import in legacy script twice
Bram Moolenaar <Bram@vim.org>
parents: 23362
diff changeset
610 if (imported == NULL)
17a0e32eefd4 patch 8.2.2225: Vim9: error when using :import in legacy script twice
Bram Moolenaar <Bram@vim.org>
parents: 23362
diff changeset
611 goto erret;
17a0e32eefd4 patch 8.2.2225: Vim9: error when using :import in legacy script twice
Bram Moolenaar <Bram@vim.org>
parents: 23362
diff changeset
612
24029
429b4f8d2fac patch 8.2.2556: Vim9: :import with "as" not fully supported
Bram Moolenaar <Bram@vim.org>
parents: 23980
diff changeset
613 if (as_name == NULL)
429b4f8d2fac patch 8.2.2556: Vim9: :import with "as" not fully supported
Bram Moolenaar <Bram@vim.org>
parents: 23980
diff changeset
614 {
429b4f8d2fac patch 8.2.2556: Vim9: :import with "as" not fully supported
Bram Moolenaar <Bram@vim.org>
parents: 23980
diff changeset
615 imported->imp_name = name;
429b4f8d2fac patch 8.2.2556: Vim9: :import with "as" not fully supported
Bram Moolenaar <Bram@vim.org>
parents: 23980
diff changeset
616 ((char_u **)names.ga_data)[i] = NULL;
24033
308d29307910 patch 8.2.2558: no error if a lambda argument shadows a variable
Bram Moolenaar <Bram@vim.org>
parents: 24031
diff changeset
617 }
24029
429b4f8d2fac patch 8.2.2556: Vim9: :import with "as" not fully supported
Bram Moolenaar <Bram@vim.org>
parents: 23980
diff changeset
618 else
429b4f8d2fac patch 8.2.2556: Vim9: :import with "as" not fully supported
Bram Moolenaar <Bram@vim.org>
parents: 23980
diff changeset
619 {
429b4f8d2fac patch 8.2.2556: Vim9: :import with "as" not fully supported
Bram Moolenaar <Bram@vim.org>
parents: 23980
diff changeset
620 // "import This as That ..."
429b4f8d2fac patch 8.2.2556: Vim9: :import with "as" not fully supported
Bram Moolenaar <Bram@vim.org>
parents: 23980
diff changeset
621 imported->imp_name = as_name;
429b4f8d2fac patch 8.2.2556: Vim9: :import with "as" not fully supported
Bram Moolenaar <Bram@vim.org>
parents: 23980
diff changeset
622 ((char_u **)as_names.ga_data)[i] = NULL;
429b4f8d2fac patch 8.2.2556: Vim9: :import with "as" not fully supported
Bram Moolenaar <Bram@vim.org>
parents: 23980
diff changeset
623 }
23364
17a0e32eefd4 patch 8.2.2225: Vim9: error when using :import in legacy script twice
Bram Moolenaar <Bram@vim.org>
parents: 23362
diff changeset
624 imported->imp_sid = sid;
17a0e32eefd4 patch 8.2.2225: Vim9: error when using :import in legacy script twice
Bram Moolenaar <Bram@vim.org>
parents: 23362
diff changeset
625 if (idx >= 0)
17a0e32eefd4 patch 8.2.2225: Vim9: error when using :import in legacy script twice
Bram Moolenaar <Bram@vim.org>
parents: 23362
diff changeset
626 {
17a0e32eefd4 patch 8.2.2225: Vim9: error when using :import in legacy script twice
Bram Moolenaar <Bram@vim.org>
parents: 23362
diff changeset
627 imported->imp_type = type;
17a0e32eefd4 patch 8.2.2225: Vim9: error when using :import in legacy script twice
Bram Moolenaar <Bram@vim.org>
parents: 23362
diff changeset
628 imported->imp_var_vals_idx = idx;
17a0e32eefd4 patch 8.2.2225: Vim9: error when using :import in legacy script twice
Bram Moolenaar <Bram@vim.org>
parents: 23362
diff changeset
629 }
17a0e32eefd4 patch 8.2.2225: Vim9: error when using :import in legacy script twice
Bram Moolenaar <Bram@vim.org>
parents: 23362
diff changeset
630 else
17a0e32eefd4 patch 8.2.2225: Vim9: error when using :import in legacy script twice
Bram Moolenaar <Bram@vim.org>
parents: 23362
diff changeset
631 {
17a0e32eefd4 patch 8.2.2225: Vim9: error when using :import in legacy script twice
Bram Moolenaar <Bram@vim.org>
parents: 23362
diff changeset
632 imported->imp_type = ufunc->uf_func_type;
17a0e32eefd4 patch 8.2.2225: Vim9: error when using :import in legacy script twice
Bram Moolenaar <Bram@vim.org>
parents: 23362
diff changeset
633 imported->imp_funcname = ufunc->uf_name;
17a0e32eefd4 patch 8.2.2225: Vim9: error when using :import in legacy script twice
Bram Moolenaar <Bram@vim.org>
parents: 23362
diff changeset
634 }
21473
8bcd1ee2630b patch 8.2.1287: Vim9: crash when using an imported function
Bram Moolenaar <Bram@vim.org>
parents: 21449
diff changeset
635 }
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
636 }
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
637 }
21146
465d6e40e79c patch 8.2.1124: Vim9: no line break allowed in :import command
Bram Moolenaar <Bram@vim.org>
parents: 21120
diff changeset
638 erret:
25282
9bce044c7643 patch 8.2.3178: Vim9: the file name of an :import cannot be an expression
Bram Moolenaar <Bram@vim.org>
parents: 25186
diff changeset
639 clear_tv(&tv);
21146
465d6e40e79c patch 8.2.1124: Vim9: no line break allowed in :import command
Bram Moolenaar <Bram@vim.org>
parents: 21120
diff changeset
640 ga_clear_strings(&names);
24029
429b4f8d2fac patch 8.2.2556: Vim9: :import with "as" not fully supported
Bram Moolenaar <Bram@vim.org>
parents: 23980
diff changeset
641 ga_clear_strings(&as_names);
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
642 return cmd_end;
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
643 }
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
644
20840
0600ab7b9f09 patch 8.2.0972: Vim9 script variable declarations need a type
Bram Moolenaar <Bram@vim.org>
parents: 20816
diff changeset
645 /*
25567
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
646 * ":import Item from 'filename'"
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
647 * ":import Item as Alias from 'filename'"
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
648 * ":import {Item} from 'filename'".
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
649 * ":import {Item as Alias} from 'filename'"
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
650 * ":import {Item, Item} from 'filename'"
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
651 * ":import {Item, Item as Alias} from 'filename'"
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
652 *
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
653 * ":import * as Name from 'filename'"
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
654 */
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
655 void
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
656 ex_import(exarg_T *eap)
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
657 {
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
658 char_u *cmd_end;
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
659 evalarg_T evalarg;
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
660
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
661 if (!getline_equal(eap->getline, eap->cookie, getsourceline))
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
662 {
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
663 emsg(_(e_import_can_only_be_used_in_script));
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
664 return;
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
665 }
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
666 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
667
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
668 cmd_end = handle_import(eap->arg, NULL, current_sctx.sc_sid,
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
669 &evalarg, NULL);
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
670 if (cmd_end != NULL)
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
671 set_nextcmd(eap, cmd_end);
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
672 clear_evalarg(&evalarg, eap);
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
673 }
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
674
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
675 /*
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
676 * Find an exported item in "sid" matching the name at "*argp".
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
677 * When it is a variable return the index.
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
678 * When it is a user function return "*ufunc".
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
679 * When not found returns -1 and "*ufunc" is NULL.
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
680 */
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
681 int
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
682 find_exported(
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
683 int sid,
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
684 char_u *name,
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
685 ufunc_T **ufunc,
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
686 type_T **type,
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
687 cctx_T *cctx,
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
688 int verbose)
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
689 {
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
690 int idx = -1;
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
691 svar_T *sv;
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
692 scriptitem_T *script = SCRIPT_ITEM(sid);
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
693
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
694 // Find name in "script".
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
695 idx = get_script_item_idx(sid, name, 0, cctx);
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
696 if (idx >= 0)
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
697 {
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
698 sv = ((svar_T *)script->sn_var_vals.ga_data) + idx;
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
699 if (!sv->sv_export)
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
700 {
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
701 if (verbose)
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
702 semsg(_(e_item_not_exported_in_script_str), name);
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
703 return -1;
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
704 }
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
705 *type = sv->sv_type;
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
706 *ufunc = NULL;
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
707 }
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
708 else
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
709 {
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
710 char_u buffer[200];
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
711 char_u *funcname;
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
712
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
713 // it could be a user function.
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
714 if (STRLEN(name) < sizeof(buffer) - 15)
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
715 funcname = buffer;
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
716 else
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
717 {
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
718 funcname = alloc(STRLEN(name) + 15);
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
719 if (funcname == NULL)
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
720 return -1;
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
721 }
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
722 funcname[0] = K_SPECIAL;
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
723 funcname[1] = KS_EXTRA;
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
724 funcname[2] = (int)KE_SNR;
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
725 sprintf((char *)funcname + 3, "%ld_%s", (long)sid, name);
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
726 *ufunc = find_func(funcname, FALSE, NULL);
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
727 if (funcname != buffer)
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
728 vim_free(funcname);
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
729
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
730 if (*ufunc == NULL)
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
731 {
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
732 if (verbose)
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
733 semsg(_(e_item_not_found_in_script_str), name);
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
734 return -1;
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
735 }
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
736 else if (((*ufunc)->uf_flags & FC_EXPORT) == 0)
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
737 {
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
738 if (verbose)
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
739 semsg(_(e_item_not_exported_in_script_str), name);
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
740 *ufunc = NULL;
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
741 return -1;
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
742 }
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
743 }
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
744
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
745 return idx;
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
746 }
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
747
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
748 /*
20840
0600ab7b9f09 patch 8.2.0972: Vim9 script variable declarations need a type
Bram Moolenaar <Bram@vim.org>
parents: 20816
diff changeset
749 * Declare a script-local variable without init: "let var: type".
0600ab7b9f09 patch 8.2.0972: Vim9 script variable declarations need a type
Bram Moolenaar <Bram@vim.org>
parents: 20816
diff changeset
750 * "const" is an error since the value is missing.
0600ab7b9f09 patch 8.2.0972: Vim9 script variable declarations need a type
Bram Moolenaar <Bram@vim.org>
parents: 20816
diff changeset
751 * Returns a pointer to after the type.
0600ab7b9f09 patch 8.2.0972: Vim9 script variable declarations need a type
Bram Moolenaar <Bram@vim.org>
parents: 20816
diff changeset
752 */
0600ab7b9f09 patch 8.2.0972: Vim9 script variable declarations need a type
Bram Moolenaar <Bram@vim.org>
parents: 20816
diff changeset
753 char_u *
0600ab7b9f09 patch 8.2.0972: Vim9 script variable declarations need a type
Bram Moolenaar <Bram@vim.org>
parents: 20816
diff changeset
754 vim9_declare_scriptvar(exarg_T *eap, char_u *arg)
0600ab7b9f09 patch 8.2.0972: Vim9 script variable declarations need a type
Bram Moolenaar <Bram@vim.org>
parents: 20816
diff changeset
755 {
0600ab7b9f09 patch 8.2.0972: Vim9 script variable declarations need a type
Bram Moolenaar <Bram@vim.org>
parents: 20816
diff changeset
756 char_u *p;
0600ab7b9f09 patch 8.2.0972: Vim9 script variable declarations need a type
Bram Moolenaar <Bram@vim.org>
parents: 20816
diff changeset
757 char_u *name;
0600ab7b9f09 patch 8.2.0972: Vim9 script variable declarations need a type
Bram Moolenaar <Bram@vim.org>
parents: 20816
diff changeset
758 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
0600ab7b9f09 patch 8.2.0972: Vim9 script variable declarations need a type
Bram Moolenaar <Bram@vim.org>
parents: 20816
diff changeset
759 type_T *type;
0600ab7b9f09 patch 8.2.0972: Vim9 script variable declarations need a type
Bram Moolenaar <Bram@vim.org>
parents: 20816
diff changeset
760 typval_T init_tv;
0600ab7b9f09 patch 8.2.0972: Vim9 script variable declarations need a type
Bram Moolenaar <Bram@vim.org>
parents: 20816
diff changeset
761
22391
a9fb7efa31d6 patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents: 22250
diff changeset
762 if (eap->cmdidx == CMD_final || eap->cmdidx == CMD_const)
20840
0600ab7b9f09 patch 8.2.0972: Vim9 script variable declarations need a type
Bram Moolenaar <Bram@vim.org>
parents: 20816
diff changeset
763 {
22391
a9fb7efa31d6 patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents: 22250
diff changeset
764 if (eap->cmdidx == CMD_final)
a9fb7efa31d6 patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents: 22250
diff changeset
765 emsg(_(e_final_requires_a_value));
a9fb7efa31d6 patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents: 22250
diff changeset
766 else
a9fb7efa31d6 patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents: 22250
diff changeset
767 emsg(_(e_const_requires_a_value));
20840
0600ab7b9f09 patch 8.2.0972: Vim9 script variable declarations need a type
Bram Moolenaar <Bram@vim.org>
parents: 20816
diff changeset
768 return arg + STRLEN(arg);
0600ab7b9f09 patch 8.2.0972: Vim9 script variable declarations need a type
Bram Moolenaar <Bram@vim.org>
parents: 20816
diff changeset
769 }
0600ab7b9f09 patch 8.2.0972: Vim9 script variable declarations need a type
Bram Moolenaar <Bram@vim.org>
parents: 20816
diff changeset
770
0600ab7b9f09 patch 8.2.0972: Vim9 script variable declarations need a type
Bram Moolenaar <Bram@vim.org>
parents: 20816
diff changeset
771 // Check for valid starting character.
0600ab7b9f09 patch 8.2.0972: Vim9 script variable declarations need a type
Bram Moolenaar <Bram@vim.org>
parents: 20816
diff changeset
772 if (!eval_isnamec1(*arg))
0600ab7b9f09 patch 8.2.0972: Vim9 script variable declarations need a type
Bram Moolenaar <Bram@vim.org>
parents: 20816
diff changeset
773 {
26865
bce848ec8b1b patch 8.2.3961: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 26662
diff changeset
774 semsg(_(e_invalid_argument_str), arg);
20840
0600ab7b9f09 patch 8.2.0972: Vim9 script variable declarations need a type
Bram Moolenaar <Bram@vim.org>
parents: 20816
diff changeset
775 return arg + STRLEN(arg);
0600ab7b9f09 patch 8.2.0972: Vim9 script variable declarations need a type
Bram Moolenaar <Bram@vim.org>
parents: 20816
diff changeset
776 }
0600ab7b9f09 patch 8.2.0972: Vim9 script variable declarations need a type
Bram Moolenaar <Bram@vim.org>
parents: 20816
diff changeset
777
20846
709379ab5110 patch 8.2.0975: Vim9: script variable does not accept optional s: prefix
Bram Moolenaar <Bram@vim.org>
parents: 20844
diff changeset
778 for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p))
20921
187c3fb42c8f patch 8.2.1012: Vim9: cannot declare single character script variables
Bram Moolenaar <Bram@vim.org>
parents: 20919
diff changeset
779 if (*p == ':' && (VIM_ISWHITE(p[1]) || p != arg + 1))
20846
709379ab5110 patch 8.2.0975: Vim9: script variable does not accept optional s: prefix
Bram Moolenaar <Bram@vim.org>
parents: 20844
diff changeset
780 break;
20840
0600ab7b9f09 patch 8.2.0972: Vim9 script variable declarations need a type
Bram Moolenaar <Bram@vim.org>
parents: 20816
diff changeset
781
0600ab7b9f09 patch 8.2.0972: Vim9 script variable declarations need a type
Bram Moolenaar <Bram@vim.org>
parents: 20816
diff changeset
782 if (*p != ':')
0600ab7b9f09 patch 8.2.0972: Vim9 script variable declarations need a type
Bram Moolenaar <Bram@vim.org>
parents: 20816
diff changeset
783 {
21789
f84625b961a8 patch 8.2.1444: error messages are spread out and names can be confusing
Bram Moolenaar <Bram@vim.org>
parents: 21584
diff changeset
784 emsg(_(e_type_or_initialization_required));
20840
0600ab7b9f09 patch 8.2.0972: Vim9 script variable declarations need a type
Bram Moolenaar <Bram@vim.org>
parents: 20816
diff changeset
785 return arg + STRLEN(arg);
0600ab7b9f09 patch 8.2.0972: Vim9 script variable declarations need a type
Bram Moolenaar <Bram@vim.org>
parents: 20816
diff changeset
786 }
20846
709379ab5110 patch 8.2.0975: Vim9: script variable does not accept optional s: prefix
Bram Moolenaar <Bram@vim.org>
parents: 20844
diff changeset
787 if (!VIM_ISWHITE(p[1]))
709379ab5110 patch 8.2.0975: Vim9: script variable does not accept optional s: prefix
Bram Moolenaar <Bram@vim.org>
parents: 20844
diff changeset
788 {
23877
85cf06ddb2a8 patch 8.2.2480: Vim9: some errors for white space do not show context
Bram Moolenaar <Bram@vim.org>
parents: 23578
diff changeset
789 semsg(_(e_white_space_required_after_str_str), ":", p);
20846
709379ab5110 patch 8.2.0975: Vim9: script variable does not accept optional s: prefix
Bram Moolenaar <Bram@vim.org>
parents: 20844
diff changeset
790 return arg + STRLEN(arg);
709379ab5110 patch 8.2.0975: Vim9: script variable does not accept optional s: prefix
Bram Moolenaar <Bram@vim.org>
parents: 20844
diff changeset
791 }
20840
0600ab7b9f09 patch 8.2.0972: Vim9 script variable declarations need a type
Bram Moolenaar <Bram@vim.org>
parents: 20816
diff changeset
792 name = vim_strnsave(arg, p - arg);
0600ab7b9f09 patch 8.2.0972: Vim9 script variable declarations need a type
Bram Moolenaar <Bram@vim.org>
parents: 20816
diff changeset
793
24717
bf8feac8a89a patch 8.2.2897: Vim9: can use reserved words at the script level
Bram Moolenaar <Bram@vim.org>
parents: 24533
diff changeset
794 // parse type, check for reserved name
20840
0600ab7b9f09 patch 8.2.0972: Vim9 script variable declarations need a type
Bram Moolenaar <Bram@vim.org>
parents: 20816
diff changeset
795 p = skipwhite(p + 1);
23332
cdb706d5c43d patch 8.2.2209: Vim9: return type of => lambda not parsed
Bram Moolenaar <Bram@vim.org>
parents: 23094
diff changeset
796 type = parse_type(&p, &si->sn_type_list, TRUE);
24717
bf8feac8a89a patch 8.2.2897: Vim9: can use reserved words at the script level
Bram Moolenaar <Bram@vim.org>
parents: 24533
diff changeset
797 if (type == NULL || check_reserved_name(name) == FAIL)
20844
1360541e8c74 patch 8.2.0974: Vim9: memory leak when script var has wrong type
Bram Moolenaar <Bram@vim.org>
parents: 20842
diff changeset
798 {
1360541e8c74 patch 8.2.0974: Vim9: memory leak when script var has wrong type
Bram Moolenaar <Bram@vim.org>
parents: 20842
diff changeset
799 vim_free(name);
20840
0600ab7b9f09 patch 8.2.0972: Vim9 script variable declarations need a type
Bram Moolenaar <Bram@vim.org>
parents: 20816
diff changeset
800 return p;
20844
1360541e8c74 patch 8.2.0974: Vim9: memory leak when script var has wrong type
Bram Moolenaar <Bram@vim.org>
parents: 20842
diff changeset
801 }
20840
0600ab7b9f09 patch 8.2.0972: Vim9 script variable declarations need a type
Bram Moolenaar <Bram@vim.org>
parents: 20816
diff changeset
802
0600ab7b9f09 patch 8.2.0972: Vim9 script variable declarations need a type
Bram Moolenaar <Bram@vim.org>
parents: 20816
diff changeset
803 // Create the variable with 0/NULL value.
0600ab7b9f09 patch 8.2.0972: Vim9 script variable declarations need a type
Bram Moolenaar <Bram@vim.org>
parents: 20816
diff changeset
804 CLEAR_FIELD(init_tv);
22250
dd42235ed626 patch 8.2.1674: Vim9: internal error when using variable that was not set
Bram Moolenaar <Bram@vim.org>
parents: 22202
diff changeset
805 if (type->tt_type == VAR_ANY)
dd42235ed626 patch 8.2.1674: Vim9: internal error when using variable that was not set
Bram Moolenaar <Bram@vim.org>
parents: 22202
diff changeset
806 // A variable of type "any" is not possible, just use zero instead
dd42235ed626 patch 8.2.1674: Vim9: internal error when using variable that was not set
Bram Moolenaar <Bram@vim.org>
parents: 22202
diff changeset
807 init_tv.v_type = VAR_NUMBER;
dd42235ed626 patch 8.2.1674: Vim9: internal error when using variable that was not set
Bram Moolenaar <Bram@vim.org>
parents: 22202
diff changeset
808 else
dd42235ed626 patch 8.2.1674: Vim9: internal error when using variable that was not set
Bram Moolenaar <Bram@vim.org>
parents: 22202
diff changeset
809 init_tv.v_type = type->tt_type;
23917
4b417b776b95 patch 8.2.2501: not always clear where an error is reported
Bram Moolenaar <Bram@vim.org>
parents: 23877
diff changeset
810 set_var_const(name, type, &init_tv, FALSE, 0, 0);
20840
0600ab7b9f09 patch 8.2.0972: Vim9 script variable declarations need a type
Bram Moolenaar <Bram@vim.org>
parents: 20816
diff changeset
811
0600ab7b9f09 patch 8.2.0972: Vim9 script variable declarations need a type
Bram Moolenaar <Bram@vim.org>
parents: 20816
diff changeset
812 vim_free(name);
0600ab7b9f09 patch 8.2.0972: Vim9 script variable declarations need a type
Bram Moolenaar <Bram@vim.org>
parents: 20816
diff changeset
813 return p;
0600ab7b9f09 patch 8.2.0972: Vim9 script variable declarations need a type
Bram Moolenaar <Bram@vim.org>
parents: 20816
diff changeset
814 }
0600ab7b9f09 patch 8.2.0972: Vim9 script variable declarations need a type
Bram Moolenaar <Bram@vim.org>
parents: 20816
diff changeset
815
20842
bacc2ab11810 patch 8.2.0973: Vim9: type is not checked when assigning to a script variable
Bram Moolenaar <Bram@vim.org>
parents: 20840
diff changeset
816 /*
22643
71b57779177d patch 8.2.1870: Vim9: no need to keep all script variables
Bram Moolenaar <Bram@vim.org>
parents: 22596
diff changeset
817 * Vim9 part of adding a script variable: add it to sn_all_vars (lookup by name
71b57779177d patch 8.2.1870: Vim9: no need to keep all script variables
Bram Moolenaar <Bram@vim.org>
parents: 22596
diff changeset
818 * with a hashtable) and sn_var_vals (lookup by index).
23362
f181fe2150ab patch 8.2.2224: Vim9: crash if script reloaded with different variable type
Bram Moolenaar <Bram@vim.org>
parents: 23358
diff changeset
819 * When "create" is TRUE this is a new variable, otherwise find and update an
f181fe2150ab patch 8.2.2224: Vim9: crash if script reloaded with different variable type
Bram Moolenaar <Bram@vim.org>
parents: 23358
diff changeset
820 * existing variable.
23578
85ce241ff9e3 patch 8.2.2331: Vim9: wrong error when modifying dict declared with :final
Bram Moolenaar <Bram@vim.org>
parents: 23458
diff changeset
821 * "flags" can have ASSIGN_FINAL or ASSIGN_CONST.
24438
5c6ccab68d1e patch 8.2.2759: Vim9: for loop infers type of loop variable
Bram Moolenaar <Bram@vim.org>
parents: 24285
diff changeset
822 * When "*type" is NULL use "tv" for the type and update "*type". If
5c6ccab68d1e patch 8.2.2759: Vim9: for loop infers type of loop variable
Bram Moolenaar <Bram@vim.org>
parents: 24285
diff changeset
823 * "do_member" is TRUE also use the member type, otherwise use "any".
22594
209c7aa56325 patch 8.2.1845: Vim9: function defined in a block can't use block variables
Bram Moolenaar <Bram@vim.org>
parents: 22529
diff changeset
824 */
209c7aa56325 patch 8.2.1845: Vim9: function defined in a block can't use block variables
Bram Moolenaar <Bram@vim.org>
parents: 22529
diff changeset
825 void
23578
85ce241ff9e3 patch 8.2.2331: Vim9: wrong error when modifying dict declared with :final
Bram Moolenaar <Bram@vim.org>
parents: 23458
diff changeset
826 update_vim9_script_var(
85ce241ff9e3 patch 8.2.2331: Vim9: wrong error when modifying dict declared with :final
Bram Moolenaar <Bram@vim.org>
parents: 23458
diff changeset
827 int create,
85ce241ff9e3 patch 8.2.2331: Vim9: wrong error when modifying dict declared with :final
Bram Moolenaar <Bram@vim.org>
parents: 23458
diff changeset
828 dictitem_T *di,
85ce241ff9e3 patch 8.2.2331: Vim9: wrong error when modifying dict declared with :final
Bram Moolenaar <Bram@vim.org>
parents: 23458
diff changeset
829 int flags,
85ce241ff9e3 patch 8.2.2331: Vim9: wrong error when modifying dict declared with :final
Bram Moolenaar <Bram@vim.org>
parents: 23458
diff changeset
830 typval_T *tv,
24438
5c6ccab68d1e patch 8.2.2759: Vim9: for loop infers type of loop variable
Bram Moolenaar <Bram@vim.org>
parents: 24285
diff changeset
831 type_T **type,
5c6ccab68d1e patch 8.2.2759: Vim9: for loop infers type of loop variable
Bram Moolenaar <Bram@vim.org>
parents: 24285
diff changeset
832 int do_member)
22594
209c7aa56325 patch 8.2.1845: Vim9: function defined in a block can't use block variables
Bram Moolenaar <Bram@vim.org>
parents: 22529
diff changeset
833 {
23362
f181fe2150ab patch 8.2.2224: Vim9: crash if script reloaded with different variable type
Bram Moolenaar <Bram@vim.org>
parents: 23358
diff changeset
834 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
f181fe2150ab patch 8.2.2224: Vim9: crash if script reloaded with different variable type
Bram Moolenaar <Bram@vim.org>
parents: 23358
diff changeset
835 hashitem_T *hi;
25358
f03271631eb5 patch 8.2.3216: Vim9: crash when using variable in a loop at script level
Bram Moolenaar <Bram@vim.org>
parents: 25320
diff changeset
836 svar_T *sv = NULL;
22594
209c7aa56325 patch 8.2.1845: Vim9: function defined in a block can't use block variables
Bram Moolenaar <Bram@vim.org>
parents: 22529
diff changeset
837
23362
f181fe2150ab patch 8.2.2224: Vim9: crash if script reloaded with different variable type
Bram Moolenaar <Bram@vim.org>
parents: 23358
diff changeset
838 if (create)
22594
209c7aa56325 patch 8.2.1845: Vim9: function defined in a block can't use block variables
Bram Moolenaar <Bram@vim.org>
parents: 22529
diff changeset
839 {
25358
f03271631eb5 patch 8.2.3216: Vim9: crash when using variable in a loop at script level
Bram Moolenaar <Bram@vim.org>
parents: 25320
diff changeset
840 sallvar_T *newsav;
f03271631eb5 patch 8.2.3216: Vim9: crash when using variable in a loop at script level
Bram Moolenaar <Bram@vim.org>
parents: 25320
diff changeset
841 sallvar_T *sav = NULL;
23362
f181fe2150ab patch 8.2.2224: Vim9: crash if script reloaded with different variable type
Bram Moolenaar <Bram@vim.org>
parents: 23358
diff changeset
842
f181fe2150ab patch 8.2.2224: Vim9: crash if script reloaded with different variable type
Bram Moolenaar <Bram@vim.org>
parents: 23358
diff changeset
843 // Store a pointer to the typval_T, so that it can be found by index
f181fe2150ab patch 8.2.2224: Vim9: crash if script reloaded with different variable type
Bram Moolenaar <Bram@vim.org>
parents: 23358
diff changeset
844 // instead of using a hastab lookup.
f181fe2150ab patch 8.2.2224: Vim9: crash if script reloaded with different variable type
Bram Moolenaar <Bram@vim.org>
parents: 23358
diff changeset
845 if (ga_grow(&si->sn_var_vals, 1) == FAIL)
f181fe2150ab patch 8.2.2224: Vim9: crash if script reloaded with different variable type
Bram Moolenaar <Bram@vim.org>
parents: 23358
diff changeset
846 return;
f181fe2150ab patch 8.2.2224: Vim9: crash if script reloaded with different variable type
Bram Moolenaar <Bram@vim.org>
parents: 23358
diff changeset
847
25358
f03271631eb5 patch 8.2.3216: Vim9: crash when using variable in a loop at script level
Bram Moolenaar <Bram@vim.org>
parents: 25320
diff changeset
848 hi = hash_find(&si->sn_all_vars.dv_hashtab, di->di_key);
22594
209c7aa56325 patch 8.2.1845: Vim9: function defined in a block can't use block variables
Bram Moolenaar <Bram@vim.org>
parents: 22529
diff changeset
849 if (!HASHITEM_EMPTY(hi))
209c7aa56325 patch 8.2.1845: Vim9: function defined in a block can't use block variables
Bram Moolenaar <Bram@vim.org>
parents: 22529
diff changeset
850 {
25358
f03271631eb5 patch 8.2.3216: Vim9: crash when using variable in a loop at script level
Bram Moolenaar <Bram@vim.org>
parents: 25320
diff changeset
851 // Variable with this name exists, either in this block or in
f03271631eb5 patch 8.2.3216: Vim9: crash when using variable in a loop at script level
Bram Moolenaar <Bram@vim.org>
parents: 25320
diff changeset
852 // another block.
f03271631eb5 patch 8.2.3216: Vim9: crash when using variable in a loop at script level
Bram Moolenaar <Bram@vim.org>
parents: 25320
diff changeset
853 for (sav = HI2SAV(hi); ; sav = sav->sav_next)
f03271631eb5 patch 8.2.3216: Vim9: crash when using variable in a loop at script level
Bram Moolenaar <Bram@vim.org>
parents: 25320
diff changeset
854 {
f03271631eb5 patch 8.2.3216: Vim9: crash when using variable in a loop at script level
Bram Moolenaar <Bram@vim.org>
parents: 25320
diff changeset
855 if (sav->sav_block_id == si->sn_current_block_id)
f03271631eb5 patch 8.2.3216: Vim9: crash when using variable in a loop at script level
Bram Moolenaar <Bram@vim.org>
parents: 25320
diff changeset
856 {
f03271631eb5 patch 8.2.3216: Vim9: crash when using variable in a loop at script level
Bram Moolenaar <Bram@vim.org>
parents: 25320
diff changeset
857 // variable defined in a loop, re-use the entry
f03271631eb5 patch 8.2.3216: Vim9: crash when using variable in a loop at script level
Bram Moolenaar <Bram@vim.org>
parents: 25320
diff changeset
858 sv = ((svar_T *)si->sn_var_vals.ga_data)
f03271631eb5 patch 8.2.3216: Vim9: crash when using variable in a loop at script level
Bram Moolenaar <Bram@vim.org>
parents: 25320
diff changeset
859 + sav->sav_var_vals_idx;
f03271631eb5 patch 8.2.3216: Vim9: crash when using variable in a loop at script level
Bram Moolenaar <Bram@vim.org>
parents: 25320
diff changeset
860 // unhide the variable
f03271631eb5 patch 8.2.3216: Vim9: crash when using variable in a loop at script level
Bram Moolenaar <Bram@vim.org>
parents: 25320
diff changeset
861 if (sv->sv_tv == &sav->sav_tv)
f03271631eb5 patch 8.2.3216: Vim9: crash when using variable in a loop at script level
Bram Moolenaar <Bram@vim.org>
parents: 25320
diff changeset
862 {
f03271631eb5 patch 8.2.3216: Vim9: crash when using variable in a loop at script level
Bram Moolenaar <Bram@vim.org>
parents: 25320
diff changeset
863 clear_tv(&sav->sav_tv);
f03271631eb5 patch 8.2.3216: Vim9: crash when using variable in a loop at script level
Bram Moolenaar <Bram@vim.org>
parents: 25320
diff changeset
864 sv->sv_tv = &di->di_tv;
f03271631eb5 patch 8.2.3216: Vim9: crash when using variable in a loop at script level
Bram Moolenaar <Bram@vim.org>
parents: 25320
diff changeset
865 sav->sav_di = di;
f03271631eb5 patch 8.2.3216: Vim9: crash when using variable in a loop at script level
Bram Moolenaar <Bram@vim.org>
parents: 25320
diff changeset
866 }
f03271631eb5 patch 8.2.3216: Vim9: crash when using variable in a loop at script level
Bram Moolenaar <Bram@vim.org>
parents: 25320
diff changeset
867 break;
f03271631eb5 patch 8.2.3216: Vim9: crash when using variable in a loop at script level
Bram Moolenaar <Bram@vim.org>
parents: 25320
diff changeset
868 }
f03271631eb5 patch 8.2.3216: Vim9: crash when using variable in a loop at script level
Bram Moolenaar <Bram@vim.org>
parents: 25320
diff changeset
869 if (sav->sav_next == NULL)
f03271631eb5 patch 8.2.3216: Vim9: crash when using variable in a loop at script level
Bram Moolenaar <Bram@vim.org>
parents: 25320
diff changeset
870 break;
f03271631eb5 patch 8.2.3216: Vim9: crash when using variable in a loop at script level
Bram Moolenaar <Bram@vim.org>
parents: 25320
diff changeset
871 }
f03271631eb5 patch 8.2.3216: Vim9: crash when using variable in a loop at script level
Bram Moolenaar <Bram@vim.org>
parents: 25320
diff changeset
872 }
22594
209c7aa56325 patch 8.2.1845: Vim9: function defined in a block can't use block variables
Bram Moolenaar <Bram@vim.org>
parents: 22529
diff changeset
873
25358
f03271631eb5 patch 8.2.3216: Vim9: crash when using variable in a loop at script level
Bram Moolenaar <Bram@vim.org>
parents: 25320
diff changeset
874 if (sv == NULL)
f03271631eb5 patch 8.2.3216: Vim9: crash when using variable in a loop at script level
Bram Moolenaar <Bram@vim.org>
parents: 25320
diff changeset
875 {
f03271631eb5 patch 8.2.3216: Vim9: crash when using variable in a loop at script level
Bram Moolenaar <Bram@vim.org>
parents: 25320
diff changeset
876 // Variable not defined or not defined in current block: Add a
f03271631eb5 patch 8.2.3216: Vim9: crash when using variable in a loop at script level
Bram Moolenaar <Bram@vim.org>
parents: 25320
diff changeset
877 // svar_T and create a new sallvar_T.
f03271631eb5 patch 8.2.3216: Vim9: crash when using variable in a loop at script level
Bram Moolenaar <Bram@vim.org>
parents: 25320
diff changeset
878 sv = ((svar_T *)si->sn_var_vals.ga_data) + si->sn_var_vals.ga_len;
f03271631eb5 patch 8.2.3216: Vim9: crash when using variable in a loop at script level
Bram Moolenaar <Bram@vim.org>
parents: 25320
diff changeset
879 newsav = (sallvar_T *)alloc_clear(
f03271631eb5 patch 8.2.3216: Vim9: crash when using variable in a loop at script level
Bram Moolenaar <Bram@vim.org>
parents: 25320
diff changeset
880 sizeof(sallvar_T) + STRLEN(di->di_key));
f03271631eb5 patch 8.2.3216: Vim9: crash when using variable in a loop at script level
Bram Moolenaar <Bram@vim.org>
parents: 25320
diff changeset
881 if (newsav == NULL)
f03271631eb5 patch 8.2.3216: Vim9: crash when using variable in a loop at script level
Bram Moolenaar <Bram@vim.org>
parents: 25320
diff changeset
882 return;
f03271631eb5 patch 8.2.3216: Vim9: crash when using variable in a loop at script level
Bram Moolenaar <Bram@vim.org>
parents: 25320
diff changeset
883
f03271631eb5 patch 8.2.3216: Vim9: crash when using variable in a loop at script level
Bram Moolenaar <Bram@vim.org>
parents: 25320
diff changeset
884 sv->sv_tv = &di->di_tv;
f03271631eb5 patch 8.2.3216: Vim9: crash when using variable in a loop at script level
Bram Moolenaar <Bram@vim.org>
parents: 25320
diff changeset
885 sv->sv_const = (flags & ASSIGN_FINAL) ? ASSIGN_FINAL
f03271631eb5 patch 8.2.3216: Vim9: crash when using variable in a loop at script level
Bram Moolenaar <Bram@vim.org>
parents: 25320
diff changeset
886 : (flags & ASSIGN_CONST) ? ASSIGN_CONST : 0;
f03271631eb5 patch 8.2.3216: Vim9: crash when using variable in a loop at script level
Bram Moolenaar <Bram@vim.org>
parents: 25320
diff changeset
887 sv->sv_export = is_export;
f03271631eb5 patch 8.2.3216: Vim9: crash when using variable in a loop at script level
Bram Moolenaar <Bram@vim.org>
parents: 25320
diff changeset
888 newsav->sav_var_vals_idx = si->sn_var_vals.ga_len;
f03271631eb5 patch 8.2.3216: Vim9: crash when using variable in a loop at script level
Bram Moolenaar <Bram@vim.org>
parents: 25320
diff changeset
889 ++si->sn_var_vals.ga_len;
f03271631eb5 patch 8.2.3216: Vim9: crash when using variable in a loop at script level
Bram Moolenaar <Bram@vim.org>
parents: 25320
diff changeset
890 STRCPY(&newsav->sav_key, di->di_key);
f03271631eb5 patch 8.2.3216: Vim9: crash when using variable in a loop at script level
Bram Moolenaar <Bram@vim.org>
parents: 25320
diff changeset
891 sv->sv_name = newsav->sav_key;
f03271631eb5 patch 8.2.3216: Vim9: crash when using variable in a loop at script level
Bram Moolenaar <Bram@vim.org>
parents: 25320
diff changeset
892 newsav->sav_di = di;
f03271631eb5 patch 8.2.3216: Vim9: crash when using variable in a loop at script level
Bram Moolenaar <Bram@vim.org>
parents: 25320
diff changeset
893 newsav->sav_block_id = si->sn_current_block_id;
f03271631eb5 patch 8.2.3216: Vim9: crash when using variable in a loop at script level
Bram Moolenaar <Bram@vim.org>
parents: 25320
diff changeset
894
f03271631eb5 patch 8.2.3216: Vim9: crash when using variable in a loop at script level
Bram Moolenaar <Bram@vim.org>
parents: 25320
diff changeset
895 if (HASHITEM_EMPTY(hi))
f03271631eb5 patch 8.2.3216: Vim9: crash when using variable in a loop at script level
Bram Moolenaar <Bram@vim.org>
parents: 25320
diff changeset
896 // new variable name
f03271631eb5 patch 8.2.3216: Vim9: crash when using variable in a loop at script level
Bram Moolenaar <Bram@vim.org>
parents: 25320
diff changeset
897 hash_add(&si->sn_all_vars.dv_hashtab, newsav->sav_key);
f03271631eb5 patch 8.2.3216: Vim9: crash when using variable in a loop at script level
Bram Moolenaar <Bram@vim.org>
parents: 25320
diff changeset
898 else if (sav != NULL)
f03271631eb5 patch 8.2.3216: Vim9: crash when using variable in a loop at script level
Bram Moolenaar <Bram@vim.org>
parents: 25320
diff changeset
899 // existing name in a new block, append to the list
f03271631eb5 patch 8.2.3216: Vim9: crash when using variable in a loop at script level
Bram Moolenaar <Bram@vim.org>
parents: 25320
diff changeset
900 sav->sav_next = newsav;
22594
209c7aa56325 patch 8.2.1845: Vim9: function defined in a block can't use block variables
Bram Moolenaar <Bram@vim.org>
parents: 22529
diff changeset
901 }
23362
f181fe2150ab patch 8.2.2224: Vim9: crash if script reloaded with different variable type
Bram Moolenaar <Bram@vim.org>
parents: 23358
diff changeset
902 }
f181fe2150ab patch 8.2.2224: Vim9: crash if script reloaded with different variable type
Bram Moolenaar <Bram@vim.org>
parents: 23358
diff changeset
903 else
f181fe2150ab patch 8.2.2224: Vim9: crash if script reloaded with different variable type
Bram Moolenaar <Bram@vim.org>
parents: 23358
diff changeset
904 {
25186
0a3b1c66d3f2 patch 8.2.3129: Vim9: imported uninitialized list does not get type checked
Bram Moolenaar <Bram@vim.org>
parents: 25184
diff changeset
905 sv = find_typval_in_script(&di->di_tv);
23362
f181fe2150ab patch 8.2.2224: Vim9: crash if script reloaded with different variable type
Bram Moolenaar <Bram@vim.org>
parents: 23358
diff changeset
906 }
f181fe2150ab patch 8.2.2224: Vim9: crash if script reloaded with different variable type
Bram Moolenaar <Bram@vim.org>
parents: 23358
diff changeset
907 if (sv != NULL)
f181fe2150ab patch 8.2.2224: Vim9: crash if script reloaded with different variable type
Bram Moolenaar <Bram@vim.org>
parents: 23358
diff changeset
908 {
23458
d2b1269c2c68 patch 8.2.2272: Vim9: extend() can violate the type of a variable
Bram Moolenaar <Bram@vim.org>
parents: 23392
diff changeset
909 if (*type == NULL)
25358
f03271631eb5 patch 8.2.3216: Vim9: crash when using variable in a loop at script level
Bram Moolenaar <Bram@vim.org>
parents: 25320
diff changeset
910 *type = typval2type(tv, get_copyID(), &si->sn_type_list, do_member);
26482
b115b552071f patch 8.2.3771: Vim9: accessing freed memory when checking type
Bram Moolenaar <Bram@vim.org>
parents: 26302
diff changeset
911 if (sv->sv_type_allocated)
b115b552071f patch 8.2.3771: Vim9: accessing freed memory when checking type
Bram Moolenaar <Bram@vim.org>
parents: 26302
diff changeset
912 free_type(sv->sv_type);
b115b552071f patch 8.2.3771: Vim9: accessing freed memory when checking type
Bram Moolenaar <Bram@vim.org>
parents: 26302
diff changeset
913 if (*type != NULL && ((*type)->tt_type == VAR_FUNC
b115b552071f patch 8.2.3771: Vim9: accessing freed memory when checking type
Bram Moolenaar <Bram@vim.org>
parents: 26302
diff changeset
914 || (*type)->tt_type == VAR_PARTIAL))
b115b552071f patch 8.2.3771: Vim9: accessing freed memory when checking type
Bram Moolenaar <Bram@vim.org>
parents: 26302
diff changeset
915 {
b115b552071f patch 8.2.3771: Vim9: accessing freed memory when checking type
Bram Moolenaar <Bram@vim.org>
parents: 26302
diff changeset
916 // The type probably uses uf_type_list, which is cleared when the
b115b552071f patch 8.2.3771: Vim9: accessing freed memory when checking type
Bram Moolenaar <Bram@vim.org>
parents: 26302
diff changeset
917 // function is freed, but the script variable may keep the type.
b115b552071f patch 8.2.3771: Vim9: accessing freed memory when checking type
Bram Moolenaar <Bram@vim.org>
parents: 26302
diff changeset
918 // Make a copy to avoid using freed memory.
b115b552071f patch 8.2.3771: Vim9: accessing freed memory when checking type
Bram Moolenaar <Bram@vim.org>
parents: 26302
diff changeset
919 sv->sv_type = alloc_type(*type);
b115b552071f patch 8.2.3771: Vim9: accessing freed memory when checking type
Bram Moolenaar <Bram@vim.org>
parents: 26302
diff changeset
920 sv->sv_type_allocated = TRUE;
b115b552071f patch 8.2.3771: Vim9: accessing freed memory when checking type
Bram Moolenaar <Bram@vim.org>
parents: 26302
diff changeset
921 }
b115b552071f patch 8.2.3771: Vim9: accessing freed memory when checking type
Bram Moolenaar <Bram@vim.org>
parents: 26302
diff changeset
922 else
b115b552071f patch 8.2.3771: Vim9: accessing freed memory when checking type
Bram Moolenaar <Bram@vim.org>
parents: 26302
diff changeset
923 {
b115b552071f patch 8.2.3771: Vim9: accessing freed memory when checking type
Bram Moolenaar <Bram@vim.org>
parents: 26302
diff changeset
924 sv->sv_type = *type;
b115b552071f patch 8.2.3771: Vim9: accessing freed memory when checking type
Bram Moolenaar <Bram@vim.org>
parents: 26302
diff changeset
925 sv->sv_type_allocated = FALSE;
b115b552071f patch 8.2.3771: Vim9: accessing freed memory when checking type
Bram Moolenaar <Bram@vim.org>
parents: 26302
diff changeset
926 }
23362
f181fe2150ab patch 8.2.2224: Vim9: crash if script reloaded with different variable type
Bram Moolenaar <Bram@vim.org>
parents: 23358
diff changeset
927 }
22594
209c7aa56325 patch 8.2.1845: Vim9: function defined in a block can't use block variables
Bram Moolenaar <Bram@vim.org>
parents: 22529
diff changeset
928
23362
f181fe2150ab patch 8.2.2224: Vim9: crash if script reloaded with different variable type
Bram Moolenaar <Bram@vim.org>
parents: 23358
diff changeset
929 // let ex_export() know the export worked.
f181fe2150ab patch 8.2.2224: Vim9: crash if script reloaded with different variable type
Bram Moolenaar <Bram@vim.org>
parents: 23358
diff changeset
930 is_export = FALSE;
22594
209c7aa56325 patch 8.2.1845: Vim9: function defined in a block can't use block variables
Bram Moolenaar <Bram@vim.org>
parents: 22529
diff changeset
931 }
209c7aa56325 patch 8.2.1845: Vim9: function defined in a block can't use block variables
Bram Moolenaar <Bram@vim.org>
parents: 22529
diff changeset
932
22596
107eae953b87 patch 8.2.1846: Vim9: block variables are not found in compiled function
Bram Moolenaar <Bram@vim.org>
parents: 22594
diff changeset
933 /*
107eae953b87 patch 8.2.1846: Vim9: block variables are not found in compiled function
Bram Moolenaar <Bram@vim.org>
parents: 22594
diff changeset
934 * Hide a script variable when leaving a block.
107eae953b87 patch 8.2.1846: Vim9: block variables are not found in compiled function
Bram Moolenaar <Bram@vim.org>
parents: 22594
diff changeset
935 * "idx" is de index in sn_var_vals.
22643
71b57779177d patch 8.2.1870: Vim9: no need to keep all script variables
Bram Moolenaar <Bram@vim.org>
parents: 22596
diff changeset
936 * When "func_defined" is non-zero then a function was defined in this block,
71b57779177d patch 8.2.1870: Vim9: no need to keep all script variables
Bram Moolenaar <Bram@vim.org>
parents: 22596
diff changeset
937 * the variable may be accessed by it. Otherwise the variable can be cleared.
22596
107eae953b87 patch 8.2.1846: Vim9: block variables are not found in compiled function
Bram Moolenaar <Bram@vim.org>
parents: 22594
diff changeset
938 */
22594
209c7aa56325 patch 8.2.1845: Vim9: function defined in a block can't use block variables
Bram Moolenaar <Bram@vim.org>
parents: 22529
diff changeset
939 void
22643
71b57779177d patch 8.2.1870: Vim9: no need to keep all script variables
Bram Moolenaar <Bram@vim.org>
parents: 22596
diff changeset
940 hide_script_var(scriptitem_T *si, int idx, int func_defined)
22594
209c7aa56325 patch 8.2.1845: Vim9: function defined in a block can't use block variables
Bram Moolenaar <Bram@vim.org>
parents: 22529
diff changeset
941 {
22596
107eae953b87 patch 8.2.1846: Vim9: block variables are not found in compiled function
Bram Moolenaar <Bram@vim.org>
parents: 22594
diff changeset
942 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
22594
209c7aa56325 patch 8.2.1845: Vim9: function defined in a block can't use block variables
Bram Moolenaar <Bram@vim.org>
parents: 22529
diff changeset
943 hashtab_T *script_ht = get_script_local_ht();
209c7aa56325 patch 8.2.1845: Vim9: function defined in a block can't use block variables
Bram Moolenaar <Bram@vim.org>
parents: 22529
diff changeset
944 hashtab_T *all_ht = &si->sn_all_vars.dv_hashtab;
209c7aa56325 patch 8.2.1845: Vim9: function defined in a block can't use block variables
Bram Moolenaar <Bram@vim.org>
parents: 22529
diff changeset
945 hashitem_T *script_hi;
209c7aa56325 patch 8.2.1845: Vim9: function defined in a block can't use block variables
Bram Moolenaar <Bram@vim.org>
parents: 22529
diff changeset
946 hashitem_T *all_hi;
209c7aa56325 patch 8.2.1845: Vim9: function defined in a block can't use block variables
Bram Moolenaar <Bram@vim.org>
parents: 22529
diff changeset
947
209c7aa56325 patch 8.2.1845: Vim9: function defined in a block can't use block variables
Bram Moolenaar <Bram@vim.org>
parents: 22529
diff changeset
948 // Remove a variable declared inside the block, if it still exists.
22643
71b57779177d patch 8.2.1870: Vim9: no need to keep all script variables
Bram Moolenaar <Bram@vim.org>
parents: 22596
diff changeset
949 // If it was added in a nested block it will already have been removed.
22594
209c7aa56325 patch 8.2.1845: Vim9: function defined in a block can't use block variables
Bram Moolenaar <Bram@vim.org>
parents: 22529
diff changeset
950 // The typval is moved into the sallvar_T.
209c7aa56325 patch 8.2.1845: Vim9: function defined in a block can't use block variables
Bram Moolenaar <Bram@vim.org>
parents: 22529
diff changeset
951 script_hi = hash_find(script_ht, sv->sv_name);
209c7aa56325 patch 8.2.1845: Vim9: function defined in a block can't use block variables
Bram Moolenaar <Bram@vim.org>
parents: 22529
diff changeset
952 all_hi = hash_find(all_ht, sv->sv_name);
209c7aa56325 patch 8.2.1845: Vim9: function defined in a block can't use block variables
Bram Moolenaar <Bram@vim.org>
parents: 22529
diff changeset
953 if (!HASHITEM_EMPTY(script_hi) && !HASHITEM_EMPTY(all_hi))
209c7aa56325 patch 8.2.1845: Vim9: function defined in a block can't use block variables
Bram Moolenaar <Bram@vim.org>
parents: 22529
diff changeset
954 {
209c7aa56325 patch 8.2.1845: Vim9: function defined in a block can't use block variables
Bram Moolenaar <Bram@vim.org>
parents: 22529
diff changeset
955 dictitem_T *di = HI2DI(script_hi);
209c7aa56325 patch 8.2.1845: Vim9: function defined in a block can't use block variables
Bram Moolenaar <Bram@vim.org>
parents: 22529
diff changeset
956 sallvar_T *sav = HI2SAV(all_hi);
22643
71b57779177d patch 8.2.1870: Vim9: no need to keep all script variables
Bram Moolenaar <Bram@vim.org>
parents: 22596
diff changeset
957 sallvar_T *sav_prev = NULL;
22594
209c7aa56325 patch 8.2.1845: Vim9: function defined in a block can't use block variables
Bram Moolenaar <Bram@vim.org>
parents: 22529
diff changeset
958
22596
107eae953b87 patch 8.2.1846: Vim9: block variables are not found in compiled function
Bram Moolenaar <Bram@vim.org>
parents: 22594
diff changeset
959 // There can be multiple entries with the same name in different
107eae953b87 patch 8.2.1846: Vim9: block variables are not found in compiled function
Bram Moolenaar <Bram@vim.org>
parents: 22594
diff changeset
960 // blocks, find the right one.
107eae953b87 patch 8.2.1846: Vim9: block variables are not found in compiled function
Bram Moolenaar <Bram@vim.org>
parents: 22594
diff changeset
961 while (sav != NULL && sav->sav_var_vals_idx != idx)
22643
71b57779177d patch 8.2.1870: Vim9: no need to keep all script variables
Bram Moolenaar <Bram@vim.org>
parents: 22596
diff changeset
962 {
71b57779177d patch 8.2.1870: Vim9: no need to keep all script variables
Bram Moolenaar <Bram@vim.org>
parents: 22596
diff changeset
963 sav_prev = sav;
22596
107eae953b87 patch 8.2.1846: Vim9: block variables are not found in compiled function
Bram Moolenaar <Bram@vim.org>
parents: 22594
diff changeset
964 sav = sav->sav_next;
22643
71b57779177d patch 8.2.1870: Vim9: no need to keep all script variables
Bram Moolenaar <Bram@vim.org>
parents: 22596
diff changeset
965 }
22596
107eae953b87 patch 8.2.1846: Vim9: block variables are not found in compiled function
Bram Moolenaar <Bram@vim.org>
parents: 22594
diff changeset
966 if (sav != NULL)
107eae953b87 patch 8.2.1846: Vim9: block variables are not found in compiled function
Bram Moolenaar <Bram@vim.org>
parents: 22594
diff changeset
967 {
22643
71b57779177d patch 8.2.1870: Vim9: no need to keep all script variables
Bram Moolenaar <Bram@vim.org>
parents: 22596
diff changeset
968 if (func_defined)
71b57779177d patch 8.2.1870: Vim9: no need to keep all script variables
Bram Moolenaar <Bram@vim.org>
parents: 22596
diff changeset
969 {
71b57779177d patch 8.2.1870: Vim9: no need to keep all script variables
Bram Moolenaar <Bram@vim.org>
parents: 22596
diff changeset
970 // move the typval from the dictitem to the sallvar
71b57779177d patch 8.2.1870: Vim9: no need to keep all script variables
Bram Moolenaar <Bram@vim.org>
parents: 22596
diff changeset
971 sav->sav_tv = di->di_tv;
71b57779177d patch 8.2.1870: Vim9: no need to keep all script variables
Bram Moolenaar <Bram@vim.org>
parents: 22596
diff changeset
972 di->di_tv.v_type = VAR_UNKNOWN;
71b57779177d patch 8.2.1870: Vim9: no need to keep all script variables
Bram Moolenaar <Bram@vim.org>
parents: 22596
diff changeset
973 sav->sav_flags = di->di_flags;
71b57779177d patch 8.2.1870: Vim9: no need to keep all script variables
Bram Moolenaar <Bram@vim.org>
parents: 22596
diff changeset
974 sav->sav_di = NULL;
71b57779177d patch 8.2.1870: Vim9: no need to keep all script variables
Bram Moolenaar <Bram@vim.org>
parents: 22596
diff changeset
975 sv->sv_tv = &sav->sav_tv;
71b57779177d patch 8.2.1870: Vim9: no need to keep all script variables
Bram Moolenaar <Bram@vim.org>
parents: 22596
diff changeset
976 }
71b57779177d patch 8.2.1870: Vim9: no need to keep all script variables
Bram Moolenaar <Bram@vim.org>
parents: 22596
diff changeset
977 else
71b57779177d patch 8.2.1870: Vim9: no need to keep all script variables
Bram Moolenaar <Bram@vim.org>
parents: 22596
diff changeset
978 {
71b57779177d patch 8.2.1870: Vim9: no need to keep all script variables
Bram Moolenaar <Bram@vim.org>
parents: 22596
diff changeset
979 if (sav_prev == NULL)
71b57779177d patch 8.2.1870: Vim9: no need to keep all script variables
Bram Moolenaar <Bram@vim.org>
parents: 22596
diff changeset
980 hash_remove(all_ht, all_hi);
71b57779177d patch 8.2.1870: Vim9: no need to keep all script variables
Bram Moolenaar <Bram@vim.org>
parents: 22596
diff changeset
981 else
71b57779177d patch 8.2.1870: Vim9: no need to keep all script variables
Bram Moolenaar <Bram@vim.org>
parents: 22596
diff changeset
982 sav_prev->sav_next = sav->sav_next;
71b57779177d patch 8.2.1870: Vim9: no need to keep all script variables
Bram Moolenaar <Bram@vim.org>
parents: 22596
diff changeset
983 sv->sv_name = NULL;
71b57779177d patch 8.2.1870: Vim9: no need to keep all script variables
Bram Moolenaar <Bram@vim.org>
parents: 22596
diff changeset
984 vim_free(sav);
71b57779177d patch 8.2.1870: Vim9: no need to keep all script variables
Bram Moolenaar <Bram@vim.org>
parents: 22596
diff changeset
985 }
22596
107eae953b87 patch 8.2.1846: Vim9: block variables are not found in compiled function
Bram Moolenaar <Bram@vim.org>
parents: 22594
diff changeset
986 delete_var(script_ht, script_hi);
107eae953b87 patch 8.2.1846: Vim9: block variables are not found in compiled function
Bram Moolenaar <Bram@vim.org>
parents: 22594
diff changeset
987 }
22594
209c7aa56325 patch 8.2.1845: Vim9: function defined in a block can't use block variables
Bram Moolenaar <Bram@vim.org>
parents: 22529
diff changeset
988 }
209c7aa56325 patch 8.2.1845: Vim9: function defined in a block can't use block variables
Bram Moolenaar <Bram@vim.org>
parents: 22529
diff changeset
989 }
209c7aa56325 patch 8.2.1845: Vim9: function defined in a block can't use block variables
Bram Moolenaar <Bram@vim.org>
parents: 22529
diff changeset
990
209c7aa56325 patch 8.2.1845: Vim9: function defined in a block can't use block variables
Bram Moolenaar <Bram@vim.org>
parents: 22529
diff changeset
991 /*
22529
35ef9b0a81a3 patch 8.2.1813: Vim9: can assign wrong type to script dict
Bram Moolenaar <Bram@vim.org>
parents: 22391
diff changeset
992 * Find the script-local variable that links to "dest".
25186
0a3b1c66d3f2 patch 8.2.3129: Vim9: imported uninitialized list does not get type checked
Bram Moolenaar <Bram@vim.org>
parents: 25184
diff changeset
993 * Returns NULL if not found and give an internal error.
20842
bacc2ab11810 patch 8.2.0973: Vim9: type is not checked when assigning to a script variable
Bram Moolenaar <Bram@vim.org>
parents: 20840
diff changeset
994 */
22529
35ef9b0a81a3 patch 8.2.1813: Vim9: can assign wrong type to script dict
Bram Moolenaar <Bram@vim.org>
parents: 22391
diff changeset
995 svar_T *
25186
0a3b1c66d3f2 patch 8.2.3129: Vim9: imported uninitialized list does not get type checked
Bram Moolenaar <Bram@vim.org>
parents: 25184
diff changeset
996 find_typval_in_script(typval_T *dest)
20842
bacc2ab11810 patch 8.2.0973: Vim9: type is not checked when assigning to a script variable
Bram Moolenaar <Bram@vim.org>
parents: 20840
diff changeset
997 {
bacc2ab11810 patch 8.2.0973: Vim9: type is not checked when assigning to a script variable
Bram Moolenaar <Bram@vim.org>
parents: 20840
diff changeset
998 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
bacc2ab11810 patch 8.2.0973: Vim9: type is not checked when assigning to a script variable
Bram Moolenaar <Bram@vim.org>
parents: 20840
diff changeset
999 int idx;
bacc2ab11810 patch 8.2.0973: Vim9: type is not checked when assigning to a script variable
Bram Moolenaar <Bram@vim.org>
parents: 20840
diff changeset
1000
21907
f4e21796f47d patch 8.2.1503: Vim9: error for autocmd defined in :def in legacy script
Bram Moolenaar <Bram@vim.org>
parents: 21821
diff changeset
1001 if (si->sn_version != SCRIPT_VERSION_VIM9)
f4e21796f47d patch 8.2.1503: Vim9: error for autocmd defined in :def in legacy script
Bram Moolenaar <Bram@vim.org>
parents: 21821
diff changeset
1002 // legacy script doesn't store variable types
22529
35ef9b0a81a3 patch 8.2.1813: Vim9: can assign wrong type to script dict
Bram Moolenaar <Bram@vim.org>
parents: 22391
diff changeset
1003 return NULL;
21907
f4e21796f47d patch 8.2.1503: Vim9: error for autocmd defined in :def in legacy script
Bram Moolenaar <Bram@vim.org>
parents: 21821
diff changeset
1004
25955
4003fc2340dc patch 8.2.3511: Vim9: entry for loop variable is created every round
Bram Moolenaar <Bram@vim.org>
parents: 25755
diff changeset
1005 // Find the svar_T in sn_var_vals. Start at the end, in a for loop the
4003fc2340dc patch 8.2.3511: Vim9: entry for loop variable is created every round
Bram Moolenaar <Bram@vim.org>
parents: 25755
diff changeset
1006 // variable was added at the end.
4003fc2340dc patch 8.2.3511: Vim9: entry for loop variable is created every round
Bram Moolenaar <Bram@vim.org>
parents: 25755
diff changeset
1007 for (idx = si->sn_var_vals.ga_len - 1; idx >= 0; --idx)
20842
bacc2ab11810 patch 8.2.0973: Vim9: type is not checked when assigning to a script variable
Bram Moolenaar <Bram@vim.org>
parents: 20840
diff changeset
1008 {
bacc2ab11810 patch 8.2.0973: Vim9: type is not checked when assigning to a script variable
Bram Moolenaar <Bram@vim.org>
parents: 20840
diff changeset
1009 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
bacc2ab11810 patch 8.2.0973: Vim9: type is not checked when assigning to a script variable
Bram Moolenaar <Bram@vim.org>
parents: 20840
diff changeset
1010
23094
8b93aea8d9d7 patch 8.2.2093: Vim9: script test sometimes fails
Bram Moolenaar <Bram@vim.org>
parents: 22740
diff changeset
1011 // If "sv_name" is NULL the variable was hidden when leaving a block,
8b93aea8d9d7 patch 8.2.2093: Vim9: script test sometimes fails
Bram Moolenaar <Bram@vim.org>
parents: 22740
diff changeset
1012 // don't check "sv_tv" then, it might be used for another variable now.
8b93aea8d9d7 patch 8.2.2093: Vim9: script test sometimes fails
Bram Moolenaar <Bram@vim.org>
parents: 22740
diff changeset
1013 if (sv->sv_name != NULL && sv->sv_tv == dest)
22529
35ef9b0a81a3 patch 8.2.1813: Vim9: can assign wrong type to script dict
Bram Moolenaar <Bram@vim.org>
parents: 22391
diff changeset
1014 return sv;
20842
bacc2ab11810 patch 8.2.0973: Vim9: type is not checked when assigning to a script variable
Bram Moolenaar <Bram@vim.org>
parents: 20840
diff changeset
1015 }
25186
0a3b1c66d3f2 patch 8.2.3129: Vim9: imported uninitialized list does not get type checked
Bram Moolenaar <Bram@vim.org>
parents: 25184
diff changeset
1016 iemsg("find_typval_in_script(): not found");
22529
35ef9b0a81a3 patch 8.2.1813: Vim9: can assign wrong type to script dict
Bram Moolenaar <Bram@vim.org>
parents: 22391
diff changeset
1017 return NULL;
35ef9b0a81a3 patch 8.2.1813: Vim9: can assign wrong type to script dict
Bram Moolenaar <Bram@vim.org>
parents: 22391
diff changeset
1018 }
35ef9b0a81a3 patch 8.2.1813: Vim9: can assign wrong type to script dict
Bram Moolenaar <Bram@vim.org>
parents: 22391
diff changeset
1019
35ef9b0a81a3 patch 8.2.1813: Vim9: can assign wrong type to script dict
Bram Moolenaar <Bram@vim.org>
parents: 22391
diff changeset
1020 /*
35ef9b0a81a3 patch 8.2.1813: Vim9: can assign wrong type to script dict
Bram Moolenaar <Bram@vim.org>
parents: 22391
diff changeset
1021 * Check if the type of script variable "dest" allows assigning "value".
35ef9b0a81a3 patch 8.2.1813: Vim9: can assign wrong type to script dict
Bram Moolenaar <Bram@vim.org>
parents: 22391
diff changeset
1022 * If needed convert "value" to a bool.
35ef9b0a81a3 patch 8.2.1813: Vim9: can assign wrong type to script dict
Bram Moolenaar <Bram@vim.org>
parents: 22391
diff changeset
1023 */
35ef9b0a81a3 patch 8.2.1813: Vim9: can assign wrong type to script dict
Bram Moolenaar <Bram@vim.org>
parents: 22391
diff changeset
1024 int
23917
4b417b776b95 patch 8.2.2501: not always clear where an error is reported
Bram Moolenaar <Bram@vim.org>
parents: 23877
diff changeset
1025 check_script_var_type(
26302
7351926fbe9e patch 8.2.3682: Vim9: assigning to a script variable drops the type
Bram Moolenaar <Bram@vim.org>
parents: 25955
diff changeset
1026 svar_T *sv,
23917
4b417b776b95 patch 8.2.2501: not always clear where an error is reported
Bram Moolenaar <Bram@vim.org>
parents: 23877
diff changeset
1027 typval_T *value,
4b417b776b95 patch 8.2.2501: not always clear where an error is reported
Bram Moolenaar <Bram@vim.org>
parents: 23877
diff changeset
1028 char_u *name,
4b417b776b95 patch 8.2.2501: not always clear where an error is reported
Bram Moolenaar <Bram@vim.org>
parents: 23877
diff changeset
1029 where_T where)
22529
35ef9b0a81a3 patch 8.2.1813: Vim9: can assign wrong type to script dict
Bram Moolenaar <Bram@vim.org>
parents: 22391
diff changeset
1030 {
35ef9b0a81a3 patch 8.2.1813: Vim9: can assign wrong type to script dict
Bram Moolenaar <Bram@vim.org>
parents: 22391
diff changeset
1031 int ret;
35ef9b0a81a3 patch 8.2.1813: Vim9: can assign wrong type to script dict
Bram Moolenaar <Bram@vim.org>
parents: 22391
diff changeset
1032
26302
7351926fbe9e patch 8.2.3682: Vim9: assigning to a script variable drops the type
Bram Moolenaar <Bram@vim.org>
parents: 25955
diff changeset
1033 if (sv->sv_const != 0)
7351926fbe9e patch 8.2.3682: Vim9: assigning to a script variable drops the type
Bram Moolenaar <Bram@vim.org>
parents: 25955
diff changeset
1034 {
7351926fbe9e patch 8.2.3682: Vim9: assigning to a script variable drops the type
Bram Moolenaar <Bram@vim.org>
parents: 25955
diff changeset
1035 semsg(_(e_cannot_change_readonly_variable_str), name);
7351926fbe9e patch 8.2.3682: Vim9: assigning to a script variable drops the type
Bram Moolenaar <Bram@vim.org>
parents: 25955
diff changeset
1036 return FAIL;
7351926fbe9e patch 8.2.3682: Vim9: assigning to a script variable drops the type
Bram Moolenaar <Bram@vim.org>
parents: 25955
diff changeset
1037 }
7351926fbe9e patch 8.2.3682: Vim9: assigning to a script variable drops the type
Bram Moolenaar <Bram@vim.org>
parents: 25955
diff changeset
1038 ret = check_typval_type(sv->sv_type, value, where);
7351926fbe9e patch 8.2.3682: Vim9: assigning to a script variable drops the type
Bram Moolenaar <Bram@vim.org>
parents: 25955
diff changeset
1039 if (ret == OK && need_convert_to_bool(sv->sv_type, value))
22529
35ef9b0a81a3 patch 8.2.1813: Vim9: can assign wrong type to script dict
Bram Moolenaar <Bram@vim.org>
parents: 22391
diff changeset
1040 {
26302
7351926fbe9e patch 8.2.3682: Vim9: assigning to a script variable drops the type
Bram Moolenaar <Bram@vim.org>
parents: 25955
diff changeset
1041 int val = tv2bool(value);
22529
35ef9b0a81a3 patch 8.2.1813: Vim9: can assign wrong type to script dict
Bram Moolenaar <Bram@vim.org>
parents: 22391
diff changeset
1042
26302
7351926fbe9e patch 8.2.3682: Vim9: assigning to a script variable drops the type
Bram Moolenaar <Bram@vim.org>
parents: 25955
diff changeset
1043 clear_tv(value);
7351926fbe9e patch 8.2.3682: Vim9: assigning to a script variable drops the type
Bram Moolenaar <Bram@vim.org>
parents: 25955
diff changeset
1044 value->v_type = VAR_BOOL;
7351926fbe9e patch 8.2.3682: Vim9: assigning to a script variable drops the type
Bram Moolenaar <Bram@vim.org>
parents: 25955
diff changeset
1045 value->v_lock = 0;
7351926fbe9e patch 8.2.3682: Vim9: assigning to a script variable drops the type
Bram Moolenaar <Bram@vim.org>
parents: 25955
diff changeset
1046 value->vval.v_number = val ? VVAL_TRUE : VVAL_FALSE;
22529
35ef9b0a81a3 patch 8.2.1813: Vim9: can assign wrong type to script dict
Bram Moolenaar <Bram@vim.org>
parents: 22391
diff changeset
1047 }
26302
7351926fbe9e patch 8.2.3682: Vim9: assigning to a script variable drops the type
Bram Moolenaar <Bram@vim.org>
parents: 25955
diff changeset
1048 return ret;
20842
bacc2ab11810 patch 8.2.0973: Vim9: type is not checked when assigning to a script variable
Bram Moolenaar <Bram@vim.org>
parents: 20840
diff changeset
1049 }
20840
0600ab7b9f09 patch 8.2.0972: Vim9 script variable declarations need a type
Bram Moolenaar <Bram@vim.org>
parents: 20816
diff changeset
1050
24717
bf8feac8a89a patch 8.2.2897: Vim9: can use reserved words at the script level
Bram Moolenaar <Bram@vim.org>
parents: 24533
diff changeset
1051 // words that cannot be used as a variable
bf8feac8a89a patch 8.2.2897: Vim9: can use reserved words at the script level
Bram Moolenaar <Bram@vim.org>
parents: 24533
diff changeset
1052 static char *reserved[] = {
bf8feac8a89a patch 8.2.2897: Vim9: can use reserved words at the script level
Bram Moolenaar <Bram@vim.org>
parents: 24533
diff changeset
1053 "true",
bf8feac8a89a patch 8.2.2897: Vim9: can use reserved words at the script level
Bram Moolenaar <Bram@vim.org>
parents: 24533
diff changeset
1054 "false",
bf8feac8a89a patch 8.2.2897: Vim9: can use reserved words at the script level
Bram Moolenaar <Bram@vim.org>
parents: 24533
diff changeset
1055 "null",
24888
b6ac4ed5e2d2 patch 8.2.2982: Vim9: future commands are not reserved yet
Bram Moolenaar <Bram@vim.org>
parents: 24717
diff changeset
1056 "this",
24717
bf8feac8a89a patch 8.2.2897: Vim9: can use reserved words at the script level
Bram Moolenaar <Bram@vim.org>
parents: 24533
diff changeset
1057 NULL
bf8feac8a89a patch 8.2.2897: Vim9: can use reserved words at the script level
Bram Moolenaar <Bram@vim.org>
parents: 24533
diff changeset
1058 };
bf8feac8a89a patch 8.2.2897: Vim9: can use reserved words at the script level
Bram Moolenaar <Bram@vim.org>
parents: 24533
diff changeset
1059
bf8feac8a89a patch 8.2.2897: Vim9: can use reserved words at the script level
Bram Moolenaar <Bram@vim.org>
parents: 24533
diff changeset
1060 int
bf8feac8a89a patch 8.2.2897: Vim9: can use reserved words at the script level
Bram Moolenaar <Bram@vim.org>
parents: 24533
diff changeset
1061 check_reserved_name(char_u *name)
bf8feac8a89a patch 8.2.2897: Vim9: can use reserved words at the script level
Bram Moolenaar <Bram@vim.org>
parents: 24533
diff changeset
1062 {
bf8feac8a89a patch 8.2.2897: Vim9: can use reserved words at the script level
Bram Moolenaar <Bram@vim.org>
parents: 24533
diff changeset
1063 int idx;
bf8feac8a89a patch 8.2.2897: Vim9: can use reserved words at the script level
Bram Moolenaar <Bram@vim.org>
parents: 24533
diff changeset
1064
bf8feac8a89a patch 8.2.2897: Vim9: can use reserved words at the script level
Bram Moolenaar <Bram@vim.org>
parents: 24533
diff changeset
1065 for (idx = 0; reserved[idx] != NULL; ++idx)
bf8feac8a89a patch 8.2.2897: Vim9: can use reserved words at the script level
Bram Moolenaar <Bram@vim.org>
parents: 24533
diff changeset
1066 if (STRCMP(reserved[idx], name) == 0)
bf8feac8a89a patch 8.2.2897: Vim9: can use reserved words at the script level
Bram Moolenaar <Bram@vim.org>
parents: 24533
diff changeset
1067 {
bf8feac8a89a patch 8.2.2897: Vim9: can use reserved words at the script level
Bram Moolenaar <Bram@vim.org>
parents: 24533
diff changeset
1068 semsg(_(e_cannot_use_reserved_name), name);
bf8feac8a89a patch 8.2.2897: Vim9: can use reserved words at the script level
Bram Moolenaar <Bram@vim.org>
parents: 24533
diff changeset
1069 return FAIL;
bf8feac8a89a patch 8.2.2897: Vim9: can use reserved words at the script level
Bram Moolenaar <Bram@vim.org>
parents: 24533
diff changeset
1070 }
bf8feac8a89a patch 8.2.2897: Vim9: can use reserved words at the script level
Bram Moolenaar <Bram@vim.org>
parents: 24533
diff changeset
1071 return OK;
bf8feac8a89a patch 8.2.2897: Vim9: can use reserved words at the script level
Bram Moolenaar <Bram@vim.org>
parents: 24533
diff changeset
1072 }
bf8feac8a89a patch 8.2.2897: Vim9: can use reserved words at the script level
Bram Moolenaar <Bram@vim.org>
parents: 24533
diff changeset
1073
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1074 #endif // FEAT_EVAL