Add a Context parameter to commands

This commit is contained in:
Maxime Coste 2011-11-26 18:32:57 +00:00
parent 417802cbdf
commit 957446dee4
4 changed files with 63 additions and 22 deletions

View File

@ -57,7 +57,8 @@ struct command_not_found : runtime_error
: 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);
if (tokens.empty())
@ -78,7 +79,18 @@ void CommandManager::execute(const std::string& command_line)
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)

View File

@ -13,13 +13,16 @@
namespace Kakoune
{
struct Context;
struct wrong_argument_count : runtime_error
{
wrong_argument_count() : runtime_error("wrong argument count") {}
};
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&,
size_t, size_t)> CommandCompleter;
@ -50,7 +53,10 @@ private:
class CommandManager : public Singleton<CommandManager>
{
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);
void register_command(const std::string& command_name,

23
src/context.hh Normal file
View 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

View File

@ -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)
{
@ -370,21 +370,21 @@ Buffer* open_or_create(const std::string& filename)
return buffer;
}
void edit(const CommandParameters& params)
void edit(const CommandParameters& params, const Context& context)
{
if (params.size() != 1)
throw wrong_argument_count();
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)
throw wrong_argument_count();
Buffer& buffer = current_window->buffer();
Buffer& buffer = context.window->buffer();
std::string filename = params.empty() ? buffer.name() : params[0];
write_buffer_to_file(buffer, filename);
@ -394,7 +394,7 @@ void write_buffer(const CommandParameters& params)
bool quit_requested = false;
template<bool force>
void quit(const CommandParameters& params)
void quit(const CommandParameters& params, const Context& context)
{
if (params.size() != 0)
throw wrong_argument_count();
@ -413,7 +413,7 @@ void quit(const CommandParameters& params)
quit_requested = true;
}
void show_buffer(const CommandParameters& params)
void show_buffer(const CommandParameters& params, const Context& context)
{
if (params.size() != 1)
throw wrong_argument_count();
@ -422,10 +422,10 @@ void show_buffer(const CommandParameters& params)
if (not buffer)
print_status("buffer " + params[0] + " does not exists");
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)
throw wrong_argument_count();
@ -434,7 +434,7 @@ void add_filter(const CommandParameters& params)
{
FilterRegistry& registry = FilterRegistry::instance();
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);
}
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)
throw wrong_argument_count();
current_window->remove_filter(params[0]);
context.window->remove_filter(params[0]);
}
void do_command()
@ -459,7 +459,7 @@ void do_command()
&CommandManager::instance(),
_1, _2));
CommandManager::instance().execute(cmdline);
CommandManager::instance().execute(cmdline, main_context);
}
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,
PerArgumentCommandCompleter {
[&](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();
@ -665,9 +665,9 @@ int main(int argc, char* argv[])
try
{
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;
while(not quit_requested)
{
@ -699,8 +699,8 @@ int main(int argc, char* argv[])
if (active_keymap.find(c) != active_keymap.end())
{
active_keymap[c](*current_window, count);
draw_window(*current_window);
active_keymap[c](*main_context.window, count);
draw_window(*main_context.window);
}
count = 0;
}