diff options
| author | Maxime Coste <frrrwww@gmail.com> | 2011-12-20 19:22:05 +0000 |
|---|---|---|
| committer | Maxime Coste <frrrwww@gmail.com> | 2011-12-20 19:22:05 +0000 |
| commit | ba1fe056f482a32624968bb0364b04e47954d2b9 (patch) | |
| tree | 6f339178577a96869cb5cb3abb8e500e18761aa7 /src | |
| parent | 7116ed6d61777b09bdc424bf18e3aaef2ed20a5b (diff) | |
Add an exec command, similar to vim normal command
Diffstat (limited to 'src')
| -rw-r--r-- | src/main.cc | 53 | ||||
| -rw-r--r-- | src/window.cc | 2 |
2 files changed, 54 insertions, 1 deletions
diff --git a/src/main.cc b/src/main.cc index 415d9ffb..673f0887 100644 --- a/src/main.cc +++ b/src/main.cc @@ -12,6 +12,7 @@ #include "filters.hh" #include "filter_registry.hh" #include "hooks_manager.hh" +#include "keys.hh" #include <unordered_map> #include <map> @@ -172,7 +173,7 @@ void deinit_ncurses() struct prompt_aborted {}; -std::string prompt(const std::string& text, Completer completer = complete_nothing) +std::string ncurses_prompt(const std::string& text, Completer completer = complete_nothing) { int max_x, max_y; getmaxyx(stdscr, max_y, max_x); @@ -263,6 +264,13 @@ std::string prompt(const std::string& text, Completer completer = complete_nothi return result; } +static std::function<std::string (const std::string&, Completer)> prompt_func = ncurses_prompt; + +std::string prompt(const std::string& text, Completer completer = complete_nothing) +{ + return prompt_func(text, completer); +} + void print_status(const std::string& status) { int x,y; @@ -731,6 +739,47 @@ std::unordered_map<char, std::function<void (Window& window, int count)>> alt_ke { 'x', [](Window& window, int count) { window.multi_select(select_whole_lines); } }, }; +void exec_string(const CommandParameters& params, + const Context& context) +{ + if (params.size() != 1) + throw wrong_argument_count(); + + size_t pos = 0; + + KeyList keys = parse_keys(params[0]); + + prompt_func = [&](const std::string&, Completer) { + size_t begin = pos; + while (pos < keys.size() and keys[pos].key != '\n') + ++pos; + + std::string result; + for (size_t i = begin; i < pos; ++i) + result += keys[i].key; + ++pos; + + return result; + }; + + auto restore_prompt = on_scope_end([&]() { prompt_func = ncurses_prompt; }); + + int count = 0; + while(pos < keys.size()) + { + const Key& key = keys[pos++]; + + if (key.modifiers == Key::Modifiers::None and isdigit(key.key)) + count = count * 10 + key.key - '0'; + else + { + if (keymap.find(key.key) != keymap.end()) + keymap[key.key](*context.window, count); + count = 0; + } + } +} + int main(int argc, char* argv[]) { init_ncurses(); @@ -777,6 +826,8 @@ int main(int argc, char* argv[]) command_manager.register_command(std::vector<std::string>{ "source" }, exec_commands_in_file, PerArgumentCommandCompleter{ complete_filename }); + command_manager.register_command(std::vector<std::string>{ "exec" }, exec_string); + register_highlighters(); register_filters(); diff --git a/src/window.cc b/src/window.cc index bcdfc137..9770ca50 100644 --- a/src/window.cc +++ b/src/window.cc @@ -433,6 +433,8 @@ IncrementalInserter::IncrementalInserter(Window& window, Mode mode) IncrementalInserter::~IncrementalInserter() { + HooksManager::instance().run_hook("WinInsertEnd", "", Context(m_window)); + move_cursor(DisplayCoord(0, -1)); assert(m_window.m_current_inserter == this); m_window.m_current_inserter = nullptr; |
