From 70e0393c4dd09dcdf0b818b1ac058c58707bc14e Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Sun, 27 Nov 2011 12:59:59 +0000 Subject: [PATCH] Add a source command to execute commands from file a kakrc file in current directory is sourced automatically at start --- src/main.cc | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/main.cc b/src/main.cc index 9cddfb9f..9971d4c4 100644 --- a/src/main.cc +++ b/src/main.cc @@ -476,6 +476,26 @@ void add_hook(const CommandParameters& params, const Context& context) }); } +void exec_commands_in_file(const CommandParameters& params, + const Context& context) +{ + if (params.size() != 1) + throw wrong_argument_count(); + + std::string file_content = read_file(params[0]); + CommandManager& cmd_manager = CommandManager::instance(); + + size_t pos = 0; + while (true) + { + size_t end_pos = file_content.find_first_of('\n', pos); + cmd_manager.execute(file_content.substr(pos, end_pos), context); + if (end_pos == std::string::npos) + break; + pos = end_pos + 1; + } +} + void do_command() { try @@ -688,8 +708,20 @@ int main(int argc, char* argv[]) }); command_manager.register_command(std::vector{ "hook" }, add_hook); + command_manager.register_command(std::vector{ "source" }, exec_commands_in_file, + PerArgumentCommandCompleter{ complete_filename }); + register_filters(); + try + { + exec_commands_in_file({ "kakrc" }, main_context); + } + catch (Kakoune::runtime_error& error) + { + print_status(error.description()); + } + try { auto buffer = (argc > 1) ? open_or_create(argv[1]) : new Buffer("*scratch*", Buffer::Type::Scratch);