1
1
mirror of https://github.com/mawww/kakoune.git synced 2024-12-15 06:02:49 +03:00

Add -save-regs <regs> option to exec and eval to preserve registers

Fixes #279
This commit is contained in:
Maxime Coste 2015-11-25 23:40:38 +00:00
parent 3644f2a056
commit 001da44e11
2 changed files with 46 additions and 19 deletions

View File

@ -976,6 +976,8 @@ Some parameters provide a way to change the context of execution:
on all buffers.
* `-no-hooks`: disable hook execution while executing the keys/commands
* `-with-maps`: use user key mapping in `:exec` instead of built in keys.
* `-save-regs <regs>`: regs is a string of registers to be restored after
execution.
The execution stops when the last key/command is reached, or an error
is raised.

View File

@ -1198,7 +1198,8 @@ const ParameterDesc context_wrap_params = {
{ "draft", { false, "run in a disposable context" } },
{ "no-hooks", { false, "disable hooks" } },
{ "with-maps", { false, "use user defined key mapping when executing keys" } },
{ "itersel", { false, "run once for each selection with that selection as the only one" } } },
{ "itersel", { false, "run once for each selection with that selection as the only one" } },
{ "save-regs", { true, "restore all given registers after execution" } } },
ParameterDesc::Flags::SwitchesOnlyAtStart, 1
};
@ -1216,6 +1217,41 @@ private:
T m_prev_value;
};
class RegisterRestorer
{
public:
RegisterRestorer(char name, const Context& context)
: m_name(name)
{
ConstArrayView<String> save = RegisterManager::instance()[name].values(context);
m_save = Vector<String>(save.begin(), save.end());
}
RegisterRestorer(RegisterRestorer&& other) noexcept
: m_save(std::move(other.m_save)), m_name(other.m_name)
{
other.m_name = 0;
}
RegisterRestorer& operator=(RegisterRestorer&& other) noexcept
{
m_save = std::move(other.m_save);
m_name = other.m_name;
other.m_name = 0;
return *this;
}
~RegisterRestorer()
{
if (m_name != 0)
RegisterManager::instance()[m_name] = m_save;
}
private:
Vector<String> m_save;
char m_name;
};
template<typename Func>
void context_wrap(const ParametersParser& parser, Context& context, Func func)
{
@ -1229,6 +1265,13 @@ void context_wrap(const ParametersParser& parser, Context& context, Func func)
context.user_hooks_disabled();
const bool disable_keymaps = not parser.get_switch("with-maps");
Vector<RegisterRestorer> saved_registers;
if (auto regs = parser.get_switch("save-regs"))
{
for (auto& r : *regs)
saved_registers.emplace_back(r, context);
}
ClientManager& cm = ClientManager::instance();
if (auto bufnames = parser.get_switch("buffer"))
{
@ -1652,24 +1695,6 @@ const CommandDesc change_working_directory_cmd = {
}
};
class RegisterRestorer
{
public:
RegisterRestorer(char name, const Context& context)
: m_name(name)
{
ConstArrayView<String> save = RegisterManager::instance()[name].values(context);
m_save = Vector<String>(save.begin(), save.end());
}
~RegisterRestorer()
{ RegisterManager::instance()[m_name] = m_save; }
private:
Vector<String> m_save;
char m_name;
};
}
void exec_keys(ConstArrayView<Key> keys, Context& context)