Add support for -itersel option in exec/eval

-itersel makes a -draft eval/exec run once for each selections
separately rather than with all selections at a time.
This commit is contained in:
Maxime Coste 2013-11-05 23:50:44 +00:00
parent 088f670fe9
commit 7495d04a47
3 changed files with 27 additions and 9 deletions

View File

@ -299,10 +299,14 @@ Commands are entered using +:+.
is relative to kak executable path.
* +nameclient <name>+: set current client name
* +namebuf <name>+: set current buffer name
* +exec [-client <name>] <keys>+: execute <keys> as if pressed in normal mode.
if client if specified, exec keys in the named client context.
* +eval [-client <name>] <command>+: execute <command> as if entered in command line
if client if specified, exec command in the named client context.
* +exec [-client <name>] [-draft [-itersel]] <keys>+: execute <keys> as if
pressed in normal mode. if +client+ if specified, exec keys in the named
client context. if +draft+ is specified, execute the keys in a draft
context. if +itersel+ is specified, apply the keys on each selection
separately.
* +eval [-client <name>] [-draft [-itersel]] <command>+: execute <command> as
if entered in command line. +draft+, +client+ and +itersel+ works the same
as for the +exec+ command.
* +echo <text>+: show <text> in status line
* +name <name>+: sets current client name to name
* +nop+: does nothing, but as with every other commands, arguments may be

View File

@ -540,7 +540,7 @@ public:
template<typename Func>
void context_wrap(CommandParameters params, Context& context, Func func)
{
ParametersParser parser(params, { { "client", true }, { "draft", false }},
ParametersParser parser(params, { { "client", true }, { "draft", false }, { "itersel", false } },
ParametersParser::Flags::OptionsOnlyAtStart, 1);
Context& real_context = parser.has_option("client") ?
@ -554,10 +554,24 @@ void context_wrap(CommandParameters params, Context& context, Func func)
real_context.has_client() ? real_context.client().name() : "");
DynamicSelectionList sels{editor.buffer(), editor.selections()};
auto restore_sels = on_scope_end([&]{ editor.select(sels); });
func(parser, client.context());
if (parser.has_option("itersel"))
{
for (auto& sel : sels)
{
editor.select(sel);
func(parser, client.context());
}
}
else
func(parser, client.context());
}
else
{
if (parser.has_option("itersel"))
throw runtime_error("-itersel makes no sense without -draft");
func(parser, real_context);
}
// force redraw of this client window
if (parser.has_option("client") and real_context.has_window())

View File

@ -19,13 +19,13 @@ hook global WinSetOption filetype=cpp %~
addhl -group cpp-highlight regex "(?<!')\".*?(?<!\\)(\\\\)*\"" 0:string
addhl -group cpp-highlight regex "(//[^\n]*\n)|(/\*.*?(\*/|\'))" 0:comment
hook window InsertEnd .* -id cpp-hooks %{ try %{ exec -draft <a-x>s\h+$<ret>d } } # cleanup trailing whitespaces when exiting insert mode
hook window InsertChar \n -id cpp-hooks %[ try %{ exec -draft k<a-x>s\h+$<ret>d } ] # cleanup trailing white space son previous line
hook window InsertChar \n -id cpp-indent %@
hook window InsertChar \n -id cpp-indent %@ eval -draft -itersel %_
try %{ exec -draft k<a-x>s^\h+<ret>yj<a-h>P } # preserve previous line indent
try %[ exec -draft k<a-x><a-k>[{(]\h*$<ret>j<a-gt> ] # indent after lines ending with { or (
try %{ exec -draft k<a-x>s\h+$<ret>d } # cleanup trailing white space son previous line
try %{ exec -draft [(<a-k>\`\([^\n]+\n[^\n]*\n?\'<ret>s\`..|.\'<ret>& } # align to opening paren of previous line
@
_ @
hook window InsertChar \} -id cpp-indent %[ try %[ exec -draft <a-h><a-k>^\h+\}$<ret>< ] ] # deindent on insert } alone on a line
~