private void Reemplazar(NoteBuffer b, string s, string r) { /* * Changes every occurrences of string s for r in buffer b. * Why three passess? Because TextIter left invalidated every time I modify * content of a note. I use marks to postprocessing deletions and insertions. * Despite that, still invalidation warnings happens. */ bool found; Gtk.TextIter w; // Where to insert "r". List<Gtk.TextMark> inserciones = new List<Gtk.TextMark>(); // Marks to insert. Gtk.TextIter ss; // Start of slice containing the occurrence Gtk.TextIter es; // End of slice List<Gtk.TextMark> comienzos = new List<Gtk.TextMark>(); List<Gtk.TextMark> finales = new List<Gtk.TextMark>(); int i = 0; int j; Gtk.TextIter pos; // First pass. Finding... pos = b.StartIter; do{ found = pos.ForwardSearch(s, Gtk.TextSearchFlags.TextOnly, out ss, out es, b.EndIter); if (found){ inserciones.Add(b.CreateMark("check" + i.ToString(), ss, false)); comienzos.Add(b.CreateMark("comienzo" + i.ToString(), ss, false)); finales.Add(b.CreateMark("final" + i.ToString(), es, false)); i++; pos = es; // Search is started after «s» in next iteration } }while (found); // Second pass. Removing... for (j = 0; j < i; j++){ ss = b.GetIterAtMark(comienzos[j]); es = b.GetIterAtMark(finales[j]); b.Delete(ref ss, ref es); } // Third pass. Inserting... for (j = 0; j < i; j++){ w = b.GetIterAtMark(inserciones[j]); // Special case. If "Something()" in r, is a method, not a string. string methodName = esMetodo(r); if (methodName != null) runMethod(methodName, w); else b.Insert(ref w, r); } }
List<Match> FindMatchesInBuffer (NoteBuffer buffer, string [] words) { List<Match> matches = new List<Match> (); string note_text = buffer.GetSlice (buffer.StartIter, buffer.EndIter, false /* hidden_chars */); note_text = note_text.ToLower (); foreach (string word in words) { int idx = 0; bool this_word_found = false; if (word == String.Empty) continue; while (true) { idx = note_text.IndexOf (word, idx); if (idx == -1) { if (this_word_found) break; else return null; } this_word_found = true; Gtk.TextIter start = buffer.GetIterAtOffset (idx); Gtk.TextIter end = start; end.ForwardChars (word.Length); Match match = new Match (); match.Buffer = buffer; match.StartMark = buffer.CreateMark (null, start, false); match.EndMark = buffer.CreateMark (null, end, true); match.Highlighting = false; matches.Add (match); idx += word.Length; } } if (matches.Count == 0) return null; else return matches; }