From ea35e1c460c3cbb4a9130aaeaed8e28dc0656961 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Andr=C3=A9=20P=C3=B6nitz?= Date: Thu, 15 Nov 2001 09:51:57 +0000 Subject: [PATCH] make cursor less eager to leave the formula when pressing 'up' or 'down' git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@3030 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/mathed/formula.C | 1 + src/mathed/math_atom.C | 14 +++----- src/mathed/math_atom.h | 2 -- src/mathed/math_cursor.C | 64 ++++++++++++++++------------------- src/mathed/math_exintinset.C | 7 ++-- src/mathed/math_extern.C | 64 +++++++++++++++++++++++++++-------- src/mathed/math_fracbase.C | 32 ++++++++++++++++++ src/mathed/math_fracbase.h | 16 ++++++--- src/mathed/math_inset.C | 24 +++++++++++++ src/mathed/math_inset.h | 8 +++++ src/mathed/math_scriptinset.C | 40 ++++++++++++++++++++++ src/mathed/math_scriptinset.h | 12 +++++-- 12 files changed, 214 insertions(+), 70 deletions(-) diff --git a/src/mathed/formula.C b/src/mathed/formula.C index ce8e4d5bc0..a4441609b2 100644 --- a/src/mathed/formula.C +++ b/src/mathed/formula.C @@ -61,6 +61,7 @@ namespace { lyxerr << "calling: " << full << "\n"; Systemcalls dummy(Systemcalls::System, full, 0); string out = GetFileContents(outfile); + lyx::unlink(outfile); lyxerr << "result: '" << out << "'\n"; return out; } diff --git a/src/mathed/math_atom.C b/src/mathed/math_atom.C index 8a47a3e723..bcf72881cc 100644 --- a/src/mathed/math_atom.C +++ b/src/mathed/math_atom.C @@ -42,10 +42,10 @@ MathAtom::MathAtom(MathAtom const & p) void MathAtom::operator=(MathAtom const & p) { - if (this != &p) { - done(); - copy(p); - } + if (this == &p) + return; + done(); + copy(p); } @@ -85,12 +85,6 @@ MathInset * MathAtom::nucleus() const } -bool MathAtom::hasNucleus() const -{ - return nucleus_; -} - - MathInset * MathAtom::operator->() const { return nucleus(); diff --git a/src/mathed/math_atom.h b/src/mathed/math_atom.h index 543a861ecf..8713c4113b 100644 --- a/src/mathed/math_atom.h +++ b/src/mathed/math_atom.h @@ -43,8 +43,6 @@ public: /// void reset(MathInset * p); /// - bool hasNucleus() const; - /// MathInset * nucleus() const; /// MathInset * operator->() const; diff --git a/src/mathed/math_cursor.C b/src/mathed/math_cursor.C index 7526a637e9..792493ea59 100644 --- a/src/mathed/math_cursor.C +++ b/src/mathed/math_cursor.C @@ -591,25 +591,22 @@ bool MathCursor::up(bool sel) selHandle(sel); if (!selection_) { - // check whether we could move into a superscript - if (hasPrevAtom()) { - MathAtom & p = prevAtom(); - if (p->asScriptInset() && p->asScriptInset()->hasUp()) { - pushRight(p); - idx() = 1; - pos() = size(); - return true; - } + MathInset::idx_type i = 0; + MathInset::pos_type p = 0; + + // check whether we could move into the inset + if (hasPrevAtom() && prevAtom()->idxLastUp(i, p)) { + pushRight(prevAtom()); + idx() = i; + pos() = p; + return true; } - if (hasNextAtom()) { - MathAtom & n = nextAtom(); - if (n->asScriptInset() && n->asScriptInset()->hasUp()) { - pushLeft(n); - idx() = 1; - pos() = 0; - return true; - } + if (hasNextAtom() && nextAtom()->idxFirstUp(i, p)) { + pushLeft(nextAtom()); + idx() = i; + pos() = p; + return true; } } @@ -624,25 +621,22 @@ bool MathCursor::down(bool sel) selHandle(sel); if (!selection_) { - // check whether we could move into a subscript - if (hasPrevAtom()) { - MathAtom & p = prevAtom(); - if (p->asScriptInset() && p->asScriptInset()->hasDown()) { - pushRight(p); - idx() = 0; - pos() = size(); - return true; - } + MathInset::idx_type i = 0; + MathInset::pos_type p = 0; + + // check whether we could move into the inset + if (hasPrevAtom() && prevAtom()->idxLastDown(i, p)) { + pushRight(prevAtom()); + idx() = i; + pos() = p; + return true; } - if (hasNextAtom()) { - MathAtom & n = nextAtom(); - if (n->asScriptInset() && n->asScriptInset()->hasDown()) { - pushLeft(n); - idx() = 0; - pos() = 0; - return true; - } + if (hasNextAtom() && nextAtom()->idxFirstDown(i, p)) { + pushLeft(nextAtom()); + idx() = i; + pos() = p; + return true; } } @@ -1212,6 +1206,8 @@ bool MathCursor::goDown() return true; } + // does the inset know + // if not, apply brute force. int x0; int y0; diff --git a/src/mathed/math_exintinset.C b/src/mathed/math_exintinset.C index 4fce6e180d..23758c6ee7 100644 --- a/src/mathed/math_exintinset.C +++ b/src/mathed/math_exintinset.C @@ -6,7 +6,7 @@ MathExIntInset::MathExIntInset(string const & name) - : MathNestInset(2), symbol_(name) + : MathNestInset(2), symbol_(name), scripts_(new MathScriptInset) {} @@ -36,7 +36,8 @@ void MathExIntInset::symbol(string const & symbol) bool MathExIntInset::hasScripts() const { - return scripts_.hasNucleus(); + // take empty upper bound as "no scripts" + return !scripts_->asScriptInset()->up().data_.empty(); } @@ -45,7 +46,7 @@ void MathExIntInset::normalize(NormalStream & os) const { os << '[' << symbol_.c_str() << ' ' << cell(0) << ' ' << cell(1); if (hasScripts()) - os << scripts_.nucleus(); + os << ' ' << scripts_.nucleus(); os << ']'; } diff --git a/src/mathed/math_extern.C b/src/mathed/math_extern.C index 4846bf146f..d6e0d68603 100644 --- a/src/mathed/math_extern.C +++ b/src/mathed/math_extern.C @@ -271,13 +271,24 @@ void extractFunctions(MathArray & ar) return; lyxerr << "\nFunctions from: " << ar << "\n"; - for (MathArray::size_type i = 0; i < ar.size() - 1; ++i) { + for (MathArray::size_type i = 0; i + 1 < ar.size(); ++i) { MathArray::iterator it = ar.begin() + i; - // is this a function name? + // is this a well known function name? MathFuncInset * func = (*it)->asFuncInset(); - if (!func) + string name; + if (func) + name = func->name(); + else { + // is this a user defined function? + // guess so, if this is a "string" and it is followed by + // a DelimInset + //name = extractString((*it)->nucleus()); + //if (name.size() && it + 1 + //if ((*it + // FIXME continue; + } // do we have an exponent? // simply skippping the postion does the right thing: @@ -293,7 +304,7 @@ void extractFunctions(MathArray & ar) } // create a proper inset as replacement - MathExFuncInset * p = new MathExFuncInset(func->name()); + MathExFuncInset * p = new MathExFuncInset(name); // jt points to the "argument". Get hold of this. MathArray::iterator st = extractArgument(p->cell(0), jt, ar.end()); @@ -339,7 +350,7 @@ void extractIntegrals(MathArray & ar) return; lyxerr << "\nIntegrals from: " << ar << "\n"; - for (MathArray::size_type i = 0; i < ar.size() - 1; ++i) { + for (MathArray::size_type i = 0; i + 1< ar.size(); ++i) { MathArray::iterator it = ar.begin() + i; // is this a integral name? @@ -409,7 +420,7 @@ void extractSums(MathArray & ar) return; lyxerr << "\nSums from: " << ar << "\n"; - for (MathArray::size_type i = 0; i < ar.size() - 1; ++i) { + for (MathArray::size_type i = 0; i + 1< ar.size(); ++i) { MathArray::iterator it = ar.begin() + i; // is this a sum name? @@ -439,7 +450,6 @@ void extractSums(MathArray & ar) } else { // use everything as summation index, don't use scripts. p->cell(1) = ar; - p->scripts().reset(0); } } } @@ -483,7 +493,7 @@ bool diffFracTest(MathInset * p) void extractDiff(MathArray & ar) { lyxerr << "\nDiffs from: " << ar << "\n"; - for (MathArray::size_type i = 0; i < ar.size() - 1; ++i) { + for (MathArray::size_type i = 0; i < ar.size(); ++i) { MathArray::iterator it = ar.begin() + i; // is this a "differential fraction"? @@ -499,15 +509,39 @@ void extractDiff(MathArray & ar) // create a proper diff inset MathDiffInset * p = new MathDiffInset; - // collect function + // collect function, let jt point behind last used item MathArray::iterator jt = it + 1; - if (f->cell(0).size() > 1) - p->cell(0) = MathArray(f->cell(0).begin() + 1, f->cell(0).end()); - else - jt = extractArgument(p->cell(0), jt, ar.end()); - + int n = 1; + MathArray & numer = f->cell(0); + if (numer.size() > 1 && numer.at(1)->asScriptInset()) { + // this is something like d^n f(x) / d... or d^n / d... + n = 1; // FIXME + if (numer.size() > 2) + p->cell(0) = MathArray(numer.begin() + 2, numer.end()); + else + jt = extractArgument(p->cell(0), jt, ar.end()); + } else { + // simply d f(x) / d... or d/d... + if (numer.size() > 1) + p->cell(0) = MathArray(numer.begin() + 1, numer.end()); + else + jt = extractArgument(p->cell(0), jt, ar.end()); + } + // collect denominator - + MathArray & denom = f->cell(1); + for (MathArray::iterator dt = denom.begin(); dt + 1 != denom.end(); ) { + if (!diffItemTest((*dt).nucleus())) { + lyxerr << "extractDiff: should not happen 2\n"; + return; + } + MathArray diff; + dt = extractArgument(diff, dt + 1, denom.end()); + p->addDer(diff); + // safeguard + if (dt == denom.end()) + break; + } // cleanup ar.erase(it + 1, jt); diff --git a/src/mathed/math_fracbase.C b/src/mathed/math_fracbase.C index 5efcc40b26..01745ff548 100644 --- a/src/mathed/math_fracbase.C +++ b/src/mathed/math_fracbase.C @@ -25,6 +25,38 @@ bool MathFracbaseInset::idxLeft(MathInset::idx_type &, } +bool MathFracbaseInset::idxFirstUp(idx_type & idx, pos_type & pos) const +{ + idx = 0; + pos = 0; + return true; +} + + +bool MathFracbaseInset::idxFirstDown(idx_type & idx, pos_type & pos) const +{ + idx = 1; + pos = 0; + return true; +} + + +bool MathFracbaseInset::idxLastUp(idx_type & idx, pos_type & pos) const +{ + idx = 0; + pos = cell(0).size(); + return true; +} + + +bool MathFracbaseInset::idxLastDown(idx_type & idx, pos_type & pos) const +{ + idx = 1; + pos = cell(1).size(); + return true; +} + + bool MathFracbaseInset::idxUp(MathInset::idx_type & idx, MathInset::pos_type & pos) const { diff --git a/src/mathed/math_fracbase.h b/src/mathed/math_fracbase.h index 08f05017d5..035d4f7c4d 100644 --- a/src/mathed/math_fracbase.h +++ b/src/mathed/math_fracbase.h @@ -13,13 +13,21 @@ public: /// MathFracbaseInset(); /// - bool idxUp(MathInset::idx_type &, MathInset::pos_type &) const; + bool idxUp(idx_type &, pos_type &) const; /// - bool idxDown(MathInset::idx_type &, MathInset::pos_type &) const; + bool idxDown(idx_type &, pos_type &) const; /// - bool idxLeft(MathInset::idx_type &, MathInset::pos_type &) const; + bool idxLeft(idx_type &, pos_type &) const; /// - bool idxRight(MathInset::idx_type &, MathInset::pos_type &) const; + bool idxRight(idx_type &, pos_type &) const; + /// + bool idxFirstUp(idx_type &, pos_type &) const; + /// + bool idxFirstDown(idx_type &, pos_type &) const; + /// + bool idxLastUp(idx_type &, pos_type &) const; + /// + bool idxLastDown(idx_type &, pos_type &) const; }; #endif diff --git a/src/mathed/math_inset.C b/src/mathed/math_inset.C index 37000b2583..320d03f269 100644 --- a/src/mathed/math_inset.C +++ b/src/mathed/math_inset.C @@ -132,12 +132,36 @@ bool MathInset::idxFirst(idx_type &, pos_type &) const } +bool MathInset::idxFirstUp(idx_type &, pos_type &) const +{ + return false; +} + + +bool MathInset::idxFirstDown(idx_type &, pos_type &) const +{ + return false; +} + + bool MathInset::idxLast(idx_type &, pos_type &) const { return false; } +bool MathInset::idxLastUp(idx_type &, pos_type &) const +{ + return false; +} + + +bool MathInset::idxLastDown(idx_type &, pos_type &) const +{ + return false; +} + + bool MathInset::idxHome(idx_type &, pos_type &) const { return false; diff --git a/src/mathed/math_inset.h b/src/mathed/math_inset.h index a6587c4398..b617ed20af 100644 --- a/src/mathed/math_inset.h +++ b/src/mathed/math_inset.h @@ -119,8 +119,16 @@ public: /// Target pos when we enter the inset from the left by pressing "Right" virtual bool idxFirst(idx_type & idx, pos_type & pos) const; + /// Target pos when we enter the inset from the left by pressing "Up" + virtual bool idxFirstUp(idx_type & idx, pos_type & pos) const; + /// Target pos when we enter the inset from the left by pressing "Down" + virtual bool idxFirstDown(idx_type & idx, pos_type & pos) const; /// Target pos when we enter the inset from the right by pressing "Left" virtual bool idxLast(idx_type & idx, pos_type & pos) const; + /// Target pos when we enter the inset from the right by pressing "Up" + virtual bool idxLastUp(idx_type & idx, pos_type & pos) const; + /// Target pos when we enter the inset from the right by pressing "Down" + virtual bool idxLastDown(idx_type & idx, pos_type & pos) const; /// Where should we go if we press home? virtual bool idxHome(idx_type & idx, pos_type & pos) const; diff --git a/src/mathed/math_scriptinset.C b/src/mathed/math_scriptinset.C index 1180becf75..b403760e0b 100644 --- a/src/mathed/math_scriptinset.C +++ b/src/mathed/math_scriptinset.C @@ -272,6 +272,46 @@ bool MathScriptInset::idxLeft(MathInset::idx_type &, } +bool MathScriptInset::idxFirstUp(idx_type & idx, pos_type & pos) const +{ + if (!hasUp()) + return false; + idx = 1; + pos = 0; + return true; +} + + +bool MathScriptInset::idxFirstDown(idx_type & idx, pos_type & pos) const +{ + if (!hasDown()) + return false; + idx = 0; + pos = 0; + return true; +} + + +bool MathScriptInset::idxLastUp(idx_type & idx, pos_type & pos) const +{ + if (!hasUp()) + return false; + idx = 1; + pos = up().data_.size(); + return true; +} + + +bool MathScriptInset::idxLastDown(idx_type & idx, pos_type & pos) const +{ + if (!hasDown()) + return false; + idx = 0; + pos = down().data_.size(); + return true; +} + + void MathScriptInset::write(WriteStream & os) const { //lyxerr << "unexpected call to MathScriptInset::write()\n"; diff --git a/src/mathed/math_scriptinset.h b/src/mathed/math_scriptinset.h index 32424933e9..6ecf64f4db 100644 --- a/src/mathed/math_scriptinset.h +++ b/src/mathed/math_scriptinset.h @@ -41,9 +41,17 @@ public: int width(MathInset const * nuc) const; /// - bool idxLeft(MathInset::idx_type &, MathInset::pos_type &) const; + bool idxLeft(idx_type &, pos_type &) const; /// - bool idxRight(MathInset::idx_type &, MathInset::pos_type &) const; + bool idxRight(idx_type &, pos_type &) const; + /// + bool idxFirstUp(idx_type &, pos_type &) const; + /// + bool idxFirstDown(idx_type &, pos_type &) const; + /// + bool idxLastUp(idx_type &, pos_type &) const; + /// + bool idxLastDown(idx_type &, pos_type &) const; /// MathScriptInset const * asScriptInset() const; -- 2.39.2