+87
-35
lines changedFilter options
+87
-35
lines changed Original file line number Diff line number Diff line change
@@ -5774,26 +5774,44 @@ static void ex_delcommand(exarg_T *eap)
5774
5774
/// Split a string by unescaped whitespace (space & tab), used for f-args on Lua commands callback.
5775
5775
/// Similar to uc_split_args(), but does not allocate, add quotes, add commas and is an iterator.
5776
5776
///
5777
-
/// @note If no separator is found start = 0 and end = length - 1
5778
-
/// @param[in] arg String to split
5779
-
/// @param[in] iter Iteration counter
5780
-
/// @param[out] start Start of the split
5781
-
/// @param[out] end End of the split
5782
-
/// @param[in] length Length of the string
5777
+
/// @param[in] arg String to split
5778
+
/// @param[in] arglen Length of {arg}
5779
+
/// @param[inout] end Index of last character of previous iteration
5780
+
/// @param[out] buf Buffer to copy string into
5781
+
/// @param[out] len Length of string in {buf}
5783
5782
///
5784
-
/// @return false if it's the last split (don't call again), true otherwise (call again).
5785
-
bool uc_split_args_iter(const char_u *arg, int iter, int *start, int *end, int length)
5786
-
{
5787
-
int pos;
5788
-
*start = *end + (iter > 1 ? 2 : 0); // Skip whitespace after the first split
5789
-
for (pos = *start; pos < length - 2; pos++) {
5790
-
if (arg[pos] != '\\' && ascii_iswhite(arg[pos + 1])) {
5791
-
*end = pos;
5792
-
return true;
5783
+
/// @return true if iteration is complete, else false
5784
+
bool uc_split_args_iter(const char_u *arg, size_t arglen, size_t *end, char *buf, size_t *len)
5785
+
{
5786
+
if (!arglen) {
5787
+
return true;
5788
+
}
5789
+
5790
+
size_t pos = *end;
5791
+
while (pos < arglen && ascii_iswhite(arg[pos])) {
5792
+
pos++;
5793
+
}
5794
+
5795
+
size_t l = 0;
5796
+
for (; pos < arglen - 1; pos++) {
5797
+
if (arg[pos] == '\\' && (arg[pos + 1] == '\\' || ascii_iswhite(arg[pos + 1]))) {
5798
+
buf[l++] = arg[++pos];
5799
+
} else {
5800
+
buf[l++] = arg[pos];
5801
+
if (ascii_iswhite(arg[pos + 1])) {
5802
+
*end = pos + 1;
5803
+
*len = l;
5804
+
return false;
5805
+
}
5793
5806
}
5794
5807
}
5795
-
*end = length - 1;
5796
-
return false;
5808
+
5809
+
if (pos < arglen && !ascii_iswhite(arg[pos])) {
5810
+
buf[l++] = arg[pos];
5811
+
}
5812
+
5813
+
*len = l;
5814
+
return true;
5797
5815
}
5798
5816
5799
5817
/// split and quote args for <f-args>
Original file line number Diff line number Diff line change
@@ -1869,17 +1869,21 @@ void nlua_do_ucmd(ucmd_T *cmd, exarg_T *eap)
1869
1869
} else {
1870
1870
// Commands with more than one possible argument we split
1871
1871
lua_pop(lstate, 1); // Pop the reference of opts.args
1872
-
int length = (int)STRLEN(eap->arg);
1873
-
int start = 0;
1874
-
int end = 0;
1872
+
size_t length = STRLEN(eap->arg);
1873
+
size_t end = 0;
1874
+
size_t len = 0;
1875
1875
int i = 1;
1876
-
bool res = true;
1877
-
while (res) {
1878
-
res = uc_split_args_iter(eap->arg, i, &start, &end, length);
1879
-
lua_pushlstring(lstate, (const char *)eap->arg + start, (size_t)(end - start + 1));
1880
-
lua_rawseti(lstate, -2, i);
1881
-
i++;
1876
+
char *buf = xcalloc(length, sizeof(char));
1877
+
bool done = false;
1878
+
while (!done) {
1879
+
done = uc_split_args_iter(eap->arg, length, &end, buf, &len);
1880
+
if (len > 0) {
1881
+
lua_pushlstring(lstate, buf, len);
1882
+
lua_rawseti(lstate, -2, i);
1883
+
i++;
1884
+
}
1882
1885
}
1886
+
xfree(buf);
1883
1887
}
1884
1888
lua_setfield(lstate, -2, "fargs");
1885
1889
Original file line number Diff line number Diff line change
@@ -114,8 +114,8 @@ describe('nvim_create_user_command', function()
114
114
]]
115
115
116
116
eq({
117
-
args = [[hello my\ friend how\ are\ you?]],
118
-
fargs = {[[hello]], [[my\ friend]], [[how\ are\ you?]]},
117
+
args = [[this is a\ test]],
118
+
fargs = {"this", "is", "a test"},
119
119
bang = false,
120
120
line1 = 1,
121
121
line2 = 1,
@@ -124,12 +124,42 @@ describe('nvim_create_user_command', function()
124
124
count = 2,
125
125
reg = "",
126
126
}, exec_lua [=[
127
-
vim.api.nvim_command([[CommandWithLuaCallback hello my\ friend how\ are\ you?]])
127
+
vim.api.nvim_command([[CommandWithLuaCallback this is a\ test]])
128
128
return result
129
129
]=])
130
130
131
131
eq({
132
-
args = 'h\tey',
132
+
args = [[this includes\ a backslash: \\]],
133
+
fargs = {"this", "includes a", "backslash:", "\\"},
134
+
bang = false,
135
+
line1 = 1,
136
+
line2 = 1,
137
+
mods = "",
138
+
range = 0,
139
+
count = 2,
140
+
reg = "",
141
+
}, exec_lua [=[
142
+
vim.api.nvim_command([[CommandWithLuaCallback this includes\ a backslash: \\]])
143
+
return result
144
+
]=])
145
+
146
+
eq({
147
+
args = "a\\b",
148
+
fargs = {"a\\b"},
149
+
bang = false,
150
+
line1 = 1,
151
+
line2 = 1,
152
+
mods = "",
153
+
range = 0,
154
+
count = 2,
155
+
reg = "",
156
+
}, exec_lua [=[
157
+
vim.api.nvim_command('CommandWithLuaCallback a\\b')
158
+
return result
159
+
]=])
160
+
161
+
eq({
162
+
args = 'h\tey ',
133
163
fargs = {[[h]], [[ey]]},
134
164
bang = true,
135
165
line1 = 10,
@@ -139,7 +169,7 @@ describe('nvim_create_user_command', function()
139
169
count = 10,
140
170
reg = "",
141
171
}, exec_lua [=[
142
-
vim.api.nvim_command('botright 10CommandWithLuaCallback! h\tey')
172
+
vim.api.nvim_command('botright 10CommandWithLuaCallback! h\tey ')
143
173
return result
144
174
]=])
145
175
@@ -160,7 +190,7 @@ describe('nvim_create_user_command', function()
160
190
161
191
eq({
162
192
args = "",
163
-
fargs = {""}, -- fargs works without args
193
+
fargs = {}, -- fargs works without args
164
194
bang = false,
165
195
line1 = 1,
166
196
line2 = 1,
@@ -186,8 +216,8 @@ describe('nvim_create_user_command', function()
186
216
]]
187
217
188
218
eq({
189
-
args = "hello I'm one argmuent",
190
-
fargs = {"hello I'm one argmuent"}, -- Doesn't split args
219
+
args = "hello I'm one argument",
220
+
fargs = {"hello I'm one argument"}, -- Doesn't split args
191
221
bang = false,
192
222
line1 = 1,
193
223
line2 = 1,
@@ -196,7 +226,7 @@ describe('nvim_create_user_command', function()
196
226
count = 2,
197
227
reg = "",
198
228
}, exec_lua [[
199
-
vim.api.nvim_command('CommandWithOneArg hello I\'m one argmuent')
229
+
vim.api.nvim_command('CommandWithOneArg hello I\'m one argument')
200
230
return result
201
231
]])
202
232
You can’t perform that action at this time.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4