]> git.lyx.org Git - lyx.git/blobdiff - src/lyxfind.cpp
Replace Q_WS_WIN with appropriate guards to account for Qt5.
[lyx.git] / src / lyxfind.cpp
index 99e361ea47c6b7fb59d37441ddcd484fcd0a3f16..6b9556e8790e92fe3d2a4f87202cfce0d99927c7 100644 (file)
@@ -182,9 +182,9 @@ int replaceAll(BufferView * bv,
                cur.recordUndo();
                int striked = ssize -
                        cur.paragraph().eraseChars(pos, pos + match_len,
-                                                  buf.params().trackChanges);
+                                                  buf.params().track_changes);
                cur.paragraph().insert(pos, replacestr, font,
-                                      Change(buf.params().trackChanges
+                                      Change(buf.params().track_changes
                                              ? Change::INSERTED
                                              : Change::UNCHANGED));
                for (int i = 0; i < rsize + striked; ++i)
@@ -232,7 +232,9 @@ pair<bool, int> replaceOne(BufferView * bv, docstring searchstr,
                if (!cur.inTexted())
                        // bail in math
                        return make_pair(false, 0);
-               // select current word and treat it as the search string
+               // select current word and treat it as the search string.
+               // This causes a minor bug as undo will restore this selection,
+               // which the user did not create (#8986).
                cur.innerText()->selectWord(cur, WHOLE_WORD);
                searchstr = cur.selectionAsString(false);
        }
@@ -398,7 +400,7 @@ bool findChange(DocIterator & cur, bool next)
                        if (!next)
                                // if we search backwards, take a step forward
                                // to correctly set the anchor
-                               cur.forwardPos();
+                               cur.top().forwardPos();
                        return true;
                }
 
@@ -417,21 +419,24 @@ bool findChange(BufferView * bv, bool next)
        // of this function). This will avoid changes to be selected half.
        bool search_both_sides = false;
        Cursor tmpcur = cur;
-       // Leave math first
+       // Find enclosing text cursor
        while (tmpcur.inMathed())
                tmpcur.pop_back();
        Change change_next_pos
                = tmpcur.paragraph().lookupChange(tmpcur.pos());
-       if (change_next_pos.changed() && cur.inMathed()) {
-               cur = tmpcur;
-               search_both_sides = true;
-       } else if (tmpcur.pos() > 0 && tmpcur.inTexted()) {
-               Change change_prev_pos
-                       = tmpcur.paragraph().lookupChange(tmpcur.pos() - 1);
-               if (change_next_pos.isSimilarTo(change_prev_pos))
+       if (change_next_pos.changed()) {
+               if (cur.inMathed()) {
+                       cur = tmpcur;
                        search_both_sides = true;
+               } else if (tmpcur.pos() > 0 && tmpcur.inTexted()) {
+                       Change change_prev_pos
+                               = tmpcur.paragraph().lookupChange(tmpcur.pos() - 1);
+                       if (change_next_pos.isSimilarTo(change_prev_pos))
+                               search_both_sides = true;
+               }
        }
 
+       // find the next change
        if (!findChange(cur, next))
                return false;
 
@@ -439,7 +444,7 @@ bool findChange(BufferView * bv, bool next)
 
        CursorSlice & tip = cur.top();
 
-       if (!next)
+       if (!next && tip.pos() > 0)
                // take a step into the change
                tip.backwardPos();
 
@@ -777,14 +782,17 @@ static docstring stringifySearchBuffer(Buffer & buffer, FindAndReplaceOptions co
                runparams.flavor = OutputParams::LATEX;
                runparams.linelen = 100000; //lyxrc.plaintext_linelen;
                runparams.dryrun = true;
+               runparams.for_search = true;
                for (pos_type pit = pos_type(0); pit < (pos_type)buffer.paragraphs().size(); ++pit) {
                        Paragraph const & par = buffer.paragraphs().at(pit);
                        LYXERR(Debug::FIND, "Adding to search string: '"
-                              << par.stringify(pos_type(0), par.size(),
-                                               AS_STR_INSETS, runparams)
+                              << par.asString(pos_type(0), par.size(),
+                                              AS_STR_INSETS | AS_STR_SKIPDELETE | AS_STR_PLAINTEXT,
+                                              &runparams)
                               << "'");
-                       str += par.stringify(pos_type(0), par.size(),
-                                            AS_STR_INSETS, runparams);
+                       str += par.asString(pos_type(0), par.size(),
+                                           AS_STR_INSETS | AS_STR_SKIPDELETE | AS_STR_PLAINTEXT,
+                                           &runparams);
                }
        }
        return str;
@@ -1035,7 +1043,9 @@ docstring stringifyFromCursor(DocIterator const & cur, int len)
                runparams.dryrun = true;
                LYXERR(Debug::FIND, "Stringifying with cur: "
                       << cur << ", from pos: " << cur.pos() << ", end: " << end);
-               return par.stringify(cur.pos(), end, AS_STR_INSETS, runparams);
+               return par.asString(cur.pos(), end,
+                       AS_STR_INSETS | AS_STR_SKIPDELETE | AS_STR_PLAINTEXT,
+                       &runparams);
        } else if (cur.inMathed()) {
                docstring s;
                CursorSlice cs = cur.top();
@@ -1372,7 +1382,8 @@ static void findAdvReplace(BufferView * bv, FindAndReplaceOptions const & opt, M
        DocIterator sel_beg = cur.selectionBegin();
        DocIterator sel_end = cur.selectionEnd();
        if (&sel_beg.inset() != &sel_end.inset()
-           || sel_beg.pit() != sel_end.pit())
+           || sel_beg.pit() != sel_end.pit()
+           || sel_beg.idx() != sel_end.idx())
                return;
        int sel_len = sel_end.pos() - sel_beg.pos();
        LYXERR(Debug::FIND, "sel_beg: " << sel_beg << ", sel_end: " << sel_end