rename Client to InputHandler

This commit is contained in:
Maxime Coste 2012-10-17 13:14:03 +02:00
parent 878a377673
commit 4aa3a36102
5 changed files with 79 additions and 79 deletions

View File

@ -7,7 +7,7 @@
#include "buffer.hh" #include "buffer.hh"
#include "window.hh" #include "window.hh"
#include "file.hh" #include "file.hh"
#include "client.hh" #include "input_handler.hh"
#include "string.hh" #include "string.hh"
#include "highlighter_registry.hh" #include "highlighter_registry.hh"
#include "filter_registry.hh" #include "filter_registry.hh"
@ -674,13 +674,13 @@ void exec_keys(const KeyList& keys, Context& context)
RegisterRestorer slash('/', context); RegisterRestorer slash('/', context);
BatchUI batch_ui(keys); BatchUI batch_ui(keys);
Client batch_client; InputHandler batch_input_handler;
scoped_edition edition(context.editor()); scoped_edition edition(context.editor());
Context new_context(batch_client, context.editor(), batch_ui); Context new_context(batch_input_handler, context.editor(), batch_ui);
while (batch_ui.has_key_left()) while (batch_ui.has_key_left())
batch_client.handle_next_input(new_context); batch_input_handler.handle_next_input(new_context);
context.change_editor(new_context.editor()); context.change_editor(new_context.editor());
} }
@ -721,7 +721,7 @@ void menu(const CommandParameters& params, Context& context)
commands.push_back(parser[i+1]); commands.push_back(parser[i+1]);
} }
context.client().menu(choices, context.input_handler().menu(choices,
[=](int choice, Context& context) { [=](int choice, Context& context) {
if (choice >= 0 and choice < commands.size()) if (choice >= 0 and choice < commands.size())
CommandManager::instance().execute(commands[choice], context); CommandManager::instance().execute(commands[choice], context);

View File

@ -2,7 +2,7 @@
#define context_hh_INCLUDED #define context_hh_INCLUDED
#include "window.hh" #include "window.hh"
#include "client.hh" #include "input_handler.hh"
#include "user_interface.hh" #include "user_interface.hh"
namespace Kakoune namespace Kakoune
@ -11,7 +11,7 @@ namespace Kakoune
// A Context is used to access non singleton objects for various services // A Context is used to access non singleton objects for various services
// in commands. // in commands.
// //
// The Context object links a Client, an Editor (which may be a Window), // The Context object links an InputHandler, an Editor (which may be a Window),
// and a UserInterface. It may represent an interactive user window, or // and a UserInterface. It may represent an interactive user window, or
// a hook execution or a macro replay. // a hook execution or a macro replay.
struct Context struct Context
@ -20,8 +20,8 @@ struct Context
explicit Context(Editor& editor) explicit Context(Editor& editor)
: m_editor(&editor) {} : m_editor(&editor) {}
Context(Client& client, Editor& editor, UserInterface& ui) Context(InputHandler& input_handler, Editor& editor, UserInterface& ui)
: m_client(&client), m_editor(&editor), m_ui(&ui) {} : m_input_handler(&input_handler), m_editor(&editor), m_ui(&ui) {}
// to allow func(Context(Editor(...))) // to allow func(Context(Editor(...)))
// make sure the context will not survive the next ';' // make sure the context will not survive the next ';'
@ -56,13 +56,13 @@ struct Context
} }
bool has_window() const { return (bool)m_editor and dynamic_cast<Window*>(m_editor.get()); } bool has_window() const { return (bool)m_editor and dynamic_cast<Window*>(m_editor.get()); }
Client& client() const InputHandler& input_handler() const
{ {
if (not has_client()) if (not has_input_handler())
throw runtime_error("no client in context"); throw runtime_error("no input handler in context");
return *m_client; return *m_input_handler;
} }
bool has_client() const { return (bool)m_client; } bool has_input_handler() const { return (bool)m_input_handler; }
UserInterface& ui() const UserInterface& ui() const
{ {
@ -109,7 +109,7 @@ struct Context
int& numeric_param() { return m_numeric_param; } int& numeric_param() { return m_numeric_param; }
private: private:
safe_ptr<Editor> m_editor; safe_ptr<Editor> m_editor;
safe_ptr<Client> m_client; safe_ptr<InputHandler> m_input_handler;
safe_ptr<UserInterface> m_ui; safe_ptr<UserInterface> m_ui;
Insertion m_last_insert = {InsertMode::Insert, {}}; Insertion m_last_insert = {InsertMode::Insert, {}};

View File

@ -1,4 +1,4 @@
#include "client.hh" #include "input_handler.hh"
#include "context.hh" #include "context.hh"
#include "editor.hh" #include "editor.hh"
@ -12,29 +12,29 @@ namespace Kakoune
extern std::unordered_map<Key, std::function<void (Context& context)>> keymap; extern std::unordered_map<Key, std::function<void (Context& context)>> keymap;
class ClientMode class InputMode
{ {
public: public:
ClientMode(Client& client) : m_client(client) {} InputMode(InputHandler& input_handler) : m_input_handler(input_handler) {}
virtual ~ClientMode() {} virtual ~InputMode() {}
ClientMode(const ClientMode&) = delete; InputMode(const InputMode&) = delete;
ClientMode& operator=(const ClientMode&) = delete; InputMode& operator=(const InputMode&) = delete;
virtual void on_key(const Key& key, Context& context) = 0; virtual void on_key(const Key& key, Context& context) = 0;
protected: protected:
void reset_normal_mode(); void reset_normal_mode();
private: private:
Client& m_client; InputHandler& m_input_handler;
}; };
namespace ClientModes namespace InputModes
{ {
class Normal : public ClientMode class Normal : public InputMode
{ {
public: public:
Normal(Client& client) Normal(InputHandler& input_handler)
: ClientMode(client) : InputMode(input_handler)
{ {
} }
@ -126,12 +126,12 @@ private:
String m_line; String m_line;
}; };
class Menu : public ClientMode class Menu : public InputMode
{ {
public: public:
Menu(Context& context, const memoryview<String>& choices, Menu(Context& context, const memoryview<String>& choices,
MenuCallback callback) MenuCallback callback)
: ClientMode(context.client()), : InputMode(context.input_handler()),
m_callback(callback), m_choices(choices.begin(), choices.end()), m_callback(callback), m_choices(choices.begin(), choices.end()),
m_selected(m_choices.begin()) m_selected(m_choices.begin())
{ {
@ -228,12 +228,12 @@ private:
LineEditor m_filter_editor; LineEditor m_filter_editor;
}; };
class Prompt : public ClientMode class Prompt : public InputMode
{ {
public: public:
Prompt(Context& context, const String& prompt, Prompt(Context& context, const String& prompt,
Completer completer, PromptCallback callback) Completer completer, PromptCallback callback)
: ClientMode(context.client()), m_prompt(prompt), : InputMode(context.input_handler()), m_prompt(prompt),
m_completer(completer), m_callback(callback) m_completer(completer), m_callback(callback)
{ {
m_history_it = ms_history[m_prompt].end(); m_history_it = ms_history[m_prompt].end();
@ -372,11 +372,11 @@ private:
}; };
std::unordered_map<String, std::vector<String>> Prompt::ms_history; std::unordered_map<String, std::vector<String>> Prompt::ms_history;
class NextKey : public ClientMode class NextKey : public InputMode
{ {
public: public:
NextKey(Client& client, KeyCallback callback) NextKey(InputHandler& input_handler, KeyCallback callback)
: ClientMode(client), m_callback(callback) {} : InputMode(input_handler), m_callback(callback) {}
void on_key(const Key& key, Context& context) override void on_key(const Key& key, Context& context) override
{ {
@ -475,11 +475,11 @@ String codepoint_to_str(Codepoint cp)
return String(str); return String(str);
} }
class Insert : public ClientMode class Insert : public InputMode
{ {
public: public:
Insert(Context& context, InsertMode mode) Insert(Context& context, InsertMode mode)
: ClientMode(context.client()), : InputMode(context.input_handler()),
m_inserter(context.editor(), mode) m_inserter(context.editor(), mode)
{ {
context.last_insert().first = mode; context.last_insert().first = mode;
@ -568,30 +568,30 @@ private:
} }
void ClientMode::reset_normal_mode() void InputMode::reset_normal_mode()
{ {
m_client.m_mode.reset(new ClientModes::Normal(m_client)); m_input_handler.m_mode.reset(new InputModes::Normal(m_input_handler));
} }
Client::Client() InputHandler::InputHandler()
: m_mode(new ClientModes::Normal(*this)) : m_mode(new InputModes::Normal(*this))
{ {
} }
Client::~Client() InputHandler::~InputHandler()
{ {
} }
void Client::insert(Context& context, InsertMode mode) void InputHandler::insert(Context& context, InsertMode mode)
{ {
assert(&context.client() == this); assert(&context.input_handler() == this);
m_mode.reset(new ClientModes::Insert(context, mode)); m_mode.reset(new InputModes::Insert(context, mode));
} }
void Client::repeat_last_insert(Context& context) void InputHandler::repeat_last_insert(Context& context)
{ {
assert(&context.client() == this); assert(&context.input_handler() == this);
Context::Insertion& last_insert = context.last_insert(); Context::Insertion& last_insert = context.last_insert();
if (last_insert.second.empty()) if (last_insert.second.empty())
return; return;
@ -600,32 +600,32 @@ void Client::repeat_last_insert(Context& context)
swap(keys, last_insert.second); swap(keys, last_insert.second);
// context.last_insert will be refilled by the new Insert // context.last_insert will be refilled by the new Insert
// this is very inefficient. // this is very inefficient.
m_mode.reset(new ClientModes::Insert(context, last_insert.first)); m_mode.reset(new InputModes::Insert(context, last_insert.first));
for (auto& key : keys) for (auto& key : keys)
m_mode->on_key(key, context); m_mode->on_key(key, context);
assert(dynamic_cast<ClientModes::Normal*>(m_mode.get()) != nullptr); assert(dynamic_cast<InputModes::Normal*>(m_mode.get()) != nullptr);
} }
void Client::prompt(const String& prompt, Completer completer, void InputHandler::prompt(const String& prompt, Completer completer,
PromptCallback callback, Context& context) PromptCallback callback, Context& context)
{ {
assert(&context.client() == this); assert(&context.input_handler() == this);
m_mode.reset(new ClientModes::Prompt(context, prompt, completer, callback)); m_mode.reset(new InputModes::Prompt(context, prompt, completer, callback));
} }
void Client::menu(const memoryview<String>& choices, void InputHandler::menu(const memoryview<String>& choices,
MenuCallback callback, Context& context) MenuCallback callback, Context& context)
{ {
assert(&context.client() == this); assert(&context.input_handler() == this);
m_mode.reset(new ClientModes::Menu(context, choices, callback)); m_mode.reset(new InputModes::Menu(context, choices, callback));
} }
void Client::on_next_key(KeyCallback callback) void InputHandler::on_next_key(KeyCallback callback)
{ {
m_mode.reset(new ClientModes::NextKey(*this, callback)); m_mode.reset(new InputModes::NextKey(*this, callback));
} }
void Client::handle_next_input(Context& context) void InputHandler::handle_next_input(Context& context)
{ {
m_mode->on_key(context.ui().get_key(), context); m_mode->on_key(context.ui().get_key(), context);
context.draw_ifn(); context.draw_ifn();

View File

@ -1,5 +1,5 @@
#ifndef client_hh_INCLUDED #ifndef input_handler_hh_INCLUDED
#define client_hh_INCLUDED #define input_handler_hh_INCLUDED
#include "keys.hh" #include "keys.hh"
#include "completion.hh" #include "completion.hh"
@ -16,14 +16,14 @@ using MenuCallback = std::function<void (int, Context&)>;
using PromptCallback = std::function<void (const String&, Context&)>; using PromptCallback = std::function<void (const String&, Context&)>;
using KeyCallback = std::function<void (const Key&, Context&)>; using KeyCallback = std::function<void (const Key&, Context&)>;
class ClientMode; class InputMode;
enum class InsertMode : unsigned; enum class InsertMode : unsigned;
class Client : public SafeCountable class InputHandler : public SafeCountable
{ {
public: public:
Client(); InputHandler();
~Client(); ~InputHandler();
void insert(Context& context, InsertMode mode); void insert(Context& context, InsertMode mode);
void repeat_last_insert(Context& context); void repeat_last_insert(Context& context);
@ -39,12 +39,12 @@ public:
void handle_next_input(Context& context); void handle_next_input(Context& context);
private: private:
friend class ClientMode; friend class InputMode;
std::unique_ptr<ClientMode> m_mode; std::unique_ptr<InputMode> m_mode;
}; };
struct prompt_aborted {}; struct prompt_aborted {};
} }
#endif // client_hh_INCLUDED #endif // input_handler_hh_INCLUDED

View File

@ -40,12 +40,12 @@ bool quit_requested = false;
template<InsertMode mode> template<InsertMode mode>
void do_insert(Context& context) void do_insert(Context& context)
{ {
context.client().insert(context, mode); context.input_handler().insert(context, mode);
} }
void do_repeat_insert(Context& context) void do_repeat_insert(Context& context)
{ {
context.client().repeat_last_insert(context); context.input_handler().repeat_last_insert(context);
} }
template<SelectMode mode> template<SelectMode mode>
@ -62,7 +62,7 @@ void do_go(Context& context)
context.window().center_selection(); context.window().center_selection();
} }
else else
context.client().on_next_key([](const Key& key, Context& context) { context.input_handler().on_next_key([](const Key& key, Context& context) {
if (key.modifiers != Key::Modifiers::None) if (key.modifiers != Key::Modifiers::None)
return; return;
@ -93,14 +93,14 @@ void do_go(Context& context)
void do_replace_with_char(Context& context) void do_replace_with_char(Context& context)
{ {
context.client().on_next_key([](const Key& key, Context& context) { context.input_handler().on_next_key([](const Key& key, Context& context) {
context.editor().insert(String() + key.key, InsertMode::Replace); context.editor().insert(String() + key.key, InsertMode::Replace);
}); });
} }
void do_command(Context& context) void do_command(Context& context)
{ {
context.client().prompt( context.input_handler().prompt(
":", std::bind(&CommandManager::complete, &CommandManager::instance(), _1, _2, _3), ":", std::bind(&CommandManager::complete, &CommandManager::instance(), _1, _2, _3),
[](const String& cmdline, Context& context) { CommandManager::instance().execute(cmdline, context); }, [](const String& cmdline, Context& context) { CommandManager::instance().execute(cmdline, context); },
context); context);
@ -108,7 +108,7 @@ void do_command(Context& context)
void do_pipe(Context& context) void do_pipe(Context& context)
{ {
context.client().prompt("|", complete_nothing, context.input_handler().prompt("|", complete_nothing,
[](const String& cmdline, Context& context) [](const String& cmdline, Context& context)
{ {
Editor& editor = context.editor(); Editor& editor = context.editor();
@ -124,7 +124,7 @@ void do_pipe(Context& context)
template<SelectMode mode> template<SelectMode mode>
void do_search(Context& context) void do_search(Context& context)
{ {
context.client().prompt("/", complete_nothing, context.input_handler().prompt("/", complete_nothing,
[](const String& str, Context& context) { [](const String& str, Context& context) {
String ex = str; String ex = str;
if (ex.empty()) if (ex.empty())
@ -204,7 +204,7 @@ void do_paste(Context& context)
void do_select_regex(Context& context) void do_select_regex(Context& context)
{ {
context.client().prompt("select: ", complete_nothing, context.input_handler().prompt("select: ", complete_nothing,
[](const String& ex, Context& context) [](const String& ex, Context& context)
{ context.editor().multi_select(std::bind(select_all_matches, _1, ex)); }, { context.editor().multi_select(std::bind(select_all_matches, _1, ex)); },
context); context);
@ -212,7 +212,7 @@ void do_select_regex(Context& context)
void do_split_regex(Context& context) void do_split_regex(Context& context)
{ {
context.client().prompt("select: ", complete_nothing, context.input_handler().prompt("select: ", complete_nothing,
[](const String& ex, Context& context) [](const String& ex, Context& context)
{ context.editor().multi_select(std::bind(split_selection, _1, ex)); }, { context.editor().multi_select(std::bind(split_selection, _1, ex)); },
context); context);
@ -232,7 +232,7 @@ void do_join(Context& context)
template<bool inner> template<bool inner>
void do_select_object(Context& context) void do_select_object(Context& context)
{ {
context.client().on_next_key( context.input_handler().on_next_key(
[](const Key& key, Context& context) { [](const Key& key, 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 =
@ -317,7 +317,7 @@ template<int flags>
void select_to_next_char(Context& context) void select_to_next_char(Context& context)
{ {
int param = context.numeric_param(); int param = context.numeric_param();
context.client().on_next_key([param](const Key& key, Context& context) { context.input_handler().on_next_key([param](const Key& key, Context& context) {
context.editor().select( context.editor().select(
std::bind(flags & SelectFlags::Reverse ? select_to_reverse : select_to, std::bind(flags & SelectFlags::Reverse ? select_to_reverse : select_to,
_1, key.key, param, flags & SelectFlags::Inclusive), _1, key.key, param, flags & SelectFlags::Inclusive),
@ -500,7 +500,7 @@ int main(int argc, char* argv[])
try try
{ {
Client client; InputHandler input_handler;
NCursesUI ui; NCursesUI ui;
try try
@ -530,11 +530,11 @@ int main(int argc, char* argv[])
else else
buffer = new Buffer("*scratch*", Buffer::Type::Scratch); buffer = new Buffer("*scratch*", Buffer::Type::Scratch);
Context context(client, *buffer->get_or_create_window(), ui); Context context(input_handler, *buffer->get_or_create_window(), ui);
event_manager.watch(0, [&](int) { event_manager.watch(0, [&](int) {
try try
{ {
client.handle_next_input(context); input_handler.handle_next_input(context);
} }
catch (Kakoune::runtime_error& error) catch (Kakoune::runtime_error& error)
{ {