Add support for running kakoune as a filter, using -f 'keys'

It will cycle on every given files, apply the keys and write to
<filename>.kak-out. Only normal/insert mode is available, kakrc
are not read.
This commit is contained in:
Maxime Coste 2014-08-14 23:51:24 +01:00
parent 0588780866
commit ceb10665d1
4 changed files with 50 additions and 4 deletions

View File

@ -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()

View File

@ -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{};
}
}

View File

@ -385,6 +385,36 @@ int run_server(StringView session, StringView init_command,
return 0;
}
int run_filter(StringView keystr, memoryview<StringView> 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<StringView> 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
{

View File

@ -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),