You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
70 lines
1.6 KiB
70 lines
1.6 KiB
#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
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
} |