1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
|
#include "command_manager.hh"
#include "assert.hh"
#include "context.hh"
#include "register_manager.hh"
#include "shell_manager.hh"
#include "utils.hh"
#include <algorithm>
namespace Kakoune
{
bool CommandManager::command_defined(const String& command_name) const
{
return m_commands.find(command_name) != m_commands.end();
}
void CommandManager::register_command(String command_name,
Command command,
CommandCompleter completer)
{
m_commands[command_name] = { std::move(command), std::move(completer) };
}
void CommandManager::register_commands(const memoryview<String>& command_names,
Command command,
CommandCompleter completer)
{
for (auto command_name : command_names)
m_commands[command_name] = { command, completer };
}
parse_error::parse_error(const String& error)
: runtime_error{"parse error: " + error} {}
namespace
{
struct Token
{
enum class Type
{
Raw,
ShellExpand,
RegisterExpand,
OptionExpand,
CommandSeparator
};
Token() : m_type(Type::Raw) {}
explicit Token(const String& string) : m_content(string), m_type(Type::Raw) {}
explicit Token(Type type) : m_type(type) {}
Token(Type type, String str) : m_content(str), m_type(type) {}
Type type() const { return m_type; }
const String& content() const { return m_content; }
private:
Type m_type;
String m_content;
};
using TokenList = std::vector<Token>;
using TokenPosList = std::vector<std::pair<ByteCount, ByteCount>>;
bool is_command_separator(char c)
{
return c == ';' or c == '\n';
}
bool is_horizontal_blank(char c)
{
return c == ' ' or c == '\t';
}
struct unterminated_string : parse_error
{
unterminated_string(const String& open, const String& close, int nest = 0)
: parse_error{"unterminated string '" + open + "..." + close + "'" +
(nest > 0 ? "(nesting: " + int_to_str(nest) + ")" : "")}
{}
};
struct unknown_expand : parse_error
{
unknown_expand(const String& name)
: parse_error{"unknown expand '" + name + "'"} {}
};
TokenList parse(const String& line,
TokenPosList* opt_token_pos_info = nullptr)
{
TokenList result;
ByteCount length = line.length();
ByteCount pos = 0;
while (pos < length)
{
while (pos != length)
{
if (is_horizontal_blank(line[pos]))
++pos;
else if (line[pos] == '\\' and pos+1 < length and line[pos+1] == '\n')
pos += 2;
else if (line[pos] == '#')
{
while (pos != length and line[pos] != '\n')
++pos;
}
else
break;
}
ByteCount token_start = pos;
ByteCount start_pos = pos;
Token::Type type = Token::Type::Raw;
if (line[pos] == '"' or line[pos] == '\'')
{
char delimiter = line[pos];
token_start = ++pos;
while ((line[pos] != delimiter or line[pos-1] == '\\') and
pos != length)
++pos;
if (pos == length)
throw unterminated_string(String{delimiter}, String{delimiter});
}
else if (line[pos] == '%')
{
ByteCount type_start = ++pos;
while (isalpha(line[pos]))
++pos;
String type_name = line.substr(type_start, pos - type_start);
if (pos == length)
throw parse_error{"expected a string delimiter after '%" + type_name + "'"};
if (type_name == "sh")
type = Token::Type::ShellExpand;
else if (type_name == "reg")
type = Token::Type::RegisterExpand;
else if (type_name == "opt")
type = Token::Type::OptionExpand;
else if (type_name != "")
throw unknown_expand{type_name};
static const std::unordered_map<char, char> matching_delimiters = {
{ '(', ')' }, { '[', ']' }, { '{', '}' }, { '<', '>' }
};
char opening_delimiter = line[pos];
token_start = ++pos;
auto delim_it = matching_delimiters.find(opening_delimiter);
if (delim_it != matching_delimiters.end())
{
char closing_delimiter = delim_it->second;
int level = 0;
while (pos != length)
{
if (line[pos-1] != '\\' and line[pos] == opening_delimiter)
++level;
if (line[pos-1] != '\\' and line[pos] == closing_delimiter)
{
if (level > 0)
--level;
else
break;
}
++pos;
}
if (pos == length)
throw unterminated_string("%" + type_name + opening_delimiter,
String{closing_delimiter}, level);
}
else
{
while (pos != length and
(line[pos] != opening_delimiter or line[pos-1] == '\\'))
++pos;
if (pos == length)
throw unterminated_string("%" + type_name + opening_delimiter,
String{opening_delimiter});
}
}
else
while (pos != length and
((not is_command_separator(line[pos]) and
not is_horizontal_blank(line[pos]))
or (pos != 0 and line[pos-1] == '\\')))
++pos;
if (start_pos != pos)
{
if (opt_token_pos_info)
opt_token_pos_info->push_back({token_start, pos});
String token = line.substr(token_start, pos - token_start);
token = token.replace(R"(\\([ \t;\n]))", "\\1");
result.push_back({type, token});
}
if (is_command_separator(line[pos]))
{
if (opt_token_pos_info)
opt_token_pos_info->push_back({pos, pos+1});
result.push_back(Token{ Token::Type::CommandSeparator });
}
++pos;
}
if (not result.empty() and result.back().type() == Token::Type::CommandSeparator)
result.pop_back();
return result;
}
}
struct command_not_found : runtime_error
{
command_not_found(const String& command)
: runtime_error(command + " : no such command") {}
};
void CommandManager::execute_single_command(const CommandParameters& params,
Context& context) const
{
if (params.empty())
return;
auto command_it = m_commands.find(params[0]);
if (command_it == m_commands.end())
throw command_not_found(params[0]);
memoryview<String> param_view(params.begin()+1, params.end());
command_it->second.command(param_view, context);
}
void CommandManager::execute(const String& command_line,
Context& context,
const memoryview<String>& shell_params,
const EnvVarMap& env_vars)
{
TokenList tokens = parse(command_line);
if (tokens.empty())
return;
std::vector<String> params;
for (auto it = tokens.begin(); it != tokens.end(); ++it)
{
if (it->type() == Token::Type::ShellExpand)
{
String output = ShellManager::instance().eval(it->content(),
context, shell_params,
env_vars);
TokenList shell_tokens = parse(output);
it = tokens.erase(it);
for (auto& token : shell_tokens)
it = ++tokens.insert(it, std::move(token));
it -= shell_tokens.size();
// when last token is a ShellExpand which produces no output
if (it == tokens.end())
break;
}
if (it->type() == Token::Type::RegisterExpand)
{
if (it->content().length() != 1)
throw runtime_error("wrong register name: " + it->content());
Register& reg = RegisterManager::instance()[it->content()[0]];
params.push_back(reg.values(context)[0]);
}
if (it->type() == Token::Type::OptionExpand)
{
const Option& option = context.options()[it->content()];
params.push_back(option.get_as_string());
}
if (it->type() == Token::Type::CommandSeparator)
{
execute_single_command(params, context);
params.clear();
}
if (it->type() == Token::Type::Raw)
params.push_back(it->content());
}
execute_single_command(params, context);
}
Completions CommandManager::complete(const Context& context,
const String& command_line, ByteCount cursor_pos)
{
TokenPosList pos_info;
TokenList tokens = parse(command_line, &pos_info);
size_t token_to_complete = tokens.size();
for (size_t i = 0; i < tokens.size(); ++i)
{
if (pos_info[i].first <= cursor_pos and pos_info[i].second >= cursor_pos)
{
token_to_complete = i;
break;
}
}
if (token_to_complete == 0 or tokens.empty()) // command name completion
{
ByteCount cmd_start = tokens.empty() ? 0 : pos_info[0].first;
Completions result(cmd_start, cursor_pos);
String prefix = command_line.substr(cmd_start,
cursor_pos - cmd_start);
for (auto& command : m_commands)
{
if (command.first.substr(0, prefix.length()) == prefix)
result.candidates.push_back(command.first);
}
std::sort(result.candidates.begin(), result.candidates.end());
return result;
}
kak_assert(not tokens.empty());
if (tokens[0].type() != Token::Type::Raw)
return Completions();
const String& command_name = tokens[0].content();
auto command_it = m_commands.find(command_name);
if (command_it == m_commands.end() or not command_it->second.completer)
return Completions();
ByteCount start = token_to_complete < tokens.size() ?
pos_info[token_to_complete].first : cursor_pos;
Completions result(start , cursor_pos);
ByteCount cursor_pos_in_token = cursor_pos - start;
std::vector<String> params;
for (auto token_it = tokens.begin()+1; token_it != tokens.end(); ++token_it)
params.push_back(token_it->content());
result.candidates = command_it->second.completer(context, params,
token_to_complete - 1,
cursor_pos_in_token);
return result;
}
CandidateList PerArgumentCommandCompleter::operator()(const Context& context,
const CommandParameters& params,
size_t token_to_complete,
ByteCount pos_in_token) const
{
if (token_to_complete >= m_completers.size())
return CandidateList();
// it is possible to try to complete a new argument
kak_assert(token_to_complete <= params.size());
const String& argument = token_to_complete < params.size() ?
params[token_to_complete] : String();
return m_completers[token_to_complete](context, argument, pos_in_token);
}
}
|