1
1
mirror of https://github.com/mawww/kakoune.git synced 2024-12-25 12:36:11 +03:00

Commands take a mutable context, main_context is gone !

This commit is contained in:
Maxime Coste 2012-08-06 22:02:11 +02:00
parent a712dd5bbe
commit a38a9c3bf2
4 changed files with 111 additions and 114 deletions

View File

@ -181,7 +181,7 @@ struct command_not_found : runtime_error
}; };
void CommandManager::execute_single_command(const CommandParameters& params, void CommandManager::execute_single_command(const CommandParameters& params,
const Context& context) const Context& context) const
{ {
if (params.empty()) if (params.empty())
return; return;
@ -194,7 +194,7 @@ void CommandManager::execute_single_command(const CommandParameters& params,
} }
void CommandManager::execute(const String& command_line, void CommandManager::execute(const String& command_line,
const Context& context, Context& context,
const EnvVarMap& env_vars) const EnvVarMap& env_vars)
{ {
TokenList tokens = parse(command_line); TokenList tokens = parse(command_line);

View File

@ -24,7 +24,7 @@ struct wrong_argument_count : runtime_error
using CommandParameters = memoryview<String>; using CommandParameters = memoryview<String>;
typedef std::function<void (const CommandParameters&, typedef std::function<void (const CommandParameters&,
const Context& context)> Command; Context& context)> Command;
typedef std::function<CandidateList (const Context& context, typedef std::function<CandidateList (const Context& context,
const CommandParameters&, const CommandParameters&,
@ -52,7 +52,7 @@ private:
class CommandManager : public Singleton<CommandManager> class CommandManager : public Singleton<CommandManager>
{ {
public: public:
void execute(const String& command_line, const Context& context, void execute(const String& command_line, Context& context,
const EnvVarMap& env_vars = EnvVarMap()); const EnvVarMap& env_vars = EnvVarMap());
Completions complete(const Context& context, Completions complete(const Context& context,
@ -70,7 +70,7 @@ public:
private: private:
void execute_single_command(const CommandParameters& params, void execute_single_command(const CommandParameters& params,
const Context& context) const; Context& context) const;
struct CommandDescriptor struct CommandDescriptor
{ {
Command command; Command command;

View File

@ -25,10 +25,9 @@ namespace Kakoune
using namespace std::placeholders; using namespace std::placeholders;
// berk // berk
extern Context main_context;
extern bool quit_requested; extern bool quit_requested;
extern std::unordered_map<Key, std::function<void (const Context& context)>> keymap; extern std::unordered_map<Key, std::function<void (Context& context)>> keymap;
namespace namespace
{ {
@ -231,7 +230,7 @@ Buffer* open_or_create(const String& filename)
} }
template<bool force_reload> template<bool force_reload>
void edit(const CommandParameters& params, const Context& context) void edit(const CommandParameters& params, Context& context)
{ {
if (params.size() == 0 or params.size() > 3) if (params.size() == 0 or params.size() > 3)
throw wrong_argument_count(); throw wrong_argument_count();
@ -255,10 +254,10 @@ void edit(const CommandParameters& params, const Context& context)
window.select(window.buffer().iterator_at({line, column})); window.select(window.buffer().iterator_at({line, column}));
} }
main_context = Context(window); context = Context(window);
} }
void write_buffer(const CommandParameters& params, const Context& context) void write_buffer(const CommandParameters& params, Context& context)
{ {
if (params.size() > 1) if (params.size() > 1)
throw wrong_argument_count(); throw wrong_argument_count();
@ -272,7 +271,7 @@ void write_buffer(const CommandParameters& params, const Context& context)
} }
template<bool force> template<bool force>
void quit(const CommandParameters& params, const Context& context) void quit(const CommandParameters& params, Context& context)
{ {
if (params.size() != 0) if (params.size() != 0)
throw wrong_argument_count(); throw wrong_argument_count();
@ -302,13 +301,13 @@ void quit(const CommandParameters& params, const Context& context)
} }
template<bool force> template<bool force>
void write_and_quit(const CommandParameters& params, const Context& context) void write_and_quit(const CommandParameters& params, Context& context)
{ {
write_buffer(params, context); write_buffer(params, context);
quit<force>(CommandParameters(), context); quit<force>(CommandParameters(), context);
} }
void show_buffer(const CommandParameters& params, const Context& context) void show_buffer(const CommandParameters& params, Context& context)
{ {
if (params.size() != 1) if (params.size() != 1)
throw wrong_argument_count(); throw wrong_argument_count();
@ -318,10 +317,10 @@ void show_buffer(const CommandParameters& params, const Context& context)
if (not buffer) if (not buffer)
throw runtime_error("buffer " + buffer_name + " does not exists"); throw runtime_error("buffer " + buffer_name + " does not exists");
main_context = Context(*buffer->get_or_create_window()); context = Context(*buffer->get_or_create_window());
} }
void delete_buffer(const CommandParameters& params, const Context& context) void delete_buffer(const CommandParameters& params, Context& context)
{ {
if (params.size() > 1) if (params.size() > 1)
throw wrong_argument_count(); throw wrong_argument_count();
@ -340,7 +339,7 @@ void delete_buffer(const CommandParameters& params, const Context& context)
if (buffer->type()!= Buffer::Type::Scratch and buffer->is_modified()) if (buffer->type()!= Buffer::Type::Scratch and buffer->is_modified())
throw runtime_error("buffer " + buffer->name() + " is modified"); throw runtime_error("buffer " + buffer->name() + " is modified");
if (&main_context.buffer() == buffer) if (&context.buffer() == buffer)
{ {
if (manager.count() == 1) if (manager.count() == 1)
throw runtime_error("buffer " + buffer->name() + " is the last one"); throw runtime_error("buffer " + buffer->name() + " is the last one");
@ -348,7 +347,7 @@ void delete_buffer(const CommandParameters& params, const Context& context)
{ {
if (&buf != buffer) if (&buf != buffer)
{ {
main_context = Context(*buf.get_or_create_window()); context = Context(*buf.get_or_create_window());
break; break;
} }
} }
@ -356,7 +355,7 @@ void delete_buffer(const CommandParameters& params, const Context& context)
delete buffer; delete buffer;
} }
void add_highlighter(const CommandParameters& params, const Context& context) void add_highlighter(const CommandParameters& params, Context& context)
{ {
ParametersParser parser(params, { { "group", true } }); ParametersParser parser(params, { { "group", true } });
if (parser.positional_count() < 1) if (parser.positional_count() < 1)
@ -379,7 +378,7 @@ void add_highlighter(const CommandParameters& params, const Context& context)
highlighter_params); highlighter_params);
} }
void rm_highlighter(const CommandParameters& params, const Context& context) void rm_highlighter(const CommandParameters& params, Context& context)
{ {
ParametersParser parser(params, { { "group", true } }); ParametersParser parser(params, { { "group", true } });
if (parser.positional_count() != 1) if (parser.positional_count() != 1)
@ -393,7 +392,7 @@ void rm_highlighter(const CommandParameters& params, const Context& context)
group.remove(*parser.begin()); group.remove(*parser.begin());
} }
void add_filter(const CommandParameters& params, const Context& context) void add_filter(const CommandParameters& params, Context& context)
{ {
ParametersParser parser(params, { { "group", true } }); ParametersParser parser(params, { { "group", true } });
if (parser.positional_count() < 1) if (parser.positional_count() < 1)
@ -415,7 +414,7 @@ void add_filter(const CommandParameters& params, const Context& context)
registry.add_filter_to_group(group, name, filter_params); registry.add_filter_to_group(group, name, filter_params);
} }
void rm_filter(const CommandParameters& params, const Context& context) void rm_filter(const CommandParameters& params, Context& context)
{ {
ParametersParser parser(params, { { "group", true } }); ParametersParser parser(params, { { "group", true } });
if (parser.positional_count() != 1) if (parser.positional_count() != 1)
@ -429,7 +428,7 @@ void rm_filter(const CommandParameters& params, const Context& context)
group.remove(*parser.begin()); group.remove(*parser.begin());
} }
void add_hook(const CommandParameters& params, const Context& context) void add_hook(const CommandParameters& params, Context& context)
{ {
if (params.size() != 4) if (params.size() != 4)
throw wrong_argument_count(); throw wrong_argument_count();
@ -440,7 +439,10 @@ void add_hook(const CommandParameters& params, const Context& context)
auto hook_func = [=](const String& param, const Context& context) { auto hook_func = [=](const String& param, const Context& context) {
if (boost::regex_match(param.begin(), param.end(), if (boost::regex_match(param.begin(), param.end(),
Regex(regex.begin(), regex.end()))) Regex(regex.begin(), regex.end())))
CommandManager::instance().execute(command, context); {
Context new_context(context);
CommandManager::instance().execute(command, new_context);
}
}; };
const String& scope = params[0]; const String& scope = params[0];
@ -467,7 +469,7 @@ EnvVarMap params_to_env_var_map(const CommandParameters& params)
return vars; return vars;
} }
void define_command(const CommandParameters& params, const Context& context) void define_command(const CommandParameters& params, Context& context)
{ {
ParametersParser parser(params, ParametersParser parser(params,
{ { "env-params", false }, { { "env-params", false },
@ -489,14 +491,14 @@ void define_command(const CommandParameters& params, const Context& context)
Command cmd; Command cmd;
if (parser.has_option("env-params")) if (parser.has_option("env-params"))
{ {
cmd = [=](const CommandParameters& params, const Context& context) { cmd = [=](const CommandParameters& params, Context& context) {
CommandManager::instance().execute(commands, context, CommandManager::instance().execute(commands, context,
params_to_env_var_map(params)); params_to_env_var_map(params));
}; };
} }
else else
{ {
cmd = [=](const CommandParameters& params, const Context& context) { cmd = [=](const CommandParameters& params, Context& context) {
if (not params.empty()) if (not params.empty())
throw wrong_argument_count(); throw wrong_argument_count();
CommandManager::instance().execute(commands, context); CommandManager::instance().execute(commands, context);
@ -521,7 +523,7 @@ void define_command(const CommandParameters& params, const Context& context)
CommandManager::instance().register_command(cmd_name, cmd); CommandManager::instance().register_command(cmd_name, cmd);
} }
void echo_message(const CommandParameters& params, const Context& context) void echo_message(const CommandParameters& params, Context& context)
{ {
String message; String message;
for (auto& param : params) for (auto& param : params)
@ -530,7 +532,7 @@ void echo_message(const CommandParameters& params, const Context& context)
} }
void exec_commands_in_file(const CommandParameters& params, void exec_commands_in_file(const CommandParameters& params,
const Context& context) Context& context)
{ {
if (params.size() != 1) if (params.size() != 1)
throw wrong_argument_count(); throw wrong_argument_count();
@ -540,7 +542,7 @@ void exec_commands_in_file(const CommandParameters& params,
} }
void exec_commands_in_runtime_file(const CommandParameters& params, void exec_commands_in_runtime_file(const CommandParameters& params,
const Context& context) Context& context)
{ {
if (params.size() != 1) if (params.size() != 1)
throw wrong_argument_count(); throw wrong_argument_count();
@ -564,12 +566,12 @@ void exec_commands_in_runtime_file(const CommandParameters& params,
if (ptr) if (ptr)
{ {
strcpy(ptr+1, filename.c_str()); strcpy(ptr+1, filename.c_str());
exec_commands_in_file(CommandParameters(buffer), main_context); exec_commands_in_file(CommandParameters(buffer), context);
} }
} }
void set_option(OptionManager& option_manager, const CommandParameters& params, void set_option(OptionManager& option_manager, const CommandParameters& params,
const Context& context) Context& context)
{ {
if (params.size() != 2) if (params.size() != 2)
throw wrong_argument_count(); throw wrong_argument_count();
@ -649,8 +651,7 @@ private:
Client* m_previous_client; Client* m_previous_client;
}; };
void exec_keys(const KeyList& keys, void exec_keys(const KeyList& keys, Context& context)
const Context& context)
{ {
BatchClient batch_client(keys); BatchClient batch_client(keys);
@ -684,8 +685,7 @@ void exec_keys(const KeyList& keys,
} }
} }
void exec_string(const CommandParameters& params, void exec_string(const CommandParameters& params, Context& context)
const Context& context)
{ {
if (params.size() != 1) if (params.size() != 1)
throw wrong_argument_count(); throw wrong_argument_count();
@ -695,8 +695,7 @@ void exec_string(const CommandParameters& params,
exec_keys(keys, context); exec_keys(keys, context);
} }
void menu(const CommandParameters& params, void menu(const CommandParameters& params, Context& context)
const Context& context)
{ {
ParametersParser parser(params, { { "auto-single", false } }); ParametersParser parser(params, { { "auto-single", false } });
@ -724,8 +723,7 @@ void menu(const CommandParameters& params,
CommandManager::instance().execute(parser[(i-1)*2+1], context); CommandManager::instance().execute(parser[(i-1)*2+1], context);
} }
void try_catch(const CommandParameters& params, void try_catch(const CommandParameters& params, Context& context)
const Context& context)
{ {
if (params.size() != 3) if (params.size() != 3)
throw wrong_argument_count(); throw wrong_argument_count();
@ -835,21 +833,21 @@ void register_commands()
cm.register_command("echo", echo_message); cm.register_command("echo", echo_message);
cm.register_commands({ "setg", "setglobal" }, cm.register_commands({ "setg", "setglobal" },
[](const CommandParameters& params, const Context& context) [](const CommandParameters& params, Context& context)
{ set_option(GlobalOptionManager::instance(), params, context); }, { set_option(GlobalOptionManager::instance(), params, context); },
PerArgumentCommandCompleter({ PerArgumentCommandCompleter({
[](const Context& context, const String& prefix, size_t cursor_pos) [](const Context& context, const String& prefix, size_t cursor_pos)
{ return GlobalOptionManager::instance().complete_option_name(prefix, cursor_pos); } { return GlobalOptionManager::instance().complete_option_name(prefix, cursor_pos); }
})); }));
cm.register_commands({ "setb", "setbuffer" }, cm.register_commands({ "setb", "setbuffer" },
[](const CommandParameters& params, const Context& context) [](const CommandParameters& params, Context& context)
{ set_option(context.buffer().option_manager(), params, context); }, { set_option(context.buffer().option_manager(), params, context); },
PerArgumentCommandCompleter({ PerArgumentCommandCompleter({
[](const Context& context, const String& prefix, size_t cursor_pos) [](const Context& context, const String& prefix, size_t cursor_pos)
{ return context.buffer().option_manager().complete_option_name(prefix, cursor_pos); } { return context.buffer().option_manager().complete_option_name(prefix, cursor_pos); }
})); }));
cm.register_commands({ "setw", "setwindow" }, cm.register_commands({ "setw", "setwindow" },
[](const CommandParameters& params, const Context& context) [](const CommandParameters& params, Context& context)
{ set_option(context.window().option_manager(), params, context); }, { set_option(context.window().option_manager(), params, context); },
PerArgumentCommandCompleter({ PerArgumentCommandCompleter({
[](const Context& context, const String& prefix, size_t cursor_pos) [](const Context& context, const String& prefix, size_t cursor_pos)

View File

@ -28,7 +28,6 @@ using namespace std::placeholders;
namespace Kakoune namespace Kakoune
{ {
Context main_context;
bool quit_requested = false; bool quit_requested = false;
struct InsertSequence struct InsertSequence
@ -93,7 +92,7 @@ void insert_sequence(IncrementalInserter& inserter,
} }
template<IncrementalInserter::Mode mode> template<IncrementalInserter::Mode mode>
void do_insert(const Context& context) void do_insert(Context& context)
{ {
last_insert_sequence.mode = mode; last_insert_sequence.mode = mode;
last_insert_sequence.keys.clear(); last_insert_sequence.keys.clear();
@ -106,7 +105,7 @@ void do_insert(const Context& context)
[&]() { draw_editor_ifn(context.editor()); }); [&]() { draw_editor_ifn(context.editor()); });
} }
void do_repeat_insert(const Context& context) void do_repeat_insert(Context& context)
{ {
if (last_insert_sequence.keys.empty()) if (last_insert_sequence.keys.empty())
return; return;
@ -119,7 +118,7 @@ void do_repeat_insert(const Context& context)
} }
template<bool append> template<bool append>
void do_go(const Context& context) void do_go(Context& context)
{ {
int count = context.numeric_param(); int count = context.numeric_param();
Editor& editor = context.editor(); Editor& editor = context.editor();
@ -165,7 +164,7 @@ void do_go(const Context& context)
} }
} }
void do_command(const Context& context) void do_command(Context& context)
{ {
try try
{ {
@ -179,7 +178,7 @@ void do_command(const Context& context)
catch (prompt_aborted&) {} catch (prompt_aborted&) {}
} }
void do_pipe(const Context& context) void do_pipe(Context& context)
{ {
try try
{ {
@ -199,7 +198,7 @@ void do_pipe(const Context& context)
} }
template<bool append> template<bool append>
void do_search(const Context& context) void do_search(Context& context)
{ {
try try
{ {
@ -215,7 +214,7 @@ void do_search(const Context& context)
} }
template<bool append> template<bool append>
void do_search_next(const Context& context) void do_search_next(Context& context)
{ {
const String& ex = RegisterManager::instance()['/'].values(context)[0]; const String& ex = RegisterManager::instance()['/'].values(context)[0];
if (not ex.empty()) if (not ex.empty())
@ -224,18 +223,18 @@ void do_search_next(const Context& context)
print_status("no search pattern"); print_status("no search pattern");
} }
void do_yank(const Context& context) void do_yank(Context& context)
{ {
RegisterManager::instance()['"'] = context.editor().selections_content(); RegisterManager::instance()['"'] = context.editor().selections_content();
} }
void do_erase(const Context& context) void do_erase(Context& context)
{ {
RegisterManager::instance()['"'] = context.editor().selections_content(); RegisterManager::instance()['"'] = context.editor().selections_content();
context.editor().erase(); context.editor().erase();
} }
void do_change(const Context& context) void do_change(Context& context)
{ {
RegisterManager::instance()['"'] = context.editor().selections_content(); RegisterManager::instance()['"'] = context.editor().selections_content();
do_insert<IncrementalInserter::Mode::Change>(context); do_insert<IncrementalInserter::Mode::Change>(context);
@ -249,7 +248,7 @@ enum class PasteMode
}; };
template<PasteMode paste_mode> template<PasteMode paste_mode>
void do_paste(const Context& context) void do_paste(Context& context)
{ {
Editor& editor = context.editor(); Editor& editor = context.editor();
int count = context.numeric_param(); int count = context.numeric_param();
@ -274,7 +273,7 @@ void do_paste(const Context& context)
} }
} }
void do_select_regex(const Context& context) void do_select_regex(Context& context)
{ {
try try
{ {
@ -284,7 +283,7 @@ void do_select_regex(const Context& context)
catch (prompt_aborted&) {} catch (prompt_aborted&) {}
} }
void do_split_regex(const Context& context) void do_split_regex(Context& context)
{ {
try try
{ {
@ -294,7 +293,7 @@ void do_split_regex(const Context& context)
catch (prompt_aborted&) {} catch (prompt_aborted&) {}
} }
void do_join(const Context& context) void do_join(Context& context)
{ {
Editor& editor = context.editor(); Editor& editor = context.editor();
editor.select(select_whole_lines); editor.select(select_whole_lines);
@ -306,7 +305,7 @@ void do_join(const Context& context)
} }
template<bool inner> template<bool inner>
void do_select_object(const Context& context) void do_select_object(Context& context)
{ {
typedef std::function<SelectionAndCaptures (const Selection&)> Selector; typedef std::function<SelectionAndCaptures (const Selection&)> Selector;
static const std::unordered_map<Key, Selector> key_to_selector = static const std::unordered_map<Key, Selector> key_to_selector =
@ -337,7 +336,7 @@ class Repeated
public: public:
Repeated(T t) : m_func(t) {} Repeated(T t) : m_func(t) {}
void operator() (const Context& context) void operator() (Context& context)
{ {
int count = context.numeric_param(); int count = context.numeric_param();
do { m_func(context); } while(--count > 0); do { m_func(context); } while(--count > 0);
@ -349,22 +348,22 @@ private:
template<typename T> template<typename T>
Repeated<T> repeated(T func) { return Repeated<T>(func); } Repeated<T> repeated(T func) { return Repeated<T>(func); }
std::unordered_map<Key, std::function<void (const Context& context)>> keymap = std::unordered_map<Key, std::function<void (Context& context)>> keymap =
{ {
{ { Key::Modifiers::None, 'h' }, [](const Context& context) { context.editor().move_selections(BufferCoord(0, -std::max(context.numeric_param(),1))); } }, { { Key::Modifiers::None, 'h' }, [](Context& context) { context.editor().move_selections(BufferCoord(0, -std::max(context.numeric_param(),1))); } },
{ { Key::Modifiers::None, 'j' }, [](const Context& context) { context.editor().move_selections(BufferCoord( std::max(context.numeric_param(),1), 0)); } }, { { Key::Modifiers::None, 'j' }, [](Context& context) { context.editor().move_selections(BufferCoord( std::max(context.numeric_param(),1), 0)); } },
{ { Key::Modifiers::None, 'k' }, [](const Context& context) { context.editor().move_selections(BufferCoord(-std::max(context.numeric_param(),1), 0)); } }, { { Key::Modifiers::None, 'k' }, [](Context& context) { context.editor().move_selections(BufferCoord(-std::max(context.numeric_param(),1), 0)); } },
{ { Key::Modifiers::None, 'l' }, [](const Context& context) { context.editor().move_selections(BufferCoord(0, std::max(context.numeric_param(),1))); } }, { { Key::Modifiers::None, 'l' }, [](Context& context) { context.editor().move_selections(BufferCoord(0, std::max(context.numeric_param(),1))); } },
{ { Key::Modifiers::None, 'H' }, [](const Context& context) { context.editor().move_selections(BufferCoord(0, -std::max(context.numeric_param(),1)), true); } }, { { Key::Modifiers::None, 'H' }, [](Context& context) { context.editor().move_selections(BufferCoord(0, -std::max(context.numeric_param(),1)), true); } },
{ { Key::Modifiers::None, 'J' }, [](const Context& context) { context.editor().move_selections(BufferCoord( std::max(context.numeric_param(),1), 0), true); } }, { { Key::Modifiers::None, 'J' }, [](Context& context) { context.editor().move_selections(BufferCoord( std::max(context.numeric_param(),1), 0), true); } },
{ { Key::Modifiers::None, 'K' }, [](const Context& context) { context.editor().move_selections(BufferCoord(-std::max(context.numeric_param(),1), 0), true); } }, { { Key::Modifiers::None, 'K' }, [](Context& context) { context.editor().move_selections(BufferCoord(-std::max(context.numeric_param(),1), 0), true); } },
{ { Key::Modifiers::None, 'L' }, [](const Context& context) { context.editor().move_selections(BufferCoord(0, std::max(context.numeric_param(),1)), true); } }, { { Key::Modifiers::None, 'L' }, [](Context& context) { context.editor().move_selections(BufferCoord(0, std::max(context.numeric_param(),1)), true); } },
{ { Key::Modifiers::None, 't' }, [](const Context& context) { context.editor().select(std::bind(select_to, _1, get_key().key, context.numeric_param(), false)); } }, { { Key::Modifiers::None, 't' }, [](Context& context) { context.editor().select(std::bind(select_to, _1, get_key().key, context.numeric_param(), false)); } },
{ { Key::Modifiers::None, 'f' }, [](const Context& context) { context.editor().select(std::bind(select_to, _1, get_key().key, context.numeric_param(), true)); } }, { { Key::Modifiers::None, 'f' }, [](Context& context) { context.editor().select(std::bind(select_to, _1, get_key().key, context.numeric_param(), true)); } },
{ { Key::Modifiers::None, 'T' }, [](const Context& context) { context.editor().select(std::bind(select_to, _1, get_key().key, context.numeric_param(), false), true); } }, { { Key::Modifiers::None, 'T' }, [](Context& context) { context.editor().select(std::bind(select_to, _1, get_key().key, context.numeric_param(), false), true); } },
{ { Key::Modifiers::None, 'F' }, [](const Context& context) { context.editor().select(std::bind(select_to, _1, get_key().key, context.numeric_param(), true), true); } }, { { Key::Modifiers::None, 'F' }, [](Context& context) { context.editor().select(std::bind(select_to, _1, get_key().key, context.numeric_param(), true), true); } },
{ { Key::Modifiers::None, 'd' }, do_erase }, { { Key::Modifiers::None, 'd' }, do_erase },
{ { Key::Modifiers::None, 'c' }, do_change }, { { Key::Modifiers::None, 'c' }, do_change },
@ -388,60 +387,60 @@ std::unordered_map<Key, std::function<void (const Context& context)>> keymap =
{ { Key::Modifiers::None, '.' }, do_repeat_insert }, { { Key::Modifiers::None, '.' }, do_repeat_insert },
{ { Key::Modifiers::None, '%' }, [](const Context& context) { context.editor().clear_selections(); context.editor().select(select_whole_buffer); } }, { { Key::Modifiers::None, '%' }, [](Context& context) { context.editor().clear_selections(); context.editor().select(select_whole_buffer); } },
{ { Key::Modifiers::None, ':' }, do_command }, { { Key::Modifiers::None, ':' }, do_command },
{ { Key::Modifiers::None, '|' }, do_pipe }, { { Key::Modifiers::None, '|' }, do_pipe },
{ { Key::Modifiers::None, ' ' }, [](const Context& context) { int count = context.numeric_param(); { { Key::Modifiers::None, ' ' }, [](Context& context) { int count = context.numeric_param();
if (count == 0) context.editor().clear_selections(); if (count == 0) context.editor().clear_selections();
else context.editor().keep_selection(count-1); } }, else context.editor().keep_selection(count-1); } },
{ { Key::Modifiers::Alt, ' ' }, [](const Context& context) { int count = context.numeric_param(); { { Key::Modifiers::Alt, ' ' }, [](Context& context) { int count = context.numeric_param();
if (count == 0) context.editor().clear_selections(); if (count == 0) context.editor().clear_selections();
else context.editor().remove_selection(count-1); } }, else context.editor().remove_selection(count-1); } },
{ { Key::Modifiers::None, 'w' }, repeated([](const Context& context) { context.editor().select(select_to_next_word<false>); }) }, { { Key::Modifiers::None, 'w' }, repeated([](Context& context) { context.editor().select(select_to_next_word<false>); }) },
{ { Key::Modifiers::None, 'e' }, repeated([](const Context& context) { context.editor().select(select_to_next_word_end<false>); }) }, { { Key::Modifiers::None, 'e' }, repeated([](Context& context) { context.editor().select(select_to_next_word_end<false>); }) },
{ { Key::Modifiers::None, 'b' }, repeated([](const Context& context) { context.editor().select(select_to_previous_word<false>); }) }, { { Key::Modifiers::None, 'b' }, repeated([](Context& context) { context.editor().select(select_to_previous_word<false>); }) },
{ { Key::Modifiers::None, 'W' }, repeated([](const Context& context) { context.editor().select(select_to_next_word<false>, true); }) }, { { Key::Modifiers::None, 'W' }, repeated([](Context& context) { context.editor().select(select_to_next_word<false>, true); }) },
{ { Key::Modifiers::None, 'E' }, repeated([](const Context& context) { context.editor().select(select_to_next_word_end<false>, true); }) }, { { Key::Modifiers::None, 'E' }, repeated([](Context& context) { context.editor().select(select_to_next_word_end<false>, true); }) },
{ { Key::Modifiers::None, 'B' }, repeated([](const Context& context) { context.editor().select(select_to_previous_word<false>, true); }) }, { { Key::Modifiers::None, 'B' }, repeated([](Context& context) { context.editor().select(select_to_previous_word<false>, true); }) },
{ { Key::Modifiers::None, 'x' }, repeated([](const Context& context) { context.editor().select(select_line, false); }) }, { { Key::Modifiers::None, 'x' }, repeated([](Context& context) { context.editor().select(select_line, false); }) },
{ { Key::Modifiers::None, 'X' }, repeated([](const Context& context) { context.editor().select(select_line, true); }) }, { { Key::Modifiers::None, 'X' }, repeated([](Context& context) { context.editor().select(select_line, true); }) },
{ { Key::Modifiers::None, 'm' }, [](const Context& context) { context.editor().select(select_matching); } }, { { Key::Modifiers::None, 'm' }, [](Context& context) { context.editor().select(select_matching); } },
{ { Key::Modifiers::None, 'M' }, [](const Context& context) { context.editor().select(select_matching, true); } }, { { Key::Modifiers::None, 'M' }, [](Context& context) { context.editor().select(select_matching, true); } },
{ { Key::Modifiers::None, '/' }, do_search<false> }, { { Key::Modifiers::None, '/' }, do_search<false> },
{ { Key::Modifiers::None, '?' }, do_search<true> }, { { Key::Modifiers::None, '?' }, do_search<true> },
{ { Key::Modifiers::None, 'n' }, do_search_next<false> }, { { Key::Modifiers::None, 'n' }, do_search_next<false> },
{ { Key::Modifiers::None, 'N' }, do_search_next<true> }, { { Key::Modifiers::None, 'N' }, do_search_next<true> },
{ { Key::Modifiers::None, 'u' }, repeated([](const Context& context) { if (not context.editor().undo()) { print_status("nothing left to undo"); } }) }, { { Key::Modifiers::None, 'u' }, repeated([](Context& context) { if (not context.editor().undo()) { print_status("nothing left to undo"); } }) },
{ { Key::Modifiers::None, 'U' }, repeated([](const Context& context) { if (not context.editor().redo()) { print_status("nothing left to redo"); } }) }, { { Key::Modifiers::None, 'U' }, repeated([](Context& context) { if (not context.editor().redo()) { print_status("nothing left to redo"); } }) },
{ { Key::Modifiers::Alt, 'i' }, do_select_object<true> }, { { Key::Modifiers::Alt, 'i' }, do_select_object<true> },
{ { Key::Modifiers::Alt, 'a' }, do_select_object<false> }, { { Key::Modifiers::Alt, 'a' }, do_select_object<false> },
{ { Key::Modifiers::Alt, 't' }, [](const Context& context) { context.editor().select(std::bind(select_to_reverse, _1, get_key().key, context.numeric_param(), false)); } }, { { Key::Modifiers::Alt, 't' }, [](Context& context) { context.editor().select(std::bind(select_to_reverse, _1, get_key().key, context.numeric_param(), false)); } },
{ { Key::Modifiers::Alt, 'f' }, [](const Context& context) { context.editor().select(std::bind(select_to_reverse, _1, get_key().key, context.numeric_param(), true)); } }, { { Key::Modifiers::Alt, 'f' }, [](Context& context) { context.editor().select(std::bind(select_to_reverse, _1, get_key().key, context.numeric_param(), true)); } },
{ { Key::Modifiers::Alt, 'T' }, [](const Context& context) { context.editor().select(std::bind(select_to_reverse, _1, get_key().key, context.numeric_param(), false), true); } }, { { Key::Modifiers::Alt, 'T' }, [](Context& context) { context.editor().select(std::bind(select_to_reverse, _1, get_key().key, context.numeric_param(), false), true); } },
{ { Key::Modifiers::Alt, 'F' }, [](const Context& context) { context.editor().select(std::bind(select_to_reverse, _1, get_key().key, context.numeric_param(), true), true); } }, { { Key::Modifiers::Alt, 'F' }, [](Context& context) { context.editor().select(std::bind(select_to_reverse, _1, get_key().key, context.numeric_param(), true), true); } },
{ { Key::Modifiers::Alt, 'w' }, repeated([](const Context& context) { context.editor().select(select_to_next_word<true>); }) }, { { Key::Modifiers::Alt, 'w' }, repeated([](Context& context) { context.editor().select(select_to_next_word<true>); }) },
{ { Key::Modifiers::Alt, 'e' }, repeated([](const Context& context) { context.editor().select(select_to_next_word_end<true>); }) }, { { Key::Modifiers::Alt, 'e' }, repeated([](Context& context) { context.editor().select(select_to_next_word_end<true>); }) },
{ { Key::Modifiers::Alt, 'b' }, repeated([](const Context& context) { context.editor().select(select_to_previous_word<true>); }) }, { { Key::Modifiers::Alt, 'b' }, repeated([](Context& context) { context.editor().select(select_to_previous_word<true>); }) },
{ { Key::Modifiers::Alt, 'W' }, repeated([](const Context& context) { context.editor().select(select_to_next_word<true>, true); }) }, { { Key::Modifiers::Alt, 'W' }, repeated([](Context& context) { context.editor().select(select_to_next_word<true>, true); }) },
{ { Key::Modifiers::Alt, 'E' }, repeated([](const Context& context) { context.editor().select(select_to_next_word_end<true>, true); }) }, { { Key::Modifiers::Alt, 'E' }, repeated([](Context& context) { context.editor().select(select_to_next_word_end<true>, true); }) },
{ { Key::Modifiers::Alt, 'B' }, repeated([](const Context& context) { context.editor().select(select_to_previous_word<true>, true); }) }, { { Key::Modifiers::Alt, 'B' }, repeated([](Context& context) { context.editor().select(select_to_previous_word<true>, true); }) },
{ { Key::Modifiers::Alt, 'l' }, repeated([](const Context& context) { context.editor().select(select_to_eol, false); }) }, { { Key::Modifiers::Alt, 'l' }, repeated([](Context& context) { context.editor().select(select_to_eol, false); }) },
{ { Key::Modifiers::Alt, 'L' }, repeated([](const Context& context) { context.editor().select(select_to_eol, true); }) }, { { Key::Modifiers::Alt, 'L' }, repeated([](Context& context) { context.editor().select(select_to_eol, true); }) },
{ { Key::Modifiers::Alt, 'h' }, repeated([](const Context& context) { context.editor().select(select_to_eol_reverse, false); }) }, { { Key::Modifiers::Alt, 'h' }, repeated([](Context& context) { context.editor().select(select_to_eol_reverse, false); }) },
{ { Key::Modifiers::Alt, 'H' }, repeated([](const Context& context) { context.editor().select(select_to_eol_reverse, true); }) }, { { Key::Modifiers::Alt, 'H' }, repeated([](Context& context) { context.editor().select(select_to_eol_reverse, true); }) },
{ { Key::Modifiers::Alt, 's' }, do_split_regex }, { { Key::Modifiers::Alt, 's' }, do_split_regex },
{ { Key::Modifiers::Alt, 'j' }, do_join }, { { Key::Modifiers::Alt, 'j' }, do_join },
{ { Key::Modifiers::Alt, 'x' }, [](const Context& context) { context.editor().select(select_whole_lines); } }, { { Key::Modifiers::Alt, 'x' }, [](Context& context) { context.editor().select(select_whole_lines); } },
}; };
} }
@ -485,10 +484,11 @@ int main(int argc, char* argv[])
{ {
NCursesClient client; NCursesClient client;
current_client = &client; current_client = &client;
Context context;
try try
{ {
command_manager.execute("runtime kakrc", main_context); command_manager.execute("runtime kakrc", context);
} }
catch (Kakoune::runtime_error& error) catch (Kakoune::runtime_error& error)
{ {
@ -500,14 +500,14 @@ int main(int argc, char* argv[])
write_debug("utf-8 test: é á ï"); write_debug("utf-8 test: é á ï");
if (argc > 1) if (argc > 1)
command_manager.execute(String("edit ") + argv[1], main_context); command_manager.execute(String("edit ") + argv[1], context);
else else
{ {
auto buffer = new Buffer("*scratch*", Buffer::Type::Scratch); auto buffer = new Buffer("*scratch*", Buffer::Type::Scratch);
main_context = Context(*buffer->get_or_create_window()); context = Context(*buffer->get_or_create_window());
} }
current_client->draw_window(main_context.window()); current_client->draw_window(context.window());
int count = 0; int count = 0;
while(not quit_requested) while(not quit_requested)
{ {
@ -521,9 +521,9 @@ int main(int argc, char* argv[])
auto it = keymap.find(key); auto it = keymap.find(key);
if (it != keymap.end()) if (it != keymap.end())
{ {
main_context.numeric_param(count); context.numeric_param(count);
it->second(main_context); it->second(context);
current_client->draw_window(main_context.window()); current_client->draw_window(context.window());
} }
count = 0; count = 0;
} }
@ -540,6 +540,5 @@ int main(int argc, char* argv[])
puts(error.description().c_str()); puts(error.description().c_str());
return -1; return -1;
} }
main_context = Context();
return 0; return 0;
} }