parent
02d1f7f6c8
commit
1077799f63
5 changed files with 103 additions and 2 deletions
@ -0,0 +1,70 @@ |
|||||||
|
#include <modes/Search.hpp> |
||||||
|
#include <Editor.hpp> |
||||||
|
#include <regex> |
||||||
|
|
||||||
|
|
||||||
|
namespace groove { |
||||||
|
namespace modes { |
||||||
|
|
||||||
|
void Search::input(int c) { |
||||||
|
this->editor.movement(c); |
||||||
|
switch (c) { |
||||||
|
case 27: |
||||||
|
this->editor.mode_ = groove::Mode::EDIT; |
||||||
|
this->editor.input(' '); |
||||||
|
break; |
||||||
|
case KEY_BACKSPACE: |
||||||
|
if (this->keyword.size() > 0) |
||||||
|
this->keyword.pop_back(); |
||||||
|
break; |
||||||
|
case KEY_ENTER: |
||||||
|
case 10: |
||||||
|
this->search(); |
||||||
|
break; |
||||||
|
default: |
||||||
|
{ |
||||||
|
std::regex regex("[ -~]"); |
||||||
|
std::string character(1, c); |
||||||
|
if (!std::regex_match(character, regex)) |
||||||
|
break; |
||||||
|
this->keyword += c; |
||||||
|
} |
||||||
|
break; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
void Search::search() { |
||||||
|
if (this->keyword == "") |
||||||
|
return; |
||||||
|
std::regex regex(this->keyword); |
||||||
|
int pos = this->match == 0 ? this->position + 1 : this->position; |
||||||
|
for (long i = pos; i < this->editor.buffer->size(); i++) { |
||||||
|
if (i >= this->editor.buffer->size() - 1) |
||||||
|
this->position = 0; |
||||||
|
std::string line = this->editor.buffer->at(i); |
||||||
|
try { |
||||||
|
std::sregex_iterator next(line.begin(), line.end(), regex); |
||||||
|
std::sregex_iterator end; |
||||||
|
if (next == end) { |
||||||
|
this->match = 0; |
||||||
|
} |
||||||
|
while (next != end) { |
||||||
|
std::smatch match = *next; |
||||||
|
for (unsigned n = this->match; n < match.size(); ++n) { |
||||||
|
this->editor.y = i; |
||||||
|
this->editor.offset = i - LINES / 2; |
||||||
|
this->editor.x = match.position(n); |
||||||
|
this->position = i; |
||||||
|
this->match++; |
||||||
|
return; |
||||||
|
} |
||||||
|
next++; |
||||||
|
} |
||||||
|
} catch (std::regex_error& e) { |
||||||
|
// Syntax error in the regular expression
|
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
|
} |
@ -0,0 +1,24 @@ |
|||||||
|
#pragma once |
||||||
|
|
||||||
|
#include <modes/Mode.hpp> |
||||||
|
|
||||||
|
namespace groove { |
||||||
|
namespace modes { |
||||||
|
|
||||||
|
class Search : public Mode { |
||||||
|
private: |
||||||
|
std::string keyword; |
||||||
|
long position; |
||||||
|
int match; |
||||||
|
|
||||||
|
void search(); |
||||||
|
public: |
||||||
|
Search(groove::Editor& editor) : Mode(editor), keyword(""), position(0), match(0) {}; |
||||||
|
std::string status() { |
||||||
|
return "\tFind: " + this->keyword + ' ' + this->status_; |
||||||
|
} |
||||||
|
void input(int c); |
||||||
|
}; |
||||||
|
|
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue