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"
|
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,
|
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-05-13 00:25:15 +02:00
|
|
|
m_input_handler{std::move(selections),
|
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);
|
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)
|
|
|
|
{
|
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
|
|
|
{
|
2014-01-28 20:05:49 +01:00
|
|
|
auto pos = context().selections().main().cursor();
|
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
|
|
|
|
2014-07-07 20:57:03 +02:00
|
|
|
DisplayLine status;
|
2014-07-11 01:27:04 +02:00
|
|
|
Face info_face = get_face("Information");
|
|
|
|
Face status_face = get_face("StatusLine");
|
2014-07-07 20:57:03 +02:00
|
|
|
|
2014-07-11 01:27:04 +02:00
|
|
|
status.push_back({ context().buffer().display_name(), status_face });
|
|
|
|
status.push_back({ " " + to_string((int)pos.line+1) + ":" + to_string((int)col+1) + " ", status_face });
|
2013-10-10 22:34:19 +02:00
|
|
|
if (context().buffer().is_modified())
|
2014-07-11 01:27:04 +02:00
|
|
|
status.push_back({ "[+]", info_face });
|
2013-11-14 19:09:15 +01:00
|
|
|
if (m_input_handler.is_recording())
|
2014-07-11 01:27:04 +02:00
|
|
|
status.push_back({ "[recording ("_str + m_input_handler.recording_reg() + ")]", info_face });
|
2013-10-10 22:34:19 +02:00
|
|
|
if (context().buffer().flags() & Buffer::Flags::New)
|
2014-07-11 01:27:04 +02:00
|
|
|
status.push_back({ "[new file]", info_face });
|
2014-07-26 00:47:47 +02:00
|
|
|
if (context().are_user_hooks_disabled())
|
|
|
|
status.push_back({ "[no-hooks]", info_face });
|
2014-07-07 20:57:03 +02:00
|
|
|
if (context().buffer().flags() & Buffer::Flags::Fifo)
|
2014-07-11 01:27:04 +02:00
|
|
|
status.push_back({ "[fifo]", info_face });
|
|
|
|
status.push_back({ " ", status_face });
|
2014-09-10 20:06:53 +02:00
|
|
|
for (auto& atom : m_input_handler.mode_line())
|
|
|
|
status.push_back(std::move(atom));
|
2014-07-11 01:27:04 +02:00
|
|
|
status.push_back({ " - " + context().name() + "@[" + Server::instance().session() + "]", status_face });
|
2014-07-07 20:57:03 +02:00
|
|
|
|
|
|
|
return status;
|
2013-09-16 20:15:13 +02:00
|
|
|
}
|
|
|
|
|
2013-12-20 21:10:08 +01:00
|
|
|
void Client::change_buffer(Buffer& buffer)
|
|
|
|
{
|
2014-04-01 19:54:46 +02:00
|
|
|
auto& client_manager = ClientManager::instance();
|
|
|
|
client_manager.add_free_window(std::move(m_window),
|
|
|
|
std::move(context().selections()));
|
|
|
|
WindowAndSelections ws = client_manager.get_free_window(buffer);
|
2014-01-27 21:28:38 +01:00
|
|
|
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()
|
|
|
|
{
|
2014-07-07 21:13:08 +02:00
|
|
|
DisplayLine mode_line = generate_mode_line();
|
|
|
|
const bool buffer_changed = context().window().timestamp() != context().buffer().timestamp();
|
2014-08-12 20:19:46 +02:00
|
|
|
const bool mode_line_changed = mode_line.atoms() != m_mode_line.atoms();
|
|
|
|
const bool status_line_changed = m_status_line.atoms() != m_pending_status_line.atoms();
|
|
|
|
if (buffer_changed or status_line_changed or mode_line_changed)
|
2013-09-16 20:15:13 +02:00
|
|
|
{
|
2014-07-07 21:13:08 +02:00
|
|
|
if (buffer_changed)
|
|
|
|
{
|
|
|
|
CharCoord dimensions = context().ui().dimensions();
|
|
|
|
if (dimensions == CharCoord{0,0})
|
|
|
|
return;
|
|
|
|
context().window().set_dimensions(dimensions);
|
|
|
|
context().window().update_display_buffer(context());
|
|
|
|
}
|
|
|
|
m_mode_line = std::move(mode_line);
|
2014-08-12 20:19:46 +02:00
|
|
|
m_status_line = m_pending_status_line;
|
2013-11-14 19:09:15 +01:00
|
|
|
context().ui().draw(context().window().display_buffer(),
|
2014-07-07 21:13:08 +02:00
|
|
|
m_status_line, m_mode_line);
|
2013-09-16 20:15:13 +02:00
|
|
|
}
|
2014-04-15 20:19:44 +02:00
|
|
|
context().ui().refresh();
|
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)
|
|
|
|
{
|
2014-05-07 20:51:01 +02:00
|
|
|
CharCoord view_pos = context.window().position();
|
|
|
|
ByteCoord cursor_pos = context.selections().main().cursor();
|
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);
|
2014-05-13 00:25:15 +02:00
|
|
|
context.selections() = SelectionList{ *buf, buf->clamp(cursor_pos)};
|
2013-12-20 21:10:08 +01:00
|
|
|
context.window().set_position(view_pos);
|
2013-10-21 19:58:11 +02:00
|
|
|
context.print_status({ "'" + buf->display_name() + "' reloaded",
|
2014-07-11 01:27:04 +02:00
|
|
|
get_face("Information") });
|
2013-10-21 19:58:11 +02:00
|
|
|
}
|
|
|
|
|
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);
|
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-05-07 20:51:01 +02:00
|
|
|
CharCoord pos = context().window().dimensions();
|
2014-03-24 20:31:40 +01:00
|
|
|
pos.column -= 1;
|
2014-04-01 19:54:46 +02:00
|
|
|
m_ui->info_show(
|
|
|
|
"reload '" + buffer.display_name() + "' ?",
|
|
|
|
"'" + buffer.display_name() + "' was modified externally\n"
|
|
|
|
"press r or y to reload, k or n to keep",
|
2014-07-11 01:27:04 +02:00
|
|
|
pos, get_face("Information"), MenuStyle::Prompt);
|
2014-03-24 20:31:40 +01:00
|
|
|
|
2014-09-23 14:45:18 +02:00
|
|
|
m_input_handler.on_next_key(KeymapMode::None,
|
2014-10-22 20:24:26 +02:00
|
|
|
[this, filename](Key key, Context& context) {
|
2013-10-15 19:51:31 +02:00
|
|
|
Buffer* buf = BufferManager::instance().get_buffer_ifp(filename);
|
2014-03-24 20:31:40 +01:00
|
|
|
m_ui->info_hide();
|
2013-10-15 19:51:31 +02:00
|
|
|
// buffer got deleted while waiting for the key, do nothing
|
|
|
|
if (not buf)
|
|
|
|
return;
|
|
|
|
if (key == 'r' or key == 'y')
|
2013-10-21 19:58:11 +02:00
|
|
|
reload_buffer(context, filename);
|
2014-03-26 20:06:16 +01:00
|
|
|
else if (key == 'k' or key == 'n')
|
2013-10-15 19:51:31 +02:00
|
|
|
{
|
2014-10-22 20:24:26 +02:00
|
|
|
// reread timestamp in case the file was modified again
|
|
|
|
buf->set_fs_timestamp(get_fs_timestamp(filename));
|
2014-04-01 19:54:46 +02:00
|
|
|
print_status({ "'" + buf->display_name() + "' kept",
|
2014-07-11 01:27:04 +02:00
|
|
|
get_face("Information") });
|
2013-10-15 19:51:31 +02:00
|
|
|
}
|
|
|
|
else
|
2014-03-24 20:31:40 +01:00
|
|
|
{
|
2014-04-01 19:54:46 +02:00
|
|
|
print_status({ "'" + key_to_str(key) + "' is not a valid choice",
|
2014-07-11 01:27:04 +02:00
|
|
|
get_face("Error") });
|
2013-10-15 19:51:31 +02:00
|
|
|
check_buffer_fs_timestamp();
|
2014-03-24 20:31:40 +01:00
|
|
|
}
|
2013-10-15 19:51:31 +02:00
|
|
|
});
|
|
|
|
}
|
2013-10-21 19:58:11 +02:00
|
|
|
else
|
|
|
|
reload_buffer(context(), filename);
|
2013-10-15 19:51:31 +02:00
|
|
|
}
|
|
|
|
|
2014-04-07 22:25:44 +02:00
|
|
|
const String& Client::get_env_var(const String& name) const
|
|
|
|
{
|
|
|
|
auto it = m_env_vars.find(name);
|
|
|
|
static String empty{};
|
|
|
|
if (it == m_env_vars.end())
|
|
|
|
return empty;
|
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
|
2012-09-03 14:22:02 +02:00
|
|
|
}
|