]> git.lyx.org Git - features.git/commitdiff
simple search-and-replace
authorAndré Pönitz <poenitz@gmx.net>
Fri, 16 Nov 2001 09:55:37 +0000 (09:55 +0000)
committerAndré Pönitz <poenitz@gmx.net>
Fri, 16 Nov 2001 09:55:37 +0000 (09:55 +0000)
git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@3037 a592a061-630c-0410-9148-cb99ea01b6c8

src/mathed/Makefile.am
src/mathed/math_charinset.C
src/mathed/math_charinset.h
src/mathed/math_cursor.C
src/mathed/math_data.C
src/mathed/math_data.h
src/mathed/math_inset.h
src/mathed/math_macro.h
src/mathed/math_nestinset.C
src/mathed/math_nestinset.h

index bdc3367f2ea92ee09b902ed2d68f6b9cb575d20b..31e82e731914d7d61e2c10e52ff8c026c2ffc261 100644 (file)
@@ -94,6 +94,7 @@ libmathed_la_SOURCES = \
        math_notinset.h \
        math_parser.C \
        math_parser.h \
+       math_replace.h \
        math_rootinset.C \
        math_rootinset.h \
        math_scriptinset.C \
index a19d79aef66dd63655b9ef8e3be71328e497fa29..417078bd6d738a268ac5eff17ffe0344f566fdd7 100644 (file)
@@ -118,3 +118,10 @@ void MathCharInset::handleFont(MathTextCodes t)
 {
        code_ = (code_ == t) ? LM_TC_VAR : t;
 }
+
+
+bool MathCharInset::match(MathInset * p) const
+{
+       MathCharInset const * q = p->asCharInset();
+       return q && char_ == q->char_ && code_ == q->code_;
+}
index 4f4978793ea249f4c95d2c3b8d99004f7fc18502..faf298033de712af5c650e3e17ae2c686b9404e6 100644 (file)
@@ -52,6 +52,8 @@ public:
        bool isRelOp() const;
        ///
        void handleFont(MathTextCodes t);
+       ///
+       bool match(MathInset *) const;
 
 private:
        /// the character
index 792493ea596aaf11659c514428dd5caeafa2c9c5..dd6f39e3980a57b0b4433b4f8bfadbb1288f3cdc 100644 (file)
@@ -42,6 +42,8 @@
 #include "math_spaceinset.h"
 #include "math_specialcharinset.h"
 #include "math_mathmlstream.h"
+#include "math_replace.h"
+#include "math_parser.h"
 
 #define FILEDEBUG 0
 
@@ -1272,6 +1274,18 @@ bool MathCursor::interpret(string const & s)
                return true;
        }
 
+       if (s.size() >= 7 && s.substr(0, 7) == "replace") {
+               ReplaceData rep;
+               istringstream is(s.substr(7).c_str());
+               string from, to;
+               is >> from >> to;
+               mathed_parse_cell(rep.from, from);
+               mathed_parse_cell(rep.to, to);
+               lyxerr << "replacing '" << from << "' with '" << to << "'\n";
+               par()->replace(rep);
+               return true;
+       }
+
        if (s == "\\over" || s == "\\choose" || s == "\\atop") {
                MathArray ar = array();
                MathAtom t = createMathInset(s.substr(1));
index 017742014fa77dafbd317433ed45dc51ac4990e3..6e95cdcfceda9dad1212a4ca4c9a41558f784758 100644 (file)
@@ -2,6 +2,7 @@
 #pragma implementation
 #endif
 
+#include "math_data.h"
 #include "math_inset.h"
 #include "math_deliminset.h"
 #include "math_charinset.h"
@@ -10,7 +11,7 @@
 #include "math_matrixinset.h"
 #include "math_mathmlstream.h"
 #include "math_support.h"
-#include "math_data.h"
+#include "math_replace.h"
 #include "debug.h"
 #include "support/LAssert.h"
 
@@ -215,3 +216,25 @@ bool MathArray::match(MathArray const & ar) const
                        return false;
        return true;
 }
+
+
+void MathArray::replace(ReplaceData & rep)
+{
+       for (size_type i = 0; i < size(); ++i) {
+               iterator it = begin() + i;
+               const_iterator rt = rep.from.begin();
+               const_iterator et = rep.from.end();
+               for (const_iterator jt = it; jt != end() && rt != et; ++jt, ++rt)
+                       if (!jt->nucleus()->match(rt->nucleus()))
+                               break;
+               if (rt == et) {
+                       // match found
+                       lyxerr << "match found!\n";
+                       erase(it, it + rep.from.size());
+                       insert(i, rep.to);
+               }
+       }
+
+       for (const_iterator it = begin(); it != end(); ++it)
+               it->nucleus()->replace(rep);
+}
index 4c5b7fbf0426e8ec71405ba4dd6c95b8185cfb88..d6033375264f9af789d695afe34405f825e3808f 100644 (file)
@@ -22,6 +22,7 @@
 
 class MathMacro;
 class LaTeXFeatures;
+class ReplaceData;
 
 
 #ifdef __GNUG__
@@ -104,6 +105,8 @@ public:
        void substitute(MathMacro const &);
        ///
        bool match(MathArray const &) const;
+       ///
+       void replace(ReplaceData &);
 
        ///
        MathAtom & at(size_type pos);
index 9703fa0c9225642ab48547462a20af3183636e34..5b2d88af9fc24672042a8c2b73551ecb8f3ff8c9 100644 (file)
@@ -231,6 +231,8 @@ public:
        virtual void handleFont(MathTextCodes) {}
        ///
        virtual bool match(MathInset *) const { return false; }
+       ///
+       virtual void replace(ReplaceData &) {}
 
        /// write normalized content
        virtual void normalize(NormalStream &) const;
index 036152a984172e505bb5d4ed165ff02fb7c5b5ad..5c5f4bc60ee5590a7d0e57ac2df3778159894cd5 100644 (file)
@@ -63,6 +63,8 @@ public:
        void validate(LaTeXFeatures &) const;
        ///
        bool isMacro() const { return true; }
+       ///
+       bool match(MathInset *) const { return false; }
 
        ///
        void normalize(NormalStream &) const;
index 4d4cfb6152278e8dd5d2f6aa9225f8319ec90a87..09d8116767872476983375b25e8a5d167450d68f 100644 (file)
@@ -189,3 +189,10 @@ bool MathNestInset::match(MathInset * p) const
                        return false;
        return true;
 }
+
+
+void MathNestInset::replace(ReplaceData & rep)
+{
+       for (idx_type i = 0; i < nargs(); ++i)
+               cell(i).replace(rep);
+}
index 0108943bc009a2e441cd389abf27b86a119b3e4b..5945f55e46d74954926bffb6d5a5d9f8a6f9ac5d 100644 (file)
@@ -67,6 +67,8 @@ public:
        void dump() const;
        ///
        bool match(MathInset *) const;
+       ///
+       void replace(ReplaceData &);
 
        ///
        void validate(LaTeXFeatures & features) const;