Commit 59b414df authored by twanvl's avatar twanvl

find/replace working better (but not done yet)

parent fcc72286
...@@ -271,7 +271,7 @@ void CardsPanel::doPaste() { CUT_COPY_PASTE(doPaste, ;) } ...@@ -271,7 +271,7 @@ void CardsPanel::doPaste() { CUT_COPY_PASTE(doPaste, ;) }
class CardsPanel::SearchFindInfo : public FindInfo { class CardsPanel::SearchFindInfo : public FindInfo {
public: public:
SearchFindInfo(CardsPanel& panel, wxFindReplaceData& what) : FindInfo(what), panel(panel) {} SearchFindInfo(CardsPanel& panel, wxFindReplaceData& what) : FindInfo(what), panel(panel) {}
virtual bool handle(const CardP& card, const TextValueP& value, size_t pos) { virtual bool handle(const CardP& card, const TextValueP& value, size_t pos, bool was_selection) {
// Select the card // Select the card
panel.card_list->setCard(card); panel.card_list->setCard(card);
return true; return true;
...@@ -283,13 +283,18 @@ class CardsPanel::SearchFindInfo : public FindInfo { ...@@ -283,13 +283,18 @@ class CardsPanel::SearchFindInfo : public FindInfo {
class CardsPanel::ReplaceFindInfo : public FindInfo { class CardsPanel::ReplaceFindInfo : public FindInfo {
public: public:
ReplaceFindInfo(CardsPanel& panel, wxFindReplaceData& what) : FindInfo(what), panel(panel) {} ReplaceFindInfo(CardsPanel& panel, wxFindReplaceData& what) : FindInfo(what), panel(panel) {}
virtual bool handle(const CardP& card, const TextValueP& value, size_t pos) { virtual bool handle(const CardP& card, const TextValueP& value, size_t pos, bool was_selection) {
// Select the card // Select the card
panel.card_list->setCard(card); panel.card_list->setCard(card);
// Replace // Replace
panel.editor->insert(escape(what.GetReplaceString()), _("Replace")); if (was_selection) {
return true; panel.editor->insert(escape(what.GetReplaceString()), _("Replace"));
return false;
} else {
return true;
}
} }
virtual bool searchSelection() const { return true; }
private: private:
CardsPanel& panel; CardsPanel& panel;
}; };
......
...@@ -677,14 +677,17 @@ bool TextValueEditor::matchSubstr(const String& s, size_t pos, FindInfo& find) { ...@@ -677,14 +677,17 @@ bool TextValueEditor::matchSubstr(const String& s, size_t pos, FindInfo& find) {
if (!is_substr(s, pos, find.findString().Lower())) return false; if (!is_substr(s, pos, find.findString().Lower())) return false;
} }
// handle // handle
bool was_selection = false;
if (find.select()) { if (find.select()) {
editor().select(this); editor().select(this);
editor().SetFocus(); editor().SetFocus();
size_t old_sel_start = selection_start, old_sel_end = selection_end;
selection_start_i = untagged_to_index(value().value(), pos, true); selection_start_i = untagged_to_index(value().value(), pos, true);
selection_end_i = untagged_to_index(value().value(), pos + find.findString().size(), true); selection_end_i = untagged_to_index(value().value(), pos + find.findString().size(), true);
fixSelection(TYPE_INDEX); fixSelection(TYPE_INDEX);
was_selection = old_sel_start == selection_start && old_sel_end == selection_end;
} }
if (find.handle(viewer.getCard(), valueP(), pos)) { if (find.handle(viewer.getCard(), valueP(), pos, was_selection)) {
return true; return true;
} else { } else {
// TODO: string might have changed when doing replace all // TODO: string might have changed when doing replace all
...@@ -695,15 +698,17 @@ bool TextValueEditor::matchSubstr(const String& s, size_t pos, FindInfo& find) { ...@@ -695,15 +698,17 @@ bool TextValueEditor::matchSubstr(const String& s, size_t pos, FindInfo& find) {
bool TextValueEditor::search(FindInfo& find, bool from_start) { bool TextValueEditor::search(FindInfo& find, bool from_start) {
String v = untag(value().value()); String v = untag(value().value());
if (!find.caseSensitive()) v.LowerCase(); if (!find.caseSensitive()) v.LowerCase();
size_t selection_min = index_to_untagged(value().value(), min(selection_start_i, selection_end_i));
size_t selection_max = index_to_untagged(value().value(), max(selection_start_i, selection_end_i));
if (find.forward()) { if (find.forward()) {
size_t start = min(v.size(), max(selection_start, selection_end)); size_t start = min(v.size(), find.searchSelection() ? selection_min : selection_max);
size_t end = max(0, (int)v.size() - (int)find.findString().size()); size_t end = max(0, (int)v.size() - (int)find.findString().size());
for (size_t i = start ; i <= end ; ++i) { for (size_t i = start ; i <= end ; ++i) {
if (matchSubstr(v, i, find)) return true; if (matchSubstr(v, i, find)) return true;
} }
} else { } else {
size_t start = 0; size_t start = 0;
int end = (int)min(selection_start, selection_end) - (int)find.findString().size(); int end = (int)(find.searchSelection() ? selection_max : selection_min) - (int)find.findString().size();
if (end < 0) return false; if (end < 0) return false;
for (size_t i = end ; i >= start ; --i) { for (size_t i = end ; i >= start ; --i) {
if (matchSubstr(v, i, find)) return true; if (matchSubstr(v, i, find)) return true;
......
...@@ -329,6 +329,21 @@ size_t untagged_to_index(const String& str, size_t pos, bool inside) { ...@@ -329,6 +329,21 @@ size_t untagged_to_index(const String& str, size_t pos, bool inside) {
return i; return i;
} }
size_t index_to_untagged(const String& str, size_t index) {
size_t i = 0, p = 0;
index = min(str.size(), index);
while (i < index) {
Char c = str.GetChar(i);
if (c == _('<')) {
i = skip_tag(str, i);
} else {
i++;
p++;
}
}
return p;
}
// ----------------------------------------------------------------------------- : Global operations // ----------------------------------------------------------------------------- : Global operations
String remove_tag(const String& str, const String& tag) { String remove_tag(const String& str, const String& tag) {
......
...@@ -119,6 +119,11 @@ size_t cursor_to_index(const String& str, size_t cursor, Movement dir = MOVE_MID ...@@ -119,6 +119,11 @@ size_t cursor_to_index(const String& str, size_t cursor, Movement dir = MOVE_MID
*/ */
size_t untagged_to_index(const String& str, size_t pos, bool inside); size_t untagged_to_index(const String& str, size_t pos, bool inside);
/// Find the untagged position corresponding to the given tagged position.
/** An untagged position in str is a position in untag(str).
*/
size_t index_to_untagged(const String& str, size_t index);
// ----------------------------------------------------------------------------- : Global operations // ----------------------------------------------------------------------------- : Global operations
/// Remove all instances of a tag and its close tag, but keep the contents. /// Remove all instances of a tag and its close tag, but keep the contents.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment