From a3f60ae91edbbbe66a7e2b941cb31a928a43c5b3 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Andr=C3=A9=20P=C3=B6nitz?= Date: Thu, 3 Jan 2002 09:41:26 +0000 Subject: [PATCH] forward search in math insets. ugly. seems to work. don't ask why. git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@3279 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/mathed/formulabase.C | 63 +++++++++++++++++++++++++++++++++++++- src/mathed/formulabase.h | 10 ++++++ src/mathed/math_cursor.C | 7 +++++ src/mathed/math_cursor.h | 2 ++ src/mathed/math_data.C | 13 ++++++-- src/mathed/math_data.h | 6 +++- src/mathed/math_iterator.C | 14 ++++++++- src/mathed/math_iterator.h | 12 +++++--- src/mathed/math_pos.C | 1 + 9 files changed, 118 insertions(+), 10 deletions(-) diff --git a/src/mathed/formulabase.C b/src/mathed/formulabase.C index 1af5cd2f1d..26a4f1b526 100644 --- a/src/mathed/formulabase.C +++ b/src/mathed/formulabase.C @@ -40,8 +40,10 @@ #include "math_cursor.h" #include "math_factory.h" #include "math_hullinset.h" +#include "math_iterator.h" #include "math_macrotable.h" #include "math_parser.h" +#include "math_pos.h" #include "math_spaceinset.h" #include "undo_funcs.h" @@ -113,6 +115,7 @@ void InsetFormulaBase::metrics(BufferView * bv, LyXFont const & f) const metrics(bv); } + void InsetFormulaBase::metrics(BufferView * bv) const { if (bv) @@ -220,7 +223,7 @@ void InsetFormulaBase::showInsetCursor(BufferView * bv, bool) MathMetricsInfo mi(bv, font_, display() ? LM_ST_DISPLAY : LM_ST_TEXT); math_font_max_dim(LM_TC_TEXTRM, mi, asc, des); bv->fitLockedInsetCursor(x, y, asc, des); - //lyxerr << "showInsetCursor: x: " << x << " y: " << y << "\n"; + //lyxerr << "showInsetCursor: x: " << x << " y: " << y << " yo: " << yo_ << "\n"; } toggleInsetCursor(bv); } @@ -717,6 +720,64 @@ int InsetFormulaBase::xhigh() const ///////////////////////////////////////////////////////////////////// +bool InsetFormulaBase::searchForward(BufferView * bv, string const & str, + bool const &, bool const &) +{ +#ifdef WITH_WARNINGS +#warning pretty ugly +#endif + static InsetFormulaBase * lastformula = 0; + static MathIterator current = MathIterator(ibegin(par().nucleus())); + static MathArray ar; + static string laststr; + + if (lastformula != this || laststr != str) { + //lyxerr << "reset lastformula to " << this << "\n"; + lastformula = this; + laststr = str; + current = ibegin(par().nucleus()); + ar.clear(); + mathed_parse_cell(ar, str); + } else { + ++current; + } + //lyxerr << "searching '" << str << "' in " << this << ar << endl; + + for (MathIterator it = current; it != iend(par().nucleus()); ++it) { + if (it.cell().matchpart(ar, it.position().pos_)) { + mathcursor->setSelection(it.cursor(), ar.size()); + current = it; + it.jump(ar.size()); + // I guess some of the following can go + bv->toggleSelection(true); + hideInsetCursor(bv); + updateLocal(bv, true); + showInsetCursor(bv); + metrics(bv); + return true; + } + } + + //lyxerr << "not found!\n"; + lastformula = 0; + // we have to unlock ourself in this function by default! + // don't ask me why... + bv->unlockInset(this); + return false; +} + + +bool InsetFormulaBase::searchBackward(BufferView * bv, string const & what, + bool const & a, bool const & b) +{ + lyxerr << "searching backward not implemented in mathed" << endl; + return searchForward(bv, what, a, b); +} + + +///////////////////////////////////////////////////////////////////// + + void mathDispatchCreation(BufferView * bv, string const & arg, bool display) { if (bv->available()) { diff --git a/src/mathed/formulabase.h b/src/mathed/formulabase.h index 21e856578c..54e2554956 100644 --- a/src/mathed/formulabase.h +++ b/src/mathed/formulabase.h @@ -102,6 +102,16 @@ public: /// BufferView * view() const { return view_; } + /// + virtual bool searchForward(BufferView *, string const &, + bool const & = true, bool const & = false); + /// + virtual bool searchBackward(BufferView *, string const &, + bool const & = true, bool const & = false); + /// + virtual bool isTextInset() const { return true; } + + private: /// unimplemented void operator=(const InsetFormulaBase &); diff --git a/src/mathed/math_cursor.C b/src/mathed/math_cursor.C index efd13f33b7..24e53be570 100644 --- a/src/mathed/math_cursor.C +++ b/src/mathed/math_cursor.C @@ -1449,3 +1449,10 @@ void MathCursor::stripFromLastEqualSign() } +void MathCursor::setSelection(cursor_type const & where, size_type n) +{ + selection_ = true; + Anchor_ = where; + Cursor_ = where; + cursor().pos_ += n; +} diff --git a/src/mathed/math_cursor.h b/src/mathed/math_cursor.h index 91a52bb009..ef9172a0ad 100644 --- a/src/mathed/math_cursor.h +++ b/src/mathed/math_cursor.h @@ -235,6 +235,8 @@ public: void dump(char const * str) const; /// void stripFromLastEqualSign(); + /// moves on + void setSelection(cursor_type const & where, size_type n); /// friend class Selection; diff --git a/src/mathed/math_data.C b/src/mathed/math_data.C index 69b9bfe69c..faf37c415e 100644 --- a/src/mathed/math_data.C +++ b/src/mathed/math_data.C @@ -211,10 +211,17 @@ MathArray::iterator MathArray::end() bool MathArray::match(MathArray const & ar) const { - if (size() != ar.size()) + return size() == ar.size() && matchpart(ar, 0); +} + + +bool MathArray::matchpart(MathArray const & ar, pos_type pos) const +{ + if (size() < ar.size() + pos) return false; - for (const_iterator it = begin(), jt = ar.begin(); it != end(); ++it, ++jt) - if (!it->nucleus()->match(jt->nucleus())) + const_iterator it = begin() + pos; + for (const_iterator jt = ar.begin(); jt != ar.end(); ++jt, ++it) + if (!jt->nucleus()->match(it->nucleus())) return false; return true; } diff --git a/src/mathed/math_data.h b/src/mathed/math_data.h index 86b8125eda..373396bbdb 100644 --- a/src/mathed/math_data.h +++ b/src/mathed/math_data.h @@ -108,8 +108,12 @@ public: void dump2() const; /// void substitute(MathMacro const &); - /// + /// looks for exact match bool match(MathArray const &) const; + /// looks for inclusion match starting at pos + bool matchpart(MathArray const &, pos_type pos) const; + /// looks for containment + const_iterator find(MathArray const &) const; /// void replace(ReplaceData &); diff --git a/src/mathed/math_iterator.C b/src/mathed/math_iterator.C index f84858abf6..0ba7588063 100644 --- a/src/mathed/math_iterator.C +++ b/src/mathed/math_iterator.C @@ -59,6 +59,12 @@ MathXArray const & MathIterator::xcell() const } +MathArray const & MathIterator::cell() const +{ + return par()->xcell(position().idx_).data_; +} + + MathInset * MathIterator::nextInset() const { if (position().pos_ == xcell().data_.size()) @@ -132,6 +138,13 @@ void MathIterator::operator++() } +void MathIterator::jump(int i) +{ + position().pos_ += i; + lyx::Assert(position().pos_ >= 0); + lyx::Assert(position().pos_ <= cell().size()); +} + bool operator==(MathIterator const & it, MathIterator const & jt) { @@ -151,7 +164,6 @@ bool operator!=(MathIterator const & it, MathIterator const & jt) } - MathIterator ibegin(MathInset * p) { return MathIterator(p); diff --git a/src/mathed/math_iterator.h b/src/mathed/math_iterator.h index d95ff6f00f..03bd047870 100644 --- a/src/mathed/math_iterator.h +++ b/src/mathed/math_iterator.h @@ -18,8 +18,10 @@ public: MathCursorPos const & operator*() const; /// MathCursorPos const & operator->() const; - /// + /// move on one step void operator++(); + /// move on several steps + void jump(int n); /// read access to top most item MathCursorPos const & position() const; /// write access to top most item @@ -32,9 +34,11 @@ public: MathInset * par(); /// helper for iend void goEnd(); - -private: - /// write access to top most item + /// read access to top most item + MathArray const & cell() const; + +private: + /// read access to top most item MathXArray const & xcell() const; /// write access to top most item MathInset * nextInset() const; diff --git a/src/mathed/math_pos.C b/src/mathed/math_pos.C index 4322409656..7b57373f2e 100644 --- a/src/mathed/math_pos.C +++ b/src/mathed/math_pos.C @@ -20,6 +20,7 @@ MathCursorPos::MathCursorPos(MathInset * p) } + MathArray & MathCursorPos::cell(MathArray::idx_type idx) const { lyx::Assert(par_); -- 2.39.2