Add "Shift+F3" for backward search - thx OmrSi :)

Fix #3041
This commit is contained in:
Nikolaj Olsson 2018-08-21 18:47:28 +02:00
parent 17a69466d1
commit 00a2a24391
2 changed files with 190 additions and 2 deletions

View File

@ -4768,7 +4768,8 @@ namespace Nikse.SubtitleEdit.Forms
selectedIndex = SubtitleListview1.SelectedItems[0].Index;
int textBoxStart = tb.SelectionStart;
if (_findHelper.SelectedPosition - 1 == tb.SelectionStart && tb.SelectionLength > 0)
if (_findHelper.SelectedPosition - 1 == tb.SelectionStart && tb.SelectionLength > 0 ||
_findHelper.FindText == tb.SelectedText)
{
textBoxStart = tb.SelectionStart + 1;
}
@ -4828,6 +4829,59 @@ namespace Nikse.SubtitleEdit.Forms
_findHelper.InProgress = false;
}
private void FindPrevious()
{
if (_findHelper == null)
{
return;
}
_findHelper.InProgress = true;
TextBox tb = GetFindRepaceTextBox();
if (tabControlSubtitle.SelectedIndex == TabControlListView)
{
int selectedIndex = -1;
if (SubtitleListview1.SelectedItems.Count > 0)
selectedIndex = SubtitleListview1.SelectedItems[0].Index;
int textBoxStart = tb.SelectionStart;
if (_findHelper.SelectedPosition - 1 == tb.SelectionStart && tb.SelectionLength > 0)
{
textBoxStart = tb.SelectionStart - 1;
}
if (_findHelper.FindPrevious(_subtitle, _subtitleAlternate, selectedIndex, textBoxStart, Configuration.Settings.General.AllowEditOfOriginalSubtitle))
{
tb = GetFindRepaceTextBox();
SubtitleListview1.SelectIndexAndEnsureVisible(_findHelper.SelectedIndex, true);
ShowStatus(string.Format(_language.XFoundAtLineNumberY, _findHelper.FindText, _findHelper.SelectedIndex + 1));
tb.Focus();
tb.SelectionStart = _findHelper.SelectedPosition;
tb.SelectionLength = _findHelper.FindTextLength;
_findHelper.SelectedPosition--;
}
else
{
ShowStatus(string.Format(_language.XNotFound, _findHelper.FindText));
}
}
else if (tabControlSubtitle.SelectedIndex == TabControlSourceView)
{
if (_findHelper.FindPrevious(textBoxSource.Text, textBoxSource.SelectionStart))
{
textBoxSource.SelectionStart = _findHelper.SelectedIndex;
textBoxSource.SelectionLength = _findHelper.FindTextLength;
textBoxSource.ScrollToCaret();
ShowStatus(string.Format(_language.XFoundAtLineNumberY, _findHelper.FindText, textBoxSource.GetLineFromCharIndex(textBoxSource.SelectionStart)));
}
else
{
ShowStatus(string.Format(_language.XNotFound, _findHelper.FindText));
}
}
_findHelper.InProgress = false;
}
private void ToolStripButtonReplaceClick(object sender, EventArgs e)
{
ReloadFromSourceView();
@ -12792,6 +12846,11 @@ namespace Nikse.SubtitleEdit.Forms
}
e.SuppressKeyPress = true;
}
else if (e.KeyCode == Keys.F3 && e.Modifiers == Keys.Shift)
{
FindPrevious();
e.SuppressKeyPress = true;
}
// TABS - MUST BE LAST
else if (tabControlButtons.SelectedTab == tabPageAdjust)

View File

@ -142,7 +142,7 @@ namespace Nikse.SubtitleEdit.Logic
}
position = 0;
}
if (index < subtitle.Paragraphs.Count -1)
if (index < subtitle.Paragraphs.Count - 1)
MatchInOriginal = false;
if (originalSubtitle != null && allowEditOfOriginalSubtitle)
@ -168,6 +168,74 @@ namespace Nikse.SubtitleEdit.Logic
return false;
}
public bool FindPrevious(Subtitle subtitle, Subtitle originalSubtitle, int startIndex, int position, bool allowEditOfOriginalSubtitle)
{
Success = false;
int index = startIndex;
if (position < 0)
position = 0;
bool first = true;
for (var i = startIndex; i >= 0; i--)
{
Paragraph p = subtitle.Paragraphs[i];
if (originalSubtitle != null && allowEditOfOriginalSubtitle)
{
if (!first || MatchInOriginal)
{
Paragraph o = Utilities.GetOriginalParagraph(index, p, originalSubtitle.Paragraphs);
if (o != null)
{
if (!first)
position = o.Text.Length - 1;
for (var j = 0; j <= position; j++)
{
var t = o.Text.Substring(position - j, j);
int pos = FindPositionInText(t, 0);
if (pos >= 0)
{
pos += position - j;
MatchInOriginal = true;
SelectedIndex = index;
SelectedPosition = pos;
Success = true;
return true;
}
}
}
position = p.Text.Length - 1;
}
}
MatchInOriginal = false;
if (!first)
position = p.Text.Length - 1;
for (var j = 0; j <= position; j++)
{
var t = p.Text.Substring(position - j, j + 1);
int pos = FindPositionInText(t, 0);
if (pos >= 0)
{
pos += position - j;
MatchInOriginal = false;
SelectedIndex = index;
SelectedPosition = pos;
Success = true;
return true;
}
}
position = 0;
first = false;
index--;
}
return false;
}
public static ContextMenu GetRegExContextMenu(TextBox textBox)
{
var cm = new ContextMenu();
@ -250,6 +318,67 @@ namespace Nikse.SubtitleEdit.Logic
return false;
}
public bool FindPrevious(string text, int startIndex)
{
Success = false;
startIndex--;
if (startIndex < text.Length)
{
if (FindReplaceType.FindType == FindType.RegEx)
{
var matches = _regEx.Matches(text.Substring(0, startIndex));
if (matches.Count > 0)
{
string groupName = RegexUtils.GetRegExGroup(_findText);
var last = matches[matches.Count - 1];
if (groupName != null && last.Groups[groupName] != null && last.Groups[groupName].Success)
{
_findTextLength = last.Groups[groupName].Length;
SelectedIndex = last.Groups[groupName].Index;
}
else
{
_findTextLength = last.Length;
SelectedIndex = last.Index;
}
Success = true;
}
return Success;
}
string searchText = text.Substring(0, startIndex);
int pos = -1;
var comparison = GetComparison();
var idx = searchText.LastIndexOf(_findText, startIndex, comparison);
while (idx >= 0)
{
if (FindReplaceType.WholeWord)
{
var startOk = idx == 0 || SeparatorChars.Contains(searchText[idx - 1]);
var endOk = idx + _findText.Length == searchText.Length || SeparatorChars.Contains(searchText[idx + _findText.Length]);
if (startOk && endOk)
{
pos = idx;
break;
}
}
else
{
pos = idx;
break;
}
searchText = text.Substring(0, idx);
idx = searchText.LastIndexOf(_findText, comparison);
}
if (pos >= 0)
{
SelectedIndex = pos;
return true;
}
}
return false;
}
public int FindCount(Subtitle subtitle, bool wholeWord)
{
var count = 0;