From: André Pönitz Date: Fri, 16 Nov 2001 09:55:37 +0000 (+0000) Subject: simple search-and-replace X-Git-Tag: 1.6.10~20328 X-Git-Url: https://git.lyx.org/gitweb/?a=commitdiff_plain;h=39924c351c01bc3dafc1f85a4952b6314b37c593;p=features.git simple search-and-replace git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@3037 a592a061-630c-0410-9148-cb99ea01b6c8 --- diff --git a/src/mathed/Makefile.am b/src/mathed/Makefile.am index bdc3367f2e..31e82e7319 100644 --- a/src/mathed/Makefile.am +++ b/src/mathed/Makefile.am @@ -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 \ diff --git a/src/mathed/math_charinset.C b/src/mathed/math_charinset.C index a19d79aef6..417078bd6d 100644 --- a/src/mathed/math_charinset.C +++ b/src/mathed/math_charinset.C @@ -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_; +} diff --git a/src/mathed/math_charinset.h b/src/mathed/math_charinset.h index 4f4978793e..faf298033d 100644 --- a/src/mathed/math_charinset.h +++ b/src/mathed/math_charinset.h @@ -52,6 +52,8 @@ public: bool isRelOp() const; /// void handleFont(MathTextCodes t); + /// + bool match(MathInset *) const; private: /// the character diff --git a/src/mathed/math_cursor.C b/src/mathed/math_cursor.C index 792493ea59..dd6f39e398 100644 --- a/src/mathed/math_cursor.C +++ b/src/mathed/math_cursor.C @@ -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)); diff --git a/src/mathed/math_data.C b/src/mathed/math_data.C index 017742014f..6e95cdcfce 100644 --- a/src/mathed/math_data.C +++ b/src/mathed/math_data.C @@ -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); +} diff --git a/src/mathed/math_data.h b/src/mathed/math_data.h index 4c5b7fbf04..d603337526 100644 --- a/src/mathed/math_data.h +++ b/src/mathed/math_data.h @@ -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); diff --git a/src/mathed/math_inset.h b/src/mathed/math_inset.h index 9703fa0c92..5b2d88af9f 100644 --- a/src/mathed/math_inset.h +++ b/src/mathed/math_inset.h @@ -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; diff --git a/src/mathed/math_macro.h b/src/mathed/math_macro.h index 036152a984..5c5f4bc60e 100644 --- a/src/mathed/math_macro.h +++ b/src/mathed/math_macro.h @@ -63,6 +63,8 @@ public: void validate(LaTeXFeatures &) const; /// bool isMacro() const { return true; } + /// + bool match(MathInset *) const { return false; } /// void normalize(NormalStream &) const; diff --git a/src/mathed/math_nestinset.C b/src/mathed/math_nestinset.C index 4d4cfb6152..09d8116767 100644 --- a/src/mathed/math_nestinset.C +++ b/src/mathed/math_nestinset.C @@ -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); +} diff --git a/src/mathed/math_nestinset.h b/src/mathed/math_nestinset.h index 0108943bc0..5945f55e46 100644 --- a/src/mathed/math_nestinset.h +++ b/src/mathed/math_nestinset.h @@ -67,6 +67,8 @@ public: void dump() const; /// bool match(MathInset *) const; + /// + void replace(ReplaceData &); /// void validate(LaTeXFeatures & features) const;