From b370f91025a6847fe3204e34f5e9016b34ef4f0f Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Sat, 26 Apr 2014 15:09:07 +0100 Subject: [PATCH] Add -init switch to :prompt to set the initial text Fixes #97 --- src/commands.cc | 11 +++++++++-- src/input_handler.cc | 14 +++++++++----- src/input_handler.hh | 5 +++-- src/normal.cc | 6 +++--- 4 files changed, 24 insertions(+), 12 deletions(-) diff --git a/src/commands.cc b/src/commands.cc index 7a621647..ae0115a7 100644 --- a/src/commands.cc +++ b/src/commands.cc @@ -1068,7 +1068,10 @@ const CommandDesc prompt_cmd = { nullptr, "prompt : prompt the use to enter a text string " "stores it in and then executes ", - ParameterDesc{ SwitchMap{}, ParameterDesc::Flags::None, 3, 3 }, + ParameterDesc{ + SwitchMap{ { "init", { true, "set initial prompt content" } } }, + ParameterDesc::Flags::None, 3, 3 + }, CommandFlags::None, CommandCompleter{}, [](const ParametersParser& params, Context& context) @@ -1078,8 +1081,12 @@ const CommandDesc prompt_cmd = { const char reg = params[1][0]; const String& command = params[2]; + String initstr; + if (params.has_option("init")) + initstr = params.option_value("init"); + context.input_handler().prompt( - params[0], get_color("Prompt"), Completer{}, + params[0], std::move(initstr), get_color("Prompt"), Completer{}, [=](const String& str, PromptEvent event, Context& context) { if (event != PromptEvent::Validate) diff --git a/src/input_handler.cc b/src/input_handler.cc index 49c02367..d7274b6d 100644 --- a/src/input_handler.cc +++ b/src/input_handler.cc @@ -353,13 +353,15 @@ class Prompt : public InputMode { public: Prompt(InputHandler& input_handler, const String& prompt, - ColorPair colors, Completer completer, PromptCallback callback) + String initstr, ColorPair colors, Completer completer, + PromptCallback callback) : InputMode(input_handler), m_prompt(prompt), m_prompt_colors(colors), m_completer(completer), m_callback(callback) { m_history_it = ms_history[m_prompt].end(); if (context().options()["autoshowcompl"].get()) refresh_completions(CompletionFlags::Fast); + m_line_editor.reset(std::move(initstr)); display(); } @@ -1259,11 +1261,13 @@ void InputHandler::repeat_last_insert() kak_assert(dynamic_cast(m_mode.get()) != nullptr); } -void InputHandler::prompt(const String& prompt, ColorPair prompt_colors, - Completer completer, PromptCallback callback) +void InputHandler::prompt(const String& prompt, String initstr, + ColorPair prompt_colors, Completer completer, + PromptCallback callback) { - change_input_mode(new InputModes::Prompt(*this, prompt, prompt_colors, - completer, callback)); + change_input_mode(new InputModes::Prompt(*this, prompt, initstr, + prompt_colors, completer, + callback)); } void InputHandler::set_prompt_colors(ColorPair prompt_colors) diff --git a/src/input_handler.hh b/src/input_handler.hh index 1d2e992e..0d9d34cc 100644 --- a/src/input_handler.hh +++ b/src/input_handler.hh @@ -47,8 +47,9 @@ public: // abort or validation with corresponding PromptEvent value // returns to normal mode after validation if callback does // not change the mode itself - void prompt(const String& prompt, ColorPair prompt_colors, - Completer completer, PromptCallback callback); + void prompt(const String& prompt, String initstr, + ColorPair prompt_colors, Completer completer, + PromptCallback callback); void set_prompt_colors(ColorPair prompt_colors); // enter menu mode, callback is called on each selection change, diff --git a/src/normal.cc b/src/normal.cc index 8b691190..1e2edd82 100644 --- a/src/normal.cc +++ b/src/normal.cc @@ -426,7 +426,7 @@ void for_each_char(Context& context, int) void command(Context& context, int) { context.input_handler().prompt( - ":", get_color("Prompt"), + ":", "", get_color("Prompt"), std::bind(&CommandManager::complete, &CommandManager::instance(), _1, _2, _3, _4), [](const String& cmdline, PromptEvent event, Context& context) { if (context.has_ui()) @@ -451,7 +451,7 @@ template void pipe(Context& context, int) { const char* prompt = mode == InsertMode::Replace ? "pipe:" : "pipe (ins):"; - context.input_handler().prompt(prompt, get_color("Prompt"), shell_complete, + context.input_handler().prompt(prompt, "", get_color("Prompt"), shell_complete, [](const String& cmdline, PromptEvent event, Context& context) { if (event != PromptEvent::Validate) @@ -589,7 +589,7 @@ template void regex_prompt(Context& context, const String prompt, T func) { DynamicSelectionList selections{context.buffer(), context.selections()}; - context.input_handler().prompt(prompt, get_color("Prompt"), complete_nothing, + context.input_handler().prompt(prompt, "", get_color("Prompt"), complete_nothing, [=](const String& str, PromptEvent event, Context& context) { try {