implemented rudimentary search

development
Michael Ochmann 8 years ago
parent 02d1f7f6c8
commit 1077799f63
  1. 2
      src/Editor.cpp
  2. 5
      src/Editor.hpp
  3. 4
      src/modes/Edit.cpp
  4. 70
      src/modes/Search.cpp
  5. 24
      src/modes/Search.hpp

@ -1,7 +1,6 @@
#include <Editor.hpp>
#include <Highlighter.hpp>
#include <fstream>
#include <iostream>
#include <math.h>
namespace groove {
@ -15,6 +14,7 @@ namespace groove {
this->modes.emplace(Mode::QUIT, std::make_unique<modes::Quit>(*this));
this->modes.emplace(Mode::SAVE, std::make_unique<modes::Save>(*this));
this->modes.emplace(Mode::EXIT, std::make_unique<modes::Exit>(*this));
this->modes.emplace(Mode::SEARCH, std::make_unique<modes::Search>(*this));
this->lineMode = this->config.get<std::string>("linenumbers") == "relative" ? LineMode::RELATIVE :
(this->config.get<std::string>("linenumbers") == "none" ? LineMode::NONE : LineMode::NUMBERS);

@ -8,6 +8,7 @@
#include <modes/Edit.hpp>
#include <modes/Quit.hpp>
#include <modes/Save.hpp>
#include <modes/Search.hpp>
#include <History.hpp>
#include <config/ConfigParser.hpp>
#include <modes/Exit.hpp>
@ -19,7 +20,8 @@ namespace groove {
EDIT,
SAVE,
QUIT,
EXIT
EXIT,
SEARCH
};
enum LineMode {
@ -36,6 +38,7 @@ namespace groove {
friend class modes::Quit;
friend class modes::Save;
friend class modes::Exit;
friend class modes::Search;
friend class History;
private:
long x, y;

@ -88,6 +88,10 @@ namespace groove {
break;
}
break;
case 'f':
this->editor.mode_ = groove::Mode::SEARCH;
this->editor.input(' ');
break;
case 'u':
this->editor.history.undo();
break;

@ -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…
Cancel
Save