2013-09-12 23:47:23 +02:00
|
|
|
#include "client.hh"
|
2012-09-03 14:22:02 +02:00
|
|
|
|
2013-04-09 19:39:03 +02:00
|
|
|
#include "color_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"
|
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"
|
2013-11-14 22:12:59 +01:00
|
|
|
#include "window.hh"
|
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,
|
|
|
|
SelectionList selections, String name)
|
|
|
|
: m_ui{std::move(ui)}, m_window{std::move(window)},
|
|
|
|
m_input_handler{m_window->buffer(), std::move(selections), std::move(name)}
|
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);
|
2013-11-14 19:09:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Client::~Client()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void Client::handle_available_input()
|
|
|
|
{
|
|
|
|
while (m_ui->is_key_available())
|
|
|
|
{
|
|
|
|
m_input_handler.handle_key(m_ui->get_key());
|
|
|
|
m_input_handler.clear_mode_trash();
|
|
|
|
}
|
|
|
|
context().window().forget_timestamp();
|
|
|
|
}
|
|
|
|
|
2013-09-16 20:15:13 +02:00
|
|
|
void Client::print_status(DisplayLine status_line)
|
|
|
|
{
|
|
|
|
m_status_line = std::move(status_line);
|
2013-11-14 19:09:15 +01:00
|
|
|
context().window().forget_timestamp();
|
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
|
|
|
{
|
2013-12-15 15:25:23 +01:00
|
|
|
auto pos = context().selections().main().last();
|
2013-10-10 22:34:19 +02:00
|
|
|
auto col = context().buffer()[pos.line].char_count_to(pos.column);
|
2013-09-16 20:15:13 +02:00
|
|
|
|
|
|
|
std::ostringstream oss;
|
2013-10-10 22:34:19 +02:00
|
|
|
oss << context().buffer().display_name()
|
2013-09-16 20:15:13 +02:00
|
|
|
<< " " << (int)pos.line+1 << ":" << (int)col+1;
|
2013-10-10 22:34:19 +02:00
|
|
|
if (context().buffer().is_modified())
|
2013-09-16 20:15:13 +02:00
|
|
|
oss << " [+]";
|
2013-11-14 19:09:15 +01:00
|
|
|
if (m_input_handler.is_recording())
|
|
|
|
oss << " [recording (" << m_input_handler.recording_reg() << ")]";
|
2013-10-10 22:34:19 +02:00
|
|
|
if (context().buffer().flags() & Buffer::Flags::New)
|
2013-09-16 20:15:13 +02:00
|
|
|
oss << " [new file]";
|
2013-11-14 22:12:59 +01:00
|
|
|
oss << " [" << m_input_handler.mode_string() << "]" << " - "
|
2013-11-14 21:51:25 +01:00
|
|
|
<< context().name() << "@[" << Server::instance().session() << "]";
|
2013-09-16 20:15:13 +02:00
|
|
|
return { oss.str(), get_color("StatusLine") };
|
|
|
|
}
|
|
|
|
|
2013-12-20 21:10:08 +01:00
|
|
|
void Client::change_buffer(Buffer& buffer)
|
|
|
|
{
|
|
|
|
ClientManager::instance().add_free_window(std::move(m_window), std::move(context().selections()));
|
2014-01-27 21:28:38 +01:00
|
|
|
WindowAndSelections ws = ClientManager::instance().get_free_window(buffer);
|
|
|
|
m_window = std::move(ws.window);
|
|
|
|
context().m_selections = std::move(ws.selections);
|
2013-12-20 21:10:08 +01:00
|
|
|
context().set_window(*m_window);
|
|
|
|
m_window->set_dimensions(ui().dimensions());
|
|
|
|
m_window->hooks().run_hook("WinDisplay", buffer.name(), context());
|
|
|
|
}
|
|
|
|
|
2013-09-16 20:15:13 +02:00
|
|
|
void Client::redraw_ifn()
|
|
|
|
{
|
2013-11-14 19:09:15 +01:00
|
|
|
if (context().window().timestamp() != context().buffer().timestamp())
|
2013-09-16 20:15:13 +02:00
|
|
|
{
|
2013-11-14 19:09:15 +01:00
|
|
|
DisplayCoord dimensions = context().ui().dimensions();
|
2013-09-16 20:15:13 +02:00
|
|
|
if (dimensions == DisplayCoord{0,0})
|
|
|
|
return;
|
2013-11-14 19:09:15 +01:00
|
|
|
context().window().set_dimensions(dimensions);
|
2013-12-17 00:24:08 +01:00
|
|
|
context().window().update_display_buffer(context());
|
2013-09-16 20:15:13 +02:00
|
|
|
|
2013-11-14 19:09:15 +01:00
|
|
|
context().ui().draw(context().window().display_buffer(),
|
2013-10-10 22:34:19 +02:00
|
|
|
m_status_line, generate_mode_line());
|
2013-09-16 20:15:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-21 19:58:11 +02:00
|
|
|
static void reload_buffer(Context& context, const String& filename)
|
|
|
|
{
|
|
|
|
DisplayCoord view_pos = context.window().position();
|
2013-12-15 15:25:23 +01:00
|
|
|
BufferCoord cursor_pos = context.selections().main().last();
|
2013-10-21 19:58:11 +02:00
|
|
|
Buffer* buf = create_buffer_from_file(filename);
|
|
|
|
if (not buf)
|
|
|
|
return;
|
2013-12-20 21:10:08 +01:00
|
|
|
context.change_buffer(*buf);
|
|
|
|
context.selections() = SelectionList{cursor_pos};
|
|
|
|
context.window().set_position(view_pos);
|
2013-10-21 19:58:11 +02:00
|
|
|
context.print_status({ "'" + buf->display_name() + "' reloaded",
|
|
|
|
get_color("Information") });
|
|
|
|
}
|
|
|
|
|
2013-10-15 19:51:31 +02:00
|
|
|
void Client::check_buffer_fs_timestamp()
|
|
|
|
{
|
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);
|
2013-10-21 19:58:11 +02:00
|
|
|
if (ts == buffer.fs_timestamp())
|
|
|
|
return;
|
|
|
|
if (reload == Ask)
|
2013-10-15 19:51:31 +02:00
|
|
|
{
|
|
|
|
print_status({"'" + buffer.display_name() + "' was modified externally, press r or y to reload, k or n to keep", get_color("Prompt")});
|
2013-11-14 19:09:15 +01:00
|
|
|
m_input_handler.on_next_key([this, ts, filename](Key key, Context& context) {
|
2013-10-15 19:51:31 +02:00
|
|
|
Buffer* buf = BufferManager::instance().get_buffer_ifp(filename);
|
|
|
|
// buffer got deleted while waiting for the key, do nothing
|
|
|
|
if (not buf)
|
|
|
|
{
|
|
|
|
print_status({});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (key == 'r' or key == 'y')
|
2013-10-21 19:58:11 +02:00
|
|
|
reload_buffer(context, filename);
|
2013-10-15 19:51:31 +02:00
|
|
|
if (key == 'k' or key == 'n')
|
|
|
|
{
|
|
|
|
buf->set_fs_timestamp(ts);
|
|
|
|
print_status({"'" + buf->display_name() + "' kept", get_color("Information") });
|
|
|
|
}
|
|
|
|
else
|
|
|
|
check_buffer_fs_timestamp();
|
|
|
|
});
|
|
|
|
}
|
2013-10-21 19:58:11 +02:00
|
|
|
else
|
|
|
|
reload_buffer(context(), filename);
|
2013-10-15 19:51:31 +02:00
|
|
|
}
|
|
|
|
|
2012-09-03 14:22:02 +02:00
|
|
|
}
|