void FindNext(bool forward, bool selecting) { if (Searches.Count == 0) { SetSelections(new List <Range>()); return; } var sels = new List <Range>(); foreach (var selection in Selections) { int index; if (forward) { index = Searches.BinaryFindFirst(range => range.Start >= selection.End); if (index == -1) { index = 0; } } else { index = Searches.BinaryFindLast(range => range.Start < selection.Start); if (index == -1) { index = Searches.Count - 1; } } if (!selecting) { sels.Add(new Range(Searches[index].End, Searches[index].Start)); } else if (forward) { sels.Add(new Range(Searches[index].End, selection.Start)); } else { sels.Add(new Range(Searches[index].Start, selection.End)); } } SetSelections(sels); }