1
1
mirror of https://github.com/mawww/kakoune.git synced 2024-12-22 19:11:40 +03:00
kakoune/src/client.cc

279 lines
8.3 KiB
C++
Raw Normal View History

2013-09-13 01:47:23 +04:00
#include "client.hh"
#include "face_registry.hh"
#include "context.hh"
#include "buffer_manager.hh"
#include "user_interface.hh"
#include "file.hh"
#include "remote.hh"
#include "client_manager.hh"
#include "event_manager.hh"
#include "window.hh"
#include <signal.h>
#include <unistd.h>
namespace Kakoune
{
Client::Client(std::unique_ptr<UserInterface>&& ui,
std::unique_ptr<Window>&& window,
SelectionList selections,
EnvVarMap env_vars,
String name)
: m_ui{std::move(ui)}, m_window{std::move(window)},
m_input_handler{std::move(selections), Context::Flags::None,
std::move(name)},
m_env_vars(env_vars)
2013-11-14 22:09:15 +04:00
{
context().set_client(*this);
context().set_window(*m_window);
m_window->options().register_watcher(*this);
m_ui->set_ui_options(m_window->options()["ui_options"].get<UserInterface::Options>());
2013-11-14 22:09:15 +04:00
}
Client::~Client()
{
m_window->options().unregister_watcher(*this);
2013-11-14 22:09:15 +04:00
}
Optional<Key> Client::get_next_key(EventMode mode)
{
if (not m_pending_keys.empty())
{
Key key = m_pending_keys.front();
m_pending_keys.erase(m_pending_keys.begin());
return key;
}
if (mode != EventMode::Pending and m_ui->is_key_available())
return m_ui->get_key();
return {};
}
void Client::handle_available_input(EventMode mode)
2013-11-14 22:09:15 +04:00
{
if (mode == EventMode::Urgent)
{
Key key = m_ui->get_key();
if (key == ctrl('c'))
killpg(getpgrp(), SIGINT);
else
m_pending_keys.push_back(key);
2015-07-22 15:30:03 +03:00
return;
}
2015-07-22 15:30:03 +03:00
try
{
2015-07-22 15:30:03 +03:00
while (Optional<Key> key = get_next_key(mode))
{
2015-07-22 15:30:03 +03:00
if (*key == ctrl('c'))
killpg(getpgrp(), SIGINT);
else if (*key == Key::FocusIn)
context().hooks().run_hook("FocusIn", context().name(), context());
else if (*key == Key::FocusOut)
context().hooks().run_hook("FocusOut", context().name(), context());
2015-08-23 17:18:18 +03:00
else if (key->modifiers == Key::Modifiers::Resize)
force_redraw();
2015-07-22 15:30:03 +03:00
else
m_input_handler.handle_key(*key);
}
}
2015-07-22 15:30:03 +03:00
catch (Kakoune::runtime_error& error)
{
context().print_status({ error.what().str(), get_face("Error") });
context().hooks().run_hook("RuntimeError", error.what(), context());
}
catch (Kakoune::client_removed&)
{
ClientManager::instance().remove_client(*this);
}
2013-11-14 22:09:15 +04:00
}
void Client::print_status(DisplayLine status_line)
{
m_pending_status_line = std::move(status_line);
}
DisplayLine Client::generate_mode_line() const
{
auto pos = context().selections().main().cursor();
auto col = context().buffer()[pos.line].char_count_to(pos.column);
DisplayLine status;
Face info_face = get_face("Information");
status.push_back({ context().buffer().display_name() });
status.push_back({ format(" {}:{} ", pos.line+1, col+1) });
if (context().buffer().is_modified())
status.push_back({ "[+]", info_face });
2013-11-14 22:09:15 +04:00
if (m_input_handler.is_recording())
2015-03-31 01:56:33 +03:00
status.push_back({ format("[recording ({})]", m_input_handler.recording_reg()), info_face });
if (context().buffer().flags() & Buffer::Flags::New)
status.push_back({ "[new file]", info_face });
if (context().user_hooks_disabled())
status.push_back({ "[no-hooks]", info_face });
if (context().buffer().flags() & Buffer::Flags::Fifo)
status.push_back({ "[fifo]", info_face });
status.push_back({ " " });
for (auto& atom : m_input_handler.mode_line())
status.push_back(std::move(atom));
status.push_back({ format(" - {}@[{}]", context().name(), Server::instance().session()) });
return status;
}
void Client::change_buffer(Buffer& buffer)
{
if (m_buffer_reload_dialog_opened)
close_buffer_reload_dialog();
2014-04-01 21:54:46 +04:00
auto& client_manager = ClientManager::instance();
m_window->options().unregister_watcher(*this);
2014-04-01 21:54:46 +04:00
client_manager.add_free_window(std::move(m_window),
std::move(context().selections()));
WindowAndSelections ws = client_manager.get_free_window(buffer);
2014-01-28 00:28:38 +04:00
m_window = std::move(ws.window);
m_window->options().register_watcher(*this);
m_ui->set_ui_options(m_window->options()["ui_options"].get<UserInterface::Options>());
context().selections_write_only() = std::move(ws.selections);
context().set_window(*m_window);
m_window->set_dimensions(ui().dimensions());
m_window->hooks().run_hook("WinDisplay", buffer.name(), context());
}
void Client::redraw_ifn()
{
2015-06-22 15:34:22 +03:00
Window& window = context().window();
UserInterface& ui = context().ui();
const bool needs_redraw = window.needs_redraw(context());
if (needs_redraw)
ui.draw(window.update_display_buffer(context()), get_face("Default"));
DisplayLine mode_line = generate_mode_line();
if (needs_redraw or
m_status_line.atoms() != m_pending_status_line.atoms() or
mode_line.atoms() != m_mode_line.atoms())
{
m_mode_line = std::move(mode_line);
m_status_line = m_pending_status_line;
ui.draw_status(m_status_line, m_mode_line, get_face("StatusLine"));
}
2015-06-22 15:34:22 +03:00
ui.refresh();
}
void Client::force_redraw()
{
if (m_window)
m_window->force_redraw();
}
void Client::reload_buffer()
{
auto& buffer = context().buffer();
kak_assert(buffer.flags() & Buffer::Flags::File);
if (Buffer* buf = create_buffer_from_file(buffer.name()))
{
kak_assert(buf == &buffer);
context().print_status({ format("'{}' reloaded", buffer.display_name()),
get_face("Information") });
}
else
context().print_status({ format("could not reload '{}'", buffer.display_name()),
get_face("Error") });
}
void Client::on_buffer_reload_key(Key key)
{
auto& buffer = context().buffer();
if (key == 'y' or key == ctrl('m'))
reload_buffer();
else if (key == 'n' or key == Key::Escape)
{
// reread timestamp in case the file was modified again
buffer.set_fs_timestamp(get_fs_timestamp(buffer.name()));
print_status({ format("'{}' kept", buffer.display_name()),
get_face("Information") });
}
else
{
print_status({ format("'{}' is not a valid choice", key_to_str(key)),
get_face("Error") });
m_input_handler.on_next_key(KeymapMode::None, [this](Key key, Context&){ on_buffer_reload_key(key); });
return;
}
for (auto& client : ClientManager::instance())
{
if (&client->context().buffer() == &buffer and
client->m_buffer_reload_dialog_opened)
client->close_buffer_reload_dialog();
}
}
void Client::close_buffer_reload_dialog()
{
kak_assert(m_buffer_reload_dialog_opened);
m_buffer_reload_dialog_opened = false;
m_ui->info_hide();
m_input_handler.reset_normal_mode();
}
void Client::check_if_buffer_needs_reloading()
{
if (m_buffer_reload_dialog_opened)
return;
2013-11-14 22:09:15 +04:00
Buffer& buffer = context().buffer();
auto reload = context().options()["autoreload"].get<YesNoAsk>();
if (not (buffer.flags() & Buffer::Flags::File) or reload == No)
return;
const String& filename = buffer.name();
time_t ts = get_fs_timestamp(filename);
if (ts == InvalidTime or ts == buffer.fs_timestamp())
return;
if (reload == Ask)
{
2014-04-01 21:54:46 +04:00
m_ui->info_show(
format("reload '{}' ?", buffer.display_name()),
format("'{}' was modified externally\n"
"press <ret> or y to reload, <esc> or n to keep",
buffer.display_name()),
CharCoord{}, get_face("Information"), InfoStyle::Prompt);
m_buffer_reload_dialog_opened = true;
m_input_handler.on_next_key(KeymapMode::None, [this](Key key, Context&){ on_buffer_reload_key(key); });
}
else
reload_buffer();
}
2014-12-20 21:40:17 +03:00
StringView Client::get_env_var(const String& name) const
{
auto it = m_env_vars.find(name);
if (it == m_env_vars.end())
2014-12-20 21:40:17 +03:00
return {};
return it->second;
}
StringView Client::get_env_var(StringView name) const
{
return get_env_var(name.str());
}
void Client::on_option_changed(const Option& option)
{
if (option.name() == "ui_options")
m_ui->set_ui_options(option.get<UserInterface::Options>());
}
}