private bool FindNext(FindReplaceViewModel f,
                              bool invertLeftRight = false)
        {
            IEditor ce = f.GetCurrentEditor();

            if (ce == null)
            {
                return(false);
            }

            Regex r;
            Match m = FindNextMatchInText(ce.SelectionStart, ce.SelectionLength,
                                          invertLeftRight, ce.Text, ref f, out r);

            if (m.Success)
            {
                ce.Select(m.Index, m.Length);

                return(true);
            }
            else
            {
                if (f.SearchIn == Dialogs.FindReplace.SearchScope.CurrentDocument)
                {
                    _MsgBox.Show(Util.Local.Strings.STR_MSG_FIND_NO_MORE_ITEMS_FOUND);

                    return(false);
                }

                // we have reached the end of the document
                // start again from the beginning/end,
                object oldEditor = f.CurrentEditor;
                do
                {
                    if (f.SearchIn == Dialogs.FindReplace.SearchScope.AllDocuments)
                    {
                        ce = GetNextEditor(f, r.Options.HasFlag(RegexOptions.RightToLeft));

                        if (ce == null)
                        {
                            return(false);
                        }

                        f.CurrentEditor = ce;

                        return(true);
                    }

                    m = r.Options.HasFlag(RegexOptions.RightToLeft) ? r.Match(ce.Text, ce.Text.Length - 1) : r.Match(ce.Text, 0);

                    if (m.Success)
                    {
                        ce.Select(m.Index, m.Length);
                        break;
                    }
                    else
                    {
                        _MsgBox.Show(Util.Local.Strings.STR_MSG_FIND_NO_MORE_ITEMS_FOUND2,
                                     Util.Local.Strings.STR_MSG_FIND_Caption);
                    }
                } while (f.CurrentEditor != oldEditor);
            }

            return(false);
        }
        private IEditor GetNextEditor(FindReplaceViewModel f,
                                      bool previous = false
                                      )
        {
            // There is no next open document if there is none or only one open
            if (this.Files.Count <= 1)
            {
                return(f.GetCurrentEditor());
            }

            // There is no next open document If the user wants to search the current document only
            if (f.SearchIn == Edi.Dialogs.FindReplace.SearchScope.CurrentDocument)
            {
                return(f.GetCurrentEditor());
            }

            var l = new List <object>(this.Files.Cast <object>());

            int idxStart = l.IndexOf(f.CurrentEditor);
            int i        = idxStart;

            if (i >= 0)
            {
                Match m = null;

                bool textSearchSuccess = false;
                do
                {
                    if (previous == true)                  // Get next/previous document
                    {
                        i = (i < 1 ? l.Count - 1 : i - 1);
                    }
                    else
                    {
                        i = (i >= l.Count - 1 ? 0 : i + 1);
                    }

                    //// i = (i + (previous ? l.Count - 1 : +1)) % l.Count;

                    // Search text in document
                    if (l[i] is EdiViewModel)
                    {
                        EdiViewModel fTmp = l[i] as EdiViewModel;

                        Regex r;
                        m = this.FindNextMatchInText(0, 0, false, fTmp.Text, ref f, out r);

                        textSearchSuccess = m.Success;
                    }
                }while (i != idxStart && textSearchSuccess != true);

                // Found a match so activate the corresponding document and select the text with scroll into view
                if (textSearchSuccess == true && m != null)
                {
                    var doc = l[i] as EdiViewModel;

                    if (doc != null)
                    {
                        this.ActiveDocument = doc;
                    }

                    // Ensure that no pending calls are in the dispatcher queue
                    // This makes sure that we are blocked until bindings are re-established
                    // Bindings are required to scroll a selection into view
                    Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.SystemIdle, (Action) delegate
                    {
                        if (this.ActiveDocument != null && doc != null)
                        {
                            doc.TextEditorSelectionStart  = m.Index;
                            doc.TextEditorSelectionLength = m.Length;

                            // Reset cursor position to make sure we search a document from its beginning
                            doc.TxtControl.SelectText(m.Index, m.Length);

                            f.CurrentEditor = l[i] as IEditor;

                            IEditor edi = f.GetCurrentEditor();

                            if (edi != null)
                            {
                                edi.Select(m.Index, m.Length);
                            }
                        }
                    });

                    return(f.GetCurrentEditor());
                }
            }

            return(null);
        }
Example #3
0
        private bool FindNext(FindReplaceViewModel f,
                              bool InvertLeftRight = false)
        {
            IEditor CE = f.GetCurrentEditor();

            if (CE == null)
            {
                return(false);
            }

            Regex r;
            Match m = this.FindNextMatchInText(CE.SelectionStart, CE.SelectionLength,
                                               InvertLeftRight, CE.Text, ref f, out r);

            if (m.Success)
            {
                CE.Select(m.Index, m.Length);

                return(true);
            }
            else
            {
                if (f.SearchIn == EdiViews.FindReplace.SearchScope.CurrentDocument)
                {
                    Edi.Msg.Box.Show("There are no more search results to display.");

                    return(false);
                }

                // we have reached the end of the document
                // start again from the beginning/end,
                object OldEditor = f.CurrentEditor;
                do
                {
                    if (f.SearchIn == EdiViews.FindReplace.SearchScope.AllDocuments)
                    {
                        CE = this.GetNextEditor(f, r.Options.HasFlag(RegexOptions.RightToLeft));

                        if (CE == null)
                        {
                            return(false);
                        }

                        f.CurrentEditor = CE;

                        return(true);
                    }

                    if (r.Options.HasFlag(RegexOptions.RightToLeft))
                    {
                        m = r.Match(CE.Text, CE.Text.Length - 1);
                    }
                    else
                    {
                        m = r.Match(CE.Text, 0);
                    }

                    if (m.Success)
                    {
                        CE.Select(m.Index, m.Length);
                        break;
                    }
                    else
                    {
                        Edi.Msg.Box.Show("No further occurence found.", "Search");
                    }
                } while (f.CurrentEditor != OldEditor);
            }

            return(false);
        }