1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
|
#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 "window.hh"
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),
std::move(name)},
m_env_vars(env_vars)
{
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>());
}
Client::~Client()
{
m_window->options().unregister_watcher(*this);
}
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();
}
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");
Face status_face = get_face("StatusLine");
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 });
if (context().buffer().is_modified())
status.push_back({ "[+]", info_face });
if (m_input_handler.is_recording())
status.push_back({ "[recording ("_str + m_input_handler.recording_reg() + ")]", info_face });
if (context().buffer().flags() & Buffer::Flags::New)
status.push_back({ "[new file]", info_face });
if (context().are_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({ " ", status_face });
for (auto& atom : m_input_handler.mode_line())
status.push_back(std::move(atom));
status.push_back({ " - " + context().name() + "@[" + Server::instance().session() + "]", status_face });
return status;
}
void Client::change_buffer(Buffer& buffer)
{
auto& client_manager = ClientManager::instance();
m_window->options().unregister_watcher(*this);
client_manager.add_free_window(std::move(m_window),
std::move(context().selections()));
WindowAndSelections ws = client_manager.get_free_window(buffer);
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().m_selections = 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()
{
DisplayLine mode_line = generate_mode_line();
const bool buffer_changed = context().window().timestamp() != context().buffer().timestamp();
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)
{
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);
m_status_line = m_pending_status_line;
context().ui().draw(context().window().display_buffer(),
m_status_line, m_mode_line);
}
context().ui().refresh();
}
static void reload_buffer(Context& context, StringView filename)
{
CharCoord view_pos = context.window().position();
ByteCoord cursor_pos = context.selections().main().cursor();
Buffer* buf = create_buffer_from_file(filename);
if (not buf)
return;
context.change_buffer(*buf);
context.selections() = SelectionList{ *buf, buf->clamp(cursor_pos)};
context.window().set_position(view_pos);
context.print_status({ "'" + buf->display_name() + "' reloaded",
get_face("Information") });
}
void Client::check_buffer_fs_timestamp()
{
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)
{
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",
CharCoord{}, get_face("Information"), InfoStyle::Prompt);
m_input_handler.on_next_key(KeymapMode::None,
[this, filename](Key key, Context& context) {
Buffer* buf = BufferManager::instance().get_buffer_ifp(filename);
m_ui->info_hide();
// buffer got deleted while waiting for the key, do nothing
if (not buf)
return;
if (key == 'r' or key == 'y')
reload_buffer(context, filename);
else if (key == 'k' or key == 'n')
{
// reread timestamp in case the file was modified again
buf->set_fs_timestamp(get_fs_timestamp(filename));
print_status({ "'" + buf->display_name() + "' kept",
get_face("Information") });
}
else
{
print_status({ "'" + key_to_str(key) + "' is not a valid choice",
get_face("Error") });
check_buffer_fs_timestamp();
}
});
}
else
reload_buffer(context(), filename);
}
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;
}
void Client::on_option_changed(const Option& option)
{
if (option.name() == "ui_options")
m_ui->set_ui_options(option.get<UserInterface::Options>());
}
}
|