annotate src/vim9script.c @ 29631:9d54c8e6c682 v9.0.0156

patch 9.0.0156: giving E1170 only in an expression is confusing Commit: https://github.com/vim/vim/commit/3f74c0ab32de7ed17dd16d9b06f5455a0bb1c297 Author: Bram Moolenaar <Bram@vim.org> Date: Sat Aug 6 18:12:06 2022 +0100 patch 9.0.0156: giving E1170 only in an expression is confusing Problem: Giving E1170 only in an expression is confusing. Solution: Give E1170 for any "#{ comment". (closes https://github.com/vim/vim/issues/10855)
author Bram Moolenaar <Bram@vim.org>
date Sat, 06 Aug 2022 19:15:03 +0200
parents 842690841d50
children 3fcd11b8c81a
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 /*
29631
9d54c8e6c682 patch 9.0.0156: giving E1170 only in an expression is confusing
Bram Moolenaar <Bram@vim.org>
parents: 29619
diff changeset
179 * Return TRUE if "p" points at "#{", not "#{{".
9d54c8e6c682 patch 9.0.0156: giving E1170 only in an expression is confusing
Bram Moolenaar <Bram@vim.org>
parents: 29619
diff changeset
180 * Give an error message if not done already.
24158
93e69703a290 patch 8.2.2620: Vim9: Using #{ for a dictionary gives strange errors
Bram Moolenaar <Bram@vim.org>
parents: 24114
diff changeset
181 * 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
182 * understand errors.
93e69703a290 patch 8.2.2620: Vim9: Using #{ for a dictionary gives strange errors
Bram Moolenaar <Bram@vim.org>
parents: 24114
diff changeset
183 */
93e69703a290 patch 8.2.2620: Vim9: Using #{ for a dictionary gives strange errors
Bram Moolenaar <Bram@vim.org>
parents: 24114
diff changeset
184 int
93e69703a290 patch 8.2.2620: Vim9: Using #{ for a dictionary gives strange errors
Bram Moolenaar <Bram@vim.org>
parents: 24114
diff changeset
185 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
186 {
29631
9d54c8e6c682 patch 9.0.0156: giving E1170 only in an expression is confusing
Bram Moolenaar <Bram@vim.org>
parents: 29619
diff changeset
187 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
188 {
29631
9d54c8e6c682 patch 9.0.0156: giving E1170 only in an expression is confusing
Bram Moolenaar <Bram@vim.org>
parents: 29619
diff changeset
189 if (!did_emsg)
9d54c8e6c682 patch 9.0.0156: giving E1170 only in an expression is confusing
Bram Moolenaar <Bram@vim.org>
parents: 29619
diff changeset
190 emsg(_(e_cannot_use_hash_curly_to_start_comment));
24158
93e69703a290 patch 8.2.2620: Vim9: Using #{ for a dictionary gives strange errors
Bram Moolenaar <Bram@vim.org>
parents: 24114
diff changeset
191 return TRUE;
93e69703a290 patch 8.2.2620: Vim9: Using #{ for a dictionary gives strange errors
Bram Moolenaar <Bram@vim.org>
parents: 24114
diff changeset
192 }
93e69703a290 patch 8.2.2620: Vim9: Using #{ for a dictionary gives strange errors
Bram Moolenaar <Bram@vim.org>
parents: 24114
diff changeset
193 return FALSE;
93e69703a290 patch 8.2.2620: Vim9: Using #{ for a dictionary gives strange errors
Bram Moolenaar <Bram@vim.org>
parents: 24114
diff changeset
194 }
27018
268f6a3511df patch 8.2.4038: various code not used when features are disabled
Bram Moolenaar <Bram@vim.org>
parents: 26992
diff changeset
195 #endif
24158
93e69703a290 patch 8.2.2620: Vim9: Using #{ for a dictionary gives strange errors
Bram Moolenaar <Bram@vim.org>
parents: 24114
diff changeset
196
93e69703a290 patch 8.2.2620: Vim9: Using #{ for a dictionary gives strange errors
Bram Moolenaar <Bram@vim.org>
parents: 24114
diff changeset
197 /*
24176
12378fbc99bc patch 8.2.2629: Vim9: error for #{{ is not desired
Bram Moolenaar <Bram@vim.org>
parents: 24160
diff changeset
198 * Return TRUE if "p" points at a "#" not followed by one '{'.
29631
9d54c8e6c682 patch 9.0.0156: giving E1170 only in an expression is confusing
Bram Moolenaar <Bram@vim.org>
parents: 29619
diff changeset
199 * Gives an error for using "#{", not for "#{{".
24158
93e69703a290 patch 8.2.2620: Vim9: Using #{ for a dictionary gives strange errors
Bram Moolenaar <Bram@vim.org>
parents: 24114
diff changeset
200 * 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
201 */
517fca70e084 patch 8.2.2239: Vim9: concatenating lines with backslash is inconvenient
Bram Moolenaar <Bram@vim.org>
parents: 23390
diff changeset
202 int
517fca70e084 patch 8.2.2239: Vim9: concatenating lines with backslash is inconvenient
Bram Moolenaar <Bram@vim.org>
parents: 23390
diff changeset
203 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
204 {
29631
9d54c8e6c682 patch 9.0.0156: giving E1170 only in an expression is confusing
Bram Moolenaar <Bram@vim.org>
parents: 29619
diff changeset
205 return p[0] == '#' && !vim9_bad_comment(p);
23392
517fca70e084 patch 8.2.2239: Vim9: concatenating lines with backslash is inconvenient
Bram Moolenaar <Bram@vim.org>
parents: 23390
diff changeset
206 }
517fca70e084 patch 8.2.2239: Vim9: concatenating lines with backslash is inconvenient
Bram Moolenaar <Bram@vim.org>
parents: 23390
diff changeset
207
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
208 #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
209
21516
c7b2ce90c2de patch 8.2.1308: Vim9: accidentally using "x" causes Vim to exit
Bram Moolenaar <Bram@vim.org>
parents: 21473
diff changeset
210 /*
24533
9c404d78d767 patch 8.2.2806: Vim9: using "++nr" as a command might not work
Bram Moolenaar <Bram@vim.org>
parents: 24531
diff changeset
211 * "++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
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 void
9c404d78d767 patch 8.2.2806: Vim9: using "++nr" as a command might not work
Bram Moolenaar <Bram@vim.org>
parents: 24531
diff changeset
214 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
215 {
9c404d78d767 patch 8.2.2806: Vim9: using "++nr" as a command might not work
Bram Moolenaar <Bram@vim.org>
parents: 24531
diff changeset
216 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
217 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
218 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
219
25022
39551b6e0112 patch 8.2.3048: strange error for white space after ++ command
Bram Moolenaar <Bram@vim.org>
parents: 25020
diff changeset
220 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
221 {
39551b6e0112 patch 8.2.3048: strange error for white space after ++ command
Bram Moolenaar <Bram@vim.org>
parents: 25020
diff changeset
222 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
223 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
224 return;
39551b6e0112 patch 8.2.3048: strange error for white space after ++ command
Bram Moolenaar <Bram@vim.org>
parents: 25020
diff changeset
225 }
39551b6e0112 patch 8.2.3048: strange error for white space after ++ command
Bram Moolenaar <Bram@vim.org>
parents: 25020
diff changeset
226
24533
9c404d78d767 patch 8.2.2806: Vim9: using "++nr" as a command might not work
Bram Moolenaar <Bram@vim.org>
parents: 24531
diff changeset
227 // 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
228 // 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
229 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
230 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
231 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
232 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
233 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
234 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
235 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
236 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
237 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
238 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
239 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
240 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
241 }
9c404d78d767 patch 8.2.2806: Vim9: using "++nr" as a command might not work
Bram Moolenaar <Bram@vim.org>
parents: 24531
diff changeset
242
9c404d78d767 patch 8.2.2806: Vim9: using "++nr" as a command might not work
Bram Moolenaar <Bram@vim.org>
parents: 24531
diff changeset
243 /*
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
244 * ":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
245 * ":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
246 * ":export def Name(..."
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
247 * ":export class Name ..."
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
248 */
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
249 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
250 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
251 {
27074
19fefc42a063 patch 8.2.4066: Vim9: imported autoload script loaded again
Bram Moolenaar <Bram@vim.org>
parents: 27057
diff changeset
252 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
253
21279
8d1d11afd8c8 patch 8.2.1190: Vim9: checking for Vim9 syntax is spread out
Bram Moolenaar <Bram@vim.org>
parents: 21218
diff changeset
254 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
255 {
21821
0deb6f96a5a3 patch 8.2.1460: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 21789
diff changeset
256 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
257 return;
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
258 }
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
259
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
260 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
261 (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
262 switch (eap->cmdidx)
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
263 {
22391
a9fb7efa31d6 patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents: 22250
diff changeset
264 case CMD_var:
a9fb7efa31d6 patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents: 22250
diff changeset
265 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
266 case CMD_const:
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
267 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
268 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
269 // case CMD_class:
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
270 is_export = TRUE;
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
271 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
272 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
273
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
274 // 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
275 if (is_export)
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
276 {
27074
19fefc42a063 patch 8.2.4066: Vim9: imported autoload script loaded again
Bram Moolenaar <Bram@vim.org>
parents: 27057
diff changeset
277 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
278 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
279 is_export = FALSE;
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
280 }
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
281 break;
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
282 default:
27074
19fefc42a063 patch 8.2.4066: Vim9: imported autoload script loaded again
Bram Moolenaar <Bram@vim.org>
parents: 27057
diff changeset
283 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
284 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
285 break;
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 }
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 /*
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
290 * 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
291 */
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
292 static imported_T *
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
293 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
294 {
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
295 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
296 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
297 return NULL;
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
298 }
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
299
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
300 /*
25567
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
301 * 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
302 */
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
303 static void
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
304 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
305 {
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
306 int todo;
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
307 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
308 hashitem_T *hi;
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
309 sallvar_T *sav;
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
310 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
311 int idx;
25567
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
312
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
313 hash_lock(ht);
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
314 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
315 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
316 {
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
317 if (!HASHITEM_EMPTY(hi))
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
318 {
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
319 --todo;
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
320
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
321 // 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
322 // 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
323 sav = HI2SAV(hi);
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
324 while (sav != NULL)
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
325 {
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
326 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
327 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
328 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
329 vim_free(sav);
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
330 sav = sav_next;
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
331 }
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
332 }
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
333 }
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
334 hash_clear(ht);
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
335 hash_init(ht);
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
336
26482
b115b552071f patch 8.2.3771: Vim9: accessing freed memory when checking type
Bram Moolenaar <Bram@vim.org>
parents: 26302
diff changeset
337 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
338 {
b115b552071f patch 8.2.3771: Vim9: accessing freed memory when checking type
Bram Moolenaar <Bram@vim.org>
parents: 26302
diff changeset
339 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
340
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
341 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
342 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
343 }
25567
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
344 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
345
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
346 // 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
347 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
348 }
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
349
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
350 /*
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
351 * 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
352 */
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
353 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
354 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
355 {
19191
133ef7ba4e4e patch 8.2.0154: reallocating the list of scripts is inefficient
Bram Moolenaar <Bram@vim.org>
parents: 19181
diff changeset
356 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
357 int idx;
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
358
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
359 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
360 {
19726
ad37a198a708 patch 8.2.0419: various memory leaks in Vim9 script code
Bram Moolenaar <Bram@vim.org>
parents: 19623
diff changeset
361 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
362
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
363 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
364 }
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
365 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
366
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
367 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
368
21218
1f4d0375f947 patch 8.2.1160: Vim9: memory leak in allocated types
Bram Moolenaar <Bram@vim.org>
parents: 21194
diff changeset
369 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
370 }
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
371
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
372 /*
23364
17a0e32eefd4 patch 8.2.2225: Vim9: error when using :import in legacy script twice
Bram Moolenaar <Bram@vim.org>
parents: 23362
diff changeset
373 * 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
374 * 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
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 void
17a0e32eefd4 patch 8.2.2225: Vim9: error when using :import in legacy script twice
Bram Moolenaar <Bram@vim.org>
parents: 23362
diff changeset
377 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
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 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
380 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
381
17a0e32eefd4 patch 8.2.2225: Vim9: error when using :import in legacy script twice
Bram Moolenaar <Bram@vim.org>
parents: 23362
diff changeset
382 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
383 {
17a0e32eefd4 patch 8.2.2225: Vim9: error when using :import in legacy script twice
Bram Moolenaar <Bram@vim.org>
parents: 23362
diff changeset
384 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
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 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
387 }
17a0e32eefd4 patch 8.2.2225: Vim9: error when using :import in legacy script twice
Bram Moolenaar <Bram@vim.org>
parents: 23362
diff changeset
388 }
17a0e32eefd4 patch 8.2.2225: Vim9: error when using :import in legacy script twice
Bram Moolenaar <Bram@vim.org>
parents: 23362
diff changeset
389
17a0e32eefd4 patch 8.2.2225: Vim9: error when using :import in legacy script twice
Bram Moolenaar <Bram@vim.org>
parents: 23362
diff changeset
390 /*
28249
4b322951ebac patch 8.2.4650: "import autoload" only works with using 'runtimepath'
Bram Moolenaar <Bram@vim.org>
parents: 28231
diff changeset
391 * 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
392 * 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
393 */
4b322951ebac patch 8.2.4650: "import autoload" only works with using 'runtimepath'
Bram Moolenaar <Bram@vim.org>
parents: 28231
diff changeset
394 static int
4b322951ebac patch 8.2.4650: "import autoload" only works with using 'runtimepath'
Bram Moolenaar <Bram@vim.org>
parents: 28231
diff changeset
395 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
396 {
4b322951ebac patch 8.2.4650: "import autoload" only works with using 'runtimepath'
Bram Moolenaar <Bram@vim.org>
parents: 28231
diff changeset
397 if (is_autoload)
4b322951ebac patch 8.2.4650: "import autoload" only works with using 'runtimepath'
Bram Moolenaar <Bram@vim.org>
parents: 28231
diff changeset
398 {
4b322951ebac patch 8.2.4650: "import autoload" only works with using 'runtimepath'
Bram Moolenaar <Bram@vim.org>
parents: 28231
diff changeset
399 scriptitem_T *si;
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 *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
402 if (*sid < 0)
4b322951ebac patch 8.2.4650: "import autoload" only works with using 'runtimepath'
Bram Moolenaar <Bram@vim.org>
parents: 28231
diff changeset
403 {
4b322951ebac patch 8.2.4650: "import autoload" only works with using 'runtimepath'
Bram Moolenaar <Bram@vim.org>
parents: 28231
diff changeset
404 int error = OK;
4b322951ebac patch 8.2.4650: "import autoload" only works with using 'runtimepath'
Bram Moolenaar <Bram@vim.org>
parents: 28231
diff changeset
405
28257
6d2b5c2178e2 patch 8.2.4654: missing changes for import check
Bram Moolenaar <Bram@vim.org>
parents: 28249
diff changeset
406 // 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
407 // scriptitem.
6d2b5c2178e2 patch 8.2.4654: missing changes for import check
Bram Moolenaar <Bram@vim.org>
parents: 28249
diff changeset
408 if (!file_is_readable(fname))
6d2b5c2178e2 patch 8.2.4654: missing changes for import check
Bram Moolenaar <Bram@vim.org>
parents: 28249
diff changeset
409 {
6d2b5c2178e2 patch 8.2.4654: missing changes for import check
Bram Moolenaar <Bram@vim.org>
parents: 28249
diff changeset
410 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
411 : 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
412 return FAIL;
6d2b5c2178e2 patch 8.2.4654: missing changes for import check
Bram Moolenaar <Bram@vim.org>
parents: 28249
diff changeset
413 }
28249
4b322951ebac patch 8.2.4650: "import autoload" only works with using 'runtimepath'
Bram Moolenaar <Bram@vim.org>
parents: 28231
diff changeset
414 *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
415 if (error == FAIL)
4b322951ebac patch 8.2.4650: "import autoload" only works with using 'runtimepath'
Bram Moolenaar <Bram@vim.org>
parents: 28231
diff changeset
416 return FAIL;
4b322951ebac patch 8.2.4650: "import autoload" only works with using 'runtimepath'
Bram Moolenaar <Bram@vim.org>
parents: 28231
diff changeset
417 }
4b322951ebac patch 8.2.4650: "import autoload" only works with using 'runtimepath'
Bram Moolenaar <Bram@vim.org>
parents: 28231
diff changeset
418
4b322951ebac patch 8.2.4650: "import autoload" only works with using 'runtimepath'
Bram Moolenaar <Bram@vim.org>
parents: 28231
diff changeset
419 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
420 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
421
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
422 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
423 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
424
28249
4b322951ebac patch 8.2.4650: "import autoload" only works with using 'runtimepath'
Bram Moolenaar <Bram@vim.org>
parents: 28231
diff changeset
425 // 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
426 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
427 return OK;
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 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
430 }
4b322951ebac patch 8.2.4650: "import autoload" only works with using 'runtimepath'
Bram Moolenaar <Bram@vim.org>
parents: 28231
diff changeset
431
4b322951ebac patch 8.2.4650: "import autoload" only works with using 'runtimepath'
Bram Moolenaar <Bram@vim.org>
parents: 28231
diff changeset
432 /*
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
433 * 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
434 * 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
435 * "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
436 * 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
437 */
25567
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
438 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
439 handle_import(
465d6e40e79c patch 8.2.1124: Vim9: no line break allowed in :import command
Bram Moolenaar <Bram@vim.org>
parents: 21120
diff changeset
440 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
441 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
442 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
443 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
444 void *cctx)
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
445 {
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
446 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
447 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
448 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
449 int getnext;
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
450 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
451 int ret = FAIL;
26980
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
452 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
453 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
454 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
455 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
456 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
457 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
458 int i;
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
459
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
460 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
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 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
463 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
464 }
15f40772e10a patch 8.2.4050: Vim9: need to prefix every item in an autoload script
Bram Moolenaar <Bram@vim.org>
parents: 27030
diff changeset
465
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 // 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
467 // string.
26980
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
468 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
469 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
470 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
471 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
472 || 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
473 {
26980
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
474 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
475 goto erret;
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
476 }
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
477
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
478 // 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
479 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
480
21146
465d6e40e79c patch 8.2.1124: Vim9: no line break allowed in :import command
Bram Moolenaar <Bram@vim.org>
parents: 21120
diff changeset
481 /*
465d6e40e79c patch 8.2.1124: Vim9: no line break allowed in :import command
Bram Moolenaar <Bram@vim.org>
parents: 21120
diff changeset
482 * 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
483 */
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
484 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
485 {
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
486 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
487 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
488 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
489 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
490
28249
4b322951ebac patch 8.2.4650: "import autoload" only works with using 'runtimepath'
Bram Moolenaar <Bram@vim.org>
parents: 28231
diff changeset
491 // 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
492 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
493 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
494 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
495 goto erret;
4b322951ebac patch 8.2.4650: "import autoload" only works with using 'runtimepath'
Bram Moolenaar <Bram@vim.org>
parents: 28231
diff changeset
496 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
497 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
498 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
499 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
500
28249
4b322951ebac patch 8.2.4650: "import autoload" only works with using 'runtimepath'
Bram Moolenaar <Bram@vim.org>
parents: 28231
diff changeset
501 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
502 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
503 }
27211
61d6a31a5874 patch 8.2.4134: MS-Windows: test for import with absolute path fails
Bram Moolenaar <Bram@vim.org>
parents: 27207
diff changeset
504 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
505 #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
506 // 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
507 // 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
508 || *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
509 #endif
61d6a31a5874 patch 8.2.4134: MS-Windows: test for import with absolute path fails
Bram Moolenaar <Bram@vim.org>
parents: 27207
diff changeset
510 )
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
511 {
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
512 // 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
513 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
514 }
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 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
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 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
518 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
519
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 // 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
521 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
522 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
523 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
524 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
525 // 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
526 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
527 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
528 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
529 {
1e2a6c6c7e42 patch 8.2.4072: Vim9: compiling function fails when autoload is not loaded
Bram Moolenaar <Bram@vim.org>
parents: 27074
diff changeset
530 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
531
1e2a6c6c7e42 patch 8.2.4072: Vim9: compiling function fails when autoload is not loaded
Bram Moolenaar <Bram@vim.org>
parents: 27074
diff changeset
532 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
533 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
534 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
535 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
536 // 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
537 (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
538 }
1e2a6c6c7e42 patch 8.2.4072: Vim9: compiling function fails when autoload is not loaded
Bram Moolenaar <Bram@vim.org>
parents: 27074
diff changeset
539 else
1e2a6c6c7e42 patch 8.2.4072: Vim9: compiling function fails when autoload is not loaded
Bram Moolenaar <Bram@vim.org>
parents: 27074
diff changeset
540 res = FAIL;
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
541 }
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
542 else
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 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
545 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
546
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
547 // 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
548 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
549 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
550 goto erret;
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
551 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
552 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
553 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
554 }
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
555
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
556 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
557 {
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
558 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
559 ? 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
560 : 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
561 goto erret;
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
562 }
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
563
27607
db1831f585a4 patch 8.2.4330: Vim9: no error if script imports itself
Bram Moolenaar <Bram@vim.org>
parents: 27390
diff changeset
564 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
565 {
db1831f585a4 patch 8.2.4330: Vim9: no error if script imports itself
Bram Moolenaar <Bram@vim.org>
parents: 27390
diff changeset
566 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
567 goto erret;
db1831f585a4 patch 8.2.4330: Vim9: no error if script imports itself
Bram Moolenaar <Bram@vim.org>
parents: 27390
diff changeset
568 }
db1831f585a4 patch 8.2.4330: Vim9: no error if script imports itself
Bram Moolenaar <Bram@vim.org>
parents: 27390
diff changeset
569
27030
c8809b8d19de patch 8.2.4044: Vim9: no error when importing the same script twice
Bram Moolenaar <Bram@vim.org>
parents: 27018
diff changeset
570 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
571 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
572 {
c8809b8d19de patch 8.2.4044: Vim9: no error when importing the same script twice
Bram Moolenaar <Bram@vim.org>
parents: 27018
diff changeset
573 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
574
c8809b8d19de patch 8.2.4044: Vim9: no error when importing the same script twice
Bram Moolenaar <Bram@vim.org>
parents: 27018
diff changeset
575 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
576 {
c8809b8d19de patch 8.2.4044: Vim9: no error when importing the same script twice
Bram Moolenaar <Bram@vim.org>
parents: 27018
diff changeset
577 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
578 {
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
579 // 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
580 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
581 break;
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 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
584 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
585 }
c8809b8d19de patch 8.2.4044: Vim9: no error when importing the same script twice
Bram Moolenaar <Bram@vim.org>
parents: 27018
diff changeset
586 }
c8809b8d19de patch 8.2.4044: Vim9: no error when importing the same script twice
Bram Moolenaar <Bram@vim.org>
parents: 27018
diff changeset
587
26980
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
588 // 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
589 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
590 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
591 {
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
592 char_u *p;
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
593
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
594 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
595 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
596 else
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
597 arg = nextarg;
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
598
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
599 // 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
600 // 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
601 arg = skipwhite(arg + 2);
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
602 p = arg;
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
603 if (eval_isnamec1(*arg))
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
604 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
605 ++arg;
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
606 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
607 {
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
608 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
609 goto erret;
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 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
612 arg = skipwhite(arg);
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
613 }
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
614 else
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 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
617 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
618
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
619 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
620 {
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
621 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
622 goto erret;
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
623 }
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
624 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
625 {
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
626 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
627 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
628 }
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
629 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
630 {
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
631 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
632 goto erret;
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 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
635 }
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
636
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
637 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
638 {
24029
429b4f8d2fac patch 8.2.2556: Vim9: :import with "as" not fully supported
Bram Moolenaar <Bram@vim.org>
parents: 23980
diff changeset
639 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
640
27698
3813036f19cb patch 8.2.4375: ctx_imports is not used
Bram Moolenaar <Bram@vim.org>
parents: 27613
diff changeset
641 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
642 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
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 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
645 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
646 }
27030
c8809b8d19de patch 8.2.4044: Vim9: no error when importing the same script twice
Bram Moolenaar <Bram@vim.org>
parents: 27018
diff changeset
647 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
648 && 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
649 FALSE) == FAIL)
26980
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
650 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
651
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
652 if (imported == NULL)
27074
19fefc42a063 patch 8.2.4066: Vim9: imported autoload script loaded again
Bram Moolenaar <Bram@vim.org>
parents: 27057
diff changeset
653 {
19fefc42a063 patch 8.2.4066: Vim9: imported autoload script loaded again
Bram Moolenaar <Bram@vim.org>
parents: 27057
diff changeset
654 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
655 if (imported == NULL)
19fefc42a063 patch 8.2.4066: Vim9: imported autoload script loaded again
Bram Moolenaar <Bram@vim.org>
parents: 27057
diff changeset
656 goto erret;
19fefc42a063 patch 8.2.4066: Vim9: imported autoload script loaded again
Bram Moolenaar <Bram@vim.org>
parents: 27057
diff changeset
657 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
658 as_name = NULL;
19fefc42a063 patch 8.2.4066: Vim9: imported autoload script loaded again
Bram Moolenaar <Bram@vim.org>
parents: 27057
diff changeset
659 imported->imp_sid = sid;
19fefc42a063 patch 8.2.4066: Vim9: imported autoload script loaded again
Bram Moolenaar <Bram@vim.org>
parents: 27057
diff changeset
660 if (is_autoload)
19fefc42a063 patch 8.2.4066: Vim9: imported autoload script loaded again
Bram Moolenaar <Bram@vim.org>
parents: 27057
diff changeset
661 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
662 }
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
663 }
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
664
21146
465d6e40e79c patch 8.2.1124: Vim9: no line break allowed in :import command
Bram Moolenaar <Bram@vim.org>
parents: 21120
diff changeset
665 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
666 clear_tv(&tv);
26980
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
667 vim_free(as_name);
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
668 return arg;
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
669 }
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
670
20840
0600ab7b9f09 patch 8.2.0972: Vim9 script variable declarations need a type
Bram Moolenaar <Bram@vim.org>
parents: 20816
diff changeset
671 /*
26980
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
672 * ":import 'filename'"
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
673 * ":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
674 */
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
675 void
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
676 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
677 {
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
678 char_u *cmd_end;
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
679 evalarg_T evalarg;
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
680
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
681 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
682 {
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
683 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
684 return;
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
685 }
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
686 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
687
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
688 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
689 &evalarg, NULL);
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
690 if (cmd_end != NULL)
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
691 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
692 clear_evalarg(&evalarg, eap);
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
693 }
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
694
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
695 /*
26980
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
696 * 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
697 * 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
698 * 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
699 * 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
700 * 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
701 */
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
702 int
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
703 find_exported(
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
704 int sid,
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
705 char_u *name,
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
706 ufunc_T **ufunc,
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
707 type_T **type,
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
708 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
709 cstack_T *cstack,
25567
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
710 int verbose)
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
711 {
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
712 int idx = -1;
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
713 svar_T *sv;
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
714 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
715
28596
daf785caecf2 patch 8.2.4822: setting ufunc to NULL twice
Bram Moolenaar <Bram@vim.org>
parents: 28594
diff changeset
716 *ufunc = NULL;
daf785caecf2 patch 8.2.4822: setting ufunc to NULL twice
Bram Moolenaar <Bram@vim.org>
parents: 28594
diff changeset
717
28249
4b322951ebac patch 8.2.4650: "import autoload" only works with using 'runtimepath'
Bram Moolenaar <Bram@vim.org>
parents: 28231
diff changeset
718 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
719 {
4b322951ebac patch 8.2.4650: "import autoload" only works with using 'runtimepath'
Bram Moolenaar <Bram@vim.org>
parents: 28231
diff changeset
720 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
721 {
294771aab3ae patch 8.2.4821: crash when imported autoload script was deleted
Bram Moolenaar <Bram@vim.org>
parents: 28457
diff changeset
722 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
723 return -1;
28594
294771aab3ae patch 8.2.4821: crash when imported autoload script was deleted
Bram Moolenaar <Bram@vim.org>
parents: 28457
diff changeset
724 }
28249
4b322951ebac patch 8.2.4650: "import autoload" only works with using 'runtimepath'
Bram Moolenaar <Bram@vim.org>
parents: 28231
diff changeset
725 }
4b322951ebac patch 8.2.4650: "import autoload" only works with using 'runtimepath'
Bram Moolenaar <Bram@vim.org>
parents: 28231
diff changeset
726
25567
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
727 // 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
728 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
729 if (idx >= 0)
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 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
732 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
733 {
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
734 if (verbose)
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
735 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
736 return -1;
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
737 }
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
738 *type = sv->sv_type;
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
739 }
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
740 else
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
741 {
27049
140102677c12 patch 8.2.4053: Vim9: autoload mechanism doesn't fully work yet
Bram Moolenaar <Bram@vim.org>
parents: 27043
diff changeset
742 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
743 char_u buffer[200];
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
744 char_u *funcname;
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
745
27049
140102677c12 patch 8.2.4053: Vim9: autoload mechanism doesn't fully work yet
Bram Moolenaar <Bram@vim.org>
parents: 27043
diff changeset
746 // 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
747 // "<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
748 // 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
749 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
750 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
751 else
140102677c12 patch 8.2.4053: Vim9: autoload mechanism doesn't fully work yet
Bram Moolenaar <Bram@vim.org>
parents: 27043
diff changeset
752 len += 15;
140102677c12 patch 8.2.4053: Vim9: autoload mechanism doesn't fully work yet
Bram Moolenaar <Bram@vim.org>
parents: 27043
diff changeset
753
140102677c12 patch 8.2.4053: Vim9: autoload mechanism doesn't fully work yet
Bram Moolenaar <Bram@vim.org>
parents: 27043
diff changeset
754 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
755 funcname = buffer;
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
756 else
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
757 {
27049
140102677c12 patch 8.2.4053: Vim9: autoload mechanism doesn't fully work yet
Bram Moolenaar <Bram@vim.org>
parents: 27043
diff changeset
758 funcname = alloc(len);
25567
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
759 if (funcname == NULL)
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
760 return -1;
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
761 }
27049
140102677c12 patch 8.2.4053: Vim9: autoload mechanism doesn't fully work yet
Bram Moolenaar <Bram@vim.org>
parents: 27043
diff changeset
762 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
763 {
140102677c12 patch 8.2.4053: Vim9: autoload mechanism doesn't fully work yet
Bram Moolenaar <Bram@vim.org>
parents: 27043
diff changeset
764 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
765 }
140102677c12 patch 8.2.4053: Vim9: autoload mechanism doesn't fully work yet
Bram Moolenaar <Bram@vim.org>
parents: 27043
diff changeset
766 else
140102677c12 patch 8.2.4053: Vim9: autoload mechanism doesn't fully work yet
Bram Moolenaar <Bram@vim.org>
parents: 27043
diff changeset
767 {
140102677c12 patch 8.2.4053: Vim9: autoload mechanism doesn't fully work yet
Bram Moolenaar <Bram@vim.org>
parents: 27043
diff changeset
768 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
769 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
770 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
771 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
772 }
27114
98a01021e465 patch 8.2.4086: "cctx" argument of find_func_even_dead() is unused
Bram Moolenaar <Bram@vim.org>
parents: 27094
diff changeset
773 *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
774
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
775 if (*ufunc == NULL)
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
776 {
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
777 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
778 {
facb54d20a50 patch 8.2.4137: Vim9: calling import with and without method is inconsistent
Bram Moolenaar <Bram@vim.org>
parents: 27215
diff changeset
779 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
780
facb54d20a50 patch 8.2.4137: Vim9: calling import with and without method is inconsistent
Bram Moolenaar <Bram@vim.org>
parents: 27215
diff changeset
781 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
782 {
facb54d20a50 patch 8.2.4137: Vim9: calling import with and without method is inconsistent
Bram Moolenaar <Bram@vim.org>
parents: 27215
diff changeset
783 // 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
784 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
785 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
786 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
787 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
788 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
789 }
facb54d20a50 patch 8.2.4137: Vim9: calling import with and without method is inconsistent
Bram Moolenaar <Bram@vim.org>
parents: 27215
diff changeset
790 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
791 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
792 else
facb54d20a50 patch 8.2.4137: Vim9: calling import with and without method is inconsistent
Bram Moolenaar <Bram@vim.org>
parents: 27215
diff changeset
793 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
794 }
25567
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
795 }
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
796 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
797 {
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
798 if (verbose)
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
799 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
800 *ufunc = NULL;
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
801 }
27217
facb54d20a50 patch 8.2.4137: Vim9: calling import with and without method is inconsistent
Bram Moolenaar <Bram@vim.org>
parents: 27215
diff changeset
802 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
803 vim_free(funcname);
25567
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 return idx;
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
807 }
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
808
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25521
diff changeset
809 /*
29006
99a7eb1881c7 patch 8.2.5025: Vim9: a few lines not covered by tests
Bram Moolenaar <Bram@vim.org>
parents: 28690
diff changeset
810 * 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
811 * "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
812 * 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
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 *
0600ab7b9f09 patch 8.2.0972: Vim9 script variable declarations need a type
Bram Moolenaar <Bram@vim.org>
parents: 20816
diff changeset
815 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
816 {
0600ab7b9f09 patch 8.2.0972: Vim9 script variable declarations need a type
Bram Moolenaar <Bram@vim.org>
parents: 20816
diff changeset
817 char_u *p;
0600ab7b9f09 patch 8.2.0972: Vim9 script variable declarations need a type
Bram Moolenaar <Bram@vim.org>
parents: 20816
diff changeset
818 char_u *name;
0600ab7b9f09 patch 8.2.0972: Vim9 script variable declarations need a type
Bram Moolenaar <Bram@vim.org>
parents: 20816
diff changeset
819 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
820 type_T *type;
0600ab7b9f09 patch 8.2.0972: Vim9 script variable declarations need a type
Bram Moolenaar <Bram@vim.org>
parents: 20816
diff changeset
821 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
822
22391
a9fb7efa31d6 patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents: 22250
diff changeset
823 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
824 {
22391
a9fb7efa31d6 patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents: 22250
diff changeset
825 if (eap->cmdidx == CMD_final)
a9fb7efa31d6 patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents: 22250
diff changeset
826 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
827 else
a9fb7efa31d6 patch 8.2.1744: Vim9: using ":const!" is weird
Bram Moolenaar <Bram@vim.org>
parents: 22250
diff changeset
828 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
829 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
830 }
0600ab7b9f09 patch 8.2.0972: Vim9 script variable declarations need a type
Bram Moolenaar <Bram@vim.org>
parents: 20816
diff changeset
831
0600ab7b9f09 patch 8.2.0972: Vim9 script variable declarations need a type
Bram Moolenaar <Bram@vim.org>
parents: 20816
diff changeset
832 // 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
833 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
834 {
26865
bce848ec8b1b patch 8.2.3961: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 26662
diff changeset
835 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
836 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
837 }
0600ab7b9f09 patch 8.2.0972: Vim9 script variable declarations need a type
Bram Moolenaar <Bram@vim.org>
parents: 20816
diff changeset
838
20846
709379ab5110 patch 8.2.0975: Vim9: script variable does not accept optional s: prefix
Bram Moolenaar <Bram@vim.org>
parents: 20844
diff changeset
839 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
840 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
841 break;
20840
0600ab7b9f09 patch 8.2.0972: Vim9 script variable declarations need a type
Bram Moolenaar <Bram@vim.org>
parents: 20816
diff changeset
842
0600ab7b9f09 patch 8.2.0972: Vim9 script variable declarations need a type
Bram Moolenaar <Bram@vim.org>
parents: 20816
diff changeset
843 if (*p != ':')
0600ab7b9f09 patch 8.2.0972: Vim9 script variable declarations need a type
Bram Moolenaar <Bram@vim.org>
parents: 20816
diff changeset
844 {
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
845 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
846 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
847 }
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 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
849 {
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
850 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
851 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
852 }
20840
0600ab7b9f09 patch 8.2.0972: Vim9 script variable declarations need a type
Bram Moolenaar <Bram@vim.org>
parents: 20816
diff changeset
853 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
854
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 // 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
856 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
857 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
858 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
859 {
1360541e8c74 patch 8.2.0974: Vim9: memory leak when script var has wrong type
Bram Moolenaar <Bram@vim.org>
parents: 20842
diff changeset
860 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
861 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
862 }
20840
0600ab7b9f09 patch 8.2.0972: Vim9 script variable declarations need a type
Bram Moolenaar <Bram@vim.org>
parents: 20816
diff changeset
863
0600ab7b9f09 patch 8.2.0972: Vim9 script variable declarations need a type
Bram Moolenaar <Bram@vim.org>
parents: 20816
diff changeset
864 // 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
865 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
866 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
867 // 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
868 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
869 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
870 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
871 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
872
0600ab7b9f09 patch 8.2.0972: Vim9 script variable declarations need a type
Bram Moolenaar <Bram@vim.org>
parents: 20816
diff changeset
873 vim_free(name);
0600ab7b9f09 patch 8.2.0972: Vim9 script variable declarations need a type
Bram Moolenaar <Bram@vim.org>
parents: 20816
diff changeset
874 return p;
0600ab7b9f09 patch 8.2.0972: Vim9 script variable declarations need a type
Bram Moolenaar <Bram@vim.org>
parents: 20816
diff changeset
875 }
0600ab7b9f09 patch 8.2.0972: Vim9 script variable declarations need a type
Bram Moolenaar <Bram@vim.org>
parents: 20816
diff changeset
876
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
877 /*
22643
71b57779177d patch 8.2.1870: Vim9: no need to keep all script variables
Bram Moolenaar <Bram@vim.org>
parents: 22596
diff changeset
878 * 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
879 * 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
880 * 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
881 * 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
882 * "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
883 * 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
884 * "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
885 */
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
886 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
887 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
888 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
889 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
890 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
891 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
892 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
893 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
894 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
895 {
23362
f181fe2150ab patch 8.2.2224: Vim9: crash if script reloaded with different variable type
Bram Moolenaar <Bram@vim.org>
parents: 23358
diff changeset
896 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
897 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
898 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
899
23362
f181fe2150ab patch 8.2.2224: Vim9: crash if script reloaded with different variable type
Bram Moolenaar <Bram@vim.org>
parents: 23358
diff changeset
900 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
901 {
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
902 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
903 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
904
f181fe2150ab patch 8.2.2224: Vim9: crash if script reloaded with different variable type
Bram Moolenaar <Bram@vim.org>
parents: 23358
diff changeset
905 // 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
906 // 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
907 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
908 return;
f181fe2150ab patch 8.2.2224: Vim9: crash if script reloaded with different variable type
Bram Moolenaar <Bram@vim.org>
parents: 23358
diff changeset
909
27049
140102677c12 patch 8.2.4053: Vim9: autoload mechanism doesn't fully work yet
Bram Moolenaar <Bram@vim.org>
parents: 27043
diff changeset
910 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
911 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
912 {
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
913 // 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
914 // 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
915 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
916 {
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 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
918 {
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 // 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
920 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
921 + 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
922 // 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
923 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
924 {
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 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
926 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
927 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
928 }
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 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
932 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
933 }
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 }
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
935
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
936 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
937 {
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 // 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
939 // 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
940 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
941 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
942 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
943 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
944 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
945
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
946 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
947 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
948 : (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
949 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
950 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
951 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
952 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
953 ++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
954 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
955 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
956 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
957 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
958
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 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
960 // 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
961 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
962 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
963 // 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
964 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
965 }
23362
f181fe2150ab patch 8.2.2224: Vim9: crash if script reloaded with different variable type
Bram Moolenaar <Bram@vim.org>
parents: 23358
diff changeset
966 }
f181fe2150ab patch 8.2.2224: Vim9: crash if script reloaded with different variable type
Bram Moolenaar <Bram@vim.org>
parents: 23358
diff changeset
967 else
f181fe2150ab patch 8.2.2224: Vim9: crash if script reloaded with different variable type
Bram Moolenaar <Bram@vim.org>
parents: 23358
diff changeset
968 {
28313
b418e073b42f patch 8.2.4682: Vim9: can use :unlockvar for const variable
Bram Moolenaar <Bram@vim.org>
parents: 28261
diff changeset
969 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
970 }
f181fe2150ab patch 8.2.2224: Vim9: crash if script reloaded with different variable type
Bram Moolenaar <Bram@vim.org>
parents: 23358
diff changeset
971 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
972 {
23458
d2b1269c2c68 patch 8.2.2272: Vim9: extend() can violate the type of a variable
Bram Moolenaar <Bram@vim.org>
parents: 23392
diff changeset
973 if (*type == NULL)
26925
4e77f9961650 patch 8.2.3991: Vim9: error when extending dict<any>
Bram Moolenaar <Bram@vim.org>
parents: 26877
diff changeset
974 *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
975 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
976 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
977 && (*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
978 && 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
979 {
66b245d84f37 patch 8.2.4642: Vim9: in :def function script var cannot be null
Bram Moolenaar <Bram@vim.org>
parents: 28164
diff changeset
980 // "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
981 *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
982 }
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
983 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
984 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
985 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
986 || (*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
987 {
b115b552071f patch 8.2.3771: Vim9: accessing freed memory when checking type
Bram Moolenaar <Bram@vim.org>
parents: 26302
diff changeset
988 // 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
989 // 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
990 // 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
991 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
992 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
993 }
b115b552071f patch 8.2.3771: Vim9: accessing freed memory when checking type
Bram Moolenaar <Bram@vim.org>
parents: 26302
diff changeset
994 else
b115b552071f patch 8.2.3771: Vim9: accessing freed memory when checking type
Bram Moolenaar <Bram@vim.org>
parents: 26302
diff changeset
995 {
b115b552071f patch 8.2.3771: Vim9: accessing freed memory when checking type
Bram Moolenaar <Bram@vim.org>
parents: 26302
diff changeset
996 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
997 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
998 }
23362
f181fe2150ab patch 8.2.2224: Vim9: crash if script reloaded with different variable type
Bram Moolenaar <Bram@vim.org>
parents: 23358
diff changeset
999 }
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
23362
f181fe2150ab patch 8.2.2224: Vim9: crash if script reloaded with different variable type
Bram Moolenaar <Bram@vim.org>
parents: 23358
diff changeset
1001 // 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
1002 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
1003 }
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
1004
22596
107eae953b87 patch 8.2.1846: Vim9: block variables are not found in compiled function
Bram Moolenaar <Bram@vim.org>
parents: 22594
diff changeset
1005 /*
107eae953b87 patch 8.2.1846: Vim9: block variables are not found in compiled function
Bram Moolenaar <Bram@vim.org>
parents: 22594
diff changeset
1006 * 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
1007 * "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
1008 * 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
1009 * 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
1010 */
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
1011 void
22643
71b57779177d patch 8.2.1870: Vim9: no need to keep all script variables
Bram Moolenaar <Bram@vim.org>
parents: 22596
diff changeset
1012 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
1013 {
22596
107eae953b87 patch 8.2.1846: Vim9: block variables are not found in compiled function
Bram Moolenaar <Bram@vim.org>
parents: 22594
diff changeset
1014 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
1015 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
1016 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
1017 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
1018 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
1019
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 // 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
1021 // 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
1022 // 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
1023 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
1024 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
1025 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
1026 {
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 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
1028 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
1029 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
1030
22596
107eae953b87 patch 8.2.1846: Vim9: block variables are not found in compiled function
Bram Moolenaar <Bram@vim.org>
parents: 22594
diff changeset
1031 // 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
1032 // 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
1033 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
1034 {
71b57779177d patch 8.2.1870: Vim9: no need to keep all script variables
Bram Moolenaar <Bram@vim.org>
parents: 22596
diff changeset
1035 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
1036 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
1037 }
22596
107eae953b87 patch 8.2.1846: Vim9: block variables are not found in compiled function
Bram Moolenaar <Bram@vim.org>
parents: 22594
diff changeset
1038 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
1039 {
22643
71b57779177d patch 8.2.1870: Vim9: no need to keep all script variables
Bram Moolenaar <Bram@vim.org>
parents: 22596
diff changeset
1040 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
1041 {
71b57779177d patch 8.2.1870: Vim9: no need to keep all script variables
Bram Moolenaar <Bram@vim.org>
parents: 22596
diff changeset
1042 // 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
1043 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
1044 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
1045 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
1046 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
1047 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
1048 }
71b57779177d patch 8.2.1870: Vim9: no need to keep all script variables
Bram Moolenaar <Bram@vim.org>
parents: 22596
diff changeset
1049 else
71b57779177d patch 8.2.1870: Vim9: no need to keep all script variables
Bram Moolenaar <Bram@vim.org>
parents: 22596
diff changeset
1050 {
71b57779177d patch 8.2.1870: Vim9: no need to keep all script variables
Bram Moolenaar <Bram@vim.org>
parents: 22596
diff changeset
1051 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
1052 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
1053 else
71b57779177d patch 8.2.1870: Vim9: no need to keep all script variables
Bram Moolenaar <Bram@vim.org>
parents: 22596
diff changeset
1054 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
1055 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
1056 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
1057 }
22596
107eae953b87 patch 8.2.1846: Vim9: block variables are not found in compiled function
Bram Moolenaar <Bram@vim.org>
parents: 22594
diff changeset
1058 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
1059 }
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
1060 }
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
1061 }
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
1062
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
1063 /*
22529
35ef9b0a81a3 patch 8.2.1813: Vim9: can assign wrong type to script dict
Bram Moolenaar <Bram@vim.org>
parents: 22391
diff changeset
1064 * 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
1065 * 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
1066 * 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
1067 * 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
1068 */
22529
35ef9b0a81a3 patch 8.2.1813: Vim9: can assign wrong type to script dict
Bram Moolenaar <Bram@vim.org>
parents: 22391
diff changeset
1069 svar_T *
28313
b418e073b42f patch 8.2.4682: Vim9: can use :unlockvar for const variable
Bram Moolenaar <Bram@vim.org>
parents: 28261
diff changeset
1070 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
1071 {
26980
8796f1384750 patch 8.2.4019: Vim9: import mechanism is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 26925
diff changeset
1072 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
1073 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
1074
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 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
1076 // 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
1077 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
1078
25955
4003fc2340dc patch 8.2.3511: Vim9: entry for loop variable is created every round
Bram Moolenaar <Bram@vim.org>
parents: 25755
diff changeset
1079 // 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
1080 // 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
1081 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
1082 {
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
1083 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
1084
23094
8b93aea8d9d7 patch 8.2.2093: Vim9: script test sometimes fails
Bram Moolenaar <Bram@vim.org>
parents: 22740
diff changeset
1085 // 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
1086 // 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
1087 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
1088 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
1089 }
28313
b418e073b42f patch 8.2.4682: Vim9: can use :unlockvar for const variable
Bram Moolenaar <Bram@vim.org>
parents: 28261
diff changeset
1090 if (must_find)
b418e073b42f patch 8.2.4682: Vim9: can use :unlockvar for const variable
Bram Moolenaar <Bram@vim.org>
parents: 28261
diff changeset
1091 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
1092 return NULL;
35ef9b0a81a3 patch 8.2.1813: Vim9: can assign wrong type to script dict
Bram Moolenaar <Bram@vim.org>
parents: 22391
diff changeset
1093 }
35ef9b0a81a3 patch 8.2.1813: Vim9: can assign wrong type to script dict
Bram Moolenaar <Bram@vim.org>
parents: 22391
diff changeset
1094
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 * 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
1097 * 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
1098 */
35ef9b0a81a3 patch 8.2.1813: Vim9: can assign wrong type to script dict
Bram Moolenaar <Bram@vim.org>
parents: 22391
diff changeset
1099 int
23917
4b417b776b95 patch 8.2.2501: not always clear where an error is reported
Bram Moolenaar <Bram@vim.org>
parents: 23877
diff changeset
1100 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
1101 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
1102 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
1103 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
1104 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
1105 {
35ef9b0a81a3 patch 8.2.1813: Vim9: can assign wrong type to script dict
Bram Moolenaar <Bram@vim.org>
parents: 22391
diff changeset
1106 int ret;
35ef9b0a81a3 patch 8.2.1813: Vim9: can assign wrong type to script dict
Bram Moolenaar <Bram@vim.org>
parents: 22391
diff changeset
1107
26302
7351926fbe9e patch 8.2.3682: Vim9: assigning to a script variable drops the type
Bram Moolenaar <Bram@vim.org>
parents: 25955
diff changeset
1108 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
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 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
1111 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
1112 }
7351926fbe9e patch 8.2.3682: Vim9: assigning to a script variable drops the type
Bram Moolenaar <Bram@vim.org>
parents: 25955
diff changeset
1113 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
1114 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
1115 {
26302
7351926fbe9e patch 8.2.3682: Vim9: assigning to a script variable drops the type
Bram Moolenaar <Bram@vim.org>
parents: 25955
diff changeset
1116 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
1117
26302
7351926fbe9e patch 8.2.3682: Vim9: assigning to a script variable drops the type
Bram Moolenaar <Bram@vim.org>
parents: 25955
diff changeset
1118 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
1119 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
1120 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
1121 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
1122 }
26302
7351926fbe9e patch 8.2.3682: Vim9: assigning to a script variable drops the type
Bram Moolenaar <Bram@vim.org>
parents: 25955
diff changeset
1123 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
1124 }
20840
0600ab7b9f09 patch 8.2.0972: Vim9 script variable declarations need a type
Bram Moolenaar <Bram@vim.org>
parents: 20816
diff changeset
1125
24717
bf8feac8a89a patch 8.2.2897: Vim9: can use reserved words at the script level
Bram Moolenaar <Bram@vim.org>
parents: 24533
diff changeset
1126 // 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
1127 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
1128 "true",
bf8feac8a89a patch 8.2.2897: Vim9: can use reserved words at the script level
Bram Moolenaar <Bram@vim.org>
parents: 24533
diff changeset
1129 "false",
bf8feac8a89a patch 8.2.2897: Vim9: can use reserved words at the script level
Bram Moolenaar <Bram@vim.org>
parents: 24533
diff changeset
1130 "null",
28002
1012048eed26 patch 8.2.4526: Vim9: cannot set variables to a null value
Bram Moolenaar <Bram@vim.org>
parents: 27698
diff changeset
1131 "null_blob",
1012048eed26 patch 8.2.4526: Vim9: cannot set variables to a null value
Bram Moolenaar <Bram@vim.org>
parents: 27698
diff changeset
1132 "null_dict",
1012048eed26 patch 8.2.4526: Vim9: cannot set variables to a null value
Bram Moolenaar <Bram@vim.org>
parents: 27698
diff changeset
1133 "null_function",
1012048eed26 patch 8.2.4526: Vim9: cannot set variables to a null value
Bram Moolenaar <Bram@vim.org>
parents: 27698
diff changeset
1134 "null_list",
1012048eed26 patch 8.2.4526: Vim9: cannot set variables to a null value
Bram Moolenaar <Bram@vim.org>
parents: 27698
diff changeset
1135 "null_partial",
1012048eed26 patch 8.2.4526: Vim9: cannot set variables to a null value
Bram Moolenaar <Bram@vim.org>
parents: 27698
diff changeset
1136 "null_string",
1012048eed26 patch 8.2.4526: Vim9: cannot set variables to a null value
Bram Moolenaar <Bram@vim.org>
parents: 27698
diff changeset
1137 "null_channel",
1012048eed26 patch 8.2.4526: Vim9: cannot set variables to a null value
Bram Moolenaar <Bram@vim.org>
parents: 27698
diff changeset
1138 "null_job",
24888
b6ac4ed5e2d2 patch 8.2.2982: Vim9: future commands are not reserved yet
Bram Moolenaar <Bram@vim.org>
parents: 24717
diff changeset
1139 "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
1140 NULL
bf8feac8a89a patch 8.2.2897: Vim9: can use reserved words at the script level
Bram Moolenaar <Bram@vim.org>
parents: 24533
diff changeset
1141 };
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
bf8feac8a89a patch 8.2.2897: Vim9: can use reserved words at the script level
Bram Moolenaar <Bram@vim.org>
parents: 24533
diff changeset
1144 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
1145 {
bf8feac8a89a patch 8.2.2897: Vim9: can use reserved words at the script level
Bram Moolenaar <Bram@vim.org>
parents: 24533
diff changeset
1146 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
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 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
1149 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
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 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
1152 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
1153 }
bf8feac8a89a patch 8.2.2897: Vim9: can use reserved words at the script level
Bram Moolenaar <Bram@vim.org>
parents: 24533
diff changeset
1154 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
1155 }
bf8feac8a89a patch 8.2.2897: Vim9: can use reserved words at the script level
Bram Moolenaar <Bram@vim.org>
parents: 24533
diff changeset
1156
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1157 #endif // FEAT_EVAL