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
|
#include "command_manager.hh"
#include "utils.hh"
#include "assert.hh"
#include "context.hh"
#include <algorithm>
#include <cstring>
#include <sys/types.h>
#include <sys/wait.h>
namespace Kakoune
{
void CommandManager::register_command(const std::string& command_name, Command command,
unsigned flags,
const CommandCompleter& completer)
{
m_commands[command_name] = CommandDescriptor { command, flags, completer };
}
void CommandManager::register_commands(const memoryview<std::string>& command_names, Command command,
unsigned flags,
const CommandCompleter& completer)
{
for (auto command_name : command_names)
register_command(command_name, command, flags, completer);
}
static bool is_blank(char c)
{
return c == ' ' or c == '\t' or c == '\n';
}
typedef std::vector<std::pair<size_t, size_t>> TokenList;
static TokenList split(const std::string& line)
{
TokenList result;
size_t pos = 0;
while (pos < line.length())
{
while(is_blank(line[pos]) and pos != line.length())
++pos;
size_t token_start = pos;
if (line[pos] == '"' or line[pos] == '\'' or line[pos] == '`')
{
char delimiter = line[pos];
++pos;
token_start = delimiter == '`' ? pos - 1 : pos;
while ((line[pos] != delimiter or line[pos-1] == '\\') and
pos != line.length())
++pos;
if (delimiter == '`' and line[pos] == '`')
++pos;
}
else
while (not is_blank(line[pos]) and pos != line.length() and
(line[pos] != ';' or line[pos-1] == '\\'))
++pos;
if (token_start != pos)
result.push_back(std::make_pair(token_start, pos));
if (line[pos] == ';')
result.push_back(std::make_pair(pos, pos+1));
++pos;
}
return result;
}
struct command_not_found : runtime_error
{
command_not_found(const std::string& command)
: runtime_error(command + " : no such command") {}
};
void CommandManager::execute(const std::string& command_line,
const Context& context)
{
TokenList tokens = split(command_line);
if (tokens.empty())
return;
std::vector<std::string> params;
for (auto it = tokens.begin(); it != tokens.end(); ++it)
{
params.push_back(command_line.substr(it->first,
it->second - it->first));
}
execute(params, context);
}
static void shell_eval(std::vector<std::string>& params,
const std::string& cmdline,
const Context& context)
{
int write_pipe[2];
int read_pipe[2];
pipe(write_pipe);
pipe(read_pipe);
if (pid_t pid = fork())
{
close(write_pipe[0]);
close(read_pipe[1]);
close(write_pipe[1]);
std::string output;
char buffer[1024];
while (size_t size = read(read_pipe[0], buffer, 1024))
output += std::string(buffer, buffer+size);
close(read_pipe[0]);
waitpid(pid, NULL, 0);
TokenList tokens = split(output);
for (auto it = tokens.begin(); it != tokens.end(); ++it)
{
params.push_back(output.substr(it->first,
it->second - it->first));
}
}
else
{
close(write_pipe[1]);
close(read_pipe[0]);
dup2(read_pipe[1], 1);
dup2(write_pipe[0], 0);
if (context.has_buffer())
setenv("kak_bufname", context.buffer().name().c_str(), 1);
execlp("sh", "sh", "-c", cmdline.c_str(), NULL);
}
}
void CommandManager::execute(const CommandParameters& params,
const Context& context)
{
if (params.empty())
return;
auto begin = params.begin();
auto end = begin;
while (true)
{
while (end != params.end() and *end != ";")
++end;
if (end != begin)
{
std::vector<std::string> expanded_params;
auto command_it = m_commands.find(*begin);
if (command_it == m_commands.end() and
begin->front() == '`' and begin->back() == '`')
{
shell_eval(expanded_params,
begin->substr(1, begin->length() - 2),
context);
if (not expanded_params.empty())
{
command_it = m_commands.find(expanded_params[0]);
expanded_params.erase(expanded_params.begin());
}
}
if (command_it == m_commands.end())
throw command_not_found(*begin);
if (command_it->second.flags & IgnoreSemiColons)
end = params.end();
if (command_it->second.flags & DeferredShellEval)
command_it->second.command(CommandParameters(begin + 1, end), context);
else
{
for (auto param = begin+1; param != end; ++param)
{
if (param->front() == '`' and param->back() == '`')
shell_eval(expanded_params,
param->substr(1, param->length() - 2),
context);
else
expanded_params.push_back(*param);
}
command_it->second.command(expanded_params, context);
}
}
if (end == params.end())
break;
begin = end+1;
end = begin;
}
}
Completions CommandManager::complete(const std::string& command_line, size_t cursor_pos)
{
TokenList tokens = split(command_line);
size_t token_to_complete = tokens.size();
for (size_t i = 0; i < tokens.size(); ++i)
{
if (tokens[i].first <= cursor_pos and tokens[i].second >= cursor_pos)
{
token_to_complete = i;
break;
}
}
if (token_to_complete == 0 or tokens.empty()) // command name completion
{
size_t cmd_start = tokens.empty() ? 0 : tokens[0].first;
Completions result(cmd_start, cursor_pos);
std::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);
}
return result;
}
assert(not tokens.empty());
std::string command_name =
command_line.substr(tokens[0].first,
tokens[0].second - tokens[0].first);
auto command_it = m_commands.find(command_name);
if (command_it == m_commands.end() or not command_it->second.completer)
return Completions();
std::vector<std::string> params;
for (auto it = tokens.begin() + 1; it != tokens.end(); ++it)
{
params.push_back(command_line.substr(it->first,
it->second - it->first));
}
size_t start = token_to_complete < tokens.size() ?
tokens[token_to_complete].first : cursor_pos;
Completions result(start , cursor_pos);
size_t cursor_pos_in_token = cursor_pos - start;
result.candidates = command_it->second.completer(params,
token_to_complete - 1,
cursor_pos_in_token);
return result;
}
CandidateList PerArgumentCommandCompleter::operator()(const CommandParameters& params,
size_t token_to_complete,
size_t pos_in_token) const
{
if (token_to_complete >= m_completers.size())
return CandidateList();
// it is possible to try to complete a new argument
assert(token_to_complete <= params.size());
const std::string& argument = token_to_complete < params.size() ?
params[token_to_complete] : std::string();
return m_completers[token_to_complete](argument, pos_in_token);
}
}
|