PromptHandler now call callback when edited and aborted.

Used for a new feature: incremental search
This commit is contained in:
Maxime Coste 2012-12-05 19:22:40 +01:00
parent 3438ba7ec3
commit b81ea0bc92
3 changed files with 41 additions and 17 deletions

View File

@ -280,7 +280,7 @@ public:
reset_normal_mode(); reset_normal_mode();
// call callback after reset_normal_mode so that callback // call callback after reset_normal_mode so that callback
// may change the mode // may change the mode
m_callback(line, context); m_callback(line, PromptEvent::Validate, context);
return; return;
} }
else if (key == Key::Escape or key == Key { Key::Modifiers::Control, 'c' }) else if (key == Key::Escape or key == Key { Key::Modifiers::Control, 'c' })
@ -288,6 +288,7 @@ public:
context.ui().print_status(""); context.ui().print_status("");
context.ui().menu_hide(); context.ui().menu_hide();
reset_normal_mode(); reset_normal_mode();
m_callback(line, PromptEvent::Abort, context);
return; return;
} }
else if (key == Key{Key::Modifiers::Control, 'r'}) else if (key == Key{Key::Modifiers::Control, 'r'})
@ -385,6 +386,7 @@ public:
} }
context.ui().print_status(m_prompt + line, context.ui().print_status(m_prompt + line,
m_prompt.char_length() + m_line_editor.cursor_pos()); m_prompt.char_length() + m_line_editor.cursor_pos());
m_callback(line, PromptEvent::Change, context);
} }
private: private:

View File

@ -13,7 +13,14 @@ class Editor;
class Context; class Context;
using MenuCallback = std::function<void (int, Context&)>; using MenuCallback = std::function<void (int, Context&)>;
using PromptCallback = std::function<void (const String&, Context&)>;
enum class PromptEvent
{
Change,
Abort,
Validate
};
using PromptCallback = std::function<void (const String&, PromptEvent, Context&)>;
using KeyCallback = std::function<void (const Key&, Context&)>; using KeyCallback = std::function<void (const Key&, Context&)>;
class InputMode; class InputMode;

View File

@ -107,15 +107,20 @@ void do_command(Context& context)
{ {
context.input_handler().prompt( context.input_handler().prompt(
":", std::bind(&CommandManager::complete, &CommandManager::instance(), _1, _2, _3), ":", std::bind(&CommandManager::complete, &CommandManager::instance(), _1, _2, _3),
[](const String& cmdline, Context& context) { CommandManager::instance().execute(cmdline, context); }, [](const String& cmdline, PromptEvent event, Context& context) {
context); if (event == PromptEvent::Validate)
CommandManager::instance().execute(cmdline, context);
}, context);
} }
void do_pipe(Context& context) void do_pipe(Context& context)
{ {
context.input_handler().prompt("|", complete_nothing, context.input_handler().prompt("|", complete_nothing,
[](const String& cmdline, Context& context) [](const String& cmdline, PromptEvent event, Context& context)
{ {
if (event != PromptEvent::Validate)
return;
Editor& editor = context.editor(); Editor& editor = context.editor();
std::vector<String> strings; std::vector<String> strings;
for (auto& sel : const_cast<const Editor&>(context.editor()).selections()) for (auto& sel : const_cast<const Editor&>(context.editor()).selections())
@ -129,14 +134,22 @@ void do_pipe(Context& context)
template<SelectMode mode> template<SelectMode mode>
void do_search(Context& context) void do_search(Context& context)
{ {
SelectionList selections = context.editor().selections();
context.input_handler().prompt("/", complete_nothing, context.input_handler().prompt("/", complete_nothing,
[](const String& str, Context& context) { [selections](const String& str, PromptEvent event, Context& context) {
String ex = str; context.editor().select(selections);
if (ex.empty())
ex = RegisterManager::instance()['/'].values(context)[0];
else
RegisterManager::instance()['/'] = ex;
if (str.empty() or event == PromptEvent::Abort)
return;
String ex = str;
if (event == PromptEvent::Validate)
{
if (ex.empty())
ex = RegisterManager::instance()['/'].values(context)[0];
else
RegisterManager::instance()['/'] = ex;
}
context.editor().select(std::bind(select_next_match, _1, ex), mode); context.editor().select(std::bind(select_next_match, _1, ex), mode);
}, context); }, context);
} }
@ -212,17 +225,19 @@ void do_paste(Context& context)
void do_select_regex(Context& context) void do_select_regex(Context& context)
{ {
context.input_handler().prompt("select: ", complete_nothing, context.input_handler().prompt("select: ", complete_nothing,
[](const String& ex, Context& context) [](const String& ex, PromptEvent event, Context& context) {
{ context.editor().multi_select(std::bind(select_all_matches, _1, ex)); }, if (event == PromptEvent::Validate)
context); context.editor().multi_select(std::bind(select_all_matches, _1, ex));
}, context);
} }
void do_split_regex(Context& context) void do_split_regex(Context& context)
{ {
context.input_handler().prompt("split: ", complete_nothing, context.input_handler().prompt("split: ", complete_nothing,
[](const String& ex, Context& context) [](const String& ex, PromptEvent event, Context& context) {
{ context.editor().multi_select(std::bind(split_selection, _1, ex)); }, if (event == PromptEvent::Validate)
context); context.editor().multi_select(std::bind(split_selection, _1, ex));
}, context);
} }
void do_join(Context& context) void do_join(Context& context)