2011-09-07 20:16:56 +02:00
|
|
|
#include "command_manager.hh"
|
|
|
|
|
2014-10-30 00:22:54 +01:00
|
|
|
#include "alias_registry.hh"
|
2011-09-16 11:18:51 +02:00
|
|
|
#include "assert.hh"
|
2017-08-29 10:23:03 +02:00
|
|
|
#include "buffer_utils.hh"
|
2012-02-13 22:38:07 +01:00
|
|
|
#include "context.hh"
|
2017-03-15 19:25:59 +01:00
|
|
|
#include "flags.hh"
|
2019-04-07 01:43:40 +02:00
|
|
|
#include "file.hh"
|
2017-08-29 10:23:03 +02:00
|
|
|
#include "optional.hh"
|
2018-05-26 02:01:26 +02:00
|
|
|
#include "option_types.hh"
|
2017-08-29 10:23:03 +02:00
|
|
|
#include "ranges.hh"
|
2019-11-23 08:59:44 +01:00
|
|
|
#include "regex.hh"
|
2012-08-11 12:13:48 +02:00
|
|
|
#include "register_manager.hh"
|
2013-04-09 20:05:40 +02:00
|
|
|
#include "shell_manager.hh"
|
|
|
|
#include "utils.hh"
|
Refactor command line parsing
Command line parsing now works as follow:
* Quoted strings ('...', "..." and %~...~ with '~' non nestable)
use 'doubling-up' for escaping their delimiter, if the delimiter
appears twice in a row, it is considered as part of the string and
represent one delimiter character. So 'abc''def' == "abc'def". No
other escaping takes place in those strings.
* Balanced strings (%{...}) do not support any kind of escaping, but
finds the matching closing delimiter by taking nesting into account.
So %{abc{def}} == "abc{def}".
* Non quoted words support escaping of `;` and whitespaces with `\`,
`%`, `'` and '"` can be escaped with `\` at the start of the word,
they do not need escaping (and will not be escaped) else where in
a word where they are treated literally. Any other use of '\' is a
literal '\'. So \%abc%\;\ def == "%abc%; def"
As discussed in #2046 this should make our command line syntax more
robust, provide a simple programmatic way to escape a string content
(s/<delim>/<delim><delim>/g), be well defined instead of ad-hoc
undocumented behaviour, and interact nicely with other common
escaping by avoiding escaping hell (:grep <regex> can in most case
be written with the regex unquoted).
2018-05-21 14:22:34 +02:00
|
|
|
#include "unit_tests.hh"
|
2011-09-16 11:18:51 +02:00
|
|
|
|
2011-09-07 20:16:56 +02:00
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2017-04-10 22:33:20 +02:00
|
|
|
bool CommandManager::command_defined(StringView command_name) const
|
2012-06-02 17:49:35 +02:00
|
|
|
{
|
2014-10-30 00:22:54 +01:00
|
|
|
return m_commands.find(command_name) != m_commands.end();
|
2012-06-02 17:49:35 +02:00
|
|
|
}
|
|
|
|
|
2012-11-22 14:28:14 +01:00
|
|
|
void CommandManager::register_command(String command_name,
|
2017-11-04 09:00:34 +01:00
|
|
|
CommandFunc func,
|
2014-02-12 10:02:09 +01:00
|
|
|
String docstring,
|
2014-02-08 02:02:58 +01:00
|
|
|
ParameterDesc param_desc,
|
2013-11-12 20:38:19 +01:00
|
|
|
CommandFlags flags,
|
2015-02-08 20:04:20 +01:00
|
|
|
CommandHelper helper,
|
2012-11-22 14:28:14 +01:00
|
|
|
CommandCompleter completer)
|
2011-09-07 20:16:56 +02:00
|
|
|
{
|
2017-11-04 09:00:34 +01:00
|
|
|
m_commands[command_name] = { std::move(func),
|
2014-04-08 20:54:32 +02:00
|
|
|
std::move(docstring),
|
|
|
|
std::move(param_desc),
|
|
|
|
flags,
|
2015-02-08 20:04:20 +01:00
|
|
|
std::move(helper),
|
2014-04-08 20:54:32 +02:00
|
|
|
std::move(completer) };
|
2011-09-07 20:16:56 +02:00
|
|
|
}
|
|
|
|
|
2019-03-12 18:34:30 +01:00
|
|
|
bool CommandManager::module_defined(StringView module_name) const
|
|
|
|
{
|
|
|
|
return m_modules.find(module_name) != m_modules.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CommandManager::register_module(String module_name, String commands)
|
|
|
|
{
|
|
|
|
auto module = m_modules.find(module_name);
|
|
|
|
if (module != m_modules.end() and module->value.loaded)
|
|
|
|
throw runtime_error{format("module already loaded: '{}'", module_name)};
|
|
|
|
|
|
|
|
m_modules[module_name] = { false, std::move(commands) };
|
|
|
|
}
|
|
|
|
|
|
|
|
void CommandManager::load_module(StringView module_name, Context& context)
|
|
|
|
{
|
|
|
|
auto module = m_modules.find(module_name);
|
|
|
|
if (module == m_modules.end())
|
|
|
|
throw runtime_error{format("no such module: '{}'", module_name)};
|
|
|
|
if (module->value.loaded)
|
|
|
|
return;
|
|
|
|
|
|
|
|
module->value.loaded = true;
|
2019-03-16 09:38:32 +01:00
|
|
|
Context empty_context{Context::EmptyContextFlag{}};
|
|
|
|
execute(module->value.commands, empty_context);
|
2019-03-12 18:34:30 +01:00
|
|
|
module->value.commands.clear();
|
2019-03-13 20:46:53 +01:00
|
|
|
|
2019-06-25 17:48:24 +02:00
|
|
|
context.hooks().run_hook(Hook::ModuleLoaded, module_name, context);
|
2019-03-12 18:34:30 +01:00
|
|
|
}
|
|
|
|
|
2013-07-28 16:40:02 +02:00
|
|
|
struct parse_error : runtime_error
|
|
|
|
{
|
2014-11-15 19:45:56 +01:00
|
|
|
parse_error(StringView error)
|
2015-03-31 00:06:02 +02:00
|
|
|
: runtime_error{format("parse error: {}", error)} {}
|
2013-07-28 16:40:02 +02:00
|
|
|
};
|
2012-11-21 13:56:52 +01:00
|
|
|
|
2018-02-15 11:23:12 +01:00
|
|
|
Codepoint Reader::operator*() const
|
2012-08-02 06:41:55 +02:00
|
|
|
{
|
2018-02-15 11:23:12 +01:00
|
|
|
kak_assert(pos < str.end());
|
|
|
|
return utf8::codepoint(pos, str.end());
|
|
|
|
}
|
2012-08-02 06:41:55 +02:00
|
|
|
|
2018-05-23 15:13:26 +02:00
|
|
|
Codepoint Reader::peek_next() const
|
|
|
|
{
|
|
|
|
return utf8::codepoint(utf8::next(pos, str.end()), str.end());
|
|
|
|
}
|
|
|
|
|
2018-02-15 11:23:12 +01:00
|
|
|
Reader& Reader::operator++()
|
2015-08-27 21:48:51 +02:00
|
|
|
{
|
2018-02-15 11:23:12 +01:00
|
|
|
kak_assert(pos < str.end());
|
|
|
|
if (*pos == '\n')
|
2019-12-23 22:34:24 +01:00
|
|
|
{
|
2018-02-15 11:23:12 +01:00
|
|
|
++line;
|
2019-12-23 22:34:24 +01:00
|
|
|
line_start = ++pos;
|
|
|
|
return *this;
|
|
|
|
}
|
2018-02-15 11:23:12 +01:00
|
|
|
utf8::to_next(pos, str.end());
|
|
|
|
return *this;
|
|
|
|
}
|
2018-02-03 23:21:15 +01:00
|
|
|
|
2019-12-23 22:34:24 +01:00
|
|
|
void Reader::next_byte()
|
|
|
|
{
|
|
|
|
kak_assert(pos < str.end());
|
|
|
|
if (*pos++ == '\n')
|
|
|
|
{
|
|
|
|
++line;
|
|
|
|
line_start = pos;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-15 11:23:12 +01:00
|
|
|
namespace
|
|
|
|
{
|
2015-08-27 21:48:51 +02:00
|
|
|
|
2018-02-03 23:21:15 +01:00
|
|
|
bool is_command_separator(Codepoint c)
|
2012-07-31 14:22:57 +02:00
|
|
|
{
|
|
|
|
return c == ';' or c == '\n';
|
|
|
|
}
|
|
|
|
|
Refactor command line parsing
Command line parsing now works as follow:
* Quoted strings ('...', "..." and %~...~ with '~' non nestable)
use 'doubling-up' for escaping their delimiter, if the delimiter
appears twice in a row, it is considered as part of the string and
represent one delimiter character. So 'abc''def' == "abc'def". No
other escaping takes place in those strings.
* Balanced strings (%{...}) do not support any kind of escaping, but
finds the matching closing delimiter by taking nesting into account.
So %{abc{def}} == "abc{def}".
* Non quoted words support escaping of `;` and whitespaces with `\`,
`%`, `'` and '"` can be escaped with `\` at the start of the word,
they do not need escaping (and will not be escaped) else where in
a word where they are treated literally. Any other use of '\' is a
literal '\'. So \%abc%\;\ def == "%abc%; def"
As discussed in #2046 this should make our command line syntax more
robust, provide a simple programmatic way to escape a string content
(s/<delim>/<delim><delim>/g), be well defined instead of ad-hoc
undocumented behaviour, and interact nicely with other common
escaping by avoiding escaping hell (:grep <regex> can in most case
be written with the regex unquoted).
2018-05-21 14:22:34 +02:00
|
|
|
struct QuotedResult
|
|
|
|
{
|
|
|
|
String content;
|
|
|
|
bool terminated;
|
|
|
|
};
|
|
|
|
|
|
|
|
QuotedResult parse_quoted(Reader& reader, Codepoint delimiter)
|
2013-06-27 20:07:26 +02:00
|
|
|
{
|
2015-08-27 21:48:51 +02:00
|
|
|
auto beg = reader.pos;
|
2013-06-27 20:07:26 +02:00
|
|
|
String str;
|
2015-08-27 21:48:51 +02:00
|
|
|
|
|
|
|
while (reader)
|
2013-06-27 20:07:26 +02:00
|
|
|
{
|
2018-02-03 23:21:15 +01:00
|
|
|
const Codepoint c = *reader;
|
Refactor command line parsing
Command line parsing now works as follow:
* Quoted strings ('...', "..." and %~...~ with '~' non nestable)
use 'doubling-up' for escaping their delimiter, if the delimiter
appears twice in a row, it is considered as part of the string and
represent one delimiter character. So 'abc''def' == "abc'def". No
other escaping takes place in those strings.
* Balanced strings (%{...}) do not support any kind of escaping, but
finds the matching closing delimiter by taking nesting into account.
So %{abc{def}} == "abc{def}".
* Non quoted words support escaping of `;` and whitespaces with `\`,
`%`, `'` and '"` can be escaped with `\` at the start of the word,
they do not need escaping (and will not be escaped) else where in
a word where they are treated literally. Any other use of '\' is a
literal '\'. So \%abc%\;\ def == "%abc%; def"
As discussed in #2046 this should make our command line syntax more
robust, provide a simple programmatic way to escape a string content
(s/<delim>/<delim><delim>/g), be well defined instead of ad-hoc
undocumented behaviour, and interact nicely with other common
escaping by avoiding escaping hell (:grep <regex> can in most case
be written with the regex unquoted).
2018-05-21 14:22:34 +02:00
|
|
|
if (c == delimiter)
|
2013-06-27 20:07:26 +02:00
|
|
|
{
|
2018-05-23 15:13:26 +02:00
|
|
|
if (reader.peek_next() != delimiter)
|
2015-07-25 09:56:27 +02:00
|
|
|
{
|
2018-05-23 15:13:26 +02:00
|
|
|
str += reader.substr_from(beg);
|
|
|
|
++reader;
|
Refactor command line parsing
Command line parsing now works as follow:
* Quoted strings ('...', "..." and %~...~ with '~' non nestable)
use 'doubling-up' for escaping their delimiter, if the delimiter
appears twice in a row, it is considered as part of the string and
represent one delimiter character. So 'abc''def' == "abc'def". No
other escaping takes place in those strings.
* Balanced strings (%{...}) do not support any kind of escaping, but
finds the matching closing delimiter by taking nesting into account.
So %{abc{def}} == "abc{def}".
* Non quoted words support escaping of `;` and whitespaces with `\`,
`%`, `'` and '"` can be escaped with `\` at the start of the word,
they do not need escaping (and will not be escaped) else where in
a word where they are treated literally. Any other use of '\' is a
literal '\'. So \%abc%\;\ def == "%abc%; def"
As discussed in #2046 this should make our command line syntax more
robust, provide a simple programmatic way to escape a string content
(s/<delim>/<delim><delim>/g), be well defined instead of ad-hoc
undocumented behaviour, and interact nicely with other common
escaping by avoiding escaping hell (:grep <regex> can in most case
be written with the regex unquoted).
2018-05-21 14:22:34 +02:00
|
|
|
return {str, true};
|
2018-05-23 15:13:26 +02:00
|
|
|
}
|
|
|
|
str += (++reader).substr_from(beg);
|
|
|
|
beg = reader.pos+1;
|
2013-06-27 20:07:26 +02:00
|
|
|
}
|
2015-08-27 21:48:51 +02:00
|
|
|
++reader;
|
2013-06-27 20:07:26 +02:00
|
|
|
}
|
2018-02-03 23:21:15 +01:00
|
|
|
if (beg < reader.str.end())
|
2015-08-27 21:48:51 +02:00
|
|
|
str += reader.substr_from(beg);
|
Refactor command line parsing
Command line parsing now works as follow:
* Quoted strings ('...', "..." and %~...~ with '~' non nestable)
use 'doubling-up' for escaping their delimiter, if the delimiter
appears twice in a row, it is considered as part of the string and
represent one delimiter character. So 'abc''def' == "abc'def". No
other escaping takes place in those strings.
* Balanced strings (%{...}) do not support any kind of escaping, but
finds the matching closing delimiter by taking nesting into account.
So %{abc{def}} == "abc{def}".
* Non quoted words support escaping of `;` and whitespaces with `\`,
`%`, `'` and '"` can be escaped with `\` at the start of the word,
they do not need escaping (and will not be escaped) else where in
a word where they are treated literally. Any other use of '\' is a
literal '\'. So \%abc%\;\ def == "%abc%; def"
As discussed in #2046 this should make our command line syntax more
robust, provide a simple programmatic way to escape a string content
(s/<delim>/<delim><delim>/g), be well defined instead of ad-hoc
undocumented behaviour, and interact nicely with other common
escaping by avoiding escaping hell (:grep <regex> can in most case
be written with the regex unquoted).
2018-05-21 14:22:34 +02:00
|
|
|
return {str, false};
|
2013-06-27 20:07:26 +02:00
|
|
|
}
|
|
|
|
|
2018-07-25 12:58:07 +02:00
|
|
|
QuotedResult parse_quoted_balanced(Reader& reader, char opening_delimiter,
|
|
|
|
char closing_delimiter)
|
2015-08-27 21:48:51 +02:00
|
|
|
{
|
2018-02-03 23:21:15 +01:00
|
|
|
kak_assert(utf8::codepoint(utf8::previous(reader.pos, reader.str.begin()),
|
|
|
|
reader.str.end()) == opening_delimiter);
|
2013-12-29 19:37:48 +01:00
|
|
|
int level = 0;
|
2015-08-27 21:48:51 +02:00
|
|
|
auto start = reader.pos;
|
|
|
|
while (reader)
|
2013-12-29 19:37:48 +01:00
|
|
|
{
|
2018-07-25 12:58:07 +02:00
|
|
|
const char c = *reader.pos;
|
2015-08-27 21:48:51 +02:00
|
|
|
if (c == opening_delimiter)
|
2013-12-29 19:37:48 +01:00
|
|
|
++level;
|
Refactor command line parsing
Command line parsing now works as follow:
* Quoted strings ('...', "..." and %~...~ with '~' non nestable)
use 'doubling-up' for escaping their delimiter, if the delimiter
appears twice in a row, it is considered as part of the string and
represent one delimiter character. So 'abc''def' == "abc'def". No
other escaping takes place in those strings.
* Balanced strings (%{...}) do not support any kind of escaping, but
finds the matching closing delimiter by taking nesting into account.
So %{abc{def}} == "abc{def}".
* Non quoted words support escaping of `;` and whitespaces with `\`,
`%`, `'` and '"` can be escaped with `\` at the start of the word,
they do not need escaping (and will not be escaped) else where in
a word where they are treated literally. Any other use of '\' is a
literal '\'. So \%abc%\;\ def == "%abc%; def"
As discussed in #2046 this should make our command line syntax more
robust, provide a simple programmatic way to escape a string content
(s/<delim>/<delim><delim>/g), be well defined instead of ad-hoc
undocumented behaviour, and interact nicely with other common
escaping by avoiding escaping hell (:grep <regex> can in most case
be written with the regex unquoted).
2018-05-21 14:22:34 +02:00
|
|
|
else if (c == closing_delimiter and level-- == 0)
|
|
|
|
{
|
|
|
|
auto content = reader.substr_from(start);
|
2019-12-23 22:34:24 +01:00
|
|
|
reader.next_byte();
|
2019-03-19 12:00:57 +01:00
|
|
|
return {String{String::NoCopy{}, content}, true};
|
Refactor command line parsing
Command line parsing now works as follow:
* Quoted strings ('...', "..." and %~...~ with '~' non nestable)
use 'doubling-up' for escaping their delimiter, if the delimiter
appears twice in a row, it is considered as part of the string and
represent one delimiter character. So 'abc''def' == "abc'def". No
other escaping takes place in those strings.
* Balanced strings (%{...}) do not support any kind of escaping, but
finds the matching closing delimiter by taking nesting into account.
So %{abc{def}} == "abc{def}".
* Non quoted words support escaping of `;` and whitespaces with `\`,
`%`, `'` and '"` can be escaped with `\` at the start of the word,
they do not need escaping (and will not be escaped) else where in
a word where they are treated literally. Any other use of '\' is a
literal '\'. So \%abc%\;\ def == "%abc%; def"
As discussed in #2046 this should make our command line syntax more
robust, provide a simple programmatic way to escape a string content
(s/<delim>/<delim><delim>/g), be well defined instead of ad-hoc
undocumented behaviour, and interact nicely with other common
escaping by avoiding escaping hell (:grep <regex> can in most case
be written with the regex unquoted).
2018-05-21 14:22:34 +02:00
|
|
|
}
|
2019-12-23 22:34:24 +01:00
|
|
|
reader.next_byte();
|
Refactor command line parsing
Command line parsing now works as follow:
* Quoted strings ('...', "..." and %~...~ with '~' non nestable)
use 'doubling-up' for escaping their delimiter, if the delimiter
appears twice in a row, it is considered as part of the string and
represent one delimiter character. So 'abc''def' == "abc'def". No
other escaping takes place in those strings.
* Balanced strings (%{...}) do not support any kind of escaping, but
finds the matching closing delimiter by taking nesting into account.
So %{abc{def}} == "abc{def}".
* Non quoted words support escaping of `;` and whitespaces with `\`,
`%`, `'` and '"` can be escaped with `\` at the start of the word,
they do not need escaping (and will not be escaped) else where in
a word where they are treated literally. Any other use of '\' is a
literal '\'. So \%abc%\;\ def == "%abc%; def"
As discussed in #2046 this should make our command line syntax more
robust, provide a simple programmatic way to escape a string content
(s/<delim>/<delim><delim>/g), be well defined instead of ad-hoc
undocumented behaviour, and interact nicely with other common
escaping by avoiding escaping hell (:grep <regex> can in most case
be written with the regex unquoted).
2018-05-21 14:22:34 +02:00
|
|
|
}
|
2019-03-19 12:00:57 +01:00
|
|
|
return {String{String::NoCopy{}, reader.substr_from(start)}, false};
|
Refactor command line parsing
Command line parsing now works as follow:
* Quoted strings ('...', "..." and %~...~ with '~' non nestable)
use 'doubling-up' for escaping their delimiter, if the delimiter
appears twice in a row, it is considered as part of the string and
represent one delimiter character. So 'abc''def' == "abc'def". No
other escaping takes place in those strings.
* Balanced strings (%{...}) do not support any kind of escaping, but
finds the matching closing delimiter by taking nesting into account.
So %{abc{def}} == "abc{def}".
* Non quoted words support escaping of `;` and whitespaces with `\`,
`%`, `'` and '"` can be escaped with `\` at the start of the word,
they do not need escaping (and will not be escaped) else where in
a word where they are treated literally. Any other use of '\' is a
literal '\'. So \%abc%\;\ def == "%abc%; def"
As discussed in #2046 this should make our command line syntax more
robust, provide a simple programmatic way to escape a string content
(s/<delim>/<delim><delim>/g), be well defined instead of ad-hoc
undocumented behaviour, and interact nicely with other common
escaping by avoiding escaping hell (:grep <regex> can in most case
be written with the regex unquoted).
2018-05-21 14:22:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
String parse_unquoted(Reader& reader)
|
|
|
|
{
|
|
|
|
auto beg = reader.pos;
|
|
|
|
String str;
|
|
|
|
|
|
|
|
while (reader)
|
|
|
|
{
|
2018-07-25 12:58:07 +02:00
|
|
|
const char c = *reader.pos;
|
Refactor command line parsing
Command line parsing now works as follow:
* Quoted strings ('...', "..." and %~...~ with '~' non nestable)
use 'doubling-up' for escaping their delimiter, if the delimiter
appears twice in a row, it is considered as part of the string and
represent one delimiter character. So 'abc''def' == "abc'def". No
other escaping takes place in those strings.
* Balanced strings (%{...}) do not support any kind of escaping, but
finds the matching closing delimiter by taking nesting into account.
So %{abc{def}} == "abc{def}".
* Non quoted words support escaping of `;` and whitespaces with `\`,
`%`, `'` and '"` can be escaped with `\` at the start of the word,
they do not need escaping (and will not be escaped) else where in
a word where they are treated literally. Any other use of '\' is a
literal '\'. So \%abc%\;\ def == "%abc%; def"
As discussed in #2046 this should make our command line syntax more
robust, provide a simple programmatic way to escape a string content
(s/<delim>/<delim><delim>/g), be well defined instead of ad-hoc
undocumented behaviour, and interact nicely with other common
escaping by avoiding escaping hell (:grep <regex> can in most case
be written with the regex unquoted).
2018-05-21 14:22:34 +02:00
|
|
|
if (is_command_separator(c) or is_horizontal_blank(c))
|
2013-12-29 19:37:48 +01:00
|
|
|
{
|
Refactor command line parsing
Command line parsing now works as follow:
* Quoted strings ('...', "..." and %~...~ with '~' non nestable)
use 'doubling-up' for escaping their delimiter, if the delimiter
appears twice in a row, it is considered as part of the string and
represent one delimiter character. So 'abc''def' == "abc'def". No
other escaping takes place in those strings.
* Balanced strings (%{...}) do not support any kind of escaping, but
finds the matching closing delimiter by taking nesting into account.
So %{abc{def}} == "abc{def}".
* Non quoted words support escaping of `;` and whitespaces with `\`,
`%`, `'` and '"` can be escaped with `\` at the start of the word,
they do not need escaping (and will not be escaped) else where in
a word where they are treated literally. Any other use of '\' is a
literal '\'. So \%abc%\;\ def == "%abc%; def"
As discussed in #2046 this should make our command line syntax more
robust, provide a simple programmatic way to escape a string content
(s/<delim>/<delim><delim>/g), be well defined instead of ad-hoc
undocumented behaviour, and interact nicely with other common
escaping by avoiding escaping hell (:grep <regex> can in most case
be written with the regex unquoted).
2018-05-21 14:22:34 +02:00
|
|
|
str += reader.substr_from(beg);
|
2018-07-25 12:58:07 +02:00
|
|
|
if (reader.pos != reader.str.begin() and *(reader.pos - 1) == '\\')
|
Refactor command line parsing
Command line parsing now works as follow:
* Quoted strings ('...', "..." and %~...~ with '~' non nestable)
use 'doubling-up' for escaping their delimiter, if the delimiter
appears twice in a row, it is considered as part of the string and
represent one delimiter character. So 'abc''def' == "abc'def". No
other escaping takes place in those strings.
* Balanced strings (%{...}) do not support any kind of escaping, but
finds the matching closing delimiter by taking nesting into account.
So %{abc{def}} == "abc{def}".
* Non quoted words support escaping of `;` and whitespaces with `\`,
`%`, `'` and '"` can be escaped with `\` at the start of the word,
they do not need escaping (and will not be escaped) else where in
a word where they are treated literally. Any other use of '\' is a
literal '\'. So \%abc%\;\ def == "%abc%; def"
As discussed in #2046 this should make our command line syntax more
robust, provide a simple programmatic way to escape a string content
(s/<delim>/<delim><delim>/g), be well defined instead of ad-hoc
undocumented behaviour, and interact nicely with other common
escaping by avoiding escaping hell (:grep <regex> can in most case
be written with the regex unquoted).
2018-05-21 14:22:34 +02:00
|
|
|
{
|
|
|
|
str.back() = c;
|
|
|
|
beg = reader.pos+1;
|
|
|
|
}
|
2013-12-29 19:37:48 +01:00
|
|
|
else
|
Refactor command line parsing
Command line parsing now works as follow:
* Quoted strings ('...', "..." and %~...~ with '~' non nestable)
use 'doubling-up' for escaping their delimiter, if the delimiter
appears twice in a row, it is considered as part of the string and
represent one delimiter character. So 'abc''def' == "abc'def". No
other escaping takes place in those strings.
* Balanced strings (%{...}) do not support any kind of escaping, but
finds the matching closing delimiter by taking nesting into account.
So %{abc{def}} == "abc{def}".
* Non quoted words support escaping of `;` and whitespaces with `\`,
`%`, `'` and '"` can be escaped with `\` at the start of the word,
they do not need escaping (and will not be escaped) else where in
a word where they are treated literally. Any other use of '\' is a
literal '\'. So \%abc%\;\ def == "%abc%; def"
As discussed in #2046 this should make our command line syntax more
robust, provide a simple programmatic way to escape a string content
(s/<delim>/<delim><delim>/g), be well defined instead of ad-hoc
undocumented behaviour, and interact nicely with other common
escaping by avoiding escaping hell (:grep <regex> can in most case
be written with the regex unquoted).
2018-05-21 14:22:34 +02:00
|
|
|
return str;
|
2013-12-29 19:37:48 +01:00
|
|
|
}
|
2019-12-23 22:34:24 +01:00
|
|
|
reader.next_byte();
|
2013-12-29 19:37:48 +01:00
|
|
|
}
|
Refactor command line parsing
Command line parsing now works as follow:
* Quoted strings ('...', "..." and %~...~ with '~' non nestable)
use 'doubling-up' for escaping their delimiter, if the delimiter
appears twice in a row, it is considered as part of the string and
represent one delimiter character. So 'abc''def' == "abc'def". No
other escaping takes place in those strings.
* Balanced strings (%{...}) do not support any kind of escaping, but
finds the matching closing delimiter by taking nesting into account.
So %{abc{def}} == "abc{def}".
* Non quoted words support escaping of `;` and whitespaces with `\`,
`%`, `'` and '"` can be escaped with `\` at the start of the word,
they do not need escaping (and will not be escaped) else where in
a word where they are treated literally. Any other use of '\' is a
literal '\'. So \%abc%\;\ def == "%abc%; def"
As discussed in #2046 this should make our command line syntax more
robust, provide a simple programmatic way to escape a string content
(s/<delim>/<delim><delim>/g), be well defined instead of ad-hoc
undocumented behaviour, and interact nicely with other common
escaping by avoiding escaping hell (:grep <regex> can in most case
be written with the regex unquoted).
2018-05-21 14:22:34 +02:00
|
|
|
if (beg < reader.str.end())
|
|
|
|
str += reader.substr_from(beg);
|
|
|
|
return str;
|
2013-12-29 19:37:48 +01:00
|
|
|
}
|
|
|
|
|
2018-02-15 11:23:12 +01:00
|
|
|
Token::Type token_type(StringView type_name, bool throw_on_invalid)
|
2013-12-29 19:37:48 +01:00
|
|
|
{
|
|
|
|
if (type_name == "")
|
2015-09-20 12:19:10 +02:00
|
|
|
return Token::Type::RawQuoted;
|
2013-12-29 19:37:48 +01:00
|
|
|
else if (type_name == "sh")
|
|
|
|
return Token::Type::ShellExpand;
|
|
|
|
else if (type_name == "reg")
|
|
|
|
return Token::Type::RegisterExpand;
|
|
|
|
else if (type_name == "opt")
|
|
|
|
return Token::Type::OptionExpand;
|
2014-06-18 20:28:48 +02:00
|
|
|
else if (type_name == "val")
|
|
|
|
return Token::Type::ValExpand;
|
2015-12-01 21:07:14 +01:00
|
|
|
else if (type_name == "arg")
|
|
|
|
return Token::Type::ArgExpand;
|
2019-04-07 01:43:40 +02:00
|
|
|
else if (type_name == "file")
|
|
|
|
return Token::Type::FileExpand;
|
2014-02-27 07:41:58 +01:00
|
|
|
else if (throw_on_invalid)
|
2017-06-07 20:01:26 +02:00
|
|
|
throw parse_error{format("unknown expand '{}'", type_name)};
|
2014-02-27 07:41:58 +01:00
|
|
|
else
|
2015-09-20 12:19:10 +02:00
|
|
|
return Token::Type::RawQuoted;
|
2013-12-29 19:37:48 +01:00
|
|
|
}
|
|
|
|
|
2015-08-27 21:48:51 +02:00
|
|
|
void skip_blanks_and_comments(Reader& reader)
|
2013-12-29 19:37:48 +01:00
|
|
|
{
|
2015-08-27 21:48:51 +02:00
|
|
|
while (reader)
|
2013-12-29 19:37:48 +01:00
|
|
|
{
|
2018-02-03 23:21:15 +01:00
|
|
|
const Codepoint c = *reader;
|
2015-08-27 21:48:51 +02:00
|
|
|
if (is_horizontal_blank(c))
|
|
|
|
++reader;
|
2018-02-15 11:23:12 +01:00
|
|
|
else if (c == '\\' and reader.pos + 1 != reader.str.end() and
|
|
|
|
*(reader.pos + 1) == '\n')
|
2015-08-27 21:48:51 +02:00
|
|
|
++(++reader);
|
|
|
|
else if (c == '#')
|
2013-12-29 19:37:48 +01:00
|
|
|
{
|
2017-05-25 09:30:49 +02:00
|
|
|
while (reader and *reader != '\n')
|
|
|
|
++reader;
|
2013-12-29 19:37:48 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-15 11:23:12 +01:00
|
|
|
Token parse_percent_token(Reader& reader, bool throw_on_unterminated)
|
2014-04-04 00:57:14 +02:00
|
|
|
{
|
2018-03-17 18:58:15 +01:00
|
|
|
kak_assert(*reader == '%');
|
2015-08-27 21:48:51 +02:00
|
|
|
++reader;
|
2018-05-06 23:29:52 +02:00
|
|
|
|
2018-02-03 23:21:15 +01:00
|
|
|
const auto type_start = reader.pos;
|
2018-02-06 10:29:08 +01:00
|
|
|
while (reader and iswalpha(*reader))
|
2015-08-27 21:48:51 +02:00
|
|
|
++reader;
|
|
|
|
StringView type_name = reader.substr_from(type_start);
|
2014-04-04 00:57:14 +02:00
|
|
|
|
2017-06-07 14:09:45 +02:00
|
|
|
if (not reader or is_blank(*reader))
|
|
|
|
{
|
|
|
|
if (throw_on_unterminated)
|
|
|
|
throw parse_error{format("expected a string delimiter after '%{}'",
|
|
|
|
type_name)};
|
2017-04-19 22:52:27 +02:00
|
|
|
return {};
|
2017-06-07 14:09:45 +02:00
|
|
|
}
|
2014-04-04 00:57:14 +02:00
|
|
|
|
2018-02-15 11:23:12 +01:00
|
|
|
Token::Type type = token_type(type_name, throw_on_unterminated);
|
2015-08-27 22:36:49 +02:00
|
|
|
|
2018-02-03 23:21:15 +01:00
|
|
|
constexpr struct CharPair { Codepoint opening; Codepoint closing; } matching_pairs[] = {
|
2014-04-04 00:57:14 +02:00
|
|
|
{ '(', ')' }, { '[', ']' }, { '{', '}' }, { '<', '>' }
|
|
|
|
};
|
|
|
|
|
2018-02-03 23:21:15 +01:00
|
|
|
const Codepoint opening_delimiter = *reader;
|
|
|
|
auto coord = reader.coord();
|
2015-08-27 21:48:51 +02:00
|
|
|
++reader;
|
|
|
|
auto start = reader.pos;
|
2014-04-04 00:57:14 +02:00
|
|
|
|
2015-08-27 22:36:49 +02:00
|
|
|
auto it = find_if(matching_pairs, [opening_delimiter](const CharPair& cp)
|
|
|
|
{ return opening_delimiter == cp.opening; });
|
|
|
|
|
2018-02-03 23:21:15 +01:00
|
|
|
const auto str_beg = reader.str.begin();
|
2015-08-27 22:36:49 +02:00
|
|
|
if (it != std::end(matching_pairs))
|
2014-04-04 00:57:14 +02:00
|
|
|
{
|
2018-02-03 23:21:15 +01:00
|
|
|
const Codepoint closing_delimiter = it->closing;
|
Refactor command line parsing
Command line parsing now works as follow:
* Quoted strings ('...', "..." and %~...~ with '~' non nestable)
use 'doubling-up' for escaping their delimiter, if the delimiter
appears twice in a row, it is considered as part of the string and
represent one delimiter character. So 'abc''def' == "abc'def". No
other escaping takes place in those strings.
* Balanced strings (%{...}) do not support any kind of escaping, but
finds the matching closing delimiter by taking nesting into account.
So %{abc{def}} == "abc{def}".
* Non quoted words support escaping of `;` and whitespaces with `\`,
`%`, `'` and '"` can be escaped with `\` at the start of the word,
they do not need escaping (and will not be escaped) else where in
a word where they are treated literally. Any other use of '\' is a
literal '\'. So \%abc%\;\ def == "%abc%; def"
As discussed in #2046 this should make our command line syntax more
robust, provide a simple programmatic way to escape a string content
(s/<delim>/<delim><delim>/g), be well defined instead of ad-hoc
undocumented behaviour, and interact nicely with other common
escaping by avoiding escaping hell (:grep <regex> can in most case
be written with the regex unquoted).
2018-05-21 14:22:34 +02:00
|
|
|
auto quoted = parse_quoted_balanced(reader, opening_delimiter, closing_delimiter);
|
|
|
|
if (throw_on_unterminated and not quoted.terminated)
|
2015-08-27 21:48:51 +02:00
|
|
|
throw parse_error{format("{}:{}: unterminated string '%{}{}...{}'",
|
|
|
|
coord.line, coord.column, type_name,
|
|
|
|
opening_delimiter, closing_delimiter)};
|
|
|
|
|
Refactor command line parsing
Command line parsing now works as follow:
* Quoted strings ('...', "..." and %~...~ with '~' non nestable)
use 'doubling-up' for escaping their delimiter, if the delimiter
appears twice in a row, it is considered as part of the string and
represent one delimiter character. So 'abc''def' == "abc'def". No
other escaping takes place in those strings.
* Balanced strings (%{...}) do not support any kind of escaping, but
finds the matching closing delimiter by taking nesting into account.
So %{abc{def}} == "abc{def}".
* Non quoted words support escaping of `;` and whitespaces with `\`,
`%`, `'` and '"` can be escaped with `\` at the start of the word,
they do not need escaping (and will not be escaped) else where in
a word where they are treated literally. Any other use of '\' is a
literal '\'. So \%abc%\;\ def == "%abc%; def"
As discussed in #2046 this should make our command line syntax more
robust, provide a simple programmatic way to escape a string content
(s/<delim>/<delim><delim>/g), be well defined instead of ad-hoc
undocumented behaviour, and interact nicely with other common
escaping by avoiding escaping hell (:grep <regex> can in most case
be written with the regex unquoted).
2018-05-21 14:22:34 +02:00
|
|
|
return {type, start - str_beg, coord, std::move(quoted.content)};
|
2014-04-04 00:57:14 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
Refactor command line parsing
Command line parsing now works as follow:
* Quoted strings ('...', "..." and %~...~ with '~' non nestable)
use 'doubling-up' for escaping their delimiter, if the delimiter
appears twice in a row, it is considered as part of the string and
represent one delimiter character. So 'abc''def' == "abc'def". No
other escaping takes place in those strings.
* Balanced strings (%{...}) do not support any kind of escaping, but
finds the matching closing delimiter by taking nesting into account.
So %{abc{def}} == "abc{def}".
* Non quoted words support escaping of `;` and whitespaces with `\`,
`%`, `'` and '"` can be escaped with `\` at the start of the word,
they do not need escaping (and will not be escaped) else where in
a word where they are treated literally. Any other use of '\' is a
literal '\'. So \%abc%\;\ def == "%abc%; def"
As discussed in #2046 this should make our command line syntax more
robust, provide a simple programmatic way to escape a string content
(s/<delim>/<delim><delim>/g), be well defined instead of ad-hoc
undocumented behaviour, and interact nicely with other common
escaping by avoiding escaping hell (:grep <regex> can in most case
be written with the regex unquoted).
2018-05-21 14:22:34 +02:00
|
|
|
auto quoted = parse_quoted(reader, opening_delimiter);
|
2015-08-27 21:48:51 +02:00
|
|
|
|
Refactor command line parsing
Command line parsing now works as follow:
* Quoted strings ('...', "..." and %~...~ with '~' non nestable)
use 'doubling-up' for escaping their delimiter, if the delimiter
appears twice in a row, it is considered as part of the string and
represent one delimiter character. So 'abc''def' == "abc'def". No
other escaping takes place in those strings.
* Balanced strings (%{...}) do not support any kind of escaping, but
finds the matching closing delimiter by taking nesting into account.
So %{abc{def}} == "abc{def}".
* Non quoted words support escaping of `;` and whitespaces with `\`,
`%`, `'` and '"` can be escaped with `\` at the start of the word,
they do not need escaping (and will not be escaped) else where in
a word where they are treated literally. Any other use of '\' is a
literal '\'. So \%abc%\;\ def == "%abc%; def"
As discussed in #2046 this should make our command line syntax more
robust, provide a simple programmatic way to escape a string content
(s/<delim>/<delim><delim>/g), be well defined instead of ad-hoc
undocumented behaviour, and interact nicely with other common
escaping by avoiding escaping hell (:grep <regex> can in most case
be written with the regex unquoted).
2018-05-21 14:22:34 +02:00
|
|
|
if (throw_on_unterminated and not quoted.terminated)
|
2015-08-27 21:48:51 +02:00
|
|
|
throw parse_error{format("{}:{}: unterminated string '%{}{}...{}'",
|
|
|
|
coord.line, coord.column, type_name,
|
|
|
|
opening_delimiter, opening_delimiter)};
|
|
|
|
|
Refactor command line parsing
Command line parsing now works as follow:
* Quoted strings ('...', "..." and %~...~ with '~' non nestable)
use 'doubling-up' for escaping their delimiter, if the delimiter
appears twice in a row, it is considered as part of the string and
represent one delimiter character. So 'abc''def' == "abc'def". No
other escaping takes place in those strings.
* Balanced strings (%{...}) do not support any kind of escaping, but
finds the matching closing delimiter by taking nesting into account.
So %{abc{def}} == "abc{def}".
* Non quoted words support escaping of `;` and whitespaces with `\`,
`%`, `'` and '"` can be escaped with `\` at the start of the word,
they do not need escaping (and will not be escaped) else where in
a word where they are treated literally. Any other use of '\' is a
literal '\'. So \%abc%\;\ def == "%abc%; def"
As discussed in #2046 this should make our command line syntax more
robust, provide a simple programmatic way to escape a string content
(s/<delim>/<delim><delim>/g), be well defined instead of ad-hoc
undocumented behaviour, and interact nicely with other common
escaping by avoiding escaping hell (:grep <regex> can in most case
be written with the regex unquoted).
2018-05-21 14:22:34 +02:00
|
|
|
return {type, start - str_beg, coord, std::move(quoted.content)};
|
2014-04-04 00:57:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-02 21:49:26 +01:00
|
|
|
auto expand_option(const Option& opt, std::true_type)
|
2015-12-12 07:50:58 +01:00
|
|
|
{
|
2019-06-18 14:18:17 +02:00
|
|
|
return opt.get_as_string(Quoting::Raw);
|
2018-05-30 15:23:38 +02:00
|
|
|
}
|
|
|
|
|
2019-11-02 21:49:26 +01:00
|
|
|
auto expand_option(const Option& opt, std::false_type)
|
2018-05-30 15:23:38 +02:00
|
|
|
{
|
|
|
|
return opt.get_as_strings();
|
|
|
|
}
|
|
|
|
|
2018-06-03 06:46:44 +02:00
|
|
|
auto expand_register(StringView reg, const Context& context, std::true_type)
|
|
|
|
{
|
2018-06-03 13:18:55 +02:00
|
|
|
return context.main_sel_register_value(reg).str();
|
2018-06-03 06:46:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
auto expand_register(StringView reg, const Context& context, std::false_type)
|
|
|
|
{
|
|
|
|
return RegisterManager::instance()[reg].get(context) | gather<Vector<String>>();
|
|
|
|
}
|
|
|
|
|
2018-05-30 15:23:38 +02:00
|
|
|
String expand_arobase(ConstArrayView<String> params, std::true_type)
|
|
|
|
{
|
|
|
|
return join(params, ' ', false);
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector<String> expand_arobase(ConstArrayView<String> params, std::false_type)
|
|
|
|
{
|
|
|
|
return {params.begin(), params.end()};
|
|
|
|
}
|
|
|
|
|
|
|
|
template<bool single>
|
|
|
|
std::conditional_t<single, String, Vector<String>>
|
|
|
|
expand_token(const Token& token, const Context& context, const ShellContext& shell_context)
|
|
|
|
{
|
|
|
|
using IsSingle = std::integral_constant<bool, single>;
|
2017-09-01 12:09:34 +02:00
|
|
|
auto& content = token.content;
|
|
|
|
switch (token.type)
|
2015-12-12 07:50:58 +01:00
|
|
|
{
|
|
|
|
case Token::Type::ShellExpand:
|
2016-06-19 18:01:56 +02:00
|
|
|
{
|
|
|
|
auto str = ShellManager::instance().eval(
|
|
|
|
content, context, {}, ShellManager::Flags::WaitForStdout,
|
|
|
|
shell_context).first;
|
|
|
|
|
|
|
|
int trailing_eol_count = 0;
|
|
|
|
for (auto c : str | reverse())
|
|
|
|
{
|
|
|
|
if (c != '\n')
|
|
|
|
break;
|
|
|
|
++trailing_eol_count;
|
|
|
|
}
|
|
|
|
str.resize(str.length() - trailing_eol_count, 0);
|
2018-05-30 15:23:38 +02:00
|
|
|
return {str};
|
2016-06-19 18:01:56 +02:00
|
|
|
}
|
2015-12-12 07:50:58 +01:00
|
|
|
case Token::Type::RegisterExpand:
|
2018-06-03 06:46:44 +02:00
|
|
|
return expand_register(content, context, IsSingle{});
|
2015-12-12 07:50:58 +01:00
|
|
|
case Token::Type::OptionExpand:
|
2018-05-30 15:23:38 +02:00
|
|
|
return expand_option(context.options()[content], IsSingle{});
|
2015-12-12 07:50:58 +01:00
|
|
|
case Token::Type::ValExpand:
|
|
|
|
{
|
|
|
|
auto it = shell_context.env_vars.find(content);
|
|
|
|
if (it != shell_context.env_vars.end())
|
2018-05-30 15:23:38 +02:00
|
|
|
return {it->value};
|
2020-03-01 03:28:06 +01:00
|
|
|
if constexpr (single)
|
|
|
|
return join(ShellManager::instance().get_val(content, context), false, ' ');
|
|
|
|
else
|
|
|
|
return ShellManager::instance().get_val(content, context);
|
2015-12-12 07:50:58 +01:00
|
|
|
}
|
|
|
|
case Token::Type::ArgExpand:
|
|
|
|
{
|
|
|
|
auto& params = shell_context.params;
|
|
|
|
if (content == '@')
|
2018-05-30 15:23:38 +02:00
|
|
|
return expand_arobase(params, IsSingle{});
|
2015-12-12 07:50:58 +01:00
|
|
|
|
|
|
|
const int arg = str_to_int(content)-1;
|
|
|
|
if (arg < 0)
|
|
|
|
throw runtime_error("invalid argument index");
|
2018-05-30 15:23:38 +02:00
|
|
|
return {arg < params.size() ? params[arg] : String{}};
|
2015-12-12 07:50:58 +01:00
|
|
|
}
|
2019-04-07 01:43:40 +02:00
|
|
|
case Token::Type::FileExpand:
|
|
|
|
return {read_file(content)};
|
2015-12-12 07:50:58 +01:00
|
|
|
case Token::Type::RawEval:
|
2018-05-30 15:23:38 +02:00
|
|
|
return {expand(content, context, shell_context)};
|
2015-12-12 07:50:58 +01:00
|
|
|
case Token::Type::Raw:
|
|
|
|
case Token::Type::RawQuoted:
|
2018-05-30 15:23:38 +02:00
|
|
|
return {content};
|
2015-12-12 07:50:58 +01:00
|
|
|
default: kak_assert(false);
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-02-15 11:23:12 +01:00
|
|
|
CommandParser::CommandParser(StringView command_line) : m_reader{command_line} {}
|
2011-09-07 20:16:56 +02:00
|
|
|
|
2018-02-15 11:23:12 +01:00
|
|
|
Optional<Token> CommandParser::read_token(bool throw_on_unterminated)
|
|
|
|
{
|
|
|
|
skip_blanks_and_comments(m_reader);
|
|
|
|
if (not m_reader)
|
|
|
|
return {};
|
2015-08-27 21:48:51 +02:00
|
|
|
|
2018-02-15 11:23:12 +01:00
|
|
|
const StringView line = m_reader.str;
|
|
|
|
const char* start = m_reader.pos;
|
|
|
|
auto coord = m_reader.coord();
|
2011-11-26 18:20:02 +01:00
|
|
|
|
2018-02-15 11:23:12 +01:00
|
|
|
const Codepoint c = *m_reader;
|
|
|
|
if (c == '"' or c == '\'')
|
|
|
|
{
|
|
|
|
start = (++m_reader).pos;
|
Refactor command line parsing
Command line parsing now works as follow:
* Quoted strings ('...', "..." and %~...~ with '~' non nestable)
use 'doubling-up' for escaping their delimiter, if the delimiter
appears twice in a row, it is considered as part of the string and
represent one delimiter character. So 'abc''def' == "abc'def". No
other escaping takes place in those strings.
* Balanced strings (%{...}) do not support any kind of escaping, but
finds the matching closing delimiter by taking nesting into account.
So %{abc{def}} == "abc{def}".
* Non quoted words support escaping of `;` and whitespaces with `\`,
`%`, `'` and '"` can be escaped with `\` at the start of the word,
they do not need escaping (and will not be escaped) else where in
a word where they are treated literally. Any other use of '\' is a
literal '\'. So \%abc%\;\ def == "%abc%; def"
As discussed in #2046 this should make our command line syntax more
robust, provide a simple programmatic way to escape a string content
(s/<delim>/<delim><delim>/g), be well defined instead of ad-hoc
undocumented behaviour, and interact nicely with other common
escaping by avoiding escaping hell (:grep <regex> can in most case
be written with the regex unquoted).
2018-05-21 14:22:34 +02:00
|
|
|
QuotedResult quoted = parse_quoted(m_reader, c);
|
|
|
|
if (throw_on_unterminated and not quoted.terminated)
|
2018-02-15 11:23:12 +01:00
|
|
|
throw parse_error{format("unterminated string {0}...{0}", c)};
|
|
|
|
return Token{c == '"' ? Token::Type::RawEval
|
|
|
|
: Token::Type::RawQuoted,
|
Refactor command line parsing
Command line parsing now works as follow:
* Quoted strings ('...', "..." and %~...~ with '~' non nestable)
use 'doubling-up' for escaping their delimiter, if the delimiter
appears twice in a row, it is considered as part of the string and
represent one delimiter character. So 'abc''def' == "abc'def". No
other escaping takes place in those strings.
* Balanced strings (%{...}) do not support any kind of escaping, but
finds the matching closing delimiter by taking nesting into account.
So %{abc{def}} == "abc{def}".
* Non quoted words support escaping of `;` and whitespaces with `\`,
`%`, `'` and '"` can be escaped with `\` at the start of the word,
they do not need escaping (and will not be escaped) else where in
a word where they are treated literally. Any other use of '\' is a
literal '\'. So \%abc%\;\ def == "%abc%; def"
As discussed in #2046 this should make our command line syntax more
robust, provide a simple programmatic way to escape a string content
(s/<delim>/<delim><delim>/g), be well defined instead of ad-hoc
undocumented behaviour, and interact nicely with other common
escaping by avoiding escaping hell (:grep <regex> can in most case
be written with the regex unquoted).
2018-05-21 14:22:34 +02:00
|
|
|
start - line.begin(), coord, std::move(quoted.content)};
|
2018-02-15 11:23:12 +01:00
|
|
|
}
|
|
|
|
else if (c == '%')
|
|
|
|
{
|
|
|
|
auto token = parse_percent_token(m_reader, throw_on_unterminated);
|
|
|
|
return token;
|
2011-09-07 20:16:56 +02:00
|
|
|
}
|
2018-02-15 11:23:12 +01:00
|
|
|
else if (is_command_separator(*m_reader))
|
|
|
|
{
|
|
|
|
++m_reader;
|
|
|
|
return Token{Token::Type::CommandSeparator,
|
|
|
|
m_reader.pos - line.begin(), coord, {}};
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
Refactor command line parsing
Command line parsing now works as follow:
* Quoted strings ('...', "..." and %~...~ with '~' non nestable)
use 'doubling-up' for escaping their delimiter, if the delimiter
appears twice in a row, it is considered as part of the string and
represent one delimiter character. So 'abc''def' == "abc'def". No
other escaping takes place in those strings.
* Balanced strings (%{...}) do not support any kind of escaping, but
finds the matching closing delimiter by taking nesting into account.
So %{abc{def}} == "abc{def}".
* Non quoted words support escaping of `;` and whitespaces with `\`,
`%`, `'` and '"` can be escaped with `\` at the start of the word,
they do not need escaping (and will not be escaped) else where in
a word where they are treated literally. Any other use of '\' is a
literal '\'. So \%abc%\;\ def == "%abc%; def"
As discussed in #2046 this should make our command line syntax more
robust, provide a simple programmatic way to escape a string content
(s/<delim>/<delim><delim>/g), be well defined instead of ad-hoc
undocumented behaviour, and interact nicely with other common
escaping by avoiding escaping hell (:grep <regex> can in most case
be written with the regex unquoted).
2018-05-21 14:22:34 +02:00
|
|
|
if (c == '\\')
|
|
|
|
{
|
2018-05-23 15:13:26 +02:00
|
|
|
auto next = m_reader.peek_next();
|
Refactor command line parsing
Command line parsing now works as follow:
* Quoted strings ('...', "..." and %~...~ with '~' non nestable)
use 'doubling-up' for escaping their delimiter, if the delimiter
appears twice in a row, it is considered as part of the string and
represent one delimiter character. So 'abc''def' == "abc'def". No
other escaping takes place in those strings.
* Balanced strings (%{...}) do not support any kind of escaping, but
finds the matching closing delimiter by taking nesting into account.
So %{abc{def}} == "abc{def}".
* Non quoted words support escaping of `;` and whitespaces with `\`,
`%`, `'` and '"` can be escaped with `\` at the start of the word,
they do not need escaping (and will not be escaped) else where in
a word where they are treated literally. Any other use of '\' is a
literal '\'. So \%abc%\;\ def == "%abc%; def"
As discussed in #2046 this should make our command line syntax more
robust, provide a simple programmatic way to escape a string content
(s/<delim>/<delim><delim>/g), be well defined instead of ad-hoc
undocumented behaviour, and interact nicely with other common
escaping by avoiding escaping hell (:grep <regex> can in most case
be written with the regex unquoted).
2018-05-21 14:22:34 +02:00
|
|
|
if (next == '%' or next == '\'' or next == '"')
|
|
|
|
++m_reader;
|
|
|
|
}
|
2018-02-15 11:23:12 +01:00
|
|
|
return Token{Token::Type::Raw, start - line.begin(),
|
Refactor command line parsing
Command line parsing now works as follow:
* Quoted strings ('...', "..." and %~...~ with '~' non nestable)
use 'doubling-up' for escaping their delimiter, if the delimiter
appears twice in a row, it is considered as part of the string and
represent one delimiter character. So 'abc''def' == "abc'def". No
other escaping takes place in those strings.
* Balanced strings (%{...}) do not support any kind of escaping, but
finds the matching closing delimiter by taking nesting into account.
So %{abc{def}} == "abc{def}".
* Non quoted words support escaping of `;` and whitespaces with `\`,
`%`, `'` and '"` can be escaped with `\` at the start of the word,
they do not need escaping (and will not be escaped) else where in
a word where they are treated literally. Any other use of '\' is a
literal '\'. So \%abc%\;\ def == "%abc%; def"
As discussed in #2046 this should make our command line syntax more
robust, provide a simple programmatic way to escape a string content
(s/<delim>/<delim><delim>/g), be well defined instead of ad-hoc
undocumented behaviour, and interact nicely with other common
escaping by avoiding escaping hell (:grep <regex> can in most case
be written with the regex unquoted).
2018-05-21 14:22:34 +02:00
|
|
|
coord, parse_unquoted(m_reader)};
|
2018-02-15 11:23:12 +01:00
|
|
|
}
|
|
|
|
return {};
|
2011-11-26 19:32:57 +01:00
|
|
|
}
|
|
|
|
|
2016-12-07 21:07:32 +01:00
|
|
|
template<typename Postprocess>
|
|
|
|
String expand_impl(StringView str, const Context& context,
|
|
|
|
const ShellContext& shell_context,
|
|
|
|
Postprocess postprocess)
|
2015-05-04 18:08:57 +02:00
|
|
|
{
|
2015-08-27 21:48:51 +02:00
|
|
|
Reader reader{str};
|
2015-05-04 18:08:57 +02:00
|
|
|
String res;
|
2018-02-03 23:21:15 +01:00
|
|
|
auto beg = str.begin();
|
2015-08-27 21:48:51 +02:00
|
|
|
while (reader)
|
2015-05-04 18:08:57 +02:00
|
|
|
{
|
2018-02-03 23:21:15 +01:00
|
|
|
Codepoint c = *reader;
|
2018-05-23 15:13:26 +02:00
|
|
|
if (c == '%')
|
2015-05-04 18:08:57 +02:00
|
|
|
{
|
2018-05-23 15:13:26 +02:00
|
|
|
if (reader.peek_next() == '%')
|
|
|
|
{
|
|
|
|
res += (++reader).substr_from(beg);
|
|
|
|
beg = (++reader).pos;
|
|
|
|
}
|
|
|
|
else
|
2015-07-25 09:56:27 +02:00
|
|
|
{
|
2015-08-27 21:48:51 +02:00
|
|
|
res += reader.substr_from(beg);
|
2018-05-30 15:23:38 +02:00
|
|
|
res += postprocess(expand_token<true>(parse_percent_token(reader, true), context, shell_context));
|
Refactor command line parsing
Command line parsing now works as follow:
* Quoted strings ('...', "..." and %~...~ with '~' non nestable)
use 'doubling-up' for escaping their delimiter, if the delimiter
appears twice in a row, it is considered as part of the string and
represent one delimiter character. So 'abc''def' == "abc'def". No
other escaping takes place in those strings.
* Balanced strings (%{...}) do not support any kind of escaping, but
finds the matching closing delimiter by taking nesting into account.
So %{abc{def}} == "abc{def}".
* Non quoted words support escaping of `;` and whitespaces with `\`,
`%`, `'` and '"` can be escaped with `\` at the start of the word,
they do not need escaping (and will not be escaped) else where in
a word where they are treated literally. Any other use of '\' is a
literal '\'. So \%abc%\;\ def == "%abc%; def"
As discussed in #2046 this should make our command line syntax more
robust, provide a simple programmatic way to escape a string content
(s/<delim>/<delim><delim>/g), be well defined instead of ad-hoc
undocumented behaviour, and interact nicely with other common
escaping by avoiding escaping hell (:grep <regex> can in most case
be written with the regex unquoted).
2018-05-21 14:22:34 +02:00
|
|
|
beg = reader.pos;
|
2015-07-25 09:56:27 +02:00
|
|
|
}
|
2015-05-04 18:08:57 +02:00
|
|
|
}
|
|
|
|
else
|
2015-08-27 21:48:51 +02:00
|
|
|
++reader;
|
2015-05-04 18:08:57 +02:00
|
|
|
}
|
2015-08-27 21:48:51 +02:00
|
|
|
res += reader.substr_from(beg);
|
2015-05-04 18:08:57 +02:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2016-12-07 21:07:32 +01:00
|
|
|
String expand(StringView str, const Context& context,
|
|
|
|
const ShellContext& shell_context)
|
|
|
|
{
|
2017-01-09 00:26:15 +01:00
|
|
|
return expand_impl(str, context, shell_context, [](String s){ return s; });
|
2016-12-07 21:07:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
String expand(StringView str, const Context& context,
|
|
|
|
const ShellContext& shell_context,
|
2019-09-01 05:03:01 +02:00
|
|
|
const FunctionRef<String (String)>& postprocess)
|
2016-12-07 21:07:32 +01:00
|
|
|
{
|
2019-09-01 05:03:01 +02:00
|
|
|
return expand_impl(str, context, shell_context, postprocess);
|
2016-12-07 21:07:32 +01:00
|
|
|
}
|
|
|
|
|
2012-08-01 14:27:34 +02:00
|
|
|
struct command_not_found : runtime_error
|
|
|
|
{
|
2017-11-04 09:00:34 +01:00
|
|
|
command_not_found(StringView name)
|
2018-04-06 16:56:53 +02:00
|
|
|
: runtime_error(format("no such command: '{}'", name)) {}
|
2012-08-01 14:27:34 +02:00
|
|
|
};
|
|
|
|
|
2014-04-08 20:54:32 +02:00
|
|
|
CommandManager::CommandMap::const_iterator
|
2017-04-10 22:33:20 +02:00
|
|
|
CommandManager::find_command(const Context& context, StringView name) const
|
2013-12-24 03:06:22 +01:00
|
|
|
{
|
2014-10-30 00:22:54 +01:00
|
|
|
auto alias = context.aliases()[name];
|
2017-04-10 22:33:20 +02:00
|
|
|
StringView cmd_name = alias.empty() ? name : alias;
|
2013-12-24 03:06:22 +01:00
|
|
|
|
|
|
|
return m_commands.find(cmd_name);
|
|
|
|
}
|
|
|
|
|
2013-07-26 01:17:12 +02:00
|
|
|
void CommandManager::execute_single_command(CommandParameters params,
|
2014-05-07 21:39:59 +02:00
|
|
|
Context& context,
|
2015-10-22 14:59:23 +02:00
|
|
|
const ShellContext& shell_context,
|
2018-02-09 10:30:33 +01:00
|
|
|
BufferCoord pos)
|
2012-08-06 19:29:51 +02:00
|
|
|
{
|
|
|
|
if (params.empty())
|
|
|
|
return;
|
|
|
|
|
2017-01-29 14:56:05 +01:00
|
|
|
constexpr int max_command_depth = 100;
|
|
|
|
if (m_command_depth > max_command_depth)
|
|
|
|
throw runtime_error("maximum nested command depth hit");
|
|
|
|
|
|
|
|
++m_command_depth;
|
|
|
|
auto pop_cmd = on_scope_end([this] { --m_command_depth; });
|
|
|
|
|
2016-12-25 02:42:31 +01:00
|
|
|
ParameterList param_view(params.begin()+1, params.end());
|
2014-10-30 00:22:54 +01:00
|
|
|
auto command_it = find_command(context, params[0]);
|
2012-08-06 19:29:51 +02:00
|
|
|
if (command_it == m_commands.end())
|
|
|
|
throw command_not_found(params[0]);
|
2014-04-08 20:54:32 +02:00
|
|
|
|
2017-06-17 10:27:07 +02:00
|
|
|
const DebugFlags debug_flags = context.options()["debug"].get<DebugFlags>();
|
|
|
|
if (debug_flags & DebugFlags::Commands)
|
2018-03-26 23:25:47 +02:00
|
|
|
write_to_debug_buffer(format("command {} {}", params[0], join(param_view, ' ')));
|
2017-06-17 10:27:07 +02:00
|
|
|
|
2014-05-07 21:39:59 +02:00
|
|
|
try
|
|
|
|
{
|
|
|
|
ParametersParser parameter_parser(param_view,
|
2017-03-07 01:30:54 +01:00
|
|
|
command_it->value.param_desc);
|
2017-11-04 09:00:34 +01:00
|
|
|
command_it->value.func(parameter_parser, context, shell_context);
|
2014-05-07 21:39:59 +02:00
|
|
|
}
|
2018-05-26 05:07:03 +02:00
|
|
|
catch (failure& error)
|
|
|
|
{
|
|
|
|
throw;
|
|
|
|
}
|
2014-05-07 21:39:59 +02:00
|
|
|
catch (runtime_error& error)
|
|
|
|
{
|
2018-05-26 05:07:03 +02:00
|
|
|
error.set_what(format("{}:{}: '{}' {}", pos.line+1, pos.column+1,
|
|
|
|
params[0], error.what()));
|
|
|
|
throw;
|
2014-05-07 21:39:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-21 22:49:25 +02:00
|
|
|
void CommandManager::execute(StringView command_line,
|
2015-10-22 14:48:57 +02:00
|
|
|
Context& context, const ShellContext& shell_context)
|
2011-11-26 19:32:57 +01:00
|
|
|
{
|
2018-02-15 11:23:12 +01:00
|
|
|
CommandParser parser(command_line);
|
2012-01-14 15:02:54 +01:00
|
|
|
|
2018-02-09 10:30:33 +01:00
|
|
|
BufferCoord command_coord;
|
2018-05-06 23:29:52 +02:00
|
|
|
Vector<String, MemoryDomain::Commands> params;
|
|
|
|
while (Optional<Token> token_opt = parser.read_token(true))
|
2012-01-15 02:37:35 +01:00
|
|
|
{
|
2018-02-15 11:23:12 +01:00
|
|
|
auto& token = *token_opt;
|
2014-05-07 21:39:59 +02:00
|
|
|
if (params.empty())
|
2017-10-17 04:25:20 +02:00
|
|
|
command_coord = token.coord;
|
2014-05-07 21:39:59 +02:00
|
|
|
|
2017-10-17 04:25:20 +02:00
|
|
|
if (token.type == Token::Type::CommandSeparator)
|
2012-01-15 02:37:35 +01:00
|
|
|
{
|
2015-10-22 14:59:23 +02:00
|
|
|
execute_single_command(params, context, shell_context, command_coord);
|
2014-04-04 00:57:14 +02:00
|
|
|
params.clear();
|
|
|
|
}
|
2017-10-17 04:25:20 +02:00
|
|
|
else if (token.type == Token::Type::ArgExpand and token.content == '@')
|
2016-12-07 14:26:11 +01:00
|
|
|
params.insert(params.end(), shell_context.params.begin(),
|
|
|
|
shell_context.params.end());
|
2014-04-04 00:57:14 +02:00
|
|
|
else
|
2018-05-30 15:23:38 +02:00
|
|
|
{
|
|
|
|
auto tokens = expand_token<false>(token, context, shell_context);
|
|
|
|
params.insert(params.end(),
|
|
|
|
std::make_move_iterator(tokens.begin()),
|
|
|
|
std::make_move_iterator(tokens.end()));
|
|
|
|
}
|
2012-01-15 02:37:35 +01:00
|
|
|
}
|
2015-10-22 14:59:23 +02:00
|
|
|
execute_single_command(params, context, shell_context, command_coord);
|
2011-09-07 20:16:56 +02:00
|
|
|
}
|
|
|
|
|
2016-12-23 17:23:31 +01:00
|
|
|
Optional<CommandInfo> CommandManager::command_info(const Context& context, StringView command_line) const
|
2014-02-11 23:16:17 +01:00
|
|
|
{
|
2018-02-15 11:23:12 +01:00
|
|
|
CommandParser parser{command_line};
|
|
|
|
Vector<Token> tokens;
|
|
|
|
while (auto token = parser.read_token(false))
|
2014-02-11 23:16:17 +01:00
|
|
|
{
|
2018-02-15 11:23:12 +01:00
|
|
|
if (token->type == Token::Type::CommandSeparator)
|
|
|
|
tokens.clear();
|
|
|
|
else
|
|
|
|
tokens.push_back(std::move(*token));
|
2014-02-11 23:16:17 +01:00
|
|
|
}
|
|
|
|
|
2018-02-15 11:23:12 +01:00
|
|
|
if (tokens.empty() or
|
|
|
|
(tokens.front().type != Token::Type::Raw and
|
|
|
|
tokens.front().type != Token::Type::RawQuoted))
|
2016-12-23 17:23:31 +01:00
|
|
|
return {};
|
2014-02-11 23:16:17 +01:00
|
|
|
|
2018-02-15 11:23:12 +01:00
|
|
|
auto cmd = find_command(context, tokens.front().content);
|
2014-02-11 23:16:17 +01:00
|
|
|
if (cmd == m_commands.end())
|
2016-12-23 17:23:31 +01:00
|
|
|
return {};
|
2014-02-11 23:16:17 +01:00
|
|
|
|
2016-12-23 17:23:31 +01:00
|
|
|
CommandInfo res;
|
2017-03-07 01:30:54 +01:00
|
|
|
res.name = cmd->key;
|
|
|
|
if (not cmd->value.docstring.empty())
|
|
|
|
res.info += cmd->value.docstring + "\n";
|
2014-05-03 16:54:51 +02:00
|
|
|
|
2017-03-07 01:30:54 +01:00
|
|
|
if (cmd->value.helper)
|
2015-02-08 20:04:20 +01:00
|
|
|
{
|
|
|
|
Vector<String> params;
|
2018-02-15 11:23:12 +01:00
|
|
|
for (auto it = tokens.begin() + 1; it != tokens.end(); ++it)
|
2015-02-08 20:04:20 +01:00
|
|
|
{
|
2017-09-01 12:09:34 +02:00
|
|
|
if (it->type == Token::Type::Raw or
|
|
|
|
it->type == Token::Type::RawQuoted or
|
|
|
|
it->type == Token::Type::RawEval)
|
|
|
|
params.push_back(it->content);
|
2015-02-08 20:04:20 +01:00
|
|
|
}
|
2017-03-07 01:30:54 +01:00
|
|
|
String helpstr = cmd->value.helper(context, params);
|
2015-02-08 20:04:20 +01:00
|
|
|
if (not helpstr.empty())
|
2017-06-04 09:37:51 +02:00
|
|
|
res.info += format("{}\n", helpstr);
|
2015-02-08 20:04:20 +01:00
|
|
|
}
|
|
|
|
|
2014-05-03 16:54:51 +02:00
|
|
|
String aliases;
|
2017-03-07 01:30:54 +01:00
|
|
|
for (auto& alias : context.aliases().aliases_for(cmd->key))
|
2014-10-30 00:22:54 +01:00
|
|
|
aliases += " " + alias;
|
2014-05-03 16:54:51 +02:00
|
|
|
if (not aliases.empty())
|
2017-06-04 09:37:51 +02:00
|
|
|
res.info += format("Aliases:{}\n", aliases);
|
2014-05-03 16:54:51 +02:00
|
|
|
|
2017-03-07 01:30:54 +01:00
|
|
|
auto& switches = cmd->value.param_desc.switches;
|
2014-02-11 23:23:44 +01:00
|
|
|
if (not switches.empty())
|
2017-06-04 09:37:51 +02:00
|
|
|
res.info += format("Switches:\n{}", indent(generate_switches_doc(switches)));
|
2014-02-11 23:16:17 +01:00
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2017-06-29 08:43:20 +02:00
|
|
|
Completions CommandManager::complete_command_name(const Context& context, StringView query) const
|
2016-03-08 14:56:37 +01:00
|
|
|
{
|
2016-03-24 01:05:40 +01:00
|
|
|
auto commands = m_commands
|
2017-03-07 01:30:54 +01:00
|
|
|
| filter([](const CommandMap::Item& cmd) { return not (cmd.value.flags & CommandFlags::Hidden); })
|
2018-03-13 04:24:03 +01:00
|
|
|
| transform(&CommandMap::Item::key);
|
2016-03-08 22:35:56 +01:00
|
|
|
|
2018-02-20 12:09:03 +01:00
|
|
|
auto aliases = context.aliases().flatten_aliases()
|
2018-03-13 04:24:03 +01:00
|
|
|
| transform(&HashItem<String, String>::key)
|
2018-02-20 12:09:03 +01:00
|
|
|
| filter([](auto& alias) { return alias.length() > 3; });
|
|
|
|
|
|
|
|
return {0, query.length(), Kakoune::complete(query, query.length(), concatenated(commands, aliases))};
|
2016-03-08 14:56:37 +01:00
|
|
|
}
|
|
|
|
|
2019-05-13 09:34:04 +02:00
|
|
|
Completions CommandManager::complete_module_name(StringView query) const
|
|
|
|
{
|
|
|
|
return {0, query.length(),
|
|
|
|
Kakoune::complete(query, query.length(), m_modules | filter([](auto&& item) { return not item.value.loaded; })
|
|
|
|
| transform(&ModuleMap::Item::key))};
|
|
|
|
}
|
|
|
|
|
2014-04-08 20:54:32 +02:00
|
|
|
Completions CommandManager::complete(const Context& context,
|
|
|
|
CompletionFlags flags,
|
2014-04-21 22:49:25 +02:00
|
|
|
StringView command_line,
|
2014-04-08 20:54:32 +02:00
|
|
|
ByteCount cursor_pos)
|
2011-09-13 23:16:48 +02:00
|
|
|
{
|
2018-02-15 11:23:12 +01:00
|
|
|
CommandParser parser{command_line};
|
|
|
|
const char* cursor = command_line.begin() + (int)cursor_pos;
|
|
|
|
Vector<Token> tokens;
|
2011-09-13 23:16:48 +02:00
|
|
|
|
2018-02-15 11:23:12 +01:00
|
|
|
bool is_last_token = true;
|
|
|
|
while (auto token = parser.read_token(false))
|
2011-09-13 23:16:48 +02:00
|
|
|
{
|
2018-02-15 11:23:12 +01:00
|
|
|
if (token->type == Token::Type::CommandSeparator)
|
|
|
|
{
|
|
|
|
tokens.clear();
|
|
|
|
continue;
|
|
|
|
}
|
2014-01-03 21:07:40 +01:00
|
|
|
|
2018-02-15 11:23:12 +01:00
|
|
|
tokens.push_back(std::move(*token));
|
|
|
|
if (parser.pos() >= cursor)
|
2011-09-13 23:16:48 +02:00
|
|
|
{
|
2018-02-15 11:23:12 +01:00
|
|
|
is_last_token = false;
|
2011-09-13 23:16:48 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-15 11:23:12 +01:00
|
|
|
if (is_last_token)
|
|
|
|
tokens.push_back({Token::Type::Raw, command_line.length(), parser.coord(), {}});
|
|
|
|
kak_assert(not tokens.empty());
|
|
|
|
const auto& token = tokens.back();
|
|
|
|
|
2015-06-26 14:52:01 +02:00
|
|
|
// command name completion
|
2018-02-15 11:23:12 +01:00
|
|
|
if (tokens.size() == 1 and (token.type == Token::Type::Raw or
|
|
|
|
token.type == Token::Type::RawQuoted))
|
2011-09-13 23:16:48 +02:00
|
|
|
{
|
2018-02-15 11:23:12 +01:00
|
|
|
auto cmd_start = token.pos;
|
2016-03-08 14:56:37 +01:00
|
|
|
StringView query = command_line.substr(cmd_start, cursor_pos - cmd_start);
|
2017-06-29 08:43:20 +02:00
|
|
|
return offset_pos(complete_command_name(context, query), cmd_start);
|
2011-09-13 23:16:48 +02:00
|
|
|
}
|
2011-09-16 11:18:51 +02:00
|
|
|
|
2018-02-15 11:23:12 +01:00
|
|
|
const ByteCount start = token.pos;
|
|
|
|
const ByteCount cursor_pos_in_token = cursor_pos - start;
|
2011-09-16 11:18:51 +02:00
|
|
|
|
2018-02-15 11:23:12 +01:00
|
|
|
switch (token.type)
|
2013-12-30 15:22:18 +01:00
|
|
|
{
|
2019-11-23 08:47:14 +01:00
|
|
|
case Token::Type::RegisterExpand:
|
|
|
|
return {start , cursor_pos,
|
|
|
|
RegisterManager::instance().complete_register_name(
|
|
|
|
token.content, cursor_pos_in_token) };
|
|
|
|
|
2013-12-30 15:22:18 +01:00
|
|
|
case Token::Type::OptionExpand:
|
2015-08-27 14:57:56 +02:00
|
|
|
return {start , cursor_pos,
|
|
|
|
GlobalScope::instance().option_registry().complete_option_name(
|
2018-02-15 11:23:12 +01:00
|
|
|
token.content, cursor_pos_in_token) };
|
2015-08-27 14:57:56 +02:00
|
|
|
|
2013-12-30 15:22:18 +01:00
|
|
|
case Token::Type::ShellExpand:
|
2018-02-15 11:23:12 +01:00
|
|
|
return offset_pos(shell_complete(context, flags, token.content,
|
2015-08-27 14:57:56 +02:00
|
|
|
cursor_pos_in_token), start);
|
|
|
|
|
2016-04-17 20:21:43 +02:00
|
|
|
case Token::Type::ValExpand:
|
|
|
|
return {start , cursor_pos,
|
|
|
|
ShellManager::instance().complete_env_var(
|
2018-02-15 11:23:12 +01:00
|
|
|
token.content, cursor_pos_in_token) };
|
2016-04-17 20:21:43 +02:00
|
|
|
|
2019-11-23 08:59:44 +01:00
|
|
|
case Token::Type::FileExpand:
|
|
|
|
{
|
|
|
|
const auto& ignored_files = context.options()["ignored_files"].get<Regex>();
|
|
|
|
return {start , cursor_pos, complete_filename(
|
|
|
|
token.content, ignored_files, cursor_pos_in_token, FilenameFlags::Expand) };
|
|
|
|
}
|
|
|
|
|
2013-12-30 15:22:18 +01:00
|
|
|
case Token::Type::Raw:
|
2015-09-20 12:19:10 +02:00
|
|
|
case Token::Type::RawQuoted:
|
|
|
|
case Token::Type::RawEval:
|
2013-12-30 15:22:18 +01:00
|
|
|
{
|
2018-02-18 22:29:04 +01:00
|
|
|
if (token.type != Token::Type::Raw and token.type != Token::Type::RawQuoted)
|
2013-12-30 15:22:18 +01:00
|
|
|
return Completions{};
|
|
|
|
|
2018-02-15 11:23:12 +01:00
|
|
|
StringView command_name = tokens.front().content;
|
2016-08-05 14:53:19 +02:00
|
|
|
if (command_name != m_last_complete_command)
|
|
|
|
{
|
2017-04-10 22:33:20 +02:00
|
|
|
m_last_complete_command = command_name.str();
|
2016-08-05 14:53:19 +02:00
|
|
|
flags |= CompletionFlags::Start;
|
|
|
|
}
|
2013-12-30 15:22:18 +01:00
|
|
|
|
2014-10-30 00:22:54 +01:00
|
|
|
auto command_it = find_command(context, command_name);
|
2018-08-26 04:29:11 +02:00
|
|
|
if (command_it == m_commands.end())
|
|
|
|
return Completions{};
|
|
|
|
|
|
|
|
if (token.content.substr(0_byte, 1_byte) == "-")
|
|
|
|
{
|
|
|
|
auto switches = Kakoune::complete(token.content.substr(1_byte), cursor_pos_in_token,
|
|
|
|
command_it->value.param_desc.switches |
|
|
|
|
transform(&SwitchMap::Item::key));
|
|
|
|
if (not switches.empty())
|
|
|
|
return Completions{start+1, cursor_pos, std::move(switches)};
|
|
|
|
}
|
|
|
|
if (not command_it->value.completer)
|
|
|
|
return Completions{};
|
2013-12-30 15:22:18 +01:00
|
|
|
|
2015-01-12 14:58:41 +01:00
|
|
|
Vector<String> params;
|
2018-02-15 11:23:12 +01:00
|
|
|
for (auto it = tokens.begin() + 1; it != tokens.end(); ++it)
|
2017-09-01 12:09:34 +02:00
|
|
|
params.push_back(it->content);
|
2017-03-07 01:30:54 +01:00
|
|
|
Completions completions = offset_pos(command_it->value.completer(
|
2018-02-15 11:23:12 +01:00
|
|
|
context, flags, params, tokens.size() - 2,
|
2015-09-20 12:19:10 +02:00
|
|
|
cursor_pos_in_token), start);
|
2014-05-05 13:54:23 +02:00
|
|
|
|
2019-05-12 13:25:08 +02:00
|
|
|
if (not (completions.flags & Completions::Flags::Quoted) and token.type == Token::Type::Raw)
|
2015-09-20 12:19:10 +02:00
|
|
|
{
|
2018-06-23 04:43:06 +02:00
|
|
|
for (auto& c : completions.candidates)
|
|
|
|
c = (not c.empty() and contains("%'\"", c[0]) ? "\\" : "") + escape(c, "; \t", '\\');
|
2015-09-20 12:19:10 +02:00
|
|
|
}
|
2014-05-05 13:54:23 +02:00
|
|
|
|
2014-01-26 17:14:02 +01:00
|
|
|
return completions;
|
2013-12-30 15:22:18 +01:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return Completions{};
|
2011-09-16 11:18:51 +02:00
|
|
|
}
|
|
|
|
|
2015-06-26 14:52:01 +02:00
|
|
|
Completions CommandManager::complete(const Context& context,
|
|
|
|
CompletionFlags flags,
|
|
|
|
CommandParameters params,
|
|
|
|
size_t token_to_complete,
|
|
|
|
ByteCount pos_in_token)
|
|
|
|
{
|
|
|
|
StringView prefix = params[token_to_complete].substr(0, pos_in_token);
|
|
|
|
|
|
|
|
if (token_to_complete == 0)
|
2017-06-29 08:43:20 +02:00
|
|
|
return complete_command_name(context, prefix);
|
2015-06-26 14:52:01 +02:00
|
|
|
else
|
|
|
|
{
|
2017-04-10 22:33:20 +02:00
|
|
|
StringView command_name = params[0];
|
2016-08-05 14:53:19 +02:00
|
|
|
if (command_name != m_last_complete_command)
|
|
|
|
{
|
2017-04-10 22:33:20 +02:00
|
|
|
m_last_complete_command = command_name.str();
|
2016-08-05 14:53:19 +02:00
|
|
|
flags |= CompletionFlags::Start;
|
|
|
|
}
|
2015-06-26 14:52:01 +02:00
|
|
|
|
|
|
|
auto command_it = find_command(context, command_name);
|
2017-03-07 01:30:54 +01:00
|
|
|
if (command_it != m_commands.end() and command_it->value.completer)
|
|
|
|
return command_it->value.completer(
|
2016-11-20 12:15:15 +01:00
|
|
|
context, flags, params.subrange(1),
|
2015-06-26 14:52:01 +02:00
|
|
|
token_to_complete-1, pos_in_token);
|
|
|
|
}
|
|
|
|
return Completions{};
|
|
|
|
}
|
|
|
|
|
Refactor command line parsing
Command line parsing now works as follow:
* Quoted strings ('...', "..." and %~...~ with '~' non nestable)
use 'doubling-up' for escaping their delimiter, if the delimiter
appears twice in a row, it is considered as part of the string and
represent one delimiter character. So 'abc''def' == "abc'def". No
other escaping takes place in those strings.
* Balanced strings (%{...}) do not support any kind of escaping, but
finds the matching closing delimiter by taking nesting into account.
So %{abc{def}} == "abc{def}".
* Non quoted words support escaping of `;` and whitespaces with `\`,
`%`, `'` and '"` can be escaped with `\` at the start of the word,
they do not need escaping (and will not be escaped) else where in
a word where they are treated literally. Any other use of '\' is a
literal '\'. So \%abc%\;\ def == "%abc%; def"
As discussed in #2046 this should make our command line syntax more
robust, provide a simple programmatic way to escape a string content
(s/<delim>/<delim><delim>/g), be well defined instead of ad-hoc
undocumented behaviour, and interact nicely with other common
escaping by avoiding escaping hell (:grep <regex> can in most case
be written with the regex unquoted).
2018-05-21 14:22:34 +02:00
|
|
|
UnitTest test_command_parsing{[]
|
|
|
|
{
|
|
|
|
auto check_quoted = [](StringView str, bool terminated, StringView content)
|
|
|
|
{
|
|
|
|
Reader reader{str};
|
|
|
|
const Codepoint delimiter = *reader;
|
|
|
|
auto quoted = parse_quoted(++reader, delimiter);
|
|
|
|
kak_assert(quoted.terminated == terminated);
|
|
|
|
kak_assert(quoted.content == content);
|
|
|
|
};
|
|
|
|
check_quoted("'abc'", true, "abc");
|
|
|
|
check_quoted("'abc''def", false, "abc'def");
|
|
|
|
check_quoted("'abc''def'''", true, "abc'def'");
|
|
|
|
|
|
|
|
auto check_balanced = [](StringView str, Codepoint opening, Codepoint closing, bool terminated, StringView content)
|
|
|
|
{
|
|
|
|
Reader reader{str};
|
|
|
|
auto quoted = parse_quoted_balanced(++reader, opening, closing);
|
|
|
|
kak_assert(quoted.terminated == terminated);
|
|
|
|
kak_assert(quoted.content == content);
|
|
|
|
};
|
|
|
|
check_balanced("{abc}", '{', '}', true, "abc");
|
|
|
|
check_balanced("{abc{def}}", '{', '}', true, "abc{def}");
|
|
|
|
check_balanced("{{abc}{def}", '{', '}', false, "{abc}{def}");
|
|
|
|
|
|
|
|
auto check_unquoted = [](StringView str, StringView content)
|
|
|
|
{
|
|
|
|
Reader reader{str};
|
|
|
|
auto res = parse_unquoted(reader);
|
|
|
|
kak_assert(res == content);
|
|
|
|
};
|
|
|
|
check_unquoted("abc def", "abc");
|
|
|
|
check_unquoted("abc; def", "abc");
|
|
|
|
check_unquoted("abc\\; def", "abc;");
|
|
|
|
check_unquoted("abc\\;\\ def", "abc; def");
|
2018-05-23 15:13:26 +02:00
|
|
|
|
|
|
|
{
|
|
|
|
CommandParser parser(R"(foo 'bar' "baz" qux)");
|
|
|
|
kak_assert(parser.read_token(false)->content == "foo");
|
|
|
|
kak_assert(parser.read_token(false)->content == "bar");
|
|
|
|
kak_assert(parser.read_token(false)->content == "baz");
|
|
|
|
kak_assert(parser.read_token(false)->content == "qux");
|
|
|
|
kak_assert(not parser.read_token(false));
|
|
|
|
}
|
Refactor command line parsing
Command line parsing now works as follow:
* Quoted strings ('...', "..." and %~...~ with '~' non nestable)
use 'doubling-up' for escaping their delimiter, if the delimiter
appears twice in a row, it is considered as part of the string and
represent one delimiter character. So 'abc''def' == "abc'def". No
other escaping takes place in those strings.
* Balanced strings (%{...}) do not support any kind of escaping, but
finds the matching closing delimiter by taking nesting into account.
So %{abc{def}} == "abc{def}".
* Non quoted words support escaping of `;` and whitespaces with `\`,
`%`, `'` and '"` can be escaped with `\` at the start of the word,
they do not need escaping (and will not be escaped) else where in
a word where they are treated literally. Any other use of '\' is a
literal '\'. So \%abc%\;\ def == "%abc%; def"
As discussed in #2046 this should make our command line syntax more
robust, provide a simple programmatic way to escape a string content
(s/<delim>/<delim><delim>/g), be well defined instead of ad-hoc
undocumented behaviour, and interact nicely with other common
escaping by avoiding escaping hell (:grep <regex> can in most case
be written with the regex unquoted).
2018-05-21 14:22:34 +02:00
|
|
|
}};
|
|
|
|
|
2011-09-07 20:16:56 +02:00
|
|
|
}
|