2006-01-16 22:02:54 +01:00
|
|
|
// Copyright (c) 2005, Rodrigo Braz Monteiro
|
|
|
|
// All rights reserved.
|
|
|
|
//
|
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are met:
|
|
|
|
//
|
|
|
|
// * Redistributions of source code must retain the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer.
|
|
|
|
// * Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer in the documentation
|
|
|
|
// and/or other materials provided with the distribution.
|
|
|
|
// * Neither the name of the Aegisub Group nor the names of its contributors
|
|
|
|
// may be used to endorse or promote products derived from this software
|
|
|
|
// without specific prior written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
// POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
//
|
2009-07-29 07:43:02 +02:00
|
|
|
// Aegisub Project http://www.aegisub.org/
|
|
|
|
|
|
|
|
/// @file dialog_search_replace.cpp
|
|
|
|
/// @brief Find and Search/replace dialogue box and logic
|
|
|
|
/// @ingroup secondary_ui
|
|
|
|
///
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2009-01-04 07:31:48 +01:00
|
|
|
#include "config.h"
|
|
|
|
|
2013-01-11 05:45:59 +01:00
|
|
|
#include "dialog_search_replace.h"
|
2009-09-10 15:06:40 +02:00
|
|
|
|
2006-01-16 22:02:54 +01:00
|
|
|
#include "ass_dialogue.h"
|
2009-09-10 15:06:40 +02:00
|
|
|
#include "ass_file.h"
|
2011-07-15 06:25:23 +02:00
|
|
|
#include "compat.h"
|
2011-01-16 08:17:36 +01:00
|
|
|
#include "include/aegisub/context.h"
|
2013-01-07 02:50:09 +01:00
|
|
|
#include "options.h"
|
2010-12-08 04:36:10 +01:00
|
|
|
#include "selection_controller.h"
|
2012-10-08 16:32:51 +02:00
|
|
|
#include "text_selection_controller.h"
|
2009-09-10 15:06:40 +02:00
|
|
|
#include "subs_grid.h"
|
2013-01-11 05:45:59 +01:00
|
|
|
#include "utils.h"
|
2006-03-14 01:39:33 +01:00
|
|
|
|
2012-11-05 17:20:58 +01:00
|
|
|
#include <libaegisub/of_type_adaptor.h>
|
|
|
|
|
2013-01-11 05:45:59 +01:00
|
|
|
#include <functional>
|
|
|
|
|
|
|
|
#include <wx/button.h>
|
2013-01-11 06:22:10 +01:00
|
|
|
#include <wx/checkbox.h>
|
|
|
|
#include <wx/combobox.h>
|
2013-01-11 05:45:59 +01:00
|
|
|
#include <wx/msgdlg.h>
|
2013-01-11 06:22:10 +01:00
|
|
|
#include <wx/radiobox.h>
|
2013-01-11 05:45:59 +01:00
|
|
|
#include <wx/regex.h>
|
|
|
|
#include <wx/sizer.h>
|
|
|
|
#include <wx/stattext.h>
|
2013-01-11 06:22:10 +01:00
|
|
|
#include <wx/textctrl.h>
|
2013-01-11 05:45:59 +01:00
|
|
|
|
2011-01-16 08:17:36 +01:00
|
|
|
enum {
|
|
|
|
BUTTON_FIND_NEXT,
|
|
|
|
BUTTON_REPLACE_NEXT,
|
2013-01-11 06:22:10 +01:00
|
|
|
BUTTON_REPLACE_ALL
|
2011-01-16 08:17:36 +01:00
|
|
|
};
|
Note: This was done using a script! it's far from perfect but 95% of the work has been done already formatting-wise.
Document all functions, class, struct, union, enum, macro, variable, typedefs. This isn't the actual document in itself but empty documentation using any old documentation if it was there.
This was done using exuberant ctags to get tag info, then a TCL script to parse/remove old comments and convert them into Doxygen-style.
Some notes:
* Anything labeled 'DOCME' needs to be documented, @param and @return have been left blank as it would be annoying to delete the 'DOCME' from every one of those.
* Some multiline comments may have been munged into single line comments
* Leave the /// comments above global variables with a space, if they're harder to read then we'll be less likey to use them.
* Enum comments can go after the enumeration itself '[value] /// comment'
* include/aegisub/*.h haven't been converted yet, this will be done in a later commit
* Some documentation blocks are in the wrong place, in the .h when it should be in the .cpp, or vice versa.
See http://devel.aegisub.org/wiki/Doxygen for some details on Doxygen and a 'style guide'.
Originally committed to SVN as r3312.
2009-07-30 00:59:22 +02:00
|
|
|
|
2013-01-11 17:18:10 +01:00
|
|
|
enum {
|
|
|
|
FIELD_TEXT = 0,
|
|
|
|
FIELD_STYLE,
|
|
|
|
FIELD_ACTOR,
|
|
|
|
FIELD_EFFECT
|
|
|
|
};
|
|
|
|
|
|
|
|
enum {
|
|
|
|
LIMIT_ALL = 0,
|
|
|
|
LIMIT_SELECTED
|
|
|
|
};
|
|
|
|
|
2011-12-22 22:14:42 +01:00
|
|
|
DialogSearchReplace::DialogSearchReplace(agi::Context* c, bool withReplace)
|
|
|
|
: wxDialog(c->parent, -1, withReplace ? _("Replace") : _("Find"))
|
2013-01-11 17:53:36 +01:00
|
|
|
, c(c)
|
2011-12-24 01:47:41 +01:00
|
|
|
, hasReplace(withReplace)
|
2006-01-16 22:02:54 +01:00
|
|
|
{
|
2013-01-11 06:22:10 +01:00
|
|
|
wxSizer *FindSizer = new wxFlexGridSizer(2, 2, 5, 15);
|
|
|
|
FindEdit = new wxComboBox(this, -1, "", wxDefaultPosition, wxSize(300, -1), lagi_MRU_wxAS("Find"), wxCB_DROPDOWN);
|
2012-05-18 07:03:05 +02:00
|
|
|
if (!FindEdit->IsListEmpty())
|
|
|
|
FindEdit->SetSelection(0);
|
2013-01-11 06:22:10 +01:00
|
|
|
FindSizer->Add(new wxStaticText(this, -1, _("Find what:")), 0, wxALIGN_CENTER_VERTICAL);
|
|
|
|
FindSizer->Add(FindEdit);
|
2006-01-16 22:02:54 +01:00
|
|
|
if (hasReplace) {
|
2013-01-11 06:22:10 +01:00
|
|
|
ReplaceEdit = new wxComboBox(this, -1, "", wxDefaultPosition, wxSize(300, -1), lagi_MRU_wxAS("Replace"), wxCB_DROPDOWN);
|
|
|
|
FindSizer->Add(new wxStaticText(this, -1, _("Replace with:")), 0, wxALIGN_CENTER_VERTICAL);
|
|
|
|
FindSizer->Add(ReplaceEdit);
|
2012-05-18 07:03:05 +02:00
|
|
|
if (!ReplaceEdit->IsListEmpty())
|
|
|
|
ReplaceEdit->SetSelection(0);
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
wxSizer *OptionsSizer = new wxBoxSizer(wxVERTICAL);
|
2013-01-11 06:22:10 +01:00
|
|
|
CheckMatchCase = new wxCheckBox(this, -1, _("&Match case"));
|
|
|
|
CheckRegExp = new wxCheckBox(this, -1, _("&Use regular expressions"));
|
2010-05-21 03:13:36 +02:00
|
|
|
CheckMatchCase->SetValue(OPT_GET("Tool/Search Replace/Match Case")->GetBool());
|
|
|
|
CheckRegExp->SetValue(OPT_GET("Tool/Search Replace/RegExp")->GetBool());
|
2013-01-11 06:22:10 +01:00
|
|
|
OptionsSizer->Add(CheckMatchCase, wxSizerFlags().Border(wxBOTTOM));
|
|
|
|
OptionsSizer->Add(CheckRegExp);
|
|
|
|
|
|
|
|
// Left sizer
|
|
|
|
wxSizer *LeftSizer = new wxBoxSizer(wxVERTICAL);
|
|
|
|
LeftSizer->Add(FindSizer, wxSizerFlags().DoubleBorder(wxBOTTOM));
|
|
|
|
LeftSizer->Add(OptionsSizer);
|
2006-01-16 22:02:54 +01:00
|
|
|
|
|
|
|
// Limits sizer
|
2013-01-11 05:45:59 +01:00
|
|
|
wxString field[] = { _("Text"), _("Style"), _("Actor"), _("Effect") };
|
|
|
|
wxString affect[] = { _("All rows"), _("Selected rows") };
|
2013-01-11 06:22:10 +01:00
|
|
|
Field = new wxRadioBox(this, -1, _("In Field"), wxDefaultPosition, wxDefaultSize, countof(field), field);
|
|
|
|
Affect = new wxRadioBox(this, -1, _("Limit to"), wxDefaultPosition, wxDefaultSize, countof(affect), affect);
|
2006-01-16 22:02:54 +01:00
|
|
|
wxSizer *LimitSizer = new wxBoxSizer(wxHORIZONTAL);
|
2013-01-11 06:22:10 +01:00
|
|
|
LimitSizer->Add(Field, wxSizerFlags().Border(wxRIGHT));
|
|
|
|
LimitSizer->Add(Affect);
|
2010-05-21 03:13:36 +02:00
|
|
|
Field->SetSelection(OPT_GET("Tool/Search Replace/Field")->GetInt());
|
|
|
|
Affect->SetSelection(OPT_GET("Tool/Search Replace/Affect")->GetInt());
|
2006-01-16 22:02:54 +01:00
|
|
|
|
|
|
|
// Buttons
|
|
|
|
wxSizer *ButtonSizer = new wxBoxSizer(wxVERTICAL);
|
2013-01-11 06:22:10 +01:00
|
|
|
wxButton *FindNext = new wxButton(this, BUTTON_FIND_NEXT, _("&Find next"));
|
2006-01-16 22:02:54 +01:00
|
|
|
FindNext->SetDefault();
|
2013-01-11 06:22:10 +01:00
|
|
|
ButtonSizer->Add(FindNext, wxSizerFlags().Border(wxBOTTOM));
|
2006-01-16 22:02:54 +01:00
|
|
|
if (hasReplace) {
|
2013-01-11 06:22:10 +01:00
|
|
|
ButtonSizer->Add(new wxButton(this, BUTTON_REPLACE_NEXT, _("Replace &next")), wxSizerFlags().Border(wxBOTTOM));
|
|
|
|
ButtonSizer->Add(new wxButton(this, BUTTON_REPLACE_ALL, _("Replace &all")), wxSizerFlags().Border(wxBOTTOM));
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
2013-01-11 06:22:10 +01:00
|
|
|
ButtonSizer->Add(new wxButton(this, wxID_CANCEL));
|
|
|
|
|
|
|
|
wxSizer *TopSizer = new wxBoxSizer(wxHORIZONTAL);
|
|
|
|
TopSizer->Add(LeftSizer, wxSizerFlags().Border());
|
|
|
|
TopSizer->Add(ButtonSizer, wxSizerFlags().Border());
|
2006-01-16 22:02:54 +01:00
|
|
|
|
|
|
|
// Main sizer
|
2013-01-11 06:22:10 +01:00
|
|
|
wxSizer *MainSizer = new wxBoxSizer(wxVERTICAL);
|
|
|
|
MainSizer->Add(TopSizer);
|
|
|
|
MainSizer->Add(LimitSizer, wxSizerFlags().Border());
|
2011-12-22 22:14:32 +01:00
|
|
|
SetSizerAndFit(MainSizer);
|
2006-01-16 22:02:54 +01:00
|
|
|
CenterOnParent();
|
2006-12-30 21:58:24 +01:00
|
|
|
|
2013-01-11 17:53:36 +01:00
|
|
|
c->search->OnDialogOpen();
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2012-09-25 01:35:27 +02:00
|
|
|
Bind(wxEVT_COMMAND_BUTTON_CLICKED, std::bind(&DialogSearchReplace::FindReplace, this, 0), BUTTON_FIND_NEXT);
|
|
|
|
Bind(wxEVT_COMMAND_BUTTON_CLICKED, std::bind(&DialogSearchReplace::FindReplace, this, 1), BUTTON_REPLACE_NEXT);
|
|
|
|
Bind(wxEVT_COMMAND_BUTTON_CLICKED, std::bind(&DialogSearchReplace::FindReplace, this, 2), BUTTON_REPLACE_ALL);
|
2011-12-22 22:14:32 +01:00
|
|
|
}
|
Note: This was done using a script! it's far from perfect but 95% of the work has been done already formatting-wise.
Document all functions, class, struct, union, enum, macro, variable, typedefs. This isn't the actual document in itself but empty documentation using any old documentation if it was there.
This was done using exuberant ctags to get tag info, then a TCL script to parse/remove old comments and convert them into Doxygen-style.
Some notes:
* Anything labeled 'DOCME' needs to be documented, @param and @return have been left blank as it would be annoying to delete the 'DOCME' from every one of those.
* Some multiline comments may have been munged into single line comments
* Leave the /// comments above global variables with a space, if they're harder to read then we'll be less likey to use them.
* Enum comments can go after the enumeration itself '[value] /// comment'
* include/aegisub/*.h haven't been converted yet, this will be done in a later commit
* Some documentation blocks are in the wrong place, in the .h when it should be in the .cpp, or vice versa.
See http://devel.aegisub.org/wiki/Doxygen for some details on Doxygen and a 'style guide'.
Originally committed to SVN as r3312.
2009-07-30 00:59:22 +02:00
|
|
|
|
2006-01-16 22:02:54 +01:00
|
|
|
DialogSearchReplace::~DialogSearchReplace() {
|
2013-01-11 17:53:36 +01:00
|
|
|
c->search->isReg = CheckRegExp->IsChecked() && CheckRegExp->IsEnabled();
|
|
|
|
c->search->matchCase = CheckMatchCase->IsChecked();
|
2010-05-21 03:13:36 +02:00
|
|
|
OPT_SET("Tool/Search Replace/Match Case")->SetBool(CheckMatchCase->IsChecked());
|
|
|
|
OPT_SET("Tool/Search Replace/RegExp")->SetBool(CheckRegExp->IsChecked());
|
|
|
|
OPT_SET("Tool/Search Replace/Field")->SetInt(Field->GetSelection());
|
|
|
|
OPT_SET("Tool/Search Replace/Affect")->SetInt(Affect->GetSelection());
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
|
2007-03-28 03:11:52 +02:00
|
|
|
void DialogSearchReplace::FindReplace(int mode) {
|
|
|
|
if (mode < 0 || mode > 2) return;
|
|
|
|
|
2006-01-16 22:02:54 +01:00
|
|
|
wxString LookFor = FindEdit->GetValue();
|
2011-12-22 22:14:32 +01:00
|
|
|
if (!LookFor) return;
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2013-01-11 17:53:36 +01:00
|
|
|
c->search->isReg = CheckRegExp->IsChecked() && CheckRegExp->IsEnabled();
|
|
|
|
c->search->matchCase = CheckMatchCase->IsChecked();
|
|
|
|
c->search->LookFor = LookFor;
|
|
|
|
c->search->initialized = true;
|
|
|
|
c->search->affect = Affect->GetSelection();
|
|
|
|
c->search->field = Field->GetSelection();
|
2007-03-28 03:11:52 +02:00
|
|
|
|
2013-01-11 06:31:34 +01:00
|
|
|
if (hasReplace) {
|
2006-03-05 21:42:38 +01:00
|
|
|
wxString ReplaceWith = ReplaceEdit->GetValue();
|
2013-01-11 17:53:36 +01:00
|
|
|
c->search->ReplaceWith = ReplaceWith;
|
2012-12-23 00:18:38 +01:00
|
|
|
config::mru->Add("Replace", from_wx(ReplaceWith));
|
2007-03-28 03:11:52 +02:00
|
|
|
}
|
2012-03-25 06:05:06 +02:00
|
|
|
|
2013-01-11 06:31:34 +01:00
|
|
|
if (mode == 0)
|
2013-01-11 17:53:36 +01:00
|
|
|
c->search->FindNext();
|
2013-01-11 06:31:34 +01:00
|
|
|
else if (mode == 1)
|
2013-01-11 17:53:36 +01:00
|
|
|
c->search->ReplaceNext();
|
2013-01-11 06:31:34 +01:00
|
|
|
else
|
2013-01-11 17:53:36 +01:00
|
|
|
c->search->ReplaceAll();
|
2013-01-11 06:31:34 +01:00
|
|
|
|
2012-12-23 00:18:38 +01:00
|
|
|
config::mru->Add("Find", from_wx(LookFor));
|
2006-01-16 22:02:54 +01:00
|
|
|
UpdateDropDowns();
|
|
|
|
}
|
|
|
|
|
2013-01-11 06:31:34 +01:00
|
|
|
static void update_mru(wxComboBox *cb, const char *mru_name) {
|
|
|
|
cb->Freeze();
|
|
|
|
cb->Clear();
|
|
|
|
cb->Append(lagi_MRU_wxAS(mru_name));
|
|
|
|
if (!cb->IsListEmpty())
|
|
|
|
cb->SetSelection(0);
|
|
|
|
cb->Thaw();
|
|
|
|
}
|
|
|
|
|
2006-01-16 22:02:54 +01:00
|
|
|
void DialogSearchReplace::UpdateDropDowns() {
|
2013-01-11 06:31:34 +01:00
|
|
|
update_mru(FindEdit, "Find");
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2013-01-11 06:31:34 +01:00
|
|
|
if (hasReplace)
|
|
|
|
update_mru(ReplaceEdit, "Replace");
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
|
2013-01-11 17:53:36 +01:00
|
|
|
SearchReplaceEngine::SearchReplaceEngine(agi::Context *c)
|
|
|
|
: context(c)
|
|
|
|
, curLine(0)
|
2012-10-15 06:37:14 +02:00
|
|
|
, pos(0)
|
|
|
|
, matchLen(0)
|
|
|
|
, replaceLen(0)
|
|
|
|
, LastWasFind(true)
|
|
|
|
, hasReplace(false)
|
|
|
|
, isReg(false)
|
|
|
|
, matchCase(false)
|
2013-01-11 06:33:49 +01:00
|
|
|
, initialized(false)
|
2013-01-11 17:18:10 +01:00
|
|
|
, field(FIELD_TEXT)
|
|
|
|
, affect(LIMIT_ALL)
|
2012-10-15 06:37:14 +02:00
|
|
|
{
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
|
2012-12-04 23:35:59 +01:00
|
|
|
static boost::flyweight<wxString> *get_text(AssDialogue *cur, int field) {
|
2013-01-11 17:18:10 +01:00
|
|
|
switch (field) {
|
|
|
|
case FIELD_TEXT: return &cur->Text;
|
|
|
|
case FIELD_STYLE: return &cur->Style;
|
|
|
|
case FIELD_ACTOR: return &cur->Actor;
|
|
|
|
case FIELD_EFFECT: return &cur->Effect;
|
|
|
|
default: throw agi::InternalError("Bad find/replace field", 0);
|
|
|
|
}
|
2012-12-04 23:35:59 +01:00
|
|
|
}
|
2006-01-16 22:02:54 +01:00
|
|
|
|
|
|
|
void SearchReplaceEngine::ReplaceNext(bool DoReplace) {
|
2013-01-11 06:33:49 +01:00
|
|
|
if (!initialized) {
|
2006-01-16 22:02:54 +01:00
|
|
|
OpenDialog(DoReplace);
|
|
|
|
return;
|
|
|
|
}
|
2012-03-25 06:05:06 +02:00
|
|
|
|
2011-01-16 08:17:36 +01:00
|
|
|
wxArrayInt sels = context->subsGrid->GetSelection();
|
2008-02-15 22:47:09 +01:00
|
|
|
int firstLine = 0;
|
|
|
|
if (sels.Count() > 0) firstLine = sels[0];
|
2006-03-07 23:38:46 +01:00
|
|
|
// if selection has changed reset values
|
2010-10-15 18:58:50 +02:00
|
|
|
if (firstLine != curLine) {
|
2008-02-15 22:47:09 +01:00
|
|
|
curLine = firstLine;
|
2006-03-07 23:38:46 +01:00
|
|
|
LastWasFind = true;
|
|
|
|
pos = 0;
|
|
|
|
matchLen = 0;
|
|
|
|
replaceLen = 0;
|
|
|
|
}
|
2006-01-16 22:02:54 +01:00
|
|
|
|
|
|
|
// Setup
|
|
|
|
int start = curLine;
|
2011-01-16 08:17:36 +01:00
|
|
|
int nrows = context->subsGrid->GetRows();
|
2006-01-16 22:02:54 +01:00
|
|
|
bool found = false;
|
|
|
|
int regFlags = wxRE_ADVANCED;
|
|
|
|
if (!matchCase) {
|
2013-01-11 06:31:34 +01:00
|
|
|
if (isReg)
|
|
|
|
regFlags |= wxRE_ICASE;
|
|
|
|
else
|
|
|
|
LookFor.MakeLower();
|
|
|
|
}
|
|
|
|
wxRegEx regex;
|
|
|
|
if (isReg) {
|
|
|
|
regex.Compile(LookFor, regFlags);
|
|
|
|
|
|
|
|
if (!regex.IsValid()) {
|
|
|
|
LastWasFind = !DoReplace;
|
|
|
|
return;
|
|
|
|
}
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Search for it
|
2012-12-04 23:35:59 +01:00
|
|
|
boost::flyweight<wxString> *Text = nullptr;
|
2006-01-16 22:02:54 +01:00
|
|
|
while (!found) {
|
2012-12-04 23:35:59 +01:00
|
|
|
Text = get_text(context->subsGrid->GetDialogue(curLine), field);
|
2013-01-11 17:55:35 +01:00
|
|
|
size_t tempPos;
|
2011-12-22 22:19:12 +01:00
|
|
|
if (DoReplace && LastWasFind)
|
|
|
|
tempPos = pos;
|
|
|
|
else
|
2013-01-11 17:55:35 +01:00
|
|
|
tempPos = pos + replaceLen;
|
2006-01-16 22:02:54 +01:00
|
|
|
|
|
|
|
if (isReg) {
|
2013-01-11 17:55:35 +01:00
|
|
|
if (regex.Matches(Text->get().substr(tempPos))) {
|
2013-01-11 06:31:34 +01:00
|
|
|
size_t match_start;
|
2013-01-11 17:55:35 +01:00
|
|
|
regex.GetMatch(&match_start, &matchLen, 0);
|
2013-01-11 06:31:34 +01:00
|
|
|
pos = match_start + tempPos;
|
|
|
|
found = true;
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2013-01-11 17:55:35 +01:00
|
|
|
wxString src = Text->get().substr(tempPos);
|
2006-01-16 22:02:54 +01:00
|
|
|
if (!matchCase) src.MakeLower();
|
2013-01-11 17:55:35 +01:00
|
|
|
size_t textPos = src.find(LookFor);
|
|
|
|
if (textPos != src.npos) {
|
2011-12-26 23:20:49 +01:00
|
|
|
pos = tempPos+textPos;
|
2006-01-16 22:02:54 +01:00
|
|
|
found = true;
|
2013-01-11 17:55:35 +01:00
|
|
|
matchLen = LookFor.size();
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Didn't find, go to next line
|
|
|
|
if (!found) {
|
2013-01-11 06:31:34 +01:00
|
|
|
curLine = (curLine + 1) % nrows;
|
2006-01-16 22:02:54 +01:00
|
|
|
pos = 0;
|
|
|
|
matchLen = 0;
|
|
|
|
replaceLen = 0;
|
|
|
|
if (curLine == start) break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (found) {
|
2013-01-11 05:45:59 +01:00
|
|
|
if (!DoReplace)
|
|
|
|
replaceLen = matchLen;
|
|
|
|
else {
|
2006-01-16 22:02:54 +01:00
|
|
|
if (isReg) {
|
2013-01-11 17:55:35 +01:00
|
|
|
wxString toReplace = Text->get().substr(pos,matchLen);
|
2006-01-16 22:02:54 +01:00
|
|
|
regex.ReplaceFirst(&toReplace,ReplaceWith);
|
2013-01-11 17:55:35 +01:00
|
|
|
*Text = Text->get().Left(pos) + toReplace + Text->get().substr(pos+matchLen);
|
|
|
|
replaceLen = toReplace.size();
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
else {
|
2013-01-11 17:55:35 +01:00
|
|
|
*Text = Text->get().Left(pos) + ReplaceWith + Text->get().substr(pos+matchLen);
|
|
|
|
replaceLen = ReplaceWith.size();
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
|
2011-09-15 07:16:32 +02:00
|
|
|
context->ass->Commit(_("replace"), AssFile::COMMIT_DIAG_TEXT);
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
|
2011-01-16 08:17:36 +01:00
|
|
|
context->subsGrid->SelectRow(curLine,false);
|
|
|
|
context->subsGrid->MakeCellVisible(curLine,0);
|
2013-01-11 17:18:10 +01:00
|
|
|
if (field == FIELD_TEXT) {
|
2012-10-08 16:32:51 +02:00
|
|
|
context->selectionController->SetActiveLine(context->subsGrid->GetDialogue(curLine));
|
|
|
|
context->textSelectionController->SetSelection(pos, pos + replaceLen);
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
// hAx to prevent double match on style/actor
|
2013-01-11 17:18:10 +01:00
|
|
|
else
|
|
|
|
replaceLen = 99999;
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
LastWasFind = !DoReplace;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SearchReplaceEngine::ReplaceAll() {
|
|
|
|
size_t count = 0;
|
2011-12-22 22:19:12 +01:00
|
|
|
|
2006-01-16 22:02:54 +01:00
|
|
|
int regFlags = wxRE_ADVANCED;
|
2011-12-22 22:19:12 +01:00
|
|
|
if (!matchCase)
|
|
|
|
regFlags |= wxRE_ICASE;
|
|
|
|
wxRegEx reg;
|
|
|
|
if (isReg)
|
|
|
|
reg.Compile(LookFor, regFlags);
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2012-10-05 05:22:54 +02:00
|
|
|
SubtitleSelection const& sel = context->selectionController->GetSelectedSet();
|
2011-12-22 22:19:12 +01:00
|
|
|
bool hasSelection = !sel.empty();
|
2013-01-11 17:18:10 +01:00
|
|
|
bool inSel = affect == LIMIT_SELECTED;
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2012-11-05 17:20:58 +01:00
|
|
|
for (auto diag : context->ass->Line | agi::of_type<AssDialogue>()) {
|
2011-12-22 22:19:12 +01:00
|
|
|
if (inSel && hasSelection && !sel.count(diag))
|
2006-01-16 22:02:54 +01:00
|
|
|
continue;
|
|
|
|
|
2012-12-04 23:35:59 +01:00
|
|
|
boost::flyweight<wxString> *Text = get_text(diag, field);
|
2006-01-16 22:02:54 +01:00
|
|
|
|
|
|
|
if (isReg) {
|
2011-12-22 22:19:12 +01:00
|
|
|
if (reg.Matches(*Text)) {
|
|
|
|
size_t start, len;
|
|
|
|
reg.GetMatch(&start, &len);
|
|
|
|
|
|
|
|
// A zero length match (such as '$') will always be replaced
|
|
|
|
// maxMatches times, which is almost certainly not what the user
|
|
|
|
// wanted, so limit it to one replacement in that situation
|
2012-12-04 23:35:59 +01:00
|
|
|
wxString repl(*Text);
|
|
|
|
count += reg.Replace(&repl, ReplaceWith, len > 0 ? 1000 : 1);
|
|
|
|
*Text = repl;
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2013-01-11 17:53:36 +01:00
|
|
|
if (!matchCase) {
|
2011-12-22 22:19:12 +01:00
|
|
|
bool replaced = false;
|
|
|
|
wxString Left, Right = *Text;
|
|
|
|
size_t pos = 0;
|
|
|
|
Left.reserve(Right.size());
|
|
|
|
while (pos + LookFor.size() <= Right.size()) {
|
2013-01-11 17:55:35 +01:00
|
|
|
if (Right.substr(pos, LookFor.size()).CmpNoCase(LookFor) == 0) {
|
2011-12-22 22:19:12 +01:00
|
|
|
Left.Append(Right.Left(pos)).Append(ReplaceWith);
|
2013-01-11 17:55:35 +01:00
|
|
|
Right = Right.substr(pos + LookFor.size());
|
2011-12-22 22:19:12 +01:00
|
|
|
++count;
|
2008-06-15 19:56:05 +02:00
|
|
|
replaced = true;
|
|
|
|
pos = 0;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
pos++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (replaced) {
|
2008-07-23 18:09:27 +02:00
|
|
|
*Text = Left + Right;
|
2008-03-07 06:34:01 +01:00
|
|
|
}
|
|
|
|
}
|
2012-12-04 23:35:59 +01:00
|
|
|
else if(Text->get().Contains(LookFor)) {
|
|
|
|
wxString repl(*Text);
|
|
|
|
count += repl.Replace(LookFor, ReplaceWith);
|
|
|
|
*Text = repl;
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count > 0) {
|
2011-09-15 07:16:32 +02:00
|
|
|
context->ass->Commit(_("replace"), AssFile::COMMIT_DIAG_TEXT);
|
2012-04-27 21:08:09 +02:00
|
|
|
wxMessageBox(wxString::Format(_("%i matches were replaced."), (int)count));
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
wxMessageBox(_("No matches found."));
|
|
|
|
}
|
|
|
|
LastWasFind = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SearchReplaceEngine::OnDialogOpen() {
|
2011-01-16 08:17:36 +01:00
|
|
|
wxArrayInt sels = context->subsGrid->GetSelection();
|
2006-01-16 22:02:54 +01:00
|
|
|
curLine = 0;
|
|
|
|
if (sels.Count() > 0) curLine = sels[0];
|
|
|
|
|
|
|
|
LastWasFind = true;
|
|
|
|
pos = 0;
|
|
|
|
matchLen = 0;
|
|
|
|
replaceLen = 0;
|
|
|
|
}
|
|
|
|
|
2013-01-11 05:45:59 +01:00
|
|
|
void SearchReplaceEngine::OpenDialog(bool replace) {
|
2012-11-13 17:51:01 +01:00
|
|
|
static DialogSearchReplace *diag = nullptr;
|
2006-03-05 21:42:38 +01:00
|
|
|
|
2013-01-11 06:31:34 +01:00
|
|
|
if (diag && replace != hasReplace) {
|
|
|
|
// Already opened, but wrong type - destroy and create the right one
|
2006-03-05 21:42:38 +01:00
|
|
|
diag->Destroy();
|
2013-01-11 06:31:34 +01:00
|
|
|
diag = nullptr;
|
2006-03-05 21:42:38 +01:00
|
|
|
}
|
2013-01-11 06:31:34 +01:00
|
|
|
|
|
|
|
if (!diag)
|
|
|
|
diag = new DialogSearchReplace(context, replace);
|
|
|
|
|
2006-12-29 23:58:09 +01:00
|
|
|
diag->FindEdit->SetFocus();
|
2006-03-05 21:42:38 +01:00
|
|
|
diag->Show();
|
|
|
|
hasReplace = replace;
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|