From 979244a163f5f46d508e5a56458e6c67af1a47c7 Mon Sep 17 00:00:00 2001
From: MaxED <j.maxed@gmail.com>
Date: Wed, 10 Dec 2014 14:22:54 +0000
Subject: [PATCH] A couple of fixes to ScriptDocumentTab.FindPrevious() logic.

---
 Source/Core/Controls/ScriptDocumentTab.cs | 17 +++++++++--------
 Source/Core/Controls/ScriptEditorPanel.cs |  2 +-
 2 files changed, 10 insertions(+), 9 deletions(-)

diff --git a/Source/Core/Controls/ScriptDocumentTab.cs b/Source/Core/Controls/ScriptDocumentTab.cs
index 6bc727c28..d88b8b5fa 100644
--- a/Source/Core/Controls/ScriptDocumentTab.cs
+++ b/Source/Core/Controls/ScriptDocumentTab.cs
@@ -300,17 +300,20 @@ namespace CodeImp.DoomBuilder.Controls
 		// Find previous result (mxd)
 		public bool FindPrevious(FindReplaceOptions options) 
 		{
+			bool wrapped = false;
 			byte[] data = editor.GetText();
 			string text = Encoding.GetEncoding(config.CodePage).GetString(data);
 			StringComparison mode = options.CaseSensitive ? StringComparison.CurrentCulture : StringComparison.CurrentCultureIgnoreCase;
-			int endpos = Math.Min(editor.SelectionStart, editor.SelectionEnd);
-			int initialendpos = endpos;
-			int searchlength = endpos;
-			bool wrapped = false;
+			int endpos = Math.Min(editor.SelectionStart, editor.SelectionEnd) - 1;
+			if(endpos < 0)
+			{
+				endpos = text.Length - 1;
+				wrapped = true;
+			}
 
 			while(true) 
 			{
-				int result = text.LastIndexOf(options.FindText, endpos, searchlength, mode);
+				int result = text.LastIndexOf(options.FindText, endpos, mode);
 				if(result > -1) 
 				{
 					// Check to see if it is the whole word
@@ -321,7 +324,6 @@ namespace CodeImp.DoomBuilder.Controls
 						if(foundword.Length != options.FindText.Length) 
 						{
 							endpos = result - 1;
-							searchlength = endpos;
 							result = -1;
 						}
 					}
@@ -341,8 +343,7 @@ namespace CodeImp.DoomBuilder.Controls
 					// If we haven't tried from the end, try from the end now
 					if(!wrapped) 
 					{
-						endpos = text.Length - 1;
-						searchlength = endpos - initialendpos;
+						endpos = Math.Max(0, text.Length - 2);
 						wrapped = true;
 					} 
 					else 
diff --git a/Source/Core/Controls/ScriptEditorPanel.cs b/Source/Core/Controls/ScriptEditorPanel.cs
index 90aa4857d..62463c676 100644
--- a/Source/Core/Controls/ScriptEditorPanel.cs
+++ b/Source/Core/Controls/ScriptEditorPanel.cs
@@ -906,7 +906,7 @@ namespace CodeImp.DoomBuilder.Controls
 
 		private void searchbox_TextChanged(object sender, EventArgs e)
 		{
-			bool success = ActiveTab.FindNext(GetQuickSearchOptions(), true);
+			bool success = (searchbox.Text.Length > 0 && ActiveTab.FindNext(GetQuickSearchOptions(), true));
 			searchbox.BackColor = ((success || searchbox.Text.Length == 0) ? SystemColors.Window : Color.MistyRose);
 			searchnext.Enabled = success;
 			searchprev.Enabled = success;
-- 
GitLab