From a65e8142f37fc8c22fc697440ea5632d604ff91f Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Thu, 1 Dec 2016 20:40:50 +0000 Subject: [PATCH] Rework handling of initial coordinates so that init commands can change them Fixes #944 --- src/client_manager.cc | 11 +++++++--- src/client_manager.hh | 3 ++- src/main.cc | 47 +++++++++++++++++++------------------------ src/remote.cc | 6 +++--- 4 files changed, 34 insertions(+), 33 deletions(-) diff --git a/src/client_manager.cc b/src/client_manager.cc index 8edaeb2f..97ace9e1 100644 --- a/src/client_manager.cc +++ b/src/client_manager.cc @@ -37,8 +37,8 @@ String ClientManager::generate_name() const } Client* ClientManager::create_client(std::unique_ptr&& 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&& 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) { diff --git a/src/client_manager.hh b/src/client_manager.hh index de99fceb..6b7cac63 100644 --- a/src/client_manager.hh +++ b/src/client_manager.hh @@ -21,7 +21,8 @@ public: ~ClientManager(); Client* create_client(std::unique_ptr&& 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(); } diff --git a/src/main.cc b/src/main.cc index 3385d423..0a72b7e6 100644 --- a/src/main.cc +++ b/src/main.cc @@ -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 files, BufferCoord target_coord) + ConstArrayView 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) - local_client->print_status({ - "error during startup, see *debug* buffer for details", - get_face("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 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) { diff --git a/src/remote.cc b/src/remote.cc index 743483ad..e402322e 100644 --- a/src/remote.cc +++ b/src/remote.cc @@ -650,13 +650,13 @@ private: { case MessageType::Connect: { - auto init_command = m_reader.read(); + auto init_cmds = m_reader.read(); auto dimensions = m_reader.read(); auto env_vars = m_reader.read_idmap(); RemoteUI* ui = new RemoteUI{sock, dimensions}; if (auto* client = ClientManager::instance().create_client( - std::unique_ptr{ui}, - std::move(env_vars), init_command)) + std::unique_ptr(ui), + std::move(env_vars), init_cmds, {})) ui->set_client(client); Server::instance().remove_accepter(this);