Rework handling of initial coordinates so that init commands can change them

Fixes #944
This commit is contained in:
Maxime Coste 2016-12-01 20:40:50 +00:00
parent 3ad554167d
commit a65e8142f3
4 changed files with 34 additions and 33 deletions

View File

@ -37,8 +37,8 @@ String ClientManager::generate_name() const
}
Client* ClientManager::create_client(std::unique_ptr<UserInterface>&& ui,
EnvVarMap env_vars,
StringView init_commands)
EnvVarMap env_vars, StringView init_cmds,
BufferCoord init_coord)
{
Buffer& buffer = BufferManager::instance().get_first_buffer();
WindowAndSelections ws = get_free_window(buffer);
@ -46,9 +46,14 @@ Client* ClientManager::create_client(std::unique_ptr<UserInterface>&& ui,
std::move(ws.selections), std::move(env_vars),
generate_name()};
m_clients.emplace_back(client);
auto& selections = client->context().selections_write_only();
selections = SelectionList(buffer, buffer.clamp(init_coord));
client->context().window().center_line(init_coord.line);
try
{
CommandManager::instance().execute(init_commands, client->context());
CommandManager::instance().execute(init_cmds, client->context());
}
catch (Kakoune::runtime_error& error)
{

View File

@ -21,7 +21,8 @@ public:
~ClientManager();
Client* create_client(std::unique_ptr<UserInterface>&& ui,
EnvVarMap env_vars, StringView init_cmd);
EnvVarMap env_vars, StringView init_cmds,
BufferCoord init_coord);
bool empty() const { return m_clients.empty(); }
size_t count() const { return m_clients.size(); }

View File

@ -454,12 +454,12 @@ void signal_handler(int signal)
abort();
}
int run_client(StringView session, StringView init_command, UIType ui_type)
int run_client(StringView session, StringView init_cmds, UIType ui_type)
{
try
{
EventManager event_manager;
RemoteClient client{session, make_ui(ui_type), get_env_vars(), init_command};
RemoteClient client{session, make_ui(ui_type), get_env_vars(), init_cmds};
while (true)
event_manager.handle_next_events(EventMode::Normal);
}
@ -471,9 +471,10 @@ int run_client(StringView session, StringView init_command, UIType ui_type)
return 0;
}
int run_server(StringView session, StringView init_command,
int run_server(StringView session,
StringView init_cmds, BufferCoord init_coord,
bool ignore_kakrc, bool daemon, bool readonly, UIType ui_type,
ConstArrayView<StringView> files, BufferCoord target_coord)
ConstArrayView<StringView> files)
{
static bool terminate = false;
if (daemon)
@ -565,21 +566,15 @@ int run_server(StringView session, StringView init_command,
try
{
if (not daemon and
(local_client = client_manager.create_client(
create_local_ui(ui_type), get_env_vars(), init_command)))
{
auto& selections = local_client->context().selections_write_only();
auto& buffer = selections.buffer();
selections = SelectionList(buffer, buffer.clamp(target_coord));
local_client->context().window().center_line(target_coord.line);
if (not daemon)
local_client = client_manager.create_client(
create_local_ui(ui_type), get_env_vars(), init_cmds, init_coord);
if (startup_error)
if (local_client and startup_error)
local_client->print_status({
"error during startup, see *debug* buffer for details",
get_face("Error")
});
}
while (not terminate and (not client_manager.empty() or daemon))
{
@ -794,7 +789,7 @@ int main(int argc, char* argv[])
return run_pipe(*session);
}
auto init_command = parser.get_switch("e").value_or(StringView{});
auto init_cmds = parser.get_switch("e").value_or(StringView{});
const UIType ui_type = parse_ui_type(parser.get_switch("ui").value_or("ncurses"));
if (auto keys = parser.get_switch("f"))
@ -809,7 +804,7 @@ int main(int argc, char* argv[])
for (size_t i = 0; i < parser.positional_count(); ++i)
files.emplace_back(parser[i]);
return run_filter(*keys, init_command, files,
return run_filter(*keys, init_cmds, files,
(bool)parser.get_switch("q"));
}
@ -827,11 +822,11 @@ int main(int argc, char* argv[])
for (auto name : parser)
new_files += format("edit '{}';", escape(real_path(name), "'", '\\'));
return run_client(*server_session, new_files + init_command, ui_type);
return run_client(*server_session, new_files + init_cmds, ui_type);
}
else
{
BufferCoord target_coord;
BufferCoord init_coord;
Vector<StringView> files;
for (auto& name : parser)
{
@ -840,9 +835,9 @@ int main(int argc, char* argv[])
auto colon = find(name, ':');
if (auto line = str_to_int_ifp({name.begin()+1, colon}))
{
target_coord.line = *line - 1;
init_coord.line = *line - 1;
if (colon != name.end())
target_coord.column = str_to_int_ifp({colon+1, name.end()}).value_or(1) - 1;
init_coord.column = str_to_int_ifp({colon+1, name.end()}).value_or(1) - 1;
continue;
}
@ -854,11 +849,11 @@ int main(int argc, char* argv[])
StringView session = parser.get_switch("s").value_or(StringView{});
try
{
return run_server(session, init_command,
return run_server(session, init_cmds, init_coord,
(bool)parser.get_switch("n"),
(bool)parser.get_switch("d"),
(bool)parser.get_switch("ro"),
ui_type, files, target_coord);
ui_type, files);
}
catch (convert_to_client_mode& convert)
{

View File

@ -650,13 +650,13 @@ private:
{
case MessageType::Connect:
{
auto init_command = m_reader.read<String>();
auto init_cmds = m_reader.read<String>();
auto dimensions = m_reader.read<DisplayCoord>();
auto env_vars = m_reader.read_idmap<String, MemoryDomain::EnvVars>();
RemoteUI* ui = new RemoteUI{sock, dimensions};
if (auto* client = ClientManager::instance().create_client(
std::unique_ptr<UserInterface>{ui},
std::move(env_vars), init_command))
std::unique_ptr<UserInterface>(ui),
std::move(env_vars), init_cmds, {}))
ui->set_client(client);
Server::instance().remove_accepter(this);