annotate src/vim9script.c @ 26992:8433e5c87651 v8.2.4025

patch 8.2.4025: error for import not ending in .vim does not work for .vimrc Commit: https://github.com/vim/vim/commit/834d41853e12b9022f60b08c32480928c2a9e48f Author: Bram Moolenaar <Bram@vim.org> Date: Fri Jan 7 13:38:24 2022 +0000 patch 8.2.4025: error for import not ending in .vim does not work for .vimrc Problem: Error for import not ending in .vim does not work for .vimrc. Solution: Check that .vim is the end. (closes https://github.com/vim/vim/issues/9484)
author Bram Moolenaar <Bram@vim.org>
date Fri, 07 Jan 2022 14:45:04 +0100
parents bef1f0167251
children 268f6a3511df
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;
26980
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
366 char_u *nextarg;
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
367 int getnext;
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
368 char_u *expr_end;
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
369 int ret = FAIL;
26980
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
370 char_u *as_name = NULL;
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
371 typval_T tv;
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
372 int sid = -1;
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
373 int res;
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 // 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
377 // string.
26980
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
378 ret = eval0_retarg(arg, &tv, NULL, evalarg, &expr_end);
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
379 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
380 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
381 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
382 || 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
383 {
26980
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
384 semsg(_(e_invalid_string_for_import_str), arg);
21146
465d6e40e79c patch 8.2.1124: Vim9: no line break allowed in :import command
Bram Moolenaar <Bram@vim.org>
parents: 21120
diff changeset
385 goto erret;
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
386 }
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
387
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
388 // 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
389 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
390
21146
465d6e40e79c patch 8.2.1124: Vim9: no line break allowed in :import command
Bram Moolenaar <Bram@vim.org>
parents: 21120
diff changeset
391 /*
465d6e40e79c patch 8.2.1124: Vim9: no line break allowed in :import command
Bram Moolenaar <Bram@vim.org>
parents: 21120
diff changeset
392 * 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
393 */
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
394 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
395 {
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
396 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
397 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
398 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
399 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
400
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
401 // 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
402 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
403 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
404 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
405 goto erret;
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
406 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
407 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
408 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
409 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
410
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
411 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
412 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
413 }
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
414 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
415 {
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
416 // 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
417 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
418 }
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
419 else
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
420 {
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
421 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
422 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
423
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
424 // 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
425 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
426 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
427 {
21146
465d6e40e79c patch 8.2.1124: Vim9: no line break allowed in :import command
Bram Moolenaar <Bram@vim.org>
parents: 21120
diff changeset
428 goto erret;
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
429 }
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
430 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
431 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
432 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
433 }
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
434
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
435 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
436 {
21821
0deb6f96a5a3 patch 8.2.1460: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 21789
diff changeset
437 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
438 goto erret;
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
439 }
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
440
26980
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
441 // Allow for the "as Name" to be in the next line.
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
442 nextarg = eval_next_non_blank(expr_end, evalarg, &getnext);
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
443 if (STRNCMP("as", nextarg, 2) == 0 && IS_WHITE_OR_NUL(nextarg[2]))
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
444 {
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
445 char_u *p;
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
446
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
447 if (getnext)
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
448 arg = eval_next_line(evalarg);
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
449 else
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
450 arg = nextarg;
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
451
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
452 // Skip over "as Name "; no line break allowed after "as".
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
453 // Do not allow for ':' and '#'.
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
454 arg = skipwhite(arg + 2);
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
455 p = arg;
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
456 if (eval_isnamec1(*arg))
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
457 while (ASCII_ISALNUM(*arg) || *arg == '_')
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
458 ++arg;
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
459 if (p == arg || !IS_WHITE_OR_NUL(*arg))
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
460 {
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
461 semsg(_(e_syntax_error_in_import_str), p);
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
462 goto erret;
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
463 }
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
464 as_name = vim_strnsave(p, arg - p);
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
465 arg = skipwhite(arg);
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
466 }
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
467 else
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
468 {
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
469 char_u *p = gettail(tv.vval.v_string);
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
470 char_u *end = (char_u *)strstr((char *)p, ".vim");
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
471
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
472 if (!ends_excmd2(arg_start, expr_end))
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
473 {
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
474 semsg(_(e_trailing_characters_str), expr_end);
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
475 goto erret;
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
476 }
26992
8433e5c87651 patch 8.2.4025: error for import not ending in .vim does not work for .vimrc
Bram Moolenaar <Bram@vim.org>
parents: 26988
diff changeset
477 if (end == NULL || end[4] != NUL)
26980
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
478 {
26992
8433e5c87651 patch 8.2.4025: error for import not ending in .vim does not work for .vimrc
Bram Moolenaar <Bram@vim.org>
parents: 26988
diff changeset
479 semsg(_(e_imported_script_must_use_as_or_end_in_dot_vim_str), p);
8433e5c87651 patch 8.2.4025: error for import not ending in .vim does not work for .vimrc
Bram Moolenaar <Bram@vim.org>
parents: 26988
diff changeset
480 goto erret;
8433e5c87651 patch 8.2.4025: error for import not ending in .vim does not work for .vimrc
Bram Moolenaar <Bram@vim.org>
parents: 26988
diff changeset
481 }
8433e5c87651 patch 8.2.4025: error for import not ending in .vim does not work for .vimrc
Bram Moolenaar <Bram@vim.org>
parents: 26988
diff changeset
482 if (end == p)
8433e5c87651 patch 8.2.4025: error for import not ending in .vim does not work for .vimrc
Bram Moolenaar <Bram@vim.org>
parents: 26988
diff changeset
483 {
8433e5c87651 patch 8.2.4025: error for import not ending in .vim does not work for .vimrc
Bram Moolenaar <Bram@vim.org>
parents: 26988
diff changeset
484 semsg(_(e_cannot_import_dot_vim_without_using_as), p);
26980
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
485 goto erret;
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
486 }
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
487 as_name = vim_strnsave(p, end - p);
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
488 }
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
489
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
490 if (as_name != NULL)
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
491 {
24029
429b4f8d2fac patch 8.2.2556: Vim9: :import with "as" not fully supported
Bram Moolenaar <Bram@vim.org>
parents: 23980
diff changeset
492 imported_T *imported;
23364
17a0e32eefd4 patch 8.2.2225: Vim9: error when using :import in legacy script twice
Bram Moolenaar <Bram@vim.org>
parents: 23362
diff changeset
493
17a0e32eefd4 patch 8.2.2225: Vim9: error when using :import in legacy script twice
Bram Moolenaar <Bram@vim.org>
parents: 23362
diff changeset
494 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
495 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
496 {
17a0e32eefd4 patch 8.2.2225: Vim9: error when using :import in legacy script twice
Bram Moolenaar <Bram@vim.org>
parents: 23362
diff changeset
497 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
498 // 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
499 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
500 else
17a0e32eefd4 patch 8.2.2225: Vim9: error when using :import in legacy script twice
Bram Moolenaar <Bram@vim.org>
parents: 23362
diff changeset
501 {
17a0e32eefd4 patch 8.2.2225: Vim9: error when using :import in legacy script twice
Bram Moolenaar <Bram@vim.org>
parents: 23362
diff changeset
502 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
503 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
504 }
17a0e32eefd4 patch 8.2.2225: Vim9: error when using :import in legacy script twice
Bram Moolenaar <Bram@vim.org>
parents: 23362
diff changeset
505 }
26980
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
506 else if (check_defined(as_name, STRLEN(as_name), cctx, FALSE) == FAIL)
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
507 goto erret;
23364
17a0e32eefd4 patch 8.2.2225: Vim9: error when using :import in legacy script twice
Bram Moolenaar <Bram@vim.org>
parents: 23362
diff changeset
508
17a0e32eefd4 patch 8.2.2225: Vim9: error when using :import in legacy script twice
Bram Moolenaar <Bram@vim.org>
parents: 23362
diff changeset
509 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
510 : &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
511 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
512 goto erret;
465d6e40e79c patch 8.2.1124: Vim9: no line break allowed in :import command
Bram Moolenaar <Bram@vim.org>
parents: 21120
diff changeset
513 imported->imp_name = as_name;
26980
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
514 as_name = NULL;
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
515 imported->imp_sid = sid;
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
516 }
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
517
21146
465d6e40e79c patch 8.2.1124: Vim9: no line break allowed in :import command
Bram Moolenaar <Bram@vim.org>
parents: 21120
diff changeset
518 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
519 clear_tv(&tv);
26980
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
520 vim_free(as_name);
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
521 return arg;
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
522 }
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
523
20840
0600ab7b9f09 patch 8.2.0972: Vim9 script variable declarations need a type
Bram Moolenaar <Bram@vim.org>
parents: 20816
diff changeset
524 /*
26980
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
525 * ":import 'filename'"
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
526 * ":import 'filename' as Name"
25567
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
527 */
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
528 void
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
529 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
530 {
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
531 char_u *cmd_end;
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
532 evalarg_T evalarg;
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
533
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
534 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
535 {
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
536 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
537 return;
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
538 }
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
539 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
540
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
541 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
542 &evalarg, NULL);
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
543 if (cmd_end != NULL)
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
544 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
545 clear_evalarg(&evalarg, eap);
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
546 }
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
547
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
548 /*
26980
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
549 * Find an exported item in "sid" matching "name".
25567
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
550 * 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
551 * 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
552 * 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
553 */
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
554 int
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
555 find_exported(
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
556 int sid,
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
557 char_u *name,
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
558 ufunc_T **ufunc,
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
559 type_T **type,
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
560 cctx_T *cctx,
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
561 int verbose)
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
562 {
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
563 int idx = -1;
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
564 svar_T *sv;
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
565 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
566
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
567 // Find name in "script".
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
568 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
569 if (idx >= 0)
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
570 {
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
571 sv = ((svar_T *)script->sn_var_vals.ga_data) + idx;
26988
bef1f0167251 patch 8.2.4023: using uninitialized variable
Bram Moolenaar <Bram@vim.org>
parents: 26980
diff changeset
572 *ufunc = NULL;
25567
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
573 if (!sv->sv_export)
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
574 {
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
575 if (verbose)
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
576 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
577 return -1;
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
578 }
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
579 *type = sv->sv_type;
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
580 }
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
581 else
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
582 {
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
583 char_u buffer[200];
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
584 char_u *funcname;
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
585
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
586 // 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
587 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
588 funcname = buffer;
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
589 else
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
590 {
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
591 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
592 if (funcname == NULL)
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
593 return -1;
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
594 }
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
595 funcname[0] = K_SPECIAL;
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
596 funcname[1] = KS_EXTRA;
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
597 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
598 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
599 *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
600 if (funcname != buffer)
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
601 vim_free(funcname);
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
602
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
603 if (*ufunc == NULL)
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
604 {
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
605 if (verbose)
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
606 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
607 return -1;
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
608 }
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
609 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
610 {
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
611 if (verbose)
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
612 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
613 *ufunc = NULL;
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
614 return -1;
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
615 }
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
616 }
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
617
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
618 return idx;
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
619 }
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
620
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
621 /*
20840
0600ab7b9f09 patch 8.2.0972: Vim9 script variable declarations need a type
Bram Moolenaar <Bram@vim.org>
parents: 20816
diff changeset
622 * 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
623 * "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
624 * 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
625 */
0600ab7b9f09 patch 8.2.0972: Vim9 script variable declarations need a type
Bram Moolenaar <Bram@vim.org>
parents: 20816
diff changeset
626 char_u *
0600ab7b9f09 patch 8.2.0972: Vim9 script variable declarations need a type
Bram Moolenaar <Bram@vim.org>
parents: 20816
diff changeset
627 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
628 {
0600ab7b9f09 patch 8.2.0972: Vim9 script variable declarations need a type
Bram Moolenaar <Bram@vim.org>
parents: 20816
diff changeset
629 char_u *p;
0600ab7b9f09 patch 8.2.0972: Vim9 script variable declarations need a type
Bram Moolenaar <Bram@vim.org>
parents: 20816
diff changeset
630 char_u *name;
0600ab7b9f09 patch 8.2.0972: Vim9 script variable declarations need a type
Bram Moolenaar <Bram@vim.org>
parents: 20816
diff changeset
631 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
632 type_T *type;
0600ab7b9f09 patch 8.2.0972: Vim9 script variable declarations need a type
Bram Moolenaar <Bram@vim.org>
parents: 20816
diff changeset
633 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
634
22391
a9fb7efa31d6 patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents: 22250
diff changeset
635 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
636 {
22391
a9fb7efa31d6 patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents: 22250
diff changeset
637 if (eap->cmdidx == CMD_final)
a9fb7efa31d6 patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents: 22250
diff changeset
638 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
639 else
a9fb7efa31d6 patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents: 22250
diff changeset
640 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
641 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
642 }
0600ab7b9f09 patch 8.2.0972: Vim9 script variable declarations need a type
Bram Moolenaar <Bram@vim.org>
parents: 20816
diff changeset
643
0600ab7b9f09 patch 8.2.0972: Vim9 script variable declarations need a type
Bram Moolenaar <Bram@vim.org>
parents: 20816
diff changeset
644 // 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
645 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
646 {
26865
bce848ec8b1b patch 8.2.3961: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 26662
diff changeset
647 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
648 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
649 }
0600ab7b9f09 patch 8.2.0972: Vim9 script variable declarations need a type
Bram Moolenaar <Bram@vim.org>
parents: 20816
diff changeset
650
20846
709379ab5110 patch 8.2.0975: Vim9: script variable does not accept optional s: prefix
Bram Moolenaar <Bram@vim.org>
parents: 20844
diff changeset
651 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
652 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
653 break;
20840
0600ab7b9f09 patch 8.2.0972: Vim9 script variable declarations need a type
Bram Moolenaar <Bram@vim.org>
parents: 20816
diff changeset
654
0600ab7b9f09 patch 8.2.0972: Vim9 script variable declarations need a type
Bram Moolenaar <Bram@vim.org>
parents: 20816
diff changeset
655 if (*p != ':')
0600ab7b9f09 patch 8.2.0972: Vim9 script variable declarations need a type
Bram Moolenaar <Bram@vim.org>
parents: 20816
diff changeset
656 {
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
657 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
658 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
659 }
20846
709379ab5110 patch 8.2.0975: Vim9: script variable does not accept optional s: prefix
Bram Moolenaar <Bram@vim.org>
parents: 20844
diff changeset
660 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
661 {
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
662 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
663 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
664 }
20840
0600ab7b9f09 patch 8.2.0972: Vim9 script variable declarations need a type
Bram Moolenaar <Bram@vim.org>
parents: 20816
diff changeset
665 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
666
24717
bf8feac8a89a patch 8.2.2897: Vim9: can use reserved words at the script level
Bram Moolenaar <Bram@vim.org>
parents: 24533
diff changeset
667 // 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
668 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
669 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
670 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
671 {
1360541e8c74 patch 8.2.0974: Vim9: memory leak when script var has wrong type
Bram Moolenaar <Bram@vim.org>
parents: 20842
diff changeset
672 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
673 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
674 }
20840
0600ab7b9f09 patch 8.2.0972: Vim9 script variable declarations need a type
Bram Moolenaar <Bram@vim.org>
parents: 20816
diff changeset
675
0600ab7b9f09 patch 8.2.0972: Vim9 script variable declarations need a type
Bram Moolenaar <Bram@vim.org>
parents: 20816
diff changeset
676 // 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
677 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
678 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
679 // 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
680 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
681 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
682 init_tv.v_type = type->tt_type;
26980
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
683 set_var_const(name, 0, 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
684
0600ab7b9f09 patch 8.2.0972: Vim9 script variable declarations need a type
Bram Moolenaar <Bram@vim.org>
parents: 20816
diff changeset
685 vim_free(name);
0600ab7b9f09 patch 8.2.0972: Vim9 script variable declarations need a type
Bram Moolenaar <Bram@vim.org>
parents: 20816
diff changeset
686 return p;
0600ab7b9f09 patch 8.2.0972: Vim9 script variable declarations need a type
Bram Moolenaar <Bram@vim.org>
parents: 20816
diff changeset
687 }
0600ab7b9f09 patch 8.2.0972: Vim9 script variable declarations need a type
Bram Moolenaar <Bram@vim.org>
parents: 20816
diff changeset
688
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
689 /*
22643
71b57779177d patch 8.2.1870: Vim9: no need to keep all script variables
Bram Moolenaar <Bram@vim.org>
parents: 22596
diff changeset
690 * 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
691 * 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
692 * 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
693 * 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
694 * "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
695 * 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
696 * "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
697 */
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
698 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
699 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
700 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
701 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
702 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
703 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
704 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
705 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
706 {
23362
f181fe2150ab patch 8.2.2224: Vim9: crash if script reloaded with different variable type
Bram Moolenaar <Bram@vim.org>
parents: 23358
diff changeset
707 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
708 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
709 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
710
23362
f181fe2150ab patch 8.2.2224: Vim9: crash if script reloaded with different variable type
Bram Moolenaar <Bram@vim.org>
parents: 23358
diff changeset
711 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
712 {
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
713 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
714 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
715
f181fe2150ab patch 8.2.2224: Vim9: crash if script reloaded with different variable type
Bram Moolenaar <Bram@vim.org>
parents: 23358
diff changeset
716 // 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
717 // 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
718 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
719 return;
f181fe2150ab patch 8.2.2224: Vim9: crash if script reloaded with different variable type
Bram Moolenaar <Bram@vim.org>
parents: 23358
diff changeset
720
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
721 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
722 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
723 {
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
724 // 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
725 // 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
726 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
727 {
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
728 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
729 {
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
730 // 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
731 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
732 + 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
733 // 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
734 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
735 {
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
736 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
737 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
738 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
739 }
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
740 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
741 }
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
742 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
743 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
744 }
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
745 }
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
746
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
747 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
748 {
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
749 // 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
750 // 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
751 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
752 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
753 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
754 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
755 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
756
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
757 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
758 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
759 : (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
760 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
761 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
762 ++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
763 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
764 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
765 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
766 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
767
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
768 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
769 // 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
770 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
771 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
772 // 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
773 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
774 }
23362
f181fe2150ab patch 8.2.2224: Vim9: crash if script reloaded with different variable type
Bram Moolenaar <Bram@vim.org>
parents: 23358
diff changeset
775 }
f181fe2150ab patch 8.2.2224: Vim9: crash if script reloaded with different variable type
Bram Moolenaar <Bram@vim.org>
parents: 23358
diff changeset
776 else
f181fe2150ab patch 8.2.2224: Vim9: crash if script reloaded with different variable type
Bram Moolenaar <Bram@vim.org>
parents: 23358
diff changeset
777 {
26980
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
778 sv = find_typval_in_script(&di->di_tv, 0);
23362
f181fe2150ab patch 8.2.2224: Vim9: crash if script reloaded with different variable type
Bram Moolenaar <Bram@vim.org>
parents: 23358
diff changeset
779 }
f181fe2150ab patch 8.2.2224: Vim9: crash if script reloaded with different variable type
Bram Moolenaar <Bram@vim.org>
parents: 23358
diff changeset
780 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
781 {
23458
d2b1269c2c68 patch 8.2.2272: Vim9: extend() can violate the type of a variable
Bram Moolenaar <Bram@vim.org>
parents: 23392
diff changeset
782 if (*type == NULL)
26925
4e77f9961650 patch 8.2.3991: Vim9: error when extending dict<any>
Bram Moolenaar <Bram@vim.org>
parents: 26877
diff changeset
783 *type = typval2type(tv, get_copyID(), &si->sn_type_list,
4e77f9961650 patch 8.2.3991: Vim9: error when extending dict<any>
Bram Moolenaar <Bram@vim.org>
parents: 26877
diff changeset
784 do_member ? TVTT_DO_MEMBER : 0);
26482
b115b552071f patch 8.2.3771: Vim9: accessing freed memory when checking type
Bram Moolenaar <Bram@vim.org>
parents: 26302
diff changeset
785 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
786 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
787 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
788 || (*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
789 {
b115b552071f patch 8.2.3771: Vim9: accessing freed memory when checking type
Bram Moolenaar <Bram@vim.org>
parents: 26302
diff changeset
790 // 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
791 // 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
792 // 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
793 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
794 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
795 }
b115b552071f patch 8.2.3771: Vim9: accessing freed memory when checking type
Bram Moolenaar <Bram@vim.org>
parents: 26302
diff changeset
796 else
b115b552071f patch 8.2.3771: Vim9: accessing freed memory when checking type
Bram Moolenaar <Bram@vim.org>
parents: 26302
diff changeset
797 {
b115b552071f patch 8.2.3771: Vim9: accessing freed memory when checking type
Bram Moolenaar <Bram@vim.org>
parents: 26302
diff changeset
798 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
799 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
800 }
23362
f181fe2150ab patch 8.2.2224: Vim9: crash if script reloaded with different variable type
Bram Moolenaar <Bram@vim.org>
parents: 23358
diff changeset
801 }
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
802
23362
f181fe2150ab patch 8.2.2224: Vim9: crash if script reloaded with different variable type
Bram Moolenaar <Bram@vim.org>
parents: 23358
diff changeset
803 // 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
804 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
805 }
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
806
22596
107eae953b87 patch 8.2.1846: Vim9: block variables are not found in compiled function
Bram Moolenaar <Bram@vim.org>
parents: 22594
diff changeset
807 /*
107eae953b87 patch 8.2.1846: Vim9: block variables are not found in compiled function
Bram Moolenaar <Bram@vim.org>
parents: 22594
diff changeset
808 * 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
809 * "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
810 * 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
811 * 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
812 */
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
813 void
22643
71b57779177d patch 8.2.1870: Vim9: no need to keep all script variables
Bram Moolenaar <Bram@vim.org>
parents: 22596
diff changeset
814 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
815 {
22596
107eae953b87 patch 8.2.1846: Vim9: block variables are not found in compiled function
Bram Moolenaar <Bram@vim.org>
parents: 22594
diff changeset
816 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
817 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
818 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
819 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
820 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
821
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
822 // 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
823 // 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
824 // 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
825 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
826 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
827 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
828 {
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
829 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
830 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
831 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
832
22596
107eae953b87 patch 8.2.1846: Vim9: block variables are not found in compiled function
Bram Moolenaar <Bram@vim.org>
parents: 22594
diff changeset
833 // 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
834 // 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
835 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
836 {
71b57779177d patch 8.2.1870: Vim9: no need to keep all script variables
Bram Moolenaar <Bram@vim.org>
parents: 22596
diff changeset
837 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
838 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
839 }
22596
107eae953b87 patch 8.2.1846: Vim9: block variables are not found in compiled function
Bram Moolenaar <Bram@vim.org>
parents: 22594
diff changeset
840 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
841 {
22643
71b57779177d patch 8.2.1870: Vim9: no need to keep all script variables
Bram Moolenaar <Bram@vim.org>
parents: 22596
diff changeset
842 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
843 {
71b57779177d patch 8.2.1870: Vim9: no need to keep all script variables
Bram Moolenaar <Bram@vim.org>
parents: 22596
diff changeset
844 // 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
845 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
846 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
847 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
848 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
849 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
850 }
71b57779177d patch 8.2.1870: Vim9: no need to keep all script variables
Bram Moolenaar <Bram@vim.org>
parents: 22596
diff changeset
851 else
71b57779177d patch 8.2.1870: Vim9: no need to keep all script variables
Bram Moolenaar <Bram@vim.org>
parents: 22596
diff changeset
852 {
71b57779177d patch 8.2.1870: Vim9: no need to keep all script variables
Bram Moolenaar <Bram@vim.org>
parents: 22596
diff changeset
853 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
854 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
855 else
71b57779177d patch 8.2.1870: Vim9: no need to keep all script variables
Bram Moolenaar <Bram@vim.org>
parents: 22596
diff changeset
856 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
857 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
858 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
859 }
22596
107eae953b87 patch 8.2.1846: Vim9: block variables are not found in compiled function
Bram Moolenaar <Bram@vim.org>
parents: 22594
diff changeset
860 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
861 }
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
862 }
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
863 }
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
864
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
865 /*
22529
35ef9b0a81a3 patch 8.2.1813: Vim9: can assign wrong type to script dict
Bram Moolenaar <Bram@vim.org>
parents: 22391
diff changeset
866 * Find the script-local variable that links to "dest".
26980
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
867 * If "sid" is zero use the current script.
25186
0a3b1c66d3f2 patch 8.2.3129: Vim9: imported uninitialized list does not get type checked
Bram Moolenaar <Bram@vim.org>
parents: 25184
diff changeset
868 * 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
869 */
22529
35ef9b0a81a3 patch 8.2.1813: Vim9: can assign wrong type to script dict
Bram Moolenaar <Bram@vim.org>
parents: 22391
diff changeset
870 svar_T *
26980
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
871 find_typval_in_script(typval_T *dest, scid_T sid)
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
872 {
26980
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
873 scriptitem_T *si = SCRIPT_ITEM(sid == 0 ? current_sctx.sc_sid : sid);
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
874 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
875
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
876 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
877 // 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
878 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
879
25955
4003fc2340dc patch 8.2.3511: Vim9: entry for loop variable is created every round
Bram Moolenaar <Bram@vim.org>
parents: 25755
diff changeset
880 // 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
881 // 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
882 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
883 {
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
884 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
885
23094
8b93aea8d9d7 patch 8.2.2093: Vim9: script test sometimes fails
Bram Moolenaar <Bram@vim.org>
parents: 22740
diff changeset
886 // 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
887 // 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
888 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
889 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
890 }
25186
0a3b1c66d3f2 patch 8.2.3129: Vim9: imported uninitialized list does not get type checked
Bram Moolenaar <Bram@vim.org>
parents: 25184
diff changeset
891 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
892 return NULL;
35ef9b0a81a3 patch 8.2.1813: Vim9: can assign wrong type to script dict
Bram Moolenaar <Bram@vim.org>
parents: 22391
diff changeset
893 }
35ef9b0a81a3 patch 8.2.1813: Vim9: can assign wrong type to script dict
Bram Moolenaar <Bram@vim.org>
parents: 22391
diff changeset
894
35ef9b0a81a3 patch 8.2.1813: Vim9: can assign wrong type to script dict
Bram Moolenaar <Bram@vim.org>
parents: 22391
diff changeset
895 /*
35ef9b0a81a3 patch 8.2.1813: Vim9: can assign wrong type to script dict
Bram Moolenaar <Bram@vim.org>
parents: 22391
diff changeset
896 * 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
897 * 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
898 */
35ef9b0a81a3 patch 8.2.1813: Vim9: can assign wrong type to script dict
Bram Moolenaar <Bram@vim.org>
parents: 22391
diff changeset
899 int
23917
4b417b776b95 patch 8.2.2501: not always clear where an error is reported
Bram Moolenaar <Bram@vim.org>
parents: 23877
diff changeset
900 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
901 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
902 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
903 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
904 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
905 {
35ef9b0a81a3 patch 8.2.1813: Vim9: can assign wrong type to script dict
Bram Moolenaar <Bram@vim.org>
parents: 22391
diff changeset
906 int ret;
35ef9b0a81a3 patch 8.2.1813: Vim9: can assign wrong type to script dict
Bram Moolenaar <Bram@vim.org>
parents: 22391
diff changeset
907
26302
7351926fbe9e patch 8.2.3682: Vim9: assigning to a script variable drops the type
Bram Moolenaar <Bram@vim.org>
parents: 25955
diff changeset
908 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
909 {
7351926fbe9e patch 8.2.3682: Vim9: assigning to a script variable drops the type
Bram Moolenaar <Bram@vim.org>
parents: 25955
diff changeset
910 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
911 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
912 }
7351926fbe9e patch 8.2.3682: Vim9: assigning to a script variable drops the type
Bram Moolenaar <Bram@vim.org>
parents: 25955
diff changeset
913 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
914 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
915 {
26302
7351926fbe9e patch 8.2.3682: Vim9: assigning to a script variable drops the type
Bram Moolenaar <Bram@vim.org>
parents: 25955
diff changeset
916 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
917
26302
7351926fbe9e patch 8.2.3682: Vim9: assigning to a script variable drops the type
Bram Moolenaar <Bram@vim.org>
parents: 25955
diff changeset
918 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
919 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
920 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
921 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
922 }
26302
7351926fbe9e patch 8.2.3682: Vim9: assigning to a script variable drops the type
Bram Moolenaar <Bram@vim.org>
parents: 25955
diff changeset
923 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
924 }
20840
0600ab7b9f09 patch 8.2.0972: Vim9 script variable declarations need a type
Bram Moolenaar <Bram@vim.org>
parents: 20816
diff changeset
925
24717
bf8feac8a89a patch 8.2.2897: Vim9: can use reserved words at the script level
Bram Moolenaar <Bram@vim.org>
parents: 24533
diff changeset
926 // 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
927 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
928 "true",
bf8feac8a89a patch 8.2.2897: Vim9: can use reserved words at the script level
Bram Moolenaar <Bram@vim.org>
parents: 24533
diff changeset
929 "false",
bf8feac8a89a patch 8.2.2897: Vim9: can use reserved words at the script level
Bram Moolenaar <Bram@vim.org>
parents: 24533
diff changeset
930 "null",
24888
b6ac4ed5e2d2 patch 8.2.2982: Vim9: future commands are not reserved yet
Bram Moolenaar <Bram@vim.org>
parents: 24717
diff changeset
931 "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
932 NULL
bf8feac8a89a patch 8.2.2897: Vim9: can use reserved words at the script level
Bram Moolenaar <Bram@vim.org>
parents: 24533
diff changeset
933 };
bf8feac8a89a patch 8.2.2897: Vim9: can use reserved words at the script level
Bram Moolenaar <Bram@vim.org>
parents: 24533
diff changeset
934
bf8feac8a89a patch 8.2.2897: Vim9: can use reserved words at the script level
Bram Moolenaar <Bram@vim.org>
parents: 24533
diff changeset
935 int
bf8feac8a89a patch 8.2.2897: Vim9: can use reserved words at the script level
Bram Moolenaar <Bram@vim.org>
parents: 24533
diff changeset
936 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
937 {
bf8feac8a89a patch 8.2.2897: Vim9: can use reserved words at the script level
Bram Moolenaar <Bram@vim.org>
parents: 24533
diff changeset
938 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
939
bf8feac8a89a patch 8.2.2897: Vim9: can use reserved words at the script level
Bram Moolenaar <Bram@vim.org>
parents: 24533
diff changeset
940 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
941 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
942 {
bf8feac8a89a patch 8.2.2897: Vim9: can use reserved words at the script level
Bram Moolenaar <Bram@vim.org>
parents: 24533
diff changeset
943 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
944 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
945 }
bf8feac8a89a patch 8.2.2897: Vim9: can use reserved words at the script level
Bram Moolenaar <Bram@vim.org>
parents: 24533
diff changeset
946 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
947 }
bf8feac8a89a patch 8.2.2897: Vim9: can use reserved words at the script level
Bram Moolenaar <Bram@vim.org>
parents: 24533
diff changeset
948
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
949 #endif // FEAT_EVAL