]> git.lyx.org Git - features.git/commitdiff
the spellcheck cleanup
authorAlfredo Braunstein <abraunst@lyx.org>
Tue, 4 Nov 2003 12:01:15 +0000 (12:01 +0000)
committerAlfredo Braunstein <abraunst@lyx.org>
Tue, 4 Nov 2003 12:01:15 +0000 (12:01 +0000)
git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@8034 a592a061-630c-0410-9148-cb99ea01b6c8

27 files changed:
src/BufferView_pimpl.C
src/ChangeLog
src/frontends/controllers/ChangeLog
src/frontends/controllers/ControlSearch.C
src/frontends/controllers/ControlSearch.h
src/frontends/controllers/ControlThesaurus.C
src/frontends/qt2/ChangeLog
src/frontends/qt2/QSearch.C
src/frontends/qt2/QSearch.h
src/frontends/qt2/QSearchDialog.C
src/frontends/xforms/ChangeLog
src/frontends/xforms/FormSearch.C
src/insets/ChangeLog
src/insets/insetcollapsable.C
src/insets/insetcollapsable.h
src/insets/insetert.C
src/insets/insettabular.C
src/insets/insettabular.h
src/insets/insettext.C
src/insets/insettext.h
src/insets/updatableinset.C
src/insets/updatableinset.h
src/lyxfind.C
src/lyxfind.h
src/lyxfunc.C
src/paragraph.C
src/paragraph.h

index dafede22355b31f2992e5e1be3b0f5e612239aa4..93b49159fe9775986ec819ab1233c8076758e89a 100644 (file)
@@ -418,8 +418,6 @@ void BufferView::Pimpl::resizeCurrentBuffer()
                bv_->text->fullRebreak();
                update();
        } else {
-               lyxerr << "text not available!" << endl;
-                       lyxerr << "no text in cache!" << endl;
                        bv_->text = new LyXText(bv_, 0, false, bv_->buffer()->paragraphs());
                        bv_->text->init(bv_);
        }
index e879aa8c1ec12b1c1c53d08c88c009d576481881..086b6668aa8d6f167a57e1e2e2ffe3e859a11038 100644 (file)
@@ -1,3 +1,9 @@
+2003-11-04  Alfredo Braunstein  <abraunst@libero.it>
+
+       * lyxfind.[Ch]: complete overhaul
+       * BufferView_pimpl.C:
+       * lyxfunc.C: adjust
+       * paragraph.[Ch] (insert): add
 
 2003-11-04  Alfredo Braunstein  <abraunst@libero.it>
 
index 67e532930d8f3c9f4739c32922b0b0b3cbba82db..785887848e804f564035924855a74189b26f634d 100644 (file)
@@ -1,3 +1,9 @@
+
+2003-11-04  Alfredo Braunstein  <abraunst@libero.it>
+
+       * ControlSearch.[Ch]:
+       * ControlThesaurus.C: adjust to spellcheck code changes
+
 2003-11-04  Alfredo Braunstein  <abraunst@libero.it>
 
        * ControlSpellchecker.C (nextWord): do not send ligature breaks
index 7b3fdbff6a90ea0be0a269327f2604e7d49ab33a..e1482ec28f45b282990f3fea74cbce8b4c2a7759 100644 (file)
@@ -32,8 +32,8 @@ void ControlSearch::find(string const & search,
                         bool casesensitive, bool matchword, bool forward)
 {
        bool const found = lyx::find::find(bufferview(), search,
-                                           forward, casesensitive,
-                                           matchword);
+                                          casesensitive, matchword,
+                                          forward);
 
        if (!found)
                lv_.message(_("String not found!"));
@@ -41,16 +41,17 @@ void ControlSearch::find(string const & search,
 
 
 void ControlSearch::replace(string const & search, string const & replace,
-                           bool casesensitive, bool matchword, bool all)
+                           bool casesensitive, bool matchword,
+                           bool forward, bool all)
 {
        // If not replacing all instances of the word, then do not
        // move on to the next instance once the present instance has been
        // changed
-       bool const once = !all;
-       int const replace_count =
-               lyx::find::replace(bufferview(),
-                                   search, replace, true, casesensitive,
-                                   matchword, all, once);
+       int const replace_count = all ?
+               lyx::find::replaceAll(bufferview(), search, replace,
+                                     casesensitive, matchword)
+               : lyx::find::replace(bufferview(), search, replace,
+                                    casesensitive, matchword, forward);
 
        if (replace_count == 0) {
                lv_.message(_("String not found!"));
index 16855db05f98c15f6aa8ebb7c54d76c5738d9476..5c34643c4c368aff145f7ebe04b95593a9e632dd 100644 (file)
@@ -28,7 +28,8 @@ public:
 
        /// Replaces occurence of string
        void replace(std::string const & search, std::string const & replace,
-                    bool casesensitive, bool matchword, bool all);
+                    bool casesensitive, bool matchword,
+                    bool forward, bool all);
 private:
        /// not needed.
        virtual void apply() {}
index da3883f0fe32d123f17f204d660b619abd2fe6b5..d6692583f4f76515252ea9db094f6d1554000ebe 100644 (file)
@@ -48,7 +48,7 @@ void ControlThesaurus::replace(string const & newstr)
         */
        int const replace_count =
                lyx::find::replace(kernel().bufferview(), oldstr_, newstr,
-                                   true, true, true, false, true);
+                                  true /*cs*/, true /*mw*/, true /*fw*/);
 
        oldstr_ = newstr;
 
index 4c4686d1778e9b542820d3d773b37a7ea9ee5f8d..94a01dc9b6472e3c5aaf496b1b764d46f35743d9 100644 (file)
@@ -1,3 +1,9 @@
+
+2003-11-04  Alfredo Braunstein  <abraunst@libero.it>
+
+       * QSearch.[Ch]:
+       * QSearchDialog.C: adjust to spellcheck code changes
+
 2003-11-01  Juergen Spitzmueller  <j.spitzmueller@gmx.de>
 
        * ui/QParagraphDialogBase.ui: finally remove [line|pagebreak]_[above|below]
index 6a705f4c7c2826bf8da6a2787b5a5398bf849385..a74f29b0e2b9e7883168cfea13d4cdda937d1486 100644 (file)
@@ -54,7 +54,8 @@ void QSearch::find(string const & str, bool casesens,
 
 
 void QSearch::replace(string const & findstr, string const & replacestr,
-       bool casesens, bool words, bool all)
+       bool casesens, bool words, bool backwards, bool all)
 {
-       controller().replace(findstr, replacestr, casesens, words, all);
+       controller().replace(findstr, replacestr, casesens, words,
+                            !backwards, all);
 }
index 3da9d557ffd602f1bd2a2bd2e647b7ca41ccdd0f..3c179dac9f2240d3e0f4891fc002c8e69845a3b4 100644 (file)
@@ -39,8 +39,9 @@ private:
        void find(std::string const & str, bool casesens,
                  bool words, bool backwards);
 
-       void replace(std::string const & findstr, std::string const & replacestr,
-               bool casesens, bool words, bool all);
+       void replace(std::string const & findstr,
+                    std::string const & replacestr,
+                    bool casesens, bool words, bool backwards, bool all);
 };
 
 #endif // QSEARCH_H
index 7131e42bd4b72b7afd29cf1266ffd9dcc4429755..113202d11af8e58678181d9bae539e19cb6a1564 100644 (file)
@@ -94,7 +94,7 @@ void QSearchDialog::replaceClicked()
        form_->replace(find, replace,
                caseCB->isChecked(),
                wordsCB->isChecked(),
-               false);
+               backwardsCB->isChecked(), false);
        uniqueInsert(findCO, findCO->currentText());
        uniqueInsert(replaceCO, replaceCO->currentText());
 }
@@ -106,7 +106,7 @@ void QSearchDialog::replaceallClicked()
                fromqstr(replaceCO->currentText()),
                caseCB->isChecked(),
                wordsCB->isChecked(),
-               true);
+               false, true);
        uniqueInsert(findCO, findCO->currentText());
        uniqueInsert(replaceCO, replaceCO->currentText());
 }
index 007b4a9619b24c9bc1bcc399294feb9a12bf773f..93dd6dc69d513733ae9351f56fa7c1e5dc528d88 100644 (file)
@@ -1,4 +1,8 @@
 
+2003-11-04  Alfredo Braunstein  <abraunst@libero.it>
+
+       * FormSearch.C: adjust to spellcheck changes
+
 2003-10-27  André Pönitz  <poenitz@gmx.net>
 
        * FormParagraph.C:
index 66037d7bc1db5ae8de9f07f39b74465ee9764258..261d895cf6143011997af7cf69b214fe05f94a8f 100644 (file)
@@ -85,6 +85,7 @@ ButtonPolicy::SMInput FormSearch::input(FL_OBJECT * ob, long)
                                     getString(dialog_->input_replace),
                                     fl_get_button(dialog_->check_casesensitive),
                                     fl_get_button(dialog_->check_matchword),
+                                    !fl_get_button(dialog_->check_searchbackwards),                                 
                                     all);
        }
 
index 907802f4271f696ee2f6271f60cb9bf96c79169d..2a304e608c74f907fc1684190c8fb44457ccca9e 100644 (file)
@@ -1,4 +1,13 @@
 
+2003-11-04  Alfredo Braunstein  <abraunst@libero.it>
+
+       * inset.h:
+       * insetcollapsable.[Ch]:
+       * insetert.[Ch]:
+       * insettabular.[Ch]:
+       * insettext.[Ch]:
+       * updatableinset.[Ch]: remove dead search code
+
 2003-11-04  Alfredo Braunstein  <abraunst@libero.it>
 
        * inset.h:
index e3a889d80fc81a6f18247cbe0b989341d46d2e73..fa467dfeb48ea1f5974cb26f5ee4ee5391d97ab2 100644 (file)
@@ -527,42 +527,6 @@ void InsetCollapsable::markErased()
 }
 
 
-bool InsetCollapsable::nextChange(BufferView * bv, lyx::pos_type & length)
-{
-       bool found = inset.nextChange(bv, length);
-
-       if (first_after_edit && !found)
-               close(bv);
-       else if (!found)
-               first_after_edit = false;
-       return found;
-}
-
-
-bool InsetCollapsable::searchForward(BufferView * bv, string const & str,
-                                    bool cs, bool mw)
-{
-       bool found = inset.searchForward(bv, str, cs, mw);
-       if (first_after_edit && !found)
-               close(bv);
-       else if (!found)
-               first_after_edit = false;
-       return found;
-}
-
-
-bool InsetCollapsable::searchBackward(BufferView * bv, string const & str,
-                                     bool cs, bool mw)
-{
-       bool found = inset.searchBackward(bv, str, cs, mw);
-       if (first_after_edit && !found)
-               close(bv);
-       else if (!found)
-               first_after_edit = false;
-       return found;
-}
-
-
 void InsetCollapsable::addPreview(PreviewLoader & loader) const
 {
        inset.addPreview(loader);
index 9a85975d8398c6dc14533b55a5bcc40a4bfc029c..de9f2229b164d649dd9d67f8e86ddd3246f043de 100644 (file)
@@ -133,14 +133,6 @@ public:
 
        void markErased();
 
-       bool nextChange(BufferView * bv, lyx::pos_type & length);
-
-       ///
-       bool searchForward(BufferView * bv, std::string const & str,
-                          bool = true, bool = false);
-       bool searchBackward(BufferView * bv, std::string const & str,
-                           bool = true, bool = false);
-
        ///
        void addPreview(lyx::graphics::PreviewLoader &) const;
 
index 0dfa14f46d8fd5e965ebb21dbc5560261330e98f..1621b2016c9d5ce19687c49829e2481c4672cdd3 100644 (file)
@@ -445,6 +445,8 @@ InsetERT::priv_dispatch(FuncRequest const & cmd,
                                break;
                        result = inset.dispatch(cmd);
                } else {
+                       // Is the following line correct? Ab
+                       open(bv);
                        result = InsetCollapsable::priv_dispatch(cmd, idx, pos);
                }
                set_latex_font(bv);
index 38f0eb82966da70d2da964d38951909c0b3f01af..eb170b68bb1d823c994a8a418b061c10e1e30efa 100644 (file)
@@ -2425,88 +2425,6 @@ void InsetTabular::markErased()
 }
 
 
-bool InsetTabular::nextChange(BufferView * bv, lyx::pos_type & length)
-{
-       if (the_locking_inset) {
-               if (the_locking_inset->nextChange(bv, length)) {
-                       updateLocal(bv);
-                       return true;
-               }
-               if (tabular.isLastCell(actcell))
-                       return false;
-               ++actcell;
-       }
-       InsetText & inset = tabular.getCellInset(actcell);
-       if (inset.nextChange(bv, length)) {
-               updateLocal(bv);
-               return true;
-       }
-       while (!tabular.isLastCell(actcell)) {
-               ++actcell;
-               InsetText & inset = tabular.getCellInset(actcell);
-               if (inset.nextChange(bv, length)) {
-                       updateLocal(bv);
-                       return true;
-               }
-       }
-       return false;
-}
-
-
-bool InsetTabular::searchForward(BufferView * bv, string const & str,
-                                bool cs, bool mw)
-{
-       int cell = 0;
-       if (the_locking_inset) {
-               if (the_locking_inset->searchForward(bv, str, cs, mw)) {
-                       updateLocal(bv);
-                       return true;
-               }
-               if (tabular.isLastCell(actcell))
-                       return false;
-               cell = actcell + 1;
-       }
-       InsetText & inset = tabular.getCellInset(cell);
-       if (inset.searchForward(bv, str, cs, mw)) {
-               updateLocal(bv);
-               return true;
-       }
-       while (!tabular.isLastCell(cell)) {
-               ++cell;
-               InsetText & inset = tabular.getCellInset(cell);
-               if (inset.searchForward(bv, str, cs, mw)) {
-                       updateLocal(bv);
-                       return true;
-               }
-       }
-       return false;
-}
-
-
-bool InsetTabular::searchBackward(BufferView * bv, string const & str,
-                              bool cs, bool mw)
-{
-       int cell = tabular.getNumberOfCells();
-       if (the_locking_inset) {
-               if (the_locking_inset->searchBackward(bv, str, cs, mw)) {
-                       updateLocal(bv);
-                       return true;
-               }
-               cell = actcell;
-       }
-
-       while (cell) {
-               --cell;
-               InsetText & inset = tabular.getCellInset(cell);
-               if (inset.searchBackward(bv, str, cs, mw)) {
-                       updateLocal(bv);
-                       return true;
-               }
-       }
-       return false;
-}
-
-
 bool InsetTabular::insetAllowed(InsetOld::Code code) const
 {
        if (the_locking_inset)
index 145bd1ee646966e9adaef31baa029ab73c90d146..0e012ba93cdd48029dda4bfaa0824b4a75e8bf31 100644 (file)
@@ -169,14 +169,6 @@ public:
        ///
        void markErased();
 
-       /// find next change
-       bool nextChange(BufferView *, lyx::pos_type & length);
-       ///
-       bool searchForward(BufferView *, std::string const &,
-                          bool = true, bool = false);
-       bool searchBackward(BufferView *, std::string const &,
-                           bool = true, bool = false);
-
        // this should return true if we have a "normal" cell, otherwise true.
        // "normal" means without width set!
        bool forceDefaultParagraphs(InsetOld const * in) const;
index 150193edc1a647da6ae3ccb684e3fd0d23c488ca..e029e2cd497f6f226fc858c68a1f564c09641767 100644 (file)
@@ -1455,79 +1455,6 @@ LyXCursor const & InsetText::cursor(BufferView * bv) const
 }
 
 
-bool InsetText::nextChange(BufferView * bv, lyx::pos_type & length)
-{
-       if (the_locking_inset) {
-               if (the_locking_inset->nextChange(bv, length))
-                       return true;
-               text_.cursorRight(true);
-       }
-       lyx::find::SearchResult result =
-               lyx::find::findNextChange(bv, &text_, length);
-
-       if (result == lyx::find::SR_FOUND) {
-               LyXCursor cur = text_.cursor;
-               bv->unlockInset(bv->theLockingInset());
-               if (bv->lockInset(this))
-                       locked = true;
-               text_.cursor = cur;
-               text_.setSelectionRange(length);
-               updateLocal(bv, false);
-       }
-       return result != lyx::find::SR_NOT_FOUND;
-}
-
-
-bool InsetText::searchForward(BufferView * bv, string const & str,
-                             bool cs, bool mw)
-{
-       if (the_locking_inset) {
-               if (the_locking_inset->searchForward(bv, str, cs, mw))
-                       return true;
-               text_.cursorRight(true);
-       }
-       lyx::find::SearchResult result =
-               lyx::find::find(bv, &text_, str, true, cs, mw);
-
-       if (result == lyx::find::SR_FOUND) {
-               LyXCursor cur = text_.cursor;
-               bv->unlockInset(bv->theLockingInset());
-               if (bv->lockInset(this))
-                       locked = true;
-               text_.cursor = cur;
-               text_.setSelectionRange(str.length());
-               updateLocal(bv, false);
-       }
-       return result != lyx::find::SR_NOT_FOUND;
-}
-
-
-bool InsetText::searchBackward(BufferView * bv, string const & str,
-                              bool cs, bool mw)
-{
-       if (the_locking_inset) {
-               if (the_locking_inset->searchBackward(bv, str, cs, mw))
-                       return true;
-       }
-       if (!locked) {
-               text_.setCursor(paragraphs.size() - 1, paragraphs.back().size());
-       }
-       lyx::find::SearchResult result =
-               lyx::find::find(bv, &text_, str, false, cs, mw);
-
-       if (result == lyx::find::SR_FOUND) {
-               LyXCursor cur = text_.cursor;
-               bv->unlockInset(bv->theLockingInset());
-               if (bv->lockInset(this))
-                       locked = true;
-               text_.cursor = cur;
-               text_.setSelectionRange(str.length());
-               updateLocal(bv, false);
-       }
-       return result != lyx::find::SR_NOT_FOUND;
-}
-
-
 bool InsetText::checkInsertChar(LyXFont & font)
 {
        return owner() ? owner()->checkInsertChar(font) : true;
index b9de092828b21cce0366807cfca6abe885ba32b0..57ce3daa25403035441f93b9d0d7caa5bbbe47cb 100644 (file)
@@ -168,15 +168,7 @@ public:
         * for the (empty) paragraph contained.
         */
        void markNew(bool track_changes = false);
-       /// find next change
-       bool nextChange(BufferView *, lyx::pos_type & length);
 
-       ///
-       bool searchForward(BufferView *, std::string const &,
-                          bool = true, bool = false);
-       ///
-       bool searchBackward(BufferView *, std::string const &,
-                           bool = true, bool = false);
        ///
        bool checkInsertChar(LyXFont &);
        ///
index a62fbc0be7c112af2a1856dfc6c543b4645290ad..4a5cb9a2e992f6526889240fd3884a0acb947afa 100644 (file)
@@ -136,29 +136,3 @@ LyXCursor const & InsetOld::cursor(BufferView * bv) const
                return owner()->getLyXText(bv, false)->cursor;
        return bv->text->cursor;
 }
-
-
-bool UpdatableInset::nextChange(BufferView * bv, lyx::pos_type &)
-{
-       // we have to unlock ourself in this function by default!
-       bv->unlockInset(const_cast<UpdatableInset *>(this));
-       return false;
-}
-
-
-bool UpdatableInset::searchForward(BufferView * bv, string const &,
-                                  bool, bool)
-{
-       // we have to unlock ourself in this function by default!
-       bv->unlockInset(const_cast<UpdatableInset *>(this));
-       return false;
-}
-
-
-bool UpdatableInset::searchBackward(BufferView * bv, string const &,
-                                   bool, bool)
-{
-       // we have to unlock ourself in this function by default!
-       bv->unlockInset(const_cast<UpdatableInset *>(this));
-       return false;
-}
index bdb503bced32cde9b6c756d53575cc126b464c37..058db608dd7227fd359fcdbb18128ec1ec626fb1 100644 (file)
@@ -89,18 +89,6 @@ public:
        ///
        virtual void toggleSelection(BufferView *, bool /*kill_selection*/) {}
 
-       /// find the next change in the inset
-       virtual bool nextChange(BufferView * bv, lyx::pos_type & length);
-
-       ///
-       // needed for search/replace functionality
-       ///
-       virtual bool searchForward(BufferView *, std::string const &,
-                                  bool = true, bool = false);
-       ///
-       virtual bool searchBackward(BufferView *, std::string const &,
-                                   bool = true, bool = false);
-
 protected:
        ///  An updatable inset could handle lyx editing commands
        virtual
index dfa961eb181a8593628c0dbf2bff82e0f690c521..fe64e943a80ddef6e4c59351df3a37b3ffd880c7 100644 (file)
@@ -6,6 +6,7 @@
  * \author Lars Gullik Bjønnes
  * \author John Levon
  * \author Jürgen Vigna
+ * \author Alfredo Braunstein
  *
  * Full author contact details are available in file CREDITS.
  */
 
 #include "buffer.h"
 #include "BufferView.h"
+#include "debug.h"
+#include "iterators.h"
 #include "gettext.h"
 #include "lyxtext.h"
 #include "paragraph.h"
+#include "PosIterator.h"
+#include "undo.h"
 
 #include "frontends/Alert.h"
 
@@ -28,6 +33,7 @@
 
 using lyx::support::lowercase;
 using lyx::support::uppercase;
+using bv_funcs::put_selection_at;
 
 using std::string;
 
@@ -37,304 +43,234 @@ namespace find {
 
 namespace {
 
-// returns true if the specified string is at the specified position
-bool isStringInText(Paragraph const & par, pos_type pos,
-                   string const & str, bool const & cs,
-                   bool const & mw)
+class MatchString
 {
-       string::size_type size = str.length();
-       pos_type i = 0;
-       pos_type parsize = par.size();
-       while ((pos + i < parsize)
-              && (string::size_type(i) < size)
-              && (cs ? (str[i] == par.getChar(pos + i))
-                  : (uppercase(str[i]) == uppercase(par.getChar(pos + i))))) {
-               ++i;
-       }
-
-       if (size == string::size_type(i)) {
-               // if necessary, check whether string matches word
-               if (!mw)
-                       return true;
-               if ((pos <= 0 || !IsLetterCharOrDigit(par.getChar(pos - 1)))
-                       && (pos + pos_type(size) >= parsize
-                       || !IsLetterCharOrDigit(par.getChar(pos + size)))) {
-                       return true;
+public:
+       MatchString(string const & str, bool cs, bool mw)
+               : str(str), cs(cs), mw(mw) {};
+// returns true if the specified string is at the specified position
+       bool operator()(Paragraph const & par, pos_type pos) const
+       {                       
+               string::size_type size = str.length();
+               pos_type i = 0;
+               pos_type parsize = par.size();
+               while ((pos + i < parsize)
+                      && (string::size_type(i) < size)
+                      && (cs ? (str[i] == par.getChar(pos + i))
+                          : (uppercase(str[i]) == uppercase(par.getChar(pos + i))))) {
+                       ++i;
                }
-       }
-       return false;
-}
-
-// forward search:
-// if the string can be found: return true and set the cursor to
-// the new position, cs = casesensitive, mw = matchword
-SearchResult searchForward(BufferView * bv, LyXText * text, string const & str,
-                          bool const & cs, bool const & mw)
-{
-       ParagraphList::iterator pit = text->cursorPar();
-       ParagraphList::iterator pend = text->ownerParagraphs().end();
-       pos_type pos = text->cursor.pos();
-       UpdatableInset * inset;
-
-       while (pit != pend && !isStringInText(*pit, pos, str, cs, mw)) {
-               if (pos < pit->size()
-                   && pit->isInset(pos)
-                   && (inset = (UpdatableInset *)pit->getInset(pos))
-                   && inset->isTextInset()
-                   && inset->searchForward(bv, str, cs, mw))
-                       return SR_FOUND_NOUPDATE;
-
-               if (++pos >= pit->size()) {
-                       ++pit;
-                       pos = 0;
+               if (size == string::size_type(i)) {
+                       // if necessary, check whether string matches word
+                       if (!mw)
+                               return true;
+                       if ((pos <= 0 || !IsLetterCharOrDigit(par.getChar(pos - 1)))
+                           && (pos + pos_type(size) >= parsize
+                               || !IsLetterCharOrDigit(par.getChar(pos + size)))) {
+                               return true;
+                       }
                }
+               return false;
        }
+       
+private:
+       string str;
+       bool cs;
+       bool mw;
+};
 
-       if (pit != pend) {
-               text->setCursor(pit, pos);
-               return SR_FOUND;
-       }
-       return SR_NOT_FOUND;
-}
 
 
-// backward search:
-// if the string can be found: return true and set the cursor to
-// the new position, cs = casesensitive, mw = matchword
-SearchResult searchBackward(BufferView * bv, LyXText * text,
-                           string const & str,
-                           bool const & cs, bool const & mw)
-{
-       ParagraphList::iterator pit = text->cursorPar();
-       ParagraphList::iterator pbegin = text->ownerParagraphs().begin();
-       pos_type pos = text->cursor.pos();
-
-       // skip past a match at the current cursor pos
-       if (pos > 0) {
-               --pos;
-       } else if (pit != pbegin) {
-               --pit;
-               pos = pit->size();
-       } else {
-               return SR_NOT_FOUND;
-       }
+} //namespace anon
 
-       while (true) {
-               if (pos < pit->size()) {
-                       if (pit->isInset(pos) && pit->getInset(pos)->isTextInset()) {
-                               UpdatableInset * inset = (UpdatableInset *)pit->getInset(pos);
-                               if (inset->searchBackward(bv, str, cs, mw))
-                                       return SR_FOUND_NOUPDATE;
-                       }
 
-                       if (isStringInText(*pit, pos, str, cs, mw)) {
-                               text->setCursor(pit, pos);
-                               return SR_FOUND;
-                       }
-               }
 
-               if (pos == 0 && pit == pbegin)
-                       break;
 
-               if (pos > 0) {
-                       --pos;
-               } else if (pit != pbegin) {
-                       --pit;
-                       pos = pit->size();
-               }
-       }
 
-       return SR_NOT_FOUND;
-}
+namespace {
 
-} // anon namespace
 
 
 
-int replace(BufferView * bv,
-              string const & searchstr, string const & replacestr,
-              bool forward, bool casesens, bool matchwrd, bool replaceall,
-              bool once)
+bool findForward(PosIterator & cur, PosIterator const & end,
+                MatchString & match)
 {
-       if (!bv->available() || bv->buffer()->isReadonly())
-               return 0;
+       for (; cur != end && !match(*cur.pit(), cur.pos()); ++cur)
+               ;
 
-       // CutSelection cannot cut a single space, so we have to stop
-       // in order to avoid endless loop :-(
-       if (searchstr.length() == 0
-               || (searchstr.length() == 1 && searchstr[0] == ' ')) {
-#ifdef WITH_WARNINGS
-#warning BLECH. If we have an LFUN for replace, we can sort of fix this bogosity
-#endif
-               Alert::error(_("Cannot replace"),
-                       _("You cannot replace a single space or "
-                         "an empty character."));
-               return 0;
-       }
-
-       // now we can start searching for the first
-       // start at top if replaceall
-       LyXText * text = bv->getLyXText();
-       bool fw = forward;
-       if (replaceall) {
-               text->clearSelection();
-               bv->unlockInset(bv->theLockingInset());
-               text = bv->text;
-               text->cursorTop();
-               // override search direction because we search top to bottom
-               fw = true;
-       }
+       return cur != end;
+}
 
-       // if nothing selected or selection does not equal search string
-       // search and select next occurance and return if no replaceall
-       string str1;
-       string str2;
-       if (casesens) {
-               str1 = searchstr;
-               str2 = text->selectionAsString(*bv->buffer(), false);
-       } else {
-               str1 = lowercase(searchstr);
-               str2 = lowercase(text->selectionAsString(*bv->buffer(), false));
-       }
-       if (str1 != str2) {
-               if (!find(bv, searchstr, fw, casesens, matchwrd) ||
-                       !replaceall) {
-                       return 0;
-               }
-       }
 
-       bool found = false;
-       int replace_count = 0;
+bool findBackwards(PosIterator & cur, PosIterator const & beg,
+                  MatchString & match)
+{
+       if (beg == cur)
+               return false;
        do {
-               text = bv->getLyXText();
-               // 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()))) {
-                       text->replaceSelectionWithString(replacestr);
-                       text->setSelectionRange(replacestr.length());
-                       ++replace_count;
-               }
-               if (!once)
-                       found = find(bv, searchstr, fw, casesens, matchwrd);
-       } while (!once && replaceall && found);
+               --cur;
+               if (match(*cur.pit(), cur.pos()))
+                       break;
+       } while (cur != beg);
 
-       // FIXME: should be called via an LFUN
-       bv->buffer()->markDirty();
-       bv->update();
+       return match(*cur.pit(), cur.pos());
+}
 
-       return replace_count;
+
+bool findChange(PosIterator & cur, PosIterator const & end)
+{
+       for (; cur != end; ++cur) {
+               if ((!cur.pit()->size() || !cur.at_end())
+                   && cur.pit()->lookupChange(cur.pos()) != Change::UNCHANGED)
+                       break;
+       }
+       
+       return cur != end;
 }
 
 
-bool find(BufferView * bv,
-            string const & searchstr, bool forward,
-            bool casesens, bool matchwrd)
+bool searchAllowed(BufferView * bv, string const & str)
 {
-       if (!bv->available() || searchstr.empty())
+       if (str.empty()) {
+               Alert::error(_("Search error"), _("Search string is empty"));
                return false;
-
-       if (bv->theLockingInset()) {
-               bool found = forward ?
-                       bv->theLockingInset()->searchForward(bv, searchstr, casesens, matchwrd) :
-                       bv->theLockingInset()->searchBackward(bv, searchstr, casesens, matchwrd);
-               // 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.
-               if (forward) {
-                       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 = forward ?
-                       text->selection.end : text->selection.start;
-
-       text->clearSelection();
-
-       SearchResult result = forward ?
-               searchForward(bv, text, searchstr, casesens, matchwrd) :
-               searchBackward(bv, text, searchstr, casesens, matchwrd);
-
-       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());
-               text->setSelectionRange(searchstr.length());
-       } else if (result == SR_NOT_FOUND) {
-               bv->unlockInset(bv->theLockingInset());
-               found = false;
-       }
-       bv->update();
-
-       return found;
+       if (!bv->available())
+               return false;
+       return true;
+           
 }
 
+} // namespace anon
 
-SearchResult find(BufferView * bv, LyXText * text,
-                    string const & searchstr, bool forward,
-                    bool casesens, bool matchwrd)
+
+bool find(BufferView * bv, string const & searchstr,
+         bool cs, bool mw, bool fw)
 {
-       if (text->selection.set())
-               text->cursor = forward ?
-                       text->selection.end : text->selection.start;
+       if (!searchAllowed(bv, searchstr))
+               return false;
+
+       PosIterator cur = PosIterator(*bv);
 
-       text->clearSelection();
+       MatchString match(searchstr, cs, mw);
+       
+       bool found;
 
-       SearchResult result = forward ?
-               searchForward(bv, text, searchstr, casesens, matchwrd) :
-               searchBackward(bv, text, searchstr, casesens, matchwrd);
+       if (fw) {
+               PosIterator const end = bv->buffer()->pos_iterator_end();
+               found = findForward(cur, end, match);
+       } else {
+               PosIterator const beg = bv->buffer()->pos_iterator_begin();
+               found = findBackwards(cur, beg, match);
+       }
+       
+       if (found)
+               put_selection_at(bv, cur, searchstr.length(), !fw);
 
-       return result;
+       return found;
 }
 
+namespace {
+       
+
 
+} //namespace anon
 
 
-SearchResult nextChange(BufferView * bv, LyXText * text, pos_type & length)
+int replaceAll(BufferView * bv,
+              string const & searchstr, string const & replacestr,
+              bool cs, bool mw)
 {
-       ParagraphList::iterator pit = text->cursorPar();
-       ParagraphList::iterator pend = text->ownerParagraphs().end();
-       pos_type pos = text->cursor.pos();
+       Buffer & buf = *bv->buffer();
 
-       while (pit != pend) {
-               pos_type parsize = pit->size();
+       if (!searchAllowed(bv, searchstr) || buf.isReadonly())
+               return 0;
+       
+       recordUndo(Undo::ATOMIC, bv->text, 0,
+                  buf.paragraphs().size() - 1);
+       
+       PosIterator cur = buf.pos_iterator_begin();
+       PosIterator const end = buf.pos_iterator_end();
+       MatchString match(searchstr, cs, mw);
+       int num = 0;
+
+       int const rsize = replacestr.size();
+       int const ssize = searchstr.size();
+       while (findForward(cur, end, match)) {
+               pos_type pos = cur.pos();
+               LyXFont const font
+                       = cur.pit()->getFontSettings(buf.params(), pos);
+               int striked = ssize - cur.pit()->erase(pos, pos + ssize);
+               cur.pit()->insert(pos, replacestr, font);
+               advance(cur, rsize + striked);
+               ++num;
+       }
+       PosIterator beg = buf.pos_iterator_begin();
+       bv->text->init(bv);
+       put_selection_at(bv, beg, 0, false);
+       return num;
+}
 
-               if (pos < parsize) {
-                       if ((!parsize || pos != parsize)
-                               && pit->lookupChange(pos) != Change::UNCHANGED)
-                               break;
 
-                       if (pit->isInset(pos) && pit->getInset(pos)->isTextInset()) {
-                               UpdatableInset * inset = (UpdatableInset *)pit->getInset(pos);
-                               if (inset->nextChange(bv, length))
-                                       return SR_FOUND_NOUPDATE;
-                       }
+int replace(BufferView * bv,
+           string const & searchstr, string const & replacestr,
+           bool cs, bool mw, bool fw)
+{
+       if (!searchAllowed(bv, searchstr) || bv->buffer()->isReadonly())
+               return 0;
+       
+       {
+               LyXText * text = bv->getLyXText();
+               // if nothing selected or selection does not equal search
+               // string search and select next occurance and return
+               string const str1 = searchstr;
+               string const str2 = text->selectionAsString(*bv->buffer(),
+                                                           false);
+               if ((cs && str1 != str2)
+                   || lowercase(str1) != lowercase(str2)) {
+                       find(bv, searchstr, cs, mw, fw);
+                       return 0;
                }
+       }
 
-               ++pos;
-
-               if (pos >= parsize) {
-                       ++pit;
-                       pos = 0;
-               }
+       LyXText * text = bv->getLyXText();
+       // 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()))) {
+               text->replaceSelectionWithString(replacestr);
+               text->setSelectionRange(replacestr.length());
+               text->cursor = fw ? text->selection.end
+                       : text->selection.start;
        }
 
-       if (pit == pend)
-               return SR_NOT_FOUND;
+       // FIXME: should be called via an LFUN
+       bv->buffer()->markDirty();
+
+       find(bv, searchstr, cs, mw, fw);
+       bv->update();
+       
+       return 1;
+}
+
+
+bool findNextChange(BufferView * bv)
+{
+       if (!bv->available())
+               return false;
+
+       PosIterator cur = PosIterator(*bv);
+       PosIterator const endit = bv->buffer()->pos_iterator_end();
 
-       text->setCursor(pit, pos);
+       if (!findChange(cur, endit))
+               return false;
+       
+       
+       ParagraphList::iterator pit = cur.pit();
+       pos_type pos = cur.pos();
+       
        Change orig_change = pit->lookupChangeFull(pos);
        pos_type parsize = pit->size();
        pos_type end = pos;
@@ -349,69 +285,10 @@ SearchResult nextChange(BufferView * bv, LyXText * text, pos_type & length)
                                break;
                }
        }
-       length = end - pos;
-       return SR_FOUND;
-}
-
-
-SearchResult findNextChange(BufferView * bv, LyXText * text, pos_type & length)
-{
-       if (text->selection.set())
-               text->cursor = text->selection.end;
-
-       text->clearSelection();
-
-       return nextChange(bv, text, length);
-}
-
-
-bool findNextChange(BufferView * bv)
-{
-       if (!bv->available())
-               return false;
-
-       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;
-
-       text->clearSelection();
-
-       SearchResult result = nextChange(bv, text, length);
-
-       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());
-               text->setSelectionRange(length);
-       } else if (result == SR_NOT_FOUND) {
-               bv->unlockInset(bv->theLockingInset());
-               found = false;
-       }
-
-       bv->update();
-
-       return found;
+       pos_type length = end - pos;
+       bv->text->init(bv);
+       put_selection_at(bv, cur, length, true);
+       return true;
 }
 
 } // find namespace
index 488804763862df2f9aa79b13c48ffae121079ae2..61e3977053bd935b4bdd1a5df94b525c93a5967b 100644 (file)
@@ -7,6 +7,7 @@
  * \author Lars Gullik Bjønnes
  * \author John Levon
  * \author Jürgen Vigna
+ * \author Alfredo Braunstein
  *
  * Full author contact details are available in file CREDITS.
  */
@@ -24,49 +25,50 @@ class LyXText;
 namespace lyx {
 namespace find {
 
-enum SearchResult {
-       //
-       SR_NOT_FOUND = 0,
-       //
-       SR_FOUND,
-       //
-       SR_FOUND_NOUPDATE
-};
-
+/**
+ * This function replaces an ocurrence of \param search with the
+ * string \param replace
+ *
+ *  \param bv the BufferView in which the search is to be performed,
+ *         starting at the current cursor position.
+ *  \param search the string we're looking for.
+ *  \param replace if \c search is found, replace it with this.
+ *  \param cs perform a case-sensitive search for \c search.
+ *  \param mw match whole words only.
+ *  \param fw search forward from the current cursor position.
+ */
 
 int replace(BufferView * bv,
-              std::string const &, std::string const &,
-              bool, bool = true, bool = false,
-              bool = false, bool = false);
-
+           std::string const & search, std::string const & replace,
+           bool cs, bool mw, bool fw);
 /**
- * This function is called as a general interface to find some
- * text from the actual cursor position in whatever direction
- * we want to go. This does also update the screen.
+ * This function replaces all ocurrences of \param search with
+ * the string \param replace
+ *
+ *  \param bv the BufferView in which the search is to be performed,
+ *         starting at the current cursor position.
+ *  \param search the string we're looking for.
+ *  \param replace if \c search is found, replace it with this.
+ *  \param cs perform a case-sensitive search for \c search.
+ *  \param mw match whole words only.
  */
-bool find(BufferView *,
-            std::string const & searchstr, bool forward,
-            bool casesens = true, bool matchwrd = false);
+
+int replaceAll(BufferView * bv,
+              std::string const & search, std::string const & replace,
+              bool cs, bool mw);
 
 /**
- * This function does search from the cursor position inside the
- * passed LyXText parameter and regards this LyXText as the root
- * LyXText. It will NOT update any screen stuff. It will however
- * set the cursor to the new position inside LyXText, before
- * returning to the calling function.
+ * This function is called as a general interface to find some text
+ * from the actual cursor position in whatever direction we want to
+ * go. This does also update the screen.
  */
+bool find(BufferView *, std::string const & search, 
+         bool cs, bool mw, bool fw);
 
-SearchResult find(BufferView *, LyXText * text,
-                    std::string const & searchstr, bool forward,
-                    bool casesens = true, bool matchwrd = false);
 
 /// find the next change in the buffer
 bool findNextChange(BufferView * bv);
 
-SearchResult findNextChange(BufferView * bv, LyXText * text, lyx::pos_type & length);
-
-SearchResult nextChange(BufferView * bv, LyXText * text, lyx::pos_type & length);
-
 } // namespace find
 } // namespace lyx
 
index 99c68b272fd26fa3cc8e18686a869af27ca1f65d..a4bac22fb7dadafc68621160c4b1f061c35ed74e 100644 (file)
@@ -1087,7 +1087,8 @@ void LyXFunc::dispatch(FuncRequest const & func, bool verbose)
                }
                bool fw = (action == LFUN_WORDFINDFORWARD);
                if (!searched_string.empty())
-                       lyx::find::find(view(), searched_string, fw);
+                       lyx::find::find(view(), searched_string,
+                                       true, false, fw);
                break;
        }
 
index e95ab46dd5420ab2ab670dceb4c5d61738faaad2..aedb75733811d8aaf06aba07a1dc90bdfb97a574 100644 (file)
@@ -257,6 +257,15 @@ int Paragraph::erase(pos_type start, pos_type end)
 }
 
 
+void Paragraph::insert(pos_type start, string const & str,
+                      LyXFont const & font)
+{
+       int size = str.size();
+       for (int i = 0 ; i < size ; ++i)
+               insertChar(start + i, str[i], font);
+}
+
+
 bool Paragraph::checkInsertChar(LyXFont & font)
 {
        if (pimpl_->inset_owner)
index 977520372df3b252e417bb3d459caaf8e6dc7b46..d0338cd4c45656e29003c0cbbb8e7efeaa400735 100644 (file)
@@ -271,6 +271,9 @@ public:
                                        lyx::pos_type endpos,
                                        LyXFont_size def_size) const;
        ///
+       void insert(lyx::pos_type pos, std::string const & str,
+                   LyXFont const & font);
+       ///
        void insertChar(lyx::pos_type pos, value_type c);
        ///
        void insertChar(lyx::pos_type pos, value_type c, LyXFont const &, Change change = Change(Change::INSERTED));