mirror of
https://github.com/mawww/kakoune.git
synced 2024-12-25 12:36:11 +03:00
rename Client to InputHandler
This commit is contained in:
parent
878a377673
commit
4aa3a36102
@ -7,7 +7,7 @@
|
||||
#include "buffer.hh"
|
||||
#include "window.hh"
|
||||
#include "file.hh"
|
||||
#include "client.hh"
|
||||
#include "input_handler.hh"
|
||||
#include "string.hh"
|
||||
#include "highlighter_registry.hh"
|
||||
#include "filter_registry.hh"
|
||||
@ -674,13 +674,13 @@ void exec_keys(const KeyList& keys, Context& context)
|
||||
RegisterRestorer slash('/', context);
|
||||
|
||||
BatchUI batch_ui(keys);
|
||||
Client batch_client;
|
||||
InputHandler batch_input_handler;
|
||||
|
||||
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())
|
||||
batch_client.handle_next_input(new_context);
|
||||
batch_input_handler.handle_next_input(new_context);
|
||||
context.change_editor(new_context.editor());
|
||||
}
|
||||
|
||||
@ -721,7 +721,7 @@ void menu(const CommandParameters& params, Context& context)
|
||||
commands.push_back(parser[i+1]);
|
||||
}
|
||||
|
||||
context.client().menu(choices,
|
||||
context.input_handler().menu(choices,
|
||||
[=](int choice, Context& context) {
|
||||
if (choice >= 0 and choice < commands.size())
|
||||
CommandManager::instance().execute(commands[choice], context);
|
||||
|
@ -2,7 +2,7 @@
|
||||
#define context_hh_INCLUDED
|
||||
|
||||
#include "window.hh"
|
||||
#include "client.hh"
|
||||
#include "input_handler.hh"
|
||||
#include "user_interface.hh"
|
||||
|
||||
namespace Kakoune
|
||||
@ -11,7 +11,7 @@ namespace Kakoune
|
||||
// A Context is used to access non singleton objects for various services
|
||||
// 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
|
||||
// a hook execution or a macro replay.
|
||||
struct Context
|
||||
@ -20,8 +20,8 @@ struct Context
|
||||
explicit Context(Editor& editor)
|
||||
: m_editor(&editor) {}
|
||||
|
||||
Context(Client& client, Editor& editor, UserInterface& ui)
|
||||
: m_client(&client), m_editor(&editor), m_ui(&ui) {}
|
||||
Context(InputHandler& input_handler, Editor& editor, UserInterface& ui)
|
||||
: m_input_handler(&input_handler), m_editor(&editor), m_ui(&ui) {}
|
||||
|
||||
// to allow func(Context(Editor(...)))
|
||||
// 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()); }
|
||||
|
||||
Client& client() const
|
||||
InputHandler& input_handler() const
|
||||
{
|
||||
if (not has_client())
|
||||
throw runtime_error("no client in context");
|
||||
return *m_client;
|
||||
if (not has_input_handler())
|
||||
throw runtime_error("no input handler in context");
|
||||
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
|
||||
{
|
||||
@ -109,7 +109,7 @@ struct Context
|
||||
int& numeric_param() { return m_numeric_param; }
|
||||
private:
|
||||
safe_ptr<Editor> m_editor;
|
||||
safe_ptr<Client> m_client;
|
||||
safe_ptr<InputHandler> m_input_handler;
|
||||
safe_ptr<UserInterface> m_ui;
|
||||
|
||||
Insertion m_last_insert = {InsertMode::Insert, {}};
|
||||
|
@ -1,4 +1,4 @@
|
||||
#include "client.hh"
|
||||
#include "input_handler.hh"
|
||||
|
||||
#include "context.hh"
|
||||
#include "editor.hh"
|
||||
@ -12,29 +12,29 @@ namespace Kakoune
|
||||
|
||||
extern std::unordered_map<Key, std::function<void (Context& context)>> keymap;
|
||||
|
||||
class ClientMode
|
||||
class InputMode
|
||||
{
|
||||
public:
|
||||
ClientMode(Client& client) : m_client(client) {}
|
||||
virtual ~ClientMode() {}
|
||||
ClientMode(const ClientMode&) = delete;
|
||||
ClientMode& operator=(const ClientMode&) = delete;
|
||||
InputMode(InputHandler& input_handler) : m_input_handler(input_handler) {}
|
||||
virtual ~InputMode() {}
|
||||
InputMode(const InputMode&) = delete;
|
||||
InputMode& operator=(const InputMode&) = delete;
|
||||
|
||||
virtual void on_key(const Key& key, Context& context) = 0;
|
||||
protected:
|
||||
void reset_normal_mode();
|
||||
private:
|
||||
Client& m_client;
|
||||
InputHandler& m_input_handler;
|
||||
};
|
||||
|
||||
namespace ClientModes
|
||||
namespace InputModes
|
||||
{
|
||||
|
||||
class Normal : public ClientMode
|
||||
class Normal : public InputMode
|
||||
{
|
||||
public:
|
||||
Normal(Client& client)
|
||||
: ClientMode(client)
|
||||
Normal(InputHandler& input_handler)
|
||||
: InputMode(input_handler)
|
||||
{
|
||||
}
|
||||
|
||||
@ -126,12 +126,12 @@ private:
|
||||
String m_line;
|
||||
};
|
||||
|
||||
class Menu : public ClientMode
|
||||
class Menu : public InputMode
|
||||
{
|
||||
public:
|
||||
Menu(Context& context, const memoryview<String>& choices,
|
||||
MenuCallback callback)
|
||||
: ClientMode(context.client()),
|
||||
: InputMode(context.input_handler()),
|
||||
m_callback(callback), m_choices(choices.begin(), choices.end()),
|
||||
m_selected(m_choices.begin())
|
||||
{
|
||||
@ -228,12 +228,12 @@ private:
|
||||
LineEditor m_filter_editor;
|
||||
};
|
||||
|
||||
class Prompt : public ClientMode
|
||||
class Prompt : public InputMode
|
||||
{
|
||||
public:
|
||||
Prompt(Context& context, const String& prompt,
|
||||
Completer completer, PromptCallback callback)
|
||||
: ClientMode(context.client()), m_prompt(prompt),
|
||||
: InputMode(context.input_handler()), m_prompt(prompt),
|
||||
m_completer(completer), m_callback(callback)
|
||||
{
|
||||
m_history_it = ms_history[m_prompt].end();
|
||||
@ -372,11 +372,11 @@ private:
|
||||
};
|
||||
std::unordered_map<String, std::vector<String>> Prompt::ms_history;
|
||||
|
||||
class NextKey : public ClientMode
|
||||
class NextKey : public InputMode
|
||||
{
|
||||
public:
|
||||
NextKey(Client& client, KeyCallback callback)
|
||||
: ClientMode(client), m_callback(callback) {}
|
||||
NextKey(InputHandler& input_handler, KeyCallback callback)
|
||||
: InputMode(input_handler), m_callback(callback) {}
|
||||
|
||||
void on_key(const Key& key, Context& context) override
|
||||
{
|
||||
@ -475,11 +475,11 @@ String codepoint_to_str(Codepoint cp)
|
||||
return String(str);
|
||||
}
|
||||
|
||||
class Insert : public ClientMode
|
||||
class Insert : public InputMode
|
||||
{
|
||||
public:
|
||||
Insert(Context& context, InsertMode mode)
|
||||
: ClientMode(context.client()),
|
||||
: InputMode(context.input_handler()),
|
||||
m_inserter(context.editor(), 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()
|
||||
: m_mode(new ClientModes::Normal(*this))
|
||||
InputHandler::InputHandler()
|
||||
: 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);
|
||||
m_mode.reset(new ClientModes::Insert(context, mode));
|
||||
assert(&context.input_handler() == this);
|
||||
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();
|
||||
if (last_insert.second.empty())
|
||||
return;
|
||||
@ -600,32 +600,32 @@ void Client::repeat_last_insert(Context& context)
|
||||
swap(keys, last_insert.second);
|
||||
// context.last_insert will be refilled by the new Insert
|
||||
// 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)
|
||||
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)
|
||||
{
|
||||
assert(&context.client() == this);
|
||||
m_mode.reset(new ClientModes::Prompt(context, prompt, completer, callback));
|
||||
assert(&context.input_handler() == this);
|
||||
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)
|
||||
{
|
||||
assert(&context.client() == this);
|
||||
m_mode.reset(new ClientModes::Menu(context, choices, callback));
|
||||
assert(&context.input_handler() == this);
|
||||
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);
|
||||
context.draw_ifn();
|
@ -1,5 +1,5 @@
|
||||
#ifndef client_hh_INCLUDED
|
||||
#define client_hh_INCLUDED
|
||||
#ifndef input_handler_hh_INCLUDED
|
||||
#define input_handler_hh_INCLUDED
|
||||
|
||||
#include "keys.hh"
|
||||
#include "completion.hh"
|
||||
@ -16,14 +16,14 @@ using MenuCallback = std::function<void (int, Context&)>;
|
||||
using PromptCallback = std::function<void (const String&, Context&)>;
|
||||
using KeyCallback = std::function<void (const Key&, Context&)>;
|
||||
|
||||
class ClientMode;
|
||||
class InputMode;
|
||||
enum class InsertMode : unsigned;
|
||||
|
||||
class Client : public SafeCountable
|
||||
class InputHandler : public SafeCountable
|
||||
{
|
||||
public:
|
||||
Client();
|
||||
~Client();
|
||||
InputHandler();
|
||||
~InputHandler();
|
||||
|
||||
void insert(Context& context, InsertMode mode);
|
||||
void repeat_last_insert(Context& context);
|
||||
@ -39,12 +39,12 @@ public:
|
||||
void handle_next_input(Context& context);
|
||||
|
||||
private:
|
||||
friend class ClientMode;
|
||||
std::unique_ptr<ClientMode> m_mode;
|
||||
friend class InputMode;
|
||||
std::unique_ptr<InputMode> m_mode;
|
||||
};
|
||||
|
||||
struct prompt_aborted {};
|
||||
|
||||
}
|
||||
|
||||
#endif // client_hh_INCLUDED
|
||||
#endif // input_handler_hh_INCLUDED
|
28
src/main.cc
28
src/main.cc
@ -40,12 +40,12 @@ bool quit_requested = false;
|
||||
template<InsertMode mode>
|
||||
void do_insert(Context& context)
|
||||
{
|
||||
context.client().insert(context, mode);
|
||||
context.input_handler().insert(context, mode);
|
||||
}
|
||||
|
||||
void do_repeat_insert(Context& context)
|
||||
{
|
||||
context.client().repeat_last_insert(context);
|
||||
context.input_handler().repeat_last_insert(context);
|
||||
}
|
||||
|
||||
template<SelectMode mode>
|
||||
@ -62,7 +62,7 @@ void do_go(Context& context)
|
||||
context.window().center_selection();
|
||||
}
|
||||
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)
|
||||
return;
|
||||
|
||||
@ -93,14 +93,14 @@ void do_go(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);
|
||||
});
|
||||
}
|
||||
|
||||
void do_command(Context& context)
|
||||
{
|
||||
context.client().prompt(
|
||||
context.input_handler().prompt(
|
||||
":", std::bind(&CommandManager::complete, &CommandManager::instance(), _1, _2, _3),
|
||||
[](const String& cmdline, Context& context) { CommandManager::instance().execute(cmdline, context); },
|
||||
context);
|
||||
@ -108,7 +108,7 @@ void do_command(Context& context)
|
||||
|
||||
void do_pipe(Context& context)
|
||||
{
|
||||
context.client().prompt("|", complete_nothing,
|
||||
context.input_handler().prompt("|", complete_nothing,
|
||||
[](const String& cmdline, Context& context)
|
||||
{
|
||||
Editor& editor = context.editor();
|
||||
@ -124,7 +124,7 @@ void do_pipe(Context& context)
|
||||
template<SelectMode mode>
|
||||
void do_search(Context& context)
|
||||
{
|
||||
context.client().prompt("/", complete_nothing,
|
||||
context.input_handler().prompt("/", complete_nothing,
|
||||
[](const String& str, Context& context) {
|
||||
String ex = str;
|
||||
if (ex.empty())
|
||||
@ -204,7 +204,7 @@ void do_paste(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)
|
||||
{ context.editor().multi_select(std::bind(select_all_matches, _1, ex)); },
|
||||
context);
|
||||
@ -212,7 +212,7 @@ void do_select_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)
|
||||
{ context.editor().multi_select(std::bind(split_selection, _1, ex)); },
|
||||
context);
|
||||
@ -232,7 +232,7 @@ void do_join(Context& context)
|
||||
template<bool inner>
|
||||
void do_select_object(Context& context)
|
||||
{
|
||||
context.client().on_next_key(
|
||||
context.input_handler().on_next_key(
|
||||
[](const Key& key, Context& context) {
|
||||
typedef std::function<SelectionAndCaptures (const Selection&)> 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)
|
||||
{
|
||||
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(
|
||||
std::bind(flags & SelectFlags::Reverse ? select_to_reverse : select_to,
|
||||
_1, key.key, param, flags & SelectFlags::Inclusive),
|
||||
@ -500,7 +500,7 @@ int main(int argc, char* argv[])
|
||||
|
||||
try
|
||||
{
|
||||
Client client;
|
||||
InputHandler input_handler;
|
||||
NCursesUI ui;
|
||||
|
||||
try
|
||||
@ -530,11 +530,11 @@ int main(int argc, char* argv[])
|
||||
else
|
||||
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) {
|
||||
try
|
||||
{
|
||||
client.handle_next_input(context);
|
||||
input_handler.handle_next_input(context);
|
||||
}
|
||||
catch (Kakoune::runtime_error& error)
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user