More work on Find/Replace

This commit is contained in:
niksedk 2023-04-16 20:46:02 +02:00
parent 5c824f5855
commit 5fb8a93418
9 changed files with 116 additions and 50 deletions

View File

@ -47,7 +47,12 @@ namespace Nikse.SubtitleEdit.Forms
{
get
{
var result = new ReplaceType();
var result = new ReplaceType
{
SearchOriginal = true,
SearchTranslation = true
};
if (radioButtonNormal.Checked)
{
result.FindType = FindType.Normal;
@ -136,12 +141,12 @@ namespace Nikse.SubtitleEdit.Forms
else if (radioButtonNormal.Checked)
{
DialogResult = DialogResult.OK;
_findAndReplaceMethods.FindDialogFind(FindText);
_findAndReplaceMethods.FindDialogFind(FindText, FindReplaceType);
}
else if (radioButtonCaseSensitive.Checked)
{
DialogResult = DialogResult.OK;
_findAndReplaceMethods.FindDialogFind(FindText);
_findAndReplaceMethods.FindDialogFind(FindText, FindReplaceType);
}
else if (radioButtonRegEx.Checked)
{
@ -149,7 +154,7 @@ namespace Nikse.SubtitleEdit.Forms
{
_regEx = new Regex(RegexUtils.FixNewLine(searchText), RegexOptions.Compiled, TimeSpan.FromSeconds(5));
DialogResult = DialogResult.OK;
_findAndReplaceMethods.FindDialogFind(FindText);
_findAndReplaceMethods.FindDialogFind(FindText, FindReplaceType);
}
catch (Exception exception)
{

View File

@ -6246,6 +6246,8 @@ namespace Nikse.SubtitleEdit.Forms
if (_findHelper != null)
{
_findHelper.InProgress = false;
_findHelper.MatchInOriginal = false;
_findHelper.SelectedPosition = -1;
}
Focus();
@ -6259,11 +6261,16 @@ namespace Nikse.SubtitleEdit.Forms
FindPrevious();
}
public void FindDialogFind(string findText)
public void FindDialogFind(string findText, ReplaceType findReplaceType)
{
_findHelper = _findHelper ?? _findDialog.GetFindDialogHelper(_subtitleListViewIndex);
_findHelper.FindText = findText;
_findHelper.FindTextLength = findText.Length;
_findHelper.FindReplaceType = findReplaceType;
if (findReplaceType.FindType == FindType.RegEx)
{
}
DialogFind(_findHelper);
}
@ -6471,8 +6478,24 @@ namespace Nikse.SubtitleEdit.Forms
}
}
public void ReplaceDialogFind()
public void ReplaceDialogFind(FindReplaceDialogHelper findReplaceDialogHelper)
{
_findHelper = findReplaceDialogHelper;
if (_findHelper != null)
{
if (_findHelper.SelectedLineIndex != _subtitleListViewIndex)
{
_findHelper.SelectedLineIndex = _subtitleListViewIndex;
_findHelper.SelectedPosition = -1;
_findHelper.MatchInOriginal = false;
_findHelper.ReplaceFromPosition = 0;
}
DialogFind(_findHelper);
return;
}
DialogFind(_replaceDialog.GetFindDialogHelper(_subtitleListViewIndex));
}
@ -6507,6 +6530,11 @@ namespace Nikse.SubtitleEdit.Forms
if (found)
{
SelectListViewIndexAndEnsureVisible(_findHelper.SelectedLineIndex);
textBoxListViewText.SelectionStart = 0;
textBoxListViewText.SelectionLength = 0;
textBoxListViewTextOriginal.SelectionStart = 0;
textBoxListViewTextOriginal.SelectionLength = 0;
tb.SelectionLength = 0;
tb.Focus();
tb.SelectionStart = _findHelper.SelectedPosition;
tb.SelectionLength = _findHelper.FindTextLength;
@ -6534,7 +6562,7 @@ namespace Nikse.SubtitleEdit.Forms
}
}
public void ReplaceDialogReplace()
public void ReplaceDialogReplace(FindReplaceDialogHelper findReplaceDialogHelper)
{
if (InListView)
{
@ -6589,6 +6617,10 @@ namespace Nikse.SubtitleEdit.Forms
if (_findHelper.FindNext(_subtitle, _subtitleOriginal, _findHelper.SelectedLineIndex, _findHelper.SelectedPosition, Configuration.Settings.General.AllowEditOfOriginalSubtitle))
{
SelectListViewIndexAndEnsureVisible(_findHelper.SelectedLineIndex);
textBoxListViewText.SelectionStart = 0;
textBoxListViewText.SelectionLength = 0;
textBoxListViewTextOriginal.SelectionStart = 0;
textBoxListViewTextOriginal.SelectionLength = 0;
tb = GetFindReplaceTextBox();
tb.Focus();
tb.SelectionStart = _findHelper.SelectedPosition;
@ -6702,7 +6734,7 @@ namespace Nikse.SubtitleEdit.Forms
}
}
public void ReplaceDialogReplaceAll()
public void ReplaceDialogReplaceAll(FindReplaceDialogHelper findReplaceDialogHelper)
{
_findHelper = _replaceDialog.GetFindDialogHelper(_subtitleListViewIndex);
ShowStatus(string.Format(_language.SearchingForXFromLineY, _findHelper.FindText, _subtitleListViewIndex + 1));
@ -6724,6 +6756,13 @@ namespace Nikse.SubtitleEdit.Forms
RestartHistory();
}
if (_findHelper != null)
{
_findHelper.InProgress = false;
_findHelper.MatchInOriginal = false;
_findHelper.SelectedPosition = -1;
}
Focus();
}
@ -16191,7 +16230,7 @@ namespace Nikse.SubtitleEdit.Forms
e.SuppressKeyPress = true;
}
else if (_shortcuts.MainTextBoxSelectionToggleCasing == e.KeyData)
else if (_shortcuts.MainTextBoxSelectionToggleCasing == e.KeyData)
{
e.SuppressKeyPress = true;
if (textBoxListViewText.Focused || textBoxListViewTextOriginal.Focused)

View File

@ -26,6 +26,7 @@ using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
using Nikse.SubtitleEdit.Core.Enums;
namespace Nikse.SubtitleEdit.Forms.Ocr
{
@ -9783,11 +9784,12 @@ namespace Nikse.SubtitleEdit.Forms.Ocr
Configuration.Settings.Tools.OcrGoogleCloudVisionSeHandlesTextMerge = checkBoxSeHandlesTextMerge.Checked;
}
public void FindDialogFind(string findText)
public void FindDialogFind(string findText, ReplaceType findReplaceType)
{
_findHelper = _findHelper ?? _findDialog.GetFindDialogHelper(_selectedIndex);
_findHelper.FindText = findText;
_findHelper.FindTextLength = findText.Length;
_findHelper.FindReplaceType = findReplaceType;
_findHelper.InProgress = true;
if (!string.IsNullOrWhiteSpace(_findHelper.FindText))
{
@ -9875,17 +9877,17 @@ namespace Nikse.SubtitleEdit.Forms.Ocr
_findHelper.InProgress = false;
}
public void ReplaceDialogFind()
public void ReplaceDialogFind(FindReplaceDialogHelper findReplaceDialogHelper)
{
throw new NotImplementedException();
}
public void ReplaceDialogReplace()
public void ReplaceDialogReplace(FindReplaceDialogHelper findReplaceDialogHelper)
{
throw new NotImplementedException();
}
public void ReplaceDialogReplaceAll()
public void ReplaceDialogReplaceAll(FindReplaceDialogHelper findReplaceDialogHelper)
{
throw new NotImplementedException();
}

View File

@ -50,7 +50,7 @@
this.radioButtonRegEx.Location = new System.Drawing.Point(15, 235);
this.radioButtonRegEx.Name = "radioButtonRegEx";
this.radioButtonRegEx.Size = new System.Drawing.Size(56, 17);
this.radioButtonRegEx.TabIndex = 5;
this.radioButtonRegEx.TabIndex = 70;
this.radioButtonRegEx.Text = "RegEx";
this.radioButtonRegEx.UseVisualStyleBackColor = true;
this.radioButtonRegEx.CheckedChanged += new System.EventHandler(this.RadioButtonCheckedChanged);
@ -62,7 +62,7 @@
this.radioButtonCaseSensitive.Location = new System.Drawing.Point(15, 212);
this.radioButtonCaseSensitive.Name = "radioButtonCaseSensitive";
this.radioButtonCaseSensitive.Size = new System.Drawing.Size(94, 17);
this.radioButtonCaseSensitive.TabIndex = 4;
this.radioButtonCaseSensitive.TabIndex = 60;
this.radioButtonCaseSensitive.Text = "Case sensitive";
this.radioButtonCaseSensitive.UseVisualStyleBackColor = true;
this.radioButtonCaseSensitive.CheckedChanged += new System.EventHandler(this.RadioButtonCheckedChanged);
@ -75,7 +75,7 @@
this.radioButtonNormal.Location = new System.Drawing.Point(15, 189);
this.radioButtonNormal.Name = "radioButtonNormal";
this.radioButtonNormal.Size = new System.Drawing.Size(58, 17);
this.radioButtonNormal.TabIndex = 3;
this.radioButtonNormal.TabIndex = 50;
this.radioButtonNormal.TabStop = true;
this.radioButtonNormal.Text = "Normal";
this.radioButtonNormal.UseVisualStyleBackColor = true;
@ -87,7 +87,7 @@
this.buttonReplace.Location = new System.Drawing.Point(266, 54);
this.buttonReplace.Name = "buttonReplace";
this.buttonReplace.Size = new System.Drawing.Size(119, 23);
this.buttonReplace.TabIndex = 7;
this.buttonReplace.TabIndex = 88;
this.buttonReplace.Text = "Replace";
this.buttonReplace.UseVisualStyleBackColor = true;
this.buttonReplace.Click += new System.EventHandler(this.ButtonReplaceClick);
@ -107,7 +107,7 @@
this.buttonReplaceAll.Location = new System.Drawing.Point(266, 83);
this.buttonReplaceAll.Name = "buttonReplaceAll";
this.buttonReplaceAll.Size = new System.Drawing.Size(119, 23);
this.buttonReplaceAll.TabIndex = 8;
this.buttonReplaceAll.TabIndex = 92;
this.buttonReplaceAll.Text = "Replace all";
this.buttonReplaceAll.UseVisualStyleBackColor = true;
this.buttonReplaceAll.Click += new System.EventHandler(this.ButtonReplaceAllClick);
@ -117,7 +117,7 @@
this.textBoxReplace.Location = new System.Drawing.Point(15, 71);
this.textBoxReplace.Name = "textBoxReplace";
this.textBoxReplace.Size = new System.Drawing.Size(232, 21);
this.textBoxReplace.TabIndex = 1;
this.textBoxReplace.TabIndex = 10;
this.textBoxReplace.KeyDown += new System.Windows.Forms.KeyEventHandler(this.TextBoxFindKeyDown);
//
// labelReplaceWith
@ -144,7 +144,7 @@
this.buttonFind.Location = new System.Drawing.Point(266, 25);
this.buttonFind.Name = "buttonFind";
this.buttonFind.Size = new System.Drawing.Size(119, 23);
this.buttonFind.TabIndex = 6;
this.buttonFind.TabIndex = 80;
this.buttonFind.Text = "Find";
this.buttonFind.UseVisualStyleBackColor = true;
this.buttonFind.Click += new System.EventHandler(this.ButtonFindClick);
@ -156,7 +156,7 @@
this.checkBoxWholeWord.Location = new System.Drawing.Point(15, 159);
this.checkBoxWholeWord.Name = "checkBoxWholeWord";
this.checkBoxWholeWord.Size = new System.Drawing.Size(83, 17);
this.checkBoxWholeWord.TabIndex = 2;
this.checkBoxWholeWord.TabIndex = 30;
this.checkBoxWholeWord.Text = "Whole word";
this.checkBoxWholeWord.UseVisualStyleBackColor = true;
//
@ -167,7 +167,7 @@
this.comboBoxFindReplaceIn.Location = new System.Drawing.Point(15, 120);
this.comboBoxFindReplaceIn.Name = "comboBoxFindReplaceIn";
this.comboBoxFindReplaceIn.Size = new System.Drawing.Size(232, 21);
this.comboBoxFindReplaceIn.TabIndex = 15;
this.comboBoxFindReplaceIn.TabIndex = 20;
//
// labelFindReplaceIn
//

View File

@ -45,9 +45,9 @@ namespace Nikse.SubtitleEdit.Forms
_findNextShortcut = UiUtil.GetKeys(Configuration.Settings.Shortcuts.MainEditFindNext);
comboBoxFindReplaceIn.Items.Clear();
comboBoxFindReplaceIn.Items.Add("Translation and original");
comboBoxFindReplaceIn.Items.Add("Translation only");
comboBoxFindReplaceIn.Items.Add("Original only");
comboBoxFindReplaceIn.Items.Add(LanguageSettings.Current.ReplaceDialog.TranslationAndOriginal);
comboBoxFindReplaceIn.Items.Add(LanguageSettings.Current.ReplaceDialog.TranslationOnly);
comboBoxFindReplaceIn.Items.Add(LanguageSettings.Current.ReplaceDialog.OriginalOnly);
comboBoxFindReplaceIn.SelectedIndex = 0;
}
@ -79,6 +79,7 @@ namespace Nikse.SubtitleEdit.Forms
comboBoxFindReplaceIn.SelectedIndex == 1;
result.WholeWord = checkBoxWholeWord.Checked;
return result;
}
@ -150,8 +151,8 @@ namespace Nikse.SubtitleEdit.Forms
Validate(textBoxFind.Text);
if (DialogResult == DialogResult.OK)
{
var findType = GetFindType();
_findAndReplaceMethods.ReplaceDialogReplace();
UpdateFindHelper();
_findAndReplaceMethods.ReplaceDialogReplace(_findHelper);
}
buttonReplace.Focus();
@ -165,8 +166,8 @@ namespace Nikse.SubtitleEdit.Forms
Validate(textBoxFind.Text);
if (DialogResult == DialogResult.OK)
{
var findType = GetFindType();
_findAndReplaceMethods.ReplaceDialogReplaceAll();
UpdateFindHelper();
_findAndReplaceMethods.ReplaceDialogReplaceAll(_findHelper);
}
buttonReplaceAll.Focus();
@ -216,27 +217,36 @@ namespace Nikse.SubtitleEdit.Forms
_findHelper.ReplaceFromPosition++;
}
UpdateFindHelper();
ReplaceAll = false;
FindOnly = true;
Validate(textBoxFind.Text);
if (DialogResult == DialogResult.OK)
{
var findType = GetFindType();
_findAndReplaceMethods.ReplaceDialogFind();
_findAndReplaceMethods.ReplaceDialogFind(_findHelper);
}
}
private void UpdateFindHelper()
{
if (_findHelper == null)
{
return;
}
_findHelper.FindReplaceType = GetFindType();
_findHelper.FindText = textBoxFind.Text;
_findHelper.FindTextLength = textBoxFind.Text.Length;
}
private void RadioButtonCheckedChanged(object sender, EventArgs e)
{
if (sender == radioButtonRegEx)
{
textBoxFind.ContextMenuStrip = FindReplaceDialogHelper.GetRegExContextMenu(textBoxFind);
}
else
{
textBoxFind.ContextMenuStrip = null;
}
textBoxFind.ContextMenuStrip = sender == radioButtonRegEx
? FindReplaceDialogHelper.GetRegExContextMenu(textBoxFind)
: null;
checkBoxWholeWord.Enabled = !radioButtonRegEx.Checked;
}

View File

@ -89,6 +89,7 @@ namespace Nikse.SubtitleEdit.Logic
Match match;
try
{
_regEx = new Regex(FindText, RegexOptions.None, TimeSpan.FromSeconds(5));
match = _regEx.Match(text, startIndex);
}
catch (RegexMatchTimeoutException exception)
@ -131,6 +132,7 @@ namespace Nikse.SubtitleEdit.Logic
if (!first)
{
position = 0;
MatchInOriginal = false;
}
int pos;
@ -147,11 +149,11 @@ namespace Nikse.SubtitleEdit.Logic
return true;
}
position = 0;
}
if (index < subtitle.Paragraphs.Count - 1)
{
MatchInOriginal = false;
if (index < subtitle.Paragraphs.Count - 1)
{
MatchInOriginal = false;
}
}
if (originalSubtitle != null && allowEditOfOriginalSubtitle && FindReplaceType.SearchOriginal)
@ -171,8 +173,10 @@ namespace Nikse.SubtitleEdit.Logic
}
}
}
first = false;
}
index++;
}
@ -182,8 +186,6 @@ namespace Nikse.SubtitleEdit.Logic
public bool FindPrevious(Subtitle subtitle, Subtitle originalSubtitle, int startIndex, int position, bool allowEditOfOriginalSubtitle)
{
//TODO: us whole word
Success = false;
var index = startIndex;
var first = true;

View File

@ -1,14 +1,16 @@
namespace Nikse.SubtitleEdit.Logic
using Nikse.SubtitleEdit.Core.Enums;
namespace Nikse.SubtitleEdit.Logic
{
public interface IFindAndReplace
{
void FindDialogFind(string findText);
void FindDialogFind(string findText, ReplaceType findReplaceType);
void FindDialogFindPrevious(string findText);
void FindDialogClose();
void ReplaceDialogFind();
void ReplaceDialogReplace();
void ReplaceDialogReplaceAll();
void ReplaceDialogFind(FindReplaceDialogHelper findReplaceDialogHelper);
void ReplaceDialogReplace(FindReplaceDialogHelper findReplaceDialogHelper);
void ReplaceDialogReplaceAll(FindReplaceDialogHelper findReplaceDialogHelper);
void ReplaceDialogClose();
}
}

View File

@ -2425,6 +2425,9 @@ can edit in same subtitle file (collaboration)",
Find = "&Find",
Replace = "&Replace",
ReplaceAll = "Replace &all",
TranslationAndOriginal = "Translation and original",
TranslationOnly = "Translation only",
OriginalOnly = "Original only",
};
RestoreAutoBackup = new LanguageStructure.RestoreAutoBackup

View File

@ -2272,6 +2272,9 @@ namespace Nikse.SubtitleEdit.Logic
public string Find { get; set; }
public string Replace { get; set; }
public string ReplaceAll { get; set; }
public string TranslationAndOriginal { get; set; }
public string TranslationOnly { get; set; }
public string OriginalOnly { get; set; }
}
public class RestoreAutoBackup