2013-09-13 01:47:23 +04:00
|
|
|
#include "client.hh"
|
2012-09-03 16:22:02 +04:00
|
|
|
|
2014-07-11 03:27:04 +04:00
|
|
|
#include "face_registry.hh"
|
2012-09-03 16:22:02 +04:00
|
|
|
#include "context.hh"
|
2013-05-20 16:10:53 +04:00
|
|
|
#include "buffer_manager.hh"
|
2013-04-09 21:39:03 +04:00
|
|
|
#include "user_interface.hh"
|
2013-08-02 20:58:37 +04:00
|
|
|
#include "file.hh"
|
2013-10-11 21:43:23 +04:00
|
|
|
#include "remote.hh"
|
2013-10-15 21:51:31 +04:00
|
|
|
#include "client_manager.hh"
|
2013-11-15 01:12:59 +04:00
|
|
|
#include "window.hh"
|
2012-09-03 16:22:02 +04:00
|
|
|
|
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2013-12-21 00:10:08 +04:00
|
|
|
Client::Client(std::unique_ptr<UserInterface>&& ui,
|
|
|
|
std::unique_ptr<Window>&& window,
|
2014-04-08 00:25:44 +04:00
|
|
|
SelectionList selections,
|
|
|
|
EnvVarMap env_vars,
|
|
|
|
String name)
|
2013-12-21 00:10:08 +04:00
|
|
|
: m_ui{std::move(ui)}, m_window{std::move(window)},
|
2014-05-13 02:25:15 +04:00
|
|
|
m_input_handler{std::move(selections),
|
2014-04-08 00:25:44 +04:00
|
|
|
std::move(name)},
|
|
|
|
m_env_vars(env_vars)
|
2013-11-14 22:09:15 +04:00
|
|
|
{
|
|
|
|
context().set_client(*this);
|
2013-12-21 00:10:08 +04:00
|
|
|
context().set_window(*m_window);
|
2013-11-14 22:09:15 +04: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 22:15:13 +04:00
|
|
|
void Client::print_status(DisplayLine status_line)
|
|
|
|
{
|
2014-08-12 22:19:46 +04:00
|
|
|
m_pending_status_line = std::move(status_line);
|
2013-09-16 22:15:13 +04:00
|
|
|
}
|
|
|
|
|
2013-10-11 00:34:19 +04:00
|
|
|
DisplayLine Client::generate_mode_line() const
|
2013-09-16 22:15:13 +04:00
|
|
|
{
|
2014-01-28 23:05:49 +04:00
|
|
|
auto pos = context().selections().main().cursor();
|
2013-10-11 00:34:19 +04:00
|
|
|
auto col = context().buffer()[pos.line].char_count_to(pos.column);
|
2013-09-16 22:15:13 +04:00
|
|
|
|
2014-07-07 22:57:03 +04:00
|
|
|
DisplayLine status;
|
2014-07-11 03:27:04 +04:00
|
|
|
Face info_face = get_face("Information");
|
|
|
|
Face status_face = get_face("StatusLine");
|
|
|
|
Face prompt_face = get_face("Prompt");
|
2014-07-07 22:57:03 +04:00
|
|
|
|
2014-07-11 03:27:04 +04: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-11 00:34:19 +04:00
|
|
|
if (context().buffer().is_modified())
|
2014-07-11 03:27:04 +04:00
|
|
|
status.push_back({ "[+]", info_face });
|
2013-11-14 22:09:15 +04:00
|
|
|
if (m_input_handler.is_recording())
|
2014-07-11 03:27:04 +04:00
|
|
|
status.push_back({ "[recording ("_str + m_input_handler.recording_reg() + ")]", info_face });
|
2013-10-11 00:34:19 +04:00
|
|
|
if (context().buffer().flags() & Buffer::Flags::New)
|
2014-07-11 03:27:04 +04:00
|
|
|
status.push_back({ "[new file]", info_face });
|
2014-07-26 02:47:47 +04:00
|
|
|
if (context().are_user_hooks_disabled())
|
|
|
|
status.push_back({ "[no-hooks]", info_face });
|
2014-07-07 22:57:03 +04:00
|
|
|
if (context().buffer().flags() & Buffer::Flags::Fifo)
|
2014-07-11 03:27:04 +04:00
|
|
|
status.push_back({ "[fifo]", info_face });
|
|
|
|
status.push_back({ " ", status_face });
|
|
|
|
status.push_back({ m_input_handler.mode_string(), prompt_face });
|
|
|
|
status.push_back({ " - " + context().name() + "@[" + Server::instance().session() + "]", status_face });
|
2014-07-07 22:57:03 +04:00
|
|
|
|
|
|
|
return status;
|
2013-09-16 22:15:13 +04:00
|
|
|
}
|
|
|
|
|
2013-12-21 00:10:08 +04:00
|
|
|
void Client::change_buffer(Buffer& buffer)
|
|
|
|
{
|
2014-04-01 21:54:46 +04: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-28 00:28:38 +04:00
|
|
|
m_window = std::move(ws.window);
|
|
|
|
context().m_selections = std::move(ws.selections);
|
2013-12-21 00:10:08 +04:00
|
|
|
context().set_window(*m_window);
|
|
|
|
m_window->set_dimensions(ui().dimensions());
|
|
|
|
m_window->hooks().run_hook("WinDisplay", buffer.name(), context());
|
|
|
|
}
|
|
|
|
|
2013-09-16 22:15:13 +04:00
|
|
|
void Client::redraw_ifn()
|
|
|
|
{
|
2014-07-07 23:13:08 +04:00
|
|
|
DisplayLine mode_line = generate_mode_line();
|
|
|
|
const bool buffer_changed = context().window().timestamp() != context().buffer().timestamp();
|
2014-08-12 22:19:46 +04: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 22:15:13 +04:00
|
|
|
{
|
2014-07-07 23:13:08 +04: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 22:19:46 +04:00
|
|
|
m_status_line = m_pending_status_line;
|
2013-11-14 22:09:15 +04:00
|
|
|
context().ui().draw(context().window().display_buffer(),
|
2014-07-07 23:13:08 +04:00
|
|
|
m_status_line, m_mode_line);
|
2013-09-16 22:15:13 +04:00
|
|
|
}
|
2014-04-15 22:19:44 +04:00
|
|
|
context().ui().refresh();
|
2013-09-16 22:15:13 +04:00
|
|
|
}
|
|
|
|
|
2013-10-21 21:58:11 +04:00
|
|
|
static void reload_buffer(Context& context, const String& filename)
|
|
|
|
{
|
2014-05-07 22:51:01 +04:00
|
|
|
CharCoord view_pos = context.window().position();
|
|
|
|
ByteCoord cursor_pos = context.selections().main().cursor();
|
2013-10-21 21:58:11 +04:00
|
|
|
Buffer* buf = create_buffer_from_file(filename);
|
|
|
|
if (not buf)
|
|
|
|
return;
|
2013-12-21 00:10:08 +04:00
|
|
|
context.change_buffer(*buf);
|
2014-05-13 02:25:15 +04:00
|
|
|
context.selections() = SelectionList{ *buf, buf->clamp(cursor_pos)};
|
2013-12-21 00:10:08 +04:00
|
|
|
context.window().set_position(view_pos);
|
2013-10-21 21:58:11 +04:00
|
|
|
context.print_status({ "'" + buf->display_name() + "' reloaded",
|
2014-07-11 03:27:04 +04:00
|
|
|
get_face("Information") });
|
2013-10-21 21:58:11 +04:00
|
|
|
}
|
|
|
|
|
2013-10-15 21:51:31 +04:00
|
|
|
void Client::check_buffer_fs_timestamp()
|
|
|
|
{
|
2013-11-14 22:09:15 +04:00
|
|
|
Buffer& buffer = context().buffer();
|
2013-10-21 21:58:11 +04:00
|
|
|
auto reload = context().options()["autoreload"].get<YesNoAsk>();
|
|
|
|
if (not (buffer.flags() & Buffer::Flags::File) or reload == No)
|
2013-10-15 21:51:31 +04:00
|
|
|
return;
|
2013-10-21 21:58:11 +04:00
|
|
|
|
|
|
|
const String& filename = buffer.name();
|
2013-10-15 21:51:31 +04:00
|
|
|
time_t ts = get_fs_timestamp(filename);
|
2014-06-22 14:09:44 +04:00
|
|
|
if (ts == InvalidTime or ts == buffer.fs_timestamp())
|
2013-10-21 21:58:11 +04:00
|
|
|
return;
|
|
|
|
if (reload == Ask)
|
2013-10-15 21:51:31 +04:00
|
|
|
{
|
2014-05-07 22:51:01 +04:00
|
|
|
CharCoord pos = context().window().dimensions();
|
2014-03-24 23:31:40 +04:00
|
|
|
pos.column -= 1;
|
2014-04-01 21:54:46 +04: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 03:27:04 +04:00
|
|
|
pos, get_face("Information"), MenuStyle::Prompt);
|
2014-03-24 23:31:40 +04:00
|
|
|
|
2014-04-03 01:33:52 +04:00
|
|
|
m_input_handler.on_next_key([this, filename, ts](Key key, Context& context) {
|
2013-10-15 21:51:31 +04:00
|
|
|
Buffer* buf = BufferManager::instance().get_buffer_ifp(filename);
|
2014-03-24 23:31:40 +04:00
|
|
|
m_ui->info_hide();
|
2013-10-15 21:51:31 +04:00
|
|
|
// buffer got deleted while waiting for the key, do nothing
|
|
|
|
if (not buf)
|
|
|
|
return;
|
|
|
|
if (key == 'r' or key == 'y')
|
2013-10-21 21:58:11 +04:00
|
|
|
reload_buffer(context, filename);
|
2014-03-26 23:06:16 +04:00
|
|
|
else if (key == 'k' or key == 'n')
|
2013-10-15 21:51:31 +04:00
|
|
|
{
|
|
|
|
buf->set_fs_timestamp(ts);
|
2014-04-01 21:54:46 +04:00
|
|
|
print_status({ "'" + buf->display_name() + "' kept",
|
2014-07-11 03:27:04 +04:00
|
|
|
get_face("Information") });
|
2013-10-15 21:51:31 +04:00
|
|
|
}
|
|
|
|
else
|
2014-03-24 23:31:40 +04:00
|
|
|
{
|
2014-04-01 21:54:46 +04:00
|
|
|
print_status({ "'" + key_to_str(key) + "' is not a valid choice",
|
2014-07-11 03:27:04 +04:00
|
|
|
get_face("Error") });
|
2013-10-15 21:51:31 +04:00
|
|
|
check_buffer_fs_timestamp();
|
2014-03-24 23:31:40 +04:00
|
|
|
}
|
2013-10-15 21:51:31 +04:00
|
|
|
});
|
|
|
|
}
|
2013-10-21 21:58:11 +04:00
|
|
|
else
|
|
|
|
reload_buffer(context(), filename);
|
2013-10-15 21:51:31 +04:00
|
|
|
}
|
|
|
|
|
2014-04-08 00:25:44 +04: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 16:22:02 +04:00
|
|
|
}
|