2013-09-12 23:47:23 +02:00
|
|
|
#include "client.hh"
|
2012-09-03 14:22:02 +02:00
|
|
|
|
2014-07-11 01:27:04 +02:00
|
|
|
#include "face_registry.hh"
|
2012-09-03 14:22:02 +02:00
|
|
|
#include "context.hh"
|
2013-05-20 14:10:53 +02:00
|
|
|
#include "buffer_manager.hh"
|
2015-09-19 13:43:39 +02:00
|
|
|
#include "buffer_utils.hh"
|
2013-04-09 19:39:03 +02:00
|
|
|
#include "user_interface.hh"
|
2013-08-02 18:58:37 +02:00
|
|
|
#include "file.hh"
|
2013-10-11 19:43:23 +02:00
|
|
|
#include "remote.hh"
|
2013-10-15 19:51:31 +02:00
|
|
|
#include "client_manager.hh"
|
2015-09-19 13:43:39 +02:00
|
|
|
#include "command_manager.hh"
|
2014-11-25 02:00:18 +01:00
|
|
|
#include "event_manager.hh"
|
2013-11-14 22:12:59 +01:00
|
|
|
#include "window.hh"
|
2012-09-03 14:22:02 +02:00
|
|
|
|
2014-11-25 02:00:18 +01:00
|
|
|
#include <signal.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2012-09-03 14:22:02 +02:00
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2013-12-20 21:10:08 +01:00
|
|
|
Client::Client(std::unique_ptr<UserInterface>&& ui,
|
|
|
|
std::unique_ptr<Window>&& window,
|
2014-04-07 22:25:44 +02:00
|
|
|
SelectionList selections,
|
|
|
|
EnvVarMap env_vars,
|
|
|
|
String name)
|
2013-12-20 21:10:08 +01:00
|
|
|
: m_ui{std::move(ui)}, m_window{std::move(window)},
|
2014-12-19 00:12:58 +01:00
|
|
|
m_input_handler{std::move(selections), Context::Flags::None,
|
2014-04-07 22:25:44 +02:00
|
|
|
std::move(name)},
|
|
|
|
m_env_vars(env_vars)
|
2013-11-14 19:09:15 +01:00
|
|
|
{
|
|
|
|
context().set_client(*this);
|
2013-12-20 21:10:08 +01:00
|
|
|
context().set_window(*m_window);
|
2014-11-11 00:29:16 +01:00
|
|
|
|
|
|
|
m_window->options().register_watcher(*this);
|
|
|
|
m_ui->set_ui_options(m_window->options()["ui_options"].get<UserInterface::Options>());
|
2013-11-14 19:09:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Client::~Client()
|
|
|
|
{
|
2014-11-11 00:29:16 +01:00
|
|
|
m_window->options().unregister_watcher(*this);
|
2013-11-14 19:09:15 +01:00
|
|
|
}
|
|
|
|
|
2014-11-29 21:14:52 +01: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 {};
|
|
|
|
}
|
|
|
|
|
2014-11-25 02:00:18 +01:00
|
|
|
void Client::handle_available_input(EventMode mode)
|
2013-11-14 19:09:15 +01:00
|
|
|
{
|
2014-11-29 21:14:52 +01: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 14:30:03 +02:00
|
|
|
return;
|
2014-11-29 21:14:52 +01:00
|
|
|
}
|
2015-07-22 14:30:03 +02:00
|
|
|
|
|
|
|
try
|
2014-11-25 02:00:18 +01:00
|
|
|
{
|
2015-07-22 14:30:03 +02:00
|
|
|
while (Optional<Key> key = get_next_key(mode))
|
2014-11-25 02:00:18 +01:00
|
|
|
{
|
2015-07-22 14:30:03 +02: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 16:18:18 +02:00
|
|
|
else if (key->modifiers == Key::Modifiers::Resize)
|
|
|
|
force_redraw();
|
2015-07-22 14:30:03 +02:00
|
|
|
else
|
|
|
|
m_input_handler.handle_key(*key);
|
2014-11-25 02:00:18 +01:00
|
|
|
}
|
|
|
|
}
|
2015-07-22 14:30:03 +02: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 19:09:15 +01:00
|
|
|
}
|
|
|
|
|
2013-09-16 20:15:13 +02:00
|
|
|
void Client::print_status(DisplayLine status_line)
|
|
|
|
{
|
2014-08-12 20:19:46 +02:00
|
|
|
m_pending_status_line = std::move(status_line);
|
2013-09-16 20:15:13 +02:00
|
|
|
}
|
|
|
|
|
2013-10-10 22:34:19 +02:00
|
|
|
DisplayLine Client::generate_mode_line() const
|
2013-09-16 20:15:13 +02:00
|
|
|
{
|
2015-09-19 13:43:39 +02:00
|
|
|
DisplayLine modeline;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
const String& modelinefmt = context().options()["modelinefmt"].get<String>();
|
|
|
|
|
2015-09-20 12:34:13 +02:00
|
|
|
modeline = parse_display_line(expand(modelinefmt, context()));
|
2015-09-19 13:43:39 +02:00
|
|
|
}
|
|
|
|
catch (runtime_error& err)
|
|
|
|
{
|
|
|
|
write_to_debug_buffer(format("Error while parsing modelinefmt: {}", err.what()));
|
|
|
|
modeline.push_back({ "modelinefmt error, see *debug* buffer", get_face("Error") });
|
|
|
|
}
|
2013-09-16 20:15:13 +02:00
|
|
|
|
2014-07-11 01:27:04 +02:00
|
|
|
Face info_face = get_face("Information");
|
2014-07-07 20:57:03 +02:00
|
|
|
|
2013-10-10 22:34:19 +02:00
|
|
|
if (context().buffer().is_modified())
|
2015-09-19 13:43:39 +02:00
|
|
|
modeline.push_back({ "[+]", info_face });
|
2013-11-14 19:09:15 +01:00
|
|
|
if (m_input_handler.is_recording())
|
2015-09-19 13:43:39 +02:00
|
|
|
modeline.push_back({ format("[recording ({})]", m_input_handler.recording_reg()), info_face });
|
2013-10-10 22:34:19 +02:00
|
|
|
if (context().buffer().flags() & Buffer::Flags::New)
|
2015-09-19 13:43:39 +02:00
|
|
|
modeline.push_back({ "[new file]", info_face });
|
2015-08-19 00:16:53 +02:00
|
|
|
if (context().user_hooks_disabled())
|
2015-09-19 13:43:39 +02:00
|
|
|
modeline.push_back({ "[no-hooks]", info_face });
|
2014-07-07 20:57:03 +02:00
|
|
|
if (context().buffer().flags() & Buffer::Flags::Fifo)
|
2015-09-19 13:43:39 +02:00
|
|
|
modeline.push_back({ "[fifo]", info_face });
|
|
|
|
modeline.push_back({ " " });
|
2014-09-10 20:06:53 +02:00
|
|
|
for (auto& atom : m_input_handler.mode_line())
|
2015-09-19 13:43:39 +02:00
|
|
|
modeline.push_back(std::move(atom));
|
|
|
|
modeline.push_back({ format(" - {}@[{}]", context().name(), Server::instance().session()) });
|
2014-07-07 20:57:03 +02:00
|
|
|
|
2015-09-19 13:43:39 +02:00
|
|
|
return modeline;
|
2013-09-16 20:15:13 +02:00
|
|
|
}
|
|
|
|
|
2013-12-20 21:10:08 +01:00
|
|
|
void Client::change_buffer(Buffer& buffer)
|
|
|
|
{
|
2015-02-12 15:55:02 +01:00
|
|
|
if (m_buffer_reload_dialog_opened)
|
|
|
|
close_buffer_reload_dialog();
|
|
|
|
|
2014-04-01 19:54:46 +02:00
|
|
|
auto& client_manager = ClientManager::instance();
|
2014-11-11 00:29:16 +01:00
|
|
|
m_window->options().unregister_watcher(*this);
|
2014-04-01 19:54:46 +02:00
|
|
|
client_manager.add_free_window(std::move(m_window),
|
|
|
|
std::move(context().selections()));
|
|
|
|
WindowAndSelections ws = client_manager.get_free_window(buffer);
|
2014-11-11 00:29:16 +01:00
|
|
|
|
2014-01-27 21:28:38 +01:00
|
|
|
m_window = std::move(ws.window);
|
2014-11-11 00:29:16 +01:00
|
|
|
m_window->options().register_watcher(*this);
|
|
|
|
m_ui->set_ui_options(m_window->options()["ui_options"].get<UserInterface::Options>());
|
|
|
|
|
2015-04-19 16:12:16 +02:00
|
|
|
context().selections_write_only() = std::move(ws.selections);
|
2013-12-20 21:10:08 +01:00
|
|
|
context().set_window(*m_window);
|
|
|
|
m_window->set_dimensions(ui().dimensions());
|
2014-11-11 00:29:16 +01:00
|
|
|
|
2013-12-20 21:10:08 +01:00
|
|
|
m_window->hooks().run_hook("WinDisplay", buffer.name(), context());
|
|
|
|
}
|
|
|
|
|
2013-09-16 20:15:13 +02:00
|
|
|
void Client::redraw_ifn()
|
|
|
|
{
|
2015-06-22 14:34:22 +02:00
|
|
|
Window& window = context().window();
|
|
|
|
UserInterface& ui = context().ui();
|
2015-06-17 22:28:02 +02:00
|
|
|
|
2015-06-24 14:35:46 +02:00
|
|
|
const bool needs_redraw = window.needs_redraw(context());
|
|
|
|
if (needs_redraw)
|
2015-06-29 23:48:26 +02:00
|
|
|
ui.draw(window.update_display_buffer(context()), get_face("Default"));
|
2015-06-17 22:28:02 +02:00
|
|
|
|
2014-07-07 21:13:08 +02:00
|
|
|
DisplayLine mode_line = generate_mode_line();
|
2015-06-24 14:35:46 +02:00
|
|
|
if (needs_redraw or
|
|
|
|
m_status_line.atoms() != m_pending_status_line.atoms() or
|
2015-06-17 22:28:02 +02:00
|
|
|
mode_line.atoms() != m_mode_line.atoms())
|
2013-09-16 20:15:13 +02:00
|
|
|
{
|
2014-07-07 21:13:08 +02:00
|
|
|
m_mode_line = std::move(mode_line);
|
2014-08-12 20:19:46 +02:00
|
|
|
m_status_line = m_pending_status_line;
|
2015-06-17 22:28:02 +02:00
|
|
|
|
2015-06-29 23:48:26 +02:00
|
|
|
ui.draw_status(m_status_line, m_mode_line, get_face("StatusLine"));
|
2013-09-16 20:15:13 +02:00
|
|
|
}
|
2015-06-17 22:28:02 +02:00
|
|
|
|
2015-06-22 14:34:22 +02:00
|
|
|
ui.refresh();
|
2013-09-16 20:15:13 +02:00
|
|
|
}
|
|
|
|
|
2015-08-23 14:29:24 +02:00
|
|
|
void Client::force_redraw()
|
|
|
|
{
|
|
|
|
if (m_window)
|
|
|
|
m_window->force_redraw();
|
|
|
|
}
|
|
|
|
|
2015-02-12 15:55:02 +01:00
|
|
|
void Client::reload_buffer()
|
|
|
|
{
|
|
|
|
auto& buffer = context().buffer();
|
|
|
|
kak_assert(buffer.flags() & Buffer::Flags::File);
|
2015-06-05 14:11:04 +02:00
|
|
|
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") });
|
2015-02-12 15:55:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Client::on_buffer_reload_key(Key key)
|
2013-10-21 19:58:11 +02:00
|
|
|
{
|
2015-02-12 15:55:02 +01:00
|
|
|
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()));
|
2015-06-01 22:15:59 +02:00
|
|
|
print_status({ format("'{}' kept", buffer.display_name()),
|
2015-02-12 15:55:02 +01:00
|
|
|
get_face("Information") });
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-06-01 22:15:59 +02:00
|
|
|
print_status({ format("'{}' is not a valid choice", key_to_str(key)),
|
2015-02-12 15:55:02 +01:00
|
|
|
get_face("Error") });
|
|
|
|
m_input_handler.on_next_key(KeymapMode::None, [this](Key key, Context&){ on_buffer_reload_key(key); });
|
2013-10-21 19:58:11 +02:00
|
|
|
return;
|
2015-02-12 15:55:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
2013-10-21 19:58:11 +02:00
|
|
|
}
|
|
|
|
|
2015-02-12 15:55:02 +01:00
|
|
|
void Client::check_if_buffer_needs_reloading()
|
2013-10-15 19:51:31 +02:00
|
|
|
{
|
2015-03-04 21:46:26 +01:00
|
|
|
if (m_buffer_reload_dialog_opened)
|
|
|
|
return;
|
|
|
|
|
2013-11-14 19:09:15 +01:00
|
|
|
Buffer& buffer = context().buffer();
|
2013-10-21 19:58:11 +02:00
|
|
|
auto reload = context().options()["autoreload"].get<YesNoAsk>();
|
|
|
|
if (not (buffer.flags() & Buffer::Flags::File) or reload == No)
|
2013-10-15 19:51:31 +02:00
|
|
|
return;
|
2013-10-21 19:58:11 +02:00
|
|
|
|
|
|
|
const String& filename = buffer.name();
|
2013-10-15 19:51:31 +02:00
|
|
|
time_t ts = get_fs_timestamp(filename);
|
2014-06-22 12:09:44 +02:00
|
|
|
if (ts == InvalidTime or ts == buffer.fs_timestamp())
|
2013-10-21 19:58:11 +02:00
|
|
|
return;
|
|
|
|
if (reload == Ask)
|
2013-10-15 19:51:31 +02:00
|
|
|
{
|
2014-04-01 19:54:46 +02:00
|
|
|
m_ui->info_show(
|
2015-06-01 22:15:59 +02:00
|
|
|
format("reload '{}' ?", buffer.display_name()),
|
|
|
|
format("'{}' was modified externally\n"
|
|
|
|
"press <ret> or y to reload, <esc> or n to keep",
|
|
|
|
buffer.display_name()),
|
2014-11-08 20:08:23 +01:00
|
|
|
CharCoord{}, get_face("Information"), InfoStyle::Prompt);
|
2014-03-24 20:31:40 +01:00
|
|
|
|
2015-02-12 15:55:02 +01:00
|
|
|
m_buffer_reload_dialog_opened = true;
|
|
|
|
m_input_handler.on_next_key(KeymapMode::None, [this](Key key, Context&){ on_buffer_reload_key(key); });
|
2013-10-15 19:51:31 +02:00
|
|
|
}
|
2013-10-21 19:58:11 +02:00
|
|
|
else
|
2015-02-12 15:55:02 +01:00
|
|
|
reload_buffer();
|
2013-10-15 19:51:31 +02:00
|
|
|
}
|
|
|
|
|
2015-09-16 20:57:57 +02:00
|
|
|
StringView Client::get_env_var(StringView name) const
|
2014-04-07 22:25:44 +02:00
|
|
|
{
|
|
|
|
auto it = m_env_vars.find(name);
|
|
|
|
if (it == m_env_vars.end())
|
2014-12-20 19:40:17 +01:00
|
|
|
return {};
|
2015-09-16 20:57:57 +02:00
|
|
|
return it->value;
|
2015-03-10 20:33:46 +01:00
|
|
|
}
|
|
|
|
|
2014-11-11 00:29:16 +01:00
|
|
|
void Client::on_option_changed(const Option& option)
|
|
|
|
{
|
|
|
|
if (option.name() == "ui_options")
|
|
|
|
m_ui->set_ui_options(option.get<UserInterface::Options>());
|
|
|
|
}
|
|
|
|
|
2012-09-03 14:22:02 +02:00
|
|
|
}
|