From 132c31042ec7a5b4afc7a856791f9498eea99ca3 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Wed, 17 Oct 2012 17:49:34 +0200 Subject: [PATCH] main: prepare for multi client support --- src/main.cc | 85 +++++++++++++++++++++++++++++++++-------------------- 1 file changed, 53 insertions(+), 32 deletions(-) diff --git a/src/main.cc b/src/main.cc index 461b8e6f..f05052ca 100644 --- a/src/main.cc +++ b/src/main.cc @@ -450,6 +450,53 @@ std::unordered_map> keymap = void run_unit_tests(); +struct Client +{ + std::unique_ptr ui; + std::unique_ptr input_handler; + std::unique_ptr context; + + Client(UserInterface* ui, Window& window) + : ui(ui), + input_handler(new InputHandler{}), + context(new Context(*input_handler, window, *ui)) {} +}; + +Client create_client(const String& file) +{ + Buffer* buffer = nullptr; + UserInterface* ui = new NCursesUI{}; + if (not file.empty()) + { + buffer = create_buffer_from_file(file); + if (not buffer) + { + ui->print_status("new file " + file, -1); + buffer = new Buffer(file, Buffer::Type::NewFile); + } + } + else + buffer = new Buffer("*scratch*", Buffer::Type::Scratch); + + Client client{ui, *buffer->get_or_create_window()}; + + InputHandler* input_handler = client.input_handler.get(); + Context* context = client.context.get(); + EventManager::instance().watch(0, [=](int) { + try + { + input_handler->handle_next_input(*context); + } + catch (Kakoune::runtime_error& error) + { + ui->print_status(error.description(), -1); + } + }); + + context->draw_ifn(); + return client; +} + int main(int argc, char* argv[]) { EventManager event_manager; @@ -498,11 +545,12 @@ int main(int argc, char* argv[]) register_highlighters(); register_filters(); + write_debug("*** This is the debug buffer, where debug info will be written ***\n"); + write_debug("utf-8 test: é á ï"); + + std::vector clients; try { - InputHandler input_handler; - NCursesUI ui; - try { Context initialisation_context; @@ -511,38 +559,11 @@ int main(int argc, char* argv[]) } catch (Kakoune::runtime_error& error) { - ui.print_status(error.description(), -1); + write_debug("error while parsing kakrc: " + error.description()); } - write_debug("*** This is the debug buffer, where debug info will be written ***\n"); - write_debug("utf-8 test: é á ï"); + clients.push_back(create_client(argc > 1 ? argv[1] : "")); - Buffer* buffer = nullptr; - if (argc > 1) - { - buffer = create_buffer_from_file(argv[1]); - if (not buffer) - { - ui.print_status("new file "_str + argv[1], -1); - buffer = new Buffer(argv[1], Buffer::Type::NewFile); - } - } - else - buffer = new Buffer("*scratch*", Buffer::Type::Scratch); - - Context context(input_handler, *buffer->get_or_create_window(), ui); - event_manager.watch(0, [&](int) { - try - { - input_handler.handle_next_input(context); - } - catch (Kakoune::runtime_error& error) - { - context.print_status(error.description()); - } - }); - - context.draw_ifn(); while(not quit_requested) event_manager.handle_next_events(); }