From ceb10665d1f76f6ad8a9360d2cb70176baa9e889 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Thu, 14 Aug 2014 23:51:24 +0100 Subject: [PATCH] Add support for running kakoune as a filter, using -f 'keys' It will cycle on every given files, apply the keys and write to .kak-out. Only normal/insert mode is available, kakrc are not read. --- src/event_manager.cc | 6 ++++-- src/face_registry.hh | 4 +++- src/main.cc | 41 ++++++++++++++++++++++++++++++++++++++++- src/normal.cc | 3 +++ 4 files changed, 50 insertions(+), 4 deletions(-) diff --git a/src/event_manager.cc b/src/event_manager.cc index 9a51b885b..152791ad3 100644 --- a/src/event_manager.cc +++ b/src/event_manager.cc @@ -19,12 +19,14 @@ FDWatcher::~FDWatcher() Timer::Timer(TimePoint date, Callback callback) : m_date{date}, m_callback{std::move(callback)} { - EventManager::instance().m_timers.insert(this); + if (EventManager::has_instance()) + EventManager::instance().m_timers.insert(this); } Timer::~Timer() { - EventManager::instance().m_timers.erase(this); + if (EventManager::has_instance()) + EventManager::instance().m_timers.erase(this); } void Timer::run() diff --git a/src/face_registry.hh b/src/face_registry.hh index 08ceed49b..80a30ffc7 100644 --- a/src/face_registry.hh +++ b/src/face_registry.hh @@ -27,7 +27,9 @@ private: inline Face get_face(const String& facedesc) { - return FaceRegistry::instance()[facedesc]; + if (FaceRegistry::has_instance()) + return FaceRegistry::instance()[facedesc]; + return Face{}; } } diff --git a/src/main.cc b/src/main.cc index c24c8b07e..81430fed8 100644 --- a/src/main.cc +++ b/src/main.cc @@ -385,6 +385,36 @@ int run_server(StringView session, StringView init_command, return 0; } +int run_filter(StringView keystr, memoryview files) +{ + GlobalOptions global_options; + GlobalHooks global_hooks; + GlobalKeymaps global_keymaps; + ShellManager shell_manager; + BufferManager buffer_manager; + RegisterManager register_manager; + + register_env_vars(); + register_registers(); + + auto keys = parse_keys(keystr); + + for (auto& file : files) + { + Buffer* buffer = create_buffer_from_file(file); + InputHandler input_handler{{ *buffer, Selection{} }}; + + for (auto& key : keys) + input_handler.handle_key(key); + + write_buffer_to_file(*buffer, file + ".kak-out"); + + buffer_manager.delete_buffer(*buffer); + } + buffer_manager.clear_buffer_trash(); + return 0; +} + int run_pipe(StringView session) { char buf[512]; @@ -424,6 +454,14 @@ int kakoune(const ParametersParser& parser) } return run_pipe(parser.option_value("p")); } + else if (parser.has_option("f")) + { + std::vector files; + for (size_t i = 0; i < parser.positional_count(); ++i) + files.emplace_back(parser[i]); + + return run_filter(parser.option_value("f"), files); + } String init_command; if (parser.has_option("e")) @@ -476,7 +514,8 @@ int main(int argc, char* argv[]) { "n", { false, "do not source kakrc files on startup" } }, { "s", { true, "set session name" } }, { "d", { false, "run as a headless session (requires -s)" } }, - { "p", { true, "just send stdin as commands to the given session" } } } + { "p", { true, "just send stdin as commands to the given session" } }, + { "f", { true, "act as a filter, executing given keys on given files" } } } }; try { diff --git a/src/normal.cc b/src/normal.cc index 04aef5e3f..34428a1aa 100644 --- a/src/normal.cc +++ b/src/normal.cc @@ -341,6 +341,9 @@ void for_each_char(Context& context, int) void command(Context& context, int) { + if (not CommandManager::has_instance()) + return; + context.input_handler().prompt( ":", "", get_face("Prompt"), std::bind(&CommandManager::complete, &CommandManager::instance(), _1, _2, _3, _4),