simple search-and-replace

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@3037 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
André Pönitz 2001-11-16 09:55:37 +00:00
parent af2a6c6512
commit 39924c351c
10 changed files with 64 additions and 1 deletions

View 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 \

View 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_;
}

View File

@ -52,6 +52,8 @@ public:
bool isRelOp() const;
///
void handleFont(MathTextCodes t);
///
bool match(MathInset *) const;
private:
/// the character

View 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));

View 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);
}

View 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);

View 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;

View 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;

View 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);
}

View File

@ -67,6 +67,8 @@ public:
void dump() const;
///
bool match(MathInset *) const;
///
void replace(ReplaceData &);
///
void validate(LaTeXFeatures & features) const;