annotate src/vim9script.c @ 29234:96ff6c230a66 v8.2.5136

patch 8.2.5136: debugger test fails when run with valgrind Commit: https://github.com/vim/vim/commit/e366ed4f2c6fa8cb663f1b9599b39d57ddbd8a2a Author: Bram Moolenaar <Bram@vim.org> Date: Sun Jun 19 20:13:56 2022 +0100 patch 8.2.5136: debugger test fails when run with valgrind Problem: Debugger test fails when run with valgrind. Solution: Wait longer when using valgrind.
author Bram Moolenaar <Bram@vim.org>
date Sun, 19 Jun 2022 21:15:03 +0200
parents 99a7eb1881c7
children 842690841d50
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
28164
b0b712d48225 patch 8.2.4607: sourcing buffer lines may lead to errors for conflicts
Bram Moolenaar <Bram@vim.org>
parents: 28139
diff changeset
62 #ifdef FEAT_EVAL
b0b712d48225 patch 8.2.4607: sourcing buffer lines may lead to errors for conflicts
Bram Moolenaar <Bram@vim.org>
parents: 28139
diff changeset
63 /*
b0b712d48225 patch 8.2.4607: sourcing buffer lines may lead to errors for conflicts
Bram Moolenaar <Bram@vim.org>
parents: 28139
diff changeset
64 * Clear Vim9 script-local variables and functions.
b0b712d48225 patch 8.2.4607: sourcing buffer lines may lead to errors for conflicts
Bram Moolenaar <Bram@vim.org>
parents: 28139
diff changeset
65 */
b0b712d48225 patch 8.2.4607: sourcing buffer lines may lead to errors for conflicts
Bram Moolenaar <Bram@vim.org>
parents: 28139
diff changeset
66 void
b0b712d48225 patch 8.2.4607: sourcing buffer lines may lead to errors for conflicts
Bram Moolenaar <Bram@vim.org>
parents: 28139
diff changeset
67 clear_vim9_scriptlocal_vars(int sid)
b0b712d48225 patch 8.2.4607: sourcing buffer lines may lead to errors for conflicts
Bram Moolenaar <Bram@vim.org>
parents: 28139
diff changeset
68 {
b0b712d48225 patch 8.2.4607: sourcing buffer lines may lead to errors for conflicts
Bram Moolenaar <Bram@vim.org>
parents: 28139
diff changeset
69 hashtab_T *ht = &SCRIPT_VARS(sid);
b0b712d48225 patch 8.2.4607: sourcing buffer lines may lead to errors for conflicts
Bram Moolenaar <Bram@vim.org>
parents: 28139
diff changeset
70
b0b712d48225 patch 8.2.4607: sourcing buffer lines may lead to errors for conflicts
Bram Moolenaar <Bram@vim.org>
parents: 28139
diff changeset
71 hashtab_free_contents(ht);
b0b712d48225 patch 8.2.4607: sourcing buffer lines may lead to errors for conflicts
Bram Moolenaar <Bram@vim.org>
parents: 28139
diff changeset
72 hash_init(ht);
b0b712d48225 patch 8.2.4607: sourcing buffer lines may lead to errors for conflicts
Bram Moolenaar <Bram@vim.org>
parents: 28139
diff changeset
73 delete_script_functions(sid);
b0b712d48225 patch 8.2.4607: sourcing buffer lines may lead to errors for conflicts
Bram Moolenaar <Bram@vim.org>
parents: 28139
diff changeset
74
b0b712d48225 patch 8.2.4607: sourcing buffer lines may lead to errors for conflicts
Bram Moolenaar <Bram@vim.org>
parents: 28139
diff changeset
75 // old imports and script variables are no longer valid
b0b712d48225 patch 8.2.4607: sourcing buffer lines may lead to errors for conflicts
Bram Moolenaar <Bram@vim.org>
parents: 28139
diff changeset
76 free_imports_and_script_vars(sid);
b0b712d48225 patch 8.2.4607: sourcing buffer lines may lead to errors for conflicts
Bram Moolenaar <Bram@vim.org>
parents: 28139
diff changeset
77 }
b0b712d48225 patch 8.2.4607: sourcing buffer lines may lead to errors for conflicts
Bram Moolenaar <Bram@vim.org>
parents: 28139
diff changeset
78 #endif
b0b712d48225 patch 8.2.4607: sourcing buffer lines may lead to errors for conflicts
Bram Moolenaar <Bram@vim.org>
parents: 28139
diff changeset
79
24279
e3dbf2e58c6a patch 8.2.2680: Vim9: problem defining a script variable from legacy function
Bram Moolenaar <Bram@vim.org>
parents: 24176
diff changeset
80 /*
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
81 * ":vim9script".
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
82 */
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
83 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
84 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
85 {
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
86 #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
87 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
88 scriptitem_T *si;
27043
15f40772e10a patch 8.2.4050: Vim9: need to prefix every item in an autoload script
Bram Moolenaar <Bram@vim.org>
parents: 27030
diff changeset
89 int found_noclear = FALSE;
15f40772e10a patch 8.2.4050: Vim9: need to prefix every item in an autoload script
Bram Moolenaar <Bram@vim.org>
parents: 27030
diff changeset
90 char_u *p;
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
91
28139
f34afadbef47 patch 8.2.4594: need to write script to a file to be able to source them
Bram Moolenaar <Bram@vim.org>
parents: 28002
diff changeset
92 if (!sourcing_a_script(eap))
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
93 {
21821
0deb6f96a5a3 patch 8.2.1460: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 21789
diff changeset
94 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
95 return;
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
96 }
23358
b3142fc0a414 patch 8.2.2222: Vim9: cannot keep script variables when reloading
Bram Moolenaar <Bram@vim.org>
parents: 23332
diff changeset
97
b3142fc0a414 patch 8.2.2222: Vim9: cannot keep script variables when reloading
Bram Moolenaar <Bram@vim.org>
parents: 23332
diff changeset
98 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
99 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
100 {
21821
0deb6f96a5a3 patch 8.2.1460: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 21789
diff changeset
101 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
102 return;
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
103 }
27043
15f40772e10a patch 8.2.4050: Vim9: need to prefix every item in an autoload script
Bram Moolenaar <Bram@vim.org>
parents: 27030
diff changeset
104
15f40772e10a patch 8.2.4050: Vim9: need to prefix every item in an autoload script
Bram Moolenaar <Bram@vim.org>
parents: 27030
diff changeset
105 for (p = eap->arg; !IS_WHITE_OR_NUL(*p); p = skipwhite(skiptowhite(p)))
23358
b3142fc0a414 patch 8.2.2222: Vim9: cannot keep script variables when reloading
Bram Moolenaar <Bram@vim.org>
parents: 23332
diff changeset
106 {
27043
15f40772e10a patch 8.2.4050: Vim9: need to prefix every item in an autoload script
Bram Moolenaar <Bram@vim.org>
parents: 27030
diff changeset
107 if (STRNCMP(p, "noclear", 7) == 0 && IS_WHITE_OR_NUL(p[7]))
15f40772e10a patch 8.2.4050: Vim9: need to prefix every item in an autoload script
Bram Moolenaar <Bram@vim.org>
parents: 27030
diff changeset
108 {
15f40772e10a patch 8.2.4050: Vim9: need to prefix every item in an autoload script
Bram Moolenaar <Bram@vim.org>
parents: 27030
diff changeset
109 if (found_noclear)
15f40772e10a patch 8.2.4050: Vim9: need to prefix every item in an autoload script
Bram Moolenaar <Bram@vim.org>
parents: 27030
diff changeset
110 {
15f40772e10a patch 8.2.4050: Vim9: need to prefix every item in an autoload script
Bram Moolenaar <Bram@vim.org>
parents: 27030
diff changeset
111 semsg(_(e_duplicate_argument_str), p);
15f40772e10a patch 8.2.4050: Vim9: need to prefix every item in an autoload script
Bram Moolenaar <Bram@vim.org>
parents: 27030
diff changeset
112 return;
15f40772e10a patch 8.2.4050: Vim9: need to prefix every item in an autoload script
Bram Moolenaar <Bram@vim.org>
parents: 27030
diff changeset
113 }
15f40772e10a patch 8.2.4050: Vim9: need to prefix every item in an autoload script
Bram Moolenaar <Bram@vim.org>
parents: 27030
diff changeset
114 found_noclear = TRUE;
15f40772e10a patch 8.2.4050: Vim9: need to prefix every item in an autoload script
Bram Moolenaar <Bram@vim.org>
parents: 27030
diff changeset
115 }
15f40772e10a patch 8.2.4050: Vim9: need to prefix every item in an autoload script
Bram Moolenaar <Bram@vim.org>
parents: 27030
diff changeset
116 else
15f40772e10a patch 8.2.4050: Vim9: need to prefix every item in an autoload script
Bram Moolenaar <Bram@vim.org>
parents: 27030
diff changeset
117 {
15f40772e10a patch 8.2.4050: Vim9: need to prefix every item in an autoload script
Bram Moolenaar <Bram@vim.org>
parents: 27030
diff changeset
118 semsg(_(e_invalid_argument_str), eap->arg);
15f40772e10a patch 8.2.4050: Vim9: need to prefix every item in an autoload script
Bram Moolenaar <Bram@vim.org>
parents: 27030
diff changeset
119 return;
15f40772e10a patch 8.2.4050: Vim9: need to prefix every item in an autoload script
Bram Moolenaar <Bram@vim.org>
parents: 27030
diff changeset
120 }
23358
b3142fc0a414 patch 8.2.2222: Vim9: cannot keep script variables when reloading
Bram Moolenaar <Bram@vim.org>
parents: 23332
diff changeset
121 }
27043
15f40772e10a patch 8.2.4050: Vim9: need to prefix every item in an autoload script
Bram Moolenaar <Bram@vim.org>
parents: 27030
diff changeset
122
15f40772e10a patch 8.2.4050: Vim9: need to prefix every item in an autoload script
Bram Moolenaar <Bram@vim.org>
parents: 27030
diff changeset
123 if (si->sn_state == SN_STATE_RELOAD && !found_noclear)
23358
b3142fc0a414 patch 8.2.2222: Vim9: cannot keep script variables when reloading
Bram Moolenaar <Bram@vim.org>
parents: 23332
diff changeset
124 // 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
125 // script-local variables and functions.
28164
b0b712d48225 patch 8.2.4607: sourcing buffer lines may lead to errors for conflicts
Bram Moolenaar <Bram@vim.org>
parents: 28139
diff changeset
126 clear_vim9_scriptlocal_vars(sid);
23358
b3142fc0a414 patch 8.2.2222: Vim9: cannot keep script variables when reloading
Bram Moolenaar <Bram@vim.org>
parents: 23332
diff changeset
127 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
128
27114
98a01021e465 patch 8.2.4086: "cctx" argument of find_func_even_dead() is unused
Bram Moolenaar <Bram@vim.org>
parents: 27094
diff changeset
129 // Store the prefix with the script, it is used to find exported functions.
27094
70ad00a80185 patch 8.2.4076: memory leak in autoload import
Bram Moolenaar <Bram@vim.org>
parents: 27086
diff changeset
130 if (si->sn_autoload_prefix == NULL)
70ad00a80185 patch 8.2.4076: memory leak in autoload import
Bram Moolenaar <Bram@vim.org>
parents: 27086
diff changeset
131 si->sn_autoload_prefix = get_autoload_prefix(si);
27043
15f40772e10a patch 8.2.4050: Vim9: need to prefix every item in an autoload script
Bram Moolenaar <Bram@vim.org>
parents: 27030
diff changeset
132
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
133 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
134 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
135
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
136 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
137 {
22643
71b57779177d patch 8.2.1870: Vim9: no need to keep all script variables
Bram Moolenaar <Bram@vim.org>
parents: 22596
diff changeset
138 si->sn_save_cpo = vim_strsave(p_cpo);
28457
4dcccb2673fe patch 8.2.4753: error from setting an option is silently ignored
Bram Moolenaar <Bram@vim.org>
parents: 28345
diff changeset
139 set_option_value_give_err((char_u *)"cpo",
4dcccb2673fe patch 8.2.4753: error from setting an option is silently ignored
Bram Moolenaar <Bram@vim.org>
parents: 28345
diff changeset
140 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
141 }
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
142 #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
143 // 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
144 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
145 #endif
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
146 }
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
147
27018
268f6a3511df patch 8.2.4038: various code not used when features are disabled
Bram Moolenaar <Bram@vim.org>
parents: 26992
diff changeset
148 #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
149 /*
21516
c7b2ce90c2de patch 8.2.1308: Vim9: accidentally using "x" causes Vim to exit
Bram Moolenaar <Bram@vim.org>
parents: 21473
diff changeset
150 * 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
151 */
c7b2ce90c2de patch 8.2.1308: Vim9: accidentally using "x" causes Vim to exit
Bram Moolenaar <Bram@vim.org>
parents: 21473
diff changeset
152 int
c7b2ce90c2de patch 8.2.1308: Vim9: accidentally using "x" causes Vim to exit
Bram Moolenaar <Bram@vim.org>
parents: 21473
diff changeset
153 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
154 {
21522
e17d0b882194 patch 8.2.1311: test failures with legacy Vim script
Bram Moolenaar <Bram@vim.org>
parents: 21516
diff changeset
155 if (in_vim9script())
e17d0b882194 patch 8.2.1311: test failures with legacy Vim script
Bram Moolenaar <Bram@vim.org>
parents: 21516
diff changeset
156 switch (eap->cmdidx)
e17d0b882194 patch 8.2.1311: test failures with legacy Vim script
Bram Moolenaar <Bram@vim.org>
parents: 21516
diff changeset
157 {
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
158 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
159 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
160 {
26877
06a137af96f8 patch 8.2.3967: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 26865
diff changeset
161 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
162 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
163 }
bee8c78c0c6a patch 8.2.2532: Vim9: confusing error if :k is used with a range
Bram Moolenaar <Bram@vim.org>
parents: 23978
diff changeset
164 // FALLTHROUGH
21522
e17d0b882194 patch 8.2.1311: test failures with legacy Vim script
Bram Moolenaar <Bram@vim.org>
parents: 21516
diff changeset
165 case CMD_append:
e17d0b882194 patch 8.2.1311: test failures with legacy Vim script
Bram Moolenaar <Bram@vim.org>
parents: 21516
diff changeset
166 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
167 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
168 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
169 case CMD_t:
21522
e17d0b882194 patch 8.2.1311: test failures with legacy Vim script
Bram Moolenaar <Bram@vim.org>
parents: 21516
diff changeset
170 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
171 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
172 return FAIL;
e17d0b882194 patch 8.2.1311: test failures with legacy Vim script
Bram Moolenaar <Bram@vim.org>
parents: 21516
diff changeset
173 default: break;
e17d0b882194 patch 8.2.1311: test failures with legacy Vim script
Bram Moolenaar <Bram@vim.org>
parents: 21516
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 return OK;
c7b2ce90c2de patch 8.2.1308: Vim9: accidentally using "x" causes Vim to exit
Bram Moolenaar <Bram@vim.org>
parents: 21473
diff changeset
176 }
c7b2ce90c2de patch 8.2.1308: Vim9: accidentally using "x" causes Vim to exit
Bram Moolenaar <Bram@vim.org>
parents: 21473
diff changeset
177
23392
517fca70e084 patch 8.2.2239: Vim9: concatenating lines with backslash is inconvenient
Bram Moolenaar <Bram@vim.org>
parents: 23390
diff changeset
178 /*
24158
93e69703a290 patch 8.2.2620: Vim9: Using #{ for a dictionary gives strange errors
Bram Moolenaar <Bram@vim.org>
parents: 24114
diff changeset
179 * 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
180 * 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
181 * understand errors.
93e69703a290 patch 8.2.2620: Vim9: Using #{ for a dictionary gives strange errors
Bram Moolenaar <Bram@vim.org>
parents: 24114
diff changeset
182 */
93e69703a290 patch 8.2.2620: Vim9: Using #{ for a dictionary gives strange errors
Bram Moolenaar <Bram@vim.org>
parents: 24114
diff changeset
183 int
93e69703a290 patch 8.2.2620: Vim9: Using #{ for a dictionary gives strange errors
Bram Moolenaar <Bram@vim.org>
parents: 24114
diff changeset
184 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
185 {
24176
12378fbc99bc patch 8.2.2629: Vim9: error for #{{ is not desired
Bram Moolenaar <Bram@vim.org>
parents: 24160
diff changeset
186 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
187 {
93e69703a290 patch 8.2.2620: Vim9: Using #{ for a dictionary gives strange errors
Bram Moolenaar <Bram@vim.org>
parents: 24114
diff changeset
188 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
189 return TRUE;
93e69703a290 patch 8.2.2620: Vim9: Using #{ for a dictionary gives strange errors
Bram Moolenaar <Bram@vim.org>
parents: 24114
diff changeset
190 }
93e69703a290 patch 8.2.2620: Vim9: Using #{ for a dictionary gives strange errors
Bram Moolenaar <Bram@vim.org>
parents: 24114
diff changeset
191 return FALSE;
93e69703a290 patch 8.2.2620: Vim9: Using #{ for a dictionary gives strange errors
Bram Moolenaar <Bram@vim.org>
parents: 24114
diff changeset
192 }
27018
268f6a3511df patch 8.2.4038: various code not used when features are disabled
Bram Moolenaar <Bram@vim.org>
parents: 26992
diff changeset
193 #endif
24158
93e69703a290 patch 8.2.2620: Vim9: Using #{ for a dictionary gives strange errors
Bram Moolenaar <Bram@vim.org>
parents: 24114
diff changeset
194
93e69703a290 patch 8.2.2620: Vim9: Using #{ for a dictionary gives strange errors
Bram Moolenaar <Bram@vim.org>
parents: 24114
diff changeset
195 /*
24176
12378fbc99bc patch 8.2.2629: Vim9: error for #{{ is not desired
Bram Moolenaar <Bram@vim.org>
parents: 24160
diff changeset
196 * 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
197 * 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
198 */
517fca70e084 patch 8.2.2239: Vim9: concatenating lines with backslash is inconvenient
Bram Moolenaar <Bram@vim.org>
parents: 23390
diff changeset
199 int
517fca70e084 patch 8.2.2239: Vim9: concatenating lines with backslash is inconvenient
Bram Moolenaar <Bram@vim.org>
parents: 23390
diff changeset
200 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
201 {
24176
12378fbc99bc patch 8.2.2629: Vim9: error for #{{ is not desired
Bram Moolenaar <Bram@vim.org>
parents: 24160
diff changeset
202 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
203 }
517fca70e084 patch 8.2.2239: Vim9: concatenating lines with backslash is inconvenient
Bram Moolenaar <Bram@vim.org>
parents: 23390
diff changeset
204
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
205 #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
206
21516
c7b2ce90c2de patch 8.2.1308: Vim9: accidentally using "x" causes Vim to exit
Bram Moolenaar <Bram@vim.org>
parents: 21473
diff changeset
207 /*
24533
9c404d78d767 patch 8.2.2806: Vim9: using "++nr" as a command might not work
Bram Moolenaar <Bram@vim.org>
parents: 24531
diff changeset
208 * "++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
209 */
9c404d78d767 patch 8.2.2806: Vim9: using "++nr" as a command might not work
Bram Moolenaar <Bram@vim.org>
parents: 24531
diff changeset
210 void
9c404d78d767 patch 8.2.2806: Vim9: using "++nr" as a command might not work
Bram Moolenaar <Bram@vim.org>
parents: 24531
diff changeset
211 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
212 {
9c404d78d767 patch 8.2.2806: Vim9: using "++nr" as a command might not work
Bram Moolenaar <Bram@vim.org>
parents: 24531
diff changeset
213 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
214 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
215 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
216
25022
39551b6e0112 patch 8.2.3048: strange error for white space after ++ command
Bram Moolenaar <Bram@vim.org>
parents: 25020
diff changeset
217 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
218 {
39551b6e0112 patch 8.2.3048: strange error for white space after ++ command
Bram Moolenaar <Bram@vim.org>
parents: 25020
diff changeset
219 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
220 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
221 return;
39551b6e0112 patch 8.2.3048: strange error for white space after ++ command
Bram Moolenaar <Bram@vim.org>
parents: 25020
diff changeset
222 }
39551b6e0112 patch 8.2.3048: strange error for white space after ++ command
Bram Moolenaar <Bram@vim.org>
parents: 25020
diff changeset
223
24533
9c404d78d767 patch 8.2.2806: Vim9: using "++nr" as a command might not work
Bram Moolenaar <Bram@vim.org>
parents: 24531
diff changeset
224 // 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
225 // 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
226 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
227 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
228 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
229 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
230 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
231 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
232 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
233 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
234 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
235 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
236 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
237 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
238 }
9c404d78d767 patch 8.2.2806: Vim9: using "++nr" as a command might not work
Bram Moolenaar <Bram@vim.org>
parents: 24531
diff changeset
239
9c404d78d767 patch 8.2.2806: Vim9: using "++nr" as a command might not work
Bram Moolenaar <Bram@vim.org>
parents: 24531
diff changeset
240 /*
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
241 * ":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
242 * ":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
243 * ":export def Name(..."
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
244 * ":export class Name ..."
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
245 */
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
246 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
247 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
248 {
27074
19fefc42a063 patch 8.2.4066: Vim9: imported autoload script loaded again
Bram Moolenaar <Bram@vim.org>
parents: 27057
diff changeset
249 int prev_did_emsg = did_emsg;
19fefc42a063 patch 8.2.4066: Vim9: imported autoload script loaded again
Bram Moolenaar <Bram@vim.org>
parents: 27057
diff changeset
250
21279
8d1d11afd8c8 patch 8.2.1190: Vim9: checking for Vim9 syntax is spread out
Bram Moolenaar <Bram@vim.org>
parents: 21218
diff changeset
251 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
252 {
21821
0deb6f96a5a3 patch 8.2.1460: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 21789
diff changeset
253 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
254 return;
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
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
257 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
258 (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
259 switch (eap->cmdidx)
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
260 {
22391
a9fb7efa31d6 patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents: 22250
diff changeset
261 case CMD_var:
a9fb7efa31d6 patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents: 22250
diff changeset
262 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
263 case CMD_const:
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
264 case CMD_def:
27057
8f6cab688901 patch 8.2.4057: Vim9: not fully implementing the autoload mechanism
Bram Moolenaar <Bram@vim.org>
parents: 27049
diff changeset
265 case CMD_function:
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
266 // case CMD_class:
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
267 is_export = TRUE;
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
268 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
269 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
270
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
271 // 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
272 if (is_export)
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
273 {
27074
19fefc42a063 patch 8.2.4066: Vim9: imported autoload script loaded again
Bram Moolenaar <Bram@vim.org>
parents: 27057
diff changeset
274 if (did_emsg == prev_did_emsg)
19fefc42a063 patch 8.2.4066: Vim9: imported autoload script loaded again
Bram Moolenaar <Bram@vim.org>
parents: 27057
diff changeset
275 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
276 is_export = FALSE;
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
277 }
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
278 break;
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
279 default:
27074
19fefc42a063 patch 8.2.4066: Vim9: imported autoload script loaded again
Bram Moolenaar <Bram@vim.org>
parents: 27057
diff changeset
280 if (did_emsg == prev_did_emsg)
19fefc42a063 patch 8.2.4066: Vim9: imported autoload script loaded again
Bram Moolenaar <Bram@vim.org>
parents: 27057
diff changeset
281 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
282 break;
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
283 }
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
284 }
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
285
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
286 /*
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
287 * 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
288 */
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
289 static imported_T *
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
290 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
291 {
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
292 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
293 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
294 return NULL;
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
295 }
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
296
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
297 /*
25567
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
298 * 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
299 */
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
300 static void
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
301 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
302 {
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
303 int todo;
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
304 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
305 hashitem_T *hi;
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
306 sallvar_T *sav;
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
307 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
308 int idx;
25567
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 hash_lock(ht);
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
311 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
312 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
313 {
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
314 if (!HASHITEM_EMPTY(hi))
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
315 {
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
316 --todo;
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
317
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
318 // 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
319 // 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
320 sav = HI2SAV(hi);
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
321 while (sav != NULL)
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
322 {
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
323 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
324 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
325 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
326 vim_free(sav);
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
327 sav = sav_next;
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
328 }
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
329 }
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
330 }
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
331 hash_clear(ht);
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
332 hash_init(ht);
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
333
26482
b115b552071f patch 8.2.3771: Vim9: accessing freed memory when checking type
Bram Moolenaar <Bram@vim.org>
parents: 26302
diff changeset
334 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
335 {
b115b552071f patch 8.2.3771: Vim9: accessing freed memory when checking type
Bram Moolenaar <Bram@vim.org>
parents: 26302
diff changeset
336 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
337
28345
fabe722b24e9 patch 8.2.4698: Vim9: script variable has no flag that it was set
Bram Moolenaar <Bram@vim.org>
parents: 28313
diff changeset
338 if (sv->sv_flags & SVFLAG_TYPE_ALLOCATED)
26482
b115b552071f patch 8.2.3771: Vim9: accessing freed memory when checking type
Bram Moolenaar <Bram@vim.org>
parents: 26302
diff changeset
339 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
340 }
25567
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
341 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
342
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
343 // 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
344 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
345 }
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
346
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
347 /*
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
348 * 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
349 */
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
350 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
351 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
352 {
19191
133ef7ba4e4e patch 8.2.0154: reallocating the list of scripts is inefficient
Bram Moolenaar <Bram@vim.org>
parents: 19181
diff changeset
353 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
354 int idx;
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
355
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
356 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
357 {
19726
ad37a198a708 patch 8.2.0419: various memory leaks in Vim9 script code
Bram Moolenaar <Bram@vim.org>
parents: 19623
diff changeset
358 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
359
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
360 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
361 }
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
362 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
363
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
364 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
365
21218
1f4d0375f947 patch 8.2.1160: Vim9: memory leak in allocated types
Bram Moolenaar <Bram@vim.org>
parents: 21194
diff changeset
366 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
367 }
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
368
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
369 /*
23364
17a0e32eefd4 patch 8.2.2225: Vim9: error when using :import in legacy script twice
Bram Moolenaar <Bram@vim.org>
parents: 23362
diff changeset
370 * 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
371 * 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
372 */
17a0e32eefd4 patch 8.2.2225: Vim9: error when using :import in legacy script twice
Bram Moolenaar <Bram@vim.org>
parents: 23362
diff changeset
373 void
17a0e32eefd4 patch 8.2.2225: Vim9: error when using :import in legacy script twice
Bram Moolenaar <Bram@vim.org>
parents: 23362
diff changeset
374 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
375 {
17a0e32eefd4 patch 8.2.2225: Vim9: error when using :import in legacy script twice
Bram Moolenaar <Bram@vim.org>
parents: 23362
diff changeset
376 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
377 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
378
17a0e32eefd4 patch 8.2.2225: Vim9: error when using :import in legacy script twice
Bram Moolenaar <Bram@vim.org>
parents: 23362
diff changeset
379 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
380 {
17a0e32eefd4 patch 8.2.2225: Vim9: error when using :import in legacy script twice
Bram Moolenaar <Bram@vim.org>
parents: 23362
diff changeset
381 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
382
17a0e32eefd4 patch 8.2.2225: Vim9: error when using :import in legacy script twice
Bram Moolenaar <Bram@vim.org>
parents: 23362
diff changeset
383 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
384 }
17a0e32eefd4 patch 8.2.2225: Vim9: error when using :import in legacy script twice
Bram Moolenaar <Bram@vim.org>
parents: 23362
diff changeset
385 }
17a0e32eefd4 patch 8.2.2225: Vim9: error when using :import in legacy script twice
Bram Moolenaar <Bram@vim.org>
parents: 23362
diff changeset
386
17a0e32eefd4 patch 8.2.2225: Vim9: error when using :import in legacy script twice
Bram Moolenaar <Bram@vim.org>
parents: 23362
diff changeset
387 /*
28249
4b322951ebac patch 8.2.4650: "import autoload" only works with using 'runtimepath'
Bram Moolenaar <Bram@vim.org>
parents: 28231
diff changeset
388 * Part of "import" that handles a relative or absolute file name/
4b322951ebac patch 8.2.4650: "import autoload" only works with using 'runtimepath'
Bram Moolenaar <Bram@vim.org>
parents: 28231
diff changeset
389 * Returns OK or FAIL.
4b322951ebac patch 8.2.4650: "import autoload" only works with using 'runtimepath'
Bram Moolenaar <Bram@vim.org>
parents: 28231
diff changeset
390 */
4b322951ebac patch 8.2.4650: "import autoload" only works with using 'runtimepath'
Bram Moolenaar <Bram@vim.org>
parents: 28231
diff changeset
391 static int
4b322951ebac patch 8.2.4650: "import autoload" only works with using 'runtimepath'
Bram Moolenaar <Bram@vim.org>
parents: 28231
diff changeset
392 handle_import_fname(char_u *fname, int is_autoload, int *sid)
4b322951ebac patch 8.2.4650: "import autoload" only works with using 'runtimepath'
Bram Moolenaar <Bram@vim.org>
parents: 28231
diff changeset
393 {
4b322951ebac patch 8.2.4650: "import autoload" only works with using 'runtimepath'
Bram Moolenaar <Bram@vim.org>
parents: 28231
diff changeset
394 if (is_autoload)
4b322951ebac patch 8.2.4650: "import autoload" only works with using 'runtimepath'
Bram Moolenaar <Bram@vim.org>
parents: 28231
diff changeset
395 {
4b322951ebac patch 8.2.4650: "import autoload" only works with using 'runtimepath'
Bram Moolenaar <Bram@vim.org>
parents: 28231
diff changeset
396 scriptitem_T *si;
4b322951ebac patch 8.2.4650: "import autoload" only works with using 'runtimepath'
Bram Moolenaar <Bram@vim.org>
parents: 28231
diff changeset
397
4b322951ebac patch 8.2.4650: "import autoload" only works with using 'runtimepath'
Bram Moolenaar <Bram@vim.org>
parents: 28231
diff changeset
398 *sid = find_script_by_name(fname);
4b322951ebac patch 8.2.4650: "import autoload" only works with using 'runtimepath'
Bram Moolenaar <Bram@vim.org>
parents: 28231
diff changeset
399 if (*sid < 0)
4b322951ebac patch 8.2.4650: "import autoload" only works with using 'runtimepath'
Bram Moolenaar <Bram@vim.org>
parents: 28231
diff changeset
400 {
4b322951ebac patch 8.2.4650: "import autoload" only works with using 'runtimepath'
Bram Moolenaar <Bram@vim.org>
parents: 28231
diff changeset
401 int error = OK;
4b322951ebac patch 8.2.4650: "import autoload" only works with using 'runtimepath'
Bram Moolenaar <Bram@vim.org>
parents: 28231
diff changeset
402
28257
6d2b5c2178e2 patch 8.2.4654: missing changes for import check
Bram Moolenaar <Bram@vim.org>
parents: 28249
diff changeset
403 // Script does not exist yet, check name and create a new
6d2b5c2178e2 patch 8.2.4654: missing changes for import check
Bram Moolenaar <Bram@vim.org>
parents: 28249
diff changeset
404 // scriptitem.
6d2b5c2178e2 patch 8.2.4654: missing changes for import check
Bram Moolenaar <Bram@vim.org>
parents: 28249
diff changeset
405 if (!file_is_readable(fname))
6d2b5c2178e2 patch 8.2.4654: missing changes for import check
Bram Moolenaar <Bram@vim.org>
parents: 28249
diff changeset
406 {
6d2b5c2178e2 patch 8.2.4654: missing changes for import check
Bram Moolenaar <Bram@vim.org>
parents: 28249
diff changeset
407 semsg(_(mch_isdir(fname) ? e_str_is_directory
6d2b5c2178e2 patch 8.2.4654: missing changes for import check
Bram Moolenaar <Bram@vim.org>
parents: 28249
diff changeset
408 : e_cannot_read_from_str_2), fname);
6d2b5c2178e2 patch 8.2.4654: missing changes for import check
Bram Moolenaar <Bram@vim.org>
parents: 28249
diff changeset
409 return FAIL;
6d2b5c2178e2 patch 8.2.4654: missing changes for import check
Bram Moolenaar <Bram@vim.org>
parents: 28249
diff changeset
410 }
28249
4b322951ebac patch 8.2.4650: "import autoload" only works with using 'runtimepath'
Bram Moolenaar <Bram@vim.org>
parents: 28231
diff changeset
411 *sid = get_new_scriptitem_for_fname(&error, fname);
4b322951ebac patch 8.2.4650: "import autoload" only works with using 'runtimepath'
Bram Moolenaar <Bram@vim.org>
parents: 28231
diff changeset
412 if (error == FAIL)
4b322951ebac patch 8.2.4650: "import autoload" only works with using 'runtimepath'
Bram Moolenaar <Bram@vim.org>
parents: 28231
diff changeset
413 return FAIL;
4b322951ebac patch 8.2.4650: "import autoload" only works with using 'runtimepath'
Bram Moolenaar <Bram@vim.org>
parents: 28231
diff changeset
414 }
4b322951ebac patch 8.2.4650: "import autoload" only works with using 'runtimepath'
Bram Moolenaar <Bram@vim.org>
parents: 28231
diff changeset
415
4b322951ebac patch 8.2.4650: "import autoload" only works with using 'runtimepath'
Bram Moolenaar <Bram@vim.org>
parents: 28231
diff changeset
416 si = SCRIPT_ITEM(*sid);
4b322951ebac patch 8.2.4650: "import autoload" only works with using 'runtimepath'
Bram Moolenaar <Bram@vim.org>
parents: 28231
diff changeset
417 si->sn_import_autoload = TRUE;
4b322951ebac patch 8.2.4650: "import autoload" only works with using 'runtimepath'
Bram Moolenaar <Bram@vim.org>
parents: 28231
diff changeset
418
28261
20f6b0cdf38c patch 8.2.4656: Vim9: can't use item from "import autoload" with autoload dir
Bram Moolenaar <Bram@vim.org>
parents: 28257
diff changeset
419 if (si->sn_autoload_prefix == NULL)
20f6b0cdf38c patch 8.2.4656: Vim9: can't use item from "import autoload" with autoload dir
Bram Moolenaar <Bram@vim.org>
parents: 28257
diff changeset
420 si->sn_autoload_prefix = get_autoload_prefix(si);
20f6b0cdf38c patch 8.2.4656: Vim9: can't use item from "import autoload" with autoload dir
Bram Moolenaar <Bram@vim.org>
parents: 28257
diff changeset
421
28249
4b322951ebac patch 8.2.4650: "import autoload" only works with using 'runtimepath'
Bram Moolenaar <Bram@vim.org>
parents: 28231
diff changeset
422 // with testing override: load autoload script right away
4b322951ebac patch 8.2.4650: "import autoload" only works with using 'runtimepath'
Bram Moolenaar <Bram@vim.org>
parents: 28231
diff changeset
423 if (!override_autoload || si->sn_state != SN_STATE_NOT_LOADED)
4b322951ebac patch 8.2.4650: "import autoload" only works with using 'runtimepath'
Bram Moolenaar <Bram@vim.org>
parents: 28231
diff changeset
424 return OK;
4b322951ebac patch 8.2.4650: "import autoload" only works with using 'runtimepath'
Bram Moolenaar <Bram@vim.org>
parents: 28231
diff changeset
425 }
4b322951ebac patch 8.2.4650: "import autoload" only works with using 'runtimepath'
Bram Moolenaar <Bram@vim.org>
parents: 28231
diff changeset
426 return do_source(fname, FALSE, DOSO_NONE, sid);
4b322951ebac patch 8.2.4650: "import autoload" only works with using 'runtimepath'
Bram Moolenaar <Bram@vim.org>
parents: 28231
diff changeset
427 }
4b322951ebac patch 8.2.4650: "import autoload" only works with using 'runtimepath'
Bram Moolenaar <Bram@vim.org>
parents: 28231
diff changeset
428
4b322951ebac patch 8.2.4650: "import autoload" only works with using 'runtimepath'
Bram Moolenaar <Bram@vim.org>
parents: 28231
diff changeset
429 /*
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
430 * 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
431 * 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
432 * "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
433 * 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
434 */
25567
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
435 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
436 handle_import(
465d6e40e79c patch 8.2.1124: Vim9: no line break allowed in :import command
Bram Moolenaar <Bram@vim.org>
parents: 21120
diff changeset
437 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
438 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
439 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
440 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
441 void *cctx)
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
442 {
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
443 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
444 char_u *nextarg;
27043
15f40772e10a patch 8.2.4050: Vim9: need to prefix every item in an autoload script
Bram Moolenaar <Bram@vim.org>
parents: 27030
diff changeset
445 int is_autoload = FALSE;
26980
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
446 int getnext;
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
447 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
448 int ret = FAIL;
26980
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
449 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
450 typval_T tv;
27207
7b8e2bea0ec5 patch 8.2.4132: Vim9: wrong error message when autoload script can't be found
Bram Moolenaar <Bram@vim.org>
parents: 27116
diff changeset
451 int sid = -2;
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
452 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
453 long start_lnum = SOURCING_LNUM;
27030
c8809b8d19de patch 8.2.4044: Vim9: no error when importing the same script twice
Bram Moolenaar <Bram@vim.org>
parents: 27018
diff changeset
454 garray_T *import_gap;
c8809b8d19de patch 8.2.4044: Vim9: no error when importing the same script twice
Bram Moolenaar <Bram@vim.org>
parents: 27018
diff changeset
455 int i;
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
456
27043
15f40772e10a patch 8.2.4050: Vim9: need to prefix every item in an autoload script
Bram Moolenaar <Bram@vim.org>
parents: 27030
diff changeset
457 if (STRNCMP(arg, "autoload", 8) == 0 && VIM_ISWHITE(arg[8]))
15f40772e10a patch 8.2.4050: Vim9: need to prefix every item in an autoload script
Bram Moolenaar <Bram@vim.org>
parents: 27030
diff changeset
458 {
15f40772e10a patch 8.2.4050: Vim9: need to prefix every item in an autoload script
Bram Moolenaar <Bram@vim.org>
parents: 27030
diff changeset
459 is_autoload = TRUE;
15f40772e10a patch 8.2.4050: Vim9: need to prefix every item in an autoload script
Bram Moolenaar <Bram@vim.org>
parents: 27030
diff changeset
460 arg = skipwhite(arg + 8);
15f40772e10a patch 8.2.4050: Vim9: need to prefix every item in an autoload script
Bram Moolenaar <Bram@vim.org>
parents: 27030
diff changeset
461 }
15f40772e10a patch 8.2.4050: Vim9: need to prefix every item in an autoload script
Bram Moolenaar <Bram@vim.org>
parents: 27030
diff changeset
462
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
463 // 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
464 // string.
26980
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
465 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
466 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
467 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
468 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
469 || 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
470 {
26980
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
471 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
472 goto erret;
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
473 }
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
474
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
475 // 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
476 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
477
21146
465d6e40e79c patch 8.2.1124: Vim9: no line break allowed in :import command
Bram Moolenaar <Bram@vim.org>
parents: 21120
diff changeset
478 /*
465d6e40e79c patch 8.2.1124: Vim9: no line break allowed in :import command
Bram Moolenaar <Bram@vim.org>
parents: 21120
diff changeset
479 * 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
480 */
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
481 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
482 {
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
483 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
484 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
485 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
486 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
487
28249
4b322951ebac patch 8.2.4650: "import autoload" only works with using 'runtimepath'
Bram Moolenaar <Bram@vim.org>
parents: 28231
diff changeset
488 // Relative to current script: "./name.vim", "../../name.vim".
4b322951ebac patch 8.2.4650: "import autoload" only works with using 'runtimepath'
Bram Moolenaar <Bram@vim.org>
parents: 28231
diff changeset
489 len = STRLEN(si->sn_name) - STRLEN(tail) + STRLEN(tv.vval.v_string) + 2;
4b322951ebac patch 8.2.4650: "import autoload" only works with using 'runtimepath'
Bram Moolenaar <Bram@vim.org>
parents: 28231
diff changeset
490 from_name = alloc((int)len);
4b322951ebac patch 8.2.4650: "import autoload" only works with using 'runtimepath'
Bram Moolenaar <Bram@vim.org>
parents: 28231
diff changeset
491 if (from_name == NULL)
4b322951ebac patch 8.2.4650: "import autoload" only works with using 'runtimepath'
Bram Moolenaar <Bram@vim.org>
parents: 28231
diff changeset
492 goto erret;
4b322951ebac patch 8.2.4650: "import autoload" only works with using 'runtimepath'
Bram Moolenaar <Bram@vim.org>
parents: 28231
diff changeset
493 vim_strncpy(from_name, si->sn_name, tail - si->sn_name);
4b322951ebac patch 8.2.4650: "import autoload" only works with using 'runtimepath'
Bram Moolenaar <Bram@vim.org>
parents: 28231
diff changeset
494 add_pathsep(from_name);
4b322951ebac patch 8.2.4650: "import autoload" only works with using 'runtimepath'
Bram Moolenaar <Bram@vim.org>
parents: 28231
diff changeset
495 STRCAT(from_name, tv.vval.v_string);
4b322951ebac patch 8.2.4650: "import autoload" only works with using 'runtimepath'
Bram Moolenaar <Bram@vim.org>
parents: 28231
diff changeset
496 simplify_filename(from_name);
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
497
28249
4b322951ebac patch 8.2.4650: "import autoload" only works with using 'runtimepath'
Bram Moolenaar <Bram@vim.org>
parents: 28231
diff changeset
498 res = handle_import_fname(from_name, is_autoload, &sid);
4b322951ebac patch 8.2.4650: "import autoload" only works with using 'runtimepath'
Bram Moolenaar <Bram@vim.org>
parents: 28231
diff changeset
499 vim_free(from_name);
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
500 }
27211
61d6a31a5874 patch 8.2.4134: MS-Windows: test for import with absolute path fails
Bram Moolenaar <Bram@vim.org>
parents: 27207
diff changeset
501 else if (mch_isFullName(tv.vval.v_string)
61d6a31a5874 patch 8.2.4134: MS-Windows: test for import with absolute path fails
Bram Moolenaar <Bram@vim.org>
parents: 27207
diff changeset
502 #ifdef BACKSLASH_IN_FILENAME
61d6a31a5874 patch 8.2.4134: MS-Windows: test for import with absolute path fails
Bram Moolenaar <Bram@vim.org>
parents: 27207
diff changeset
503 // On MS-Windows omitting the drive is still handled like an
61d6a31a5874 patch 8.2.4134: MS-Windows: test for import with absolute path fails
Bram Moolenaar <Bram@vim.org>
parents: 27207
diff changeset
504 // absolute path, not using 'runtimepath'.
61d6a31a5874 patch 8.2.4134: MS-Windows: test for import with absolute path fails
Bram Moolenaar <Bram@vim.org>
parents: 27207
diff changeset
505 || *tv.vval.v_string == '/' || *tv.vval.v_string == '\\'
61d6a31a5874 patch 8.2.4134: MS-Windows: test for import with absolute path fails
Bram Moolenaar <Bram@vim.org>
parents: 27207
diff changeset
506 #endif
61d6a31a5874 patch 8.2.4134: MS-Windows: test for import with absolute path fails
Bram Moolenaar <Bram@vim.org>
parents: 27207
diff changeset
507 )
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
508 {
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
509 // Absolute path: "/tmp/name.vim"
28249
4b322951ebac patch 8.2.4650: "import autoload" only works with using 'runtimepath'
Bram Moolenaar <Bram@vim.org>
parents: 28231
diff changeset
510 res = handle_import_fname(tv.vval.v_string, is_autoload, &sid);
27043
15f40772e10a patch 8.2.4050: Vim9: need to prefix every item in an autoload script
Bram Moolenaar <Bram@vim.org>
parents: 27030
diff changeset
511 }
15f40772e10a patch 8.2.4050: Vim9: need to prefix every item in an autoload script
Bram Moolenaar <Bram@vim.org>
parents: 27030
diff changeset
512 else if (is_autoload)
15f40772e10a patch 8.2.4050: Vim9: need to prefix every item in an autoload script
Bram Moolenaar <Bram@vim.org>
parents: 27030
diff changeset
513 {
15f40772e10a patch 8.2.4050: Vim9: need to prefix every item in an autoload script
Bram Moolenaar <Bram@vim.org>
parents: 27030
diff changeset
514 size_t len = 9 + STRLEN(tv.vval.v_string) + 1;
15f40772e10a patch 8.2.4050: Vim9: need to prefix every item in an autoload script
Bram Moolenaar <Bram@vim.org>
parents: 27030
diff changeset
515 char_u *from_name;
15f40772e10a patch 8.2.4050: Vim9: need to prefix every item in an autoload script
Bram Moolenaar <Bram@vim.org>
parents: 27030
diff changeset
516
15f40772e10a patch 8.2.4050: Vim9: need to prefix every item in an autoload script
Bram Moolenaar <Bram@vim.org>
parents: 27030
diff changeset
517 // Find file in "autoload" subdirs in 'runtimepath'.
15f40772e10a patch 8.2.4050: Vim9: need to prefix every item in an autoload script
Bram Moolenaar <Bram@vim.org>
parents: 27030
diff changeset
518 from_name = alloc((int)len);
15f40772e10a patch 8.2.4050: Vim9: need to prefix every item in an autoload script
Bram Moolenaar <Bram@vim.org>
parents: 27030
diff changeset
519 if (from_name == NULL)
15f40772e10a patch 8.2.4050: Vim9: need to prefix every item in an autoload script
Bram Moolenaar <Bram@vim.org>
parents: 27030
diff changeset
520 goto erret;
15f40772e10a patch 8.2.4050: Vim9: need to prefix every item in an autoload script
Bram Moolenaar <Bram@vim.org>
parents: 27030
diff changeset
521 vim_snprintf((char *)from_name, len, "autoload/%s", tv.vval.v_string);
15f40772e10a patch 8.2.4050: Vim9: need to prefix every item in an autoload script
Bram Moolenaar <Bram@vim.org>
parents: 27030
diff changeset
522 // we need a scriptitem without loading the script
15f40772e10a patch 8.2.4050: Vim9: need to prefix every item in an autoload script
Bram Moolenaar <Bram@vim.org>
parents: 27030
diff changeset
523 sid = find_script_in_rtp(from_name);
15f40772e10a patch 8.2.4050: Vim9: need to prefix every item in an autoload script
Bram Moolenaar <Bram@vim.org>
parents: 27030
diff changeset
524 vim_free(from_name);
27086
1e2a6c6c7e42 patch 8.2.4072: Vim9: compiling function fails when autoload is not loaded
Bram Moolenaar <Bram@vim.org>
parents: 27074
diff changeset
525 if (SCRIPT_ID_VALID(sid))
1e2a6c6c7e42 patch 8.2.4072: Vim9: compiling function fails when autoload is not loaded
Bram Moolenaar <Bram@vim.org>
parents: 27074
diff changeset
526 {
1e2a6c6c7e42 patch 8.2.4072: Vim9: compiling function fails when autoload is not loaded
Bram Moolenaar <Bram@vim.org>
parents: 27074
diff changeset
527 scriptitem_T *si = SCRIPT_ITEM(sid);
1e2a6c6c7e42 patch 8.2.4072: Vim9: compiling function fails when autoload is not loaded
Bram Moolenaar <Bram@vim.org>
parents: 27074
diff changeset
528
1e2a6c6c7e42 patch 8.2.4072: Vim9: compiling function fails when autoload is not loaded
Bram Moolenaar <Bram@vim.org>
parents: 27074
diff changeset
529 if (si->sn_autoload_prefix == NULL)
1e2a6c6c7e42 patch 8.2.4072: Vim9: compiling function fails when autoload is not loaded
Bram Moolenaar <Bram@vim.org>
parents: 27074
diff changeset
530 si->sn_autoload_prefix = get_autoload_prefix(si);
1e2a6c6c7e42 patch 8.2.4072: Vim9: compiling function fails when autoload is not loaded
Bram Moolenaar <Bram@vim.org>
parents: 27074
diff changeset
531 res = OK;
27116
5b267700e6ab patch 8.2.4087: cannot test items from an autoload script easily
Bram Moolenaar <Bram@vim.org>
parents: 27114
diff changeset
532 if (override_autoload && si->sn_state == SN_STATE_NOT_LOADED)
5b267700e6ab patch 8.2.4087: cannot test items from an autoload script easily
Bram Moolenaar <Bram@vim.org>
parents: 27114
diff changeset
533 // testing override: load autoload script right away
5b267700e6ab patch 8.2.4087: cannot test items from an autoload script easily
Bram Moolenaar <Bram@vim.org>
parents: 27114
diff changeset
534 (void)do_source(si->sn_name, FALSE, DOSO_NONE, NULL);
27086
1e2a6c6c7e42 patch 8.2.4072: Vim9: compiling function fails when autoload is not loaded
Bram Moolenaar <Bram@vim.org>
parents: 27074
diff changeset
535 }
1e2a6c6c7e42 patch 8.2.4072: Vim9: compiling function fails when autoload is not loaded
Bram Moolenaar <Bram@vim.org>
parents: 27074
diff changeset
536 else
1e2a6c6c7e42 patch 8.2.4072: Vim9: compiling function fails when autoload is not loaded
Bram Moolenaar <Bram@vim.org>
parents: 27074
diff changeset
537 res = FAIL;
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
538 }
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
539 else
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
540 {
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
541 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
542 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
543
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
544 // 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
545 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
546 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
547 goto erret;
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
548 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
549 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
550 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
551 }
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
552
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
553 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
554 {
27207
7b8e2bea0ec5 patch 8.2.4132: Vim9: wrong error message when autoload script can't be found
Bram Moolenaar <Bram@vim.org>
parents: 27116
diff changeset
555 semsg(_(is_autoload && sid == -2
27043
15f40772e10a patch 8.2.4050: Vim9: need to prefix every item in an autoload script
Bram Moolenaar <Bram@vim.org>
parents: 27030
diff changeset
556 ? e_autoload_import_cannot_use_absolute_or_relative_path
15f40772e10a patch 8.2.4050: Vim9: need to prefix every item in an autoload script
Bram Moolenaar <Bram@vim.org>
parents: 27030
diff changeset
557 : 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
558 goto erret;
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
559 }
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
560
27607
db1831f585a4 patch 8.2.4330: Vim9: no error if script imports itself
Bram Moolenaar <Bram@vim.org>
parents: 27390
diff changeset
561 if (sid == current_sctx.sc_sid)
db1831f585a4 patch 8.2.4330: Vim9: no error if script imports itself
Bram Moolenaar <Bram@vim.org>
parents: 27390
diff changeset
562 {
db1831f585a4 patch 8.2.4330: Vim9: no error if script imports itself
Bram Moolenaar <Bram@vim.org>
parents: 27390
diff changeset
563 emsg(_(e_script_cannot_import_itself));
db1831f585a4 patch 8.2.4330: Vim9: no error if script imports itself
Bram Moolenaar <Bram@vim.org>
parents: 27390
diff changeset
564 goto erret;
db1831f585a4 patch 8.2.4330: Vim9: no error if script imports itself
Bram Moolenaar <Bram@vim.org>
parents: 27390
diff changeset
565 }
db1831f585a4 patch 8.2.4330: Vim9: no error if script imports itself
Bram Moolenaar <Bram@vim.org>
parents: 27390
diff changeset
566
27030
c8809b8d19de patch 8.2.4044: Vim9: no error when importing the same script twice
Bram Moolenaar <Bram@vim.org>
parents: 27018
diff changeset
567 import_gap = gap != NULL ? gap : &SCRIPT_ITEM(import_sid)->sn_imports;
c8809b8d19de patch 8.2.4044: Vim9: no error when importing the same script twice
Bram Moolenaar <Bram@vim.org>
parents: 27018
diff changeset
568 for (i = 0; i < import_gap->ga_len; ++i)
c8809b8d19de patch 8.2.4044: Vim9: no error when importing the same script twice
Bram Moolenaar <Bram@vim.org>
parents: 27018
diff changeset
569 {
c8809b8d19de patch 8.2.4044: Vim9: no error when importing the same script twice
Bram Moolenaar <Bram@vim.org>
parents: 27018
diff changeset
570 imported_T *import = (imported_T *)import_gap->ga_data + i;
c8809b8d19de patch 8.2.4044: Vim9: no error when importing the same script twice
Bram Moolenaar <Bram@vim.org>
parents: 27018
diff changeset
571
c8809b8d19de patch 8.2.4044: Vim9: no error when importing the same script twice
Bram Moolenaar <Bram@vim.org>
parents: 27018
diff changeset
572 if (import->imp_sid == sid)
c8809b8d19de patch 8.2.4044: Vim9: no error when importing the same script twice
Bram Moolenaar <Bram@vim.org>
parents: 27018
diff changeset
573 {
c8809b8d19de patch 8.2.4044: Vim9: no error when importing the same script twice
Bram Moolenaar <Bram@vim.org>
parents: 27018
diff changeset
574 if (import->imp_flags & IMP_FLAGS_RELOAD)
c8809b8d19de patch 8.2.4044: Vim9: no error when importing the same script twice
Bram Moolenaar <Bram@vim.org>
parents: 27018
diff changeset
575 {
27043
15f40772e10a patch 8.2.4050: Vim9: need to prefix every item in an autoload script
Bram Moolenaar <Bram@vim.org>
parents: 27030
diff changeset
576 // encountering same script first time on a reload is OK
27030
c8809b8d19de patch 8.2.4044: Vim9: no error when importing the same script twice
Bram Moolenaar <Bram@vim.org>
parents: 27018
diff changeset
577 import->imp_flags &= ~IMP_FLAGS_RELOAD;
c8809b8d19de patch 8.2.4044: Vim9: no error when importing the same script twice
Bram Moolenaar <Bram@vim.org>
parents: 27018
diff changeset
578 break;
c8809b8d19de patch 8.2.4044: Vim9: no error when importing the same script twice
Bram Moolenaar <Bram@vim.org>
parents: 27018
diff changeset
579 }
c8809b8d19de patch 8.2.4044: Vim9: no error when importing the same script twice
Bram Moolenaar <Bram@vim.org>
parents: 27018
diff changeset
580 semsg(_(e_cannot_import_same_script_twice_str), tv.vval.v_string);
c8809b8d19de patch 8.2.4044: Vim9: no error when importing the same script twice
Bram Moolenaar <Bram@vim.org>
parents: 27018
diff changeset
581 goto erret;
c8809b8d19de patch 8.2.4044: Vim9: no error when importing the same script twice
Bram Moolenaar <Bram@vim.org>
parents: 27018
diff changeset
582 }
c8809b8d19de patch 8.2.4044: Vim9: no error when importing the same script twice
Bram Moolenaar <Bram@vim.org>
parents: 27018
diff changeset
583 }
c8809b8d19de patch 8.2.4044: Vim9: no error when importing the same script twice
Bram Moolenaar <Bram@vim.org>
parents: 27018
diff changeset
584
26980
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
585 // 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
586 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
587 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
588 {
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
589 char_u *p;
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
590
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
591 if (getnext)
28690
9a7a2908e1a8 patch 8.2.4869: expression in command block does not look after NL
Bram Moolenaar <Bram@vim.org>
parents: 28596
diff changeset
592 arg = eval_next_line(expr_end, evalarg);
26980
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
593 else
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
594 arg = nextarg;
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
595
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
596 // 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
597 // 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
598 arg = skipwhite(arg + 2);
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
599 p = arg;
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
600 if (eval_isnamec1(*arg))
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
601 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
602 ++arg;
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
603 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
604 {
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
605 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
606 goto erret;
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
607 }
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
608 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
609 arg = skipwhite(arg);
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
610 }
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
611 else
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
612 {
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
613 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
614 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
615
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
616 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
617 {
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
618 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
619 goto erret;
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
620 }
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
621 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
622 {
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
623 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
624 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
625 }
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
626 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
627 {
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
628 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
629 goto erret;
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
630 }
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
631 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
632 }
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
633
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
634 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
635 {
24029
429b4f8d2fac patch 8.2.2556: Vim9: :import with "as" not fully supported
Bram Moolenaar <Bram@vim.org>
parents: 23980
diff changeset
636 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
637
27698
3813036f19cb patch 8.2.4375: ctx_imports is not used
Bram Moolenaar <Bram@vim.org>
parents: 27613
diff changeset
638 imported = find_imported(as_name, STRLEN(as_name), FALSE);
27030
c8809b8d19de patch 8.2.4044: Vim9: no error when importing the same script twice
Bram Moolenaar <Bram@vim.org>
parents: 27018
diff changeset
639 if (imported != NULL && imported->imp_sid != sid)
23364
17a0e32eefd4 patch 8.2.2225: Vim9: error when using :import in legacy script twice
Bram Moolenaar <Bram@vim.org>
parents: 23362
diff changeset
640 {
27030
c8809b8d19de patch 8.2.4044: Vim9: no error when importing the same script twice
Bram Moolenaar <Bram@vim.org>
parents: 27018
diff changeset
641 semsg(_(e_name_already_defined_str), as_name);
c8809b8d19de patch 8.2.4044: Vim9: no error when importing the same script twice
Bram Moolenaar <Bram@vim.org>
parents: 27018
diff changeset
642 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
643 }
27030
c8809b8d19de patch 8.2.4044: Vim9: no error when importing the same script twice
Bram Moolenaar <Bram@vim.org>
parents: 27018
diff changeset
644 else if (imported == NULL
27611
e311a80f8cbe patch 8.2.4332: Vim9: incomplete test for existing script variable in block
Bram Moolenaar <Bram@vim.org>
parents: 27607
diff changeset
645 && check_defined(as_name, STRLEN(as_name), cctx, NULL,
e311a80f8cbe patch 8.2.4332: Vim9: incomplete test for existing script variable in block
Bram Moolenaar <Bram@vim.org>
parents: 27607
diff changeset
646 FALSE) == FAIL)
26980
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
647 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
648
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
649 if (imported == NULL)
27074
19fefc42a063 patch 8.2.4066: Vim9: imported autoload script loaded again
Bram Moolenaar <Bram@vim.org>
parents: 27057
diff changeset
650 {
19fefc42a063 patch 8.2.4066: Vim9: imported autoload script loaded again
Bram Moolenaar <Bram@vim.org>
parents: 27057
diff changeset
651 imported = new_imported(import_gap);
19fefc42a063 patch 8.2.4066: Vim9: imported autoload script loaded again
Bram Moolenaar <Bram@vim.org>
parents: 27057
diff changeset
652 if (imported == NULL)
19fefc42a063 patch 8.2.4066: Vim9: imported autoload script loaded again
Bram Moolenaar <Bram@vim.org>
parents: 27057
diff changeset
653 goto erret;
19fefc42a063 patch 8.2.4066: Vim9: imported autoload script loaded again
Bram Moolenaar <Bram@vim.org>
parents: 27057
diff changeset
654 imported->imp_name = as_name;
19fefc42a063 patch 8.2.4066: Vim9: imported autoload script loaded again
Bram Moolenaar <Bram@vim.org>
parents: 27057
diff changeset
655 as_name = NULL;
19fefc42a063 patch 8.2.4066: Vim9: imported autoload script loaded again
Bram Moolenaar <Bram@vim.org>
parents: 27057
diff changeset
656 imported->imp_sid = sid;
19fefc42a063 patch 8.2.4066: Vim9: imported autoload script loaded again
Bram Moolenaar <Bram@vim.org>
parents: 27057
diff changeset
657 if (is_autoload)
19fefc42a063 patch 8.2.4066: Vim9: imported autoload script loaded again
Bram Moolenaar <Bram@vim.org>
parents: 27057
diff changeset
658 imported->imp_flags = IMP_FLAGS_AUTOLOAD;
19fefc42a063 patch 8.2.4066: Vim9: imported autoload script loaded again
Bram Moolenaar <Bram@vim.org>
parents: 27057
diff changeset
659 }
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
660 }
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
661
21146
465d6e40e79c patch 8.2.1124: Vim9: no line break allowed in :import command
Bram Moolenaar <Bram@vim.org>
parents: 21120
diff changeset
662 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
663 clear_tv(&tv);
26980
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
664 vim_free(as_name);
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
665 return arg;
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
666 }
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
667
20840
0600ab7b9f09 patch 8.2.0972: Vim9 script variable declarations need a type
Bram Moolenaar <Bram@vim.org>
parents: 20816
diff changeset
668 /*
26980
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
669 * ":import 'filename'"
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
670 * ":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
671 */
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
672 void
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
673 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
674 {
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
675 char_u *cmd_end;
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
676 evalarg_T evalarg;
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
677
28139
f34afadbef47 patch 8.2.4594: need to write script to a file to be able to source them
Bram Moolenaar <Bram@vim.org>
parents: 28002
diff changeset
678 if (!sourcing_a_script(eap))
25567
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
679 {
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
680 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
681 return;
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
682 }
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
683 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
684
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
685 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
686 &evalarg, NULL);
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
687 if (cmd_end != NULL)
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
688 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
689 clear_evalarg(&evalarg, eap);
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
690 }
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
691
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
692 /*
26980
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
693 * Find an exported item in "sid" matching "name".
27613
42d0279c6e7c patch 8.2.4333: cstack not always passed to where it is needed
Bram Moolenaar <Bram@vim.org>
parents: 27611
diff changeset
694 * Either "cctx" or "cstack" is NULL.
25567
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
695 * 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
696 * 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
697 * 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
698 */
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
699 int
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
700 find_exported(
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
701 int sid,
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
702 char_u *name,
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
703 ufunc_T **ufunc,
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
704 type_T **type,
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
705 cctx_T *cctx,
27613
42d0279c6e7c patch 8.2.4333: cstack not always passed to where it is needed
Bram Moolenaar <Bram@vim.org>
parents: 27611
diff changeset
706 cstack_T *cstack,
25567
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
707 int verbose)
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
708 {
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
709 int idx = -1;
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
710 svar_T *sv;
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
711 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
712
28596
daf785caecf2 patch 8.2.4822: setting ufunc to NULL twice
Bram Moolenaar <Bram@vim.org>
parents: 28594
diff changeset
713 *ufunc = NULL;
daf785caecf2 patch 8.2.4822: setting ufunc to NULL twice
Bram Moolenaar <Bram@vim.org>
parents: 28594
diff changeset
714
28249
4b322951ebac patch 8.2.4650: "import autoload" only works with using 'runtimepath'
Bram Moolenaar <Bram@vim.org>
parents: 28231
diff changeset
715 if (script->sn_import_autoload && script->sn_state == SN_STATE_NOT_LOADED)
4b322951ebac patch 8.2.4650: "import autoload" only works with using 'runtimepath'
Bram Moolenaar <Bram@vim.org>
parents: 28231
diff changeset
716 {
4b322951ebac patch 8.2.4650: "import autoload" only works with using 'runtimepath'
Bram Moolenaar <Bram@vim.org>
parents: 28231
diff changeset
717 if (do_source(script->sn_name, FALSE, DOSO_NONE, NULL) == FAIL)
28594
294771aab3ae patch 8.2.4821: crash when imported autoload script was deleted
Bram Moolenaar <Bram@vim.org>
parents: 28457
diff changeset
718 {
294771aab3ae patch 8.2.4821: crash when imported autoload script was deleted
Bram Moolenaar <Bram@vim.org>
parents: 28457
diff changeset
719 semsg(_(e_cant_open_file_str), script->sn_name);
28249
4b322951ebac patch 8.2.4650: "import autoload" only works with using 'runtimepath'
Bram Moolenaar <Bram@vim.org>
parents: 28231
diff changeset
720 return -1;
28594
294771aab3ae patch 8.2.4821: crash when imported autoload script was deleted
Bram Moolenaar <Bram@vim.org>
parents: 28457
diff changeset
721 }
28249
4b322951ebac patch 8.2.4650: "import autoload" only works with using 'runtimepath'
Bram Moolenaar <Bram@vim.org>
parents: 28231
diff changeset
722 }
4b322951ebac patch 8.2.4650: "import autoload" only works with using 'runtimepath'
Bram Moolenaar <Bram@vim.org>
parents: 28231
diff changeset
723
25567
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
724 // Find name in "script".
27613
42d0279c6e7c patch 8.2.4333: cstack not always passed to where it is needed
Bram Moolenaar <Bram@vim.org>
parents: 27611
diff changeset
725 idx = get_script_item_idx(sid, name, 0, cctx, cstack);
25567
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
726 if (idx >= 0)
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
727 {
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
728 sv = ((svar_T *)script->sn_var_vals.ga_data) + idx;
28345
fabe722b24e9 patch 8.2.4698: Vim9: script variable has no flag that it was set
Bram Moolenaar <Bram@vim.org>
parents: 28313
diff changeset
729 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0)
25567
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
730 {
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
731 if (verbose)
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
732 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
733 return -1;
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
734 }
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
735 *type = sv->sv_type;
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
736 }
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
737 else
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
738 {
27049
140102677c12 patch 8.2.4053: Vim9: autoload mechanism doesn't fully work yet
Bram Moolenaar <Bram@vim.org>
parents: 27043
diff changeset
739 size_t len = STRLEN(name);
25567
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
740 char_u buffer[200];
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
741 char_u *funcname;
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
742
27049
140102677c12 patch 8.2.4053: Vim9: autoload mechanism doesn't fully work yet
Bram Moolenaar <Bram@vim.org>
parents: 27043
diff changeset
743 // It could be a user function. Normally this is stored as
140102677c12 patch 8.2.4053: Vim9: autoload mechanism doesn't fully work yet
Bram Moolenaar <Bram@vim.org>
parents: 27043
diff changeset
744 // "<SNR>99_name". For an autoload script a function is stored with
140102677c12 patch 8.2.4053: Vim9: autoload mechanism doesn't fully work yet
Bram Moolenaar <Bram@vim.org>
parents: 27043
diff changeset
745 // the autoload prefix: "dir#script#name".
140102677c12 patch 8.2.4053: Vim9: autoload mechanism doesn't fully work yet
Bram Moolenaar <Bram@vim.org>
parents: 27043
diff changeset
746 if (script->sn_autoload_prefix != NULL)
140102677c12 patch 8.2.4053: Vim9: autoload mechanism doesn't fully work yet
Bram Moolenaar <Bram@vim.org>
parents: 27043
diff changeset
747 len += STRLEN(script->sn_autoload_prefix) + 2;
140102677c12 patch 8.2.4053: Vim9: autoload mechanism doesn't fully work yet
Bram Moolenaar <Bram@vim.org>
parents: 27043
diff changeset
748 else
140102677c12 patch 8.2.4053: Vim9: autoload mechanism doesn't fully work yet
Bram Moolenaar <Bram@vim.org>
parents: 27043
diff changeset
749 len += 15;
140102677c12 patch 8.2.4053: Vim9: autoload mechanism doesn't fully work yet
Bram Moolenaar <Bram@vim.org>
parents: 27043
diff changeset
750
140102677c12 patch 8.2.4053: Vim9: autoload mechanism doesn't fully work yet
Bram Moolenaar <Bram@vim.org>
parents: 27043
diff changeset
751 if (len < sizeof(buffer))
25567
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
752 funcname = buffer;
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
753 else
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
754 {
27049
140102677c12 patch 8.2.4053: Vim9: autoload mechanism doesn't fully work yet
Bram Moolenaar <Bram@vim.org>
parents: 27043
diff changeset
755 funcname = alloc(len);
25567
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
756 if (funcname == NULL)
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
757 return -1;
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
758 }
27049
140102677c12 patch 8.2.4053: Vim9: autoload mechanism doesn't fully work yet
Bram Moolenaar <Bram@vim.org>
parents: 27043
diff changeset
759 if (script->sn_autoload_prefix != NULL)
140102677c12 patch 8.2.4053: Vim9: autoload mechanism doesn't fully work yet
Bram Moolenaar <Bram@vim.org>
parents: 27043
diff changeset
760 {
140102677c12 patch 8.2.4053: Vim9: autoload mechanism doesn't fully work yet
Bram Moolenaar <Bram@vim.org>
parents: 27043
diff changeset
761 sprintf((char *)funcname, "%s%s", script->sn_autoload_prefix, name);
140102677c12 patch 8.2.4053: Vim9: autoload mechanism doesn't fully work yet
Bram Moolenaar <Bram@vim.org>
parents: 27043
diff changeset
762 }
140102677c12 patch 8.2.4053: Vim9: autoload mechanism doesn't fully work yet
Bram Moolenaar <Bram@vim.org>
parents: 27043
diff changeset
763 else
140102677c12 patch 8.2.4053: Vim9: autoload mechanism doesn't fully work yet
Bram Moolenaar <Bram@vim.org>
parents: 27043
diff changeset
764 {
140102677c12 patch 8.2.4053: Vim9: autoload mechanism doesn't fully work yet
Bram Moolenaar <Bram@vim.org>
parents: 27043
diff changeset
765 funcname[0] = K_SPECIAL;
140102677c12 patch 8.2.4053: Vim9: autoload mechanism doesn't fully work yet
Bram Moolenaar <Bram@vim.org>
parents: 27043
diff changeset
766 funcname[1] = KS_EXTRA;
140102677c12 patch 8.2.4053: Vim9: autoload mechanism doesn't fully work yet
Bram Moolenaar <Bram@vim.org>
parents: 27043
diff changeset
767 funcname[2] = (int)KE_SNR;
140102677c12 patch 8.2.4053: Vim9: autoload mechanism doesn't fully work yet
Bram Moolenaar <Bram@vim.org>
parents: 27043
diff changeset
768 sprintf((char *)funcname + 3, "%ld_%s", (long)sid, name);
140102677c12 patch 8.2.4053: Vim9: autoload mechanism doesn't fully work yet
Bram Moolenaar <Bram@vim.org>
parents: 27043
diff changeset
769 }
27114
98a01021e465 patch 8.2.4086: "cctx" argument of find_func_even_dead() is unused
Bram Moolenaar <Bram@vim.org>
parents: 27094
diff changeset
770 *ufunc = find_func(funcname, FALSE);
25567
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
771
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
772 if (*ufunc == NULL)
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
773 {
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
774 if (verbose)
27217
facb54d20a50 patch 8.2.4137: Vim9: calling import with and without method is inconsistent
Bram Moolenaar <Bram@vim.org>
parents: 27215
diff changeset
775 {
facb54d20a50 patch 8.2.4137: Vim9: calling import with and without method is inconsistent
Bram Moolenaar <Bram@vim.org>
parents: 27215
diff changeset
776 ufunc_T *alt_ufunc = NULL;
facb54d20a50 patch 8.2.4137: Vim9: calling import with and without method is inconsistent
Bram Moolenaar <Bram@vim.org>
parents: 27215
diff changeset
777
facb54d20a50 patch 8.2.4137: Vim9: calling import with and without method is inconsistent
Bram Moolenaar <Bram@vim.org>
parents: 27215
diff changeset
778 if (script->sn_autoload_prefix != NULL)
facb54d20a50 patch 8.2.4137: Vim9: calling import with and without method is inconsistent
Bram Moolenaar <Bram@vim.org>
parents: 27215
diff changeset
779 {
facb54d20a50 patch 8.2.4137: Vim9: calling import with and without method is inconsistent
Bram Moolenaar <Bram@vim.org>
parents: 27215
diff changeset
780 // try find the function by the script-local name
facb54d20a50 patch 8.2.4137: Vim9: calling import with and without method is inconsistent
Bram Moolenaar <Bram@vim.org>
parents: 27215
diff changeset
781 funcname[0] = K_SPECIAL;
facb54d20a50 patch 8.2.4137: Vim9: calling import with and without method is inconsistent
Bram Moolenaar <Bram@vim.org>
parents: 27215
diff changeset
782 funcname[1] = KS_EXTRA;
facb54d20a50 patch 8.2.4137: Vim9: calling import with and without method is inconsistent
Bram Moolenaar <Bram@vim.org>
parents: 27215
diff changeset
783 funcname[2] = (int)KE_SNR;
facb54d20a50 patch 8.2.4137: Vim9: calling import with and without method is inconsistent
Bram Moolenaar <Bram@vim.org>
parents: 27215
diff changeset
784 sprintf((char *)funcname + 3, "%ld_%s", (long)sid, name);
facb54d20a50 patch 8.2.4137: Vim9: calling import with and without method is inconsistent
Bram Moolenaar <Bram@vim.org>
parents: 27215
diff changeset
785 alt_ufunc = find_func(funcname, FALSE);
facb54d20a50 patch 8.2.4137: Vim9: calling import with and without method is inconsistent
Bram Moolenaar <Bram@vim.org>
parents: 27215
diff changeset
786 }
facb54d20a50 patch 8.2.4137: Vim9: calling import with and without method is inconsistent
Bram Moolenaar <Bram@vim.org>
parents: 27215
diff changeset
787 if (alt_ufunc != NULL)
facb54d20a50 patch 8.2.4137: Vim9: calling import with and without method is inconsistent
Bram Moolenaar <Bram@vim.org>
parents: 27215
diff changeset
788 semsg(_(e_item_not_exported_in_script_str), name);
facb54d20a50 patch 8.2.4137: Vim9: calling import with and without method is inconsistent
Bram Moolenaar <Bram@vim.org>
parents: 27215
diff changeset
789 else
facb54d20a50 patch 8.2.4137: Vim9: calling import with and without method is inconsistent
Bram Moolenaar <Bram@vim.org>
parents: 27215
diff changeset
790 semsg(_(e_item_not_found_in_script_str), name);
facb54d20a50 patch 8.2.4137: Vim9: calling import with and without method is inconsistent
Bram Moolenaar <Bram@vim.org>
parents: 27215
diff changeset
791 }
25567
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
792 }
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
793 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
794 {
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
795 if (verbose)
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
796 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
797 *ufunc = NULL;
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
798 }
27217
facb54d20a50 patch 8.2.4137: Vim9: calling import with and without method is inconsistent
Bram Moolenaar <Bram@vim.org>
parents: 27215
diff changeset
799 if (funcname != buffer)
facb54d20a50 patch 8.2.4137: Vim9: calling import with and without method is inconsistent
Bram Moolenaar <Bram@vim.org>
parents: 27215
diff changeset
800 vim_free(funcname);
25567
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
801 }
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
802
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
803 return idx;
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
804 }
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
805
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
806 /*
29006
99a7eb1881c7 patch 8.2.5025: Vim9: a few lines not covered by tests
Bram Moolenaar <Bram@vim.org>
parents: 28690
diff changeset
807 * Declare a script-local variable without init: "var name: type".
20840
0600ab7b9f09 patch 8.2.0972: Vim9 script variable declarations need a type
Bram Moolenaar <Bram@vim.org>
parents: 20816
diff changeset
808 * "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
809 * 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
810 */
0600ab7b9f09 patch 8.2.0972: Vim9 script variable declarations need a type
Bram Moolenaar <Bram@vim.org>
parents: 20816
diff changeset
811 char_u *
0600ab7b9f09 patch 8.2.0972: Vim9 script variable declarations need a type
Bram Moolenaar <Bram@vim.org>
parents: 20816
diff changeset
812 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
813 {
0600ab7b9f09 patch 8.2.0972: Vim9 script variable declarations need a type
Bram Moolenaar <Bram@vim.org>
parents: 20816
diff changeset
814 char_u *p;
0600ab7b9f09 patch 8.2.0972: Vim9 script variable declarations need a type
Bram Moolenaar <Bram@vim.org>
parents: 20816
diff changeset
815 char_u *name;
0600ab7b9f09 patch 8.2.0972: Vim9 script variable declarations need a type
Bram Moolenaar <Bram@vim.org>
parents: 20816
diff changeset
816 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
817 type_T *type;
0600ab7b9f09 patch 8.2.0972: Vim9 script variable declarations need a type
Bram Moolenaar <Bram@vim.org>
parents: 20816
diff changeset
818 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
819
22391
a9fb7efa31d6 patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents: 22250
diff changeset
820 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
821 {
22391
a9fb7efa31d6 patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents: 22250
diff changeset
822 if (eap->cmdidx == CMD_final)
a9fb7efa31d6 patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents: 22250
diff changeset
823 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
824 else
a9fb7efa31d6 patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents: 22250
diff changeset
825 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
826 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
827 }
0600ab7b9f09 patch 8.2.0972: Vim9 script variable declarations need a type
Bram Moolenaar <Bram@vim.org>
parents: 20816
diff changeset
828
0600ab7b9f09 patch 8.2.0972: Vim9 script variable declarations need a type
Bram Moolenaar <Bram@vim.org>
parents: 20816
diff changeset
829 // 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
830 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
831 {
26865
bce848ec8b1b patch 8.2.3961: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 26662
diff changeset
832 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
833 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
834 }
0600ab7b9f09 patch 8.2.0972: Vim9 script variable declarations need a type
Bram Moolenaar <Bram@vim.org>
parents: 20816
diff changeset
835
20846
709379ab5110 patch 8.2.0975: Vim9: script variable does not accept optional s: prefix
Bram Moolenaar <Bram@vim.org>
parents: 20844
diff changeset
836 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
837 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
838 break;
20840
0600ab7b9f09 patch 8.2.0972: Vim9 script variable declarations need a type
Bram Moolenaar <Bram@vim.org>
parents: 20816
diff changeset
839
0600ab7b9f09 patch 8.2.0972: Vim9 script variable declarations need a type
Bram Moolenaar <Bram@vim.org>
parents: 20816
diff changeset
840 if (*p != ':')
0600ab7b9f09 patch 8.2.0972: Vim9 script variable declarations need a type
Bram Moolenaar <Bram@vim.org>
parents: 20816
diff changeset
841 {
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
842 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
843 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
844 }
20846
709379ab5110 patch 8.2.0975: Vim9: script variable does not accept optional s: prefix
Bram Moolenaar <Bram@vim.org>
parents: 20844
diff changeset
845 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
846 {
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
847 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
848 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
849 }
20840
0600ab7b9f09 patch 8.2.0972: Vim9 script variable declarations need a type
Bram Moolenaar <Bram@vim.org>
parents: 20816
diff changeset
850 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
851
24717
bf8feac8a89a patch 8.2.2897: Vim9: can use reserved words at the script level
Bram Moolenaar <Bram@vim.org>
parents: 24533
diff changeset
852 // 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
853 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
854 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
855 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
856 {
1360541e8c74 patch 8.2.0974: Vim9: memory leak when script var has wrong type
Bram Moolenaar <Bram@vim.org>
parents: 20842
diff changeset
857 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
858 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
859 }
20840
0600ab7b9f09 patch 8.2.0972: Vim9 script variable declarations need a type
Bram Moolenaar <Bram@vim.org>
parents: 20816
diff changeset
860
0600ab7b9f09 patch 8.2.0972: Vim9 script variable declarations need a type
Bram Moolenaar <Bram@vim.org>
parents: 20816
diff changeset
861 // 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
862 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
863 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
864 // 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
865 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
866 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
867 init_tv.v_type = type->tt_type;
28231
66b245d84f37 patch 8.2.4642: Vim9: in :def function script var cannot be null
Bram Moolenaar <Bram@vim.org>
parents: 28164
diff changeset
868 set_var_const(name, 0, type, &init_tv, FALSE, ASSIGN_INIT, 0);
20840
0600ab7b9f09 patch 8.2.0972: Vim9 script variable declarations need a type
Bram Moolenaar <Bram@vim.org>
parents: 20816
diff changeset
869
0600ab7b9f09 patch 8.2.0972: Vim9 script variable declarations need a type
Bram Moolenaar <Bram@vim.org>
parents: 20816
diff changeset
870 vim_free(name);
0600ab7b9f09 patch 8.2.0972: Vim9 script variable declarations need a type
Bram Moolenaar <Bram@vim.org>
parents: 20816
diff changeset
871 return p;
0600ab7b9f09 patch 8.2.0972: Vim9 script variable declarations need a type
Bram Moolenaar <Bram@vim.org>
parents: 20816
diff changeset
872 }
0600ab7b9f09 patch 8.2.0972: Vim9 script variable declarations need a type
Bram Moolenaar <Bram@vim.org>
parents: 20816
diff changeset
873
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 /*
22643
71b57779177d patch 8.2.1870: Vim9: no need to keep all script variables
Bram Moolenaar <Bram@vim.org>
parents: 22596
diff changeset
875 * 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
876 * 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
877 * 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
878 * existing variable.
28345
fabe722b24e9 patch 8.2.4698: Vim9: script variable has no flag that it was set
Bram Moolenaar <Bram@vim.org>
parents: 28313
diff changeset
879 * "flags" can have ASSIGN_FINAL, ASSIGN_CONST or ASSIGN_INIT.
24438
5c6ccab68d1e patch 8.2.2759: Vim9: for loop infers type of loop variable
Bram Moolenaar <Bram@vim.org>
parents: 24285
diff changeset
880 * 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
881 * "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
882 */
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
883 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
884 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
885 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
886 dictitem_T *di,
27049
140102677c12 patch 8.2.4053: Vim9: autoload mechanism doesn't fully work yet
Bram Moolenaar <Bram@vim.org>
parents: 27043
diff changeset
887 char_u *name,
23578
85ce241ff9e3 patch 8.2.2331: Vim9: wrong error when modifying dict declared with :final
Bram Moolenaar <Bram@vim.org>
parents: 23458
diff changeset
888 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
889 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
890 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
891 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
892 {
23362
f181fe2150ab patch 8.2.2224: Vim9: crash if script reloaded with different variable type
Bram Moolenaar <Bram@vim.org>
parents: 23358
diff changeset
893 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
894 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
895 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
896
23362
f181fe2150ab patch 8.2.2224: Vim9: crash if script reloaded with different variable type
Bram Moolenaar <Bram@vim.org>
parents: 23358
diff changeset
897 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
898 {
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
899 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
900 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
901
f181fe2150ab patch 8.2.2224: Vim9: crash if script reloaded with different variable type
Bram Moolenaar <Bram@vim.org>
parents: 23358
diff changeset
902 // 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
903 // 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
904 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
905 return;
f181fe2150ab patch 8.2.2224: Vim9: crash if script reloaded with different variable type
Bram Moolenaar <Bram@vim.org>
parents: 23358
diff changeset
906
27049
140102677c12 patch 8.2.4053: Vim9: autoload mechanism doesn't fully work yet
Bram Moolenaar <Bram@vim.org>
parents: 27043
diff changeset
907 hi = hash_find(&si->sn_all_vars.dv_hashtab, name);
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
908 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
909 {
25358
f03271631eb5 patch 8.2.3216: Vim9: crash when using variable in a loop at script level
Bram Moolenaar <Bram@vim.org>
parents: 25320
diff changeset
910 // 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
911 // 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
912 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
913 {
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
914 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
915 {
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
916 // 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
917 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
918 + 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
919 // 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
920 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
921 {
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
922 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
923 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
924 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
925 }
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
926 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
927 }
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
928 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
929 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
930 }
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
931 }
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
932
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
933 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
934 {
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
935 // 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
936 // 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
937 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
938 newsav = (sallvar_T *)alloc_clear(
27049
140102677c12 patch 8.2.4053: Vim9: autoload mechanism doesn't fully work yet
Bram Moolenaar <Bram@vim.org>
parents: 27043
diff changeset
939 sizeof(sallvar_T) + STRLEN(name));
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
940 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
941 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
942
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
943 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
944 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
945 : (flags & ASSIGN_CONST) ? ASSIGN_CONST : 0;
28345
fabe722b24e9 patch 8.2.4698: Vim9: script variable has no flag that it was set
Bram Moolenaar <Bram@vim.org>
parents: 28313
diff changeset
946 sv->sv_flags = is_export ? SVFLAG_EXPORTED : 0;
fabe722b24e9 patch 8.2.4698: Vim9: script variable has no flag that it was set
Bram Moolenaar <Bram@vim.org>
parents: 28313
diff changeset
947 if ((flags & ASSIGN_INIT) == 0)
fabe722b24e9 patch 8.2.4698: Vim9: script variable has no flag that it was set
Bram Moolenaar <Bram@vim.org>
parents: 28313
diff changeset
948 sv->sv_flags |= SVFLAG_ASSIGNED;
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
949 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
950 ++si->sn_var_vals.ga_len;
27049
140102677c12 patch 8.2.4053: Vim9: autoload mechanism doesn't fully work yet
Bram Moolenaar <Bram@vim.org>
parents: 27043
diff changeset
951 STRCPY(&newsav->sav_key, name);
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
952 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
953 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
954 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
955
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
956 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
957 // 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
958 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
959 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
960 // 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
961 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
962 }
23362
f181fe2150ab patch 8.2.2224: Vim9: crash if script reloaded with different variable type
Bram Moolenaar <Bram@vim.org>
parents: 23358
diff changeset
963 }
f181fe2150ab patch 8.2.2224: Vim9: crash if script reloaded with different variable type
Bram Moolenaar <Bram@vim.org>
parents: 23358
diff changeset
964 else
f181fe2150ab patch 8.2.2224: Vim9: crash if script reloaded with different variable type
Bram Moolenaar <Bram@vim.org>
parents: 23358
diff changeset
965 {
28313
b418e073b42f patch 8.2.4682: Vim9: can use :unlockvar for const variable
Bram Moolenaar <Bram@vim.org>
parents: 28261
diff changeset
966 sv = find_typval_in_script(&di->di_tv, 0, TRUE);
23362
f181fe2150ab patch 8.2.2224: Vim9: crash if script reloaded with different variable type
Bram Moolenaar <Bram@vim.org>
parents: 23358
diff changeset
967 }
f181fe2150ab patch 8.2.2224: Vim9: crash if script reloaded with different variable type
Bram Moolenaar <Bram@vim.org>
parents: 23358
diff changeset
968 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
969 {
23458
d2b1269c2c68 patch 8.2.2272: Vim9: extend() can violate the type of a variable
Bram Moolenaar <Bram@vim.org>
parents: 23392
diff changeset
970 if (*type == NULL)
26925
4e77f9961650 patch 8.2.3991: Vim9: error when extending dict<any>
Bram Moolenaar <Bram@vim.org>
parents: 26877
diff changeset
971 *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
972 do_member ? TVTT_DO_MEMBER : 0);
28231
66b245d84f37 patch 8.2.4642: Vim9: in :def function script var cannot be null
Bram Moolenaar <Bram@vim.org>
parents: 28164
diff changeset
973 else if ((flags & ASSIGN_INIT) == 0
66b245d84f37 patch 8.2.4642: Vim9: in :def function script var cannot be null
Bram Moolenaar <Bram@vim.org>
parents: 28164
diff changeset
974 && (*type)->tt_type == VAR_BLOB && tv->v_type == VAR_BLOB
66b245d84f37 patch 8.2.4642: Vim9: in :def function script var cannot be null
Bram Moolenaar <Bram@vim.org>
parents: 28164
diff changeset
975 && tv->vval.v_blob == NULL)
66b245d84f37 patch 8.2.4642: Vim9: in :def function script var cannot be null
Bram Moolenaar <Bram@vim.org>
parents: 28164
diff changeset
976 {
66b245d84f37 patch 8.2.4642: Vim9: in :def function script var cannot be null
Bram Moolenaar <Bram@vim.org>
parents: 28164
diff changeset
977 // "var b: blob = null_blob" has a different type.
66b245d84f37 patch 8.2.4642: Vim9: in :def function script var cannot be null
Bram Moolenaar <Bram@vim.org>
parents: 28164
diff changeset
978 *type = &t_blob_null;
66b245d84f37 patch 8.2.4642: Vim9: in :def function script var cannot be null
Bram Moolenaar <Bram@vim.org>
parents: 28164
diff changeset
979 }
28345
fabe722b24e9 patch 8.2.4698: Vim9: script variable has no flag that it was set
Bram Moolenaar <Bram@vim.org>
parents: 28313
diff changeset
980 if (sv->sv_flags & SVFLAG_TYPE_ALLOCATED)
26482
b115b552071f patch 8.2.3771: Vim9: accessing freed memory when checking type
Bram Moolenaar <Bram@vim.org>
parents: 26302
diff changeset
981 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
982 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
983 || (*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
984 {
b115b552071f patch 8.2.3771: Vim9: accessing freed memory when checking type
Bram Moolenaar <Bram@vim.org>
parents: 26302
diff changeset
985 // 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
986 // 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
987 // 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
988 sv->sv_type = alloc_type(*type);
28345
fabe722b24e9 patch 8.2.4698: Vim9: script variable has no flag that it was set
Bram Moolenaar <Bram@vim.org>
parents: 28313
diff changeset
989 sv->sv_flags |= SVFLAG_TYPE_ALLOCATED;
26482
b115b552071f patch 8.2.3771: Vim9: accessing freed memory when checking type
Bram Moolenaar <Bram@vim.org>
parents: 26302
diff changeset
990 }
b115b552071f patch 8.2.3771: Vim9: accessing freed memory when checking type
Bram Moolenaar <Bram@vim.org>
parents: 26302
diff changeset
991 else
b115b552071f patch 8.2.3771: Vim9: accessing freed memory when checking type
Bram Moolenaar <Bram@vim.org>
parents: 26302
diff changeset
992 {
b115b552071f patch 8.2.3771: Vim9: accessing freed memory when checking type
Bram Moolenaar <Bram@vim.org>
parents: 26302
diff changeset
993 sv->sv_type = *type;
28345
fabe722b24e9 patch 8.2.4698: Vim9: script variable has no flag that it was set
Bram Moolenaar <Bram@vim.org>
parents: 28313
diff changeset
994 sv->sv_flags &= ~SVFLAG_TYPE_ALLOCATED;
26482
b115b552071f patch 8.2.3771: Vim9: accessing freed memory when checking type
Bram Moolenaar <Bram@vim.org>
parents: 26302
diff changeset
995 }
23362
f181fe2150ab patch 8.2.2224: Vim9: crash if script reloaded with different variable type
Bram Moolenaar <Bram@vim.org>
parents: 23358
diff changeset
996 }
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
997
23362
f181fe2150ab patch 8.2.2224: Vim9: crash if script reloaded with different variable type
Bram Moolenaar <Bram@vim.org>
parents: 23358
diff changeset
998 // 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
999 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
1000 }
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
1001
22596
107eae953b87 patch 8.2.1846: Vim9: block variables are not found in compiled function
Bram Moolenaar <Bram@vim.org>
parents: 22594
diff changeset
1002 /*
107eae953b87 patch 8.2.1846: Vim9: block variables are not found in compiled function
Bram Moolenaar <Bram@vim.org>
parents: 22594
diff changeset
1003 * 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
1004 * "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
1005 * 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
1006 * 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
1007 */
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
1008 void
22643
71b57779177d patch 8.2.1870: Vim9: no need to keep all script variables
Bram Moolenaar <Bram@vim.org>
parents: 22596
diff changeset
1009 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
1010 {
22596
107eae953b87 patch 8.2.1846: Vim9: block variables are not found in compiled function
Bram Moolenaar <Bram@vim.org>
parents: 22594
diff changeset
1011 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
1012 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
1013 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
1014 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
1015 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
1016
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
1017 // 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
1018 // 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
1019 // 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
1020 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
1021 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
1022 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
1023 {
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
1024 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
1025 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
1026 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
1027
22596
107eae953b87 patch 8.2.1846: Vim9: block variables are not found in compiled function
Bram Moolenaar <Bram@vim.org>
parents: 22594
diff changeset
1028 // 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
1029 // 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
1030 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
1031 {
71b57779177d patch 8.2.1870: Vim9: no need to keep all script variables
Bram Moolenaar <Bram@vim.org>
parents: 22596
diff changeset
1032 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
1033 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
1034 }
22596
107eae953b87 patch 8.2.1846: Vim9: block variables are not found in compiled function
Bram Moolenaar <Bram@vim.org>
parents: 22594
diff changeset
1035 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
1036 {
22643
71b57779177d patch 8.2.1870: Vim9: no need to keep all script variables
Bram Moolenaar <Bram@vim.org>
parents: 22596
diff changeset
1037 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
1038 {
71b57779177d patch 8.2.1870: Vim9: no need to keep all script variables
Bram Moolenaar <Bram@vim.org>
parents: 22596
diff changeset
1039 // 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
1040 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
1041 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
1042 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
1043 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
1044 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
1045 }
71b57779177d patch 8.2.1870: Vim9: no need to keep all script variables
Bram Moolenaar <Bram@vim.org>
parents: 22596
diff changeset
1046 else
71b57779177d patch 8.2.1870: Vim9: no need to keep all script variables
Bram Moolenaar <Bram@vim.org>
parents: 22596
diff changeset
1047 {
71b57779177d patch 8.2.1870: Vim9: no need to keep all script variables
Bram Moolenaar <Bram@vim.org>
parents: 22596
diff changeset
1048 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
1049 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
1050 else
71b57779177d patch 8.2.1870: Vim9: no need to keep all script variables
Bram Moolenaar <Bram@vim.org>
parents: 22596
diff changeset
1051 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
1052 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
1053 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
1054 }
22596
107eae953b87 patch 8.2.1846: Vim9: block variables are not found in compiled function
Bram Moolenaar <Bram@vim.org>
parents: 22594
diff changeset
1055 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
1056 }
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
1057 }
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
1058 }
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
1059
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
1060 /*
22529
35ef9b0a81a3 patch 8.2.1813: Vim9: can assign wrong type to script dict
Bram Moolenaar <Bram@vim.org>
parents: 22391
diff changeset
1061 * 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
1062 * If "sid" is zero use the current script.
28313
b418e073b42f patch 8.2.4682: Vim9: can use :unlockvar for const variable
Bram Moolenaar <Bram@vim.org>
parents: 28261
diff changeset
1063 * if "must_find" is TRUE and "dest" cannot be found report an internal error.
25186
0a3b1c66d3f2 patch 8.2.3129: Vim9: imported uninitialized list does not get type checked
Bram Moolenaar <Bram@vim.org>
parents: 25184
diff changeset
1064 * 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
1065 */
22529
35ef9b0a81a3 patch 8.2.1813: Vim9: can assign wrong type to script dict
Bram Moolenaar <Bram@vim.org>
parents: 22391
diff changeset
1066 svar_T *
28313
b418e073b42f patch 8.2.4682: Vim9: can use :unlockvar for const variable
Bram Moolenaar <Bram@vim.org>
parents: 28261
diff changeset
1067 find_typval_in_script(typval_T *dest, scid_T sid, int must_find)
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
1068 {
26980
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
1069 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
1070 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
1071
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
1072 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
1073 // 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
1074 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
1075
25955
4003fc2340dc patch 8.2.3511: Vim9: entry for loop variable is created every round
Bram Moolenaar <Bram@vim.org>
parents: 25755
diff changeset
1076 // 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
1077 // 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
1078 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
1079 {
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
1080 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
1081
23094
8b93aea8d9d7 patch 8.2.2093: Vim9: script test sometimes fails
Bram Moolenaar <Bram@vim.org>
parents: 22740
diff changeset
1082 // 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
1083 // 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
1084 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
1085 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
1086 }
28313
b418e073b42f patch 8.2.4682: Vim9: can use :unlockvar for const variable
Bram Moolenaar <Bram@vim.org>
parents: 28261
diff changeset
1087 if (must_find)
b418e073b42f patch 8.2.4682: Vim9: can use :unlockvar for const variable
Bram Moolenaar <Bram@vim.org>
parents: 28261
diff changeset
1088 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
1089 return NULL;
35ef9b0a81a3 patch 8.2.1813: Vim9: can assign wrong type to script dict
Bram Moolenaar <Bram@vim.org>
parents: 22391
diff changeset
1090 }
35ef9b0a81a3 patch 8.2.1813: Vim9: can assign wrong type to script dict
Bram Moolenaar <Bram@vim.org>
parents: 22391
diff changeset
1091
35ef9b0a81a3 patch 8.2.1813: Vim9: can assign wrong type to script dict
Bram Moolenaar <Bram@vim.org>
parents: 22391
diff changeset
1092 /*
35ef9b0a81a3 patch 8.2.1813: Vim9: can assign wrong type to script dict
Bram Moolenaar <Bram@vim.org>
parents: 22391
diff changeset
1093 * 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
1094 * 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
1095 */
35ef9b0a81a3 patch 8.2.1813: Vim9: can assign wrong type to script dict
Bram Moolenaar <Bram@vim.org>
parents: 22391
diff changeset
1096 int
23917
4b417b776b95 patch 8.2.2501: not always clear where an error is reported
Bram Moolenaar <Bram@vim.org>
parents: 23877
diff changeset
1097 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
1098 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
1099 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
1100 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
1101 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
1102 {
35ef9b0a81a3 patch 8.2.1813: Vim9: can assign wrong type to script dict
Bram Moolenaar <Bram@vim.org>
parents: 22391
diff changeset
1103 int ret;
35ef9b0a81a3 patch 8.2.1813: Vim9: can assign wrong type to script dict
Bram Moolenaar <Bram@vim.org>
parents: 22391
diff changeset
1104
26302
7351926fbe9e patch 8.2.3682: Vim9: assigning to a script variable drops the type
Bram Moolenaar <Bram@vim.org>
parents: 25955
diff changeset
1105 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
1106 {
7351926fbe9e patch 8.2.3682: Vim9: assigning to a script variable drops the type
Bram Moolenaar <Bram@vim.org>
parents: 25955
diff changeset
1107 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
1108 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
1109 }
7351926fbe9e patch 8.2.3682: Vim9: assigning to a script variable drops the type
Bram Moolenaar <Bram@vim.org>
parents: 25955
diff changeset
1110 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
1111 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
1112 {
26302
7351926fbe9e patch 8.2.3682: Vim9: assigning to a script variable drops the type
Bram Moolenaar <Bram@vim.org>
parents: 25955
diff changeset
1113 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
1114
26302
7351926fbe9e patch 8.2.3682: Vim9: assigning to a script variable drops the type
Bram Moolenaar <Bram@vim.org>
parents: 25955
diff changeset
1115 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
1116 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
1117 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
1118 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
1119 }
26302
7351926fbe9e patch 8.2.3682: Vim9: assigning to a script variable drops the type
Bram Moolenaar <Bram@vim.org>
parents: 25955
diff changeset
1120 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
1121 }
20840
0600ab7b9f09 patch 8.2.0972: Vim9 script variable declarations need a type
Bram Moolenaar <Bram@vim.org>
parents: 20816
diff changeset
1122
24717
bf8feac8a89a patch 8.2.2897: Vim9: can use reserved words at the script level
Bram Moolenaar <Bram@vim.org>
parents: 24533
diff changeset
1123 // 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
1124 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
1125 "true",
bf8feac8a89a patch 8.2.2897: Vim9: can use reserved words at the script level
Bram Moolenaar <Bram@vim.org>
parents: 24533
diff changeset
1126 "false",
bf8feac8a89a patch 8.2.2897: Vim9: can use reserved words at the script level
Bram Moolenaar <Bram@vim.org>
parents: 24533
diff changeset
1127 "null",
28002
1012048eed26 patch 8.2.4526: Vim9: cannot set variables to a null value
Bram Moolenaar <Bram@vim.org>
parents: 27698
diff changeset
1128 "null_blob",
1012048eed26 patch 8.2.4526: Vim9: cannot set variables to a null value
Bram Moolenaar <Bram@vim.org>
parents: 27698
diff changeset
1129 "null_dict",
1012048eed26 patch 8.2.4526: Vim9: cannot set variables to a null value
Bram Moolenaar <Bram@vim.org>
parents: 27698
diff changeset
1130 "null_function",
1012048eed26 patch 8.2.4526: Vim9: cannot set variables to a null value
Bram Moolenaar <Bram@vim.org>
parents: 27698
diff changeset
1131 "null_list",
1012048eed26 patch 8.2.4526: Vim9: cannot set variables to a null value
Bram Moolenaar <Bram@vim.org>
parents: 27698
diff changeset
1132 "null_partial",
1012048eed26 patch 8.2.4526: Vim9: cannot set variables to a null value
Bram Moolenaar <Bram@vim.org>
parents: 27698
diff changeset
1133 "null_string",
1012048eed26 patch 8.2.4526: Vim9: cannot set variables to a null value
Bram Moolenaar <Bram@vim.org>
parents: 27698
diff changeset
1134 "null_channel",
1012048eed26 patch 8.2.4526: Vim9: cannot set variables to a null value
Bram Moolenaar <Bram@vim.org>
parents: 27698
diff changeset
1135 "null_job",
24888
b6ac4ed5e2d2 patch 8.2.2982: Vim9: future commands are not reserved yet
Bram Moolenaar <Bram@vim.org>
parents: 24717
diff changeset
1136 "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
1137 NULL
bf8feac8a89a patch 8.2.2897: Vim9: can use reserved words at the script level
Bram Moolenaar <Bram@vim.org>
parents: 24533
diff changeset
1138 };
bf8feac8a89a patch 8.2.2897: Vim9: can use reserved words at the script level
Bram Moolenaar <Bram@vim.org>
parents: 24533
diff changeset
1139
bf8feac8a89a patch 8.2.2897: Vim9: can use reserved words at the script level
Bram Moolenaar <Bram@vim.org>
parents: 24533
diff changeset
1140 int
bf8feac8a89a patch 8.2.2897: Vim9: can use reserved words at the script level
Bram Moolenaar <Bram@vim.org>
parents: 24533
diff changeset
1141 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
1142 {
bf8feac8a89a patch 8.2.2897: Vim9: can use reserved words at the script level
Bram Moolenaar <Bram@vim.org>
parents: 24533
diff changeset
1143 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
1144
bf8feac8a89a patch 8.2.2897: Vim9: can use reserved words at the script level
Bram Moolenaar <Bram@vim.org>
parents: 24533
diff changeset
1145 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
1146 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
1147 {
bf8feac8a89a patch 8.2.2897: Vim9: can use reserved words at the script level
Bram Moolenaar <Bram@vim.org>
parents: 24533
diff changeset
1148 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
1149 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
1150 }
bf8feac8a89a patch 8.2.2897: Vim9: can use reserved words at the script level
Bram Moolenaar <Bram@vim.org>
parents: 24533
diff changeset
1151 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
1152 }
bf8feac8a89a patch 8.2.2897: Vim9: can use reserved words at the script level
Bram Moolenaar <Bram@vim.org>
parents: 24533
diff changeset
1153
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1154 #endif // FEAT_EVAL