Add a Context parameter to commands
This commit is contained in:
parent
417802cbdf
commit
957446dee4
|
@ -57,7 +57,8 @@ struct command_not_found : runtime_error
|
||||||
: runtime_error(command + " : no such command") {}
|
: runtime_error(command + " : no such command") {}
|
||||||
};
|
};
|
||||||
|
|
||||||
void CommandManager::execute(const std::string& command_line)
|
void CommandManager::execute(const std::string& command_line,
|
||||||
|
const Context& context)
|
||||||
{
|
{
|
||||||
TokenList tokens = split(command_line);
|
TokenList tokens = split(command_line);
|
||||||
if (tokens.empty())
|
if (tokens.empty())
|
||||||
|
@ -78,7 +79,18 @@ void CommandManager::execute(const std::string& command_line)
|
||||||
it->second - it->first));
|
it->second - it->first));
|
||||||
}
|
}
|
||||||
|
|
||||||
command_it->second.command(params);
|
command_it->second.command(params, context);
|
||||||
|
}
|
||||||
|
|
||||||
|
void CommandManager::execute(const std::string& command,
|
||||||
|
const CommandParameters& params,
|
||||||
|
const Context& context)
|
||||||
|
{
|
||||||
|
auto command_it = m_commands.find(command);
|
||||||
|
if (command_it == m_commands.end())
|
||||||
|
throw command_not_found(command);
|
||||||
|
|
||||||
|
command_it->second.command(params, context);
|
||||||
}
|
}
|
||||||
|
|
||||||
Completions CommandManager::complete(const std::string& command_line, size_t cursor_pos)
|
Completions CommandManager::complete(const std::string& command_line, size_t cursor_pos)
|
||||||
|
|
|
@ -13,13 +13,16 @@
|
||||||
namespace Kakoune
|
namespace Kakoune
|
||||||
{
|
{
|
||||||
|
|
||||||
|
struct Context;
|
||||||
|
|
||||||
struct wrong_argument_count : runtime_error
|
struct wrong_argument_count : runtime_error
|
||||||
{
|
{
|
||||||
wrong_argument_count() : runtime_error("wrong argument count") {}
|
wrong_argument_count() : runtime_error("wrong argument count") {}
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef std::vector<std::string> CommandParameters;
|
typedef std::vector<std::string> CommandParameters;
|
||||||
typedef std::function<void (const CommandParameters&)> Command;
|
typedef std::function<void (const CommandParameters&,
|
||||||
|
const Context& context)> Command;
|
||||||
|
|
||||||
typedef std::function<CandidateList (const CommandParameters&,
|
typedef std::function<CandidateList (const CommandParameters&,
|
||||||
size_t, size_t)> CommandCompleter;
|
size_t, size_t)> CommandCompleter;
|
||||||
|
@ -50,7 +53,10 @@ private:
|
||||||
class CommandManager : public Singleton<CommandManager>
|
class CommandManager : public Singleton<CommandManager>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
void execute(const std::string& command_line);
|
void execute(const std::string& command_line, const Context& context);
|
||||||
|
void execute(const std::string& command, const CommandParameters& params,
|
||||||
|
const Context& context);
|
||||||
|
|
||||||
Completions complete(const std::string& command_line, size_t cursor_pos);
|
Completions complete(const std::string& command_line, size_t cursor_pos);
|
||||||
|
|
||||||
void register_command(const std::string& command_name,
|
void register_command(const std::string& command_name,
|
||||||
|
|
23
src/context.hh
Normal file
23
src/context.hh
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
#ifndef context_hh_INCLUDED
|
||||||
|
#define context_hh_INCLUDED
|
||||||
|
|
||||||
|
#include "window.hh"
|
||||||
|
|
||||||
|
namespace Kakoune
|
||||||
|
{
|
||||||
|
|
||||||
|
struct Context
|
||||||
|
{
|
||||||
|
Window* window;
|
||||||
|
Buffer* buffer;
|
||||||
|
|
||||||
|
Context()
|
||||||
|
: window(nullptr), buffer(nullptr) {}
|
||||||
|
Context(Window& window)
|
||||||
|
: window(&window), buffer(&window.buffer()) {}
|
||||||
|
Context(Buffer& buffer)
|
||||||
|
: window(nullptr), buffer(&buffer) {}
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
#endif // context_hh_INCLUDED
|
36
src/main.cc
36
src/main.cc
|
@ -353,7 +353,7 @@ void do_go(Window& window, int count)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Window* current_window;
|
Context main_context;
|
||||||
|
|
||||||
Buffer* open_or_create(const std::string& filename)
|
Buffer* open_or_create(const std::string& filename)
|
||||||
{
|
{
|
||||||
|
@ -370,21 +370,21 @@ Buffer* open_or_create(const std::string& filename)
|
||||||
return buffer;
|
return buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
void edit(const CommandParameters& params)
|
void edit(const CommandParameters& params, const Context& context)
|
||||||
{
|
{
|
||||||
if (params.size() != 1)
|
if (params.size() != 1)
|
||||||
throw wrong_argument_count();
|
throw wrong_argument_count();
|
||||||
|
|
||||||
std::string filename = params[0];
|
std::string filename = params[0];
|
||||||
current_window = open_or_create(filename)->get_or_create_window();
|
main_context = Context(*open_or_create(filename)->get_or_create_window());
|
||||||
}
|
}
|
||||||
|
|
||||||
void write_buffer(const CommandParameters& params)
|
void write_buffer(const CommandParameters& params, const Context& context)
|
||||||
{
|
{
|
||||||
if (params.size() > 1)
|
if (params.size() > 1)
|
||||||
throw wrong_argument_count();
|
throw wrong_argument_count();
|
||||||
|
|
||||||
Buffer& buffer = current_window->buffer();
|
Buffer& buffer = context.window->buffer();
|
||||||
std::string filename = params.empty() ? buffer.name() : params[0];
|
std::string filename = params.empty() ? buffer.name() : params[0];
|
||||||
|
|
||||||
write_buffer_to_file(buffer, filename);
|
write_buffer_to_file(buffer, filename);
|
||||||
|
@ -394,7 +394,7 @@ void write_buffer(const CommandParameters& params)
|
||||||
bool quit_requested = false;
|
bool quit_requested = false;
|
||||||
|
|
||||||
template<bool force>
|
template<bool force>
|
||||||
void quit(const CommandParameters& params)
|
void quit(const CommandParameters& params, const Context& context)
|
||||||
{
|
{
|
||||||
if (params.size() != 0)
|
if (params.size() != 0)
|
||||||
throw wrong_argument_count();
|
throw wrong_argument_count();
|
||||||
|
@ -413,7 +413,7 @@ void quit(const CommandParameters& params)
|
||||||
quit_requested = true;
|
quit_requested = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void show_buffer(const CommandParameters& params)
|
void show_buffer(const CommandParameters& params, const Context& context)
|
||||||
{
|
{
|
||||||
if (params.size() != 1)
|
if (params.size() != 1)
|
||||||
throw wrong_argument_count();
|
throw wrong_argument_count();
|
||||||
|
@ -422,10 +422,10 @@ void show_buffer(const CommandParameters& params)
|
||||||
if (not buffer)
|
if (not buffer)
|
||||||
print_status("buffer " + params[0] + " does not exists");
|
print_status("buffer " + params[0] + " does not exists");
|
||||||
else
|
else
|
||||||
current_window = buffer->get_or_create_window();
|
main_context = Context(*buffer->get_or_create_window());
|
||||||
}
|
}
|
||||||
|
|
||||||
void add_filter(const CommandParameters& params)
|
void add_filter(const CommandParameters& params, const Context& context)
|
||||||
{
|
{
|
||||||
if (params.size() < 1)
|
if (params.size() < 1)
|
||||||
throw wrong_argument_count();
|
throw wrong_argument_count();
|
||||||
|
@ -434,7 +434,7 @@ void add_filter(const CommandParameters& params)
|
||||||
{
|
{
|
||||||
FilterRegistry& registry = FilterRegistry::instance();
|
FilterRegistry& registry = FilterRegistry::instance();
|
||||||
FilterParameters filter_params(params.begin()+1, params.end());
|
FilterParameters filter_params(params.begin()+1, params.end());
|
||||||
registry.add_filter_to_window(*current_window, params[0],
|
registry.add_filter_to_window(*context.window, params[0],
|
||||||
filter_params);
|
filter_params);
|
||||||
}
|
}
|
||||||
catch (runtime_error& err)
|
catch (runtime_error& err)
|
||||||
|
@ -443,12 +443,12 @@ void add_filter(const CommandParameters& params)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void rm_filter(const CommandParameters& params)
|
void rm_filter(const CommandParameters& params, const Context& context)
|
||||||
{
|
{
|
||||||
if (params.size() != 1)
|
if (params.size() != 1)
|
||||||
throw wrong_argument_count();
|
throw wrong_argument_count();
|
||||||
|
|
||||||
current_window->remove_filter(params[0]);
|
context.window->remove_filter(params[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
void do_command()
|
void do_command()
|
||||||
|
@ -459,7 +459,7 @@ void do_command()
|
||||||
&CommandManager::instance(),
|
&CommandManager::instance(),
|
||||||
_1, _2));
|
_1, _2));
|
||||||
|
|
||||||
CommandManager::instance().execute(cmdline);
|
CommandManager::instance().execute(cmdline, main_context);
|
||||||
}
|
}
|
||||||
catch (prompt_aborted&) {}
|
catch (prompt_aborted&) {}
|
||||||
}
|
}
|
||||||
|
@ -657,7 +657,7 @@ int main(int argc, char* argv[])
|
||||||
command_manager.register_command(std::vector<std::string>{ "rf", "rmfilter" }, rm_filter,
|
command_manager.register_command(std::vector<std::string>{ "rf", "rmfilter" }, rm_filter,
|
||||||
PerArgumentCommandCompleter {
|
PerArgumentCommandCompleter {
|
||||||
[&](const std::string& prefix, size_t cursor_pos)
|
[&](const std::string& prefix, size_t cursor_pos)
|
||||||
{ return current_window->complete_filterid(prefix, cursor_pos); }
|
{ return main_context.window->complete_filterid(prefix, cursor_pos); }
|
||||||
});
|
});
|
||||||
|
|
||||||
register_filters();
|
register_filters();
|
||||||
|
@ -665,9 +665,9 @@ int main(int argc, char* argv[])
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
auto buffer = (argc > 1) ? open_or_create(argv[1]) : new Buffer("*scratch*", Buffer::Type::Scratch);
|
auto buffer = (argc > 1) ? open_or_create(argv[1]) : new Buffer("*scratch*", Buffer::Type::Scratch);
|
||||||
current_window = buffer->get_or_create_window();
|
main_context = Context(*buffer->get_or_create_window());
|
||||||
|
|
||||||
draw_window(*current_window);
|
draw_window(*main_context.window);
|
||||||
int count = 0;
|
int count = 0;
|
||||||
while(not quit_requested)
|
while(not quit_requested)
|
||||||
{
|
{
|
||||||
|
@ -699,8 +699,8 @@ int main(int argc, char* argv[])
|
||||||
|
|
||||||
if (active_keymap.find(c) != active_keymap.end())
|
if (active_keymap.find(c) != active_keymap.end())
|
||||||
{
|
{
|
||||||
active_keymap[c](*current_window, count);
|
active_keymap[c](*main_context.window, count);
|
||||||
draw_window(*current_window);
|
draw_window(*main_context.window);
|
||||||
}
|
}
|
||||||
count = 0;
|
count = 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user