forked from mika314/texteditor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathisearch_buffer.cpp
72 lines (66 loc) · 1.78 KB
/
isearch_buffer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include "isearch_buffer.hpp"
#include "screen.hpp"
#include <cassert>
#include <iostream>
#include <algorithm>
IsearchBuffer::IsearchBuffer(Screen *screen):
screen_(screen),
initialCursor_(screen_->cursor())
{
buffer_.push_back(L"I-search:");
}
void IsearchBuffer::postInsert(Coord &cursor, std::wstring value)
{
assert(screen_->textBuffer());
std::transform(begin(value), end(value), begin(value), ::tolower);
searchString_ += value;
search();
}
int IsearchBuffer::preBackspace(Coord &cursor, int value)
{
if (searchString_.size() > 0)
return value;
else
return 0;
}
void IsearchBuffer::postBackspace(Coord &cursor, int value)
{
if (searchString_.size() > 0)
{
searchString_.resize(searchString_.size() - value);
screen_->setCursor(initialCursor_);
search();
}
}
void IsearchBuffer::findNext()
{
if (searchString_.empty())
return;
screen_->moveCursorRight();
if (!search())
{
screen_->setCursor(initialCursor_);
search();
}
}
bool IsearchBuffer::search()
{
size_t x = std::max(0, static_cast<int>(screen_->cursor().x - searchString_.size()));
int y = screen_->cursor().y;
while (y < screen_->textBuffer()->size())
{
auto tmpStr = (*screen_->textBuffer())[y];
std::transform(begin(tmpStr), end(tmpStr), begin(tmpStr), ::tolower);
auto tmp = tmpStr.find(searchString_, x);
if (tmp != std::wstring::npos)
{
screen_->setCursor(tmp + searchString_.size(), y);
screen_->setStartSelection(Coord{ static_cast<int>(tmp), y });
screen_->setEndSelection(Coord{ static_cast<int>(tmp + searchString_.size()), y });
return true;
}
++y;
x = 0;
}
return false;
}