]> git.lyx.org Git - lyx.git/blobdiff - src/lyxfind.cpp
Fix a crash when closing tabs
[lyx.git] / src / lyxfind.cpp
index 6caa7fa54279c8caeedd42c08a1ca49602cf658d..46e1283ecfd696aec6d7a357cba6ea5344b2e360 100644 (file)
@@ -279,6 +279,15 @@ bool findOne(BufferView * bv, docstring const & searchstr,
             bool find_del, bool check_wrap, bool auto_wrap,
             bool instant, bool onlysel)
 {
+       // Clean up previous selections with empty searchstr on instant
+       if (searchstr.empty() && instant) {
+               if (bv->cursor().selection()) {
+                       bv->setCursor(bv->cursor().selectionBegin());
+                       bv->clearSelection();
+               }
+               return true;
+       }
+
        if (!searchAllowed(searchstr))
                return false;
 
@@ -391,25 +400,30 @@ int replaceAll(BufferView * bv,
                pos_type const pos = cur.pos();
                Font const font = cur.paragraph().getFontSettings(buf.params(), pos);
                cur.recordUndo();
-               int struck = ssize -
+               int ct_deleted_text = ssize -
                        cur.paragraph().eraseChars(pos, pos + match_len,
                                                   buf.params().track_changes);
                cur.paragraph().insert(pos, replacestr, font,
                                       Change(buf.params().track_changes
                                              ? Change::INSERTED
                                              : Change::UNCHANGED));
-               for (int i = 0; i < rsize + struck; ++i)
-                       cur.forwardChar();
+               for (int i = 0; i < rsize + ct_deleted_text
+                    && cur.pos() < cur.lastpos(); ++i)
+                       cur.forwardPos();
                if (onlysel && cur.pit() == endcur.pit() && cur.idx() == endcur.idx()) {
                        // Adjust end of selection for replace-all in selection
                        if (rsize > ssize) {
                                int const offset = rsize - ssize;
-                               for (int i = 0; i < offset + struck; ++i)
+                               for (int i = 0; i < offset + ct_deleted_text
+                                    && endcur.pos() < endcur.lastpos(); ++i)
                                        endcur.forwardPos();
                        } else {
                                int const offset = ssize - rsize;
-                               for (int i = 0; i < offset + struck; ++i)
+                               for (int i = 0; i < offset && endcur.pos() > 0; ++i)
                                        endcur.backwardPos();
+                               for (int i = 0; i < ct_deleted_text
+                                    && endcur.pos() < endcur.lastpos(); ++i)
+                                       endcur.forwardPos();
                        }
                }
                ++num;