private TextRange[] FindAll(SearchExecutedEventArgs e)
        {
            string query = e.Query;

            if (query == null)
            {
                return(null);
            }

            if (!e.Options.HasFlag(SearchOptions.Regex))
            {
                query = Regex.Escape(query);
            }

            if (e.Options.HasFlag(SearchOptions.WholeWord))
            {
                query = "\\b" + query + "\\b";
            }

            try
            {
                return(Document.FindAll(query, !e.Options.HasFlag(SearchOptions.MatchCase) ? RegexOptions.IgnoreCase : RegexOptions.None, out _cachedStack, _cachedStack));
            }
            catch (ArgumentException)
            {
                // Invalid Regex.
                query = Regex.Escape(query);
                return(Document.FindAll(query, !e.Options.HasFlag(SearchOptions.MatchCase) ? RegexOptions.IgnoreCase : RegexOptions.None, out _cachedStack, _cachedStack));
            }
        }
        //private void searchUI_SearchExecuted(object sender, SearchExecutedEventArgs e)
        //{
        //	// Try first to find a match starting at the current selection position.
        //	string all = new TextRange(Selection.Start, Document.ContentEnd).Text;
        //	string query = e.Query;

        //	if (query == null)
        //		return;

        //	if (!e.Options.HasFlag(SearchOptions.Regex))
        //		query = Regex.Escape(query);

        //	if (e.Options.HasFlag(SearchOptions.WholeWord))
        //		query = "\\b" + query + "\\b";

        //	Match match = Regex.Match(all, query, !e.Options.HasFlag(SearchOptions.MatchCase) ? RegexOptions.IgnoreCase : RegexOptions.None);

        //	// If we couldn't find a match, re-run the search from the beginning of the document,
        //	// and include the entire document.
        //	if (!match.Success)
        //	{
        //		all = new TextRange(Document.ContentStart, Document.ContentEnd).Text;
        //		match = Regex.Match(all, query, !e.Options.HasFlag(SearchOptions.MatchCase) ? RegexOptions.IgnoreCase : RegexOptions.None);
        //	}

        //	if (match.Success)
        //	{
        //		Selection.Select(Document.ContentStart.GetPositionAtOffset(match.Index + 2),
        //			Document.ContentStart.GetPositionAtOffset(match.Index + match.Length + 2));
        //		Focus();
        //	}
        //	else
        //	{
        //		TaskDialog dlg = new TaskDialog(Window.GetWindow(this), "Find", "The query you requested was not found.", MessageType.Error);
        //		dlg.ShowDialog();
        //	}
        //}

        private void searchUI_SearchExecuted(object sender, SearchExecutedEventArgs e)
        {
            TextRange match = Find(e);

            if (match != null)
            {
                Selection.Select(match.Start, match.End);
                this.ScrollToSelection();
                Focus();
            }
            else
            {
                ShowNotFoundMessage(e.Query);
            }
        }
        private TextRange Find(SearchExecutedEventArgs e)
        {
            string query = e.Query;

            if (query == null)
            {
                return(null);
            }

            if (!e.Options.HasFlag(SearchOptions.Regex))
            {
                query = Regex.Escape(query);
            }

            if (e.Options.HasFlag(SearchOptions.WholeWord))
            {
                query = "\\b" + query + "\\b";
            }

            try
            {
                return(Document.Find(query, Selection.End, !e.Options.HasFlag(SearchOptions.MatchCase) ? RegexOptions.IgnoreCase : RegexOptions.None, out _cachedStack, _cachedStack));
            }
            catch (ArgumentException)
            {
                // Invalid Regex.
                query = Regex.Escape(query);
                return(Document.Find(query, Selection.End, !e.Options.HasFlag(SearchOptions.MatchCase) ? RegexOptions.IgnoreCase : RegexOptions.None, out _cachedStack, _cachedStack));
            }

            //TextRange match = FindQueryFromPosition(Selection.End, query, !e.Options.HasFlag(SearchOptions.MatchCase) ? RegexOptions.IgnoreCase : RegexOptions.None);

            //// If we couldn't find a match, re-run the search from the beginning of the document,
            //// and include the entire document.
            //if (match == null)
            //	match = FindQueryFromPosition(Document.ContentStart, query, !e.Options.HasFlag(SearchOptions.MatchCase) ? RegexOptions.IgnoreCase : RegexOptions.None);

            //return match;
        }