Change a-| to ignore output, and add ! and a-! for insert/append cmd output

This commit is contained in:
Maxime Coste 2014-12-11 23:21:11 +00:00
parent c68c9abcac
commit 9c1ca303ff

View File

@ -367,10 +367,10 @@ void command(Context& context, NormalParams)
}); });
} }
template<InsertMode mode> template<bool replace>
void pipe(Context& context, NormalParams) void pipe(Context& context, NormalParams)
{ {
const char* prompt = mode == InsertMode::Replace ? "pipe:" : "pipe (ins):"; const char* prompt = replace ? "pipe:" : "pipe-to:";
context.input_handler().prompt(prompt, "", get_face("Prompt"), shell_complete, context.input_handler().prompt(prompt, "", get_face("Prompt"), shell_complete,
[](StringView cmdline, PromptEvent event, Context& context) [](StringView cmdline, PromptEvent event, Context& context)
{ {
@ -391,21 +391,60 @@ void pipe(Context& context, NormalParams)
Buffer& buffer = context.buffer(); Buffer& buffer = context.buffer();
SelectionList& selections = context.selections(); SelectionList& selections = context.selections();
std::vector<String> strings; if (replace)
for (auto& sel : selections)
{ {
auto str = content(buffer, sel); std::vector<String> strings;
bool insert_eol = str.back() != '\n'; for (auto& sel : selections)
if (insert_eol) {
str += '\n'; auto str = content(buffer, sel);
str = ShellManager::instance().pipe(str, real_cmd, context, bool insert_eol = str.back() != '\n';
{}, EnvVarMap{}); if (insert_eol)
if (insert_eol and str.back() == '\n') str += '\n';
str = str.substr(0, str.length()-1); str = ShellManager::instance().pipe(str, real_cmd, context,
strings.push_back(str); {}, EnvVarMap{});
if (insert_eol and str.back() == '\n')
str = str.substr(0, str.length()-1);
strings.push_back(std::move(str));
}
ScopedEdition edition(context);
selections.insert(strings, InsertMode::Replace);
} }
else
{
for (auto& sel : selections)
ShellManager::instance().pipe(
content(buffer, sel), real_cmd, context, {},
EnvVarMap{});
}
});
}
template<InsertMode mode>
void insert_output(Context& context, NormalParams)
{
const char* prompt = mode == InsertMode::Insert ? "insert-output:" : "append-output:";
context.input_handler().prompt(prompt, "", get_face("Prompt"), shell_complete,
[](StringView cmdline, PromptEvent event, Context& context)
{
if (event != PromptEvent::Validate)
return;
StringView real_cmd;
if (cmdline.empty())
real_cmd = context.main_sel_register_value("|");
else
{
RegisterManager::instance()['|'] = String{cmdline};
real_cmd = cmdline;
}
if (real_cmd.empty())
return;
auto str = ShellManager::instance().eval(real_cmd, context, {},
EnvVarMap{});
ScopedEdition edition(context); ScopedEdition edition(context);
selections.insert(strings, mode); context.selections().insert(str, mode);
}); });
} }
@ -1313,8 +1352,11 @@ KeyMap keymap =
{ '%', { "select whole buffer", [](Context& context, NormalParams) { select_buffer(context.selections()); } } }, { '%', { "select whole buffer", [](Context& context, NormalParams) { select_buffer(context.selections()); } } },
{ ':', { "enter command prompt", command } }, { ':', { "enter command prompt", command } },
{ '|', { "pipe each selection through filter and replace with output", pipe<InsertMode::Replace> } }, { '|', { "pipe each selection through filter and replace with output", pipe<true> } },
{ alt('|'), { "pipe each selection through filter and append with output", pipe<InsertMode::Append> } }, { alt('|'), { "pipe each selection through command and ignore output", pipe<false> } },
{ '!', { "insert command output", insert_output<InsertMode::Insert> } },
{ alt('!'), { "append command output", insert_output<InsertMode::Append> } },
{ ' ', { "remove all selection except main", [](Context& context, NormalParams p) { keep_selection(context.selections(), p.count ? p.count-1 : context.selections().main_index()); } } }, { ' ', { "remove all selection except main", [](Context& context, NormalParams p) { keep_selection(context.selections(), p.count ? p.count-1 : context.selections().main_index()); } } },
{ alt(' '), { "remove main selection", [](Context& context, NormalParams p) { remove_selection(context.selections(), p.count ? p.count-1 : context.selections().main_index()); } } }, { alt(' '), { "remove main selection", [](Context& context, NormalParams p) { remove_selection(context.selections(), p.count ? p.count-1 : context.selections().main_index()); } } },
{ ';', { "reduce selections to their cursor", [](Context& context, NormalParams) { clear_selections(context.selections()); } } }, { ';', { "reduce selections to their cursor", [](Context& context, NormalParams) { clear_selections(context.selections()); } } },