mirror of
https://github.com/mawww/kakoune.git
synced 2024-12-20 10:01:57 +03:00
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:
parent
0588780866
commit
ceb10665d1
@ -19,12 +19,14 @@ FDWatcher::~FDWatcher()
|
|||||||
Timer::Timer(TimePoint date, Callback callback)
|
Timer::Timer(TimePoint date, Callback callback)
|
||||||
: m_date{date}, m_callback{std::move(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()
|
Timer::~Timer()
|
||||||
{
|
{
|
||||||
EventManager::instance().m_timers.erase(this);
|
if (EventManager::has_instance())
|
||||||
|
EventManager::instance().m_timers.erase(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Timer::run()
|
void Timer::run()
|
||||||
|
@ -27,7 +27,9 @@ private:
|
|||||||
|
|
||||||
inline Face get_face(const String& facedesc)
|
inline Face get_face(const String& facedesc)
|
||||||
{
|
{
|
||||||
return FaceRegistry::instance()[facedesc];
|
if (FaceRegistry::has_instance())
|
||||||
|
return FaceRegistry::instance()[facedesc];
|
||||||
|
return Face{};
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
41
src/main.cc
41
src/main.cc
@ -385,6 +385,36 @@ int run_server(StringView session, StringView init_command,
|
|||||||
return 0;
|
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)
|
int run_pipe(StringView session)
|
||||||
{
|
{
|
||||||
char buf[512];
|
char buf[512];
|
||||||
@ -424,6 +454,14 @@ int kakoune(const ParametersParser& parser)
|
|||||||
}
|
}
|
||||||
return run_pipe(parser.option_value("p"));
|
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;
|
String init_command;
|
||||||
if (parser.has_option("e"))
|
if (parser.has_option("e"))
|
||||||
@ -476,7 +514,8 @@ int main(int argc, char* argv[])
|
|||||||
{ "n", { false, "do not source kakrc files on startup" } },
|
{ "n", { false, "do not source kakrc files on startup" } },
|
||||||
{ "s", { true, "set session name" } },
|
{ "s", { true, "set session name" } },
|
||||||
{ "d", { false, "run as a headless session (requires -s)" } },
|
{ "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
|
try
|
||||||
{
|
{
|
||||||
|
@ -341,6 +341,9 @@ void for_each_char(Context& context, int)
|
|||||||
|
|
||||||
void command(Context& context, int)
|
void command(Context& context, int)
|
||||||
{
|
{
|
||||||
|
if (not CommandManager::has_instance())
|
||||||
|
return;
|
||||||
|
|
||||||
context.input_handler().prompt(
|
context.input_handler().prompt(
|
||||||
":", "", get_face("Prompt"),
|
":", "", get_face("Prompt"),
|
||||||
std::bind(&CommandManager::complete, &CommandManager::instance(), _1, _2, _3, _4),
|
std::bind(&CommandManager::complete, &CommandManager::instance(), _1, _2, _3, _4),
|
||||||
|
Loading…
Reference in New Issue
Block a user