]> git.lyx.org Git - lyx.git/blobdiff - src/lyxfind.C
fix some C++ parsing bugs
[lyx.git] / src / lyxfind.C
index 78dd3b416b66e44fa983e51123b91641cf41881a..77427ae82d2ae4b3b15052dd983806d343a628a3 100644 (file)
@@ -1,11 +1,8 @@
 #include <config.h>
 
-#ifdef __GNUG__
-#pragma implementation
-#endif
-
 #include "lyxtext.h"
 #include "lyxfind.h"
+#include "paragraph.h"
 #include "frontends/LyXView.h"
 #include "frontends/Alert.h"
 #include "support/textutils.h"
 #include "buffer.h"
 #include "debug.h"
 #include "gettext.h"
+#include "insets/insettext.h"
+#include "changes.h"
 
 using lyx::pos_type;
+using std::endl;
 
 namespace lyxfind {
 
@@ -57,7 +57,7 @@ int LyXReplace(BufferView * bv,
                text->clearSelection();
                bv->unlockInset(bv->theLockingInset());
                text = bv->text;
-               text->cursorTop(bv);
+               text->cursorTop();
                // override search direction because we search top to bottom
                fw = true;
        }
@@ -85,20 +85,31 @@ int LyXReplace(BufferView * bv,
        int replace_count = 0;
        do {
                text = bv->getLyXText();
-               bv->hideCursor();
-               bv->update(text, BufferView::SELECT|BufferView::FITCUR);
-               bv->toggleSelection(false);
-               text->replaceSelectionWithString(bv, replacestr);
-               text->setSelectionOverString(bv, replacestr);
-               bv->update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
-               ++replace_count;
+               // We have to do this check only because mathed insets don't
+               // return their own LyXText but the LyXText of it's parent!
+               if (!bv->theLockingInset() ||
+                       ((text != bv->text) &&
+                        (text->inset_owner == text->inset_owner->getLockingInset())))
+               {
+                       bv->hideCursor();
+                       bv->update(text, BufferView::SELECT);
+                       bv->toggleSelection(false);
+                       text->replaceSelectionWithString(replacestr);
+                       text->setSelectionRange(replacestr.length());
+                       bv->update(text, BufferView::SELECT);
+                       ++replace_count;
+               }
                if (!once)
                        found = LyXFind(bv, searchstr, fw, casesens, matchwrd);
        } while (!once && replaceall && found);
 
+       // FIXME: should be called via an LFUN
+       bv->buffer()->markDirty();
+       bv->fitCursor();
+
        return replace_count;
 }
+
 
 bool LyXFind(BufferView * bv,
             string const & searchstr, bool forward,
@@ -108,7 +119,7 @@ bool LyXFind(BufferView * bv,
                return false;
 
        bv->hideCursor();
-       bv->update(bv->getLyXText(), BufferView::SELECT|BufferView::FITCUR);
+       bv->update(bv->getLyXText(), BufferView::SELECT);
 
        if (bv->theLockingInset()) {
                bool found = forward ?
@@ -121,7 +132,7 @@ bool LyXFind(BufferView * bv,
                // We now are in the main text but if we did a forward
                // search we have to put the cursor behind the inset.
                if (forward) {
-                       bv->text->cursorRight(bv, true);
+                       bv->text->cursorRight(true);
                }
        }
        // If we arrive here we are in the main text again so we
@@ -135,7 +146,7 @@ bool LyXFind(BufferView * bv,
 
        bv->toggleSelection();
        text->clearSelection();
-       
+
        SearchResult result = forward ?
                SearchForward(bv, text, searchstr, casesens, matchwrd) :
                SearchBackward(bv, text, searchstr, casesens, matchwrd);
@@ -146,19 +157,21 @@ bool LyXFind(BufferView * bv,
        // inset did it already.
        if (result == SR_FOUND) {
                bv->unlockInset(bv->theLockingInset());
-               bv->update(text, BufferView::SELECT|BufferView::FITCUR);
-               text->setSelectionOverString(bv, searchstr);
+               bv->update(text, BufferView::SELECT);
+               text->setSelectionRange(searchstr.length());
                bv->toggleSelection(false);
-               bv->update(text, BufferView::SELECT|BufferView::FITCUR);
+               bv->update(text, BufferView::SELECT);
        } else if (result == SR_NOT_FOUND) {
                bv->unlockInset(bv->theLockingInset());
-               bv->update(text, BufferView::SELECT|BufferView::FITCUR);
+               bv->update(text, BufferView::SELECT);
                found = false;
        }
 
+       bv->fitCursor();
+
        return found;
 }
+
 
 SearchResult LyXFind(BufferView * bv, LyXText * text,
                     string const & searchstr, bool forward,
@@ -244,12 +257,12 @@ SearchResult SearchForward(BufferView * bv, LyXText * text, string const & str,
        }
 
        if (par) {
-               text->setCursor(bv, par, pos);
+               text->setCursor(par, pos);
                return SR_FOUND;
        } else {
                // make sure we end up at the end of the text,
                // not the start point of the last search
-               text->setCursor(bv, prev_par, prev_par->size());
+               text->setCursor(prev_par, prev_par->size());
                return SR_NOT_FOUND;
        }
 }
@@ -294,13 +307,139 @@ SearchResult SearchBackward(BufferView * bv, LyXText * text,
        } while (par && !IsStringInText(par, pos, str, cs, mw));
 
        if (par) {
-               text->setCursor(bv, par, pos);
+               text->setCursor(par, pos);
                return SR_FOUND;
        } else {
                // go to the last part of the unsuccessful search
-               text->setCursor(bv, prev_par, 0);
+               text->setCursor(prev_par, 0);
                return SR_NOT_FOUND;
        }
 }
 
+
+SearchResult nextChange(BufferView * bv, LyXText * text, pos_type & length)
+{
+       Paragraph * par = text->cursor.par();
+       pos_type pos = text->cursor.pos();
+       Paragraph * prev_par = par;
+       UpdatableInset * inset;
+
+       while (par) {
+               if ((!par->size() || pos != par->size())
+                       && par->lookupChange(pos) != Change::UNCHANGED)
+                       break;
+
+               if (par->isInset(pos) &&
+                       (inset = (UpdatableInset *)par->getInset(pos)) &&
+                       (inset->isTextInset())) {
+                       if (inset->nextChange(bv, length))
+                               return SR_FOUND_NOUPDATE;
+               }
+
+               ++pos;
+
+               if (pos >= par->size()) {
+                       prev_par = par;
+                       par = par->next();
+                       pos = 0;
+               }
+       }
+
+       if (par) {
+               text->setCursor(par, pos);
+               Change orig_change = par->lookupChangeFull(pos);
+               pos_type end = pos;
+
+               for (; end != par->size(); ++end) {
+                       Change change = par->lookupChangeFull(end);
+                       if (change != orig_change) {
+                               // slight UI optimisation: for replacements, we get
+                               // text like : _old_new. Consider that as one change.
+                               if (!(orig_change.type == Change::DELETED &&
+                                       change.type == Change::INSERTED))
+                                       break;
+                       }
+               }
+               length = end - pos;
+               return SR_FOUND;
+       } else {
+               // make sure we end up at the end of the text,
+               // not the start point of the last search
+               text->setCursor(prev_par, prev_par->size());
+               return SR_NOT_FOUND;
+       }
+}
+
+
+SearchResult findNextChange(BufferView * bv, LyXText * text, pos_type & length)
+{
+       if (text->selection.set())
+               text->cursor = text->selection.end;
+
+       bv->toggleSelection();
+       text->clearSelection();
+
+       return nextChange(bv, text, length);
+}
+
+
+bool findNextChange(BufferView * bv)
+{
+       if (!bv->available())
+               return false;
+
+       bv->hideCursor();
+       bv->update(bv->getLyXText(), BufferView::SELECT);
+
+       pos_type length;
+
+       if (bv->theLockingInset()) {
+               bool found = bv->theLockingInset()->nextChange(bv, length);
+
+               // We found the stuff inside the inset so we don't have to
+               // do anything as the inset did all the update for us!
+               if (found)
+                       return true;
+
+               // We now are in the main text but if we did a forward
+               // search we have to put the cursor behind the inset.
+               bv->text->cursorRight(true);
+       }
+       // If we arrive here we are in the main text again so we
+       // just start searching from the root LyXText at the position
+       // we are!
+       LyXText * text = bv->text;
+
+       if (text->selection.set())
+               text->cursor = text->selection.end;
+
+       bv->toggleSelection();
+       text->clearSelection();
+
+       SearchResult result = nextChange(bv, text, length);
+
+       lyxerr << "Result is " << result << endl;
+
+       bool found = true;
+
+       // If we found the cursor inside an inset we will get back
+       // SR_FOUND_NOUPDATE and we don't have to do anything as the
+       // inset did it already.
+       if (result == SR_FOUND) {
+               bv->unlockInset(bv->theLockingInset());
+               bv->update(text, BufferView::SELECT);
+               text->setSelectionRange(length);
+               bv->toggleSelection(false);
+               bv->update(text, BufferView::SELECT);
+       } else if (result == SR_NOT_FOUND) {
+               bv->unlockInset(bv->theLockingInset());
+               bv->update(text, BufferView::SELECT);
+               found = false;
+       }
+
+       bv->fitCursor();
+
+       return found;
+}
+
 } // end lyxfind namespace