Add a source command to execute commands from file

a kakrc file in current directory is sourced automatically
at start
This commit is contained in:
Maxime Coste 2011-11-27 12:59:59 +00:00
parent de19aeb035
commit 70e0393c4d

View File

@ -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<std::string>{ "hook" }, add_hook);
command_manager.register_command(std::vector<std::string>{ "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);